Loopback Card Driver Design

Source: Internet
Author: User

The loopback network card is a virtual network card, which is sent back to the upper stack when a packet is sent from the upper layer protocol stack. The following is a programmatic implementation of the loopback network card driver.

Required header File

#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>

Global variables

Long 0    Long0;     Number of recorded packets struct net_device *dev; Define a NIC driver

From the analysis of network card driver architecture, it is first to initialize the network card.

Static int loopback_net_init (struct net *net) {    = Alloc_netdev (0,"lo%  D", Lo_setup);            Assigning Space        Register_netdev (DEV) to the NIC driver;          Register NIC driver    net->loopback_dev = Dev;     return 0 ;}

 The Alloc_etherdev function is not used here, because Alloc_etherdev can only allocate space for the Ethernet card. Here we use the Alloc_netdev function, which is defined in the Linux function as

Define Alloc_netdev (Sizeof_priv, name, Setup), where the first parameter represents the size of a member Priv space in the net_device structure, we do not use it here, so fill 0. The second parameter represents the name of the loopback network card, the third parameter is a function pointer, and the Setup function is automatically called when the space is allocated to initialize the NIC, so we move the initialized operation to the Setup function. After allocating the space, register the NIC driver with the Linux kernel. Finally tell the Linux kernel loopback card driver to Dev.

Next is the implementation of the Lo_setup function

Static        void __init lo_setup (struct net_device *dev) {    dev->netdev_ops = &lo_ops;  Network card operating function set    dev->mtu = (* *1024x768)        The maximum size of the packet to receive    dev->flags = iff_loopback;   Nic driver logo    dev->header_ops = &eth_header_ops; function set of the construction header}

 The Lo_setup function initializes the NIC. The previous article mentioned that the initialization interrupt number, I/O base address, MAC address are for the physical network card, our loopback card today is a virtual network card, so do not need to initialize them. Here are some of the items that are initialized:

1. Network card operation function set

2, the largest can receive the size of the packet

3, the network card driver flag, indicates that is a loopback network card

4, the function set of the structure head

Network card operation function set implements two functions: data receiving and querying NIC status

struct net_device_ops lo_ops = {    = lo_xmit,    = lo_get_stats,};

First look at the implementation of the Send data function:

  

 static  int  lo_xmit (struct  sk_buff *skb, struct  net_device * /*   Mark Ethernet protocol  */  SKB ->protocol = Eth_type_trans (    Skb,dev);  /*   statistics    bites  + = Skb->len;    Packets  ++;    Netif_rx (SKB);  return  0  ;}  

Because the loopback network card receives the data immediately after sending the data, it is not necessary for the upper layer protocol to pause sending the data. Furthermore, the loopback network card is a virtual network card and does not require data to be sent to the register. In this function we simply count the length of the data and the number of packets, and then send the packet back to the upper layer protocol stack.

Then the view state function

Static struct net_device_stats *lo_get_stats (struct net_device *Dev) {    struct net_ Device_stats *stats = &dev->stats;    Stats->rx_packets = packets;    Stats->tx_packets = packets;    Stats->rx_bytes = bites;    Stats->tx_bytes = bites;     return stats;}

This way, the driver for the loopback network card is roughly designed. Because the loopback network card is a virtual network card, not involved in interrupt, register and other hardware information, so the driver is very simple.

  If you have questions or errors, please note.

  

Loopback Card Driver Design

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.