日期:2014-05-16 浏览次数:20685 次
7.6 tcf_proto_ops的一些相关操作 7.6.1 登记和撤销 /* Register(unregister) new classifier type */ // 登记新的tcf_proto_ops分类操作结构 int register_tcf_proto_ops(struct tcf_proto_ops *ops) { struct tcf_proto_ops *t, **tp; int rc = -EEXIST; write_lock(&cls_mod_lock); // 遍历当前tcf_proto_ops链表 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next) // 检查是否有名称相同的项, 有的话返回对象已存在错误 if (!strcmp(ops->kind, t->kind)) goto out; // 添加到链表末尾, 也是dummy header算法 ops->next = NULL; *tp = ops; rc = 0; out: write_unlock(&cls_mod_lock); return rc; } // 撤销tcf_proto_ops分类结构 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) { struct tcf_proto_ops *t, **tp; int rc = -ENOENT; write_lock(&cls_mod_lock); // 遍历链表 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next) // 直接进行tcf_proto_ops结构地址比较, 相同的话中断循环 if (t == ops) break; if (!t) goto out; // 将找到的tp节点从链表中断开, 不用释放操作, 因为这些ops其实都是静态定义的 *tp = t->next; rc = 0; out: write_unlock(&cls_mod_lock); return rc; } 7.6.2 tcf扩展 tcf扩展增加了对分类后数据进行某种操作的功能, 有点象netfilter的target,使用这些功能需要在配置内核时定义NET_CLS_ACT或NET_CLS_POLICE。 /* include/net/pkt_cls.h */ // tcf扩展结构, 如果没定义NET_CLS_ACT和NET_CLS_POLICE的话就是个空结构 struct tcf_exts { #ifdef CONFIG_NET_CLS_ACT // 动作 struct tc_action *action; #elif defined CONFIG_NET_CLS_POLICE // 策略 struct tcf_police *police; #endif }; /* Map to export classifier specific extension TLV types to the * generic extensions API. Unsupported extensions must be set to 0. */ struct tcf_ext_map { int action; int police; }; /** * tcf_exts_is_predicative - check if a predicative extension is present * @exts: tc filter extensions handle * * Returns 1 if a predicative extension is present, i.e. an extension which * might cause further actions and thus overrule the regular tcf_result. */ // 返回扩展结构中的元素是否为空 static inline int tcf_exts_is_predicative(struct tcf_exts *exts) { #ifdef CONFIG_NET_CLS_ACT // !!是为了保证返回值0或1 return !!exts->action; #elif defined CONFIG_NET_CLS_POLICE return !!exts->police; #else return 0; #endif } /** * tcf_exts_is_available - check if at least one extension is present * @exts: tc filter extensions handle * * Returns 1 if at least one extension is present. */ // 实际就是cf_exts_is_predicative函数 static inline int tcf_exts_is_available(struct tcf_exts *exts) { /* All non-predicative extensions must be added here. */ return tcf_exts_is_predicative(exts); } /** * tcf_exts_exec - execute tc filter extensions * @skb: socket buffer * @exts: tc filter extensions handle * @res: desired result * * Executes all configured extensions. Returns 0 on a normal execution, * a negative number if the filter must be considered unmatched or * a positive action code (TC_ACT_*) which must be returned to the * underlying layer. */ static inline int tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts, struct tcf_result *res) { #ifdef CONFIG_NET_CLS_ACT if (exts->action) return tcf_action_exec(skb, exts->action, res); #elif defined CONFIG_NET_CLS_POLICE if (exts->police) return tcf_police(skb, exts->police); #endif return 0; } /* net/sched/cls_api.c */ // 是否tcf扩展结构 void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts) { #ifdef CONFIG_NET_CLS_ACT if (exts->action) { // 释放tcf动作 tcf_action_destroy(exts->action, TCA_ACT_UNBIND); exts->action = NULL; } #elif defined CONFIG_NET_CLS_POLICE if (exts->police) { // 释放tcf策略 tcf_police_release(exts->police, TCA_ACT_UNBIND); exts->police = NULL; } #endif } int tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb, struct rtattr *rate_tlv, struct tcf_exts *exts, struct tcf_ext_map *map) { // 结构清零 memset(exts, 0, sizeof(*exts)); #ifdef CONFIG_NET_CLS_ACT { int err; struct tc_action *