2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
32 #include "command-line.h"
36 #include "dynamic-string.h"
43 #define THIS_MODULE VLM_dpctl
49 void (*handler)(int argc, char *argv[]);
52 static struct command all_commands[];
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
57 int main(int argc, char *argv[])
61 set_program_name(argv[0]);
64 parse_options(argc, argv);
65 signal(SIGPIPE, SIG_IGN);
70 ovs_fatal(0, "missing command name; use --help for help");
72 for (p = all_commands; p->name != NULL; p++) {
73 if (!strcmp(p->name, argv[0])) {
75 if (n_arg < p->min_args)
76 ovs_fatal(0, "'%s' command requires at least %d arguments",
77 p->name, p->min_args);
78 else if (n_arg > p->max_args)
79 ovs_fatal(0, "'%s' command takes at most %d arguments",
80 p->name, p->max_args);
82 p->handler(argc, argv);
84 ovs_fatal(0, "write to stdout failed");
87 ovs_fatal(0, "write to stderr failed");
93 ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
99 parse_options(int argc, char *argv[])
101 static struct option long_options[] = {
102 {"timeout", required_argument, 0, 't'},
103 {"verbose", optional_argument, 0, 'v'},
104 {"help", no_argument, 0, 'h'},
105 {"version", no_argument, 0, 'V'},
108 char *short_options = long_options_to_short_options(long_options);
111 unsigned long int timeout;
114 c = getopt_long(argc, argv, short_options, long_options, NULL);
121 timeout = strtoul(optarg, NULL, 10);
123 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
134 OVS_PRINT_VERSION(0, 0);
138 vlog_set_verbosity(optarg);
154 printf("%s: Open vSwitch datapath management utility\n"
155 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
156 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
157 " del-dp DP delete local datapath DP\n"
158 " add-if DP IFACE... add each IFACE as a port on DP\n"
159 " del-if DP IFACE... delete each IFACE from DP\n"
160 " show show basic info on all datapaths\n"
161 " show DP... show basic info on each DP\n"
162 " dump-flows DP display flows in DP\n"
163 " del-flows DP delete all flows from DP\n"
164 " dump-groups DP display port groups in DP\n",
165 program_name, program_name);
167 printf("\nOther options:\n"
168 " -t, --timeout=SECS give up after SECS seconds\n"
169 " -h, --help display this help message\n"
170 " -V, --version display version information\n");
174 static void run(int retval, const char *message, ...)
177 static void run(int retval, const char *message, ...)
182 fprintf(stderr, "%s: ", program_name);
183 va_start(args, message);
184 vfprintf(stderr, message, args);
187 fputs(": unexpected end of file\n", stderr);
189 fprintf(stderr, ": %s\n", strerror(retval));
196 static void do_add_if(int argc, char *argv[]);
198 static int if_up(const char *netdev_name)
200 struct netdev *netdev;
203 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
205 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
206 netdev_close(netdev);
212 do_add_dp(int argc UNUSED, char *argv[])
215 run(dpif_create(argv[1], &dpif), "add_dp");
218 do_add_if(argc, argv);
223 do_del_dp(int argc UNUSED, char *argv[])
226 run(dpif_open(argv[1], &dpif), "opening datapath");
227 run(dpif_delete(&dpif), "del_dp");
232 compare_ports(const void *a_, const void *b_)
234 const struct odp_port *a = a_;
235 const struct odp_port *b = b_;
236 return a->port < b->port ? -1 : a->port > b->port;
240 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
242 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
243 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
247 get_free_port(struct dpif *dpif)
249 struct odp_port *ports;
253 query_ports(dpif, &ports, &n_ports);
254 for (port_no = 0; port_no <= UINT16_MAX; port_no++) {
256 for (i = 0; i < n_ports; i++) {
257 if (ports[i].port == port_no) {
266 ovs_fatal(0, "no free datapath ports");
270 do_add_if(int argc UNUSED, char *argv[])
272 bool failure = false;
276 run(dpif_open(argv[1], &dpif), "opening datapath");
277 for (i = 2; i < argc; i++) {
278 char *save_ptr = NULL;
279 char *devname, *suboptions;
284 devname = strtok_r(argv[i], ",,", &save_ptr);
286 ovs_error(0, "%s is not a valid network device name", argv[i]);
290 suboptions = strtok_r(NULL, "", &save_ptr);
296 static char *options[] = {
301 while (*suboptions != '\0') {
304 switch (getsubopt(&suboptions, options, &value)) {
307 ovs_error(0, "'port' suboption requires a value");
313 flags |= ODP_PORT_INTERNAL;
317 ovs_error(0, "unknown suboption '%s'", value);
323 port = get_free_port(&dpif);
326 error = dpif_port_add(&dpif, devname, port, flags);
328 ovs_error(error, "adding %s as port %"PRIu16" of %s failed",
329 devname, port, argv[1]);
331 } else if (if_up(devname)) {
342 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
344 struct odp_port *ports;
348 query_ports(dpif, &ports, &n_ports);
349 for (i = 0; i < n_ports; i++) {
350 if (!strcmp(name, ports[i].devname)) {
351 *port = ports[i].port;
357 ovs_error(0, "no port named %s", name);
362 do_del_if(int argc UNUSED, char *argv[])
364 bool failure = false;
368 run(dpif_open(argv[1], &dpif), "opening datapath");
369 for (i = 2; i < argc; i++) {
370 const char *name = argv[i];
374 if (!name[strspn(name, "0123456789")]) {
376 } else if (!get_port_number(&dpif, name, &port)) {
381 error = dpif_port_del(&dpif, port);
383 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
394 show_dpif(struct dpif *dpif)
396 struct odp_port *ports;
397 struct odp_stats stats;
401 printf("dp%u:\n", dpif_id(dpif));
402 if (!dpif_get_dp_stats(dpif, &stats)) {
403 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
404 "hard-max:%"PRIu32"\n",
405 stats.n_flows, stats.cur_capacity, stats.max_capacity);
406 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
407 stats.n_ports, stats.max_ports);
408 printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
409 printf("\tlookups: frags:%"PRIu64", hit:%"PRIu64", missed:%"PRIu64", "
411 stats.n_frags, stats.n_hit, stats.n_missed, stats.n_lost);
412 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
413 stats.max_miss_queue, stats.max_action_queue);
415 query_ports(dpif, &ports, &n_ports);
416 for (i = 0; i < n_ports; i++) {
417 printf("\tport %u: %s", ports[i].port, ports[i].devname);
418 if (ports[i].flags & ODP_PORT_INTERNAL) {
419 printf(" (internal)");
428 do_show(int argc UNUSED, char *argv[])
430 bool failure = false;
433 for (i = 1; i < argc; i++) {
434 const char *name = argv[i];
438 error = dpif_open(name, &dpif);
442 ovs_error(error, "opening datapath %s failed", name);
448 for (i = 0; i < ODP_MAX; i++) {
453 sprintf(name, "dp%u", i);
454 error = dpif_open(name, &dpif);
457 } else if (error != ENODEV) {
458 ovs_error(error, "opening datapath %s failed", name);
469 do_dump_flows(int argc UNUSED, char *argv[])
471 struct odp_flow *flows;
477 run(dpif_open(argv[1], &dpif), "opening datapath");
478 run(dpif_flow_list_all(&dpif, &flows, &n_flows), "listing all flows");
481 for (i = 0; i < n_flows; i++) {
482 struct odp_flow *f = &flows[i];
483 enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
484 union odp_action actions[MAX_ACTIONS];
486 f->actions = actions;
487 f->n_actions = MAX_ACTIONS;
488 dpif_flow_get(&dpif, f);
491 format_odp_flow(&ds, f);
492 printf("%s\n", ds_cstr(&ds));
499 do_del_flows(int argc UNUSED, char *argv[])
503 run(dpif_open(argv[1], &dpif), "opening datapath");
504 run(dpif_flow_flush(&dpif), "deleting all flows");
509 do_dump_groups(int argc UNUSED, char *argv[])
511 struct odp_stats stats;
515 run(dpif_open(argv[1], &dpif), "opening datapath");
516 run(dpif_get_dp_stats(&dpif, &stats), "get datapath stats");
517 for (i = 0; i < stats.max_groups; i++) {
518 uint16_t ports[UINT16_MAX];
521 if (!dpif_port_group_get(&dpif, i, ports,
522 ARRAY_SIZE(ports), &n_ports) && n_ports) {
525 printf("group %u:", i);
526 for (j = 0; j < n_ports; j++) {
527 printf(" %"PRIu16, ports[j]);
536 do_help(int argc UNUSED, char *argv[] UNUSED)
541 static struct command all_commands[] = {
542 { "add-dp", 1, INT_MAX, do_add_dp },
543 { "del-dp", 1, 1, do_del_dp },
544 { "add-if", 2, INT_MAX, do_add_if },
545 { "del-if", 2, INT_MAX, do_del_if },
546 { "show", 0, INT_MAX, do_show },
547 { "dump-flows", 1, 1, do_dump_flows },
548 { "del-flows", 1, 1, do_del_flows },
549 { "dump-groups", 1, 1, do_dump_groups },
550 { "help", 0, INT_MAX, do_help },
551 { NULL, 0, 0, NULL },