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"
46 VLOG_DEFINE_THIS_MODULE(dpctl);
48 static const struct command all_commands[];
50 static void usage(void) NO_RETURN;
51 static void parse_options(int argc, char *argv[]);
54 main(int argc, char *argv[])
56 set_program_name(argv[0]);
57 parse_options(argc, argv);
58 signal(SIGPIPE, SIG_IGN);
59 run_command(argc - optind, argv + optind, all_commands);
64 parse_options(int argc, char *argv[])
67 OPT_DUMMY = UCHAR_MAX + 1,
70 static struct option long_options[] = {
71 {"timeout", required_argument, 0, 't'},
72 {"help", no_argument, 0, 'h'},
73 {"version", no_argument, 0, 'V'},
77 char *short_options = long_options_to_short_options(long_options);
80 unsigned long int timeout;
83 c = getopt_long(argc, argv, short_options, long_options, NULL);
90 timeout = strtoul(optarg, NULL, 10);
92 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
103 OVS_PRINT_VERSION(0, 0);
121 printf("%s: Open vSwitch datapath management utility\n"
122 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
123 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
124 " del-dp DP delete local datapath DP\n"
125 " add-if DP IFACE... add each IFACE as a port on DP\n"
126 " del-if DP IFACE... delete each IFACE from DP\n"
127 " dump-dps display names of all datapaths\n"
128 " show show basic info on all datapaths\n"
129 " show DP... show basic info on each DP\n"
130 " dump-flows DP display flows in DP\n"
131 " del-flows DP delete all flows from 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 do_add_if(int argc OVS_UNUSED, char *argv[])
220 bool failure = false;
224 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
225 for (i = 2; i < argc; i++) {
226 char *save_ptr = NULL;
227 struct netdev_options options;
228 struct netdev *netdev;
233 options.name = strtok_r(argv[i], ",", &save_ptr);
234 options.type = "system";
235 options.args = &args;
236 options.ethertype = NETDEV_ETH_TYPE_NONE;
239 ovs_error(0, "%s is not a valid network device name", argv[i]);
244 while ((option = strtok_r(NULL, "", &save_ptr)) != NULL) {
245 char *save_ptr_2 = NULL;
248 key = strtok_r(option, "=", &save_ptr_2);
249 value = strtok_r(NULL, "", &save_ptr_2);
254 if (!strcmp(key, "type")) {
255 options.type = value;
256 } else if (!shash_add_once(&args, key, value)) {
257 ovs_error(0, "duplicate \"%s\" option", key);
261 error = netdev_open(&options, &netdev);
263 ovs_error(error, "%s: failed to open network device",
266 error = dpif_port_add(dpif, netdev, NULL);
268 ovs_error(error, "adding %s to %s failed",
269 options.name, argv[1]);
271 error = if_up(options.name);
273 netdev_close(netdev);
286 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
288 struct odp_port odp_port;
290 if (!dpif_port_query_by_name(dpif, name, &odp_port)) {
291 *port = odp_port.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 show_dpif(struct dpif *dpif)
334 struct dpif_port_dump dump;
335 struct odp_port odp_port;
336 struct odp_stats stats;
338 printf("%s:\n", dpif_name(dpif));
339 if (!dpif_get_dp_stats(dpif, &stats)) {
340 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
341 "hard-max:%"PRIu32"\n",
342 stats.n_flows, stats.cur_capacity, stats.max_capacity);
343 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
344 stats.n_ports, stats.max_ports);
345 printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
346 (unsigned long long int) stats.n_frags,
347 (unsigned long long int) stats.n_hit,
348 (unsigned long long int) stats.n_missed,
349 (unsigned long long int) stats.n_lost);
350 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
351 stats.max_miss_queue, stats.max_action_queue);
353 DPIF_PORT_FOR_EACH (&odp_port, &dump, dpif) {
356 printf("\tport %u: %s", odp_port.port, odp_port.devname);
359 format_odp_port_type(&ds, &odp_port);
360 printf("%s\n", ds_cstr(&ds));
367 do_show(int argc, char *argv[])
369 bool failure = false;
372 for (i = 1; i < argc; i++) {
373 const char *name = argv[i];
377 error = parsed_dpif_open(name, false, &dpif);
381 ovs_error(error, "opening datapath %s failed", name);
391 dp_enumerate_types(&types);
392 SVEC_FOR_EACH (i, type, &types) {
398 if (dp_enumerate_names(type, &names)) {
402 SVEC_FOR_EACH (j, name, &names) {
406 error = dpif_open(name, type, &dpif);
410 ovs_error(error, "opening datapath %s failed", name);
414 svec_destroy(&names);
416 svec_destroy(&types);
424 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
426 struct svec dpif_names, dpif_types;
430 svec_init(&dpif_names);
431 svec_init(&dpif_types);
432 dp_enumerate_types(&dpif_types);
434 for (i = 0; i < dpif_types.n; i++) {
438 retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
443 for (j = 0; j < dpif_names.n; j++) {
445 if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
446 printf("%s\n", dpif_name(dpif));
452 svec_destroy(&dpif_names);
453 svec_destroy(&dpif_types);
460 do_dump_flows(int argc OVS_UNUSED, char *argv[])
462 struct dpif_flow_dump dump;
466 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
469 dpif_flow_dump_start(&dump, dpif);
471 enum { MAX_ACTIONS = 4096 }; /* An arbitrary but large number. */
472 uint32_t actions[MAX_ACTIONS * sizeof(struct nlattr) / 4];
473 uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S];
476 memset(&f, 0, sizeof f);
477 f.actions = (struct nlattr *) actions;
478 f.actions_len = sizeof actions;
479 f.key = (struct nlattr *) keybuf;
480 f.key_len = sizeof keybuf;
482 if (!dpif_flow_dump_next(&dump, &f)) {
487 format_odp_flow(&ds, &f);
488 printf("%s\n", ds_cstr(&ds));
490 dpif_flow_dump_done(&dump);
496 do_del_flows(int argc OVS_UNUSED, char *argv[])
500 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
501 run(dpif_flow_flush(dpif), "deleting all flows");
506 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
511 static const struct command all_commands[] = {
512 { "add-dp", 1, INT_MAX, do_add_dp },
513 { "del-dp", 1, 1, do_del_dp },
514 { "add-if", 2, INT_MAX, do_add_if },
515 { "del-if", 2, INT_MAX, do_del_if },
516 { "dump-dps", 0, 0, do_dump_dps },
517 { "show", 0, INT_MAX, do_show },
518 { "dump-flows", 1, 1, do_dump_flows },
519 { "del-flows", 1, 1, do_del_flows },
520 { "help", 0, INT_MAX, do_help },
521 { NULL, 0, 0, NULL },