Skip to contentSkip to conversation

Bare UDP L3 Encapsulation Module

PATCH Superseded Martin Varghese 3 participants 12 messages Nov 16, 2019 – Jan 2, 2020

Subsystems (4)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover Bare UDP L3 Encapsulation Module

From: Martin Varghese <[email protected]>

There are various L3 encapsulation standards using UDP being discussed to leverage the UDP based load balancing capability of different networks. MPLSoUDP (https://tools.ietf.org/html/rfc7510) is one among them.

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling support for tunnelling different L3 protocols like MPLS, IP, NSH etc. inside a UDP tunnel.

Special Handling
----------------
The bareudp device supports special handling for MPLS & IP as they can have multiple ethertypes.
MPLS procotcol can have ethertypes 0x8847 (unicast) & 0x8848 (multicast). IP proctocol can have ethertypes 0x0800 (v4) & 0x86dd (v6).
This special handling can be enabled only for ethertype 0x0800 & 0x8847 with a flag called extended mode.

Usage
------

1. Device creation & deletion

a. ip link add dev bareudp0 type bareudp dstport 6635 ethertype 0x8847

This creates a bareudp tunnel device which tunnels L3 traffic with ethertype 0x8847 (MPLS traffic). The destination port of the UDP header will be set to 6635 The device will listen on UDP port 6635 to receive traffic.

b. ip link delete bareudp0

2. Device creation with extended mode enabled

There are two ways to create a bareudp device for MPLS & IP with extended mode enabled.

a. ip link add dev bareudp0 type bareudp dstport 6635 ethertype 0x8847 extmode 1

b. ip link add dev bareudp0 type bareudp dstport 6635 ethertype mpls

Why not FOU ?
------------
FOU by design does l4 encapsulation.It maps udp port to ipproto (IP protocol number for l4 protocol).

Bareudp acheives a generic l3 encapsulation.It maps udp port to l3 ethertype

For example in the case of MPLS -

In the egress direction an MPLS packet "eth header | mpls header | payload" is encapsulated to "eth header | ip header | udp header | mpls header | payload"

In the Ingress direction the udp tunnel packet
"eth header | ip header | udp header | mpls header | payload" is decapsulated to "eth header | mpls header | payload"

Martin Varghese (2):
UDP tunnel encapsulation module for tunnelling different protocols
like MPLS,IP,NSH etc.
Special handling for IP & MPLS.

 Documentation/networking/bareudp.rst |  44 ++
 Documentation/networking/index.rst   |   1 +
 drivers/net/Kconfig                  |  13 +
 drivers/net/Makefile                 |   1 +
 drivers/net/bareudp.c                | 876 +++++++++++++++++++++++++++++++++++
 include/net/bareudp.h                |  20 +
 include/net/ip6_tunnel.h             |  45 ++
 include/net/ip_tunnels.h             |  42 ++
 include/uapi/linux/if_link.h         |  13 +
 9 files changed, 1055 insertions(+)
 create mode 100644 Documentation/networking/bareudp.rst
 create mode 100644 drivers/net/bareudp.c
 create mode 100644 include/net/bareudp.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v3 1/2 UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

---
Changes in v2:
- Fixed documentation errors.
- Converted documentation to rst format.
- Moved ip tunnel rt lookup code to a common location.
- Removed seperate v4 and v6 socket.
- Added call to skb_ensure_writable before updating ethernet header.
- Simplified bareudp_destroy_tunnels as deleting devices under a namespace is taken care be the default pernet exit code.
- Fixed bareudp_change_mtu.

Changes in v3:
- Re-sending the patch again.

 Documentation/networking/bareudp.rst |  26 ++
 Documentation/networking/index.rst   |   1 +
 drivers/net/Kconfig                  |  13 +
 drivers/net/Makefile                 |   1 +
 drivers/net/bareudp.c                | 808 +++++++++++++++++++++++++++++++++++
 include/net/bareudp.h                |  19 +
 include/net/ip6_tunnel.h             |  45 ++
 include/net/ip_tunnels.h             |  42 ++
 include/uapi/linux/if_link.h         |  12 +
 9 files changed, 967 insertions(+)
 create mode 100644 Documentation/networking/bareudp.rst
 create mode 100644 drivers/net/bareudp.c
 create mode 100644 include/net/bareudp.h
Documentation/networking/bareudp.rst new file +26 -0
new file mode 100644
index 0000000..2828521
@@ -0,0 +1,26 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+========================================
+Bare UDP Tunnelling Module Documentation
+========================================
+
+There are various L3 encapsulation standards using UDP being discussed to
+leverage the UDP based load balancing capability of different networks.
+MPLSoUDP (__ https://tools.ietf.org/html/rfc7510) is one among them.
+
+The Bareudp tunnel module provides a generic L3 encapsulation tunnelling
+support for tunnelling different L3 protocols like MPLS, IP, NSH etc. inside
+a UDP tunnel.
+
+Usage
+------
+
+1) Device creation & deletion
+
+    a) ip link add dev bareudp0 type bareudp dstport 6635 ethertype 0x8847.
+
+       This creates a bareudp tunnel device which tunnels L3 traffic with ethertype
+       0x8847 (MPLS traffic). The destination port of the UDP header will be set to
+       6635.The device will listen on UDP port 6635 to receive traffic.
+
+    b) ip link delete bareudp0
Documentation/networking/index.rst +1 -0
index d4dca42..0e954a3 100644
@@ -33,6 +33,7 @@ Contents:
    scaling
    tls
    tls-offload
+   bareudp
 
 .. only::  subproject and html
 
drivers/net/Kconfig +13 -0
index df1c798..b4b57b5 100644
@@ -215,6 +215,19 @@ config GENEVE
 	  To compile this driver as a module, choose M here: the module
 	  will be called geneve.
 
+config BAREUDP
+       tristate "Bare UDP Encapsulation"
+       depends on INET && NET_UDP_TUNNEL
+       depends on IPV6 || !IPV6
+       select NET_IP_TUNNEL
+       select GRO_CELLS
+       help
+          This adds a bare UDP tunnel module for tunnelling different
+          kinds of traffic like MPLS, IP, etc. inside a UDP tunnel.
+
+          To compile this driver as a module, choose M here: the module
+          will be called bareudp.
+
 config GTP
 	tristate "GPRS Tunneling Protocol datapath (GTP-U)"
 	depends on INET
