[NET]: New sysctls should use __read_mostly tags
[safe/jmp/linux-2.6] / net / core / skbuff.c
index a1c9ecf..336958f 100644 (file)
  *     The functions in this file will not compile correctly with gcc 2.4.x
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
-#include <linux/sched.h>
 #include <linux/mm.h>
 #include <linux/interrupt.h>
 #include <linux/in.h>
@@ -57,7 +55,6 @@
 #include <linux/cache.h>
 #include <linux/rtnetlink.h>
 #include <linux/init.h>
-#include <linux/highmem.h>
 
 #include <net/protocol.h>
 #include <net/dst.h>
 #include <asm/uaccess.h>
 #include <asm/system.h>
 
-static kmem_cache_t *skbuff_head_cache __read_mostly;
-static kmem_cache_t *skbuff_fclone_cache __read_mostly;
+#include "kmap_skb.h"
+
+static struct kmem_cache *skbuff_head_cache __read_mostly;
+static struct kmem_cache *skbuff_fclone_cache __read_mostly;
 
 /*
  *     Keep out-of-line to prevent kernel bloat.
@@ -88,7 +87,7 @@ static kmem_cache_t *skbuff_fclone_cache __read_mostly;
 void skb_over_panic(struct sk_buff *skb, int sz, void *here)
 {
        printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
-                         "data:%p tail:%p end:%p dev:%s\n",
+                         "data:%p tail:%p end:%p dev:%s\n",
               here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
               skb->dev ? skb->dev->name : "<NULL>");
        BUG();
@@ -106,7 +105,7 @@ void skb_over_panic(struct sk_buff *skb, int sz, void *here)
 void skb_under_panic(struct sk_buff *skb, int sz, void *here)
 {
        printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
-                         "data:%p tail:%p end:%p dev:%s\n",
+                         "data:%p tail:%p end:%p dev:%s\n",
               here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
               skb->dev ? skb->dev->name : "<NULL>");
        BUG();
@@ -132,6 +131,7 @@ EXPORT_SYMBOL(skb_truesize_bug);
  *     @gfp_mask: allocation mask
  *     @fclone: allocate from fclone cache instead of head cache
  *             and allocate a cloned (child) skb
+ *     @node: numa node to allocate memory on
  *
  *     Allocate a new &sk_buff. The returned buffer has no headroom and a
  *     tail room of size bytes. The object has a reference count of one.
@@ -141,9 +141,9 @@ EXPORT_SYMBOL(skb_truesize_bug);
  *     %GFP_ATOMIC.
  */
 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
-                           int fclone)
+                           int fclone, int node)
 {
-       kmem_cache_t *cache;
+       struct kmem_cache *cache;
        struct skb_shared_info *shinfo;
        struct sk_buff *skb;
        u8 *data;
@@ -151,13 +151,14 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
        cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
 
        /* Get the HEAD */
-       skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA);
+       skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
        if (!skb)
                goto out;
 
        /* Get the DATA. Size must match skb_add_mtu(). */
        size = SKB_DATA_ALIGN(size);
-       data = ____kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
+       data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
+                       gfp_mask, node);
        if (!data)
                goto nodata;
 
@@ -196,66 +197,37 @@ nodata:
 }
 
 /**
- *     alloc_skb_from_cache    -       allocate a network buffer
- *     @cp: kmem_cache from which to allocate the data area
- *           (object size must be big enough for @size bytes + skb overheads)
- *     @size: size to allocate
- *     @gfp_mask: allocation mask
+ *     __netdev_alloc_skb - allocate an skbuff for rx on a specific device
+ *     @dev: network device to receive on
+ *     @length: length to allocate
+ *     @gfp_mask: get_free_pages mask, passed to alloc_skb
  *
- *     Allocate a new &sk_buff. The returned buffer has no headroom and
- *     tail room of size bytes. The object has a reference count of one.
- *     The return is the buffer. On a failure the return is %NULL.
+ *     Allocate a new &sk_buff and assign it a usage count of one. The
+ *     buffer has unspecified headroom built in. Users should allocate
+ *     the headroom they think they need without accounting for the
+ *     built in space. The built in space is used for optimisations.
  *
- *     Buffers may only be allocated from interrupts using a @gfp_mask of
- *     %GFP_ATOMIC.
+ *     %NULL is returned if there is no free memory.
  */
-struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
-                                    unsigned int size,
-                                    gfp_t gfp_mask)
+struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
+               unsigned int length, gfp_t gfp_mask)
 {
+       int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
        struct sk_buff *skb;
-       u8 *data;
-
-       /* Get the HEAD */
-       skb = kmem_cache_alloc(skbuff_head_cache,
-                              gfp_mask & ~__GFP_DMA);
-       if (!skb)
-               goto out;
-
-       /* Get the DATA. */
-       size = SKB_DATA_ALIGN(size);
-       data = kmem_cache_alloc(cp, gfp_mask);
-       if (!data)
-               goto nodata;
-
-       memset(skb, 0, offsetof(struct sk_buff, truesize));
-       skb->truesize = size + sizeof(struct sk_buff);
-       atomic_set(&skb->users, 1);
-       skb->head = data;
-       skb->data = data;
-       skb->tail = data;
-       skb->end  = data + size;
 
-       atomic_set(&(skb_shinfo(skb)->dataref), 1);
-       skb_shinfo(skb)->nr_frags  = 0;
-       skb_shinfo(skb)->gso_size = 0;
-       skb_shinfo(skb)->gso_segs = 0;
-       skb_shinfo(skb)->gso_type = 0;
-       skb_shinfo(skb)->frag_list = NULL;
-out:
+       skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
+       if (likely(skb)) {
+               skb_reserve(skb, NET_SKB_PAD);
+               skb->dev = dev;
+       }
        return skb;
-nodata:
-       kmem_cache_free(skbuff_head_cache, skb);
-       skb = NULL;
-       goto out;
 }
 
-
-static void skb_drop_fraglist(struct sk_buff *skb)
+static void skb_drop_list(struct sk_buff **listp)
 {
-       struct sk_buff *list = skb_shinfo(skb)->frag_list;
+       struct sk_buff *list = *listp;
 
-       skb_shinfo(skb)->frag_list = NULL;
+       *listp = NULL;
 
        do {
                struct sk_buff *this = list;
@@ -264,6 +236,11 @@ static void skb_drop_fraglist(struct sk_buff *skb)
        } while (list);
 }
 
