Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 注册新网络设备的正确方法是什么?_Linux_Linux Kernel - Fatal编程技术网

Linux 注册新网络设备的正确方法是什么?

Linux 注册新网络设备的正确方法是什么?,linux,linux-kernel,Linux,Linux Kernel,我正在尝试在linux中注册一个新的net\u设备。我可以分配并正确注册它,然后ifconfig显示它。当我尝试设置界面时,问题出现了: ifconfig my_dev up 内核冻结发生了…问题只出现在x86机器上,我无法找出原因…在pcc机器上一切正常。代码非常简单: static struct net_device *my_dev; static int veth_dev_init(struct net_device *dev); static int veth_open(struct

我正在尝试在linux中注册一个新的
net\u设备
。我可以分配并正确注册它,然后
ifconfig
显示它。当我尝试设置界面时,问题出现了:

ifconfig my_dev up
内核冻结发生了…问题只出现在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);
前四个函数
veth_dev_init、veth_open、veth_close
veth_ioctl
只返回0。 可能在
veth_ops
结构中缺少字段


谢谢大家!

是的,您在结构网络设备操作中遗漏了一个元素
同时添加.ndo\u start\u xmit,该函数必须返回NETDEV\u TX\u OK或NETDEV\u TX\u 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我的_dev192.168.10.98…

谢谢Faisal,它很有效!但我无法理解为什么在我设置界面时系统应该调用函数
dev->netdev_ops->ndo_start_xmit
?示例版本的
veth_xmit
可能也应该调用
consumer_skb(skb)
。如果启用了ipv4,系统可能会为您的ip发送arp请求,查看本地lan上是否存在冲突。对于IPv6,系统可以执行邻居发现。当我打开一个接口时,我通常会发现大约有30个数据包离开。啊,好的!这是有道理的!也许所有这些都可以在ppc系统上运行,这仅仅取决于系统不发送这些数据包的事实……谢谢sharth!