Skip to content

Commit 96c63fa

Browse files
David Aherndavem330
authored andcommitted
net: Add l3mdev rule
Currently, VRFs require 1 oif and 1 iif rule per address family per VRF. As the number of VRF devices increases it brings scalability issues with the increasing rule list. All of the VRF rules have the same format with the exception of the specific table id to direct the lookup. Since the table id is available from the oif or iif in the loopup, the VRF rules can be consolidated to a single rule that pulls the table from the VRF device. This patch introduces a new rule attribute l3mdev. The l3mdev rule means the table id used for the lookup is pulled from the L3 master device (e.g., VRF) rather than being statically defined. With the l3mdev rule all of the basic VRF FIB rules are reduced to 1 l3mdev rule per address family (IPv4 and IPv6). If an admin wishes to insert higher priority rules for specific VRFs those rules will co-exist with the l3mdev rule. This capability means current VRF scripts will co-exist with this new simpler implementation. Currently, the rules list for both ipv4 and ipv6 look like this: $ ip ru ls 1000: from all oif vrf1 lookup 1001 1000: from all iif vrf1 lookup 1001 1000: from all oif vrf2 lookup 1002 1000: from all iif vrf2 lookup 1002 1000: from all oif vrf3 lookup 1003 1000: from all iif vrf3 lookup 1003 1000: from all oif vrf4 lookup 1004 1000: from all iif vrf4 lookup 1004 1000: from all oif vrf5 lookup 1005 1000: from all iif vrf5 lookup 1005 1000: from all oif vrf6 lookup 1006 1000: from all iif vrf6 lookup 1006 1000: from all oif vrf7 lookup 1007 1000: from all iif vrf7 lookup 1007 1000: from all oif vrf8 lookup 1008 1000: from all iif vrf8 lookup 1008 ... 32765: from all lookup local 32766: from all lookup main 32767: from all lookup default With the l3mdev rule the list is just the following regardless of the number of VRFs: $ ip ru ls 1000: from all lookup [l3mdev table] 32765: from all lookup local 32766: from all lookup main 32767: from all lookup default (Note: the above pretty print of the rule is based on an iproute2 prototype. Actual verbage may change) Signed-off-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6278e03 commit 96c63fa

File tree

7 files changed

+109
-11
lines changed

7 files changed

+109
-11
lines changed

