zoukankan      html  css  js  c++  java
  • dev_alloc_skb(len+16) skb_reserve(skb,2) skb_put(skb,len)

    /**
     *      dev_alloc_skb - allocate an skbuff for receiving
     *      @length: length to allocate
     *
     *      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.
     *
     *      %NULL is returned if there is no free memory. Although this function
     *      allocates memory it can be called from an interrupt.
     */
    static inline struct sk_buff *dev_alloc_skb(unsigned int length)

    {
            return __dev_alloc_skb(length, GFP_ATOMIC);

        首先L2的地址一共是14(6+6+2)个BYTE,而MAC+IP+TCP/UDP结构为:L2+L3(IP addr)+L4。

        按照常理L2地址是在L3后加, 所以在ALLOCSKB的时候要留14个BYTE,为了以后给L2用.但是计算机一般是4字节对齐的, 如果留14个BYTE那么IP只能排在15个byte位子, ip头要经常访问所以这样效率似乎不好.于是在预留2个(14+2 = 16) 正好让IP头4字节对齐. 

    /**
     *      skb_reserve - adjust headroom
     *      @skb: buffer to alter
     *      @len: bytes to move
     *
     *      Increase the headroom of an empty &sk_buff by reducing the tail
     *      room. This is only allowed for an empty buffer.
     */
    static inline void skb_reserve(struct sk_buff *skb, int len)
    {
            skb->data += len;
            skb->tail += len;
    }

     

    skb_reserve(skb,2);

    The networking layer currently aligns IP headers in rx packets. It does this via skb_reserve(,2).skb_put 于 skb_push:

    skb_put() 增长数据区的长度来为memcpy准备空间. 许多的网络操作需要加入一些桢头, 这可以使用skb_push来将数据区向后推, 为头留出空间. 
    ---------------------------------------- 
    | head | data | | 
    ---------------------------------------- 
    skb_put 
    ----------------------------------------- 
    | head | data | put_data | | 
    ----------------------------------------- 
    skb_push
    ------------------------------------------ 
    | head | push_data | data | put_data | | 
    ------------------------------------------ 

    /**
    * skb_put - add data to a buffer 
    * @skb: buffer to use
    * @len: amount of data to add 
    *
    * This function extends the used data area of the buffer. If this would
    * exceed the total buffer size the kernel will panic. A pointer to the
    * first byte of the extra data is returned. 
    */
    static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)

         unsigned char *tmp = skb->tail; 
         SKB_LINEAR_ASSERT(skb); 
         skb->tail += len; 
         skb->len += len; 
         if (unlikely(skb->tail>skb->end)) 
               skb_over_panic(skb, len, current_text_addr()); 
         return tmp; 
    }

  • 相关阅读:
    《编程珠玑》读后感之一
    《梦断代码》读后感之三
    java项目中下载文件文件名乱码
    struts中action与页面之间的传值方式
    使用JSON数据报错和方法
    java中实现将一个数字字符串转换成逗号分隔的数字串, 即从右边开始每三个数字用逗号分隔
    java中判断一个字符在字符串中出现的次数
    使用面向对象(OO)的思想,实现循环输入多个会员的信息,根据会员编号,查找会员积分
    MySQL添加用户、删除用户与授权
    vi编辑器的使用
  • 原文地址:https://www.cnblogs.com/zxc2man/p/6437512.html
Copyright © 2011-2022 走看看