[v3,1/3] node/kernel_tx: support packet transmit to kernel

Message ID 20230602162216.1868870-2-vattunuru@marvell.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series node: Introduce kernel_rx & kernel_tx nodes |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Vamsi Krishna Attunuru June 2, 2023, 4:22 p.m. UTC
  Patch adds a node to transmit the packets to kernel over
a raw socket.

Signed-off-by: Vamsi Attunuru <vattunuru@marvell.com>
---
 doc/guides/prog_guide/graph_lib.rst |   9 ++
 lib/node/kernel_tx.c                | 122 ++++++++++++++++++++++++++++
 lib/node/kernel_tx_priv.h           |  16 ++++
 lib/node/meson.build                |   1 +
 4 files changed, 148 insertions(+)
  

Comments

Nithin Dabilpuram June 5, 2023, 12:47 p.m. UTC | #1
Acked-by: Nithin Dabilpuram <ndabilpuram@marvell.com>

On Fri, Jun 2, 2023 at 9:52 PM Vamsi Attunuru <vattunuru@marvell.com> wrote:
>
> Patch adds a node to transmit the packets to kernel over
> a raw socket.
>
> Signed-off-by: Vamsi Attunuru <vattunuru@marvell.com>
> ---
>  doc/guides/prog_guide/graph_lib.rst |   9 ++
>  lib/node/kernel_tx.c                | 122 ++++++++++++++++++++++++++++
>  lib/node/kernel_tx_priv.h           |  16 ++++
>  lib/node/meson.build                |   1 +
>  4 files changed, 148 insertions(+)
>
> diff --git a/doc/guides/prog_guide/graph_lib.rst b/doc/guides/prog_guide/graph_lib.rst
> index 1cfdc86433..fa22b014f3 100644
> --- a/doc/guides/prog_guide/graph_lib.rst
> +++ b/doc/guides/prog_guide/graph_lib.rst
> @@ -392,3 +392,12 @@ null
>  ~~~~
>  This node ignores the set of objects passed to it and reports that all are
>  processed.
> +
> +kernel_tx
> +~~~~~~~~~
> +This node is an exit node that forwards the packets to kernel. It will be used
> +to forward any control plane traffic to kernel stack from DPDK. It uses a raw
> +socket interface to transmit the packets, it uses the packet's destination
> +IP address in sockaddr_in address structure and ``sendto`` function to send
> +data on the raw socket. Aftering sending the burst of packets to kernel,
> +this node free up the packet buffers.
> diff --git a/lib/node/kernel_tx.c b/lib/node/kernel_tx.c
> new file mode 100644
> index 0000000000..27d1808c71
> --- /dev/null
> +++ b/lib/node/kernel_tx.c
> @@ -0,0 +1,122 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(C) 2023 Marvell International Ltd.
> + */
> +
> +#include <fcntl.h>
> +#include <sys/ioctl.h>
> +#include <sys/socket.h>
> +#include <unistd.h>
> +
> +#include <rte_debug.h>
> +#include <rte_ethdev.h>
> +#include <rte_graph.h>
> +#include <rte_graph_worker.h>
> +#include <rte_ip.h>
> +
> +#include "kernel_tx_priv.h"
> +#include "node_private.h"
> +
> +static __rte_always_inline void
> +kernel_tx_process_mbuf(struct rte_node *node, struct rte_mbuf **mbufs, uint16_t cnt)
> +{
> +       kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
> +       struct sockaddr_in sin = {0};
> +       struct rte_ipv4_hdr *ip4;
> +       size_t len;
> +       char *buf;
> +       int i;
> +
> +       for (i = 0; i < cnt; i++) {
> +               ip4 = rte_pktmbuf_mtod(mbufs[i], struct rte_ipv4_hdr *);
> +               len = rte_pktmbuf_data_len(mbufs[i]);
> +               buf = (char *)ip4;
> +
> +               sin.sin_family = AF_INET;
> +               sin.sin_port = 0;
> +               sin.sin_addr.s_addr = ip4->dst_addr;
> +
> +               if (sendto(ctx->sock, buf, len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
> +                       node_err("kernel_tx", "Unable to send packets: %s\n", strerror(errno));
> +       }
> +}
> +
> +static uint16_t
> +kernel_tx_node_process(struct rte_graph *graph __rte_unused, struct rte_node *node, void **objs,
> +                        uint16_t nb_objs)
> +{
> +       struct rte_mbuf **pkts = (struct rte_mbuf **)objs;
> +       uint16_t obj_left = nb_objs;
> +
> +#define PREFETCH_CNT 4
> +
> +       while (obj_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_offset(pkts[4], void *, pkts[4]->l2_len));
> +               rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[5], void *, pkts[5]->l2_len));
> +               rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[6], void *, pkts[6]->l2_len));
> +               rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[7], void *, pkts[7]->l2_len));
> +
> +               kernel_tx_process_mbuf(node, pkts, PREFETCH_CNT);
> +
> +               obj_left -= PREFETCH_CNT;
> +               pkts += PREFETCH_CNT;
> +       }
> +
> +       while (obj_left > 0) {
> +               kernel_tx_process_mbuf(node, pkts, 1);
> +
> +               obj_left--;
> +               pkts++;
> +       }
> +
> +       rte_pktmbuf_free_bulk((struct rte_mbuf **)objs, nb_objs);
> +
> +       return nb_objs;
> +}
> +
> +static int
> +kernel_tx_node_init(const struct rte_graph *graph __rte_unused, struct rte_node *node)
> +{
> +       kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
> +
> +       ctx->sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
> +       if (ctx->sock < 0)
> +               node_err("kernel_tx", "Unable to open RAW socket\n");
> +
> +       return 0;
> +}
> +
> +static void
> +kernel_tx_node_fini(const struct rte_graph *graph __rte_unused, struct rte_node *node)
> +{
> +       kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
> +
> +       if (ctx->sock >= 0) {
> +               close(ctx->sock);
> +               ctx->sock = -1;
> +       }
> +}
> +
> +static struct rte_node_register kernel_tx_node_base = {
> +       .process = kernel_tx_node_process,
> +       .name = "kernel_tx",
> +
> +       .init = kernel_tx_node_init,
> +       .fini = kernel_tx_node_fini,
> +
> +       .nb_edges = 0,
> +};
> +
> +struct rte_node_register *
> +kernel_tx_node_get(void)
> +{
> +       return &kernel_tx_node_base;
> +}
> +
> +RTE_NODE_REGISTER(kernel_tx_node_base);
> diff --git a/lib/node/kernel_tx_priv.h b/lib/node/kernel_tx_priv.h
> new file mode 100644
> index 0000000000..6fd8a4f2af
> --- /dev/null
> +++ b/lib/node/kernel_tx_priv.h
> @@ -0,0 +1,16 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(C) 2023 Marvell International Ltd.
> + */
> +
> +#ifndef __KERNEL_TX_PRIV_H__
> +#define __KERNEL_TX_PRIV_H__
> +
> +/* kernel_tx node context structure. */
> +typedef struct kernel_tx_node_ctx {
> +       int sock;
> +} kernel_tx_node_ctx_t;
> +
> +/* Get the pointer to kernel_tx node register structure */
> +struct rte_node_register *kernel_tx_node_get(void);
> +
> +#endif /* __KERNEL_TX_PRIV_H__ */
> diff --git a/lib/node/meson.build b/lib/node/meson.build
> index dbdf673c86..0520be23ff 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_tx.c',
>          'log.c',
>          'null.c',
>          'pkt_cls.c',
> --
> 2.25.1
>
  
