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);
199 /* Retrieve the name of the datapath if exactly one exists. The caller
200 * is responsible for freeing the returned string. If there is not one
201 * datapath, aborts with an error message. */
207 char *dp_name = NULL;
211 dp_enumerate_types(&types);
212 SSET_FOR_EACH (type, &types) {
216 if (!dp_enumerate_names(type, &names)) {
217 count += sset_count(&names);
218 if (!dp_name && count == 1) {
219 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
222 sset_destroy(&names);
224 sset_destroy(&types);
227 ovs_fatal(0, "no datapaths exist");
228 } else if (count > 1) {
229 ovs_fatal(0, "multiple datapaths, specify one");
236 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
241 dp_parse_name(arg_, &name, &type);
244 result = dpif_create(name, type, dpifp);
246 result = dpif_open(name, type, dpifp);
255 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
258 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
261 dpctl_add_if(argc, argv);
266 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
269 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
270 run(dpif_delete(dpif), "del_dp");
275 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
277 bool failure = false;
281 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
282 for (i = 2; i < argc; i++) {
283 const char *name, *type;
284 char *save_ptr = NULL;
285 struct netdev *netdev = NULL;
287 uint32_t port_no = UINT32_MAX;
291 name = strtok_r(argv[i], ",", &save_ptr);
295 ovs_error(0, "%s is not a valid network device name", argv[i]);
301 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
302 char *save_ptr_2 = NULL;
305 key = strtok_r(option, "=", &save_ptr_2);
306 value = strtok_r(NULL, "", &save_ptr_2);
311 if (!strcmp(key, "type")) {
313 } else if (!strcmp(key, "port_no")) {
314 port_no = atoi(value);
315 } else if (!smap_add_once(&args, key, value)) {
316 ovs_error(0, "duplicate \"%s\" option", key);
320 error = netdev_open(name, type, &netdev);
322 ovs_error(error, "%s: failed to open network device", name);
326 error = netdev_set_config(netdev, &args);
328 ovs_error(error, "%s: failed to configure network device", name);
332 error = dpif_port_add(dpif, netdev, &port_no);
334 ovs_error(error, "adding %s to %s failed", name, argv[1]);
341 netdev_close(netdev);
353 dpctl_set_if(int argc, char *argv[])
355 bool failure = false;
359 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
360 for (i = 2; i < argc; i++) {
361 struct netdev *netdev = NULL;
362 struct dpif_port dpif_port;
363 char *save_ptr = NULL;
371 name = strtok_r(argv[i], ",", &save_ptr);
373 ovs_error(0, "%s is not a valid network device name", argv[i]);
378 /* Get the port's type from the datapath. */
379 error = dpif_port_query_by_name(dpif, name, &dpif_port);
381 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
384 type = xstrdup(dpif_port.type);
385 port_no = dpif_port.port_no;
386 dpif_port_destroy(&dpif_port);
388 /* Retrieve its existing configuration. */
389 error = netdev_open(name, type, &netdev);
391 ovs_error(error, "%s: failed to open network device", name);
396 error = netdev_get_config(netdev, &args);
398 ovs_error(error, "%s: failed to fetch configuration", name);
402 /* Parse changes to configuration. */
403 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
404 char *save_ptr_2 = NULL;
407 key = strtok_r(option, "=", &save_ptr_2);
408 value = strtok_r(NULL, "", &save_ptr_2);
413 if (!strcmp(key, "type")) {
414 if (strcmp(value, type)) {
415 ovs_error(0, "%s: can't change type from %s to %s",
419 } else if (!strcmp(key, "port_no")) {
420 if (port_no != atoi(value)) {
421 ovs_error(0, "%s: can't change port number from "
423 name, port_no, atoi(value));
426 } else if (value[0] == '\0') {
427 smap_remove(&args, key);
429 smap_replace(&args, key, value);
433 /* Update configuration. */
434 error = netdev_set_config(netdev, &args);
437 ovs_error(error, "%s: failed to configure network device", name);
443 netdev_close(netdev);
455 get_port_number(struct dpif *dpif, const char *name, uint32_t *port)
457 struct dpif_port dpif_port;
459 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
460 *port = dpif_port.port_no;
461 dpif_port_destroy(&dpif_port);
464 ovs_error(0, "no port named %s", name);
470 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
472 bool failure = false;
476 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
477 for (i = 2; i < argc; i++) {
478 const char *name = argv[i];
482 if (!name[strspn(name, "0123456789")]) {
484 } else if (!get_port_number(dpif, name, &port)) {
489 error = dpif_port_del(dpif, port);
491 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
502 print_stat(const char *leader, uint64_t value)
504 fputs(leader, stdout);
505 if (value != UINT64_MAX) {
506 printf("%"PRIu64, value);
513 print_human_size(uint64_t value)
515 if (value == UINT64_MAX) {
517 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
518 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
519 } else if (value >= 1024ULL * 1024 * 1024) {
520 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
521 } else if (value >= 1024ULL * 1024) {
522 printf(" (%.1f MiB)", value / (1024.0 * 1024));
523 } else if (value >= 1024) {
524 printf(" (%.1f KiB)", value / 1024.0);
529 show_dpif(struct dpif *dpif)
531 struct dpif_port_dump dump;
532 struct dpif_port dpif_port;
533 struct dpif_dp_stats stats;
534 struct netdev *netdev;
536 printf("%s:\n", dpif_name(dpif));
537 if (!dpif_get_dp_stats(dpif, &stats)) {
538 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
539 "\tflows: %"PRIu64"\n",
540 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
542 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
543 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
545 if (strcmp(dpif_port.type, "system")) {
548 printf (" (%s", dpif_port.type);
550 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
555 error = netdev_get_config(netdev, &config);
557 const struct smap_node **nodes;
560 nodes = smap_sort(&config);
561 for (i = 0; i < smap_count(&config); i++) {
562 const struct smap_node *node = nodes[i];
563 printf("%c %s=%s", i ? ',' : ':', node->key,
568 printf(", could not retrieve configuration (%s)",
571 smap_destroy(&config);
573 netdev_close(netdev);
575 printf(": open failed (%s)", strerror(error));
581 if (print_statistics) {
582 struct netdev_stats s;
585 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
587 printf(", open failed (%s)", strerror(error));
590 error = netdev_get_stats(netdev, &s);
592 printf(", could not retrieve stats (%s)", strerror(error));
596 netdev_close(netdev);
597 print_stat("\t\tRX packets:", s.rx_packets);
598 print_stat(" errors:", s.rx_errors);
599 print_stat(" dropped:", s.rx_dropped);
600 print_stat(" overruns:", s.rx_over_errors);
601 print_stat(" frame:", s.rx_frame_errors);
604 print_stat("\t\tTX packets:", s.tx_packets);
605 print_stat(" errors:", s.tx_errors);
606 print_stat(" dropped:", s.tx_dropped);
607 print_stat(" aborted:", s.tx_aborted_errors);
608 print_stat(" carrier:", s.tx_carrier_errors);
611 print_stat("\t\tcollisions:", s.collisions);
614 print_stat("\t\tRX bytes:", s.rx_bytes);
615 print_human_size(s.rx_bytes);
616 print_stat(" TX bytes:", s.tx_bytes);
617 print_human_size(s.tx_bytes);
625 dpctl_show(int argc, char *argv[])
627 bool failure = false;
630 for (i = 1; i < argc; i++) {
631 const char *name = argv[i];
635 error = parsed_dpif_open(name, false, &dpif);
639 ovs_error(error, "opening datapath %s failed", name);
648 dp_enumerate_types(&types);
649 SSET_FOR_EACH (type, &types) {
654 if (dp_enumerate_names(type, &names)) {
658 SSET_FOR_EACH (name, &names) {
662 error = dpif_open(name, type, &dpif);
666 ovs_error(error, "opening datapath %s failed", name);
670 sset_destroy(&names);
672 sset_destroy(&types);
680 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
682 struct sset dpif_names, dpif_types;
686 sset_init(&dpif_names);
687 sset_init(&dpif_types);
688 dp_enumerate_types(&dpif_types);
690 SSET_FOR_EACH (type, &dpif_types) {
694 retval = dp_enumerate_names(type, &dpif_names);
699 SSET_FOR_EACH (name, &dpif_names) {
701 if (!dpif_open(name, type, &dpif)) {
702 printf("%s\n", dpif_name(dpif));
708 sset_destroy(&dpif_names);
709 sset_destroy(&dpif_types);
716 dpctl_dump_flows(int argc, char *argv[])
718 const struct dpif_flow_stats *stats;
719 const struct nlattr *actions;
720 struct dpif_flow_dump dump;
721 const struct nlattr *key;
728 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
729 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
733 dpif_flow_dump_start(&dump, dpif);
734 while (dpif_flow_dump_next(&dump, &key, &key_len,
735 &actions, &actions_len, &stats)) {
737 odp_flow_key_format(key, key_len, &ds);
738 ds_put_cstr(&ds, ", ");
739 dpif_flow_stats_format(stats, &ds);
740 ds_put_cstr(&ds, ", actions:");
741 format_odp_actions(&ds, actions, actions_len);
742 printf("%s\n", ds_cstr(&ds));
744 dpif_flow_dump_done(&dump);
750 dpctl_del_flows(int argc, char *argv[])
755 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
756 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
759 run(dpif_flow_flush(dpif), "deleting all flows");
764 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
769 /* Undocumented commands for unit testing. */
772 dpctl_parse_actions(int argc, char *argv[])
776 for (i = 1; i < argc; i++) {
777 struct ofpbuf actions;
780 ofpbuf_init(&actions, 0);
781 run(odp_actions_from_string(argv[i], NULL, &actions),
782 "odp_actions_from_string");
785 format_odp_actions(&s, actions.data, actions.size);
789 ofpbuf_uninit(&actions);
793 struct actions_for_flow {
794 struct hmap_node hmap_node;
796 struct ofpbuf actions;
799 static struct actions_for_flow *
800 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
802 uint32_t hash = flow_hash(flow, 0);
803 struct actions_for_flow *af;
805 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
806 if (flow_equal(&af->flow, flow)) {
811 af = xmalloc(sizeof *af);
813 ofpbuf_init(&af->actions, 0);
814 hmap_insert(actions_per_flow, &af->hmap_node, hash);
819 compare_actions_for_flow(const void *a_, const void *b_)
821 struct actions_for_flow *const *a = a_;
822 struct actions_for_flow *const *b = b_;
824 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
828 compare_output_actions(const void *a_, const void *b_)
830 const struct nlattr *a = a_;
831 const struct nlattr *b = b_;
832 uint32_t a_port = nl_attr_get_u32(a);
833 uint32_t b_port = nl_attr_get_u32(b);
835 return a_port < b_port ? -1 : a_port > b_port;
839 sort_output_actions__(struct nlattr *first, struct nlattr *end)
841 size_t bytes = (uint8_t *) end - (uint8_t *) first;
842 size_t n = bytes / NL_A_U32_SIZE;
844 assert(bytes % NL_A_U32_SIZE == 0);
845 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
849 sort_output_actions(struct nlattr *actions, size_t length)
851 struct nlattr *first_output = NULL;
855 NL_ATTR_FOR_EACH (a, left, actions, length) {
856 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
862 sort_output_actions__(first_output, a);
868 uint8_t *end = (uint8_t *) actions + length;
869 sort_output_actions__(first_output, (struct nlattr *) end);
873 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
874 * have the syntax used by "ovs-dpctl dump-flows".
876 * This command prints ACTIONS in a format that shows what happens for each
877 * VLAN, independent of the order of the ACTIONS. For example, there is more
878 * than one way to output a packet on VLANs 9 and 11, but this command will
879 * print the same output for any form.
881 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
882 * so far the implementation only covers VLANs. */
884 dpctl_normalize_actions(int argc, char *argv[])
886 struct simap port_names;
887 struct ofpbuf keybuf;
889 struct ofpbuf odp_actions;
890 struct hmap actions_per_flow;
891 struct actions_for_flow **afs;
892 struct actions_for_flow *af;
901 simap_init(&port_names);
902 for (i = 3; i < argc; i++) {
907 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
908 uintptr_t n = number;
909 simap_put(&port_names, name, n);
911 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
915 /* Parse flow key. */
916 ofpbuf_init(&keybuf, 0);
917 run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
918 "odp_flow_key_from_string");
921 odp_flow_key_format(keybuf.data, keybuf.size, &s);
922 printf("input flow: %s\n", ds_cstr(&s));
924 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
925 "odp_flow_key_to_flow");
926 ofpbuf_uninit(&keybuf);
929 ofpbuf_init(&odp_actions, 0);
930 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
931 "odp_actions_from_string");
935 format_odp_actions(&s, odp_actions.data, odp_actions.size);
936 printf("input actions: %s\n", ds_cstr(&s));
939 hmap_init(&actions_per_flow);
940 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
941 if (nl_attr_type(a) == OVS_ACTION_ATTR_POP_VLAN) {
942 flow.vlan_tci = htons(0);
946 if (nl_attr_type(a) == OVS_ACTION_ATTR_PUSH_VLAN) {
947 const struct ovs_action_push_vlan *push;
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) {
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));
983 format_odp_actions(&s, af->actions.data, af->actions.size);
989 static const struct command all_commands[] = {
990 { "add-dp", 1, INT_MAX, dpctl_add_dp },
991 { "del-dp", 1, 1, dpctl_del_dp },
992 { "add-if", 2, INT_MAX, dpctl_add_if },
993 { "del-if", 2, INT_MAX, dpctl_del_if },
994 { "set-if", 2, INT_MAX, dpctl_set_if },
995 { "dump-dps", 0, 0, dpctl_dump_dps },
996 { "show", 0, INT_MAX, dpctl_show },
997 { "dump-flows", 0, 1, dpctl_dump_flows },
998 { "del-flows", 0, 1, dpctl_del_flows },
999 { "help", 0, INT_MAX, dpctl_help },
1001 /* Undocumented commands for testing. */
1002 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1003 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1005 { NULL, 0, 0, NULL },