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