当前位置:   article > 正文

学习Linux-4.12内核网路协议栈(2.3)——接口层数据包的接收(下半部)_static_key_false(&ingress_needed)

static_key_false(&ingress_needed)



我们来继续分析net_rx_action:

  1. static __latent_entropy void net_rx_action(struct softirq_action *h)
  2. {
  3. struct softnet_data *sd = this_cpu_ptr(&softnet_data);
  4. unsigned long time_limit = jiffies +
  5. usecs_to_jiffies(netdev_budget_usecs);
  6. int budget = netdev_budget; //指定一次软中断处理的skb的数目,这里是300
  7. LIST_HEAD(list);
  8. LIST_HEAD(repoll);
  9. local_irq_disable();
  10. list_splice_init(&sd->poll_list, &list);
  11. local_irq_enable();
  12. for (;;) {
  13. struct napi_struct *n;
  14. if (list_empty(&list)) { //检查POLL队列(poll_list)上是否有设备在准备等待轮询
  15. if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
  16. goto out;
  17. break;
  18. }
  19. n = list_first_entry(&list, struct napi_struct, poll_list);
  20. budget -= napi_poll(n, &repoll); //调用poll函数从网卡驱动中读取一定数量的skb
  21. /* If softirq window is exhausted then punt.
  22. * Allow this to run for 2 jiffies since which will allow
  23. * an average latency of 1.5/HZ.
  24. */
  25.         if (unlikely(budget <= 0 || //如果读取的数量超过300,则终止中断处理
  26.                  time_after_eq(jiffies, time_limit))) {
  27.             sd->time_squeeze++;
  28.             break;
  29.         }
  30.     }
  31.     local_irq_disable();
  32.     list_splice_tail_init(&sd->poll_list, &list);
  33.     list_splice_tail(&repoll, &list);
  34.     list_splice(&list, &sd->poll_list);
  35.     if (!list_empty(&sd->poll_list)) //如果poll list中不为空,表示还有skb没有读取完成,则继续读取,触发下一次软中断
  36.         __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  37.     net_rps_action_and_irq_enable(sd);
  38. out:
  39.     __kfree_skb_flush();
  40. }
  41.  

  1. static int napi_poll(struct napi_struct *n, struct list_head *repoll)
  2. {
  3. void *have;
  4. int work, weight;
  5. list_del_init(&n->poll_list);
  6. have = netpoll_poll_lock(n);
  7. weight = n->weight;
  8. /* This NAPI_STATE_SCHED test is for avoiding a race
  9. * with netpoll's poll_napi(). Only the entity which
  10. * obtains the lock and sees NAPI_STATE_SCHED set will
  11. * actually make the ->poll() call. Therefore we avoid
  12. * accidentally calling ->poll() when NAPI is not scheduled.
  13. */
  14. work = 0;
  15. if (test_bit(NAPI_STATE_SCHED, &n->state)) {
  16. work = n->poll(n, weight); //在这里调用驱动的poll函数,如果驱动有支持NAPI,会定义并初始化这个poll函数,默认的poll函数是process_backlog
  17. trace_napi_poll(n, work, weight);
  18. }
  19. WARN_ON_ONCE(work > weight);
  20. if (likely(work < weight))
  21. goto out_unlock;
  22. /* Drivers must not modify the NAPI state if they
  23. * consume the entire weight. In such cases this code
  24. * still "owns" the NAPI instance and therefore can
  25. * move the instance around on the list at-will.
  26. */
  27. if (unlikely(napi_disable_pending(n))) {
  28. napi_complete(n);
  29. goto out_unlock;
  30. }
  1.         if (n->gro_list) {
  2.                 /* flush too old packets
  3.                  * If HZ < 1000, flush all packets.
  4.                  */
  5.                 napi_gro_flush(n, HZ >= 1000);
  6.         }
  7.         /* Some drivers may have called napi_schedule
  8.          * prior to exhausting their budget.
  9.          */
  10.         if (unlikely(!list_empty(&n->poll_list))) {
  11.                 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
  12.                              n->dev ? n->dev->name : "backlog");
  13.                 goto out_unlock;
  14.         }
  15.         list_add_tail(&n->poll_list, repoll);
  16. out_unlock:
  17.         netpoll_poll_unlock(have);
  18.         return work;
  19. }

到这里我们知道了poll函数是怎么被调用的,对于非NAPI来说,它的poll函数是process_backlog,最后调用__netif_receive_skb传送给网路层;

对于NAPI来说,它的poll函数是在驱动加载初始化的时候指定的,如果驱动支持GRO,则会在它的poll函数中调用napi_gro_receive()函数进行包的接收与组装,然后调用netif_receive_skb进一步时间戳和RPS的处理,最后调用__netif_receive_skb传送给网路层;

那么接下来分析一下这几个关键函数:

