Skip to content

LinuxKernelContent/NetDevice

Repository files navigation

About

This is a basic example of a Linux kernel network driver.

The code defines a network device driver named "my_netdev". The driver provides three functions: my_netdev_open is called when the device is opened, my_netdev_release is called when the device is closed, and my_netdev_xmit is called when a packet is transmitted.

In the my_netdev_init function, the driver first allocates memory for the network device using the alloc_etherdev function, which creates an Ethernet device with an automatically generated MAC address. The network device operations structure my_netdev_ops is then initialized with pointers to the driver's open, release, and xmit functions.

Next, the name of the network device is set to "my_netdev" using the strcpy function. Finally, the driver is registered with the kernel using register_netdev.

In the my_netdev_exit function, the driver is unregistered and the memory allocated for the network device is freed using the unregister_netdev and free_netdev functions.

With this code, you can load the driver into the kernel by compiling it into a module and inserting it into the kernel using the insmod command. Once loaded, the driver will create a network interface called "my_netdev". You can then use this interface like any other network interface to transmit and receive packets.

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.108  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2a0d:6fc2:6691:3000:9a5b:c12c:98d9:9ca2  prefixlen 64  scopeid 0x0<global>
        inet6 2a0d:6fc2:6691:3000:7bc0:11e9:16f2:275e  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::4e89:f128:ab6:c6ef  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:ee:ae:ea  txqueuelen 1000  (Ethernet)
        RX packets 502206  bytes 738168057 (738.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 43183  bytes 3852633 (3.8 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1071  bytes 127724 (127.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1071  bytes 127724 (127.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

my_netdev: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:00:00:00:00:00  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Some notes

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

These lines are preprocessor directives that include various header files required for the program to run. module.h contains macros and functions that are used to load and unload kernel modules, kernel.h provides access to various kernel functions and structures, init.h contains functions for initializing kernel objects, netdevice.h contains structures and functions related to network devices, and etherdevice.h contains functions for Ethernet devices.

static struct net_device *netdev;

This line declares a pointer to a structure of type net_device, which is the main structure used to represent network devices in the Linux kernel.

static int my_netdev_open(struct net_device *dev)
{
    printk(KERN_INFO "my_netdev: device opened\n");
    netif_start_queue(dev);
    return 0;
}

This is a function that will be used as the device's open callback. The open callback is called when a network device is opened, and in this case, the function simply prints a message to the kernel log and starts the device's transmission queue.

static int my_netdev_release(struct net_device *dev)
{
    printk(KERN_INFO "my_netdev: device released\n");
    netif_stop_queue(dev);
    return 0;
}

This is a function that will be used as the device's release callback. The release callback is called when a network device is closed, and in this case, the function simply prints a message to the kernel log and stops the device's transmission queue.

static netdev_tx_t my_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
{
    printk(KERN_INFO "my_netdev: packet transmitted\n");
    dev_kfree_skb(skb);
    return NETDEV_TX_OK;
}

This is a function that will be used as the device's xmit callback. The xmit callback is called when a network packet is ready to be transmitted, and in this case, the function simply prints a message to the kernel log and frees the packet buffer.

static const struct net_device_ops my_netdev_ops = {
    .ndo_open = my_netdev_open,
    .ndo_stop = my_netdev_release,
    .ndo_start_xmit = my_netdev_xmit,
};

This declares a net_device_ops structure, which is used to define the network device operations. In this case, the structure is initialized with pointers to the open, release, and xmit callbacks defined earlier.

static int __init my_netdev_init(void)
{
    netdev = alloc_etherdev(0);
    if (!netdev) {
        printk(KERN_ERR "my_netdev: failed to allocate etherdev\n");
        return -ENOMEM;
    }

    netdev->netdev_ops = &my_netdev_ops;
    strcpy(netdev->name, "my_netdev");

    if (register_netdev(netdev)) {
        printk(KERN_ERR "my_netdev: failed to register netdev\n");
        free_netdev(netdev);
        return -EINVAL;
    }

    printk(KERN_INFO "my_netdev: driver loaded\n");
    return 0;
}

This is the module initialization function. It allocates an Ethernet network device using the alloc_etherdev function, sets the net_device_ops structure to the operations defined earlier, sets the device name to "my_net

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published