標籤:linux 協議棧 notifier 通知錶鏈
為什麼要有核心通知錶鏈: Linux由多個相互依賴的子系統組成。其中一些子系統可能需要對其他子系統的一些事件感興趣。這樣子系統之間需要一些通訊機制來實現這一功能。 在接觸Notification Chain之前,我們可能想到通過輪詢來實現,事件發生時,子系統輪詢所有其他的子系統,看看有沒有對這一事件感興趣的,有沒有需要執行的子函數。
If (subsystem_X_enabled) {do_something_1}if (subsystem_Y_enabled) {do_something_2}If (subsystem_Z_enabled) {do_something_3}... ... ...
這種方式看起來可以滿足我們的需要,但是代碼太繁雜了,會導致每個子系統變得很龐大。而Notification Chain則大大的改善了這些不足。
Notification Chain基本原理
Notification Chain是一種“發布——訂閱”模型,當某個子系統發生某一事件時,它就通知所有對這一事件感興趣的子系統,以便執行響應函數。在這個模型中,發生事件的子系統是主動端,對事件感興趣的子系統是被動端。(本文也將被動端成為被通知端)。 在事件發生時,檢測或產生事件的子系統作為主動一方通過通知函數來告知作為被動一方的訂閱者(對此事件感興趣的子系統)。這裡有個其他需求,訂閱一方要提供callback函數以供發布方調用,當然,提供什麼樣的callback函數完全由訂閱者決定。
通知錶鏈模型圖:
Notification Chain原始碼解析
結構體:
//通知塊struct notifier_block{int (*notifier_call)(struct notifier_block *self, unsigned long, void *);//回呼函數struct notifier_block *next; //int priority; //註冊的優先順序,使用者自行指定,優先順序越高回呼函數就越早執行};
通知錶鏈的定義Linux核心中有三種通知錶鏈:
//原子通知錶鏈struct atomic_notifier_head { spinlock_t lock; struct notifier_block __rcu *head;}; //可阻塞通知錶鏈struct blocking_notifier_head { struct rw_semaphore rwsem; struct notifier_block __rcu *head;}; //原始通知錶鏈struct raw_notifier_head { struct notifier_block __rcu *head;};//可阻塞通知錶鏈的變體 struct srcu_notifier_head { struct mutex mutex; struct srcu_struct srcu; struct notifier_block __rcu *head;};
需要定義一個通知錶鏈時,用XXX_NOTIFIER_HEAD(name)來定義,下面以RAW_NOTIFIER_HEAD為例:
static RAW_NOTIFIER_HEAD(netdev_chain);
追蹤 RAW_NOTIFIER_HEAD,
#define ATOMIC_NOTIFIER_HEAD(name) struct atomic_notifier_head name = ATOMIC_NOTIFIER_INIT(name)#define BLOCKING_NOTIFIER_HEAD(name) struct blocking_notifier_head name = BLOCKING_NOTIFIER_INIT(name)#define RAW_NOTIFIER_HEAD(name) struct raw_notifier_head name = RAW_NOTIFIER_INIT(name)
繼續
#define ATOMIC_NOTIFIER_INIT(name) { .lock = __SPIN_LOCK_UNLOCKED(name.lock), .head = NULL }#define BLOCKING_NOTIFIER_INIT(name) { .rwsem = __RWSEM_INITIALIZER((name).rwsem), .head = NULL }#define RAW_NOTIFIER_INIT(name) { .head = NULL }/* srcu_notifier_heads cannot be initialized statically */
由此完成了一條通知錶鏈的定義和初始化。
Linux 核心中有許多個notification chain,我們常用到的有三個netdev_chain,inetaddr_chain,inet6addr_chain。結合struct notifier_block,每條chain上有一個回呼函數的隊列(鏈表)notifier_call ,那麼這些回呼函數(notification通知塊)如何使用,合適調用呢。 首先我們在穿件通知錶鏈之初,通知錶鏈只有鏈表頭,沒有其他的notifier_block塊,這個鏈表會關注一些事件(如netdev_chain會關注裝置加入刪除等,具體下文會做更清楚的介紹。)如果某個子系統對這條chain關注的時間感興趣,它就註冊一個notifier_block到鏈表中。notifier_block塊的註冊和登出,以下面形式實現:
int notifier_chain_register(struct notifier_block **list, struct notifier_block *n)int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n)
這兩個是封裝函數,具體的通知鏈都會對這兩個函數進行封裝,定義自己的註冊和登出函數。以netdev_chain為例,封裝過程:
int register_netdevice_notifier(struct notifier_block *nb) { raw_notifier_chain_register(&netdev_chain, nb); }int raw_notifier_chain_register(struct raw_notifier_head *nh, struct notifier_block *n){ return notifier_chain_register(&nh->head, n);}
某個子系統需要關注裝置的變化資訊時,就將處理函數notifier_call(自己編寫的)作為notifier_block註冊的通知錶鏈。
事件發生時,通知錶鏈執行過程 當事件發生時,時間會調用notifier_call_chain遍曆通知錶鏈中每個通知塊裡的回呼函數,由回呼函數確定是否要執行處理。
static int __kprobes notifier_call_chain(struct notifier_block **nl, unsigned long val, void *v, int nr_to_call, int *nr_calls) { int ret = NOTIFY_DONE; struct notifier_block *nb, *next_nb; nb = rcu_dereference_raw(*nl); while (nb && nr_to_call) { next_nb = rcu_dereference_raw(nb->next); #ifdef CONFIG_DEBUG_NOTIFIERS if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) { WARN(1, "Invalid notifier called!"); nb = next_nb; continue; }#endif ret = nb->notifier_call(nb, val, v); if (nr_calls) (*nr_calls)++; if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK) break; nb = next_nb; nr_to_call--; } return ret;}
深入理解Linux網路技術內幕——Notification核心通知錶鏈