1. process_backlog

  1. static int process_backlog(struct napi_struct *napi, int quota)
  2. {
  3. struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
  4. bool again = true;
  5. int work = 0;
  6. /* Check if we have pending ipi, its better to send them now,
  7. * not waiting net_rx_action() end.
  8. */
  9. if (sd_has_rps_ipi_waiting(sd)) {
  10. local_irq_disable();
  11. net_rps_action_and_irq_enable(sd);
  12. }
  13. napi->weight = dev_rx_weight;
  14. while (again) {
  15. struct sk_buff *skb;
  16. while ((skb = __skb_dequeue(&sd->process_queue))) { //从队列头部读取一个skb
  17. rcu_read_lock();
  18. __netif_receive_skb(skb); //调用改函数将skb传给网路层
  19. rcu_read_unlock();
  20. input_queue_head_incr(sd); //将队列头部往后偏移一个单位
  21. if (++work >= quota)
  22. return work;
  23. }
  1.                 local_irq_disable();
  2.                 rps_lock(sd);
  3.                 if (skb_queue_empty(&sd->input_pkt_queue)) { //如果队列为空,表示skb读取完了
  4.                         /*
  5.                          * Inline a custom version of __napi_complete().
  6.                          * only current cpu owns and manipulates this napi,
  7.                          * and NAPI_STATE_SCHED is the only possible flag set
  8.                          * on backlog.
  9.                          * We can use a plain write instead of clear_bit(),
  10.                          * and we dont need an smp_mb() memory barrier.
  11.                          */
  12.                         napi->state = 0; //状态置0并退出读取循环
  13.                         again = false;
  14.                 } else {
  15.                         skb_queue_splice_tail_init(&sd->input_pkt_queue,
  16.                                                    &sd->process_queue);
  17.                 }
  18.                 rps_unlock(sd);
  19.                 local_irq_enable();
  20.         }
  21.         return work;
  22. }

2. napi_gro_receive()

它主要是将分片的skb进行组装,然后形成一个skb,请自行了解

linux kernel 网络协议栈之GRO(Generic receive offload)


3. netif_receive_skb()

这个函数没什么内容,主要检测一下时间戳,并对时间戳进行更新,然后确认一下有没有开启RPS功能,如果有则将skb交给对应的cpu处理,他最终还是会调用__netif_receive_skb(skb)将skb传送给网路层


下面用e100网卡来总结一下流程:

neif_rx会调用enqueue_to_backlog 将skb存入softnet_data,并调用____napi_schedule函数。
netif_rx===>netif_rx_internal===>enqueue_to_backlog===>____napi_schedule===>net_rx_action===>process_backlog===>__netif_receive_skb
e100网卡的NAPI调用流程入下:
e100_intr===>__napi_schedule===>net_rx_action===>e100_poll===>e100_rx_clean===>e100_rx_indicate===>netif_receive_skb===>__netif_receive_skb


最后我们来看看接口层数据输入的最后一站:__netif_receive_skb(skb)

