热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

深入解析Linux内核网络协议栈:第二部分——sk_buff结构体操作函数详解

本文深入探讨了Linux内核网络协议栈中sk_buff结构体的操作函数,详细解析了其在数据包处理过程中的关键作用与实现机制,为开发者提供了深入了解和优化网络性能的宝贵资源。

本文分析基于Linux Kernel 3.2.1

更多请查看 Linux内核--网络内核实现分析

1、alloc_skb()函数

该函数的作用是在上层协议要发送数据包的时候或网络设备准备接收数据包的时候会调用alloc_skb()函数分配sk_buff结构体,需要释放时调用kfree_skb()函数。

  1. static inline struct sk_buff *alloc_skb(unsigned int size,  
  2.                     gfp_t priority)  
  3. {  
  4.     return __alloc_skb(size, priority, 0, NUMA_NO_NODE);  

这里使用内联函数,非内联函数调用会进堆栈的切换,造成额外的开销,而内联函数可以解决这一点,可以提高执行效率,只是增加了程序的空间开销。

函数调用需要时间和空间开销,调用函数实际上将程序执行流程转移到被调函数中,被调函数的代码执行完后,再返回到调用的地方。这种调用操作要求调用前保护好现场并记忆执行的地址,返回后恢复现场,并按原来保存的地址继续执行。对于较长的函数这种开销可以忽略不计,但对于一些函数体代码很短,又被频繁调用的函数,就不能忽视这种开销。引入内联函数正是为了解决这个问题,提高程序的运行效率。

  1. /*  Allocate a new skbuff. We do this ourselves so we can fill in a few 
  2.  *  'private' fields and also do memory statistics to find all the 
  3.  *  [BEEP] leaks. 
  4.  * 
  5.  */  
  6.   
  7. /** 
  8.  *  __alloc_skb -   allocate a network buffer 
  9.  *  @size: size to allocate 
  10.  *  @gfp_mask: allocation mask 
  11.  *  @fclone: allocate from fclone cache instead of head cache 
  12.  *      and allocate a cloned (child) skb 
  13.  *  @node: numa node to allocate memory on 
  14.  * 
  15.  *  Allocate a new &sk_buff. The returned buffer has no headroom and a 
  16.  *  tail room of size bytes. The object has a reference count of one. 
  17.  *  The return is the buffer. On a failure the return is %NULL. 
  18.  * 
  19.  *  Buffers may only be allocated from interrupts using a @gfp_mask of 
  20.  *  %GFP_ATOMIC. 
  21.  */  
  22. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,  
  23.                 int fclone, int node)  
  24. {  
  25.     struct kmem_cache *cache;  
  26.     struct skb_shared_info *shinfo;  
  27.     struct sk_buff *skb;  
  28.     u8 *data;  
  29.   
  30.     cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;  
  31.   
  32.     /* Get the HEAD */  
  33.     skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);//分配存储空间   
  34.     if (!skb)  
  35.         goto out;//分配失败,返回NULL   
  36.     prefetchw(skb);  
  37.   
  38.     /* We do our best to align skb_shared_info on a separate cache 
  39.      * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 
  40.      * aligned memory blocks, unless SLUB/SLAB debug is enabled. 
  41.      * Both skb->head and skb_shared_info are cache line aligned. 
  42.      */  
  43.     size = SKB_DATA_ALIGN(size);//调整skb大小   
  44.     size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));  
  45.     data = kmalloc_node_track_caller(size, gfp_mask, node);//分配数据区   
  46.     if (!data)  
  47.         goto nodata;  
  48.     /* kmalloc(size) might give us more room than requested. 
  49.      * Put skb_shared_info exactly at the end of allocated zone, 
  50.      * to allow max possible filling before reallocation. 
  51.      */  
  52.     size = SKB_WITH_OVERHEAD(ksize(data));  
  53.     prefetchw(data + size);  
  54.   
  55.     /* 
  56.      * Only clear those fields we need to clear, not those that we will 
  57.      * actually initialise below. Hence, don't put any more fields after 
  58.      * the tail pointer in struct sk_buff! 
  59.      */  
  60.      //sk_buff结构体中最后6个属性不能改变位置,只能在最后   
  61.     memset(skb, 0, offsetof(struct sk_buff, tail));//将sk_buff结构体中tail属性之前的属性清零   
  62.     /* Account for allocated memory : skb + skb->head */  
  63.     skb->truesize = SKB_TRUESIZE(size);//计算缓冲区的尺寸   
  64.     atomic_set(&skb->users, 1);  
  65.     //初始化数据区的指针   
  66.     skb->head = data;  
  67.     skb->data = data;  
  68.     skb_reset_tail_pointer(skb);  
  69.     skb->end = skb->tail + size;  
  70. #ifdef NET_SKBUFF_DATA_USES_OFFSET   
  71.     skb->mac_header = ~0U;  
  72. #endif   
  73.   
  74.     /* make sure we initialize shinfo sequentially */  
  75.     //初始化skb_shared_info   
  76.     shinfo = skb_shinfo(skb);  
  77.     memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));  
  78.     atomic_set(&shinfo->dataref, 1);  
  79.     kmemcheck_annotate_variable(shinfo->destructor_arg);  
  80.   
  81.     if (fclone) {  
  82.         struct sk_buff *child = skb + 1;  
  83.         atomic_t *fclone_ref = (atomic_t *) (child + 1);  
  84.   
  85.         kmemcheck_annotate_bitfield(child, flags1);  
  86.         kmemcheck_annotate_bitfield(child, flags2);  
  87.         skb->fclone = SKB_FCLONE_ORIG;  
  88.         atomic_set(fclone_ref, 1);  
  89.   
  90.         child->fclone = SKB_FCLONE_UNAVAILABLE;  
  91.     }  
  92. out:  
  93.     return skb;  
  94. nodata:  
  95.     kmem_cache_free(cache, skb);  
  96.     skb = NULL;  
  97.     goto out;  
  98. }  

推荐阅读
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 深入理解Tornado模板系统
    本文详细介绍了Tornado框架中模板系统的使用方法。Tornado自带的轻量级、高效且灵活的模板语言位于tornado.template模块,支持嵌入Python代码片段,帮助开发者快速构建动态网页。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
author-avatar
佳蓁政睿9
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有