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