2d9144b20eb80b681f64ab000333941291609c85
[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);
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         odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
775                         &portno_names, &ds, dpctl_p->verbosity);
776         ds_put_cstr(&ds, ", ");
777
778         dpif_flow_stats_format(&f.stats, &ds);
779         ds_put_cstr(&ds, ", actions:");
780         format_odp_actions(&ds, f.actions, f.actions_len);
781         dpctl_print(dpctl_p, "%s\n", ds_cstr(&ds));
782     }
783     dpif_flow_dump_thread_destroy(flow_dump_thread);
784     error = dpif_flow_dump_destroy(flow_dump);
785
786     if (error) {
787         dpctl_error(dpctl_p, error, "Failed to dump flows from datapath");
788     }
789     ds_destroy(&ds);
790
791 out_dpifclose:
792     odp_portno_names_destroy(&portno_names);
793     simap_destroy(&names_portno);
794     hmap_destroy(&portno_names);
795     dpif_close(dpif);
796 out_freefilter:
797     free(filter);
798     return error;
799 }
800
801 static int
802 dpctl_put_flow(int argc, const char *argv[], enum dpif_flow_put_flags flags,
803                struct dpctl_params *dpctl_p)
804 {
805     const char *key_s = argv[argc - 2];
806     const char *actions_s = argv[argc - 1];
807     struct dpif_flow_stats stats;
808     struct dpif_port dpif_port;
809     struct dpif_port_dump port_dump;
810     struct ofpbuf actions;
811     struct ofpbuf key;
812     struct ofpbuf mask;
813     struct dpif *dpif;
814     char *dp_name;
815     struct simap port_names;
816     int error;
817
818     dp_name = argc == 4 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
819     if (!dp_name) {
820         return EINVAL;
821     }
822     error = parsed_dpif_open(dp_name, false, &dpif);
823     free(dp_name);
824     if (error) {
825         dpctl_error(dpctl_p, error, "opening datapath");
826         return error;
827     }
828
829
830     simap_init(&port_names);
831     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
832         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
833     }
834
835     ofpbuf_init(&key, 0);
836     ofpbuf_init(&mask, 0);
837     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
838     simap_destroy(&port_names);
839     if (error) {
840         dpctl_error(dpctl_p, error, "parsing flow key");
841         goto out_freekeymask;
842     }
843
844     ofpbuf_init(&actions, 0);
845     error = odp_actions_from_string(actions_s, NULL, &actions);
846     if (error) {
847         dpctl_error(dpctl_p, error, "parsing actions");
848         goto out_freeactions;
849     }
850     error = dpif_flow_put(dpif, flags,
851                           ofpbuf_data(&key), ofpbuf_size(&key),
852                           ofpbuf_size(&mask) == 0 ? NULL : ofpbuf_data(&mask),
853                           ofpbuf_size(&mask),
854                           ofpbuf_data(&actions), ofpbuf_size(&actions),
855                           dpctl_p->print_statistics ? &stats : NULL);
856     if (error) {
857         dpctl_error(dpctl_p, error, "updating flow table");
858         goto out_freeactions;
859     }
860
861     if (dpctl_p->print_statistics) {
862         struct ds s;
863
864         ds_init(&s);
865         dpif_flow_stats_format(&stats, &s);
866         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
867         ds_destroy(&s);
868     }
869
870 out_freeactions:
871     ofpbuf_uninit(&actions);
872 out_freekeymask:
873     ofpbuf_uninit(&mask);
874     ofpbuf_uninit(&key);
875     dpif_close(dpif);
876     return error;
877 }
878
879 static int
880 dpctl_add_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
881 {
882     return dpctl_put_flow(argc, argv, DPIF_FP_CREATE, dpctl_p);
883 }
884
885 static int
886 dpctl_mod_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
887 {
888     enum dpif_flow_put_flags flags;
889
890     flags = DPIF_FP_MODIFY;
891     if (dpctl_p->may_create) {
892         flags |= DPIF_FP_CREATE;
893     }
894     if (dpctl_p->zero_statistics) {
895         flags |= DPIF_FP_ZERO_STATS;
896     }
897
898     return dpctl_put_flow(argc, argv, flags, dpctl_p);
899 }
900
901 static int
902 dpctl_del_flow(int argc, const char *argv[], struct dpctl_params *dpctl_p)
903 {
904     const char *key_s = argv[argc - 1];
905     struct dpif_flow_stats stats;
906     struct dpif_port dpif_port;
907     struct dpif_port_dump port_dump;
908     struct ofpbuf key;
909     struct ofpbuf mask; /* To be ignored. */
910     struct dpif *dpif;
911     char *dp_name;
912     struct simap port_names;
913     int error;
914
915     dp_name = argc == 3 ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
916     if (!dp_name) {
917         return EINVAL;
918     }
919     error = parsed_dpif_open(dp_name, false, &dpif);
920     free(dp_name);
921     if (error) {
922         dpctl_error(dpctl_p, error, "opening datapath");
923         return error;
924     }
925
926     simap_init(&port_names);
927     DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, dpif) {
928         simap_put(&port_names, dpif_port.name, odp_to_u32(dpif_port.port_no));
929     }
930
931     ofpbuf_init(&key, 0);
932     ofpbuf_init(&mask, 0);
933
934     error = odp_flow_from_string(key_s, &port_names, &key, &mask);
935     if (error) {
936         dpctl_error(dpctl_p, error, "parsing flow key");
937         goto out;
938     }
939
940     error = dpif_flow_del(dpif,
941                           ofpbuf_data(&key), ofpbuf_size(&key),
942                           dpctl_p->print_statistics ? &stats : NULL);
943     if (error) {
944         dpctl_error(dpctl_p, error, "deleting flow");
945         goto out;
946     }
947
948     if (dpctl_p->print_statistics) {
949         struct ds s;
950
951         ds_init(&s);
952         dpif_flow_stats_format(&stats, &s);
953         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
954         ds_destroy(&s);
955     }
956
957 out:
958     ofpbuf_uninit(&mask);
959     ofpbuf_uninit(&key);
960     simap_destroy(&port_names);
961     dpif_close(dpif);
962     return error;
963 }
964
965 static int
966 dpctl_del_flows(int argc, const char *argv[], struct dpctl_params *dpctl_p)
967 {
968     struct dpif *dpif;
969     char *name;
970     int error;
971
972     name = (argc == 2) ? xstrdup(argv[1]) : get_one_dp(dpctl_p);
973     if (!name) {
974         return EINVAL;
975     }
976     error = parsed_dpif_open(name, false, &dpif);
977     free(name);
978     if (error) {
979         dpctl_error(dpctl_p, error, "opening datapath");
980         return error;
981     }
982
983     error = dpif_flow_flush(dpif);
984     if (error) {
985         dpctl_error(dpctl_p, error, "deleting all flows");
986     }
987     dpif_close(dpif);
988     return error;
989 }
990
991 static int
992 dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
993            struct dpctl_params *dpctl_p)
994 {
995     if (dpctl_p->usage) {
996         dpctl_p->usage(dpctl_p->aux);
997     }
998
999     return 0;
1000 }
1001
1002 static int
1003 dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
1004                     struct dpctl_params *dpctl_p)
1005 {
1006     struct ds ds = DS_EMPTY_INITIALIZER;
1007     const struct dpctl_command *commands = get_all_dpctl_commands();
1008
1009     ds_put_cstr(&ds, "The available commands are:\n");
1010     for (; commands->name; commands++) {
1011         const struct dpctl_command *c = commands;
1012
1013         ds_put_format(&ds, "  %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "",
1014                       c->name, c->usage);
1015     }
1016     dpctl_puts(dpctl_p, false, ds.string);
1017     ds_destroy(&ds);
1018
1019     return 0;
1020 }
1021 \f
1022 /* Undocumented commands for unit testing. */
1023
1024 static int
1025 dpctl_parse_actions(int argc, const char *argv[], struct dpctl_params* dpctl_p)
1026 {
1027     int i, error = 0;
1028
1029     for (i = 1; i < argc; i++) {
1030         struct ofpbuf actions;
1031         struct ds s;
1032
1033         ofpbuf_init(&actions, 0);
1034         error = odp_actions_from_string(argv[i], NULL, &actions);
1035
1036         if (error) {
1037             ofpbuf_uninit(&actions);
1038             dpctl_error(dpctl_p, error, "odp_actions_from_string");
1039             return error;
1040         }
1041
1042         ds_init(&s);
1043         format_odp_actions(&s, ofpbuf_data(&actions), ofpbuf_size(&actions));
1044         dpctl_print(dpctl_p, "%s\n", ds_cstr(&s));
1045         ds_destroy(&s);
1046
1047         ofpbuf_uninit(&actions);
1048     }
1049
1050     return error;
1051 }
1052
1053 struct actions_for_flow {
1054     struct hmap_node hmap_node;
1055     struct flow flow;
1056     struct ofpbuf actions;
1057 };
1058
1059 static struct actions_for_flow *
1060 get_actions_for_flow(struct hmap *actions_per_flow, const struct flow *flow)
1061 {
1062     uint32_t hash = flow_hash(flow, 0);
1063     struct actions_for_flow *af;
1064
1065     HMAP_FOR_EACH_WITH_HASH (af, hmap_node, hash, actions_per_flow) {
1066         if (flow_equal(&af->flow, flow)) {
1067             return af;
1068         }
1069     }
1070
1071     af = xmalloc(sizeof *af);
1072     af->flow = *flow;
1073     ofpbuf_init(&af->actions, 0);
1074     hmap_insert(actions_per_flow, &af->hmap_node, hash);
1075     return af;
1076 }
1077
1078 static int
1079 compare_actions_for_flow(const void *a_, const void *b_)
1080 {
1081     struct actions_for_flow *const *a = a_;
1082     struct actions_for_flow *const *b = b_;
1083
1084     return flow_compare_3way(&(*a)->flow, &(*b)->flow);
1085 }
1086
1087 static int
1088 compare_output_actions(const void *a_, const void *b_)
1089 {
1090     const struct nlattr *a = a_;
1091     const struct nlattr *b = b_;
1092     uint32_t a_port = nl_attr_get_u32(a);
1093     uint32_t b_port = nl_attr_get_u32(b);
1094
1095     return a_port < b_port ? -1 : a_port > b_port;
1096 }
1097
1098 static void
1099 sort_output_actions__(struct nlattr *first, struct nlattr *end)
1100 {
1101     size_t bytes = (uint8_t *) end - (uint8_t *) first;
1102     size_t n = bytes / NL_A_U32_SIZE;
1103
1104     ovs_assert(bytes % NL_A_U32_SIZE == 0);
1105     qsort(first, n, NL_A_U32_SIZE, compare_output_actions);
1106 }
1107
1108 static void
1109 sort_output_actions(struct nlattr *actions, size_t length)
1110 {
1111     struct nlattr *first_output = NULL;
1112     struct nlattr *a;
1113     int left;
1114
1115     NL_ATTR_FOR_EACH (a, left, actions, length) {
1116         if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT) {
1117             if (!first_output) {
1118                 first_output = a;
1119             }
1120         } else {
1121             if (first_output) {
1122                 sort_output_actions__(first_output, a);
1123                 first_output = NULL;
1124             }
1125         }
1126     }
1127     if (first_output) {
1128         uint8_t *end = (uint8_t *) actions + length;
1129         sort_output_actions__(first_output,
1130                               ALIGNED_CAST(struct nlattr *, end));
1131     }
1132 }
1133
1134 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1135  * have the syntax used by "ovs-dpctl dump-flows".
1136  *
1137  * This command prints ACTIONS in a format that shows what happens for each
1138  * VLAN, independent of the order of the ACTIONS.  For example, there is more
1139  * than one way to output a packet on VLANs 9 and 11, but this command will
1140  * print the same output for any form.
1141  *
1142  * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1143  * so far the implementation only covers VLANs. */
1144 static int
1145 dpctl_normalize_actions(int argc, const char *argv[],
1146                         struct dpctl_params *dpctl_p)
1147 {
1148     struct simap port_names;
1149     struct ofpbuf keybuf;
1150     struct flow flow;
1151     struct ofpbuf odp_actions;
1152     struct hmap actions_per_flow;
1153     struct actions_for_flow **afs;
1154     struct actions_for_flow *af;
1155     struct nlattr *a;
1156     size_t n_afs;
1157     struct ds s;
1158     int left;
1159     int i, error;
1160
1161     ds_init(&s);
1162
1163     simap_init(&port_names);
1164     for (i = 3; i < argc; i++) {
1165         char name[16];
1166         int number;
1167
1168         if (ovs_scan(argv[i], "%15[^=]=%d", name, &number)) {
1169             uintptr_t n = number;
1170             simap_put(&port_names, name, n);
1171         } else {
1172             dpctl_error(dpctl_p, 0, "%s: expected NAME=NUMBER", argv[i]);
1173             error = EINVAL;
1174             goto out;
1175         }
1176     }
1177
1178     /* Parse flow key. */
1179     ofpbuf_init(&keybuf, 0);
1180     error = odp_flow_from_string(argv[1], &port_names, &keybuf, NULL);
1181     if (error) {
1182         dpctl_error(dpctl_p, error, "odp_flow_key_from_string");
1183         goto out_freekeybuf;
1184     }
1185
1186     ds_clear(&s);
1187     odp_flow_format(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf), NULL, 0, NULL,
1188                     &s, dpctl_p->verbosity);
1189     dpctl_print(dpctl_p, "input flow: %s\n", ds_cstr(&s));
1190
1191     error = odp_flow_key_to_flow(ofpbuf_data(&keybuf), ofpbuf_size(&keybuf),
1192                                  &flow);
1193     if (error) {
1194         dpctl_error(dpctl_p, error, "odp_flow_key_to_flow");
1195         goto out_freekeybuf;
1196     }
1197
1198     /* Parse actions. */
1199     ofpbuf_init(&odp_actions, 0);
1200     error = odp_actions_from_string(argv[2], &port_names, &odp_actions);
1201     if (error) {
1202         dpctl_error(dpctl_p, error, "odp_actions_from_string");
1203         goto out_freeactions;
1204     }
1205
1206     if (dpctl_p->verbosity) {
1207         ds_clear(&s);
1208         format_odp_actions(&s, ofpbuf_data(&odp_actions),
1209                            ofpbuf_size(&odp_actions));
1210         dpctl_print(dpctl_p, "input actions: %s\n", ds_cstr(&s));
1211     }
1212
1213     hmap_init(&actions_per_flow);
1214     NL_ATTR_FOR_EACH (a, left, ofpbuf_data(&odp_actions),
1215                       ofpbuf_size(&odp_actions)) {
1216         const struct ovs_action_push_vlan *push;
1217         switch(nl_attr_type(a)) {
1218         case OVS_ACTION_ATTR_POP_VLAN:
1219             flow.vlan_tci = htons(0);
1220             continue;
1221
1222         case OVS_ACTION_ATTR_PUSH_VLAN:
1223             push = nl_attr_get_unspec(a, sizeof *push);
1224             flow.vlan_tci = push->vlan_tci;
1225             continue;
1226         }
1227
1228         af = get_actions_for_flow(&actions_per_flow, &flow);
1229         nl_msg_put_unspec(&af->actions, nl_attr_type(a),
1230                           nl_attr_get(a), nl_attr_get_size(a));
1231     }
1232
1233     n_afs = hmap_count(&actions_per_flow);
1234     afs = xmalloc(n_afs * sizeof *afs);
1235     i = 0;
1236     HMAP_FOR_EACH (af, hmap_node, &actions_per_flow) {
1237         afs[i++] = af;
1238     }
1239
1240     ovs_assert(i == n_afs);
1241     hmap_destroy(&actions_per_flow);
1242
1243     qsort(afs, n_afs, sizeof *afs, compare_actions_for_flow);
1244
1245     for (i = 0; i < n_afs; i++) {
1246         struct actions_for_flow *af = afs[i];
1247
1248         sort_output_actions(ofpbuf_data(&af->actions),
1249                             ofpbuf_size(&af->actions));
1250
1251         if (af->flow.vlan_tci != htons(0)) {
1252             dpctl_print(dpctl_p, "vlan(vid=%"PRIu16",pcp=%d): ",
1253                         vlan_tci_to_vid(af->flow.vlan_tci),
1254                         vlan_tci_to_pcp(af->flow.vlan_tci));
1255         } else {
1256             dpctl_print(dpctl_p, "no vlan: ");
1257         }
1258
1259         if (eth_type_mpls(af->flow.dl_type)) {
1260             dpctl_print(dpctl_p, "mpls(label=%"PRIu32",tc=%d,ttl=%d): ",
1261                         mpls_lse_to_label(af->flow.mpls_lse[0]),
1262                         mpls_lse_to_tc(af->flow.mpls_lse[0]),
1263                         mpls_lse_to_ttl(af->flow.mpls_lse[0]));
1264         } else {
1265             dpctl_print(dpctl_p, "no mpls: ");
1266         }
1267
1268         ds_clear(&s);
1269         format_odp_actions(&s, ofpbuf_data(&af->actions),
1270                            ofpbuf_size(&af->actions));
1271         dpctl_print(dpctl_p, ds_cstr(&s));
1272
1273         ofpbuf_uninit(&af->actions);
1274         free(af);
1275     }
1276     free(afs);
1277
1278
1279 out_freeactions:
1280     ofpbuf_uninit(&odp_actions);
1281 out_freekeybuf:
1282     ofpbuf_uninit(&keybuf);
1283 out:
1284     simap_destroy(&port_names);
1285     ds_destroy(&s);
1286
1287     return error;
1288 }
1289 \f
1290 static const struct dpctl_command all_commands[] = {
1291     { "add-dp", "add-dp dp [iface...]", 1, INT_MAX, dpctl_add_dp },
1292     { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp },
1293     { "add-if", "add-if dp iface...", 2, INT_MAX, dpctl_add_if },
1294     { "del-if", "del-if dp iface...", 2, INT_MAX, dpctl_del_if },
1295     { "set-if", "set-if dp iface...", 2, INT_MAX, dpctl_set_if },
1296     { "dump-dps", "", 0, 0, dpctl_dump_dps },
1297     { "show", "[dp...]", 0, INT_MAX, dpctl_show },
1298     { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows },
1299     { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow },
1300     { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow },
1301     { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow },
1302     { "del-flows", "[dp]", 0, 1, dpctl_del_flows },
1303     { "help", "", 0, INT_MAX, dpctl_help },
1304     { "list-commands", "", 0, INT_MAX, dpctl_list_commands },
1305
1306     /* Undocumented commands for testing. */
1307     { "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions },
1308     { "normalize-actions", "actions", 2, INT_MAX, dpctl_normalize_actions },
1309
1310     { NULL, NULL, 0, 0, NULL },
1311 };
1312
1313 static const struct dpctl_command *get_all_dpctl_commands(void)
1314 {
1315     return all_commands;
1316 }
1317
1318 /* Runs the command designated by argv[0] within the command table specified by
1319  * 'commands', which must be terminated by a command whose 'name' member is a
1320  * null pointer. */
1321 int
1322 dpctl_run_command(int argc, const char *argv[], struct dpctl_params *dpctl_p)
1323 {
1324     const struct dpctl_command *p;
1325
1326     if (argc < 1) {
1327         dpctl_error(dpctl_p, 0, "missing command name; use --help for help");
1328         return EINVAL;
1329     }
1330
1331     for (p = all_commands; p->name != NULL; p++) {
1332         if (!strcmp(p->name, argv[0])) {
1333             int n_arg = argc - 1;
1334             if (n_arg < p->min_args) {
1335                 dpctl_error(dpctl_p, 0,
1336                             "'%s' command requires at least %d arguments",
1337                             p->name, p->min_args);
1338                 return EINVAL;
1339             } else if (n_arg > p->max_args) {
1340                 dpctl_error(dpctl_p, 0,
1341                             "'%s' command takes at most %d arguments",
1342                             p->name, p->max_args);
1343                 return EINVAL;
1344             } else {
1345                 return p->handler(argc, argv, dpctl_p);
1346             }
1347         }
1348     }
1349
1350     dpctl_error(dpctl_p, 0, "unknown command '%s'; use --help for help",
1351                 argv[0]);
1352     return EINVAL;
1353 }
1354 \f
1355 static void
1356 dpctl_unixctl_print(void *userdata, bool error OVS_UNUSED, const char *msg)
1357 {
1358     struct ds *ds = userdata;
1359     ds_put_cstr(ds, msg);
1360 }
1361
1362 static void
1363 dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
1364                       void *aux)
1365 {
1366     struct ds ds = DS_EMPTY_INITIALIZER;
1367     struct dpctl_params dpctl_p;
1368     bool opt_parse_err = false;
1369
1370     dpctl_command_handler *handler = (dpctl_command_handler *) aux;
1371
1372     dpctl_p.print_statistics = false;
1373     dpctl_p.zero_statistics = false;
1374     dpctl_p.may_create = false;
1375     dpctl_p.verbosity = 0;
1376
1377     /* Parse options (like getopt). Unfortunately it does
1378      * not seem a good idea to call getopt_long() here, since it uses global
1379      * variables */
1380     while (argc > 1 && !opt_parse_err) {
1381         const char *arg = argv[1];
1382         if (!strncmp(arg, "--", 2)) {
1383             /* Long option */
1384             if (!strcmp(arg, "--statistics")) {
1385                 dpctl_p.print_statistics = true;
1386             } else if (!strcmp(arg, "--clear")) {
1387                 dpctl_p.zero_statistics = true;
1388             } else if (!strcmp(arg, "--may-create")) {
1389                 dpctl_p.may_create = true;
1390             } else if (!strcmp(arg, "--more")) {
1391                 dpctl_p.verbosity++;
1392             } else {
1393                 ds_put_format(&ds, "Unrecognized option %s", argv[1]);
1394                 opt_parse_err = true;
1395             }
1396         } else if (arg[0] == '-' && arg[1] != '\0') {
1397             /* Short option[s] */
1398             const char *opt = &arg[1];
1399
1400             while (*opt && !opt_parse_err) {
1401                 switch (*opt) {
1402                 case 'm':
1403                     dpctl_p.verbosity++;
1404                     break;
1405                 case 's':
1406                     dpctl_p.print_statistics = true;
1407                     break;
1408                 default:
1409                     ds_put_format(&ds, "Unrecognized option -%c", *opt);
1410                     opt_parse_err = true;
1411                     break;
1412                 }
1413                 opt++;
1414             }
1415         } else {
1416             /* Doesn't start with -, not an option */
1417             break;
1418         }
1419
1420         if (opt_parse_err) {
1421             break;
1422         }
1423         argv++;
1424         argc--;
1425     }
1426
1427     if (!opt_parse_err) {
1428         dpctl_p.is_appctl = true;
1429         dpctl_p.output = dpctl_unixctl_print;
1430         dpctl_p.aux = &ds;
1431
1432         handler(argc, argv, &dpctl_p);
1433     }
1434
1435     unixctl_command_reply(conn, ds_cstr(&ds));
1436
1437     ds_destroy(&ds);
1438 }
1439
1440 void
1441 dpctl_unixctl_register(void)
1442 {
1443     const struct dpctl_command *p;
1444
1445     for (p = all_commands; p->name != NULL; p++) {
1446         char *cmd_name = xasprintf("dpctl/%s", p->name);
1447         unixctl_command_register(cmd_name, "", p->min_args, p->max_args,
1448                                  dpctl_unixctl_handler, p->handler);
1449         free(cmd_name);
1450     }
1451 }