网络子系统66_策略路由初始化

来源:互联网 发布:技校学院网络组建方案 编辑:程序博客网 时间:2024/05/20 06:31
//策略路由链表//系统中所有策略路由通过fib_rules链接在一起static struct fib_rule default_rule = {.r_clntref =ATOMIC_INIT(2),//引用计数.r_preference =0x7FFF,//路由规则优先级.r_table =RT_TABLE_DEFAULT,//路由表标示符.r_action =RTN_UNICAST,//路由类型,间接定义了当路由查找匹配时应采取的动作};static struct fib_rule main_rule = {.r_next =&default_rule,.r_clntref =ATOMIC_INIT(2),.r_preference =0x7FFE,.r_table =RT_TABLE_MAIN,.r_action =RTN_UNICAST,};static struct fib_rule local_rule = {.r_next =&main_rule,.r_clntref =ATOMIC_INIT(2),.r_table =RT_TABLE_LOCAL,.r_action =RTN_UNICAST,};static struct fib_rule *fib_rules = &local_rule;//策略路由初始化//向netdev_chain注册监听块//调用路径:ip_rt_init->ip_fib_init->fib_rules_init2.1 void __init fib_rules_init(void){register_netdevice_notifier(&fib_rules_notifier);}2.1 static struct notifier_block fib_rules_notifier = {.notifier_call =fib_rules_event,};//策略路由对设备事件的处理//只处理设备注册,注销事件static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr){struct net_device *dev = ptr;//设备注销,删除与设备相关的策略路由if (event == NETDEV_UNREGISTER)fib_rules_detach(dev);else if (event == NETDEV_REGISTER)fib_rules_attach(dev);//添加与设备相关的策略return NOTIFY_DONE;}//禁止与设备相关的策略路由//将策略路由r_ifindex=-1,禁止策略路由2.2 static void fib_rules_detach(struct net_device *dev){struct fib_rule *r;//遍历系统中所有的策略路由for (r=fib_rules; r; r=r->r_next) {if (r->r_ifindex == dev->ifindex) {//策略路由使用的设备indexwrite_lock_bh(&fib_rules_lock);r->r_ifindex = -1;//禁止该策略路由write_unlock_bh(&fib_rules_lock);}}}//激活设备的策略路由//fib_rule->r_ifname表示策略应用的设备2.3 static void fib_rules_attach(struct net_device *dev){struct fib_rule *r;for (r=fib_rules; r; r=r->r_next) {if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {write_lock_bh(&fib_rules_lock);r->r_ifindex = dev->ifindex;write_unlock_bh(&fib_rules_lock);}}}

原创粉丝点击