Skip to contentSkip to conversation

xfrm: support collect metadata mode for xfrm interfaces

PATCH Superseded Eyal Birger 3 participants 10 messages Aug 25, 2022 – Aug 26, 2022

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover xfrm: support collect metadata mode for xfrm interfaces

From: Eyal Birger <[email protected]>

This series adds support for "collect_md" mode in XFRM interfaces.

This feature is useful for maintaining a large number of IPsec connections with the benefits of using a network interface while reducing the overhead of maintaining a large number of devices.

Currently this is possible by having multiple connections share a common interface by sharing the if_id identifier and using some other criteria to distinguish between them - such as different subnets or skb marks. This becomes complex in multi-tenant environments where subnets collide and the mark space is used for other purposes.

Since the xfrm interface uses the if_id as the differentiator when looking for policies, setting the if_id in the dst_metadata framework allows using a single interface for different connections while having the ability to selectively steer traffic to each one. In addition the xfrm interface "link" property can also be specified to affect underlying routing in the context of VRFs.

The series is composed of the following steps:

- Introduce a new METADATA_XFRM metadata type to be used for this purpose. Reuse of the existing "METADATA_IP_TUNNEL" type was rejected in [0] as XFRM does not necessarily represent an IP tunnel.

- Add support for collect metadata mode in xfrm interfaces

- Allow setting the XFRM metadata from the LWT infrastructure

Future additions could allow setting/getting the XFRM metadata from eBPF programs, TC, OVS, NF, etc.

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/#23824575

Eyal Birger (3):
net: allow storing xfrm interface metadata in metadata_dst
xfrm: interface: support collect metadata mode
xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md
mode

 include/net/dst_metadata.h    |  31 +++++
 include/net/xfrm.h            |  11 +-
 include/uapi/linux/if_link.h  |   1 +
 include/uapi/linux/lwtunnel.h |  10 ++
 net/core/lwtunnel.c           |   1 +
 net/xfrm/xfrm_input.c         |   7 +-
 net/xfrm/xfrm_interface.c     | 206 ++++++++++++++++++++++++++++++----
 net/xfrm/xfrm_policy.c        |  10 +-
 8 files changed, 248 insertions(+), 29 deletions(-)

----

v3: coding changes as suggested by Nikolay Aleksandrov and
Nicolas Dichtel
v2: add "link" property as suggested by Nicolas Dichtel

Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 net: allow storing xfrm interface metadata in metadata_dst

XFRM interfaces provide the association of various XFRM transformations to a netdevice using an 'if_id' identifier common to both the XFRM data structures (polcies, states) and the interface. The if_id is configured by the controlling entity (usually the IKE daemon) and can be used by the administrator to define logical relations between different connections.

For example, different connections can share the if_id identifier so that they pass through the same interface, . However, currently it is not possible for connections using a different if_id to use the same interface while retaining the logical separation between them, without using additional criteria such as skb marks or different traffic selectors.

When having a large number of connections, it is useful to have a the logical separation offered by the if_id identifier but use a single network interface. Similar to the way collect_md mode is used in IP tunnels.

This patch attempts to enable different configuration mechanisms - such as ebpf programs, LWT encapsulations, and TC - to attach metadata to skbs which would carry the if_id. This way a single xfrm interface in collect_md mode can demux traffic based on this configuration on tx and provide this metadata on rx.

The XFRM metadata is somewhat similar to ip tunnel metadata in that it has an "id", and shares similar configuration entities (bpf, tc, ...), however, it does not necessarily represent an IP tunnel or use other ip tunnel information, and also has an optional "link" property which can be used for affecting underlying routing decisions.

Additional xfrm related criteria may also be added in the future.

Therefore, a new metadata type is introduced, to be used in subsequent patches in the xfrm interface and configuration entities.

----

v2: add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/net/dst_metadata.h +20 -0
index adab27ba1ecb..e4b059908cc7 100644
@@ -9,6 +9,7 @@
 enum metadata_type {
 	METADATA_IP_TUNNEL,
 	METADATA_HW_PORT_MUX,
+	METADATA_XFRM,
 };
 
 struct hw_port_info {
@@ -16,12 +17,18 @@ struct hw_port_info {
 	u32 port_id;
 };
 
+struct xfrm_md_info {
+	u32 if_id;
+	int link;
+};
+
 struct metadata_dst {
 	struct dst_entry		dst;
 	enum metadata_type		type;
 	union {
 		struct ip_tunnel_info	tun_info;
 		struct hw_port_info	port_info;
+		struct xfrm_md_info	xfrm_info;
 	} u;
 };
 
@@ -53,6 +60,16 @@ skb_tunnel_info(const struct sk_buff *skb)
 	return NULL;
 }
 