它封装了__netif_receive_skb_core

  1. static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc)
  2. {
  3. struct packet_type *ptype, *pt_prev; //用于操作包类型
  4. rx_handler_func_t *rx_handler;
  5. struct net_device *orig_dev; //存放报文的原始设备
  6. bool deliver_exact = false; //默认接收失败
  7. int ret = NET_RX_DROP; //默认返回失败
  8. __be16 type;
  9. net_timestamp_check(!netdev_tstamp_prequeue, skb); //check时间戳,并且会更新skb的时间戳,skb->tstamp
  10. trace_netif_receive_skb(skb);
  11. orig_dev = skb->dev; //将原始的dve做一个备份
  12. skb_reset_network_header(skb); //重置network header,此时skb已经指向IP头(没有vlan的情况下)
  1. //把L3、L4的头都指向data数据结构,到这里的时候skb已经处理完L2层的头了  
  2. if (!skb_transport_header_was_set(skb))
  3. skb_reset_transport_header(skb);
  4. skb_reset_mac_len(skb); //重置mac len
  1.         if (skb_skip_tc_classify(skb)) //是否跳过流量控制分类 ?
  2.                 goto skip_classify;
  3.         if (pfmemalloc)
  4.                 goto skip_taps;
  5.         list_for_each_entry_rcu(ptype, &ptype_all, list) { //把包交给特定协议相关的处理函数前,先调用ptype_all中注册的函数
  6. //最常见的为tcpdump,该工具就是从这里拿到所有收到的包的,例如raw socket和tcpdump实现  
  7.                 if (pt_prev)
  8.                         ret = deliver_skb(skb, pt_prev, orig_dev); //将包直接传给应用层
  9.                 pt_prev = ptype; //pt_prev的加入是为了优化,只有当找到下一个匹配的时候,才执行这一次的回调函数
  10.         }
  11.         list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { //设备上注册ptype_all,做相应的处理,更加精细的控制,ptype_all里面包括IP和arp等 
  12.                 if (pt_prev)
  13.                         ret = deliver_skb(skb, pt_prev, orig_dev);
  14.                 pt_prev = ptype;
  15.         }
  16. skip_taps:
  17. #ifdef CONFIG_NET_INGRESS
  18.         if (static_key_false(&ingress_needed)) {
  19.                 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
  20.                 if (!skb)
  21.                         goto out;
  22.                 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
  23.                         goto out;
  24.         }
  25. #endif
  26.         skb_reset_tc(skb);
  27. pt_prev = NULL;
  28. another_round:
  29. skb->skb_iif = skb->dev->ifindex;
  30. __this_cpu_inc(softnet_data.processed);
  31. if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
  32. skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
  33. skb = skb_vlan_untag(skb); //去除vlan tag
  34. if (unlikely(!skb))
  35. goto out;
  36. }
  1. skip_classify:
  2.         if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
  3.                 goto drop;
  4.         if (skb_vlan_tag_present(skb)) { //如果需要将vlan的信息提供给上层,则执行下面的代码
  5.                 if (pt_prev) {
  6.                         ret = deliver_skb(skb, pt_prev, orig_dev);
  7.                         pt_prev = NULL;
  8.                 }
  9.                 if (vlan_do_receive(&skb))
  10.                         goto another_round;
  11.                 else if (unlikely(!skb))
  12.                         goto out;
  13.         }
  14.         rx_handler = rcu_dereference(skb->dev->rx_handler); //设备rx_handler,加入OVS时会注册为OVS的入口函数  
  15.         if (rx_handler) {
  16.                 if (pt_prev) {
  17.                         ret = deliver_skb(skb, pt_prev, orig_dev);
  18.                         pt_prev = NULL;
  19.                 }
  20.                 switch (rx_handler(&skb)) { //执行rx_handler处理,例如进入OVS,OVS不支持报头中携带vlan的报文  
  21.                 case RX_HANDLER_CONSUMED:
  22.                         ret = NET_RX_SUCCESS;
  23.                         goto out;
  24.                 case RX_HANDLER_ANOTHER:
  25.                         goto another_round;
  26.                 case RX_HANDLER_EXACT:
  27.                         deliver_exact = true;
  28.                 case RX_HANDLER_PASS:
  29.                         break;
  30.                 default:
  31.                         BUG();
  32.                 }
  33.         }
  34.         if (unlikely(skb_vlan_tag_present(skb))) {
  1.                 if (skb_vlan_tag_get_id(skb))
  2.                         skb->pkt_type = PACKET_OTHERHOST;
  3.                 /* Note: we might in the future use prio bits
  4.                  * and set skb->priority like in vlan_do_receive()
  5.                  * For the time being, just ignore Priority Code Point
  6.                  */
  7.                 skb->vlan_tci = 0;
  8.         }
  9.         type = skb->protocol;
  10.         /* deliver only exact match when indicated */
  11.         if (likely(!deliver_exact)) {
  12.                 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, //根据全局定义的协议处理报文 
  13.                                        &ptype_base[ntohs(type) &
  14.                                                    PTYPE_HASH_MASK]);
  15.         }
  16.         deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, //根据设备上注册的协议进行处理   
  17.                                &orig_dev->ptype_specific);
  18.         if (unlikely(skb->dev != orig_dev)) {
  19.                 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, //如果设备发生变化,那么还需要针对新设备的注册协议进行处理
  20.                                        &skb->dev->ptype_specific);
  21.         }
  22.         if (pt_prev) {
  23.                 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  24.                         goto drop;
  25.                 else
  26.                         ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev); //调用协议处理  
  27.         } else {
  28. drop:
  29.                 if (!deliver_exact)
  30.                         atomic_long_inc(&skb->dev->rx_dropped);
  31.                 else
  32.                         atomic_long_inc(&skb->dev->rx_nohandler);
  33.                 kfree_skb(skb);
  34.                 /* Jamal, now you will not able to escape explaining
  1.                  * me how you were going to use this. :-)
  2.                  */
  3.                 ret = NET_RX_DROP;
  4.         }
  5. out:
  6.         return ret;
  7. }

上面的代码可能看着不是很清晰,可能会有疑问:就这几个指针是怎么实现将数据包从接口层往网络层传递的呢?其实主要原因是对ptype_base和ptype_all这两个对象的印象不够直观,我们接下来着看这两个对象的组成就明白了:



从图中可以看到,ptype_all是一个链表,这个链表里面最大的区别是func=packet_rcv,也就是说,这个链表往往是提供给一些抓包程序使用的,比如tcp_dump,它可以不区分包的类型而将所有的包的抓取过来,它的统一处理函数都是packet_rcv,在这里面可以对一些过滤选项进行处理。对象中的type一般使用的是以太网类型,而dev表示在哪个接口上抓包。

但是ptype_base则是一个哈希表,注意这个表是以type来进行分类的,比如ip协议可以指定不同的dev接口,但是他们都在同一张表上。不同的协议类型对应了不同的接收函数,比如IP报文的接收函数是ip_rcv, 802.2对应的是llc_rcv等。总的来说,报文从网卡驱动里面上来以后,第一次在这里进行分流,不同的报文类型交给不同的协议处理函数进行处理(我们这里暂时先不考虑桥接)。

到这里就结束了数据包从驱动接收,然后通过接口层传送给网路层的过程。后面的文章将介绍一个报文怎么在网络层通过接口层传送给网卡驱动。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/252969
推荐阅读
相关标签
  

闽ICP备14008679号