Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / utilities / ovs-dpctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 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 "shash.h"
41 #include "sset.h"
42 #include "timeval.h"
43 #include "util.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(dpctl);
47
48 static const struct command all_commands[];
49
50 static void usage(void) NO_RETURN;
51 static void parse_options(int argc, char *argv[]);
52
53 int
54 main(int argc, char *argv[])
55 {
56     set_program_name(argv[0]);
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     enum {
67         OPT_DUMMY = UCHAR_MAX + 1,
68         VLOG_OPTION_ENUMS
69     };
70     static struct option long_options[] = {
71         {"timeout", required_argument, 0, 't'},
72         {"help", no_argument, 0, 'h'},
73         {"version", no_argument, 0, 'V'},
74         VLOG_LONG_OPTIONS,
75         {0, 0, 0, 0},
76     };
77     char *short_options = long_options_to_short_options(long_options);
78
79     for (;;) {
80         unsigned long int timeout;
81         int c;
82
83         c = getopt_long(argc, argv, short_options, long_options, NULL);
84         if (c == -1) {
85             break;
86         }
87
88         switch (c) {
89         case 't':
90             timeout = strtoul(optarg, NULL, 10);
91             if (timeout <= 0) {
92                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
93                           optarg);
94             } else {
95                 time_alarm(timeout);
96             }
97             break;
98
99         case 'h':
100             usage();
101
102         case 'V':
103             OVS_PRINT_VERSION(0, 0);
104             exit(EXIT_SUCCESS);
105
106         VLOG_OPTION_HANDLERS
107
108         case '?':
109             exit(EXIT_FAILURE);
110
111         default:
112             abort();
113         }
114     }
115     free(short_options);
116 }
117
118 static void
119 usage(void)
120 {
121     printf("%s: Open vSwitch datapath management utility\n"
122            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
123            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
124            "  del-dp DP                delete local datapath DP\n"
125            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
126            "  del-if DP IFACE...       delete each IFACE from DP\n"
127            "  dump-dps                 display names of all datapaths\n"
128            "  show                     show basic info on all datapaths\n"
129            "  show DP...               show basic info on each DP\n"
130            "  dump-flows DP            display flows in DP\n"
131            "  del-flows DP             delete all flows from DP\n",
132            program_name, program_name);
133     vlog_usage();
134     printf("\nOther options:\n"
135            "  -t, --timeout=SECS          give up after SECS seconds\n"
136            "  -h, --help                  display this help message\n"
137            "  -V, --version               display version information\n");
138     exit(EXIT_SUCCESS);
139 }
140
141 static void run(int retval, const char *message, ...)
142     PRINTF_FORMAT(2, 3);
143
144 static void run(int retval, const char *message, ...)
145 {
146     if (retval) {
147         va_list args;
148
149         va_start(args, message);
150         ovs_fatal_valist(retval, message, args);
151     }
152 }
153 \f
154 static void do_add_if(int argc, char *argv[]);
155
156 static int if_up(const char *netdev_name)
157 {
158     struct netdev *netdev;
159     int retval;
160
161     retval = netdev_open_default(netdev_name, &netdev);
162     if (!retval) {
163         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
164         netdev_close(netdev);
165     }
166     return retval;
167 }
168
169 static int
170 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
171 {
172     int result;
173     char *name, *type;
174
175     dp_parse_name(arg_, &name, &type);
176
177     if (create) {
178         result = dpif_create(name, type, dpifp);
179     } else {
180         result = dpif_open(name, type, dpifp);
181     }
182
183     free(name);
184     free(type);
185     return result;
186 }
187
188 static void
189 do_add_dp(int argc OVS_UNUSED, char *argv[])
190 {
191     struct dpif *dpif;
192     run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
193     dpif_close(dpif);
194     if (argc > 2) {
195         do_add_if(argc, argv);
196     }
197 }
198
199 static void
200 do_del_dp(int argc OVS_UNUSED, char *argv[])
201 {
202     struct dpif *dpif;
203     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
204     run(dpif_delete(dpif), "del_dp");
205     dpif_close(dpif);
206 }
207
208 static void
209 do_add_if(int argc OVS_UNUSED, char *argv[])
210 {
211     bool failure = false;
212     struct dpif *dpif;
213     int i;
214
215     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
216     for (i = 2; i < argc; i++) {
217         char *save_ptr = NULL;
218         struct netdev_options options;
219         struct netdev *netdev;
220         struct shash args;
221         char *option;
222         int error;
223
224         options.name = strtok_r(argv[i], ",", &save_ptr);
225         options.type = "system";
226         options.args = &args;
227         options.ethertype = NETDEV_ETH_TYPE_NONE;
228
229         if (!options.name) {
230             ovs_error(0, "%s is not a valid network device name", argv[i]);
231             continue;
232         }
233
234         shash_init(&args);
235         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
236             char *save_ptr_2 = NULL;
237             char *key, *value;
238
239             key = strtok_r(option, "=", &save_ptr_2);
240             value = strtok_r(NULL, "", &save_ptr_2);
241             if (!value) {
242                 value = "";
243             }
244
245             if (!strcmp(key, "type")) {
246                 options.type = value;
247             } else if (!shash_add_once(&args, key, value)) {
248                 ovs_error(0, "duplicate \"%s\" option", key);
249             }
250         }
251
252         error = netdev_open(&options, &netdev);
253         if (error) {
254             ovs_error(error, "%s: failed to open network device",
255                       options.name);
256         } else {
257             error = dpif_port_add(dpif, netdev, NULL);
258             if (error) {
259                 ovs_error(error, "adding %s to %s failed",
260                           options.name, argv[1]);
261             } else {
262                 error = if_up(options.name);
263             }
264             netdev_close(netdev);
265         }
266         if (error) {
267             failure = true;
268         }
269     }
270     dpif_close(dpif);
271     if (failure) {
272         exit(EXIT_FAILURE);
273     }
274 }
275
276 static bool
277 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
278 {
279     struct dpif_port dpif_port;
280
281     if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
282         *port = dpif_port.port_no;
283         dpif_port_destroy(&dpif_port);
284         return true;
285     } else {
286         ovs_error(0, "no port named %s", name);
287         return false;
288     }
289 }
290
291 static void
292 do_del_if(int argc OVS_UNUSED, char *argv[])
293 {
294     bool failure = false;
295     struct dpif *dpif;
296     int i;
297
298     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
299     for (i = 2; i < argc; i++) {
300         const char *name = argv[i];
301         uint16_t port;
302         int error;
303
304         if (!name[strspn(name, "0123456789")]) {
305             port = atoi(name);
306         } else if (!get_port_number(dpif, name, &port)) {
307             failure = true;
308             continue;
309         }
310
311         error = dpif_port_del(dpif, port);
312         if (error) {
313             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
314             failure = true;
315         }
316     }
317     dpif_close(dpif);
318     if (failure) {
319         exit(EXIT_FAILURE);
320     }
321 }
322
323 static void
324 show_dpif(struct dpif *dpif)
325 {
326     struct dpif_port_dump dump;
327     struct dpif_port dpif_port;
328     struct odp_stats stats;
329
330     printf("%s:\n", dpif_name(dpif));
331     if (!dpif_get_dp_stats(dpif, &stats)) {
332         printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
333                (unsigned long long int) stats.n_frags,
334                (unsigned long long int) stats.n_hit,
335                (unsigned long long int) stats.n_missed,
336                (unsigned long long int) stats.n_lost);
337     }
338     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
339         printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
340
341         if (strcmp(dpif_port.type, "system")) {
342             struct netdev_options netdev_options;
343             struct netdev *netdev;
344             int error;
345
346             printf (" (%s", dpif_port.type);
347
348             netdev_options.name = dpif_port.name;
349             netdev_options.type = dpif_port.type;
350             netdev_options.args = NULL;
351             netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
352             error = netdev_open(&netdev_options, &netdev);
353             if (!error) {
354                 const struct shash_node **nodes;
355                 const struct shash *config;
356                 size_t i;
357
358                 config = netdev_get_config(netdev);
359                 nodes = shash_sort(config);
360                 for (i = 0; i < shash_count(config); i++) {
361                     const struct shash_node *node = nodes[i];
362                     printf("%c %s=%s", i ? ',' : ':',
363                            node->name, (char *) node->data);
364                 }
365                 free(nodes);
366
367                 netdev_close(netdev);
368             } else {
369                 printf(": open failed (%s)", strerror(error));
370             }
371             putchar(')');
372         }
373         putchar('\n');
374     }
375     dpif_close(dpif);
376 }
377
378 static void
379 do_show(int argc, char *argv[])
380 {
381     bool failure = false;
382     if (argc > 1) {
383         int i;
384         for (i = 1; i < argc; i++) {
385             const char *name = argv[i];
386             struct dpif *dpif;
387             int error;
388
389             error = parsed_dpif_open(name, false, &dpif);
390             if (!error) {
391                 show_dpif(dpif);
392             } else {
393                 ovs_error(error, "opening datapath %s failed", name);
394                 failure = true;
395             }
396         }
397     } else {
398         struct sset types;
399         const char *type;
400
401         sset_init(&types);
402         dp_enumerate_types(&types);
403         SSET_FOR_EACH (type, &types) {
404             struct sset names;
405             const char *name;
406
407             sset_init(&names);
408             if (dp_enumerate_names(type, &names)) {
409                 failure = true;
410                 continue;
411             }
412             SSET_FOR_EACH (name, &names) {
413                 struct dpif *dpif;
414                 int error;
415
416                 error = dpif_open(name, type, &dpif);
417                 if (!error) {
418                     show_dpif(dpif);
419                 } else {
420                     ovs_error(error, "opening datapath %s failed", name);
421                     failure = true;
422                 }
423             }
424             sset_destroy(&names);
425         }
426         sset_destroy(&types);
427     }
428     if (failure) {
429         exit(EXIT_FAILURE);
430     }
431 }
432
433 static void
434 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
435 {
436     struct sset dpif_names, dpif_types;
437     const char *type;
438     int error = 0;
439
440     sset_init(&dpif_names);
441     sset_init(&dpif_types);
442     dp_enumerate_types(&dpif_types);
443
444     SSET_FOR_EACH (type, &dpif_types) {
445         const char *name;
446         int retval;
447
448         retval = dp_enumerate_names(type, &dpif_names);
449         if (retval) {
450             error = retval;
451         }
452
453         SSET_FOR_EACH (name, &dpif_names) {
454             struct dpif *dpif;
455             if (!dpif_open(name, type, &dpif)) {
456                 printf("%s\n", dpif_name(dpif));
457                 dpif_close(dpif);
458             }
459         }
460     }
461
462     sset_destroy(&dpif_names);
463     sset_destroy(&dpif_types);
464     if (error) {
465         exit(EXIT_FAILURE);
466     }
467 }
468
469 static void
470 do_dump_flows(int argc OVS_UNUSED, char *argv[])
471 {
472     const struct dpif_flow_stats *stats;
473     const struct nlattr *actions;
474     struct dpif_flow_dump dump;
475     const struct nlattr *key;
476     size_t actions_len;
477     struct dpif *dpif;
478     size_t key_len;
479     struct ds ds;
480
481     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
482
483     ds_init(&ds);
484     dpif_flow_dump_start(&dump, dpif);
485     while (dpif_flow_dump_next(&dump, &key, &key_len,
486                                &actions, &actions_len, &stats)) {
487         ds_clear(&ds);
488         odp_flow_key_format(key, key_len, &ds);
489         ds_put_cstr(&ds, ", ");
490         dpif_flow_stats_format(stats, &ds);
491         ds_put_cstr(&ds, ", actions:");
492         format_odp_actions(&ds, actions, actions_len);
493         printf("%s\n", ds_cstr(&ds));
494     }
495     dpif_flow_dump_done(&dump);
496     ds_destroy(&ds);
497     dpif_close(dpif);
498 }
499
500 static void
501 do_del_flows(int argc OVS_UNUSED, char *argv[])
502 {
503     struct dpif *dpif;
504
505     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
506     run(dpif_flow_flush(dpif), "deleting all flows");
507     dpif_close(dpif);
508 }
509
510 static void
511 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
512 {
513     usage();
514 }
515
516 static const struct command all_commands[] = {
517     { "add-dp", 1, INT_MAX, do_add_dp },
518     { "del-dp", 1, 1, do_del_dp },
519     { "add-if", 2, INT_MAX, do_add_if },
520     { "del-if", 2, INT_MAX, do_del_if },
521     { "dump-dps", 0, 0, do_dump_dps },
522     { "show", 0, INT_MAX, do_show },
523     { "dump-flows", 1, 1, do_dump_flows },
524     { "del-flows", 1, 1, do_del_flows },
525     { "help", 0, INT_MAX, do_help },
526     { NULL, 0, 0, NULL },
527 };