drivers/net/Makefile +1 -0
index 0d3ba05..0bb7de5 100644
@@ -28,6 +28,7 @@ obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VXLAN) += vxlan.o
 obj-$(CONFIG_GENEVE) += geneve.o
+obj-$(CONFIG_BAREUDP) += bareudp.o
 obj-$(CONFIG_GTP) += gtp.o
 obj-$(CONFIG_NLMON) += nlmon.o
 obj-$(CONFIG_NET_VRF) += vrf.o
drivers/net/bareudp.c new file +808 -0

808 lines not on this page · raw message

include/net/bareudp.h new file +19 -0
new file mode 100644
index 0000000..513fae6
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __NET_BAREUDP_H
+#define __NET_BAREUDP_H
+
+#include <linux/types.h>
+#include <linux/skbuff.h>
+
+struct bareudp_conf {
+	__be16 ethertype;
+	__be16 port;
+	u16 sport_min;
+};
+
+struct net_device *bareudp_dev_create(struct net *net, const char *name,
+				      u8 name_assign_type,
+				      struct bareudp_conf *info);
+
+#endif
include/net/ip6_tunnel.h +45 -0

50 lines not on this page · raw message

include/net/ip_tunnels.h +42 -0

48 lines not on this page · raw message

include/uapi/linux/if_link.h +12 -0
index 4a8c02c..012f7e8 100644
@@ -580,6 +580,18 @@ enum ifla_geneve_df {
 	GENEVE_DF_MAX = __GENEVE_DF_END - 1,
 };
 
+/* Bareudp section  */
+enum {
+	IFLA_BAREUDP_UNSPEC,
+	IFLA_BAREUDP_PORT,
+	IFLA_BAREUDP_ETHERTYPE,
+	IFLA_BAREUDP_SRCPORT_MIN,
+	__IFLA_BAREUDP_MAX
+};
+
+#define IFLA_BAREUDP_MAX (__IFLA_BAREUDP_MAX - 1)
+
+
 /* PPP section */
 enum {
 	IFLA_PPP_UNSPEC,
Signature · 1 line
1.8.3.1
#8 lore raw

PATCH v3 2/2 Special handling for IP & MPLS.

In reply to Martin Varghese

From: Martin Varghese <[email protected]>

Special handling is needed in bareudp module for IP & MPLS as they support more than one ethertypes.

MPLS has 2 ethertypes. 0x8847 for MPLS unicast and 0x8848 for MPLS multicast. While decapsulating MPLS packet from UDP packet the tunnel destination IP address is checked to determine the ethertype. The ethertype of the packet will be set to 0x8848 if the tunnel destination IP address is a multicast IP address. The ethertype of the packet will be set to 0x8847 if the tunnel destination IP address is a unicast IP address.

IP has 2 ethertypes.0x0800 for IPV4 and 0x86dd for IPv6. The version field of the IP header tunnelled will be checked to determine the ethertype.

This special handling to tunnel additional ethertypes will be disabled by default and can be enabled using a flag called ext mode. This flag can be used only with ethertypes 0x8847 and 0x0800.

---
Changes in v2:
- Fixed documentation errors.
- Changed commit message.

Changes in v3:
- Re-sending the patch.

 Documentation/networking/bareudp.rst | 18 ++++++++
 drivers/net/bareudp.c                | 82 +++++++++++++++++++++++++++++++++---
 include/net/bareudp.h                |  1 +
 include/uapi/linux/if_link.h         |  1 +
 4 files changed, 95 insertions(+), 7 deletions(-)
Documentation/networking/bareudp.rst +18 -0
index 2828521..1f01dfd 100644
@@ -12,6 +12,15 @@ The Bareudp tunnel module provides a generic L3 encapsulation tunnelling
 support for tunnelling different L3 protocols like MPLS, IP, NSH etc. inside
 a UDP tunnel.
 
+Special Handling
+----------------
+The bareudp device supports special handling for MPLS & IP as they can have
+multiple ethertypes.
+MPLS procotcol can have ethertypes ETH_P_MPLS_UC  (unicast) & ETH_P_MPLS_MC (multicast).
+IP proctocol can have ethertypes ETH_P_IP (v4) & ETH_P_IPV6 (v6).
+This special handling can be enabled only for ethertypes ETH_P_IP & ETH_P_MPLS_UC
+with a flag called extended mode.
+
 Usage
 ------
 
@@ -24,3 +33,12 @@ Usage
        6635.The device will listen on UDP port 6635 to receive traffic.
 
     b) ip link delete bareudp0
+
+2) Device creation with extended mode enabled
+
+There are two ways to create a bareudp device for MPLS & IP with extended mode
+enabled.
+
+    a) ip link add dev  bareudp0 type bareudp dstport 6635 ethertype 0x8847 extmode
+
+    b) ip link add dev  bareudp0 type bareudp dstport 6635 ethertype mpls
drivers/net/bareudp.c +75 -7

139 lines not on this page · raw message

include/net/bareudp.h +1 -0
index 513fae6..2c121d8 100644
@@ -10,6 +10,7 @@ struct bareudp_conf {
 	__be16 ethertype;
 	__be16 port;
 	u16 sport_min;
+	bool ext_mode;
 };
 
 struct net_device *bareudp_dev_create(struct net *net, const char *name,
include/uapi/linux/if_link.h +1 -0
index 012f7e8..2b91c872 100644
@@ -586,6 +586,7 @@ enum {
 	IFLA_BAREUDP_PORT,
 	IFLA_BAREUDP_ETHERTYPE,
 	IFLA_BAREUDP_SRCPORT_MIN,
+	IFLA_BAREUDP_EXTMODE,
 	__IFLA_BAREUDP_MAX
 };
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

In reply to Martin Varghese
Quoted 7 lines · Martin Varghese

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

Quoted 6 lines

---
Changes in v2:
- Fixed documentation errors.
- Converted documentation to rst format.
- Moved ip tunnel rt lookup code to a common location.

This does not actually deduplicate with the existing code in geneve (and vxlan, maybe others).

Mentioned a few more obvious examples inline. I won't keep harping on this point, as I have no real stake in this.

But if you don't deduplicate from the start, I'm skeptical that it will happen later. And then fixes to one driver are likely to be missed for the other, slowly drifting the code further apart.

Documentation/networking/bareudp.rst line 25

Quoted 8 lines

+1) Device creation & deletion

