"Read only" 将驱动程序函数分配给 netdev_ops 函数指针时出错

"Read only" error in assigning driver function to netdev_ops function pointer

我正在 qemu VM 运行 Ubuntu 16.04.1 中试验网络驱动程序。 uname -r 给出 4.4.0-31-generic 作为内核版本。

linux/netdevice.h for that kernel version中找到评论

"@netdev_ops:包括几个指向回调的指针, 如果有人想覆盖 ndo_*() 函数

然而,在我的代码中,诸如 dev->netdev_ops->ndo_open = netmod_open 的赋值(其中 netmod_open 是我自己的驱动程序中的 open 方法)给出了编译器错误:

assignment of member 'ndo_open' in read-only object

此外,netdev_ops在源代码中声明为const

const struct net_device_ops *netdev_ops;

如果包含它们的指针的结构是const,我如何"override the ndo_*() functions"?

How can I "override the ndo_*() functions"

定义您自己的类型 struct net_device_ops 变量,设置您要覆盖的字段(实际上是挂钩),并将此变量分配给 dev->netdev_ops

注意,根据description for struct net_device_ops,唯一需要设置的字段.ndo_start_xmit,其他字段可以保留未初始化(即NULL)。