我想创建一个虚拟以太网网络设备驱动程序,但没有获得任何接口端口



我想写一个虚拟网络驱动程序来在我的PC上创建一个假的以太网端口。为此,我写了我的驱动程序代码如下。我使用的是内核版本5.8

#问题

  1. 我能够将驱动程序插入内核,但在dmesg中没有得到任何printk语句,尽管我已经给出了#echo "7"比;/proc/sys/kernel/printk
  2. 没有通过插入该模块创建以太网端口。# insmod fake_ethernet.ko。

请通过指出我代码中的任何错误/错误来帮助我创建虚拟以太网端口。

/* 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 calledn");
/* 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 calledn") ;
netif_stop_queue(dev) ;
return 0;
}
static int fake_eth_xmit(struct sk_buff *skb, struct net_device *ndev) {
pr_info("dummy xmit calledn");
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 donen") ;
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 cardn", 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 modulen");
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");

提前感谢。

我认为你的代码是嵌入式linux,你必须有一个dts设置。我添加了一个像这样的假节点:

fake_eth {
compatible = "packt, fake-eth";
status = "ok";
};

和I工作。它添加了一个eth1接口

eth1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
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

相关内容

最新更新