]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/skbuff.c
[NET] BRIDGE: Fix whitespace errors.
[net-next-2.6.git] / net / core / skbuff.c
CommitLineData
1da177e4
LT
1/*
2 * Routines having to do with the 'struct sk_buff' memory handlers.
3 *
4 * Authors: Alan Cox <iiitac@pyr.swan.ac.uk>
5 * Florian La Roche <rzsfl@rz.uni-sb.de>
6 *
7 * Version: $Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
8 *
9 * Fixes:
10 * Alan Cox : Fixed the worst of the load
11 * balancer bugs.
12 * Dave Platt : Interrupt stacking fix.
13 * Richard Kooijman : Timestamp fixes.
14 * Alan Cox : Changed buffer format.
15 * Alan Cox : destructor hook for AF_UNIX etc.
16 * Linus Torvalds : Better skb_clone.
17 * Alan Cox : Added skb_copy.
18 * Alan Cox : Added all the changed routines Linus
19 * only put in the headers
20 * Ray VanTassle : Fixed --skb->lock in free
21 * Alan Cox : skb_copy copy arp field
22 * Andi Kleen : slabified it.
23 * Robert Olsson : Removed skb_head_pool
24 *
25 * NOTE:
26 * The __skb_ routines should be called with interrupts
27 * disabled, or you better be *real* sure that the operation is atomic
28 * with respect to whatever list is being frobbed (e.g. via lock_sock()
29 * or via disabling bottom half handlers, etc).
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36
37/*
38 * The functions in this file will not compile correctly with gcc 2.4.x
39 */
40
1da177e4
LT
41#include <linux/module.h>
42#include <linux/types.h>
43#include <linux/kernel.h>
44#include <linux/sched.h>
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/in.h>
48#include <linux/inet.h>
49#include <linux/slab.h>
50#include <linux/netdevice.h>
51#ifdef CONFIG_NET_CLS_ACT
52#include <net/pkt_sched.h>
53#endif
54#include <linux/string.h>
55#include <linux/skbuff.h>
56#include <linux/cache.h>
57#include <linux/rtnetlink.h>
58#include <linux/init.h>
1da177e4
LT
59
60#include <net/protocol.h>
61#include <net/dst.h>
62#include <net/sock.h>
63#include <net/checksum.h>
64#include <net/xfrm.h>
65
66#include <asm/uaccess.h>
67#include <asm/system.h>
68
a1f8e7f7
AV
69#include "kmap_skb.h"
70
e18b890b
CL
71static struct kmem_cache *skbuff_head_cache __read_mostly;
72static struct kmem_cache *skbuff_fclone_cache __read_mostly;
1da177e4
LT
73
74/*
75 * Keep out-of-line to prevent kernel bloat.
76 * __builtin_return_address is not used because it is not always
77 * reliable.
78 */
79
80/**
81 * skb_over_panic - private function
82 * @skb: buffer
83 * @sz: size
84 * @here: address
85 *
86 * Out of line support code for skb_put(). Not user callable.
87 */
88void skb_over_panic(struct sk_buff *skb, int sz, void *here)
89{
26095455
PM
90 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
91 "data:%p tail:%p end:%p dev:%s\n",
92 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
93 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
94 BUG();
95}
96
97/**
98 * skb_under_panic - private function
99 * @skb: buffer
100 * @sz: size
101 * @here: address
102 *
103 * Out of line support code for skb_push(). Not user callable.
104 */
105
106void skb_under_panic(struct sk_buff *skb, int sz, void *here)
107{
26095455
PM
108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
109 "data:%p tail:%p end:%p dev:%s\n",
110 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
111 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
112 BUG();
113}
114
dc6de336
DM
115void skb_truesize_bug(struct sk_buff *skb)
116{
117 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
118 "len=%u, sizeof(sk_buff)=%Zd\n",
119 skb->truesize, skb->len, sizeof(struct sk_buff));
120}
121EXPORT_SYMBOL(skb_truesize_bug);
122
1da177e4
LT
123/* Allocate a new skbuff. We do this ourselves so we can fill in a few
124 * 'private' fields and also do memory statistics to find all the
125 * [BEEP] leaks.
126 *
127 */
128
129/**
d179cd12 130 * __alloc_skb - allocate a network buffer
1da177e4
LT
131 * @size: size to allocate
132 * @gfp_mask: allocation mask
c83c2486
RD
133 * @fclone: allocate from fclone cache instead of head cache
134 * and allocate a cloned (child) skb
b30973f8 135 * @node: numa node to allocate memory on
1da177e4
LT
136 *
137 * Allocate a new &sk_buff. The returned buffer has no headroom and a
138 * tail room of size bytes. The object has a reference count of one.
139 * The return is the buffer. On a failure the return is %NULL.
140 *
141 * Buffers may only be allocated from interrupts using a @gfp_mask of
142 * %GFP_ATOMIC.
143 */
dd0fc66f 144struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
b30973f8 145 int fclone, int node)
1da177e4 146{
e18b890b 147 struct kmem_cache *cache;
4947d3ef 148 struct skb_shared_info *shinfo;
1da177e4
LT
149 struct sk_buff *skb;
150 u8 *data;
151
8798b3fb
HX
152 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
153
1da177e4 154 /* Get the HEAD */
b30973f8 155 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
156 if (!skb)
157 goto out;
158
159 /* Get the DATA. Size must match skb_add_mtu(). */
160 size = SKB_DATA_ALIGN(size);
b30973f8
CH
161 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
162 gfp_mask, node);
1da177e4
LT
163 if (!data)
164 goto nodata;
165
166 memset(skb, 0, offsetof(struct sk_buff, truesize));
167 skb->truesize = size + sizeof(struct sk_buff);
168 atomic_set(&skb->users, 1);
169 skb->head = data;
170 skb->data = data;
171 skb->tail = data;
172 skb->end = data + size;
4947d3ef
BL
173 /* make sure we initialize shinfo sequentially */
174 shinfo = skb_shinfo(skb);
175 atomic_set(&shinfo->dataref, 1);
176 shinfo->nr_frags = 0;
7967168c
HX
177 shinfo->gso_size = 0;
178 shinfo->gso_segs = 0;
179 shinfo->gso_type = 0;
4947d3ef
BL
180 shinfo->ip6_frag_id = 0;
181 shinfo->frag_list = NULL;
182
d179cd12
DM
183 if (fclone) {
184 struct sk_buff *child = skb + 1;
185 atomic_t *fclone_ref = (atomic_t *) (child + 1);
1da177e4 186
d179cd12
DM
187 skb->fclone = SKB_FCLONE_ORIG;
188 atomic_set(fclone_ref, 1);
189
190 child->fclone = SKB_FCLONE_UNAVAILABLE;
191 }
1da177e4
LT
192out:
193 return skb;
194nodata:
8798b3fb 195 kmem_cache_free(cache, skb);
1da177e4
LT
196 skb = NULL;
197 goto out;
198}
199
200/**
201 * alloc_skb_from_cache - allocate a network buffer
202 * @cp: kmem_cache from which to allocate the data area
203 * (object size must be big enough for @size bytes + skb overheads)
204 * @size: size to allocate
205 * @gfp_mask: allocation mask
206 *
207 * Allocate a new &sk_buff. The returned buffer has no headroom and
208 * tail room of size bytes. The object has a reference count of one.
209 * The return is the buffer. On a failure the return is %NULL.
210 *
211 * Buffers may only be allocated from interrupts using a @gfp_mask of
212 * %GFP_ATOMIC.
213 */
e18b890b 214struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
86a76caf 215 unsigned int size,
dd0fc66f 216 gfp_t gfp_mask)
1da177e4
LT
217{
218 struct sk_buff *skb;
219 u8 *data;
220
221 /* Get the HEAD */
222 skb = kmem_cache_alloc(skbuff_head_cache,
223 gfp_mask & ~__GFP_DMA);
224 if (!skb)
225 goto out;
226
227 /* Get the DATA. */
228 size = SKB_DATA_ALIGN(size);
229 data = kmem_cache_alloc(cp, gfp_mask);
230 if (!data)
231 goto nodata;
232
233 memset(skb, 0, offsetof(struct sk_buff, truesize));
234 skb->truesize = size + sizeof(struct sk_buff);
235 atomic_set(&skb->users, 1);
236 skb->head = data;
237 skb->data = data;
238 skb->tail = data;
239 skb->end = data + size;
240
241 atomic_set(&(skb_shinfo(skb)->dataref), 1);
242 skb_shinfo(skb)->nr_frags = 0;
7967168c
HX
243 skb_shinfo(skb)->gso_size = 0;
244 skb_shinfo(skb)->gso_segs = 0;
245 skb_shinfo(skb)->gso_type = 0;
1da177e4
LT
246 skb_shinfo(skb)->frag_list = NULL;
247out:
248 return skb;
249nodata:
250 kmem_cache_free(skbuff_head_cache, skb);
251 skb = NULL;
252 goto out;
253}
254
8af27456
CH
255/**
256 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
257 * @dev: network device to receive on
258 * @length: length to allocate
259 * @gfp_mask: get_free_pages mask, passed to alloc_skb
260 *
261 * Allocate a new &sk_buff and assign it a usage count of one. The
262 * buffer has unspecified headroom built in. Users should allocate
263 * the headroom they think they need without accounting for the
264 * built in space. The built in space is used for optimisations.
265 *
266 * %NULL is returned if there is no free memory.
267 */
268struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
269 unsigned int length, gfp_t gfp_mask)
270{
43cb76d9 271 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
8af27456
CH
272 struct sk_buff *skb;
273
b30973f8 274 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
7b2e497a 275 if (likely(skb)) {
8af27456 276 skb_reserve(skb, NET_SKB_PAD);
7b2e497a
CH
277 skb->dev = dev;
278 }
8af27456
CH
279 return skb;
280}
1da177e4 281
27b437c8 282static void skb_drop_list(struct sk_buff **listp)
1da177e4 283{
27b437c8 284 struct sk_buff *list = *listp;
1da177e4 285
27b437c8 286 *listp = NULL;
1da177e4
LT
287
288 do {
289 struct sk_buff *this = list;
290 list = list->next;
291 kfree_skb(this);
292 } while (list);
293}
294
27b437c8
HX
295static inline void skb_drop_fraglist(struct sk_buff *skb)
296{
297 skb_drop_list(&skb_shinfo(skb)->frag_list);
298}
299
1da177e4
LT
300static void skb_clone_fraglist(struct sk_buff *skb)
301{
302 struct sk_buff *list;
303
304 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
305 skb_get(list);
306}
307
5bba1712 308static void skb_release_data(struct sk_buff *skb)
1da177e4
LT
309{
310 if (!skb->cloned ||
311 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
312 &skb_shinfo(skb)->dataref)) {
313 if (skb_shinfo(skb)->nr_frags) {
314 int i;
315 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
316 put_page(skb_shinfo(skb)->frags[i].page);
317 }
318
319 if (skb_shinfo(skb)->frag_list)
320 skb_drop_fraglist(skb);
321
322 kfree(skb->head);
323 }
324}
325
326/*
327 * Free an skbuff by memory without cleaning the state.
328 */
329void kfree_skbmem(struct sk_buff *skb)
330{
d179cd12
DM
331 struct sk_buff *other;
332 atomic_t *fclone_ref;
333
1da177e4 334 skb_release_data(skb);
d179cd12
DM
335 switch (skb->fclone) {
336 case SKB_FCLONE_UNAVAILABLE:
337 kmem_cache_free(skbuff_head_cache, skb);
338 break;
339
340 case SKB_FCLONE_ORIG:
341 fclone_ref = (atomic_t *) (skb + 2);
342 if (atomic_dec_and_test(fclone_ref))
343 kmem_cache_free(skbuff_fclone_cache, skb);
344 break;
345
346 case SKB_FCLONE_CLONE:
347 fclone_ref = (atomic_t *) (skb + 1);
348 other = skb - 1;
349
350 /* The clone portion is available for
351 * fast-cloning again.
352 */
353 skb->fclone = SKB_FCLONE_UNAVAILABLE;
354
355 if (atomic_dec_and_test(fclone_ref))
356 kmem_cache_free(skbuff_fclone_cache, other);
357 break;
358 };
1da177e4
LT
359}
360
361/**
362 * __kfree_skb - private function
363 * @skb: buffer
364 *
365 * Free an sk_buff. Release anything attached to the buffer.
366 * Clean the state. This is an internal helper function. Users should
367 * always call kfree_skb
368 */
369
370void __kfree_skb(struct sk_buff *skb)
371{
1da177e4
LT
372 dst_release(skb->dst);
373#ifdef CONFIG_XFRM
374 secpath_put(skb->sp);
375#endif
9c2b3328
SH
376 if (skb->destructor) {
377 WARN_ON(in_irq());
1da177e4
LT
378 skb->destructor(skb);
379 }
380#ifdef CONFIG_NETFILTER
381 nf_conntrack_put(skb->nfct);
9fb9cbb1
YK
382#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
383 nf_conntrack_put_reasm(skb->nfct_reasm);
384#endif
1da177e4
LT
385#ifdef CONFIG_BRIDGE_NETFILTER
386 nf_bridge_put(skb->nf_bridge);
387#endif
388#endif
389/* XXX: IS this still necessary? - JHS */
390#ifdef CONFIG_NET_SCHED
391 skb->tc_index = 0;
392#ifdef CONFIG_NET_CLS_ACT
393 skb->tc_verd = 0;
1da177e4
LT
394#endif
395#endif
396
397 kfree_skbmem(skb);
398}
399