Skip to contentSkip to conversation

openvswitch: enable NSH support

PATCH Superseded Yi Yang 4 participants 8 messages Aug 16, 2017 – Aug 17, 2017 1 message not in the archive

Subsystems (3)

v3 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH v3 1/1 openvswitch: enable NSH support

v2->v3
- Change OVS_KEY_ATTR_NSH to nested key to handle length-fixed attributes and length-variable attriubte more flexibly.
- Remove struct ovs_action_push_nsh completely
- Add code to handle nested attribute for SET_MASKED
- Change PUSH_NSH to use the nested OVS_KEY_ATTR_NSH to transfer NSH header data.
- Fix comments and coding style issues by Jiri and Eric

v1->v2
- Change encap_nsh and decap_nsh to push_nsh and pop_nsh
- Dynamically allocate struct ovs_action_push_nsh for length-variable metadata.

OVS master and 2.8 branch has merged NSH userspace patch series, this patch is to enable NSH support in kernel data path in order that OVS can support NSH in 2.8 release in compat mode by porting this.

---
 drivers/net/vxlan.c              |   7 +
 include/net/nsh.h                | 150 +++++++++++++++++++
 include/uapi/linux/openvswitch.h |  30 ++++
 net/openvswitch/actions.c        | 175 ++++++++++++++++++++++
 net/openvswitch/flow.c           |  39 +++++
 net/openvswitch/flow.h           |  11 ++
 net/openvswitch/flow_netlink.c   | 304 ++++++++++++++++++++++++++++++++++++++-
 net/openvswitch/flow_netlink.h   |   4 +
 8 files changed, 719 insertions(+), 1 deletion(-)
 create mode 100644 include/net/nsh.h
drivers/net/vxlan.c +7 -0
index dbca067..843714c 100644
@@ -27,6 +27,7 @@
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
 #include <net/vxlan.h>
+#include <net/nsh.h>
 
 #if IS_ENABLED(CONFIG_IPV6)
 #include <net/ip6_tunnel.h>
@@ -1267,6 +1268,9 @@ static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
 	case VXLAN_GPE_NP_IPV6:
 		*protocol = htons(ETH_P_IPV6);
 		break;
+	case VXLAN_GPE_NP_NSH:
+		*protocol = htons(ETH_P_NSH);
+		break;
 	case VXLAN_GPE_NP_ETHERNET:
 		*protocol = htons(ETH_P_TEB);
 		break;
@@ -1806,6 +1810,9 @@ static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
 	case htons(ETH_P_IPV6):
 		gpe->next_protocol = VXLAN_GPE_NP_IPV6;
 		return 0;
+	case htons(ETH_P_NSH):
+		gpe->next_protocol = VXLAN_GPE_NP_NSH;
+		return 0;
 	case htons(ETH_P_TEB):
 		gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
 		return 0;
include/net/nsh.h new file +150 -0

150 lines not on this page · raw message

