xref: /dpdk/examples/ip_pipeline/main.c (revision f78c100bc87119c6a94130a6689d773afdaa9d98)
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>
6*72b452c5SDmitry Kozlyuk #include <stdlib.h>
7fbc74e66SJasvinder Singh #include <string.h>
8fbc74e66SJasvinder Singh #include <fcntl.h>
9fbc74e66SJasvinder Singh #include <unistd.h>
10fbc74e66SJasvinder Singh #include <getopt.h>
1177a33467SCristian Dumitrescu 
12a8bd581dSJasvinder Singh #include <rte_launch.h>
13fbc74e66SJasvinder Singh #include <rte_eal.h>
1477a33467SCristian Dumitrescu 
154bbf8e30SJasvinder Singh #include "cli.h"
164bbf8e30SJasvinder Singh #include "conn.h"
17bd9b67a1SFan Zhang #include "cryptodev.h"
18133c2c65SJasvinder Singh #include "link.h"
196bfe74f8SJasvinder Singh #include "mempool.h"
20d75c371eSJasvinder Singh #include "pipeline.h"
218245472cSJasvinder Singh #include "swq.h"
222f74ae28SJasvinder Singh #include "tap.h"
23c0b668e0SJasvinder Singh #include "thread.h"
2425961ff3SJasvinder Singh #include "tmgr.h"
254bbf8e30SJasvinder Singh 
264bbf8e30SJasvinder Singh static const char usage[] =
274bbf8e30SJasvinder Singh 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
284bbf8e30SJasvinder Singh 
294bbf8e30SJasvinder Singh static const char welcome[] =
304bbf8e30SJasvinder Singh 	"\n"
314bbf8e30SJasvinder Singh 	"Welcome to IP Pipeline!\n"
324bbf8e30SJasvinder Singh 	"\n";
334bbf8e30SJasvinder Singh 
344bbf8e30SJasvinder Singh static const char prompt[] = "pipeline> ";
354bbf8e30SJasvinder Singh 
364bbf8e30SJasvinder Singh static struct app_params {
374bbf8e30SJasvinder Singh 	struct conn_params conn;
384bbf8e30SJasvinder Singh 	char *script_name;
394bbf8e30SJasvinder Singh } app = {
404bbf8e30SJasvinder Singh 	.conn = {
414bbf8e30SJasvinder Singh 		.welcome = welcome,
424bbf8e30SJasvinder Singh 		.prompt = prompt,
434bbf8e30SJasvinder Singh 		.addr = "0.0.0.0",
444bbf8e30SJasvinder Singh 		.port = 8086,
454bbf8e30SJasvinder Singh 		.buf_size = 1024 * 1024,
464bbf8e30SJasvinder Singh 		.msg_in_len_max = 1024,
474bbf8e30SJasvinder Singh 		.msg_out_len_max = 1024 * 1024,
484bbf8e30SJasvinder Singh 		.msg_handle = cli_process,
494bbf8e30SJasvinder Singh 	},
504bbf8e30SJasvinder Singh 	.script_name = NULL,
514bbf8e30SJasvinder Singh };
524bbf8e30SJasvinder Singh 
534bbf8e30SJasvinder Singh static int
parse_args(int argc,char ** argv)544bbf8e30SJasvinder Singh parse_args(int argc, char **argv)
554bbf8e30SJasvinder Singh {
564bbf8e30SJasvinder Singh 	char *app_name = argv[0];
574bbf8e30SJasvinder Singh 	struct option lgopts[] = {
584bbf8e30SJasvinder Singh 		{ NULL,  0, 0, 0 }
594bbf8e30SJasvinder Singh 	};
604bbf8e30SJasvinder Singh 	int opt, option_index;
614bbf8e30SJasvinder Singh 	int h_present, p_present, s_present, n_args, i;
624bbf8e30SJasvinder Singh 
634bbf8e30SJasvinder Singh 	/* Skip EAL input args */
644bbf8e30SJasvinder Singh 	n_args = argc;
654bbf8e30SJasvinder Singh 	for (i = 0; i < n_args; i++)
664bbf8e30SJasvinder Singh 		if (strcmp(argv[i], "--") == 0) {
674bbf8e30SJasvinder Singh 			argc -= i;
684bbf8e30SJasvinder Singh 			argv += i;
694bbf8e30SJasvinder Singh 			break;
704bbf8e30SJasvinder Singh 		}
714bbf8e30SJasvinder Singh 
724bbf8e30SJasvinder Singh 	if (i == n_args)
734bbf8e30SJasvinder Singh 		return 0;
744bbf8e30SJasvinder Singh 
754bbf8e30SJasvinder Singh 	/* Parse args */
764bbf8e30SJasvinder Singh 	h_present = 0;
774bbf8e30SJasvinder Singh 	p_present = 0;
784bbf8e30SJasvinder Singh 	s_present = 0;
794bbf8e30SJasvinder Singh 
804bbf8e30SJasvinder Singh 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
814bbf8e30SJasvinder Singh 			!= EOF)
824bbf8e30SJasvinder Singh 		switch (opt) {
834bbf8e30SJasvinder Singh 		case 'h':
844bbf8e30SJasvinder Singh 			if (h_present) {
854bbf8e30SJasvinder Singh 				printf("Error: Multiple -h arguments\n");
864bbf8e30SJasvinder Singh 				return -1;
874bbf8e30SJasvinder Singh 			}
884bbf8e30SJasvinder Singh 			h_present = 1;
894bbf8e30SJasvinder Singh 
904bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
914bbf8e30SJasvinder Singh 				printf("Error: Argument for -h not provided\n");
924bbf8e30SJasvinder Singh 				return -1;
934bbf8e30SJasvinder Singh 			}
944bbf8e30SJasvinder Singh 
954bbf8e30SJasvinder Singh 			app.conn.addr = strdup(optarg);
964bbf8e30SJasvinder Singh 			if (app.conn.addr == NULL) {
974bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
984bbf8e30SJasvinder Singh 				return -1;
994bbf8e30SJasvinder Singh 			}
1004bbf8e30SJasvinder Singh 			break;
1014bbf8e30SJasvinder Singh 
1024bbf8e30SJasvinder Singh 		case 'p':
1034bbf8e30SJasvinder Singh 			if (p_present) {
1044bbf8e30SJasvinder Singh 				printf("Error: Multiple -p arguments\n");
1054bbf8e30SJasvinder Singh 				return -1;
1064bbf8e30SJasvinder Singh 			}
1074bbf8e30SJasvinder Singh 			p_present = 1;
1084bbf8e30SJasvinder Singh 
1094bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1104bbf8e30SJasvinder Singh 				printf("Error: Argument for -p not provided\n");
1114bbf8e30SJasvinder Singh 				return -1;
1124bbf8e30SJasvinder Singh 			}
1134bbf8e30SJasvinder Singh 
1144bbf8e30SJasvinder Singh 			app.conn.port = (uint16_t) atoi(optarg);
1154bbf8e30SJasvinder Singh 			break;
1164bbf8e30SJasvinder Singh 
1174bbf8e30SJasvinder Singh 		case 's':
1184bbf8e30SJasvinder Singh 			if (s_present) {
1194bbf8e30SJasvinder Singh 				printf("Error: Multiple -s arguments\n");
1204bbf8e30SJasvinder Singh 				return -1;
1214bbf8e30SJasvinder Singh 			}
1224bbf8e30SJasvinder Singh 			s_present = 1;
1234bbf8e30SJasvinder Singh 
1244bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1254bbf8e30SJasvinder Singh 				printf("Error: Argument for -s not provided\n");
1264bbf8e30SJasvinder Singh 				return -1;
1274bbf8e30SJasvinder Singh 			}
1284bbf8e30SJasvinder Singh 
1294bbf8e30SJasvinder Singh 			app.script_name = strdup(optarg);
1304bbf8e30SJasvinder Singh 			if (app.script_name == NULL) {
1314bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
1324bbf8e30SJasvinder Singh 				return -1;
1334bbf8e30SJasvinder Singh 			}
1344bbf8e30SJasvinder Singh 			break;
1354bbf8e30SJasvinder Singh 
1364bbf8e30SJasvinder Singh 		default:
1374bbf8e30SJasvinder Singh 			printf(usage, app_name);
1384bbf8e30SJasvinder Singh 			return -1;
1394bbf8e30SJasvinder Singh 		}
1404bbf8e30SJasvinder Singh 
1414bbf8e30SJasvinder Singh 	optind = 1; /* reset getopt lib */
1424bbf8e30SJasvinder Singh 
1434bbf8e30SJasvinder Singh 	return 0;
1444bbf8e30SJasvinder Singh }
1454bbf8e30SJasvinder Singh 
14677a33467SCristian Dumitrescu int
main(int argc,char ** argv)14798a16481SDavid Marchand main(int argc, char **argv)
14877a33467SCristian Dumitrescu {
1494bbf8e30SJasvinder Singh 	struct conn *conn;
150fbc74e66SJasvinder Singh 	int status;
15177a33467SCristian Dumitrescu 
1524bbf8e30SJasvinder Singh 	/* Parse application arguments */
1534bbf8e30SJasvinder Singh 	status = parse_args(argc, argv);
1544bbf8e30SJasvinder Singh 	if (status < 0)
1554bbf8e30SJasvinder Singh 		return status;
1564bbf8e30SJasvinder Singh 
157fbc74e66SJasvinder Singh 	/* EAL */
158fbc74e66SJasvinder Singh 	status = rte_eal_init(argc, argv);
159fbc74e66SJasvinder Singh 	if (status < 0) {
160fbc74e66SJasvinder Singh 		printf("Error: EAL initialization failed (%d)\n", status);
161fbc74e66SJasvinder Singh 		return status;
162fbc74e66SJasvinder Singh 	};
16377a33467SCristian Dumitrescu 
1644bbf8e30SJasvinder Singh 	/* Connectivity */
1654bbf8e30SJasvinder Singh 	conn = conn_init(&app.conn);
1664bbf8e30SJasvinder Singh 	if (conn == NULL) {
1674bbf8e30SJasvinder Singh 		printf("Error: Connectivity initialization failed (%d)\n",
1684bbf8e30SJasvinder Singh 			status);
1694bbf8e30SJasvinder Singh 		return status;
1704bbf8e30SJasvinder Singh 	};
1714bbf8e30SJasvinder Singh 
1726bfe74f8SJasvinder Singh 	/* Mempool */
1736bfe74f8SJasvinder Singh 	status = mempool_init();
1746bfe74f8SJasvinder Singh 	if (status) {
1756bfe74f8SJasvinder Singh 		printf("Error: Mempool initialization failed (%d)\n", status);
1766bfe74f8SJasvinder Singh 		return status;
1776bfe74f8SJasvinder Singh 	}
1786bfe74f8SJasvinder Singh 
179133c2c65SJasvinder Singh 	/* Link */
180133c2c65SJasvinder Singh 	status = link_init();
181133c2c65SJasvinder Singh 	if (status) {
182133c2c65SJasvinder Singh 		printf("Error: Link initialization failed (%d)\n", status);
183133c2c65SJasvinder Singh 		return status;
184133c2c65SJasvinder Singh 	}
185133c2c65SJasvinder Singh 
1868245472cSJasvinder Singh 	/* SWQ */
1878245472cSJasvinder Singh 	status = swq_init();
1888245472cSJasvinder Singh 	if (status) {
1898245472cSJasvinder Singh 		printf("Error: SWQ initialization failed (%d)\n", status);
1908245472cSJasvinder Singh 		return status;
1918245472cSJasvinder Singh 	}
1928245472cSJasvinder Singh 
19325961ff3SJasvinder Singh 	/* Traffic Manager */
19425961ff3SJasvinder Singh 	status = tmgr_init();
19525961ff3SJasvinder Singh 	if (status) {
19625961ff3SJasvinder Singh 		printf("Error: TMGR initialization failed (%d)\n", status);
19725961ff3SJasvinder Singh 		return status;
19825961ff3SJasvinder Singh 	}
19925961ff3SJasvinder Singh 
2002f74ae28SJasvinder Singh 	/* TAP */
2012f74ae28SJasvinder Singh 	status = tap_init();
2022f74ae28SJasvinder Singh 	if (status) {
2032f74ae28SJasvinder Singh 		printf("Error: TAP initialization failed (%d)\n", status);
2042f74ae28SJasvinder Singh 		return status;
2052f74ae28SJasvinder Singh 	}
2062f74ae28SJasvinder Singh 
207bd9b67a1SFan Zhang 	/* Sym Crypto */
208bd9b67a1SFan Zhang 	status = cryptodev_init();
209bd9b67a1SFan Zhang 	if (status) {
210bd9b67a1SFan Zhang 		printf("Error: Cryptodev initialization failed (%d)\n",
211bd9b67a1SFan Zhang 				status);
212bd9b67a1SFan Zhang 		return status;
213bd9b67a1SFan Zhang 	}
214bd9b67a1SFan Zhang 
21571937434SJasvinder Singh 	/* Action */
21671937434SJasvinder Singh 	status = port_in_action_profile_init();
21771937434SJasvinder Singh 	if (status) {
21871937434SJasvinder Singh 		printf("Error: Input port action profile initialization failed (%d)\n", status);
21971937434SJasvinder Singh 		return status;
22071937434SJasvinder Singh 	}
22171937434SJasvinder Singh 
22271937434SJasvinder Singh 	status = table_action_profile_init();
22371937434SJasvinder Singh 	if (status) {
22471937434SJasvinder Singh 		printf("Error: Action profile initialization failed (%d)\n",
22571937434SJasvinder Singh 			status);
22671937434SJasvinder Singh 		return status;
22771937434SJasvinder Singh 	}
22871937434SJasvinder Singh 
229d75c371eSJasvinder Singh 	/* Pipeline */
230d75c371eSJasvinder Singh 	status = pipeline_init();
231d75c371eSJasvinder Singh 	if (status) {
232d75c371eSJasvinder Singh 		printf("Error: Pipeline initialization failed (%d)\n", status);
233d75c371eSJasvinder Singh 		return status;
234d75c371eSJasvinder Singh 	}
235d75c371eSJasvinder Singh 
236c0b668e0SJasvinder Singh 	/* Thread */
237c0b668e0SJasvinder Singh 	status = thread_init();
238c0b668e0SJasvinder Singh 	if (status) {
239c0b668e0SJasvinder Singh 		printf("Error: Thread initialization failed (%d)\n", status);
240c0b668e0SJasvinder Singh 		return status;
241c0b668e0SJasvinder Singh 	}
242c0b668e0SJasvinder Singh 
243a8bd581dSJasvinder Singh 	rte_eal_mp_remote_launch(
244a8bd581dSJasvinder Singh 		thread_main,
245a8bd581dSJasvinder Singh 		NULL,
246cb056611SStephen Hemminger 		SKIP_MAIN);
247a8bd581dSJasvinder Singh 
2484bbf8e30SJasvinder Singh 	/* Script */
2494bbf8e30SJasvinder Singh 	if (app.script_name)
2504bbf8e30SJasvinder Singh 		cli_script_process(app.script_name,
2514bbf8e30SJasvinder Singh 			app.conn.msg_in_len_max,
2524bbf8e30SJasvinder Singh 			app.conn.msg_out_len_max);
2534bbf8e30SJasvinder Singh 
2544bbf8e30SJasvinder Singh 	/* Dispatch loop */
2554bbf8e30SJasvinder Singh 	for ( ; ; ) {
2564bbf8e30SJasvinder Singh 		conn_poll_for_conn(conn);
2574bbf8e30SJasvinder Singh 
2584bbf8e30SJasvinder Singh 		conn_poll_for_msg(conn);
2594bbf8e30SJasvinder Singh 	}
26077a33467SCristian Dumitrescu }
261