xref: /dpdk/app/graph/ethdev_rx.c (revision da7e701151ea8b742d4c38ace3e4fefd1b4507fc)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2023 Marvell.
3  */
4 
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <cmdline_parse.h>
9 #include <cmdline_parse_num.h>
10 #include <cmdline_parse_string.h>
11 #include <cmdline_socket.h>
12 #include <rte_ethdev.h>
13 
14 #include "ethdev_rx_priv.h"
15 #include "module_api.h"
16 
17 static const char
18 cmd_ethdev_rx_help[] = "ethdev_rx map port <ethdev_name> queue <q_num> core <core_id>";
19 
20 static struct lcore_params lcore_params_array[ETHDEV_RX_LCORE_PARAMS_MAX];
21 struct rte_node_ethdev_config ethdev_conf[RTE_MAX_ETHPORTS];
22 struct lcore_params *lcore_params = lcore_params_array;
23 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
24 uint16_t nb_lcore_params;
25 
26 static void
27 rx_map_configure(uint8_t port_id, uint32_t queue, uint32_t core)
28 {
29 	uint8_t n_rx_queue;
30 
31 	n_rx_queue = lcore_conf[core].n_rx_queue;
32 	lcore_conf[core].rx_queue_list[n_rx_queue].port_id = port_id;
33 	lcore_conf[core].rx_queue_list[n_rx_queue].queue_id = queue;
34 	lcore_conf[core].n_rx_queue++;
35 }
36 
37 uint8_t
38 ethdev_rx_num_rx_queues_get(uint16_t port)
39 {
40 	int queue = -1;
41 	uint16_t i;
42 
43 	for (i = 0; i < nb_lcore_params; ++i) {
44 		if (lcore_params[i].port_id == port) {
45 			if (lcore_params[i].queue_id == queue + 1)
46 				queue = lcore_params[i].queue_id;
47 			else
48 				rte_exit(EXIT_FAILURE,
49 					 "Queue ids of the port %d must be"
50 					 " in sequence and must start with 0\n",
51 					 lcore_params[i].port_id);
52 		}
53 	}
54 
55 	return (uint8_t)(++queue);
56 }
57 
58 static int
59 ethdev_rx_map_add(char *name, uint32_t queue, uint32_t core)
60 {
61 	uint64_t coremask;
62 	uint16_t port_id;
63 	int rc;
64 
65 	if (nb_lcore_params >= ETHDEV_RX_LCORE_PARAMS_MAX)
66 		return -EINVAL;
67 
68 	rc = rte_eth_dev_get_port_by_name(name, &port_id);
69 	if (rc)
70 		return -EINVAL;
71 
72 	coremask = graph_coremask_get();
73 
74 	if (!(coremask & (1 << core)))
75 		return -EINVAL;
76 
77 	rx_map_configure(port_id, queue, core);
78 
79 	lcore_params_array[nb_lcore_params].port_id = port_id;
80 	lcore_params_array[nb_lcore_params].queue_id = queue;
81 	lcore_params_array[nb_lcore_params].lcore_id = core;
82 	nb_lcore_params++;
83 	return 0;
84 }
85 
86 static void
87 cli_ethdev_rx_help(__rte_unused void *parsed_result, __rte_unused struct cmdline *cl,
88 		   __rte_unused void *data)
89 {
90 	size_t len;
91 
92 	len = strlen(conn->msg_out);
93 	conn->msg_out += len;
94 	snprintf(conn->msg_out, conn->msg_out_len_max, "\n%s\n%s\n",
95 		 "----------------------------- ethdev_rx command help -----------------------------",
96 		 cmd_ethdev_rx_help);
97 
98 	len = strlen(conn->msg_out);
99 	conn->msg_out_len_max -= len;
100 }
101 
102 static void
103 cli_ethdev_rx(void *parsed_result, __rte_unused struct cmdline *cl, void *data __rte_unused)
104 {
105 	struct ethdev_rx_cmd_tokens *res = parsed_result;
106 	int rc = -EINVAL;
107 
108 	rc = ethdev_rx_map_add(res->dev, res->qid, res->core_id);
109 	if (rc < 0) {
110 		cli_exit();
111 		printf(MSG_CMD_FAIL, res->cmd);
112 		rte_exit(EXIT_FAILURE, "input core is Invalid\n");
113 	}
114 
115 }
116 
117 cmdline_parse_token_string_t ethdev_rx_cmd =
118 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, cmd, "ethdev_rx");
119 cmdline_parse_token_string_t ethdev_rx_map =
120 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, map, "map");
121 cmdline_parse_token_string_t ethdev_rx_port =
122 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, port, "port");
123 cmdline_parse_token_string_t ethdev_rx_dev =
124 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, dev, NULL);
125 cmdline_parse_token_string_t ethdev_rx_queue =
126 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, queue, "queue");
127 cmdline_parse_token_num_t ethdev_rx_qid =
128 	TOKEN_NUM_INITIALIZER(struct ethdev_rx_cmd_tokens, qid, RTE_UINT32);
129 cmdline_parse_token_string_t ethdev_rx_core =
130 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_cmd_tokens, core, "core");
131 cmdline_parse_token_num_t ethdev_rx_core_id =
132 	TOKEN_NUM_INITIALIZER(struct ethdev_rx_cmd_tokens, core_id, RTE_UINT32);
133 
134 cmdline_parse_inst_t ethdev_rx_cmd_ctx = {
135 	.f = cli_ethdev_rx,
136 	.data = NULL,
137 	.help_str = cmd_ethdev_rx_help,
138 	.tokens = {
139 		(void *)&ethdev_rx_cmd,
140 		(void *)&ethdev_rx_map,
141 		(void *)&ethdev_rx_port,
142 		(void *)&ethdev_rx_dev,
143 		(void *)&ethdev_rx_queue,
144 		(void *)&ethdev_rx_qid,
145 		(void *)&ethdev_rx_core,
146 		(void *)&ethdev_rx_core_id,
147 		NULL,
148 	},
149 };
150 
151 cmdline_parse_token_string_t ethdev_rx_help_cmd =
152 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_help_cmd_tokens, cmd, "help");
153 cmdline_parse_token_string_t ethdev_rx_help_module =
154 	TOKEN_STRING_INITIALIZER(struct ethdev_rx_help_cmd_tokens, module, "ethdev_rx");
155 
156 cmdline_parse_inst_t ethdev_rx_help_cmd_ctx = {
157 	.f = cli_ethdev_rx_help,
158 	.data = NULL,
159 	.help_str = "",
160 	.tokens = {
161 		(void *)&ethdev_rx_help_cmd,
162 		(void *)&ethdev_rx_help_module,
163 		NULL,
164 	},
165 };
166