xref: /dpdk/examples/pipeline/cli.c (revision 96893df75bf50a82cabd8debe3492d564a5d13d5)
15074e1d5SCristian Dumitrescu /* SPDX-License-Identifier: BSD-3-Clause
25074e1d5SCristian Dumitrescu  * Copyright(c) 2020 Intel Corporation
35074e1d5SCristian Dumitrescu  */
45074e1d5SCristian Dumitrescu 
572b452c5SDmitry Kozlyuk #include <ctype.h>
65074e1d5SCristian Dumitrescu #include <stdio.h>
75074e1d5SCristian Dumitrescu #include <stdint.h>
85074e1d5SCristian Dumitrescu #include <stdlib.h>
95074e1d5SCristian Dumitrescu #include <string.h>
106bc14d9fSCristian Dumitrescu #include <unistd.h>
115074e1d5SCristian Dumitrescu 
125074e1d5SCristian Dumitrescu #include <rte_common.h>
135074e1d5SCristian Dumitrescu #include <rte_ethdev.h>
14607dd517SCristian Dumitrescu #include <rte_ring.h>
155074e1d5SCristian Dumitrescu #include <rte_swx_port_ethdev.h>
1677a41301SCristian Dumitrescu #include <rte_swx_port_ring.h>
175074e1d5SCristian Dumitrescu #include <rte_swx_port_source_sink.h>
18e2b8dc52SVenkata Suresh Kumar P #include <rte_swx_port_fd.h>
195074e1d5SCristian Dumitrescu #include <rte_swx_pipeline.h>
205074e1d5SCristian Dumitrescu #include <rte_swx_ctl.h>
213b0cc5fbSCristian Dumitrescu #include <rte_swx_ipsec.h>
222df20a1dSDavid Marchand #include <rte_string_fns.h>
235074e1d5SCristian Dumitrescu 
245074e1d5SCristian Dumitrescu #include "cli.h"
255074e1d5SCristian Dumitrescu 
265074e1d5SCristian Dumitrescu #include "obj.h"
275074e1d5SCristian Dumitrescu #include "thread.h"
285074e1d5SCristian Dumitrescu 
295074e1d5SCristian Dumitrescu #ifndef CMD_MAX_TOKENS
305074e1d5SCristian Dumitrescu #define CMD_MAX_TOKENS     256
315074e1d5SCristian Dumitrescu #endif
325074e1d5SCristian Dumitrescu 
336bc14d9fSCristian Dumitrescu #ifndef MAX_LINE_SIZE
346bc14d9fSCristian Dumitrescu #define MAX_LINE_SIZE 2048
356bc14d9fSCristian Dumitrescu #endif
366bc14d9fSCristian Dumitrescu 
375074e1d5SCristian Dumitrescu #define MSG_OUT_OF_MEMORY   "Not enough memory.\n"
385074e1d5SCristian Dumitrescu #define MSG_CMD_UNKNOWN     "Unknown command \"%s\".\n"
395074e1d5SCristian Dumitrescu #define MSG_CMD_UNIMPLEM    "Command \"%s\" not implemented.\n"
405074e1d5SCristian Dumitrescu #define MSG_ARG_NOT_ENOUGH  "Not enough arguments for command \"%s\".\n"
415074e1d5SCristian Dumitrescu #define MSG_ARG_TOO_MANY    "Too many arguments for command \"%s\".\n"
425074e1d5SCristian Dumitrescu #define MSG_ARG_MISMATCH    "Wrong number of arguments for command \"%s\".\n"
435074e1d5SCristian Dumitrescu #define MSG_ARG_NOT_FOUND   "Argument \"%s\" not found.\n"
445074e1d5SCristian Dumitrescu #define MSG_ARG_INVALID     "Invalid value for argument \"%s\".\n"
455074e1d5SCristian Dumitrescu #define MSG_FILE_ERR        "Error in file \"%s\" at line %u.\n"
465074e1d5SCristian Dumitrescu #define MSG_FILE_NOT_ENOUGH "Not enough rules in file \"%s\".\n"
475074e1d5SCristian Dumitrescu #define MSG_CMD_FAIL        "Command \"%s\" failed.\n"
485074e1d5SCristian Dumitrescu 
495074e1d5SCristian Dumitrescu static int
505074e1d5SCristian Dumitrescu parser_read_uint64(uint64_t *value, const char *p)
515074e1d5SCristian Dumitrescu {
525074e1d5SCristian Dumitrescu 	char *next;
535074e1d5SCristian Dumitrescu 	uint64_t val;
545074e1d5SCristian Dumitrescu 
552df20a1dSDavid Marchand 	p = rte_str_skip_leading_spaces(p);
565074e1d5SCristian Dumitrescu 	if (!isdigit(*p))
575074e1d5SCristian Dumitrescu 		return -EINVAL;
585074e1d5SCristian Dumitrescu 
590d644eb6SChurchill Khangar 	val = strtoul(p, &next, 0);
605074e1d5SCristian Dumitrescu 	if (p == next)
615074e1d5SCristian Dumitrescu 		return -EINVAL;
625074e1d5SCristian Dumitrescu 
635074e1d5SCristian Dumitrescu 	p = next;
645074e1d5SCristian Dumitrescu 	switch (*p) {
655074e1d5SCristian Dumitrescu 	case 'T':
665074e1d5SCristian Dumitrescu 		val *= 1024ULL;
675074e1d5SCristian Dumitrescu 		/* fall through */
685074e1d5SCristian Dumitrescu 	case 'G':
695074e1d5SCristian Dumitrescu 		val *= 1024ULL;
705074e1d5SCristian Dumitrescu 		/* fall through */
715074e1d5SCristian Dumitrescu 	case 'M':
725074e1d5SCristian Dumitrescu 		val *= 1024ULL;
735074e1d5SCristian Dumitrescu 		/* fall through */
745074e1d5SCristian Dumitrescu 	case 'k':
755074e1d5SCristian Dumitrescu 	case 'K':
765074e1d5SCristian Dumitrescu 		val *= 1024ULL;
775074e1d5SCristian Dumitrescu 		p++;
785074e1d5SCristian Dumitrescu 		break;
795074e1d5SCristian Dumitrescu 	}
805074e1d5SCristian Dumitrescu 
812df20a1dSDavid Marchand 	p = rte_str_skip_leading_spaces(p);
825074e1d5SCristian Dumitrescu 	if (*p != '\0')
835074e1d5SCristian Dumitrescu 		return -EINVAL;
845074e1d5SCristian Dumitrescu 
855074e1d5SCristian Dumitrescu 	*value = val;
865074e1d5SCristian Dumitrescu 	return 0;
875074e1d5SCristian Dumitrescu }
885074e1d5SCristian Dumitrescu 
895074e1d5SCristian Dumitrescu static int
905074e1d5SCristian Dumitrescu parser_read_uint32(uint32_t *value, const char *p)
915074e1d5SCristian Dumitrescu {
925074e1d5SCristian Dumitrescu 	uint64_t val = 0;
935074e1d5SCristian Dumitrescu 	int ret = parser_read_uint64(&val, p);
945074e1d5SCristian Dumitrescu 
955074e1d5SCristian Dumitrescu 	if (ret < 0)
965074e1d5SCristian Dumitrescu 		return ret;
975074e1d5SCristian Dumitrescu 
985074e1d5SCristian Dumitrescu 	if (val > UINT32_MAX)
995074e1d5SCristian Dumitrescu 		return -ERANGE;
1005074e1d5SCristian Dumitrescu 
1015074e1d5SCristian Dumitrescu 	*value = val;
1025074e1d5SCristian Dumitrescu 	return 0;
1035074e1d5SCristian Dumitrescu }
1045074e1d5SCristian Dumitrescu 
1055074e1d5SCristian Dumitrescu #define PARSE_DELIMITER " \f\n\r\t\v"
1065074e1d5SCristian Dumitrescu 
1075074e1d5SCristian Dumitrescu static int
1085074e1d5SCristian Dumitrescu parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
1095074e1d5SCristian Dumitrescu {
1105074e1d5SCristian Dumitrescu 	uint32_t i;
1115074e1d5SCristian Dumitrescu 
1125074e1d5SCristian Dumitrescu 	if ((string == NULL) ||
1135074e1d5SCristian Dumitrescu 		(tokens == NULL) ||
1145074e1d5SCristian Dumitrescu 		(*n_tokens < 1))
1155074e1d5SCristian Dumitrescu 		return -EINVAL;
1165074e1d5SCristian Dumitrescu 
1175074e1d5SCristian Dumitrescu 	for (i = 0; i < *n_tokens; i++) {
1185074e1d5SCristian Dumitrescu 		tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
1195074e1d5SCristian Dumitrescu 		if (tokens[i] == NULL)
1205074e1d5SCristian Dumitrescu 			break;
1215074e1d5SCristian Dumitrescu 	}
1225074e1d5SCristian Dumitrescu 
1235074e1d5SCristian Dumitrescu 	if ((i == *n_tokens) && strtok_r(string, PARSE_DELIMITER, &string))
1245074e1d5SCristian Dumitrescu 		return -E2BIG;
1255074e1d5SCristian Dumitrescu 
1265074e1d5SCristian Dumitrescu 	*n_tokens = i;
1275074e1d5SCristian Dumitrescu 	return 0;
1285074e1d5SCristian Dumitrescu }
1295074e1d5SCristian Dumitrescu 
1305074e1d5SCristian Dumitrescu static int
1315074e1d5SCristian Dumitrescu is_comment(char *in)
1325074e1d5SCristian Dumitrescu {
1335074e1d5SCristian Dumitrescu 	if ((strlen(in) && index("!#%;", in[0])) ||
1345074e1d5SCristian Dumitrescu 		(strncmp(in, "//", 2) == 0) ||
1355074e1d5SCristian Dumitrescu 		(strncmp(in, "--", 2) == 0))
1365074e1d5SCristian Dumitrescu 		return 1;
1375074e1d5SCristian Dumitrescu 
1385074e1d5SCristian Dumitrescu 	return 0;
1395074e1d5SCristian Dumitrescu }
1405074e1d5SCristian Dumitrescu 
14183f58a7bSCristian Dumitrescu static void
14283f58a7bSCristian Dumitrescu table_entry_free(struct rte_swx_table_entry *entry)
14383f58a7bSCristian Dumitrescu {
14483f58a7bSCristian Dumitrescu 	if (!entry)
14583f58a7bSCristian Dumitrescu 		return;
14683f58a7bSCristian Dumitrescu 
14783f58a7bSCristian Dumitrescu 	free(entry->key);
14883f58a7bSCristian Dumitrescu 	free(entry->key_mask);
14983f58a7bSCristian Dumitrescu 	free(entry->action_data);
15083f58a7bSCristian Dumitrescu 	free(entry);
15183f58a7bSCristian Dumitrescu }
15283f58a7bSCristian Dumitrescu 
15383f58a7bSCristian Dumitrescu static struct rte_swx_table_entry *
15483f58a7bSCristian Dumitrescu parse_table_entry(struct rte_swx_ctl_pipeline *p,
15583f58a7bSCristian Dumitrescu 		  char *table_name,
15683f58a7bSCristian Dumitrescu 		  char **tokens,
15783f58a7bSCristian Dumitrescu 		  uint32_t n_tokens)
15883f58a7bSCristian Dumitrescu {
15983f58a7bSCristian Dumitrescu 	struct rte_swx_table_entry *entry;
16083f58a7bSCristian Dumitrescu 	char *line;
16183f58a7bSCristian Dumitrescu 	uint32_t i;
16283f58a7bSCristian Dumitrescu 
16383f58a7bSCristian Dumitrescu 	/* Buffer allocation. */
16483f58a7bSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
16583f58a7bSCristian Dumitrescu 	if (!line)
16683f58a7bSCristian Dumitrescu 		return NULL;
16783f58a7bSCristian Dumitrescu 
16883f58a7bSCristian Dumitrescu 	/* Copy tokens to buffer. Since the tokens were initially part of a buffer of size
16983f58a7bSCristian Dumitrescu 	 * MAX_LINE_LENGTH, it is guaranteed that putting back some of them into a buffer of the
17083f58a7bSCristian Dumitrescu 	 * same size separated by a single space will not result in buffer overrun.
17183f58a7bSCristian Dumitrescu 	 */
17283f58a7bSCristian Dumitrescu 	line[0] = 0;
17383f58a7bSCristian Dumitrescu 	for (i = 0; i < n_tokens; i++) {
17483f58a7bSCristian Dumitrescu 		if (i)
17583f58a7bSCristian Dumitrescu 			strcat(line, " ");
17683f58a7bSCristian Dumitrescu 
17783f58a7bSCristian Dumitrescu 		strcat(line, tokens[i]);
17883f58a7bSCristian Dumitrescu 	}
17983f58a7bSCristian Dumitrescu 
18083f58a7bSCristian Dumitrescu 	/* Read the table entry from the input buffer. */
18183f58a7bSCristian Dumitrescu 	entry = rte_swx_ctl_pipeline_table_entry_read(p, table_name, line, NULL);
18283f58a7bSCristian Dumitrescu 
18383f58a7bSCristian Dumitrescu 	/* Buffer free. */
18483f58a7bSCristian Dumitrescu 	free(line);
18583f58a7bSCristian Dumitrescu 
18683f58a7bSCristian Dumitrescu 	return entry;
18783f58a7bSCristian Dumitrescu }
18883f58a7bSCristian Dumitrescu 
1895074e1d5SCristian Dumitrescu static const char cmd_mempool_help[] =
19002d36ef6SCristian Dumitrescu "mempool <mempool_name> "
19102d36ef6SCristian Dumitrescu "meta <mbuf_private_size> "
19202d36ef6SCristian Dumitrescu "pkt <pkt_buffer_size> "
19302d36ef6SCristian Dumitrescu "pool <pool_size> "
19402d36ef6SCristian Dumitrescu "cache <cache_size> "
19502d36ef6SCristian Dumitrescu "numa <numa_node>\n";
1965074e1d5SCristian Dumitrescu 
1975074e1d5SCristian Dumitrescu static void
1985074e1d5SCristian Dumitrescu cmd_mempool(char **tokens,
1995074e1d5SCristian Dumitrescu 	    uint32_t n_tokens,
2005074e1d5SCristian Dumitrescu 	    char *out,
2015074e1d5SCristian Dumitrescu 	    size_t out_size,
20202d36ef6SCristian Dumitrescu 	    void *obj __rte_unused)
2035074e1d5SCristian Dumitrescu {
20402d36ef6SCristian Dumitrescu 	struct rte_mempool *mp;
20502d36ef6SCristian Dumitrescu 	char *mempool_name;
20602d36ef6SCristian Dumitrescu 	uint32_t mbuf_private_size, pkt_buffer_size, pool_size, cache_size, numa_node;
2075074e1d5SCristian Dumitrescu 
20802d36ef6SCristian Dumitrescu 	if (n_tokens != 12) {
2095074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2105074e1d5SCristian Dumitrescu 		return;
2115074e1d5SCristian Dumitrescu 	}
2125074e1d5SCristian Dumitrescu 
21302d36ef6SCristian Dumitrescu 	mempool_name = tokens[1];
2145074e1d5SCristian Dumitrescu 
21502d36ef6SCristian Dumitrescu 	if (strcmp(tokens[2], "meta")) {
21602d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meta");
2175074e1d5SCristian Dumitrescu 		return;
2185074e1d5SCristian Dumitrescu 	}
2195074e1d5SCristian Dumitrescu 
22002d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&mbuf_private_size, tokens[3])) {
22102d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "mbuf_private_size");
2225074e1d5SCristian Dumitrescu 		return;
2235074e1d5SCristian Dumitrescu 	}
2245074e1d5SCristian Dumitrescu 
22502d36ef6SCristian Dumitrescu 	if (strcmp(tokens[4], "pkt")) {
22602d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pkt");
22702d36ef6SCristian Dumitrescu 		return;
22802d36ef6SCristian Dumitrescu 	}
22902d36ef6SCristian Dumitrescu 
23002d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&pkt_buffer_size, tokens[5])) {
23102d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pkt_buffer_size");
23202d36ef6SCristian Dumitrescu 		return;
23302d36ef6SCristian Dumitrescu 	}
23402d36ef6SCristian Dumitrescu 
23502d36ef6SCristian Dumitrescu 	if (strcmp(tokens[6], "pool")) {
2365074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
2375074e1d5SCristian Dumitrescu 		return;
2385074e1d5SCristian Dumitrescu 	}
2395074e1d5SCristian Dumitrescu 
24002d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&pool_size, tokens[7])) {
2415074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
2425074e1d5SCristian Dumitrescu 		return;
2435074e1d5SCristian Dumitrescu 	}
2445074e1d5SCristian Dumitrescu 
24502d36ef6SCristian Dumitrescu 	if (strcmp(tokens[8], "cache")) {
2465074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
2475074e1d5SCristian Dumitrescu 		return;
2485074e1d5SCristian Dumitrescu 	}
2495074e1d5SCristian Dumitrescu 
25002d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&cache_size, tokens[9])) {
2515074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
2525074e1d5SCristian Dumitrescu 		return;
2535074e1d5SCristian Dumitrescu 	}
2545074e1d5SCristian Dumitrescu 
25502d36ef6SCristian Dumitrescu 	if (strcmp(tokens[10], "numa")) {
25602d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
2575074e1d5SCristian Dumitrescu 		return;
2585074e1d5SCristian Dumitrescu 	}
2595074e1d5SCristian Dumitrescu 
26002d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[11])) {
26102d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
2625074e1d5SCristian Dumitrescu 		return;
2635074e1d5SCristian Dumitrescu 	}
2645074e1d5SCristian Dumitrescu 
26502d36ef6SCristian Dumitrescu 	mp = rte_pktmbuf_pool_create(mempool_name,
26602d36ef6SCristian Dumitrescu 				     pool_size,
26702d36ef6SCristian Dumitrescu 				     cache_size,
26802d36ef6SCristian Dumitrescu 				     mbuf_private_size,
26902d36ef6SCristian Dumitrescu 				     pkt_buffer_size,
27002d36ef6SCristian Dumitrescu 				     numa_node);
27102d36ef6SCristian Dumitrescu 	if (!mp) {
2725074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
2735074e1d5SCristian Dumitrescu 		return;
2745074e1d5SCristian Dumitrescu 	}
2755074e1d5SCristian Dumitrescu }
2765074e1d5SCristian Dumitrescu 
277f31c80f8SCristian Dumitrescu static const char cmd_ethdev_help[] =
278f31c80f8SCristian Dumitrescu "ethdev <ethdev_name>\n"
2795074e1d5SCristian Dumitrescu "   rxq <n_queues> <queue_size> <mempool_name>\n"
2805074e1d5SCristian Dumitrescu "   txq <n_queues> <queue_size>\n"
2815074e1d5SCristian Dumitrescu "   promiscuous on | off\n"
2825074e1d5SCristian Dumitrescu "   [rss <qid_0> ... <qid_n>]\n";
2835074e1d5SCristian Dumitrescu 
2845074e1d5SCristian Dumitrescu static void
285f31c80f8SCristian Dumitrescu cmd_ethdev(char **tokens,
2865074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
2875074e1d5SCristian Dumitrescu 	char *out,
2885074e1d5SCristian Dumitrescu 	size_t out_size,
28978dffe31SCristian Dumitrescu 	void *obj __rte_unused)
2905074e1d5SCristian Dumitrescu {
29178dffe31SCristian Dumitrescu 	struct ethdev_params p;
29278dffe31SCristian Dumitrescu 	struct ethdev_params_rss rss;
2935074e1d5SCristian Dumitrescu 	char *name;
29478dffe31SCristian Dumitrescu 	int status;
2955074e1d5SCristian Dumitrescu 
2965074e1d5SCristian Dumitrescu 	memset(&p, 0, sizeof(p));
29778dffe31SCristian Dumitrescu 	memset(&rss, 0, sizeof(rss));
2985074e1d5SCristian Dumitrescu 
29978dffe31SCristian Dumitrescu 	if (n_tokens < 11 || n_tokens > 12 + ETHDEV_RXQ_RSS_MAX) {
3005074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
3015074e1d5SCristian Dumitrescu 		return;
3025074e1d5SCristian Dumitrescu 	}
3035074e1d5SCristian Dumitrescu 	name = tokens[1];
3045074e1d5SCristian Dumitrescu 
305f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[2], "rxq") != 0) {
3065074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
3075074e1d5SCristian Dumitrescu 		return;
3085074e1d5SCristian Dumitrescu 	}
3095074e1d5SCristian Dumitrescu 
310f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.rx.n_queues, tokens[3]) != 0) {
3115074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
3125074e1d5SCristian Dumitrescu 		return;
3135074e1d5SCristian Dumitrescu 	}
314f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.rx.queue_size, tokens[4]) != 0) {
3155074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
3165074e1d5SCristian Dumitrescu 		return;
3175074e1d5SCristian Dumitrescu 	}
3185074e1d5SCristian Dumitrescu 
319f31c80f8SCristian Dumitrescu 	p.rx.mempool_name = tokens[5];
3205074e1d5SCristian Dumitrescu 
321f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[6], "txq") != 0) {
3225074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
3235074e1d5SCristian Dumitrescu 		return;
3245074e1d5SCristian Dumitrescu 	}
3255074e1d5SCristian Dumitrescu 
326f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.tx.n_queues, tokens[7]) != 0) {
3275074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
3285074e1d5SCristian Dumitrescu 		return;
3295074e1d5SCristian Dumitrescu 	}
3305074e1d5SCristian Dumitrescu 
331f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.tx.queue_size, tokens[8]) != 0) {
3325074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
3335074e1d5SCristian Dumitrescu 		return;
3345074e1d5SCristian Dumitrescu 	}
3355074e1d5SCristian Dumitrescu 
336f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[9], "promiscuous") != 0) {
3375074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
3385074e1d5SCristian Dumitrescu 		return;
3395074e1d5SCristian Dumitrescu 	}
3405074e1d5SCristian Dumitrescu 
341f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[10], "on") == 0)
3425074e1d5SCristian Dumitrescu 		p.promiscuous = 1;
343f31c80f8SCristian Dumitrescu 	else if (strcmp(tokens[10], "off") == 0)
3445074e1d5SCristian Dumitrescu 		p.promiscuous = 0;
3455074e1d5SCristian Dumitrescu 	else {
3465074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
3475074e1d5SCristian Dumitrescu 		return;
3485074e1d5SCristian Dumitrescu 	}
3495074e1d5SCristian Dumitrescu 
3505074e1d5SCristian Dumitrescu 	/* RSS */
3515074e1d5SCristian Dumitrescu 	p.rx.rss = NULL;
352f31c80f8SCristian Dumitrescu 	if (n_tokens > 11) {
3535074e1d5SCristian Dumitrescu 		uint32_t queue_id, i;
3545074e1d5SCristian Dumitrescu 
355f31c80f8SCristian Dumitrescu 		if (strcmp(tokens[11], "rss") != 0) {
3565074e1d5SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
3575074e1d5SCristian Dumitrescu 			return;
3585074e1d5SCristian Dumitrescu 		}
3595074e1d5SCristian Dumitrescu 
3605074e1d5SCristian Dumitrescu 		p.rx.rss = &rss;
3615074e1d5SCristian Dumitrescu 
3625074e1d5SCristian Dumitrescu 		rss.n_queues = 0;
363f31c80f8SCristian Dumitrescu 		for (i = 12; i < n_tokens; i++) {
3645074e1d5SCristian Dumitrescu 			if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
3655074e1d5SCristian Dumitrescu 				snprintf(out, out_size, MSG_ARG_INVALID,
3665074e1d5SCristian Dumitrescu 					"queue_id");
3675074e1d5SCristian Dumitrescu 				return;
3685074e1d5SCristian Dumitrescu 			}
3695074e1d5SCristian Dumitrescu 
3705074e1d5SCristian Dumitrescu 			rss.queue_id[rss.n_queues] = queue_id;
3715074e1d5SCristian Dumitrescu 			rss.n_queues++;
3725074e1d5SCristian Dumitrescu 		}
3735074e1d5SCristian Dumitrescu 	}
3745074e1d5SCristian Dumitrescu 
37578dffe31SCristian Dumitrescu 	status = ethdev_config(name, &p);
37678dffe31SCristian Dumitrescu 	if (status) {
3775074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
3785074e1d5SCristian Dumitrescu 		return;
3795074e1d5SCristian Dumitrescu 	}
3805074e1d5SCristian Dumitrescu }
3815074e1d5SCristian Dumitrescu 
3825074e1d5SCristian Dumitrescu static void
38378dffe31SCristian Dumitrescu ethdev_show(uint16_t port_id, char **out, size_t *out_size)
3845074e1d5SCristian Dumitrescu {
38578dffe31SCristian Dumitrescu 	char name[RTE_ETH_NAME_MAX_LEN];
38678dffe31SCristian Dumitrescu 	struct rte_eth_dev_info info;
3875074e1d5SCristian Dumitrescu 	struct rte_eth_stats stats;
38878dffe31SCristian Dumitrescu 	struct rte_ether_addr addr;
38978dffe31SCristian Dumitrescu 	struct rte_eth_link link;
39078dffe31SCristian Dumitrescu 	uint32_t length;
39178dffe31SCristian Dumitrescu 	uint16_t mtu = 0;
3925074e1d5SCristian Dumitrescu 
39378dffe31SCristian Dumitrescu 	if (!rte_eth_dev_is_valid_port(port_id))
3945074e1d5SCristian Dumitrescu 		return;
3955074e1d5SCristian Dumitrescu 
39678dffe31SCristian Dumitrescu 	rte_eth_dev_get_name_by_port(port_id, name);
39778dffe31SCristian Dumitrescu 	rte_eth_dev_info_get(port_id, &info);
39878dffe31SCristian Dumitrescu 	rte_eth_stats_get(port_id, &stats);
39978dffe31SCristian Dumitrescu 	rte_eth_macaddr_get(port_id, &addr);
40078dffe31SCristian Dumitrescu 	rte_eth_link_get(port_id, &link);
40178dffe31SCristian Dumitrescu 	rte_eth_dev_get_mtu(port_id, &mtu);
4025074e1d5SCristian Dumitrescu 
40378dffe31SCristian Dumitrescu 	snprintf(*out, *out_size,
4045074e1d5SCristian Dumitrescu 		 "%s: flags=<%s> mtu %u\n"
405c2c4f87bSAman Deep Singh 		 "\tether " RTE_ETHER_ADDR_PRT_FMT " rxqueues %u txqueues %u\n"
4065074e1d5SCristian Dumitrescu 		 "\tport# %u  speed %s\n"
4075074e1d5SCristian Dumitrescu 		 "\tRX packets %" PRIu64"  bytes %" PRIu64"\n"
4085074e1d5SCristian Dumitrescu 		 "\tRX errors %" PRIu64"  missed %" PRIu64"  no-mbuf %" PRIu64"\n"
4095074e1d5SCristian Dumitrescu 		 "\tTX packets %" PRIu64"  bytes %" PRIu64"\n"
41078dffe31SCristian Dumitrescu 		 "\tTX errors %" PRIu64"\n\n",
41178dffe31SCristian Dumitrescu 		 name,
41278dffe31SCristian Dumitrescu 		 link.link_status ? "UP" : "DOWN",
4135074e1d5SCristian Dumitrescu 		 mtu,
41478dffe31SCristian Dumitrescu 		 RTE_ETHER_ADDR_BYTES(&addr),
41578dffe31SCristian Dumitrescu 		 info.nb_rx_queues,
41678dffe31SCristian Dumitrescu 		 info.nb_tx_queues,
41778dffe31SCristian Dumitrescu 		 port_id,
41878dffe31SCristian Dumitrescu 		 rte_eth_link_speed_to_str(link.link_speed),
4195074e1d5SCristian Dumitrescu 		 stats.ipackets,
4205074e1d5SCristian Dumitrescu 		 stats.ibytes,
4215074e1d5SCristian Dumitrescu 		 stats.ierrors,
4225074e1d5SCristian Dumitrescu 		 stats.imissed,
4235074e1d5SCristian Dumitrescu 		 stats.rx_nombuf,
4245074e1d5SCristian Dumitrescu 		 stats.opackets,
4255074e1d5SCristian Dumitrescu 		 stats.obytes,
4265074e1d5SCristian Dumitrescu 		 stats.oerrors);
42778dffe31SCristian Dumitrescu 
42878dffe31SCristian Dumitrescu 	length = strlen(*out);
42978dffe31SCristian Dumitrescu 	*out_size -= length;
43078dffe31SCristian Dumitrescu 	*out += length;
4315074e1d5SCristian Dumitrescu }
4325074e1d5SCristian Dumitrescu 
43378dffe31SCristian Dumitrescu 
43478dffe31SCristian Dumitrescu static char cmd_ethdev_show_help[] =
43578dffe31SCristian Dumitrescu "ethdev show [ <ethdev_name> ]\n";
43678dffe31SCristian Dumitrescu 
4375074e1d5SCristian Dumitrescu static void
438f31c80f8SCristian Dumitrescu cmd_ethdev_show(char **tokens,
4395074e1d5SCristian Dumitrescu 	      uint32_t n_tokens,
4405074e1d5SCristian Dumitrescu 	      char *out,
4415074e1d5SCristian Dumitrescu 	      size_t out_size,
44278dffe31SCristian Dumitrescu 	      void *obj __rte_unused)
4435074e1d5SCristian Dumitrescu {
44478dffe31SCristian Dumitrescu 	uint16_t port_id;
4455074e1d5SCristian Dumitrescu 
4465074e1d5SCristian Dumitrescu 	if (n_tokens != 2 && n_tokens != 3) {
4475074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
4485074e1d5SCristian Dumitrescu 		return;
4495074e1d5SCristian Dumitrescu 	}
4505074e1d5SCristian Dumitrescu 
45178dffe31SCristian Dumitrescu 	/* Single device. */
45278dffe31SCristian Dumitrescu 	if (n_tokens == 3) {
45378dffe31SCristian Dumitrescu 		int status;
4545074e1d5SCristian Dumitrescu 
45578dffe31SCristian Dumitrescu 		status = rte_eth_dev_get_port_by_name(tokens[2], &port_id);
45678dffe31SCristian Dumitrescu 		if (status)
45778dffe31SCristian Dumitrescu 			snprintf(out, out_size, "Error: Invalid Ethernet device name.\n");
4585074e1d5SCristian Dumitrescu 
45978dffe31SCristian Dumitrescu 		ethdev_show(port_id, &out, &out_size);
4605074e1d5SCristian Dumitrescu 		return;
4615074e1d5SCristian Dumitrescu 	}
46278dffe31SCristian Dumitrescu 
46378dffe31SCristian Dumitrescu 	/*  All devices. */
46478dffe31SCristian Dumitrescu 	for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
46578dffe31SCristian Dumitrescu 		if (rte_eth_dev_is_valid_port(port_id))
46678dffe31SCristian Dumitrescu 			ethdev_show(port_id, &out, &out_size);
4675074e1d5SCristian Dumitrescu }
4685074e1d5SCristian Dumitrescu 
46977a41301SCristian Dumitrescu static const char cmd_ring_help[] =
47077a41301SCristian Dumitrescu "ring <ring_name> size <size> numa <numa_node>\n";
47177a41301SCristian Dumitrescu 
47277a41301SCristian Dumitrescu static void
47377a41301SCristian Dumitrescu cmd_ring(char **tokens,
47477a41301SCristian Dumitrescu 	uint32_t n_tokens,
47577a41301SCristian Dumitrescu 	char *out,
47677a41301SCristian Dumitrescu 	size_t out_size,
477607dd517SCristian Dumitrescu 	void *obj __rte_unused)
47877a41301SCristian Dumitrescu {
479607dd517SCristian Dumitrescu 	struct rte_ring *r;
48077a41301SCristian Dumitrescu 	char *name;
481607dd517SCristian Dumitrescu 	uint32_t size, numa_node;
48277a41301SCristian Dumitrescu 
48377a41301SCristian Dumitrescu 	if (n_tokens != 6) {
48477a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
48577a41301SCristian Dumitrescu 		return;
48677a41301SCristian Dumitrescu 	}
48777a41301SCristian Dumitrescu 
48877a41301SCristian Dumitrescu 	name = tokens[1];
48977a41301SCristian Dumitrescu 
490607dd517SCristian Dumitrescu 	if (strcmp(tokens[2], "size")) {
49177a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
49277a41301SCristian Dumitrescu 		return;
49377a41301SCristian Dumitrescu 	}
49477a41301SCristian Dumitrescu 
495607dd517SCristian Dumitrescu 	if (parser_read_uint32(&size, tokens[3])) {
49677a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "size");
49777a41301SCristian Dumitrescu 		return;
49877a41301SCristian Dumitrescu 	}
49977a41301SCristian Dumitrescu 
500607dd517SCristian Dumitrescu 	if (strcmp(tokens[4], "numa")) {
50177a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
50277a41301SCristian Dumitrescu 		return;
50377a41301SCristian Dumitrescu 	}
50477a41301SCristian Dumitrescu 
505607dd517SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[5])) {
50677a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
50777a41301SCristian Dumitrescu 		return;
50877a41301SCristian Dumitrescu 	}
50977a41301SCristian Dumitrescu 
510607dd517SCristian Dumitrescu 	r = rte_ring_create(
511607dd517SCristian Dumitrescu 		name,
512607dd517SCristian Dumitrescu 		size,
513607dd517SCristian Dumitrescu 		(int)numa_node,
514607dd517SCristian Dumitrescu 		RING_F_SP_ENQ | RING_F_SC_DEQ);
515607dd517SCristian Dumitrescu 	if (!r) {
51677a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
51777a41301SCristian Dumitrescu 		return;
51877a41301SCristian Dumitrescu 	}
51977a41301SCristian Dumitrescu }
52077a41301SCristian Dumitrescu 
5211b41a527SCristian Dumitrescu static const char cmd_cryptodev_help[] =
5221b41a527SCristian Dumitrescu "cryptodev <cryptodev_name> queues <n_queue_pairs> qsize <queue_size>\n";
5231b41a527SCristian Dumitrescu 
5241b41a527SCristian Dumitrescu static void
5251b41a527SCristian Dumitrescu cmd_cryptodev(char **tokens,
5261b41a527SCristian Dumitrescu 	      uint32_t n_tokens,
5271b41a527SCristian Dumitrescu 	      char *out,
5281b41a527SCristian Dumitrescu 	      size_t out_size,
5291b41a527SCristian Dumitrescu 	      void *obj __rte_unused)
5301b41a527SCristian Dumitrescu {
5311b41a527SCristian Dumitrescu 	struct cryptodev_params params;
5321b41a527SCristian Dumitrescu 	char *cryptodev_name;
5331b41a527SCristian Dumitrescu 	int status;
5341b41a527SCristian Dumitrescu 
5351b41a527SCristian Dumitrescu 	if (n_tokens != 6) {
5361b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
5371b41a527SCristian Dumitrescu 		return;
5381b41a527SCristian Dumitrescu 	}
5391b41a527SCristian Dumitrescu 
5401b41a527SCristian Dumitrescu 	if (strcmp(tokens[0], "cryptodev")) {
5411b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptodev");
5421b41a527SCristian Dumitrescu 		return;
5431b41a527SCristian Dumitrescu 	}
5441b41a527SCristian Dumitrescu 
5451b41a527SCristian Dumitrescu 	cryptodev_name = tokens[1];
5461b41a527SCristian Dumitrescu 
5471b41a527SCristian Dumitrescu 	if (strcmp(tokens[2], "queues")) {
5481b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "queues");
5491b41a527SCristian Dumitrescu 		return;
5501b41a527SCristian Dumitrescu 	}
5511b41a527SCristian Dumitrescu 
5521b41a527SCristian Dumitrescu 	if (parser_read_uint32(&params.n_queue_pairs, tokens[3])) {
5531b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queue_pairs");
5541b41a527SCristian Dumitrescu 		return;
5551b41a527SCristian Dumitrescu 	}
5561b41a527SCristian Dumitrescu 
5571b41a527SCristian Dumitrescu 	if (strcmp(tokens[4], "qsize")) {
5581b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "qsize");
5591b41a527SCristian Dumitrescu 		return;
5601b41a527SCristian Dumitrescu 	}
5611b41a527SCristian Dumitrescu 
5621b41a527SCristian Dumitrescu 
5631b41a527SCristian Dumitrescu 	if (parser_read_uint32(&params.queue_size, tokens[5])) {
5641b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
5651b41a527SCristian Dumitrescu 		return;
5661b41a527SCristian Dumitrescu 	}
5671b41a527SCristian Dumitrescu 
5681b41a527SCristian Dumitrescu 	status = cryptodev_config(cryptodev_name, &params);
5691b41a527SCristian Dumitrescu 	if (status)
5701b41a527SCristian Dumitrescu 		snprintf(out, out_size, "Crypto device configuration failed (%d).\n", status);
5711b41a527SCristian Dumitrescu }
5721b41a527SCristian Dumitrescu 
5739043f66aSCristian Dumitrescu static const char cmd_pipeline_codegen_help[] =
5749043f66aSCristian Dumitrescu "pipeline codegen <spec_file> <code_file>\n";
5759043f66aSCristian Dumitrescu 
5769043f66aSCristian Dumitrescu static void
5779043f66aSCristian Dumitrescu cmd_pipeline_codegen(char **tokens,
5789043f66aSCristian Dumitrescu 	uint32_t n_tokens,
5799043f66aSCristian Dumitrescu 	char *out,
5809043f66aSCristian Dumitrescu 	size_t out_size,
5819043f66aSCristian Dumitrescu 	void *obj __rte_unused)
5829043f66aSCristian Dumitrescu {
5839043f66aSCristian Dumitrescu 	FILE *spec_file = NULL;
5849043f66aSCristian Dumitrescu 	FILE *code_file = NULL;
5859043f66aSCristian Dumitrescu 	uint32_t err_line;
5869043f66aSCristian Dumitrescu 	const char *err_msg;
5879043f66aSCristian Dumitrescu 	int status;
5889043f66aSCristian Dumitrescu 
5899043f66aSCristian Dumitrescu 	if (n_tokens != 4) {
5909043f66aSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
5919043f66aSCristian Dumitrescu 		return;
5929043f66aSCristian Dumitrescu 	}
5939043f66aSCristian Dumitrescu 
5949043f66aSCristian Dumitrescu 	spec_file = fopen(tokens[2], "r");
5959043f66aSCristian Dumitrescu 	if (!spec_file) {
5969043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[2]);
5979043f66aSCristian Dumitrescu 		return;
5989043f66aSCristian Dumitrescu 	}
5999043f66aSCristian Dumitrescu 
6009043f66aSCristian Dumitrescu 	code_file = fopen(tokens[3], "w");
6019043f66aSCristian Dumitrescu 	if (!code_file) {
6029043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[3]);
603ab492f94SHarshad Narayane 		fclose(spec_file);
6049043f66aSCristian Dumitrescu 		return;
6059043f66aSCristian Dumitrescu 	}
6069043f66aSCristian Dumitrescu 
6079043f66aSCristian Dumitrescu 	status = rte_swx_pipeline_codegen(spec_file,
6089043f66aSCristian Dumitrescu 					  code_file,
6099043f66aSCristian Dumitrescu 					  &err_line,
6109043f66aSCristian Dumitrescu 					  &err_msg);
6119043f66aSCristian Dumitrescu 
6129043f66aSCristian Dumitrescu 	fclose(spec_file);
6139043f66aSCristian Dumitrescu 	fclose(code_file);
6149043f66aSCristian Dumitrescu 
6159043f66aSCristian Dumitrescu 	if (status) {
6169043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Error %d at line %u: %s\n.",
6179043f66aSCristian Dumitrescu 			status, err_line, err_msg);
6189043f66aSCristian Dumitrescu 		return;
6199043f66aSCristian Dumitrescu 	}
6209043f66aSCristian Dumitrescu }
6216bc14d9fSCristian Dumitrescu 
6226bc14d9fSCristian Dumitrescu static const char cmd_pipeline_libbuild_help[] =
6236bc14d9fSCristian Dumitrescu "pipeline libbuild <code_file> <lib_file>\n";
6246bc14d9fSCristian Dumitrescu 
6256bc14d9fSCristian Dumitrescu static void
6266bc14d9fSCristian Dumitrescu cmd_pipeline_libbuild(char **tokens,
6276bc14d9fSCristian Dumitrescu 	uint32_t n_tokens,
6286bc14d9fSCristian Dumitrescu 	char *out,
6296bc14d9fSCristian Dumitrescu 	size_t out_size,
6306bc14d9fSCristian Dumitrescu 	void *obj __rte_unused)
6316bc14d9fSCristian Dumitrescu {
6326bc14d9fSCristian Dumitrescu 	char *code_file, *lib_file, *obj_file = NULL, *log_file = NULL;
6336bc14d9fSCristian Dumitrescu 	char *install_dir, *cwd = NULL, *buffer = NULL;
6346bc14d9fSCristian Dumitrescu 	size_t length;
6356bc14d9fSCristian Dumitrescu 	int status = 0;
6366bc14d9fSCristian Dumitrescu 
6376bc14d9fSCristian Dumitrescu 	if (n_tokens != 4) {
6386bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
6396bc14d9fSCristian Dumitrescu 		goto free;
6406bc14d9fSCristian Dumitrescu 	}
6416bc14d9fSCristian Dumitrescu 
6426bc14d9fSCristian Dumitrescu 	install_dir = getenv("RTE_INSTALL_DIR");
6436bc14d9fSCristian Dumitrescu 	if (!install_dir) {
6446bc14d9fSCristian Dumitrescu 		cwd = malloc(MAX_LINE_SIZE);
6456bc14d9fSCristian Dumitrescu 		if (!cwd) {
6466bc14d9fSCristian Dumitrescu 			snprintf(out, out_size, MSG_OUT_OF_MEMORY);
6476bc14d9fSCristian Dumitrescu 			goto free;
6486bc14d9fSCristian Dumitrescu 		}
6496bc14d9fSCristian Dumitrescu 
6506bc14d9fSCristian Dumitrescu 		install_dir = getcwd(cwd, MAX_LINE_SIZE);
6516bc14d9fSCristian Dumitrescu 		if (!install_dir) {
6526bc14d9fSCristian Dumitrescu 			snprintf(out, out_size, "Error: Path too long.\n");
6536bc14d9fSCristian Dumitrescu 			goto free;
6546bc14d9fSCristian Dumitrescu 		}
6556bc14d9fSCristian Dumitrescu 	}
6566bc14d9fSCristian Dumitrescu 
6576bc14d9fSCristian Dumitrescu 	snprintf(out, out_size, "Using DPDK source code from \"%s\".\n", install_dir);
6586bc14d9fSCristian Dumitrescu 	out_size -= strlen(out);
6596bc14d9fSCristian Dumitrescu 	out += strlen(out);
6606bc14d9fSCristian Dumitrescu 
6616bc14d9fSCristian Dumitrescu 	code_file = tokens[2];
6626bc14d9fSCristian Dumitrescu 	length = strnlen(code_file, MAX_LINE_SIZE);
6636bc14d9fSCristian Dumitrescu 	if ((length < 3) ||
6646bc14d9fSCristian Dumitrescu 	    (code_file[length - 2] != '.') ||
6656bc14d9fSCristian Dumitrescu 	    (code_file[length - 1] != 'c')) {
6666bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "code_file");
6676bc14d9fSCristian Dumitrescu 		goto free;
6686bc14d9fSCristian Dumitrescu 	}
6696bc14d9fSCristian Dumitrescu 
6706bc14d9fSCristian Dumitrescu 	lib_file = tokens[3];
6716bc14d9fSCristian Dumitrescu 	length = strnlen(lib_file, MAX_LINE_SIZE);
6726bc14d9fSCristian Dumitrescu 	if ((length < 4) ||
6736bc14d9fSCristian Dumitrescu 	    (lib_file[length - 3] != '.') ||
6746bc14d9fSCristian Dumitrescu 	    (lib_file[length - 2] != 's') ||
6756bc14d9fSCristian Dumitrescu 	    (lib_file[length - 1] != 'o')) {
6766bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "lib_file");
6776bc14d9fSCristian Dumitrescu 		goto free;
6786bc14d9fSCristian Dumitrescu 	}
6796bc14d9fSCristian Dumitrescu 
6806bc14d9fSCristian Dumitrescu 	obj_file = malloc(length);
6816bc14d9fSCristian Dumitrescu 	log_file = malloc(length + 2);
6826bc14d9fSCristian Dumitrescu 	if (!obj_file || !log_file) {
6836bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
6846bc14d9fSCristian Dumitrescu 		goto free;
6856bc14d9fSCristian Dumitrescu 	}
6866bc14d9fSCristian Dumitrescu 
6876bc14d9fSCristian Dumitrescu 	memcpy(obj_file, lib_file, length - 2);
6886bc14d9fSCristian Dumitrescu 	obj_file[length - 2] = 'o';
6896bc14d9fSCristian Dumitrescu 	obj_file[length - 1] = 0;
6906bc14d9fSCristian Dumitrescu 
6916bc14d9fSCristian Dumitrescu 	memcpy(log_file, lib_file, length - 2);
6926bc14d9fSCristian Dumitrescu 	log_file[length - 2] = 'l';
6936bc14d9fSCristian Dumitrescu 	log_file[length - 1] = 'o';
6946bc14d9fSCristian Dumitrescu 	log_file[length] = 'g';
6956bc14d9fSCristian Dumitrescu 	log_file[length + 1] = 0;
6966bc14d9fSCristian Dumitrescu 
6976bc14d9fSCristian Dumitrescu 	buffer = malloc(MAX_LINE_SIZE);
6986bc14d9fSCristian Dumitrescu 	if (!buffer) {
6996bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
700b42f3e2fSHarshad Narayane 		goto free;
7016bc14d9fSCristian Dumitrescu 	}
7026bc14d9fSCristian Dumitrescu 
7036bc14d9fSCristian Dumitrescu 	snprintf(buffer,
7046bc14d9fSCristian Dumitrescu 		 MAX_LINE_SIZE,
7056bc14d9fSCristian Dumitrescu 		 "gcc -c -O3 -fpic -Wno-deprecated-declarations -o %s %s "
7066bc14d9fSCristian Dumitrescu 		 "-I %s/lib/pipeline "
7076bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/include "
7086bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/x86/include "
7096bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/include/generic "
710*96893df7SCristian Dumitrescu 		 "-I %s/lib/log "
7116bc14d9fSCristian Dumitrescu 		 "-I %s/lib/meter "
7126bc14d9fSCristian Dumitrescu 		 "-I %s/lib/port "
7136bc14d9fSCristian Dumitrescu 		 "-I %s/lib/table "
7146bc14d9fSCristian Dumitrescu 		 "-I %s/lib/pipeline "
7156bc14d9fSCristian Dumitrescu 		 "-I %s/config "
7166bc14d9fSCristian Dumitrescu 		 "-I %s/build "
7176bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/linux/include "
7186bc14d9fSCristian Dumitrescu 		 ">%s 2>&1 "
7196bc14d9fSCristian Dumitrescu 		 "&& "
7206bc14d9fSCristian Dumitrescu 		 "gcc -shared %s -o %s "
7216bc14d9fSCristian Dumitrescu 		 ">>%s 2>&1",
7226bc14d9fSCristian Dumitrescu 		 obj_file,
7236bc14d9fSCristian Dumitrescu 		 code_file,
7246bc14d9fSCristian Dumitrescu 		 install_dir,
7256bc14d9fSCristian Dumitrescu 		 install_dir,
7266bc14d9fSCristian Dumitrescu 		 install_dir,
7276bc14d9fSCristian Dumitrescu 		 install_dir,
7286bc14d9fSCristian Dumitrescu 		 install_dir,
7296bc14d9fSCristian Dumitrescu 		 install_dir,
7306bc14d9fSCristian Dumitrescu 		 install_dir,
7316bc14d9fSCristian Dumitrescu 		 install_dir,
7326bc14d9fSCristian Dumitrescu 		 install_dir,
7336bc14d9fSCristian Dumitrescu 		 install_dir,
7346bc14d9fSCristian Dumitrescu 		 install_dir,
735*96893df7SCristian Dumitrescu 		 install_dir,
7366bc14d9fSCristian Dumitrescu 		 log_file,
7376bc14d9fSCristian Dumitrescu 		 obj_file,
7386bc14d9fSCristian Dumitrescu 		 lib_file,
7396bc14d9fSCristian Dumitrescu 		 log_file);
7406bc14d9fSCristian Dumitrescu 
7416bc14d9fSCristian Dumitrescu 	status = system(buffer);
7426bc14d9fSCristian Dumitrescu 	if (status) {
7436bc14d9fSCristian Dumitrescu 		snprintf(out,
7446bc14d9fSCristian Dumitrescu 			 out_size,
7456bc14d9fSCristian Dumitrescu 			 "Library build failed, see file \"%s\" for details.\n",
7466bc14d9fSCristian Dumitrescu 			 log_file);
7476bc14d9fSCristian Dumitrescu 		goto free;
7486bc14d9fSCristian Dumitrescu 	}
7496bc14d9fSCristian Dumitrescu 
7506bc14d9fSCristian Dumitrescu free:
7516bc14d9fSCristian Dumitrescu 	free(cwd);
7526bc14d9fSCristian Dumitrescu 	free(obj_file);
7536bc14d9fSCristian Dumitrescu 	free(log_file);
7546bc14d9fSCristian Dumitrescu 	free(buffer);
7556bc14d9fSCristian Dumitrescu }
7566bc14d9fSCristian Dumitrescu 
7575074e1d5SCristian Dumitrescu static const char cmd_pipeline_build_help[] =
75868b95704SCristian Dumitrescu "pipeline <pipeline_name> build lib <lib_file> io <iospec_file> numa <numa_node>\n";
7595074e1d5SCristian Dumitrescu 
7605074e1d5SCristian Dumitrescu static void
7615074e1d5SCristian Dumitrescu cmd_pipeline_build(char **tokens,
7625074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
7635074e1d5SCristian Dumitrescu 	char *out,
7645074e1d5SCristian Dumitrescu 	size_t out_size,
76568b95704SCristian Dumitrescu 	void *obj __rte_unused)
7665074e1d5SCristian Dumitrescu {
76768b95704SCristian Dumitrescu 	struct rte_swx_pipeline *p = NULL;
76868b95704SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl = NULL;
76968b95704SCristian Dumitrescu 	char *pipeline_name, *lib_file_name, *iospec_file_name;
77068b95704SCristian Dumitrescu 	FILE *iospec_file = NULL;
77168b95704SCristian Dumitrescu 	uint32_t numa_node = 0;
77268b95704SCristian Dumitrescu 	int status = 0;
7735074e1d5SCristian Dumitrescu 
77468b95704SCristian Dumitrescu 	/* Parsing. */
77568b95704SCristian Dumitrescu 	if (n_tokens != 9) {
7765074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
7775074e1d5SCristian Dumitrescu 		return;
7785074e1d5SCristian Dumitrescu 	}
7795074e1d5SCristian Dumitrescu 
78068b95704SCristian Dumitrescu 	pipeline_name = tokens[1];
78168b95704SCristian Dumitrescu 
78268b95704SCristian Dumitrescu 	if (strcmp(tokens[2], "build")) {
78368b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "build");
7845074e1d5SCristian Dumitrescu 		return;
7855074e1d5SCristian Dumitrescu 	}
7865074e1d5SCristian Dumitrescu 
78768b95704SCristian Dumitrescu 	if (strcmp(tokens[3], "lib")) {
78868b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "lib");
7895074e1d5SCristian Dumitrescu 		return;
7905074e1d5SCristian Dumitrescu 	}
7915074e1d5SCristian Dumitrescu 
79268b95704SCristian Dumitrescu 	lib_file_name = tokens[4];
79368b95704SCristian Dumitrescu 
79468b95704SCristian Dumitrescu 	if (strcmp(tokens[5], "io")) {
79568b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "io");
79668b95704SCristian Dumitrescu 		return;
79768b95704SCristian Dumitrescu 	}
79868b95704SCristian Dumitrescu 
79968b95704SCristian Dumitrescu 	iospec_file_name = tokens[6];
80068b95704SCristian Dumitrescu 
80168b95704SCristian Dumitrescu 	if (strcmp(tokens[7], "numa")) {
80268b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
80368b95704SCristian Dumitrescu 		return;
80468b95704SCristian Dumitrescu 	}
80568b95704SCristian Dumitrescu 
80668b95704SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[8])) {
80768b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
80868b95704SCristian Dumitrescu 		return;
80968b95704SCristian Dumitrescu 	}
81068b95704SCristian Dumitrescu 
81168b95704SCristian Dumitrescu 	/* I/O spec file open. */
81268b95704SCristian Dumitrescu 	iospec_file = fopen(iospec_file_name, "r");
81368b95704SCristian Dumitrescu 	if (!iospec_file) {
81468b95704SCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file \"%s\".\n", iospec_file_name);
81568b95704SCristian Dumitrescu 		return;
81668b95704SCristian Dumitrescu 	}
81768b95704SCristian Dumitrescu 
81868b95704SCristian Dumitrescu 	status = rte_swx_pipeline_build_from_lib(&p,
81968b95704SCristian Dumitrescu 						 pipeline_name,
82068b95704SCristian Dumitrescu 						 lib_file_name,
82168b95704SCristian Dumitrescu 						 iospec_file,
82268b95704SCristian Dumitrescu 						 (int)numa_node);
8235074e1d5SCristian Dumitrescu 	if (status) {
82468b95704SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline build failed (%d).", status);
82568b95704SCristian Dumitrescu 		goto free;
8265074e1d5SCristian Dumitrescu 	}
8275074e1d5SCristian Dumitrescu 
82868b95704SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_create(p);
82968b95704SCristian Dumitrescu 	if (!ctl) {
8305074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline control create failed.");
83168b95704SCristian Dumitrescu 		goto free;
8325074e1d5SCristian Dumitrescu 	}
83368b95704SCristian Dumitrescu 
83468b95704SCristian Dumitrescu free:
83568b95704SCristian Dumitrescu 	if (status)
83668b95704SCristian Dumitrescu 		rte_swx_pipeline_free(p);
83768b95704SCristian Dumitrescu 
83868b95704SCristian Dumitrescu 	if (iospec_file)
83968b95704SCristian Dumitrescu 		fclose(iospec_file);
8405074e1d5SCristian Dumitrescu }
8415074e1d5SCristian Dumitrescu 
84275129cebSChurchill Khangar static int
84375129cebSChurchill Khangar pipeline_table_entries_add(struct rte_swx_ctl_pipeline *p,
84475129cebSChurchill Khangar 			   const char *table_name,
84575129cebSChurchill Khangar 			   FILE *file,
84675129cebSChurchill Khangar 			   uint32_t *file_line_number)
84775129cebSChurchill Khangar {
84875129cebSChurchill Khangar 	char *line = NULL;
84975129cebSChurchill Khangar 	uint32_t line_id = 0;
85075129cebSChurchill Khangar 	int status = 0;
85175129cebSChurchill Khangar 
85275129cebSChurchill Khangar 	/* Buffer allocation. */
85375129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
85475129cebSChurchill Khangar 	if (!line)
85575129cebSChurchill Khangar 		return -ENOMEM;
85675129cebSChurchill Khangar 
85775129cebSChurchill Khangar 	/* File read. */
85875129cebSChurchill Khangar 	for (line_id = 1; ; line_id++) {
85975129cebSChurchill Khangar 		struct rte_swx_table_entry *entry;
86075129cebSChurchill Khangar 		int is_blank_or_comment;
86175129cebSChurchill Khangar 
86275129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
86375129cebSChurchill Khangar 			break;
86475129cebSChurchill Khangar 
86575129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
86675129cebSChurchill Khangar 							      table_name,
86775129cebSChurchill Khangar 							      line,
86875129cebSChurchill Khangar 							      &is_blank_or_comment);
86975129cebSChurchill Khangar 		if (!entry) {
87075129cebSChurchill Khangar 			if (is_blank_or_comment)
87175129cebSChurchill Khangar 				continue;
87275129cebSChurchill Khangar 
87375129cebSChurchill Khangar 			status = -EINVAL;
87475129cebSChurchill Khangar 			goto error;
87575129cebSChurchill Khangar 		}
87675129cebSChurchill Khangar 
87775129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_entry_add(p,
87875129cebSChurchill Khangar 							      table_name,
87975129cebSChurchill Khangar 							      entry);
88075129cebSChurchill Khangar 		table_entry_free(entry);
88175129cebSChurchill Khangar 		if (status)
88275129cebSChurchill Khangar 			goto error;
88375129cebSChurchill Khangar 	}
88475129cebSChurchill Khangar 
88575129cebSChurchill Khangar error:
88675129cebSChurchill Khangar 	free(line);
88775129cebSChurchill Khangar 	*file_line_number = line_id;
88875129cebSChurchill Khangar 	return status;
88975129cebSChurchill Khangar }
89075129cebSChurchill Khangar 
89175129cebSChurchill Khangar static const char cmd_pipeline_table_add_help[] =
89275129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> add <file_name>\n";
8935074e1d5SCristian Dumitrescu 
8945074e1d5SCristian Dumitrescu static void
89575129cebSChurchill Khangar cmd_pipeline_table_add(char **tokens,
8965074e1d5SCristian Dumitrescu 		       uint32_t n_tokens,
8975074e1d5SCristian Dumitrescu 		       char *out,
8985074e1d5SCristian Dumitrescu 		       size_t out_size,
899b9559f94SCristian Dumitrescu 		       void *obj __rte_unused)
9005074e1d5SCristian Dumitrescu {
901b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
90275129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
90375129cebSChurchill Khangar 	FILE *file = NULL;
90475129cebSChurchill Khangar 	uint32_t file_line_number = 0;
9055074e1d5SCristian Dumitrescu 	int status;
9065074e1d5SCristian Dumitrescu 
90775129cebSChurchill Khangar 	if (n_tokens != 6) {
9085074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
9095074e1d5SCristian Dumitrescu 		return;
9105074e1d5SCristian Dumitrescu 	}
9115074e1d5SCristian Dumitrescu 
9125074e1d5SCristian Dumitrescu 	pipeline_name = tokens[1];
913b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
914b9559f94SCristian Dumitrescu 	if (!ctl) {
9155074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
9165074e1d5SCristian Dumitrescu 		return;
9175074e1d5SCristian Dumitrescu 	}
9185074e1d5SCristian Dumitrescu 
91975129cebSChurchill Khangar 	table_name = tokens[3];
92075129cebSChurchill Khangar 
92175129cebSChurchill Khangar 	file_name = tokens[5];
92275129cebSChurchill Khangar 	file = fopen(file_name, "r");
92375129cebSChurchill Khangar 	if (!file) {
92475129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
92575129cebSChurchill Khangar 		return;
92675129cebSChurchill Khangar 	}
92775129cebSChurchill Khangar 
928b9559f94SCristian Dumitrescu 	status = pipeline_table_entries_add(ctl,
92975129cebSChurchill Khangar 					    table_name,
93075129cebSChurchill Khangar 					    file,
93175129cebSChurchill Khangar 					    &file_line_number);
93275129cebSChurchill Khangar 	if (status)
93375129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
93475129cebSChurchill Khangar 			 file_name,
93575129cebSChurchill Khangar 			 file_line_number);
93675129cebSChurchill Khangar 
93775129cebSChurchill Khangar 	fclose(file);
93875129cebSChurchill Khangar }
93975129cebSChurchill Khangar 
94075129cebSChurchill Khangar static int
94175129cebSChurchill Khangar pipeline_table_entries_delete(struct rte_swx_ctl_pipeline *p,
94275129cebSChurchill Khangar 			      const char *table_name,
94375129cebSChurchill Khangar 			      FILE *file,
94475129cebSChurchill Khangar 			      uint32_t *file_line_number)
94575129cebSChurchill Khangar {
94675129cebSChurchill Khangar 	char *line = NULL;
94775129cebSChurchill Khangar 	uint32_t line_id = 0;
94875129cebSChurchill Khangar 	int status = 0;
94975129cebSChurchill Khangar 
95075129cebSChurchill Khangar 	/* Buffer allocation. */
95175129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
95275129cebSChurchill Khangar 	if (!line)
95375129cebSChurchill Khangar 		return -ENOMEM;
95475129cebSChurchill Khangar 
95575129cebSChurchill Khangar 	/* File read. */
95675129cebSChurchill Khangar 	for (line_id = 1; ; line_id++) {
95775129cebSChurchill Khangar 		struct rte_swx_table_entry *entry;
95875129cebSChurchill Khangar 		int is_blank_or_comment;
95975129cebSChurchill Khangar 
96075129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
96175129cebSChurchill Khangar 			break;
96275129cebSChurchill Khangar 
96375129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
96475129cebSChurchill Khangar 							      table_name,
96575129cebSChurchill Khangar 							      line,
96675129cebSChurchill Khangar 							      &is_blank_or_comment);
96775129cebSChurchill Khangar 		if (!entry) {
96875129cebSChurchill Khangar 			if (is_blank_or_comment)
96975129cebSChurchill Khangar 				continue;
97075129cebSChurchill Khangar 
97175129cebSChurchill Khangar 			status = -EINVAL;
97275129cebSChurchill Khangar 			goto error;
97375129cebSChurchill Khangar 		}
97475129cebSChurchill Khangar 
97575129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_entry_delete(p,
97675129cebSChurchill Khangar 								 table_name,
97775129cebSChurchill Khangar 								 entry);
97875129cebSChurchill Khangar 		table_entry_free(entry);
97975129cebSChurchill Khangar 		if (status)
98075129cebSChurchill Khangar 			goto error;
98175129cebSChurchill Khangar 	}
98275129cebSChurchill Khangar 
98375129cebSChurchill Khangar error:
98475129cebSChurchill Khangar 	*file_line_number = line_id;
98575129cebSChurchill Khangar 	free(line);
98675129cebSChurchill Khangar 	return status;
98775129cebSChurchill Khangar }
98875129cebSChurchill Khangar 
98975129cebSChurchill Khangar static const char cmd_pipeline_table_delete_help[] =
99075129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> delete <file_name>\n";
99175129cebSChurchill Khangar 
99275129cebSChurchill Khangar static void
99375129cebSChurchill Khangar cmd_pipeline_table_delete(char **tokens,
99475129cebSChurchill Khangar 			  uint32_t n_tokens,
99575129cebSChurchill Khangar 			  char *out,
99675129cebSChurchill Khangar 			  size_t out_size,
997b9559f94SCristian Dumitrescu 			  void *obj __rte_unused)
99875129cebSChurchill Khangar {
999b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
100075129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
100175129cebSChurchill Khangar 	FILE *file = NULL;
100275129cebSChurchill Khangar 	uint32_t file_line_number = 0;
100375129cebSChurchill Khangar 	int status;
100475129cebSChurchill Khangar 
100575129cebSChurchill Khangar 	if (n_tokens != 6) {
100675129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
100775129cebSChurchill Khangar 		return;
100875129cebSChurchill Khangar 	}
100975129cebSChurchill Khangar 
101075129cebSChurchill Khangar 	pipeline_name = tokens[1];
1011b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1012b9559f94SCristian Dumitrescu 	if (!ctl) {
101375129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
10145074e1d5SCristian Dumitrescu 		return;
10155074e1d5SCristian Dumitrescu 	}
10165074e1d5SCristian Dumitrescu 
10175074e1d5SCristian Dumitrescu 	table_name = tokens[3];
10185074e1d5SCristian Dumitrescu 
101975129cebSChurchill Khangar 	file_name = tokens[5];
102075129cebSChurchill Khangar 	file = fopen(file_name, "r");
102175129cebSChurchill Khangar 	if (!file) {
102275129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
10235074e1d5SCristian Dumitrescu 		return;
10245074e1d5SCristian Dumitrescu 	}
10255074e1d5SCristian Dumitrescu 
1026b9559f94SCristian Dumitrescu 	status = pipeline_table_entries_delete(ctl,
102775129cebSChurchill Khangar 					       table_name,
102875129cebSChurchill Khangar 					       file,
102975129cebSChurchill Khangar 					       &file_line_number);
103075129cebSChurchill Khangar 	if (status)
103175129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
103275129cebSChurchill Khangar 			 file_name,
103375129cebSChurchill Khangar 			 file_line_number);
10345074e1d5SCristian Dumitrescu 
103575129cebSChurchill Khangar 	fclose(file);
10365074e1d5SCristian Dumitrescu }
10375074e1d5SCristian Dumitrescu 
103875129cebSChurchill Khangar static int
103975129cebSChurchill Khangar pipeline_table_default_entry_add(struct rte_swx_ctl_pipeline *p,
104075129cebSChurchill Khangar 				 const char *table_name,
104175129cebSChurchill Khangar 				 FILE *file,
104275129cebSChurchill Khangar 				 uint32_t *file_line_number)
104375129cebSChurchill Khangar {
104475129cebSChurchill Khangar 	char *line = NULL;
104575129cebSChurchill Khangar 	uint32_t line_id = 0;
104675129cebSChurchill Khangar 	int status = 0;
10475074e1d5SCristian Dumitrescu 
10485074e1d5SCristian Dumitrescu 	/* Buffer allocation. */
104975129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
105075129cebSChurchill Khangar 	if (!line)
105175129cebSChurchill Khangar 		return -ENOMEM;
10525074e1d5SCristian Dumitrescu 
105375129cebSChurchill Khangar 	/* File read. */
10545074e1d5SCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
10555074e1d5SCristian Dumitrescu 		struct rte_swx_table_entry *entry;
1056cff9a717SCristian Dumitrescu 		int is_blank_or_comment;
10575074e1d5SCristian Dumitrescu 
105875129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
10595074e1d5SCristian Dumitrescu 			break;
10605074e1d5SCristian Dumitrescu 
106175129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
10625074e1d5SCristian Dumitrescu 							      table_name,
1063cff9a717SCristian Dumitrescu 							      line,
1064cff9a717SCristian Dumitrescu 							      &is_blank_or_comment);
10655074e1d5SCristian Dumitrescu 		if (!entry) {
1066cff9a717SCristian Dumitrescu 			if (is_blank_or_comment)
1067cff9a717SCristian Dumitrescu 				continue;
1068cff9a717SCristian Dumitrescu 
106975129cebSChurchill Khangar 			status = -EINVAL;
10705074e1d5SCristian Dumitrescu 			goto error;
10715074e1d5SCristian Dumitrescu 		}
10725074e1d5SCristian Dumitrescu 
107375129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_default_entry_add(p,
10745074e1d5SCristian Dumitrescu 								      table_name,
10755074e1d5SCristian Dumitrescu 								      entry);
1076275ebefeSCristian Dumitrescu 		table_entry_free(entry);
107775129cebSChurchill Khangar 		if (status)
10785074e1d5SCristian Dumitrescu 			goto error;
10795074e1d5SCristian Dumitrescu 	}
108075129cebSChurchill Khangar 
108175129cebSChurchill Khangar error:
108275129cebSChurchill Khangar 	*file_line_number = line_id;
108375129cebSChurchill Khangar 	free(line);
108475129cebSChurchill Khangar 	return status;
10855074e1d5SCristian Dumitrescu }
10865074e1d5SCristian Dumitrescu 
108775129cebSChurchill Khangar static const char cmd_pipeline_table_default_help[] =
108875129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> default <file_name>\n";
10895074e1d5SCristian Dumitrescu 
109075129cebSChurchill Khangar static void
109175129cebSChurchill Khangar cmd_pipeline_table_default(char **tokens,
109275129cebSChurchill Khangar 			   uint32_t n_tokens,
109375129cebSChurchill Khangar 			   char *out,
109475129cebSChurchill Khangar 			   size_t out_size,
1095b9559f94SCristian Dumitrescu 			   void *obj __rte_unused)
109675129cebSChurchill Khangar {
1097b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
109875129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
109975129cebSChurchill Khangar 	FILE *file = NULL;
110075129cebSChurchill Khangar 	uint32_t file_line_number = 0;
110175129cebSChurchill Khangar 	int status;
11025074e1d5SCristian Dumitrescu 
110375129cebSChurchill Khangar 	if (n_tokens != 6) {
110475129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
110575129cebSChurchill Khangar 		return;
110675129cebSChurchill Khangar 	}
11075074e1d5SCristian Dumitrescu 
110875129cebSChurchill Khangar 	pipeline_name = tokens[1];
1109b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1110b9559f94SCristian Dumitrescu 	if (!ctl) {
111175129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
111275129cebSChurchill Khangar 		return;
111375129cebSChurchill Khangar 	}
111475129cebSChurchill Khangar 
111575129cebSChurchill Khangar 	table_name = tokens[3];
111675129cebSChurchill Khangar 
111775129cebSChurchill Khangar 	file_name = tokens[5];
111875129cebSChurchill Khangar 	file = fopen(file_name, "r");
111975129cebSChurchill Khangar 	if (!file) {
112075129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
112175129cebSChurchill Khangar 		return;
112275129cebSChurchill Khangar 	}
112375129cebSChurchill Khangar 
1124b9559f94SCristian Dumitrescu 	status = pipeline_table_default_entry_add(ctl,
11255074e1d5SCristian Dumitrescu 						  table_name,
112675129cebSChurchill Khangar 						  file,
112775129cebSChurchill Khangar 						  &file_line_number);
112875129cebSChurchill Khangar 	if (status)
112975129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
113075129cebSChurchill Khangar 			 file_name,
113175129cebSChurchill Khangar 			 file_line_number);
1132cff9a717SCristian Dumitrescu 
113375129cebSChurchill Khangar 	fclose(file);
11345074e1d5SCristian Dumitrescu }
11355074e1d5SCristian Dumitrescu 
113675129cebSChurchill Khangar static const char cmd_pipeline_table_show_help[] =
1137a4c1146cSCristian Dumitrescu "pipeline <pipeline_name> table <table_name> show [filename]\n";
113875129cebSChurchill Khangar 
113975129cebSChurchill Khangar static void
114075129cebSChurchill Khangar cmd_pipeline_table_show(char **tokens,
114175129cebSChurchill Khangar 	uint32_t n_tokens,
114275129cebSChurchill Khangar 	char *out,
114375129cebSChurchill Khangar 	size_t out_size,
1144b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
114575129cebSChurchill Khangar {
1146b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
114775129cebSChurchill Khangar 	char *pipeline_name, *table_name;
1148a4c1146cSCristian Dumitrescu 	FILE *file = NULL;
114975129cebSChurchill Khangar 	int status;
115075129cebSChurchill Khangar 
1151a4c1146cSCristian Dumitrescu 	if (n_tokens != 5 && n_tokens != 6) {
115275129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
115375129cebSChurchill Khangar 		return;
11545074e1d5SCristian Dumitrescu 	}
11555074e1d5SCristian Dumitrescu 
115675129cebSChurchill Khangar 	pipeline_name = tokens[1];
1157b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1158b9559f94SCristian Dumitrescu 	if (!ctl) {
115975129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
116075129cebSChurchill Khangar 		return;
11615074e1d5SCristian Dumitrescu 	}
11625074e1d5SCristian Dumitrescu 
116375129cebSChurchill Khangar 	table_name = tokens[3];
1164a4c1146cSCristian Dumitrescu 	file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
1165a4c1146cSCristian Dumitrescu 	if (!file) {
1166a4c1146cSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
1167a4c1146cSCristian Dumitrescu 		return;
1168a4c1146cSCristian Dumitrescu 	}
1169a4c1146cSCristian Dumitrescu 
1170b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_table_fprintf(file, ctl, table_name);
117175129cebSChurchill Khangar 	if (status)
117275129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "table_name");
1173a4c1146cSCristian Dumitrescu 
1174a4c1146cSCristian Dumitrescu 	if (file)
1175a4c1146cSCristian Dumitrescu 		fclose(file);
11765074e1d5SCristian Dumitrescu }
117775129cebSChurchill Khangar 
1178598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_add_help[] =
1179598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group add\n";
1180598fe0ddSCristian Dumitrescu 
1181598fe0ddSCristian Dumitrescu static void
1182598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_add(char **tokens,
1183598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1184598fe0ddSCristian Dumitrescu 	char *out,
1185598fe0ddSCristian Dumitrescu 	size_t out_size,
1186b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1187598fe0ddSCristian Dumitrescu {
1188b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1189598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1190598fe0ddSCristian Dumitrescu 	uint32_t group_id;
1191598fe0ddSCristian Dumitrescu 	int status;
1192598fe0ddSCristian Dumitrescu 
1193598fe0ddSCristian Dumitrescu 	if (n_tokens != 6) {
1194598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1195598fe0ddSCristian Dumitrescu 		return;
1196598fe0ddSCristian Dumitrescu 	}
1197598fe0ddSCristian Dumitrescu 
1198598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1199b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1200b9559f94SCristian Dumitrescu 	if (!ctl) {
1201598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1202598fe0ddSCristian Dumitrescu 		return;
1203598fe0ddSCristian Dumitrescu 	}
1204598fe0ddSCristian Dumitrescu 
1205598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1206598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1207598fe0ddSCristian Dumitrescu 		return;
1208598fe0ddSCristian Dumitrescu 	}
1209598fe0ddSCristian Dumitrescu 
1210598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1211598fe0ddSCristian Dumitrescu 
1212598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1213598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "add")) {
1214598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group add");
1215598fe0ddSCristian Dumitrescu 		return;
1216598fe0ddSCristian Dumitrescu 	}
1217598fe0ddSCristian Dumitrescu 
1218b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_group_add(ctl,
1219598fe0ddSCristian Dumitrescu 		selector_name,
1220598fe0ddSCristian Dumitrescu 		&group_id);
1221598fe0ddSCristian Dumitrescu 	if (status)
1222598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
1223598fe0ddSCristian Dumitrescu 	else
1224598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Group ID: %u\n", group_id);
1225598fe0ddSCristian Dumitrescu }
1226598fe0ddSCristian Dumitrescu 
1227598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_delete_help[] =
1228598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group delete <group_id>\n";
1229598fe0ddSCristian Dumitrescu 
1230598fe0ddSCristian Dumitrescu static void
1231598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_delete(char **tokens,
1232598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1233598fe0ddSCristian Dumitrescu 	char *out,
1234598fe0ddSCristian Dumitrescu 	size_t out_size,
1235b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1236598fe0ddSCristian Dumitrescu {
1237b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1238598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1239598fe0ddSCristian Dumitrescu 	uint32_t group_id;
1240598fe0ddSCristian Dumitrescu 	int status;
1241598fe0ddSCristian Dumitrescu 
1242598fe0ddSCristian Dumitrescu 	if (n_tokens != 7) {
1243598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1244598fe0ddSCristian Dumitrescu 		return;
1245598fe0ddSCristian Dumitrescu 	}
1246598fe0ddSCristian Dumitrescu 
1247598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1248b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1249b9559f94SCristian Dumitrescu 	if (!ctl) {
1250598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1251598fe0ddSCristian Dumitrescu 		return;
1252598fe0ddSCristian Dumitrescu 	}
1253598fe0ddSCristian Dumitrescu 
1254598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1255598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1256598fe0ddSCristian Dumitrescu 		return;
1257598fe0ddSCristian Dumitrescu 	}
1258598fe0ddSCristian Dumitrescu 
1259598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1260598fe0ddSCristian Dumitrescu 
1261598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1262598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "delete")) {
1263598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group delete");
1264598fe0ddSCristian Dumitrescu 		return;
1265598fe0ddSCristian Dumitrescu 	}
1266598fe0ddSCristian Dumitrescu 
1267598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&group_id, tokens[6]) != 0) {
1268598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "group_id");
1269598fe0ddSCristian Dumitrescu 		return;
1270598fe0ddSCristian Dumitrescu 	}
1271598fe0ddSCristian Dumitrescu 
1272b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_group_delete(ctl,
1273598fe0ddSCristian Dumitrescu 		selector_name,
1274598fe0ddSCristian Dumitrescu 		group_id);
1275598fe0ddSCristian Dumitrescu 	if (status)
1276598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
1277598fe0ddSCristian Dumitrescu }
1278598fe0ddSCristian Dumitrescu 
1279598fe0ddSCristian Dumitrescu #define GROUP_MEMBER_INFO_TOKENS_MAX 6
1280598fe0ddSCristian Dumitrescu 
1281598fe0ddSCristian Dumitrescu static int
1282598fe0ddSCristian Dumitrescu token_is_comment(const char *token)
1283598fe0ddSCristian Dumitrescu {
1284598fe0ddSCristian Dumitrescu 	if ((token[0] == '#') ||
1285598fe0ddSCristian Dumitrescu 	    (token[0] == ';') ||
1286598fe0ddSCristian Dumitrescu 	    ((token[0] == '/') && (token[1] == '/')))
1287598fe0ddSCristian Dumitrescu 		return 1; /* TRUE. */
1288598fe0ddSCristian Dumitrescu 
1289598fe0ddSCristian Dumitrescu 	return 0; /* FALSE. */
1290598fe0ddSCristian Dumitrescu }
1291598fe0ddSCristian Dumitrescu 
1292598fe0ddSCristian Dumitrescu static int
1293598fe0ddSCristian Dumitrescu pipeline_selector_group_member_read(const char *string,
1294598fe0ddSCristian Dumitrescu 				      uint32_t *group_id,
1295598fe0ddSCristian Dumitrescu 				      uint32_t *member_id,
1296598fe0ddSCristian Dumitrescu 				      uint32_t *weight,
1297598fe0ddSCristian Dumitrescu 				      int *is_blank_or_comment)
1298598fe0ddSCristian Dumitrescu {
1299598fe0ddSCristian Dumitrescu 	char *token_array[GROUP_MEMBER_INFO_TOKENS_MAX], **tokens;
1300598fe0ddSCristian Dumitrescu 	char *s0 = NULL, *s;
130100b67591SAli Alnubani 	uint32_t n_tokens = 0, group_id_val = 0, member_id_val = 0, weight_val = 0;
1302598fe0ddSCristian Dumitrescu 	int blank_or_comment = 0;
1303598fe0ddSCristian Dumitrescu 
1304598fe0ddSCristian Dumitrescu 	/* Check input arguments. */
1305598fe0ddSCristian Dumitrescu 	if (!string || !string[0])
1306598fe0ddSCristian Dumitrescu 		goto error;
1307598fe0ddSCristian Dumitrescu 
1308598fe0ddSCristian Dumitrescu 	/* Memory allocation. */
1309598fe0ddSCristian Dumitrescu 	s0 = strdup(string);
1310598fe0ddSCristian Dumitrescu 	if (!s0)
1311598fe0ddSCristian Dumitrescu 		goto error;
1312598fe0ddSCristian Dumitrescu 
1313598fe0ddSCristian Dumitrescu 	/* Parse the string into tokens. */
1314598fe0ddSCristian Dumitrescu 	for (s = s0; ; ) {
1315598fe0ddSCristian Dumitrescu 		char *token;
1316598fe0ddSCristian Dumitrescu 
1317598fe0ddSCristian Dumitrescu 		token = strtok_r(s, " \f\n\r\t\v", &s);
1318598fe0ddSCristian Dumitrescu 		if (!token || token_is_comment(token))
1319598fe0ddSCristian Dumitrescu 			break;
1320598fe0ddSCristian Dumitrescu 
1321cfcc7bf8SCristian Dumitrescu 		if (n_tokens >= GROUP_MEMBER_INFO_TOKENS_MAX)
1322598fe0ddSCristian Dumitrescu 			goto error;
1323598fe0ddSCristian Dumitrescu 
1324598fe0ddSCristian Dumitrescu 		token_array[n_tokens] = token;
1325598fe0ddSCristian Dumitrescu 		n_tokens++;
1326598fe0ddSCristian Dumitrescu 	}
1327598fe0ddSCristian Dumitrescu 
1328598fe0ddSCristian Dumitrescu 	if (!n_tokens) {
1329598fe0ddSCristian Dumitrescu 		blank_or_comment = 1;
1330598fe0ddSCristian Dumitrescu 		goto error;
1331598fe0ddSCristian Dumitrescu 	}
1332598fe0ddSCristian Dumitrescu 
1333598fe0ddSCristian Dumitrescu 	tokens = token_array;
1334598fe0ddSCristian Dumitrescu 
1335598fe0ddSCristian Dumitrescu 	if (n_tokens < 4 ||
1336598fe0ddSCristian Dumitrescu 		strcmp(tokens[0], "group") ||
1337598fe0ddSCristian Dumitrescu 		strcmp(tokens[2], "member"))
1338598fe0ddSCristian Dumitrescu 		goto error;
1339598fe0ddSCristian Dumitrescu 
1340598fe0ddSCristian Dumitrescu 	/*
1341598fe0ddSCristian Dumitrescu 	 * Group ID.
1342598fe0ddSCristian Dumitrescu 	 */
1343598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&group_id_val, tokens[1]) != 0)
1344598fe0ddSCristian Dumitrescu 		goto error;
1345598fe0ddSCristian Dumitrescu 	*group_id = group_id_val;
1346598fe0ddSCristian Dumitrescu 
1347598fe0ddSCristian Dumitrescu 	/*
1348598fe0ddSCristian Dumitrescu 	 * Member ID.
1349598fe0ddSCristian Dumitrescu 	 */
1350598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&member_id_val, tokens[3]) != 0)
1351598fe0ddSCristian Dumitrescu 		goto error;
1352598fe0ddSCristian Dumitrescu 	*member_id = member_id_val;
1353598fe0ddSCristian Dumitrescu 
1354598fe0ddSCristian Dumitrescu 	tokens += 4;
1355598fe0ddSCristian Dumitrescu 	n_tokens -= 4;
1356598fe0ddSCristian Dumitrescu 
1357598fe0ddSCristian Dumitrescu 	/*
1358598fe0ddSCristian Dumitrescu 	 * Weight.
1359598fe0ddSCristian Dumitrescu 	 */
1360598fe0ddSCristian Dumitrescu 	if (n_tokens && !strcmp(tokens[0], "weight")) {
1361598fe0ddSCristian Dumitrescu 		if (n_tokens < 2)
1362598fe0ddSCristian Dumitrescu 			goto error;
1363598fe0ddSCristian Dumitrescu 
1364598fe0ddSCristian Dumitrescu 		if (parser_read_uint32(&weight_val, tokens[1]) != 0)
1365598fe0ddSCristian Dumitrescu 			goto error;
1366598fe0ddSCristian Dumitrescu 		*weight = weight_val;
1367598fe0ddSCristian Dumitrescu 
1368598fe0ddSCristian Dumitrescu 		tokens += 2;
1369598fe0ddSCristian Dumitrescu 		n_tokens -= 2;
1370598fe0ddSCristian Dumitrescu 	}
1371598fe0ddSCristian Dumitrescu 
1372598fe0ddSCristian Dumitrescu 	if (n_tokens)
1373598fe0ddSCristian Dumitrescu 		goto error;
1374598fe0ddSCristian Dumitrescu 
1375598fe0ddSCristian Dumitrescu 	free(s0);
1376598fe0ddSCristian Dumitrescu 	return 0;
1377598fe0ddSCristian Dumitrescu 
1378598fe0ddSCristian Dumitrescu error:
1379598fe0ddSCristian Dumitrescu 	free(s0);
1380598fe0ddSCristian Dumitrescu 	if (is_blank_or_comment)
1381598fe0ddSCristian Dumitrescu 		*is_blank_or_comment = blank_or_comment;
1382598fe0ddSCristian Dumitrescu 	return -EINVAL;
1383598fe0ddSCristian Dumitrescu }
1384598fe0ddSCristian Dumitrescu 
1385598fe0ddSCristian Dumitrescu static int
1386598fe0ddSCristian Dumitrescu pipeline_selector_group_members_add(struct rte_swx_ctl_pipeline *p,
1387598fe0ddSCristian Dumitrescu 			   const char *selector_name,
1388598fe0ddSCristian Dumitrescu 			   FILE *file,
1389598fe0ddSCristian Dumitrescu 			   uint32_t *file_line_number)
1390598fe0ddSCristian Dumitrescu {
1391598fe0ddSCristian Dumitrescu 	char *line = NULL;
1392598fe0ddSCristian Dumitrescu 	uint32_t line_id = 0;
1393598fe0ddSCristian Dumitrescu 	int status = 0;
1394598fe0ddSCristian Dumitrescu 
1395598fe0ddSCristian Dumitrescu 	/* Buffer allocation. */
1396598fe0ddSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
1397598fe0ddSCristian Dumitrescu 	if (!line)
1398598fe0ddSCristian Dumitrescu 		return -ENOMEM;
1399598fe0ddSCristian Dumitrescu 
1400598fe0ddSCristian Dumitrescu 	/* File read. */
1401598fe0ddSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
1402598fe0ddSCristian Dumitrescu 		uint32_t group_id, member_id, weight;
1403598fe0ddSCristian Dumitrescu 		int is_blank_or_comment;
1404598fe0ddSCristian Dumitrescu 
1405598fe0ddSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
1406598fe0ddSCristian Dumitrescu 			break;
1407598fe0ddSCristian Dumitrescu 
1408598fe0ddSCristian Dumitrescu 		status = pipeline_selector_group_member_read(line,
1409598fe0ddSCristian Dumitrescu 							      &group_id,
1410598fe0ddSCristian Dumitrescu 							      &member_id,
1411598fe0ddSCristian Dumitrescu 							      &weight,
1412598fe0ddSCristian Dumitrescu 							      &is_blank_or_comment);
1413598fe0ddSCristian Dumitrescu 		if (status) {
1414598fe0ddSCristian Dumitrescu 			if (is_blank_or_comment)
1415598fe0ddSCristian Dumitrescu 				continue;
1416598fe0ddSCristian Dumitrescu 
1417598fe0ddSCristian Dumitrescu 			goto error;
1418598fe0ddSCristian Dumitrescu 		}
1419598fe0ddSCristian Dumitrescu 
1420598fe0ddSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_selector_group_member_add(p,
1421598fe0ddSCristian Dumitrescu 			selector_name,
1422598fe0ddSCristian Dumitrescu 			group_id,
1423598fe0ddSCristian Dumitrescu 			member_id,
1424598fe0ddSCristian Dumitrescu 			weight);
1425598fe0ddSCristian Dumitrescu 		if (status)
1426598fe0ddSCristian Dumitrescu 			goto error;
1427598fe0ddSCristian Dumitrescu 	}
1428598fe0ddSCristian Dumitrescu 
1429598fe0ddSCristian Dumitrescu error:
1430598fe0ddSCristian Dumitrescu 	free(line);
1431598fe0ddSCristian Dumitrescu 	*file_line_number = line_id;
1432598fe0ddSCristian Dumitrescu 	return status;
1433598fe0ddSCristian Dumitrescu }
1434598fe0ddSCristian Dumitrescu 
1435598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_member_add_help[] =
1436598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group member add <file_name>";
1437598fe0ddSCristian Dumitrescu 
1438598fe0ddSCristian Dumitrescu static void
1439598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_member_add(char **tokens,
1440598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1441598fe0ddSCristian Dumitrescu 	char *out,
1442598fe0ddSCristian Dumitrescu 	size_t out_size,
1443b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1444598fe0ddSCristian Dumitrescu {
1445b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1446598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name, *file_name;
1447598fe0ddSCristian Dumitrescu 	FILE *file = NULL;
1448598fe0ddSCristian Dumitrescu 	uint32_t file_line_number = 0;
1449598fe0ddSCristian Dumitrescu 	int status;
1450598fe0ddSCristian Dumitrescu 
1451598fe0ddSCristian Dumitrescu 	if (n_tokens != 8) {
1452598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1453598fe0ddSCristian Dumitrescu 		return;
1454598fe0ddSCristian Dumitrescu 	}
1455598fe0ddSCristian Dumitrescu 
1456598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1457b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1458b9559f94SCristian Dumitrescu 	if (!ctl) {
1459598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1460598fe0ddSCristian Dumitrescu 		return;
1461598fe0ddSCristian Dumitrescu 	}
1462598fe0ddSCristian Dumitrescu 
1463598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1464598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1465598fe0ddSCristian Dumitrescu 		return;
1466598fe0ddSCristian Dumitrescu 	}
1467598fe0ddSCristian Dumitrescu 
1468598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1469598fe0ddSCristian Dumitrescu 
1470598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1471598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "member") ||
1472598fe0ddSCristian Dumitrescu 		strcmp(tokens[6], "add")) {
1473598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group member add");
1474598fe0ddSCristian Dumitrescu 		return;
1475598fe0ddSCristian Dumitrescu 	}
1476598fe0ddSCristian Dumitrescu 
1477598fe0ddSCristian Dumitrescu 	file_name = tokens[7];
1478598fe0ddSCristian Dumitrescu 	file = fopen(file_name, "r");
1479598fe0ddSCristian Dumitrescu 	if (!file) {
1480598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
1481598fe0ddSCristian Dumitrescu 		return;
1482598fe0ddSCristian Dumitrescu 	}
1483598fe0ddSCristian Dumitrescu 
1484b9559f94SCristian Dumitrescu 	status = pipeline_selector_group_members_add(ctl,
1485598fe0ddSCristian Dumitrescu 					    selector_name,
1486598fe0ddSCristian Dumitrescu 					    file,
1487598fe0ddSCristian Dumitrescu 					    &file_line_number);
1488598fe0ddSCristian Dumitrescu 	if (status)
1489598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
1490598fe0ddSCristian Dumitrescu 			 file_name,
1491598fe0ddSCristian Dumitrescu 			 file_line_number);
1492598fe0ddSCristian Dumitrescu 
1493598fe0ddSCristian Dumitrescu 	fclose(file);
1494598fe0ddSCristian Dumitrescu }
1495598fe0ddSCristian Dumitrescu 
1496598fe0ddSCristian Dumitrescu static int
1497598fe0ddSCristian Dumitrescu pipeline_selector_group_members_delete(struct rte_swx_ctl_pipeline *p,
1498598fe0ddSCristian Dumitrescu 			   const char *selector_name,
1499598fe0ddSCristian Dumitrescu 			   FILE *file,
1500598fe0ddSCristian Dumitrescu 			   uint32_t *file_line_number)
1501598fe0ddSCristian Dumitrescu {
1502598fe0ddSCristian Dumitrescu 	char *line = NULL;
1503598fe0ddSCristian Dumitrescu 	uint32_t line_id = 0;
1504598fe0ddSCristian Dumitrescu 	int status = 0;
1505598fe0ddSCristian Dumitrescu 
1506598fe0ddSCristian Dumitrescu 	/* Buffer allocation. */
1507598fe0ddSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
1508598fe0ddSCristian Dumitrescu 	if (!line)
1509598fe0ddSCristian Dumitrescu 		return -ENOMEM;
1510598fe0ddSCristian Dumitrescu 
1511598fe0ddSCristian Dumitrescu 	/* File read. */
1512598fe0ddSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
1513598fe0ddSCristian Dumitrescu 		uint32_t group_id, member_id, weight;
1514598fe0ddSCristian Dumitrescu 		int is_blank_or_comment;
1515598fe0ddSCristian Dumitrescu 
1516598fe0ddSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
1517598fe0ddSCristian Dumitrescu 			break;
1518598fe0ddSCristian Dumitrescu 
1519598fe0ddSCristian Dumitrescu 		status = pipeline_selector_group_member_read(line,
1520598fe0ddSCristian Dumitrescu 							      &group_id,
1521598fe0ddSCristian Dumitrescu 							      &member_id,
1522598fe0ddSCristian Dumitrescu 							      &weight,
1523598fe0ddSCristian Dumitrescu 							      &is_blank_or_comment);
1524598fe0ddSCristian Dumitrescu 		if (status) {
1525598fe0ddSCristian Dumitrescu 			if (is_blank_or_comment)
1526598fe0ddSCristian Dumitrescu 				continue;
1527598fe0ddSCristian Dumitrescu 
1528598fe0ddSCristian Dumitrescu 			goto error;
1529598fe0ddSCristian Dumitrescu 		}
1530598fe0ddSCristian Dumitrescu 
1531598fe0ddSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_selector_group_member_delete(p,
1532598fe0ddSCristian Dumitrescu 			selector_name,
1533598fe0ddSCristian Dumitrescu 			group_id,
1534598fe0ddSCristian Dumitrescu 			member_id);
1535598fe0ddSCristian Dumitrescu 		if (status)
1536598fe0ddSCristian Dumitrescu 			goto error;
1537598fe0ddSCristian Dumitrescu 	}
1538598fe0ddSCristian Dumitrescu 
1539598fe0ddSCristian Dumitrescu error:
1540598fe0ddSCristian Dumitrescu 	free(line);
1541598fe0ddSCristian Dumitrescu 	*file_line_number = line_id;
1542598fe0ddSCristian Dumitrescu 	return status;
1543598fe0ddSCristian Dumitrescu }
1544598fe0ddSCristian Dumitrescu 
1545598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_member_delete_help[] =
1546598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group member delete <file_name>";
1547598fe0ddSCristian Dumitrescu 
1548598fe0ddSCristian Dumitrescu static void
1549598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_member_delete(char **tokens,
1550598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1551598fe0ddSCristian Dumitrescu 	char *out,
1552598fe0ddSCristian Dumitrescu 	size_t out_size,
1553b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1554598fe0ddSCristian Dumitrescu {
1555b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1556598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name, *file_name;
1557598fe0ddSCristian Dumitrescu 	FILE *file = NULL;
1558598fe0ddSCristian Dumitrescu 	uint32_t file_line_number = 0;
1559598fe0ddSCristian Dumitrescu 	int status;
1560598fe0ddSCristian Dumitrescu 
1561598fe0ddSCristian Dumitrescu 	if (n_tokens != 8) {
1562598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1563598fe0ddSCristian Dumitrescu 		return;
1564598fe0ddSCristian Dumitrescu 	}
1565598fe0ddSCristian Dumitrescu 
1566598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1567b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1568b9559f94SCristian Dumitrescu 	if (!ctl) {
1569598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1570598fe0ddSCristian Dumitrescu 		return;
1571598fe0ddSCristian Dumitrescu 	}
1572598fe0ddSCristian Dumitrescu 
1573598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1574598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1575598fe0ddSCristian Dumitrescu 		return;
1576598fe0ddSCristian Dumitrescu 	}
1577598fe0ddSCristian Dumitrescu 
1578598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1579598fe0ddSCristian Dumitrescu 
1580598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1581598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "member") ||
1582598fe0ddSCristian Dumitrescu 		strcmp(tokens[6], "delete")) {
1583598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group member delete");
1584598fe0ddSCristian Dumitrescu 		return;
1585598fe0ddSCristian Dumitrescu 	}
1586598fe0ddSCristian Dumitrescu 
1587598fe0ddSCristian Dumitrescu 	file_name = tokens[7];
1588598fe0ddSCristian Dumitrescu 	file = fopen(file_name, "r");
1589598fe0ddSCristian Dumitrescu 	if (!file) {
1590598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
1591598fe0ddSCristian Dumitrescu 		return;
1592598fe0ddSCristian Dumitrescu 	}
1593598fe0ddSCristian Dumitrescu 
1594b9559f94SCristian Dumitrescu 	status = pipeline_selector_group_members_delete(ctl,
1595598fe0ddSCristian Dumitrescu 					    selector_name,
1596598fe0ddSCristian Dumitrescu 					    file,
1597598fe0ddSCristian Dumitrescu 					    &file_line_number);
1598598fe0ddSCristian Dumitrescu 	if (status)
1599598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
1600598fe0ddSCristian Dumitrescu 			 file_name,
1601598fe0ddSCristian Dumitrescu 			 file_line_number);
1602598fe0ddSCristian Dumitrescu 
1603598fe0ddSCristian Dumitrescu 	fclose(file);
1604598fe0ddSCristian Dumitrescu }
1605598fe0ddSCristian Dumitrescu 
1606598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_show_help[] =
1607a4c1146cSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> show [filename]\n";
1608598fe0ddSCristian Dumitrescu 
1609598fe0ddSCristian Dumitrescu static void
1610598fe0ddSCristian Dumitrescu cmd_pipeline_selector_show(char **tokens,
1611598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1612598fe0ddSCristian Dumitrescu 	char *out,
1613598fe0ddSCristian Dumitrescu 	size_t out_size,
1614b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1615598fe0ddSCristian Dumitrescu {
1616b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1617598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1618a4c1146cSCristian Dumitrescu 	FILE *file = NULL;
1619598fe0ddSCristian Dumitrescu 	int status;
1620598fe0ddSCristian Dumitrescu 
1621a4c1146cSCristian Dumitrescu 	if (n_tokens != 5 && n_tokens != 6) {
1622598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1623598fe0ddSCristian Dumitrescu 		return;
1624598fe0ddSCristian Dumitrescu 	}
1625598fe0ddSCristian Dumitrescu 
1626598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1627b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1628b9559f94SCristian Dumitrescu 	if (!ctl) {
1629598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1630598fe0ddSCristian Dumitrescu 		return;
1631598fe0ddSCristian Dumitrescu 	}
1632598fe0ddSCristian Dumitrescu 
1633598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1634a4c1146cSCristian Dumitrescu 
1635a4c1146cSCristian Dumitrescu 	file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
1636a4c1146cSCristian Dumitrescu 	if (!file) {
1637a4c1146cSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
1638a4c1146cSCristian Dumitrescu 		return;
1639a4c1146cSCristian Dumitrescu 	}
1640a4c1146cSCristian Dumitrescu 
1641b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_fprintf(file, ctl, selector_name);
1642598fe0ddSCristian Dumitrescu 	if (status)
1643598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "selector_name");
1644a4c1146cSCristian Dumitrescu 
1645a4c1146cSCristian Dumitrescu 	if (file)
1646a4c1146cSCristian Dumitrescu 		fclose(file);
1647598fe0ddSCristian Dumitrescu }
1648598fe0ddSCristian Dumitrescu 
16498bd4862fSCristian Dumitrescu static int
16508bd4862fSCristian Dumitrescu pipeline_learner_default_entry_add(struct rte_swx_ctl_pipeline *p,
16518bd4862fSCristian Dumitrescu 				   const char *learner_name,
16528bd4862fSCristian Dumitrescu 				   FILE *file,
16538bd4862fSCristian Dumitrescu 				   uint32_t *file_line_number)
16548bd4862fSCristian Dumitrescu {
16558bd4862fSCristian Dumitrescu 	char *line = NULL;
16568bd4862fSCristian Dumitrescu 	uint32_t line_id = 0;
16578bd4862fSCristian Dumitrescu 	int status = 0;
16588bd4862fSCristian Dumitrescu 
16598bd4862fSCristian Dumitrescu 	/* Buffer allocation. */
16608bd4862fSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
16618bd4862fSCristian Dumitrescu 	if (!line)
16628bd4862fSCristian Dumitrescu 		return -ENOMEM;
16638bd4862fSCristian Dumitrescu 
16648bd4862fSCristian Dumitrescu 	/* File read. */
16658bd4862fSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
16668bd4862fSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
16678bd4862fSCristian Dumitrescu 		int is_blank_or_comment;
16688bd4862fSCristian Dumitrescu 
16698bd4862fSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
16708bd4862fSCristian Dumitrescu 			break;
16718bd4862fSCristian Dumitrescu 
16728bd4862fSCristian Dumitrescu 		entry = rte_swx_ctl_pipeline_learner_default_entry_read(p,
16738bd4862fSCristian Dumitrescu 									learner_name,
16748bd4862fSCristian Dumitrescu 									line,
16758bd4862fSCristian Dumitrescu 									&is_blank_or_comment);
16768bd4862fSCristian Dumitrescu 		if (!entry) {
16778bd4862fSCristian Dumitrescu 			if (is_blank_or_comment)
16788bd4862fSCristian Dumitrescu 				continue;
16798bd4862fSCristian Dumitrescu 
16808bd4862fSCristian Dumitrescu 			status = -EINVAL;
16818bd4862fSCristian Dumitrescu 			goto error;
16828bd4862fSCristian Dumitrescu 		}
16838bd4862fSCristian Dumitrescu 
16848bd4862fSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_learner_default_entry_add(p,
16858bd4862fSCristian Dumitrescu 									learner_name,
16868bd4862fSCristian Dumitrescu 									entry);
16878bd4862fSCristian Dumitrescu 		table_entry_free(entry);
16888bd4862fSCristian Dumitrescu 		if (status)
16898bd4862fSCristian Dumitrescu 			goto error;
16908bd4862fSCristian Dumitrescu 	}
16918bd4862fSCristian Dumitrescu 
16928bd4862fSCristian Dumitrescu error:
16938bd4862fSCristian Dumitrescu 	*file_line_number = line_id;
16948bd4862fSCristian Dumitrescu 	free(line);
16958bd4862fSCristian Dumitrescu 	return status;
16968bd4862fSCristian Dumitrescu }
16978bd4862fSCristian Dumitrescu 
16988bd4862fSCristian Dumitrescu static const char cmd_pipeline_learner_default_help[] =
16998bd4862fSCristian Dumitrescu "pipeline <pipeline_name> learner <learner_name> default <file_name>\n";
17008bd4862fSCristian Dumitrescu 
17018bd4862fSCristian Dumitrescu static void
17028bd4862fSCristian Dumitrescu cmd_pipeline_learner_default(char **tokens,
17038bd4862fSCristian Dumitrescu 			     uint32_t n_tokens,
17048bd4862fSCristian Dumitrescu 			     char *out,
17058bd4862fSCristian Dumitrescu 			     size_t out_size,
1706b9559f94SCristian Dumitrescu 			     void *obj __rte_unused)
17078bd4862fSCristian Dumitrescu {
1708b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
17098bd4862fSCristian Dumitrescu 	char *pipeline_name, *learner_name, *file_name;
17108bd4862fSCristian Dumitrescu 	FILE *file = NULL;
17118bd4862fSCristian Dumitrescu 	uint32_t file_line_number = 0;
17128bd4862fSCristian Dumitrescu 	int status;
17138bd4862fSCristian Dumitrescu 
17148bd4862fSCristian Dumitrescu 	if (n_tokens != 6) {
17158bd4862fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
17168bd4862fSCristian Dumitrescu 		return;
17178bd4862fSCristian Dumitrescu 	}
17188bd4862fSCristian Dumitrescu 
17198bd4862fSCristian Dumitrescu 	pipeline_name = tokens[1];
1720b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1721b9559f94SCristian Dumitrescu 	if (!ctl) {
17228bd4862fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
17238bd4862fSCristian Dumitrescu 		return;
17248bd4862fSCristian Dumitrescu 	}
17258bd4862fSCristian Dumitrescu 
17268bd4862fSCristian Dumitrescu 	learner_name = tokens[3];
17278bd4862fSCristian Dumitrescu 
17288bd4862fSCristian Dumitrescu 	file_name = tokens[5];
17298bd4862fSCristian Dumitrescu 	file = fopen(file_name, "r");
17308bd4862fSCristian Dumitrescu 	if (!file) {
17318bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
17328bd4862fSCristian Dumitrescu 		return;
17338bd4862fSCristian Dumitrescu 	}
17348bd4862fSCristian Dumitrescu 
1735b9559f94SCristian Dumitrescu 	status = pipeline_learner_default_entry_add(ctl,
17368bd4862fSCristian Dumitrescu 						    learner_name,
17378bd4862fSCristian Dumitrescu 						    file,
17388bd4862fSCristian Dumitrescu 						    &file_line_number);
17398bd4862fSCristian Dumitrescu 	if (status)
17408bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
17418bd4862fSCristian Dumitrescu 			 file_name,
17428bd4862fSCristian Dumitrescu 			 file_line_number);
17438bd4862fSCristian Dumitrescu 
17448bd4862fSCristian Dumitrescu 	fclose(file);
17458bd4862fSCristian Dumitrescu }
17468bd4862fSCristian Dumitrescu 
174775129cebSChurchill Khangar static const char cmd_pipeline_commit_help[] =
174875129cebSChurchill Khangar "pipeline <pipeline_name> commit\n";
174975129cebSChurchill Khangar 
175075129cebSChurchill Khangar static void
175175129cebSChurchill Khangar cmd_pipeline_commit(char **tokens,
175275129cebSChurchill Khangar 	uint32_t n_tokens,
175375129cebSChurchill Khangar 	char *out,
175475129cebSChurchill Khangar 	size_t out_size,
1755b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
175675129cebSChurchill Khangar {
1757b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
175875129cebSChurchill Khangar 	char *pipeline_name;
175975129cebSChurchill Khangar 	int status;
176075129cebSChurchill Khangar 
176175129cebSChurchill Khangar 	if (n_tokens != 3) {
176275129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
176375129cebSChurchill Khangar 		return;
176475129cebSChurchill Khangar 	}
176575129cebSChurchill Khangar 
176675129cebSChurchill Khangar 	pipeline_name = tokens[1];
1767b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1768b9559f94SCristian Dumitrescu 	if (!ctl) {
176975129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
177075129cebSChurchill Khangar 		return;
17715074e1d5SCristian Dumitrescu 	}
17725074e1d5SCristian Dumitrescu 
1773b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_commit(ctl, 1);
177475129cebSChurchill Khangar 	if (status)
177575129cebSChurchill Khangar 		snprintf(out, out_size, "Commit failed. "
177675129cebSChurchill Khangar 			"Use \"commit\" to retry or \"abort\" to discard the pending work.\n");
17775074e1d5SCristian Dumitrescu }
17785074e1d5SCristian Dumitrescu 
177975129cebSChurchill Khangar static const char cmd_pipeline_abort_help[] =
178075129cebSChurchill Khangar "pipeline <pipeline_name> abort\n";
17815074e1d5SCristian Dumitrescu 
178275129cebSChurchill Khangar static void
178375129cebSChurchill Khangar cmd_pipeline_abort(char **tokens,
178475129cebSChurchill Khangar 	uint32_t n_tokens,
178575129cebSChurchill Khangar 	char *out,
178675129cebSChurchill Khangar 	size_t out_size,
1787b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
178875129cebSChurchill Khangar {
1789b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
179075129cebSChurchill Khangar 	char *pipeline_name;
17915074e1d5SCristian Dumitrescu 
179275129cebSChurchill Khangar 	if (n_tokens != 3) {
179375129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
17945074e1d5SCristian Dumitrescu 		return;
179575129cebSChurchill Khangar 	}
17965074e1d5SCristian Dumitrescu 
179775129cebSChurchill Khangar 	pipeline_name = tokens[1];
1798b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1799b9559f94SCristian Dumitrescu 	if (!ctl) {
180075129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
180175129cebSChurchill Khangar 		return;
180275129cebSChurchill Khangar 	}
180375129cebSChurchill Khangar 
1804b9559f94SCristian Dumitrescu 	rte_swx_ctl_pipeline_abort(ctl);
18055074e1d5SCristian Dumitrescu }
18065074e1d5SCristian Dumitrescu 
180764cfcebdSCristian Dumitrescu static const char cmd_pipeline_regrd_help[] =
180883f58a7bSCristian Dumitrescu "pipeline <pipeline_name> regrd <register_array_name>\n"
180983f58a7bSCristian Dumitrescu 	"index <index>\n"
181083f58a7bSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
181164cfcebdSCristian Dumitrescu 
181264cfcebdSCristian Dumitrescu static void
181364cfcebdSCristian Dumitrescu cmd_pipeline_regrd(char **tokens,
181464cfcebdSCristian Dumitrescu 	uint32_t n_tokens,
181564cfcebdSCristian Dumitrescu 	char *out,
181664cfcebdSCristian Dumitrescu 	size_t out_size,
1817b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
181864cfcebdSCristian Dumitrescu {
1819b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
182083f58a7bSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
182183f58a7bSCristian Dumitrescu 	const char *pipeline_name, *name;
182264cfcebdSCristian Dumitrescu 	uint64_t value;
182364cfcebdSCristian Dumitrescu 	int status;
182464cfcebdSCristian Dumitrescu 
182583f58a7bSCristian Dumitrescu 	if (n_tokens < 5) {
182664cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
182764cfcebdSCristian Dumitrescu 		return;
182864cfcebdSCristian Dumitrescu 	}
182964cfcebdSCristian Dumitrescu 
183083f58a7bSCristian Dumitrescu 	pipeline_name = tokens[1];
183183f58a7bSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
183283f58a7bSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
183383f58a7bSCristian Dumitrescu 	if (!p || !ctl) {
183464cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
183564cfcebdSCristian Dumitrescu 		return;
183664cfcebdSCristian Dumitrescu 	}
183764cfcebdSCristian Dumitrescu 
183864cfcebdSCristian Dumitrescu 	if (strcmp(tokens[2], "regrd")) {
183964cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "regrd");
184064cfcebdSCristian Dumitrescu 		return;
184164cfcebdSCristian Dumitrescu 	}
184264cfcebdSCristian Dumitrescu 
184364cfcebdSCristian Dumitrescu 	name = tokens[3];
184464cfcebdSCristian Dumitrescu 
184583f58a7bSCristian Dumitrescu 	/* index. */
184683f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[4], "index")) {
1847327820afSAli Alnubani 		uint32_t idx = 0;
184883f58a7bSCristian Dumitrescu 
184983f58a7bSCristian Dumitrescu 		if (n_tokens != 6) {
185083f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
185183f58a7bSCristian Dumitrescu 			return;
185283f58a7bSCristian Dumitrescu 		}
185383f58a7bSCristian Dumitrescu 
185483f58a7bSCristian Dumitrescu 		if (parser_read_uint32(&idx, tokens[5])) {
185564cfcebdSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index");
185664cfcebdSCristian Dumitrescu 			return;
185764cfcebdSCristian Dumitrescu 		}
185864cfcebdSCristian Dumitrescu 
1859b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_read(p, name, idx, &value);
186064cfcebdSCristian Dumitrescu 		if (status) {
186164cfcebdSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
186264cfcebdSCristian Dumitrescu 			return;
186364cfcebdSCristian Dumitrescu 		}
186464cfcebdSCristian Dumitrescu 
186564cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
186683f58a7bSCristian Dumitrescu 		return;
186783f58a7bSCristian Dumitrescu 	}
186883f58a7bSCristian Dumitrescu 
186983f58a7bSCristian Dumitrescu 	/* table. */
187083f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[4], "table")) {
187183f58a7bSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
187283f58a7bSCristian Dumitrescu 		char *table_name;
187383f58a7bSCristian Dumitrescu 
187483f58a7bSCristian Dumitrescu 		if (n_tokens < 8) {
187583f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
187683f58a7bSCristian Dumitrescu 			return;
187783f58a7bSCristian Dumitrescu 		}
187883f58a7bSCristian Dumitrescu 
187983f58a7bSCristian Dumitrescu 		table_name = tokens[5];
188083f58a7bSCristian Dumitrescu 
188183f58a7bSCristian Dumitrescu 		if (strcmp(tokens[6], "match")) {
188283f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
188383f58a7bSCristian Dumitrescu 			return;
188483f58a7bSCristian Dumitrescu 		}
188583f58a7bSCristian Dumitrescu 
188683f58a7bSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[6], n_tokens - 6);
188783f58a7bSCristian Dumitrescu 		if (!entry) {
188883f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
188983f58a7bSCristian Dumitrescu 			return;
189083f58a7bSCristian Dumitrescu 		}
189183f58a7bSCristian Dumitrescu 
189283f58a7bSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_read_with_key(p,
189383f58a7bSCristian Dumitrescu 								     name,
189483f58a7bSCristian Dumitrescu 								     table_name,
189583f58a7bSCristian Dumitrescu 								     entry->key,
189683f58a7bSCristian Dumitrescu 								     &value);
189783f58a7bSCristian Dumitrescu 		table_entry_free(entry);
189883f58a7bSCristian Dumitrescu 		if (status) {
189983f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
190083f58a7bSCristian Dumitrescu 			return;
190183f58a7bSCristian Dumitrescu 		}
190283f58a7bSCristian Dumitrescu 
190383f58a7bSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
190483f58a7bSCristian Dumitrescu 		return;
190583f58a7bSCristian Dumitrescu 	}
190683f58a7bSCristian Dumitrescu 
190783f58a7bSCristian Dumitrescu 	/* anything else. */
190883f58a7bSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[4]);
190983f58a7bSCristian Dumitrescu 	return;
191064cfcebdSCristian Dumitrescu }
191164cfcebdSCristian Dumitrescu 
191264cfcebdSCristian Dumitrescu static const char cmd_pipeline_regwr_help[] =
191383f58a7bSCristian Dumitrescu "pipeline <pipeline_name> regwr <register_array_name> value <value>\n"
191483f58a7bSCristian Dumitrescu 	"index <index>\n"
191583f58a7bSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
191664cfcebdSCristian Dumitrescu 
191764cfcebdSCristian Dumitrescu static void
191864cfcebdSCristian Dumitrescu cmd_pipeline_regwr(char **tokens,
191964cfcebdSCristian Dumitrescu 	uint32_t n_tokens,
192064cfcebdSCristian Dumitrescu 	char *out,
192164cfcebdSCristian Dumitrescu 	size_t out_size,
1922b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
192364cfcebdSCristian Dumitrescu {
1924b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
192583f58a7bSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
192683f58a7bSCristian Dumitrescu 	const char *pipeline_name, *name;
192783f58a7bSCristian Dumitrescu 	uint64_t value = 0;
192864cfcebdSCristian Dumitrescu 	int status;
192964cfcebdSCristian Dumitrescu 
193083f58a7bSCristian Dumitrescu 	if (n_tokens < 7) {
193164cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
193264cfcebdSCristian Dumitrescu 		return;
193364cfcebdSCristian Dumitrescu 	}
193464cfcebdSCristian Dumitrescu 
193583f58a7bSCristian Dumitrescu 	pipeline_name = tokens[1];
193683f58a7bSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
193783f58a7bSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
193883f58a7bSCristian Dumitrescu 	if (!p || !ctl) {
193964cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
194064cfcebdSCristian Dumitrescu 		return;
194164cfcebdSCristian Dumitrescu 	}
194264cfcebdSCristian Dumitrescu 
194364cfcebdSCristian Dumitrescu 	if (strcmp(tokens[2], "regwr")) {
194464cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "regwr");
194564cfcebdSCristian Dumitrescu 		return;
194664cfcebdSCristian Dumitrescu 	}
194764cfcebdSCristian Dumitrescu 
194864cfcebdSCristian Dumitrescu 	name = tokens[3];
194964cfcebdSCristian Dumitrescu 
195083f58a7bSCristian Dumitrescu 	if (strcmp(tokens[4], "value")) {
195183f58a7bSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "value");
195264cfcebdSCristian Dumitrescu 		return;
195364cfcebdSCristian Dumitrescu 	}
195464cfcebdSCristian Dumitrescu 
195564cfcebdSCristian Dumitrescu 	if (parser_read_uint64(&value, tokens[5])) {
195664cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "value");
195764cfcebdSCristian Dumitrescu 		return;
195864cfcebdSCristian Dumitrescu 	}
195964cfcebdSCristian Dumitrescu 
196083f58a7bSCristian Dumitrescu 	/* index. */
196183f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[6], "index")) {
1962327820afSAli Alnubani 		uint32_t idx = 0;
196383f58a7bSCristian Dumitrescu 
196483f58a7bSCristian Dumitrescu 		if (n_tokens != 8) {
196583f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
196683f58a7bSCristian Dumitrescu 			return;
196783f58a7bSCristian Dumitrescu 		}
196883f58a7bSCristian Dumitrescu 
196983f58a7bSCristian Dumitrescu 		if (parser_read_uint32(&idx, tokens[7])) {
197083f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index");
197183f58a7bSCristian Dumitrescu 			return;
197283f58a7bSCristian Dumitrescu 		}
197383f58a7bSCristian Dumitrescu 
1974b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_write(p, name, idx, value);
197564cfcebdSCristian Dumitrescu 		if (status) {
197664cfcebdSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
197764cfcebdSCristian Dumitrescu 			return;
197864cfcebdSCristian Dumitrescu 		}
197983f58a7bSCristian Dumitrescu 
198083f58a7bSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
198183f58a7bSCristian Dumitrescu 		return;
198283f58a7bSCristian Dumitrescu 	}
198383f58a7bSCristian Dumitrescu 
198483f58a7bSCristian Dumitrescu 	/* table. */
198583f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[6], "table")) {
198683f58a7bSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
198783f58a7bSCristian Dumitrescu 		char *table_name;
198883f58a7bSCristian Dumitrescu 
198983f58a7bSCristian Dumitrescu 		if (n_tokens < 10) {
199083f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
199183f58a7bSCristian Dumitrescu 			return;
199283f58a7bSCristian Dumitrescu 		}
199383f58a7bSCristian Dumitrescu 
199483f58a7bSCristian Dumitrescu 		table_name = tokens[7];
199583f58a7bSCristian Dumitrescu 
199683f58a7bSCristian Dumitrescu 		if (strcmp(tokens[8], "match")) {
199783f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
199883f58a7bSCristian Dumitrescu 			return;
199983f58a7bSCristian Dumitrescu 		}
200083f58a7bSCristian Dumitrescu 
200183f58a7bSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[8], n_tokens - 8);
200283f58a7bSCristian Dumitrescu 		if (!entry) {
200383f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
200483f58a7bSCristian Dumitrescu 			return;
200583f58a7bSCristian Dumitrescu 		}
200683f58a7bSCristian Dumitrescu 
200783f58a7bSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_write_with_key(p,
200883f58a7bSCristian Dumitrescu 								      name,
200983f58a7bSCristian Dumitrescu 								      table_name,
201083f58a7bSCristian Dumitrescu 								      entry->key,
201183f58a7bSCristian Dumitrescu 								      value);
201283f58a7bSCristian Dumitrescu 		table_entry_free(entry);
201383f58a7bSCristian Dumitrescu 		if (status) {
201483f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
201583f58a7bSCristian Dumitrescu 			return;
201683f58a7bSCristian Dumitrescu 		}
201783f58a7bSCristian Dumitrescu 
201883f58a7bSCristian Dumitrescu 		return;
201983f58a7bSCristian Dumitrescu 	}
202083f58a7bSCristian Dumitrescu 
202183f58a7bSCristian Dumitrescu 	/* anything else. */
202283f58a7bSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[6]);
202383f58a7bSCristian Dumitrescu 	return;
202464cfcebdSCristian Dumitrescu }
202564cfcebdSCristian Dumitrescu 
2026f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_profile_add_help[] =
2027f38913b7SCristian Dumitrescu "pipeline <pipeline_name> meter profile <profile_name> add "
2028f38913b7SCristian Dumitrescu 	"cir <cir> pir <pir> cbs <cbs> pbs <pbs>\n";
2029f38913b7SCristian Dumitrescu 
2030f38913b7SCristian Dumitrescu static void
2031f38913b7SCristian Dumitrescu cmd_pipeline_meter_profile_add(char **tokens,
2032f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2033f38913b7SCristian Dumitrescu 	char *out,
2034f38913b7SCristian Dumitrescu 	size_t out_size,
2035b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2036f38913b7SCristian Dumitrescu {
2037f38913b7SCristian Dumitrescu 	struct rte_meter_trtcm_params params;
2038b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
2039f38913b7SCristian Dumitrescu 	const char *profile_name;
2040f38913b7SCristian Dumitrescu 	int status;
2041f38913b7SCristian Dumitrescu 
2042f38913b7SCristian Dumitrescu 	if (n_tokens != 14) {
2043f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2044f38913b7SCristian Dumitrescu 		return;
2045f38913b7SCristian Dumitrescu 	}
2046f38913b7SCristian Dumitrescu 
2047b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2048b9559f94SCristian Dumitrescu 	if (!p) {
2049f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2050f38913b7SCristian Dumitrescu 		return;
2051f38913b7SCristian Dumitrescu 	}
2052f38913b7SCristian Dumitrescu 
2053f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2054f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2055f38913b7SCristian Dumitrescu 		return;
2056f38913b7SCristian Dumitrescu 	}
2057f38913b7SCristian Dumitrescu 
2058f38913b7SCristian Dumitrescu 	if (strcmp(tokens[3], "profile")) {
2059f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2060f38913b7SCristian Dumitrescu 		return;
2061f38913b7SCristian Dumitrescu 	}
2062f38913b7SCristian Dumitrescu 
2063f38913b7SCristian Dumitrescu 	profile_name = tokens[4];
2064f38913b7SCristian Dumitrescu 
2065f38913b7SCristian Dumitrescu 	if (strcmp(tokens[5], "add")) {
2066f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "add");
2067f38913b7SCristian Dumitrescu 		return;
2068f38913b7SCristian Dumitrescu 	}
2069f38913b7SCristian Dumitrescu 
2070f38913b7SCristian Dumitrescu 	if (strcmp(tokens[6], "cir")) {
2071f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cir");
2072f38913b7SCristian Dumitrescu 		return;
2073f38913b7SCristian Dumitrescu 	}
2074f38913b7SCristian Dumitrescu 
2075f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.cir, tokens[7])) {
2076f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cir");
2077f38913b7SCristian Dumitrescu 		return;
2078f38913b7SCristian Dumitrescu 	}
2079f38913b7SCristian Dumitrescu 
2080f38913b7SCristian Dumitrescu 	if (strcmp(tokens[8], "pir")) {
2081f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pir");
2082f38913b7SCristian Dumitrescu 		return;
2083f38913b7SCristian Dumitrescu 	}
2084f38913b7SCristian Dumitrescu 
2085f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.pir, tokens[9])) {
2086f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pir");
2087f38913b7SCristian Dumitrescu 		return;
2088f38913b7SCristian Dumitrescu 	}
2089f38913b7SCristian Dumitrescu 
2090f38913b7SCristian Dumitrescu 	if (strcmp(tokens[10], "cbs")) {
2091f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cbs");
2092f38913b7SCristian Dumitrescu 		return;
2093f38913b7SCristian Dumitrescu 	}
2094f38913b7SCristian Dumitrescu 
2095f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.cbs, tokens[11])) {
2096f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cbs");
2097f38913b7SCristian Dumitrescu 		return;
2098f38913b7SCristian Dumitrescu 	}
2099f38913b7SCristian Dumitrescu 
2100f38913b7SCristian Dumitrescu 	if (strcmp(tokens[12], "pbs")) {
2101f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pbs");
2102f38913b7SCristian Dumitrescu 		return;
2103f38913b7SCristian Dumitrescu 	}
2104f38913b7SCristian Dumitrescu 
2105f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.pbs, tokens[13])) {
2106f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pbs");
2107f38913b7SCristian Dumitrescu 		return;
2108f38913b7SCristian Dumitrescu 	}
2109f38913b7SCristian Dumitrescu 
2110b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_meter_profile_add(p, profile_name, &params);
2111f38913b7SCristian Dumitrescu 	if (status) {
2112f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "Command failed.\n");
2113f38913b7SCristian Dumitrescu 		return;
2114f38913b7SCristian Dumitrescu 	}
2115f38913b7SCristian Dumitrescu }
2116f38913b7SCristian Dumitrescu 
2117f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_profile_delete_help[] =
2118f38913b7SCristian Dumitrescu "pipeline <pipeline_name> meter profile <profile_name> delete\n";
2119f38913b7SCristian Dumitrescu 
2120f38913b7SCristian Dumitrescu static void
2121f38913b7SCristian Dumitrescu cmd_pipeline_meter_profile_delete(char **tokens,
2122f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2123f38913b7SCristian Dumitrescu 	char *out,
2124f38913b7SCristian Dumitrescu 	size_t out_size,
2125b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2126f38913b7SCristian Dumitrescu {
2127b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
2128f38913b7SCristian Dumitrescu 	const char *profile_name;
2129f38913b7SCristian Dumitrescu 	int status;
2130f38913b7SCristian Dumitrescu 
2131f38913b7SCristian Dumitrescu 	if (n_tokens != 6) {
2132f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2133f38913b7SCristian Dumitrescu 		return;
2134f38913b7SCristian Dumitrescu 	}
2135f38913b7SCristian Dumitrescu 
2136b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2137b9559f94SCristian Dumitrescu 	if (!p) {
2138f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2139f38913b7SCristian Dumitrescu 		return;
2140f38913b7SCristian Dumitrescu 	}
2141f38913b7SCristian Dumitrescu 
2142f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2143f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2144f38913b7SCristian Dumitrescu 		return;
2145f38913b7SCristian Dumitrescu 	}
2146f38913b7SCristian Dumitrescu 
2147f38913b7SCristian Dumitrescu 	if (strcmp(tokens[3], "profile")) {
2148f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2149f38913b7SCristian Dumitrescu 		return;
2150f38913b7SCristian Dumitrescu 	}
2151f38913b7SCristian Dumitrescu 
2152f38913b7SCristian Dumitrescu 	profile_name = tokens[4];
2153f38913b7SCristian Dumitrescu 
2154f38913b7SCristian Dumitrescu 	if (strcmp(tokens[5], "delete")) {
2155f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "delete");
2156f38913b7SCristian Dumitrescu 		return;
2157f38913b7SCristian Dumitrescu 	}
2158f38913b7SCristian Dumitrescu 
2159b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_meter_profile_delete(p, profile_name);
2160f38913b7SCristian Dumitrescu 	if (status) {
2161f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "Command failed.\n");
2162f38913b7SCristian Dumitrescu 		return;
2163f38913b7SCristian Dumitrescu 	}
2164f38913b7SCristian Dumitrescu }
2165f38913b7SCristian Dumitrescu 
2166f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_reset_help[] =
216712eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> reset\n"
216812eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
216912eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2170f38913b7SCristian Dumitrescu 
2171f38913b7SCristian Dumitrescu static void
2172f38913b7SCristian Dumitrescu cmd_pipeline_meter_reset(char **tokens,
2173f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2174f38913b7SCristian Dumitrescu 	char *out,
2175f38913b7SCristian Dumitrescu 	size_t out_size,
2176b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2177f38913b7SCristian Dumitrescu {
2178b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
217912eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
218012eda78dSCristian Dumitrescu 	const char *pipeline_name, *name;
2181f38913b7SCristian Dumitrescu 
218212eda78dSCristian Dumitrescu 	if (n_tokens < 6) {
2183f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2184f38913b7SCristian Dumitrescu 		return;
2185f38913b7SCristian Dumitrescu 	}
2186f38913b7SCristian Dumitrescu 
218712eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
218812eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
218912eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
219012eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2191f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2192f38913b7SCristian Dumitrescu 		return;
2193f38913b7SCristian Dumitrescu 	}
2194f38913b7SCristian Dumitrescu 
2195f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2196f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2197f38913b7SCristian Dumitrescu 		return;
2198f38913b7SCristian Dumitrescu 	}
2199f38913b7SCristian Dumitrescu 
2200f38913b7SCristian Dumitrescu 	name = tokens[3];
2201f38913b7SCristian Dumitrescu 
220212eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "reset")) {
220312eda78dSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "reset");
220412eda78dSCristian Dumitrescu 		return;
220512eda78dSCristian Dumitrescu 	}
220612eda78dSCristian Dumitrescu 
220712eda78dSCristian Dumitrescu 	/* index. */
220812eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "index")) {
220912eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
221012eda78dSCristian Dumitrescu 
221112eda78dSCristian Dumitrescu 		if (n_tokens != 10) {
221212eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
221312eda78dSCristian Dumitrescu 			return;
221412eda78dSCristian Dumitrescu 		}
221512eda78dSCristian Dumitrescu 
221612eda78dSCristian Dumitrescu 		if (strcmp(tokens[6], "from")) {
2217f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
2218f38913b7SCristian Dumitrescu 			return;
2219f38913b7SCristian Dumitrescu 		}
2220f38913b7SCristian Dumitrescu 
222112eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[7])) {
2222f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
2223f38913b7SCristian Dumitrescu 			return;
2224f38913b7SCristian Dumitrescu 		}
2225f38913b7SCristian Dumitrescu 
222612eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "to")) {
2227f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
2228f38913b7SCristian Dumitrescu 			return;
2229f38913b7SCristian Dumitrescu 		}
2230f38913b7SCristian Dumitrescu 
223112eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[9]) || (idx1 < idx0)) {
2232f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
2233f38913b7SCristian Dumitrescu 			return;
2234f38913b7SCristian Dumitrescu 		}
2235f38913b7SCristian Dumitrescu 
2236f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2237f38913b7SCristian Dumitrescu 			int status;
2238f38913b7SCristian Dumitrescu 
2239b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_reset(p, name, idx0);
2240f38913b7SCristian Dumitrescu 			if (status) {
2241f38913b7SCristian Dumitrescu 				snprintf(out, out_size, "Command failed for index %u.\n", idx0);
2242f38913b7SCristian Dumitrescu 				return;
2243f38913b7SCristian Dumitrescu 			}
2244f38913b7SCristian Dumitrescu 		}
224512eda78dSCristian Dumitrescu 
224612eda78dSCristian Dumitrescu 		return;
224712eda78dSCristian Dumitrescu 	}
224812eda78dSCristian Dumitrescu 
224912eda78dSCristian Dumitrescu 	/* table. */
225012eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "table")) {
225112eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
225212eda78dSCristian Dumitrescu 		char *table_name;
225312eda78dSCristian Dumitrescu 		int status;
225412eda78dSCristian Dumitrescu 
225512eda78dSCristian Dumitrescu 		if (n_tokens < 9) {
225612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
225712eda78dSCristian Dumitrescu 			return;
225812eda78dSCristian Dumitrescu 		}
225912eda78dSCristian Dumitrescu 
226012eda78dSCristian Dumitrescu 		table_name = tokens[6];
226112eda78dSCristian Dumitrescu 
226212eda78dSCristian Dumitrescu 		if (strcmp(tokens[7], "match")) {
226312eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
226412eda78dSCristian Dumitrescu 			return;
226512eda78dSCristian Dumitrescu 		}
226612eda78dSCristian Dumitrescu 
226712eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[7], n_tokens - 7);
226812eda78dSCristian Dumitrescu 		if (!entry) {
226912eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
227012eda78dSCristian Dumitrescu 			return;
227112eda78dSCristian Dumitrescu 		}
227212eda78dSCristian Dumitrescu 
227312eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_reset_with_key(p, name, table_name, entry->key);
227412eda78dSCristian Dumitrescu 		table_entry_free(entry);
227512eda78dSCristian Dumitrescu 		if (status) {
227612eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
227712eda78dSCristian Dumitrescu 			return;
227812eda78dSCristian Dumitrescu 		}
227912eda78dSCristian Dumitrescu 
228012eda78dSCristian Dumitrescu 		return;
228112eda78dSCristian Dumitrescu 	}
228212eda78dSCristian Dumitrescu 
228312eda78dSCristian Dumitrescu 	/* anything else. */
228412eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[5]);
228512eda78dSCristian Dumitrescu 	return;
2286f38913b7SCristian Dumitrescu }
2287f38913b7SCristian Dumitrescu 
2288f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_set_help[] =
228912eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> set profile <profile_name>\n"
229012eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
229112eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2292f38913b7SCristian Dumitrescu 
2293f38913b7SCristian Dumitrescu static void
2294f38913b7SCristian Dumitrescu cmd_pipeline_meter_set(char **tokens,
2295f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2296f38913b7SCristian Dumitrescu 	char *out,
2297f38913b7SCristian Dumitrescu 	size_t out_size,
2298b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2299f38913b7SCristian Dumitrescu {
2300b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
230112eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
230212eda78dSCristian Dumitrescu 	const char *pipeline_name, *name, *profile_name;
2303f38913b7SCristian Dumitrescu 
230412eda78dSCristian Dumitrescu 	if (n_tokens < 8) {
2305f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2306f38913b7SCristian Dumitrescu 		return;
2307f38913b7SCristian Dumitrescu 	}
2308f38913b7SCristian Dumitrescu 
230912eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
231012eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
231112eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
231212eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2313f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2314f38913b7SCristian Dumitrescu 		return;
2315f38913b7SCristian Dumitrescu 	}
2316f38913b7SCristian Dumitrescu 
2317f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2318f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2319f38913b7SCristian Dumitrescu 		return;
2320f38913b7SCristian Dumitrescu 	}
2321f38913b7SCristian Dumitrescu 
2322f38913b7SCristian Dumitrescu 	name = tokens[3];
2323f38913b7SCristian Dumitrescu 
232412eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "set")) {
2325f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "set");
2326f38913b7SCristian Dumitrescu 		return;
2327f38913b7SCristian Dumitrescu 	}
2328f38913b7SCristian Dumitrescu 
232912eda78dSCristian Dumitrescu 	if (strcmp(tokens[5], "profile")) {
2330f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2331f38913b7SCristian Dumitrescu 		return;
2332f38913b7SCristian Dumitrescu 	}
2333f38913b7SCristian Dumitrescu 
233412eda78dSCristian Dumitrescu 	profile_name = tokens[6];
233512eda78dSCristian Dumitrescu 
233612eda78dSCristian Dumitrescu 	/* index. */
233712eda78dSCristian Dumitrescu 	if (!strcmp(tokens[7], "index")) {
233812eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
233912eda78dSCristian Dumitrescu 
234012eda78dSCristian Dumitrescu 		if (n_tokens != 12) {
234112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
234212eda78dSCristian Dumitrescu 			return;
234312eda78dSCristian Dumitrescu 		}
234412eda78dSCristian Dumitrescu 
234512eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "from")) {
234612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
234712eda78dSCristian Dumitrescu 			return;
234812eda78dSCristian Dumitrescu 		}
234912eda78dSCristian Dumitrescu 
235012eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[9])) {
235112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
235212eda78dSCristian Dumitrescu 			return;
235312eda78dSCristian Dumitrescu 		}
235412eda78dSCristian Dumitrescu 
235512eda78dSCristian Dumitrescu 		if (strcmp(tokens[10], "to")) {
235612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
235712eda78dSCristian Dumitrescu 			return;
235812eda78dSCristian Dumitrescu 		}
235912eda78dSCristian Dumitrescu 
236012eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[11]) || (idx1 < idx0)) {
236112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
236212eda78dSCristian Dumitrescu 			return;
236312eda78dSCristian Dumitrescu 		}
2364f38913b7SCristian Dumitrescu 
2365f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2366f38913b7SCristian Dumitrescu 			int status;
2367f38913b7SCristian Dumitrescu 
2368b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_set(p, name, idx0, profile_name);
2369f38913b7SCristian Dumitrescu 			if (status) {
2370f38913b7SCristian Dumitrescu 				snprintf(out, out_size, "Command failed for index %u.\n", idx0);
2371f38913b7SCristian Dumitrescu 				return;
2372f38913b7SCristian Dumitrescu 			}
2373f38913b7SCristian Dumitrescu 		}
237412eda78dSCristian Dumitrescu 
237512eda78dSCristian Dumitrescu 		return;
237612eda78dSCristian Dumitrescu 	}
237712eda78dSCristian Dumitrescu 
237812eda78dSCristian Dumitrescu 	/* table. */
237912eda78dSCristian Dumitrescu 	if (!strcmp(tokens[7], "table")) {
238012eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
238112eda78dSCristian Dumitrescu 		char *table_name;
238212eda78dSCristian Dumitrescu 		int status;
238312eda78dSCristian Dumitrescu 
238412eda78dSCristian Dumitrescu 		if (n_tokens < 11) {
238512eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
238612eda78dSCristian Dumitrescu 			return;
238712eda78dSCristian Dumitrescu 		}
238812eda78dSCristian Dumitrescu 
238912eda78dSCristian Dumitrescu 		table_name = tokens[8];
239012eda78dSCristian Dumitrescu 
239112eda78dSCristian Dumitrescu 		if (strcmp(tokens[9], "match")) {
239212eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
239312eda78dSCristian Dumitrescu 			return;
239412eda78dSCristian Dumitrescu 		}
239512eda78dSCristian Dumitrescu 
239612eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[9], n_tokens - 9);
239712eda78dSCristian Dumitrescu 		if (!entry) {
239812eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
239912eda78dSCristian Dumitrescu 			return;
240012eda78dSCristian Dumitrescu 		}
240112eda78dSCristian Dumitrescu 
240212eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_set_with_key(p,
240312eda78dSCristian Dumitrescu 							name,
240412eda78dSCristian Dumitrescu 							table_name,
240512eda78dSCristian Dumitrescu 							entry->key,
240612eda78dSCristian Dumitrescu 							profile_name);
240712eda78dSCristian Dumitrescu 		table_entry_free(entry);
240812eda78dSCristian Dumitrescu 		if (status) {
240912eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
241012eda78dSCristian Dumitrescu 			return;
241112eda78dSCristian Dumitrescu 		}
241212eda78dSCristian Dumitrescu 
241312eda78dSCristian Dumitrescu 		return;
241412eda78dSCristian Dumitrescu 	}
241512eda78dSCristian Dumitrescu 
241612eda78dSCristian Dumitrescu 	/* anything else. */
241712eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[7]);
241812eda78dSCristian Dumitrescu 	return;
2419f38913b7SCristian Dumitrescu }
2420f38913b7SCristian Dumitrescu 
2421f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_stats_help[] =
242212eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> stats\n"
242312eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
242412eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2425f38913b7SCristian Dumitrescu 
2426f38913b7SCristian Dumitrescu static void
2427f38913b7SCristian Dumitrescu cmd_pipeline_meter_stats(char **tokens,
2428f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2429f38913b7SCristian Dumitrescu 	char *out,
2430f38913b7SCristian Dumitrescu 	size_t out_size,
2431b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2432f38913b7SCristian Dumitrescu {
2433f38913b7SCristian Dumitrescu 	struct rte_swx_ctl_meter_stats stats;
2434b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
243512eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
243612eda78dSCristian Dumitrescu 	const char *pipeline_name, *name;
2437f38913b7SCristian Dumitrescu 
243812eda78dSCristian Dumitrescu 	if (n_tokens < 6) {
2439f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2440f38913b7SCristian Dumitrescu 		return;
2441f38913b7SCristian Dumitrescu 	}
2442f38913b7SCristian Dumitrescu 
244312eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
244412eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
244512eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
244612eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2447f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2448f38913b7SCristian Dumitrescu 		return;
2449f38913b7SCristian Dumitrescu 	}
2450f38913b7SCristian Dumitrescu 
2451f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2452f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2453f38913b7SCristian Dumitrescu 		return;
2454f38913b7SCristian Dumitrescu 	}
2455f38913b7SCristian Dumitrescu 
2456f38913b7SCristian Dumitrescu 	name = tokens[3];
2457f38913b7SCristian Dumitrescu 
245812eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "stats")) {
245912eda78dSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "stats");
246012eda78dSCristian Dumitrescu 		return;
246112eda78dSCristian Dumitrescu 	}
246212eda78dSCristian Dumitrescu 
246312eda78dSCristian Dumitrescu 	/* index. */
246412eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "index")) {
246512eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
246612eda78dSCristian Dumitrescu 
246712eda78dSCristian Dumitrescu 		if (n_tokens != 10) {
246812eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
246912eda78dSCristian Dumitrescu 			return;
247012eda78dSCristian Dumitrescu 		}
247112eda78dSCristian Dumitrescu 
247212eda78dSCristian Dumitrescu 		if (strcmp(tokens[6], "from")) {
2473f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
2474f38913b7SCristian Dumitrescu 			return;
2475f38913b7SCristian Dumitrescu 		}
2476f38913b7SCristian Dumitrescu 
247712eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[7])) {
2478f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
2479f38913b7SCristian Dumitrescu 			return;
2480f38913b7SCristian Dumitrescu 		}
2481f38913b7SCristian Dumitrescu 
248212eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "to")) {
2483f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
2484f38913b7SCristian Dumitrescu 			return;
2485f38913b7SCristian Dumitrescu 		}
2486f38913b7SCristian Dumitrescu 
248712eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[9]) || (idx1 < idx0)) {
2488f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
2489f38913b7SCristian Dumitrescu 			return;
2490f38913b7SCristian Dumitrescu 		}
2491f38913b7SCristian Dumitrescu 
2492f38913b7SCristian Dumitrescu 		/* Table header. */
2493f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
2494f38913b7SCristian Dumitrescu 			 "-------",
2495f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------",
2496f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------");
2497f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2498f38913b7SCristian Dumitrescu 		out += strlen(out);
2499f38913b7SCristian Dumitrescu 
2500f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "| %4s | %16s | %16s | %16s | %16s | %16s | %16s |\n",
2501f38913b7SCristian Dumitrescu 			 "METER #",
2502f38913b7SCristian Dumitrescu 			 "GREEN (packets)", "YELLOW (packets)", "RED (packets)",
2503f38913b7SCristian Dumitrescu 			 "GREEN (bytes)", "YELLOW (bytes)", "RED (bytes)");
2504f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2505f38913b7SCristian Dumitrescu 		out += strlen(out);
2506f38913b7SCristian Dumitrescu 
2507f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
2508f38913b7SCristian Dumitrescu 			 "-------",
2509f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------",
2510f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------");
2511f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2512f38913b7SCristian Dumitrescu 		out += strlen(out);
2513f38913b7SCristian Dumitrescu 
2514f38913b7SCristian Dumitrescu 		/* Table rows. */
2515f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2516f38913b7SCristian Dumitrescu 			int status;
2517f38913b7SCristian Dumitrescu 
2518b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_stats_read(p, name, idx0, &stats);
2519f38913b7SCristian Dumitrescu 			if (status) {
252012eda78dSCristian Dumitrescu 				snprintf(out, out_size, "Meter stats error at index %u.\n", idx0);
2521f38913b7SCristian Dumitrescu 				out_size -= strlen(out);
2522f38913b7SCristian Dumitrescu 				out += strlen(out);
2523f38913b7SCristian Dumitrescu 				return;
2524f38913b7SCristian Dumitrescu 			}
2525f38913b7SCristian Dumitrescu 
2526f38913b7SCristian Dumitrescu 			snprintf(out, out_size, "| %7d | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64
2527f38913b7SCristian Dumitrescu 				 " | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64 " |\n",
2528f38913b7SCristian Dumitrescu 				 idx0,
2529f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_GREEN],
2530f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_YELLOW],
2531f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_RED],
2532f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_GREEN],
2533f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_YELLOW],
2534f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_RED]);
2535f38913b7SCristian Dumitrescu 			out_size -= strlen(out);
2536f38913b7SCristian Dumitrescu 			out += strlen(out);
2537f38913b7SCristian Dumitrescu 		}
253812eda78dSCristian Dumitrescu 
253912eda78dSCristian Dumitrescu 		return;
254012eda78dSCristian Dumitrescu 	}
254112eda78dSCristian Dumitrescu 
254212eda78dSCristian Dumitrescu 	/* table. */
254312eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "table")) {
254412eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
254512eda78dSCristian Dumitrescu 		char *table_name;
254612eda78dSCristian Dumitrescu 		int status;
254712eda78dSCristian Dumitrescu 
254812eda78dSCristian Dumitrescu 		if (n_tokens < 9) {
254912eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
255012eda78dSCristian Dumitrescu 			return;
255112eda78dSCristian Dumitrescu 		}
255212eda78dSCristian Dumitrescu 
255312eda78dSCristian Dumitrescu 		table_name = tokens[6];
255412eda78dSCristian Dumitrescu 
255512eda78dSCristian Dumitrescu 		if (strcmp(tokens[7], "match")) {
255612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
255712eda78dSCristian Dumitrescu 			return;
255812eda78dSCristian Dumitrescu 		}
255912eda78dSCristian Dumitrescu 
256012eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[7], n_tokens - 7);
256112eda78dSCristian Dumitrescu 		if (!entry) {
256212eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
256312eda78dSCristian Dumitrescu 			return;
256412eda78dSCristian Dumitrescu 		}
256512eda78dSCristian Dumitrescu 
256612eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_stats_read_with_key(p,
256712eda78dSCristian Dumitrescu 							name,
256812eda78dSCristian Dumitrescu 							table_name,
256912eda78dSCristian Dumitrescu 							entry->key,
257012eda78dSCristian Dumitrescu 							&stats);
257112eda78dSCristian Dumitrescu 		table_entry_free(entry);
257212eda78dSCristian Dumitrescu 		if (status) {
257312eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
257412eda78dSCristian Dumitrescu 			return;
257512eda78dSCristian Dumitrescu 		}
257612eda78dSCristian Dumitrescu 
257712eda78dSCristian Dumitrescu 		/* Table header. */
257812eda78dSCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
257912eda78dSCristian Dumitrescu 			 "-------",
258012eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------",
258112eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------");
258212eda78dSCristian Dumitrescu 		out_size -= strlen(out);
258312eda78dSCristian Dumitrescu 		out += strlen(out);
258412eda78dSCristian Dumitrescu 
258512eda78dSCristian Dumitrescu 		snprintf(out, out_size, "| %4s | %16s | %16s | %16s | %16s | %16s | %16s |\n",
258612eda78dSCristian Dumitrescu 			 "METER #",
258712eda78dSCristian Dumitrescu 			 "GREEN (packets)", "YELLOW (packets)", "RED (packets)",
258812eda78dSCristian Dumitrescu 			 "GREEN (bytes)", "YELLOW (bytes)", "RED (bytes)");
258912eda78dSCristian Dumitrescu 		out_size -= strlen(out);
259012eda78dSCristian Dumitrescu 		out += strlen(out);
259112eda78dSCristian Dumitrescu 
259212eda78dSCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
259312eda78dSCristian Dumitrescu 			 "-------",
259412eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------",
259512eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------");
259612eda78dSCristian Dumitrescu 		out_size -= strlen(out);
259712eda78dSCristian Dumitrescu 		out += strlen(out);
259812eda78dSCristian Dumitrescu 
259912eda78dSCristian Dumitrescu 		/* Table row. */
260012eda78dSCristian Dumitrescu 		snprintf(out, out_size, "| %7d | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64
260112eda78dSCristian Dumitrescu 			 " | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64 " |\n",
260212eda78dSCristian Dumitrescu 			 0,
260312eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_GREEN],
260412eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_YELLOW],
260512eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_RED],
260612eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_GREEN],
260712eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_YELLOW],
260812eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_RED]);
260912eda78dSCristian Dumitrescu 		out_size -= strlen(out);
261012eda78dSCristian Dumitrescu 		out += strlen(out);
261112eda78dSCristian Dumitrescu 
261212eda78dSCristian Dumitrescu 		return;
261312eda78dSCristian Dumitrescu 	}
261412eda78dSCristian Dumitrescu 
261512eda78dSCristian Dumitrescu 	/* anything else. */
261612eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[5]);
261712eda78dSCristian Dumitrescu 	return;
2618f38913b7SCristian Dumitrescu }
2619f38913b7SCristian Dumitrescu 
26208ba342ceSCristian Dumitrescu static const char cmd_pipeline_rss_help[] =
26218ba342ceSCristian Dumitrescu "pipeline <pipeline_name> rss <rss_obj_name> key <key_byte0> ...\n";
26228ba342ceSCristian Dumitrescu 
26238ba342ceSCristian Dumitrescu static void
26248ba342ceSCristian Dumitrescu cmd_pipeline_rss(char **tokens,
26258ba342ceSCristian Dumitrescu 	uint32_t n_tokens,
26268ba342ceSCristian Dumitrescu 	char *out,
26278ba342ceSCristian Dumitrescu 	size_t out_size,
26288ba342ceSCristian Dumitrescu 	void *obj __rte_unused)
26298ba342ceSCristian Dumitrescu {
26308ba342ceSCristian Dumitrescu 	uint8_t rss_key[CMD_MAX_TOKENS];
26318ba342ceSCristian Dumitrescu 	struct rte_swx_pipeline *p;
26328ba342ceSCristian Dumitrescu 	const char *rss_obj_name;
26338ba342ceSCristian Dumitrescu 	uint32_t rss_key_size, i;
26348ba342ceSCristian Dumitrescu 	int status;
26358ba342ceSCristian Dumitrescu 
26368ba342ceSCristian Dumitrescu 	if (n_tokens < 6) {
26378ba342ceSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
26388ba342ceSCristian Dumitrescu 		return;
26398ba342ceSCristian Dumitrescu 	}
26408ba342ceSCristian Dumitrescu 
26418ba342ceSCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
26428ba342ceSCristian Dumitrescu 	if (!p) {
26438ba342ceSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
26448ba342ceSCristian Dumitrescu 		return;
26458ba342ceSCristian Dumitrescu 	}
26468ba342ceSCristian Dumitrescu 
26478ba342ceSCristian Dumitrescu 	if (strcmp(tokens[2], "rss")) {
26488ba342ceSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
26498ba342ceSCristian Dumitrescu 		return;
26508ba342ceSCristian Dumitrescu 	}
26518ba342ceSCristian Dumitrescu 
26528ba342ceSCristian Dumitrescu 	rss_obj_name = tokens[3];
26538ba342ceSCristian Dumitrescu 
26548ba342ceSCristian Dumitrescu 	if (strcmp(tokens[4], "key")) {
26558ba342ceSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "key");
26568ba342ceSCristian Dumitrescu 		return;
26578ba342ceSCristian Dumitrescu 	}
26588ba342ceSCristian Dumitrescu 
26598ba342ceSCristian Dumitrescu 	tokens += 5;
26608ba342ceSCristian Dumitrescu 	n_tokens -= 5;
26618ba342ceSCristian Dumitrescu 	rss_key_size = n_tokens;
26628ba342ceSCristian Dumitrescu 
26638ba342ceSCristian Dumitrescu 	for (i = 0; i < rss_key_size; i++) {
26648ba342ceSCristian Dumitrescu 		uint32_t key_byte;
26658ba342ceSCristian Dumitrescu 
26668ba342ceSCristian Dumitrescu 		if (parser_read_uint32(&key_byte, tokens[i]) || (key_byte >= UINT8_MAX)) {
26678ba342ceSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "key byte");
26688ba342ceSCristian Dumitrescu 			return;
26698ba342ceSCristian Dumitrescu 		}
26708ba342ceSCristian Dumitrescu 
26718ba342ceSCristian Dumitrescu 		rss_key[i] = (uint8_t)key_byte;
26728ba342ceSCristian Dumitrescu 	}
26738ba342ceSCristian Dumitrescu 
26748ba342ceSCristian Dumitrescu 	status = rte_swx_ctl_pipeline_rss_key_write(p, rss_obj_name, rss_key_size, rss_key);
26758ba342ceSCristian Dumitrescu 	if (status) {
26768ba342ceSCristian Dumitrescu 		snprintf(out, out_size, "Command failed.\n");
26778ba342ceSCristian Dumitrescu 		return;
26788ba342ceSCristian Dumitrescu 	}
26798ba342ceSCristian Dumitrescu }
26808ba342ceSCristian Dumitrescu 
26815074e1d5SCristian Dumitrescu static const char cmd_pipeline_stats_help[] =
26825074e1d5SCristian Dumitrescu "pipeline <pipeline_name> stats\n";
26835074e1d5SCristian Dumitrescu 
26845074e1d5SCristian Dumitrescu static void
26855074e1d5SCristian Dumitrescu cmd_pipeline_stats(char **tokens,
26865074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
26875074e1d5SCristian Dumitrescu 	char *out,
26885074e1d5SCristian Dumitrescu 	size_t out_size,
2689b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
26905074e1d5SCristian Dumitrescu {
26915074e1d5SCristian Dumitrescu 	struct rte_swx_ctl_pipeline_info info;
2692b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
26935074e1d5SCristian Dumitrescu 	uint32_t i;
26945074e1d5SCristian Dumitrescu 	int status;
26955074e1d5SCristian Dumitrescu 
26965074e1d5SCristian Dumitrescu 	if (n_tokens != 3) {
26975074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
26985074e1d5SCristian Dumitrescu 		return;
26995074e1d5SCristian Dumitrescu 	}
27005074e1d5SCristian Dumitrescu 
2701b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2702b9559f94SCristian Dumitrescu 	if (!p) {
27035074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
27045074e1d5SCristian Dumitrescu 		return;
27055074e1d5SCristian Dumitrescu 	}
27065074e1d5SCristian Dumitrescu 
27075074e1d5SCristian Dumitrescu 	if (strcmp(tokens[2], "stats")) {
27085074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "stats");
27095074e1d5SCristian Dumitrescu 		return;
27105074e1d5SCristian Dumitrescu 	}
27115074e1d5SCristian Dumitrescu 
2712b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_info_get(p, &info);
27135074e1d5SCristian Dumitrescu 	if (status) {
27145074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline info get error.");
27155074e1d5SCristian Dumitrescu 		return;
27165074e1d5SCristian Dumitrescu 	}
27175074e1d5SCristian Dumitrescu 
27185074e1d5SCristian Dumitrescu 	snprintf(out, out_size, "Input ports:\n");
27195074e1d5SCristian Dumitrescu 	out_size -= strlen(out);
27205074e1d5SCristian Dumitrescu 	out += strlen(out);
27215074e1d5SCristian Dumitrescu 
27225074e1d5SCristian Dumitrescu 	for (i = 0; i < info.n_ports_in; i++) {
27235074e1d5SCristian Dumitrescu 		struct rte_swx_port_in_stats stats;
27245074e1d5SCristian Dumitrescu 
2725b9559f94SCristian Dumitrescu 		rte_swx_ctl_pipeline_port_in_stats_read(p, i, &stats);
27265074e1d5SCristian Dumitrescu 
27275074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\tPort %u:"
27285074e1d5SCristian Dumitrescu 			" packets %" PRIu64
27295074e1d5SCristian Dumitrescu 			" bytes %" PRIu64
27305074e1d5SCristian Dumitrescu 			" empty %" PRIu64 "\n",
27315074e1d5SCristian Dumitrescu 			i, stats.n_pkts, stats.n_bytes, stats.n_empty);
27325074e1d5SCristian Dumitrescu 		out_size -= strlen(out);
27335074e1d5SCristian Dumitrescu 		out += strlen(out);
27345074e1d5SCristian Dumitrescu 	}
27355074e1d5SCristian Dumitrescu 
2736742b0a57SCristian Dumitrescu 	snprintf(out, out_size, "\nOutput ports:\n");
27375074e1d5SCristian Dumitrescu 	out_size -= strlen(out);
27385074e1d5SCristian Dumitrescu 	out += strlen(out);
27395074e1d5SCristian Dumitrescu 
27405074e1d5SCristian Dumitrescu 	for (i = 0; i < info.n_ports_out; i++) {
27415074e1d5SCristian Dumitrescu 		struct rte_swx_port_out_stats stats;
27425074e1d5SCristian Dumitrescu 
2743b9559f94SCristian Dumitrescu 		rte_swx_ctl_pipeline_port_out_stats_read(p, i, &stats);
27445074e1d5SCristian Dumitrescu 
274596b37959SCristian Dumitrescu 		if (i != info.n_ports_out - 1)
274617225455SCristian Dumitrescu 			snprintf(out, out_size, "\tPort %u:", i);
274796b37959SCristian Dumitrescu 		else
274817225455SCristian Dumitrescu 			snprintf(out, out_size, "\tDROP:");
274917225455SCristian Dumitrescu 
275017225455SCristian Dumitrescu 		out_size -= strlen(out);
275117225455SCristian Dumitrescu 		out += strlen(out);
275217225455SCristian Dumitrescu 
275317225455SCristian Dumitrescu 		snprintf(out,
275417225455SCristian Dumitrescu 			out_size,
275596b37959SCristian Dumitrescu 			" packets %" PRIu64
275617225455SCristian Dumitrescu 			" bytes %" PRIu64
275767f707b3SCristian Dumitrescu 			" packets dropped %" PRIu64
275867f707b3SCristian Dumitrescu 			" bytes dropped %" PRIu64
275917225455SCristian Dumitrescu 			" clone %" PRIu64
276017225455SCristian Dumitrescu 			" clonerr %" PRIu64 "\n",
276117225455SCristian Dumitrescu 			stats.n_pkts,
276217225455SCristian Dumitrescu 			stats.n_bytes,
276367f707b3SCristian Dumitrescu 			stats.n_pkts_drop,
276467f707b3SCristian Dumitrescu 			stats.n_bytes_drop,
276517225455SCristian Dumitrescu 			stats.n_pkts_clone,
276617225455SCristian Dumitrescu 			stats.n_pkts_clone_err);
276796b37959SCristian Dumitrescu 
27685074e1d5SCristian Dumitrescu 		out_size -= strlen(out);
27695074e1d5SCristian Dumitrescu 		out += strlen(out);
27705074e1d5SCristian Dumitrescu 	}
2771742b0a57SCristian Dumitrescu 
2772742b0a57SCristian Dumitrescu 	snprintf(out, out_size, "\nTables:\n");
2773742b0a57SCristian Dumitrescu 	out_size -= strlen(out);
2774742b0a57SCristian Dumitrescu 	out += strlen(out);
2775742b0a57SCristian Dumitrescu 
2776742b0a57SCristian Dumitrescu 	for (i = 0; i < info.n_tables; i++) {
2777742b0a57SCristian Dumitrescu 		struct rte_swx_ctl_table_info table_info;
2778742b0a57SCristian Dumitrescu 		uint64_t n_pkts_action[info.n_actions];
2779742b0a57SCristian Dumitrescu 		struct rte_swx_table_stats stats = {
2780742b0a57SCristian Dumitrescu 			.n_pkts_hit = 0,
2781742b0a57SCristian Dumitrescu 			.n_pkts_miss = 0,
2782742b0a57SCristian Dumitrescu 			.n_pkts_action = n_pkts_action,
2783742b0a57SCristian Dumitrescu 		};
2784742b0a57SCristian Dumitrescu 		uint32_t j;
2785742b0a57SCristian Dumitrescu 
2786b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_table_info_get(p, i, &table_info);
2787742b0a57SCristian Dumitrescu 		if (status) {
2788742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "Table info get error.");
2789742b0a57SCristian Dumitrescu 			return;
2790742b0a57SCristian Dumitrescu 		}
2791742b0a57SCristian Dumitrescu 
2792b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_table_stats_read(p, table_info.name, &stats);
2793742b0a57SCristian Dumitrescu 		if (status) {
2794742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "Table stats read error.");
2795742b0a57SCristian Dumitrescu 			return;
2796742b0a57SCristian Dumitrescu 		}
2797742b0a57SCristian Dumitrescu 
2798742b0a57SCristian Dumitrescu 		snprintf(out, out_size, "\tTable %s:\n"
2799742b0a57SCristian Dumitrescu 			"\t\tHit (packets): %" PRIu64 "\n"
2800742b0a57SCristian Dumitrescu 			"\t\tMiss (packets): %" PRIu64 "\n",
2801742b0a57SCristian Dumitrescu 			table_info.name,
2802742b0a57SCristian Dumitrescu 			stats.n_pkts_hit,
2803742b0a57SCristian Dumitrescu 			stats.n_pkts_miss);
2804742b0a57SCristian Dumitrescu 		out_size -= strlen(out);
2805742b0a57SCristian Dumitrescu 		out += strlen(out);
2806742b0a57SCristian Dumitrescu 
2807742b0a57SCristian Dumitrescu 		for (j = 0; j < info.n_actions; j++) {
2808742b0a57SCristian Dumitrescu 			struct rte_swx_ctl_action_info action_info;
2809742b0a57SCristian Dumitrescu 
2810b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_action_info_get(p, j, &action_info);
2811742b0a57SCristian Dumitrescu 			if (status) {
2812742b0a57SCristian Dumitrescu 				snprintf(out, out_size, "Action info get error.");
2813742b0a57SCristian Dumitrescu 				return;
2814742b0a57SCristian Dumitrescu 			}
2815742b0a57SCristian Dumitrescu 
2816742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "\t\tAction %s (packets): %" PRIu64 "\n",
2817742b0a57SCristian Dumitrescu 				action_info.name,
2818742b0a57SCristian Dumitrescu 				stats.n_pkts_action[j]);
2819742b0a57SCristian Dumitrescu 			out_size -= strlen(out);
2820742b0a57SCristian Dumitrescu 			out += strlen(out);
2821742b0a57SCristian Dumitrescu 		}
2822742b0a57SCristian Dumitrescu 	}
28238bd4862fSCristian Dumitrescu 
28248bd4862fSCristian Dumitrescu 	snprintf(out, out_size, "\nLearner tables:\n");
28258bd4862fSCristian Dumitrescu 	out_size -= strlen(out);
28268bd4862fSCristian Dumitrescu 	out += strlen(out);
28278bd4862fSCristian Dumitrescu 
28288bd4862fSCristian Dumitrescu 	for (i = 0; i < info.n_learners; i++) {
28298bd4862fSCristian Dumitrescu 		struct rte_swx_ctl_learner_info learner_info;
28308bd4862fSCristian Dumitrescu 		uint64_t n_pkts_action[info.n_actions];
28318bd4862fSCristian Dumitrescu 		struct rte_swx_learner_stats stats = {
28328bd4862fSCristian Dumitrescu 			.n_pkts_hit = 0,
28338bd4862fSCristian Dumitrescu 			.n_pkts_miss = 0,
28348bd4862fSCristian Dumitrescu 			.n_pkts_action = n_pkts_action,
28358bd4862fSCristian Dumitrescu 		};
28368bd4862fSCristian Dumitrescu 		uint32_t j;
28378bd4862fSCristian Dumitrescu 
2838b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_learner_info_get(p, i, &learner_info);
28398bd4862fSCristian Dumitrescu 		if (status) {
28408bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "Learner table info get error.");
28418bd4862fSCristian Dumitrescu 			return;
28428bd4862fSCristian Dumitrescu 		}
28438bd4862fSCristian Dumitrescu 
2844b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_learner_stats_read(p, learner_info.name, &stats);
28458bd4862fSCristian Dumitrescu 		if (status) {
28468bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "Learner table stats read error.");
28478bd4862fSCristian Dumitrescu 			return;
28488bd4862fSCristian Dumitrescu 		}
28498bd4862fSCristian Dumitrescu 
28508bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "\tLearner table %s:\n"
28518bd4862fSCristian Dumitrescu 			"\t\tHit (packets): %" PRIu64 "\n"
28528bd4862fSCristian Dumitrescu 			"\t\tMiss (packets): %" PRIu64 "\n"
28538bd4862fSCristian Dumitrescu 			"\t\tLearn OK (packets): %" PRIu64 "\n"
28548bd4862fSCristian Dumitrescu 			"\t\tLearn error (packets): %" PRIu64 "\n"
285580dd28afSCristian Dumitrescu 			"\t\tRearm (packets): %" PRIu64 "\n"
28568bd4862fSCristian Dumitrescu 			"\t\tForget (packets): %" PRIu64 "\n",
28578bd4862fSCristian Dumitrescu 			learner_info.name,
28588bd4862fSCristian Dumitrescu 			stats.n_pkts_hit,
28598bd4862fSCristian Dumitrescu 			stats.n_pkts_miss,
28608bd4862fSCristian Dumitrescu 			stats.n_pkts_learn_ok,
28618bd4862fSCristian Dumitrescu 			stats.n_pkts_learn_err,
286280dd28afSCristian Dumitrescu 			stats.n_pkts_rearm,
28638bd4862fSCristian Dumitrescu 			stats.n_pkts_forget);
28648bd4862fSCristian Dumitrescu 		out_size -= strlen(out);
28658bd4862fSCristian Dumitrescu 		out += strlen(out);
28668bd4862fSCristian Dumitrescu 
28678bd4862fSCristian Dumitrescu 		for (j = 0; j < info.n_actions; j++) {
28688bd4862fSCristian Dumitrescu 			struct rte_swx_ctl_action_info action_info;
28698bd4862fSCristian Dumitrescu 
2870b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_action_info_get(p, j, &action_info);
28718bd4862fSCristian Dumitrescu 			if (status) {
28728bd4862fSCristian Dumitrescu 				snprintf(out, out_size, "Action info get error.");
28738bd4862fSCristian Dumitrescu 				return;
28748bd4862fSCristian Dumitrescu 			}
28758bd4862fSCristian Dumitrescu 
28768bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "\t\tAction %s (packets): %" PRIu64 "\n",
28778bd4862fSCristian Dumitrescu 				action_info.name,
28788bd4862fSCristian Dumitrescu 				stats.n_pkts_action[j]);
28798bd4862fSCristian Dumitrescu 			out_size -= strlen(out);
28808bd4862fSCristian Dumitrescu 			out += strlen(out);
28818bd4862fSCristian Dumitrescu 		}
28828bd4862fSCristian Dumitrescu 	}
28835074e1d5SCristian Dumitrescu }
28845074e1d5SCristian Dumitrescu 
288517225455SCristian Dumitrescu static const char cmd_pipeline_mirror_session_help[] =
288617225455SCristian Dumitrescu "pipeline <pipeline_name> mirror session <session_id> port <port_id> clone fast | slow "
288717225455SCristian Dumitrescu "truncate <truncation_length>\n";
288817225455SCristian Dumitrescu 
288917225455SCristian Dumitrescu static void
289017225455SCristian Dumitrescu cmd_pipeline_mirror_session(char **tokens,
289117225455SCristian Dumitrescu 	uint32_t n_tokens,
289217225455SCristian Dumitrescu 	char *out,
289317225455SCristian Dumitrescu 	size_t out_size,
2894b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
289517225455SCristian Dumitrescu {
289617225455SCristian Dumitrescu 	struct rte_swx_pipeline_mirroring_session_params params;
2897b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
289877dd857dSAli Alnubani 	uint32_t session_id = 0;
289917225455SCristian Dumitrescu 	int status;
290017225455SCristian Dumitrescu 
290117225455SCristian Dumitrescu 	if (n_tokens != 11) {
290217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
290317225455SCristian Dumitrescu 		return;
290417225455SCristian Dumitrescu 	}
290517225455SCristian Dumitrescu 
290617225455SCristian Dumitrescu 	if (strcmp(tokens[0], "pipeline")) {
290717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipeline");
290817225455SCristian Dumitrescu 		return;
290917225455SCristian Dumitrescu 	}
291017225455SCristian Dumitrescu 
2911b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2912b9559f94SCristian Dumitrescu 	if (!p) {
291317225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
291417225455SCristian Dumitrescu 		return;
291517225455SCristian Dumitrescu 	}
291617225455SCristian Dumitrescu 
291717225455SCristian Dumitrescu 	if (strcmp(tokens[2], "mirror")) {
291817225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mirror");
291917225455SCristian Dumitrescu 		return;
292017225455SCristian Dumitrescu 	}
292117225455SCristian Dumitrescu 
292217225455SCristian Dumitrescu 	if (strcmp(tokens[3], "session")) {
292317225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "session");
292417225455SCristian Dumitrescu 		return;
292517225455SCristian Dumitrescu 	}
292617225455SCristian Dumitrescu 
292717225455SCristian Dumitrescu 	if (parser_read_uint32(&session_id, tokens[4])) {
292817225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "session_id");
292917225455SCristian Dumitrescu 		return;
293017225455SCristian Dumitrescu 	}
293117225455SCristian Dumitrescu 
293217225455SCristian Dumitrescu 	if (strcmp(tokens[5], "port")) {
293317225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "port");
293417225455SCristian Dumitrescu 		return;
293517225455SCristian Dumitrescu 	}
293617225455SCristian Dumitrescu 
293717225455SCristian Dumitrescu 	if (parser_read_uint32(&params.port_id, tokens[6])) {
293817225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
293917225455SCristian Dumitrescu 		return;
294017225455SCristian Dumitrescu 	}
294117225455SCristian Dumitrescu 
294217225455SCristian Dumitrescu 	if (strcmp(tokens[7], "clone")) {
294317225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "clone");
294417225455SCristian Dumitrescu 		return;
294517225455SCristian Dumitrescu 	}
294617225455SCristian Dumitrescu 
294717225455SCristian Dumitrescu 	if (!strcmp(tokens[8], "fast"))
294817225455SCristian Dumitrescu 		params.fast_clone = 1;
294917225455SCristian Dumitrescu 	else if (!strcmp(tokens[8], "slow"))
295017225455SCristian Dumitrescu 		params.fast_clone = 0;
295117225455SCristian Dumitrescu 	else {
295217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "clone");
295317225455SCristian Dumitrescu 		return;
295417225455SCristian Dumitrescu 	}
295517225455SCristian Dumitrescu 
295617225455SCristian Dumitrescu 	if (strcmp(tokens[9], "truncate")) {
295717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "truncate");
295817225455SCristian Dumitrescu 		return;
295917225455SCristian Dumitrescu 	}
296017225455SCristian Dumitrescu 
296117225455SCristian Dumitrescu 	if (parser_read_uint32(&params.truncation_length, tokens[10])) {
296217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "truncation_length");
296317225455SCristian Dumitrescu 		return;
296417225455SCristian Dumitrescu 	}
296517225455SCristian Dumitrescu 
2966b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_mirroring_session_set(p, session_id, &params);
296717225455SCristian Dumitrescu 	if (status) {
296817225455SCristian Dumitrescu 		snprintf(out, out_size, "Command failed!\n");
296917225455SCristian Dumitrescu 		return;
297017225455SCristian Dumitrescu 	}
297117225455SCristian Dumitrescu }
297217225455SCristian Dumitrescu 
29733b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_create_help[] =
29743b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> create "
29753b0cc5fbSCristian Dumitrescu "in <ring_in_name> out <ring_out_name> "
29763b0cc5fbSCristian Dumitrescu "cryptodev <crypto_dev_name> cryptoq <crypto_dev_queue_pair_id> "
29773b0cc5fbSCristian Dumitrescu "bsz <ring_rd_bsz> <ring_wr_bsz> <crypto_wr_bsz> <crypto_rd_bsz> "
29783b0cc5fbSCristian Dumitrescu "samax <n_sa_max> "
29793b0cc5fbSCristian Dumitrescu "numa <numa_node>\n";
29803b0cc5fbSCristian Dumitrescu 
29813b0cc5fbSCristian Dumitrescu static void
29823b0cc5fbSCristian Dumitrescu cmd_ipsec_create(char **tokens,
29833b0cc5fbSCristian Dumitrescu 		 uint32_t n_tokens,
29843b0cc5fbSCristian Dumitrescu 		 char *out,
29853b0cc5fbSCristian Dumitrescu 		 size_t out_size,
29863b0cc5fbSCristian Dumitrescu 		 void *obj __rte_unused)
29873b0cc5fbSCristian Dumitrescu {
29883b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec_params p;
29893b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
29903b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name;
29913b0cc5fbSCristian Dumitrescu 	uint32_t numa_node;
29923b0cc5fbSCristian Dumitrescu 	int status;
29933b0cc5fbSCristian Dumitrescu 
29943b0cc5fbSCristian Dumitrescu 	if (n_tokens != 20) {
29953b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
29963b0cc5fbSCristian Dumitrescu 		return;
29973b0cc5fbSCristian Dumitrescu 	}
29983b0cc5fbSCristian Dumitrescu 
29993b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
30003b0cc5fbSCristian Dumitrescu 
30013b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "create")) {
30023b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "create");
30033b0cc5fbSCristian Dumitrescu 		return;
30043b0cc5fbSCristian Dumitrescu 	}
30053b0cc5fbSCristian Dumitrescu 
30063b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "in")) {
30073b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "in");
30083b0cc5fbSCristian Dumitrescu 		return;
30093b0cc5fbSCristian Dumitrescu 	}
30103b0cc5fbSCristian Dumitrescu 
30113b0cc5fbSCristian Dumitrescu 	p.ring_in_name = tokens[4];
30123b0cc5fbSCristian Dumitrescu 
30133b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[5], "out")) {
30143b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "out");
30153b0cc5fbSCristian Dumitrescu 		return;
30163b0cc5fbSCristian Dumitrescu 	}
30173b0cc5fbSCristian Dumitrescu 
30183b0cc5fbSCristian Dumitrescu 	p.ring_out_name = tokens[6];
30193b0cc5fbSCristian Dumitrescu 
30203b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[7], "cryptodev")) {
30213b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptodev");
30223b0cc5fbSCristian Dumitrescu 		return;
30233b0cc5fbSCristian Dumitrescu 	}
30243b0cc5fbSCristian Dumitrescu 
30253b0cc5fbSCristian Dumitrescu 	p.crypto_dev_name = tokens[8];
30263b0cc5fbSCristian Dumitrescu 
30273b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[9], "cryptoq")) {
30283b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptoq");
30293b0cc5fbSCristian Dumitrescu 		return;
30303b0cc5fbSCristian Dumitrescu 	}
30313b0cc5fbSCristian Dumitrescu 
30323b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.crypto_dev_queue_pair_id, tokens[10])) {
30333b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_dev_queue_pair_id");
30343b0cc5fbSCristian Dumitrescu 		return;
30353b0cc5fbSCristian Dumitrescu 	}
30363b0cc5fbSCristian Dumitrescu 
30373b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[11], "bsz")) {
30383b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "bsz");
30393b0cc5fbSCristian Dumitrescu 		return;
30403b0cc5fbSCristian Dumitrescu 	}
30413b0cc5fbSCristian Dumitrescu 
30423b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.ring_rd, tokens[12])) {
30433b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ring_rd_bsz");
30443b0cc5fbSCristian Dumitrescu 		return;
30453b0cc5fbSCristian Dumitrescu 	}
30463b0cc5fbSCristian Dumitrescu 
30473b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.ring_wr, tokens[13])) {
30483b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ring_wr_bsz");
30493b0cc5fbSCristian Dumitrescu 		return;
30503b0cc5fbSCristian Dumitrescu 	}
30513b0cc5fbSCristian Dumitrescu 
30523b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.crypto_wr, tokens[14])) {
30533b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_wr_bsz");
30543b0cc5fbSCristian Dumitrescu 		return;
30553b0cc5fbSCristian Dumitrescu 	}
30563b0cc5fbSCristian Dumitrescu 
30573b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.crypto_rd, tokens[15])) {
30583b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_rd_bsz");
30593b0cc5fbSCristian Dumitrescu 		return;
30603b0cc5fbSCristian Dumitrescu 	}
30613b0cc5fbSCristian Dumitrescu 
30623b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[16], "samax")) {
30633b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "samax");
30643b0cc5fbSCristian Dumitrescu 		return;
30653b0cc5fbSCristian Dumitrescu 	}
30663b0cc5fbSCristian Dumitrescu 
30673b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.n_sa_max, tokens[17])) {
30683b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_sa_max");
30693b0cc5fbSCristian Dumitrescu 		return;
30703b0cc5fbSCristian Dumitrescu 	}
30713b0cc5fbSCristian Dumitrescu 
30723b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[18], "numa")) {
30733b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
30743b0cc5fbSCristian Dumitrescu 		return;
30753b0cc5fbSCristian Dumitrescu 	}
30763b0cc5fbSCristian Dumitrescu 
30773b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[19])) {
30783b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
30793b0cc5fbSCristian Dumitrescu 		return;
30803b0cc5fbSCristian Dumitrescu 	}
30813b0cc5fbSCristian Dumitrescu 
30823b0cc5fbSCristian Dumitrescu 	status = rte_swx_ipsec_create(&ipsec,
30833b0cc5fbSCristian Dumitrescu 				      ipsec_instance_name,
30843b0cc5fbSCristian Dumitrescu 				      &p,
30853b0cc5fbSCristian Dumitrescu 				      (int)numa_node);
30863b0cc5fbSCristian Dumitrescu 	if (status)
30873b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "IPsec instance creation failed (%d).\n", status);
30883b0cc5fbSCristian Dumitrescu }
30893b0cc5fbSCristian Dumitrescu 
30903b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_sa_add_help[] =
30913b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> sa add <file_name>\n";
30923b0cc5fbSCristian Dumitrescu 
30933b0cc5fbSCristian Dumitrescu static void
30943b0cc5fbSCristian Dumitrescu cmd_ipsec_sa_add(char **tokens,
30953b0cc5fbSCristian Dumitrescu 		 uint32_t n_tokens,
30963b0cc5fbSCristian Dumitrescu 		 char *out,
30973b0cc5fbSCristian Dumitrescu 		 size_t out_size,
30983b0cc5fbSCristian Dumitrescu 		 void *obj __rte_unused)
30993b0cc5fbSCristian Dumitrescu {
31003b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
31013b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name, *file_name, *line = NULL;
31023b0cc5fbSCristian Dumitrescu 	FILE *file = NULL;
31033b0cc5fbSCristian Dumitrescu 	uint32_t line_id = 0;
31043b0cc5fbSCristian Dumitrescu 
31053b0cc5fbSCristian Dumitrescu 	if (n_tokens != 5) {
31063b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
31073b0cc5fbSCristian Dumitrescu 		return;
31083b0cc5fbSCristian Dumitrescu 	}
31093b0cc5fbSCristian Dumitrescu 
31103b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
31113b0cc5fbSCristian Dumitrescu 	ipsec = rte_swx_ipsec_find(ipsec_instance_name);
31123b0cc5fbSCristian Dumitrescu 	if (!ipsec) {
31133b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ipsec_instance_name");
31143b0cc5fbSCristian Dumitrescu 		goto free;
31153b0cc5fbSCristian Dumitrescu 	}
31163b0cc5fbSCristian Dumitrescu 
31173b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "sa")) {
31183b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "sa");
31193b0cc5fbSCristian Dumitrescu 		goto free;
31203b0cc5fbSCristian Dumitrescu 	}
31213b0cc5fbSCristian Dumitrescu 
31223b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "add")) {
31233b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "add");
31243b0cc5fbSCristian Dumitrescu 		goto free;
31253b0cc5fbSCristian Dumitrescu 	}
31263b0cc5fbSCristian Dumitrescu 
31273b0cc5fbSCristian Dumitrescu 	file_name = tokens[4];
31283b0cc5fbSCristian Dumitrescu 	file = fopen(file_name, "r");
31293b0cc5fbSCristian Dumitrescu 	if (!file) {
31303b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
31313b0cc5fbSCristian Dumitrescu 		goto free;
31323b0cc5fbSCristian Dumitrescu 	}
31333b0cc5fbSCristian Dumitrescu 
31343b0cc5fbSCristian Dumitrescu 	/* Buffer allocation. */
31353b0cc5fbSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
31363b0cc5fbSCristian Dumitrescu 	if (!line) {
31373b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
31383b0cc5fbSCristian Dumitrescu 		goto free;
31393b0cc5fbSCristian Dumitrescu 	}
31403b0cc5fbSCristian Dumitrescu 
31413b0cc5fbSCristian Dumitrescu 	/* File read. */
31423b0cc5fbSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
31433b0cc5fbSCristian Dumitrescu 		struct rte_swx_ipsec_sa_params *sa;
31443b0cc5fbSCristian Dumitrescu 		const char *err_msg;
31453b0cc5fbSCristian Dumitrescu 		uint32_t sa_id = 0;
31463b0cc5fbSCristian Dumitrescu 		int is_blank_or_comment, status = 0;
31473b0cc5fbSCristian Dumitrescu 
31483b0cc5fbSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
31493b0cc5fbSCristian Dumitrescu 			break;
31503b0cc5fbSCristian Dumitrescu 
31513b0cc5fbSCristian Dumitrescu 		/* Read SA from file. */
31523b0cc5fbSCristian Dumitrescu 		sa = rte_swx_ipsec_sa_read(ipsec, line, &is_blank_or_comment, &err_msg);
31533b0cc5fbSCristian Dumitrescu 		if (!sa) {
31543b0cc5fbSCristian Dumitrescu 			if (is_blank_or_comment)
31553b0cc5fbSCristian Dumitrescu 				continue;
31563b0cc5fbSCristian Dumitrescu 
31573b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "Invalid SA in file \"%s\" at line %u: \"%s\"\n",
31583b0cc5fbSCristian Dumitrescu 				file_name, line_id, err_msg);
31593b0cc5fbSCristian Dumitrescu 			goto free;
31603b0cc5fbSCristian Dumitrescu 		}
31613b0cc5fbSCristian Dumitrescu 
31623b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "%s", line);
31633b0cc5fbSCristian Dumitrescu 		out_size -= strlen(out);
31643b0cc5fbSCristian Dumitrescu 		out += strlen(out);
31653b0cc5fbSCristian Dumitrescu 
31663b0cc5fbSCristian Dumitrescu 		/* Add the SA to the IPsec instance. Free the SA. */
31673b0cc5fbSCristian Dumitrescu 		status = rte_swx_ipsec_sa_add(ipsec, sa, &sa_id);
31683b0cc5fbSCristian Dumitrescu 		if (status)
31693b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "\t: Error (%d)\n", status);
31703b0cc5fbSCristian Dumitrescu 		else
31713b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "\t: OK (SA ID = %u)\n", sa_id);
31723b0cc5fbSCristian Dumitrescu 		out_size -= strlen(out);
31733b0cc5fbSCristian Dumitrescu 		out += strlen(out);
31743b0cc5fbSCristian Dumitrescu 
31753b0cc5fbSCristian Dumitrescu 		free(sa);
31763b0cc5fbSCristian Dumitrescu 		if (status)
31773b0cc5fbSCristian Dumitrescu 			goto free;
31783b0cc5fbSCristian Dumitrescu 	}
31793b0cc5fbSCristian Dumitrescu 
31803b0cc5fbSCristian Dumitrescu free:
31813b0cc5fbSCristian Dumitrescu 	if (file)
31823b0cc5fbSCristian Dumitrescu 		fclose(file);
31833b0cc5fbSCristian Dumitrescu 	free(line);
31843b0cc5fbSCristian Dumitrescu }
31853b0cc5fbSCristian Dumitrescu 
31863b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_sa_delete_help[] =
31873b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> sa delete <sa_id>\n";
31883b0cc5fbSCristian Dumitrescu 
31893b0cc5fbSCristian Dumitrescu static void
31903b0cc5fbSCristian Dumitrescu cmd_ipsec_sa_delete(char **tokens,
31913b0cc5fbSCristian Dumitrescu 		    uint32_t n_tokens,
31923b0cc5fbSCristian Dumitrescu 		    char *out,
31933b0cc5fbSCristian Dumitrescu 		    size_t out_size,
31943b0cc5fbSCristian Dumitrescu 		    void *obj __rte_unused)
31953b0cc5fbSCristian Dumitrescu {
31963b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
31973b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name;
31983b0cc5fbSCristian Dumitrescu 	uint32_t sa_id;
31993b0cc5fbSCristian Dumitrescu 
32003b0cc5fbSCristian Dumitrescu 	if (n_tokens != 5) {
32013b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
32023b0cc5fbSCristian Dumitrescu 		return;
32033b0cc5fbSCristian Dumitrescu 	}
32043b0cc5fbSCristian Dumitrescu 
32053b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
32063b0cc5fbSCristian Dumitrescu 	ipsec = rte_swx_ipsec_find(ipsec_instance_name);
32073b0cc5fbSCristian Dumitrescu 	if (!ipsec) {
32083b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ipsec_instance_name");
32093b0cc5fbSCristian Dumitrescu 		return;
32103b0cc5fbSCristian Dumitrescu 	}
32113b0cc5fbSCristian Dumitrescu 
32123b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "sa")) {
32133b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "sa");
32143b0cc5fbSCristian Dumitrescu 		return;
32153b0cc5fbSCristian Dumitrescu 	}
32163b0cc5fbSCristian Dumitrescu 
32173b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "delete")) {
32183b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "delete");
32193b0cc5fbSCristian Dumitrescu 		return;
32203b0cc5fbSCristian Dumitrescu 	}
32213b0cc5fbSCristian Dumitrescu 
32223b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&sa_id, tokens[4])) {
32233b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "sa_id");
32243b0cc5fbSCristian Dumitrescu 		return;
32253b0cc5fbSCristian Dumitrescu 	}
32263b0cc5fbSCristian Dumitrescu 
32273b0cc5fbSCristian Dumitrescu 	rte_swx_ipsec_sa_delete(ipsec, sa_id);
32283b0cc5fbSCristian Dumitrescu }
32293b0cc5fbSCristian Dumitrescu 
323041f5dfcbSCristian Dumitrescu static const char cmd_pipeline_enable_help[] =
323141f5dfcbSCristian Dumitrescu "pipeline <pipeline_name> enable thread <thread_id>\n";
32325074e1d5SCristian Dumitrescu 
32335074e1d5SCristian Dumitrescu static void
323441f5dfcbSCristian Dumitrescu cmd_pipeline_enable(char **tokens,
32355074e1d5SCristian Dumitrescu 		    uint32_t n_tokens,
32365074e1d5SCristian Dumitrescu 		    char *out,
32375074e1d5SCristian Dumitrescu 		    size_t out_size,
3238b9559f94SCristian Dumitrescu 		    void *obj __rte_unused)
32395074e1d5SCristian Dumitrescu {
32405074e1d5SCristian Dumitrescu 	char *pipeline_name;
3241b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
32425074e1d5SCristian Dumitrescu 	uint32_t thread_id;
32435074e1d5SCristian Dumitrescu 	int status;
32445074e1d5SCristian Dumitrescu 
32455074e1d5SCristian Dumitrescu 	if (n_tokens != 5) {
32465074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
32475074e1d5SCristian Dumitrescu 		return;
32485074e1d5SCristian Dumitrescu 	}
32495074e1d5SCristian Dumitrescu 
325041f5dfcbSCristian Dumitrescu 	pipeline_name = tokens[1];
3251b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
3252b9559f94SCristian Dumitrescu 	if (!p) {
32535074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
32545074e1d5SCristian Dumitrescu 		return;
32555074e1d5SCristian Dumitrescu 	}
32565074e1d5SCristian Dumitrescu 
325741f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[2], "enable") != 0) {
325841f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "enable");
325941f5dfcbSCristian Dumitrescu 		return;
326041f5dfcbSCristian Dumitrescu 	}
326141f5dfcbSCristian Dumitrescu 
326241f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[3], "thread") != 0) {
326341f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "thread");
326441f5dfcbSCristian Dumitrescu 		return;
326541f5dfcbSCristian Dumitrescu 	}
326641f5dfcbSCristian Dumitrescu 
326741f5dfcbSCristian Dumitrescu 	if (parser_read_uint32(&thread_id, tokens[4]) != 0) {
326841f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "thread_id");
326941f5dfcbSCristian Dumitrescu 		return;
327041f5dfcbSCristian Dumitrescu 	}
327141f5dfcbSCristian Dumitrescu 
327241f5dfcbSCristian Dumitrescu 	status = pipeline_enable(p, thread_id);
327341f5dfcbSCristian Dumitrescu 	if (status) {
327441f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, "pipeline enable");
327541f5dfcbSCristian Dumitrescu 		return;
327641f5dfcbSCristian Dumitrescu 	}
327741f5dfcbSCristian Dumitrescu }
327841f5dfcbSCristian Dumitrescu 
327941f5dfcbSCristian Dumitrescu static const char cmd_pipeline_disable_help[] =
328041f5dfcbSCristian Dumitrescu "pipeline <pipeline_name> disable\n";
328141f5dfcbSCristian Dumitrescu 
328241f5dfcbSCristian Dumitrescu static void
328341f5dfcbSCristian Dumitrescu cmd_pipeline_disable(char **tokens,
328441f5dfcbSCristian Dumitrescu 		     uint32_t n_tokens,
328541f5dfcbSCristian Dumitrescu 		     char *out,
328641f5dfcbSCristian Dumitrescu 		     size_t out_size,
328741f5dfcbSCristian Dumitrescu 		     void *obj __rte_unused)
328841f5dfcbSCristian Dumitrescu {
328941f5dfcbSCristian Dumitrescu 	struct rte_swx_pipeline *p;
329041f5dfcbSCristian Dumitrescu 	char *pipeline_name;
329141f5dfcbSCristian Dumitrescu 
329241f5dfcbSCristian Dumitrescu 	if (n_tokens != 3) {
329341f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
329441f5dfcbSCristian Dumitrescu 		return;
329541f5dfcbSCristian Dumitrescu 	}
329641f5dfcbSCristian Dumitrescu 
329741f5dfcbSCristian Dumitrescu 	pipeline_name = tokens[1];
329841f5dfcbSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
329941f5dfcbSCristian Dumitrescu 	if (!p) {
330041f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
330141f5dfcbSCristian Dumitrescu 		return;
330241f5dfcbSCristian Dumitrescu 	}
330341f5dfcbSCristian Dumitrescu 
330441f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[2], "disable") != 0) {
33055074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "disable");
33065074e1d5SCristian Dumitrescu 		return;
33075074e1d5SCristian Dumitrescu 	}
33085074e1d5SCristian Dumitrescu 
330941f5dfcbSCristian Dumitrescu 	pipeline_disable(p);
33105074e1d5SCristian Dumitrescu }
33115074e1d5SCristian Dumitrescu 
33126d99096cSCristian Dumitrescu static const char cmd_block_enable_help[] =
33136d99096cSCristian Dumitrescu "block type <block_type> instance <block_name> enable thread <thread_id>\n";
33146d99096cSCristian Dumitrescu 
33156d99096cSCristian Dumitrescu static void
33166d99096cSCristian Dumitrescu cmd_block_enable(char **tokens,
33176d99096cSCristian Dumitrescu 		 uint32_t n_tokens,
33186d99096cSCristian Dumitrescu 		 char *out,
33196d99096cSCristian Dumitrescu 		 size_t out_size,
33206d99096cSCristian Dumitrescu 		 void *obj __rte_unused)
33216d99096cSCristian Dumitrescu {
33226d99096cSCristian Dumitrescu 	char *block_type, *block_name;
33236d99096cSCristian Dumitrescu 	block_run_f block_func = NULL;
33246d99096cSCristian Dumitrescu 	void *block = NULL;
33256d99096cSCristian Dumitrescu 	uint32_t thread_id;
33266d99096cSCristian Dumitrescu 	int status;
33276d99096cSCristian Dumitrescu 
33286d99096cSCristian Dumitrescu 	if (n_tokens != 8) {
33296d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
33306d99096cSCristian Dumitrescu 		return;
33316d99096cSCristian Dumitrescu 	}
33326d99096cSCristian Dumitrescu 
33336d99096cSCristian Dumitrescu 	if (strcmp(tokens[1], "type") != 0) {
33346d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "type");
33356d99096cSCristian Dumitrescu 		return;
33366d99096cSCristian Dumitrescu 	}
33376d99096cSCristian Dumitrescu 
33386d99096cSCristian Dumitrescu 	block_type = tokens[2];
33396d99096cSCristian Dumitrescu 
33406d99096cSCristian Dumitrescu 	if (strcmp(tokens[3], "instance") != 0) {
33416d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "instance");
33426d99096cSCristian Dumitrescu 		return;
33436d99096cSCristian Dumitrescu 	}
33446d99096cSCristian Dumitrescu 
33456d99096cSCristian Dumitrescu 	block_name = tokens[4];
33466d99096cSCristian Dumitrescu 
33476d99096cSCristian Dumitrescu 	if (strcmp(tokens[5], "enable") != 0) {
33486d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "enable");
33496d99096cSCristian Dumitrescu 		return;
33506d99096cSCristian Dumitrescu 	}
33516d99096cSCristian Dumitrescu 
33526d99096cSCristian Dumitrescu 	if (strcmp(tokens[6], "thread") != 0) {
33536d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "thread");
33546d99096cSCristian Dumitrescu 		return;
33556d99096cSCristian Dumitrescu 	}
33566d99096cSCristian Dumitrescu 
33576d99096cSCristian Dumitrescu 	if (parser_read_uint32(&thread_id, tokens[7]) != 0) {
33586d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "thread_id");
33596d99096cSCristian Dumitrescu 		return;
33606d99096cSCristian Dumitrescu 	}
33616d99096cSCristian Dumitrescu 
33626d99096cSCristian Dumitrescu 	if (!strcmp(block_type, "ipsec")) {
33636d99096cSCristian Dumitrescu 		struct rte_swx_ipsec *ipsec;
33646d99096cSCristian Dumitrescu 
33656d99096cSCristian Dumitrescu 		ipsec = rte_swx_ipsec_find(block_name);
33666d99096cSCristian Dumitrescu 		if (!ipsec) {
33676d99096cSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "block_name");
33686d99096cSCristian Dumitrescu 			return;
33696d99096cSCristian Dumitrescu 		}
33706d99096cSCristian Dumitrescu 
33716d99096cSCristian Dumitrescu 		block_func = (block_run_f)rte_swx_ipsec_run;
33726d99096cSCristian Dumitrescu 		block = (void *)ipsec;
33736d99096cSCristian Dumitrescu 	} else {
33746d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "block_type");
33756d99096cSCristian Dumitrescu 		return;
33766d99096cSCristian Dumitrescu 	}
33776d99096cSCristian Dumitrescu 
33786d99096cSCristian Dumitrescu 	status = block_enable(block_func, block, thread_id);
33796d99096cSCristian Dumitrescu 	if (status) {
33806d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, "block enable");
33816d99096cSCristian Dumitrescu 		return;
33826d99096cSCristian Dumitrescu 	}
33836d99096cSCristian Dumitrescu }
33846d99096cSCristian Dumitrescu 
33856d99096cSCristian Dumitrescu static const char cmd_block_disable_help[] =
33866d99096cSCristian Dumitrescu "block type <block_type> instance <block_name> disable\n";
33876d99096cSCristian Dumitrescu 
33886d99096cSCristian Dumitrescu static void
33896d99096cSCristian Dumitrescu cmd_block_disable(char **tokens,
33906d99096cSCristian Dumitrescu 		  uint32_t n_tokens,
33916d99096cSCristian Dumitrescu 		  char *out,
33926d99096cSCristian Dumitrescu 		  size_t out_size,
33936d99096cSCristian Dumitrescu 		  void *obj __rte_unused)
33946d99096cSCristian Dumitrescu {
33956d99096cSCristian Dumitrescu 	char *block_type, *block_name;
33966d99096cSCristian Dumitrescu 	void *block = NULL;
33976d99096cSCristian Dumitrescu 
33986d99096cSCristian Dumitrescu 	if (n_tokens != 6) {
33996d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
34006d99096cSCristian Dumitrescu 		return;
34016d99096cSCristian Dumitrescu 	}
34026d99096cSCristian Dumitrescu 
34036d99096cSCristian Dumitrescu 	if (strcmp(tokens[1], "type") != 0) {
34046d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "type");
34056d99096cSCristian Dumitrescu 		return;
34066d99096cSCristian Dumitrescu 	}
34076d99096cSCristian Dumitrescu 
34086d99096cSCristian Dumitrescu 	block_type = tokens[2];
34096d99096cSCristian Dumitrescu 
34106d99096cSCristian Dumitrescu 	if (strcmp(tokens[3], "instance") != 0) {
34116d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "instance");
34126d99096cSCristian Dumitrescu 		return;
34136d99096cSCristian Dumitrescu 	}
34146d99096cSCristian Dumitrescu 
34156d99096cSCristian Dumitrescu 	block_name = tokens[4];
34166d99096cSCristian Dumitrescu 
34176d99096cSCristian Dumitrescu 	if (strcmp(tokens[5], "disable") != 0) {
34186d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "disable");
34196d99096cSCristian Dumitrescu 		return;
34206d99096cSCristian Dumitrescu 	}
34216d99096cSCristian Dumitrescu 
34226d99096cSCristian Dumitrescu 	if (!strcmp(block_type, "ipsec")) {
34236d99096cSCristian Dumitrescu 		struct rte_swx_ipsec *ipsec;
34246d99096cSCristian Dumitrescu 
34256d99096cSCristian Dumitrescu 		ipsec = rte_swx_ipsec_find(block_name);
34266d99096cSCristian Dumitrescu 		if (!ipsec) {
34276d99096cSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "block_name");
34286d99096cSCristian Dumitrescu 			return;
34296d99096cSCristian Dumitrescu 		}
34306d99096cSCristian Dumitrescu 
34316d99096cSCristian Dumitrescu 		block = (void *)ipsec;
34326d99096cSCristian Dumitrescu 	} else {
34336d99096cSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "block_type");
34346d99096cSCristian Dumitrescu 		return;
34356d99096cSCristian Dumitrescu 	}
34366d99096cSCristian Dumitrescu 
34376d99096cSCristian Dumitrescu 	block_disable(block);
34386d99096cSCristian Dumitrescu }
34396d99096cSCristian Dumitrescu 
34405074e1d5SCristian Dumitrescu static void
34415074e1d5SCristian Dumitrescu cmd_help(char **tokens,
34425074e1d5SCristian Dumitrescu 	 uint32_t n_tokens,
34435074e1d5SCristian Dumitrescu 	 char *out,
34445074e1d5SCristian Dumitrescu 	 size_t out_size,
34455074e1d5SCristian Dumitrescu 	 void *arg __rte_unused)
34465074e1d5SCristian Dumitrescu {
34475074e1d5SCristian Dumitrescu 	tokens++;
34485074e1d5SCristian Dumitrescu 	n_tokens--;
34495074e1d5SCristian Dumitrescu 
34505074e1d5SCristian Dumitrescu 	if (n_tokens == 0) {
34515074e1d5SCristian Dumitrescu 		snprintf(out, out_size,
34527fef9ef1SYogesh Jangra 			"Type 'help <command>' for command details.\n\n"
34537fef9ef1SYogesh Jangra 			"List of commands:\n"
34547fef9ef1SYogesh Jangra 			"\tmempool\n"
3455f31c80f8SCristian Dumitrescu 			"\tethdev\n"
345678dffe31SCristian Dumitrescu 			"\tethdev show\n"
3457607dd517SCristian Dumitrescu 			"\tring\n"
34581b41a527SCristian Dumitrescu 			"\tcryptodev\n"
34599043f66aSCristian Dumitrescu 			"\tpipeline codegen\n"
34606bc14d9fSCristian Dumitrescu 			"\tpipeline libbuild\n"
34617fef9ef1SYogesh Jangra 			"\tpipeline build\n"
346275129cebSChurchill Khangar 			"\tpipeline table add\n"
346375129cebSChurchill Khangar 			"\tpipeline table delete\n"
346475129cebSChurchill Khangar 			"\tpipeline table default\n"
346575129cebSChurchill Khangar 			"\tpipeline table show\n"
3466598fe0ddSCristian Dumitrescu 			"\tpipeline selector group add\n"
3467598fe0ddSCristian Dumitrescu 			"\tpipeline selector group delete\n"
3468598fe0ddSCristian Dumitrescu 			"\tpipeline selector group member add\n"
3469598fe0ddSCristian Dumitrescu 			"\tpipeline selector group member delete\n"
3470598fe0ddSCristian Dumitrescu 			"\tpipeline selector show\n"
34718bd4862fSCristian Dumitrescu 			"\tpipeline learner default\n"
347275129cebSChurchill Khangar 			"\tpipeline commit\n"
347375129cebSChurchill Khangar 			"\tpipeline abort\n"
347464cfcebdSCristian Dumitrescu 			"\tpipeline regrd\n"
347564cfcebdSCristian Dumitrescu 			"\tpipeline regwr\n"
3476f38913b7SCristian Dumitrescu 			"\tpipeline meter profile add\n"
3477f38913b7SCristian Dumitrescu 			"\tpipeline meter profile delete\n"
3478f38913b7SCristian Dumitrescu 			"\tpipeline meter reset\n"
3479f38913b7SCristian Dumitrescu 			"\tpipeline meter set\n"
3480f38913b7SCristian Dumitrescu 			"\tpipeline meter stats\n"
34818ba342ceSCristian Dumitrescu 			"\tpipeline rss\n"
34827fef9ef1SYogesh Jangra 			"\tpipeline stats\n"
348317225455SCristian Dumitrescu 			"\tpipeline mirror session\n"
348441f5dfcbSCristian Dumitrescu 			"\tpipeline enable\n"
348541f5dfcbSCristian Dumitrescu 			"\tpipeline disable\n\n"
34863b0cc5fbSCristian Dumitrescu 			"\tipsec create\n"
34873b0cc5fbSCristian Dumitrescu 			"\tipsec sa add\n"
34883b0cc5fbSCristian Dumitrescu 			"\tipsec sa delete\n"
34896d99096cSCristian Dumitrescu 			"\tblock enable\n"
34906d99096cSCristian Dumitrescu 			"\tblock disable\n"
349141f5dfcbSCristian Dumitrescu 			);
34925074e1d5SCristian Dumitrescu 		return;
34935074e1d5SCristian Dumitrescu 	}
34945074e1d5SCristian Dumitrescu 
34955074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "mempool") == 0) {
34965074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_mempool_help);
34975074e1d5SCristian Dumitrescu 		return;
34985074e1d5SCristian Dumitrescu 	}
34995074e1d5SCristian Dumitrescu 
350078dffe31SCristian Dumitrescu 	if (!strcmp(tokens[0], "ethdev")) {
350178dffe31SCristian Dumitrescu 		if (n_tokens == 1) {
3502f31c80f8SCristian Dumitrescu 			snprintf(out, out_size, "\n%s\n", cmd_ethdev_help);
35035074e1d5SCristian Dumitrescu 			return;
35045074e1d5SCristian Dumitrescu 		}
35055074e1d5SCristian Dumitrescu 
350678dffe31SCristian Dumitrescu 		if (n_tokens == 2 && !strcmp(tokens[1], "show")) {
350778dffe31SCristian Dumitrescu 			snprintf(out, out_size, "\n%s\n", cmd_ethdev_show_help);
350878dffe31SCristian Dumitrescu 			return;
350978dffe31SCristian Dumitrescu 		}
351078dffe31SCristian Dumitrescu 	}
351178dffe31SCristian Dumitrescu 
351277a41301SCristian Dumitrescu 	if (strcmp(tokens[0], "ring") == 0) {
351377a41301SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ring_help);
351477a41301SCristian Dumitrescu 		return;
351577a41301SCristian Dumitrescu 	}
351677a41301SCristian Dumitrescu 
35171b41a527SCristian Dumitrescu 	if (!strcmp(tokens[0], "cryptodev")) {
35181b41a527SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_cryptodev_help);
35191b41a527SCristian Dumitrescu 		return;
35201b41a527SCristian Dumitrescu 	}
35211b41a527SCristian Dumitrescu 
35225074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
35239043f66aSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "codegen") == 0)) {
35249043f66aSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_codegen_help);
35259043f66aSCristian Dumitrescu 		return;
35269043f66aSCristian Dumitrescu 	}
35279043f66aSCristian Dumitrescu 
35289043f66aSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
35296bc14d9fSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "libbuild") == 0)) {
35306bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_libbuild_help);
35316bc14d9fSCristian Dumitrescu 		return;
35326bc14d9fSCristian Dumitrescu 	}
35336bc14d9fSCristian Dumitrescu 
35346bc14d9fSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
35357fef9ef1SYogesh Jangra 		(n_tokens == 2) && (strcmp(tokens[1], "build") == 0)) {
35365074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_build_help);
35375074e1d5SCristian Dumitrescu 		return;
35385074e1d5SCristian Dumitrescu 	}
35395074e1d5SCristian Dumitrescu 
35405074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
35417fef9ef1SYogesh Jangra 		(n_tokens == 3) &&
35427fef9ef1SYogesh Jangra 		(strcmp(tokens[1], "table") == 0) &&
354375129cebSChurchill Khangar 		(strcmp(tokens[2], "add") == 0)) {
35445074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
354575129cebSChurchill Khangar 			cmd_pipeline_table_add_help);
354675129cebSChurchill Khangar 		return;
354775129cebSChurchill Khangar 	}
354875129cebSChurchill Khangar 
354975129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
355075129cebSChurchill Khangar 		(n_tokens == 3) &&
355175129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
355275129cebSChurchill Khangar 		(strcmp(tokens[2], "delete") == 0)) {
355375129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
355475129cebSChurchill Khangar 			cmd_pipeline_table_delete_help);
355575129cebSChurchill Khangar 		return;
355675129cebSChurchill Khangar 	}
355775129cebSChurchill Khangar 
355875129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
355975129cebSChurchill Khangar 		(n_tokens == 3) &&
356075129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
356175129cebSChurchill Khangar 		(strcmp(tokens[2], "default") == 0)) {
356275129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
356375129cebSChurchill Khangar 			cmd_pipeline_table_default_help);
356475129cebSChurchill Khangar 		return;
356575129cebSChurchill Khangar 	}
356675129cebSChurchill Khangar 
356775129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
356875129cebSChurchill Khangar 		(n_tokens == 3) &&
356975129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
357075129cebSChurchill Khangar 		(strcmp(tokens[2], "show") == 0)) {
357175129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
357275129cebSChurchill Khangar 			cmd_pipeline_table_show_help);
357375129cebSChurchill Khangar 		return;
357475129cebSChurchill Khangar 	}
357575129cebSChurchill Khangar 
357675129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3577598fe0ddSCristian Dumitrescu 		(n_tokens == 4) &&
3578598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3579598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3580598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "add") == 0)) {
3581598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3582598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_add_help);
3583598fe0ddSCristian Dumitrescu 		return;
3584598fe0ddSCristian Dumitrescu 	}
3585598fe0ddSCristian Dumitrescu 
3586598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3587598fe0ddSCristian Dumitrescu 		(n_tokens == 4) &&
3588598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3589598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3590598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "delete") == 0)) {
3591598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3592598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_delete_help);
3593598fe0ddSCristian Dumitrescu 		return;
3594598fe0ddSCristian Dumitrescu 	}
3595598fe0ddSCristian Dumitrescu 
3596598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3597598fe0ddSCristian Dumitrescu 		(n_tokens == 5) &&
3598598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3599598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3600598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "member") == 0) &&
3601598fe0ddSCristian Dumitrescu 		(strcmp(tokens[4], "add") == 0)) {
3602598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3603598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_add_help);
3604598fe0ddSCristian Dumitrescu 		return;
3605598fe0ddSCristian Dumitrescu 	}
3606598fe0ddSCristian Dumitrescu 
3607598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3608598fe0ddSCristian Dumitrescu 		(n_tokens == 5) &&
3609598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3610598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3611598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "member") == 0) &&
3612598fe0ddSCristian Dumitrescu 		(strcmp(tokens[4], "delete") == 0)) {
3613598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3614598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_delete_help);
3615598fe0ddSCristian Dumitrescu 		return;
3616598fe0ddSCristian Dumitrescu 	}
3617598fe0ddSCristian Dumitrescu 
3618598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3619598fe0ddSCristian Dumitrescu 		(n_tokens == 3) &&
3620598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3621598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "show") == 0)) {
3622598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3623598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_show_help);
3624598fe0ddSCristian Dumitrescu 		return;
3625598fe0ddSCristian Dumitrescu 	}
3626598fe0ddSCristian Dumitrescu 
3627598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
36288bd4862fSCristian Dumitrescu 		(n_tokens == 3) &&
36298bd4862fSCristian Dumitrescu 		(strcmp(tokens[1], "learner") == 0) &&
36308bd4862fSCristian Dumitrescu 		(strcmp(tokens[2], "default") == 0)) {
36318bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
36328bd4862fSCristian Dumitrescu 			cmd_pipeline_learner_default_help);
36338bd4862fSCristian Dumitrescu 		return;
36348bd4862fSCristian Dumitrescu 	}
36358bd4862fSCristian Dumitrescu 
36368bd4862fSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
363775129cebSChurchill Khangar 		(n_tokens == 2) &&
363875129cebSChurchill Khangar 		(strcmp(tokens[1], "commit") == 0)) {
363975129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
364075129cebSChurchill Khangar 			cmd_pipeline_commit_help);
364175129cebSChurchill Khangar 		return;
364275129cebSChurchill Khangar 	}
364375129cebSChurchill Khangar 
364475129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
364575129cebSChurchill Khangar 		(n_tokens == 2) &&
364675129cebSChurchill Khangar 		(strcmp(tokens[1], "abort") == 0)) {
364775129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
364875129cebSChurchill Khangar 			cmd_pipeline_abort_help);
36495074e1d5SCristian Dumitrescu 		return;
36505074e1d5SCristian Dumitrescu 	}
36515074e1d5SCristian Dumitrescu 
36525074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
365364cfcebdSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "regrd") == 0)) {
365464cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_regrd_help);
365564cfcebdSCristian Dumitrescu 		return;
365664cfcebdSCristian Dumitrescu 	}
365764cfcebdSCristian Dumitrescu 
365864cfcebdSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
365964cfcebdSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "regwr") == 0)) {
366064cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_regwr_help);
366164cfcebdSCristian Dumitrescu 		return;
366264cfcebdSCristian Dumitrescu 	}
366364cfcebdSCristian Dumitrescu 
3664f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3665f38913b7SCristian Dumitrescu 		(n_tokens == 4) && !strcmp(tokens[1], "meter")
3666f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "profile")
3667f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[3], "add")) {
3668f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_profile_add_help);
3669f38913b7SCristian Dumitrescu 		return;
3670f38913b7SCristian Dumitrescu 	}
3671f38913b7SCristian Dumitrescu 
3672f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3673f38913b7SCristian Dumitrescu 		(n_tokens == 4) && !strcmp(tokens[1], "meter")
3674f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "profile")
3675f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[3], "delete")) {
3676f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_profile_delete_help);
3677f38913b7SCristian Dumitrescu 		return;
3678f38913b7SCristian Dumitrescu 	}
3679f38913b7SCristian Dumitrescu 
3680f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3681f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3682f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "reset")) {
3683f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_reset_help);
3684f38913b7SCristian Dumitrescu 		return;
3685f38913b7SCristian Dumitrescu 	}
3686f38913b7SCristian Dumitrescu 
3687f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3688f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3689f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "set")) {
3690f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_set_help);
3691f38913b7SCristian Dumitrescu 		return;
3692f38913b7SCristian Dumitrescu 	}
3693f38913b7SCristian Dumitrescu 
3694f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3695f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3696f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "stats")) {
3697f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_stats_help);
3698f38913b7SCristian Dumitrescu 		return;
3699f38913b7SCristian Dumitrescu 	}
3700f38913b7SCristian Dumitrescu 
37018ba342ceSCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
37028ba342ceSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "rss")) {
37038ba342ceSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_rss_help);
37048ba342ceSCristian Dumitrescu 		return;
37058ba342ceSCristian Dumitrescu 	}
37068ba342ceSCristian Dumitrescu 
370764cfcebdSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
37087fef9ef1SYogesh Jangra 		(n_tokens == 2) && (strcmp(tokens[1], "stats") == 0)) {
37095074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_stats_help);
37105074e1d5SCristian Dumitrescu 		return;
37115074e1d5SCristian Dumitrescu 	}
37125074e1d5SCristian Dumitrescu 
371317225455SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
371417225455SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "mirror")
371517225455SCristian Dumitrescu 		&& !strcmp(tokens[2], "session")) {
371617225455SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_mirror_session_help);
371717225455SCristian Dumitrescu 		return;
371817225455SCristian Dumitrescu 	}
371917225455SCristian Dumitrescu 
372041f5dfcbSCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
372141f5dfcbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "enable")) {
372241f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_enable_help);
372341f5dfcbSCristian Dumitrescu 		return;
372441f5dfcbSCristian Dumitrescu 	}
372541f5dfcbSCristian Dumitrescu 
372641f5dfcbSCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
372741f5dfcbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "disable")) {
372841f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_disable_help);
372941f5dfcbSCristian Dumitrescu 		return;
373041f5dfcbSCristian Dumitrescu 	}
373141f5dfcbSCristian Dumitrescu 
37323b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
37333b0cc5fbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "create")) {
37343b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_create_help);
37353b0cc5fbSCristian Dumitrescu 		return;
37363b0cc5fbSCristian Dumitrescu 	}
37373b0cc5fbSCristian Dumitrescu 
37383b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
37393b0cc5fbSCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "sa")
37403b0cc5fbSCristian Dumitrescu 		&& !strcmp(tokens[2], "add")) {
37413b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_sa_add_help);
37423b0cc5fbSCristian Dumitrescu 		return;
37433b0cc5fbSCristian Dumitrescu 	}
37443b0cc5fbSCristian Dumitrescu 
37453b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
37463b0cc5fbSCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "sa")
37473b0cc5fbSCristian Dumitrescu 		&& !strcmp(tokens[2], "delete")) {
37483b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_sa_delete_help);
37493b0cc5fbSCristian Dumitrescu 		return;
37503b0cc5fbSCristian Dumitrescu 	}
37513b0cc5fbSCristian Dumitrescu 
37526d99096cSCristian Dumitrescu 	if (!strcmp(tokens[0], "block") &&
37536d99096cSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "enable")) {
37546d99096cSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_block_enable_help);
37556d99096cSCristian Dumitrescu 		return;
37566d99096cSCristian Dumitrescu 	}
37576d99096cSCristian Dumitrescu 
37586d99096cSCristian Dumitrescu 	if (!strcmp(tokens[0], "block") &&
37596d99096cSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "disable")) {
37606d99096cSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_block_disable_help);
37616d99096cSCristian Dumitrescu 		return;
37626d99096cSCristian Dumitrescu 	}
37636d99096cSCristian Dumitrescu 
37645074e1d5SCristian Dumitrescu 	snprintf(out, out_size, "Invalid command\n");
37655074e1d5SCristian Dumitrescu }
37665074e1d5SCristian Dumitrescu 
37675074e1d5SCristian Dumitrescu void
37685074e1d5SCristian Dumitrescu cli_process(char *in, char *out, size_t out_size, void *obj)
37695074e1d5SCristian Dumitrescu {
37705074e1d5SCristian Dumitrescu 	char *tokens[CMD_MAX_TOKENS];
37715074e1d5SCristian Dumitrescu 	uint32_t n_tokens = RTE_DIM(tokens);
37725074e1d5SCristian Dumitrescu 	int status;
37735074e1d5SCristian Dumitrescu 
37745074e1d5SCristian Dumitrescu 	if (is_comment(in))
37755074e1d5SCristian Dumitrescu 		return;
37765074e1d5SCristian Dumitrescu 
37775074e1d5SCristian Dumitrescu 	status = parse_tokenize_string(in, tokens, &n_tokens);
37785074e1d5SCristian Dumitrescu 	if (status) {
37795074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
37805074e1d5SCristian Dumitrescu 		return;
37815074e1d5SCristian Dumitrescu 	}
37825074e1d5SCristian Dumitrescu 
37835074e1d5SCristian Dumitrescu 	if (n_tokens == 0)
37845074e1d5SCristian Dumitrescu 		return;
37855074e1d5SCristian Dumitrescu 
37865074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "help") == 0) {
37875074e1d5SCristian Dumitrescu 		cmd_help(tokens, n_tokens, out, out_size, obj);
37885074e1d5SCristian Dumitrescu 		return;
37895074e1d5SCristian Dumitrescu 	}
37905074e1d5SCristian Dumitrescu 
37915074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "mempool") == 0) {
37925074e1d5SCristian Dumitrescu 		cmd_mempool(tokens, n_tokens, out, out_size, obj);
37935074e1d5SCristian Dumitrescu 		return;
37945074e1d5SCristian Dumitrescu 	}
37955074e1d5SCristian Dumitrescu 
3796f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[0], "ethdev") == 0) {
3797821848f5SCristian Dumitrescu 		if ((n_tokens >= 2) && (strcmp(tokens[1], "show") == 0)) {
3798f31c80f8SCristian Dumitrescu 			cmd_ethdev_show(tokens, n_tokens, out, out_size, obj);
37995074e1d5SCristian Dumitrescu 			return;
38005074e1d5SCristian Dumitrescu 		}
38015074e1d5SCristian Dumitrescu 
3802f31c80f8SCristian Dumitrescu 		cmd_ethdev(tokens, n_tokens, out, out_size, obj);
38035074e1d5SCristian Dumitrescu 		return;
38045074e1d5SCristian Dumitrescu 	}
38055074e1d5SCristian Dumitrescu 
380677a41301SCristian Dumitrescu 	if (strcmp(tokens[0], "ring") == 0) {
380777a41301SCristian Dumitrescu 		cmd_ring(tokens, n_tokens, out, out_size, obj);
380877a41301SCristian Dumitrescu 		return;
380977a41301SCristian Dumitrescu 	}
381077a41301SCristian Dumitrescu 
38111b41a527SCristian Dumitrescu 	if (!strcmp(tokens[0], "cryptodev")) {
38121b41a527SCristian Dumitrescu 		cmd_cryptodev(tokens, n_tokens, out, out_size, obj);
38131b41a527SCristian Dumitrescu 		return;
38141b41a527SCristian Dumitrescu 	}
38151b41a527SCristian Dumitrescu 
38165074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "pipeline") == 0) {
38175074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
38189043f66aSCristian Dumitrescu 			(strcmp(tokens[1], "codegen") == 0)) {
38199043f66aSCristian Dumitrescu 			cmd_pipeline_codegen(tokens, n_tokens, out, out_size,
38209043f66aSCristian Dumitrescu 				obj);
38219043f66aSCristian Dumitrescu 			return;
38229043f66aSCristian Dumitrescu 		}
38239043f66aSCristian Dumitrescu 
38249043f66aSCristian Dumitrescu 		if ((n_tokens >= 3) &&
38256bc14d9fSCristian Dumitrescu 			(strcmp(tokens[1], "libbuild") == 0)) {
38266bc14d9fSCristian Dumitrescu 			cmd_pipeline_libbuild(tokens, n_tokens, out, out_size,
38276bc14d9fSCristian Dumitrescu 				obj);
38286bc14d9fSCristian Dumitrescu 			return;
38296bc14d9fSCristian Dumitrescu 		}
38306bc14d9fSCristian Dumitrescu 
38316bc14d9fSCristian Dumitrescu 		if ((n_tokens >= 3) &&
38325074e1d5SCristian Dumitrescu 			(strcmp(tokens[2], "build") == 0)) {
38335074e1d5SCristian Dumitrescu 			cmd_pipeline_build(tokens, n_tokens, out, out_size,
38345074e1d5SCristian Dumitrescu 				obj);
38355074e1d5SCristian Dumitrescu 			return;
38365074e1d5SCristian Dumitrescu 		}
38375074e1d5SCristian Dumitrescu 
383875129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
383975129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
384075129cebSChurchill Khangar 			(strcmp(tokens[4], "add") == 0)) {
384175129cebSChurchill Khangar 			cmd_pipeline_table_add(tokens, n_tokens, out,
384275129cebSChurchill Khangar 				out_size, obj);
384375129cebSChurchill Khangar 			return;
384475129cebSChurchill Khangar 		}
384575129cebSChurchill Khangar 
384675129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
384775129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
384875129cebSChurchill Khangar 			(strcmp(tokens[4], "delete") == 0)) {
384975129cebSChurchill Khangar 			cmd_pipeline_table_delete(tokens, n_tokens, out,
385075129cebSChurchill Khangar 				out_size, obj);
385175129cebSChurchill Khangar 			return;
385275129cebSChurchill Khangar 		}
385375129cebSChurchill Khangar 
385475129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
385575129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
385675129cebSChurchill Khangar 			(strcmp(tokens[4], "default") == 0)) {
385775129cebSChurchill Khangar 			cmd_pipeline_table_default(tokens, n_tokens, out,
385875129cebSChurchill Khangar 				out_size, obj);
385975129cebSChurchill Khangar 			return;
386075129cebSChurchill Khangar 		}
386175129cebSChurchill Khangar 
386275129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
386375129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
386475129cebSChurchill Khangar 			(strcmp(tokens[4], "show") == 0)) {
386575129cebSChurchill Khangar 			cmd_pipeline_table_show(tokens, n_tokens, out,
386675129cebSChurchill Khangar 				out_size, obj);
386775129cebSChurchill Khangar 			return;
386875129cebSChurchill Khangar 		}
386975129cebSChurchill Khangar 
3870598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 6) &&
3871598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3872598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3873598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "add") == 0)) {
3874598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_add(tokens, n_tokens, out,
3875598fe0ddSCristian Dumitrescu 				out_size, obj);
3876598fe0ddSCristian Dumitrescu 			return;
3877598fe0ddSCristian Dumitrescu 		}
3878598fe0ddSCristian Dumitrescu 
3879598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 6) &&
3880598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3881598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3882598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "delete") == 0)) {
3883598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_delete(tokens, n_tokens, out,
3884598fe0ddSCristian Dumitrescu 				out_size, obj);
3885598fe0ddSCristian Dumitrescu 			return;
3886598fe0ddSCristian Dumitrescu 		}
3887598fe0ddSCristian Dumitrescu 
3888598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 7) &&
3889598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3890598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3891598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "member") == 0) &&
3892598fe0ddSCristian Dumitrescu 			(strcmp(tokens[6], "add") == 0)) {
3893598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_add(tokens, n_tokens, out,
3894598fe0ddSCristian Dumitrescu 				out_size, obj);
3895598fe0ddSCristian Dumitrescu 			return;
3896598fe0ddSCristian Dumitrescu 		}
3897598fe0ddSCristian Dumitrescu 
3898598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 7) &&
3899598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3900598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3901598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "member") == 0) &&
3902598fe0ddSCristian Dumitrescu 			(strcmp(tokens[6], "delete") == 0)) {
3903598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_delete(tokens, n_tokens, out,
3904598fe0ddSCristian Dumitrescu 				out_size, obj);
3905598fe0ddSCristian Dumitrescu 			return;
3906598fe0ddSCristian Dumitrescu 		}
3907598fe0ddSCristian Dumitrescu 
3908598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 5) &&
3909598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3910598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "show") == 0)) {
3911598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_show(tokens, n_tokens, out,
3912598fe0ddSCristian Dumitrescu 				out_size, obj);
3913598fe0ddSCristian Dumitrescu 			return;
3914598fe0ddSCristian Dumitrescu 		}
3915598fe0ddSCristian Dumitrescu 
39168bd4862fSCristian Dumitrescu 		if ((n_tokens >= 5) &&
39178bd4862fSCristian Dumitrescu 			(strcmp(tokens[2], "learner") == 0) &&
39188bd4862fSCristian Dumitrescu 			(strcmp(tokens[4], "default") == 0)) {
39198bd4862fSCristian Dumitrescu 			cmd_pipeline_learner_default(tokens, n_tokens, out,
39208bd4862fSCristian Dumitrescu 				out_size, obj);
39218bd4862fSCristian Dumitrescu 			return;
39228bd4862fSCristian Dumitrescu 		}
39238bd4862fSCristian Dumitrescu 
39245074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
392575129cebSChurchill Khangar 			(strcmp(tokens[2], "commit") == 0)) {
392675129cebSChurchill Khangar 			cmd_pipeline_commit(tokens, n_tokens, out,
392775129cebSChurchill Khangar 				out_size, obj);
392875129cebSChurchill Khangar 			return;
392975129cebSChurchill Khangar 		}
393075129cebSChurchill Khangar 
393175129cebSChurchill Khangar 		if ((n_tokens >= 3) &&
393275129cebSChurchill Khangar 			(strcmp(tokens[2], "abort") == 0)) {
393375129cebSChurchill Khangar 			cmd_pipeline_abort(tokens, n_tokens, out,
39345074e1d5SCristian Dumitrescu 				out_size, obj);
39355074e1d5SCristian Dumitrescu 			return;
39365074e1d5SCristian Dumitrescu 		}
39375074e1d5SCristian Dumitrescu 
39385074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
393964cfcebdSCristian Dumitrescu 			(strcmp(tokens[2], "regrd") == 0)) {
394064cfcebdSCristian Dumitrescu 			cmd_pipeline_regrd(tokens, n_tokens, out, out_size, obj);
394164cfcebdSCristian Dumitrescu 			return;
394264cfcebdSCristian Dumitrescu 		}
394364cfcebdSCristian Dumitrescu 
394464cfcebdSCristian Dumitrescu 		if ((n_tokens >= 3) &&
394564cfcebdSCristian Dumitrescu 			(strcmp(tokens[2], "regwr") == 0)) {
394664cfcebdSCristian Dumitrescu 			cmd_pipeline_regwr(tokens, n_tokens, out, out_size, obj);
394764cfcebdSCristian Dumitrescu 			return;
394864cfcebdSCristian Dumitrescu 		}
394964cfcebdSCristian Dumitrescu 
3950f38913b7SCristian Dumitrescu 		if ((n_tokens >= 6) &&
3951f38913b7SCristian Dumitrescu 			(strcmp(tokens[2], "meter") == 0) &&
3952f38913b7SCristian Dumitrescu 			(strcmp(tokens[3], "profile") == 0) &&
3953f38913b7SCristian Dumitrescu 			(strcmp(tokens[5], "add") == 0)) {
3954f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_profile_add(tokens, n_tokens, out, out_size, obj);
3955f38913b7SCristian Dumitrescu 			return;
3956f38913b7SCristian Dumitrescu 		}
3957f38913b7SCristian Dumitrescu 
3958f38913b7SCristian Dumitrescu 		if ((n_tokens >= 6) &&
3959f38913b7SCristian Dumitrescu 			(strcmp(tokens[2], "meter") == 0) &&
3960f38913b7SCristian Dumitrescu 			(strcmp(tokens[3], "profile") == 0) &&
3961f38913b7SCristian Dumitrescu 			(strcmp(tokens[5], "delete") == 0)) {
3962f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_profile_delete(tokens, n_tokens, out, out_size, obj);
3963f38913b7SCristian Dumitrescu 			return;
3964f38913b7SCristian Dumitrescu 		}
3965f38913b7SCristian Dumitrescu 
396612eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "reset")) {
3967f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_reset(tokens, n_tokens, out, out_size, obj);
3968f38913b7SCristian Dumitrescu 			return;
3969f38913b7SCristian Dumitrescu 		}
3970f38913b7SCristian Dumitrescu 
397112eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "set")) {
3972f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_set(tokens, n_tokens, out, out_size, obj);
3973f38913b7SCristian Dumitrescu 			return;
3974f38913b7SCristian Dumitrescu 		}
3975f38913b7SCristian Dumitrescu 
397612eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "stats")) {
3977f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_stats(tokens, n_tokens, out, out_size, obj);
3978f38913b7SCristian Dumitrescu 			return;
3979f38913b7SCristian Dumitrescu 		}
3980f38913b7SCristian Dumitrescu 
39818ba342ceSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "rss")) {
39828ba342ceSCristian Dumitrescu 			cmd_pipeline_rss(tokens, n_tokens, out, out_size, obj);
39838ba342ceSCristian Dumitrescu 			return;
39848ba342ceSCristian Dumitrescu 		}
39858ba342ceSCristian Dumitrescu 
398664cfcebdSCristian Dumitrescu 		if ((n_tokens >= 3) &&
39875074e1d5SCristian Dumitrescu 			(strcmp(tokens[2], "stats") == 0)) {
39885074e1d5SCristian Dumitrescu 			cmd_pipeline_stats(tokens, n_tokens, out, out_size,
39895074e1d5SCristian Dumitrescu 				obj);
39905074e1d5SCristian Dumitrescu 			return;
39915074e1d5SCristian Dumitrescu 		}
399217225455SCristian Dumitrescu 
399317225455SCristian Dumitrescu 		if ((n_tokens >= 4) &&
399417225455SCristian Dumitrescu 			(strcmp(tokens[2], "mirror") == 0) &&
399517225455SCristian Dumitrescu 			(strcmp(tokens[3], "session") == 0)) {
399617225455SCristian Dumitrescu 			cmd_pipeline_mirror_session(tokens, n_tokens, out, out_size, obj);
399717225455SCristian Dumitrescu 			return;
399817225455SCristian Dumitrescu 		}
399941f5dfcbSCristian Dumitrescu 
400041f5dfcbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "enable")) {
400141f5dfcbSCristian Dumitrescu 			cmd_pipeline_enable(tokens, n_tokens, out, out_size, obj);
400241f5dfcbSCristian Dumitrescu 			return;
400341f5dfcbSCristian Dumitrescu 		}
400441f5dfcbSCristian Dumitrescu 
400541f5dfcbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "disable")) {
400641f5dfcbSCristian Dumitrescu 			cmd_pipeline_disable(tokens, n_tokens, out, out_size, obj);
400741f5dfcbSCristian Dumitrescu 			return;
400841f5dfcbSCristian Dumitrescu 		}
40095074e1d5SCristian Dumitrescu 	}
40105074e1d5SCristian Dumitrescu 
40113b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec")) {
40123b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "create")) {
40133b0cc5fbSCristian Dumitrescu 			cmd_ipsec_create(tokens, n_tokens, out, out_size, obj);
40143b0cc5fbSCristian Dumitrescu 			return;
40153b0cc5fbSCristian Dumitrescu 		}
40163b0cc5fbSCristian Dumitrescu 
40173b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 4 && !strcmp(tokens[2], "sa") && !strcmp(tokens[3], "add")) {
40183b0cc5fbSCristian Dumitrescu 			cmd_ipsec_sa_add(tokens, n_tokens, out, out_size, obj);
40193b0cc5fbSCristian Dumitrescu 			return;
40203b0cc5fbSCristian Dumitrescu 		}
40213b0cc5fbSCristian Dumitrescu 
40223b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 4 && !strcmp(tokens[2], "sa") && !strcmp(tokens[3], "delete")) {
40233b0cc5fbSCristian Dumitrescu 			cmd_ipsec_sa_delete(tokens, n_tokens, out, out_size, obj);
40243b0cc5fbSCristian Dumitrescu 			return;
40253b0cc5fbSCristian Dumitrescu 		}
40263b0cc5fbSCristian Dumitrescu 	}
40273b0cc5fbSCristian Dumitrescu 
40286d99096cSCristian Dumitrescu 	if (!strcmp(tokens[0], "block")) {
40296d99096cSCristian Dumitrescu 		if (n_tokens >= 6 && !strcmp(tokens[5], "enable")) {
40306d99096cSCristian Dumitrescu 			cmd_block_enable(tokens, n_tokens, out, out_size, obj);
40316d99096cSCristian Dumitrescu 			return;
40326d99096cSCristian Dumitrescu 		}
40336d99096cSCristian Dumitrescu 
40346d99096cSCristian Dumitrescu 		if (n_tokens >= 6 && !strcmp(tokens[5], "disable")) {
40356d99096cSCristian Dumitrescu 			cmd_block_disable(tokens, n_tokens, out, out_size, obj);
40366d99096cSCristian Dumitrescu 			return;
40376d99096cSCristian Dumitrescu 		}
40386d99096cSCristian Dumitrescu 	}
40396d99096cSCristian Dumitrescu 
40405074e1d5SCristian Dumitrescu 	snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
40415074e1d5SCristian Dumitrescu }
40425074e1d5SCristian Dumitrescu 
40435074e1d5SCristian Dumitrescu int
40445074e1d5SCristian Dumitrescu cli_script_process(const char *file_name,
40455074e1d5SCristian Dumitrescu 	size_t msg_in_len_max,
40465074e1d5SCristian Dumitrescu 	size_t msg_out_len_max,
40475074e1d5SCristian Dumitrescu 	void *obj)
40485074e1d5SCristian Dumitrescu {
40495074e1d5SCristian Dumitrescu 	char *msg_in = NULL, *msg_out = NULL;
40505074e1d5SCristian Dumitrescu 	FILE *f = NULL;
40515074e1d5SCristian Dumitrescu 
40525074e1d5SCristian Dumitrescu 	/* Check input arguments */
40535074e1d5SCristian Dumitrescu 	if ((file_name == NULL) ||
40545074e1d5SCristian Dumitrescu 		(strlen(file_name) == 0) ||
40555074e1d5SCristian Dumitrescu 		(msg_in_len_max == 0) ||
40565074e1d5SCristian Dumitrescu 		(msg_out_len_max == 0))
40575074e1d5SCristian Dumitrescu 		return -EINVAL;
40585074e1d5SCristian Dumitrescu 
40595074e1d5SCristian Dumitrescu 	msg_in = malloc(msg_in_len_max + 1);
40605074e1d5SCristian Dumitrescu 	msg_out = malloc(msg_out_len_max + 1);
40615074e1d5SCristian Dumitrescu 	if ((msg_in == NULL) ||
40625074e1d5SCristian Dumitrescu 		(msg_out == NULL)) {
40635074e1d5SCristian Dumitrescu 		free(msg_out);
40645074e1d5SCristian Dumitrescu 		free(msg_in);
40655074e1d5SCristian Dumitrescu 		return -ENOMEM;
40665074e1d5SCristian Dumitrescu 	}
40675074e1d5SCristian Dumitrescu 
40685074e1d5SCristian Dumitrescu 	/* Open input file */
40695074e1d5SCristian Dumitrescu 	f = fopen(file_name, "r");
40705074e1d5SCristian Dumitrescu 	if (f == NULL) {
40715074e1d5SCristian Dumitrescu 		free(msg_out);
40725074e1d5SCristian Dumitrescu 		free(msg_in);
40735074e1d5SCristian Dumitrescu 		return -EIO;
40745074e1d5SCristian Dumitrescu 	}
40755074e1d5SCristian Dumitrescu 
40765074e1d5SCristian Dumitrescu 	/* Read file */
40775074e1d5SCristian Dumitrescu 	for ( ; ; ) {
40785074e1d5SCristian Dumitrescu 		if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
40795074e1d5SCristian Dumitrescu 			break;
40805074e1d5SCristian Dumitrescu 
40815074e1d5SCristian Dumitrescu 		printf("%s", msg_in);
40825074e1d5SCristian Dumitrescu 		msg_out[0] = 0;
40835074e1d5SCristian Dumitrescu 
40845074e1d5SCristian Dumitrescu 		cli_process(msg_in,
40855074e1d5SCristian Dumitrescu 			msg_out,
40865074e1d5SCristian Dumitrescu 			msg_out_len_max,
40875074e1d5SCristian Dumitrescu 			obj);
40885074e1d5SCristian Dumitrescu 
40895074e1d5SCristian Dumitrescu 		if (strlen(msg_out))
40905074e1d5SCristian Dumitrescu 			printf("%s", msg_out);
40915074e1d5SCristian Dumitrescu 	}
40925074e1d5SCristian Dumitrescu 
40935074e1d5SCristian Dumitrescu 	/* Close file */
40945074e1d5SCristian Dumitrescu 	fclose(f);
40955074e1d5SCristian Dumitrescu 	free(msg_out);
40965074e1d5SCristian Dumitrescu 	free(msg_in);
40975074e1d5SCristian Dumitrescu 	return 0;
40985074e1d5SCristian Dumitrescu }
4099