diff mbox series

[RFC,net-next,1/5] l3mdev: add infrastructure for table to VRF mapping

Message ID 20200612164937.5468-2-andrea.mayer@uniroma2.it (mailing list archive)
State New
Headers show
Series Strict mode for VRF | expand

Commit Message

Andrea Mayer June 12, 2020, 4:49 p.m. UTC
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>
---
 include/net/l3mdev.h | 37 +++++++++++++++++
 net/l3mdev/l3mdev.c  | 95 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)

Comments

Jakub Kicinski June 12, 2020, 5:51 p.m. UTC | #1
On Fri, 12 Jun 2020 18:49:33 +0200 Andrea Mayer wrote:
> 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>

net/l3mdev/l3mdev.c:12:1: warning: symbol 'l3mdev_lock' was not declared. Should it be static?

Please make sure it doesn't add errors with W=1 C=1 :)
Andrea Mayer June 13, 2020, 10:35 p.m. UTC | #2
On Fri, 12 Jun 2020 10:51:48 -0700
Jakub Kicinski <kuba@kernel.org> wrote:

> net/l3mdev/l3mdev.c:12:1: warning: symbol 'l3mdev_lock' was not declared. Should it be static?
> 
> Please make sure it doesn't add errors with W=1 C=1 :)

Hi Jakub,
thanks for your feedback.

sorry, I did not want to add more warnings! I will declare the l3mdev_lock
static! I will be more careful while checking with W=1 and C=1 next time.

Thanks,
Andrea
David Ahern June 14, 2020, 12:37 a.m. UTC | #3
On 6/12/20 10:49 AM, Andrea Mayer wrote:
> @@ -37,6 +45,15 @@ struct l3mdev_ops {
>  
>  #ifdef CONFIG_NET_L3_MASTER_DEV
>  
> +int l3mdev_table_lookup_register(enum l3mdev_type l3type,
> +				 int (*fn)(struct net *net, u32 table_id));
> +
> +void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
> +				    int (*fn)(struct net *net, u32 table_id));
> +
> +int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
> +				      u32 table_id);
> +
>  int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
>  			  struct fib_lookup_arg *arg);
>  
> @@ -280,6 +297,26 @@ struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
>  	return skb;
>  }
>  
> +static inline
> +int l3mdev_table_lookup_register(enum l3mdev_type l3type,
> +				 int (*fn)(struct net *net, u32 table_id))
> +{
> +	return -EOPNOTSUPP;
> +}
> +
> +static inline
> +void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
> +				    int (*fn)(struct net *net, u32 table_id))
> +{
> +}
> +
> +static inline
> +int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
> +				      u32 table_id)
> +{
> +	return -ENODEV;
> +}
> +
>  static inline
>  int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
>  			  struct fib_lookup_arg *arg)
> diff --git a/net/l3mdev/l3mdev.c b/net/l3mdev/l3mdev.c
> index f35899d45a9a..6cc1fe7eb039 100644
> --- a/net/l3mdev/l3mdev.c
> +++ b/net/l3mdev/l3mdev.c
> @@ -9,6 +9,101 @@
>  #include <net/fib_rules.h>
>  #include <net/l3mdev.h>
>  
> +DEFINE_SPINLOCK(l3mdev_lock);
> +
> +typedef int (*lookup_by_table_id_t)(struct net *net, u32 table_d);
> +

I should have caught this earlier. Move lookup_by_table_id_t to l3mdev.h
and use above for 'fn' in l3mdev_table_lookup_{un,}register
Andrea Mayer June 14, 2020, 9:56 p.m. UTC | #4
On Sat, 13 Jun 2020 18:37:09 -0600
David Ahern <dsahern@gmail.com> wrote:

> On 6/12/20 10:49 AM, Andrea Mayer wrote:
> > @@ -37,6 +45,15 @@ struct l3mdev_ops {
> >  
> >  #ifdef CONFIG_NET_L3_MASTER_DEV
> >  
> > +int l3mdev_table_lookup_register(enum l3mdev_type l3type,
> > +				 int (*fn)(struct net *net, u32 table_id));
> > +
> > +void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
> > +				    int (*fn)(struct net *net, u32 table_id));
> > +
> > +int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
> > +				      u32 table_id);
> > +
> >  int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
> >  			  struct fib_lookup_arg *arg);
> >  
> > @@ -280,6 +297,26 @@ struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
> >  	return skb;
> >  }
> >  
> > +static inline
> > +int l3mdev_table_lookup_register(enum l3mdev_type l3type,
> > +				 int (*fn)(struct net *net, u32 table_id))
> > +{
> > +	return -EOPNOTSUPP;
> > +}
> > +
> > +static inline
> > +void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
> > +				    int (*fn)(struct net *net, u32 table_id))
> > +{
> > +}
> > +
> > +static inline
> > +int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
> > +				      u32 table_id)
> > +{
> > +	return -ENODEV;
> > +}
> > +
> >  static inline
> >  int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
> >  			  struct fib_lookup_arg *arg)
> > diff --git a/net/l3mdev/l3mdev.c b/net/l3mdev/l3mdev.c
> > index f35899d45a9a..6cc1fe7eb039 100644
> > --- a/net/l3mdev/l3mdev.c
> > +++ b/net/l3mdev/l3mdev.c
> > @@ -9,6 +9,101 @@
> >  #include <net/fib_rules.h>
> >  #include <net/l3mdev.h>
> >  
> > +DEFINE_SPINLOCK(l3mdev_lock);
> > +
> > +typedef int (*lookup_by_table_id_t)(struct net *net, u32 table_d);
> > +
> 
> I should have caught this earlier. Move lookup_by_table_id_t to l3mdev.h
> and use above for 'fn' in l3mdev_table_lookup_{un,}register
> 

