LDD3中网络驱动源码注释

来源:互联网 发布:dlp数据泄露防护 编辑:程序博客网 时间:2024/05/17 01:51

LDD3中网络驱动源码注释[复制链接]
00

独孤九贱

富足长乐

Rank: 5Rank: 5

帖子
2415
主题
441
精华
36
可用积分
6081
专家积分
0
在线时间
760 小时
注册时间
2003-08-12
最后登录
2014-09-11
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
跳转到指定楼层
1楼[收藏(0)][报告][回复][引用]
发表于 2006-10-22 13:53:01|只看该作者|倒序浏览
《Linux设备驱动程序》第三版,第十七章,网络驱动源码示例的注释。
事实上,作者已经写得很明白了,再注释是显得苍白的,不过近来有朋友有问,偶把它注释出来,以作回答,希望能做为补充和旁注之用,水平有限,不对之处还要请各位一一指正!!
  1. #include <linux/config.h>
  2. #include <linux/module.h>
  3. #include <linux/init.h>
  4. #include <linux/moduleparam.h>

  5. #include <linux/sched.h>
  6. #include <linux/kernel.h> /* printk() */
  7. #include <linux/slab.h> /* kmalloc() */
  8. #include <linux/errno.h>  /* error codes */
  9. #include <linux/types.h>  /* size_t */
  10. #include <linux/interrupt.h> /* mark_bh */

  11. #include <linux/in.h>
  12. #include <linux/netdevice.h>   /* struct device, and other headers */
  13. #include <linux/etherdevice.h> /* eth_type_trans */
  14. #include <linux/ip.h>          /* struct iphdr */
  15. #include <linux/tcp.h>         /* struct tcphdr */
  16. #include <linux/skbuff.h>

  17. #include "snull.h"

  18. #include <linux/in6.h>
  19. #include <asm/checksum.h>

  20. MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
  21. MODULE_LICENSE("Dual BSD/GPL");


  22. /*
  23. * Transmitter lockup simulation, normally disabled.
  24. */
  25. static int lockup = 0;
  26. module_param(lockup, int, 0);

  27. static int timeout = SNULL_TIMEOUT;
  28. module_param(timeout, int, 0);

  29. /*
  30. * Do we run in NAPI mode?
  31. */
  32. static int use_napi = 0;
  33. module_param(use_napi, int, 0);


  34. /*
  35. * A structure representing an in-flight packet.
  36. */
  37. struct snull_packet {
  38.         struct snull_packet *next;
  39.         struct net_device *dev;
  40.         int        datalen;
  41.         u8 data[ETH_DATA_LEN];
  42. };

  43. int pool_size = 8;
  44. module_param(pool_size, int, 0);

  45. /*
  46. * This structure is private to each device. It is used to pass
  47. * packets in and out, so there is place for a packet
  48. */

  49. struct snull_priv {
  50.         struct net_device_stats stats;
  51.         int status;
  52.         struct snull_packet *ppool;
  53.         struct snull_packet *rx_queue;  /* List of incoming packets */
  54.         int rx_int_enabled;
  55.         int tx_packetlen;
  56.         u8 *tx_packetdata;
  57.         struct sk_buff *skb;
  58.         spinlock_t lock;
  59. };

  60. static void snull_tx_timeout(struct net_device *dev);
  61. static void (*snull_interrupt)(int, void *, struct pt_regs *);

  62. /*
  63. * 设置设备的包缓冲池.
  64. * 当需要使用NAPI,而非中断处理的时候,设备需要能够保存多个数据包的能力,这个保存所需的缓存,
  65. * 或者在板卡上,或者在内核的DMA环中。
  66. * 作者这里的演示程序,根据pool_size的大小,分配pool_size个大小为struct snull_packet的缓冲区,
  67. * 这个缓冲池用链表组织,“私有数据”结构的ppool成员指针指向链表首部。
  68. */
  69. void snull_setup_pool(struct net_device *dev)
  70. {
  71.         struct snull_priv *priv = netdev_priv(dev);
  72.         int i;
  73.         struct snull_packet *pkt;

  74.         priv->ppool = NULL;
  75.         for (i = 0; i < pool_size; i++) {
  76.                 pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
  77.                 if (pkt == NULL) {
  78.                         printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
  79.                         return;
  80.                 }
  81.                 pkt->dev = dev;
  82.                 pkt->next = priv->ppool;
  83.                 priv->ppool = pkt;
  84.         }
  85. }

  86. /*因为snull_setup_pool分配了pool_size个struct snull_packet,所以,驱动退出时,需要释放内存*/
  87. void snull_teardown_pool(struct net_device *dev)
  88. {
  89.         struct snull_priv *priv = netdev_priv(dev);
  90.         struct snull_packet *pkt;
  91.    
  92.         while ((pkt = priv->ppool)) {
  93.                 priv->ppool = pkt->next;
  94.                 kfree (pkt);
  95.                 /* FIXME - in-flight packets ? */
  96.         }
  97. }   

  98. /*
  99. * 获取设备要传输的第一个包,传输队列首部相应的移动到下一个数据包.
  100. */
  101. struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
  102. {
  103.         struct snull_priv *priv = netdev_priv(dev);
  104.         unsigned long flags;
  105.         struct snull_packet *pkt;
  106.    
  107.         spin_lock_irqsave(&priv->lock, flags);
  108.         pkt = priv->ppool;
  109.         priv->ppool = pkt->next;
  110.         if (priv->ppool == NULL) {
  111.                 printk (KERN_INFO "Pool empty\n");
  112.                 netif_stop_queue(dev);
  113.         }
  114.         spin_unlock_irqrestore(&priv->lock, flags);
  115.         return pkt;
  116. }

  117. /*将包缓存交还给缓存池*/
  118. void snull_release_buffer(struct snull_packet *pkt)
  119. {
  120.         unsigned long flags;
  121.         struct snull_priv *priv = netdev_priv(pkt->dev);
  122.        
  123.         spin_lock_irqsave(&priv->lock, flags);
  124.         pkt->next = priv->ppool;
  125.         priv->ppool = pkt;
  126.         spin_unlock_irqrestore(&priv->lock, flags);
  127.         if (netif_queue_stopped(pkt->dev) && pkt->next == NULL)
  128.                 netif_wake_queue(pkt->dev);
  129. }

  130. /*将要传输的包加入到设备dev的传输队列首部,当然,这只是一个演示,这样一来,就变成先进先出了*/
  131. void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)
  132. {
  133.         unsigned long flags;
  134.         struct snull_priv *priv = netdev_priv(dev);

  135.         spin_lock_irqsave(&priv->lock, flags);
  136.         pkt->next = priv->rx_queue;  /* FIXME - misorders packets */
  137.         priv->rx_queue = pkt;
  138.         spin_unlock_irqrestore(&priv->lock, flags);
  139. }

  140. /*取得传输队列中的第一个数据包*/
  141. struct snull_packet *snull_dequeue_buf(struct net_device *dev)
  142. {
  143.         struct snull_priv *priv = netdev_priv(dev);
  144.         struct snull_packet *pkt;
  145.         unsigned long flags;

  146.         spin_lock_irqsave(&priv->lock, flags);
  147.         pkt = priv->rx_queue;
  148.         if (pkt != NULL)
  149.                 priv->rx_queue = pkt->next;
  150.         spin_unlock_irqrestore(&priv->lock, flags);
  151.         return pkt;
  152. }

  153. /*
  154. * 打开/关闭接收中断.
  155. */
  156. static void snull_rx_ints(struct net_device *dev, int enable)
  157. {
  158.         struct snull_priv *priv = netdev_priv(dev);
  159.         priv->rx_int_enabled = enable;
  160. }

  161.    
  162. /*
  163. * 设备打开函数,是驱动最重要的函数之一,它应该注册所有的系统资源(I/O端口,IRQ、DMA等等),
  164. * 并对设备执行其他所需的设置。
  165. * 因为这个例子中,并没有真正的物理设备,所以,它最重要的工作就是启动传输队列。
  166. */

  167. int snull_open(struct net_device *dev)
  168. {
  169.         /* request_region(), request_irq(), ....  (like fops->open) */

  170.         /*
  171.          * Assign the hardware address of the board: use "\0SNULx", where
  172.          * x is 0 or 1. The first byte is '\0' to avoid being a multicast
  173.          * address (the first byte of multicast addrs is odd).
  174.          */
  175.         memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
  176.         if (dev == snull_devs[1])
  177.                 dev->dev_addr[ETH_ALEN-1]++; /* \0SNUL1 */
  178.         netif_start_queue(dev);
  179.         return 0;
  180. }

  181. /*设备停止函数,这里的工作就是停止传输队列*/
  182. int snull_release(struct net_device *dev)
  183. {
  184.     /* release ports, irq and such -- like fops->close */

  185.         netif_stop_queue(dev); /* can't transmit any more */
  186.         return 0;
  187. }

  188. /*
  189. * 当用户调用ioctl时类型为SIOCSIFMAP时,如使用ifconfig,系统会调用驱动程序的set_config 方法。
  190. * 用户会传递一个ifmap结构包含需要设置的I/O地址、中断等参数。
  191. */
  192. int snull_config(struct net_device *dev, struct ifmap *map)
  193. {
  194.         if (dev->flags & IFF_UP) /* 不能设置一个正在运行状态的设备 */
  195.                 return -EBUSY;

  196.         /* 这个例子中,不允许改变 I/O 地址*/
  197.         if (map->base_addr != dev->base_addr) {
  198.                 printk(KERN_WARNING "snull: Can't change I/O address\n");
  199.                 return -EOPNOTSUPP;
  200.         }

  201.         /* 允许改变 IRQ */
  202.         if (map->irq != dev->irq) {
  203.                 dev->irq = map->irq;
  204.                 /* request_irq() is delayed to open-time */
  205.         }

  206.         /* ignore other fields */
  207.         return 0;
  208. }

  209. /*
  210. * 接收数据包函数
  211. * 它被“接收中断”调用,重组数据包,并调用函数netif_rx进一步处理。
  212. * 我们从“硬件”中收到的包,是用struct snull_packet来描述的,但是内核中描述一个包,是使用
  213. * struct sk_buff(简称skb),所以,这里要完成一个把硬件接收的包拷贝至内核缓存skb的一个
  214. * 组包过程(PS:不知在接收之前直接分配一个skb,省去这一步,会如何提高性能,没有研究过,见笑了^o^)。
  215. */
  216. void snull_rx(struct net_device *dev, struct snull_packet *pkt)
  217. {
  218.         struct sk_buff *skb;
  219.         struct snull_priv *priv = netdev_priv(dev);

  220.         /*
  221.          * 分配skb缓存
  222.          */
  223.         skb = dev_alloc_skb(pkt->datalen + 2);
  224.         if (!skb) {                        /*分配失败*/
  225.                 if (printk_ratelimit())
  226.                         printk(KERN_NOTICE "snull rx: low on mem - packet dropped\n");
  227.                 priv->stats.rx_dropped++;
  228.                 goto out;
  229.         }
  230.         /*
  231.          * skb_reserver用来增加skb的date和tail,因为以太网头部为14字节长,再补上两个字节就刚好16字节边界
  232.          * 对齐,所以大多数以太网设备都会在数据包之前保留2个字节。
  233.          */
  234.         skb_reserve(skb, 2); /* align IP on 16B boundary */  
  235.         memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);

  236.         skb->dev = dev;                        /*skb与接收设备就关联起来了,它在网络栈中会被广泛使用,没道理不知道数据是谁接收来的吧*/
  237.         skb->protocol = eth_type_trans(skb, dev);        /*获取上层协议类型,这样,上层处理函数才知道如何进一步处理*/
  238.         skb->ip_summed = CHECKSUM_UNNECESSARY;                 /* 设置较验标志:不进行任何校验,作者的驱动的收发都在内存中进行,是没有必要进行校验*/
  239.        
  240.         /*累加计数器*/
  241.         priv->stats.rx_packets++;
  242.         priv->stats.rx_bytes += pkt->datalen;
  243.        
  244.         /*
  245.          * 把数据包交给上层。netif_rx会逐步调用netif_rx_schedule -->__netif_rx_schedule,
  246.          * __netif_rx_schedule函数会调用__raise_softirq_irqoff(NET_RX_SOFTIRQ);触发网络接收数据包的软中断函数net_rx_action。
  247.          * 软中断是Linux内核完成中断推后处理工作的一种机制,请参考《Linux内核设计与实现》第二版。
  248.          * 唯一需要提及的是,这个软中断函数net_rx_action是在网络系统初始化的时候(linux/net/core/dev.c):注册的
  249.          * open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
  250.          */
  251.         netif_rx(skb);
  252.   out:
  253.         return;
  254. }
  255.    

  256. /*
  257. * NAPI 的poll轮询函数.
  258. */
  259. static int snull_poll(struct net_device *dev, int *budget)
  260. {
  261.         /*
  262.          * dev->quota是当前CPU能够从所有接口中接收数据包的最大数目,budget是在
  263.          * 初始化阶段分配给接口的weight值,轮询函数必须接受二者之间的最小值。表示
  264.          * 轮询函数本次要处理的数据包个数。
  265.          */
  266.         int npackets = 0, quota = min(dev->quota, *budget);
  267.         struct sk_buff *skb;
  268.         struct snull_priv *priv = netdev_priv(dev);
  269.         struct snull_packet *pkt;
  270.    
  271.             /*这个循环次数由要处理的数据包个数,并且,以处理完接收队列为上限*/
  272.         while (npackets < quota && priv->rx_queue) {
  273.                 /*从队列中取出数据包*/
  274.                 pkt = snull_dequeue_buf(dev);
  275.                
  276.                 /*接下来的处理,和传统中断事实上是一样的*/
  277.                 skb = dev_alloc_skb(pkt->datalen + 2);
  278.                 if (! skb) {
  279.                         if (printk_ratelimit())
  280.                                 printk(KERN_NOTICE "snull: packet dropped\n");
  281.                         priv->stats.rx_dropped++;
  282.                         snull_release_buffer(pkt);
  283.                         continue;
  284.                 }
  285.                 skb_reserve(skb, 2); /* align IP on 16B boundary */  
  286.                 memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
  287.                 skb->dev = dev;
  288.                 skb->protocol = eth_type_trans(skb, dev);
  289.                 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
  290.                
  291.                 /*需要调用netif_receive_skb而不是net_rx将包交给上层协议栈*/
  292.                 netif_receive_skb(skb);
  293.                
  294.                 /*累加计数器 */
  295.                 npackets++;
  296.                 priv->stats.rx_packets++;
  297.                 priv->stats.rx_bytes += pkt->datalen;
  298.                 snull_release_buffer(pkt);
  299.         }
  300.         /* If we processed all packets, we're done; tell the kernel and reenable ints */
  301.         *budget -= npackets;
  302.         dev->quota -= npackets;
  303.        
  304.         //
  305.         if (! priv->rx_queue) {
  306.                 netif_rx_complete(dev);
  307.                 snull_rx_ints(dev, 1);
  308.                 return 0;
  309.         }
  310.         /* We couldn't process everything. */
  311.         return 1;
  312. }
  313.             
  314.         
  315. /*
  316. * 设备的中断函数,当需要发/收数据,出现错误,连接状态变化等,它会被触发
  317. * 对于典型的网络设备,一般会在open函数中注册中断函数,这样,当网络设备产生中断时,如接收到数据包时,
  318. * 中断函数将会被调用。不过在这个例子中,因为没有真正的物理设备,所以,不存在注册中断,也就不存在触
  319. * 发,对于接收和发送,它都是在自己设计的函数的特定位置被调用。
  320. * 这个中断函数设计得很简单,就是取得设备的状态,判断是“接收”还是“发送”的中断,以调用相应的处理函数。
  321. * 而对于,“是哪个设备产生的中断”这个问题,则由调用它的函数通过第二个参数的赋值来决定。
  322. */
  323. static void snull_regular_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  324. {
  325.         int statusword;
  326.         struct snull_priv *priv;
  327.         struct snull_packet *pkt = NULL;
  328.         /*
  329.          * 通常,需要检查 "device" 指针以确保这个中断是发送给自己的。
  330.          * 然后为 "struct device *dev" 赋
  331.          */
  332.         struct net_device *dev = (struct net_device *)dev_id;

  333.         /* paranoid */
  334.         if (!dev)
  335.                 return;

  336.         /* 锁住设备 */
  337.         priv = netdev_priv(dev);
  338.         spin_lock(&priv->lock);

  339.         /* 取得设备状态指字,对于真实设备,使用I/O指令,比如:int txsr = inb(TX_STATUS); */
  340.         statusword = priv->status;
  341.         priv->status = 0;
  342.         if (statusword & SNULL_RX_INTR) {                /*如果是接收数据包的中断*/
  343.                 /* send it to snull_rx for handling */
  344.                 pkt = priv->rx_queue;
  345.                 if (pkt) {
  346.                         priv->rx_queue = pkt->next;
  347.                         snull_rx(dev, pkt);
  348.                 }
  349.         }
  350.         if (statusword & SNULL_TX_INTR) {                /*如果是发送数据包的中断*/
  351.                 /* a transmission is over: free the skb */
  352.                 priv->stats.tx_packets++;
  353.                 priv->stats.tx_bytes += priv->tx_packetlen;
  354.                 dev_kfree_skb(priv->skb);
  355.         }

  356.         /* 释放锁 */
  357.         spin_unlock(&priv->lock);
  358.        
  359.         /*释放缓冲区*/
  360.         if (pkt) snull_release_buffer(pkt); /* Do this outside the lock! */
  361.         return;
  362. }

  363. /*
  364. * A NAPI interrupt handler.
  365. * 在设备初始化的时候,poll指向指向了snull_poll函数,所以,NAPI中断处理函数很简单,
  366. * 当“接收中断”到达的时候,它就屏蔽此中断,然后netif_rx_schedule函数接收,接收函数
  367. * 会在未来某一时刻调用注册的snull_poll函数实现轮询,当然,对于“传输中断”,处理方法
  368. * 同传统中断处理并无二致。
  369. */
  370. static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  371. {
  372.         int statusword;
  373.         struct snull_priv *priv;

  374.         /*
  375.          * As usual, check the "device" pointer for shared handlers.
  376.          * Then assign "struct device *dev"
  377.          */
  378.         struct net_device *dev = (struct net_device *)dev_id;
  379.         /* ... and check with hw if it's really ours */

  380.         /* paranoid */
  381.         if (!dev)
  382.                 return;

  383.         /* Lock the device */
  384.         priv = netdev_priv(dev);
  385.         spin_lock(&priv->lock);

  386.         /* retrieve statusword: real netdevices use I/O instructions */
  387.         statusword = priv->status;
  388.         priv->status = 0;
  389.        
  390.         /*
  391.          * 唯一的区别就在这里,它先屏蔽掉接收中断,然后调用netif_rx_schedule,而不是netif_rx
  392.          * 重点还是在于poll函数的设计。
  393.          */
  394.         if (statusword & SNULL_RX_INTR) {
  395.                 snull_rx_ints(dev, 0);  /* Disable further interrupts */
  396.                 netif_rx_schedule(dev);
  397.         }
  398.         if (statusword & SNULL_TX_INTR) {
  399.                 /* a transmission is over: free the skb */
  400.                 priv->stats.tx_packets++;
  401.                 priv->stats.tx_bytes += priv->tx_packetlen;
  402.                 dev_kfree_skb(priv->skb);
  403.         }

  404.         /* Unlock the device and we are done */
  405.         spin_unlock(&priv->lock);
  406.         return;
  407. }



  408. /*
  409. * Transmit a packet (low level interface)
  410. */
  411. static void snull_hw_tx(char *buf, int len, struct net_device *dev)
  412. {
  413.         /*
  414.          * This function deals with hw details. This interface loops
  415.          * back the packet to the other snull interface (if any).
  416.          * In other words, this function implements the snull behaviour,
  417.          * while all other procedures are rather device-independent
  418.          */
  419.         struct iphdr *ih;
  420.         struct net_device *dest;
  421.         struct snull_priv *priv;
  422.         u32 *saddr, *daddr;
  423.         struct snull_packet *tx_buffer;
  424.    
  425.         /* I am paranoid. Ain't I? */
  426.         if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) {
  427.                 printk("snull: Hmm... packet too short (%i octets)\n",
  428.                                 len);
  429.                 return;
  430.         }

  431.         if (0) { /* enable this conditional to look at the data */
  432.                 int i;
  433.                 PDEBUG("len is %i\n" KERN_DEBUG "data:",len);
  434.                 for (i=14 ; i<len; i++)
  435.                         printk(" %02x",buf[i]&0xff);
  436.                 printk("\n");
  437.         }
  438.         /*
  439.          * 取得来源IP和目的IP地址
  440.          */
  441.         ih = (struct iphdr *)(buf+sizeof(struct ethhdr));
  442.         saddr = &ih->saddr;
  443.         daddr = &ih->daddr;
  444.        
  445.         /*
  446.          * 这里做了三个调换,以实现欺骗:来源地址第三octet 0<->1,目的地址第三octet 0<->1,设备snX编辑0<->1,这样做的理由是:
  447.          * sn0(发):192.168.0.88 --> 192.168.0.99        做了调换后,就变成:
  448.          * sn1(收):192.168.1.88 --> 192.168.1.99        因为sn1的地址就是192.168.1.99,所以,它收到这个包后,会回应:
  449.          * sn1(发):192.168.1.99 --> 192.168.1.88        ,同样地,做了这样的调换后,就变成:
  450.          * sn0(收):192.168.0.99 --> 192.168.0.88        这样,sn0就会收到这个包,实现了ping的请求与应答,^o^
  451.          */
  452.         ((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
  453.         ((u8 *)daddr)[2] ^= 1;

  454.         /*重新计算较验和*/
  455.         ih->check = 0;         /* and rebuild the checksum (ip needs it) */
  456.         ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);

  457.         /*输出调试信息*/
  458.         if (dev == snull_devs[0])
  459.                 PDEBUGG("%08x:%05i --> %08x:%05i\n",
  460.                                 ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
  461.                                 ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
  462.         else
  463.                 PDEBUGG("%08x:%05i <-- %08x:%05i\n",
  464.                                 ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
  465.                                 ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));

  466.         /*调换设备编号,即dest指向接收设备,原因如前所述*/
  467.         dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
  468.        
  469.         /*将发送的数据添加到接收设备的接收队列中*/
  470.         priv = netdev_priv(dest);
  471.         tx_buffer = snull_get_tx_buffer(dev);
  472.         tx_buffer->datalen = len;
  473.         memcpy(tx_buffer->data, buf, len);
  474.         snull_enqueue_buf(dest, tx_buffer);
  475.        
  476.         /*
  477.          * 如果设备接收标志打开,就调用中断函数把数据包发送给目标设备——即触发目的设备的接收中断,这样
  478.          * 中断程序就会自接收设备的接收队列中接收数据包,并交给上层网络栈处理
  479.         */
  480.         if (priv->rx_int_enabled) {
  481.                 priv->status |= SNULL_RX_INTR;
  482.                 snull_interrupt(0, dest, NULL);
  483.         }

  484.         /*发送完成后,触发“发送完成”中断*/
  485.         priv = netdev_priv(dev);
  486.         priv->tx_packetlen = len;
  487.         priv->tx_packetdata = buf;
  488.         priv->status |= SNULL_TX_INTR;
  489.        
  490.         /*
  491.          * 如果insmod驱动的时候,指定了模拟硬件锁的lockup=n,则在会传输n个数据包后,模拟一次硬件锁住的情况,
  492.          * 这是通过调用netif_stop_queue函数来停止传输队列,标记“设备不能再传输数据包”实现的,它将在传输的超
  493.          * 时函数中,调用netif_wake_queue函数来重新启动传输队例,同时超时函数中会再次调用“接收中断”,这样
  494.          * stats.tx_packets累加,又可以重新传输新的数据包了(参接收中断和超时处理函数的实现)。
  495.          */
  496.         if (lockup && ((priv->stats.tx_packets + 1) % lockup) == 0) {
  497.                 /* Simulate a dropped transmit interrupt */
  498.                 netif_stop_queue(dev);                        /*停止数据包的传输*/
  499.                 PDEBUG("Simulate lockup at %ld, txp %ld\n", jiffies,
  500.                                 (unsigned long) priv->stats.tx_packets);
  501.         }
  502.         else
  503.         /*发送完成后,触发中断,中断函数发现发送完成,就累加计数器,释放skb缓存*/
  504.                 snull_interrupt(0, dev, NULL);
  505.                
  506.         /*
  507.          * 看到这里,我们可以看到,这个发送函数其实并没有把数据包通过I/O指令发送给硬件,而仅仅是做了一个地址/设备的调换,
  508.          * 并把数据包加入到接收设备的队例当中。
  509.          */               
  510. }

  511. /*
  512. * 数据包传输函数,Linux网络堆栈,在发送数据包时,会调用驱动程序的hard_start_transmit函数,
  513. * 在设备初始化的时候,这个函数指针指向了snull_tx。
  514. */
  515. int snull_tx(struct sk_buff *skb, struct net_device *dev)
  516. {
  517.         int len;
  518.         char *data, shortpkt[ETH_ZLEN];
  519.         struct snull_priv *priv = netdev_priv(dev);
  520.        
  521.         data = skb->data;
  522.         len = skb->len;
  523.         if (len < ETH_ZLEN) {                        /*处理短帧的情况,如果小于以太帧最小长度,不足位全部补0*/
  524.                 memset(shortpkt, 0, ETH_ZLEN);
  525.                 memcpy(shortpkt, skb->data, skb->len);
  526.                 len = ETH_ZLEN;
  527.                 data = shortpkt;
  528.         }
  529.         dev->trans_start = jiffies; /* 保存时间戳 */

  530.         /*
  531.          * 因为“发送”完成后,需要释放skb,所以,先要保存它 ,释放都是在网卡发送完成,产生中断,而中断函数收
  532.          * 到网卡的发送完成的中断信号后释放
  533.          */
  534.         priv->skb = skb;

  535.         /*
  536.          * 让硬件把数据包发送出去,对于物理设备,就是一个读网卡寄存器的过程,不过,这里,只是一些
  537.          * 为了实现演示功能的虚假的欺骗函数,比如操作源/目的IP,然后调用接收函数(所以,接收时不用调用中断)
  538.          */
  539.         snull_hw_tx(data, len, dev);

  540.         return 0; /* Our simple device can not fail */
  541. }

  542. /*
  543. * 传输超时处理函数
  544. * 比如在传输数据时,由于缓冲已满,需要关闭传输队列,但是驱动程序是不能丢弃数据包,它将在“超时”的时候触发
  545. * 超时处理函数,这个函数将发送一个“传输中断”,以填补丢失的中断,并重新启动传输队例子
  546. */
  547. void snull_tx_timeout (struct net_device *dev)
  548. {
  549.         struct snull_priv *priv = netdev_priv(dev);

  550.         PDEBUG("Transmit timeout at %ld, latency %ld\n", jiffies,
  551.                         jiffies - dev->trans_start);
  552.         /* Simulate a transmission interrupt to get things moving */
  553.         priv->status = SNULL_TX_INTR;
  554.         snull_interrupt(0, dev, NULL);
  555.         priv->stats.tx_errors++;
  556.         netif_wake_queue(dev);
  557.         return;
  558. }



  559. /*
  560. * Ioctl 命令
  561. */
  562. int snull_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  563. {
  564.         PDEBUG("ioctl\n");
  565.         return 0;
  566. }

  567. /*
  568. * 获取设备的状态
  569. */
  570. struct net_device_stats *snull_stats(struct net_device *dev)
  571. {
  572.         struct snull_priv *priv = netdev_priv(dev);
  573.         return &priv->stats;
  574. }
