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"
46 VLOG_DEFINE_THIS_MODULE(dpctl);
48 /* -s, --statistics: Print port statistics? */
49 static bool print_statistics;
51 static const struct command all_commands[];
53 static void usage(void) NO_RETURN;
54 static void parse_options(int argc, char *argv[]);
57 main(int argc, char *argv[])
59 set_program_name(argv[0]);
60 parse_options(argc, argv);
61 signal(SIGPIPE, SIG_IGN);
62 run_command(argc - optind, argv + optind, all_commands);
67 parse_options(int argc, char *argv[])
70 OPT_DUMMY = UCHAR_MAX + 1,
73 static struct option long_options[] = {
74 {"statistics", no_argument, NULL, 's'},
75 {"timeout", required_argument, NULL, 't'},
76 {"help", no_argument, NULL, 'h'},
77 {"version", no_argument, NULL, 'V'},
81 char *short_options = long_options_to_short_options(long_options);
84 unsigned long int timeout;
87 c = getopt_long(argc, argv, short_options, long_options, NULL);
94 print_statistics = true;
98 timeout = strtoul(optarg, NULL, 10);
100 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
111 ovs_print_version(0, 0);
129 printf("%s: Open vSwitch datapath management utility\n"
130 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
131 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
132 " del-dp DP delete local datapath DP\n"
133 " add-if DP IFACE... add each IFACE as a port on DP\n"
134 " set-if DP IFACE... reconfigure each IFACE within DP\n"
135 " del-if DP IFACE... delete each IFACE from DP\n"
136 " dump-dps display names of all datapaths\n"
137 " show show basic info on all datapaths\n"
138 " show DP... show basic info on each DP\n"
139 " dump-flows DP display flows in DP\n"
140 " del-flows DP delete all flows from DP\n"
141 "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
142 "comma-separated options. See ovs-dpctl(8) for syntax, or the\n"
143 "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
144 program_name, program_name);
146 printf("\nOther options:\n"
147 " -t, --timeout=SECS give up after SECS seconds\n"
148 " -h, --help display this help message\n"
149 " -V, --version display version information\n");
153 static void run(int retval, const char *message, ...)
156 static void run(int retval, const char *message, ...)
161 va_start(args, message);
162 ovs_fatal_valist(retval, message, args);
166 static void do_add_if(int argc, char *argv[]);
168 static int if_up(const char *netdev_name)
170 struct netdev *netdev;
173 retval = netdev_open(netdev_name, "system", &netdev);
175 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
176 netdev_close(netdev);
182 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
187 dp_parse_name(arg_, &name, &type);
190 result = dpif_create(name, type, dpifp);
192 result = dpif_open(name, type, dpifp);
201 do_add_dp(int argc OVS_UNUSED, char *argv[])
204 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
207 do_add_if(argc, argv);
212 do_del_dp(int argc OVS_UNUSED, char *argv[])
215 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
216 run(dpif_delete(dpif), "del_dp");
221 do_add_if(int argc OVS_UNUSED, char *argv[])
223 bool failure = false;
227 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
228 for (i = 2; i < argc; i++) {
229 const char *name, *type;
230 char *save_ptr = NULL;
231 struct netdev *netdev = NULL;
236 name = strtok_r(argv[i], ",", &save_ptr);
240 ovs_error(0, "%s is not a valid network device name", argv[i]);
246 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
247 char *save_ptr_2 = NULL;
250 key = strtok_r(option, "=", &save_ptr_2);
251 value = strtok_r(NULL, "", &save_ptr_2);
256 if (!strcmp(key, "type")) {
258 } else if (!shash_add_once(&args, key, value)) {
259 ovs_error(0, "duplicate \"%s\" option", key);
263 error = netdev_open(name, type, &netdev);
265 ovs_error(error, "%s: failed to open network device", name);
269 error = netdev_set_config(netdev, &args);
271 ovs_error(error, "%s: failed to configure network device", name);
275 error = dpif_port_add(dpif, netdev, NULL);
277 ovs_error(error, "adding %s to %s failed", name, argv[1]);
284 netdev_close(netdev);
296 do_set_if(int argc, char *argv[])
298 bool failure = false;
302 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
303 for (i = 2; i < argc; i++) {
304 struct netdev *netdev = NULL;
305 struct dpif_port dpif_port;
306 char *save_ptr = NULL;
313 name = strtok_r(argv[i], ",", &save_ptr);
315 ovs_error(0, "%s is not a valid network device name", argv[i]);
320 /* Get the port's type from the datapath. */
321 error = dpif_port_query_by_name(dpif, name, &dpif_port);
323 ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
326 type = xstrdup(dpif_port.type);
327 dpif_port_destroy(&dpif_port);
329 /* Retrieve its existing configuration. */
330 error = netdev_open(name, type, &netdev);
332 ovs_error(error, "%s: failed to open network device", name);
337 error = netdev_get_config(netdev, &args);
339 ovs_error(error, "%s: failed to fetch configuration", name);
343 /* Parse changes to configuration. */
344 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
345 char *save_ptr_2 = NULL;
348 key = strtok_r(option, "=", &save_ptr_2);
349 value = strtok_r(NULL, "", &save_ptr_2);
354 if (!strcmp(key, "type")) {
355 if (strcmp(value, type)) {
356 ovs_error(0, "%s: can't change type from %s to %s",
360 } else if (value[0] == '\0') {
361 free(shash_find_and_delete(&args, key));
363 free(shash_replace(&args, key, xstrdup(value)));
367 /* Update configuration. */
368 error = netdev_set_config(netdev, &args);
371 ovs_error(error, "%s: failed to configure network device", name);
377 netdev_close(netdev);
389 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
391 struct dpif_port dpif_port;
393 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
394 *port = dpif_port.port_no;
395 dpif_port_destroy(&dpif_port);
398 ovs_error(0, "no port named %s", name);
404 do_del_if(int argc OVS_UNUSED, char *argv[])
406 bool failure = false;
410 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
411 for (i = 2; i < argc; i++) {
412 const char *name = argv[i];
416 if (!name[strspn(name, "0123456789")]) {
418 } else if (!get_port_number(dpif, name, &port)) {
423 error = dpif_port_del(dpif, port);
425 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
436 print_stat(const char *leader, uint64_t value)
438 fputs(leader, stdout);
439 if (value != UINT64_MAX) {
440 printf("%"PRIu64, value);
447 print_human_size(uint64_t value)
449 if (value == UINT64_MAX) {
451 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
452 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
453 } else if (value >= 1024ULL * 1024 * 1024) {
454 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
455 } else if (value >= 1024ULL * 1024) {
456 printf(" (%.1f MiB)", value / (1024.0 * 1024));
457 } else if (value >= 1024) {
458 printf(" (%.1f KiB)", value / 1024.0);
463 show_dpif(struct dpif *dpif)
465 struct dpif_port_dump dump;
466 struct dpif_port dpif_port;
467 struct dpif_dp_stats stats;
468 struct netdev *netdev;
470 printf("%s:\n", dpif_name(dpif));
471 if (!dpif_get_dp_stats(dpif, &stats)) {
472 printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
473 "\tflows: %"PRIu64"\n",
474 stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
476 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
477 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
479 if (strcmp(dpif_port.type, "system")) {
482 printf (" (%s", dpif_port.type);
484 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
489 error = netdev_get_config(netdev, &config);
491 const struct shash_node **nodes;
494 nodes = shash_sort(&config);
495 for (i = 0; i < shash_count(&config); i++) {
496 const struct shash_node *node = nodes[i];
497 printf("%c %s=%s", i ? ',' : ':',
498 node->name, (char *) node->data);
502 printf(", could not retrieve configuration (%s)",
505 shash_destroy_free_data(&config);
507 netdev_close(netdev);
509 printf(": open failed (%s)", strerror(error));
515 if (print_statistics) {
516 struct netdev_stats s;
519 error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
521 printf(", open failed (%s)", strerror(error));
524 error = netdev_get_stats(netdev, &s);
526 printf(", could not retrieve stats (%s)", strerror(error));
530 netdev_close(netdev);
531 print_stat("\t\tRX packets:", s.rx_packets);
532 print_stat(" errors:", s.rx_errors);
533 print_stat(" dropped:", s.rx_dropped);
534 print_stat(" overruns:", s.rx_over_errors);
535 print_stat(" frame:", s.rx_frame_errors);
538 print_stat("\t\tTX packets:", s.tx_packets);
539 print_stat(" errors:", s.tx_errors);
540 print_stat(" dropped:", s.tx_dropped);
541 print_stat(" aborted:", s.tx_aborted_errors);
542 print_stat(" carrier:", s.tx_carrier_errors);
545 print_stat("\t\tcollisions:", s.collisions);
548 print_stat("\t\tRX bytes:", s.rx_bytes);
549 print_human_size(s.rx_bytes);
550 print_stat(" TX bytes:", s.tx_bytes);
551 print_human_size(s.tx_bytes);
559 do_show(int argc, char *argv[])
561 bool failure = false;
564 for (i = 1; i < argc; i++) {
565 const char *name = argv[i];
569 error = parsed_dpif_open(name, false, &dpif);
573 ovs_error(error, "opening datapath %s failed", name);
582 dp_enumerate_types(&types);
583 SSET_FOR_EACH (type, &types) {
588 if (dp_enumerate_names(type, &names)) {
592 SSET_FOR_EACH (name, &names) {
596 error = dpif_open(name, type, &dpif);
600 ovs_error(error, "opening datapath %s failed", name);
604 sset_destroy(&names);
606 sset_destroy(&types);
614 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
616 struct sset dpif_names, dpif_types;
620 sset_init(&dpif_names);
621 sset_init(&dpif_types);
622 dp_enumerate_types(&dpif_types);
624 SSET_FOR_EACH (type, &dpif_types) {
628 retval = dp_enumerate_names(type, &dpif_names);
633 SSET_FOR_EACH (name, &dpif_names) {
635 if (!dpif_open(name, type, &dpif)) {
636 printf("%s\n", dpif_name(dpif));
642 sset_destroy(&dpif_names);
643 sset_destroy(&dpif_types);
650 do_dump_flows(int argc OVS_UNUSED, char *argv[])
652 const struct dpif_flow_stats *stats;
653 const struct nlattr *actions;
654 struct dpif_flow_dump dump;
655 const struct nlattr *key;
661 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
664 dpif_flow_dump_start(&dump, dpif);
665 while (dpif_flow_dump_next(&dump, &key, &key_len,
666 &actions, &actions_len, &stats)) {
668 odp_flow_key_format(key, key_len, &ds);
669 ds_put_cstr(&ds, ", ");
670 dpif_flow_stats_format(stats, &ds);
671 ds_put_cstr(&ds, ", actions:");
672 format_odp_actions(&ds, actions, actions_len);
673 printf("%s\n", ds_cstr(&ds));
675 dpif_flow_dump_done(&dump);
681 do_del_flows(int argc OVS_UNUSED, char *argv[])
685 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
686 run(dpif_flow_flush(dpif), "deleting all flows");
691 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
696 static const struct command all_commands[] = {
697 { "add-dp", 1, INT_MAX, do_add_dp },
698 { "del-dp", 1, 1, do_del_dp },
699 { "add-if", 2, INT_MAX, do_add_if },
700 { "del-if", 2, INT_MAX, do_del_if },
701 { "set-if", 2, INT_MAX, do_set_if },
702 { "dump-dps", 0, 0, do_dump_dps },
703 { "show", 0, INT_MAX, do_show },
704 { "dump-flows", 1, 1, do_dump_flows },
705 { "del-flows", 1, 1, do_del_flows },
706 { "help", 0, INT_MAX, do_help },
707 { NULL, 0, 0, NULL },