linux通知链相关

来源:互联网 发布:机器人编程入门 小学生 编辑:程序博客网 时间:2024/04/29 13:38


一.头文件

[cpp] view plaincopy
  1. #include <linux/notifier.h>  

二.结构体

[cpp] view plaincopy
  1. //通知块   
  2. struct notifier_block {  
  3.     int (*notifier_call)(struct notifier_block *, unsigned longvoid *);   //回调函数  
  4.     struct notifier_block __rcu *next;  //指向通知链表的下一项  
  5.     int priority;   //优先级  
  6. };  
  7. //原子通知链 运行在中断上下文,不允许阻塞  
  8. struct atomic_notifier_head {  
  9.     spinlock_t lock;  
  10.     struct notifier_block __rcu *head;  
  11. };  
  12. //阻塞通知链 运行在进程上下文,允许阻塞  
  13. struct blocking_notifier_head {  
  14.     struct rw_semaphore rwsem;  
  15.     struct notifier_block __rcu *head;  
  16. };  
  17. //原始通知链,锁和保护机制由调用者维护  
  18. struct raw_notifier_head {  
  19.     struct notifier_block __rcu *head;  
  20. };  
  21. //SRCU通知链 阻塞通知链的变体  
  22. struct srcu_notifier_head {  
  23.     struct mutex mutex;  
  24.     struct srcu_struct srcu;  
  25.     struct notifier_block __rcu *head;  
  26. };  

三.通知链头初始化

调用定义好的宏初始化通知链表头

[cpp] view plaincopy
  1. #define ATOMIC_NOTIFIER_HEAD(name)              \  
  2.     struct atomic_notifier_head name =  ATOMIC_NOTIFIER_INIT(name)  
  3. #define BLOCKING_NOTIFIER_HEAD(name)                \  
  4.     struct blocking_notifier_head name =    BLOCKING_NOTIFIER_INIT(name)  
  5. #define RAW_NOTIFIER_HEAD(name)             \  
  6.     struct raw_notifier_head name = RAW_NOTIFIER_INIT(name)  

定义通知链表头结构体,然后初始化赋值

[cpp] view plaincopy
  1. #define ATOMIC_NOTIFIER_INIT(name) {                \  
  2.         .lock = __SPIN_LOCK_UNLOCKED(name.lock),    \  
  3.         .head = NULL }  
  4. #define BLOCKING_NOTIFIER_INIT(name) {          \  
  5.         .rwsem = __RWSEM_INITIALIZER((name).rwsem), \  
  6.         .head = NULL }  
  7. #define RAW_NOTIFIER_INIT(name) {           \  
  8.         .head = NULL }  

SRCU通知链不支持静态调用

四.注册注销通知块

1.注册通知块

[cpp] view plaincopy
  1. int atomic_notifier_chain_register(struct atomic_notifier_head *nh,struct notifier_block *nb);  
  2. int blocking_notifier_chain_register(struct blocking_notifier_head *nh,struct notifier_block *nb);  
  3. int raw_notifier_chain_register(struct raw_notifier_head *nh,struct notifier_block *nb);  
  4. int srcu_notifier_chain_register(struct srcu_notifier_head *nh,struct notifier_block *nb);  
  5. int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,struct notifier_block *nb);  

最终调用notifier_chain_register

[cpp] view plaincopy
  1. static int notifier_chain_register(struct notifier_block **nl,struct notifier_block *n)  
  2. {  
  3.     while ((*nl) != NULL) { //若链表项非空  
  4.         if (n->priority > (*nl)->priority)  
  5.             break;  
  6.         nl = &((*nl)->next); //则nl指向其下一链表项  
  7.     }  
  8.     n->next = *nl;   //将通知链表项添加进通知链表  
  9.     rcu_assign_pointer(*nl, n);  
  10.     return 0;  
  11. }  

2.注销通知块

[cpp] view plaincopy
  1. int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,struct notifier_block *n)  
  2. int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,struct notifier_block *n)  
  3. int raw_notifier_chain_unregister(struct raw_notifier_head *nh,struct notifier_block *n)  
  4. int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,struct notifier_block *n)  

最终调用

[cpp] view plaincopy
  1. static int notifier_chain_unregister(struct notifier_block **nl,struct notifier_block *n)  
  2. {  
  3.     while ((*nl) != NULL) {  
  4.         if ((*nl) == n) {  
  5.             rcu_assign_pointer(*nl, n->next);  
  6.             return 0;  
  7.         }  
  8.         nl = &((*nl)->next); //从通知链表中移除通知块  
  9.     }  
  10.     return -ENOENT;  
  11. }  

