skb管理函数之skb_clone、pskb_copy、skb_copy
skb_clone–只复制skb描述符本身,如果只修改skb描述符则使用该函数克隆;
pskb_copy–复制skb描述符+线性数据区域(包括skb_shared_info),如果需要修改描述符以及数据则使用该函数复制;
skb_copy–复制所有数据,包括skb描述符+线性数据区域+非线性数据区,如果需要修改描述符和全部数据则使用该函数复制;
以下为三个函数的分析;
/**
* skb_clone - duplicate an sk_buff
* @skb: buffer to clone
* @gfp_mask: allocation priority
*
* Duplicate an &sk_buff. The new one is not owned by a socket. Both
* copies share the same packet data but not structure. The new
* buffer has a reference count of 1. If the allocation fails the
* function returns %NULL otherwise the new buffer is returned.
*
* If this function is called from an interrupt gfp_mask() must be
* %GFP_ATOMIC.
*/
/*
用于修改skb描述符中的某些字段
克隆skb,该函数只克隆sk_buff部分
数据区域公用(需要递增引用计数)
*/
struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
{
/* 获取到支持克隆的skb */
struct sk_buff_fclones *fclones = container_of(skb,
struct sk_buff_fclones,
skb1);
struct sk_buff *n; /*
若发送标记有零拷贝,则拷贝用户空间的
片段缓存到内核空间
*/
if (skb_orphan_frags(skb, gfp_mask))
return NULL; /* 如果skb可以被克隆,并且克隆引用为1 */
if (skb->fclone == SKB_FCLONE_ORIG &&
atomic_read(&fclones->fclone_ref) == ) {
/* 待克隆n指向skb2 */
n = &fclones->skb2;
/* 增加引用计数 */
atomic_set(&fclones->fclone_ref, );
}
/* 否则,为新克隆的skb分配内存 */
else {
if (skb_pfmemalloc(skb))
gfp_mask |= __GFP_MEMALLOC; n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
if (!n)
return NULL; kmemcheck_annotate_bitfield(n, flags1);
/* 初始化克隆状态 */
n->fclone = SKB_FCLONE_UNAVAILABLE;
} /* 调用克隆 */
return __skb_clone(n, skb);
}
/*
用于修改skb描述符和数据缓冲区内容时拷贝
skb拷贝,拷贝skb描述符+ 线性数据缓冲区,
线性缓冲区以外数据共享
*/
static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
gfp_t gfp_mask)
{
return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
}
/**
* __pskb_copy_fclone - create copy of an sk_buff with private head.
* @skb: buffer to copy
* @headroom: headroom of new skb
* @gfp_mask: allocation priority
* @fclone: if true allocate the copy of the skb from the fclone
* cache instead of the head cache; it is recommended to set this
* to true for the cases where the copy will likely be cloned
*
* Make a copy of both an &sk_buff and part of its data, located
* in header. Fragmented data remain shared. This is used when
* the caller wishes to modify only header of &sk_buff and needs
* private copy of the header to alter. Returns %NULL on failure
* or the pointer to the buffer on success.
* The returned buffer has a reference count of 1.
*/
/*
拷贝skb描述符+ 线性数据缓冲区,
线性缓冲区外部数据共享
*/
struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
gfp_t gfp_mask, bool fclone)
{
unsigned int size = skb_headlen(skb) + headroom;
int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : );
struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); if (!n)
goto out; /* Set the data pointer */
/* 保留头部空间 */
skb_reserve(n, headroom);
/* Set the tail pointer and length */
/* 增加尾指针和数据长度 */
skb_put(n, skb_headlen(skb));
/* Copy the bytes */
/* 拷贝线性缓冲区 */
skb_copy_from_linear_data(skb, n->data, n->len); /* 设置长度值 */
n->truesize += skb->data_len;
n->data_len = skb->data_len;
n->len = skb->len; /* 若有片段 */
if (skb_shinfo(skb)->nr_frags) {
int i; /* 片段数据拷贝到内核 */
if (skb_orphan_frags(skb, gfp_mask)) {
kfree_skb(n);
n = NULL;
goto out;
} /* 复制判断内容 */
for (i = ; i < skb_shinfo(skb)->nr_frags; i++) {
skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
skb_frag_ref(skb, i);
} /* 片段数 */
skb_shinfo(n)->nr_frags = i;
} /* 如果有片段链表 */
if (skb_has_frag_list(skb)) {
/* 引用片段链表 */
skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
skb_clone_fraglist(n);
} /* 拷贝头部信息 */
copy_skb_header(n, skb);
out:
return n;
}
/**
* skb_copy - create private copy of an sk_buff
* @skb: buffer to copy
* @gfp_mask: allocation priority
*
* Make a copy of both an &sk_buff and its data. This is used when the
* caller wishes to modify the data and needs a private copy of the
* data to alter. Returns %NULL on failure or the pointer to the buffer
* on success. The returned buffer has a reference count of 1.
*
* As by-product this function converts non-linear &sk_buff to linear
* one, so that &sk_buff becomes completely private and caller is allowed
* to modify all the data of returned buffer. This means that this
* function is not recommended for use in circumstances when only
* header is going to be modified. Use pskb_copy() instead.
*/
/* 拷贝skb描述符+线性缓冲区+非线性缓冲区 */
struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
{
/* 头部空间长度 */
int headerlen = skb_headroom(skb); /* 分配空间= skb空间+ skb以外的数据空间 */
unsigned int size = skb_end_offset(skb) + skb->data_len;
struct sk_buff *n = __alloc_skb(size, gfp_mask,
skb_alloc_rx_flag(skb), NUMA_NO_NODE); if (!n)
return NULL; /* Set the data pointer */
//保留头部空间
skb_reserve(n, headerlen);
/* Set the tail pointer and length */
/* 偏移尾部指针修改总长度 */
skb_put(n, skb->len); /* 拷贝数据 */
if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
BUG(); /* 拷贝skb头 */
copy_skb_header(n, skb);
return n;
}
skb管理函数之skb_clone、pskb_copy、skb_copy的更多相关文章
- skb管理函数之alloc_skb、dev_alloc_skb、kfree_skb、dev_kfree_skb、consume_skb
alloc_skb--分配skb dev_alloc_skb--分配skb,通常被设备驱动用在中断上下文中,它是alloc_skb的封装函数,因为在中断处理函数中被调用,因此要求原子操作(GFP_AT ...
- skb管理函数之skb_put、skb_push、skb_pull、skb_reserve
四个操作函数直接的区别,如下图: /** * skb_put - add data to a buffer * @skb: buffer to use * @len: amount of data t ...
- μC/OS-Ⅲ系统的时间管理函数和定时器
一.时间管理函数 μC/OS-Ⅲ系统提供一些列时间管理服务函数: 1.OSTimeDly():任务延时n个时钟节拍. 2.OSTimeDlyHMSM():任务延时指定的时间,采用“时:分:秒:毫秒”方 ...
- 内存管理运算符new delete与内存管理函数malloc free的区别——已经他们对对象创建的过程。
(1)内存管理函数与内存管理运算符的区别 内存管理函数有内存分配函数,malloc calloc realloc 以及内存释放函数free. 内存管理运算符有new 和delete. 两种内存管理方式 ...
- functools:管理函数工具(部分)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #functools:管理函数工具 #作用:处理其他函数的函数 #版 ...
- POSIX多线程——基本线程管理函数介绍
POSIX基本的几个线程管理函数见下表: ------------------------------------------------------------------------------- ...
- [图形学] Chp9 三维几何变换--栈处理函数与矩阵管理函数的区别
矩阵管理函数:glLoadIdentity()是把当前活动矩阵设置为单位矩阵. 栈处理函数:glPushMatrix()是将当前活动的变换矩阵复制一份,压入栈顶:glPopMatrix()是破坏当前活 ...
- C++内存管理-重载内存管理函数
记录学习的点点滴滴,参考侯捷<<C++内存管理>> 我们先重载一下C++的几个内存管理函数 operator new, operator new[], operator del ...
- 【教训】null == '',改造ThinkSNS 系统里面的一个缓存管理函数S()后,留下一个大bug
本来想简化 ThinkSNS 系统里面的一个缓存管理函数: <?php /** * 用来对应用缓存信息的读.写.删除 * $expire = null/0 表示永久缓存,否则为缓存有效期 */ ...
随机推荐
- C语言中printf直接打出2进制数是%什么?16进制是什么?
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...
- hash 默认使用equal进行元素比较 防止元素重复
hash 默认使用equal进行元素比较 防止元素重复
- 【bzoj1096】[ZJOI2007]仓库建设 斜率优化dp
题目描述 L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天,L公司的总裁L ...
- NOIP2016Day1T2天天爱跑步(LCA+桶)
据说是今年NOIP最难一题了...我还记得当时满怀期待心情点开Day1的题发现T2就不会了于是怀疑人生良久... 啊好像很多大爷都是用线段树合并写的,我怎么什么数据结构都不会啊呜呜呜... 题目大意就 ...
- 微信小程序 事件绑定 bind和catch 区别
转自:https://blog.csdn.net/xiaoqiang_0719/article/details/79729592 本文以冒泡事件tap(手指触摸后马上离开,也就是点击事件)为例子来区别 ...
- UVA 10201 DP
Adventures in Moving - Part IV 题意: 汽车邮箱容量200升,最初有100升油,要求到达终点油箱中的油不少于100升的最小花费,不能到达终点输出Impossible. 汽 ...
- CMDB服务器管理系统【s5day88】:兼容的实现
比较麻烦的实现方式 类的继承方式 目录结构如下: auto_client\bin\run.py import sys import os import importlib import request ...
- Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview
在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待.本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果. 其效果图: 一.ListVie ...
- Java设计模式の工厂模式
-------------------------------------------------------- 目录: 一.序言 二.简单工厂模式 三.工厂方法模式 四.简单工厂和工厂方法模式的比 ...
- NOI2001 方程的解数
1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛 时间限制: 5 s 空间限制: 64000 KB 题目描述 Descripti ...