Wu.Country@侠缘

勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!

导航

[译]5.7. Initializing the Device Handling Layer: net_dev_init 设备处理层的初始化:net_dev_init

目录:http://www.cnblogs.com/WuCountry/archive/2008/11/15/1333960.html
 
[不提供插图,读者最好从网上下载源书]

5.7. Initializing the Device Handling Layer: net_dev_init
设备处理层的初始化:net_dev_init

An important part of initialization for the networking code, including Traffic Control and per-CPU ingress queues, is performed at boot time by net_dev_init, defined in net/core/dev.c:
网络代码初始化的一个很重要的部份,包括流量控制和每个CPU的入队列,都是在启动时候,由net_dev_init来完成的,它们在net/core/dev.c中定义:

static int _ _init net_dev_init(void)
{
    ...
}
subsys_initcall(net_dev_init);


See Chapter 7 for how the subsys_initcall macros ensure that net_dev_init runs before any NIC device drivers register themselves, and why this is important. You also will see why net_dev_init is tagged with the _ _init macro.
参见第7章,subsys_initcall这个宏确保net_dev_init在NIC设备驱动注册他们自己之前运行,以及为什么这个很重要。你同样可以看到,为什么net_dev_init要用__init这个宏标记。

Let's walk through the main parts of net_dev_init:
让我们看看net_dev_init的主要部份:

The per-CPU data structures used by the two networking software interrupts (softirqs) are initialized. In Chapter 9, we will see what a softirq is and go into detail on how the networking code uses softirqs.
每个CPU的数据结构是由两个网络软件中断(软中断)初始化所使用。在第9章中,我们会看到什么是软中断以及网络代码是如何使用它们详细情况。

When the kernel is compiled with support for the /proc filesystem (which is the default configuration), a few files are added to /proc with dev_proc_init and dev_mcast_init. See the later section "Tuning via /proc Filesystem" for more details.
当内核编译成支持/proc文件系统时(这是一个默认配置),会有一些文件和with_proc_init一起添加到/proc中。详细情况参见后面的“通过/proc文件系统调整(内核)”章节。

netdev_sysfs_init registers the net class with sysfs. This creates the directory /sys/class/net, under which you will find a subdirectory for each registered network device. These directories include lots of files, some of which used to be in /proc.
netdev_sysfs_init通过sysfs注册网络类。这会创建一个/sys/class/net/的目录,在这个目录下,你可以发现与每一个注册的网络设备的子目录。这个目录包括话多文件,其中有一些就/proc所使用。

net_random_init initializes a per-CPU vector of seeds that will be used when generating random numbers with the net_random routine. net_random is used in different contexts, described later in this section.
net_random_init初始化每个CPU的向量种子数,这个数会通过net_radnom例程一起用于生成随机数。net_random在不同的上下文中使用,在这一节的后面会说明。

The protocol-independent destination cache (DST), described in Chapter 33, is initialized with dst_init.
与协议相关的目的缓存(DST)是通过dst_init初始的,这个会在第33章中说明。

The protocol handler vector ptype_base, used to demultiplex ingress traffic, is initialized. See Chapter 13 for more details.
处理协议的向量ptype_base也被初始化,它用于多功能的入队流量管理,详细的参见第13章。

When the OFFLINE_SAMPLE symbol is defined, the kernel sets up a function to run at regular intervals to collect statistics about the devices' queue lengths. In this case, net_dev_init needs to create the timer that runs the function regularly. See the section "Average Queue Length and Congestion-Level Computation" in Chapter 10.
当OFFLINE_SAMPLE符号被定义时,内核设置了一个函数在规则的间隙运行,用于收集与设备队列长度相关的统计。在这种情况下,net_dev_init需要创建一个计时器,用于规则的运行这个函数。参见第10章的“平均队列长度和拥塞等级运算”。

A callback handler is registered with the notification chain that issues notifications about CPU hotplug events. The callback used is dev_cpu_callback. Currently, the only event processed is the halting of a CPU. When this notification is received, the buffers in the CPU's ingress queue are dequeued and are passed to netif_rx. See Chapter 9 for more detail on per-CPU ingress queues.
一个回调句柄被注册到通知链中,这样可以确保CPU会通知热插事件。这个回调使用的是dev_cpu_callback。目前,唯一的事件处理就是暂停CPU。当收到这个通知时,CPU里的入队列缓存队列就被清出,然后传给netif_rx。详细的参见第9章的每CPU入队列。

