在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);
}
还没有评论,来说两句吧...