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