Deep understanding of Linux Network Technology Insider--registration and initialization of equipment (II.)

Source: Internet
Author: User

Device registration for removal of equipmentequipment registration and removal of equipment is generallyRegister_netdev andUnregister_netdev completed. These two are wrapped functions that are responsible for locking, and what really works is its invocation of theRegister_netdevice and Unregister_netdevice. See also:Net/core/dev.c.describes some of the state changes in the device registration process



The state change will use the state registering between uninitialized and registered. These processes are netdev_run_todo. Refer to "Cutting operation: Netdev_run_todo"
when registering and de-listing a device, the device driver can initialize and clean up private data using the Net_device two virtual functions init and uninit.
when the device is removed, only the reference count of Net_device is 0,Netdev_wait_allrefs will return, the delisting will be completed smoothly.
The registration and de-listing of the equipment is done by Netdev_run_todo. cutting operation: Netdev_run_todoRegister_netdevice will be responsible for part of the registration work, and then letNetdev_run_todo to be completed.
the change of Net_device will passRtnl_lock andRtnl_unlock receivedRouting netlink Signal Volume protection. That's whyThe reason that the Register_netdev needs to request a lock when executing and to release the lock on return.once Register_netdevice has completed its own work, it will add the net_device structure to net_todo_list through Net_set_todo. The net_todo_list contains a list of devices that need to be completed for a registered (or de-listing) operation. This list hasthe Register_netdev is disposed of during the release lock time. soRtnl_unlock will not only release the lock, it will also call Netdev_run_todo. Netdev_run_todo will browse the Net_todo_list list and complete all instance registrations.

void Rtnl_unlock (void) {/    * This fellow would unlock it for us. *    /Netdev_run_todo ();                                                          } Export_symbol (Rtnl_unlock);
    The work done by Netdev_run_todo does not require holding a lock.


Device registration Status Notificationkernel components and applications may want to know the device's registration, delisting, opening, and shutting down. Device loading information transmits notifications in two ways. netdeV_chainRtmgrp_link Multicast group for NetLink
netdev_chain notification Chain:
The registration and de-listing stages of the equipment change in each state is through the netdev_chain to advertise. The kernel components that are of interest to it can be individuallyRegister_netdevice_notifier andunregister_netdevice_notifier to register and de-list the chain. the events reported by Netdev_chain are defined in the following:
Format: #define NETDEV_UP   0x0001  /* For now you can ' t veto a device Up/down */#define NETDEV_DOWN 0x0002 ...//meaning: N ETDEV_UP        //The report indicates that the device is enabled, generated by the Dev_open report Netdev_down      //device has been shut down, report Netdev_reboot//    due to hardware device, Device has restarted Netdev_change    //device status or configuration changed Netdev_register        //device has been registered Netdev_unregister      //device has been removed netdev_ CHANGEMTU       //netde   //netdev_pre_up               //netdev_pre_type_change      //netdev_post_type_change     // Netdev_post_init            //netdev_unregister_final     //netdev_release              //netdev_notify_peers         //NETDEV _join                 //netdev_changeupper  v_changeaddr      //Device hardware address (or associated broadcast address) has changed Netdev_going_down      //netdev_ ChangeName      //device name changed Netdev_feat_change     //netdev_bonding_failover          //netdev_resend_igmp          //


Many devices are registered in the Netdev_chain, such as: routing, firewall rtnetlink Link Notificationwhen a device has some state changes (or other events), itRtmsg_ifinfo Pass the notification to the link multicast group.
Device Registration:device registration is more than just embedding net_device into global table dev_base and hash tablesDev_name_head,Dev_index_head, it also includes initializing the net_device part of the parameters, sending a broadcast notification (reminding other modules to join the device), and some other work.
int Register_netdev (struct net_device *dev) {    int err;      Rtnl_lock ();    Err = Register_netdevice (dev);                                                                                           Rtnl_unlock ();    return err; }
Register_netdevice starts the device registration work and calls Net_set_todo, and Net_set_todo eventually calls Netdev_run_todo to complete the registration.
    Register_netdevice's work mainly includes the following sections:
  • Initialize partial fields of Net_device
  • if the kernel supports divert function, the data space blocks required for this function are allocated with ALLOC_DIVERT_BLK and connected to the Dev->divert
  • If the device driver has already Dev->init is initialized, this function is executed.
  • by Dev_new_index assigned to the device an identification code.
  • Insert Net_device into global table dev_base, and two hash table Dev_name_head,dev_index_head.
  • Check that the feature ID has invalid combinations.
  • Set the __link_state_present identity in the dev->state so that the device can be used by the kernel.
  • with Dev_init_scheduler initializes the device queue rules so that traffic control is used to implement QoS.
  • through The netdev_chain notification bracelet informs all subsystems interested in registering this device.