include/uapi/linux/openvswitch.h +30 -0
index 156ee4c..596a043 100644
@@ -333,6 +333,7 @@ enum ovs_key_attr {
 	OVS_KEY_ATTR_CT_LABELS,	/* 16-octet connection tracking label */
 	OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,   /* struct ovs_key_ct_tuple_ipv4 */
 	OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,   /* struct ovs_key_ct_tuple_ipv6 */
+	OVS_KEY_ATTR_NSH,       /* Nested set of ovs_nsh_key_* */
 
 #ifdef __KERNEL__
 	OVS_KEY_ATTR_TUNNEL_INFO,  /* struct ip_tunnel_info */
@@ -491,6 +492,29 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
+enum ovs_nsh_key_attr {
+	OVS_NSH_KEY_ATTR_BASE,  /* struct ovs_nsh_key_base. */
+	OVS_NSH_KEY_ATTR_MD1,   /* struct ovs_nsh_key_md1. */
+	OVS_NSH_KEY_ATTR_MD2,   /* variable-length octets for MD type 2. */
+	__OVS_NSH_KEY_ATTR_MAX
+};
+
+#define OVS_NSH_KEY_ATTR_MAX (__OVS_NSH_KEY_ATTR_MAX - 1)
+
+struct ovs_nsh_key_base {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+};
+
+#define NSH_MD1_CONTEXT_SIZE 4
+
+struct ovs_nsh_key_md1 {
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 /**
  * enum ovs_flow_attr - attributes for %OVS_FLOW_* commands.
  * @OVS_FLOW_ATTR_KEY: Nested %OVS_KEY_ATTR_* attributes specifying the flow
@@ -769,6 +793,8 @@ struct ovs_action_push_eth {
 	struct ovs_key_ethernet addresses;
 };
 
+#define OVS_PUSH_NSH_MAX_MD_LEN 248
+
 /**
  * enum ovs_action_attr - Action types.
  *
@@ -806,6 +832,8 @@ struct ovs_action_push_eth {
  * packet.
  * @OVS_ACTION_ATTR_POP_ETH: Pop the outermost Ethernet header off the
  * packet.
+ * @OVS_ACTION_ATTR_PUSH_NSH: push NSH header to the packet.
+ * @OVS_ACTION_ATTR_POP_NSH: pop the outermost NSH header off the packet.
  *
  * Only a single header can be set with a single %OVS_ACTION_ATTR_SET.  Not all
  * fields within a header are modifiable, e.g. the IPv4 protocol and fragment
@@ -835,6 +863,8 @@ enum ovs_action_attr {
 	OVS_ACTION_ATTR_TRUNC,        /* u32 struct ovs_action_trunc. */
 	OVS_ACTION_ATTR_PUSH_ETH,     /* struct ovs_action_push_eth. */
 	OVS_ACTION_ATTR_POP_ETH,      /* No argument. */
+	OVS_ACTION_ATTR_PUSH_NSH,     /* Nested OVS_NSH_KEY_ATTR_*. */
+	OVS_ACTION_ATTR_POP_NSH,      /* No argument. */
 
 	__OVS_ACTION_ATTR_MAX,	      /* Nothing past this will be accepted
 				       * from userspace. */
net/openvswitch/actions.c +175 -0

205 lines not on this page · raw message

net/openvswitch/flow.c +39 -0
index 8c94cef..e2d6463 100644
@@ -46,6 +46,7 @@
 #include <net/ipv6.h>
 #include <net/mpls.h>
 #include <net/ndisc.h>
+#include <net/nsh.h>
 
 #include "conntrack.h"
 #include "datapath.h"
@@ -490,6 +491,40 @@ static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
 	return 0;
 }
 
+static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
+{
+	struct nsh_hdr *nsh = (struct nsh_hdr *)skb_network_header(skb);
+	u8 version, length;
+	u32 path_hdr;
+	int i;
+
+	memset(&key->nsh, 0, sizeof(struct ovs_key_nsh));
+	version = nsh_get_ver(nsh);
+	length = nsh_get_len(nsh);
+
+	key->nsh.flags = nsh_get_flags(nsh);
+	key->nsh.mdtype = nsh->md_type;
+	key->nsh.np = nsh->next_proto;
+	path_hdr = ntohl(nsh->path_hdr);
+	key->nsh.path_hdr = nsh->path_hdr;
+	switch (key->nsh.mdtype) {
+	case NSH_M_TYPE1:
+		if ((length << 2) != NSH_M_TYPE1_LEN)
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++)
+			key->nsh.context[i] = nsh->md1.context[i];
+
+		break;
+	case NSH_M_TYPE2:
+		/* Don't support MD type 2 yet */
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 /**
  * key_extract - extracts a flow key from an Ethernet frame.
  * @skb: sk_buff that contains the frame, with skb->data pointing to the
@@ -735,6 +770,10 @@ static int key_extract(struct sk_buff *skb, struct sw_flow_key *key)
 				memset(&key->tp, 0, sizeof(key->tp));
 			}
 		}
+	} else if (key->eth.type == htons(ETH_P_NSH)) {
+		error = parse_nsh(skb, key);
+		if (error)
+			return error;
 	}
 	return 0;
 }
net/openvswitch/flow.h +11 -0
index 1875bba..6c738d0 100644
@@ -35,6 +35,7 @@
 #include <net/inet_ecn.h>
 #include <net/ip_tunnels.h>
 #include <net/dst_metadata.h>
+#include <net/nsh.h>
 
 struct sk_buff;
 
@@ -66,6 +67,15 @@ struct vlan_head {
 	(offsetof(struct sw_flow_key, recirc_id) +	\
 	FIELD_SIZEOF(struct sw_flow_key, recirc_id))
 
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 struct sw_flow_key {
 	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
 	u8 tun_opts_len;
@@ -144,6 +154,7 @@ struct sw_flow_key {
 			};
 		} ipv6;
 	};
+	struct ovs_key_nsh nsh;         /* network service header */
 	struct {
 		/* Connection tracking fields not packed above. */
 		struct {
net/openvswitch/flow_netlink.c +303 -1

390 lines not on this page · raw message

net/openvswitch/flow_netlink.h +4 -0
index 929c665..f9c14be 100644
@@ -79,4 +79,8 @@ int ovs_nla_put_actions(const struct nlattr *attr,
 void ovs_nla_free_flow_actions(struct sw_flow_actions *);
 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *);
 
+int nsh_key_from_nlattr(const struct nlattr *attr, struct ovs_key_nsh *nsh);
+int push_nsh_para_from_nlattr(const struct nlattr *attr,
+			      struct push_nsh_para *pnp);
+
 #endif /* flow_netlink.h */
Signature · 1 line
2.5.5
#1 lore raw

Please always CC reviewers of the previous versions, thanks.

Quoted 9 lines · Yi Yang

v2->v3
- Change OVS_KEY_ATTR_NSH to nested key to handle length-fixed attributes and length-variable attriubte more flexibly.
- Remove struct ovs_action_push_nsh completely
- Add code to handle nested attribute for SET_MASKED
- Change PUSH_NSH to use the nested OVS_KEY_ATTR_NSH to transfer NSH header data.
- Fix comments and coding style issues by Jiri and Eric

Thanks! I like this version. I think we're almost there. A few more comments below.

include/net/nsh.h line 78

Quoted 7 lines
include/net/nsh.h new file +7 -0
+struct push_nsh_para {
+	__be16 ver_flags_len;
+	u8 md_type;
+	u8 next_proto;
+	__be32 path_hdr;
+	u8 metadata[NSH_M_TYPE2_MAX_LEN-8];
+};

Please get rid of this struct. It's a copy of struct nsh_hdr with some space added to the bottom.

One of the options (though maybe not the best one, feel free to come up with something better) is to change struct nsh_md1_ctx to:

struct nsh_md1_ctx {
	__be32 context[];
};

and change struct push_nsh_para:

struct push_nsh_para {
	struct nsh_hdr hdr;
	u8 metadata[NSH_M_TYPE2_MAX_LEN-8];
};

Another option (a better one, though a bit more work) is to get rid of push_nsh_para completely and just pass a properly allocated nsh_hdr around. Introduce macros and/or functions to help with the allocation.

include/net/nsh.h line 132

Quoted 9 lines
include/net/nsh.h new file +9 -0
+static inline struct nsh_md1_ctx *nsh_md1_ctx(struct nsh_hdr *nsh)
+{
+	return &nsh->md1;
+}
+
+static inline struct nsh_md2_tlv *nsh_md2_ctx(struct nsh_hdr *nsh)
+{
+	return nsh->md2;
+}

These are unused, please remove them.

Quoted 2 lines
include/uapi/linux/openvswitch.h +0 -0

[...]

Quoted 1 line

+#define NSH_MD1_CONTEXT_SIZE 4

Please move this to nsh.h and use it there, too, instead of the open coded 4.

include/uapi/linux/openvswitch.h

Quoted 6 lines
include/uapi/linux/openvswitch.h +6 -0
+static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
+		    const struct push_nsh_para *pnp)
+{
+	struct nsh_hdr *nsh;
+	size_t length = ((ntohs(pnp->ver_flags_len) & NSH_LEN_MASK)
+			     >> NSH_LEN_SHIFT) << 2;

Once push_nsh_para is removed/changed, this can be changed to a call to nsh_hdr_len.

include/uapi/linux/openvswitch.h

Quoted 2 lines
include/uapi/linux/openvswitch.h +2 -0
+	flags = (ntohs(nsh->ver_flags_len) & NSH_FLAGS_MASK) >>
+		NSH_FLAGS_SHIFT;

nsh_get_flags

Quoted 5 lines
include/uapi/linux/openvswitch.h +5 -0
+	case OVS_KEY_ATTR_NSH: {
+		struct ovs_key_nsh nsh;
+		struct ovs_key_nsh nsh_mask;
+		size_t size = nla_len(a) / 2;
+		struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)

include/uapi/linux/openvswitch.h

Quoted 10 lines
include/uapi/linux/openvswitch.h +10 -0
+						    , sizeof(struct nlattr))];
+		struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)
+						    , sizeof(struct nlattr))];
+
+		attr->nla_type = nla_type(a);
+		mask->nla_type = attr->nla_type;
+		attr->nla_len = NLA_HDRLEN + size;
+		mask->nla_len = attr->nla_len;
+		memcpy(attr + 1, (char *)(a + 1), size);
+		memcpy(mask + 1, (char *)(a + 1) + size, size);

