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