Skip to contentSkip to conversation

mpls: multipath route support

PATCH Superseded Roopa Prabhu 3 participants 6 messages Oct 19, 2015 – Oct 22, 2015

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 mpls: multipath route support

From: Roopa Prabhu <[email protected]>

This patch adds support for MPLS multipath routes.

Includes following changes to support multipath:
- splits struct mpls_route into 'struct mpls_route + struct mpls_nh'

- 'struct mpls_nh' represents a mpls nexthop label forwarding entry

- moves mpls route and nexthop structures into internal.h

- A mpls_route can point to multiple mpls_nh structs

- the nexthops are maintained as a array (similar to ipv4 fib)

- In the process of restructuring, this patch also consistently changes all labels to u8

- Adds support to parse/fill RTA_MULTIPATH netlink attribute for multipath routes similar to ipv4/v6 fib

- In this patch, the multipath route nexthop selection algorithm simply returns the first nexthop. It is replaced by a hash based algorithm from Robert Shearman in the next patch

- mpls_route_update cleanup: remove 'dev' handling in mpls_route_update. mpls_route_update though implemented to update based on dev, it was never used that way. And the dev handling gets tricky with multiple nexthops. Cannot match against any single nexthops dev. So, this patch removes the unused 'dev' handling in mpls_route_update.

Example:

$ip -f mpls route add 100 nexthop as 200 via inet 10.1.1.2 dev swp1 \
                nexthop as 700 via inet 10.1.1.6 dev swp2 \
                nexthop as 800 via inet 40.1.1.2 dev swp3

$ip -f mpls route show
100

        nexthop as to 200 via inet 10.1.1.2  dev swp1
        nexthop as to 700 via inet 10.1.1.6  dev swp2
        nexthop as to 800 via inet 40.1.1.2  dev swp3
---
 include/net/mpls_iptunnel.h |   2 +-
 net/mpls/af_mpls.c          | 498 +++++++++++++++++++++++++++++++-------------
 net/mpls/internal.h         |  52 ++++-
 3 files changed, 403 insertions(+), 149 deletions(-)
include/net/mpls_iptunnel.h +1 -1
index 4757997..179253f 100644
@@ -18,7 +18,7 @@
 
 struct mpls_iptunnel_encap {
 	u32	label[MAX_NEW_LABELS];
-	u32	labels;
+	u8	labels;
 };
 
 static inline struct mpls_iptunnel_encap *mpls_lwtunnel_encap(struct lwtunnel_state *lwtstate)
net/mpls/af_mpls.c +351 -147

717 lines not on this page · raw message

net/mpls/internal.h +51 -1

65 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 54 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch adds support for MPLS multipath routes.

Includes following changes to support multipath:
- splits struct mpls_route into 'struct mpls_route + struct mpls_nh'

- 'struct mpls_nh' represents a mpls nexthop label forwarding entry

- moves mpls route and nexthop structures into internal.h

- A mpls_route can point to multiple mpls_nh structs

- the nexthops are maintained as a array (similar to ipv4 fib)

- In the process of restructuring, this patch also consistently changes all labels to u8

- Adds support to parse/fill RTA_MULTIPATH netlink attribute for multipath routes similar to ipv4/v6 fib

- In this patch, the multipath route nexthop selection algorithm simply returns the first nexthop. It is replaced by a hash based algorithm from Robert Shearman in the next patch

- mpls_route_update cleanup: remove 'dev' handling in mpls_route_update. mpls_route_update though implemented to update based on dev, it was never used that way. And the dev handling gets tricky with multiple nexthops. Cannot match against any single nexthops dev. So, this patch removes the unused 'dev' handling in mpls_route_update.

Example:

$ip -f mpls route add 100 nexthop as 200 via inet 10.1.1.2 dev swp1 \
                nexthop as 700 via inet 10.1.1.6 dev swp2 \
                nexthop as 800 via inet 40.1.1.2 dev swp3

$ip -f mpls route show
100

        nexthop as to 200 via inet 10.1.1.2  dev swp1
        nexthop as to 700 via inet 10.1.1.6  dev swp2
        nexthop as to 800 via inet 40.1.1.2  dev swp3
---
 include/net/mpls_iptunnel.h |   2 +-
 net/mpls/af_mpls.c          | 498 +++++++++++++++++++++++++++++++-------------
 net/mpls/internal.h         |  52 ++++-
 3 files changed, 403 insertions(+), 149 deletions(-)
net/mpls/af_mpls.c +0 -0
index bb185a2..ebefdd4 100644

[...]

