socket 数据结构
{Back to Index}
Table of Contents
1. Socket 定义
struct socket { socket_state state; short type; unsigned long flags; struct file *file; struct sock *sk; const struct proto_ops *ops; /* Might change with IPV6_ADDRFORM or MPTCP. */ struct socket_wq wq; };
2. Socket 文件视角
3. Socket 操作
内核会创建:
- struct file(文件抽象层)
- struct socket(BSD socket 抽象层)
- struct sock(网络协议栈内部传输层抽象)
socket 相关的操作接口定义在 inet_stream_ops 函数集合中,负责对上给用户提供接口。
而 socket 与内核协议栈之间的操作接口定义在 struct sock 中的 sk_prot 指针上,这里指向 tcp_prot 协议操作函数集合。
对 socket 发起的系统IO调用,在内核中首先会调用 socket 的文件结构 struct file 中的 file_operations 文件操作集合,然后调用 struct socket 中的 ops 指向的 inet_stream_opssocket 操作函数,最终调用到 struct sock 中 sk_prot 指针指向的 tcp_prot 内核协议栈操作函数接口集合。
4. epoll_create 创建 struct eventpoll 对象
ep_poll_callback 回调函数中就可以根据 socket 等待队列中的等待项 wait ,通过 container_of 宏找到 eppoll_entry ,继而找到 epitem 。
等待项 wait_queue_t 中的 private 设置的是 null ,因为这里 socket 是交给 epoll 来管理的,阻塞在 socket 上的进程是也由 epoll 来唤醒。在等待项 wait_queue_t 注册的 func 是 ep_poll_callback 而不是 autoremove_wake_function ,阻塞进程并不需要 autoremove_wake_function 来唤醒,所以这里设置 private 为 null 。
