]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-dpctl.c
ofproto: Use shash instead of svec for uniquifying, in reinit_ports().
[mirror_ovs.git] / utilities / ovs-dpctl.c
CommitLineData
064af421 1/*
149f577a 2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
064af421
BP
15 */
16
17#include <config.h>
18#include <arpa/inet.h>
19#include <errno.h>
20#include <getopt.h>
21#include <inttypes.h>
9d82ec47 22#include <sys/socket.h>
064af421
BP
23#include <net/if.h>
24#include <netinet/in.h>
25#include <signal.h>
26#include <stdarg.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/stat.h>
31#include <sys/time.h>
32
33#include "command-line.h"
34#include "compiler.h"
35#include "dirs.h"
36#include "dpif.h"
37#include "dynamic-string.h"
38#include "netdev.h"
39#include "odp-util.h"
c3827f61 40#include "shash.h"
b566902b 41#include "svec.h"
064af421
BP
42#include "timeval.h"
43#include "util.h"
064af421 44#include "vlog.h"
5136ce49 45
d98e6007 46VLOG_DEFINE_THIS_MODULE(dpctl);
064af421 47
675febfa 48static const struct command all_commands[];
064af421
BP
49
50static void usage(void) NO_RETURN;
51static void parse_options(int argc, char *argv[]);
52
675febfa
BP
53int
54main(int argc, char *argv[])
064af421 55{
064af421 56 set_program_name(argv[0]);
064af421
BP
57 parse_options(argc, argv);
58 signal(SIGPIPE, SIG_IGN);
675febfa 59 run_command(argc - optind, argv + optind, all_commands);
064af421
BP
60 return 0;
61}
62
63static void
64parse_options(int argc, char *argv[])
65{
87c84891
JP
66 enum {
67 OPT_DUMMY = UCHAR_MAX + 1,
68 VLOG_OPTION_ENUMS
69 };
064af421
BP
70 static struct option long_options[] = {
71 {"timeout", required_argument, 0, 't'},
064af421
BP
72 {"help", no_argument, 0, 'h'},
73 {"version", no_argument, 0, 'V'},
87c84891 74 VLOG_LONG_OPTIONS,
064af421
BP
75 {0, 0, 0, 0},
76 };
77 char *short_options = long_options_to_short_options(long_options);
78
79 for (;;) {
80 unsigned long int timeout;
81 int c;
82
83 c = getopt_long(argc, argv, short_options, long_options, NULL);
84 if (c == -1) {
85 break;
86 }
87
88 switch (c) {
89 case 't':
90 timeout = strtoul(optarg, NULL, 10);
91 if (timeout <= 0) {
92 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
93 optarg);
94 } else {
95 time_alarm(timeout);
96 }
97 break;
98
99 case 'h':
100 usage();
101
102 case 'V':
103 OVS_PRINT_VERSION(0, 0);
104 exit(EXIT_SUCCESS);
105
87c84891 106 VLOG_OPTION_HANDLERS
064af421
BP
107
108 case '?':
109 exit(EXIT_FAILURE);
110
111 default:
112 abort();
113 }
114 }
115 free(short_options);
116}
117
118static void
119usage(void)
120{
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"
b566902b 127 " dump-dps display names of all datapaths\n"
064af421
BP
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"
f1588b1f 131 " del-flows DP delete all flows from DP\n",
064af421
BP
132 program_name, program_name);
133 vlog_usage();
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");
138 exit(EXIT_SUCCESS);
139}
140
141static void run(int retval, const char *message, ...)
142 PRINTF_FORMAT(2, 3);
143
144static void run(int retval, const char *message, ...)
145{
146 if (retval) {
147 va_list args;
148
149 fprintf(stderr, "%s: ", program_name);
150 va_start(args, message);
151 vfprintf(stderr, message, args);
152 va_end(args);
153 if (retval == EOF) {
154 fputs(": unexpected end of file\n", stderr);
155 } else {
156 fprintf(stderr, ": %s\n", strerror(retval));
157 }
158
159 exit(EXIT_FAILURE);
160 }
161}
162\f
163static void do_add_if(int argc, char *argv[]);
164
165static int if_up(const char *netdev_name)
166{
167 struct netdev *netdev;
168 int retval;
169
149f577a 170 retval = netdev_open_default(netdev_name, &netdev);
064af421
BP
171 if (!retval) {
172 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
173 netdev_close(netdev);
174 }
175 return retval;
176}
177
1a6f1e2a
JG
178static int
179parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
180{
181 int result;
182 char *name, *type;
183
184 dp_parse_name(arg_, &name, &type);
185
186 if (create) {
187 result = dpif_create(name, type, dpifp);
188 } else {
189 result = dpif_open(name, type, dpifp);
190 }
191
192 free(name);
193 free(type);
194 return result;
195}
196
064af421 197static void
67a4917b 198do_add_dp(int argc OVS_UNUSED, char *argv[])
064af421 199{
c228a364 200 struct dpif *dpif;
1a6f1e2a 201 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
c228a364 202 dpif_close(dpif);
064af421
BP
203 if (argc > 2) {
204 do_add_if(argc, argv);
205 }
206}
207
208static void
67a4917b 209do_del_dp(int argc OVS_UNUSED, char *argv[])
064af421 210{
c228a364 211 struct dpif *dpif;
1a6f1e2a 212 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
c228a364
BP
213 run(dpif_delete(dpif), "del_dp");
214 dpif_close(dpif);
064af421
BP
215}
216
217static int
218compare_ports(const void *a_, const void *b_)
219{
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;
223}
224
225static void
226query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
227{
228 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
229 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
230}
231
064af421 232static void
67a4917b 233do_add_if(int argc OVS_UNUSED, char *argv[])
064af421
BP
234{
235 bool failure = false;
c228a364 236 struct dpif *dpif;
064af421
BP
237 int i;
238
1a6f1e2a 239 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
064af421
BP
240 for (i = 2; i < argc; i++) {
241 char *save_ptr = NULL;
c3827f61
BP
242 struct netdev_options options;
243 struct netdev *netdev;
244 struct shash args;
245 char *option;
064af421
BP
246 int error;
247
c3827f61
BP
248 options.name = strtok_r(argv[i], ",", &save_ptr);
249 options.type = "system";
250 options.args = &args;
251 options.ethertype = NETDEV_ETH_TYPE_NONE;
252
253 if (!options.name) {
064af421
BP
254 ovs_error(0, "%s is not a valid network device name", argv[i]);
255 continue;
256 }
257
c3827f61
BP
258 shash_init(&args);
259 while ((option = strtok_r(NULL, "", &save_ptr)) != NULL) {
260 char *save_ptr_2 = NULL;
261 char *key, *value;
262
263 key = strtok_r(option, "=", &save_ptr_2);
264 value = strtok_r(NULL, "", &save_ptr_2);
265 if (!value) {
266 value = "";
267 }
268
269 if (!strcmp(key, "type")) {
270 options.type = value;
271 } else if (!shash_add_once(&args, key, value)) {
272 ovs_error(0, "duplicate \"%s\" option", key);
064af421
BP
273 }
274 }
064af421 275
c3827f61
BP
276 error = netdev_open(&options, &netdev);
277 if (error) {
278 ovs_error(error, "%s: failed to open network device",
279 options.name);
280 } else {
281 error = dpif_port_add(dpif, netdev, NULL);
282 if (error) {
283 ovs_error(error, "adding %s to %s failed",
284 options.name, argv[1]);
285 } else {
286 error = if_up(options.name);
287 }
288 netdev_close(netdev);
289 }
064af421 290 if (error) {
064af421
BP
291 failure = true;
292 }
293 }
c228a364 294 dpif_close(dpif);
064af421
BP
295 if (failure) {
296 exit(EXIT_FAILURE);
297 }
298}
299
300static bool
301get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
302{
303 struct odp_port *ports;
304 size_t n_ports;
305 size_t i;
306
307 query_ports(dpif, &ports, &n_ports);
308 for (i = 0; i < n_ports; i++) {
309 if (!strcmp(name, ports[i].devname)) {
310 *port = ports[i].port;
311 free(ports);
312 return true;
313 }
314 }
315 free(ports);
316 ovs_error(0, "no port named %s", name);
317 return false;
318}
319
320static void
67a4917b 321do_del_if(int argc OVS_UNUSED, char *argv[])
064af421
BP
322{
323 bool failure = false;
c228a364 324 struct dpif *dpif;
064af421
BP
325 int i;
326
1a6f1e2a 327 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
064af421
BP
328 for (i = 2; i < argc; i++) {
329 const char *name = argv[i];
330 uint16_t port;
331 int error;
332
333 if (!name[strspn(name, "0123456789")]) {
334 port = atoi(name);
c228a364 335 } else if (!get_port_number(dpif, name, &port)) {
064af421
BP
336 failure = true;
337 continue;
338 }
339
c228a364 340 error = dpif_port_del(dpif, port);
064af421
BP
341 if (error) {
342 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
343 failure = true;
344 }
345 }
c228a364 346 dpif_close(dpif);
064af421
BP
347 if (failure) {
348 exit(EXIT_FAILURE);
349 }
350}
351
352static void
353show_dpif(struct dpif *dpif)
354{
355 struct odp_port *ports;
356 struct odp_stats stats;
357 size_t n_ports;
358 size_t i;
359
b29ba128 360 printf("%s:\n", dpif_name(dpif));
064af421
BP
361 if (!dpif_get_dp_stats(dpif, &stats)) {
362 printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
363 "hard-max:%"PRIu32"\n",
364 stats.n_flows, stats.cur_capacity, stats.max_capacity);
365 printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
366 stats.n_ports, stats.max_ports);
2886875a
BP
367 printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
368 (unsigned long long int) stats.n_frags,
369 (unsigned long long int) stats.n_hit,
370 (unsigned long long int) stats.n_missed,
371 (unsigned long long int) stats.n_lost);
064af421
BP
372 printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
373 stats.max_miss_queue, stats.max_action_queue);
374 }
375 query_ports(dpif, &ports, &n_ports);
376 for (i = 0; i < n_ports; i++) {
c3827f61 377 const struct odp_port *p = &ports[i];
0ae60917 378 struct ds ds;
c3827f61
BP
379
380 printf("\tport %u: %s", p->port, p->devname);
0ae60917
JP
381
382 ds_init(&ds);
383 format_odp_port_type(&ds, p);
384 printf("%s\n", ds_cstr(&ds));
385 ds_destroy(&ds);
064af421
BP
386 }
387 free(ports);
388 dpif_close(dpif);
389}
390
391static void
c4fca56a 392do_show(int argc, char *argv[])
064af421
BP
393{
394 bool failure = false;
395 if (argc > 1) {
396 int i;
397 for (i = 1; i < argc; i++) {
398 const char *name = argv[i];
c228a364 399 struct dpif *dpif;
064af421
BP
400 int error;
401
1a6f1e2a 402 error = parsed_dpif_open(name, false, &dpif);
064af421 403 if (!error) {
c228a364 404 show_dpif(dpif);
064af421
BP
405 } else {
406 ovs_error(error, "opening datapath %s failed", name);
407 failure = true;
408 }
409 }
410 } else {
411 unsigned int i;
412 for (i = 0; i < ODP_MAX; i++) {
413 char name[128];
c228a364 414 struct dpif *dpif;
064af421
BP
415 int error;
416
417 sprintf(name, "dp%u", i);
1a6f1e2a 418 error = parsed_dpif_open(name, false, &dpif);
064af421 419 if (!error) {
c228a364 420 show_dpif(dpif);
064af421
BP
421 } else if (error != ENODEV) {
422 ovs_error(error, "opening datapath %s failed", name);
423 failure = true;
424 }
425 }
426 }
427 if (failure) {
428 exit(EXIT_FAILURE);
429 }
430}
431
b566902b 432static void
67a4917b 433do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
b566902b 434{
1a6f1e2a 435 struct svec dpif_names, dpif_types;
b566902b 436 unsigned int i;
1a6f1e2a 437 int error = 0;
b566902b 438
1a6f1e2a
JG
439 svec_init(&dpif_names);
440 svec_init(&dpif_types);
441 dp_enumerate_types(&dpif_types);
b566902b 442
1a6f1e2a
JG
443 for (i = 0; i < dpif_types.n; i++) {
444 unsigned int j;
445 int retval;
446
447 retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
448 if (retval) {
449 error = retval;
450 }
451
452 for (j = 0; j < dpif_names.n; j++) {
453 struct dpif *dpif;
454 if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
455 printf("%s\n", dpif_name(dpif));
456 dpif_close(dpif);
457 }
b566902b 458 }
b566902b
JP
459 }
460
1a6f1e2a
JG
461 svec_destroy(&dpif_names);
462 svec_destroy(&dpif_types);
b566902b
JP
463 if (error) {
464 exit(EXIT_FAILURE);
465 }
466}
467
064af421 468static void
67a4917b 469do_dump_flows(int argc OVS_UNUSED, char *argv[])
064af421
BP
470{
471 struct odp_flow *flows;
c228a364 472 struct dpif *dpif;
064af421
BP
473 size_t n_flows;
474 struct ds ds;
475 size_t i;
476
1a6f1e2a 477 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
c228a364 478 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
064af421
BP
479
480 ds_init(&ds);
481 for (i = 0; i < n_flows; i++) {
482 struct odp_flow *f = &flows[i];
cdee00fd
BP
483 enum { MAX_ACTIONS = 4096 }; /* An arbitrary but large number. */
484 struct nlattr actions[MAX_ACTIONS];
064af421
BP
485
486 f->actions = actions;
cdee00fd 487 f->actions_len = sizeof actions;
379c2564
BP
488 if (!dpif_flow_get(dpif, f)) {
489 ds_clear(&ds);
490 format_odp_flow(&ds, f);
491 printf("%s\n", ds_cstr(&ds));
492 }
064af421
BP
493 }
494 ds_destroy(&ds);
c228a364 495 dpif_close(dpif);
064af421
BP
496}
497
498static void
67a4917b 499do_del_flows(int argc OVS_UNUSED, char *argv[])
064af421 500{
c228a364 501 struct dpif *dpif;
064af421 502
1a6f1e2a 503 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
c228a364
BP
504 run(dpif_flow_flush(dpif), "deleting all flows");
505 dpif_close(dpif);
064af421
BP
506}
507
064af421 508static void
67a4917b 509do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
064af421
BP
510{
511 usage();
512}
513
675febfa 514static const struct command all_commands[] = {
064af421
BP
515 { "add-dp", 1, INT_MAX, do_add_dp },
516 { "del-dp", 1, 1, do_del_dp },
517 { "add-if", 2, INT_MAX, do_add_if },
518 { "del-if", 2, INT_MAX, do_del_if },
b566902b 519 { "dump-dps", 0, 0, do_dump_dps },
064af421
BP
520 { "show", 0, INT_MAX, do_show },
521 { "dump-flows", 1, 1, do_dump_flows },
522 { "del-flows", 1, 1, do_del_flows },
064af421
BP
523 { "help", 0, INT_MAX, do_help },
524 { NULL, 0, 0, NULL },
525};