原创文章是freas_1990,转载请注明出处:http://blog.csdn.net/freas_1990/article/details/23795587

在Linux 2.6一旦(不包含2.6,对于更详细的调查是不是版本号),控制块的概念,各种协议的状态管理还出于比較混乱的状态。

Linux 2.6以后。传输控制块机制使代码看起来比較规整了。

创建传输控制块:

  1. /*
  2. * Create an inet socket.
  3. */
  4.  
  5. static int inet_create(struct socket *sock, int protocol)
  6. {
  7. struct sock *sk;
  8. struct list_head *p;
  9. struct inet_protosw *answer;
  10. struct inet_opt *inet;
  11. int err = -ENOBUFS;
  12.  
  13. sock->state = SS_UNCONNECTED;
  14. sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
  15. inet_sk_slab(protocol));
  16. if (!sk)
  17. goto out;
  18.  
  19. /* Look for the requested type/protocol pair. */
  20. answer = NULL;
  21. rcu_read_lock();
  22. list_for_each_rcu(p, &inetsw[sock->type]) {
  23. answer = list_entry(p, struct inet_protosw, list);
  24.  
  25. /* Check the non-wild match. */
  26. if (protocol == answer->protocol) {
  27. if (protocol != IPPROTO_IP)
  28. break;
  29. } else {
  30. /* Check for the two wild cases. */
  31. if (IPPROTO_IP == protocol) {
  32. protocol = answer->protocol;
  33. break;
  34. }
  35. if (IPPROTO_IP == answer->protocol)
  36. break;
  37. }
  38. answer = NULL;
  39. }
  40.  
  41. err = -ESOCKTNOSUPPORT;
  42. if (!answer)
  43. goto out_sk_free;
  44. err = -EPERM;
  45. if (answer->capability > 0 && !capable(answer->capability))
  46. goto out_sk_free;
  47. err = -EPROTONOSUPPORT;
  48. if (!protocol)
  49. goto out_sk_free;
  50. err = 0;
  51. sock->ops = answer->ops;
  52. sk->sk_prot = answer->prot;
  53. sk->sk_no_check = answer->no_check;
  54. if (INET_PROTOSW_REUSE & answer->flags)
  55. sk->sk_reuse = 1;
  56. rcu_read_unlock();
  57.  
  58. inet = inet_sk(sk);
  59.  
  60. if (SOCK_RAW == sock->type) {
  61. inet->num = protocol;
  62. if (IPPROTO_RAW == protocol)
  63. inet->hdrincl = 1;
  64. }
  65.  
  66. if (ipv4_config.no_pmtu_disc)
  67. inet->pmtudisc = IP_PMTUDISC_DONT;
  68. else
  69. inet->pmtudisc = IP_PMTUDISC_WANT;
  70.  
  71. inet->id = 0;
  72.  
  73. sock_init_data(sock, sk);
  74. sk_set_owner(sk, THIS_MODULE);
  75.  
  76. sk->sk_destruct = inet_sock_destruct;
  77. sk->sk_zapped = 0;
  78. sk->sk_family = PF_INET;
  79. sk->sk_protocol = protocol;
  80. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  81.  
  82. inet->uc_ttl = -1;
  83. inet->mc_loop = 1;
  84. inet->mc_ttl = 1;
  85. inet->mc_index = 0;
  86. inet->mc_list = NULL;
  87.  
  88. #ifdef INET_REFCNT_DEBUG
  89. atomic_inc(&inet_sock_nr);
  90. #endif
  91.  
  92. if (inet->num) {
  93. /* It assumes that any protocol which allows
  94. * the user to assign a number at socket
  95. * creation time automatically
  96. * shares.
  97. */
  98. inet->sport = htons(inet->num);
  99. /* Add to protocol hash chains. */
  100. sk->sk_prot->hash(sk);
  101. }
  102.  
  103. if (sk->sk_prot->init) {
  104. err = sk->sk_prot->init(sk);
  105. if (err)
  106. inet_sock_release(sk);
  107. }
  108. out:
  109. return err;
  110. out_sk_free:
  111. rcu_read_unlock();
  112. sk_free(sk);
  113. goto out;
  114. }

这里的sk_alloc是重点:

  1. sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
  2. inet_sk_slab(protocol));

