xref: /dpdk/examples/ip_pipeline/main.c (revision 2f74ae28e23f441c6f51241b4f3ea1c9b8b15812)
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"
15133c2c65SJasvinder Singh #include "link.h"
166bfe74f8SJasvinder Singh #include "mempool.h"
178245472cSJasvinder Singh #include "swq.h"
18*2f74ae28SJasvinder Singh #include "tap.h"
1925961ff3SJasvinder Singh #include "tmgr.h"
204bbf8e30SJasvinder Singh 
214bbf8e30SJasvinder Singh static const char usage[] =
224bbf8e30SJasvinder Singh 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
234bbf8e30SJasvinder Singh 
244bbf8e30SJasvinder Singh static const char welcome[] =
254bbf8e30SJasvinder Singh 	"\n"
264bbf8e30SJasvinder Singh 	"Welcome to IP Pipeline!\n"
274bbf8e30SJasvinder Singh 	"\n";
284bbf8e30SJasvinder Singh 
294bbf8e30SJasvinder Singh static const char prompt[] = "pipeline> ";
304bbf8e30SJasvinder Singh 
314bbf8e30SJasvinder Singh static struct app_params {
324bbf8e30SJasvinder Singh 	struct conn_params conn;
334bbf8e30SJasvinder Singh 	char *script_name;
344bbf8e30SJasvinder Singh } app = {
354bbf8e30SJasvinder Singh 	.conn = {
364bbf8e30SJasvinder Singh 		.welcome = welcome,
374bbf8e30SJasvinder Singh 		.prompt = prompt,
384bbf8e30SJasvinder Singh 		.addr = "0.0.0.0",
394bbf8e30SJasvinder Singh 		.port = 8086,
404bbf8e30SJasvinder Singh 		.buf_size = 1024 * 1024,
414bbf8e30SJasvinder Singh 		.msg_in_len_max = 1024,
424bbf8e30SJasvinder Singh 		.msg_out_len_max = 1024 * 1024,
434bbf8e30SJasvinder Singh 		.msg_handle = cli_process,
444bbf8e30SJasvinder Singh 	},
454bbf8e30SJasvinder Singh 	.script_name = NULL,
464bbf8e30SJasvinder Singh };
474bbf8e30SJasvinder Singh 
484bbf8e30SJasvinder Singh static int
494bbf8e30SJasvinder Singh parse_args(int argc, char **argv)
504bbf8e30SJasvinder Singh {
514bbf8e30SJasvinder Singh 	char *app_name = argv[0];
524bbf8e30SJasvinder Singh 	struct option lgopts[] = {
534bbf8e30SJasvinder Singh 		{ NULL,  0, 0, 0 }
544bbf8e30SJasvinder Singh 	};
554bbf8e30SJasvinder Singh 	int opt, option_index;
564bbf8e30SJasvinder Singh 	int h_present, p_present, s_present, n_args, i;
574bbf8e30SJasvinder Singh 
584bbf8e30SJasvinder Singh 	/* Skip EAL input args */
594bbf8e30SJasvinder Singh 	n_args = argc;
604bbf8e30SJasvinder Singh 	for (i = 0; i < n_args; i++)
614bbf8e30SJasvinder Singh 		if (strcmp(argv[i], "--") == 0) {
624bbf8e30SJasvinder Singh 			argc -= i;
634bbf8e30SJasvinder Singh 			argv += i;
644bbf8e30SJasvinder Singh 			break;
654bbf8e30SJasvinder Singh 		}
664bbf8e30SJasvinder Singh 
674bbf8e30SJasvinder Singh 	if (i == n_args)
684bbf8e30SJasvinder Singh 		return 0;
694bbf8e30SJasvinder Singh 
704bbf8e30SJasvinder Singh 	/* Parse args */
714bbf8e30SJasvinder Singh 	h_present = 0;
724bbf8e30SJasvinder Singh 	p_present = 0;
734bbf8e30SJasvinder Singh 	s_present = 0;
744bbf8e30SJasvinder Singh 
754bbf8e30SJasvinder Singh 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
764bbf8e30SJasvinder Singh 			!= EOF)
774bbf8e30SJasvinder Singh 		switch (opt) {
784bbf8e30SJasvinder Singh 		case 'h':
794bbf8e30SJasvinder Singh 			if (h_present) {
804bbf8e30SJasvinder Singh 				printf("Error: Multiple -h arguments\n");
814bbf8e30SJasvinder Singh 				return -1;
824bbf8e30SJasvinder Singh 			}
834bbf8e30SJasvinder Singh 			h_present = 1;
844bbf8e30SJasvinder Singh 
854bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
864bbf8e30SJasvinder Singh 				printf("Error: Argument for -h not provided\n");
874bbf8e30SJasvinder Singh 				return -1;
884bbf8e30SJasvinder Singh 			}
894bbf8e30SJasvinder Singh 
904bbf8e30SJasvinder Singh 			app.conn.addr = strdup(optarg);
914bbf8e30SJasvinder Singh 			if (app.conn.addr == NULL) {
924bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
934bbf8e30SJasvinder Singh 				return -1;
944bbf8e30SJasvinder Singh 			}
954bbf8e30SJasvinder Singh 			break;
964bbf8e30SJasvinder Singh 
974bbf8e30SJasvinder Singh 		case 'p':
984bbf8e30SJasvinder Singh 			if (p_present) {
994bbf8e30SJasvinder Singh 				printf("Error: Multiple -p arguments\n");
1004bbf8e30SJasvinder Singh 				return -1;
1014bbf8e30SJasvinder Singh 			}
1024bbf8e30SJasvinder Singh 			p_present = 1;
1034bbf8e30SJasvinder Singh 
1044bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1054bbf8e30SJasvinder Singh 				printf("Error: Argument for -p not provided\n");
1064bbf8e30SJasvinder Singh 				return -1;
1074bbf8e30SJasvinder Singh 			}
1084bbf8e30SJasvinder Singh 
1094bbf8e30SJasvinder Singh 			app.conn.port = (uint16_t) atoi(optarg);
1104bbf8e30SJasvinder Singh 			break;
1114bbf8e30SJasvinder Singh 
1124bbf8e30SJasvinder Singh 		case 's':
1134bbf8e30SJasvinder Singh 			if (s_present) {
1144bbf8e30SJasvinder Singh 				printf("Error: Multiple -s arguments\n");
1154bbf8e30SJasvinder Singh 				return -1;
1164bbf8e30SJasvinder Singh 			}
1174bbf8e30SJasvinder Singh 			s_present = 1;
1184bbf8e30SJasvinder Singh 
1194bbf8e30SJasvinder Singh 			if (!strlen(optarg)) {
1204bbf8e30SJasvinder Singh 				printf("Error: Argument for -s not provided\n");
1214bbf8e30SJasvinder Singh 				return -1;
1224bbf8e30SJasvinder Singh 			}
1234bbf8e30SJasvinder Singh 
1244bbf8e30SJasvinder Singh 			app.script_name = strdup(optarg);
1254bbf8e30SJasvinder Singh 			if (app.script_name == NULL) {
1264bbf8e30SJasvinder Singh 				printf("Error: Not enough memory\n");
1274bbf8e30SJasvinder Singh 				return -1;
1284bbf8e30SJasvinder Singh 			}
1294bbf8e30SJasvinder Singh 			break;
1304bbf8e30SJasvinder Singh 
1314bbf8e30SJasvinder Singh 		default:
1324bbf8e30SJasvinder Singh 			printf(usage, app_name);
1334bbf8e30SJasvinder Singh 			return -1;
1344bbf8e30SJasvinder Singh 		}
1354bbf8e30SJasvinder Singh 
1364bbf8e30SJasvinder Singh 	optind = 1; /* reset getopt lib */
1374bbf8e30SJasvinder Singh 
1384bbf8e30SJasvinder Singh 	return 0;
1394bbf8e30SJasvinder Singh }
1404bbf8e30SJasvinder Singh 
14177a33467SCristian Dumitrescu int
14298a16481SDavid Marchand main(int argc, char **argv)
14377a33467SCristian Dumitrescu {
1444bbf8e30SJasvinder Singh 	struct conn *conn;
145fbc74e66SJasvinder Singh 	int status;
14677a33467SCristian Dumitrescu 
1474bbf8e30SJasvinder Singh 	/* Parse application arguments */
1484bbf8e30SJasvinder Singh 	status = parse_args(argc, argv);
1494bbf8e30SJasvinder Singh 	if (status < 0)
1504bbf8e30SJasvinder Singh 		return status;
1514bbf8e30SJasvinder Singh 
152fbc74e66SJasvinder Singh 	/* EAL */
153fbc74e66SJasvinder Singh 	status = rte_eal_init(argc, argv);
154fbc74e66SJasvinder Singh 	if (status < 0) {
155fbc74e66SJasvinder Singh 		printf("Error: EAL initialization failed (%d)\n", status);
156fbc74e66SJasvinder Singh 		return status;
157fbc74e66SJasvinder Singh 	};
15877a33467SCristian Dumitrescu 
1594bbf8e30SJasvinder Singh 	/* Connectivity */
1604bbf8e30SJasvinder Singh 	conn = conn_init(&app.conn);
1614bbf8e30SJasvinder Singh 	if (conn == NULL) {
1624bbf8e30SJasvinder Singh 		printf("Error: Connectivity initialization failed (%d)\n",
1634bbf8e30SJasvinder Singh 			status);
1644bbf8e30SJasvinder Singh 		return status;
1654bbf8e30SJasvinder Singh 	};
1664bbf8e30SJasvinder Singh 
1676bfe74f8SJasvinder Singh 	/* Mempool */
1686bfe74f8SJasvinder Singh 	status = mempool_init();
1696bfe74f8SJasvinder Singh 	if (status) {
1706bfe74f8SJasvinder Singh 		printf("Error: Mempool initialization failed (%d)\n", status);
1716bfe74f8SJasvinder Singh 		return status;
1726bfe74f8SJasvinder Singh 	}
1736bfe74f8SJasvinder Singh 
174133c2c65SJasvinder Singh 	/* Link */
175133c2c65SJasvinder Singh 	status = link_init();
176133c2c65SJasvinder Singh 	if (status) {
177133c2c65SJasvinder Singh 		printf("Error: Link initialization failed (%d)\n", status);
178133c2c65SJasvinder Singh 		return status;
179133c2c65SJasvinder Singh 	}
180133c2c65SJasvinder Singh 
1818245472cSJasvinder Singh 	/* SWQ */
1828245472cSJasvinder Singh 	status = swq_init();
1838245472cSJasvinder Singh 	if (status) {
1848245472cSJasvinder Singh 		printf("Error: SWQ initialization failed (%d)\n", status);
1858245472cSJasvinder Singh 		return status;
1868245472cSJasvinder Singh 	}
1878245472cSJasvinder Singh 
18825961ff3SJasvinder Singh 	/* Traffic Manager */
18925961ff3SJasvinder Singh 	status = tmgr_init();
19025961ff3SJasvinder Singh 	if (status) {
19125961ff3SJasvinder Singh 		printf("Error: TMGR initialization failed (%d)\n", status);
19225961ff3SJasvinder Singh 		return status;
19325961ff3SJasvinder Singh 	}
19425961ff3SJasvinder Singh 
195*2f74ae28SJasvinder Singh 	/* TAP */
196*2f74ae28SJasvinder Singh 	status = tap_init();
197*2f74ae28SJasvinder Singh 	if (status) {
198*2f74ae28SJasvinder Singh 		printf("Error: TAP initialization failed (%d)\n", status);
199*2f74ae28SJasvinder Singh 		return status;
200*2f74ae28SJasvinder Singh 	}
201*2f74ae28SJasvinder Singh 
2024bbf8e30SJasvinder Singh 	/* Script */
2034bbf8e30SJasvinder Singh 	if (app.script_name)
2044bbf8e30SJasvinder Singh 		cli_script_process(app.script_name,
2054bbf8e30SJasvinder Singh 			app.conn.msg_in_len_max,
2064bbf8e30SJasvinder Singh 			app.conn.msg_out_len_max);
2074bbf8e30SJasvinder Singh 
2084bbf8e30SJasvinder Singh 	/* Dispatch loop */
2094bbf8e30SJasvinder Singh 	for ( ; ; ) {
2104bbf8e30SJasvinder Singh 		conn_poll_for_conn(conn);
2114bbf8e30SJasvinder Singh 
2124bbf8e30SJasvinder Singh 		conn_poll_for_msg(conn);
2134bbf8e30SJasvinder Singh 	}
21477a33467SCristian Dumitrescu }
215