2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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>
23 #include <sys/socket.h>
25 #include <netinet/in.h>
34 #include "command-line.h"
38 #include "dynamic-string.h"
53 VLOG_DEFINE_THIS_MODULE(dpctl);
55 /* -s, --statistics: Print port statistics? */
56 static bool print_statistics;
58 /* -m, --more: Output verbosity.
60 * So far only undocumented commands honor this option, so we don't document
61 * the option itself. */
64 static const struct command all_commands[];
66 static void usage(void) NO_RETURN;
67 static void parse_options(int argc, char *argv[]);
70 main(int argc, char *argv[])
72 set_program_name(argv[0]);
73 parse_options(argc, argv);
74 signal(SIGPIPE, SIG_IGN);
75 run_command(argc - optind, argv + optind, all_commands);
80 parse_options(int argc, char *argv[])
83 OPT_DUMMY = UCHAR_MAX + 1,
86 static struct option long_options[] = {
87 {"statistics", no_argument, NULL, 's'},
88 {"more", no_argument, NULL, 'm'},
89 {"timeout", required_argument, NULL, 't'},
90 {"help", no_argument, NULL, 'h'},
91 {"version", no_argument, NULL, 'V'},
95 char *short_options = long_options_to_short_options(long_options);
98 unsigned long int timeout;
101 c = getopt_long(argc, argv, short_options, long_options, NULL);
108 print_statistics = true;
116 timeout = strtoul(optarg, NULL, 10);
118 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
129 ovs_print_version(0, 0);
147 printf("%s: Open vSwitch datapath management utility\n"
148 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
149 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
150 " del-dp DP delete local datapath DP\n"
151 " add-if DP IFACE... add each IFACE as a port on DP\n"
152 " set-if DP IFACE... reconfigure each IFACE within DP\n"
153 " del-if DP IFACE... delete each IFACE from DP\n"
154 " dump-dps display names of all datapaths\n"
155 " show show basic info on all datapaths\n"
156 " show DP... show basic info on each DP\n"
157 " dump-flows DP display flows in DP\n"
158 " del-flows DP delete all flows from DP\n"
159 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
160 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
161 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
162 program_name, program_name);
164 printf("\nOther options:\n"
165 " -t, --timeout=SECS give up after SECS seconds\n"
166 " -h, --help display this help message\n"
167 " -V, --version display version information\n");
171 static void run(int retval, const char *message, ...)
174 static void run(int retval, const char *message, ...)
179 va_start(args, message);
180 ovs_fatal_valist(retval, message, args);
184 static void dpctl_add_if(int argc, char *argv[]);
186 static int if_up(const char *netdev_name)
188 struct netdev *netdev;
191 retval = netdev_open(netdev_name, "system", &netdev);
193 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
194 netdev_close(netdev);
200 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
205 dp_parse_name(arg_, &name, &type);
208 result = dpif_create(name, type, dpifp);
210 result = dpif_open(name, type, dpifp);
219 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
222 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
225 dpctl_add_if(argc, argv);
230 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
233 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
234 run(dpif_delete(dpif), "del_dp");
239 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
241 bool failure = false;
245 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
246 for (i = 2; i < argc; i++) {
247 const char *name, *type;
248 char *save_ptr = NULL;
249 struct netdev *netdev = NULL;
251 uint16_t port_no = UINT16_MAX;
255 name = strtok_r(argv[i], ",", &save_ptr);
259 ovs_error(0, "%s is not a valid network device name", argv[i]);
265 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
266 char *save_ptr_2 = NULL;
269 key = strtok_r(option, "=", &save_ptr_2);
270 value = strtok_r(NULL, "", &save_ptr_2);
275 if (!strcmp(key, "type")) {
277 } else if (!strcmp(key, "port_no")) {
278 port_no = atoi(value);
279 } else if (!smap_add_once(&args, key, value)) {
280 ovs_error(0, "duplicate \"%s\" option", key);
284 error = netdev_open(name, type, &netdev);
286 ovs_error(error, "%s: failed to open network device", name);
290 error = netdev_set_config(netdev, &args);
292 ovs_error(error, "%s: failed to configure network device", name);
296 error = dpif_port_add(dpif, netdev, &port_no);
298 ovs_error(error, "adding %s to %s failed", name, argv[1]);
305 netdev_close(netdev);
317 dpctl_set_if(int argc, char *argv[])
319 bool failure = false;
323 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
324 for (i = 2; i < argc; i++) {
325 struct netdev *netdev = NULL;
326 struct dpif_port dpif_port;
327 char *save_ptr = NULL;
335 name = strtok_r(argv[i], ",", &save_ptr);
337 ovs_error(0, "%s is not a valid network device name", argv[i]);
342 /* Get the port's type from the datapath. */
343 error = dpif_port_query_by_name(dpif, name, &dpif_port);
345 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
348 type = xstrdup(dpif_port.type);
349 port_no = dpif_port.port_no;
350 dpif_port_destroy(&dpif_port);
352 /* Retrieve its existing configuration. */
353 error = netdev_open(name, type, &netdev);
355 ovs_error(error, "%s: failed to open network device", name);
360 error = netdev_get_config(netdev, &args);
362 ovs_error(error, "%s: failed to fetch configuration", name);
366 /* Parse changes to configuration. */
367 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
368 char *save_ptr_2 = NULL;
371 key = strtok_r(option, "=", &save_ptr_2);
372 value = strtok_r(NULL, "", &save_ptr_2);
377 if (!strcmp(key, "type")) {
378 if (strcmp(value, type)) {
379 ovs_error(0, "%s: can't change type from %s to %s",
383 } else if (!strcmp(key, "port_no")) {
384 if (port_no != atoi(value)) {
385 ovs_error(0, "%s: can't change port number from "
387 name, port_no, atoi(value));
390 } else if (value[0] == '\0') {
391 smap_remove(&args, key);
393 smap_replace(&args, key, value);
397 /* Update configuration. */
398 error = netdev_set_config(netdev, &args);
401 ovs_error(error, "%s: failed to configure network device", name);
407 netdev_close(netdev);
419 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
421 struct dpif_port dpif_port;
423 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
424 *port = dpif_port.port_no;
425 dpif_port_destroy(&dpif_port);
428 ovs_error(0, "no port named %s", name);
434 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
436 bool failure = false;
440 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
441 for (i = 2; i < argc; i++) {
442 const char *name = argv[i];
446 if (!name[strspn(name, "0123456789")]) {
448 } else if (!get_port_number(dpif, name, &port)) {
453 error = dpif_port_del(dpif, port);
455 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
466 print_stat(const char *leader, uint64_t value)
468 fputs(leader, stdout);
469 if (value != UINT64_MAX) {
470 printf("%"PRIu64, value);
477 print_human_size(uint64_t value)
479 if (value == UINT64_MAX) {
481 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
482 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
483 } else if (value >= 1024ULL * 1024 * 1024) {
484 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
485 } else if (value >= 1024ULL * 1024) {
486 printf(" (%.1f MiB)", value / (1024.0 * 1024));
487 } else if (value >= 1024) {
488 printf(" (%.1f KiB)", value / 1024.0);
493 show_dpif(struct dpif *dpif)
495 struct dpif_port_dump dump;
496 struct dpif_port dpif_port;
497 struct dpif_dp_stats stats;
498 struct netdev *netdev;
500 printf("%s:\n", dpif_name(dpif));
501 if (!dpif_get_dp_stats(dpif, &stats)) {
502 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
503 "\tflows: %"PRIu64"\n",
504 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
506 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
507 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
509 if (strcmp(dpif_port.type, "system")) {
512 printf (" (%s", dpif_port.type);
514 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
519 error = netdev_get_config(netdev, &config);
521 const struct smap_node **nodes;
524 nodes = smap_sort(&config);
525 for (i = 0; i < smap_count(&config); i++) {
526 const struct smap_node *node = nodes[i];
527 printf("%c %s=%s", i ? ',' : ':', node->key,
532 printf(", could not retrieve configuration (%s)",
535 smap_destroy(&config);
537 netdev_close(netdev);
539 printf(": open failed (%s)", strerror(error));
545 if (print_statistics) {
546 struct netdev_stats s;
549 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
551 printf(", open failed (%s)", strerror(error));
554 error = netdev_get_stats(netdev, &s);
556 printf(", could not retrieve stats (%s)", strerror(error));
560 netdev_close(netdev);
561 print_stat("\t\tRX packets:", s.rx_packets);
562 print_stat(" errors:", s.rx_errors);
563 print_stat(" dropped:", s.rx_dropped);
564 print_stat(" overruns:", s.rx_over_errors);
565 print_stat(" frame:", s.rx_frame_errors);
568 print_stat("\t\tTX packets:", s.tx_packets);
569 print_stat(" errors:", s.tx_errors);
570 print_stat(" dropped:", s.tx_dropped);
571 print_stat(" aborted:", s.tx_aborted_errors);
572 print_stat(" carrier:", s.tx_carrier_errors);
575 print_stat("\t\tcollisions:", s.collisions);
578 print_stat("\t\tRX bytes:", s.rx_bytes);
579 print_human_size(s.rx_bytes);
580 print_stat(" TX bytes:", s.tx_bytes);
581 print_human_size(s.tx_bytes);
589 dpctl_show(int argc, char *argv[])
591 bool failure = false;
594 for (i = 1; i < argc; i++) {
595 const char *name = argv[i];
599 error = parsed_dpif_open(name, false, &dpif);
603 ovs_error(error, "opening datapath %s failed", name);
612 dp_enumerate_types(&types);
613 SSET_FOR_EACH (type, &types) {
618 if (dp_enumerate_names(type, &names)) {
622 SSET_FOR_EACH (name, &names) {
626 error = dpif_open(name, type, &dpif);
630 ovs_error(error, "opening datapath %s failed", name);
634 sset_destroy(&names);
636 sset_destroy(&types);
644 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
646 struct sset dpif_names, dpif_types;
650 sset_init(&dpif_names);
651 sset_init(&dpif_types);
652 dp_enumerate_types(&dpif_types);
654 SSET_FOR_EACH (type, &dpif_types) {
658 retval = dp_enumerate_names(type, &dpif_names);
663 SSET_FOR_EACH (name, &dpif_names) {
665 if (!dpif_open(name, type, &dpif)) {
666 printf("%s\n", dpif_name(dpif));
672 sset_destroy(&dpif_names);
673 sset_destroy(&dpif_types);
680 dpctl_dump_flows(int argc OVS_UNUSED, char *argv[])
682 const struct dpif_flow_stats *stats;
683 const struct nlattr *actions;
684 struct dpif_flow_dump dump;
685 const struct nlattr *key;
691 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
694 dpif_flow_dump_start(&dump, dpif);
695 while (dpif_flow_dump_next(&dump, &key, &key_len,
696 &actions, &actions_len, &stats)) {
698 odp_flow_key_format(key, key_len, &ds);
699 ds_put_cstr(&ds, ", ");
700 dpif_flow_stats_format(stats, &ds);
701 ds_put_cstr(&ds, ", actions:");
702 format_odp_actions(&ds, actions, actions_len);
703 printf("%s\n", ds_cstr(&ds));
705 dpif_flow_dump_done(&dump);
711 dpctl_del_flows(int argc OVS_UNUSED, char *argv[])
715 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
716 run(dpif_flow_flush(dpif), "deleting all flows");
721 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
726 /* Undocumented commands for unit testing. */
729 dpctl_parse_actions(int argc, char *argv[])
733 for (i = 1; i < argc; i++) {
734 struct ofpbuf actions;
737 ofpbuf_init(&actions, 0);
738 run(odp_actions_from_string(argv[i], NULL, &actions),
739 "odp_actions_from_string");
742 format_odp_actions(&s, actions.data, actions.size);
746 ofpbuf_uninit(&actions);
750 struct actions_for_flow {
751 struct hmap_node hmap_node;
753 struct ofpbuf actions;
756 static struct actions_for_flow *
757 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
759 uint32_t hash = flow_hash(flow, 0);
760 struct actions_for_flow *af;
762 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
763 if (flow_equal(&af->flow, flow)) {
768 af = xmalloc(sizeof *af);
770 ofpbuf_init(&af->actions, 0);
771 hmap_insert(actions_per_flow, &af->hmap_node, hash);
776 compare_actions_for_flow(const void *a_, const void *b_)
778 struct actions_for_flow *const *a = a_;
779 struct actions_for_flow *const *b = b_;
781 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
785 compare_output_actions(const void *a_, const void *b_)
787 const struct nlattr *a = a_;
788 const struct nlattr *b = b_;
789 uint32_t a_port = nl_attr_get_u32(a);
790 uint32_t b_port = nl_attr_get_u32(b);
792 return a_port < b_port ? -1 : a_port > b_port;
796 sort_output_actions__(struct nlattr *first, struct nlattr *end)
798 size_t bytes = (uint8_t *) end - (uint8_t *) first;
799 size_t n = bytes / NL_A_U32_SIZE;
801 assert(bytes % NL_A_U32_SIZE == 0);
802 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
806 sort_output_actions(struct nlattr *actions, size_t length)
808 struct nlattr *first_output = NULL;
812 NL_ATTR_FOR_EACH (a, left, actions, length) {
813 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
819 sort_output_actions__(first_output, a);
825 uint8_t *end = (uint8_t *) actions + length;
826 sort_output_actions__(first_output, (struct nlattr *) end);
830 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
831 * have the syntax used by "ovs-dpctl dump-flows".
833 * This command prints ACTIONS in a format that shows what happens for each
834 * VLAN, independent of the order of the ACTIONS. For example, there is more
835 * than one way to output a packet on VLANs 9 and 11, but this command will
836 * print the same output for any form.
838 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
839 * so far the implementation only covers VLANs. */
841 dpctl_normalize_actions(int argc, char *argv[])
843 struct simap port_names;
844 struct ofpbuf keybuf;
846 struct ofpbuf odp_actions;
847 struct hmap actions_per_flow;
848 struct actions_for_flow **afs;
849 struct actions_for_flow *af;
858 simap_init(&port_names);
859 for (i = 3; i < argc; i++) {
864 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
865 uintptr_t n = number;
866 simap_put(&port_names, name, n);
868 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
872 /* Parse flow key. */
873 ofpbuf_init(&keybuf, 0);
874 run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
875 "odp_flow_key_from_string");
878 odp_flow_key_format(keybuf.data, keybuf.size, &s);
879 printf("input flow: %s\n", ds_cstr(&s));
881 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
882 "odp_flow_key_to_flow");
883 ofpbuf_uninit(&keybuf);
886 ofpbuf_init(&odp_actions, 0);
887 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
888 "odp_actions_from_string");
892 format_odp_actions(&s, odp_actions.data, odp_actions.size);
893 printf("input actions: %s\n", ds_cstr(&s));
896 hmap_init(&actions_per_flow);
897 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
898 if (nl_attr_type(a) == OVS_ACTION_ATTR_POP_VLAN) {
899 flow.vlan_tci = htons(0);
903 if (nl_attr_type(a) == OVS_ACTION_ATTR_PUSH_VLAN) {
904 const struct ovs_action_push_vlan *push;
906 push = nl_attr_get_unspec(a, sizeof *push);
907 flow.vlan_tci = push->vlan_tci;
911 af = get_actions_for_flow(&actions_per_flow, &flow);
912 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
913 nl_attr_get(a), nl_attr_get_size(a));
916 n_afs = hmap_count(&actions_per_flow);
917 afs = xmalloc(n_afs * sizeof *afs);
919 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
924 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
926 for (i = 0; i < n_afs; i++) {
927 const struct actions_for_flow *af = afs[i];
929 sort_output_actions(af->actions.data, af->actions.size);
931 if (af->flow.vlan_tci != htons(0)) {
932 printf("vlan(vid=%"PRIu16",pcp=%d): ",
933 vlan_tci_to_vid(af->flow.vlan_tci),
934 vlan_tci_to_pcp(af->flow.vlan_tci));
940 format_odp_actions(&s, af->actions.data, af->actions.size);
946 static const struct command all_commands[] = {
947 { "add-dp", 1, INT_MAX, dpctl_add_dp },
948 { "del-dp", 1, 1, dpctl_del_dp },
949 { "add-if", 2, INT_MAX, dpctl_add_if },
950 { "del-if", 2, INT_MAX, dpctl_del_if },
951 { "set-if", 2, INT_MAX, dpctl_set_if },
952 { "dump-dps", 0, 0, dpctl_dump_dps },
953 { "show", 0, INT_MAX, dpctl_show },
954 { "dump-flows", 1, 1, dpctl_dump_flows },
955 { "del-flows", 1, 1, dpctl_del_flows },
956 { "help", 0, INT_MAX, dpctl_help },
958 /* Undocumented commands for testing. */
959 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
960 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
962 { NULL, 0, 0, NULL },