The data transmission on the link layer of the linux protocol stack

  

<<prison break>> The fifth episode of the third season is finally late in the hope, scofid uses its amazing wisdom once The next time I was in danger, but how will he escape in the evil Sona prison? We don't know this, but we can analyze the Linux network driver to get the data packet through the layer of the physical interface to achieve communication purposes:-) A: Prerequisite knowledge about I /O memory mapping. The device is connected to the CPU via a control bus, a data bus, and a status bus. The total number of control signals is transmitted, for example, the activation of the network card. The data bus controls data transmission. For example, the network card sends data. The total number of states is generally the current state of the reading device, such as reading the MAC address of the network card. In the traditional operation, it is realized by reading and writing the value of the device register. But this consumes the CPU clock. Moreover, the device registers are read every time the value is taken, resulting in low efficiency. In the modern operating system
. I/O memory map is referenced. That is, the value of the register is reflected to the main memory. The operation of the device registers is converted to the operation of the main memory, which greatly improves the efficiency. About DMA This is a way of handling device data. The traditional processing method is: when the device receives the data, it reports the interrupt to the CPU. The CPU processes the interrupt and puts the data into memory. The DMA introduced in the modern operating system means that when the device receives the data, it puts the data into the DMA memory and then generates an interrupt to the CPU. This saves a lot of CPU time. Regarding soft interrupts and NAPI in modern operating systems, the processing speed of interrupts is getting higher and higher. In response to the interruption, the interruption is divided into two parts, the upper half and the lower half. The top half pushes the data into the processing queue and responds to the interrupt. Then the second half is scheduled to complete the remaining tasks. NAPI is a new concept introduced in 2.6 that disables interrupts when an interrupt occurs. Then process the data. After that, at regular intervals, it will actively ask the device if there is any data to process. I/O, DMA will discuss the implementation in linux2.6.21 in subsequent code analysis. The detailed knowledge of soft interrupts and NAPI will be used to analyze the interrupt handling, one by one: from the NIC driver. Take the intel 100M NIC driver as an example to briefly outline the process of receiving and sending data packets. The code see (drivers/net/e100.c) NIC is a PCI device, its registration is no different from the general PCI device registration. static int __init e100_init_module (void) {if (((1 & lt; & lt; debug) - 1) & NETIF_MSG_DRV) {printk (KERN_INFO PFX "% s,% s \\ n ", DRV_DESCRIPTION, DRV_VERSION); printk (KERN_INFO PFX "% s \\ n ", DRV_COPYRIGHT);} //Register PCIreturn pci_module_init (& e100_driver);} where e100_driver corresponded to pci_driver.static struct pci_driver e100_driver = {//NIC driver name corresponding .name = DRV_NAME, //match type .id_table = e100_id_table, //detection function .probe = e100_probe, //remove function, this function is called .remove = __devexit_p (e100_remove) when the device is removed, # ifdef CONFIG_PM.suspend = e100_suspend ,. Resume = e100_resume,#endif}

When the total number of PCI devices is detected to match the parameters in e100_id_table, e100_probe will be called to start device initialization in e100_probe: static int __devinit e100_probe(struct pci_dev *pdev, const Struct pci_device_id *ent){struct net_device *netdev;struct nic *nic;int err;//Assign net_device and assign it //alloc_etherdev to the net_device assignment function of the Ethernet interface. It is alloc_netdev wrapper function if {if (((1 & lt; & lt; debug) - 1) & NETIF_MSG_PROBE) ((netdev = alloc_etherdev (sizeof (struct nic)))!) Printk (KERN_ERR PFX " Etherdev alloc failed ., abort \\ n "); return -ENOMEM;}

//initial value of the function pointer in netdev netdev- & gt; open = e100_open; netdev- & gt; stop = e100_close; netdev- & gt; //the tool support ethtool; hard_start_xmit = e100_xmit_frame; netdev- & gt; get_stats = e100_get_stats; netdev- & gt; set_multicast_list = e100_set_multicast_list; netdev- & gt; set_mac_address = e100_set_mac_address; netdev- & gt; change_mtu = e100_change_mtu; netdev- & gt; do_ioctl = e100_do_ioctl effective SET_ETHTOOL_OPS (netdev, & e100_ethtool_ops); netdev- & gt; tx_timeout = e100_tx_timeout; netdev- & gt; watchdog_timeo = E100_WATCHDOG_PERIOD; //polling function netdev- & gt; poll = e100_poll; netdev- & gt; weight = E100_NAPI_WEIGHT; #ifdef CONFIG_NET_POLL_CONTROLLERnetdev - & gt; poll_controller = e100_netpoll; # endif //get net_device private data area, and its assignment

//private data size is alloc_ etherdev () parameter specified nic = netdev_priv (netdev); nic- & gt; netdev = netdev; nic- & gt; pdev = pdev; nic- & gt; msg_enable = (1 & lt; & lt; debug) - 1; pci_set_drvdata (pdev , netdev); //prepare for the card after the start DMA, I /O memory mapped control register //it is actually assigned to PCI achieved if ((err = pci_enable_device (pdev))) {DPRINTK (PROBE. , ERR, ". Can not enable PCI device, aborting \\ n ");

Copyright © Windows knowledge All Rights Reserved