This is too hacky. Please find a better way to handle this.

One option is to create a struct with struct nlattr as the first member followed by a char buffer. Still not nice but at least it's clear what's the intent.

Quoted 5 lines
include/uapi/linux/openvswitch.h +5 -0
+static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
+{
+	struct nsh_hdr *nsh = (struct nsh_hdr *)skb_network_header(skb);
+	u8 version, length;
+	u32 path_hdr;

include/uapi/linux/openvswitch.h

Quoted 10 lines
include/uapi/linux/openvswitch.h +10 -0
+	int i;
+
+	memset(&key->nsh, 0, sizeof(struct ovs_key_nsh));
+	version = nsh_get_ver(nsh);
+	length = nsh_get_len(nsh);
+
+	key->nsh.flags = nsh_get_flags(nsh);
+	key->nsh.mdtype = nsh->md_type;
+	key->nsh.np = nsh->next_proto;
+	path_hdr = ntohl(nsh->path_hdr);

The path_hdr variable is unused.

include/uapi/linux/openvswitch.h

Quoted 4 lines
include/uapi/linux/openvswitch.h +4 -0
+	key->nsh.path_hdr = nsh->path_hdr;
+	switch (key->nsh.mdtype) {
+	case NSH_M_TYPE1:
+		if ((length << 2) != NSH_M_TYPE1_LEN)

Why length << 2?

include/uapi/linux/openvswitch.h

Quoted 3 lines
include/uapi/linux/openvswitch.h +3 -0
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++)

NSH_MD1_CONTEXT_SIZE

include/uapi/linux/openvswitch.h

Quoted 3 lines
include/uapi/linux/openvswitch.h +3 -0
+			key->nsh.context[i] = nsh->md1.context[i];
+
+		break;

Will go through the rest later. Feel free to send a new version meanwhile.

Thanks,

Jiri

author
#2 lore raw
Quoted 1 line · Jiri Benc

Please always CC reviewers of the previous versions, thanks.

Jiri, thank you for quick review. Sorry, I made a mistake on sending and missed all the CCs, will indeed do this in next version.

Quoted 19 lines

include/net/nsh.h line 78

Quoted 6 lines
include/net/nsh.h new file +6 -0
+	__be16 ver_flags_len;
+	u8 md_type;
+	u8 next_proto;
+	__be32 path_hdr;
+	u8 metadata[NSH_M_TYPE2_MAX_LEN-8];
+};

Please get rid of this struct. It's a copy of struct nsh_hdr with some space added to the bottom.

One of the options (though maybe not the best one, feel free to come up with something better) is to change struct nsh_md1_ctx to:

struct nsh_md1_ctx {
	__be32 context[];
};

and change struct push_nsh_para:

Quoted 9 lines
struct push_nsh_para {
	struct nsh_hdr hdr;
	u8 metadata[NSH_M_TYPE2_MAX_LEN-8];
};

Another option (a better one, though a bit more work) is to get rid of push_nsh_para completely and just pass a properly allocated nsh_hdr around. Introduce macros and/or functions to help with the allocation.

Yeah, good point, we can use dynamic allocation and struct nsh_hdr * to handle this.

Quoted 11 lines

include/net/nsh.h line 132

Quoted 9 lines
include/net/nsh.h new file +9 -0
+static inline struct nsh_md1_ctx *nsh_md1_ctx(struct nsh_hdr *nsh)
+{
+	return &nsh->md1;
+}
+
+static inline struct nsh_md2_tlv *nsh_md2_ctx(struct nsh_hdr *nsh)
+{
+	return nsh->md2;
+}

These are unused, please remove them.

Will remove them, userspace does use them.

Quoted 8 lines
Quoted 3 lines
include/uapi/linux/openvswitch.h +0 -0

[...]

Quoted 1 line

+#define NSH_MD1_CONTEXT_SIZE 4

Please move this to nsh.h and use it there, too, instead of the open coded 4.

ovs code is very ugly, it will convert array[4] in datapath/linux/compat/include/linux/openvswitch.h to other struct, I have to change context[4] to such format :-), we can use 4 here for Linux kernel.

Quoted 10 lines

include/uapi/linux/openvswitch.h

Quoted 7 lines
include/uapi/linux/openvswitch.h +6 -0
+static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
+		    const struct push_nsh_para *pnp)
+{
+	struct nsh_hdr *nsh;
+	size_t length = ((ntohs(pnp->ver_flags_len) & NSH_LEN_MASK)
+			     >> NSH_LEN_SHIFT) << 2;

Once push_nsh_para is removed/changed, this can be changed to a call to nsh_hdr_len.

Yes, will do that way.

Quoted 5 lines

include/uapi/linux/openvswitch.h

Quoted 3 lines
include/uapi/linux/openvswitch.h +2 -0
+	flags = (ntohs(nsh->ver_flags_len) & NSH_FLAGS_MASK) >>
+		NSH_FLAGS_SHIFT;

nsh_get_flags

Missed this one :-)

Quoted 19 lines
Quoted 6 lines
include/uapi/linux/openvswitch.h +5 -0
+	case OVS_KEY_ATTR_NSH: {
+		struct ovs_key_nsh nsh;
+		struct ovs_key_nsh nsh_mask;
+		size_t size = nla_len(a) / 2;
+		struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)