+static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
+{
+	struct metadata_dst *md_dst = skb_metadata_dst(skb);
+
+	if (md_dst && md_dst->type == METADATA_XFRM)
+		return &md_dst->u.xfrm_info;
+
+	return NULL;
+}
+
 static inline bool skb_valid_dst(const struct sk_buff *skb)
 {
 	struct dst_entry *dst = skb_dst(skb);
@@ -82,6 +99,9 @@ static inline int skb_metadata_dst_cmp(const struct sk_buff *skb_a,
 		return memcmp(&a->u.tun_info, &b->u.tun_info,
 			      sizeof(a->u.tun_info) +
 					 a->u.tun_info.options_len);
+	case METADATA_XFRM:
+		return memcmp(&a->u.xfrm_info, &b->u.xfrm_info,
+			      sizeof(a->u.xfrm_info));
 	default:
 		return 1;
 	}
Signature · 1 line
2.34.1
Reviewed-by on v3 1/3
#2 lore raw

Re: [PATCH ipsec-next,v3 1/3] net: allow storing xfrm interface metadata in metadata_dst

Quoted 35 lines · Eyal Birger

XFRM interfaces provide the association of various XFRM transformations to a netdevice using an 'if_id' identifier common to both the XFRM data structures (polcies, states) and the interface. The if_id is configured by the controlling entity (usually the IKE daemon) and can be used by the administrator to define logical relations between different connections.

For example, different connections can share the if_id identifier so that they pass through the same interface, . However, currently it is not possible for connections using a different if_id to use the same interface while retaining the logical separation between them, without using additional criteria such as skb marks or different traffic selectors.

When having a large number of connections, it is useful to have a the logical separation offered by the if_id identifier but use a single network interface. Similar to the way collect_md mode is used in IP tunnels.

This patch attempts to enable different configuration mechanisms - such as ebpf programs, LWT encapsulations, and TC - to attach metadata to skbs which would carry the if_id. This way a single xfrm interface in collect_md mode can demux traffic based on this configuration on tx and provide this metadata on rx.

The XFRM metadata is somewhat similar to ip tunnel metadata in that it has an "id", and shares similar configuration entities (bpf, tc, ...), however, it does not necessarily represent an IP tunnel or use other ip tunnel information, and also has an optional "link" property which can be used for affecting underlying routing decisions.

Additional xfrm related criteria may also be added in the future.

Therefore, a new metadata type is introduced, to be used in subsequent patches in the xfrm interface and configuration entities.

Quoted 9 lines

----

v2: add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
#3 lore raw

PATCH v3 2/3 xfrm: interface: support collect metadata mode

In reply to Eyal Birger

This commit adds support for 'collect_md' mode on xfrm interfaces.

Each net can have one collect_md device, created by providing the IFLA_XFRM_COLLECT_METADATA flag at creation. This device cannot be altered and has no if_id or link device attributes.

On transmit to this device, the if_id is fetched from the attached dst metadata on the skb. If exists, the link property is also fetched from the metadata. The dst metadata type used is METADATA_XFRM which holds these properties.

On the receive side, xfrmi_rcv_cb() populates a dst metadata for each packet received and attaches it to the skb. The if_id used in this case is fetched from the xfrm state, and the link is fetched from the incoming device. This information can later be used by upper layers such as tc, ebpf, and ip rules.

Because the skb is scrubed in xfrmi_rcv_cb(), the attachment of the dst metadata is postponed until after scrubing. Similarly, xfrm_input() is adapted to avoid dropping metadata dsts by only dropping 'valid' (skb_valid_dst(skb) == true) dsts.

Policy matching on packets arriving from collect_md xfrmi devices is done by using the xfrm state existing in the skb's sec_path. The xfrm_if_cb.decode_cb() interface implemented by xfrmi_decode_session() is changed to keep the details of the if_id extraction tucked away in xfrm_interface.c.

----

v3:
- coding improvements suggested by Nikolay Aleksandrov
- use RCU_INIT_POINTER() instead of rcu_assign_pointer() in xfrmi_dev_uninit() and rtnl_dereference() in xfrmi_exit_batch_net() as suggested by Nikolay Aleksandrov
- omit redundant check on link assignment from metadata as suggested by Nicolas Dichtel
- add missing extack message as suggested by Nicolas Dichtel

v2:
- add "link" property as suggested by Nicolas Dichtel
- rename xfrm_if_decode_session_params to xfrm_if_decode_session_result

---
 include/net/xfrm.h           |  11 +++-
 include/uapi/linux/if_link.h |   1 +
 net/xfrm/xfrm_input.c        |   7 +-
 net/xfrm/xfrm_interface.c    | 121 +++++++++++++++++++++++++++++------
 net/xfrm/xfrm_policy.c       |  10 +--
 5 files changed, 121 insertions(+), 29 deletions(-)
include/net/xfrm.h +9 -2
index 6e8fa98f786f..28b988577ed2 100644
@@ -312,9 +312,15 @@ struct km_event {
 	struct net *net;
 };
 
+struct xfrm_if_decode_session_result {
+	struct net *net;
+	u32 if_id;
+};
+
 struct xfrm_if_cb {
-	struct xfrm_if	*(*decode_session)(struct sk_buff *skb,
-					   unsigned short family);
+	bool (*decode_session)(struct sk_buff *skb,
+			       unsigned short family,
+			       struct xfrm_if_decode_session_result *res);
 };
 
 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb);
