Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2023 Marvell.
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <fcntl.h>
7 : : #include <getopt.h>
8 : : #include <signal.h>
9 : : #include <stdio.h>
10 : : #include <stdlib.h>
11 : : #include <string.h>
12 : : #include <sys/select.h>
13 : : #include <unistd.h>
14 : :
15 : : #include <rte_cycles.h>
16 : : #include <rte_eal.h>
17 : : #include <rte_launch.h>
18 : :
19 : : #include "module_api.h"
20 : :
21 : : volatile bool force_quit;
22 : : struct conn *conn;
23 : :
24 : : static const char usage[] = "%s EAL_ARGS -- -s SCRIPT [-h HOST] [-p PORT] [--enable-graph-stats] "
25 : : "[--help]\n";
26 : :
27 : : static struct app_params {
28 : : struct conn_params conn;
29 : : char *script_name;
30 : : bool enable_graph_stats;
31 : : bool enable_feature_arc;
32 : : } app = {
33 : : .conn = {
34 : : .welcome = "\nWelcome!\n\n",
35 : : .prompt = "graph> ",
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 : : .msg_handle_arg = NULL, /* set later. */
43 : : },
44 : : .script_name = NULL,
45 : : .enable_graph_stats = false,
46 : : .enable_feature_arc = false,
47 : : };
48 : :
49 : : static void
50 : 0 : signal_handler(int signum)
51 : : {
52 : 0 : if (signum == SIGINT || signum == SIGTERM)
53 : 0 : force_quit = true;
54 : 0 : }
55 : :
56 : : static int
57 : 0 : app_args_parse(int argc, char **argv)
58 : : {
59 : 0 : struct option lgopts[] = {
60 : : {"help", 0, 0, 'H'},
61 : : {"enable-graph-stats", 0, 0, 'g'},
62 : : {"enable-graph-feature-arc", 0, 0, 'f'},
63 : : };
64 : : int h_present, p_present, s_present, n_args, i;
65 : 0 : char *app_name = argv[0];
66 : : int opt, option_index;
67 : :
68 : : /* Skip EAL input args */
69 : : n_args = argc;
70 : 0 : for (i = 0; i < n_args; i++)
71 : 0 : if (strcmp(argv[i], "--") == 0) {
72 : 0 : argc -= i;
73 : : argv += i;
74 : 0 : break;
75 : : }
76 : :
77 : 0 : if (i == n_args)
78 : : return 0;
79 : :
80 : : /* Parse args */
81 : : h_present = 0;
82 : : p_present = 0;
83 : : s_present = 0;
84 : :
85 : 0 : while ((opt = getopt_long(argc, argv, "h:p:s:f", lgopts, &option_index)) != EOF) {
86 : 0 : switch (opt) {
87 : 0 : case 'h':
88 : 0 : if (h_present) {
89 : : printf("Error: Multiple -h arguments\n");
90 : 0 : return -1;
91 : : }
92 : : h_present = 1;
93 : :
94 : 0 : if (!strlen(optarg)) {
95 : : printf("Error: Argument for -h not provided\n");
96 : 0 : return -1;
97 : : }
98 : :
99 : 0 : app.conn.addr = strdup(optarg);
100 : 0 : if (app.conn.addr == NULL) {
101 : : printf("Error: Not enough memory\n");
102 : 0 : return -1;
103 : : }
104 : : break;
105 : :
106 : 0 : case 'p':
107 : 0 : if (p_present) {
108 : : printf("Error: Multiple -p arguments\n");
109 : 0 : return -1;
110 : : }
111 : : p_present = 1;
112 : :
113 : 0 : if (!strlen(optarg)) {
114 : : printf("Error: Argument for -p not provided\n");
115 : 0 : return -1;
116 : : }
117 : :
118 : 0 : app.conn.port = (uint16_t)strtoul(optarg, NULL, 10);
119 : 0 : break;
120 : :
121 : 0 : case 's':
122 : 0 : if (s_present) {
123 : : printf("Error: Multiple -s arguments\n");
124 : 0 : return -1;
125 : : }
126 : : s_present = 1;
127 : :
128 : 0 : if (!strlen(optarg)) {
129 : : printf("Error: Argument for -s not provided\n");
130 : 0 : return -1;
131 : : }
132 : :
133 : 0 : app.script_name = strdup(optarg);
134 : 0 : if (app.script_name == NULL) {
135 : : printf("Error: Not enough memory\n");
136 : 0 : return -1;
137 : : }
138 : : break;
139 : :
140 : 0 : case 'g':
141 : 0 : app.enable_graph_stats = true;
142 : : printf("WARNING! Telnet session can not be accessed with"
143 : : "--enable-graph-stats");
144 : : break;
145 : :
146 : 0 : case 'f':
147 : 0 : app.enable_feature_arc = true;
148 : 0 : break;
149 : :
150 : : case 'H':
151 : : default:
152 : : printf(usage, app_name);
153 : 0 : return -1;
154 : : }
155 : : }
156 : 0 : optind = 1; /* reset getopt lib */
157 : :
158 : 0 : return 0;
159 : : }
160 : :
161 : : bool
162 : 0 : app_graph_stats_enabled(void)
163 : : {
164 : 0 : return app.enable_graph_stats;
165 : : }
166 : :
167 : : bool
168 : 0 : app_graph_feature_arc_enabled(void)
169 : : {
170 : 0 : return app.enable_feature_arc;
171 : : }
172 : :
173 : : bool
174 : 0 : app_graph_exit(void)
175 : : {
176 : : struct timeval tv;
177 : : fd_set fds;
178 : : int ret;
179 : : char c;
180 : :
181 : 0 : FD_ZERO(&fds);
182 : 0 : FD_SET(0, &fds);
183 : 0 : tv.tv_sec = 0;
184 : 0 : tv.tv_usec = 100;
185 : 0 : ret = select(1, &fds, NULL, NULL, &tv);
186 : 0 : if ((ret < 0 && errno == EINTR) || (ret == 1 && read(0, &c, 1) > 0))
187 : 0 : return true;
188 : : else
189 : 0 : return false;
190 : :
191 : : }
192 : :
193 : : int
194 : 0 : main(int argc, char **argv)
195 : : {
196 : : int rc;
197 : :
198 : : /* Parse application arguments */
199 : 0 : rc = app_args_parse(argc, argv);
200 : 0 : if (rc < 0)
201 : : return rc;
202 : :
203 : : /* EAL */
204 : 0 : rc = rte_eal_init(argc, argv);
205 : 0 : if (rc < 0) {
206 : : printf("Error: EAL initialization failed (%d)\n", rc);
207 : 0 : return rc;
208 : : };
209 : :
210 : 0 : force_quit = false;
211 : 0 : signal(SIGINT, signal_handler);
212 : 0 : signal(SIGTERM, signal_handler);
213 : :
214 : 0 : cli_init();
215 : :
216 : : /* Script */
217 : 0 : if (app.script_name) {
218 : 0 : cli_script_process(app.script_name, app.conn.msg_in_len_max,
219 : : app.conn.msg_out_len_max, NULL);
220 : : }
221 : :
222 : : /* Connectivity */
223 : 0 : app.conn.msg_handle_arg = NULL;
224 : 0 : conn = conn_init(&app.conn);
225 : 0 : if (!conn) {
226 : : printf("Error: Connectivity initialization failed\n");
227 : 0 : goto exit;
228 : : };
229 : :
230 : : rte_delay_ms(1);
231 : : printf("Press enter to exit\n");
232 : :
233 : : /* Dispatch loop */
234 : 0 : while (!force_quit) {
235 : 0 : conn_req_poll(conn);
236 : :
237 : 0 : conn_msg_poll(conn);
238 : 0 : if (app_graph_exit())
239 : 0 : force_quit = true;
240 : : }
241 : :
242 : 0 : exit:
243 : 0 : conn_free(conn);
244 : 0 : ethdev_stop();
245 : 0 : cli_exit();
246 : 0 : rte_eal_cleanup();
247 : 0 : return 0;
248 : : }
|