ovs-dpctl: Add new "set-if" command.
[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            "  set-if DP IFACE...       reconfigure each IFACE within DP\n"
135            "  del-if DP IFACE...       delete each IFACE from DP\n"
136            "  dump-dps                 display names of all datapaths\n"
137            "  show                     show basic info on all datapaths\n"
138            "  show DP...               show basic info on each DP\n"
139            "  dump-flows DP            display flows in DP\n"
140            "  del-flows DP             delete all flows from DP\n"
141            "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
142            "comma-separated options.  See ovs-dpctl(8) for syntax, or the\n"
143            "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
144            program_name, program_name);
145     vlog_usage();
146     printf("\nOther options:\n"
147            "  -t, --timeout=SECS          give up after SECS seconds\n"
148            "  -h, --help                  display this help message\n"
149            "  -V, --version               display version information\n");
150     exit(EXIT_SUCCESS);
151 }
152
153 static void run(int retval, const char *message, ...)
154     PRINTF_FORMAT(2, 3);
155
156 static void run(int retval, const char *message, ...)
157 {
158     if (retval) {
159         va_list args;
160
161         va_start(args, message);
162         ovs_fatal_valist(retval, message, args);
163     }
164 }
165 \f
166 static void do_add_if(int argc, char *argv[]);
167
168 static int if_up(const char *netdev_name)
169 {
170     struct netdev *netdev;
171     int retval;
172
173     retval = netdev_open(netdev_name, "system", &netdev);
174     if (!retval) {
175         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
176         netdev_close(netdev);
177     }
178     return retval;
179 }
180
181 static int
182 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
183 {
184     int result;
185     char *name, *type;
186
187     dp_parse_name(arg_, &name, &type);
188
189     if (create) {
190         result = dpif_create(name, type, dpifp);
191     } else {
192         result = dpif_open(name, type, dpifp);
193     }
194
195     free(name);
196     free(type);
197     return result;
198 }
199
200 static void
201 do_add_dp(int argc OVS_UNUSED, char *argv[])
202 {
203     struct dpif *dpif;
204     run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
205     dpif_close(dpif);
206     if (argc > 2) {
207         do_add_if(argc, argv);
208     }
209 }
210
211 static void
212 do_del_dp(int argc OVS_UNUSED, char *argv[])
213 {
214     struct dpif *dpif;
215     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
216     run(dpif_delete(dpif), "del_dp");
217     dpif_close(dpif);
218 }
219
220 static void
221 do_add_if(int argc OVS_UNUSED, char *argv[])
222 {
223     bool failure = false;
224     struct dpif *dpif;
225     int i;
226
227     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
228     for (i = 2; i < argc; i++) {
229         const char *name, *type;
230         char *save_ptr = NULL;
231         struct netdev *netdev = NULL;
232         struct shash args;
233         char *option;
234         int error;
235
236         name = strtok_r(argv[i], ",", &save_ptr);
237         type = "system";
238
239         if (!name) {
240             ovs_error(0, "%s is not a valid network device name", argv[i]);
241             failure = true;
242             continue;
243         }
244
245         shash_init(&args);
246         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
247             char *save_ptr_2 = NULL;
248             char *key, *value;
249
250             key = strtok_r(option, "=", &save_ptr_2);
251             value = strtok_r(NULL, "", &save_ptr_2);
252             if (!value) {
253                 value = "";
254             }
255
256             if (!strcmp(key, "type")) {
257                 type = value;
258             } else if (!shash_add_once(&args, key, value)) {
259                 ovs_error(0, "duplicate \"%s\" option", key);
260             }
261         }
262
263         error = netdev_open(name, type, &netdev);
264         if (error) {
265             ovs_error(error, "%s: failed to open network device", name);
266             goto next;
267         }
268
269         error = netdev_set_config(netdev, &args);
270         if (error) {
271             ovs_error(error, "%s: failed to configure network device", name);
272             goto next;
273         }
274
275         error = dpif_port_add(dpif, netdev, NULL);
276         if (error) {
277             ovs_error(error, "adding %s to %s failed", name, argv[1]);
278             goto next;
279         }
280
281         error = if_up(name);
282
283 next:
284         netdev_close(netdev);
285         if (error) {
286             failure = true;
287         }
288     }
289     dpif_close(dpif);
290     if (failure) {
291         exit(EXIT_FAILURE);
292     }
293 }
294
295 static void
296 do_set_if(int argc, char *argv[])
297 {
298     bool failure = false;
299     struct dpif *dpif;
300     int i;
301
302     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
303     for (i = 2; i < argc; i++) {
304         struct netdev *netdev = NULL;
305         struct dpif_port dpif_port;
306         char *save_ptr = NULL;
307         char *type = NULL;
308         const char *name;
309         struct shash args;
310         char *option;
311         int error;
312
313         name = strtok_r(argv[i], ",", &save_ptr);
314         if (!name) {
315             ovs_error(0, "%s is not a valid network device name", argv[i]);
316             failure = true;
317             continue;
318         }
319
320         /* Get the port's type from the datapath. */
321         error = dpif_port_query_by_name(dpif, name, &dpif_port);
322         if (error) {
323             ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
324             goto next;
325         }
326         type = xstrdup(dpif_port.type);
327         dpif_port_destroy(&dpif_port);
328
329         /* Retrieve its existing configuration. */
330         error = netdev_open(name, type, &netdev);
331         if (error) {
332             ovs_error(error, "%s: failed to open network device", name);
333             goto next;
334         }
335
336         shash_init(&args);
337         error = netdev_get_config(netdev, &args);
338         if (error) {
339             ovs_error(error, "%s: failed to fetch configuration", name);
340             goto next;
341         }
342
343         /* Parse changes to configuration. */
344         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
345             char *save_ptr_2 = NULL;
346             char *key, *value;
347
348             key = strtok_r(option, "=", &save_ptr_2);
349             value = strtok_r(NULL, "", &save_ptr_2);
350             if (!value) {
351                 value = "";
352             }
353
354             if (!strcmp(key, "type")) {
355                 if (strcmp(value, type)) {
356                     ovs_error(0, "%s: can't change type from %s to %s",
357                               name, type, value);
358                     failure = true;
359                 }
360             } else if (value[0] == '\0') {
361                 free(shash_find_and_delete(&args, key));
362             } else {
363                 free(shash_replace(&args, key, xstrdup(value)));
364             }
365         }
366
367         /* Update configuration. */
368         error = netdev_set_config(netdev, &args);
369         smap_destroy(&args);
370         if (error) {
371             ovs_error(error, "%s: failed to configure network device", name);
372             goto next;
373         }
374
375 next:
376         free(type);
377         netdev_close(netdev);
378         if (error) {
379             failure = true;
380         }
381     }
382     dpif_close(dpif);
383     if (failure) {
384         exit(EXIT_FAILURE);
385     }
386 }
387
388 static bool
389 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
390 {
391     struct dpif_port dpif_port;
392
393     if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
394         *port = dpif_port.port_no;
395         dpif_port_destroy(&dpif_port);
396         return true;
397     } else {
398         ovs_error(0, "no port named %s", name);
399         return false;
400     }
401 }
402
403 static void
404 do_del_if(int argc OVS_UNUSED, char *argv[])
405 {
406     bool failure = false;
407     struct dpif *dpif;
408     int i;
409
410     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
411     for (i = 2; i < argc; i++) {
412         const char *name = argv[i];
413         uint16_t port;
414         int error;
415
416         if (!name[strspn(name, "0123456789")]) {
417             port = atoi(name);
418         } else if (!get_port_number(dpif, name, &port)) {
419             failure = true;
420             continue;
421         }
422
423         error = dpif_port_del(dpif, port);
424         if (error) {
425             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
426             failure = true;
427         }
428     }
429     dpif_close(dpif);
430     if (failure) {
431         exit(EXIT_FAILURE);
432     }
433 }
434
435 static void
436 print_stat(const char *leader, uint64_t value)
437 {
438     fputs(leader, stdout);
439     if (value != UINT64_MAX) {
440         printf("%"PRIu64, value);
441     } else {
442         putchar('?');
443     }
444 }
445
446 static void
447 print_human_size(uint64_t value)
448 {
449     if (value == UINT64_MAX) {
450         /* Nothing to do. */
451     } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
452         printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
453     } else if (value >= 1024ULL * 1024 * 1024) {
454         printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
455     } else if (value >= 1024ULL * 1024) {
456         printf(" (%.1f MiB)", value / (1024.0 * 1024));
457     } else if (value >= 1024) {
458         printf(" (%.1f KiB)", value / 1024.0);
459     }
460 }
461
462 static void
463 show_dpif(struct dpif *dpif)
464 {
465     struct dpif_port_dump dump;
466     struct dpif_port dpif_port;
467     struct dpif_dp_stats stats;
468     struct netdev *netdev;
469
470     printf("%s:\n", dpif_name(dpif));
471     if (!dpif_get_dp_stats(dpif, &stats)) {
472         printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
473                "\tflows: %"PRIu64"\n",
474                stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
475     }
476     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
477         printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
478
479         if (strcmp(dpif_port.type, "system")) {
480             int error;
481
482             printf (" (%s", dpif_port.type);
483
484             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
485             if (!error) {
486                 struct shash config;
487
488                 shash_init(&config);
489                 error = netdev_get_config(netdev, &config);
490                 if (!error) {
491                     const struct shash_node **nodes;
492                     size_t i;
493
494                     nodes = shash_sort(&config);
495                     for (i = 0; i < shash_count(&config); i++) {
496                         const struct shash_node *node = nodes[i];
497                         printf("%c %s=%s", i ? ',' : ':',
498                                node->name, (char *) node->data);
499                     }
500                     free(nodes);
501                 } else {
502                     printf(", could not retrieve configuration (%s)",
503                            strerror(error));
504                 }
505                 shash_destroy_free_data(&config);
506
507                 netdev_close(netdev);
508             } else {
509                 printf(": open failed (%s)", strerror(error));
510             }
511             putchar(')');
512         }
513         putchar('\n');
514
515         if (print_statistics) {
516             struct netdev_stats s;
517             int error;
518
519             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
520             if (error) {
521                 printf(", open failed (%s)", strerror(error));
522                 continue;
523             }
524             error = netdev_get_stats(netdev, &s);
525             if (error) {
526                 printf(", could not retrieve stats (%s)", strerror(error));
527                 continue;
528             }
529
530             netdev_close(netdev);
531             print_stat("\t\tRX packets:", s.rx_packets);
532             print_stat(" errors:", s.rx_errors);
533             print_stat(" dropped:", s.rx_dropped);
534             print_stat(" overruns:", s.rx_over_errors);
535             print_stat(" frame:", s.rx_frame_errors);
536             printf("\n");
537
538             print_stat("\t\tTX packets:", s.tx_packets);
539             print_stat(" errors:", s.tx_errors);
540             print_stat(" dropped:", s.tx_dropped);
541             print_stat(" aborted:", s.tx_aborted_errors);
542             print_stat(" carrier:", s.tx_carrier_errors);
543             printf("\n");
544
545             print_stat("\t\tcollisions:", s.collisions);
546             printf("\n");
547
548             print_stat("\t\tRX bytes:", s.rx_bytes);
549             print_human_size(s.rx_bytes);
550             print_stat("  TX bytes:", s.tx_bytes);
551             print_human_size(s.tx_bytes);
552             printf("\n");
553         }
554     }
555     dpif_close(dpif);
556 }
557
558 static void
559 do_show(int argc, char *argv[])
560 {
561     bool failure = false;
562     if (argc > 1) {
563         int i;
564         for (i = 1; i < argc; i++) {
565             const char *name = argv[i];
566             struct dpif *dpif;
567             int error;
568
569             error = parsed_dpif_open(name, false, &dpif);
570             if (!error) {
571                 show_dpif(dpif);
572             } else {
573                 ovs_error(error, "opening datapath %s failed", name);
574                 failure = true;
575             }
576         }
577     } else {
578         struct sset types;
579         const char *type;
580
581         sset_init(&types);
582         dp_enumerate_types(&types);
583         SSET_FOR_EACH (type, &types) {
584             struct sset names;
585             const char *name;
586
587             sset_init(&names);
588             if (dp_enumerate_names(type, &names)) {
589                 failure = true;
590                 continue;
591             }
592             SSET_FOR_EACH (name, &names) {
593                 struct dpif *dpif;
594                 int error;
595
596                 error = dpif_open(name, type, &dpif);
597                 if (!error) {
598                     show_dpif(dpif);
599                 } else {
600                     ovs_error(error, "opening datapath %s failed", name);
601                     failure = true;
602                 }
603             }
604             sset_destroy(&names);
605         }
606         sset_destroy(&types);
607     }
608     if (failure) {
609         exit(EXIT_FAILURE);
610     }
611 }
612
613 static void
614 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
615 {
616     struct sset dpif_names, dpif_types;
617     const char *type;
618     int error = 0;
619
620     sset_init(&dpif_names);
621     sset_init(&dpif_types);
622     dp_enumerate_types(&dpif_types);
623
624     SSET_FOR_EACH (type, &dpif_types) {
625         const char *name;
626         int retval;
627
628         retval = dp_enumerate_names(type, &dpif_names);
629         if (retval) {
630             error = retval;
631         }
632
633         SSET_FOR_EACH (name, &dpif_names) {
634             struct dpif *dpif;
635             if (!dpif_open(name, type, &dpif)) {
636                 printf("%s\n", dpif_name(dpif));
637                 dpif_close(dpif);
638             }
639         }
640     }
641
642     sset_destroy(&dpif_names);
643     sset_destroy(&dpif_types);
644     if (error) {
645         exit(EXIT_FAILURE);
646     }
647 }
648
649 static void
650 do_dump_flows(int argc OVS_UNUSED, char *argv[])
651 {
652     const struct dpif_flow_stats *stats;
653     const struct nlattr *actions;
654     struct dpif_flow_dump dump;
655     const struct nlattr *key;
656     size_t actions_len;
657     struct dpif *dpif;
658     size_t key_len;
659     struct ds ds;
660
661     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
662
663     ds_init(&ds);
664     dpif_flow_dump_start(&dump, dpif);
665     while (dpif_flow_dump_next(&dump, &key, &key_len,
666                                &actions, &actions_len, &stats)) {
667         ds_clear(&ds);
668         odp_flow_key_format(key, key_len, &ds);
669         ds_put_cstr(&ds, ", ");
670         dpif_flow_stats_format(stats, &ds);
671         ds_put_cstr(&ds, ", actions:");
672         format_odp_actions(&ds, actions, actions_len);
673         printf("%s\n", ds_cstr(&ds));
674     }
675     dpif_flow_dump_done(&dump);
676     ds_destroy(&ds);
677     dpif_close(dpif);
678 }
679
680 static void
681 do_del_flows(int argc OVS_UNUSED, char *argv[])
682 {
683     struct dpif *dpif;
684
685     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
686     run(dpif_flow_flush(dpif), "deleting all flows");
687     dpif_close(dpif);
688 }
689
690 static void
691 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
692 {
693     usage();
694 }
695
696 static const struct command all_commands[] = {
697     { "add-dp", 1, INT_MAX, do_add_dp },
698     { "del-dp", 1, 1, do_del_dp },
699     { "add-if", 2, INT_MAX, do_add_if },
700     { "del-if", 2, INT_MAX, do_del_if },
701     { "set-if", 2, INT_MAX, do_set_if },
702     { "dump-dps", 0, 0, do_dump_dps },
703     { "show", 0, INT_MAX, do_show },
704     { "dump-flows", 1, 1, do_dump_flows },
705     { "del-flows", 1, 1, do_del_flows },
706     { "help", 0, INT_MAX, do_help },
707     { NULL, 0, 0, NULL },
708 };