Skip to contentSkip to conversation

openvswitch: enable NSH support

PATCH Superseded Yi Yang 8 participants 17 messages Aug 10, 2017 – Aug 15, 2017 4 messages not in the archive

Subsystems (3)

v2 of 17 Superseded Latest is v17 →

lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH v2 1/1 openvswitch: enable NSH support

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                | 126 ++++++++++++++++++++++++++++++
 include/uapi/linux/openvswitch.h |  33 ++++++++
 net/openvswitch/actions.c        | 165 +++++++++++++++++++++++++++++++++++++++
 net/openvswitch/flow.c           |  41 ++++++++++
 net/openvswitch/flow.h           |   1 +
 net/openvswitch/flow_netlink.c   |  54 ++++++++++++-
 7 files changed, 426 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 +126 -0

126 lines not on this page · raw message

include/uapi/linux/openvswitch.h +33 -0
index 156ee4c..5a1c94b 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,                  /* struct ovs_key_nsh */
 
 #ifdef __KERNEL__
 	OVS_KEY_ATTR_TUNNEL_INFO,  /* struct ip_tunnel_info */
@@ -491,6 +492,15 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 c[4];
+};
+
 /**
  * enum ovs_flow_attr - attributes for %OVS_FLOW_* commands.
  * @OVS_FLOW_ATTR_KEY: Nested %OVS_KEY_ATTR_* attributes specifying the flow
@@ -769,6 +779,25 @@ struct ovs_action_push_eth {
 	struct ovs_key_ethernet addresses;
 };
 
+#define OVS_PUSH_NSH_MAX_MD_LEN 248
+/*
+ * struct ovs_action_push_nsh - %OVS_ACTION_ATTR_PUSH_NSH
+ * @flags: NSH header flags.
+ * @mdtype: NSH metadata type.
+ * @mdlen: Length of NSH metadata in bytes.
+ * @np: NSH next_protocol: Inner packet type.
+ * @path_hdr: NSH service path id and service index.
+ * @metadata: NSH metadata for MD type 1 or 2
+ */
+struct ovs_action_push_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 mdlen;
+	__u8 np;
+	__be32 path_hdr;
+	__u8 metadata[];
+};
+
 /**
  * enum ovs_action_attr - Action types.
  *
@@ -806,6 +835,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 +866,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,     /* struct ovs_action_push_nsh. */
+	OVS_ACTION_ATTR_POP_NSH,      /* No argument. */
 
 	__OVS_ACTION_ATTR_MAX,	      /* Nothing past this will be accepted
 				       * from userspace. */
net/openvswitch/actions.c +165 -0

195 lines not on this page · raw message

net/openvswitch/flow.c +41 -0

59 lines not on this page · raw message

net/openvswitch/flow.h +1 -0
index 1875bba..d2a0e56 100644
@@ -144,6 +144,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 +53 -1

117 lines not on this page · raw message

Signature · 1 line
2.5.5

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

#2 lore raw
Quoted 4 lines · Yi Yang

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.

Please include changelog when posting a new version of a patch.

include/net/nsh.h line 109

Quoted 2 lines
include/net/nsh.h new file +2 -0
+static inline u16
+nsh_hdr_len(const struct nsh_hdr *nsh)

Single line, please. And all other instances of this in nsh.h, too.

Quoted 11 lines
include/uapi/linux/openvswitch.h +1 -0
@@ -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,                  /* struct ovs_key_nsh */
 
 #ifdef __KERNEL__
 	OVS_KEY_ATTR_TUNNEL_INFO,  /* struct ip_tunnel_info */

include/uapi/linux/openvswitch.h struct ovs_key_ct_tuple_ipv6 { · line 501

Quoted 10 lines
include/uapi/linux/openvswitch.h +7 -0
@@ -491,6 +492,15 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 c[4];

I still don't like the "c" name. Please change it to something descriptive. This is uAPI and can't be changed later.

And I still don't see my comment about this not being extensible for MD type 2 addressed. Please understand this is uAPI and it is set in stone once it is merged into the kernel. It's very important we get this right since the beginning.

include/uapi/linux/openvswitch.h struct ovs_action_push_eth { · line 799

Quoted 8 lines
include/uapi/linux/openvswitch.h +8 -0
+struct ovs_action_push_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 mdlen;
+	__u8 np;
+	__be32 path_hdr;
+	__u8 metadata[];
+};

This is not how netlink attributes work. Please reread Ben Pfaff's explanation on how this needs to be structured (Message-ID:
<[email protected]>) and rework the patch. I 100% agree with what he wrote, his proposal is very clean and matches how netlink
is designed.

