Logesh
Logesh

Reputation: 53

Linux kernel module to change the MTU size is not working

I am learning kernel modules and new to it. I want to change the MTU size of the eth0. Here is the module program i have written.

The intention is to change the MTU size of eth0 to 1000.But its not getting changed. Can anyone tell me, what I am missing. If the approach itself is wrong , can you point me in the right direction?

#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);

Upvotes: 1

Views: 1955

Answers (1)

Alexander Dzyoba
Alexander Dzyoba

Reputation: 4189

You are setting MTU on structure that is not assigned with any actual network device. You declared local variable dev in init and then changed it's field.

You should first find network device you want to change. This is done with __dev_get_by_name like this:

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

After that your changes will be applied to your network interface.

Upvotes: 1

Related Questions