Two. Program design of loopback network card
/********************************** loopback nic driver ************** Lu Xiaoning ********************2015.12.26***************** *******************/#include<linux/kernel.h>#include<linux/jiffies.h>#include<linux/module.h>#include<linux/interrupt.h>#include<linux/fs.h>#include<linux/types.h>#include<linux/string.h>#include<linux/socket.h>#include<linux/errno.h>#include<linux/fcntl.h>#include<linux/inch.h>#include<linux/init.h>#include<asm/system.h>#include<asm/uaccess.h>#include<asm/io.h>#include<linux/inet.h>#include<linux/netdevice.h>#include<linux/etherdevice.h>#include<linux/skbuff.h>#include<linux/ethtool.h>#include<net/sock.h>#include<net/checksum.h>#include<linux/if_ether.h>/*For the statistics structure.*/#include<linux/if_arp.h>/*For Arphrd_ether*/#include<linux/ip.h>#include<linux/tcp.h>#include<linux/percpu.h>#include<net/net_namespace.h>unsignedLongbytes =0; unsignedLongPackets =0;structNet_device *Dev;//Send functionintLoopback_xmit (structSk_buff *SKB,structNet_device *Dev) { //writes the value of the SKB to the RegisterSkb->protocol =Eth_type_trans (Skb,dev); //amount of data sentBytes + = skb->Len; Packets++; Netif_rx (SKB); return 0;}//Accept FunctionStatic structNet_device_stats *loopback_get_stats (structNet_device *Dev) { //Read Accept State structNet_device_stats *stats = &dev->stats; //Read Accept lengthStats->tx_bytes =bytes; Stats->rx_bytes =bytes; Stats->rx_packets =packets; Stats->tx_packets =packets; returnstats; }structNet_device_ops Loopback_ops ={. Ndo_start_xmit=loopback_xmit,. Ndo_get_stats=Loopback_get_stats,};void StaticLoopback_setup (structNet_device *Dev) { //2. Net_dev Initialization ParametersDEV->MTU = ( -*1024x768)+ -+ -+ A; Dev->flags =Iff_loopback; Dev->header_ops = &Eth_header_ops; Dev->netdev_ops = &Loopback_ops; }/*Setup and register the loopback device.*/Static__net_initintLoopback_net_init (structNET *net) { intErr =-Enomem; //1. Assigning Net_device Structuresdev = Alloc_netdev (0,"lo%d", Loopback_setup); //4. Completion of Net_device registrationErr =Register_netdev (Dev); NET->loopback_dev =Dev; return 0; }Static__net_exitvoidLoopback_net_exit (structNET *net) { //Unregister this loopback card driverUnregister_netdev (dev);}/*Registered in NET/CORE/DEV.C*/structPernet_operations __net_initdata loopback_net_ops ={. init=loopback_net_init,. Exit=Loopback_net_exit,};
Linux Network device driver programming (2)