include/uapi/linux/openvswitch.h enum ovs_action_attr { · line 870

Quoted 6 lines
include/uapi/linux/openvswitch.h +2 -0
@@ -835,6 +866,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,     /* struct ovs_action_push_nsh. */
+	OVS_ACTION_ATTR_POP_NSH,      /* No argument. */

Thank you for changing this to push/pop, it looks much cleaner now.

include/uapi/linux/openvswitch.h enum ovs_action_attr {

Quoted 12 lines
include/uapi/linux/openvswitch.h +12 -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);
+	u16 ver_flags_len;
+	u8 version, length;
+	u32 path_hdr;
+	int i;
+
+	memset(&key->nsh, 0, sizeof(struct ovs_key_nsh));
+	ver_flags_len = ntohs(nsh->ver_flags_len);
+	version = (ver_flags_len & NSH_VER_MASK) >> NSH_VER_SHIFT;
+	length = (ver_flags_len & NSH_LEN_MASK) >> NSH_LEN_SHIFT;

A nit: the operation to get/set version, length and flags from the NSH header seems to be repeated enough to warrant helper functions in include/net/nsh.h. Something like:

static inline u8 nsh_get_version(const struct nsh_hdr *nsh)

{
	return (ntohs(nsh->ver_flags_len) & NSH_VER_MASK) >> NSH_VER_SHIFT;
}

etc.

Not a blocker, though, it may be done later if needed.

include/uapi/linux/openvswitch.h static bool actions_may_change_flow(const struct nlattr *actions) · line 87

Quoted 12 lines
include/uapi/linux/openvswitch.h +2 -0
@@ -76,9 +77,11 @@ static bool actions_may_change_flow(const struct nlattr *actions)
 
 		case OVS_ACTION_ATTR_CT:
 		case OVS_ACTION_ATTR_HASH:
+		case OVS_ACTION_ATTR_POP_NSH:
 		case OVS_ACTION_ATTR_POP_ETH:
 		case OVS_ACTION_ATTR_POP_MPLS:
 		case OVS_ACTION_ATTR_POP_VLAN:
+		case OVS_ACTION_ATTR_PUSH_NSH:
 		case OVS_ACTION_ATTR_PUSH_ETH:
 		case OVS_ACTION_ATTR_PUSH_MPLS:
 		case OVS_ACTION_ATTR_PUSH_VLAN:

Alphabetical order, please.

Thanks,

Jiri

#3 lore raw
Quoted 6 lines · Jiri Benc
Quoted 4 lines · Yi Yang

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.

Quoted 6 lines

Please include changelog when posting a new version of a patch.

include/net/nsh.h line 109

Quoted 2 lines
include/net/nsh.h new file +2 -0
+static inline u16
+nsh_hdr_len(const struct nsh_hdr *nsh)

Single line, please. And all other instances of this in nsh.h, too.

Thanks Jiri, I'll change these in next version.

Quoted 15 lines
Quoted 15 lines
include/uapi/linux/openvswitch.h +1 -0
@@ -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,                  /* struct ovs_key_nsh */
 
 #ifdef __KERNEL__
 	OVS_KEY_ATTR_TUNNEL_INFO,  /* struct ip_tunnel_info */
@@ -491,6 +492,15 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
Quoted 10 lines

include/uapi/linux/openvswitch.h struct ovs_key_ct_tuple_ipv6 { · line 501

Quoted 7 lines
include/uapi/linux/openvswitch.h +7 -0
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 c[4];

I still don't like the "c" name. Please change it to something descriptive. This is uAPI and can't be changed later.

is "__be32 context[4]" ok?

Quoted 5 lines

And I still don't see my comment about this not being extensible for MD type 2 addressed. Please understand this is uAPI and it is set in stone once it is merged into the kernel. It's very important we get this right since the beginning.

I understood it, but I don't know how I can do this per your comments,

Per Ben's comments,

PUSH_NSH message looks like

PUSH_NSH begin
nsh base header
MD type 1
PUSH_NSH end

or

PUSH_NSH begin
nsh base header
MD type 2
PUSH_NSH end

If so, I think we don't need struct ovs_action_push_nsh at all, just as _CT action did, treat it as a variable data buffer.

So define three new netlink attributes

OVS_ACTION_ATTR_NSH_BASE_HEADER
OVS_ACTION_ATTR_NSH_MD1_DATA
OVS_ACTION_ATTR_NSH_MD2_DATA

OVS_ACTION_ATTR_PUSH_NSH is nested netlink attribute, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD1_DATA for MD type 1, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD2_DATA for MD type 2. I'll compeletely remove struct ovs_action_push_nsh, is it ok?

Quoted 42 lines

include/uapi/linux/openvswitch.h struct ovs_action_push_eth { · line 799

Quoted 9 lines
include/uapi/linux/openvswitch.h +8 -0
+struct ovs_action_push_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 mdlen;
+	__u8 np;
+	__be32 path_hdr;
+	__u8 metadata[];
+};

This is not how netlink attributes work. Please reread Ben Pfaff's explanation on how this needs to be structured (Message-ID:
<[email protected]>) and rework the patch. I 100% agree with what he wrote, his proposal is very clean and matches how netlink
is designed.

include/uapi/linux/openvswitch.h enum ovs_action_attr { · line 870

Quoted 6 lines
include/uapi/linux/openvswitch.h +2 -0
@@ -835,6 +866,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,     /* struct ovs_action_push_nsh. */
+	OVS_ACTION_ATTR_POP_NSH,      /* No argument. */

Thank you for changing this to push/pop, it looks much cleaner now.

include/uapi/linux/openvswitch.h enum ovs_action_attr {

Quoted 12 lines
include/uapi/linux/openvswitch.h +12 -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);
+	u16 ver_flags_len;
+	u8 version, length;
+	u32 path_hdr;
+	int i;
+
+	memset(&key->nsh, 0, sizeof(struct ovs_key_nsh));
+	ver_flags_len = ntohs(nsh->ver_flags_len);
+	version = (ver_flags_len & NSH_VER_MASK) >> NSH_VER_SHIFT;
+	length = (ver_flags_len & NSH_LEN_MASK) >> NSH_LEN_SHIFT;

A nit: the operation to get/set version, length and flags from the NSH header seems to be repeated enough to warrant helper functions in include/net/nsh.h. Something like:

Quoted 8 lines

static inline u8 nsh_get_version(const struct nsh_hdr *nsh)

{
	return (ntohs(nsh->ver_flags_len) & NSH_VER_MASK) >> NSH_VER_SHIFT;
}

etc.

Not a blocker, though, it may be done later if needed.

Ok, will change it.

Quoted 15 lines

include/uapi/linux/openvswitch.h static bool actions_may_change_flow(const struct nlattr *actions) · line 87

Quoted 13 lines
include/uapi/linux/openvswitch.h +2 -0
@@ -76,9 +77,11 @@ static bool actions_may_change_flow(const struct nlattr *actions)
 
 		case OVS_ACTION_ATTR_CT:
 		case OVS_ACTION_ATTR_HASH:
+		case OVS_ACTION_ATTR_POP_NSH:
 		case OVS_ACTION_ATTR_POP_ETH:
 		case OVS_ACTION_ATTR_POP_MPLS:
 		case OVS_ACTION_ATTR_POP_VLAN:
+		case OVS_ACTION_ATTR_PUSH_NSH:
 		case OVS_ACTION_ATTR_PUSH_ETH:
 		case OVS_ACTION_ATTR_PUSH_MPLS:
 		case OVS_ACTION_ATTR_PUSH_VLAN:

Alphabetical order, please.

Ok, will change.

Quoted 4 lines

Thanks,

Jiri

#4 lore raw
Quoted 1 line · Yi

is "__be32 context[4]" ok?

Yes, that looks better.

Quoted 11 lines

So define three new netlink attributes

OVS_ACTION_ATTR_NSH_BASE_HEADER
OVS_ACTION_ATTR_NSH_MD1_DATA
OVS_ACTION_ATTR_NSH_MD2_DATA

OVS_ACTION_ATTR_PUSH_NSH is nested netlink attribute, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD1_DATA for MD type 1, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD2_DATA for MD type 2. I'll compeletely remove struct ovs_action_push_nsh, is it ok?

Yes, that's the way to do it.

What should be done with struct ovs_key_nsh? Even with "c" renamed to "context", it's still MD type 1 only structure. What is the plan for MD type 2 support wrt. this structure?

Thanks,

Jiri

author
#5 lore raw

So far, we're not clear how we can support MD type 2 better, as I explained before, we need to reuse tun_metadata in struct flow_tnl which is the thing Geneve is using. Geneve predefined 64 keys for this from tun_metadata0 to tun_metadata63, we will reuse it for MD type 2. But you know NSH is not tunnel, so it has to be changed to support both Geneve and NSH. Anyway, they won't be part of ovs_key_nsh.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Jiri Benc
Sent: Friday, August 11, 2017 5:11 PM
To: Yang, Yi Y <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH net-next v2] openvswitch: enable NSH support

Quoted 1 line · Yi

is "__be32 context[4]" ok?

Yes, that looks better.

Quoted 11 lines

So define three new netlink attributes

OVS_ACTION_ATTR_NSH_BASE_HEADER
OVS_ACTION_ATTR_NSH_MD1_DATA
OVS_ACTION_ATTR_NSH_MD2_DATA

OVS_ACTION_ATTR_PUSH_NSH is nested netlink attribute, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD1_DATA for MD type 1, it will nest OVS_ACTION_ATTR_NSH_BASE_HEADER and OVS_ACTION_ATTR_NSH_MD2_DATA for MD type 2. I'll compeletely remove struct ovs_action_push_nsh, is it ok?

Yes, that's the way to do it.

What should be done with struct ovs_key_nsh? Even with "c" renamed to "context", it's still MD type 1 only structure. What is the plan for MD type 2 support wrt. this structure?

Thanks,

Jiri

#6 lore raw
Quoted 7 lines · Yi Y

So far, we're not clear how we can support MD type 2 better, as I explained before, we need to reuse tun_metadata in struct flow_tnl which is the thing Geneve is using. Geneve predefined 64 keys for this from tun_metadata0 to tun_metadata63, we will reuse it for MD type 2. But you know NSH is not tunnel, so it has to be changed to support both Geneve and NSH. Anyway, they won't be part of ovs_key_nsh.

Please do not top post.

The context field does not apply to MD type 2. It looks wrong for the context field to be included in netlink attribute for anything other than MD type 1. Perhaps it needs to be put into a separate attribute, too?

Note that I'm talking only about the uAPI. Internally, ovs can use struct ovs_key_nsh that is MD type 1 only, there's no problem changing that later. But for the user space interface, this needs to be clean. This can be solved for example this way:

In include/uapi/linux/openvswitch.h:

struct ovs_key_nsh_base {
	__u8 flags;
	__u8 mdtype;
	__u8 np;
	__u8 pad;
	__be32 path_hdr;
};

+ one more netlink attribute carrying MD type 1 info. Will probably require to change OVS_KEY_ATTR_NSH to a nested attribute etc.

In net/openvswitch/flow.h (or perhaps a different header would be more appropriate?):

struct ovs_key_nsh {
	struct ovs_key_nsh_base base;
	__be32 context[4];
};

Plus needed conversions between OVS_KEY_ATTR_NSH and struct ovs_key_nsh when interfacing between the kernel and user space.

That way, we can have MD type 1 support only for now while still being allowed to redesign things in whatever way later.

Jiri

author
#7 lore raw
Quoted 10 lines · Jiri Benc
Quoted 7 lines · Yi Y

So far, we're not clear how we can support MD type 2 better, as I explained before, we need to reuse tun_metadata in struct flow_tnl which is the thing Geneve is using. Geneve predefined 64 keys for this from tun_metadata0 to tun_metadata63, we will reuse it for MD type 2. But you know NSH is not tunnel, so it has to be changed to support both Geneve and NSH. Anyway, they won't be part of ovs_key_nsh.

Please do not top post.

Sorry for this inconvenience, I'm using outlook. But now I have to use mutt to address your concern :-)

Quoted 32 lines

The context field does not apply to MD type 2. It looks wrong for the context field to be included in netlink attribute for anything other than MD type 1. Perhaps it needs to be put into a separate attribute, too?

Note that I'm talking only about the uAPI. Internally, ovs can use struct ovs_key_nsh that is MD type 1 only, there's no problem changing that later. But for the user space interface, this needs to be clean. This can be solved for example this way:

In include/uapi/linux/openvswitch.h:

struct ovs_key_nsh_base {
	__u8 flags;
	__u8 mdtype;
	__u8 np;
	__u8 pad;
	__be32 path_hdr;
};

+ one more netlink attribute carrying MD type 1 info. Will probably require to change OVS_KEY_ATTR_NSH to a nested attribute etc.

In net/openvswitch/flow.h (or perhaps a different header would be more appropriate?):

struct ovs_key_nsh {
	struct ovs_key_nsh_base base;
	__be32 context[4];
};
Quoted 5 lines

Plus needed conversions between OVS_KEY_ATTR_NSH and struct ovs_key_nsh when interfacing between the kernel and user space.

That way, we can have MD type 1 support only for now while still being allowed to redesign things in whatever way later.

Yeah, good suggestion, will try to do that way.

Quoted 2 lines

Jiri

#8 lore raw
In reply to Jiri Benc
Quoted 35 lines

-----Original Message-----
From: Jiri Benc [mailto:[email protected]]
Sent: Friday, 11 August, 2017 11:45

The context field does not apply to MD type 2. It looks wrong for the context field to be included in netlink attribute for anything other than MD type 1. Perhaps it needs to be put into a separate attribute, too?

Note that I'm talking only about the uAPI. Internally, ovs can use struct ovs_key_nsh that is MD type 1 only, there's no problem changing that later. But for the user space interface, this needs to be clean. This can be solved for example this way:

In include/uapi/linux/openvswitch.h:

struct ovs_key_nsh_base {
	__u8 flags;
	__u8 mdtype;
	__u8 np;
	__u8 pad;
	__be32 path_hdr;
};

+ one more netlink attribute carrying MD type 1 info. Will probably require to change OVS_KEY_ATTR_NSH to a nested attribute etc.

In net/openvswitch/flow.h (or perhaps a different header would be more appropriate?):

struct ovs_key_nsh {
	struct ovs_key_nsh_base base;
	__be32 context[4];
};
Quoted 7 lines

Plus needed conversions between OVS_KEY_ATTR_NSH and struct ovs_key_nsh when interfacing between the kernel and user space.

That way, we can have MD type 1 support only for now while still being allowed to redesign things in whatever way later.

Jiri

For the OVS_KEY_ATTR_NSH I agree to move the fixed size MD1 context headers from nsh_base to a separate struct and use nested netlink attributes.

For OVS_ACTION_ATTR_PUSH_NSH attribute any metadata included is opaque to the datapath and should be copied as is into the packet header. I doubt that there is any benefit to model this with nested attributes for MD1 or MD2. This just adds complexity on sender and receiver side and requires updates in case there should be other MD types added later on.

Unless someone can explain to me why the datapath should understand the internal structure/format of metadata in push_nsh, I would strongly vote to keep the metadata as variable length octet sequence in the non-structured OVS_ACTION_ATTR_PUSH_NSH

BR, Jan

A message that is not in the archive (sent off-list or not yet received): CFF8EF42F1132E4CBE2BF0AB6C21C58D7273679A-hqolJogE5njKJFWPz4pdheaU1rCVNFv4@public.gmane.org

#10 lore raw
Quoted 4 lines · Jan Scheurich

Unless someone can explain to me why the datapath should understand the internal structure/format of metadata in push_nsh, I would strongly vote to keep the metadata as variable length octet sequence in the non-structured OVS_ACTION_ATTR_PUSH_NSH

Could be but it still needs to be in a different attribute and not in the ovs_action_push_nsh structure.

Separate attributes for MD1/MD2 has the advantage of easier validation:
with a separate MD1 type attribute, the size check is easier. With an unstructured MD attribute, we'd need to look into the OVS_ACTION_ATTR_NSH_BASE_HEADER attribute for mdtype and then validate the unstructured MD attribute size manually. Not a big deal, though. I don't have strong opinion here.

But I do have strong opinion that MD needs to go into a separate attribute, whether there are separate attributes for MD1/2 or not.

Jiri

#11 lore raw
Quoted 19 lines

From: Jiri Benc [mailto:[email protected]]
Sent: Friday, 11 August, 2017 12:23

Quoted 4 lines · Jan Scheurich

Unless someone can explain to me why the datapath should understand the internal structure/format of metadata in push_nsh, I would strongly vote to keep the metadata as variable length octet sequence in the non-structured OVS_ACTION_ATTR_PUSH_NSH

Could be but it still needs to be in a different attribute and not in the ovs_action_push_nsh structure.

Separate attributes for MD1/MD2 has the advantage of easier validation:
with a separate MD1 type attribute, the size check is easier. With an unstructured MD attribute, we'd need to look into the OVS_ACTION_ATTR_NSH_BASE_HEADER attribute for mdtype and then validate the unstructured MD attribute size manually. Not a big deal, though. I don't have strong opinion here.

Quoted 2 lines

But I do have strong opinion that MD needs to go into a separate attribute, whether there are separate attributes for MD1/2 or not.

Jiri, I am not too familiar with conventions on the OVS netlink interface regarding the handling of variable length fields. What is the benefit of structuring the push_nsh action into

OVS_ACTION_ATTR_PUSH_NSH

+-- OVS_ACTION_ATTR_NSH_BASE_HEADER
+-- OVS_ACTION_ATTR_NSH_MD

instead of grouping the base header fields and the variable length MD into a single monolithic attribute OVS_ACTION_ATTR_PUSH_NSH? Is the main concern a potential future need to add additional parameters to the push_nsh datapath action? Are there examples for such structured actions other than OVS_ACTION_ATTR_CT where the need is obvious because it is very polymorphic?

BR, Jan

BTW: The name OVS_ACTION_ATTR_NSH_BASE_HEADER is misleading because in the NSH draft the term base header is used for the first 32-bit word, whereas here it includes also the 32-bit Service Path header.

A message that is not in the archive (sent off-list or not yet received): CFF8EF42F1132E4CBE2BF0AB6C21C58D72736EAE-hqolJogE5njKJFWPz4pdheaU1rCVNFv4@public.gmane.org

#13 lore raw
Quoted 8 lines · Jan Scheurich

Jiri, I am not too familiar with conventions on the OVS netlink interface regarding the handling of variable length fields. What is the benefit of structuring the push_nsh action into

OVS_ACTION_ATTR_PUSH_NSH

+-- OVS_ACTION_ATTR_NSH_BASE_HEADER
+-- OVS_ACTION_ATTR_NSH_MD
Quoted 6 lines

instead of grouping the base header fields and the variable length MD into a single monolithic attribute OVS_ACTION_ATTR_PUSH_NSH? Is the main concern a potential future need to add additional parameters to the push_nsh datapath action? Are there examples for such structured actions other than OVS_ACTION_ATTR_CT where the need is obvious because it is very polymorphic?

This is about having the design clean. What would you do if you had two variable length fields? Would you still put them in a single structure and have a length field in the structure, too? That would be wrong, we have length in the attribute header. I doubt you would do that. Which indicates that putting variable length fields to an attribute with anything other is wrong.

Also, look at how ugly the code would be. You'd have to subtract the base header length from the attribute length to get the variable data length. That's not nice at all.

Think about the netlink in the way that by default, every field should have its own attribute. Structures are included only for performance reasons where certain fields are always passed in a message and having them in separate attributes would be impractical and waste of space. Going out of your way to include the context data in the structure thus doesn't make sense.

Quoted 3 lines

BTW: The name OVS_ACTION_ATTR_NSH_BASE_HEADER is misleading because in the NSH draft the term base header is used for the first 32-bit word, whereas here it includes also the 32-bit Service Path header.

An excellent comment. This means that it's very well possible that future NSH versions may not include SP header or may have it of a different size. Maybe we should consider putting it into a separate attribute, too? Not sure it is needed, though, I don't think it's likely to happen.

Jiri

#14 lore raw
Quoted 39 lines

From: Jiri Benc [mailto:[email protected]]
Sent: Monday, 14 August, 2017 09:51

Quoted 8 lines · Jan Scheurich

Jiri, I am not too familiar with conventions on the OVS netlink interface regarding the handling of variable length fields. What is the benefit of structuring the push_nsh action into

OVS_ACTION_ATTR_PUSH_NSH

+-- OVS_ACTION_ATTR_NSH_BASE_HEADER
+-- OVS_ACTION_ATTR_NSH_MD
Quoted 6 lines

instead of grouping the base header fields and the variable length MD into a single monolithic attribute OVS_ACTION_ATTR_PUSH_NSH? Is the main concern a potential future need to add additional parameters to the push_nsh datapath action? Are there examples for such structured actions other than OVS_ACTION_ATTR_CT where the need is obvious because it is very polymorphic?

This is about having the design clean. What would you do if you had two variable length fields? Would you still put them in a single structure and have a length field in the structure, too? That would be wrong, we have length in the attribute header. I doubt you would do that. Which indicates that putting variable length fields to an attribute with anything other is wrong.

Also, look at how ugly the code would be. You'd have to subtract the base header length from the attribute length to get the variable data length. That's not nice at all.

Think about the netlink in the way that by default, every field should have its own attribute. Structures are included only for performance reasons where certain fields are always passed in a message and having them in separate attributes would be impractical and waste of space. Going out of your way to include the context data in the structure thus doesn't make sense.

Quoted 1 line

BTW: The name OVS_ACTION_ATTR_NSH_BASE_HEADER is misleading

because

Quoted 8 lines
Quoted 2 lines

in the NSH draft the term base header is used for the first 32-bit word, whereas here it includes also the 32-bit Service Path header.

An excellent comment. This means that it's very well possible that future NSH versions may not include SP header or may have it of a different size. Maybe we should consider putting it into a separate attribute, too? Not sure it is needed, though, I don't think it's likely to happen.

I think the NSH RFC draft is pretty clear that the NSH header (Version 0) will always contain the Base Header, the Service Header and a (possibly empty) set of Context Headers. The length of the context headers is implied by the total NSH header Length in the Base Header. The internal structure of the context headers, implied by the "MD type" in the base header, is irrelevant for the push_nsh action as it is managed by the ofproto-dpif layer. The current NSH header format simply does not allow for a second variable length section in the header.

Is it worth to speculate on how a hypothetical future NSH version (with a different Version value in the Base header) might look like? If this should ever arise, we could introduce a new push_nsh_v2 action.

In summary, I'm still not convinced that we gain any significant generality by splitting up the OVS_ACTION_ATTR_PUSH_NSH action into nested attributes. It will only slow down the execution of the action in the datapath (or requires some optimization in the datapath to merge the attributes into a single internal action struct).

Note: For the OVS_KEY_ATTR_NSH we should split up in to OVS_KEY_ATTR_NSH_FIXED_HEADER (covering Base and Service headers) and OVS_KEY_ATTR_NSH_MD1.

Jan

A message that is not in the archive (sent off-list or not yet received): CFF8EF42F1132E4CBE2BF0AB6C21C58D727373EA-hqolJogE5njKJFWPz4pdheaU1rCVNFv4@public.gmane.org

#16 lore raw
Quoted 2 lines · Jan Scheurich

Is it worth to speculate on how a hypothetical future NSH version (with a different Version value in the Base header) might look like?

Absolutely. This is uAPI we're talking about and once merged, it's set in stone. Whatever we come up with should allow future extensibility.

Quoted 2 lines

If this should ever arise, we could introduce a new push_nsh_v2 action.

Which would mean we failed with the design. We would have to maintain two parallel APIs forever. This is not how the design should be made.

Jiri

#17 lore raw
Quoted 7 lines

From: Jiri Benc [mailto:[email protected]]
Sent: Monday, 14 August, 2017 12:48

Quoted 2 lines · Jan Scheurich

Is it worth to speculate on how a hypothetical future NSH version (with a different Version value in the Base header) might look like?

Quoted 8 lines

Absolutely. This is uAPI we're talking about and once merged, it's set in stone. Whatever we come up with should allow future extensibility.

Quoted 2 lines

If this should ever arise, we could introduce a new push_nsh_v2 action.

Which would mean we failed with the design. We would have to maintain two parallel APIs forever. This is not how the design should be made.

Well, if that is the ultimate design goal, we'll have no choice.

But if the hypothetic next NSH version looks entirely different, we will have to manage the incompatibility anyhow on the level of the nested attributes. So the only thing we will for sure manage to keep fixed might be the empty wrapper OVS_ACTION_ATTR_PUSH_NSH...

We decided earlier on to go for dedicated push/pop_<Header> actions in the datapath instead of a single pair of (very polymorphic) generic encap/decap datapath actions to make the implementation in the datapath more explicit and straightforward. Why not follow the same philosophy also when we need to support push/pop for incompatible versions of the same protocol?

Jan

#18 lore raw
In reply to Jan Scheurich
Quoted 41 lines · Fri, Aug 11, 2017 at 12:09:36PM CEST, [email protected] wrote:
Quoted 41 lines

-----Original Message-----
From: Jiri Benc [mailto:[email protected]]
Sent: Friday, 11 August, 2017 11:45

The context field does not apply to MD type 2. It looks wrong for the context field to be included in netlink attribute for anything other than MD type 1. Perhaps it needs to be put into a separate attribute, too?

Note that I'm talking only about the uAPI. Internally, ovs can use struct ovs_key_nsh that is MD type 1 only, there's no problem changing that later. But for the user space interface, this needs to be clean. This can be solved for example this way:

In include/uapi/linux/openvswitch.h:

struct ovs_key_nsh_base {
	__u8 flags;
	__u8 mdtype;
	__u8 np;
	__u8 pad;
	__be32 path_hdr;
};

+ one more netlink attribute carrying MD type 1 info. Will probably require to change OVS_KEY_ATTR_NSH to a nested attribute etc.

In net/openvswitch/flow.h (or perhaps a different header would be more appropriate?):

struct ovs_key_nsh {
	struct ovs_key_nsh_base base;
	__be32 context[4];
};

Plus needed conversions between OVS_KEY_ATTR_NSH and struct ovs_key_nsh when interfacing between the kernel and user space.

That way, we can have MD type 1 support only for now while still being allowed to redesign things in whatever way later.

Quoted 7 lines
Quoted 1 line

Jiri

For the OVS_KEY_ATTR_NSH I agree to move the fixed size MD1 context headers from nsh_base to a separate struct and use nested netlink attributes.

For OVS_ACTION_ATTR_PUSH_NSH attribute any metadata included is opaque to the datapath and should be copied as is into the packet header. I doubt that there is any benefit to model this with nested attributes for MD1 or MD2. This just adds complexity on sender and receiver side and requires updates in case there should be other MD types added later on.

Unless someone can explain to me why the datapath should understand the internal structure/format of metadata in push_nsh, I would strongly vote to keep the metadata as variable length octet sequence in the non-structured OVS_ACTION_ATTR_PUSH_NSH

Could you please wrap lines at 72 chars? This is unreadable. Thanks!

#19 lore raw
In reply to a message not in the archive
Quoted 6 lines · Yi Yang

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.

Quoted 10 lines
---
 drivers/net/vxlan.c              |   7 ++
 include/net/nsh.h                | 126 ++++++++++++++++++++++++++++++
 include/uapi/linux/openvswitch.h |  33 ++++++++
 net/openvswitch/actions.c        | 165 +++++++++++++++++++++++++++++++++++++++
 net/openvswitch/flow.c           |  41 ++++++++++
 net/openvswitch/flow.h           |   1 +
 net/openvswitch/flow_netlink.c   |  54 ++++++++++++-
 7 files changed, 426 insertions(+), 1 deletion(-)
 create mode 100644 include/net/nsh.h

Hi Yi,

In general I'd like to echo Jiri's comments on the netlink attributes. I'd like to see the metadata separate.

I have a few other comments below.

Thanks.
Eric.

[..]

Quoted 107 lines
include/net/nsh.h new file +101 -0
new file mode 100644
index 0000000..96477a1
@@ -0,0 +1,126 @@
+#ifndef __NET_NSH_H
+#define __NET_NSH_H 1
+
+
+/*
+ * Network Service Header:
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |Ver|O|C|R|R|R|R|R|R|    Length   |   MD Type   |  Next Proto   |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                Service Path ID                | Service Index |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                                                               |
+ * ~               Mandatory/Optional Context Header               ~
+ * |                                                               |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * Ver = The version field is used to ensure backward compatibility
+ *       going forward with future NSH updates.  It MUST be set to 0x0
+ *       by the sender, in this first revision of NSH.
+ *
+ * O = OAM. when set to 0x1 indicates that this packet is an operations
+ *     and management (OAM) packet.  The receiving SFF and SFs nodes
+ *     MUST examine the payload and take appropriate action.
+ *
+ * C = context. Indicates that a critical metadata TLV is present.
+ *
+ * Length : total length, in 4-byte words, of NSH including the Base
+ *          Header, the Service Path Header and the optional variable
+ *          TLVs.
+ * MD Type: indicates the format of NSH beyond the mandatory Base Header
+ *          and the Service Path Header.
+ *
+ * Next Protocol: indicates the protocol type of the original packet. A
+ *          new IANA registry will be created for protocol type.
+ *
+ * Service Path Identifier (SPI): identifies a service path.
+ *          Participating nodes MUST use this identifier for Service
+ *          Function Path selection.
+ *
+ * Service Index (SI): provides location within the SFP.
+ *
+ * [0] https://tools.ietf.org/html/draft-ietf-sfc-nsh-13
+ */
+
+/**
+ * struct nsh_md1_ctx - Keeps track of NSH context data
+ * @nshc<1-4>: NSH Contexts.
+ */
+struct nsh_md1_ctx {
+	__be32 c[4];
+};
+
+struct nsh_md2_tlv {
+	__be16 md_class;
+	u8 type;
+	u8 length;
+	u8 md_value[];
+};
+
+struct nsh_hdr {
+	__be16 ver_flags_len;
+	u8 md_type;
+	u8 next_proto;
+	__be32 path_hdr;
+	union {
+	    struct nsh_md1_ctx md1;
+	    struct nsh_md2_tlv md2[0];
+	};
+};
+
+/* Masking NSH header fields. */
+#define NSH_VER_MASK       0xc000
+#define NSH_VER_SHIFT      14
+#define NSH_FLAGS_MASK     0x3fc0
+#define NSH_FLAGS_SHIFT    6
+#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. */
+
+/* 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
+

include/net/nsh.h line 111

Quoted 10 lines
include/net/nsh.h new file +10 -0
+/* NSH Base Header Length */
+#define NSH_BASE_HDR_LEN  8
+
+/* NSH MD Type 1 header Length. */
+#define NSH_M_TYPE1_LEN   24
+
+static inline u16
+nsh_hdr_len(const struct nsh_hdr *nsh)
+{
+	return 4 * (ntohs(nsh->ver_flags_len) & NSH_LEN_MASK) >> NSH_LEN_SHIFT;

This is doing the multiplication before the shift. It works only because the shift is 0.

Quoted 119 lines
include/net/nsh.h new file +15 -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;
+}
+
+#endif /* __NET_NSH_H */
include/uapi/linux/openvswitch.h +33 -0
index 156ee4c..5a1c94b 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,                  /* struct ovs_key_nsh */
 
 #ifdef __KERNEL__
 	OVS_KEY_ATTR_TUNNEL_INFO,  /* struct ip_tunnel_info */
@@ -491,6 +492,15 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
+struct ovs_key_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 np;
+	__u8 pad;
+	__be32 path_hdr;
+	__be32 c[4];
+};
+
 /**
  * enum ovs_flow_attr - attributes for %OVS_FLOW_* commands.
  * @OVS_FLOW_ATTR_KEY: Nested %OVS_KEY_ATTR_* attributes specifying the flow
@@ -769,6 +779,25 @@ struct ovs_action_push_eth {
 	struct ovs_key_ethernet addresses;
 };
 
+#define OVS_PUSH_NSH_MAX_MD_LEN 248
+/*
+ * struct ovs_action_push_nsh - %OVS_ACTION_ATTR_PUSH_NSH
+ * @flags: NSH header flags.
+ * @mdtype: NSH metadata type.
+ * @mdlen: Length of NSH metadata in bytes.
+ * @np: NSH next_protocol: Inner packet type.
+ * @path_hdr: NSH service path id and service index.
+ * @metadata: NSH metadata for MD type 1 or 2
+ */
+struct ovs_action_push_nsh {
+	__u8 flags;
+	__u8 mdtype;
+	__u8 mdlen;
+	__u8 np;
+	__be32 path_hdr;
+	__u8 metadata[];
+};
+
 /**
  * enum ovs_action_attr - Action types.
  *
@@ -806,6 +835,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 +866,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,     /* struct ovs_action_push_nsh. */
+	OVS_ACTION_ATTR_POP_NSH,      /* No argument. */
 
 	__OVS_ACTION_ATTR_MAX,	      /* Nothing past this will be accepted
 				       * from userspace. */