include/uapi/linux/openvswitch.h

Quoted 10 lines
include/uapi/linux/openvswitch.h +10 -0
+						    , sizeof(struct nlattr))];
+		struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)
+						    , sizeof(struct nlattr))];
+
+		attr->nla_type = nla_type(a);
+		mask->nla_type = attr->nla_type;
+		attr->nla_len = NLA_HDRLEN + size;
+		mask->nla_len = attr->nla_len;
+		memcpy(attr + 1, (char *)(a + 1), size);
+		memcpy(mask + 1, (char *)(a + 1) + size, size);

This is too hacky. Please find a better way to handle this.

Quoted 3 lines

One option is to create a struct with struct nlattr as the first member followed by a char buffer. Still not nice but at least it's clear what's the intent.

The issue is nested attributes only can use this way, nested attribute for SET_MASKED is very special, we have to handle it specially.

Quoted 10 lines
Quoted 10 lines
include/uapi/linux/openvswitch.h +9 -0
+static int parse_nsh(struct sk_buff *skb, struct sw_flow_key *key)
+{
+	struct nsh_hdr *nsh = (struct nsh_hdr *)skb_network_header(skb);
+	u8 version, length;
+	u32 path_hdr;
+	int i;
+
+	memset(&key->nsh, 0, sizeof(struct ovs_key_nsh));
+	version = nsh_get_ver(nsh);
Quoted 8 lines

include/uapi/linux/openvswitch.h

Quoted 6 lines
include/uapi/linux/openvswitch.h +6 -0
+	length = nsh_get_len(nsh);
+
+	key->nsh.flags = nsh_get_flags(nsh);
+	key->nsh.mdtype = nsh->md_type;
+	key->nsh.np = nsh->next_proto;
+	path_hdr = ntohl(nsh->path_hdr);

The path_hdr variable is unused.

Will remove it.

Quoted 7 lines

include/uapi/linux/openvswitch.h

Quoted 5 lines
include/uapi/linux/openvswitch.h +4 -0
+	key->nsh.path_hdr = nsh->path_hdr;
+	switch (key->nsh.mdtype) {
+	case NSH_M_TYPE1:
+		if ((length << 2) != NSH_M_TYPE1_LEN)

Why length << 2?

len in NSH header is number of 4 octets, so need to multiply 4.

Quoted 6 lines

include/uapi/linux/openvswitch.h

Quoted 4 lines
include/uapi/linux/openvswitch.h +3 -0
+			return -EINVAL;
+
+		for (i = 0; i < 4; i++)

NSH_MD1_CONTEXT_SIZE

Ok

Quoted 11 lines

include/uapi/linux/openvswitch.h

Quoted 4 lines
include/uapi/linux/openvswitch.h +3 -0
+			key->nsh.context[i] = nsh->md1.context[i];
+
+		break;

Will go through the rest later. Feel free to send a new version meanwhile.

Thanks,

Jiri

Thank you so much for your comments, will work out new version ASAP.

#3 lore raw
Quoted 6 lines · Yi
Quoted 5 lines · Jiri Benc
Quoted 2 lines
include/uapi/linux/openvswitch.h +0 -0

[...]

Quoted 1 line

+#define NSH_MD1_CONTEXT_SIZE 4

Quoted 7 lines
Quoted 2 lines

Please move this to nsh.h and use it there, too, instead of the open coded 4.

ovs code is very ugly, it will convert array[4] in datapath/linux/compat/include/linux/openvswitch.h to other struct, I have to change context[4] to such format :-), we can use 4 here for Linux kernel.

Oh, right, this is uAPI and nsh.h is kernel internal. My suggestion was nonsense, let's keep it as it was in your patch.

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
include/uapi/linux/openvswitch.h +5 -0
+	case OVS_KEY_ATTR_NSH: {
+		struct ovs_key_nsh nsh;
+		struct ovs_key_nsh nsh_mask;
+		size_t size = nla_len(a) / 2;
+		struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)

include/uapi/linux/openvswitch.h

Quoted 10 lines
include/uapi/linux/openvswitch.h +10 -0
+						    , sizeof(struct nlattr))];
+		struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)
+						    , sizeof(struct nlattr))];
+
+		attr->nla_type = nla_type(a);
+		mask->nla_type = attr->nla_type;
+		attr->nla_len = NLA_HDRLEN + size;
+		mask->nla_len = attr->nla_len;
+		memcpy(attr + 1, (char *)(a + 1), size);
+		memcpy(mask + 1, (char *)(a + 1) + size, size);

This is too hacky. Please find a better way to handle this.

Quoted 6 lines
Quoted 3 lines

One option is to create a struct with struct nlattr as the first member followed by a char buffer. Still not nice but at least it's clear what's the intent.

The issue is nested attributes only can use this way, nested attribute for SET_MASKED is very special, we have to handle it specially.

I'm not sure you understood what I meant. Let me explain in code:

	struct {
		struct nlattr attr;
		struct ovs_key_ipv6 data;
	} attr, mask;

	attr->attr.nla_type = nla_type(a);
	attr->attr.nl_len = NLA_HDRLEN + size;
	memcpy(&attr->data, a + 1, size);

It's much less hacky and doing the same thing.

I'm not sure we don't need verification of size not overflowing the available buffer. Is it checked beforehand elsewhere?

I also spotted one more bug: the 'mask' variable is not used anywhere. The second call of nsh_key_from_nlattr should use mask, not attr.

Quoted 8 lines
Quoted 6 lines

include/uapi/linux/openvswitch.h

Quoted 4 lines
include/uapi/linux/openvswitch.h +4 -0
+	key->nsh.path_hdr = nsh->path_hdr;
+	switch (key->nsh.mdtype) {
+	case NSH_M_TYPE1:
+		if ((length << 2) != NSH_M_TYPE1_LEN)

Why length << 2?

len in NSH header is number of 4 octets, so need to multiply 4.

Should nsh_get_len take care of that, then?

Thanks,

Jiri

author
#4 lore raw
Quoted 54 lines · Jiri Benc
Quoted 6 lines · Yi
Quoted 5 lines · Jiri Benc
Quoted 2 lines
include/uapi/linux/openvswitch.h +0 -0

[...]

Quoted 1 line

+#define NSH_MD1_CONTEXT_SIZE 4

Quoted 7 lines
Quoted 2 lines

Please move this to nsh.h and use it there, too, instead of the open coded 4.

ovs code is very ugly, it will convert array[4] in datapath/linux/compat/include/linux/openvswitch.h to other struct, I have to change context[4] to such format :-), we can use 4 here for Linux kernel.

