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>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
33 #include "command-line.h"
37 #include "dynamic-string.h"
52 VLOG_DEFINE_THIS_MODULE(dpctl);
54 /* -s, --statistics: Print port statistics? */
55 static bool print_statistics;
57 /* -m, --more: Output verbosity.
59 * So far only undocumented commands honor this option, so we don't document
60 * the option itself. */
63 static const struct command all_commands[];
65 static void usage(void) NO_RETURN;
66 static void parse_options(int argc, char *argv[]);
69 main(int argc, char *argv[])
71 set_program_name(argv[0]);
72 parse_options(argc, argv);
73 signal(SIGPIPE, SIG_IGN);
74 run_command(argc - optind, argv + optind, all_commands);
79 parse_options(int argc, char *argv[])
82 OPT_DUMMY = UCHAR_MAX + 1,
85 static struct option long_options[] = {
86 {"statistics", no_argument, NULL, 's'},
87 {"more", no_argument, NULL, 'm'},
88 {"timeout", required_argument, NULL, 't'},
89 {"help", no_argument, NULL, 'h'},
90 {"version", no_argument, NULL, 'V'},
94 char *short_options = long_options_to_short_options(long_options);
97 unsigned long int timeout;
100 c = getopt_long(argc, argv, short_options, long_options, NULL);
107 print_statistics = true;
115 timeout = strtoul(optarg, NULL, 10);
117 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
128 ovs_print_version(0, 0);
146 printf("%s: Open vSwitch datapath management utility\n"
147 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
148 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
149 " del-dp DP delete local datapath DP\n"
150 " add-if DP IFACE... add each IFACE as a port on DP\n"
151 " set-if DP IFACE... reconfigure each IFACE within DP\n"
152 " del-if DP IFACE... delete each IFACE from DP\n"
153 " dump-dps display names of all datapaths\n"
154 " show show basic info on all datapaths\n"
155 " show DP... show basic info on each DP\n"
156 " dump-flows DP display flows in DP\n"
157 " del-flows DP delete all flows from DP\n"
158 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
159 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
160 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
161 program_name, program_name);
163 printf("\nOptions for show:\n"
164 " -s, --statistics print port statistics\n"
166 " -t, --timeout=SECS give up after SECS seconds\n"
167 " -h, --help display this help message\n"
168 " -V, --version display version information\n");
172 static void run(int retval, const char *message, ...)
175 static void run(int retval, const char *message, ...)
180 va_start(args, message);
181 ovs_fatal_valist(retval, message, args);
185 static void dpctl_add_if(int argc, char *argv[]);
187 static int if_up(const char *netdev_name)
189 struct netdev *netdev;
192 retval = netdev_open(netdev_name, "system", &netdev);
194 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
195 netdev_close(netdev);
200 /* Retrieve the name of the datapath if exactly one exists. The caller
201 * is responsible for freeing the returned string. If there is not one
202 * datapath, aborts with an error message. */
208 char *dp_name = NULL;
212 dp_enumerate_types(&types);
213 SSET_FOR_EACH (type, &types) {
217 if (!dp_enumerate_names(type, &names)) {
218 count += sset_count(&names);
219 if (!dp_name && count == 1) {
220 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
223 sset_destroy(&names);
225 sset_destroy(&types);
228 ovs_fatal(0, "no datapaths exist");
229 } else if (count > 1) {
230 ovs_fatal(0, "multiple datapaths, specify one");
237 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
242 dp_parse_name(arg_, &name, &type);
245 result = dpif_create(name, type, dpifp);
247 result = dpif_open(name, type, dpifp);
256 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
259 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
262 dpctl_add_if(argc, argv);
267 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
270 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
271 run(dpif_delete(dpif), "del_dp");
276 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
278 bool failure = false;
282 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
283 for (i = 2; i < argc; i++) {
284 const char *name, *type;
285 char *save_ptr = NULL;
286 struct netdev *netdev = NULL;
288 uint32_t port_no = UINT32_MAX;
292 name = strtok_r(argv[i], ",", &save_ptr);
296 ovs_error(0, "%s is not a valid network device name", argv[i]);
302 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
303 char *save_ptr_2 = NULL;
306 key = strtok_r(option, "=", &save_ptr_2);
307 value = strtok_r(NULL, "", &save_ptr_2);
312 if (!strcmp(key, "type")) {
314 } else if (!strcmp(key, "port_no")) {
315 port_no = atoi(value);
316 } else if (!smap_add_once(&args, key, value)) {
317 ovs_error(0, "duplicate \"%s\" option", key);
321 error = netdev_open(name, type, &netdev);
323 ovs_error(error, "%s: failed to open network device", name);
327 error = netdev_set_config(netdev, &args);
329 ovs_error(error, "%s: failed to configure network device", name);
333 error = dpif_port_add(dpif, netdev, &port_no);
335 ovs_error(error, "adding %s to %s failed", name, argv[1]);
342 netdev_close(netdev);
354 dpctl_set_if(int argc, char *argv[])
356 bool failure = false;
360 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
361 for (i = 2; i < argc; i++) {
362 struct netdev *netdev = NULL;
363 struct dpif_port dpif_port;
364 char *save_ptr = NULL;
372 name = strtok_r(argv[i], ",", &save_ptr);
374 ovs_error(0, "%s is not a valid network device name", argv[i]);
379 /* Get the port's type from the datapath. */
380 error = dpif_port_query_by_name(dpif, name, &dpif_port);
382 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
385 type = xstrdup(dpif_port.type);
386 port_no = dpif_port.port_no;
387 dpif_port_destroy(&dpif_port);
389 /* Retrieve its existing configuration. */
390 error = netdev_open(name, type, &netdev);
392 ovs_error(error, "%s: failed to open network device", name);
397 error = netdev_get_config(netdev, &args);
399 ovs_error(error, "%s: failed to fetch configuration", name);
403 /* Parse changes to configuration. */
404 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
405 char *save_ptr_2 = NULL;
408 key = strtok_r(option, "=", &save_ptr_2);
409 value = strtok_r(NULL, "", &save_ptr_2);
414 if (!strcmp(key, "type")) {
415 if (strcmp(value, type)) {
416 ovs_error(0, "%s: can't change type from %s to %s",
420 } else if (!strcmp(key, "port_no")) {
421 if (port_no != atoi(value)) {
422 ovs_error(0, "%s: can't change port number from "
424 name, port_no, atoi(value));
427 } else if (value[0] == '\0') {
428 smap_remove(&args, key);
430 smap_replace(&args, key, value);
434 /* Update configuration. */
435 error = netdev_set_config(netdev, &args);
438 ovs_error(error, "%s: failed to configure network device", name);
444 netdev_close(netdev);
456 get_port_number(struct dpif *dpif, const char *name, uint32_t *port)
458 struct dpif_port dpif_port;
460 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
461 *port = dpif_port.port_no;
462 dpif_port_destroy(&dpif_port);
465 ovs_error(0, "no port named %s", name);
471 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
473 bool failure = false;
477 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
478 for (i = 2; i < argc; i++) {
479 const char *name = argv[i];
483 if (!name[strspn(name, "0123456789")]) {
485 } else if (!get_port_number(dpif, name, &port)) {
490 error = dpif_port_del(dpif, port);
492 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
503 print_stat(const char *leader, uint64_t value)
505 fputs(leader, stdout);
506 if (value != UINT64_MAX) {
507 printf("%"PRIu64, value);
514 print_human_size(uint64_t value)
516 if (value == UINT64_MAX) {
518 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
519 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
520 } else if (value >= 1024ULL * 1024 * 1024) {
521 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
522 } else if (value >= 1024ULL * 1024) {
523 printf(" (%.1f MiB)", value / (1024.0 * 1024));
524 } else if (value >= 1024) {
525 printf(" (%.1f KiB)", value / 1024.0);
530 show_dpif(struct dpif *dpif)
532 struct dpif_port_dump dump;
533 struct dpif_port dpif_port;
534 struct dpif_dp_stats stats;
535 struct netdev *netdev;
537 printf("%s:\n", dpif_name(dpif));
538 if (!dpif_get_dp_stats(dpif, &stats)) {
539 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
540 "\tflows: %"PRIu64"\n",
541 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
543 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
544 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
546 if (strcmp(dpif_port.type, "system")) {
549 printf (" (%s", dpif_port.type);
551 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
556 error = netdev_get_config(netdev, &config);
558 const struct smap_node **nodes;
561 nodes = smap_sort(&config);
562 for (i = 0; i < smap_count(&config); i++) {
563 const struct smap_node *node = nodes[i];
564 printf("%c %s=%s", i ? ',' : ':', node->key,
569 printf(", could not retrieve configuration (%s)",
572 smap_destroy(&config);
574 netdev_close(netdev);
576 printf(": open failed (%s)", strerror(error));
582 if (print_statistics) {
583 struct netdev_stats s;
586 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
588 printf(", open failed (%s)", strerror(error));
591 error = netdev_get_stats(netdev, &s);
593 printf(", could not retrieve stats (%s)", strerror(error));
597 netdev_close(netdev);
598 print_stat("\t\tRX packets:", s.rx_packets);
599 print_stat(" errors:", s.rx_errors);
600 print_stat(" dropped:", s.rx_dropped);
601 print_stat(" overruns:", s.rx_over_errors);
602 print_stat(" frame:", s.rx_frame_errors);
605 print_stat("\t\tTX packets:", s.tx_packets);
606 print_stat(" errors:", s.tx_errors);
607 print_stat(" dropped:", s.tx_dropped);
608 print_stat(" aborted:", s.tx_aborted_errors);
609 print_stat(" carrier:", s.tx_carrier_errors);
612 print_stat("\t\tcollisions:", s.collisions);
615 print_stat("\t\tRX bytes:", s.rx_bytes);
616 print_human_size(s.rx_bytes);
617 print_stat(" TX bytes:", s.tx_bytes);
618 print_human_size(s.tx_bytes);
626 dpctl_show(int argc, char *argv[])
628 bool failure = false;
631 for (i = 1; i < argc; i++) {
632 const char *name = argv[i];
636 error = parsed_dpif_open(name, false, &dpif);
640 ovs_error(error, "opening datapath %s failed", name);
649 dp_enumerate_types(&types);
650 SSET_FOR_EACH (type, &types) {
655 if (dp_enumerate_names(type, &names)) {
659 SSET_FOR_EACH (name, &names) {
663 error = dpif_open(name, type, &dpif);
667 ovs_error(error, "opening datapath %s failed", name);
671 sset_destroy(&names);
673 sset_destroy(&types);
681 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
683 struct sset dpif_names, dpif_types;
687 sset_init(&dpif_names);
688 sset_init(&dpif_types);
689 dp_enumerate_types(&dpif_types);
691 SSET_FOR_EACH (type, &dpif_types) {
695 retval = dp_enumerate_names(type, &dpif_names);
700 SSET_FOR_EACH (name, &dpif_names) {
702 if (!dpif_open(name, type, &dpif)) {
703 printf("%s\n", dpif_name(dpif));
709 sset_destroy(&dpif_names);
710 sset_destroy(&dpif_types);
717 dpctl_dump_flows(int argc, char *argv[])
719 const struct dpif_flow_stats *stats;
720 const struct nlattr *actions;
721 struct dpif_flow_dump dump;
722 const struct nlattr *key;
729 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
730 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
734 dpif_flow_dump_start(&dump, dpif);
735 while (dpif_flow_dump_next(&dump, &key, &key_len,
736 &actions, &actions_len, &stats)) {
738 odp_flow_key_format(key, key_len, &ds);
739 ds_put_cstr(&ds, ", ");
740 dpif_flow_stats_format(stats, &ds);
741 ds_put_cstr(&ds, ", actions:");
742 format_odp_actions(&ds, actions, actions_len);
743 printf("%s\n", ds_cstr(&ds));
745 dpif_flow_dump_done(&dump);
751 dpctl_del_flows(int argc, char *argv[])
756 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
757 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
760 run(dpif_flow_flush(dpif), "deleting all flows");
765 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
770 /* Undocumented commands for unit testing. */
773 dpctl_parse_actions(int argc, char *argv[])
777 for (i = 1; i < argc; i++) {
778 struct ofpbuf actions;
781 ofpbuf_init(&actions, 0);
782 run(odp_actions_from_string(argv[i], NULL, &actions),
783 "odp_actions_from_string");
786 format_odp_actions(&s, actions.data, actions.size);
790 ofpbuf_uninit(&actions);
794 struct actions_for_flow {
795 struct hmap_node hmap_node;
797 struct ofpbuf actions;
800 static struct actions_for_flow *
801 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
803 uint32_t hash = flow_hash(flow, 0);
804 struct actions_for_flow *af;
806 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
807 if (flow_equal(&af->flow, flow)) {
812 af = xmalloc(sizeof *af);
814 ofpbuf_init(&af->actions, 0);
815 hmap_insert(actions_per_flow, &af->hmap_node, hash);
820 compare_actions_for_flow(const void *a_, const void *b_)
822 struct actions_for_flow *const *a = a_;
823 struct actions_for_flow *const *b = b_;
825 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
829 compare_output_actions(const void *a_, const void *b_)
831 const struct nlattr *a = a_;
832 const struct nlattr *b = b_;
833 uint32_t a_port = nl_attr_get_u32(a);
834 uint32_t b_port = nl_attr_get_u32(b);
836 return a_port < b_port ? -1 : a_port > b_port;
840 sort_output_actions__(struct nlattr *first, struct nlattr *end)
842 size_t bytes = (uint8_t *) end - (uint8_t *) first;
843 size_t n = bytes / NL_A_U32_SIZE;
845 ovs_assert(bytes % NL_A_U32_SIZE == 0);
846 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
850 sort_output_actions(struct nlattr *actions, size_t length)
852 struct nlattr *first_output = NULL;
856 NL_ATTR_FOR_EACH (a, left, actions, length) {
857 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
863 sort_output_actions__(first_output, a);
869 uint8_t *end = (uint8_t *) actions + length;
870 sort_output_actions__(first_output, (struct nlattr *) end);
874 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
875 * have the syntax used by "ovs-dpctl dump-flows".
877 * This command prints ACTIONS in a format that shows what happens for each
878 * VLAN, independent of the order of the ACTIONS. For example, there is more
879 * than one way to output a packet on VLANs 9 and 11, but this command will
880 * print the same output for any form.
882 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
883 * so far the implementation only covers VLANs. */
885 dpctl_normalize_actions(int argc, char *argv[])
887 struct simap port_names;
888 struct ofpbuf keybuf;
890 struct ofpbuf odp_actions;
891 struct hmap actions_per_flow;
892 struct actions_for_flow **afs;
893 struct actions_for_flow *af;
902 simap_init(&port_names);
903 for (i = 3; i < argc; i++) {
908 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
909 uintptr_t n = number;
910 simap_put(&port_names, name, n);
912 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
916 /* Parse flow key. */
917 ofpbuf_init(&keybuf, 0);
918 run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
919 "odp_flow_key_from_string");
922 odp_flow_key_format(keybuf.data, keybuf.size, &s);
923 printf("input flow: %s\n", ds_cstr(&s));
925 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
926 "odp_flow_key_to_flow");
927 ofpbuf_uninit(&keybuf);
930 ofpbuf_init(&odp_actions, 0);
931 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
932 "odp_actions_from_string");
936 format_odp_actions(&s, odp_actions.data, odp_actions.size);
937 printf("input actions: %s\n", ds_cstr(&s));
940 hmap_init(&actions_per_flow);
941 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
942 const struct ovs_action_push_vlan *push;
943 switch(nl_attr_type(a)) {
944 case OVS_ACTION_ATTR_POP_VLAN:
945 flow.vlan_tci = htons(0);
948 case OVS_ACTION_ATTR_PUSH_VLAN:
949 push = nl_attr_get_unspec(a, sizeof *push);
950 flow.vlan_tci = push->vlan_tci;
954 af = get_actions_for_flow(&actions_per_flow, &flow);
955 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
956 nl_attr_get(a), nl_attr_get_size(a));
959 n_afs = hmap_count(&actions_per_flow);
960 afs = xmalloc(n_afs * sizeof *afs);
962 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
965 ovs_assert(i == n_afs);
967 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
969 for (i = 0; i < n_afs; i++) {
970 const struct actions_for_flow *af = afs[i];
972 sort_output_actions(af->actions.data, af->actions.size);
974 if (af->flow.vlan_tci != htons(0)) {
975 printf("vlan(vid=%"PRIu16",pcp=%d): ",
976 vlan_tci_to_vid(af->flow.vlan_tci),
977 vlan_tci_to_pcp(af->flow.vlan_tci));
982 if (af->flow.mpls_depth) {
983 printf("mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
984 mpls_lse_to_label(af->flow.mpls_lse),
985 mpls_lse_to_tc(af->flow.mpls_lse),
986 mpls_lse_to_ttl(af->flow.mpls_lse));
992 format_odp_actions(&s, af->actions.data, af->actions.size);
998 static const struct command all_commands[] = {
999 { "add-dp", 1, INT_MAX, dpctl_add_dp },
1000 { "del-dp", 1, 1, dpctl_del_dp },
1001 { "add-if", 2, INT_MAX, dpctl_add_if },
1002 { "del-if", 2, INT_MAX, dpctl_del_if },
1003 { "set-if", 2, INT_MAX, dpctl_set_if },
1004 { "dump-dps", 0, 0, dpctl_dump_dps },
1005 { "show", 0, INT_MAX, dpctl_show },
1006 { "dump-flows", 0, 1, dpctl_dump_flows },
1007 { "del-flows", 0, 1, dpctl_del_flows },
1008 { "help", 0, INT_MAX, dpctl_help },
1010 /* Undocumented commands for testing. */
1011 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1012 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1014 { NULL, 0, 0, NULL },