Chinaunix首页 | 论坛 | 博客
  • 博客访问: 179493
  • 博文数量: 37
  • 博客积分: 171
  • 博客等级: 入伍新兵
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-13 22:54
个人简介

寻找方向程序猿、攻城狮

文章存档

2023年(1)

2022年(4)

2019年(1)

2018年(1)

2017年(1)

2015年(2)

2014年(19)

2013年(2)

2012年(1)

2011年(5)

分类: LINUX

2018-01-13 23:50:22

net/core/dev.c的__netif_receive_skb函数中,函数末尾需要匹配相应的协议处理函数以此上传数据包:

  1. list_for_each_entry_rcu(ptype,
  2.             &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) {
  3.         if (ptype->type == type && (ptype->dev == null_or_orig ||
  4.          ptype->dev == skb->dev || ptype->dev == orig_dev ||
  5.          ptype->dev == orig_or_bond)) {
  6.             if (pt_prev)
  7.                 ret = deliver_skb(skb, pt_prev, orig_dev);
  8.             pt_prev = ptype;
  9.         }
  10.     }
1 list_for_each_entry_rcu宏的实现如下:

  1. /**
  2.  * list_for_each_entry_rcu    -    iterate over rcu list of given type 在指定类型的rcu链表上迭代(遍历)
  3.  * @pos:    the type * to use as a loop cursor. 类型为type*的迭代游标
  4.  * @head:    the head for your list.链表头
  5.  * @member:    the name of the list_struct within the struct. type数据结构中链表结构的名字,一般指向链表结构的指针
  6.  *
  7.  * This list-traversal primitive may safely run concurrently with
  8.  * the _rcu list-mutation primitives such as list_add_rcu()
  9.  * as long as the traversal is guarded by rcu_read_lock(). 调用这个链表遍历的宏之前需要调用rcu_read_lock函数,这样这个链表遍历原语可以在并发的情况下安全地遍历通过list_add_rcu()函数构造的链表。
  10.  */
  11. #define list_for_each_entry_rcu(pos, head, member) \
  12.     for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \  list_entry_rcu宏的实现如下,相信看到container_of都不会陌生。取得链表头
  13.         prefetch(pos->member.next), &pos->member != (head); \ 判断是否已经遍历完。prefetch主要是告诉处理器预取指定变量的值以提高处理效率,但实际上根据实践来看可能让链表遍历更慢,可以查找相关文章看看。
  14.         pos = list_entry_rcu(pos->member.next, typeof(*pos), member)) 取得下一个元素



点击(此处)折叠或打开

  1. /**
  2.  * list_entry_rcu - get the struct for this entry
  3.  * @ptr: the &struct list_head pointer.
  4.  * @type: the type of the struct this is embedded in.
  5.  * @member: the name of the list_struct within the struct.
  6.  *
  7.  * This primitive may safely run concurrently with the _rcu list-mutation
  8.  * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  9.  */
  10. #define list_entry_rcu(ptr, type, member) \
  11.     container_of(rcu_dereference_raw(ptr), type, member) 其实这个链表遍历之所以用rcu做后缀,主要是这儿的指针调用了rcu_dereference_raw的使用,其中调用了smp_read_barrier_depends宏,以确保取出的指针值是更新后的值,不是旧值。
继续看下面packet的type(协议类型)的匹配
  1.  if (ptype->type == type && (ptype->dev == null_or_orig ||
  2.          ptype->dev == skb->dev || ptype->dev == orig_dev ||
  3.          ptype->dev == orig_or_bond)) {
  4.             if (pt_prev)
  5.                 ret = deliver_skb(skb, pt_prev, orig_dev);
  6.             pt_prev = ptype;
  7.         }
如果第一次找到类型匹配的协议处理结构,那么现用pt_prev存下来,然后如果找到第二个那么前面存储下来的那个结构就要先进行处理了,直到最后一个,最后一个不会调用deliver_skb函数对数据包进行处理,而是调用其自己的函数进行处理,如下:

  1. if (pt_prev) {
  2.         ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
  3.     } else {
  4.         kfree_skb(skb);
  5.         /* Jamal, now you will not able to escape explaining
  6.          * me how you were going to use this. :-)
  7.          */
  8.         ret = NET_RX_DROP;
  9.     }
实际上linux网络协议栈中很少会注册多个type相同的协议处理结构,所以一般会执行pt_prev->func这个函数指针。

阅读(1311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~