Oh, right, this is uAPI and nsh.h is kernel internal. My suggestion was nonsense, let's keep it as it was in your patch.

Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
include/uapi/linux/openvswitch.h +5 -0
+	case OVS_KEY_ATTR_NSH: {
+		struct ovs_key_nsh nsh;
+		struct ovs_key_nsh nsh_mask;
+		size_t size = nla_len(a) / 2;
+		struct nlattr attr[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)

include/uapi/linux/openvswitch.h

Quoted 10 lines
include/uapi/linux/openvswitch.h +10 -0
+						    , sizeof(struct nlattr))];
+		struct nlattr mask[1 + DIV_ROUND_UP(sizeof(struct ovs_key_ipv6)
+						    , sizeof(struct nlattr))];
+
+		attr->nla_type = nla_type(a);
+		mask->nla_type = attr->nla_type;
+		attr->nla_len = NLA_HDRLEN + size;
+		mask->nla_len = attr->nla_len;
+		memcpy(attr + 1, (char *)(a + 1), size);
+		memcpy(mask + 1, (char *)(a + 1) + size, size);

This is too hacky. Please find a better way to handle this.

Quoted 6 lines
Quoted 3 lines

One option is to create a struct with struct nlattr as the first member followed by a char buffer. Still not nice but at least it's clear what's the intent.

The issue is nested attributes only can use this way, nested attribute for SET_MASKED is very special, we have to handle it specially.

I'm not sure you understood what I meant. Let me explain in code:

	struct {
		struct nlattr attr;
		struct ovs_key_ipv6 data;
	} attr, mask;

	attr->attr.nla_type = nla_type(a);
	attr->attr.nl_len = NLA_HDRLEN + size;
	memcpy(&attr->data, a + 1, size);
Quoted 8 lines

It's much less hacky and doing the same thing.

I'm not sure we don't need verification of size not overflowing the available buffer. Is it checked beforehand elsewhere?

I also spotted one more bug: the 'mask' variable is not used anywhere. The second call of nsh_key_from_nlattr should use mask, not attr.

I have verified it in my real sfc test environment, but it is indeed a bug, because mask and attr are same, so the result is still attr.

But here attr and mask shoud be of "struct nlattr attr", I'll polish it.

Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

include/uapi/linux/openvswitch.h

