struct sk_buff与socket
作者:gfree.wind@gmail.com
在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组织起来的。