@@ -985,6 +991,7 @@ void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
 struct xfrm_if_parms {
 	int link;		/* ifindex of underlying L2 interface */
 	u32 if_id;		/* interface identifyer */
+	bool collect_md;
 };
 
 struct xfrm_if {
include/uapi/linux/if_link.h +1 -0
index e36d9d2c65a7..d96f13a42589 100644
@@ -694,6 +694,7 @@ enum {
 	IFLA_XFRM_UNSPEC,
 	IFLA_XFRM_LINK,
 	IFLA_XFRM_IF_ID,
+	IFLA_XFRM_COLLECT_METADATA,
 	__IFLA_XFRM_MAX
 };
 
net/xfrm/xfrm_input.c +5 -2
index 144238a50f3d..25e822fb5771 100644
@@ -20,6 +20,7 @@
 #include <net/xfrm.h>
 #include <net/ip_tunnels.h>
 #include <net/ip6_tunnel.h>
+#include <net/dst_metadata.h>
 
 #include "xfrm_inout.h"
 
@@ -720,7 +721,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 		sp = skb_sec_path(skb);
 		if (sp)
 			sp->olen = 0;
-		skb_dst_drop(skb);
+		if (skb_valid_dst(skb))
+			skb_dst_drop(skb);
 		gro_cells_receive(&gro_cells, skb);
 		return 0;
 	} else {
@@ -738,7 +740,8 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 			sp = skb_sec_path(skb);
 			if (sp)
 				sp->olen = 0;
-			skb_dst_drop(skb);
+			if (skb_valid_dst(skb))
+				skb_dst_drop(skb);
 			gro_cells_receive(&gro_cells, skb);
 			return err;
 		}
net/xfrm/xfrm_interface.c +101 -20

249 lines not on this page · raw message

net/xfrm/xfrm_policy.c +5 -5
index f1a0bab920a5..70376f3fe84a 100644
@@ -3516,17 +3516,17 @@ int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
 	int xerr_idx = -1;
 	const struct xfrm_if_cb *ifcb;
 	struct sec_path *sp;
