openvswitch: enable NSH support
Subsystems (3)
PATCH v12 1/1 openvswitch: enable NSH support
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 | 119 +++++++++++++++ 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, 585 insertions(+), 1 deletion(-)
include/net/nsh.h +3 -0
@@ -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 skb_push_nsh (struct sk_buff *skb,const struct nshhdr *pushed_nh); +int skb_pop_nsh (struct sk_buff *skb); +#endif /* __NET_NSH_H */
include/uapi/linux/openvswitch.h +29 -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 ,/* 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
@@ -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 +119 -0
149 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
@@ -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
@@ -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
Quoted 19 lines · Yi Yang
net/openvswitch/actions.c +19
-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; + + err =nsh_key_from_nlattr (a, &key, &mask); +if (err) +return err; + +/* Make sure the NSH base header is there */ + err = skb_ensure_writable(skb, skb_network_offset(skb) +
net/openvswitch/actions.c static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, · line 671
Quoted 10 lines
net/openvswitch/actions.c +10
-0+ NSH_BASE_HDR_LEN); +if (unlikely (err)) +return err; + + nh =nsh_hdr (skb); + length =nsh_hdr_len (nh); + +/* Make sure the whole NSH header is there */ + err =skb_ensure_writable (skb,skb_network_offset (skb) + + length);
Calling skb_ensure_writable twice is an unnecessary waste in the fast path. If anything, the first call should be changed to pskb_may_pull.
But what we really should do here is to move the header only once. We know how much data we're going to write, we have everything stored in the key and can calculate it from there.
length = NSH_BASE_HDR_LEN;
switch (flow_key->nsh.base.mdtype) {
case NSH_MD_TYPE1:
length += sizeof(struct ovs_nsh_key_md1);
break;
case NSH_MD_TYPE2:
length += whatever_way_we_store_the_tlvs_in_flow_key;
break;
}
err = skb_ensure_writable(skb, skb_network_offset(skb) + length);However, set_nsh does not support MD type 2, thus the second case is a dead code. In both switches in this function. As such, it should be removed and added only when MD type 2 is introduced. I'd still keep the overall logic to ease the future addition, though. This boils down to:
length = NSH_BASE_HDR_LEN;
/* Assume MD type 1. This function cannot be called for anything
* else currently. When MD type 2 is added, the line below will
* have to be turned into a switch on flow_key->nsh.base.mdtype.
*/
length += sizeof(struct ovs_nsh_key_md1);
err = skb_ensure_writable(skb, skb_network_offset(skb) + length);
...
flow_key->nsh.base.path_hdr = nh->path_hdr;
/* Only MD type 1, see the comment above. */
for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) {
...Please verify I'm not missing something.
It seems we also rely on the user space checking first whether the packet is indeed compatible with the pushed key/mask. Most importantly, that it's of the same mdtype and (in the future) that the MD type 2 TLVs being written actually fit. Seems this is done the same way in the other set_* actions, thus fine with me.
Jiri
Quoted 34 lines · Jiri Benc
Quoted 19 lines · Yi Yang
net/openvswitch/actions.c +19
-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; + + err =nsh_key_from_nlattr (a, &key, &mask); +if (err) +return err; + +/* Make sure the NSH base header is there */ + err = skb_ensure_writable(skb, skb_network_offset(skb) +net/openvswitch/actions.c static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key, · line 671
Quoted 10 lines
net/openvswitch/actions.c +10
-0+ NSH_BASE_HDR_LEN); +if (unlikely (err)) +return err; + + nh =nsh_hdr (skb); + length =nsh_hdr_len (nh); + +/* Make sure the whole NSH header is there */ + err =skb_ensure_writable (skb,skb_network_offset (skb) + + length);Calling
skb_ensure_writabletwice is an unnecessary waste in the fast path. If anything, the first call should be changed topskb_may_pull.
Quoted 3 lines
But what we really should do here is to move the header only once. We know how much data we're going to write, we have everything stored in the key and can calculate it from there.
Actually mdtype can't be set, only push_nsh can set mdtype, so set_nsh won't have mdtype flow key, we can't get it from flow_key in set_nsh, only ttl, flags and path_hdr can be set in set_nsh as you can see in code. I understand your concern is calling skb_ensure_writable twice, so changing the first one to pskb_may_pull is more appropriate for set_nsh.
Quoted 39 lines
length = NSH_BASE_HDR_LEN; switch (flow_key->nsh.base.mdtype) { case NSH_MD_TYPE1: length += sizeof(struct ovs_nsh_key_md1); break; case NSH_MD_TYPE2: length += whatever_way_we_store_the_tlvs_in_flow_key; break; } err = skb_ensure_writable(skb, skb_network_offset(skb) + length);However,
set_nshdoes not support MD type 2, thus the second case is a dead code. In both switches in this function. As such, it should be removed and added only when MD type 2 is introduced. I'd still keep the overall logic to ease the future addition, though. This boils down to:length = NSH_BASE_HDR_LEN; /* Assume MD type 1. This function cannot be called for anything * else currently. When MD type 2 is added, the line below will * have to be turned into a switch on flow_key->nsh.base.mdtype. */ length += sizeof(struct ovs_nsh_key_md1); err = skb_ensure_writable(skb, skb_network_offset(skb) + length); ... flow_key->nsh.base.path_hdr = nh->path_hdr; /* Only MD type 1, see the comment above. */ for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++) { ...Please verify I'm not missing something.
It seems we also rely on the user space checking first whether the packet is indeed compatible with the pushed key/mask. Most importantly, that it's of the same mdtype and (in the future) that the MD type 2 TLVs being written actually fit. Seems this is done the same way in the other set_* actions, thus fine with me.
Jiri
Quoted 5 lines · Yi
Actually mdtype can't be set, only
push_nshcan set mdtype, soset_nshwon't have mdtype flow key, we can't get it fromflow_keyinset_nsh, only ttl, flags andpath_hdrcan be set inset_nshas you can see in code. I understand your concern is callingskb_ensure_writabletwice, so changing the first one topskb_may_pullis more appropriate forset_nsh.
Isn't set_nsh called only after the packet was already dissected (i.e. parse_nsh called)? The dissected fields should be available in flow_key.
Jiri
Quoted 9 lines · Jiri Benc
Quoted 5 lines · Yi
Actually mdtype can't be set, only
push_nshcan set mdtype, soset_nshwon't have mdtype flow key, we can't get it fromflow_keyinset_nsh, only ttl, flags andpath_hdrcan be set inset_nshas you can see in code. I understand your concern is callingskb_ensure_writabletwice, so changing the first one topskb_may_pullis more appropriate forset_nsh.Isn't
set_nshcalled only after the packet was already dissected (i.e.parse_nshcalled)? The dissected fields should be available inflow_key.
flow_key in set_nsh is got from netlink message which is set by commit_nsh in user space, here is code.
mask.mdtype = 0; /* Not writable. */
mask.np = 0; /* Not writable. */
if (commit_nsh(&base_flow->nsh, use_masked, &key, &base, &mask,
sizeof key, odp_actions)) {
put_nsh_key(&base, base_flow, false);
if (mask.mdtype != 0) { /* Mask was changed by commit(). */
put_nsh_key(&mask, &wc->masks, true);
}
}Here set is set with mask, so key value is masked by mask.mdtype
/* OVS_KEY_ATTR_NSH keys */ nsh_key_ofs = nl_msg_start_nested(odp_actions,OVS_KEY_ATTR_NSH);
/* put value and mask for OVS_NSH_KEY_ATTR_BASE */ char *data = nl_msg_put_unspec_uninit(odp_actions,
OVS_NSH_KEY_ATTR_BASE, 2 * sizeof(nsh_base));
const char *lkey = (char *)&nsh_base, *lmask = (char
*)&nsh_base_mask;
size_t lkey_size = sizeof(nsh_base);
while (lkey_size--) {
*data++ = *lkey++ & *lmask++;
}
lmask = (char *)&nsh_base_mask;
memcpy(data, lmask, sizeof(nsh_base_mask));Quoted 2 lines
Jiri
Quoted 2 lines · Yi
flow_keyinset_nshis got from netlink message which is set bycommit_nshin user space, here is code.
Isn't this the 'key' local variable that you're talking about, while I'm referring to the 'flow_key' parameter?
Jiri
Quoted 6 lines · Jiri Benc
Quoted 2 lines · Yi
flow_keyinset_nshis got from netlink message which is set bycommit_nshin user space, here is code.Isn't this the 'key' local variable that you're talking about, while I'm referring to the '
flow_key' parameter?
Oh, my mistake, but it is possible not to polulate nsh key in flow_key for push_nsh then set, as Jan and I explained before, we don't recirculate the packet after push_nsh for performance, so parse function isn't called for NSH header, mdtype can't be gotten from flow_key yet. Only one case is true, i.e. an ingress NSH packet is parsed then set by changing si and ttl.
For push_nsh, my typical use scinario is push_nsh then set then output to vxlangpe port.
Quoted 2 lines
Jiri
Quoted 2 lines · Yi
For
push_nsh, my typical use scinario ispush_nshthen set then output to vxlangpe port.
Okay.
Jiri
Quoted 5 lines · Jiri Benc
Quoted 2 lines · Yi
For
push_nsh, my typical use scinario ispush_nshthen set then output to vxlangpe port.Okay.
Then I just need to do the below change against v12.
net/openvswitch/actions.c +0 -0
@@ -658,10 +658,8 @@ static int set_nsh(struct sk_buff *skb, struct
sw_flow_key *flow_key,
return err;
/* Make sure the NSH base header is there */- err = skb_ensure_writable(skb, skb_network_offset(skb) +
- NSH_BASE_HDR_LEN);
- if (unlikely(err))
- return err;
+ if (!pskb_may_pull(skb, NSH_BASE_HDR_LEN))
+ return -ENOMEM;
nh = nsh_hdr(skb);
length = nsh_hdr_len(nh);I'll send out v13 with this if you're ok.
Quoted 2 lines
Jiri