struct sk_buff與socket作者:[email protected]
在Linux2.6中,struct sk_buf承擔了socket的輸入輸出的傳輸緩存的任務。
首先,還是先看struct socket的定義
/**
* struct socket - general BSD socket
* @state: socket state (%SS_CONNECTED, etc)
* @type: socket type (%SOCK_STREAM, etc)
* @flags: socket flags (%SOCK_ASYNC_NOSPACE, etc)
* @ops: protocol specific socket operations
* @file: File back pointer for gc
* @sk: internal networking protocol agnostic socket representation
* @wq: wait queue for several uses
*/
struct socket {
socket_state state;
kmemcheck_bitfield_begin(type);
short type;
kmemcheck_bitfield_end(type);
unsigned long flags;
struct socket_wq *wq;
struct file *file;
struct sock *sk;
const struct proto_ops *ops;
};
代碼中的注釋對于每一個變量說的都很清楚——看到這裡,我先感歎一下,linux2.6的結構體的注釋比老版本要清楚的多。到目前為止,我所看到的關鍵的結構體,都有清晰的注釋。我們可以看出struct socket中的sock變量,是socket變量的工作核心。
那麼現在跳轉到struct sock的定義處。由于struct sock的定義過長,是以隻展示一部分。
struct sock {
/*
* Now struct inet_timewait_sock also uses sock_common, so please just
* don't add nothing before this first member (__sk_common) --acme
*/
struct sock_common __sk_common;
/* skip some codes */
int sk_rcvbuf;
/* skip some codes */
int sk_sndbuf;
struct sk_buff_head sk_receive_queue;
struct sk_buff_head sk_write_queue;
};
其中,sk_rcvbuf和sk_sendbuf分别是接收和發送緩存的位元組數。而struct 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;
可以看出socket的接收和發送緩存是使用一個雙連結清單将sk_buff組織起來的。