Random number generation is a support function that the kernel performs to help randomize some of its own activity. You will see in this book that many networking subsystems use randomly generated values. For instance, they often add a random component to the delay of timers, making it less likely for timers to run simultaneously and load down the CPU with background processing. Randomization can also defend against a Denial of Service (DoS) attack by someone who tries to guess the organization of certain data structures.
随机数生成器是一个辅助功能,可以让内核帮助它们实现一些随机化的活动。你会从本书中看到,有很多网络子系统使用了随机生成数。例如,他们经常添加一个随机的组件到计时器中,使它不至于让计时器同时地运行,并在后台进程中分担CPU。随机还可以防止一些人使用DOS攻击来猜测组织结构和数据结构。

The degree to which the kernel's numbers can be considered truly random is called system entropy . It is improved through contributions by kernel components whose activity has a nondeterministic aspect, and networking often falls in this category. Currently, only a few NIC device drivers contribute to system entropy (see earlier discussion on SA_SAMPLE_RANDOM). A patch for kernel 2.4 adds a compile time option that you can use to enable or disable the contribution to system entropy by NICs. Search the Web using the keyword "SA_SAMPLE_NET_RANDOM," and you will find the current version.
可以被内核数所真实信任的随机数的度,被称为系统熵(译:看原文,不知道什么意思)。这可以增加内核中那些行为有不确定特性的组件的贡献,并且一些网络经常落到这一话题中。目前,只有少数的NIC设备驱动给内核贡献了熵(参见前面所讨论的SA_SAMPLE_RANDOM)。有一个给2.4的补丁,添加了编译选项。你可以通过这些选项打开或者关闭,是否让NIC设备给内核贡献熵。从网络上搜索“SA_SAMPLE_NET_RANDOM”,你会发现最新的版本内容。

5.7.1. Legacy Code 遗传代码
I mentioned in the previous section that the subsys_initcall macros ensure that net_dev_init is executed before any device driver has a chance to register its devices. Before the introduction of this mechanism, the order of execution used to be enforced differently, using the old-fashioned mechanism of a one-time flag.
我在前面的章节中已经提到过,subsys_initcall宏用于确保net_dev_init在任何设备注册它的驱动之间被调用。在介绍这个机制之前,执行的顺序通常是强制要求不同的,在老的形式中是使用一个时间标志。

The global variable dev_boot_phase was used as a Boolean flag to remember whether net_dev_init had to be executed. It was initialized to 1 (i.e., net_dev_init had not been executed yet) and was cleared by net_dev_init. Each time register_netdevice was invoked by a device driver, it checked the value of dev_boot_phase and executed net_dev_init if the flag was set, indicating the function had not yet been executed.
全局变量dev_boot_phase当成一个逻辑标志位,用于记住net_dev_init是否已经被执行过。它在初始化时设置为1(就是说net_dev_init还没有被执行),并由net_dev_init清零。每次register_netdevice被设备驱动调用时,它就检测dev_boot_phase这个值,关在标志被设置的时候执行net_dev_init,即指示这个函数还没有被执行过。

This mechanism is not needed anymore, because register_netdevice cannot be called before net_dev_init if the correct tagging is applied to key device drivers' routines, as described in Chapter 7. However, to detect wrong tagging or buggy code, net_dev_init still clears the value of dev_boot_phase, and register_netdevice uses the macro BUG_ON to make sure it is never called when dev_boot_phase is set.[*]
这种机制现在并不需要了,因为当正确的标签被应用到设备驱动程序的关键字上时,register_netdevice不能在net_dev_init之前被调用,这会在第7章中讨论。然而,为了检测错误的标签,或者是代码bug,net_dev_init还是会清除dev_boot_phase的值,并register_netdevice用宏BUG_ON来确保当dev_boot_phase被设置时,这决对不会调用。

[*] The use of the macros BUG_ON and BUG_TRAP is a common mechanism to make sure necessary conditions are met at specific code points, and is useful when transitioning from one design to another.
BUG_ON和BUG_TRAP宏的使用是一种常用的机制,用于在一些特殊的代码点上,保证一些必须的条件,而且在从一个设计人转到另一个人那里的时候特别有用。

 

posted on 2009-02-20 21:01  Wu.Country@侠缘  阅读(844)  评论(0编辑  收藏  举报