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