netdev: Clean up and refactor packet receive interface.
[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 /* -s, --statistics: Print port statistics? */
49 static bool print_statistics;
50
51 static const struct command all_commands[];
52
53 static void usage(void) NO_RETURN;
54 static void parse_options(int argc, char *argv[]);
55
56 int
57 main(int argc, char *argv[])
58 {
59     set_program_name(argv[0]);
60     parse_options(argc, argv);
61     signal(SIGPIPE, SIG_IGN);
62     run_command(argc - optind, argv + optind, all_commands);
63     return 0;
64 }
65
66 static void
67 parse_options(int argc, char *argv[])
68 {
69     enum {
70         OPT_DUMMY = UCHAR_MAX + 1,
71         VLOG_OPTION_ENUMS
72     };
73     static struct option long_options[] = {
74         {"statistics", no_argument, NULL, 's'},
75         {"timeout", required_argument, NULL, 't'},
76         {"help", no_argument, NULL, 'h'},
77         {"version", no_argument, NULL, 'V'},
78         VLOG_LONG_OPTIONS,
79         {NULL, 0, NULL, 0},
80     };
81     char *short_options = long_options_to_short_options(long_options);
82
83     for (;;) {
84         unsigned long int timeout;
85         int c;
86
87         c = getopt_long(argc, argv, short_options, long_options, NULL);
88         if (c == -1) {
89             break;
90         }
91
92         switch (c) {
93         case 's':
94             print_statistics = true;
95             break;
96
97         case 't':
98             timeout = strtoul(optarg, NULL, 10);
99             if (timeout <= 0) {
100                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
101                           optarg);
102             } else {
103                 time_alarm(timeout);
104             }
105             break;
106
107         case 'h':
108             usage();
109
110         case 'V':
111             ovs_print_version(0, 0);
112             exit(EXIT_SUCCESS);
113
114         VLOG_OPTION_HANDLERS
115
116         case '?':
117             exit(EXIT_FAILURE);
118
119         default:
120             abort();
121         }
122     }
123     free(short_options);
124 }
125
126 static void
127 usage(void)
128 {
129     printf("%s: Open vSwitch datapath management utility\n"
130            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
131            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
132            "  del-dp DP                delete local datapath DP\n"
133            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
134            "  del-if DP IFACE...       delete each IFACE from DP\n"
135            "  dump-dps                 display names of all datapaths\n"
136            "  show                     show basic info on all datapaths\n"
137            "  show DP...               show basic info on each DP\n"
138            "  dump-flows DP            display flows in DP\n"
139            "  del-flows DP             delete all flows from DP\n",
140            program_name, program_name);
141     vlog_usage();
142     printf("\nOther options:\n"
143            "  -t, --timeout=SECS          give up after SECS seconds\n"
144            "  -h, --help                  display this help message\n"
145            "  -V, --version               display version information\n");
146     exit(EXIT_SUCCESS);
147 }
148
149 static void run(int retval, const char *message, ...)
150     PRINTF_FORMAT(2, 3);
151
152 static void run(int retval, const char *message, ...)
153 {
154     if (retval) {
155         va_list args;
156
157         va_start(args, message);
158         ovs_fatal_valist(retval, message, args);
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 OVS_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 OVS_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 void
217 do_add_if(int argc OVS_UNUSED, char *argv[])
218 {
219     bool failure = false;
220     struct dpif *dpif;
221     int i;
222
223     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
224     for (i = 2; i < argc; i++) {
225         char *save_ptr = NULL;
226         struct netdev_options options;
227         struct netdev *netdev;
228         struct shash args;
229         char *option;
230         int error;
231
232         options.name = strtok_r(argv[i], ",", &save_ptr);
233         options.type = "system";
234         options.args = &args;
235
236         if (!options.name) {
237             ovs_error(0, "%s is not a valid network device name", argv[i]);
238             continue;
239         }
240
241         shash_init(&args);
242         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
243             char *save_ptr_2 = NULL;
244             char *key, *value;
245
246             key = strtok_r(option, "=", &save_ptr_2);
247             value = strtok_r(NULL, "", &save_ptr_2);
248             if (!value) {
249                 value = "";
250             }
251
252             if (!strcmp(key, "type")) {
253                 options.type = value;
254             } else if (!shash_add_once(&args, key, value)) {
255                 ovs_error(0, "duplicate \"%s\" option", key);
256             }
257         }
258
259         error = netdev_open(&options, &netdev);
260         if (error) {
261             ovs_error(error, "%s: failed to open network device",
262                       options.name);
263         } else {
264             error = dpif_port_add(dpif, netdev, NULL);
265             if (error) {
266                 ovs_error(error, "adding %s to %s failed",
267                           options.name, argv[1]);
268             } else {
269                 error = if_up(options.name);
270             }
271             netdev_close(netdev);
272         }
273         if (error) {
274             failure = true;
275         }
276     }
277     dpif_close(dpif);
278     if (failure) {
279         exit(EXIT_FAILURE);
280     }
281 }
282
283 static bool
284 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
285 {
286     struct dpif_port dpif_port;
287
288     if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
289         *port = dpif_port.port_no;
290         dpif_port_destroy(&dpif_port);
291         return true;
292     } else {
293         ovs_error(0, "no port named %s", name);
294         return false;
295     }
296 }
297
298 static void
299 do_del_if(int argc OVS_UNUSED, char *argv[])
300 {
301     bool failure = false;
302     struct dpif *dpif;
303     int i;
304
305     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
306     for (i = 2; i < argc; i++) {
307         const char *name = argv[i];
308         uint16_t port;
309         int error;
310
311         if (!name[strspn(name, "0123456789")]) {
312             port = atoi(name);
313         } else if (!get_port_number(dpif, name, &port)) {
314             failure = true;
315             continue;
316         }
317
318         error = dpif_port_del(dpif, port);
319         if (error) {
320             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
321             failure = true;
322         }
323     }
324     dpif_close(dpif);
325     if (failure) {
326         exit(EXIT_FAILURE);
327     }
328 }
329
330 static void
331 print_stat(const char *leader, uint64_t value)
332 {
333     fputs(leader, stdout);
334     if (value != UINT64_MAX) {
335         printf("%"PRIu64, value);
336     } else {
337         putchar('?');
338     }
339 }
340
341 static void
342 print_human_size(uint64_t value)
343 {
344     if (value == UINT64_MAX) {
345         /* Nothing to do. */
346     } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
347         printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
348     } else if (value >= 1024ULL * 1024 * 1024) {
349         printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
350     } else if (value >= 1024ULL * 1024) {
351         printf(" (%.1f MiB)", value / (1024.0 * 1024));
352     } else if (value >= 1024) {
353         printf(" (%.1f KiB)", value / 1024.0);
354     }
355 }
356
357 static void
358 show_dpif(struct dpif *dpif)
359 {
360     struct dpif_port_dump dump;
361     struct dpif_port dpif_port;
362     struct odp_stats stats;
363
364     printf("%s:\n", dpif_name(dpif));
365     if (!dpif_get_dp_stats(dpif, &stats)) {
366         printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
367                (unsigned long long int) stats.n_frags,
368                (unsigned long long int) stats.n_hit,
369                (unsigned long long int) stats.n_missed,
370                (unsigned long long int) stats.n_lost);
371         printf("\tflows: %llu\n", (unsigned long long int)stats.n_flows);
372     }
373     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
374         printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
375
376         if (strcmp(dpif_port.type, "system")) {
377             struct netdev_options netdev_options;
378             struct netdev *netdev;
379             int error;
380
381             printf (" (%s", dpif_port.type);
382
383             netdev_options.name = dpif_port.name;
384             netdev_options.type = dpif_port.type;
385             netdev_options.args = NULL;
386             error = netdev_open(&netdev_options, &netdev);
387             if (!error) {
388                 const struct shash_node **nodes;
389                 const struct shash *config;
390                 size_t i;
391
392                 config = netdev_get_config(netdev);
393                 nodes = shash_sort(config);
394                 for (i = 0; i < shash_count(config); i++) {
395                     const struct shash_node *node = nodes[i];
396                     printf("%c %s=%s", i ? ',' : ':',
397                            node->name, (char *) node->data);
398                 }
399                 free(nodes);
400
401                 netdev_close(netdev);
402             } else {
403                 printf(": open failed (%s)", strerror(error));
404             }
405             putchar(')');
406         }
407         putchar('\n');
408
409         if (print_statistics) {
410             const struct netdev_stats *s = &dpif_port.stats;
411
412             print_stat("\t\tRX packets:", s->rx_packets);
413             print_stat(" errors:", s->rx_errors);
414             print_stat(" dropped:", s->rx_dropped);
415             print_stat(" overruns:", s->rx_over_errors);
416             print_stat(" frame:", s->rx_frame_errors);
417             printf("\n");
418
419             print_stat("\t\tTX packets:", s->tx_packets);
420             print_stat(" errors:", s->tx_errors);
421             print_stat(" dropped:", s->tx_dropped);
422             print_stat(" aborted:", s->tx_aborted_errors);
423             print_stat(" carrier:", s->tx_carrier_errors);
424             printf("\n");
425
426             print_stat("\t\tcollisions:", s->collisions);
427             printf("\n");
428
429             print_stat("\t\tRX bytes:", s->rx_bytes);
430             print_human_size(s->rx_bytes);
431             print_stat("  TX bytes:", s->tx_bytes);
432             print_human_size(s->tx_bytes);
433             printf("\n");
434         }
435     }
436     dpif_close(dpif);
437 }
438
439 static void
440 do_show(int argc, char *argv[])
441 {
442     bool failure = false;
443     if (argc > 1) {
444         int i;
445         for (i = 1; i < argc; i++) {
446             const char *name = argv[i];
447             struct dpif *dpif;
448             int error;
449
450             error = parsed_dpif_open(name, false, &dpif);
451             if (!error) {
452                 show_dpif(dpif);
453             } else {
454                 ovs_error(error, "opening datapath %s failed", name);
455                 failure = true;
456             }
457         }
458     } else {
459         struct sset types;
460         const char *type;
461
462         sset_init(&types);
463         dp_enumerate_types(&types);
464         SSET_FOR_EACH (type, &types) {
465             struct sset names;
466             const char *name;
467
468             sset_init(&names);
469             if (dp_enumerate_names(type, &names)) {
470                 failure = true;
471                 continue;
472             }
473             SSET_FOR_EACH (name, &names) {
474                 struct dpif *dpif;
475                 int error;
476
477                 error = dpif_open(name, type, &dpif);
478                 if (!error) {
479                     show_dpif(dpif);
480                 } else {
481                     ovs_error(error, "opening datapath %s failed", name);
482                     failure = true;
483                 }
484             }
485             sset_destroy(&names);
486         }
487         sset_destroy(&types);
488     }
489     if (failure) {
490         exit(EXIT_FAILURE);
491     }
492 }
493
494 static void
495 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
496 {
497     struct sset dpif_names, dpif_types;
498     const char *type;
499     int error = 0;
500
501     sset_init(&dpif_names);
502     sset_init(&dpif_types);
503     dp_enumerate_types(&dpif_types);
504
505     SSET_FOR_EACH (type, &dpif_types) {
506         const char *name;
507         int retval;
508
509         retval = dp_enumerate_names(type, &dpif_names);
510         if (retval) {
511             error = retval;
512         }
513
514         SSET_FOR_EACH (name, &dpif_names) {
515             struct dpif *dpif;
516             if (!dpif_open(name, type, &dpif)) {
517                 printf("%s\n", dpif_name(dpif));
518                 dpif_close(dpif);
519             }
520         }
521     }
522
523     sset_destroy(&dpif_names);
524     sset_destroy(&dpif_types);
525     if (error) {
526         exit(EXIT_FAILURE);
527     }
528 }
529
530 static void
531 do_dump_flows(int argc OVS_UNUSED, char *argv[])
532 {
533     const struct dpif_flow_stats *stats;
534     const struct nlattr *actions;
535     struct dpif_flow_dump dump;
536     const struct nlattr *key;
537     size_t actions_len;
538     struct dpif *dpif;
539     size_t key_len;
540     struct ds ds;
541
542     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
543
544     ds_init(&ds);
545     dpif_flow_dump_start(&dump, dpif);
546     while (dpif_flow_dump_next(&dump, &key, &key_len,
547                                &actions, &actions_len, &stats)) {
548         ds_clear(&ds);
549         odp_flow_key_format(key, key_len, &ds);
550         ds_put_cstr(&ds, ", ");
551         dpif_flow_stats_format(stats, &ds);
552         ds_put_cstr(&ds, ", actions:");
553         format_odp_actions(&ds, actions, actions_len);
554         printf("%s\n", ds_cstr(&ds));
555     }
556     dpif_flow_dump_done(&dump);
557     ds_destroy(&ds);
558     dpif_close(dpif);
559 }
560
561 static void
562 do_del_flows(int argc OVS_UNUSED, char *argv[])
563 {
564     struct dpif *dpif;
565
566     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
567     run(dpif_flow_flush(dpif), "deleting all flows");
568     dpif_close(dpif);
569 }
570
571 static void
572 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
573 {
574     usage();
575 }
576
577 static const struct command all_commands[] = {
578     { "add-dp", 1, INT_MAX, do_add_dp },
579     { "del-dp", 1, 1, do_del_dp },
580     { "add-if", 2, INT_MAX, do_add_if },
581     { "del-if", 2, INT_MAX, do_del_if },
582     { "dump-dps", 0, 0, do_dump_dps },
583     { "show", 0, INT_MAX, do_show },
584     { "dump-flows", 1, 1, do_dump_flows },
585     { "del-flows", 1, 1, do_del_flows },
586     { "help", 0, INT_MAX, do_help },
587     { NULL, 0, 0, NULL },
588 };