Hi David,
Ok, I will do it!

Thank you,
Andrea
diff mbox series

Patch

diff --git a/include/net/l3mdev.h b/include/net/l3mdev.h
index e942372b077b..37aed117455f 100644
--- a/include/net/l3mdev.h
+++ b/include/net/l3mdev.h
@@ -10,6 +10,14 @@ 
 #include <net/dst.h>
 #include <net/fib_rules.h>
 
+enum l3mdev_type {
+	L3MDEV_TYPE_UNSPEC,
+	L3MDEV_TYPE_VRF,
+	__L3MDEV_TYPE_MAX
+};
+
+#define L3MDEV_TYPE_MAX (__L3MDEV_TYPE_MAX - 1)
+
 /**
  * struct l3mdev_ops - l3mdev operations
  *
@@ -37,6 +45,15 @@  struct l3mdev_ops {
 
 #ifdef CONFIG_NET_L3_MASTER_DEV
 
+int l3mdev_table_lookup_register(enum l3mdev_type l3type,
+				 int (*fn)(struct net *net, u32 table_id));
+
+void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
+				    int (*fn)(struct net *net, u32 table_id));
+
+int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
+				      u32 table_id);
+
 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
 			  struct fib_lookup_arg *arg);
 
@@ -280,6 +297,26 @@  struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
 	return skb;
 }
 
+static inline
+int l3mdev_table_lookup_register(enum l3mdev_type l3type,
+				 int (*fn)(struct net *net, u32 table_id))
+{
+	return -EOPNOTSUPP;
+}
+
+static inline
+void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
+				    int (*fn)(struct net *net, u32 table_id))
+{
+}
+
+static inline
+int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,
+				      u32 table_id)
+{
+	return -ENODEV;
+}
+
 static inline
 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
 			  struct fib_lookup_arg *arg)
diff --git a/net/l3mdev/l3mdev.c b/net/l3mdev/l3mdev.c
index f35899d45a9a..6cc1fe7eb039 100644
--- a/net/l3mdev/l3mdev.c
+++ b/net/l3mdev/l3mdev.c
@@ -9,6 +9,101 @@ 
 #include <net/fib_rules.h>
 #include <net/l3mdev.h>
 
+DEFINE_SPINLOCK(l3mdev_lock);
+
+typedef int (*lookup_by_table_id_t)(struct net *net, u32 table_d);
+
+struct l3mdev_handler {
+	lookup_by_table_id_t dev_lookup;
+};
+
+static struct l3mdev_handler l3mdev_handlers[L3MDEV_TYPE_MAX + 1];
+
+static int l3mdev_check_type(enum l3mdev_type l3type)
+{
+	if (l3type <= L3MDEV_TYPE_UNSPEC || l3type > L3MDEV_TYPE_MAX)
+		return -EINVAL;
+
+	return 0;
+}
+
+int l3mdev_table_lookup_register(enum l3mdev_type l3type,
+				 lookup_by_table_id_t fn)
+{
+	struct l3mdev_handler *hdlr;
+	int res;
+
+	res = l3mdev_check_type(l3type);
+	if (res)
+		return res;
+
+	hdlr = &l3mdev_handlers[l3type];
+
+	spin_lock(&l3mdev_lock);
+
+	if (hdlr->dev_lookup) {
+		res = -EBUSY;
+		goto unlock;
+	}
+
+	hdlr->dev_lookup = fn;
+	res = 0;
+
+unlock:
+	spin_unlock(&l3mdev_lock);
+
+	return res;
+}
+EXPORT_SYMBOL_GPL(l3mdev_table_lookup_register);
+
+void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
+				    lookup_by_table_id_t fn)
+{
+	struct l3mdev_handler *hdlr;
+
+	if (l3mdev_check_type(l3type))
+		return;
+
+	hdlr = &l3mdev_handlers[l3type];
+
+	spin_lock(&l3mdev_lock);
+
+	if (hdlr->dev_lookup == fn)
+		hdlr->dev_lookup = NULL;
+
+	spin_unlock(&l3mdev_lock);
+}
+EXPORT_SYMBOL_GPL(l3mdev_table_lookup_unregister);
+
+int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type,
+				      struct net *net, u32 table_id)
+{
+	lookup_by_table_id_t lookup;
+	struct l3mdev_handler *hdlr;
+	int ifindex = -EINVAL;
+	int res;
+
+	res = l3mdev_check_type(l3type);
+	if (res)
+		return res;
+
+	hdlr = &l3mdev_handlers[l3type];
+
+	spin_lock(&l3mdev_lock);
+
+	lookup = hdlr->dev_lookup;
+	if (!lookup)
+		goto unlock;
+
+	ifindex = lookup(net, table_id);
+
+unlock:
+	spin_unlock(&l3mdev_lock);
+
+	return ifindex;
+}
+EXPORT_SYMBOL_GPL(l3mdev_ifindex_lookup_by_table_id);
+
 /**
  *	l3mdev_master_ifindex - get index of L3 master device
  *	@dev: targeted interface