From patchwork Tue Apr 25 13:15:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vamsi Krishna Attunuru X-Patchwork-Id: 126509 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id EE0A6429EB; Tue, 25 Apr 2023 15:15:56 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 905FB42D0C; Tue, 25 Apr 2023 15:15:48 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by mails.dpdk.org (Postfix) with ESMTP id 8012342BC9 for ; Tue, 25 Apr 2023 15:15:47 +0200 (CEST) Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33PC7PX9004832; Tue, 25 Apr 2023 06:15:47 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=x6Qz3JFCWeMUedPrPqc1QnuLxeEcWsxOt5N9ijcmTzE=; b=Fc2wBgJ4BqVu6ndOCPtoJbxqZcyFVhM6c/v93qmdEU9tu6akToW3M5wPehnSewFmkQTj mOFqhRQ75JBg94Ph5FOaM1oF+nhLCM/aacxeoPNckC/drsYM10OuLPTmGmr9aZZ2ue7a we7ckbDqqY5KnrExVARIr3kUt+FafXjjbUUZZWtjJC1vx1p9NJsXnhBJu6YCJuSy71XD Kuf1P49v3S5QKVvn27j2nyi7AVX7/d6wQT/4q2MQktXRZqY76Hv8BAGMYWbf3d5Q3DaT CJgjKWPTkq7w6ABcvrkgJgvnu7XwSM18AYHwDtLBUKeSLnMfTlQtIsKbQAAOxjXzNiUz eQ== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3q4f3pa6ar-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Tue, 25 Apr 2023 06:15:46 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Tue, 25 Apr 2023 06:15:44 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Tue, 25 Apr 2023 06:15:44 -0700 Received: from localhost.localdomain (unknown [10.28.36.156]) by maili.marvell.com (Postfix) with ESMTP id 370F43F7057; Tue, 25 Apr 2023 06:15:41 -0700 (PDT) From: Vamsi Attunuru To: , , CC: , Subject: [PATCH v2 2/4] node: add a node to receive pkts from kernel Date: Tue, 25 Apr 2023 06:15:14 -0700 Message-ID: <20230425131516.3308612-3-vattunuru@marvell.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230425131516.3308612-1-vattunuru@marvell.com> References: <20230421060245.3136217-1-vattunuru@marvell.com> <20230425131516.3308612-1-vattunuru@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: AQ0hiF0DXvR2732ZAPOe3SzwA4Bwhk6y X-Proofpoint-GUID: AQ0hiF0DXvR2732ZAPOe3SzwA4Bwhk6y X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-25_05,2023-04-25_01,2023-02-09_01 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Patch adds a node to receive packets from kernel over a raw socket. Signed-off-by: Vamsi Attunuru --- doc/guides/prog_guide/graph_lib.rst | 7 + lib/node/kernel_recv.c | 276 ++++++++++++++++++++++++++++ lib/node/kernel_recv_priv.h | 74 ++++++++ lib/node/meson.build | 1 + 4 files changed, 358 insertions(+) diff --git a/doc/guides/prog_guide/graph_lib.rst b/doc/guides/prog_guide/graph_lib.rst index b3b5b14827..1057f16de8 100644 --- a/doc/guides/prog_guide/graph_lib.rst +++ b/doc/guides/prog_guide/graph_lib.rst @@ -402,3 +402,10 @@ on the raw socket. Aftering sending the burst of packets to kernel, this node redirects the same objects to pkt_drop node to free up the packet buffers. + +kernel_recv +~~~~~~~~~~~ +This node receives packets from kernel over a raw socket interface. Uses ``poll`` +function to poll on the socket fd for ``POLLIN`` events to read the packets from +raw socket to stream buffer and does ``rte_node_next_stream_move()`` when there +are received packets. diff --git a/lib/node/kernel_recv.c b/lib/node/kernel_recv.c new file mode 100644 index 0000000000..9b28ad76d3 --- /dev/null +++ b/lib/node/kernel_recv.c @@ -0,0 +1,276 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(C) 2023 Marvell International Ltd. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ethdev_rx_priv.h" +#include "kernel_recv_priv.h" +#include "node_private.h" + +static struct kernel_recv_node_main kernel_recv_main; + +static inline struct rte_mbuf * +alloc_rx_mbuf(kernel_recv_node_ctx_t *ctx) +{ + kernel_recv_info_t *rx = ctx->recv_info; + + if (rx->idx >= rx->cnt) { + uint16_t cnt; + + rx->idx = 0; + rx->cnt = 0; + + cnt = rte_pktmbuf_alloc_bulk(ctx->pktmbuf_pool, rx->rx_bufs, KERN_RECV_CACHE_COUNT); + if (cnt <= 0) + return NULL; + + rx->cnt = cnt; + } + + return rx->rx_bufs[rx->idx++]; +} + +static inline void +mbuf_update(struct rte_mbuf **mbufs, uint16_t nb_pkts) +{ + struct rte_net_hdr_lens hdr_lens; + struct rte_mbuf *m; + int i; + + for (i = 0; i < nb_pkts; i++) { + m = mbufs[i]; + + m->packet_type = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK); + + m->ol_flags = 0; + m->tx_offload = 0; + + m->l2_len = hdr_lens.l2_len; + m->l3_len = hdr_lens.l3_len; + m->l4_len = hdr_lens.l4_len; + } +} + +static uint16_t +recv_pkt_parse(void **objs, uint16_t nb_pkts) +{ + uint16_t pkts_left = nb_pkts; + struct rte_mbuf **pkts; + int i; + + pkts = (struct rte_mbuf **)objs; + + if (pkts_left >= 4) { + for (i = 0; i < 4; i++) + rte_prefetch0(rte_pktmbuf_mtod(pkts[i], void *)); + } + + while (pkts_left >= 12) { + /* Prefetch next-next mbufs */ + rte_prefetch0(pkts[8]); + rte_prefetch0(pkts[9]); + rte_prefetch0(pkts[10]); + rte_prefetch0(pkts[11]); + + /* Prefetch next mbuf data */ + rte_prefetch0(rte_pktmbuf_mtod(pkts[4], void *)); + rte_prefetch0(rte_pktmbuf_mtod(pkts[5], void *)); + rte_prefetch0(rte_pktmbuf_mtod(pkts[6], void *)); + rte_prefetch0(rte_pktmbuf_mtod(pkts[7], void *)); + + /* Extract ptype of mbufs */ + mbuf_update(pkts, 4); + + pkts += 4; + pkts_left -= 4; + } + + if (pkts_left > 0) + mbuf_update(pkts, pkts_left); + + return nb_pkts; +} + +static uint16_t +kernel_recv_node_do(struct rte_graph *graph, struct rte_node *node, kernel_recv_node_ctx_t *ctx) +{ + kernel_recv_info_t *rx; + uint16_t next_index; + int fd; + + rx = ctx->recv_info; + next_index = rx->cls_next; + + fd = rx->sock; + if (fd > 0) { + struct rte_mbuf **mbufs; + uint16_t len = 0, count = 0; + int nb_cnt, i; + + nb_cnt = (node->size >= RTE_GRAPH_BURST_SIZE) ? RTE_GRAPH_BURST_SIZE : node->size; + + mbufs = (struct rte_mbuf **)node->objs; + for (i = 0; i < nb_cnt; i++) { + struct rte_mbuf *m = alloc_rx_mbuf(ctx); + + if (!m) + break; + + len = read(fd, rte_pktmbuf_mtod(m, char *), rte_pktmbuf_tailroom(m)); + if (len == 0 || len == 0xFFFF) { + rte_pktmbuf_free(m); + if (rx->idx <= 0) + node_dbg("kernel_recv", "rx_mbuf array is empty\n"); + rx->idx--; + break; + } + *mbufs++ = m; + + m->port = node->id; + rte_pktmbuf_data_len(m) = len; + + count++; + } + + if (count) { + recv_pkt_parse(node->objs, count); + node->idx = count; + + /* Enqueue to next node */ + rte_node_next_stream_move(graph, node, next_index); + } + + return count; + } + + return 0; +} + +static uint16_t +kernel_recv_node_process(struct rte_graph *graph, struct rte_node *node, void **objs, + uint16_t nb_objs) +{ + kernel_recv_node_ctx_t *ctx = (kernel_recv_node_ctx_t *)node->ctx; + int fd; + + RTE_SET_USED(objs); + RTE_SET_USED(nb_objs); + + if (!ctx) + return 0; + + fd = ctx->recv_info->sock; + if (fd > 0) { + struct pollfd fds = {.fd = fd, .events = POLLIN}; + + if (poll(&fds, 1, 0) > 0) { + if (fds.revents & POLLIN) + return kernel_recv_node_do(graph, node, ctx); + } + } + + return 0; +} + +static int +kernel_recv_node_init(const struct rte_graph *graph, struct rte_node *node) +{ + kernel_recv_node_ctx_t *ctx = (kernel_recv_node_ctx_t *)node->ctx; + kernel_recv_node_elem_t *elem = kernel_recv_main.head; + kernel_recv_info_t *recv_info; + int sock; + + while (elem) { + if (elem->nid == node->id) { + /* Update node specific context */ + memcpy(ctx, &elem->ctx, sizeof(kernel_recv_node_ctx_t)); + break; + } + elem = elem->next; + } + + RTE_VERIFY(elem != NULL); + + if (ctx->pktmbuf_pool == NULL) { + node_err("kernel_recv", "Invalid mbuf pool on graph %s\n", graph->name); + return -EINVAL; + } + + recv_info = + rte_zmalloc("kernel_recv_info", sizeof(kernel_recv_info_t), RTE_CACHE_LINE_SIZE); + if (!recv_info) { + node_err("kernel_recv", "Kernel recv_info is NULL\n"); + return -ENOMEM; + } + + sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (sock < 0) { + node_err("kernel_recv", "Unable to open RAW socket\n"); + return sock; + } + + recv_info->sock = sock; + ctx->recv_info = recv_info; + + return 0; +} + +static void +kernel_recv_node_fini(const struct rte_graph *graph __rte_unused, struct rte_node *node) +{ + kernel_recv_node_ctx_t *ctx = (kernel_recv_node_ctx_t *)node->ctx; + + if (ctx->recv_info) { + close(ctx->recv_info->sock); + ctx->recv_info->sock = -1; + rte_free(ctx->recv_info); + } + + ctx->recv_info = NULL; +} + +struct kernel_recv_node_main * +kernel_recv_node_data_get(void) +{ + return &kernel_recv_main; +} + +static struct rte_node_register kernel_recv_node_base = { + .process = kernel_recv_node_process, + .flags = RTE_NODE_SOURCE_F, + .name = "kernel_recv", + + .init = kernel_recv_node_init, + .fini = kernel_recv_node_fini, + + .nb_edges = KERNEL_RECV_NEXT_MAX, + .next_nodes = { + /* Default pkt classification node */ + [KERNEL_RECV_NEXT_PKT_CLS] = "pkt_cls", + [KERNEL_RECV_NEXT_IP4_LOOKUP] = "ip4_lookup", + }, +}; + +struct rte_node_register * +kernel_recv_node_get(void) +{ + return &kernel_recv_node_base; +} + +RTE_NODE_REGISTER(kernel_recv_node_base); diff --git a/lib/node/kernel_recv_priv.h b/lib/node/kernel_recv_priv.h new file mode 100644 index 0000000000..c2bfbf40bd --- /dev/null +++ b/lib/node/kernel_recv_priv.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(C) 2023 Marvell International Ltd. + */ + +#ifndef __INCLUDE_KERNEL_RECV_PRIV_H__ +#define __INCLUDE_KERNEL_RECV_PRIV_H__ + +#define KERN_RECV_CACHE_COUNT 64 + +typedef struct kernel_recv_info { + struct rte_mbuf *rx_bufs[KERN_RECV_CACHE_COUNT]; + uint16_t cls_next; + uint16_t idx; + uint16_t cnt; + int sock; +} kernel_recv_info_t; + +/** + * @internal + * + * Kernel Recv node context structure. + */ +typedef struct kernel_recv_node_ctx { + struct rte_mempool *pktmbuf_pool; + kernel_recv_info_t *recv_info; +} kernel_recv_node_ctx_t; + +/** + * @internal + * + * Kernel Recv node list element structure. + */ +typedef struct kernel_recv_node_elem { + struct kernel_recv_node_elem *next; /**< Pointer to the next node element. */ + struct kernel_recv_node_ctx ctx; /**< Kernel Recv node context. */ + rte_node_t nid; /**< Node identifier of the Kernel Recv node. */ +} kernel_recv_node_elem_t; + +enum kernel_recv_next_nodes { + KERNEL_RECV_NEXT_IP4_LOOKUP, + KERNEL_RECV_NEXT_PKT_CLS, + KERNEL_RECV_NEXT_MAX, +}; + +/** + * @internal + * + * Kernel Recv node main structure. + */ +struct kernel_recv_node_main { + kernel_recv_node_elem_t *head; /**< Pointer to the head node element. */ +}; + +/** + * @internal + * + * Get the Kernel Recv node data. + * + * @return + * Pointer to Kernel Recv node data. + */ +struct kernel_recv_node_main *kernel_recv_node_data_get(void); + +/** + * @internal + * + * Get the Kernel Recv node. + * + * @return + * Pointer to the Kernel Recv node. + */ +struct rte_node_register *kernel_recv_node_get(void); + +#endif /* __INCLUDE_KERNEL_RECV_PRIV_H__ */ diff --git a/lib/node/meson.build b/lib/node/meson.build index 48c2da73f7..8b1b024f61 100644 --- a/lib/node/meson.build +++ b/lib/node/meson.build @@ -13,6 +13,7 @@ sources = files( 'ethdev_tx.c', 'ip4_lookup.c', 'ip4_rewrite.c', + 'kernel_recv.c', 'log.c', 'null.c', 'pkt_cls.c',