-	struct xfrm_if *xi;
 	u32 if_id = 0;
 
 	rcu_read_lock();
 	ifcb = xfrm_if_get_cb();
 
 	if (ifcb) {
-		xi = ifcb->decode_session(skb, family);
-		if (xi) {
-			if_id = xi->p.if_id;
-			net = xi->net;
+		struct xfrm_if_decode_session_result r;
+
+		if (ifcb->decode_session(skb, family, &r)) {
+			if_id = r.if_id;
+			net = r.net;
 		}
 	}
 	rcu_read_unlock();
Signature · 1 line
2.34.1
Reviewed-by on v3 2/3
#4 lore raw

Re: [PATCH ipsec-next,v3 2/3] xfrm: interface: support collect metadata mode

Le 25/08/2022 à 17:46, Eyal Birger a écrit :

Quoted 22 lines

This commit adds support for 'collect_md' mode on xfrm interfaces.

Each net can have one collect_md device, created by providing the IFLA_XFRM_COLLECT_METADATA flag at creation. This device cannot be altered and has no if_id or link device attributes.

On transmit to this device, the if_id is fetched from the attached dst metadata on the skb. If exists, the link property is also fetched from the metadata. The dst metadata type used is METADATA_XFRM which holds these properties.

On the receive side, xfrmi_rcv_cb() populates a dst metadata for each packet received and attaches it to the skb. The if_id used in this case is fetched from the xfrm state, and the link is fetched from the incoming device. This information can later be used by upper layers such as tc, ebpf, and ip rules.

Because the skb is scrubed in xfrmi_rcv_cb(), the attachment of the dst metadata is postponed until after scrubing. Similarly, xfrm_input() is adapted to avoid dropping metadata dsts by only dropping 'valid' (skb_valid_dst(skb) == true) dsts.

Quoted 8 lines

Policy matching on packets arriving from collect_md xfrmi devices is done by using the xfrm state existing in the skb's sec_path. The xfrm_if_cb.decode_cb() interface implemented by xfrmi_decode_session() is changed to keep the details of the if_id extraction tucked away in xfrm_interface.c.

Reviewed-by on v3 2/3
#5 lore raw

Re: [PATCH ipsec-next,v3 2/3] xfrm: interface: support collect metadata mode

In reply to Eyal Birger
Quoted 44 lines · Eyal Birger

This commit adds support for 'collect_md' mode on xfrm interfaces.

Each net can have one collect_md device, created by providing the IFLA_XFRM_COLLECT_METADATA flag at creation. This device cannot be altered and has no if_id or link device attributes.

On transmit to this device, the if_id is fetched from the attached dst metadata on the skb. If exists, the link property is also fetched from the metadata. The dst metadata type used is METADATA_XFRM which holds these properties.

On the receive side, xfrmi_rcv_cb() populates a dst metadata for each packet received and attaches it to the skb. The if_id used in this case is fetched from the xfrm state, and the link is fetched from the incoming device. This information can later be used by upper layers such as tc, ebpf, and ip rules.

Because the skb is scrubed in xfrmi_rcv_cb(), the attachment of the dst metadata is postponed until after scrubing. Similarly, xfrm_input() is adapted to avoid dropping metadata dsts by only dropping 'valid' (skb_valid_dst(skb) == true) dsts.

Policy matching on packets arriving from collect_md xfrmi devices is done by using the xfrm state existing in the skb's sec_path. The xfrm_if_cb.decode_cb() interface implemented by xfrmi_decode_session() is changed to keep the details of the if_id extraction tucked away in xfrm_interface.c.

----

v3:
- coding improvements suggested by Nikolay Aleksandrov
- use RCU_INIT_POINTER() instead of rcu_assign_pointer() in xfrmi_dev_uninit() and rtnl_dereference() in xfrmi_exit_batch_net() as suggested by Nikolay Aleksandrov
- omit redundant check on link assignment from metadata as suggested by Nicolas Dichtel
- add missing extack message as suggested by Nicolas Dichtel

v2:
- add "link" property as suggested by Nicolas Dichtel
- rename xfrm_if_decode_session_params to xfrm_if_decode_session_result

Quoted 8 lines
---
 include/net/xfrm.h           |  11 +++-
 include/uapi/linux/if_link.h |   1 +
 net/xfrm/xfrm_input.c        |   7 +-
 net/xfrm/xfrm_interface.c    | 121 +++++++++++++++++++++++++++++------
 net/xfrm/xfrm_policy.c       |  10 +--
 5 files changed, 121 insertions(+), 29 deletions(-)
#6 lore raw

PATCH v3 3/3 xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md mode

In reply to Eyal Birger

Allow specifying the xfrm interface if_id and link as part of a route metadata using the lwtunnel infrastructure.

This allows for example using a single xfrm interface in collect_md mode as the target of multiple routes each specifying a different if_id.

With the appropriate changes to iproute2, considering an xfrm device ipsec1 in collect_md mode one can for example add a route specifying an if_id like so:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1

In which case traffic routed to the device via this route would use if_id in the xfrm interface policy lookup.

Or in the context of vrf, one can also specify the "link" property:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1 link_dev eth15

----

v3: netlink improvements as suggested by Nikolay Aleksandrov and
Nicolas Dichtel

v2:
- move lwt_xfrm_info() helper to dst_metadata.h
- add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h    | 11 +++++
 include/uapi/linux/lwtunnel.h | 10 +++++
 net/core/lwtunnel.c           |  1 +
 net/xfrm/xfrm_interface.c     | 85 +++++++++++++++++++++++++++++++++++
 4 files changed, 107 insertions(+)
include/net/dst_metadata.h +11 -0
index e4b059908cc7..57f75960fa28 100644
@@ -60,13 +60,24 @@ skb_tunnel_info(const struct sk_buff *skb)
 	return NULL;
 }
 
