【问题标题】:what is net_generic function in linux include/net/net_namespace.h?linux include/net/net_namespace.h 中的 net_generic 函数是什么?
【发布时间】:2013-11-25 10:32:48
【问题描述】:

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

【问题讨论】:

    标签: linux linux-kernel linux-device-driver


    【解决方案1】:

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

    所以你可以做的是,在 pernet_operations 结构中有两个字段,'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);
          }
    

    可以参考驱动drivers/net/bonding/bond_main.c。

    【讨论】:

    • 感谢您的回复,您能不能给我指出一本我可以参考这些功能的书或有一些详细信息的网站
    • 参考 Rami Rosen 所著的 Linux Kernel Networking: Implementation and Theory 一书
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多