2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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.
22 #include <sys/socket.h>
28 #include <sys/fcntl.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
41 #include "ofp-actions.h"
42 #include "ofp-errors.h"
44 #include "ofp-parse.h"
45 #include "ofp-print.h"
47 #include "ofp-version-opt.h"
49 #include "ofproto/ofproto.h"
50 #include "openflow/nicira-ext.h"
51 #include "openflow/openflow.h"
53 #include "poll-loop.h"
55 #include "stream-ssl.h"
56 #include "socket-util.h"
62 #include "meta-flow.h"
65 VLOG_DEFINE_THIS_MODULE(ofctl
);
67 /* --strict: Use strict matching for flow mod commands? Additionally governs
68 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
72 /* --readd: If true, on replace-flows, re-add even flows that have not changed
73 * (to reset flow counters). */
76 /* -F, --flow-format: Allowed protocols. By default, any protocol is
78 static enum ofputil_protocol allowed_protocols
= OFPUTIL_P_ANY
;
80 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
81 * commands. Either one of NXPIF_* to force a particular packet_in format, or
82 * -1 to let ovs-ofctl choose the default. */
83 static int preferred_packet_in_format
= -1;
85 /* -m, --more: Additional verbosity for ofp-print functions. */
88 /* --timestamp: Print a timestamp before each received packet on "monitor" and
90 static bool timestamp
;
92 /* --sort, --rsort: Sort order. */
93 enum sort_order
{ SORT_ASC
, SORT_DESC
};
94 struct sort_criterion
{
95 const struct mf_field
*field
; /* NULL means to sort by priority. */
96 enum sort_order order
;
98 static struct sort_criterion
*criteria
;
99 static size_t n_criteria
, allocated_criteria
;
101 static const struct command all_commands
[];
103 static void usage(void) NO_RETURN
;
104 static void parse_options(int argc
, char *argv
[]);
106 static bool recv_flow_stats_reply(struct vconn
*, ovs_be32 send_xid
,
107 struct ofpbuf
**replyp
,
108 struct ofputil_flow_stats
*,
109 struct ofpbuf
*ofpacts
);
111 main(int argc
, char *argv
[])
113 set_program_name(argv
[0]);
114 parse_options(argc
, argv
);
115 signal(SIGPIPE
, SIG_IGN
);
116 run_command(argc
- optind
, argv
+ optind
, all_commands
);
121 add_sort_criterion(enum sort_order order
, const char *field
)
123 struct sort_criterion
*sc
;
125 if (n_criteria
>= allocated_criteria
) {
126 criteria
= x2nrealloc(criteria
, &allocated_criteria
, sizeof *criteria
);
129 sc
= &criteria
[n_criteria
++];
130 if (!field
|| !strcasecmp(field
, "priority")) {
133 sc
->field
= mf_from_name(field
);
135 ovs_fatal(0, "%s: unknown field name", field
);
142 parse_options(int argc
, char *argv
[])
145 OPT_STRICT
= UCHAR_MAX
+ 1,
151 OFP_VERSION_OPTION_ENUMS
,
154 static struct option long_options
[] = {
155 {"timeout", required_argument
, NULL
, 't'},
156 {"strict", no_argument
, NULL
, OPT_STRICT
},
157 {"readd", no_argument
, NULL
, OPT_READD
},
158 {"flow-format", required_argument
, NULL
, 'F'},
159 {"packet-in-format", required_argument
, NULL
, 'P'},
160 {"more", no_argument
, NULL
, 'm'},
161 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
162 {"sort", optional_argument
, NULL
, OPT_SORT
},
163 {"rsort", optional_argument
, NULL
, OPT_RSORT
},
164 {"help", no_argument
, NULL
, 'h'},
166 OFP_VERSION_LONG_OPTIONS
,
168 STREAM_SSL_LONG_OPTIONS
,
171 char *short_options
= long_options_to_short_options(long_options
);
173 enum ofputil_protocol version_protocols
;
176 unsigned long int timeout
;
179 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
186 timeout
= strtoul(optarg
, NULL
, 10);
188 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
196 allowed_protocols
= ofputil_protocols_from_string(optarg
);
197 if (!allowed_protocols
) {
198 ovs_fatal(0, "%s: invalid flow format(s)", optarg
);
203 preferred_packet_in_format
=
204 ofputil_packet_in_format_from_string(optarg
);
205 if (preferred_packet_in_format
< 0) {
206 ovs_fatal(0, "unknown packet-in format `%s'", optarg
);
230 add_sort_criterion(SORT_ASC
, optarg
);
234 add_sort_criterion(SORT_DESC
, optarg
);
237 DAEMON_OPTION_HANDLERS
238 OFP_VERSION_OPTION_HANDLERS
240 STREAM_SSL_OPTION_HANDLERS
251 /* Always do a final sort pass based on priority. */
252 add_sort_criterion(SORT_DESC
, "priority");
257 versions
= get_allowed_ofp_versions();
258 version_protocols
= ofputil_protocols_from_version_bitmap(versions
);
259 if (!(allowed_protocols
& version_protocols
)) {
260 char *protocols
= ofputil_protocols_to_string(allowed_protocols
);
261 struct ds version_s
= DS_EMPTY_INITIALIZER
;
263 ofputil_format_version_bitmap_names(&version_s
, versions
);
264 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
265 "any of the enabled flow formats (%s). (Use -O to enable "
266 "additional OpenFlow versions or -F to enable additional "
267 "flow formats.)", ds_cstr(&version_s
), protocols
);
269 allowed_protocols
&= version_protocols
;
270 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
277 printf("%s: OpenFlow switch management utility\n"
278 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
279 "\nFor OpenFlow switches:\n"
280 " show SWITCH show OpenFlow information\n"
281 " dump-desc SWITCH print switch description\n"
282 " dump-tables SWITCH print table stats\n"
283 " mod-port SWITCH IFACE ACT modify port behavior\n"
284 " get-frags SWITCH print fragment handling behavior\n"
285 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
286 " dump-ports SWITCH [PORT] print port statistics\n"
287 " dump-ports-desc SWITCH print port descriptions\n"
288 " dump-flows SWITCH print all flow entries\n"
289 " dump-flows SWITCH FLOW print matching FLOWs\n"
290 " dump-aggregate SWITCH print aggregate flow statistics\n"
291 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
292 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
293 " add-flow SWITCH FLOW add flow described by FLOW\n"
294 " add-flows SWITCH FILE add flows from FILE\n"
295 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
296 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
297 " replace-flows SWITCH FILE replace flows with those in FILE\n"
298 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
299 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
300 " execute ACTIONS on PACKET\n"
301 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
302 " print packets received from SWITCH\n"
303 " snoop SWITCH snoop on SWITCH and its controller\n"
304 "\nFor OpenFlow switches and controllers:\n"
305 " probe TARGET probe whether TARGET is up\n"
306 " ping TARGET [N] latency of N-byte echos\n"
307 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
308 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
309 program_name
, program_name
);
310 vconn_usage(true, false, false);
314 printf("\nOther options:\n"
315 " --strict use strict match for flow commands\n"
316 " --readd replace flows that haven't changed\n"
317 " -F, --flow-format=FORMAT force particular flow format\n"
318 " -P, --packet-in-format=FRMT force particular packet in format\n"
319 " -m, --more be more verbose printing OpenFlow\n"
320 " --timestamp (monitor, snoop) print timestamps\n"
321 " -t, --timeout=SECS give up after SECS seconds\n"
322 " --sort[=field] sort in ascending order\n"
323 " --rsort[=field] sort in descending order\n"
324 " -h, --help display this help message\n"
325 " -V, --version display version information\n");
330 ofctl_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
331 const char *argv
[] OVS_UNUSED
, void *exiting_
)
333 bool *exiting
= exiting_
;
335 unixctl_command_reply(conn
, NULL
);
338 static void run(int retval
, const char *message
, ...)
342 run(int retval
, const char *message
, ...)
347 va_start(args
, message
);
348 ovs_fatal_valist(retval
, message
, args
);
352 /* Generic commands. */
355 open_vconn_socket(const char *name
, struct vconn
**vconnp
)
357 char *vconn_name
= xasprintf("unix:%s", name
);
360 error
= vconn_open(vconn_name
, get_allowed_ofp_versions(), DSCP_DEFAULT
,
362 if (error
&& error
!= ENOENT
) {
363 ovs_fatal(0, "%s: failed to open socket (%s)", name
,
371 enum open_target
{ MGMT
, SNOOP
};
373 static enum ofputil_protocol
374 open_vconn__(const char *name
, enum open_target target
,
375 struct vconn
**vconnp
)
377 const char *suffix
= target
== MGMT
? "mgmt" : "snoop";
378 char *datapath_name
, *datapath_type
, *socket_name
;
379 enum ofputil_protocol protocol
;
384 bridge_path
= xasprintf("%s/%s.%s", ovs_rundir(), name
, suffix
);
386 ofproto_parse_name(name
, &datapath_name
, &datapath_type
);
387 socket_name
= xasprintf("%s/%s.%s", ovs_rundir(), datapath_name
, suffix
);
391 if (strchr(name
, ':')) {
392 run(vconn_open(name
, get_allowed_ofp_versions(), DSCP_DEFAULT
, vconnp
),
393 "connecting to %s", name
);
394 } else if (!open_vconn_socket(name
, vconnp
)) {
396 } else if (!open_vconn_socket(bridge_path
, vconnp
)) {
398 } else if (!open_vconn_socket(socket_name
, vconnp
)) {
401 ovs_fatal(0, "%s is not a bridge or a socket", name
);
404 if (target
== SNOOP
) {
405 vconn_set_recv_any_version(*vconnp
);
411 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp
));
412 error
= vconn_connect_block(*vconnp
);
414 ovs_fatal(0, "%s: failed to connect to socket (%s)", name
,
418 ofp_version
= vconn_get_version(*vconnp
);
419 protocol
= ofputil_protocol_from_ofp_version(ofp_version
);
421 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
427 static enum ofputil_protocol
428 open_vconn(const char *name
, struct vconn
**vconnp
)
430 return open_vconn__(name
, MGMT
, vconnp
);
434 send_openflow_buffer(struct vconn
*vconn
, struct ofpbuf
*buffer
)
436 ofpmsg_update_length(buffer
);
437 run(vconn_send_block(vconn
, buffer
), "failed to send packet to switch");
441 dump_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
443 struct ofpbuf
*reply
;
445 ofpmsg_update_length(request
);
446 run(vconn_transact(vconn
, request
, &reply
), "talking to %s",
447 vconn_get_name(vconn
));
448 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
449 ofpbuf_delete(reply
);
453 dump_trivial_transaction(const char *vconn_name
, enum ofpraw raw
)
455 struct ofpbuf
*request
;
458 open_vconn(vconn_name
, &vconn
);
459 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
460 dump_transaction(vconn
, request
);
465 dump_stats_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
467 const struct ofp_header
*request_oh
= request
->data
;
468 ovs_be32 send_xid
= request_oh
->xid
;
469 enum ofpraw request_raw
;
470 enum ofpraw reply_raw
;
473 ofpraw_decode_partial(&request_raw
, request
->data
, request
->size
);
474 reply_raw
= ofpraw_stats_request_to_reply(request_raw
,
475 request_oh
->version
);
477 send_openflow_buffer(vconn
, request
);
480 struct ofpbuf
*reply
;
482 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
483 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
484 if (send_xid
== recv_xid
) {
487 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
489 ofpraw_decode(&raw
, reply
->data
);
490 if (ofptype_from_ofpraw(raw
) == OFPTYPE_ERROR
) {
492 } else if (raw
== reply_raw
) {
493 done
= !ofpmp_more(reply
->data
);
495 ovs_fatal(0, "received bad reply: %s",
496 ofp_to_string(reply
->data
, reply
->size
,
500 VLOG_DBG("received reply with xid %08"PRIx32
" "
501 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
503 ofpbuf_delete(reply
);
508 dump_trivial_stats_transaction(const char *vconn_name
, enum ofpraw raw
)
510 struct ofpbuf
*request
;
513 open_vconn(vconn_name
, &vconn
);
514 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
515 dump_stats_transaction(vconn
, request
);
519 /* Sends 'request', which should be a request that only has a reply if an error
520 * occurs, and waits for it to succeed or fail. If an error does occur, prints
521 * it and exits with an error.
523 * Destroys all of the 'requests'. */
525 transact_multiple_noreply(struct vconn
*vconn
, struct list
*requests
)
527 struct ofpbuf
*request
, *reply
;
529 LIST_FOR_EACH (request
, list_node
, requests
) {
530 ofpmsg_update_length(request
);
533 run(vconn_transact_multiple_noreply(vconn
, requests
, &reply
),
534 "talking to %s", vconn_get_name(vconn
));
536 ofp_print(stderr
, reply
->data
, reply
->size
, verbosity
+ 2);
539 ofpbuf_delete(reply
);
542 /* Sends 'request', which should be a request that only has a reply if an error
543 * occurs, and waits for it to succeed or fail. If an error does occur, prints
544 * it and exits with an error.
546 * Destroys 'request'. */
548 transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
)
550 struct list requests
;
552 list_init(&requests
);
553 list_push_back(&requests
, &request
->list_node
);
554 transact_multiple_noreply(vconn
, &requests
);
558 fetch_switch_config(struct vconn
*vconn
, struct ofp_switch_config
*config_
)
560 struct ofp_switch_config
*config
;
561 struct ofpbuf
*request
;
562 struct ofpbuf
*reply
;
565 request
= ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST
,
566 vconn_get_version(vconn
), 0);
567 run(vconn_transact(vconn
, request
, &reply
),
568 "talking to %s", vconn_get_name(vconn
));
570 if (ofptype_pull(&type
, reply
) || type
!= OFPTYPE_GET_CONFIG_REPLY
) {
571 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn
));
574 config
= ofpbuf_pull(reply
, sizeof *config
);
577 ofpbuf_delete(reply
);
581 set_switch_config(struct vconn
*vconn
, const struct ofp_switch_config
*config
)
583 struct ofpbuf
*request
;
585 request
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, vconn_get_version(vconn
), 0);
586 ofpbuf_put(request
, config
, sizeof *config
);
588 transact_noreply(vconn
, request
);
592 ofctl_show(int argc OVS_UNUSED
, char *argv
[])
594 const char *vconn_name
= argv
[1];
596 struct ofpbuf
*request
;
597 struct ofpbuf
*reply
;
600 open_vconn(vconn_name
, &vconn
);
601 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
,
602 vconn_get_version(vconn
), 0);
603 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
605 trunc
= ofputil_switch_features_ports_trunc(reply
);
606 ofp_print(stdout
, reply
->data
, reply
->size
, verbosity
+ 1);
608 ofpbuf_delete(reply
);
611 /* The Features Reply may not contain all the ports, so send a
612 * Port Description stats request, which doesn't have size
614 dump_trivial_stats_transaction(vconn_name
,
615 OFPRAW_OFPST_PORT_DESC_REQUEST
);
617 dump_trivial_transaction(vconn_name
, OFPRAW_OFPT_GET_CONFIG_REQUEST
);
622 ofctl_dump_desc(int argc OVS_UNUSED
, char *argv
[])
624 dump_trivial_stats_transaction(argv
[1], OFPRAW_OFPST_DESC_REQUEST
);
628 ofctl_dump_tables(int argc OVS_UNUSED
, char *argv
[])
630 dump_trivial_stats_transaction(argv
[1], OFPRAW_OFPST_TABLE_REQUEST
);
634 fetch_port_by_features(const char *vconn_name
,
635 const char *port_name
, unsigned int port_no
,
636 struct ofputil_phy_port
*pp
, bool *trunc
)
638 struct ofputil_switch_features features
;
639 const struct ofp_header
*oh
;
640 struct ofpbuf
*request
, *reply
;
647 /* Fetch the switch's ofp_switch_features. */
648 open_vconn(vconn_name
, &vconn
);
649 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
,
650 vconn_get_version(vconn
), 0);
651 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
655 if (ofptype_decode(&type
, reply
->data
)
656 || type
!= OFPTYPE_FEATURES_REPLY
) {
657 ovs_fatal(0, "%s: received bad features reply", vconn_name
);
661 if (ofputil_switch_features_ports_trunc(reply
)) {
666 error
= ofputil_decode_switch_features(oh
, &features
, &b
);
668 ovs_fatal(0, "%s: failed to decode features reply (%s)",
669 vconn_name
, ofperr_to_string(error
));
672 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
673 if (port_no
!= UINT_MAX
674 ? port_no
== pp
->port_no
675 : !strcmp(pp
->name
, port_name
)) {
682 ofpbuf_delete(reply
);
687 fetch_port_by_stats(const char *vconn_name
,
688 const char *port_name
, unsigned int port_no
,
689 struct ofputil_phy_port
*pp
)
691 struct ofpbuf
*request
;
697 request
= ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST
, OFP10_VERSION
, 0);
698 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
700 open_vconn(vconn_name
, &vconn
);
701 send_openflow_buffer(vconn
, request
);
704 struct ofpbuf
*reply
;
706 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
707 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
708 if (send_xid
== recv_xid
) {
709 struct ofp_header
*oh
= reply
->data
;
714 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
715 if (ofptype_pull(&type
, &b
)
716 || type
!= OFPTYPE_PORT_DESC_STATS_REPLY
) {
717 ovs_fatal(0, "received bad reply: %s",
718 ofp_to_string(reply
->data
, reply
->size
,
722 flags
= ofpmp_flags(oh
);
723 done
= !(flags
& OFPSF_REPLY_MORE
);
726 /* We've already found the port, but we need to drain
727 * the queue of any other replies for this request. */
731 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
732 if (port_no
!= UINT_MAX
? port_no
== pp
->port_no
733 : !strcmp(pp
->name
, port_name
)) {
739 VLOG_DBG("received reply with xid %08"PRIx32
" "
740 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
742 ofpbuf_delete(reply
);
750 /* Opens a connection to 'vconn_name', fetches the port structure for
751 * 'port_name' (which may be a port name or number), and copies it into
754 fetch_ofputil_phy_port(const char *vconn_name
, const char *port_name
,
755 struct ofputil_phy_port
*pp
)
757 unsigned int port_no
;
761 /* Try to interpret the argument as a port number. */
762 if (!str_to_uint(port_name
, 10, &port_no
)) {
766 /* Try to find the port based on the Features Reply. If it looks
767 * like the results may be truncated, then use the Port Description
768 * stats message introduced in OVS 1.7. */
769 found
= fetch_port_by_features(vconn_name
, port_name
, port_no
, pp
,
772 found
= fetch_port_by_stats(vconn_name
, port_name
, port_no
, pp
);
776 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name
, port_name
);
780 /* Returns the port number corresponding to 'port_name' (which may be a port
781 * name or number) within the switch 'vconn_name'. */
783 str_to_port_no(const char *vconn_name
, const char *port_name
)
787 if (ofputil_port_from_string(port_name
, &port_no
)) {
790 struct ofputil_phy_port pp
;
792 fetch_ofputil_phy_port(vconn_name
, port_name
, &pp
);
798 try_set_protocol(struct vconn
*vconn
, enum ofputil_protocol want
,
799 enum ofputil_protocol
*cur
)
802 struct ofpbuf
*request
, *reply
;
803 enum ofputil_protocol next
;
805 request
= ofputil_encode_set_protocol(*cur
, want
, &next
);
810 run(vconn_transact_noreply(vconn
, request
, &reply
),
811 "talking to %s", vconn_get_name(vconn
));
813 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
814 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
815 vconn_get_name(vconn
), s
);
817 ofpbuf_delete(reply
);
825 static enum ofputil_protocol
826 set_protocol_for_flow_dump(struct vconn
*vconn
,
827 enum ofputil_protocol cur_protocol
,
828 enum ofputil_protocol usable_protocols
)
833 for (i
= 0; i
< ofputil_n_flow_dump_protocols
; i
++) {
834 enum ofputil_protocol f
= ofputil_flow_dump_protocols
[i
];
835 if (f
& usable_protocols
& allowed_protocols
836 && try_set_protocol(vconn
, f
, &cur_protocol
)) {
841 usable_s
= ofputil_protocols_to_string(usable_protocols
);
842 if (usable_protocols
& allowed_protocols
) {
843 ovs_fatal(0, "switch does not support any of the usable flow "
844 "formats (%s)", usable_s
);
846 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
847 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
848 "allowed flow formats (%s)", usable_s
, allowed_s
);
852 static struct vconn
*
853 prepare_dump_flows(int argc
, char *argv
[], bool aggregate
,
854 struct ofpbuf
**requestp
)
856 enum ofputil_protocol usable_protocols
, protocol
;
857 struct ofputil_flow_stats_request fsr
;
860 parse_ofp_flow_stats_request_str(&fsr
, aggregate
, argc
> 2 ? argv
[2] : "");
861 usable_protocols
= ofputil_flow_stats_request_usable_protocols(&fsr
);
863 protocol
= open_vconn(argv
[1], &vconn
);
864 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
865 *requestp
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
870 ofctl_dump_flows__(int argc
, char *argv
[], bool aggregate
)
872 struct ofpbuf
*request
;
875 vconn
= prepare_dump_flows(argc
, argv
, aggregate
, &request
);
876 dump_stats_transaction(vconn
, request
);
881 compare_flows(const void *afs_
, const void *bfs_
)
883 const struct ofputil_flow_stats
*afs
= afs_
;
884 const struct ofputil_flow_stats
*bfs
= bfs_
;
885 const struct match
*a
= &afs
->match
;
886 const struct match
*b
= &bfs
->match
;
887 const struct sort_criterion
*sc
;
889 for (sc
= criteria
; sc
< &criteria
[n_criteria
]; sc
++) {
890 const struct mf_field
*f
= sc
->field
;
894 unsigned int a_pri
= afs
->priority
;
895 unsigned int b_pri
= bfs
->priority
;
896 ret
= a_pri
< b_pri
? -1 : a_pri
> b_pri
;
900 ina
= mf_are_prereqs_ok(f
, &a
->flow
) && !mf_is_all_wild(f
, &a
->wc
);
901 inb
= mf_are_prereqs_ok(f
, &b
->flow
) && !mf_is_all_wild(f
, &b
->wc
);
903 /* Skip the test for sc->order, so that missing fields always
904 * sort to the end whether we're sorting in ascending or
905 * descending order. */
908 union mf_value aval
, bval
;
910 mf_get_value(f
, &a
->flow
, &aval
);
911 mf_get_value(f
, &b
->flow
, &bval
);
912 ret
= memcmp(&aval
, &bval
, f
->n_bytes
);
917 return sc
->order
== SORT_ASC
? ret
: -ret
;
925 ofctl_dump_flows(int argc
, char *argv
[])
928 return ofctl_dump_flows__(argc
, argv
, false);
930 struct ofputil_flow_stats
*fses
;
931 size_t n_fses
, allocated_fses
;
932 struct ofpbuf
*request
;
933 struct ofpbuf ofpacts
;
934 struct ofpbuf
*reply
;
940 vconn
= prepare_dump_flows(argc
, argv
, false, &request
);
941 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
942 send_openflow_buffer(vconn
, request
);
945 n_fses
= allocated_fses
= 0;
947 ofpbuf_init(&ofpacts
, 0);
949 struct ofputil_flow_stats
*fs
;
951 if (n_fses
>= allocated_fses
) {
952 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
956 if (!recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
,
960 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
963 ofpbuf_uninit(&ofpacts
);
965 qsort(fses
, n_fses
, sizeof *fses
, compare_flows
);
968 for (i
= 0; i
< n_fses
; i
++) {
970 ofp_print_flow_stats(&s
, &fses
[i
]);
975 for (i
= 0; i
< n_fses
; i
++) {
976 free(fses
[i
].ofpacts
);
985 ofctl_dump_aggregate(int argc
, char *argv
[])
987 return ofctl_dump_flows__(argc
, argv
, true);
991 ofctl_queue_stats(int argc
, char *argv
[])
993 struct ofpbuf
*request
;
995 struct ofputil_queue_stats_request oqs
;
997 open_vconn(argv
[1], &vconn
);
999 if (argc
> 2 && argv
[2][0] && strcasecmp(argv
[2], "all")) {
1000 oqs
.port_no
= str_to_port_no(argv
[1], argv
[2]);
1002 oqs
.port_no
= OFPP_ANY
;
1004 if (argc
> 3 && argv
[3][0] && strcasecmp(argv
[3], "all")) {
1005 oqs
.queue_id
= atoi(argv
[3]);
1007 oqs
.queue_id
= OFPQ_ALL
;
1010 request
= ofputil_encode_queue_stats_request(vconn_get_version(vconn
), &oqs
);
1011 dump_stats_transaction(vconn
, request
);
1015 static enum ofputil_protocol
1016 open_vconn_for_flow_mod(const char *remote
,
1017 const struct ofputil_flow_mod
*fms
, size_t n_fms
,
1018 struct vconn
**vconnp
)
1020 enum ofputil_protocol usable_protocols
;
1021 enum ofputil_protocol cur_protocol
;
1025 /* Figure out what flow formats will work. */
1026 usable_protocols
= ofputil_flow_mod_usable_protocols(fms
, n_fms
);
1027 if (!(usable_protocols
& allowed_protocols
)) {
1028 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
1029 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1030 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1031 "allowed flow formats (%s)", usable_s
, allowed_s
);
1034 /* If the initial flow format is allowed and usable, keep it. */
1035 cur_protocol
= open_vconn(remote
, vconnp
);
1036 if (usable_protocols
& allowed_protocols
& cur_protocol
) {
1037 return cur_protocol
;
1040 /* Otherwise try each flow format in turn. */
1041 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1042 enum ofputil_protocol f
= 1 << i
;
1044 if (f
!= cur_protocol
1045 && f
& usable_protocols
& allowed_protocols
1046 && try_set_protocol(*vconnp
, f
, &cur_protocol
)) {
1051 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1052 ovs_fatal(0, "switch does not support any of the usable flow "
1053 "formats (%s)", usable_s
);
1057 ofctl_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
,
1060 enum ofputil_protocol protocol
;
1061 struct vconn
*vconn
;
1064 protocol
= open_vconn_for_flow_mod(remote
, fms
, n_fms
, &vconn
);
1066 for (i
= 0; i
< n_fms
; i
++) {
1067 struct ofputil_flow_mod
*fm
= &fms
[i
];
1069 transact_noreply(vconn
, ofputil_encode_flow_mod(fm
, protocol
));
1076 ofctl_flow_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
1078 struct ofputil_flow_mod
*fms
= NULL
;
1081 parse_ofp_flow_mod_file(argv
[2], command
, &fms
, &n_fms
);
1082 ofctl_flow_mod__(argv
[1], fms
, n_fms
);
1087 ofctl_flow_mod(int argc
, char *argv
[], uint16_t command
)
1089 if (argc
> 2 && !strcmp(argv
[2], "-")) {
1090 ofctl_flow_mod_file(argc
, argv
, command
);
1092 struct ofputil_flow_mod fm
;
1093 parse_ofp_flow_mod_str(&fm
, argc
> 2 ? argv
[2] : "", command
, false);
1094 ofctl_flow_mod__(argv
[1], &fm
, 1);
1099 ofctl_add_flow(int argc
, char *argv
[])
1101 ofctl_flow_mod(argc
, argv
, OFPFC_ADD
);
1105 ofctl_add_flows(int argc
, char *argv
[])
1107 ofctl_flow_mod_file(argc
, argv
, OFPFC_ADD
);
1111 ofctl_mod_flows(int argc
, char *argv
[])
1113 ofctl_flow_mod(argc
, argv
, strict
? OFPFC_MODIFY_STRICT
: OFPFC_MODIFY
);
1117 ofctl_del_flows(int argc
, char *argv
[])
1119 ofctl_flow_mod(argc
, argv
, strict
? OFPFC_DELETE_STRICT
: OFPFC_DELETE
);
1123 set_packet_in_format(struct vconn
*vconn
,
1124 enum nx_packet_in_format packet_in_format
)
1126 struct ofpbuf
*spif
;
1128 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1130 transact_noreply(vconn
, spif
);
1131 VLOG_DBG("%s: using user-specified packet in format %s",
1132 vconn_get_name(vconn
),
1133 ofputil_packet_in_format_to_string(packet_in_format
));
1137 monitor_set_invalid_ttl_to_controller(struct vconn
*vconn
)
1139 struct ofp_switch_config config
;
1140 enum ofp_config_flags flags
;
1142 fetch_switch_config(vconn
, &config
);
1143 flags
= ntohs(config
.flags
);
1144 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1145 /* Set the invalid ttl config. */
1146 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
1148 config
.flags
= htons(flags
);
1149 set_switch_config(vconn
, &config
);
1151 /* Then retrieve the configuration to see if it really took. OpenFlow
1152 * doesn't define error reporting for bad modes, so this is all we can
1154 fetch_switch_config(vconn
, &config
);
1155 flags
= ntohs(config
.flags
);
1156 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1157 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1158 "switch probably doesn't support mode)");
1165 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1166 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1167 * an error message and stores NULL in '*msgp'. */
1169 openflow_from_hex(const char *hex
, struct ofpbuf
**msgp
)
1171 struct ofp_header
*oh
;
1174 msg
= ofpbuf_new(strlen(hex
) / 2);
1177 if (ofpbuf_put_hex(msg
, hex
, NULL
)[0] != '\0') {
1179 return "Trailing garbage in hex data";
1182 if (msg
->size
< sizeof(struct ofp_header
)) {
1184 return "Message too short for OpenFlow";
1188 if (msg
->size
!= ntohs(oh
->length
)) {
1190 return "Message size does not match length in OpenFlow header";
1198 ofctl_send(struct unixctl_conn
*conn
, int argc
,
1199 const char *argv
[], void *vconn_
)
1201 struct vconn
*vconn
= vconn_
;
1208 for (i
= 1; i
< argc
; i
++) {
1209 const char *error_msg
;
1213 error_msg
= openflow_from_hex(argv
[i
], &msg
);
1215 ds_put_format(&reply
, "%s\n", error_msg
);
1220 fprintf(stderr
, "send: ");
1221 ofp_print(stderr
, msg
->data
, msg
->size
, verbosity
);
1223 error
= vconn_send_block(vconn
, msg
);
1226 ds_put_format(&reply
, "%s\n", strerror(error
));
1229 ds_put_cstr(&reply
, "sent\n");
1234 unixctl_command_reply(conn
, ds_cstr(&reply
));
1236 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
1241 struct barrier_aux
{
1242 struct vconn
*vconn
; /* OpenFlow connection for sending barrier. */
1243 struct unixctl_conn
*conn
; /* Connection waiting for barrier response. */
1247 ofctl_barrier(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1248 const char *argv
[] OVS_UNUSED
, void *aux_
)
1250 struct barrier_aux
*aux
= aux_
;
1255 unixctl_command_reply_error(conn
, "already waiting for barrier reply");
1259 msg
= ofputil_encode_barrier_request(vconn_get_version(aux
->vconn
));
1260 error
= vconn_send_block(aux
->vconn
, msg
);
1263 unixctl_command_reply_error(conn
, strerror(error
));
1270 ofctl_set_output_file(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1271 const char *argv
[], void *aux OVS_UNUSED
)
1275 fd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
1277 unixctl_command_reply_error(conn
, strerror(errno
));
1282 dup2(fd
, STDERR_FILENO
);
1284 unixctl_command_reply(conn
, NULL
);
1288 ofctl_block(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1289 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1291 bool *blocked
= blocked_
;
1295 unixctl_command_reply(conn
, NULL
);
1297 unixctl_command_reply(conn
, "already blocking");
1302 ofctl_unblock(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1303 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1305 bool *blocked
= blocked_
;
1309 unixctl_command_reply(conn
, NULL
);
1311 unixctl_command_reply(conn
, "already unblocked");
1316 monitor_vconn(struct vconn
*vconn
)
1318 struct barrier_aux barrier_aux
= { vconn
, NULL
};
1319 struct unixctl_server
*server
;
1320 bool exiting
= false;
1321 bool blocked
= false;
1324 daemon_save_fd(STDERR_FILENO
);
1326 error
= unixctl_server_create(NULL
, &server
);
1328 ovs_fatal(error
, "failed to create unixctl server");
1330 unixctl_command_register("exit", "", 0, 0, ofctl_exit
, &exiting
);
1331 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX
,
1333 unixctl_command_register("ofctl/barrier", "", 0, 0,
1334 ofctl_barrier
, &barrier_aux
);
1335 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1336 ofctl_set_output_file
, NULL
);
1338 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block
, &blocked
);
1339 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock
,
1342 daemonize_complete();
1348 unixctl_server_run(server
);
1353 retval
= vconn_recv(vconn
, &b
);
1354 if (retval
== EAGAIN
) {
1357 run(retval
, "vconn_recv");
1360 time_t now
= time_wall();
1363 strftime(s
, sizeof s
, "%Y-%m-%d %H:%M:%S: ", gmtime(&now
));
1367 ofptype_decode(&type
, b
->data
);
1368 ofp_print(stderr
, b
->data
, b
->size
, verbosity
+ 2);
1371 if (barrier_aux
.conn
&& type
== OFPTYPE_BARRIER_REPLY
) {
1372 unixctl_command_reply(barrier_aux
.conn
, NULL
);
1373 barrier_aux
.conn
= NULL
;
1382 vconn_run_wait(vconn
);
1384 vconn_recv_wait(vconn
);
1386 unixctl_server_wait(server
);
1390 unixctl_server_destroy(server
);
1394 ofctl_monitor(int argc
, char *argv
[])
1396 struct vconn
*vconn
;
1399 open_vconn(argv
[1], &vconn
);
1400 for (i
= 2; i
< argc
; i
++) {
1401 const char *arg
= argv
[i
];
1403 if (isdigit((unsigned char) *arg
)) {
1404 struct ofp_switch_config config
;
1406 fetch_switch_config(vconn
, &config
);
1407 config
.miss_send_len
= htons(atoi(arg
));
1408 set_switch_config(vconn
, &config
);
1409 } else if (!strcmp(arg
, "invalid_ttl")) {
1410 monitor_set_invalid_ttl_to_controller(vconn
);
1411 } else if (!strncmp(arg
, "watch:", 6)) {
1412 struct ofputil_flow_monitor_request fmr
;
1415 parse_flow_monitor_request(&fmr
, arg
+ 6);
1417 msg
= ofpbuf_new(0);
1418 ofputil_append_flow_monitor_request(&fmr
, msg
);
1419 dump_stats_transaction(vconn
, msg
);
1421 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg
);
1425 if (preferred_packet_in_format
>= 0) {
1426 set_packet_in_format(vconn
, preferred_packet_in_format
);
1428 enum ofp_version version
= vconn_get_version(vconn
);
1431 case OFP10_VERSION
: {
1432 struct ofpbuf
*spif
, *reply
;
1434 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1436 run(vconn_transact_noreply(vconn
, spif
, &reply
),
1437 "talking to %s", vconn_get_name(vconn
));
1439 char *s
= ofp_to_string(reply
->data
, reply
->size
, 2);
1440 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1441 " replied: %s. Falling back to the switch default.",
1442 vconn_get_name(vconn
), s
);
1444 ofpbuf_delete(reply
);
1457 monitor_vconn(vconn
);
1461 ofctl_snoop(int argc OVS_UNUSED
, char *argv
[])
1463 struct vconn
*vconn
;
1465 open_vconn__(argv
[1], SNOOP
, &vconn
);
1466 monitor_vconn(vconn
);
1470 ofctl_dump_ports(int argc
, char *argv
[])
1472 struct ofpbuf
*request
;
1473 struct vconn
*vconn
;
1476 open_vconn(argv
[1], &vconn
);
1477 port
= argc
> 2 ? str_to_port_no(argv
[1], argv
[2]) : OFPP_ANY
;
1478 request
= ofputil_encode_dump_ports_request(vconn_get_version(vconn
), port
);
1479 dump_stats_transaction(vconn
, request
);
1484 ofctl_dump_ports_desc(int argc OVS_UNUSED
, char *argv
[])
1486 dump_trivial_stats_transaction(argv
[1], OFPRAW_OFPST_PORT_DESC_REQUEST
);
1490 ofctl_probe(int argc OVS_UNUSED
, char *argv
[])
1492 struct ofpbuf
*request
;
1493 struct vconn
*vconn
;
1494 struct ofpbuf
*reply
;
1496 open_vconn(argv
[1], &vconn
);
1497 request
= make_echo_request(vconn_get_version(vconn
));
1498 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", argv
[1]);
1499 if (reply
->size
!= sizeof(struct ofp_header
)) {
1500 ovs_fatal(0, "reply does not match request");
1502 ofpbuf_delete(reply
);
1507 ofctl_packet_out(int argc
, char *argv
[])
1509 enum ofputil_protocol protocol
;
1510 struct ofputil_packet_out po
;
1511 struct ofpbuf ofpacts
;
1512 struct vconn
*vconn
;
1515 ofpbuf_init(&ofpacts
, 64);
1516 parse_ofpacts(argv
[3], &ofpacts
);
1518 po
.buffer_id
= UINT32_MAX
;
1519 po
.in_port
= str_to_port_no(argv
[1], argv
[2]);
1520 po
.ofpacts
= ofpacts
.data
;
1521 po
.ofpacts_len
= ofpacts
.size
;
1523 protocol
= open_vconn(argv
[1], &vconn
);
1524 for (i
= 4; i
< argc
; i
++) {
1525 struct ofpbuf
*packet
, *opo
;
1526 const char *error_msg
;
1528 error_msg
= eth_from_hex(argv
[i
], &packet
);
1530 ovs_fatal(0, "%s", error_msg
);
1533 po
.packet
= packet
->data
;
1534 po
.packet_len
= packet
->size
;
1535 opo
= ofputil_encode_packet_out(&po
, protocol
);
1536 transact_noreply(vconn
, opo
);
1537 ofpbuf_delete(packet
);
1540 ofpbuf_uninit(&ofpacts
);
1544 ofctl_mod_port(int argc OVS_UNUSED
, char *argv
[])
1546 struct ofp_config_flag
{
1547 const char *name
; /* The flag's name. */
1548 enum ofputil_port_config bit
; /* Bit to turn on or off. */
1549 bool on
; /* Value to set the bit to. */
1551 static const struct ofp_config_flag flags
[] = {
1552 { "up", OFPUTIL_PC_PORT_DOWN
, false },
1553 { "down", OFPUTIL_PC_PORT_DOWN
, true },
1554 { "stp", OFPUTIL_PC_NO_STP
, false },
1555 { "receive", OFPUTIL_PC_NO_RECV
, false },
1556 { "receive-stp", OFPUTIL_PC_NO_RECV_STP
, false },
1557 { "flood", OFPUTIL_PC_NO_FLOOD
, false },
1558 { "forward", OFPUTIL_PC_NO_FWD
, false },
1559 { "packet-in", OFPUTIL_PC_NO_PACKET_IN
, false },
1562 const struct ofp_config_flag
*flag
;
1563 enum ofputil_protocol protocol
;
1564 struct ofputil_port_mod pm
;
1565 struct ofputil_phy_port pp
;
1566 struct vconn
*vconn
;
1567 const char *command
;
1570 fetch_ofputil_phy_port(argv
[1], argv
[2], &pp
);
1572 pm
.port_no
= pp
.port_no
;
1573 memcpy(pm
.hw_addr
, pp
.hw_addr
, ETH_ADDR_LEN
);
1578 if (!strncasecmp(argv
[3], "no-", 3)) {
1579 command
= argv
[3] + 3;
1581 } else if (!strncasecmp(argv
[3], "no", 2)) {
1582 command
= argv
[3] + 2;
1588 for (flag
= flags
; flag
< &flags
[ARRAY_SIZE(flags
)]; flag
++) {
1589 if (!strcasecmp(command
, flag
->name
)) {
1590 pm
.mask
= flag
->bit
;
1591 pm
.config
= flag
->on
^ not ? flag
->bit
: 0;
1595 ovs_fatal(0, "unknown mod-port command '%s'", argv
[3]);
1598 protocol
= open_vconn(argv
[1], &vconn
);
1599 transact_noreply(vconn
, ofputil_encode_port_mod(&pm
, protocol
));
1604 ofctl_get_frags(int argc OVS_UNUSED
, char *argv
[])
1606 struct ofp_switch_config config
;
1607 struct vconn
*vconn
;
1609 open_vconn(argv
[1], &vconn
);
1610 fetch_switch_config(vconn
, &config
);
1611 puts(ofputil_frag_handling_to_string(ntohs(config
.flags
)));
1616 ofctl_set_frags(int argc OVS_UNUSED
, char *argv
[])
1618 struct ofp_switch_config config
;
1619 enum ofp_config_flags mode
;
1620 struct vconn
*vconn
;
1623 if (!ofputil_frag_handling_from_string(argv
[2], &mode
)) {
1624 ovs_fatal(0, "%s: unknown fragment handling mode", argv
[2]);
1627 open_vconn(argv
[1], &vconn
);
1628 fetch_switch_config(vconn
, &config
);
1629 flags
= htons(mode
) | (config
.flags
& htons(~OFPC_FRAG_MASK
));
1630 if (flags
!= config
.flags
) {
1631 /* Set the configuration. */
1632 config
.flags
= flags
;
1633 set_switch_config(vconn
, &config
);
1635 /* Then retrieve the configuration to see if it really took. OpenFlow
1636 * doesn't define error reporting for bad modes, so this is all we can
1638 fetch_switch_config(vconn
, &config
);
1639 if (flags
!= config
.flags
) {
1640 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1641 "switch probably doesn't support mode \"%s\")",
1642 argv
[1], ofputil_frag_handling_to_string(mode
));
1649 ofctl_ping(int argc
, char *argv
[])
1651 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
1652 unsigned int payload
;
1653 struct vconn
*vconn
;
1656 payload
= argc
> 2 ? atoi(argv
[2]) : 64;
1657 if (payload
> max_payload
) {
1658 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload
);
1661 open_vconn(argv
[1], &vconn
);
1662 for (i
= 0; i
< 10; i
++) {
1663 struct timeval start
, end
;
1664 struct ofpbuf
*request
, *reply
;
1665 const struct ofp_header
*rpy_hdr
;
1668 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
1669 vconn_get_version(vconn
), payload
);
1670 random_bytes(ofpbuf_put_uninit(request
, payload
), payload
);
1672 xgettimeofday(&start
);
1673 run(vconn_transact(vconn
, ofpbuf_clone(request
), &reply
), "transact");
1674 xgettimeofday(&end
);
1676 rpy_hdr
= reply
->data
;
1677 if (ofptype_pull(&type
, reply
)
1678 || type
!= OFPTYPE_ECHO_REPLY
1679 || reply
->size
!= payload
1680 || memcmp(request
->l3
, reply
->l3
, payload
)) {
1681 printf("Reply does not match request. Request:\n");
1682 ofp_print(stdout
, request
, request
->size
, verbosity
+ 2);
1684 ofp_print(stdout
, reply
, reply
->size
, verbosity
+ 2);
1686 printf("%zu bytes from %s: xid=%08"PRIx32
" time=%.1f ms\n",
1687 reply
->size
, argv
[1], ntohl(rpy_hdr
->xid
),
1688 (1000*(double)(end
.tv_sec
- start
.tv_sec
))
1689 + (.001*(end
.tv_usec
- start
.tv_usec
)));
1690 ofpbuf_delete(request
);
1691 ofpbuf_delete(reply
);
1697 ofctl_benchmark(int argc OVS_UNUSED
, char *argv
[])
1699 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
1700 struct timeval start
, end
;
1701 unsigned int payload_size
, message_size
;
1702 struct vconn
*vconn
;
1707 payload_size
= atoi(argv
[2]);
1708 if (payload_size
> max_payload
) {
1709 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload
);
1711 message_size
= sizeof(struct ofp_header
) + payload_size
;
1713 count
= atoi(argv
[3]);
1715 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1716 count
, message_size
, count
* message_size
);
1718 open_vconn(argv
[1], &vconn
);
1719 xgettimeofday(&start
);
1720 for (i
= 0; i
< count
; i
++) {
1721 struct ofpbuf
*request
, *reply
;
1723 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
1724 vconn_get_version(vconn
), payload_size
);
1725 ofpbuf_put_zeros(request
, payload_size
);
1726 run(vconn_transact(vconn
, request
, &reply
), "transact");
1727 ofpbuf_delete(reply
);
1729 xgettimeofday(&end
);
1732 duration
= ((1000*(double)(end
.tv_sec
- start
.tv_sec
))
1733 + (.001*(end
.tv_usec
- start
.tv_usec
)));
1734 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1735 duration
, count
/ (duration
/ 1000.0),
1736 count
* message_size
/ (duration
/ 1000.0));
1740 ofctl_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
1745 /* replace-flows and diff-flows commands. */
1747 /* A flow table entry, possibly with two different versions. */
1749 struct cls_rule rule
; /* Within a "struct classifier". */
1750 struct fte_version
*versions
[2];
1753 /* One version of a Flow Table Entry. */
1754 struct fte_version
{
1756 uint16_t idle_timeout
;
1757 uint16_t hard_timeout
;
1759 struct ofpact
*ofpacts
;
1763 /* Frees 'version' and the data that it owns. */
1765 fte_version_free(struct fte_version
*version
)
1768 free(version
->ofpacts
);
1773 /* Returns true if 'a' and 'b' are the same, false if they differ.
1775 * Ignores differences in 'flags' because there's no way to retrieve flags from
1776 * an OpenFlow switch. We have to assume that they are the same. */
1778 fte_version_equals(const struct fte_version
*a
, const struct fte_version
*b
)
1780 return (a
->cookie
== b
->cookie
1781 && a
->idle_timeout
== b
->idle_timeout
1782 && a
->hard_timeout
== b
->hard_timeout
1783 && ofpacts_equal(a
->ofpacts
, a
->ofpacts_len
,
1784 b
->ofpacts
, b
->ofpacts_len
));
1787 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
1788 * 'index' into 's', followed by a new-line. */
1790 fte_version_format(const struct fte
*fte
, int index
, struct ds
*s
)
1792 const struct fte_version
*version
= fte
->versions
[index
];
1799 cls_rule_format(&fte
->rule
, s
);
1800 if (version
->cookie
!= htonll(0)) {
1801 ds_put_format(s
, " cookie=0x%"PRIx64
, ntohll(version
->cookie
));
1803 if (version
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
1804 ds_put_format(s
, " idle_timeout=%"PRIu16
, version
->idle_timeout
);
1806 if (version
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
1807 ds_put_format(s
, " hard_timeout=%"PRIu16
, version
->hard_timeout
);
1810 ds_put_char(s
, ' ');
1811 ofpacts_format(version
->ofpacts
, version
->ofpacts_len
, s
);
1813 ds_put_char(s
, '\n');
1817 fte_from_cls_rule(const struct cls_rule
*cls_rule
)
1819 return cls_rule
? CONTAINER_OF(cls_rule
, struct fte
, rule
) : NULL
;
1822 /* Frees 'fte' and its versions. */
1824 fte_free(struct fte
*fte
)
1827 fte_version_free(fte
->versions
[0]);
1828 fte_version_free(fte
->versions
[1]);
1829 cls_rule_destroy(&fte
->rule
);
1834 /* Frees all of the FTEs within 'cls'. */
1836 fte_free_all(struct classifier
*cls
)
1838 struct cls_cursor cursor
;
1839 struct fte
*fte
, *next
;
1841 cls_cursor_init(&cursor
, cls
, NULL
);
1842 CLS_CURSOR_FOR_EACH_SAFE (fte
, next
, rule
, &cursor
) {
1843 classifier_remove(cls
, &fte
->rule
);
1846 classifier_destroy(cls
);
1849 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1850 * necessary. Sets 'version' as the version of that rule with the given
1851 * 'index', replacing any existing version, if any.
1853 * Takes ownership of 'version'. */
1855 fte_insert(struct classifier
*cls
, const struct match
*match
,
1856 unsigned int priority
, struct fte_version
*version
, int index
)
1858 struct fte
*old
, *fte
;
1860 fte
= xzalloc(sizeof *fte
);
1861 cls_rule_init(&fte
->rule
, match
, priority
);
1862 fte
->versions
[index
] = version
;
1864 old
= fte_from_cls_rule(classifier_replace(cls
, &fte
->rule
));
1866 fte_version_free(old
->versions
[index
]);
1867 fte
->versions
[!index
] = old
->versions
[!index
];
1868 cls_rule_destroy(&old
->rule
);
1873 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1874 * with the specified 'index'. Returns the flow formats able to represent the
1875 * flows that were read. */
1876 static enum ofputil_protocol
1877 read_flows_from_file(const char *filename
, struct classifier
*cls
, int index
)
1879 enum ofputil_protocol usable_protocols
;
1883 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
1885 ovs_fatal(errno
, "%s: open", filename
);
1889 usable_protocols
= OFPUTIL_P_ANY
;
1890 while (!ds_get_preprocessed_line(&s
, file
)) {
1891 struct fte_version
*version
;
1892 struct ofputil_flow_mod fm
;
1894 parse_ofp_str(&fm
, OFPFC_ADD
, ds_cstr(&s
), true);
1896 version
= xmalloc(sizeof *version
);
1897 version
->cookie
= fm
.new_cookie
;
1898 version
->idle_timeout
= fm
.idle_timeout
;
1899 version
->hard_timeout
= fm
.hard_timeout
;
1900 version
->flags
= fm
.flags
& (OFPFF_SEND_FLOW_REM
| OFPFF10_EMERG
);
1901 version
->ofpacts
= fm
.ofpacts
;
1902 version
->ofpacts_len
= fm
.ofpacts_len
;
1904 usable_protocols
&= ofputil_usable_protocols(&fm
.match
);
1906 fte_insert(cls
, &fm
.match
, fm
.priority
, version
, index
);
1910 if (file
!= stdin
) {
1914 return usable_protocols
;
1918 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
1919 struct ofpbuf
**replyp
,
1920 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
1922 struct ofpbuf
*reply
= *replyp
;
1928 /* Get a flow stats reply message, if we don't already have one. */
1934 run(vconn_recv_block(vconn
, &reply
),
1935 "OpenFlow packet receive failed");
1936 } while (((struct ofp_header
*) reply
->data
)->xid
!= send_xid
);
1938 error
= ofptype_decode(&type
, reply
->data
);
1939 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
1940 ovs_fatal(0, "received bad reply: %s",
1941 ofp_to_string(reply
->data
, reply
->size
,
1946 /* Pull an individual flow stats reply out of the message. */
1947 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
1954 more
= ofpmp_more(reply
->l2
);
1955 ofpbuf_delete(reply
);
1964 ovs_fatal(0, "parse error in reply (%s)",
1965 ofperr_to_string(retval
));
1970 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1971 * format 'protocol', and adds them as flow table entries in 'cls' for the
1972 * version with the specified 'index'. */
1974 read_flows_from_switch(struct vconn
*vconn
,
1975 enum ofputil_protocol protocol
,
1976 struct classifier
*cls
, int index
)
1978 struct ofputil_flow_stats_request fsr
;
1979 struct ofputil_flow_stats fs
;
1980 struct ofpbuf
*request
;
1981 struct ofpbuf ofpacts
;
1982 struct ofpbuf
*reply
;
1985 fsr
.aggregate
= false;
1986 match_init_catchall(&fsr
.match
);
1987 fsr
.out_port
= OFPP_ANY
;
1988 fsr
.table_id
= 0xff;
1989 fsr
.cookie
= fsr
.cookie_mask
= htonll(0);
1990 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
1991 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
1992 send_openflow_buffer(vconn
, request
);
1995 ofpbuf_init(&ofpacts
, 0);
1996 while (recv_flow_stats_reply(vconn
, send_xid
, &reply
, &fs
, &ofpacts
)) {
1997 struct fte_version
*version
;
1999 version
= xmalloc(sizeof *version
);
2000 version
->cookie
= fs
.cookie
;
2001 version
->idle_timeout
= fs
.idle_timeout
;
2002 version
->hard_timeout
= fs
.hard_timeout
;
2004 version
->ofpacts_len
= fs
.ofpacts_len
;
2005 version
->ofpacts
= xmemdup(fs
.ofpacts
, fs
.ofpacts_len
);
2007 fte_insert(cls
, &fs
.match
, fs
.priority
, version
, index
);
2009 ofpbuf_uninit(&ofpacts
);
2013 fte_make_flow_mod(const struct fte
*fte
, int index
, uint16_t command
,
2014 enum ofputil_protocol protocol
, struct list
*packets
)
2016 const struct fte_version
*version
= fte
->versions
[index
];
2017 struct ofputil_flow_mod fm
;
2020 minimatch_expand(&fte
->rule
.match
, &fm
.match
);
2021 fm
.priority
= fte
->rule
.priority
;
2022 fm
.cookie
= htonll(0);
2023 fm
.cookie_mask
= htonll(0);
2024 fm
.new_cookie
= version
->cookie
;
2026 fm
.command
= command
;
2027 fm
.idle_timeout
= version
->idle_timeout
;
2028 fm
.hard_timeout
= version
->hard_timeout
;
2029 fm
.buffer_id
= UINT32_MAX
;
2030 fm
.out_port
= OFPP_ANY
;
2031 fm
.flags
= version
->flags
;
2032 if (command
== OFPFC_ADD
|| command
== OFPFC_MODIFY
||
2033 command
== OFPFC_MODIFY_STRICT
) {
2034 fm
.ofpacts
= version
->ofpacts
;
2035 fm
.ofpacts_len
= version
->ofpacts_len
;
2041 ofm
= ofputil_encode_flow_mod(&fm
, protocol
);
2042 list_push_back(packets
, &ofm
->list_node
);
2046 ofctl_replace_flows(int argc OVS_UNUSED
, char *argv
[])
2048 enum { FILE_IDX
= 0, SWITCH_IDX
= 1 };
2049 enum ofputil_protocol usable_protocols
, protocol
;
2050 struct cls_cursor cursor
;
2051 struct classifier cls
;
2052 struct list requests
;
2053 struct vconn
*vconn
;
2056 classifier_init(&cls
);
2057 usable_protocols
= read_flows_from_file(argv
[2], &cls
, FILE_IDX
);
2059 protocol
= open_vconn(argv
[1], &vconn
);
2060 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
2062 read_flows_from_switch(vconn
, protocol
, &cls
, SWITCH_IDX
);
2064 list_init(&requests
);
2066 /* Delete flows that exist on the switch but not in the file. */
2067 cls_cursor_init(&cursor
, &cls
, NULL
);
2068 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
2069 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2070 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2072 if (sw_ver
&& !file_ver
) {
2073 fte_make_flow_mod(fte
, SWITCH_IDX
, OFPFC_DELETE_STRICT
,
2074 protocol
, &requests
);
2078 /* Add flows that exist in the file but not on the switch.
2079 * Update flows that exist in both places but differ. */
2080 cls_cursor_init(&cursor
, &cls
, NULL
);
2081 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
2082 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2083 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2086 && (readd
|| !sw_ver
|| !fte_version_equals(sw_ver
, file_ver
))) {
2087 fte_make_flow_mod(fte
, FILE_IDX
, OFPFC_ADD
, protocol
, &requests
);
2090 transact_multiple_noreply(vconn
, &requests
);
2097 read_flows_from_source(const char *source
, struct classifier
*cls
, int index
)
2101 if (source
[0] == '/' || source
[0] == '.'
2102 || (!strchr(source
, ':') && !stat(source
, &s
))) {
2103 read_flows_from_file(source
, cls
, index
);
2105 enum ofputil_protocol protocol
;
2106 struct vconn
*vconn
;
2108 protocol
= open_vconn(source
, &vconn
);
2109 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, OFPUTIL_P_ANY
);
2110 read_flows_from_switch(vconn
, protocol
, cls
, index
);
2116 ofctl_diff_flows(int argc OVS_UNUSED
, char *argv
[])
2118 bool differences
= false;
2119 struct cls_cursor cursor
;
2120 struct classifier cls
;
2124 classifier_init(&cls
);
2125 read_flows_from_source(argv
[1], &cls
, 0);
2126 read_flows_from_source(argv
[2], &cls
, 1);
2131 cls_cursor_init(&cursor
, &cls
, NULL
);
2132 CLS_CURSOR_FOR_EACH (fte
, rule
, &cursor
) {
2133 struct fte_version
*a
= fte
->versions
[0];
2134 struct fte_version
*b
= fte
->versions
[1];
2136 if (!a
|| !b
|| !fte_version_equals(a
, b
)) {
2137 fte_version_format(fte
, 0, &a_s
);
2138 fte_version_format(fte
, 1, &b_s
);
2139 if (strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
))) {
2141 printf("-%s", ds_cstr(&a_s
));
2144 printf("+%s", ds_cstr(&b_s
));
2161 /* Undocumented commands for unit testing. */
2164 ofctl_parse_flows__(struct ofputil_flow_mod
*fms
, size_t n_fms
)
2166 enum ofputil_protocol usable_protocols
;
2167 enum ofputil_protocol protocol
= 0;
2171 usable_protocols
= ofputil_flow_mod_usable_protocols(fms
, n_fms
);
2172 usable_s
= ofputil_protocols_to_string(usable_protocols
);
2173 printf("usable protocols: %s\n", usable_s
);
2176 if (!(usable_protocols
& allowed_protocols
)) {
2177 ovs_fatal(0, "no usable protocol");
2179 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
2181 if (protocol
& usable_protocols
& allowed_protocols
) {
2185 ovs_assert(is_pow2(protocol
));
2187 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol
));
2189 for (i
= 0; i
< n_fms
; i
++) {
2190 struct ofputil_flow_mod
*fm
= &fms
[i
];
2193 msg
= ofputil_encode_flow_mod(fm
, protocol
);
2194 ofp_print(stdout
, msg
->data
, msg
->size
, verbosity
);
2201 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2202 * it back to stdout. */
2204 ofctl_parse_flow(int argc OVS_UNUSED
, char *argv
[])
2206 struct ofputil_flow_mod fm
;
2208 parse_ofp_flow_mod_str(&fm
, argv
[1], OFPFC_ADD
, false);
2209 ofctl_parse_flows__(&fm
, 1);
2212 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2213 * add-flows) and prints each of the flows back to stdout. */
2215 ofctl_parse_flows(int argc OVS_UNUSED
, char *argv
[])
2217 struct ofputil_flow_mod
*fms
= NULL
;
2220 parse_ofp_flow_mod_file(argv
[1], OFPFC_ADD
, &fms
, &n_fms
);
2221 ofctl_parse_flows__(fms
, n_fms
);
2226 ofctl_parse_nxm__(bool oxm
)
2231 while (!ds_get_test_line(&in
, stdin
)) {
2232 struct ofpbuf nx_match
;
2234 ovs_be64 cookie
, cookie_mask
;
2238 /* Convert string to nx_match. */
2239 ofpbuf_init(&nx_match
, 0);
2241 match_len
= oxm_match_from_string(ds_cstr(&in
), &nx_match
);
2243 match_len
= nx_match_from_string(ds_cstr(&in
), &nx_match
);
2246 /* Convert nx_match to match. */
2249 error
= oxm_pull_match(&nx_match
, &match
);
2251 error
= nx_pull_match(&nx_match
, match_len
, &match
,
2252 &cookie
, &cookie_mask
);
2256 error
= oxm_pull_match_loose(&nx_match
, &match
);
2258 error
= nx_pull_match_loose(&nx_match
, match_len
, &match
,
2259 &cookie
, &cookie_mask
);
2267 /* Convert match back to nx_match. */
2268 ofpbuf_uninit(&nx_match
);
2269 ofpbuf_init(&nx_match
, 0);
2271 match_len
= oxm_put_match(&nx_match
, &match
);
2272 out
= oxm_match_to_string(nx_match
.data
, match_len
);
2274 match_len
= nx_put_match(&nx_match
, &match
,
2275 cookie
, cookie_mask
);
2276 out
= nx_match_to_string(nx_match
.data
, match_len
);
2282 printf("nx_pull_match() returned error %s\n",
2283 ofperr_get_name(error
));
2286 ofpbuf_uninit(&nx_match
);
2291 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2292 * stdin, does some internal fussing with them, and then prints them back as
2293 * strings on stdout. */
2295 ofctl_parse_nxm(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2297 return ofctl_parse_nxm__(false);
2300 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2301 * stdin, does some internal fussing with them, and then prints them back as
2302 * strings on stdout. */
2304 ofctl_parse_oxm(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2306 return ofctl_parse_nxm__(true);
2310 print_differences(const char *prefix
,
2311 const void *a_
, size_t a_len
,
2312 const void *b_
, size_t b_len
)
2314 const uint8_t *a
= a_
;
2315 const uint8_t *b
= b_
;
2318 for (i
= 0; i
< MIN(a_len
, b_len
); i
++) {
2320 printf("%s%2zu: %02"PRIx8
" -> %02"PRIx8
"\n",
2321 prefix
, i
, a
[i
], b
[i
]);
2324 for (i
= a_len
; i
< b_len
; i
++) {
2325 printf("%s%2zu: (none) -> %02"PRIx8
"\n", prefix
, i
, b
[i
]);
2327 for (i
= b_len
; i
< a_len
; i
++) {
2328 printf("%s%2zu: %02"PRIx8
" -> (none)\n", prefix
, i
, a
[i
]);
2332 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2333 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2334 * on stdout, and then converts them back to hex bytes and prints any
2335 * differences from the input. */
2337 ofctl_parse_ofp10_actions(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2342 while (!ds_get_preprocessed_line(&in
, stdin
)) {
2343 struct ofpbuf of10_out
;
2344 struct ofpbuf of10_in
;
2345 struct ofpbuf ofpacts
;
2350 /* Parse hex bytes. */
2351 ofpbuf_init(&of10_in
, 0);
2352 if (ofpbuf_put_hex(&of10_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
2353 ovs_fatal(0, "Trailing garbage in hex data");
2356 /* Convert to ofpacts. */
2357 ofpbuf_init(&ofpacts
, 0);
2358 size
= of10_in
.size
;
2359 error
= ofpacts_pull_openflow10(&of10_in
, of10_in
.size
, &ofpacts
);
2361 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error
));
2362 ofpbuf_uninit(&ofpacts
);
2363 ofpbuf_uninit(&of10_in
);
2366 ofpbuf_push_uninit(&of10_in
, size
);
2368 /* Print cls_rule. */
2370 ofpacts_format(ofpacts
.data
, ofpacts
.size
, &s
);
2374 /* Convert back to ofp10 actions and print differences from input. */
2375 ofpbuf_init(&of10_out
, 0);
2376 ofpacts_put_openflow10(ofpacts
.data
, ofpacts
.size
, &of10_out
);
2378 print_differences("", of10_in
.data
, of10_in
.size
,
2379 of10_out
.data
, of10_out
.size
);
2382 ofpbuf_uninit(&ofpacts
);
2383 ofpbuf_uninit(&of10_in
);
2384 ofpbuf_uninit(&of10_out
);
2389 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2390 * bytes from stdin, converts them to cls_rules, prints them as strings on
2391 * stdout, and then converts them back to hex bytes and prints any differences
2394 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2395 * values are ignored in the input and will be set to zero when OVS converts
2396 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2397 * it does the conversion to hex, to ensure that in fact they are ignored. */
2399 ofctl_parse_ofp10_match(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2406 while (!ds_get_preprocessed_line(&in
, stdin
)) {
2407 struct ofpbuf match_in
, match_expout
;
2408 struct ofp10_match match_out
;
2409 struct ofp10_match match_normal
;
2413 /* Parse hex bytes to use for expected output. */
2415 ds_put_cstr(&expout
, ds_cstr(&in
));
2416 for (p
= ds_cstr(&expout
); *p
; p
++) {
2421 ofpbuf_init(&match_expout
, 0);
2422 if (ofpbuf_put_hex(&match_expout
, ds_cstr(&expout
), NULL
)[0] != '\0') {
2423 ovs_fatal(0, "Trailing garbage in hex data");
2425 if (match_expout
.size
!= sizeof(struct ofp10_match
)) {
2426 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2427 match_expout
.size
, sizeof(struct ofp10_match
));
2430 /* Parse hex bytes for input. */
2431 for (p
= ds_cstr(&in
); *p
; p
++) {
2433 *p
= "0123456789abcdef"[random_uint32() & 0xf];
2436 ofpbuf_init(&match_in
, 0);
2437 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
2438 ovs_fatal(0, "Trailing garbage in hex data");
2440 if (match_in
.size
!= sizeof(struct ofp10_match
)) {
2441 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2442 match_in
.size
, sizeof(struct ofp10_match
));
2445 /* Convert to cls_rule and print. */
2446 ofputil_match_from_ofp10_match(match_in
.data
, &match
);
2447 match_print(&match
);
2449 /* Convert back to ofp10_match and print differences from input. */
2450 ofputil_match_to_ofp10_match(&match
, &match_out
);
2451 print_differences("", match_expout
.data
, match_expout
.size
,
2452 &match_out
, sizeof match_out
);
2454 /* Normalize, then convert and compare again. */
2455 ofputil_normalize_match(&match
);
2456 ofputil_match_to_ofp10_match(&match
, &match_normal
);
2457 print_differences("normal: ", &match_out
, sizeof match_out
,
2458 &match_normal
, sizeof match_normal
);
2461 ofpbuf_uninit(&match_in
);
2462 ofpbuf_uninit(&match_expout
);
2465 ds_destroy(&expout
);
2468 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2469 * bytes from stdin, converts them to "struct match"es, prints them as strings
2470 * on stdout, and then converts them back to hex bytes and prints any
2471 * differences from the input. */
2473 ofctl_parse_ofp11_match(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2478 while (!ds_get_preprocessed_line(&in
, stdin
)) {
2479 struct ofpbuf match_in
;
2480 struct ofp11_match match_out
;
2484 /* Parse hex bytes. */
2485 ofpbuf_init(&match_in
, 0);
2486 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
2487 ovs_fatal(0, "Trailing garbage in hex data");
2489 if (match_in
.size
!= sizeof(struct ofp11_match
)) {
2490 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2491 match_in
.size
, sizeof(struct ofp11_match
));
2494 /* Convert to match. */
2495 error
= ofputil_match_from_ofp11_match(match_in
.data
, &match
);
2497 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error
));
2498 ofpbuf_uninit(&match_in
);
2503 match_print(&match
);
2505 /* Convert back to ofp11_match and print differences from input. */
2506 ofputil_match_to_ofp11_match(&match
, &match_out
);
2508 print_differences("", match_in
.data
, match_in
.size
,
2509 &match_out
, sizeof match_out
);
2512 ofpbuf_uninit(&match_in
);
2517 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2518 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2519 * on stdout, and then converts them back to hex bytes and prints any
2520 * differences from the input. */
2522 ofctl_parse_ofp11_actions(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2527 while (!ds_get_preprocessed_line(&in
, stdin
)) {
2528 struct ofpbuf of11_out
;
2529 struct ofpbuf of11_in
;
2530 struct ofpbuf ofpacts
;
2535 /* Parse hex bytes. */
2536 ofpbuf_init(&of11_in
, 0);
2537 if (ofpbuf_put_hex(&of11_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
2538 ovs_fatal(0, "Trailing garbage in hex data");
2541 /* Convert to ofpacts. */
2542 ofpbuf_init(&ofpacts
, 0);
2543 size
= of11_in
.size
;
2544 error
= ofpacts_pull_openflow11_actions(&of11_in
, of11_in
.size
,
2547 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error
));
2548 ofpbuf_uninit(&ofpacts
);
2549 ofpbuf_uninit(&of11_in
);
2552 ofpbuf_push_uninit(&of11_in
, size
);
2554 /* Print cls_rule. */
2556 ofpacts_format(ofpacts
.data
, ofpacts
.size
, &s
);
2560 /* Convert back to ofp11 actions and print differences from input. */
2561 ofpbuf_init(&of11_out
, 0);
2562 ofpacts_put_openflow11_actions(ofpacts
.data
, ofpacts
.size
, &of11_out
);
2564 print_differences("", of11_in
.data
, of11_in
.size
,
2565 of11_out
.data
, of11_out
.size
);
2568 ofpbuf_uninit(&ofpacts
);
2569 ofpbuf_uninit(&of11_in
);
2570 ofpbuf_uninit(&of11_out
);
2575 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2576 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2577 * them as strings on stdout, and then converts them back to hex bytes and
2578 * prints any differences from the input. */
2580 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2585 while (!ds_get_preprocessed_line(&in
, stdin
)) {
2586 struct ofpbuf of11_out
;
2587 struct ofpbuf of11_in
;
2588 struct ofpbuf ofpacts
;
2593 /* Parse hex bytes. */
2594 ofpbuf_init(&of11_in
, 0);
2595 if (ofpbuf_put_hex(&of11_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
2596 ovs_fatal(0, "Trailing garbage in hex data");
2599 /* Convert to ofpacts. */
2600 ofpbuf_init(&ofpacts
, 0);
2601 size
= of11_in
.size
;
2602 error
= ofpacts_pull_openflow11_instructions(&of11_in
, of11_in
.size
,
2605 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error
));
2606 ofpbuf_uninit(&ofpacts
);
2607 ofpbuf_uninit(&of11_in
);
2610 ofpbuf_push_uninit(&of11_in
, size
);
2612 /* Print cls_rule. */
2614 ofpacts_format(ofpacts
.data
, ofpacts
.size
, &s
);
2618 /* Convert back to ofp11 instructions and print differences from
2620 ofpbuf_init(&of11_out
, 0);
2621 ofpacts_put_openflow11_instructions(ofpacts
.data
, ofpacts
.size
,
2624 print_differences("", of11_in
.data
, of11_in
.size
,
2625 of11_out
.data
, of11_out
.size
);
2628 ofpbuf_uninit(&ofpacts
);
2629 ofpbuf_uninit(&of11_in
);
2630 ofpbuf_uninit(&of11_out
);
2635 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2636 * mask values to and from various formats and prints the results. */
2638 ofctl_check_vlan(int argc OVS_UNUSED
, char *argv
[])
2643 struct ofputil_flow_mod fm
;
2646 struct match nxm_match
;
2650 struct ofp10_match of10_raw
;
2651 struct match of10_match
;
2653 struct ofp11_match of11_raw
;
2654 struct match of11_match
;
2658 match_init_catchall(&match
);
2659 match
.flow
.vlan_tci
= htons(strtoul(argv
[1], NULL
, 16));
2660 match
.wc
.masks
.vlan_tci
= htons(strtoul(argv
[2], NULL
, 16));
2662 /* Convert to and from string. */
2663 string_s
= match_to_string(&match
, OFP_DEFAULT_PRIORITY
);
2664 printf("%s -> ", string_s
);
2666 parse_ofp_str(&fm
, -1, string_s
, false);
2667 printf("%04"PRIx16
"/%04"PRIx16
"\n",
2668 ntohs(fm
.match
.flow
.vlan_tci
),
2669 ntohs(fm
.match
.wc
.masks
.vlan_tci
));
2672 /* Convert to and from NXM. */
2673 ofpbuf_init(&nxm
, 0);
2674 nxm_match_len
= nx_put_match(&nxm
, &match
, htonll(0), htonll(0));
2675 nxm_s
= nx_match_to_string(nxm
.data
, nxm_match_len
);
2676 error
= nx_pull_match(&nxm
, nxm_match_len
, &nxm_match
, NULL
, NULL
);
2677 printf("NXM: %s -> ", nxm_s
);
2679 printf("%s\n", ofperr_to_string(error
));
2681 printf("%04"PRIx16
"/%04"PRIx16
"\n",
2682 ntohs(nxm_match
.flow
.vlan_tci
),
2683 ntohs(nxm_match
.wc
.masks
.vlan_tci
));
2686 ofpbuf_uninit(&nxm
);
2688 /* Convert to and from OXM. */
2689 ofpbuf_init(&nxm
, 0);
2690 nxm_match_len
= oxm_put_match(&nxm
, &match
);
2691 nxm_s
= oxm_match_to_string(nxm
.data
, nxm_match_len
);
2692 error
= oxm_pull_match(&nxm
, &nxm_match
);
2693 printf("OXM: %s -> ", nxm_s
);
2695 printf("%s\n", ofperr_to_string(error
));
2697 uint16_t vid
= ntohs(nxm_match
.flow
.vlan_tci
) &
2698 (VLAN_VID_MASK
| VLAN_CFI
);
2699 uint16_t mask
= ntohs(nxm_match
.wc
.masks
.vlan_tci
) &
2700 (VLAN_VID_MASK
| VLAN_CFI
);
2702 printf("%04"PRIx16
"/%04"PRIx16
",", vid
, mask
);
2703 if (vid
&& vlan_tci_to_pcp(nxm_match
.wc
.masks
.vlan_tci
)) {
2704 printf("%02"PRIx8
"\n", vlan_tci_to_pcp(nxm_match
.flow
.vlan_tci
));
2710 ofpbuf_uninit(&nxm
);
2712 /* Convert to and from OpenFlow 1.0. */
2713 ofputil_match_to_ofp10_match(&match
, &of10_raw
);
2714 ofputil_match_from_ofp10_match(&of10_raw
, &of10_match
);
2715 printf("OF1.0: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
2716 ntohs(of10_raw
.dl_vlan
),
2717 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN
)) != 0,
2718 of10_raw
.dl_vlan_pcp
,
2719 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN_PCP
)) != 0,
2720 ntohs(of10_match
.flow
.vlan_tci
),
2721 ntohs(of10_match
.wc
.masks
.vlan_tci
));
2723 /* Convert to and from OpenFlow 1.1. */
2724 ofputil_match_to_ofp11_match(&match
, &of11_raw
);
2725 ofputil_match_from_ofp11_match(&of11_raw
, &of11_match
);
2726 printf("OF1.1: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
2727 ntohs(of11_raw
.dl_vlan
),
2728 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN
)) != 0,
2729 of11_raw
.dl_vlan_pcp
,
2730 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN_PCP
)) != 0,
2731 ntohs(of11_match
.flow
.vlan_tci
),
2732 ntohs(of11_match
.wc
.masks
.vlan_tci
));
2735 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2738 ofctl_print_error(int argc OVS_UNUSED
, char *argv
[])
2743 error
= ofperr_from_name(argv
[1]);
2745 ovs_fatal(0, "unknown error \"%s\"", argv
[1]);
2748 for (version
= 0; version
<= UINT8_MAX
; version
++) {
2749 const char *name
= ofperr_domain_get_name(version
);
2753 printf("%s: %d,%d\n",
2754 ofperr_domain_get_name(version
),
2755 ofperr_get_type(error
, version
),
2756 ofperr_get_code(error
, version
));
2760 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
2761 * error named ENUM and prints the error reply in hex. */
2763 ofctl_encode_error_reply(int argc OVS_UNUSED
, char *argv
[])
2765 const struct ofp_header
*oh
;
2766 struct ofpbuf request
, *reply
;
2769 error
= ofperr_from_name(argv
[1]);
2771 ovs_fatal(0, "unknown error \"%s\"", argv
[1]);
2774 ofpbuf_init(&request
, 0);
2775 if (ofpbuf_put_hex(&request
, argv
[2], NULL
)[0] != '\0') {
2776 ovs_fatal(0, "Trailing garbage in hex data");
2778 if (request
.size
< sizeof(struct ofp_header
)) {
2779 ovs_fatal(0, "Request too short");
2783 if (request
.size
!= ntohs(oh
->length
)) {
2784 ovs_fatal(0, "Request size inconsistent");
2787 reply
= ofperr_encode_reply(error
, request
.data
);
2788 ofpbuf_uninit(&request
);
2790 ovs_hex_dump(stdout
, reply
->data
, reply
->size
, 0, false);
2791 ofpbuf_delete(reply
);
2794 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2795 * binary data, interpreting them as an OpenFlow message, and prints the
2796 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2798 ofctl_ofp_print(int argc
, char *argv
[])
2800 struct ofpbuf packet
;
2802 ofpbuf_init(&packet
, strlen(argv
[1]) / 2);
2803 if (ofpbuf_put_hex(&packet
, argv
[1], NULL
)[0] != '\0') {
2804 ovs_fatal(0, "trailing garbage following hex bytes");
2806 ofp_print(stdout
, packet
.data
, packet
.size
, argc
> 2 ? atoi(argv
[2]) : 2);
2807 ofpbuf_uninit(&packet
);
2810 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
2811 * and dumps each message in hex. */
2813 ofctl_encode_hello(int argc OVS_UNUSED
, char *argv
[])
2815 uint32_t bitmap
= strtol(argv
[1], NULL
, 0);
2816 struct ofpbuf
*hello
;
2818 hello
= ofputil_encode_hello(bitmap
);
2819 ovs_hex_dump(stdout
, hello
->data
, hello
->size
, 0, false);
2820 ofp_print(stdout
, hello
->data
, hello
->size
, verbosity
);
2821 ofpbuf_delete(hello
);
2824 static const struct command all_commands
[] = {
2825 { "show", 1, 1, ofctl_show
},
2826 { "monitor", 1, 3, ofctl_monitor
},
2827 { "snoop", 1, 1, ofctl_snoop
},
2828 { "dump-desc", 1, 1, ofctl_dump_desc
},
2829 { "dump-tables", 1, 1, ofctl_dump_tables
},
2830 { "dump-flows", 1, 2, ofctl_dump_flows
},
2831 { "dump-aggregate", 1, 2, ofctl_dump_aggregate
},
2832 { "queue-stats", 1, 3, ofctl_queue_stats
},
2833 { "add-flow", 2, 2, ofctl_add_flow
},
2834 { "add-flows", 2, 2, ofctl_add_flows
},
2835 { "mod-flows", 2, 2, ofctl_mod_flows
},
2836 { "del-flows", 1, 2, ofctl_del_flows
},
2837 { "replace-flows", 2, 2, ofctl_replace_flows
},
2838 { "diff-flows", 2, 2, ofctl_diff_flows
},
2839 { "packet-out", 4, INT_MAX
, ofctl_packet_out
},
2840 { "dump-ports", 1, 2, ofctl_dump_ports
},
2841 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc
},
2842 { "mod-port", 3, 3, ofctl_mod_port
},
2843 { "get-frags", 1, 1, ofctl_get_frags
},
2844 { "set-frags", 2, 2, ofctl_set_frags
},
2845 { "probe", 1, 1, ofctl_probe
},
2846 { "ping", 1, 2, ofctl_ping
},
2847 { "benchmark", 3, 3, ofctl_benchmark
},
2848 { "help", 0, INT_MAX
, ofctl_help
},
2850 /* Undocumented commands for testing. */
2851 { "parse-flow", 1, 1, ofctl_parse_flow
},
2852 { "parse-flows", 1, 1, ofctl_parse_flows
},
2853 { "parse-nx-match", 0, 0, ofctl_parse_nxm
},
2854 { "parse-nxm", 0, 0, ofctl_parse_nxm
},
2855 { "parse-oxm", 0, 0, ofctl_parse_oxm
},
2856 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions
},
2857 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match
},
2858 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match
},
2859 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions
},
2860 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions
},
2861 { "check-vlan", 2, 2, ofctl_check_vlan
},
2862 { "print-error", 1, 1, ofctl_print_error
},
2863 { "encode-error-reply", 2, 2, ofctl_encode_error_reply
},
2864 { "ofp-print", 1, 2, ofctl_ofp_print
},
2865 { "encode-hello", 1, 1, ofctl_encode_hello
},
2867 { NULL
, 0, 0, NULL
},