+static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
+{
+	return (struct xfrm_md_info *)lwt->data;
+}
+
 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
 {
 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
+	struct dst_entry *dst;
 
 	if (md_dst && md_dst->type == METADATA_XFRM)
 		return &md_dst->u.xfrm_info;
 
+	dst = skb_dst(skb);
+	if (dst && dst->lwtstate &&
+	    dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
+		return lwt_xfrm_info(dst->lwtstate);
+
 	return NULL;
 }
 
include/uapi/linux/lwtunnel.h +10 -0
index 2e206919125c..229655ef792f 100644
@@ -15,6 +15,7 @@ enum lwtunnel_encap_types {
 	LWTUNNEL_ENCAP_SEG6_LOCAL,
 	LWTUNNEL_ENCAP_RPL,
 	LWTUNNEL_ENCAP_IOAM6,
+	LWTUNNEL_ENCAP_XFRM,
 	__LWTUNNEL_ENCAP_MAX,
 };
 
@@ -111,4 +112,13 @@ enum {
 
 #define LWT_BPF_MAX_HEADROOM 256
 
+enum {
+	LWT_XFRM_UNSPEC,
+	LWT_XFRM_IF_ID,
+	LWT_XFRM_LINK,
+	__LWT_XFRM_MAX,
+};
+
+#define LWT_XFRM_MAX (__LWT_XFRM_MAX - 1)
+
 #endif /* _UAPI_LWTUNNEL_H_ */
net/core/lwtunnel.c +1 -0
index 9ccd64e8a666..6fac2f0ef074 100644
@@ -50,6 +50,7 @@ static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
 		return "IOAM6";
 	case LWTUNNEL_ENCAP_IP6:
 	case LWTUNNEL_ENCAP_IP:
+	case LWTUNNEL_ENCAP_XFRM:
 	case LWTUNNEL_ENCAP_NONE:
 	case __LWTUNNEL_ENCAP_MAX:
 		/* should not have got here */
net/xfrm/xfrm_interface.c +85 -0

103 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH ipsec-next,v3 3/3] xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md mode

Le 25/08/2022 à 17:46, Eyal Birger a écrit :

Quoted 108 lines

Allow specifying the xfrm interface if_id and link as part of a route metadata using the lwtunnel infrastructure.

This allows for example using a single xfrm interface in collect_md mode as the target of multiple routes each specifying a different if_id.

With the appropriate changes to iproute2, considering an xfrm device ipsec1 in collect_md mode one can for example add a route specifying an if_id like so:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1

In which case traffic routed to the device via this route would use if_id in the xfrm interface policy lookup.

Or in the context of vrf, one can also specify the "link" property:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1 link_dev eth15

----

v3: netlink improvements as suggested by Nikolay Aleksandrov and
Nicolas Dichtel

v2:
- move lwt_xfrm_info() helper to dst_metadata.h
- add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h    | 11 +++++
 include/uapi/linux/lwtunnel.h | 10 +++++
 net/core/lwtunnel.c           |  1 +
 net/xfrm/xfrm_interface.c     | 85 +++++++++++++++++++++++++++++++++++
 4 files changed, 107 insertions(+)
include/net/dst_metadata.h +11 -0
index e4b059908cc7..57f75960fa28 100644
@@ -60,13 +60,24 @@ skb_tunnel_info(const struct sk_buff *skb)
 	return NULL;
 }
 