inet_sk_size定义例如以下:

  1. static __inline__ int inet_sk_size(int protocol)
  2. {
  3. int rc = sizeof(struct tcp_sock);
  4.  
  5. if (protocol == IPPROTO_UDP)
  6. rc = sizeof(struct udp_sock);
  7. else if (protocol == IPPROTO_RAW)
  8. rc = sizeof(struct raw_sock);
  9. return rc;
  10. }

它会依据详细的传输层协议定义返回对应的传输控制块的大小。

在socket里,sock指针仅仅是一个“泛型”,它可能指向struct sock,struct tcp_sock,struct udp_sock,根据该协议的细节。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Linux解析内核源代码——传输控制块诞生的更多相关文章

  1. 【linux】内核源代码下载与阅读

      原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog. ...

  2. 编译Android4.3内核源代码

     --------------------------------------------------------------------------------------------------- ...

  3. Linux内核源代码解析——TCP状态转移图以及其实现

    本文原创为freas_1990,转载请标明出处http://blog.csdn.net/freas_1990/article/details/10223581 TCP状态转移的原理并不高深,但是处理逻 ...

  4. Linux内核源代码情景分析系列

    http://blog.sina.com.cn/s/blog_6b94d5680101vfqv.html Linux内核源代码情景分析---第五章 文件系统  5.1 概述 构成一个操作系统最重要的就 ...

  5. linux device tree源代码解析--转

    //Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...

  6. Linux内核源代码获取教程

    Linux内核源代码获取方法 什么叫Linux 什么叫Linux内核 Linux内核源代码的获取 什么叫Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UN ...

  7. 在windows下解压缩Linux内核源代码出现重复文件原因

    在windows下解压缩Linux内核源代码出现重复文件原因 2009年06月30日 13:35 来源:ChinaUnix博客 作者:embededgood 编辑:周荣茂     原因一.因为在Lin ...

  8. 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...

  9. ARM linux解析之压缩内核zImage的启动过程

    ARM linux解析之压缩内核zImage的启动过程 semilog@163.com 首先,我们要知道在zImage的生成过程中,是把arch/arm/boot/compressed/head.s  ...

随机推荐

  1. ubuntu 系统设置bugzilla制

    随着时间的推移.在大脑中形成的记忆总会慢慢的淡去.人的记忆力就是这样.所以最好的办法就是形成博客去记录下来,一方面给自己以后回想用.一方面也算是自己的一个积累.所以一旦选择了一个行业,最好不要轻 易转 ...

  2. android选择和裁剪图像拍摄的图像

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/39994913 近期从曾经的项目中扒下来一个经常使用的模块.在这里有必要记录一下的. ...

  3. POJ 3264-Balanced Lineup(段树:单点更新,间隔查询)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 ...

  4. [Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误

    dele_id = Fee_details_invoices.objects.filter(fee_detail_id__in=fee_id_list, return_type='2').values ...

  5. 【PHP】PHP5.4.0版本号ChangeLog具体解释(上)

    前言 随着大量的框架使用composer和namespace,渐渐的线上环境也从之前的5.3变成了5.4或者5.5甚至5.6,随着7月份PHP7的公布,会有很多其它的公司採用新版本号. 之前好久就想写 ...

  6. Java乔晓松-android的四大组件之一Service(服务的绑定)

    android的四大组件之一Service(服务的绑定) 怎么绑定服务,又怎么解除服务,代码如下: MainActivity.java源码: package com.example.lesson14_ ...

  7. Android含文档server结束(client UI接口异步请求的一部分)三

    在本文中,AsyncTask为了实现异步请求,详细代码如下所示的: public class downloadActivity extends Activity { private TextView ...

  8. TimesTen更改CacheGroup管理用户ORACLE结束和TT结束password【TimesTen操作和维修基地】

    password管理一直操作的一部分的安全管理和维护.CacheGroup管理用户password虽然并不复杂变化.然而,这是用于生产,改不好比较easy导致失败.简介点击这里CacheGroup管理 ...

  9. Blend4精选案例图解教程(二):找张图片玩特效

    原文:Blend4精选案例图解教程(二):找张图片玩特效 Blend中的特效给了我们在处理资源时更多的想象空间,合理地运用特效往往会得到梦幻般效果,本次教程展示对图片应用特效的常规操作,当然特效不仅限 ...

  10. LayoutInflater使用

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会依据情况在代码中自己定义控件,这就须要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作 ...