【问题标题】:why am I getting an "implicit declaration of function 'ndo_get_stats' " error?为什么我会收到“函数'ndo_get_stats'的隐式声明”错误?
【发布时间】:2012-04-10 07:43:06
【问题描述】:

我正在构建一个非常简单的内核模块,用于从网卡收集一些统计信息,这是代码,我不断收到错误 implicit declaration of function 'ndo_get_stats'。我不知道为什么......

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/netdevice.h>    /* Needed for netdevice*/



static int __init hello_start(void)
{
    struct net_device *dev;



    printk(KERN_INFO "Loading Stats module...\n");
    printk(KERN_ALERT "Hello world\n");
    dev = first_net_device(&init_net);
    while (dev)
    {
         printk(KERN_INFO "found [%s] and it's [%d]\n", dev->name, dev->flags & IFF_UP);

         printk(KERN_INFO "End of dev struct ... now starts the get_stats struct\n");

     dev->stats = ndo_get_stats(dev);

     printk(KERN_INFO "recive errors: [%li]\n transmission errors: [%li]\n number of collisions: [%li]", dev->stats.rx_errors , dev->stats.tx_errors, dev->stats.collisions);

      dev = next_net_device(dev);
     }

    return 0;
}

static void __exit hello_end(void)
{
    printk(KERN_ALERT "Goodbye.\n");
}

module_init(hello_start);
module_exit(hello_end);

谢谢

【问题讨论】:

    标签: linux device-driver kernel-module


    【解决方案1】:

    ndo_get_stats 是一个net_device_ops 函数指针。您必须通过net_devicenetdev_ops 字段调用它。

    这样的事情会起作用:

    stats = dev->netdev_ops->ndo_get_stats(dev);
    

    【讨论】:

    • 感谢您的回答,但...incompatible types to assigning type struct net_device_stats from net_device_stats* 仍然无法正常工作
    • 请发布您的更新代码。您是否正在尝试执行“struct net_device_stats stats = dev->netdev_ops->nd_get_stats(dev);”?注意 nd_get_stats 返回一个指向 struct_dev_device_stats 的指针。
    • 另请注意,您尝试执行的操作可能无效。您不能覆盖 net_device 的 stats 字段。这是由 net_device 自己管理的。
    • 我只编辑了这一行:dev-&gt;stats = ndo_get_stats(dev); 变成:dev-&gt;stats = dev-&gt;netdev_ops-&gt;ndo_get_stats(dev);
    • 正如我所说,这是无效的。 dev->stats 字段由设备本身管理。你甚至不被允许阅读它。您需要创建一个结构 net_device_stats *stats,并将 ndo_get_stats 的结果分配给它以读取统计信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多