Documentation/networking/bareudp.rst new file +7 -0
+
+    a) ip link add dev bareudp0 type bareudp dstport 6635 ethertype 0x8847.
+
+       This creates a bareudp tunnel device which tunnels L3 traffic with ethertype
+       0x8847 (MPLS traffic). The destination port of the UDP header will be set to
+       6635.The device will listen on UDP port 6635 to receive traffic.
+

Might be useful to also document how to use this on transmission.

drivers/net/bareudp.c line 45

Quoted 5 lines
drivers/net/bareudp.c new file +5 -0
+struct bareudp_sock {
+       struct socket           *sock;
+       struct rcu_head         rcu;
+       struct bareudp_dev      *bareudp;
+};

Is this intermediate struct needed, or could struct bareudp_dev directly hang off of sk_user_data if it gains a struct rcu_head? Especially now that there is a 1:1 relationship between socket and device.

drivers/net/bareudp.c line 112

Quoted 12 lines
drivers/net/bareudp.c new file +12 -0
+       skb_push(skb, sizeof(struct ethhdr));
+       skb_reset_mac_header(skb);
+
+       err = skb_ensure_writable(skb, sizeof(struct ethhdr));
+       if (unlikely(err))
+               goto drop;
+
+       eh = (struct ethhdr *)skb->data;
+       eh->h_proto = proto;
+       skb->protocol = eth_type_trans(skb, bareudp->dev);
+       oiph = skb_network_header(skb);
+       skb_reset_network_header(skb);

After decapsulation packets now have a fake ethernet header with garbage in the src and dst address. Tcpdump will show that.

Instead of pseudo Ethernet, should this device be of link layer type ARPHRD_NONE. Or alternatively a new type analogous to ARPHRD_TUNNEL that includes the udp header?

The same can probably be said about geneve, so no strong objection to what is here. Just a point for thought.

drivers/net/bareudp.c line 119

Quoted 6 lines
drivers/net/bareudp.c new file +6 -0
+       if (bareudp_get_sk_family(bs) == AF_INET)
+               err = IP_ECN_decapsulate(oiph, skb);
+#if IS_ENABLED(CONFIG_IPV6)
+       else
+               err = IP6_ECN_decapsulate(oiph, skb);
+#endif

with dual stack sockets, should this check now be based on packet header instead of bareudp_get_sk_family?

Quoted 26 lines
drivers/net/bareudp.c new file +26 -0
+       if (unlikely(err)) {
+               if (log_ecn_error) {
+                       if (bareudp_get_sk_family(bs) == AF_INET)
+                               net_info_ratelimited("non-ECT from %pI4 "
+                                                    "with TOS=%#x\n",
+                                                    &((struct iphdr *)oiph)->saddr,
+                                                    ((struct iphdr *)oiph)->tos);
+#if IS_ENABLED(CONFIG_IPV6)
+                       else
+                               net_info_ratelimited("non-ECT from %pI6\n",
+                                                    &((struct ipv6hdr *)oiph)->saddr);
+#endif
+               }
+               if (err > 1) {
+                       ++bareudp->dev->stats.rx_frame_errors;
+                       ++bareudp->dev->stats.rx_errors;
+                       goto drop;
+               }
+       }
+
+       len = skb->len;
+       err = gro_cells_receive(&bareudp->gro_cells, skb);
+       if (likely(err == NET_RX_SUCCESS)) {
+               stats = this_cpu_ptr(bareudp->dev->tstats);
+               u64_stats_update_begin(&stats->syncp);
+               stats->rx_packets++;

drivers/net/bareudp.c line 156

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+               stats->rx_bytes += len;
+               u64_stats_update_end(&stats->syncp);
+       }
+       return 0;
+drop:
+       /* Consume bad packet */
+       kfree_skb(skb);
+
+       return 0;
+}

All of this can pretty easily be shared with geneve.

drivers/net/bareudp.c line 170

