[v5,07/12] app/graph: add ipv6_lookup command line interfaces

Message ID 20230921100832.2036256-8-skori@marvell.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series add CLI based graph application |

Checks

Context Check Description
ci/checkpatch warning coding style issues

Commit Message

Sunil Kumar Kori Sept. 21, 2023, 10:08 a.m. UTC
  From: Rakesh Kudurumalla <rkudurumalla@marvell.com>

It adds ipv6_lookup module to configure LPM6 table. This LPM6 table
will be used for IPv6 lookup and forwarding.

Following commands are exposed:
 - ipv6_lookup route add ipv6 <ip> netmask <mask> via <ip>
 - help ipv6_lookup

Signed-off-by: Sunil Kumar Kori <skori@marvell.com>
Signed-off-by: Rakesh Kudurumalla <rkudurumalla@marvell.com>
---
 app/graph/cli.c            |   2 +
 app/graph/ethdev.c         |   1 +
 app/graph/ip6_route.c      | 210 +++++++++++++++++++++++++++++++++++++
 app/graph/meson.build      |   1 +
 app/graph/route.h          |  14 +++
 doc/guides/tools/graph.rst |   9 ++
 6 files changed, 237 insertions(+)
 create mode 100644 app/graph/ip6_route.c
  

Patch

diff --git a/app/graph/cli.c b/app/graph/cli.c
index 430750db6e..7213a91ad2 100644
--- a/app/graph/cli.c
+++ b/app/graph/cli.c
@@ -32,6 +32,8 @@  cmdline_parse_ctx_t modules_ctx[] = {
 	(cmdline_parse_inst_t *)&ethdev_help_cmd_ctx,
 	(cmdline_parse_inst_t *)&ipv4_lookup_cmd_ctx,
 	(cmdline_parse_inst_t *)&ipv4_lookup_help_cmd_ctx,
+	(cmdline_parse_inst_t *)&ipv6_lookup_cmd_ctx,
+	(cmdline_parse_inst_t *)&ipv6_lookup_help_cmd_ctx,
 	NULL,
 };
 
diff --git a/app/graph/ethdev.c b/app/graph/ethdev.c
index cb29c9054d..0a4eb7be4f 100644
--- a/app/graph/ethdev.c
+++ b/app/graph/ethdev.c
@@ -161,6 +161,7 @@  ethdev_stop(void)
 
 	ethdev_list_clean();
 	route_ip4_list_clean();
+	route_ip6_list_clean();
 	printf("Bye...\n");
 }
 
