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"
43 #include "ofp-parse.h"
54 /* -s, --statistics: Print port/flow statistics? */
55 static bool print_statistics;
57 /* --clear: Reset existing statistics to zero when modifying a flow? */
58 static bool zero_statistics;
60 /* --may-create: Allow mod-flows command to create a new flow? */
61 static bool may_create;
63 /* -m, --more: Increase output verbosity. */
66 static const struct command *get_all_commands(void);
68 static void usage(void) NO_RETURN;
69 static void parse_options(int argc, char *argv[]);
72 main(int argc, char *argv[])
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());
82 parse_options(int argc, char *argv[])
85 OPT_CLEAR = UCHAR_MAX + 1,
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'},
100 char *short_options = long_options_to_short_options(long_options);
103 unsigned long int timeout;
106 c = getopt_long(argc, argv, short_options, long_options, NULL);
113 print_statistics = true;
117 zero_statistics = true;
129 timeout = strtoul(optarg, NULL, 10);
131 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
142 ovs_print_version(0, 0);
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 "For COMMAND dump-flows, add-flow, mod-flow, del-flow and\n"
179 "del-flows, DP is optional if there is only one datapath.\n",
180 program_name, program_name);
182 printf("\nOptions for show and mod-flow:\n"
183 " -s, --statistics print statistics for port or flow\n"
184 "\nOptions for dump-flows:\n"
185 " -m, --more increase verbosity of output\n"
186 "\nOptions for mod-flow:\n"
187 " --may-create create flow if it doesn't exist\n"
188 " --clear reset existing stats to zero\n"
190 " -t, --timeout=SECS give up after SECS seconds\n"
191 " -h, --help display this help message\n"
192 " -V, --version display version information\n");
196 static void run(int retval, const char *message, ...)
199 static void run(int retval, const char *message, ...)
204 va_start(args, message);
205 ovs_fatal_valist(retval, message, args);
209 static void dpctl_add_if(int argc, char *argv[]);
211 static int if_up(const char *netdev_name)
213 struct netdev *netdev;
216 retval = netdev_open(netdev_name, "system", &netdev);
218 retval = netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
219 netdev_close(netdev);
224 /* Retrieve the name of the datapath if exactly one exists. The caller
225 * is responsible for freeing the returned string. If there is not one
226 * datapath, aborts with an error message. */
232 char *dp_name = NULL;
236 dp_enumerate_types(&types);
237 SSET_FOR_EACH (type, &types) {
241 if (!dp_enumerate_names(type, &names)) {
242 count += sset_count(&names);
243 if (!dp_name && count == 1) {
244 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
247 sset_destroy(&names);
249 sset_destroy(&types);
252 ovs_fatal(0, "no datapaths exist");
253 } else if (count > 1) {
254 ovs_fatal(0, "multiple datapaths, specify one");
261 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
266 dp_parse_name(arg_, &name, &type);
269 result = dpif_create(name, type, dpifp);
271 result = dpif_open(name, type, dpifp);
280 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
283 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
286 dpctl_add_if(argc, argv);
291 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
294 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
295 run(dpif_delete(dpif), "del_dp");
300 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
302 bool failure = false;
306 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
307 for (i = 2; i < argc; i++) {
308 const char *name, *type;
309 char *save_ptr = NULL;
310 struct netdev *netdev = NULL;
312 odp_port_t port_no = ODPP_NONE;
316 name = strtok_r(argv[i], ",", &save_ptr);
320 ovs_error(0, "%s is not a valid network device name", argv[i]);
326 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
327 char *save_ptr_2 = NULL;
330 key = strtok_r(option, "=", &save_ptr_2);
331 value = strtok_r(NULL, "", &save_ptr_2);
336 if (!strcmp(key, "type")) {
338 } else if (!strcmp(key, "port_no")) {
339 port_no = u32_to_odp(atoi(value));
340 } else if (!smap_add_once(&args, key, value)) {
341 ovs_error(0, "duplicate \"%s\" option", key);
345 error = netdev_open(name, type, &netdev);
347 ovs_error(error, "%s: failed to open network device", name);
351 error = netdev_set_config(netdev, &args);
353 ovs_error(error, "%s: failed to configure network device", name);
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);
462 ovs_error(error, "%s: failed to configure network device", name);
468 netdev_close(netdev);
480 get_port_number(struct dpif *dpif, const char *name, odp_port_t *port)
482 struct dpif_port dpif_port;
484 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
485 *port = dpif_port.port_no;
486 dpif_port_destroy(&dpif_port);
489 ovs_error(0, "no port named %s", name);
495 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
497 bool failure = false;
501 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
502 for (i = 2; i < argc; i++) {
503 const char *name = argv[i];
507 if (!name[strspn(name, "0123456789")]) {
508 port = u32_to_odp(atoi(name));
509 } else if (!get_port_number(dpif, name, &port)) {
514 error = dpif_port_del(dpif, port);
516 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
527 print_stat(const char *leader, uint64_t value)
529 fputs(leader, stdout);
530 if (value != UINT64_MAX) {
531 printf("%"PRIu64, value);
538 print_human_size(uint64_t value)
540 if (value == UINT64_MAX) {
542 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
543 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
544 } else if (value >= 1024ULL * 1024 * 1024) {
545 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
546 } else if (value >= 1024ULL * 1024) {
547 printf(" (%.1f MiB)", value / (1024.0 * 1024));
548 } else if (value >= 1024) {
549 printf(" (%.1f KiB)", value / 1024.0);
554 show_dpif(struct dpif *dpif)
556 struct dpif_port_dump dump;
557 struct dpif_port dpif_port;
558 struct dpif_dp_stats stats;
559 struct netdev *netdev;
561 printf("%s:\n", dpif_name(dpif));
562 if (!dpif_get_dp_stats(dpif, &stats)) {
563 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
564 "\tflows: %"PRIu64"\n",
565 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
566 if (stats.n_masks != UINT64_MAX) {
567 uint64_t n_pkts = stats.n_hit + stats.n_missed;
568 double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
570 printf("\tmasks: hit:%"PRIu64" total:%"PRIu64" hit/pkt:%.2f\n",
571 stats.n_mask_hit, stats.n_masks, avg);
575 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
576 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
578 if (strcmp(dpif_port.type, "system")) {
581 printf (" (%s", dpif_port.type);
583 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
588 error = netdev_get_config(netdev, &config);
590 const struct smap_node **nodes;
593 nodes = smap_sort(&config);
594 for (i = 0; i < smap_count(&config); i++) {
595 const struct smap_node *node = nodes[i];
596 printf("%c %s=%s", i ? ',' : ':', node->key,
601 printf(", could not retrieve configuration (%s)",
602 ovs_strerror(error));
604 smap_destroy(&config);
606 netdev_close(netdev);
608 printf(": open failed (%s)", ovs_strerror(error));
614 if (print_statistics) {
615 struct netdev_stats s;
618 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
620 printf(", open failed (%s)", ovs_strerror(error));
623 error = netdev_get_stats(netdev, &s);
625 printf(", could not retrieve stats (%s)", ovs_strerror(error));
629 netdev_close(netdev);
630 print_stat("\t\tRX packets:", s.rx_packets);
631 print_stat(" errors:", s.rx_errors);
632 print_stat(" dropped:", s.rx_dropped);
633 print_stat(" overruns:", s.rx_over_errors);
634 print_stat(" frame:", s.rx_frame_errors);
637 print_stat("\t\tTX packets:", s.tx_packets);
638 print_stat(" errors:", s.tx_errors);
639 print_stat(" dropped:", s.tx_dropped);
640 print_stat(" aborted:", s.tx_aborted_errors);
641 print_stat(" carrier:", s.tx_carrier_errors);
644 print_stat("\t\tcollisions:", s.collisions);
647 print_stat("\t\tRX bytes:", s.rx_bytes);
648 print_human_size(s.rx_bytes);
649 print_stat(" TX bytes:", s.tx_bytes);
650 print_human_size(s.tx_bytes);
658 dpctl_show(int argc, char *argv[])
660 bool failure = false;
663 for (i = 1; i < argc; i++) {
664 const char *name = argv[i];
668 error = parsed_dpif_open(name, false, &dpif);
672 ovs_error(error, "opening datapath %s failed", name);
681 dp_enumerate_types(&types);
682 SSET_FOR_EACH (type, &types) {
687 if (dp_enumerate_names(type, &names)) {
691 SSET_FOR_EACH (name, &names) {
695 error = dpif_open(name, type, &dpif);
699 ovs_error(error, "opening datapath %s failed", name);
703 sset_destroy(&names);
705 sset_destroy(&types);
713 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
715 struct sset dpif_names, dpif_types;
719 sset_init(&dpif_names);
720 sset_init(&dpif_types);
721 dp_enumerate_types(&dpif_types);
723 SSET_FOR_EACH (type, &dpif_types) {
727 retval = dp_enumerate_names(type, &dpif_names);
732 SSET_FOR_EACH (name, &dpif_names) {
734 if (!dpif_open(name, type, &dpif)) {
735 printf("%s\n", dpif_name(dpif));
741 sset_destroy(&dpif_names);
742 sset_destroy(&dpif_types);
749 dpctl_dump_flows(int argc, char *argv[])
751 const struct dpif_flow_stats *stats;
752 const struct nlattr *actions;
753 struct dpif_flow_dump flow_dump;
754 const struct nlattr *key;
755 const struct nlattr *mask;
756 struct dpif_port dpif_port;
757 struct dpif_port_dump port_dump;
758 struct hmap portno_names;
759 struct simap names_portno;
765 char *name, *error, *filter = NULL;
766 struct flow flow_filter;
767 struct flow_wildcards wc_filter;
769 if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
770 filter = xstrdup(argv[--argc] + 7);
772 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
774 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
777 hmap_init(&portno_names);
778 simap_init(&names_portno);
779 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
780 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
781 simap_put(&names_portno, dpif_port.name,
782 odp_to_u32(dpif_port.port_no));
786 error = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks, filter,
789 ovs_fatal(0, "Failed to parse filter (%s)", error);
794 dpif_flow_dump_start(&flow_dump, dpif);
795 while (dpif_flow_dump_next(&flow_dump, &key, &key_len,
797 &actions, &actions_len, &stats)) {
800 struct flow_wildcards wc;
801 struct match match, match_filter;
802 struct minimatch minimatch;
804 odp_flow_key_to_flow(key, key_len, &flow);
805 odp_flow_key_to_mask(mask, mask_len, &wc.masks, &flow);
806 match_init(&match, &flow, &wc);
808 match_init(&match_filter, &flow_filter, &wc);
809 match_init(&match_filter, &match_filter.flow, &wc_filter);
810 minimatch_init(&minimatch, &match_filter);
812 if (!minimatch_matches_flow(&minimatch, &match.flow)) {
813 minimatch_destroy(&minimatch);
816 minimatch_destroy(&minimatch);
819 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
821 ds_put_cstr(&ds, ", ");
823 dpif_flow_stats_format(stats, &ds);
824 ds_put_cstr(&ds, ", actions:");
825 format_odp_actions(&ds, actions, actions_len);
826 printf("%s\n", ds_cstr(&ds));
828 dpif_flow_dump_done(&flow_dump);
831 odp_portno_names_destroy(&portno_names);
832 hmap_destroy(&portno_names);
833 simap_destroy(&names_portno);
839 dpctl_put_flow(int argc, char *argv[], enum dpif_flow_put_flags flags)
841 const char *key_s = argv[argc - 2];
842 const char *actions_s = argv[argc - 1];
843 struct dpif_flow_stats stats;
844 struct dpif_port dpif_port;
845 struct dpif_port_dump port_dump;
846 struct ofpbuf actions;
852 struct simap port_names;
854 dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp();
855 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
859 simap_init(&port_names);
860 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
861 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
865 ofpbuf_init(&key, 0);
866 ofpbuf_init(&mask, 0);
867 run(odp_flow_from_string(key_s, &port_names, &key, &mask),
870 simap_destroy(&port_names);
872 ofpbuf_init(&actions, 0);
873 run(odp_actions_from_string(actions_s, NULL, &actions), "parsing actions");
875 run(dpif_flow_put(dpif, flags,
877 mask.size == 0 ? NULL : mask.data, mask.size,
878 actions.data, actions.size,
879 print_statistics ? &stats : NULL),
880 "updating flow table");
883 ofpbuf_uninit(&mask);
884 ofpbuf_uninit(&actions);
886 if (print_statistics) {
890 dpif_flow_stats_format(&stats, &s);
897 dpctl_add_flow(int argc, char *argv[])
899 dpctl_put_flow(argc, argv, DPIF_FP_CREATE);
903 dpctl_mod_flow(int argc OVS_UNUSED, char *argv[])
905 enum dpif_flow_put_flags flags;
907 flags = DPIF_FP_MODIFY;
909 flags |= DPIF_FP_CREATE;
911 if (zero_statistics) {
912 flags |= DPIF_FP_ZERO_STATS;
915 dpctl_put_flow(argc, argv, flags);
919 dpctl_del_flow(int argc, char *argv[])
921 const char *key_s = argv[argc - 1];
922 struct dpif_flow_stats stats;
923 struct dpif_port dpif_port;
924 struct dpif_port_dump port_dump;
926 struct ofpbuf mask; /* To be ignored. */
929 struct simap port_names;
931 dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp();
932 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
935 simap_init(&port_names);
936 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
937 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
940 ofpbuf_init(&key, 0);
941 ofpbuf_init(&mask, 0);
942 run(odp_flow_from_string(key_s, &port_names, &key, &mask), "parsing flow key");
944 run(dpif_flow_del(dpif,
946 print_statistics ? &stats : NULL), "deleting flow");
948 simap_destroy(&port_names);
950 ofpbuf_uninit(&mask);
952 if (print_statistics) {
956 dpif_flow_stats_format(&stats, &s);
963 dpctl_del_flows(int argc, char *argv[])
968 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
969 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
972 run(dpif_flow_flush(dpif), "deleting all flows");
977 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
982 /* Undocumented commands for unit testing. */
985 dpctl_parse_actions(int argc, char *argv[])
989 for (i = 1; i < argc; i++) {
990 struct ofpbuf actions;
993 ofpbuf_init(&actions, 0);
994 run(odp_actions_from_string(argv[i], NULL, &actions),
995 "odp_actions_from_string");
998 format_odp_actions(&s, actions.data, actions.size);
1002 ofpbuf_uninit(&actions);
1006 struct actions_for_flow {
1007 struct hmap_node hmap_node;
1009 struct ofpbuf actions;
1012 static struct actions_for_flow *
1013 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1015 uint32_t hash = flow_hash(flow, 0);
1016 struct actions_for_flow *af;
1018 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1019 if (flow_equal(&af->flow, flow)) {
1024 af = xmalloc(sizeof *af);
1026 ofpbuf_init(&af->actions, 0);
1027 hmap_insert(actions_per_flow, &af->hmap_node, hash);
1032 compare_actions_for_flow(const void *a_, const void *b_)
1034 struct actions_for_flow *const *a = a_;
1035 struct actions_for_flow *const *b = b_;
1037 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1041 compare_output_actions(const void *a_, const void *b_)
1043 const struct nlattr *a = a_;
1044 const struct nlattr *b = b_;
1045 uint32_t a_port = nl_attr_get_u32(a);
1046 uint32_t b_port = nl_attr_get_u32(b);
1048 return a_port < b_port ? -1 : a_port > b_port;
1052 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1054 size_t bytes = (uint8_t *) end - (uint8_t *) first;
1055 size_t n = bytes / NL_A_U32_SIZE;
1057 ovs_assert(bytes % NL_A_U32_SIZE == 0);
1058 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1062 sort_output_actions(struct nlattr *actions, size_t length)
1064 struct nlattr *first_output = NULL;
1068 NL_ATTR_FOR_EACH (a, left, actions, length) {
1069 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1070 if (!first_output) {
1075 sort_output_actions__(first_output, a);
1076 first_output = NULL;
1081 uint8_t *end = (uint8_t *) actions + length;
1082 sort_output_actions__(first_output,
1083 ALIGNED_CAST(struct nlattr *, end));
1087 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1088 * have the syntax used by "ovs-dpctl dump-flows".
1090 * This command prints ACTIONS in a format that shows what happens for each
1091 * VLAN, independent of the order of the ACTIONS. For example, there is more
1092 * than one way to output a packet on VLANs 9 and 11, but this command will
1093 * print the same output for any form.
1095 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1096 * so far the implementation only covers VLANs. */
1098 dpctl_normalize_actions(int argc, char *argv[])
1100 struct simap port_names;
1101 struct ofpbuf keybuf;
1103 struct ofpbuf odp_actions;
1104 struct hmap actions_per_flow;
1105 struct actions_for_flow **afs;
1106 struct actions_for_flow *af;
1115 simap_init(&port_names);
1116 for (i = 3; i < argc; i++) {
1121 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
1122 uintptr_t n = number;
1123 simap_put(&port_names, name, n);
1125 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
1129 /* Parse flow key. */
1130 ofpbuf_init(&keybuf, 0);
1131 run(odp_flow_from_string(argv[1], &port_names, &keybuf, NULL),
1132 "odp_flow_key_from_string");
1135 odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL, &s, verbosity);
1136 printf("input flow: %s\n", ds_cstr(&s));
1138 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
1139 "odp_flow_key_to_flow");
1140 ofpbuf_uninit(&keybuf);
1142 /* Parse actions. */
1143 ofpbuf_init(&odp_actions, 0);
1144 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
1145 "odp_actions_from_string");
1146 simap_destroy(&port_names);
1150 format_odp_actions(&s, odp_actions.data, odp_actions.size);
1151 printf("input actions: %s\n", ds_cstr(&s));
1154 hmap_init(&actions_per_flow);
1155 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1156 const struct ovs_action_push_vlan *push;
1157 switch(nl_attr_type(a)) {
1158 case OVS_ACTION_ATTR_POP_VLAN:
1159 flow.vlan_tci = htons(0);
1162 case OVS_ACTION_ATTR_PUSH_VLAN:
1163 push = nl_attr_get_unspec(a, sizeof *push);
1164 flow.vlan_tci = push->vlan_tci;
1168 af = get_actions_for_flow(&actions_per_flow, &flow);
1169 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1170 nl_attr_get(a), nl_attr_get_size(a));
1173 n_afs = hmap_count(&actions_per_flow);
1174 afs = xmalloc(n_afs * sizeof *afs);
1176 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1179 ovs_assert(i == n_afs);
1181 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1183 for (i = 0; i < n_afs; i++) {
1184 const struct actions_for_flow *af = afs[i];
1186 sort_output_actions(af->actions.data, af->actions.size);
1188 if (af->flow.vlan_tci != htons(0)) {
1189 printf("vlan(vid=%"PRIu16",pcp=%d): ",
1190 vlan_tci_to_vid(af->flow.vlan_tci),
1191 vlan_tci_to_pcp(af->flow.vlan_tci));
1193 printf("no vlan: ");
1196 if (eth_type_mpls(af->flow.dl_type)) {
1197 printf("mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1198 mpls_lse_to_label(af->flow.mpls_lse),
1199 mpls_lse_to_tc(af->flow.mpls_lse),
1200 mpls_lse_to_ttl(af->flow.mpls_lse));
1202 printf("no mpls: ");
1206 format_odp_actions(&s, af->actions.data, af->actions.size);
1212 static const struct command all_commands[] = {
1213 { "add-dp", 1, INT_MAX, dpctl_add_dp },
1214 { "del-dp", 1, 1, dpctl_del_dp },
1215 { "add-if", 2, INT_MAX, dpctl_add_if },
1216 { "del-if", 2, INT_MAX, dpctl_del_if },
1217 { "set-if", 2, INT_MAX, dpctl_set_if },
1218 { "dump-dps", 0, 0, dpctl_dump_dps },
1219 { "show", 0, INT_MAX, dpctl_show },
1220 { "dump-flows", 0, 2, dpctl_dump_flows },
1221 { "add-flow", 2, 3, dpctl_add_flow },
1222 { "mod-flow", 2, 3, dpctl_mod_flow },
1223 { "del-flow", 1, 2, dpctl_del_flow },
1224 { "del-flows", 0, 1, dpctl_del_flows },
1225 { "help", 0, INT_MAX, dpctl_help },
1227 /* Undocumented commands for testing. */
1228 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1229 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1231 { NULL, 0, 0, NULL },
1234 static const struct command *get_all_commands(void)
1236 return all_commands;