我正在尝试编写一个可以复制环回设备功能的网络设备驱动程序。我修改了可用的代码关联。我正在 Linux 4.8 上进行测试。我删除了与网络命名空间的统计和环回注册相关的代码。为了这变量,我找不到定义,所以现在将其删除。
/* testlo.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 <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>
#include <linux/u64_stats_sync.h>
/*
* The higher levels take care of making this non-reentrant (it's
* called with bh's disabled).
*/
netdev_tx_t loopback_xmit(struct sk_buff *skb,
struct net_device *dev)
{
//struct pcpu_lstats *lb_stats;
int len;
skb_orphan(skb);
/* Before queueing this packet to netif_rx(),
* make sure dst is refcounted.
*/
skb_dst_force(skb);
skb->protocol = eth_type_trans(skb, dev);
len = skb->len;
if (likely(netif_rx(skb) == NET_RX_SUCCESS)) { }
printk("Received packet at device driver \n");
return NETDEV_TX_OK;
}
u32 always_on(struct net_device *dev)
{
return 1;
}
const struct ethtool_ops loopback_ethtool_ops = {
.get_link = always_on,
};
int loopback_dev_init(struct net_device *dev)
{
return 0;
}
void loopback_dev_free(struct net_device *dev)
{
free_netdev(dev);
}
const struct net_device_ops loopback_ops = {
.ndo_init = loopback_dev_init,
.ndo_start_xmit= loopback_xmit,
.ndo_set_mac_address = eth_mac_addr,
};
/*
* The loopback device is special. There is only one instance
* per network namespace.
*/
void loopback_setup(struct net_device *dev)
{
dev->mtu = 64 * 1024;
dev->hard_header_len = ETH_HLEN; /* 14 */
dev->addr_len = ETH_ALEN; /* 6 */
dev->type = ARPHRD_LOOPBACK; /* 0x0001*/
dev->flags = IFF_LOOPBACK;
dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
netif_keep_dst(dev);
dev->hw_features = NETIF_F_GSO_SOFTWARE;
dev->features = NETIF_F_SG | NETIF_F_FRAGLIST
| NETIF_F_GSO_SOFTWARE
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
| NETIF_F_SCTP_CRC
| NETIF_F_HIGHDMA
| NETIF_F_LLTX
| NETIF_F_NETNS_LOCAL
| NETIF_F_VLAN_CHALLENGED
| NETIF_F_LOOPBACK;
dev->ethtool_ops = &loopback_ethtool_ops;
dev->netdev_ops = &loopback_ops;
dev->destructor = loopback_dev_free;
}
struct net_device *global_dev;
/* Setup and register the loopback device. */
int init_dev (void)
{
struct net_device *dev;
int err;
printk ("Init Module\n");
err = -ENOMEM;
dev = alloc_netdev(0, "testlo", NET_NAME_UNKNOWN, loopback_setup);
if (!dev)
goto out;
err = register_netdev(dev);
if (err)
goto out_free_netdev;
BUG_ON(dev->ifindex != LOOPBACK_IFINDEX);
global_dev = dev;
return 0;
out_free_netdev:
free_netdev(dev);
out:
return err;
}
void cleanup (void)
{
printk ("Cleaning Up Module\n");
unregister_netdev (global_dev);
return;
}
module_init (init_dev);
module_exit (cleanup);
生成文件:
obj-m += testlo.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
一旦我编译代码并插入内核模块,我就可以在列表中看到我的新设备(testlo)ip a
。我可以将设备状态设置为启动并为其分配 IP 地址。但是,如果我尝试 ping 该 IP(我确实看到了 ping 响应),则代码的功能loopback_xmit()
不会被触发。如果我检查wireshark,当我ping 新设备的IP 时,没有任何痕迹。在默认环回设备 (lo) 中,ping 会127.0.0.1
在wireshark 中显示跟踪。
有人可以告诉代码中缺少什么以使其表现得像环回设备吗?