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