2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #include "command-line.h"
34 #include "dynamic-string.h"
40 #include "ofp-parse.h"
51 typedef int dpctl_command_handler(int argc
, const char *argv
[],
52 struct dpctl_params
*);
53 struct dpctl_command
{
58 dpctl_command_handler
*handler
;
60 static const struct dpctl_command
*get_all_dpctl_commands(void);
63 dpctl_puts(struct dpctl_params
*dpctl_p
, bool error
, const char *string
)
65 dpctl_p
->output(dpctl_p
->aux
, error
, string
);
69 dpctl_print(struct dpctl_params
*dpctl_p
, const char *fmt
, ...)
75 string
= xvasprintf(fmt
, args
);
78 dpctl_puts(dpctl_p
, false, string
);
83 dpctl_error(struct dpctl_params
* dpctl_p
, int err_no
, const char *fmt
, ...)
85 const char *subprogram_name
= get_subprogram_name();
86 struct ds ds
= DS_EMPTY_INITIALIZER
;
87 int save_errno
= errno
;
91 if (subprogram_name
[0]) {
92 ds_put_format(&ds
, "%s(%s): ", program_name
,subprogram_name
);
94 ds_put_format(&ds
, "%s: ", program_name
);
98 ds_put_format_valist(&ds
, fmt
, args
);
102 ds_put_format(&ds
, " (%s)", ovs_retval_to_string(err_no
));
104 ds_put_cstr(&ds
, "\n");
106 dpctl_puts(dpctl_p
, true, ds_cstr(&ds
));
113 static int dpctl_add_if(int argc
, const char *argv
[], struct dpctl_params
*);
116 if_up(struct netdev
*netdev
)
118 return netdev_turn_flags_on(netdev
, NETDEV_UP
, NULL
);
121 /* Retrieve the name of the datapath if exactly one exists. The caller
122 * is responsible for freeing the returned string. If there is not one
123 * datapath, aborts with an error message. */
125 get_one_dp(struct dpctl_params
*dpctl_p
)
129 char *dp_name
= NULL
;
133 dp_enumerate_types(&types
);
134 SSET_FOR_EACH (type
, &types
) {
138 if (!dp_enumerate_names(type
, &names
)) {
139 count
+= sset_count(&names
);
140 if (!dp_name
&& count
== 1) {
141 dp_name
= xasprintf("%s@%s", type
, SSET_FIRST(&names
));
144 sset_destroy(&names
);
146 sset_destroy(&types
);
149 dpctl_error(dpctl_p
, 0, "no datapaths exist");
150 } else if (count
> 1) {
151 dpctl_error(dpctl_p
, 0, "multiple datapaths, specify one");
160 parsed_dpif_open(const char *arg_
, bool create
, struct dpif
**dpifp
)
165 dp_parse_name(arg_
, &name
, &type
);
168 result
= dpif_create(name
, type
, dpifp
);
170 result
= dpif_open(name
, type
, dpifp
);
179 dpctl_add_dp(int argc
, const char *argv
[],
180 struct dpctl_params
*dpctl_p
)
185 error
= parsed_dpif_open(argv
[1], true, &dpif
);
187 dpctl_error(dpctl_p
, error
, "add_dp");
192 error
= dpctl_add_if(argc
, argv
, dpctl_p
);
198 dpctl_del_dp(int argc OVS_UNUSED
, const char *argv
[],
199 struct dpctl_params
*dpctl_p
)
204 error
= parsed_dpif_open(argv
[1], false, &dpif
);
206 dpctl_error(dpctl_p
, error
, "opening datapath");
209 error
= dpif_delete(dpif
);
211 dpctl_error(dpctl_p
, error
, "del_dp");
219 dpctl_add_if(int argc OVS_UNUSED
, const char *argv
[],
220 struct dpctl_params
*dpctl_p
)
223 int i
, error
, lasterror
= 0;
225 error
= parsed_dpif_open(argv
[1], false, &dpif
);
227 dpctl_error(dpctl_p
, error
, "opening datapath");
230 for (i
= 2; i
< argc
; i
++) {
231 const char *name
, *type
;
232 char *save_ptr
= NULL
, *argcopy
;
233 struct netdev
*netdev
= NULL
;
235 odp_port_t port_no
= ODPP_NONE
;
238 argcopy
= xstrdup(argv
[i
]);
239 name
= strtok_r(argcopy
, ",", &save_ptr
);
243 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
250 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
251 char *save_ptr_2
= NULL
;
254 key
= strtok_r(option
, "=", &save_ptr_2
);
255 value
= strtok_r(NULL
, "", &save_ptr_2
);
260 if (!strcmp(key
, "type")) {
262 } else if (!strcmp(key
, "port_no")) {
263 port_no
= u32_to_odp(atoi(value
));
264 } else if (!smap_add_once(&args
, key
, value
)) {
265 dpctl_error(dpctl_p
, 0, "duplicate \"%s\" option", key
);
269 error
= netdev_open(name
, type
, &netdev
);
271 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
273 goto next_destroy_args
;
276 error
= netdev_set_config(netdev
, &args
, NULL
);
278 goto next_destroy_args
;
281 error
= dpif_port_add(dpif
, netdev
, &port_no
);
283 dpctl_error(dpctl_p
, error
, "adding %s to %s failed", name
,
285 goto next_destroy_args
;
288 error
= if_up(netdev
);
290 dpctl_error(dpctl_p
, error
, "%s: failed bringing interface up",
295 netdev_close(netdev
);
309 dpctl_set_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
312 int i
, error
, lasterror
= 0;
314 error
= parsed_dpif_open(argv
[1], false, &dpif
);
316 dpctl_error(dpctl_p
, error
, "opening datapath");
319 for (i
= 2; i
< argc
; i
++) {
320 struct netdev
*netdev
= NULL
;
321 struct dpif_port dpif_port
;
322 char *save_ptr
= NULL
;
331 argcopy
= xstrdup(argv
[i
]);
332 name
= strtok_r(argcopy
, ",", &save_ptr
);
334 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
339 /* Get the port's type from the datapath. */
340 error
= dpif_port_query_by_name(dpif
, name
, &dpif_port
);
342 dpctl_error(dpctl_p
, error
, "%s: failed to query port in %s", name
,
346 type
= xstrdup(dpif_port
.type
);
347 port_no
= dpif_port
.port_no
;
348 dpif_port_destroy(&dpif_port
);
350 /* Retrieve its existing configuration. */
351 error
= netdev_open(name
, type
, &netdev
);
353 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
359 error
= netdev_get_config(netdev
, &args
);
361 dpctl_error(dpctl_p
, error
, "%s: failed to fetch configuration",
363 goto next_destroy_args
;
366 /* Parse changes to configuration. */
367 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
368 char *save_ptr_2
= NULL
;
371 key
= strtok_r(option
, "=", &save_ptr_2
);
372 value
= strtok_r(NULL
, "", &save_ptr_2
);
377 if (!strcmp(key
, "type")) {
378 if (strcmp(value
, type
)) {
379 dpctl_error(dpctl_p
, 0,
380 "%s: can't change type from %s to %s",
384 } else if (!strcmp(key
, "port_no")) {
385 if (port_no
!= u32_to_odp(atoi(value
))) {
386 dpctl_error(dpctl_p
, 0, "%s: can't change port number from"
387 " %"PRIu32
" to %d", name
, port_no
, atoi(value
));
390 } else if (value
[0] == '\0') {
391 smap_remove(&args
, key
);
393 smap_replace(&args
, key
, value
);
397 /* Update configuration. */
398 error
= netdev_set_config(netdev
, &args
, NULL
);
400 goto next_destroy_args
;
406 netdev_close(netdev
);
419 get_port_number(struct dpif
*dpif
, const char *name
, odp_port_t
*port
,
420 struct dpctl_params
*dpctl_p
)
422 struct dpif_port dpif_port
;
424 if (!dpif_port_query_by_name(dpif
, name
, &dpif_port
)) {
425 *port
= dpif_port
.port_no
;
426 dpif_port_destroy(&dpif_port
);
429 dpctl_error(dpctl_p
, 0, "no port named %s", name
);
435 dpctl_del_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
438 int i
, error
, lasterror
= 0;
440 error
= parsed_dpif_open(argv
[1], false, &dpif
);
442 dpctl_error(dpctl_p
, error
, "opening datapath");
445 for (i
= 2; i
< argc
; i
++) {
446 const char *name
= argv
[i
];
449 if (!name
[strspn(name
, "0123456789")]) {
450 port
= u32_to_odp(atoi(name
));
451 } else if (!get_port_number(dpif
, name
, &port
, dpctl_p
)) {
456 error
= dpif_port_del(dpif
, port
);
458 dpctl_error(dpctl_p
, error
, "deleting port %s from %s failed",
468 print_stat(struct dpctl_params
*dpctl_p
, const char *leader
, uint64_t value
)
470 dpctl_print(dpctl_p
, "%s", leader
);
471 if (value
!= UINT64_MAX
) {
472 dpctl_print(dpctl_p
, "%"PRIu64
, value
);
474 dpctl_print(dpctl_p
, "?");
479 print_human_size(struct dpctl_params
*dpctl_p
, uint64_t value
)
481 if (value
== UINT64_MAX
) {
483 } else if (value
>= 1024ULL * 1024 * 1024 * 1024) {
484 dpctl_print(dpctl_p
, " (%.1f TiB)",
485 value
/ (1024.0 * 1024 * 1024 * 1024));
486 } else if (value
>= 1024ULL * 1024 * 1024) {
487 dpctl_print(dpctl_p
, " (%.1f GiB)", value
/ (1024.0 * 1024 * 1024));
488 } else if (value
>= 1024ULL * 1024) {
489 dpctl_print(dpctl_p
, " (%.1f MiB)", value
/ (1024.0 * 1024));
490 } else if (value
>= 1024) {
491 dpctl_print(dpctl_p
, " (%.1f KiB)", value
/ 1024.0);
496 show_dpif(struct dpif
*dpif
, struct dpctl_params
*dpctl_p
)
498 struct dpif_port_dump dump
;
499 struct dpif_port dpif_port
;
500 struct dpif_dp_stats stats
;
501 struct netdev
*netdev
;
503 dpctl_print(dpctl_p
, "%s:\n", dpif_name(dpif
));
504 if (!dpif_get_dp_stats(dpif
, &stats
)) {
505 dpctl_print(dpctl_p
, "\tlookups: hit:%"PRIu64
" missed:%"PRIu64
506 " lost:%"PRIu64
"\n\tflows: %"PRIu64
"\n",
507 stats
.n_hit
, stats
.n_missed
, stats
.n_lost
, stats
.n_flows
);
508 if (stats
.n_masks
!= UINT32_MAX
) {
509 uint64_t n_pkts
= stats
.n_hit
+ stats
.n_missed
;
510 double avg
= n_pkts
? (double) stats
.n_mask_hit
/ n_pkts
: 0.0;
512 dpctl_print(dpctl_p
, "\tmasks: hit:%"PRIu64
" total:%"PRIu32
514 stats
.n_mask_hit
, stats
.n_masks
, avg
);
518 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, dpif
) {
519 dpctl_print(dpctl_p
, "\tport %u: %s",
520 dpif_port
.port_no
, dpif_port
.name
);
522 if (strcmp(dpif_port
.type
, "system")) {
525 dpctl_print(dpctl_p
, " (%s", dpif_port
.type
);
527 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
532 error
= netdev_get_config(netdev
, &config
);
534 const struct smap_node
**nodes
;
537 nodes
= smap_sort(&config
);
538 for (i
= 0; i
< smap_count(&config
); i
++) {
539 const struct smap_node
*node
= nodes
[i
];
540 dpctl_print(dpctl_p
, "%c %s=%s", i
? ',' : ':',
541 node
->key
, node
->value
);
545 dpctl_print(dpctl_p
, ", could not retrieve configuration "
546 "(%s)", ovs_strerror(error
));
548 smap_destroy(&config
);
550 netdev_close(netdev
);
552 dpctl_print(dpctl_p
, ": open failed (%s)",
553 ovs_strerror(error
));
555 dpctl_print(dpctl_p
, ")");
557 dpctl_print(dpctl_p
, "\n");
559 if (dpctl_p
->print_statistics
) {
560 struct netdev_stats s
;
563 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
565 dpctl_print(dpctl_p
, ", open failed (%s)",
566 ovs_strerror(error
));
569 error
= netdev_get_stats(netdev
, &s
);
571 netdev_close(netdev
);
572 print_stat(dpctl_p
, "\t\tRX packets:", s
.rx_packets
);
573 print_stat(dpctl_p
, " errors:", s
.rx_errors
);
574 print_stat(dpctl_p
, " dropped:", s
.rx_dropped
);
575 print_stat(dpctl_p
, " overruns:", s
.rx_over_errors
);
576 print_stat(dpctl_p
, " frame:", s
.rx_frame_errors
);
577 dpctl_print(dpctl_p
, "\n");
579 print_stat(dpctl_p
, "\t\tTX packets:", s
.tx_packets
);
580 print_stat(dpctl_p
, " errors:", s
.tx_errors
);
581 print_stat(dpctl_p
, " dropped:", s
.tx_dropped
);
582 print_stat(dpctl_p
, " aborted:", s
.tx_aborted_errors
);
583 print_stat(dpctl_p
, " carrier:", s
.tx_carrier_errors
);
584 dpctl_print(dpctl_p
, "\n");
586 print_stat(dpctl_p
, "\t\tcollisions:", s
.collisions
);
587 dpctl_print(dpctl_p
, "\n");
589 print_stat(dpctl_p
, "\t\tRX bytes:", s
.rx_bytes
);
590 print_human_size(dpctl_p
, s
.rx_bytes
);
591 print_stat(dpctl_p
, " TX bytes:", s
.tx_bytes
);
592 print_human_size(dpctl_p
, s
.tx_bytes
);
593 dpctl_print(dpctl_p
, "\n");
595 dpctl_print(dpctl_p
, ", could not retrieve stats (%s)",
596 ovs_strerror(error
));
604 dpctl_show(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
606 int error
, lasterror
= 0;
609 for (i
= 1; i
< argc
; i
++) {
610 const char *name
= argv
[i
];
613 error
= parsed_dpif_open(name
, false, &dpif
);
615 show_dpif(dpif
, dpctl_p
);
617 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
627 dp_enumerate_types(&types
);
628 SSET_FOR_EACH (type
, &types
) {
633 error
= dp_enumerate_names(type
, &names
);
638 SSET_FOR_EACH (name
, &names
) {
641 error
= dpif_open(name
, type
, &dpif
);
643 show_dpif(dpif
, dpctl_p
);
645 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
651 sset_destroy(&names
);
653 sset_destroy(&types
);
659 dpctl_dump_dps(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
660 struct dpctl_params
*dpctl_p
)
662 struct sset dpif_names
, dpif_types
;
664 int error
, lasterror
= 0;
666 sset_init(&dpif_names
);
667 sset_init(&dpif_types
);
668 dp_enumerate_types(&dpif_types
);
670 SSET_FOR_EACH (type
, &dpif_types
) {
673 error
= dp_enumerate_names(type
, &dpif_names
);
678 SSET_FOR_EACH (name
, &dpif_names
) {
680 if (!dpif_open(name
, type
, &dpif
)) {
681 dpctl_print(dpctl_p
, "%s\n", dpif_name(dpif
));
687 sset_destroy(&dpif_names
);
688 sset_destroy(&dpif_types
);
693 dpctl_dump_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
700 struct flow flow_filter
;
701 struct flow_wildcards wc_filter
;
703 struct dpif_port_dump port_dump
;
704 struct dpif_port dpif_port
;
705 struct hmap portno_names
;
706 struct simap names_portno
;
708 struct dpif_flow_dump_thread
*flow_dump_thread
;
709 struct dpif_flow_dump
*flow_dump
;
714 if (argc
> 1 && !strncmp(argv
[argc
- 1], "filter=", 7)) {
715 filter
= xstrdup(argv
[--argc
] + 7);
717 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
723 error
= parsed_dpif_open(name
, false, &dpif
);
726 dpctl_error(dpctl_p
, error
, "opening datapath");
731 hmap_init(&portno_names
);
732 simap_init(&names_portno
);
733 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
734 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
735 simap_put(&names_portno
, dpif_port
.name
,
736 odp_to_u32(dpif_port
.port_no
));
740 char *err
= parse_ofp_exact_flow(&flow_filter
, &wc_filter
.masks
,
741 filter
, &names_portno
);
743 dpctl_error(dpctl_p
, 0, "Failed to parse filter (%s)", err
);
750 flow_dump
= dpif_flow_dump_create(dpif
);
751 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
752 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
755 struct flow_wildcards wc
;
756 struct match match
, match_filter
;
757 struct minimatch minimatch
;
759 odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
);
760 odp_flow_key_to_mask(f
.mask
, f
.mask_len
, &wc
.masks
, &flow
);
761 match_init(&match
, &flow
, &wc
);
763 match_init(&match_filter
, &flow_filter
, &wc
);
764 match_init(&match_filter
, &match_filter
.flow
, &wc_filter
);
765 minimatch_init(&minimatch
, &match_filter
);
767 if (!minimatch_matches_flow(&minimatch
, &match
.flow
)) {
768 minimatch_destroy(&minimatch
);
771 minimatch_destroy(&minimatch
);
774 odp_flow_format(f
.key
, f
.key_len
, f
.mask
, f
.mask_len
,
775 &portno_names
, &ds
, dpctl_p
->verbosity
);
776 ds_put_cstr(&ds
, ", ");
778 dpif_flow_stats_format(&f
.stats
, &ds
);
779 ds_put_cstr(&ds
, ", actions:");
780 format_odp_actions(&ds
, f
.actions
, f
.actions_len
);
781 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
783 dpif_flow_dump_thread_destroy(flow_dump_thread
);
784 error
= dpif_flow_dump_destroy(flow_dump
);
787 dpctl_error(dpctl_p
, error
, "Failed to dump flows from datapath");
792 odp_portno_names_destroy(&portno_names
);
793 simap_destroy(&names_portno
);
794 hmap_destroy(&portno_names
);
802 dpctl_put_flow(int argc
, const char *argv
[], enum dpif_flow_put_flags flags
,
803 struct dpctl_params
*dpctl_p
)
805 const char *key_s
= argv
[argc
- 2];
806 const char *actions_s
= argv
[argc
- 1];
807 struct dpif_flow_stats stats
;
808 struct dpif_port dpif_port
;
809 struct dpif_port_dump port_dump
;
810 struct ofpbuf actions
;
815 struct simap port_names
;
818 dp_name
= argc
== 4 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
822 error
= parsed_dpif_open(dp_name
, false, &dpif
);
825 dpctl_error(dpctl_p
, error
, "opening datapath");
830 simap_init(&port_names
);
831 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
832 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
835 ofpbuf_init(&key
, 0);
836 ofpbuf_init(&mask
, 0);
837 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
838 simap_destroy(&port_names
);
840 dpctl_error(dpctl_p
, error
, "parsing flow key");
841 goto out_freekeymask
;
844 ofpbuf_init(&actions
, 0);
845 error
= odp_actions_from_string(actions_s
, NULL
, &actions
);
847 dpctl_error(dpctl_p
, error
, "parsing actions");
848 goto out_freeactions
;
850 error
= dpif_flow_put(dpif
, flags
,
851 ofpbuf_data(&key
), ofpbuf_size(&key
),
852 ofpbuf_size(&mask
) == 0 ? NULL
: ofpbuf_data(&mask
),
854 ofpbuf_data(&actions
), ofpbuf_size(&actions
),
855 dpctl_p
->print_statistics
? &stats
: NULL
);
857 dpctl_error(dpctl_p
, error
, "updating flow table");
858 goto out_freeactions
;
861 if (dpctl_p
->print_statistics
) {
865 dpif_flow_stats_format(&stats
, &s
);
866 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
871 ofpbuf_uninit(&actions
);
873 ofpbuf_uninit(&mask
);
880 dpctl_add_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
882 return dpctl_put_flow(argc
, argv
, DPIF_FP_CREATE
, dpctl_p
);
886 dpctl_mod_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
888 enum dpif_flow_put_flags flags
;
890 flags
= DPIF_FP_MODIFY
;
891 if (dpctl_p
->may_create
) {
892 flags
|= DPIF_FP_CREATE
;
894 if (dpctl_p
->zero_statistics
) {
895 flags
|= DPIF_FP_ZERO_STATS
;
898 return dpctl_put_flow(argc
, argv
, flags
, dpctl_p
);
902 dpctl_del_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
904 const char *key_s
= argv
[argc
- 1];
905 struct dpif_flow_stats stats
;
906 struct dpif_port dpif_port
;
907 struct dpif_port_dump port_dump
;
909 struct ofpbuf mask
; /* To be ignored. */
912 struct simap port_names
;
915 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
919 error
= parsed_dpif_open(dp_name
, false, &dpif
);
922 dpctl_error(dpctl_p
, error
, "opening datapath");
926 simap_init(&port_names
);
927 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
928 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
931 ofpbuf_init(&key
, 0);
932 ofpbuf_init(&mask
, 0);
934 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
936 dpctl_error(dpctl_p
, error
, "parsing flow key");
940 error
= dpif_flow_del(dpif
,
941 ofpbuf_data(&key
), ofpbuf_size(&key
),
942 dpctl_p
->print_statistics
? &stats
: NULL
);
944 dpctl_error(dpctl_p
, error
, "deleting flow");
948 if (dpctl_p
->print_statistics
) {
952 dpif_flow_stats_format(&stats
, &s
);
953 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
958 ofpbuf_uninit(&mask
);
960 simap_destroy(&port_names
);
966 dpctl_del_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
972 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
976 error
= parsed_dpif_open(name
, false, &dpif
);
979 dpctl_error(dpctl_p
, error
, "opening datapath");
983 error
= dpif_flow_flush(dpif
);
985 dpctl_error(dpctl_p
, error
, "deleting all flows");
992 dpctl_help(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
993 struct dpctl_params
*dpctl_p
)
995 if (dpctl_p
->usage
) {
996 dpctl_p
->usage(dpctl_p
->aux
);
1003 dpctl_list_commands(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1004 struct dpctl_params
*dpctl_p
)
1006 struct ds ds
= DS_EMPTY_INITIALIZER
;
1007 const struct dpctl_command
*commands
= get_all_dpctl_commands();
1009 ds_put_cstr(&ds
, "The available commands are:\n");
1010 for (; commands
->name
; commands
++) {
1011 const struct dpctl_command
*c
= commands
;
1013 ds_put_format(&ds
, " %s%-23s %s\n", dpctl_p
->is_appctl
? "dpctl/" : "",
1016 dpctl_puts(dpctl_p
, false, ds
.string
);
1022 /* Undocumented commands for unit testing. */
1025 dpctl_parse_actions(int argc
, const char *argv
[], struct dpctl_params
* dpctl_p
)
1029 for (i
= 1; i
< argc
; i
++) {
1030 struct ofpbuf actions
;
1033 ofpbuf_init(&actions
, 0);
1034 error
= odp_actions_from_string(argv
[i
], NULL
, &actions
);
1037 ofpbuf_uninit(&actions
);
1038 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1043 format_odp_actions(&s
, ofpbuf_data(&actions
), ofpbuf_size(&actions
));
1044 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1047 ofpbuf_uninit(&actions
);
1053 struct actions_for_flow
{
1054 struct hmap_node hmap_node
;
1056 struct ofpbuf actions
;
1059 static struct actions_for_flow
*
1060 get_actions_for_flow(struct hmap
*actions_per_flow
, const struct flow
*flow
)
1062 uint32_t hash
= flow_hash(flow
, 0);
1063 struct actions_for_flow
*af
;
1065 HMAP_FOR_EACH_WITH_HASH (af
, hmap_node
, hash
, actions_per_flow
) {
1066 if (flow_equal(&af
->flow
, flow
)) {
1071 af
= xmalloc(sizeof *af
);
1073 ofpbuf_init(&af
->actions
, 0);
1074 hmap_insert(actions_per_flow
, &af
->hmap_node
, hash
);
1079 compare_actions_for_flow(const void *a_
, const void *b_
)
1081 struct actions_for_flow
*const *a
= a_
;
1082 struct actions_for_flow
*const *b
= b_
;
1084 return flow_compare_3way(&(*a
)->flow
, &(*b
)->flow
);
1088 compare_output_actions(const void *a_
, const void *b_
)
1090 const struct nlattr
*a
= a_
;
1091 const struct nlattr
*b
= b_
;
1092 uint32_t a_port
= nl_attr_get_u32(a
);
1093 uint32_t b_port
= nl_attr_get_u32(b
);
1095 return a_port
< b_port
? -1 : a_port
> b_port
;
1099 sort_output_actions__(struct nlattr
*first
, struct nlattr
*end
)
1101 size_t bytes
= (uint8_t *) end
- (uint8_t *) first
;
1102 size_t n
= bytes
/ NL_A_U32_SIZE
;
1104 ovs_assert(bytes
% NL_A_U32_SIZE
== 0);
1105 qsort(first
, n
, NL_A_U32_SIZE
, compare_output_actions
);
1109 sort_output_actions(struct nlattr
*actions
, size_t length
)
1111 struct nlattr
*first_output
= NULL
;
1115 NL_ATTR_FOR_EACH (a
, left
, actions
, length
) {
1116 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
) {
1117 if (!first_output
) {
1122 sort_output_actions__(first_output
, a
);
1123 first_output
= NULL
;
1128 uint8_t *end
= (uint8_t *) actions
+ length
;
1129 sort_output_actions__(first_output
,
1130 ALIGNED_CAST(struct nlattr
*, end
));
1134 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1135 * have the syntax used by "ovs-dpctl dump-flows".
1137 * This command prints ACTIONS in a format that shows what happens for each
1138 * VLAN, independent of the order of the ACTIONS. For example, there is more
1139 * than one way to output a packet on VLANs 9 and 11, but this command will
1140 * print the same output for any form.
1142 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1143 * so far the implementation only covers VLANs. */
1145 dpctl_normalize_actions(int argc
, const char *argv
[],
1146 struct dpctl_params
*dpctl_p
)
1148 struct simap port_names
;
1149 struct ofpbuf keybuf
;
1151 struct ofpbuf odp_actions
;
1152 struct hmap actions_per_flow
;
1153 struct actions_for_flow
**afs
;
1154 struct actions_for_flow
*af
;
1163 simap_init(&port_names
);
1164 for (i
= 3; i
< argc
; i
++) {
1168 if (ovs_scan(argv
[i
], "%15[^=]=%d", name
, &number
)) {
1169 uintptr_t n
= number
;
1170 simap_put(&port_names
, name
, n
);
1172 dpctl_error(dpctl_p
, 0, "%s: expected NAME=NUMBER", argv
[i
]);
1178 /* Parse flow key. */
1179 ofpbuf_init(&keybuf
, 0);
1180 error
= odp_flow_from_string(argv
[1], &port_names
, &keybuf
, NULL
);
1182 dpctl_error(dpctl_p
, error
, "odp_flow_key_from_string");
1183 goto out_freekeybuf
;
1187 odp_flow_format(ofpbuf_data(&keybuf
), ofpbuf_size(&keybuf
), NULL
, 0, NULL
,
1188 &s
, dpctl_p
->verbosity
);
1189 dpctl_print(dpctl_p
, "input flow: %s\n", ds_cstr(&s
));
1191 error
= odp_flow_key_to_flow(ofpbuf_data(&keybuf
), ofpbuf_size(&keybuf
),
1194 dpctl_error(dpctl_p
, error
, "odp_flow_key_to_flow");
1195 goto out_freekeybuf
;
1198 /* Parse actions. */
1199 ofpbuf_init(&odp_actions
, 0);
1200 error
= odp_actions_from_string(argv
[2], &port_names
, &odp_actions
);
1202 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1203 goto out_freeactions
;
1206 if (dpctl_p
->verbosity
) {
1208 format_odp_actions(&s
, ofpbuf_data(&odp_actions
),
1209 ofpbuf_size(&odp_actions
));
1210 dpctl_print(dpctl_p
, "input actions: %s\n", ds_cstr(&s
));
1213 hmap_init(&actions_per_flow
);
1214 NL_ATTR_FOR_EACH (a
, left
, ofpbuf_data(&odp_actions
),
1215 ofpbuf_size(&odp_actions
)) {
1216 const struct ovs_action_push_vlan
*push
;
1217 switch(nl_attr_type(a
)) {
1218 case OVS_ACTION_ATTR_POP_VLAN
:
1219 flow
.vlan_tci
= htons(0);
1222 case OVS_ACTION_ATTR_PUSH_VLAN
:
1223 push
= nl_attr_get_unspec(a
, sizeof *push
);
1224 flow
.vlan_tci
= push
->vlan_tci
;
1228 af
= get_actions_for_flow(&actions_per_flow
, &flow
);
1229 nl_msg_put_unspec(&af
->actions
, nl_attr_type(a
),
1230 nl_attr_get(a
), nl_attr_get_size(a
));
1233 n_afs
= hmap_count(&actions_per_flow
);
1234 afs
= xmalloc(n_afs
* sizeof *afs
);
1236 HMAP_FOR_EACH (af
, hmap_node
, &actions_per_flow
) {
1240 ovs_assert(i
== n_afs
);
1241 hmap_destroy(&actions_per_flow
);
1243 qsort(afs
, n_afs
, sizeof *afs
, compare_actions_for_flow
);
1245 for (i
= 0; i
< n_afs
; i
++) {
1246 struct actions_for_flow
*af
= afs
[i
];
1248 sort_output_actions(ofpbuf_data(&af
->actions
),
1249 ofpbuf_size(&af
->actions
));
1251 if (af
->flow
.vlan_tci
!= htons(0)) {
1252 dpctl_print(dpctl_p
, "vlan(vid=%"PRIu16
",pcp=%d): ",
1253 vlan_tci_to_vid(af
->flow
.vlan_tci
),
1254 vlan_tci_to_pcp(af
->flow
.vlan_tci
));
1256 dpctl_print(dpctl_p
, "no vlan: ");
1259 if (eth_type_mpls(af
->flow
.dl_type
)) {
1260 dpctl_print(dpctl_p
, "mpls(label=%"PRIu32
",tc=%d,ttl=%d): ",
1261 mpls_lse_to_label(af
->flow
.mpls_lse
[0]),
1262 mpls_lse_to_tc(af
->flow
.mpls_lse
[0]),
1263 mpls_lse_to_ttl(af
->flow
.mpls_lse
[0]));
1265 dpctl_print(dpctl_p
, "no mpls: ");
1269 format_odp_actions(&s
, ofpbuf_data(&af
->actions
),
1270 ofpbuf_size(&af
->actions
));
1271 dpctl_print(dpctl_p
, ds_cstr(&s
));
1273 ofpbuf_uninit(&af
->actions
);
1280 ofpbuf_uninit(&odp_actions
);
1282 ofpbuf_uninit(&keybuf
);
1284 simap_destroy(&port_names
);
1290 static const struct dpctl_command all_commands
[] = {
1291 { "add-dp", "add-dp dp [iface...]", 1, INT_MAX
, dpctl_add_dp
},
1292 { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp
},
1293 { "add-if", "add-if dp iface...", 2, INT_MAX
, dpctl_add_if
},
1294 { "del-if", "del-if dp iface...", 2, INT_MAX
, dpctl_del_if
},
1295 { "set-if", "set-if dp iface...", 2, INT_MAX
, dpctl_set_if
},
1296 { "dump-dps", "", 0, 0, dpctl_dump_dps
},
1297 { "show", "[dp...]", 0, INT_MAX
, dpctl_show
},
1298 { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows
},
1299 { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow
},
1300 { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow
},
1301 { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow
},
1302 { "del-flows", "[dp]", 0, 1, dpctl_del_flows
},
1303 { "help", "", 0, INT_MAX
, dpctl_help
},
1304 { "list-commands", "", 0, INT_MAX
, dpctl_list_commands
},
1306 /* Undocumented commands for testing. */
1307 { "parse-actions", "actions", 1, INT_MAX
, dpctl_parse_actions
},
1308 { "normalize-actions", "actions", 2, INT_MAX
, dpctl_normalize_actions
},
1310 { NULL
, NULL
, 0, 0, NULL
},
1313 static const struct dpctl_command
*get_all_dpctl_commands(void)
1315 return all_commands
;
1318 /* Runs the command designated by argv[0] within the command table specified by
1319 * 'commands', which must be terminated by a command whose 'name' member is a
1322 dpctl_run_command(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1324 const struct dpctl_command
*p
;
1327 dpctl_error(dpctl_p
, 0, "missing command name; use --help for help");
1331 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1332 if (!strcmp(p
->name
, argv
[0])) {
1333 int n_arg
= argc
- 1;
1334 if (n_arg
< p
->min_args
) {
1335 dpctl_error(dpctl_p
, 0,
1336 "'%s' command requires at least %d arguments",
1337 p
->name
, p
->min_args
);
1339 } else if (n_arg
> p
->max_args
) {
1340 dpctl_error(dpctl_p
, 0,
1341 "'%s' command takes at most %d arguments",
1342 p
->name
, p
->max_args
);
1345 return p
->handler(argc
, argv
, dpctl_p
);
1350 dpctl_error(dpctl_p
, 0, "unknown command '%s'; use --help for help",
1356 dpctl_unixctl_print(void *userdata
, bool error OVS_UNUSED
, const char *msg
)
1358 struct ds
*ds
= userdata
;
1359 ds_put_cstr(ds
, msg
);
1363 dpctl_unixctl_handler(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1366 struct ds ds
= DS_EMPTY_INITIALIZER
;
1367 struct dpctl_params dpctl_p
;
1368 bool opt_parse_err
= false;
1370 dpctl_command_handler
*handler
= (dpctl_command_handler
*) aux
;
1372 dpctl_p
.print_statistics
= false;
1373 dpctl_p
.zero_statistics
= false;
1374 dpctl_p
.may_create
= false;
1375 dpctl_p
.verbosity
= 0;
1377 /* Parse options (like getopt). Unfortunately it does
1378 * not seem a good idea to call getopt_long() here, since it uses global
1380 while (argc
> 1 && !opt_parse_err
) {
1381 const char *arg
= argv
[1];
1382 if (!strncmp(arg
, "--", 2)) {
1384 if (!strcmp(arg
, "--statistics")) {
1385 dpctl_p
.print_statistics
= true;
1386 } else if (!strcmp(arg
, "--clear")) {
1387 dpctl_p
.zero_statistics
= true;
1388 } else if (!strcmp(arg
, "--may-create")) {
1389 dpctl_p
.may_create
= true;
1390 } else if (!strcmp(arg
, "--more")) {
1391 dpctl_p
.verbosity
++;
1393 ds_put_format(&ds
, "Unrecognized option %s", argv
[1]);
1394 opt_parse_err
= true;
1396 } else if (arg
[0] == '-' && arg
[1] != '\0') {
1397 /* Short option[s] */
1398 const char *opt
= &arg
[1];
1400 while (*opt
&& !opt_parse_err
) {
1403 dpctl_p
.verbosity
++;
1406 dpctl_p
.print_statistics
= true;
1409 ds_put_format(&ds
, "Unrecognized option -%c", *opt
);
1410 opt_parse_err
= true;
1416 /* Doesn't start with -, not an option */
1420 if (opt_parse_err
) {
1427 if (!opt_parse_err
) {
1428 dpctl_p
.is_appctl
= true;
1429 dpctl_p
.output
= dpctl_unixctl_print
;
1432 handler(argc
, argv
, &dpctl_p
);
1435 unixctl_command_reply(conn
, ds_cstr(&ds
));
1441 dpctl_unixctl_register(void)
1443 const struct dpctl_command
*p
;
1445 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1446 char *cmd_name
= xasprintf("dpctl/%s", p
->name
);
1447 unixctl_command_register(cmd_name
, "", p
->min_args
, p
->max_args
,
1448 dpctl_unixctl_handler
, p
->handler
);