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