你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux内核--网络协议栈深入分析(三)--INET协议族socket和传输层sock

[日期:2012-09-15] 来源:csdn  作者:闫明 [字体:大 中 小]

最后一个属性就指向了上面所述的操作集。若使用TCP协议,ops就是inet_stream_ops,若是UDP协议,ops就是inet_dgram_ops。

short type属性的取值可以是如下值:

  1. enum sock_type {  
  2.     SOCK_DGRAM  = 1,  
  3.     SOCK_STREAM = 2,  
  4.     SOCK_RAW    = 3,  
  5.     SOCK_RDM    = 4,  
  6.     SOCK_SEQPACKET  = 5,  
  7.     SOCK_DCCP   = 6,  
  8.     SOCK_PACKET = 10,  
  9. };  

传输层的协议操作集结构体定义:

  1. struct proto {  
  2.     void            (*close)(struct sock *sk,   
  3.                     long timeout);  
  4.     int         (*connect)(struct sock *sk,  
  5.                         struct sockaddr *uaddr,   
  6.                     int addr_len);  
  7.     int         (*disconnect)(struct sock *sk, int flags);  
  8.   
  9.     struct sock *       (*accept) (struct sock *sk, int flags, int *err);  
  10.   
  11.     int         (*ioctl)(struct sock *sk, int cmd,  
  12.                      unsigned long arg);  
  13.     int         (*init)(struct sock *sk);  
  14.     void            (*destroy)(struct sock *sk);  
  15.     void            (*shutdown)(struct sock *sk, int how);  
  16.     int         (*setsockopt)(struct sock *sk, int level,   
  17.                     int optname, char __user *optval,  
  18.                     unsigned int optlen);  
  19.     int         (*getsockopt)(struct sock *sk, int level,   
  20.                     int optname, char __user *optval,   
  21.                     int __user *option);       
  22. #ifdef CONFIG_COMPAT   
  23.     int         (*compat_setsockopt)(struct sock *sk,  
  24.                     int level,  
  25.                     int optname, char __user *optval,  
  26.                     unsigned int optlen);  
  27.     int         (*compat_getsockopt)(struct sock *sk,  
  28.                     int level,  
  29.                     int optname, char __user *optval,  
  30.                     int __user *option);  
  31.     int         (*compat_ioctl)(struct sock *sk,  
  32.                     unsigned int cmd, unsigned long arg);  
  33. #endif   
  34.     int         (*sendmsg)(struct kiocb *iocb, struct sock *sk,  
  35.                        struct msghdr *msg, size_t len);  
  36.     int         (*recvmsg)(struct kiocb *iocb, struct sock *sk,  
  37.                        struct msghdr *msg,  
  38.                     size_t len, int noblock, int flags,   
  39.                     int *addr_len);  
  40.     int         (*sendpage)(struct sock *sk, struct page *page,  
  41.                     int offset, size_t size, int flags);  
  42.     int         (*bind)(struct sock *sk,   
  43.                     struct sockaddr *uaddr, int addr_len);  
  44.   
  45.     int         (*backlog_rcv) (struct sock *sk,   
  46.                         struct sk_buff *skb);  
  47.   
  48.     /* Keeping track of sk's, looking them up, and port selection methods. */  
  49.     void            (*hash)(struct sock *sk);  
  50.     void            (*unhash)(struct sock *sk);  
  51.     void            (*rehash)(struct sock *sk);  
  52.     int         (*get_port)(struct sock *sk, unsigned short snum);  
  53.     void            (*clear_sk)(struct sock *sk, int size);  
  54.   
  55.     /* Keeping track of sockets in use */  
  56. #ifdef CONFIG_PROC_FS   
  57.     unsigned int        inuse_idx;  
  58. #endif   
  59.   
  60.     /* Memory pressure */  
  61.     void            (*enter_memory_pressure)(struct sock *sk);  
  62.     atomic_long_t       *memory_allocated;  /* Current allocated memory. */  
  63.     struct percpu_counter   *sockets_allocated; /* Current number of sockets. */  
  64.     /* 
  65.      * Pressure flag: try to collapse. 
  66.      * Technical note: it is used by multiple contexts non atomically. 
  67.      * All the __sk_mem_schedule() is of this nature: accounting 
  68.      * is strict, actions are advisory and have some latency. 
  69.      */  
  70.     int         *memory_pressure;  
  71.     long            *sysctl_mem;  
  72.     int         *sysctl_wmem;  
  73.     int         *sysctl_rmem;  
  74.     int         max_header;  
  75.     bool            no_autobind;  
  76.   
  77.     struct kmem_cache   *slab;  
  78.     unsigned int        obj_size;  
  79.     int         slab_flags;  
  80.   
  81.     struct percpu_counter   *orphan_count;  
  82.   
  83.     struct request_sock_ops *rsk_prot;  
  84.     struct timewait_sock_ops *twsk_prot;  
  85.   
  86.     union {  
  87.         struct inet_hashinfo    *hashinfo;  
  88.         struct udp_table    *udp_table;  
  89.         struct raw_hashinfo *raw_hash;  
  90.     } h;  
  91.   
  92.     struct module       *owner;  
  93.   
  94.     char            name[32];  
  95.   
  96.     struct list_head    node;  
  97. #ifdef SOCK_REFCNT_DEBUG   
  98.     atomic_t        socks;  
  99. #endif   
  100. };  
