注册新的net_device的正确方法是什么?

Mir*_*chi 7 linux linux-kernel

我正在尝试net_device在linux中注册一个新的...我可以正确分配和注册它并ifconfig显示它.当我尝试打开界面时,问题就出现了:

ifconfig my_dev up
Run Code Online (Sandbox Code Playgroud)

内核冻结发生......问题仅存在于x86机器上,我无法弄清楚原因......在pcc机器上运行良好.代码很简单:

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);
Run Code Online (Sandbox Code Playgroud)

前四个函数veth_dev_init, veth_open, veth_closeveth_ioctl简单地返回0.也许veth_ops结构中缺少一个字段?

谢谢你们!

小智 5

是的,你错过了struct net_device_ops中的 一个元素
添加.ndo_start_xmit,并且该函数必须返回NETDEV_TX_OK或NETDEV_TX_BUSY.

使用如下

static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
{
    return NETDEV_TX_OK;
}
Run Code Online (Sandbox Code Playgroud)

并且还将打开更改为

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

然后在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,
};
Run Code Online (Sandbox Code Playgroud)

然后插入模块后

给ifconfig my_dev 192.168.10.98 ...