include/net/fib_rules.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ struct fib_rule {
1717
u32 flags;
1818
u32 table;
1919
u8 action;
20-
/* 3 bytes hole, try to use */
20+
u8 l3mdev;
21+
/* 2 bytes hole, try to use */
2122
u32 target;
2223
__be64 tun_id;
2324
struct fib_rule __rcu *ctarget;
@@ -36,6 +37,7 @@ struct fib_lookup_arg {
3637
void *lookup_ptr;
3738
void *result;
3839
struct fib_rule *rule;
40+
u32 table;
3941
int flags;
4042
#define FIB_LOOKUP_NOREF 1
4143
#define FIB_LOOKUP_IGNORE_LINKSTATE 2
@@ -89,7 +91,8 @@ struct fib_rules_ops {
8991
[FRA_TABLE] = { .type = NLA_U32 }, \
9092
[FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \
9193
[FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \
92-
[FRA_GOTO] = { .type = NLA_U32 }
94+
[FRA_GOTO] = { .type = NLA_U32 }, \
95+
[FRA_L3MDEV] = { .type = NLA_U8 }
9396

9497
static inline void fib_rule_get(struct fib_rule *rule)
9598
{
@@ -102,6 +105,20 @@ static inline void fib_rule_put(struct fib_rule *rule)
102105
kfree_rcu(rule, rcu);
103106
}
104107

108+
#ifdef CONFIG_NET_L3_MASTER_DEV
109+
static inline u32 fib_rule_get_table(struct fib_rule *rule,
110+
struct fib_lookup_arg *arg)
111+
{
112+
return rule->l3mdev ? arg->table : rule->table;
113+
}
114+
#else
115+
static inline u32 fib_rule_get_table(struct fib_rule *rule,
116+
struct fib_lookup_arg *arg)
117+
{
118+
return rule->table;
119+
}
120+
#endif
121+
105122
static inline u32 frh_get_table(struct fib_rule_hdr *frh, struct nlattr **nla)
106123
{
107124
if (nla[FRA_TABLE])
@@ -117,4 +134,7 @@ int fib_rules_lookup(struct fib_rules_ops *, struct flowi *, int flags,
117134
struct fib_lookup_arg *);
118135
int fib_default_rule_add(struct fib_rules_ops *, u32 pref, u32 table,
119136
u32 flags);
137+
138+
int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh);
139+
int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh);
120140
#endif

include/net/l3mdev.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#ifndef _NET_L3MDEV_H_
1212
#define _NET_L3MDEV_H_
1313

14+
#include <net/fib_rules.h>
15+
1416
/**
1517
* struct l3mdev_ops - l3mdev operations
1618
*
@@ -41,6 +43,9 @@ struct l3mdev_ops {
4143

4244
#ifdef CONFIG_NET_L3_MASTER_DEV
4345

46+
int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
47+
struct fib_lookup_arg *arg);
48+
4449
int l3mdev_master_ifindex_rcu(const struct net_device *dev);
4550
static inline int l3mdev_master_ifindex(struct net_device *dev)
4651
{
@@ -236,6 +241,13 @@ struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
236241
{
237242
return skb;
238243
}
244+
245+
static inline
246+
int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
247+
struct fib_lookup_arg *arg)
248+
{
249+
return 1;
250+
}
239251
#endif
240252

241253
#endif /* _NET_L3MDEV_H_ */

include/uapi/linux/fib_rules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ enum {
5050
FRA_FWMASK, /* mask for netfilter mark */
5151
FRA_OIFNAME,
5252
FRA_PAD,
53+
FRA_L3MDEV, /* iif or oif is l3mdev goto its table */
5354
__FRA_MAX
5455
};
5556

net/core/fib_rules.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ void fib_rules_unregister(struct fib_rules_ops *ops)
173173
EXPORT_SYMBOL_GPL(fib_rules_unregister);
174174

175175
static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
176-
struct flowi *fl, int flags)
176+
struct flowi *fl, int flags,
177+
struct fib_lookup_arg *arg)
177178
{
178179
int ret = 0;
179180

@@ -189,6 +190,9 @@ static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
189190
if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
190191
goto out;
191192

193+
if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
194+
goto out;
195+
192196
ret = ops->match(rule, fl, flags);
193197
out:
194198
return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
@@ -204,7 +208,7 @@ int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
204208

205209
list_for_each_entry_rcu(rule, &ops->rules_list, list) {
206210
jumped:
207-
if (!fib_rule_match(rule, ops, fl, flags))
211+
if (!fib_rule_match(rule, ops, fl, flags, arg))
208212
continue;
209213

210214
if (rule->action == FR_ACT_GOTO) {
@@ -265,7 +269,7 @@ static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
265269
return err;
266270
}
267271

268-
static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
272+
int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
269273
{
270274
struct net *net = sock_net(skb->sk);
271275
struct fib_rule_hdr *frh = nlmsg_data(nlh);
@@ -336,6 +340,14 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
336340
if (tb[FRA_TUN_ID])
337341
rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
338342

343+
if (tb[FRA_L3MDEV]) {
344+
#ifdef CONFIG_NET_L3_MASTER_DEV
345+
rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
346+
if (rule->l3mdev != 1)
347+
#endif
348+
goto errout_free;
349+
}
350+
339351
rule->action = frh->action;
340352
rule->flags = frh->flags;
341353
rule->table = frh_get_table(frh, tb);
@@ -371,6 +383,9 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
371383
} else if (rule->action == FR_ACT_GOTO)
372384
goto errout_free;
373385

386+
if (rule->l3mdev && rule->table)
387+
goto errout_free;
388+
374389
err = ops->configure(rule, skb, frh, tb);
375390
if (err < 0)
376391
goto errout_free;
@@ -424,8 +439,9 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
424439
rules_ops_put(ops);
425440
return err;
426441
}
442+
EXPORT_SYMBOL_GPL(fib_nl_newrule);
427443

428-
static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
444+
int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
429445
{
430446
struct net *net = sock_net(skb->sk);
431447
struct fib_rule_hdr *frh = nlmsg_data(nlh);
@@ -483,6 +499,10 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
483499
(rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
484500
continue;
485501

502+
if (tb[FRA_L3MDEV] &&
503+
(rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
504+
continue;
505+
486506
if (!ops->compare(rule, frh, tb))
487507
continue;
488508

@@ -536,6 +556,7 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
536556
rules_ops_put(ops);
537557
return err;
538558
}
559+
EXPORT_SYMBOL_GPL(fib_nl_delrule);
539560

540561
static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
541562
struct fib_rule *rule)
@@ -607,7 +628,9 @@ static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
607628
(rule->target &&
608629
nla_put_u32(skb, FRA_GOTO, rule->target)) ||
609630
(rule->tun_id &&
610-
nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)))
631+
nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
632+
(rule->l3mdev &&
633+
nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)))
611634
goto nla_put_failure;
612635

613636
if (rule->suppress_ifgroup != -1) {

net/ipv4/fib_rules.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
7676
{
7777
int err = -EAGAIN;
7878
struct fib_table *tbl;
79+
u32 tb_id;
7980

8081
switch (rule->action) {
8182
case FR_ACT_TO_TBL:
@@ -94,7 +95,8 @@ static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
9495

9596
rcu_read_lock();
9697

97-
tbl = fib_get_table(rule->fr_net, rule->table);
98+
tb_id = fib_rule_get_table(rule, arg);
99+
tbl = fib_get_table(rule->fr_net, tb_id);
98100
if (tbl)
99101
err = fib_table_lookup(tbl, &flp->u.ip4,
100102
(struct fib_result *)arg->result,
@@ -180,7 +182,7 @@ static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
180182
if (err)
181183
goto errout;
182184

183-
if (rule->table == RT_TABLE_UNSPEC) {
185+
if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
184186
if (rule->action == FR_ACT_TO_TBL) {
185187
struct fib_table *table;
186188

net/ipv6/fib6_rules.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
6767
struct net *net = rule->fr_net;
6868
pol_lookup_t lookup = arg->lookup_ptr;
6969
int err = 0;
70+
u32 tb_id;
7071

7172
switch (rule->action) {
7273
case FR_ACT_TO_TBL:
@@ -86,7 +87,8 @@ static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
8687
goto discard_pkt;
8788
}
8889

89-
table = fib6_get_table(net, rule->table);
90+
tb_id = fib_rule_get_table(rule, arg);
91+
table = fib6_get_table(net, tb_id);
9092
if (!table) {
9193
err = -EAGAIN;
9294
goto out;
@@ -199,7 +201,7 @@ static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199201
struct net *net = sock_net(skb->sk);
200202
struct fib6_rule *rule6 = (struct fib6_rule *) rule;
201203

202-
if (rule->action == FR_ACT_TO_TBL) {
204+
if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
203205
if (rule->table == RT6_TABLE_UNSPEC)
204206
goto errout;
205207

net/l3mdev/l3mdev.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include <linux/netdevice.h>
13+
#include <net/fib_rules.h>
1314
#include <net/l3mdev.h>
1415

1516
/**
@@ -160,3 +161,40 @@ int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4)
160161
return rc;
161162
}
162163
EXPORT_SYMBOL_GPL(l3mdev_get_saddr);
164+
165+
/**
166+
* l3mdev_fib_rule_match - Determine if flowi references an
167+
* L3 master device
168+
* @net: network namespace for device index lookup
169+
* @fl: flow struct
170+
*/
171+
172+
int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
173+
struct fib_lookup_arg *arg)
174+
{
175+
struct net_device *dev;
176+
int rc = 0;
177+
178+
rcu_read_lock();
179+
180+
dev = dev_get_by_index_rcu(net, fl->flowi_oif);
181+
if (dev && netif_is_l3_master(dev) &&
182+
dev->l3mdev_ops->l3mdev_fib_table) {
183+
arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
184+
rc = 1;
185+
goto out;
186+
}
187+
188+
dev = dev_get_by_index_rcu(net, fl->flowi_iif);
189+
if (dev && netif_is_l3_master(dev) &&
190+
dev->l3mdev_ops->l3mdev_fib_table) {
191+
arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
192+
rc = 1;
193+
goto out;
194+
}
195+
196+
out:
197+
rcu_read_unlock();
198+
199+
return rc;
200+
}

0 commit comments

Comments
 (0)