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