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