net/openvswitch/actions.c +17 -0
index e461067..50f80bc 100644
@@ -38,6 +38,7 @@
 #include <net/dsfield.h>
 #include <net/mpls.h>
 #include <net/sctp/checksum.h>
+#include <net/nsh.h>
 
 #include "datapath.h"
 #include "flow.h"
@@ -380,6 +381,114 @@ static int push_eth(struct sk_buff *skb, struct sw_flow_key *key,
 	return 0;
 }
 
+static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
+		    const struct ovs_action_push_nsh *oapn)
+{
+	struct nsh_hdr *nsh;
+	size_t length = NSH_BASE_HDR_LEN + oapn->mdlen;
+	u8 next_proto;
+
+	if (key->mac_proto == MAC_PROTO_ETHERNET) {
+		next_proto = NSH_P_ETHERNET;
+	} else {
+		switch (ntohs(skb->protocol)) {
+		case ETH_P_IP:
+			next_proto = NSH_P_IPV4;
+			break;
+		case ETH_P_IPV6:
+			next_proto = NSH_P_IPV6;

net/openvswitch/actions.c static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, · line 409

Quoted 10 lines
net/openvswitch/actions.c +9 -0
+			break;
+		case ETH_P_NSH:
+			next_proto = NSH_P_NSH;
+			break;
+		default:
+			return -ENOTSUPP;
+		}
+	}
+
 

I believe you need to validate that oapn->mdlen is a multiple of 4.

Quoted 10 lines
net/openvswitch/actions.c +10 -0
+	/* Add the NSH header */
+	if (skb_cow_head(skb, length) < 0)
+		return -ENOMEM;
+
+	skb_push(skb, length);
+	nsh = (struct nsh_hdr *)(skb->data);
+	nsh->ver_flags_len = htons((oapn->flags << NSH_FLAGS_SHIFT) |
+				 (length >> 2));
+	nsh->next_proto = next_proto;
+	nsh->path_hdr = oapn->path_hdr;

net/openvswitch/actions.c static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, · line 428

Quoted 10 lines
net/openvswitch/actions.c +10 -0
+	nsh->md_type = oapn->mdtype;
+	switch (nsh->md_type) {
+	case NSH_M_TYPE1:
+		nsh->md1 = *(struct nsh_md1_ctx *)oapn->metadata;
+		break;
+	case NSH_M_TYPE2: {
+		/* The MD2 metadata in oapn is already padded to 4 bytes. */
+		size_t len = DIV_ROUND_UP(oapn->mdlen, 4) * 4;
+
+		memcpy(nsh->md2, oapn->metadata, len);

I don't see any validation of oapn->mdlen. Normally this happens in __ovs_nla_copy_actions(). It will be made easier if you add a separate MD attribute as Jiri has suggested.

Quoted 20 lines
net/openvswitch/actions.c +20 -0
+		break;
+	}
+	default:
+		return -ENOTSUPP;
+	}
+
+	if (!skb->inner_protocol)
+		skb_set_inner_protocol(skb, skb->protocol);
+
+	skb->protocol = htons(ETH_P_NSH);
+	key->eth.type = htons(ETH_P_NSH);
+	skb_reset_mac_header(skb);
+	skb_reset_mac_len(skb);
+
+	/* safe right before invalidate_flow_key */
+	key->mac_proto = MAC_PROTO_NONE;
+	invalidate_flow_key(key);
+	return 0;
+}
+

