Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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/github/3.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中的net_泛型函数include/net/net_namespace.h是什么?_Linux_Linux Kernel_Linux Device Driver - Fatal编程技术网

linux中的net_泛型函数include/net/net_namespace.h是什么?

linux中的net_泛型函数include/net/net_namespace.h是什么?,linux,linux-kernel,linux-device-driver,Linux,Linux Kernel,Linux Device Driver,我是Linux开发新手。我正在编写一个Linux网络驱动程序示例教程,遇到了net_generic(const struct net*net,int id)函数。有人能解释一下net_generic(const struct net*net,int id)的用法吗 我在谷歌上搜索它,但只找到了头文件。在我可以参考的资源(站点或书籍)中,有人能指给我看吗。感谢有一种方法可以在创建或销毁新的网络名称空间时获得网络核心的通知。例如,作为设备驱动程序开发人员或其他内核代码开发人员,当创建或销毁新的网络名

我是Linux开发新手。我正在编写一个Linux网络驱动程序示例教程,遇到了net_generic(const struct net*net,int id)函数。有人能解释一下net_generic(const struct net*net,int id)的用法吗
我在谷歌上搜索它,但只找到了头文件。在我可以参考的资源(站点或书籍)中,有人能指给我看吗。感谢

有一种方法可以在创建或销毁新的网络名称空间时获得网络核心的通知。例如,作为设备驱动程序开发人员或其他内核代码开发人员,当创建或销毁新的网络名称空间时,您的模块希望得到网络核心的通知。为此,您需要创建struct pernet_操作的对象,并且必须使用register_pernet_subsys()函数向网络子系统注册。在驱动程序中,您可能希望将一些驱动程序私有数据存储在struct net对象中,该对象是网络命名空间的对象,并且希望在收到有关命名空间事件的通知时访问该私有数据。这就像在net_设备对象中有一个驱动程序私有数据一样

因此,您可以做的是,在pernet_操作结构中有两个字段,“id”和“size”。id是指向整数的指针,size是整数。您需要在驱动程序中有一个全局整数变量,并将该地址存储在结构的“id”字段中,并告诉您想要的私有数据的大小

例如:

  static struct pernet_operations bond_net_ops = {
       .init = bond_net_init,
       .exit = bond_net_exit, 
       .id   = &bond_net_id,
       .size = sizeof(struct bond_net),
  };    
之后,当您调用register_pernet_subsys()函数向网络子系统注册时,网络子系统将分配所需大小的内存,并在struct net结构中进行内部维护。并创建一个唯一的id,并将其存储在“id”指向的指针中,这意味着在上述情况下是在bond_net_id中。此id类似于您分配的私有数据的锚

在此之后,每当您想要访问指向私有数据的指针时,都可以调用net_generic()函数,该函数返回分配内存的开始。例如,在上述情况下,采用这种方式

      static void __net_exit bond_net_exit(struct net *net)
      {       
           struct bond_net *bn = net_generic(net, bond_net_id);
      }

你可以参考driver-drivers/net/bonding/bond_main.c.

谢谢你的回复,你能给我指一本我可以参考这些函数的书或者一个有一些细节的网站吗请参阅Rami Rosen的《Linux内核网络:实现与理论》一书