复制代码
  • 本版精华
  • 工作队列分析
  • 实战linux内核编译
  • Netfilter 连接跟踪与状态检测的实现
  • 我也来学做嵌入式Linux系统V0.1(完整版)
  • 摩托罗拉E380/海尔N60/夏新E600/philips968等手机系统分析
  • 基于S3C2410的Linux全线移植文档
  • 急求linux0.11内核源码?
  • 对Linux堆内存释放的总结
  • 用netconsole从网络上收集Kernel Panic信息详细方法
  • Linux内核配置文档!!!(make menuconfig)
www.smart6.net,致力国内最专业的软路由系统!!!
shell怎么读取网页内容  |  ssh等待连接的超时问题  |  curl: (56) Recv failure: Connection rese ...  |  CACTI 不能安装WINE,怎么办?  |  
 
  
独孤九贱

富足长乐

Rank: 5Rank: 5

帖子
2415
主题
441
精华
36
可用积分
6081
专家积分
0
在线时间
760 小时
注册时间
2003-08-12
最后登录
2014-09-11
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
2楼[报告][回复][引用]
发表于 2006-10-22 13:54:12|只看该作者
  1. /*
  2. * 有些网络有硬件地址(比如Ethernet),并且在发送硬件帧时需要知道目的硬件 地址会进行ARP请求/应答,以完成MAC地址解析,
  3. * 需要做arp请求的设备在发送之前会调用驱动程序的rebuild_header函数。需要做arp的的设备在发送之前会调用驱动程序的
  4. * rebuild_header方 法。调用的主要参数包括指向硬件帧头的指针,协议层地址。如果驱动程序能够解 析硬件地址,就返回1,
  5. * 如果不能,返回0。
  6. * 当然,作者实现的演示设备中,不支持这个过程。
  7. */
  8. int snull_rebuild_header(struct sk_buff *skb)
  9. {
  10.         struct ethhdr *eth = (struct ethhdr *) skb->data;
  11.         struct net_device *dev = skb->dev;
  12.    
  13.         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
  14.         memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
  15.         eth->h_dest[ETH_ALEN-1]   ^= 0x01;   /* dest is us xor 1 */
  16.         return 0;
  17. }

  18. /*
  19. * 为上层协议创建一个二层的以太网首部。
  20. * 事实上,如果一开始调用alloc_etherdev分配以太设备,它会调用ether_setup进行初始化,初始化函数会设置:
  21. *        dev->hard_header        = eth_header;
  22. *        dev->rebuild_header         = eth_rebuild_header;
  23. * 驱动开发人员并不需要自己来实现这个函数,作者这样做,只是为了展示细节。
  24. */

  25. int snull_header(struct sk_buff *skb, struct net_device *dev,
  26.                 unsigned short type, void *daddr, void *saddr,
  27.                 unsigned int len)
  28. {
  29.         /*获取以太头指针*/
  30.         struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);

  31.         eth->h_proto = htons(type);                /*填写协议*/
  32.        
  33.         /*填写来源/目的MAC地址,如果地址为空,则用设备自己的地址代替之*/
  34.         memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
  35.         memcpy(eth->h_dest,   daddr ? daddr : dev->dev_addr, dev->addr_len);
  36.        
  37.         /*
  38.          * 将第一个octet设为0,主要是为了可以在不支持组播链路,如ppp链路上运行
  39.          * PS:作者这样做,仅仅是演示在PC机上的实现,事实上,直接使用ETH_ALEN-1是
  40.          * 不适合“大头”机器的。
  41.          */
  42.         eth->h_dest[ETH_ALEN-1]   ^= 0x01;   /* dest is us xor 1 */
  43.         return (dev->hard_header_len);
  44. }

  45. /*
  46. * 改变设备MTU值.
  47. */
  48. int snull_change_mtu(struct net_device *dev, int new_mtu)
  49. {
  50.         unsigned long flags;
  51.         struct snull_priv *priv = netdev_priv(dev);
  52.         spinlock_t *lock = &priv->lock;
  53.    
  54.         /* check ranges */
  55.         if ((new_mtu < 68) || (new_mtu > 1500))
  56.                 return -EINVAL;
  57.         /*
  58.          * Do anything you need, and the accept the value
  59.          */
  60.         spin_lock_irqsave(lock, flags);
  61.         dev->mtu = new_mtu;
  62.         spin_unlock_irqrestore(lock, flags);
  63.         return 0; /* success */
  64. }



  65. /*
  66. * 设备初始化函数,它必须在 register_netdev 函数被调用之前调用
  67. */
  68. void snull_init(struct net_device *dev)
  69. {
  70.         /*设备的“私有”结构,保存一些设备一些“私有数据”*/
  71.         struct snull_priv *priv;
  72. #if 0
  73.             /*
  74.          * Make the usual checks: check_region(), probe irq, ...  -ENODEV
  75.          * should be returned if no device found.  No resource should be
  76.          * grabbed: this is done on open().
  77.          */
  78. #endif

  79.             /*
  80.          * 初始化以太网设备的一些共用的成员
  81.          */
  82.         ether_setup(dev); /* assign some of the fields */

  83.         /*设置设备的许多成员函数指针*/
  84.         dev->open            = snull_open;
  85.         dev->stop            = snull_release;
  86.         dev->set_config      = snull_config;
  87.         dev->hard_start_xmit = snull_tx;
  88.         dev->do_ioctl        = snull_ioctl;
  89.         dev->get_stats       = snull_stats;
  90.         dev->change_mtu      = snull_change_mtu;  
  91.         dev->rebuild_header  = snull_rebuild_header;
  92.         dev->hard_header     = snull_header;
  93.         dev->tx_timeout      = snull_tx_timeout;
  94.         dev->watchdog_timeo = timeout;
  95.        
  96.         /*如果使用NAPI,设置pool函数*/
  97.         if (use_napi) {
  98.                 dev->poll        = snull_poll;
  99.                 dev->weight      = 2;                /*weight是接口在资源紧张时,在接口上能承受多大流量的权重*/
  100.         }
  101.         /* keep the default flags, just add NOARP */
  102.         dev->flags           |= IFF_NOARP;
  103.         dev->features        |= NETIF_F_NO_CSUM;
  104.         dev->hard_header_cache = NULL;      /* Disable caching */

  105.         /*
  106.          * 取得私有数据区,并初始化它.
  107.          */
  108.         priv = netdev_priv(dev);
  109.         memset(priv, 0, sizeof(struct snull_priv));
  110.         spin_lock_init(&priv->lock);
  111.         snull_rx_ints(dev, 1);                /* 打开接收中断标志 */
  112.         snull_setup_pool(dev);                /*设置使用NAPI时的接收缓冲池*/
  113. }

  114. /*
  115. * The devices
  116. */

  117. struct net_device *snull_devs[2];



  118. /*
  119. * 模块卸载函数,先unregister已经注册的设备,然后释放pool缓存申请的内存,最后释放设
  120. */

  121. void snull_cleanup(void)
  122. {
  123.         int i;
  124.    
  125.         for (i = 0; i < 2;  i++) {
  126.                 if (snull_devs[i]) {
  127.                         unregister_netdev(snull_devs[i]);
  128.                         snull_teardown_pool(snull_devs[i]);
  129.                         free_netdev(snull_devs[i]);
  130.                 }
  131.         }
  132.         return;
  133. }

  134. /*模块初始化,初始化的只有一个工作:分配一个设备结构并注册它*/
  135. int snull_init_module(void)
  136. {
  137.         int result, i, ret = -ENOMEM;

  138.         /*中断函数指针,因是否使用NAPI而指向不同的中断函数*/
  139.         snull_interrupt = use_napi ? snull_napi_interrupt : snull_regular_interrupt;

  140.         /*
  141.          * 分配两个设备,网络设备都是用struct net_device来描述,alloc_netdev分配设备,第三个参数是
  142.          * 对struct net_device结构成员进行初始化的函数,对于以太网来说,可以把alloc_netdev/snull_init
  143.          * 两个函数变为一个,alloc_etherdev,它会自动调用以太网的初始化函数ether_setup,因为以太网的初
  144.          * 始化函数工作都是近乎一样的 */
  145.         snull_devs[0] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
  146.                         snull_init);
  147.         snull_devs[1] = alloc_netdev(sizeof(struct snull_priv), "sn%d",
  148.                         snull_init);
  149.         /*分配失败*/
  150.         if (snull_devs[0] == NULL || snull_devs[1] == NULL)
  151.                 goto out;

  152.         ret = -ENODEV;
  153.         /*向内核注册网络设备,这样,设备就可以被使用了*/
  154.         for (i = 0; i < 2;  i++)
  155.                 if ((result = register_netdev(snull_devs[i])))
  156.                         printk("snull: error %i registering device \"%s\"\n",
  157.                                         result, snull_devs[i]->name);
  158.                 else
  159.                         ret = 0;
  160.    out:
  161.         if (ret)
  162.                 snull_cleanup();
  163.         return ret;
  164. }


  165. module_init(snull_init_module);
  166. module_exit(snull_cleanup);[/
复制代码

0 0
原创粉丝点击