xref: /dpdk/examples/ip_pipeline/main.c (revision a8bd581de3975b5d7ad95afd227e5d889765680e)
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 
11*a8bd581dSJasvinder Singh #include <rte_launch.h>
12fbc74e66SJasvinder Singh #include <rte_eal.h>
1377a33467SCristian Dumitrescu 
144bbf8e30SJasvinder Singh #include "cli.h"
154bbf8e30SJasvinder Singh #include "conn.h"
169a408cc8SJasvinder Singh #include "kni.h"
17133c2c65SJasvinder Singh #include "link.h"
186bfe74f8SJasvinder Singh #include "mempool.h"
19d75c371eSJasvinder Singh #include "pipeline.h"
208245472cSJasvinder Singh #include "swq.h"
212f74ae28SJasvinder Singh #include "tap.h"
22c0b668e0SJasvinder Singh #include "thread.h"
2325961ff3SJasvinder Singh #include "tmgr.h"
244bbf8e30SJasvinder Singh 
254bbf8e30SJasvinder Singh static const char usage[] =
264bbf8e30SJasvinder Singh 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
274bbf8e30SJasvinder Singh 
284bbf8e30SJasvinder Singh static const char welcome[] =
294bbf8e30SJasvinder Singh 	"\n"
304bbf8e30SJasvinder Singh 	"Welcome to IP Pipeline!\n"
314bbf8e30SJasvinder Singh 	"\n";
324bbf8e30SJasvinder Singh 
334bbf8e30SJasvinder Singh static const char prompt[] = "pipeline> ";
344bbf8e30SJasvinder Singh 
354bbf8e30SJasvinder Singh static struct app_params {
364bbf8e30SJasvinder Singh 	struct conn_params conn;
374bbf8e30SJasvinder Singh 	char *script_name;
384bbf8e30SJasvinder Singh } app = {
394bbf8e30SJasvinder Singh 	.conn = {
404bbf8e30SJasvinder Singh 		.welcome = welcome,
414bbf8e30SJasvinder Singh 		.prompt = prompt,
424bbf8e30SJasvinder Singh 		.addr = "0.0.0.0",
434bbf8e30SJasvinder Singh 		.port = 8086,
444bbf8e30SJasvinder Singh 		.buf_size = 1024 * 1024,
454bbf8e30SJasvinder Singh 		.msg_in_len_max = 1024,
464bbf8e30SJasvinder Singh 		.msg_out_len_max = 1024 * 1024,
474bbf8e30SJasvinder Singh 		.msg_handle = cli_process,
484bbf8e30SJasvinder Singh 	},
494bbf8e30SJasvinder Singh 	.script_name = NULL,
504bbf8e30SJasvinder Singh };
514bbf8e30SJasvinder Singh 
524bbf8e30SJasvinder Singh static int
534bbf8e30SJasvinder Singh parse_args(int argc, char **argv)
544bbf8e30SJasvinder Singh {
554bbf8e30SJasvinder Singh 	char *app_name = argv[0];
564bbf8e30SJasvinder Singh 	struct option lgopts[] = {
574bbf8e30SJasvinder Singh 		{ NULL,  0, 0, 0 }
584bbf8e30SJasvinder Singh 	};
594bbf8e30SJasvinder Singh 	int opt, option_index;
604bbf8e30SJasvinder Singh 	int h_present, p_present, s_present, n_args, i;
614bbf8e30SJasvinder Singh 
624bbf8e30SJasvinder Singh 	/* Skip EAL input args */
634bbf8e30SJasvinder Singh 	n_args = argc;
644bbf8e30SJasvinder Singh 	for (i = 0; i < n_args; i++)
654bbf8e30SJasvinder Singh 		if (strcmp(argv[i], "--") == 0) {
664bbf8e30SJasvinder Singh 			argc -= i;
674bbf8e30SJasvinder Singh 			argv += i;
684bbf8e30SJasvinder Singh 			break;
694bbf8e30SJasvinder Singh 		}
704bbf8e30SJasvinder Singh 
714bbf8e30SJasvinder Singh 	if (i == n_args)
724bbf8e30SJasvinder Singh 		return 0;
734bbf8e30SJasvinder Singh 
744bbf8e30SJasvinder Singh 	/* Parse args */
754bbf8e30SJasvinder Singh 	h_present = 0;
764bbf8e30SJasvinder Singh 	p_present = 0;
774bbf8e30SJasvinder Singh 	s_present = 0;
784bbf8e30SJasvinder Singh 
794bbf8e30SJasvinder Singh 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
804bbf8e30SJasvinder Singh 			!= EOF)
814bbf8e30SJasvinder Singh 		switch (opt) {
824bbf8e30SJasvinder Singh 		case 'h':
834bbf8e30SJasvinder Singh 			if (h_present) {
844bbf8e30SJasvinder Singh 				printf("Error: Multiple -h arguments\n");
854bbf8e30SJasvinder Singh 				return -1;
864bbf8e30SJasvinder Singh 			}
874bbf8e30SJasvinder Singh 			h_present = 1;
884bbf8e30SJasvinder Singh 
894bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
904bbf8e30SJasvinder Singh 				printf("Error: Argument for -h not provided\n");
914bbf8e30SJasvinder Singh 				return -1;
924bbf8e30SJasvinder Singh 			}
934bbf8e30SJasvinder Singh 
944bbf8e30SJasvinder Singh 			app.conn.addr = strdup(optarg);
954bbf8e30SJasvinder Singh 			if (app.conn.addr == NULL) {
964bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
974bbf8e30SJasvinder Singh 				return -1;
984bbf8e30SJasvinder Singh 			}
994bbf8e30SJasvinder Singh 			break;
1004bbf8e30SJasvinder Singh 
1014bbf8e30SJasvinder Singh 		case 'p':
1024bbf8e30SJasvinder Singh 			if (p_present) {
1034bbf8e30SJasvinder Singh 				printf("Error: Multiple -p arguments\n");
1044bbf8e30SJasvinder Singh 				return -1;
1054bbf8e30SJasvinder Singh 			}
1064bbf8e30SJasvinder Singh 			p_present = 1;
1074bbf8e30SJasvinder Singh 
1084bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1094bbf8e30SJasvinder Singh 				printf("Error: Argument for -p not provided\n");
1104bbf8e30SJasvinder Singh 				return -1;
1114bbf8e30SJasvinder Singh 			}
1124bbf8e30SJasvinder Singh 
1134bbf8e30SJasvinder Singh 			app.conn.port = (uint16_t) atoi(optarg);
1144bbf8e30SJasvinder Singh 			break;
1154bbf8e30SJasvinder Singh 
1164bbf8e30SJasvinder Singh 		case 's':
1174bbf8e30SJasvinder Singh 			if (s_present) {
1184bbf8e30SJasvinder Singh 				printf("Error: Multiple -s arguments\n");
1194bbf8e30SJasvinder Singh 				return -1;
1204bbf8e30SJasvinder Singh 			}
1214bbf8e30SJasvinder Singh 			s_present = 1;
1224bbf8e30SJasvinder Singh 
1234bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1244bbf8e30SJasvinder Singh 				printf("Error: Argument for -s not provided\n");
1254bbf8e30SJasvinder Singh 				return -1;
1264bbf8e30SJasvinder Singh 			}
1274bbf8e30SJasvinder Singh 
1284bbf8e30SJasvinder Singh 			app.script_name = strdup(optarg);
1294bbf8e30SJasvinder Singh 			if (app.script_name == NULL) {
1304bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
1314bbf8e30SJasvinder Singh 				return -1;
1324bbf8e30SJasvinder Singh 			}
1334bbf8e30SJasvinder Singh 			break;
1344bbf8e30SJasvinder Singh 
1354bbf8e30SJasvinder Singh 		default:
1364bbf8e30SJasvinder Singh 			printf(usage, app_name);
1374bbf8e30SJasvinder Singh 			return -1;
1384bbf8e30SJasvinder Singh 		}
1394bbf8e30SJasvinder Singh 
1404bbf8e30SJasvinder Singh 	optind = 1; /* reset getopt lib */
1414bbf8e30SJasvinder Singh 
1424bbf8e30SJasvinder Singh 	return 0;
1434bbf8e30SJasvinder Singh }
1444bbf8e30SJasvinder Singh 
14577a33467SCristian Dumitrescu int
14698a16481SDavid Marchand main(int argc, char **argv)
14777a33467SCristian Dumitrescu {
1484bbf8e30SJasvinder Singh 	struct conn *conn;
149fbc74e66SJasvinder Singh 	int status;
15077a33467SCristian Dumitrescu 
1514bbf8e30SJasvinder Singh 	/* Parse application arguments */
1524bbf8e30SJasvinder Singh 	status = parse_args(argc, argv);
1534bbf8e30SJasvinder Singh 	if (status < 0)
1544bbf8e30SJasvinder Singh 		return status;
1554bbf8e30SJasvinder Singh 
156fbc74e66SJasvinder Singh 	/* EAL */
157fbc74e66SJasvinder Singh 	status = rte_eal_init(argc, argv);
158fbc74e66SJasvinder Singh 	if (status < 0) {
159fbc74e66SJasvinder Singh 		printf("Error: EAL initialization failed (%d)\n", status);
160fbc74e66SJasvinder Singh 		return status;
161fbc74e66SJasvinder Singh 	};
16277a33467SCristian Dumitrescu 
1634bbf8e30SJasvinder Singh 	/* Connectivity */
1644bbf8e30SJasvinder Singh 	conn = conn_init(&app.conn);
1654bbf8e30SJasvinder Singh 	if (conn == NULL) {
1664bbf8e30SJasvinder Singh 		printf("Error: Connectivity initialization failed (%d)\n",
1674bbf8e30SJasvinder Singh 			status);
1684bbf8e30SJasvinder Singh 		return status;
1694bbf8e30SJasvinder Singh 	};
1704bbf8e30SJasvinder Singh 
1716bfe74f8SJasvinder Singh 	/* Mempool */
1726bfe74f8SJasvinder Singh 	status = mempool_init();
1736bfe74f8SJasvinder Singh 	if (status) {
1746bfe74f8SJasvinder Singh 		printf("Error: Mempool initialization failed (%d)\n", status);
1756bfe74f8SJasvinder Singh 		return status;
1766bfe74f8SJasvinder Singh 	}
1776bfe74f8SJasvinder Singh 
178133c2c65SJasvinder Singh 	/* Link */
179133c2c65SJasvinder Singh 	status = link_init();
180133c2c65SJasvinder Singh 	if (status) {
181133c2c65SJasvinder Singh 		printf("Error: Link initialization failed (%d)\n", status);
182133c2c65SJasvinder Singh 		return status;
183133c2c65SJasvinder Singh 	}
184133c2c65SJasvinder Singh 
1858245472cSJasvinder Singh 	/* SWQ */
1868245472cSJasvinder Singh 	status = swq_init();
1878245472cSJasvinder Singh 	if (status) {
1888245472cSJasvinder Singh 		printf("Error: SWQ initialization failed (%d)\n", status);
1898245472cSJasvinder Singh 		return status;
1908245472cSJasvinder Singh 	}
1918245472cSJasvinder Singh 
19225961ff3SJasvinder Singh 	/* Traffic Manager */
19325961ff3SJasvinder Singh 	status = tmgr_init();
19425961ff3SJasvinder Singh 	if (status) {
19525961ff3SJasvinder Singh 		printf("Error: TMGR initialization failed (%d)\n", status);
19625961ff3SJasvinder Singh 		return status;
19725961ff3SJasvinder Singh 	}
19825961ff3SJasvinder Singh 
1992f74ae28SJasvinder Singh 	/* TAP */
2002f74ae28SJasvinder Singh 	status = tap_init();
2012f74ae28SJasvinder Singh 	if (status) {
2022f74ae28SJasvinder Singh 		printf("Error: TAP initialization failed (%d)\n", status);
2032f74ae28SJasvinder Singh 		return status;
2042f74ae28SJasvinder Singh 	}
2052f74ae28SJasvinder Singh 
2069a408cc8SJasvinder Singh 	/* KNI */
2079a408cc8SJasvinder Singh 	status = kni_init();
2089a408cc8SJasvinder Singh 	if (status) {
2099a408cc8SJasvinder Singh 		printf("Error: KNI initialization failed (%d)\n", status);
2109a408cc8SJasvinder Singh 		return status;
2119a408cc8SJasvinder Singh 	}
2129a408cc8SJasvinder Singh 
21371937434SJasvinder Singh 	/* Action */
21471937434SJasvinder Singh 	status = port_in_action_profile_init();
21571937434SJasvinder Singh 	if (status) {
21671937434SJasvinder Singh 		printf("Error: Input port action profile initialization failed (%d)\n", status);
21771937434SJasvinder Singh 		return status;
21871937434SJasvinder Singh 	}
21971937434SJasvinder Singh 
22071937434SJasvinder Singh 	status = table_action_profile_init();
22171937434SJasvinder Singh 	if (status) {
22271937434SJasvinder Singh 		printf("Error: Action profile initialization failed (%d)\n",
22371937434SJasvinder Singh 			status);
22471937434SJasvinder Singh 		return status;
22571937434SJasvinder Singh 	}
22671937434SJasvinder Singh 
227d75c371eSJasvinder Singh 	/* Pipeline */
228d75c371eSJasvinder Singh 	status = pipeline_init();
229d75c371eSJasvinder Singh 	if (status) {
230d75c371eSJasvinder Singh 		printf("Error: Pipeline initialization failed (%d)\n", status);
231d75c371eSJasvinder Singh 		return status;
232d75c371eSJasvinder Singh 	}
233d75c371eSJasvinder Singh 
234c0b668e0SJasvinder Singh 	/* Thread */
235c0b668e0SJasvinder Singh 	status = thread_init();
236c0b668e0SJasvinder Singh 	if (status) {
237c0b668e0SJasvinder Singh 		printf("Error: Thread initialization failed (%d)\n", status);
238c0b668e0SJasvinder Singh 		return status;
239c0b668e0SJasvinder Singh 	}
240c0b668e0SJasvinder Singh 
241*a8bd581dSJasvinder Singh 	rte_eal_mp_remote_launch(
242*a8bd581dSJasvinder Singh 		thread_main,
243*a8bd581dSJasvinder Singh 		NULL,
244*a8bd581dSJasvinder Singh 		SKIP_MASTER);
245*a8bd581dSJasvinder Singh 
2464bbf8e30SJasvinder Singh 	/* Script */
2474bbf8e30SJasvinder Singh 	if (app.script_name)
2484bbf8e30SJasvinder Singh 		cli_script_process(app.script_name,
2494bbf8e30SJasvinder Singh 			app.conn.msg_in_len_max,
2504bbf8e30SJasvinder Singh 			app.conn.msg_out_len_max);
2514bbf8e30SJasvinder Singh 
2524bbf8e30SJasvinder Singh 	/* Dispatch loop */
2534bbf8e30SJasvinder Singh 	for ( ; ; ) {
2544bbf8e30SJasvinder Singh 		conn_poll_for_conn(conn);
2554bbf8e30SJasvinder Singh 
2564bbf8e30SJasvinder Singh 		conn_poll_for_msg(conn);
2579a408cc8SJasvinder Singh 
2589a408cc8SJasvinder Singh 		kni_handle_request();
2594bbf8e30SJasvinder Singh 	}
26077a33467SCristian Dumitrescu }
261