五.通知通知链

[cpp] view plaincopy
  1. int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,unsigned long val, void *v)  
  2. int blocking_notifier_call_chain(struct blocking_notifier_head *nh,unsigned long val, void *v)  
  3. int raw_notifier_call_chain(struct raw_notifier_head *nh,unsigned long val, void *v)  
  4. int srcu_notifier_call_chain(struct srcu_notifier_head *nh,unsigned long val, void *v)  

最终会调用

[cpp] view plaincopy
  1. static int __kprobes notifier_call_chain(struct notifier_block **nl,unsigned long val, void *v,int nr_to_call,  int *nr_calls)  
  2. {  
  3.     int ret = NOTIFY_DONE;  
  4.     struct notifier_block *nb, *next_nb;  
  5.   
  6.     nb = rcu_dereference_raw(*nl);  
  7.   
  8.     while (nb && nr_to_call) {  //遍历整个通知链  
  9.         next_nb = rcu_dereference_raw(nb->next);  
  10.   
  11. #ifdef CONFIG_DEBUG_NOTIFIERS  
  12.         if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {  
  13.             WARN(1, "Invalid notifier called!");  
  14.             nb = next_nb;  
  15.             continue;  
  16.         }  
  17. #endif  
  18.         ret = nb->notifier_call(nb, val, v); //调用通知块的notifier_call函数  
  19.   
  20.         if (nr_calls)  
  21.             (*nr_calls)++;  
  22.   
  23.         if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)  
  24.             break;  
  25.         nb = next_nb;   //指向下一通知块  
  26.         nr_to_call--;  
  27.     }  
  28.     return ret;  
  29. }  

六.运用

发送通知者:定义通知块及回调函数,注册通知块

接收通知者:,通知通知链

在drivers/usb/core/usb.c中
定义了通识块及其回调函数
static struct notifier_block usb_bus_nb = {
 .notifier_call = usb_bus_notify,
};
在drivers/usb/core/usb.c中usb_init函数中
注册了总线通知链
 retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
总线通知链中注册了阻塞通知链
int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
{
 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
}

通知者
1.有设备添加
device_register
 device_add
 
 if (dev->bus)
  blocking_notifier_call_chain(&dev->bus->p->bus_notifier,BUS_NOTIFY_ADD_DEVICE, dev);
2.有设备移除
device_unregister
 device_del 
 
 if (dev->bus)
  blocking_notifier_call_chain(&dev->bus->p->bus_notifier,BUS_NOTIFY_DEL_DEVICE, dev);

通知链回调函数

[cpp] view plaincopy
  1. static int usb_bus_notify(struct notifier_block *nb, unsigned long action,void *data)  
  2. {  
  3.     struct device *dev = data;  
  4.     //回调函数根据传递进来的参数获得设备文件及行为标识,对设备添加和移除做分别处理  
  5.     switch (action) {  
  6.     case BUS_NOTIFY_ADD_DEVICE:  
  7.         if (dev->type == &usb_device_type)  
  8.             (void) usb_create_sysfs_dev_files(to_usb_device(dev));  
  9.         else if (dev->type == &usb_if_device_type)  
  10.             (void) usb_create_sysfs_intf_files(to_usb_interface(dev));  
  11.         break;  
  12.   
  13.     case BUS_NOTIFY_DEL_DEVICE:  
  14.         if (dev->type == &usb_device_type)  
  15.             usb_remove_sysfs_dev_files(to_usb_device(dev));  
  16.         else if (dev->type == &usb_if_device_type)  
  17.             usb_remove_sysfs_intf_files(to_usb_interface(dev));  
  18.         break;  
  19.     }  
  20.     return 0;  

==================


在Linux内核中,各个子系统之间有很强的相互关系,某些子系统可能对其它子系统产生的事件感兴趣。为了让某个子系统在发生某个事件时通知感兴趣的子系统,Linux内核引入了通知链技术。通知链只能够在内核的子系统之间使用,而不能够在内核和用户空间进行事件的通知。

1 数据结构:
通知链有四种类型:

·原子通知链( Atomic notifier chains ):通知链元素的回调函数(当事件发生时要执行的函数)只能在中断上下文中运行,不允许阻塞。对应的链表头结构:

struct atomic_notifier_head {

spinlock_t lock;

struct notifier_block *head;

};

·可阻塞通知链( Blocking notifier chains ):通知链元素的回调函数在进程上下文中运行,允许阻塞。对应的链表头:

struct blocking_notifier_head {

struct rw_semaphore rwsem;

struct notifier_block *head;

};

·原始通知链( Raw notifier chains ):对通知链元素的回调函数没有任何限制,所有锁和保护机制都由调用者维护。对应的链表头:

struct raw_notifier_head {

struct notifier_block *head;

};

·SRCU 通知链( SRCU notifier chains ):可阻塞通知链的一种变体。对应的链表头:

struct srcu_notifier_head {

struct mutex mutex;

struct srcu_struct srcu;

struct notifier_block *head;

};

通知链的核心结构:

struct notifier_block {

int (*notifier_call)(struct notifier_block *, unsigned long, void*);//回调函数指针

struct notifier_block *next;

int priority;

};

其中notifier_call是通知链要执行的函数指针,next用来连接其它的通知结构,priority是这个通知的优先级,同一条链上的notifier_block{}是按优先级排列的。内核代码中一般把通知链命名为xxx_chain, xxx_nofitier_chain这种形式的变量名。


2 运作机制
通知链的运作机制包括两个角色:

·被通知者:对某一事件感兴趣一方。定义了当事件发生时,相应的处理函数,即回调函数。但需要事先将其注册到通知链中(被通知者注册的动作就是在通知链中增加一项)。

·通知者:事件的通知者。当检测到某事件,或者本身产生事件时,通知所有对该事件感兴趣的一方事件发生。他定义了一个通知链,其中保存了每一个被通知者对事件的处理函数(回调函数)。通知这个过程实际上就是遍历通知链中的每一项,然后调用相应的事件处理函数。

包括以下过程:

·通知者定义通知链

·被通知者向通知链中注册回调函数

·当事件发生时,通知者发出通知(执行通知链中所有元素的回调函数)

被通知者调用 notifier_chain_register 函数注册回调函数,该函数按照优先级将回调函数加入到通知链中,注意二重指针的使用

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);二重指针指向notifier_block 的next字段

}

n->next = *nl;

rcu_assign_pointer(*nl, n);//rcu同步,将n赋值给*nl

return 0;

}

注销回调函数则使用 notifier_chain_unregister 函数,即将回调函数从通知链中删除:

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);//二重指针指向notifier_block 的next字段

}

return -ENOENT;

}

通知者调用 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(*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;

}

参数nl是通知链的头部,val表示事件类型,v用来指向通知链上的函数执行时需要用到的参数,一般不同的通知链,参数类型也不一样,例如当通知一个网卡被注册时,v就指向net_device结构,nr_to_call表示准备最多通知几个,-1表示整条链都通知,nr_calls非空的话,返回通知了多少个。

每个被执行的notifier_block回调函数的返回值可能取值为以下几个:

·NOTIFY_DONE:表示对相关的事件类型不关心

·NOTIFY_OK:顺利执行

·NOTIFY_BAD:执行有错

·NOTIFY_STOP:停止执行后面的回调函数

·NOTIFY_STOP_MASK:停止执行的掩码

Notifier_call_chain()把最后一个被调用的回调函数的返回值作为它的返回值。

内核网络代码中对通知链的使用:
内核网络部分使用的一些通知链:
·inetaddr_chain:ipv4地址变动时的通知链
·netdev_chain:网络设备状态变动时的通知链
网络代码中对通知链的调用一般都有一个包装函数,例如对netdev_chain的注册就是由register_netdevice_notifier()函数完成的:
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;
}
这个函数主要完成两件事情:
1)把参数struct notifier_block *nb注册到netdev_chain通知链上;
2)系统中所有已经被注册过的或者激活的网络设备的事件都要被新增的这个通知的回调函数重新调用一遍,使设备更新到一个完整的状态。
dev_boot_phase定义如下,表示在启动阶段。
static int dev_boot_phase = 1;
例如,在启动阶段的网络模块初始化过程中,有一个调用过程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()实现为:
static int inetdev_event(struct notifier_block *this, unsigned long event,
void *ptr)//event为触发的事件类型
{
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为空时,调用inetdev_init()函数分配一个struct in_device,此时如果是Loopback设备才有动作了。





0 0
原创粉丝点击