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