Quoted 13 lines
drivers/net/bareudp.c new file +13 -0
+static struct socket *bareudp_create_sock(struct net *net, bool ipv6,
+                                         __be16 port)
+{
+       struct socket *sock;
+       struct udp_port_cfg udp_conf;
+       int err;
+
+       memset(&udp_conf, 0, sizeof(udp_conf));
+#if IS_ENABLED(CONFIG_IPV6)
+       udp_conf.family = AF_INET6;
+#else
+       udp_conf.family = AF_INET;
+#endif

Still need to take bool ipv6 in account when creating the socket?

drivers/net/bareudp.c line 232

Quoted 13 lines
drivers/net/bareudp.c new file +13 -0
+static int bareudp_sock_add(struct bareudp_dev *bareudp, bool ipv6)
+{
+       struct net *net = bareudp->net;
+       struct bareudp_sock *bs;
+
+       bs = bareudp_socket_create(net, bareudp->conf.port, ipv6);
+       if (IS_ERR(bs))
+               return PTR_ERR(bs);
+
+       rcu_assign_pointer(bareudp->sock, bs);
+       bs->bareudp = bareudp;
+
+       return 0;

Especially now that the device only has one socket, probably no need to have a separate bareudp_socket_create from bareudp_sock_add. Same for __bareudp_sock_release and bareudp_sock_release.

Quoted 52 lines
drivers/net/bareudp.c new file +52 -0
+}
+
+static void __bareudp_sock_release(struct bareudp_sock *bs)
+{
+       if (!bs)
+               return;
+
+       udp_tunnel_sock_release(bs->sock);
+       kfree_rcu(bs, rcu);
+}
+
+static void bareudp_sock_release(struct bareudp_dev *bareudp)
+{
+       struct bareudp_sock *bs = rtnl_dereference(bareudp->sock);
+
+       rcu_assign_pointer(bareudp->sock, NULL);
+       synchronize_net();
+       __bareudp_sock_release(bs);
+}
+
+static int bareudp_fill_metadata_dst(struct net_device *dev,
+                                    struct sk_buff *skb)
+{
+       struct ip_tunnel_info *info = skb_tunnel_info(skb);
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
+
+       if (ip_tunnel_info_af(info) == AF_INET) {
+               struct rtable *rt;
+               struct flowi4 fl4;
+
+               rt = iptunnel_get_v4_rt(skb, dev, bareudp->net, &fl4, info,
+                                       use_cache);
+               if (IS_ERR(rt))
+                       return PTR_ERR(rt);
+
+               ip_rt_put(rt);
+               info->key.u.ipv4.src = fl4.saddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (ip_tunnel_info_af(info) == AF_INET6) {
+               struct dst_entry *dst;
+               struct flowi6 fl6;
+               struct bareudp_sock *bs6 = rcu_dereference(bareudp->sock);
+
+               dst = ip6tunnel_get_dst(skb, dev, bareudp->net, bs6->sock, &fl6,
+                                       info, use_cache);
+               if (IS_ERR(dst))
+                       return PTR_ERR(dst);
+
+               dst_release(dst);
+               info->key.u.ipv6.src = fl6.saddr;
+#endif

drivers/net/bareudp.c line 294

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+       } else {
+               return -EINVAL;
+       }
+
+       info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
+                                            bareudp->sport_min,
+                                            USHRT_MAX, true);
+       info->key.tp_dst = bareudp->conf.port;
+       return 0;
+}

This can probably all be deduplicated with geneve_fill_metadata_dst once both use iptunnel_get_v4_rt.

drivers/net/bareudp.c line 428

Quoted 12 lines
drivers/net/bareudp.c new file +12 -0
+static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       struct ip_tunnel_info *info = NULL;
+       int err;
+
+       if (skb->protocol != bareudp->ethertype) {
+               err = -EINVAL;
+               goto tx_error;
+       }
+
+       info = skb_tunnel_info(skb);

Like geneve, should this have two modes, either associated with LWT if geneve->collect_md, else static for the device (geneve->info)? As opposed to requiring collect_md mode.

Quoted 11 lines
drivers/net/bareudp.c new file +11 -0
+/* Initialize the device structure. */
+static void bareudp_setup(struct net_device *dev)
+{
+       ether_setup(dev);
+
+       dev->netdev_ops = &bareudp_netdev_ops;
+       dev->ethtool_ops = &bareudp_ethtool_ops;
+       dev->needs_free_netdev = true;
+
+       SET_NETDEV_DEVTYPE(dev, &bareudp_type);
+

drivers/net/bareudp.c line 572

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+       dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM;
+       dev->features    |= NETIF_F_RXCSUM;
+       dev->features    |= NETIF_F_GSO_SOFTWARE;
+
+       dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
+       dev->hw_features |= NETIF_F_GSO_SOFTWARE;
+
+       /* MTU range: 68 - (something less than 65535) */
+       dev->min_mtu = ETH_MIN_MTU;
+       dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN - dev->hard_header_len;

For IPv6, MSS is 64 kB without headers, so dev->max_mtu could perhaps be IP_MAX_MTU. Also, why is hard_header_len subtracted? Again, same for geneve, so if it works there, fine to leave here. Just something that surprised me and might be worth giving another thought.

#9 lore raw

Re: [PATCH v3 net-next 2/2] Special handling for IP & MPLS.

In reply to Martin Varghese
Quoted 13 lines · Martin Varghese

From: Martin Varghese <[email protected]>

Special handling is needed in bareudp module for IP & MPLS as they support more than one ethertypes.

MPLS has 2 ethertypes. 0x8847 for MPLS unicast and 0x8848 for MPLS multicast. While decapsulating MPLS packet from UDP packet the tunnel destination IP address is checked to determine the ethertype. The ethertype of the packet will be set to 0x8848 if the tunnel destination IP address is a multicast IP address. The ethertype of the packet will be set to 0x8847 if the tunnel destination IP address is a unicast IP address.

Quoted 2 lines

IP has 2 ethertypes.0x0800 for IPV4 and 0x86dd for IPv6. The version field of the IP header tunnelled will be checked to determine the ethertype.

If using ipv6 dual stack, it might make more sense to use extended mode with the ipv6 device instead of the ipv4 device.

Also, the term extended mode is not self describing. Dual stack as term would be, but is not relevant to MPLS. Maybe "dual_proto"?

Quoted 5 lines
Documentation/networking/bareudp.rst +0 -0
index 2828521..1f01dfd 100644

Documentation/networking/bareudp.rst The Bareudp tunnel module provides a generic L3 encapsulation tunnelling · line 20

Quoted 9 lines
Documentation/networking/bareudp.rst +6 -0
@@ -12,6 +12,15 @@ The Bareudp tunnel module provides a generic L3 encapsulation tunnelling
 support for tunnelling different L3 protocols like MPLS, IP, NSH etc. inside
 a UDP tunnel.
 
+Special Handling
+----------------
+The bareudp device supports special handling for MPLS & IP as they can have
+multiple ethertypes.
+MPLS procotcol can have ethertypes ETH_P_MPLS_UC  (unicast) & ETH_P_MPLS_MC (multicast).
+IP proctocol can have ethertypes ETH_P_IP (v4) & ETH_P_IPV6 (v6).

proctocol -> protocol

#7 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

In reply to Martin Varghese
Thank you for the patch! Yet something to improve:

Hi Martin,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on net-next/master]
[also build test ERROR on v5.4-rc8 next-20191120]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Martin-Varghese/Bare-UDP-L3-Encapsulation-Module/20191116-135036
base:   https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git 20021578ba226bda1f0ddf50e4d4a12ea1c6c6c1
config: powerpc-tqm8560_defconfig (attached as .config)
compiler: powerpc-linux-gcc (GCC) 7.4.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=powerpc

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from include/net/dst_metadata.h:6:0,

                    from net/core/dev.c:101:
   include/net/ip_tunnels.h: In function 'iptunnel_get_v4_rt':
>> include/net/ip_tunnels.h:514:39: error: 'const struct ip_tunnel_info' has no member named 'dst_cache'
     dst_cache = (struct dst_cache *)&info->dst_cache;
                                          ^~
--
   In file included from net/ipv4/ip_tunnel_core.c:26:0:
   include/net/ip_tunnels.h: In function 'iptunnel_get_v4_rt':
>> include/net/ip_tunnels.h:514:39: error: 'const struct ip_tunnel_info' has no member named 'dst_cache'
     dst_cache = (struct dst_cache *)&info->dst_cache;
                                          ^~
   In file included from net/ipv4/ip_tunnel_core.c:27:0:
   include/net/ip6_tunnel.h: In function 'ip6tunnel_get_dst':
>> include/net/ip6_tunnel.h:191:39: error: 'const struct ip_tunnel_info' has no member named 'dst_cache'
     dst_cache = (struct dst_cache *)&info->dst_cache;
                                          ^~
>> include/net/ip6_tunnel.h:193:9: error: implicit declaration of function 'dst_cache_get_ip6'; did you mean 'dst_cache_get_ip4'? [-Werror=implicit-function-declaration]
      dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
            ^~~~~~~~~~~~~~~~~
            dst_cache_get_ip4
   include/net/ip6_tunnel.h:193:7: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
      dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
          ^
>> include/net/ip6_tunnel.h:209:3: error: implicit declaration of function 'dst_cache_set_ip6'; did you mean 'dst_cache_set_ip4'? [-Werror=implicit-function-declaration]
      dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
      ^~~~~~~~~~~~~~~~~
      dst_cache_set_ip4

cc1: some warnings being treated as errors

vim +514 include/net/ip_tunnels.h

492
493 static inline struct rtable *iptunnel_get_v4_rt(struct sk_buff *skb,
494 struct net_device *dev,
495 struct net *net,
496 struct flowi4 *fl4,
497 const struct ip_tunnel_info *info,
498 bool use_cache)
499 {
500 struct dst_cache *dst_cache;
501 struct rtable *rt = NULL;
502 __u8 tos;
503
504
505 memset(fl4, 0, sizeof(*fl4));
506 fl4->flowi4_mark = skb->mark;
507 fl4->flowi4_proto = IPPROTO_UDP;
508 fl4->daddr = info->key.u.ipv4.dst;
509 fl4->saddr = info->key.u.ipv4.src;
510
511 tos = info->key.tos;
512 fl4->flowi4_tos = RT_TOS(tos);
513

 > 514		dst_cache = (struct dst_cache *)&info->dst_cache;

515 if (use_cache) {
516 rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
517 if (rt)
518 return rt;
519 }
520 rt = ip_route_output_key(net, fl4);
521 if (IS_ERR(rt)) {
522 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
523 return ERR_PTR(-ENETUNREACH);
524 }
525 if (rt->dst.dev == dev) { /* is this necessary? */
526 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
527 ip_rt_put(rt);
528 return ERR_PTR(-ELOOP);
529 }
530 if (use_cache)
531 dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
532 return rt;
533 }
534

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

#10 lore raw

Re: [PATCH v3 net-next 2/2] Special handling for IP & MPLS.

In reply to Willem de Bruijn
Quoted 15 lines · Willem de Bruijn
Quoted 13 lines · Martin Varghese

From: Martin Varghese <[email protected]>

Special handling is needed in bareudp module for IP & MPLS as they support more than one ethertypes.

MPLS has 2 ethertypes. 0x8847 for MPLS unicast and 0x8848 for MPLS multicast. While decapsulating MPLS packet from UDP packet the tunnel destination IP address is checked to determine the ethertype. The ethertype of the packet will be set to 0x8848 if the tunnel destination IP address is a multicast IP address. The ethertype of the packet will be set to 0x8847 if the tunnel destination IP address is a unicast IP address.

Quoted 6 lines
Quoted 2 lines

IP has 2 ethertypes.0x0800 for IPV4 and 0x86dd for IPv6. The version field of the IP header tunnelled will be checked to determine the ethertype.

If using ipv6 dual stack, it might make more sense to use extended mode with the ipv6 device instead of the ipv4 device.

ipv6 dual stack (v6 socket) is for the tunnel.the ethertype mentioned above is for the inner protocol being tunnelled

Quoted 3 lines

Also, the term extended mode is not self describing. Dual stack as term would be, but is not relevant to MPLS. Maybe "dual_proto"?

multi_proto ?

Quoted 8 lines
Quoted 8 lines
Documentation/networking/bareudp.rst +0 -0
index 2828521..1f01dfd 100644
@@ -12,6 +12,15 @@ The Bareudp tunnel module provides a generic L3 encapsulation tunnelling
 support for tunnelling different L3 protocols like MPLS, IP, NSH etc. inside
 a UDP tunnel.
 
Quoted 8 lines

Documentation/networking/bareudp.rst The Bareudp tunnel module provides a generic L3 encapsulation tunnelling · line 20

Quoted 6 lines
Documentation/networking/bareudp.rst +6 -0
+Special Handling
+----------------
+The bareudp device supports special handling for MPLS & IP as they can have
+multiple ethertypes.
+MPLS procotcol can have ethertypes ETH_P_MPLS_UC  (unicast) & ETH_P_MPLS_MC (multicast).
+IP proctocol can have ethertypes ETH_P_IP (v4) & ETH_P_IPV6 (v6).

proctocol -> protocol

Noted.

Thanks for your time

#3 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

In reply to Willem de Bruijn
Quoted 19 lines · Willem de Bruijn
Quoted 7 lines · Martin Varghese

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

Quoted 6 lines

---
Changes in v2:
- Fixed documentation errors.
- Converted documentation to rst format.
- Moved ip tunnel rt lookup code to a common location.

This does not actually deduplicate with the existing code in geneve (and vxlan, maybe others).

Quoted 7 lines

Mentioned a few more obvious examples inline. I won't keep harping on this point, as I have no real stake in this.

But if you don't deduplicate from the start, I'm skeptical that it will happen later. And then fixes to one driver are likely to be missed for the other, slowly drifting the code further apart.

Adaptation of geneve module to use the generic API will be in a seperate patch series.

Quoted 11 lines

Documentation/networking/bareudp.rst line 25

Quoted 8 lines

+1) Device creation & deletion

