simap: New data structure for string-to-integer maps.
[sliver-openvswitch.git] / utilities / ovs-dpctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 <assert.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <inttypes.h>
23 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33
34 #include "command-line.h"
35 #include "compiler.h"
36 #include "dirs.h"
37 #include "dpif.h"
38 #include "dynamic-string.h"
39 #include "flow.h"
40 #include "netdev.h"
41 #include "netlink.h"
42 #include "odp-util.h"
43 #include "ofpbuf.h"
44 #include "packets.h"
45 #include "shash.h"
46 #include "simap.h"
47 #include "sset.h"
48 #include "timeval.h"
49 #include "util.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(dpctl);
53
54 /* -s, --statistics: Print port statistics? */
55 static bool print_statistics;
56
57 /* -m, --more: Output verbosity.
58  *
59  * So far only undocumented commands honor this option, so we don't document
60  * the option itself. */
61 static int verbosity;
62
63 static const struct command all_commands[];
64
65 static void usage(void) NO_RETURN;
66 static void parse_options(int argc, char *argv[]);
67
68 int
69 main(int argc, char *argv[])
70 {
71     set_program_name(argv[0]);
72     parse_options(argc, argv);
73     signal(SIGPIPE, SIG_IGN);
74     run_command(argc - optind, argv + optind, all_commands);
75     return 0;
76 }
77
78 static void
79 parse_options(int argc, char *argv[])
80 {
81     enum {
82         OPT_DUMMY = UCHAR_MAX + 1,
83         VLOG_OPTION_ENUMS
84     };
85     static struct option long_options[] = {
86         {"statistics", no_argument, NULL, 's'},
87         {"more", no_argument, NULL, 'm'},
88         {"timeout", required_argument, NULL, 't'},
89         {"help", no_argument, NULL, 'h'},
90         {"version", no_argument, NULL, 'V'},
91         VLOG_LONG_OPTIONS,
92         {NULL, 0, NULL, 0},
93     };
94     char *short_options = long_options_to_short_options(long_options);
95
96     for (;;) {
97         unsigned long int timeout;
98         int c;
99
100         c = getopt_long(argc, argv, short_options, long_options, NULL);
101         if (c == -1) {
102             break;
103         }
104
105         switch (c) {
106         case 's':
107             print_statistics = true;
108             break;
109
110         case 'm':
111             verbosity++;
112             break;
113
114         case 't':
115             timeout = strtoul(optarg, NULL, 10);
116             if (timeout <= 0) {
117                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
118                           optarg);
119             } else {
120                 time_alarm(timeout);
121             }
122             break;
123
124         case 'h':
125             usage();
126
127         case 'V':
128             ovs_print_version(0, 0);
129             exit(EXIT_SUCCESS);
130
131         VLOG_OPTION_HANDLERS
132
133         case '?':
134             exit(EXIT_FAILURE);
135
136         default:
137             abort();
138         }
139     }
140     free(short_options);
141 }
142
143 static void
144 usage(void)
145 {
146     printf("%s: Open vSwitch datapath management utility\n"
147            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
148            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
149            "  del-dp DP                delete local datapath DP\n"
150            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
151            "  set-if DP IFACE...       reconfigure each IFACE within DP\n"
152            "  del-if DP IFACE...       delete each IFACE from DP\n"
153            "  dump-dps                 display names of all datapaths\n"
154            "  show                     show basic info on all datapaths\n"
155            "  show DP...               show basic info on each DP\n"
156            "  dump-flows DP            display flows in DP\n"
157            "  del-flows DP             delete all flows from DP\n"
158            "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
159            "comma-separated options.  See ovs-dpctl(8) for syntax, or the\n"
160            "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n",
161            program_name, program_name);
162     vlog_usage();
163     printf("\nOther options:\n"
164            "  -t, --timeout=SECS          give up after SECS seconds\n"
165            "  -h, --help                  display this help message\n"
166            "  -V, --version               display version information\n");
167     exit(EXIT_SUCCESS);
168 }
169
170 static void run(int retval, const char *message, ...)
171     PRINTF_FORMAT(2, 3);
172
173 static void run(int retval, const char *message, ...)
174 {
175     if (retval) {
176         va_list args;
177
178         va_start(args, message);
179         ovs_fatal_valist(retval, message, args);
180     }
181 }
182 \f
183 static void do_add_if(int argc, char *argv[]);
184
185 static int if_up(const char *netdev_name)
186 {
187     struct netdev *netdev;
188     int retval;
189
190     retval = netdev_open(netdev_name, "system", &netdev);
191     if (!retval) {
192         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
193         netdev_close(netdev);
194     }
195     return retval;
196 }
197
198 static int
199 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
200 {
201     int result;
202     char *name, *type;
203
204     dp_parse_name(arg_, &name, &type);
205
206     if (create) {
207         result = dpif_create(name, type, dpifp);
208     } else {
209         result = dpif_open(name, type, dpifp);
210     }
211
212     free(name);
213     free(type);
214     return result;
215 }
216
217 static void
218 do_add_dp(int argc OVS_UNUSED, char *argv[])
219 {
220     struct dpif *dpif;
221     run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
222     dpif_close(dpif);
223     if (argc > 2) {
224         do_add_if(argc, argv);
225     }
226 }
227
228 static void
229 do_del_dp(int argc OVS_UNUSED, char *argv[])
230 {
231     struct dpif *dpif;
232     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
233     run(dpif_delete(dpif), "del_dp");
234     dpif_close(dpif);
235 }
236
237 static void
238 do_add_if(int argc OVS_UNUSED, char *argv[])
239 {
240     bool failure = false;
241     struct dpif *dpif;
242     int i;
243
244     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
245     for (i = 2; i < argc; i++) {
246         const char *name, *type;
247         char *save_ptr = NULL;
248         struct netdev *netdev = NULL;
249         struct shash args;
250         char *option;
251         int error;
252
253         name = strtok_r(argv[i], ",", &save_ptr);
254         type = "system";
255
256         if (!name) {
257             ovs_error(0, "%s is not a valid network device name", argv[i]);
258             failure = true;
259             continue;
260         }
261
262         shash_init(&args);
263         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
264             char *save_ptr_2 = NULL;
265             char *key, *value;
266
267             key = strtok_r(option, "=", &save_ptr_2);
268             value = strtok_r(NULL, "", &save_ptr_2);
269             if (!value) {
270                 value = "";
271             }
272
273             if (!strcmp(key, "type")) {
274                 type = value;
275             } else if (!shash_add_once(&args, key, value)) {
276                 ovs_error(0, "duplicate \"%s\" option", key);
277             }
278         }
279
280         error = netdev_open(name, type, &netdev);
281         if (error) {
282             ovs_error(error, "%s: failed to open network device", name);
283             goto next;
284         }
285
286         error = netdev_set_config(netdev, &args);
287         if (error) {
288             ovs_error(error, "%s: failed to configure network device", name);
289             goto next;
290         }
291
292         error = dpif_port_add(dpif, netdev, NULL);
293         if (error) {
294             ovs_error(error, "adding %s to %s failed", name, argv[1]);
295             goto next;
296         }
297
298         error = if_up(name);
299
300 next:
301         netdev_close(netdev);
302         if (error) {
303             failure = true;
304         }
305     }
306     dpif_close(dpif);
307     if (failure) {
308         exit(EXIT_FAILURE);
309     }
310 }
311
312 static void
313 do_set_if(int argc, 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         struct netdev *netdev = NULL;
322         struct dpif_port dpif_port;
323         char *save_ptr = NULL;
324         char *type = NULL;
325         const char *name;
326         struct shash args;
327         char *option;
328         int error;
329
330         name = strtok_r(argv[i], ",", &save_ptr);
331         if (!name) {
332             ovs_error(0, "%s is not a valid network device name", argv[i]);
333             failure = true;
334             continue;
335         }
336
337         /* Get the port's type from the datapath. */
338         error = dpif_port_query_by_name(dpif, name, &dpif_port);
339         if (error) {
340             ovs_error(error, "%s: failed to query port in %s", name, argv[1]);
341             goto next;
342         }
343         type = xstrdup(dpif_port.type);
344         dpif_port_destroy(&dpif_port);
345
346         /* Retrieve its existing configuration. */
347         error = netdev_open(name, type, &netdev);
348         if (error) {
349             ovs_error(error, "%s: failed to open network device", name);
350             goto next;
351         }
352
353         shash_init(&args);
354         error = netdev_get_config(netdev, &args);
355         if (error) {
356             ovs_error(error, "%s: failed to fetch configuration", name);
357             goto next;
358         }
359
360         /* Parse changes to configuration. */
361         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
362             char *save_ptr_2 = NULL;
363             char *key, *value;
364
365             key = strtok_r(option, "=", &save_ptr_2);
366             value = strtok_r(NULL, "", &save_ptr_2);
367             if (!value) {
368                 value = "";
369             }
370
371             if (!strcmp(key, "type")) {
372                 if (strcmp(value, type)) {
373                     ovs_error(0, "%s: can't change type from %s to %s",
374                               name, type, value);
375                     failure = true;
376                 }
377             } else if (value[0] == '\0') {
378                 free(shash_find_and_delete(&args, key));
379             } else {
380                 free(shash_replace(&args, key, xstrdup(value)));
381             }
382         }
383
384         /* Update configuration. */
385         error = netdev_set_config(netdev, &args);
386         smap_destroy(&args);
387         if (error) {
388             ovs_error(error, "%s: failed to configure network device", name);
389             goto next;
390         }
391
392 next:
393         free(type);
394         netdev_close(netdev);
395         if (error) {
396             failure = true;
397         }
398     }
399     dpif_close(dpif);
400     if (failure) {
401         exit(EXIT_FAILURE);
402     }
403 }
404
405 static bool
406 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
407 {
408     struct dpif_port dpif_port;
409
410     if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
411         *port = dpif_port.port_no;
412         dpif_port_destroy(&dpif_port);
413         return true;
414     } else {
415         ovs_error(0, "no port named %s", name);
416         return false;
417     }
418 }
419
420 static void
421 do_del_if(int argc OVS_UNUSED, char *argv[])
422 {
423     bool failure = false;
424     struct dpif *dpif;
425     int i;
426
427     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
428     for (i = 2; i < argc; i++) {
429         const char *name = argv[i];
430         uint16_t port;
431         int error;
432
433         if (!name[strspn(name, "0123456789")]) {
434             port = atoi(name);
435         } else if (!get_port_number(dpif, name, &port)) {
436             failure = true;
437             continue;
438         }
439
440         error = dpif_port_del(dpif, port);
441         if (error) {
442             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
443             failure = true;
444         }
445     }
446     dpif_close(dpif);
447     if (failure) {
448         exit(EXIT_FAILURE);
449     }
450 }
451
452 static void
453 print_stat(const char *leader, uint64_t value)
454 {
455     fputs(leader, stdout);
456     if (value != UINT64_MAX) {
457         printf("%"PRIu64, value);
458     } else {
459         putchar('?');
460     }
461 }
462
463 static void
464 print_human_size(uint64_t value)
465 {
466     if (value == UINT64_MAX) {
467         /* Nothing to do. */
468     } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
469         printf(" (%.1f TiB)", value / (1024.0 * 1024 * 1024 * 1024));
470     } else if (value >= 1024ULL * 1024 * 1024) {
471         printf(" (%.1f GiB)", value / (1024.0 * 1024 * 1024));
472     } else if (value >= 1024ULL * 1024) {
473         printf(" (%.1f MiB)", value / (1024.0 * 1024));
474     } else if (value >= 1024) {
475         printf(" (%.1f KiB)", value / 1024.0);
476     }
477 }
478
479 static void
480 show_dpif(struct dpif *dpif)
481 {
482     struct dpif_port_dump dump;
483     struct dpif_port dpif_port;
484     struct dpif_dp_stats stats;
485     struct netdev *netdev;
486
487     printf("%s:\n", dpif_name(dpif));
488     if (!dpif_get_dp_stats(dpif, &stats)) {
489         printf("\tlookups: hit:%"PRIu64" missed:%"PRIu64" lost:%"PRIu64"\n"
490                "\tflows: %"PRIu64"\n",
491                stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
492     }
493     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
494         printf("\tport %u: %s", dpif_port.port_no, dpif_port.name);
495
496         if (strcmp(dpif_port.type, "system")) {
497             int error;
498
499             printf (" (%s", dpif_port.type);
500
501             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
502             if (!error) {
503                 struct shash config;
504
505                 shash_init(&config);
506                 error = netdev_get_config(netdev, &config);
507                 if (!error) {
508                     const struct shash_node **nodes;
509                     size_t i;
510
511                     nodes = shash_sort(&config);
512                     for (i = 0; i < shash_count(&config); i++) {
513                         const struct shash_node *node = nodes[i];
514                         printf("%c %s=%s", i ? ',' : ':',
515                                node->name, (char *) node->data);
516                     }
517                     free(nodes);
518                 } else {
519                     printf(", could not retrieve configuration (%s)",
520                            strerror(error));
521                 }
522                 shash_destroy_free_data(&config);
523
524                 netdev_close(netdev);
525             } else {
526                 printf(": open failed (%s)", strerror(error));
527             }
528             putchar(')');
529         }
530         putchar('\n');
531
532         if (print_statistics) {
533             struct netdev_stats s;
534             int error;
535
536             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
537             if (error) {
538                 printf(", open failed (%s)", strerror(error));
539                 continue;
540             }
541             error = netdev_get_stats(netdev, &s);
542             if (error) {
543                 printf(", could not retrieve stats (%s)", strerror(error));
544                 continue;
545             }
546
547             netdev_close(netdev);
548             print_stat("\t\tRX packets:", s.rx_packets);
549             print_stat(" errors:", s.rx_errors);
550             print_stat(" dropped:", s.rx_dropped);
551             print_stat(" overruns:", s.rx_over_errors);
552             print_stat(" frame:", s.rx_frame_errors);
553             printf("\n");
554
555             print_stat("\t\tTX packets:", s.tx_packets);
556             print_stat(" errors:", s.tx_errors);
557             print_stat(" dropped:", s.tx_dropped);
558             print_stat(" aborted:", s.tx_aborted_errors);
559             print_stat(" carrier:", s.tx_carrier_errors);
560             printf("\n");
561
562             print_stat("\t\tcollisions:", s.collisions);
563             printf("\n");
564
565             print_stat("\t\tRX bytes:", s.rx_bytes);
566             print_human_size(s.rx_bytes);
567             print_stat("  TX bytes:", s.tx_bytes);
568             print_human_size(s.tx_bytes);
569             printf("\n");
570         }
571     }
572     dpif_close(dpif);
573 }
574
575 static void
576 do_show(int argc, char *argv[])
577 {
578     bool failure = false;
579     if (argc > 1) {
580         int i;
581         for (i = 1; i < argc; i++) {
582             const char *name = argv[i];
583             struct dpif *dpif;
584             int error;
585
586             error = parsed_dpif_open(name, false, &dpif);
587             if (!error) {
588                 show_dpif(dpif);
589             } else {
590                 ovs_error(error, "opening datapath %s failed", name);
591                 failure = true;
592             }
593         }
594     } else {
595         struct sset types;
596         const char *type;
597
598         sset_init(&types);
599         dp_enumerate_types(&types);
600         SSET_FOR_EACH (type, &types) {
601             struct sset names;
602             const char *name;
603
604             sset_init(&names);
605             if (dp_enumerate_names(type, &names)) {
606                 failure = true;
607                 continue;
608             }
609             SSET_FOR_EACH (name, &names) {
610                 struct dpif *dpif;
611                 int error;
612
613                 error = dpif_open(name, type, &dpif);
614                 if (!error) {
615                     show_dpif(dpif);
616                 } else {
617                     ovs_error(error, "opening datapath %s failed", name);
618                     failure = true;
619                 }
620             }
621             sset_destroy(&names);
622         }
623         sset_destroy(&types);
624     }
625     if (failure) {
626         exit(EXIT_FAILURE);
627     }
628 }
629
630 static void
631 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
632 {
633     struct sset dpif_names, dpif_types;
634     const char *type;
635     int error = 0;
636
637     sset_init(&dpif_names);
638     sset_init(&dpif_types);
639     dp_enumerate_types(&dpif_types);
640
641     SSET_FOR_EACH (type, &dpif_types) {
642         const char *name;
643         int retval;
644
645         retval = dp_enumerate_names(type, &dpif_names);
646         if (retval) {
647             error = retval;
648         }
649
650         SSET_FOR_EACH (name, &dpif_names) {
651             struct dpif *dpif;
652             if (!dpif_open(name, type, &dpif)) {
653                 printf("%s\n", dpif_name(dpif));
654                 dpif_close(dpif);
655             }
656         }
657     }
658
659     sset_destroy(&dpif_names);
660     sset_destroy(&dpif_types);
661     if (error) {
662         exit(EXIT_FAILURE);
663     }
664 }
665
666 static void
667 do_dump_flows(int argc OVS_UNUSED, char *argv[])
668 {
669     const struct dpif_flow_stats *stats;
670     const struct nlattr *actions;
671     struct dpif_flow_dump dump;
672     const struct nlattr *key;
673     size_t actions_len;
674     struct dpif *dpif;
675     size_t key_len;
676     struct ds ds;
677
678     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
679
680     ds_init(&ds);
681     dpif_flow_dump_start(&dump, dpif);
682     while (dpif_flow_dump_next(&dump, &key, &key_len,
683                                &actions, &actions_len, &stats)) {
684         ds_clear(&ds);
685         odp_flow_key_format(key, key_len, &ds);
686         ds_put_cstr(&ds, ", ");
687         dpif_flow_stats_format(stats, &ds);
688         ds_put_cstr(&ds, ", actions:");
689         format_odp_actions(&ds, actions, actions_len);
690         printf("%s\n", ds_cstr(&ds));
691     }
692     dpif_flow_dump_done(&dump);
693     ds_destroy(&ds);
694     dpif_close(dpif);
695 }
696
697 static void
698 do_del_flows(int argc OVS_UNUSED, char *argv[])
699 {
700     struct dpif *dpif;
701
702     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
703     run(dpif_flow_flush(dpif), "deleting all flows");
704     dpif_close(dpif);
705 }
706
707 static void
708 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
709 {
710     usage();
711 }
712 \f
713 /* Undocumented commands for unit testing. */
714
715 static void
716 do_parse_actions(int argc, char *argv[])
717 {
718     int i;
719
720     for (i = 1; i < argc; i++) {
721         struct ofpbuf actions;
722         struct ds s;
723
724         ofpbuf_init(&actions, 0);
725         run(odp_actions_from_string(argv[i], NULL, &actions),
726             "odp_actions_from_string");
727
728         ds_init(&s);
729         format_odp_actions(&s, actions.data, actions.size);
730         puts(ds_cstr(&s));
731         ds_destroy(&s);
732
733         ofpbuf_uninit(&actions);
734     }
735 }
736
737 struct actions_for_flow {
738     struct hmap_node hmap_node;
739     struct flow flow;
740     struct ofpbuf actions;
741 };
742
743 static struct actions_for_flow *
744 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
745 {
746     uint32_t hash = flow_hash(flow, 0);
747     struct actions_for_flow *af;
748
749     HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
750         if (flow_equal(&af->flow, flow)) {
751             return af;
752         }
753     }
754
755     af = xmalloc(sizeof *af);
756     af->flow = *flow;
757     ofpbuf_init(&af->actions, 0);
758     hmap_insert(actions_per_flow, &af->hmap_node, hash);
759     return af;
760 }
761
762 static int
763 compare_actions_for_flow(const void *a_, const void *b_)
764 {
765     struct actions_for_flow *const *a = a_;
766     struct actions_for_flow *const *b = b_;
767
768     return flow_compare_3way(&(*a)->flow, &(*b)->flow);
769 }
770
771 static int
772 compare_output_actions(const void *a_, const void *b_)
773 {
774     const struct nlattr *a = a_;
775     const struct nlattr *b = b_;
776     uint32_t a_port = nl_attr_get_u32(a);
777     uint32_t b_port = nl_attr_get_u32(b);
778
779     return a_port < b_port ? -1 : a_port > b_port;
780 }
781
782 static void
783 sort_output_actions__(struct nlattr *first, struct nlattr *end)
784 {
785     size_t bytes = (uint8_t *) end - (uint8_t *) first;
786     size_t n = bytes / NL_A_U32_SIZE;
787
788     assert(bytes % NL_A_U32_SIZE == 0);
789     qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
790 }
791
792 static void
793 sort_output_actions(struct nlattr *actions, size_t length)
794 {
795     struct nlattr *first_output = NULL;
796     struct nlattr *a;
797     int left;
798
799     NL_ATTR_FOR_EACH (a, left, actions, length) {
800         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
801             if (!first_output) {
802                 first_output = a;
803             }
804         } else {
805             if (first_output) {
806                 sort_output_actions__(first_output, a);
807                 first_output = NULL;
808             }
809         }
810     }
811     if (first_output) {
812         uint8_t *end = (uint8_t *) actions + length;
813         sort_output_actions__(first_output, (struct nlattr *) end);
814     }
815 }
816
817 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
818  * have the syntax used by "ovs-dpctl dump-flows".
819  *
820  * This command prints ACTIONS in a format that shows what happens for each
821  * VLAN, independent of the order of the ACTIONS.  For example, there is more
822  * than one way to output a packet on VLANs 9 and 11, but this command will
823  * print the same output for any form.
824  *
825  * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
826  * so far the implementation only covers VLANs. */
827 static void
828 do_normalize_actions(int argc, char *argv[])
829 {
830     struct simap port_names;
831     struct ofpbuf keybuf;
832     struct flow flow;
833     struct ofpbuf odp_actions;
834     struct hmap actions_per_flow;
835     struct actions_for_flow **afs;
836     struct actions_for_flow *af;
837     struct nlattr *a;
838     size_t n_afs;
839     struct ds s;
840     int left;
841     int i;
842
843     ds_init(&s);
844
845     simap_init(&port_names);
846     for (i = 3; i < argc; i++) {
847         char name[16];
848         int number;
849         int n = -1;
850
851         if (sscanf(argv[i], "%15[^=]=%d%n", name, &number, &n) > 0 && n > 0) {
852             uintptr_t n = number;
853             simap_put(&port_names, name, n);
854         } else {
855             ovs_fatal(0, "%s: expected NAME=NUMBER", argv[i]);
856         }
857     }
858
859     /* Parse flow key. */
860     ofpbuf_init(&keybuf, 0);
861     run(odp_flow_key_from_string(argv[1], &port_names, &keybuf),
862         "odp_flow_key_from_string");
863
864     ds_clear(&s);
865     odp_flow_key_format(keybuf.data, keybuf.size, &s);
866     printf("input flow: %s\n", ds_cstr(&s));
867
868     run(odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow),
869         "odp_flow_key_to_flow");
870     ofpbuf_uninit(&keybuf);
871
872     /* Parse actions. */
873     ofpbuf_init(&odp_actions, 0);
874     run(odp_actions_from_string(argv[2], &port_names, &odp_actions),
875         "odp_actions_from_string");
876
877     if (verbosity) {
878         ds_clear(&s);
879         format_odp_actions(&s, odp_actions.data, odp_actions.size);
880         printf("input actions: %s\n", ds_cstr(&s));
881     }
882
883     hmap_init(&actions_per_flow);
884     NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
885         if (nl_attr_type(a) == OVS_ACTION_ATTR_POP_VLAN) {
886             flow.vlan_tci = htons(0);
887             continue;
888         }
889
890         if (nl_attr_type(a) == OVS_ACTION_ATTR_PUSH_VLAN) {
891             const struct ovs_action_push_vlan *push;
892
893             push = nl_attr_get_unspec(a, sizeof *push);
894             flow.vlan_tci = push->vlan_tci;
895             continue;
896         }
897
898         af = get_actions_for_flow(&actions_per_flow, &flow);
899         nl_msg_put_unspec(&af->actions, nl_attr_type(a),
900                           nl_attr_get(a), nl_attr_get_size(a));
901     }
902
903     n_afs = hmap_count(&actions_per_flow);
904     afs = xmalloc(n_afs * sizeof *afs);
905     i = 0;
906     HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
907         afs[i++] = af;
908     }
909     assert(i == n_afs);
910
911     qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
912
913     for (i = 0; i < n_afs; i++) {
914         const struct actions_for_flow *af = afs[i];
915
916         sort_output_actions(af->actions.data, af->actions.size);
917
918         if (af->flow.vlan_tci != htons(0)) {
919             printf("vlan(vid=%"PRIu16",pcp=%d): ",
920                    vlan_tci_to_vid(af->flow.vlan_tci),
921                    vlan_tci_to_pcp(af->flow.vlan_tci));
922         } else {
923             printf("no vlan: ");
924         }
925
926         ds_clear(&s);
927         format_odp_actions(&s, af->actions.data, af->actions.size);
928         puts(ds_cstr(&s));
929     }
930     ds_destroy(&s);
931 }
932
933 static const struct command all_commands[] = {
934     { "add-dp", 1, INT_MAX, do_add_dp },
935     { "del-dp", 1, 1, do_del_dp },
936     { "add-if", 2, INT_MAX, do_add_if },
937     { "del-if", 2, INT_MAX, do_del_if },
938     { "set-if", 2, INT_MAX, do_set_if },
939     { "dump-dps", 0, 0, do_dump_dps },
940     { "show", 0, INT_MAX, do_show },
941     { "dump-flows", 1, 1, do_dump_flows },
942     { "del-flows", 1, 1, do_del_flows },
943     { "help", 0, INT_MAX, do_help },
944
945     /* Undocumented commands for testing. */
946     { "parse-actions", 1, INT_MAX, do_parse_actions },
947     { "normalize-actions", 2, INT_MAX, do_normalize_actions },
948
949     { NULL, 0, 0, NULL },
950 };