哪种是注册新net_device的正确方法? [英] Which is the correct way to register a new net_device?
问题描述
我正在尝试在linux中注册新的net_device
...我可以分配并正确注册它,而ifconfig
显示它.当我尝试设置界面时,问题就来了:
i'm trying to register a new net_device
in linux...i can alloc and register it correctly and ifconfig
shows it. The problem arrives when i try to put the interface up:
ifconfig my_dev up
发生内核冻结...问题仅存在于x86机器上,我无法弄清原因...在pcc机器上一切正常.代码很简单:
A kernel freeze occurs...the problem is present only on x86 machines and i can't figure out the reason...on a pcc machine all works well. The code is very simple:
static struct net_device *my_dev;
static int veth_dev_init(struct net_device *dev);
static int veth_open(struct net_device *dev);
static int veth_close(struct net_device *dev);
static int veth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
static struct veth_priv
{
...
};
static struct net_device_ops veth_ops =
{
.ndo_init = veth_dev_init,
.ndo_open = veth_open,
.ndo_stop = veth_close,
.ndo_do_ioctl = veth_ioctl
};
static int __init veth_init()
{
my_dev = alloc_netdev(sizeof(struct veth_priv), "my_dev", ether_setup);
if (my_dev == NULL)
return -ENOMEM;
my_dev->netdev_ops = &veth_ops;
register_netdev(my_dev);
return 0;
}
static void __exit veth_exit()
{
unregister_netdev(my_dev);
free_netdev(my_dev);
}
module_init(veth_init);
module_exit(veth_exit);
前四个函数veth_dev_init, veth_open, veth_close
和veth_ioctl
仅返回0.
也许veth_ops
结构中缺少字段?
The first four functions veth_dev_init, veth_open, veth_close
and veth_ioctl
simply return 0.
Maybe is there a missing field in veth_ops
structure?
谢谢大家!
推荐答案
是的,您错过了 struct net_device_ops 中的一个元素
还要添加 .ndo_start_xmit ,并且该函数必须返回NETDEV_TX_OK或NETDEV_TX_BUSY.
Yea, you missed one element in struct net_device_ops
Add .ndo_start_xmit also, And the function must return NETDEV_TX_OK or NETDEV_TX_BUSY.
用法如下
static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
{
return NETDEV_TX_OK;
}
并将打开更改为
static int veth_open(struct net_device *dev)
{
memcpy(dev->dev_addr, "\0ABCD0", ETH_ALEN);
netif_start_queue(dev);
return 0;
}
然后在 veth_ops
static struct net_device_ops veth_ops = {
.ndo_init = veth_dev_init,
.ndo_open = veth_open,
.ndo_stop = veth_close,
.ndo_start_xmit = veth_xmit,
.ndo_do_ioctl = veth_ioctl,
};
然后在插入模块后
给出ifconfig my_dev 192.168.10.98 ...
give ifconfig my_dev 192.168.10.98 ...
这篇关于哪种是注册新net_device的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!