diff --git a/app/graph/ip6_route.c b/app/graph/ip6_route.c
new file mode 100644
index 0000000000..93c2c967aa
--- /dev/null
+++ b/app/graph/ip6_route.c
@@ -0,0 +1,210 @@ 
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 Marvell.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmdline_parse.h>
+#include <cmdline_parse_num.h>
+#include <cmdline_parse_string.h>
+#include <cmdline_socket.h>
+
+#include <rte_node_ip6_api.h>
+
+#include "module_api.h"
+#include "route_priv.h"
+
+static const char
+cmd_ipv6_lookup_help[] = "ipv6_lookup route add ipv6 <ip> netmask <mask> via <ip>";
+
+struct ip6_route route6 = TAILQ_HEAD_INITIALIZER(route6);
+
+void
+route_ip6_list_clean(void)
+{
+	struct route_ipv6_config *route;
+
+	while (!TAILQ_EMPTY(&route6)) {
+		route = TAILQ_FIRST(&route6);
+		TAILQ_REMOVE(&route6, route, next);
+	}
+}
+
+static struct route_ipv6_config *
+find_route6_entry(struct route_ipv6_config *route)
+{
+	struct route_ipv6_config *ipv6route;
+
+	TAILQ_FOREACH(ipv6route, &route6, next) {
+		if (!memcmp(ipv6route, route, sizeof(*route)))
+			return ipv6route;
+	}
+	return NULL;
+}
+
+static uint8_t
+convert_ip6_netmask_to_depth(uint8_t *netmask)
+{
+	uint8_t setbits = 0;
+	uint8_t mask;
+	int i;
+
+	for (i = 0; i < ETHDEV_IPV6_ADDR_LEN; i++) {
+		mask = netmask[i];
+		while (mask & 0x80) {
+			mask = mask << 1;
+			setbits++;
+		}
+	}
+
+	return setbits;
+}
+
+static int
+route_ip6_add(struct route_ipv6_config *route)
+{
+	int j;
+
+	struct route_ipv6_config *ipv6route;
+
+	ipv6route = find_route6_entry(route);
+	if (!ipv6route) {
+		ipv6route = malloc(sizeof(struct route_ipv6_config));
+		if (!ipv6route)
+			return -ENOMEM;
+	} else {
+		return 0;
+	}
+
+	for (j = 0; j < ETHDEV_IPV6_ADDR_LEN; j++) {
+		ipv6route->ip[j] = route->ip[j];
+		ipv6route->mask[j] = route->mask[j];
+		ipv6route->gateway[j] = route->gateway[j];
+	}
+	ipv6route->is_used = true;
+
+	return 0;
+}
+
+int
+route_ip6_add_to_lookup(void)
+{
+	struct route_ipv6_config *route = NULL;
+	int rc = -EINVAL;
+	uint8_t depth;
+	int portid;
+
+	TAILQ_FOREACH(route, &route6, next) {
+
+		portid = ethdev_portid_by_ip6(route->gateway, route->mask);
+		if (portid < 0) {
+			printf("Invalid portid found to install the route\n");
+			return rc;
+		}
+		depth = convert_ip6_netmask_to_depth(route->mask);
+
+		rc = rte_node_ip6_route_add(route->ip, depth, portid,
+					     RTE_NODE_IP6_LOOKUP_NEXT_REWRITE);
+		if (rc < 0)
+			return rc;
+	}
+
+	return 0;
+}
+
+static void
+cli_ipv6_lookup_help(__rte_unused void *parsed_result, __rte_unused struct cmdline *cl,
+		     __rte_unused void *data)
+{
+	size_t len;
+
+	len = strlen(conn->msg_out);
+	conn->msg_out += len;
+	snprintf(conn->msg_out, conn->msg_out_len_max, "\n%s\n%s\n",
+		 "--------------------------- ipv6_lookup command help ---------------------------",
+		 cmd_ipv6_lookup_help);
+
+	len = strlen(conn->msg_out);
+	conn->msg_out_len_max -= len;
+}
+
+static void
+cli_ipv6_lookup(void *parsed_result, __rte_unused struct cmdline *cl, void *data __rte_unused)
+{
+	struct ip6_lookup_cmd_tokens *res = parsed_result;
+	struct route_ipv6_config config;
+	int rc = -EINVAL;
+
+	if (parser_ip6_read(config.ip, res->ip)) {
+		printf(MSG_ARG_INVALID, "ipv6");
+		return;
+	}
+
+	if (parser_ip6_read(config.mask, res->mask)) {
+		printf(MSG_ARG_INVALID, "netmask");
+		return;
+	}
+
+	if (parser_ip6_read(config.gateway, res->via_ip)) {
+		printf(MSG_ARG_INVALID, "gateway ip");
+		return;
+	}
+
+	rc = route_ip6_add(&config);
+	if (rc)
+		printf(MSG_CMD_FAIL, res->cmd);
+}
+
+cmdline_parse_token_string_t ip6_lookup_cmd =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, cmd, "ipv6_lookup");
+cmdline_parse_token_string_t ip6_lookup_route =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, route, "route");
+cmdline_parse_token_string_t ip6_lookup_add =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, add, "add");
+cmdline_parse_token_string_t ip6_lookup_ip6 =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, ip6, "ipv6");
+cmdline_parse_token_string_t ip6_lookup_ip =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, ip, NULL);
+cmdline_parse_token_string_t ip6_lookup_netmask =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, netmask, "netmask");
+cmdline_parse_token_string_t ip6_lookup_mask =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, mask, NULL);
+cmdline_parse_token_string_t ip6_lookup_via =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, via, "via");
+cmdline_parse_token_string_t ip6_lookup_via_ip =
+	TOKEN_STRING_INITIALIZER(struct ip6_lookup_cmd_tokens, via_ip, NULL);
+
+cmdline_parse_inst_t ipv6_lookup_cmd_ctx = {
+	.f = cli_ipv6_lookup,
+	.data = NULL,
+	.help_str = cmd_ipv6_lookup_help,
+	.tokens = {
+		(void *)&ip6_lookup_cmd,
+		(void *)&ip6_lookup_route,
+		(void *)&ip6_lookup_add,
+		(void *)&ip6_lookup_ip6,
+		(void *)&ip6_lookup_ip,
+		(void *)&ip6_lookup_netmask,
+		(void *)&ip6_lookup_mask,
+		(void *)&ip6_lookup_via,
+		(void *)&ip6_lookup_via_ip,
+		NULL,
+	},
+};
+
+cmdline_parse_token_string_t ipv6_lookup_help_cmd =
+	TOKEN_STRING_INITIALIZER(struct ipv6_lookup_help_cmd_tokens, cmd, "help");
+cmdline_parse_token_string_t ipv6_lookup_help_module =
+	TOKEN_STRING_INITIALIZER(struct ipv6_lookup_help_cmd_tokens, module, "ipv6_lookup");
+
+cmdline_parse_inst_t ipv6_lookup_help_cmd_ctx = {
+	.f = cli_ipv6_lookup_help,
+	.data = NULL,
+	.help_str = "",
+	.tokens = {
+		(void *)&ipv6_lookup_help_cmd,
+		(void *)&ipv6_lookup_help_module,
+		NULL,
+	},
+};
diff --git a/app/graph/meson.build b/app/graph/meson.build
index b993d6375f..6841a25eb6 100644
--- a/app/graph/meson.build
+++ b/app/graph/meson.build
@@ -13,6 +13,7 @@  sources = files(
         'conn.c',
         'ethdev.c',
         'ip4_route.c',
+        'ip6_route.c',
         'main.c',
         'mempool.c',
         'utils.c',
diff --git a/app/graph/route.h b/app/graph/route.h
index a44d401d55..0d271d1350 100644
--- a/app/graph/route.h
+++ b/app/graph/route.h
@@ -8,7 +8,9 @@ 
 #define MAX_ROUTE_ENTRIES 32
 
 extern cmdline_parse_inst_t ipv4_lookup_cmd_ctx;
+extern cmdline_parse_inst_t ipv6_lookup_cmd_ctx;
 extern cmdline_parse_inst_t ipv4_lookup_help_cmd_ctx;
+extern cmdline_parse_inst_t ipv6_lookup_help_cmd_ctx;
 
 struct route_ipv4_config {
 	TAILQ_ENTRY(route_ipv4_config) next;
@@ -20,7 +22,19 @@  struct route_ipv4_config {
 
 TAILQ_HEAD(ip4_route, route_ipv4_config);
 
+struct route_ipv6_config {
+	TAILQ_ENTRY(route_ipv6_config) next;
+	uint8_t ip[16];
+	uint8_t mask[16];
+	uint8_t gateway[16];
+	bool is_used;
+};
+
+TAILQ_HEAD(ip6_route, route_ipv6_config);
+
 int route_ip4_add_to_lookup(void);
+int route_ip6_add_to_lookup(void);
 void route_ip4_list_clean(void);
+void route_ip6_list_clean(void);
 
 #endif
diff --git a/doc/guides/tools/graph.rst b/doc/guides/tools/graph.rst
index e2b4a3dfd5..0782eb3461 100644
--- a/doc/guides/tools/graph.rst
+++ b/doc/guides/tools/graph.rst
@@ -133,6 +133,15 @@  file to express the requested use case configuration.
      - Command to dump ipv4_lookup help message
      - Yes
      - Yes
+   * - ipv6_lookup route add ipv6 <ip> netmask <mask> via <ip>
+     - Command to add a route into ``ipv6_lookup`` LPM table. It is needed if user
+       wishes to route the packets based on LPM6 lookup table.
+     - No
+     - Yes
+   * - help ipv6_lookup
+     - Command to dump ipv6_lookup help message
+     - Yes
+     - Yes
 
 Runtime configuration
 ---------------------