2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
33 #include "command-line.h"
37 #include "dynamic-string.h"
38 #include "fatal-signal.h"
44 #include "ofp-parse.h"
55 /* -s, --statistics: Print port/flow statistics? */
56 static bool print_statistics;
58 /* --clear: Reset existing statistics to zero when modifying a flow? */
59 static bool zero_statistics;
61 /* --may-create: Allow mod-flows command to create a new flow? */
62 static bool may_create;
64 /* -m, --more: Increase output verbosity. */
67 static const struct command *get_all_commands(void);
69 static void usage(void) NO_RETURN;
70 static void parse_options(int argc, char *argv[]);
73 main(int argc, char *argv[])
75 set_program_name(argv[0]);
76 parse_options(argc, argv);
77 fatal_ignore_sigpipe();
78 run_command(argc - optind, argv + optind, get_all_commands());
83 parse_options(int argc, char *argv[])
86 OPT_CLEAR = UCHAR_MAX + 1,
90 static const struct option long_options[] = {
91 {"statistics", no_argument, NULL, 's'},
92 {"clear", no_argument, NULL, OPT_CLEAR},
93 {"may-create", no_argument, NULL, OPT_MAY_CREATE},
94 {"more", no_argument, NULL, 'm'},
95 {"timeout", required_argument, NULL, 't'},
96 {"help", no_argument, NULL, 'h'},
97 {"version", no_argument, NULL, 'V'},
101 char *short_options = long_options_to_short_options(long_options);
104 unsigned long int timeout;
107 c = getopt_long(argc, argv, short_options, long_options, NULL);
114 print_statistics = true;
118 zero_statistics = true;
130 timeout = strtoul(optarg, NULL, 10);
132 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
143 ovs_print_version(0, 0);
161 printf("%s: Open vSwitch datapath management utility\n"
162 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
163 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
164 " del-dp DP delete local datapath DP\n"
165 " add-if DP IFACE... add each IFACE as a port on DP\n"
166 " set-if DP IFACE... reconfigure each IFACE within DP\n"
167 " del-if DP IFACE... delete each IFACE from DP\n"
168 " dump-dps display names of all datapaths\n"
169 " show show basic info on all datapaths\n"
170 " show DP... show basic info on each DP\n"
171 " dump-flows [DP] display flows in DP\n"
172 " add-flow [DP] FLOW ACTIONS add FLOW with ACTIONS to DP\n"
173 " mod-flow [DP] FLOW ACTIONS change FLOW actions to ACTIONS in DP\n"
174 " del-flow [DP] FLOW delete FLOW from DP\n"
175 " del-flows [DP] delete all flows from DP\n"
176 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
177 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
178 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n"
179 "For COMMAND dump-flows, add-flow, mod-flow, del-flow and\n"
180 "del-flows, DP is optional if there is only one datapath.\n",
181 program_name, program_name);
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"
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");
197 static void run(int retval, const char *message, ...)
200 static void run(int retval, const char *message, ...)
205 va_start(args, message);
206 ovs_fatal_valist(retval, message, args);
210 static void dpctl_add_if(int argc, char *argv[]);
212 static int if_up(const char *netdev_name)
214 struct netdev *netdev;
217 retval = netdev_open(netdev_name, "system", &netdev);
219 retval = netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
220 netdev_close(netdev);
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. */
233 char *dp_name = NULL;
237 dp_enumerate_types(&types);
238 SSET_FOR_EACH (type, &types) {
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));
248 sset_destroy(&names);
250 sset_destroy(&types);
253 ovs_fatal(0, "no datapaths exist");
254 } else if (count > 1) {
255 ovs_fatal(0, "multiple datapaths, specify one");
262 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
267 dp_parse_name(arg_, &name, &type);
270 result = dpif_create(name, type, dpifp);
272 result = dpif_open(name, type, dpifp);
281 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
284 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
287 dpctl_add_if(argc, argv);
292 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
295 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
296 run(dpif_delete(dpif), "del_dp");
301 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
303 bool failure = false;
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;
313 odp_port_t port_no = ODPP_NONE;
317 name = strtok_r(argv[i], ",", &save_ptr);
321 ovs_error(0, "%s is not a valid network device name", argv[i]);
327 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
328 char *save_ptr_2 = NULL;
331 key = strtok_r(option, "=", &save_ptr_2);
332 value = strtok_r(NULL, "", &save_ptr_2);
337 if (!strcmp(key, "type")) {
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);
346 error = netdev_open(name, type, &netdev);
348 ovs_error(error, "%s: failed to open network device", name);
352 error = netdev_set_config(netdev, &args);
357 error = dpif_port_add(dpif, netdev, &port_no);
359 ovs_error(error, "adding %s to %s failed", name, argv[1]);
366 netdev_close(netdev);
378 dpctl_set_if(int argc, char *argv[])
380 bool failure = false;
384 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
385 for (i = 2; i < argc; i++) {
386 struct netdev *netdev = NULL;
387 struct dpif_port dpif_port;
388 char *save_ptr = NULL;
396 name = strtok_r(argv[i], ",", &save_ptr);
398 ovs_error(0, "%s is not a valid network device name", argv[i]);
403 /* Get the port's type from the datapath. */
404 error = dpif_port_query_by_name(dpif, name, &dpif_port);
406 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
409 type = xstrdup(dpif_port.type);
410 port_no = dpif_port.port_no;
411 dpif_port_destroy(&dpif_port);
413 /* Retrieve its existing configuration. */
414 error = netdev_open(name, type, &netdev);
416 ovs_error(error, "%s: failed to open network device", name);
421 error = netdev_get_config(netdev, &args);
423 ovs_error(error, "%s: failed to fetch configuration", name);
427 /* Parse changes to configuration. */
428 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
429 char *save_ptr_2 = NULL;
432 key = strtok_r(option, "=", &save_ptr_2);
433 value = strtok_r(NULL, "", &save_ptr_2);
438 if (!strcmp(key, "type")) {
439 if (strcmp(value, type)) {
440 ovs_error(0, "%s: can't change type from %s to %s",
444 } else if (!strcmp(key, "port_no")) {
445 if (port_no != u32_to_odp(atoi(value))) {
446 ovs_error(0, "%s: can't change port number from "
448 name, port_no, atoi(value));
451 } else if (value[0] == '\0') {
452 smap_remove(&args, key);
454 smap_replace(&args, key, value);
458 /* Update configuration. */
459 error = netdev_set_config(netdev, &args);
467 netdev_close(netdev);
479 get_port_number(struct dpif *dpif, const char *name, odp_port_t *port)
481 struct dpif_port dpif_port;
483 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
484 *port = dpif_port.port_no;
485 dpif_port_destroy(&dpif_port);
488 ovs_error(0, "no port named %s", name);
494 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
496 bool failure = false;
500 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
501 for (i = 2; i < argc; i++) {
502 const char *name = argv[i];
506 if (!name[strspn(name, "0123456789")]) {
507 port = u32_to_odp(atoi(name));
508 } else if (!get_port_number(dpif, name, &port)) {
513 error = dpif_port_del(dpif, port);
515 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
526 print_stat(const char *leader, uint64_t value)
528 fputs(leader, stdout);
529 if (value != UINT64_MAX) {
530 printf("%"PRIu64, value);
537 print_human_size(uint64_t value)
539 if (value == UINT64_MAX) {
541 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
542 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
543 } else if (value >= 1024ULL * 1024 * 1024) {
544 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
545 } else if (value >= 1024ULL * 1024) {
546 printf(" (%.1f MiB)", value / (1024.0 * 1024));
547 } else if (value >= 1024) {
548 printf(" (%.1f KiB)", value / 1024.0);
553 show_dpif(struct dpif *dpif)
555 struct dpif_port_dump dump;
556 struct dpif_port dpif_port;
557 struct dpif_dp_stats stats;
558 struct netdev *netdev;
560 printf("%s:\n", dpif_name(dpif));
561 if (!dpif_get_dp_stats(dpif, &stats)) {
562 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
563 "\tflows: %"PRIu64"\n",
564 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
565 if (stats.n_masks != UINT32_MAX) {
566 uint64_t n_pkts = stats.n_hit + stats.n_missed;
567 double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
569 printf("\tmasks: hit:%"PRIu64" total:%"PRIu32" hit/pkt:%.2f\n",
570 stats.n_mask_hit, stats.n_masks, avg);
574 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
575 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
577 if (strcmp(dpif_port.type, "system")) {
580 printf (" (%s", dpif_port.type);
582 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
587 error = netdev_get_config(netdev, &config);
589 const struct smap_node **nodes;
592 nodes = smap_sort(&config);
593 for (i = 0; i < smap_count(&config); i++) {
594 const struct smap_node *node = nodes[i];
595 printf("%c %s=%s", i ? ',' : ':', node->key,
600 printf(", could not retrieve configuration (%s)",
601 ovs_strerror(error));
603 smap_destroy(&config);
605 netdev_close(netdev);
607 printf(": open failed (%s)", ovs_strerror(error));
613 if (print_statistics) {
614 struct netdev_stats s;
617 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
619 printf(", open failed (%s)", ovs_strerror(error));
622 error = netdev_get_stats(netdev, &s);
624 printf(", could not retrieve stats (%s)", ovs_strerror(error));
628 netdev_close(netdev);
629 print_stat("\t\tRX packets:", s.rx_packets);
630 print_stat(" errors:", s.rx_errors);
631 print_stat(" dropped:", s.rx_dropped);
632 print_stat(" overruns:", s.rx_over_errors);
633 print_stat(" frame:", s.rx_frame_errors);
636 print_stat("\t\tTX packets:", s.tx_packets);
637 print_stat(" errors:", s.tx_errors);
638 print_stat(" dropped:", s.tx_dropped);
639 print_stat(" aborted:", s.tx_aborted_errors);
640 print_stat(" carrier:", s.tx_carrier_errors);
643 print_stat("\t\tcollisions:", s.collisions);
646 print_stat("\t\tRX bytes:", s.rx_bytes);
647 print_human_size(s.rx_bytes);
648 print_stat(" TX bytes:", s.tx_bytes);
649 print_human_size(s.tx_bytes);
657 dpctl_show(int argc, char *argv[])
659 bool failure = false;
662 for (i = 1; i < argc; i++) {
663 const char *name = argv[i];
667 error = parsed_dpif_open(name, false, &dpif);
671 ovs_error(error, "opening datapath %s failed", name);
680 dp_enumerate_types(&types);
681 SSET_FOR_EACH (type, &types) {
686 if (dp_enumerate_names(type, &names)) {
690 SSET_FOR_EACH (name, &names) {
694 error = dpif_open(name, type, &dpif);
698 ovs_error(error, "opening datapath %s failed", name);
702 sset_destroy(&names);
704 sset_destroy(&types);
712 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
714 struct sset dpif_names, dpif_types;
718 sset_init(&dpif_names);
719 sset_init(&dpif_types);
720 dp_enumerate_types(&dpif_types);
722 SSET_FOR_EACH (type, &dpif_types) {
726 retval = dp_enumerate_names(type, &dpif_names);
731 SSET_FOR_EACH (name, &dpif_names) {
733 if (!dpif_open(name, type, &dpif)) {
734 printf("%s\n", dpif_name(dpif));
740 sset_destroy(&dpif_names);
741 sset_destroy(&dpif_types);
748 dpctl_dump_flows(int argc, char *argv[])
750 const struct dpif_flow_stats *stats;
751 const struct nlattr *actions;
752 struct dpif_flow_dump flow_dump;
753 const struct nlattr *key;
754 const struct nlattr *mask;
755 struct dpif_port dpif_port;
756 struct dpif_port_dump port_dump;
757 struct hmap portno_names;
758 struct simap names_portno;
764 char *name, *filter = NULL;
765 struct flow flow_filter;
766 struct flow_wildcards wc_filter;
770 if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
771 filter = xstrdup(argv[--argc] + 7);
773 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
775 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
778 hmap_init(&portno_names);
779 simap_init(&names_portno);
780 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
781 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
782 simap_put(&names_portno, dpif_port.name,
783 odp_to_u32(dpif_port.port_no));
787 char *err = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks,
788 filter, &names_portno);
790 ovs_fatal(0, "Failed to parse filter (%s)", err);
795 error = dpif_flow_dump_start(&flow_dump, dpif);
799 dpif_flow_dump_state_init(dpif, &state);
800 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
801 &mask, &mask_len, &actions, &actions_len,
805 struct flow_wildcards wc;
806 struct match match, match_filter;
807 struct minimatch minimatch;
809 odp_flow_key_to_flow(key, key_len, &flow);
810 odp_flow_key_to_mask(mask, mask_len, &wc.masks, &flow);
811 match_init(&match, &flow, &wc);
813 match_init(&match_filter, &flow_filter, &wc);
814 match_init(&match_filter, &match_filter.flow, &wc_filter);
815 minimatch_init(&minimatch, &match_filter);
817 if (!minimatch_matches_flow(&minimatch, &match.flow)) {
818 minimatch_destroy(&minimatch);
821 minimatch_destroy(&minimatch);
824 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
826 ds_put_cstr(&ds, ", ");
828 dpif_flow_stats_format(stats, &ds);
829 ds_put_cstr(&ds, ", actions:");
830 format_odp_actions(&ds, actions, actions_len);
831 printf("%s\n", ds_cstr(&ds));
833 dpif_flow_dump_state_uninit(dpif, state);
834 error = dpif_flow_dump_done(&flow_dump);
838 ovs_fatal(error, "Failed to dump flows from datapath");
841 odp_portno_names_destroy(&portno_names);
842 hmap_destroy(&portno_names);
843 simap_destroy(&names_portno);
849 dpctl_put_flow(int argc, char *argv[], enum dpif_flow_put_flags flags)
851 const char *key_s = argv[argc - 2];
852 const char *actions_s = argv[argc - 1];
853 struct dpif_flow_stats stats;
854 struct dpif_port dpif_port;
855 struct dpif_port_dump port_dump;
856 struct ofpbuf actions;
862 struct simap port_names;
864 dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp();
865 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
869 simap_init(&port_names);
870 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
871 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
875 ofpbuf_init(&key, 0);
876 ofpbuf_init(&mask, 0);
877 run(odp_flow_from_string(key_s, &port_names, &key, &mask),
880 simap_destroy(&port_names);
882 ofpbuf_init(&actions, 0);
883 run(odp_actions_from_string(actions_s, NULL, &actions), "parsing actions");
885 run(dpif_flow_put(dpif, flags,
886 ofpbuf_data(&key), ofpbuf_size(&key),
887 ofpbuf_size(&mask) == 0 ? NULL : ofpbuf_data(&mask),
889 ofpbuf_data(&actions), ofpbuf_size(&actions),
890 print_statistics ? &stats : NULL),
891 "updating flow table");
894 ofpbuf_uninit(&mask);
895 ofpbuf_uninit(&actions);
897 if (print_statistics) {
901 dpif_flow_stats_format(&stats, &s);
908 dpctl_add_flow(int argc, char *argv[])
910 dpctl_put_flow(argc, argv, DPIF_FP_CREATE);
914 dpctl_mod_flow(int argc OVS_UNUSED, char *argv[])
916 enum dpif_flow_put_flags flags;
918 flags = DPIF_FP_MODIFY;
920 flags |= DPIF_FP_CREATE;
922 if (zero_statistics) {
923 flags |= DPIF_FP_ZERO_STATS;
926 dpctl_put_flow(argc, argv, flags);
930 dpctl_del_flow(int argc, char *argv[])
932 const char *key_s = argv[argc - 1];
933 struct dpif_flow_stats stats;
934 struct dpif_port dpif_port;
935 struct dpif_port_dump port_dump;
937 struct ofpbuf mask; /* To be ignored. */
940 struct simap port_names;
942 dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp();
943 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
946 simap_init(&port_names);
947 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
948 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
951 ofpbuf_init(&key, 0);
952 ofpbuf_init(&mask, 0);
953 run(odp_flow_from_string(key_s, &port_names, &key, &mask), "parsing flow key");
955 run(dpif_flow_del(dpif,
956 ofpbuf_data(&key), ofpbuf_size(&key),
957 print_statistics ? &stats : NULL), "deleting flow");
959 simap_destroy(&port_names);
961 ofpbuf_uninit(&mask);
963 if (print_statistics) {
967 dpif_flow_stats_format(&stats, &s);
974 dpctl_del_flows(int argc, char *argv[])
979 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
980 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
983 run(dpif_flow_flush(dpif), "deleting all flows");
988 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
993 /* Undocumented commands for unit testing. */
996 dpctl_parse_actions(int argc, char *argv[])
1000 for (i = 1; i < argc; i++) {
1001 struct ofpbuf actions;
1004 ofpbuf_init(&actions, 0);
1005 run(odp_actions_from_string(argv[i], NULL, &actions),
1006 "odp_actions_from_string");
1009 format_odp_actions(&s, ofpbuf_data(&actions), ofpbuf_size(&actions));
1013 ofpbuf_uninit(&actions);
1017 struct actions_for_flow {
1018 struct hmap_node hmap_node;
1020 struct ofpbuf actions;
1023 static struct actions_for_flow *
1024 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1026 uint32_t hash = flow_hash(flow, 0);
1027 struct actions_for_flow *af;
1029 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1030 if (flow_equal(&af->flow, flow)) {
1035 af = xmalloc(sizeof *af);
1037 ofpbuf_init(&af->actions, 0);
1038 hmap_insert(actions_per_flow, &af->hmap_node, hash);
1043 compare_actions_for_flow(const void *a_, const void *b_)
1045 struct actions_for_flow *const *a = a_;
1046 struct actions_for_flow *const *b = b_;
1048 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1052 compare_output_actions(const void *a_, const void *b_)
1054 const struct nlattr *a = a_;
1055 const struct nlattr *b = b_;
1056 uint32_t a_port = nl_attr_get_u32(a);
1057 uint32_t b_port = nl_attr_get_u32(b);
1059 return a_port < b_port ? -1 : a_port > b_port;
1063 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1065 size_t bytes = (uint8_t *) end - (uint8_t *) first;
1066 size_t n = bytes / NL_A_U32_SIZE;
1068 ovs_assert(bytes % NL_A_U32_SIZE == 0);
1069 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1073 sort_output_actions(struct nlattr *actions, size_t length)
1075 struct nlattr *first_output = NULL;
1079 NL_ATTR_FOR_EACH (a, left, actions, length) {
1080 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1081 if (!first_output) {
1086 sort_output_actions__(first_output, a);
1087 first_output = NULL;
1092 uint8_t *end = (uint8_t *) actions + length;
1093 sort_output_actions__(first_output,
1094 ALIGNED_CAST(struct nlattr *, end));
1098 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1099 * have the syntax used by "ovs-dpctl dump-flows".
1101 * This command prints ACTIONS in a format that shows what happens for each
1102 * VLAN, independent of the order of the ACTIONS. For example, there is more
1103 * than one way to output a packet on VLANs 9 and 11, but this command will
1104 * print the same output for any form.
1106 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1107 * so far the implementation only covers VLANs. */
1109 dpctl_normalize_actions(int argc, char *argv[])
1111 struct simap port_names;
1112 struct ofpbuf keybuf;
1114 struct ofpbuf odp_actions;
1115 struct hmap actions_per_flow;
1116 struct actions_for_flow **afs;
1117 struct actions_for_flow *af;
1126 simap_init(&port_names);
1127 for (i = 3; i < argc; i++) {
1131 if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1132 uintptr_t n = number;
1133 simap_put(&port_names, name, n);
1135 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
1139 /* Parse flow key. */
1140 ofpbuf_init(&keybuf, 0);
1141 run(odp_flow_from_string(argv[1], &port_names, &keybuf, NULL),
1142 "odp_flow_key_from_string");
1145 odp_flow_format(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf), NULL, 0, NULL, &s, verbosity);
1146 printf("input flow: %s\n", ds_cstr(&s));
1148 run(odp_flow_key_to_flow(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf), &flow),
1149 "odp_flow_key_to_flow");
1150 ofpbuf_uninit(&keybuf);
1152 /* Parse actions. */
1153 ofpbuf_init(&odp_actions, 0);
1154 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
1155 "odp_actions_from_string");
1156 simap_destroy(&port_names);
1160 format_odp_actions(&s, ofpbuf_data(&odp_actions), ofpbuf_size(&odp_actions));
1161 printf("input actions: %s\n", ds_cstr(&s));
1164 hmap_init(&actions_per_flow);
1165 NL_ATTR_FOR_EACH (a, left, ofpbuf_data(&odp_actions), ofpbuf_size(&odp_actions)) {
1166 const struct ovs_action_push_vlan *push;
1167 switch(nl_attr_type(a)) {
1168 case OVS_ACTION_ATTR_POP_VLAN:
1169 flow.vlan_tci = htons(0);
1172 case OVS_ACTION_ATTR_PUSH_VLAN:
1173 push = nl_attr_get_unspec(a, sizeof *push);
1174 flow.vlan_tci = push->vlan_tci;
1178 af = get_actions_for_flow(&actions_per_flow, &flow);
1179 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1180 nl_attr_get(a), nl_attr_get_size(a));
1183 n_afs = hmap_count(&actions_per_flow);
1184 afs = xmalloc(n_afs * sizeof *afs);
1186 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1189 ovs_assert(i == n_afs);
1191 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1193 for (i = 0; i < n_afs; i++) {
1194 const struct actions_for_flow *af = afs[i];
1196 sort_output_actions(ofpbuf_data(&af->actions), ofpbuf_size(&af->actions));
1198 if (af->flow.vlan_tci != htons(0)) {
1199 printf("vlan(vid=%"PRIu16",pcp=%d): ",
1200 vlan_tci_to_vid(af->flow.vlan_tci),
1201 vlan_tci_to_pcp(af->flow.vlan_tci));
1203 printf("no vlan: ");
1206 if (eth_type_mpls(af->flow.dl_type)) {
1207 printf("mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1208 mpls_lse_to_label(af->flow.mpls_lse[0]),
1209 mpls_lse_to_tc(af->flow.mpls_lse[0]),
1210 mpls_lse_to_ttl(af->flow.mpls_lse[0]));
1212 printf("no mpls: ");
1216 format_odp_actions(&s, ofpbuf_data(&af->actions), ofpbuf_size(&af->actions));
1222 static const struct command all_commands[] = {
1223 { "add-dp", 1, INT_MAX, dpctl_add_dp },
1224 { "del-dp", 1, 1, dpctl_del_dp },
1225 { "add-if", 2, INT_MAX, dpctl_add_if },
1226 { "del-if", 2, INT_MAX, dpctl_del_if },
1227 { "set-if", 2, INT_MAX, dpctl_set_if },
1228 { "dump-dps", 0, 0, dpctl_dump_dps },
1229 { "show", 0, INT_MAX, dpctl_show },
1230 { "dump-flows", 0, 2, dpctl_dump_flows },
1231 { "add-flow", 2, 3, dpctl_add_flow },
1232 { "mod-flow", 2, 3, dpctl_mod_flow },
1233 { "del-flow", 1, 2, dpctl_del_flow },
1234 { "del-flows", 0, 1, dpctl_del_flows },
1235 { "help", 0, INT_MAX, dpctl_help },
1237 /* Undocumented commands for testing. */
1238 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1239 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1241 { NULL, 0, 0, NULL },
1244 static const struct command *get_all_commands(void)
1246 return all_commands;