1. 程式人生 > >linux核心 策略路由之基本結構

linux核心 策略路由之基本結構

四、LINUX策略規則
4.1 基本結構體
4.1.1策略規則結構體fib_rule

struct fib_rule
{
    struct list_head    list;//策 略 規 則 鏈 表
    atomic_t        refcnt;//引 用 計 數
    int         ifindex;//接 口 index
    char        ifname[IFNAMSIZ];//接 口 名 稱
    u32         mark;//mark 值
    u32         mark_mask;//mark 掩 碼 值
    u32         pref;//優先順序, 值越小優先順序越大
    u32         flags;
    u32         table;//路由表id
    u8          action;//規則
    u32         target;
    struct fib_rule *   ctarget;
    struct rcu_head     rcu;
    struct net *        fr_net;
};

Action各巨集含義

enum
{
    FR_ACT_UNSPEC,
    FR_ACT_TO_TBL,      /* Pass to fixed table */
    FR_ACT_GOTO,        /* Jump to another rule */
    FR_ACT_NOP,     /* No operation */
    FR_ACT_RES3,
    FR_ACT_RES4,
    FR_ACT_BLACKHOLE,   /* Drop without notification */
    FR_ACT_UNREACHABLE, /* Drop with ENETUNREACH */
    FR_ACT_PROHIBIT,    /* Drop with EACCES */
    __FR_ACT_MAX,
};

4.1.2 策略操作函式fib_rules_ops
        策略規則中協議相關的操作函式結構體fib_rules_ops
struct fib_rules_ops
{
    int         family;//對應協議簇
    struct list_head    list;//將註冊到系統的fib_rules_ops連結到連結串列rules_ops
    int         rule_size;//一個策略規則所佔用的記憶體大小
    int         addr_size;//協議相關的地址長度
    int         unresolved_rules;
    int         nr_goto_rules;

    //action函式,策略規則匹配後,所呼叫的action函式,執行後續的操作。
    //一般是獲取到相應的路由表,查詢符合要求的路由項
    int         (*action)(struct fib_rule *,
                      struct flowi *, int,
                      struct fib_lookup_arg *);

    //規則匹配函式,對於策略規則的匹配,首先是通用匹配,待通用匹配完成後,
    //則會呼叫該函式,進行協議相關引數(源、目的地址等)的匹配
    int         (*match)(struct fib_rule *,
                     struct flowi *, int);
    //協議相關的配置引數
    int         (*configure)(struct fib_rule *,
                         struct sk_buff *,
                         struct fib_rule_hdr *,
                         struct nlattr **);
    int         (*compare)(struct fib_rule *,
                       struct fib_rule_hdr *,
                       struct nlattr **);
    int         (*fill)(struct fib_rule *, struct sk_buff *,
                    struct fib_rule_hdr *);
	//獲取預設優先順序
    u32         (*default_pref)(struct fib_rules_ops *ops);
    size_t          (*nlmsg_payload)(struct fib_rule *);

    /* Called after modifications to the rules set, must flush
     * the route cache if one exists. */
    void            (*flush_cache)(struct fib_rules_ops *ops);

    //下面兩個是netlink相關引數
    int         nlgroup;
    const struct nla_policy *policy;
    
    struct list_head    rules_list;//將該協議簇已新增的所有fib_rule規則連結在一起
    struct module       *owner;
    struct net      *fro_net;
};

4.1.3 ipv4型別的策略規則
       IPV4協議相關的fib4_rule結構,該結構包含了基礎的fib_rule,增加源IP、目的IP、tos等相關IPV4成員
struct fib4_rule//ipv4相關的結構
{
    struct fib_rule     common;
    u8          dst_len;
    u8          src_len;
    u8          tos;
    __be32          src;//源ip
    __be32          srcmask;
    __be32          dst;//目的ip
    __be32          dstmask;
#ifdef CONFIG_NET_CLS_ROUTE
    u32         tclassid;
#endif
};