内核中接收网络帧的处理(2层)

来源:互联网 发布:centos无线网络配置 编辑:程序博客网 时间:2024/05/20 17:40
我这里描述的只是2层的处理。 

首先,我们来看softnet_data这个结构,每个cpu都有这样的一个队列,它主要是用来存储incoming frame。由于他是每个cpu都有一个队列,因此在不同的cpu之间我们就不要任何锁来控制并发的处理这个帧队列。我们在操作系统层要取得帧数据,都是通过这个数据来读取。 


Java代码  收藏代码
  1. /* 
  2.  * Incoming packets are placed on per-cpu queues so that 
  3.  * no locking is needed. 
  4.  */  
  5. struct softnet_data  
  6. {  
  7. ///qdisc是queueing discipline的简写,也就是排队规则,就是我们经常说的qos.这里也就是输出帧的控制。  
  8.     struct Qdisc        *output_queue;  
  9. ///当输入帧被驱动取得之前,就保存在这个队列里,这里要注意,这个只是非napi的驱动才会这样,而napi的驱动则是有自己的私有的队列。  
  10.     struct sk_buff_head input_pkt_queue;  
  11. ///表示有输入帧待处理的设备链表。  
  12.     struct list_head    poll_list;  
  13. ///表示已经成功被传递出的帧的链表。  
  14.     struct sk_buff      *completion_queue;  
  15. ///用来兼容非napi的驱动。  
  16.     struct napi_struct  backlog;  
  17. #ifdef CONFIG_NET_DMA  
  18.     struct dma_chan     *net_dma;  
  19. #endif  
  20. };  



这张图很好的表示了coy的softnet_data结构和网络设备的关系: 

 


接下来我们来看它的初始化: 

Java代码  收藏代码
  1. static int __init net_dev_init(void)  
  2. {  
  3. ..............................  
  4.   
  5.     for_each_possible_cpu(i) {  
  6.         struct softnet_data *queue;  
  7. ///取每一个cpu的队列,并初始化。  
  8.         queue = &per_cpu(softnet_data, i);  
  9.         skb_queue_head_init(&queue->input_pkt_queue);  
  10.         queue->completion_queue = NULL;  
  11.         INIT_LIST_HEAD(&queue->poll_list);  
  12.   
  13. ///这里我们就很清楚的看到backlog的功能了,由于napi的驱动都会有一个poll的虚函数,而非napi是没有的,因此这里会给所有的非napi的驱动赋值一个默认的处理方法。  
  14.         queue->backlog.poll = process_backlog;  
  15.         queue->backlog.weight = weight_p;  
  16.     }  
  17. ...............................................  
  18.     return rc;  
  19. }  


当一个新的帧的到达之后,内核处理的函数有两种(其实也就是2层的处理,当处理完后,就将帧扔到3层): 

1 老的netif_rx函数 
2 新的napi接口。 

首先来介绍napi。 

简单来说就是,当内核还在处理一个帧的时候,有另外的帧到来,这时napi不需要再执行中断,而是保持轮询设备的输入队列,从而取得新到的帧,当队列为空时,退出轮询。重新打开中断。 

napi的数据结构: 

Java代码  收藏代码
  1. struct napi_struct {  
  2.     /* The poll_list must only be managed by the entity which 
  3.      * changes the state of the NAPI_STATE_SCHED bit.  This means 
  4.      * whoever atomically sets that bit can add this napi_struct 
  5.      * to the per-cpu poll_list, and whoever clears that bit 
  6.      * can remove from the list right before clearing the bit. 
  7.      */  
  8. ///有新的帧等到被执行的设备链表,这个链表的头就是softnet_data->poll_list.  
  9.     struct list_head    poll_list;  
  10. ///napi的状态  
  11.     unsigned long       state;  
  12. ///也就是表示分配给此napi的所能处理的帧的限额。  
  13.     int         weight;  
  14. ///从设备的输入队列中取得数据的虚函数。  
  15.     int         (*poll)(struct napi_struct *, int);  
  16. #ifdef CONFIG_NETPOLL  
  17.     spinlock_t      poll_lock;  
  18.     int         poll_owner;  
  19.     struct net_device   *dev;  
  20.     struct list_head    dev_list;  
  21. #endif  
  22. };  


下面的这张图可以看出napi和net_rx_action(软中断处理函数)的关系: 

 

接下来我们来看一下内核如何把老的驱动模型和napi统一到一起,关键的数据结构就是我们上面讲的softnet_data的backlog结构。 

先来看下napi和非napi驱动的区别: 




这里很清楚的看到在飞napi的驱动中,要调用netif_rx函数,然后再调用netif_rx_schedule来把所需处理的帧交给软中断处理链表。 

我们来看它的实现: 

