From patchwork Wed Sep 15 12:43:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Mi X-Patchwork-Id: 1528339 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (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 ozlabs.org (Postfix) with ESMTPS id 4H8fz06zWsz9sSs for ; Wed, 15 Sep 2021 22:43:40 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 9F85360BE6; Wed, 15 Sep 2021 12:43:37 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id htWCLFcvAeUK; Wed, 15 Sep 2021 12:43:36 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp3.osuosl.org (Postfix) with ESMTPS id 7001760BB0; Wed, 15 Sep 2021 12:43:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 8640CC0027; Wed, 15 Sep 2021 12:43:33 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 4CC7CC0011 for ; Wed, 15 Sep 2021 12:43:30 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 20B62401DA for ; Wed, 15 Sep 2021 12:43:30 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id k-QbkbYkNUbR for ; Wed, 15 Sep 2021 12:43:29 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by smtp4.osuosl.org (Postfix) with ESMTP id 8DED640121 for ; Wed, 15 Sep 2021 12:43:28 +0000 (UTC) Received: from Internal Mail-Server by MTLPINE1 (envelope-from cmi@nvidia.com) with SMTP; 15 Sep 2021 15:43:21 +0300 Received: from c-141-46-1-010.mtl.labs.mlnx (c-141-46-1-010.mtl.labs.mlnx [10.141.46.10]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 18FChLbZ010242; Wed, 15 Sep 2021 15:43:21 +0300 To: dev@openvswitch.org Date: Wed, 15 Sep 2021 15:43:17 +0300 Message-Id: <20210915124319.1683219-6-cmi@nvidia.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210915124319.1683219-1-cmi@nvidia.com> References: <20210915124319.1683219-1-cmi@nvidia.com> MIME-Version: 1.0 Cc: elibr@nvidia.com, simon.horman@netronome.com, roniba@nvidia.com, i.maximets@ovn.org Subject: [ovs-dev] [PATCH v15 5/7] 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-netlink.c | 2 +- lib/dpif-offload-netlink.c | 208 ++++++++++++++++++++++++++++++++++++ lib/dpif-offload-provider.h | 13 ++- 4 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 lib/dpif-offload-netlink.c diff --git a/lib/automake.mk b/lib/automake.mk index 9259f57de..18cffa827 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -446,6 +446,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-netlink.c b/lib/dpif-netlink.c index f546b48e5..19ae543e6 100644 --- a/lib/dpif-netlink.c +++ b/lib/dpif-netlink.c @@ -4341,7 +4341,7 @@ const struct dpif_class dpif_netlink_class = { NULL, /* bond_add */ NULL, /* bond_del */ NULL, /* bond_stats_get */ - NULL, /* dpif_offload_api */ + &dpif_offload_netlink, }; static int diff --git a/lib/dpif-offload-netlink.c b/lib/dpif-offload-netlink.c new file mode 100644 index 000000000..fc3712764 --- /dev/null +++ b/lib/dpif-offload-netlink.c @@ -0,0 +1,208 @@ +/* + * 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. */ +}; + +static void +dpif_offload_netlink_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_INFO("%s: Generic Netlink family '%s' does not exist. " + "Please make sure the kernel module psample is loaded. " + "Error %d", __func__, PSAMPLE_GENL_NAME, err); + return; + } + + err = nl_lookup_genl_mcgroup(PSAMPLE_GENL_NAME, + PSAMPLE_NL_MCGRP_SAMPLE_NAME, + &psample_mcgroup); + if (err) { + VLOG_INFO("%s: Failed to join multicast group '%s' for Generic " + "Netlink family '%s' with error %d", __func__, + PSAMPLE_NL_MCGRP_SAMPLE_NAME, + PSAMPLE_GENL_NAME, err); + return; + } + + err = nl_sock_create(NETLINK_GENERIC, &psample_sock); + if (err) { + VLOG_INFO("%s: Failed to create psample socket with error %d", + __func__, err); + return; + } + + err = nl_sock_join_mcgroup(psample_sock, psample_mcgroup); + if (err) { + VLOG_INFO("%s: Failed to join psample mcgroup with error %d", + __func__, err); + nl_sock_destroy(psample_sock); + return; + } +} + +static void +dpif_offload_netlink_destroy(void) +{ + if (!psample_sock) { + return; + } + + nl_sock_destroy(psample_sock); + psample_sock = NULL; +} + +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; + uint64_t buf_stub[4096 / 8]; + struct ofpbuf buf; + int error; + + ofpbuf_use_stub(&buf, buf_stub, sizeof 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, "%s: error reading or parsing netlink (%s)", + __func__, ovs_strerror(error)); + nl_sock_drain(psample_sock); + error = ENOBUFS; + } + + ofpbuf_uninit(&buf); + if (error) { + return error; + } + } +} + +const struct dpif_offload_api dpif_offload_netlink = { + .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 95c29c0d8..db46209e1 100644 --- a/lib/dpif-offload-provider.h +++ b/lib/dpif-offload-provider.h @@ -17,12 +17,12 @@ #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; /* 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 +37,13 @@ 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. */ + 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. */ @@ -61,4 +68,8 @@ void dpif_offload_sflow_recv_wait(const struct dpif *dpif); int dpif_offload_sflow_recv(const struct dpif *dpif, struct dpif_offload_sflow *sflow); +#ifdef __linux__ +extern const struct dpif_offload_api dpif_offload_netlink; +#endif + #endif /* DPIF_OFFLOAD_PROVIDER_H */