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