xref: /dpdk/examples/ip_pipeline/main.c (revision d75c371e9b46565bab99e40f74973bda73e315e1)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
24bbf8e30SJasvinder Singh  * Copyright(c) 2010-2018 Intel Corporation
377a33467SCristian Dumitrescu  */
477a33467SCristian Dumitrescu 
5fbc74e66SJasvinder Singh #include <stdio.h>
6fbc74e66SJasvinder Singh #include <string.h>
7fbc74e66SJasvinder Singh #include <fcntl.h>
8fbc74e66SJasvinder Singh #include <unistd.h>
9fbc74e66SJasvinder Singh #include <getopt.h>
1077a33467SCristian Dumitrescu 
11fbc74e66SJasvinder Singh #include <rte_eal.h>
1277a33467SCristian Dumitrescu 
134bbf8e30SJasvinder Singh #include "cli.h"
144bbf8e30SJasvinder Singh #include "conn.h"
159a408cc8SJasvinder Singh #include "kni.h"
16133c2c65SJasvinder Singh #include "link.h"
176bfe74f8SJasvinder Singh #include "mempool.h"
18*d75c371eSJasvinder Singh #include "pipeline.h"
198245472cSJasvinder Singh #include "swq.h"
202f74ae28SJasvinder Singh #include "tap.h"
2125961ff3SJasvinder Singh #include "tmgr.h"
224bbf8e30SJasvinder Singh 
234bbf8e30SJasvinder Singh static const char usage[] =
244bbf8e30SJasvinder Singh 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
254bbf8e30SJasvinder Singh 
264bbf8e30SJasvinder Singh static const char welcome[] =
274bbf8e30SJasvinder Singh 	"\n"
284bbf8e30SJasvinder Singh 	"Welcome to IP Pipeline!\n"
294bbf8e30SJasvinder Singh 	"\n";
304bbf8e30SJasvinder Singh 
314bbf8e30SJasvinder Singh static const char prompt[] = "pipeline> ";
324bbf8e30SJasvinder Singh 
334bbf8e30SJasvinder Singh static struct app_params {
344bbf8e30SJasvinder Singh 	struct conn_params conn;
354bbf8e30SJasvinder Singh 	char *script_name;
364bbf8e30SJasvinder Singh } app = {
374bbf8e30SJasvinder Singh 	.conn = {
384bbf8e30SJasvinder Singh 		.welcome = welcome,
394bbf8e30SJasvinder Singh 		.prompt = prompt,
404bbf8e30SJasvinder Singh 		.addr = "0.0.0.0",
414bbf8e30SJasvinder Singh 		.port = 8086,
424bbf8e30SJasvinder Singh 		.buf_size = 1024 * 1024,
434bbf8e30SJasvinder Singh 		.msg_in_len_max = 1024,
444bbf8e30SJasvinder Singh 		.msg_out_len_max = 1024 * 1024,
454bbf8e30SJasvinder Singh 		.msg_handle = cli_process,
464bbf8e30SJasvinder Singh 	},
474bbf8e30SJasvinder Singh 	.script_name = NULL,
484bbf8e30SJasvinder Singh };
494bbf8e30SJasvinder Singh 
504bbf8e30SJasvinder Singh static int
514bbf8e30SJasvinder Singh parse_args(int argc, char **argv)
524bbf8e30SJasvinder Singh {
534bbf8e30SJasvinder Singh 	char *app_name = argv[0];
544bbf8e30SJasvinder Singh 	struct option lgopts[] = {
554bbf8e30SJasvinder Singh 		{ NULL,  0, 0, 0 }
564bbf8e30SJasvinder Singh 	};
574bbf8e30SJasvinder Singh 	int opt, option_index;
584bbf8e30SJasvinder Singh 	int h_present, p_present, s_present, n_args, i;
594bbf8e30SJasvinder Singh 
604bbf8e30SJasvinder Singh 	/* Skip EAL input args */
614bbf8e30SJasvinder Singh 	n_args = argc;
624bbf8e30SJasvinder Singh 	for (i = 0; i < n_args; i++)
634bbf8e30SJasvinder Singh 		if (strcmp(argv[i], "--") == 0) {
644bbf8e30SJasvinder Singh 			argc -= i;
654bbf8e30SJasvinder Singh 			argv += i;
664bbf8e30SJasvinder Singh 			break;
674bbf8e30SJasvinder Singh 		}
684bbf8e30SJasvinder Singh 
694bbf8e30SJasvinder Singh 	if (i == n_args)
704bbf8e30SJasvinder Singh 		return 0;
714bbf8e30SJasvinder Singh 
724bbf8e30SJasvinder Singh 	/* Parse args */
734bbf8e30SJasvinder Singh 	h_present = 0;
744bbf8e30SJasvinder Singh 	p_present = 0;
754bbf8e30SJasvinder Singh 	s_present = 0;
764bbf8e30SJasvinder Singh 
774bbf8e30SJasvinder Singh 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
784bbf8e30SJasvinder Singh 			!= EOF)
794bbf8e30SJasvinder Singh 		switch (opt) {
804bbf8e30SJasvinder Singh 		case 'h':
814bbf8e30SJasvinder Singh 			if (h_present) {
824bbf8e30SJasvinder Singh 				printf("Error: Multiple -h arguments\n");
834bbf8e30SJasvinder Singh 				return -1;
844bbf8e30SJasvinder Singh 			}
854bbf8e30SJasvinder Singh 			h_present = 1;
864bbf8e30SJasvinder Singh 
874bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
884bbf8e30SJasvinder Singh 				printf("Error: Argument for -h not provided\n");
894bbf8e30SJasvinder Singh 				return -1;
904bbf8e30SJasvinder Singh 			}
914bbf8e30SJasvinder Singh 
924bbf8e30SJasvinder Singh 			app.conn.addr = strdup(optarg);
934bbf8e30SJasvinder Singh 			if (app.conn.addr == NULL) {
944bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
954bbf8e30SJasvinder Singh 				return -1;
964bbf8e30SJasvinder Singh 			}
974bbf8e30SJasvinder Singh 			break;
984bbf8e30SJasvinder Singh 
994bbf8e30SJasvinder Singh 		case 'p':
1004bbf8e30SJasvinder Singh 			if (p_present) {
1014bbf8e30SJasvinder Singh 				printf("Error: Multiple -p arguments\n");
1024bbf8e30SJasvinder Singh 				return -1;
1034bbf8e30SJasvinder Singh 			}
1044bbf8e30SJasvinder Singh 			p_present = 1;
1054bbf8e30SJasvinder Singh 
1064bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1074bbf8e30SJasvinder Singh 				printf("Error: Argument for -p not provided\n");
1084bbf8e30SJasvinder Singh 				return -1;
1094bbf8e30SJasvinder Singh 			}
1104bbf8e30SJasvinder Singh 
1114bbf8e30SJasvinder Singh 			app.conn.port = (uint16_t) atoi(optarg);
1124bbf8e30SJasvinder Singh 			break;
1134bbf8e30SJasvinder Singh 
1144bbf8e30SJasvinder Singh 		case 's':
1154bbf8e30SJasvinder Singh 			if (s_present) {
1164bbf8e30SJasvinder Singh 				printf("Error: Multiple -s arguments\n");
1174bbf8e30SJasvinder Singh 				return -1;
1184bbf8e30SJasvinder Singh 			}
1194bbf8e30SJasvinder Singh 			s_present = 1;
1204bbf8e30SJasvinder Singh 
1214bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1224bbf8e30SJasvinder Singh 				printf("Error: Argument for -s not provided\n");
1234bbf8e30SJasvinder Singh 				return -1;
1244bbf8e30SJasvinder Singh 			}
1254bbf8e30SJasvinder Singh 
1264bbf8e30SJasvinder Singh 			app.script_name = strdup(optarg);
1274bbf8e30SJasvinder Singh 			if (app.script_name == NULL) {
1284bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
1294bbf8e30SJasvinder Singh 				return -1;
1304bbf8e30SJasvinder Singh 			}
1314bbf8e30SJasvinder Singh 			break;
1324bbf8e30SJasvinder Singh 
1334bbf8e30SJasvinder Singh 		default:
1344bbf8e30SJasvinder Singh 			printf(usage, app_name);
1354bbf8e30SJasvinder Singh 			return -1;
1364bbf8e30SJasvinder Singh 		}
1374bbf8e30SJasvinder Singh 
1384bbf8e30SJasvinder Singh 	optind = 1; /* reset getopt lib */
1394bbf8e30SJasvinder Singh 
1404bbf8e30SJasvinder Singh 	return 0;
1414bbf8e30SJasvinder Singh }
1424bbf8e30SJasvinder Singh 
14377a33467SCristian Dumitrescu int
14498a16481SDavid Marchand main(int argc, char **argv)
14577a33467SCristian Dumitrescu {
1464bbf8e30SJasvinder Singh 	struct conn *conn;
147fbc74e66SJasvinder Singh 	int status;
14877a33467SCristian Dumitrescu 
1494bbf8e30SJasvinder Singh 	/* Parse application arguments */
1504bbf8e30SJasvinder Singh 	status = parse_args(argc, argv);
1514bbf8e30SJasvinder Singh 	if (status < 0)
1524bbf8e30SJasvinder Singh 		return status;
1534bbf8e30SJasvinder Singh 
154fbc74e66SJasvinder Singh 	/* EAL */
155fbc74e66SJasvinder Singh 	status = rte_eal_init(argc, argv);
156fbc74e66SJasvinder Singh 	if (status < 0) {
157fbc74e66SJasvinder Singh 		printf("Error: EAL initialization failed (%d)\n", status);
158fbc74e66SJasvinder Singh 		return status;
159fbc74e66SJasvinder Singh 	};
16077a33467SCristian Dumitrescu 
1614bbf8e30SJasvinder Singh 	/* Connectivity */
1624bbf8e30SJasvinder Singh 	conn = conn_init(&app.conn);
1634bbf8e30SJasvinder Singh 	if (conn == NULL) {
1644bbf8e30SJasvinder Singh 		printf("Error: Connectivity initialization failed (%d)\n",
1654bbf8e30SJasvinder Singh 			status);
1664bbf8e30SJasvinder Singh 		return status;
1674bbf8e30SJasvinder Singh 	};
1684bbf8e30SJasvinder Singh 
1696bfe74f8SJasvinder Singh 	/* Mempool */
1706bfe74f8SJasvinder Singh 	status = mempool_init();
1716bfe74f8SJasvinder Singh 	if (status) {
1726bfe74f8SJasvinder Singh 		printf("Error: Mempool initialization failed (%d)\n", status);
1736bfe74f8SJasvinder Singh 		return status;
1746bfe74f8SJasvinder Singh 	}
1756bfe74f8SJasvinder Singh 
176133c2c65SJasvinder Singh 	/* Link */
177133c2c65SJasvinder Singh 	status = link_init();
178133c2c65SJasvinder Singh 	if (status) {
179133c2c65SJasvinder Singh 		printf("Error: Link initialization failed (%d)\n", status);
180133c2c65SJasvinder Singh 		return status;
181133c2c65SJasvinder Singh 	}
182133c2c65SJasvinder Singh 
1838245472cSJasvinder Singh 	/* SWQ */
1848245472cSJasvinder Singh 	status = swq_init();
1858245472cSJasvinder Singh 	if (status) {
1868245472cSJasvinder Singh 		printf("Error: SWQ initialization failed (%d)\n", status);
1878245472cSJasvinder Singh 		return status;
1888245472cSJasvinder Singh 	}
1898245472cSJasvinder Singh 
19025961ff3SJasvinder Singh 	/* Traffic Manager */
19125961ff3SJasvinder Singh 	status = tmgr_init();
19225961ff3SJasvinder Singh 	if (status) {
19325961ff3SJasvinder Singh 		printf("Error: TMGR initialization failed (%d)\n", status);
19425961ff3SJasvinder Singh 		return status;
19525961ff3SJasvinder Singh 	}
19625961ff3SJasvinder Singh 
1972f74ae28SJasvinder Singh 	/* TAP */
1982f74ae28SJasvinder Singh 	status = tap_init();
1992f74ae28SJasvinder Singh 	if (status) {
2002f74ae28SJasvinder Singh 		printf("Error: TAP initialization failed (%d)\n", status);
2012f74ae28SJasvinder Singh 		return status;
2022f74ae28SJasvinder Singh 	}
2032f74ae28SJasvinder Singh 
2049a408cc8SJasvinder Singh 	/* KNI */
2059a408cc8SJasvinder Singh 	status = kni_init();
2069a408cc8SJasvinder Singh 	if (status) {
2079a408cc8SJasvinder Singh 		printf("Error: KNI initialization failed (%d)\n", status);
2089a408cc8SJasvinder Singh 		return status;
2099a408cc8SJasvinder Singh 	}
2109a408cc8SJasvinder Singh 
21171937434SJasvinder Singh 	/* Action */
21271937434SJasvinder Singh 	status = port_in_action_profile_init();
21371937434SJasvinder Singh 	if (status) {
21471937434SJasvinder Singh 		printf("Error: Input port action profile initialization failed (%d)\n", status);
21571937434SJasvinder Singh 		return status;
21671937434SJasvinder Singh 	}
21771937434SJasvinder Singh 
21871937434SJasvinder Singh 	status = table_action_profile_init();
21971937434SJasvinder Singh 	if (status) {
22071937434SJasvinder Singh 		printf("Error: Action profile initialization failed (%d)\n",
22171937434SJasvinder Singh 			status);
22271937434SJasvinder Singh 		return status;
22371937434SJasvinder Singh 	}
22471937434SJasvinder Singh 
225*d75c371eSJasvinder Singh 	/* Pipeline */
226*d75c371eSJasvinder Singh 	status = pipeline_init();
227*d75c371eSJasvinder Singh 	if (status) {
228*d75c371eSJasvinder Singh 		printf("Error: Pipeline initialization failed (%d)\n", status);
229*d75c371eSJasvinder Singh 		return status;
230*d75c371eSJasvinder Singh 	}
231*d75c371eSJasvinder Singh 
2324bbf8e30SJasvinder Singh 	/* Script */
2334bbf8e30SJasvinder Singh 	if (app.script_name)
2344bbf8e30SJasvinder Singh 		cli_script_process(app.script_name,
2354bbf8e30SJasvinder Singh 			app.conn.msg_in_len_max,
2364bbf8e30SJasvinder Singh 			app.conn.msg_out_len_max);
2374bbf8e30SJasvinder Singh 
2384bbf8e30SJasvinder Singh 	/* Dispatch loop */
2394bbf8e30SJasvinder Singh 	for ( ; ; ) {
2404bbf8e30SJasvinder Singh 		conn_poll_for_conn(conn);
2414bbf8e30SJasvinder Singh 
2424bbf8e30SJasvinder Singh 		conn_poll_for_msg(conn);
2439a408cc8SJasvinder Singh 
2449a408cc8SJasvinder Singh 		kni_handle_request();
2454bbf8e30SJasvinder Singh 	}
24677a33467SCristian Dumitrescu }
247