Skip to content

Commit

Permalink
[NET] fib_rules: goto rule action
Browse files Browse the repository at this point in the history
This patch adds a new rule action FR_ACT_GOTO which allows
to skip a set of rules by jumping to another rule. The rule
to jump to is specified via the FRA_GOTO attribute which
carries a rule preference.

Referring to a rule which doesn't exists is explicitely allowed.
Such goto rules are marked with the flag FIB_RULE_UNRESOLVED
and will act like a rule with a non-matching selector. The rule
will become functional as soon as its target is present.

The goto action enables performance optimizations by reducing
the average number of rules that have to be passed per lookup.

Example:
0:      from all lookup local
40:     not from all to 192.168.23.128 goto 32766
41:     from all fwmark 0xa blackhole
42:     from all fwmark 0xff blackhole
32766:  from all lookup main

Signed-off-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Thomas Graf authored and David S. Miller committed Apr 26, 2007
1 parent 2f7826c commit 0947c9f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/linux/fib_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* rule is permanent, and cannot be deleted */
#define FIB_RULE_PERMANENT 1
#define FIB_RULE_INVERT 2
#define FIB_RULE_UNRESOLVED 4

struct fib_rule_hdr
{
Expand All @@ -29,7 +30,7 @@ enum
FRA_DST, /* destination address */
FRA_SRC, /* source address */
FRA_IFNAME, /* interface name */
FRA_UNUSED1,
FRA_GOTO, /* target to jump to (FR_ACT_GOTO) */
FRA_UNUSED2,
FRA_PRIORITY, /* priority/preference */
FRA_UNUSED3,
Expand All @@ -51,7 +52,7 @@ enum
{
FR_ACT_UNSPEC,
FR_ACT_TO_TBL, /* Pass to fixed table */
FR_ACT_RES1,
FR_ACT_GOTO, /* Jump to another rule */
FR_ACT_RES2,
FR_ACT_RES3,
FR_ACT_RES4,
Expand Down
7 changes: 6 additions & 1 deletion include/net/fib_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct fib_rule
u32 flags;
u32 table;
u8 action;
u32 target;
struct fib_rule * ctarget;
struct rcu_head rcu;
};

Expand All @@ -35,6 +37,8 @@ struct fib_rules_ops
struct list_head list;
int rule_size;
int addr_size;
int unresolved_rules;
int nr_goto_rules;

int (*action)(struct fib_rule *,
struct flowi *, int,
Expand Down Expand Up @@ -66,7 +70,8 @@ struct fib_rules_ops
[FRA_PRIORITY] = { .type = NLA_U32 }, \
[FRA_FWMARK] = { .type = NLA_U32 }, \
[FRA_FWMASK] = { .type = NLA_U32 }, \
[FRA_TABLE] = { .type = NLA_U32 }
[FRA_TABLE] = { .type = NLA_U32 }, \
[FRA_GOTO] = { .type = NLA_U32 }

static inline void fib_rule_get(struct fib_rule *rule)
{
Expand Down
88 changes: 85 additions & 3 deletions net/core/fib_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,23 @@ int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
rcu_read_lock();

list_for_each_entry_rcu(rule, ops->rules_list, list) {
jumped:
if (!fib_rule_match(rule, ops, fl, flags))
continue;

err = ops->action(rule, fl, flags, arg);
if (rule->action == FR_ACT_GOTO) {
struct fib_rule *target;

target = rcu_dereference(rule->ctarget);
if (target == NULL) {
continue;
} else {
rule = target;
goto jumped;
}
} else
err = ops->action(rule, fl, flags, arg);

if (err != -EAGAIN) {
fib_rule_get(rule);
arg->rule = rule;
Expand Down Expand Up @@ -180,7 +193,7 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
struct fib_rules_ops *ops = NULL;
struct fib_rule *rule, *r, *last = NULL;
struct nlattr *tb[FRA_MAX+1];
int err = -EINVAL;
int err = -EINVAL, unresolved = 0;

if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
goto errout;
Expand Down Expand Up @@ -237,6 +250,28 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
if (!rule->pref && ops->default_pref)
rule->pref = ops->default_pref();

err = -EINVAL;
if (tb[FRA_GOTO]) {
if (rule->action != FR_ACT_GOTO)
goto errout_free;

rule->target = nla_get_u32(tb[FRA_GOTO]);
/* Backward jumps are prohibited to avoid endless loops */
if (rule->target <= rule->pref)
goto errout_free;

list_for_each_entry(r, ops->rules_list, list) {
if (r->pref == rule->target) {
rule->ctarget = r;
break;
}
}

if (rule->ctarget == NULL)
unresolved = 1;
} else if (rule->action == FR_ACT_GOTO)
goto errout_free;

err = ops->configure(rule, skb, nlh, frh, tb);
if (err < 0)
goto errout_free;
Expand All @@ -249,6 +284,28 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)

fib_rule_get(rule);

if (ops->unresolved_rules) {
/*
* There are unresolved goto rules in the list, check if
* any of them are pointing to this new rule.
*/
list_for_each_entry(r, ops->rules_list, list) {
if (r->action == FR_ACT_GOTO &&
r->target == rule->pref) {
BUG_ON(r->ctarget != NULL);
rcu_assign_pointer(r->ctarget, rule);
if (--ops->unresolved_rules == 0)
break;
}
}
}

if (rule->action == FR_ACT_GOTO)
ops->nr_goto_rules++;

if (unresolved)
ops->unresolved_rules++;

if (last)
list_add_rcu(&rule->list, &last->list);
else
Expand All @@ -269,7 +326,7 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
{
struct fib_rule_hdr *frh = nlmsg_data(nlh);
struct fib_rules_ops *ops = NULL;
struct fib_rule *rule;
struct fib_rule *rule, *tmp;
struct nlattr *tb[FRA_MAX+1];
int err = -EINVAL;

Expand Down Expand Up @@ -322,6 +379,25 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
}

list_del_rcu(&rule->list);

if (rule->action == FR_ACT_GOTO)
ops->nr_goto_rules--;

/*
* Check if this rule is a target to any of them. If so,
* disable them. As this operation is eventually very
* expensive, it is only performed if goto rules have
* actually been added.
*/
if (ops->nr_goto_rules > 0) {
list_for_each_entry(tmp, ops->rules_list, list) {
if (tmp->ctarget == rule) {
rcu_assign_pointer(tmp->ctarget, NULL);
ops->unresolved_rules++;
}
}
}

synchronize_rcu();
notify_rule_change(RTM_DELRULE, rule, ops, nlh,
NETLINK_CB(skb).pid);
Expand Down Expand Up @@ -371,6 +447,9 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
frh->action = rule->action;
frh->flags = rule->flags;

if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
frh->flags |= FIB_RULE_UNRESOLVED;

if (rule->ifname[0])
NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);

Expand All @@ -383,6 +462,9 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
if (rule->mark_mask || rule->mark)
NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);

if (rule->target)
NLA_PUT_U32(skb, FRA_GOTO, rule->target);

if (ops->fill(rule, skb, nlh, frh) < 0)
goto nla_put_failure;

Expand Down

0 comments on commit 0947c9f

Please sign in to comment.