2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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"
51 VLOG_DEFINE_THIS_MODULE(dpctl);
53 /* -s, --statistics: Print port statistics? */
54 static bool print_statistics;
56 /* -m, --more: Output verbosity.
58 * So far only undocumented commands honor this option, so we don't document
59 * the option itself. */
62 static const struct command all_commands[];
64 static void usage(void) NO_RETURN;
65 static void parse_options(int argc, char *argv[]);
68 main(int argc, char *argv[])
70 set_program_name(argv[0]);
71 parse_options(argc, argv);
72 signal(SIGPIPE, SIG_IGN);
73 run_command(argc - optind, argv + optind, all_commands);
78 parse_options(int argc, char *argv[])
81 OPT_DUMMY = UCHAR_MAX + 1,
84 static struct option long_options[] = {
85 {"statistics", no_argument, NULL, 's'},
86 {"more", no_argument, NULL, 'm'},
87 {"timeout", required_argument, NULL, 't'},
88 {"help", no_argument, NULL, 'h'},
89 {"version", no_argument, NULL, 'V'},
93 char *short_options = long_options_to_short_options(long_options);
96 unsigned long int timeout;
99 c = getopt_long(argc, argv, short_options, long_options, NULL);
106 print_statistics = true;
114 timeout = strtoul(optarg, NULL, 10);
116 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
127 ovs_print_version(0, 0);
145 printf("%s: Open vSwitch datapath management utility\n"
146 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
147 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
148 " del-dp DP delete local datapath DP\n"
149 " add-if DP IFACE... add each IFACE as a port on DP\n"
150 " set-if DP IFACE... reconfigure each IFACE within DP\n"
151 " del-if DP IFACE... delete each IFACE from DP\n"
152 " dump-dps display names of all datapaths\n"
153 " show show basic info on all datapaths\n"
154 " show DP... show basic info on each DP\n"
155 " dump-flows DP display flows in DP\n"
156 " del-flows DP delete all flows from DP\n"
157 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
158 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
159 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
160 program_name, program_name);
162 printf("\nOther options:\n"
163 " -t, --timeout=SECS give up after SECS seconds\n"
164 " -h, --help display this help message\n"
165 " -V, --version display version information\n");
169 static void run(int retval, const char *message, ...)
172 static void run(int retval, const char *message, ...)
177 va_start(args, message);
178 ovs_fatal_valist(retval, message, args);
182 static void do_add_if(int argc, char *argv[]);
184 static int if_up(const char *netdev_name)
186 struct netdev *netdev;
189 retval = netdev_open(netdev_name, "system", &netdev);
191 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
192 netdev_close(netdev);
198 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
203 dp_parse_name(arg_, &name, &type);
206 result = dpif_create(name, type, dpifp);
208 result = dpif_open(name, type, dpifp);
217 do_add_dp(int argc OVS_UNUSED, char *argv[])
220 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
223 do_add_if(argc, argv);
228 do_del_dp(int argc OVS_UNUSED, char *argv[])
231 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
232 run(dpif_delete(dpif), "del_dp");
237 do_add_if(int argc OVS_UNUSED, char *argv[])
239 bool failure = false;
243 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
244 for (i = 2; i < argc; i++) {
245 const char *name, *type;
246 char *save_ptr = NULL;
247 struct netdev *netdev = NULL;
252 name = strtok_r(argv[i], ",", &save_ptr);
256 ovs_error(0, "%s is not a valid network device name", argv[i]);
262 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
263 char *save_ptr_2 = NULL;
266 key = strtok_r(option, "=", &save_ptr_2);
267 value = strtok_r(NULL, "", &save_ptr_2);
272 if (!strcmp(key, "type")) {
274 } else if (!shash_add_once(&args, key, value)) {
275 ovs_error(0, "duplicate \"%s\" option", key);
279 error = netdev_open(name, type, &netdev);
281 ovs_error(error, "%s: failed to open network device", name);
285 error = netdev_set_config(netdev, &args);
287 ovs_error(error, "%s: failed to configure network device", name);
291 error = dpif_port_add(dpif, netdev, NULL);
293 ovs_error(error, "adding %s to %s failed", name, argv[1]);
300 netdev_close(netdev);
312 do_set_if(int argc, char *argv[])
314 bool failure = false;
318 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
319 for (i = 2; i < argc; i++) {
320 struct netdev *netdev = NULL;
321 struct dpif_port dpif_port;
322 char *save_ptr = NULL;
329 name = strtok_r(argv[i], ",", &save_ptr);
331 ovs_error(0, "%s is not a valid network device name", argv[i]);
336 /* Get the port's type from the datapath. */
337 error = dpif_port_query_by_name(dpif, name, &dpif_port);
339 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
342 type = xstrdup(dpif_port.type);
343 dpif_port_destroy(&dpif_port);
345 /* Retrieve its existing configuration. */
346 error = netdev_open(name, type, &netdev);
348 ovs_error(error, "%s: failed to open network device", name);
353 error = netdev_get_config(netdev, &args);
355 ovs_error(error, "%s: failed to fetch configuration", name);
359 /* Parse changes to configuration. */
360 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
361 char *save_ptr_2 = NULL;
364 key = strtok_r(option, "=", &save_ptr_2);
365 value = strtok_r(NULL, "", &save_ptr_2);
370 if (!strcmp(key, "type")) {
371 if (strcmp(value, type)) {
372 ovs_error(0, "%s: can't change type from %s to %s",
376 } else if (value[0] == '\0') {
377 free(shash_find_and_delete(&args, key));
379 free(shash_replace(&args, key, xstrdup(value)));
383 /* Update configuration. */
384 error = netdev_set_config(netdev, &args);
387 ovs_error(error, "%s: failed to configure network device", name);
393 netdev_close(netdev);
405 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
407 struct dpif_port dpif_port;
409 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
410 *port = dpif_port.port_no;
411 dpif_port_destroy(&dpif_port);
414 ovs_error(0, "no port named %s", name);
420 do_del_if(int argc OVS_UNUSED, char *argv[])
422 bool failure = false;
426 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
427 for (i = 2; i < argc; i++) {
428 const char *name = argv[i];
432 if (!name[strspn(name, "0123456789")]) {
434 } else if (!get_port_number(dpif, name, &port)) {
439 error = dpif_port_del(dpif, port);
441 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
452 print_stat(const char *leader, uint64_t value)
454 fputs(leader, stdout);
455 if (value != UINT64_MAX) {
456 printf("%"PRIu64, value);
463 print_human_size(uint64_t value)
465 if (value == UINT64_MAX) {
467 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
468 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
469 } else if (value >= 1024ULL * 1024 * 1024) {
470 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
471 } else if (value >= 1024ULL * 1024) {
472 printf(" (%.1f MiB)", value / (1024.0 * 1024));
473 } else if (value >= 1024) {
474 printf(" (%.1f KiB)", value / 1024.0);
479 show_dpif(struct dpif *dpif)
481 struct dpif_port_dump dump;
482 struct dpif_port dpif_port;
483 struct dpif_dp_stats stats;
484 struct netdev *netdev;
486 printf("%s:\n", dpif_name(dpif));
487 if (!dpif_get_dp_stats(dpif, &stats)) {
488 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
489 "\tflows: %"PRIu64"\n",
490 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
492 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
493 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
495 if (strcmp(dpif_port.type, "system")) {
498 printf (" (%s", dpif_port.type);
500 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
505 error = netdev_get_config(netdev, &config);
507 const struct shash_node **nodes;
510 nodes = shash_sort(&config);
511 for (i = 0; i < shash_count(&config); i++) {
512 const struct shash_node *node = nodes[i];
513 printf("%c %s=%s", i ? ',' : ':',
514 node->name, (char *) node->data);
518 printf(", could not retrieve configuration (%s)",
521 shash_destroy_free_data(&config);
523 netdev_close(netdev);
525 printf(": open failed (%s)", strerror(error));
531 if (print_statistics) {
532 struct netdev_stats s;
535 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
537 printf(", open failed (%s)", strerror(error));
540 error = netdev_get_stats(netdev, &s);
542 printf(", could not retrieve stats (%s)", strerror(error));
546 netdev_close(netdev);
547 print_stat("\t\tRX packets:", s.rx_packets);
548 print_stat(" errors:", s.rx_errors);
549 print_stat(" dropped:", s.rx_dropped);
550 print_stat(" overruns:", s.rx_over_errors);
551 print_stat(" frame:", s.rx_frame_errors);
554 print_stat("\t\tTX packets:", s.tx_packets);
555 print_stat(" errors:", s.tx_errors);
556 print_stat(" dropped:", s.tx_dropped);
557 print_stat(" aborted:", s.tx_aborted_errors);
558 print_stat(" carrier:", s.tx_carrier_errors);
561 print_stat("\t\tcollisions:", s.collisions);
564 print_stat("\t\tRX bytes:", s.rx_bytes);
565 print_human_size(s.rx_bytes);
566 print_stat(" TX bytes:", s.tx_bytes);
567 print_human_size(s.tx_bytes);
575 do_show(int argc, char *argv[])
577 bool failure = false;
580 for (i = 1; i < argc; i++) {
581 const char *name = argv[i];
585 error = parsed_dpif_open(name, false, &dpif);
589 ovs_error(error, "opening datapath %s failed", name);
598 dp_enumerate_types(&types);
599 SSET_FOR_EACH (type, &types) {
604 if (dp_enumerate_names(type, &names)) {
608 SSET_FOR_EACH (name, &names) {
612 error = dpif_open(name, type, &dpif);
616 ovs_error(error, "opening datapath %s failed", name);
620 sset_destroy(&names);
622 sset_destroy(&types);
630 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
632 struct sset dpif_names, dpif_types;
636 sset_init(&dpif_names);
637 sset_init(&dpif_types);
638 dp_enumerate_types(&dpif_types);
640 SSET_FOR_EACH (type, &dpif_types) {
644 retval = dp_enumerate_names(type, &dpif_names);
649 SSET_FOR_EACH (name, &dpif_names) {
651 if (!dpif_open(name, type, &dpif)) {
652 printf("%s\n", dpif_name(dpif));
658 sset_destroy(&dpif_names);
659 sset_destroy(&dpif_types);
666 do_dump_flows(int argc OVS_UNUSED, char *argv[])
668 const struct dpif_flow_stats *stats;
669 const struct nlattr *actions;
670 struct dpif_flow_dump dump;
671 const struct nlattr *key;
677 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
680 dpif_flow_dump_start(&dump, dpif);
681 while (dpif_flow_dump_next(&dump, &key, &key_len,
682 &actions, &actions_len, &stats)) {
684 odp_flow_key_format(key, key_len, &ds);
685 ds_put_cstr(&ds, ", ");
686 dpif_flow_stats_format(stats, &ds);
687 ds_put_cstr(&ds, ", actions:");
688 format_odp_actions(&ds, actions, actions_len);
689 printf("%s\n", ds_cstr(&ds));
691 dpif_flow_dump_done(&dump);
697 do_del_flows(int argc OVS_UNUSED, char *argv[])
701 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
702 run(dpif_flow_flush(dpif), "deleting all flows");
707 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
712 /* Undocumented commands for unit testing. */
715 do_parse_actions(int argc, char *argv[])
719 for (i = 1; i < argc; i++) {
720 struct ofpbuf actions;
723 ofpbuf_init(&actions, 0);
724 run(odp_actions_from_string(argv[i], NULL, &actions),
725 "odp_actions_from_string");
728 format_odp_actions(&s, actions.data, actions.size);
732 ofpbuf_uninit(&actions);
736 struct actions_for_flow {
737 struct hmap_node hmap_node;
739 struct ofpbuf actions;
742 static struct actions_for_flow *
743 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
745 uint32_t hash = flow_hash(flow, 0);
746 struct actions_for_flow *af;
748 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
749 if (flow_equal(&af->flow, flow)) {
754 af = xmalloc(sizeof *af);
756 ofpbuf_init(&af->actions, 0);
757 hmap_insert(actions_per_flow, &af->hmap_node, hash);
762 compare_actions_for_flow(const void *a_, const void *b_)
764 struct actions_for_flow *const *a = a_;
765 struct actions_for_flow *const *b = b_;
767 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
771 compare_output_actions(const void *a_, const void *b_)
773 const struct nlattr *a = a_;
774 const struct nlattr *b = b_;
775 uint32_t a_port = nl_attr_get_u32(a);
776 uint32_t b_port = nl_attr_get_u32(b);
778 return a_port < b_port ? -1 : a_port > b_port;
782 sort_output_actions__(struct nlattr *first, struct nlattr *end)
784 size_t bytes = (uint8_t *) end - (uint8_t *) first;
785 size_t n = bytes / NL_A_U32_SIZE;
787 assert(bytes % NL_A_U32_SIZE == 0);
788 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
792 sort_output_actions(struct nlattr *actions, size_t length)
794 struct nlattr *first_output = NULL;
798 NL_ATTR_FOR_EACH (a, left, actions, length) {
799 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
805 sort_output_actions__(first_output, a);
811 uint8_t *end = (uint8_t *) actions + length;
812 sort_output_actions__(first_output, (struct nlattr *) end);
816 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
817 * have the syntax used by "ovs-dpctl dump-flows".
819 * This command prints ACTIONS in a format that shows what happens for each
820 * VLAN, independent of the order of the ACTIONS. For example, there is more
821 * than one way to output a packet on VLANs 9 and 11, but this command will
822 * print the same output for any form.
824 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
825 * so far the implementation only covers VLANs. */
827 do_normalize_actions(int argc, char *argv[])
829 struct shash port_names;
830 struct ofpbuf keybuf;
832 struct ofpbuf odp_actions;
833 struct hmap actions_per_flow;
834 struct actions_for_flow **afs;
835 struct actions_for_flow *af;
844 shash_init(&port_names);
845 for (i = 3; i < argc; i++) {
850 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
851 uintptr_t n = number;
852 shash_add(&port_names, name, (void *) n);
854 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
858 /* Parse flow key. */
859 ofpbuf_init(&keybuf, 0);
860 run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
861 "odp_flow_key_from_string");
864 odp_flow_key_format(keybuf.data, keybuf.size, &s);
865 printf("input flow: %s\n", ds_cstr(&s));
867 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
868 "odp_flow_key_to_flow");
869 ofpbuf_uninit(&keybuf);
872 ofpbuf_init(&odp_actions, 0);
873 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
874 "odp_actions_from_string");
878 format_odp_actions(&s, odp_actions.data, odp_actions.size);
879 printf("input actions: %s\n", ds_cstr(&s));
882 hmap_init(&actions_per_flow);
883 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
884 if (nl_attr_type(a) == OVS_ACTION_ATTR_POP_VLAN) {
885 flow.vlan_tci = htons(0);
889 if (nl_attr_type(a) == OVS_ACTION_ATTR_PUSH_VLAN) {
890 const struct ovs_action_push_vlan *push;
892 push = nl_attr_get_unspec(a, sizeof *push);
893 flow.vlan_tci = push->vlan_tci;
897 af = get_actions_for_flow(&actions_per_flow, &flow);
898 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
899 nl_attr_get(a), nl_attr_get_size(a));
902 n_afs = hmap_count(&actions_per_flow);
903 afs = xmalloc(n_afs * sizeof *afs);
905 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
910 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
912 for (i = 0; i < n_afs; i++) {
913 const struct actions_for_flow *af = afs[i];
915 sort_output_actions(af->actions.data, af->actions.size);
917 if (af->flow.vlan_tci != htons(0)) {
918 printf("vlan(vid=%"PRIu16",pcp=%d): ",
919 vlan_tci_to_vid(af->flow.vlan_tci),
920 vlan_tci_to_pcp(af->flow.vlan_tci));
926 format_odp_actions(&s, af->actions.data, af->actions.size);
932 static const struct command all_commands[] = {
933 { "add-dp", 1, INT_MAX, do_add_dp },
934 { "del-dp", 1, 1, do_del_dp },
935 { "add-if", 2, INT_MAX, do_add_if },
936 { "del-if", 2, INT_MAX, do_del_if },
937 { "set-if", 2, INT_MAX, do_set_if },
938 { "dump-dps", 0, 0, do_dump_dps },
939 { "show", 0, INT_MAX, do_show },
940 { "dump-flows", 1, 1, do_dump_flows },
941 { "del-flows", 1, 1, do_del_flows },
942 { "help", 0, INT_MAX, do_help },
944 /* Undocumented commands for testing. */
945 { "parse-actions", 1, INT_MAX, do_parse_actions },
946 { "normalize-actions", 2, INT_MAX, do_normalize_actions },
948 { NULL, 0, 0, NULL },