Java代码  收藏代码
  1. int netif_rx(struct sk_buff *skb)  
  2. {  
  3.     struct softnet_data *queue;  
  4.     unsigned long flags;  
  5.   
  6.     /* if netpoll wants it, pretend we never saw it */  
  7.     if (netpoll_rx(skb))  
  8.         return NET_RX_DROP;  
  9. ///得到帧被接收的时间  
  10.     if (!skb->tstamp.tv64)  
  11.         net_timestamp(skb);  
  12.   
  13.     /* 
  14.      * The code is rearranged so that the path is the most 
  15.      * short when CPU is congested, but is still operating. 
  16.      */  
  17. ///保存当前的状态设备状态。  
  18.     local_irq_save(flags);  
  19. ///取得当前cpu的softnet_data数据  
  20.     queue = &__get_cpu_var(softnet_data);  
  21.   
  22. ///将被当前cpu所接受的总帧的数目加一。  
  23.     __get_cpu_var(netdev_rx_stat).total++;  
  24. ///监测设备是否还有空间来存储帧,如果空间已满,表示网络阻塞严重,则返回一个错误,此后cpu将丢掉再来的帧。  
  25.     if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {  
  26.         if (queue->input_pkt_queue.qlen) {  
  27. enqueue:  
  28. ///这个帧被加入到softnet_data的输入队列。并返回成功。  
  29.             __skb_queue_tail(&queue->input_pkt_queue, skb);  
  30.             local_irq_restore(flags);  
  31.             return NET_RX_SUCCESS;  
  32.         }  
  33. ///当队列是空的时候,表明这个队列并没有被软中断所schedule,因此我们需要将此队列加入到软中断的处理链表中。可以看到加入的正好是backlog,由于调用netif_rx的是非napi的驱动,因此backlog就是初始化时的process_backlog函数。  
  34.         napi_schedule(&queue->backlog);  
  35.         goto enqueue;  
  36.     }  
  37.   
  38.     __get_cpu_var(netdev_rx_stat).dropped++;  
  39.     local_irq_restore(flags);  
  40.     kfree_skb(skb);  
  41.     return NET_RX_DROP;  
  42. }  



然后我们来看网络代码最核心的一个函数net_rx_action 也就是软中断(NET_RX_SOFTIRQ)的处理函数: 

Java代码  收藏代码
  1. static void net_rx_action(struct softirq_action *h)  
  2. {  
  3. ///得到设备链表  
  4.     struct list_head *list = &__get_cpu_var(softnet_data).poll_list;  
  5. ///执行开始时间  
  6.     unsigned long start_time = jiffies;  
  7. ///当前所要处理的帧的最大数目。这个数目一定要限制,因为我们不能在此软中断耗费太多的时间。  
  8.     int budget = netdev_budget;  
  9.     void *have;  
  10. ///关闭中断  
  11.     local_irq_disable();  
  12.   
  13. ///开始循环处理设备链表  
  14.     while (!list_empty(list)) {  
  15.         struct napi_struct *n;  
  16.         int work, weight;  
  17. ///当处理完所需处理的帧,或者时间超时,则直接退出。  
  18.         if (unlikely(budget <= 0 || jiffies != start_time))  
  19.             goto softnet_break;  
  20.   
  21.         local_irq_enable();  
  22.   
  23. ///取出设备的napi数据结构。  
  24.         n = list_entry(list->next, struct napi_struct, poll_list);  
  25. ///加锁( 自旋锁)  
  26.         have = netpoll_poll_lock(n);  
  27.   
  28.         weight = n->weight;  
  29.   
  30.         work = 0;  
  31. ///测试napi的状态,只有为NAPI_STATE_SCHED状态时,我们才能调用napi的poll方法。它会返回所处理的帧的数目。  
  32.         if (test_bit(NAPI_STATE_SCHED, &n->state))  
  33.             work = n->poll(n, weight);  
  34.   
  35.         WARN_ON_ONCE(work > weight);  
  36. ///得到还需处理的帧的数目  
  37.         budget -= work;  
  38.   
  39.         local_irq_disable();  
  40.   
  41. ///当处理的帧等于分配给此设备的处理帧的限额,则进行相关处理  
  42.         if (unlikely(work == weight)) {  
  43.             if (unlikely(napi_disable_pending(n)))  
  44.                 __napi_complete(n);  
  45.             else  
  46. ///移除设备队列。  
  47.                 list_move_tail(&n->poll_list, list);  
  48.         }  
  49.   
  50.         netpoll_poll_unlock(have);  
  51.     }  
  52. out:  
  53.     local_irq_enable();  
  54. ......................................  
  55.   
  56.     return;  
  57.   
  58. softnet_break:  
  59.     __get_cpu_var(netdev_rx_stat).time_squeeze++;  
  60.     __raise_softirq_irqoff(NET_RX_SOFTIRQ);  
  61.     goto out;  
  62. }  



下来我们来看process_backlog函数,这个函数也就是非napi的驱动的默认poll的实现,napi的驱动的poll的实现,与它大体类似。 


Java代码  收藏代码
  1. static int process_backlog(struct napi_struct *napi, int quota)  
  2. {  
  3. ///得到一些初始化值。  
  4.     int work = 0;  
  5.     struct softnet_data *queue = &__get_cpu_var(softnet_data);  
  6.     unsigned long start_time = jiffies;  
  7.   
  8.     napi->weight = weight_p;  
  9.   
  10. ///进入循环处理。  
  11.     do {  
  12.         struct sk_buff *skb;  
  13.   
  14.         local_irq_disable();  
  15. ///得到输入队列。  
  16.         skb = __skb_dequeue(&queue->input_pkt_queue);  
  17.         if (!skb) {  
  18. ///如果输入队列为空,则设置此napi的标志,并退出。  
  19.             __napi_complete(napi);  
  20.             local_irq_enable();  
  21.             break;  
  22.         }  
  23.         local_irq_enable();  
  24. ///处理输入帧,也就是进行一些2层的处理从而发给三层。  
  25.         netif_receive_skb(skb);  
  26.     } while (++work < quota && jiffies == start_time);///设备处理帧的配额已经完成,或者时间太长,则退出。  
  27.     return work;  
  28. }