Documentation/networking/bareudp.rst new file +7 -0
+
+    a) ip link add dev bareudp0 type bareudp dstport 6635 ethertype 0x8847.
+
+       This creates a bareudp tunnel device which tunnels L3 traffic with ethertype
+       0x8847 (MPLS traffic). The destination port of the UDP header will be set to
+       6635.The device will listen on UDP port 6635 to receive traffic.
+

Might be useful to also document how to use this on transmission.

Quoted 11 lines

drivers/net/bareudp.c line 45

Quoted 5 lines
drivers/net/bareudp.c new file +5 -0
+struct bareudp_sock {
+       struct socket           *sock;
+       struct rcu_head         rcu;
+       struct bareudp_dev      *bareudp;
+};

Is this intermediate struct needed, or could struct bareudp_dev directly hang off of sk_user_data if it gains a struct rcu_head? Especially now that there is a 1:1 relationship between socket and device.

will check.

Quoted 16 lines

drivers/net/bareudp.c line 112

Quoted 12 lines
drivers/net/bareudp.c new file +12 -0
+       skb_push(skb, sizeof(struct ethhdr));
+       skb_reset_mac_header(skb);
+
+       err = skb_ensure_writable(skb, sizeof(struct ethhdr));
+       if (unlikely(err))
+               goto drop;
+
+       eh = (struct ethhdr *)skb->data;
+       eh->h_proto = proto;
+       skb->protocol = eth_type_trans(skb, bareudp->dev);
+       oiph = skb_network_header(skb);
+       skb_reset_network_header(skb);

