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