轉(zhuǎn)載自 http://blog.csdn.net/jjbear_ustc/archive/2009/12/22/5057009.aspx
在Linux內(nèi)核中,各個子系統(tǒng)之間有很強的相互關(guān)系,某些子系統(tǒng)可能對其它子系統(tǒng)產(chǎn)生的事件感興趣。為了讓某個子系統(tǒng)在發(fā)生某個事件時通知感興趣的子系統(tǒng),Linux內(nèi)核引入了通知鏈技術(shù)。通知鏈只能夠在內(nèi)核的子系統(tǒng)之間使用,而不能夠在內(nèi)核和用戶空間進(jìn)行事件的通知。
1 數(shù)據(jù)結(jié)構(gòu):
通知鏈有四種類型:
·原子通知鏈( Atomic notifier chains ):通知鏈元素的回調(diào)函數(shù)(當(dāng)事件發(fā)生時要執(zhí)行的函數(shù))只能在中斷上下文中運行,不允許阻塞。對應(yīng)的鏈表頭結(jié)構(gòu):
struct atomic_notifier_head {
spinlock_t lock;
struct notifier_block *head;
};
·可阻塞通知鏈( Blocking notifier chains ):通知鏈元素的回調(diào)函數(shù)在進(jìn)程上下文中運行,允許阻塞。對應(yīng)的鏈表頭:
struct blocking_notifier_head {
struct rw_semaphore rwsem;
struct notifier_block *head;
};
·原始通知鏈( Raw notifier chains ):對通知鏈元素的回調(diào)函數(shù)沒有任何限制,所有鎖和保護機制都由調(diào)用者維護。對應(yīng)的鏈表頭:
struct raw_notifier_head {
struct notifier_block *head;
};
·SRCU 通知鏈( SRCU notifier chains ):可阻塞通知鏈的一種變體。對應(yīng)的鏈表頭:
struct srcu_notifier_head {
struct mutex mutex;
struct srcu_struct srcu;
struct notifier_block *head;
};
通知鏈的核心結(jié)構(gòu):
struct notifier_block {
int (*notifier_call)(struct notifier_block *, unsigned long, void *);
struct notifier_block *next;
int priority;
};
其中notifier_call是通知鏈要執(zhí)行的函數(shù)指針,next用來連接其它的通知結(jié)構(gòu),priority是這個通知的優(yōu)先級,同一條鏈上的notifier_block{}是按優(yōu)先級排列的。內(nèi)核代碼中一般把通知鏈命名為xxx_chain, xxx_nofitier_chain這種形式的變量名。
2 運作機制
通知鏈的運作機制包括兩個角色:
·被通知者:對某一事件感興趣一方。定義了當(dāng)事件發(fā)生時,相應(yīng)的處理函數(shù),即回調(diào)函數(shù)。但需要事先將其注冊到通知鏈中(被通知者注冊的動作就是在通知鏈中增加一項)。
·通知者:事件的通知者。當(dāng)檢測到某事件,或者本身產(chǎn)生事件時,通知所有對該事件感興趣的一方事件發(fā)生。他定義了一個通知鏈,其中保存了每一個被通知者對事件的處理函數(shù)(回調(diào)函數(shù))。通知這個過程實際上就是遍歷通知鏈中的每一項,然后調(diào)用相應(yīng)的事件處理函數(shù)。
包括以下過程:
·通知者定義通知鏈
·被通知者向通知鏈中注冊回調(diào)函數(shù)
·當(dāng)事件發(fā)生時,通知者發(fā)出通知(執(zhí)行通知鏈中所有元素的回調(diào)函數(shù))
被通知者調(diào)用 notifier_chain_register 函數(shù)注冊回調(diào)函數(shù),該函數(shù)按照優(yōu)先級將回調(diào)函數(shù)加入到通知鏈中:
static int notifier_chain_register(struct notifier_block **nl,
struct notifier_block *n)
{
while ((*nl) != NULL) {
if (n->priority > (*nl)->priority)
break;
nl = &((*nl)->next);
}
n->next = *nl;
rcu_assign_pointer(*nl, n);
return 0;
}
注銷回調(diào)函數(shù)則使用 notifier_chain_unregister 函數(shù),即將回調(diào)函數(shù)從通知鏈中刪除:
static int notifier_chain_unregister(struct notifier_block **nl,
struct notifier_block *n)
{
while ((*nl) != NULL) {
if ((*nl) == n) {
rcu_assign_pointer(*nl, n->next);
return 0;
}
nl = &((*nl)->next);
}
return -ENOENT;
}
通知者調(diào)用 notifier_call_chain 函數(shù)通知事件的到達(dá),這個函數(shù)會遍歷通知鏈中所有的元素,然后依次調(diào)用每一個的回調(diào)函數(shù)(即完成通知動作):
/**
* notifier_call_chain - Informs the registered notifiers about an event.
* @nl: Pointer to head of the blocking notifier chain
* @val: Value passed unmodified to notifier function
* @v: Pointer passed unmodified to notifier function
* @nr_to_call: Number of notifier functions to be called. Don't care
* value of this parameter is -1.
* @nr_calls: Records the number of notifications sent. Don't care
* value of this field is NULL.
* @returns: notifier_call_chain returns the value returned by the
* last notifier function called.
*/
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(*nl);
while (nb && nr_to_call) {
next_nb = rcu_dereference(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;
}
參數(shù)nl是通知鏈的頭部,val表示事件類型,v用來指向通知鏈上的函數(shù)執(zhí)行時需要用到的參數(shù),一般不同的通知鏈,參數(shù)類型也不一樣,例如當(dāng)通知一個網(wǎng)卡被注冊時,v就指向net_device結(jié)構(gòu),nr_to_call表示準(zhǔn)備最多通知幾個,-1表示整條鏈都通知,nr_calls非空的話,返回通知了多少個。
每個被執(zhí)行的notifier_block回調(diào)函數(shù)的返回值可能取值為以下幾個:
·NOTIFY_DONE:表示對相關(guān)的事件類型不關(guān)心
·NOTIFY_OK:順利執(zhí)行
·NOTIFY_BAD:執(zhí)行有錯
·NOTIFY_STOP:停止執(zhí)行后面的回調(diào)函數(shù)
·NOTIFY_STOP_MASK:停止執(zhí)行的掩碼
Notifier_call_chain()把最后一個被調(diào)用的回調(diào)函數(shù)的返回值作為它的返回值。
3 內(nèi)核網(wǎng)絡(luò)代碼中對通知鏈的使用
內(nèi)核網(wǎng)絡(luò)部分使用的一些通知鏈:
·inetaddr_chain:ipv4地址變動時的通知鏈
·netdev_chain:網(wǎng)絡(luò)設(shè)備狀態(tài)變動時的通知鏈
網(wǎng)絡(luò)代碼中對通知鏈的調(diào)用一般都有一個包裝函數(shù),例如對netdev_chain的注冊就是由register_netdevice_notifier()函數(shù)完成的:
int register_netdevice_notifier(struct notifier_block *nb)
{
struct net_device *dev;
struct net_device *last;
struct net *net;
int err;
rtnl_lock();
err = raw_notifier_chain_register(&netdev_chain, nb);
if (err)
goto unlock;
if (dev_boot_phase)
goto unlock;
for_each_net(net) {
for_each_netdev(net, dev) {
err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
err = notifier_to_errno(err);
if (err)
goto rollback;
if (!(dev->flags & IFF_UP))
continue;
nb->notifier_call(nb, NETDEV_UP, dev);
}
}
unlock:
rtnl_unlock();
return err;
rollback:
last = dev;
for_each_net(net) {
for_each_netdev(net, dev) {
if (dev == last)
break;
if (dev->flags & IFF_UP) {
nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
nb->notifier_call(nb, NETDEV_DOWN, dev);
}
nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
}
}
raw_notifier_chain_unregister(&netdev_chain, nb);
goto unlock;
}
這個函數(shù)主要完成兩件事情:
1)把參數(shù)struct notifier_block *nb注冊到netdev_chain通知鏈上;
2)系統(tǒng)中所有已經(jīng)被注冊過的或者激活的網(wǎng)絡(luò)設(shè)備的事件都要被新增的這個通知的回調(diào)函數(shù)重新調(diào)用一遍,使設(shè)備更新到一個完整的狀態(tài)。
dev_boot_phase定義如下,表示在啟動階段。
static int dev_boot_phase = 1;
例如,在啟動階段的網(wǎng)絡(luò)模塊初始化過程中,有一個調(diào)用過程inet_init()-->ip_init()-->ip_rt_init()-->devinet_init(),會注冊一個ip_netdev_notifier通知鏈:
register_netdevice_notifier(&ip_netdev_notifier);
而ip_netdev_notifier定義為:
static struct notifier_block ip_netdev_notifier = {
.notifier_call = inetdev_event,
};
inetdev_event()實現(xiàn)為:
static int inetdev_event(struct notifier_block *this, unsigned long event,
void *ptr)
{
struct net_device *dev = ptr;
struct in_device *in_dev = __in_dev_get_rtnl(dev);
ASSERT_RTNL();
if (!in_dev) {
if (event == NETDEV_REGISTER) {
in_dev = inetdev_init(dev);
if (!in_dev)
return notifier_from_errno(-ENOMEM);
if (dev->flags & IFF_LOOPBACK) {
IN_DEV_CONF_SET(in_dev, NOXFRM, 1);
IN_DEV_CONF_SET(in_dev, NOPOLICY, 1);
}
} else if (event == NETDEV_CHANGEMTU) {
/* Re-enabling IP */
if (inetdev_valid_mtu(dev->mtu))
in_dev = inetdev_init(dev);
}
goto out;
}
switch (event) {
case NETDEV_REGISTER:
printk(KERN_DEBUG "inetdev_event: bug\n");
dev->ip_ptr = NULL;
break;
case NETDEV_UP:
if (!inetdev_valid_mtu(dev->mtu))
break;
if (dev->flags & IFF_LOOPBACK) {
struct in_ifaddr *ifa;
if ((ifa = inet_alloc_ifa()) != NULL) {
ifa->ifa_local =
? ifa->ifa_address = htonl(INADDR_LOOPBACK);
ifa->ifa_prefixlen = 8;
ifa->ifa_mask = inet_make_mask(8);
in_dev_hold(in_dev);
ifa->ifa_dev = in_dev;
ifa->ifa_scope = RT_SCOPE_HOST;
memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
inet_insert_ifa(ifa);
}
}
ip_mc_up(in_dev);
/* fall through */
case NETDEV_CHANGEADDR:
if (IN_DEV_ARP_NOTIFY(in_dev))
arp_send(ARPOP_REQUEST, ETH_P_ARP,
in_dev->ifa_list->ifa_address,
dev,
in_dev->ifa_list->ifa_address,
NULL, dev->dev_addr, NULL);
break;
case NETDEV_DOWN:
ip_mc_down(in_dev);
break;
case NETDEV_CHANGEMTU:
if (inetdev_valid_mtu(dev->mtu))
break;
/* disable IP when MTU is not enough */
case NETDEV_UNREGISTER:
inetdev_destroy(in_dev);
break;
case NETDEV_CHANGENAME:
/* Do not notify about label change, this event is
* not interesting to applications using netlink.
*/
inetdev_changename(dev, in_dev);
devinet_sysctl_unregister(in_dev);
devinet_sysctl_register(in_dev);
break;
}
out:
return NOTIFY_DONE;
}
在注冊的時候傳遞的是NETDEV_REGISTER事件,所以在in_dev不為空時,只做switch語句中的一個動作:dev->ip_ptr = NULL;在in_dev為空時,調(diào)用inetdev_init()函數(shù)分配一個struct in_device,此時如果是Loopback設(shè)備才有動作了。
4 舉例
這個例子由參考文章二給出。
? 在這里,寫了一個簡單的通知鏈表的代碼。
? 實際上,整個通知鏈的編寫也就兩個過程:
??? 首先是定義自己的通知鏈的頭節(jié)點,并將要執(zhí)行的函數(shù)注冊到自己的通知鏈中。
??? 其次則是由另外的子系統(tǒng)來通知這個鏈,讓其上面注冊的函數(shù)運行。
??? 這里將第一個過程分成了兩步來寫,第一步是定義了頭節(jié)點和一些自定義的注冊函數(shù)(針對該頭節(jié)點的),第二步則是使用自定義的注冊函數(shù)注冊了一些通知鏈節(jié)點。分別在代碼buildchain.c與regchain.c中。
??? 發(fā)送通知信息的代碼為notify.c。
代碼1 buildchain.c
??? 它的作用是自定義一個通知鏈表test_chain,然后再自定義兩個函數(shù)分別向這個通知鏈中加入或刪除節(jié)點,最后再定義一個函數(shù)通知這個test_chain鏈。
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
/*
* 定義自己的通知鏈頭結(jié)點以及注冊和卸載通知鏈的外包函數(shù)
*/
/*
* RAW_NOTIFIER_HEAD是定義一個通知鏈的頭部結(jié)點,
* 通過這個頭部結(jié)點可以找到這個鏈中的其它所有的notifier_block
*/
static RAW_NOTIFIER_HEAD(test_chain);
/*
* 自定義的注冊函數(shù),將notifier_block節(jié)點加到剛剛定義的test_chain這個鏈表中來
* raw_notifier_chain_register會調(diào)用notifier_chain_register
*/
int register_test_notifier(struct notifier_block *nb)
{
??????? return raw_notifier_chain_register(&test_chain, nb);
}
EXPORT_SYMBOL(register_test_notifier);
int unregister_test_notifier(struct notifier_block *nb)
{
??????? return raw_notifier_chain_unregister(&test_chain, nb);
}
EXPORT_SYMBOL(unregister_test_notifier);
/*
* 自定義的通知鏈表的函數(shù),即通知test_chain指向的鏈表中的所有節(jié)點執(zhí)行相應(yīng)的函數(shù)
*/
int test_notifier_call_chain(unsigned long val, void *v)
{
??????? return raw_notifier_call_chain(&test_chain, val, v);
}
EXPORT_SYMBOL(test_notifier_call_chain);
/*
* init and exit
*/
static int __init init_notifier(void)
{
??????? printk("init_notifier\n");
??????? return 0;
}
static void __exit exit_notifier(void)
{
??????? printk("exit_notifier\n");
}
module_init(init_notifier);
module_exit(exit_notifier);
代碼2 regchain.c
??? 該代碼的作用是將test_notifier1 test_notifier2 test_notifier3這三個節(jié)點加到之前定義的test_chain這個通知鏈表上,同時每個節(jié)點都注冊了一個函數(shù)。
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
/*
* 注冊通知鏈
*/
extern int register_test_notifier(struct notifier_block*);
extern int unregister_test_notifier(struct notifier_block*);
static int test_event1(struct notifier_block *this, unsigned long event, void *ptr)
{
??????? printk("In Event 1: Event Number is %d\n", event);
??????? return 0;
}
static int test_event2(struct notifier_block *this, unsigned long event, void *ptr)
{
??????? printk("In Event 2: Event Number is %d\n", event);
??????? return 0;
}
static int test_event3(struct notifier_block *this, unsigned long event, void *ptr)
{
??????? printk("In Event 3: Event Number is %d\n", event);
??????? return 0;
}
/*
* 事件1,該節(jié)點執(zhí)行的函數(shù)為test_event1
*/
static struct notifier_block test_notifier1 =
{
??????? .notifier_call = test_event1,
};
/*
* 事件2,該節(jié)點執(zhí)行的函數(shù)為test_event1
*/
static struct notifier_block test_notifier2 =
{
??????? .notifier_call = test_event2,
};
/*
* 事件3,該節(jié)點執(zhí)行的函數(shù)為test_event1
*/
static struct notifier_block test_notifier3 =
{
??????? .notifier_call = test_event3,
};
/*
* 對這些事件進(jìn)行注冊
*/
static int __init reg_notifier(void)
{
??????? int err;
??????? printk("Begin to register:\n");
??????? err = register_test_notifier(&test_notifier1);
??????? if (err)
??????? {
??????????????? printk("register test_notifier1 error\n");
??????????????? return -1;
??????? }
??????? printk("register test_notifier1 completed\n");
??????? err = register_test_notifier(&test_notifier2);
??????? if (err)
??????? {
??????????????? printk("register test_notifier2 error\n");
??????????????? return -1;
??????? }
??????? printk("register test_notifier2 completed\n");
??????? err = register_test_notifier(&test_notifier3);
??????? if (err)
??????? {
??????????????? printk("register test_notifier3 error\n");
??????????????? return -1;
??????? }
??????? printk("register test_notifier3 completed\n");
??????? return err;
}
/*
* 卸載剛剛注冊了的通知鏈
*/
static void __exit unreg_notifier(void)
{
??????? printk("Begin to unregister\n");
??????? unregister_test_notifier(&test_notifier1);
??????? unregister_test_notifier(&test_notifier2);
??????? unregister_test_notifier(&test_notifier3);
??????? printk("Unregister finished\n");
}
module_init(reg_notifier);
module_exit(unreg_notifier);
代碼3 notify.c
??? 該代碼的作用就是向test_chain通知鏈中發(fā)送消息,讓鏈中的函數(shù)運行。
#include <asm/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/notifier.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
extern int test_notifier_call_chain(unsigned long val, void *v);
/*
* 向通知鏈發(fā)送消息以觸發(fā)注冊了的函數(shù)
*/
static int __init call_notifier(void)
{
??????? int err;
??????? printk("Begin to notify:\n");
/*
* 調(diào)用自定義的函數(shù),向test_chain鏈發(fā)送消息
*/
??????? printk("==============================\n");
??????? err = test_notifier_call_chain(1, NULL);
??????? printk("==============================\n");
??????? if (err)
??????????????? printk("notifier_call_chain error\n");
??????? return err;
}
static void __exit uncall_notifier(void)
{
??????? printk("End notify\n");
}
module_init(call_notifier);
module_exit(uncall_notifier);
Makefile文件(我修改了)
注意,記得先檢查有沒有安裝當(dāng)前l(fā)inux版本的內(nèi)核頭文件:
obj-m:=buildchain.o regchain.o notify.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
KERNELDIR := /usr/src/linux-headers-$(LINUX_KERNEL)
all:
make -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
make -C $(KERNELDIR) M=$(CURRENT_PATH) clean?
運行(注意insmod要root權(quán)限)
make
insmod buildchain.ko
insmod regchain.ko
insmod notify.ko
這樣就可以看到通知鏈運行的效果了
下面是我在自己的機器上面運行得到的結(jié)果(dmesg命令):
init_notifier
Begin to register:
register test_notifier1 completed
register test_notifier2 completed
register test_notifier3 completed
Begin to notify:
==============================
In Event 1: Event Number is 1
In Event 2: Event Number is 1
In Event 3: Event Number is 1
==============================
5 參考文章:
1 http://hi.baidu.com/mczyh/blog/item/80f4200098588c087bec2ce8.html
2 http://www.yuanma.org/data/2009/0427/article_3645.htm
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
