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>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
33 #include "command-line.h"
37 #include "dynamic-string.h"
47 VLOG_DEFINE_THIS_MODULE(dpctl);
49 /* -s, --statistics: Print port statistics? */
50 static bool print_statistics;
52 static const struct command all_commands[];
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
58 main(int argc, char *argv[])
60 set_program_name(argv[0]);
61 parse_options(argc, argv);
62 signal(SIGPIPE, SIG_IGN);
63 run_command(argc - optind, argv + optind, all_commands);
68 parse_options(int argc, char *argv[])
71 OPT_DUMMY = UCHAR_MAX + 1,
74 static struct option long_options[] = {
75 {"statistics", no_argument, NULL, 's'},
76 {"timeout", required_argument, NULL, 't'},
77 {"help", no_argument, NULL, 'h'},
78 {"version", no_argument, NULL, 'V'},
82 char *short_options = long_options_to_short_options(long_options);
85 unsigned long int timeout;
88 c = getopt_long(argc, argv, short_options, long_options, NULL);
95 print_statistics = true;
99 timeout = strtoul(optarg, NULL, 10);
101 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
112 ovs_print_version(0, 0);
130 printf("%s: Open vSwitch datapath management utility\n"
131 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
132 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
133 " del-dp DP delete local datapath DP\n"
134 " add-if DP IFACE... add each IFACE as a port on DP\n"
135 " set-if DP IFACE... reconfigure each IFACE within DP\n"
136 " del-if DP IFACE... delete each IFACE from DP\n"
137 " dump-dps display names of all datapaths\n"
138 " show show basic info on all datapaths\n"
139 " show DP... show basic info on each DP\n"
140 " dump-flows DP display flows in DP\n"
141 " del-flows DP delete all flows from DP\n"
142 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
143 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
144 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
145 program_name, program_name);
147 printf("\nOther options:\n"
148 " -t, --timeout=SECS give up after SECS seconds\n"
149 " -h, --help display this help message\n"
150 " -V, --version display version information\n");
154 static void run(int retval, const char *message, ...)
157 static void run(int retval, const char *message, ...)
162 va_start(args, message);
163 ovs_fatal_valist(retval, message, args);
167 static void do_add_if(int argc, char *argv[]);
169 static int if_up(const char *netdev_name)
171 struct netdev *netdev;
174 retval = netdev_open(netdev_name, "system", &netdev);
176 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
177 netdev_close(netdev);
183 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
188 dp_parse_name(arg_, &name, &type);
191 result = dpif_create(name, type, dpifp);
193 result = dpif_open(name, type, dpifp);
202 do_add_dp(int argc OVS_UNUSED, char *argv[])
205 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
208 do_add_if(argc, argv);
213 do_del_dp(int argc OVS_UNUSED, char *argv[])
216 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
217 run(dpif_delete(dpif), "del_dp");
222 do_add_if(int argc OVS_UNUSED, char *argv[])
224 bool failure = false;
228 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
229 for (i = 2; i < argc; i++) {
230 const char *name, *type;
231 char *save_ptr = NULL;
232 struct netdev *netdev = NULL;
237 name = strtok_r(argv[i], ",", &save_ptr);
241 ovs_error(0, "%s is not a valid network device name", argv[i]);
247 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
248 char *save_ptr_2 = NULL;
251 key = strtok_r(option, "=", &save_ptr_2);
252 value = strtok_r(NULL, "", &save_ptr_2);
257 if (!strcmp(key, "type")) {
259 } else if (!shash_add_once(&args, key, value)) {
260 ovs_error(0, "duplicate \"%s\" option", key);
264 error = netdev_open(name, type, &netdev);
266 ovs_error(error, "%s: failed to open network device", name);
270 error = netdev_set_config(netdev, &args);
272 ovs_error(error, "%s: failed to configure network device", name);
276 error = dpif_port_add(dpif, netdev, NULL);
278 ovs_error(error, "adding %s to %s failed", name, argv[1]);
285 netdev_close(netdev);
297 do_set_if(int argc, char *argv[])
299 bool failure = false;
303 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
304 for (i = 2; i < argc; i++) {
305 struct netdev *netdev = NULL;
306 struct dpif_port dpif_port;
307 char *save_ptr = NULL;
314 name = strtok_r(argv[i], ",", &save_ptr);
316 ovs_error(0, "%s is not a valid network device name", argv[i]);
321 /* Get the port's type from the datapath. */
322 error = dpif_port_query_by_name(dpif, name, &dpif_port);
324 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
327 type = xstrdup(dpif_port.type);
328 dpif_port_destroy(&dpif_port);
330 /* Retrieve its existing configuration. */
331 error = netdev_open(name, type, &netdev);
333 ovs_error(error, "%s: failed to open network device", name);
338 error = netdev_get_config(netdev, &args);
340 ovs_error(error, "%s: failed to fetch configuration", name);
344 /* Parse changes to configuration. */
345 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
346 char *save_ptr_2 = NULL;
349 key = strtok_r(option, "=", &save_ptr_2);
350 value = strtok_r(NULL, "", &save_ptr_2);
355 if (!strcmp(key, "type")) {
356 if (strcmp(value, type)) {
357 ovs_error(0, "%s: can't change type from %s to %s",
361 } else if (value[0] == '\0') {
362 free(shash_find_and_delete(&args, key));
364 free(shash_replace(&args, key, xstrdup(value)));
368 /* Update configuration. */
369 error = netdev_set_config(netdev, &args);
372 ovs_error(error, "%s: failed to configure network device", name);
378 netdev_close(netdev);
390 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
392 struct dpif_port dpif_port;
394 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
395 *port = dpif_port.port_no;
396 dpif_port_destroy(&dpif_port);
399 ovs_error(0, "no port named %s", name);
405 do_del_if(int argc OVS_UNUSED, char *argv[])
407 bool failure = false;
411 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
412 for (i = 2; i < argc; i++) {
413 const char *name = argv[i];
417 if (!name[strspn(name, "0123456789")]) {
419 } else if (!get_port_number(dpif, name, &port)) {
424 error = dpif_port_del(dpif, port);
426 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
437 print_stat(const char *leader, uint64_t value)
439 fputs(leader, stdout);
440 if (value != UINT64_MAX) {
441 printf("%"PRIu64, value);
448 print_human_size(uint64_t value)
450 if (value == UINT64_MAX) {
452 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
453 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
454 } else if (value >= 1024ULL * 1024 * 1024) {
455 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
456 } else if (value >= 1024ULL * 1024) {
457 printf(" (%.1f MiB)", value / (1024.0 * 1024));
458 } else if (value >= 1024) {
459 printf(" (%.1f KiB)", value / 1024.0);
464 show_dpif(struct dpif *dpif)
466 struct dpif_port_dump dump;
467 struct dpif_port dpif_port;
468 struct dpif_dp_stats stats;
469 struct netdev *netdev;
471 printf("%s:\n", dpif_name(dpif));
472 if (!dpif_get_dp_stats(dpif, &stats)) {
473 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
474 "\tflows: %"PRIu64"\n",
475 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
477 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
478 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
480 if (strcmp(dpif_port.type, "system")) {
483 printf (" (%s", dpif_port.type);
485 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
490 error = netdev_get_config(netdev, &config);
492 const struct shash_node **nodes;
495 nodes = shash_sort(&config);
496 for (i = 0; i < shash_count(&config); i++) {
497 const struct shash_node *node = nodes[i];
498 printf("%c %s=%s", i ? ',' : ':',
499 node->name, (char *) node->data);
503 printf(", could not retrieve configuration (%s)",
506 shash_destroy_free_data(&config);
508 netdev_close(netdev);
510 printf(": open failed (%s)", strerror(error));
516 if (print_statistics) {
517 struct netdev_stats s;
520 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
522 printf(", open failed (%s)", strerror(error));
525 error = netdev_get_stats(netdev, &s);
527 printf(", could not retrieve stats (%s)", strerror(error));
531 netdev_close(netdev);
532 print_stat("\t\tRX packets:", s.rx_packets);
533 print_stat(" errors:", s.rx_errors);
534 print_stat(" dropped:", s.rx_dropped);
535 print_stat(" overruns:", s.rx_over_errors);
536 print_stat(" frame:", s.rx_frame_errors);
539 print_stat("\t\tTX packets:", s.tx_packets);
540 print_stat(" errors:", s.tx_errors);
541 print_stat(" dropped:", s.tx_dropped);
542 print_stat(" aborted:", s.tx_aborted_errors);
543 print_stat(" carrier:", s.tx_carrier_errors);
546 print_stat("\t\tcollisions:", s.collisions);
549 print_stat("\t\tRX bytes:", s.rx_bytes);
550 print_human_size(s.rx_bytes);
551 print_stat(" TX bytes:", s.tx_bytes);
552 print_human_size(s.tx_bytes);
560 do_show(int argc, char *argv[])
562 bool failure = false;
565 for (i = 1; i < argc; i++) {
566 const char *name = argv[i];
570 error = parsed_dpif_open(name, false, &dpif);
574 ovs_error(error, "opening datapath %s failed", name);
583 dp_enumerate_types(&types);
584 SSET_FOR_EACH (type, &types) {
589 if (dp_enumerate_names(type, &names)) {
593 SSET_FOR_EACH (name, &names) {
597 error = dpif_open(name, type, &dpif);
601 ovs_error(error, "opening datapath %s failed", name);
605 sset_destroy(&names);
607 sset_destroy(&types);
615 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
617 struct sset dpif_names, dpif_types;
621 sset_init(&dpif_names);
622 sset_init(&dpif_types);
623 dp_enumerate_types(&dpif_types);
625 SSET_FOR_EACH (type, &dpif_types) {
629 retval = dp_enumerate_names(type, &dpif_names);
634 SSET_FOR_EACH (name, &dpif_names) {
636 if (!dpif_open(name, type, &dpif)) {
637 printf("%s\n", dpif_name(dpif));
643 sset_destroy(&dpif_names);
644 sset_destroy(&dpif_types);
651 do_dump_flows(int argc OVS_UNUSED, char *argv[])
653 const struct dpif_flow_stats *stats;
654 const struct nlattr *actions;
655 struct dpif_flow_dump dump;
656 const struct nlattr *key;
662 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
665 dpif_flow_dump_start(&dump, dpif);
666 while (dpif_flow_dump_next(&dump, &key, &key_len,
667 &actions, &actions_len, &stats)) {
669 odp_flow_key_format(key, key_len, &ds);
670 ds_put_cstr(&ds, ", ");
671 dpif_flow_stats_format(stats, &ds);
672 ds_put_cstr(&ds, ", actions:");
673 format_odp_actions(&ds, actions, actions_len);
674 printf("%s\n", ds_cstr(&ds));
676 dpif_flow_dump_done(&dump);
682 do_del_flows(int argc OVS_UNUSED, char *argv[])
686 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
687 run(dpif_flow_flush(dpif), "deleting all flows");
692 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
697 /* Undocumented commands for unit testing. */
700 do_parse_actions(int argc, char *argv[])
704 for (i = 1; i < argc; i++) {
705 struct ofpbuf actions;
708 ofpbuf_init(&actions, 0);
709 run(odp_actions_from_string(argv[i], NULL, &actions),
710 "odp_actions_from_string");
713 format_odp_actions(&s, actions.data, actions.size);
717 ofpbuf_uninit(&actions);
721 static const struct command all_commands[] = {
722 { "add-dp", 1, INT_MAX, do_add_dp },
723 { "del-dp", 1, 1, do_del_dp },
724 { "add-if", 2, INT_MAX, do_add_if },
725 { "del-if", 2, INT_MAX, do_del_if },
726 { "set-if", 2, INT_MAX, do_set_if },
727 { "dump-dps", 0, 0, do_dump_dps },
728 { "show", 0, INT_MAX, do_show },
729 { "dump-flows", 1, 1, do_dump_flows },
730 { "del-flows", 1, 1, do_del_flows },
731 { "help", 0, INT_MAX, do_help },
733 /* Undocumented commands for testing. */
734 { "parse-actions", 1, INT_MAX, do_parse_actions },
736 { NULL, 0, 0, NULL },