0f21a51306873b688c904ca3dfff971de9b4e8f1
[cascardo/ovs.git] / lib / dpctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 <errno.h>
20 #include <inttypes.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "command-line.h"
30 #include "compiler.h"
31 #include "dirs.h"
32 #include "dpctl.h"
33 #include "dpif.h"
34 #include "dynamic-string.h"
35 #include "flow.h"
36 #include "match.h"
37 #include "netdev.h"
38 #include "netdev-dpdk.h"
39 #include "netlink.h"
40 #include "odp-util.h"
41 #include "ofp-parse.h"
42 #include "ofpbuf.h"
43 #include "ovs-numa.h"
44 #include "packets.h"
45 #include "shash.h"
46 #include "simap.h"
47 #include "smap.h"
48 #include "sset.h"
49 #include "timeval.h"
50 #include "unixctl.h"
51 #include "util.h"
52
53 typedef int dpctl_command_handler(int argc, const char *argv[],
54                                   struct dpctl_params *);
55 struct dpctl_command {
56     const char *name;
57     const char *usage;
58     int min_args;
59     int max_args;
60     dpctl_command_handler *handler;
61 };
62 static const struct dpctl_command *get_all_dpctl_commands(void);
63 static void dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...)
64     OVS_PRINTF_FORMAT(2, 3);
65 static void dpctl_error(struct dpctl_params* dpctl_p, int err_no,
66                         const char *fmt, ...)
67     OVS_PRINTF_FORMAT(3, 4);
68
69 static void
70 dpctl_puts(struct dpctl_params *dpctl_p, bool error, const char *string)
71 {
72     dpctl_p->output(dpctl_p->aux, error, string);
73 }
74
75 static void
76 dpctl_print(struct dpctl_params *dpctl_p, const char *fmt, ...)
77 {
78     char *string;
79     va_list args;
80
81     va_start(args, fmt);
82     string = xvasprintf(fmt, args);
83     va_end(args);
84
85     dpctl_puts(dpctl_p, false, string);
86     free(string);
87 }
88
89 static void
90 dpctl_error(struct dpctl_params* dpctl_p, int err_no, const char *fmt, ...)
91 {
92     const char *subprogram_name = get_subprogram_name();
93     struct ds ds = DS_EMPTY_INITIALIZER;
94     int save_errno = errno;
95     va_list args;
96
97
98     if (subprogram_name[0]) {
99         ds_put_format(&ds, "%s(%s): ", program_name,subprogram_name);
100     } else {
101         ds_put_format(&ds, "%s: ", program_name);
102     }
103
104     va_start(args, fmt);
105     ds_put_format_valist(&ds, fmt, args);
106     va_end(args);
107
108     if (err_no != 0) {
109         ds_put_format(&ds, " (%s)", ovs_retval_to_string(err_no));
110     }
111     ds_put_cstr(&ds, "\n");
112
113     dpctl_puts(dpctl_p, true, ds_cstr(&ds));
114
115     ds_destroy(&ds);
116
117     errno = save_errno;
118 }
119 \f
120 static int dpctl_add_if(int argc, const char *argv[], struct dpctl_params *);
121
122 static int
123 if_up(struct netdev *netdev)
124 {
125     return netdev_turn_flags_on(netdev, NETDEV_UP, NULL);
126 }
127
128 /* Retrieve the name of the datapath if exactly one exists.  The caller
129  * is responsible for freeing the returned string.  If there is not one
130  * datapath, aborts with an error message. */
131 static char *
132 get_one_dp(struct dpctl_params *dpctl_p)
133 {
134     struct sset types;
135     const char *type;
136     char *dp_name = NULL;
137     size_t count = 0;
138
139     sset_init(&types);
140     dp_enumerate_types(&types);
141     SSET_FOR_EACH (type, &types) {
142         struct sset names;
143
144         sset_init(&names);
145         if (!dp_enumerate_names(type, &names)) {
146             count += sset_count(&names);
147             if (!dp_name && count == 1) {
148                 dp_name = xasprintf("%s@%s", type, SSET_FIRST(&names));
149             }
150         }
151         sset_destroy(&names);
152     }
153     sset_destroy(&types);
154
155     if (!count) {
156         dpctl_error(dpctl_p, 0, "no datapaths exist");
157     } else if (count > 1) {
158         dpctl_error(dpctl_p, 0, "multiple datapaths, specify one");
159         free(dp_name);
160         dp_name = NULL;
161     }
162
163     return dp_name;
164 }
165
166 static int
167 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
168 {
169     int result;
170     char *name, *type;
171
172     dp_parse_name(arg_, &name, &type);
173
174     if (create) {
175         result = dpif_create(name, type, dpifp);
176     } else {
177         result = dpif_open(name, type, dpifp);
178     }
179
180     free(name);
181     free(type);
182     return result;
183 }
184
185 static int
186 dpctl_add_dp(int argc, const char *argv[],
187              struct dpctl_params *dpctl_p)
188 {
189     struct dpif *dpif;
190     int error;
191
192     error = parsed_dpif_open(argv[1], true, &dpif);
193     if (error) {
194         dpctl_error(dpctl_p, error, "add_dp");
195         return error;
196     }
197     dpif_close(dpif);
198     if (argc > 2) {
199         error = dpctl_add_if(argc, argv, dpctl_p);
200     }
201     return error;
202 }
203
204 static int
205 dpctl_del_dp(int argc OVS_UNUSED, const char *argv[],
206              struct dpctl_params *dpctl_p)
207 {
208     struct dpif *dpif;
209     int error;
210
211     error = parsed_dpif_open(argv[1], false, &dpif);
212     if (error) {
213         dpctl_error(dpctl_p, error, "opening datapath");
214         return error;
215     }
216     error = dpif_delete(dpif);
217     if (error) {
218         dpctl_error(dpctl_p, error, "del_dp");
219     }
220
221     dpif_close(dpif);
222     return error;
223 }
224
225 static int
226 dpctl_add_if(int argc OVS_UNUSED, const char *argv[],
227              struct dpctl_params *dpctl_p)
228 {
229     struct dpif *dpif;
230     int i, error, lasterror = 0;
231
232     error = parsed_dpif_open(argv[1], false, &dpif);
233     if (error) {
234         dpctl_error(dpctl_p, error, "opening datapath");
235         return error;
236     }
237     for (i = 2; i < argc; i++) {
238         const char *name, *type;
239         char *save_ptr = NULL, *argcopy;
240         struct netdev *netdev = NULL;
241         struct smap args;
242         odp_port_t port_no = ODPP_NONE;
243         char *option;
244
245         argcopy = xstrdup(argv[i]);
246         name = strtok_r(argcopy, ",", &save_ptr);
247         type = "system";
248
249         if (!name) {
250             dpctl_error(dpctl_p, 0, "%s is not a valid network device name",
251                         argv[i]);
252             error = EINVAL;
253             goto next;
254         }
255
256         smap_init(&args);
257         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
258             char *save_ptr_2 = NULL;
259             char *key, *value;
260
261             key = strtok_r(option, "=", &save_ptr_2);
262             value = strtok_r(NULL, "", &save_ptr_2);
263             if (!value) {
264                 value = "";
265             }
266
267             if (!strcmp(key, "type")) {
268                 type = value;
269             } else if (!strcmp(key, "port_no")) {
270                 port_no = u32_to_odp(atoi(value));
271             } else if (!smap_add_once(&args, key, value)) {
272                 dpctl_error(dpctl_p, 0, "duplicate \"%s\" option", key);
273             }
274         }
275
276         error = netdev_open(name, type, &netdev);
277         if (error) {
278             dpctl_error(dpctl_p, error, "%s: failed to open network device",
279                         name);
280             goto next_destroy_args;
281         }
282
283         error = netdev_set_config(netdev, &args, NULL);
284         if (error) {
285             goto next_destroy_args;
286         }
287
288         error = dpif_port_add(dpif, netdev, &port_no);
289         if (error) {
290             dpctl_error(dpctl_p, error, "adding %s to %s failed", name,
291                         argv[1]);
292             goto next_destroy_args;
293         }
294
295         error = if_up(netdev);
296         if (error) {
297             dpctl_error(dpctl_p, error, "%s: failed bringing interface up",
298                         name);
299         }
300
301 next_destroy_args:
302         netdev_close(netdev);
303         smap_destroy(&args);
304 next:
305         free(argcopy);
306         if (error) {
307             lasterror = error;
308         }
309     }
310     dpif_close(dpif);
311
312     return lasterror;
313 }
314
315 static int
316 dpctl_set_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
317 {
318     struct dpif *dpif;
319     int i, error, lasterror = 0;
320
321     error = parsed_dpif_open(argv[1], false, &dpif);
322     if (error) {
323         dpctl_error(dpctl_p, error, "opening datapath");
324         return error;
325     }
326     for (i = 2; i < argc; i++) {
327         struct netdev *netdev = NULL;
328         struct dpif_port dpif_port;
329         char *save_ptr = NULL;
330         char *type = NULL;
331         char *argcopy;
332         const char *name;
333         struct smap args;
334         odp_port_t port_no;
335         char *option;
336         int error = 0;
337
338         argcopy = xstrdup(argv[i]);
339         name = strtok_r(argcopy, ",", &save_ptr);
340         if (!name) {
341             dpctl_error(dpctl_p, 0, "%s is not a valid network device name",
342                         argv[i]);
343             goto next;
344         }
345
346         /* Get the port's type from the datapath. */
347         error = dpif_port_query_by_name(dpif, name, &dpif_port);
348         if (error) {
349             dpctl_error(dpctl_p, error, "%s: failed to query port in %s", name,
350                         argv[1]);
351             goto next;
352         }
353         type = xstrdup(dpif_port.type);
354         port_no = dpif_port.port_no;
355         dpif_port_destroy(&dpif_port);
356
357         /* Retrieve its existing configuration. */
358         error = netdev_open(name, type, &netdev);
359         if (error) {
360             dpctl_error(dpctl_p, error, "%s: failed to open network device",
361                         name);
362             goto next;
363         }
364
365         smap_init(&args);
366         error = netdev_get_config(netdev, &args);
367         if (error) {
368             dpctl_error(dpctl_p, error, "%s: failed to fetch configuration",
369                         name);
370             goto next_destroy_args;
371         }
372
373         /* Parse changes to configuration. */
374         while ((option = strtok_r(NULL, ",", &save_ptr)) != NULL) {
375             char *save_ptr_2 = NULL;
376             char *key, *value;
377
378             key = strtok_r(option, "=", &save_ptr_2);
379             value = strtok_r(NULL, "", &save_ptr_2);
380             if (!value) {
381                 value = "";
382             }
383
384             if (!strcmp(key, "type")) {
385                 if (strcmp(value, type)) {
386                     dpctl_error(dpctl_p, 0,
387                                 "%s: can't change type from %s to %s",
388                                  name, type, value);
389                     error = EINVAL;
390                     goto next_destroy_args;
391                 }
392             } else if (!strcmp(key, "port_no")) {
393                 if (port_no != u32_to_odp(atoi(value))) {
394                     dpctl_error(dpctl_p, 0, "%s: can't change port number from"
395                               " %"PRIu32" to %d", name, port_no, atoi(value));
396                     error = EINVAL;
397                     goto next_destroy_args;
398                 }
399             } else if (value[0] == '\0') {
400                 smap_remove(&args, key);
401             } else {
402                 smap_replace(&args, key, value);
403             }
404         }
405
406         /* Update configuration. */
407         char *err_s = NULL;
408         error = netdev_set_config(netdev, &args, &err_s);
409         if (err_s || error) {
410             dpctl_error(dpctl_p, error, "%s",
411                         err_s ? err_s : "Error updating configuration");
412             free(err_s);
413         }
414         if (error) {
415             goto next_destroy_args;
416         }
417
418 next_destroy_args:
419         smap_destroy(&args);
420 next:
421         netdev_close(netdev);
422         free(type);
423         free(argcopy);
424         if (error) {
425             lasterror = error;
426         }
427     }
428     dpif_close(dpif);
429
430     return lasterror;
431 }
432
433 static bool
434 get_port_number(struct dpif *dpif, const char *name, odp_port_t *port,
435                 struct dpctl_params *dpctl_p)
436 {
437     struct dpif_port dpif_port;
438
439     if (!dpif_port_query_by_name(dpif, name, &dpif_port)) {
440         *port = dpif_port.port_no;
441         dpif_port_destroy(&dpif_port);
442         return true;
443     } else {
444         dpctl_error(dpctl_p, 0, "no port named %s", name);
445         return false;
446     }
447 }
448
449 static int
450 dpctl_del_if(int argc, const char *argv[], struct dpctl_params *dpctl_p)
451 {
452     struct dpif *dpif;
453     int i, error, lasterror = 0;
454
455     error = parsed_dpif_open(argv[1], false, &dpif);
456     if (error) {
457         dpctl_error(dpctl_p, error, "opening datapath");
458         return error;
459     }
460     for (i = 2; i < argc; i++) {
461         const char *name = argv[i];
462         odp_port_t port;
463
464         if (!name[strspn(name, "0123456789")]) {
465             port = u32_to_odp(atoi(name));
466         } else if (!get_port_number(dpif, name, &port, dpctl_p)) {
467             lasterror = ENOENT;
468             continue;
469         }
470
471         error = dpif_port_del(dpif, port);
472         if (error) {
473             dpctl_error(dpctl_p, error, "deleting port %s from %s failed",
474                         name, argv[1]);
475             lasterror = error;
476         }
477     }
478     dpif_close(dpif);
479     return lasterror;
480 }
481
482 static void
483 print_stat(struct dpctl_params *dpctl_p, const char *leader, uint64_t value)
484 {
485     dpctl_print(dpctl_p, "%s", leader);
486     if (value != UINT64_MAX) {
487         dpctl_print(dpctl_p, "%"PRIu64, value);
488     } else {
489         dpctl_print(dpctl_p, "?");
490     }
491 }
492
493 static void
494 print_human_size(struct dpctl_params *dpctl_p, uint64_t value)
495 {
496     if (value == UINT64_MAX) {
497         /* Nothing to do. */
498     } else if (value >= 1024ULL * 1024 * 1024 * 1024) {
499         dpctl_print(dpctl_p, " (%.1f TiB)",
500                     value / (1024.0 * 1024 * 1024 * 1024));
501     } else if (value >= 1024ULL * 1024 * 1024) {
502         dpctl_print(dpctl_p, " (%.1f GiB)", value / (1024.0 * 1024 * 1024));
503     } else if (value >= 1024ULL * 1024) {
504         dpctl_print(dpctl_p, " (%.1f MiB)", value / (1024.0 * 1024));
505     } else if (value >= 1024) {
506         dpctl_print(dpctl_p, " (%.1f KiB)", value / 1024.0);
507     }
508 }
509
510 static void
511 show_dpif(struct dpif *dpif, struct dpctl_params *dpctl_p)
512 {
513     struct dpif_port_dump dump;
514     struct dpif_port dpif_port;
515     struct dpif_dp_stats stats;
516     struct netdev *netdev;
517
518     dpctl_print(dpctl_p, "%s:\n", dpif_name(dpif));
519     if (!dpif_get_dp_stats(dpif, &stats)) {
520         dpctl_print(dpctl_p, "\tlookups: hit:%"PRIu64" missed:%"PRIu64
521                              " lost:%"PRIu64"\n\tflows: %"PRIu64"\n",
522                     stats.n_hit, stats.n_missed, stats.n_lost, stats.n_flows);
523         if (stats.n_masks != UINT32_MAX) {
524             uint64_t n_pkts = stats.n_hit + stats.n_missed;
525             double avg = n_pkts ? (double) stats.n_mask_hit / n_pkts : 0.0;
526
527             dpctl_print(dpctl_p, "\tmasks: hit:%"PRIu64" total:%"PRIu32
528                                  " hit/pkt:%.2f\n",
529                         stats.n_mask_hit, stats.n_masks, avg);
530         }
531     }
532
533     DPIF_PORT_FOR_EACH (&dpif_port, &dump, dpif) {
534         dpctl_print(dpctl_p, "\tport %u: %s",
535                     dpif_port.port_no, dpif_port.name);
536
537         if (strcmp(dpif_port.type, "system")) {
538             int error;
539
540             dpctl_print(dpctl_p, " (%s", dpif_port.type);
541
542             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
543             if (!error) {
544                 struct smap config;
545
546                 smap_init(&config);
547                 error = netdev_get_config(netdev, &config);
548                 if (!error) {
549                     const struct smap_node **nodes;
550                     size_t i;
551
552                     nodes = smap_sort(&config);
553                     for (i = 0; i < smap_count(&config); i++) {
554                         const struct smap_node *node = nodes[i];
555                         dpctl_print(dpctl_p, "%c %s=%s", i ? ',' : ':',
556                                     node->key, node->value);
557                     }
558                     free(nodes);
559                 } else {
560                     dpctl_print(dpctl_p, ", could not retrieve configuration "
561                                          "(%s)",  ovs_strerror(error));
562                 }
563                 smap_destroy(&config);
564
565                 netdev_close(netdev);
566             } else {
567                 dpctl_print(dpctl_p, ": open failed (%s)",
568                             ovs_strerror(error));
569             }
570             dpctl_print(dpctl_p, ")");
571         }
572         dpctl_print(dpctl_p, "\n");
573
574         if (dpctl_p->print_statistics) {
575             struct netdev_stats s;
576             int error;
577
578             error = netdev_open(dpif_port.name, dpif_port.type, &netdev);
579             if (error) {
580                 dpctl_print(dpctl_p, ", open failed (%s)",
581                             ovs_strerror(error));
582                 continue;
583             }
584             error = netdev_get_stats(netdev, &s);
585             if (!error) {
586                 netdev_close(netdev);
587                 print_stat(dpctl_p, "\t\tRX packets:", s.rx_packets);
588                 print_stat(dpctl_p, " errors:", s.rx_errors);
589                 print_stat(dpctl_p, " dropped:", s.rx_dropped);
590                 print_stat(dpctl_p, " overruns:", s.rx_over_errors);
591                 print_stat(dpctl_p, " frame:", s.rx_frame_errors);
592                 dpctl_print(dpctl_p, "\n");
593
594                 print_stat(dpctl_p, "\t\tTX packets:", s.tx_packets);
595                 print_stat(dpctl_p, " errors:", s.tx_errors);
596                 print_stat(dpctl_p, " dropped:", s.tx_dropped);
597                 print_stat(dpctl_p, " aborted:", s.tx_aborted_errors);
598                 print_stat(dpctl_p, " carrier:", s.tx_carrier_errors);
599                 dpctl_print(dpctl_p, "\n");
600
601                 print_stat(dpctl_p, "\t\tcollisions:", s.collisions);
602                 dpctl_print(dpctl_p, "\n");
603
604                 print_stat(dpctl_p, "\t\tRX bytes:", s.rx_bytes);
605                 print_human_size(dpctl_p, s.rx_bytes);
606                 print_stat(dpctl_p, "  TX bytes:", s.tx_bytes);
607                 print_human_size(dpctl_p, s.tx_bytes);
608                 dpctl_print(dpctl_p, "\n");
609             } else {
610                 dpctl_print(dpctl_p, ", could not retrieve stats (%s)",
611                             ovs_strerror(error));
612             }
613         }
614     }
615     dpif_close(dpif);
616 }
617
618 static int
619 dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p)
620 {
621     int error, lasterror = 0;
622     if (argc > 1) {
623         int i;
624         for (i = 1; i < argc; i++) {
625             const char *name = argv[i];
626             struct dpif *dpif;
627
628             error = parsed_dpif_open(name, false, &dpif);
629             if (!error) {
630                 show_dpif(dpif, dpctl_p);
631             } else {
632                 dpctl_error(dpctl_p, error, "opening datapath %s failed",
633                             name);
634                 lasterror = error;
635             }
636         }
637     } else {
638         struct sset types;
639         const char *type;
640
641         sset_init(&types);
642         dp_enumerate_types(&types);
643         SSET_FOR_EACH (type, &types) {
644             struct sset names;
645             const char *name;
646
647             sset_init(&names);
648             error = dp_enumerate_names(type, &names);
649             if (error) {
650                 lasterror = error;
651                 goto next;
652             }
653             SSET_FOR_EACH (name, &names) {
654                 struct dpif *dpif;
655
656                 error = dpif_open(name, type, &dpif);
657                 if (!error) {
658                     show_dpif(dpif, dpctl_p);
659                 } else {
660                     dpctl_error(dpctl_p, error, "opening datapath %s failed",
661                                 name);
662                     lasterror = error;
663                 }
664             }
665 next:
666             sset_destroy(&names);
667         }
668         sset_destroy(&types);
669     }
670     return lasterror;
671 }
672
673 static int
674 dpctl_dump_dps(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
675                struct dpctl_params *dpctl_p)
676 {
677     struct sset dpif_names, dpif_types;
678     const char *type;
679     int error, lasterror = 0;
680
681     sset_init(&dpif_names);
682     sset_init(&dpif_types);
683     dp_enumerate_types(&dpif_types);
684
685     SSET_FOR_EACH (type, &dpif_types) {
686         const char *name;
687
688         error = dp_enumerate_names(type, &dpif_names);
689         if (error) {
690             lasterror = error;
691         }
692
693         SSET_FOR_EACH (name, &dpif_names) {
694             struct dpif *dpif;
695             if (!dpif_open(name, type, &dpif)) {
696                 dpctl_print(dpctl_p, "%s\n", dpif_name(dpif));
697                 dpif_close(dpif);
698             }
699         }
700     }
701
702     sset_destroy(&dpif_names);
703     sset_destroy(&dpif_types);
704     return lasterror;
705 }
706
707 static void
708 format_dpif_flow(struct ds *ds, const struct dpif_flow *f, struct hmap *ports,
709                  struct dpctl_params *dpctl_p)
710 {
711     if (dpctl_p->verbosity) {
712         if (f->ufid_present) {
713             odp_format_ufid(&f->ufid, ds);
714             ds_put_cstr(ds, ", ");
715         } else {
716             ds_put_cstr(ds, "ufid:<empty>, ");
717         }
718     }
719     odp_flow_format(f->key, f->key_len, f->mask, f->mask_len, ports, ds,
720                     dpctl_p->verbosity);
721     ds_put_cstr(ds, ", ");
722
723     dpif_flow_stats_format(&f->stats, ds);
724     ds_put_cstr(ds, ", actions:");
725     format_odp_actions(ds, f->actions, f->actions_len);
726 }
727
728 static int
729 dpctl_dump_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
730 {
731     struct dpif *dpif;
732     struct ds ds;
733     char *name;
734
735     char *filter = NULL;
736     struct flow flow_filter;
737     struct flow_wildcards wc_filter;
738
739     struct dpif_port_dump port_dump;
740     struct dpif_port dpif_port;
741     struct hmap portno_names;
742     struct simap names_portno;
743
744     struct dpif_flow_dump_thread *flow_dump_thread;
745     struct dpif_flow_dump *flow_dump;
746     struct dpif_flow f;
747     int pmd_id = PMD_ID_NULL;
748     int error;
749
750     if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
751         filter = xstrdup(argv[--argc] + 7);
752     }
753     name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
754     if (!name) {
755         error = EINVAL;
756         goto out_freefilter;
757     }
758
759     error = parsed_dpif_open(name, false, &dpif);
760     free(name);
761     if (error) {
762         dpctl_error(dpctl_p, error, "opening datapath");
763         goto out_freefilter;
764     }
765
766
767     hmap_init(&portno_names);
768     simap_init(&names_portno);
769     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
770         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
771         simap_put(&names_portno, dpif_port.name,
772                   odp_to_u32(dpif_port.port_no));
773     }
774
775     if (filter) {
776         char *err = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks,
777                                          filter, &names_portno);
778         if (err) {
779             dpctl_error(dpctl_p, 0, "Failed to parse filter (%s)", err);
780             error = EINVAL;
781             goto out_dpifclose;
782         }
783     }
784
785     ds_init(&ds);
786     flow_dump = dpif_flow_dump_create(dpif, false);
787     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
788     while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
789         if (filter) {
790             struct flow flow;
791             struct flow_wildcards wc;
792             struct match match, match_filter;
793             struct minimatch minimatch;
794
795             odp_flow_key_to_flow(f.key, f.key_len, &flow);
796             odp_flow_key_to_mask(f.mask, f.mask_len, &wc.masks, &flow);
797             match_init(&match, &flow, &wc);
798
799             match_init(&match_filter, &flow_filter, &wc);
800             match_init(&match_filter, &match_filter.flow, &wc_filter);
801             minimatch_init(&minimatch, &match_filter);
802
803             if (!minimatch_matches_flow(&minimatch, &match.flow)) {
804                 minimatch_destroy(&minimatch);
805                 continue;
806             }
807             minimatch_destroy(&minimatch);
808         }
809         ds_clear(&ds);
810         /* If 'pmd_id' is specified, overlapping flows could be dumped from
811          * different pmd threads.  So, separates dumps from different pmds
812          * by printing a title line. */
813         if (pmd_id != f.pmd_id) {
814             if (f.pmd_id == NON_PMD_CORE_ID) {
815                 ds_put_format(&ds, "flow-dump from non-dpdk interfaces:\n");
816             } else {
817                 ds_put_format(&ds, "flow-dump from pmd on cpu core: %d\n",
818                               f.pmd_id);
819             }
820             pmd_id = f.pmd_id;
821         }
822         format_dpif_flow(&ds, &f, &portno_names, dpctl_p);
823         dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
824     }
825     dpif_flow_dump_thread_destroy(flow_dump_thread);
826     error = dpif_flow_dump_destroy(flow_dump);
827
828     if (error) {
829         dpctl_error(dpctl_p, error, "Failed to dump flows from datapath");
830     }
831     ds_destroy(&ds);
832
833 out_dpifclose:
834     odp_portno_names_destroy(&portno_names);
835     simap_destroy(&names_portno);
836     hmap_destroy(&portno_names);
837     dpif_close(dpif);
838 out_freefilter:
839     free(filter);
840     return error;
841 }
842
843 /* Extracts the in_port from the parsed keys, and returns the reference
844  * to the 'struct netdev *' of the dpif port.  On error, returns NULL.
845  * Users must call 'netdev_close()' after finish using the returned
846  * reference. */
847 static struct netdev *
848 get_in_port_netdev_from_key(struct dpif *dpif, const struct ofpbuf *key)
849 {
850     const struct nlattr *in_port_nla;
851     struct netdev *dev = NULL;
852
853     in_port_nla = nl_attr_find(key, 0, OVS_KEY_ATTR_IN_PORT);
854     if (in_port_nla) {
855         struct dpif_port dpif_port;
856         odp_port_t port_no;
857         int error;
858
859         port_no = ODP_PORT_C(nl_attr_get_u32(in_port_nla));
860         error = dpif_port_query_by_number(dpif, port_no, &dpif_port);
861         if (error) {
862             goto out;
863         }
864
865         netdev_open(dpif_port.name, dpif_port.type, &dev);
866         dpif_port_destroy(&dpif_port);
867     }
868
869 out:
870     return dev;
871 }
872
873 static int
874 dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
875                struct dpctl_params *dpctl_p)
876 {
877     const char *key_s = argv[argc - 2];
878     const char *actions_s = argv[argc - 1];
879     struct netdev *in_port_netdev = NULL;
880     struct dpif_flow_stats stats;
881     struct dpif_port dpif_port;
882     struct dpif_port_dump port_dump;
883     struct ofpbuf actions;
884     struct ofpbuf key;
885     struct ofpbuf mask;
886     struct dpif *dpif;
887     ovs_u128 ufid;
888     bool ufid_present;
889     char *dp_name;
890     struct simap port_names;
891     int n, error;
892
893     dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
894     if (!dp_name) {
895         return EINVAL;
896     }
897     error = parsed_dpif_open(dp_name, false, &dpif);
898     free(dp_name);
899     if (error) {
900         dpctl_error(dpctl_p, error, "opening datapath");
901         return error;
902     }
903
904     ufid_present = false;
905     n = odp_ufid_from_string(key_s, &ufid);
906     if (n < 0) {
907         dpctl_error(dpctl_p, -n, "parsing flow ufid");
908         return -n;
909     } else if (n) {
910         key_s += n;
911         ufid_present = true;
912     }
913
914     simap_init(&port_names);
915     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
916         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
917     }
918
919     ofpbuf_init(&key, 0);
920     ofpbuf_init(&mask, 0);
921     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
922     simap_destroy(&port_names);
923     if (error) {
924         dpctl_error(dpctl_p, error, "parsing flow key");
925         goto out_freekeymask;
926     }
927
928     ofpbuf_init(&actions, 0);
929     error = odp_actions_from_string(actions_s, NULL, &actions);
930     if (error) {
931         dpctl_error(dpctl_p, error, "parsing actions");
932         goto out_freeactions;
933     }
934
935     /* For DPDK interface, applies the operation to all pmd threads
936      * on the same numa node. */
937     in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
938     if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
939         int numa_id;
940
941         numa_id = netdev_get_numa_id(in_port_netdev);
942         if (ovs_numa_numa_id_is_valid(numa_id)) {
943             struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
944             struct ovs_numa_info *iter;
945
946             FOR_EACH_CORE_ON_NUMA (iter, dump) {
947                 if (ovs_numa_core_is_pinned(iter->core_id)) {
948                     error = dpif_flow_put(dpif, flags,
949                                           key.data, key.size,
950                                           mask.size == 0 ? NULL : mask.data,
951                                           mask.size, actions.data,
952                                           actions.size, ufid_present ? &ufid : NULL,
953                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
954                 }
955             }
956             ovs_numa_dump_destroy(dump);
957         } else {
958             error = EINVAL;
959         }
960     } else {
961         error = dpif_flow_put(dpif, flags,
962                               key.data, key.size,
963                               mask.size == 0 ? NULL : mask.data,
964                               mask.size, actions.data,
965                               actions.size, ufid_present ? &ufid : NULL,
966                               PMD_ID_NULL, dpctl_p->print_statistics ? &stats : NULL);
967     }
968     if (error) {
969         dpctl_error(dpctl_p, error, "updating flow table");
970         goto out_freeactions;
971     }
972
973     if (dpctl_p->print_statistics) {
974         struct ds s;
975
976         ds_init(&s);
977         dpif_flow_stats_format(&stats, &s);
978         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
979         ds_destroy(&s);
980     }
981
982 out_freeactions:
983     ofpbuf_uninit(&actions);
984 out_freekeymask:
985     ofpbuf_uninit(&mask);
986     ofpbuf_uninit(&key);
987     dpif_close(dpif);
988     netdev_close(in_port_netdev);
989     return error;
990 }
991
992 static int
993 dpctl_add_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
994 {
995     return dpctl_put_flow(argc, argv, DPIF_FP_CREATE, dpctl_p);
996 }
997
998 static int
999 dpctl_mod_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1000 {
1001     enum dpif_flow_put_flags flags;
1002
1003     flags = DPIF_FP_MODIFY;
1004     if (dpctl_p->may_create) {
1005         flags |= DPIF_FP_CREATE;
1006     }
1007     if (dpctl_p->zero_statistics) {
1008         flags |= DPIF_FP_ZERO_STATS;
1009     }
1010
1011     return dpctl_put_flow(argc, argv, flags, dpctl_p);
1012 }
1013
1014 static int
1015 dpctl_get_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1016 {
1017     const char *key_s = argv[argc - 1];
1018     struct dpif_flow flow;
1019     struct dpif_port dpif_port;
1020     struct dpif_port_dump port_dump;
1021     struct dpif *dpif;
1022     char *dp_name;
1023     struct hmap portno_names;
1024     ovs_u128 ufid;
1025     struct ofpbuf buf;
1026     uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1027     struct ds ds;
1028     int n, error;
1029
1030     dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1031     if (!dp_name) {
1032         return EINVAL;
1033     }
1034     error = parsed_dpif_open(dp_name, false, &dpif);
1035     free(dp_name);
1036     if (error) {
1037         dpctl_error(dpctl_p, error, "opening datapath");
1038         return error;
1039     }
1040
1041     ofpbuf_use_stub(&buf, &stub, sizeof stub);
1042     hmap_init(&portno_names);
1043     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1044         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
1045     }
1046
1047     n = odp_ufid_from_string(key_s, &ufid);
1048     if (n <= 0) {
1049         dpctl_error(dpctl_p, -n, "parsing flow ufid");
1050         goto out;
1051     }
1052
1053     /* Does not work for DPDK, since do not know which 'pmd' to apply the
1054      * operation.  So, just uses PMD_ID_NULL. */
1055     error = dpif_flow_get(dpif, NULL, 0, &ufid, PMD_ID_NULL, &buf, &flow);
1056     if (error) {
1057         dpctl_error(dpctl_p, error, "getting flow");
1058         goto out;
1059     }
1060
1061     ds_init(&ds);
1062     format_dpif_flow(&ds, &flow, &portno_names, dpctl_p);
1063     dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
1064     ds_destroy(&ds);
1065
1066 out:
1067     odp_portno_names_destroy(&portno_names);
1068     hmap_destroy(&portno_names);
1069     ofpbuf_uninit(&buf);
1070     dpif_close(dpif);
1071     return error;
1072 }
1073
1074 static int
1075 dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1076 {
1077     const char *key_s = argv[argc - 1];
1078     struct netdev *in_port_netdev = NULL;
1079     struct dpif_flow_stats stats;
1080     struct dpif_port dpif_port;
1081     struct dpif_port_dump port_dump;
1082     struct ofpbuf key;
1083     struct ofpbuf mask; /* To be ignored. */
1084     struct dpif *dpif;
1085     ovs_u128 ufid;
1086     bool ufid_present;
1087     char *dp_name;
1088     struct simap port_names;
1089     int n, error;
1090
1091     dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1092     if (!dp_name) {
1093         return EINVAL;
1094     }
1095     error = parsed_dpif_open(dp_name, false, &dpif);
1096     free(dp_name);
1097     if (error) {
1098         dpctl_error(dpctl_p, error, "opening datapath");
1099         return error;
1100     }
1101
1102     ufid_present = false;
1103     n = odp_ufid_from_string(key_s, &ufid);
1104     if (n < 0) {
1105         dpctl_error(dpctl_p, -n, "parsing flow ufid");
1106         return -n;
1107     } else if (n) {
1108         key_s += n;
1109         ufid_present = true;
1110     }
1111
1112     simap_init(&port_names);
1113     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1114         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
1115     }
1116
1117     ofpbuf_init(&key, 0);
1118     ofpbuf_init(&mask, 0);
1119
1120     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
1121     if (error) {
1122         dpctl_error(dpctl_p, error, "parsing flow key");
1123         goto out;
1124     }
1125
1126     /* For DPDK interface, applies the operation to all pmd threads
1127      * on the same numa node. */
1128     in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
1129     if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
1130         int numa_id;
1131
1132         numa_id = netdev_get_numa_id(in_port_netdev);
1133         if (ovs_numa_numa_id_is_valid(numa_id)) {
1134             struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
1135             struct ovs_numa_info *iter;
1136
1137             FOR_EACH_CORE_ON_NUMA (iter, dump) {
1138                 if (ovs_numa_core_is_pinned(iter->core_id)) {
1139                     error = dpif_flow_del(dpif, key.data,
1140                                           key.size, ufid_present ? &ufid : NULL,
1141                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
1142                 }
1143             }
1144             ovs_numa_dump_destroy(dump);
1145         } else {
1146             error = EINVAL;
1147         }
1148     } else {
1149         error = dpif_flow_del(dpif, key.data, key.size,
1150                               ufid_present ? &ufid : NULL, PMD_ID_NULL,
1151                               dpctl_p->print_statistics ? &stats : NULL);
1152     }
1153     if (error) {
1154         dpctl_error(dpctl_p, error, "deleting flow");
1155         if (error == ENOENT && !ufid_present) {
1156             struct ds s;
1157
1158             ds_init(&s);
1159             ds_put_format(&s, "Perhaps you need to specify a UFID?");
1160             dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1161             ds_destroy(&s);
1162         }
1163         goto out;
1164     }
1165
1166     if (dpctl_p->print_statistics) {
1167         struct ds s;
1168
1169         ds_init(&s);
1170         dpif_flow_stats_format(&stats, &s);
1171         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1172         ds_destroy(&s);
1173     }
1174
1175 out:
1176     ofpbuf_uninit(&mask);
1177     ofpbuf_uninit(&key);
1178     simap_destroy(&port_names);
1179     dpif_close(dpif);
1180     netdev_close(in_port_netdev);
1181     return error;
1182 }
1183
1184 static int
1185 dpctl_del_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1186 {
1187     struct dpif *dpif;
1188     char *name;
1189     int error;
1190
1191     name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1192     if (!name) {
1193         return EINVAL;
1194     }
1195     error = parsed_dpif_open(name, false, &dpif);
1196     free(name);
1197     if (error) {
1198         dpctl_error(dpctl_p, error, "opening datapath");
1199         return error;
1200     }
1201
1202     error = dpif_flow_flush(dpif);
1203     if (error) {
1204         dpctl_error(dpctl_p, error, "deleting all flows");
1205     }
1206     dpif_close(dpif);
1207     return error;
1208 }
1209
1210 static int
1211 dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1212            struct dpctl_params *dpctl_p)
1213 {
1214     if (dpctl_p->usage) {
1215         dpctl_p->usage(dpctl_p->aux);
1216     }
1217
1218     return 0;
1219 }
1220
1221 static int
1222 dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1223                     struct dpctl_params *dpctl_p)
1224 {
1225     struct ds ds = DS_EMPTY_INITIALIZER;
1226     const struct dpctl_command *commands = get_all_dpctl_commands();
1227
1228     ds_put_cstr(&ds, "The available commands are:\n");
1229     for (; commands->name; commands++) {
1230         const struct dpctl_command *c = commands;
1231
1232         ds_put_format(&ds, "  %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "",
1233                       c->name, c->usage);
1234     }
1235     dpctl_puts(dpctl_p, false, ds.string);
1236     ds_destroy(&ds);
1237
1238     return 0;
1239 }
1240 \f
1241 /* Undocumented commands for unit testing. */
1242
1243 static int
1244 dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p)
1245 {
1246     int i, error = 0;
1247
1248     for (i = 1; i < argc; i++) {
1249         struct ofpbuf actions;
1250         struct ds s;
1251
1252         ofpbuf_init(&actions, 0);
1253         error = odp_actions_from_string(argv[i], NULL, &actions);
1254
1255         if (error) {
1256             ofpbuf_uninit(&actions);
1257             dpctl_error(dpctl_p, error, "odp_actions_from_string");
1258             return error;
1259         }
1260
1261         ds_init(&s);
1262         format_odp_actions(&s, actions.data, actions.size);
1263         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1264         ds_destroy(&s);
1265
1266         ofpbuf_uninit(&actions);
1267     }
1268
1269     return error;
1270 }
1271
1272 struct actions_for_flow {
1273     struct hmap_node hmap_node;
1274     struct flow flow;
1275     struct ofpbuf actions;
1276 };
1277
1278 static struct actions_for_flow *
1279 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1280 {
1281     uint32_t hash = flow_hash(flow, 0);
1282     struct actions_for_flow *af;
1283
1284     HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1285         if (flow_equal(&af->flow, flow)) {
1286             return af;
1287         }
1288     }
1289
1290     af = xmalloc(sizeof *af);
1291     af->flow = *flow;
1292     ofpbuf_init(&af->actions, 0);
1293     hmap_insert(actions_per_flow, &af->hmap_node, hash);
1294     return af;
1295 }
1296
1297 static int
1298 compare_actions_for_flow(const void *a_, const void *b_)
1299 {
1300     struct actions_for_flow *const *a = a_;
1301     struct actions_for_flow *const *b = b_;
1302
1303     return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1304 }
1305
1306 static int
1307 compare_output_actions(const void *a_, const void *b_)
1308 {
1309     const struct nlattr *a = a_;
1310     const struct nlattr *b = b_;
1311     uint32_t a_port = nl_attr_get_u32(a);
1312     uint32_t b_port = nl_attr_get_u32(b);
1313
1314     return a_port < b_port ? -1 : a_port > b_port;
1315 }
1316
1317 static void
1318 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1319 {
1320     size_t bytes = (uint8_t *) end - (uint8_t *) first;
1321     size_t n = bytes / NL_A_U32_SIZE;
1322
1323     ovs_assert(bytes % NL_A_U32_SIZE == 0);
1324     qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1325 }
1326
1327 static void
1328 sort_output_actions(struct nlattr *actions, size_t length)
1329 {
1330     struct nlattr *first_output = NULL;
1331     struct nlattr *a;
1332     int left;
1333
1334     NL_ATTR_FOR_EACH (a, left, actions, length) {
1335         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1336             if (!first_output) {
1337                 first_output = a;
1338             }
1339         } else {
1340             if (first_output) {
1341                 sort_output_actions__(first_output, a);
1342                 first_output = NULL;
1343             }
1344         }
1345     }
1346     if (first_output) {
1347         uint8_t *end = (uint8_t *) actions + length;
1348         sort_output_actions__(first_output,
1349                               ALIGNED_CAST(struct nlattr *, end));
1350     }
1351 }
1352
1353 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1354  * have the syntax used by "ovs-dpctl dump-flows".
1355  *
1356  * This command prints ACTIONS in a format that shows what happens for each
1357  * VLAN, independent of the order of the ACTIONS.  For example, there is more
1358  * than one way to output a packet on VLANs 9 and 11, but this command will
1359  * print the same output for any form.
1360  *
1361  * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1362  * so far the implementation only covers VLANs. */
1363 static int
1364 dpctl_normalize_actions(int argc, const char *argv[],
1365                         struct dpctl_params *dpctl_p)
1366 {
1367     struct simap port_names;
1368     struct ofpbuf keybuf;
1369     struct flow flow;
1370     struct ofpbuf odp_actions;
1371     struct hmap actions_per_flow;
1372     struct actions_for_flow **afs;
1373     struct actions_for_flow *af;
1374     struct nlattr *a;
1375     size_t n_afs;
1376     struct ds s;
1377     int left;
1378     int i, error;
1379
1380     ds_init(&s);
1381
1382     simap_init(&port_names);
1383     for (i = 3; i < argc; i++) {
1384         char name[16];
1385         int number;
1386
1387         if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1388             uintptr_t n = number;
1389             simap_put(&port_names, name, n);
1390         } else {
1391             dpctl_error(dpctl_p, 0, "%s: expected NAME=NUMBER", argv[i]);
1392             error = EINVAL;
1393             goto out;
1394         }
1395     }
1396
1397     /* Parse flow key. */
1398     ofpbuf_init(&keybuf, 0);
1399     error = odp_flow_from_string(argv[1], &port_names, &keybuf, NULL);
1400     if (error) {
1401         dpctl_error(dpctl_p, error, "odp_flow_key_from_string");
1402         goto out_freekeybuf;
1403     }
1404
1405     ds_clear(&s);
1406     odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL,
1407                     &s, dpctl_p->verbosity);
1408     dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s));
1409
1410     error = odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow);
1411     if (error) {
1412         dpctl_error(dpctl_p, error, "odp_flow_key_to_flow");
1413         goto out_freekeybuf;
1414     }
1415
1416     /* Parse actions. */
1417     ofpbuf_init(&odp_actions, 0);
1418     error = odp_actions_from_string(argv[2], &port_names, &odp_actions);
1419     if (error) {
1420         dpctl_error(dpctl_p, error, "odp_actions_from_string");
1421         goto out_freeactions;
1422     }
1423
1424     if (dpctl_p->verbosity) {
1425         ds_clear(&s);
1426         format_odp_actions(&s, odp_actions.data, odp_actions.size);
1427         dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s));
1428     }
1429
1430     hmap_init(&actions_per_flow);
1431     NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1432         const struct ovs_action_push_vlan *push;
1433         switch(nl_attr_type(a)) {
1434         case OVS_ACTION_ATTR_POP_VLAN:
1435             flow.vlan_tci = htons(0);
1436             continue;
1437
1438         case OVS_ACTION_ATTR_PUSH_VLAN:
1439             push = nl_attr_get_unspec(a, sizeof *push);
1440             flow.vlan_tci = push->vlan_tci;
1441             continue;
1442         }
1443
1444         af = get_actions_for_flow(&actions_per_flow, &flow);
1445         nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1446                           nl_attr_get(a), nl_attr_get_size(a));
1447     }
1448
1449     n_afs = hmap_count(&actions_per_flow);
1450     afs = xmalloc(n_afs * sizeof *afs);
1451     i = 0;
1452     HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1453         afs[i++] = af;
1454     }
1455
1456     ovs_assert(i == n_afs);
1457     hmap_destroy(&actions_per_flow);
1458
1459     qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1460
1461     for (i = 0; i < n_afs; i++) {
1462         struct actions_for_flow *af = afs[i];
1463
1464         sort_output_actions(af->actions.data, af->actions.size);
1465
1466         if (af->flow.vlan_tci != htons(0)) {
1467             dpctl_print(dpctl_p, "vlan(vid=%"PRIu16",pcp=%d): ",
1468                         vlan_tci_to_vid(af->flow.vlan_tci),
1469                         vlan_tci_to_pcp(af->flow.vlan_tci));
1470         } else {
1471             dpctl_print(dpctl_p, "no vlan: ");
1472         }
1473
1474         if (eth_type_mpls(af->flow.dl_type)) {
1475             dpctl_print(dpctl_p, "mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1476                         mpls_lse_to_label(af->flow.mpls_lse[0]),
1477                         mpls_lse_to_tc(af->flow.mpls_lse[0]),
1478                         mpls_lse_to_ttl(af->flow.mpls_lse[0]));
1479         } else {
1480             dpctl_print(dpctl_p, "no mpls: ");
1481         }
1482
1483         ds_clear(&s);
1484         format_odp_actions(&s, af->actions.data, af->actions.size);
1485         dpctl_puts(dpctl_p, false, ds_cstr(&s));
1486
1487         ofpbuf_uninit(&af->actions);
1488         free(af);
1489     }
1490     free(afs);
1491
1492
1493 out_freeactions:
1494     ofpbuf_uninit(&odp_actions);
1495 out_freekeybuf:
1496     ofpbuf_uninit(&keybuf);
1497 out:
1498     simap_destroy(&port_names);
1499     ds_destroy(&s);
1500
1501     return error;
1502 }
1503 \f
1504 static const struct dpctl_command all_commands[] = {
1505     { "add-dp", "add-dp dp [iface...]", 1, INT_MAX, dpctl_add_dp },
1506     { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp },
1507     { "add-if", "add-if dp iface...", 2, INT_MAX, dpctl_add_if },
1508     { "del-if", "del-if dp iface...", 2, INT_MAX, dpctl_del_if },
1509     { "set-if", "set-if dp iface...", 2, INT_MAX, dpctl_set_if },
1510     { "dump-dps", "", 0, 0, dpctl_dump_dps },
1511     { "show", "[dp...]", 0, INT_MAX, dpctl_show },
1512     { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows },
1513     { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow },
1514     { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow },
1515     { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow },
1516     { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow },
1517     { "del-flows", "[dp]", 0, 1, dpctl_del_flows },
1518     { "help", "", 0, INT_MAX, dpctl_help },
1519     { "list-commands", "", 0, INT_MAX, dpctl_list_commands },
1520
1521     /* Undocumented commands for testing. */
1522     { "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions },
1523     { "normalize-actions", "actions", 2, INT_MAX, dpctl_normalize_actions },
1524
1525     { NULL, NULL, 0, 0, NULL },
1526 };
1527
1528 static const struct dpctl_command *get_all_dpctl_commands(void)
1529 {
1530     return all_commands;
1531 }
1532
1533 /* Runs the command designated by argv[0] within the command table specified by
1534  * 'commands', which must be terminated by a command whose 'name' member is a
1535  * null pointer. */
1536 int
1537 dpctl_run_command(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1538 {
1539     const struct dpctl_command *p;
1540
1541     if (argc < 1) {
1542         dpctl_error(dpctl_p, 0, "missing command name; use --help for help");
1543         return EINVAL;
1544     }
1545
1546     for (p = all_commands; p->name != NULL; p++) {
1547         if (!strcmp(p->name, argv[0])) {
1548             int n_arg = argc - 1;
1549             if (n_arg < p->min_args) {
1550                 dpctl_error(dpctl_p, 0,
1551                             "'%s' command requires at least %d arguments",
1552                             p->name, p->min_args);
1553                 return EINVAL;
1554             } else if (n_arg > p->max_args) {
1555                 dpctl_error(dpctl_p, 0,
1556                             "'%s' command takes at most %d arguments",
1557                             p->name, p->max_args);
1558                 return EINVAL;
1559             } else {
1560                 return p->handler(argc, argv, dpctl_p);
1561             }
1562         }
1563     }
1564
1565     dpctl_error(dpctl_p, 0, "unknown command '%s'; use --help for help",
1566                 argv[0]);
1567     return EINVAL;
1568 }
1569 \f
1570 static void
1571 dpctl_unixctl_print(void *userdata, bool error OVS_UNUSED, const char *msg)
1572 {
1573     struct ds *ds = userdata;
1574     ds_put_cstr(ds, msg);
1575 }
1576
1577 static void
1578 dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
1579                       void *aux)
1580 {
1581     struct ds ds = DS_EMPTY_INITIALIZER;
1582     struct dpctl_params dpctl_p;
1583     bool error = false;
1584
1585     dpctl_command_handler *handler = (dpctl_command_handler *) aux;
1586
1587     dpctl_p.print_statistics = false;
1588     dpctl_p.zero_statistics = false;
1589     dpctl_p.may_create = false;
1590     dpctl_p.verbosity = 0;
1591
1592     /* Parse options (like getopt). Unfortunately it does
1593      * not seem a good idea to call getopt_long() here, since it uses global
1594      * variables */
1595     while (argc > 1 && !error) {
1596         const char *arg = argv[1];
1597         if (!strncmp(arg, "--", 2)) {
1598             /* Long option */
1599             if (!strcmp(arg, "--statistics")) {
1600                 dpctl_p.print_statistics = true;
1601             } else if (!strcmp(arg, "--clear")) {
1602                 dpctl_p.zero_statistics = true;
1603             } else if (!strcmp(arg, "--may-create")) {
1604                 dpctl_p.may_create = true;
1605             } else if (!strcmp(arg, "--more")) {
1606                 dpctl_p.verbosity++;
1607             } else {
1608                 ds_put_format(&ds, "Unrecognized option %s", argv[1]);
1609                 error = true;
1610             }
1611         } else if (arg[0] == '-' && arg[1] != '\0') {
1612             /* Short option[s] */
1613             const char *opt = &arg[1];
1614
1615             while (*opt && !error) {
1616                 switch (*opt) {
1617                 case 'm':
1618                     dpctl_p.verbosity++;
1619                     break;
1620                 case 's':
1621                     dpctl_p.print_statistics = true;
1622                     break;
1623                 default:
1624                     ds_put_format(&ds, "Unrecognized option -%c", *opt);
1625                     error = true;
1626                     break;
1627                 }
1628                 opt++;
1629             }
1630         } else {
1631             /* Doesn't start with -, not an option */
1632             break;
1633         }
1634
1635         if (error) {
1636             break;
1637         }
1638         argv++;
1639         argc--;
1640     }
1641
1642     if (!error) {
1643         dpctl_p.is_appctl = true;
1644         dpctl_p.output = dpctl_unixctl_print;
1645         dpctl_p.aux = &ds;
1646
1647         error = handler(argc, argv, &dpctl_p) != 0;
1648     }
1649
1650     if (error) {
1651         unixctl_command_reply_error(conn, ds_cstr(&ds));
1652     } else {
1653         unixctl_command_reply(conn, ds_cstr(&ds));
1654     }
1655
1656     ds_destroy(&ds);
1657 }
1658
1659 void
1660 dpctl_unixctl_register(void)
1661 {
1662     const struct dpctl_command *p;
1663
1664     for (p = all_commands; p->name != NULL; p++) {
1665         char *cmd_name = xasprintf("dpctl/%s", p->name);
1666         unixctl_command_register(cmd_name, "", p->min_args, p->max_args,
1667                                  dpctl_unixctl_handler, p->handler);
1668         free(cmd_name);
1669     }
1670 }