Quoted 4 lines
include/uapi/linux/openvswitch.h +4 -0
+	key->nsh.path_hdr = nsh->path_hdr;
+	switch (key->nsh.mdtype) {
+	case NSH_M_TYPE1:
+		if ((length << 2) != NSH_M_TYPE1_LEN)

Why length << 2?

len in NSH header is number of 4 octets, so need to multiply 4.

Should nsh_get_len take care of that, then?

There are two helpers for this, nsh_hdr_len gets actual length, nsh_get_len gets "len" field in nsh header.

Quoted 3 lines

Thanks,

Jiri

A message that is not in the archive (sent off-list or not yet received): 1502861730-76203-1-git-send-email-yi.y.yang-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#6 lore raw
Quoted 9 lines · Yi Yang

v2->v3
- Change OVS_KEY_ATTR_NSH to nested key to handle length-fixed attributes and length-variable attriubte more flexibly.
- Remove struct ovs_action_push_nsh completely
- Add code to handle nested attribute for SET_MASKED
- Change PUSH_NSH to use the nested OVS_KEY_ATTR_NSH to transfer NSH header data.
- Fix comments and coding style issues by Jiri and Eric

Thanks for the updates Yi. Some feedback below.

Quoted 24 lines

v1->v2
- Change encap_nsh and decap_nsh to push_nsh and pop_nsh
- Dynamically allocate struct ovs_action_push_nsh for length-variable metadata.

OVS master and 2.8 branch has merged NSH userspace patch series, this patch is to enable NSH support in kernel data path in order that OVS can support NSH in 2.8 release in compat mode by porting this.

---
 drivers/net/vxlan.c              |   7 +
 include/net/nsh.h                | 150 +++++++++++++++++++
 include/uapi/linux/openvswitch.h |  30 ++++
 net/openvswitch/actions.c        | 175 ++++++++++++++++++++++
 net/openvswitch/flow.c           |  39 +++++
 net/openvswitch/flow.h           |  11 ++
 net/openvswitch/flow_netlink.c   | 304 ++++++++++++++++++++++++++++++++++++++-
 net/openvswitch/flow_netlink.h   |   4 +
 8 files changed, 719 insertions(+), 1 deletion(-)
 create mode 100644 include/net/nsh.h

[..]

Quoted 6 lines
include/net/nsh.h new file +0 -0
new file mode 100644
index 0000000..54f44f6
@@ -0,0 +1,150 @@

[..]

Quoted 4 lines
include/net/nsh.h new file +4 -0
+#define NSH_VER_MASK       0xc000
+#define NSH_VER_SHIFT      14
+#define NSH_FLAGS_MASK     0x3fc0
+#define NSH_FLAGS_SHIFT    6

include/net/nsh.h line 94

Quoted 10 lines
include/net/nsh.h new file +10 -0
+#define NSH_LEN_MASK       0x003f
+#define NSH_LEN_SHIFT      0
+
+#define NSH_SPI_MASK       0xffffff00
+#define NSH_SPI_SHIFT      8
+#define NSH_SI_MASK        0x000000ff
+#define NSH_SI_SHIFT       0
+
+#define NSH_DST_PORT    4790     /* UDP Port for NSH on VXLAN. */
+#define ETH_P_NSH       0x894F   /* Ethertype for NSH. */

ETH_P_NSH probably belongs in include/uapi/linux/if_ether.h with all the other ETH_P_* defines.

Quoted 23 lines
include/net/nsh.h new file +23 -0
+
+/* NSH Base Header Next Protocol. */
+#define NSH_P_IPV4        0x01
+#define NSH_P_IPV6        0x02
+#define NSH_P_ETHERNET    0x03
+#define NSH_P_NSH         0x04
+#define NSH_P_MPLS        0x05
+
+/* MD Type Registry. */
+#define NSH_M_TYPE1     0x01
+#define NSH_M_TYPE2     0x02
+#define NSH_M_EXP1      0xFE
+#define NSH_M_EXP2      0xFF
+
+/* NSH Metadata Length. */
+#define NSH_M_TYPE1_MDLEN 16
+
+/* NSH Base Header Length */
+#define NSH_BASE_HDR_LEN  8
+
+/* NSH MD Type 1 header Length. */
+#define NSH_M_TYPE1_LEN   24
+

[..]

Quoted 29 lines
net/openvswitch/flow.h +10 -0
index 1875bba..6c738d0 100644
@@ -35,6 +35,7 @@
 #include <net/inet_ecn.h>
 #include <net/ip_tunnels.h>
 #include <net/dst_metadata.h>
+#include <net/nsh.h>
 
 struct sk_buff;
 
@@ -66,6 +67,15 @@ struct vlan_head {
 	(offsetof(struct sw_flow_key, recirc_id) +	\
 	FIELD_SIZEOF(struct sw_flow_key, recirc_id))
 
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 struct sw_flow_key {
 	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
 	u8 tun_opts_len;

net/openvswitch/flow.h struct sw_flow_key { · line 157

Quoted 4 lines
net/openvswitch/flow.h +1 -0
@@ -144,6 +154,7 @@ struct sw_flow_key {
 			};
 		} ipv6;
 	};
+	struct ovs_key_nsh nsh;         /* network service header */

Are you intentionally not reserving space in the flow key for OVS_NSH_KEY_ATTR_MD2? I know it's not supported yet, but much of the code is stubbed out for it - just making sure this wasn't an oversight.

Quoted 21 lines
net/openvswitch/flow.h +0 -0
 	struct {
 		/* Connection tracking fields not packed above. */
 		struct {
net/openvswitch/flow_netlink.c +2 -0
index f07d10a..79059db 100644
@@ -78,9 +78,11 @@ static bool actions_may_change_flow(const struct nlattr *actions)
 		case OVS_ACTION_ATTR_HASH:
 		case OVS_ACTION_ATTR_POP_ETH:
 		case OVS_ACTION_ATTR_POP_MPLS:
+		case OVS_ACTION_ATTR_POP_NSH:
 		case OVS_ACTION_ATTR_POP_VLAN:
 		case OVS_ACTION_ATTR_PUSH_ETH:
 		case OVS_ACTION_ATTR_PUSH_MPLS:
+		case OVS_ACTION_ATTR_PUSH_NSH:
 		case OVS_ACTION_ATTR_PUSH_VLAN:
 		case OVS_ACTION_ATTR_SAMPLE:
 		case OVS_ACTION_ATTR_SET:
@@ -322,12 +324,22 @@ size_t ovs_tun_key_attr_size(void)
 		+ nla_total_size(2);   /* OVS_TUNNEL_KEY_ATTR_TP_DST */

net/openvswitch/flow_netlink.c size_t ovs_tun_key_attr_size(void) · line 334

Quoted 10 lines
net/openvswitch/flow_netlink.c +8 -0
 }
 
+size_t ovs_nsh_key_attr_size(void)
+{
+	/* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
+	 * updating this function.
+	 */
+	return  nla_total_size(8)      /* OVS_NSH_KEY_ATTR_BASE */
+		+ nla_total_size(16)   /* OVS_NSH_KEY_ATTR_MD1 */
+		+ nla_total_size(248); /* OVS_NSH_KEY_ATTR_MD2 */

_MD1 and _MD2 are mutually exclusive. You only need the larger of the two.

Maybe use OVS_PUSH_NSH_MAX_MD_LEN instead of 248.

Quoted 157 lines
net/openvswitch/flow_netlink.c +123 -1
+}
+
 size_t ovs_key_attr_size(void)
 {
 	/* Whenever adding new OVS_KEY_ FIELDS, we should consider
 	 * updating this function.
 	 */
-	BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
+	BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
 
 	return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
 		+ nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
@@ -341,6 +353,8 @@ size_t ovs_key_attr_size(void)
 		+ nla_total_size(4)   /* OVS_KEY_ATTR_CT_MARK */
 		+ nla_total_size(16)  /* OVS_KEY_ATTR_CT_LABELS */
 		+ nla_total_size(40)  /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
+		+ nla_total_size(0)   /* OVS_KEY_ATTR_NSH */
+		  + ovs_nsh_key_attr_size()
 		+ nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
 		+ nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
 		+ nla_total_size(4)   /* OVS_KEY_ATTR_VLAN */
@@ -373,6 +387,13 @@ static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1]
 	[OVS_TUNNEL_KEY_ATTR_IPV6_DST]      = { .len = sizeof(struct in6_addr) },
 };
 
+static const struct ovs_len_tbl
+ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
+	[OVS_NSH_KEY_ATTR_BASE]     = { .len = 8 },
+	[OVS_NSH_KEY_ATTR_MD1]      = { .len = 16 },
+	[OVS_NSH_KEY_ATTR_MD2]      = { .len = OVS_ATTR_VARIABLE },
+};
+
 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
 	[OVS_KEY_ATTR_ENCAP]	 = { .len = OVS_ATTR_NESTED },
@@ -405,6 +426,8 @@ static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
 		.len = sizeof(struct ovs_key_ct_tuple_ipv4) },
 	[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
 		.len = sizeof(struct ovs_key_ct_tuple_ipv6) },
+	[OVS_KEY_ATTR_NSH]       = { .len = OVS_ATTR_NESTED,
+				     .next = ovs_nsh_key_attr_lens, },
 };
 
 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
@@ -1179,6 +1202,209 @@ static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
 	return 0;
 }
 
