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