2 * Copyright (c) 2008, 2009, 2010 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"
45 VLOG_DEFINE_THIS_MODULE(dpctl)
47 static const struct command all_commands[];
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
53 main(int argc, char *argv[])
55 set_program_name(argv[0]);
56 parse_options(argc, argv);
57 signal(SIGPIPE, SIG_IGN);
58 run_command(argc - optind, argv + optind, all_commands);
63 parse_options(int argc, char *argv[])
66 OPT_DUMMY = UCHAR_MAX + 1,
69 static struct option long_options[] = {
70 {"timeout", required_argument, 0, 't'},
71 {"help", no_argument, 0, 'h'},
72 {"version", no_argument, 0, 'V'},
76 char *short_options = long_options_to_short_options(long_options);
79 unsigned long int timeout;
82 c = getopt_long(argc, argv, short_options, long_options, NULL);
89 timeout = strtoul(optarg, NULL, 10);
91 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
102 OVS_PRINT_VERSION(0, 0);
120 printf("%s: Open vSwitch datapath management utility\n"
121 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
122 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
123 " del-dp DP delete local datapath DP\n"
124 " add-if DP IFACE... add each IFACE as a port on DP\n"
125 " del-if DP IFACE... delete each IFACE from DP\n"
126 " dump-dps display names of all datapaths\n"
127 " show show basic info on all datapaths\n"
128 " show DP... show basic info on each DP\n"
129 " dump-flows DP display flows in DP\n"
130 " del-flows DP delete all flows from DP\n"
131 " dump-groups DP display port groups in DP\n",
132 program_name, program_name);
134 printf("\nOther options:\n"
135 " -t, --timeout=SECS give up after SECS seconds\n"
136 " -h, --help display this help message\n"
137 " -V, --version display version information\n");
141 static void run(int retval, const char *message, ...)
144 static void run(int retval, const char *message, ...)
149 fprintf(stderr, "%s: ", program_name);
150 va_start(args, message);
151 vfprintf(stderr, message, args);
154 fputs(": unexpected end of file\n", stderr);
156 fprintf(stderr, ": %s\n", strerror(retval));
163 static void do_add_if(int argc, char *argv[]);
165 static int if_up(const char *netdev_name)
167 struct netdev *netdev;
170 retval = netdev_open_default(netdev_name, &netdev);
172 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
173 netdev_close(netdev);
179 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
184 dp_parse_name(arg_, &name, &type);
187 result = dpif_create(name, type, dpifp);
189 result = dpif_open(name, type, dpifp);
198 do_add_dp(int argc OVS_UNUSED, char *argv[])
201 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
204 do_add_if(argc, argv);
209 do_del_dp(int argc OVS_UNUSED, char *argv[])
212 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
213 run(dpif_delete(dpif), "del_dp");
218 compare_ports(const void *a_, const void *b_)
220 const struct odp_port *a = a_;
221 const struct odp_port *b = b_;
222 return a->port < b->port ? -1 : a->port > b->port;
226 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
228 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
229 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
233 do_add_if(int argc OVS_UNUSED, char *argv[])
235 bool failure = false;
239 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
240 for (i = 2; i < argc; i++) {
241 char *save_ptr = NULL;
242 char *devname, *suboptions;
246 devname = strtok_r(argv[i], ",", &save_ptr);
248 ovs_error(0, "%s is not a valid network device name", argv[i]);
252 suboptions = strtok_r(NULL, "", &save_ptr);
257 static char *options[] = {
261 while (*suboptions != '\0') {
264 switch (getsubopt(&suboptions, options, &value)) {
266 flags |= ODP_PORT_INTERNAL;
270 ovs_error(0, "unknown suboption '%s'", value);
276 error = dpif_port_add(dpif, devname, flags, NULL);
278 ovs_error(error, "adding %s to %s failed", devname, argv[1]);
280 } else if (if_up(devname)) {
291 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
293 struct odp_port *ports;
297 query_ports(dpif, &ports, &n_ports);
298 for (i = 0; i < n_ports; i++) {
299 if (!strcmp(name, ports[i].devname)) {
300 *port = ports[i].port;
306 ovs_error(0, "no port named %s", name);
311 do_del_if(int argc OVS_UNUSED, char *argv[])
313 bool failure = false;
317 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
318 for (i = 2; i < argc; i++) {
319 const char *name = argv[i];
323 if (!name[strspn(name, "0123456789")]) {
325 } else if (!get_port_number(dpif, name, &port)) {
330 error = dpif_port_del(dpif, port);
332 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
343 show_dpif(struct dpif *dpif)
345 struct odp_port *ports;
346 struct odp_stats stats;
350 printf("%s:\n", dpif_name(dpif));
351 if (!dpif_get_dp_stats(dpif, &stats)) {
352 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
353 "hard-max:%"PRIu32"\n",
354 stats.n_flows, stats.cur_capacity, stats.max_capacity);
355 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
356 stats.n_ports, stats.max_ports);
357 printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
358 printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
359 (unsigned long long int) stats.n_frags,
360 (unsigned long long int) stats.n_hit,
361 (unsigned long long int) stats.n_missed,
362 (unsigned long long int) stats.n_lost);
363 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
364 stats.max_miss_queue, stats.max_action_queue);
366 query_ports(dpif, &ports, &n_ports);
367 for (i = 0; i < n_ports; i++) {
368 printf("\tport %u: %s", ports[i].port, ports[i].devname);
369 if (ports[i].flags & ODP_PORT_INTERNAL) {
370 printf(" (internal)");
379 do_show(int argc, char *argv[])
381 bool failure = false;
384 for (i = 1; i < argc; i++) {
385 const char *name = argv[i];
389 error = parsed_dpif_open(name, false, &dpif);
393 ovs_error(error, "opening datapath %s failed", name);
399 for (i = 0; i < ODP_MAX; i++) {
404 sprintf(name, "dp%u", i);
405 error = parsed_dpif_open(name, false, &dpif);
408 } else if (error != ENODEV) {
409 ovs_error(error, "opening datapath %s failed", name);
420 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
422 struct svec dpif_names, dpif_types;
426 svec_init(&dpif_names);
427 svec_init(&dpif_types);
428 dp_enumerate_types(&dpif_types);
430 for (i = 0; i < dpif_types.n; i++) {
434 retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
439 for (j = 0; j < dpif_names.n; j++) {
441 if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
442 printf("%s\n", dpif_name(dpif));
448 svec_destroy(&dpif_names);
449 svec_destroy(&dpif_types);
456 do_dump_flows(int argc OVS_UNUSED, char *argv[])
458 struct odp_flow *flows;
464 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
465 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
468 for (i = 0; i < n_flows; i++) {
469 struct odp_flow *f = &flows[i];
470 enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
471 union odp_action actions[MAX_ACTIONS];
473 f->actions = actions;
474 f->n_actions = MAX_ACTIONS;
475 if (!dpif_flow_get(dpif, f)) {
477 format_odp_flow(&ds, f);
478 printf("%s\n", ds_cstr(&ds));
486 do_del_flows(int argc OVS_UNUSED, char *argv[])
490 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
491 run(dpif_flow_flush(dpif), "deleting all flows");
496 do_dump_groups(int argc OVS_UNUSED, char *argv[])
498 struct odp_stats stats;
502 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
503 run(dpif_get_dp_stats(dpif, &stats), "get datapath stats");
504 for (i = 0; i < stats.max_groups; i++) {
508 if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
511 printf("group %u:", i);
512 for (j = 0; j < n_ports; j++) {
513 printf(" %"PRIu16, ports[j]);
523 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
528 static const struct command all_commands[] = {
529 { "add-dp", 1, INT_MAX, do_add_dp },
530 { "del-dp", 1, 1, do_del_dp },
531 { "add-if", 2, INT_MAX, do_add_if },
532 { "del-if", 2, INT_MAX, do_del_if },
533 { "dump-dps", 0, 0, do_dump_dps },
534 { "show", 0, INT_MAX, do_show },
535 { "dump-flows", 1, 1, do_dump_flows },
536 { "del-flows", 1, 1, do_del_flows },
537 { "dump-groups", 1, 1, do_dump_groups },
538 { "help", 0, INT_MAX, do_help },
539 { NULL, 0, 0, NULL },