xenserver: Store XAPI dbcache as XML in interface-reconfigure.
[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 "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 void
247 do_add_if(int argc UNUSED, char *argv[])
248 {
249     bool failure = false;
250     struct dpif *dpif;
251     int i;
252
253     run(dpif_open(argv[1], &dpif), "opening datapath");
254     for (i = 2; i < argc; i++) {
255         char *save_ptr = NULL;
256         char *devname, *suboptions;
257         int flags = 0;
258         int error;
259
260         devname = strtok_r(argv[i], ",", &save_ptr);
261         if (!devname) {
262             ovs_error(0, "%s is not a valid network device name", argv[i]);
263             continue;
264         }
265
266         suboptions = strtok_r(NULL, "", &save_ptr);
267         if (suboptions) {
268             enum {
269                 AP_INTERNAL
270             };
271             static char *options[] = {
272                 "internal"
273             };
274
275             while (*suboptions != '\0') {
276                 char *value;
277
278                 switch (getsubopt(&suboptions, options, &value)) {
279                 case AP_INTERNAL:
280                     flags |= ODP_PORT_INTERNAL;
281                     break;
282
283                 default:
284                     ovs_error(0, "unknown suboption '%s'", value);
285                     break;
286                 }
287             }
288         }
289
290         error = dpif_port_add(dpif, devname, flags, NULL);
291         if (error) {
292             ovs_error(error, "adding %s to %s failed", devname, argv[1]);
293             failure = true;
294         } else if (if_up(devname)) {
295             failure = true;
296         }
297     }
298     dpif_close(dpif);
299     if (failure) {
300         exit(EXIT_FAILURE);
301     }
302 }
303
304 static bool
305 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
306 {
307     struct odp_port *ports;
308     size_t n_ports;
309     size_t i;
310
311     query_ports(dpif, &ports, &n_ports);
312     for (i = 0; i < n_ports; i++) {
313         if (!strcmp(name, ports[i].devname)) {
314             *port = ports[i].port;
315             free(ports);
316             return true;
317         }
318     }
319     free(ports);
320     ovs_error(0, "no port named %s", name);
321     return false;
322 }
323
324 static void
325 do_del_if(int argc UNUSED, char *argv[])
326 {
327     bool failure = false;
328     struct dpif *dpif;
329     int i;
330
331     run(dpif_open(argv[1], &dpif), "opening datapath");
332     for (i = 2; i < argc; i++) {
333         const char *name = argv[i];
334         uint16_t port;
335         int error;
336
337         if (!name[strspn(name, "0123456789")]) {
338             port = atoi(name);
339         } else if (!get_port_number(dpif, name, &port)) {
340             failure = true;
341             continue;
342         }
343
344         error = dpif_port_del(dpif, port);
345         if (error) {
346             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
347             failure = true;
348         }
349     }
350     dpif_close(dpif);
351     if (failure) {
352         exit(EXIT_FAILURE);
353     }
354 }
355
356 static void
357 show_dpif(struct dpif *dpif)
358 {
359     struct odp_port *ports;
360     struct odp_stats stats;
361     size_t n_ports;
362     size_t i;
363
364     printf("%s:\n", dpif_name(dpif));
365     if (!dpif_get_dp_stats(dpif, &stats)) {
366         printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
367                "hard-max:%"PRIu32"\n",
368                stats.n_flows, stats.cur_capacity, stats.max_capacity);
369         printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
370                stats.n_ports, stats.max_ports);
371         printf("\tgroups: max:%"PRIu16"\n", stats.max_groups);
372         printf("\tlookups: frags:%"PRIu64", hit:%"PRIu64", missed:%"PRIu64", "
373                "lost:%"PRIu64"\n",
374                stats.n_frags, stats.n_hit, stats.n_missed, stats.n_lost);
375         printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
376                stats.max_miss_queue, stats.max_action_queue);
377     }
378     query_ports(dpif, &ports, &n_ports);
379     for (i = 0; i < n_ports; i++) {
380         printf("\tport %u: %s", ports[i].port, ports[i].devname);
381         if (ports[i].flags & ODP_PORT_INTERNAL) {
382             printf(" (internal)");
383         }
384         printf("\n");
385     }
386     free(ports);
387     dpif_close(dpif);
388 }
389
390 static void
391 do_show(int argc UNUSED, char *argv[])
392 {
393     bool failure = false;
394     if (argc > 1) {
395         int i;
396         for (i = 1; i < argc; i++) {
397             const char *name = argv[i];
398             struct dpif *dpif;
399             int error;
400
401             error = dpif_open(name, &dpif);
402             if (!error) {
403                 show_dpif(dpif);
404             } else {
405                 ovs_error(error, "opening datapath %s failed", name);
406                 failure = true;
407             }
408         }
409     } else {
410         unsigned int i;
411         for (i = 0; i < ODP_MAX; i++) {
412             char name[128];
413             struct dpif *dpif;
414             int error;
415
416             sprintf(name, "dp%u", i);
417             error = dpif_open(name, &dpif);
418             if (!error) {
419                 show_dpif(dpif);
420             } else if (error != ENODEV) {
421                 ovs_error(error, "opening datapath %s failed", name);
422                 failure = true;
423             }
424         }
425     }
426     if (failure) {
427         exit(EXIT_FAILURE);
428     }
429 }
430
431 static void
432 do_dump_flows(int argc UNUSED, char *argv[])
433 {
434     struct odp_flow *flows;
435     struct dpif *dpif;
436     size_t n_flows;
437     struct ds ds;
438     size_t i;
439
440     run(dpif_open(argv[1], &dpif), "opening datapath");
441     run(dpif_flow_list_all(dpif, &flows, &n_flows), "listing all flows");
442
443     ds_init(&ds);
444     for (i = 0; i < n_flows; i++) {
445         struct odp_flow *f = &flows[i];
446         enum { MAX_ACTIONS = 4096 / sizeof(union odp_action) };
447         union odp_action actions[MAX_ACTIONS];
448
449         f->actions = actions;
450         f->n_actions = MAX_ACTIONS;
451         dpif_flow_get(dpif, f);
452
453         ds_clear(&ds);
454         format_odp_flow(&ds, f);
455         printf("%s\n", ds_cstr(&ds));
456     }
457     ds_destroy(&ds);
458     dpif_close(dpif);
459 }
460
461 static void
462 do_del_flows(int argc UNUSED, char *argv[])
463 {
464     struct dpif *dpif;
465
466     run(dpif_open(argv[1], &dpif), "opening datapath");
467     run(dpif_flow_flush(dpif), "deleting all flows");
468     dpif_close(dpif);
469 }
470
471 static void
472 do_dump_groups(int argc UNUSED, char *argv[])
473 {
474     struct odp_stats stats;
475     struct dpif *dpif;
476     unsigned int i;
477
478     run(dpif_open(argv[1], &dpif), "opening datapath");
479     run(dpif_get_dp_stats(dpif, &stats), "get datapath stats");
480     for (i = 0; i < stats.max_groups; i++) {
481         uint16_t *ports;
482         size_t n_ports;
483
484         if (!dpif_port_group_get(dpif, i, &ports, &n_ports) && n_ports) {
485             size_t j;
486
487             printf("group %u:", i);
488             for (j = 0; j < n_ports; j++) {
489                 printf(" %"PRIu16, ports[j]);
490             }
491             printf("\n");
492         }
493         free(ports);
494     }
495     dpif_close(dpif);
496 }
497
498 static void
499 do_help(int argc UNUSED, char *argv[] UNUSED)
500 {
501     usage();
502 }
503
504 static struct command all_commands[] = {
505     { "add-dp", 1, INT_MAX, do_add_dp },
506     { "del-dp", 1, 1, do_del_dp },
507     { "add-if", 2, INT_MAX, do_add_if },
508     { "del-if", 2, INT_MAX, do_del_if },
509     { "show", 0, INT_MAX, do_show },
510     { "dump-flows", 1, 1, do_dump_flows },
511     { "del-flows", 1, 1, do_del_flows },
512     { "dump-groups", 1, 1, do_dump_groups },
513     { "help", 0, INT_MAX, do_help },
514     { NULL, 0, 0, NULL },
515 };