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"
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 do_add_if(int argc UNUSED, char *argv[])
249 bool failure = false;
253 run(dpif_open(argv[1], &dpif), "opening datapath");
254 for (i = 2; i < argc; i++) {
255 char *save_ptr = NULL;
256 char *devname, *suboptions;
260 devname = strtok_r(argv[i], ",", &save_ptr);
262 ovs_error(0, "%s is not a valid network device name", argv[i]);
266 suboptions = strtok_r(NULL, "", &save_ptr);
271 static char *options[] = {
275 while (*suboptions != '\0') {
278 switch (getsubopt(&suboptions, options, &value)) {
280 flags |= ODP_PORT_INTERNAL;
284 ovs_error(0, "unknown suboption '%s'", value);
290 error = dpif_port_add(dpif, devname, flags, NULL);
292 ovs_error(error, "adding %s to %s failed", devname, argv[1]);
294 } else if (if_up(devname)) {
305 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
307 struct odp_port *ports;
311 query_ports(dpif, &ports, &n_ports);
312 for (i = 0; i < n_ports; i++) {
313 if (!strcmp(name, ports[i].devname)) {
314 *port = ports[i].port;
320 ovs_error(0, "no port named %s", name);
325 do_del_if(int argc UNUSED, char *argv[])
327 bool failure = false;
331 run(dpif_open(argv[1], &dpif), "opening datapath");
332 for (i = 2; i < argc; i++) {
333 const char *name = argv[i];
337 if (!name[strspn(name, "0123456789")]) {
339 } else if (!get_port_number(dpif, name, &port)) {
344 error = dpif_port_del(dpif, port);
346 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
357 show_dpif(struct dpif *dpif)
359 struct odp_port *ports;
360 struct odp_stats stats;
364 printf("%s:\n", dpif_name(dpif));
365 if (!dpif_get_dp_stats(dpif, &stats)) {
366 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
367 "hard-max:%"PRIu32"\n",
368 stats.n_flows, stats.cur_capacity, stats.max_capacity);
369 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
370 stats.n_ports, stats.max_ports);
371 printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
372 printf("\tlookups: frags:%"PRIu64", hit:%"PRIu64", missed:%"PRIu64", "
374 stats.n_frags, stats.n_hit, stats.n_missed, stats.n_lost);
375 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
376 stats.max_miss_queue, stats.max_action_queue);
378 query_ports(dpif, &ports, &n_ports);
379 for (i = 0; i < n_ports; i++) {
380 printf("\tport %u: %s", ports[i].port, ports[i].devname);
381 if (ports[i].flags & ODP_PORT_INTERNAL) {
382 printf(" (internal)");
391 do_show(int argc UNUSED, char *argv[])
393 bool failure = false;
396 for (i = 1; i < argc; i++) {
397 const char *name = argv[i];
401 error = dpif_open(name, &dpif);
405 ovs_error(error, "opening datapath %s failed", name);
411 for (i = 0; i < ODP_MAX; i++) {
416 sprintf(name, "dp%u", i);
417 error = dpif_open(name, &dpif);
420 } else if (error != ENODEV) {
421 ovs_error(error, "opening datapath %s failed", name);
432 do_dump_flows(int argc UNUSED, char *argv[])
434 struct odp_flow *flows;
440 run(dpif_open(argv[1], &dpif), "opening datapath");
441 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
444 for (i = 0; i < n_flows; i++) {
445 struct odp_flow *f = &flows[i];
446 enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
447 union odp_action actions[MAX_ACTIONS];
449 f->actions = actions;
450 f->n_actions = MAX_ACTIONS;
451 dpif_flow_get(dpif, f);
454 format_odp_flow(&ds, f);
455 printf("%s\n", ds_cstr(&ds));
462 do_del_flows(int argc UNUSED, char *argv[])
466 run(dpif_open(argv[1], &dpif), "opening datapath");
467 run(dpif_flow_flush(dpif), "deleting all flows");
472 do_dump_groups(int argc UNUSED, char *argv[])
474 struct odp_stats stats;
478 run(dpif_open(argv[1], &dpif), "opening datapath");
479 run(dpif_get_dp_stats(dpif, &stats), "get datapath stats");
480 for (i = 0; i < stats.max_groups; i++) {
484 if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
487 printf("group %u:", i);
488 for (j = 0; j < n_ports; j++) {
489 printf(" %"PRIu16, ports[j]);
499 do_help(int argc UNUSED, char *argv[] UNUSED)
504 static struct command all_commands[] = {
505 { "add-dp", 1, INT_MAX, do_add_dp },
506 { "del-dp", 1, 1, do_del_dp },
507 { "add-if", 2, INT_MAX, do_add_if },
508 { "del-if", 2, INT_MAX, do_del_if },
509 { "show", 0, INT_MAX, do_show },
510 { "dump-flows", 1, 1, do_dump_flows },
511 { "del-flows", 1, 1, do_del_flows },
512 { "dump-groups", 1, 1, do_dump_groups },
513 { "help", 0, INT_MAX, do_help },
514 { NULL, 0, 0, NULL },