After decapsulation packets now have a fake ethernet header with garbage in the src and dst address. Tcpdump will show that.

Quoted 7 lines

Instead of pseudo Ethernet, should this device be of link layer type ARPHRD_NONE. Or alternatively a new type analogous to ARPHRD_TUNNEL that includes the udp header?

The same can probably be said about geneve, so no strong objection to what is here. Just a point for thought.

I am pondering more on this comment.

Quoted 10 lines

drivers/net/bareudp.c line 119

Quoted 6 lines
drivers/net/bareudp.c new file +6 -0
+       if (bareudp_get_sk_family(bs) == AF_INET)
+               err = IP_ECN_decapsulate(oiph, skb);
+#if IS_ENABLED(CONFIG_IPV6)
+       else
+               err = IP6_ECN_decapsulate(oiph, skb);
+#endif

with dual stack sockets, should this check now be based on packet header instead of bareudp_get_sk_family?

Noted

Quoted 30 lines
Quoted 30 lines
drivers/net/bareudp.c new file +30 -0
+       if (unlikely(err)) {
+               if (log_ecn_error) {
+                       if (bareudp_get_sk_family(bs) == AF_INET)
+                               net_info_ratelimited("non-ECT from %pI4 "
+                                                    "with TOS=%#x\n",
+                                                    &((struct iphdr *)oiph)->saddr,
+                                                    ((struct iphdr *)oiph)->tos);
+#if IS_ENABLED(CONFIG_IPV6)
+                       else
+                               net_info_ratelimited("non-ECT from %pI6\n",
+                                                    &((struct ipv6hdr *)oiph)->saddr);
+#endif
+               }
+               if (err > 1) {
+                       ++bareudp->dev->stats.rx_frame_errors;
+                       ++bareudp->dev->stats.rx_errors;
+                       goto drop;
+               }
+       }
+
+       len = skb->len;
+       err = gro_cells_receive(&bareudp->gro_cells, skb);
+       if (likely(err == NET_RX_SUCCESS)) {
+               stats = this_cpu_ptr(bareudp->dev->tstats);
+               u64_stats_update_begin(&stats->syncp);
+               stats->rx_packets++;
+               stats->rx_bytes += len;
+               u64_stats_update_end(&stats->syncp);
+       }
+       return 0;
Quoted 9 lines

drivers/net/bareudp.c line 156

Quoted 6 lines
drivers/net/bareudp.c new file +6 -0
+drop:
+       /* Consume bad packet */
+       kfree_skb(skb);
+
+       return 0;
+}

All of this can pretty easily be shared with geneve.

if we remove the bareudp_get_sk_family(bs) check.we may mot have lot of common code.will check.

