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"
38 #include "netdev-dpdk.h"
41 #include "ofp-parse.h"
53 typedef int dpctl_command_handler(int argc
, const char *argv
[],
54 struct dpctl_params
*);
55 struct dpctl_command
{
60 dpctl_command_handler
*handler
;
62 static const struct dpctl_command
*get_all_dpctl_commands(void);
65 dpctl_puts(struct dpctl_params
*dpctl_p
, bool error
, const char *string
)
67 dpctl_p
->output(dpctl_p
->aux
, error
, string
);
71 dpctl_print(struct dpctl_params
*dpctl_p
, const char *fmt
, ...)
77 string
= xvasprintf(fmt
, args
);
80 dpctl_puts(dpctl_p
, false, string
);
85 dpctl_error(struct dpctl_params
* dpctl_p
, int err_no
, const char *fmt
, ...)
87 const char *subprogram_name
= get_subprogram_name();
88 struct ds ds
= DS_EMPTY_INITIALIZER
;
89 int save_errno
= errno
;
93 if (subprogram_name
[0]) {
94 ds_put_format(&ds
, "%s(%s): ", program_name
,subprogram_name
);
96 ds_put_format(&ds
, "%s: ", program_name
);
100 ds_put_format_valist(&ds
, fmt
, args
);
104 ds_put_format(&ds
, " (%s)", ovs_retval_to_string(err_no
));
106 ds_put_cstr(&ds
, "\n");
108 dpctl_puts(dpctl_p
, true, ds_cstr(&ds
));
115 static int dpctl_add_if(int argc
, const char *argv
[], struct dpctl_params
*);
118 if_up(struct netdev
*netdev
)
120 return netdev_turn_flags_on(netdev
, NETDEV_UP
, NULL
);
123 /* Retrieve the name of the datapath if exactly one exists. The caller
124 * is responsible for freeing the returned string. If there is not one
125 * datapath, aborts with an error message. */
127 get_one_dp(struct dpctl_params
*dpctl_p
)
131 char *dp_name
= NULL
;
135 dp_enumerate_types(&types
);
136 SSET_FOR_EACH (type
, &types
) {
140 if (!dp_enumerate_names(type
, &names
)) {
141 count
+= sset_count(&names
);
142 if (!dp_name
&& count
== 1) {
143 dp_name
= xasprintf("%s@%s", type
, SSET_FIRST(&names
));
146 sset_destroy(&names
);
148 sset_destroy(&types
);
151 dpctl_error(dpctl_p
, 0, "no datapaths exist");
152 } else if (count
> 1) {
153 dpctl_error(dpctl_p
, 0, "multiple datapaths, specify one");
162 parsed_dpif_open(const char *arg_
, bool create
, struct dpif
**dpifp
)
167 dp_parse_name(arg_
, &name
, &type
);
170 result
= dpif_create(name
, type
, dpifp
);
172 result
= dpif_open(name
, type
, dpifp
);
181 dpctl_add_dp(int argc
, const char *argv
[],
182 struct dpctl_params
*dpctl_p
)
187 error
= parsed_dpif_open(argv
[1], true, &dpif
);
189 dpctl_error(dpctl_p
, error
, "add_dp");
194 error
= dpctl_add_if(argc
, argv
, dpctl_p
);
200 dpctl_del_dp(int argc OVS_UNUSED
, const char *argv
[],
201 struct dpctl_params
*dpctl_p
)
206 error
= parsed_dpif_open(argv
[1], false, &dpif
);
208 dpctl_error(dpctl_p
, error
, "opening datapath");
211 error
= dpif_delete(dpif
);
213 dpctl_error(dpctl_p
, error
, "del_dp");
221 dpctl_add_if(int argc OVS_UNUSED
, const char *argv
[],
222 struct dpctl_params
*dpctl_p
)
225 int i
, error
, lasterror
= 0;
227 error
= parsed_dpif_open(argv
[1], false, &dpif
);
229 dpctl_error(dpctl_p
, error
, "opening datapath");
232 for (i
= 2; i
< argc
; i
++) {
233 const char *name
, *type
;
234 char *save_ptr
= NULL
, *argcopy
;
235 struct netdev
*netdev
= NULL
;
237 odp_port_t port_no
= ODPP_NONE
;
240 argcopy
= xstrdup(argv
[i
]);
241 name
= strtok_r(argcopy
, ",", &save_ptr
);
245 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
252 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
253 char *save_ptr_2
= NULL
;
256 key
= strtok_r(option
, "=", &save_ptr_2
);
257 value
= strtok_r(NULL
, "", &save_ptr_2
);
262 if (!strcmp(key
, "type")) {
264 } else if (!strcmp(key
, "port_no")) {
265 port_no
= u32_to_odp(atoi(value
));
266 } else if (!smap_add_once(&args
, key
, value
)) {
267 dpctl_error(dpctl_p
, 0, "duplicate \"%s\" option", key
);
271 error
= netdev_open(name
, type
, &netdev
);
273 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
275 goto next_destroy_args
;
278 error
= netdev_set_config(netdev
, &args
, NULL
);
280 goto next_destroy_args
;
283 error
= dpif_port_add(dpif
, netdev
, &port_no
);
285 dpctl_error(dpctl_p
, error
, "adding %s to %s failed", name
,
287 goto next_destroy_args
;
290 error
= if_up(netdev
);
292 dpctl_error(dpctl_p
, error
, "%s: failed bringing interface up",
297 netdev_close(netdev
);
311 dpctl_set_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
314 int i
, error
, lasterror
= 0;
316 error
= parsed_dpif_open(argv
[1], false, &dpif
);
318 dpctl_error(dpctl_p
, error
, "opening datapath");
321 for (i
= 2; i
< argc
; i
++) {
322 struct netdev
*netdev
= NULL
;
323 struct dpif_port dpif_port
;
324 char *save_ptr
= NULL
;
333 argcopy
= xstrdup(argv
[i
]);
334 name
= strtok_r(argcopy
, ",", &save_ptr
);
336 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
341 /* Get the port's type from the datapath. */
342 error
= dpif_port_query_by_name(dpif
, name
, &dpif_port
);
344 dpctl_error(dpctl_p
, error
, "%s: failed to query port in %s", name
,
348 type
= xstrdup(dpif_port
.type
);
349 port_no
= dpif_port
.port_no
;
350 dpif_port_destroy(&dpif_port
);
352 /* Retrieve its existing configuration. */
353 error
= netdev_open(name
, type
, &netdev
);
355 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
361 error
= netdev_get_config(netdev
, &args
);
363 dpctl_error(dpctl_p
, error
, "%s: failed to fetch configuration",
365 goto next_destroy_args
;
368 /* Parse changes to configuration. */
369 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
370 char *save_ptr_2
= NULL
;
373 key
= strtok_r(option
, "=", &save_ptr_2
);
374 value
= strtok_r(NULL
, "", &save_ptr_2
);
379 if (!strcmp(key
, "type")) {
380 if (strcmp(value
, type
)) {
381 dpctl_error(dpctl_p
, 0,
382 "%s: can't change type from %s to %s",
386 } else if (!strcmp(key
, "port_no")) {
387 if (port_no
!= u32_to_odp(atoi(value
))) {
388 dpctl_error(dpctl_p
, 0, "%s: can't change port number from"
389 " %"PRIu32
" to %d", name
, port_no
, atoi(value
));
392 } else if (value
[0] == '\0') {
393 smap_remove(&args
, key
);
395 smap_replace(&args
, key
, value
);
399 /* Update configuration. */
400 error
= netdev_set_config(netdev
, &args
, NULL
);
402 goto next_destroy_args
;
408 netdev_close(netdev
);
421 get_port_number(struct dpif
*dpif
, const char *name
, odp_port_t
*port
,
422 struct dpctl_params
*dpctl_p
)
424 struct dpif_port dpif_port
;
426 if (!dpif_port_query_by_name(dpif
, name
, &dpif_port
)) {
427 *port
= dpif_port
.port_no
;
428 dpif_port_destroy(&dpif_port
);
431 dpctl_error(dpctl_p
, 0, "no port named %s", name
);
437 dpctl_del_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
440 int i
, error
, lasterror
= 0;
442 error
= parsed_dpif_open(argv
[1], false, &dpif
);
444 dpctl_error(dpctl_p
, error
, "opening datapath");
447 for (i
= 2; i
< argc
; i
++) {
448 const char *name
= argv
[i
];
451 if (!name
[strspn(name
, "0123456789")]) {
452 port
= u32_to_odp(atoi(name
));
453 } else if (!get_port_number(dpif
, name
, &port
, dpctl_p
)) {
458 error
= dpif_port_del(dpif
, port
);
460 dpctl_error(dpctl_p
, error
, "deleting port %s from %s failed",
470 print_stat(struct dpctl_params
*dpctl_p
, const char *leader
, uint64_t value
)
472 dpctl_print(dpctl_p
, "%s", leader
);
473 if (value
!= UINT64_MAX
) {
474 dpctl_print(dpctl_p
, "%"PRIu64
, value
);
476 dpctl_print(dpctl_p
, "?");
481 print_human_size(struct dpctl_params
*dpctl_p
, uint64_t value
)
483 if (value
== UINT64_MAX
) {
485 } else if (value
>= 1024ULL * 1024 * 1024 * 1024) {
486 dpctl_print(dpctl_p
, " (%.1f TiB)",
487 value
/ (1024.0 * 1024 * 1024 * 1024));
488 } else if (value
>= 1024ULL * 1024 * 1024) {
489 dpctl_print(dpctl_p
, " (%.1f GiB)", value
/ (1024.0 * 1024 * 1024));
490 } else if (value
>= 1024ULL * 1024) {
491 dpctl_print(dpctl_p
, " (%.1f MiB)", value
/ (1024.0 * 1024));
492 } else if (value
>= 1024) {
493 dpctl_print(dpctl_p
, " (%.1f KiB)", value
/ 1024.0);
498 show_dpif(struct dpif
*dpif
, struct dpctl_params
*dpctl_p
)
500 struct dpif_port_dump dump
;
501 struct dpif_port dpif_port
;
502 struct dpif_dp_stats stats
;
503 struct netdev
*netdev
;
505 dpctl_print(dpctl_p
, "%s:\n", dpif_name(dpif
));
506 if (!dpif_get_dp_stats(dpif
, &stats
)) {
507 dpctl_print(dpctl_p
, "\tlookups: hit:%"PRIu64
" missed:%"PRIu64
508 " lost:%"PRIu64
"\n\tflows: %"PRIu64
"\n",
509 stats
.n_hit
, stats
.n_missed
, stats
.n_lost
, stats
.n_flows
);
510 if (stats
.n_masks
!= UINT32_MAX
) {
511 uint64_t n_pkts
= stats
.n_hit
+ stats
.n_missed
;
512 double avg
= n_pkts
? (double) stats
.n_mask_hit
/ n_pkts
: 0.0;
514 dpctl_print(dpctl_p
, "\tmasks: hit:%"PRIu64
" total:%"PRIu32
516 stats
.n_mask_hit
, stats
.n_masks
, avg
);
520 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, dpif
) {
521 dpctl_print(dpctl_p
, "\tport %u: %s",
522 dpif_port
.port_no
, dpif_port
.name
);
524 if (strcmp(dpif_port
.type
, "system")) {
527 dpctl_print(dpctl_p
, " (%s", dpif_port
.type
);
529 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
534 error
= netdev_get_config(netdev
, &config
);
536 const struct smap_node
**nodes
;
539 nodes
= smap_sort(&config
);
540 for (i
= 0; i
< smap_count(&config
); i
++) {
541 const struct smap_node
*node
= nodes
[i
];
542 dpctl_print(dpctl_p
, "%c %s=%s", i
? ',' : ':',
543 node
->key
, node
->value
);
547 dpctl_print(dpctl_p
, ", could not retrieve configuration "
548 "(%s)", ovs_strerror(error
));
550 smap_destroy(&config
);
552 netdev_close(netdev
);
554 dpctl_print(dpctl_p
, ": open failed (%s)",
555 ovs_strerror(error
));
557 dpctl_print(dpctl_p
, ")");
559 dpctl_print(dpctl_p
, "\n");
561 if (dpctl_p
->print_statistics
) {
562 struct netdev_stats s
;
565 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
567 dpctl_print(dpctl_p
, ", open failed (%s)",
568 ovs_strerror(error
));
571 error
= netdev_get_stats(netdev
, &s
);
573 netdev_close(netdev
);
574 print_stat(dpctl_p
, "\t\tRX packets:", s
.rx_packets
);
575 print_stat(dpctl_p
, " errors:", s
.rx_errors
);
576 print_stat(dpctl_p
, " dropped:", s
.rx_dropped
);
577 print_stat(dpctl_p
, " overruns:", s
.rx_over_errors
);
578 print_stat(dpctl_p
, " frame:", s
.rx_frame_errors
);
579 dpctl_print(dpctl_p
, "\n");
581 print_stat(dpctl_p
, "\t\tTX packets:", s
.tx_packets
);
582 print_stat(dpctl_p
, " errors:", s
.tx_errors
);
583 print_stat(dpctl_p
, " dropped:", s
.tx_dropped
);
584 print_stat(dpctl_p
, " aborted:", s
.tx_aborted_errors
);
585 print_stat(dpctl_p
, " carrier:", s
.tx_carrier_errors
);
586 dpctl_print(dpctl_p
, "\n");
588 print_stat(dpctl_p
, "\t\tcollisions:", s
.collisions
);
589 dpctl_print(dpctl_p
, "\n");
591 print_stat(dpctl_p
, "\t\tRX bytes:", s
.rx_bytes
);
592 print_human_size(dpctl_p
, s
.rx_bytes
);
593 print_stat(dpctl_p
, " TX bytes:", s
.tx_bytes
);
594 print_human_size(dpctl_p
, s
.tx_bytes
);
595 dpctl_print(dpctl_p
, "\n");
597 dpctl_print(dpctl_p
, ", could not retrieve stats (%s)",
598 ovs_strerror(error
));
606 dpctl_show(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
608 int error
, lasterror
= 0;
611 for (i
= 1; i
< argc
; i
++) {
612 const char *name
= argv
[i
];
615 error
= parsed_dpif_open(name
, false, &dpif
);
617 show_dpif(dpif
, dpctl_p
);
619 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
629 dp_enumerate_types(&types
);
630 SSET_FOR_EACH (type
, &types
) {
635 error
= dp_enumerate_names(type
, &names
);
640 SSET_FOR_EACH (name
, &names
) {
643 error
= dpif_open(name
, type
, &dpif
);
645 show_dpif(dpif
, dpctl_p
);
647 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
653 sset_destroy(&names
);
655 sset_destroy(&types
);
661 dpctl_dump_dps(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
662 struct dpctl_params
*dpctl_p
)
664 struct sset dpif_names
, dpif_types
;
666 int error
, lasterror
= 0;
668 sset_init(&dpif_names
);
669 sset_init(&dpif_types
);
670 dp_enumerate_types(&dpif_types
);
672 SSET_FOR_EACH (type
, &dpif_types
) {
675 error
= dp_enumerate_names(type
, &dpif_names
);
680 SSET_FOR_EACH (name
, &dpif_names
) {
682 if (!dpif_open(name
, type
, &dpif
)) {
683 dpctl_print(dpctl_p
, "%s\n", dpif_name(dpif
));
689 sset_destroy(&dpif_names
);
690 sset_destroy(&dpif_types
);
695 format_dpif_flow(struct ds
*ds
, const struct dpif_flow
*f
, struct hmap
*ports
,
696 struct dpctl_params
*dpctl_p
)
698 if (dpctl_p
->verbosity
) {
699 if (f
->ufid_present
) {
700 odp_format_ufid(&f
->ufid
, ds
);
701 ds_put_cstr(ds
, ", ");
703 ds_put_cstr(ds
, "ufid:<empty>, ");
706 odp_flow_format(f
->key
, f
->key_len
, f
->mask
, f
->mask_len
, ports
, ds
,
708 ds_put_cstr(ds
, ", ");
710 dpif_flow_stats_format(&f
->stats
, ds
);
711 ds_put_cstr(ds
, ", actions:");
712 format_odp_actions(ds
, f
->actions
, f
->actions_len
);
716 dpctl_dump_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
723 struct flow flow_filter
;
724 struct flow_wildcards wc_filter
;
726 struct dpif_port_dump port_dump
;
727 struct dpif_port dpif_port
;
728 struct hmap portno_names
;
729 struct simap names_portno
;
731 struct dpif_flow_dump_thread
*flow_dump_thread
;
732 struct dpif_flow_dump
*flow_dump
;
734 int pmd_id
= PMD_ID_NULL
;
737 if (argc
> 1 && !strncmp(argv
[argc
- 1], "filter=", 7)) {
738 filter
= xstrdup(argv
[--argc
] + 7);
740 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
746 error
= parsed_dpif_open(name
, false, &dpif
);
749 dpctl_error(dpctl_p
, error
, "opening datapath");
754 hmap_init(&portno_names
);
755 simap_init(&names_portno
);
756 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
757 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
758 simap_put(&names_portno
, dpif_port
.name
,
759 odp_to_u32(dpif_port
.port_no
));
763 char *err
= parse_ofp_exact_flow(&flow_filter
, &wc_filter
.masks
,
764 filter
, &names_portno
);
766 dpctl_error(dpctl_p
, 0, "Failed to parse filter (%s)", err
);
773 flow_dump
= dpif_flow_dump_create(dpif
, false);
774 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
775 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
778 struct flow_wildcards wc
;
779 struct match match
, match_filter
;
780 struct minimatch minimatch
;
782 odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
);
783 odp_flow_key_to_mask(f
.mask
, f
.mask_len
, &wc
.masks
, &flow
);
784 match_init(&match
, &flow
, &wc
);
786 match_init(&match_filter
, &flow_filter
, &wc
);
787 match_init(&match_filter
, &match_filter
.flow
, &wc_filter
);
788 minimatch_init(&minimatch
, &match_filter
);
790 if (!minimatch_matches_flow(&minimatch
, &match
.flow
)) {
791 minimatch_destroy(&minimatch
);
794 minimatch_destroy(&minimatch
);
797 /* If 'pmd_id' is specified, overlapping flows could be dumped from
798 * different pmd threads. So, separates dumps from different pmds
799 * by printing a title line. */
800 if (pmd_id
!= f
.pmd_id
) {
801 if (f
.pmd_id
== NON_PMD_CORE_ID
) {
802 ds_put_format(&ds
, "flow-dump from non-dpdk interfaces:\n");
804 ds_put_format(&ds
, "flow-dump from pmd on cpu core: %d\n",
809 format_dpif_flow(&ds
, &f
, &portno_names
, dpctl_p
);
810 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
812 dpif_flow_dump_thread_destroy(flow_dump_thread
);
813 error
= dpif_flow_dump_destroy(flow_dump
);
816 dpctl_error(dpctl_p
, error
, "Failed to dump flows from datapath");
821 odp_portno_names_destroy(&portno_names
);
822 simap_destroy(&names_portno
);
823 hmap_destroy(&portno_names
);
830 /* Extracts the in_port from the parsed keys, and returns the reference
831 * to the 'struct netdev *' of the dpif port. On error, returns NULL.
832 * Users must call 'netdev_close()' after finish using the returned
834 static struct netdev
*
835 get_in_port_netdev_from_key(struct dpif
*dpif
, const struct ofpbuf
*key
)
837 const struct nlattr
*in_port_nla
;
838 struct netdev
*dev
= NULL
;
840 in_port_nla
= nl_attr_find(key
, 0, OVS_KEY_ATTR_IN_PORT
);
842 struct dpif_port dpif_port
;
846 port_no
= ODP_PORT_C(nl_attr_get_u32(in_port_nla
));
847 error
= dpif_port_query_by_number(dpif
, port_no
, &dpif_port
);
852 netdev_open(dpif_port
.name
, dpif_port
.type
, &dev
);
853 dpif_port_destroy(&dpif_port
);
861 dpctl_put_flow(int argc
, const char *argv
[], enum dpif_flow_put_flags flags
,
862 struct dpctl_params
*dpctl_p
)
864 const char *key_s
= argv
[argc
- 2];
865 const char *actions_s
= argv
[argc
- 1];
866 struct netdev
*in_port_netdev
= NULL
;
867 struct dpif_flow_stats stats
;
868 struct dpif_port dpif_port
;
869 struct dpif_port_dump port_dump
;
870 struct ofpbuf actions
;
877 struct simap port_names
;
880 dp_name
= argc
== 4 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
884 error
= parsed_dpif_open(dp_name
, false, &dpif
);
887 dpctl_error(dpctl_p
, error
, "opening datapath");
891 ufid_present
= false;
892 n
= odp_ufid_from_string(key_s
, &ufid
);
894 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
901 simap_init(&port_names
);
902 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
903 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
906 ofpbuf_init(&key
, 0);
907 ofpbuf_init(&mask
, 0);
908 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
909 simap_destroy(&port_names
);
911 dpctl_error(dpctl_p
, error
, "parsing flow key");
912 goto out_freekeymask
;
915 ofpbuf_init(&actions
, 0);
916 error
= odp_actions_from_string(actions_s
, NULL
, &actions
);
918 dpctl_error(dpctl_p
, error
, "parsing actions");
919 goto out_freeactions
;
922 /* For DPDK interface, applies the operation to all pmd threads
923 * on the same numa node. */
924 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
925 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
928 numa_id
= netdev_get_numa_id(in_port_netdev
);
929 if (ovs_numa_numa_id_is_valid(numa_id
)) {
930 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
931 struct ovs_numa_info
*iter
;
933 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
934 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
935 error
= dpif_flow_put(dpif
, flags
,
937 mask
.size
== 0 ? NULL
: mask
.data
,
938 mask
.size
, actions
.data
,
939 actions
.size
, ufid_present
? &ufid
: NULL
,
940 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
943 ovs_numa_dump_destroy(dump
);
948 error
= dpif_flow_put(dpif
, flags
,
950 mask
.size
== 0 ? NULL
: mask
.data
,
951 mask
.size
, actions
.data
,
952 actions
.size
, ufid_present
? &ufid
: NULL
,
953 PMD_ID_NULL
, dpctl_p
->print_statistics
? &stats
: NULL
);
956 dpctl_error(dpctl_p
, error
, "updating flow table");
957 goto out_freeactions
;
960 if (dpctl_p
->print_statistics
) {
964 dpif_flow_stats_format(&stats
, &s
);
965 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
970 ofpbuf_uninit(&actions
);
972 ofpbuf_uninit(&mask
);
975 netdev_close(in_port_netdev
);
980 dpctl_add_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
982 return dpctl_put_flow(argc
, argv
, DPIF_FP_CREATE
, dpctl_p
);
986 dpctl_mod_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
988 enum dpif_flow_put_flags flags
;
990 flags
= DPIF_FP_MODIFY
;
991 if (dpctl_p
->may_create
) {
992 flags
|= DPIF_FP_CREATE
;
994 if (dpctl_p
->zero_statistics
) {
995 flags
|= DPIF_FP_ZERO_STATS
;
998 return dpctl_put_flow(argc
, argv
, flags
, dpctl_p
);
1002 dpctl_get_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1004 const char *key_s
= argv
[argc
- 1];
1005 struct dpif_flow flow
;
1006 struct dpif_port dpif_port
;
1007 struct dpif_port_dump port_dump
;
1010 struct hmap portno_names
;
1013 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1017 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1021 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1024 dpctl_error(dpctl_p
, error
, "opening datapath");
1028 ofpbuf_use_stub(&buf
, &stub
, sizeof stub
);
1029 hmap_init(&portno_names
);
1030 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1031 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
1034 n
= odp_ufid_from_string(key_s
, &ufid
);
1036 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1040 /* Does not work for DPDK, since do not know which 'pmd' to apply the
1041 * operation. So, just uses PMD_ID_NULL. */
1042 error
= dpif_flow_get(dpif
, NULL
, 0, &ufid
, PMD_ID_NULL
, &buf
, &flow
);
1044 dpctl_error(dpctl_p
, error
, "getting flow");
1049 format_dpif_flow(&ds
, &flow
, &portno_names
, dpctl_p
);
1050 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
1054 odp_portno_names_destroy(&portno_names
);
1055 hmap_destroy(&portno_names
);
1056 ofpbuf_uninit(&buf
);
1062 dpctl_del_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1064 const char *key_s
= argv
[argc
- 1];
1065 struct netdev
*in_port_netdev
= NULL
;
1066 struct dpif_flow_stats stats
;
1067 struct dpif_port dpif_port
;
1068 struct dpif_port_dump port_dump
;
1070 struct ofpbuf mask
; /* To be ignored. */
1075 struct simap port_names
;
1078 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1082 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1085 dpctl_error(dpctl_p
, error
, "opening datapath");
1089 ufid_present
= false;
1090 n
= odp_ufid_from_string(key_s
, &ufid
);
1092 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1096 ufid_present
= true;
1099 simap_init(&port_names
);
1100 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1101 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
1104 ofpbuf_init(&key
, 0);
1105 ofpbuf_init(&mask
, 0);
1107 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
1109 dpctl_error(dpctl_p
, error
, "parsing flow key");
1113 /* For DPDK interface, applies the operation to all pmd threads
1114 * on the same numa node. */
1115 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
1116 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
1119 numa_id
= netdev_get_numa_id(in_port_netdev
);
1120 if (ovs_numa_numa_id_is_valid(numa_id
)) {
1121 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
1122 struct ovs_numa_info
*iter
;
1124 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
1125 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
1126 error
= dpif_flow_del(dpif
, key
.data
,
1127 key
.size
, ufid_present
? &ufid
: NULL
,
1128 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
1131 ovs_numa_dump_destroy(dump
);
1136 error
= dpif_flow_del(dpif
, key
.data
, key
.size
,
1137 ufid_present
? &ufid
: NULL
, PMD_ID_NULL
,
1138 dpctl_p
->print_statistics
? &stats
: NULL
);
1141 dpctl_error(dpctl_p
, error
, "deleting flow");
1142 if (error
== ENOENT
&& !ufid_present
) {
1146 ds_put_format(&s
, "Perhaps you need to specify a UFID?");
1147 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1153 if (dpctl_p
->print_statistics
) {
1157 dpif_flow_stats_format(&stats
, &s
);
1158 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1163 ofpbuf_uninit(&mask
);
1164 ofpbuf_uninit(&key
);
1165 simap_destroy(&port_names
);
1167 netdev_close(in_port_netdev
);
1172 dpctl_del_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1178 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1182 error
= parsed_dpif_open(name
, false, &dpif
);
1185 dpctl_error(dpctl_p
, error
, "opening datapath");
1189 error
= dpif_flow_flush(dpif
);
1191 dpctl_error(dpctl_p
, error
, "deleting all flows");
1198 dpctl_help(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1199 struct dpctl_params
*dpctl_p
)
1201 if (dpctl_p
->usage
) {
1202 dpctl_p
->usage(dpctl_p
->aux
);
1209 dpctl_list_commands(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1210 struct dpctl_params
*dpctl_p
)
1212 struct ds ds
= DS_EMPTY_INITIALIZER
;
1213 const struct dpctl_command
*commands
= get_all_dpctl_commands();
1215 ds_put_cstr(&ds
, "The available commands are:\n");
1216 for (; commands
->name
; commands
++) {
1217 const struct dpctl_command
*c
= commands
;
1219 ds_put_format(&ds
, " %s%-23s %s\n", dpctl_p
->is_appctl
? "dpctl/" : "",
1222 dpctl_puts(dpctl_p
, false, ds
.string
);
1228 /* Undocumented commands for unit testing. */
1231 dpctl_parse_actions(int argc
, const char *argv
[], struct dpctl_params
* dpctl_p
)
1235 for (i
= 1; i
< argc
; i
++) {
1236 struct ofpbuf actions
;
1239 ofpbuf_init(&actions
, 0);
1240 error
= odp_actions_from_string(argv
[i
], NULL
, &actions
);
1243 ofpbuf_uninit(&actions
);
1244 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1249 format_odp_actions(&s
, actions
.data
, actions
.size
);
1250 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1253 ofpbuf_uninit(&actions
);
1259 struct actions_for_flow
{
1260 struct hmap_node hmap_node
;
1262 struct ofpbuf actions
;
1265 static struct actions_for_flow
*
1266 get_actions_for_flow(struct hmap
*actions_per_flow
, const struct flow
*flow
)
1268 uint32_t hash
= flow_hash(flow
, 0);
1269 struct actions_for_flow
*af
;
1271 HMAP_FOR_EACH_WITH_HASH (af
, hmap_node
, hash
, actions_per_flow
) {
1272 if (flow_equal(&af
->flow
, flow
)) {
1277 af
= xmalloc(sizeof *af
);
1279 ofpbuf_init(&af
->actions
, 0);
1280 hmap_insert(actions_per_flow
, &af
->hmap_node
, hash
);
1285 compare_actions_for_flow(const void *a_
, const void *b_
)
1287 struct actions_for_flow
*const *a
= a_
;
1288 struct actions_for_flow
*const *b
= b_
;
1290 return flow_compare_3way(&(*a
)->flow
, &(*b
)->flow
);
1294 compare_output_actions(const void *a_
, const void *b_
)
1296 const struct nlattr
*a
= a_
;
1297 const struct nlattr
*b
= b_
;
1298 uint32_t a_port
= nl_attr_get_u32(a
);
1299 uint32_t b_port
= nl_attr_get_u32(b
);
1301 return a_port
< b_port
? -1 : a_port
> b_port
;
1305 sort_output_actions__(struct nlattr
*first
, struct nlattr
*end
)
1307 size_t bytes
= (uint8_t *) end
- (uint8_t *) first
;
1308 size_t n
= bytes
/ NL_A_U32_SIZE
;
1310 ovs_assert(bytes
% NL_A_U32_SIZE
== 0);
1311 qsort(first
, n
, NL_A_U32_SIZE
, compare_output_actions
);
1315 sort_output_actions(struct nlattr
*actions
, size_t length
)
1317 struct nlattr
*first_output
= NULL
;
1321 NL_ATTR_FOR_EACH (a
, left
, actions
, length
) {
1322 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
) {
1323 if (!first_output
) {
1328 sort_output_actions__(first_output
, a
);
1329 first_output
= NULL
;
1334 uint8_t *end
= (uint8_t *) actions
+ length
;
1335 sort_output_actions__(first_output
,
1336 ALIGNED_CAST(struct nlattr
*, end
));
1340 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1341 * have the syntax used by "ovs-dpctl dump-flows".
1343 * This command prints ACTIONS in a format that shows what happens for each
1344 * VLAN, independent of the order of the ACTIONS. For example, there is more
1345 * than one way to output a packet on VLANs 9 and 11, but this command will
1346 * print the same output for any form.
1348 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1349 * so far the implementation only covers VLANs. */
1351 dpctl_normalize_actions(int argc
, const char *argv
[],
1352 struct dpctl_params
*dpctl_p
)
1354 struct simap port_names
;
1355 struct ofpbuf keybuf
;
1357 struct ofpbuf odp_actions
;
1358 struct hmap actions_per_flow
;
1359 struct actions_for_flow
**afs
;
1360 struct actions_for_flow
*af
;
1369 simap_init(&port_names
);
1370 for (i
= 3; i
< argc
; i
++) {
1374 if (ovs_scan(argv
[i
], "%15[^=]=%d", name
, &number
)) {
1375 uintptr_t n
= number
;
1376 simap_put(&port_names
, name
, n
);
1378 dpctl_error(dpctl_p
, 0, "%s: expected NAME=NUMBER", argv
[i
]);
1384 /* Parse flow key. */
1385 ofpbuf_init(&keybuf
, 0);
1386 error
= odp_flow_from_string(argv
[1], &port_names
, &keybuf
, NULL
);
1388 dpctl_error(dpctl_p
, error
, "odp_flow_key_from_string");
1389 goto out_freekeybuf
;
1393 odp_flow_format(keybuf
.data
, keybuf
.size
, NULL
, 0, NULL
,
1394 &s
, dpctl_p
->verbosity
);
1395 dpctl_print(dpctl_p
, "input flow: %s\n", ds_cstr(&s
));
1397 error
= odp_flow_key_to_flow(keybuf
.data
, keybuf
.size
, &flow
);
1399 dpctl_error(dpctl_p
, error
, "odp_flow_key_to_flow");
1400 goto out_freekeybuf
;
1403 /* Parse actions. */
1404 ofpbuf_init(&odp_actions
, 0);
1405 error
= odp_actions_from_string(argv
[2], &port_names
, &odp_actions
);
1407 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1408 goto out_freeactions
;
1411 if (dpctl_p
->verbosity
) {
1413 format_odp_actions(&s
, odp_actions
.data
, odp_actions
.size
);
1414 dpctl_print(dpctl_p
, "input actions: %s\n", ds_cstr(&s
));
1417 hmap_init(&actions_per_flow
);
1418 NL_ATTR_FOR_EACH (a
, left
, odp_actions
.data
, odp_actions
.size
) {
1419 const struct ovs_action_push_vlan
*push
;
1420 switch(nl_attr_type(a
)) {
1421 case OVS_ACTION_ATTR_POP_VLAN
:
1422 flow
.vlan_tci
= htons(0);
1425 case OVS_ACTION_ATTR_PUSH_VLAN
:
1426 push
= nl_attr_get_unspec(a
, sizeof *push
);
1427 flow
.vlan_tci
= push
->vlan_tci
;
1431 af
= get_actions_for_flow(&actions_per_flow
, &flow
);
1432 nl_msg_put_unspec(&af
->actions
, nl_attr_type(a
),
1433 nl_attr_get(a
), nl_attr_get_size(a
));
1436 n_afs
= hmap_count(&actions_per_flow
);
1437 afs
= xmalloc(n_afs
* sizeof *afs
);
1439 HMAP_FOR_EACH (af
, hmap_node
, &actions_per_flow
) {
1443 ovs_assert(i
== n_afs
);
1444 hmap_destroy(&actions_per_flow
);
1446 qsort(afs
, n_afs
, sizeof *afs
, compare_actions_for_flow
);
1448 for (i
= 0; i
< n_afs
; i
++) {
1449 struct actions_for_flow
*af
= afs
[i
];
1451 sort_output_actions(af
->actions
.data
, af
->actions
.size
);
1453 if (af
->flow
.vlan_tci
!= htons(0)) {
1454 dpctl_print(dpctl_p
, "vlan(vid=%"PRIu16
",pcp=%d): ",
1455 vlan_tci_to_vid(af
->flow
.vlan_tci
),
1456 vlan_tci_to_pcp(af
->flow
.vlan_tci
));
1458 dpctl_print(dpctl_p
, "no vlan: ");
1461 if (eth_type_mpls(af
->flow
.dl_type
)) {
1462 dpctl_print(dpctl_p
, "mpls(label=%"PRIu32
",tc=%d,ttl=%d): ",
1463 mpls_lse_to_label(af
->flow
.mpls_lse
[0]),
1464 mpls_lse_to_tc(af
->flow
.mpls_lse
[0]),
1465 mpls_lse_to_ttl(af
->flow
.mpls_lse
[0]));
1467 dpctl_print(dpctl_p
, "no mpls: ");
1471 format_odp_actions(&s
, af
->actions
.data
, af
->actions
.size
);
1472 dpctl_print(dpctl_p
, ds_cstr(&s
));
1474 ofpbuf_uninit(&af
->actions
);
1481 ofpbuf_uninit(&odp_actions
);
1483 ofpbuf_uninit(&keybuf
);
1485 simap_destroy(&port_names
);
1491 static const struct dpctl_command all_commands
[] = {
1492 { "add-dp", "add-dp dp [iface...]", 1, INT_MAX
, dpctl_add_dp
},
1493 { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp
},
1494 { "add-if", "add-if dp iface...", 2, INT_MAX
, dpctl_add_if
},
1495 { "del-if", "del-if dp iface...", 2, INT_MAX
, dpctl_del_if
},
1496 { "set-if", "set-if dp iface...", 2, INT_MAX
, dpctl_set_if
},
1497 { "dump-dps", "", 0, 0, dpctl_dump_dps
},
1498 { "show", "[dp...]", 0, INT_MAX
, dpctl_show
},
1499 { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows
},
1500 { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow
},
1501 { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow
},
1502 { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow
},
1503 { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow
},
1504 { "del-flows", "[dp]", 0, 1, dpctl_del_flows
},
1505 { "help", "", 0, INT_MAX
, dpctl_help
},
1506 { "list-commands", "", 0, INT_MAX
, dpctl_list_commands
},
1508 /* Undocumented commands for testing. */
1509 { "parse-actions", "actions", 1, INT_MAX
, dpctl_parse_actions
},
1510 { "normalize-actions", "actions", 2, INT_MAX
, dpctl_normalize_actions
},
1512 { NULL
, NULL
, 0, 0, NULL
},
1515 static const struct dpctl_command
*get_all_dpctl_commands(void)
1517 return all_commands
;
1520 /* Runs the command designated by argv[0] within the command table specified by
1521 * 'commands', which must be terminated by a command whose 'name' member is a
1524 dpctl_run_command(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1526 const struct dpctl_command
*p
;
1529 dpctl_error(dpctl_p
, 0, "missing command name; use --help for help");
1533 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1534 if (!strcmp(p
->name
, argv
[0])) {
1535 int n_arg
= argc
- 1;
1536 if (n_arg
< p
->min_args
) {
1537 dpctl_error(dpctl_p
, 0,
1538 "'%s' command requires at least %d arguments",
1539 p
->name
, p
->min_args
);
1541 } else if (n_arg
> p
->max_args
) {
1542 dpctl_error(dpctl_p
, 0,
1543 "'%s' command takes at most %d arguments",
1544 p
->name
, p
->max_args
);
1547 return p
->handler(argc
, argv
, dpctl_p
);
1552 dpctl_error(dpctl_p
, 0, "unknown command '%s'; use --help for help",
1558 dpctl_unixctl_print(void *userdata
, bool error OVS_UNUSED
, const char *msg
)
1560 struct ds
*ds
= userdata
;
1561 ds_put_cstr(ds
, msg
);
1565 dpctl_unixctl_handler(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1568 struct ds ds
= DS_EMPTY_INITIALIZER
;
1569 struct dpctl_params dpctl_p
;
1570 bool opt_parse_err
= false;
1572 dpctl_command_handler
*handler
= (dpctl_command_handler
*) aux
;
1574 dpctl_p
.print_statistics
= false;
1575 dpctl_p
.zero_statistics
= false;
1576 dpctl_p
.may_create
= false;
1577 dpctl_p
.verbosity
= 0;
1579 /* Parse options (like getopt). Unfortunately it does
1580 * not seem a good idea to call getopt_long() here, since it uses global
1582 while (argc
> 1 && !opt_parse_err
) {
1583 const char *arg
= argv
[1];
1584 if (!strncmp(arg
, "--", 2)) {
1586 if (!strcmp(arg
, "--statistics")) {
1587 dpctl_p
.print_statistics
= true;
1588 } else if (!strcmp(arg
, "--clear")) {
1589 dpctl_p
.zero_statistics
= true;
1590 } else if (!strcmp(arg
, "--may-create")) {
1591 dpctl_p
.may_create
= true;
1592 } else if (!strcmp(arg
, "--more")) {
1593 dpctl_p
.verbosity
++;
1595 ds_put_format(&ds
, "Unrecognized option %s", argv
[1]);
1596 opt_parse_err
= true;
1598 } else if (arg
[0] == '-' && arg
[1] != '\0') {
1599 /* Short option[s] */
1600 const char *opt
= &arg
[1];
1602 while (*opt
&& !opt_parse_err
) {
1605 dpctl_p
.verbosity
++;
1608 dpctl_p
.print_statistics
= true;
1611 ds_put_format(&ds
, "Unrecognized option -%c", *opt
);
1612 opt_parse_err
= true;
1618 /* Doesn't start with -, not an option */
1622 if (opt_parse_err
) {
1629 if (!opt_parse_err
) {
1630 dpctl_p
.is_appctl
= true;
1631 dpctl_p
.output
= dpctl_unixctl_print
;
1634 handler(argc
, argv
, &dpctl_p
);
1637 unixctl_command_reply(conn
, ds_cstr(&ds
));
1643 dpctl_unixctl_register(void)
1645 const struct dpctl_command
*p
;
1647 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1648 char *cmd_name
= xasprintf("dpctl/%s", p
->name
);
1649 unixctl_command_register(cmd_name
, "", p
->min_args
, p
->max_args
,
1650 dpctl_unixctl_handler
, p
->handler
);