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
, f
.key
, f
.key_len
,
801 match_init(&match
, &flow
, &wc
);
803 match_init(&match_filter
, &flow_filter
, &wc
);
804 match_init(&match_filter
, &match_filter
.flow
, &wc_filter
);
805 minimatch_init(&minimatch
, &match_filter
);
807 if (!minimatch_matches_flow(&minimatch
, &match
.flow
)) {
808 minimatch_destroy(&minimatch
);
811 minimatch_destroy(&minimatch
);
814 /* If 'pmd_id' is specified, overlapping flows could be dumped from
815 * different pmd threads. So, separates dumps from different pmds
816 * by printing a title line. */
817 if (pmd_id
!= f
.pmd_id
) {
818 if (f
.pmd_id
== NON_PMD_CORE_ID
) {
819 ds_put_format(&ds
, "flow-dump from non-dpdk interfaces:\n");
821 ds_put_format(&ds
, "flow-dump from pmd on cpu core: %d\n",
826 format_dpif_flow(&ds
, &f
, &portno_names
, dpctl_p
);
827 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
829 dpif_flow_dump_thread_destroy(flow_dump_thread
);
830 error
= dpif_flow_dump_destroy(flow_dump
);
833 dpctl_error(dpctl_p
, error
, "Failed to dump flows from datapath");
838 odp_portno_names_destroy(&portno_names
);
839 simap_destroy(&names_portno
);
840 hmap_destroy(&portno_names
);
847 /* Extracts the in_port from the parsed keys, and returns the reference
848 * to the 'struct netdev *' of the dpif port. On error, returns NULL.
849 * Users must call 'netdev_close()' after finish using the returned
851 static struct netdev
*
852 get_in_port_netdev_from_key(struct dpif
*dpif
, const struct ofpbuf
*key
)
854 const struct nlattr
*in_port_nla
;
855 struct netdev
*dev
= NULL
;
857 in_port_nla
= nl_attr_find(key
, 0, OVS_KEY_ATTR_IN_PORT
);
859 struct dpif_port dpif_port
;
863 port_no
= ODP_PORT_C(nl_attr_get_u32(in_port_nla
));
864 error
= dpif_port_query_by_number(dpif
, port_no
, &dpif_port
);
869 netdev_open(dpif_port
.name
, dpif_port
.type
, &dev
);
870 dpif_port_destroy(&dpif_port
);
878 dpctl_put_flow(int argc
, const char *argv
[], enum dpif_flow_put_flags flags
,
879 struct dpctl_params
*dpctl_p
)
881 const char *key_s
= argv
[argc
- 2];
882 const char *actions_s
= argv
[argc
- 1];
883 struct netdev
*in_port_netdev
= NULL
;
884 struct dpif_flow_stats stats
;
885 struct dpif_port dpif_port
;
886 struct dpif_port_dump port_dump
;
887 struct ofpbuf actions
;
894 struct simap port_names
;
897 dp_name
= argc
== 4 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
901 error
= parsed_dpif_open(dp_name
, false, &dpif
);
904 dpctl_error(dpctl_p
, error
, "opening datapath");
908 ufid_present
= false;
909 n
= odp_ufid_from_string(key_s
, &ufid
);
911 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
918 simap_init(&port_names
);
919 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
920 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
923 ofpbuf_init(&key
, 0);
924 ofpbuf_init(&mask
, 0);
925 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
926 simap_destroy(&port_names
);
928 dpctl_error(dpctl_p
, error
, "parsing flow key");
929 goto out_freekeymask
;
932 ofpbuf_init(&actions
, 0);
933 error
= odp_actions_from_string(actions_s
, NULL
, &actions
);
935 dpctl_error(dpctl_p
, error
, "parsing actions");
936 goto out_freeactions
;
939 /* For DPDK interface, applies the operation to all pmd threads
940 * on the same numa node. */
941 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
942 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
945 numa_id
= netdev_get_numa_id(in_port_netdev
);
946 if (ovs_numa_numa_id_is_valid(numa_id
)) {
947 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
948 struct ovs_numa_info
*iter
;
950 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
951 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
952 error
= dpif_flow_put(dpif
, flags
,
954 mask
.size
== 0 ? NULL
: mask
.data
,
955 mask
.size
, actions
.data
,
956 actions
.size
, ufid_present
? &ufid
: NULL
,
957 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
960 ovs_numa_dump_destroy(dump
);
965 error
= dpif_flow_put(dpif
, flags
,
967 mask
.size
== 0 ? NULL
: mask
.data
,
968 mask
.size
, actions
.data
,
969 actions
.size
, ufid_present
? &ufid
: NULL
,
970 PMD_ID_NULL
, dpctl_p
->print_statistics
? &stats
: NULL
);
973 dpctl_error(dpctl_p
, error
, "updating flow table");
974 goto out_freeactions
;
977 if (dpctl_p
->print_statistics
) {
981 dpif_flow_stats_format(&stats
, &s
);
982 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
987 ofpbuf_uninit(&actions
);
989 ofpbuf_uninit(&mask
);
992 netdev_close(in_port_netdev
);
997 dpctl_add_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
999 return dpctl_put_flow(argc
, argv
, DPIF_FP_CREATE
, dpctl_p
);
1003 dpctl_mod_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1005 enum dpif_flow_put_flags flags
;
1007 flags
= DPIF_FP_MODIFY
;
1008 if (dpctl_p
->may_create
) {
1009 flags
|= DPIF_FP_CREATE
;
1011 if (dpctl_p
->zero_statistics
) {
1012 flags
|= DPIF_FP_ZERO_STATS
;
1015 return dpctl_put_flow(argc
, argv
, flags
, dpctl_p
);
1019 dpctl_get_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1021 const char *key_s
= argv
[argc
- 1];
1022 struct dpif_flow flow
;
1023 struct dpif_port dpif_port
;
1024 struct dpif_port_dump port_dump
;
1027 struct hmap portno_names
;
1030 uint64_t stub
[DPIF_FLOW_BUFSIZE
/ 8];
1034 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1038 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1041 dpctl_error(dpctl_p
, error
, "opening datapath");
1045 ofpbuf_use_stub(&buf
, &stub
, sizeof stub
);
1046 hmap_init(&portno_names
);
1047 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1048 odp_portno_names_set(&portno_names
, dpif_port
.port_no
, dpif_port
.name
);
1051 n
= odp_ufid_from_string(key_s
, &ufid
);
1053 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1057 /* Does not work for DPDK, since do not know which 'pmd' to apply the
1058 * operation. So, just uses PMD_ID_NULL. */
1059 error
= dpif_flow_get(dpif
, NULL
, 0, &ufid
, PMD_ID_NULL
, &buf
, &flow
);
1061 dpctl_error(dpctl_p
, error
, "getting flow");
1066 format_dpif_flow(&ds
, &flow
, &portno_names
, dpctl_p
);
1067 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&ds
));
1071 odp_portno_names_destroy(&portno_names
);
1072 hmap_destroy(&portno_names
);
1073 ofpbuf_uninit(&buf
);
1079 dpctl_del_flow(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1081 const char *key_s
= argv
[argc
- 1];
1082 struct netdev
*in_port_netdev
= NULL
;
1083 struct dpif_flow_stats stats
;
1084 struct dpif_port dpif_port
;
1085 struct dpif_port_dump port_dump
;
1087 struct ofpbuf mask
; /* To be ignored. */
1092 struct simap port_names
;
1095 dp_name
= argc
== 3 ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1099 error
= parsed_dpif_open(dp_name
, false, &dpif
);
1102 dpctl_error(dpctl_p
, error
, "opening datapath");
1106 ufid_present
= false;
1107 n
= odp_ufid_from_string(key_s
, &ufid
);
1109 dpctl_error(dpctl_p
, -n
, "parsing flow ufid");
1113 ufid_present
= true;
1116 simap_init(&port_names
);
1117 DPIF_PORT_FOR_EACH (&dpif_port
, &port_dump
, dpif
) {
1118 simap_put(&port_names
, dpif_port
.name
, odp_to_u32(dpif_port
.port_no
));
1121 ofpbuf_init(&key
, 0);
1122 ofpbuf_init(&mask
, 0);
1124 error
= odp_flow_from_string(key_s
, &port_names
, &key
, &mask
);
1126 dpctl_error(dpctl_p
, error
, "parsing flow key");
1130 /* For DPDK interface, applies the operation to all pmd threads
1131 * on the same numa node. */
1132 in_port_netdev
= get_in_port_netdev_from_key(dpif
, &key
);
1133 if (in_port_netdev
&& netdev_is_pmd(in_port_netdev
)) {
1136 numa_id
= netdev_get_numa_id(in_port_netdev
);
1137 if (ovs_numa_numa_id_is_valid(numa_id
)) {
1138 struct ovs_numa_dump
*dump
= ovs_numa_dump_cores_on_numa(numa_id
);
1139 struct ovs_numa_info
*iter
;
1141 FOR_EACH_CORE_ON_NUMA (iter
, dump
) {
1142 if (ovs_numa_core_is_pinned(iter
->core_id
)) {
1143 error
= dpif_flow_del(dpif
, key
.data
,
1144 key
.size
, ufid_present
? &ufid
: NULL
,
1145 iter
->core_id
, dpctl_p
->print_statistics
? &stats
: NULL
);
1148 ovs_numa_dump_destroy(dump
);
1153 error
= dpif_flow_del(dpif
, key
.data
, key
.size
,
1154 ufid_present
? &ufid
: NULL
, PMD_ID_NULL
,
1155 dpctl_p
->print_statistics
? &stats
: NULL
);
1158 dpctl_error(dpctl_p
, error
, "deleting flow");
1159 if (error
== ENOENT
&& !ufid_present
) {
1163 ds_put_format(&s
, "Perhaps you need to specify a UFID?");
1164 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1170 if (dpctl_p
->print_statistics
) {
1174 dpif_flow_stats_format(&stats
, &s
);
1175 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1180 ofpbuf_uninit(&mask
);
1181 ofpbuf_uninit(&key
);
1182 simap_destroy(&port_names
);
1184 netdev_close(in_port_netdev
);
1189 dpctl_del_flows(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1195 name
= (argc
== 2) ? xstrdup(argv
[1]) : get_one_dp(dpctl_p
);
1199 error
= parsed_dpif_open(name
, false, &dpif
);
1202 dpctl_error(dpctl_p
, error
, "opening datapath");
1206 error
= dpif_flow_flush(dpif
);
1208 dpctl_error(dpctl_p
, error
, "deleting all flows");
1215 dpctl_help(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1216 struct dpctl_params
*dpctl_p
)
1218 if (dpctl_p
->usage
) {
1219 dpctl_p
->usage(dpctl_p
->aux
);
1226 dpctl_list_commands(int argc OVS_UNUSED
, const char *argv
[] OVS_UNUSED
,
1227 struct dpctl_params
*dpctl_p
)
1229 struct ds ds
= DS_EMPTY_INITIALIZER
;
1230 const struct dpctl_command
*commands
= get_all_dpctl_commands();
1232 ds_put_cstr(&ds
, "The available commands are:\n");
1233 for (; commands
->name
; commands
++) {
1234 const struct dpctl_command
*c
= commands
;
1236 ds_put_format(&ds
, " %s%-23s %s\n", dpctl_p
->is_appctl
? "dpctl/" : "",
1239 dpctl_puts(dpctl_p
, false, ds
.string
);
1245 /* Undocumented commands for unit testing. */
1248 dpctl_parse_actions(int argc
, const char *argv
[], struct dpctl_params
* dpctl_p
)
1252 for (i
= 1; i
< argc
; i
++) {
1253 struct ofpbuf actions
;
1256 ofpbuf_init(&actions
, 0);
1257 error
= odp_actions_from_string(argv
[i
], NULL
, &actions
);
1260 ofpbuf_uninit(&actions
);
1261 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1266 format_odp_actions(&s
, actions
.data
, actions
.size
);
1267 dpctl_print(dpctl_p
, "%s\n", ds_cstr(&s
));
1270 ofpbuf_uninit(&actions
);
1276 struct actions_for_flow
{
1277 struct hmap_node hmap_node
;
1279 struct ofpbuf actions
;
1282 static struct actions_for_flow
*
1283 get_actions_for_flow(struct hmap
*actions_per_flow
, const struct flow
*flow
)
1285 uint32_t hash
= flow_hash(flow
, 0);
1286 struct actions_for_flow
*af
;
1288 HMAP_FOR_EACH_WITH_HASH (af
, hmap_node
, hash
, actions_per_flow
) {
1289 if (flow_equal(&af
->flow
, flow
)) {
1294 af
= xmalloc(sizeof *af
);
1296 ofpbuf_init(&af
->actions
, 0);
1297 hmap_insert(actions_per_flow
, &af
->hmap_node
, hash
);
1302 compare_actions_for_flow(const void *a_
, const void *b_
)
1304 struct actions_for_flow
*const *a
= a_
;
1305 struct actions_for_flow
*const *b
= b_
;
1307 return flow_compare_3way(&(*a
)->flow
, &(*b
)->flow
);
1311 compare_output_actions(const void *a_
, const void *b_
)
1313 const struct nlattr
*a
= a_
;
1314 const struct nlattr
*b
= b_
;
1315 uint32_t a_port
= nl_attr_get_u32(a
);
1316 uint32_t b_port
= nl_attr_get_u32(b
);
1318 return a_port
< b_port
? -1 : a_port
> b_port
;
1322 sort_output_actions__(struct nlattr
*first
, struct nlattr
*end
)
1324 size_t bytes
= (uint8_t *) end
- (uint8_t *) first
;
1325 size_t n
= bytes
/ NL_A_U32_SIZE
;
1327 ovs_assert(bytes
% NL_A_U32_SIZE
== 0);
1328 qsort(first
, n
, NL_A_U32_SIZE
, compare_output_actions
);
1332 sort_output_actions(struct nlattr
*actions
, size_t length
)
1334 struct nlattr
*first_output
= NULL
;
1338 NL_ATTR_FOR_EACH (a
, left
, actions
, length
) {
1339 if (nl_attr_type(a
) == OVS_ACTION_ATTR_OUTPUT
) {
1340 if (!first_output
) {
1345 sort_output_actions__(first_output
, a
);
1346 first_output
= NULL
;
1351 uint8_t *end
= (uint8_t *) actions
+ length
;
1352 sort_output_actions__(first_output
,
1353 ALIGNED_CAST(struct nlattr
*, end
));
1357 /* usage: "ovs-dpctl normalize-actions FLOW ACTIONS" where FLOW and ACTIONS
1358 * have the syntax used by "ovs-dpctl dump-flows".
1360 * This command prints ACTIONS in a format that shows what happens for each
1361 * VLAN, independent of the order of the ACTIONS. For example, there is more
1362 * than one way to output a packet on VLANs 9 and 11, but this command will
1363 * print the same output for any form.
1365 * The idea here generalizes beyond VLANs (e.g. to setting other fields) but
1366 * so far the implementation only covers VLANs. */
1368 dpctl_normalize_actions(int argc
, const char *argv
[],
1369 struct dpctl_params
*dpctl_p
)
1371 struct simap port_names
;
1372 struct ofpbuf keybuf
;
1374 struct ofpbuf odp_actions
;
1375 struct hmap actions_per_flow
;
1376 struct actions_for_flow
**afs
;
1377 struct actions_for_flow
*af
;
1386 simap_init(&port_names
);
1387 for (i
= 3; i
< argc
; i
++) {
1391 if (ovs_scan(argv
[i
], "%15[^=]=%d", name
, &number
)) {
1392 uintptr_t n
= number
;
1393 simap_put(&port_names
, name
, n
);
1395 dpctl_error(dpctl_p
, 0, "%s: expected NAME=NUMBER", argv
[i
]);
1401 /* Parse flow key. */
1402 ofpbuf_init(&keybuf
, 0);
1403 error
= odp_flow_from_string(argv
[1], &port_names
, &keybuf
, NULL
);
1405 dpctl_error(dpctl_p
, error
, "odp_flow_key_from_string");
1406 goto out_freekeybuf
;
1410 odp_flow_format(keybuf
.data
, keybuf
.size
, NULL
, 0, NULL
,
1411 &s
, dpctl_p
->verbosity
);
1412 dpctl_print(dpctl_p
, "input flow: %s\n", ds_cstr(&s
));
1414 error
= odp_flow_key_to_flow(keybuf
.data
, keybuf
.size
, &flow
);
1416 dpctl_error(dpctl_p
, error
, "odp_flow_key_to_flow");
1417 goto out_freekeybuf
;
1420 /* Parse actions. */
1421 ofpbuf_init(&odp_actions
, 0);
1422 error
= odp_actions_from_string(argv
[2], &port_names
, &odp_actions
);
1424 dpctl_error(dpctl_p
, error
, "odp_actions_from_string");
1425 goto out_freeactions
;
1428 if (dpctl_p
->verbosity
) {
1430 format_odp_actions(&s
, odp_actions
.data
, odp_actions
.size
);
1431 dpctl_print(dpctl_p
, "input actions: %s\n", ds_cstr(&s
));
1434 hmap_init(&actions_per_flow
);
1435 NL_ATTR_FOR_EACH (a
, left
, odp_actions
.data
, odp_actions
.size
) {
1436 const struct ovs_action_push_vlan
*push
;
1437 switch(nl_attr_type(a
)) {
1438 case OVS_ACTION_ATTR_POP_VLAN
:
1439 flow
.vlan_tci
= htons(0);
1442 case OVS_ACTION_ATTR_PUSH_VLAN
:
1443 push
= nl_attr_get_unspec(a
, sizeof *push
);
1444 flow
.vlan_tci
= push
->vlan_tci
;
1448 af
= get_actions_for_flow(&actions_per_flow
, &flow
);
1449 nl_msg_put_unspec(&af
->actions
, nl_attr_type(a
),
1450 nl_attr_get(a
), nl_attr_get_size(a
));
1453 n_afs
= hmap_count(&actions_per_flow
);
1454 afs
= xmalloc(n_afs
* sizeof *afs
);
1456 HMAP_FOR_EACH (af
, hmap_node
, &actions_per_flow
) {
1460 ovs_assert(i
== n_afs
);
1461 hmap_destroy(&actions_per_flow
);
1463 qsort(afs
, n_afs
, sizeof *afs
, compare_actions_for_flow
);
1465 for (i
= 0; i
< n_afs
; i
++) {
1466 struct actions_for_flow
*af
= afs
[i
];
1468 sort_output_actions(af
->actions
.data
, af
->actions
.size
);
1470 if (af
->flow
.vlan_tci
!= htons(0)) {
1471 dpctl_print(dpctl_p
, "vlan(vid=%"PRIu16
",pcp=%d): ",
1472 vlan_tci_to_vid(af
->flow
.vlan_tci
),
1473 vlan_tci_to_pcp(af
->flow
.vlan_tci
));
1475 dpctl_print(dpctl_p
, "no vlan: ");
1478 if (eth_type_mpls(af
->flow
.dl_type
)) {
1479 dpctl_print(dpctl_p
, "mpls(label=%"PRIu32
",tc=%d,ttl=%d): ",
1480 mpls_lse_to_label(af
->flow
.mpls_lse
[0]),
1481 mpls_lse_to_tc(af
->flow
.mpls_lse
[0]),
1482 mpls_lse_to_ttl(af
->flow
.mpls_lse
[0]));
1484 dpctl_print(dpctl_p
, "no mpls: ");
1488 format_odp_actions(&s
, af
->actions
.data
, af
->actions
.size
);
1489 dpctl_puts(dpctl_p
, false, ds_cstr(&s
));
1491 ofpbuf_uninit(&af
->actions
);
1498 ofpbuf_uninit(&odp_actions
);
1500 ofpbuf_uninit(&keybuf
);
1502 simap_destroy(&port_names
);
1508 static const struct dpctl_command all_commands
[] = {
1509 { "add-dp", "add-dp dp [iface...]", 1, INT_MAX
, dpctl_add_dp
},
1510 { "del-dp", "del-dp dp", 1, 1, dpctl_del_dp
},
1511 { "add-if", "add-if dp iface...", 2, INT_MAX
, dpctl_add_if
},
1512 { "del-if", "del-if dp iface...", 2, INT_MAX
, dpctl_del_if
},
1513 { "set-if", "set-if dp iface...", 2, INT_MAX
, dpctl_set_if
},
1514 { "dump-dps", "", 0, 0, dpctl_dump_dps
},
1515 { "show", "[dp...]", 0, INT_MAX
, dpctl_show
},
1516 { "dump-flows", "[dp]", 0, 2, dpctl_dump_flows
},
1517 { "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow
},
1518 { "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow
},
1519 { "get-flow", "get-flow [dp] ufid", 1, 2, dpctl_get_flow
},
1520 { "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow
},
1521 { "del-flows", "[dp]", 0, 1, dpctl_del_flows
},
1522 { "help", "", 0, INT_MAX
, dpctl_help
},
1523 { "list-commands", "", 0, INT_MAX
, dpctl_list_commands
},
1525 /* Undocumented commands for testing. */
1526 { "parse-actions", "actions", 1, INT_MAX
, dpctl_parse_actions
},
1527 { "normalize-actions", "actions", 2, INT_MAX
, dpctl_normalize_actions
},
1529 { NULL
, NULL
, 0, 0, NULL
},
1532 static const struct dpctl_command
*get_all_dpctl_commands(void)
1534 return all_commands
;
1537 /* Runs the command designated by argv[0] within the command table specified by
1538 * 'commands', which must be terminated by a command whose 'name' member is a
1541 dpctl_run_command(int argc
, const char *argv
[], struct dpctl_params
*dpctl_p
)
1543 const struct dpctl_command
*p
;
1546 dpctl_error(dpctl_p
, 0, "missing command name; use --help for help");
1550 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1551 if (!strcmp(p
->name
, argv
[0])) {
1552 int n_arg
= argc
- 1;
1553 if (n_arg
< p
->min_args
) {
1554 dpctl_error(dpctl_p
, 0,
1555 "'%s' command requires at least %d arguments",
1556 p
->name
, p
->min_args
);
1558 } else if (n_arg
> p
->max_args
) {
1559 dpctl_error(dpctl_p
, 0,
1560 "'%s' command takes at most %d arguments",
1561 p
->name
, p
->max_args
);
1564 return p
->handler(argc
, argv
, dpctl_p
);
1569 dpctl_error(dpctl_p
, 0, "unknown command '%s'; use --help for help",
1575 dpctl_unixctl_print(void *userdata
, bool error OVS_UNUSED
, const char *msg
)
1577 struct ds
*ds
= userdata
;
1578 ds_put_cstr(ds
, msg
);
1582 dpctl_unixctl_handler(struct unixctl_conn
*conn
, int argc
, const char *argv
[],
1585 struct ds ds
= DS_EMPTY_INITIALIZER
;
1586 struct dpctl_params dpctl_p
;
1589 dpctl_command_handler
*handler
= (dpctl_command_handler
*) aux
;
1591 dpctl_p
.print_statistics
= false;
1592 dpctl_p
.zero_statistics
= false;
1593 dpctl_p
.may_create
= false;
1594 dpctl_p
.verbosity
= 0;
1596 /* Parse options (like getopt). Unfortunately it does
1597 * not seem a good idea to call getopt_long() here, since it uses global
1599 while (argc
> 1 && !error
) {
1600 const char *arg
= argv
[1];
1601 if (!strncmp(arg
, "--", 2)) {
1603 if (!strcmp(arg
, "--statistics")) {
1604 dpctl_p
.print_statistics
= true;
1605 } else if (!strcmp(arg
, "--clear")) {
1606 dpctl_p
.zero_statistics
= true;
1607 } else if (!strcmp(arg
, "--may-create")) {
1608 dpctl_p
.may_create
= true;
1609 } else if (!strcmp(arg
, "--more")) {
1610 dpctl_p
.verbosity
++;
1612 ds_put_format(&ds
, "Unrecognized option %s", argv
[1]);
1615 } else if (arg
[0] == '-' && arg
[1] != '\0') {
1616 /* Short option[s] */
1617 const char *opt
= &arg
[1];
1619 while (*opt
&& !error
) {
1622 dpctl_p
.verbosity
++;
1625 dpctl_p
.print_statistics
= true;
1628 ds_put_format(&ds
, "Unrecognized option -%c", *opt
);
1635 /* Doesn't start with -, not an option */
1647 dpctl_p
.is_appctl
= true;
1648 dpctl_p
.output
= dpctl_unixctl_print
;
1651 error
= handler(argc
, argv
, &dpctl_p
) != 0;
1655 unixctl_command_reply_error(conn
, ds_cstr(&ds
));
1657 unixctl_command_reply(conn
, ds_cstr(&ds
));
1664 dpctl_unixctl_register(void)
1666 const struct dpctl_command
*p
;
1668 for (p
= all_commands
; p
->name
!= NULL
; p
++) {
1669 char *cmd_name
= xasprintf("dpctl/%s", p
->name
);
1670 unixctl_command_register(cmd_name
, "", p
->min_args
, p
->max_args
,
1671 dpctl_unixctl_handler
, p
->handler
);