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