Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

linux - I want to create a dummy ethernet network device driver but not getting any interface port

I want to write a dummy network driver to create a fake ethernet port on my PC. For that i have written my driver code as follow. I am using kernel version 5.8.

#issue

  1. I am able to insert driver into kernel but not getting any printk statement in the dmesg, although i have given #echo "7" > /proc/sys/kernel/printk .
  2. Ethernet port is not created by inserting this module. #insmod fake_ethernet.ko.

Please help me in creating a dummy ethernet port by pointing out any error/mistake in my code.

    /* This is a fake ethernet driver module */

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/errno.h>
#include<linux/init.h>
#include<linux/netdevice.h>
#include<linux/etherdevice.h>
#include<linux/ethtool.h>
#include<linux/skbuff.h>
#include<linux/slab.h>
#include<linux/of.h>            /* For DT */
#include<linux/platform_device.h>   /* For plateform device */

/* private data structure...*/

struct eth_struct {
    int bar;
    int foo;
    struct net_device *dummy_ndev;
} ;

static int fake_eth_open(struct net_device *dev) {
    pr_info("fake_eth_device open called
");
    /* We are now ready to accept transmit request from 
     * queue layer of the networking.
     */
    netif_start_queue(dev);
    return 0;
}

static int fake_eth_release(struct net_device *dev) {
    pr_warn("fake_eth_device release called
") ;
    netif_stop_queue(dev) ;
    return 0;

}

static int fake_eth_xmit(struct sk_buff *skb, struct net_device *ndev) {
    pr_info("dummy xmit called
");
    ndev->stats.tx_bytes += skb->len ;
    ndev->stats.tx_packets++ ;

    skb_tx_timestamp(skb);
    dev_kfree_skb(skb);
    return NETDEV_TX_OK ;
}

static int fake_eth_init(struct net_device *dev) {
    pr_warn("fake eth device init done
") ;
    return 0;

}

static const struct net_device_ops my_netdev_ops = {

    .ndo_init = fake_eth_init,
    .ndo_open = fake_eth_open,
    .ndo_stop = fake_eth_release,
    .ndo_start_xmit = fake_eth_xmit,
    .ndo_validate_addr = eth_validate_addr,
    .ndo_validate_addr = eth_validate_addr,
};

static const struct of_device_id  fake_eth_dt_ids[] = {
    {.compatible = "packt, fake-eth", },
    { /* sential */ } 
};


static int fake_eth_probe(struct platform_device *pdev) {
    
    int ret ;
    struct eth_struct *priv;
    struct net_device *dummy_ndev ;
    
    /* allocate space for plateform device */
    priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);

    /* create instance of net_device here */
    dummy_ndev = alloc_etherdev(sizeof(struct eth_struct)) ;

    /* filling the net device with proper information */
    dummy_ndev->if_port = IF_PORT_10BASET;
    dummy_ndev->netdev_ops = &my_netdev_ops ;

    /* register the net_device object */
    ret = register_netdev(dummy_ndev);
    
    if(ret) {
        pr_info("dumm net dev: Error %d initialization card
", ret);
        return ret ;
    } //if 

    priv->dummy_ndev = dummy_ndev;
    platform_set_drvdata(pdev, priv);
    return 0;

}

static int fake_eth_remove(struct platform_device *pdev) {
    struct eth_struct *priv ;
    priv = platform_get_drvdata(pdev);
    pr_info("Cleaning up the module
");
    unregister_netdev(priv->dummy_ndev);
    free_netdev(priv->dummy_ndev) ;

    return 0 ;
}

static struct platform_driver mypdrv = {
    .probe = fake_eth_probe,
    .remove = fake_eth_remove,
        .driver = {
        .name = "fake-eth",
        .of_match_table = of_match_ptr(fake_eth_dt_ids),
        .owner = THIS_MODULE,
    },  
};

module_platform_driver(mypdrv);

MODULE_LICENSE("GPL") ;
MODULE_AUTHOR("Rakesh kumar");
MODULE_DESCRIPTION("fake ethernet driver ");
MODULE_VERSION("1.30");

Thanks in advance.

question from:https://stackoverflow.com/questions/65867380/i-want-to-create-a-dummy-ethernet-network-device-driver-but-not-getting-any-inte

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...