【问题标题】:Linux kernel module to change the MTU size is not working更改 MTU 大小的 Linux 内核模块不起作用
【发布时间】:2014-01-29 07:36:19
【问题描述】:

我正在学习内核模块并且是新手。 我想更改 eth0 的 MTU 大小。这是我写的模块程序。

目的是将 eth0 的 MTU 大小更改为 1000。但它没有改变。 谁能告诉我,我错过了什么。如果方法本身是错误的,你能指出我正确的方向吗?

#include <linux/module.h>       
#include <linux/kernel.h>       
#include <linux/init.h>         
#include <linux/etherdevice.h>
#include <linux/netdevice.h>

static int __init hello_2_init(void)
{
        printk(KERN_INFO "Hello, world 2\n");

        struct net_device dev;
        eth_change_mtu(&dev,1000); //calling eth_change_mtu
        return 0;
}

static void __exit hello_2_exit(void)
{
        printk(KERN_INFO "Goodbye, world 2\n");
}


int eth_change_mtu(struct net_device *dev, int new_mtu)
{
        dev->mtu = new_mtu;
        printk(KERN_INFO "New MTU is %d",new_mtu);
        return 0;
}

module_init(hello_2_init);
module_exit(hello_2_exit);

【问题讨论】:

  • 你不需要内核模块来做这个,在终端输入这个命令ifconfig eth0 mtu 1000 up

标签: linux network-programming linux-device-driver kernel-module mtu


【解决方案1】:

您在未分配给任何实际网络设备的结构上设置 MTU。您在 init 中声明了 local 变量 dev,然后更改了它的字段。

您应该首先找到要更改的网络设备。这是通过__dev_get_by_name 完成的,如下所示:

struct net_device *dev = __dev_get_by_name("eth0");

之后,您的更改将应用​​到您的网络接口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2020-09-12
    • 1970-01-01
    • 2017-04-25
    • 2020-04-20
    • 2019-03-20
    • 2012-04-11
    相关资源
    最近更新 更多