开山之作--一个虚拟网卡的实现


这是一个虚拟网卡实现的初级代码。。其中功能还有待改善。。先贴出来再说
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>

MODULE_LICENSE("Dual BSD/GPL");
static LIST_HEAD(zzh_nic_list);
static int zzh_nic_open(struct net_device *dev)
{
	printk("zzh_nic_open run\n");
	return 1;
}

static int zzh_nic_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	printk("zzh_nic_start_xmit run\n");
	kfree_skb(skb);
	return 1;
}

static int zzh_nic_close(struct net_device *dev)
{
	printk("zzh_nic_close run!\n");
	return 1;
}

struct zzh_nic_struct{
	struct list_head list;
	struct net_device *dev;
};

static const struct net_device_ops zzh_nic_netdev_ops={
	.ndo_open	= zzh_nic_open,
	.ndo_stop	= zzh_nic_close,
	.ndo_start_xmit	= zzh_nic_start_xmit,
};

void zzh_nic_setup(struct net_device *dev)
{
	struct zzh_nic_struct *zzh_nic = netdev_priv(dev);
	ether_setup(dev);
	dev->netdev_ops = &zzh_nic_netdev_ops;
	random_ether_addr(dev->dev_addr);
	zzh_nic->dev=dev;
	list_add(&zzh_nic->list,&zzh_nic_list);
}

struct net_device *zzh_nic_setup_dev(void)
{
	struct net_device *dev;
	dev = alloc_netdev(sizeof(struct zzh_nic_struct),"zzh_nic%d",zzh_nic_setup);
	if (dev == NULL){
		
		printk("alloc net device failed\n");
		return NULL;
	}
	if (register_netdev(dev) != 0){
		printk("register net device failed\n");
		return NULL;
	}
	return dev;
}

int zzh_nic_shutdown_dev(void){

	struct zzh_nic_struct *zzh_nic;
	struct zzh_nic_struct *next;
	list_for_each_entry_safe(zzh_nic,next,&zzh_nic_list,list){
		list_del(&zzh_nic->list);
		unregister_netdev(zzh_nic->dev);
		free_netdev(zzh_nic->dev);
	}
	return 0;
}

static int zzh_nic_init(void)
{
	
	struct net_device *dev;
	printk("zzh_nic_init run\n");
	if ((dev = zzh_nic_setup_dev()) == NULL){
		printk("setup net device failed\n");
		return -1;
	}
	return 0;
}

static void zzh_nic_exit(void)
{
	printk("zzh_nic_exit run\n");
	if (zzh_nic_shutdown_dev() == -1)
		printk("shutdown net device failed\n");
}
module_init(zzh_nic_init);
module_exit(zzh_nic_exit);

Makefile内容:
ifneq ($(KERNELRELEASE),)
obj-m :=zzh_nic.o
else
PWD :=$(shell pwd)
KDIR :=/lib/modules/$(shell uname -r)/build

default:
    $(MAKE) -C $(KDIR) M=$(PWD) modules
endif

/*
KERNELRELEASE
是在内核源码的顶层Makefile中定义的一个变量,在第一次读取执行此Makefile时,KERNELRELEASE没有被定义,
所以make将读取执行else之后的内容。如果make的目标是clean,直接执行clean操作,然后结束。当make的目标为all时,-C
$(KDIR) 指明跳转到内核源码目录下读取那里的Makefile;M=$(PWD)
表明然后返回到当前目录继续读入、执行当前的Makefile。当从内核源码目录返回时,KERNELRELEASE已被被定义,kbuild也被启动去
解析kbuild语法的语句,make将继续读取else之前的内容。else之前的内容为kbuild语法的语句,
指明模块源码中各文件的依赖关系,以及要生成的目标模块名。。obj-m :=
zzh_nic.o表示编译连接后将生成mytest.o模块。
*/

编译完后;
insmod zzh_nic.ko
可以查看是否加入:
cat /proc/net/dev
可以看到咱们的网卡已经加入
sudo ifconfig zzh_nic0 192.168.4.1 netmask 255.255.255.0 up  
然后
ifconfig zzh_nic0
可以看到相关信息

虚拟网卡驱动源代码(原版): /* * snull.c -- the Simple Network Utility * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $ */ #include #include #include #include #include #include /* printk() */ #include /* kmalloc() */ #include /* error codes */ #include /* size_t */ #include /* mark_bh */ #include #include /* struct device, and other headers */ #include /* eth_type_trans */ #include /* struct iphdr */ #include /* struct tcphdr */ #include #include "snull.h" #include #include MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet"); MODULE_LICENSE("Dual BSD/GPL"); /* * Transmitter lockup simulation, normally disabled. */ static int lockup = 0; module_param(lockup, int, 0); static int timeout = SNULL_TIMEOUT; module_param(timeout, int, 0); /* * Do we run in NAPI mode? */ static int use_napi = 0; module_param(use_napi, int, 0); /* * A structure representing an in-flight packet. */ struct snull_packet { struct snull_packet *next; struct net_device *dev; int datalen; u8 data[ETH_DATA_LEN]; }; int pool_size = 8; module_param(pool_size, int, 0); /* * This structure is private to each device. It is used to
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值