Quoted 14 lines
net/mpls/af_mpls.c +10 -0
@@ -431,15 +417,171 @@ static struct net_device *find_outdev(struct net *net,
 	return dev;
 }
 
+static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
+{
+	struct net_device *dev = NULL;
+	int err = -ENODEV;
+
+	dev = find_outdev(net, nh, oif);
+	if (IS_ERR(dev)) {
+		err = PTR_ERR(dev);
+		dev = NULL;
+		goto errout;

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 439

Quoted 10 lines
net/mpls/af_mpls.c +10 -0
+	}
+
+	/* Ensure this is a supported device */
+	err = -EINVAL;
+	if (!mpls_dev_get(dev))
+		goto errout;
+
+	/* For now just support ethernet devices */
+	if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
+		goto errout;

Roopa you don't need this extra test of ARPHRD_ETHER and ARPHRD_LOOPBACK. We already guard mpls_add_dev with this, so mpls_dev_get will fail if it is the wrong kind of device.

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 450

Quoted 11 lines
net/mpls/af_mpls.c +11 -0
+
+	RCU_INIT_POINTER(nh->nh_dev, dev);
+	dev_put(dev);
+
+	return 0;
+
+errout:
+	if (dev)
+		dev_put(dev);
+	return err;
+}

Eric

#3 lore raw
In reply to Roopa Prabhu

net/mpls/af_mpls.c static int mpls_forward(struct sk_buff *skb, struct net_device *dev, · line 148

Quoted 13 lines · Roopa Prabhu
net/mpls/af_mpls.c +2 -0
@@ -159,6 +137,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  	struct net *net = dev_net(dev);
  	struct mpls_shim_hdr *hdr;
  	struct mpls_route *rt;
+	struct mpls_nh *nh;
  	struct mpls_entry_decoded dec;
  	struct net_device *out_dev;
  	struct mpls_dev *mdev;
@@ -166,6 +145,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  	unsigned int new_header_size;
  	unsigned int mtu;
  	int err;
+	int nhidx;

nhidx looks to be unused. Remove?

Quoted 61 lines
net/mpls/af_mpls.c +10 -5
  	/* Careful this entire function runs inside of an rcu critical section */
 
@@ -196,8 +176,12 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  	if (!rt)
  		goto drop;
 
+	nh = mpls_select_multipath(rt);
+	if (!nh)
+		goto drop;
+
  	/* Find the output device */
-	out_dev = rcu_dereference(rt->rt_dev);
+	out_dev = rcu_dereference(nh->nh_dev);
  	if (!mpls_output_possible(out_dev))
  		goto drop;
 
@@ -212,7 +196,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  	dec.ttl -= 1;
 
  	/* Verify the destination can hold the packet */
-	new_header_size = mpls_rt_header_size(rt);
+	new_header_size = mpls_nh_header_size(nh);
  	mtu = mpls_dev_mtu(out_dev);
  	if (mpls_pkt_too_big(skb, mtu - new_header_size))
  		goto drop;
@@ -240,13 +224,14 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  		/* Push the new labels */
  		hdr = mpls_hdr(skb);
  		bos = dec.bos;
-		for (i = rt->rt_labels - 1; i >= 0; i--) {
-			hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
+		for (i = nh->nh_labels - 1; i >= 0; i--) {
+			hdr[i] = mpls_entry_encode(nh->nh_label[i],
+						   dec.ttl, 0, bos);
  			bos = false;
  		}
  	}
 
-	err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
+	err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
  	if (err)
  		net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  				    __func__, err);
  	return dev;
  }
net/mpls/af_mpls.c +13 -0
+static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
+{
+	struct net_device *dev = NULL;
+	int err = -ENODEV;
+
+	dev = find_outdev(net, nh, oif);
+	if (IS_ERR(dev)) {
+		err = PTR_ERR(dev);
+		dev = NULL;
+		goto errout;
+	}
+
+	/* Ensure this is a supported device */

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 442

Quoted 10 lines
net/mpls/af_mpls.c +10 -0
+	err = -EINVAL;
+	if (!mpls_dev_get(dev))
+		goto errout;
+
+	/* For now just support ethernet devices */
+	if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
+		goto errout;
+
+	RCU_INIT_POINTER(nh->nh_dev, dev);
+	dev_put(dev);

If it's safe to not keep a reference to dev (i.e. because the device cannot go away while a netlink message is being processed), then why not just change find_outdev to use __dev_get_by_index? If the dev can go away during this, then we could end up adding an mpls route that points to a freed interface, because until we've added the route to the label table mpls_ifdown won't find it.

Quoted 9 lines
(quoted diff) +9 -0
+
+	return 0;
+
+errout:
+	if (dev)
+		dev_put(dev);
+	return err;
+}
+

