]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/skbuff.c
[CREDITS]: Update Arnaldo entry
[thirdparty/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 "
4305b541 90 "data:%p tail:%#lx end:%#lx dev:%s\n",
27a884dc 91 here, skb->len, sz, skb->head, skb->data,
4305b541 92 (unsigned long)skb->tail, (unsigned long)skb->end,
26095455 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 108 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
4305b541 109 "data:%p tail:%#lx end:%#lx dev:%s\n",
27a884dc 110 here, skb->len, sz, skb->head, skb->data,
4305b541 111 (unsigned long)skb->tail, (unsigned long)skb->end,
26095455 112 skb->dev ? skb->dev->name : "<NULL>");
1da177e4
LT
113 BUG();
114}
115
dc6de336
DM
116void skb_truesize_bug(struct sk_buff *skb)
117{
118 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
119 "len=%u, sizeof(sk_buff)=%Zd\n",
120 skb->truesize, skb->len, sizeof(struct sk_buff));
121}
122EXPORT_SYMBOL(skb_truesize_bug);
123
1da177e4
LT
124/* Allocate a new skbuff. We do this ourselves so we can fill in a few
125 * 'private' fields and also do memory statistics to find all the
126 * [BEEP] leaks.
127 *
128 */
129
130/**
d179cd12 131 * __alloc_skb - allocate a network buffer
1da177e4
LT
132 * @size: size to allocate
133 * @gfp_mask: allocation mask
c83c2486
RD
134 * @fclone: allocate from fclone cache instead of head cache
135 * and allocate a cloned (child) skb
b30973f8 136 * @node: numa node to allocate memory on
1da177e4
LT
137 *
138 * Allocate a new &sk_buff. The returned buffer has no headroom and a
139 * tail room of size bytes. The object has a reference count of one.
140 * The return is the buffer. On a failure the return is %NULL.
141 *
142 * Buffers may only be allocated from interrupts using a @gfp_mask of
143 * %GFP_ATOMIC.
144 */
dd0fc66f 145struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
b30973f8 146 int fclone, int node)
1da177e4 147{
e18b890b 148 struct kmem_cache *cache;
4947d3ef 149 struct skb_shared_info *shinfo;
1da177e4
LT
150 struct sk_buff *skb;
151 u8 *data;
152
8798b3fb
HX
153 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
154
1da177e4 155 /* Get the HEAD */
b30973f8 156 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1da177e4
LT
157 if (!skb)
158 goto out;
159
1da177e4 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
ca0605a7
ACM
166 /*
167 * See comment in sk_buff definition, just before the 'tail' member
168 */
169 memset(skb, 0, offsetof(struct sk_buff, tail));
1da177e4
LT
170 skb->truesize = size + sizeof(struct sk_buff);
171 atomic_set(&skb->users, 1);
172 skb->head = data;
173 skb->data = data;
27a884dc 174 skb_reset_tail_pointer(skb);
4305b541 175 skb->end = skb->tail + size;
4947d3ef
BL
176 /* make sure we initialize shinfo sequentially */
177 shinfo = skb_shinfo(skb);
178 atomic_set(&shinfo->dataref, 1);
179 shinfo->nr_frags = 0;
7967168c
HX
180 shinfo->gso_size = 0;
181 shinfo->gso_segs = 0;
182 shinfo->gso_type = 0;
4947d3ef
BL
183 shinfo->ip6_frag_id = 0;
184 shinfo->frag_list = NULL;
185
d179cd12
DM
186 if (fclone) {
187 struct sk_buff *child = skb + 1;
188 atomic_t *fclone_ref = (atomic_t *) (child + 1);
1da177e4 189
d179cd12
DM
190 skb->fclone = SKB_FCLONE_ORIG;
191 atomic_set(fclone_ref, 1);
192
193 child->fclone = SKB_FCLONE_UNAVAILABLE;
194 }
1da177e4
LT
195out:
196 return skb;
197nodata:
8798b3fb 198 kmem_cache_free(cache, skb);
1da177e4
LT
199 skb = NULL;
200 goto out;
1da177e4
LT
201}
202
8af27456
CH
203/**
204 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
205 * @dev: network device to receive on
206 * @length: length to allocate
207 * @gfp_mask: get_free_pages mask, passed to alloc_skb
208 *
209 * Allocate a new &sk_buff and assign it a usage count of one. The
210 * buffer has unspecified headroom built in. Users should allocate
211 * the headroom they think they need without accounting for the
212 * built in space. The built in space is used for optimisations.
213 *
214 * %NULL is returned if there is no free memory.
215 */
216struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
217 unsigned int length, gfp_t gfp_mask)
218{
43cb76d9 219 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
8af27456
CH
220 struct sk_buff *skb;
221
4ec93edb 222 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
7b2e497a 223 if (likely(skb)) {
8af27456 224 skb_reserve(skb, NET_SKB_PAD);
7b2e497a
CH
225 skb->dev = dev;
226 }
8af27456
CH
227 return skb;
228}
1da177e4 229
27b437c8 230static void skb_drop_list(struct sk_buff **listp)
1da177e4 231{
27b437c8 232 struct sk_buff *list = *listp;
1da177e4 233
27b437c8 234 *listp = NULL;
1da177e4
LT
235
236 do {
237 struct sk_buff *this = list;
238 list = list->next;
239 kfree_skb(this);
240 } while (list);
241}
242
27b437c8
HX
243static inline void skb_drop_fraglist(struct sk_buff *skb)
244{
245 skb_drop_list(&skb_shinfo(skb)->frag_list);
246}
247
1da177e4
LT
248static void skb_clone_fraglist(struct sk_buff *skb)
249{
250 struct sk_buff *list;
251
252 for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
253 skb_get(list);
254}
255
5bba1712 256static void skb_release_data(struct sk_buff *skb)
1da177e4
LT
257{
258 if (!skb->cloned ||
259 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
260 &skb_shinfo(skb)->dataref)) {
261 if (skb_shinfo(skb)->nr_frags) {
262 int i;
263 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
264 put_page(skb_shinfo(skb)->frags[i].page);
265 }
266
267 if (skb_shinfo(skb)->frag_list)
268 skb_drop_fraglist(skb);
269
270 kfree(skb->head);
271 }
272}
273
274/*
275 * Free an skbuff by memory without cleaning the state.
276 */
277void kfree_skbmem(struct sk_buff *skb)
278{
d179cd12
DM
279 struct sk_buff *other;
280 atomic_t *fclone_ref;
281
1da177e4 282 skb_release_data(skb);
d179cd12
DM
283 switch (skb->fclone) {
284 case SKB_FCLONE_UNAVAILABLE:
285 kmem_cache_free(skbuff_head_cache, skb);
286 break;
287
288 case SKB_FCLONE_ORIG:
289 fclone_ref = (atomic_t *) (skb + 2);
290 if (atomic_dec_and_test(fclone_ref))
291 kmem_cache_free(skbuff_fclone_cache, skb);
292 break;
293
294 case SKB_FCLONE_CLONE:
295 fclone_ref = (atomic_t *) (skb + 1);
296 other = skb - 1;
297
298 /* The clone portion is available for
299 * fast-cloning again.
300 */
301 skb->fclone = SKB_FCLONE_UNAVAILABLE;
302
303 if (atomic_dec_and_test(fclone_ref))
304 kmem_cache_free(skbuff_fclone_cache, other);
305 break;
306 };
1da177e4
LT
307}
308
309/**
310 * __kfree_skb - private function
311 * @skb: buffer
312 *
313 * Free an sk_buff. Release anything attached to the buffer.
314 * Clean the state. This is an internal helper function. Users should
315 * always call kfree_skb
316 */
317
318void __kfree_skb(struct sk_buff *skb)
319{
1da177e4
LT
320 dst_release(skb->dst);
321#ifdef CONFIG_XFRM
322 secpath_put(skb->sp);
323#endif
9c2b3328
SH
324 if (skb->destructor) {
325 WARN_ON(in_irq());
1da177e4
LT
326 skb->destructor(skb);
327 }
9fb9cbb1 328#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
5f79e0f9 329 nf_conntrack_put(skb->nfct);
9fb9cbb1
YK
330 nf_conntrack_put_reasm(skb->nfct_reasm);
331#endif
1da177e4
LT
332#ifdef CONFIG_BRIDGE_NETFILTER
333 nf_bridge_put(skb->nf_bridge);
334#endif
1da177e4
LT
335/* XXX: IS this still necessary? - JHS */
336#ifdef CONFIG_NET_SCHED
337 skb->tc_index = 0;
338#ifdef CONFIG_NET_CLS_ACT
339 skb->tc_verd = 0;
1da177e4
LT
340#endif
341#endif
342
343 kfree_skbmem(skb);
344}
345