Skip to content

Commit 49042c2

Browse files
skorpion17davem330
authored andcommitted
l3mdev: add infrastructure for table to VRF mapping
Add infrastructure to l3mdev (the core code for Layer 3 master devices) in order to find out the corresponding VRF device for a given table id. Therefore, the l3mdev implementations: - can register a callback that returns the device index of the l3mdev associated with a given table id; - can offer the lookup function (table to VRF device). Signed-off-by: Andrea Mayer <andrea.mayer@uniroma2.it> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c5eb179 commit 49042c2

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎include/net/l3mdev.h‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
#include <net/dst.h>
1111
#include <net/fib_rules.h>
1212

13+
enum l3mdev_type {
14+
L3MDEV_TYPE_UNSPEC,
15+
L3MDEV_TYPE_VRF,
16+
__L3MDEV_TYPE_MAX
17+
};
18+
19+
#define L3MDEV_TYPE_MAX (__L3MDEV_TYPE_MAX - 1)
20+
21+
typedef int (*lookup_by_table_id_t)(struct net *net, u32 table_d);
22+
1323
/**
1424
* struct l3mdev_ops - l3mdev operations
1525
*
@@ -37,6 +47,15 @@ struct l3mdev_ops {
3747

3848
#ifdef CONFIG_NET_L3_MASTER_DEV
3949

50+
int l3mdev_table_lookup_register(enum l3mdev_type l3type,
51+
lookup_by_table_id_t fn);
52+
53+
void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
54+
lookup_by_table_id_t fn);
55+
56+
int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
57+
u32 table_id);
58+
4059
int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
4160
struct fib_lookup_arg *arg);
4261

@@ -280,6 +299,26 @@ struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
280299
return skb;
281300
}
282301

302+
static inline
303+
int l3mdev_table_lookup_register(enum l3mdev_type l3type,
304+
lookup_by_table_id_t fn)
305+
{
306+
return -EOPNOTSUPP;
307+
}
308+
309+
static inline
310+
void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
311+
lookup_by_table_id_t fn)
312+
{
313+
}
314+
315+
static inline
316+
int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
317+
u32 table_id)
318+
{
319+
return -ENODEV;
320+
}
321+
283322
static inline
284323
int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
285324
struct fib_lookup_arg *arg)

‎net/l3mdev/l3mdev.c‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,99 @@
99
#include <net/fib_rules.h>
1010
#include <net/l3mdev.h>
1111

12+
static DEFINE_SPINLOCK(l3mdev_lock);
13+
14+
struct l3mdev_handler {
15+
lookup_by_table_id_t dev_lookup;
16+
};
17+
18+
static struct l3mdev_handler l3mdev_handlers[L3MDEV_TYPE_MAX + 1];
19+
20+
static int l3mdev_check_type(enum l3mdev_type l3type)
21+
{
22+
if (l3type <= L3MDEV_TYPE_UNSPEC || l3type > L3MDEV_TYPE_MAX)
23+
return -EINVAL;
24+
25+
return 0;
26+
}
27+
28+
int l3mdev_table_lookup_register(enum l3mdev_type l3type,
29+
lookup_by_table_id_t fn)
30+
{
31+
struct l3mdev_handler *hdlr;
32+
int res;
33+
34+
res = l3mdev_check_type(l3type);
35+
if (res)
36+
return res;
37+
38+
hdlr = &l3mdev_handlers[l3type];
39+
40+
spin_lock(&l3mdev_lock);
41+
42+
if (hdlr->dev_lookup) {
43+
res = -EBUSY;
44+
goto unlock;
45+
}
46+
47+
hdlr->dev_lookup = fn;
48+
res = 0;
49+
50+
unlock:
51+
spin_unlock(&l3mdev_lock);
52+
53+
return res;
54+
}
55+
EXPORT_SYMBOL_GPL(l3mdev_table_lookup_register);
56+
57+
void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
58+
lookup_by_table_id_t fn)
59+
{
60+
struct l3mdev_handler *hdlr;
61+
62+
if (l3mdev_check_type(l3type))
63+
return;
64+
65+
hdlr = &l3mdev_handlers[l3type];
66+
67+
spin_lock(&l3mdev_lock);
68+
69+
if (hdlr->dev_lookup == fn)
70+
hdlr->dev_lookup = NULL;
71+
72+
spin_unlock(&l3mdev_lock);
73+
}
74+
EXPORT_SYMBOL_GPL(l3mdev_table_lookup_unregister);
75+
76+
int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type,
77+
struct net *net, u32 table_id)
78+
{
79+
lookup_by_table_id_t lookup;
80+
struct l3mdev_handler *hdlr;
81+
int ifindex = -EINVAL;
82+
int res;
83+
84+
res = l3mdev_check_type(l3type);
85+
if (res)
86+
return res;
87+
88+
hdlr = &l3mdev_handlers[l3type];
89+
90+
spin_lock(&l3mdev_lock);
91+
92+
lookup = hdlr->dev_lookup;
93+
if (!lookup)
94+
goto unlock;
95+
96+
ifindex = lookup(net, table_id);
97+
98+
unlock:
99+
spin_unlock(&l3mdev_lock);
100+
101+
return ifindex;
102+
}
103+
EXPORT_SYMBOL_GPL(l3mdev_ifindex_lookup_by_table_id);
104+
12105
/**
13106
* l3mdev_master_ifindex - get index of L3 master device
14107
* @dev: targeted interface

0 commit comments

Comments
 (0)