Chinaunix首页 | 论坛 | 博客
  • 博客访问: 542640
  • 博文数量: 101
  • 博客积分: 1889
  • 博客等级: 上尉
  • 技术积分: 906
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-14 16:22
文章分类

全部博文(101)

文章存档

2012年(11)

2011年(19)

2010年(59)

2009年(12)

我的朋友

分类: LINUX

2011-03-03 16:50:31

  1. /*
  2. snull.c -- the Simple Network Utility 
  3. *
  4. * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
  5. * Copyright (C) 2001 O'Reilly & Associates
  6. *
  7. * The source code in this file can be freely used, adapted,
  8. * and redistributed in source or binary form, so long as an
  9. * acknowledgment appears in derived source files. The citation
  10. * should list that the code comes from the book "Linux Device
  11. * Drivers" by Alessandro Rubini and Jonathan Corbet, published
  12. * by O'Reilly & Associates. No warranty is attached;
  13. * we cannot take responsibility for errors or fitness for use.
  14. *
  15. * $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $
  16. */

  17. #include <linux config.h>
  18. #include <linux module.h>
  19. #include <linux init.h>
  20. #include <linux moduleparam.h>
  21.    
  22. #include <linux sched.h>
  23. #include <linux kernel.h> /* printk() */
  24. #include <linux slab.h> /* kmalloc() */
  25. #include <linux errno.h> /* error codes */
  26. #include <linux types.h> /* size_t */
  27. #include <linux interrupt.h> /* mark_bh */
  28.    
  29. #include <linux in.h>
  30. #include <linux netdevice.h> /* struct device, and other headers */
  31. #include <linux etherdevice.h> /* eth_type_trans */
  32. #include <linux ip.h> /* struct iphdr */
  33. #include <linux tcp.h> /* struct tcphdr */
  34. #include <linux skbuff.h>
  35.    
  36. #include "snull.h"
  37.    
  38. #include <linux in6.h="">
  39. #include <asm checksum.h="">
  40.    
  41. MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
  42. MODULE_LICENSE("Dual BSD/GPL");
  43.    
  44. /*
  45. * Transmitter lockup simulation, normally disabled.
  46. */
  47. static int lockup = 0;
  48. module_param(lockup, int, 0);
  49.    
  50. static int timeout = SNULL_TIMEOUT;
  51. module_param(timeout, int, 0);
  52.    
  53. struct net_device *snull_devs[2];
  54.    
  55. /*
  56. * A structure representing an in-flight packet.
  57. */
  58. struct snull_packet {
  59.         struct snull_packet *next;
  60.         struct net_device *dev;
  61.         int datalen;
  62.         u8 data[ETH_DATA_LEN];
  63. };
  64.    
  65. int pool_size = 8;
  66. module_param(pool_size, int, 0);
  67.    
  68. /*
  69. * This structure is private to each device. It is used to pass packets in and out, so there is place for a packet 
  70. */
  71.    
  72. struct snull_priv {
  73.         struct net_device_stats stats;
  74.         int status;
  75.         struct snull_packet *ppool;
  76.         struct snull_packet *rx_queue; /* List of incoming packets */
  77.         int rx_int_enabled;
  78.         int tx_packetlen;
  79.         u8 *tx_packetdata;
  80.         struct sk_buff *skb;
  81.         spinlock_t lock;
  82. };
  83.    
  84. static void snull_tx_timeout(struct net_device *dev);
  85. static void (*snull_interrupt)(int, void *, struct pt_regs *);
  86.    
  87. void snull_setup_pool(struct net_device *dev)
  88. {
  89.         struct snull_priv *priv = netdev_priv(dev);
  90.         int i;
  91.         struct snull_packet *pkt;
  92.    
  93.         priv->ppool = NULL;
  94.         for (i = 0; i < pool_size; i++) {
  95.                 pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
  96.                 if (pkt == NULL) {
  97.                         printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
  98.                         return;
  99.                 }
  100.                 pkt->dev = dev;
  101.                 pkt->next = priv->ppool;
  102.                 priv->ppool = pkt;
  103.         }
  104. }
  105.    
  106. /*因为snull_setup_pool分配了pool_size个struct snull_packet,所以,驱动退出时,需要释放内存*/
  107. void snull_teardown_pool(struct net_device *dev)
  108. {
  109.         struct snull_priv *priv = netdev_priv(dev);
  110.         struct snull_packet *pkt;
  111.        
  112.         while ((pkt = priv->ppool)) {
  113.                 priv->ppool = pkt->next;
  114.                 kfree (pkt);
  115.                 /* FIXME - in-flight packets ? */
  116.         }
  117. }
  118.    
  119. /* 
  120. * 获取设备要传输的第一个包,传输队列首部相应的移动到下一个数据包.
  121. */
  122. struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
  123. {
  124.         struct snull_priv *priv = netdev_priv(dev);
  125.         unsigned long flags;
  126.         struct snull_packet *pkt;
  127.        
  128.         spin_lock_irqsave(&priv->lock, flags);//确保数据修改时,资源基本的独占
  129.         pkt = priv->ppool;
  130.         priv->ppool = pkt->next;
  131.         if (priv->ppool == NULL) {
  132.                 printk (KERN_INFO "Pool empty\n");
  133.                 netif_stop_queue(dev);
  134.         }
  135.         spin_unlock_irqrestore(&priv->lock, flags);//开锁
  136.         return pkt;
  137. }
  138.    
  139. /*将包缓存交还给缓存池*/
  140. void snull_release_buffer(struct snull_packet *pkt)
  141. {
  142.         unsigned long flags;
  143.         struct snull_priv *priv = netdev_priv(pkt->dev);
  144.            
  145.         spin_lock_irqsave(&priv->lock, flags);
  146.         pkt->next = priv->ppool;
  147.         priv->ppool = pkt;
  148.         spin_unlock_irqrestore(&priv->lock, flags);
  149.         if (netif_queue_stopped(pkt->dev) && pkt->next == NULL)
  150.                 netif_wake_queue(pkt->dev);
  151. }
  152.    
  153. /*将要传输的包加入到设备dev的传输队列首部,当然,这只是一个演示,这样一来,就变成先进先出了*/
  154. void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)
  155. {
  156.         unsigned long flags;
  157.         struct snull_priv *priv = netdev_priv(dev);
  158.    
  159.         spin_lock_irqsave(&priv->lock, flags);
  160.         pkt->next = priv->rx_queue; /* FIXME - misorders packets */
  161.         priv->rx_queue = pkt;
  162.         spin_unlock_irqrestore(&priv->lock, flags);
  163. }
  164.    
  165. /*取得传输队列中的第一个数据包*/
  166. struct snull_packet *snull_dequeue_buf(struct net_device *dev)
  167. {
  168.         struct snull_priv *priv = netdev_priv(dev);
  169.         struct snull_packet *pkt;
  170.         unsigned long flags;
  171.    
  172.         spin_lock_irqsave(&priv->lock, flags);
  173.         pkt = priv->rx_queue;
  174.         if (pkt != NULL)
  175.                 priv->rx_queue = pkt->next;
  176.         spin_unlock_irqrestore(&priv->lock, flags);
  177.         return pkt;
  178. }
  179.    
  180. /*
  181. * 打开/关闭接收中断.
  182. */
  183. static void snull_rx_ints(struct net_device *dev, int enable)
  184. {
  185.         struct snull_priv *priv = netdev_priv(dev);
  186.         priv->rx_int_enabled = enable;
  187. }
  188.        
  189. /*
  190. * 设备打开函数,是驱动最重要的函数之一,它应该注册所有的系统资源(I/O端口,IRQ、DMA等等),并对设备执行其他所需的设置。因为这个例子中,并没有真正的物理设备,所以,它最重要的工作就是启动传输队列。 
  191. */
  192.    
  193. int snull_open(struct net_device *dev)
  194. {
  195.         /* request_region(), request_irq(), .... (like fops->open) */
  196.    
  197.         /*
  198.          * Assign the hardware address of the board: use "\0SNULx", where
  199.          * x is 0 or 1. The first byte is '\0' to avoid being a multicast
  200.          * address (the first byte of multicast addrs is odd).
  201.          */
  202.         memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
  203.         if (dev == snull_devs[1])
  204.                 dev->dev_addr[ETH_ALEN-1]++; /* \0SNUL1 */
  205.         netif_start_queue(dev);
  206.         return 0;
  207. }
  208.    
  209. /*设备停止函数,这里的工作就是停止传输队列*/
  210. int snull_release(struct net_device *dev)
  211. {
  212.     /* release ports, irq and such -- like fops->close */
  213.    
  214.         netif_stop_queue(dev); /* can't transmit any more */
  215.         return 0;
  216. }
  217.    
  218. /*
  219. * 当用户调用ioctl时类型为SIOCSIFMAP时,如使用ifconfig,系统会调用驱动程序的set_config 方法。用户会传递一个ifmap结构包含需要设置的I/O地址、中断等参数。 
  220. */
  221. int snull_config(struct net_device *dev, struct ifmap *map)
  222. {
  223.         if (dev->flags & IFF_UP) /* 不能设置一个正在运行状态的设备 */
  224.                 return -EBUSY;
  225.    
  226.         /* 这个例子中,不允许改变 I/O 地址*/
  227.         if (map->base_addr != dev->base_addr) {
  228.                 printk(KERN_WARNING "snull: Can't change I/O address\n");
  229.                 return -EOPNOTSUPP;
  230.         }
  231.    
  232.         /* 允许改变 IRQ */
  233.         if (map->irq != dev->irq) {
  234.                 dev->irq = map->irq;
  235.                 /* request_irq() is delayed to open-time */
  236.         }
  237.    
  238.         /* ignore other fields */
  239.         return 0;
  240. }
  241.    
  242. /*
  243. * 接收数据包函数
  244. * 它被“接收中断”调用,重组数据包,并调用函数netif_rx进一步处理。我们从“硬件”中收到的包,是用struct snull_packet来描述的,但是内核中描述一个包,是使用struct sk_buff(简称skb),所以,这里要完成一个把硬件接收的包拷贝至内核缓存skb的一个组包过程(PS:不知在接收之前直接分配一个skb,省去这一步,会如何提高性能,没有研究过,见笑了^o^)。 
  245. */
  246. void snull_rx(struct net_device *dev, struct snull_packet *pkt)
  247. {
  248.         struct sk_buff *skb;
  249.         struct snull_priv *priv = netdev_priv(dev);
  250.    
  251.         /*
  252.          * 分配skb缓存
  253.          */
  254.         skb = dev_alloc_skb(pkt->datalen + 2);
  255.         if (!skb) { /*分配失败*/
  256.                 if (printk_ratelimit())
  257.                         printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n");
  258.                 priv->stats.rx_dropped++;
  259.                 goto out;
  260.         }
  261. /*
  262. * skb_reserver用来增加skb的date和tail,因为以太网头部为14字节长,再补上两个字节就刚好16字 节边界对齐,所以大多数以太网设备都会在数据包之前保留2个字节。 
  263. */
  264.         skb_reserve(skb, 2); /* align IP on 16B boundary */
  265.         memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
  266.    
  267.         skb->dev = dev; 
  268. /*skb与接收设备就关联起来了,它在网络栈中会被广泛使用,没道理不知道数据是谁接收来的吧*/
  269.         skb->protocol = eth_type_trans(skb, dev); 
  270.         /*获取上层协议类型,这样,上层处理函数才知道如何进一步处理*/
  271.         skb->ip_summed = CHECKSUM_UNNECESSARY; 
  272.         /* 设置较验标志:不进行任何校验,作者的驱动的收发都在内存中进行,是没有必要进行校验*/
  273.            
  274.         /*累加计数器*/
  275.         priv->stats.rx_packets++;
  276.         priv->stats.rx_bytes += pkt->datalen;
  277.            
  278.         /*
  279.          * 把数据包交给上层。netif_rx会逐步调用netif_rx_schedule -->__netif_rx_schedule,__netif_rx_schedule函数会调用__raise_softirq_irqoff(NET_RX_SOFTIRQ);触发网络接收数据包的软中 断函数net_rx_action。 
  280.          * 软中断是Linux内核完成中断推后处理工作的一种机制,请参考《Linux内核设计与实现》第二版。
  281.          * 唯一需要提及的是,这个软中断函数net_rx_action是在网络系统初始化的时候(linux/net/core/dev.c):注册的
  282.          * open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
  283.          */
  284.         netif_rx(skb);
  285.   out:
  286.         return;
  287. }
  288.             
  289. /*
  290. * 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发
  291. * 对于典型的网络设备,一般会在open函数中注册中断函数,这样,当网络设备产生中断时,如接收到数据包时,中断函数将会被调用。不过在这个例子中,因为没有真正的物理设备,所以,不存在注册中断,也就不存在触发,对于接收和发送,它都是在自己设计的函数的特定位置被调用。 
  292. * 这个中断函数设计得很简单,就是取得设备的状态,判断是“接收”还是“发送”的中断,以调用相应的处理函数。
  293. * 而对于“是哪个设备产生的中断”这个问题,则由调用它的函数通过第二个参数的赋值来决定。
  294. */
  295. static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  296. {
  297.         int statusword;
  298.         struct snull_priv *priv;
  299.         struct snull_packet *pkt = NULL;
  300.         /*
  301.          * 通常,需要检查 "device" 指针以确保这个中断是发送给自己的。
  302.          * 然后为 "struct device *dev" 赋
  303.          */
  304.         struct net_device *dev = (struct net_device *)dev_id;
  305.    
  306.         /* paranoid */
  307.         if (!dev)
  308.                 return;
  309.    
  310.         /* 锁住设备 */
  311.         priv = netdev_priv(dev);
  312.         spin_lock(&priv->lock);
  313.    
  314.         /* 取得设备状态指字,对于真实设备,使用I/O指令,比如:int txsr = inb(TX_STATUS); */
  315.         statusword = priv->status;
  316.         priv->status = 0;
  317.         if (statusword & SNULL_RX_INTR) { /*如果是接收数据包的中断*/
  318.                 /* send it to snull_rx for handling */
  319.                 pkt = priv->rx_queue;
  320.                 if (pkt) {
  321.                         priv->rx_queue = pkt->next;
  322.                         snull_rx(dev, pkt);
  323.                 }
  324.         }
  325.         if (statusword & SNULL_TX_INTR) { /*如果是发送数据包的中断*/
  326.                 /* a transmission is over: free the skb */
  327.                 priv->stats.tx_packets++;
  328.                 priv->stats.tx_bytes += priv->tx_packetlen;
  329.                 dev_kfree_skb(priv->skb);
  330.         }
  331.    
  332.         /* 释放锁 */
  333.         spin_unlock(&priv->lock);
  334.            
  335.         /*释放缓冲区*/
  336.         if (pkt) snull_release_buffer(pkt); /* Do this outside the lock! */
  337.         return;
  338. }
  339.    
  340.    
  341. /*
  342. * Transmit a packet (low level interface)
  343. */
  344. static void snull_hw_tx(char *buf, int len, struct net_device *dev)
  345. {
  346.         /*
  347.          * This function deals with hw details. This interface loops
  348.          * back the packet to the other snull interface (if any).
  349.          * In other words, this function implements the snull behaviour,
  350.          * while all other procedures are rather device-independent
  351.          */
  352.         struct iphdr *ih;
  353.         struct net_device *dest;
  354.         struct snull_priv *priv;
  355.         u32 *saddr, *daddr;
  356.         struct snull_packet *tx_buffer;
  357.        
  358.         /* I am paranoid. Ain't I? */
  359.         if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) {
  360.                 printk("snull: Hmm... packet too short (%i octets)\n",
  361.                                 len);
  362.                 return;
  363.         }
  364.    
  365.         if (0) { /* enable this conditional to look at the data */
  366.                 int i;
  367.                 PDEBUG("len is %i\n" KERN_DEBUG "data:",len);
  368.                 for (i=14 ; i<len; i++)
  369. printk("="" %02x",buf[i]&0xff);
  370. printk("\n");="" 
  371. }
  372. /* 取得来源ip和目的ip地址 */
  373. ih (struct iphdr *)(buf+sizeof(struct ethhdr)); 
  374. saddr &ih->saddr;
  375.         daddr = &ih->daddr;
  376.            
  377. /*
  378. 这里做了三个调换,以实现欺骗:来源地址第三octet 0<->1,目的地址第三octet 0<->1,设备snX编辑0<->1,这样做的理由是:
  379. sn0(发):192.168.0.88 --> 192.168.0.99
  380. 做了调换后,就变成:
  381. sn1(收):192.168.1.88 --> 192.168.1.99 
  382. 因为sn1的地址就是192.168.1.99,所以,它收到这个包后,会回应: 
  383.          sn1(发):192.168.1.99 --> 192.168.1.88,
  384. 同样地,做了这样的调换后,就变成:
  385.          sn0(收):192.168.0.99 --> 192.168.0.88
  386. 这样,sn0就会收到这个包,实现了ping的请求与应答,^o^
  387. */
  388.         ((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
  389.         ((u8 *)daddr)[2] ^= 1;
  390.    
  391.         /*重新计算较验和*/
  392.         ih->check = 0; /* and rebuild the checksum (ip needs it) */
  393.         ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
  394.    
  395.         /*输出调试信息*/
  396.         if (dev == snull_devs[0])
  397.                 PDEBUGG("%08x:%05i --> %08x:%05i\n",
  398.                       ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
  399.                       ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
  400.         else
  401.                 PDEBUGG("%08x:%05i <-- %08x:%05i\n",
  402.                       ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
  403.                       ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));
  404.    
  405.         /*调换设备编号,即dest指向接收设备,原因如前所述*/
  406.         dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
  407.            
  408.         /*将发送的数据添加到接收设备的接收队列中*/
  409.         priv = netdev_priv(dest);
  410.         tx_buffer = snull_get_tx_buffer(dev);
  411.         tx_buffer->datalen = len;
  412.         memcpy(tx_buffer->data, buf, len);
  413.         snull_enqueue_buf(dest, tx_buffer);
  414.            
  415.         /*
  416.          * 如果设备接收标志打开,就调用中断函数把数据包发送给目标设备——即触发目的设备的接收中断,这样
  417.          * 中断程序就会自接收设备的接收队列中接收数据包,并交给上层网络栈处理
  418.         */
  419.         if (priv->rx_int_enabled) {
  420.                 priv->status |= SNULL_RX_INTR;
  421.                 snull_interrupt(0, dest, NULL);
  422.         }
  423.    
  424.         /*发送完成后,触发“发送完成”中断*/
  425.         priv = netdev_priv(dev);
  426.         priv->tx_packetlen = len;
  427.         priv->tx_packetdata = buf;
  428.         priv->status |= SNULL_TX_INTR;
  429.            
  430.         /*
  431.         如果insmod驱动的时候,指定了模拟硬件锁的lockup=n,则在会传输n个数据包后,模拟一次硬件锁住的情况,这是通过调用netif_stop_queue函数来停止传输队列,标记“设备不能再传输数据包”实现的,它将在传输的超时函数中,调用netif_wake_queue函数来重新启动传输队例,同时超时函数中会再次调用“接收中断”,这样stats.tx_packets累加,又可以重新传输新的数据包了(参接收中断和超时处理函数的实现)
  432.          */
  433.         if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0) {
  434.                 /* Simulate a dropped transmit interrupt */
  435.                 netif_stop_queue(dev); /*停止数据包的传输*/
  436.                 PDEBUG("Simulate lockup at %ld, txp %ld\n", jiffies,
  437.                                 (unsigned long) priv->stats.tx_packets);
  438.         }
  439.         else
  440.         /*发送完成后,触发中断,中断函数发现发送完成,就累加计数器,释放skb缓存*/
  441.                 snull_interrupt(0, dev, NULL);
  442.                    
  443.         /*
  444.         看到这里,我们可以看到,这个发送函数其实并没有把数据包通过I/O指令发送给硬件,而仅仅是做了一个地址/设备的调换,并把数据包加入到接收设备的队例当中。 
  445.          */
  446. }
  447.    
  448. /*
  449. 数据包传输函数,Linux网络堆栈,在发送数据包时,会调用驱动程序的hard_start_transmit函数,在设备初始化的时候,这个函数指针指向了snull_tx。 
  450. */
  451. int snull_tx(struct sk_buff *skb, struct net_device *dev)
  452. {
  453.         int len;
  454.         char *data, shortpkt[ETH_ZLEN];
  455.         struct snull_priv *priv = netdev_priv(dev);
  456.            
  457.         data = skb->data;
  458.         len = skb->len;
  459.         if (len < ETH_ZLEN) { /*处理短帧的情况,如果小于以太帧最小长度,不足位全部补0*/
  460.                 memset(shortpkt, 0, ETH_ZLEN);
  461.                 memcpy(shortpkt, skb->data, skb->len);
  462.                 len = ETH_ZLEN;
  463.                 data = shortpkt;
  464.         }
  465.         dev->trans_start = jiffies; /* 保存时间戳 */
  466.    
  467.         /*
  468.         因为“发送”完成后,需要释放skb,所以,先要保存它 ,释放都是在网卡发送完成,产生中断,而中断函 数收到网卡的发送完成的中断信号后释放 
  469.          */
  470.         priv->skb = skb;
  471.    
  472.         /*
  473.         让硬件把数据包发送出去,对于物理设备,就是一个读网卡寄存器的过程,不过,这里,只是一些为了实现演示功能的虚假的欺骗函数,比如操作源/目的IP,然后调用接收函数(所以,接收时不用调用中断) 
  474.          */
  475.         snull_hw_tx(data, len, dev);
  476.    
  477.         return 0; /* Our simple device can not fail */
  478. }
  479.    
  480. /*
  481. * 传输超时处理函数
  482. 比如在传输数据时,由于缓冲已满,需要关闭传输队列,但是驱动程序是不能丢弃数据包,它将在“超时”的时候触发超时处理函数,这个函数将发送一个“传输中断”,以填补丢失的中断,并重新启动传输队例子 
  483. */
  484. void snull_tx_timeout (struct net_device *dev)
  485. {
  486.         struct snull_priv *priv = netdev_priv(dev);
  487.    
  488.         PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
  489.                         jiffies - dev->trans_start);
  490.         /* Simulate a transmission interrupt to get things moving */
  491.         priv->status = SNULL_TX_INTR;
  492.         snull_interrupt(0, dev, NULL);
  493.         priv->stats.tx_errors++;
  494.         netif_wake_queue(dev);
  495.         return;
  496. }
  497.    
  498. /* 
  499. * Ioctl 命令
  500. */
  501. int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  502. {
  503.         PDEBUG("ioctl\n");
  504.         return 0;
  505. }
  506.    
  507. /*
  508. * 获取设备的状态
  509. */
  510. struct net_device_stats *snull_stats(struct net_device *dev)
  511. {
  512.         struct snull_priv *priv = netdev_priv(dev);
  513.         return &priv->stats;
  514. }
  515.    
  516. /*
  517. 有些网络有硬件地址(比如Ethernet),并且在发送硬件帧时需要知道目的硬件 地址会进行ARP请求/应答,以完成MAC地址解析,需要做arp请求的设备在发送之前会调用驱动程序的rebuild_header函数。需要做arp的的设备在发送之前会调用驱动程序的rebuild_header方法。调用的主要参数包括指向硬件帧头的指针,协议层地址。如果驱动程序能够解 析硬件地址,就返回1,如果不能,返回0。 
  518. 当然,作者实现的演示设备中,不支持这个过程。
  519. */
  520. int snull_rebuild_header(struct sk_buff *skb)
  521. {
  522.         struct ethhdr *eth = (struct ethhdr *) skb->data;
  523.         struct net_device *dev = skb->dev;
  524.        
  525.         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
  526.         memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
  527.         eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
  528.         return 0;
  529. }
  530.    
  531. /*
  532. * 为上层协议创建一个二层的以太网首部。
  533. * 事实上,如果一开始调用alloc_etherdev分配以太设备,它会调用ether_setup进行初始化,初始化函数会设置:
  534. * dev->hard_header = eth_header;
  535. * dev->rebuild_header = eth_rebuild_header;
  536. * 驱动开发人员并不需要自己来实现这个函数,作者这样做,只是为了展示细节。
  537. */
  538.    
  539. int snull_header(struct sk_buff *skb, struct net_device *dev,
  540.                 unsigned short type, void *daddr, void *saddr,
  541.                 unsigned int len)
  542. {
  543.         /*获取以太头指针*/
  544.         struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);
  545.    
  546.         eth->h_proto = htons(type); /*填写协议*/
  547.            
  548.         /*填写来源/目的MAC地址,如果地址为空,则用设备自己的地址代替之*/
  549.         memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
  550.         memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
  551.            
  552.         /*
  553.          * 将第一个octet设为0,主要是为了可以在不支持组播链路,如ppp链路上运行
  554.          * PS:作者这样做,仅仅是演示在PC机上的实现,事实上,直接使用ETH_ALEN-1是
  555.          * 不适合“大头”机器的。
  556.          */
  557.         eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
  558.         return (dev->hard_header_len);
  559. }
  560.    
  561. /*
  562. * 改变设备MTU值.
  563. */
  564. int snull_change_mtu(struct net_device *dev, int new_mtu)
  565. {
  566.         unsigned long flags;
  567.         struct snull_priv *priv = netdev_priv(dev);
  568.         spinlock_t *lock = &priv->lock;
  569.        
  570.         /* check ranges */
  571.         if ((new_mtu < 68) || (new_mtu > 1500))
  572.                 return -EINVAL;
  573.         /*
  574.          * Do anything you need, and the accept the value
  575.          */
  576.         spin_lock_irqsave(lock, flags);
  577.         dev->mtu = new_mtu;
  578.         spin_unlock_irqrestore(lock, flags);
  579.         return 0; /* success */
  580. }
  581.    
  582. /*
  583. * 设备初始化函数,它必须在 register_netdev 函数被调用之前调用
  584. */
  585. void snull_init(struct net_device *dev)
  586. {
  587.         /*设备的“私有”结构,保存一些设备一些“私有数据”*/
  588.         struct snull_priv *priv;
  589. #if 0
  590.             /*
  591.          * Make the usual checks: check_region(), probe irq, ... -ENODEV
  592.          * should be returned if no device found. No resource should be
  593.          * grabbed: this is done on open().
  594.          */
  595. #endif
  596.          /* 
  597.          * 初始化以太网设备的一些共用的成员
  598.          */
  599.         ether_setup(dev); /* assign some of the fields */
  600.    
  601.         /*设置设备的许多成员函数指针*/
  602.         dev->open = snull_open;
  603.         dev->stop = snull_release;
  604.         dev->set_config = snull_config;
  605.         dev->hard_start_xmit = snull_tx;
  606.         dev->do_ioctl = snull_ioctl;
  607.         dev->get_stats = snull_stats;
  608.         dev->change_mtu = snull_change_mtu;
  609.         dev->rebuild_header = snull_rebuild_header;
  610.         dev->hard_header = snull_header;
  611.         dev->tx_timeout = snull_tx_timeout;
  612.         dev->watchdog_timeo = timeout;
  613.            
  614.         /* keep the default flags, just add NOARP */
  615.         dev->flags |= IFF_NOARP;
  616.         dev->features |= NETIF_F_NO_CSUM;
  617.         dev->hard_header_cache = NULL; /* Disable caching */
  618.    
  619.         /*
  620.          * 取得私有数据区,并初始化它.
  621.          */
  622.         priv = netdev_priv(dev);
  623.         memset(priv, 0, sizeof(struct snull_priv));
  624.         spin_lock_init(&priv->lock);
  625.         snull_rx_ints(dev, 1); /* 打开接收中断标志 */
  626.         snull_setup_pool(dev); /*设置使用NAPI时的接收缓冲池*/
  627. }
  628.    
  629. /*
  630. * The devices
  631. */
  632.    
  633. /*
  634. * Finally, the module stuff
  635. */
  636.    
  637. void snull_cleanup(void)
  638. {
  639.         int i;
  640.        
  641.         for (i = 0; i < 2; i++) {
  642.                 if (snull_devs[i]) {
  643.                         unregister_netdev(snull_devs[i]);
  644.                         snull_teardown_pool(snull_devs[i]);
  645.                         free_netdev(snull_devs[i]);
  646.                 }
  647.         }
  648.         return;
  649. }
  650.    
  651. /*模块初始化,初始化的只有一个工作:分配一个设备结构并注册它*/
  652. int snull_init_module(void)
  653. {
  654.         int result, i, ret = -ENOMEM;
  655.    
  656.         /*中断函数指针,因是否使用NAPI而指向不同的中断函数*/
  657.         snull_interrupt = snull_regular_interrupt;
  658.    
  659.      /*
  660.      * 分配两个设备,网络设备都是用struct net_device来描述,alloc_netdev分配设备,第三个参数是对struct net_device结构成员进行初始化的函数,对于以太网来说,可以把alloc_netdev/snull_init两个函数变为一个,alloc_etherdev,它会自动调用以太网的初始化函数ether_setup,因为以太网的初始化函数工作都是近乎一样的 */ 
  661.         snull_devs[0] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
  662.                         snull_init);
  663.         snull_devs[1] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
  664.                         snull_init);
  665.         /*分配失败*/
  666.         if (snull_devs[0] == NULL || snull_devs[1] == NULL)
  667.                 goto out;
  668.    
  669.         ret = -ENODEV;
  670.         /*向内核注册网络设备,这样,设备就可以被使用了*/
  671.         for (i = 0; i < 2; i++)
  672.                 if ((result = register_netdev(snull_devs[i])))
  673.                         printk("snull: error %i registering device \"%s\"\n",
  674.                                         result, snull_devs[i]->name);
  675.                 else
  676.                         ret = 0;
  677.    out:
  678.         if (ret)
  679.                 snull_cleanup();
  680.         return ret;
  681. }
  682.    
  683. module_init(snull_init_module);
  684. module_exit(snull_cleanup);
阅读(4394) | 评论(0) | 转发(1) |
0

上一篇:LDD3---网络驱动程序

下一篇:接地技术总结

给主人留下些什么吧!~~