xref: /dpdk/examples/pipeline/main.c (revision cb056611a8ed9ab9024f3b91bf26e97255194514)
1b77f6600SCristian Dumitrescu /* SPDX-License-Identifier: BSD-3-Clause
2b77f6600SCristian Dumitrescu  * Copyright(c) 2020 Intel Corporation
3b77f6600SCristian Dumitrescu  */
4b77f6600SCristian Dumitrescu 
5b77f6600SCristian Dumitrescu #include <stdio.h>
6b77f6600SCristian Dumitrescu #include <string.h>
7b77f6600SCristian Dumitrescu #include <fcntl.h>
8b77f6600SCristian Dumitrescu #include <unistd.h>
9b77f6600SCristian Dumitrescu #include <getopt.h>
10b77f6600SCristian Dumitrescu 
11b77f6600SCristian Dumitrescu #include <rte_launch.h>
12b77f6600SCristian Dumitrescu #include <rte_eal.h>
13b77f6600SCristian Dumitrescu 
145074e1d5SCristian Dumitrescu #include "cli.h"
155f657a7fSCristian Dumitrescu #include "conn.h"
16b77f6600SCristian Dumitrescu #include "obj.h"
17b77f6600SCristian Dumitrescu #include "thread.h"
18b77f6600SCristian Dumitrescu 
195f657a7fSCristian Dumitrescu static const char usage[] =
205f657a7fSCristian Dumitrescu 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
215f657a7fSCristian Dumitrescu 
225f657a7fSCristian Dumitrescu static struct app_params {
235f657a7fSCristian Dumitrescu 	struct conn_params conn;
245f657a7fSCristian Dumitrescu 	char *script_name;
255f657a7fSCristian Dumitrescu } app = {
265f657a7fSCristian Dumitrescu 	.conn = {
275f657a7fSCristian Dumitrescu 		.welcome = "\nWelcome!\n\n",
285f657a7fSCristian Dumitrescu 		.prompt = "pipeline> ",
295f657a7fSCristian Dumitrescu 		.addr = "0.0.0.0",
305f657a7fSCristian Dumitrescu 		.port = 8086,
315f657a7fSCristian Dumitrescu 		.buf_size = 1024 * 1024,
325f657a7fSCristian Dumitrescu 		.msg_in_len_max = 1024,
335f657a7fSCristian Dumitrescu 		.msg_out_len_max = 1024 * 1024,
345074e1d5SCristian Dumitrescu 		.msg_handle = cli_process,
355f657a7fSCristian Dumitrescu 		.msg_handle_arg = NULL, /* set later. */
365f657a7fSCristian Dumitrescu 	},
375f657a7fSCristian Dumitrescu 	.script_name = NULL,
385f657a7fSCristian Dumitrescu };
395f657a7fSCristian Dumitrescu 
405f657a7fSCristian Dumitrescu static int
415f657a7fSCristian Dumitrescu parse_args(int argc, char **argv)
425f657a7fSCristian Dumitrescu {
435f657a7fSCristian Dumitrescu 	char *app_name = argv[0];
445f657a7fSCristian Dumitrescu 	struct option lgopts[] = {
455f657a7fSCristian Dumitrescu 		{ NULL,  0, 0, 0 }
465f657a7fSCristian Dumitrescu 	};
475f657a7fSCristian Dumitrescu 	int opt, option_index;
485f657a7fSCristian Dumitrescu 	int h_present, p_present, s_present, n_args, i;
495f657a7fSCristian Dumitrescu 
505f657a7fSCristian Dumitrescu 	/* Skip EAL input args */
515f657a7fSCristian Dumitrescu 	n_args = argc;
525f657a7fSCristian Dumitrescu 	for (i = 0; i < n_args; i++)
535f657a7fSCristian Dumitrescu 		if (strcmp(argv[i], "--") == 0) {
545f657a7fSCristian Dumitrescu 			argc -= i;
555f657a7fSCristian Dumitrescu 			argv += i;
565f657a7fSCristian Dumitrescu 			break;
575f657a7fSCristian Dumitrescu 		}
585f657a7fSCristian Dumitrescu 
595f657a7fSCristian Dumitrescu 	if (i == n_args)
605f657a7fSCristian Dumitrescu 		return 0;
615f657a7fSCristian Dumitrescu 
625f657a7fSCristian Dumitrescu 	/* Parse args */
635f657a7fSCristian Dumitrescu 	h_present = 0;
645f657a7fSCristian Dumitrescu 	p_present = 0;
655f657a7fSCristian Dumitrescu 	s_present = 0;
665f657a7fSCristian Dumitrescu 
675f657a7fSCristian Dumitrescu 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
685f657a7fSCristian Dumitrescu 			!= EOF)
695f657a7fSCristian Dumitrescu 		switch (opt) {
705f657a7fSCristian Dumitrescu 		case 'h':
715f657a7fSCristian Dumitrescu 			if (h_present) {
725f657a7fSCristian Dumitrescu 				printf("Error: Multiple -h arguments\n");
735f657a7fSCristian Dumitrescu 				return -1;
745f657a7fSCristian Dumitrescu 			}
755f657a7fSCristian Dumitrescu 			h_present = 1;
765f657a7fSCristian Dumitrescu 
775f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
785f657a7fSCristian Dumitrescu 				printf("Error: Argument for -h not provided\n");
795f657a7fSCristian Dumitrescu 				return -1;
805f657a7fSCristian Dumitrescu 			}
815f657a7fSCristian Dumitrescu 
825f657a7fSCristian Dumitrescu 			app.conn.addr = strdup(optarg);
835f657a7fSCristian Dumitrescu 			if (app.conn.addr == NULL) {
845f657a7fSCristian Dumitrescu 				printf("Error: Not enough memory\n");
855f657a7fSCristian Dumitrescu 				return -1;
865f657a7fSCristian Dumitrescu 			}
875f657a7fSCristian Dumitrescu 			break;
885f657a7fSCristian Dumitrescu 
895f657a7fSCristian Dumitrescu 		case 'p':
905f657a7fSCristian Dumitrescu 			if (p_present) {
915f657a7fSCristian Dumitrescu 				printf("Error: Multiple -p arguments\n");
925f657a7fSCristian Dumitrescu 				return -1;
935f657a7fSCristian Dumitrescu 			}
945f657a7fSCristian Dumitrescu 			p_present = 1;
955f657a7fSCristian Dumitrescu 
965f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
975f657a7fSCristian Dumitrescu 				printf("Error: Argument for -p not provided\n");
985f657a7fSCristian Dumitrescu 				return -1;
995f657a7fSCristian Dumitrescu 			}
1005f657a7fSCristian Dumitrescu 
1015f657a7fSCristian Dumitrescu 			app.conn.port = (uint16_t) atoi(optarg);
1025f657a7fSCristian Dumitrescu 			break;
1035f657a7fSCristian Dumitrescu 
1045f657a7fSCristian Dumitrescu 		case 's':
1055f657a7fSCristian Dumitrescu 			if (s_present) {
1065f657a7fSCristian Dumitrescu 				printf("Error: Multiple -s arguments\n");
1075f657a7fSCristian Dumitrescu 				return -1;
1085f657a7fSCristian Dumitrescu 			}
1095f657a7fSCristian Dumitrescu 			s_present = 1;
1105f657a7fSCristian Dumitrescu 
1115f657a7fSCristian Dumitrescu 			if (!strlen(optarg)) {
1125f657a7fSCristian Dumitrescu 				printf("Error: Argument for -s not provided\n");
1135f657a7fSCristian Dumitrescu 				return -1;
1145f657a7fSCristian Dumitrescu 			}
1155f657a7fSCristian Dumitrescu 
1165f657a7fSCristian Dumitrescu 			app.script_name = strdup(optarg);
1175f657a7fSCristian Dumitrescu 			if (app.script_name == NULL) {
1185f657a7fSCristian Dumitrescu 				printf("Error: Not enough memory\n");
1195f657a7fSCristian Dumitrescu 				return -1;
1205f657a7fSCristian Dumitrescu 			}
1215f657a7fSCristian Dumitrescu 			break;
1225f657a7fSCristian Dumitrescu 
1235f657a7fSCristian Dumitrescu 		default:
1245f657a7fSCristian Dumitrescu 			printf(usage, app_name);
1255f657a7fSCristian Dumitrescu 			return -1;
1265f657a7fSCristian Dumitrescu 		}
1275f657a7fSCristian Dumitrescu 
1285f657a7fSCristian Dumitrescu 	optind = 1; /* reset getopt lib */
1295f657a7fSCristian Dumitrescu 
1305f657a7fSCristian Dumitrescu 	return 0;
1315f657a7fSCristian Dumitrescu }
1325f657a7fSCristian Dumitrescu 
133b77f6600SCristian Dumitrescu int
134b77f6600SCristian Dumitrescu main(int argc, char **argv)
135b77f6600SCristian Dumitrescu {
1365f657a7fSCristian Dumitrescu 	struct conn *conn;
137b77f6600SCristian Dumitrescu 	struct obj *obj;
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 	/* Obj */
153b77f6600SCristian Dumitrescu 	obj = obj_init();
154b77f6600SCristian Dumitrescu 	if (!obj) {
155b77f6600SCristian Dumitrescu 		printf("Error: Obj initialization failed (%d)\n", status);
156b77f6600SCristian Dumitrescu 		return status;
157b77f6600SCristian Dumitrescu 	}
158b77f6600SCristian Dumitrescu 
159b77f6600SCristian Dumitrescu 	/* Thread */
160b77f6600SCristian Dumitrescu 	status = thread_init();
161b77f6600SCristian Dumitrescu 	if (status) {
162b77f6600SCristian Dumitrescu 		printf("Error: Thread initialization failed (%d)\n", status);
163b77f6600SCristian Dumitrescu 		return status;
164b77f6600SCristian Dumitrescu 	}
165b77f6600SCristian Dumitrescu 
166b77f6600SCristian Dumitrescu 	rte_eal_mp_remote_launch(
167b77f6600SCristian Dumitrescu 		thread_main,
168b77f6600SCristian Dumitrescu 		NULL,
169*cb056611SStephen Hemminger 		SKIP_MAIN);
170b77f6600SCristian Dumitrescu 
1715074e1d5SCristian Dumitrescu 	/* Script */
1725074e1d5SCristian Dumitrescu 	if (app.script_name)
1735074e1d5SCristian Dumitrescu 		cli_script_process(app.script_name,
1745074e1d5SCristian Dumitrescu 			app.conn.msg_in_len_max,
1755074e1d5SCristian Dumitrescu 			app.conn.msg_out_len_max,
1765074e1d5SCristian Dumitrescu 			obj);
1775074e1d5SCristian Dumitrescu 
1785f657a7fSCristian Dumitrescu 	/* Connectivity */
1795f657a7fSCristian Dumitrescu 	app.conn.msg_handle_arg = obj;
1805f657a7fSCristian Dumitrescu 	conn = conn_init(&app.conn);
1815f657a7fSCristian Dumitrescu 	if (!conn) {
1825f657a7fSCristian Dumitrescu 		printf("Error: Connectivity initialization failed (%d)\n",
1835f657a7fSCristian Dumitrescu 			status);
1845f657a7fSCristian Dumitrescu 		return status;
1855f657a7fSCristian Dumitrescu 	};
1865f657a7fSCristian Dumitrescu 
1875f657a7fSCristian Dumitrescu 	/* Dispatch loop */
1885f657a7fSCristian Dumitrescu 	for ( ; ; ) {
1895f657a7fSCristian Dumitrescu 		conn_poll_for_conn(conn);
1905f657a7fSCristian Dumitrescu 
1915f657a7fSCristian Dumitrescu 		conn_poll_for_msg(conn);
1925f657a7fSCristian Dumitrescu 	}
193b77f6600SCristian Dumitrescu }
194