Linux内核网络协议栈笔记2:初始化
参考文献《Understanding Linux Network Internals》中用了整整一章(part II)来介绍system initialization。本文只提供一个简单的概述,如果需要详细信息,还请看参考文献。 我们这里所说的初始化过程指的是从硬件加电启动,到可以从网络接收或发送数据包之前的过程。在Linux系统中,网卡拥有双重身份:struct pci_dev和struct net_device。pci_dev对象代表通用硬件的性质,是作为一个标准的PCI的设备插入了PCI的卡槽,由驱动程序进行管理;另一方面,net_device对象代表网络传输的性质,与内核的网络协议栈交互,进行数据传输。因此我们也必须通过两个方面来进行初始化,但是后者是我们的重点。而且我们并不关心内核与硬件的交互细节,例如寄存器读写与I/O映射等。 内核在初始化时,也会初始化一些与网络相关的数据结构;而且对应我们前面的日志所提及的内核网络协议栈层次结构(点这里),内核也需要一定的初始化工作来建立这种层次结构。 笔者认
void __init sock_init(void
)
{ //Initialize sock SLAB cache. sk_init(); //Initialize skbuff SLAB cache skb_init(); //Initialize the protocols module. init_inodecache(); register_filesystem(& sock_fs_type); sock_mnt = kern_mount(& sock_fs_type); //The real protocol initialization is performed when do_initcalls is run. netfilter_init(); } 为初始化最重要的就是重要数据结构(通常用粗体标注)。因此也希望读者能够记住重要的数据结构的作用。 下面我们将分层,自底向上的分析整个初始化过程: (一)驱动程序层 本文中以一个realtek 8139系列网卡作为例子,因为其驱动只有一个c文件(/drivers/net/8139too.c),比较容易分析。读者也可以参考e1000网卡的另一篇文章(点这里)。内核版本基于2.6.11。 驱动程序加载/注册主要包括以下的步骤: (a)将设备驱动程序(pci_driver)添加到内核驱动程序链表中; (b)调用每个驱动中的probe函数(其中重要一步就是初始化net_device对象)。 下面进行详细分解。 通常,在Linux中使用insmod命令加载一个驱动程序模块,例如8139too.o目标文件。加载之后,Linux会默认执行模块中的module_init(rtl8139_init_module)宏函数,其中的参数rtl8139_init_module是一个函数指针,指向在具体的驱动程序8139too.o中声明的rtl8139_init_module函数。这个函数定义如下:
static int __init rtl8139_init_module (void
)
{ return pci_module_init (&rtl8139_pci_driver); } pci_module_init是一个宏定义,实际上就等于pci_register_driver函数。(在2.6.30内核版本中,直接变成了return pci_register_driver(&rtl8139_pci_driver) )。pci_register_driver函数的注释说明了它的作用:register a new pci driver.Adds the driver structure to the list of registered drivers。也就是把如下的这样一个驱动程序(pci_driver类型)挂到系统的驱动程序链表中:
static struct pci_driver rtl8139_pci_driver =
{
.name = DRV_NAME, .id_table = rtl8139_pci_tbl, .probe = rtl8139_init_on .remove = __devexit_p(rtl8139_remove_on #ifdef CONFIG_PM .suspend = rtl8139_suspend, .resume = rtl8139_resume, #endif /* CONFIG_PM */ }; 这一步我们应该这样理解(熟悉面向对象编程的读者):所有的pci_driver应该提供一致的接口(比如remove卸载/suspend挂起);但是这些接口的每个具体实现是不同的(pci声卡和pci显卡的挂起应该是不同的),所以采用了这样的函数指针结构。这个pci_driver结构其中最重要的就是probe函数指针,指向rtl8139_init_on
int driver_register(struct device_driver *
drv)
{ INIT_LIST_HEAD(&drv-> devices); init_MUTEX_LOCKED(&drv-> unload_sem); return bus_add_driver(drv); } 前两个就是实现了添加到链表的功能,bus_add_driver才是主要的函数(/drivers/base/bus.c中),内部又调用了driver_attach函数,这个函数的主体是一个list_for_each循环,对链表中的每一个成员调用driver_probe_device函数(哈哈,出现了probe!),这个函数就调用了drv->probe(dev)(drv就是pci_driver类型的对象)!这样也就调用了驱动程序中的probe函数指针,也就是调用了rtl8139_init_on 函数rtl8139_init_on
static int __devinit rtl8139_init_on
{ struct net_device *dev = NULL; rtl8139_init_board (pdev, & dev); /* The Rtl8139-specific entries in the device structure. */ dev ->open = rtl8139_open; dev->hard_start_xmit = rtl8139_start_xmit; dev->poll = rtl8139_poll; dev->stop = rtl8139_close; dev->do_ioctl = netdev_ioctl; } 整个的调用链如下:pci_register_driver ==> driver_register ==> bus_add_driver ==> driver_attach ==> driver_probe_device ==> drv->probe ==> rtl8139_init_on 一个简单的net_device生命周期示意图如下(左边为初始化,右边为卸载):
这个net_device数据结构的生成,标志着网络硬件和驱动程序层初始化完毕。也意味着,网络协议栈与硬件之间的纽带已经建立起来。
。 (二)设备无关层/网络协议层/协议无关接口socket层 Linux内核在启动后所执行的一些内核函数如下图所示: 系统初始化的过程中会调用do_basic_setup函数进行一些初始化操作。其中2.6.11内核中就直接包括了driver_init()驱动程序初始化,以及sock_init函数初始化socket层。然后do_initcalls()函数调用一组前缀为__init类型(这个宏就表示为需要在系统初始化时执行)的函数。与网络相关的以__init宏标记的函数有:net_dev_init初始化设备无关层;inet_init初始化网络协议层。 (fs_initcall和module_init这两个宏也具有类似的作用。由于这一阶段处于系统初始化,宏定义比较多,欲详细了解各种宏的使用的读者请参阅参考文献《Understanding Linux Network Internals》Part II Chapter 7) 我们下面详细介绍一下这三个初始化函数都进行了哪些工作。 (a)net_dev_init(在文件/net/core/dev.c中):设备操作层
static int __init net_dev_init(void
)
{ if (dev_proc_init()) if (netdev_sysfs_init()) INIT_LIST_HEAD(& ptype_all); for (i = 0; i < 16; i++ ) INIT_LIST_HEAD(& ptype_base[i]); for (i = 0; i < ARRAY_SIZE(dev_name_head); i++ ) INIT_HLIST_HEAD(& dev_name_head[i]); for (i = 0; i < ARRAY_SIZE(dev_index_head); i++ ) INIT_HLIST_HEAD(& dev_index_head[i]); //Initialise the packet receive queues. for (i = 0; i < NR_CPUS; i++ ) { struct softnet_da queue = & per_cpu(softnet_da skb_queue_head_init(&queue-> input_pkt_queue); queue->throttle = 0 ; queue->cng_level = 0 ; queue->avg_blog = 10; /* arbitrary non-zero */ queue ->completion_queue = NULL; INIT_LIST_HEAD(&queue-> poll_list); set_bit(__LINK_STATE_START, &queue-> backlog_dev.state); queue->backlog_dev.weight = weight_p; queue->backlog_dev.poll = process_backlog; atomic_set(&queue->backlog_dev.refcnt, 1 ); } open_softirq(NET_TX_SOFTIRQ, net_tx_act open_softirq(NET_RX_SOFTIRQ, net_rx_act } 这个函数所做的具体工作主要包括:初始化softnet_da (b)inet_init(在文件/net/ipv4/af_inet.c中):网络层 由于各种网络协议是按照协议族(protocol family,PF或者address family,AF)为单位组织起来的。我们在这里仅以Internet协议族(AF_INET或者PF_INET,在内核中这二者是等价的)为例。 有时候这一层又被称为INET socket层(对应的数据结构为struct sock),请注意与BSD socket层区别(对应数据结构为struct socket):BSD socket层提供一组统一的接口,与协议无关;但具体到网络层就必须与协议相关了,因此操作也会有一些不同。 代码如下(有删节):
static int __init inet_init(void
)
{ struct sk_buff * dummy_skb; struct inet_protosw * q; struct list_head * r; rc = sk_alloc_slab(&tcp_prot, "tcp_sock" ); rc = sk_alloc_slab(&udp_prot, "udp_sock" ); rc = sk_alloc_slab(&raw_prot, "raw_sock" ); //Tell SOCKET that we are alive (void)sock_register(& inet_family_ops); //Add all the base protocols. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0 ); if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0 ); /* Register the socket-side information for inet_create. */ for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++ r) INIT_LIST_HEAD(r); for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++ q) inet_register_protosw(q); //Set the ARP module up arp_init(); //Set the IP module up ip_init(); tcp_v4_init(& inet_family_ops); /* Setup TCP slab cache for open requests. */ tcp_init(); //dev_add_pack(&ip_packet_type); } module_init(inet_init); 这个函数中包括的主要函数包括: sock_register:在以前的文章中说过,Linux内核网络协议栈采用了分层结构,所以层与层之间肯定是松耦合的。上层的socket层并不知道下面的网络协议层都具体包括哪些协议。因此本函数有两个作用:(a)周知INET协议族;(b)注册到socket模块上。 inet_add_protocol:在协议族中添加具体的协议。 inet_register_protosw:各种inet协议族中的协议在内核中是保存在inetsw_array[]数组,其中元素为inet_protosw类型(/include/net/protocol.h文件中),每一个元素对应一个协议。每一个协议又有两个数据结构:struct proto/struct proto_ops。这两个结构中都是一些函数操作,但proto表示每个协议独特的操作,而proto_ops是通用的socket操作(包含在struct socket中);这种区别就类似于INET socket和BSD socket的区别。 (c)sock_init(在文件/net/socket.c中):BSD socket层 定义如下(代码有删节): 此函数主要执行的工作是:初始化sock和sk_buff数据结构(这些重要的数据结构在后面的文章中还会涉及);由于sock属于linux文件系统的一部分,因此要注册成为文件系统;以及如果使用netfilter,也要进行初始化。 |