下载 >  网络技术 >  网络基础 > 虚拟网卡驱动源代码(原版)

虚拟网卡驱动源代码(原版) 评分:

虚拟网卡驱动源代码(原版): /* * 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 deri ved 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 <linux/config.h> #include <linux/module.h> #include <linux/init.h> #include <linux/moduleparam.h> #include <linux/sched.h> #include <linux/kernel.h> /* printk() */ #include <linux/slab.h> /* kmalloc() */ #include <linux/errno.h> /* error codes */ #include <linux/types.h> /* size_t */ #include <linux/interrupt.h> /* mark_bh */ #include <linux/in.h> #include <linux/netdevice.h> /* struct device, and other headers */ #include <linux/etherdevice.h> /* eth_type_trans */ #include <linux/ip.h> /* struct iphdr */ #include <linux/tcp.h> /* struct tcphdr */ #include <linux/skbuff.h> #include "snull.h" #include <linux/in6.h> #include <asm/checksum.h> 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 pass * packets in and out, so there is place for a packet */ struct snull_priv { struct net_device_stats stats; int status; struct snull_packet *ppool; struct snull_packet *rx_queue; /* List of incoming packets */ int rx_int_enabled; int tx_packetlen; u8 *tx_packetdata; struct sk_buff *skb; spinlock_t lock; }; static void snull_tx_timeout(struct net_device *dev); static void (*snull_interrupt)(int, void *, struct pt_regs *); /* * Set up a device's packet pool. */ void snull_setup_pool(struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); int i; struct snull_packet *pkt; priv->ppool = NULL; for (i = 0; i < pool_size; i++) { pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL); if (pkt == NULL) { printk (KERN_NOTICE "Ran out of memory allocating packet pool\n"); return; } pkt->dev = dev; pkt->next = priv->ppool; priv->ppool = pkt; } } void snull_teardown_pool(struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); struct snull_packet *pkt; while ((pkt = priv->ppool)) { priv->ppool = pkt->next; kfree (pkt); /* FIXME - in-flight packets ? */ } } /* * Buffer/pool management. */ struct snull_packet *snull_get_tx_buffer(struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); unsigned long flags; struct snull_packet *pkt; spin_lock_irqsave(&priv;->lock, flags); pkt = priv->ppool; priv->ppool = pkt->next; if (priv->ppool == NULL) { printk (KERN_INFO "Pool empty\n"); netif_stop_queue(dev); } spin_unlock_irqrestore(&priv;->lock, flags); return pkt; } void snull_release_buffer(struct snull_packet *pkt) { unsigned long flags; struct snull_priv *priv = netdev_priv(pkt->dev); spin_lock_irqsave(&priv;->lock, flags); pkt->next = priv->ppool; priv->ppool = pkt; spin_unlock_irqrestore(&priv;->lock, flags); if (netif_queue_stopped(pkt->dev) && pkt->next == NULL) netif_wake_queue(pkt->dev); } void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt) { unsigned long flags; struct snull_priv *priv = netdev_priv(dev); spin_lock_irqsave(&priv;->lock, flags); pkt->next = priv->rx_queue; /* FIXME - misorders packets */ priv->rx_queue = pkt; spin_unlock_irqrestore(&priv;->lock, flags); } struct snull_packet *snull_dequeue_buf(struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); struct snull_packet *pkt; unsigned long flags; spin_lock_irqsave(&priv;->lock, flags); pkt = priv->rx_queue; if (pkt != NULL) priv->rx_queue = pkt->next; spin_unlock_irqrestore(&priv;->lock, flags); return pkt; } /* * Enable and disable receive interrupts. */ static void snull_rx_ints(struct net_device *dev, int enable) { struct snull_priv *priv = netdev_priv(dev); priv->rx_int_enabled = enable; } /* * Open and close */ int snull_open(struct net_device *dev) { /* request_region(), request_irq(), .... (like fops->open) */ /* * Assign the hardware address of the board: use "\0SNULx", where * x is 0 or 1. The first byte is '\0' to avoid being a multicast * address (the first byte of multicast addrs is odd). */ memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN); if (dev == snull_devs[1]) dev->dev_addr[ETH_ALEN-1]++; /* \0SNUL1 */ netif_start_queue(dev); return 0; } int snull_release(struct net_device *dev) { /* release ports, irq and such -- like fops->close */ netif_stop_queue(dev); /* can't transmit any more */ return 0; } /* * Configuration changes (passed on by ifconfig) */ int snull_config(struct net_device *dev, struct ifmap *map) { if (dev->flags & IFF_UP) /* can't act on a running interface */ return -EBUSY; /* Don't allow changing the I/O address */ if (map->base_addr != dev->base_addr) { printk(KERN_WARNING "snull: Can't change I/O address\n"); return -EOPNOTSUPP; } /* Allow changing the IRQ */ if (map->irq != dev->irq) { dev->irq = map->irq; /* request_irq() is delayed to open-time */ } /* ignore other fields */ return 0; } /* * Receive a packet: retrieve, encapsulate and pass over to upper levels */ void snull_rx(struct net_device *dev, struct snull_packet *pkt) { struct sk_buff *skb; struct snull_priv *priv = netdev_priv(dev); /* * The packet has been retrieved from the transmission * medium. Build an skb around it, so upper layers can handle it */ skb = dev_alloc_skb(pkt->datalen + 2); if (!skb) { if (printk_ratelimit()) printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n"); priv->stats.rx_dropped++; goto out; } skb_reserve(skb, 2); /* align IP on 16B boundary */ memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen); /* Write metadata, and then pass to the receive level */ skb->dev = dev; skb->protocol = eth_type_trans(skb, dev); skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ priv->stats.rx_packets++; priv->stats.rx_bytes += pkt->datalen; netif_rx(skb); out: return; } /* * The poll implementation. */ static int snull_poll(struct net_device *dev, int *budget) { int npackets = 0, quota = min(dev->quota, *budget); struct sk_buff *skb; struct snull_priv *priv = netdev_priv(dev); struct snull_packet *pkt; while (npackets < quota && priv->rx_queue) { pkt = snull_dequeue_buf(dev); skb = dev_alloc_skb(pkt->datalen + 2); if (! skb) { if (printk_ratelimit()) printk(KERN_NOTICE "snull: packet dropped\n"); priv->stats.rx_dropped++; snull_release_buffer(pkt); continue; } skb_reserve(skb, 2); /* align IP on 16B boundary */ memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen); skb->dev = dev; skb->protocol = eth_type_trans(skb, dev); skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */ netif_receive_skb(skb); /* Maintain stats */ npackets++; priv->stats.rx_packets++; priv->stats.rx_bytes += pkt->datalen; snull_release_buffer(pkt); } /* If we processed all packets, we're done; tell the kernel and reenable ints */ *budget -= npackets; dev->quota -= npackets; if (! priv->rx_queue) { netif_rx_complete(dev); snull_rx_ints(dev, 1); return 0; } /* We couldn't process everything. */ return 1; } /* * The typical interrupt entry point */ static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs) { int statusword; struct snull_priv *priv; struct snull_packet *pkt = NULL; /* * As usual, check the "device" pointer to be sure it is * really interrupting. * Then assign "struct device *dev" */ struct net_device *dev = (struct net_device *)dev_id; /* ... and check with hw if it's really ours */ /* paranoid */ if (!dev) return; /* Lock the device */ priv = netdev_priv(dev); spin_lock(&priv;->lock); /* retrieve statusword: real netdevices use I/O instructions */ statusword = priv->status; priv->status = 0; if (statusword & SNULL_RX_INTR) { /* send it to snull_rx for handling */ pkt = priv->rx_queue; if (pkt) { priv->rx_queue = pkt->next; snull_rx(dev, pkt); } } if (statusword & SNULL_TX_INTR) { /* a transmission is over: free the skb */ priv->stats.tx_packets++; priv->stats.tx_bytes += priv->tx_packetlen; dev_kfree_skb(priv->skb); } /* Unlock the device and we are done */ spin_unlock(&priv;->lock); if (pkt) snull_release_buffer(pkt); /* Do this outside the lock! */ return; } /* * A NAPI interrupt handler. */ static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs) { int statusword; struct snull_priv *priv; /* * As usual, check the "device" pointer for shared handlers. * Then assign "struct device *dev" */ struct net_device *dev = (struct net_device *)dev_id; /* ... and check with hw if it's really ours */ /* paranoid */ if (!dev) return; /* Lock the device */ priv = netdev_priv(dev); spin_lock(&priv;->lock); /* retrieve statusword: real netdevices use I/O instructions */ statusword = priv->status; priv->status = 0; if (statusword & SNULL_RX_INTR) { snull_rx_ints(dev, 0); /* Disable further interrupts */ netif_rx_schedule(dev); } if (statusword & SNULL_TX_INTR) { /* a transmission is over: free the skb */ priv->stats.tx_packets++; priv->stats.tx_bytes += priv->tx_packetlen; dev_kfree_skb(priv->skb); } /* Unlock the device and we are done */ spin_unlock(&priv;->lock); return; } /* * Transmit a packet (low level interface) */ static void snull_hw_tx(char *buf, int len, struct net_device *dev) { /* * This function deals with hw details. This interface loops * back the packet to the other snull interface (if any). * In other words, this function implements the snull behaviour, * while all other procedures are rather device-independent */ struct iphdr *ih; struct net_device *dest; struct snull_priv *priv; u32 *saddr, *daddr; struct snull_packet *tx_buffer; /* I am paranoid. Ain't I? */ if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) { printk("snull: Hmm... packet too short (%i octets)\n", len); return; } if (0) { /* enable this conditional to look at the data */ int i; PDEBUG("len is %i\n" KERN_DEBUG "data:",len); for (i=14 ; i<len; i++) printk(" x",buf[i]&0xff;); printk("\n"); } /* * Ethhdr is 14 bytes, but the kernel arranges for iphdr * to be aligned (i.e., ethhdr is unaligned) */ ih = (struct iphdr *)(buf+sizeof(struct ethhdr)); saddr = &ih;->saddr; daddr = &ih;->daddr; ((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */ ((u8 *)daddr)[2] ^= 1; ih->check = 0; /* and rebuild the checksum (ip needs it) */ ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl); if (dev == snull_devs[0]) PDEBUGG("x:i --&gt; x:i\n", ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source), ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest)); else PDEBUGG("x:i <-- x:i\n", ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest), ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source)); /* * Ok, now the packet is ready for transmission: first simulate a * receive interrupt on the twin device, then a * transmission-done on the transmitting device */ dest = snull_devs[dev == snull_devs[0] ? 1 : 0]; priv = netdev_priv(dest); tx_buffer = snull_get_tx_buffer(dev); tx_buffer->datalen = len; memcpy(tx_buffer->data, buf, len); snull_enqueue_buf(dest, tx_buffer); if (priv->rx_int_enabled) { priv->status |= SNULL_RX_INTR; snull_interrupt(0, dest, NULL); } priv = netdev_priv(dev); priv->tx_packetlen = len; priv->tx_packetdata = buf; priv->status |= SNULL_TX_INTR; if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0) { /* Simulate a dropped transmit interrupt */ netif_stop_queue(dev); PDEBUG("Simulate lockup at %ld, txp %ld\n", jiffies, (unsigned long) priv->stats.tx_packets); } else snull_interrupt(0, dev, NULL); } /* * Transmit a packet (called by the kernel) */ int snull_tx(struct sk_buff *skb, struct net_device *dev) { int len; char *data, shortpkt[ETH_ZLEN]; struct snull_priv *priv = netdev_priv(dev); data = skb->data; len = skb->len; if (len < ETH_ZLEN) { memset(shortpkt, 0, ETH_ZLEN); memcpy(shortpkt, skb->data, skb->len); len = ETH_ZLEN; data = shortpkt; } dev->trans_start = jiffies; /* save the timestamp */ /* Remember the skb, so we can free it at interrupt time */ priv->skb = skb; /* actual deliver of data is device-specific, and not shown here */ snull_hw_tx(data, len, dev); return 0; /* Our simple device can not fail */ } /* * Deal with a transmit timeout. */ void snull_tx_timeout (struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies, jiffies - dev->trans_start); /* Simulate a transmission interrupt to get things moving */ priv->status = SNULL_TX_INTR; snull_interrupt(0, dev, NULL); priv->stats.tx_errors++; netif_wake_queue(dev); return; } /* * Ioctl commands */ int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) { PDEBUG("ioctl\n"); return 0; } /* * Return statistics to the caller */ struct net_device_stats *snull_stats(struct net_device *dev) { struct snull_priv *priv = netdev_priv(dev); return &priv;->stats; } /* * This function is called to fill up an eth header, since arp is not * available on the interface */ int snull_rebuild_header(struct sk_buff *skb) { struct ethhdr *eth = (struct ethhdr *) skb->data; struct net_device *dev = skb->dev; memcpy(eth->h_source, dev->dev_addr, dev->addr_len); memcpy(eth->h_dest, dev->dev_addr, dev->addr_len); eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */ return 0; } int snull_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, void *daddr, void *saddr, unsigned int len) { struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN); eth->h_proto = htons(type); memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len); memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len); eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */ return (dev->hard_header_len); } /* * The "change_mtu" method is usually not needed. * If you need it, it must be like this. */ int snull_change_mtu(struct net_device *dev, int new_mtu) { unsigned long flags; struct snull_priv *priv = netdev_priv(dev); spinlock_t *lock = &priv;->lock; /* check ranges */ if ((new_mtu < 68) || (new_mtu > 1500)) return -EINVAL; /* * Do anything you need, and the accept the value */ spin_lock_irqsave(lock, flags); dev->mtu = new_mtu; spin_unlock_irqrestore(lock, flags); return 0; /* success */ } /* * The init function (sometimes called probe). * It is invoked by register_netdev() */ void snull_init(struct net_device *dev) { struct snull_priv *priv; #if 0 /* * Make the usual checks: check_region(), probe irq, ... -ENODEV * should be returned if no device found. No resource should be * grabbed: this is done on open(). */ #endif /* * Then, assign other fields in dev, using ether_setup() and some * hand assignments */ ether_setup(dev); /* assign some of the fields */ dev->open = snull_open; dev->stop = snull_release; dev->set_config = snull_config; dev->hard_start_xmit = snull_tx; dev->do_ioctl = snull_ioctl; dev->get_stats = snull_stats; dev->change_mtu = snull_change_mtu; dev->rebuild_header = snull_rebuild_header; dev->hard_header = snull_header; dev->tx_timeout = snull_tx_timeout; dev->watchdog_timeo = timeout; if (use_napi) { dev->poll = snull_poll; dev->weight = 2; } /* keep the default flags, just add NOARP */ dev->flags |= IFF_NOARP; dev->features |= NETIF_F_NO_CSUM; dev->hard_header_cache = NULL; /* Disable caching */ /* * Then, initialize the priv field. This encloses the statistics * and a few private fields. */ priv = netdev_priv(dev); memset(priv, 0, sizeof(struct snull_priv)); spin_lock_init(&priv;->lock); snull_rx_ints(dev, 1); /* enable receive interrupts */ snull_setup_pool(dev); } /* * The devices */ struct net_device *snull_devs[2]; /* * Finally, the module stuff */ void snull_cleanup(void) { int i; for (i = 0; i < 2; i++) { if (snull_devs[i]) { unregister_netdev(snull_devs[i]); snull_teardown_pool(snull_devs[i]); free_netdev(snull_devs[i]); } } return; } int snull_init_module(void) { int result, i, ret = -ENOMEM; snull_interrupt = use_napi ? snull_napi_interrupt : snull_regular_interrupt; /* Allocate the devices */ snull_devs[0] = alloc_netdev(sizeof(struct snull_priv), "sn%d", snull_init); snull_devs[1] = alloc_netdev(sizeof(struct snull_priv), "sn%d", snull_init); if (snull_devs[0] == NULL || snull_devs[1] == NULL) goto out; ret = -ENODEV; for (i = 0; i < 2; i++) if ((result = register_netdev(snull_devs[i]))) printk("snull: error %i registering device \"%s\"\n", result, snull_devs[i]->name); else ret = 0; out: if (ret) snull_cleanup(); return ret; } module_init(snull_init_module); module_exit(snull_cleanup);
...展开详情收缩
2013-01-31 上传大小:14KB
分享
收藏 (1) 举报