Thomas Monjalon June 12, 2023, 7:32 p.m. UTC | #2
05/06/2023 14:47, Nithin Dabilpuram:
> Acked-by: Nithin Dabilpuram <ndabilpuram@marvell.com>
> 
> On Fri, Jun 2, 2023 at 9:52 PM Vamsi Attunuru <vattunuru@marvell.com> wrote:
> >
> > Patch adds a node to transmit the packets to kernel over
> > a raw socket.
> >
> > Signed-off-by: Vamsi Attunuru <vattunuru@marvell.com>
> > ---
[...]
> > +kernel_tx
> > +~~~~~~~~~
> > +This node is an exit node that forwards the packets to kernel. It will be used
> > +to forward any control plane traffic to kernel stack from DPDK. It uses a raw
> > +socket interface to transmit the packets, it uses the packet's destination
> > +IP address in sockaddr_in address structure and ``sendto`` function to send
> > +data on the raw socket. Aftering sending the burst of packets to kernel,

Typo: Aftering -> After
Fixed on apply.
  

Patch

diff --git a/doc/guides/prog_guide/graph_lib.rst b/doc/guides/prog_guide/graph_lib.rst
index 1cfdc86433..fa22b014f3 100644
--- a/doc/guides/prog_guide/graph_lib.rst
+++ b/doc/guides/prog_guide/graph_lib.rst
@@ -392,3 +392,12 @@  null
 ~~~~
 This node ignores the set of objects passed to it and reports that all are
 processed.