该结构体和proto_ops的区别是:该结构体和具体的传输层协议相关,其中的函数指针指向对应的协议的相应的���作函数。

TCP协议的操作集定义如下:

  1. struct proto tcp_prot = {  
  2.     .name           = "TCP",  
  3.     .owner          = THIS_MODULE,  
  4.     .close          = tcp_close,  
  5.     .connect        = tcp_v4_connect,  
  6.     .disconnect     = tcp_disconnect,  
  7.     .accept         = inet_csk_accept,  
  8.     .ioctl          = tcp_ioctl,  
  9.     .init           = tcp_v4_init_sock,  
  10.     .destroy        = tcp_v4_destroy_sock,  
  11.     .shutdown       = tcp_shutdown,  
  12.     .setsockopt     = tcp_setsockopt,  
  13.     .getsockopt     = tcp_getsockopt,  
  14.     .recvmsg        = tcp_recvmsg,  
  15.     .sendmsg        = tcp_sendmsg,  
  16.     .sendpage       = tcp_sendpage,  
  17.     .backlog_rcv        = tcp_v4_do_rcv,  
  18.     .hash           = inet_hash,  
  19.     .unhash         = inet_unhash,  
  20.     .get_port       = inet_csk_get_port,  
  21.     .enter_memory_pressure  = tcp_enter_memory_pressure,  
  22.     .sockets_allocated  = &tcp_sockets_allocated,  
  23.     .orphan_count       = &tcp_orphan_count,  
  24.     .memory_allocated   = &tcp_memory_allocated,  
  25.     .memory_pressure    = &tcp_memory_pressure,  
  26.     .sysctl_mem     = sysctl_tcp_mem,  
  27.     .sysctl_wmem        = sysctl_tcp_wmem,  
  28.     .sysctl_rmem        = sysctl_tcp_rmem,  
  29.     .max_header     = MAX_TCP_HEADER,  
  30.     .obj_size       = sizeof(struct tcp_sock),  
  31.     .slab_flags     = SLAB_DESTROY_BY_RCU,  
  32.     .twsk_prot      = &tcp_timewait_sock_ops,  
  33.     .rsk_prot       = &tcp_request_sock_ops,  
  34.     .h.hashinfo     = &tcp_hashinfo,  
  35.     .no_autobind        = true,  
  36. #ifdef CONFIG_COMPAT   
  37.     .compat_setsockopt  = compat_tcp_setsockopt,  
  38.     .compat_getsockopt  = compat_tcp_getsockopt,  
  39. #endif   
  40. };  
UDP协议的操作集则为:
  1. struct proto udp_prot = {  
  2.     .name          = "UDP",  
  3.     .owner         = THIS_MODULE,  
  4.     .close         = udp_lib_close,  
  5.     .connect       = ip4_datagram_connect,  
  6.     .disconnect    = udp_disconnect,  
  7.     .ioctl         = udp_ioctl,  
  8.     .destroy       = udp_destroy_sock,  
  9.     .setsockopt    = udp_setsockopt,  
  10.     .getsockopt    = udp_getsockopt,  
  11.     .sendmsg       = udp_sendmsg,  
  12.     .recvmsg       = udp_recvmsg,  
  13.     .sendpage      = udp_sendpage,  
  14.     .backlog_rcv       = __udp_queue_rcv_skb,  
  15.     .hash          = udp_lib_hash,  
  16.     .unhash        = udp_lib_unhash,  
  17.     .rehash        = udp_v4_rehash,  
  18.     .get_port      = udp_v4_get_port,  
  19.     .memory_allocated  = &udp_memory_allocated,  
  20.     .sysctl_mem    = sysctl_udp_mem,  
  21.     .sysctl_wmem       = &sysctl_udp_wmem_min,  
  22.     .sysctl_rmem       = &sysctl_udp_rmem_min,  
  23.     .obj_size      = sizeof(struct udp_sock),  
  24.     .slab_flags    = SLAB_DESTROY_BY_RCU,  
  25.     .h.udp_table       = &udp_table,  
  26. #ifdef CONFIG_COMPAT   
  27.     .compat_setsockopt = compat_udp_setsockopt,  
  28.     .compat_getsockopt = compat_udp_getsockopt,  
  29. #endif   
  30.     .clear_sk      = sk_prot_clear_portaddr_nulls,  
  31. };  