评论 共12条

liu3332699 还不错的额资源,可以使用
2018-10-15
回复
a020490 资源太老了,需要修改不少东西
2016-11-25
回复
qq_23917751 很好的东西
2016-01-20
回复
kingtutu626 有编译错误,自己一点一点改吧
2015-12-15
回复
ferrylv 挺好,下一个研究一下,希望有用
2015-07-16
回复
hu0904 linux 本人Linux盲 过段时间再研究
2015-05-05
回复
ziyanc 研究中,有用.
2015-03-07
回复
qq_18822793 研究研究 东西不多
2014-12-27
回复
sz133 版本比较老,现在的版本不能直接编译,需要修改才行
2014-12-17
回复
lbhqfwj 恩,编译有错误,自己得改些东西。CFlag, linux/config, struct net_device等问题。
2014-08-04
回复
windows虚拟网卡驱动开发
网卡驱动之虚拟网卡驱动编写
linux驱动-虚拟网卡驱动
网卡驱动(1)——虚拟网卡
安装微软自带虚拟网卡驱动
开山之作--一个虚拟网卡的实现
virtnet虚拟网卡驱动

一个很好的虚拟网卡驱动,能够实现各种常用的虚拟网卡功能

立即下载
VMware 虚拟网卡驱动无法安装
虚拟网卡驱动VirtualMachineNetworkDriver

