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