Linux从用户层到内核层系列 – TCP/IP协议栈部分系列5:内核定时器的定义与使用及STP定时器

释放双眼,带上耳机,听听看~!

内核定时器的定义与使用及STP定时器

在Linux内核中大量的使用了定时器,协议栈中也不例外,例如网桥模式的时候启用定时器来发送STP(spanning tree protocol) BPDU包。

首先看内核中定时器的定义

1.使用文件 /linux/include/linux/timer.h

struct timer_list {

/*

* All fields that change during normal runtime grouped to the

* same cacheline

*/

struct list_head entry;

unsigned long expires;     
//以jiffies为单位的定时值

struct tvec_base *base;

void (*function)(unsigned long);     
//定时器回调函数

unsigned long data;   
//定时器生效时,传给function函数的长整形参数

int slack;
#ifdef CONFIG_TIMER_STATS

int start_pid;

void *start_site;

char start_comm[16];
#endif
#ifdef CONFIG_LOCKDEP

struct lockdep_map lockdep_map;
#endif
};

2.与定时器相关的处理函数

void add_timer(struct timer_list *timer)       //为内核增加一个定时器,在定时器生效的时候执行timer->function函数

 void add_timer_on(struct timer_list *timer, int cpu);      //在特定的CPU上开启定时器

int del_timer(struct timer_list *timer);        //取消一个定时器

int mod_timer(struct timer_list *timer, unsigned long expires)     //修改定时器的超时值

其次看内核定时器的使用实例

3.使用实例

#include<linux/timer.h>

struct timer_list my_timer = {

    .expires = jiffies + delay,

    .data = 0,

    function = my_callback_func

}

add_timer( my_timer );


再次看协议栈中2层网桥STP的内核定时器

4.STP timer 初始化

void br_stp_timer_init(struct net_bridge *br)
{

setup_timer(&br->hello_timer, br_hello_timer_expired,

     (unsigned long) br);

setup_timer(&br->tcn_timer, br_tcn_timer_expired,

     (unsigned long) br);

setup_timer(&br->topology_change_timer,

     br_topology_change_timer_expired,

     (unsigned long) br);

setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
}

给TA打赏
共{{data.count}}人
人已打赏
安全运维

WordPress网站专用docker容器环境带Waf

2020-7-18 20:04:44

安全运维

运维安全-Gitlab管理员权限安全思考

2021-9-19 9:16:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索