xref: /dpdk/examples/ip_pipeline/main.c (revision 25961ff3bcb9ae8d7a46305a1565a33438333b7a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <getopt.h>
10 
11 #include <rte_eal.h>
12 
13 #include "cli.h"
14 #include "conn.h"
15 #include "link.h"
16 #include "mempool.h"
17 #include "swq.h"
18 #include "tmgr.h"
19 
20 static const char usage[] =
21 	"%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n";
22 
23 static const char welcome[] =
24 	"\n"
25 	"Welcome to IP Pipeline!\n"
26 	"\n";
27 
28 static const char prompt[] = "pipeline> ";
29 
30 static struct app_params {
31 	struct conn_params conn;
32 	char *script_name;
33 } app = {
34 	.conn = {
35 		.welcome = welcome,
36 		.prompt = prompt,
37 		.addr = "0.0.0.0",
38 		.port = 8086,
39 		.buf_size = 1024 * 1024,
40 		.msg_in_len_max = 1024,
41 		.msg_out_len_max = 1024 * 1024,
42 		.msg_handle = cli_process,
43 	},
44 	.script_name = NULL,
45 };
46 
47 static int
48 parse_args(int argc, char **argv)
49 {
50 	char *app_name = argv[0];
51 	struct option lgopts[] = {
52 		{ NULL,  0, 0, 0 }
53 	};
54 	int opt, option_index;
55 	int h_present, p_present, s_present, n_args, i;
56 
57 	/* Skip EAL input args */
58 	n_args = argc;
59 	for (i = 0; i < n_args; i++)
60 		if (strcmp(argv[i], "--") == 0) {
61 			argc -= i;
62 			argv += i;
63 			break;
64 		}
65 
66 	if (i == n_args)
67 		return 0;
68 
69 	/* Parse args */
70 	h_present = 0;
71 	p_present = 0;
72 	s_present = 0;
73 
74 	while ((opt = getopt_long(argc, argv, "h:p:s:", lgopts, &option_index))
75 			!= EOF)
76 		switch (opt) {
77 		case 'h':
78 			if (h_present) {
79 				printf("Error: Multiple -h arguments\n");
80 				return -1;
81 			}
82 			h_present = 1;
83 
84 			if (!strlen(optarg)) {
85 				printf("Error: Argument for -h not provided\n");
86 				return -1;
87 			}
88 
89 			app.conn.addr = strdup(optarg);
90 			if (app.conn.addr == NULL) {
91 				printf("Error: Not enough memory\n");
92 				return -1;
93 			}
94 			break;
95 
96 		case 'p':
97 			if (p_present) {
98 				printf("Error: Multiple -p arguments\n");
99 				return -1;
100 			}
101 			p_present = 1;
102 
103 			if (!strlen(optarg)) {
104 				printf("Error: Argument for -p not provided\n");
105 				return -1;
106 			}
107 
108 			app.conn.port = (uint16_t) atoi(optarg);
109 			break;
110 
111 		case 's':
112 			if (s_present) {
113 				printf("Error: Multiple -s arguments\n");
114 				return -1;
115 			}
116 			s_present = 1;
117 
118 			if (!strlen(optarg)) {
119 				printf("Error: Argument for -s not provided\n");
120 				return -1;
121 			}
122 
123 			app.script_name = strdup(optarg);
124 			if (app.script_name == NULL) {
125 				printf("Error: Not enough memory\n");
126 				return -1;
127 			}
128 			break;
129 
130 		default:
131 			printf(usage, app_name);
132 			return -1;
133 		}
134 
135 	optind = 1; /* reset getopt lib */
136 
137 	return 0;
138 }
139 
140 int
141 main(int argc, char **argv)
142 {
143 	struct conn *conn;
144 	int status;
145 
146 	/* Parse application arguments */
147 	status = parse_args(argc, argv);
148 	if (status < 0)
149 		return status;
150 
151 	/* EAL */
152 	status = rte_eal_init(argc, argv);
153 	if (status < 0) {
154 		printf("Error: EAL initialization failed (%d)\n", status);
155 		return status;
156 	};
157 
158 	/* Connectivity */
159 	conn = conn_init(&app.conn);
160 	if (conn == NULL) {
161 		printf("Error: Connectivity initialization failed (%d)\n",
162 			status);
163 		return status;
164 	};
165 
166 	/* Mempool */
167 	status = mempool_init();
168 	if (status) {
169 		printf("Error: Mempool initialization failed (%d)\n", status);
170 		return status;
171 	}
172 
173 	/* Link */
174 	status = link_init();
175 	if (status) {
176 		printf("Error: Link initialization failed (%d)\n", status);
177 		return status;
178 	}
179 
180 	/* SWQ */
181 	status = swq_init();
182 	if (status) {
183 		printf("Error: SWQ initialization failed (%d)\n", status);
184 		return status;
185 	}
186 
187 	/* Traffic Manager */
188 	status = tmgr_init();
189 	if (status) {
190 		printf("Error: TMGR initialization failed (%d)\n", status);
191 		return status;
192 	}
193 
194 	/* Script */
195 	if (app.script_name)
196 		cli_script_process(app.script_name,
197 			app.conn.msg_in_len_max,
198 			app.conn.msg_out_len_max);
199 
200 	/* Dispatch loop */
201 	for ( ; ; ) {
202 		conn_poll_for_conn(conn);
203 
204 		conn_poll_for_msg(conn);
205 	}
206 }
207