2 * Copyright (c) 2008, 2009 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>
23 #include <netinet/in.h>
32 #include "command-line.h"
36 #include "dynamic-string.h"
44 #define THIS_MODULE VLM_dpctl
50 void (*handler)(int argc, char *argv[]);
53 static struct command all_commands[];
55 static void usage(void) NO_RETURN;
56 static void parse_options(int argc, char *argv[]);
58 int main(int argc, char *argv[])
62 set_program_name(argv[0]);
65 parse_options(argc, argv);
66 signal(SIGPIPE, SIG_IGN);
71 ovs_fatal(0, "missing command name; use --help for help");
73 for (p = all_commands; p->name != NULL; p++) {
74 if (!strcmp(p->name, argv[0])) {
76 if (n_arg < p->min_args)
77 ovs_fatal(0, "'%s' command requires at least %d arguments",
78 p->name, p->min_args);
79 else if (n_arg > p->max_args)
80 ovs_fatal(0, "'%s' command takes at most %d arguments",
81 p->name, p->max_args);
83 p->handler(argc, argv);
85 ovs_fatal(0, "write to stdout failed");
88 ovs_fatal(0, "write to stderr failed");
94 ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
100 parse_options(int argc, char *argv[])
102 static struct option long_options[] = {
103 {"timeout", required_argument, 0, 't'},
104 {"verbose", optional_argument, 0, 'v'},
105 {"help", no_argument, 0, 'h'},
106 {"version", no_argument, 0, 'V'},
109 char *short_options = long_options_to_short_options(long_options);
112 unsigned long int timeout;
115 c = getopt_long(argc, argv, short_options, long_options, NULL);
122 timeout = strtoul(optarg, NULL, 10);
124 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
135 OVS_PRINT_VERSION(0, 0);
139 vlog_set_verbosity(optarg);
155 printf("%s: Open vSwitch datapath management utility\n"
156 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
157 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
158 " del-dp DP delete local datapath DP\n"
159 " add-if DP IFACE... add each IFACE as a port on DP\n"
160 " del-if DP IFACE... delete each IFACE from DP\n"
161 " dump-dps display names of all datapaths\n"
162 " show show basic info on all datapaths\n"
163 " show DP... show basic info on each DP\n"
164 " dump-flows DP display flows in DP\n"
165 " del-flows DP delete all flows from DP\n"
166 " dump-groups DP display port groups in DP\n",
167 program_name, program_name);
169 printf("\nOther options:\n"
170 " -t, --timeout=SECS give up after SECS seconds\n"
171 " -h, --help display this help message\n"
172 " -V, --version display version information\n");
176 static void run(int retval, const char *message, ...)
179 static void run(int retval, const char *message, ...)
184 fprintf(stderr, "%s: ", program_name);
185 va_start(args, message);
186 vfprintf(stderr, message, args);
189 fputs(": unexpected end of file\n", stderr);
191 fprintf(stderr, ": %s\n", strerror(retval));
198 static void do_add_if(int argc, char *argv[]);
200 static int if_up(const char *netdev_name)
202 struct netdev *netdev;
205 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
207 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
208 netdev_close(netdev);
214 do_add_dp(int argc UNUSED, char *argv[])
217 run(dpif_create(argv[1], &dpif), "add_dp");
220 do_add_if(argc, argv);
225 do_del_dp(int argc UNUSED, char *argv[])
228 run(dpif_open(argv[1], &dpif), "opening datapath");
229 run(dpif_delete(dpif), "del_dp");
234 compare_ports(const void *a_, const void *b_)
236 const struct odp_port *a = a_;
237 const struct odp_port *b = b_;
238 return a->port < b->port ? -1 : a->port > b->port;
242 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
244 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
245 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
249 do_add_if(int argc UNUSED, char *argv[])
251 bool failure = false;
255 run(dpif_open(argv[1], &dpif), "opening datapath");
256 for (i = 2; i < argc; i++) {
257 char *save_ptr = NULL;
258 char *devname, *suboptions;
262 devname = strtok_r(argv[i], ",", &save_ptr);
264 ovs_error(0, "%s is not a valid network device name", argv[i]);
268 suboptions = strtok_r(NULL, "", &save_ptr);
273 static char *options[] = {
277 while (*suboptions != '\0') {
280 switch (getsubopt(&suboptions, options, &value)) {
282 flags |= ODP_PORT_INTERNAL;
286 ovs_error(0, "unknown suboption '%s'", value);
292 error = dpif_port_add(dpif, devname, flags, NULL);
294 ovs_error(error, "adding %s to %s failed", devname, argv[1]);
296 } else if (if_up(devname)) {
307 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
309 struct odp_port *ports;
313 query_ports(dpif, &ports, &n_ports);
314 for (i = 0; i < n_ports; i++) {
315 if (!strcmp(name, ports[i].devname)) {
316 *port = ports[i].port;
322 ovs_error(0, "no port named %s", name);
327 do_del_if(int argc UNUSED, char *argv[])
329 bool failure = false;
333 run(dpif_open(argv[1], &dpif), "opening datapath");
334 for (i = 2; i < argc; i++) {
335 const char *name = argv[i];
339 if (!name[strspn(name, "0123456789")]) {
341 } else if (!get_port_number(dpif, name, &port)) {
346 error = dpif_port_del(dpif, port);
348 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
359 show_dpif(struct dpif *dpif)
361 struct odp_port *ports;
362 struct odp_stats stats;
366 printf("%s:\n", dpif_name(dpif));
367 if (!dpif_get_dp_stats(dpif, &stats)) {
368 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
369 "hard-max:%"PRIu32"\n",
370 stats.n_flows, stats.cur_capacity, stats.max_capacity);
371 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
372 stats.n_ports, stats.max_ports);
373 printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
374 printf("\tlookups: frags:%"PRIu64", hit:%"PRIu64", missed:%"PRIu64", "
376 stats.n_frags, stats.n_hit, stats.n_missed, stats.n_lost);
377 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
378 stats.max_miss_queue, stats.max_action_queue);
380 query_ports(dpif, &ports, &n_ports);
381 for (i = 0; i < n_ports; i++) {
382 printf("\tport %u: %s", ports[i].port, ports[i].devname);
383 if (ports[i].flags & ODP_PORT_INTERNAL) {
384 printf(" (internal)");
393 do_show(int argc, char *argv[])
395 bool failure = false;
398 for (i = 1; i < argc; i++) {
399 const char *name = argv[i];
403 error = dpif_open(name, &dpif);
407 ovs_error(error, "opening datapath %s failed", name);
413 for (i = 0; i < ODP_MAX; i++) {
418 sprintf(name, "dp%u", i);
419 error = dpif_open(name, &dpif);
422 } else if (error != ENODEV) {
423 ovs_error(error, "opening datapath %s failed", name);
434 do_dump_dps(int argc UNUSED, char *argv[] UNUSED)
441 error = dp_enumerate(&all_dps);
443 for (i = 0; i < all_dps.n; i++) {
445 if (!dpif_open(all_dps.names[i], &dpif)) {
446 printf("%s\n", dpif_name(dpif));
451 svec_destroy(&all_dps);
458 do_dump_flows(int argc UNUSED, char *argv[])
460 struct odp_flow *flows;
466 run(dpif_open(argv[1], &dpif), "opening datapath");
467 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
470 for (i = 0; i < n_flows; i++) {
471 struct odp_flow *f = &flows[i];
472 enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
473 union odp_action actions[MAX_ACTIONS];
475 f->actions = actions;
476 f->n_actions = MAX_ACTIONS;
477 dpif_flow_get(dpif, f);
480 format_odp_flow(&ds, f);
481 printf("%s\n", ds_cstr(&ds));
488 do_del_flows(int argc UNUSED, char *argv[])
492 run(dpif_open(argv[1], &dpif), "opening datapath");
493 run(dpif_flow_flush(dpif), "deleting all flows");
498 do_dump_groups(int argc UNUSED, char *argv[])
500 struct odp_stats stats;
504 run(dpif_open(argv[1], &dpif), "opening datapath");
505 run(dpif_get_dp_stats(dpif, &stats), "get datapath stats");
506 for (i = 0; i < stats.max_groups; i++) {
510 if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
513 printf("group %u:", i);
514 for (j = 0; j < n_ports; j++) {
515 printf(" %"PRIu16, ports[j]);
525 do_help(int argc UNUSED, char *argv[] UNUSED)
530 static struct command all_commands[] = {
531 { "add-dp", 1, INT_MAX, do_add_dp },
532 { "del-dp", 1, 1, do_del_dp },
533 { "add-if", 2, INT_MAX, do_add_if },
534 { "del-if", 2, INT_MAX, do_del_if },
535 { "dump-dps", 0, 0, do_dump_dps },
536 { "show", 0, INT_MAX, do_show },
537 { "dump-flows", 1, 1, do_dump_flows },
538 { "del-flows", 1, 1, do_del_flows },
539 { "dump-groups", 1, 1, do_dump_groups },
540 { "help", 0, INT_MAX, do_help },
541 { NULL, 0, 0, NULL },