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

题记:本系列文章的目的是抛开书本从源代码和使用的角度分析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]


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章