when Netdev_run_todo is called to complete the registration, it only updatesDev->reg_state and enroll the device into SYSFS. Removal of equipment:removal of equipment requires recovery of all work done during the registration of the device and a number of other matters:
    • with Dev_close shutting down the device
    • Frees all allocated resources (IRQ, I/O ports ...). )
    • Removing devices from the global table dev_base and the two Zhang Hashi tables
    • When all references to the device are freed, the space for the Net_device structure is freed, the driver's private data structure, and the linked memory area block.
    • Delete all files added to/proc and/sys
three function pointers in Net_device are useful:      
Dev->stop       //used to shut down the device, usually including closing the netif_stop_queue exit, freeing the hardware resources, etc. dev->uninit     //mainly responsible for reference counting, less use to Dev->destructor/ /few virtual devices used, typically initialized to: Free_netdev or its wrapping function

The removal of the device has an int unregister_netdevice (struct net_device *dev), whose main work is as follows:
    •     If the device is not turned off, use Dev_close to close
    • Span style= "FONT-SIZE:10.5PT; line-height:1.5 ">    Remove device
    •     All the queue rule instances associated with the device, destroyed by Dev_shutdown
    •     send netdev_ Unregister message to Netdev_chain notification chain.
    •     delisting messages must also inform user space.
    •     any data blocks linked to Net_device are freed
    •      register_netdevice all operations in Dev_init must be dev_uninit restored.
finally call Net_set_todo, so that Net_run_todo can complete the delisting.
Reference Countthe Net_device structure can be freed only when the device reference count is released.
    the reference count is saved in theThe dev->refcnt. Use Dev_hold and dev_put to update reference counts when references are added or removed.
when the devicewhen Register_netdevice is registered, the dev->refcnt is initialized to 1, which is the first reference to be held by the kernel code responsible for the network device database. Therefore, this value can be reduced to 0 only if the device is de-removed.
    
enabling and shutting down a network deviceonce the device is registered, the device is still unable to receive and transmit the data stream without explicitly turning on its functionality.
the device's open request is completed by Dev_open:
Net/core/dev.cint dev_open (struct net_device *dev) static int __dev_open (struct net_device *dev)
enabling devices includes the following tasks:
    • if Dev->open is initialized, it calls Dev->open.
    • Set the __link_state_start identity in the dev_state to indicate that the device is turned on or running
    • Set the iff_up in Dev_flags to identify the device as turned on.
    • called Dev_activate initializes the egress queue rules that are required for traffic control, and then enables the watchdog timer.
    • Send NETDEV_UP notification to Netdev_chain
the device's enablement must show execution, but the device's shutdown can be performed explicitly by a user command or implicitly through another program. device shutdown has the following tasks:
  • Send to Netdev_chain Netdev_going_down Notice
  • called Dev_deactivat Closing a queue rule
  • Clear Dev->state's _ _link_state_start flag to indicate that the device is disabled
  • If you are performing a polling operation that reads the Ingress queue packet, wait for the operation to complete. Because of the dev->state relationship, the ingress queue will no longer be queued, so the read is currently in the queue
  • if Dev->stop has a definition, executes it.
  • Clear dev->flags the iff_up operation.
  • Send to Netdev_chain Netdev_down notice.
    
Update Queue rule status:
Add later




Configure device-related information from user spaceMany tools can be used to configure device-related parameters:
    • Ipconfig and mii-tool from the Net-tool kit
    • Ethtool from Ethtool Kit
    • IP link: from IP ROUTER2 Suite
 









Deep understanding of Linux Network Technology Insider--registration and initialization of equipment (II.)

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.