381c936f80635aa457ced9f883fb619cc268055b
[cascardo/ovs.git] / utilities / ovs-dpctl.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 "timeval.h"
40 #include "util.h"
41
42 #include "vlog.h"
43 #define THIS_MODULE VLM_dpctl
44
45 struct command {
46     const char *name;
47     int min_args;
48     int max_args;
49     void (*handler)(int argc, char *argv[]);
50 };
51
52 static struct command all_commands[];
53
54 static void usage(void) NO_RETURN;
55 static void parse_options(int argc, char *argv[]);
56
57 int main(int argc, char *argv[])
58 {
59     struct command *p;
60
61     set_program_name(argv[0]);
62     time_init();
63     vlog_init();
64     parse_options(argc, argv);
65     signal(SIGPIPE, SIG_IGN);
66
67     argc -= optind;
68     argv += optind;
69     if (argc < 1)
70         ovs_fatal(0, "missing command name; use --help for help");
71
72     for (p = all_commands; p->name != NULL; p++) {
73         if (!strcmp(p->name, argv[0])) {
74             int n_arg = argc - 1;
75             if (n_arg < p->min_args)
76                 ovs_fatal(0, "'%s' command requires at least %d arguments",
77                           p->name, p->min_args);
78             else if (n_arg > p->max_args)
79                 ovs_fatal(0, "'%s' command takes at most %d arguments",
80                           p->name, p->max_args);
81             else {
82                 p->handler(argc, argv);
83                 if (ferror(stdout)) {
84                     ovs_fatal(0, "write to stdout failed");
85                 }
86                 if (ferror(stderr)) {
87                     ovs_fatal(0, "write to stderr failed");
88                 }
89                 exit(0);
90             }
91         }
92     }
93     ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
94
95     return 0;
96 }
97
98 static void
99 parse_options(int argc, char *argv[])
100 {
101     static struct option long_options[] = {
102         {"timeout", required_argument, 0, 't'},
103         {"verbose", optional_argument, 0, 'v'},
104         {"help", no_argument, 0, 'h'},
105         {"version", no_argument, 0, 'V'},
106         {0, 0, 0, 0},
107     };
108     char *short_options = long_options_to_short_options(long_options);
109
110     for (;;) {
111         unsigned long int timeout;
112         int c;
113
114         c = getopt_long(argc, argv, short_options, long_options, NULL);
115         if (c == -1) {
116             break;
117         }
118
119         switch (c) {
120         case 't':
121             timeout = strtoul(optarg, NULL, 10);
122             if (timeout <= 0) {
123                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
124                           optarg);
125             } else {
126                 time_alarm(timeout);
127             }
128             break;
129
130         case 'h':
131             usage();
132
133         case 'V':
134             OVS_PRINT_VERSION(0, 0);
135             exit(EXIT_SUCCESS);
136
137         case 'v':
138             vlog_set_verbosity(optarg);
139             break;
140
141         case '?':
142             exit(EXIT_FAILURE);
143
144         default:
145             abort();
146         }
147     }
148     free(short_options);
149 }
150
151 static void
152 usage(void)
153 {
154     printf("%s: Open vSwitch datapath management utility\n"
155            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
156            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
157            "  del-dp DP                delete local datapath DP\n"
158            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
159            "  del-if DP IFACE...       delete each IFACE from DP\n"
160            "  show                     show basic info on all datapaths\n"
161            "  show DP...               show basic info on each DP\n"
162            "  dump-flows DP            display flows in DP\n"
163            "  del-flows DP             delete all flows from DP\n"
164            "  dump-groups DP           display port groups in DP\n",
165            program_name, program_name);
166     vlog_usage();
167     printf("\nOther options:\n"
168            "  -t, --timeout=SECS          give up after SECS seconds\n"
169            "  -h, --help                  display this help message\n"
170            "  -V, --version               display version information\n");
171     exit(EXIT_SUCCESS);
172 }
173
174 static void run(int retval, const char *message, ...)
175     PRINTF_FORMAT(2, 3);
176
177 static void run(int retval, const char *message, ...)
178 {
179     if (retval) {
180         va_list args;
181
182         fprintf(stderr, "%s: ", program_name);
183         va_start(args, message);
184         vfprintf(stderr, message, args);
185         va_end(args);
186         if (retval == EOF) {
187             fputs(": unexpected end of file\n", stderr);
188         } else {
189             fprintf(stderr, ": %s\n", strerror(retval));
190         }
191
192         exit(EXIT_FAILURE);
193     }
194 }
195 \f
196 static void do_add_if(int argc, char *argv[]);
197
198 static int if_up(const char *netdev_name)
199 {
200     struct netdev *netdev;
201     int retval;
202
203     retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
204     if (!retval) {
205         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
206         netdev_close(netdev);
207     }
208     return retval;
209 }
210
211 static void
212 do_add_dp(int argc UNUSED, char *argv[])
213 {
214     struct dpif dpif;
215     run(dpif_create(argv[1], &dpif), "add_dp");
216     dpif_close(&dpif);
217     if (argc > 2) {
218         do_add_if(argc, argv);
219     }
220 }
221
222 static void
223 do_del_dp(int argc UNUSED, char *argv[])
224 {
225     struct dpif dpif;
226     run(dpif_open(argv[1], &dpif), "opening datapath");
227     run(dpif_delete(&dpif), "del_dp");
228     dpif_close(&dpif);
229 }
230
231 static int
232 compare_ports(const void *a_, const void *b_)
233 {
234     const struct odp_port *a = a_;
235     const struct odp_port *b = b_;
236     return a->port < b->port ? -1 : a->port > b->port;
237 }
238
239 static void
240 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
241 {
242     run(dpif_port_list(dpif, ports, n_ports), "listing ports");
243     qsort(*ports, *n_ports, sizeof **ports, compare_ports);
244 }
245
246 static uint16_t
247 get_free_port(struct dpif *dpif)
248 {
249     struct odp_port *ports;
250     size_t n_ports;
251     int port_no;
252
253     query_ports(dpif, &ports, &n_ports);
254     for (port_no = 0; port_no <= UINT16_MAX; port_no++) {
255         size_t i;
256         for (i = 0; i < n_ports; i++) {
257             if (ports[i].port == port_no) {
258                 goto next_portno;
259             }
260         }
261         free(ports);
262         return port_no;
263
264     next_portno: ;
265     }
266     ovs_fatal(0, "no free datapath ports");
267 }
268
269 static void
270 do_add_if(int argc UNUSED, char *argv[])
271 {
272     bool failure = false;
273     struct dpif dpif;
274     int i;
275
276     run(dpif_open(argv[1], &dpif), "opening datapath");
277     for (i = 2; i < argc; i++) {
278         char *save_ptr = NULL;
279         char *devname, *suboptions;
280         int port = -1;
281         int flags = 0;
282         int error;
283
284         devname = strtok_r(argv[i], ",", &save_ptr);
285         if (!devname) {
286             ovs_error(0, "%s is not a valid network device name", argv[i]);
287             continue;
288         }
289
290         suboptions = strtok_r(NULL, "", &save_ptr);
291         if (suboptions) {
292             enum {
293                 AP_PORT,
294                 AP_INTERNAL
295             };
296             static char *options[] = {
297                 "port",
298                 "internal"
299             };
300
301             while (*suboptions != '\0') {
302                 char *value;
303
304                 switch (getsubopt(&suboptions, options, &value)) {
305                 case AP_PORT:
306                     if (!value) {
307                         ovs_error(0, "'port' suboption requires a value");
308                     }
309                     port = atoi(value);
310                     break;
311
312                 case AP_INTERNAL:
313                     flags |= ODP_PORT_INTERNAL;
314                     break;
315
316                 default:
317                     ovs_error(0, "unknown suboption '%s'", value);
318                     break;
319                 }
320             }
321         }
322         if (port < 0) {
323             port = get_free_port(&dpif);
324         }
325
326         error = dpif_port_add(&dpif, devname, port, flags);
327         if (error) {
328             ovs_error(error, "adding %s as port %"PRIu16" of %s failed",
329                       devname, port, argv[1]);
330             failure = true;
331         } else if (if_up(devname)) {
332             failure = true;
333         }
334     }
335     dpif_close(&dpif);
336     if (failure) {
337         exit(EXIT_FAILURE);
338     }
339 }
340
341 static bool
342 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
343 {
344     struct odp_port *ports;
345     size_t n_ports;
346     size_t i;
347
348     query_ports(dpif, &ports, &n_ports);
349     for (i = 0; i < n_ports; i++) {
350         if (!strcmp(name, ports[i].devname)) {
351             *port = ports[i].port;
352             free(ports);
353             return true;
354         }
355     }
356     free(ports);
357     ovs_error(0, "no port named %s", name);
358     return false;
359 }
360
361 static void
362 do_del_if(int argc UNUSED, char *argv[])
363 {
364     bool failure = false;
365     struct dpif dpif;
366     int i;
367
368     run(dpif_open(argv[1], &dpif), "opening datapath");
369     for (i = 2; i < argc; i++) {
370         const char *name = argv[i];
371         uint16_t port;
372         int error;
373
374         if (!name[strspn(name, "0123456789")]) {
375             port = atoi(name);
376         } else if (!get_port_number(&dpif, name, &port)) {
377             failure = true;
378             continue;
379         }
380
381         error = dpif_port_del(&dpif, port);
382         if (error) {
383             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
384             failure = true;
385         }
386     }
387     dpif_close(&dpif);
388     if (failure) {
389         exit(EXIT_FAILURE);
390     }
391 }
392
393 static void
394 show_dpif(struct dpif *dpif)
395 {
396     struct odp_port *ports;
397     struct odp_stats stats;
398     size_t n_ports;
399     size_t i;
400
401     printf("dp%u:\n", dpif_id(dpif));
402     if (!dpif_get_dp_stats(dpif, &stats)) {
403         printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
404                "hard-max:%"PRIu32"\n",
405                stats.n_flows, stats.cur_capacity, stats.max_capacity);
406         printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
407                stats.n_ports, stats.max_ports);
408         printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
409         printf("\tlookups: frags:%"PRIu64", hit:%"PRIu64", missed:%"PRIu64", "
410                "lost:%"PRIu64"\n",
411                stats.n_frags, stats.n_hit, stats.n_missed, stats.n_lost);
412         printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
413                stats.max_miss_queue, stats.max_action_queue);
414     }
415     query_ports(dpif, &ports, &n_ports);
416     for (i = 0; i < n_ports; i++) {
417         printf("\tport %u: %s", ports[i].port, ports[i].devname);
418         if (ports[i].flags & ODP_PORT_INTERNAL) {
419             printf(" (internal)");
420         }
421         printf("\n");
422     }
423     free(ports);
424     dpif_close(dpif);
425 }
426
427 static void
428 do_show(int argc UNUSED, char *argv[])
429 {
430     bool failure = false;
431     if (argc > 1) {
432         int i;
433         for (i = 1; i < argc; i++) {
434             const char *name = argv[i];
435             struct dpif dpif;
436             int error;
437
438             error = dpif_open(name, &dpif);
439             if (!error) {
440                 show_dpif(&dpif);
441             } else {
442                 ovs_error(error, "opening datapath %s failed", name);
443                 failure = true;
444             }
445         }
446     } else {
447         unsigned int i;
448         for (i = 0; i < ODP_MAX; i++) {
449             char name[128];
450             struct dpif dpif;
451             int error;
452
453             sprintf(name, "dp%u", i);
454             error = dpif_open(name, &dpif);
455             if (!error) {
456                 show_dpif(&dpif);
457             } else if (error != ENODEV) {
458                 ovs_error(error, "opening datapath %s failed", name);
459                 failure = true;
460             }
461         }
462     }
463     if (failure) {
464         exit(EXIT_FAILURE);
465     }
466 }
467
468 static void
469 do_dump_flows(int argc UNUSED, char *argv[])
470 {
471     struct odp_flow *flows;
472     struct dpif dpif;
473     size_t n_flows;
474     struct ds ds;
475     size_t i;
476
477     run(dpif_open(argv[1], &dpif), "opening datapath");
478     run(dpif_flow_list_all(&dpif, &flows, &n_flows), "listing all flows");
479
480     ds_init(&ds);
481     for (i = 0; i < n_flows; i++) {
482         struct odp_flow *f = &flows[i];
483         enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
484         union odp_action actions[MAX_ACTIONS];
485
486         f->actions = actions;
487         f->n_actions = MAX_ACTIONS;
488         dpif_flow_get(&dpif, f);
489
490         ds_clear(&ds);
491         format_odp_flow(&ds, f);
492         printf("%s\n", ds_cstr(&ds));
493     }
494     ds_destroy(&ds);
495     dpif_close(&dpif);
496 }
497
498 static void
499 do_del_flows(int argc UNUSED, char *argv[])
500 {
501     struct dpif dpif;
502
503     run(dpif_open(argv[1], &dpif), "opening datapath");
504     run(dpif_flow_flush(&dpif), "deleting all flows");
505     dpif_close(&dpif);
506 }
507
508 static void
509 do_dump_groups(int argc UNUSED, char *argv[])
510 {
511     struct odp_stats stats;
512     struct dpif dpif;
513     unsigned int i;
514
515     run(dpif_open(argv[1], &dpif), "opening datapath");
516     run(dpif_get_dp_stats(&dpif, &stats), "get datapath stats");
517     for (i = 0; i < stats.max_groups; i++) {
518         uint16_t ports[UINT16_MAX];
519         size_t n_ports;
520
521         if (!dpif_port_group_get(&dpif, i, ports,
522                                  ARRAY_SIZE(ports), &n_ports) && n_ports) {
523             size_t j;
524
525             printf("group %u:", i);
526             for (j = 0; j < n_ports; j++) {
527                 printf(" %"PRIu16, ports[j]);
528             }
529             printf("\n");
530         }
531     }
532     dpif_close(&dpif);
533 }
534
535 static void
536 do_help(int argc UNUSED, char *argv[] UNUSED)
537 {
538     usage();
539 }
540
541 static struct command all_commands[] = {
542     { "add-dp", 1, INT_MAX, do_add_dp },
543     { "del-dp", 1, 1, do_del_dp },
544     { "add-if", 2, INT_MAX, do_add_if },
545     { "del-if", 2, INT_MAX, do_del_if },
546     { "show", 0, INT_MAX, do_show },
547     { "dump-flows", 1, 1, do_dump_flows },
548     { "del-flows", 1, 1, do_del_flows },
549     { "dump-groups", 1, 1, do_dump_groups },
550     { "help", 0, INT_MAX, do_help },
551     { NULL, 0, 0, NULL },
552 };