Review

Quoted 10 lines
(quoted diff) +6 -0
 
  }
  EXPORT_SYMBOL_GPL(nla_get_labels);
 
+int nla_get_via(const struct nlattr *nla, u8 *via_alen,
+		u8 *via_table, u8 via_addr[])
+{
+	struct rtvia *via = nla_data(nla);
+	int err = -EINVAL;
+	u8 alen;

This should be an int to avoid nexthop address lengths > 255 wrapping and possibly accepted instead of rejected.

Quoted 29 lines
net/mpls/af_mpls.c +29 -0
+
+	if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
+		goto errout;
+	alen = nla_len(nla) -
+			offsetof(struct rtvia, rtvia_addr);
+	if (alen > MAX_VIA_ALEN)
+		goto errout;
+
+	/* Validate the address family */
+	switch(via->rtvia_family) {
+	case AF_PACKET:
+		*via_table = NEIGH_LINK_TABLE;
+		break;
+	case AF_INET:
+		*via_table = NEIGH_ARP_TABLE;
+		if (alen != 4)
+			goto errout;
+		break;
+	case AF_INET6:
+		*via_table = NEIGH_ND_TABLE;
+		if (alen != 16)
+			goto errout;
+		break;
+	default:
+		/* Unsupported address family */
+		goto errout;
+	}
+
+	memcpy(via_addr, via->rtvia_addr, alen);

net/mpls/af_mpls.c int nla_get_labels(const struct nlattr *nla, · line 958

Quoted 10 lines
net/mpls/af_mpls.c +7 -0
+	*via_alen = alen;
+	err = 0;
+
+errout:
+	return err;
+}
+
  static int rtm_to_route_config(struct sk_buff *skb,  struct nlmsghdr *nlh,
  			       struct mpls_route_config *cfg)
  {

Otherwise, looks good.

Thanks,
Rob

author
#2 lore raw
In reply to Eric W. Biederman
Quoted 76 lines · Eric W. Biederman
Quoted 54 lines · Roopa Prabhu

From: Roopa Prabhu <[email protected]>

This patch adds support for MPLS multipath routes.

Includes following changes to support multipath:
- splits struct mpls_route into 'struct mpls_route + struct mpls_nh'

- 'struct mpls_nh' represents a mpls nexthop label forwarding entry

- moves mpls route and nexthop structures into internal.h

- A mpls_route can point to multiple mpls_nh structs

- the nexthops are maintained as a array (similar to ipv4 fib)

- In the process of restructuring, this patch also consistently changes all labels to u8

- Adds support to parse/fill RTA_MULTIPATH netlink attribute for multipath routes similar to ipv4/v6 fib

- In this patch, the multipath route nexthop selection algorithm simply returns the first nexthop. It is replaced by a hash based algorithm from Robert Shearman in the next patch

- mpls_route_update cleanup: remove 'dev' handling in mpls_route_update. mpls_route_update though implemented to update based on dev, it was never used that way. And the dev handling gets tricky with multiple nexthops. Cannot match against any single nexthops dev. So, this patch removes the unused 'dev' handling in mpls_route_update.

Example:

$ip -f mpls route add 100 nexthop as 200 via inet 10.1.1.2 dev swp1 \
                nexthop as 700 via inet 10.1.1.6 dev swp2 \
                nexthop as 800 via inet 40.1.1.2 dev swp3

$ip -f mpls route show
100

        nexthop as to 200 via inet 10.1.1.2  dev swp1
        nexthop as to 700 via inet 10.1.1.6  dev swp2
        nexthop as to 800 via inet 40.1.1.2  dev swp3
---
 include/net/mpls_iptunnel.h |   2 +-
 net/mpls/af_mpls.c          | 498 +++++++++++++++++++++++++++++++-------------
 net/mpls/internal.h         |  52 ++++-
 3 files changed, 403 insertions(+), 149 deletions(-)
net/mpls/af_mpls.c +0 -0
index bb185a2..ebefdd4 100644

[...]

Quoted 19 lines
net/mpls/af_mpls.c +15 -0
@@ -431,15 +417,171 @@ static struct net_device *find_outdev(struct net *net,
 	return dev;
 }
 
+static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
+{
+	struct net_device *dev = NULL;
+	int err = -ENODEV;
+
+	dev = find_outdev(net, nh, oif);
+	if (IS_ERR(dev)) {
+		err = PTR_ERR(dev);
+		dev = NULL;
+		goto errout;
+	}
+
+	/* Ensure this is a supported device */
+	err = -EINVAL;
+	if (!mpls_dev_get(dev))
Quoted 9 lines

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 439

Quoted 5 lines
net/mpls/af_mpls.c +5 -0
+		goto errout;
+
+	/* For now just support ethernet devices */
+	if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
+		goto errout;

Roopa you don't need this extra test of ARPHRD_ETHER and ARPHRD_LOOPBACK. We already guard mpls_add_dev with this, so mpls_dev_get will fail if it is the wrong kind of device.

Ack, will respin. thanks.

author
#4 lore raw
In reply to Robert Shearman
Quoted 16 lines · Robert Shearman

net/mpls/af_mpls.c static int mpls_forward(struct sk_buff *skb, struct net_device *dev, · line 148

Quoted 13 lines · Roopa Prabhu
net/mpls/af_mpls.c +2 -0
@@ -159,6 +137,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
      struct net *net = dev_net(dev);
      struct mpls_shim_hdr *hdr;
      struct mpls_route *rt;
+    struct mpls_nh *nh;
      struct mpls_entry_decoded dec;
      struct net_device *out_dev;
      struct mpls_dev *mdev;
@@ -166,6 +145,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
      unsigned int new_header_size;
      unsigned int mtu;
      int err;
+    int nhidx;

nhidx looks to be unused. Remove?

ack,

Quoted 66 lines
Quoted 66 lines
net/mpls/af_mpls.c +10 -5
      /* Careful this entire function runs inside of an rcu critical section */
 
@@ -196,8 +176,12 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
      if (!rt)
          goto drop;
 
+    nh = mpls_select_multipath(rt);
+    if (!nh)
+        goto drop;
+
      /* Find the output device */
-    out_dev = rcu_dereference(rt->rt_dev);
+    out_dev = rcu_dereference(nh->nh_dev);
      if (!mpls_output_possible(out_dev))
          goto drop;
 
@@ -212,7 +196,7 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
      dec.ttl -= 1;
 
      /* Verify the destination can hold the packet */
-    new_header_size = mpls_rt_header_size(rt);
+    new_header_size = mpls_nh_header_size(nh);
      mtu = mpls_dev_mtu(out_dev);
      if (mpls_pkt_too_big(skb, mtu - new_header_size))
          goto drop;
@@ -240,13 +224,14 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
          /* Push the new labels */
          hdr = mpls_hdr(skb);
          bos = dec.bos;
-        for (i = rt->rt_labels - 1; i >= 0; i--) {
-            hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
+        for (i = nh->nh_labels - 1; i >= 0; i--) {
+            hdr[i] = mpls_entry_encode(nh->nh_label[i],
+                           dec.ttl, 0, bos);
              bos = false;
          }
      }
 
-    err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
+    err = neigh_xmit(nh->nh_via_table, out_dev, nh->nh_via, skb);
      if (err)
          net_dbg_ratelimited("%s: packet transmission failed: %d\n",
                      __func__, err);
      return dev;

}

net/mpls/af_mpls.c +17 -0
+static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
+{
+    struct net_device *dev = NULL;
+    int err = -ENODEV;
+
+    dev = find_outdev(net, nh, oif);
+    if (IS_ERR(dev)) {
+        err = PTR_ERR(dev);
+        dev = NULL;
+        goto errout;
+    }
+
+    /* Ensure this is a supported device */
+    err = -EINVAL;
+    if (!mpls_dev_get(dev))
+        goto errout;
+
Quoted 8 lines

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 442

Quoted 6 lines
net/mpls/af_mpls.c +6 -0
+    /* For now just support ethernet devices */
+    if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
+        goto errout;
+
+    RCU_INIT_POINTER(nh->nh_dev, dev);
+    dev_put(dev);

If it's safe to not keep a reference to dev (i.e. because the device cannot go away while a netlink message is being processed), then why not just change find_outdev to use __dev_get_by_index?

yes, rtnl is held here. dev_get_by_index was used by the original code. So i did not feel the need to change that.
I can see the need to get away with the dev_hold instead of doing a dev_hold with dev_get_by_index, rcu assign and put because of rtnl. But, it seems cleaner to use it the way it is today, given the other cases where the dev is already held during lookup when oif is not specified.

Quoted 1 line

If the dev can go away during this, then we could end up adding an mpls route that points to a freed interface, because until we've added the route to the label table mpls_ifdown won't find it.

dev should not go away because we have rtnl held at this point. no ?
If there is a problem i can think about moving the dev_put's for nexthop devs after mpls_route_update

Quoted 14 lines
Quoted 14 lines
net/mpls/af_mpls.c +9 -0
+
+    return 0;
+
+errout:
+    if (dev)
+        dev_put(dev);
+    return err;
+}
+
 
  }
  EXPORT_SYMBOL_GPL(nla_get_labels);
Quoted 8 lines

net/mpls/af_mpls.c int nla_get_labels(const struct nlattr *nla, · line 919

Quoted 6 lines
net/mpls/af_mpls.c +6 -0
+int nla_get_via(const struct nlattr *nla, u8 *via_alen,
+        u8 *via_table, u8 via_addr[])
+{
+    struct rtvia *via = nla_data(nla);
+    int err = -EINVAL;
+    u8 alen;

This should be an int to avoid nexthop address lengths > 255 wrapping and possibly accepted instead of rejected.

ack

Quoted 35 lines
Quoted 35 lines
net/mpls/af_mpls.c +34 -0
+
+    if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
+        goto errout;
+    alen = nla_len(nla) -
+            offsetof(struct rtvia, rtvia_addr);
+    if (alen > MAX_VIA_ALEN)
+        goto errout;
+
+    /* Validate the address family */
+    switch(via->rtvia_family) {
+    case AF_PACKET:
+        *via_table = NEIGH_LINK_TABLE;
+        break;
+    case AF_INET:
+        *via_table = NEIGH_ARP_TABLE;
+        if (alen != 4)
+            goto errout;
+        break;
+    case AF_INET6:
+        *via_table = NEIGH_ND_TABLE;
+        if (alen != 16)
+            goto errout;
+        break;
+    default:
+        /* Unsupported address family */
+        goto errout;
+    }
+
+    memcpy(via_addr, via->rtvia_addr, alen);
+    *via_alen = alen;
+    err = 0;
+
+errout:
+    return err;
Quoted 9 lines

net/mpls/af_mpls.c int nla_get_labels(const struct nlattr *nla, · line 958

Quoted 5 lines
net/mpls/af_mpls.c +2 -0
+}
+
  static int rtm_to_route_config(struct sk_buff *skb,  struct nlmsghdr *nlh,
                     struct mpls_route_config *cfg)
  {

Otherwise, looks good.

Thanks for the review.

#5 lore raw
Quoted 30 lines · roopa
Quoted 21 lines · Robert Shearman
Quoted 20 lines · Roopa Prabhu
       return dev;

}

net/mpls/af_mpls.c +17 -0
+static int mpls_nh_assign_dev(struct net *net, struct mpls_nh *nh, int oif)
+{
+    struct net_device *dev = NULL;
+    int err = -ENODEV;
+
+    dev = find_outdev(net, nh, oif);
+    if (IS_ERR(dev)) {
+        err = PTR_ERR(dev);
+        dev = NULL;
+        goto errout;
+    }
+
+    /* Ensure this is a supported device */
+    err = -EINVAL;
+    if (!mpls_dev_get(dev))
+        goto errout;
+
Quoted 8 lines

net/mpls/af_mpls.c static struct net_device *find_outdev(struct net *net, · line 442

Quoted 6 lines
net/mpls/af_mpls.c +6 -0
+    /* For now just support ethernet devices */
+    if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
+        goto errout;
+
+    RCU_INIT_POINTER(nh->nh_dev, dev);
+    dev_put(dev);

If it's safe to not keep a reference to dev (i.e. because the device cannot go away while a netlink message is being processed), then why not just change find_outdev to use __dev_get_by_index?

Quoted 6 lines

yes, rtnl is held here. dev_get_by_index was used by the original code. So i did not feel the need to change that.
I can see the need to get away with the dev_hold instead of doing a dev_hold with dev_get_by_index, rcu assign and put because of rtnl. But, it seems cleaner to use it the way it is today, given the other cases where the dev is already held during lookup when oif is not specified.

Quoted 1 line

If the dev can go away during this, then we could end up adding an mpls route that points to a freed interface, because until we've added the route to the label table mpls_ifdown won't find it.

dev should not go away because we have rtnl held at this point. no ?
If there is a problem i can think about moving the dev_put's for nexthop devs after mpls_route_update

Yes, either do that or change find_outdev to avoid holding a reference to the dev (which given that it's safe is my preference). The way it is here is slightly confusing as it raises the question in the reader's mind about whether a reference to dev is required or not.

Thanks,
Rob