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>
23 #include <netinet/in.h>
32 #include "command-line.h"
36 #include "dynamic-string.h"
44 #define THIS_MODULE VLM_dpctl
46 static const struct command all_commands[];
48 static void usage(void) NO_RETURN;
49 static void parse_options(int argc, char *argv[]);
52 main(int argc, char *argv[])
54 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 " dump-groups DP display port groups in DP\n",
133 program_name, program_name);
135 printf("\nOther options:\n"
136 " -t, --timeout=SECS give up after SECS seconds\n"
137 " -h, --help display this help message\n"
138 " -V, --version display version information\n");
142 static void run(int retval, const char *message, ...)
145 static void run(int retval, const char *message, ...)
150 fprintf(stderr, "%s: ", program_name);
151 va_start(args, message);
152 vfprintf(stderr, message, args);
155 fputs(": unexpected end of file\n", stderr);
157 fprintf(stderr, ": %s\n", strerror(retval));
164 static void do_add_if(int argc, char *argv[]);
166 static int if_up(const char *netdev_name)
168 struct netdev *netdev;
171 retval = netdev_open_default(netdev_name, &netdev);
173 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
174 netdev_close(netdev);
180 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
185 dp_parse_name(arg_, &name, &type);
188 result = dpif_create(name, type, dpifp);
190 result = dpif_open(name, type, dpifp);
199 do_add_dp(int argc OVS_UNUSED, char *argv[])
202 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
205 do_add_if(argc, argv);
210 do_del_dp(int argc OVS_UNUSED, char *argv[])
213 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
214 run(dpif_delete(dpif), "del_dp");
219 compare_ports(const void *a_, const void *b_)
221 const struct odp_port *a = a_;
222 const struct odp_port *b = b_;
223 return a->port < b->port ? -1 : a->port > b->port;
227 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
229 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
230 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
234 do_add_if(int argc OVS_UNUSED, char *argv[])
236 bool failure = false;
240 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
241 for (i = 2; i < argc; i++) {
242 char *save_ptr = NULL;
243 char *devname, *suboptions;
247 devname = strtok_r(argv[i], ",", &save_ptr);
249 ovs_error(0, "%s is not a valid network device name", argv[i]);
253 suboptions = strtok_r(NULL, "", &save_ptr);
258 static char *options[] = {
262 while (*suboptions != '\0') {
265 switch (getsubopt(&suboptions, options, &value)) {
267 flags |= ODP_PORT_INTERNAL;
271 ovs_error(0, "unknown suboption '%s'", value);
277 error = dpif_port_add(dpif, devname, flags, NULL);
279 ovs_error(error, "adding %s to %s failed", devname, argv[1]);
281 } else if (if_up(devname)) {
292 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
294 struct odp_port *ports;
298 query_ports(dpif, &ports, &n_ports);
299 for (i = 0; i < n_ports; i++) {
300 if (!strcmp(name, ports[i].devname)) {
301 *port = ports[i].port;
307 ovs_error(0, "no port named %s", name);
312 do_del_if(int argc OVS_UNUSED, char *argv[])
314 bool failure = false;
318 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
319 for (i = 2; i < argc; i++) {
320 const char *name = argv[i];
324 if (!name[strspn(name, "0123456789")]) {
326 } else if (!get_port_number(dpif, name, &port)) {
331 error = dpif_port_del(dpif, port);
333 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
344 show_dpif(struct dpif *dpif)
346 struct odp_port *ports;
347 struct odp_stats stats;
351 printf("%s:\n", dpif_name(dpif));
352 if (!dpif_get_dp_stats(dpif, &stats)) {
353 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
354 "hard-max:%"PRIu32"\n",
355 stats.n_flows, stats.cur_capacity, stats.max_capacity);
356 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
357 stats.n_ports, stats.max_ports);
358 printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
359 printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
360 (unsigned long long int) stats.n_frags,
361 (unsigned long long int) stats.n_hit,
362 (unsigned long long int) stats.n_missed,
363 (unsigned long long int) stats.n_lost);
364 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
365 stats.max_miss_queue, stats.max_action_queue);
367 query_ports(dpif, &ports, &n_ports);
368 for (i = 0; i < n_ports; i++) {
369 printf("\tport %u: %s", ports[i].port, ports[i].devname);
370 if (ports[i].flags & ODP_PORT_INTERNAL) {
371 printf(" (internal)");
380 do_show(int argc, char *argv[])
382 bool failure = false;
385 for (i = 1; i < argc; i++) {
386 const char *name = argv[i];
390 error = parsed_dpif_open(name, false, &dpif);
394 ovs_error(error, "opening datapath %s failed", name);
400 for (i = 0; i < ODP_MAX; i++) {
405 sprintf(name, "dp%u", i);
406 error = parsed_dpif_open(name, false, &dpif);
409 } else if (error != ENODEV) {
410 ovs_error(error, "opening datapath %s failed", name);
421 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
423 struct svec dpif_names, dpif_types;
427 svec_init(&dpif_names);
428 svec_init(&dpif_types);
429 dp_enumerate_types(&dpif_types);
431 for (i = 0; i < dpif_types.n; i++) {
435 retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
440 for (j = 0; j < dpif_names.n; j++) {
442 if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
443 printf("%s\n", dpif_name(dpif));
449 svec_destroy(&dpif_names);
450 svec_destroy(&dpif_types);
457 do_dump_flows(int argc OVS_UNUSED, char *argv[])
459 struct odp_flow *flows;
465 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
466 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
469 for (i = 0; i < n_flows; i++) {
470 struct odp_flow *f = &flows[i];
471 enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
472 union odp_action actions[MAX_ACTIONS];
474 f->actions = actions;
475 f->n_actions = MAX_ACTIONS;
476 if (!dpif_flow_get(dpif, f)) {
478 format_odp_flow(&ds, f);
479 printf("%s\n", ds_cstr(&ds));
487 do_del_flows(int argc OVS_UNUSED, char *argv[])
491 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
492 run(dpif_flow_flush(dpif), "deleting all flows");
497 do_dump_groups(int argc OVS_UNUSED, char *argv[])
499 struct odp_stats stats;
503 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
504 run(dpif_get_dp_stats(dpif, &stats), "get datapath stats");
505 for (i = 0; i < stats.max_groups; i++) {
509 if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
512 printf("group %u:", i);
513 for (j = 0; j < n_ports; j++) {
514 printf(" %"PRIu16, ports[j]);
524 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
529 static const struct command all_commands[] = {
530 { "add-dp", 1, INT_MAX, do_add_dp },
531 { "del-dp", 1, 1, do_del_dp },
532 { "add-if", 2, INT_MAX, do_add_if },
533 { "del-if", 2, INT_MAX, do_del_if },
534 { "dump-dps", 0, 0, do_dump_dps },
535 { "show", 0, INT_MAX, do_show },
536 { "dump-flows", 1, 1, do_dump_flows },
537 { "del-flows", 1, 1, do_del_flows },
538 { "dump-groups", 1, 1, do_dump_groups },
539 { "help", 0, INT_MAX, do_help },
540 { NULL, 0, 0, NULL },