]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-dpctl.c
ovs-dpctl: Parse the arguments correctly for del-flow.
[ovs.git] / utilities / ovs-dpctl.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32
33 #include "command-line.h"
34 #include "compiler.h"
35 #include "dirs.h"
36 #include "dpif.h"
37 #include "dynamic-string.h"
38 #include "flow.h"
39 #include "netdev.h"
40 #include "netlink.h"
41 #include "odp-util.h"
42 #include "ofpbuf.h"
43 #include "packets.h"
44 #include "shash.h"
45 #include "simap.h"
46 #include "smap.h"
47 #include "sset.h"
48 #include "timeval.h"
49 #include "util.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(dpctl);
53
54 /* -s, --statistics: Print port/flow statistics? */
55 static bool print_statistics;
56
57 /* --clear: Reset existing statistics to zero when modifying a flow? */
58 static bool zero_statistics;
59
60 /* --may-create: Allow mod-flows command to create a new flow? */
61 static bool may_create;
62
63 /* -m, --more: Increase output verbosity. */
64 static int verbosity;
65
66 static const struct command *get_all_commands(void);
67
68 static void usage(void) NO_RETURN;
69 static void parse_options(int argc, char *argv[]);
70
71 int
72 main(int argc, char *argv[])
73 {
74 set_program_name(argv[0]);
75 parse_options(argc, argv);
76 signal(SIGPIPE, SIG_IGN);
77 run_command(argc - optind, argv + optind, get_all_commands());
78 return 0;
79 }
80
81 static void
82 parse_options(int argc, char *argv[])
83 {
84 enum {
85 OPT_CLEAR = UCHAR_MAX + 1,
86 OPT_MAY_CREATE,
87 VLOG_OPTION_ENUMS
88 };
89 static const struct option long_options[] = {
90 {"statistics", no_argument, NULL, 's'},
91 {"clear", no_argument, NULL, OPT_CLEAR},
92 {"may-create", no_argument, NULL, OPT_MAY_CREATE},
93 {"more", no_argument, NULL, 'm'},
94 {"timeout", required_argument, NULL, 't'},
95 {"help", no_argument, NULL, 'h'},
96 {"version", no_argument, NULL, 'V'},
97 VLOG_LONG_OPTIONS,
98 {NULL, 0, NULL, 0},
99 };
100 char *short_options = long_options_to_short_options(long_options);
101
102 for (;;) {
103 unsigned long int timeout;
104 int c;
105
106 c = getopt_long(argc, argv, short_options, long_options, NULL);
107 if (c == -1) {
108 break;
109 }
110
111 switch (c) {
112 case 's':
113 print_statistics = true;
114 break;
115
116 case OPT_CLEAR:
117 zero_statistics = true;
118 break;
119
120 case OPT_MAY_CREATE:
121 may_create = true;
122 break;
123
124 case 'm':
125 verbosity++;
126 break;
127
128 case 't':
129 timeout = strtoul(optarg, NULL, 10);
130 if (timeout <= 0) {
131 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
132 optarg);
133 } else {
134 time_alarm(timeout);
135 }
136 break;
137
138 case 'h':
139 usage();
140
141 case 'V':
142 ovs_print_version(0, 0);
143 exit(EXIT_SUCCESS);
144
145 VLOG_OPTION_HANDLERS
146
147 case '?':
148 exit(EXIT_FAILURE);
149
150 default:
151 abort();
152 }
153 }
154 free(short_options);
155 }
156
157 static void
158 usage(void)
159 {
160 printf("%s: Open vSwitch datapath management utility\n"
161 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
162 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
163 " del-dp DP delete local datapath DP\n"
164 " add-if DP IFACE... add each IFACE as a port on DP\n"
165 " set-if DP IFACE... reconfigure each IFACE within DP\n"
166 " del-if DP IFACE... delete each IFACE from DP\n"
167 " dump-dps display names of all datapaths\n"
168 " show show basic info on all datapaths\n"
169 " show DP... show basic info on each DP\n"
170 " dump-flows DP display flows in DP\n"
171 " add-flow DP FLOW ACTIONS add FLOW with ACTIONS to DP\n"
172 " mod-flow DP FLOW ACTIONS change FLOW actions to ACTIONS in DP\n"
173 " del-flow DP FLOW delete FLOW from DP\n"
174 " del-flows DP delete all flows from DP\n"
175 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
176 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
177 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
178 program_name, program_name);
179 vlog_usage();
180 printf("\nOptions for show and mod-flow:\n"
181 " -s, --statistics print statistics for port or flow\n"
182 "\nOptions for dump-flows:\n"
183 " -m, --more increase verbosity of output\n"
184 "\nOptions for mod-flow:\n"
185 " --may-create create flow if it doesn't exist\n"
186 " --clear reset existing stats to zero\n"
187 "\nOther options:\n"
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n"
190 " -V, --version display version information\n");
191 exit(EXIT_SUCCESS);
192 }
193
194 static void run(int retval, const char *message, ...)
195 PRINTF_FORMAT(2, 3);
196
197 static void run(int retval, const char *message, ...)
198 {
199 if (retval) {
200 va_list args;
201
202 va_start(args, message);
203 ovs_fatal_valist(retval, message, args);
204 }
205 }
206 \f
207 static void dpctl_add_if(int argc, char *argv[]);
208
209 static int if_up(const char *netdev_name)
210 {
211 struct netdev *netdev;
212 int retval;
213
214 retval = netdev_open(netdev_name, "system", &netdev);
215 if (!retval) {
216 retval = netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
217 netdev_close(netdev);
218 }
219 return retval;
220 }
221
222 /* Retrieve the name of the datapath if exactly one exists. The caller
223 * is responsible for freeing the returned string. If there is not one
224 * datapath, aborts with an error message. */
225 static char *
226 get_one_dp(void)
227 {
228 struct sset types;
229 const char *type;
230 char *dp_name = NULL;
231 size_t count = 0;
232
233 sset_init(&types);
234 dp_enumerate_types(&types);
235 SSET_FOR_EACH (type, &types) {
236 struct sset names;
237
238 sset_init(&names);
239 if (!dp_enumerate_names(type, &names)) {
240 count += sset_count(&names);
241 if (!dp_name && count == 1) {
242 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
243 }
244 }
245 sset_destroy(&names);
246 }
247 sset_destroy(&types);
248
249 if (!count) {
250 ovs_fatal(0, "no datapaths exist");
251 } else if (count > 1) {
252 ovs_fatal(0, "multiple datapaths, specify one");
253 }
254
255 return dp_name;
256 }
257
258 static int
259 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
260 {
261 int result;
262 char *name, *type;
263
264 dp_parse_name(arg_, &name, &type);
265
266 if (create) {
267 result = dpif_create(name, type, dpifp);
268 } else {
269 result = dpif_open(name, type, dpifp);
270 }
271
272 free(name);
273 free(type);
274 return result;
275 }
276
277 static void
278 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
279 {
280 struct dpif *dpif;
281 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
282 dpif_close(dpif);
283 if (argc > 2) {
284 dpctl_add_if(argc, argv);
285 }
286 }
287
288 static void
289 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
290 {
291 struct dpif *dpif;
292 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
293 run(dpif_delete(dpif), "del_dp");
294 dpif_close(dpif);
295 }
296
297 static void
298 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
299 {
300 bool failure = false;
301 struct dpif *dpif;
302 int i;
303
304 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
305 for (i = 2; i < argc; i++) {
306 const char *name, *type;
307 char *save_ptr = NULL;
308 struct netdev *netdev = NULL;
309 struct smap args;
310 odp_port_t port_no = ODPP_NONE;
311 char *option;
312 int error;
313
314 name = strtok_r(argv[i], ",", &save_ptr);
315 type = "system";
316
317 if (!name) {
318 ovs_error(0, "%s is not a valid network device name", argv[i]);
319 failure = true;
320 continue;
321 }
322
323 smap_init(&args);
324 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
325 char *save_ptr_2 = NULL;
326 char *key, *value;
327
328 key = strtok_r(option, "=", &save_ptr_2);
329 value = strtok_r(NULL, "", &save_ptr_2);
330 if (!value) {
331 value = "";
332 }
333
334 if (!strcmp(key, "type")) {
335 type = value;
336 } else if (!strcmp(key, "port_no")) {
337 port_no = u32_to_odp(atoi(value));
338 } else if (!smap_add_once(&args, key, value)) {
339 ovs_error(0, "duplicate \"%s\" option", key);
340 }
341 }
342
343 error = netdev_open(name, type, &netdev);
344 if (error) {
345 ovs_error(error, "%s: failed to open network device", name);
346 goto next;
347 }
348
349 error = netdev_set_config(netdev, &args);
350 if (error) {
351 ovs_error(error, "%s: failed to configure network device", name);
352 goto next;
353 }
354
355 error = dpif_port_add(dpif, netdev, &port_no);
356 if (error) {
357 ovs_error(error, "adding %s to %s failed", name, argv[1]);
358 goto next;
359 }
360
361 error = if_up(name);
362
363 next:
364 netdev_close(netdev);
365 if (error) {
366 failure = true;
367 }
368 }
369 dpif_close(dpif);
370 if (failure) {
371 exit(EXIT_FAILURE);
372 }
373 }
374
375 static void
376 dpctl_set_if(int argc, char *argv[])
377 {
378 bool failure = false;
379 struct dpif *dpif;
380 int i;
381
382 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
383 for (i = 2; i < argc; i++) {
384 struct netdev *netdev = NULL;
385 struct dpif_port dpif_port;
386 char *save_ptr = NULL;
387 char *type = NULL;
388 const char *name;
389 struct smap args;
390 odp_port_t port_no;
391 char *option;
392 int error;
393
394 name = strtok_r(argv[i], ",", &save_ptr);
395 if (!name) {
396 ovs_error(0, "%s is not a valid network device name", argv[i]);
397 failure = true;
398 continue;
399 }
400
401 /* Get the port's type from the datapath. */
402 error = dpif_port_query_by_name(dpif, name, &dpif_port);
403 if (error) {
404 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
405 goto next;
406 }
407 type = xstrdup(dpif_port.type);
408 port_no = dpif_port.port_no;
409 dpif_port_destroy(&dpif_port);
410
411 /* Retrieve its existing configuration. */
412 error = netdev_open(name, type, &netdev);
413 if (error) {
414 ovs_error(error, "%s: failed to open network device", name);
415 goto next;
416 }
417
418 smap_init(&args);
419 error = netdev_get_config(netdev, &args);
420 if (error) {
421 ovs_error(error, "%s: failed to fetch configuration", name);
422 goto next;
423 }
424
425 /* Parse changes to configuration. */
426 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
427 char *save_ptr_2 = NULL;
428 char *key, *value;
429
430 key = strtok_r(option, "=", &save_ptr_2);
431 value = strtok_r(NULL, "", &save_ptr_2);
432 if (!value) {
433 value = "";
434 }
435
436 if (!strcmp(key, "type")) {
437 if (strcmp(value, type)) {
438 ovs_error(0, "%s: can't change type from %s to %s",
439 name, type, value);
440 failure = true;
441 }
442 } else if (!strcmp(key, "port_no")) {
443 if (port_no != u32_to_odp(atoi(value))) {
444 ovs_error(0, "%s: can't change port number from "
445 "%"PRIu32" to %d",
446 name, port_no, atoi(value));
447 failure = true;
448 }
449 } else if (value[0] == '\0') {
450 smap_remove(&args, key);
451 } else {
452 smap_replace(&args, key, value);
453 }
454 }
455
456 /* Update configuration. */
457 error = netdev_set_config(netdev, &args);
458 smap_destroy(&args);
459 if (error) {
460 ovs_error(error, "%s: failed to configure network device", name);
461 goto next;
462 }
463
464 next:
465 free(type);
466 netdev_close(netdev);
467 if (error) {
468 failure = true;
469 }
470 }
471 dpif_close(dpif);
472 if (failure) {
473 exit(EXIT_FAILURE);
474 }
475 }
476
477 static bool
478 get_port_number(struct dpif *dpif, const char *name, odp_port_t *port)
479 {
480 struct dpif_port dpif_port;
481
482 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
483 *port = dpif_port.port_no;
484 dpif_port_destroy(&dpif_port);
485 return true;
486 } else {
487 ovs_error(0, "no port named %s", name);
488 return false;
489 }
490 }
491
492 static void
493 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
494 {
495 bool failure = false;
496 struct dpif *dpif;
497 int i;
498
499 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
500 for (i = 2; i < argc; i++) {
501 const char *name = argv[i];
502 odp_port_t port;
503 int error;
504
505 if (!name[strspn(name, "0123456789")]) {
506 port = u32_to_odp(atoi(name));
507 } else if (!get_port_number(dpif, name, &port)) {
508 failure = true;
509 continue;
510 }
511
512 error = dpif_port_del(dpif, port);
513 if (error) {
514 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
515 failure = true;
516 }
517 }
518 dpif_close(dpif);
519 if (failure) {
520 exit(EXIT_FAILURE);
521 }
522 }
523
524 static void
525 print_stat(const char *leader, uint64_t value)
526 {
527 fputs(leader, stdout);
528 if (value != UINT64_MAX) {
529 printf("%"PRIu64, value);
530 } else {
531 putchar('?');
532 }
533 }
534
535 static void
536 print_human_size(uint64_t value)
537 {
538 if (value == UINT64_MAX) {
539 /* Nothing to do. */
540 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
541 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
542 } else if (value >= 1024ULL * 1024 * 1024) {
543 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
544 } else if (value >= 1024ULL * 1024) {
545 printf(" (%.1f MiB)", value / (1024.0 * 1024));
546 } else if (value >= 1024) {
547 printf(" (%.1f KiB)", value / 1024.0);
548 }
549 }
550
551 static void
552 show_dpif(struct dpif *dpif)
553 {
554 struct dpif_port_dump dump;
555 struct dpif_port dpif_port;
556 struct dpif_dp_stats stats;
557 struct netdev *netdev;
558
559 printf("%s:\n", dpif_name(dpif));
560 if (!dpif_get_dp_stats(dpif, &stats)) {
561 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
562 "\tflows: %"PRIu64"\n",
563 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
564 }
565 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
566 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
567
568 if (strcmp(dpif_port.type, "system")) {
569 int error;
570
571 printf (" (%s", dpif_port.type);
572
573 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
574 if (!error) {
575 struct smap config;
576
577 smap_init(&config);
578 error = netdev_get_config(netdev, &config);
579 if (!error) {
580 const struct smap_node **nodes;
581 size_t i;
582
583 nodes = smap_sort(&config);
584 for (i = 0; i < smap_count(&config); i++) {
585 const struct smap_node *node = nodes[i];
586 printf("%c %s=%s", i ? ',' : ':', node->key,
587 node->value);
588 }
589 free(nodes);
590 } else {
591 printf(", could not retrieve configuration (%s)",
592 ovs_strerror(error));
593 }
594 smap_destroy(&config);
595
596 netdev_close(netdev);
597 } else {
598 printf(": open failed (%s)", ovs_strerror(error));
599 }
600 putchar(')');
601 }
602 putchar('\n');
603
604 if (print_statistics) {
605 struct netdev_stats s;
606 int error;
607
608 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
609 if (error) {
610 printf(", open failed (%s)", ovs_strerror(error));
611 continue;
612 }
613 error = netdev_get_stats(netdev, &s);
614 if (error) {
615 printf(", could not retrieve stats (%s)", ovs_strerror(error));
616 continue;
617 }
618
619 netdev_close(netdev);
620 print_stat("\t\tRX packets:", s.rx_packets);
621 print_stat(" errors:", s.rx_errors);
622 print_stat(" dropped:", s.rx_dropped);
623 print_stat(" overruns:", s.rx_over_errors);
624 print_stat(" frame:", s.rx_frame_errors);
625 printf("\n");
626
627 print_stat("\t\tTX packets:", s.tx_packets);
628 print_stat(" errors:", s.tx_errors);
629 print_stat(" dropped:", s.tx_dropped);
630 print_stat(" aborted:", s.tx_aborted_errors);
631 print_stat(" carrier:", s.tx_carrier_errors);
632 printf("\n");
633
634 print_stat("\t\tcollisions:", s.collisions);
635 printf("\n");
636
637 print_stat("\t\tRX bytes:", s.rx_bytes);
638 print_human_size(s.rx_bytes);
639 print_stat(" TX bytes:", s.tx_bytes);
640 print_human_size(s.tx_bytes);
641 printf("\n");
642 }
643 }
644 dpif_close(dpif);
645 }
646
647 static void
648 dpctl_show(int argc, char *argv[])
649 {
650 bool failure = false;
651 if (argc > 1) {
652 int i;
653 for (i = 1; i < argc; i++) {
654 const char *name = argv[i];
655 struct dpif *dpif;
656 int error;
657
658 error = parsed_dpif_open(name, false, &dpif);
659 if (!error) {
660 show_dpif(dpif);
661 } else {
662 ovs_error(error, "opening datapath %s failed", name);
663 failure = true;
664 }
665 }
666 } else {
667 struct sset types;
668 const char *type;
669
670 sset_init(&types);
671 dp_enumerate_types(&types);
672 SSET_FOR_EACH (type, &types) {
673 struct sset names;
674 const char *name;
675
676 sset_init(&names);
677 if (dp_enumerate_names(type, &names)) {
678 failure = true;
679 continue;
680 }
681 SSET_FOR_EACH (name, &names) {
682 struct dpif *dpif;
683 int error;
684
685 error = dpif_open(name, type, &dpif);
686 if (!error) {
687 show_dpif(dpif);
688 } else {
689 ovs_error(error, "opening datapath %s failed", name);
690 failure = true;
691 }
692 }
693 sset_destroy(&names);
694 }
695 sset_destroy(&types);
696 }
697 if (failure) {
698 exit(EXIT_FAILURE);
699 }
700 }
701
702 static void
703 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
704 {
705 struct sset dpif_names, dpif_types;
706 const char *type;
707 int error = 0;
708
709 sset_init(&dpif_names);
710 sset_init(&dpif_types);
711 dp_enumerate_types(&dpif_types);
712
713 SSET_FOR_EACH (type, &dpif_types) {
714 const char *name;
715 int retval;
716
717 retval = dp_enumerate_names(type, &dpif_names);
718 if (retval) {
719 error = retval;
720 }
721
722 SSET_FOR_EACH (name, &dpif_names) {
723 struct dpif *dpif;
724 if (!dpif_open(name, type, &dpif)) {
725 printf("%s\n", dpif_name(dpif));
726 dpif_close(dpif);
727 }
728 }
729 }
730
731 sset_destroy(&dpif_names);
732 sset_destroy(&dpif_types);
733 if (error) {
734 exit(EXIT_FAILURE);
735 }
736 }
737
738 static void
739 dpctl_dump_flows(int argc, char *argv[])
740 {
741 const struct dpif_flow_stats *stats;
742 const struct nlattr *actions;
743 struct dpif_flow_dump dump;
744 const struct nlattr *key;
745 const struct nlattr *mask;
746 size_t actions_len;
747 struct dpif *dpif;
748 size_t key_len;
749 size_t mask_len;
750 struct ds ds;
751 char *name;
752
753 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
754 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
755 free(name);
756
757 ds_init(&ds);
758 dpif_flow_dump_start(&dump, dpif);
759 while (dpif_flow_dump_next(&dump, &key, &key_len,
760 &mask, &mask_len,
761 &actions, &actions_len, &stats)) {
762 ds_clear(&ds);
763 odp_flow_format(key, key_len, mask, mask_len, &ds, verbosity);
764 ds_put_cstr(&ds, ", ");
765
766 dpif_flow_stats_format(stats, &ds);
767 ds_put_cstr(&ds, ", actions:");
768 format_odp_actions(&ds, actions, actions_len);
769 printf("%s\n", ds_cstr(&ds));
770 }
771 dpif_flow_dump_done(&dump);
772 ds_destroy(&ds);
773 dpif_close(dpif);
774 }
775
776 static void
777 dpctl_put_flow(int argc, char *argv[], enum dpif_flow_put_flags flags)
778 {
779 const char *key_s = argv[argc - 2];
780 const char *actions_s = argv[argc - 1];
781 struct dpif_flow_stats stats;
782 struct ofpbuf actions;
783 struct ofpbuf key;
784 struct ofpbuf mask;
785 struct dpif *dpif;
786 struct ds s;
787 char *dp_name;
788
789 ds_init(&s);
790 ofpbuf_init(&key, 0);
791 ofpbuf_init(&mask, 0);
792 run(odp_flow_from_string(key_s, NULL, &key, &mask), "parsing flow key");
793
794 ofpbuf_init(&actions, 0);
795 run(odp_actions_from_string(actions_s, NULL, &actions), "parsing actions");
796
797 dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp();
798 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
799 free(dp_name);
800
801 run(dpif_flow_put(dpif, flags,
802 key.data, key.size,
803 mask.size == 0 ? NULL : mask.data, mask.size,
804 actions.data, actions.size,
805 print_statistics ? &stats : NULL),
806 "updating flow table");
807
808 ofpbuf_uninit(&key);
809 ofpbuf_uninit(&mask);
810 ofpbuf_uninit(&actions);
811
812 if (print_statistics) {
813 struct ds s;
814
815 ds_init(&s);
816 dpif_flow_stats_format(&stats, &s);
817 puts(ds_cstr(&s));
818 ds_destroy(&s);
819 }
820 }
821
822 static void
823 dpctl_add_flow(int argc, char *argv[])
824 {
825 dpctl_put_flow(argc, argv, DPIF_FP_CREATE);
826 }
827
828 static void
829 dpctl_mod_flow(int argc OVS_UNUSED, char *argv[])
830 {
831 enum dpif_flow_put_flags flags;
832
833 flags = DPIF_FP_MODIFY;
834 if (may_create) {
835 flags |= DPIF_FP_CREATE;
836 }
837 if (zero_statistics) {
838 flags |= DPIF_FP_ZERO_STATS;
839 }
840
841 dpctl_put_flow(argc, argv, flags);
842 }
843
844 static void
845 dpctl_del_flow(int argc, char *argv[])
846 {
847 const char *key_s = argv[argc - 1];
848 struct dpif_flow_stats stats;
849 struct ofpbuf key;
850 struct ofpbuf mask; /* To be ignored. */
851 struct dpif *dpif;
852 char *dp_name;
853
854 ofpbuf_init(&key, 0);
855 ofpbuf_init(&mask, 0);
856 run(odp_flow_from_string(key_s, NULL, &key, &mask), "parsing flow key");
857
858 dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp();
859 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
860 free(dp_name);
861
862 run(dpif_flow_del(dpif,
863 key.data, key.size,
864 print_statistics ? &stats : NULL), "deleting flow");
865
866 ofpbuf_uninit(&key);
867 ofpbuf_uninit(&mask);
868
869 if (print_statistics) {
870 struct ds s;
871
872 ds_init(&s);
873 dpif_flow_stats_format(&stats, &s);
874 puts(ds_cstr(&s));
875 ds_destroy(&s);
876 }
877 }
878
879 static void
880 dpctl_del_flows(int argc, char *argv[])
881 {
882 struct dpif *dpif;
883 char *name;
884
885 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
886 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
887 free(name);
888
889 run(dpif_flow_flush(dpif), "deleting all flows");
890 dpif_close(dpif);
891 }
892
893 static void
894 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
895 {
896 usage();
897 }
898 \f
899 /* Undocumented commands for unit testing. */
900
901 static void
902 dpctl_parse_actions(int argc, char *argv[])
903 {
904 int i;
905
906 for (i = 1; i < argc; i++) {
907 struct ofpbuf actions;
908 struct ds s;
909
910 ofpbuf_init(&actions, 0);
911 run(odp_actions_from_string(argv[i], NULL, &actions),
912 "odp_actions_from_string");
913
914 ds_init(&s);
915 format_odp_actions(&s, actions.data, actions.size);
916 puts(ds_cstr(&s));
917 ds_destroy(&s);
918
919 ofpbuf_uninit(&actions);
920 }
921 }
922
923 struct actions_for_flow {
924 struct hmap_node hmap_node;
925 struct flow flow;
926 struct ofpbuf actions;
927 };
928
929 static struct actions_for_flow *
930 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
931 {
932 uint32_t hash = flow_hash(flow, 0);
933 struct actions_for_flow *af;
934
935 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
936 if (flow_equal(&af->flow, flow)) {
937 return af;
938 }
939 }
940
941 af = xmalloc(sizeof *af);
942 af->flow = *flow;
943 ofpbuf_init(&af->actions, 0);
944 hmap_insert(actions_per_flow, &af->hmap_node, hash);
945 return af;
946 }
947
948 static int
949 compare_actions_for_flow(const void *a_, const void *b_)
950 {
951 struct actions_for_flow *const *a = a_;
952 struct actions_for_flow *const *b = b_;
953
954 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
955 }
956
957 static int
958 compare_output_actions(const void *a_, const void *b_)
959 {
960 const struct nlattr *a = a_;
961 const struct nlattr *b = b_;
962 uint32_t a_port = nl_attr_get_u32(a);
963 uint32_t b_port = nl_attr_get_u32(b);
964
965 return a_port < b_port ? -1 : a_port > b_port;
966 }
967
968 static void
969 sort_output_actions__(struct nlattr *first, struct nlattr *end)
970 {
971 size_t bytes = (uint8_t *) end - (uint8_t *) first;
972 size_t n = bytes / NL_A_U32_SIZE;
973
974 ovs_assert(bytes % NL_A_U32_SIZE == 0);
975 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
976 }
977
978 static void
979 sort_output_actions(struct nlattr *actions, size_t length)
980 {
981 struct nlattr *first_output = NULL;
982 struct nlattr *a;
983 int left;
984
985 NL_ATTR_FOR_EACH (a, left, actions, length) {
986 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
987 if (!first_output) {
988 first_output = a;
989 }
990 } else {
991 if (first_output) {
992 sort_output_actions__(first_output, a);
993 first_output = NULL;
994 }
995 }
996 }
997 if (first_output) {
998 uint8_t *end = (uint8_t *) actions + length;
999 sort_output_actions__(first_output,
1000 ALIGNED_CAST(struct nlattr *, end));
1001 }
1002 }
1003
1004 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1005 * have the syntax used by "ovs-dpctl dump-flows".
1006 *
1007 * This command prints ACTIONS in a format that shows what happens for each
1008 * VLAN, independent of the order of the ACTIONS. For example, there is more
1009 * than one way to output a packet on VLANs 9 and 11, but this command will
1010 * print the same output for any form.
1011 *
1012 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1013 * so far the implementation only covers VLANs. */
1014 static void
1015 dpctl_normalize_actions(int argc, char *argv[])
1016 {
1017 struct simap port_names;
1018 struct ofpbuf keybuf;
1019 struct flow flow;
1020 struct ofpbuf odp_actions;
1021 struct hmap actions_per_flow;
1022 struct actions_for_flow **afs;
1023 struct actions_for_flow *af;
1024 struct nlattr *a;
1025 size_t n_afs;
1026 struct ds s;
1027 int left;
1028 int i;
1029
1030 ds_init(&s);
1031
1032 simap_init(&port_names);
1033 for (i = 3; i < argc; i++) {
1034 char name[16];
1035 int number;
1036 int n = -1;
1037
1038 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
1039 uintptr_t n = number;
1040 simap_put(&port_names, name, n);
1041 } else {
1042 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
1043 }
1044 }
1045
1046 /* Parse flow key. */
1047 ofpbuf_init(&keybuf, 0);
1048 run(odp_flow_from_string(argv[1], &port_names, &keybuf, NULL),
1049 "odp_flow_key_from_string");
1050
1051 ds_clear(&s);
1052 odp_flow_format(keybuf.data, keybuf.size, NULL, 0, &s, verbosity);
1053 printf("input flow: %s\n", ds_cstr(&s));
1054
1055 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
1056 "odp_flow_key_to_flow");
1057 ofpbuf_uninit(&keybuf);
1058
1059 /* Parse actions. */
1060 ofpbuf_init(&odp_actions, 0);
1061 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
1062 "odp_actions_from_string");
1063 simap_destroy(&port_names);
1064
1065 if (verbosity) {
1066 ds_clear(&s);
1067 format_odp_actions(&s, odp_actions.data, odp_actions.size);
1068 printf("input actions: %s\n", ds_cstr(&s));
1069 }
1070
1071 hmap_init(&actions_per_flow);
1072 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1073 const struct ovs_action_push_vlan *push;
1074 switch(nl_attr_type(a)) {
1075 case OVS_ACTION_ATTR_POP_VLAN:
1076 flow.vlan_tci = htons(0);
1077 continue;
1078
1079 case OVS_ACTION_ATTR_PUSH_VLAN:
1080 push = nl_attr_get_unspec(a, sizeof *push);
1081 flow.vlan_tci = push->vlan_tci;
1082 continue;
1083 }
1084
1085 af = get_actions_for_flow(&actions_per_flow, &flow);
1086 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1087 nl_attr_get(a), nl_attr_get_size(a));
1088 }
1089
1090 n_afs = hmap_count(&actions_per_flow);
1091 afs = xmalloc(n_afs * sizeof *afs);
1092 i = 0;
1093 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1094 afs[i++] = af;
1095 }
1096 ovs_assert(i == n_afs);
1097
1098 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1099
1100 for (i = 0; i < n_afs; i++) {
1101 const struct actions_for_flow *af = afs[i];
1102
1103 sort_output_actions(af->actions.data, af->actions.size);
1104
1105 if (af->flow.vlan_tci != htons(0)) {
1106 printf("vlan(vid=%"PRIu16",pcp=%d): ",
1107 vlan_tci_to_vid(af->flow.vlan_tci),
1108 vlan_tci_to_pcp(af->flow.vlan_tci));
1109 } else {
1110 printf("no vlan: ");
1111 }
1112
1113 if (af->flow.mpls_depth) {
1114 printf("mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1115 mpls_lse_to_label(af->flow.mpls_lse),
1116 mpls_lse_to_tc(af->flow.mpls_lse),
1117 mpls_lse_to_ttl(af->flow.mpls_lse));
1118 } else {
1119 printf("no mpls: ");
1120 }
1121
1122 ds_clear(&s);
1123 format_odp_actions(&s, af->actions.data, af->actions.size);
1124 puts(ds_cstr(&s));
1125 }
1126 ds_destroy(&s);
1127 }
1128
1129 static const struct command all_commands[] = {
1130 { "add-dp", 1, INT_MAX, dpctl_add_dp },
1131 { "del-dp", 1, 1, dpctl_del_dp },
1132 { "add-if", 2, INT_MAX, dpctl_add_if },
1133 { "del-if", 2, INT_MAX, dpctl_del_if },
1134 { "set-if", 2, INT_MAX, dpctl_set_if },
1135 { "dump-dps", 0, 0, dpctl_dump_dps },
1136 { "show", 0, INT_MAX, dpctl_show },
1137 { "dump-flows", 0, 1, dpctl_dump_flows },
1138 { "add-flow", 2, 3, dpctl_add_flow },
1139 { "mod-flow", 2, 3, dpctl_mod_flow },
1140 { "del-flow", 1, 2, dpctl_del_flow },
1141 { "del-flows", 0, 1, dpctl_del_flows },
1142 { "help", 0, INT_MAX, dpctl_help },
1143
1144 /* Undocumented commands for testing. */
1145 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1146 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1147
1148 { NULL, 0, 0, NULL },
1149 };
1150
1151 static const struct command *get_all_commands(void)
1152 {
1153 return all_commands;
1154 }