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); printk("snull_release_buffer
"); }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 " SNULx", where * x is 0 or 1. The first byte is " " to avoid being a multicast * address (the first byte of multicast addrs is odd). */ /* [cgw]: 分配一个假的硬件地址,真正的网卡的时候,这个地址是从网卡读出来的 */ memcpy(dev->dev_addr, " SNUL0", ETH_ALEN); /* [cgw]: 因为注册了两个虚拟网卡,第二个虚拟网卡的地址跟第一个的地址必须不一样 * 即这两个网卡地址分别为 SNUL0和 SNUL1 */ if (dev == snull_devs[1]) dev->dev_addr[ETH_ALEN-1]++; /* SNUL1 */ /* [cgw]: 启动发送队列 */ netif_start_queue(dev); printk("snull_open
");
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 */
printk("snull_release
");
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
"); return -EOPNOTSUPP; } /* Allow changing the IRQ */ if (map->irq != dev->irq) { dev->irq = map->irq; /* request_irq() is delayed to open-time */ } printk("snull_config
"); /* 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 */ /* [cgw]: 为接收包分配一个skb */ skb = dev_alloc_skb(pkt->datalen + 2); if (!skb) { if (printk_ratelimit()) printk(KERN_NOTICE "snull rx: low on mem - packet dropped
"); priv->stats.rx_dropped++; goto out; } /* [cgw]: 16字节对齐,即IP首部前是网卡硬件地址首部,其占14字节,需要为其增加2 * 个字节 */ skb_reserve(skb, 2); /* align IP on 16B boundary */ /* [cgw]: 开辟一个数据缓冲区用于存放接收数据 */ memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen); /* Write metadata, and then pass to the receive level */ skb->dev = dev; if (skb->dev == snull_devs[0]) { printk("skb->dev is snull_devs[0]
"); } else { printk("skb->dev is snull_devs[1]
"); } /* [cgw]: 确定包的协议ID */ skb->protocol = eth_type_trans(skb, dev); printk("skb->protocol = %d
", skb->protocol);
/* 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) { //napi_complete(napi); netif_rx_complete(dev); snull_rx_ints(dev, 1); return 0; } /* We couldn"t process everything. */ //return npackets; 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); /* [cgw]: 判断产生的是什么类型的中断,接收还是中断 */ /* retrieve statusword: real netdevices use I/O instructions */ statusword = priv->status;
printk("priv->status = %d
", priv->status);
priv->status = 0; /* [cgw]: 接收完成中断 */ if (statusword & SNULL_RX_INTR) { /* send it to snull_rx for handling */ pkt = priv->rx_queue; if (pkt) { priv->rx_queue = pkt->next; /* [cgw]: 网卡接收到数据,上报给应用层 */ snull_rx(dev, pkt); } } /* [cgw]: 发送完成中断 */ if (statusword & SNULL_TX_INTR) { /* [cgw]: 统计已发送的包数和总字节数,并释放这个包的内存 */ /* 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! */ printk("snull_regular_interrupt
"); 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 */ printk("snull_napi_interrupt
"); /* 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 */ //napi_schedule(&priv->napi); 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)
", len); return; } /* [cgw]: 打印上层应用(即ping)要发的这个包的内容 * 这个包的格式为: * 14字节以太网首部+20字节IP地址首部+20字节TCP地址首部+n字节数据 */
if (1) { /* enable this conditional to look at the data */ int i; PDEBUG("len is %i
" KERN_DEBUG "data:",len); /* [cgw]: 14字节以太网首部 */ for (i=0 ; i<14; i++) printk(" %02x",buf[i]&0xff); printk("
"); /* [cgw]: 20字节IP地址首部 */ for (i=14 ; i<34; i++) printk(" %02x",buf[i]&0xff); printk("
"); /* [cgw]: 20字节TCP地址首部 */ for (i=34 ; i<54; i++) printk(" %02x",buf[i]&0xff); printk("
"); /* [cgw]: n字节数据 */ for (i=54 ; i<len; i++) printk(" %02x",buf[i]&0xff); printk("
"); } /* * Ethhdr is 14 bytes, but the kernel arranges for iphdr * to be aligned (i.e., ethhdr is unaligned) */ /* [cgw]: 提取本地和目标IP地址 */ ih = (struct iphdr *)(buf+sizeof(struct ethhdr)); saddr = &ih->saddr; daddr = &ih->daddr;
printk("ih->protocol = %d is buf[23]
", ih->protocol); printk("saddr = %d.%d.%d.%d
", *((u8 *)saddr + 0), *((u8 *)saddr + 1), *((u8 *)saddr + 2), *((u8 *)saddr + 3)); printk("daddr = %d.%d.%d.%d
", *((u8 *)daddr + 0), *((u8 *)daddr + 1), *((u8 *)daddr + 2), *((u8 *)daddr + 3)); /* [cgw]: 改变本地和目标IP地址的第三个字节的最低位,即原来是0,则改为1,原来是1,则改为0 */ ((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */ ((u8 *)daddr)[2] ^= 1; /* [cgw]: 从新计算校验,因为IP已改变 */ ih->check = 0; /* and rebuild the checksum (ip needs it) */ ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl); /* [cgw]: 打印更改后的IP地址,和TCP地址, */ if (dev == snull_devs[0]) //PDEBUGG("%08x:%05i --> %08x:%05i
", printk("%08x:%05i --> %08x:%05i
", ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source), ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest)); else //PDEBUGG("%08x:%05i <-- %08x:%05i
", printk("%08x:%05i <-- %08x:%05i
", 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 */ /* [cgw]: 获得目的网卡设备 */ dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
if (len < ETH_ZLEN) { memset(shortpkt, 0, ETH_ZLEN); memcpy(shortpkt, skb->data, skb->len); len = ETH_ZLEN; data = shortpkt; } /* [cgw]: 开始计算时间截,用于处理发送超时 */ dev->trans_start = jiffies; /* save the timestamp */ /* Remember the skb, so we can free it at interrupt time */ priv->skb = skb;
printk("snull_tx
"); /* actual deliver of data is device-specific, and not shown here */ /* [cgw]: 模拟把数据包写入硬件,通过硬件发送出去,但实际上不是 */ snull_hw_tx(data, len, dev); //printk("snull_tx
"); 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
", 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); printk("snull_tx_timeout
");
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 */ printk("snull_rebuild_header
");
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 */ }#if 0 static const struct header_ops snull_header_ops = { .create = snull_header, .rebuild = snull_rebuild_header };static const struct net_device_ops snull_netdev_ops = { .ndo_open = snull_open, .ndo_stop = snull_release, .ndo_start_xmit = snull_tx, .ndo_do_ioctl = snull_ioctl, .ndo_set_config = snull_config, .ndo_get_stats = snull_stats, .ndo_change_mtu = snull_change_mtu, .ndo_tx_timeout = snull_tx_timeout }; #endif/* * 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->watchdog_timeo = timeout;
/* keep the default flags, just add NOARP */ dev->flags |= IFF_NOARP; dev->features |= NETIF_F_HW_CSUM; dev->hard_header_cache = NULL; /* * Then, initialize the priv field. This encloses the statistics * and a few private fields. */ priv = netdev_priv(dev); #if 0 if (use_napi) { netif_napi_add(dev, &priv->napi, snull_poll,2); } #else if (use_napi) { dev->poll = snull_poll; dev->weight = 2; } #endif memset(priv, 0, sizeof(struct snull_priv)); spin_lock_init(&priv->lock); snull_rx_ints(dev, 1); /* enable receive interrupts */ snull_setup_pool(dev); printk("snull_init
"); }/* * 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"
", result, snull_devs[i]->name); else ret = 0; printk("snull_init_module
");