您的位置 首页 IOT

需求了解Linux内核告诉链机制的原理及完成

需要了解Linux内核通知链机制的原理及实现-大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣。为了满足这个需求,也即是让某个子系统在发生某个事件时通知其它的子系统,Linux内核提供了通知链的机制。通知链表只能够在内核的子系统之间使用,而不能够在内核与用户空间之间进行事件的通知。

一、概念:

大多数内核子体系都是彼此独立的,因而某个子体系或许对其它子体系产生的工作感兴趣。为了满意这个需求,也便是让某个子体系在产生某个工作时告诉其它的子体系,Linux内核供给了告诉链的机制。告诉链表只能够在内核的子体系之间运用,而不能够在内核与用户空间之间进行工作的告诉。 告诉链表是一个函数链表,链表上的每一个节点都注册了一个函数。当某个工作产生时,链表上一切节点对应的函数就会被履行。所以关于告诉链表来说有一个告诉方与一个接收方。在告诉这个工作时所运转的函数由被告诉方决议,实际上也便是被告诉方注册了某个函数,在产生某个工作时这些函数就得到履行。其实和体系调用signal的思维差不多。

二、数据结构:

告诉链有四种类型:

原子告诉链( 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;
};

告诉链的中心结构:

其间notifier_call是告诉链要履行的函数指针,next用来衔接其它的告诉结构,priority是这个告诉的优先级,同一条链上的notifier_block{}是按优先级摆放的。内核代码中一般把告诉链命名为xxx_chain, xxx_nofitier_chain这种方式的变量名。

三、运作机制:

告诉链的运作机制包含两个人物:

被告诉者:对某一工作感兴趣一方。界说了当工作产生时,相应的处理函数,即回调函数。但需求事前将其注册到告诉链中(被告诉者注册的动作便是在告诉链中添加一项)。

告诉者:工作的告诉者。当检测到某工作,或许自身产生工作时,告诉一切对该工作感兴趣的一方工作产生。他界说了一个告诉链,其间保存了每一个被告诉者对工作的处理函数(回调函数)。告诉这个进程实际上便是遍历告诉链中的每一项,然后调用相应的工作处理函数。

包含以下进程:

告诉者界说告诉链。

被告诉者向告诉链中注册回调函数。

当工作产生时,告诉者发出告诉(履行告诉链中一切元素的回调函数)。

被告诉者调用 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);
    }
    
    n->next = *nl;
    rcu_assign_pointer(*nl, n);
    
    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);
    }
    
    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()把最终一个被调用的回调函数的回来值作为它的回来值。

四、举例运用:

在这儿,写了一个简略的告诉链表的代码。

首先是界说自己的告诉链的头节点,并即将履行的函数注册到自己的告诉链中。

其次则是由别的的子体系来告诉这个链,让其上面注册的函数运转。

这儿将第一个进程分成了两步来写,第一步是界说了头节点和一些自界说的注册函数(针对该头节点的),第二步则是运用自界说的注册函数注册了一些告诉链节点。分别在代码buildchain.c与regchain.c中。发送告诉信息的代码为notify.c。

代码1 buildchain.c。它的作用是自界说一个告诉链表test_chain,然后再自界说两个函数分别向这个告诉链中参加或删去节点,最终再界说一个函数告诉这个test_chain链:

#include
#include #include #include #include #include #include #include MODULE_LICENSE(“GPL”);

/*
* 界说自己的告诉链头结点以及注册和卸载告诉链的外包函数
*/

/*
* RAW_NOTIFIER_HEAD是界说一个告诉链的头部结点,
* 经过这个头部结点能够找到这个链中的其它一切的notifier_block
*/
static RAW_NOTIFIER_HEAD(test_chain);

/*
* 自界说的注册函数,将notifier_block节点加到刚刚界说的test_chain这个链表中来
* raw_notifier_chain_register会调用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);

/*
* 自界说的告诉链表的函数,即告诉test_chain指向的链表中的一切节点履行相应的函数
*/
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这三个节点加到之前界说的test_chain这个告诉链表上,一起每个节点都注册了一个函数:

#include
#include #include #include #include #include #include #include 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,该节点履行的函数为test_event1
*/
static struct notifier_block test_notifier1 =
{
    .notifier_call = test_event1,
};

/*
* 工作2,该节点履行的函数为test_event1
*/
static struct notifier_block test_notifier2 =
{
    .notifier_call = test_event2,
};

/*
* 工作3,该节点履行的函数为test_event1
*/
static struct notifier_block test_notifier3 =
{
    .notifier_call = test_event3,
};

/*
* 对这些工作进行注册
*/
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告诉链中发送音讯,让链中的函数运转:

#include
#include #include #include #include #include #include #include MODULE_LICENSE(“GPL”);

extern int test_notifier_call_chain(unsigned long val, void *v);

/*
* 向告诉链发送音讯以触发注册了的函数
*/
static int __init call_notifier(void)
{
  int err;
  printk(“Begin to notify:\n”);

  /*
  * 调用自界说的函数,向test_chain链发送音讯
  */
  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);

struct notifier_block
{
    int (*notifier_call)(struct notifier_block *, unsigned long, void *);
    struct notifier_block *next;
    int priority;
};

Makefile文件:

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权限):

make

insmod buildchain.ko
insmod regchain.ko
insmod notify.ko

这样就能够看到告诉链运转的作用了:

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
==============================

 

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/iot/89834.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部