xref: /dpdk/examples/pipeline/main.c (revision 78dffe314cd7bff9cc70385d6e5aaa546b88a6a1)
1b77f6600SCristian Dumitrescu /* SPDX-License-Identifier: BSD-3-Clause
2b77f6600SCristian Dumitrescu  * Copyright(c) 2020 Intel Corporation
3b77f6600SCristian Dumitrescu  */
4b77f6600SCristian Dumitrescu 
5b77f6600SCristian Dumitrescu #include <stdio.h>
672b452c5SDmitry Kozlyuk #include <stdlib.h>
7b77f6600SCristian Dumitrescu #include <string.h>
8b77f6600SCristian Dumitrescu #include <fcntl.h>
9b77f6600SCristian Dumitrescu #include <unistd.h>
10b77f6600SCristian Dumitrescu #include <getopt.h>
11b77f6600SCristian Dumitrescu 
12b77f6600SCristian Dumitrescu #include <rte_launch.h>
13b77f6600SCristian Dumitrescu #include <rte_eal.h>
14b77f6600SCristian Dumitrescu 
155074e1d5SCristian Dumitrescu #include "cli.h"
165f657a7fSCristian Dumitrescu #include "conn.h"
17b77f6600SCristian Dumitrescu #include "obj.h"
18b77f6600SCristian Dumitrescu #include "thread.h"
19b77f6600SCristian Dumitrescu 
205f657a7fSCristian Dumitrescu static const char usage[] =
215f657a7fSCristian Dumitrescu 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
225f657a7fSCristian Dumitrescu 
235f657a7fSCristian Dumitrescu static struct app_params {
245f657a7fSCristian Dumitrescu 	struct conn_params conn;
255f657a7fSCristian Dumitrescu 	char *script_name;
265f657a7fSCristian Dumitrescu } app = {
275f657a7fSCristian Dumitrescu 	.conn = {
285f657a7fSCristian Dumitrescu 		.welcome = "\nWelcome!\n\n",
295f657a7fSCristian Dumitrescu 		.prompt = "pipeline> ",
305f657a7fSCristian Dumitrescu 		.addr = "0.0.0.0",
315f657a7fSCristian Dumitrescu 		.port = 8086,
325f657a7fSCristian Dumitrescu 		.buf_size = 1024 * 1024,
335f657a7fSCristian Dumitrescu 		.msg_in_len_max = 1024,
345f657a7fSCristian Dumitrescu 		.msg_out_len_max = 1024 * 1024,
355074e1d5SCristian Dumitrescu 		.msg_handle = cli_process,
365f657a7fSCristian Dumitrescu 		.msg_handle_arg = NULL, /* set later. */
375f657a7fSCristian Dumitrescu 	},
385f657a7fSCristian Dumitrescu 	.script_name = NULL,
395f657a7fSCristian Dumitrescu };
405f657a7fSCristian Dumitrescu 
415f657a7fSCristian Dumitrescu static int
parse_args(int argc,char ** argv)425f657a7fSCristian Dumitrescu parse_args(int argc, char **argv)
435f657a7fSCristian Dumitrescu {
445f657a7fSCristian Dumitrescu 	char *app_name = argv[0];
455f657a7fSCristian Dumitrescu 	struct option lgopts[] = {
465f657a7fSCristian Dumitrescu 		{ NULL,  0, 0, 0 }
475f657a7fSCristian Dumitrescu 	};
485f657a7fSCristian Dumitrescu 	int opt, option_index;
495f657a7fSCristian Dumitrescu 	int h_present, p_present, s_present, n_args, i;
505f657a7fSCristian Dumitrescu 
515f657a7fSCristian Dumitrescu 	/* Skip EAL input args */
525f657a7fSCristian Dumitrescu 	n_args = argc;
535f657a7fSCristian Dumitrescu 	for (i = 0; i < n_args; i++)
545f657a7fSCristian Dumitrescu 		if (strcmp(argv[i], "--") == 0) {
555f657a7fSCristian Dumitrescu 			argc -= i;
565f657a7fSCristian Dumitrescu 			argv += i;
575f657a7fSCristian Dumitrescu 			break;
585f657a7fSCristian Dumitrescu 		}
595f657a7fSCristian Dumitrescu 
605f657a7fSCristian Dumitrescu 	if (i == n_args)
615f657a7fSCristian Dumitrescu 		return 0;
625f657a7fSCristian Dumitrescu 
635f657a7fSCristian Dumitrescu 	/* Parse args */
645f657a7fSCristian Dumitrescu 	h_present = 0;
655f657a7fSCristian Dumitrescu 	p_present = 0;
665f657a7fSCristian Dumitrescu 	s_present = 0;
675f657a7fSCristian Dumitrescu 
685f657a7fSCristian Dumitrescu 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
695f657a7fSCristian Dumitrescu 			!= EOF)
705f657a7fSCristian Dumitrescu 		switch (opt) {
715f657a7fSCristian Dumitrescu 		case 'h':
725f657a7fSCristian Dumitrescu 			if (h_present) {
735f657a7fSCristian Dumitrescu 				printf("Error: Multiple -h arguments\n");
745f657a7fSCristian Dumitrescu 				return -1;
755f657a7fSCristian Dumitrescu 			}
765f657a7fSCristian Dumitrescu 			h_present = 1;
775f657a7fSCristian Dumitrescu 
785f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
795f657a7fSCristian Dumitrescu 				printf("Error: Argument for -h not provided\n");
805f657a7fSCristian Dumitrescu 				return -1;
815f657a7fSCristian Dumitrescu 			}
825f657a7fSCristian Dumitrescu 
835f657a7fSCristian Dumitrescu 			app.conn.addr = strdup(optarg);
845f657a7fSCristian Dumitrescu 			if (app.conn.addr == NULL) {
855f657a7fSCristian Dumitrescu 				printf("Error: Not enough memory\n");
865f657a7fSCristian Dumitrescu 				return -1;
875f657a7fSCristian Dumitrescu 			}
885f657a7fSCristian Dumitrescu 			break;
895f657a7fSCristian Dumitrescu 
905f657a7fSCristian Dumitrescu 		case 'p':
915f657a7fSCristian Dumitrescu 			if (p_present) {
925f657a7fSCristian Dumitrescu 				printf("Error: Multiple -p arguments\n");
935f657a7fSCristian Dumitrescu 				return -1;
945f657a7fSCristian Dumitrescu 			}
955f657a7fSCristian Dumitrescu 			p_present = 1;
965f657a7fSCristian Dumitrescu 
975f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
985f657a7fSCristian Dumitrescu 				printf("Error: Argument for -p not provided\n");
995f657a7fSCristian Dumitrescu 				return -1;
1005f657a7fSCristian Dumitrescu 			}
1015f657a7fSCristian Dumitrescu 
1025f657a7fSCristian Dumitrescu 			app.conn.port = (uint16_t) atoi(optarg);
1035f657a7fSCristian Dumitrescu 			break;
1045f657a7fSCristian Dumitrescu 
1055f657a7fSCristian Dumitrescu 		case 's':
1065f657a7fSCristian Dumitrescu 			if (s_present) {
1075f657a7fSCristian Dumitrescu 				printf("Error: Multiple -s arguments\n");
1085f657a7fSCristian Dumitrescu 				return -1;
1095f657a7fSCristian Dumitrescu 			}
1105f657a7fSCristian Dumitrescu 			s_present = 1;
1115f657a7fSCristian Dumitrescu 
1125f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
1135f657a7fSCristian Dumitrescu 				printf("Error: Argument for -s not provided\n");
1145f657a7fSCristian Dumitrescu 				return -1;
1155f657a7fSCristian Dumitrescu 			}
1165f657a7fSCristian Dumitrescu 
1175f657a7fSCristian Dumitrescu 			app.script_name = strdup(optarg);
1185f657a7fSCristian Dumitrescu 			if (app.script_name == NULL) {
1195f657a7fSCristian Dumitrescu 				printf("Error: Not enough memory\n");
1205f657a7fSCristian Dumitrescu 				return -1;
1215f657a7fSCristian Dumitrescu 			}
1225f657a7fSCristian Dumitrescu 			break;
1235f657a7fSCristian Dumitrescu 
1245f657a7fSCristian Dumitrescu 		default:
1255f657a7fSCristian Dumitrescu 			printf(usage, app_name);
1265f657a7fSCristian Dumitrescu 			return -1;
1275f657a7fSCristian Dumitrescu 		}
1285f657a7fSCristian Dumitrescu 
1295f657a7fSCristian Dumitrescu 	optind = 1; /* reset getopt lib */
1305f657a7fSCristian Dumitrescu 
1315f657a7fSCristian Dumitrescu 	return 0;
1325f657a7fSCristian Dumitrescu }
1335f657a7fSCristian Dumitrescu 
134b77f6600SCristian Dumitrescu int
main(int argc,char ** argv)135b77f6600SCristian Dumitrescu main(int argc, char **argv)
136b77f6600SCristian Dumitrescu {
1375f657a7fSCristian Dumitrescu 	struct conn *conn;
138b77f6600SCristian Dumitrescu 	int status;
139b77f6600SCristian Dumitrescu 
1405f657a7fSCristian Dumitrescu 	/* Parse application arguments */
1415f657a7fSCristian Dumitrescu 	status = parse_args(argc, argv);
1425f657a7fSCristian Dumitrescu 	if (status < 0)
1435f657a7fSCristian Dumitrescu 		return status;
1445f657a7fSCristian Dumitrescu 
145b77f6600SCristian Dumitrescu 	/* EAL */
146b77f6600SCristian Dumitrescu 	status = rte_eal_init(argc, argv);
147b77f6600SCristian Dumitrescu 	if (status < 0) {
148b77f6600SCristian Dumitrescu 		printf("Error: EAL initialization failed (%d)\n", status);
149b77f6600SCristian Dumitrescu 		return status;
150b77f6600SCristian Dumitrescu 	};
151b77f6600SCristian Dumitrescu 
152b77f6600SCristian Dumitrescu 	/* Thread */
153b77f6600SCristian Dumitrescu 	status = thread_init();
154b77f6600SCristian Dumitrescu 	if (status) {
155b77f6600SCristian Dumitrescu 		printf("Error: Thread initialization failed (%d)\n", status);
156b77f6600SCristian Dumitrescu 		return status;
157b77f6600SCristian Dumitrescu 	}
158b77f6600SCristian Dumitrescu 
159b77f6600SCristian Dumitrescu 	rte_eal_mp_remote_launch(
160b77f6600SCristian Dumitrescu 		thread_main,
161b77f6600SCristian Dumitrescu 		NULL,
162cb056611SStephen Hemminger 		SKIP_MAIN);
163b77f6600SCristian Dumitrescu 
1645074e1d5SCristian Dumitrescu 	/* Script */
1655074e1d5SCristian Dumitrescu 	if (app.script_name)
1665074e1d5SCristian Dumitrescu 		cli_script_process(app.script_name,
1675074e1d5SCristian Dumitrescu 			app.conn.msg_in_len_max,
1685074e1d5SCristian Dumitrescu 			app.conn.msg_out_len_max,
169*78dffe31SCristian Dumitrescu 			NULL);
1705074e1d5SCristian Dumitrescu 
1715f657a7fSCristian Dumitrescu 	/* Connectivity */
172*78dffe31SCristian Dumitrescu 	app.conn.msg_handle_arg = NULL;
1735f657a7fSCristian Dumitrescu 	conn = conn_init(&app.conn);
1745f657a7fSCristian Dumitrescu 	if (!conn) {
1755f657a7fSCristian Dumitrescu 		printf("Error: Connectivity initialization failed (%d)\n",
1765f657a7fSCristian Dumitrescu 			status);
1775f657a7fSCristian Dumitrescu 		return status;
1785f657a7fSCristian Dumitrescu 	};
1795f657a7fSCristian Dumitrescu 
1805f657a7fSCristian Dumitrescu 	/* Dispatch loop */
1815f657a7fSCristian Dumitrescu 	for ( ; ; ) {
1825f657a7fSCristian Dumitrescu 		conn_poll_for_conn(conn);
1835f657a7fSCristian Dumitrescu 
1845f657a7fSCristian Dumitrescu 		conn_poll_for_msg(conn);
1855f657a7fSCristian Dumitrescu 	}
18610aa3757SChengchang Tang 
18710aa3757SChengchang Tang 	/* clean up the EAL */
18810aa3757SChengchang Tang 	rte_eal_cleanup();
189b77f6600SCristian Dumitrescu }
190