Skip to contentSkip to conversation

xfrm: interface: support collect metadata mode

PATCH In review Eyal Birger 2 participants 6 messages Nov 21, 2020 – Jan 5, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfrm: interface: support collect metadata mode

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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

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. This 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.

---
 include/net/xfrm.h           |  11 +++-
 include/uapi/linux/if_link.h |   1 +
 net/xfrm/xfrm_input.c        |   7 ++-
 net/xfrm/xfrm_interface.c    | 114 ++++++++++++++++++++++++++++++-----
 net/xfrm/xfrm_policy.c       |  10 +--
 5 files changed, 119 insertions(+), 24 deletions(-)
include/net/xfrm.h +9 -2
index b2a06f10b62c..925f8dcdd0db 100644
@@ -308,9 +308,15 @@ struct xfrm_replay {
 	int	(*overflow)(struct xfrm_state *x, struct sk_buff *skb);
 };
 
+struct xfrm_if_decode_session_params {
+	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_params *params);
 };
 
 void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb);
@@ -984,6 +990,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 c4b23f06f69e..ff04a06c2b69 100644
@@ -655,6 +655,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 be6351e3f3cd..c7de46d30697 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"
 
@@ -719,7 +720,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 {
@@ -737,7 +739,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 +99 -15

234 lines not on this page · raw message

net/xfrm/xfrm_policy.c +5 -5
index d622c2548d22..4da5ea277500 100644
@@ -3519,17 +3519,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_params p;
+
+		if (ifcb->decode_session(skb, family, &p)) {
+			if_id = p.if_id;
+			net = p.net;
 		}
 	}
 	rcu_read_unlock();
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 10 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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

Can we please have a separate metadata type for xfrm interfaces?

Sharing such structures turned already out to be a bad idea on vti interfaces, let's try to avoid that misstake with xfrm interfaces.

Quoted 10 lines

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. This 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 7 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.

The rest of the patch looks good.

Thanks!

#2 lore raw

Hi Steffen,

Quoted 13 lines · Steffen Klassert
Quoted 10 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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

Quoted 5 lines

Can we please have a separate metadata type for xfrm interfaces?

Sharing such structures turned already out to be a bad idea on vti interfaces, let's try to avoid that misstake with xfrm interfaces.

My initial thought was to do that, but it looks like most of the constructs surrounding this facility - tc, nft, ovs, ebpf, ip routing - are built around struct ip_tunnel_info and don't regard other possible metadata types.

For xfrm interfaces, the only metadata used is the if_id, which is stored in the metadata tun_id, so I think other than naming consideration, the use of struct ip_tunnel_info does not imply tunneling and does not limit the use of xfrmi to a specific mode of operation.

On the other hand, adding a new metadata type would require changing all other places to regard the new metadata type, with a large number of userspace visible changes.

Quoted 17 lines
Quoted 17 lines

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. This 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.

Quoted 3 lines
Quoted 1 line

The rest of the patch looks good.

Thanks for the review!
Eyal.

maintainer
#3 lore raw
Quoted 19 lines · Eyal Birger

Hi Steffen,

Quoted 15 lines · Steffen Klassert
Quoted 10 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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

Can we please have a separate metadata type for xfrm interfaces?

Quoted 7 lines
Quoted 3 lines

Sharing such structures turned already out to be a bad idea on vti interfaces, let's try to avoid that misstake with xfrm interfaces.

My initial thought was to do that, but it looks like most of the constructs surrounding this facility - tc, nft, ovs, ebpf, ip routing - are built around struct ip_tunnel_info and don't regard other possible metadata types.

That is likely because most objects that have a collect_md mode are tunnels. We have already a second metadata type, and I don't see why we can't have a third one. Maybe we can create something more generic so that it can have other users too.

Quoted 4 lines

For xfrm interfaces, the only metadata used is the if_id, which is stored in the metadata tun_id, so I think other than naming consideration, the use of struct ip_tunnel_info does not imply tunneling and does not limit the use of xfrmi to a specific mode of operation.

