From patchwork Thu Jan 18 22:04:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 863188 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="oWlKYg40"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3zMygh2hQdz9t66 for ; Fri, 19 Jan 2018 09:05:20 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753986AbeARWFS (ORCPT ); Thu, 18 Jan 2018 17:05:18 -0500 Received: from mail-oi0-f67.google.com ([209.85.218.67]:41271 "EHLO mail-oi0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932447AbeARWFI (ORCPT ); Thu, 18 Jan 2018 17:05:08 -0500 Received: by mail-oi0-f67.google.com with SMTP id n81so16802901oig.8 for ; Thu, 18 Jan 2018 14:05:08 -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=yLFNBKe3uFim7W75gOfD5AsXjtichQp/S5WSzyBnn68=; b=oWlKYg40SfCPSuvrKdaf3dug8ruCxjk5QuFiR98Xqf5rdwqdGCG25SxrxNL/aDw8jQ XzV1y7xv0tsyNOcDsETgsdfKZ6XJNE8+0zP5xiOI8HYjTOop/CR6ElZksHFroyAG6P1b YgTGHBuui1JXcXXmG3iUcbREYc+nBy33p+ZoGXJHQXQQD+EFBpH5vgBb4+A3EUoGaFog STcBlytzb8KEKx5QS0mR4M9pZvMg3FxVv+tlyiItRtUg5jWXbsBoveY1eVMFEnSA1/2/ KQg6ZqaOZEdfxU56Tr7iRk2bphasU4Mu9I1GrixuxBJhP188Yv6twYvtUfmnVttb6tIx ABkw== 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=yLFNBKe3uFim7W75gOfD5AsXjtichQp/S5WSzyBnn68=; b=mT/79oj3KvB4i7kxC0Q4Sp/Zhz0xe/FKq3K2wz61UjyJhq90qXRGM8YVIq8QvwBbw1 xaRHz4VTOW+Ccxw4Eb31IsyHORp/i0ND8qNWHBeiil/k5oITx95FwNXDAXD1wFv1zcG6 HUWUetJiOPla7G9TDmYis+mHdDWpeFR/JD2ZGpmQaaPJ5M06txpvnKXdMW7kEMIFM6cj ae+426LHTGPx/+9jfr0J8+HC3kbE37mE8XcmuFhlknhyZJNTMwY++E7dOImeVrFig4Th nMgVgJRrQyCZqR0GE7WFKGG7K7FEufGYTrZoYz7WEE+zm3aACzvz6DayXxZ96KZJyoBX VyuA== X-Gm-Message-State: AKwxytfVuu+eCrmRqJHhLNxKHF+WMQrOYFfIsVNgijzRL8+d4c/6/hYu R1vua6Z/DKHvuEvmmNYL2XfFD/4j X-Google-Smtp-Source: ACJfBovJQS6TMVc29+UD1mpwTuGm6G7BMWm7p8wf1P00OWeuEaKUW35a4T2mvBLEGGN/424P1+6oOw== X-Received: by 10.202.60.134 with SMTP id j128mr4258716oia.268.1516313107278; Thu, 18 Jan 2018 14:05:07 -0800 (PST) Received: from sc9-mailhost3.vmware.com ([24.5.191.4]) by smtp.gmail.com with ESMTPSA id 66sm1979549ots.17.2018.01.18.14.05.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Jan 2018 14:05:06 -0800 (PST) From: William Tu To: netdev@vger.kernel.org Cc: pshelar@ovn.org, jbenc@redhat.com Subject: [PATCHv4 net-next 2/2] openvswitch: add erspan version I and II support Date: Thu, 18 Jan 2018 14:04:56 -0800 Message-Id: <1516313096-3971-3-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516313096-3971-1-git-send-email-u9012063@gmail.com> References: <1516313096-3971-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 nested attribute to support all ERSPAN v1 and v2's fields. Note taht 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 | 11 +++ net/openvswitch/flow_netlink.c | 154 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswitch.h index dcfab5e3b55c..f1f98fd703fe 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, /* u32 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,6 +373,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, /* Nested OVS_ERSPAN_OPT_* */ __OVS_TUNNEL_KEY_ATTR_MAX }; diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c index f143908b651d..c57b96b595b5 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 */ @@ -384,6 +386,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) }, @@ -400,6 +409,8 @@ 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_NESTED, + .next = ovs_erspan_opt_lens }, }; static const struct ovs_len_tbl @@ -631,6 +642,94 @@ static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr, return 0; } +static int erspan_tun_opt_from_nlattr(const struct nlattr *attr, + struct sw_flow_match *match, bool is_mask, + bool log) +{ + 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)); + 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; + } + + switch (type) { + case OVS_ERSPAN_OPT_IDX: + opts.u.index = htonl(nla_get_u32(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; + } + + SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), is_mask); + opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts)); + SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts), + 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 +837,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); @@ -803,6 +916,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_u32(skb, OVS_ERSPAN_OPT_IDX, ntohl(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, @@ -862,6 +1008,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 && + erspan_opt_to_nlattr(skb, tun_opts, + swkey_tun_opts_len)) + return -EMSGSIZE; } return 0; @@ -2486,6 +2636,8 @@ 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: + break; } };