Skip to contentSkip to conversation

openvswitch: enable NSH support

PATCH Superseded Yi Yang 5 participants 6 messages Oct 30, 2017 – Nov 1, 2017

Subsystems (3)

v14 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH v14 1/1 openvswitch: enable NSH support

v13->v14
- Rename skb_push_nsh to nsh_push per Dave's comment
- Rename skb_pop_nsh to nsh_pop per Dave's comment

v12->v13
- Fix NSH header length check in set_nsh

v11->v12
- Fix missing changes old comments pointed out
- Fix new comments for v11

v10->v11
- Fix the left three disputable comments for v9 but not fixed in v10.

v9->v10
- Change struct ovs_key_nsh to

       struct ovs_nsh_key_base base;
       __be32 context[NSH_MD1_CONTEXT_SIZE];

- Fix new comments for v9

v8->v9
- Fix build error reported by daily intel build because nsh module isn't selected by openvswitch

v7->v8
- Rework nested value and mask for OVS_KEY_ATTR_NSH
- Change pop_nsh to adapt to nsh kernel module
- Fix many issues per comments from Jiri Benc

v6->v7
- Remove NSH GSO patches in v6 because Jiri Benc reworked it as another patch series and they have been merged.
- Change it to adapt to nsh kernel module added by NSH GSO patch series

v5->v6
- Fix the rest comments for v4.
- Add NSH GSO support for VxLAN-gpe + NSH and Eth + NSH.

v4->v5
- Fix many comments by Jiri Benc and Eric Garver for v4.

v3->v4
- Add new NSH match field ttl
- Update NSH header to the latest format which will be final format and won't change per its author's confirmation.
- Fix comments for v3.

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 compat mode by porting this.

---
 include/net/nsh.h                |   3 +
 include/uapi/linux/openvswitch.h |  29 ++++
 net/nsh/nsh.c                    |  61 ++++++++
 net/openvswitch/Kconfig          |   1 +
 net/openvswitch/actions.c        | 117 +++++++++++++++
 net/openvswitch/flow.c           |  51 +++++++
 net/openvswitch/flow.h           |   7 +
 net/openvswitch/flow_netlink.c   | 310 ++++++++++++++++++++++++++++++++++++++-
 net/openvswitch/flow_netlink.h   |   5 +
 9 files changed, 583 insertions(+), 1 deletion(-)
include/net/nsh.h +3 -0
index a1eaea2..350b1ad 100644
@@ -304,4 +304,7 @@ static inline void nsh_set_flags_ttl_len(struct nshhdr *nsh, u8 flags,
 			NSH_FLAGS_MASK | NSH_TTL_MASK | NSH_LEN_MASK);
 }
 
+int nsh_push(struct sk_buff *skb, const struct nshhdr *pushed_nh);
+int nsh_pop(struct sk_buff *skb);
+
 #endif /* __NET_NSH_H */
include/uapi/linux/openvswitch.h +29 -0
index 0cd6f88..ac2623b 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 */
@@ -492,6 +493,30 @@ struct ovs_key_ct_tuple_ipv6 {
 	__u8   ipv6_proto;
 };
 
