浅析sys_socket创建socket的简易流程

浅析sys_socket创建socket的简易流程

sys_socket= >
1. sock_create= > __sock_create= >
pf- > create( net, sock, protocol) ;
inet_create= > 看看都做了什么事情,
sk_alloc= > sk_prot_alloc= > kmalloc( prot- > obj_size, priority) ;
这个prot- > obj_size的大小是从哪里来的呢, 接着看,
sk- > sk_prot = tcp_prot;
他的. obj_size = sizeof ( struct tcp_sock) , 所以
struct tcp_sock {
    /* inet_connection_sock has to be the first member of tcp_sock */
    struct inet_connection_sock inet_conn;
    . . .
}
struct inet_connection_sock {
    /* inet_sock has to be the first member! */
    struct inet_sock icsk_inet;
    . . .
}
struct inet_sock {
    /* sk and pinet6 has to be the first two members of inet_sock */
    struct sock sk;
    . . .
}
sk- > sk_prot- > init( sk) 也就是调用tcp_prot的init, 也就是
tcp_v4_init_sock= > tcp_init_xmit_timers= > inet_csk_init_xmit_timers
( sk, & tcp_write_timer, & tcp_delack_timer, & tcp_keepalive_timer) ;
对tcp使用到的3个timer定时器做初始化.
    icsk- > icsk_retransmit_timer. function = tcp_write_timer;
    icsk- > icsk_delack_timer. function = tcp_delack_timer;
    sk- > sk_timer. function         = tcp_keepalive_timer;
icsk- > icsk_af_ops = & ipv4_specific;
icsk- > icsk_ca_ops = & tcp_init_congestion_ops;
tp- > reordering = sysctl_tcp_reordering;
sk- > sk_write_space = sk_stream_write_space;
icsk- > icsk_sync_mss = tcp_sync_mss;
tp- > af_specific = & tcp_sock_ipv4_specific;
故icsk_af_ops就是ipv4_specific
这样sys_socket的创建工作也接近尾声了【gliethttp. Leith】,
sys_socket= >
2. sock_map_fd= > sock_attach_fd= >
dentry- > d_op = & sockfs_dentry_operations;
init_file( file , sock_mnt, dentry, FMODE_READ | FMODE_WRITE, & socket_file_ops) ;
file - > private_data = sock;
其中init_file= > file - > f_op = fop; 也就是file- > f_op = socket_file_ops;
所以read( ) , wirte( ) , poll( ) 和ioctl( ) 应用程序调用的file- > f_op就是socket_file_ops了,
比如:
read ( ) 对应sock_aio_read网络异步读
write ( ) 对应sock_aio_write网络异步写
ioctl( ) 对应sock_ioctl

socket_file_ops结构体具体实现如下:
static const struct file_operations socket_file_ops = {
    . owner = THIS_MODULE,
    . llseek = no_llseek,
    . aio_read = sock_aio_read,
    . aio_write = sock_aio_write,
    . poll = sock_poll,
    . unlocked_ioctl = sock_ioctl,
# ifdef CONFIG_COMPAT
    . compat_ioctl = compat_sock_ioctl,
# endif
    . mmap = sock_mmap,
    . open = sock_no_open, /* special open code to disallow open via /proc */
    . release = sock_close,
    . fasync = sock_fasync,
    . sendpage = sock_sendpage,
    . splice_write = generic_splice_sendpage,
} ;

你可能感兴趣的:(timer,tcp,socket,struct,Stream,File)