From: Rakesh Kudurumalla <rkuduruma...@marvell.com>

It adds graph module to create a graph for a given usecase like
l3fwd.

Following commands are exposed:
 - graph <usecases> [bsz <size>] [tmo <ns>] [coremask <bitmask>] \
        model <rtc | mcd | default>
 - graph start
 - graph stats show
 - help graph

Signed-off-by: Sunil Kumar Kori <sk...@marvell.com>
Signed-off-by: Rakesh Kudurumalla <rkuduruma...@marvell.com>
---
 app/graph/cli.c        |   4 +
 app/graph/graph.c      | 473 +++++++++++++++++++++++++++++++++++++++++
 app/graph/graph.h      |  18 ++
 app/graph/graph_priv.h |  61 ++++++
 app/graph/meson.build  |   1 +
 app/graph/module_api.h |   1 +
 6 files changed, 558 insertions(+)
 create mode 100644 app/graph/graph.c
 create mode 100644 app/graph/graph.h
 create mode 100644 app/graph/graph_priv.h

diff --git a/app/graph/cli.c b/app/graph/cli.c
index e947f61ee4..c43af5925c 100644
--- a/app/graph/cli.c
+++ b/app/graph/cli.c
@@ -20,6 +20,10 @@
 #define MAX_LINE_SIZE 2048
 
 cmdline_parse_ctx_t modules_ctx[] = {
+       (cmdline_parse_inst_t *)&graph_config_cmd_ctx,
+       (cmdline_parse_inst_t *)&graph_start_cmd_ctx,
+       (cmdline_parse_inst_t *)&graph_stats_cmd_ctx,
+       (cmdline_parse_inst_t *)&graph_help_cmd_ctx,
        (cmdline_parse_inst_t *)&mempool_config_cmd_ctx,
        (cmdline_parse_inst_t *)&mempool_help_cmd_ctx,
        (cmdline_parse_inst_t *)&ethdev_show_cmd_ctx,
diff --git a/app/graph/graph.c b/app/graph/graph.c
new file mode 100644
index 0000000000..d44c5f0464
--- /dev/null
+++ b/app/graph/graph.c
@@ -0,0 +1,473 @@
+/* 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_ethdev.h>
+#include <rte_graph_worker.h>
+#include <rte_log.h>
+
+#include "graph_priv.h"
+#include "module_api.h"
+
+#define RTE_LOGTYPE_APP_GRAPH RTE_LOGTYPE_USER1
+
+static const char
+cmd_graph_help[] = "graph <usecases> bsz <size> tmo <ns> coremask <bitmask> "
+                  "model <rtc | mcd | default>";
+
+static const char * const supported_usecases[] = {"l3fwd"};
+struct graph_config graph_config;
+
+/* Check the link rc of all ports in up to 9s, and print them finally */
+static void
+check_all_ports_link_status(uint32_t port_mask)
+{
+#define CHECK_INTERVAL 100 /* 100ms */
+#define MAX_CHECK_TIME 90  /* 9s (90 * 100ms) in total */
+       char link_rc_text[RTE_ETH_LINK_MAX_STR_LEN];
+       uint8_t count, all_ports_up, print_flag = 0;
+       struct rte_eth_link link;
+       uint16_t portid;
+       int rc;
+
+       printf("\nChecking link rc");
+       fflush(stdout);
+       for (count = 0; count <= MAX_CHECK_TIME; count++) {
+               if (force_quit)
+                       return;
+
+               all_ports_up = 1;
+               RTE_ETH_FOREACH_DEV(portid)
+               {
+                       if (force_quit)
+                               return;
+
+                       if ((port_mask & (1 << portid)) == 0)
+                               continue;
+
+                       memset(&link, 0, sizeof(link));
+                       rc = rte_eth_link_get_nowait(portid, &link);
+                       if (rc < 0) {
+                               all_ports_up = 0;
+                               if (print_flag == 1)
+                                       printf("Port %u link get failed: %s\n",
+                                              portid, rte_strerror(-rc));
+                               continue;
+                       }
+
+                       /* Print link rc if flag set */
+                       if (print_flag == 1) {
+                               rte_eth_link_to_str(link_rc_text, 
sizeof(link_rc_text),
+                                       &link);
+                               printf("Port %d %s\n", portid, link_rc_text);
+                               continue;
+                       }
+
+                       /* Clear all_ports_up flag if any link down */
+                       if (link.link_status == RTE_ETH_LINK_DOWN) {
+                               all_ports_up = 0;
+                               break;
+                       }
+               }
+
+               /* After finally printing all link rc, get out */
+               if (print_flag == 1)
+                       break;
+
+               if (all_ports_up == 0) {
+                       printf(".");
+                       fflush(stdout);
+                       rte_delay_ms(CHECK_INTERVAL);
+               }
+
+               /* Set the print_flag if all ports up or timeout */
+               if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
+                       print_flag = 1;
+                       printf("Done\n");
+               }
+       }
+}
+
+static bool
+parser_usecases_read(char *usecases)
+{
+       bool valid = false;
+       uint32_t i, j = 0;
+       char *token;
+
+       token = strtok(usecases, ",");
+       while (token != NULL) {
+               for (i = 0; i < RTE_DIM(supported_usecases); i++) {
+                       if (strcmp(supported_usecases[i], token) == 0) {
+                               graph_config.usecases[j].enabled = true;
+                               strcpy(graph_config.usecases[j].name, token);
+                               valid = true;
+                               j++;
+                               break;
+                       }
+               }
+               token = strtok(NULL, ",");
+       }
+
+       return valid;
+}
+
+static uint64_t
+graph_worker_count_get(void)
+{
+       uint64_t nb_worker = 0;
+       uint64_t coremask;
+
+       coremask = graph_config.params.coremask;
+       while (coremask) {
+               if (coremask & 0x1)
+                       nb_worker++;
+
+               coremask = (coremask >> 1);
+       }
+
+       return nb_worker;
+}
+
+static struct rte_node_ethdev_config *
+graph_rxtx_node_config_get(uint32_t *num_conf, uint32_t *num_graphs)
+{
+       uint32_t n_tx_queue, nb_conf = 0, lcore_id;
+       uint16_t queueid, portid, nb_graphs = 0;
+       uint8_t nb_rx_queue, queue;
+       struct lcore_conf *qconf;
+
+       n_tx_queue = graph_worker_count_get();
+       if (n_tx_queue > RTE_MAX_ETHPORTS)
+               n_tx_queue = RTE_MAX_ETHPORTS;
+
+       RTE_ETH_FOREACH_DEV(portid) {
+               /* Skip ports that are not enabled */
+               if ((enabled_port_mask & (1 << portid)) == 0) {
+                       printf("\nSkipping disabled port %d\n", portid);
+                       continue;
+               }
+
+               nb_rx_queue = ethdev_rx_num_rx_queues_get(portid);
+
+               /* Setup ethdev node config */
+               ethdev_conf[nb_conf].port_id = portid;
+               ethdev_conf[nb_conf].num_rx_queues = nb_rx_queue;
+               ethdev_conf[nb_conf].num_tx_queues = n_tx_queue;
+               ethdev_conf[nb_conf].mp = ethdev_mempool_list_by_portid(portid);
+               ethdev_conf[nb_conf].mp_count = 1; /* Check with pools */
+
+               nb_conf++;
+       }
+
+       for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
+               if (rte_lcore_is_enabled(lcore_id) == 0)
+                       continue;
+
+               qconf = &lcore_conf[lcore_id];
+               printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
+               fflush(stdout);
+
+               /* Init RX queues */
+               for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
+                       portid = qconf->rx_queue_list[queue].port_id;
+                       queueid = qconf->rx_queue_list[queue].queue_id;
+
+                       /* Add this queue node to its graph */
+                       snprintf(qconf->rx_queue_list[queue].node_name, 
RTE_NODE_NAMESIZE,
+                                "ethdev_rx-%u-%u", portid, queueid);
+               }
+               if (qconf->n_rx_queue)
+                       nb_graphs++;
+       }
+
+       printf("\n");
+
+       ethdev_start();
+       check_all_ports_link_status(enabled_port_mask);
+
+       *num_conf = nb_conf;
+       *num_graphs = nb_graphs;
+       return ethdev_conf;
+}
+
+static void
+graph_stats_print_to_file(void)
+{
+       struct rte_graph_cluster_stats_param s_param;
+       struct rte_graph_cluster_stats *stats;
+       const char *pattern = "worker_*";
+       FILE *fp = NULL;
+       size_t sz, len;
+
+       /* Prepare stats object */
+       fp = fopen("/tmp/graph_stats.txt", "w+");
+       memset(&s_param, 0, sizeof(s_param));
+       s_param.f = fp;
+       s_param.socket_id = SOCKET_ID_ANY;
+       s_param.graph_patterns = &pattern;
+       s_param.nb_graph_patterns = 1;
+
+       stats = rte_graph_cluster_stats_create(&s_param);
+       if (stats == NULL)
+               rte_exit(EXIT_FAILURE, "Unable to create stats object\n");
+
+       /* Clear screen and move to top left */
+       rte_graph_cluster_stats_get(stats, 0);
+       rte_delay_ms(1E3);
+
+       fseek(fp, 0L, SEEK_END);
+       sz = ftell(fp);
+       fseek(fp, 0L, SEEK_SET);
+
+       len = strlen(conn->msg_out);
+       conn->msg_out += len;
+
+       sz = fread(conn->msg_out, sizeof(char), sz, fp);
+       len = strlen(conn->msg_out);
+       conn->msg_out_len_max -= len;
+       rte_graph_cluster_stats_destroy(stats);
+
+       fclose(fp);
+}
+
+static void
+cli_graph_stats(__rte_unused void *parsed_result, __rte_unused struct cmdline 
*cl,
+               __rte_unused void *data)
+{
+       graph_stats_print_to_file();
+}
+
+static void
+cli_graph_start(__rte_unused void *parsed_result, __rte_unused struct cmdline 
*cl,
+               __rte_unused void *data)
+{
+       struct rte_node_ethdev_config *conf;
+       uint32_t nb_graphs = 0, nb_conf, i;
+
+       conf = graph_rxtx_node_config_get(&nb_conf, &nb_graphs);
+       for (i = 0; i < MAX_GRAPH_USECASES; i++) {
+               if (!strcmp(graph_config.usecases[i].name, "l3fwd")) {
+                       if (graph_config.usecases[i].enabled) {
+                               RTE_SET_USED(conf);
+                               break;
+                       }
+               }
+       }
+}
+
+static int
+graph_config_add(char *usecases, struct graph_config *config)
+{
+       if (!parser_usecases_read(usecases))
+               return -EINVAL;
+
+       graph_config.params.bsz = config->params.bsz;
+       graph_config.params.tmo = config->params.tmo;
+       graph_config.params.coremask = config->params.coremask;
+       graph_config.model = config->model;
+
+       return 0;
+}
+
+int
+graph_walk_start(void *conf)
+{
+       struct lcore_conf *qconf;
+       struct rte_graph *graph;
+       uint32_t lcore_id;
+
+       RTE_SET_USED(conf);
+
+       lcore_id = rte_lcore_id();
+       qconf = &lcore_conf[lcore_id];
+       graph = qconf->graph;
+
+       if (!graph) {
+               RTE_LOG(INFO, APP_GRAPH, "Lcore %u has nothing to do\n", 
lcore_id);
+               return 0;
+       }
+
+       RTE_LOG(INFO, APP_GRAPH, "Entering main loop on lcore %u, graph 
%s(%p)\n", lcore_id,
+               qconf->name, graph);
+
+       while (likely(!force_quit))
+               rte_graph_walk(graph);
+
+       return 0;
+}
+
+void
+graph_stats_print(void)
+{
+       const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
+       const char clr[] = {27, '[', '2', 'J', '\0'};
+       struct rte_graph_cluster_stats_param s_param;
+       struct rte_graph_cluster_stats *stats;
+       const char *pattern = "worker_*";
+
+       /* Prepare stats object */
+       memset(&s_param, 0, sizeof(s_param));
+       s_param.f = stdout;
+       s_param.socket_id = SOCKET_ID_ANY;
+       s_param.graph_patterns = &pattern;
+       s_param.nb_graph_patterns = 1;
+
+       stats = rte_graph_cluster_stats_create(&s_param);
+       if (stats == NULL)
+               rte_exit(EXIT_FAILURE, "Unable to create stats object\n");
+
+       while (!force_quit) {
+               /* Clear screen and move to top left */
+               printf("%s%s", clr, topLeft);
+               rte_graph_cluster_stats_get(stats, 0);
+               rte_delay_ms(1E3);
+       }
+
+       rte_graph_cluster_stats_destroy(stats);
+}
+
+static void
+cli_graph(void *parsed_result, __rte_unused struct cmdline *cl, __rte_unused 
void *data)
+{
+       struct graph_config_cmd_tokens *res = parsed_result;
+       struct graph_config config;
+       char *model_name;
+       uint8_t model;
+       int rc;
+
+       model_name = res->model_name;
+       if (strcmp(model_name, "default") == 0) {
+               model = GRAPH_MODEL_RTC;
+       } else if (strcmp(model_name, "rtc") == 0) {
+               model = GRAPH_MODEL_RTC;
+       } else if (strcmp(model_name, "mcd") == 0) {
+               model = GRAPH_MODEL_MCD;
+       } else {
+               printf(MSG_ARG_NOT_FOUND, "model arguments");
+               return;
+       }
+
+       config.params.bsz = res->size;
+       config.params.tmo = res->ns;
+       config.params.coremask = res->mask;
+       config.model = model;
+       rc = graph_config_add(res->usecase, &config);
+       if (rc < 0)
+               printf(MSG_CMD_FAIL, "graph");
+}
+
+static void
+cli_graph_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%s\n",
+                "----------------------------- graph command help 
-----------------------------",
+                cmd_graph_help, "graph start");
+
+       len = strlen(conn->msg_out);
+       conn->msg_out_len_max -= len;
+}
+
+cmdline_parse_token_string_t graph_display_graph =
+       TOKEN_STRING_INITIALIZER(struct graph_stats_cmd_tokens, graph, "graph");
+cmdline_parse_token_string_t graph_display_stats =
+       TOKEN_STRING_INITIALIZER(struct graph_stats_cmd_tokens, stats, "stats");
+cmdline_parse_token_string_t graph_display_show =
+       TOKEN_STRING_INITIALIZER(struct graph_stats_cmd_tokens, show, "show");
+
+cmdline_parse_inst_t graph_stats_cmd_ctx = {
+       .f = cli_graph_stats,
+       .data = NULL,
+       .help_str = "graph stats show",
+       .tokens = {
+               (void *)&graph_display_graph,
+               (void *)&graph_display_stats,
+               (void *)&graph_display_show,
+               NULL,
+       },
+};
+
+cmdline_parse_token_string_t graph_config_start_graph =
+       TOKEN_STRING_INITIALIZER(struct graph_start_cmd_tokens, graph, "graph");
+cmdline_parse_token_string_t graph_config_start =
+       TOKEN_STRING_INITIALIZER(struct graph_start_cmd_tokens, start, "start");
+
+cmdline_parse_inst_t graph_start_cmd_ctx = {
+       .f = cli_graph_start,
+       .data = NULL,
+       .help_str = "graph start",
+       .tokens = {
+               (void *)&graph_config_start_graph,
+               (void *)&graph_config_start,
+               NULL,
+       },
+};
+
+cmdline_parse_token_string_t graph_config_add_graph =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, graph, 
"graph");
+cmdline_parse_token_string_t graph_config_add_usecase =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, usecase, NULL);
+cmdline_parse_token_string_t graph_config_add_coremask =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, coremask, 
"coremask");
+cmdline_parse_token_num_t graph_config_add_mask =
+       TOKEN_NUM_INITIALIZER(struct graph_config_cmd_tokens, mask, RTE_UINT64);
+cmdline_parse_token_string_t graph_config_add_bsz =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, bsz, "bsz");
+cmdline_parse_token_num_t graph_config_add_size =
+       TOKEN_NUM_INITIALIZER(struct graph_config_cmd_tokens, size, RTE_UINT16);
+cmdline_parse_token_string_t graph_config_add_tmo =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, tmo, "tmo");
+cmdline_parse_token_num_t graph_config_add_ns =
+       TOKEN_NUM_INITIALIZER(struct graph_config_cmd_tokens, ns, RTE_UINT64);
+cmdline_parse_token_string_t graph_config_add_model =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, model, 
"model");
+cmdline_parse_token_string_t graph_config_add_model_name =
+       TOKEN_STRING_INITIALIZER(struct graph_config_cmd_tokens, model_name, 
"rtc#mcd#default");
+
+cmdline_parse_inst_t graph_config_cmd_ctx = {
+       .f = cli_graph,
+       .data = NULL,
+       .help_str = cmd_graph_help,
+       .tokens = {
+               (void *)&graph_config_add_graph,
+               (void *)&graph_config_add_usecase,
+               (void *)&graph_config_add_coremask,
+               (void *)&graph_config_add_mask,
+               (void *)&graph_config_add_bsz,
+               (void *)&graph_config_add_size,
+               (void *)&graph_config_add_tmo,
+               (void *)&graph_config_add_ns,
+               (void *)&graph_config_add_model,
+               (void *)&graph_config_add_model_name,
+               NULL,
+       },
+};
+
+cmdline_parse_token_string_t graph_help_cmd =
+       TOKEN_STRING_INITIALIZER(struct graph_help_cmd_tokens, help, "help");
+cmdline_parse_token_string_t graph_help_graph =
+       TOKEN_STRING_INITIALIZER(struct graph_help_cmd_tokens, graph, "graph");
+
+cmdline_parse_inst_t graph_help_cmd_ctx = {
+       .f = cli_graph_help,
+       .data = NULL,
+       .help_str = "",
+       .tokens = {
+               (void *)&graph_help_cmd,
+               (void *)&graph_help_graph,
+               NULL,
+       },
+};
diff --git a/app/graph/graph.h b/app/graph/graph.h
new file mode 100644
index 0000000000..f431ae10d2
--- /dev/null
+++ b/app/graph/graph.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 Marvell.
+ */
+
+#ifndef APP_GRAPH_H
+#define APP_GRAPH_H
+
+#include <cmdline_parse.h>
+
+extern cmdline_parse_inst_t graph_config_cmd_ctx;
+extern cmdline_parse_inst_t graph_start_cmd_ctx;
+extern cmdline_parse_inst_t graph_stats_cmd_ctx;
+extern cmdline_parse_inst_t graph_help_cmd_ctx;
+
+int graph_walk_start(void *conf);
+void graph_stats_print(void);
+
+#endif
diff --git a/app/graph/graph_priv.h b/app/graph/graph_priv.h
new file mode 100644
index 0000000000..c61e20672a
--- /dev/null
+++ b/app/graph/graph_priv.h
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2023 Marvell.
+ */
+
+#ifndef APP_GRAPH_PRIV_H
+#define APP_GRAPH_PRIV_H
+
+#define MAX_GRAPH_USECASES 32
+
+struct graph_help_cmd_tokens {
+       cmdline_fixed_string_t help;
+       cmdline_fixed_string_t graph;
+};
+
+struct graph_start_cmd_tokens {
+       cmdline_fixed_string_t graph;
+       cmdline_fixed_string_t start;
+};
+
+struct graph_stats_cmd_tokens {
+       cmdline_fixed_string_t show;
+       cmdline_fixed_string_t graph;
+       cmdline_fixed_string_t stats;
+};
+
+struct graph_config_cmd_tokens {
+       cmdline_fixed_string_t graph;
+       cmdline_fixed_string_t usecase;
+       cmdline_fixed_string_t bsz;
+       cmdline_fixed_string_t tmo;
+       cmdline_fixed_string_t coremask;
+       cmdline_fixed_string_t model;
+       cmdline_fixed_string_t model_name;
+       uint16_t size;
+       uint64_t ns;
+       uint64_t mask;
+};
+
+enum graph_model {
+       GRAPH_MODEL_RTC = 0x01,
+       GRAPH_MODEL_MCD = 0x02,
+};
+
+struct usecases {
+       char name[32];
+       bool enabled;
+};
+
+struct usecase_params {
+       uint64_t coremask;
+       uint32_t bsz;
+       uint32_t tmo;
+};
+
+struct graph_config {
+       struct usecases usecases[MAX_GRAPH_USECASES];
+       struct usecase_params params;
+       enum graph_model model;
+};
+
+#endif
diff --git a/app/graph/meson.build b/app/graph/meson.build
index caf2d20735..6292fc9684 100644
--- a/app/graph/meson.build
+++ b/app/graph/meson.build
@@ -13,6 +13,7 @@ sources = files(
         'conn.c',
         'ethdev_rx.c',
         'ethdev.c',
+        'graph.c',
         'ip4_route.c',
         'ip6_route.c',
         'main.c',
diff --git a/app/graph/module_api.h b/app/graph/module_api.h
index 0a4e383f50..8417c2d3dd 100644
--- a/app/graph/module_api.h
+++ b/app/graph/module_api.h
@@ -12,6 +12,7 @@
 #include "conn.h"
 #include "ethdev.h"
 #include "ethdev_rx.h"
+#include "graph.h"
 #include "mempool.h"
 #include "neigh.h"
 #include "route.h"
-- 
2.25.1

Reply via email to