Quoted 7 lines
Quoted 7 lines
drivers/net/bareudp.c new file +7 -0
+static struct socket *bareudp_create_sock(struct net *net, bool ipv6,
+                                         __be16 port)
+{
+       struct socket *sock;
+       struct udp_port_cfg udp_conf;
+       int err;
+
Quoted 9 lines

drivers/net/bareudp.c line 170

Quoted 6 lines
drivers/net/bareudp.c new file +6 -0
+       memset(&udp_conf, 0, sizeof(udp_conf));
+#if IS_ENABLED(CONFIG_IPV6)
+       udp_conf.family = AF_INET6;
+#else
+       udp_conf.family = AF_INET;
+#endif

Still need to take bool ipv6 in account when creating the socket?

noted

Quoted 9 lines
Quoted 9 lines
drivers/net/bareudp.c new file +9 -0
+static int bareudp_sock_add(struct bareudp_dev *bareudp, bool ipv6)
+{
+       struct net *net = bareudp->net;
+       struct bareudp_sock *bs;
+
+       bs = bareudp_socket_create(net, bareudp->conf.port, ipv6);
+       if (IS_ERR(bs))
+               return PTR_ERR(bs);
+
Quoted 9 lines

drivers/net/bareudp.c line 232

Quoted 4 lines
drivers/net/bareudp.c new file +4 -0
+       rcu_assign_pointer(bareudp->sock, bs);
+       bs->bareudp = bareudp;
+
+       return 0;

Especially now that the device only has one socket, probably no need to have a separate bareudp_socket_create from bareudp_sock_add. Same for __bareudp_sock_release and bareudp_sock_release.

NOted

Quoted 57 lines
Quoted 57 lines
drivers/net/bareudp.c new file +57 -0
+}
+
+static void __bareudp_sock_release(struct bareudp_sock *bs)
+{
+       if (!bs)
+               return;
+
+       udp_tunnel_sock_release(bs->sock);
+       kfree_rcu(bs, rcu);
+}
+
+static void bareudp_sock_release(struct bareudp_dev *bareudp)
+{
+       struct bareudp_sock *bs = rtnl_dereference(bareudp->sock);
+
+       rcu_assign_pointer(bareudp->sock, NULL);
+       synchronize_net();
+       __bareudp_sock_release(bs);
+}
+
+static int bareudp_fill_metadata_dst(struct net_device *dev,
+                                    struct sk_buff *skb)
+{
+       struct ip_tunnel_info *info = skb_tunnel_info(skb);
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
+
+       if (ip_tunnel_info_af(info) == AF_INET) {
+               struct rtable *rt;
+               struct flowi4 fl4;
+
+               rt = iptunnel_get_v4_rt(skb, dev, bareudp->net, &fl4, info,
+                                       use_cache);
+               if (IS_ERR(rt))
+                       return PTR_ERR(rt);
+
+               ip_rt_put(rt);
+               info->key.u.ipv4.src = fl4.saddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (ip_tunnel_info_af(info) == AF_INET6) {
+               struct dst_entry *dst;
+               struct flowi6 fl6;
+               struct bareudp_sock *bs6 = rcu_dereference(bareudp->sock);
+
+               dst = ip6tunnel_get_dst(skb, dev, bareudp->net, bs6->sock, &fl6,
+                                       info, use_cache);
+               if (IS_ERR(dst))
+                       return PTR_ERR(dst);
+
+               dst_release(dst);
+               info->key.u.ipv6.src = fl6.saddr;
+#endif
+       } else {
+               return -EINVAL;
+       }
+
+       info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
Quoted 9 lines

drivers/net/bareudp.c line 294

Quoted 5 lines
drivers/net/bareudp.c new file +5 -0
+                                            bareudp->sport_min,
+                                            USHRT_MAX, true);
+       info->key.tp_dst = bareudp->conf.port;
+       return 0;
+}

This can probably all be deduplicated with geneve_fill_metadata_dst once both use iptunnel_get_v4_rt.

Do you have any preference of file to keep the common function

Quoted 18 lines

drivers/net/bareudp.c line 428

Quoted 13 lines
drivers/net/bareudp.c new file +12 -0
+static netdev_tx_t bareudp_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       struct ip_tunnel_info *info = NULL;
+       int err;
+
+       if (skb->protocol != bareudp->ethertype) {
+               err = -EINVAL;
+               goto tx_error;
+       }
+
+       info = skb_tunnel_info(skb);

Like geneve, should this have two modes, either associated with LWT if geneve->collect_md, else static for the device (geneve->info)? As opposed to requiring collect_md mode.

This module will be used with OVS mostly and so it need not support static mode.

Quoted 26 lines
Quoted 11 lines
drivers/net/bareudp.c new file +11 -0
+/* Initialize the device structure. */
+static void bareudp_setup(struct net_device *dev)
+{
+       ether_setup(dev);
+
+       dev->netdev_ops = &bareudp_netdev_ops;
+       dev->ethtool_ops = &bareudp_ethtool_ops;
+       dev->needs_free_netdev = true;
+
+       SET_NETDEV_DEVTYPE(dev, &bareudp_type);
+

drivers/net/bareudp.c line 572

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+       dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM;
+       dev->features    |= NETIF_F_RXCSUM;
+       dev->features    |= NETIF_F_GSO_SOFTWARE;
+
+       dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
+       dev->hw_features |= NETIF_F_GSO_SOFTWARE;
+
+       /* MTU range: 68 - (something less than 65535) */
+       dev->min_mtu = ETH_MIN_MTU;
+       dev->max_mtu = IP_MAX_MTU - BAREUDP_BASE_HLEN - dev->hard_header_len;

For IPv6, MSS is 64 kB without headers, so dev->max_mtu could perhaps be IP_MAX_MTU. Also, why is hard_header_len subtracted? Again, same for geneve, so if it works there, fine to leave here. Just something that surprised me and might be worth giving another thought.

#4 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

Quoted 60 lines · Martin Varghese
Quoted 48 lines · Willem de Bruijn
Quoted 46 lines · Martin Varghese

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

drivers/net/bareudp.c new file +37 -0
+static int bareudp_fill_metadata_dst(struct net_device *dev,
+                                    struct sk_buff *skb)
+{
+       struct ip_tunnel_info *info = skb_tunnel_info(skb);
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
+
+       if (ip_tunnel_info_af(info) == AF_INET) {
+               struct rtable *rt;
+               struct flowi4 fl4;
+
+               rt = iptunnel_get_v4_rt(skb, dev, bareudp->net, &fl4, info,
+                                       use_cache);
+               if (IS_ERR(rt))
+                       return PTR_ERR(rt);
+
+               ip_rt_put(rt);
+               info->key.u.ipv4.src = fl4.saddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (ip_tunnel_info_af(info) == AF_INET6) {
+               struct dst_entry *dst;
+               struct flowi6 fl6;
+               struct bareudp_sock *bs6 = rcu_dereference(bareudp->sock);
+
+               dst = ip6tunnel_get_dst(skb, dev, bareudp->net, bs6->sock, &fl6,
+                                       info, use_cache);
+               if (IS_ERR(dst))
+                       return PTR_ERR(dst);
+
+               dst_release(dst);
+               info->key.u.ipv6.src = fl6.saddr;
+#endif
+       } else {
+               return -EINVAL;
+       }
+
+       info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
Quoted 9 lines

drivers/net/bareudp.c line 294

Quoted 5 lines
drivers/net/bareudp.c new file +5 -0
+                                            bareudp->sport_min,
+                                            USHRT_MAX, true);
+       info->key.tp_dst = bareudp->conf.port;
+       return 0;
+}

This can probably all be deduplicated with geneve_fill_metadata_dst once both use iptunnel_get_v4_rt.

Quoted 1 line

Do you have any preference of file to keep the common function

Perhaps net/ipv4/udp_tunnel.c

#11 lore raw

Re: [PATCH v3 net-next 2/2] Special handling for IP & MPLS.

In reply to Martin Varghese
Quoted 23 lines · Martin Varghese
Quoted 15 lines · Willem de Bruijn
Quoted 13 lines · Martin Varghese

From: Martin Varghese <[email protected]>

Special handling is needed in bareudp module for IP & MPLS as they support more than one ethertypes.

MPLS has 2 ethertypes. 0x8847 for MPLS unicast and 0x8848 for MPLS multicast. While decapsulating MPLS packet from UDP packet the tunnel destination IP address is checked to determine the ethertype. The ethertype of the packet will be set to 0x8848 if the tunnel destination IP address is a multicast IP address. The ethertype of the packet will be set to 0x8847 if the tunnel destination IP address is a unicast IP address.

Quoted 6 lines
Quoted 2 lines

IP has 2 ethertypes.0x0800 for IPV4 and 0x86dd for IPv6. The version field of the IP header tunnelled will be checked to determine the ethertype.

If using ipv6 dual stack, it might make more sense to use extended mode with the ipv6 device instead of the ipv4 device.

Quoted 7 lines

ipv6 dual stack (v6 socket) is for the tunnel.the ethertype mentioned above is for the inner protocol being tunnelled

Quoted 3 lines

Also, the term extended mode is not self describing. Dual stack as term would be, but is not relevant to MPLS. Maybe "dual_proto"?

multi_proto ?

Sounds good to me, thanks.

#5 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

In reply to Willem de Bruijn
Quoted 62 lines · Willem de Bruijn
Quoted 60 lines · Martin Varghese
Quoted 57 lines · Willem de Bruijn
Quoted 41 lines · Martin Varghese

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

drivers/net/bareudp.c new file +32 -0
+static int bareudp_fill_metadata_dst(struct net_device *dev,
+                                    struct sk_buff *skb)
+{
+       struct ip_tunnel_info *info = skb_tunnel_info(skb);
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
+
+       if (ip_tunnel_info_af(info) == AF_INET) {
+               struct rtable *rt;
+               struct flowi4 fl4;
+
+               rt = iptunnel_get_v4_rt(skb, dev, bareudp->net, &fl4, info,
+                                       use_cache);
+               if (IS_ERR(rt))
+                       return PTR_ERR(rt);
+
+               ip_rt_put(rt);
+               info->key.u.ipv4.src = fl4.saddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (ip_tunnel_info_af(info) == AF_INET6) {
+               struct dst_entry *dst;
+               struct flowi6 fl6;
+               struct bareudp_sock *bs6 = rcu_dereference(bareudp->sock);
+
+               dst = ip6tunnel_get_dst(skb, dev, bareudp->net, bs6->sock, &fl6,
+                                       info, use_cache);
+               if (IS_ERR(dst))
+                       return PTR_ERR(dst);
+
+               dst_release(dst);
+               info->key.u.ipv6.src = fl6.saddr;
+#endif

drivers/net/bareudp.c line 294

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+       } else {
+               return -EINVAL;
+       }
+
+       info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
+                                            bareudp->sport_min,
+                                            USHRT_MAX, true);
+       info->key.tp_dst = bareudp->conf.port;
+       return 0;
+}

This can probably all be deduplicated with geneve_fill_metadata_dst once both use iptunnel_get_v4_rt.

Quoted 3 lines
Quoted 1 line

Do you have any preference of file to keep the common function

Perhaps net/ipv4/udp_tunnel.c

I was trying this change and i found i dont have a lot of generic code here.

Populating L4 ports is function of the protocol implementation and it is differnt for geneve and for bareudp

The one thing we could do is to write a generic API to derive the tunnel src address.

But since it is a very small piece i dont see much value add by making it generic and i prefer to keep the way as it is now

But i am open for both the option. What you think ?

#6 lore raw

Re: [PATCH v3 net-next 1/2] UDP tunnel encapsulation module for tunnelling different protocols like MPLS,IP,NSH etc.

Quoted 64 lines · Martin Varghese
Quoted 62 lines · Willem de Bruijn
Quoted 60 lines · Martin Varghese
Quoted 57 lines · Willem de Bruijn
Quoted 41 lines · Martin Varghese

From: Martin Varghese <[email protected]>

The Bareudp tunnel module provides a generic L3 encapsulation tunnelling module for tunnelling different protocols like MPLS, IP,NSH etc inside a UDP tunnel.

drivers/net/bareudp.c new file +32 -0
+static int bareudp_fill_metadata_dst(struct net_device *dev,
+                                    struct sk_buff *skb)
+{
+       struct ip_tunnel_info *info = skb_tunnel_info(skb);
+       struct bareudp_dev *bareudp = netdev_priv(dev);
+       bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
+
+       if (ip_tunnel_info_af(info) == AF_INET) {
+               struct rtable *rt;
+               struct flowi4 fl4;
+
+               rt = iptunnel_get_v4_rt(skb, dev, bareudp->net, &fl4, info,
+                                       use_cache);
+               if (IS_ERR(rt))
+                       return PTR_ERR(rt);
+
+               ip_rt_put(rt);
+               info->key.u.ipv4.src = fl4.saddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (ip_tunnel_info_af(info) == AF_INET6) {
+               struct dst_entry *dst;
+               struct flowi6 fl6;
+               struct bareudp_sock *bs6 = rcu_dereference(bareudp->sock);
+
+               dst = ip6tunnel_get_dst(skb, dev, bareudp->net, bs6->sock, &fl6,
+                                       info, use_cache);
+               if (IS_ERR(dst))
+                       return PTR_ERR(dst);
+
+               dst_release(dst);
+               info->key.u.ipv6.src = fl6.saddr;
+#endif

drivers/net/bareudp.c line 294

Quoted 10 lines
drivers/net/bareudp.c new file +10 -0
+       } else {
+               return -EINVAL;
+       }
+
+       info->key.tp_src = udp_flow_src_port(bareudp->net, skb,
+                                            bareudp->sport_min,
+                                            USHRT_MAX, true);
+       info->key.tp_dst = bareudp->conf.port;
+       return 0;
+}

This can probably all be deduplicated with geneve_fill_metadata_dst once both use iptunnel_get_v4_rt.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Do you have any preference of file to keep the common function

Perhaps net/ipv4/udp_tunnel.c

I was trying this change and i found i dont have a lot of generic code here.

Populating L4 ports is function of the protocol implementation and it is differnt for geneve and for bareudp

If geneve can use iptunnel_get_v4_rt, then it seems like the only implementation specific logic is deriving net and sk. Would it help to just pass those explicitly to the shared helper (udptunnel_fill_metadata_dst)?

It is hard to say without seeing the geneve conversion to iptunnel_get_v4_rt. Maybe that throws up a constraint I'm missing. Lacking that, I'll trust your judgment.

Quoted 5 lines

The one thing we could do is to write a generic API to derive the tunnel src address.

But since it is a very small piece i dont see much value add by making it generic and i prefer to keep the way as it is now

In that case, that sounds sensible.

Quoted 5 lines

But i am open for both the option. What you think ?