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 ...
随机推荐
- 用C设计,用C++编码
昨天晚上看到刘江的blog又补充了好几大段,今天早上又看到云风的人肉trackback,果然还是这种话题引人关注. 云风先是提了一下所谓C++带来的思想包袱(文言文曰“心智包袱”)问题,然 ...
- Java 执行四则运算
四种基本的操作原理是将被转换成后缀缀表达式表达.然后计算. 转换思路和原则.可以参考将中缀表达式转化为后缀表达式 import java.math.BigDecimal; import java.ut ...
- Windows下一个JSP环境配置
一.首先安装JDK 门户: http://write.blog.csdn.net/postedit/39999433 二.安装Myeclipse 事实上也能够安装eclipse然后再安装Myeclip ...
- 标准I/O缓冲:全缓冲、行缓冲、无缓冲
说明:我仅仅对网络资源进行了整合,方便学习-.- 基于流的操作终于会调用read或者write函数进行I/O操作.为了使程序的执行效率最高,流对象一般会提供缓冲区,以降低调用系统I/O库函数的次数. ...
- 【SICP读书笔记(一)】正则序展开的特殊情况
scheme解释器有两种实现方式,一种是应用序,先对每个参数求值,再以首过程对所有求得的参数求值. 第二种是正则序,会“完全展开然后归约”(书中原文) SICP中的练习1.5,让我困惑了一下.原题如下 ...
- textarea文本字段的宽度和高度(width、height)自己主动适应不断变化的处理
来源:http://www.cnblogs.com/jice/archive/2011/08/07/2130069.html <HTML> <HEAD> <TITLE&g ...
- Codeforces 12D Ball 树形阵列模拟3排序元素
主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
- Android - 支持不同的设备 - 支持不同的平台版本
在最新版本的Android为app提供很好的新API时,也应该继续支持旧版本的Android直到大部分设备已经更新了.这里将要介绍如何在使用最新API带来的优点的同时继续支持老版本. Dashboar ...
- hdu 5074 相邻的和最大dp
http://acm.hdu.edu.cn/showproblem.php?pid=5074 给定一个序列 有些位数未知,给你全部两个数连续所得到的能量.问你怎么安排数字使得总能量最大 二维dp,dp ...
- 安装framework 3.5出现0x800F0922的解决方法
关闭Windows 防火墙,启动Windows model install服务 重启! 再安装 就ok了