I agree that this can work, but it is a first step into a wrong direction. Using a __be64 field of a completely unrelated structure as an u32 if_id is bad style IMO.

Quoted 3 lines

On the other hand, adding a new metadata type would require changing all other places to regard the new metadata type, with a large number of userspace visible changes.

I admit that this might have some disadvantages too, but I'm not convinced that this justifies the 'ip_tunnel_info' hack.

#4 lore raw

Hi Steffen,

Quoted 43 lines · Steffen Klassert
Quoted 19 lines · Eyal Birger

Hi Steffen,

Quoted 15 lines · Steffen Klassert
Quoted 10 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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

Can we please have a separate metadata type for xfrm interfaces?

Quoted 7 lines
Quoted 3 lines

Sharing such structures turned already out to be a bad idea on vti interfaces, let's try to avoid that misstake with xfrm interfaces.

My initial thought was to do that, but it looks like most of the constructs surrounding this facility - tc, nft, ovs, ebpf, ip routing - are built around struct ip_tunnel_info and don't regard other possible metadata types.

That is likely because most objects that have a collect_md mode are tunnels. We have already a second metadata type, and I don't see why we can't have a third one. Maybe we can create something more generic so that it can have other users too.

Quoted 4 lines

For xfrm interfaces, the only metadata used is the if_id, which is stored in the metadata tun_id, so I think other than naming consideration, the use of struct ip_tunnel_info does not imply tunneling and does not limit the use of xfrmi to a specific mode of operation.

I agree that this can work, but it is a first step into a wrong direction. Using a __be64 field of a completely unrelated structure as an u32 if_id is bad style IMO.

Quoted 7 lines
Quoted 3 lines

On the other hand, adding a new metadata type would require changing all other places to regard the new metadata type, with a large number of userspace visible changes.

I admit that this might have some disadvantages too, but I'm not convinced that this justifies the 'ip_tunnel_info' hack.

I understand. I'll try to come up with something more generic. I hope I can find a way to still utilize the existing userspace constructs.

Thanks!
Eyal.

maintainer
#5 lore raw
Quoted 51 lines · Eyal Birger

Hi Steffen,

Quoted 47 lines · Steffen Klassert
Quoted 19 lines · Eyal Birger

Hi Steffen,

Quoted 15 lines · Steffen Klassert
Quoted 10 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. The dst metadata type used is METADATA_IP_TUNNEL since the only needed property is the if_id stored in the tun_id member of the ip_tunnel_info->key.

Can we please have a separate metadata type for xfrm interfaces?

Quoted 7 lines
Quoted 3 lines

Sharing such structures turned already out to be a bad idea on vti interfaces, let's try to avoid that misstake with xfrm interfaces.

My initial thought was to do that, but it looks like most of the constructs surrounding this facility - tc, nft, ovs, ebpf, ip routing - are built around struct ip_tunnel_info and don't regard other possible metadata types.

That is likely because most objects that have a collect_md mode are tunnels. We have already a second metadata type, and I don't see why we can't have a third one. Maybe we can create something more generic so that it can have other users too.

Quoted 4 lines

For xfrm interfaces, the only metadata used is the if_id, which is stored in the metadata tun_id, so I think other than naming consideration, the use of struct ip_tunnel_info does not imply tunneling and does not limit the use of xfrmi to a specific mode of operation.

I agree that this can work, but it is a first step into a wrong direction. Using a __be64 field of a completely unrelated structure as an u32 if_id is bad style IMO.

Quoted 3 lines

On the other hand, adding a new metadata type would require changing all other places to regard the new metadata type, with a large number of userspace visible changes.

Quoted 7 lines
Quoted 3 lines

I admit that this might have some disadvantages too, but I'm not convinced that this justifies the 'ip_tunnel_info' hack.

I understand. I'll try to come up with something more generic. I hope I can find a way to still utilize the existing userspace constructs.

Great, thanks!