[国嵌攻略][134][回环网卡驱动程序设计]

回环网卡(loop)

回环网卡的Tx通道和Rx通道是直连的,所以数据直接发送到接收端口。

编写回环网卡驱动

回环网卡驱动在/driver/net/loopback.c

#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/in.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>
#include <linux/if_arp.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/percpu.h>
#include <net/net_namespace.h>

unsigned long bytes = 0;     //发送数据长度
unsigned long packets = 0;   //发送报文个数

//网卡发送数据
int loopback_xmit(struct sk_buff *skb, struct net_device *dev){
    //设置网络协议
    skb->protocol = eth_type_trans(skb, dev);
    
    //设置发送信息
    bytes += skb->len;   //发送数据长度
    packets++;           //发送报文个数
    
    //发送到协议栈
    netif_rx(skb);
    
    return 0;
}

//网卡发送状态
static struct net_device_stats *loopback_get_stats(struct net_device *dev){
    //设置发送状态
    struct net_device_stats *stats = &dev->stats;
    
    stats->tx_bytes = bytes;
    stats->rx_bytes = bytes;
    stats->tx_packets = packets;
    stats->rx_packets = packets;
    
    return stats;
}

//网卡操作方法
struct net_device_ops loopback_ops = {
    .ndo_start_xmit = loopback_xmit,       //发送数据
    .ndo_get_stats  = loopback_get_stats   //发送状态
};

//设置网卡结构
static void loopback_setup(struct net_device *dev){
    dev->mtu = (16 * 1024) + 20 + 20 + 12;   //报文最大长度
    dev->hard_header_len = ETH_HLEN;         //14
    dev->addr_len = ETH_ALEN;                 //6
    dev->tx_queue_len = 0;
    dev->type = ARPHRD_LOOPBACK;             //0x0001
    dev->flags = IFF_LOOPBACK;               //设置回环标志
    dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
    dev->features = NETIF_F_SG 
                  | NETIF_F_FRAGLIST
                  | NETIF_F_TSO
                  | NETIF_F_NO_CSUM
                  | NETIF_F_HIGHDMA
                  | NETIF_F_LLTX
                  | NETIF_F_NETNS_LOCAL;
    dev->header_ops        = &eth_header_ops;    //设置以太头部
    dev->netdev_ops        = &loopback_ops;      //设置操作方法
}

/* Setup and register the loopback device. */
static __net_init int loopback_net_init(struct net *net){
    //分配网卡结构
    struct net_device *dev;
    
    dev = alloc_netdev(0, "lo", loopback_setup);
    
    //注册网卡结构
    register_netdev(dev);
    
    //设置网卡结构
    net->loopback_dev = dev;
    
    return 0;
}

static __net_exit void loopback_net_exit(struct net *net){
    //注销网卡结构
    struct net_device *dev;
    
    dev = net->loopback_dev;
    unregister_netdev(dev);
}

/* Registered in net/core/dev.c */
struct pernet_operations __net_initdata loopback_net_ops = {
       .init = loopback_net_init,
       .exit = loopback_net_exit,
};
原文地址:https://www.cnblogs.com/d442130165/p/5267303.html