]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-dpctl.c
Merge "master" into "next".
[mirror_ovs.git] / utilities / ovs-dpctl.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3 *
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:
7 *
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.
15 */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dirs.h"
35 #include "dpif.h"
36 #include "dynamic-string.h"
37 #include "netdev.h"
38 #include "odp-util.h"
39 #include "svec.h"
40 #include "timeval.h"
41 #include "util.h"
42
43 #include "vlog.h"
44 #define THIS_MODULE VLM_dpctl
45
46 static const struct command all_commands[];
47
48 static void usage(void) NO_RETURN;
49 static void parse_options(int argc, char *argv[]);
50
51 int
52 main(int argc, char *argv[])
53 {
54 set_program_name(argv[0]);
55 time_init();
56 vlog_init();
57 parse_options(argc, argv);
58 signal(SIGPIPE, SIG_IGN);
59 run_command(argc - optind, argv + optind, all_commands);
60 return 0;
61 }
62
63 static void
64 parse_options(int argc, char *argv[])
65 {
66 enum {
67 OPT_DUMMY = UCHAR_MAX + 1,
68 VLOG_OPTION_ENUMS
69 };
70 static struct option long_options[] = {
71 {"timeout", required_argument, 0, 't'},
72 {"help", no_argument, 0, 'h'},
73 {"version", no_argument, 0, 'V'},
74 VLOG_LONG_OPTIONS,
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
106 VLOG_OPTION_HANDLERS
107
108 case '?':
109 exit(EXIT_FAILURE);
110
111 default:
112 abort();
113 }
114 }
115 free(short_options);
116 }
117
118 static void
119 usage(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"
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);
134 vlog_usage();
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");
139 exit(EXIT_SUCCESS);
140 }
141
142 static void run(int retval, const char *message, ...)
143 PRINTF_FORMAT(2, 3);
144
145 static void run(int retval, const char *message, ...)
146 {
147 if (retval) {
148 va_list args;
149
150 fprintf(stderr, "%s: ", program_name);
151 va_start(args, message);
152 vfprintf(stderr, message, args);
153 va_end(args);
154 if (retval == EOF) {
155 fputs(": unexpected end of file\n", stderr);
156 } else {
157 fprintf(stderr, ": %s\n", strerror(retval));
158 }
159
160 exit(EXIT_FAILURE);
161 }
162 }
163 \f
164 static void do_add_if(int argc, char *argv[]);
165
166 static int if_up(const char *netdev_name)
167 {
168 struct netdev *netdev;
169 int retval;
170
171 retval = netdev_open_default(netdev_name, &netdev);
172 if (!retval) {
173 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
174 netdev_close(netdev);
175 }
176 return retval;
177 }
178
179 static int
180 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
181 {
182 int result;
183 char *name, *type;
184
185 dp_parse_name(arg_, &name, &type);
186
187 if (create) {
188 result = dpif_create(name, type, dpifp);
189 } else {
190 result = dpif_open(name, type, dpifp);
191 }
192
193 free(name);
194 free(type);
195 return result;
196 }
197
198 static void
199 do_add_dp(int argc OVS_UNUSED, char *argv[])
200 {
201 struct dpif *dpif;
202 run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
203 dpif_close(dpif);
204 if (argc > 2) {
205 do_add_if(argc, argv);
206 }
207 }
208
209 static void
210 do_del_dp(int argc OVS_UNUSED, char *argv[])
211 {
212 struct dpif *dpif;
213 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
214 run(dpif_delete(dpif), "del_dp");
215 dpif_close(dpif);
216 }
217
218 static int
219 compare_ports(const void *a_, const void *b_)
220 {
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;
224 }
225
226 static void
227 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
228 {
229 run(dpif_port_list(dpif, ports, n_ports), "listing ports");
230 qsort(*ports, *n_ports, sizeof **ports, compare_ports);
231 }
232
233 static void
234 do_add_if(int argc OVS_UNUSED, char *argv[])
235 {
236 bool failure = false;
237 struct dpif *dpif;
238 int i;
239
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;
244 int flags = 0;
245 int error;
246
247 devname = strtok_r(argv[i], ",", &save_ptr);
248 if (!devname) {
249 ovs_error(0, "%s is not a valid network device name", argv[i]);
250 continue;
251 }
252
253 suboptions = strtok_r(NULL, "", &save_ptr);
254 if (suboptions) {
255 enum {
256 AP_INTERNAL
257 };
258 static char *options[] = {
259 "internal"
260 };
261
262 while (*suboptions != '\0') {
263 char *value;
264
265 switch (getsubopt(&suboptions, options, &value)) {
266 case AP_INTERNAL:
267 flags |= ODP_PORT_INTERNAL;
268 break;
269
270 default:
271 ovs_error(0, "unknown suboption '%s'", value);
272 break;
273 }
274 }
275 }
276
277 error = dpif_port_add(dpif, devname, flags, NULL);
278 if (error) {
279 ovs_error(error, "adding %s to %s failed", devname, argv[1]);
280 failure = true;
281 } else if (if_up(devname)) {
282 failure = true;
283 }
284 }
285 dpif_close(dpif);
286 if (failure) {
287 exit(EXIT_FAILURE);
288 }
289 }
290
291 static bool
292 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
293 {
294 struct odp_port *ports;
295 size_t n_ports;
296 size_t i;
297
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;
302 free(ports);
303 return true;
304 }
305 }
306 free(ports);
307 ovs_error(0, "no port named %s", name);
308 return false;
309 }
310
311 static void
312 do_del_if(int argc OVS_UNUSED, char *argv[])
313 {
314 bool failure = false;
315 struct dpif *dpif;
316 int i;
317
318 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
319 for (i = 2; i < argc; i++) {
320 const char *name = argv[i];
321 uint16_t port;
322 int error;
323
324 if (!name[strspn(name, "0123456789")]) {
325 port = atoi(name);
326 } else if (!get_port_number(dpif, name, &port)) {
327 failure = true;
328 continue;
329 }
330
331 error = dpif_port_del(dpif, port);
332 if (error) {
333 ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
334 failure = true;
335 }
336 }
337 dpif_close(dpif);
338 if (failure) {
339 exit(EXIT_FAILURE);
340 }
341 }
342
343 static void
344 show_dpif(struct dpif *dpif)
345 {
346 struct odp_port *ports;
347 struct odp_stats stats;
348 size_t n_ports;
349 size_t i;
350
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);
366 }
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)");
372 }
373 printf("\n");
374 }
375 free(ports);
376 dpif_close(dpif);
377 }
378
379 static void
380 do_show(int argc, char *argv[])
381 {
382 bool failure = false;
383 if (argc > 1) {
384 int i;
385 for (i = 1; i < argc; i++) {
386 const char *name = argv[i];
387 struct dpif *dpif;
388 int error;
389
390 error = parsed_dpif_open(name, false, &dpif);
391 if (!error) {
392 show_dpif(dpif);
393 } else {
394 ovs_error(error, "opening datapath %s failed", name);
395 failure = true;
396 }
397 }
398 } else {
399 unsigned int i;
400 for (i = 0; i < ODP_MAX; i++) {
401 char name[128];
402 struct dpif *dpif;
403 int error;
404
405 sprintf(name, "dp%u", i);
406 error = parsed_dpif_open(name, false, &dpif);
407 if (!error) {
408 show_dpif(dpif);
409 } else if (error != ENODEV) {
410 ovs_error(error, "opening datapath %s failed", name);
411 failure = true;
412 }
413 }
414 }
415 if (failure) {
416 exit(EXIT_FAILURE);
417 }
418 }
419
420 static void
421 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
422 {
423 struct svec dpif_names, dpif_types;
424 unsigned int i;
425 int error = 0;
426
427 svec_init(&dpif_names);
428 svec_init(&dpif_types);
429 dp_enumerate_types(&dpif_types);
430
431 for (i = 0; i < dpif_types.n; i++) {
432 unsigned int j;
433 int retval;
434
435 retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
436 if (retval) {
437 error = retval;
438 }
439
440 for (j = 0; j < dpif_names.n; j++) {
441 struct dpif *dpif;
442 if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
443 printf("%s\n", dpif_name(dpif));
444 dpif_close(dpif);
445 }
446 }
447 }
448
449 svec_destroy(&dpif_names);
450 svec_destroy(&dpif_types);
451 if (error) {
452 exit(EXIT_FAILURE);
453 }
454 }
455
456 static void
457 do_dump_flows(int argc OVS_UNUSED, char *argv[])
458 {
459 struct odp_flow *flows;
460 struct dpif *dpif;
461 size_t n_flows;
462 struct ds ds;
463 size_t i;
464
465 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
466 run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
467
468 ds_init(&ds);
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];
473
474 f->actions = actions;
475 f->n_actions = MAX_ACTIONS;
476 dpif_flow_get(dpif, f);
477
478 ds_clear(&ds);
479 format_odp_flow(&ds, f);
480 printf("%s\n", ds_cstr(&ds));
481 }
482 ds_destroy(&ds);
483 dpif_close(dpif);
484 }
485
486 static void
487 do_del_flows(int argc OVS_UNUSED, char *argv[])
488 {
489 struct dpif *dpif;
490
491 run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
492 run(dpif_flow_flush(dpif), "deleting all flows");
493 dpif_close(dpif);
494 }
495
496 static void
497 do_dump_groups(int argc OVS_UNUSED, char *argv[])
498 {
499 struct odp_stats stats;
500 struct dpif *dpif;
501 unsigned int i;
502
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++) {
506 uint16_t *ports;
507 size_t n_ports;
508
509 if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
510 size_t j;
511
512 printf("group %u:", i);
513 for (j = 0; j < n_ports; j++) {
514 printf(" %"PRIu16, ports[j]);
515 }
516 printf("\n");
517 }
518 free(ports);
519 }
520 dpif_close(dpif);
521 }
522
523 static void
524 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
525 {
526 usage();
527 }
528
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 },
541 };