From patchwork Thu Mar 17 01:12:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Mi X-Patchwork-Id: 1606339 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=HH1nlxF1; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KJq0D2MHcz9sCD for ; Thu, 17 Mar 2022 12:13:32 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 3E70380C38; Thu, 17 Mar 2022 01:13:30 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id fKR5G2iFSydH; Thu, 17 Mar 2022 01:13:26 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 683AA8477A; Thu, 17 Mar 2022 01:13:22 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 3406CC0012; Thu, 17 Mar 2022 01:13:21 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 10F9FC008B for ; Thu, 17 Mar 2022 01:13:20 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 2454E8475A for ; Thu, 17 Mar 2022 01:13:13 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DGK60rG7Ir_U for ; Thu, 17 Mar 2022 01:13:11 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.8.0 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on20627.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eaa::627]) by smtp1.osuosl.org (Postfix) with ESMTPS id A4E7684752 for ; Thu, 17 Mar 2022 01:13:11 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c6O6NkWb/9hueYsg4XPdOuVfVEZeIP5nj3uwwcpGZaFenVv37RF5T5D8C6N7yYO8ec0NdeoT/5QmW78/E9XnaEpdctpDFPDdrWU4G2Zk3OvJt5exLbjxmQsFNGUkyabsN0FTHTWMv7lk1pQmQLI1tcy4GIn6WQgOejyeH9ph0XUNCTL//AD+Cv+9N8prylPsy2kscH0btlCzgyB4HTuVrI+vooe2w0z4RgvXcIcdnLeSkxf+rJM/To8VanZ07Sw1/tu7ypAIUEHKn8UZCPDvzrv8UHHeRGEyZGWGxcxyGH37ryB+1Sru/4CQ6B4qT0QPpWfLDQvr0u4NOloeA1zgGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DxaRH0p0TJNSwQbjiSAsK32e5srXD2ZCAaxnuoh16UA=; b=K7iOgaAoSXnDi0xGtzwREFjBiP0SlJVfeoAJj1O1Ge7OO2cIoJotILEJeYFkNGnD4oBeC6yhPCHmaziWmRpkxtkKzVOBvRK5qC2FW/0x+PJrgqM7a8oAe9H4h6tnHZQ9HTYBSJzyJq+3ciG6Ggt/NmAderWLJTwCR8su+bcKgTI6EZSaNGJTSnR7fAayWcHn719fsvuhAnZ3ANEFuAjriXqL4ChBdFcK9aAxz6VOhUoh6uC5O0gCQ2qNo5ysRS9MaNnEAAj6aao+LLbaelkjk4iwcvuPaHHN/NNwB10eRPd6acKf1d5REEYKqA7EX/OaufH3mNZLcriXJ5AymmRsvg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=ovn.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DxaRH0p0TJNSwQbjiSAsK32e5srXD2ZCAaxnuoh16UA=; b=HH1nlxF163G+RacX28qdCSjprDiuNaBVj4OYWkAYkC5qbOoQYSjG6vIQm66FHM6MKeAKm9vAbP2LAj1QTtQp5MWkRmepzecKNnLjpMAFlBu77Gwyddpp9ljWxRSClaoeyqD1CSmt8I+yK0qpoKsul3yzgSRclCPmRQa8z50bLbwFN8Sn6C443T5Gwo4pYrdU9QoaaZ7qKC4r2S06U9YYaoW+uYPC2hTLdj8vLClycyLi7Ho3OdgRxVKY7/dZT/hXerSY4U5YLXl1n20n9IlAOLj8ISrAnFVDbuN+NgLXf1Im5/V3ZkuohuqUclA5mtVZTFo9sga//CLBrqPPTa4CBw== Received: from BN0PR04CA0142.namprd04.prod.outlook.com (2603:10b6:408:ed::27) by DM6PR12MB3900.namprd12.prod.outlook.com (2603:10b6:5:1c9::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5081.15; Thu, 17 Mar 2022 01:13:08 +0000 Received: from BN8NAM11FT065.eop-nam11.prod.protection.outlook.com (2603:10b6:408:ed:cafe::cc) by BN0PR04CA0142.outlook.office365.com (2603:10b6:408:ed::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5061.22 via Frontend Transport; Thu, 17 Mar 2022 01:13:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.238) by BN8NAM11FT065.mail.protection.outlook.com (10.13.177.63) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5081.14 via Frontend Transport; Thu, 17 Mar 2022 01:13:08 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Thu, 17 Mar 2022 01:13:07 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 16 Mar 2022 18:13:06 -0700 Received: from dev-r630-03.lab.mtl.com (10.127.8.12) by mail.nvidia.com (10.129.68.9) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Wed, 16 Mar 2022 18:13:04 -0700 To: Date: Thu, 17 Mar 2022 09:12:46 +0800 Message-ID: <20220317011249.46787-6-cmi@nvidia.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220317011249.46787-1-cmi@nvidia.com> References: <20220317011249.46787-1-cmi@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1dd76d4b-10fb-4abe-a5b5-08da07b34c27 X-MS-TrafficTypeDiagnostic: DM6PR12MB3900:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: wk/ZFE0n2nGyqKzjgZieaEtl/cZiFokyUqByugxWAHlP6QnplrjW/gPSEjsQY4CgD5UbvjByvKy2EfjdkH/gW8Tp23o6liMNlFty/Glr1wDExxaixD5UFWFUFQq77D5tCpoh9Iip3fZqUVew6X6djdfe9/aU6vFG0esMuH3lyoEWQ+raSzuVZSTf7iLJvCfJUGcKuWOGE7w2sbLK53MLiAVhIRwidZ/Y0Z1bQ78bjmSucffEDo1a2YNVwievp/1sMfLZO9Lvbdx45C5H+YNtaGJpficvqOADYGaz818NTCx6fqkEZeytomhOPT216HDBGGJxUhx/t2XhPfAPS+qJQDL5QG2JbKsYEWb6d5r4LadyeYqxGgFTkmF2ejoYPKz9ogLhEY53lRTUBCvdwLsw+FxpN3X5bv/KZ8rxO5vIkhdDPY3cPMNW6+eDlqUn5tRYcMTaQAmczB6zuIoyrLjP65CEZy0DqowNiu9nZXOt/gft8A0CHFrH0rou0ev9b4kX3fP9D3jH7uqtNp6EtW9oKX7FKA/kz3TDNU6gw0PZJYWd87wp1gfE2gcpYtxbAkFaVZwaMy9xuHB7XVi+hn3K7FjombDyngUmkxbluqZS77g23I/jdlxeR+1niQxih965jepqdm1e7k/XYBO9h+rDaDxMup6Qo1dAvVKZCZrHRp/0yccYcltcGy/PG587YLHODKH6DoKnH+K23jGoJh66CUL2/3srmhHZ6nRQX42ITKML5BoEqfOVlerdgK311NORPZ63ZSicLWxgJRLtsGoy0pTsC4Tk+bIpmgh33NXsjnw= X-Forefront-Antispam-Report: CIP:12.22.5.238; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:mail.nvidia.com; PTR:InfoNoRecords; CAT:NONE; SFS:(13230001)(4636009)(36840700001)(46966006)(40470700004)(1076003)(2616005)(426003)(86362001)(186003)(336012)(26005)(36860700001)(70206006)(70586007)(4326008)(8676002)(107886003)(47076005)(82310400004)(83380400001)(5660300002)(356005)(508600001)(966005)(30864003)(81166007)(8936002)(40460700003)(6916009)(6666004)(54906003)(316002)(2906002)(36756003)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Mar 2022 01:13:08.2209 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1dd76d4b-10fb-4abe-a5b5-08da07b34c27 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[12.22.5.238]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT065.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3900 Cc: elibr@nvidia.com, simon.horman@netronome.com, roniba@nvidia.com, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v21 5/8] dpif-offload-netlink: Implement dpif-offload-provider API X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Chris Mi via dev From: Chris Mi Reply-To: Chris Mi Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Implement dpif-offload API for netlink datapath. Signed-off-by: Chris Mi Reviewed-by: Eli Britstein --- lib/automake.mk | 1 + lib/dpif-netdev.c | 3 +- lib/dpif-netlink.c | 20 +++- lib/dpif-offload-netlink.c | 221 ++++++++++++++++++++++++++++++++++++ lib/dpif-offload-provider.h | 25 +++- lib/dpif-offload.c | 104 +++++++++++++++++ lib/dpif-provider.h | 6 +- lib/dpif.c | 3 +- 8 files changed, 375 insertions(+), 8 deletions(-) create mode 100644 lib/dpif-offload-netlink.c diff --git a/lib/automake.mk b/lib/automake.mk index 781fba47a..b4cff234a 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -453,6 +453,7 @@ lib_libopenvswitch_la_SOURCES += \ lib/dpif-netlink.h \ lib/dpif-netlink-rtnl.c \ lib/dpif-netlink-rtnl.h \ + lib/dpif-offload-netlink.c \ lib/if-notifier.c \ lib/netdev-linux.c \ lib/netdev-linux.h \ diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 9f35713ef..b0ace7f1e 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -1684,7 +1684,8 @@ create_dpif_netdev(struct dp_netdev *dp) ovs_refcount_ref(&dp->ref_cnt); dpif = xmalloc(sizeof *dpif); - dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id); + dpif_init(&dpif->dpif, dp->class, NULL, dp->name, netflow_id >> 8, + netflow_id); dpif->dp = dp; dpif->last_port_seq = seq_read(dp->port_seq); diff --git a/lib/dpif-netlink.c b/lib/dpif-netlink.c index 71e35ccdd..f00159063 100644 --- a/lib/dpif-netlink.c +++ b/lib/dpif-netlink.c @@ -455,20 +455,33 @@ dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name, static int open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp) { + struct registered_dpif_offload_class *registered_offload_class; + const char *type = dpif_netlink_class.type; struct dpif_netlink *dpif; + int error = 0; dpif = xzalloc(sizeof *dpif); dpif->port_notifier = NULL; fat_rwlock_init(&dpif->upcall_lock); - dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name, - dp->dp_ifindex, dp->dp_ifindex); + dp_offload_initialize(); + registered_offload_class = dp_offload_class_lookup(type); + if (!registered_offload_class) { + VLOG_WARN("Could not find offload class for type %s", type); + error = EAFNOSUPPORT; + goto exit; + } + + dpif_init(&dpif->dpif, &dpif_netlink_class, + registered_offload_class->offload_class, + dp->name, dp->dp_ifindex, dp->dp_ifindex); dpif->dp_ifindex = dp->dp_ifindex; dpif->user_features = dp->user_features; *dpifp = &dpif->dpif; - return 0; +exit: + return error; } #ifdef _WIN32 @@ -713,6 +726,7 @@ dpif_netlink_close(struct dpif *dpif_) { struct dpif_netlink *dpif = dpif_netlink_cast(dpif_); + dpif_offload_close(dpif_); nl_sock_destroy(dpif->port_notifier); fat_rwlock_wrlock(&dpif->upcall_lock); diff --git a/lib/dpif-offload-netlink.c b/lib/dpif-offload-netlink.c new file mode 100644 index 000000000..02aea7e2d --- /dev/null +++ b/lib/dpif-offload-netlink.c @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "dpif-offload-provider.h" +#include "netdev-offload.h" +#include "netlink-protocol.h" +#include "netlink-socket.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(dpif_offload_netlink); + +static struct nl_sock *psample_sock; +static int psample_family; + +/* Receive psample netlink message and save the attributes. */ +struct offload_psample { + struct nlattr *packet; /* Packet data. */ + int dp_group_id; /* Mapping id for sFlow offload. */ + int iifindex; /* Input ifindex. */ +}; + +/* In order to keep compatibility with kernels without psample module, + * return success even if psample is not initialized successfully. */ +static void +psample_init(void) +{ + unsigned int psample_mcgroup; + int err; + + if (!netdev_is_flow_api_enabled()) { + VLOG_DBG("Flow API is not enabled."); + return; + } + + if (psample_sock) { + VLOG_DBG("Psample socket is already initialized."); + return; + } + + err = nl_lookup_genl_family(PSAMPLE_GENL_NAME, + &psample_family); + if (err) { + VLOG_WARN("Generic Netlink family '%s' does not exist: %s\n" + "Please make sure the kernel module psample is loaded.", + PSAMPLE_GENL_NAME, ovs_strerror(err)); + return; + } + + err = nl_lookup_genl_mcgroup(PSAMPLE_GENL_NAME, + PSAMPLE_NL_MCGRP_SAMPLE_NAME, + &psample_mcgroup); + if (err) { + VLOG_WARN("Failed to join Netlink multicast group '%s': %s", + PSAMPLE_NL_MCGRP_SAMPLE_NAME, ovs_strerror(err)); + return; + } + + err = nl_sock_create(NETLINK_GENERIC, &psample_sock); + if (err) { + VLOG_WARN("Failed to create psample socket: %s", ovs_strerror(err)); + return; + } + + err = nl_sock_join_mcgroup(psample_sock, psample_mcgroup); + if (err) { + VLOG_WARN("Failed to join psample mcgroup: %s", ovs_strerror(err)); + nl_sock_destroy(psample_sock); + psample_sock = NULL; + return; + } +} + +static int +dpif_offload_netlink_init(void) +{ + psample_init(); + + return 0; +} + +static void +psample_destroy(void) +{ + if (!psample_sock) { + return; + } + + nl_sock_destroy(psample_sock); + psample_sock = NULL; +} + +static void +dpif_offload_netlink_destroy(void) +{ + psample_destroy(); +} + +static void +dpif_offload_netlink_sflow_recv_wait(void) +{ + if (psample_sock) { + nl_sock_wait(psample_sock, POLLIN); + } +} + +static int +psample_from_ofpbuf(struct offload_psample *psample, + const struct ofpbuf *buf) +{ + static const struct nl_policy ovs_psample_policy[] = { + [PSAMPLE_ATTR_IIFINDEX] = { .type = NL_A_U16 }, + [PSAMPLE_ATTR_SAMPLE_GROUP] = { .type = NL_A_U32 }, + [PSAMPLE_ATTR_GROUP_SEQ] = { .type = NL_A_U32 }, + [PSAMPLE_ATTR_DATA] = { .type = NL_A_UNSPEC }, + }; + struct nlattr *a[ARRAY_SIZE(ovs_psample_policy)]; + struct genlmsghdr *genl; + struct nlmsghdr *nlmsg; + struct ofpbuf b; + + b = ofpbuf_const_initializer(buf->data, buf->size); + nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg); + genl = ofpbuf_try_pull(&b, sizeof *genl); + if (!nlmsg || !genl || nlmsg->nlmsg_type != psample_family + || !nl_policy_parse(&b, 0, ovs_psample_policy, a, + ARRAY_SIZE(ovs_psample_policy))) { + return EINVAL; + } + + psample->iifindex = nl_attr_get_u16(a[PSAMPLE_ATTR_IIFINDEX]); + psample->dp_group_id = nl_attr_get_u32(a[PSAMPLE_ATTR_SAMPLE_GROUP]); + psample->packet = a[PSAMPLE_ATTR_DATA]; + + return 0; +} + +static int +psample_parse_packet(struct offload_psample *psample, + struct dpif_offload_sflow *sflow) +{ + dp_packet_use_stub(&sflow->packet, + CONST_CAST(struct nlattr *, + nl_attr_get(psample->packet)) - 1, + nl_attr_get_size(psample->packet) + + sizeof(struct nlattr)); + dp_packet_set_data(&sflow->packet, + (char *) dp_packet_data(&sflow->packet) + + sizeof(struct nlattr)); + dp_packet_set_size(&sflow->packet, nl_attr_get_size(psample->packet)); + + sflow->attr = dpif_offload_sflow_attr_find(psample->dp_group_id); + if (!sflow->attr) { + return ENOENT; + } + sflow->iifindex = psample->iifindex; + + return 0; +} + +static int +dpif_offload_netlink_sflow_recv(struct dpif_offload_sflow *sflow) +{ + if (!psample_sock) { + return ENOENT; + } + + for (;;) { + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + struct offload_psample psample; + struct ofpbuf buf; + int error; + + ofpbuf_use_stub(&buf, sflow->buf_stub, sizeof sflow->buf_stub); + error = nl_sock_recv(psample_sock, &buf, NULL, false); + + if (!error) { + error = psample_from_ofpbuf(&psample, &buf); + if (!error) { + ofpbuf_uninit(&buf); + error = psample_parse_packet(&psample, sflow); + return error; + } + } else if (error != EAGAIN) { + VLOG_WARN_RL(&rl, "Error reading or parsing netlink (%s).", + ovs_strerror(error)); + nl_sock_drain(psample_sock); + error = ENOBUFS; + } + + ofpbuf_uninit(&buf); + if (error) { + return error; + } + } +} + +const struct dpif_offload_class dpif_offload_netlink_class = { + .type = "system", + .init = dpif_offload_netlink_init, + .destroy = dpif_offload_netlink_destroy, + .sflow_recv_wait = dpif_offload_netlink_sflow_recv_wait, + .sflow_recv = dpif_offload_netlink_sflow_recv, +}; diff --git a/lib/dpif-offload-provider.h b/lib/dpif-offload-provider.h index af49eedb9..51a374868 100644 --- a/lib/dpif-offload-provider.h +++ b/lib/dpif-offload-provider.h @@ -17,12 +17,20 @@ #ifndef DPIF_OFFLOAD_PROVIDER_H #define DPIF_OFFLOAD_PROVIDER_H +#include "dp-packet.h" #include "netlink-protocol.h" #include "openvswitch/packets.h" #include "openvswitch/types.h" struct dpif; -struct dpif_offload_sflow; +struct registered_dpif_offload_class { + const struct dpif_offload_class *offload_class; + int refcount; +}; + +#ifdef __linux__ +extern const struct dpif_offload_class dpif_offload_netlink_class; +#endif /* When offloading sample action, userspace creates a unique ID to map * sFlow action and tunnel info and passes this ID to datapath instead @@ -37,6 +45,14 @@ struct dpif_sflow_attr { ovs_u128 ufid; /* Flow ufid. */ }; +/* Parse the specific dpif message to sFlow. So OVS can process it. */ +struct dpif_offload_sflow { + struct dp_packet packet; /* Packet data. */ + uint64_t buf_stub[4096 / 8]; /* Buffer stub for packet data. */ + uint32_t iifindex; /* Input ifindex. */ + const struct dpif_sflow_attr *attr; /* SFlow attribute. */ +}; + /* Datapath interface offload structure, to be defined by each implementation * of a datapath interface. */ @@ -62,6 +78,13 @@ struct dpif_offload_class { int (*sflow_recv)(struct dpif_offload_sflow *sflow); }; +void dp_offload_initialize(void); +void dpif_offload_close(struct dpif *); + +int dp_offload_register_provider(const struct dpif_offload_class *); +void dp_offload_class_unref(struct registered_dpif_offload_class *rc); +struct registered_dpif_offload_class *dp_offload_class_lookup(const char *); + void dpif_offload_sflow_recv_wait(const struct dpif *dpif); int dpif_offload_sflow_recv(const struct dpif *dpif, struct dpif_offload_sflow *sflow); diff --git a/lib/dpif-offload.c b/lib/dpif-offload.c index f2bf3e634..bea6a9e95 100644 --- a/lib/dpif-offload.c +++ b/lib/dpif-offload.c @@ -18,6 +18,110 @@ #include #include "dpif-provider.h" +#include "openvswitch/shash.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(dpif_offload); + +static const struct dpif_offload_class *base_dpif_offload_classes[] = { +#if defined(__linux__) + &dpif_offload_netlink_class, +#endif +}; + +static struct shash dpif_offload_classes = + SHASH_INITIALIZER(&dpif_offload_classes); + +/* Protects 'dpif_offload_classes', including the refcount. */ +static struct ovs_mutex dpif_offload_mutex = OVS_MUTEX_INITIALIZER; + +void +dp_offload_initialize(void) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + + if (ovsthread_once_start(&once)) { + for (int i = 0; i < ARRAY_SIZE(base_dpif_offload_classes); i++) { + dp_offload_register_provider(base_dpif_offload_classes[i]); + } + ovsthread_once_done(&once); + } +} + +static int +dp_offload_register_provider__(const struct dpif_offload_class *new_class) + OVS_REQUIRES(dpif_offload_mutex) +{ + struct registered_dpif_offload_class *registered_class; + int error; + + if (shash_find(&dpif_offload_classes, new_class->type)) { + VLOG_WARN("Attempted to register duplicate datapath offload " + "provider: %s", new_class->type); + return EEXIST; + } + + error = new_class->init ? new_class->init() : 0; + if (error) { + VLOG_WARN("Failed to initialize %s datapath offload class: %s", + new_class->type, ovs_strerror(error)); + return error; + } + + registered_class = xmalloc(sizeof *registered_class); + registered_class->offload_class = new_class; + registered_class->refcount = 0; + + shash_add(&dpif_offload_classes, new_class->type, registered_class); + + return 0; +} + +void dpif_offload_close(struct dpif *dpif) +{ + if (dpif->offload_class) { + struct registered_dpif_offload_class *rc; + + rc = shash_find_data(&dpif_offload_classes, dpif->offload_class->type); + dp_offload_class_unref(rc); + } +} + +int +dp_offload_register_provider(const struct dpif_offload_class *new_class) +{ + int error; + + ovs_mutex_lock(&dpif_offload_mutex); + error = dp_offload_register_provider__(new_class); + ovs_mutex_unlock(&dpif_offload_mutex); + + return error; +} + +void +dp_offload_class_unref(struct registered_dpif_offload_class *rc) +{ + ovs_mutex_lock(&dpif_offload_mutex); + ovs_assert(rc->refcount); + rc->refcount--; + ovs_mutex_unlock(&dpif_offload_mutex); +} + +struct registered_dpif_offload_class * +dp_offload_class_lookup(const char *type) +{ + struct registered_dpif_offload_class *rc; + + ovs_mutex_lock(&dpif_offload_mutex); + rc = shash_find_data(&dpif_offload_classes, type); + if (rc) { + rc->refcount++; + } + ovs_mutex_unlock(&dpif_offload_mutex); + + return rc; +} void dpif_offload_sflow_recv_wait(const struct dpif *dpif) diff --git a/lib/dpif-provider.h b/lib/dpif-provider.h index 99009722a..c8ed385a1 100644 --- a/lib/dpif-provider.h +++ b/lib/dpif-provider.h @@ -47,8 +47,10 @@ struct dpif { struct dpif_ipf_status; struct ipf_dump_ctx; -void dpif_init(struct dpif *, const struct dpif_class *, const char *name, - uint8_t netflow_engine_type, uint8_t netflow_engine_id); +void dpif_init(struct dpif *, const struct dpif_class *, + const struct dpif_offload_class *offload_class, + const char *name, uint8_t netflow_engine_type, + uint8_t netflow_engine_id); void dpif_uninit(struct dpif *dpif, bool close); static inline void dpif_assert_class(const struct dpif *dpif, diff --git a/lib/dpif.c b/lib/dpif.c index 40f5fe446..5fb8ee0a7 100644 --- a/lib/dpif.c +++ b/lib/dpif.c @@ -1699,10 +1699,11 @@ dpif_queue_to_priority(const struct dpif *dpif, uint32_t queue_id, void dpif_init(struct dpif *dpif, const struct dpif_class *dpif_class, - const char *name, + const struct dpif_offload_class *offload_class, const char *name, uint8_t netflow_engine_type, uint8_t netflow_engine_id) { dpif->dpif_class = dpif_class; + dpif->offload_class = offload_class; dpif->base_name = xstrdup(name); dpif->full_name = xasprintf("%s@%s", dpif_class->type, name); dpif->netflow_engine_type = netflow_engine_type;