虚拟网卡驱动VirtualMachineNetworkDriver,不需要在安装虚拟机增加网卡

立即下载
实现虚拟网卡代码

linux下实现虚拟网卡技术,该代码使用c语言实现,已经测试可以使用。

立即下载
vmware虚拟win7网卡驱动

vmware虚拟机安装ghostwin7后会有连不上网的现象,原因是网卡没驱动,装vm tools也不行,下载本资源,在设备管理器中更新以太网控制器的驱动即可解决

立即下载
VC Mirror Driver显示虚拟驱动经典开发
十九、Linux驱动之虚拟网卡驱动
微软自带虚拟网卡破解版

由于部分破解软件的运行依赖于网卡MAC地址,但是修改后局域网之间就无法再通信。于是修改微软自带虚拟网卡驱动,去掉MAC设置限定,可以根据自己需要修改MAC,不必只能修改为02开头的MAC。

立即下载
虚拟机找不到网卡驱动的解决方法
网卡驱动6-做一个与外界交互的虚拟网卡5(代码及调试)
模拟网络驱动snull
网卡驱动源码

ax81880,dm9000以及cs8900a驱动源码

立即下载

热点文章

下载码下载
做任务获取下载码
取消 提交下载码
img

spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip

资源所需积分/C币 当前拥有积分 当前拥有C币
5 0 0
点击完成任务获取下载码
输入下载码
为了良好体验,不建议使用迅雷下载
img

虚拟网卡驱动源代码(原版)

会员到期时间: 剩余下载个数: 剩余C币: 剩余积分:0
为了良好体验,不建议使用迅雷下载
VIP下载
您今日下载次数已达上限(为了良好下载体验及使用,每位用户24小时之内最多可下载20个资源)

积分不足!

资源所需积分/C币 当前拥有积分
您可以选择
开通VIP
4000万
程序员的必选
600万
绿色安全资源
现在开通
立省522元
或者
购买C币兑换积分 C币抽奖
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 4 45
为了良好体验,不建议使用迅雷下载
确认下载
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 0 0
为了良好体验,不建议使用迅雷下载
VIP和C币套餐优惠
img

资源所需积分/C币 当前拥有积分 当前拥有C币
5 4 45
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
确认下载
下载
您还未下载过该资源
无法举报自己的资源

兑换成功

你当前的下载分为234开始下载资源
你还不是VIP会员
开通VIP会员权限,免积分下载
立即开通

你下载资源过于频繁,请输入验证码

您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:webmaster@csdn.net!

举报

  • 举报人:
  • 被举报人:
  • *类型:
    • *投诉人姓名:
    • *投诉人联系方式:
    • *版权证明:
  • *详细原因: