Deep understanding of Linux Network Technology Insider--Network device initialization

Source: Internet
Author: User

 OverviewDuring the initialization of the kernel, the network-related work is as follows:
Kernel boot time executionStart_kernel, Start_kernel will call Rest_init before the end of theRest_init Initializationkernel thread init (kernel_init in linux3-12).
asmlinkage void __init Start_kernel (void) {    ...    Parse_early_param ();//Indirect call Parse_args    Parse_args (...);//Handling Kernel boot program (boot loader) parameters passed to the kernel during boot    ...    INIT_IRQ ();   Initialize hardware interrupt    tick_init ();    Init_timers (); Timers are used to support the operation of subsequent initialization work.    hrtimers_init ();    Softirq_init (); Initializing software interrupts ...    Rest_init ();   Kernel thread init (kernel_init) is called through the Kernel_thread function.

Static init __ref kernel_init (void *unused) {    kernel_init_freeable ();   Linux-3.12 here calls Do_basic_setup    Free_initmem ();  Used to release memory that is no longer needed.    .....    Run_init_process (Execute_command) ...    }

static void __init Do_basic_setup (void) {    cpuset_init_smp ();    Usermodehelper_init ();    Shmem_init ();    Driver_init ();    Init_irq_proc ();    Do_ctors ();    Usermodehelper_enable ();    Do_initcalls ();  Initialize the kernel subsystem and the built-in device driver     random_int_secret_init ();}



registration and initialization of the deviceto be able to work properly, a device must be recognized by the kernel and associated with the correct driver. The device driver saves all the information required to drive the device in the form of a private structure, and interacts with other components that interact with the device.
part of the registration and initialization of the device is done by the kernel, partially by the device driver.

  the initialization of the device includes hardware initialization, software initialization, function initialization, and three parts: hardware initialization: It is done by the device driver and the common bus layer, and sometimes the user is required to provide some parameters. The primary task is to configure the hardware functions as IRQ and I/O addresses so that they can interact with the kernel. Software initialization:before using the device, you must focus on the current configuration or enabled network protocols, which generally require users to provide parameters such as IP address. function Initialization:The Linux kernel provides a range of networking options, and some network options need to be configured separately for each device (such as subsystems that implement QoS), which determine how packets enter the queue and exit queues from the device.     NIC initialization target    Network devices are initialized with Net_device instances in Linux, and this section does not discuss this first,This section focuses on how the device driver allocates/establishes the resources required for the device to communicate with the kernel. IRQ Line:The Nic must dispatch an IRQ to evoke the kernel's attention if necessary (the virtual appliance does not need to assign an IRQ because its work is implemented internally.) )The /proc/interrupts file can be used to observe the current break dispatch status. I/O port and Memory registration:The driver maps a piece of memory from the device to the system memory, allowing the driver to read and write operations directly through the system memory. The registration and release operations wererequest_region and Release_region.
Interaction between the device and the kernel almost all devices interact with the kernel in the following two ways: Polling for the kernel:the kernel periodically checks the status of the device to determine if the device has any requests.
device-driven interrupt Request:device drivers send hardware signals that cause kernel attention

kernel Polling is described in other articles, and this article focuses on network-related concepts in hardware interrupts. Hardware Interruptseach of these interrupts will run an interrupt handler, which is device-driven for device-specific applications. Generally, when a device registers a NIC, it first requests and assigns an IRQ, and then registers for the IRQ (if the device is uninstalled, it needs to unregister) an IRQ responder. The corresponding kernel code isKernel/irq/manage.c and ARCH/XXX/KERNEL/IRQ.C. (where xxx is the processor architecture)
int REQUEST_THREADED_IRQ (unsigned int IRQ, irq_handler_t handler,                                                         irq_handler_t thread_fn, unsigned long irqflags,< C1/>const Char *devname, void *dev_id) void Free_irq (unsigned int irq, void *dev_id)

Note: Both the registration and deallocation functions of the IRQ have parameter dev_id. Because IRQ is shareable, IRQ number and dev_id are required to collectively represent interrupts.
Also , when registering an IRQ, you must ensure that the IRQ has not yet been requested by the device, unless all devices support IRQ sharing.
when the kernel receives an interrupt signal, the associated interrupt responder is called through the IRQ number. The IRQ number is saved as a table with the interrupt responder. Because multiple devices may share an IRQ relationship, IRQ number may have a one-to-many relationship with the interrupt responder.      Interrupt Type:receive data frames,Frame transfer failed,The DMA transfer was completed successfully,the device already has enough memory to create a new transport session (available NIC available memory reaches a certain value < typically generates an interrupt when the device mtu>)to prevent the kernel from committing a transfer request multiple times when the device is low on memory, the device driver can shut down the kernel egress queue until the resource is sufficient to restart. Here's an example:
Static netdev_tx_tel3_start_xmit (struct Sk_buff *skb, struct net_device *dev) {    ...    Netif_stop_queue (dev);    ... Dev->trans_start = jiffies;    if (INW (ioaddr + tx_free) > 1536)        netif_start_queue (dev);    else/        * Interrupt us when the FIFO has a guest for max-sized packet. *        /OUTW (Settxthreshold + 1536, ioaddr + el3_cmd) ;    ......}



     IRQ Sharing: IRQ lines are a very limited resource, and to allow a system to support more devices, only multiple devices can share IRQ lines. The mechanism for IRQ sharing is that the kernel receives an interrupt request and then invokes all the response routines associated with the interrupt, and then each response routine is self-determining whether the interrupt is processed by the filter. (Note that the IRQ and the responder are one-to-many, an IRQ occurs, which responders to handle, which do not need to be judged by the kernel, but the individual interrupt responder itself, the kernel calls all the responder.) ) the organization of IRQ and IRQ responders : Using a global vector:Irq_desc to organize, Irq_desc contains all the IRQ, each IRQ corresponds to its own linked list, and the list is all the responders associated with that IRQ. The IRQ linked list has more than one node when only the IRQ is shared. The entire organization such as:



