From patchwork Thu Mar 16 22:10:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Garver X-Patchwork-Id: 740044 X-Patchwork-Delegate: joestringer@nicira.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vkjQZ5VF9z9ryQ for ; Fri, 17 Mar 2017 09:12:54 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id B477BC47; Thu, 16 Mar 2017 22:10:29 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 1DDB5C0D for ; Thu, 16 Mar 2017 22:10:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 724662A2 for ; Thu, 16 Mar 2017 22:10:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E3CD880471 for ; Thu, 16 Mar 2017 22:10:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E3CD880471 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=erig.me Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=none smtp.mailfrom=e@erig.me DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com E3CD880471 Received: from wsfd-netdev-buildsys.ntdv.lab.eng.bos.redhat.com (wsfd-netdev-buildsys.ntdv.lab.eng.bos.redhat.com [10.19.17.61]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E44A183FA for ; Thu, 16 Mar 2017 22:10:24 +0000 (UTC) From: Eric Garver To: dev@openvswitch.org Date: Thu, 16 Mar 2017 18:10:18 -0400 Message-Id: <20170316221021.11149-5-e@erig.me> In-Reply-To: <20170316221021.11149-1-e@erig.me> References: <20170316221021.11149-1-e@erig.me> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 16 Mar 2017 22:10:25 +0000 (UTC) X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v2 4/7] dpif-netlink-rtnl: add VXLAN creation support X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Creates VXLAN devices using rtnetlink and tunnel metadata. Co-Authored-by: Thadeu Lima de Souza Cascardo Signed-off-by: Thadeu Lima de Souza Cascardo Signed-off-by: Eric Garver --- lib/dpif-netlink-rtnl.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++- lib/dpif-netlink-rtnl.h | 3 +- 2 files changed, 175 insertions(+), 3 deletions(-) diff --git a/lib/dpif-netlink-rtnl.c b/lib/dpif-netlink-rtnl.c index 1f816feee569..c07d7df8d13a 100644 --- a/lib/dpif-netlink-rtnl.c +++ b/lib/dpif-netlink-rtnl.c @@ -17,14 +17,186 @@ #include #include "dpif-netlink-rtnl.h" + +#include +#include +#include + #include "dpif-netlink.h" +#include "netdev-vport.h" +#include "netlink-socket.h" + +/* + * On some older systems, these enums are not defined. + */ +#ifndef IFLA_VXLAN_MAX +#define IFLA_VXLAN_MAX 0 +#endif +#if IFLA_VXLAN_MAX < 25 +#define IFLA_VXLAN_LEARNING 7 +#define IFLA_VXLAN_PORT 15 +#define IFLA_VXLAN_UDP_ZERO_CSUM6_RX 20 +#define IFLA_VXLAN_GBP 23 +#define IFLA_VXLAN_COLLECT_METADATA 25 +#endif + +static const struct nl_policy rtlink_policy[] = { + [IFLA_LINKINFO] = { .type = NL_A_NESTED }, +}; +static const struct nl_policy linkinfo_policy[] = { + [IFLA_INFO_KIND] = { .type = NL_A_STRING }, + [IFLA_INFO_DATA] = { .type = NL_A_NESTED }, +}; + + +static int +dpif_netlink_rtnl_destroy(const char *name) +{ + int err; + struct ofpbuf request; + + ofpbuf_init(&request, 0); + nl_msg_put_nlmsghdr(&request, 0, RTM_DELLINK, NLM_F_REQUEST | NLM_F_ACK); + ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg)); + nl_msg_put_string(&request, IFLA_IFNAME, name); + + err = nl_transact(NETLINK_ROUTE, &request, NULL); + ofpbuf_uninit(&request); + + return err; +} + +static int +dpif_netlink_rtnl_vxlan_destroy(const char *name) +{ + return dpif_netlink_rtnl_destroy(name); +} + +static int +dpif_netlink_rtnl_vxlan_verify(struct netdev *netdev, const char *name, + const char *kind) +{ + int err; + struct ofpbuf request, *reply; + struct ifinfomsg *ifmsg; + const struct netdev_tunnel_config *tnl_cfg; + + static const struct nl_policy vxlan_policy[] = { + [IFLA_VXLAN_COLLECT_METADATA] = { .type = NL_A_U8 }, + [IFLA_VXLAN_LEARNING] = { .type = NL_A_U8 }, + [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NL_A_U8 }, + [IFLA_VXLAN_PORT] = { .type = NL_A_U16 }, + }; + + tnl_cfg = netdev_get_tunnel_config(netdev); + if (!tnl_cfg) { + return EINVAL; + } + ofpbuf_init(&request, 0); + nl_msg_put_nlmsghdr(&request, 0, RTM_GETLINK, NLM_F_REQUEST); + ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg)); + nl_msg_put_string(&request, IFLA_IFNAME, name); + + err = nl_transact(NETLINK_ROUTE, &request, &reply); + ofpbuf_uninit(&request); + + if (!err) { + struct nlattr *rtlink[ARRAY_SIZE(rtlink_policy)]; + struct nlattr *linkinfo[ARRAY_SIZE(linkinfo_policy)]; + struct nlattr *vxlan[ARRAY_SIZE(vxlan_policy)]; + + ifmsg = ofpbuf_at(reply, NLMSG_HDRLEN, sizeof *ifmsg); + if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *ifmsg, + rtlink_policy, rtlink, ARRAY_SIZE(rtlink_policy)) + || !nl_parse_nested(rtlink[IFLA_LINKINFO], linkinfo_policy, + linkinfo, ARRAY_SIZE(linkinfo_policy)) + || strcmp(nl_attr_get_string(linkinfo[IFLA_INFO_KIND]), kind) + || !nl_parse_nested(linkinfo[IFLA_INFO_DATA], vxlan_policy, vxlan, + ARRAY_SIZE(vxlan_policy))) { + err = EINVAL; + } + if (!err) { + if (0 != nl_attr_get_u8(vxlan[IFLA_VXLAN_LEARNING]) + || 1 != nl_attr_get_u8(vxlan[IFLA_VXLAN_COLLECT_METADATA]) + || 1 != nl_attr_get_u8(vxlan[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) + || (tnl_cfg->dst_port != + nl_attr_get_be16(vxlan[IFLA_VXLAN_PORT]))) { + err = EINVAL; + } + } + if (!err) { + if (tnl_cfg->exts & (1 << OVS_VXLAN_EXT_GBP) + && !nl_attr_get_flag(vxlan[IFLA_VXLAN_GBP])) { + err = EINVAL; + } + } + ofpbuf_delete(reply); + } + + return err; +} + +static int +dpif_netlink_rtnl_vxlan_create_kind(struct netdev *netdev, const char *kind) +{ + int err; + struct ofpbuf request; + size_t linkinfo_off, infodata_off; + char namebuf[NETDEV_VPORT_NAME_BUFSIZE]; + const char *name = netdev_vport_get_dpif_port(netdev, + namebuf, sizeof namebuf); + struct ifinfomsg *ifinfo; + const struct netdev_tunnel_config *tnl_cfg; + tnl_cfg = netdev_get_tunnel_config(netdev); + if (!tnl_cfg) { + return EINVAL; + } + + ofpbuf_init(&request, 0); + nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK, + NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE); + ifinfo = ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg)); + ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP; + nl_msg_put_string(&request, IFLA_IFNAME, name); + nl_msg_put_u32(&request, IFLA_MTU, UINT16_MAX); + linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO); + nl_msg_put_string(&request, IFLA_INFO_KIND, kind); + infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA); + + nl_msg_put_u8(&request, IFLA_VXLAN_LEARNING, 0); + nl_msg_put_u8(&request, IFLA_VXLAN_COLLECT_METADATA, 1); + nl_msg_put_u8(&request, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, 1); + if (tnl_cfg->exts & (1 << OVS_VXLAN_EXT_GBP)) { + nl_msg_put_flag(&request, IFLA_VXLAN_GBP); + } + nl_msg_put_be16(&request, IFLA_VXLAN_PORT, tnl_cfg->dst_port); + + nl_msg_end_nested(&request, infodata_off); + nl_msg_end_nested(&request, linkinfo_off); + + err = nl_transact(NETLINK_ROUTE, &request, NULL); + ofpbuf_uninit(&request); + + if (!err && (err = dpif_netlink_rtnl_vxlan_verify(netdev, name, kind))) { + dpif_netlink_rtnl_vxlan_destroy(name); + } + + return err; +} + +static int +dpif_netlink_rtnl_vxlan_create(struct netdev *netdev) +{ + return dpif_netlink_rtnl_vxlan_create_kind(netdev, "vxlan"); +} int dpif_netlink_rtnl_port_create(struct netdev *netdev) { switch (netdev_to_ovs_vport_type(netdev_get_type(netdev))) { case OVS_VPORT_TYPE_VXLAN: + return dpif_netlink_rtnl_vxlan_create(netdev); case OVS_VPORT_TYPE_GRE: case OVS_VPORT_TYPE_GENEVE: case OVS_VPORT_TYPE_NETDEV: @@ -40,10 +212,11 @@ dpif_netlink_rtnl_port_create(struct netdev *netdev) } int -dpif_netlink_rtnl_port_destroy(const char *name OVS_UNUSED, const char *type) +dpif_netlink_rtnl_port_destroy(const char *name, const char *type) { switch (netdev_to_ovs_vport_type(type)) { case OVS_VPORT_TYPE_VXLAN: + return dpif_netlink_rtnl_vxlan_destroy(name); case OVS_VPORT_TYPE_GRE: case OVS_VPORT_TYPE_GENEVE: case OVS_VPORT_TYPE_NETDEV: diff --git a/lib/dpif-netlink-rtnl.h b/lib/dpif-netlink-rtnl.h index 5fef314a20f6..952c0d4187e5 100644 --- a/lib/dpif-netlink-rtnl.h +++ b/lib/dpif-netlink-rtnl.h @@ -23,8 +23,7 @@ /* Declare these to keep sparse happy. */ int dpif_netlink_rtnl_port_create(struct netdev *netdev); -int dpif_netlink_rtnl_port_destroy(const char *name OVS_UNUSED, - const char *type); +int dpif_netlink_rtnl_port_destroy(const char *name, const char *type); #ifndef __linux__ /* Dummy implementations for non Linux builds. */