From patchwork Wed Jan 10 01:51:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 857873 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nGsV7RmD"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zGX786tCNz9s7n for ; Wed, 10 Jan 2018 12:51:48 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933071AbeAJBvr (ORCPT ); Tue, 9 Jan 2018 20:51:47 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:35549 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933049AbeAJBvm (ORCPT ); Tue, 9 Jan 2018 20:51:42 -0500 Received: by mail-wm0-f68.google.com with SMTP id a79so24014356wma.0 for ; Tue, 09 Jan 2018 17:51:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QWb9ZkfQbRLSmg8zB8NcFeH8aiPQ7thJ5CWRaOjFmkY=; b=nGsV7RmDN8ssIKUKzDFa07ozvdQ++VrqntUOAXIhtX8Nh7dr4QpkKgIYuWbP09ZP+V 2QqUp/QB/55dGQHx9vBFG7UIv/db70IWFRDV4n/BaNpRIVtPChk8UlbLu1unk694AN9B +Yl7HLt3d1sYTxL7ZjvYK7vFV/nPbVhtVeCJ81B3AkdwZswUFtdELI5gXtKF266VZKn4 2jBFutGwiCdt613O6qeiHWj6PGDaNjKWe6z+FZb7jd1dWkpCYmDRe9xR281pVVVQ10yg JEG+SnUVpqeFQb0MZnGhu15JzU4pDbnIPLJwWeL099bwbEvEMljlSNzHmIgAEsItnhc4 EW4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QWb9ZkfQbRLSmg8zB8NcFeH8aiPQ7thJ5CWRaOjFmkY=; b=WC06KZmGlMInaldru8LDKRldgQeGHMoGm6dx12YGQlc4GHuczYo4kC4E6X9D4kHr2X HWvhJ68uz2PbuH9YSTkBCsI24Bp2w8rgBHMEpX9N09pY8Z/rOnszFNNjBq+hUqdCQsVd f9YX9Nw+kSoPzZYITXcI4S3x+8vbNJWvbp09nn/IQRxbAe+LSoymewd5AxwtaP+E3A9k mr/tq42z2/4VO6sprzHsiuytwOgAYhEN9CR7wBicFrPww/gzhZb/DM1n8f9qlEAufbOZ yAfQy/vNp0phFE0z35VpUfoBXbrcYuTuO0vh7DBAcQGF5FLFTh0ZHlJiaVkmA2qHZp+G 2dAg== X-Gm-Message-State: AKwxytezC67Gend0SiwIfX5ob1hxDzGBBoOONCnYZitnvpadpa+SUbkA KbhiP1fzuVEjG8fQjJPOqazbTUWN X-Google-Smtp-Source: ACJfBouE5P9JzQajHL0AC75UMmjZ5HOTQPv+ttxZsJO+3+D2c9uecVA5WGIxJiRqXo/ryKRNBdABhA== X-Received: by 10.80.171.21 with SMTP id s21mr4836374edc.117.1515549099867; Tue, 09 Jan 2018 17:51:39 -0800 (PST) Received: from sc9-mailhost1.vmware.com ([24.5.191.4]) by smtp.gmail.com with ESMTPSA id p37sm8277712eda.96.2018.01.09.17.51.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 09 Jan 2018 17:51:39 -0800 (PST) From: William Tu To: netdev@vger.kernel.org Cc: Pravin B Shelar Subject: [PATCHv2 net-next 2/2] openvswitch: add erspan version II support Date: Tue, 9 Jan 2018 17:51:22 -0800 Message-Id: <1515549082-4141-3-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1515549082-4141-1-git-send-email-u9012063@gmail.com> References: <1515549082-4141-1-git-send-email-u9012063@gmail.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The patch adds support for configuring the erspan V2 fields for openvswitch. For compatibility reason, the previously added attribute 'OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS' is renamed to 'OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTSV1' and deprecated, and the newly added attribute 'OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS' will handle both V1 and V2. Signed-off-by: William Tu Cc: Pravin B Shelar --- include/uapi/linux/openvswitch.h | 13 +++- net/openvswitch/flow_netlink.c | 129 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 132 insertions(+), 10 deletions(-) diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswitch.h index 4265d7f9e1f2..77c3424cc4ef 100644 --- a/include/uapi/linux/openvswitch.h +++ b/include/uapi/linux/openvswitch.h @@ -273,6 +273,16 @@ enum { #define OVS_VXLAN_EXT_MAX (__OVS_VXLAN_EXT_MAX - 1) +enum { + OVS_ERSPAN_OPT_UNSPEC, + OVS_ERSPAN_OPT_IDX, /* be32 index */ + OVS_ERSPAN_OPT_VER, /* u8 version number */ + OVS_ERSPAN_OPT_DIR, /* u8 direction */ + OVS_ERSPAN_OPT_HWID, /* u8 hardware ID */ + __OVS_ERSPAN_OPT_MAX, +}; + +#define OVS_ERSPAN_OPT_MAX (__OVS_ERSPAN_OPT_MAX - 1) /* OVS_VPORT_ATTR_OPTIONS attributes for tunnels. */ @@ -363,7 +373,8 @@ enum ovs_tunnel_key_attr { OVS_TUNNEL_KEY_ATTR_IPV6_SRC, /* struct in6_addr src IPv6 address. */ OVS_TUNNEL_KEY_ATTR_IPV6_DST, /* struct in6_addr dst IPv6 address. */ OVS_TUNNEL_KEY_ATTR_PAD, - OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, /* be32 ERSPAN index. */ + OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTSV1, /* be32 ERSPAN v1 index (deprecated). */ + OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, /* Nested OVS_ERSPAN_OPT_* */ __OVS_TUNNEL_KEY_ATTR_MAX }; diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c index bce1f78b0de5..9c6b210e7893 100644 --- a/net/openvswitch/flow_netlink.c +++ b/net/openvswitch/flow_netlink.c @@ -335,7 +335,10 @@ size_t ovs_tun_key_attr_size(void) */ + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */ + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_DST */ - + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS */ + + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTSV1 */ + /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with + * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it. + */ } static size_t ovs_nsh_key_attr_size(void) @@ -386,6 +389,13 @@ static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) }, }; +static const struct ovs_len_tbl ovs_erspan_opt_lens[OVS_ERSPAN_OPT_MAX + 1] = { + [OVS_ERSPAN_OPT_IDX] = { .len = sizeof(u32) }, + [OVS_ERSPAN_OPT_VER] = { .len = sizeof(u8) }, + [OVS_ERSPAN_OPT_DIR] = { .len = sizeof(u8) }, + [OVS_ERSPAN_OPT_HWID] = { .len = sizeof(u8) }, +}; + static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) }, [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) }, @@ -402,7 +412,9 @@ static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] .next = ovs_vxlan_ext_key_lens }, [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) }, - [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = sizeof(u32) }, + [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTSV1] = { .len = sizeof(u32) }, + [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_NESTED, + .next = ovs_erspan_opt_lens }, }; static const struct ovs_len_tbl @@ -640,16 +652,78 @@ static int erspan_tun_opt_from_nlattr(const struct nlattr *attr, { unsigned long opt_key_offset; struct erspan_metadata opts; + struct nlattr *a; + u16 hwid, dir; + int rem; BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts)); memset(&opts, 0, sizeof(opts)); - opts.u.index = nla_get_be32(attr); + nla_for_each_nested(a, attr, rem) { + int type = nla_type(a); + + if (type > OVS_ERSPAN_OPT_MAX) { + OVS_NLERR(log, "ERSPAN option %d out of range max %d", + type, OVS_ERSPAN_OPT_MAX); + return -EINVAL; + } + + if (!check_attr_len(nla_len(a), + ovs_erspan_opt_lens[type].len)) { + OVS_NLERR(log, "ERSPAN option %d has unexpected len %d expected %d", + type, nla_len(a), + ovs_erspan_opt_lens[type].len); + return -EINVAL; + } - /* Index has only 20-bit */ - if (ntohl(opts.u.index) & ~INDEX_MASK) { - OVS_NLERR(log, "ERSPAN index number %x too large.", - ntohl(opts.u.index)); + switch (type) { + case OVS_ERSPAN_OPT_IDX: + opts.u.index = nla_get_be32(a); + if (ntohl(opts.u.index) & ~INDEX_MASK) { + OVS_NLERR(log, + "ERSPAN index number %x too large.", + ntohl(opts.u.index)); + return -EINVAL; + } + break; + case OVS_ERSPAN_OPT_VER: + opts.version = nla_get_u8(a); + if (opts.version != 1 && opts.version != 2) { + OVS_NLERR(log, + "ERSPAN version %d not supported.", + opts.version); + return -EINVAL; + } + break; + case OVS_ERSPAN_OPT_DIR: + dir = nla_get_u8(a); + if (dir != 0 && dir != 1) { + OVS_NLERR(log, + "ERSPAN direction %d invalid.", + dir); + return -EINVAL; + } + opts.u.md2.dir = dir; + break; + case OVS_ERSPAN_OPT_HWID: + hwid = nla_get_u8(a); + if (hwid & ~(HWID_MASK >> HWID_OFFSET)) { + OVS_NLERR(log, + "ERSPAN hardware ID %x invalid.", + hwid); + return -EINVAL; + } + set_hwid(&opts.u.md2, hwid); + break; + default: + OVS_NLERR(log, "Unknown ERSPAN opt attribute %d", + type); + return -EINVAL; + } + } + if (rem) { + OVS_NLERR(log, "ERSPAN opt message has %d unknown bytes.", + rem); return -EINVAL; } @@ -768,6 +842,10 @@ static int ip_tun_from_nlattr(const struct nlattr *attr, break; case OVS_TUNNEL_KEY_ATTR_PAD: break; + case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTSV1: + OVS_NLERR(log, "ERSPAN attribute %d is deprecated.", + type); + return -EINVAL; case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: if (opts_type) { OVS_NLERR(log, "Multiple metadata blocks provided"); @@ -846,6 +924,39 @@ static int vxlan_opt_to_nlattr(struct sk_buff *skb, return 0; } +static int erspan_opt_to_nlattr(struct sk_buff *skb, + const void *tun_opts, int swkey_tun_opts_len) +{ + const struct erspan_metadata *opts = tun_opts; + struct nlattr *nla; + + nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS); + if (!nla) + return -EMSGSIZE; + + if (nla_put_u8(skb, OVS_ERSPAN_OPT_VER, opts->version) < 0) + return -EMSGSIZE; + + if (opts->version == 1) { + if (nla_put_be32(skb, OVS_ERSPAN_OPT_IDX, opts->u.index) < 0) + return -EMSGSIZE; + + } else if (opts->version == 2) { + if (nla_put_u8(skb, OVS_ERSPAN_OPT_DIR, + opts->u.md2.dir) < 0) + return -EMSGSIZE; + + if (nla_put_u8(skb, OVS_ERSPAN_OPT_HWID, + get_hwid(&opts->u.md2)) < 0) + return -EMSGSIZE; + } else { + return -EINVAL; + } + + nla_nest_end(skb, nla); + return 0; +} + static int __ip_tun_to_nlattr(struct sk_buff *skb, const struct ip_tunnel_key *output, const void *tun_opts, int swkey_tun_opts_len, @@ -906,8 +1017,8 @@ static int __ip_tun_to_nlattr(struct sk_buff *skb, vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len)) return -EMSGSIZE; else if (output->tun_flags & TUNNEL_ERSPAN_OPT && - nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, - ((struct erspan_metadata *)tun_opts)->u.index)) + erspan_opt_to_nlattr(skb, tun_opts, + swkey_tun_opts_len)) return -EMSGSIZE; }