題記:本系列文章的目的是拋開書本從源代碼和使用的角度分析Linux內核和相關源代碼,byhankswang和你一起玩轉linux開發
輕鬆搞定TCP/IP協議棧,原創文章歡迎交流, [email protected]
歡迎加入到CHLK - Linux開發交流羣 QQ:327084515 討論Linux開發相關問題
內核定時器的定義與使用及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);
}
Author: [email protected]