Initialization optionsall system-built components, as well as devices loaded as modules, can adjust the functionality implemented by the user's input parameters, override their default values, or have different values before and after the boot.
Module Options: (Macros for Module_param series)The module can be defined when it is loaded. In the case of built-in components, the runtime configuration can be done through sys/because it is not configurable during boot. kernel options during boot: (__SETUP series macros)provided during boot. For modules that can be built into the kernel.
    
Initialization of the device processing layerNetwork Code initialization has the following important parts: flow control, initialization of each CPU input queue. These initialization work is performed during boot time by theNet_dev_init Complete:
static int __init net_dev_init (void) subsys_initcall (net_dev_init);   


User Space Helper Program
    • /sbin/modprobe called when the kernel needs to load a module, judging if the module passed by the kernel is not an alias defined in the/etc/modprobe.conf file.
    • /sbin/hotplug is called when the kernel detects that a new device is plugged in or unplugged from the system, and its task is to load the correct driver based on the device identity

    1. load the
      in a modular manner The Kmod module loader allows kernel components to load a module by invoking a Request_module request
      For example, if the system administrator uses Ifconfig to configure a network adapter, but the NIC driver is not loaded, such as eth0, the kernel will give/sbin/ Modprobe sends a request to load a module with the name
      Eth0. If the/etc/modprobe.conf contains "alias eth0 xxx" characters,/sbin/modprobe will attempt to load the Xxx.ko module. The
      Module_param macro defines that you can access the module parameters by file after the introduction of SYSFS: The module option has three items, the first parameter name, the second parameter type, and the third item indicates that the parameter is all the permissions of the file in the SYS file system.
      Each module will generate the corresponding directory under Sys/modules, and the module parameters can be obtained through the files in the directory.
    2. pnp hot swap
      HotPlug allows the kernel to detect the insertion and removal of hot-swappable devices and notify user processes (/sbin/ HotPlug), the user process loads the corresponding driver according to these notifications
      when compiling the kernel, Modules.pcimap and modules.usbmap two files are generated in the kernel directory, each containing the PCI of the kernel supported devices ID and USB ID, the file also contains the name of the kernel module that corresponds to the ID of each device, and when the user process receives a notification from the kernel about PNP, the file is used to find the correct device driver

Virtual Appliancesvirtual appliances are also typically instantiated using the Net_device struct (there are some exceptions, such as Alias Interface Devices).
The virtual appliance typically has a user space configuration tool to configure it. In particular, advanced fields that cannot be configured by using Ifconfig.
a virtual appliance typically has a/proc interface directory, and its content verbosity depends on the design of the virtual appliance.
The virtual device corresponds to a corresponding relationship that is not one by one for the device. This leads to the configuration of the virtual appliance that may require traffic control.
the traffic for a virtual device is introduced from a real physical device, so there is no need to allocate IRQ, IO ports, and IO memory.
virtual devices, like other real-world physical devices, can respond to special event notifications.

/proc adjustment    












Deep understanding of Linux Network Technology Insider--Network device initialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.