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