xref: /dpdk/examples/pipeline/cli.c (revision 41f5dfcbf973b41d08fdfd2842c0e539edcc4f03)
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>
225074e1d5SCristian Dumitrescu 
235074e1d5SCristian Dumitrescu #include "cli.h"
245074e1d5SCristian Dumitrescu 
255074e1d5SCristian Dumitrescu #include "obj.h"
265074e1d5SCristian Dumitrescu #include "thread.h"
275074e1d5SCristian Dumitrescu 
285074e1d5SCristian Dumitrescu #ifndef CMD_MAX_TOKENS
295074e1d5SCristian Dumitrescu #define CMD_MAX_TOKENS     256
305074e1d5SCristian Dumitrescu #endif
315074e1d5SCristian Dumitrescu 
326bc14d9fSCristian Dumitrescu #ifndef MAX_LINE_SIZE
336bc14d9fSCristian Dumitrescu #define MAX_LINE_SIZE 2048
346bc14d9fSCristian Dumitrescu #endif
356bc14d9fSCristian Dumitrescu 
365074e1d5SCristian Dumitrescu #define MSG_OUT_OF_MEMORY   "Not enough memory.\n"
375074e1d5SCristian Dumitrescu #define MSG_CMD_UNKNOWN     "Unknown command \"%s\".\n"
385074e1d5SCristian Dumitrescu #define MSG_CMD_UNIMPLEM    "Command \"%s\" not implemented.\n"
395074e1d5SCristian Dumitrescu #define MSG_ARG_NOT_ENOUGH  "Not enough arguments for command \"%s\".\n"
405074e1d5SCristian Dumitrescu #define MSG_ARG_TOO_MANY    "Too many arguments for command \"%s\".\n"
415074e1d5SCristian Dumitrescu #define MSG_ARG_MISMATCH    "Wrong number of arguments for command \"%s\".\n"
425074e1d5SCristian Dumitrescu #define MSG_ARG_NOT_FOUND   "Argument \"%s\" not found.\n"
435074e1d5SCristian Dumitrescu #define MSG_ARG_INVALID     "Invalid value for argument \"%s\".\n"
445074e1d5SCristian Dumitrescu #define MSG_FILE_ERR        "Error in file \"%s\" at line %u.\n"
455074e1d5SCristian Dumitrescu #define MSG_FILE_NOT_ENOUGH "Not enough rules in file \"%s\".\n"
465074e1d5SCristian Dumitrescu #define MSG_CMD_FAIL        "Command \"%s\" failed.\n"
475074e1d5SCristian Dumitrescu 
485074e1d5SCristian Dumitrescu #define skip_white_spaces(pos)			\
495074e1d5SCristian Dumitrescu ({						\
505074e1d5SCristian Dumitrescu 	__typeof__(pos) _p = (pos);		\
515074e1d5SCristian Dumitrescu 	for ( ; isspace(*_p); _p++)		\
525074e1d5SCristian Dumitrescu 		;				\
535074e1d5SCristian Dumitrescu 	_p;					\
545074e1d5SCristian Dumitrescu })
555074e1d5SCristian Dumitrescu 
565074e1d5SCristian Dumitrescu static int
575074e1d5SCristian Dumitrescu parser_read_uint64(uint64_t *value, const char *p)
585074e1d5SCristian Dumitrescu {
595074e1d5SCristian Dumitrescu 	char *next;
605074e1d5SCristian Dumitrescu 	uint64_t val;
615074e1d5SCristian Dumitrescu 
625074e1d5SCristian Dumitrescu 	p = skip_white_spaces(p);
635074e1d5SCristian Dumitrescu 	if (!isdigit(*p))
645074e1d5SCristian Dumitrescu 		return -EINVAL;
655074e1d5SCristian Dumitrescu 
660d644eb6SChurchill Khangar 	val = strtoul(p, &next, 0);
675074e1d5SCristian Dumitrescu 	if (p == next)
685074e1d5SCristian Dumitrescu 		return -EINVAL;
695074e1d5SCristian Dumitrescu 
705074e1d5SCristian Dumitrescu 	p = next;
715074e1d5SCristian Dumitrescu 	switch (*p) {
725074e1d5SCristian Dumitrescu 	case 'T':
735074e1d5SCristian Dumitrescu 		val *= 1024ULL;
745074e1d5SCristian Dumitrescu 		/* fall through */
755074e1d5SCristian Dumitrescu 	case 'G':
765074e1d5SCristian Dumitrescu 		val *= 1024ULL;
775074e1d5SCristian Dumitrescu 		/* fall through */
785074e1d5SCristian Dumitrescu 	case 'M':
795074e1d5SCristian Dumitrescu 		val *= 1024ULL;
805074e1d5SCristian Dumitrescu 		/* fall through */
815074e1d5SCristian Dumitrescu 	case 'k':
825074e1d5SCristian Dumitrescu 	case 'K':
835074e1d5SCristian Dumitrescu 		val *= 1024ULL;
845074e1d5SCristian Dumitrescu 		p++;
855074e1d5SCristian Dumitrescu 		break;
865074e1d5SCristian Dumitrescu 	}
875074e1d5SCristian Dumitrescu 
885074e1d5SCristian Dumitrescu 	p = skip_white_spaces(p);
895074e1d5SCristian Dumitrescu 	if (*p != '\0')
905074e1d5SCristian Dumitrescu 		return -EINVAL;
915074e1d5SCristian Dumitrescu 
925074e1d5SCristian Dumitrescu 	*value = val;
935074e1d5SCristian Dumitrescu 	return 0;
945074e1d5SCristian Dumitrescu }
955074e1d5SCristian Dumitrescu 
965074e1d5SCristian Dumitrescu static int
975074e1d5SCristian Dumitrescu parser_read_uint32(uint32_t *value, const char *p)
985074e1d5SCristian Dumitrescu {
995074e1d5SCristian Dumitrescu 	uint64_t val = 0;
1005074e1d5SCristian Dumitrescu 	int ret = parser_read_uint64(&val, p);
1015074e1d5SCristian Dumitrescu 
1025074e1d5SCristian Dumitrescu 	if (ret < 0)
1035074e1d5SCristian Dumitrescu 		return ret;
1045074e1d5SCristian Dumitrescu 
1055074e1d5SCristian Dumitrescu 	if (val > UINT32_MAX)
1065074e1d5SCristian Dumitrescu 		return -ERANGE;
1075074e1d5SCristian Dumitrescu 
1085074e1d5SCristian Dumitrescu 	*value = val;
1095074e1d5SCristian Dumitrescu 	return 0;
1105074e1d5SCristian Dumitrescu }
1115074e1d5SCristian Dumitrescu 
1125074e1d5SCristian Dumitrescu #define PARSE_DELIMITER " \f\n\r\t\v"
1135074e1d5SCristian Dumitrescu 
1145074e1d5SCristian Dumitrescu static int
1155074e1d5SCristian Dumitrescu parse_tokenize_string(char *string, char *tokens[], uint32_t *n_tokens)
1165074e1d5SCristian Dumitrescu {
1175074e1d5SCristian Dumitrescu 	uint32_t i;
1185074e1d5SCristian Dumitrescu 
1195074e1d5SCristian Dumitrescu 	if ((string == NULL) ||
1205074e1d5SCristian Dumitrescu 		(tokens == NULL) ||
1215074e1d5SCristian Dumitrescu 		(*n_tokens < 1))
1225074e1d5SCristian Dumitrescu 		return -EINVAL;
1235074e1d5SCristian Dumitrescu 
1245074e1d5SCristian Dumitrescu 	for (i = 0; i < *n_tokens; i++) {
1255074e1d5SCristian Dumitrescu 		tokens[i] = strtok_r(string, PARSE_DELIMITER, &string);
1265074e1d5SCristian Dumitrescu 		if (tokens[i] == NULL)
1275074e1d5SCristian Dumitrescu 			break;
1285074e1d5SCristian Dumitrescu 	}
1295074e1d5SCristian Dumitrescu 
1305074e1d5SCristian Dumitrescu 	if ((i == *n_tokens) && strtok_r(string, PARSE_DELIMITER, &string))
1315074e1d5SCristian Dumitrescu 		return -E2BIG;
1325074e1d5SCristian Dumitrescu 
1335074e1d5SCristian Dumitrescu 	*n_tokens = i;
1345074e1d5SCristian Dumitrescu 	return 0;
1355074e1d5SCristian Dumitrescu }
1365074e1d5SCristian Dumitrescu 
1375074e1d5SCristian Dumitrescu static int
1385074e1d5SCristian Dumitrescu is_comment(char *in)
1395074e1d5SCristian Dumitrescu {
1405074e1d5SCristian Dumitrescu 	if ((strlen(in) && index("!#%;", in[0])) ||
1415074e1d5SCristian Dumitrescu 		(strncmp(in, "//", 2) == 0) ||
1425074e1d5SCristian Dumitrescu 		(strncmp(in, "--", 2) == 0))
1435074e1d5SCristian Dumitrescu 		return 1;
1445074e1d5SCristian Dumitrescu 
1455074e1d5SCristian Dumitrescu 	return 0;
1465074e1d5SCristian Dumitrescu }
1475074e1d5SCristian Dumitrescu 
14883f58a7bSCristian Dumitrescu static void
14983f58a7bSCristian Dumitrescu table_entry_free(struct rte_swx_table_entry *entry)
15083f58a7bSCristian Dumitrescu {
15183f58a7bSCristian Dumitrescu 	if (!entry)
15283f58a7bSCristian Dumitrescu 		return;
15383f58a7bSCristian Dumitrescu 
15483f58a7bSCristian Dumitrescu 	free(entry->key);
15583f58a7bSCristian Dumitrescu 	free(entry->key_mask);
15683f58a7bSCristian Dumitrescu 	free(entry->action_data);
15783f58a7bSCristian Dumitrescu 	free(entry);
15883f58a7bSCristian Dumitrescu }
15983f58a7bSCristian Dumitrescu 
16083f58a7bSCristian Dumitrescu static struct rte_swx_table_entry *
16183f58a7bSCristian Dumitrescu parse_table_entry(struct rte_swx_ctl_pipeline *p,
16283f58a7bSCristian Dumitrescu 		  char *table_name,
16383f58a7bSCristian Dumitrescu 		  char **tokens,
16483f58a7bSCristian Dumitrescu 		  uint32_t n_tokens)
16583f58a7bSCristian Dumitrescu {
16683f58a7bSCristian Dumitrescu 	struct rte_swx_table_entry *entry;
16783f58a7bSCristian Dumitrescu 	char *line;
16883f58a7bSCristian Dumitrescu 	uint32_t i;
16983f58a7bSCristian Dumitrescu 
17083f58a7bSCristian Dumitrescu 	/* Buffer allocation. */
17183f58a7bSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
17283f58a7bSCristian Dumitrescu 	if (!line)
17383f58a7bSCristian Dumitrescu 		return NULL;
17483f58a7bSCristian Dumitrescu 
17583f58a7bSCristian Dumitrescu 	/* Copy tokens to buffer. Since the tokens were initially part of a buffer of size
17683f58a7bSCristian Dumitrescu 	 * MAX_LINE_LENGTH, it is guaranteed that putting back some of them into a buffer of the
17783f58a7bSCristian Dumitrescu 	 * same size separated by a single space will not result in buffer overrun.
17883f58a7bSCristian Dumitrescu 	 */
17983f58a7bSCristian Dumitrescu 	line[0] = 0;
18083f58a7bSCristian Dumitrescu 	for (i = 0; i < n_tokens; i++) {
18183f58a7bSCristian Dumitrescu 		if (i)
18283f58a7bSCristian Dumitrescu 			strcat(line, " ");
18383f58a7bSCristian Dumitrescu 
18483f58a7bSCristian Dumitrescu 		strcat(line, tokens[i]);
18583f58a7bSCristian Dumitrescu 	}
18683f58a7bSCristian Dumitrescu 
18783f58a7bSCristian Dumitrescu 	/* Read the table entry from the input buffer. */
18883f58a7bSCristian Dumitrescu 	entry = rte_swx_ctl_pipeline_table_entry_read(p, table_name, line, NULL);
18983f58a7bSCristian Dumitrescu 
19083f58a7bSCristian Dumitrescu 	/* Buffer free. */
19183f58a7bSCristian Dumitrescu 	free(line);
19283f58a7bSCristian Dumitrescu 
19383f58a7bSCristian Dumitrescu 	return entry;
19483f58a7bSCristian Dumitrescu }
19583f58a7bSCristian Dumitrescu 
1965074e1d5SCristian Dumitrescu static const char cmd_mempool_help[] =
19702d36ef6SCristian Dumitrescu "mempool <mempool_name> "
19802d36ef6SCristian Dumitrescu "meta <mbuf_private_size> "
19902d36ef6SCristian Dumitrescu "pkt <pkt_buffer_size> "
20002d36ef6SCristian Dumitrescu "pool <pool_size> "
20102d36ef6SCristian Dumitrescu "cache <cache_size> "
20202d36ef6SCristian Dumitrescu "numa <numa_node>\n";
2035074e1d5SCristian Dumitrescu 
2045074e1d5SCristian Dumitrescu static void
2055074e1d5SCristian Dumitrescu cmd_mempool(char **tokens,
2065074e1d5SCristian Dumitrescu 	    uint32_t n_tokens,
2075074e1d5SCristian Dumitrescu 	    char *out,
2085074e1d5SCristian Dumitrescu 	    size_t out_size,
20902d36ef6SCristian Dumitrescu 	    void *obj __rte_unused)
2105074e1d5SCristian Dumitrescu {
21102d36ef6SCristian Dumitrescu 	struct rte_mempool *mp;
21202d36ef6SCristian Dumitrescu 	char *mempool_name;
21302d36ef6SCristian Dumitrescu 	uint32_t mbuf_private_size, pkt_buffer_size, pool_size, cache_size, numa_node;
2145074e1d5SCristian Dumitrescu 
21502d36ef6SCristian Dumitrescu 	if (n_tokens != 12) {
2165074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2175074e1d5SCristian Dumitrescu 		return;
2185074e1d5SCristian Dumitrescu 	}
2195074e1d5SCristian Dumitrescu 
22002d36ef6SCristian Dumitrescu 	mempool_name = tokens[1];
2215074e1d5SCristian Dumitrescu 
22202d36ef6SCristian Dumitrescu 	if (strcmp(tokens[2], "meta")) {
22302d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meta");
2245074e1d5SCristian Dumitrescu 		return;
2255074e1d5SCristian Dumitrescu 	}
2265074e1d5SCristian Dumitrescu 
22702d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&mbuf_private_size, tokens[3])) {
22802d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "mbuf_private_size");
2295074e1d5SCristian Dumitrescu 		return;
2305074e1d5SCristian Dumitrescu 	}
2315074e1d5SCristian Dumitrescu 
23202d36ef6SCristian Dumitrescu 	if (strcmp(tokens[4], "pkt")) {
23302d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pkt");
23402d36ef6SCristian Dumitrescu 		return;
23502d36ef6SCristian Dumitrescu 	}
23602d36ef6SCristian Dumitrescu 
23702d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&pkt_buffer_size, tokens[5])) {
23802d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pkt_buffer_size");
23902d36ef6SCristian Dumitrescu 		return;
24002d36ef6SCristian Dumitrescu 	}
24102d36ef6SCristian Dumitrescu 
24202d36ef6SCristian Dumitrescu 	if (strcmp(tokens[6], "pool")) {
2435074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
2445074e1d5SCristian Dumitrescu 		return;
2455074e1d5SCristian Dumitrescu 	}
2465074e1d5SCristian Dumitrescu 
24702d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&pool_size, tokens[7])) {
2485074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
2495074e1d5SCristian Dumitrescu 		return;
2505074e1d5SCristian Dumitrescu 	}
2515074e1d5SCristian Dumitrescu 
25202d36ef6SCristian Dumitrescu 	if (strcmp(tokens[8], "cache")) {
2535074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
2545074e1d5SCristian Dumitrescu 		return;
2555074e1d5SCristian Dumitrescu 	}
2565074e1d5SCristian Dumitrescu 
25702d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&cache_size, tokens[9])) {
2585074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
2595074e1d5SCristian Dumitrescu 		return;
2605074e1d5SCristian Dumitrescu 	}
2615074e1d5SCristian Dumitrescu 
26202d36ef6SCristian Dumitrescu 	if (strcmp(tokens[10], "numa")) {
26302d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
2645074e1d5SCristian Dumitrescu 		return;
2655074e1d5SCristian Dumitrescu 	}
2665074e1d5SCristian Dumitrescu 
26702d36ef6SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[11])) {
26802d36ef6SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
2695074e1d5SCristian Dumitrescu 		return;
2705074e1d5SCristian Dumitrescu 	}
2715074e1d5SCristian Dumitrescu 
27202d36ef6SCristian Dumitrescu 	mp = rte_pktmbuf_pool_create(mempool_name,
27302d36ef6SCristian Dumitrescu 				     pool_size,
27402d36ef6SCristian Dumitrescu 				     cache_size,
27502d36ef6SCristian Dumitrescu 				     mbuf_private_size,
27602d36ef6SCristian Dumitrescu 				     pkt_buffer_size,
27702d36ef6SCristian Dumitrescu 				     numa_node);
27802d36ef6SCristian Dumitrescu 	if (!mp) {
2795074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
2805074e1d5SCristian Dumitrescu 		return;
2815074e1d5SCristian Dumitrescu 	}
2825074e1d5SCristian Dumitrescu }
2835074e1d5SCristian Dumitrescu 
284f31c80f8SCristian Dumitrescu static const char cmd_ethdev_help[] =
285f31c80f8SCristian Dumitrescu "ethdev <ethdev_name>\n"
2865074e1d5SCristian Dumitrescu "   rxq <n_queues> <queue_size> <mempool_name>\n"
2875074e1d5SCristian Dumitrescu "   txq <n_queues> <queue_size>\n"
2885074e1d5SCristian Dumitrescu "   promiscuous on | off\n"
2895074e1d5SCristian Dumitrescu "   [rss <qid_0> ... <qid_n>]\n";
2905074e1d5SCristian Dumitrescu 
2915074e1d5SCristian Dumitrescu static void
292f31c80f8SCristian Dumitrescu cmd_ethdev(char **tokens,
2935074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
2945074e1d5SCristian Dumitrescu 	char *out,
2955074e1d5SCristian Dumitrescu 	size_t out_size,
29678dffe31SCristian Dumitrescu 	void *obj __rte_unused)
2975074e1d5SCristian Dumitrescu {
29878dffe31SCristian Dumitrescu 	struct ethdev_params p;
29978dffe31SCristian Dumitrescu 	struct ethdev_params_rss rss;
3005074e1d5SCristian Dumitrescu 	char *name;
30178dffe31SCristian Dumitrescu 	int status;
3025074e1d5SCristian Dumitrescu 
3035074e1d5SCristian Dumitrescu 	memset(&p, 0, sizeof(p));
30478dffe31SCristian Dumitrescu 	memset(&rss, 0, sizeof(rss));
3055074e1d5SCristian Dumitrescu 
30678dffe31SCristian Dumitrescu 	if (n_tokens < 11 || n_tokens > 12 + ETHDEV_RXQ_RSS_MAX) {
3075074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
3085074e1d5SCristian Dumitrescu 		return;
3095074e1d5SCristian Dumitrescu 	}
3105074e1d5SCristian Dumitrescu 	name = tokens[1];
3115074e1d5SCristian Dumitrescu 
312f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[2], "rxq") != 0) {
3135074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rxq");
3145074e1d5SCristian Dumitrescu 		return;
3155074e1d5SCristian Dumitrescu 	}
3165074e1d5SCristian Dumitrescu 
317f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.rx.n_queues, tokens[3]) != 0) {
3185074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
3195074e1d5SCristian Dumitrescu 		return;
3205074e1d5SCristian Dumitrescu 	}
321f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.rx.queue_size, tokens[4]) != 0) {
3225074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
3235074e1d5SCristian Dumitrescu 		return;
3245074e1d5SCristian Dumitrescu 	}
3255074e1d5SCristian Dumitrescu 
326f31c80f8SCristian Dumitrescu 	p.rx.mempool_name = tokens[5];
3275074e1d5SCristian Dumitrescu 
328f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[6], "txq") != 0) {
3295074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "txq");
3305074e1d5SCristian Dumitrescu 		return;
3315074e1d5SCristian Dumitrescu 	}
3325074e1d5SCristian Dumitrescu 
333f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.tx.n_queues, tokens[7]) != 0) {
3345074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queues");
3355074e1d5SCristian Dumitrescu 		return;
3365074e1d5SCristian Dumitrescu 	}
3375074e1d5SCristian Dumitrescu 
338f31c80f8SCristian Dumitrescu 	if (parser_read_uint32(&p.tx.queue_size, tokens[8]) != 0) {
3395074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
3405074e1d5SCristian Dumitrescu 		return;
3415074e1d5SCristian Dumitrescu 	}
3425074e1d5SCristian Dumitrescu 
343f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[9], "promiscuous") != 0) {
3445074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "promiscuous");
3455074e1d5SCristian Dumitrescu 		return;
3465074e1d5SCristian Dumitrescu 	}
3475074e1d5SCristian Dumitrescu 
348f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[10], "on") == 0)
3495074e1d5SCristian Dumitrescu 		p.promiscuous = 1;
350f31c80f8SCristian Dumitrescu 	else if (strcmp(tokens[10], "off") == 0)
3515074e1d5SCristian Dumitrescu 		p.promiscuous = 0;
3525074e1d5SCristian Dumitrescu 	else {
3535074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "on or off");
3545074e1d5SCristian Dumitrescu 		return;
3555074e1d5SCristian Dumitrescu 	}
3565074e1d5SCristian Dumitrescu 
3575074e1d5SCristian Dumitrescu 	/* RSS */
3585074e1d5SCristian Dumitrescu 	p.rx.rss = NULL;
359f31c80f8SCristian Dumitrescu 	if (n_tokens > 11) {
3605074e1d5SCristian Dumitrescu 		uint32_t queue_id, i;
3615074e1d5SCristian Dumitrescu 
362f31c80f8SCristian Dumitrescu 		if (strcmp(tokens[11], "rss") != 0) {
3635074e1d5SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "rss");
3645074e1d5SCristian Dumitrescu 			return;
3655074e1d5SCristian Dumitrescu 		}
3665074e1d5SCristian Dumitrescu 
3675074e1d5SCristian Dumitrescu 		p.rx.rss = &rss;
3685074e1d5SCristian Dumitrescu 
3695074e1d5SCristian Dumitrescu 		rss.n_queues = 0;
370f31c80f8SCristian Dumitrescu 		for (i = 12; i < n_tokens; i++) {
3715074e1d5SCristian Dumitrescu 			if (parser_read_uint32(&queue_id, tokens[i]) != 0) {
3725074e1d5SCristian Dumitrescu 				snprintf(out, out_size, MSG_ARG_INVALID,
3735074e1d5SCristian Dumitrescu 					"queue_id");
3745074e1d5SCristian Dumitrescu 				return;
3755074e1d5SCristian Dumitrescu 			}
3765074e1d5SCristian Dumitrescu 
3775074e1d5SCristian Dumitrescu 			rss.queue_id[rss.n_queues] = queue_id;
3785074e1d5SCristian Dumitrescu 			rss.n_queues++;
3795074e1d5SCristian Dumitrescu 		}
3805074e1d5SCristian Dumitrescu 	}
3815074e1d5SCristian Dumitrescu 
38278dffe31SCristian Dumitrescu 	status = ethdev_config(name, &p);
38378dffe31SCristian Dumitrescu 	if (status) {
3845074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
3855074e1d5SCristian Dumitrescu 		return;
3865074e1d5SCristian Dumitrescu 	}
3875074e1d5SCristian Dumitrescu }
3885074e1d5SCristian Dumitrescu 
3895074e1d5SCristian Dumitrescu static void
39078dffe31SCristian Dumitrescu ethdev_show(uint16_t port_id, char **out, size_t *out_size)
3915074e1d5SCristian Dumitrescu {
39278dffe31SCristian Dumitrescu 	char name[RTE_ETH_NAME_MAX_LEN];
39378dffe31SCristian Dumitrescu 	struct rte_eth_dev_info info;
3945074e1d5SCristian Dumitrescu 	struct rte_eth_stats stats;
39578dffe31SCristian Dumitrescu 	struct rte_ether_addr addr;
39678dffe31SCristian Dumitrescu 	struct rte_eth_link link;
39778dffe31SCristian Dumitrescu 	uint32_t length;
39878dffe31SCristian Dumitrescu 	uint16_t mtu = 0;
3995074e1d5SCristian Dumitrescu 
40078dffe31SCristian Dumitrescu 	if (!rte_eth_dev_is_valid_port(port_id))
4015074e1d5SCristian Dumitrescu 		return;
4025074e1d5SCristian Dumitrescu 
40378dffe31SCristian Dumitrescu 	rte_eth_dev_get_name_by_port(port_id, name);
40478dffe31SCristian Dumitrescu 	rte_eth_dev_info_get(port_id, &info);
40578dffe31SCristian Dumitrescu 	rte_eth_stats_get(port_id, &stats);
40678dffe31SCristian Dumitrescu 	rte_eth_macaddr_get(port_id, &addr);
40778dffe31SCristian Dumitrescu 	rte_eth_link_get(port_id, &link);
40878dffe31SCristian Dumitrescu 	rte_eth_dev_get_mtu(port_id, &mtu);
4095074e1d5SCristian Dumitrescu 
41078dffe31SCristian Dumitrescu 	snprintf(*out, *out_size,
4115074e1d5SCristian Dumitrescu 		 "%s: flags=<%s> mtu %u\n"
412c2c4f87bSAman Deep Singh 		 "\tether " RTE_ETHER_ADDR_PRT_FMT " rxqueues %u txqueues %u\n"
4135074e1d5SCristian Dumitrescu 		 "\tport# %u  speed %s\n"
4145074e1d5SCristian Dumitrescu 		 "\tRX packets %" PRIu64"  bytes %" PRIu64"\n"
4155074e1d5SCristian Dumitrescu 		 "\tRX errors %" PRIu64"  missed %" PRIu64"  no-mbuf %" PRIu64"\n"
4165074e1d5SCristian Dumitrescu 		 "\tTX packets %" PRIu64"  bytes %" PRIu64"\n"
41778dffe31SCristian Dumitrescu 		 "\tTX errors %" PRIu64"\n\n",
41878dffe31SCristian Dumitrescu 		 name,
41978dffe31SCristian Dumitrescu 		 link.link_status ? "UP" : "DOWN",
4205074e1d5SCristian Dumitrescu 		 mtu,
42178dffe31SCristian Dumitrescu 		 RTE_ETHER_ADDR_BYTES(&addr),
42278dffe31SCristian Dumitrescu 		 info.nb_rx_queues,
42378dffe31SCristian Dumitrescu 		 info.nb_tx_queues,
42478dffe31SCristian Dumitrescu 		 port_id,
42578dffe31SCristian Dumitrescu 		 rte_eth_link_speed_to_str(link.link_speed),
4265074e1d5SCristian Dumitrescu 		 stats.ipackets,
4275074e1d5SCristian Dumitrescu 		 stats.ibytes,
4285074e1d5SCristian Dumitrescu 		 stats.ierrors,
4295074e1d5SCristian Dumitrescu 		 stats.imissed,
4305074e1d5SCristian Dumitrescu 		 stats.rx_nombuf,
4315074e1d5SCristian Dumitrescu 		 stats.opackets,
4325074e1d5SCristian Dumitrescu 		 stats.obytes,
4335074e1d5SCristian Dumitrescu 		 stats.oerrors);
43478dffe31SCristian Dumitrescu 
43578dffe31SCristian Dumitrescu 	length = strlen(*out);
43678dffe31SCristian Dumitrescu 	*out_size -= length;
43778dffe31SCristian Dumitrescu 	*out += length;
4385074e1d5SCristian Dumitrescu }
4395074e1d5SCristian Dumitrescu 
44078dffe31SCristian Dumitrescu 
44178dffe31SCristian Dumitrescu static char cmd_ethdev_show_help[] =
44278dffe31SCristian Dumitrescu "ethdev show [ <ethdev_name> ]\n";
44378dffe31SCristian Dumitrescu 
4445074e1d5SCristian Dumitrescu static void
445f31c80f8SCristian Dumitrescu cmd_ethdev_show(char **tokens,
4465074e1d5SCristian Dumitrescu 	      uint32_t n_tokens,
4475074e1d5SCristian Dumitrescu 	      char *out,
4485074e1d5SCristian Dumitrescu 	      size_t out_size,
44978dffe31SCristian Dumitrescu 	      void *obj __rte_unused)
4505074e1d5SCristian Dumitrescu {
45178dffe31SCristian Dumitrescu 	uint16_t port_id;
4525074e1d5SCristian Dumitrescu 
4535074e1d5SCristian Dumitrescu 	if (n_tokens != 2 && n_tokens != 3) {
4545074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
4555074e1d5SCristian Dumitrescu 		return;
4565074e1d5SCristian Dumitrescu 	}
4575074e1d5SCristian Dumitrescu 
45878dffe31SCristian Dumitrescu 	/* Single device. */
45978dffe31SCristian Dumitrescu 	if (n_tokens == 3) {
46078dffe31SCristian Dumitrescu 		int status;
4615074e1d5SCristian Dumitrescu 
46278dffe31SCristian Dumitrescu 		status = rte_eth_dev_get_port_by_name(tokens[2], &port_id);
46378dffe31SCristian Dumitrescu 		if (status)
46478dffe31SCristian Dumitrescu 			snprintf(out, out_size, "Error: Invalid Ethernet device name.\n");
4655074e1d5SCristian Dumitrescu 
46678dffe31SCristian Dumitrescu 		ethdev_show(port_id, &out, &out_size);
4675074e1d5SCristian Dumitrescu 		return;
4685074e1d5SCristian Dumitrescu 	}
46978dffe31SCristian Dumitrescu 
47078dffe31SCristian Dumitrescu 	/*  All devices. */
47178dffe31SCristian Dumitrescu 	for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
47278dffe31SCristian Dumitrescu 		if (rte_eth_dev_is_valid_port(port_id))
47378dffe31SCristian Dumitrescu 			ethdev_show(port_id, &out, &out_size);
4745074e1d5SCristian Dumitrescu }
4755074e1d5SCristian Dumitrescu 
47677a41301SCristian Dumitrescu static const char cmd_ring_help[] =
47777a41301SCristian Dumitrescu "ring <ring_name> size <size> numa <numa_node>\n";
47877a41301SCristian Dumitrescu 
47977a41301SCristian Dumitrescu static void
48077a41301SCristian Dumitrescu cmd_ring(char **tokens,
48177a41301SCristian Dumitrescu 	uint32_t n_tokens,
48277a41301SCristian Dumitrescu 	char *out,
48377a41301SCristian Dumitrescu 	size_t out_size,
484607dd517SCristian Dumitrescu 	void *obj __rte_unused)
48577a41301SCristian Dumitrescu {
486607dd517SCristian Dumitrescu 	struct rte_ring *r;
48777a41301SCristian Dumitrescu 	char *name;
488607dd517SCristian Dumitrescu 	uint32_t size, numa_node;
48977a41301SCristian Dumitrescu 
49077a41301SCristian Dumitrescu 	if (n_tokens != 6) {
49177a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
49277a41301SCristian Dumitrescu 		return;
49377a41301SCristian Dumitrescu 	}
49477a41301SCristian Dumitrescu 
49577a41301SCristian Dumitrescu 	name = tokens[1];
49677a41301SCristian Dumitrescu 
497607dd517SCristian Dumitrescu 	if (strcmp(tokens[2], "size")) {
49877a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "size");
49977a41301SCristian Dumitrescu 		return;
50077a41301SCristian Dumitrescu 	}
50177a41301SCristian Dumitrescu 
502607dd517SCristian Dumitrescu 	if (parser_read_uint32(&size, tokens[3])) {
50377a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "size");
50477a41301SCristian Dumitrescu 		return;
50577a41301SCristian Dumitrescu 	}
50677a41301SCristian Dumitrescu 
507607dd517SCristian Dumitrescu 	if (strcmp(tokens[4], "numa")) {
50877a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
50977a41301SCristian Dumitrescu 		return;
51077a41301SCristian Dumitrescu 	}
51177a41301SCristian Dumitrescu 
512607dd517SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[5])) {
51377a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
51477a41301SCristian Dumitrescu 		return;
51577a41301SCristian Dumitrescu 	}
51677a41301SCristian Dumitrescu 
517607dd517SCristian Dumitrescu 	r = rte_ring_create(
518607dd517SCristian Dumitrescu 		name,
519607dd517SCristian Dumitrescu 		size,
520607dd517SCristian Dumitrescu 		(int)numa_node,
521607dd517SCristian Dumitrescu 		RING_F_SP_ENQ | RING_F_SC_DEQ);
522607dd517SCristian Dumitrescu 	if (!r) {
52377a41301SCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
52477a41301SCristian Dumitrescu 		return;
52577a41301SCristian Dumitrescu 	}
52677a41301SCristian Dumitrescu }
52777a41301SCristian Dumitrescu 
5281b41a527SCristian Dumitrescu static const char cmd_cryptodev_help[] =
5291b41a527SCristian Dumitrescu "cryptodev <cryptodev_name> queues <n_queue_pairs> qsize <queue_size>\n";
5301b41a527SCristian Dumitrescu 
5311b41a527SCristian Dumitrescu static void
5321b41a527SCristian Dumitrescu cmd_cryptodev(char **tokens,
5331b41a527SCristian Dumitrescu 	      uint32_t n_tokens,
5341b41a527SCristian Dumitrescu 	      char *out,
5351b41a527SCristian Dumitrescu 	      size_t out_size,
5361b41a527SCristian Dumitrescu 	      void *obj __rte_unused)
5371b41a527SCristian Dumitrescu {
5381b41a527SCristian Dumitrescu 	struct cryptodev_params params;
5391b41a527SCristian Dumitrescu 	char *cryptodev_name;
5401b41a527SCristian Dumitrescu 	int status;
5411b41a527SCristian Dumitrescu 
5421b41a527SCristian Dumitrescu 	if (n_tokens != 6) {
5431b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
5441b41a527SCristian Dumitrescu 		return;
5451b41a527SCristian Dumitrescu 	}
5461b41a527SCristian Dumitrescu 
5471b41a527SCristian Dumitrescu 	if (strcmp(tokens[0], "cryptodev")) {
5481b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptodev");
5491b41a527SCristian Dumitrescu 		return;
5501b41a527SCristian Dumitrescu 	}
5511b41a527SCristian Dumitrescu 
5521b41a527SCristian Dumitrescu 	cryptodev_name = tokens[1];
5531b41a527SCristian Dumitrescu 
5541b41a527SCristian Dumitrescu 	if (strcmp(tokens[2], "queues")) {
5551b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "queues");
5561b41a527SCristian Dumitrescu 		return;
5571b41a527SCristian Dumitrescu 	}
5581b41a527SCristian Dumitrescu 
5591b41a527SCristian Dumitrescu 	if (parser_read_uint32(&params.n_queue_pairs, tokens[3])) {
5601b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_queue_pairs");
5611b41a527SCristian Dumitrescu 		return;
5621b41a527SCristian Dumitrescu 	}
5631b41a527SCristian Dumitrescu 
5641b41a527SCristian Dumitrescu 	if (strcmp(tokens[4], "qsize")) {
5651b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "qsize");
5661b41a527SCristian Dumitrescu 		return;
5671b41a527SCristian Dumitrescu 	}
5681b41a527SCristian Dumitrescu 
5691b41a527SCristian Dumitrescu 
5701b41a527SCristian Dumitrescu 	if (parser_read_uint32(&params.queue_size, tokens[5])) {
5711b41a527SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "queue_size");
5721b41a527SCristian Dumitrescu 		return;
5731b41a527SCristian Dumitrescu 	}
5741b41a527SCristian Dumitrescu 
5751b41a527SCristian Dumitrescu 	status = cryptodev_config(cryptodev_name, &params);
5761b41a527SCristian Dumitrescu 	if (status)
5771b41a527SCristian Dumitrescu 		snprintf(out, out_size, "Crypto device configuration failed (%d).\n", status);
5781b41a527SCristian Dumitrescu }
5791b41a527SCristian Dumitrescu 
5809043f66aSCristian Dumitrescu static const char cmd_pipeline_codegen_help[] =
5819043f66aSCristian Dumitrescu "pipeline codegen <spec_file> <code_file>\n";
5829043f66aSCristian Dumitrescu 
5839043f66aSCristian Dumitrescu static void
5849043f66aSCristian Dumitrescu cmd_pipeline_codegen(char **tokens,
5859043f66aSCristian Dumitrescu 	uint32_t n_tokens,
5869043f66aSCristian Dumitrescu 	char *out,
5879043f66aSCristian Dumitrescu 	size_t out_size,
5889043f66aSCristian Dumitrescu 	void *obj __rte_unused)
5899043f66aSCristian Dumitrescu {
5909043f66aSCristian Dumitrescu 	FILE *spec_file = NULL;
5919043f66aSCristian Dumitrescu 	FILE *code_file = NULL;
5929043f66aSCristian Dumitrescu 	uint32_t err_line;
5939043f66aSCristian Dumitrescu 	const char *err_msg;
5949043f66aSCristian Dumitrescu 	int status;
5959043f66aSCristian Dumitrescu 
5969043f66aSCristian Dumitrescu 	if (n_tokens != 4) {
5979043f66aSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
5989043f66aSCristian Dumitrescu 		return;
5999043f66aSCristian Dumitrescu 	}
6009043f66aSCristian Dumitrescu 
6019043f66aSCristian Dumitrescu 	spec_file = fopen(tokens[2], "r");
6029043f66aSCristian Dumitrescu 	if (!spec_file) {
6039043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[2]);
6049043f66aSCristian Dumitrescu 		return;
6059043f66aSCristian Dumitrescu 	}
6069043f66aSCristian Dumitrescu 
6079043f66aSCristian Dumitrescu 	code_file = fopen(tokens[3], "w");
6089043f66aSCristian Dumitrescu 	if (!code_file) {
6099043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[3]);
610ab492f94SHarshad Narayane 		fclose(spec_file);
6119043f66aSCristian Dumitrescu 		return;
6129043f66aSCristian Dumitrescu 	}
6139043f66aSCristian Dumitrescu 
6149043f66aSCristian Dumitrescu 	status = rte_swx_pipeline_codegen(spec_file,
6159043f66aSCristian Dumitrescu 					  code_file,
6169043f66aSCristian Dumitrescu 					  &err_line,
6179043f66aSCristian Dumitrescu 					  &err_msg);
6189043f66aSCristian Dumitrescu 
6199043f66aSCristian Dumitrescu 	fclose(spec_file);
6209043f66aSCristian Dumitrescu 	fclose(code_file);
6219043f66aSCristian Dumitrescu 
6229043f66aSCristian Dumitrescu 	if (status) {
6239043f66aSCristian Dumitrescu 		snprintf(out, out_size, "Error %d at line %u: %s\n.",
6249043f66aSCristian Dumitrescu 			status, err_line, err_msg);
6259043f66aSCristian Dumitrescu 		return;
6269043f66aSCristian Dumitrescu 	}
6279043f66aSCristian Dumitrescu }
6286bc14d9fSCristian Dumitrescu 
6296bc14d9fSCristian Dumitrescu static const char cmd_pipeline_libbuild_help[] =
6306bc14d9fSCristian Dumitrescu "pipeline libbuild <code_file> <lib_file>\n";
6316bc14d9fSCristian Dumitrescu 
6326bc14d9fSCristian Dumitrescu static void
6336bc14d9fSCristian Dumitrescu cmd_pipeline_libbuild(char **tokens,
6346bc14d9fSCristian Dumitrescu 	uint32_t n_tokens,
6356bc14d9fSCristian Dumitrescu 	char *out,
6366bc14d9fSCristian Dumitrescu 	size_t out_size,
6376bc14d9fSCristian Dumitrescu 	void *obj __rte_unused)
6386bc14d9fSCristian Dumitrescu {
6396bc14d9fSCristian Dumitrescu 	char *code_file, *lib_file, *obj_file = NULL, *log_file = NULL;
6406bc14d9fSCristian Dumitrescu 	char *install_dir, *cwd = NULL, *buffer = NULL;
6416bc14d9fSCristian Dumitrescu 	size_t length;
6426bc14d9fSCristian Dumitrescu 	int status = 0;
6436bc14d9fSCristian Dumitrescu 
6446bc14d9fSCristian Dumitrescu 	if (n_tokens != 4) {
6456bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
6466bc14d9fSCristian Dumitrescu 		goto free;
6476bc14d9fSCristian Dumitrescu 	}
6486bc14d9fSCristian Dumitrescu 
6496bc14d9fSCristian Dumitrescu 	install_dir = getenv("RTE_INSTALL_DIR");
6506bc14d9fSCristian Dumitrescu 	if (!install_dir) {
6516bc14d9fSCristian Dumitrescu 		cwd = malloc(MAX_LINE_SIZE);
6526bc14d9fSCristian Dumitrescu 		if (!cwd) {
6536bc14d9fSCristian Dumitrescu 			snprintf(out, out_size, MSG_OUT_OF_MEMORY);
6546bc14d9fSCristian Dumitrescu 			goto free;
6556bc14d9fSCristian Dumitrescu 		}
6566bc14d9fSCristian Dumitrescu 
6576bc14d9fSCristian Dumitrescu 		install_dir = getcwd(cwd, MAX_LINE_SIZE);
6586bc14d9fSCristian Dumitrescu 		if (!install_dir) {
6596bc14d9fSCristian Dumitrescu 			snprintf(out, out_size, "Error: Path too long.\n");
6606bc14d9fSCristian Dumitrescu 			goto free;
6616bc14d9fSCristian Dumitrescu 		}
6626bc14d9fSCristian Dumitrescu 	}
6636bc14d9fSCristian Dumitrescu 
6646bc14d9fSCristian Dumitrescu 	snprintf(out, out_size, "Using DPDK source code from \"%s\".\n", install_dir);
6656bc14d9fSCristian Dumitrescu 	out_size -= strlen(out);
6666bc14d9fSCristian Dumitrescu 	out += strlen(out);
6676bc14d9fSCristian Dumitrescu 
6686bc14d9fSCristian Dumitrescu 	code_file = tokens[2];
6696bc14d9fSCristian Dumitrescu 	length = strnlen(code_file, MAX_LINE_SIZE);
6706bc14d9fSCristian Dumitrescu 	if ((length < 3) ||
6716bc14d9fSCristian Dumitrescu 	    (code_file[length - 2] != '.') ||
6726bc14d9fSCristian Dumitrescu 	    (code_file[length - 1] != 'c')) {
6736bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "code_file");
6746bc14d9fSCristian Dumitrescu 		goto free;
6756bc14d9fSCristian Dumitrescu 	}
6766bc14d9fSCristian Dumitrescu 
6776bc14d9fSCristian Dumitrescu 	lib_file = tokens[3];
6786bc14d9fSCristian Dumitrescu 	length = strnlen(lib_file, MAX_LINE_SIZE);
6796bc14d9fSCristian Dumitrescu 	if ((length < 4) ||
6806bc14d9fSCristian Dumitrescu 	    (lib_file[length - 3] != '.') ||
6816bc14d9fSCristian Dumitrescu 	    (lib_file[length - 2] != 's') ||
6826bc14d9fSCristian Dumitrescu 	    (lib_file[length - 1] != 'o')) {
6836bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "lib_file");
6846bc14d9fSCristian Dumitrescu 		goto free;
6856bc14d9fSCristian Dumitrescu 	}
6866bc14d9fSCristian Dumitrescu 
6876bc14d9fSCristian Dumitrescu 	obj_file = malloc(length);
6886bc14d9fSCristian Dumitrescu 	log_file = malloc(length + 2);
6896bc14d9fSCristian Dumitrescu 	if (!obj_file || !log_file) {
6906bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
6916bc14d9fSCristian Dumitrescu 		goto free;
6926bc14d9fSCristian Dumitrescu 	}
6936bc14d9fSCristian Dumitrescu 
6946bc14d9fSCristian Dumitrescu 	memcpy(obj_file, lib_file, length - 2);
6956bc14d9fSCristian Dumitrescu 	obj_file[length - 2] = 'o';
6966bc14d9fSCristian Dumitrescu 	obj_file[length - 1] = 0;
6976bc14d9fSCristian Dumitrescu 
6986bc14d9fSCristian Dumitrescu 	memcpy(log_file, lib_file, length - 2);
6996bc14d9fSCristian Dumitrescu 	log_file[length - 2] = 'l';
7006bc14d9fSCristian Dumitrescu 	log_file[length - 1] = 'o';
7016bc14d9fSCristian Dumitrescu 	log_file[length] = 'g';
7026bc14d9fSCristian Dumitrescu 	log_file[length + 1] = 0;
7036bc14d9fSCristian Dumitrescu 
7046bc14d9fSCristian Dumitrescu 	buffer = malloc(MAX_LINE_SIZE);
7056bc14d9fSCristian Dumitrescu 	if (!buffer) {
7066bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
707b42f3e2fSHarshad Narayane 		goto free;
7086bc14d9fSCristian Dumitrescu 	}
7096bc14d9fSCristian Dumitrescu 
7106bc14d9fSCristian Dumitrescu 	snprintf(buffer,
7116bc14d9fSCristian Dumitrescu 		 MAX_LINE_SIZE,
7126bc14d9fSCristian Dumitrescu 		 "gcc -c -O3 -fpic -Wno-deprecated-declarations -o %s %s "
7136bc14d9fSCristian Dumitrescu 		 "-I %s/lib/pipeline "
7146bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/include "
7156bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/x86/include "
7166bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/include/generic "
7176bc14d9fSCristian Dumitrescu 		 "-I %s/lib/meter "
7186bc14d9fSCristian Dumitrescu 		 "-I %s/lib/port "
7196bc14d9fSCristian Dumitrescu 		 "-I %s/lib/table "
7206bc14d9fSCristian Dumitrescu 		 "-I %s/lib/pipeline "
7216bc14d9fSCristian Dumitrescu 		 "-I %s/config "
7226bc14d9fSCristian Dumitrescu 		 "-I %s/build "
7236bc14d9fSCristian Dumitrescu 		 "-I %s/lib/eal/linux/include "
7246bc14d9fSCristian Dumitrescu 		 ">%s 2>&1 "
7256bc14d9fSCristian Dumitrescu 		 "&& "
7266bc14d9fSCristian Dumitrescu 		 "gcc -shared %s -o %s "
7276bc14d9fSCristian Dumitrescu 		 ">>%s 2>&1",
7286bc14d9fSCristian Dumitrescu 		 obj_file,
7296bc14d9fSCristian Dumitrescu 		 code_file,
7306bc14d9fSCristian Dumitrescu 		 install_dir,
7316bc14d9fSCristian Dumitrescu 		 install_dir,
7326bc14d9fSCristian Dumitrescu 		 install_dir,
7336bc14d9fSCristian Dumitrescu 		 install_dir,
7346bc14d9fSCristian Dumitrescu 		 install_dir,
7356bc14d9fSCristian Dumitrescu 		 install_dir,
7366bc14d9fSCristian Dumitrescu 		 install_dir,
7376bc14d9fSCristian Dumitrescu 		 install_dir,
7386bc14d9fSCristian Dumitrescu 		 install_dir,
7396bc14d9fSCristian Dumitrescu 		 install_dir,
7406bc14d9fSCristian Dumitrescu 		 install_dir,
7416bc14d9fSCristian Dumitrescu 		 log_file,
7426bc14d9fSCristian Dumitrescu 		 obj_file,
7436bc14d9fSCristian Dumitrescu 		 lib_file,
7446bc14d9fSCristian Dumitrescu 		 log_file);
7456bc14d9fSCristian Dumitrescu 
7466bc14d9fSCristian Dumitrescu 	status = system(buffer);
7476bc14d9fSCristian Dumitrescu 	if (status) {
7486bc14d9fSCristian Dumitrescu 		snprintf(out,
7496bc14d9fSCristian Dumitrescu 			 out_size,
7506bc14d9fSCristian Dumitrescu 			 "Library build failed, see file \"%s\" for details.\n",
7516bc14d9fSCristian Dumitrescu 			 log_file);
7526bc14d9fSCristian Dumitrescu 		goto free;
7536bc14d9fSCristian Dumitrescu 	}
7546bc14d9fSCristian Dumitrescu 
7556bc14d9fSCristian Dumitrescu free:
7566bc14d9fSCristian Dumitrescu 	free(cwd);
7576bc14d9fSCristian Dumitrescu 	free(obj_file);
7586bc14d9fSCristian Dumitrescu 	free(log_file);
7596bc14d9fSCristian Dumitrescu 	free(buffer);
7606bc14d9fSCristian Dumitrescu }
7616bc14d9fSCristian Dumitrescu 
7625074e1d5SCristian Dumitrescu static const char cmd_pipeline_build_help[] =
76368b95704SCristian Dumitrescu "pipeline <pipeline_name> build lib <lib_file> io <iospec_file> numa <numa_node>\n";
7645074e1d5SCristian Dumitrescu 
7655074e1d5SCristian Dumitrescu static void
7665074e1d5SCristian Dumitrescu cmd_pipeline_build(char **tokens,
7675074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
7685074e1d5SCristian Dumitrescu 	char *out,
7695074e1d5SCristian Dumitrescu 	size_t out_size,
77068b95704SCristian Dumitrescu 	void *obj __rte_unused)
7715074e1d5SCristian Dumitrescu {
77268b95704SCristian Dumitrescu 	struct rte_swx_pipeline *p = NULL;
77368b95704SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl = NULL;
77468b95704SCristian Dumitrescu 	char *pipeline_name, *lib_file_name, *iospec_file_name;
77568b95704SCristian Dumitrescu 	FILE *iospec_file = NULL;
77668b95704SCristian Dumitrescu 	uint32_t numa_node = 0;
77768b95704SCristian Dumitrescu 	int status = 0;
7785074e1d5SCristian Dumitrescu 
77968b95704SCristian Dumitrescu 	/* Parsing. */
78068b95704SCristian Dumitrescu 	if (n_tokens != 9) {
7815074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
7825074e1d5SCristian Dumitrescu 		return;
7835074e1d5SCristian Dumitrescu 	}
7845074e1d5SCristian Dumitrescu 
78568b95704SCristian Dumitrescu 	pipeline_name = tokens[1];
78668b95704SCristian Dumitrescu 
78768b95704SCristian Dumitrescu 	if (strcmp(tokens[2], "build")) {
78868b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "build");
7895074e1d5SCristian Dumitrescu 		return;
7905074e1d5SCristian Dumitrescu 	}
7915074e1d5SCristian Dumitrescu 
79268b95704SCristian Dumitrescu 	if (strcmp(tokens[3], "lib")) {
79368b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "lib");
7945074e1d5SCristian Dumitrescu 		return;
7955074e1d5SCristian Dumitrescu 	}
7965074e1d5SCristian Dumitrescu 
79768b95704SCristian Dumitrescu 	lib_file_name = tokens[4];
79868b95704SCristian Dumitrescu 
79968b95704SCristian Dumitrescu 	if (strcmp(tokens[5], "io")) {
80068b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "io");
80168b95704SCristian Dumitrescu 		return;
80268b95704SCristian Dumitrescu 	}
80368b95704SCristian Dumitrescu 
80468b95704SCristian Dumitrescu 	iospec_file_name = tokens[6];
80568b95704SCristian Dumitrescu 
80668b95704SCristian Dumitrescu 	if (strcmp(tokens[7], "numa")) {
80768b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
80868b95704SCristian Dumitrescu 		return;
80968b95704SCristian Dumitrescu 	}
81068b95704SCristian Dumitrescu 
81168b95704SCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[8])) {
81268b95704SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
81368b95704SCristian Dumitrescu 		return;
81468b95704SCristian Dumitrescu 	}
81568b95704SCristian Dumitrescu 
81668b95704SCristian Dumitrescu 	/* I/O spec file open. */
81768b95704SCristian Dumitrescu 	iospec_file = fopen(iospec_file_name, "r");
81868b95704SCristian Dumitrescu 	if (!iospec_file) {
81968b95704SCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file \"%s\".\n", iospec_file_name);
82068b95704SCristian Dumitrescu 		return;
82168b95704SCristian Dumitrescu 	}
82268b95704SCristian Dumitrescu 
82368b95704SCristian Dumitrescu 	status = rte_swx_pipeline_build_from_lib(&p,
82468b95704SCristian Dumitrescu 						 pipeline_name,
82568b95704SCristian Dumitrescu 						 lib_file_name,
82668b95704SCristian Dumitrescu 						 iospec_file,
82768b95704SCristian Dumitrescu 						 (int)numa_node);
8285074e1d5SCristian Dumitrescu 	if (status) {
82968b95704SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline build failed (%d).", status);
83068b95704SCristian Dumitrescu 		goto free;
8315074e1d5SCristian Dumitrescu 	}
8325074e1d5SCristian Dumitrescu 
83368b95704SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_create(p);
83468b95704SCristian Dumitrescu 	if (!ctl) {
8355074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline control create failed.");
83668b95704SCristian Dumitrescu 		goto free;
8375074e1d5SCristian Dumitrescu 	}
83868b95704SCristian Dumitrescu 
83968b95704SCristian Dumitrescu free:
84068b95704SCristian Dumitrescu 	if (status)
84168b95704SCristian Dumitrescu 		rte_swx_pipeline_free(p);
84268b95704SCristian Dumitrescu 
84368b95704SCristian Dumitrescu 	if (iospec_file)
84468b95704SCristian Dumitrescu 		fclose(iospec_file);
8455074e1d5SCristian Dumitrescu }
8465074e1d5SCristian Dumitrescu 
84775129cebSChurchill Khangar static int
84875129cebSChurchill Khangar pipeline_table_entries_add(struct rte_swx_ctl_pipeline *p,
84975129cebSChurchill Khangar 			   const char *table_name,
85075129cebSChurchill Khangar 			   FILE *file,
85175129cebSChurchill Khangar 			   uint32_t *file_line_number)
85275129cebSChurchill Khangar {
85375129cebSChurchill Khangar 	char *line = NULL;
85475129cebSChurchill Khangar 	uint32_t line_id = 0;
85575129cebSChurchill Khangar 	int status = 0;
85675129cebSChurchill Khangar 
85775129cebSChurchill Khangar 	/* Buffer allocation. */
85875129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
85975129cebSChurchill Khangar 	if (!line)
86075129cebSChurchill Khangar 		return -ENOMEM;
86175129cebSChurchill Khangar 
86275129cebSChurchill Khangar 	/* File read. */
86375129cebSChurchill Khangar 	for (line_id = 1; ; line_id++) {
86475129cebSChurchill Khangar 		struct rte_swx_table_entry *entry;
86575129cebSChurchill Khangar 		int is_blank_or_comment;
86675129cebSChurchill Khangar 
86775129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
86875129cebSChurchill Khangar 			break;
86975129cebSChurchill Khangar 
87075129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
87175129cebSChurchill Khangar 							      table_name,
87275129cebSChurchill Khangar 							      line,
87375129cebSChurchill Khangar 							      &is_blank_or_comment);
87475129cebSChurchill Khangar 		if (!entry) {
87575129cebSChurchill Khangar 			if (is_blank_or_comment)
87675129cebSChurchill Khangar 				continue;
87775129cebSChurchill Khangar 
87875129cebSChurchill Khangar 			status = -EINVAL;
87975129cebSChurchill Khangar 			goto error;
88075129cebSChurchill Khangar 		}
88175129cebSChurchill Khangar 
88275129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_entry_add(p,
88375129cebSChurchill Khangar 							      table_name,
88475129cebSChurchill Khangar 							      entry);
88575129cebSChurchill Khangar 		table_entry_free(entry);
88675129cebSChurchill Khangar 		if (status)
88775129cebSChurchill Khangar 			goto error;
88875129cebSChurchill Khangar 	}
88975129cebSChurchill Khangar 
89075129cebSChurchill Khangar error:
89175129cebSChurchill Khangar 	free(line);
89275129cebSChurchill Khangar 	*file_line_number = line_id;
89375129cebSChurchill Khangar 	return status;
89475129cebSChurchill Khangar }
89575129cebSChurchill Khangar 
89675129cebSChurchill Khangar static const char cmd_pipeline_table_add_help[] =
89775129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> add <file_name>\n";
8985074e1d5SCristian Dumitrescu 
8995074e1d5SCristian Dumitrescu static void
90075129cebSChurchill Khangar cmd_pipeline_table_add(char **tokens,
9015074e1d5SCristian Dumitrescu 		       uint32_t n_tokens,
9025074e1d5SCristian Dumitrescu 		       char *out,
9035074e1d5SCristian Dumitrescu 		       size_t out_size,
904b9559f94SCristian Dumitrescu 		       void *obj __rte_unused)
9055074e1d5SCristian Dumitrescu {
906b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
90775129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
90875129cebSChurchill Khangar 	FILE *file = NULL;
90975129cebSChurchill Khangar 	uint32_t file_line_number = 0;
9105074e1d5SCristian Dumitrescu 	int status;
9115074e1d5SCristian Dumitrescu 
91275129cebSChurchill Khangar 	if (n_tokens != 6) {
9135074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
9145074e1d5SCristian Dumitrescu 		return;
9155074e1d5SCristian Dumitrescu 	}
9165074e1d5SCristian Dumitrescu 
9175074e1d5SCristian Dumitrescu 	pipeline_name = tokens[1];
918b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
919b9559f94SCristian Dumitrescu 	if (!ctl) {
9205074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
9215074e1d5SCristian Dumitrescu 		return;
9225074e1d5SCristian Dumitrescu 	}
9235074e1d5SCristian Dumitrescu 
92475129cebSChurchill Khangar 	table_name = tokens[3];
92575129cebSChurchill Khangar 
92675129cebSChurchill Khangar 	file_name = tokens[5];
92775129cebSChurchill Khangar 	file = fopen(file_name, "r");
92875129cebSChurchill Khangar 	if (!file) {
92975129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
93075129cebSChurchill Khangar 		return;
93175129cebSChurchill Khangar 	}
93275129cebSChurchill Khangar 
933b9559f94SCristian Dumitrescu 	status = pipeline_table_entries_add(ctl,
93475129cebSChurchill Khangar 					    table_name,
93575129cebSChurchill Khangar 					    file,
93675129cebSChurchill Khangar 					    &file_line_number);
93775129cebSChurchill Khangar 	if (status)
93875129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
93975129cebSChurchill Khangar 			 file_name,
94075129cebSChurchill Khangar 			 file_line_number);
94175129cebSChurchill Khangar 
94275129cebSChurchill Khangar 	fclose(file);
94375129cebSChurchill Khangar }
94475129cebSChurchill Khangar 
94575129cebSChurchill Khangar static int
94675129cebSChurchill Khangar pipeline_table_entries_delete(struct rte_swx_ctl_pipeline *p,
94775129cebSChurchill Khangar 			      const char *table_name,
94875129cebSChurchill Khangar 			      FILE *file,
94975129cebSChurchill Khangar 			      uint32_t *file_line_number)
95075129cebSChurchill Khangar {
95175129cebSChurchill Khangar 	char *line = NULL;
95275129cebSChurchill Khangar 	uint32_t line_id = 0;
95375129cebSChurchill Khangar 	int status = 0;
95475129cebSChurchill Khangar 
95575129cebSChurchill Khangar 	/* Buffer allocation. */
95675129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
95775129cebSChurchill Khangar 	if (!line)
95875129cebSChurchill Khangar 		return -ENOMEM;
95975129cebSChurchill Khangar 
96075129cebSChurchill Khangar 	/* File read. */
96175129cebSChurchill Khangar 	for (line_id = 1; ; line_id++) {
96275129cebSChurchill Khangar 		struct rte_swx_table_entry *entry;
96375129cebSChurchill Khangar 		int is_blank_or_comment;
96475129cebSChurchill Khangar 
96575129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
96675129cebSChurchill Khangar 			break;
96775129cebSChurchill Khangar 
96875129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
96975129cebSChurchill Khangar 							      table_name,
97075129cebSChurchill Khangar 							      line,
97175129cebSChurchill Khangar 							      &is_blank_or_comment);
97275129cebSChurchill Khangar 		if (!entry) {
97375129cebSChurchill Khangar 			if (is_blank_or_comment)
97475129cebSChurchill Khangar 				continue;
97575129cebSChurchill Khangar 
97675129cebSChurchill Khangar 			status = -EINVAL;
97775129cebSChurchill Khangar 			goto error;
97875129cebSChurchill Khangar 		}
97975129cebSChurchill Khangar 
98075129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_entry_delete(p,
98175129cebSChurchill Khangar 								 table_name,
98275129cebSChurchill Khangar 								 entry);
98375129cebSChurchill Khangar 		table_entry_free(entry);
98475129cebSChurchill Khangar 		if (status)
98575129cebSChurchill Khangar 			goto error;
98675129cebSChurchill Khangar 	}
98775129cebSChurchill Khangar 
98875129cebSChurchill Khangar error:
98975129cebSChurchill Khangar 	*file_line_number = line_id;
99075129cebSChurchill Khangar 	free(line);
99175129cebSChurchill Khangar 	return status;
99275129cebSChurchill Khangar }
99375129cebSChurchill Khangar 
99475129cebSChurchill Khangar static const char cmd_pipeline_table_delete_help[] =
99575129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> delete <file_name>\n";
99675129cebSChurchill Khangar 
99775129cebSChurchill Khangar static void
99875129cebSChurchill Khangar cmd_pipeline_table_delete(char **tokens,
99975129cebSChurchill Khangar 			  uint32_t n_tokens,
100075129cebSChurchill Khangar 			  char *out,
100175129cebSChurchill Khangar 			  size_t out_size,
1002b9559f94SCristian Dumitrescu 			  void *obj __rte_unused)
100375129cebSChurchill Khangar {
1004b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
100575129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
100675129cebSChurchill Khangar 	FILE *file = NULL;
100775129cebSChurchill Khangar 	uint32_t file_line_number = 0;
100875129cebSChurchill Khangar 	int status;
100975129cebSChurchill Khangar 
101075129cebSChurchill Khangar 	if (n_tokens != 6) {
101175129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
101275129cebSChurchill Khangar 		return;
101375129cebSChurchill Khangar 	}
101475129cebSChurchill Khangar 
101575129cebSChurchill Khangar 	pipeline_name = tokens[1];
1016b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1017b9559f94SCristian Dumitrescu 	if (!ctl) {
101875129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
10195074e1d5SCristian Dumitrescu 		return;
10205074e1d5SCristian Dumitrescu 	}
10215074e1d5SCristian Dumitrescu 
10225074e1d5SCristian Dumitrescu 	table_name = tokens[3];
10235074e1d5SCristian Dumitrescu 
102475129cebSChurchill Khangar 	file_name = tokens[5];
102575129cebSChurchill Khangar 	file = fopen(file_name, "r");
102675129cebSChurchill Khangar 	if (!file) {
102775129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
10285074e1d5SCristian Dumitrescu 		return;
10295074e1d5SCristian Dumitrescu 	}
10305074e1d5SCristian Dumitrescu 
1031b9559f94SCristian Dumitrescu 	status = pipeline_table_entries_delete(ctl,
103275129cebSChurchill Khangar 					       table_name,
103375129cebSChurchill Khangar 					       file,
103475129cebSChurchill Khangar 					       &file_line_number);
103575129cebSChurchill Khangar 	if (status)
103675129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
103775129cebSChurchill Khangar 			 file_name,
103875129cebSChurchill Khangar 			 file_line_number);
10395074e1d5SCristian Dumitrescu 
104075129cebSChurchill Khangar 	fclose(file);
10415074e1d5SCristian Dumitrescu }
10425074e1d5SCristian Dumitrescu 
104375129cebSChurchill Khangar static int
104475129cebSChurchill Khangar pipeline_table_default_entry_add(struct rte_swx_ctl_pipeline *p,
104575129cebSChurchill Khangar 				 const char *table_name,
104675129cebSChurchill Khangar 				 FILE *file,
104775129cebSChurchill Khangar 				 uint32_t *file_line_number)
104875129cebSChurchill Khangar {
104975129cebSChurchill Khangar 	char *line = NULL;
105075129cebSChurchill Khangar 	uint32_t line_id = 0;
105175129cebSChurchill Khangar 	int status = 0;
10525074e1d5SCristian Dumitrescu 
10535074e1d5SCristian Dumitrescu 	/* Buffer allocation. */
105475129cebSChurchill Khangar 	line = malloc(MAX_LINE_SIZE);
105575129cebSChurchill Khangar 	if (!line)
105675129cebSChurchill Khangar 		return -ENOMEM;
10575074e1d5SCristian Dumitrescu 
105875129cebSChurchill Khangar 	/* File read. */
10595074e1d5SCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
10605074e1d5SCristian Dumitrescu 		struct rte_swx_table_entry *entry;
1061cff9a717SCristian Dumitrescu 		int is_blank_or_comment;
10625074e1d5SCristian Dumitrescu 
106375129cebSChurchill Khangar 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
10645074e1d5SCristian Dumitrescu 			break;
10655074e1d5SCristian Dumitrescu 
106675129cebSChurchill Khangar 		entry = rte_swx_ctl_pipeline_table_entry_read(p,
10675074e1d5SCristian Dumitrescu 							      table_name,
1068cff9a717SCristian Dumitrescu 							      line,
1069cff9a717SCristian Dumitrescu 							      &is_blank_or_comment);
10705074e1d5SCristian Dumitrescu 		if (!entry) {
1071cff9a717SCristian Dumitrescu 			if (is_blank_or_comment)
1072cff9a717SCristian Dumitrescu 				continue;
1073cff9a717SCristian Dumitrescu 
107475129cebSChurchill Khangar 			status = -EINVAL;
10755074e1d5SCristian Dumitrescu 			goto error;
10765074e1d5SCristian Dumitrescu 		}
10775074e1d5SCristian Dumitrescu 
107875129cebSChurchill Khangar 		status = rte_swx_ctl_pipeline_table_default_entry_add(p,
10795074e1d5SCristian Dumitrescu 								      table_name,
10805074e1d5SCristian Dumitrescu 								      entry);
1081275ebefeSCristian Dumitrescu 		table_entry_free(entry);
108275129cebSChurchill Khangar 		if (status)
10835074e1d5SCristian Dumitrescu 			goto error;
10845074e1d5SCristian Dumitrescu 	}
108575129cebSChurchill Khangar 
108675129cebSChurchill Khangar error:
108775129cebSChurchill Khangar 	*file_line_number = line_id;
108875129cebSChurchill Khangar 	free(line);
108975129cebSChurchill Khangar 	return status;
10905074e1d5SCristian Dumitrescu }
10915074e1d5SCristian Dumitrescu 
109275129cebSChurchill Khangar static const char cmd_pipeline_table_default_help[] =
109375129cebSChurchill Khangar "pipeline <pipeline_name> table <table_name> default <file_name>\n";
10945074e1d5SCristian Dumitrescu 
109575129cebSChurchill Khangar static void
109675129cebSChurchill Khangar cmd_pipeline_table_default(char **tokens,
109775129cebSChurchill Khangar 			   uint32_t n_tokens,
109875129cebSChurchill Khangar 			   char *out,
109975129cebSChurchill Khangar 			   size_t out_size,
1100b9559f94SCristian Dumitrescu 			   void *obj __rte_unused)
110175129cebSChurchill Khangar {
1102b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
110375129cebSChurchill Khangar 	char *pipeline_name, *table_name, *file_name;
110475129cebSChurchill Khangar 	FILE *file = NULL;
110575129cebSChurchill Khangar 	uint32_t file_line_number = 0;
110675129cebSChurchill Khangar 	int status;
11075074e1d5SCristian Dumitrescu 
110875129cebSChurchill Khangar 	if (n_tokens != 6) {
110975129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
111075129cebSChurchill Khangar 		return;
111175129cebSChurchill Khangar 	}
11125074e1d5SCristian Dumitrescu 
111375129cebSChurchill Khangar 	pipeline_name = tokens[1];
1114b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1115b9559f94SCristian Dumitrescu 	if (!ctl) {
111675129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
111775129cebSChurchill Khangar 		return;
111875129cebSChurchill Khangar 	}
111975129cebSChurchill Khangar 
112075129cebSChurchill Khangar 	table_name = tokens[3];
112175129cebSChurchill Khangar 
112275129cebSChurchill Khangar 	file_name = tokens[5];
112375129cebSChurchill Khangar 	file = fopen(file_name, "r");
112475129cebSChurchill Khangar 	if (!file) {
112575129cebSChurchill Khangar 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
112675129cebSChurchill Khangar 		return;
112775129cebSChurchill Khangar 	}
112875129cebSChurchill Khangar 
1129b9559f94SCristian Dumitrescu 	status = pipeline_table_default_entry_add(ctl,
11305074e1d5SCristian Dumitrescu 						  table_name,
113175129cebSChurchill Khangar 						  file,
113275129cebSChurchill Khangar 						  &file_line_number);
113375129cebSChurchill Khangar 	if (status)
113475129cebSChurchill Khangar 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
113575129cebSChurchill Khangar 			 file_name,
113675129cebSChurchill Khangar 			 file_line_number);
1137cff9a717SCristian Dumitrescu 
113875129cebSChurchill Khangar 	fclose(file);
11395074e1d5SCristian Dumitrescu }
11405074e1d5SCristian Dumitrescu 
114175129cebSChurchill Khangar static const char cmd_pipeline_table_show_help[] =
1142a4c1146cSCristian Dumitrescu "pipeline <pipeline_name> table <table_name> show [filename]\n";
114375129cebSChurchill Khangar 
114475129cebSChurchill Khangar static void
114575129cebSChurchill Khangar cmd_pipeline_table_show(char **tokens,
114675129cebSChurchill Khangar 	uint32_t n_tokens,
114775129cebSChurchill Khangar 	char *out,
114875129cebSChurchill Khangar 	size_t out_size,
1149b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
115075129cebSChurchill Khangar {
1151b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
115275129cebSChurchill Khangar 	char *pipeline_name, *table_name;
1153a4c1146cSCristian Dumitrescu 	FILE *file = NULL;
115475129cebSChurchill Khangar 	int status;
115575129cebSChurchill Khangar 
1156a4c1146cSCristian Dumitrescu 	if (n_tokens != 5 && n_tokens != 6) {
115775129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
115875129cebSChurchill Khangar 		return;
11595074e1d5SCristian Dumitrescu 	}
11605074e1d5SCristian Dumitrescu 
116175129cebSChurchill Khangar 	pipeline_name = tokens[1];
1162b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1163b9559f94SCristian Dumitrescu 	if (!ctl) {
116475129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
116575129cebSChurchill Khangar 		return;
11665074e1d5SCristian Dumitrescu 	}
11675074e1d5SCristian Dumitrescu 
116875129cebSChurchill Khangar 	table_name = tokens[3];
1169a4c1146cSCristian Dumitrescu 	file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
1170a4c1146cSCristian Dumitrescu 	if (!file) {
1171a4c1146cSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
1172a4c1146cSCristian Dumitrescu 		return;
1173a4c1146cSCristian Dumitrescu 	}
1174a4c1146cSCristian Dumitrescu 
1175b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_table_fprintf(file, ctl, table_name);
117675129cebSChurchill Khangar 	if (status)
117775129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "table_name");
1178a4c1146cSCristian Dumitrescu 
1179a4c1146cSCristian Dumitrescu 	if (file)
1180a4c1146cSCristian Dumitrescu 		fclose(file);
11815074e1d5SCristian Dumitrescu }
118275129cebSChurchill Khangar 
1183598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_add_help[] =
1184598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group add\n";
1185598fe0ddSCristian Dumitrescu 
1186598fe0ddSCristian Dumitrescu static void
1187598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_add(char **tokens,
1188598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1189598fe0ddSCristian Dumitrescu 	char *out,
1190598fe0ddSCristian Dumitrescu 	size_t out_size,
1191b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1192598fe0ddSCristian Dumitrescu {
1193b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1194598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1195598fe0ddSCristian Dumitrescu 	uint32_t group_id;
1196598fe0ddSCristian Dumitrescu 	int status;
1197598fe0ddSCristian Dumitrescu 
1198598fe0ddSCristian Dumitrescu 	if (n_tokens != 6) {
1199598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1200598fe0ddSCristian Dumitrescu 		return;
1201598fe0ddSCristian Dumitrescu 	}
1202598fe0ddSCristian Dumitrescu 
1203598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1204b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1205b9559f94SCristian Dumitrescu 	if (!ctl) {
1206598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1207598fe0ddSCristian Dumitrescu 		return;
1208598fe0ddSCristian Dumitrescu 	}
1209598fe0ddSCristian Dumitrescu 
1210598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1211598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1212598fe0ddSCristian Dumitrescu 		return;
1213598fe0ddSCristian Dumitrescu 	}
1214598fe0ddSCristian Dumitrescu 
1215598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1216598fe0ddSCristian Dumitrescu 
1217598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1218598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "add")) {
1219598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group add");
1220598fe0ddSCristian Dumitrescu 		return;
1221598fe0ddSCristian Dumitrescu 	}
1222598fe0ddSCristian Dumitrescu 
1223b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_group_add(ctl,
1224598fe0ddSCristian Dumitrescu 		selector_name,
1225598fe0ddSCristian Dumitrescu 		&group_id);
1226598fe0ddSCristian Dumitrescu 	if (status)
1227598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
1228598fe0ddSCristian Dumitrescu 	else
1229598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Group ID: %u\n", group_id);
1230598fe0ddSCristian Dumitrescu }
1231598fe0ddSCristian Dumitrescu 
1232598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_delete_help[] =
1233598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group delete <group_id>\n";
1234598fe0ddSCristian Dumitrescu 
1235598fe0ddSCristian Dumitrescu static void
1236598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_delete(char **tokens,
1237598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1238598fe0ddSCristian Dumitrescu 	char *out,
1239598fe0ddSCristian Dumitrescu 	size_t out_size,
1240b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1241598fe0ddSCristian Dumitrescu {
1242b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1243598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1244598fe0ddSCristian Dumitrescu 	uint32_t group_id;
1245598fe0ddSCristian Dumitrescu 	int status;
1246598fe0ddSCristian Dumitrescu 
1247598fe0ddSCristian Dumitrescu 	if (n_tokens != 7) {
1248598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1249598fe0ddSCristian Dumitrescu 		return;
1250598fe0ddSCristian Dumitrescu 	}
1251598fe0ddSCristian Dumitrescu 
1252598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1253b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1254b9559f94SCristian Dumitrescu 	if (!ctl) {
1255598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1256598fe0ddSCristian Dumitrescu 		return;
1257598fe0ddSCristian Dumitrescu 	}
1258598fe0ddSCristian Dumitrescu 
1259598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1260598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1261598fe0ddSCristian Dumitrescu 		return;
1262598fe0ddSCristian Dumitrescu 	}
1263598fe0ddSCristian Dumitrescu 
1264598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1265598fe0ddSCristian Dumitrescu 
1266598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1267598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "delete")) {
1268598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group delete");
1269598fe0ddSCristian Dumitrescu 		return;
1270598fe0ddSCristian Dumitrescu 	}
1271598fe0ddSCristian Dumitrescu 
1272598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&group_id, tokens[6]) != 0) {
1273598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "group_id");
1274598fe0ddSCristian Dumitrescu 		return;
1275598fe0ddSCristian Dumitrescu 	}
1276598fe0ddSCristian Dumitrescu 
1277b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_group_delete(ctl,
1278598fe0ddSCristian Dumitrescu 		selector_name,
1279598fe0ddSCristian Dumitrescu 		group_id);
1280598fe0ddSCristian Dumitrescu 	if (status)
1281598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
1282598fe0ddSCristian Dumitrescu }
1283598fe0ddSCristian Dumitrescu 
1284598fe0ddSCristian Dumitrescu #define GROUP_MEMBER_INFO_TOKENS_MAX 6
1285598fe0ddSCristian Dumitrescu 
1286598fe0ddSCristian Dumitrescu static int
1287598fe0ddSCristian Dumitrescu token_is_comment(const char *token)
1288598fe0ddSCristian Dumitrescu {
1289598fe0ddSCristian Dumitrescu 	if ((token[0] == '#') ||
1290598fe0ddSCristian Dumitrescu 	    (token[0] == ';') ||
1291598fe0ddSCristian Dumitrescu 	    ((token[0] == '/') && (token[1] == '/')))
1292598fe0ddSCristian Dumitrescu 		return 1; /* TRUE. */
1293598fe0ddSCristian Dumitrescu 
1294598fe0ddSCristian Dumitrescu 	return 0; /* FALSE. */
1295598fe0ddSCristian Dumitrescu }
1296598fe0ddSCristian Dumitrescu 
1297598fe0ddSCristian Dumitrescu static int
1298598fe0ddSCristian Dumitrescu pipeline_selector_group_member_read(const char *string,
1299598fe0ddSCristian Dumitrescu 				      uint32_t *group_id,
1300598fe0ddSCristian Dumitrescu 				      uint32_t *member_id,
1301598fe0ddSCristian Dumitrescu 				      uint32_t *weight,
1302598fe0ddSCristian Dumitrescu 				      int *is_blank_or_comment)
1303598fe0ddSCristian Dumitrescu {
1304598fe0ddSCristian Dumitrescu 	char *token_array[GROUP_MEMBER_INFO_TOKENS_MAX], **tokens;
1305598fe0ddSCristian Dumitrescu 	char *s0 = NULL, *s;
130600b67591SAli Alnubani 	uint32_t n_tokens = 0, group_id_val = 0, member_id_val = 0, weight_val = 0;
1307598fe0ddSCristian Dumitrescu 	int blank_or_comment = 0;
1308598fe0ddSCristian Dumitrescu 
1309598fe0ddSCristian Dumitrescu 	/* Check input arguments. */
1310598fe0ddSCristian Dumitrescu 	if (!string || !string[0])
1311598fe0ddSCristian Dumitrescu 		goto error;
1312598fe0ddSCristian Dumitrescu 
1313598fe0ddSCristian Dumitrescu 	/* Memory allocation. */
1314598fe0ddSCristian Dumitrescu 	s0 = strdup(string);
1315598fe0ddSCristian Dumitrescu 	if (!s0)
1316598fe0ddSCristian Dumitrescu 		goto error;
1317598fe0ddSCristian Dumitrescu 
1318598fe0ddSCristian Dumitrescu 	/* Parse the string into tokens. */
1319598fe0ddSCristian Dumitrescu 	for (s = s0; ; ) {
1320598fe0ddSCristian Dumitrescu 		char *token;
1321598fe0ddSCristian Dumitrescu 
1322598fe0ddSCristian Dumitrescu 		token = strtok_r(s, " \f\n\r\t\v", &s);
1323598fe0ddSCristian Dumitrescu 		if (!token || token_is_comment(token))
1324598fe0ddSCristian Dumitrescu 			break;
1325598fe0ddSCristian Dumitrescu 
1326cfcc7bf8SCristian Dumitrescu 		if (n_tokens >= GROUP_MEMBER_INFO_TOKENS_MAX)
1327598fe0ddSCristian Dumitrescu 			goto error;
1328598fe0ddSCristian Dumitrescu 
1329598fe0ddSCristian Dumitrescu 		token_array[n_tokens] = token;
1330598fe0ddSCristian Dumitrescu 		n_tokens++;
1331598fe0ddSCristian Dumitrescu 	}
1332598fe0ddSCristian Dumitrescu 
1333598fe0ddSCristian Dumitrescu 	if (!n_tokens) {
1334598fe0ddSCristian Dumitrescu 		blank_or_comment = 1;
1335598fe0ddSCristian Dumitrescu 		goto error;
1336598fe0ddSCristian Dumitrescu 	}
1337598fe0ddSCristian Dumitrescu 
1338598fe0ddSCristian Dumitrescu 	tokens = token_array;
1339598fe0ddSCristian Dumitrescu 
1340598fe0ddSCristian Dumitrescu 	if (n_tokens < 4 ||
1341598fe0ddSCristian Dumitrescu 		strcmp(tokens[0], "group") ||
1342598fe0ddSCristian Dumitrescu 		strcmp(tokens[2], "member"))
1343598fe0ddSCristian Dumitrescu 		goto error;
1344598fe0ddSCristian Dumitrescu 
1345598fe0ddSCristian Dumitrescu 	/*
1346598fe0ddSCristian Dumitrescu 	 * Group ID.
1347598fe0ddSCristian Dumitrescu 	 */
1348598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&group_id_val, tokens[1]) != 0)
1349598fe0ddSCristian Dumitrescu 		goto error;
1350598fe0ddSCristian Dumitrescu 	*group_id = group_id_val;
1351598fe0ddSCristian Dumitrescu 
1352598fe0ddSCristian Dumitrescu 	/*
1353598fe0ddSCristian Dumitrescu 	 * Member ID.
1354598fe0ddSCristian Dumitrescu 	 */
1355598fe0ddSCristian Dumitrescu 	if (parser_read_uint32(&member_id_val, tokens[3]) != 0)
1356598fe0ddSCristian Dumitrescu 		goto error;
1357598fe0ddSCristian Dumitrescu 	*member_id = member_id_val;
1358598fe0ddSCristian Dumitrescu 
1359598fe0ddSCristian Dumitrescu 	tokens += 4;
1360598fe0ddSCristian Dumitrescu 	n_tokens -= 4;
1361598fe0ddSCristian Dumitrescu 
1362598fe0ddSCristian Dumitrescu 	/*
1363598fe0ddSCristian Dumitrescu 	 * Weight.
1364598fe0ddSCristian Dumitrescu 	 */
1365598fe0ddSCristian Dumitrescu 	if (n_tokens && !strcmp(tokens[0], "weight")) {
1366598fe0ddSCristian Dumitrescu 		if (n_tokens < 2)
1367598fe0ddSCristian Dumitrescu 			goto error;
1368598fe0ddSCristian Dumitrescu 
1369598fe0ddSCristian Dumitrescu 		if (parser_read_uint32(&weight_val, tokens[1]) != 0)
1370598fe0ddSCristian Dumitrescu 			goto error;
1371598fe0ddSCristian Dumitrescu 		*weight = weight_val;
1372598fe0ddSCristian Dumitrescu 
1373598fe0ddSCristian Dumitrescu 		tokens += 2;
1374598fe0ddSCristian Dumitrescu 		n_tokens -= 2;
1375598fe0ddSCristian Dumitrescu 	}
1376598fe0ddSCristian Dumitrescu 
1377598fe0ddSCristian Dumitrescu 	if (n_tokens)
1378598fe0ddSCristian Dumitrescu 		goto error;
1379598fe0ddSCristian Dumitrescu 
1380598fe0ddSCristian Dumitrescu 	free(s0);
1381598fe0ddSCristian Dumitrescu 	return 0;
1382598fe0ddSCristian Dumitrescu 
1383598fe0ddSCristian Dumitrescu error:
1384598fe0ddSCristian Dumitrescu 	free(s0);
1385598fe0ddSCristian Dumitrescu 	if (is_blank_or_comment)
1386598fe0ddSCristian Dumitrescu 		*is_blank_or_comment = blank_or_comment;
1387598fe0ddSCristian Dumitrescu 	return -EINVAL;
1388598fe0ddSCristian Dumitrescu }
1389598fe0ddSCristian Dumitrescu 
1390598fe0ddSCristian Dumitrescu static int
1391598fe0ddSCristian Dumitrescu pipeline_selector_group_members_add(struct rte_swx_ctl_pipeline *p,
1392598fe0ddSCristian Dumitrescu 			   const char *selector_name,
1393598fe0ddSCristian Dumitrescu 			   FILE *file,
1394598fe0ddSCristian Dumitrescu 			   uint32_t *file_line_number)
1395598fe0ddSCristian Dumitrescu {
1396598fe0ddSCristian Dumitrescu 	char *line = NULL;
1397598fe0ddSCristian Dumitrescu 	uint32_t line_id = 0;
1398598fe0ddSCristian Dumitrescu 	int status = 0;
1399598fe0ddSCristian Dumitrescu 
1400598fe0ddSCristian Dumitrescu 	/* Buffer allocation. */
1401598fe0ddSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
1402598fe0ddSCristian Dumitrescu 	if (!line)
1403598fe0ddSCristian Dumitrescu 		return -ENOMEM;
1404598fe0ddSCristian Dumitrescu 
1405598fe0ddSCristian Dumitrescu 	/* File read. */
1406598fe0ddSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
1407598fe0ddSCristian Dumitrescu 		uint32_t group_id, member_id, weight;
1408598fe0ddSCristian Dumitrescu 		int is_blank_or_comment;
1409598fe0ddSCristian Dumitrescu 
1410598fe0ddSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
1411598fe0ddSCristian Dumitrescu 			break;
1412598fe0ddSCristian Dumitrescu 
1413598fe0ddSCristian Dumitrescu 		status = pipeline_selector_group_member_read(line,
1414598fe0ddSCristian Dumitrescu 							      &group_id,
1415598fe0ddSCristian Dumitrescu 							      &member_id,
1416598fe0ddSCristian Dumitrescu 							      &weight,
1417598fe0ddSCristian Dumitrescu 							      &is_blank_or_comment);
1418598fe0ddSCristian Dumitrescu 		if (status) {
1419598fe0ddSCristian Dumitrescu 			if (is_blank_or_comment)
1420598fe0ddSCristian Dumitrescu 				continue;
1421598fe0ddSCristian Dumitrescu 
1422598fe0ddSCristian Dumitrescu 			goto error;
1423598fe0ddSCristian Dumitrescu 		}
1424598fe0ddSCristian Dumitrescu 
1425598fe0ddSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_selector_group_member_add(p,
1426598fe0ddSCristian Dumitrescu 			selector_name,
1427598fe0ddSCristian Dumitrescu 			group_id,
1428598fe0ddSCristian Dumitrescu 			member_id,
1429598fe0ddSCristian Dumitrescu 			weight);
1430598fe0ddSCristian Dumitrescu 		if (status)
1431598fe0ddSCristian Dumitrescu 			goto error;
1432598fe0ddSCristian Dumitrescu 	}
1433598fe0ddSCristian Dumitrescu 
1434598fe0ddSCristian Dumitrescu error:
1435598fe0ddSCristian Dumitrescu 	free(line);
1436598fe0ddSCristian Dumitrescu 	*file_line_number = line_id;
1437598fe0ddSCristian Dumitrescu 	return status;
1438598fe0ddSCristian Dumitrescu }
1439598fe0ddSCristian Dumitrescu 
1440598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_member_add_help[] =
1441598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group member add <file_name>";
1442598fe0ddSCristian Dumitrescu 
1443598fe0ddSCristian Dumitrescu static void
1444598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_member_add(char **tokens,
1445598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1446598fe0ddSCristian Dumitrescu 	char *out,
1447598fe0ddSCristian Dumitrescu 	size_t out_size,
1448b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1449598fe0ddSCristian Dumitrescu {
1450b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1451598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name, *file_name;
1452598fe0ddSCristian Dumitrescu 	FILE *file = NULL;
1453598fe0ddSCristian Dumitrescu 	uint32_t file_line_number = 0;
1454598fe0ddSCristian Dumitrescu 	int status;
1455598fe0ddSCristian Dumitrescu 
1456598fe0ddSCristian Dumitrescu 	if (n_tokens != 8) {
1457598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1458598fe0ddSCristian Dumitrescu 		return;
1459598fe0ddSCristian Dumitrescu 	}
1460598fe0ddSCristian Dumitrescu 
1461598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1462b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1463b9559f94SCristian Dumitrescu 	if (!ctl) {
1464598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1465598fe0ddSCristian Dumitrescu 		return;
1466598fe0ddSCristian Dumitrescu 	}
1467598fe0ddSCristian Dumitrescu 
1468598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1469598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1470598fe0ddSCristian Dumitrescu 		return;
1471598fe0ddSCristian Dumitrescu 	}
1472598fe0ddSCristian Dumitrescu 
1473598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1474598fe0ddSCristian Dumitrescu 
1475598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1476598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "member") ||
1477598fe0ddSCristian Dumitrescu 		strcmp(tokens[6], "add")) {
1478598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group member add");
1479598fe0ddSCristian Dumitrescu 		return;
1480598fe0ddSCristian Dumitrescu 	}
1481598fe0ddSCristian Dumitrescu 
1482598fe0ddSCristian Dumitrescu 	file_name = tokens[7];
1483598fe0ddSCristian Dumitrescu 	file = fopen(file_name, "r");
1484598fe0ddSCristian Dumitrescu 	if (!file) {
1485598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
1486598fe0ddSCristian Dumitrescu 		return;
1487598fe0ddSCristian Dumitrescu 	}
1488598fe0ddSCristian Dumitrescu 
1489b9559f94SCristian Dumitrescu 	status = pipeline_selector_group_members_add(ctl,
1490598fe0ddSCristian Dumitrescu 					    selector_name,
1491598fe0ddSCristian Dumitrescu 					    file,
1492598fe0ddSCristian Dumitrescu 					    &file_line_number);
1493598fe0ddSCristian Dumitrescu 	if (status)
1494598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
1495598fe0ddSCristian Dumitrescu 			 file_name,
1496598fe0ddSCristian Dumitrescu 			 file_line_number);
1497598fe0ddSCristian Dumitrescu 
1498598fe0ddSCristian Dumitrescu 	fclose(file);
1499598fe0ddSCristian Dumitrescu }
1500598fe0ddSCristian Dumitrescu 
1501598fe0ddSCristian Dumitrescu static int
1502598fe0ddSCristian Dumitrescu pipeline_selector_group_members_delete(struct rte_swx_ctl_pipeline *p,
1503598fe0ddSCristian Dumitrescu 			   const char *selector_name,
1504598fe0ddSCristian Dumitrescu 			   FILE *file,
1505598fe0ddSCristian Dumitrescu 			   uint32_t *file_line_number)
1506598fe0ddSCristian Dumitrescu {
1507598fe0ddSCristian Dumitrescu 	char *line = NULL;
1508598fe0ddSCristian Dumitrescu 	uint32_t line_id = 0;
1509598fe0ddSCristian Dumitrescu 	int status = 0;
1510598fe0ddSCristian Dumitrescu 
1511598fe0ddSCristian Dumitrescu 	/* Buffer allocation. */
1512598fe0ddSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
1513598fe0ddSCristian Dumitrescu 	if (!line)
1514598fe0ddSCristian Dumitrescu 		return -ENOMEM;
1515598fe0ddSCristian Dumitrescu 
1516598fe0ddSCristian Dumitrescu 	/* File read. */
1517598fe0ddSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
1518598fe0ddSCristian Dumitrescu 		uint32_t group_id, member_id, weight;
1519598fe0ddSCristian Dumitrescu 		int is_blank_or_comment;
1520598fe0ddSCristian Dumitrescu 
1521598fe0ddSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
1522598fe0ddSCristian Dumitrescu 			break;
1523598fe0ddSCristian Dumitrescu 
1524598fe0ddSCristian Dumitrescu 		status = pipeline_selector_group_member_read(line,
1525598fe0ddSCristian Dumitrescu 							      &group_id,
1526598fe0ddSCristian Dumitrescu 							      &member_id,
1527598fe0ddSCristian Dumitrescu 							      &weight,
1528598fe0ddSCristian Dumitrescu 							      &is_blank_or_comment);
1529598fe0ddSCristian Dumitrescu 		if (status) {
1530598fe0ddSCristian Dumitrescu 			if (is_blank_or_comment)
1531598fe0ddSCristian Dumitrescu 				continue;
1532598fe0ddSCristian Dumitrescu 
1533598fe0ddSCristian Dumitrescu 			goto error;
1534598fe0ddSCristian Dumitrescu 		}
1535598fe0ddSCristian Dumitrescu 
1536598fe0ddSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_selector_group_member_delete(p,
1537598fe0ddSCristian Dumitrescu 			selector_name,
1538598fe0ddSCristian Dumitrescu 			group_id,
1539598fe0ddSCristian Dumitrescu 			member_id);
1540598fe0ddSCristian Dumitrescu 		if (status)
1541598fe0ddSCristian Dumitrescu 			goto error;
1542598fe0ddSCristian Dumitrescu 	}
1543598fe0ddSCristian Dumitrescu 
1544598fe0ddSCristian Dumitrescu error:
1545598fe0ddSCristian Dumitrescu 	free(line);
1546598fe0ddSCristian Dumitrescu 	*file_line_number = line_id;
1547598fe0ddSCristian Dumitrescu 	return status;
1548598fe0ddSCristian Dumitrescu }
1549598fe0ddSCristian Dumitrescu 
1550598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_group_member_delete_help[] =
1551598fe0ddSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> group member delete <file_name>";
1552598fe0ddSCristian Dumitrescu 
1553598fe0ddSCristian Dumitrescu static void
1554598fe0ddSCristian Dumitrescu cmd_pipeline_selector_group_member_delete(char **tokens,
1555598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1556598fe0ddSCristian Dumitrescu 	char *out,
1557598fe0ddSCristian Dumitrescu 	size_t out_size,
1558b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1559598fe0ddSCristian Dumitrescu {
1560b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1561598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name, *file_name;
1562598fe0ddSCristian Dumitrescu 	FILE *file = NULL;
1563598fe0ddSCristian Dumitrescu 	uint32_t file_line_number = 0;
1564598fe0ddSCristian Dumitrescu 	int status;
1565598fe0ddSCristian Dumitrescu 
1566598fe0ddSCristian Dumitrescu 	if (n_tokens != 8) {
1567598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1568598fe0ddSCristian Dumitrescu 		return;
1569598fe0ddSCristian Dumitrescu 	}
1570598fe0ddSCristian Dumitrescu 
1571598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1572b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1573b9559f94SCristian Dumitrescu 	if (!ctl) {
1574598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1575598fe0ddSCristian Dumitrescu 		return;
1576598fe0ddSCristian Dumitrescu 	}
1577598fe0ddSCristian Dumitrescu 
1578598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[2], "selector") != 0) {
1579598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "selector");
1580598fe0ddSCristian Dumitrescu 		return;
1581598fe0ddSCristian Dumitrescu 	}
1582598fe0ddSCristian Dumitrescu 
1583598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1584598fe0ddSCristian Dumitrescu 
1585598fe0ddSCristian Dumitrescu 	if (strcmp(tokens[4], "group") ||
1586598fe0ddSCristian Dumitrescu 		strcmp(tokens[5], "member") ||
1587598fe0ddSCristian Dumitrescu 		strcmp(tokens[6], "delete")) {
1588598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "group member delete");
1589598fe0ddSCristian Dumitrescu 		return;
1590598fe0ddSCristian Dumitrescu 	}
1591598fe0ddSCristian Dumitrescu 
1592598fe0ddSCristian Dumitrescu 	file_name = tokens[7];
1593598fe0ddSCristian Dumitrescu 	file = fopen(file_name, "r");
1594598fe0ddSCristian Dumitrescu 	if (!file) {
1595598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
1596598fe0ddSCristian Dumitrescu 		return;
1597598fe0ddSCristian Dumitrescu 	}
1598598fe0ddSCristian Dumitrescu 
1599b9559f94SCristian Dumitrescu 	status = pipeline_selector_group_members_delete(ctl,
1600598fe0ddSCristian Dumitrescu 					    selector_name,
1601598fe0ddSCristian Dumitrescu 					    file,
1602598fe0ddSCristian Dumitrescu 					    &file_line_number);
1603598fe0ddSCristian Dumitrescu 	if (status)
1604598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
1605598fe0ddSCristian Dumitrescu 			 file_name,
1606598fe0ddSCristian Dumitrescu 			 file_line_number);
1607598fe0ddSCristian Dumitrescu 
1608598fe0ddSCristian Dumitrescu 	fclose(file);
1609598fe0ddSCristian Dumitrescu }
1610598fe0ddSCristian Dumitrescu 
1611598fe0ddSCristian Dumitrescu static const char cmd_pipeline_selector_show_help[] =
1612a4c1146cSCristian Dumitrescu "pipeline <pipeline_name> selector <selector_name> show [filename]\n";
1613598fe0ddSCristian Dumitrescu 
1614598fe0ddSCristian Dumitrescu static void
1615598fe0ddSCristian Dumitrescu cmd_pipeline_selector_show(char **tokens,
1616598fe0ddSCristian Dumitrescu 	uint32_t n_tokens,
1617598fe0ddSCristian Dumitrescu 	char *out,
1618598fe0ddSCristian Dumitrescu 	size_t out_size,
1619b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
1620598fe0ddSCristian Dumitrescu {
1621b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
1622598fe0ddSCristian Dumitrescu 	char *pipeline_name, *selector_name;
1623a4c1146cSCristian Dumitrescu 	FILE *file = NULL;
1624598fe0ddSCristian Dumitrescu 	int status;
1625598fe0ddSCristian Dumitrescu 
1626a4c1146cSCristian Dumitrescu 	if (n_tokens != 5 && n_tokens != 6) {
1627598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
1628598fe0ddSCristian Dumitrescu 		return;
1629598fe0ddSCristian Dumitrescu 	}
1630598fe0ddSCristian Dumitrescu 
1631598fe0ddSCristian Dumitrescu 	pipeline_name = tokens[1];
1632b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1633b9559f94SCristian Dumitrescu 	if (!ctl) {
1634598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
1635598fe0ddSCristian Dumitrescu 		return;
1636598fe0ddSCristian Dumitrescu 	}
1637598fe0ddSCristian Dumitrescu 
1638598fe0ddSCristian Dumitrescu 	selector_name = tokens[3];
1639a4c1146cSCristian Dumitrescu 
1640a4c1146cSCristian Dumitrescu 	file = (n_tokens == 6) ? fopen(tokens[5], "w") : stdout;
1641a4c1146cSCristian Dumitrescu 	if (!file) {
1642a4c1146cSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", tokens[5]);
1643a4c1146cSCristian Dumitrescu 		return;
1644a4c1146cSCristian Dumitrescu 	}
1645a4c1146cSCristian Dumitrescu 
1646b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_selector_fprintf(file, ctl, selector_name);
1647598fe0ddSCristian Dumitrescu 	if (status)
1648598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "selector_name");
1649a4c1146cSCristian Dumitrescu 
1650a4c1146cSCristian Dumitrescu 	if (file)
1651a4c1146cSCristian Dumitrescu 		fclose(file);
1652598fe0ddSCristian Dumitrescu }
1653598fe0ddSCristian Dumitrescu 
16548bd4862fSCristian Dumitrescu static int
16558bd4862fSCristian Dumitrescu pipeline_learner_default_entry_add(struct rte_swx_ctl_pipeline *p,
16568bd4862fSCristian Dumitrescu 				   const char *learner_name,
16578bd4862fSCristian Dumitrescu 				   FILE *file,
16588bd4862fSCristian Dumitrescu 				   uint32_t *file_line_number)
16598bd4862fSCristian Dumitrescu {
16608bd4862fSCristian Dumitrescu 	char *line = NULL;
16618bd4862fSCristian Dumitrescu 	uint32_t line_id = 0;
16628bd4862fSCristian Dumitrescu 	int status = 0;
16638bd4862fSCristian Dumitrescu 
16648bd4862fSCristian Dumitrescu 	/* Buffer allocation. */
16658bd4862fSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
16668bd4862fSCristian Dumitrescu 	if (!line)
16678bd4862fSCristian Dumitrescu 		return -ENOMEM;
16688bd4862fSCristian Dumitrescu 
16698bd4862fSCristian Dumitrescu 	/* File read. */
16708bd4862fSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
16718bd4862fSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
16728bd4862fSCristian Dumitrescu 		int is_blank_or_comment;
16738bd4862fSCristian Dumitrescu 
16748bd4862fSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
16758bd4862fSCristian Dumitrescu 			break;
16768bd4862fSCristian Dumitrescu 
16778bd4862fSCristian Dumitrescu 		entry = rte_swx_ctl_pipeline_learner_default_entry_read(p,
16788bd4862fSCristian Dumitrescu 									learner_name,
16798bd4862fSCristian Dumitrescu 									line,
16808bd4862fSCristian Dumitrescu 									&is_blank_or_comment);
16818bd4862fSCristian Dumitrescu 		if (!entry) {
16828bd4862fSCristian Dumitrescu 			if (is_blank_or_comment)
16838bd4862fSCristian Dumitrescu 				continue;
16848bd4862fSCristian Dumitrescu 
16858bd4862fSCristian Dumitrescu 			status = -EINVAL;
16868bd4862fSCristian Dumitrescu 			goto error;
16878bd4862fSCristian Dumitrescu 		}
16888bd4862fSCristian Dumitrescu 
16898bd4862fSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_learner_default_entry_add(p,
16908bd4862fSCristian Dumitrescu 									learner_name,
16918bd4862fSCristian Dumitrescu 									entry);
16928bd4862fSCristian Dumitrescu 		table_entry_free(entry);
16938bd4862fSCristian Dumitrescu 		if (status)
16948bd4862fSCristian Dumitrescu 			goto error;
16958bd4862fSCristian Dumitrescu 	}
16968bd4862fSCristian Dumitrescu 
16978bd4862fSCristian Dumitrescu error:
16988bd4862fSCristian Dumitrescu 	*file_line_number = line_id;
16998bd4862fSCristian Dumitrescu 	free(line);
17008bd4862fSCristian Dumitrescu 	return status;
17018bd4862fSCristian Dumitrescu }
17028bd4862fSCristian Dumitrescu 
17038bd4862fSCristian Dumitrescu static const char cmd_pipeline_learner_default_help[] =
17048bd4862fSCristian Dumitrescu "pipeline <pipeline_name> learner <learner_name> default <file_name>\n";
17058bd4862fSCristian Dumitrescu 
17068bd4862fSCristian Dumitrescu static void
17078bd4862fSCristian Dumitrescu cmd_pipeline_learner_default(char **tokens,
17088bd4862fSCristian Dumitrescu 			     uint32_t n_tokens,
17098bd4862fSCristian Dumitrescu 			     char *out,
17108bd4862fSCristian Dumitrescu 			     size_t out_size,
1711b9559f94SCristian Dumitrescu 			     void *obj __rte_unused)
17128bd4862fSCristian Dumitrescu {
1713b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
17148bd4862fSCristian Dumitrescu 	char *pipeline_name, *learner_name, *file_name;
17158bd4862fSCristian Dumitrescu 	FILE *file = NULL;
17168bd4862fSCristian Dumitrescu 	uint32_t file_line_number = 0;
17178bd4862fSCristian Dumitrescu 	int status;
17188bd4862fSCristian Dumitrescu 
17198bd4862fSCristian Dumitrescu 	if (n_tokens != 6) {
17208bd4862fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
17218bd4862fSCristian Dumitrescu 		return;
17228bd4862fSCristian Dumitrescu 	}
17238bd4862fSCristian Dumitrescu 
17248bd4862fSCristian Dumitrescu 	pipeline_name = tokens[1];
1725b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1726b9559f94SCristian Dumitrescu 	if (!ctl) {
17278bd4862fSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
17288bd4862fSCristian Dumitrescu 		return;
17298bd4862fSCristian Dumitrescu 	}
17308bd4862fSCristian Dumitrescu 
17318bd4862fSCristian Dumitrescu 	learner_name = tokens[3];
17328bd4862fSCristian Dumitrescu 
17338bd4862fSCristian Dumitrescu 	file_name = tokens[5];
17348bd4862fSCristian Dumitrescu 	file = fopen(file_name, "r");
17358bd4862fSCristian Dumitrescu 	if (!file) {
17368bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
17378bd4862fSCristian Dumitrescu 		return;
17388bd4862fSCristian Dumitrescu 	}
17398bd4862fSCristian Dumitrescu 
1740b9559f94SCristian Dumitrescu 	status = pipeline_learner_default_entry_add(ctl,
17418bd4862fSCristian Dumitrescu 						    learner_name,
17428bd4862fSCristian Dumitrescu 						    file,
17438bd4862fSCristian Dumitrescu 						    &file_line_number);
17448bd4862fSCristian Dumitrescu 	if (status)
17458bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "Invalid entry in file %s at line %u\n",
17468bd4862fSCristian Dumitrescu 			 file_name,
17478bd4862fSCristian Dumitrescu 			 file_line_number);
17488bd4862fSCristian Dumitrescu 
17498bd4862fSCristian Dumitrescu 	fclose(file);
17508bd4862fSCristian Dumitrescu }
17518bd4862fSCristian Dumitrescu 
175275129cebSChurchill Khangar static const char cmd_pipeline_commit_help[] =
175375129cebSChurchill Khangar "pipeline <pipeline_name> commit\n";
175475129cebSChurchill Khangar 
175575129cebSChurchill Khangar static void
175675129cebSChurchill Khangar cmd_pipeline_commit(char **tokens,
175775129cebSChurchill Khangar 	uint32_t n_tokens,
175875129cebSChurchill Khangar 	char *out,
175975129cebSChurchill Khangar 	size_t out_size,
1760b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
176175129cebSChurchill Khangar {
1762b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
176375129cebSChurchill Khangar 	char *pipeline_name;
176475129cebSChurchill Khangar 	int status;
176575129cebSChurchill Khangar 
176675129cebSChurchill Khangar 	if (n_tokens != 3) {
176775129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
176875129cebSChurchill Khangar 		return;
176975129cebSChurchill Khangar 	}
177075129cebSChurchill Khangar 
177175129cebSChurchill Khangar 	pipeline_name = tokens[1];
1772b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1773b9559f94SCristian Dumitrescu 	if (!ctl) {
177475129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
177575129cebSChurchill Khangar 		return;
17765074e1d5SCristian Dumitrescu 	}
17775074e1d5SCristian Dumitrescu 
1778b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_commit(ctl, 1);
177975129cebSChurchill Khangar 	if (status)
178075129cebSChurchill Khangar 		snprintf(out, out_size, "Commit failed. "
178175129cebSChurchill Khangar 			"Use \"commit\" to retry or \"abort\" to discard the pending work.\n");
17825074e1d5SCristian Dumitrescu }
17835074e1d5SCristian Dumitrescu 
178475129cebSChurchill Khangar static const char cmd_pipeline_abort_help[] =
178575129cebSChurchill Khangar "pipeline <pipeline_name> abort\n";
17865074e1d5SCristian Dumitrescu 
178775129cebSChurchill Khangar static void
178875129cebSChurchill Khangar cmd_pipeline_abort(char **tokens,
178975129cebSChurchill Khangar 	uint32_t n_tokens,
179075129cebSChurchill Khangar 	char *out,
179175129cebSChurchill Khangar 	size_t out_size,
1792b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
179375129cebSChurchill Khangar {
1794b9559f94SCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
179575129cebSChurchill Khangar 	char *pipeline_name;
17965074e1d5SCristian Dumitrescu 
179775129cebSChurchill Khangar 	if (n_tokens != 3) {
179875129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
17995074e1d5SCristian Dumitrescu 		return;
180075129cebSChurchill Khangar 	}
18015074e1d5SCristian Dumitrescu 
180275129cebSChurchill Khangar 	pipeline_name = tokens[1];
1803b9559f94SCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
1804b9559f94SCristian Dumitrescu 	if (!ctl) {
180575129cebSChurchill Khangar 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
180675129cebSChurchill Khangar 		return;
180775129cebSChurchill Khangar 	}
180875129cebSChurchill Khangar 
1809b9559f94SCristian Dumitrescu 	rte_swx_ctl_pipeline_abort(ctl);
18105074e1d5SCristian Dumitrescu }
18115074e1d5SCristian Dumitrescu 
181264cfcebdSCristian Dumitrescu static const char cmd_pipeline_regrd_help[] =
181383f58a7bSCristian Dumitrescu "pipeline <pipeline_name> regrd <register_array_name>\n"
181483f58a7bSCristian Dumitrescu 	"index <index>\n"
181583f58a7bSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
181664cfcebdSCristian Dumitrescu 
181764cfcebdSCristian Dumitrescu static void
181864cfcebdSCristian Dumitrescu cmd_pipeline_regrd(char **tokens,
181964cfcebdSCristian Dumitrescu 	uint32_t n_tokens,
182064cfcebdSCristian Dumitrescu 	char *out,
182164cfcebdSCristian Dumitrescu 	size_t out_size,
1822b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
182364cfcebdSCristian Dumitrescu {
1824b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
182583f58a7bSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
182683f58a7bSCristian Dumitrescu 	const char *pipeline_name, *name;
182764cfcebdSCristian Dumitrescu 	uint64_t value;
182864cfcebdSCristian Dumitrescu 	int status;
182964cfcebdSCristian Dumitrescu 
183083f58a7bSCristian Dumitrescu 	if (n_tokens < 5) {
183164cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
183264cfcebdSCristian Dumitrescu 		return;
183364cfcebdSCristian Dumitrescu 	}
183464cfcebdSCristian Dumitrescu 
183583f58a7bSCristian Dumitrescu 	pipeline_name = tokens[1];
183683f58a7bSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
183783f58a7bSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
183883f58a7bSCristian Dumitrescu 	if (!p || !ctl) {
183964cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
184064cfcebdSCristian Dumitrescu 		return;
184164cfcebdSCristian Dumitrescu 	}
184264cfcebdSCristian Dumitrescu 
184364cfcebdSCristian Dumitrescu 	if (strcmp(tokens[2], "regrd")) {
184464cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "regrd");
184564cfcebdSCristian Dumitrescu 		return;
184664cfcebdSCristian Dumitrescu 	}
184764cfcebdSCristian Dumitrescu 
184864cfcebdSCristian Dumitrescu 	name = tokens[3];
184964cfcebdSCristian Dumitrescu 
185083f58a7bSCristian Dumitrescu 	/* index. */
185183f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[4], "index")) {
1852327820afSAli Alnubani 		uint32_t idx = 0;
185383f58a7bSCristian Dumitrescu 
185483f58a7bSCristian Dumitrescu 		if (n_tokens != 6) {
185583f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
185683f58a7bSCristian Dumitrescu 			return;
185783f58a7bSCristian Dumitrescu 		}
185883f58a7bSCristian Dumitrescu 
185983f58a7bSCristian Dumitrescu 		if (parser_read_uint32(&idx, tokens[5])) {
186064cfcebdSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index");
186164cfcebdSCristian Dumitrescu 			return;
186264cfcebdSCristian Dumitrescu 		}
186364cfcebdSCristian Dumitrescu 
1864b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_read(p, name, idx, &value);
186564cfcebdSCristian Dumitrescu 		if (status) {
186664cfcebdSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
186764cfcebdSCristian Dumitrescu 			return;
186864cfcebdSCristian Dumitrescu 		}
186964cfcebdSCristian Dumitrescu 
187064cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
187183f58a7bSCristian Dumitrescu 		return;
187283f58a7bSCristian Dumitrescu 	}
187383f58a7bSCristian Dumitrescu 
187483f58a7bSCristian Dumitrescu 	/* table. */
187583f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[4], "table")) {
187683f58a7bSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
187783f58a7bSCristian Dumitrescu 		char *table_name;
187883f58a7bSCristian Dumitrescu 
187983f58a7bSCristian Dumitrescu 		if (n_tokens < 8) {
188083f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
188183f58a7bSCristian Dumitrescu 			return;
188283f58a7bSCristian Dumitrescu 		}
188383f58a7bSCristian Dumitrescu 
188483f58a7bSCristian Dumitrescu 		table_name = tokens[5];
188583f58a7bSCristian Dumitrescu 
188683f58a7bSCristian Dumitrescu 		if (strcmp(tokens[6], "match")) {
188783f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
188883f58a7bSCristian Dumitrescu 			return;
188983f58a7bSCristian Dumitrescu 		}
189083f58a7bSCristian Dumitrescu 
189183f58a7bSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[6], n_tokens - 6);
189283f58a7bSCristian Dumitrescu 		if (!entry) {
189383f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
189483f58a7bSCristian Dumitrescu 			return;
189583f58a7bSCristian Dumitrescu 		}
189683f58a7bSCristian Dumitrescu 
189783f58a7bSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_read_with_key(p,
189883f58a7bSCristian Dumitrescu 								     name,
189983f58a7bSCristian Dumitrescu 								     table_name,
190083f58a7bSCristian Dumitrescu 								     entry->key,
190183f58a7bSCristian Dumitrescu 								     &value);
190283f58a7bSCristian Dumitrescu 		table_entry_free(entry);
190383f58a7bSCristian Dumitrescu 		if (status) {
190483f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
190583f58a7bSCristian Dumitrescu 			return;
190683f58a7bSCristian Dumitrescu 		}
190783f58a7bSCristian Dumitrescu 
190883f58a7bSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
190983f58a7bSCristian Dumitrescu 		return;
191083f58a7bSCristian Dumitrescu 	}
191183f58a7bSCristian Dumitrescu 
191283f58a7bSCristian Dumitrescu 	/* anything else. */
191383f58a7bSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[4]);
191483f58a7bSCristian Dumitrescu 	return;
191564cfcebdSCristian Dumitrescu }
191664cfcebdSCristian Dumitrescu 
191764cfcebdSCristian Dumitrescu static const char cmd_pipeline_regwr_help[] =
191883f58a7bSCristian Dumitrescu "pipeline <pipeline_name> regwr <register_array_name> value <value>\n"
191983f58a7bSCristian Dumitrescu 	"index <index>\n"
192083f58a7bSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
192164cfcebdSCristian Dumitrescu 
192264cfcebdSCristian Dumitrescu static void
192364cfcebdSCristian Dumitrescu cmd_pipeline_regwr(char **tokens,
192464cfcebdSCristian Dumitrescu 	uint32_t n_tokens,
192564cfcebdSCristian Dumitrescu 	char *out,
192664cfcebdSCristian Dumitrescu 	size_t out_size,
1927b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
192864cfcebdSCristian Dumitrescu {
1929b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
193083f58a7bSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
193183f58a7bSCristian Dumitrescu 	const char *pipeline_name, *name;
193283f58a7bSCristian Dumitrescu 	uint64_t value = 0;
193364cfcebdSCristian Dumitrescu 	int status;
193464cfcebdSCristian Dumitrescu 
193583f58a7bSCristian Dumitrescu 	if (n_tokens < 7) {
193664cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
193764cfcebdSCristian Dumitrescu 		return;
193864cfcebdSCristian Dumitrescu 	}
193964cfcebdSCristian Dumitrescu 
194083f58a7bSCristian Dumitrescu 	pipeline_name = tokens[1];
194183f58a7bSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
194283f58a7bSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
194383f58a7bSCristian Dumitrescu 	if (!p || !ctl) {
194464cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
194564cfcebdSCristian Dumitrescu 		return;
194664cfcebdSCristian Dumitrescu 	}
194764cfcebdSCristian Dumitrescu 
194864cfcebdSCristian Dumitrescu 	if (strcmp(tokens[2], "regwr")) {
194964cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "regwr");
195064cfcebdSCristian Dumitrescu 		return;
195164cfcebdSCristian Dumitrescu 	}
195264cfcebdSCristian Dumitrescu 
195364cfcebdSCristian Dumitrescu 	name = tokens[3];
195464cfcebdSCristian Dumitrescu 
195583f58a7bSCristian Dumitrescu 	if (strcmp(tokens[4], "value")) {
195683f58a7bSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "value");
195764cfcebdSCristian Dumitrescu 		return;
195864cfcebdSCristian Dumitrescu 	}
195964cfcebdSCristian Dumitrescu 
196064cfcebdSCristian Dumitrescu 	if (parser_read_uint64(&value, tokens[5])) {
196164cfcebdSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "value");
196264cfcebdSCristian Dumitrescu 		return;
196364cfcebdSCristian Dumitrescu 	}
196464cfcebdSCristian Dumitrescu 
196583f58a7bSCristian Dumitrescu 	/* index. */
196683f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[6], "index")) {
1967327820afSAli Alnubani 		uint32_t idx = 0;
196883f58a7bSCristian Dumitrescu 
196983f58a7bSCristian Dumitrescu 		if (n_tokens != 8) {
197083f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
197183f58a7bSCristian Dumitrescu 			return;
197283f58a7bSCristian Dumitrescu 		}
197383f58a7bSCristian Dumitrescu 
197483f58a7bSCristian Dumitrescu 		if (parser_read_uint32(&idx, tokens[7])) {
197583f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index");
197683f58a7bSCristian Dumitrescu 			return;
197783f58a7bSCristian Dumitrescu 		}
197883f58a7bSCristian Dumitrescu 
1979b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_write(p, name, idx, value);
198064cfcebdSCristian Dumitrescu 		if (status) {
198164cfcebdSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
198264cfcebdSCristian Dumitrescu 			return;
198364cfcebdSCristian Dumitrescu 		}
198483f58a7bSCristian Dumitrescu 
198583f58a7bSCristian Dumitrescu 		snprintf(out, out_size, "0x%" PRIx64 "\n", value);
198683f58a7bSCristian Dumitrescu 		return;
198783f58a7bSCristian Dumitrescu 	}
198883f58a7bSCristian Dumitrescu 
198983f58a7bSCristian Dumitrescu 	/* table. */
199083f58a7bSCristian Dumitrescu 	if (!strcmp(tokens[6], "table")) {
199183f58a7bSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
199283f58a7bSCristian Dumitrescu 		char *table_name;
199383f58a7bSCristian Dumitrescu 
199483f58a7bSCristian Dumitrescu 		if (n_tokens < 10) {
199583f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
199683f58a7bSCristian Dumitrescu 			return;
199783f58a7bSCristian Dumitrescu 		}
199883f58a7bSCristian Dumitrescu 
199983f58a7bSCristian Dumitrescu 		table_name = tokens[7];
200083f58a7bSCristian Dumitrescu 
200183f58a7bSCristian Dumitrescu 		if (strcmp(tokens[8], "match")) {
200283f58a7bSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
200383f58a7bSCristian Dumitrescu 			return;
200483f58a7bSCristian Dumitrescu 		}
200583f58a7bSCristian Dumitrescu 
200683f58a7bSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[8], n_tokens - 8);
200783f58a7bSCristian Dumitrescu 		if (!entry) {
200883f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
200983f58a7bSCristian Dumitrescu 			return;
201083f58a7bSCristian Dumitrescu 		}
201183f58a7bSCristian Dumitrescu 
201283f58a7bSCristian Dumitrescu 		status = rte_swx_ctl_pipeline_regarray_write_with_key(p,
201383f58a7bSCristian Dumitrescu 								      name,
201483f58a7bSCristian Dumitrescu 								      table_name,
201583f58a7bSCristian Dumitrescu 								      entry->key,
201683f58a7bSCristian Dumitrescu 								      value);
201783f58a7bSCristian Dumitrescu 		table_entry_free(entry);
201883f58a7bSCristian Dumitrescu 		if (status) {
201983f58a7bSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
202083f58a7bSCristian Dumitrescu 			return;
202183f58a7bSCristian Dumitrescu 		}
202283f58a7bSCristian Dumitrescu 
202383f58a7bSCristian Dumitrescu 		return;
202483f58a7bSCristian Dumitrescu 	}
202583f58a7bSCristian Dumitrescu 
202683f58a7bSCristian Dumitrescu 	/* anything else. */
202783f58a7bSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[6]);
202883f58a7bSCristian Dumitrescu 	return;
202964cfcebdSCristian Dumitrescu }
203064cfcebdSCristian Dumitrescu 
2031f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_profile_add_help[] =
2032f38913b7SCristian Dumitrescu "pipeline <pipeline_name> meter profile <profile_name> add "
2033f38913b7SCristian Dumitrescu 	"cir <cir> pir <pir> cbs <cbs> pbs <pbs>\n";
2034f38913b7SCristian Dumitrescu 
2035f38913b7SCristian Dumitrescu static void
2036f38913b7SCristian Dumitrescu cmd_pipeline_meter_profile_add(char **tokens,
2037f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2038f38913b7SCristian Dumitrescu 	char *out,
2039f38913b7SCristian Dumitrescu 	size_t out_size,
2040b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2041f38913b7SCristian Dumitrescu {
2042f38913b7SCristian Dumitrescu 	struct rte_meter_trtcm_params params;
2043b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
2044f38913b7SCristian Dumitrescu 	const char *profile_name;
2045f38913b7SCristian Dumitrescu 	int status;
2046f38913b7SCristian Dumitrescu 
2047f38913b7SCristian Dumitrescu 	if (n_tokens != 14) {
2048f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2049f38913b7SCristian Dumitrescu 		return;
2050f38913b7SCristian Dumitrescu 	}
2051f38913b7SCristian Dumitrescu 
2052b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2053b9559f94SCristian Dumitrescu 	if (!p) {
2054f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2055f38913b7SCristian Dumitrescu 		return;
2056f38913b7SCristian Dumitrescu 	}
2057f38913b7SCristian Dumitrescu 
2058f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2059f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2060f38913b7SCristian Dumitrescu 		return;
2061f38913b7SCristian Dumitrescu 	}
2062f38913b7SCristian Dumitrescu 
2063f38913b7SCristian Dumitrescu 	if (strcmp(tokens[3], "profile")) {
2064f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2065f38913b7SCristian Dumitrescu 		return;
2066f38913b7SCristian Dumitrescu 	}
2067f38913b7SCristian Dumitrescu 
2068f38913b7SCristian Dumitrescu 	profile_name = tokens[4];
2069f38913b7SCristian Dumitrescu 
2070f38913b7SCristian Dumitrescu 	if (strcmp(tokens[5], "add")) {
2071f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "add");
2072f38913b7SCristian Dumitrescu 		return;
2073f38913b7SCristian Dumitrescu 	}
2074f38913b7SCristian Dumitrescu 
2075f38913b7SCristian Dumitrescu 	if (strcmp(tokens[6], "cir")) {
2076f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cir");
2077f38913b7SCristian Dumitrescu 		return;
2078f38913b7SCristian Dumitrescu 	}
2079f38913b7SCristian Dumitrescu 
2080f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.cir, tokens[7])) {
2081f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cir");
2082f38913b7SCristian Dumitrescu 		return;
2083f38913b7SCristian Dumitrescu 	}
2084f38913b7SCristian Dumitrescu 
2085f38913b7SCristian Dumitrescu 	if (strcmp(tokens[8], "pir")) {
2086f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pir");
2087f38913b7SCristian Dumitrescu 		return;
2088f38913b7SCristian Dumitrescu 	}
2089f38913b7SCristian Dumitrescu 
2090f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.pir, tokens[9])) {
2091f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pir");
2092f38913b7SCristian Dumitrescu 		return;
2093f38913b7SCristian Dumitrescu 	}
2094f38913b7SCristian Dumitrescu 
2095f38913b7SCristian Dumitrescu 	if (strcmp(tokens[10], "cbs")) {
2096f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cbs");
2097f38913b7SCristian Dumitrescu 		return;
2098f38913b7SCristian Dumitrescu 	}
2099f38913b7SCristian Dumitrescu 
2100f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.cbs, tokens[11])) {
2101f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "cbs");
2102f38913b7SCristian Dumitrescu 		return;
2103f38913b7SCristian Dumitrescu 	}
2104f38913b7SCristian Dumitrescu 
2105f38913b7SCristian Dumitrescu 	if (strcmp(tokens[12], "pbs")) {
2106f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pbs");
2107f38913b7SCristian Dumitrescu 		return;
2108f38913b7SCristian Dumitrescu 	}
2109f38913b7SCristian Dumitrescu 
2110f38913b7SCristian Dumitrescu 	if (parser_read_uint64(&params.pbs, tokens[13])) {
2111f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pbs");
2112f38913b7SCristian Dumitrescu 		return;
2113f38913b7SCristian Dumitrescu 	}
2114f38913b7SCristian Dumitrescu 
2115b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_meter_profile_add(p, profile_name, &params);
2116f38913b7SCristian Dumitrescu 	if (status) {
2117f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "Command failed.\n");
2118f38913b7SCristian Dumitrescu 		return;
2119f38913b7SCristian Dumitrescu 	}
2120f38913b7SCristian Dumitrescu }
2121f38913b7SCristian Dumitrescu 
2122f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_profile_delete_help[] =
2123f38913b7SCristian Dumitrescu "pipeline <pipeline_name> meter profile <profile_name> delete\n";
2124f38913b7SCristian Dumitrescu 
2125f38913b7SCristian Dumitrescu static void
2126f38913b7SCristian Dumitrescu cmd_pipeline_meter_profile_delete(char **tokens,
2127f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2128f38913b7SCristian Dumitrescu 	char *out,
2129f38913b7SCristian Dumitrescu 	size_t out_size,
2130b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2131f38913b7SCristian Dumitrescu {
2132b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
2133f38913b7SCristian Dumitrescu 	const char *profile_name;
2134f38913b7SCristian Dumitrescu 	int status;
2135f38913b7SCristian Dumitrescu 
2136f38913b7SCristian Dumitrescu 	if (n_tokens != 6) {
2137f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2138f38913b7SCristian Dumitrescu 		return;
2139f38913b7SCristian Dumitrescu 	}
2140f38913b7SCristian Dumitrescu 
2141b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2142b9559f94SCristian Dumitrescu 	if (!p) {
2143f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2144f38913b7SCristian Dumitrescu 		return;
2145f38913b7SCristian Dumitrescu 	}
2146f38913b7SCristian Dumitrescu 
2147f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2148f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2149f38913b7SCristian Dumitrescu 		return;
2150f38913b7SCristian Dumitrescu 	}
2151f38913b7SCristian Dumitrescu 
2152f38913b7SCristian Dumitrescu 	if (strcmp(tokens[3], "profile")) {
2153f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2154f38913b7SCristian Dumitrescu 		return;
2155f38913b7SCristian Dumitrescu 	}
2156f38913b7SCristian Dumitrescu 
2157f38913b7SCristian Dumitrescu 	profile_name = tokens[4];
2158f38913b7SCristian Dumitrescu 
2159f38913b7SCristian Dumitrescu 	if (strcmp(tokens[5], "delete")) {
2160f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "delete");
2161f38913b7SCristian Dumitrescu 		return;
2162f38913b7SCristian Dumitrescu 	}
2163f38913b7SCristian Dumitrescu 
2164b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_meter_profile_delete(p, profile_name);
2165f38913b7SCristian Dumitrescu 	if (status) {
2166f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "Command failed.\n");
2167f38913b7SCristian Dumitrescu 		return;
2168f38913b7SCristian Dumitrescu 	}
2169f38913b7SCristian Dumitrescu }
2170f38913b7SCristian Dumitrescu 
2171f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_reset_help[] =
217212eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> reset\n"
217312eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
217412eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2175f38913b7SCristian Dumitrescu 
2176f38913b7SCristian Dumitrescu static void
2177f38913b7SCristian Dumitrescu cmd_pipeline_meter_reset(char **tokens,
2178f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2179f38913b7SCristian Dumitrescu 	char *out,
2180f38913b7SCristian Dumitrescu 	size_t out_size,
2181b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2182f38913b7SCristian Dumitrescu {
2183b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
218412eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
218512eda78dSCristian Dumitrescu 	const char *pipeline_name, *name;
2186f38913b7SCristian Dumitrescu 
218712eda78dSCristian Dumitrescu 	if (n_tokens < 6) {
2188f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2189f38913b7SCristian Dumitrescu 		return;
2190f38913b7SCristian Dumitrescu 	}
2191f38913b7SCristian Dumitrescu 
219212eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
219312eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
219412eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
219512eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2196f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2197f38913b7SCristian Dumitrescu 		return;
2198f38913b7SCristian Dumitrescu 	}
2199f38913b7SCristian Dumitrescu 
2200f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2201f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2202f38913b7SCristian Dumitrescu 		return;
2203f38913b7SCristian Dumitrescu 	}
2204f38913b7SCristian Dumitrescu 
2205f38913b7SCristian Dumitrescu 	name = tokens[3];
2206f38913b7SCristian Dumitrescu 
220712eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "reset")) {
220812eda78dSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "reset");
220912eda78dSCristian Dumitrescu 		return;
221012eda78dSCristian Dumitrescu 	}
221112eda78dSCristian Dumitrescu 
221212eda78dSCristian Dumitrescu 	/* index. */
221312eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "index")) {
221412eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
221512eda78dSCristian Dumitrescu 
221612eda78dSCristian Dumitrescu 		if (n_tokens != 10) {
221712eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
221812eda78dSCristian Dumitrescu 			return;
221912eda78dSCristian Dumitrescu 		}
222012eda78dSCristian Dumitrescu 
222112eda78dSCristian Dumitrescu 		if (strcmp(tokens[6], "from")) {
2222f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
2223f38913b7SCristian Dumitrescu 			return;
2224f38913b7SCristian Dumitrescu 		}
2225f38913b7SCristian Dumitrescu 
222612eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[7])) {
2227f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
2228f38913b7SCristian Dumitrescu 			return;
2229f38913b7SCristian Dumitrescu 		}
2230f38913b7SCristian Dumitrescu 
223112eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "to")) {
2232f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
2233f38913b7SCristian Dumitrescu 			return;
2234f38913b7SCristian Dumitrescu 		}
2235f38913b7SCristian Dumitrescu 
223612eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[9]) || (idx1 < idx0)) {
2237f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
2238f38913b7SCristian Dumitrescu 			return;
2239f38913b7SCristian Dumitrescu 		}
2240f38913b7SCristian Dumitrescu 
2241f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2242f38913b7SCristian Dumitrescu 			int status;
2243f38913b7SCristian Dumitrescu 
2244b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_reset(p, name, idx0);
2245f38913b7SCristian Dumitrescu 			if (status) {
2246f38913b7SCristian Dumitrescu 				snprintf(out, out_size, "Command failed for index %u.\n", idx0);
2247f38913b7SCristian Dumitrescu 				return;
2248f38913b7SCristian Dumitrescu 			}
2249f38913b7SCristian Dumitrescu 		}
225012eda78dSCristian Dumitrescu 
225112eda78dSCristian Dumitrescu 		return;
225212eda78dSCristian Dumitrescu 	}
225312eda78dSCristian Dumitrescu 
225412eda78dSCristian Dumitrescu 	/* table. */
225512eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "table")) {
225612eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
225712eda78dSCristian Dumitrescu 		char *table_name;
225812eda78dSCristian Dumitrescu 		int status;
225912eda78dSCristian Dumitrescu 
226012eda78dSCristian Dumitrescu 		if (n_tokens < 9) {
226112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
226212eda78dSCristian Dumitrescu 			return;
226312eda78dSCristian Dumitrescu 		}
226412eda78dSCristian Dumitrescu 
226512eda78dSCristian Dumitrescu 		table_name = tokens[6];
226612eda78dSCristian Dumitrescu 
226712eda78dSCristian Dumitrescu 		if (strcmp(tokens[7], "match")) {
226812eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
226912eda78dSCristian Dumitrescu 			return;
227012eda78dSCristian Dumitrescu 		}
227112eda78dSCristian Dumitrescu 
227212eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[7], n_tokens - 7);
227312eda78dSCristian Dumitrescu 		if (!entry) {
227412eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
227512eda78dSCristian Dumitrescu 			return;
227612eda78dSCristian Dumitrescu 		}
227712eda78dSCristian Dumitrescu 
227812eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_reset_with_key(p, name, table_name, entry->key);
227912eda78dSCristian Dumitrescu 		table_entry_free(entry);
228012eda78dSCristian Dumitrescu 		if (status) {
228112eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
228212eda78dSCristian Dumitrescu 			return;
228312eda78dSCristian Dumitrescu 		}
228412eda78dSCristian Dumitrescu 
228512eda78dSCristian Dumitrescu 		return;
228612eda78dSCristian Dumitrescu 	}
228712eda78dSCristian Dumitrescu 
228812eda78dSCristian Dumitrescu 	/* anything else. */
228912eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[5]);
229012eda78dSCristian Dumitrescu 	return;
2291f38913b7SCristian Dumitrescu }
2292f38913b7SCristian Dumitrescu 
2293f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_set_help[] =
229412eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> set profile <profile_name>\n"
229512eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
229612eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2297f38913b7SCristian Dumitrescu 
2298f38913b7SCristian Dumitrescu static void
2299f38913b7SCristian Dumitrescu cmd_pipeline_meter_set(char **tokens,
2300f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2301f38913b7SCristian Dumitrescu 	char *out,
2302f38913b7SCristian Dumitrescu 	size_t out_size,
2303b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2304f38913b7SCristian Dumitrescu {
2305b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
230612eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
230712eda78dSCristian Dumitrescu 	const char *pipeline_name, *name, *profile_name;
2308f38913b7SCristian Dumitrescu 
230912eda78dSCristian Dumitrescu 	if (n_tokens < 8) {
2310f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2311f38913b7SCristian Dumitrescu 		return;
2312f38913b7SCristian Dumitrescu 	}
2313f38913b7SCristian Dumitrescu 
231412eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
231512eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
231612eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
231712eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2318f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2319f38913b7SCristian Dumitrescu 		return;
2320f38913b7SCristian Dumitrescu 	}
2321f38913b7SCristian Dumitrescu 
2322f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2323f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2324f38913b7SCristian Dumitrescu 		return;
2325f38913b7SCristian Dumitrescu 	}
2326f38913b7SCristian Dumitrescu 
2327f38913b7SCristian Dumitrescu 	name = tokens[3];
2328f38913b7SCristian Dumitrescu 
232912eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "set")) {
2330f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "set");
2331f38913b7SCristian Dumitrescu 		return;
2332f38913b7SCristian Dumitrescu 	}
2333f38913b7SCristian Dumitrescu 
233412eda78dSCristian Dumitrescu 	if (strcmp(tokens[5], "profile")) {
2335f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "profile");
2336f38913b7SCristian Dumitrescu 		return;
2337f38913b7SCristian Dumitrescu 	}
2338f38913b7SCristian Dumitrescu 
233912eda78dSCristian Dumitrescu 	profile_name = tokens[6];
234012eda78dSCristian Dumitrescu 
234112eda78dSCristian Dumitrescu 	/* index. */
234212eda78dSCristian Dumitrescu 	if (!strcmp(tokens[7], "index")) {
234312eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
234412eda78dSCristian Dumitrescu 
234512eda78dSCristian Dumitrescu 		if (n_tokens != 12) {
234612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
234712eda78dSCristian Dumitrescu 			return;
234812eda78dSCristian Dumitrescu 		}
234912eda78dSCristian Dumitrescu 
235012eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "from")) {
235112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
235212eda78dSCristian Dumitrescu 			return;
235312eda78dSCristian Dumitrescu 		}
235412eda78dSCristian Dumitrescu 
235512eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[9])) {
235612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
235712eda78dSCristian Dumitrescu 			return;
235812eda78dSCristian Dumitrescu 		}
235912eda78dSCristian Dumitrescu 
236012eda78dSCristian Dumitrescu 		if (strcmp(tokens[10], "to")) {
236112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
236212eda78dSCristian Dumitrescu 			return;
236312eda78dSCristian Dumitrescu 		}
236412eda78dSCristian Dumitrescu 
236512eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[11]) || (idx1 < idx0)) {
236612eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
236712eda78dSCristian Dumitrescu 			return;
236812eda78dSCristian Dumitrescu 		}
2369f38913b7SCristian Dumitrescu 
2370f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2371f38913b7SCristian Dumitrescu 			int status;
2372f38913b7SCristian Dumitrescu 
2373b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_set(p, name, idx0, profile_name);
2374f38913b7SCristian Dumitrescu 			if (status) {
2375f38913b7SCristian Dumitrescu 				snprintf(out, out_size, "Command failed for index %u.\n", idx0);
2376f38913b7SCristian Dumitrescu 				return;
2377f38913b7SCristian Dumitrescu 			}
2378f38913b7SCristian Dumitrescu 		}
237912eda78dSCristian Dumitrescu 
238012eda78dSCristian Dumitrescu 		return;
238112eda78dSCristian Dumitrescu 	}
238212eda78dSCristian Dumitrescu 
238312eda78dSCristian Dumitrescu 	/* table. */
238412eda78dSCristian Dumitrescu 	if (!strcmp(tokens[7], "table")) {
238512eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
238612eda78dSCristian Dumitrescu 		char *table_name;
238712eda78dSCristian Dumitrescu 		int status;
238812eda78dSCristian Dumitrescu 
238912eda78dSCristian Dumitrescu 		if (n_tokens < 11) {
239012eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
239112eda78dSCristian Dumitrescu 			return;
239212eda78dSCristian Dumitrescu 		}
239312eda78dSCristian Dumitrescu 
239412eda78dSCristian Dumitrescu 		table_name = tokens[8];
239512eda78dSCristian Dumitrescu 
239612eda78dSCristian Dumitrescu 		if (strcmp(tokens[9], "match")) {
239712eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
239812eda78dSCristian Dumitrescu 			return;
239912eda78dSCristian Dumitrescu 		}
240012eda78dSCristian Dumitrescu 
240112eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[9], n_tokens - 9);
240212eda78dSCristian Dumitrescu 		if (!entry) {
240312eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
240412eda78dSCristian Dumitrescu 			return;
240512eda78dSCristian Dumitrescu 		}
240612eda78dSCristian Dumitrescu 
240712eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_set_with_key(p,
240812eda78dSCristian Dumitrescu 							name,
240912eda78dSCristian Dumitrescu 							table_name,
241012eda78dSCristian Dumitrescu 							entry->key,
241112eda78dSCristian Dumitrescu 							profile_name);
241212eda78dSCristian Dumitrescu 		table_entry_free(entry);
241312eda78dSCristian Dumitrescu 		if (status) {
241412eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
241512eda78dSCristian Dumitrescu 			return;
241612eda78dSCristian Dumitrescu 		}
241712eda78dSCristian Dumitrescu 
241812eda78dSCristian Dumitrescu 		return;
241912eda78dSCristian Dumitrescu 	}
242012eda78dSCristian Dumitrescu 
242112eda78dSCristian Dumitrescu 	/* anything else. */
242212eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[7]);
242312eda78dSCristian Dumitrescu 	return;
2424f38913b7SCristian Dumitrescu }
2425f38913b7SCristian Dumitrescu 
2426f38913b7SCristian Dumitrescu static const char cmd_pipeline_meter_stats_help[] =
242712eda78dSCristian Dumitrescu "pipeline <pipeline_name> meter <meter_array_name> stats\n"
242812eda78dSCristian Dumitrescu 	"index from <index0> to <index1>\n"
242912eda78dSCristian Dumitrescu 	" | table <table_name> match <field0> ...\n";
2430f38913b7SCristian Dumitrescu 
2431f38913b7SCristian Dumitrescu static void
2432f38913b7SCristian Dumitrescu cmd_pipeline_meter_stats(char **tokens,
2433f38913b7SCristian Dumitrescu 	uint32_t n_tokens,
2434f38913b7SCristian Dumitrescu 	char *out,
2435f38913b7SCristian Dumitrescu 	size_t out_size,
2436b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
2437f38913b7SCristian Dumitrescu {
2438f38913b7SCristian Dumitrescu 	struct rte_swx_ctl_meter_stats stats;
2439b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
244012eda78dSCristian Dumitrescu 	struct rte_swx_ctl_pipeline *ctl;
244112eda78dSCristian Dumitrescu 	const char *pipeline_name, *name;
2442f38913b7SCristian Dumitrescu 
244312eda78dSCristian Dumitrescu 	if (n_tokens < 6) {
2444f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
2445f38913b7SCristian Dumitrescu 		return;
2446f38913b7SCristian Dumitrescu 	}
2447f38913b7SCristian Dumitrescu 
244812eda78dSCristian Dumitrescu 	pipeline_name = tokens[1];
244912eda78dSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
245012eda78dSCristian Dumitrescu 	ctl = rte_swx_ctl_pipeline_find(pipeline_name);
245112eda78dSCristian Dumitrescu 	if (!p || !ctl) {
2452f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
2453f38913b7SCristian Dumitrescu 		return;
2454f38913b7SCristian Dumitrescu 	}
2455f38913b7SCristian Dumitrescu 
2456f38913b7SCristian Dumitrescu 	if (strcmp(tokens[2], "meter")) {
2457f38913b7SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "meter");
2458f38913b7SCristian Dumitrescu 		return;
2459f38913b7SCristian Dumitrescu 	}
2460f38913b7SCristian Dumitrescu 
2461f38913b7SCristian Dumitrescu 	name = tokens[3];
2462f38913b7SCristian Dumitrescu 
246312eda78dSCristian Dumitrescu 	if (strcmp(tokens[4], "stats")) {
246412eda78dSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "stats");
246512eda78dSCristian Dumitrescu 		return;
246612eda78dSCristian Dumitrescu 	}
246712eda78dSCristian Dumitrescu 
246812eda78dSCristian Dumitrescu 	/* index. */
246912eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "index")) {
247012eda78dSCristian Dumitrescu 		uint32_t idx0 = 0, idx1 = 0;
247112eda78dSCristian Dumitrescu 
247212eda78dSCristian Dumitrescu 		if (n_tokens != 10) {
247312eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
247412eda78dSCristian Dumitrescu 			return;
247512eda78dSCristian Dumitrescu 		}
247612eda78dSCristian Dumitrescu 
247712eda78dSCristian Dumitrescu 		if (strcmp(tokens[6], "from")) {
2478f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "from");
2479f38913b7SCristian Dumitrescu 			return;
2480f38913b7SCristian Dumitrescu 		}
2481f38913b7SCristian Dumitrescu 
248212eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx0, tokens[7])) {
2483f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index0");
2484f38913b7SCristian Dumitrescu 			return;
2485f38913b7SCristian Dumitrescu 		}
2486f38913b7SCristian Dumitrescu 
248712eda78dSCristian Dumitrescu 		if (strcmp(tokens[8], "to")) {
2488f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "to");
2489f38913b7SCristian Dumitrescu 			return;
2490f38913b7SCristian Dumitrescu 		}
2491f38913b7SCristian Dumitrescu 
249212eda78dSCristian Dumitrescu 		if (parser_read_uint32(&idx1, tokens[9]) || (idx1 < idx0)) {
2493f38913b7SCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_INVALID, "index1");
2494f38913b7SCristian Dumitrescu 			return;
2495f38913b7SCristian Dumitrescu 		}
2496f38913b7SCristian Dumitrescu 
2497f38913b7SCristian Dumitrescu 		/* Table header. */
2498f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
2499f38913b7SCristian Dumitrescu 			 "-------",
2500f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------",
2501f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------");
2502f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2503f38913b7SCristian Dumitrescu 		out += strlen(out);
2504f38913b7SCristian Dumitrescu 
2505f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "| %4s | %16s | %16s | %16s | %16s | %16s | %16s |\n",
2506f38913b7SCristian Dumitrescu 			 "METER #",
2507f38913b7SCristian Dumitrescu 			 "GREEN (packets)", "YELLOW (packets)", "RED (packets)",
2508f38913b7SCristian Dumitrescu 			 "GREEN (bytes)", "YELLOW (bytes)", "RED (bytes)");
2509f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2510f38913b7SCristian Dumitrescu 		out += strlen(out);
2511f38913b7SCristian Dumitrescu 
2512f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
2513f38913b7SCristian Dumitrescu 			 "-------",
2514f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------",
2515f38913b7SCristian Dumitrescu 			 "----------------", "----------------", "----------------");
2516f38913b7SCristian Dumitrescu 		out_size -= strlen(out);
2517f38913b7SCristian Dumitrescu 		out += strlen(out);
2518f38913b7SCristian Dumitrescu 
2519f38913b7SCristian Dumitrescu 		/* Table rows. */
2520f38913b7SCristian Dumitrescu 		for ( ; idx0 <= idx1; idx0++) {
2521f38913b7SCristian Dumitrescu 			int status;
2522f38913b7SCristian Dumitrescu 
2523b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_meter_stats_read(p, name, idx0, &stats);
2524f38913b7SCristian Dumitrescu 			if (status) {
252512eda78dSCristian Dumitrescu 				snprintf(out, out_size, "Meter stats error at index %u.\n", idx0);
2526f38913b7SCristian Dumitrescu 				out_size -= strlen(out);
2527f38913b7SCristian Dumitrescu 				out += strlen(out);
2528f38913b7SCristian Dumitrescu 				return;
2529f38913b7SCristian Dumitrescu 			}
2530f38913b7SCristian Dumitrescu 
2531f38913b7SCristian Dumitrescu 			snprintf(out, out_size, "| %7d | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64
2532f38913b7SCristian Dumitrescu 				 " | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64 " |\n",
2533f38913b7SCristian Dumitrescu 				 idx0,
2534f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_GREEN],
2535f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_YELLOW],
2536f38913b7SCristian Dumitrescu 				 stats.n_pkts[RTE_COLOR_RED],
2537f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_GREEN],
2538f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_YELLOW],
2539f38913b7SCristian Dumitrescu 				 stats.n_bytes[RTE_COLOR_RED]);
2540f38913b7SCristian Dumitrescu 			out_size -= strlen(out);
2541f38913b7SCristian Dumitrescu 			out += strlen(out);
2542f38913b7SCristian Dumitrescu 		}
254312eda78dSCristian Dumitrescu 
254412eda78dSCristian Dumitrescu 		return;
254512eda78dSCristian Dumitrescu 	}
254612eda78dSCristian Dumitrescu 
254712eda78dSCristian Dumitrescu 	/* table. */
254812eda78dSCristian Dumitrescu 	if (!strcmp(tokens[5], "table")) {
254912eda78dSCristian Dumitrescu 		struct rte_swx_table_entry *entry;
255012eda78dSCristian Dumitrescu 		char *table_name;
255112eda78dSCristian Dumitrescu 		int status;
255212eda78dSCristian Dumitrescu 
255312eda78dSCristian Dumitrescu 		if (n_tokens < 9) {
255412eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
255512eda78dSCristian Dumitrescu 			return;
255612eda78dSCristian Dumitrescu 		}
255712eda78dSCristian Dumitrescu 
255812eda78dSCristian Dumitrescu 		table_name = tokens[6];
255912eda78dSCristian Dumitrescu 
256012eda78dSCristian Dumitrescu 		if (strcmp(tokens[7], "match")) {
256112eda78dSCristian Dumitrescu 			snprintf(out, out_size, MSG_ARG_NOT_FOUND, "match");
256212eda78dSCristian Dumitrescu 			return;
256312eda78dSCristian Dumitrescu 		}
256412eda78dSCristian Dumitrescu 
256512eda78dSCristian Dumitrescu 		entry = parse_table_entry(ctl, table_name, &tokens[7], n_tokens - 7);
256612eda78dSCristian Dumitrescu 		if (!entry) {
256712eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Invalid match tokens.\n");
256812eda78dSCristian Dumitrescu 			return;
256912eda78dSCristian Dumitrescu 		}
257012eda78dSCristian Dumitrescu 
257112eda78dSCristian Dumitrescu 		status = rte_swx_ctl_meter_stats_read_with_key(p,
257212eda78dSCristian Dumitrescu 							name,
257312eda78dSCristian Dumitrescu 							table_name,
257412eda78dSCristian Dumitrescu 							entry->key,
257512eda78dSCristian Dumitrescu 							&stats);
257612eda78dSCristian Dumitrescu 		table_entry_free(entry);
257712eda78dSCristian Dumitrescu 		if (status) {
257812eda78dSCristian Dumitrescu 			snprintf(out, out_size, "Command failed.\n");
257912eda78dSCristian Dumitrescu 			return;
258012eda78dSCristian Dumitrescu 		}
258112eda78dSCristian Dumitrescu 
258212eda78dSCristian Dumitrescu 		/* Table header. */
258312eda78dSCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
258412eda78dSCristian Dumitrescu 			 "-------",
258512eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------",
258612eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------");
258712eda78dSCristian Dumitrescu 		out_size -= strlen(out);
258812eda78dSCristian Dumitrescu 		out += strlen(out);
258912eda78dSCristian Dumitrescu 
259012eda78dSCristian Dumitrescu 		snprintf(out, out_size, "| %4s | %16s | %16s | %16s | %16s | %16s | %16s |\n",
259112eda78dSCristian Dumitrescu 			 "METER #",
259212eda78dSCristian Dumitrescu 			 "GREEN (packets)", "YELLOW (packets)", "RED (packets)",
259312eda78dSCristian Dumitrescu 			 "GREEN (bytes)", "YELLOW (bytes)", "RED (bytes)");
259412eda78dSCristian Dumitrescu 		out_size -= strlen(out);
259512eda78dSCristian Dumitrescu 		out += strlen(out);
259612eda78dSCristian Dumitrescu 
259712eda78dSCristian Dumitrescu 		snprintf(out, out_size, "+-%7s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+-%16s-+\n",
259812eda78dSCristian Dumitrescu 			 "-------",
259912eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------",
260012eda78dSCristian Dumitrescu 			 "----------------", "----------------", "----------------");
260112eda78dSCristian Dumitrescu 		out_size -= strlen(out);
260212eda78dSCristian Dumitrescu 		out += strlen(out);
260312eda78dSCristian Dumitrescu 
260412eda78dSCristian Dumitrescu 		/* Table row. */
260512eda78dSCristian Dumitrescu 		snprintf(out, out_size, "| %7d | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64
260612eda78dSCristian Dumitrescu 			 " | %16" PRIx64 " | %16" PRIx64 " | %16" PRIx64 " |\n",
260712eda78dSCristian Dumitrescu 			 0,
260812eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_GREEN],
260912eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_YELLOW],
261012eda78dSCristian Dumitrescu 			 stats.n_pkts[RTE_COLOR_RED],
261112eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_GREEN],
261212eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_YELLOW],
261312eda78dSCristian Dumitrescu 			 stats.n_bytes[RTE_COLOR_RED]);
261412eda78dSCristian Dumitrescu 		out_size -= strlen(out);
261512eda78dSCristian Dumitrescu 		out += strlen(out);
261612eda78dSCristian Dumitrescu 
261712eda78dSCristian Dumitrescu 		return;
261812eda78dSCristian Dumitrescu 	}
261912eda78dSCristian Dumitrescu 
262012eda78dSCristian Dumitrescu 	/* anything else. */
262112eda78dSCristian Dumitrescu 	snprintf(out, out_size, "Invalid token %s\n.", tokens[5]);
262212eda78dSCristian Dumitrescu 	return;
2623f38913b7SCristian Dumitrescu }
2624f38913b7SCristian Dumitrescu 
26255074e1d5SCristian Dumitrescu static const char cmd_pipeline_stats_help[] =
26265074e1d5SCristian Dumitrescu "pipeline <pipeline_name> stats\n";
26275074e1d5SCristian Dumitrescu 
26285074e1d5SCristian Dumitrescu static void
26295074e1d5SCristian Dumitrescu cmd_pipeline_stats(char **tokens,
26305074e1d5SCristian Dumitrescu 	uint32_t n_tokens,
26315074e1d5SCristian Dumitrescu 	char *out,
26325074e1d5SCristian Dumitrescu 	size_t out_size,
2633b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
26345074e1d5SCristian Dumitrescu {
26355074e1d5SCristian Dumitrescu 	struct rte_swx_ctl_pipeline_info info;
2636b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
26375074e1d5SCristian Dumitrescu 	uint32_t i;
26385074e1d5SCristian Dumitrescu 	int status;
26395074e1d5SCristian Dumitrescu 
26405074e1d5SCristian Dumitrescu 	if (n_tokens != 3) {
26415074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
26425074e1d5SCristian Dumitrescu 		return;
26435074e1d5SCristian Dumitrescu 	}
26445074e1d5SCristian Dumitrescu 
2645b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2646b9559f94SCristian Dumitrescu 	if (!p) {
26475074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
26485074e1d5SCristian Dumitrescu 		return;
26495074e1d5SCristian Dumitrescu 	}
26505074e1d5SCristian Dumitrescu 
26515074e1d5SCristian Dumitrescu 	if (strcmp(tokens[2], "stats")) {
26525074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "stats");
26535074e1d5SCristian Dumitrescu 		return;
26545074e1d5SCristian Dumitrescu 	}
26555074e1d5SCristian Dumitrescu 
2656b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_info_get(p, &info);
26575074e1d5SCristian Dumitrescu 	if (status) {
26585074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "Pipeline info get error.");
26595074e1d5SCristian Dumitrescu 		return;
26605074e1d5SCristian Dumitrescu 	}
26615074e1d5SCristian Dumitrescu 
26625074e1d5SCristian Dumitrescu 	snprintf(out, out_size, "Input ports:\n");
26635074e1d5SCristian Dumitrescu 	out_size -= strlen(out);
26645074e1d5SCristian Dumitrescu 	out += strlen(out);
26655074e1d5SCristian Dumitrescu 
26665074e1d5SCristian Dumitrescu 	for (i = 0; i < info.n_ports_in; i++) {
26675074e1d5SCristian Dumitrescu 		struct rte_swx_port_in_stats stats;
26685074e1d5SCristian Dumitrescu 
2669b9559f94SCristian Dumitrescu 		rte_swx_ctl_pipeline_port_in_stats_read(p, i, &stats);
26705074e1d5SCristian Dumitrescu 
26715074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\tPort %u:"
26725074e1d5SCristian Dumitrescu 			" packets %" PRIu64
26735074e1d5SCristian Dumitrescu 			" bytes %" PRIu64
26745074e1d5SCristian Dumitrescu 			" empty %" PRIu64 "\n",
26755074e1d5SCristian Dumitrescu 			i, stats.n_pkts, stats.n_bytes, stats.n_empty);
26765074e1d5SCristian Dumitrescu 		out_size -= strlen(out);
26775074e1d5SCristian Dumitrescu 		out += strlen(out);
26785074e1d5SCristian Dumitrescu 	}
26795074e1d5SCristian Dumitrescu 
2680742b0a57SCristian Dumitrescu 	snprintf(out, out_size, "\nOutput ports:\n");
26815074e1d5SCristian Dumitrescu 	out_size -= strlen(out);
26825074e1d5SCristian Dumitrescu 	out += strlen(out);
26835074e1d5SCristian Dumitrescu 
26845074e1d5SCristian Dumitrescu 	for (i = 0; i < info.n_ports_out; i++) {
26855074e1d5SCristian Dumitrescu 		struct rte_swx_port_out_stats stats;
26865074e1d5SCristian Dumitrescu 
2687b9559f94SCristian Dumitrescu 		rte_swx_ctl_pipeline_port_out_stats_read(p, i, &stats);
26885074e1d5SCristian Dumitrescu 
268996b37959SCristian Dumitrescu 		if (i != info.n_ports_out - 1)
269017225455SCristian Dumitrescu 			snprintf(out, out_size, "\tPort %u:", i);
269196b37959SCristian Dumitrescu 		else
269217225455SCristian Dumitrescu 			snprintf(out, out_size, "\tDROP:");
269317225455SCristian Dumitrescu 
269417225455SCristian Dumitrescu 		out_size -= strlen(out);
269517225455SCristian Dumitrescu 		out += strlen(out);
269617225455SCristian Dumitrescu 
269717225455SCristian Dumitrescu 		snprintf(out,
269817225455SCristian Dumitrescu 			out_size,
269996b37959SCristian Dumitrescu 			" packets %" PRIu64
270017225455SCristian Dumitrescu 			" bytes %" PRIu64
270167f707b3SCristian Dumitrescu 			" packets dropped %" PRIu64
270267f707b3SCristian Dumitrescu 			" bytes dropped %" PRIu64
270317225455SCristian Dumitrescu 			" clone %" PRIu64
270417225455SCristian Dumitrescu 			" clonerr %" PRIu64 "\n",
270517225455SCristian Dumitrescu 			stats.n_pkts,
270617225455SCristian Dumitrescu 			stats.n_bytes,
270767f707b3SCristian Dumitrescu 			stats.n_pkts_drop,
270867f707b3SCristian Dumitrescu 			stats.n_bytes_drop,
270917225455SCristian Dumitrescu 			stats.n_pkts_clone,
271017225455SCristian Dumitrescu 			stats.n_pkts_clone_err);
271196b37959SCristian Dumitrescu 
27125074e1d5SCristian Dumitrescu 		out_size -= strlen(out);
27135074e1d5SCristian Dumitrescu 		out += strlen(out);
27145074e1d5SCristian Dumitrescu 	}
2715742b0a57SCristian Dumitrescu 
2716742b0a57SCristian Dumitrescu 	snprintf(out, out_size, "\nTables:\n");
2717742b0a57SCristian Dumitrescu 	out_size -= strlen(out);
2718742b0a57SCristian Dumitrescu 	out += strlen(out);
2719742b0a57SCristian Dumitrescu 
2720742b0a57SCristian Dumitrescu 	for (i = 0; i < info.n_tables; i++) {
2721742b0a57SCristian Dumitrescu 		struct rte_swx_ctl_table_info table_info;
2722742b0a57SCristian Dumitrescu 		uint64_t n_pkts_action[info.n_actions];
2723742b0a57SCristian Dumitrescu 		struct rte_swx_table_stats stats = {
2724742b0a57SCristian Dumitrescu 			.n_pkts_hit = 0,
2725742b0a57SCristian Dumitrescu 			.n_pkts_miss = 0,
2726742b0a57SCristian Dumitrescu 			.n_pkts_action = n_pkts_action,
2727742b0a57SCristian Dumitrescu 		};
2728742b0a57SCristian Dumitrescu 		uint32_t j;
2729742b0a57SCristian Dumitrescu 
2730b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_table_info_get(p, i, &table_info);
2731742b0a57SCristian Dumitrescu 		if (status) {
2732742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "Table info get error.");
2733742b0a57SCristian Dumitrescu 			return;
2734742b0a57SCristian Dumitrescu 		}
2735742b0a57SCristian Dumitrescu 
2736b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_table_stats_read(p, table_info.name, &stats);
2737742b0a57SCristian Dumitrescu 		if (status) {
2738742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "Table stats read error.");
2739742b0a57SCristian Dumitrescu 			return;
2740742b0a57SCristian Dumitrescu 		}
2741742b0a57SCristian Dumitrescu 
2742742b0a57SCristian Dumitrescu 		snprintf(out, out_size, "\tTable %s:\n"
2743742b0a57SCristian Dumitrescu 			"\t\tHit (packets): %" PRIu64 "\n"
2744742b0a57SCristian Dumitrescu 			"\t\tMiss (packets): %" PRIu64 "\n",
2745742b0a57SCristian Dumitrescu 			table_info.name,
2746742b0a57SCristian Dumitrescu 			stats.n_pkts_hit,
2747742b0a57SCristian Dumitrescu 			stats.n_pkts_miss);
2748742b0a57SCristian Dumitrescu 		out_size -= strlen(out);
2749742b0a57SCristian Dumitrescu 		out += strlen(out);
2750742b0a57SCristian Dumitrescu 
2751742b0a57SCristian Dumitrescu 		for (j = 0; j < info.n_actions; j++) {
2752742b0a57SCristian Dumitrescu 			struct rte_swx_ctl_action_info action_info;
2753742b0a57SCristian Dumitrescu 
2754b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_action_info_get(p, j, &action_info);
2755742b0a57SCristian Dumitrescu 			if (status) {
2756742b0a57SCristian Dumitrescu 				snprintf(out, out_size, "Action info get error.");
2757742b0a57SCristian Dumitrescu 				return;
2758742b0a57SCristian Dumitrescu 			}
2759742b0a57SCristian Dumitrescu 
2760742b0a57SCristian Dumitrescu 			snprintf(out, out_size, "\t\tAction %s (packets): %" PRIu64 "\n",
2761742b0a57SCristian Dumitrescu 				action_info.name,
2762742b0a57SCristian Dumitrescu 				stats.n_pkts_action[j]);
2763742b0a57SCristian Dumitrescu 			out_size -= strlen(out);
2764742b0a57SCristian Dumitrescu 			out += strlen(out);
2765742b0a57SCristian Dumitrescu 		}
2766742b0a57SCristian Dumitrescu 	}
27678bd4862fSCristian Dumitrescu 
27688bd4862fSCristian Dumitrescu 	snprintf(out, out_size, "\nLearner tables:\n");
27698bd4862fSCristian Dumitrescu 	out_size -= strlen(out);
27708bd4862fSCristian Dumitrescu 	out += strlen(out);
27718bd4862fSCristian Dumitrescu 
27728bd4862fSCristian Dumitrescu 	for (i = 0; i < info.n_learners; i++) {
27738bd4862fSCristian Dumitrescu 		struct rte_swx_ctl_learner_info learner_info;
27748bd4862fSCristian Dumitrescu 		uint64_t n_pkts_action[info.n_actions];
27758bd4862fSCristian Dumitrescu 		struct rte_swx_learner_stats stats = {
27768bd4862fSCristian Dumitrescu 			.n_pkts_hit = 0,
27778bd4862fSCristian Dumitrescu 			.n_pkts_miss = 0,
27788bd4862fSCristian Dumitrescu 			.n_pkts_action = n_pkts_action,
27798bd4862fSCristian Dumitrescu 		};
27808bd4862fSCristian Dumitrescu 		uint32_t j;
27818bd4862fSCristian Dumitrescu 
2782b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_learner_info_get(p, i, &learner_info);
27838bd4862fSCristian Dumitrescu 		if (status) {
27848bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "Learner table info get error.");
27858bd4862fSCristian Dumitrescu 			return;
27868bd4862fSCristian Dumitrescu 		}
27878bd4862fSCristian Dumitrescu 
2788b9559f94SCristian Dumitrescu 		status = rte_swx_ctl_pipeline_learner_stats_read(p, learner_info.name, &stats);
27898bd4862fSCristian Dumitrescu 		if (status) {
27908bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "Learner table stats read error.");
27918bd4862fSCristian Dumitrescu 			return;
27928bd4862fSCristian Dumitrescu 		}
27938bd4862fSCristian Dumitrescu 
27948bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "\tLearner table %s:\n"
27958bd4862fSCristian Dumitrescu 			"\t\tHit (packets): %" PRIu64 "\n"
27968bd4862fSCristian Dumitrescu 			"\t\tMiss (packets): %" PRIu64 "\n"
27978bd4862fSCristian Dumitrescu 			"\t\tLearn OK (packets): %" PRIu64 "\n"
27988bd4862fSCristian Dumitrescu 			"\t\tLearn error (packets): %" PRIu64 "\n"
279980dd28afSCristian Dumitrescu 			"\t\tRearm (packets): %" PRIu64 "\n"
28008bd4862fSCristian Dumitrescu 			"\t\tForget (packets): %" PRIu64 "\n",
28018bd4862fSCristian Dumitrescu 			learner_info.name,
28028bd4862fSCristian Dumitrescu 			stats.n_pkts_hit,
28038bd4862fSCristian Dumitrescu 			stats.n_pkts_miss,
28048bd4862fSCristian Dumitrescu 			stats.n_pkts_learn_ok,
28058bd4862fSCristian Dumitrescu 			stats.n_pkts_learn_err,
280680dd28afSCristian Dumitrescu 			stats.n_pkts_rearm,
28078bd4862fSCristian Dumitrescu 			stats.n_pkts_forget);
28088bd4862fSCristian Dumitrescu 		out_size -= strlen(out);
28098bd4862fSCristian Dumitrescu 		out += strlen(out);
28108bd4862fSCristian Dumitrescu 
28118bd4862fSCristian Dumitrescu 		for (j = 0; j < info.n_actions; j++) {
28128bd4862fSCristian Dumitrescu 			struct rte_swx_ctl_action_info action_info;
28138bd4862fSCristian Dumitrescu 
2814b9559f94SCristian Dumitrescu 			status = rte_swx_ctl_action_info_get(p, j, &action_info);
28158bd4862fSCristian Dumitrescu 			if (status) {
28168bd4862fSCristian Dumitrescu 				snprintf(out, out_size, "Action info get error.");
28178bd4862fSCristian Dumitrescu 				return;
28188bd4862fSCristian Dumitrescu 			}
28198bd4862fSCristian Dumitrescu 
28208bd4862fSCristian Dumitrescu 			snprintf(out, out_size, "\t\tAction %s (packets): %" PRIu64 "\n",
28218bd4862fSCristian Dumitrescu 				action_info.name,
28228bd4862fSCristian Dumitrescu 				stats.n_pkts_action[j]);
28238bd4862fSCristian Dumitrescu 			out_size -= strlen(out);
28248bd4862fSCristian Dumitrescu 			out += strlen(out);
28258bd4862fSCristian Dumitrescu 		}
28268bd4862fSCristian Dumitrescu 	}
28275074e1d5SCristian Dumitrescu }
28285074e1d5SCristian Dumitrescu 
282917225455SCristian Dumitrescu static const char cmd_pipeline_mirror_session_help[] =
283017225455SCristian Dumitrescu "pipeline <pipeline_name> mirror session <session_id> port <port_id> clone fast | slow "
283117225455SCristian Dumitrescu "truncate <truncation_length>\n";
283217225455SCristian Dumitrescu 
283317225455SCristian Dumitrescu static void
283417225455SCristian Dumitrescu cmd_pipeline_mirror_session(char **tokens,
283517225455SCristian Dumitrescu 	uint32_t n_tokens,
283617225455SCristian Dumitrescu 	char *out,
283717225455SCristian Dumitrescu 	size_t out_size,
2838b9559f94SCristian Dumitrescu 	void *obj __rte_unused)
283917225455SCristian Dumitrescu {
284017225455SCristian Dumitrescu 	struct rte_swx_pipeline_mirroring_session_params params;
2841b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
284277dd857dSAli Alnubani 	uint32_t session_id = 0;
284317225455SCristian Dumitrescu 	int status;
284417225455SCristian Dumitrescu 
284517225455SCristian Dumitrescu 	if (n_tokens != 11) {
284617225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
284717225455SCristian Dumitrescu 		return;
284817225455SCristian Dumitrescu 	}
284917225455SCristian Dumitrescu 
285017225455SCristian Dumitrescu 	if (strcmp(tokens[0], "pipeline")) {
285117225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pipeline");
285217225455SCristian Dumitrescu 		return;
285317225455SCristian Dumitrescu 	}
285417225455SCristian Dumitrescu 
2855b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(tokens[1]);
2856b9559f94SCristian Dumitrescu 	if (!p) {
285717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
285817225455SCristian Dumitrescu 		return;
285917225455SCristian Dumitrescu 	}
286017225455SCristian Dumitrescu 
286117225455SCristian Dumitrescu 	if (strcmp(tokens[2], "mirror")) {
286217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "mirror");
286317225455SCristian Dumitrescu 		return;
286417225455SCristian Dumitrescu 	}
286517225455SCristian Dumitrescu 
286617225455SCristian Dumitrescu 	if (strcmp(tokens[3], "session")) {
286717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "session");
286817225455SCristian Dumitrescu 		return;
286917225455SCristian Dumitrescu 	}
287017225455SCristian Dumitrescu 
287117225455SCristian Dumitrescu 	if (parser_read_uint32(&session_id, tokens[4])) {
287217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "session_id");
287317225455SCristian Dumitrescu 		return;
287417225455SCristian Dumitrescu 	}
287517225455SCristian Dumitrescu 
287617225455SCristian Dumitrescu 	if (strcmp(tokens[5], "port")) {
287717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "port");
287817225455SCristian Dumitrescu 		return;
287917225455SCristian Dumitrescu 	}
288017225455SCristian Dumitrescu 
288117225455SCristian Dumitrescu 	if (parser_read_uint32(&params.port_id, tokens[6])) {
288217225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "port_id");
288317225455SCristian Dumitrescu 		return;
288417225455SCristian Dumitrescu 	}
288517225455SCristian Dumitrescu 
288617225455SCristian Dumitrescu 	if (strcmp(tokens[7], "clone")) {
288717225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "clone");
288817225455SCristian Dumitrescu 		return;
288917225455SCristian Dumitrescu 	}
289017225455SCristian Dumitrescu 
289117225455SCristian Dumitrescu 	if (!strcmp(tokens[8], "fast"))
289217225455SCristian Dumitrescu 		params.fast_clone = 1;
289317225455SCristian Dumitrescu 	else if (!strcmp(tokens[8], "slow"))
289417225455SCristian Dumitrescu 		params.fast_clone = 0;
289517225455SCristian Dumitrescu 	else {
289617225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "clone");
289717225455SCristian Dumitrescu 		return;
289817225455SCristian Dumitrescu 	}
289917225455SCristian Dumitrescu 
290017225455SCristian Dumitrescu 	if (strcmp(tokens[9], "truncate")) {
290117225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "truncate");
290217225455SCristian Dumitrescu 		return;
290317225455SCristian Dumitrescu 	}
290417225455SCristian Dumitrescu 
290517225455SCristian Dumitrescu 	if (parser_read_uint32(&params.truncation_length, tokens[10])) {
290617225455SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "truncation_length");
290717225455SCristian Dumitrescu 		return;
290817225455SCristian Dumitrescu 	}
290917225455SCristian Dumitrescu 
2910b9559f94SCristian Dumitrescu 	status = rte_swx_ctl_pipeline_mirroring_session_set(p, session_id, &params);
291117225455SCristian Dumitrescu 	if (status) {
291217225455SCristian Dumitrescu 		snprintf(out, out_size, "Command failed!\n");
291317225455SCristian Dumitrescu 		return;
291417225455SCristian Dumitrescu 	}
291517225455SCristian Dumitrescu }
291617225455SCristian Dumitrescu 
29173b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_create_help[] =
29183b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> create "
29193b0cc5fbSCristian Dumitrescu "in <ring_in_name> out <ring_out_name> "
29203b0cc5fbSCristian Dumitrescu "cryptodev <crypto_dev_name> cryptoq <crypto_dev_queue_pair_id> "
29213b0cc5fbSCristian Dumitrescu "bsz <ring_rd_bsz> <ring_wr_bsz> <crypto_wr_bsz> <crypto_rd_bsz> "
29223b0cc5fbSCristian Dumitrescu "samax <n_sa_max> "
29233b0cc5fbSCristian Dumitrescu "numa <numa_node>\n";
29243b0cc5fbSCristian Dumitrescu 
29253b0cc5fbSCristian Dumitrescu static void
29263b0cc5fbSCristian Dumitrescu cmd_ipsec_create(char **tokens,
29273b0cc5fbSCristian Dumitrescu 		 uint32_t n_tokens,
29283b0cc5fbSCristian Dumitrescu 		 char *out,
29293b0cc5fbSCristian Dumitrescu 		 size_t out_size,
29303b0cc5fbSCristian Dumitrescu 		 void *obj __rte_unused)
29313b0cc5fbSCristian Dumitrescu {
29323b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec_params p;
29333b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
29343b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name;
29353b0cc5fbSCristian Dumitrescu 	uint32_t numa_node;
29363b0cc5fbSCristian Dumitrescu 	int status;
29373b0cc5fbSCristian Dumitrescu 
29383b0cc5fbSCristian Dumitrescu 	if (n_tokens != 20) {
29393b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
29403b0cc5fbSCristian Dumitrescu 		return;
29413b0cc5fbSCristian Dumitrescu 	}
29423b0cc5fbSCristian Dumitrescu 
29433b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
29443b0cc5fbSCristian Dumitrescu 
29453b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "create")) {
29463b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "create");
29473b0cc5fbSCristian Dumitrescu 		return;
29483b0cc5fbSCristian Dumitrescu 	}
29493b0cc5fbSCristian Dumitrescu 
29503b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "in")) {
29513b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "in");
29523b0cc5fbSCristian Dumitrescu 		return;
29533b0cc5fbSCristian Dumitrescu 	}
29543b0cc5fbSCristian Dumitrescu 
29553b0cc5fbSCristian Dumitrescu 	p.ring_in_name = tokens[4];
29563b0cc5fbSCristian Dumitrescu 
29573b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[5], "out")) {
29583b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "out");
29593b0cc5fbSCristian Dumitrescu 		return;
29603b0cc5fbSCristian Dumitrescu 	}
29613b0cc5fbSCristian Dumitrescu 
29623b0cc5fbSCristian Dumitrescu 	p.ring_out_name = tokens[6];
29633b0cc5fbSCristian Dumitrescu 
29643b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[7], "cryptodev")) {
29653b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptodev");
29663b0cc5fbSCristian Dumitrescu 		return;
29673b0cc5fbSCristian Dumitrescu 	}
29683b0cc5fbSCristian Dumitrescu 
29693b0cc5fbSCristian Dumitrescu 	p.crypto_dev_name = tokens[8];
29703b0cc5fbSCristian Dumitrescu 
29713b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[9], "cryptoq")) {
29723b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cryptoq");
29733b0cc5fbSCristian Dumitrescu 		return;
29743b0cc5fbSCristian Dumitrescu 	}
29753b0cc5fbSCristian Dumitrescu 
29763b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.crypto_dev_queue_pair_id, tokens[10])) {
29773b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_dev_queue_pair_id");
29783b0cc5fbSCristian Dumitrescu 		return;
29793b0cc5fbSCristian Dumitrescu 	}
29803b0cc5fbSCristian Dumitrescu 
29813b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[11], "bsz")) {
29823b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "bsz");
29833b0cc5fbSCristian Dumitrescu 		return;
29843b0cc5fbSCristian Dumitrescu 	}
29853b0cc5fbSCristian Dumitrescu 
29863b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.ring_rd, tokens[12])) {
29873b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ring_rd_bsz");
29883b0cc5fbSCristian Dumitrescu 		return;
29893b0cc5fbSCristian Dumitrescu 	}
29903b0cc5fbSCristian Dumitrescu 
29913b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.ring_wr, tokens[13])) {
29923b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ring_wr_bsz");
29933b0cc5fbSCristian Dumitrescu 		return;
29943b0cc5fbSCristian Dumitrescu 	}
29953b0cc5fbSCristian Dumitrescu 
29963b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.crypto_wr, tokens[14])) {
29973b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_wr_bsz");
29983b0cc5fbSCristian Dumitrescu 		return;
29993b0cc5fbSCristian Dumitrescu 	}
30003b0cc5fbSCristian Dumitrescu 
30013b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.bsz.crypto_rd, tokens[15])) {
30023b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "crypto_rd_bsz");
30033b0cc5fbSCristian Dumitrescu 		return;
30043b0cc5fbSCristian Dumitrescu 	}
30053b0cc5fbSCristian Dumitrescu 
30063b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[16], "samax")) {
30073b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "samax");
30083b0cc5fbSCristian Dumitrescu 		return;
30093b0cc5fbSCristian Dumitrescu 	}
30103b0cc5fbSCristian Dumitrescu 
30113b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&p.n_sa_max, tokens[17])) {
30123b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "n_sa_max");
30133b0cc5fbSCristian Dumitrescu 		return;
30143b0cc5fbSCristian Dumitrescu 	}
30153b0cc5fbSCristian Dumitrescu 
30163b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[18], "numa")) {
30173b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "numa");
30183b0cc5fbSCristian Dumitrescu 		return;
30193b0cc5fbSCristian Dumitrescu 	}
30203b0cc5fbSCristian Dumitrescu 
30213b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&numa_node, tokens[19])) {
30223b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "numa_node");
30233b0cc5fbSCristian Dumitrescu 		return;
30243b0cc5fbSCristian Dumitrescu 	}
30253b0cc5fbSCristian Dumitrescu 
30263b0cc5fbSCristian Dumitrescu 	status = rte_swx_ipsec_create(&ipsec,
30273b0cc5fbSCristian Dumitrescu 				      ipsec_instance_name,
30283b0cc5fbSCristian Dumitrescu 				      &p,
30293b0cc5fbSCristian Dumitrescu 				      (int)numa_node);
30303b0cc5fbSCristian Dumitrescu 	if (status)
30313b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "IPsec instance creation failed (%d).\n", status);
30323b0cc5fbSCristian Dumitrescu }
30333b0cc5fbSCristian Dumitrescu 
30343b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_sa_add_help[] =
30353b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> sa add <file_name>\n";
30363b0cc5fbSCristian Dumitrescu 
30373b0cc5fbSCristian Dumitrescu static void
30383b0cc5fbSCristian Dumitrescu cmd_ipsec_sa_add(char **tokens,
30393b0cc5fbSCristian Dumitrescu 		 uint32_t n_tokens,
30403b0cc5fbSCristian Dumitrescu 		 char *out,
30413b0cc5fbSCristian Dumitrescu 		 size_t out_size,
30423b0cc5fbSCristian Dumitrescu 		 void *obj __rte_unused)
30433b0cc5fbSCristian Dumitrescu {
30443b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
30453b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name, *file_name, *line = NULL;
30463b0cc5fbSCristian Dumitrescu 	FILE *file = NULL;
30473b0cc5fbSCristian Dumitrescu 	uint32_t line_id = 0;
30483b0cc5fbSCristian Dumitrescu 
30493b0cc5fbSCristian Dumitrescu 	if (n_tokens != 5) {
30503b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
30513b0cc5fbSCristian Dumitrescu 		return;
30523b0cc5fbSCristian Dumitrescu 	}
30533b0cc5fbSCristian Dumitrescu 
30543b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
30553b0cc5fbSCristian Dumitrescu 	ipsec = rte_swx_ipsec_find(ipsec_instance_name);
30563b0cc5fbSCristian Dumitrescu 	if (!ipsec) {
30573b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ipsec_instance_name");
30583b0cc5fbSCristian Dumitrescu 		goto free;
30593b0cc5fbSCristian Dumitrescu 	}
30603b0cc5fbSCristian Dumitrescu 
30613b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "sa")) {
30623b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "sa");
30633b0cc5fbSCristian Dumitrescu 		goto free;
30643b0cc5fbSCristian Dumitrescu 	}
30653b0cc5fbSCristian Dumitrescu 
30663b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "add")) {
30673b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "add");
30683b0cc5fbSCristian Dumitrescu 		goto free;
30693b0cc5fbSCristian Dumitrescu 	}
30703b0cc5fbSCristian Dumitrescu 
30713b0cc5fbSCristian Dumitrescu 	file_name = tokens[4];
30723b0cc5fbSCristian Dumitrescu 	file = fopen(file_name, "r");
30733b0cc5fbSCristian Dumitrescu 	if (!file) {
30743b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "Cannot open file %s.\n", file_name);
30753b0cc5fbSCristian Dumitrescu 		goto free;
30763b0cc5fbSCristian Dumitrescu 	}
30773b0cc5fbSCristian Dumitrescu 
30783b0cc5fbSCristian Dumitrescu 	/* Buffer allocation. */
30793b0cc5fbSCristian Dumitrescu 	line = malloc(MAX_LINE_SIZE);
30803b0cc5fbSCristian Dumitrescu 	if (!line) {
30813b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_OUT_OF_MEMORY);
30823b0cc5fbSCristian Dumitrescu 		goto free;
30833b0cc5fbSCristian Dumitrescu 	}
30843b0cc5fbSCristian Dumitrescu 
30853b0cc5fbSCristian Dumitrescu 	/* File read. */
30863b0cc5fbSCristian Dumitrescu 	for (line_id = 1; ; line_id++) {
30873b0cc5fbSCristian Dumitrescu 		struct rte_swx_ipsec_sa_params *sa;
30883b0cc5fbSCristian Dumitrescu 		const char *err_msg;
30893b0cc5fbSCristian Dumitrescu 		uint32_t sa_id = 0;
30903b0cc5fbSCristian Dumitrescu 		int is_blank_or_comment, status = 0;
30913b0cc5fbSCristian Dumitrescu 
30923b0cc5fbSCristian Dumitrescu 		if (fgets(line, MAX_LINE_SIZE, file) == NULL)
30933b0cc5fbSCristian Dumitrescu 			break;
30943b0cc5fbSCristian Dumitrescu 
30953b0cc5fbSCristian Dumitrescu 		/* Read SA from file. */
30963b0cc5fbSCristian Dumitrescu 		sa = rte_swx_ipsec_sa_read(ipsec, line, &is_blank_or_comment, &err_msg);
30973b0cc5fbSCristian Dumitrescu 		if (!sa) {
30983b0cc5fbSCristian Dumitrescu 			if (is_blank_or_comment)
30993b0cc5fbSCristian Dumitrescu 				continue;
31003b0cc5fbSCristian Dumitrescu 
31013b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "Invalid SA in file \"%s\" at line %u: \"%s\"\n",
31023b0cc5fbSCristian Dumitrescu 				file_name, line_id, err_msg);
31033b0cc5fbSCristian Dumitrescu 			goto free;
31043b0cc5fbSCristian Dumitrescu 		}
31053b0cc5fbSCristian Dumitrescu 
31063b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "%s", line);
31073b0cc5fbSCristian Dumitrescu 		out_size -= strlen(out);
31083b0cc5fbSCristian Dumitrescu 		out += strlen(out);
31093b0cc5fbSCristian Dumitrescu 
31103b0cc5fbSCristian Dumitrescu 		/* Add the SA to the IPsec instance. Free the SA. */
31113b0cc5fbSCristian Dumitrescu 		status = rte_swx_ipsec_sa_add(ipsec, sa, &sa_id);
31123b0cc5fbSCristian Dumitrescu 		if (status)
31133b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "\t: Error (%d)\n", status);
31143b0cc5fbSCristian Dumitrescu 		else
31153b0cc5fbSCristian Dumitrescu 			snprintf(out, out_size, "\t: OK (SA ID = %u)\n", sa_id);
31163b0cc5fbSCristian Dumitrescu 		out_size -= strlen(out);
31173b0cc5fbSCristian Dumitrescu 		out += strlen(out);
31183b0cc5fbSCristian Dumitrescu 
31193b0cc5fbSCristian Dumitrescu 		free(sa);
31203b0cc5fbSCristian Dumitrescu 		if (status)
31213b0cc5fbSCristian Dumitrescu 			goto free;
31223b0cc5fbSCristian Dumitrescu 	}
31233b0cc5fbSCristian Dumitrescu 
31243b0cc5fbSCristian Dumitrescu free:
31253b0cc5fbSCristian Dumitrescu 	if (file)
31263b0cc5fbSCristian Dumitrescu 		fclose(file);
31273b0cc5fbSCristian Dumitrescu 	free(line);
31283b0cc5fbSCristian Dumitrescu }
31293b0cc5fbSCristian Dumitrescu 
31303b0cc5fbSCristian Dumitrescu static const char cmd_ipsec_sa_delete_help[] =
31313b0cc5fbSCristian Dumitrescu "ipsec <ipsec_instance_name> sa delete <sa_id>\n";
31323b0cc5fbSCristian Dumitrescu 
31333b0cc5fbSCristian Dumitrescu static void
31343b0cc5fbSCristian Dumitrescu cmd_ipsec_sa_delete(char **tokens,
31353b0cc5fbSCristian Dumitrescu 		    uint32_t n_tokens,
31363b0cc5fbSCristian Dumitrescu 		    char *out,
31373b0cc5fbSCristian Dumitrescu 		    size_t out_size,
31383b0cc5fbSCristian Dumitrescu 		    void *obj __rte_unused)
31393b0cc5fbSCristian Dumitrescu {
31403b0cc5fbSCristian Dumitrescu 	struct rte_swx_ipsec *ipsec;
31413b0cc5fbSCristian Dumitrescu 	char *ipsec_instance_name;
31423b0cc5fbSCristian Dumitrescu 	uint32_t sa_id;
31433b0cc5fbSCristian Dumitrescu 
31443b0cc5fbSCristian Dumitrescu 	if (n_tokens != 5) {
31453b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
31463b0cc5fbSCristian Dumitrescu 		return;
31473b0cc5fbSCristian Dumitrescu 	}
31483b0cc5fbSCristian Dumitrescu 
31493b0cc5fbSCristian Dumitrescu 	ipsec_instance_name = tokens[1];
31503b0cc5fbSCristian Dumitrescu 	ipsec = rte_swx_ipsec_find(ipsec_instance_name);
31513b0cc5fbSCristian Dumitrescu 	if (!ipsec) {
31523b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "ipsec_instance_name");
31533b0cc5fbSCristian Dumitrescu 		return;
31543b0cc5fbSCristian Dumitrescu 	}
31553b0cc5fbSCristian Dumitrescu 
31563b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[2], "sa")) {
31573b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "sa");
31583b0cc5fbSCristian Dumitrescu 		return;
31593b0cc5fbSCristian Dumitrescu 	}
31603b0cc5fbSCristian Dumitrescu 
31613b0cc5fbSCristian Dumitrescu 	if (strcmp(tokens[3], "delete")) {
31623b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "delete");
31633b0cc5fbSCristian Dumitrescu 		return;
31643b0cc5fbSCristian Dumitrescu 	}
31653b0cc5fbSCristian Dumitrescu 
31663b0cc5fbSCristian Dumitrescu 	if (parser_read_uint32(&sa_id, tokens[4])) {
31673b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "sa_id");
31683b0cc5fbSCristian Dumitrescu 		return;
31693b0cc5fbSCristian Dumitrescu 	}
31703b0cc5fbSCristian Dumitrescu 
31713b0cc5fbSCristian Dumitrescu 	rte_swx_ipsec_sa_delete(ipsec, sa_id);
31723b0cc5fbSCristian Dumitrescu }
31733b0cc5fbSCristian Dumitrescu 
3174*41f5dfcbSCristian Dumitrescu static const char cmd_pipeline_enable_help[] =
3175*41f5dfcbSCristian Dumitrescu "pipeline <pipeline_name> enable thread <thread_id>\n";
31765074e1d5SCristian Dumitrescu 
31775074e1d5SCristian Dumitrescu static void
3178*41f5dfcbSCristian Dumitrescu cmd_pipeline_enable(char **tokens,
31795074e1d5SCristian Dumitrescu 		    uint32_t n_tokens,
31805074e1d5SCristian Dumitrescu 		    char *out,
31815074e1d5SCristian Dumitrescu 		    size_t out_size,
3182b9559f94SCristian Dumitrescu 		    void *obj __rte_unused)
31835074e1d5SCristian Dumitrescu {
31845074e1d5SCristian Dumitrescu 	char *pipeline_name;
3185b9559f94SCristian Dumitrescu 	struct rte_swx_pipeline *p;
31865074e1d5SCristian Dumitrescu 	uint32_t thread_id;
31875074e1d5SCristian Dumitrescu 	int status;
31885074e1d5SCristian Dumitrescu 
31895074e1d5SCristian Dumitrescu 	if (n_tokens != 5) {
31905074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
31915074e1d5SCristian Dumitrescu 		return;
31925074e1d5SCristian Dumitrescu 	}
31935074e1d5SCristian Dumitrescu 
3194*41f5dfcbSCristian Dumitrescu 	pipeline_name = tokens[1];
3195b9559f94SCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
3196b9559f94SCristian Dumitrescu 	if (!p) {
31975074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
31985074e1d5SCristian Dumitrescu 		return;
31995074e1d5SCristian Dumitrescu 	}
32005074e1d5SCristian Dumitrescu 
3201*41f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[2], "enable") != 0) {
3202*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "enable");
3203*41f5dfcbSCristian Dumitrescu 		return;
3204*41f5dfcbSCristian Dumitrescu 	}
3205*41f5dfcbSCristian Dumitrescu 
3206*41f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[3], "thread") != 0) {
3207*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "thread");
3208*41f5dfcbSCristian Dumitrescu 		return;
3209*41f5dfcbSCristian Dumitrescu 	}
3210*41f5dfcbSCristian Dumitrescu 
3211*41f5dfcbSCristian Dumitrescu 	if (parser_read_uint32(&thread_id, tokens[4]) != 0) {
3212*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "thread_id");
3213*41f5dfcbSCristian Dumitrescu 		return;
3214*41f5dfcbSCristian Dumitrescu 	}
3215*41f5dfcbSCristian Dumitrescu 
3216*41f5dfcbSCristian Dumitrescu 	status = pipeline_enable(p, thread_id);
3217*41f5dfcbSCristian Dumitrescu 	if (status) {
3218*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_CMD_FAIL, "pipeline enable");
3219*41f5dfcbSCristian Dumitrescu 		return;
3220*41f5dfcbSCristian Dumitrescu 	}
3221*41f5dfcbSCristian Dumitrescu }
3222*41f5dfcbSCristian Dumitrescu 
3223*41f5dfcbSCristian Dumitrescu static const char cmd_pipeline_disable_help[] =
3224*41f5dfcbSCristian Dumitrescu "pipeline <pipeline_name> disable\n";
3225*41f5dfcbSCristian Dumitrescu 
3226*41f5dfcbSCristian Dumitrescu static void
3227*41f5dfcbSCristian Dumitrescu cmd_pipeline_disable(char **tokens,
3228*41f5dfcbSCristian Dumitrescu 		     uint32_t n_tokens,
3229*41f5dfcbSCristian Dumitrescu 		     char *out,
3230*41f5dfcbSCristian Dumitrescu 		     size_t out_size,
3231*41f5dfcbSCristian Dumitrescu 		     void *obj __rte_unused)
3232*41f5dfcbSCristian Dumitrescu {
3233*41f5dfcbSCristian Dumitrescu 	struct rte_swx_pipeline *p;
3234*41f5dfcbSCristian Dumitrescu 	char *pipeline_name;
3235*41f5dfcbSCristian Dumitrescu 
3236*41f5dfcbSCristian Dumitrescu 	if (n_tokens != 3) {
3237*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
3238*41f5dfcbSCristian Dumitrescu 		return;
3239*41f5dfcbSCristian Dumitrescu 	}
3240*41f5dfcbSCristian Dumitrescu 
3241*41f5dfcbSCristian Dumitrescu 	pipeline_name = tokens[1];
3242*41f5dfcbSCristian Dumitrescu 	p = rte_swx_pipeline_find(pipeline_name);
3243*41f5dfcbSCristian Dumitrescu 	if (!p) {
3244*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_INVALID, "pipeline_name");
3245*41f5dfcbSCristian Dumitrescu 		return;
3246*41f5dfcbSCristian Dumitrescu 	}
3247*41f5dfcbSCristian Dumitrescu 
3248*41f5dfcbSCristian Dumitrescu 	if (strcmp(tokens[2], "disable") != 0) {
32495074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_NOT_FOUND, "disable");
32505074e1d5SCristian Dumitrescu 		return;
32515074e1d5SCristian Dumitrescu 	}
32525074e1d5SCristian Dumitrescu 
3253*41f5dfcbSCristian Dumitrescu 	pipeline_disable(p);
32545074e1d5SCristian Dumitrescu }
32555074e1d5SCristian Dumitrescu 
32565074e1d5SCristian Dumitrescu static void
32575074e1d5SCristian Dumitrescu cmd_help(char **tokens,
32585074e1d5SCristian Dumitrescu 	 uint32_t n_tokens,
32595074e1d5SCristian Dumitrescu 	 char *out,
32605074e1d5SCristian Dumitrescu 	 size_t out_size,
32615074e1d5SCristian Dumitrescu 	 void *arg __rte_unused)
32625074e1d5SCristian Dumitrescu {
32635074e1d5SCristian Dumitrescu 	tokens++;
32645074e1d5SCristian Dumitrescu 	n_tokens--;
32655074e1d5SCristian Dumitrescu 
32665074e1d5SCristian Dumitrescu 	if (n_tokens == 0) {
32675074e1d5SCristian Dumitrescu 		snprintf(out, out_size,
32687fef9ef1SYogesh Jangra 			"Type 'help <command>' for command details.\n\n"
32697fef9ef1SYogesh Jangra 			"List of commands:\n"
32707fef9ef1SYogesh Jangra 			"\tmempool\n"
3271f31c80f8SCristian Dumitrescu 			"\tethdev\n"
327278dffe31SCristian Dumitrescu 			"\tethdev show\n"
3273607dd517SCristian Dumitrescu 			"\tring\n"
32741b41a527SCristian Dumitrescu 			"\tcryptodev\n"
32759043f66aSCristian Dumitrescu 			"\tpipeline codegen\n"
32766bc14d9fSCristian Dumitrescu 			"\tpipeline libbuild\n"
32777fef9ef1SYogesh Jangra 			"\tpipeline build\n"
327875129cebSChurchill Khangar 			"\tpipeline table add\n"
327975129cebSChurchill Khangar 			"\tpipeline table delete\n"
328075129cebSChurchill Khangar 			"\tpipeline table default\n"
328175129cebSChurchill Khangar 			"\tpipeline table show\n"
3282598fe0ddSCristian Dumitrescu 			"\tpipeline selector group add\n"
3283598fe0ddSCristian Dumitrescu 			"\tpipeline selector group delete\n"
3284598fe0ddSCristian Dumitrescu 			"\tpipeline selector group member add\n"
3285598fe0ddSCristian Dumitrescu 			"\tpipeline selector group member delete\n"
3286598fe0ddSCristian Dumitrescu 			"\tpipeline selector show\n"
32878bd4862fSCristian Dumitrescu 			"\tpipeline learner default\n"
328875129cebSChurchill Khangar 			"\tpipeline commit\n"
328975129cebSChurchill Khangar 			"\tpipeline abort\n"
329064cfcebdSCristian Dumitrescu 			"\tpipeline regrd\n"
329164cfcebdSCristian Dumitrescu 			"\tpipeline regwr\n"
3292f38913b7SCristian Dumitrescu 			"\tpipeline meter profile add\n"
3293f38913b7SCristian Dumitrescu 			"\tpipeline meter profile delete\n"
3294f38913b7SCristian Dumitrescu 			"\tpipeline meter reset\n"
3295f38913b7SCristian Dumitrescu 			"\tpipeline meter set\n"
3296f38913b7SCristian Dumitrescu 			"\tpipeline meter stats\n"
32977fef9ef1SYogesh Jangra 			"\tpipeline stats\n"
329817225455SCristian Dumitrescu 			"\tpipeline mirror session\n"
3299*41f5dfcbSCristian Dumitrescu 			"\tpipeline enable\n"
3300*41f5dfcbSCristian Dumitrescu 			"\tpipeline disable\n\n"
33013b0cc5fbSCristian Dumitrescu 			"\tipsec create\n"
33023b0cc5fbSCristian Dumitrescu 			"\tipsec sa add\n"
33033b0cc5fbSCristian Dumitrescu 			"\tipsec sa delete\n"
3304*41f5dfcbSCristian Dumitrescu 			);
33055074e1d5SCristian Dumitrescu 		return;
33065074e1d5SCristian Dumitrescu 	}
33075074e1d5SCristian Dumitrescu 
33085074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "mempool") == 0) {
33095074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_mempool_help);
33105074e1d5SCristian Dumitrescu 		return;
33115074e1d5SCristian Dumitrescu 	}
33125074e1d5SCristian Dumitrescu 
331378dffe31SCristian Dumitrescu 	if (!strcmp(tokens[0], "ethdev")) {
331478dffe31SCristian Dumitrescu 		if (n_tokens == 1) {
3315f31c80f8SCristian Dumitrescu 			snprintf(out, out_size, "\n%s\n", cmd_ethdev_help);
33165074e1d5SCristian Dumitrescu 			return;
33175074e1d5SCristian Dumitrescu 		}
33185074e1d5SCristian Dumitrescu 
331978dffe31SCristian Dumitrescu 		if (n_tokens == 2 && !strcmp(tokens[1], "show")) {
332078dffe31SCristian Dumitrescu 			snprintf(out, out_size, "\n%s\n", cmd_ethdev_show_help);
332178dffe31SCristian Dumitrescu 			return;
332278dffe31SCristian Dumitrescu 		}
332378dffe31SCristian Dumitrescu 	}
332478dffe31SCristian Dumitrescu 
332577a41301SCristian Dumitrescu 	if (strcmp(tokens[0], "ring") == 0) {
332677a41301SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ring_help);
332777a41301SCristian Dumitrescu 		return;
332877a41301SCristian Dumitrescu 	}
332977a41301SCristian Dumitrescu 
33301b41a527SCristian Dumitrescu 	if (!strcmp(tokens[0], "cryptodev")) {
33311b41a527SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_cryptodev_help);
33321b41a527SCristian Dumitrescu 		return;
33331b41a527SCristian Dumitrescu 	}
33341b41a527SCristian Dumitrescu 
33355074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
33369043f66aSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "codegen") == 0)) {
33379043f66aSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_codegen_help);
33389043f66aSCristian Dumitrescu 		return;
33399043f66aSCristian Dumitrescu 	}
33409043f66aSCristian Dumitrescu 
33419043f66aSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
33426bc14d9fSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "libbuild") == 0)) {
33436bc14d9fSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_libbuild_help);
33446bc14d9fSCristian Dumitrescu 		return;
33456bc14d9fSCristian Dumitrescu 	}
33466bc14d9fSCristian Dumitrescu 
33476bc14d9fSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
33487fef9ef1SYogesh Jangra 		(n_tokens == 2) && (strcmp(tokens[1], "build") == 0)) {
33495074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_build_help);
33505074e1d5SCristian Dumitrescu 		return;
33515074e1d5SCristian Dumitrescu 	}
33525074e1d5SCristian Dumitrescu 
33535074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
33547fef9ef1SYogesh Jangra 		(n_tokens == 3) &&
33557fef9ef1SYogesh Jangra 		(strcmp(tokens[1], "table") == 0) &&
335675129cebSChurchill Khangar 		(strcmp(tokens[2], "add") == 0)) {
33575074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
335875129cebSChurchill Khangar 			cmd_pipeline_table_add_help);
335975129cebSChurchill Khangar 		return;
336075129cebSChurchill Khangar 	}
336175129cebSChurchill Khangar 
336275129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
336375129cebSChurchill Khangar 		(n_tokens == 3) &&
336475129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
336575129cebSChurchill Khangar 		(strcmp(tokens[2], "delete") == 0)) {
336675129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
336775129cebSChurchill Khangar 			cmd_pipeline_table_delete_help);
336875129cebSChurchill Khangar 		return;
336975129cebSChurchill Khangar 	}
337075129cebSChurchill Khangar 
337175129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
337275129cebSChurchill Khangar 		(n_tokens == 3) &&
337375129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
337475129cebSChurchill Khangar 		(strcmp(tokens[2], "default") == 0)) {
337575129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
337675129cebSChurchill Khangar 			cmd_pipeline_table_default_help);
337775129cebSChurchill Khangar 		return;
337875129cebSChurchill Khangar 	}
337975129cebSChurchill Khangar 
338075129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
338175129cebSChurchill Khangar 		(n_tokens == 3) &&
338275129cebSChurchill Khangar 		(strcmp(tokens[1], "table") == 0) &&
338375129cebSChurchill Khangar 		(strcmp(tokens[2], "show") == 0)) {
338475129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
338575129cebSChurchill Khangar 			cmd_pipeline_table_show_help);
338675129cebSChurchill Khangar 		return;
338775129cebSChurchill Khangar 	}
338875129cebSChurchill Khangar 
338975129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3390598fe0ddSCristian Dumitrescu 		(n_tokens == 4) &&
3391598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3392598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3393598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "add") == 0)) {
3394598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3395598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_add_help);
3396598fe0ddSCristian Dumitrescu 		return;
3397598fe0ddSCristian Dumitrescu 	}
3398598fe0ddSCristian Dumitrescu 
3399598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3400598fe0ddSCristian Dumitrescu 		(n_tokens == 4) &&
3401598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3402598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3403598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "delete") == 0)) {
3404598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3405598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_delete_help);
3406598fe0ddSCristian Dumitrescu 		return;
3407598fe0ddSCristian Dumitrescu 	}
3408598fe0ddSCristian Dumitrescu 
3409598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3410598fe0ddSCristian Dumitrescu 		(n_tokens == 5) &&
3411598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3412598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3413598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "member") == 0) &&
3414598fe0ddSCristian Dumitrescu 		(strcmp(tokens[4], "add") == 0)) {
3415598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3416598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_add_help);
3417598fe0ddSCristian Dumitrescu 		return;
3418598fe0ddSCristian Dumitrescu 	}
3419598fe0ddSCristian Dumitrescu 
3420598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3421598fe0ddSCristian Dumitrescu 		(n_tokens == 5) &&
3422598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3423598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "group") == 0) &&
3424598fe0ddSCristian Dumitrescu 		(strcmp(tokens[3], "member") == 0) &&
3425598fe0ddSCristian Dumitrescu 		(strcmp(tokens[4], "delete") == 0)) {
3426598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3427598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_delete_help);
3428598fe0ddSCristian Dumitrescu 		return;
3429598fe0ddSCristian Dumitrescu 	}
3430598fe0ddSCristian Dumitrescu 
3431598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
3432598fe0ddSCristian Dumitrescu 		(n_tokens == 3) &&
3433598fe0ddSCristian Dumitrescu 		(strcmp(tokens[1], "selector") == 0) &&
3434598fe0ddSCristian Dumitrescu 		(strcmp(tokens[2], "show") == 0)) {
3435598fe0ddSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
3436598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_show_help);
3437598fe0ddSCristian Dumitrescu 		return;
3438598fe0ddSCristian Dumitrescu 	}
3439598fe0ddSCristian Dumitrescu 
3440598fe0ddSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
34418bd4862fSCristian Dumitrescu 		(n_tokens == 3) &&
34428bd4862fSCristian Dumitrescu 		(strcmp(tokens[1], "learner") == 0) &&
34438bd4862fSCristian Dumitrescu 		(strcmp(tokens[2], "default") == 0)) {
34448bd4862fSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n",
34458bd4862fSCristian Dumitrescu 			cmd_pipeline_learner_default_help);
34468bd4862fSCristian Dumitrescu 		return;
34478bd4862fSCristian Dumitrescu 	}
34488bd4862fSCristian Dumitrescu 
34498bd4862fSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
345075129cebSChurchill Khangar 		(n_tokens == 2) &&
345175129cebSChurchill Khangar 		(strcmp(tokens[1], "commit") == 0)) {
345275129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
345375129cebSChurchill Khangar 			cmd_pipeline_commit_help);
345475129cebSChurchill Khangar 		return;
345575129cebSChurchill Khangar 	}
345675129cebSChurchill Khangar 
345775129cebSChurchill Khangar 	if ((strcmp(tokens[0], "pipeline") == 0) &&
345875129cebSChurchill Khangar 		(n_tokens == 2) &&
345975129cebSChurchill Khangar 		(strcmp(tokens[1], "abort") == 0)) {
346075129cebSChurchill Khangar 		snprintf(out, out_size, "\n%s\n",
346175129cebSChurchill Khangar 			cmd_pipeline_abort_help);
34625074e1d5SCristian Dumitrescu 		return;
34635074e1d5SCristian Dumitrescu 	}
34645074e1d5SCristian Dumitrescu 
34655074e1d5SCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
346664cfcebdSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "regrd") == 0)) {
346764cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_regrd_help);
346864cfcebdSCristian Dumitrescu 		return;
346964cfcebdSCristian Dumitrescu 	}
347064cfcebdSCristian Dumitrescu 
347164cfcebdSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
347264cfcebdSCristian Dumitrescu 		(n_tokens == 2) && (strcmp(tokens[1], "regwr") == 0)) {
347364cfcebdSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_regwr_help);
347464cfcebdSCristian Dumitrescu 		return;
347564cfcebdSCristian Dumitrescu 	}
347664cfcebdSCristian Dumitrescu 
3477f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3478f38913b7SCristian Dumitrescu 		(n_tokens == 4) && !strcmp(tokens[1], "meter")
3479f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "profile")
3480f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[3], "add")) {
3481f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_profile_add_help);
3482f38913b7SCristian Dumitrescu 		return;
3483f38913b7SCristian Dumitrescu 	}
3484f38913b7SCristian Dumitrescu 
3485f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3486f38913b7SCristian Dumitrescu 		(n_tokens == 4) && !strcmp(tokens[1], "meter")
3487f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "profile")
3488f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[3], "delete")) {
3489f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_profile_delete_help);
3490f38913b7SCristian Dumitrescu 		return;
3491f38913b7SCristian Dumitrescu 	}
3492f38913b7SCristian Dumitrescu 
3493f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3494f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3495f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "reset")) {
3496f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_reset_help);
3497f38913b7SCristian Dumitrescu 		return;
3498f38913b7SCristian Dumitrescu 	}
3499f38913b7SCristian Dumitrescu 
3500f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3501f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3502f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "set")) {
3503f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_set_help);
3504f38913b7SCristian Dumitrescu 		return;
3505f38913b7SCristian Dumitrescu 	}
3506f38913b7SCristian Dumitrescu 
3507f38913b7SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3508f38913b7SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "meter")
3509f38913b7SCristian Dumitrescu 		&& !strcmp(tokens[2], "stats")) {
3510f38913b7SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_meter_stats_help);
3511f38913b7SCristian Dumitrescu 		return;
3512f38913b7SCristian Dumitrescu 	}
3513f38913b7SCristian Dumitrescu 
351464cfcebdSCristian Dumitrescu 	if ((strcmp(tokens[0], "pipeline") == 0) &&
35157fef9ef1SYogesh Jangra 		(n_tokens == 2) && (strcmp(tokens[1], "stats") == 0)) {
35165074e1d5SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_stats_help);
35175074e1d5SCristian Dumitrescu 		return;
35185074e1d5SCristian Dumitrescu 	}
35195074e1d5SCristian Dumitrescu 
352017225455SCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
352117225455SCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "mirror")
352217225455SCristian Dumitrescu 		&& !strcmp(tokens[2], "session")) {
352317225455SCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_mirror_session_help);
352417225455SCristian Dumitrescu 		return;
352517225455SCristian Dumitrescu 	}
352617225455SCristian Dumitrescu 
3527*41f5dfcbSCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3528*41f5dfcbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "enable")) {
3529*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_enable_help);
3530*41f5dfcbSCristian Dumitrescu 		return;
3531*41f5dfcbSCristian Dumitrescu 	}
3532*41f5dfcbSCristian Dumitrescu 
3533*41f5dfcbSCristian Dumitrescu 	if (!strcmp(tokens[0], "pipeline") &&
3534*41f5dfcbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "disable")) {
3535*41f5dfcbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_pipeline_disable_help);
3536*41f5dfcbSCristian Dumitrescu 		return;
3537*41f5dfcbSCristian Dumitrescu 	}
3538*41f5dfcbSCristian Dumitrescu 
35393b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
35403b0cc5fbSCristian Dumitrescu 		(n_tokens == 2) && !strcmp(tokens[1], "create")) {
35413b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_create_help);
35423b0cc5fbSCristian Dumitrescu 		return;
35433b0cc5fbSCristian Dumitrescu 	}
35443b0cc5fbSCristian Dumitrescu 
35453b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
35463b0cc5fbSCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "sa")
35473b0cc5fbSCristian Dumitrescu 		&& !strcmp(tokens[2], "add")) {
35483b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_sa_add_help);
35493b0cc5fbSCristian Dumitrescu 		return;
35503b0cc5fbSCristian Dumitrescu 	}
35513b0cc5fbSCristian Dumitrescu 
35523b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec") &&
35533b0cc5fbSCristian Dumitrescu 		(n_tokens == 3) && !strcmp(tokens[1], "sa")
35543b0cc5fbSCristian Dumitrescu 		&& !strcmp(tokens[2], "delete")) {
35553b0cc5fbSCristian Dumitrescu 		snprintf(out, out_size, "\n%s\n", cmd_ipsec_sa_delete_help);
35563b0cc5fbSCristian Dumitrescu 		return;
35573b0cc5fbSCristian Dumitrescu 	}
35583b0cc5fbSCristian Dumitrescu 
35595074e1d5SCristian Dumitrescu 	snprintf(out, out_size, "Invalid command\n");
35605074e1d5SCristian Dumitrescu }
35615074e1d5SCristian Dumitrescu 
35625074e1d5SCristian Dumitrescu void
35635074e1d5SCristian Dumitrescu cli_process(char *in, char *out, size_t out_size, void *obj)
35645074e1d5SCristian Dumitrescu {
35655074e1d5SCristian Dumitrescu 	char *tokens[CMD_MAX_TOKENS];
35665074e1d5SCristian Dumitrescu 	uint32_t n_tokens = RTE_DIM(tokens);
35675074e1d5SCristian Dumitrescu 	int status;
35685074e1d5SCristian Dumitrescu 
35695074e1d5SCristian Dumitrescu 	if (is_comment(in))
35705074e1d5SCristian Dumitrescu 		return;
35715074e1d5SCristian Dumitrescu 
35725074e1d5SCristian Dumitrescu 	status = parse_tokenize_string(in, tokens, &n_tokens);
35735074e1d5SCristian Dumitrescu 	if (status) {
35745074e1d5SCristian Dumitrescu 		snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
35755074e1d5SCristian Dumitrescu 		return;
35765074e1d5SCristian Dumitrescu 	}
35775074e1d5SCristian Dumitrescu 
35785074e1d5SCristian Dumitrescu 	if (n_tokens == 0)
35795074e1d5SCristian Dumitrescu 		return;
35805074e1d5SCristian Dumitrescu 
35815074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "help") == 0) {
35825074e1d5SCristian Dumitrescu 		cmd_help(tokens, n_tokens, out, out_size, obj);
35835074e1d5SCristian Dumitrescu 		return;
35845074e1d5SCristian Dumitrescu 	}
35855074e1d5SCristian Dumitrescu 
35865074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "mempool") == 0) {
35875074e1d5SCristian Dumitrescu 		cmd_mempool(tokens, n_tokens, out, out_size, obj);
35885074e1d5SCristian Dumitrescu 		return;
35895074e1d5SCristian Dumitrescu 	}
35905074e1d5SCristian Dumitrescu 
3591f31c80f8SCristian Dumitrescu 	if (strcmp(tokens[0], "ethdev") == 0) {
3592821848f5SCristian Dumitrescu 		if ((n_tokens >= 2) && (strcmp(tokens[1], "show") == 0)) {
3593f31c80f8SCristian Dumitrescu 			cmd_ethdev_show(tokens, n_tokens, out, out_size, obj);
35945074e1d5SCristian Dumitrescu 			return;
35955074e1d5SCristian Dumitrescu 		}
35965074e1d5SCristian Dumitrescu 
3597f31c80f8SCristian Dumitrescu 		cmd_ethdev(tokens, n_tokens, out, out_size, obj);
35985074e1d5SCristian Dumitrescu 		return;
35995074e1d5SCristian Dumitrescu 	}
36005074e1d5SCristian Dumitrescu 
360177a41301SCristian Dumitrescu 	if (strcmp(tokens[0], "ring") == 0) {
360277a41301SCristian Dumitrescu 		cmd_ring(tokens, n_tokens, out, out_size, obj);
360377a41301SCristian Dumitrescu 		return;
360477a41301SCristian Dumitrescu 	}
360577a41301SCristian Dumitrescu 
36061b41a527SCristian Dumitrescu 	if (!strcmp(tokens[0], "cryptodev")) {
36071b41a527SCristian Dumitrescu 		cmd_cryptodev(tokens, n_tokens, out, out_size, obj);
36081b41a527SCristian Dumitrescu 		return;
36091b41a527SCristian Dumitrescu 	}
36101b41a527SCristian Dumitrescu 
36115074e1d5SCristian Dumitrescu 	if (strcmp(tokens[0], "pipeline") == 0) {
36125074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
36139043f66aSCristian Dumitrescu 			(strcmp(tokens[1], "codegen") == 0)) {
36149043f66aSCristian Dumitrescu 			cmd_pipeline_codegen(tokens, n_tokens, out, out_size,
36159043f66aSCristian Dumitrescu 				obj);
36169043f66aSCristian Dumitrescu 			return;
36179043f66aSCristian Dumitrescu 		}
36189043f66aSCristian Dumitrescu 
36199043f66aSCristian Dumitrescu 		if ((n_tokens >= 3) &&
36206bc14d9fSCristian Dumitrescu 			(strcmp(tokens[1], "libbuild") == 0)) {
36216bc14d9fSCristian Dumitrescu 			cmd_pipeline_libbuild(tokens, n_tokens, out, out_size,
36226bc14d9fSCristian Dumitrescu 				obj);
36236bc14d9fSCristian Dumitrescu 			return;
36246bc14d9fSCristian Dumitrescu 		}
36256bc14d9fSCristian Dumitrescu 
36266bc14d9fSCristian Dumitrescu 		if ((n_tokens >= 3) &&
36275074e1d5SCristian Dumitrescu 			(strcmp(tokens[2], "build") == 0)) {
36285074e1d5SCristian Dumitrescu 			cmd_pipeline_build(tokens, n_tokens, out, out_size,
36295074e1d5SCristian Dumitrescu 				obj);
36305074e1d5SCristian Dumitrescu 			return;
36315074e1d5SCristian Dumitrescu 		}
36325074e1d5SCristian Dumitrescu 
363375129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
363475129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
363575129cebSChurchill Khangar 			(strcmp(tokens[4], "add") == 0)) {
363675129cebSChurchill Khangar 			cmd_pipeline_table_add(tokens, n_tokens, out,
363775129cebSChurchill Khangar 				out_size, obj);
363875129cebSChurchill Khangar 			return;
363975129cebSChurchill Khangar 		}
364075129cebSChurchill Khangar 
364175129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
364275129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
364375129cebSChurchill Khangar 			(strcmp(tokens[4], "delete") == 0)) {
364475129cebSChurchill Khangar 			cmd_pipeline_table_delete(tokens, n_tokens, out,
364575129cebSChurchill Khangar 				out_size, obj);
364675129cebSChurchill Khangar 			return;
364775129cebSChurchill Khangar 		}
364875129cebSChurchill Khangar 
364975129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
365075129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
365175129cebSChurchill Khangar 			(strcmp(tokens[4], "default") == 0)) {
365275129cebSChurchill Khangar 			cmd_pipeline_table_default(tokens, n_tokens, out,
365375129cebSChurchill Khangar 				out_size, obj);
365475129cebSChurchill Khangar 			return;
365575129cebSChurchill Khangar 		}
365675129cebSChurchill Khangar 
365775129cebSChurchill Khangar 		if ((n_tokens >= 5) &&
365875129cebSChurchill Khangar 			(strcmp(tokens[2], "table") == 0) &&
365975129cebSChurchill Khangar 			(strcmp(tokens[4], "show") == 0)) {
366075129cebSChurchill Khangar 			cmd_pipeline_table_show(tokens, n_tokens, out,
366175129cebSChurchill Khangar 				out_size, obj);
366275129cebSChurchill Khangar 			return;
366375129cebSChurchill Khangar 		}
366475129cebSChurchill Khangar 
3665598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 6) &&
3666598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3667598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3668598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "add") == 0)) {
3669598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_add(tokens, n_tokens, out,
3670598fe0ddSCristian Dumitrescu 				out_size, obj);
3671598fe0ddSCristian Dumitrescu 			return;
3672598fe0ddSCristian Dumitrescu 		}
3673598fe0ddSCristian Dumitrescu 
3674598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 6) &&
3675598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3676598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3677598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "delete") == 0)) {
3678598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_delete(tokens, n_tokens, out,
3679598fe0ddSCristian Dumitrescu 				out_size, obj);
3680598fe0ddSCristian Dumitrescu 			return;
3681598fe0ddSCristian Dumitrescu 		}
3682598fe0ddSCristian Dumitrescu 
3683598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 7) &&
3684598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3685598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3686598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "member") == 0) &&
3687598fe0ddSCristian Dumitrescu 			(strcmp(tokens[6], "add") == 0)) {
3688598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_add(tokens, n_tokens, out,
3689598fe0ddSCristian Dumitrescu 				out_size, obj);
3690598fe0ddSCristian Dumitrescu 			return;
3691598fe0ddSCristian Dumitrescu 		}
3692598fe0ddSCristian Dumitrescu 
3693598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 7) &&
3694598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3695598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "group") == 0) &&
3696598fe0ddSCristian Dumitrescu 			(strcmp(tokens[5], "member") == 0) &&
3697598fe0ddSCristian Dumitrescu 			(strcmp(tokens[6], "delete") == 0)) {
3698598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_group_member_delete(tokens, n_tokens, out,
3699598fe0ddSCristian Dumitrescu 				out_size, obj);
3700598fe0ddSCristian Dumitrescu 			return;
3701598fe0ddSCristian Dumitrescu 		}
3702598fe0ddSCristian Dumitrescu 
3703598fe0ddSCristian Dumitrescu 		if ((n_tokens >= 5) &&
3704598fe0ddSCristian Dumitrescu 			(strcmp(tokens[2], "selector") == 0) &&
3705598fe0ddSCristian Dumitrescu 			(strcmp(tokens[4], "show") == 0)) {
3706598fe0ddSCristian Dumitrescu 			cmd_pipeline_selector_show(tokens, n_tokens, out,
3707598fe0ddSCristian Dumitrescu 				out_size, obj);
3708598fe0ddSCristian Dumitrescu 			return;
3709598fe0ddSCristian Dumitrescu 		}
3710598fe0ddSCristian Dumitrescu 
37118bd4862fSCristian Dumitrescu 		if ((n_tokens >= 5) &&
37128bd4862fSCristian Dumitrescu 			(strcmp(tokens[2], "learner") == 0) &&
37138bd4862fSCristian Dumitrescu 			(strcmp(tokens[4], "default") == 0)) {
37148bd4862fSCristian Dumitrescu 			cmd_pipeline_learner_default(tokens, n_tokens, out,
37158bd4862fSCristian Dumitrescu 				out_size, obj);
37168bd4862fSCristian Dumitrescu 			return;
37178bd4862fSCristian Dumitrescu 		}
37188bd4862fSCristian Dumitrescu 
37195074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
372075129cebSChurchill Khangar 			(strcmp(tokens[2], "commit") == 0)) {
372175129cebSChurchill Khangar 			cmd_pipeline_commit(tokens, n_tokens, out,
372275129cebSChurchill Khangar 				out_size, obj);
372375129cebSChurchill Khangar 			return;
372475129cebSChurchill Khangar 		}
372575129cebSChurchill Khangar 
372675129cebSChurchill Khangar 		if ((n_tokens >= 3) &&
372775129cebSChurchill Khangar 			(strcmp(tokens[2], "abort") == 0)) {
372875129cebSChurchill Khangar 			cmd_pipeline_abort(tokens, n_tokens, out,
37295074e1d5SCristian Dumitrescu 				out_size, obj);
37305074e1d5SCristian Dumitrescu 			return;
37315074e1d5SCristian Dumitrescu 		}
37325074e1d5SCristian Dumitrescu 
37335074e1d5SCristian Dumitrescu 		if ((n_tokens >= 3) &&
373464cfcebdSCristian Dumitrescu 			(strcmp(tokens[2], "regrd") == 0)) {
373564cfcebdSCristian Dumitrescu 			cmd_pipeline_regrd(tokens, n_tokens, out, out_size, obj);
373664cfcebdSCristian Dumitrescu 			return;
373764cfcebdSCristian Dumitrescu 		}
373864cfcebdSCristian Dumitrescu 
373964cfcebdSCristian Dumitrescu 		if ((n_tokens >= 3) &&
374064cfcebdSCristian Dumitrescu 			(strcmp(tokens[2], "regwr") == 0)) {
374164cfcebdSCristian Dumitrescu 			cmd_pipeline_regwr(tokens, n_tokens, out, out_size, obj);
374264cfcebdSCristian Dumitrescu 			return;
374364cfcebdSCristian Dumitrescu 		}
374464cfcebdSCristian Dumitrescu 
3745f38913b7SCristian Dumitrescu 		if ((n_tokens >= 6) &&
3746f38913b7SCristian Dumitrescu 			(strcmp(tokens[2], "meter") == 0) &&
3747f38913b7SCristian Dumitrescu 			(strcmp(tokens[3], "profile") == 0) &&
3748f38913b7SCristian Dumitrescu 			(strcmp(tokens[5], "add") == 0)) {
3749f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_profile_add(tokens, n_tokens, out, out_size, obj);
3750f38913b7SCristian Dumitrescu 			return;
3751f38913b7SCristian Dumitrescu 		}
3752f38913b7SCristian Dumitrescu 
3753f38913b7SCristian Dumitrescu 		if ((n_tokens >= 6) &&
3754f38913b7SCristian Dumitrescu 			(strcmp(tokens[2], "meter") == 0) &&
3755f38913b7SCristian Dumitrescu 			(strcmp(tokens[3], "profile") == 0) &&
3756f38913b7SCristian Dumitrescu 			(strcmp(tokens[5], "delete") == 0)) {
3757f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_profile_delete(tokens, n_tokens, out, out_size, obj);
3758f38913b7SCristian Dumitrescu 			return;
3759f38913b7SCristian Dumitrescu 		}
3760f38913b7SCristian Dumitrescu 
376112eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "reset")) {
3762f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_reset(tokens, n_tokens, out, out_size, obj);
3763f38913b7SCristian Dumitrescu 			return;
3764f38913b7SCristian Dumitrescu 		}
3765f38913b7SCristian Dumitrescu 
376612eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "set")) {
3767f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_set(tokens, n_tokens, out, out_size, obj);
3768f38913b7SCristian Dumitrescu 			return;
3769f38913b7SCristian Dumitrescu 		}
3770f38913b7SCristian Dumitrescu 
377112eda78dSCristian Dumitrescu 		if (n_tokens >= 9 && !strcmp(tokens[2], "meter") && !strcmp(tokens[4], "stats")) {
3772f38913b7SCristian Dumitrescu 			cmd_pipeline_meter_stats(tokens, n_tokens, out, out_size, obj);
3773f38913b7SCristian Dumitrescu 			return;
3774f38913b7SCristian Dumitrescu 		}
3775f38913b7SCristian Dumitrescu 
377664cfcebdSCristian Dumitrescu 		if ((n_tokens >= 3) &&
37775074e1d5SCristian Dumitrescu 			(strcmp(tokens[2], "stats") == 0)) {
37785074e1d5SCristian Dumitrescu 			cmd_pipeline_stats(tokens, n_tokens, out, out_size,
37795074e1d5SCristian Dumitrescu 				obj);
37805074e1d5SCristian Dumitrescu 			return;
37815074e1d5SCristian Dumitrescu 		}
378217225455SCristian Dumitrescu 
378317225455SCristian Dumitrescu 		if ((n_tokens >= 4) &&
378417225455SCristian Dumitrescu 			(strcmp(tokens[2], "mirror") == 0) &&
378517225455SCristian Dumitrescu 			(strcmp(tokens[3], "session") == 0)) {
378617225455SCristian Dumitrescu 			cmd_pipeline_mirror_session(tokens, n_tokens, out, out_size, obj);
378717225455SCristian Dumitrescu 			return;
378817225455SCristian Dumitrescu 		}
3789*41f5dfcbSCristian Dumitrescu 
3790*41f5dfcbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "enable")) {
3791*41f5dfcbSCristian Dumitrescu 			cmd_pipeline_enable(tokens, n_tokens, out, out_size, obj);
3792*41f5dfcbSCristian Dumitrescu 			return;
3793*41f5dfcbSCristian Dumitrescu 		}
3794*41f5dfcbSCristian Dumitrescu 
3795*41f5dfcbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "disable")) {
3796*41f5dfcbSCristian Dumitrescu 			cmd_pipeline_disable(tokens, n_tokens, out, out_size, obj);
3797*41f5dfcbSCristian Dumitrescu 			return;
3798*41f5dfcbSCristian Dumitrescu 		}
37995074e1d5SCristian Dumitrescu 	}
38005074e1d5SCristian Dumitrescu 
38013b0cc5fbSCristian Dumitrescu 	if (!strcmp(tokens[0], "ipsec")) {
38023b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 3 && !strcmp(tokens[2], "create")) {
38033b0cc5fbSCristian Dumitrescu 			cmd_ipsec_create(tokens, n_tokens, out, out_size, obj);
38043b0cc5fbSCristian Dumitrescu 			return;
38053b0cc5fbSCristian Dumitrescu 		}
38063b0cc5fbSCristian Dumitrescu 
38073b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 4 && !strcmp(tokens[2], "sa") && !strcmp(tokens[3], "add")) {
38083b0cc5fbSCristian Dumitrescu 			cmd_ipsec_sa_add(tokens, n_tokens, out, out_size, obj);
38093b0cc5fbSCristian Dumitrescu 			return;
38103b0cc5fbSCristian Dumitrescu 		}
38113b0cc5fbSCristian Dumitrescu 
38123b0cc5fbSCristian Dumitrescu 		if (n_tokens >= 4 && !strcmp(tokens[2], "sa") && !strcmp(tokens[3], "delete")) {
38133b0cc5fbSCristian Dumitrescu 			cmd_ipsec_sa_delete(tokens, n_tokens, out, out_size, obj);
38143b0cc5fbSCristian Dumitrescu 			return;
38153b0cc5fbSCristian Dumitrescu 		}
38163b0cc5fbSCristian Dumitrescu 	}
38173b0cc5fbSCristian Dumitrescu 
38185074e1d5SCristian Dumitrescu 	snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
38195074e1d5SCristian Dumitrescu }
38205074e1d5SCristian Dumitrescu 
38215074e1d5SCristian Dumitrescu int
38225074e1d5SCristian Dumitrescu cli_script_process(const char *file_name,
38235074e1d5SCristian Dumitrescu 	size_t msg_in_len_max,
38245074e1d5SCristian Dumitrescu 	size_t msg_out_len_max,
38255074e1d5SCristian Dumitrescu 	void *obj)
38265074e1d5SCristian Dumitrescu {
38275074e1d5SCristian Dumitrescu 	char *msg_in = NULL, *msg_out = NULL;
38285074e1d5SCristian Dumitrescu 	FILE *f = NULL;
38295074e1d5SCristian Dumitrescu 
38305074e1d5SCristian Dumitrescu 	/* Check input arguments */
38315074e1d5SCristian Dumitrescu 	if ((file_name == NULL) ||
38325074e1d5SCristian Dumitrescu 		(strlen(file_name) == 0) ||
38335074e1d5SCristian Dumitrescu 		(msg_in_len_max == 0) ||
38345074e1d5SCristian Dumitrescu 		(msg_out_len_max == 0))
38355074e1d5SCristian Dumitrescu 		return -EINVAL;
38365074e1d5SCristian Dumitrescu 
38375074e1d5SCristian Dumitrescu 	msg_in = malloc(msg_in_len_max + 1);
38385074e1d5SCristian Dumitrescu 	msg_out = malloc(msg_out_len_max + 1);
38395074e1d5SCristian Dumitrescu 	if ((msg_in == NULL) ||
38405074e1d5SCristian Dumitrescu 		(msg_out == NULL)) {
38415074e1d5SCristian Dumitrescu 		free(msg_out);
38425074e1d5SCristian Dumitrescu 		free(msg_in);
38435074e1d5SCristian Dumitrescu 		return -ENOMEM;
38445074e1d5SCristian Dumitrescu 	}
38455074e1d5SCristian Dumitrescu 
38465074e1d5SCristian Dumitrescu 	/* Open input file */
38475074e1d5SCristian Dumitrescu 	f = fopen(file_name, "r");
38485074e1d5SCristian Dumitrescu 	if (f == NULL) {
38495074e1d5SCristian Dumitrescu 		free(msg_out);
38505074e1d5SCristian Dumitrescu 		free(msg_in);
38515074e1d5SCristian Dumitrescu 		return -EIO;
38525074e1d5SCristian Dumitrescu 	}
38535074e1d5SCristian Dumitrescu 
38545074e1d5SCristian Dumitrescu 	/* Read file */
38555074e1d5SCristian Dumitrescu 	for ( ; ; ) {
38565074e1d5SCristian Dumitrescu 		if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
38575074e1d5SCristian Dumitrescu 			break;
38585074e1d5SCristian Dumitrescu 
38595074e1d5SCristian Dumitrescu 		printf("%s", msg_in);
38605074e1d5SCristian Dumitrescu 		msg_out[0] = 0;
38615074e1d5SCristian Dumitrescu 
38625074e1d5SCristian Dumitrescu 		cli_process(msg_in,
38635074e1d5SCristian Dumitrescu 			msg_out,
38645074e1d5SCristian Dumitrescu 			msg_out_len_max,
38655074e1d5SCristian Dumitrescu 			obj);
38665074e1d5SCristian Dumitrescu 
38675074e1d5SCristian Dumitrescu 		if (strlen(msg_out))
38685074e1d5SCristian Dumitrescu 			printf("%s", msg_out);
38695074e1d5SCristian Dumitrescu 	}
38705074e1d5SCristian Dumitrescu 
38715074e1d5SCristian Dumitrescu 	/* Close file */
38725074e1d5SCristian Dumitrescu 	fclose(f);
38735074e1d5SCristian Dumitrescu 	free(msg_out);
38745074e1d5SCristian Dumitrescu 	free(msg_in);
38755074e1d5SCristian Dumitrescu 	return 0;
38765074e1d5SCristian Dumitrescu }
3877