现在介绍struct socket结构体中一个属性struct sock类型的结构体指针,这个结构体就是传输层的套接字,所有套接字通过该结构来使用网络协议的所有服务。定义如下:

  1. struct sock {  
  2.     /* 
  3.      * Now struct inet_timewait_sock also uses sock_common, so please just 
  4.      * don't add nothing before this first member (__sk_common) --acme 
  5.      */  
  6.     struct sock_common  __sk_common;  
  7. #define sk_node         __sk_common.skc_node   
  8. #define sk_nulls_node       __sk_common.skc_nulls_node   
  9. #define sk_refcnt       __sk_common.skc_refcnt   
  10. #define sk_tx_queue_mapping __sk_common.skc_tx_queue_mapping   
  11.   
  12. #define sk_dontcopy_begin   __sk_common.skc_dontcopy_begin   
  13. #define sk_dontcopy_end     __sk_common.skc_dontcopy_end   
  14. #define sk_hash         __sk_common.skc_hash   
  15. #define sk_family       __sk_common.skc_family   
  16. #define sk_state        __sk_common.skc_state   
  17. #define sk_reuse        __sk_common.skc_reuse   
  18. #define sk_bound_dev_if     __sk_common.skc_bound_dev_if   
  19. #define sk_bind_node        __sk_common.skc_bind_node   
  20. #define sk_prot         __sk_common.skc_prot   
  21. #define sk_net          __sk_common.skc_net   
  22.     socket_lock_t       sk_lock;  
  23.     struct sk_buff_head sk_receive_queue;  
  24.     /* 
  25.      * The backlog queue is special, it is always used with 
  26.      * the per-socket spinlock held and requires low latency 
  27.      * access. Therefore we special case it's implementation. 
  28.      * Note : rmem_alloc is in this structure to fill a hole 
  29.      * on 64bit arches, not because its logically part of 
  30.      * backlog. 
  31.      */  
  32.     struct {  
  33.         atomic_t    rmem_alloc;  
  34.         int     len;  
  35.         struct sk_buff  *head;  
  36.         struct sk_buff  *tail;  
  37.     } sk_backlog;  
  38. #define sk_rmem_alloc sk_backlog.rmem_alloc   
  39.     int         sk_forward_alloc;  
  40. #ifdef CONFIG_RPS   
  41.     __u32           sk_rxhash;  
  42. #endif   
  43.     atomic_t        sk_drops;  
  44.     int         sk_rcvbuf;  
  45.   
  46.     struct sk_filter __rcu  *sk_filter;  
  47.     struct socket_wq __rcu  *sk_wq;  
  48.   
  49. #ifdef CONFIG_NET_DMA   
  50.     struct sk_buff_head sk_async_wait_queue;  
  51. #endif   
  52.   
  53. #ifdef CONFIG_XFRM   
  54.     struct xfrm_policy  *sk_policy[2];  
  55. #endif   
  56.     unsigned long       sk_flags;  
  57.     struct dst_entry    *sk_dst_cache;  
  58.     spinlock_t      sk_dst_lock;  
  59.     atomic_t        sk_wmem_alloc;  
  60.     atomic_t        sk_omem_alloc;  
  61.     int         sk_sndbuf;  
  62.     struct sk_buff_head sk_write_queue;  
  63.     kmemcheck_bitfield_begin(flags);  
  64.     unsigned int        sk_shutdown  : 2,  
  65.                 sk_no_check  : 2,  
  66.                 sk_userlocks : 4,  
  67.                 sk_protocol  : 8,  
  68.                 sk_type      : 16;  
  69.     kmemcheck_bitfield_end(flags);  
  70.     int         sk_wmem_queued;  
  71.     gfp_t           sk_allocation;  
  72.     int         sk_route_caps;  
  73.     int         sk_route_nocaps;  
  74.     int         sk_gso_type;  
  75.     unsigned int        sk_gso_max_size;  
  76.     int         sk_rcvlowat;  
  77.     unsigned long           sk_lingertime;  
  78.     struct sk_buff_head sk_error_queue;  
  79.     struct proto        *sk_prot_creator;  
  80.     rwlock_t        sk_callback_lock;  
  81.     int         sk_err,  
  82.                 sk_err_soft;  
  83.     unsigned short      sk_ack_backlog;  
  84.     unsigned short      sk_max_ack_backlog;  
  85.     __u32           sk_priority;  
  86.     struct pid      *sk_peer_pid;  
  87.     const struct cred   *sk_peer_cred;  
  88.     long            sk_rcvtimeo;  
  89.     long            sk_sndtimeo;  
  90.     void            *sk_protinfo;  
  91.     struct timer_list   sk_timer;  
  92.     ktime_t         sk_stamp;  
  93.     struct socket       *sk_socket;  
  94.     void            *sk_user_data;  
  95.     struct page     *sk_sndmsg_page;  
  96.     struct sk_buff      *sk_send_head;  
  97.     __u32           sk_sndmsg_off;  
  98.     int         sk_write_pending;  
  99. #ifdef CONFIG_SECURITY   
  100.     void            *sk_security;  
  101. #endif   
  102.     __u32           sk_mark;  
  103.     u32         sk_classid;  
  104.     void            (*sk_state_change)(struct sock *sk);  
  105.     void            (*sk_data_ready)(struct sock *sk, int bytes);  
  106.     void            (*sk_write_space)(struct sock *sk);  
  107.     void            (*sk_error_report)(struct sock *sk);  
  108.     int         (*sk_backlog_rcv)(struct sock *sk,  
  109.                           struct sk_buff *skb);    
  110.     void                    (*sk_destruct)(struct sock *sk);  
  111. };  
