⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snull.c

📁 linux 驱动程序编程第3版
💻 C
📖 第 1 页 / 共 2 页
字号:
	}	/* 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(" %02x",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("%08x:%05i --> %08x:%05i\n",				ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),				ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));	else		PDEBUGG("%08x:%05i <-- %08x:%05i\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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -