From patchwork Wed Jan 24 19:06:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 865433 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="XWqJYG/f"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zRZQs53S7z9s9Y for ; Thu, 25 Jan 2018 06:06:45 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965103AbeAXTGn (ORCPT ); Wed, 24 Jan 2018 14:06:43 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:44026 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964999AbeAXTGg (ORCPT ); Wed, 24 Jan 2018 14:06:36 -0500 Received: by mail-pg0-f66.google.com with SMTP id n17so3344585pgf.10 for ; Wed, 24 Jan 2018 11:06:35 -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=NclNYuo7CQybMMDqkfMQyCcTeiMBm1ElJmXlCMi/En8=; b=XWqJYG/fojZIOoV1bPP4qSK8D2V0hTM4SregXJh+djjchfZ6pwCL1sqXjrOVWVdwZA gDO1ciy3pTi+af9sA/nfapH623W76mxVqIFJ5JHgus6Qc+LAZdEsVEB7VXOnmXm8IlNc YOD4EXoO3VYib9ex2ZP4g1ClUh4mz46/lXyxk0gCwAwA6r4Pl44HWlp5nW65yQQjnq6N rWjtADi2d1Bhg9QJvx4459PSNhgx2aB9vqABeiOdcB8fEvmPvGrcrO26/fLUE9HAk5yP iSd9j+4wPEKFnNERhPMBE144lORdA4yNCNLuKAMI21CyUR8sSqvV5MPcNYtUjHfHBa6M rxTg== 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=NclNYuo7CQybMMDqkfMQyCcTeiMBm1ElJmXlCMi/En8=; b=SaAC+CI3/OlSJ8ab2S4NwTyI4INpTJUGDCuEnQwB7Y/Nc57BdHcmDgQeXBAyxxYxaz gzM3+Iq0xq4H68KmWdyIqEoGX3Xaazwjo/LbhFy8feIdqBX3FWFED9PcPmKXdabi4B4d oEUs4TXlXgOjAUt6EItE7n3dp/r3nQhoedj5Z+np5887qgfQaRoqloKpmH6lSV6ca8FU 9xfTD8mbmeHaPHUMg//rahEJJ8VFECxpDJN5i8Dvr1M7+47h8srej0yUXhHWo8+0sUr2 1dW5oMTJCemijBRmyzkEoPWt8T9McbwWJ7rrZEmby6UghQUTbegyoWqoIKm08an1lOqk 9sSA== X-Gm-Message-State: AKwxytcWdk/hgejOtbA2obXCkbk/yCm8hOZfoKmu7HE65FuYUS8VXCDV IwE5XzGYnHsDsRtIcVBqACDamReT X-Google-Smtp-Source: AH8x225rOVk+T/PGNmezfynRQxr2VyEfbbi2bmbKouyioAQMec97BC1Ux3BU/XLox7650H0ckLK6NQ== X-Received: by 2002:a17:902:c81:: with SMTP id 1-v6mr9022847plt.281.1516820795248; Wed, 24 Jan 2018 11:06:35 -0800 (PST) Received: from sc9-mailhost2.vmware.com ([24.5.191.4]) by smtp.gmail.com with ESMTPSA id s80sm12075846pfj.179.2018.01.24.11.06.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 24 Jan 2018 11:06:34 -0800 (PST) From: William Tu To: netdev@vger.kernel.org Cc: pshelar@ovn.org Subject: [PATCHv5 net-next 2/2] openvswitch: add erspan version I and II support Date: Wed, 24 Jan 2018 11:06:16 -0800 Message-Id: <1516820776-7995-3-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516820776-7995-1-git-send-email-u9012063@gmail.com> References: <1516820776-7995-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 openvswitch to configure erspan v1 and v2. The OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS attr is added to uapi as a binary blob to support all ERSPAN v1 and v2's fields. Note that Previous commit "openvswitch: Add erspan tunnel support." was reverted since it does not design properly. Signed-off-by: William Tu --- include/uapi/linux/openvswitch.h | 2 +- net/openvswitch/flow_netlink.c | 90 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswitch.h index dcfab5e3b55c..158c2e45c0a5 100644 --- a/include/uapi/linux/openvswitch.h +++ b/include/uapi/linux/openvswitch.h @@ -273,7 +273,6 @@ enum { #define OVS_VXLAN_EXT_MAX (__OVS_VXLAN_EXT_MAX - 1) - /* OVS_VPORT_ATTR_OPTIONS attributes for tunnels. */ enum { @@ -363,6 +362,7 @@ 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, /* struct erspan_metadata */ __OVS_TUNNEL_KEY_ATTR_MAX }; diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c index f143908b651d..9d00c24b2836 100644 --- a/net/openvswitch/flow_netlink.c +++ b/net/openvswitch/flow_netlink.c @@ -49,6 +49,7 @@ #include #include #include +#include #include "flow_netlink.h" @@ -329,7 +330,8 @@ size_t ovs_tun_key_attr_size(void) + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */ + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */ - /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with + /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and + * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it. */ + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */ @@ -400,6 +402,7 @@ 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 = OVS_ATTR_VARIABLE }, }; static const struct ovs_len_tbl @@ -631,6 +634,33 @@ static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr, return 0; } +static int erspan_tun_opt_from_nlattr(const struct nlattr *a, + struct sw_flow_match *match, bool is_mask, + bool log) +{ + unsigned long opt_key_offset; + + BUILD_BUG_ON(sizeof(struct erspan_metadata) > + sizeof(match->key->tun_opts)); + + if (nla_len(a) > sizeof(match->key->tun_opts)) { + OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).", + nla_len(a), sizeof(match->key->tun_opts)); + return -EINVAL; + } + + if (!is_mask) + SW_FLOW_KEY_PUT(match, tun_opts_len, + sizeof(struct erspan_metadata), false); + else + SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true); + + opt_key_offset = TUN_METADATA_OFFSET(nla_len(a)); + SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a), + nla_len(a), is_mask); + return 0; +} + static int ip_tun_from_nlattr(const struct nlattr *attr, struct sw_flow_match *match, bool is_mask, bool log) @@ -738,6 +768,20 @@ static int ip_tun_from_nlattr(const struct nlattr *attr, break; case OVS_TUNNEL_KEY_ATTR_PAD: break; + case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: + if (opts_type) { + OVS_NLERR(log, "Multiple metadata blocks provided"); + return -EINVAL; + } + + err = erspan_tun_opt_from_nlattr(a, match, is_mask, + log); + if (err) + return err; + + tun_flags |= TUNNEL_ERSPAN_OPT; + opts_type = type; + break; default: OVS_NLERR(log, "Unknown IP tunnel attribute %d", type); @@ -862,6 +906,10 @@ static int __ip_tun_to_nlattr(struct sk_buff *skb, else if (output->tun_flags & TUNNEL_VXLAN_OPT && vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len)) return -EMSGSIZE; + else if (output->tun_flags & TUNNEL_ERSPAN_OPT && + nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS, + swkey_tun_opts_len, tun_opts)) + return -EMSGSIZE; } return 0; @@ -2461,6 +2509,41 @@ static int validate_geneve_opts(struct sw_flow_key *key) return 0; } +static int validate_erspan_opts(struct sw_flow_key *key, bool log) +{ + int opts_len = key->tun_opts_len; + struct erspan_metadata *md; + + md = (struct erspan_metadata *)TUN_METADATA_OPTS(key, opts_len); + if (md->version == 1) { + if (ntohl(md->u.index) & ~INDEX_MASK) { + OVS_NLERR(log, + "ERSPAN index number %x too large.", + ntohl(md->u.index)); + return -EINVAL; + } + } else if (md->version == 2) { + struct erspan_md2 *md2 = &md->u.md2; + u8 hwid = get_hwid(md2); + u8 dir = md2->dir; + + if (hwid & ~(HWID_MASK >> HWID_OFFSET)) { + OVS_NLERR(log, "ERSPAN hardware id %x invalid.", hwid); + return -EINVAL; + } + + if (dir != 0 && dir != 1) { + OVS_NLERR(log, "ERSPAN direction %d invalid.", dir); + return -EINVAL; + } + } else { + OVS_NLERR(log, "ERSPAN version %d invalid.", md->version); + return -EINVAL; + } + + return 0; +} + static int validate_and_copy_set_tun(const struct nlattr *attr, struct sw_flow_actions **sfa, bool log) { @@ -2486,6 +2569,11 @@ static int validate_and_copy_set_tun(const struct nlattr *attr, break; case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS: break; + case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS: + err = validate_erspan_opts(&key, log); + if (err < 0) + return err; + break; } };