文章出自:http://www.cnblogs.com/iceocean/articles/1594160.html
前言:
以下是根據《深入理解Linux網絡技術內幕》對sk_buff的相關總結,由於是剛剛看這本書(太厚了),不免在前期出現錯誤,隨着對此書的深入我會在修改前面的錯誤,也希望各位牛人給予指點。幫助我成長。
sk_buff分析:
sk_buff是Linux網絡代碼中最重要的結構體之一。它是Linux在其協議棧裏傳送的結構體,也就是所謂的“包”,在他裏面包含了各層協議的頭部,比如ethernet, ip ,tcp ,udp等等。也有相關的操作等。熟悉他是進一步瞭解Linux網絡協議棧的基礎。
此結構定義在<include/linux/skbuff.h>頭文件中,結構體佈局大致可分爲以下四部分:
l 佈局(layout)
l 通用(general)
l 功能專用(feature-specific)
l 管理函數(management functions)
網絡選項以及內核結構
我們可以看到在此結構體裏有很多預處理,他是在需要指定相應功能時才起作用,我們在這裏先對通用的作出分析。
佈局字段:
sk_buff是一個複雜的雙向鏈表,在他結構中有next和prev指針,分別指向鏈表的下一個節點和前一個節點。並且爲了某些需求(不知道是哪些目前)需要很快定位到鏈表頭部,所以還有一個指向鏈表頭部的指針list(我在2.6.25內核沒有發現這個指針)。
sk_buff_head結構是:
struct sk_buff_head {
/* These two members must be first. */
struct sk_buff *next;
struct sk_buff *prev;
__u32 qlen; //代表元素節點數目
spinlock_t lock; //加鎖,防止對錶的併發訪問
};
struct sock *sk
這個指針指向一個套接字sock數據結構。當數據在本地產生或者本地進程接受時,需要這個指針;裏面的數據會有tcp/udp和用戶態程序使用。如果是轉發此指針爲NULL
unsigned int len
緩衝區中數據塊大小。長度包括:主要緩衝區(head所指)的數據以及一些片斷(fragment)的數據。當包在協議棧向上或向下走時,其大小會變,因爲有頭部的丟棄和添加。
unsigned int data_len
片段中數據大小
unsigned int mac_len
mac包頭大小
atomic_t users
引用計數,使用這個sk_buff的使用者的數目,可能有多個函數要使用同一個sk_buff所以防止提前釋放掉,設置此計數
unsigned int truesize
此緩衝區總大小,包括sk_buff。sk_buff只不過是個指針的集合,他所指的纔是真正的數據區,所以是兩部分。(見下圖)
sk_buff_data_t tail;
sk_buff_data_t end;
unsigned char *head, *data;
這些指針很重要,他們指向的是真正的數據區,他們的邊界。head和end指向的是數據區的開端和尾端(注意和data,tail區別)如下圖,data和tail指向的是實際數據的開頭和結尾。
因爲數據區在協議棧走的時候要一層層添加或去掉一些數據(比如報頭)所以申請一塊大的足夠的內存,然後在往裏放東西。真實的實際數據可能用不了這麼多,所以用data,tail指向真實的,head,tail指向邊界。剛開始沒填充數據時前三個指針指向的是一個地方。
void (*destructor) (…….)
此函數指針被初始化一個函數,當此緩衝區刪除時,完成某些工作。
通用字段
struct timeval stamp(2.6.25沒有,估計是ktime_t tstamp)
時間戳,表示何時被接受或有時表示包預定的傳輸時間
struct net_device *dev
描述一個網絡設備,我會以後分析他。
sk_buff_data_t transport_header; //L4
sk_buff_data_t network_header; //L3
sk_buff_data_t mac_header; //L2
這些指針分別指向報文頭部,和2.4版本比較有了變化,不再是聯合體,使用更加方便了,Linux給出了很方便的函數直接定位到各層的頭部。下圖是2.4版本的,只是說明一下。
struct dst_entry dst
路由子系統使用。目前不知道怎麼回事呢。據說比較複雜。
char cb[40]
緩衝控制區,用來存儲私有信息的空間。比如tcp用這個空間存儲一個結構體tcp_skb_cb ,可以用宏TCP_SKB_CB(__skb)定位到他,然後使用裏面的變量。
ip_summed:2
__wsum csum;
校驗和
unsigned char pkt_type
根據L2層幀的目的地址進行類型劃分。
unsigned char cloned
表示該結構是另一個sk_buff克隆的。
__u32 priority;
QoS等級
__be16 protocol;
從L2層設備驅動看使用在下一個較高層的協議。
功能專用字段
Linux是模塊化的,你編譯時可以帶上特定功能,比如netfilter等,相應的字段纔會生效。應該是那些預定義控制的。
管理函數
下面這個圖是:(a*)skb_put; (b*) skb_push; (c*) skb_pull (d*) skb_reserve的使用,主要是對skb_buf所指向的數據區的指針移動。(數據預留以及對齊)
下圖是用skb_reserve函數,把一個14字節的ethernet幀拷貝到緩衝區。skb_reserve(skb, 2), 2表示16字節對齊。14+2=16
下圖是穿過協議棧從tcp層向下到鏈路層的過程
分配內存:
alloc_skb 分配緩衝區和一個sk_buff結構
dev_alloc_skb 設備驅動程序使用的緩衝區分配函數
釋放內存:
kfree_skb 只有skb->users計數器爲1時才釋放
dev_kfree_skb
緩衝區克隆函數 skb_clone
列表管理函數:
skb_queue_head_init
隊列初始化
skb_queue_head , skb_queue_tail
把一個緩衝區添加到隊列頭或尾
skb_dequeue, skb_dequeue_tail
從頭或尾去掉
skb_queue_purge
把隊列變空
skb_queue_walk
循環隊列每個元素
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->內核也新增了幾個函數,來提供獲取這些偏移的接口:
#ifdef NET_SKBUFF_DATA_USES_OFFSET
如果使用了offset來表示偏移的話,就是說是一個相對偏移的情況:
static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
{
return skb->head + skb->transport_header;
}
static inline void skb_reset_transport_header(struct sk_buff *skb)
{
skb->transport_header = skb->data - skb->head;
}
static inline void skb_set_transport_header(struct sk_buff *skb,
const int offset)
{
skb_reset_transport_header(skb);
skb->transport_header += offset;
}
static inline unsigned char *skb_network_header(const struct sk_buff *skb)
{
return skb->head + skb->network_header;
}
static inline void skb_reset_network_header(struct sk_buff *skb)
{
skb->network_header = skb->data - skb->head;
}
static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
{
skb_reset_network_header(skb);
skb->network_header += offset;
}
static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
{
return skb->head + skb->mac_header;
}
static inline int skb_mac_header_was_set(const struct sk_buff *skb)
{
return skb->mac_header != ~0U;
}
static inline void skb_reset_mac_header(struct sk_buff *skb)
{
skb->mac_header = skb->data - skb->head;
}
static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
{
skb_reset_mac_header(skb);
skb->mac_header += offset;
}
#else /* NET_SKBUFF_DATA_USES_OFFSET */
不使用相對偏移的情況
static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
{
return skb->transport_header;
}
static inline void skb_reset_transport_header(struct sk_buff *skb)
{
skb->transport_header = skb->data;
}
static inline void skb_set_transport_header(struct sk_buff *skb,
const int offset)
{
skb->transport_header = skb->data + offset;
}
static inline unsigned char *skb_network_header(const struct sk_buff *skb)
{
return skb->network_header;
}
static inline void skb_reset_network_header(struct sk_buff *skb)
{
skb->network_header = skb->data;
}
static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
{
skb->network_header = skb->data + offset;
}
static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
{
return skb->mac_header;
}
static inline int skb_mac_header_was_set(const struct sk_buff *skb)
{
return skb->mac_header != NULL;
}
static inline void skb_reset_mac_header(struct sk_buff *skb)
{
skb->mac_header = skb->data;
}
static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
{
skb->mac_header = skb->data + offset;
}
#endif /* NET_SKBUFF_DATA_USES_OFFSET */
、TCP層獲取相關偏移的函數
static inline struct tcphdr *tcp_hdr(const struct sk_buff *skb)
{
return (struct tcphdr *)skb_transport_header(skb);
}
這個函數用來獲得sk_buff結構中TCP頭的指針
static inline unsigned int tcp_hdrlen(const struct sk_buff *skb)
{
return tcp_hdr(skb)->doff * 4;
}
這個函數用來獲得TCP頭的長度
static inline unsigned int tcp_optlen(const struct sk_buff *skb)
{
return (tcp_hdr(skb)->doff - 5) * 4;
}
獲取tcp option的長度
、IP相關的函數
static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
{
return (struct iphdr *)skb_network_header(skb);
}
該函數獲得ip頭
static inline struct iphdr *ipip_hdr(const struct sk_buff *skb)
{
return (struct iphdr *)skb_transport_header(skb);
}
該函數獲得ipip頭,實際上偏移已經跑到了傳輸層的開始
、MAC相關函數
static inline struct ebt_802_3_hdr *ebt_802_3_hdr(const struct sk_buff *skb)
{
return (struct ebt_802_3_hdr *)skb_mac_header(skb);
}
獲取802.3MAC頭指針。
static inline struct ethhdr *eth_hdr(const struct sk_buff *skb)
{
return (struct ethhdr *)skb_mac_header(skb);
}
獲取以太網MAC頭指針。以太網頭指針結構體:
struct ethhdr {
unsigned char h_dest[ETH_ALEN]; /* destination eth addr */
unsigned char h_source[ETH_ALEN]; /* source ether addr */
__be16 h_proto; /* packet type ID field */
} __attribute__((packed));
內核中網絡地址轉化爲字符串形式的IP地址的宏定義:
#define NIPQUAD(addr) \
((unsigned char *)&addr)[0], \
((unsigned char *)&addr)[1], \
((unsigned char *)&addr)[2], \
((unsigned char *)&addr)[3]
#define NIPQUAD_FMT "%u.%u.%u.%u"