]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/skbuff.c
[NET]: Annotate callers of the reset of checksum.h stuff.
[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>
59#include <linux/highmem.h>
60
61#include <net/protocol.h>
62#include <net/dst.h>
63#include <net/sock.h>
64#include <net/checksum.h>
65#include <net/xfrm.h>
66
67#include <asm/uaccess.h>
68#include <asm/system.h>
69
ba89966c
ED
70static kmem_cache_t *skbuff_head_cache __read_mostly;
71static kmem_cache_t *skbuff_fclone_cache __read_mostly;
1da177e4
LT
72
73/*
74 * Keep out-of-line to prevent kernel bloat.
75 * __builtin_return_address is not used because it is not always
76 * reliable.
77 */
78
79/**
80 * skb_over_panic - private function
81 * @skb: buffer
82 * @sz: size
83 * @here: address
84 *
85 * Out of line support code for skb_put(). Not user callable.
86 */
87void skb_over_panic(struct sk_buff *skb, int sz, void *here)
88{
26095455
PM
89 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
90 "data:%p tail:%p end:%p dev:%s\n",
91 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
92 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
93 BUG();
94}
95
96/**
97 * skb_under_panic - private function
98 * @skb: buffer
99 * @sz: size
100 * @here: address
101 *
102 * Out of line support code for skb_push(). Not user callable.
103 */
104
105void skb_under_panic(struct sk_buff *skb, int sz, void *here)
106{
26095455
PM
107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
108 "data:%p tail:%p end:%p dev:%s\n",
109 here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
110 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
111 BUG();
112}
113
dc6de336
DM
114void skb_truesize_bug(struct sk_buff *skb)
115{
116 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117 "len=%u, sizeof(sk_buff)=%Zd\n",
118 skb->truesize, skb->len, sizeof(struct sk_buff));
119}
120EXPORT_SYMBOL(skb_truesize_bug);
121
1da177e4
LT
122/* Allocate a new skbuff. We do this ourselves so we can fill in a few
123 * 'private' fields and also do memory statistics to find all the
124 * [BEEP] leaks.
125 *
126 */
127
128/**
d179cd12 129 * __alloc_skb - allocate a network buffer
1da177e4
LT
130 * @size: size to allocate
131 * @gfp_mask: allocation mask
c83c2486
RD
132 * @fclone: allocate from fclone cache instead of head cache
133 * and allocate a cloned (child) skb
1da177e4
LT
134 *
135 * Allocate a new &sk_buff. The returned buffer has no headroom and a
136 * tail room of size bytes. The object has a reference count of one.
137 * The return is the buffer. On a failure the return is %NULL.
138 *
139 * Buffers may only be allocated from interrupts using a @gfp_mask of
140 * %GFP_ATOMIC.
141 */
dd0fc66f 142struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
d179cd12 143 int fclone)
1da177e4 144{
8798b3fb 145 kmem_cache_t *cache;
4947d3ef 146 struct skb_shared_info *shinfo;
1da177e4
LT
147 struct sk_buff *skb;
148 u8 *data;
149
8798b3fb
HX
150 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
151
1da177e4 152 /* Get the HEAD */
8798b3fb 153 skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
1da177e4
LT
154 if (!skb)
155 goto out;
156
157 /* Get the DATA. Size must match skb_add_mtu(). */
158 size = SKB_DATA_ALIGN(size);
1d2c8eea
CH
159 data = kmalloc_track_caller(size + sizeof(struct skb_shared_info),
160 gfp_mask);
1da177e4
LT
161 if (!data)
162 goto nodata;
163
164 memset(skb, 0, offsetof(struct sk_buff, truesize));
165 skb->truesize = size + sizeof(struct sk_buff);
166 atomic_set(&skb->users, 1);
167 skb->head = data;
168 skb->data = data;
169 skb->tail = data;
170 skb->end = data + size;
4947d3ef
BL
171 /* make sure we initialize shinfo sequentially */
172 shinfo = skb_shinfo(skb);
173 atomic_set(&shinfo->dataref, 1);
174 shinfo->nr_frags = 0;
7967168c
HX
175 shinfo->gso_size = 0;
176 shinfo->gso_segs = 0;
177 shinfo->gso_type = 0;
4947d3ef
BL
178 shinfo->ip6_frag_id = 0;
179 shinfo->frag_list = NULL;
180
d179cd12
DM
181 if (fclone) {
182 struct sk_buff *child = skb + 1;
183 atomic_t *fclone_ref = (atomic_t *) (child + 1);
1da177e4 184
d179cd12
DM
185 skb->fclone = SKB_FCLONE_ORIG;
186 atomic_set(fclone_ref, 1);
187
188 child->fclone = SKB_FCLONE_UNAVAILABLE;
189 }
1da177e4
LT
190out:
191 return skb;
192nodata:
8798b3fb 193 kmem_cache_free(cache, skb);
1da177e4
LT
194 skb = NULL;
195 goto out;
196}
197
198/**
199 * alloc_skb_from_cache - allocate a network buffer
200 * @cp: kmem_cache from which to allocate the data area
201 * (object size must be big enough for @size bytes + skb overheads)
202 * @size: size to allocate
203 * @gfp_mask: allocation mask
204 *
205 * Allocate a new &sk_buff. The returned buffer has no headroom and
206 * tail room of size bytes. The object has a reference count of one.
207 * The return is the buffer. On a failure the return is %NULL.
208 *
209 * Buffers may only be allocated from interrupts using a @gfp_mask of
210 * %GFP_ATOMIC.
211 */
212struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
86a76caf 213 unsigned int size,
dd0fc66f 214 gfp_t gfp_mask)
1da177e4
LT
215{
216 struct sk_buff *skb;
217 u8 *data;
218
219 /* Get the HEAD */
220 skb = kmem_cache_alloc(skbuff_head_cache,
221 gfp_mask & ~__GFP_DMA);
222 if (!skb)
223 goto out;
224
225 /* Get the DATA. */
226 size = SKB_DATA_ALIGN(size);
227 data = kmem_cache_alloc(cp, gfp_mask);
228 if (!data)
229 goto nodata;
230
231 memset(skb, 0, offsetof(struct sk_buff, truesize));
232 skb->truesize = size + sizeof(struct sk_buff);
233 atomic_set(&skb->users, 1);
234 skb->head = data;
235 skb->data = data;
236 skb->tail = data;
237 skb->end = data + size;
238
239 atomic_set(&(skb_shinfo(skb)->dataref), 1);
240 skb_shinfo(skb)->nr_frags = 0;
7967168c
HX
241 skb_shinfo(skb)->gso_size = 0;
242 skb_shinfo(skb)->gso_segs = 0;
243 skb_shinfo(skb)->gso_type = 0;
1da177e4
LT
244 skb_shinfo(skb)->frag_list = NULL;
245out:
246 return skb;
247nodata:
248 kmem_cache_free(skbuff_head_cache, skb);
249 skb = NULL;
250 goto out;
251}
252
8af27456
CH
253/**
254 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
255 * @dev: network device to receive on
256 * @length: length to allocate
257 * @gfp_mask: get_free_pages mask, passed to alloc_skb
258 *
259 * Allocate a new &sk_buff and assign it a usage count of one. The
260 * buffer has unspecified headroom built in. Users should allocate
261 * the headroom they think they need without accounting for the
262 * built in space. The built in space is used for optimisations.
263 *
264 * %NULL is returned if there is no free memory.
265 */
266struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
267 unsigned int length, gfp_t gfp_mask)
268{
269 struct sk_buff *skb;
270
271 skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
7b2e497a 272 if (likely(skb)) {
8af27456 273 skb_reserve(skb, NET_SKB_PAD);
7b2e497a
CH
274 skb->dev = dev;
275 }
8af27456
CH
276 return skb;
277}
1da177e4 278
27b437c8 279static void skb_drop_list(struct sk_buff **listp)
1da177e4 280{
27b437c8 281 struct sk_buff *list = *listp;
1da177e4 282
27b437c8 283 *listp = NULL;
1da177e4
LT
284
285 do {
286 struct sk_buff *this = list;
287 list = list->next;
288 kfree_skb(this);
289 } while (list);
290}
291
27b437c8
HX
292static inline void skb_drop_fraglist(struct sk_buff *skb)
293{
294 skb_drop_list(&skb_shinfo(skb)->frag_list);
295}
296
1da177e4
LT
297static void skb_clone_fraglist(struct sk_buff *skb)
298{
299 struct sk_buff *list;
300
301 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
302 skb_get(list);
303}
304
5bba1712 305static void skb_release_data(struct sk_buff *skb)
1da177e4
LT
306{
307 if (!skb->cloned ||
308 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
309 &skb_shinfo(skb)->dataref)) {
310 if (skb_shinfo(skb)->nr_frags) {
311 int i;
312 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
313 put_page(skb_shinfo(skb)->frags[i].page);
314 }
315
316 if (skb_shinfo(skb)->frag_list)
317 skb_drop_fraglist(skb);
318
319 kfree(skb->head);
320 }
321}
322
323/*
324 * Free an skbuff by memory without cleaning the state.
325 */
326void kfree_skbmem(struct sk_buff *skb)
327{
d179cd12
DM
328 struct sk_buff *other;
329 atomic_t *fclone_ref;
330
1da177e4 331 skb_release_data(skb);
d179cd12
DM
332 switch (skb->fclone) {
333 case SKB_FCLONE_UNAVAILABLE:
334 kmem_cache_free(skbuff_head_cache, skb);
335 break;
336
337 case SKB_FCLONE_ORIG:
338 fclone_ref = (atomic_t *) (skb + 2);
339 if (atomic_dec_and_test(fclone_ref))
340 kmem_cache_free(skbuff_fclone_cache, skb);
341 break;
342
343 case SKB_FCLONE_CLONE:
344 fclone_ref = (atomic_t *) (skb + 1);
345 other = skb - 1;
346
347 /* The clone portion is available for
348 * fast-cloning again.
349 */
350 skb->fclone = SKB_FCLONE_UNAVAILABLE;
351
352 if (atomic_dec_and_test(fclone_ref))
353 kmem_cache_free(skbuff_fclone_cache, other);
354 break;
355 };
1da177e4
LT
356}
357
358/**
359 * __kfree_skb - private function
360 * @skb: buffer
361 *
362 * Free an sk_buff. Release anything attached to the buffer.
363 * Clean the state. This is an internal helper function. Users should
364 * always call kfree_skb
365 */
366
367void __kfree_skb(struct sk_buff *skb)
368{
1da177e4
LT
369 dst_release(skb->dst);
370#ifdef CONFIG_XFRM
371 secpath_put(skb->sp);
372#endif
9c2b3328
SH
373 if (skb->destructor) {
374 WARN_ON(in_irq());
1da177e4
LT
375 skb->destructor(skb);
376 }
377#ifdef CONFIG_NETFILTER
378 nf_conntrack_put(skb->nfct);
9fb9cbb1
YK
379#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
380 nf_conntrack_put_reasm(skb->nfct_reasm);
381#endif
1da177e4
LT
382#ifdef CONFIG_BRIDGE_NETFILTER
383 nf_bridge_put(skb->nf_bridge);
384#endif
385#endif
386/* XXX: IS this still necessary? - JHS */
387#ifdef CONFIG_NET_SCHED
388 skb->tc_index = 0;
389#ifdef CONFIG_NET_CLS_ACT
390 skb->tc_verd = 0;
1da177e4
LT
391#endif
392#endif
393
394 kfree_skbmem(skb);
395}
396