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