Linux Network device driver programming (2)

Source: Internet
Author: User

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-&GT;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)

Related Article

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.