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 " del-if DP IFACE... delete each IFACE from DP\n"
135 " dump-dps display names of all datapaths\n"
136 " show show basic info on all datapaths\n"
137 " show DP... show basic info on each DP\n"
138 " dump-flows DP display flows in DP\n"
139 " del-flows DP delete all flows from DP\n",
140 program_name, program_name);
142 printf("\nOther options:\n"
143 " -t, --timeout=SECS give up after SECS seconds\n"
144 " -h, --help display this help message\n"
145 " -V, --version display version information\n");
149 static void run(int retval, const char *message, ...)
152 static void run(int retval, const char *message, ...)
157 va_start(args, message);
158 ovs_fatal_valist(retval, message, args);
162 static void do_add_if(int argc, char *argv[]);
164 static int if_up(const char *netdev_name)
166 struct netdev *netdev;
169 retval = netdev_open_default(netdev_name, &netdev);
171 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
172 netdev_close(netdev);
178 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
183 dp_parse_name(arg_, &name, &type);
186 result = dpif_create(name, type, dpifp);
188 result = dpif_open(name, type, dpifp);
197 do_add_dp(int argc OVS_UNUSED, char *argv[])
200 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
203 do_add_if(argc, argv);
208 do_del_dp(int argc OVS_UNUSED, char *argv[])
211 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
212 run(dpif_delete(dpif), "del_dp");
217 do_add_if(int argc OVS_UNUSED, char *argv[])
219 bool failure = false;
223 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
224 for (i = 2; i < argc; i++) {
225 char *save_ptr = NULL;
226 struct netdev_options options;
227 struct netdev *netdev;
232 options.name = strtok_r(argv[i], ",", &save_ptr);
233 options.type = "system";
234 options.args = &args;
235 options.ethertype = NETDEV_ETH_TYPE_NONE;
238 ovs_error(0, "%s is not a valid network device name", argv[i]);
243 while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
244 char *save_ptr_2 = NULL;
247 key = strtok_r(option, "=", &save_ptr_2);
248 value = strtok_r(NULL, "", &save_ptr_2);
253 if (!strcmp(key, "type")) {
254 options.type = value;
255 } else if (!shash_add_once(&args, key, value)) {
256 ovs_error(0, "duplicate \"%s\" option", key);
260 error = netdev_open(&options, &netdev);
262 ovs_error(error, "%s: failed to open network device",
265 error = dpif_port_add(dpif, netdev, NULL);
267 ovs_error(error, "adding %s to %s failed",
268 options.name, argv[1]);
270 error = if_up(options.name);
272 netdev_close(netdev);
285 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
287 struct dpif_port dpif_port;
289 if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
290 *port = dpif_port.port_no;
291 dpif_port_destroy(&dpif_port);
294 ovs_error(0, "no port named %s", name);
300 do_del_if(int argc OVS_UNUSED, char *argv[])
302 bool failure = false;
306 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
307 for (i = 2; i < argc; i++) {
308 const char *name = argv[i];
312 if (!name[strspn(name, "0123456789")]) {
314 } else if (!get_port_number(dpif, name, &port)) {
319 error = dpif_port_del(dpif, port);
321 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
332 print_stat(const char *leader, uint64_t value)
334 fputs(leader, stdout);
335 if (value != UINT64_MAX) {
336 printf("%"PRIu64, value);
343 print_human_size(uint64_t value)
345 if (value == UINT64_MAX) {
347 } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
348 printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
349 } else if (value >= 1024ULL * 1024 * 1024) {
350 printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
351 } else if (value >= 1024ULL * 1024) {
352 printf(" (%.1f MiB)", value / (1024.0 * 1024));
353 } else if (value >= 1024) {
354 printf(" (%.1f KiB)", value / 1024.0);
359 show_dpif(struct dpif *dpif)
361 struct dpif_port_dump dump;
362 struct dpif_port dpif_port;
363 struct odp_stats stats;
365 printf("%s:\n", dpif_name(dpif));
366 if (!dpif_get_dp_stats(dpif, &stats)) {
367 printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
368 (unsigned long long int) stats.n_frags,
369 (unsigned long long int) stats.n_hit,
370 (unsigned long long int) stats.n_missed,
371 (unsigned long long int) stats.n_lost);
373 DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
374 printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
376 if (strcmp(dpif_port.type, "system")) {
377 struct netdev_options netdev_options;
378 struct netdev *netdev;
381 printf (" (%s", dpif_port.type);
383 netdev_options.name = dpif_port.name;
384 netdev_options.type = dpif_port.type;
385 netdev_options.args = NULL;
386 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
387 error = netdev_open(&netdev_options, &netdev);
389 const struct shash_node **nodes;
390 const struct shash *config;
393 config = netdev_get_config(netdev);
394 nodes = shash_sort(config);
395 for (i = 0; i < shash_count(config); i++) {
396 const struct shash_node *node = nodes[i];
397 printf("%c %s=%s", i ? ',' : ':',
398 node->name, (char *) node->data);
402 netdev_close(netdev);
404 printf(": open failed (%s)", strerror(error));
410 if (print_statistics) {
411 const struct netdev_stats *s = &dpif_port.stats;
413 print_stat("\t\tRX packets:", s->rx_packets);
414 print_stat(" errors:", s->rx_errors);
415 print_stat(" dropped:", s->rx_dropped);
416 print_stat(" overruns:", s->rx_over_errors);
417 print_stat(" frame:", s->rx_frame_errors);
420 print_stat("\t\tTX packets:", s->tx_packets);
421 print_stat(" errors:", s->tx_errors);
422 print_stat(" dropped:", s->tx_dropped);
423 print_stat(" aborted:", s->tx_aborted_errors);
424 print_stat(" carrier:", s->tx_carrier_errors);
427 print_stat("\t\tcollisions:", s->collisions);
430 print_stat("\t\tRX bytes:", s->rx_bytes);
431 print_human_size(s->rx_bytes);
432 print_stat(" TX bytes:", s->tx_bytes);
433 print_human_size(s->tx_bytes);
441 do_show(int argc, char *argv[])
443 bool failure = false;
446 for (i = 1; i < argc; i++) {
447 const char *name = argv[i];
451 error = parsed_dpif_open(name, false, &dpif);
455 ovs_error(error, "opening datapath %s failed", name);
464 dp_enumerate_types(&types);
465 SSET_FOR_EACH (type, &types) {
470 if (dp_enumerate_names(type, &names)) {
474 SSET_FOR_EACH (name, &names) {
478 error = dpif_open(name, type, &dpif);
482 ovs_error(error, "opening datapath %s failed", name);
486 sset_destroy(&names);
488 sset_destroy(&types);
496 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
498 struct sset dpif_names, dpif_types;
502 sset_init(&dpif_names);
503 sset_init(&dpif_types);
504 dp_enumerate_types(&dpif_types);
506 SSET_FOR_EACH (type, &dpif_types) {
510 retval = dp_enumerate_names(type, &dpif_names);
515 SSET_FOR_EACH (name, &dpif_names) {
517 if (!dpif_open(name, type, &dpif)) {
518 printf("%s\n", dpif_name(dpif));
524 sset_destroy(&dpif_names);
525 sset_destroy(&dpif_types);
532 do_dump_flows(int argc OVS_UNUSED, char *argv[])
534 const struct dpif_flow_stats *stats;
535 const struct nlattr *actions;
536 struct dpif_flow_dump dump;
537 const struct nlattr *key;
543 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
546 dpif_flow_dump_start(&dump, dpif);
547 while (dpif_flow_dump_next(&dump, &key, &key_len,
548 &actions, &actions_len, &stats)) {
550 odp_flow_key_format(key, key_len, &ds);
551 ds_put_cstr(&ds, ", ");
552 dpif_flow_stats_format(stats, &ds);
553 ds_put_cstr(&ds, ", actions:");
554 format_odp_actions(&ds, actions, actions_len);
555 printf("%s\n", ds_cstr(&ds));
557 dpif_flow_dump_done(&dump);
563 do_del_flows(int argc OVS_UNUSED, char *argv[])
567 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
568 run(dpif_flow_flush(dpif), "deleting all flows");
573 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
578 static const struct command all_commands[] = {
579 { "add-dp", 1, INT_MAX, do_add_dp },
580 { "del-dp", 1, 1, do_del_dp },
581 { "add-if", 2, INT_MAX, do_add_if },
582 { "del-if", 2, INT_MAX, do_del_if },
583 { "dump-dps", 0, 0, do_dump_dps },
584 { "show", 0, INT_MAX, do_show },
585 { "dump-flows", 1, 1, do_dump_flows },
586 { "del-flows", 1, 1, do_del_flows },
587 { "help", 0, INT_MAX, do_help },
588 { NULL, 0, 0, NULL },