ovs-ofctl: Always prints recirc_id in decimal
[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 }
616
617 typedef void (*dps_for_each_cb)(struct dpif *, struct dpctl_params *);
618
619 static int
620 dps_for_each(struct dpctl_params *dpctl_p, dps_for_each_cb cb)
621 {
622     struct sset dpif_names = SSET_INITIALIZER(&dpif_names),
623                 dpif_types = SSET_INITIALIZER(&dpif_types);
624     int error, openerror = 0, enumerror = 0;
625     const char *type, *name;
626     bool at_least_one = false;
627
628     dp_enumerate_types(&dpif_types);
629
630     SSET_FOR_EACH (type, &dpif_types) {
631         error = dp_enumerate_names(type, &dpif_names);
632         if (error) {
633             enumerror = error;
634         }
635
636         SSET_FOR_EACH (name, &dpif_names) {
637             struct dpif *dpif;
638
639             at_least_one = true;
640             error = dpif_open(name, type, &dpif);
641             if (!error) {
642                 cb(dpif, dpctl_p);
643                 dpif_close(dpif);
644             } else {
645                 openerror = error;
646                 dpctl_error(dpctl_p, error, "opening datapath %s failed",
647                             name);
648             }
649         }
650     }
651
652     sset_destroy(&dpif_names);
653     sset_destroy(&dpif_types);
654
655     /* If there has been an error while opening a datapath it should be
656      * reported.  Otherwise, we want to ignore the errors generated by
657      * dp_enumerate_names() if at least one datapath has been discovered,
658      * because they're not interesting for the user.  This happens, for
659      * example, if OVS is using a userspace datapath and the kernel module
660      * is not loaded. */
661     if (openerror) {
662         return openerror;
663     } else {
664         return at_least_one ? 0 : enumerror;
665     }
666 }
667
668 static int
669 dpctl_show(int argc, const char *argv[], struct dpctl_params *dpctl_p)
670 {
671     int error, lasterror = 0;
672     if (argc > 1) {
673         int i;
674         for (i = 1; i < argc; i++) {
675             const char *name = argv[i];
676             struct dpif *dpif;
677
678             error = parsed_dpif_open(name, false, &dpif);
679             if (!error) {
680                 show_dpif(dpif, dpctl_p);
681                 dpif_close(dpif);
682             } else {
683                 dpctl_error(dpctl_p, error, "opening datapath %s failed",
684                             name);
685                 lasterror = error;
686             }
687         }
688     } else {
689         lasterror = dps_for_each(dpctl_p, show_dpif);
690     }
691
692     return lasterror;
693 }
694
695 static void
696 dump_cb(struct dpif *dpif, struct dpctl_params *dpctl_p)
697 {
698     dpctl_print(dpctl_p, "%s\n", dpif_name(dpif));
699 }
700
701 static int
702 dpctl_dump_dps(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
703                struct dpctl_params *dpctl_p)
704 {
705     return dps_for_each(dpctl_p, dump_cb);
706 }
707
708 static void
709 format_dpif_flow(struct ds *ds, const struct dpif_flow *f, struct hmap *ports,
710                  struct dpctl_params *dpctl_p)
711 {
712     if (dpctl_p->verbosity) {
713         if (f->ufid_present) {
714             odp_format_ufid(&f->ufid, ds);
715             ds_put_cstr(ds, ", ");
716         } else {
717             ds_put_cstr(ds, "ufid:<empty>, ");
718         }
719     }
720     odp_flow_format(f->key, f->key_len, f->mask, f->mask_len, ports, ds,
721                     dpctl_p->verbosity);
722     ds_put_cstr(ds, ", ");
723
724     dpif_flow_stats_format(&f->stats, ds);
725     ds_put_cstr(ds, ", actions:");
726     format_odp_actions(ds, f->actions, f->actions_len);
727 }
728
729 static int
730 dpctl_dump_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
731 {
732     struct dpif *dpif;
733     struct ds ds;
734     char *name;
735
736     char *filter = NULL;
737     struct flow flow_filter;
738     struct flow_wildcards wc_filter;
739
740     struct dpif_port_dump port_dump;
741     struct dpif_port dpif_port;
742     struct hmap portno_names;
743     struct simap names_portno;
744
745     struct dpif_flow_dump_thread *flow_dump_thread;
746     struct dpif_flow_dump *flow_dump;
747     struct dpif_flow f;
748     int pmd_id = PMD_ID_NULL;
749     int error;
750
751     if (argc > 1 && !strncmp(argv[argc - 1], "filter=", 7)) {
752         filter = xstrdup(argv[--argc] + 7);
753     }
754     name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
755     if (!name) {
756         error = EINVAL;
757         goto out_freefilter;
758     }
759
760     error = parsed_dpif_open(name, false, &dpif);
761     free(name);
762     if (error) {
763         dpctl_error(dpctl_p, error, "opening datapath");
764         goto out_freefilter;
765     }
766
767
768     hmap_init(&portno_names);
769     simap_init(&names_portno);
770     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
771         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
772         simap_put(&names_portno, dpif_port.name,
773                   odp_to_u32(dpif_port.port_no));
774     }
775
776     if (filter) {
777         char *err = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks,
778                                          filter, &names_portno);
779         if (err) {
780             dpctl_error(dpctl_p, 0, "Failed to parse filter (%s)", err);
781             error = EINVAL;
782             goto out_dpifclose;
783         }
784     }
785
786     ds_init(&ds);
787     flow_dump = dpif_flow_dump_create(dpif, false);
788     flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
789     while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
790         if (filter) {
791             struct flow flow;
792             struct flow_wildcards wc;
793             struct match match, match_filter;
794             struct minimatch minimatch;
795
796             odp_flow_key_to_flow(f.key, f.key_len, &flow);
797             odp_flow_key_to_mask(f.mask, f.mask_len, &wc.masks, &flow);
798             match_init(&match, &flow, &wc);
799
800             match_init(&match_filter, &flow_filter, &wc);
801             match_init(&match_filter, &match_filter.flow, &wc_filter);
802             minimatch_init(&minimatch, &match_filter);
803
804             if (!minimatch_matches_flow(&minimatch, &match.flow)) {
805                 minimatch_destroy(&minimatch);
806                 continue;
807             }
808             minimatch_destroy(&minimatch);
809         }
810         ds_clear(&ds);
811         /* If 'pmd_id' is specified, overlapping flows could be dumped from
812          * different pmd threads.  So, separates dumps from different pmds
813          * by printing a title line. */
814         if (pmd_id != f.pmd_id) {
815             if (f.pmd_id == NON_PMD_CORE_ID) {
816                 ds_put_format(&ds, "flow-dump from non-dpdk interfaces:\n");
817             } else {
818                 ds_put_format(&ds, "flow-dump from pmd on cpu core: %d\n",
819                               f.pmd_id);
820             }
821             pmd_id = f.pmd_id;
822         }
823         format_dpif_flow(&ds, &f, &portno_names, dpctl_p);
824         dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
825     }
826     dpif_flow_dump_thread_destroy(flow_dump_thread);
827     error = dpif_flow_dump_destroy(flow_dump);
828
829     if (error) {
830         dpctl_error(dpctl_p, error, "Failed to dump flows from datapath");
831     }
832     ds_destroy(&ds);
833
834 out_dpifclose:
835     odp_portno_names_destroy(&portno_names);
836     simap_destroy(&names_portno);
837     hmap_destroy(&portno_names);
838     dpif_close(dpif);
839 out_freefilter:
840     free(filter);
841     return error;
842 }
843
844 /* Extracts the in_port from the parsed keys, and returns the reference
845  * to the 'struct netdev *' of the dpif port.  On error, returns NULL.
846  * Users must call 'netdev_close()' after finish using the returned
847  * reference. */
848 static struct netdev *
849 get_in_port_netdev_from_key(struct dpif *dpif, const struct ofpbuf *key)
850 {
851     const struct nlattr *in_port_nla;
852     struct netdev *dev = NULL;
853
854     in_port_nla = nl_attr_find(key, 0, OVS_KEY_ATTR_IN_PORT);
855     if (in_port_nla) {
856         struct dpif_port dpif_port;
857         odp_port_t port_no;
858         int error;
859
860         port_no = ODP_PORT_C(nl_attr_get_u32(in_port_nla));
861         error = dpif_port_query_by_number(dpif, port_no, &dpif_port);
862         if (error) {
863             goto out;
864         }
865
866         netdev_open(dpif_port.name, dpif_port.type, &dev);
867         dpif_port_destroy(&dpif_port);
868     }
869
870 out:
871     return dev;
872 }
873
874 static int
875 dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
876                struct dpctl_params *dpctl_p)
877 {
878     const char *key_s = argv[argc - 2];
879     const char *actions_s = argv[argc - 1];
880     struct netdev *in_port_netdev = NULL;
881     struct dpif_flow_stats stats;
882     struct dpif_port dpif_port;
883     struct dpif_port_dump port_dump;
884     struct ofpbuf actions;
885     struct ofpbuf key;
886     struct ofpbuf mask;
887     struct dpif *dpif;
888     ovs_u128 ufid;
889     bool ufid_present;
890     char *dp_name;
891     struct simap port_names;
892     int n, error;
893
894     dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
895     if (!dp_name) {
896         return EINVAL;
897     }
898     error = parsed_dpif_open(dp_name, false, &dpif);
899     free(dp_name);
900     if (error) {
901         dpctl_error(dpctl_p, error, "opening datapath");
902         return error;
903     }
904
905     ufid_present = false;
906     n = odp_ufid_from_string(key_s, &ufid);
907     if (n < 0) {
908         dpctl_error(dpctl_p, -n, "parsing flow ufid");
909         return -n;
910     } else if (n) {
911         key_s += n;
912         ufid_present = true;
913     }
914
915     simap_init(&port_names);
916     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
917         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
918     }
919
920     ofpbuf_init(&key, 0);
921     ofpbuf_init(&mask, 0);
922     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
923     simap_destroy(&port_names);
924     if (error) {
925         dpctl_error(dpctl_p, error, "parsing flow key");
926         goto out_freekeymask;
927     }
928
929     ofpbuf_init(&actions, 0);
930     error = odp_actions_from_string(actions_s, NULL, &actions);
931     if (error) {
932         dpctl_error(dpctl_p, error, "parsing actions");
933         goto out_freeactions;
934     }
935
936     /* For DPDK interface, applies the operation to all pmd threads
937      * on the same numa node. */
938     in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
939     if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
940         int numa_id;
941
942         numa_id = netdev_get_numa_id(in_port_netdev);
943         if (ovs_numa_numa_id_is_valid(numa_id)) {
944             struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
945             struct ovs_numa_info *iter;
946
947             FOR_EACH_CORE_ON_NUMA (iter, dump) {
948                 if (ovs_numa_core_is_pinned(iter->core_id)) {
949                     error = dpif_flow_put(dpif, flags,
950                                           key.data, key.size,
951                                           mask.size == 0 ? NULL : mask.data,
952                                           mask.size, actions.data,
953                                           actions.size, ufid_present ? &ufid : NULL,
954                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
955                 }
956             }
957             ovs_numa_dump_destroy(dump);
958         } else {
959             error = EINVAL;
960         }
961     } else {
962         error = dpif_flow_put(dpif, flags,
963                               key.data, key.size,
964                               mask.size == 0 ? NULL : mask.data,
965                               mask.size, actions.data,
966                               actions.size, ufid_present ? &ufid : NULL,
967                               PMD_ID_NULL, dpctl_p->print_statistics ? &stats : NULL);
968     }
969     if (error) {
970         dpctl_error(dpctl_p, error, "updating flow table");
971         goto out_freeactions;
972     }
973
974     if (dpctl_p->print_statistics) {
975         struct ds s;
976
977         ds_init(&s);
978         dpif_flow_stats_format(&stats, &s);
979         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
980         ds_destroy(&s);
981     }
982
983 out_freeactions:
984     ofpbuf_uninit(&actions);
985 out_freekeymask:
986     ofpbuf_uninit(&mask);
987     ofpbuf_uninit(&key);
988     dpif_close(dpif);
989     netdev_close(in_port_netdev);
990     return error;
991 }
992
993 static int
994 dpctl_add_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
995 {
996     return dpctl_put_flow(argc, argv, DPIF_FP_CREATE, dpctl_p);
997 }
998
999 static int
1000 dpctl_mod_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1001 {
1002     enum dpif_flow_put_flags flags;
1003
1004     flags = DPIF_FP_MODIFY;
1005     if (dpctl_p->may_create) {
1006         flags |= DPIF_FP_CREATE;
1007     }
1008     if (dpctl_p->zero_statistics) {
1009         flags |= DPIF_FP_ZERO_STATS;
1010     }
1011
1012     return dpctl_put_flow(argc, argv, flags, dpctl_p);
1013 }
1014
1015 static int
1016 dpctl_get_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1017 {
1018     const char *key_s = argv[argc - 1];
1019     struct dpif_flow flow;
1020     struct dpif_port dpif_port;
1021     struct dpif_port_dump port_dump;
1022     struct dpif *dpif;
1023     char *dp_name;
1024     struct hmap portno_names;
1025     ovs_u128 ufid;
1026     struct ofpbuf buf;
1027     uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1028     struct ds ds;
1029     int n, error;
1030
1031     dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1032     if (!dp_name) {
1033         return EINVAL;
1034     }
1035     error = parsed_dpif_open(dp_name, false, &dpif);
1036     free(dp_name);
1037     if (error) {
1038         dpctl_error(dpctl_p, error, "opening datapath");
1039         return error;
1040     }
1041
1042     ofpbuf_use_stub(&buf, &stub, sizeof stub);
1043     hmap_init(&portno_names);
1044     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1045         odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
1046     }
1047
1048     n = odp_ufid_from_string(key_s, &ufid);
1049     if (n <= 0) {
1050         dpctl_error(dpctl_p, -n, "parsing flow ufid");
1051         goto out;
1052     }
1053
1054     /* Does not work for DPDK, since do not know which 'pmd' to apply the
1055      * operation.  So, just uses PMD_ID_NULL. */
1056     error = dpif_flow_get(dpif, NULL, 0, &ufid, PMD_ID_NULL, &buf, &flow);
1057     if (error) {
1058         dpctl_error(dpctl_p, error, "getting flow");
1059         goto out;
1060     }
1061
1062     ds_init(&ds);
1063     format_dpif_flow(&ds, &flow, &portno_names, dpctl_p);
1064     dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
1065     ds_destroy(&ds);
1066
1067 out:
1068     odp_portno_names_destroy(&portno_names);
1069     hmap_destroy(&portno_names);
1070     ofpbuf_uninit(&buf);
1071     dpif_close(dpif);
1072     return error;
1073 }
1074
1075 static int
1076 dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1077 {
1078     const char *key_s = argv[argc - 1];
1079     struct netdev *in_port_netdev = NULL;
1080     struct dpif_flow_stats stats;
1081     struct dpif_port dpif_port;
1082     struct dpif_port_dump port_dump;
1083     struct ofpbuf key;
1084     struct ofpbuf mask; /* To be ignored. */
1085     struct dpif *dpif;
1086     ovs_u128 ufid;
1087     bool ufid_present;
1088     char *dp_name;
1089     struct simap port_names;
1090     int n, error;
1091
1092     dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1093     if (!dp_name) {
1094         return EINVAL;
1095     }
1096     error = parsed_dpif_open(dp_name, false, &dpif);
1097     free(dp_name);
1098     if (error) {
1099         dpctl_error(dpctl_p, error, "opening datapath");
1100         return error;
1101     }
1102
1103     ufid_present = false;
1104     n = odp_ufid_from_string(key_s, &ufid);
1105     if (n < 0) {
1106         dpctl_error(dpctl_p, -n, "parsing flow ufid");
1107         return -n;
1108     } else if (n) {
1109         key_s += n;
1110         ufid_present = true;
1111     }
1112
1113     simap_init(&port_names);
1114     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
1115         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
1116     }
1117
1118     ofpbuf_init(&key, 0);
1119     ofpbuf_init(&mask, 0);
1120
1121     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
1122     if (error) {
1123         dpctl_error(dpctl_p, error, "parsing flow key");
1124         goto out;
1125     }
1126
1127     /* For DPDK interface, applies the operation to all pmd threads
1128      * on the same numa node. */
1129     in_port_netdev = get_in_port_netdev_from_key(dpif, &key);
1130     if (in_port_netdev && netdev_is_pmd(in_port_netdev)) {
1131         int numa_id;
1132
1133         numa_id = netdev_get_numa_id(in_port_netdev);
1134         if (ovs_numa_numa_id_is_valid(numa_id)) {
1135             struct ovs_numa_dump *dump = ovs_numa_dump_cores_on_numa(numa_id);
1136             struct ovs_numa_info *iter;
1137
1138             FOR_EACH_CORE_ON_NUMA (iter, dump) {
1139                 if (ovs_numa_core_is_pinned(iter->core_id)) {
1140                     error = dpif_flow_del(dpif, key.data,
1141                                           key.size, ufid_present ? &ufid : NULL,
1142                                           iter->core_id, dpctl_p->print_statistics ? &stats : NULL);
1143                 }
1144             }
1145             ovs_numa_dump_destroy(dump);
1146         } else {
1147             error = EINVAL;
1148         }
1149     } else {
1150         error = dpif_flow_del(dpif, key.data, key.size,
1151                               ufid_present ? &ufid : NULL, PMD_ID_NULL,
1152                               dpctl_p->print_statistics ? &stats : NULL);
1153     }
1154     if (error) {
1155         dpctl_error(dpctl_p, error, "deleting flow");
1156         if (error == ENOENT && !ufid_present) {
1157             struct ds s;
1158
1159             ds_init(&s);
1160             ds_put_format(&s, "Perhaps you need to specify a UFID?");
1161             dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1162             ds_destroy(&s);
1163         }
1164         goto out;
1165     }
1166
1167     if (dpctl_p->print_statistics) {
1168         struct ds s;
1169
1170         ds_init(&s);
1171         dpif_flow_stats_format(&stats, &s);
1172         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1173         ds_destroy(&s);
1174     }
1175
1176 out:
1177     ofpbuf_uninit(&mask);
1178     ofpbuf_uninit(&key);
1179     simap_destroy(&port_names);
1180     dpif_close(dpif);
1181     netdev_close(in_port_netdev);
1182     return error;
1183 }
1184
1185 static int
1186 dpctl_del_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1187 {
1188     struct dpif *dpif;
1189     char *name;
1190     int error;
1191
1192     name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
1193     if (!name) {
1194         return EINVAL;
1195     }
1196     error = parsed_dpif_open(name, false, &dpif);
1197     free(name);
1198     if (error) {
1199         dpctl_error(dpctl_p, error, "opening datapath");
1200         return error;
1201     }
1202
1203     error = dpif_flow_flush(dpif);
1204     if (error) {
1205         dpctl_error(dpctl_p, error, "deleting all flows");
1206     }
1207     dpif_close(dpif);
1208     return error;
1209 }
1210
1211 static int
1212 dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1213            struct dpctl_params *dpctl_p)
1214 {
1215     if (dpctl_p->usage) {
1216         dpctl_p->usage(dpctl_p->aux);
1217     }
1218
1219     return 0;
1220 }
1221
1222 static int
1223 dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1224                     struct dpctl_params *dpctl_p)
1225 {
1226     struct ds ds = DS_EMPTY_INITIALIZER;
1227     const struct dpctl_command *commands = get_all_dpctl_commands();
1228
1229     ds_put_cstr(&ds, "The available commands are:\n");
1230     for (; commands->name; commands++) {
1231         const struct dpctl_command *c = commands;
1232
1233         ds_put_format(&ds, "  %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "",
1234                       c->name, c->usage);
1235     }
1236     dpctl_puts(dpctl_p, false, ds.string);
1237     ds_destroy(&ds);
1238
1239     return 0;
1240 }
1241 \f
1242 /* Undocumented commands for unit testing. */
1243
1244 static int
1245 dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p)
1246 {
1247     int i, error = 0;
1248
1249     for (i = 1; i < argc; i++) {
1250         struct ofpbuf actions;
1251         struct ds s;
1252
1253         ofpbuf_init(&actions, 0);
1254         error = odp_actions_from_string(argv[i], NULL, &actions);
1255
1256         if (error) {
1257             ofpbuf_uninit(&actions);
1258             dpctl_error(dpctl_p, error, "odp_actions_from_string");
1259             return error;
1260         }
1261
1262         ds_init(&s);
1263         format_odp_actions(&s, actions.data, actions.size);
1264         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1265         ds_destroy(&s);
1266
1267         ofpbuf_uninit(&actions);
1268     }
1269
1270     return error;
1271 }
1272
1273 struct actions_for_flow {
1274     struct hmap_node hmap_node;
1275     struct flow flow;
1276     struct ofpbuf actions;
1277 };
1278
1279 static struct actions_for_flow *
1280 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1281 {
1282     uint32_t hash = flow_hash(flow, 0);
1283     struct actions_for_flow *af;
1284
1285     HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1286         if (flow_equal(&af->flow, flow)) {
1287             return af;
1288         }
1289     }
1290
1291     af = xmalloc(sizeof *af);
1292     af->flow = *flow;
1293     ofpbuf_init(&af->actions, 0);
1294     hmap_insert(actions_per_flow, &af->hmap_node, hash);
1295     return af;
1296 }
1297
1298 static int
1299 compare_actions_for_flow(const void *a_, const void *b_)
1300 {
1301     struct actions_for_flow *const *a = a_;
1302     struct actions_for_flow *const *b = b_;
1303
1304     return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1305 }
1306
1307 static int
1308 compare_output_actions(const void *a_, const void *b_)
1309 {
1310     const struct nlattr *a = a_;
1311     const struct nlattr *b = b_;
1312     uint32_t a_port = nl_attr_get_u32(a);
1313     uint32_t b_port = nl_attr_get_u32(b);
1314
1315     return a_port < b_port ? -1 : a_port > b_port;
1316 }
1317
1318 static void
1319 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1320 {
1321     size_t bytes = (uint8_t *) end - (uint8_t *) first;
1322     size_t n = bytes / NL_A_U32_SIZE;
1323
1324     ovs_assert(bytes % NL_A_U32_SIZE == 0);
1325     qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1326 }
1327
1328 static void
1329 sort_output_actions(struct nlattr *actions, size_t length)
1330 {
1331     struct nlattr *first_output = NULL;
1332     struct nlattr *a;
1333     int left;
1334
1335     NL_ATTR_FOR_EACH (a, left, actions, length) {
1336         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1337             if (!first_output) {
1338                 first_output = a;
1339             }
1340         } else {
1341             if (first_output) {
1342                 sort_output_actions__(first_output, a);
1343                 first_output = NULL;
1344             }
1345         }
1346     }
1347     if (first_output) {
1348         uint8_t *end = (uint8_t *) actions + length;
1349         sort_output_actions__(first_output,
1350                               ALIGNED_CAST(struct nlattr *, end));
1351     }
1352 }
1353
1354 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1355  * have the syntax used by "ovs-dpctl dump-flows".
1356  *
1357  * This command prints ACTIONS in a format that shows what happens for each
1358  * VLAN, independent of the order of the ACTIONS.  For example, there is more
1359  * than one way to output a packet on VLANs 9 and 11, but this command will
1360  * print the same output for any form.
1361  *
1362  * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1363  * so far the implementation only covers VLANs. */
1364 static int
1365 dpctl_normalize_actions(int argc, const char *argv[],
1366                         struct dpctl_params *dpctl_p)
1367 {
1368     struct simap port_names;
1369     struct ofpbuf keybuf;
1370     struct flow flow;
1371     struct ofpbuf odp_actions;
1372     struct hmap actions_per_flow;
1373     struct actions_for_flow **afs;
1374     struct actions_for_flow *af;
1375     struct nlattr *a;
1376     size_t n_afs;
1377     struct ds s;
1378     int left;
1379     int i, error;
1380
1381     ds_init(&s);
1382
1383     simap_init(&port_names);
1384     for (i = 3; i < argc; i++) {
1385         char name[16];
1386         int number;
1387
1388         if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1389             uintptr_t n = number;
1390             simap_put(&port_names, name, n);
1391         } else {
1392             dpctl_error(dpctl_p, 0, "%s: expected NAME=NUMBER", argv[i]);
1393             error = EINVAL;
1394             goto out;
1395         }
1396     }
1397
1398     /* Parse flow key. */
1399     ofpbuf_init(&keybuf, 0);
1400     error = odp_flow_from_string(argv[1], &port_names, &keybuf, NULL);
1401     if (error) {
1402         dpctl_error(dpctl_p, error, "odp_flow_key_from_string");
1403         goto out_freekeybuf;
1404     }
1405
1406     ds_clear(&s);
1407     odp_flow_format(keybuf.data, keybuf.size, NULL, 0, NULL,
1408                     &s, dpctl_p->verbosity);
1409     dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s));
1410
1411     error = odp_flow_key_to_flow(keybuf.data, keybuf.size, &flow);
1412     if (error) {
1413         dpctl_error(dpctl_p, error, "odp_flow_key_to_flow");
1414         goto out_freekeybuf;
1415     }
1416
1417     /* Parse actions. */
1418     ofpbuf_init(&odp_actions, 0);
1419     error = odp_actions_from_string(argv[2], &port_names, &odp_actions);
1420     if (error) {
1421         dpctl_error(dpctl_p, error, "odp_actions_from_string");
1422         goto out_freeactions;
1423     }
1424
1425     if (dpctl_p->verbosity) {
1426         ds_clear(&s);
1427         format_odp_actions(&s, odp_actions.data, odp_actions.size);
1428         dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s));
1429     }
1430
1431     hmap_init(&actions_per_flow);
1432     NL_ATTR_FOR_EACH (a, left, odp_actions.data, odp_actions.size) {
1433         const struct ovs_action_push_vlan *push;
1434         switch(nl_attr_type(a)) {
1435         case OVS_ACTION_ATTR_POP_VLAN:
1436             flow.vlan_tci = htons(0);
1437             continue;
1438
1439         case OVS_ACTION_ATTR_PUSH_VLAN:
1440             push = nl_attr_get_unspec(a, sizeof *push);
1441             flow.vlan_tci = push->vlan_tci;
1442             continue;
1443         }
1444
1445         af = get_actions_for_flow(&actions_per_flow, &flow);
1446         nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1447                           nl_attr_get(a), nl_attr_get_size(a));
1448     }
1449
1450     n_afs = hmap_count(&actions_per_flow);
1451     afs = xmalloc(n_afs * sizeof *afs);
1452     i = 0;
1453     HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1454         afs[i++] = af;
1455     }
1456
1457     ovs_assert(i == n_afs);
1458     hmap_destroy(&actions_per_flow);
1459
1460     qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1461
1462     for (i = 0; i < n_afs; i++) {
1463         struct actions_for_flow *af = afs[i];
1464
1465         sort_output_actions(af->actions.data, af->actions.size);
1466
1467         if (af->flow.vlan_tci != htons(0)) {
1468             dpctl_print(dpctl_p, "vlan(vid=%"PRIu16",pcp=%d): ",
1469                         vlan_tci_to_vid(af->flow.vlan_tci),
1470                         vlan_tci_to_pcp(af->flow.vlan_tci));
1471         } else {
1472             dpctl_print(dpctl_p, "no vlan: ");
1473         }
1474
1475         if (eth_type_mpls(af->flow.dl_type)) {
1476             dpctl_print(dpctl_p, "mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1477                         mpls_lse_to_label(af->flow.mpls_lse[0]),
1478                         mpls_lse_to_tc(af->flow.mpls_lse[0]),
1479                         mpls_lse_to_ttl(af->flow.mpls_lse[0]));
1480         } else {
1481             dpctl_print(dpctl_p, "no mpls: ");
1482         }
1483
1484         ds_clear(&s);
1485         format_odp_actions(&s, af->actions.data, af->actions.size);
1486         dpctl_puts(dpctl_p, false, ds_cstr(&s));
1487
1488         ofpbuf_uninit(&af->actions);
1489         free(af);
1490     }
1491     free(afs);
1492
1493
1494 out_freeactions:
1495     ofpbuf_uninit(&odp_actions);
1496 out_freekeybuf:
1497     ofpbuf_uninit(&keybuf);
1498 out:
1499     simap_destroy(&port_names);
1500     ds_destroy(&s);
1501
1502     return error;
1503 }
1504 \f
1505 static const struct dpctl_command all_commands[] = {
1506     { "add-dp", "add-dp dp [iface...]", 1, INT_MAX, dpctl_add_dp },
1507     { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp },
1508     { "add-if", "add-if dp iface...", 2, INT_MAX, dpctl_add_if },
1509     { "del-if", "del-if dp iface...", 2, INT_MAX, dpctl_del_if },
1510     { "set-if", "set-if dp iface...", 2, INT_MAX, dpctl_set_if },
1511     { "dump-dps", "", 0, 0, dpctl_dump_dps },
1512     { "show", "[dp...]", 0, INT_MAX, dpctl_show },
1513     { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows },
1514     { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow },
1515     { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow },
1516     { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow },
1517     { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow },
1518     { "del-flows", "[dp]", 0, 1, dpctl_del_flows },
1519     { "help", "", 0, INT_MAX, dpctl_help },
1520     { "list-commands", "", 0, INT_MAX, dpctl_list_commands },
1521
1522     /* Undocumented commands for testing. */
1523     { "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions },
1524     { "normalize-actions", "actions", 2, INT_MAX, dpctl_normalize_actions },
1525
1526     { NULL, NULL, 0, 0, NULL },
1527 };
1528
1529 static const struct dpctl_command *get_all_dpctl_commands(void)
1530 {
1531     return all_commands;
1532 }
1533
1534 /* Runs the command designated by argv[0] within the command table specified by
1535  * 'commands', which must be terminated by a command whose 'name' member is a
1536  * null pointer. */
1537 int
1538 dpctl_run_command(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1539 {
1540     const struct dpctl_command *p;
1541
1542     if (argc < 1) {
1543         dpctl_error(dpctl_p, 0, "missing command name; use --help for help");
1544         return EINVAL;
1545     }
1546
1547     for (p = all_commands; p->name != NULL; p++) {
1548         if (!strcmp(p->name, argv[0])) {
1549             int n_arg = argc - 1;
1550             if (n_arg < p->min_args) {
1551                 dpctl_error(dpctl_p, 0,
1552                             "'%s' command requires at least %d arguments",
1553                             p->name, p->min_args);
1554                 return EINVAL;
1555             } else if (n_arg > p->max_args) {
1556                 dpctl_error(dpctl_p, 0,
1557                             "'%s' command takes at most %d arguments",
1558                             p->name, p->max_args);
1559                 return EINVAL;
1560             } else {
1561                 return p->handler(argc, argv, dpctl_p);
1562             }
1563         }
1564     }
1565
1566     dpctl_error(dpctl_p, 0, "unknown command '%s'; use --help for help",
1567                 argv[0]);
1568     return EINVAL;
1569 }
1570 \f
1571 static void
1572 dpctl_unixctl_print(void *userdata, bool error OVS_UNUSED, const char *msg)
1573 {
1574     struct ds *ds = userdata;
1575     ds_put_cstr(ds, msg);
1576 }
1577
1578 static void
1579 dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
1580                       void *aux)
1581 {
1582     struct ds ds = DS_EMPTY_INITIALIZER;
1583     struct dpctl_params dpctl_p;
1584     bool error = false;
1585
1586     dpctl_command_handler *handler = (dpctl_command_handler *) aux;
1587
1588     dpctl_p.print_statistics = false;
1589     dpctl_p.zero_statistics = false;
1590     dpctl_p.may_create = false;
1591     dpctl_p.verbosity = 0;
1592
1593     /* Parse options (like getopt). Unfortunately it does
1594      * not seem a good idea to call getopt_long() here, since it uses global
1595      * variables */
1596     while (argc > 1 && !error) {
1597         const char *arg = argv[1];
1598         if (!strncmp(arg, "--", 2)) {
1599             /* Long option */
1600             if (!strcmp(arg, "--statistics")) {
1601                 dpctl_p.print_statistics = true;
1602             } else if (!strcmp(arg, "--clear")) {
1603                 dpctl_p.zero_statistics = true;
1604             } else if (!strcmp(arg, "--may-create")) {
1605                 dpctl_p.may_create = true;
1606             } else if (!strcmp(arg, "--more")) {
1607                 dpctl_p.verbosity++;
1608             } else {
1609                 ds_put_format(&ds, "Unrecognized option %s", argv[1]);
1610                 error = true;
1611             }
1612         } else if (arg[0] == '-' && arg[1] != '\0') {
1613             /* Short option[s] */
1614             const char *opt = &arg[1];
1615
1616             while (*opt && !error) {
1617                 switch (*opt) {
1618                 case 'm':
1619                     dpctl_p.verbosity++;
1620                     break;
1621                 case 's':
1622                     dpctl_p.print_statistics = true;
1623                     break;
1624                 default:
1625                     ds_put_format(&ds, "Unrecognized option -%c", *opt);
1626                     error = true;
1627                     break;
1628                 }
1629                 opt++;
1630             }
1631         } else {
1632             /* Doesn't start with -, not an option */
1633             break;
1634         }
1635
1636         if (error) {
1637             break;
1638         }
1639         argv++;
1640         argc--;
1641     }
1642
1643     if (!error) {
1644         dpctl_p.is_appctl = true;
1645         dpctl_p.output = dpctl_unixctl_print;
1646         dpctl_p.aux = &ds;
1647
1648         error = handler(argc, argv, &dpctl_p) != 0;
1649     }
1650
1651     if (error) {
1652         unixctl_command_reply_error(conn, ds_cstr(&ds));
1653     } else {
1654         unixctl_command_reply(conn, ds_cstr(&ds));
1655     }
1656
1657     ds_destroy(&ds);
1658 }
1659
1660 void
1661 dpctl_unixctl_register(void)
1662 {
1663     const struct dpctl_command *p;
1664
1665     for (p = all_commands; p->name != NULL; p++) {
1666         char *cmd_name = xasprintf("dpctl/%s", p->name);
1667         unixctl_command_register(cmd_name, "", p->min_args, p->max_args,
1668                                  dpctl_unixctl_handler, p->handler);
1669         free(cmd_name);
1670     }
1671 }