Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[deliverable/linux.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>
1da177e4
LT
44#include <linux/mm.h>
45#include <linux/interrupt.h>
46#include <linux/in.h>
47#include <linux/inet.h>
48#include <linux/slab.h>
49#include <linux/netdevice.h>
50#ifdef CONFIG_NET_CLS_ACT
51#include <net/pkt_sched.h>
52#endif
53#include <linux/string.h>
54#include <linux/skbuff.h>
55#include <linux/cache.h>
56#include <linux/rtnetlink.h>
57#include <linux/init.h>
1da177e4
LT
58
59#include <net/protocol.h>
60#include <net/dst.h>
61#include <net/sock.h>
62#include <net/checksum.h>
63#include <net/xfrm.h>
64
65#include <asm/uaccess.h>
66#include <asm/system.h>
67
a1f8e7f7
AV
68#include "kmap_skb.h"
69
e18b890b
CL
70static struct kmem_cache *skbuff_head_cache __read_mostly;
71static struct kmem_cache *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 89 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
4ec93edb 90 "data:%p tail:%p end:%p dev:%s\n",
26095455
PM
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 107 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
4ec93edb 108 "data:%p tail:%p end:%p dev:%s\n",
26095455
PM
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
b30973f8 134 * @node: numa node to allocate memory on
1da177e4
LT
135 *
136 * Allocate a new &sk_buff. The returned buffer has no headroom and a
137 * tail room of size bytes. The object has a reference count of one.
138 * The return is the buffer. On a failure the return is %NULL.
139 *
140 * Buffers may only be allocated from interrupts using a @gfp_mask of
141 * %GFP_ATOMIC.
142 */
dd0fc66f 143struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
b30973f8 144 int fclone, int node)
1da177e4 145{
e18b890b 146 struct kmem_cache *cache;
4947d3ef 147 struct skb_shared_info *shinfo;
1da177e4
LT
148 struct sk_buff *skb;
149 u8 *data;
150
8798b3fb
HX
151 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
152
1da177e4 153 /* Get the HEAD */
b30973f8 154 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
155 if (!skb)
156 goto out;
157
158 /* Get the DATA. Size must match skb_add_mtu(). */
159 size = SKB_DATA_ALIGN(size);
b30973f8
CH
160 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
161 gfp_mask, node);
1da177e4
LT
162 if (!data)
163 goto nodata;
164
165 memset(skb, 0, offsetof(struct sk_buff, truesize));
166 skb->truesize = size + sizeof(struct sk_buff);
167 atomic_set(&skb->users, 1);
168 skb->head = data;
169 skb->data = data;
170 skb->tail = data;
171 skb->end = data + size;
4947d3ef
BL
172 /* make sure we initialize shinfo sequentially */
173 shinfo = skb_shinfo(skb);
174 atomic_set(&shinfo->dataref, 1);
175 shinfo->nr_frags = 0;
7967168c
HX
176 shinfo->gso_size = 0;
177 shinfo->gso_segs = 0;
178 shinfo->gso_type = 0;
4947d3ef
BL
179 shinfo->ip6_frag_id = 0;
180 shinfo->frag_list = NULL;
181
d179cd12
DM
182 if (fclone) {
183 struct sk_buff *child = skb + 1;
184 atomic_t *fclone_ref = (atomic_t *) (child + 1);
1da177e4 185
d179cd12
DM
186 skb->fclone = SKB_FCLONE_ORIG;
187 atomic_set(fclone_ref, 1);
188
189 child->fclone = SKB_FCLONE_UNAVAILABLE;
190 }
1da177e4
LT
191out:
192 return skb;
193nodata:
8798b3fb 194 kmem_cache_free(cache, skb);
1da177e4
LT
195 skb = NULL;
196 goto out;
197}
198
199/**
200 * alloc_skb_from_cache - allocate a network buffer
201 * @cp: kmem_cache from which to allocate the data area
202 * (object size must be big enough for @size bytes + skb overheads)
203 * @size: size to allocate
204 * @gfp_mask: allocation mask
205 *
206 * Allocate a new &sk_buff. The returned buffer has no headroom and
207 * tail room of size bytes. The object has a reference count of one.
208 * The return is the buffer. On a failure the return is %NULL.
209 *
210 * Buffers may only be allocated from interrupts using a @gfp_mask of
211 * %GFP_ATOMIC.
212 */
e18b890b 213struct sk_buff *alloc_skb_from_cache(struct kmem_cache *cp,
86a76caf 214 unsigned int size,
dd0fc66f 215 gfp_t gfp_mask)
1da177e4
LT
216{
217 struct sk_buff *skb;
218 u8 *data;
219
220 /* Get the HEAD */
221 skb = kmem_cache_alloc(skbuff_head_cache,
222 gfp_mask & ~__GFP_DMA);
223 if (!skb)
224 goto out;
225
226 /* Get the DATA. */
227 size = SKB_DATA_ALIGN(size);
228 data = kmem_cache_alloc(cp, gfp_mask);
229 if (!data)
230 goto nodata;
231
232 memset(skb, 0, offsetof(struct sk_buff, truesize));
233 skb->truesize = size + sizeof(struct sk_buff);
234 atomic_set(&skb->users, 1);
235 skb->head = data;
236 skb->data = data;
237 skb->tail = data;
238 skb->end = data + size;
239
240 atomic_set(&(skb_shinfo(skb)->dataref), 1);
241 skb_shinfo(skb)->nr_frags = 0;
7967168c
HX
242 skb_shinfo(skb)->gso_size = 0;
243 skb_shinfo(skb)->gso_segs = 0;
244 skb_shinfo(skb)->gso_type = 0;
1da177e4
LT
245 skb_shinfo(skb)->frag_list = NULL;
246out:
247 return skb;
248nodata:
249 kmem_cache_free(skbuff_head_cache, skb);
250 skb = NULL;
251 goto out;
252}
253
8af27456
CH
254/**
255 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
256 * @dev: network device to receive on
257 * @length: length to allocate
258 * @gfp_mask: get_free_pages mask, passed to alloc_skb
259 *
260 * Allocate a new &sk_buff and assign it a usage count of one. The
261 * buffer has unspecified headroom built in. Users should allocate
262 * the headroom they think they need without accounting for the
263 * built in space. The built in space is used for optimisations.
264 *
265 * %NULL is returned if there is no free memory.
266 */
267struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
268 unsigned int length, gfp_t gfp_mask)
269{
43cb76d9 270 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
8af27456
CH
271 struct sk_buff *skb;
272
4ec93edb 273 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
7b2e497a 274 if (likely(skb)) {
8af27456 275 skb_reserve(skb, NET_SKB_PAD);
7b2e497a
CH
276 skb->dev = dev;
277 }
8af27456
CH
278 return skb;
279}
1da177e4 280
27b437c8 281static void skb_drop_list(struct sk_buff **listp)
1da177e4 282{
27b437c8 283 struct sk_buff *list = *listp;
1da177e4 284
27b437c8 285 *listp = NULL;
1da177e4
LT
286
287 do {
288 struct sk_buff *this = list;
289 list = list->next;
290 kfree_skb(this);
291 } while (list);
292}
293
27b437c8
HX
294static inline void skb_drop_fraglist(struct sk_buff *skb)
295{
296 skb_drop_list(&skb_shinfo(skb)->frag_list);
297}
298
1da177e4
LT
299static void skb_clone_fraglist(struct sk_buff *skb)
300{
301 struct sk_buff *list;
302
303 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
304 skb_get(list);
305}
306
5bba1712 307static void skb_release_data(struct sk_buff *skb)
1da177e4
LT
308{
309 if (!skb->cloned ||
310 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
311 &skb_shinfo(skb)->dataref)) {
312 if (skb_shinfo(skb)->nr_frags) {
313 int i;
314 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
315 put_page(skb_shinfo(skb)->frags[i].page);
316 }
317
318 if (skb_shinfo(skb)->frag_list)
319 skb_drop_fraglist(skb);
320
321 kfree(skb->head);
322 }
323}
324
325/*
326 * Free an skbuff by memory without cleaning the state.
327 */
328void kfree_skbmem(struct sk_buff *skb)
329{
d179cd12
DM
330 struct sk_buff *other;
331 atomic_t *fclone_ref;
332
1da177e4 333 skb_release_data(skb);
d179cd12
DM
334 switch (skb->fclone) {
335 case SKB_FCLONE_UNAVAILABLE:
336 kmem_cache_free(skbuff_head_cache, skb);
337 break;
338
339 case SKB_FCLONE_ORIG:
340 fclone_ref = (atomic_t *) (skb + 2);
341 if (atomic_dec_and_test(fclone_ref))
342 kmem_cache_free(skbuff_fclone_cache, skb);
343 break;
344
345 case SKB_FCLONE_CLONE:
346 fclone_ref = (atomic_t *) (skb + 1);
347 other = skb - 1;
348
349 /* The clone portion is available for
350 * fast-cloning again.
351 */
352 skb->fclone = SKB_FCLONE_UNAVAILABLE;
353
354 if (atomic_dec_and_test(fclone_ref))
355 kmem_cache_free(skbuff_fclone_cache, other);
356 break;
357 };
1da177e4
LT
358}
359
360/**
361 * __kfree_skb - private function
362 * @skb: buffer
363 *
364 * Free an sk_buff. Release anything attached to the buffer.
365 * Clean the state. This is an internal helper function. Users should
366 * always call kfree_skb
367 */
368
369void __kfree_skb(struct sk_buff *skb)
370{
1da177e4
LT
371 dst_release(skb->dst);
372#ifdef CONFIG_XFRM
373 secpath_put(skb->sp);
374#endif
9c2b3328
SH
375 if (skb->destructor) {
376 WARN_ON(in_irq());
1da177e4
LT
377 skb->destructor(skb);
378 }
379#ifdef CONFIG_NETFILTER
380 nf_conntrack_put(skb->nfct);
9fb9cbb1
YK
381#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
382 nf_conntrack_put_reasm(skb->nfct_reasm);
383#endif
1da177e4
LT
384#ifdef CONFIG_BRIDGE_NETFILTER
385 nf_bridge_put(skb->nf_bridge);
386#endif
387#endif
388/* XXX: IS this still necessary? - JHS */
389#ifdef CONFIG_NET_SCHED
390 skb->tc_index = 0;
391#ifdef CONFIG_NET_CLS_ACT
392 skb->tc_verd = 0;
1da177e4
LT
393#endif
394#endif
395
396 kfree_skbmem(skb);
397}
398