]>
Commit | Line | Data |
---|---|---|
fceef209 | 1 | /* |
d40533fc | 2 | * Copyright (c) 2008-2019 Nicira, Inc. |
fceef209 DDP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
b2befd5b BP |
18 | #include <sys/types.h> |
19 | #include <netinet/in.h> | |
fceef209 DDP |
20 | #include <arpa/inet.h> |
21 | #include <errno.h> | |
22 | #include <inttypes.h> | |
23 | #include <sys/socket.h> | |
24 | #include <net/if.h> | |
fceef209 DDP |
25 | #include <stdarg.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | ||
30 | #include "command-line.h" | |
31 | #include "compiler.h" | |
dffae65f | 32 | #include "ct-dpif.h" |
fceef209 DDP |
33 | #include "dirs.h" |
34 | #include "dpctl.h" | |
35 | #include "dpif.h" | |
4ea96698 | 36 | #include "dpif-provider.h" |
3e8a2ad1 | 37 | #include "openvswitch/dynamic-string.h" |
fceef209 | 38 | #include "flow.h" |
e29747e4 | 39 | #include "openvswitch/match.h" |
fceef209 DDP |
40 | #include "netdev.h" |
41 | #include "netlink.h" | |
42 | #include "odp-util.h" | |
64c96779 | 43 | #include "openvswitch/ofpbuf.h" |
fceef209 | 44 | #include "packets.h" |
ee89ea7b | 45 | #include "openvswitch/shash.h" |
fceef209 DDP |
46 | #include "simap.h" |
47 | #include "smap.h" | |
48 | #include "sset.h" | |
49 | #include "timeval.h" | |
50 | #include "unixctl.h" | |
51 | #include "util.h" | |
0d71302e BP |
52 | #include "openvswitch/ofp-flow.h" |
53 | #include "openvswitch/ofp-port.h" | |
fceef209 | 54 | |
56cad666 AW |
55 | typedef int dpctl_command_handler(int argc, const char *argv[], |
56 | struct dpctl_params *); | |
57 | struct dpctl_command { | |
58 | const char *name; | |
59 | const char *usage; | |
60 | int min_args; | |
61 | int max_args; | |
62 | dpctl_command_handler *handler; | |
1f4a7252 | 63 | enum { DP_RO, DP_RW} mode; |
56cad666 AW |
64 | }; |
65 | static const struct dpctl_command *get_all_dpctl_commands(void); | |
7521b9e5 DDP |
66 | static void dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...) |
67 | OVS_PRINTF_FORMAT(2, 3); | |
68 | static void dpctl_error(struct dpctl_params* dpctl_p, int err_no, | |
69 | const char *fmt, ...) | |
70 | OVS_PRINTF_FORMAT(3, 4); | |
56cad666 | 71 | |
fceef209 DDP |
72 | static void |
73 | dpctl_puts(struct dpctl_params *dpctl_p, bool error, const char *string) | |
74 | { | |
75 | dpctl_p->output(dpctl_p->aux, error, string); | |
76 | } | |
77 | ||
78 | static void | |
79 | dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...) | |
80 | { | |
81 | char *string; | |
82 | va_list args; | |
83 | ||
84 | va_start(args, fmt); | |
85 | string = xvasprintf(fmt, args); | |
86 | va_end(args); | |
87 | ||
88 | dpctl_puts(dpctl_p, false, string); | |
89 | free(string); | |
90 | } | |
91 | ||
92 | static void | |
93 | dpctl_error(struct dpctl_params* dpctl_p, int err_no, const char *fmt, ...) | |
94 | { | |
95 | const char *subprogram_name = get_subprogram_name(); | |
96 | struct ds ds = DS_EMPTY_INITIALIZER; | |
97 | int save_errno = errno; | |
98 | va_list args; | |
99 | ||
100 | ||
101 | if (subprogram_name[0]) { | |
102 | ds_put_format(&ds, "%s(%s): ", program_name,subprogram_name); | |
103 | } else { | |
104 | ds_put_format(&ds, "%s: ", program_name); | |
105 | } | |
106 | ||
107 | va_start(args, fmt); | |
108 | ds_put_format_valist(&ds, fmt, args); | |
109 | va_end(args); | |
110 | ||
111 | if (err_no != 0) { | |
112 | ds_put_format(&ds, " (%s)", ovs_retval_to_string(err_no)); | |
113 | } | |
114 | ds_put_cstr(&ds, "\n"); | |
115 | ||
116 | dpctl_puts(dpctl_p, true, ds_cstr(&ds)); | |
117 | ||
118 | ds_destroy(&ds); | |
119 | ||
120 | errno = save_errno; | |
121 | } | |
122 | \f | |
123 | static int dpctl_add_if(int argc, const char *argv[], struct dpctl_params *); | |
124 | ||
125 | static int | |
126 | if_up(struct netdev *netdev) | |
127 | { | |
128 | return netdev_turn_flags_on(netdev, NETDEV_UP, NULL); | |
129 | } | |
130 | ||
131 | /* Retrieve the name of the datapath if exactly one exists. The caller | |
0bd28b0b JP |
132 | * is responsible for freeing the returned string. If a single datapath |
133 | * name cannot be determined, returns NULL. */ | |
fceef209 DDP |
134 | static char * |
135 | get_one_dp(struct dpctl_params *dpctl_p) | |
136 | { | |
137 | struct sset types; | |
138 | const char *type; | |
139 | char *dp_name = NULL; | |
140 | size_t count = 0; | |
141 | ||
142 | sset_init(&types); | |
143 | dp_enumerate_types(&types); | |
144 | SSET_FOR_EACH (type, &types) { | |
145 | struct sset names; | |
146 | ||
147 | sset_init(&names); | |
148 | if (!dp_enumerate_names(type, &names)) { | |
149 | count += sset_count(&names); | |
150 | if (!dp_name && count == 1) { | |
151 | dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names)); | |
152 | } | |
153 | } | |
154 | sset_destroy(&names); | |
155 | } | |
156 | sset_destroy(&types); | |
157 | ||
158 | if (!count) { | |
159 | dpctl_error(dpctl_p, 0, "no datapaths exist"); | |
160 | } else if (count > 1) { | |
161 | dpctl_error(dpctl_p, 0, "multiple datapaths, specify one"); | |
162 | free(dp_name); | |
163 | dp_name = NULL; | |
164 | } | |
165 | ||
166 | return dp_name; | |
167 | } | |
168 | ||
169 | static int | |
170 | parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp) | |
171 | { | |
172 | int result; | |
173 | char *name, *type; | |
174 | ||
175 | dp_parse_name(arg_, &name, &type); | |
176 | ||
177 | if (create) { | |
178 | result = dpif_create(name, type, dpifp); | |
179 | } else { | |
180 | result = dpif_open(name, type, dpifp); | |
181 | } | |
182 | ||
183 | free(name); | |
184 | free(type); | |
185 | return result; | |
186 | } | |
187 | ||
ffdcd110 DB |
188 | static bool |
189 | dp_exists(const char *queried_dp) | |
190 | { | |
191 | char *queried_name, *queried_type; | |
192 | dp_parse_name(queried_dp, &queried_name, &queried_type); | |
193 | struct sset dpif_names = SSET_INITIALIZER(&dpif_names), | |
194 | dpif_types = SSET_INITIALIZER(&dpif_types); | |
195 | dp_enumerate_types(&dpif_types); | |
196 | ||
197 | bool found = (sset_contains(&dpif_types, queried_type) && | |
198 | !dp_enumerate_names(queried_type, &dpif_names) && | |
199 | sset_contains(&dpif_names, queried_name)); | |
200 | ||
201 | sset_destroy(&dpif_names); | |
202 | sset_destroy(&dpif_types); | |
0f55bd29 YW |
203 | free(queried_name); |
204 | free(queried_type); | |
ffdcd110 DB |
205 | return found; |
206 | } | |
207 | ||
208 | static bool | |
209 | dp_arg_exists(int argc, const char *argv[]) | |
210 | { | |
211 | return argc > 1 && dp_exists(argv[1]); | |
212 | } | |
213 | ||
f33cdd7b JP |
214 | /* Open a dpif with an optional name argument. |
215 | * | |
ffdcd110 DB |
216 | * The datapath name is not a mandatory parameter for this command. If it is |
217 | * not specified, we retrieve it from the current setup, assuming only one | |
01dcd687 | 218 | * exists. On success stores the opened dpif in '*dpifp'. */ |
f33cdd7b JP |
219 | static int |
220 | opt_dpif_open(int argc, const char *argv[], struct dpctl_params *dpctl_p, | |
01dcd687 | 221 | int max_args, struct dpif **dpifp) |
f33cdd7b | 222 | { |
ffdcd110 | 223 | char *dpname; |
7cfb862c | 224 | |
ffdcd110 DB |
225 | if (dp_arg_exists(argc, argv)) { |
226 | dpname = xstrdup(argv[1]); | |
227 | } else if (argc != max_args) { | |
228 | dpname = get_one_dp(dpctl_p); | |
229 | } else { | |
230 | /* If the arguments are the maximum possible number and there is no | |
231 | * valid datapath argument, then we fall into the case of dpname is | |
232 | * NULL, since this is an error. */ | |
233 | dpname = NULL; | |
234 | } | |
235 | ||
f33cdd7b | 236 | int error = 0; |
f33cdd7b JP |
237 | if (!dpname) { |
238 | error = EINVAL; | |
239 | dpctl_error(dpctl_p, error, "datapath not found"); | |
240 | } else { | |
241 | error = parsed_dpif_open(dpname, false, dpifp); | |
242 | free(dpname); | |
243 | if (error) { | |
244 | dpctl_error(dpctl_p, error, "opening datapath"); | |
245 | } | |
246 | } | |
247 | return error; | |
248 | } | |
249 | ||
fceef209 DDP |
250 | static int |
251 | dpctl_add_dp(int argc, const char *argv[], | |
252 | struct dpctl_params *dpctl_p) | |
253 | { | |
254 | struct dpif *dpif; | |
255 | int error; | |
256 | ||
257 | error = parsed_dpif_open(argv[1], true, &dpif); | |
258 | if (error) { | |
259 | dpctl_error(dpctl_p, error, "add_dp"); | |
260 | return error; | |
261 | } | |
262 | dpif_close(dpif); | |
263 | if (argc > 2) { | |
264 | error = dpctl_add_if(argc, argv, dpctl_p); | |
265 | } | |
266 | return error; | |
267 | } | |
268 | ||
269 | static int | |
270 | dpctl_del_dp(int argc OVS_UNUSED, const char *argv[], | |
271 | struct dpctl_params *dpctl_p) | |
272 | { | |
273 | struct dpif *dpif; | |
274 | int error; | |
275 | ||
276 | error = parsed_dpif_open(argv[1], false, &dpif); | |
277 | if (error) { | |
278 | dpctl_error(dpctl_p, error, "opening datapath"); | |
279 | return error; | |
280 | } | |
281 | error = dpif_delete(dpif); | |
282 | if (error) { | |
283 | dpctl_error(dpctl_p, error, "del_dp"); | |
284 | } | |
285 | ||
286 | dpif_close(dpif); | |
287 | return error; | |
288 | } | |
289 | ||
290 | static int | |
291 | dpctl_add_if(int argc OVS_UNUSED, const char *argv[], | |
292 | struct dpctl_params *dpctl_p) | |
293 | { | |
294 | struct dpif *dpif; | |
295 | int i, error, lasterror = 0; | |
296 | ||
297 | error = parsed_dpif_open(argv[1], false, &dpif); | |
298 | if (error) { | |
299 | dpctl_error(dpctl_p, error, "opening datapath"); | |
300 | return error; | |
301 | } | |
302 | for (i = 2; i < argc; i++) { | |
303 | const char *name, *type; | |
304 | char *save_ptr = NULL, *argcopy; | |
305 | struct netdev *netdev = NULL; | |
306 | struct smap args; | |
307 | odp_port_t port_no = ODPP_NONE; | |
308 | char *option; | |
309 | ||
310 | argcopy = xstrdup(argv[i]); | |
311 | name = strtok_r(argcopy, ",", &save_ptr); | |
312 | type = "system"; | |
313 | ||
314 | if (!name) { | |
315 | dpctl_error(dpctl_p, 0, "%s is not a valid network device name", | |
316 | argv[i]); | |
317 | error = EINVAL; | |
318 | goto next; | |
319 | } | |
320 | ||
321 | smap_init(&args); | |
322 | while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) { | |
323 | char *save_ptr_2 = NULL; | |
324 | char *key, *value; | |
325 | ||
326 | key = strtok_r(option, "=", &save_ptr_2); | |
327 | value = strtok_r(NULL, "", &save_ptr_2); | |
328 | if (!value) { | |
329 | value = ""; | |
330 | } | |
331 | ||
332 | if (!strcmp(key, "type")) { | |
333 | type = value; | |
334 | } else if (!strcmp(key, "port_no")) { | |
335 | port_no = u32_to_odp(atoi(value)); | |
336 | } else if (!smap_add_once(&args, key, value)) { | |
337 | dpctl_error(dpctl_p, 0, "duplicate \"%s\" option", key); | |
338 | } | |
339 | } | |
340 | ||
341 | error = netdev_open(name, type, &netdev); | |
342 | if (error) { | |
343 | dpctl_error(dpctl_p, error, "%s: failed to open network device", | |
344 | name); | |
345 | goto next_destroy_args; | |
346 | } | |
347 | ||
348 | error = netdev_set_config(netdev, &args, NULL); | |
349 | if (error) { | |
350 | goto next_destroy_args; | |
351 | } | |
352 | ||
353 | error = dpif_port_add(dpif, netdev, &port_no); | |
354 | if (error) { | |
355 | dpctl_error(dpctl_p, error, "adding %s to %s failed", name, | |
356 | argv[1]); | |
357 | goto next_destroy_args; | |
358 | } | |
359 | ||
360 | error = if_up(netdev); | |
361 | if (error) { | |
362 | dpctl_error(dpctl_p, error, "%s: failed bringing interface up", | |
363 | name); | |
364 | } | |
365 | ||
366 | next_destroy_args: | |
367 | netdev_close(netdev); | |
368 | smap_destroy(&args); | |
369 | next: | |
370 | free(argcopy); | |
371 | if (error) { | |
372 | lasterror = error; | |
373 | } | |
374 | } | |
375 | dpif_close(dpif); | |
376 | ||
377 | return lasterror; | |
378 | } | |
379 | ||
380 | static int | |
381 | dpctl_set_if(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
382 | { | |
383 | struct dpif *dpif; | |
384 | int i, error, lasterror = 0; | |
385 | ||
386 | error = parsed_dpif_open(argv[1], false, &dpif); | |
387 | if (error) { | |
388 | dpctl_error(dpctl_p, error, "opening datapath"); | |
389 | return error; | |
390 | } | |
391 | for (i = 2; i < argc; i++) { | |
392 | struct netdev *netdev = NULL; | |
393 | struct dpif_port dpif_port; | |
394 | char *save_ptr = NULL; | |
395 | char *type = NULL; | |
396 | char *argcopy; | |
397 | const char *name; | |
398 | struct smap args; | |
399 | odp_port_t port_no; | |
400 | char *option; | |
71f21279 BP |
401 | |
402 | error = 0; | |
fceef209 DDP |
403 | |
404 | argcopy = xstrdup(argv[i]); | |
405 | name = strtok_r(argcopy, ",", &save_ptr); | |
406 | if (!name) { | |
407 | dpctl_error(dpctl_p, 0, "%s is not a valid network device name", | |
408 | argv[i]); | |
409 | goto next; | |
410 | } | |
411 | ||
412 | /* Get the port's type from the datapath. */ | |
413 | error = dpif_port_query_by_name(dpif, name, &dpif_port); | |
414 | if (error) { | |
415 | dpctl_error(dpctl_p, error, "%s: failed to query port in %s", name, | |
416 | argv[1]); | |
417 | goto next; | |
418 | } | |
419 | type = xstrdup(dpif_port.type); | |
420 | port_no = dpif_port.port_no; | |
421 | dpif_port_destroy(&dpif_port); | |
422 | ||
423 | /* Retrieve its existing configuration. */ | |
424 | error = netdev_open(name, type, &netdev); | |
425 | if (error) { | |
426 | dpctl_error(dpctl_p, error, "%s: failed to open network device", | |
427 | name); | |
428 | goto next; | |
429 | } | |
430 | ||
431 | smap_init(&args); | |
432 | error = netdev_get_config(netdev, &args); | |
433 | if (error) { | |
434 | dpctl_error(dpctl_p, error, "%s: failed to fetch configuration", | |
435 | name); | |
436 | goto next_destroy_args; | |
437 | } | |
438 | ||
439 | /* Parse changes to configuration. */ | |
440 | while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) { | |
441 | char *save_ptr_2 = NULL; | |
442 | char *key, *value; | |
443 | ||
444 | key = strtok_r(option, "=", &save_ptr_2); | |
445 | value = strtok_r(NULL, "", &save_ptr_2); | |
446 | if (!value) { | |
447 | value = ""; | |
448 | } | |
449 | ||
450 | if (!strcmp(key, "type")) { | |
451 | if (strcmp(value, type)) { | |
452 | dpctl_error(dpctl_p, 0, | |
453 | "%s: can't change type from %s to %s", | |
454 | name, type, value); | |
455 | error = EINVAL; | |
79ae214d | 456 | goto next_destroy_args; |
fceef209 DDP |
457 | } |
458 | } else if (!strcmp(key, "port_no")) { | |
459 | if (port_no != u32_to_odp(atoi(value))) { | |
460 | dpctl_error(dpctl_p, 0, "%s: can't change port number from" | |
461 | " %"PRIu32" to %d", name, port_no, atoi(value)); | |
462 | error = EINVAL; | |
79ae214d | 463 | goto next_destroy_args; |
fceef209 DDP |
464 | } |
465 | } else if (value[0] == '\0') { | |
466 | smap_remove(&args, key); | |
467 | } else { | |
468 | smap_replace(&args, key, value); | |
469 | } | |
470 | } | |
471 | ||
472 | /* Update configuration. */ | |
79ae214d BP |
473 | char *err_s = NULL; |
474 | error = netdev_set_config(netdev, &args, &err_s); | |
475 | if (err_s || error) { | |
7521b9e5 | 476 | dpctl_error(dpctl_p, error, "%s", |
79ae214d BP |
477 | err_s ? err_s : "Error updating configuration"); |
478 | free(err_s); | |
479 | } | |
fceef209 DDP |
480 | if (error) { |
481 | goto next_destroy_args; | |
482 | } | |
483 | ||
484 | next_destroy_args: | |
485 | smap_destroy(&args); | |
486 | next: | |
487 | netdev_close(netdev); | |
488 | free(type); | |
489 | free(argcopy); | |
490 | if (error) { | |
491 | lasterror = error; | |
492 | } | |
493 | } | |
494 | dpif_close(dpif); | |
495 | ||
496 | return lasterror; | |
497 | } | |
498 | ||
499 | static bool | |
500 | get_port_number(struct dpif *dpif, const char *name, odp_port_t *port, | |
501 | struct dpctl_params *dpctl_p) | |
502 | { | |
503 | struct dpif_port dpif_port; | |
504 | ||
505 | if (!dpif_port_query_by_name(dpif, name, &dpif_port)) { | |
506 | *port = dpif_port.port_no; | |
507 | dpif_port_destroy(&dpif_port); | |
508 | return true; | |
509 | } else { | |
510 | dpctl_error(dpctl_p, 0, "no port named %s", name); | |
511 | return false; | |
512 | } | |
513 | } | |
514 | ||
515 | static int | |
516 | dpctl_del_if(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
517 | { | |
518 | struct dpif *dpif; | |
519 | int i, error, lasterror = 0; | |
520 | ||
521 | error = parsed_dpif_open(argv[1], false, &dpif); | |
522 | if (error) { | |
523 | dpctl_error(dpctl_p, error, "opening datapath"); | |
524 | return error; | |
525 | } | |
526 | for (i = 2; i < argc; i++) { | |
527 | const char *name = argv[i]; | |
528 | odp_port_t port; | |
529 | ||
530 | if (!name[strspn(name, "0123456789")]) { | |
531 | port = u32_to_odp(atoi(name)); | |
532 | } else if (!get_port_number(dpif, name, &port, dpctl_p)) { | |
533 | lasterror = ENOENT; | |
534 | continue; | |
535 | } | |
536 | ||
97459c2f | 537 | error = dpif_port_del(dpif, port, false); |
fceef209 DDP |
538 | if (error) { |
539 | dpctl_error(dpctl_p, error, "deleting port %s from %s failed", | |
540 | name, argv[1]); | |
541 | lasterror = error; | |
542 | } | |
543 | } | |
544 | dpif_close(dpif); | |
545 | return lasterror; | |
546 | } | |
547 | ||
548 | static void | |
549 | print_stat(struct dpctl_params *dpctl_p, const char *leader, uint64_t value) | |
550 | { | |
551 | dpctl_print(dpctl_p, "%s", leader); | |
552 | if (value != UINT64_MAX) { | |
553 | dpctl_print(dpctl_p, "%"PRIu64, value); | |
554 | } else { | |
555 | dpctl_print(dpctl_p, "?"); | |
556 | } | |
557 | } | |
558 | ||
559 | static void | |
560 | print_human_size(struct dpctl_params *dpctl_p, uint64_t value) | |
561 | { | |
562 | if (value == UINT64_MAX) { | |
563 | /* Nothing to do. */ | |
564 | } else if (value >= 1024ULL * 1024 * 1024 * 1024) { | |
565 | dpctl_print(dpctl_p, " (%.1f TiB)", | |
566 | value / (1024.0 * 1024 * 1024 * 1024)); | |
567 | } else if (value >= 1024ULL * 1024 * 1024) { | |
568 | dpctl_print(dpctl_p, " (%.1f GiB)", value / (1024.0 * 1024 * 1024)); | |
569 | } else if (value >= 1024ULL * 1024) { | |
570 | dpctl_print(dpctl_p, " (%.1f MiB)", value / (1024.0 * 1024)); | |
571 | } else if (value >= 1024) { | |
572 | dpctl_print(dpctl_p, " (%.1f KiB)", value / 1024.0); | |
573 | } | |
574 | } | |
575 | ||
3ff1613e JP |
576 | /* qsort comparison function. */ |
577 | static int | |
578 | compare_port_nos(const void *a_, const void *b_) | |
579 | { | |
580 | const odp_port_t *ap = a_; | |
581 | const odp_port_t *bp = b_; | |
582 | uint32_t a = odp_to_u32(*ap); | |
583 | uint32_t b = odp_to_u32(*bp); | |
584 | ||
585 | return a < b ? -1 : a > b; | |
586 | } | |
587 | ||
fceef209 DDP |
588 | static void |
589 | show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p) | |
590 | { | |
591 | struct dpif_port_dump dump; | |
592 | struct dpif_port dpif_port; | |
593 | struct dpif_dp_stats stats; | |
594 | struct netdev *netdev; | |
595 | ||
596 | dpctl_print(dpctl_p, "%s:\n", dpif_name(dpif)); | |
597 | if (!dpif_get_dp_stats(dpif, &stats)) { | |
d14a36f4 BP |
598 | dpctl_print(dpctl_p, " lookups: hit:%"PRIu64" missed:%"PRIu64 |
599 | " lost:%"PRIu64"\n flows: %"PRIu64"\n", | |
fceef209 DDP |
600 | stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows); |
601 | if (stats.n_masks != UINT32_MAX) { | |
602 | uint64_t n_pkts = stats.n_hit + stats.n_missed; | |
603 | double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0; | |
604 | ||
d14a36f4 | 605 | dpctl_print(dpctl_p, " masks: hit:%"PRIu64" total:%"PRIu32 |
fceef209 DDP |
606 | " hit/pkt:%.2f\n", |
607 | stats.n_mask_hit, stats.n_masks, avg); | |
608 | } | |
609 | } | |
610 | ||
3ff1613e JP |
611 | odp_port_t *port_nos = NULL; |
612 | size_t allocated_port_nos = 0, n_port_nos = 0; | |
fceef209 | 613 | DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) { |
3ff1613e JP |
614 | if (n_port_nos >= allocated_port_nos) { |
615 | port_nos = x2nrealloc(port_nos, &allocated_port_nos, | |
616 | sizeof *port_nos); | |
617 | } | |
618 | ||
619 | port_nos[n_port_nos] = dpif_port.port_no; | |
620 | n_port_nos++; | |
621 | } | |
622 | ||
f77ceea3 BB |
623 | if (port_nos) { |
624 | qsort(port_nos, n_port_nos, sizeof *port_nos, compare_port_nos); | |
625 | } | |
3ff1613e JP |
626 | |
627 | for (int i = 0; i < n_port_nos; i++) { | |
628 | if (dpif_port_query_by_number(dpif, port_nos[i], &dpif_port)) { | |
629 | continue; | |
630 | } | |
631 | ||
d14a36f4 | 632 | dpctl_print(dpctl_p, " port %u: %s", |
fceef209 DDP |
633 | dpif_port.port_no, dpif_port.name); |
634 | ||
635 | if (strcmp(dpif_port.type, "system")) { | |
636 | int error; | |
637 | ||
638 | dpctl_print(dpctl_p, " (%s", dpif_port.type); | |
639 | ||
640 | error = netdev_open(dpif_port.name, dpif_port.type, &netdev); | |
641 | if (!error) { | |
642 | struct smap config; | |
643 | ||
644 | smap_init(&config); | |
645 | error = netdev_get_config(netdev, &config); | |
646 | if (!error) { | |
270fa6da JP |
647 | const struct smap_node **nodes = smap_sort(&config); |
648 | for (size_t j = 0; j < smap_count(&config); j++) { | |
649 | const struct smap_node *node = nodes[j]; | |
650 | dpctl_print(dpctl_p, "%c %s=%s", j ? ',' : ':', | |
fceef209 DDP |
651 | node->key, node->value); |
652 | } | |
653 | free(nodes); | |
654 | } else { | |
655 | dpctl_print(dpctl_p, ", could not retrieve configuration " | |
656 | "(%s)", ovs_strerror(error)); | |
657 | } | |
658 | smap_destroy(&config); | |
659 | ||
660 | netdev_close(netdev); | |
661 | } else { | |
662 | dpctl_print(dpctl_p, ": open failed (%s)", | |
663 | ovs_strerror(error)); | |
664 | } | |
665 | dpctl_print(dpctl_p, ")"); | |
666 | } | |
667 | dpctl_print(dpctl_p, "\n"); | |
668 | ||
669 | if (dpctl_p->print_statistics) { | |
670 | struct netdev_stats s; | |
671 | int error; | |
672 | ||
673 | error = netdev_open(dpif_port.name, dpif_port.type, &netdev); | |
674 | if (error) { | |
675 | dpctl_print(dpctl_p, ", open failed (%s)", | |
676 | ovs_strerror(error)); | |
3ff1613e | 677 | dpif_port_destroy(&dpif_port); |
fceef209 DDP |
678 | continue; |
679 | } | |
680 | error = netdev_get_stats(netdev, &s); | |
681 | if (!error) { | |
682 | netdev_close(netdev); | |
d14a36f4 | 683 | print_stat(dpctl_p, " RX packets:", s.rx_packets); |
fceef209 DDP |
684 | print_stat(dpctl_p, " errors:", s.rx_errors); |
685 | print_stat(dpctl_p, " dropped:", s.rx_dropped); | |
686 | print_stat(dpctl_p, " overruns:", s.rx_over_errors); | |
687 | print_stat(dpctl_p, " frame:", s.rx_frame_errors); | |
688 | dpctl_print(dpctl_p, "\n"); | |
689 | ||
d14a36f4 | 690 | print_stat(dpctl_p, " TX packets:", s.tx_packets); |
fceef209 DDP |
691 | print_stat(dpctl_p, " errors:", s.tx_errors); |
692 | print_stat(dpctl_p, " dropped:", s.tx_dropped); | |
693 | print_stat(dpctl_p, " aborted:", s.tx_aborted_errors); | |
694 | print_stat(dpctl_p, " carrier:", s.tx_carrier_errors); | |
695 | dpctl_print(dpctl_p, "\n"); | |
696 | ||
d14a36f4 | 697 | print_stat(dpctl_p, " collisions:", s.collisions); |
fceef209 DDP |
698 | dpctl_print(dpctl_p, "\n"); |
699 | ||
d14a36f4 | 700 | print_stat(dpctl_p, " RX bytes:", s.rx_bytes); |
fceef209 DDP |
701 | print_human_size(dpctl_p, s.rx_bytes); |
702 | print_stat(dpctl_p, " TX bytes:", s.tx_bytes); | |
703 | print_human_size(dpctl_p, s.tx_bytes); | |
704 | dpctl_print(dpctl_p, "\n"); | |
705 | } else { | |
706 | dpctl_print(dpctl_p, ", could not retrieve stats (%s)", | |
707 | ovs_strerror(error)); | |
708 | } | |
709 | } | |
3ff1613e | 710 | dpif_port_destroy(&dpif_port); |
fceef209 | 711 | } |
3ff1613e JP |
712 | |
713 | free(port_nos); | |
1f70f3f0 DDP |
714 | } |
715 | ||
716 | typedef void (*dps_for_each_cb)(struct dpif *, struct dpctl_params *); | |
717 | ||
718 | static int | |
719 | dps_for_each(struct dpctl_params *dpctl_p, dps_for_each_cb cb) | |
720 | { | |
721 | struct sset dpif_names = SSET_INITIALIZER(&dpif_names), | |
722 | dpif_types = SSET_INITIALIZER(&dpif_types); | |
88c98bf5 | 723 | int error, openerror = 0, enumerror = 0; |
1f70f3f0 | 724 | const char *type, *name; |
88c98bf5 | 725 | bool at_least_one = false; |
1f70f3f0 DDP |
726 | |
727 | dp_enumerate_types(&dpif_types); | |
728 | ||
729 | SSET_FOR_EACH (type, &dpif_types) { | |
730 | error = dp_enumerate_names(type, &dpif_names); | |
731 | if (error) { | |
88c98bf5 | 732 | enumerror = error; |
1f70f3f0 DDP |
733 | } |
734 | ||
735 | SSET_FOR_EACH (name, &dpif_names) { | |
736 | struct dpif *dpif; | |
737 | ||
88c98bf5 | 738 | at_least_one = true; |
1f70f3f0 DDP |
739 | error = dpif_open(name, type, &dpif); |
740 | if (!error) { | |
741 | cb(dpif, dpctl_p); | |
742 | dpif_close(dpif); | |
743 | } else { | |
88c98bf5 | 744 | openerror = error; |
1f70f3f0 DDP |
745 | dpctl_error(dpctl_p, error, "opening datapath %s failed", |
746 | name); | |
747 | } | |
748 | } | |
749 | } | |
750 | ||
751 | sset_destroy(&dpif_names); | |
752 | sset_destroy(&dpif_types); | |
753 | ||
88c98bf5 DDP |
754 | /* If there has been an error while opening a datapath it should be |
755 | * reported. Otherwise, we want to ignore the errors generated by | |
756 | * dp_enumerate_names() if at least one datapath has been discovered, | |
757 | * because they're not interesting for the user. This happens, for | |
758 | * example, if OVS is using a userspace datapath and the kernel module | |
759 | * is not loaded. */ | |
760 | if (openerror) { | |
761 | return openerror; | |
762 | } else { | |
763 | return at_least_one ? 0 : enumerror; | |
764 | } | |
fceef209 DDP |
765 | } |
766 | ||
767 | static int | |
768 | dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
769 | { | |
770 | int error, lasterror = 0; | |
771 | if (argc > 1) { | |
772 | int i; | |
773 | for (i = 1; i < argc; i++) { | |
774 | const char *name = argv[i]; | |
775 | struct dpif *dpif; | |
776 | ||
777 | error = parsed_dpif_open(name, false, &dpif); | |
778 | if (!error) { | |
779 | show_dpif(dpif, dpctl_p); | |
1f70f3f0 | 780 | dpif_close(dpif); |
fceef209 DDP |
781 | } else { |
782 | dpctl_error(dpctl_p, error, "opening datapath %s failed", | |
783 | name); | |
784 | lasterror = error; | |
785 | } | |
786 | } | |
787 | } else { | |
1f70f3f0 | 788 | lasterror = dps_for_each(dpctl_p, show_dpif); |
fceef209 | 789 | } |
1f70f3f0 | 790 | |
fceef209 DDP |
791 | return lasterror; |
792 | } | |
793 | ||
1f70f3f0 DDP |
794 | static void |
795 | dump_cb(struct dpif *dpif, struct dpctl_params *dpctl_p) | |
796 | { | |
797 | dpctl_print(dpctl_p, "%s\n", dpif_name(dpif)); | |
798 | } | |
799 | ||
fceef209 DDP |
800 | static int |
801 | dpctl_dump_dps(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, | |
802 | struct dpctl_params *dpctl_p) | |
803 | { | |
1f70f3f0 | 804 | return dps_for_each(dpctl_p, dump_cb); |
fceef209 DDP |
805 | } |
806 | ||
818650e6 JS |
807 | static void |
808 | format_dpif_flow(struct ds *ds, const struct dpif_flow *f, struct hmap *ports, | |
d63ca532 | 809 | struct dpctl_params *dpctl_p) |
818650e6 | 810 | { |
fbe794aa JS |
811 | if (dpctl_p->verbosity && f->ufid_present) { |
812 | odp_format_ufid(&f->ufid, ds); | |
813 | ds_put_cstr(ds, ", "); | |
818650e6 JS |
814 | } |
815 | odp_flow_format(f->key, f->key_len, f->mask, f->mask_len, ports, ds, | |
816 | dpctl_p->verbosity); | |
817 | ds_put_cstr(ds, ", "); | |
818 | ||
819 | dpif_flow_stats_format(&f->stats, ds); | |
d63ca532 | 820 | if (dpctl_p->verbosity && f->attrs.offloaded) { |
80944cb4 EB |
821 | if (f->attrs.dp_layer && !strcmp(f->attrs.dp_layer, "ovs")) { |
822 | ds_put_cstr(ds, ", offloaded:partial"); | |
823 | } else { | |
824 | ds_put_cstr(ds, ", offloaded:yes"); | |
825 | } | |
4742003c | 826 | } |
d63ca532 GT |
827 | if (dpctl_p->verbosity && f->attrs.dp_layer) { |
828 | ds_put_format(ds, ", dp:%s", f->attrs.dp_layer); | |
829 | } | |
818650e6 | 830 | ds_put_cstr(ds, ", actions:"); |
0722f341 | 831 | format_odp_actions(ds, f->actions, f->actions_len, ports); |
0e8f5c6a EF |
832 | if (dpctl_p->verbosity && f->attrs.dp_extra_info) { |
833 | ds_put_format(ds, ", dp-extra-info:%s", f->attrs.dp_extra_info); | |
834 | } | |
818650e6 JS |
835 | } |
836 | ||
a692410a GT |
837 | struct dump_types { |
838 | bool ovs; | |
839 | bool tc; | |
80944cb4 | 840 | bool dpdk; |
a692410a GT |
841 | bool offloaded; |
842 | bool non_offloaded; | |
80944cb4 | 843 | bool partially_offloaded; |
7e8b7199 PB |
844 | }; |
845 | ||
a692410a GT |
846 | static void |
847 | enable_all_dump_types(struct dump_types *dump_types) | |
848 | { | |
849 | dump_types->ovs = true; | |
850 | dump_types->tc = true; | |
80944cb4 | 851 | dump_types->dpdk = true; |
a692410a GT |
852 | dump_types->offloaded = true; |
853 | dump_types->non_offloaded = true; | |
80944cb4 | 854 | dump_types->partially_offloaded = true; |
a692410a GT |
855 | } |
856 | ||
857 | static int | |
858 | populate_dump_types(char *types_list, struct dump_types *dump_types, | |
859 | struct dpctl_params *dpctl_p) | |
860 | { | |
861 | if (!types_list) { | |
862 | enable_all_dump_types(dump_types); | |
863 | return 0; | |
864 | } | |
865 | ||
866 | char *current_type; | |
867 | ||
868 | while (types_list && types_list[0] != '\0') { | |
869 | current_type = types_list; | |
870 | size_t type_len = strcspn(current_type, ","); | |
871 | ||
872 | types_list += type_len + (types_list[type_len] != '\0'); | |
873 | current_type[type_len] = '\0'; | |
874 | ||
875 | if (!strcmp(current_type, "ovs")) { | |
876 | dump_types->ovs = true; | |
877 | } else if (!strcmp(current_type, "tc")) { | |
878 | dump_types->tc = true; | |
80944cb4 EB |
879 | } else if (!strcmp(current_type, "dpdk")) { |
880 | dump_types->dpdk = true; | |
a692410a GT |
881 | } else if (!strcmp(current_type, "offloaded")) { |
882 | dump_types->offloaded = true; | |
883 | } else if (!strcmp(current_type, "non-offloaded")) { | |
884 | dump_types->non_offloaded = true; | |
80944cb4 EB |
885 | } else if (!strcmp(current_type, "partially-offloaded")) { |
886 | dump_types->partially_offloaded = true; | |
a692410a GT |
887 | } else if (!strcmp(current_type, "all")) { |
888 | enable_all_dump_types(dump_types); | |
889 | } else { | |
890 | dpctl_error(dpctl_p, EINVAL, "Failed to parse type (%s)", | |
891 | current_type); | |
892 | return EINVAL; | |
893 | } | |
894 | } | |
895 | return 0; | |
896 | } | |
897 | ||
898 | static void | |
899 | determine_dpif_flow_dump_types(struct dump_types *dump_types, | |
900 | struct dpif_flow_dump_types *dpif_dump_types) | |
901 | { | |
902 | dpif_dump_types->ovs_flows = dump_types->ovs || dump_types->non_offloaded; | |
903 | dpif_dump_types->netdev_flows = dump_types->tc || dump_types->offloaded | |
80944cb4 EB |
904 | || dump_types->non_offloaded |
905 | || dump_types->dpdk | |
906 | || dump_types->partially_offloaded; | |
a692410a GT |
907 | } |
908 | ||
d63ca532 | 909 | static bool |
a692410a GT |
910 | flow_passes_type_filter(const struct dpif_flow *f, |
911 | struct dump_types *dump_types) | |
d63ca532 | 912 | { |
a692410a GT |
913 | if (dump_types->ovs && !strcmp(f->attrs.dp_layer, "ovs")) { |
914 | return true; | |
915 | } | |
916 | if (dump_types->tc && !strcmp(f->attrs.dp_layer, "tc")) { | |
917 | return true; | |
918 | } | |
80944cb4 EB |
919 | if (dump_types->dpdk && !strcmp(f->attrs.dp_layer, "dpdk")) { |
920 | return true; | |
921 | } | |
922 | if (dump_types->offloaded && f->attrs.offloaded && | |
923 | strcmp(f->attrs.dp_layer, "ovs")) { | |
924 | return true; | |
925 | } | |
926 | if (dump_types->partially_offloaded && f->attrs.offloaded && | |
927 | !strcmp(f->attrs.dp_layer, "ovs")) { | |
a692410a GT |
928 | return true; |
929 | } | |
930 | if (dump_types->non_offloaded && !(f->attrs.offloaded)) { | |
931 | return true; | |
d63ca532 | 932 | } |
a692410a | 933 | return false; |
d63ca532 GT |
934 | } |
935 | ||
d1fd1ea9 BP |
936 | static struct hmap * |
937 | dpctl_get_portno_names(struct dpif *dpif, const struct dpctl_params *dpctl_p) | |
938 | { | |
939 | if (dpctl_p->names) { | |
940 | struct hmap *portno_names = xmalloc(sizeof *portno_names); | |
941 | hmap_init(portno_names); | |
942 | ||
943 | struct dpif_port_dump port_dump; | |
944 | struct dpif_port dpif_port; | |
945 | DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) { | |
946 | odp_portno_names_set(portno_names, dpif_port.port_no, | |
947 | dpif_port.name); | |
948 | } | |
949 | ||
950 | return portno_names; | |
951 | } else { | |
952 | return NULL; | |
953 | } | |
954 | } | |
955 | ||
956 | static void | |
957 | dpctl_free_portno_names(struct hmap *portno_names) | |
958 | { | |
959 | if (portno_names) { | |
960 | odp_portno_names_destroy(portno_names); | |
961 | hmap_destroy(portno_names); | |
962 | free(portno_names); | |
963 | } | |
964 | } | |
965 | ||
fceef209 DDP |
966 | static int |
967 | dpctl_dump_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
968 | { | |
969 | struct dpif *dpif; | |
970 | struct ds ds; | |
fceef209 DDP |
971 | |
972 | char *filter = NULL; | |
973 | struct flow flow_filter; | |
974 | struct flow_wildcards wc_filter; | |
a692410a GT |
975 | char *types_list = NULL; |
976 | struct dump_types dump_types; | |
977 | struct dpif_flow_dump_types dpif_dump_types; | |
fceef209 | 978 | |
fceef209 DDP |
979 | struct dpif_flow_dump_thread *flow_dump_thread; |
980 | struct dpif_flow_dump *flow_dump; | |
981 | struct dpif_flow f; | |
1c1e46ed | 982 | int pmd_id = PMD_ID_NULL; |
7e8b7199 | 983 | int lastargc = 0; |
fceef209 DDP |
984 | int error; |
985 | ||
7e8b7199 PB |
986 | while (argc > 1 && lastargc != argc) { |
987 | lastargc = argc; | |
988 | if (!strncmp(argv[argc - 1], "filter=", 7) && !filter) { | |
989 | filter = xstrdup(argv[--argc] + 7); | |
a692410a | 990 | } else if (!strncmp(argv[argc - 1], "type=", 5) && !types_list) { |
d74ca226 IM |
991 | if (!dpctl_p->is_appctl) { |
992 | dpctl_error(dpctl_p, 0, | |
993 | "Invalid argument 'type'. " | |
994 | "Use 'ovs-appctl dpctl/dump-flows' instead."); | |
995 | error = EINVAL; | |
996 | goto out_free; | |
997 | } | |
a692410a | 998 | types_list = xstrdup(argv[--argc] + 5); |
7e8b7199 | 999 | } |
fceef209 | 1000 | } |
7e8b7199 | 1001 | |
01dcd687 | 1002 | error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
fceef209 | 1003 | if (error) { |
7e8b7199 | 1004 | goto out_free; |
fceef209 DDP |
1005 | } |
1006 | ||
d1fd1ea9 | 1007 | struct hmap *portno_names = dpctl_get_portno_names(dpif, dpctl_p); |
fceef209 DDP |
1008 | |
1009 | if (filter) { | |
50f96b10 BP |
1010 | struct ofputil_port_map port_map; |
1011 | ofputil_port_map_init(&port_map); | |
d1fd1ea9 BP |
1012 | |
1013 | struct dpif_port_dump port_dump; | |
1014 | struct dpif_port dpif_port; | |
50f96b10 BP |
1015 | DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) { |
1016 | ofputil_port_map_put(&port_map, | |
1017 | u16_to_ofp(odp_to_u32(dpif_port.port_no)), | |
1018 | dpif_port.name); | |
1019 | } | |
1020 | char *err = parse_ofp_exact_flow(&flow_filter, &wc_filter, NULL, | |
1021 | filter, &port_map); | |
1022 | ofputil_port_map_destroy(&port_map); | |
fceef209 DDP |
1023 | if (err) { |
1024 | dpctl_error(dpctl_p, 0, "Failed to parse filter (%s)", err); | |
a129fe8c | 1025 | free(err); |
fceef209 DDP |
1026 | error = EINVAL; |
1027 | goto out_dpifclose; | |
1028 | } | |
1029 | } | |
1030 | ||
a692410a GT |
1031 | memset(&dump_types, 0, sizeof dump_types); |
1032 | error = populate_dump_types(types_list, &dump_types, dpctl_p); | |
1033 | if (error) { | |
1034 | goto out_free; | |
7e8b7199 | 1035 | } |
a692410a | 1036 | determine_dpif_flow_dump_types(&dump_types, &dpif_dump_types); |
7e8b7199 | 1037 | |
d5c199ea DDP |
1038 | /* Make sure that these values are different. PMD_ID_NULL means that the |
1039 | * pmd is unspecified (e.g. because the datapath doesn't have different | |
1040 | * pmd threads), while NON_PMD_CORE_ID refers to every non pmd threads | |
1041 | * in the userspace datapath */ | |
1042 | BUILD_ASSERT(PMD_ID_NULL != NON_PMD_CORE_ID); | |
1043 | ||
fceef209 | 1044 | ds_init(&ds); |
4742003c | 1045 | memset(&f, 0, sizeof f); |
a692410a | 1046 | flow_dump = dpif_flow_dump_create(dpif, false, &dpif_dump_types); |
fceef209 DDP |
1047 | flow_dump_thread = dpif_flow_dump_thread_create(flow_dump); |
1048 | while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) { | |
1049 | if (filter) { | |
1050 | struct flow flow; | |
1051 | struct flow_wildcards wc; | |
1052 | struct match match, match_filter; | |
1053 | struct minimatch minimatch; | |
1054 | ||
d40533fc BP |
1055 | odp_flow_key_to_flow(f.key, f.key_len, &flow, NULL); |
1056 | odp_flow_key_to_mask(f.mask, f.mask_len, &wc, &flow, NULL); | |
fceef209 DDP |
1057 | match_init(&match, &flow, &wc); |
1058 | ||
1059 | match_init(&match_filter, &flow_filter, &wc); | |
1060 | match_init(&match_filter, &match_filter.flow, &wc_filter); | |
1061 | minimatch_init(&minimatch, &match_filter); | |
1062 | ||
1063 | if (!minimatch_matches_flow(&minimatch, &match.flow)) { | |
1064 | minimatch_destroy(&minimatch); | |
1065 | continue; | |
1066 | } | |
1067 | minimatch_destroy(&minimatch); | |
1068 | } | |
1069 | ds_clear(&ds); | |
1c1e46ed AW |
1070 | /* If 'pmd_id' is specified, overlapping flows could be dumped from |
1071 | * different pmd threads. So, separates dumps from different pmds | |
1072 | * by printing a title line. */ | |
1073 | if (pmd_id != f.pmd_id) { | |
1074 | if (f.pmd_id == NON_PMD_CORE_ID) { | |
138d30a9 | 1075 | ds_put_format(&ds, "flow-dump from the main thread:\n"); |
1c1e46ed AW |
1076 | } else { |
1077 | ds_put_format(&ds, "flow-dump from pmd on cpu core: %d\n", | |
1078 | f.pmd_id); | |
1079 | } | |
1080 | pmd_id = f.pmd_id; | |
1081 | } | |
a692410a | 1082 | if (flow_passes_type_filter(&f, &dump_types)) { |
d63ca532 GT |
1083 | format_dpif_flow(&ds, &f, portno_names, dpctl_p); |
1084 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds)); | |
1085 | } | |
fceef209 DDP |
1086 | } |
1087 | dpif_flow_dump_thread_destroy(flow_dump_thread); | |
1088 | error = dpif_flow_dump_destroy(flow_dump); | |
1089 | ||
1090 | if (error) { | |
1091 | dpctl_error(dpctl_p, error, "Failed to dump flows from datapath"); | |
1092 | } | |
1093 | ds_destroy(&ds); | |
1094 | ||
1095 | out_dpifclose: | |
d1fd1ea9 | 1096 | dpctl_free_portno_names(portno_names); |
fceef209 | 1097 | dpif_close(dpif); |
7e8b7199 | 1098 | out_free: |
fceef209 | 1099 | free(filter); |
a692410a | 1100 | free(types_list); |
fceef209 DDP |
1101 | return error; |
1102 | } | |
1103 | ||
1104 | static int | |
1105 | dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags, | |
1106 | struct dpctl_params *dpctl_p) | |
1107 | { | |
1108 | const char *key_s = argv[argc - 2]; | |
1109 | const char *actions_s = argv[argc - 1]; | |
1110 | struct dpif_flow_stats stats; | |
1111 | struct dpif_port dpif_port; | |
1112 | struct dpif_port_dump port_dump; | |
1113 | struct ofpbuf actions; | |
1114 | struct ofpbuf key; | |
1115 | struct ofpbuf mask; | |
1116 | struct dpif *dpif; | |
534a19b9 JS |
1117 | ovs_u128 ufid; |
1118 | bool ufid_present; | |
fceef209 | 1119 | struct simap port_names; |
534a19b9 | 1120 | int n, error; |
fceef209 | 1121 | |
01dcd687 | 1122 | error = opt_dpif_open(argc, argv, dpctl_p, 4, &dpif); |
fceef209 | 1123 | if (error) { |
fceef209 DDP |
1124 | return error; |
1125 | } | |
1126 | ||
534a19b9 JS |
1127 | ufid_present = false; |
1128 | n = odp_ufid_from_string(key_s, &ufid); | |
1129 | if (n < 0) { | |
1130 | dpctl_error(dpctl_p, -n, "parsing flow ufid"); | |
1131 | return -n; | |
1132 | } else if (n) { | |
1133 | key_s += n; | |
1134 | ufid_present = true; | |
1135 | } | |
fceef209 DDP |
1136 | |
1137 | simap_init(&port_names); | |
1138 | DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) { | |
1139 | simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no)); | |
1140 | } | |
1141 | ||
1142 | ofpbuf_init(&key, 0); | |
1143 | ofpbuf_init(&mask, 0); | |
d40533fc BP |
1144 | char *error_s; |
1145 | error = odp_flow_from_string(key_s, &port_names, &key, &mask, &error_s); | |
fceef209 DDP |
1146 | simap_destroy(&port_names); |
1147 | if (error) { | |
d40533fc BP |
1148 | dpctl_error(dpctl_p, error, "parsing flow key (%s)", error_s); |
1149 | free(error_s); | |
fceef209 DDP |
1150 | goto out_freekeymask; |
1151 | } | |
1152 | ||
1153 | ofpbuf_init(&actions, 0); | |
1154 | error = odp_actions_from_string(actions_s, NULL, &actions); | |
1155 | if (error) { | |
1156 | dpctl_error(dpctl_p, error, "parsing actions"); | |
1157 | goto out_freeactions; | |
1158 | } | |
1c1e46ed | 1159 | |
f5d317a1 DDP |
1160 | /* The flow will be added on all pmds currently in the datapath. */ |
1161 | error = dpif_flow_put(dpif, flags, | |
1162 | key.data, key.size, | |
1163 | mask.size == 0 ? NULL : mask.data, | |
1164 | mask.size, actions.data, | |
1165 | actions.size, ufid_present ? &ufid : NULL, | |
1166 | PMD_ID_NULL, | |
1167 | dpctl_p->print_statistics ? &stats : NULL); | |
1168 | ||
fceef209 DDP |
1169 | if (error) { |
1170 | dpctl_error(dpctl_p, error, "updating flow table"); | |
1171 | goto out_freeactions; | |
1172 | } | |
1173 | ||
1174 | if (dpctl_p->print_statistics) { | |
1175 | struct ds s; | |
1176 | ||
1177 | ds_init(&s); | |
1178 | dpif_flow_stats_format(&stats, &s); | |
1179 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); | |
1180 | ds_destroy(&s); | |
1181 | } | |
1182 | ||
1183 | out_freeactions: | |
1184 | ofpbuf_uninit(&actions); | |
1185 | out_freekeymask: | |
1186 | ofpbuf_uninit(&mask); | |
1187 | ofpbuf_uninit(&key); | |
1188 | dpif_close(dpif); | |
1189 | return error; | |
1190 | } | |
1191 | ||
1192 | static int | |
1193 | dpctl_add_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
1194 | { | |
1195 | return dpctl_put_flow(argc, argv, DPIF_FP_CREATE, dpctl_p); | |
1196 | } | |
1197 | ||
1198 | static int | |
1199 | dpctl_mod_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
1200 | { | |
1201 | enum dpif_flow_put_flags flags; | |
1202 | ||
1203 | flags = DPIF_FP_MODIFY; | |
1204 | if (dpctl_p->may_create) { | |
1205 | flags |= DPIF_FP_CREATE; | |
1206 | } | |
1207 | if (dpctl_p->zero_statistics) { | |
1208 | flags |= DPIF_FP_ZERO_STATS; | |
1209 | } | |
1210 | ||
1211 | return dpctl_put_flow(argc, argv, flags, dpctl_p); | |
1212 | } | |
1213 | ||
818650e6 JS |
1214 | static int |
1215 | dpctl_get_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
1216 | { | |
1217 | const char *key_s = argv[argc - 1]; | |
1218 | struct dpif_flow flow; | |
818650e6 | 1219 | struct dpif *dpif; |
818650e6 JS |
1220 | ovs_u128 ufid; |
1221 | struct ofpbuf buf; | |
1222 | uint64_t stub[DPIF_FLOW_BUFSIZE / 8]; | |
1223 | struct ds ds; | |
1224 | int n, error; | |
1225 | ||
01dcd687 | 1226 | error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); |
818650e6 | 1227 | if (error) { |
818650e6 JS |
1228 | return error; |
1229 | } | |
1230 | ||
1231 | ofpbuf_use_stub(&buf, &stub, sizeof stub); | |
d1fd1ea9 BP |
1232 | |
1233 | struct hmap *portno_names = dpctl_get_portno_names(dpif, dpctl_p); | |
818650e6 JS |
1234 | |
1235 | n = odp_ufid_from_string(key_s, &ufid); | |
1236 | if (n <= 0) { | |
1237 | dpctl_error(dpctl_p, -n, "parsing flow ufid"); | |
1238 | goto out; | |
1239 | } | |
1240 | ||
c673049c | 1241 | /* In case of PMD will be returned flow from first PMD thread with match. */ |
1c1e46ed | 1242 | error = dpif_flow_get(dpif, NULL, 0, &ufid, PMD_ID_NULL, &buf, &flow); |
818650e6 JS |
1243 | if (error) { |
1244 | dpctl_error(dpctl_p, error, "getting flow"); | |
1245 | goto out; | |
1246 | } | |
1247 | ||
1248 | ds_init(&ds); | |
d63ca532 | 1249 | format_dpif_flow(&ds, &flow, portno_names, dpctl_p); |
818650e6 JS |
1250 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds)); |
1251 | ds_destroy(&ds); | |
1252 | ||
1253 | out: | |
d1fd1ea9 | 1254 | dpctl_free_portno_names(portno_names); |
818650e6 JS |
1255 | ofpbuf_uninit(&buf); |
1256 | dpif_close(dpif); | |
1257 | return error; | |
1258 | } | |
1259 | ||
fceef209 DDP |
1260 | static int |
1261 | dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
1262 | { | |
1263 | const char *key_s = argv[argc - 1]; | |
1264 | struct dpif_flow_stats stats; | |
1265 | struct dpif_port dpif_port; | |
1266 | struct dpif_port_dump port_dump; | |
1267 | struct ofpbuf key; | |
1268 | struct ofpbuf mask; /* To be ignored. */ | |
1269 | struct dpif *dpif; | |
534a19b9 JS |
1270 | ovs_u128 ufid; |
1271 | bool ufid_present; | |
fceef209 | 1272 | struct simap port_names; |
534a19b9 | 1273 | int n, error; |
fceef209 | 1274 | |
01dcd687 | 1275 | error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); |
fceef209 | 1276 | if (error) { |
fceef209 DDP |
1277 | return error; |
1278 | } | |
1279 | ||
534a19b9 JS |
1280 | ufid_present = false; |
1281 | n = odp_ufid_from_string(key_s, &ufid); | |
1282 | if (n < 0) { | |
1283 | dpctl_error(dpctl_p, -n, "parsing flow ufid"); | |
1284 | return -n; | |
1285 | } else if (n) { | |
1286 | key_s += n; | |
1287 | ufid_present = true; | |
1288 | } | |
1289 | ||
fceef209 DDP |
1290 | simap_init(&port_names); |
1291 | DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) { | |
1292 | simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no)); | |
1293 | } | |
1294 | ||
1295 | ofpbuf_init(&key, 0); | |
1296 | ofpbuf_init(&mask, 0); | |
1297 | ||
d40533fc BP |
1298 | char *error_s; |
1299 | error = odp_flow_from_string(key_s, &port_names, &key, &mask, &error_s); | |
fceef209 | 1300 | if (error) { |
d40533fc BP |
1301 | dpctl_error(dpctl_p, error, "%s", error_s); |
1302 | free(error_s); | |
fceef209 DDP |
1303 | goto out; |
1304 | } | |
1305 | ||
f5d317a1 DDP |
1306 | /* The flow will be deleted from all pmds currently in the datapath. */ |
1307 | error = dpif_flow_del(dpif, key.data, key.size, | |
1308 | ufid_present ? &ufid : NULL, PMD_ID_NULL, | |
1309 | dpctl_p->print_statistics ? &stats : NULL); | |
1310 | ||
fceef209 DDP |
1311 | if (error) { |
1312 | dpctl_error(dpctl_p, error, "deleting flow"); | |
534a19b9 JS |
1313 | if (error == ENOENT && !ufid_present) { |
1314 | struct ds s; | |
1315 | ||
1316 | ds_init(&s); | |
1317 | ds_put_format(&s, "Perhaps you need to specify a UFID?"); | |
1318 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); | |
1319 | ds_destroy(&s); | |
1320 | } | |
fceef209 DDP |
1321 | goto out; |
1322 | } | |
1323 | ||
1324 | if (dpctl_p->print_statistics) { | |
1325 | struct ds s; | |
1326 | ||
1327 | ds_init(&s); | |
1328 | dpif_flow_stats_format(&stats, &s); | |
1329 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); | |
1330 | ds_destroy(&s); | |
1331 | } | |
1332 | ||
1333 | out: | |
1334 | ofpbuf_uninit(&mask); | |
1335 | ofpbuf_uninit(&key); | |
1336 | simap_destroy(&port_names); | |
1337 | dpif_close(dpif); | |
1338 | return error; | |
1339 | } | |
1340 | ||
1341 | static int | |
1342 | dpctl_del_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
1343 | { | |
1344 | struct dpif *dpif; | |
fceef209 | 1345 | |
01dcd687 | 1346 | int error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
fceef209 | 1347 | if (error) { |
fceef209 DDP |
1348 | return error; |
1349 | } | |
1350 | ||
1351 | error = dpif_flow_flush(dpif); | |
1352 | if (error) { | |
1353 | dpctl_error(dpctl_p, error, "deleting all flows"); | |
1354 | } | |
1355 | dpif_close(dpif); | |
1356 | return error; | |
1357 | } | |
1358 | ||
1359 | static int | |
1360 | dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, | |
1361 | struct dpctl_params *dpctl_p) | |
1362 | { | |
1363 | if (dpctl_p->usage) { | |
1364 | dpctl_p->usage(dpctl_p->aux); | |
1365 | } | |
56cad666 AW |
1366 | |
1367 | return 0; | |
1368 | } | |
1369 | ||
1370 | static int | |
1371 | dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, | |
1372 | struct dpctl_params *dpctl_p) | |
1373 | { | |
1374 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1375 | const struct dpctl_command *commands = get_all_dpctl_commands(); | |
1376 | ||
1377 | ds_put_cstr(&ds, "The available commands are:\n"); | |
1378 | for (; commands->name; commands++) { | |
1379 | const struct dpctl_command *c = commands; | |
1380 | ||
2fc160f5 IM |
1381 | if (dpctl_p->is_appctl && !strcmp(c->name, "help")) { |
1382 | continue; | |
1383 | } | |
1384 | ||
56cad666 AW |
1385 | ds_put_format(&ds, " %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "", |
1386 | c->name, c->usage); | |
1387 | } | |
1388 | dpctl_puts(dpctl_p, false, ds.string); | |
1389 | ds_destroy(&ds); | |
1390 | ||
fceef209 DDP |
1391 | return 0; |
1392 | } | |
f33cdd7b | 1393 | \f |
dffae65f DDP |
1394 | |
1395 | static int | |
1396 | dpctl_dump_conntrack(int argc, const char *argv[], | |
1397 | struct dpctl_params *dpctl_p) | |
1398 | { | |
1399 | struct ct_dpif_dump_state *dump; | |
1400 | struct ct_dpif_entry cte; | |
1401 | uint16_t zone, *pzone = NULL; | |
ded30c74 | 1402 | int tot_bkts; |
dffae65f | 1403 | struct dpif *dpif; |
dffae65f DDP |
1404 | int error; |
1405 | ||
1406 | if (argc > 1 && ovs_scan(argv[argc - 1], "zone=%"SCNu16, &zone)) { | |
1407 | pzone = &zone; | |
1408 | argc--; | |
1409 | } | |
f33cdd7b | 1410 | |
01dcd687 | 1411 | error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
dffae65f | 1412 | if (error) { |
dffae65f DDP |
1413 | return error; |
1414 | } | |
1415 | ||
ded30c74 | 1416 | error = ct_dpif_dump_start(dpif, &dump, pzone, &tot_bkts); |
dffae65f DDP |
1417 | if (error) { |
1418 | dpctl_error(dpctl_p, error, "starting conntrack dump"); | |
1419 | dpif_close(dpif); | |
1420 | return error; | |
1421 | } | |
1422 | ||
97ee6d41 | 1423 | while (!(error = ct_dpif_dump_next(dump, &cte))) { |
dffae65f DDP |
1424 | struct ds s = DS_EMPTY_INITIALIZER; |
1425 | ||
1426 | ct_dpif_format_entry(&cte, &s, dpctl_p->verbosity, | |
1427 | dpctl_p->print_statistics); | |
1428 | ct_dpif_entry_uninit(&cte); | |
1429 | ||
1430 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); | |
1431 | ds_destroy(&s); | |
1432 | } | |
97ee6d41 FA |
1433 | if (error == EOF) { |
1434 | /* Any CT entry was dumped with no issue. */ | |
1435 | error = 0; | |
1436 | } else if (error) { | |
1437 | dpctl_error(dpctl_p, error, "dumping conntrack entry"); | |
1438 | } | |
1439 | ||
dffae65f DDP |
1440 | ct_dpif_dump_done(dump); |
1441 | dpif_close(dpif); | |
1442 | return error; | |
1443 | } | |
1444 | ||
7f278d1f DDP |
1445 | static int |
1446 | dpctl_flush_conntrack(int argc, const char *argv[], | |
1447 | struct dpctl_params *dpctl_p) | |
1448 | { | |
2f15f8f3 | 1449 | struct dpif *dpif = NULL; |
c43a1331 YHW |
1450 | struct ct_dpif_tuple tuple, *ptuple = NULL; |
1451 | struct ds ds = DS_EMPTY_INITIALIZER; | |
7f278d1f | 1452 | uint16_t zone, *pzone = NULL; |
2f15f8f3 DB |
1453 | int error; |
1454 | int args = argc - 1; | |
1455 | ||
1456 | /* Parse ct tuple */ | |
1457 | if (args && ct_dpif_parse_tuple(&tuple, argv[args], &ds)) { | |
1458 | ptuple = &tuple; | |
1459 | args--; | |
c43a1331 | 1460 | } |
7f278d1f | 1461 | |
c43a1331 | 1462 | /* Parse zone */ |
2f15f8f3 | 1463 | if (args && ovs_scan(argv[args], "zone=%"SCNu16, &zone)) { |
7f278d1f | 1464 | pzone = &zone; |
2f15f8f3 | 1465 | args--; |
7f278d1f | 1466 | } |
2f15f8f3 | 1467 | |
c43a1331 | 1468 | /* Report error if there are more than one unparsed argument. */ |
2f15f8f3 DB |
1469 | if (args > 1) { |
1470 | ds_put_cstr(&ds, "invalid arguments"); | |
c43a1331 YHW |
1471 | error = EINVAL; |
1472 | goto error; | |
7f278d1f | 1473 | } |
c43a1331 | 1474 | |
01dcd687 | 1475 | error = opt_dpif_open(argc, argv, dpctl_p, 4, &dpif); |
2f15f8f3 DB |
1476 | if (error) { |
1477 | return error; | |
7f278d1f DDP |
1478 | } |
1479 | ||
c43a1331 YHW |
1480 | error = ct_dpif_flush(dpif, pzone, ptuple); |
1481 | if (!error) { | |
1482 | dpif_close(dpif); | |
1483 | return 0; | |
1484 | } else { | |
1485 | ds_put_cstr(&ds, "failed to flush conntrack"); | |
1486 | } | |
7f278d1f | 1487 | |
c43a1331 YHW |
1488 | error: |
1489 | dpctl_error(dpctl_p, error, "%s", ds_cstr(&ds)); | |
1490 | ds_destroy(&ds); | |
7f278d1f DDP |
1491 | dpif_close(dpif); |
1492 | return error; | |
1493 | } | |
8a0d9d85 FA |
1494 | |
1495 | static int | |
1496 | dpctl_ct_stats_show(int argc, const char *argv[], | |
1497 | struct dpctl_params *dpctl_p) | |
1498 | { | |
1499 | struct dpif *dpif; | |
8a0d9d85 FA |
1500 | struct ct_dpif_dump_state *dump; |
1501 | struct ct_dpif_entry cte; | |
1502 | uint16_t zone, *pzone = NULL; | |
ded30c74 | 1503 | int tot_bkts; |
8a0d9d85 FA |
1504 | int lastargc = 0; |
1505 | ||
1506 | int proto_stats[CT_STATS_MAX]; | |
1507 | int tcp_conn_per_states[CT_DPIF_TCPS_MAX_NUM]; | |
1508 | int error; | |
1509 | ||
16b361ef JP |
1510 | bool verbose = dpctl_p->verbosity; |
1511 | ||
8a0d9d85 FA |
1512 | while (argc > 1 && lastargc != argc) { |
1513 | lastargc = argc; | |
1514 | if (!strncmp(argv[argc - 1], "verbose", 7)) { | |
16b361ef | 1515 | /* Support "verbose" argument for backwards compatibility. */ |
8a0d9d85 FA |
1516 | verbose = true; |
1517 | argc--; | |
1518 | } else if (!strncmp(argv[argc - 1], "zone=", 5)) { | |
1519 | if (ovs_scan(argv[argc - 1], "zone=%"SCNu16, &zone)) { | |
1520 | pzone = &zone; | |
1521 | argc--; | |
1522 | } | |
1523 | } | |
1524 | } | |
8a0d9d85 | 1525 | |
01dcd687 | 1526 | error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
8a0d9d85 | 1527 | if (error) { |
8a0d9d85 FA |
1528 | return error; |
1529 | } | |
1530 | ||
1531 | memset(proto_stats, 0, sizeof(proto_stats)); | |
1532 | memset(tcp_conn_per_states, 0, sizeof(tcp_conn_per_states)); | |
ded30c74 | 1533 | error = ct_dpif_dump_start(dpif, &dump, pzone, &tot_bkts); |
8a0d9d85 FA |
1534 | if (error) { |
1535 | dpctl_error(dpctl_p, error, "starting conntrack dump"); | |
1536 | dpif_close(dpif); | |
1537 | return error; | |
1538 | } | |
1539 | ||
1540 | int tot_conn = 0; | |
97ee6d41 | 1541 | while (!(error = ct_dpif_dump_next(dump, &cte))) { |
8a0d9d85 FA |
1542 | ct_dpif_entry_uninit(&cte); |
1543 | tot_conn++; | |
1544 | switch (cte.tuple_orig.ip_proto) { | |
1545 | case IPPROTO_ICMP: | |
1546 | proto_stats[CT_STATS_ICMP]++; | |
1547 | break; | |
1548 | case IPPROTO_ICMPV6: | |
1549 | proto_stats[CT_STATS_ICMPV6]++; | |
1550 | break; | |
1551 | case IPPROTO_TCP: | |
1552 | proto_stats[CT_STATS_TCP]++; | |
1553 | uint8_t tcp_state; | |
1554 | /* We keep two separate tcp states, but we print just one. The | |
1555 | * Linux kernel connection tracker internally keeps only one state, | |
1556 | * so 'state_orig' and 'state_reply', will be the same. */ | |
1557 | tcp_state = MAX(cte.protoinfo.tcp.state_orig, | |
1558 | cte.protoinfo.tcp.state_reply); | |
1559 | tcp_state = ct_dpif_coalesce_tcp_state(tcp_state); | |
1560 | tcp_conn_per_states[tcp_state]++; | |
1561 | break; | |
1562 | case IPPROTO_UDP: | |
1563 | proto_stats[CT_STATS_UDP]++; | |
1564 | break; | |
1565 | case IPPROTO_SCTP: | |
1566 | proto_stats[CT_STATS_SCTP]++; | |
1567 | break; | |
1568 | case IPPROTO_UDPLITE: | |
1569 | proto_stats[CT_STATS_UDPLITE]++; | |
1570 | break; | |
1571 | case IPPROTO_DCCP: | |
1572 | proto_stats[CT_STATS_DCCP]++; | |
1573 | break; | |
1574 | case IPPROTO_IGMP: | |
1575 | proto_stats[CT_STATS_IGMP]++; | |
1576 | break; | |
1577 | default: | |
1578 | proto_stats[CT_STATS_OTHER]++; | |
1579 | break; | |
1580 | } | |
1581 | } | |
97ee6d41 FA |
1582 | if (error == EOF) { |
1583 | /* All CT entries were dumped with no issue. */ | |
1584 | error = 0; | |
1585 | } else if (error) { | |
1586 | dpctl_error(dpctl_p, error, "dumping conntrack entry"); | |
1587 | /* Fall through to show any other info we collected. */ | |
1588 | } | |
8a0d9d85 FA |
1589 | |
1590 | dpctl_print(dpctl_p, "Connections Stats:\n Total: %d\n", tot_conn); | |
1591 | if (proto_stats[CT_STATS_TCP]) { | |
d14a36f4 | 1592 | dpctl_print(dpctl_p, " TCP: %d\n", proto_stats[CT_STATS_TCP]); |
8a0d9d85 | 1593 | if (verbose) { |
d14a36f4 | 1594 | dpctl_print(dpctl_p, " Conn per TCP states:\n"); |
8a0d9d85 FA |
1595 | for (int i = 0; i < CT_DPIF_TCPS_MAX_NUM; i++) { |
1596 | if (tcp_conn_per_states[i]) { | |
1597 | struct ds s = DS_EMPTY_INITIALIZER; | |
1598 | ct_dpif_format_tcp_stat(&s, i, tcp_conn_per_states[i]); | |
1599 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); | |
1600 | ds_destroy(&s); | |
1601 | } | |
1602 | } | |
1603 | } | |
1604 | } | |
1605 | if (proto_stats[CT_STATS_UDP]) { | |
d14a36f4 | 1606 | dpctl_print(dpctl_p, " UDP: %d\n", proto_stats[CT_STATS_UDP]); |
8a0d9d85 FA |
1607 | } |
1608 | if (proto_stats[CT_STATS_UDPLITE]) { | |
d14a36f4 | 1609 | dpctl_print(dpctl_p, " UDPLITE: %d\n", proto_stats[CT_STATS_UDPLITE]); |
8a0d9d85 FA |
1610 | } |
1611 | if (proto_stats[CT_STATS_SCTP]) { | |
d14a36f4 | 1612 | dpctl_print(dpctl_p, " SCTP: %d\n", proto_stats[CT_STATS_SCTP]); |
8a0d9d85 FA |
1613 | } |
1614 | if (proto_stats[CT_STATS_ICMP]) { | |
d14a36f4 | 1615 | dpctl_print(dpctl_p, " ICMP: %d\n", proto_stats[CT_STATS_ICMP]); |
8a0d9d85 FA |
1616 | } |
1617 | if (proto_stats[CT_STATS_DCCP]) { | |
d14a36f4 | 1618 | dpctl_print(dpctl_p, " DCCP: %d\n", proto_stats[CT_STATS_DCCP]); |
8a0d9d85 FA |
1619 | } |
1620 | if (proto_stats[CT_STATS_IGMP]) { | |
d14a36f4 | 1621 | dpctl_print(dpctl_p, " IGMP: %d\n", proto_stats[CT_STATS_IGMP]); |
8a0d9d85 FA |
1622 | } |
1623 | if (proto_stats[CT_STATS_OTHER]) { | |
d14a36f4 | 1624 | dpctl_print(dpctl_p, " Other: %d\n", proto_stats[CT_STATS_OTHER]); |
8a0d9d85 FA |
1625 | } |
1626 | ||
1627 | ct_dpif_dump_done(dump); | |
1628 | dpif_close(dpif); | |
1629 | return error; | |
1630 | } | |
ded30c74 FA |
1631 | |
1632 | #define CT_BKTS_GT "gt=" | |
1633 | static int | |
1634 | dpctl_ct_bkts(int argc, const char *argv[], | |
1635 | struct dpctl_params *dpctl_p) | |
1636 | { | |
1637 | struct dpif *dpif; | |
ded30c74 FA |
1638 | struct ct_dpif_dump_state *dump; |
1639 | struct ct_dpif_entry cte; | |
1640 | uint16_t gt = 0; /* Threshold: display value when greater than gt. */ | |
1641 | uint16_t *pzone = NULL; | |
1642 | int tot_bkts = 0; | |
1643 | int error; | |
1644 | ||
1645 | if (argc > 1 && !strncmp(argv[argc - 1], CT_BKTS_GT, strlen(CT_BKTS_GT))) { | |
1646 | if (ovs_scan(argv[argc - 1], CT_BKTS_GT"%"SCNu16, >)) { | |
1647 | argc--; | |
1648 | } | |
1649 | } | |
1650 | ||
01dcd687 | 1651 | error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
ded30c74 | 1652 | if (error) { |
ded30c74 FA |
1653 | return error; |
1654 | } | |
1655 | ||
1656 | error = ct_dpif_dump_start(dpif, &dump, pzone, &tot_bkts); | |
1657 | if (error) { | |
1658 | dpctl_error(dpctl_p, error, "starting conntrack dump"); | |
1659 | dpif_close(dpif); | |
1660 | return error; | |
1661 | } | |
1662 | if (tot_bkts == -1) { | |
1663 | /* Command not available when called by kernel OvS. */ | |
1664 | dpctl_print(dpctl_p, | |
1665 | "Command is available for UserSpace ConnTracker only.\n"); | |
1666 | ct_dpif_dump_done(dump); | |
1667 | dpif_close(dpif); | |
1668 | return 0; | |
1669 | } | |
1670 | ||
1671 | dpctl_print(dpctl_p, "Total Buckets: %d\n", tot_bkts); | |
1672 | ||
1673 | int tot_conn = 0; | |
1674 | uint32_t *conn_per_bkts = xzalloc(tot_bkts * sizeof(uint32_t)); | |
1675 | ||
97ee6d41 | 1676 | while (!(error = ct_dpif_dump_next(dump, &cte))) { |
ded30c74 FA |
1677 | ct_dpif_entry_uninit(&cte); |
1678 | tot_conn++; | |
1679 | if (tot_bkts > 0) { | |
1680 | if (cte.bkt < tot_bkts) { | |
1681 | conn_per_bkts[cte.bkt]++; | |
1682 | } else { | |
1683 | dpctl_print(dpctl_p, "Bucket nr out of range: %d >= %d\n", | |
1684 | cte.bkt, tot_bkts); | |
1685 | } | |
1686 | } | |
1687 | } | |
97ee6d41 FA |
1688 | if (error == EOF) { |
1689 | /* All CT entries were dumped with no issue. */ | |
1690 | error = 0; | |
1691 | } else if (error) { | |
1692 | dpctl_error(dpctl_p, error, "dumping conntrack entry"); | |
1693 | /* Fall through and display all the collected info. */ | |
1694 | } | |
ded30c74 FA |
1695 | |
1696 | dpctl_print(dpctl_p, "Current Connections: %d\n", tot_conn); | |
1697 | dpctl_print(dpctl_p, "\n"); | |
1698 | if (tot_bkts && tot_conn) { | |
1699 | dpctl_print(dpctl_p, "+-----------+" | |
1700 | "-----------------------------------------+\n"); | |
1701 | dpctl_print(dpctl_p, "| Buckets |" | |
1702 | " Connections per Buckets |\n"); | |
1703 | dpctl_print(dpctl_p, "+-----------+" | |
1704 | "-----------------------------------------+"); | |
1705 | #define NUM_BKTS_DIPLAYED_PER_ROW 8 | |
1706 | for (int i = 0; i < tot_bkts; i++) { | |
1707 | if (i % NUM_BKTS_DIPLAYED_PER_ROW == 0) { | |
1708 | dpctl_print(dpctl_p, "\n %3d..%3d | ", | |
1709 | i, i + NUM_BKTS_DIPLAYED_PER_ROW - 1); | |
1710 | } | |
1711 | if (conn_per_bkts[i] > gt) { | |
1712 | dpctl_print(dpctl_p, "%5d", conn_per_bkts[i]); | |
1713 | } else { | |
1714 | dpctl_print(dpctl_p, "%5s", "."); | |
1715 | } | |
1716 | } | |
1717 | dpctl_print(dpctl_p, "\n\n"); | |
1718 | } | |
1719 | ||
1720 | ct_dpif_dump_done(dump); | |
1721 | dpif_close(dpif); | |
1722 | free(conn_per_bkts); | |
1723 | return error; | |
1724 | } | |
fceef209 | 1725 | \f |
c92339ad DB |
1726 | static int |
1727 | dpctl_ct_set_maxconns(int argc, const char *argv[], | |
1728 | struct dpctl_params *dpctl_p) | |
1729 | { | |
1730 | struct dpif *dpif; | |
01dcd687 | 1731 | int error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); |
c92339ad DB |
1732 | if (!error) { |
1733 | uint32_t maxconns; | |
1734 | if (ovs_scan(argv[argc - 1], "%"SCNu32, &maxconns)) { | |
1735 | error = ct_dpif_set_maxconns(dpif, maxconns); | |
1736 | ||
1737 | if (!error) { | |
1738 | dpctl_print(dpctl_p, "setting maxconns successful"); | |
1739 | } else { | |
1740 | dpctl_error(dpctl_p, error, "ct set maxconns failed"); | |
1741 | } | |
1742 | } else { | |
1743 | error = EINVAL; | |
1744 | dpctl_error(dpctl_p, error, "maxconns missing or malformed"); | |
1745 | } | |
1746 | dpif_close(dpif); | |
1747 | } | |
1748 | ||
1749 | return error; | |
1750 | } | |
1751 | ||
1752 | static int | |
1753 | dpctl_ct_get_maxconns(int argc, const char *argv[], | |
1754 | struct dpctl_params *dpctl_p) | |
1755 | { | |
1756 | struct dpif *dpif; | |
01dcd687 | 1757 | int error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
c92339ad DB |
1758 | if (!error) { |
1759 | uint32_t maxconns; | |
1760 | error = ct_dpif_get_maxconns(dpif, &maxconns); | |
1761 | ||
1762 | if (!error) { | |
1763 | dpctl_print(dpctl_p, "%u\n", maxconns); | |
1764 | } else { | |
1765 | dpctl_error(dpctl_p, error, "maxconns could not be retrieved"); | |
1766 | } | |
1767 | dpif_close(dpif); | |
1768 | } | |
1769 | ||
1770 | return error; | |
1771 | } | |
1772 | ||
875075b3 DB |
1773 | static int |
1774 | dpctl_ct_get_nconns(int argc, const char *argv[], | |
1775 | struct dpctl_params *dpctl_p) | |
1776 | { | |
1777 | struct dpif *dpif; | |
01dcd687 | 1778 | int error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); |
875075b3 DB |
1779 | if (!error) { |
1780 | uint32_t nconns; | |
1781 | error = ct_dpif_get_nconns(dpif, &nconns); | |
1782 | ||
1783 | if (!error) { | |
1784 | dpctl_print(dpctl_p, "%u\n", nconns); | |
1785 | } else { | |
1786 | dpctl_error(dpctl_p, error, "nconns could not be retrieved"); | |
1787 | } | |
1788 | dpif_close(dpif); | |
1789 | } | |
1790 | ||
1791 | return error; | |
1792 | } | |
1793 | ||
64207120 DB |
1794 | static int |
1795 | dpctl_ct_set_tcp_seq_chk__(int argc, const char *argv[], | |
1796 | struct dpctl_params *dpctl_p, bool enabled) | |
1797 | { | |
1798 | struct dpif *dpif; | |
1799 | int error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); | |
1800 | if (!error) { | |
1801 | error = ct_dpif_set_tcp_seq_chk(dpif, enabled); | |
1802 | if (!error) { | |
1803 | dpctl_print(dpctl_p, | |
1804 | "%s TCP sequence checking successful", | |
1805 | enabled ? "enabling" : "disabling"); | |
1806 | } else { | |
1807 | dpctl_error(dpctl_p, error, | |
1808 | "%s TCP sequence checking failed", | |
1809 | enabled ? "enabling" : "disabling"); | |
1810 | } | |
1811 | dpif_close(dpif); | |
1812 | } | |
1813 | return error; | |
1814 | } | |
1815 | ||
1816 | static int | |
1817 | dpctl_ct_enable_tcp_seq_chk(int argc, const char *argv[], | |
1818 | struct dpctl_params *dpctl_p) | |
1819 | { | |
1820 | return dpctl_ct_set_tcp_seq_chk__(argc, argv, dpctl_p, true); | |
1821 | } | |
1822 | ||
1823 | static int | |
1824 | dpctl_ct_disable_tcp_seq_chk(int argc, const char *argv[], | |
1825 | struct dpctl_params *dpctl_p) | |
1826 | { | |
1827 | return dpctl_ct_set_tcp_seq_chk__(argc, argv, dpctl_p, false); | |
1828 | } | |
1829 | ||
1830 | static int | |
1831 | dpctl_ct_get_tcp_seq_chk(int argc, const char *argv[], | |
1832 | struct dpctl_params *dpctl_p) | |
1833 | { | |
1834 | struct dpif *dpif; | |
1835 | int error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); | |
1836 | if (!error) { | |
1837 | bool enabled; | |
1838 | error = ct_dpif_get_tcp_seq_chk(dpif, &enabled); | |
1839 | if (!error) { | |
1840 | dpctl_print(dpctl_p, "TCP sequence checking: %s\n", | |
1841 | enabled ? "enabled" : "disabled"); | |
1842 | } else { | |
1843 | dpctl_error(dpctl_p, error, "TCP sequence checking query failed"); | |
1844 | } | |
1845 | dpif_close(dpif); | |
1846 | } | |
1847 | return error; | |
1848 | } | |
1849 | ||
4eeec031 YHW |
1850 | static int |
1851 | dpctl_ct_set_limits(int argc, const char *argv[], | |
1852 | struct dpctl_params *dpctl_p) | |
1853 | { | |
1854 | struct dpif *dpif; | |
1855 | struct ds ds = DS_EMPTY_INITIALIZER; | |
01dcd687 | 1856 | int i = dp_arg_exists(argc, argv) ? 2 : 1; |
4eeec031 YHW |
1857 | uint32_t default_limit, *p_default_limit = NULL; |
1858 | struct ovs_list zone_limits = OVS_LIST_INITIALIZER(&zone_limits); | |
1859 | ||
01dcd687 | 1860 | int error = opt_dpif_open(argc, argv, dpctl_p, INT_MAX, &dpif); |
4eeec031 YHW |
1861 | if (error) { |
1862 | return error; | |
1863 | } | |
1864 | ||
1865 | /* Parse default limit */ | |
1866 | if (!strncmp(argv[i], "default=", 8)) { | |
1867 | if (ovs_scan(argv[i], "default=%"SCNu32, &default_limit)) { | |
1868 | p_default_limit = &default_limit; | |
1869 | i++; | |
1870 | } else { | |
1871 | ds_put_cstr(&ds, "invalid default limit"); | |
1872 | error = EINVAL; | |
1873 | goto error; | |
1874 | } | |
1875 | } | |
1876 | ||
1877 | /* Parse ct zone limit tuples */ | |
1878 | while (i < argc) { | |
1879 | uint16_t zone; | |
1880 | uint32_t limit; | |
1881 | if (!ct_dpif_parse_zone_limit_tuple(argv[i++], &zone, &limit, &ds)) { | |
1882 | error = EINVAL; | |
1883 | goto error; | |
1884 | } | |
1885 | ct_dpif_push_zone_limit(&zone_limits, zone, limit, 0); | |
1886 | } | |
1887 | ||
1888 | error = ct_dpif_set_limits(dpif, p_default_limit, &zone_limits); | |
1889 | if (!error) { | |
1890 | ct_dpif_free_zone_limits(&zone_limits); | |
1891 | dpif_close(dpif); | |
1892 | return 0; | |
1893 | } else { | |
1894 | ds_put_cstr(&ds, "failed to set conntrack limit"); | |
1895 | } | |
1896 | ||
1897 | error: | |
1898 | dpctl_error(dpctl_p, error, "%s", ds_cstr(&ds)); | |
1899 | ds_destroy(&ds); | |
1900 | ct_dpif_free_zone_limits(&zone_limits); | |
1901 | dpif_close(dpif); | |
1902 | return error; | |
1903 | } | |
1904 | ||
1905 | static int | |
1906 | parse_ct_limit_zones(const char *argv, struct ovs_list *zone_limits, | |
1907 | struct ds *ds) | |
1908 | { | |
1909 | char *save_ptr = NULL, *argcopy, *next_zone; | |
1910 | uint16_t zone; | |
1911 | ||
1912 | if (strncmp(argv, "zone=", 5)) { | |
1913 | ds_put_format(ds, "invalid argument %s", argv); | |
1914 | return EINVAL; | |
1915 | } | |
1916 | ||
1917 | argcopy = xstrdup(argv + 5); | |
1918 | next_zone = strtok_r(argcopy, ",", &save_ptr); | |
1919 | ||
1920 | do { | |
1921 | if (ovs_scan(next_zone, "%"SCNu16, &zone)) { | |
1922 | ct_dpif_push_zone_limit(zone_limits, zone, 0, 0); | |
1923 | } else { | |
1924 | ds_put_cstr(ds, "invalid zone"); | |
1925 | free(argcopy); | |
1926 | return EINVAL; | |
1927 | } | |
1928 | } while ((next_zone = strtok_r(NULL, ",", &save_ptr)) != NULL); | |
1929 | ||
1930 | free(argcopy); | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1934 | static int | |
1935 | dpctl_ct_del_limits(int argc, const char *argv[], | |
1936 | struct dpctl_params *dpctl_p) | |
1937 | { | |
1938 | struct dpif *dpif; | |
1939 | struct ds ds = DS_EMPTY_INITIALIZER; | |
01dcd687 DB |
1940 | int error; |
1941 | int i = dp_arg_exists(argc, argv) ? 2 : 1; | |
4eeec031 YHW |
1942 | struct ovs_list zone_limits = OVS_LIST_INITIALIZER(&zone_limits); |
1943 | ||
01dcd687 | 1944 | error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); |
4eeec031 YHW |
1945 | if (error) { |
1946 | return error; | |
1947 | } | |
1948 | ||
1949 | error = parse_ct_limit_zones(argv[i], &zone_limits, &ds); | |
1950 | if (error) { | |
1951 | goto error; | |
1952 | } | |
1953 | ||
1954 | error = ct_dpif_del_limits(dpif, &zone_limits); | |
1955 | if (!error) { | |
1956 | goto out; | |
1957 | } else { | |
1958 | ds_put_cstr(&ds, "failed to delete conntrack limit"); | |
1959 | } | |
1960 | ||
1961 | error: | |
1962 | dpctl_error(dpctl_p, error, "%s", ds_cstr(&ds)); | |
1963 | ds_destroy(&ds); | |
1964 | out: | |
1965 | ct_dpif_free_zone_limits(&zone_limits); | |
1966 | dpif_close(dpif); | |
1967 | return error; | |
1968 | } | |
1969 | ||
1970 | static int | |
1971 | dpctl_ct_get_limits(int argc, const char *argv[], | |
1972 | struct dpctl_params *dpctl_p) | |
1973 | { | |
1974 | struct dpif *dpif; | |
1975 | struct ds ds = DS_EMPTY_INITIALIZER; | |
1976 | uint32_t default_limit; | |
01dcd687 | 1977 | int i = dp_arg_exists(argc, argv) ? 2 : 1; |
4eeec031 YHW |
1978 | struct ovs_list list_query = OVS_LIST_INITIALIZER(&list_query); |
1979 | struct ovs_list list_reply = OVS_LIST_INITIALIZER(&list_reply); | |
1980 | ||
01dcd687 | 1981 | int error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); |
4eeec031 YHW |
1982 | if (error) { |
1983 | return error; | |
1984 | } | |
1985 | ||
1986 | if (argc > i) { | |
1987 | error = parse_ct_limit_zones(argv[i], &list_query, &ds); | |
1988 | if (error) { | |
1989 | goto error; | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | error = ct_dpif_get_limits(dpif, &default_limit, &list_query, | |
1994 | &list_reply); | |
1995 | if (!error) { | |
1996 | ct_dpif_format_zone_limits(default_limit, &list_reply, &ds); | |
1997 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds)); | |
1998 | goto out; | |
1999 | } else { | |
2000 | ds_put_format(&ds, "failed to get conntrack limit %s", | |
2001 | ovs_strerror(error)); | |
2002 | } | |
2003 | ||
2004 | error: | |
2005 | dpctl_error(dpctl_p, error, "%s", ds_cstr(&ds)); | |
2006 | out: | |
2007 | ds_destroy(&ds); | |
2008 | ct_dpif_free_zone_limits(&list_query); | |
2009 | ct_dpif_free_zone_limits(&list_reply); | |
2010 | dpif_close(dpif); | |
2011 | return error; | |
2012 | } | |
2013 | ||
4ea96698 DB |
2014 | static int |
2015 | ipf_set_enabled__(int argc, const char *argv[], struct dpctl_params *dpctl_p, | |
2016 | bool enabled) | |
2017 | { | |
2018 | struct dpif *dpif; | |
2019 | int error = opt_dpif_open(argc, argv, dpctl_p, 4, &dpif); | |
2020 | if (!error) { | |
2021 | char v4_or_v6[3] = {0}; | |
2022 | if (ovs_scan(argv[argc - 1], "%2s", v4_or_v6) && | |
2023 | (!strncmp(v4_or_v6, "v4", 2) || !strncmp(v4_or_v6, "v6", 2))) { | |
2024 | error = ct_dpif_ipf_set_enabled( | |
2025 | dpif, !strncmp(v4_or_v6, "v6", 2), enabled); | |
2026 | if (!error) { | |
2027 | dpctl_print(dpctl_p, | |
2028 | "%s fragmentation reassembly successful", | |
2029 | enabled ? "enabling" : "disabling"); | |
2030 | } else { | |
2031 | dpctl_error(dpctl_p, error, | |
2032 | "%s fragmentation reassembly failed", | |
2033 | enabled ? "enabling" : "disabling"); | |
2034 | } | |
2035 | } else { | |
2036 | error = EINVAL; | |
2037 | dpctl_error(dpctl_p, error, | |
2038 | "parameter missing: 'v4' for IPv4 or 'v6' for IPv6"); | |
2039 | } | |
2040 | dpif_close(dpif); | |
2041 | } | |
2042 | return error; | |
2043 | } | |
2044 | ||
2045 | static int | |
2046 | dpctl_ipf_set_enabled(int argc, const char *argv[], | |
2047 | struct dpctl_params *dpctl_p) | |
2048 | { | |
2049 | return ipf_set_enabled__(argc, argv, dpctl_p, true); | |
2050 | } | |
2051 | ||
2052 | static int | |
2053 | dpctl_ipf_set_disabled(int argc, const char *argv[], | |
2054 | struct dpctl_params *dpctl_p) | |
2055 | { | |
2056 | return ipf_set_enabled__(argc, argv, dpctl_p, false); | |
2057 | } | |
2058 | ||
2059 | static int | |
2060 | dpctl_ipf_set_min_frag(int argc, const char *argv[], | |
2061 | struct dpctl_params *dpctl_p) | |
2062 | { | |
2063 | struct dpif *dpif; | |
2064 | int error = opt_dpif_open(argc, argv, dpctl_p, 4, &dpif); | |
2065 | if (!error) { | |
2066 | char v4_or_v6[3] = {0}; | |
2067 | if (ovs_scan(argv[argc - 2], "%2s", v4_or_v6) && | |
2068 | (!strncmp(v4_or_v6, "v4", 2) || !strncmp(v4_or_v6, "v6", 2))) { | |
2069 | uint32_t min_fragment; | |
2070 | if (ovs_scan(argv[argc - 1], "%"SCNu32, &min_fragment)) { | |
2071 | error = ct_dpif_ipf_set_min_frag( | |
2072 | dpif, !strncmp(v4_or_v6, "v6", 2), min_fragment); | |
2073 | if (!error) { | |
2074 | dpctl_print(dpctl_p, | |
2075 | "setting minimum fragment size successful"); | |
2076 | } else { | |
2077 | dpctl_error(dpctl_p, error, | |
2078 | "requested minimum fragment size too small;" | |
2079 | " see documentation"); | |
2080 | } | |
2081 | } else { | |
2082 | error = EINVAL; | |
2083 | dpctl_error(dpctl_p, error, | |
2084 | "parameter missing for minimum fragment size"); | |
2085 | } | |
2086 | } else { | |
2087 | error = EINVAL; | |
2088 | dpctl_error(dpctl_p, error, | |
2089 | "parameter missing: v4 for IPv4 or v6 for IPv6"); | |
2090 | } | |
2091 | dpif_close(dpif); | |
2092 | } | |
2093 | ||
2094 | return error; | |
2095 | } | |
2096 | ||
2097 | static int | |
2098 | dpctl_ipf_set_max_nfrags(int argc, const char *argv[], | |
2099 | struct dpctl_params *dpctl_p) | |
2100 | { | |
2101 | struct dpif *dpif; | |
2102 | int error = opt_dpif_open(argc, argv, dpctl_p, 3, &dpif); | |
2103 | if (!error) { | |
2104 | uint32_t nfrags_max; | |
2105 | if (ovs_scan(argv[argc - 1], "%"SCNu32, &nfrags_max)) { | |
2106 | error = ct_dpif_ipf_set_max_nfrags(dpif, nfrags_max); | |
2107 | if (!error) { | |
2108 | dpctl_print(dpctl_p, | |
2109 | "setting maximum fragments successful"); | |
2110 | } else { | |
2111 | dpctl_error(dpctl_p, error, | |
2112 | "setting maximum fragments failed"); | |
2113 | } | |
2114 | } else { | |
2115 | error = EINVAL; | |
2116 | dpctl_error(dpctl_p, error, | |
2117 | "parameter missing for maximum fragments"); | |
2118 | } | |
2119 | dpif_close(dpif); | |
2120 | } | |
2121 | ||
2122 | return error; | |
2123 | } | |
2124 | ||
2125 | static void | |
2126 | dpctl_dump_ipf(struct dpif *dpif, struct dpctl_params *dpctl_p) | |
2127 | { | |
2128 | struct ipf_dump_ctx *dump_ctx; | |
2129 | char *dump; | |
2130 | ||
2131 | int error = ct_dpif_ipf_dump_start(dpif, &dump_ctx); | |
2132 | if (error) { | |
2133 | dpctl_error(dpctl_p, error, "starting ipf list dump"); | |
2134 | /* Nothing to clean up, just return. */ | |
2135 | return; | |
2136 | } | |
2137 | ||
2138 | dpctl_print(dpctl_p, "\n Fragment Lists:\n\n"); | |
2139 | while (!(error = ct_dpif_ipf_dump_next(dpif, dump_ctx, &dump))) { | |
2140 | dpctl_print(dpctl_p, "%s\n", dump); | |
2141 | free(dump); | |
2142 | } | |
2143 | ||
2144 | if (error && error != EOF) { | |
2145 | dpctl_error(dpctl_p, error, "dumping ipf lists failed"); | |
2146 | } | |
2147 | ||
2148 | ct_dpif_ipf_dump_done(dpif, dump_ctx); | |
2149 | } | |
2150 | ||
2151 | static int | |
2152 | dpctl_ct_ipf_get_status(int argc, const char *argv[], | |
2153 | struct dpctl_params *dpctl_p) | |
2154 | { | |
2155 | struct dpif *dpif; | |
2156 | int error = opt_dpif_open(argc, argv, dpctl_p, 2, &dpif); | |
2157 | ||
2158 | if (!error) { | |
2159 | struct dpif_ipf_status dpif_ipf_status; | |
2160 | error = ct_dpif_ipf_get_status(dpif, &dpif_ipf_status); | |
2161 | ||
2162 | if (!error) { | |
2163 | dpctl_print(dpctl_p, " Fragmentation Module Status\n"); | |
2164 | dpctl_print(dpctl_p, " ---------------------------\n"); | |
2165 | dpctl_print(dpctl_p, " v4 enabled: %u\n", | |
2166 | dpif_ipf_status.v4.enabled); | |
2167 | dpctl_print(dpctl_p, " v6 enabled: %u\n", | |
2168 | dpif_ipf_status.v6.enabled); | |
2169 | dpctl_print(dpctl_p, " max num frags (v4/v6): %u\n", | |
2170 | dpif_ipf_status.nfrag_max); | |
2171 | dpctl_print(dpctl_p, " num frag: %u\n", | |
2172 | dpif_ipf_status.nfrag); | |
2173 | dpctl_print(dpctl_p, " min v4 frag size: %u\n", | |
2174 | dpif_ipf_status.v4.min_frag_size); | |
2175 | dpctl_print(dpctl_p, " v4 frags accepted: %"PRIu64"\n", | |
2176 | dpif_ipf_status.v4.nfrag_accepted); | |
2177 | dpctl_print(dpctl_p, " v4 frags completed: %"PRIu64"\n", | |
2178 | dpif_ipf_status.v4.nfrag_completed_sent); | |
2179 | dpctl_print(dpctl_p, " v4 frags expired: %"PRIu64"\n", | |
2180 | dpif_ipf_status.v4.nfrag_expired_sent); | |
2181 | dpctl_print(dpctl_p, " v4 frags too small: %"PRIu64"\n", | |
2182 | dpif_ipf_status.v4.nfrag_too_small); | |
2183 | dpctl_print(dpctl_p, " v4 frags overlapped: %"PRIu64"\n", | |
2184 | dpif_ipf_status.v4.nfrag_overlap); | |
2185 | dpctl_print(dpctl_p, " v4 frags purged: %"PRIu64"\n", | |
2186 | dpif_ipf_status.v4.nfrag_purged); | |
2187 | ||
2188 | dpctl_print(dpctl_p, " min v6 frag size: %u\n", | |
2189 | dpif_ipf_status.v6.min_frag_size); | |
2190 | dpctl_print(dpctl_p, " v6 frags accepted: %"PRIu64"\n", | |
2191 | dpif_ipf_status.v6.nfrag_accepted); | |
2192 | dpctl_print(dpctl_p, " v6 frags completed: %"PRIu64"\n", | |
2193 | dpif_ipf_status.v6.nfrag_completed_sent); | |
2194 | dpctl_print(dpctl_p, " v6 frags expired: %"PRIu64"\n", | |
2195 | dpif_ipf_status.v6.nfrag_expired_sent); | |
2196 | dpctl_print(dpctl_p, " v6 frags too small: %"PRIu64"\n", | |
2197 | dpif_ipf_status.v6.nfrag_too_small); | |
2198 | dpctl_print(dpctl_p, " v6 frags overlapped: %"PRIu64"\n", | |
2199 | dpif_ipf_status.v6.nfrag_overlap); | |
2200 | dpctl_print(dpctl_p, " v6 frags purged: %"PRIu64"\n", | |
2201 | dpif_ipf_status.v6.nfrag_purged); | |
2202 | } else { | |
2203 | dpctl_error(dpctl_p, error, | |
2204 | "ipf status could not be retrieved"); | |
2205 | return error; | |
2206 | } | |
2207 | ||
2208 | if (dpctl_p->verbosity) { | |
2209 | dpctl_dump_ipf(dpif, dpctl_p); | |
2210 | } | |
2211 | ||
2212 | dpif_close(dpif); | |
2213 | } | |
2214 | ||
2215 | return error; | |
2216 | } | |
2217 | ||
fceef209 DDP |
2218 | /* Undocumented commands for unit testing. */ |
2219 | ||
2220 | static int | |
2221 | dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p) | |
2222 | { | |
2223 | int i, error = 0; | |
2224 | ||
2225 | for (i = 1; i < argc; i++) { | |
2226 | struct ofpbuf actions; | |
2227 | struct ds s; | |
2228 | ||
2229 | ofpbuf_init(&actions, 0); | |
2230 | error = odp_actions_from_string(argv[i], NULL, &actions); | |
2231 | ||
2232 | if (error) { | |
2233 | ofpbuf_uninit(&actions); | |
2234 | dpctl_error(dpctl_p, error, "odp_actions_from_string"); | |
2235 | return error; | |
2236 | } | |
2237 | ||
2238 | ds_init(&s); | |
0722f341 | 2239 | format_odp_actions(&s, actions.data, actions.size, NULL); |
fceef209 DDP |
2240 | dpctl_print(dpctl_p, "%s\n", ds_cstr(&s)); |
2241 | ds_destroy(&s); | |
2242 | ||
2243 | ofpbuf_uninit(&actions); | |
2244 | } | |
2245 | ||
2246 | return error; | |
2247 | } | |
2248 | ||
2249 | struct actions_for_flow { | |
2250 | struct hmap_node hmap_node; | |
2251 | struct flow flow; | |
2252 | struct ofpbuf actions; | |
2253 | }; | |
2254 | ||
2255 | static struct actions_for_flow * | |
2256 | get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow) | |
2257 | { | |
2258 | uint32_t hash = flow_hash(flow, 0); | |
2259 | struct actions_for_flow *af; | |
2260 | ||
2261 | HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) { | |
2262 | if (flow_equal(&af->flow, flow)) { | |
2263 | return af; | |
2264 | } | |
2265 | } | |
2266 | ||
2267 | af = xmalloc(sizeof *af); | |
2268 | af->flow = *flow; | |
2269 | ofpbuf_init(&af->actions, 0); | |
2270 | hmap_insert(actions_per_flow, &af->hmap_node, hash); | |
2271 | return af; | |
2272 | } | |
2273 | ||
2274 | static int | |
2275 | compare_actions_for_flow(const void *a_, const void *b_) | |
2276 | { | |
2277 | struct actions_for_flow *const *a = a_; | |
2278 | struct actions_for_flow *const *b = b_; | |
2279 | ||
2280 | return flow_compare_3way(&(*a)->flow, &(*b)->flow); | |
2281 | } | |
2282 | ||
2283 | static int | |
2284 | compare_output_actions(const void *a_, const void *b_) | |
2285 | { | |
2286 | const struct nlattr *a = a_; | |
2287 | const struct nlattr *b = b_; | |
2288 | uint32_t a_port = nl_attr_get_u32(a); | |
2289 | uint32_t b_port = nl_attr_get_u32(b); | |
2290 | ||
2291 | return a_port < b_port ? -1 : a_port > b_port; | |
2292 | } | |
2293 | ||
2294 | static void | |
2295 | sort_output_actions__(struct nlattr *first, struct nlattr *end) | |
2296 | { | |
2297 | size_t bytes = (uint8_t *) end - (uint8_t *) first; | |
2298 | size_t n = bytes / NL_A_U32_SIZE; | |
2299 | ||
2300 | ovs_assert(bytes % NL_A_U32_SIZE == 0); | |
2301 | qsort(first, n, NL_A_U32_SIZE, compare_output_actions); | |
2302 | } | |
2303 | ||
2304 | static void | |
2305 | sort_output_actions(struct nlattr *actions, size_t length) | |
2306 | { | |
2307 | struct nlattr *first_output = NULL; | |
2308 | struct nlattr *a; | |
2309 | int left; | |
2310 | ||
2311 | NL_ATTR_FOR_EACH (a, left, actions, length) { | |
2312 | if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) { | |
2313 | if (!first_output) { | |
2314 | first_output = a; | |
2315 | } | |
2316 | } else { | |
2317 | if (first_output) { | |
2318 | sort_output_actions__(first_output, a); | |
2319 | first_output = NULL; | |
2320 | } | |
2321 | } | |
2322 | } | |
2323 | if (first_output) { | |
2324 | uint8_t *end = (uint8_t *) actions + length; | |
2325 | sort_output_actions__(first_output, | |
2326 | ALIGNED_CAST(struct nlattr *, end)); | |
2327 | } | |
2328 | } | |
2329 | ||
2330 | /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS | |
2331 | * have the syntax used by "ovs-dpctl dump-flows". | |
2332 | * | |
2333 | * This command prints ACTIONS in a format that shows what happens for each | |
2334 | * VLAN, independent of the order of the ACTIONS. For example, there is more | |
2335 | * than one way to output a packet on VLANs 9 and 11, but this command will | |
2336 | * print the same output for any form. | |
2337 | * | |
2338 | * The idea here generalizes beyond VLANs (e.g. to setting other fields) but | |
2339 | * so far the implementation only covers VLANs. */ | |
2340 | static int | |
2341 | dpctl_normalize_actions(int argc, const char *argv[], | |
2342 | struct dpctl_params *dpctl_p) | |
2343 | { | |
2344 | struct simap port_names; | |
2345 | struct ofpbuf keybuf; | |
2346 | struct flow flow; | |
2347 | struct ofpbuf odp_actions; | |
2348 | struct hmap actions_per_flow; | |
2349 | struct actions_for_flow **afs; | |
2350 | struct actions_for_flow *af; | |
2351 | struct nlattr *a; | |
2352 | size_t n_afs; | |
2353 | struct ds s; | |
2354 | int left; | |
2355 | int i, error; | |
f0fb825a | 2356 | int encaps = 0; |
fceef209 DDP |
2357 | |
2358 | ds_init(&s); | |
2359 | ||
2360 | simap_init(&port_names); | |
2361 | for (i = 3; i < argc; i++) { | |
2362 | char name[16]; | |
2363 | int number; | |
2364 | ||
2365 | if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) { | |
2366 | uintptr_t n = number; | |
2367 | simap_put(&port_names, name, n); | |
2368 | } else { | |
2369 | dpctl_error(dpctl_p, 0, "%s: expected NAME=NUMBER", argv[i]); | |
2370 | error = EINVAL; | |
2371 | goto out; | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | /* Parse flow key. */ | |
2376 | ofpbuf_init(&keybuf, 0); | |
d40533fc BP |
2377 | char *error_s; |
2378 | error = odp_flow_from_string(argv[1], &port_names, &keybuf, NULL, | |
2379 | &error_s); | |
fceef209 | 2380 | if (error) { |
d40533fc BP |
2381 | dpctl_error(dpctl_p, error, "odp_flow_key_from_string (%s)", error_s); |
2382 | free(error_s); | |
fceef209 DDP |
2383 | goto out_freekeybuf; |
2384 | } | |
2385 | ||
2386 | ds_clear(&s); | |
6fd6ed71 | 2387 | odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL, |
fceef209 DDP |
2388 | &s, dpctl_p->verbosity); |
2389 | dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s)); | |
2390 | ||
d40533fc | 2391 | error = odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow, &error_s); |
fceef209 | 2392 | if (error) { |
d40533fc BP |
2393 | dpctl_error(dpctl_p, error, "odp_flow_key_to_flow failed (%s)", |
2394 | error_s ? error_s : "reason unknown"); | |
2395 | free(error_s); | |
fceef209 DDP |
2396 | goto out_freekeybuf; |
2397 | } | |
2398 | ||
2399 | /* Parse actions. */ | |
2400 | ofpbuf_init(&odp_actions, 0); | |
2401 | error = odp_actions_from_string(argv[2], &port_names, &odp_actions); | |
2402 | if (error) { | |
2403 | dpctl_error(dpctl_p, error, "odp_actions_from_string"); | |
2404 | goto out_freeactions; | |
2405 | } | |
2406 | ||
2407 | if (dpctl_p->verbosity) { | |
2408 | ds_clear(&s); | |
0722f341 | 2409 | format_odp_actions(&s, odp_actions.data, odp_actions.size, NULL); |
fceef209 DDP |
2410 | dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s)); |
2411 | } | |
2412 | ||
2413 | hmap_init(&actions_per_flow); | |
6fd6ed71 | 2414 | NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) { |
fceef209 DDP |
2415 | const struct ovs_action_push_vlan *push; |
2416 | switch(nl_attr_type(a)) { | |
2417 | case OVS_ACTION_ATTR_POP_VLAN: | |
f0fb825a | 2418 | flow_pop_vlan(&flow, NULL); |
fceef209 DDP |
2419 | continue; |
2420 | ||
2421 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
f0fb825a | 2422 | flow_push_vlan_uninit(&flow, NULL); |
fceef209 | 2423 | push = nl_attr_get_unspec(a, sizeof *push); |
f0fb825a EG |
2424 | flow.vlans[0].tpid = push->vlan_tpid; |
2425 | flow.vlans[0].tci = push->vlan_tci; | |
fceef209 DDP |
2426 | continue; |
2427 | } | |
2428 | ||
2429 | af = get_actions_for_flow(&actions_per_flow, &flow); | |
2430 | nl_msg_put_unspec(&af->actions, nl_attr_type(a), | |
2431 | nl_attr_get(a), nl_attr_get_size(a)); | |
2432 | } | |
2433 | ||
2434 | n_afs = hmap_count(&actions_per_flow); | |
2435 | afs = xmalloc(n_afs * sizeof *afs); | |
2436 | i = 0; | |
2437 | HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) { | |
2438 | afs[i++] = af; | |
2439 | } | |
2440 | ||
2441 | ovs_assert(i == n_afs); | |
2442 | hmap_destroy(&actions_per_flow); | |
2443 | ||
2444 | qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow); | |
2445 | ||
2446 | for (i = 0; i < n_afs; i++) { | |
71f21279 | 2447 | af = afs[i]; |
6fd6ed71 | 2448 | sort_output_actions(af->actions.data, af->actions.size); |
fceef209 | 2449 | |
f0fb825a EG |
2450 | for (encaps = 0; encaps < FLOW_MAX_VLAN_HEADERS; encaps ++) { |
2451 | union flow_vlan_hdr *vlan = &af->flow.vlans[encaps]; | |
2452 | if (vlan->tci != htons(0)) { | |
2453 | dpctl_print(dpctl_p, "vlan("); | |
2454 | if (vlan->tpid != htons(ETH_TYPE_VLAN)) { | |
2455 | dpctl_print(dpctl_p, "tpid=0x%04"PRIx16",", vlan->tpid); | |
2456 | } | |
2457 | dpctl_print(dpctl_p, "vid=%"PRIu16",pcp=%d): ", | |
2458 | vlan_tci_to_vid(vlan->tci), | |
2459 | vlan_tci_to_pcp(vlan->tci)); | |
2460 | } else { | |
2461 | if (encaps == 0) { | |
2462 | dpctl_print(dpctl_p, "no vlan: "); | |
2463 | } | |
2464 | break; | |
2465 | } | |
fceef209 DDP |
2466 | } |
2467 | ||
2468 | if (eth_type_mpls(af->flow.dl_type)) { | |
2469 | dpctl_print(dpctl_p, "mpls(label=%"PRIu32",tc=%d,ttl=%d): ", | |
2470 | mpls_lse_to_label(af->flow.mpls_lse[0]), | |
2471 | mpls_lse_to_tc(af->flow.mpls_lse[0]), | |
2472 | mpls_lse_to_ttl(af->flow.mpls_lse[0])); | |
2473 | } else { | |
2474 | dpctl_print(dpctl_p, "no mpls: "); | |
2475 | } | |
2476 | ||
2477 | ds_clear(&s); | |
0722f341 | 2478 | format_odp_actions(&s, af->actions.data, af->actions.size, NULL); |
7521b9e5 | 2479 | dpctl_puts(dpctl_p, false, ds_cstr(&s)); |
fceef209 DDP |
2480 | |
2481 | ofpbuf_uninit(&af->actions); | |
2482 | free(af); | |
2483 | } | |
2484 | free(afs); | |
2485 | ||
2486 | ||
2487 | out_freeactions: | |
2488 | ofpbuf_uninit(&odp_actions); | |
2489 | out_freekeybuf: | |
2490 | ofpbuf_uninit(&keybuf); | |
2491 | out: | |
2492 | simap_destroy(&port_names); | |
2493 | ds_destroy(&s); | |
2494 | ||
2495 | return error; | |
2496 | } | |
2497 | \f | |
fceef209 | 2498 | static const struct dpctl_command all_commands[] = { |
1ed90a98 BP |
2499 | { "add-dp", "dp [iface...]", 1, INT_MAX, dpctl_add_dp, DP_RW }, |
2500 | { "del-dp", "dp", 1, 1, dpctl_del_dp, DP_RW }, | |
2501 | { "add-if", "dp iface...", 2, INT_MAX, dpctl_add_if, DP_RW }, | |
2502 | { "del-if", "dp iface...", 2, INT_MAX, dpctl_del_if, DP_RW }, | |
2503 | { "set-if", "dp iface...", 2, INT_MAX, dpctl_set_if, DP_RW }, | |
1f4a7252 RM |
2504 | { "dump-dps", "", 0, 0, dpctl_dump_dps, DP_RO }, |
2505 | { "show", "[dp...]", 0, INT_MAX, dpctl_show, DP_RO }, | |
1401f6de FA |
2506 | { "dump-flows", "[dp] [filter=..] [type=..]", |
2507 | 0, 3, dpctl_dump_flows, DP_RO }, | |
1ed90a98 BP |
2508 | { "add-flow", "[dp] flow actions", 2, 3, dpctl_add_flow, DP_RW }, |
2509 | { "mod-flow", "[dp] flow actions", 2, 3, dpctl_mod_flow, DP_RW }, | |
2510 | { "get-flow", "[dp] ufid", 1, 2, dpctl_get_flow, DP_RO }, | |
2511 | { "del-flow", "[dp] flow", 1, 2, dpctl_del_flow, DP_RW }, | |
1f4a7252 RM |
2512 | { "del-flows", "[dp]", 0, 1, dpctl_del_flows, DP_RW }, |
2513 | { "dump-conntrack", "[dp] [zone=N]", 0, 2, dpctl_dump_conntrack, DP_RO }, | |
c43a1331 YHW |
2514 | { "flush-conntrack", "[dp] [zone=N] [ct-tuple]", 0, 3, |
2515 | dpctl_flush_conntrack, DP_RW }, | |
16b361ef | 2516 | { "ct-stats-show", "[dp] [zone=N]", |
8a0d9d85 | 2517 | 0, 3, dpctl_ct_stats_show, DP_RO }, |
ded30c74 | 2518 | { "ct-bkts", "[dp] [gt=N]", 0, 2, dpctl_ct_bkts, DP_RO }, |
64207120 DB |
2519 | { "ct-set-maxconns", "[dp] maxconns", 1, 2, dpctl_ct_set_maxconns, |
2520 | DP_RW }, | |
c92339ad | 2521 | { "ct-get-maxconns", "[dp]", 0, 1, dpctl_ct_get_maxconns, DP_RO }, |
875075b3 | 2522 | { "ct-get-nconns", "[dp]", 0, 1, dpctl_ct_get_nconns, DP_RO }, |
64207120 DB |
2523 | { "ct-enable-tcp-seq-chk", "[dp]", 0, 1, dpctl_ct_enable_tcp_seq_chk, |
2524 | DP_RW }, | |
2525 | { "ct-disable-tcp-seq-chk", "[dp]", 0, 1, dpctl_ct_disable_tcp_seq_chk, | |
2526 | DP_RW }, | |
2527 | { "ct-get-tcp-seq-chk", "[dp]", 0, 1, dpctl_ct_get_tcp_seq_chk, DP_RO }, | |
4eeec031 YHW |
2528 | { "ct-set-limits", "[dp] [default=L] [zone=N,limit=L]...", 1, INT_MAX, |
2529 | dpctl_ct_set_limits, DP_RO }, | |
2530 | { "ct-del-limits", "[dp] zone=N1[,N2]...", 1, 2, dpctl_ct_del_limits, | |
2531 | DP_RO }, | |
2532 | { "ct-get-limits", "[dp] [zone=N1[,N2]...]", 0, 2, dpctl_ct_get_limits, | |
2533 | DP_RO }, | |
4ea96698 DB |
2534 | { "ipf-set-enabled", "[dp] v4|v6", 1, 2, dpctl_ipf_set_enabled, DP_RW }, |
2535 | { "ipf-set-disabled", "[dp] v4|v6", 1, 2, dpctl_ipf_set_disabled, DP_RW }, | |
2536 | { "ipf-set-min-frag", "[dp] v4|v6 minfragment", 2, 3, | |
2537 | dpctl_ipf_set_min_frag, DP_RW }, | |
2538 | { "ipf-set-max-nfrags", "[dp] maxfrags", 1, 2, | |
2539 | dpctl_ipf_set_max_nfrags, DP_RW }, | |
2540 | { "ipf-get-status", "[dp]", 0, 1, dpctl_ct_ipf_get_status, | |
2541 | DP_RO }, | |
1f4a7252 RM |
2542 | { "help", "", 0, INT_MAX, dpctl_help, DP_RO }, |
2543 | { "list-commands", "", 0, INT_MAX, dpctl_list_commands, DP_RO }, | |
fceef209 DDP |
2544 | |
2545 | /* Undocumented commands for testing. */ | |
1f4a7252 | 2546 | { "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions, DP_RO }, |
1401f6de FA |
2547 | { "normalize-actions", "actions", |
2548 | 2, INT_MAX, dpctl_normalize_actions, DP_RO }, | |
fceef209 | 2549 | |
1f4a7252 | 2550 | { NULL, NULL, 0, 0, NULL, DP_RO }, |
fceef209 DDP |
2551 | }; |
2552 | ||
56cad666 AW |
2553 | static const struct dpctl_command *get_all_dpctl_commands(void) |
2554 | { | |
2555 | return all_commands; | |
2556 | } | |
2557 | ||
fceef209 DDP |
2558 | /* Runs the command designated by argv[0] within the command table specified by |
2559 | * 'commands', which must be terminated by a command whose 'name' member is a | |
2560 | * null pointer. */ | |
2561 | int | |
2562 | dpctl_run_command(int argc, const char *argv[], struct dpctl_params *dpctl_p) | |
2563 | { | |
2564 | const struct dpctl_command *p; | |
fceef209 DDP |
2565 | if (argc < 1) { |
2566 | dpctl_error(dpctl_p, 0, "missing command name; use --help for help"); | |
2567 | return EINVAL; | |
2568 | } | |
2569 | ||
2570 | for (p = all_commands; p->name != NULL; p++) { | |
2571 | if (!strcmp(p->name, argv[0])) { | |
2572 | int n_arg = argc - 1; | |
2573 | if (n_arg < p->min_args) { | |
2574 | dpctl_error(dpctl_p, 0, | |
2575 | "'%s' command requires at least %d arguments", | |
2576 | p->name, p->min_args); | |
2577 | return EINVAL; | |
2578 | } else if (n_arg > p->max_args) { | |
2579 | dpctl_error(dpctl_p, 0, | |
2580 | "'%s' command takes at most %d arguments", | |
2581 | p->name, p->max_args); | |
2582 | return EINVAL; | |
2583 | } else { | |
1f4a7252 RM |
2584 | if (p->mode == DP_RW && dpctl_p->read_only) { |
2585 | dpctl_error(dpctl_p, 0, | |
2586 | "'%s' command does not work in read only mode", | |
2587 | p->name); | |
2588 | return EINVAL; | |
2589 | } | |
fceef209 DDP |
2590 | return p->handler(argc, argv, dpctl_p); |
2591 | } | |
2592 | } | |
2593 | } | |
2594 | ||
2595 | dpctl_error(dpctl_p, 0, "unknown command '%s'; use --help for help", | |
2596 | argv[0]); | |
2597 | return EINVAL; | |
2598 | } | |
2599 | \f | |
2600 | static void | |
2601 | dpctl_unixctl_print(void *userdata, bool error OVS_UNUSED, const char *msg) | |
2602 | { | |
2603 | struct ds *ds = userdata; | |
2604 | ds_put_cstr(ds, msg); | |
2605 | } | |
2606 | ||
2607 | static void | |
2608 | dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[], | |
2609 | void *aux) | |
2610 | { | |
2611 | struct ds ds = DS_EMPTY_INITIALIZER; | |
79ae214d | 2612 | bool error = false; |
fceef209 | 2613 | |
337c4528 BP |
2614 | struct dpctl_params dpctl_p = { |
2615 | .is_appctl = true, | |
2616 | .output = dpctl_unixctl_print, | |
2617 | .aux = &ds, | |
2618 | }; | |
fceef209 DDP |
2619 | |
2620 | /* Parse options (like getopt). Unfortunately it does | |
2621 | * not seem a good idea to call getopt_long() here, since it uses global | |
2622 | * variables */ | |
d1fd1ea9 | 2623 | bool set_names = false; |
79ae214d | 2624 | while (argc > 1 && !error) { |
fceef209 DDP |
2625 | const char *arg = argv[1]; |
2626 | if (!strncmp(arg, "--", 2)) { | |
2627 | /* Long option */ | |
2628 | if (!strcmp(arg, "--statistics")) { | |
2629 | dpctl_p.print_statistics = true; | |
2630 | } else if (!strcmp(arg, "--clear")) { | |
2631 | dpctl_p.zero_statistics = true; | |
2632 | } else if (!strcmp(arg, "--may-create")) { | |
2633 | dpctl_p.may_create = true; | |
2634 | } else if (!strcmp(arg, "--more")) { | |
2635 | dpctl_p.verbosity++; | |
d1fd1ea9 BP |
2636 | } else if (!strcmp(arg, "--names")) { |
2637 | dpctl_p.names = true; | |
2638 | set_names = true; | |
2639 | } else if (!strcmp(arg, "--no-names")) { | |
2640 | dpctl_p.names = false; | |
2641 | set_names = true; | |
fceef209 DDP |
2642 | } else { |
2643 | ds_put_format(&ds, "Unrecognized option %s", argv[1]); | |
79ae214d | 2644 | error = true; |
fceef209 DDP |
2645 | } |
2646 | } else if (arg[0] == '-' && arg[1] != '\0') { | |
2647 | /* Short option[s] */ | |
2648 | const char *opt = &arg[1]; | |
2649 | ||
79ae214d | 2650 | while (*opt && !error) { |
fceef209 DDP |
2651 | switch (*opt) { |
2652 | case 'm': | |
2653 | dpctl_p.verbosity++; | |
2654 | break; | |
2655 | case 's': | |
2656 | dpctl_p.print_statistics = true; | |
2657 | break; | |
2658 | default: | |
2659 | ds_put_format(&ds, "Unrecognized option -%c", *opt); | |
79ae214d | 2660 | error = true; |
fceef209 DDP |
2661 | break; |
2662 | } | |
2663 | opt++; | |
2664 | } | |
2665 | } else { | |
2666 | /* Doesn't start with -, not an option */ | |
2667 | break; | |
2668 | } | |
2669 | ||
79ae214d | 2670 | if (error) { |
fceef209 DDP |
2671 | break; |
2672 | } | |
2673 | argv++; | |
2674 | argc--; | |
2675 | } | |
d1fd1ea9 BP |
2676 | if (!set_names) { |
2677 | dpctl_p.names = dpctl_p.verbosity > 0; | |
2678 | } | |
fceef209 | 2679 | |
79ae214d | 2680 | if (!error) { |
337c4528 | 2681 | dpctl_command_handler *handler = (dpctl_command_handler *) aux; |
79ae214d | 2682 | error = handler(argc, argv, &dpctl_p) != 0; |
fceef209 DDP |
2683 | } |
2684 | ||
79ae214d BP |
2685 | if (error) { |
2686 | unixctl_command_reply_error(conn, ds_cstr(&ds)); | |
2687 | } else { | |
2688 | unixctl_command_reply(conn, ds_cstr(&ds)); | |
2689 | } | |
fceef209 DDP |
2690 | |
2691 | ds_destroy(&ds); | |
2692 | } | |
2693 | ||
2694 | void | |
2695 | dpctl_unixctl_register(void) | |
2696 | { | |
2697 | const struct dpctl_command *p; | |
2698 | ||
2699 | for (p = all_commands; p->name != NULL; p++) { | |
337c4528 BP |
2700 | if (strcmp(p->name, "help")) { |
2701 | char *cmd_name = xasprintf("dpctl/%s", p->name); | |
c0297846 | 2702 | unixctl_command_register(cmd_name, |
2703 | p->usage, | |
2704 | p->min_args, | |
2705 | p->max_args, | |
2706 | dpctl_unixctl_handler, | |
2707 | p->handler); | |
337c4528 BP |
2708 | free(cmd_name); |
2709 | } | |
fceef209 DDP |
2710 | } |
2711 | } |