+static inline void skb_drop_fraglist(struct sk_buff *skb)
+{
+       skb_drop_list(&skb_shinfo(skb)->frag_list);
+}
+
 static void skb_clone_fraglist(struct sk_buff *skb)
 {
        struct sk_buff *list;
@@ -431,6 +408,7 @@ struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
        memcpy(n->cb, skb->cb, sizeof(skb->cb));
        C(len);
        C(data_len);
+       C(mac_len);
        C(csum);
        C(local_df);
        n->cloned = 1;
@@ -443,8 +421,8 @@ struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
 #endif
        C(protocol);
        n->destructor = NULL;
+       C(mark);
 #ifdef CONFIG_NETFILTER
-       C(nfmark);
        C(nfct);
        nf_conntrack_get(skb->nfct);
        C(nfctinfo);
@@ -463,7 +441,7 @@ struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
        n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
        n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
        n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
-       C(input_dev);
+       C(iif);
 #endif
        skb_copy_secmark(n, skb);
 #endif
@@ -504,8 +482,8 @@ static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
        new->pkt_type   = old->pkt_type;
        new->tstamp     = old->tstamp;
        new->destructor = NULL;
+       new->mark       = old->mark;
 #ifdef CONFIG_NETFILTER
-       new->nfmark     = old->nfmark;
        new->nfct       = old->nfct;
        nf_conntrack_get(old->nfct);
        new->nfctinfo   = old->nfctinfo;
@@ -609,6 +587,7 @@ struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
        n->csum      = skb->csum;
        n->ip_summed = skb->ip_summed;
 
+       n->truesize += skb->data_len;
        n->data_len  = skb->data_len;
        n->len       = skb->len;
 
@@ -785,12 +764,12 @@ struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  *
  *     May return error in out of memory cases. The skb is freed on error.
  */
+
 int skb_pad(struct sk_buff *skb, int pad)
 {
        int err;
        int ntail;
-       
+
        /* If the skbuff is non linear tailroom is always zero.. */
        if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
                memset(skb->data+skb->len, 0, pad);
@@ -817,48 +796,88 @@ int skb_pad(struct sk_buff *skb, int pad)
 free_skb:
        kfree_skb(skb);
        return err;
-}      
+}
+
 /* Trims skb to length len. It can change skb pointers.
  */
 
 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
 {
+       struct sk_buff **fragp;
+       struct sk_buff *frag;
        int offset = skb_headlen(skb);
        int nfrags = skb_shinfo(skb)->nr_frags;
        int i;
+       int err;
 
-       for (i = 0; i < nfrags; i++) {
+       if (skb_cloned(skb) &&
+           unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
+               return err;
+
+       i = 0;
+       if (offset >= len)
+               goto drop_pages;
+
+       for (; i < nfrags; i++) {
                int end = offset + skb_shinfo(skb)->frags[i].size;
-               if (end > len) {
-                       if (skb_cloned(skb)) {
-                               if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
-                                       return -ENOMEM;
-                       }
-                       if (len <= offset) {
-                               put_page(skb_shinfo(skb)->frags[i].page);
-                               skb_shinfo(skb)->nr_frags--;
-                       } else {
-                               skb_shinfo(skb)->frags[i].size = len - offset;
-                       }
+
+               if (end < len) {
+                       offset = end;
+                       continue;
+               }
+
+               skb_shinfo(skb)->frags[i++].size = len - offset;
+
+drop_pages:
+               skb_shinfo(skb)->nr_frags = i;
+
+               for (; i < nfrags; i++)
+                       put_page(skb_shinfo(skb)->frags[i].page);
+
+               if (skb_shinfo(skb)->frag_list)
+                       skb_drop_fraglist(skb);
+               goto done;
+       }
+
+       for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
+            fragp = &frag->next) {
+               int end = offset + frag->len;
+
+               if (skb_shared(frag)) {
+                       struct sk_buff *nfrag;
+
+                       nfrag = skb_clone(frag, GFP_ATOMIC);
+                       if (unlikely(!nfrag))
+                               return -ENOMEM;
+
+                       nfrag->next = frag->next;
+                       kfree_skb(frag);
+                       frag = nfrag;
+                       *fragp = frag;
+               }
+
+               if (end < len) {
+                       offset = end;
+                       continue;
                }
-               offset = end;
+
+               if (end > len &&
+                   unlikely((err = pskb_trim(frag, len - offset))))
+                       return err;
+
+               if (frag->next)
+                       skb_drop_list(&frag->next);
+               break;
        }
 
-       if (offset < len) {
+done:
+       if (len > skb_headlen(skb)) {
                skb->data_len -= skb->len - len;
                skb->len       = len;
        } else {
-               if (len <= skb_headlen(skb)) {
-                       skb->len      = len;
-                       skb->data_len = 0;
-                       skb->tail     = skb->data + len;
-                       if (skb_shinfo(skb)->frag_list && !skb_cloned(skb))
-                               skb_drop_fraglist(skb);
-               } else {
-                       skb->data_len -= skb->len - len;
-                       skb->len       = len;
-               }
+               skb->len       = len;
+               skb->data_len  = 0;
+               skb->tail      = skb->data + len;
        }
 
        return 0;
@@ -1169,8 +1188,8 @@ EXPORT_SYMBOL(skb_store_bits);
 
 /* Checksum skb data. */
 
-unsigned int skb_checksum(const struct sk_buff *skb, int offset,
-                         int len, unsigned int csum)
+__wsum skb_checksum(const struct sk_buff *skb, int offset,
+                         int len, __wsum csum)
 {
        int start = skb_headlen(skb);
        int i, copy = start - offset;
@@ -1194,7 +1213,7 @@ unsigned int skb_checksum(const struct sk_buff *skb, int offset,
 
                end = start + skb_shinfo(skb)->frags[i].size;
                if ((copy = end - offset) > 0) {
-                       unsigned int csum2;
+                       __wsum csum2;
                        u8 *vaddr;
                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 
@@ -1223,7 +1242,7 @@ unsigned int skb_checksum(const struct sk_buff *skb, int offset,
 
                        end = start + list->len;
                        if ((copy = end - offset) > 0) {
-                               unsigned int csum2;
+                               __wsum csum2;
                                if (copy > len)
                                        copy = len;
                                csum2 = skb_checksum(list, offset - start,
@@ -1244,8 +1263,8 @@ unsigned int skb_checksum(const struct sk_buff *skb, int offset,
 
 /* Both of above in one bottle. */
 
-unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
-                                   u8 *to, int len, unsigned int csum)
+__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
+                                   u8 *to, int len, __wsum csum)
 {
        int start = skb_headlen(skb);
        int i, copy = start - offset;
@@ -1271,7 +1290,7 @@ unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
 
                end = start + skb_shinfo(skb)->frags[i].size;
                if ((copy = end - offset) > 0) {
-                       unsigned int csum2;
+                       __wsum csum2;
                        u8 *vaddr;
                        skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 
@@ -1297,7 +1316,7 @@ unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
                struct sk_buff *list = skb_shinfo(skb)->frag_list;
 
                for (; list; list = list->next) {
-                       unsigned int csum2;
+                       __wsum csum2;
                        int end;
 
                        BUG_TRAP(start <= offset + len);
@@ -1325,10 +1344,10 @@ unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
 
 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
 {
-       unsigned int csum;
+       __wsum csum;
        long csstart;
 
-       if (skb->ip_summed == CHECKSUM_HW)
+       if (skb->ip_summed == CHECKSUM_PARTIAL)
                csstart = skb->h.raw - skb->data;
        else
                csstart = skb_headlen(skb);
@@ -1342,10 +1361,10 @@ void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
                csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
                                              skb->len - csstart, 0);
 
-       if (skb->ip_summed == CHECKSUM_HW) {
-               long csstuff = csstart + skb->csum;
+       if (skb->ip_summed == CHECKSUM_PARTIAL) {
+               long csstuff = csstart + skb->csum_offset;
 
-               *((unsigned short *)(to + csstuff)) = csum_fold(csum);
+               *((__sum16 *)(to + csstuff)) = csum_fold(csum);
        }
 }
 
@@ -1829,10 +1848,10 @@ int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  *     @len: length of data pulled
  *
  *     This function performs an skb_pull on the packet and updates
- *     update the CHECKSUM_HW checksum.  It should be used on receive
- *     path processing instead of skb_pull unless you know that the
- *     checksum difference is zero (e.g., a valid IP header) or you
- *     are setting ip_summed to CHECKSUM_NONE.
+ *     update the CHECKSUM_COMPLETE checksum.  It should be used on
+ *     receive path processing instead of skb_pull unless you know
+ *     that the checksum difference is zero (e.g., a valid IP header)
+ *     or you are setting ip_summed to CHECKSUM_NONE.
  */
 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
 {
@@ -1876,7 +1895,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, int features)
        do {
                struct sk_buff *nskb;
                skb_frag_t *frag;
-               int hsize, nsize;
+               int hsize;
                int k;
                int size;
 
@@ -1887,11 +1906,10 @@ struct sk_buff *skb_segment(struct sk_buff *skb, int features)
                hsize = skb_headlen(skb) - offset;
                if (hsize < 0)
                        hsize = 0;
-               nsize = hsize + doffset;
-               if (nsize > len + doffset || !sg)
-                       nsize = len + doffset;
+               if (hsize > len || !sg)
+                       hsize = len;
 
-               nskb = alloc_skb(nsize + headroom, GFP_ATOMIC);
+               nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
                if (unlikely(!nskb))
                        goto err;
 
@@ -1925,7 +1943,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, int features)
                frag = skb_shinfo(nskb)->frags;
                k = 0;
 
-               nskb->ip_summed = CHECKSUM_HW;
+               nskb->ip_summed = CHECKSUM_PARTIAL;
                nskb->csum = skb->csum;
                memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
 
@@ -1965,7 +1983,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, int features)
 err:
        while ((skb = segs)) {
                segs = skb->next;
-               kfree(skb);
+               kfree_skb(skb);
        }
        return ERR_PTR(err);
 }
@@ -1977,19 +1995,14 @@ void __init skb_init(void)
        skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
                                              sizeof(struct sk_buff),
                                              0,
-                                             SLAB_HWCACHE_ALIGN,
+                                             SLAB_HWCACHE_ALIGN|SLAB_PANIC,
                                              NULL, NULL);
-       if (!skbuff_head_cache)
-               panic("cannot create skbuff cache");
-
        skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
                                                (2*sizeof(struct sk_buff)) +
                                                sizeof(atomic_t),
                                                0,
-                                               SLAB_HWCACHE_ALIGN,
+                                               SLAB_HWCACHE_ALIGN|SLAB_PANIC,
                                                NULL, NULL);
-       if (!skbuff_fclone_cache)
-               panic("cannot create skbuff cache");
 }
 
 EXPORT_SYMBOL(___pskb_trim);
@@ -1997,6 +2010,7 @@ EXPORT_SYMBOL(__kfree_skb);
 EXPORT_SYMBOL(kfree_skb);
 EXPORT_SYMBOL(__pskb_pull_tail);
 EXPORT_SYMBOL(__alloc_skb);
+EXPORT_SYMBOL(__netdev_alloc_skb);
 EXPORT_SYMBOL(pskb_copy);
 EXPORT_SYMBOL(pskb_expand_head);
 EXPORT_SYMBOL(skb_checksum);