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

Linux内核--网络协议栈深入分析(四)--套接字内核初始化和创建过程

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

3.2、INET层socket(inet_socket)和传输层socket(struct sock)创建

函数inet_create完成了上述功能,并初始化了sock的属性值,将socket的sk属性指向sock结构

  1. static int inet_create(struct net *net, struct socket *sock, int protocol,  
  2.                int kern)  
  3. {  
  4.     struct sock *sk;  
  5.     struct inet_protosw *answer;  
  6.     struct inet_sock *inet;  
  7.     struct proto *answer_prot;  
  8.     unsigned char answer_flags;  
  9.     char answer_no_check;  
  10.     int try_loading_module = 0;  
  11.     int err;  
  12.   
  13.     if (unlikely(!inet_ehash_secret))  
  14.         if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)  
  15.             build_ehash_secret();  
  16.   
  17.     sock->state = SS_UNCONNECTED;  
  18.   
  19.     /* Look for the requested type/protocol pair. */  
  20. lookup_protocol:  
  21.     err = -ESOCKTNOSUPPORT;  
  22.     rcu_read_lock();  
  23.     //根据传输层协议的类型创建sock结构   
  24.     //遍历inetsw链表   
  25.     list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {  
  26.   
  27.         err = 0;  
  28.         /* Check the non-wild match. */  
  29.         if (protocol == answer->protocol) {  
  30.             if (protocol != IPPROTO_IP)  
  31.                 break;//找到了适配的inetsw[]元素   
  32.         } else {//处理RAW类型的套接字   
  33.             /* Check for the two wild cases. */  
  34.             if (IPPROTO_IP == protocol) {  
  35.                 protocol = answer->protocol;  
  36.                 break;  
  37.             }  
  38.             if (IPPROTO_IP == answer->protocol)  
  39.                 break;  
  40.         }  
  41.         err = -EPROTONOSUPPORT;  
  42.     }  
  43.     //到这里answer指向了合适的inetsw结构,若是TCP协议,answer指向内容如下   
  44.     /* 
  45.     *   .type =       SOCK_STREAM, 
  46.     *   .protocol =   IPPROTO_TCP, 
  47.     *   .prot =       &tcp_prot, 
  48.     *   .ops =        &inet_stream_ops, 
  49.     *   .no_check =   0, 
  50.     *   .flags =      INET_PROTOSW_PERMANENT | 
  51.     *             INET_PROTOSW_ICSK, 
  52.     */  
  53.     if (unlikely(err)) {  
  54.         if (try_loading_module < 2) {  
  55.             rcu_read_unlock();  
  56.             /* 
  57.              * Be more specific, e.g. net-pf-2-proto-132-type-1 
  58.              * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM) 
  59.              */  
  60.             if (++try_loading_module == 1)  
  61.                 request_module("net-pf-%d-proto-%d-type-%d",  
  62.                            PF_INET, protocol, sock->type);  
  63.             /* 
  64.              * Fall back to generic, e.g. net-pf-2-proto-132 
  65.              * (net-pf-PF_INET-proto-IPPROTO_SCTP) 
  66.              */  
  67.             else  
  68.                 request_module("net-pf-%d-proto-%d",  
  69.                            PF_INET, protocol);  
  70.             goto lookup_protocol;  
  71.         } else  
  72.             goto out_rcu_unlock;  
  73.     }  
  74.   
  75.     err = -EPERM;  
  76.     if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))  
  77.         goto out_rcu_unlock;  
  78.   
  79.     err = -EAFNOSUPPORT;  
  80.     if (!inet_netns_ok(net, protocol))  
  81.         goto out_rcu_unlock;  
  82.   
  83.     sock->ops = answer->ops;  
  84.     answer_prot = answer->prot;  
  85.     answer_no_check = answer->no_check;  
  86.     answer_flags = answer->flags;  
  87.     rcu_read_unlock();  
  88.   
  89.     WARN_ON(answer_prot->slab == NULL);  
  90.   
  91.     err = -ENOBUFS;  
  92.     //分配sock结构体内存   
  93.     sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);  
  94.     if (sk == NULL)  
  95.         goto out;  
  96.   
  97.     err = 0;  
  98.     sk->sk_no_check = answer_no_check;  
  99.     if (INET_PROTOSW_REUSE & answer_flags)  
  100.         sk->sk_reuse = 1;  
  101.   
  102.     inet = inet_sk(sk);  
  103.     inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;  
  104.   
  105.     inet->nodefrag = 0;  
  106.   
  107.     if (SOCK_RAW == sock->type) {  
  108.         inet->inet_num = protocol;  
  109.         if (IPPROTO_RAW == protocol)  
  110.             inet->hdrincl = 1;  
  111.     }  
  112.   
  113.     if (ipv4_config.no_pmtu_disc)  
  114.         inet->pmtudisc = IP_PMTUDISC_DONT;  
  115.     else  
  116.         inet->pmtudisc = IP_PMTUDISC_WANT;  
  117.   
  118.     inet->inet_id = 0;  
  119.     //对sk进行初始化设置并将sock中的sk指针指向sk结构   
  120.     sock_init_data(sock, sk);  
  121.   
  122.     //进一步设置sk的其他属性信息   
  123.     sk->sk_destruct     = inet_sock_destruct;  
  124.     sk->sk_protocol     = protocol;  
  125.     sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;  
  126.   
  127.     inet->uc_ttl = -1;  
  128.     inet->mc_loop    = 1;  
  129.     inet->mc_ttl = 1;  
  130.     inet->mc_all = 1;  
  131.     inet->mc_index   = 0;  
  132.     inet->mc_list    = NULL;  
  133.   
  134.     sk_refcnt_debug_inc(sk);  
  135.   
  136.     if (inet->inet_num) {  
  137.         /* It assumes that any protocol which allows 
  138.          * the user to assign a number at socket 
  139.          * creation time automatically 
  140.          * shares. 
  141.          */  
  142.         inet->inet_sport = htons(inet->inet_num);  
  143.         /* Add to protocol hash chains. */  
  144.         sk->sk_prot->hash(sk);//调用inet_hash函数   
  145.     }  
  146.       
  147.     if (sk->sk_prot->init) {  
  148.         err = sk->sk_prot->init(sk);//调用tcp_v4_init_sock函数进行进一步的初始化,由于在函数sk_alloc中一些属性被设置成0了,所以在此调用进行初始化   
  149.         if (err)  
  150.             sk_common_release(sk);  
  151.     }  
  152. out:  
  153.     return err;  
  154. out_rcu_unlock:  
  155.     rcu_read_unlock();  
  156.     goto out;  
  157. }  

关于套接字struct sock与struct inet_sock、struct tcp_sock、struct inet_connection_sock结构之间的关系有待进一步了解。

更多请查看 Linux内核--网络内核实现分析

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

       

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