+int push_nsh_para_from_nlattr(const struct nlattr *attr,
+			      struct push_nsh_para *pnp)
+{
+	struct nlattr *a;
+	int rem;
+	u8 flags = 0;
+	size_t mdlen = 0;
+
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+
+		if (type > OVS_NSH_KEY_ATTR_MAX) {
+			OVS_NLERR(1, "nsh attr %d is out of range max %d",
+				  type, OVS_NSH_KEY_ATTR_MAX);
+			return -EINVAL;
+		}
+
+		if (!check_attr_len(nla_len(a),
+				    ovs_nsh_key_attr_lens[type].len)) {
+			OVS_NLERR(
+			    1,
+			    "nsh attr %d has unexpected len %d expected %d",
+			    type,
+			    nla_len(a),
+			    ovs_nsh_key_attr_lens[type].len
+			);
+			return -EINVAL;
+		}
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base =
+				(struct ovs_nsh_key_base *)nla_data(a);
+			flags = base->flags;
+			pnp->next_proto = base->np;
+			pnp->md_type = base->mdtype;
+			pnp->path_hdr = base->path_hdr;
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD1: {
+			const struct ovs_nsh_key_md1 *md1 =
+				(struct ovs_nsh_key_md1 *)nla_data(a);
+
+			mdlen = nla_len(a);
+			memcpy(pnp->metadata, md1, mdlen);
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD2: {
+			const struct u8 *md2 = nla_data(a);
+
+			mdlen = nla_len(a);
+			memcpy(pnp->metadata, md2, mdlen);
+			break;
+		}
+		default:
+			OVS_NLERR(1, "Unknown nsh attribute %d",
+				  type);
+			return -EINVAL;
+		}
+	}
+
+	if (rem > 0) {
+		OVS_NLERR(1, "nsh attribute has %d unknown bytes.", rem);
+		return -EINVAL;
+	}
+
+	/* nsh header length  = NSH_BASE_HDR_LEN + mdlen */
+	pnp->ver_flags_len = htons(flags << NSH_FLAGS_SHIFT |
+				   (NSH_BASE_HDR_LEN + mdlen) >> 2);
+
+	return 0;
+}
+
+int nsh_key_from_nlattr(const struct nlattr *attr,
+			struct ovs_key_nsh *nsh)
+{
+	struct nlattr *a;
+	int rem;
+
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+
+		if (type > OVS_NSH_KEY_ATTR_MAX) {
+			OVS_NLERR(1, "nsh attr %d is out of range max %d",
+				  type, OVS_NSH_KEY_ATTR_MAX);
+			return -EINVAL;
+		}
+
+		if (!check_attr_len(nla_len(a),
+				    ovs_nsh_key_attr_lens[type].len)) {
+			OVS_NLERR(
+			    1,
+			    "nsh attr %d has unexpected len %d expected %d",
+			    type,
+			    nla_len(a),
+			    ovs_nsh_key_attr_lens[type].len
+			);
+			return -EINVAL;
+		}
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base =
+				(struct ovs_nsh_key_base *)nla_data(a);
+			nsh->flags = base->flags;
+			nsh->mdtype = base->mdtype;
+			nsh->np = base->np;
+			nsh->path_hdr = base->path_hdr;
+			break;

net/openvswitch/flow_netlink.c static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match, · line 1323

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+		}
+		case OVS_NSH_KEY_ATTR_MD1: {
+			const struct ovs_nsh_key_md1 *md1 =
+				(struct ovs_nsh_key_md1 *)nla_data(a);
+			memcpy(nsh->context, md1->context, sizeof(*md1));
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD2:
+			/* Not supported yet */
+			break;

When we encounter these metadata attributes do we need to verify that nsh->mdtype is set correctly? Keep in mind we may parse ATTR_MD{1,2} before ATTR_BASE.

Quoted 87 lines
net/openvswitch/flow_netlink.c +84 -0
+		default:
+			OVS_NLERR(1, "Unknown nsh attribute %d",
+				  type);
+			return -EINVAL;
+		}
+	}
+
+	if (rem > 0) {
+		OVS_NLERR(1, "nsh attribute has %d unknown bytes.", rem);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int nsh_key_put_from_nlattr(const struct nlattr *attr,
+				   struct sw_flow_match *match, bool is_mask,
+				   bool log)
+{
+	struct nlattr *a;
+	int rem;
+
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+		int i;
+
+		if (type > OVS_NSH_KEY_ATTR_MAX) {
+			OVS_NLERR(log, "nsh attr %d is out of range max %d",
+				  type, OVS_NSH_KEY_ATTR_MAX);
+			return -EINVAL;
+		}
+
+		if (!check_attr_len(nla_len(a),
+				    ovs_nsh_key_attr_lens[type].len)) {
+			OVS_NLERR(
+			    log,
+			    "nsh attr %d has unexpected len %d expected %d",
+			    type,
+			    nla_len(a),
+			    ovs_nsh_key_attr_lens[type].len
+			);
+			return -EINVAL;
+		}
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base =
+				(struct ovs_nsh_key_base *)nla_data(a);
+			SW_FLOW_KEY_PUT(match, nsh.flags,
+					base->flags, is_mask);
+			SW_FLOW_KEY_PUT(match, nsh.mdtype,
+					base->mdtype, is_mask);
+			SW_FLOW_KEY_PUT(match, nsh.np,
+					base->np, is_mask);
+			SW_FLOW_KEY_PUT(match, nsh.path_hdr,
+					base->path_hdr, is_mask);
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD1: {
+			const struct ovs_nsh_key_md1 *md1 =
+				(struct ovs_nsh_key_md1 *)nla_data(a);
+			for (i = 0; i < 4; i++)
+				SW_FLOW_KEY_PUT(match, nsh.context[i],
+						md1->context[i], is_mask);
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD2:
+			/* Not supported yet */
+			break;
+		default:
+			OVS_NLERR(log, "Unknown nsh attribute %d",
+				  type);
+			return -EINVAL;
+		}
+	}
+
+	if (rem > 0) {
+		OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
 				u64 attrs, const struct nlattr **a,
 				bool is_mask, bool log)

[..]

#7 lore raw
Quoted 8 lines · Eric Garver

include/net/nsh.h line 94

Quoted 3 lines · Yi Yang
include/net/nsh.h new file +3 -0
+
+#define NSH_DST_PORT    4790     /* UDP Port for NSH on VXLAN. */
+#define ETH_P_NSH       0x894F   /* Ethertype for NSH. */

ETH_P_NSH probably belongs in include/uapi/linux/if_ether.h with all the other ETH_P_* defines.

Ok, I'll move it to include/uapi/linux/if_ether.h, but in userspace, we still need to keep it in nsh.h.

Quoted 14 lines
Quoted 14 lines
net/openvswitch/flow.h +9 -0
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 struct sw_flow_key {
 	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
 	u8 tun_opts_len;
@@ -144,6 +154,7 @@ struct sw_flow_key {
Quoted 9 lines

net/openvswitch/flow.h struct sw_flow_key { · line 157

Quoted 4 lines
net/openvswitch/flow.h +1 -0
 			};
 		} ipv6;
 	};
+	struct ovs_key_nsh nsh;         /* network service header */

Are you intentionally not reserving space in the flow key for OVS_NSH_KEY_ATTR_MD2? I know it's not supported yet, but much of the code is stubbed out for it - just making sure this wasn't an oversight.

For MD type 2, we'll reuse tun_metedata keys in struct flow_tnl which will be reworked and it will be shared by NSH and GENEVE, so we won't have new keys in "struct ovs_key_nsh" for MD type 2.

Quoted 35 lines
Quoted 21 lines
net/openvswitch/flow.h +0 -0
 	struct {
 		/* Connection tracking fields not packed above. */
 		struct {
net/openvswitch/flow_netlink.c +2 -0
index f07d10a..79059db 100644
@@ -78,9 +78,11 @@ static bool actions_may_change_flow(const struct nlattr *actions)
 		case OVS_ACTION_ATTR_HASH:
 		case OVS_ACTION_ATTR_POP_ETH:
 		case OVS_ACTION_ATTR_POP_MPLS:
+		case OVS_ACTION_ATTR_POP_NSH:
 		case OVS_ACTION_ATTR_POP_VLAN:
 		case OVS_ACTION_ATTR_PUSH_ETH:
 		case OVS_ACTION_ATTR_PUSH_MPLS:
+		case OVS_ACTION_ATTR_PUSH_NSH:
 		case OVS_ACTION_ATTR_PUSH_VLAN:
 		case OVS_ACTION_ATTR_SAMPLE:
 		case OVS_ACTION_ATTR_SET:
@@ -322,12 +324,22 @@ size_t ovs_tun_key_attr_size(void)
 		+ nla_total_size(2);   /* OVS_TUNNEL_KEY_ATTR_TP_DST */

net/openvswitch/flow_netlink.c size_t ovs_tun_key_attr_size(void) · line 334

Quoted 10 lines
net/openvswitch/flow_netlink.c +8 -0
 }
 
+size_t ovs_nsh_key_attr_size(void)
+{
+	/* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
+	 * updating this function.
+	 */
+	return  nla_total_size(8)      /* OVS_NSH_KEY_ATTR_BASE */
+		+ nla_total_size(16)   /* OVS_NSH_KEY_ATTR_MD1 */
+		+ nla_total_size(248); /* OVS_NSH_KEY_ATTR_MD2 */

_MD1 and _MD2 are mutually exclusive. You only need the larger of the two.

Quoted 1 line

Maybe use OVS_PUSH_NSH_MAX_MD_LEN instead of 248.

Got it, will use nla_total_size(NSH_M_TYPE2_MAX_LEN - NSH_BASE_HDR_LEN)

Quoted 16 lines
Quoted 16 lines
net/openvswitch/flow_netlink.c +16 -0
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base =
+				(struct ovs_nsh_key_base *)nla_data(a);
+			nsh->flags = base->flags;
+			nsh->mdtype = base->mdtype;
+			nsh->np = base->np;
+			nsh->path_hdr = base->path_hdr;
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD1: {
+			const struct ovs_nsh_key_md1 *md1 =
+				(struct ovs_nsh_key_md1 *)nla_data(a);
+			memcpy(nsh->context, md1->context, sizeof(*md1));
+			break;
Quoted 8 lines

net/openvswitch/flow_netlink.c static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match, · line 1323

Quoted 4 lines
net/openvswitch/flow_netlink.c +4 -0
+		}
+		case OVS_NSH_KEY_ATTR_MD2:
+			/* Not supported yet */
+			break;

When we encounter these metadata attributes do we need to verify that nsh->mdtype is set correctly? Keep in mind we may parse ATTR_MD{1,2} before ATTR_BASE.

Good point, will add check code for this.

#8 lore raw
Quoted 37 lines · Yi
Quoted 8 lines · Eric Garver

include/net/nsh.h line 94

Quoted 3 lines · Yi Yang
include/net/nsh.h new file +3 -0
+
+#define NSH_DST_PORT    4790     /* UDP Port for NSH on VXLAN. */
+#define ETH_P_NSH       0x894F   /* Ethertype for NSH. */

ETH_P_NSH probably belongs in include/uapi/linux/if_ether.h with all the other ETH_P_* defines.

Ok, I'll move it to include/uapi/linux/if_ether.h, but in userspace, we still need to keep it in nsh.h.

Quoted 14 lines
Quoted 14 lines
net/openvswitch/flow.h +9 -0
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 struct sw_flow_key {
 	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
 	u8 tun_opts_len;
@@ -144,6 +154,7 @@ struct sw_flow_key {
Quoted 9 lines

net/openvswitch/flow.h struct sw_flow_key { · line 157

Quoted 4 lines
net/openvswitch/flow.h +1 -0
 			};
 		} ipv6;
 	};
+	struct ovs_key_nsh nsh;         /* network service header */

Are you intentionally not reserving space in the flow key for OVS_NSH_KEY_ATTR_MD2? I know it's not supported yet, but much of the code is stubbed out for it - just making sure this wasn't an oversight.

Quoted 3 lines

For MD type 2, we'll reuse tun_metedata keys in struct flow_tnl which will be reworked and it will be shared by NSH and GENEVE, so we won't have new keys in "struct ovs_key_nsh" for MD type 2.

Be careful here. VXLAN also uses tun_metadata for GBP. VXLAN-GPE (+ NSH) and VXLAN-GBP are mutually exclusive AFAICS, but you should verify it all behaves as expected.

[..]