Linux解析内核源代码——传输控制块诞生
原创文章是freas_1990,转载请注明出处:http://blog.csdn.net/freas_1990/article/details/23795587
在Linux 2.6一旦(不包含2.6,对于更详细的调查是不是版本号),控制块的概念,各种协议的状态管理还出于比較混乱的状态。
Linux 2.6以后。传输控制块机制使代码看起来比較规整了。
创建传输控制块:
/*
* Create an inet socket.
*/ static int inet_create(struct socket *sock, int protocol)
{
struct sock *sk;
struct list_head *p;
struct inet_protosw *answer;
struct inet_opt *inet;
int err = -ENOBUFS; sock->state = SS_UNCONNECTED;
sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
inet_sk_slab(protocol));
if (!sk)
goto out; /* Look for the requested type/protocol pair. */
answer = NULL;
rcu_read_lock();
list_for_each_rcu(p, &inetsw[sock->type]) {
answer = list_entry(p, struct inet_protosw, list); /* Check the non-wild match. */
if (protocol == answer->protocol) {
if (protocol != IPPROTO_IP)
break;
} else {
/* Check for the two wild cases. */
if (IPPROTO_IP == protocol) {
protocol = answer->protocol;
break;
}
if (IPPROTO_IP == answer->protocol)
break;
}
answer = NULL;
} err = -ESOCKTNOSUPPORT;
if (!answer)
goto out_sk_free;
err = -EPERM;
if (answer->capability > 0 && !capable(answer->capability))
goto out_sk_free;
err = -EPROTONOSUPPORT;
if (!protocol)
goto out_sk_free;
err = 0;
sock->ops = answer->ops;
sk->sk_prot = answer->prot;
sk->sk_no_check = answer->no_check;
if (INET_PROTOSW_REUSE & answer->flags)
sk->sk_reuse = 1;
rcu_read_unlock(); inet = inet_sk(sk); if (SOCK_RAW == sock->type) {
inet->num = protocol;
if (IPPROTO_RAW == protocol)
inet->hdrincl = 1;
} if (ipv4_config.no_pmtu_disc)
inet->pmtudisc = IP_PMTUDISC_DONT;
else
inet->pmtudisc = IP_PMTUDISC_WANT; inet->id = 0; sock_init_data(sock, sk);
sk_set_owner(sk, THIS_MODULE); sk->sk_destruct = inet_sock_destruct;
sk->sk_zapped = 0;
sk->sk_family = PF_INET;
sk->sk_protocol = protocol;
sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; inet->uc_ttl = -1;
inet->mc_loop = 1;
inet->mc_ttl = 1;
inet->mc_index = 0;
inet->mc_list = NULL; #ifdef INET_REFCNT_DEBUG
atomic_inc(&inet_sock_nr);
#endif if (inet->num) {
/* It assumes that any protocol which allows
* the user to assign a number at socket
* creation time automatically
* shares.
*/
inet->sport = htons(inet->num);
/* Add to protocol hash chains. */
sk->sk_prot->hash(sk);
} if (sk->sk_prot->init) {
err = sk->sk_prot->init(sk);
if (err)
inet_sock_release(sk);
}
out:
return err;
out_sk_free:
rcu_read_unlock();
sk_free(sk);
goto out;
}
这里的sk_alloc是重点:
sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
inet_sk_slab(protocol));
inet_sk_size定义例如以下:
static __inline__ int inet_sk_size(int protocol)
{
int rc = sizeof(struct tcp_sock); if (protocol == IPPROTO_UDP)
rc = sizeof(struct udp_sock);
else if (protocol == IPPROTO_RAW)
rc = sizeof(struct raw_sock);
return rc;
}
它会依据详细的传输层协议定义返回对应的传输控制块的大小。
在socket里,sock指针仅仅是一个“泛型”,它可能指向struct sock,struct tcp_sock,struct udp_sock,根据该协议的细节。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Linux解析内核源代码——传输控制块诞生的更多相关文章
- 【linux】内核源代码下载与阅读
原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog. ...
- 编译Android4.3内核源代码
--------------------------------------------------------------------------------------------------- ...
- Linux内核源代码解析——TCP状态转移图以及其实现
本文原创为freas_1990,转载请标明出处http://blog.csdn.net/freas_1990/article/details/10223581 TCP状态转移的原理并不高深,但是处理逻 ...
- Linux内核源代码情景分析系列
http://blog.sina.com.cn/s/blog_6b94d5680101vfqv.html Linux内核源代码情景分析---第五章 文件系统 5.1 概述 构成一个操作系统最重要的就 ...
- linux device tree源代码解析--转
//Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...
- Linux内核源代码获取教程
Linux内核源代码获取方法 什么叫Linux 什么叫Linux内核 Linux内核源代码的获取 什么叫Linux? Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UN ...
- 在windows下解压缩Linux内核源代码出现重复文件原因
在windows下解压缩Linux内核源代码出现重复文件原因 2009年06月30日 13:35 来源:ChinaUnix博客 作者:embededgood 编辑:周荣茂 原因一.因为在Lin ...
- 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6564592 在前一篇文章提到,从源代码树下载下 ...
- ARM linux解析之压缩内核zImage的启动过程
ARM linux解析之压缩内核zImage的启动过程 semilog@163.com 首先,我们要知道在zImage的生成过程中,是把arch/arm/boot/compressed/head.s ...
随机推荐
- HTML5 在canvas绘制一个矩形
笔者:本笃庆军 原文地址:http://blog.csdn.net/qingdujun/article/details/32930501 一.绘制矩形 canvas使用原点(0,0)在左上角的坐标系统 ...
- java输入输出高速
头文件: import java.io.*; 定义: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); ...
- 好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效button
我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...
- UVA - 12130 Summits
Description Problem G - Summits Time limit: 8 seconds You recently started working for the largest m ...
- C++11:强类型枚举(enum)
// C++11之前的enum类型是继承C的,不温不火: // C++11对enum动刀了,加强了类型检查,推出强类型enum类型,眼前一亮 // 使用过QT 的都知道,早就应该这么做了,用的非常爽! ...
- 取缔Chrome装载电脑管家的广告过滤脚本代码
今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...
- UVA 11149 - Power of Matrix(矩阵乘法)
UVA 11149 - Power of Matrix 题目链接 题意:给定一个n*n的矩阵A和k,求∑kiAi 思路:利用倍增去搞.∑kiAi=(1+Ak/2)∑k/2iAi,不断二分就可以 代码: ...
- cocospods 卡在 Analyzing dependencies
參考链接:http://www.cocoachina.com/bbs/read.php? tid=193398 关于pod stetup的详解在这里.对于初次使用CocoaPods的同学,即使你不使用 ...
- 11gR2 Database Services for "Policy" and "Administrator" Managed Databases (文件 ID 1481647.1)
In this Document _afrLoop=1459311711568804&id=1481647.1&displayIndex=6&_afrWindowMode= ...
- WORD中怎样自己主动生成文件夹?
步骤: 1.输入当做标题的文字 2.将文字设置为标题样式 3.光标放在要加入�文件夹的位置 4.选择插入->引用->索引和文件夹->文件夹->确定