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

Socket的实现(Linux内核部分)

[日期:2010-08-23] 来源:Linux社区  作者:Linux [字体:大 中 小]

我们知道每次创建一个socket,都是要依赖于当前的protocol family类型的(后面会分析sys_socket的源码的时候会看到).而在内核中,每种类型的protocol family都会有一个相对应的net_proto_family结构,然后将这个结构注册到内核的net_families数组中,这样我们创建 socket的时候,就可以调用这个数组来创建socket.

我们先来看sock_register的源码,也就是如何将一个net_proto_family注册到相应的数组:

   1. static const struct net_proto_family *net_families[NPROTO] __read_mostly; 
   2.  
   3. int sock_register(const struct net_proto_family *ops) 
   4. { 
   5.     int err; 
   6.  
   7.     if (ops->family >= NPROTO) { 
   8.         printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, 
   9.                NPROTO); 
  10.         return -ENOBUFS; 
  11.     } 
  12.  
  13.     spin_lock(&net_family_lock); 
  14. ///代码非常简单,就是根据类型,然后放到相应的位置. 
  15.     if (net_families[ops->family]) 
  16.         err = -EEXIST; 
  17.     else { 
  18.         net_families[ops->family] = ops; 
  19.         err = 0; 
  20.     } 
  21.     spin_unlock(&net_family_lock); 
  22.  
  23.     printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family); 
  24.     return err; 
  25. } 

我们知道每个协议簇和相应的套接口都对应有好多种组合,因此在协议簇的实现中保存了一个相应的结构来保存这些组合,然后后面就首先通过family然后确定到某个结构,再根据套接口的类型来得到这个结构,并赋值给sock.

这里要注意我们只分析af_inet的实现,其他的协议簇都差不多:

我们来看这个的实现:

   1. ///可以看到这是一个数组,每个元素都是一个链表,也就是每种类型的socket就是一个链表.而这个链表所包含的是不同4层协议的inetsw.可是在inet中,现在每种类型的socket只对应一个4层协议.这里只是为了以后扩展. 
   2. static struct list_head inetsw[SOCK_MAX]; 
   3.  
   4. ///相应的socket的对应的信息的结构. 
   5. struct inet_protosw { 
   6.     struct list_head list; 
   7.  
   8. ///需要这两个key才能定位一个inet_protosw. 
   9.     unsigned short   type;     /* This is the 2nd argument to socket(2). */ 
  10.     unsigned short   protocol; /* This is the L4 protocol number.  */ 
  11.  
  12. ///相应的基于ipv4的4层协议的操作集合. 
  13.     struct proto     *prot; 
  14. ///相应的协议簇的操作信息. 
  15.     const struct proto_ops *ops; 
  16.    
  17.     int              capability; /* Which (if any) capability do
  18.                       * we need to use this socket
  19.                       * interface?
  20.                                       */ 
  21.     char             no_check;   /* checksum on rcv/xmit/none? */ 
  22.     unsigned char    flags;      /* See INET_PROTOSW_* below.  */ 
  23. }; 
  24.  
  25. void inet_register_protosw(struct inet_protosw *p) 
  26. { 
  27.     struct list_head *lh; 
  28.     struct inet_protosw *answer; 
  29.     int protocol = p->protocol; 
  30.     struct list_head *last_perm; 
  31. ............................................. 
  32.     answer = NULL; 
  33.     last_perm = &inetsw[p->type]; 
  34. ///这个操作也很简单,就是将inet_protosw根据套接口类型插入到全局链表数组. 
  35.     list_for_each(lh, &inetsw[p->type]) { 
  36.         answer = list_entry(lh, struct inet_protosw, list); 
  37.  
  38.         /* Check only the non-wild match. */ 
  39.         if (INET_PROTOSW_PERMANENT & answer->flags) { 
  40.             if (protocol == answer->protocol) 
  41.                 break; 
  42.             last_perm = lh; 
  43.         } 
  44.  
  45.         answer = NULL; 
  46.     } 
  47.     if (answer) 
  48.         goto out_permanent; 
  49. ///插入链表. 
  50.     list_add_rcu(&p->list, last_perm); 
  51. .............................. 

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

       

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