vswitchd: Use wdp functions instead of xfif ones for enumerating bridges.
[sliver-openvswitch.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 <sys/socket.h>
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 "dynamic-string.h"
37 #include "netdev.h"
38 #include "xflow-util.h"
39 #include "svec.h"
40 #include "timeval.h"
41 #include "util.h"
42 #include "vlog.h"
43 #include "xfif.h"
44
45 VLOG_DEFINE_THIS_MODULE(dpctl)
46
47 static const struct command all_commands[];
48
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
51
52 int
53 main(int argc, char *argv[])
54 {
55     set_program_name(argv[0]);
56     parse_options(argc, argv);
57     signal(SIGPIPE, SIG_IGN);
58     run_command(argc - optind, argv + optind, all_commands);
59     return 0;
60 }
61
62 static void
63 parse_options(int argc, char *argv[])
64 {
65     enum {
66         OPT_DUMMY = UCHAR_MAX + 1,
67         VLOG_OPTION_ENUMS
68     };
69     static struct option long_options[] = {
70         {"timeout", required_argument, 0, 't'},
71         {"help", no_argument, 0, 'h'},
72         {"version", no_argument, 0, 'V'},
73         VLOG_LONG_OPTIONS,
74         {0, 0, 0, 0},
75     };
76     char *short_options = long_options_to_short_options(long_options);
77
78     for (;;) {
79         unsigned long int timeout;
80         int c;
81
82         c = getopt_long(argc, argv, short_options, long_options, NULL);
83         if (c == -1) {
84             break;
85         }
86
87         switch (c) {
88         case 't':
89             timeout = strtoul(optarg, NULL, 10);
90             if (timeout <= 0) {
91                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
92                           optarg);
93             } else {
94                 time_alarm(timeout);
95             }
96             break;
97
98         case 'h':
99             usage();
100
101         case 'V':
102             OVS_PRINT_VERSION(0, 0);
103             exit(EXIT_SUCCESS);
104
105         VLOG_OPTION_HANDLERS
106
107         case '?':
108             exit(EXIT_FAILURE);
109
110         default:
111             abort();
112         }
113     }
114     free(short_options);
115 }
116
117 static void
118 usage(void)
119 {
120     printf("%s: Open vSwitch datapath management utility\n"
121            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
122            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
123            "  del-dp DP                delete local datapath DP\n"
124            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
125            "  del-if DP IFACE...       delete each IFACE from DP\n"
126            "  dump-dps                 display names of all datapaths\n"
127            "  show                     show basic info on all datapaths\n"
128            "  show DP...               show basic info on each DP\n"
129            "  dump-flows DP            display flows in DP\n"
130            "  del-flows DP             delete all flows from DP\n"
131            "  dump-groups DP           display port groups in DP\n",
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
141 static void run(int retval, const char *message, ...)
142     PRINTF_FORMAT(2, 3);
143
144 static 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
163 static void do_add_if(int argc, char *argv[]);
164
165 static int if_up(const char *netdev_name)
166 {
167     struct netdev *netdev;
168     int retval;
169
170     retval = netdev_open_default(netdev_name, &netdev);
171     if (!retval) {
172         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
173         netdev_close(netdev);
174     }
175     return retval;
176 }
177
178 static int
179 parsed_xfif_open(const char *arg_, bool create, struct xfif **xfifp)
180 {
181     int result;
182     char *name, *type;
183
184     xf_parse_name(arg_, &name, &type);
185
186     if (create) {
187         result = xfif_create(name, type, xfifp);
188     } else {
189         result = xfif_open(name, type, xfifp);
190     }
191
192     free(name);
193     free(type);
194     return result;
195 }
196
197 static void
198 do_add_dp(int argc OVS_UNUSED, char *argv[])
199 {
200     struct xfif *xfif;
201     run(parsed_xfif_open(argv[1], true, &xfif), "add_dp");
202     xfif_close(xfif);
203     if (argc > 2) {
204         do_add_if(argc, argv);
205     }
206 }
207
208 static void
209 do_del_dp(int argc OVS_UNUSED, char *argv[])
210 {
211     struct xfif *xfif;
212     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
213     run(xfif_delete(xfif), "del_dp");
214     xfif_close(xfif);
215 }
216
217 static int
218 compare_ports(const void *a_, const void *b_)
219 {
220     const struct xflow_port *a = a_;
221     const struct xflow_port *b = b_;
222     return a->port < b->port ? -1 : a->port > b->port;
223 }
224
225 static void
226 query_ports(struct xfif *xfif, struct xflow_port **ports, size_t *n_ports)
227 {
228     run(xfif_port_list(xfif, ports, n_ports), "listing ports");
229     qsort(*ports, *n_ports, sizeof **ports, compare_ports);
230 }
231
232 static void
233 do_add_if(int argc OVS_UNUSED, char *argv[])
234 {
235     bool failure = false;
236     struct xfif *xfif;
237     int i;
238
239     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
240     for (i = 2; i < argc; i++) {
241         char *save_ptr = NULL;
242         char *devname, *suboptions;
243         int flags = 0;
244         int error;
245
246         devname = strtok_r(argv[i], ",", &save_ptr);
247         if (!devname) {
248             ovs_error(0, "%s is not a valid network device name", argv[i]);
249             continue;
250         }
251
252         suboptions = strtok_r(NULL, "", &save_ptr);
253         if (suboptions) {
254             enum {
255                 AP_INTERNAL
256             };
257             static char *options[] = {
258                 "internal"
259             };
260
261             while (*suboptions != '\0') {
262                 char *value;
263
264                 switch (getsubopt(&suboptions, options, &value)) {
265                 case AP_INTERNAL:
266                     flags |= XFLOW_PORT_INTERNAL;
267                     break;
268
269                 default:
270                     ovs_error(0, "unknown suboption '%s'", value);
271                     break;
272                 }
273             }
274         }
275
276         error = xfif_port_add(xfif, devname, flags, NULL);
277         if (error) {
278             ovs_error(error, "adding %s to %s failed", devname, argv[1]);
279             failure = true;
280         } else if (if_up(devname)) {
281             failure = true;
282         }
283     }
284     xfif_close(xfif);
285     if (failure) {
286         exit(EXIT_FAILURE);
287     }
288 }
289
290 static bool
291 get_port_number(struct xfif *xfif, const char *name, uint16_t *port)
292 {
293     struct xflow_port *ports;
294     size_t n_ports;
295     size_t i;
296
297     query_ports(xfif, &ports, &n_ports);
298     for (i = 0; i < n_ports; i++) {
299         if (!strcmp(name, ports[i].devname)) {
300             *port = ports[i].port;
301             free(ports);
302             return true;
303         }
304     }
305     free(ports);
306     ovs_error(0, "no port named %s", name);
307     return false;
308 }
309
310 static void
311 do_del_if(int argc OVS_UNUSED, char *argv[])
312 {
313     bool failure = false;
314     struct xfif *xfif;
315     int i;
316
317     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
318     for (i = 2; i < argc; i++) {
319         const char *name = argv[i];
320         uint16_t port;
321         int error;
322
323         if (!name[strspn(name, "0123456789")]) {
324             port = atoi(name);
325         } else if (!get_port_number(xfif, name, &port)) {
326             failure = true;
327             continue;
328         }
329
330         error = xfif_port_del(xfif, port);
331         if (error) {
332             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
333             failure = true;
334         }
335     }
336     xfif_close(xfif);
337     if (failure) {
338         exit(EXIT_FAILURE);
339     }
340 }
341
342 static void
343 show_xfif(struct xfif *xfif)
344 {
345     struct xflow_port *ports;
346     struct xflow_stats stats;
347     size_t n_ports;
348     size_t i;
349
350     printf("%s:\n", xfif_name(xfif));
351     if (!xfif_get_xf_stats(xfif, &stats)) {
352         printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
353                "hard-max:%"PRIu32"\n",
354                stats.n_flows, stats.cur_capacity, stats.max_capacity);
355         printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
356                stats.n_ports, stats.max_ports);
357         printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
358         printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
359                (unsigned long long int) stats.n_frags,
360                (unsigned long long int) stats.n_hit,
361                (unsigned long long int) stats.n_missed,
362                (unsigned long long int) stats.n_lost);
363         printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
364                stats.max_miss_queue, stats.max_action_queue);
365     }
366     query_ports(xfif, &ports, &n_ports);
367     for (i = 0; i < n_ports; i++) {
368         printf("\tport %u: %s", ports[i].port, ports[i].devname);
369         if (ports[i].flags & XFLOW_PORT_INTERNAL) {
370             printf(" (internal)");
371         }
372         printf("\n");
373     }
374     free(ports);
375     xfif_close(xfif);
376 }
377
378 static void
379 do_show(int argc, char *argv[])
380 {
381     bool failure = false;
382     if (argc > 1) {
383         int i;
384         for (i = 1; i < argc; i++) {
385             const char *name = argv[i];
386             struct xfif *xfif;
387             int error;
388
389             error = parsed_xfif_open(name, false, &xfif);
390             if (!error) {
391                 show_xfif(xfif);
392             } else {
393                 ovs_error(error, "opening datapath %s failed", name);
394                 failure = true;
395             }
396         }
397     } else {
398         unsigned int i;
399         for (i = 0; i < XFLOW_MAX; i++) {
400             char name[128];
401             struct xfif *xfif;
402             int error;
403
404             sprintf(name, "dp%u", i);
405             error = parsed_xfif_open(name, false, &xfif);
406             if (!error) {
407                 show_xfif(xfif);
408             } else if (error != ENODEV) {
409                 ovs_error(error, "opening datapath %s failed", name);
410                 failure = true;
411             }
412         }
413     }
414     if (failure) {
415         exit(EXIT_FAILURE);
416     }
417 }
418
419 static void
420 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
421 {
422     struct svec xfif_names, xfif_types;
423     unsigned int i;
424     int error = 0;
425
426     svec_init(&xfif_names);
427     svec_init(&xfif_types);
428     xf_enumerate_types(&xfif_types);
429
430     for (i = 0; i < xfif_types.n; i++) {
431         unsigned int j;
432         int retval;
433
434         retval = xf_enumerate_names(xfif_types.names[i], &xfif_names);
435         if (retval) {
436             error = retval;
437         }
438
439         for (j = 0; j < xfif_names.n; j++) {
440             struct xfif *xfif;
441             if (!xfif_open(xfif_names.names[j], xfif_types.names[i], &xfif)) {
442                 printf("%s\n", xfif_name(xfif));
443                 xfif_close(xfif);
444             }
445         }
446     }
447
448     svec_destroy(&xfif_names);
449     svec_destroy(&xfif_types);
450     if (error) {
451         exit(EXIT_FAILURE);
452     }
453 }
454
455 static void
456 do_dump_flows(int argc OVS_UNUSED, char *argv[])
457 {
458     struct xflow_flow *flows;
459     struct xfif *xfif;
460     size_t n_flows;
461     struct ds ds;
462     size_t i;
463
464     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
465     run(xfif_flow_list_all(xfif, &flows, &n_flows), "listing all flows");
466
467     ds_init(&ds);
468     for (i = 0; i < n_flows; i++) {
469         struct xflow_flow *f = &flows[i];
470         enum { MAX_ACTIONS = 4096 / sizeof(union xflow_action) };
471         union xflow_action actions[MAX_ACTIONS];
472
473         f->actions = actions;
474         f->n_actions = MAX_ACTIONS;
475         if (!xfif_flow_get(xfif, f)) {
476
477             ds_clear(&ds);
478             format_xflow_flow(&ds, f);
479             printf("%s\n", ds_cstr(&ds));
480         }
481     }
482     ds_destroy(&ds);
483     xfif_close(xfif);
484 }
485
486 static void
487 do_del_flows(int argc OVS_UNUSED, char *argv[])
488 {
489     struct xfif *xfif;
490
491     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
492     run(xfif_flow_flush(xfif), "deleting all flows");
493     xfif_close(xfif);
494 }
495
496 static void
497 do_dump_groups(int argc OVS_UNUSED, char *argv[])
498 {
499     struct xflow_stats stats;
500     struct xfif *xfif;
501     unsigned int i;
502
503     run(parsed_xfif_open(argv[1], false, &xfif), "opening datapath");
504     run(xfif_get_xf_stats(xfif, &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 (!xfif_port_group_get(xfif, 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     xfif_close(xfif);
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 };