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