《深入理解Linux网络技术内幕》阅读笔记(四)

通知链
数据结构:

 14 struct notifier_block
 15 {
 16         int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
 17         struct notifier_block *next;
 18         int priority;
 19 };

模型:
通知链就是一份简单的函数列表,当给定的事件发生时予以执行。每个函数都让另一个子系统知道,调用此函数的子系统内所发生的一个事件或者子系统所侦测到的一个事件。
因此,通知链是所谓的发布-订阅模型:
1.被通知者就是要求接收某事件的子系统,而且会提供回调函数予以调用。
2.通知者就是感受到一个事件并调用回调函数的子系统。
通知链的注册接口实现:

 83 int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)
 84 {
 85         write_lock(&notifier_lock);
 86         while(*list)
 87         {
 88                 if(n->priority > (*list)->priority)
 89                         break;
 90                 list= &((*list)->next);
 91         }
 92         n->next = *list;
 93         *list=n;
 94         write_unlock(&notifier_lock);
 95         return 0;
 96 }

其中的语句(用的是>而不是>=):

if(n->priority > (*list)->priority)

决定了对于每条链条而言,那些notifier_block实体被插入到一个按优先级排序的列表中。相同的优先级的元素则按插入时间排序:新的元素排在尾端。
实现通知:

141 int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
142 {
143         int ret=NOTIFY_DONE;
144         struct notifier_block *nb = *n;
145 
146         while(nb)
147         {
148                 ret=nb->notifier_call(nb,val,v);
149                 if(ret&NOTIFY_STOP_MASK)
150                 {
151                         return ret;
152                 }
153                 nb=nb->next;
154         }
155         return ret;
156 }

网络子系统几条重要的通知链:
inetaddr_chain:
发送有关本地接口上的IPv4地址的插入,删除以及变更的通知信息。
netdev_chain:
发送有关网络设备注册状态的通知信息。
其它:
网络代码也可以注册其他内核组件产生的通知信息。例如,某些NIC设备驱动程序可以用reboot_notifier_list链注册,当系统重新引导时,此链会发出警告。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章