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("\nOptions for show:\n"
165 " -s, --statistics print port statistics\n"
167 " -t, --timeout=SECS give up after SECS seconds\n"
168 " -h, --help display this help message\n"
169 " -V, --version display version information\n");
173 static void run(int retval, const char *message, ...)
176 static void run(int retval, const char *message, ...)
181 va_start(args, message);
182 ovs_fatal_valist(retval, message, args);
186 static void dpctl_add_if(int argc, char *argv[]);
188 static int if_up(const char *netdev_name)
190 struct netdev *netdev;
193 retval = netdev_open(netdev_name, "system", &netdev);
195 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
196 netdev_close(netdev);
201 /* Retrieve the name of the datapath if exactly one exists. The caller
202 * is responsible for freeing the returned string. If there is not one
203 * datapath, aborts with an error message. */
209 char *dp_name = NULL;
213 dp_enumerate_types(&types);
214 SSET_FOR_EACH (type, &types) {
218 if (!dp_enumerate_names(type, &names)) {
219 count += sset_count(&names);
220 if (!dp_name && count == 1) {
221 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
224 sset_destroy(&names);
226 sset_destroy(&types);
229 ovs_fatal(0, "no datapaths exist");
230 } else if (count > 1) {
231 ovs_fatal(0, "multiple datapaths, specify one");
238 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
243 dp_parse_name(arg_, &name, &type);
246 result = dpif_create(name, type, dpifp);
248 result = dpif_open(name, type, dpifp);
257 dpctl_add_dp(int argc OVS_UNUSED, char *argv[])
260 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
263 dpctl_add_if(argc, argv);
268 dpctl_del_dp(int argc OVS_UNUSED, char *argv[])
271 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
272 run(dpif_delete(dpif), "del_dp");
277 dpctl_add_if(int argc OVS_UNUSED, char *argv[])
279 bool failure = false;
283 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
284 for (i = 2; i < argc; i++) {
285 const char *name, *type;
286 char *save_ptr = NULL;
287 struct netdev *netdev = NULL;
289 uint32_t port_no = UINT32_MAX;
293 name = strtok_r(argv[i], ",", &save_ptr);
297 ovs_error(0, "%s is not a valid network device name", argv[i]);
303 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
304 char *save_ptr_2 = NULL;
307 key = strtok_r(option, "=", &save_ptr_2);
308 value = strtok_r(NULL, "", &save_ptr_2);
313 if (!strcmp(key, "type")) {
315 } else if (!strcmp(key, "port_no")) {
316 port_no = atoi(value);
317 } else if (!smap_add_once(&args, key, value)) {
318 ovs_error(0, "duplicate \"%s\" option", key);
322 error = netdev_open(name, type, &netdev);
324 ovs_error(error, "%s: failed to open network device", name);
328 error = netdev_set_config(netdev, &args);
330 ovs_error(error, "%s: failed to configure network device", name);
334 error = dpif_port_add(dpif, netdev, &port_no);
336 ovs_error(error, "adding %s to %s failed", name, argv[1]);
343 netdev_close(netdev);
355 dpctl_set_if(int argc, char *argv[])
357 bool failure = false;
361 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
362 for (i = 2; i < argc; i++) {
363 struct netdev *netdev = NULL;
364 struct dpif_port dpif_port;
365 char *save_ptr = NULL;
373 name = strtok_r(argv[i], ",", &save_ptr);
375 ovs_error(0, "%s is not a valid network device name", argv[i]);
380 /* Get the port's type from the datapath. */
381 error = dpif_port_query_by_name(dpif, name, &dpif_port);
383 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
386 type = xstrdup(dpif_port.type);
387 port_no = dpif_port.port_no;
388 dpif_port_destroy(&dpif_port);
390 /* Retrieve its existing configuration. */
391 error = netdev_open(name, type, &netdev);
393 ovs_error(error, "%s: failed to open network device", name);
398 error = netdev_get_config(netdev, &args);
400 ovs_error(error, "%s: failed to fetch configuration", name);
404 /* Parse changes to configuration. */
405 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
406 char *save_ptr_2 = NULL;
409 key = strtok_r(option, "=", &save_ptr_2);
410 value = strtok_r(NULL, "", &save_ptr_2);
415 if (!strcmp(key, "type")) {
416 if (strcmp(value, type)) {
417 ovs_error(0, "%s: can't change type from %s to %s",
421 } else if (!strcmp(key, "port_no")) {
422 if (port_no != atoi(value)) {
423 ovs_error(0, "%s: can't change port number from "
425 name, port_no, atoi(value));
428 } else if (value[0] == '\0') {
429 smap_remove(&args, key);
431 smap_replace(&args, key, value);
435 /* Update configuration. */
436 error = netdev_set_config(netdev, &args);
439 ovs_error(error, "%s: failed to configure network device", name);
445 netdev_close(netdev);
457 get_port_number(struct dpif *dpif, const char *name, uint32_t *port)
459 struct dpif_port dpif_port;
461 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
462 *port = dpif_port.port_no;
463 dpif_port_destroy(&dpif_port);
466 ovs_error(0, "no port named %s", name);
472 dpctl_del_if(int argc OVS_UNUSED, char *argv[])
474 bool failure = false;
478 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
479 for (i = 2; i < argc; i++) {
480 const char *name = argv[i];
484 if (!name[strspn(name, "0123456789")]) {
486 } else if (!get_port_number(dpif, name, &port)) {
491 error = dpif_port_del(dpif, port);
493 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
504 print_stat(const char *leader, uint64_t value)
506 fputs(leader, stdout);
507 if (value != UINT64_MAX) {
508 printf("%"PRIu64, value);
515 print_human_size(uint64_t value)
517 if (value == UINT64_MAX) {
519 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
520 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
521 } else if (value >= 1024ULL * 1024 * 1024) {
522 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
523 } else if (value >= 1024ULL * 1024) {
524 printf(" (%.1f MiB)", value / (1024.0 * 1024));
525 } else if (value >= 1024) {
526 printf(" (%.1f KiB)", value / 1024.0);
531 show_dpif(struct dpif *dpif)
533 struct dpif_port_dump dump;
534 struct dpif_port dpif_port;
535 struct dpif_dp_stats stats;
536 struct netdev *netdev;
538 printf("%s:\n", dpif_name(dpif));
539 if (!dpif_get_dp_stats(dpif, &stats)) {
540 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
541 "\tflows: %"PRIu64"\n",
542 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
544 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
545 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
547 if (strcmp(dpif_port.type, "system")) {
550 printf (" (%s", dpif_port.type);
552 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
557 error = netdev_get_config(netdev, &config);
559 const struct smap_node **nodes;
562 nodes = smap_sort(&config);
563 for (i = 0; i < smap_count(&config); i++) {
564 const struct smap_node *node = nodes[i];
565 printf("%c %s=%s", i ? ',' : ':', node->key,
570 printf(", could not retrieve configuration (%s)",
573 smap_destroy(&config);
575 netdev_close(netdev);
577 printf(": open failed (%s)", strerror(error));
583 if (print_statistics) {
584 struct netdev_stats s;
587 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
589 printf(", open failed (%s)", strerror(error));
592 error = netdev_get_stats(netdev, &s);
594 printf(", could not retrieve stats (%s)", strerror(error));
598 netdev_close(netdev);
599 print_stat("\t\tRX packets:", s.rx_packets);
600 print_stat(" errors:", s.rx_errors);
601 print_stat(" dropped:", s.rx_dropped);
602 print_stat(" overruns:", s.rx_over_errors);
603 print_stat(" frame:", s.rx_frame_errors);
606 print_stat("\t\tTX packets:", s.tx_packets);
607 print_stat(" errors:", s.tx_errors);
608 print_stat(" dropped:", s.tx_dropped);
609 print_stat(" aborted:", s.tx_aborted_errors);
610 print_stat(" carrier:", s.tx_carrier_errors);
613 print_stat("\t\tcollisions:", s.collisions);
616 print_stat("\t\tRX bytes:", s.rx_bytes);
617 print_human_size(s.rx_bytes);
618 print_stat(" TX bytes:", s.tx_bytes);
619 print_human_size(s.tx_bytes);
627 dpctl_show(int argc, char *argv[])
629 bool failure = false;
632 for (i = 1; i < argc; i++) {
633 const char *name = argv[i];
637 error = parsed_dpif_open(name, false, &dpif);
641 ovs_error(error, "opening datapath %s failed", name);
650 dp_enumerate_types(&types);
651 SSET_FOR_EACH (type, &types) {
656 if (dp_enumerate_names(type, &names)) {
660 SSET_FOR_EACH (name, &names) {
664 error = dpif_open(name, type, &dpif);
668 ovs_error(error, "opening datapath %s failed", name);
672 sset_destroy(&names);
674 sset_destroy(&types);
682 dpctl_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
684 struct sset dpif_names, dpif_types;
688 sset_init(&dpif_names);
689 sset_init(&dpif_types);
690 dp_enumerate_types(&dpif_types);
692 SSET_FOR_EACH (type, &dpif_types) {
696 retval = dp_enumerate_names(type, &dpif_names);
701 SSET_FOR_EACH (name, &dpif_names) {
703 if (!dpif_open(name, type, &dpif)) {
704 printf("%s\n", dpif_name(dpif));
710 sset_destroy(&dpif_names);
711 sset_destroy(&dpif_types);
718 dpctl_dump_flows(int argc, char *argv[])
720 const struct dpif_flow_stats *stats;
721 const struct nlattr *actions;
722 struct dpif_flow_dump dump;
723 const struct nlattr *key;
730 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
731 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
735 dpif_flow_dump_start(&dump, dpif);
736 while (dpif_flow_dump_next(&dump, &key, &key_len,
737 &actions, &actions_len, &stats)) {
739 odp_flow_key_format(key, key_len, &ds);
740 ds_put_cstr(&ds, ", ");
741 dpif_flow_stats_format(stats, &ds);
742 ds_put_cstr(&ds, ", actions:");
743 format_odp_actions(&ds, actions, actions_len);
744 printf("%s\n", ds_cstr(&ds));
746 dpif_flow_dump_done(&dump);
752 dpctl_del_flows(int argc, char *argv[])
757 name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp();
758 run(parsed_dpif_open(name, false, &dpif), "opening datapath");
761 run(dpif_flow_flush(dpif), "deleting all flows");
766 dpctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
771 /* Undocumented commands for unit testing. */
774 dpctl_parse_actions(int argc, char *argv[])
778 for (i = 1; i < argc; i++) {
779 struct ofpbuf actions;
782 ofpbuf_init(&actions, 0);
783 run(odp_actions_from_string(argv[i], NULL, &actions),
784 "odp_actions_from_string");
787 format_odp_actions(&s, actions.data, actions.size);
791 ofpbuf_uninit(&actions);
795 struct actions_for_flow {
796 struct hmap_node hmap_node;
798 struct ofpbuf actions;
801 static struct actions_for_flow *
802 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
804 uint32_t hash = flow_hash(flow, 0);
805 struct actions_for_flow *af;
807 HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
808 if (flow_equal(&af->flow, flow)) {
813 af = xmalloc(sizeof *af);
815 ofpbuf_init(&af->actions, 0);
816 hmap_insert(actions_per_flow, &af->hmap_node, hash);
821 compare_actions_for_flow(const void *a_, const void *b_)
823 struct actions_for_flow *const *a = a_;
824 struct actions_for_flow *const *b = b_;
826 return flow_compare_3way(&(*a)->flow, &(*b)->flow);
830 compare_output_actions(const void *a_, const void *b_)
832 const struct nlattr *a = a_;
833 const struct nlattr *b = b_;
834 uint32_t a_port = nl_attr_get_u32(a);
835 uint32_t b_port = nl_attr_get_u32(b);
837 return a_port < b_port ? -1 : a_port > b_port;
841 sort_output_actions__(struct nlattr *first, struct nlattr *end)
843 size_t bytes = (uint8_t *) end - (uint8_t *) first;
844 size_t n = bytes / NL_A_U32_SIZE;
846 assert(bytes % NL_A_U32_SIZE == 0);
847 qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
851 sort_output_actions(struct nlattr *actions, size_t length)
853 struct nlattr *first_output = NULL;
857 NL_ATTR_FOR_EACH (a, left, actions, length) {
858 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
864 sort_output_actions__(first_output, a);
870 uint8_t *end = (uint8_t *) actions + length;
871 sort_output_actions__(first_output, (struct nlattr *) end);
875 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
876 * have the syntax used by "ovs-dpctl dump-flows".
878 * This command prints ACTIONS in a format that shows what happens for each
879 * VLAN, independent of the order of the ACTIONS. For example, there is more
880 * than one way to output a packet on VLANs 9 and 11, but this command will
881 * print the same output for any form.
883 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
884 * so far the implementation only covers VLANs. */
886 dpctl_normalize_actions(int argc, char *argv[])
888 struct simap port_names;
889 struct ofpbuf keybuf;
891 struct ofpbuf odp_actions;
892 struct hmap actions_per_flow;
893 struct actions_for_flow **afs;
894 struct actions_for_flow *af;
903 simap_init(&port_names);
904 for (i = 3; i < argc; i++) {
909 if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
910 uintptr_t n = number;
911 simap_put(&port_names, name, n);
913 ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
917 /* Parse flow key. */
918 ofpbuf_init(&keybuf, 0);
919 run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
920 "odp_flow_key_from_string");
923 odp_flow_key_format(keybuf.data, keybuf.size, &s);
924 printf("input flow: %s\n", ds_cstr(&s));
926 run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
927 "odp_flow_key_to_flow");
928 ofpbuf_uninit(&keybuf);
931 ofpbuf_init(&odp_actions, 0);
932 run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
933 "odp_actions_from_string");
937 format_odp_actions(&s, odp_actions.data, odp_actions.size);
938 printf("input actions: %s\n", ds_cstr(&s));
941 hmap_init(&actions_per_flow);
942 NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
943 if (nl_attr_type(a) == OVS_ACTION_ATTR_POP_VLAN) {
944 flow.vlan_tci = htons(0);
948 if (nl_attr_type(a) == OVS_ACTION_ATTR_PUSH_VLAN) {
949 const struct ovs_action_push_vlan *push;
951 push = nl_attr_get_unspec(a, sizeof *push);
952 flow.vlan_tci = push->vlan_tci;
956 af = get_actions_for_flow(&actions_per_flow, &flow);
957 nl_msg_put_unspec(&af->actions, nl_attr_type(a),
958 nl_attr_get(a), nl_attr_get_size(a));
961 n_afs = hmap_count(&actions_per_flow);
962 afs = xmalloc(n_afs * sizeof *afs);
964 HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
969 qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
971 for (i = 0; i < n_afs; i++) {
972 const struct actions_for_flow *af = afs[i];
974 sort_output_actions(af->actions.data, af->actions.size);
976 if (af->flow.vlan_tci != htons(0)) {
977 printf("vlan(vid=%"PRIu16",pcp=%d): ",
978 vlan_tci_to_vid(af->flow.vlan_tci),
979 vlan_tci_to_pcp(af->flow.vlan_tci));
985 format_odp_actions(&s, af->actions.data, af->actions.size);
991 static const struct command all_commands[] = {
992 { "add-dp", 1, INT_MAX, dpctl_add_dp },
993 { "del-dp", 1, 1, dpctl_del_dp },
994 { "add-if", 2, INT_MAX, dpctl_add_if },
995 { "del-if", 2, INT_MAX, dpctl_del_if },
996 { "set-if", 2, INT_MAX, dpctl_set_if },
997 { "dump-dps", 0, 0, dpctl_dump_dps },
998 { "show", 0, INT_MAX, dpctl_show },
999 { "dump-flows", 0, 1, dpctl_dump_flows },
1000 { "del-flows", 0, 1, dpctl_del_flows },
1001 { "help", 0, INT_MAX, dpctl_help },
1003 /* Undocumented commands for testing. */
1004 { "parse-actions", 1, INT_MAX, dpctl_parse_actions },
1005 { "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
1007 { NULL, 0, 0, NULL },