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