Linux1.0 TCP/IP初始化

一切都要从函数start_kernel说起:

asmlinkage void start_kernel(void)
{
    ...
    sock_init();
    ...
}

函数start_kernel定义在init/main.c中,是系统启动过程中运行的第一个C函数,由head.s中的startup_32直接调用。

void sock_init(void)
{
	struct socket *sock;
	int i;
	/* Set up our SOCKET VFS major device */
	if (register_chrdev(SOCKET_MAJOR, "socket", &net_fops) < 0)
	{
		return;
	}
	/* Release all sockets */
	for (sock = sockets; sock<= last_socket; ++sock)
	{
		sock->state = SS_FREE;
	}
	/* Initialize all address(protocol) familied */
	for (i = 0; i < NRPOTO; ++i)
	{
		pops[i] = NULL;
	}
	/* Initialize the DDI module */
	ddi_init();	
}
其中, socketspops的定义在 /net/socket.c如下:
#define NSOCKETS 	128 	/* should be dynamic, later... */
#define NPROTO 		16 	/* should be enought for now   */
static struct socket 	sockets[NSOCKETS];
static struct proto_ops *pops[NPROTO];
结构体 socketproto_ops的定义在 /include/linux/net.h文件中。
void ddi_init(void)
{
	struct ddi_proto 	*pro;
	struct ddi_device 	*dev;
	/* First off, kick all configured protocols */
	pro = protocols;
	while (pro->name != NULL)
	{
		(*pro->init)(pro); pro++;
	}
	/* Done. Now kick all configured device drivers */
	dev = devices;
	while (dev->title != NULL)
	{
		(*dev->init)(dev); dev++;
	}
	/* We're all done... */
}
函数 ddi_init做了两件事:初始化所有(定义)的 protocolsdevices
struct ddi_proto portocols[] = {
	{"UNIX", unix_proto_init},
	{"INET", inet_proto_init}
};
其中 protocols中(暂时只)定义了如上的两类。
void inet_proto_init(struct ddi_proto *pro)
{
	struct inet_protocol *p;
	int i;
	/* Set up our UNIX VFS major device */
	if (register_chrdev(AF_INET_MAJOR, "af_inet", &inet_fops) < 0) 	return;
	/* Tell SOCKET that we are alive... */
	(void) socket_register(inet_proto_ops.family, &inet_proto_ops);
	seq_offset = CURRENT_TIME * 250;
	/* Add all the protocols */
	for (i = 0; i < SOCK_ARRAY_SIZE; i++)
	{
		tcp_prot.sock_array[i] = NULL;
		udp_prot.sock_array[i] = NULL;
		raw_prot.sock_array[i] = NULL;
	}
	for (p = inet_protocol_base; p != NULL; )
	{
		struct inet_protocol *tmp;
		tmp = (struct inet_protocol *) p->next;
		p = tmp;
	}
	dev_init();
	bh_base[INET_BH].routine = inet_bh;
}



函数sock_register完成了proto_ops的设定:
/* This function is called by a protocol handler that wants to
 * advertise its address family, and have it linked into the
 * SOCKET module */
int sock_register(int family, struct proto_ops *ops)
{
	int i;
	cli(/* clear interrupt-enable flag */);
	for (i = 0; i < NPROTO; i++)
	{
		if (pops[i] != NULL) 	continue;
		pops[i] = ops;
		pops[i]->family = family;
		sti(/* set interrupt-enable flag */);
		return(i);
	}
	sti(/* set interrupt-enable flag */);
	return(-ENOMEM);
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值