/**
 *      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; 
}

dev_alloc_skb(len+16) skb_reserve(skb,2) skb_put(skb,len)的更多相关文章

  1. linux内核数据包转发流程(三)网卡帧接收分析

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 每一个cpu都有队列来处理接收到的帧,都有其数据结构来处理入口和出口流量,因此,不 ...

  2. linux内核包转发过程(三)NIC帧接收分析

    [版权声明:转载请保留源:blog.csdn.net/gentleliu.邮箱:shallnew*163.com] 每一个cpu都有队列来处理接收到的帧.都有其数据结构来处理入口和出口流量,因此.不同 ...

  3. Linux网络设备驱动架构

    Linux网络设备驱动程序体系结构分为四层:网络协议接口层.网络设备接口层.提供实际功能的设备驱动层以及网络设备与媒介层. (1)网络协议接口层向网络层协议提供统一的数据包收发接口,不论上层协议是AR ...

  4. IP输出 之 分片ip_fragment、ip_do_fragment

    概述 ip_fragment函数用于判断是否进行分片,在没有设置DF标记的情况下进入分片,如果设置了DF标记,则继续判断,如果不允许DF分片或者收到的最大分片大于MTU大小,则回复ICMP,释放skb ...

  5. android & Linux uevent机制

    Linux uevent机制 Uevent是内核通知android有状态变化的一种方法,比如USB线插入.拔出,电池电量变化等等.其本质是内核发送(可以通过socket)一个字符串,应用层(andro ...

  6. TCP的发送系列 — tcp_sendmsg()的实现(二)

    主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上篇blog中分析了tcp_sendmsg()这个主要函 ...

  7. 设备模型的uevent机制

    内核模块的热插拔事件的通知基于uevent机制. 当kobject的状态发生改变(如,add, remove等)时,会通知用户空间,用户空间接收到事件通知后可以做相应的处理.uevent把事件上报给用 ...

  8. Linux uevent分析、用户接收uevent以及mdev分析

    关键词:uevent.netlink.ADD/REMOVE/CHANGE.uevent_helper.hotplug.usermode helper.mdev.mdev.conf等等. 本文从三方面了 ...

  9. 菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)

    俗话说的好,牵牛要牵牛鼻子 驾车顶牛,处理复杂的东西,只要抓住重点,才能理清脉络,不至于深陷其中,不能自拔.对复杂的nginx而言,main函数就是“牛之鼻”,只要能理清main函数,就一定能理解其中 ...

随机推荐

  1. wpf下使用NotifyIcon

    以前在winForm下使用过NotifyIcon,到wpf找不到了,在wpf下还是直接用WinForm里的那个NotifyIcon实现最小到系统托盘 定义一个NotifyIcon成员 : Notify ...

  2. bootstrap-datapicker 时间约束

    <div class="input-group date date-picker" id="StartTime"> <input type=& ...

  3. svn和git的区别及适用场景

    svn和git的区别及适用场景 来源 https://blog.csdn.net/wz947324/article/details/80104621 svn的优势: 优异的跨平台支持,对windows ...

  4. 【刷题】清橙 A1295 necklace

    试题来源 清华大学2011年百名信息学优秀高中学子夏令营 问题描述 有人打算送给你一条宝石项链,包含了N颗五颜六色(一共有M种颜色)的宝石.因为本问题中你只关心每个宝石的颜色,而且项链现在两头还没有接 ...

  5. IOS8模糊毛玻璃的效果UIVisualEffectView

    UIVisualEffectView实现两种模糊效果:UIBlurEffect 和 UIVibrancyEffect 两者都是继承自UIView,前者放在任意的View里边都能对下册的视图渲染出模糊效 ...

  6. Redis存储系统

    一.QuickStart 1.Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string ...

  7. 解题:CTSC 2017 吉夫特

    题面 首先有个结论:$C_n^m$为奇数当且仅当$m$是$n$的一个子集 于是从后往前推,记录每个数出现的位置,然后对每个位置枚举子集统计在它后面的贡献即可 #include<cstdio> ...

  8. ContestHunter#24-C 逃不掉的路

    Description: 求无向图的必经边 思路:一眼题 将无向图缩成树,然后求两点树上距离 #include<iostream> #include<vector> #incl ...

  9. Qt ------ 获取 wifi 信息

    QProcess:可以调用外部进程 netsh wlan show interfaces:可以查看连接哪个wifi netsh wlan show networks:显示所有可用的wifi netsh ...

  10. 进程间共享数据Manager

    一.前言 进程间的通信Queue()和Pipe(),可以实现进程间的数据传递.但是要使python进程间共享数据,我们就要使用multiprocessing.Manager. Manager()返回的 ...