#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/skbuff.h>
#define TAG "HELLO# "
static int my_netdev_open(struct net_device *dev)
{
printk(TAG "%s called\n", __func__);
return 0;
}
static int my_netdev_close(struct net_device *dev)
{
printk(TAG "%s called\n", __func__);
return 0;
}
static netdev_tx_t my_netdev_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
printk(TAG "%s called\n", __func__);
return NETDEV_TX_OK;
}
static int my_netdev_set_mac_add(struct net_device *dev, void *p)
{
printk(TAG "%s called\n", __func__);
return 0;
}
static struct net_device_ops my_netdev_netdev_ops = {
.ndo_open = my_netdev_open,
.ndo_stop = my_netdev_close,
.ndo_start_xmit = my_netdev_start_xmit,
.ndo_set_mac_address = my_netdev_set_mac_add,
.ndo_validate_addr = eth_validate_addr,
};
static struct net_device *dev = NULL;
static int my_netdev_init(void)
{
printk(TAG "%s called\n", __func__);
dev = alloc_etherdev(0);
dev->dev_addr_shadow[0] = 0xaa;
dev->dev_addr_shadow[1] = 0xbb;
dev->dev_addr_shadow[2] = 0xcc;
dev->dev_addr_shadow[3] = 0xdd;
dev->dev_addr_shadow[4] = 0xee;
dev->dev_addr_shadow[5] = 0xff;
dev->dev_addr = dev->dev_addr_shadow;
snprintf(dev->name, IFNAMSIZ, "hello");
dev->netdev_ops = &my_netdev_netdev_ops;
register_netdev(dev);
return 0;
}
static void my_netdev_exit(void)
{
unregister_netdev(dev);
free_netdev(dev);
printk(TAG "%s called\n", __func__);
}
module_init(my_netdev_init);
module_exit(my_netdev_exit);
MODULE_LICENSE("GPL");
效果
可以看到多一个一个hello名字的网卡