Deep understanding of Linux Network Technology Insider--registration of devices in initialization (i)

Source: Internet
Author: User

Subtitle: Principle framework of basic structure related to device registration

Device Registration and deletion time    The device is registered in the following two cases: 1) When the NIC driver is loaded 2) when inserting a hot-swappable device    The NIC here is somewhat different from a hot-swappable device. A. For non-hot-swappable NICs, the registration of the NIC is accompanied by its driver, and the NIC can be built into the kernel or loaded as a module, and if built into the kernel, both the NIC device and initialization occur at boot time, and if the NIC is loaded as a module, the registration and driver initialization of the NIC occurs when the module is loaded. B. For hot-swappable NIC devices, the driver is loaded, so the registration of the device occurs when the device is plugged in and the kernel notifies the associated driver.the cancellation of the device mainly occurs in the following two cases
    1) When the NIC driver is uninstalled (only for devices loaded as modules)
    2) When unplugging a hot-swappable device
Registering the device with the logoff schemadescribes the approximate process of registering and unregistering network devices, respectively (take the PCI Ethernet NIC as an example,other networks are similar, mainly name differences)

The first call to Alloc_etherdev (that is, the Alloc_netdev wrap function), Alloc_etherdev initializes the parameters that are common to all Ethernet devices, and the driver initializes another part of the Net_device. Then complete the registration with Register_netdev.
Net_device allocation Net_devicenet_device Allocated space by Alloc_netdev
Include/linux/netdevice.h#define Alloc_netdev (Sizeof_priv, name, Setup)     Alloc_netdev_mqs (Sizeof_priv, name, Setup, 1, 1)  //net/core/dev.cstruct net_device *alloc_netdev_mqs (int sizeof_priv, const char *name,        Void (* Setup) (struct Net_device *),        unsigned int txqs, unsigned int rxqs)
Alloc_netdev with three parameters: Sizeof_priv: Because the Net_device can be extended by the driver private space, this parameter represents the size of the expanded private space
Name: Device name (part of the name, possibly part of the kernel) Setup: Some parameters for initializing the device
Here the device will be assigned a device name, change the name usually with a serial number to ensure uniqueness: such as Eth0,eth1.
Alloc_netdev usually have some parcel functions to wrap them:
The device (net_device) initialization net_device structure is very large and its members will have different functions initialized in batches. Each function is responsible for a different set of field subsets, respectively. In particular, the following three parts:
Device drivers:
such as IRQ, IO memory, and IO port, the value depends on the hardware configuration, and the device driver is initialized.
Device Type:
common fields for the same device type are initialized by Xxx_setup. (Device type Initial session is part of device driver initialization, Xxx_setup has xxx_probe call)
various functions:
some are performed in Register_device, and some are made when the associated module is notified.
organization of the NET_DEVICE structure    Alloc_netdev The first parameter is known as the size of the private data block, so the private data block is appended with the net_device structure. That is, Net_device can be divided into two parts, part is the data of the Net_device Foundation, the second part is the private data block (such as so). The spatial allocation of private data blocks can be done with the first part of the Net_device data space (such as driver A, B), or it can have a driver assigned itself (such as driver C).

As you can see, the Net_device space allocation is that there will be a block on its area (alignment padding), so dev_base (the table header pointer of the global table) and the next in Net_deivce need to point to the beginning of the structure, Instead of the beginning of the allocated area block (that is, skipping the fill area block).
Net_device is inserted in a global list (beginning with dev_base) and is described later in the two Zhang Hashi tables ( inDev_name_head and Dev_index_head opening).
Dev_base:The global table, which contains all net_device devices, allows the kernel to easily browse all devices.
Dev_name_head:hash table, indexed by device name, is extremely useful when using an older generation Configuration tool IOCTL interface. Dev_index_head:hash table, with Device ID:Dev->ifindex is indexed. With the next-generation Configuration tool IP (from the IPROUTER2 suite), IP tools interact with the kernel through NetLink, which is usually indexed by device ID.
    
Enquiry:
generally byDev_get_by_name and Dev_get_by_index are implemented by the above two hash tables. It is possible to implement the global table Dev_base list based on the device type and MAC address.    all queries are protected by a dev_base_lock lock    All lookup routines is defined innet/core/dev.c.
Device status Net_dev has multiple fields to define the current state of the device:
Flags     //bit fields for various identities, most identities represent the capabilities of the device, but the IFF_UP is special, the identity device is turned on or off, see uapi/linux/if.h (iff_xxx definition), and refer to "turning network devices on and off" below Reg_state Device registration status, see registration status below     for state//and other device-related device status, see "Queue rule status" below

Device Registration Status:
netreg_uninitialized//defined as 0,net_device has been assigned, and the content is clear 0netreg_registering    //net_device has been added to the structure mentioned in "Organization of the Net_device structure", But the kernel still wants to/sys Add Project netreg_registered     //Device registration complete netreg_unregistering  //net_device has been organized by "Net_device structure" Mentioned structure removal netreg_unregistered   //device complete de-listing netreg_released//       all references to the NET_DEVICE structure have been released

Queue Rule Status:
_ _link_state_start      //Device Open _ _link_state_present    //device exists _ _link_state_nocarrier  //No Carrier _ _link_state_ Linkwatch_event  //Device status Changed _ _link_state_xoff             //The following three identities are used by the equipment responsible for managing traffic ingress and egress _link_state_shed             //_ _link_ state_rx_sched         //
Virtual Devices:  There are several differences between virtual devices and real devices:virtual devices occasionally callRegister_netdevice and Unregister_netdevice, not their wrapping functions. and lock and unlock yourself to get more time to hold the lock.   The real device cannot be removed from the user's command and will be removed at the time of uninstallation. Virtual devices can be de-removed through user commands, but whether delisting succeeds depends on the virtual device-driven design.
Virtual Appliance This article does not introduce much, and then add a separate blog post for the difference between the virtual device and the real device .
Locked:Net_devices includes a number of locking means, later added ...

    
    






















Deep understanding of Linux Network Technology Insider--registration of devices in initialization (i)

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.