+enum ovs_nsh_key_attr {
+	OVS_NSH_KEY_ATTR_UNSPEC,
+	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 ttl;
+	__u8 mdtype;
+	__u8 np;
+	__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
@@ -808,6 +833,8 @@ struct ovs_action_push_eth {
  * @OVS_ACTION_ATTR_POP_ETH: Pop the outermost Ethernet header off the
  * packet.
  * @OVS_ACTION_ATTR_CT_CLEAR: Clear conntrack state from 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
@@ -838,6 +865,8 @@ enum ovs_action_attr {
 	OVS_ACTION_ATTR_PUSH_ETH,     /* struct ovs_action_push_eth. */
 	OVS_ACTION_ATTR_POP_ETH,      /* No argument. */
 	OVS_ACTION_ATTR_CT_CLEAR,     /* 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/nsh/nsh.c +61 -0

67 lines not on this page · raw message

net/openvswitch/Kconfig +1 -0
index ce94729..2650205 100644
@@ -14,6 +14,7 @@ config OPENVSWITCH
 	select MPLS
 	select NET_MPLS_GSO
 	select DST_CACHE
+	select NET_NSH
 	---help---
 	  Open vSwitch is a multilayer Ethernet switch targeted at virtualized
 	  environments.  In addition to supporting a variety of features
net/openvswitch/actions.c +117 -0

147 lines not on this page · raw message

net/openvswitch/flow.c +51 -0

69 lines not on this page · raw message

net/openvswitch/flow.h +7 -0
index 1875bba..8eeae749 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,11 @@ struct vlan_head {
 	(offsetof(struct sw_flow_key, recirc_id) +	\
 	FIELD_SIZEOF(struct sw_flow_key, recirc_id))
 
+struct ovs_key_nsh {
+	struct ovs_nsh_key_base base;
+	__be32 context[NSH_MD1_CONTEXT_SIZE];
+};
+
 struct sw_flow_key {
 	u8 tun_opts[IP_TUNNEL_OPTS_MAX];
 	u8 tun_opts_len;
@@ -144,6 +150,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 +309 -1

402 lines not on this page · raw message

net/openvswitch/flow_netlink.h +5 -0
index 929c665..6657606 100644
@@ -79,4 +79,9 @@ 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,
+			struct ovs_key_nsh *nsh_mask);
+int nsh_hdr_from_nlattr(const struct nlattr *attr, struct nshhdr *nh,
+			size_t size);
+
 #endif /* flow_netlink.h */
Signature · 1 line
2.5.5
#1 lore raw

On Mon, Oct 30, 2017 at 09:29:34AM +0800, Yi Yang wrote:
[...]

Quoted 5 lines
net/nsh/nsh.c +5 -0
+int nsh_pop(struct sk_buff *skb)
+{
+	struct nshhdr *nh;
+	size_t length;
+	__be16 inner_proto;

net/nsh/nsh.c line 63

Quoted 10 lines
net/nsh/nsh.c +10 -0
+
+	if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
+		return -ENOMEM;
+	nh = (struct nshhdr *)(skb->data);
+	length = nsh_hdr_len(nh);
+	if (!pskb_may_pull(skb, length))
+		return -ENOMEM;
+
+	nh = (struct nshhdr *)(skb->data);
+	inner_proto = tun_p_to_eth_p(nh->np);

If you fetch inner_proto before the second pskb_may_pull then there is no need to reload the nh pointer as you won't use it later.

net/nsh/nsh.c line 67

Quoted 4 lines
net/nsh/nsh.c +4 -0
+	if (!inner_proto)
+		return -EAFNOSUPPORT;
+
+	length = nsh_hdr_len(nh);

You already have the length from above. No need to get it again.

Quoted 9 lines
net/nsh/nsh.c +9 -0
+	skb_pull(skb, length);
+	skb_reset_mac_header(skb);
+	skb_reset_network_header(skb);
+	skb_reset_mac_len(skb);
+	skb->protocol = inner_proto;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nsh_pop);

[...]

Quoted 4 lines
net/openvswitch/flow_netlink.c +4 -0
+static int nsh_key_put_from_nlattr(const struct nlattr *attr,
+				   struct sw_flow_match *match, bool is_mask,
+				   bool is_push_nsh, bool log)
+{

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

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+	struct nlattr *a;
+	int rem;
+	bool has_base = false;
+	bool has_md1 = false;
+	bool has_md2 = false;
+	u8 mdtype = 0;
+	int mdlen = 0;
+
+	if (WARN_ON(is_push_nsh && is_mask))
+		return -EINVAL;

OVS_NLERR() is probably more appropriate.

Quoted 5 lines
net/openvswitch/flow_netlink.c +5 -0
+
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+		int i;
+

[...]

#2 lore raw
Quoted 5 lines · Eric Garver

Review

Quoted 2 lines · Yi Yang
(quoted diff) +2 -0
+	if (WARN_ON(is_push_nsh && is_mask))
+		return -EINVAL;

OVS_NLERR() is probably more appropriate.

No, not here. If this happens, it's a bug in the kernel and WARN_ON is what we need. This is not triggerable from user space and user space has no way to fix it if this happens.

Jiri

#4 lore raw
In reply to Yi Yang
Quoted 9 lines · Yi Yang
net/openvswitch/actions.c +9 -0
+static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key,
+		   const struct nlattr *a)
+{
+	struct nshhdr *nh;
+	size_t length;
+	int err;
+	u8 flags;
+	u8 ttl;
+	int i;

net/openvswitch/actions.c static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, · line 661

Quoted 10 lines
net/openvswitch/actions.c +10 -0
+
+	struct ovs_key_nsh key;
+	struct ovs_key_nsh mask;
+
+	err = nsh_key_from_nlattr(a, &key, &mask);
+	if (err)
+		return err;
+
+	/* Make sure the NSH base header is there */
+	if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))

This should be skb_network_offset(skb) + NSH_BASE_HDR_LEN.

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

Quoted 12 lines
net/openvswitch/flow_netlink.c +12 -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(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
+		/* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
+		 * mutually exclusive, so the bigger one can cover
+		 * the small one.
+		 *
+		 * OVS_NSH_KEY_ATTR_MD2
+		 */

A nit, not important but since you'll need to respin anyway: the last line in the comment above seems to be a left over from some previous version of the comment. This should be enough:

		/* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
		 * mutually exclusive, so the bigger one can cover
		 * the small one.
		 */

Or maybe I misunderstood what you meant.

Quoted 18 lines
net/openvswitch/flow_netlink.c +18 -0
+int nsh_hdr_from_nlattr(const struct nlattr *attr,
+			struct nshhdr *nh, size_t size)
+{
+	struct nlattr *a;
+	int rem;
+	u8 flags = 0;
+	u8 ttl = 0;
+	int mdlen = 0;
+
+	/* validate_nsh has check this, so we needn't do duplicate check here
+	 */
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base = nla_data(a);
+

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

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+			flags = base->flags;
+			ttl = base->ttl;
+			nh->np = base->np;
+			nh->mdtype = base->mdtype;
+			nh->path_hdr = base->path_hdr;
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD1:
+			mdlen = nla_len(a);
+			memcpy(&nh->md1, nla_data(a), mdlen);

The check for 'size' disappeared from here somehow.

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

Quoted 5 lines
net/openvswitch/flow_netlink.c +5 -0
+			break;
+
+		case OVS_NSH_KEY_ATTR_MD2:
+			mdlen = nla_len(a);
+			memcpy(&nh->md2, nla_data(a), mdlen);

And here.

Jiri

#3 lore raw
In reply to Eric Garver
Quoted 21 lines · Eric Garver

On Mon, Oct 30, 2017 at 09:29:34AM +0800, Yi Yang wrote:
[...]

Quoted 5 lines
net/nsh/nsh.c +5 -0
+int nsh_pop(struct sk_buff *skb)
+{
+	struct nshhdr *nh;
+	size_t length;
+	__be16 inner_proto;

net/nsh/nsh.c line 63

Quoted 10 lines
net/nsh/nsh.c +10 -0
+
+	if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
+		return -ENOMEM;
+	nh = (struct nshhdr *)(skb->data);
+	length = nsh_hdr_len(nh);
+	if (!pskb_may_pull(skb, length))
+		return -ENOMEM;
+
+	nh = (struct nshhdr *)(skb->data);
+	inner_proto = tun_p_to_eth_p(nh->np);

If you fetch inner_proto before the second pskb_may_pull then there is no need to reload the nh pointer as you won't use it later.

Quoted 6 lines

net/nsh/nsh.c line 67

Quoted 4 lines
net/nsh/nsh.c +4 -0
+	if (!inner_proto)
+		return -EAFNOSUPPORT;
+
+	length = nsh_hdr_len(nh);

You already have the length from above. No need to get it again.

Good catch, fixed in v15.

#5 lore raw
In reply to Jiri Benc
Quoted 14 lines · Jiri Benc
Quoted 13 lines · Yi Yang
net/openvswitch/actions.c +13 -0
+static int set_nsh(struct sk_buff *skb, struct sw_flow_key *flow_key,
+		   const struct nlattr *a)
+{
+	struct nshhdr *nh;
+	size_t length;
+	int err;
+	u8 flags;
+	u8 ttl;
+	int i;
+
+	struct ovs_key_nsh key;
+	struct ovs_key_nsh mask;
+
Quoted 9 lines

net/openvswitch/actions.c static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, · line 661

Quoted 6 lines
net/openvswitch/actions.c +6 -0
+	err = nsh_key_from_nlattr(a, &key, &mask);
+	if (err)
+		return err;
+
+	/* Make sure the NSH base header is there */
+	if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))

This should be skb_network_offset(skb) + NSH_BASE_HDR_LEN.

Fixed in v15.

Quoted 17 lines

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

Quoted 12 lines
net/openvswitch/flow_netlink.c +12 -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(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
+		/* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
+		 * mutually exclusive, so the bigger one can cover
+		 * the small one.
+		 *
+		 * OVS_NSH_KEY_ATTR_MD2
+		 */

A nit, not important but since you'll need to respin anyway: the last line in the comment above seems to be a left over from some previous version of the comment. This should be enough:

Quoted 7 lines
		/* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
		 * mutually exclusive, so the bigger one can cover
		 * the small one.
		 */

Or maybe I misunderstood what you meant.

Fixed it per the above one.

Quoted 31 lines
Quoted 18 lines
net/openvswitch/flow_netlink.c +18 -0
+int nsh_hdr_from_nlattr(const struct nlattr *attr,
+			struct nshhdr *nh, size_t size)
+{
+	struct nlattr *a;
+	int rem;
+	u8 flags = 0;
+	u8 ttl = 0;
+	int mdlen = 0;
+
+	/* validate_nsh has check this, so we needn't do duplicate check here
+	 */
+	nla_for_each_nested(a, attr, rem) {
+		int type = nla_type(a);
+
+		switch (type) {
+		case OVS_NSH_KEY_ATTR_BASE: {
+			const struct ovs_nsh_key_base *base = nla_data(a);
+

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

Quoted 10 lines
net/openvswitch/flow_netlink.c +10 -0
+			flags = base->flags;
+			ttl = base->ttl;
+			nh->np = base->np;
+			nh->mdtype = base->mdtype;
+			nh->path_hdr = base->path_hdr;
+			break;
+		}
+		case OVS_NSH_KEY_ATTR_MD1:
+			mdlen = nla_len(a);
+			memcpy(&nh->md1, nla_data(a), mdlen);

The check for 'size' disappeared from here somehow.

Quoted 7 lines

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

Quoted 5 lines
net/openvswitch/flow_netlink.c +5 -0
+			break;
+
+		case OVS_NSH_KEY_ATTR_MD2:
+			mdlen = nla_len(a);
+			memcpy(&nh->md2, nla_data(a), mdlen);

And here.

validate_nsh checked netlink attributes but can't check size, yes, we should add size check for mdlen, v15 has had them. Please check v15, thanks a lot.