若sk_family是PF_INET,则sk_type可以取值:SOCK_STREAM,SOCK_DGRAM,SOCK_RAW。其中sk_prot就是指向具体协议的操作集,如TCP协议就为tcp_prot。

若要将协议族操作集和具体协议操作集整合起来为IP协议提供接口,就需要下面的结构体定义:

  1. struct inet_protosw {  
  2.     struct list_head list;  
  3.   
  4.         /* These two fields form the lookup key.  */  
  5.     unsigned short   type;     /* This is the 2nd argument to socket(2). */  
  6.     unsigned short   protocol; /* This is the L4 protocol number.  */  
  7.   
  8.     struct proto     *prot;  
  9.     const struct proto_ops *ops;  
  10.     
  11.     char             no_check;   /* checksum on rcv/xmit/none? */  
  12.     unsigned char    flags;      /* See INET_PROTOSW_* below.  */  
  13. };  
INET三种套接字定义的inetsw_array数组如下:
  1. static struct inet_protosw inetsw_array[] =  
  2. {  
  3.     {  
  4.         .type =       SOCK_STREAM,  
  5.         .protocol =   IPPROTO_TCP,  
  6.         .prot =       &tcp_prot,  
  7.         .ops =        &inet_stream_ops,  
  8.         .no_check =   0,  
  9.         .flags =      INET_PROTOSW_PERMANENT |  
  10.                   INET_PROTOSW_ICSK,  
  11.     },  
  12.   
  13.     {  
  14.         .type =       SOCK_DGRAM,  
  15.         .protocol =   IPPROTO_UDP,  
  16.         .prot =       &udp_prot,  
  17.         .ops =        &inet_dgram_ops,  
  18.         .no_check =   UDP_CSUM_DEFAULT,  
  19.         .flags =      INET_PROTOSW_PERMANENT,  
  20.        },  
  21.   
  22.        {  
  23.         .type =       SOCK_DGRAM,  
  24.         .protocol =   IPPROTO_ICMP,  
  25.         .prot =       &ping_prot,  
  26.         .ops =        &inet_dgram_ops,  
  27.         .no_check =   UDP_CSUM_DEFAULT,  
  28.         .flags =      INET_PROTOSW_REUSE,  
  29.        },  
  30.   
  31.        {  
  32.            .type =       SOCK_RAW,  
  33.            .protocol =   IPPROTO_IP,    /* wild card */  
  34.            .prot =       &raw_prot,  
  35.            .ops =        &inet_sockraw_ops,  
  36.            .no_check =   UDP_CSUM_DEFAULT,  
  37.            .flags =      INET_PROTOSW_REUSE,  
  38.        }  
  39. };  
不过,在初始化的时候我们会将上面数组中的的元素按套接字类型插入inetsw链表数组中。其定义如下:
  1. static struct list_head inetsw[SOCK_MAX];  

下篇将分析套接字的操作函数。更多请查看 Linux内核--网络内核实现分析

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款