[..]

#20 lore raw
Quoted 7 lines · Eric Garver

On Thu, Aug 10, 2017 at 09:21:15PM +0800, Yi Yang wrote:

Hi Yi,

In general I'd like to echo Jiri's comments on the netlink attributes. I'd like to see the metadata separate.

Quoted 6 lines

I have a few other comments below.

Thanks.
Eric.

[..]

Thanks Eric, I'm doing this and it is almost done, there is still an issue to fix.

Quoted 6 lines

include/net/nsh.h line 111

Quoted 2 lines
include/net/nsh.h new file +2 -0
+{
+	return 4 * (ntohs(nsh->ver_flags_len) & NSH_LEN_MASK) >> NSH_LEN_SHIFT;

This is doing the multiplication before the shift. It works only because the shift is 0.

Thank you for catching this, the right one:

((ntohs(nsh->ver_flags_len) & NSH_LEN_MASK) >> NSH_LEN_SHIFT) << 2

Quoted 20 lines
Quoted 20 lines
net/openvswitch/actions.c +20 -0
+static int push_nsh(struct sk_buff *skb, struct sw_flow_key *key,
+		    const struct ovs_action_push_nsh *oapn)
+{
+	struct nsh_hdr *nsh;
+	size_t length = NSH_BASE_HDR_LEN + oapn->mdlen;
+	u8 next_proto;
+
+	if (key->mac_proto == MAC_PROTO_ETHERNET) {
+		next_proto = NSH_P_ETHERNET;
+	} else {
+		switch (ntohs(skb->protocol)) {
+		case ETH_P_IP:
+			next_proto = NSH_P_IPV4;
+			break;
+		case ETH_P_IPV6:
+			next_proto = NSH_P_IPV6;
+			break;
+		case ETH_P_NSH:
+			next_proto = NSH_P_NSH;
+			break;
Quoted 9 lines

net/openvswitch/actions.c static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, · line 408

Quoted 6 lines
net/openvswitch/actions.c +5 -0
+		default:
+			return -ENOTSUPP;
+		}
+	}
+

I believe you need to validate that oapn->mdlen is a multiple of 4.

I'll add this check.

Quoted 5 lines
Quoted 5 lines
net/openvswitch/actions.c +5 -0
+	switch (nsh->md_type) {
+	case NSH_M_TYPE1:
+		nsh->md1 = *(struct nsh_md1_ctx *)oapn->metadata;
+		break;
+	case NSH_M_TYPE2: {
Quoted 9 lines

net/openvswitch/actions.c static int push_eth(struct sk_buff *skb, struct sw_flow_key *key, · line 428

Quoted 4 lines
net/openvswitch/actions.c +4 -0
+		/* The MD2 metadata in oapn is already padded to 4 bytes. */
+		size_t len = DIV_ROUND_UP(oapn->mdlen, 4) * 4;
+
+		memcpy(nsh->md2, oapn->metadata, len);

I don't see any validation of oapn->mdlen. Normally this happens in __ovs_nla_copy_actions(). It will be made easier if you add a separate MD attribute as Jiri has suggested.

Got it, will include this in next version.