What is the right way to register a new network device?

I am trying to register a new net_device in linux ... I can correctly detect and register it, and ifconfig shows it. The problem occurs when I try to put the interface up:

 ifconfig my_dev up 

Kernel freezing occurs ... the problem is present only on x86 machines, and I can not understand the reason ... on pcc machine everything works fine. 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); 

The first four functions veth_dev_init, veth_open, veth_close and veth_ioctl just return 0. Perhaps there is a missing field in the veth_ops structure?

Thanks everyone!

+6
source share
1 answer

Yes, you missed one element in the net_device_ops structure
Add .ndo_start_xmit , and the function should return NETDEV_TX_OK or NETDEV_TX_BUSY.

use as follows

 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev) { return NETDEV_TX_OK; } 

And also change open as

 static int veth_open(struct net_device *dev) { memcpy(dev->dev_addr, "\0ABCD0", ETH_ALEN); netif_start_queue(dev); return 0; } 

Then in 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, }; 

Then after inserting the module

specify ifconfig my_dev 192.168.10.98 ...

+5
source

Source: https://habr.com/ru/post/892956/


All Articles