+static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
+{
+	return (struct xfrm_md_info *)lwt->data;
+}
+
 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
 {
 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
+	struct dst_entry *dst;
 
 	if (md_dst && md_dst->type == METADATA_XFRM)
 		return &md_dst->u.xfrm_info;
 
+	dst = skb_dst(skb);
+	if (dst && dst->lwtstate &&
+	    dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
+		return lwt_xfrm_info(dst->lwtstate);
+
 	return NULL;
 }
 
include/uapi/linux/lwtunnel.h +10 -0
index 2e206919125c..229655ef792f 100644
@@ -15,6 +15,7 @@ enum lwtunnel_encap_types {
 	LWTUNNEL_ENCAP_SEG6_LOCAL,
 	LWTUNNEL_ENCAP_RPL,
 	LWTUNNEL_ENCAP_IOAM6,
+	LWTUNNEL_ENCAP_XFRM,
 	__LWTUNNEL_ENCAP_MAX,
 };
 
@@ -111,4 +112,13 @@ enum {
 
 #define LWT_BPF_MAX_HEADROOM 256
 
+enum {
+	LWT_XFRM_UNSPEC,
+	LWT_XFRM_IF_ID,
+	LWT_XFRM_LINK,
+	__LWT_XFRM_MAX,
+};
+
+#define LWT_XFRM_MAX (__LWT_XFRM_MAX - 1)
+
 #endif /* _UAPI_LWTUNNEL_H_ */
net/core/lwtunnel.c +1 -0
index 9ccd64e8a666..6fac2f0ef074 100644
@@ -50,6 +50,7 @@ static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
 		return "IOAM6";
 	case LWTUNNEL_ENCAP_IP6:
 	case LWTUNNEL_ENCAP_IP:
+	case LWTUNNEL_ENCAP_XFRM:
 	case LWTUNNEL_ENCAP_NONE:
 	case __LWTUNNEL_ENCAP_MAX:
 		/* should not have got here */
net/xfrm/xfrm_interface.c +0 -0
index e9a355047468..495dee8b0764 100644

net/xfrm/xfrm_interface.c struct xfrmi_net { · line 65

Quoted 6 lines
net/xfrm/xfrm_interface.c +3 -0
@@ -60,6 +60,88 @@ struct xfrmi_net {
 	struct xfrm_if __rcu *collect_md_xfrmi;
 };
 
+static const struct nla_policy xfrm_lwt_policy[LWT_XFRM_MAX + 1] = {
+	[LWT_XFRM_IF_ID]	= NLA_POLICY_MIN(NLA_U32, 1),
+	[LWT_XFRM_LINK]		= NLA_POLICY_MIN(NLA_S32, 1),

IMHO, it would be better to keep consistency with IFLA_XFRM_LINK.

$ git grep _LINK.*NLA_U32 net/ drivers/net/
drivers/net/gtp.c:      [GTPA_LINK]             = { .type = NLA_U32, },
drivers/net/vxlan/vxlan_core.c: [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
...
net/core/rtnetlink.c:   [IFLA_LINK]             = { .type = NLA_U32 },
...
net/ipv4/ip_gre.c:      [IFLA_GRE_LINK]         = { .type = NLA_U32 },
net/ipv4/ip_vti.c:      [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv4/ipip.c:        [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_gre.c:     [IFLA_GRE_LINK]        = { .type = NLA_U32 },
net/ipv6/ip6_tunnel.c:  [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_vti.c:     [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv6/sit.c: [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/sched/cls_u32.c:    [TCA_U32_LINK]          = { .type = NLA_U32 },
...
net/xfrm/xfrm_interface.c:      [IFLA_XFRM_LINK]        = { .type = NLA_U32 },
$ git grep _LINK.*NLA_S32 net/ drivers/net/
net/core/rtnetlink.c:   [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
$

They all are U32. Adding one S32 would just add confusion.

#8 lore raw

Re: [PATCH ipsec-next,v3 3/3] xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md mode

Quoted 139 lines · Nicolas Dichtel
Quoted 108 lines · Eyal Birger

Allow specifying the xfrm interface if_id and link as part of a route metadata using the lwtunnel infrastructure.

This allows for example using a single xfrm interface in collect_md mode as the target of multiple routes each specifying a different if_id.

With the appropriate changes to iproute2, considering an xfrm device ipsec1 in collect_md mode one can for example add a route specifying an if_id like so:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1

In which case traffic routed to the device via this route would use if_id in the xfrm interface policy lookup.

Or in the context of vrf, one can also specify the "link" property:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1 link_dev eth15

----

v3: netlink improvements as suggested by Nikolay Aleksandrov and
Nicolas Dichtel

v2:
- move lwt_xfrm_info() helper to dst_metadata.h
- add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h    | 11 +++++
 include/uapi/linux/lwtunnel.h | 10 +++++
 net/core/lwtunnel.c           |  1 +
 net/xfrm/xfrm_interface.c     | 85 +++++++++++++++++++++++++++++++++++
 4 files changed, 107 insertions(+)
include/net/dst_metadata.h +11 -0
index e4b059908cc7..57f75960fa28 100644
@@ -60,13 +60,24 @@ skb_tunnel_info(const struct sk_buff *skb)
      return NULL;
 }
 
+static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
+{
+     return (struct xfrm_md_info *)lwt->data;
+}
+
 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
 {
      struct metadata_dst *md_dst = skb_metadata_dst(skb);
+     struct dst_entry *dst;
 
      if (md_dst && md_dst->type == METADATA_XFRM)
              return &md_dst->u.xfrm_info;
 
+     dst = skb_dst(skb);
+     if (dst && dst->lwtstate &&
+         dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
+             return lwt_xfrm_info(dst->lwtstate);
+
      return NULL;
 }
 
include/uapi/linux/lwtunnel.h +10 -0
index 2e206919125c..229655ef792f 100644
@@ -15,6 +15,7 @@ enum lwtunnel_encap_types {
      LWTUNNEL_ENCAP_SEG6_LOCAL,
      LWTUNNEL_ENCAP_RPL,
      LWTUNNEL_ENCAP_IOAM6,
+     LWTUNNEL_ENCAP_XFRM,
      __LWTUNNEL_ENCAP_MAX,
 };
 
@@ -111,4 +112,13 @@ enum {
 
 #define LWT_BPF_MAX_HEADROOM 256
 
+enum {
+     LWT_XFRM_UNSPEC,
+     LWT_XFRM_IF_ID,
+     LWT_XFRM_LINK,
+     __LWT_XFRM_MAX,
+};
+
+#define LWT_XFRM_MAX (__LWT_XFRM_MAX - 1)
+
 #endif /* _UAPI_LWTUNNEL_H_ */
net/core/lwtunnel.c +1 -0
index 9ccd64e8a666..6fac2f0ef074 100644
@@ -50,6 +50,7 @@ static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
              return "IOAM6";
      case LWTUNNEL_ENCAP_IP6:
      case LWTUNNEL_ENCAP_IP:
+     case LWTUNNEL_ENCAP_XFRM:
      case LWTUNNEL_ENCAP_NONE:
      case __LWTUNNEL_ENCAP_MAX:
              /* should not have got here */
net/xfrm/xfrm_interface.c +0 -0
index e9a355047468..495dee8b0764 100644

net/xfrm/xfrm_interface.c struct xfrmi_net { · line 65

Quoted 6 lines
net/xfrm/xfrm_interface.c +3 -0
@@ -60,6 +60,88 @@ struct xfrmi_net {
      struct xfrm_if __rcu *collect_md_xfrmi;
 };
 
+static const struct nla_policy xfrm_lwt_policy[LWT_XFRM_MAX + 1] = {
+     [LWT_XFRM_IF_ID]        = NLA_POLICY_MIN(NLA_U32, 1),
+     [LWT_XFRM_LINK]         = NLA_POLICY_MIN(NLA_S32, 1),

IMHO, it would be better to keep consistency with IFLA_XFRM_LINK.

$ git grep _LINK.*NLA_U32 net/ drivers/net/
drivers/net/gtp.c:      [GTPA_LINK]             = { .type = NLA_U32, },
drivers/net/vxlan/vxlan_core.c: [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
...
net/core/rtnetlink.c:   [IFLA_LINK]             = { .type = NLA_U32 },
...
net/ipv4/ip_gre.c:      [IFLA_GRE_LINK]         = { .type = NLA_U32 },
net/ipv4/ip_vti.c:      [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv4/ipip.c:        [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_gre.c:     [IFLA_GRE_LINK]        = { .type = NLA_U32 },
net/ipv6/ip6_tunnel.c:  [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_vti.c:     [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv6/sit.c: [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/sched/cls_u32.c:    [TCA_U32_LINK]          = { .type = NLA_U32 },
...
net/xfrm/xfrm_interface.c:      [IFLA_XFRM_LINK]        = { .type = NLA_U32 },
$ git grep _LINK.*NLA_S32 net/ drivers/net/
net/core/rtnetlink.c:   [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
$
Quoted 1 line

They all are U32. Adding one S32 would just add confusion.

Thanks for this input!

Indeed going over the other references it seems ifindex is treated as U32 when interfacing with userspace almost everywhere including netlink and bpf. In the IOCTL interface it seems to be implemented as int, but at least on my Ubuntu machine the manpage for e.g. if_nametoindex() describes it as returning unsigned int.

Therefore I intend to resubmit this as U32.

Thanks,
Eyal.

#9 lore raw

Re: [PATCH ipsec-next,v3 3/3] xfrm: lwtunnel: add lwtunnel support for xfrm interfaces in collect_md mode

Quoted 151 lines · Eyal Birger
Quoted 139 lines · Nicolas Dichtel
Quoted 108 lines · Eyal Birger

Allow specifying the xfrm interface if_id and link as part of a route metadata using the lwtunnel infrastructure.

This allows for example using a single xfrm interface in collect_md mode as the target of multiple routes each specifying a different if_id.

With the appropriate changes to iproute2, considering an xfrm device ipsec1 in collect_md mode one can for example add a route specifying an if_id like so:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1

In which case traffic routed to the device via this route would use if_id in the xfrm interface policy lookup.

Or in the context of vrf, one can also specify the "link" property:

ip route add <SUBNET> dev ipsec1 encap xfrm if_id 1 link_dev eth15

----

v3: netlink improvements as suggested by Nikolay Aleksandrov and
Nicolas Dichtel

v2:
- move lwt_xfrm_info() helper to dst_metadata.h
- add "link" property as suggested by Nicolas Dichtel

---
 include/net/dst_metadata.h    | 11 +++++
 include/uapi/linux/lwtunnel.h | 10 +++++
 net/core/lwtunnel.c           |  1 +
 net/xfrm/xfrm_interface.c     | 85 +++++++++++++++++++++++++++++++++++
 4 files changed, 107 insertions(+)
include/net/dst_metadata.h +11 -0
index e4b059908cc7..57f75960fa28 100644
@@ -60,13 +60,24 @@ skb_tunnel_info(const struct sk_buff *skb)
      return NULL;
 }
 
+static inline struct xfrm_md_info *lwt_xfrm_info(struct lwtunnel_state *lwt)
+{
+     return (struct xfrm_md_info *)lwt->data;
+}
+
 static inline struct xfrm_md_info *skb_xfrm_md_info(const struct sk_buff *skb)
 {
      struct metadata_dst *md_dst = skb_metadata_dst(skb);
+     struct dst_entry *dst;
 
      if (md_dst && md_dst->type == METADATA_XFRM)
              return &md_dst->u.xfrm_info;
 
+     dst = skb_dst(skb);
+     if (dst && dst->lwtstate &&
+         dst->lwtstate->type == LWTUNNEL_ENCAP_XFRM)
+             return lwt_xfrm_info(dst->lwtstate);
+
      return NULL;
 }
 
include/uapi/linux/lwtunnel.h +10 -0
index 2e206919125c..229655ef792f 100644
@@ -15,6 +15,7 @@ enum lwtunnel_encap_types {
      LWTUNNEL_ENCAP_SEG6_LOCAL,
      LWTUNNEL_ENCAP_RPL,
      LWTUNNEL_ENCAP_IOAM6,
+     LWTUNNEL_ENCAP_XFRM,
      __LWTUNNEL_ENCAP_MAX,
 };
 
@@ -111,4 +112,13 @@ enum {
 
 #define LWT_BPF_MAX_HEADROOM 256
 
+enum {
+     LWT_XFRM_UNSPEC,
+     LWT_XFRM_IF_ID,
+     LWT_XFRM_LINK,
+     __LWT_XFRM_MAX,
+};
+
+#define LWT_XFRM_MAX (__LWT_XFRM_MAX - 1)
+
 #endif /* _UAPI_LWTUNNEL_H_ */
net/core/lwtunnel.c +1 -0
index 9ccd64e8a666..6fac2f0ef074 100644
@@ -50,6 +50,7 @@ static const char *lwtunnel_encap_str(enum lwtunnel_encap_types encap_type)
              return "IOAM6";
      case LWTUNNEL_ENCAP_IP6:
      case LWTUNNEL_ENCAP_IP:
+     case LWTUNNEL_ENCAP_XFRM:
      case LWTUNNEL_ENCAP_NONE:
      case __LWTUNNEL_ENCAP_MAX:
              /* should not have got here */
net/xfrm/xfrm_interface.c +0 -0
index e9a355047468..495dee8b0764 100644

net/xfrm/xfrm_interface.c struct xfrmi_net { · line 65

Quoted 6 lines
net/xfrm/xfrm_interface.c +3 -0
@@ -60,6 +60,88 @@ struct xfrmi_net {
      struct xfrm_if __rcu *collect_md_xfrmi;
 };
 
+static const struct nla_policy xfrm_lwt_policy[LWT_XFRM_MAX + 1] = {
+     [LWT_XFRM_IF_ID]        = NLA_POLICY_MIN(NLA_U32, 1),
+     [LWT_XFRM_LINK]         = NLA_POLICY_MIN(NLA_S32, 1),

IMHO, it would be better to keep consistency with IFLA_XFRM_LINK.

$ git grep _LINK.*NLA_U32 net/ drivers/net/
drivers/net/gtp.c:      [GTPA_LINK]             = { .type = NLA_U32, },
drivers/net/vxlan/vxlan_core.c: [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
...
net/core/rtnetlink.c:   [IFLA_LINK]             = { .type = NLA_U32 },
...
net/ipv4/ip_gre.c:      [IFLA_GRE_LINK]         = { .type = NLA_U32 },
net/ipv4/ip_vti.c:      [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv4/ipip.c:        [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_gre.c:     [IFLA_GRE_LINK]        = { .type = NLA_U32 },
net/ipv6/ip6_tunnel.c:  [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/ipv6/ip6_vti.c:     [IFLA_VTI_LINK]         = { .type = NLA_U32 },
net/ipv6/sit.c: [IFLA_IPTUN_LINK]               = { .type = NLA_U32 },
net/sched/cls_u32.c:    [TCA_U32_LINK]          = { .type = NLA_U32 },
...
net/xfrm/xfrm_interface.c:      [IFLA_XFRM_LINK]        = { .type = NLA_U32 },
$ git grep _LINK.*NLA_S32 net/ drivers/net/
net/core/rtnetlink.c:   [IFLA_LINK_NETNSID]     = { .type = NLA_S32 },
$
Quoted 1 line

They all are U32. Adding one S32 would just add confusion.

Thanks for this input!

Indeed going over the other references it seems ifindex is treated as U32 when interfacing with userspace almost everywhere including netlink and bpf. In the IOCTL interface it seems to be implemented as int, but at least on my Ubuntu machine the manpage for e.g. if_nametoindex() describes it as returning unsigned int.

Quoted 4 lines

Therefore I intend to resubmit this as U32.

Thanks,
Eyal.

Ack, good point, note that ifindex is not always a u32 and ifindex itself is usually a signed integer field in structs (e.g. net_device), as well as flowic_oif (flowi_oif). :)
rtnetlink.c: [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),
rtnetlink.c: [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1),

Just using the old U32 code and making link a u32 should be ok.

Cheers,
Nik