+
+kernel_tx
+~~~~~~~~~
+This node is an exit node that forwards the packets to kernel. It will be used
+to forward any control plane traffic to kernel stack from DPDK. It uses a raw
+socket interface to transmit the packets, it uses the packet's destination
+IP address in sockaddr_in address structure and ``sendto`` function to send
+data on the raw socket. Aftering sending the burst of packets to kernel,
+this node free up the packet buffers.
diff --git a/lib/node/kernel_tx.c b/lib/node/kernel_tx.c
new file mode 100644
index 0000000000..27d1808c71
--- /dev/null
+++ b/lib/node/kernel_tx.c
@@ -0,0 +1,122 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell International Ltd.
+ */
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <rte_debug.h>
+#include <rte_ethdev.h>
+#include <rte_graph.h>
+#include <rte_graph_worker.h>
+#include <rte_ip.h>
+
+#include "kernel_tx_priv.h"
+#include "node_private.h"
+
+static __rte_always_inline void
+kernel_tx_process_mbuf(struct rte_node *node, struct rte_mbuf **mbufs, uint16_t cnt)
+{
+	kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
+	struct sockaddr_in sin = {0};
+	struct rte_ipv4_hdr *ip4;
+	size_t len;
+	char *buf;
+	int i;
+
+	for (i = 0; i < cnt; i++) {
+		ip4 = rte_pktmbuf_mtod(mbufs[i], struct rte_ipv4_hdr *);
+		len = rte_pktmbuf_data_len(mbufs[i]);
+		buf = (char *)ip4;
+
+		sin.sin_family = AF_INET;
+		sin.sin_port = 0;
+		sin.sin_addr.s_addr = ip4->dst_addr;
+
+		if (sendto(ctx->sock, buf, len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+			node_err("kernel_tx", "Unable to send packets: %s\n", strerror(errno));
+	}
+}
+
+static uint16_t
+kernel_tx_node_process(struct rte_graph *graph __rte_unused, struct rte_node *node, void **objs,
+			 uint16_t nb_objs)
+{
+	struct rte_mbuf **pkts = (struct rte_mbuf **)objs;
+	uint16_t obj_left = nb_objs;
+
+#define PREFETCH_CNT 4
+
+	while (obj_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_offset(pkts[4], void *, pkts[4]->l2_len));
+		rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[5], void *, pkts[5]->l2_len));
+		rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[6], void *, pkts[6]->l2_len));
+		rte_prefetch0(rte_pktmbuf_mtod_offset(pkts[7], void *, pkts[7]->l2_len));
+
+		kernel_tx_process_mbuf(node, pkts, PREFETCH_CNT);
+
+		obj_left -= PREFETCH_CNT;
+		pkts += PREFETCH_CNT;
+	}
+
+	while (obj_left > 0) {
+		kernel_tx_process_mbuf(node, pkts, 1);
+
+		obj_left--;
+		pkts++;
+	}
+
+	rte_pktmbuf_free_bulk((struct rte_mbuf **)objs, nb_objs);
+
+	return nb_objs;
+}
+
+static int
+kernel_tx_node_init(const struct rte_graph *graph __rte_unused, struct rte_node *node)
+{
+	kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
+
+	ctx->sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+	if (ctx->sock < 0)
+		node_err("kernel_tx", "Unable to open RAW socket\n");
+
+	return 0;
+}
+
+static void
+kernel_tx_node_fini(const struct rte_graph *graph __rte_unused, struct rte_node *node)
+{
+	kernel_tx_node_ctx_t *ctx = (kernel_tx_node_ctx_t *)node->ctx;
+
+	if (ctx->sock >= 0) {
+		close(ctx->sock);
+		ctx->sock = -1;
+	}
+}
+
+static struct rte_node_register kernel_tx_node_base = {
+	.process = kernel_tx_node_process,
+	.name = "kernel_tx",
+
+	.init = kernel_tx_node_init,
+	.fini = kernel_tx_node_fini,
+
+	.nb_edges = 0,
+};
+
+struct rte_node_register *
+kernel_tx_node_get(void)
+{
+	return &kernel_tx_node_base;
+}
+
+RTE_NODE_REGISTER(kernel_tx_node_base);
diff --git a/lib/node/kernel_tx_priv.h b/lib/node/kernel_tx_priv.h
new file mode 100644
index 0000000000..6fd8a4f2af
--- /dev/null
+++ b/lib/node/kernel_tx_priv.h
@@ -0,0 +1,16 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(C) 2023 Marvell International Ltd.
+ */
+
+#ifndef __KERNEL_TX_PRIV_H__
+#define __KERNEL_TX_PRIV_H__
+
+/* kernel_tx node context structure. */
+typedef struct kernel_tx_node_ctx {
+	int sock;
+} kernel_tx_node_ctx_t;
+
+/* Get the pointer to kernel_tx node register structure */
+struct rte_node_register *kernel_tx_node_get(void);
+
+#endif /* __KERNEL_TX_PRIV_H__ */
diff --git a/lib/node/meson.build b/lib/node/meson.build
index dbdf673c86..0520be23ff 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_tx.c',
         'log.c',
         'null.c',
         'pkt_cls.c',