2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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);
63 static void dpctl_print(struct dpctl_params
*dpctl_p
, const char *fmt
, ...)
64 OVS_PRINTF_FORMAT(2, 3);
65 static void dpctl_error(struct dpctl_params
* dpctl_p
, int err_no
,
67 OVS_PRINTF_FORMAT(3, 4);
70 dpctl_puts(struct dpctl_params
*dpctl_p
, bool error
, const char *string
)
72 dpctl_p
->output(dpctl_p
->aux
, error
, string
);
76 dpctl_print(struct dpctl_params
*dpctl_p
, const char *fmt
, ...)
82 string
= xvasprintf(fmt
, args
);
85 dpctl_puts(dpctl_p
, false, string
);
90 dpctl_error(struct dpctl_params
* dpctl_p
, int err_no
, const char *fmt
, ...)
92 const char *subprogram_name
= get_subprogram_name();
93 struct ds ds
= DS_EMPTY_INITIALIZER
;
94 int save_errno
= errno
;
98 if (subprogram_name
[0]) {
99 ds_put_format(&ds
, "%s(%s): ", program_name
,subprogram_name
);
101 ds_put_format(&ds
, "%s: ", program_name
);
105 ds_put_format_valist(&ds
, fmt
, args
);
109 ds_put_format(&ds
, " (%s)", ovs_retval_to_string(err_no
));
111 ds_put_cstr(&ds
, "\n");
113 dpctl_puts(dpctl_p
, true, ds_cstr(&ds
));
120 static int dpctl_add_if(int argc
, const char *argv
[], struct dpctl_params
*);
123 if_up(struct netdev
*netdev
)
125 return netdev_turn_flags_on(netdev
, NETDEV_UP
, NULL
);
128 /* Retrieve the name of the datapath if exactly one exists. The caller
129 * is responsible for freeing the returned string. If there is not one
130 * datapath, aborts with an error message. */
132 get_one_dp(struct dpctl_params
*dpctl_p
)
136 char *dp_name
= NULL
;
140 dp_enumerate_types(&types
);
141 SSET_FOR_EACH (type
, &types
) {
145 if (!dp_enumerate_names(type
, &names
)) {
146 count
+= sset_count(&names
);
147 if (!dp_name
&& count
== 1) {
148 dp_name
= xasprintf("%s@%s", type
, SSET_FIRST(&names
));
151 sset_destroy(&names
);
153 sset_destroy(&types
);
156 dpctl_error(dpctl_p
, 0, "no datapaths exist");
157 } else if (count
> 1) {
158 dpctl_error(dpctl_p
, 0, "multiple datapaths, specify one");
167 parsed_dpif_open(const char *arg_
, bool create
, struct dpif
**dpifp
)
172 dp_parse_name(arg_
, &name
, &type
);
175 result
= dpif_create(name
, type
, dpifp
);
177 result
= dpif_open(name
, type
, dpifp
);
186 dpctl_add_dp(int argc
, const char *argv
[],
187 struct dpctl_params
*dpctl_p
)
192 error
= parsed_dpif_open(argv
[1], true, &dpif
);
194 dpctl_error(dpctl_p
, error
, "add_dp");
199 error
= dpctl_add_if(argc
, argv
, dpctl_p
);
205 dpctl_del_dp(int argc OVS_UNUSED
, const char *argv
[],
206 struct dpctl_params
*dpctl_p
)
211 error
= parsed_dpif_open(argv
[1], false, &dpif
);
213 dpctl_error(dpctl_p
, error
, "opening datapath");
216 error
= dpif_delete(dpif
);
218 dpctl_error(dpctl_p
, error
, "del_dp");
226 dpctl_add_if(int argc OVS_UNUSED
, const char *argv
[],
227 struct dpctl_params
*dpctl_p
)
230 int i
, error
, lasterror
= 0;
232 error
= parsed_dpif_open(argv
[1], false, &dpif
);
234 dpctl_error(dpctl_p
, error
, "opening datapath");
237 for (i
= 2; i
< argc
; i
++) {
238 const char *name
, *type
;
239 char *save_ptr
= NULL
, *argcopy
;
240 struct netdev
*netdev
= NULL
;
242 odp_port_t port_no
= ODPP_NONE
;
245 argcopy
= xstrdup(argv
[i
]);
246 name
= strtok_r(argcopy
, ",", &save_ptr
);
250 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
257 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
258 char *save_ptr_2
= NULL
;
261 key
= strtok_r(option
, "=", &save_ptr_2
);
262 value
= strtok_r(NULL
, "", &save_ptr_2
);
267 if (!strcmp(key
, "type")) {
269 } else if (!strcmp(key
, "port_no")) {
270 port_no
= u32_to_odp(atoi(value
));
271 } else if (!smap_add_once(&args
, key
, value
)) {
272 dpctl_error(dpctl_p
, 0, "duplicate \"%s\" option", key
);
276 error
= netdev_open(name
, type
, &netdev
);
278 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
280 goto next_destroy_args
;
283 error
= netdev_set_config(netdev
, &args
, NULL
);
285 goto next_destroy_args
;
288 error
= dpif_port_add(dpif
, netdev
, &port_no
);
290 dpctl_error(dpctl_p
, error
, "adding %s to %s failed", name
,
292 goto next_destroy_args
;
295 error
= if_up(netdev
);
297 dpctl_error(dpctl_p
, error
, "%s: failed bringing interface up",
302 netdev_close(netdev
);
316 dpctl_set_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
319 int i
, error
, lasterror
= 0;
321 error
= parsed_dpif_open(argv
[1], false, &dpif
);
323 dpctl_error(dpctl_p
, error
, "opening datapath");
326 for (i
= 2; i
< argc
; i
++) {
327 struct netdev
*netdev
= NULL
;
328 struct dpif_port dpif_port
;
329 char *save_ptr
= NULL
;
338 argcopy
= xstrdup(argv
[i
]);
339 name
= strtok_r(argcopy
, ",", &save_ptr
);
341 dpctl_error(dpctl_p
, 0, "%s is not a valid network device name",
346 /* Get the port's type from the datapath. */
347 error
= dpif_port_query_by_name(dpif
, name
, &dpif_port
);
349 dpctl_error(dpctl_p
, error
, "%s: failed to query port in %s", name
,
353 type
= xstrdup(dpif_port
.type
);
354 port_no
= dpif_port
.port_no
;
355 dpif_port_destroy(&dpif_port
);
357 /* Retrieve its existing configuration. */
358 error
= netdev_open(name
, type
, &netdev
);
360 dpctl_error(dpctl_p
, error
, "%s: failed to open network device",
366 error
= netdev_get_config(netdev
, &args
);
368 dpctl_error(dpctl_p
, error
, "%s: failed to fetch configuration",
370 goto next_destroy_args
;
373 /* Parse changes to configuration. */
374 while ((option
= strtok_r(NULL
, ",", &save_ptr
)) != NULL
) {
375 char *save_ptr_2
= NULL
;
378 key
= strtok_r(option
, "=", &save_ptr_2
);
379 value
= strtok_r(NULL
, "", &save_ptr_2
);
384 if (!strcmp(key
, "type")) {
385 if (strcmp(value
, type
)) {
386 dpctl_error(dpctl_p
, 0,
387 "%s: can't change type from %s to %s",
390 goto next_destroy_args
;
392 } else if (!strcmp(key
, "port_no")) {
393 if (port_no
!= u32_to_odp(atoi(value
))) {
394 dpctl_error(dpctl_p
, 0, "%s: can't change port number from"
395 " %"PRIu32
" to %d", name
, port_no
, atoi(value
));
397 goto next_destroy_args
;
399 } else if (value
[0] == '\0') {
400 smap_remove(&args
, key
);
402 smap_replace(&args
, key
, value
);
406 /* Update configuration. */
408 error
= netdev_set_config(netdev
, &args
, &err_s
);
409 if (err_s
|| error
) {
410 dpctl_error(dpctl_p
, error
, "%s",
411 err_s
? err_s
: "Error updating configuration");
415 goto next_destroy_args
;
421 netdev_close(netdev
);
434 get_port_number(struct dpif
*dpif
, const char *name
, odp_port_t
*port
,
435 struct dpctl_params
*dpctl_p
)
437 struct dpif_port dpif_port
;
439 if (!dpif_port_query_by_name(dpif
, name
, &dpif_port
)) {
440 *port
= dpif_port
.port_no
;
441 dpif_port_destroy(&dpif_port
);
444 dpctl_error(dpctl_p
, 0, "no port named %s", name
);
450 dpctl_del_if(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
453 int i
, error
, lasterror
= 0;
455 error
= parsed_dpif_open(argv
[1], false, &dpif
);
457 dpctl_error(dpctl_p
, error
, "opening datapath");
460 for (i
= 2; i
< argc
; i
++) {
461 const char *name
= argv
[i
];
464 if (!name
[strspn(name
, "0123456789")]) {
465 port
= u32_to_odp(atoi(name
));
466 } else if (!get_port_number(dpif
, name
, &port
, dpctl_p
)) {
471 error
= dpif_port_del(dpif
, port
);
473 dpctl_error(dpctl_p
, error
, "deleting port %s from %s failed",
483 print_stat(struct dpctl_params
*dpctl_p
, const char *leader
, uint64_t value
)
485 dpctl_print(dpctl_p
, "%s", leader
);
486 if (value
!= UINT64_MAX
) {
487 dpctl_print(dpctl_p
, "%"PRIu64
, value
);
489 dpctl_print(dpctl_p
, "?");
494 print_human_size(struct dpctl_params
*dpctl_p
, uint64_t value
)
496 if (value
== UINT64_MAX
) {
498 } else if (value
>= 1024ULL * 1024 * 1024 * 1024) {
499 dpctl_print(dpctl_p
, " (%.1f TiB)",
500 value
/ (1024.0 * 1024 * 1024 * 1024));
501 } else if (value
>= 1024ULL * 1024 * 1024) {
502 dpctl_print(dpctl_p
, " (%.1f GiB)", value
/ (1024.0 * 1024 * 1024));
503 } else if (value
>= 1024ULL * 1024) {
504 dpctl_print(dpctl_p
, " (%.1f MiB)", value
/ (1024.0 * 1024));
505 } else if (value
>= 1024) {
506 dpctl_print(dpctl_p
, " (%.1f KiB)", value
/ 1024.0);
511 show_dpif(struct dpif
*dpif
, struct dpctl_params
*dpctl_p
)
513 struct dpif_port_dump dump
;
514 struct dpif_port dpif_port
;
515 struct dpif_dp_stats stats
;
516 struct netdev
*netdev
;
518 dpctl_print(dpctl_p
, "%s:\n", dpif_name(dpif
));
519 if (!dpif_get_dp_stats(dpif
, &stats
)) {
520 dpctl_print(dpctl_p
, "\tlookups: hit:%"PRIu64
" missed:%"PRIu64
521 " lost:%"PRIu64
"\n\tflows: %"PRIu64
"\n",
522 stats
.n_hit
, stats
.n_missed
, stats
.n_lost
, stats
.n_flows
);
523 if (stats
.n_masks
!= UINT32_MAX
) {
524 uint64_t n_pkts
= stats
.n_hit
+ stats
.n_missed
;
525 double avg
= n_pkts
? (double) stats
.n_mask_hit
/ n_pkts
: 0.0;
527 dpctl_print(dpctl_p
, "\tmasks: hit:%"PRIu64
" total:%"PRIu32
529 stats
.n_mask_hit
, stats
.n_masks
, avg
);
533 DPIF_PORT_FOR_EACH (&dpif_port
, &dump
, dpif
) {
534 dpctl_print(dpctl_p
, "\tport %u: %s",
535 dpif_port
.port_no
, dpif_port
.name
);
537 if (strcmp(dpif_port
.type
, "system")) {
540 dpctl_print(dpctl_p
, " (%s", dpif_port
.type
);
542 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
547 error
= netdev_get_config(netdev
, &config
);
549 const struct smap_node
**nodes
;
552 nodes
= smap_sort(&config
);
553 for (i
= 0; i
< smap_count(&config
); i
++) {
554 const struct smap_node
*node
= nodes
[i
];
555 dpctl_print(dpctl_p
, "%c %s=%s", i
? ',' : ':',
556 node
->key
, node
->value
);
560 dpctl_print(dpctl_p
, ", could not retrieve configuration "
561 "(%s)", ovs_strerror(error
));
563 smap_destroy(&config
);
565 netdev_close(netdev
);
567 dpctl_print(dpctl_p
, ": open failed (%s)",
568 ovs_strerror(error
));
570 dpctl_print(dpctl_p
, ")");
572 dpctl_print(dpctl_p
, "\n");
574 if (dpctl_p
->print_statistics
) {
575 struct netdev_stats s
;
578 error
= netdev_open(dpif_port
.name
, dpif_port
.type
, &netdev
);
580 dpctl_print(dpctl_p
, ", open failed (%s)",
581 ovs_strerror(error
));
584 error
= netdev_get_stats(netdev
, &s
);
586 netdev_close(netdev
);
587 print_stat(dpctl_p
, "\t\tRX packets:", s
.rx_packets
);
588 print_stat(dpctl_p
, " errors:", s
.rx_errors
);
589 print_stat(dpctl_p
, " dropped:", s
.rx_dropped
);
590 print_stat(dpctl_p
, " overruns:", s
.rx_over_errors
);
591 print_stat(dpctl_p
, " frame:", s
.rx_frame_errors
);
592 dpctl_print(dpctl_p
, "\n");
594 print_stat(dpctl_p
, "\t\tTX packets:", s
.tx_packets
);
595 print_stat(dpctl_p
, " errors:", s
.tx_errors
);
596 print_stat(dpctl_p
, " dropped:", s
.tx_dropped
);
597 print_stat(dpctl_p
, " aborted:", s
.tx_aborted_errors
);
598 print_stat(dpctl_p
, " carrier:", s
.tx_carrier_errors
);
599 dpctl_print(dpctl_p
, "\n");
601 print_stat(dpctl_p
, "\t\tcollisions:", s
.collisions
);
602 dpctl_print(dpctl_p
, "\n");
604 print_stat(dpctl_p
, "\t\tRX bytes:", s
.rx_bytes
);
605 print_human_size(dpctl_p
, s
.rx_bytes
);
606 print_stat(dpctl_p
, " TX bytes:", s
.tx_bytes
);
607 print_human_size(dpctl_p
, s
.tx_bytes
);
608 dpctl_print(dpctl_p
, "\n");
610 dpctl_print(dpctl_p
, ", could not retrieve stats (%s)",
611 ovs_strerror(error
));
617 typedef void (*dps_for_each_cb
)(struct dpif
*, struct dpctl_params
*);
620 dps_for_each(struct dpctl_params
*dpctl_p
, dps_for_each_cb cb
)
622 struct sset dpif_names
= SSET_INITIALIZER(&dpif_names
),
623 dpif_types
= SSET_INITIALIZER(&dpif_types
);
624 int error
, openerror
= 0, enumerror
= 0;
625 const char *type
, *name
;
626 bool at_least_one
= false;
628 dp_enumerate_types(&dpif_types
);
630 SSET_FOR_EACH (type
, &dpif_types
) {
631 error
= dp_enumerate_names(type
, &dpif_names
);
636 SSET_FOR_EACH (name
, &dpif_names
) {
640 error
= dpif_open(name
, type
, &dpif
);
646 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
652 sset_destroy(&dpif_names
);
653 sset_destroy(&dpif_types
);
655 /* If there has been an error while opening a datapath it should be
656 * reported. Otherwise, we want to ignore the errors generated by
657 * dp_enumerate_names() if at least one datapath has been discovered,
658 * because they're not interesting for the user. This happens, for
659 * example, if OVS is using a userspace datapath and the kernel module
664 return at_least_one
? 0 : enumerror
;
669 dpctl_show(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
671 int error
, lasterror
= 0;
674 for (i
= 1; i
< argc
; i
++) {
675 const char *name
= argv
[i
];
678 error
= parsed_dpif_open(name
, false, &dpif
);
680 show_dpif(dpif
, dpctl_p
);
683 dpctl_error(dpctl_p
, error
, "opening datapath %s failed",
689 lasterror
= dps_for_each(dpctl_p
, show_dpif
);
696 dump_cb(struct dpif
*dpif
, struct dpctl_params
*dpctl_p
)
698 dpctl_print(dpctl_p
, "%s\n", dpif_name(dpif
));
702 dpctl_dump_dps(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
703 struct dpctl_params
*dpctl_p
)
705 return dps_for_each(dpctl_p
, dump_cb
);
709 format_dpif_flow(struct ds
*ds
, const struct dpif_flow
*f
, struct hmap
*ports
,
710 struct dpctl_params
*dpctl_p
)
712 if (dpctl_p
->verbosity
&& f
->ufid_present
) {
713 odp_format_ufid(&f
->ufid
, ds
);
714 ds_put_cstr(ds
, ", ");
716 odp_flow_format(f
->key
, f
->key_len
, f
->mask
, f
->mask_len
, ports
, ds
,
718 ds_put_cstr(ds
, ", ");
720 dpif_flow_stats_format(&f
->stats
, ds
);
721 ds_put_cstr(ds
, ", actions:");
722 format_odp_actions(ds
, f
->actions
, f
->actions_len
);
726 dpctl_dump_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
733 struct flow flow_filter
;
734 struct flow_wildcards wc_filter
;
736 struct dpif_port_dump port_dump
;
737 struct dpif_port dpif_port
;
738 struct hmap portno_names
;
739 struct simap names_portno
;
741 struct dpif_flow_dump_thread
*flow_dump_thread
;
742 struct dpif_flow_dump
*flow_dump
;
744 int pmd_id
= PMD_ID_NULL
;
747 if (argc
> 1 && !strncmp(argv
[argc
- 1], "filter=", 7)) {
748 filter
= xstrdup(argv
[--argc
] + 7);
750 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
756 error
= parsed_dpif_open(name
, false, &dpif
);
759 dpctl_error(dpctl_p
, error
, "opening datapath");
764 hmap_init(&portno_names
);
765 simap_init(&names_portno
);
766 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
767 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
768 simap_put(&names_portno
, dpif_port
.name
,
769 odp_to_u32(dpif_port
.port_no
));
773 char *err
= parse_ofp_exact_flow(&flow_filter
, &wc_filter
.masks
,
774 filter
, &names_portno
);
776 dpctl_error(dpctl_p
, 0, "Failed to parse filter (%s)", err
);
782 /* Make sure that these values are different. PMD_ID_NULL means that the
783 * pmd is unspecified (e.g. because the datapath doesn't have different
784 * pmd threads), while NON_PMD_CORE_ID refers to every non pmd threads
785 * in the userspace datapath */
786 BUILD_ASSERT(PMD_ID_NULL
!= NON_PMD_CORE_ID
);
789 flow_dump
= dpif_flow_dump_create(dpif
, false);
790 flow_dump_thread
= dpif_flow_dump_thread_create(flow_dump
);
791 while (dpif_flow_dump_next(flow_dump_thread
, &f
, 1)) {
794 struct flow_wildcards wc
;
795 struct match match
, match_filter
;
796 struct minimatch minimatch
;
798 odp_flow_key_to_flow(f
.key
, f
.key_len
, &flow
);
799 odp_flow_key_to_mask(f
.mask
, f
.mask_len
, &wc
.masks
, &flow
);
800 match_init(&match
, &flow
, &wc
);
802 match_init(&match_filter
, &flow_filter
, &wc
);
803 match_init(&match_filter
, &match_filter
.flow
, &wc_filter
);
804 minimatch_init(&minimatch
, &match_filter
);
806 if (!minimatch_matches_flow(&minimatch
, &match
.flow
)) {
807 minimatch_destroy(&minimatch
);
810 minimatch_destroy(&minimatch
);
813 /* If 'pmd_id' is specified, overlapping flows could be dumped from
814 * different pmd threads. So, separates dumps from different pmds
815 * by printing a title line. */
816 if (pmd_id
!= f
.pmd_id
) {
817 if (f
.pmd_id
== NON_PMD_CORE_ID
) {
818 ds_put_format(&ds
, "flow-dump from non-dpdk interfaces:\n");
820 ds_put_format(&ds
, "flow-dump from pmd on cpu core: %d\n",
825 format_dpif_flow(&ds
, &f
, &portno_names
, dpctl_p
);
826 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
828 dpif_flow_dump_thread_destroy(flow_dump_thread
);
829 error
= dpif_flow_dump_destroy(flow_dump
);
832 dpctl_error(dpctl_p
, error
, "Failed to dump flows from datapath");
837 odp_portno_names_destroy(&portno_names
);
838 simap_destroy(&names_portno
);
839 hmap_destroy(&portno_names
);
846 /* Extracts the in_port from the parsed keys, and returns the reference
847 * to the 'struct netdev *' of the dpif port. On error, returns NULL.
848 * Users must call 'netdev_close()' after finish using the returned
850 static struct netdev
*
851 get_in_port_netdev_from_key(struct dpif
*dpif
, const struct ofpbuf
*key
)
853 const struct nlattr
*in_port_nla
;
854 struct netdev
*dev
= NULL
;
856 in_port_nla
= nl_attr_find(key
, 0, OVS_KEY_ATTR_IN_PORT
);
858 struct dpif_port dpif_port
;
862 port_no
= ODP_PORT_C(nl_attr_get_u32(in_port_nla
));
863 error
= dpif_port_query_by_number(dpif
, port_no
, &dpif_port
);
868 netdev_open(dpif_port
.name
, dpif_port
.type
, &dev
);
869 dpif_port_destroy(&dpif_port
);
877 dpctl_put_flow(int argc
, const char *argv
[], enum dpif_flow_put_flags flags
,
878 struct dpctl_params
*dpctl_p
)
880 const char *key_s
= argv
[argc
- 2];
881 const char *actions_s
= argv
[argc
- 1];
882 struct netdev
*in_port_netdev
= NULL
;
883 struct dpif_flow_stats stats
;
884 struct dpif_port dpif_port
;
885 struct dpif_port_dump port_dump
;
886 struct ofpbuf actions
;
893 struct simap port_names
;
896 dp_name
= argc
== 4 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
900 error
= parsed_dpif_open(dp_name
, false, &dpif
);
903 dpctl_error(dpctl_p
, error
, "opening datapath");
907 ufid_present
= false;
908 n
= odp_ufid_from_string(key_s
, &ufid
);
910 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
917 simap_init(&port_names
);
918 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
919 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
922 ofpbuf_init(&key
, 0);
923 ofpbuf_init(&mask
, 0);
924 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
925 simap_destroy(&port_names
);
927 dpctl_error(dpctl_p
, error
, "parsing flow key");
928 goto out_freekeymask
;
931 ofpbuf_init(&actions
, 0);
932 error
= odp_actions_from_string(actions_s
, NULL
, &actions
);
934 dpctl_error(dpctl_p
, error
, "parsing actions");
935 goto out_freeactions
;
938 /* For DPDK interface, applies the operation to all pmd threads
939 * on the same numa node. */
940 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
941 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
944 numa_id
= netdev_get_numa_id(in_port_netdev
);
945 if (ovs_numa_numa_id_is_valid(numa_id
)) {
946 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
947 struct ovs_numa_info
*iter
;
949 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
950 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
951 error
= dpif_flow_put(dpif
, flags
,
953 mask
.size
== 0 ? NULL
: mask
.data
,
954 mask
.size
, actions
.data
,
955 actions
.size
, ufid_present
? &ufid
: NULL
,
956 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
959 ovs_numa_dump_destroy(dump
);
964 error
= dpif_flow_put(dpif
, flags
,
966 mask
.size
== 0 ? NULL
: mask
.data
,
967 mask
.size
, actions
.data
,
968 actions
.size
, ufid_present
? &ufid
: NULL
,
969 PMD_ID_NULL
, dpctl_p
->print_statistics
? &stats
: NULL
);
972 dpctl_error(dpctl_p
, error
, "updating flow table");
973 goto out_freeactions
;
976 if (dpctl_p
->print_statistics
) {
980 dpif_flow_stats_format(&stats
, &s
);
981 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
986 ofpbuf_uninit(&actions
);
988 ofpbuf_uninit(&mask
);
991 netdev_close(in_port_netdev
);
996 dpctl_add_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
998 return dpctl_put_flow(argc
, argv
, DPIF_FP_CREATE
, dpctl_p
);
1002 dpctl_mod_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1004 enum dpif_flow_put_flags flags
;
1006 flags
= DPIF_FP_MODIFY
;
1007 if (dpctl_p
->may_create
) {
1008 flags
|= DPIF_FP_CREATE
;
1010 if (dpctl_p
->zero_statistics
) {
1011 flags
|= DPIF_FP_ZERO_STATS
;
1014 return dpctl_put_flow(argc
, argv
, flags
, dpctl_p
);
1018 dpctl_get_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1020 const char *key_s
= argv
[argc
- 1];
1021 struct dpif_flow flow
;
1022 struct dpif_port dpif_port
;
1023 struct dpif_port_dump port_dump
;
1026 struct hmap portno_names
;
1029 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1033 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1037 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1040 dpctl_error(dpctl_p
, error
, "opening datapath");
1044 ofpbuf_use_stub(&buf
, &stub
, sizeof stub
);
1045 hmap_init(&portno_names
);
1046 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1047 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
1050 n
= odp_ufid_from_string(key_s
, &ufid
);
1052 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1056 /* Does not work for DPDK, since do not know which 'pmd' to apply the
1057 * operation. So, just uses PMD_ID_NULL. */
1058 error
= dpif_flow_get(dpif
, NULL
, 0, &ufid
, PMD_ID_NULL
, &buf
, &flow
);
1060 dpctl_error(dpctl_p
, error
, "getting flow");
1065 format_dpif_flow(&ds
, &flow
, &portno_names
, dpctl_p
);
1066 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
1070 odp_portno_names_destroy(&portno_names
);
1071 hmap_destroy(&portno_names
);
1072 ofpbuf_uninit(&buf
);
1078 dpctl_del_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1080 const char *key_s
= argv
[argc
- 1];
1081 struct netdev
*in_port_netdev
= NULL
;
1082 struct dpif_flow_stats stats
;
1083 struct dpif_port dpif_port
;
1084 struct dpif_port_dump port_dump
;
1086 struct ofpbuf mask
; /* To be ignored. */
1091 struct simap port_names
;
1094 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1098 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1101 dpctl_error(dpctl_p
, error
, "opening datapath");
1105 ufid_present
= false;
1106 n
= odp_ufid_from_string(key_s
, &ufid
);
1108 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1112 ufid_present
= true;
1115 simap_init(&port_names
);
1116 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1117 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
1120 ofpbuf_init(&key
, 0);
1121 ofpbuf_init(&mask
, 0);
1123 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
1125 dpctl_error(dpctl_p
, error
, "parsing flow key");
1129 /* For DPDK interface, applies the operation to all pmd threads
1130 * on the same numa node. */
1131 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
1132 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
1135 numa_id
= netdev_get_numa_id(in_port_netdev
);
1136 if (ovs_numa_numa_id_is_valid(numa_id
)) {
1137 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
1138 struct ovs_numa_info
*iter
;
1140 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
1141 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
1142 error
= dpif_flow_del(dpif
, key
.data
,
1143 key
.size
, ufid_present
? &ufid
: NULL
,
1144 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
1147 ovs_numa_dump_destroy(dump
);
1152 error
= dpif_flow_del(dpif
, key
.data
, key
.size
,
1153 ufid_present
? &ufid
: NULL
, PMD_ID_NULL
,
1154 dpctl_p
->print_statistics
? &stats
: NULL
);
1157 dpctl_error(dpctl_p
, error
, "deleting flow");
1158 if (error
== ENOENT
&& !ufid_present
) {
1162 ds_put_format(&s
, "Perhaps you need to specify a UFID?");
1163 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1169 if (dpctl_p
->print_statistics
) {
1173 dpif_flow_stats_format(&stats
, &s
);
1174 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1179 ofpbuf_uninit(&mask
);
1180 ofpbuf_uninit(&key
);
1181 simap_destroy(&port_names
);
1183 netdev_close(in_port_netdev
);
1188 dpctl_del_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1194 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1198 error
= parsed_dpif_open(name
, false, &dpif
);
1201 dpctl_error(dpctl_p
, error
, "opening datapath");
1205 error
= dpif_flow_flush(dpif
);
1207 dpctl_error(dpctl_p
, error
, "deleting all flows");
1214 dpctl_help(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1215 struct dpctl_params
*dpctl_p
)
1217 if (dpctl_p
->usage
) {
1218 dpctl_p
->usage(dpctl_p
->aux
);
1225 dpctl_list_commands(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1226 struct dpctl_params
*dpctl_p
)
1228 struct ds ds
= DS_EMPTY_INITIALIZER
;
1229 const struct dpctl_command
*commands
= get_all_dpctl_commands();
1231 ds_put_cstr(&ds
, "The available commands are:\n");
1232 for (; commands
->name
; commands
++) {
1233 const struct dpctl_command
*c
= commands
;
1235 ds_put_format(&ds
, " %s%-23s %s\n", dpctl_p
->is_appctl
? "dpctl/" : "",
1238 dpctl_puts(dpctl_p
, false, ds
.string
);
1244 /* Undocumented commands for unit testing. */
1247 dpctl_parse_actions(int argc
, const char *argv
[], struct dpctl_params
* dpctl_p
)
1251 for (i
= 1; i
< argc
; i
++) {
1252 struct ofpbuf actions
;
1255 ofpbuf_init(&actions
, 0);
1256 error
= odp_actions_from_string(argv
[i
], NULL
, &actions
);
1259 ofpbuf_uninit(&actions
);
1260 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1265 format_odp_actions(&s
, actions
.data
, actions
.size
);
1266 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1269 ofpbuf_uninit(&actions
);
1275 struct actions_for_flow
{
1276 struct hmap_node hmap_node
;
1278 struct ofpbuf actions
;
1281 static struct actions_for_flow
*
1282 get_actions_for_flow(struct hmap
*actions_per_flow
, const struct flow
*flow
)
1284 uint32_t hash
= flow_hash(flow
, 0);
1285 struct actions_for_flow
*af
;
1287 HMAP_FOR_EACH_WITH_HASH (af
, hmap_node
, hash
, actions_per_flow
) {
1288 if (flow_equal(&af
->flow
, flow
)) {
1293 af
= xmalloc(sizeof *af
);
1295 ofpbuf_init(&af
->actions
, 0);
1296 hmap_insert(actions_per_flow
, &af
->hmap_node
, hash
);
1301 compare_actions_for_flow(const void *a_
, const void *b_
)
1303 struct actions_for_flow
*const *a
= a_
;
1304 struct actions_for_flow
*const *b
= b_
;
1306 return flow_compare_3way(&(*a
)->flow
, &(*b
)->flow
);
1310 compare_output_actions(const void *a_
, const void *b_
)
1312 const struct nlattr
*a
= a_
;
1313 const struct nlattr
*b
= b_
;
1314 uint32_t a_port
= nl_attr_get_u32(a
);
1315 uint32_t b_port
= nl_attr_get_u32(b
);
1317 return a_port
< b_port
? -1 : a_port
> b_port
;
1321 sort_output_actions__(struct nlattr
*first
, struct nlattr
*end
)
1323 size_t bytes
= (uint8_t *) end
- (uint8_t *) first
;
1324 size_t n
= bytes
/ NL_A_U32_SIZE
;
1326 ovs_assert(bytes
% NL_A_U32_SIZE
== 0);
1327 qsort(first
, n
, NL_A_U32_SIZE
, compare_output_actions
);
1331 sort_output_actions(struct nlattr
*actions
, size_t length
)
1333 struct nlattr
*first_output
= NULL
;
1337 NL_ATTR_FOR_EACH (a
, left
, actions
, length
) {
1338 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
) {
1339 if (!first_output
) {
1344 sort_output_actions__(first_output
, a
);
1345 first_output
= NULL
;
1350 uint8_t *end
= (uint8_t *) actions
+ length
;
1351 sort_output_actions__(first_output
,
1352 ALIGNED_CAST(struct nlattr
*, end
));
1356 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1357 * have the syntax used by "ovs-dpctl dump-flows".
1359 * This command prints ACTIONS in a format that shows what happens for each
1360 * VLAN, independent of the order of the ACTIONS. For example, there is more
1361 * than one way to output a packet on VLANs 9 and 11, but this command will
1362 * print the same output for any form.
1364 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1365 * so far the implementation only covers VLANs. */
1367 dpctl_normalize_actions(int argc
, const char *argv
[],
1368 struct dpctl_params
*dpctl_p
)
1370 struct simap port_names
;
1371 struct ofpbuf keybuf
;
1373 struct ofpbuf odp_actions
;
1374 struct hmap actions_per_flow
;
1375 struct actions_for_flow
**afs
;
1376 struct actions_for_flow
*af
;
1385 simap_init(&port_names
);
1386 for (i
= 3; i
< argc
; i
++) {
1390 if (ovs_scan(argv
[i
], "%15[^=]=%d", name
, &number
)) {
1391 uintptr_t n
= number
;
1392 simap_put(&port_names
, name
, n
);
1394 dpctl_error(dpctl_p
, 0, "%s: expected NAME=NUMBER", argv
[i
]);
1400 /* Parse flow key. */
1401 ofpbuf_init(&keybuf
, 0);
1402 error
= odp_flow_from_string(argv
[1], &port_names
, &keybuf
, NULL
);
1404 dpctl_error(dpctl_p
, error
, "odp_flow_key_from_string");
1405 goto out_freekeybuf
;
1409 odp_flow_format(keybuf
.data
, keybuf
.size
, NULL
, 0, NULL
,
1410 &s
, dpctl_p
->verbosity
);
1411 dpctl_print(dpctl_p
, "input flow: %s\n", ds_cstr(&s
));
1413 error
= odp_flow_key_to_flow(keybuf
.data
, keybuf
.size
, &flow
);
1415 dpctl_error(dpctl_p
, error
, "odp_flow_key_to_flow");
1416 goto out_freekeybuf
;
1419 /* Parse actions. */
1420 ofpbuf_init(&odp_actions
, 0);
1421 error
= odp_actions_from_string(argv
[2], &port_names
, &odp_actions
);
1423 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1424 goto out_freeactions
;
1427 if (dpctl_p
->verbosity
) {
1429 format_odp_actions(&s
, odp_actions
.data
, odp_actions
.size
);
1430 dpctl_print(dpctl_p
, "input actions: %s\n", ds_cstr(&s
));
1433 hmap_init(&actions_per_flow
);
1434 NL_ATTR_FOR_EACH (a
, left
, odp_actions
.data
, odp_actions
.size
) {
1435 const struct ovs_action_push_vlan
*push
;
1436 switch(nl_attr_type(a
)) {
1437 case OVS_ACTION_ATTR_POP_VLAN
:
1438 flow
.vlan_tci
= htons(0);
1441 case OVS_ACTION_ATTR_PUSH_VLAN
:
1442 push
= nl_attr_get_unspec(a
, sizeof *push
);
1443 flow
.vlan_tci
= push
->vlan_tci
;
1447 af
= get_actions_for_flow(&actions_per_flow
, &flow
);
1448 nl_msg_put_unspec(&af
->actions
, nl_attr_type(a
),
1449 nl_attr_get(a
), nl_attr_get_size(a
));
1452 n_afs
= hmap_count(&actions_per_flow
);
1453 afs
= xmalloc(n_afs
* sizeof *afs
);
1455 HMAP_FOR_EACH (af
, hmap_node
, &actions_per_flow
) {
1459 ovs_assert(i
== n_afs
);
1460 hmap_destroy(&actions_per_flow
);
1462 qsort(afs
, n_afs
, sizeof *afs
, compare_actions_for_flow
);
1464 for (i
= 0; i
< n_afs
; i
++) {
1465 struct actions_for_flow
*af
= afs
[i
];
1467 sort_output_actions(af
->actions
.data
, af
->actions
.size
);
1469 if (af
->flow
.vlan_tci
!= htons(0)) {
1470 dpctl_print(dpctl_p
, "vlan(vid=%"PRIu16
",pcp=%d): ",
1471 vlan_tci_to_vid(af
->flow
.vlan_tci
),
1472 vlan_tci_to_pcp(af
->flow
.vlan_tci
));
1474 dpctl_print(dpctl_p
, "no vlan: ");
1477 if (eth_type_mpls(af
->flow
.dl_type
)) {
1478 dpctl_print(dpctl_p
, "mpls(label=%"PRIu32
",tc=%d,ttl=%d): ",
1479 mpls_lse_to_label(af
->flow
.mpls_lse
[0]),
1480 mpls_lse_to_tc(af
->flow
.mpls_lse
[0]),
1481 mpls_lse_to_ttl(af
->flow
.mpls_lse
[0]));
1483 dpctl_print(dpctl_p
, "no mpls: ");
1487 format_odp_actions(&s
, af
->actions
.data
, af
->actions
.size
);
1488 dpctl_puts(dpctl_p
, false, ds_cstr(&s
));
1490 ofpbuf_uninit(&af
->actions
);
1497 ofpbuf_uninit(&odp_actions
);
1499 ofpbuf_uninit(&keybuf
);
1501 simap_destroy(&port_names
);
1507 static const struct dpctl_command all_commands
[] = {
1508 { "add-dp", "add-dp dp [iface...]", 1, INT_MAX
, dpctl_add_dp
},
1509 { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp
},
1510 { "add-if", "add-if dp iface...", 2, INT_MAX
, dpctl_add_if
},
1511 { "del-if", "del-if dp iface...", 2, INT_MAX
, dpctl_del_if
},
1512 { "set-if", "set-if dp iface...", 2, INT_MAX
, dpctl_set_if
},
1513 { "dump-dps", "", 0, 0, dpctl_dump_dps
},
1514 { "show", "[dp...]", 0, INT_MAX
, dpctl_show
},
1515 { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows
},
1516 { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow
},
1517 { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow
},
1518 { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow
},
1519 { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow
},
1520 { "del-flows", "[dp]", 0, 1, dpctl_del_flows
},
1521 { "help", "", 0, INT_MAX
, dpctl_help
},
1522 { "list-commands", "", 0, INT_MAX
, dpctl_list_commands
},
1524 /* Undocumented commands for testing. */
1525 { "parse-actions", "actions", 1, INT_MAX
, dpctl_parse_actions
},
1526 { "normalize-actions", "actions", 2, INT_MAX
, dpctl_normalize_actions
},
1528 { NULL
, NULL
, 0, 0, NULL
},
1531 static const struct dpctl_command
*get_all_dpctl_commands(void)
1533 return all_commands
;
1536 /* Runs the command designated by argv[0] within the command table specified by
1537 * 'commands', which must be terminated by a command whose 'name' member is a
1540 dpctl_run_command(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1542 const struct dpctl_command
*p
;
1545 dpctl_error(dpctl_p
, 0, "missing command name; use --help for help");
1549 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1550 if (!strcmp(p
->name
, argv
[0])) {
1551 int n_arg
= argc
- 1;
1552 if (n_arg
< p
->min_args
) {
1553 dpctl_error(dpctl_p
, 0,
1554 "'%s' command requires at least %d arguments",
1555 p
->name
, p
->min_args
);
1557 } else if (n_arg
> p
->max_args
) {
1558 dpctl_error(dpctl_p
, 0,
1559 "'%s' command takes at most %d arguments",
1560 p
->name
, p
->max_args
);
1563 return p
->handler(argc
, argv
, dpctl_p
);
1568 dpctl_error(dpctl_p
, 0, "unknown command '%s'; use --help for help",
1574 dpctl_unixctl_print(void *userdata
, bool error OVS_UNUSED
, const char *msg
)
1576 struct ds
*ds
= userdata
;
1577 ds_put_cstr(ds
, msg
);
1581 dpctl_unixctl_handler(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1584 struct ds ds
= DS_EMPTY_INITIALIZER
;
1585 struct dpctl_params dpctl_p
;
1588 dpctl_command_handler
*handler
= (dpctl_command_handler
*) aux
;
1590 dpctl_p
.print_statistics
= false;
1591 dpctl_p
.zero_statistics
= false;
1592 dpctl_p
.may_create
= false;
1593 dpctl_p
.verbosity
= 0;
1595 /* Parse options (like getopt). Unfortunately it does
1596 * not seem a good idea to call getopt_long() here, since it uses global
1598 while (argc
> 1 && !error
) {
1599 const char *arg
= argv
[1];
1600 if (!strncmp(arg
, "--", 2)) {
1602 if (!strcmp(arg
, "--statistics")) {
1603 dpctl_p
.print_statistics
= true;
1604 } else if (!strcmp(arg
, "--clear")) {
1605 dpctl_p
.zero_statistics
= true;
1606 } else if (!strcmp(arg
, "--may-create")) {
1607 dpctl_p
.may_create
= true;
1608 } else if (!strcmp(arg
, "--more")) {
1609 dpctl_p
.verbosity
++;
1611 ds_put_format(&ds
, "Unrecognized option %s", argv
[1]);
1614 } else if (arg
[0] == '-' && arg
[1] != '\0') {
1615 /* Short option[s] */
1616 const char *opt
= &arg
[1];
1618 while (*opt
&& !error
) {
1621 dpctl_p
.verbosity
++;
1624 dpctl_p
.print_statistics
= true;
1627 ds_put_format(&ds
, "Unrecognized option -%c", *opt
);
1634 /* Doesn't start with -, not an option */
1646 dpctl_p
.is_appctl
= true;
1647 dpctl_p
.output
= dpctl_unixctl_print
;
1650 error
= handler(argc
, argv
, &dpctl_p
) != 0;
1654 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
1656 unixctl_command_reply(conn
, ds_cstr(&ds
));
1663 dpctl_unixctl_register(void)
1665 const struct dpctl_command
*p
;
1667 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1668 char *cmd_name
= xasprintf("dpctl/%s", p
->name
);
1669 unixctl_command_register(cmd_name
, "", p
->min_args
, p
->max_args
,
1670 dpctl_unixctl_handler
, p
->handler
);