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);
356 error = dpif_port_add(dpif, netdev, &port_no);
358 ovs_error(error, "adding %s to %s failed", name, argv[1]);
365 netdev_close(netdev);
377 dpctl_set_if(int argc, char *argv[])
379 bool failure = false;
383 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
384 for (i = 2; i < argc; i++) {
385 struct netdev *netdev = NULL;
386 struct dpif_port dpif_port;
387 char *save_ptr = NULL;
395 name = strtok_r(argv[i], ",", &save_ptr);
397 ovs_error(0, "%s is not a valid network device name", argv[i]);
402 /* Get the port's type from the datapath. */
403 error = dpif_port_query_by_name(dpif, name, &dpif_port);
405 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
408 type = xstrdup(dpif_port.type);
409 port_no = dpif_port.port_no;
410 dpif_port_destroy(&dpif_port);
412 /* Retrieve its existing configuration. */
413 error = netdev_open(name, type, &netdev);
415 ovs_error(error, "%s: failed to open network device", name);
420 error = netdev_get_config(netdev, &args);
422 ovs_error(error, "%s: failed to fetch configuration", name);
426 /* Parse changes to configuration. */
427 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
428 char *save_ptr_2 = NULL;
431 key = strtok_r(option, "=", &save_ptr_2);
432 value = strtok_r(NULL, "", &save_ptr_2);
437 if (!strcmp(key, "type")) {
438 if (strcmp(value, type)) {
439 ovs_error(0, "%s: can't change type from %s to %s",
443 } else if (!strcmp(key, "port_no")) {
444 if (port_no != u32_to_odp(atoi(value))) {
445 ovs_error(0, "%s: can't change port number from "
447 name, port_no, atoi(value));
450 } else if (value[0] == '\0') {
451 smap_remove(&args, key);
453 smap_replace(&args, key, value);
457 /* Update configuration. */
458 error = netdev_set_config(netdev, &args);
466 netdev_close(netdev);
478 get_port_number(struct dpif *dpif, const char *name, odp_port_t *port)
480 struct dpif_port dpif_port;
482 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
483 *port = dpif_port.port_no;
484 dpif_port_destroy(&dpif_port);
487 ovs_error(0, "no port named %s", name);
493 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
495 bool failure = false;
499 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
500 for (i = 2; i < argc; i++) {
501 const char *name = argv[i];
505 if (!name[strspn(name, "0123456789")]) {
506 port = u32_to_odp(atoi(name));
507 } else if (!get_port_number(dpif, name, &port)) {
512 error = dpif_port_del(dpif, port);
514 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
525 print_stat(const char *leader, uint64_t value)
527 fputs(leader, stdout);
528 if (value != UINT64_MAX) {
529 printf("%"PRIu64, value);
536 print_human_size(uint64_t value)
538 if (value == UINT64_MAX) {
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);
552 show_dpif(struct dpif *dpif)
554 struct dpif_port_dump dump;
555 struct dpif_port dpif_port;
556 struct dpif_dp_stats stats;
557 struct netdev *netdev;
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 if (stats.n_masks != UINT32_MAX) {
565 uint64_t n_pkts = stats.n_hit + stats.n_missed;
566 double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
568 printf("\tmasks: hit:%"PRIu64" total:%"PRIu32" hit/pkt:%.2f\n",
569 stats.n_mask_hit, stats.n_masks, avg);
573 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
574 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
576 if (strcmp(dpif_port.type, "system")) {
579 printf (" (%s", dpif_port.type);
581 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
586 error = netdev_get_config(netdev, &config);
588 const struct smap_node **nodes;
591 nodes = smap_sort(&config);
592 for (i = 0; i < smap_count(&config); i++) {
593 const struct smap_node *node = nodes[i];
594 printf("%c %s=%s", i ? ',' : ':', node->key,
599 printf(", could not retrieve configuration (%s)",
600 ovs_strerror(error));
602 smap_destroy(&config);
604 netdev_close(netdev);
606 printf(": open failed (%s)", ovs_strerror(error));
612 if (print_statistics) {
613 struct netdev_stats s;
616 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
618 printf(", open failed (%s)", ovs_strerror(error));
621 error = netdev_get_stats(netdev, &s);
623 printf(", could not retrieve stats (%s)", ovs_strerror(error));
627 netdev_close(netdev);
628 print_stat("\t\tRX packets:", s.rx_packets);
629 print_stat(" errors:", s.rx_errors);
630 print_stat(" dropped:", s.rx_dropped);
631 print_stat(" overruns:", s.rx_over_errors);
632 print_stat(" frame:", s.rx_frame_errors);
635 print_stat("\t\tTX packets:", s.tx_packets);
636 print_stat(" errors:", s.tx_errors);
637 print_stat(" dropped:", s.tx_dropped);
638 print_stat(" aborted:", s.tx_aborted_errors);
639 print_stat(" carrier:", s.tx_carrier_errors);
642 print_stat("\t\tcollisions:", s.collisions);
645 print_stat("\t\tRX bytes:", s.rx_bytes);
646 print_human_size(s.rx_bytes);
647 print_stat(" TX bytes:", s.tx_bytes);
648 print_human_size(s.tx_bytes);
656 dpctl_show(int argc, char *argv[])
658 bool failure = false;
661 for (i = 1; i < argc; i++) {
662 const char *name = argv[i];
666 error = parsed_dpif_open(name, false, &dpif);
670 ovs_error(error, "opening datapath %s failed", name);
679 dp_enumerate_types(&types);
680 SSET_FOR_EACH (type, &types) {
685 if (dp_enumerate_names(type, &names)) {
689 SSET_FOR_EACH (name, &names) {
693 error = dpif_open(name, type, &dpif);
697 ovs_error(error, "opening datapath %s failed", name);
701 sset_destroy(&names);
703 sset_destroy(&types);
711 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
713 struct sset dpif_names, dpif_types;
717 sset_init(&dpif_names);
718 sset_init(&dpif_types);
719 dp_enumerate_types(&dpif_types);
721 SSET_FOR_EACH (type, &dpif_types) {
725 retval = dp_enumerate_names(type, &dpif_names);
730 SSET_FOR_EACH (name, &dpif_names) {
732 if (!dpif_open(name, type, &dpif)) {
733 printf("%s\n", dpif_name(dpif));
739 sset_destroy(&dpif_names);
740 sset_destroy(&dpif_types);
747 dpctl_dump_flows(int argc, char *argv[])
749 const struct dpif_flow_stats *stats;
750 const struct nlattr *actions;
751 struct dpif_flow_dump flow_dump;
752 const struct nlattr *key;
753 const struct nlattr *mask;
754 struct dpif_port dpif_port;
755 struct dpif_port_dump port_dump;
756 struct hmap portno_names;
757 struct simap names_portno;
763 char *name, *error, *filter = NULL;
764 struct flow flow_filter;
765 struct flow_wildcards wc_filter;
767 if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
768 filter = xstrdup(argv[--argc] + 7);
770 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
772 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
775 hmap_init(&portno_names);
776 simap_init(&names_portno);
777 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
778 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
779 simap_put(&names_portno, dpif_port.name,
780 odp_to_u32(dpif_port.port_no));
784 error = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks, filter,
787 ovs_fatal(0, "Failed to parse filter (%s)", error);
792 dpif_flow_dump_start(&flow_dump, dpif);
793 while (dpif_flow_dump_next(&flow_dump, &key, &key_len,
795 &actions, &actions_len, &stats)) {
798 struct flow_wildcards wc;
799 struct match match, match_filter;
800 struct minimatch minimatch;
802 odp_flow_key_to_flow(key, key_len, &flow);
803 odp_flow_key_to_mask(mask, mask_len, &wc.masks, &flow);
804 match_init(&match, &flow, &wc);
806 match_init(&match_filter, &flow_filter, &wc);
807 match_init(&match_filter, &match_filter.flow, &wc_filter);
808 minimatch_init(&minimatch, &match_filter);
810 if (!minimatch_matches_flow(&minimatch, &match.flow)) {
811 minimatch_destroy(&minimatch);
814 minimatch_destroy(&minimatch);
817 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
819 ds_put_cstr(&ds, ", ");
821 dpif_flow_stats_format(stats, &ds);
822 ds_put_cstr(&ds, ", actions:");
823 format_odp_actions(&ds, actions, actions_len);
824 printf("%s\n", ds_cstr(&ds));
826 dpif_flow_dump_done(&flow_dump);
829 odp_portno_names_destroy(&portno_names);
830 hmap_destroy(&portno_names);
831 simap_destroy(&names_portno);
837 dpctl_put_flow(int argc, char *argv[], enum dpif_flow_put_flags flags)
839 const char *key_s = argv[argc - 2];
840 const char *actions_s = argv[argc - 1];
841 struct dpif_flow_stats stats;
842 struct dpif_port dpif_port;
843 struct dpif_port_dump port_dump;
844 struct ofpbuf actions;
850 struct simap port_names;
852 dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp();
853 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
857 simap_init(&port_names);
858 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
859 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
863 ofpbuf_init(&key, 0);
864 ofpbuf_init(&mask, 0);
865 run(odp_flow_from_string(key_s, &port_names, &key, &mask),
868 simap_destroy(&port_names);
870 ofpbuf_init(&actions, 0);
871 run(odp_actions_from_string(actions_s, NULL, &actions), "parsing actions");
873 run(dpif_flow_put(dpif, flags,
875 mask.size == 0 ? NULL : mask.data, mask.size,
876 actions.data, actions.size,
877 print_statistics ? &stats : NULL),
878 "updating flow table");
881 ofpbuf_uninit(&mask);
882 ofpbuf_uninit(&actions);
884 if (print_statistics) {
888 dpif_flow_stats_format(&stats, &s);
895 dpctl_add_flow(int argc, char *argv[])
897 dpctl_put_flow(argc, argv, DPIF_FP_CREATE);
901 dpctl_mod_flow(int argc OVS_UNUSED, char *argv[])
903 enum dpif_flow_put_flags flags;
905 flags = DPIF_FP_MODIFY;
907 flags |= DPIF_FP_CREATE;
909 if (zero_statistics) {
910 flags |= DPIF_FP_ZERO_STATS;
913 dpctl_put_flow(argc, argv, flags);
917 dpctl_del_flow(int argc, char *argv[])
919 const char *key_s = argv[argc - 1];
920 struct dpif_flow_stats stats;
921 struct dpif_port dpif_port;
922 struct dpif_port_dump port_dump;
924 struct ofpbuf mask; /* To be ignored. */
927 struct simap port_names;
929 dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp();
930 run(parsed_dpif_open(dp_name, false, &dpif), "opening datapath");
933 simap_init(&port_names);
934 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
935 simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
938 ofpbuf_init(&key, 0);
939 ofpbuf_init(&mask, 0);
940 run(odp_flow_from_string(key_s, &port_names, &key, &mask), "parsing flow key");
942 run(dpif_flow_del(dpif,
944 print_statistics ? &stats : NULL), "deleting flow");
946 simap_destroy(&port_names);
948 ofpbuf_uninit(&mask);
950 if (print_statistics) {
954 dpif_flow_stats_format(&stats, &s);
961 dpctl_del_flows(int argc, char *argv[])
966 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
967 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
970 run(dpif_flow_flush(dpif), "deleting all flows");
975 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
980 /* Undocumented commands for unit testing. */
983 dpctl_parse_actions(int argc, char *argv[])
987 for (i = 1; i < argc; i++) {
988 struct ofpbuf actions;
991 ofpbuf_init(&actions, 0);
992 run(odp_actions_from_string(argv[i], NULL, &actions),
993 "odp_actions_from_string");
996 format_odp_actions(&s, actions.data, actions.size);
1000 ofpbuf_uninit(&actions);
1004 struct actions_for_flow {
1005 struct hmap_node hmap_node;
1007 struct ofpbuf actions;
1010 static struct actions_for_flow *
1011 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1013 uint32_t hash = flow_hash(flow, 0);
1014 struct actions_for_flow *af;
1016 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1017 if (flow_equal(&af->flow, flow)) {
1022 af = xmalloc(sizeof *af);
1024 ofpbuf_init(&af->actions, 0);
1025 hmap_insert(actions_per_flow, &af->hmap_node, hash);
1030 compare_actions_for_flow(const void *a_, const void *b_)
1032 struct actions_for_flow *const *a = a_;
1033 struct actions_for_flow *const *b = b_;
1035 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1039 compare_output_actions(const void *a_, const void *b_)
1041 const struct nlattr *a = a_;
1042 const struct nlattr *b = b_;
1043 uint32_t a_port = nl_attr_get_u32(a);
1044 uint32_t b_port = nl_attr_get_u32(b);
1046 return a_port < b_port ? -1 : a_port > b_port;
1050 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1052 size_t bytes = (uint8_t *) end - (uint8_t *) first;
1053 size_t n = bytes / NL_A_U32_SIZE;
1055 ovs_assert(bytes % NL_A_U32_SIZE == 0);
1056 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1060 sort_output_actions(struct nlattr *actions, size_t length)
1062 struct nlattr *first_output = NULL;
1066 NL_ATTR_FOR_EACH (a, left, actions, length) {
1067 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1068 if (!first_output) {
1073 sort_output_actions__(first_output, a);
1074 first_output = NULL;
1079 uint8_t *end = (uint8_t *) actions + length;
1080 sort_output_actions__(first_output,
1081 ALIGNED_CAST(struct nlattr *, end));
1085 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1086 * have the syntax used by "ovs-dpctl dump-flows".
1088 * This command prints ACTIONS in a format that shows what happens for each
1089 * VLAN, independent of the order of the ACTIONS. For example, there is more
1090 * than one way to output a packet on VLANs 9 and 11, but this command will
1091 * print the same output for any form.
1093 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1094 * so far the implementation only covers VLANs. */
1096 dpctl_normalize_actions(int argc, char *argv[])
1098 struct simap port_names;
1099 struct ofpbuf keybuf;
1101 struct ofpbuf odp_actions;
1102 struct hmap actions_per_flow;
1103 struct actions_for_flow **afs;
1104 struct actions_for_flow *af;
1113 simap_init(&port_names);
1114 for (i = 3; i < argc; i++) {
1118 if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1119 uintptr_t n = number;
1120 simap_put(&port_names, name, n);
1122 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
1126 /* Parse flow key. */
1127 ofpbuf_init(&keybuf, 0);
1128 run(odp_flow_from_string(argv[1], &port_names, &keybuf, NULL),
1129 "odp_flow_key_from_string");
1132 odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL, &s, verbosity);
1133 printf("input flow: %s\n", ds_cstr(&s));
1135 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
1136 "odp_flow_key_to_flow");
1137 ofpbuf_uninit(&keybuf);
1139 /* Parse actions. */
1140 ofpbuf_init(&odp_actions, 0);
1141 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
1142 "odp_actions_from_string");
1143 simap_destroy(&port_names);
1147 format_odp_actions(&s, odp_actions.data, odp_actions.size);
1148 printf("input actions: %s\n", ds_cstr(&s));
1151 hmap_init(&actions_per_flow);
1152 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1153 const struct ovs_action_push_vlan *push;
1154 switch(nl_attr_type(a)) {
1155 case OVS_ACTION_ATTR_POP_VLAN:
1156 flow.vlan_tci = htons(0);
1159 case OVS_ACTION_ATTR_PUSH_VLAN:
1160 push = nl_attr_get_unspec(a, sizeof *push);
1161 flow.vlan_tci = push->vlan_tci;
1165 af = get_actions_for_flow(&actions_per_flow, &flow);
1166 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1167 nl_attr_get(a), nl_attr_get_size(a));
1170 n_afs = hmap_count(&actions_per_flow);
1171 afs = xmalloc(n_afs * sizeof *afs);
1173 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1176 ovs_assert(i == n_afs);
1178 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1180 for (i = 0; i < n_afs; i++) {
1181 const struct actions_for_flow *af = afs[i];
1183 sort_output_actions(af->actions.data, af->actions.size);
1185 if (af->flow.vlan_tci != htons(0)) {
1186 printf("vlan(vid=%"PRIu16",pcp=%d): ",
1187 vlan_tci_to_vid(af->flow.vlan_tci),
1188 vlan_tci_to_pcp(af->flow.vlan_tci));
1190 printf("no vlan: ");
1193 if (eth_type_mpls(af->flow.dl_type)) {
1194 printf("mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1195 mpls_lse_to_label(af->flow.mpls_lse),
1196 mpls_lse_to_tc(af->flow.mpls_lse),
1197 mpls_lse_to_ttl(af->flow.mpls_lse));
1199 printf("no mpls: ");
1203 format_odp_actions(&s, af->actions.data, af->actions.size);
1209 static const struct command all_commands[] = {
1210 { "add-dp", 1, INT_MAX, dpctl_add_dp },
1211 { "del-dp", 1, 1, dpctl_del_dp },
1212 { "add-if", 2, INT_MAX, dpctl_add_if },
1213 { "del-if", 2, INT_MAX, dpctl_del_if },
1214 { "set-if", 2, INT_MAX, dpctl_set_if },
1215 { "dump-dps", 0, 0, dpctl_dump_dps },
1216 { "show", 0, INT_MAX, dpctl_show },
1217 { "dump-flows", 0, 2, dpctl_dump_flows },
1218 { "add-flow", 2, 3, dpctl_add_flow },
1219 { "mod-flow", 2, 3, dpctl_mod_flow },
1220 { "del-flow", 1, 2, dpctl_del_flow },
1221 { "del-flows", 0, 1, dpctl_del_flows },
1222 { "help", 0, INT_MAX, dpctl_help },
1224 /* Undocumented commands for testing. */
1225 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1226 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1228 { NULL, 0, 0, NULL },
1231 static const struct command *get_all_commands(void)
1233 return all_commands;