2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
48 #include "ofp-version-opt.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
54 #include "pcap-file.h"
55 #include "poll-loop.h"
57 #include "stream-ssl.h"
58 #include "socket-util.h"
64 #include "meta-flow.h"
67 VLOG_DEFINE_THIS_MODULE(ofctl
);
69 /* --strict: Use strict matching for flow mod commands? Additionally governs
70 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
74 /* --readd: If true, on replace-flows, re-add even flows that have not changed
75 * (to reset flow counters). */
78 /* -F, --flow-format: Allowed protocols. By default, any protocol is
80 static enum ofputil_protocol allowed_protocols
= OFPUTIL_P_ANY
;
82 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
83 * commands. Either one of NXPIF_* to force a particular packet_in format, or
84 * -1 to let ovs-ofctl choose the default. */
85 static int preferred_packet_in_format
= -1;
87 /* -m, --more: Additional verbosity for ofp-print functions. */
90 /* --timestamp: Print a timestamp before each received packet on "monitor" and
92 static bool timestamp
;
94 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
96 static char *unixctl_path
;
98 /* --sort, --rsort: Sort order. */
99 enum sort_order
{ SORT_ASC
, SORT_DESC
};
100 struct sort_criterion
{
101 const struct mf_field
*field
; /* NULL means to sort by priority. */
102 enum sort_order order
;
104 static struct sort_criterion
*criteria
;
105 static size_t n_criteria
, allocated_criteria
;
107 static const struct command
*get_all_commands(void);
109 NO_RETURN
static void usage(void);
110 static void parse_options(int argc
, char *argv
[]);
112 static bool recv_flow_stats_reply(struct vconn
*, ovs_be32 send_xid
,
113 struct ofpbuf
**replyp
,
114 struct ofputil_flow_stats
*,
115 struct ofpbuf
*ofpacts
);
117 main(int argc
, char *argv
[])
119 set_program_name(argv
[0]);
120 service_start(&argc
, &argv
);
121 parse_options(argc
, argv
);
122 fatal_ignore_sigpipe();
123 run_command(argc
- optind
, argv
+ optind
, get_all_commands());
128 add_sort_criterion(enum sort_order order
, const char *field
)
130 struct sort_criterion
*sc
;
132 if (n_criteria
>= allocated_criteria
) {
133 criteria
= x2nrealloc(criteria
, &allocated_criteria
, sizeof *criteria
);
136 sc
= &criteria
[n_criteria
++];
137 if (!field
|| !strcasecmp(field
, "priority")) {
140 sc
->field
= mf_from_name(field
);
142 ovs_fatal(0, "%s: unknown field name", field
);
149 parse_options(int argc
, char *argv
[])
152 OPT_STRICT
= UCHAR_MAX
+ 1,
159 OFP_VERSION_OPTION_ENUMS
,
162 static const struct option long_options
[] = {
163 {"timeout", required_argument
, NULL
, 't'},
164 {"strict", no_argument
, NULL
, OPT_STRICT
},
165 {"readd", no_argument
, NULL
, OPT_READD
},
166 {"flow-format", required_argument
, NULL
, 'F'},
167 {"packet-in-format", required_argument
, NULL
, 'P'},
168 {"more", no_argument
, NULL
, 'm'},
169 {"timestamp", no_argument
, NULL
, OPT_TIMESTAMP
},
170 {"sort", optional_argument
, NULL
, OPT_SORT
},
171 {"rsort", optional_argument
, NULL
, OPT_RSORT
},
172 {"unixctl", required_argument
, NULL
, OPT_UNIXCTL
},
173 {"help", no_argument
, NULL
, 'h'},
174 {"option", no_argument
, NULL
, 'o'},
176 OFP_VERSION_LONG_OPTIONS
,
178 STREAM_SSL_LONG_OPTIONS
,
181 char *short_options
= long_options_to_short_options(long_options
);
183 enum ofputil_protocol version_protocols
;
185 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
186 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
187 * requests, but those requests have subtly different semantics in
188 * different OpenFlow versions. For example:
190 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
191 * existing flow to modify adds a new flow.
193 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
194 * existing flow to modify adds a new flow, but only if the mod-flow
195 * did not match on the flow cookie.
197 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
200 set_allowed_ofp_versions("OpenFlow10");
203 unsigned long int timeout
;
206 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
213 timeout
= strtoul(optarg
, NULL
, 10);
215 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
223 allowed_protocols
= ofputil_protocols_from_string(optarg
);
224 if (!allowed_protocols
) {
225 ovs_fatal(0, "%s: invalid flow format(s)", optarg
);
230 preferred_packet_in_format
=
231 ofputil_packet_in_format_from_string(optarg
);
232 if (preferred_packet_in_format
< 0) {
233 ovs_fatal(0, "unknown packet-in format `%s'", optarg
);
245 print_options(long_options
);
261 add_sort_criterion(SORT_ASC
, optarg
);
265 add_sort_criterion(SORT_DESC
, optarg
);
269 unixctl_path
= optarg
;
272 DAEMON_OPTION_HANDLERS
273 OFP_VERSION_OPTION_HANDLERS
275 STREAM_SSL_OPTION_HANDLERS
286 /* Always do a final sort pass based on priority. */
287 add_sort_criterion(SORT_DESC
, "priority");
292 versions
= get_allowed_ofp_versions();
293 version_protocols
= ofputil_protocols_from_version_bitmap(versions
);
294 if (!(allowed_protocols
& version_protocols
)) {
295 char *protocols
= ofputil_protocols_to_string(allowed_protocols
);
296 struct ds version_s
= DS_EMPTY_INITIALIZER
;
298 ofputil_format_version_bitmap_names(&version_s
, versions
);
299 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
300 "any of the enabled flow formats (%s). (Use -O to enable "
301 "additional OpenFlow versions or -F to enable additional "
302 "flow formats.)", ds_cstr(&version_s
), protocols
);
304 allowed_protocols
&= version_protocols
;
305 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
312 printf("%s: OpenFlow switch management utility\n"
313 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
314 "\nFor OpenFlow switches:\n"
315 " show SWITCH show OpenFlow information\n"
316 " dump-desc SWITCH print switch description\n"
317 " dump-tables SWITCH print table stats\n"
318 " dump-table-features SWITCH print table features\n"
319 " mod-port SWITCH IFACE ACT modify port behavior\n"
320 " mod-table SWITCH MOD modify flow table behavior\n"
321 " get-frags SWITCH print fragment handling behavior\n"
322 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
323 " dump-ports SWITCH [PORT] print port statistics\n"
324 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
325 " dump-flows SWITCH print all flow entries\n"
326 " dump-flows SWITCH FLOW print matching FLOWs\n"
327 " dump-aggregate SWITCH print aggregate flow statistics\n"
328 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
329 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
330 " add-flow SWITCH FLOW add flow described by FLOW\n"
331 " add-flows SWITCH FILE add flows from FILE\n"
332 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
333 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
334 " replace-flows SWITCH FILE replace flows with those in FILE\n"
335 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
336 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
337 " execute ACTIONS on PACKET\n"
338 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
339 " print packets received from SWITCH\n"
340 " snoop SWITCH snoop on SWITCH and its controller\n"
341 " add-group SWITCH GROUP add group described by GROUP\n"
342 " add-groups SWITCH FILE add group from FILE\n"
343 " mod-group SWITCH GROUP modify specific group\n"
344 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
345 " dump-group-features SWITCH print group features\n"
346 " dump-groups SWITCH [GROUP] print group description\n"
347 " dump-group-stats SWITCH [GROUP] print group statistics\n"
348 " queue-get-config SWITCH PORT print queue information for port\n"
349 " add-meter SWITCH METER add meter described by METER\n"
350 " mod-meter SWITCH METER modify specific METER\n"
351 " del-meter SWITCH METER delete METER\n"
352 " del-meters SWITCH delete all meters\n"
353 " dump-meter SWITCH METER print METER configuration\n"
354 " dump-meters SWITCH print all meter configuration\n"
355 " meter-stats SWITCH [METER] print meter statistics\n"
356 " meter-features SWITCH print meter features\n"
357 "\nFor OpenFlow switches and controllers:\n"
358 " probe TARGET probe whether TARGET is up\n"
359 " ping TARGET [N] latency of N-byte echos\n"
360 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
361 "SWITCH or TARGET is an active OpenFlow connection method.\n"
362 "\nOther commands:\n"
363 " ofp-parse FILE print messages read from FILE\n"
364 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
365 program_name
, program_name
);
366 vconn_usage(true, false, false);
370 printf("\nOther options:\n"
371 " --strict use strict match for flow commands\n"
372 " --readd replace flows that haven't changed\n"
373 " -F, --flow-format=FORMAT force particular flow format\n"
374 " -P, --packet-in-format=FRMT force particular packet in format\n"
375 " -m, --more be more verbose printing OpenFlow\n"
376 " --timestamp (monitor, snoop) print timestamps\n"
377 " -t, --timeout=SECS give up after SECS seconds\n"
378 " --sort[=field] sort in ascending order\n"
379 " --rsort[=field] sort in descending order\n"
380 " --unixctl=SOCKET set control socket name\n"
381 " -h, --help display this help message\n"
382 " -V, --version display version information\n");
387 ofctl_exit(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
388 const char *argv
[] OVS_UNUSED
, void *exiting_
)
390 bool *exiting
= exiting_
;
392 unixctl_command_reply(conn
, NULL
);
395 static void run(int retval
, const char *message
, ...)
399 run(int retval
, const char *message
, ...)
404 va_start(args
, message
);
405 ovs_fatal_valist(retval
, message
, args
);
409 /* Generic commands. */
412 open_vconn_socket(const char *name
, struct vconn
**vconnp
)
414 char *vconn_name
= xasprintf("unix:%s", name
);
417 error
= vconn_open(vconn_name
, get_allowed_ofp_versions(), DSCP_DEFAULT
,
419 if (error
&& error
!= ENOENT
) {
420 ovs_fatal(0, "%s: failed to open socket (%s)", name
,
421 ovs_strerror(error
));
428 enum open_target
{ MGMT
, SNOOP
};
430 static enum ofputil_protocol
431 open_vconn__(const char *name
, enum open_target target
,
432 struct vconn
**vconnp
)
434 const char *suffix
= target
== MGMT
? "mgmt" : "snoop";
435 char *datapath_name
, *datapath_type
, *socket_name
;
436 enum ofputil_protocol protocol
;
441 bridge_path
= xasprintf("%s/%s.%s", ovs_rundir(), name
, suffix
);
443 ofproto_parse_name(name
, &datapath_name
, &datapath_type
);
444 socket_name
= xasprintf("%s/%s.%s", ovs_rundir(), datapath_name
, suffix
);
448 if (strchr(name
, ':')) {
449 run(vconn_open(name
, get_allowed_ofp_versions(), DSCP_DEFAULT
, vconnp
),
450 "connecting to %s", name
);
451 } else if (!open_vconn_socket(name
, vconnp
)) {
453 } else if (!open_vconn_socket(bridge_path
, vconnp
)) {
455 } else if (!open_vconn_socket(socket_name
, vconnp
)) {
458 ovs_fatal(0, "%s is not a bridge or a socket", name
);
461 if (target
== SNOOP
) {
462 vconn_set_recv_any_version(*vconnp
);
468 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp
));
469 error
= vconn_connect_block(*vconnp
);
471 ovs_fatal(0, "%s: failed to connect to socket (%s)", name
,
472 ovs_strerror(error
));
475 ofp_version
= vconn_get_version(*vconnp
);
476 protocol
= ofputil_protocol_from_ofp_version(ofp_version
);
478 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
484 static enum ofputil_protocol
485 open_vconn(const char *name
, struct vconn
**vconnp
)
487 return open_vconn__(name
, MGMT
, vconnp
);
491 send_openflow_buffer(struct vconn
*vconn
, struct ofpbuf
*buffer
)
493 ofpmsg_update_length(buffer
);
494 run(vconn_send_block(vconn
, buffer
), "failed to send packet to switch");
498 dump_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
500 struct ofpbuf
*reply
;
502 ofpmsg_update_length(request
);
503 run(vconn_transact(vconn
, request
, &reply
), "talking to %s",
504 vconn_get_name(vconn
));
505 ofp_print(stdout
, ofpbuf_data(reply
), ofpbuf_size(reply
), verbosity
+ 1);
506 ofpbuf_delete(reply
);
510 dump_trivial_transaction(const char *vconn_name
, enum ofpraw raw
)
512 struct ofpbuf
*request
;
515 open_vconn(vconn_name
, &vconn
);
516 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
517 dump_transaction(vconn
, request
);
522 dump_stats_transaction(struct vconn
*vconn
, struct ofpbuf
*request
)
524 const struct ofp_header
*request_oh
= ofpbuf_data(request
);
525 ovs_be32 send_xid
= request_oh
->xid
;
526 enum ofpraw request_raw
;
527 enum ofpraw reply_raw
;
530 ofpraw_decode_partial(&request_raw
, ofpbuf_data(request
), ofpbuf_size(request
));
531 reply_raw
= ofpraw_stats_request_to_reply(request_raw
,
532 request_oh
->version
);
534 send_openflow_buffer(vconn
, request
);
537 struct ofpbuf
*reply
;
539 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
540 recv_xid
= ((struct ofp_header
*) ofpbuf_data(reply
))->xid
;
541 if (send_xid
== recv_xid
) {
544 ofp_print(stdout
, ofpbuf_data(reply
), ofpbuf_size(reply
), verbosity
+ 1);
546 ofpraw_decode(&raw
, ofpbuf_data(reply
));
547 if (ofptype_from_ofpraw(raw
) == OFPTYPE_ERROR
) {
549 } else if (raw
== reply_raw
) {
550 done
= !ofpmp_more(ofpbuf_data(reply
));
552 ovs_fatal(0, "received bad reply: %s",
553 ofp_to_string(ofpbuf_data(reply
), ofpbuf_size(reply
),
557 VLOG_DBG("received reply with xid %08"PRIx32
" "
558 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
560 ofpbuf_delete(reply
);
565 dump_trivial_stats_transaction(const char *vconn_name
, enum ofpraw raw
)
567 struct ofpbuf
*request
;
570 open_vconn(vconn_name
, &vconn
);
571 request
= ofpraw_alloc(raw
, vconn_get_version(vconn
), 0);
572 dump_stats_transaction(vconn
, request
);
576 /* Sends all of the 'requests', which should be requests that only have replies
577 * if an error occurs, and waits for them to succeed or fail. If an error does
578 * occur, prints it and exits with an error.
580 * Destroys all of the 'requests'. */
582 transact_multiple_noreply(struct vconn
*vconn
, struct list
*requests
)
584 struct ofpbuf
*request
, *reply
;
586 LIST_FOR_EACH (request
, list_node
, requests
) {
587 ofpmsg_update_length(request
);
590 run(vconn_transact_multiple_noreply(vconn
, requests
, &reply
),
591 "talking to %s", vconn_get_name(vconn
));
593 ofp_print(stderr
, ofpbuf_data(reply
), ofpbuf_size(reply
), verbosity
+ 2);
596 ofpbuf_delete(reply
);
599 /* Sends 'request', which should be a request that only has a reply if an error
600 * occurs, and waits for it to succeed or fail. If an error does occur, prints
601 * it and exits with an error.
603 * Destroys 'request'. */
605 transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
)
607 struct list requests
;
609 list_init(&requests
);
610 list_push_back(&requests
, &request
->list_node
);
611 transact_multiple_noreply(vconn
, &requests
);
615 fetch_switch_config(struct vconn
*vconn
, struct ofp_switch_config
*config_
)
617 struct ofp_switch_config
*config
;
618 struct ofpbuf
*request
;
619 struct ofpbuf
*reply
;
622 request
= ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST
,
623 vconn_get_version(vconn
), 0);
624 run(vconn_transact(vconn
, request
, &reply
),
625 "talking to %s", vconn_get_name(vconn
));
627 if (ofptype_pull(&type
, reply
) || type
!= OFPTYPE_GET_CONFIG_REPLY
) {
628 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn
));
631 config
= ofpbuf_pull(reply
, sizeof *config
);
634 ofpbuf_delete(reply
);
638 set_switch_config(struct vconn
*vconn
, const struct ofp_switch_config
*config
)
640 struct ofpbuf
*request
;
642 request
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, vconn_get_version(vconn
), 0);
643 ofpbuf_put(request
, config
, sizeof *config
);
645 transact_noreply(vconn
, request
);
649 ofctl_show(int argc OVS_UNUSED
, char *argv
[])
651 const char *vconn_name
= argv
[1];
652 enum ofp_version version
;
654 struct ofpbuf
*request
;
655 struct ofpbuf
*reply
;
658 open_vconn(vconn_name
, &vconn
);
659 version
= vconn_get_version(vconn
);
660 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
, version
, 0);
661 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", vconn_name
);
663 has_ports
= ofputil_switch_features_has_ports(reply
);
664 ofp_print(stdout
, ofpbuf_data(reply
), ofpbuf_size(reply
), verbosity
+ 1);
665 ofpbuf_delete(reply
);
668 request
= ofputil_encode_port_desc_stats_request(version
, OFPP_ANY
);
669 dump_stats_transaction(vconn
, request
);
671 dump_trivial_transaction(vconn_name
, OFPRAW_OFPT_GET_CONFIG_REQUEST
);
676 ofctl_dump_desc(int argc OVS_UNUSED
, char *argv
[])
678 dump_trivial_stats_transaction(argv
[1], OFPRAW_OFPST_DESC_REQUEST
);
682 ofctl_dump_tables(int argc OVS_UNUSED
, char *argv
[])
684 dump_trivial_stats_transaction(argv
[1], OFPRAW_OFPST_TABLE_REQUEST
);
688 ofctl_dump_table_features(int argc OVS_UNUSED
, char *argv
[])
690 struct ofpbuf
*request
;
693 open_vconn(argv
[1], &vconn
);
694 request
= ofputil_encode_table_features_request(vconn_get_version(vconn
));
696 dump_stats_transaction(vconn
, request
);
702 static bool fetch_port_by_stats(struct vconn
*,
703 const char *port_name
, ofp_port_t port_no
,
704 struct ofputil_phy_port
*);
706 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
707 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
708 * successful, false on failure.
710 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
711 * list of ports in OFPT_FEATURES_REPLY. */
713 fetch_port_by_features(struct vconn
*vconn
,
714 const char *port_name
, ofp_port_t port_no
,
715 struct ofputil_phy_port
*pp
)
717 struct ofputil_switch_features features
;
718 const struct ofp_header
*oh
;
719 struct ofpbuf
*request
, *reply
;
725 /* Fetch the switch's ofp_switch_features. */
726 request
= ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST
,
727 vconn_get_version(vconn
), 0);
728 run(vconn_transact(vconn
, request
, &reply
),
729 "talking to %s", vconn_get_name(vconn
));
731 oh
= ofpbuf_data(reply
);
732 if (ofptype_decode(&type
, ofpbuf_data(reply
))
733 || type
!= OFPTYPE_FEATURES_REPLY
) {
734 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn
));
736 if (!ofputil_switch_features_has_ports(reply
)) {
737 /* The switch features reply does not contain a complete list of ports.
738 * Probably, there are more ports than will fit into a single 64 kB
739 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
741 ofpbuf_delete(reply
);
742 return fetch_port_by_stats(vconn
, port_name
, port_no
, pp
);
745 error
= ofputil_decode_switch_features(oh
, &features
, &b
);
747 ovs_fatal(0, "%s: failed to decode features reply (%s)",
748 vconn_get_name(vconn
), ofperr_to_string(error
));
751 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
752 if (port_no
!= OFPP_NONE
753 ? port_no
== pp
->port_no
754 : !strcmp(pp
->name
, port_name
)) {
759 ofpbuf_delete(reply
);
763 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
764 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
765 * successful, false on failure.
767 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
768 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
769 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
770 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
772 fetch_port_by_stats(struct vconn
*vconn
,
773 const char *port_name
, ofp_port_t port_no
,
774 struct ofputil_phy_port
*pp
)
776 struct ofpbuf
*request
;
781 request
= ofputil_encode_port_desc_stats_request(vconn_get_version(vconn
),
783 send_xid
= ((struct ofp_header
*) ofpbuf_data(request
))->xid
;
785 send_openflow_buffer(vconn
, request
);
788 struct ofpbuf
*reply
;
790 run(vconn_recv_block(vconn
, &reply
), "OpenFlow packet receive failed");
791 recv_xid
= ((struct ofp_header
*) ofpbuf_data(reply
))->xid
;
792 if (send_xid
== recv_xid
) {
793 struct ofp_header
*oh
= ofpbuf_data(reply
);
798 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
799 if (ofptype_pull(&type
, &b
)
800 || type
!= OFPTYPE_PORT_DESC_STATS_REPLY
) {
801 ovs_fatal(0, "received bad reply: %s",
802 ofp_to_string(ofpbuf_data(reply
), ofpbuf_size(reply
),
806 flags
= ofpmp_flags(oh
);
807 done
= !(flags
& OFPSF_REPLY_MORE
);
810 /* We've already found the port, but we need to drain
811 * the queue of any other replies for this request. */
815 while (!ofputil_pull_phy_port(oh
->version
, &b
, pp
)) {
816 if (port_no
!= OFPP_NONE
? port_no
== pp
->port_no
817 : !strcmp(pp
->name
, port_name
)) {
823 VLOG_DBG("received reply with xid %08"PRIx32
" "
824 "!= expected %08"PRIx32
, recv_xid
, send_xid
);
826 ofpbuf_delete(reply
);
833 str_to_ofp(const char *s
, ofp_port_t
*ofp_port
)
838 ret
= str_to_uint(s
, 10, &port_
);
839 *ofp_port
= u16_to_ofp(port_
);
843 /* Opens a connection to 'vconn_name', fetches the port structure for
844 * 'port_name' (which may be a port name or number), and copies it into
847 fetch_ofputil_phy_port(const char *vconn_name
, const char *port_name
,
848 struct ofputil_phy_port
*pp
)
854 /* Try to interpret the argument as a port number. */
855 if (!str_to_ofp(port_name
, &port_no
)) {
859 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
860 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
861 * into the OFPST_PORT_DESC reply. Try it the correct way. */
862 open_vconn(vconn_name
, &vconn
);
863 found
= (vconn_get_version(vconn
) < OFP13_VERSION
864 ? fetch_port_by_features(vconn
, port_name
, port_no
, pp
)
865 : fetch_port_by_stats(vconn
, port_name
, port_no
, pp
));
869 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name
, port_name
);
873 /* Returns the port number corresponding to 'port_name' (which may be a port
874 * name or number) within the switch 'vconn_name'. */
876 str_to_port_no(const char *vconn_name
, const char *port_name
)
880 if (ofputil_port_from_string(port_name
, &port_no
)) {
883 struct ofputil_phy_port pp
;
885 fetch_ofputil_phy_port(vconn_name
, port_name
, &pp
);
891 try_set_protocol(struct vconn
*vconn
, enum ofputil_protocol want
,
892 enum ofputil_protocol
*cur
)
895 struct ofpbuf
*request
, *reply
;
896 enum ofputil_protocol next
;
898 request
= ofputil_encode_set_protocol(*cur
, want
, &next
);
903 run(vconn_transact_noreply(vconn
, request
, &reply
),
904 "talking to %s", vconn_get_name(vconn
));
906 char *s
= ofp_to_string(ofpbuf_data(reply
), ofpbuf_size(reply
), 2);
907 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
908 vconn_get_name(vconn
), s
);
910 ofpbuf_delete(reply
);
918 static enum ofputil_protocol
919 set_protocol_for_flow_dump(struct vconn
*vconn
,
920 enum ofputil_protocol cur_protocol
,
921 enum ofputil_protocol usable_protocols
)
926 for (i
= 0; i
< ofputil_n_flow_dump_protocols
; i
++) {
927 enum ofputil_protocol f
= ofputil_flow_dump_protocols
[i
];
928 if (f
& usable_protocols
& allowed_protocols
929 && try_set_protocol(vconn
, f
, &cur_protocol
)) {
934 usable_s
= ofputil_protocols_to_string(usable_protocols
);
935 if (usable_protocols
& allowed_protocols
) {
936 ovs_fatal(0, "switch does not support any of the usable flow "
937 "formats (%s)", usable_s
);
939 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
940 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
941 "allowed flow formats (%s)", usable_s
, allowed_s
);
945 static struct vconn
*
946 prepare_dump_flows(int argc
, char *argv
[], bool aggregate
,
947 struct ofpbuf
**requestp
)
949 enum ofputil_protocol usable_protocols
, protocol
;
950 struct ofputil_flow_stats_request fsr
;
954 error
= parse_ofp_flow_stats_request_str(&fsr
, aggregate
,
955 argc
> 2 ? argv
[2] : "",
958 ovs_fatal(0, "%s", error
);
961 protocol
= open_vconn(argv
[1], &vconn
);
962 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
963 *requestp
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
968 ofctl_dump_flows__(int argc
, char *argv
[], bool aggregate
)
970 struct ofpbuf
*request
;
973 vconn
= prepare_dump_flows(argc
, argv
, aggregate
, &request
);
974 dump_stats_transaction(vconn
, request
);
979 compare_flows(const void *afs_
, const void *bfs_
)
981 const struct ofputil_flow_stats
*afs
= afs_
;
982 const struct ofputil_flow_stats
*bfs
= bfs_
;
983 const struct match
*a
= &afs
->match
;
984 const struct match
*b
= &bfs
->match
;
985 const struct sort_criterion
*sc
;
987 for (sc
= criteria
; sc
< &criteria
[n_criteria
]; sc
++) {
988 const struct mf_field
*f
= sc
->field
;
992 int a_pri
= afs
->priority
;
993 int b_pri
= bfs
->priority
;
994 ret
= a_pri
< b_pri
? -1 : a_pri
> b_pri
;
998 ina
= mf_are_prereqs_ok(f
, &a
->flow
) && !mf_is_all_wild(f
, &a
->wc
);
999 inb
= mf_are_prereqs_ok(f
, &b
->flow
) && !mf_is_all_wild(f
, &b
->wc
);
1001 /* Skip the test for sc->order, so that missing fields always
1002 * sort to the end whether we're sorting in ascending or
1003 * descending order. */
1004 return ina
? -1 : 1;
1006 union mf_value aval
, bval
;
1008 mf_get_value(f
, &a
->flow
, &aval
);
1009 mf_get_value(f
, &b
->flow
, &bval
);
1010 ret
= memcmp(&aval
, &bval
, f
->n_bytes
);
1015 return sc
->order
== SORT_ASC
? ret
: -ret
;
1023 ofctl_dump_flows(int argc
, char *argv
[])
1026 ofctl_dump_flows__(argc
, argv
, false);
1029 struct ofputil_flow_stats
*fses
;
1030 size_t n_fses
, allocated_fses
;
1031 struct ofpbuf
*request
;
1032 struct ofpbuf ofpacts
;
1033 struct ofpbuf
*reply
;
1034 struct vconn
*vconn
;
1039 vconn
= prepare_dump_flows(argc
, argv
, false, &request
);
1040 send_xid
= ((struct ofp_header
*) ofpbuf_data(request
))->xid
;
1041 send_openflow_buffer(vconn
, request
);
1044 n_fses
= allocated_fses
= 0;
1046 ofpbuf_init(&ofpacts
, 0);
1048 struct ofputil_flow_stats
*fs
;
1050 if (n_fses
>= allocated_fses
) {
1051 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
1055 if (!recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
,
1059 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
1062 ofpbuf_uninit(&ofpacts
);
1064 qsort(fses
, n_fses
, sizeof *fses
, compare_flows
);
1067 for (i
= 0; i
< n_fses
; i
++) {
1069 ofp_print_flow_stats(&s
, &fses
[i
]);
1074 for (i
= 0; i
< n_fses
; i
++) {
1075 free(CONST_CAST(struct ofpact
*, fses
[i
].ofpacts
));
1084 ofctl_dump_aggregate(int argc
, char *argv
[])
1086 ofctl_dump_flows__(argc
, argv
, true);
1090 ofctl_queue_stats(int argc
, char *argv
[])
1092 struct ofpbuf
*request
;
1093 struct vconn
*vconn
;
1094 struct ofputil_queue_stats_request oqs
;
1096 open_vconn(argv
[1], &vconn
);
1098 if (argc
> 2 && argv
[2][0] && strcasecmp(argv
[2], "all")) {
1099 oqs
.port_no
= str_to_port_no(argv
[1], argv
[2]);
1101 oqs
.port_no
= OFPP_ANY
;
1103 if (argc
> 3 && argv
[3][0] && strcasecmp(argv
[3], "all")) {
1104 oqs
.queue_id
= atoi(argv
[3]);
1106 oqs
.queue_id
= OFPQ_ALL
;
1109 request
= ofputil_encode_queue_stats_request(vconn_get_version(vconn
), &oqs
);
1110 dump_stats_transaction(vconn
, request
);
1115 ofctl_queue_get_config(int argc OVS_UNUSED
, char *argv
[])
1117 const char *vconn_name
= argv
[1];
1118 const char *port_name
= argv
[2];
1119 enum ofputil_protocol protocol
;
1120 enum ofp_version version
;
1121 struct ofpbuf
*request
;
1122 struct vconn
*vconn
;
1125 port
= str_to_port_no(vconn_name
, port_name
);
1127 protocol
= open_vconn(vconn_name
, &vconn
);
1128 version
= ofputil_protocol_to_ofp_version(protocol
);
1129 request
= ofputil_encode_queue_get_config_request(version
, port
);
1130 dump_transaction(vconn
, request
);
1134 static enum ofputil_protocol
1135 open_vconn_for_flow_mod(const char *remote
, struct vconn
**vconnp
,
1136 enum ofputil_protocol usable_protocols
)
1138 enum ofputil_protocol cur_protocol
;
1142 if (!(usable_protocols
& allowed_protocols
)) {
1143 char *allowed_s
= ofputil_protocols_to_string(allowed_protocols
);
1144 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1145 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1146 "allowed flow formats (%s)", usable_s
, allowed_s
);
1149 /* If the initial flow format is allowed and usable, keep it. */
1150 cur_protocol
= open_vconn(remote
, vconnp
);
1151 if (usable_protocols
& allowed_protocols
& cur_protocol
) {
1152 return cur_protocol
;
1155 /* Otherwise try each flow format in turn. */
1156 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1157 enum ofputil_protocol f
= 1 << i
;
1159 if (f
!= cur_protocol
1160 && f
& usable_protocols
& allowed_protocols
1161 && try_set_protocol(*vconnp
, f
, &cur_protocol
)) {
1166 usable_s
= ofputil_protocols_to_string(usable_protocols
);
1167 ovs_fatal(0, "switch does not support any of the usable flow "
1168 "formats (%s)", usable_s
);
1172 ofctl_flow_mod__(const char *remote
, struct ofputil_flow_mod
*fms
,
1173 size_t n_fms
, enum ofputil_protocol usable_protocols
)
1175 enum ofputil_protocol protocol
;
1176 struct vconn
*vconn
;
1179 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
1181 for (i
= 0; i
< n_fms
; i
++) {
1182 struct ofputil_flow_mod
*fm
= &fms
[i
];
1184 transact_noreply(vconn
, ofputil_encode_flow_mod(fm
, protocol
));
1185 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
1191 ofctl_flow_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
1193 enum ofputil_protocol usable_protocols
;
1194 struct ofputil_flow_mod
*fms
= NULL
;
1198 error
= parse_ofp_flow_mod_file(argv
[2], command
, &fms
, &n_fms
,
1201 ovs_fatal(0, "%s", error
);
1203 ofctl_flow_mod__(argv
[1], fms
, n_fms
, usable_protocols
);
1208 ofctl_flow_mod(int argc
, char *argv
[], uint16_t command
)
1210 if (argc
> 2 && !strcmp(argv
[2], "-")) {
1211 ofctl_flow_mod_file(argc
, argv
, command
);
1213 struct ofputil_flow_mod fm
;
1215 enum ofputil_protocol usable_protocols
;
1217 error
= parse_ofp_flow_mod_str(&fm
, argc
> 2 ? argv
[2] : "", command
,
1220 ovs_fatal(0, "%s", error
);
1222 ofctl_flow_mod__(argv
[1], &fm
, 1, usable_protocols
);
1227 ofctl_add_flow(int argc
, char *argv
[])
1229 ofctl_flow_mod(argc
, argv
, OFPFC_ADD
);
1233 ofctl_add_flows(int argc
, char *argv
[])
1235 ofctl_flow_mod_file(argc
, argv
, OFPFC_ADD
);
1239 ofctl_mod_flows(int argc
, char *argv
[])
1241 ofctl_flow_mod(argc
, argv
, strict
? OFPFC_MODIFY_STRICT
: OFPFC_MODIFY
);
1245 ofctl_del_flows(int argc
, char *argv
[])
1247 ofctl_flow_mod(argc
, argv
, strict
? OFPFC_DELETE_STRICT
: OFPFC_DELETE
);
1251 set_packet_in_format(struct vconn
*vconn
,
1252 enum nx_packet_in_format packet_in_format
)
1254 struct ofpbuf
*spif
;
1256 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1258 transact_noreply(vconn
, spif
);
1259 VLOG_DBG("%s: using user-specified packet in format %s",
1260 vconn_get_name(vconn
),
1261 ofputil_packet_in_format_to_string(packet_in_format
));
1265 monitor_set_invalid_ttl_to_controller(struct vconn
*vconn
)
1267 struct ofp_switch_config config
;
1268 enum ofp_config_flags flags
;
1270 fetch_switch_config(vconn
, &config
);
1271 flags
= ntohs(config
.flags
);
1272 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1273 /* Set the invalid ttl config. */
1274 flags
|= OFPC_INVALID_TTL_TO_CONTROLLER
;
1276 config
.flags
= htons(flags
);
1277 set_switch_config(vconn
, &config
);
1279 /* Then retrieve the configuration to see if it really took. OpenFlow
1280 * doesn't define error reporting for bad modes, so this is all we can
1282 fetch_switch_config(vconn
, &config
);
1283 flags
= ntohs(config
.flags
);
1284 if (!(flags
& OFPC_INVALID_TTL_TO_CONTROLLER
)) {
1285 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1286 "switch probably doesn't support mode)");
1293 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1294 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1295 * an error message and stores NULL in '*msgp'. */
1297 openflow_from_hex(const char *hex
, struct ofpbuf
**msgp
)
1299 struct ofp_header
*oh
;
1302 msg
= ofpbuf_new(strlen(hex
) / 2);
1305 if (ofpbuf_put_hex(msg
, hex
, NULL
)[0] != '\0') {
1307 return "Trailing garbage in hex data";
1310 if (ofpbuf_size(msg
) < sizeof(struct ofp_header
)) {
1312 return "Message too short for OpenFlow";
1315 oh
= ofpbuf_data(msg
);
1316 if (ofpbuf_size(msg
) != ntohs(oh
->length
)) {
1318 return "Message size does not match length in OpenFlow header";
1326 ofctl_send(struct unixctl_conn
*conn
, int argc
,
1327 const char *argv
[], void *vconn_
)
1329 struct vconn
*vconn
= vconn_
;
1336 for (i
= 1; i
< argc
; i
++) {
1337 const char *error_msg
;
1341 error_msg
= openflow_from_hex(argv
[i
], &msg
);
1343 ds_put_format(&reply
, "%s\n", error_msg
);
1348 fprintf(stderr
, "send: ");
1349 ofp_print(stderr
, ofpbuf_data(msg
), ofpbuf_size(msg
), verbosity
);
1351 error
= vconn_send_block(vconn
, msg
);
1354 ds_put_format(&reply
, "%s\n", ovs_strerror(error
));
1357 ds_put_cstr(&reply
, "sent\n");
1362 unixctl_command_reply(conn
, ds_cstr(&reply
));
1364 unixctl_command_reply_error(conn
, ds_cstr(&reply
));
1369 struct barrier_aux
{
1370 struct vconn
*vconn
; /* OpenFlow connection for sending barrier. */
1371 struct unixctl_conn
*conn
; /* Connection waiting for barrier response. */
1375 ofctl_barrier(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1376 const char *argv
[] OVS_UNUSED
, void *aux_
)
1378 struct barrier_aux
*aux
= aux_
;
1383 unixctl_command_reply_error(conn
, "already waiting for barrier reply");
1387 msg
= ofputil_encode_barrier_request(vconn_get_version(aux
->vconn
));
1388 error
= vconn_send_block(aux
->vconn
, msg
);
1391 unixctl_command_reply_error(conn
, ovs_strerror(error
));
1398 ofctl_set_output_file(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1399 const char *argv
[], void *aux OVS_UNUSED
)
1403 fd
= open(argv
[1], O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
1405 unixctl_command_reply_error(conn
, ovs_strerror(errno
));
1410 dup2(fd
, STDERR_FILENO
);
1412 unixctl_command_reply(conn
, NULL
);
1416 ofctl_block(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1417 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1419 bool *blocked
= blocked_
;
1423 unixctl_command_reply(conn
, NULL
);
1425 unixctl_command_reply(conn
, "already blocking");
1430 ofctl_unblock(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
1431 const char *argv
[] OVS_UNUSED
, void *blocked_
)
1433 bool *blocked
= blocked_
;
1437 unixctl_command_reply(conn
, NULL
);
1439 unixctl_command_reply(conn
, "already unblocked");
1443 /* Prints to stdout all of the messages received on 'vconn'.
1445 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1446 * received on 'vconn'. */
1448 monitor_vconn(struct vconn
*vconn
, bool reply_to_echo_requests
)
1450 struct barrier_aux barrier_aux
= { vconn
, NULL
};
1451 struct unixctl_server
*server
;
1452 bool exiting
= false;
1453 bool blocked
= false;
1456 daemon_save_fd(STDERR_FILENO
);
1458 error
= unixctl_server_create(unixctl_path
, &server
);
1460 ovs_fatal(error
, "failed to create unixctl server");
1462 unixctl_command_register("exit", "", 0, 0, ofctl_exit
, &exiting
);
1463 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX
,
1465 unixctl_command_register("ofctl/barrier", "", 0, 0,
1466 ofctl_barrier
, &barrier_aux
);
1467 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1468 ofctl_set_output_file
, NULL
);
1470 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block
, &blocked
);
1471 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock
,
1474 daemonize_complete();
1480 unixctl_server_run(server
);
1485 retval
= vconn_recv(vconn
, &b
);
1486 if (retval
== EAGAIN
) {
1489 run(retval
, "vconn_recv");
1492 char *s
= xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1493 time_wall_msec(), true);
1498 ofptype_decode(&type
, ofpbuf_data(b
));
1499 ofp_print(stderr
, ofpbuf_data(b
), ofpbuf_size(b
), verbosity
+ 2);
1502 switch ((int) type
) {
1503 case OFPTYPE_BARRIER_REPLY
:
1504 if (barrier_aux
.conn
) {
1505 unixctl_command_reply(barrier_aux
.conn
, NULL
);
1506 barrier_aux
.conn
= NULL
;
1510 case OFPTYPE_ECHO_REQUEST
:
1511 if (reply_to_echo_requests
) {
1512 struct ofpbuf
*reply
;
1514 reply
= make_echo_reply(ofpbuf_data(b
));
1515 retval
= vconn_send_block(vconn
, reply
);
1517 ovs_fatal(retval
, "failed to send echo reply");
1530 vconn_run_wait(vconn
);
1532 vconn_recv_wait(vconn
);
1534 unixctl_server_wait(server
);
1538 unixctl_server_destroy(server
);
1542 ofctl_monitor(int argc
, char *argv
[])
1544 struct vconn
*vconn
;
1546 enum ofputil_protocol usable_protocols
;
1548 open_vconn(argv
[1], &vconn
);
1549 for (i
= 2; i
< argc
; i
++) {
1550 const char *arg
= argv
[i
];
1552 if (isdigit((unsigned char) *arg
)) {
1553 struct ofp_switch_config config
;
1555 fetch_switch_config(vconn
, &config
);
1556 config
.miss_send_len
= htons(atoi(arg
));
1557 set_switch_config(vconn
, &config
);
1558 } else if (!strcmp(arg
, "invalid_ttl")) {
1559 monitor_set_invalid_ttl_to_controller(vconn
);
1560 } else if (!strncmp(arg
, "watch:", 6)) {
1561 struct ofputil_flow_monitor_request fmr
;
1565 error
= parse_flow_monitor_request(&fmr
, arg
+ 6,
1568 ovs_fatal(0, "%s", error
);
1571 msg
= ofpbuf_new(0);
1572 ofputil_append_flow_monitor_request(&fmr
, msg
);
1573 dump_stats_transaction(vconn
, msg
);
1576 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg
);
1580 if (preferred_packet_in_format
>= 0) {
1581 set_packet_in_format(vconn
, preferred_packet_in_format
);
1583 enum ofp_version version
= vconn_get_version(vconn
);
1586 case OFP10_VERSION
: {
1587 struct ofpbuf
*spif
, *reply
;
1589 spif
= ofputil_make_set_packet_in_format(vconn_get_version(vconn
),
1591 run(vconn_transact_noreply(vconn
, spif
, &reply
),
1592 "talking to %s", vconn_get_name(vconn
));
1594 char *s
= ofp_to_string(ofpbuf_data(reply
), ofpbuf_size(reply
), 2);
1595 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1596 " replied: %s. Falling back to the switch default.",
1597 vconn_get_name(vconn
), s
);
1599 ofpbuf_delete(reply
);
1614 monitor_vconn(vconn
, true);
1618 ofctl_snoop(int argc OVS_UNUSED
, char *argv
[])
1620 struct vconn
*vconn
;
1622 open_vconn__(argv
[1], SNOOP
, &vconn
);
1623 monitor_vconn(vconn
, false);
1627 ofctl_dump_ports(int argc
, char *argv
[])
1629 struct ofpbuf
*request
;
1630 struct vconn
*vconn
;
1633 open_vconn(argv
[1], &vconn
);
1634 port
= argc
> 2 ? str_to_port_no(argv
[1], argv
[2]) : OFPP_ANY
;
1635 request
= ofputil_encode_dump_ports_request(vconn_get_version(vconn
), port
);
1636 dump_stats_transaction(vconn
, request
);
1641 ofctl_dump_ports_desc(int argc OVS_UNUSED
, char *argv
[])
1643 struct ofpbuf
*request
;
1644 struct vconn
*vconn
;
1647 open_vconn(argv
[1], &vconn
);
1648 port
= argc
> 2 ? str_to_port_no(argv
[1], argv
[2]) : OFPP_ANY
;
1649 request
= ofputil_encode_port_desc_stats_request(vconn_get_version(vconn
),
1651 dump_stats_transaction(vconn
, request
);
1656 ofctl_probe(int argc OVS_UNUSED
, char *argv
[])
1658 struct ofpbuf
*request
;
1659 struct vconn
*vconn
;
1660 struct ofpbuf
*reply
;
1662 open_vconn(argv
[1], &vconn
);
1663 request
= make_echo_request(vconn_get_version(vconn
));
1664 run(vconn_transact(vconn
, request
, &reply
), "talking to %s", argv
[1]);
1665 if (ofpbuf_size(reply
) != sizeof(struct ofp_header
)) {
1666 ovs_fatal(0, "reply does not match request");
1668 ofpbuf_delete(reply
);
1673 ofctl_packet_out(int argc
, char *argv
[])
1675 enum ofputil_protocol protocol
;
1676 struct ofputil_packet_out po
;
1677 struct ofpbuf ofpacts
;
1678 struct vconn
*vconn
;
1681 enum ofputil_protocol usable_protocols
; /* XXX: Use in proto selection */
1683 ofpbuf_init(&ofpacts
, 64);
1684 error
= ofpacts_parse_actions(argv
[3], &ofpacts
, &usable_protocols
);
1686 ovs_fatal(0, "%s", error
);
1689 po
.buffer_id
= UINT32_MAX
;
1690 po
.in_port
= str_to_port_no(argv
[1], argv
[2]);
1691 po
.ofpacts
= ofpbuf_data(&ofpacts
);
1692 po
.ofpacts_len
= ofpbuf_size(&ofpacts
);
1694 protocol
= open_vconn(argv
[1], &vconn
);
1695 for (i
= 4; i
< argc
; i
++) {
1696 struct ofpbuf
*packet
, *opo
;
1697 const char *error_msg
;
1699 error_msg
= eth_from_hex(argv
[i
], &packet
);
1701 ovs_fatal(0, "%s", error_msg
);
1704 po
.packet
= ofpbuf_data(packet
);
1705 po
.packet_len
= ofpbuf_size(packet
);
1706 opo
= ofputil_encode_packet_out(&po
, protocol
);
1707 transact_noreply(vconn
, opo
);
1708 ofpbuf_delete(packet
);
1711 ofpbuf_uninit(&ofpacts
);
1715 ofctl_mod_port(int argc OVS_UNUSED
, char *argv
[])
1717 struct ofp_config_flag
{
1718 const char *name
; /* The flag's name. */
1719 enum ofputil_port_config bit
; /* Bit to turn on or off. */
1720 bool on
; /* Value to set the bit to. */
1722 static const struct ofp_config_flag flags
[] = {
1723 { "up", OFPUTIL_PC_PORT_DOWN
, false },
1724 { "down", OFPUTIL_PC_PORT_DOWN
, true },
1725 { "stp", OFPUTIL_PC_NO_STP
, false },
1726 { "receive", OFPUTIL_PC_NO_RECV
, false },
1727 { "receive-stp", OFPUTIL_PC_NO_RECV_STP
, false },
1728 { "flood", OFPUTIL_PC_NO_FLOOD
, false },
1729 { "forward", OFPUTIL_PC_NO_FWD
, false },
1730 { "packet-in", OFPUTIL_PC_NO_PACKET_IN
, false },
1733 const struct ofp_config_flag
*flag
;
1734 enum ofputil_protocol protocol
;
1735 struct ofputil_port_mod pm
;
1736 struct ofputil_phy_port pp
;
1737 struct vconn
*vconn
;
1738 const char *command
;
1741 fetch_ofputil_phy_port(argv
[1], argv
[2], &pp
);
1743 pm
.port_no
= pp
.port_no
;
1744 memcpy(pm
.hw_addr
, pp
.hw_addr
, ETH_ADDR_LEN
);
1749 if (!strncasecmp(argv
[3], "no-", 3)) {
1750 command
= argv
[3] + 3;
1752 } else if (!strncasecmp(argv
[3], "no", 2)) {
1753 command
= argv
[3] + 2;
1759 for (flag
= flags
; flag
< &flags
[ARRAY_SIZE(flags
)]; flag
++) {
1760 if (!strcasecmp(command
, flag
->name
)) {
1761 pm
.mask
= flag
->bit
;
1762 pm
.config
= flag
->on
^ not ? flag
->bit
: 0;
1766 ovs_fatal(0, "unknown mod-port command '%s'", argv
[3]);
1769 protocol
= open_vconn(argv
[1], &vconn
);
1770 transact_noreply(vconn
, ofputil_encode_port_mod(&pm
, protocol
));
1775 ofctl_mod_table(int argc OVS_UNUSED
, char *argv
[])
1777 enum ofputil_protocol protocol
, usable_protocols
;
1778 struct ofputil_table_mod tm
;
1779 struct vconn
*vconn
;
1783 error
= parse_ofp_table_mod(&tm
, argv
[2], argv
[3], &usable_protocols
);
1785 ovs_fatal(0, "%s", error
);
1788 protocol
= open_vconn(argv
[1], &vconn
);
1789 if (!(protocol
& usable_protocols
)) {
1790 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
1791 enum ofputil_protocol f
= 1 << i
;
1793 && f
& usable_protocols
1794 && try_set_protocol(vconn
, f
, &protocol
)) {
1801 if (!(protocol
& usable_protocols
)) {
1802 char *usable_s
= ofputil_protocols_to_string(usable_protocols
);
1803 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s
);
1806 transact_noreply(vconn
, ofputil_encode_table_mod(&tm
, protocol
));
1811 ofctl_get_frags(int argc OVS_UNUSED
, char *argv
[])
1813 struct ofp_switch_config config
;
1814 struct vconn
*vconn
;
1816 open_vconn(argv
[1], &vconn
);
1817 fetch_switch_config(vconn
, &config
);
1818 puts(ofputil_frag_handling_to_string(ntohs(config
.flags
)));
1823 ofctl_set_frags(int argc OVS_UNUSED
, char *argv
[])
1825 struct ofp_switch_config config
;
1826 enum ofp_config_flags mode
;
1827 struct vconn
*vconn
;
1830 if (!ofputil_frag_handling_from_string(argv
[2], &mode
)) {
1831 ovs_fatal(0, "%s: unknown fragment handling mode", argv
[2]);
1834 open_vconn(argv
[1], &vconn
);
1835 fetch_switch_config(vconn
, &config
);
1836 flags
= htons(mode
) | (config
.flags
& htons(~OFPC_FRAG_MASK
));
1837 if (flags
!= config
.flags
) {
1838 /* Set the configuration. */
1839 config
.flags
= flags
;
1840 set_switch_config(vconn
, &config
);
1842 /* Then retrieve the configuration to see if it really took. OpenFlow
1843 * doesn't define error reporting for bad modes, so this is all we can
1845 fetch_switch_config(vconn
, &config
);
1846 if (flags
!= config
.flags
) {
1847 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1848 "switch probably doesn't support mode \"%s\")",
1849 argv
[1], ofputil_frag_handling_to_string(mode
));
1856 ofctl_ofp_parse(int argc OVS_UNUSED
, char *argv
[])
1858 const char *filename
= argv
[1];
1862 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
1864 ovs_fatal(errno
, "%s: open", filename
);
1867 ofpbuf_init(&b
, 65536);
1869 struct ofp_header
*oh
;
1870 size_t length
, tail_len
;
1875 oh
= ofpbuf_put_uninit(&b
, sizeof *oh
);
1876 n
= fread(oh
, 1, sizeof *oh
, file
);
1879 } else if (n
< sizeof *oh
) {
1880 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
1883 length
= ntohs(oh
->length
);
1884 if (length
< sizeof *oh
) {
1885 ovs_fatal(0, "%s: %"PRIuSIZE
"-byte message is too short for OpenFlow",
1889 tail_len
= length
- sizeof *oh
;
1890 tail
= ofpbuf_put_uninit(&b
, tail_len
);
1891 n
= fread(tail
, 1, tail_len
, file
);
1893 ovs_fatal(0, "%s: unexpected end of file mid-message", filename
);
1896 ofp_print(stdout
, ofpbuf_data(&b
), ofpbuf_size(&b
), verbosity
+ 2);
1900 if (file
!= stdin
) {
1906 is_openflow_port(ovs_be16 port_
, char *ports
[])
1908 uint16_t port
= ntohs(port_
);
1912 for (i
= 0; ports
[i
]; i
++) {
1913 if (port
== atoi(ports
[i
])) {
1919 return port
== OFP_PORT
|| port
== OFP_OLD_PORT
;
1924 ofctl_ofp_parse_pcap(int argc OVS_UNUSED
, char *argv
[])
1926 struct tcp_reader
*reader
;
1931 file
= ovs_pcap_open(argv
[1], "rb");
1933 ovs_fatal(errno
, "%s: open failed", argv
[1]);
1936 reader
= tcp_reader_open();
1939 struct ofpbuf
*packet
;
1942 const struct pkt_metadata md
= PKT_METADATA_INITIALIZER(ODPP_NONE
);
1944 error
= ovs_pcap_read(file
, &packet
, &when
);
1948 flow_extract(packet
, &md
, &flow
);
1949 if (flow
.dl_type
== htons(ETH_TYPE_IP
)
1950 && flow
.nw_proto
== IPPROTO_TCP
1951 && (is_openflow_port(flow
.tp_src
, argv
+ 2) ||
1952 is_openflow_port(flow
.tp_dst
, argv
+ 2))) {
1953 struct ofpbuf
*payload
= tcp_reader_run(reader
, &flow
, packet
);
1955 while (ofpbuf_size(payload
) >= sizeof(struct ofp_header
)) {
1956 const struct ofp_header
*oh
;
1957 void *data
= ofpbuf_data(payload
);
1960 /* Align OpenFlow on 8-byte boundary for safe access. */
1961 ofpbuf_shift(payload
, -((intptr_t) data
& 7));
1963 oh
= ofpbuf_data(payload
);
1964 length
= ntohs(oh
->length
);
1965 if (ofpbuf_size(payload
) < length
) {
1975 char *s
= xastrftime_msec("%H:%M:%S.### ", when
, true);
1980 printf(IP_FMT
".%"PRIu16
" > "IP_FMT
".%"PRIu16
":\n",
1981 IP_ARGS(flow
.nw_src
), ntohs(flow
.tp_src
),
1982 IP_ARGS(flow
.nw_dst
), ntohs(flow
.tp_dst
));
1983 ofp_print(stdout
, ofpbuf_data(payload
), length
, verbosity
+ 1);
1984 ofpbuf_pull(payload
, length
);
1988 ofpbuf_delete(packet
);
1990 tcp_reader_close(reader
);
1994 ofctl_ping(int argc
, char *argv
[])
1996 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
1997 unsigned int payload
;
1998 struct vconn
*vconn
;
2001 payload
= argc
> 2 ? atoi(argv
[2]) : 64;
2002 if (payload
> max_payload
) {
2003 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2006 open_vconn(argv
[1], &vconn
);
2007 for (i
= 0; i
< 10; i
++) {
2008 struct timeval start
, end
;
2009 struct ofpbuf
*request
, *reply
;
2010 const struct ofp_header
*rpy_hdr
;
2013 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2014 vconn_get_version(vconn
), payload
);
2015 random_bytes(ofpbuf_put_uninit(request
, payload
), payload
);
2017 xgettimeofday(&start
);
2018 run(vconn_transact(vconn
, ofpbuf_clone(request
), &reply
), "transact");
2019 xgettimeofday(&end
);
2021 rpy_hdr
= ofpbuf_data(reply
);
2022 if (ofptype_pull(&type
, reply
)
2023 || type
!= OFPTYPE_ECHO_REPLY
2024 || ofpbuf_size(reply
) != payload
2025 || memcmp(ofpbuf_l3(request
), ofpbuf_l3(reply
), payload
)) {
2026 printf("Reply does not match request. Request:\n");
2027 ofp_print(stdout
, request
, ofpbuf_size(request
), verbosity
+ 2);
2029 ofp_print(stdout
, reply
, ofpbuf_size(reply
), verbosity
+ 2);
2031 printf("%"PRIu32
" bytes from %s: xid=%08"PRIx32
" time=%.1f ms\n",
2032 ofpbuf_size(reply
), argv
[1], ntohl(rpy_hdr
->xid
),
2033 (1000*(double)(end
.tv_sec
- start
.tv_sec
))
2034 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2035 ofpbuf_delete(request
);
2036 ofpbuf_delete(reply
);
2042 ofctl_benchmark(int argc OVS_UNUSED
, char *argv
[])
2044 size_t max_payload
= 65535 - sizeof(struct ofp_header
);
2045 struct timeval start
, end
;
2046 unsigned int payload_size
, message_size
;
2047 struct vconn
*vconn
;
2052 payload_size
= atoi(argv
[2]);
2053 if (payload_size
> max_payload
) {
2054 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE
" bytes", max_payload
);
2056 message_size
= sizeof(struct ofp_header
) + payload_size
;
2058 count
= atoi(argv
[3]);
2060 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2061 count
, message_size
, count
* message_size
);
2063 open_vconn(argv
[1], &vconn
);
2064 xgettimeofday(&start
);
2065 for (i
= 0; i
< count
; i
++) {
2066 struct ofpbuf
*request
, *reply
;
2068 request
= ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST
,
2069 vconn_get_version(vconn
), payload_size
);
2070 ofpbuf_put_zeros(request
, payload_size
);
2071 run(vconn_transact(vconn
, request
, &reply
), "transact");
2072 ofpbuf_delete(reply
);
2074 xgettimeofday(&end
);
2077 duration
= ((1000*(double)(end
.tv_sec
- start
.tv_sec
))
2078 + (.001*(end
.tv_usec
- start
.tv_usec
)));
2079 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2080 duration
, count
/ (duration
/ 1000.0),
2081 count
* message_size
/ (duration
/ 1000.0));
2085 ofctl_group_mod__(const char *remote
, struct ofputil_group_mod
*gms
,
2086 size_t n_gms
, enum ofputil_protocol usable_protocols
)
2088 enum ofputil_protocol protocol
;
2089 struct ofputil_group_mod
*gm
;
2090 enum ofp_version version
;
2091 struct ofpbuf
*request
;
2093 struct vconn
*vconn
;
2096 protocol
= open_vconn_for_flow_mod(remote
, &vconn
, usable_protocols
);
2097 version
= ofputil_protocol_to_ofp_version(protocol
);
2099 for (i
= 0; i
< n_gms
; i
++) {
2101 request
= ofputil_encode_group_mod(version
, gm
);
2103 transact_noreply(vconn
, request
);
2113 ofctl_group_mod_file(int argc OVS_UNUSED
, char *argv
[], uint16_t command
)
2115 struct ofputil_group_mod
*gms
= NULL
;
2116 enum ofputil_protocol usable_protocols
;
2121 error
= parse_ofp_group_mod_file(argv
[2], command
, &gms
, &n_gms
,
2124 ovs_fatal(0, "%s", error
);
2126 ofctl_group_mod__(argv
[1], gms
, n_gms
, usable_protocols
);
2127 for (i
= 0; i
< n_gms
; i
++) {
2128 ofputil_bucket_list_destroy(&gms
[i
].buckets
);
2134 ofctl_group_mod(int argc
, char *argv
[], uint16_t command
)
2136 if (argc
> 2 && !strcmp(argv
[2], "-")) {
2137 ofctl_group_mod_file(argc
, argv
, command
);
2139 enum ofputil_protocol usable_protocols
;
2140 struct ofputil_group_mod gm
;
2143 error
= parse_ofp_group_mod_str(&gm
, command
, argc
> 2 ? argv
[2] : "",
2146 ovs_fatal(0, "%s", error
);
2148 ofctl_group_mod__(argv
[1], &gm
, 1, usable_protocols
);
2149 ofputil_bucket_list_destroy(&gm
.buckets
);
2154 ofctl_add_group(int argc
, char *argv
[])
2156 ofctl_group_mod(argc
, argv
, OFPGC11_ADD
);
2160 ofctl_add_groups(int argc
, char *argv
[])
2162 ofctl_group_mod_file(argc
, argv
, OFPGC11_ADD
);
2166 ofctl_mod_group(int argc
, char *argv
[])
2168 ofctl_group_mod(argc
, argv
, OFPGC11_MODIFY
);
2172 ofctl_del_groups(int argc
, char *argv
[])
2174 ofctl_group_mod(argc
, argv
, OFPGC11_DELETE
);
2178 ofctl_dump_group_stats(int argc
, char *argv
[])
2180 enum ofputil_protocol usable_protocols
;
2181 struct ofputil_group_mod gm
;
2182 struct ofpbuf
*request
;
2183 struct vconn
*vconn
;
2187 memset(&gm
, 0, sizeof gm
);
2189 error
= parse_ofp_group_mod_str(&gm
, OFPGC11_DELETE
,
2190 argc
> 2 ? argv
[2] : "",
2193 ovs_fatal(0, "%s", error
);
2196 group_id
= gm
.group_id
;
2198 open_vconn(argv
[1], &vconn
);
2199 request
= ofputil_encode_group_stats_request(vconn_get_version(vconn
),
2202 dump_stats_transaction(vconn
, request
);
2209 ofctl_dump_group_desc(int argc OVS_UNUSED
, char *argv
[])
2211 struct ofpbuf
*request
;
2212 struct vconn
*vconn
;
2215 open_vconn(argv
[1], &vconn
);
2217 if (argc
< 3 || !ofputil_group_from_string(argv
[2], &group_id
)) {
2218 group_id
= OFPG11_ALL
;
2221 request
= ofputil_encode_group_desc_request(vconn_get_version(vconn
),
2224 dump_stats_transaction(vconn
, request
);
2231 ofctl_dump_group_features(int argc OVS_UNUSED
, char *argv
[])
2233 struct ofpbuf
*request
;
2234 struct vconn
*vconn
;
2236 open_vconn(argv
[1], &vconn
);
2237 request
= ofputil_encode_group_features_request(vconn_get_version(vconn
));
2239 dump_stats_transaction(vconn
, request
);
2246 ofctl_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2252 ofctl_list_commands(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2254 print_commands(get_all_commands());
2257 /* replace-flows and diff-flows commands. */
2259 /* A flow table entry, possibly with two different versions. */
2261 struct cls_rule rule
; /* Within a "struct classifier". */
2262 struct fte_version
*versions
[2];
2265 /* One version of a Flow Table Entry. */
2266 struct fte_version
{
2268 uint16_t idle_timeout
;
2269 uint16_t hard_timeout
;
2271 struct ofpact
*ofpacts
;
2275 /* Frees 'version' and the data that it owns. */
2277 fte_version_free(struct fte_version
*version
)
2280 free(CONST_CAST(struct ofpact
*, version
->ofpacts
));
2285 /* Returns true if 'a' and 'b' are the same, false if they differ.
2287 * Ignores differences in 'flags' because there's no way to retrieve flags from
2288 * an OpenFlow switch. We have to assume that they are the same. */
2290 fte_version_equals(const struct fte_version
*a
, const struct fte_version
*b
)
2292 return (a
->cookie
== b
->cookie
2293 && a
->idle_timeout
== b
->idle_timeout
2294 && a
->hard_timeout
== b
->hard_timeout
2295 && ofpacts_equal(a
->ofpacts
, a
->ofpacts_len
,
2296 b
->ofpacts
, b
->ofpacts_len
));
2299 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2300 * 'index' into 's', followed by a new-line. */
2302 fte_version_format(const struct fte
*fte
, int index
, struct ds
*s
)
2304 const struct fte_version
*version
= fte
->versions
[index
];
2311 cls_rule_format(&fte
->rule
, s
);
2312 if (version
->cookie
!= htonll(0)) {
2313 ds_put_format(s
, " cookie=0x%"PRIx64
, ntohll(version
->cookie
));
2315 if (version
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
2316 ds_put_format(s
, " idle_timeout=%"PRIu16
, version
->idle_timeout
);
2318 if (version
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
2319 ds_put_format(s
, " hard_timeout=%"PRIu16
, version
->hard_timeout
);
2322 ds_put_cstr(s
, " actions=");
2323 ofpacts_format(version
->ofpacts
, version
->ofpacts_len
, s
);
2325 ds_put_char(s
, '\n');
2329 fte_from_cls_rule(const struct cls_rule
*cls_rule
)
2331 return cls_rule
? CONTAINER_OF(cls_rule
, struct fte
, rule
) : NULL
;
2334 /* Frees 'fte' and its versions. */
2336 fte_free(struct fte
*fte
)
2339 fte_version_free(fte
->versions
[0]);
2340 fte_version_free(fte
->versions
[1]);
2341 cls_rule_destroy(&fte
->rule
);
2346 /* Frees all of the FTEs within 'cls'. */
2348 fte_free_all(struct classifier
*cls
)
2352 CLS_FOR_EACH_SAFE (fte
, rule
, cls
) {
2353 classifier_remove(cls
, &fte
->rule
);
2356 classifier_destroy(cls
);
2359 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2360 * necessary. Sets 'version' as the version of that rule with the given
2361 * 'index', replacing any existing version, if any.
2363 * Takes ownership of 'version'. */
2365 fte_insert(struct classifier
*cls
, const struct match
*match
,
2366 int priority
, struct fte_version
*version
, int index
)
2368 struct fte
*old
, *fte
;
2370 fte
= xzalloc(sizeof *fte
);
2371 cls_rule_init(&fte
->rule
, match
, priority
);
2372 fte
->versions
[index
] = version
;
2374 old
= fte_from_cls_rule(classifier_replace(cls
, &fte
->rule
));
2376 fte_version_free(old
->versions
[index
]);
2377 fte
->versions
[!index
] = old
->versions
[!index
];
2378 cls_rule_destroy(&old
->rule
);
2383 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2384 * with the specified 'index'. Returns the flow formats able to represent the
2385 * flows that were read. */
2386 static enum ofputil_protocol
2387 read_flows_from_file(const char *filename
, struct classifier
*cls
, int index
)
2389 enum ofputil_protocol usable_protocols
;
2394 file
= !strcmp(filename
, "-") ? stdin
: fopen(filename
, "r");
2396 ovs_fatal(errno
, "%s: open", filename
);
2400 usable_protocols
= OFPUTIL_P_ANY
;
2402 while (!ds_get_preprocessed_line(&s
, file
, &line_number
)) {
2403 struct fte_version
*version
;
2404 struct ofputil_flow_mod fm
;
2406 enum ofputil_protocol usable
;
2408 error
= parse_ofp_str(&fm
, OFPFC_ADD
, ds_cstr(&s
), &usable
);
2410 ovs_fatal(0, "%s:%d: %s", filename
, line_number
, error
);
2412 usable_protocols
&= usable
;
2414 version
= xmalloc(sizeof *version
);
2415 version
->cookie
= fm
.new_cookie
;
2416 version
->idle_timeout
= fm
.idle_timeout
;
2417 version
->hard_timeout
= fm
.hard_timeout
;
2418 version
->flags
= fm
.flags
& (OFPUTIL_FF_SEND_FLOW_REM
2419 | OFPUTIL_FF_EMERG
);
2420 version
->ofpacts
= fm
.ofpacts
;
2421 version
->ofpacts_len
= fm
.ofpacts_len
;
2423 fte_insert(cls
, &fm
.match
, fm
.priority
, version
, index
);
2427 if (file
!= stdin
) {
2431 return usable_protocols
;
2435 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
2436 struct ofpbuf
**replyp
,
2437 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
2439 struct ofpbuf
*reply
= *replyp
;
2445 /* Get a flow stats reply message, if we don't already have one. */
2451 run(vconn_recv_block(vconn
, &reply
),
2452 "OpenFlow packet receive failed");
2453 } while (((struct ofp_header
*) ofpbuf_data(reply
))->xid
!= send_xid
);
2455 error
= ofptype_decode(&type
, ofpbuf_data(reply
));
2456 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
2457 ovs_fatal(0, "received bad reply: %s",
2458 ofp_to_string(ofpbuf_data(reply
), ofpbuf_size(reply
),
2463 /* Pull an individual flow stats reply out of the message. */
2464 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
2471 more
= ofpmp_more(reply
->frame
);
2472 ofpbuf_delete(reply
);
2481 ovs_fatal(0, "parse error in reply (%s)",
2482 ofperr_to_string(retval
));
2487 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2488 * format 'protocol', and adds them as flow table entries in 'cls' for the
2489 * version with the specified 'index'. */
2491 read_flows_from_switch(struct vconn
*vconn
,
2492 enum ofputil_protocol protocol
,
2493 struct classifier
*cls
, int index
)
2495 struct ofputil_flow_stats_request fsr
;
2496 struct ofputil_flow_stats fs
;
2497 struct ofpbuf
*request
;
2498 struct ofpbuf ofpacts
;
2499 struct ofpbuf
*reply
;
2502 fsr
.aggregate
= false;
2503 match_init_catchall(&fsr
.match
);
2504 fsr
.out_port
= OFPP_ANY
;
2505 fsr
.table_id
= 0xff;
2506 fsr
.cookie
= fsr
.cookie_mask
= htonll(0);
2507 request
= ofputil_encode_flow_stats_request(&fsr
, protocol
);
2508 send_xid
= ((struct ofp_header
*) ofpbuf_data(request
))->xid
;
2509 send_openflow_buffer(vconn
, request
);
2512 ofpbuf_init(&ofpacts
, 0);
2513 while (recv_flow_stats_reply(vconn
, send_xid
, &reply
, &fs
, &ofpacts
)) {
2514 struct fte_version
*version
;
2516 version
= xmalloc(sizeof *version
);
2517 version
->cookie
= fs
.cookie
;
2518 version
->idle_timeout
= fs
.idle_timeout
;
2519 version
->hard_timeout
= fs
.hard_timeout
;
2521 version
->ofpacts_len
= fs
.ofpacts_len
;
2522 version
->ofpacts
= xmemdup(fs
.ofpacts
, fs
.ofpacts_len
);
2524 fte_insert(cls
, &fs
.match
, fs
.priority
, version
, index
);
2526 ofpbuf_uninit(&ofpacts
);
2530 fte_make_flow_mod(const struct fte
*fte
, int index
, uint16_t command
,
2531 enum ofputil_protocol protocol
, struct list
*packets
)
2533 const struct fte_version
*version
= fte
->versions
[index
];
2534 struct ofputil_flow_mod fm
;
2537 minimatch_expand(&fte
->rule
.match
, &fm
.match
);
2538 fm
.priority
= fte
->rule
.priority
;
2539 fm
.cookie
= htonll(0);
2540 fm
.cookie_mask
= htonll(0);
2541 fm
.new_cookie
= version
->cookie
;
2542 fm
.modify_cookie
= true;
2544 fm
.command
= command
;
2545 fm
.idle_timeout
= version
->idle_timeout
;
2546 fm
.hard_timeout
= version
->hard_timeout
;
2547 fm
.buffer_id
= UINT32_MAX
;
2548 fm
.out_port
= OFPP_ANY
;
2549 fm
.flags
= version
->flags
;
2550 if (command
== OFPFC_ADD
|| command
== OFPFC_MODIFY
||
2551 command
== OFPFC_MODIFY_STRICT
) {
2552 fm
.ofpacts
= version
->ofpacts
;
2553 fm
.ofpacts_len
= version
->ofpacts_len
;
2558 fm
.delete_reason
= OFPRR_DELETE
;
2560 ofm
= ofputil_encode_flow_mod(&fm
, protocol
);
2561 list_push_back(packets
, &ofm
->list_node
);
2565 ofctl_replace_flows(int argc OVS_UNUSED
, char *argv
[])
2567 enum { FILE_IDX
= 0, SWITCH_IDX
= 1 };
2568 enum ofputil_protocol usable_protocols
, protocol
;
2569 struct classifier cls
;
2570 struct list requests
;
2571 struct vconn
*vconn
;
2574 classifier_init(&cls
, NULL
);
2575 usable_protocols
= read_flows_from_file(argv
[2], &cls
, FILE_IDX
);
2577 protocol
= open_vconn(argv
[1], &vconn
);
2578 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, usable_protocols
);
2580 read_flows_from_switch(vconn
, protocol
, &cls
, SWITCH_IDX
);
2582 list_init(&requests
);
2584 /* Delete flows that exist on the switch but not in the file. */
2585 CLS_FOR_EACH (fte
, rule
, &cls
) {
2586 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2587 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2589 if (sw_ver
&& !file_ver
) {
2590 fte_make_flow_mod(fte
, SWITCH_IDX
, OFPFC_DELETE_STRICT
,
2591 protocol
, &requests
);
2595 /* Add flows that exist in the file but not on the switch.
2596 * Update flows that exist in both places but differ. */
2597 CLS_FOR_EACH (fte
, rule
, &cls
) {
2598 struct fte_version
*file_ver
= fte
->versions
[FILE_IDX
];
2599 struct fte_version
*sw_ver
= fte
->versions
[SWITCH_IDX
];
2602 && (readd
|| !sw_ver
|| !fte_version_equals(sw_ver
, file_ver
))) {
2603 fte_make_flow_mod(fte
, FILE_IDX
, OFPFC_ADD
, protocol
, &requests
);
2606 transact_multiple_noreply(vconn
, &requests
);
2613 read_flows_from_source(const char *source
, struct classifier
*cls
, int index
)
2617 if (source
[0] == '/' || source
[0] == '.'
2618 || (!strchr(source
, ':') && !stat(source
, &s
))) {
2619 read_flows_from_file(source
, cls
, index
);
2621 enum ofputil_protocol protocol
;
2622 struct vconn
*vconn
;
2624 protocol
= open_vconn(source
, &vconn
);
2625 protocol
= set_protocol_for_flow_dump(vconn
, protocol
, OFPUTIL_P_ANY
);
2626 read_flows_from_switch(vconn
, protocol
, cls
, index
);
2632 ofctl_diff_flows(int argc OVS_UNUSED
, char *argv
[])
2634 bool differences
= false;
2635 struct classifier cls
;
2639 classifier_init(&cls
, NULL
);
2640 read_flows_from_source(argv
[1], &cls
, 0);
2641 read_flows_from_source(argv
[2], &cls
, 1);
2646 CLS_FOR_EACH (fte
, rule
, &cls
) {
2647 struct fte_version
*a
= fte
->versions
[0];
2648 struct fte_version
*b
= fte
->versions
[1];
2650 if (!a
|| !b
|| !fte_version_equals(a
, b
)) {
2651 fte_version_format(fte
, 0, &a_s
);
2652 fte_version_format(fte
, 1, &b_s
);
2653 if (strcmp(ds_cstr(&a_s
), ds_cstr(&b_s
))) {
2655 printf("-%s", ds_cstr(&a_s
));
2658 printf("+%s", ds_cstr(&b_s
));
2676 ofctl_meter_mod__(const char *bridge
, const char *str
, int command
)
2678 struct ofputil_meter_mod mm
;
2679 struct vconn
*vconn
;
2680 enum ofputil_protocol protocol
;
2681 enum ofputil_protocol usable_protocols
;
2682 enum ofp_version version
;
2686 error
= parse_ofp_meter_mod_str(&mm
, str
, command
, &usable_protocols
);
2688 ovs_fatal(0, "%s", error
);
2691 usable_protocols
= OFPUTIL_P_OF13_UP
;
2692 mm
.command
= command
;
2693 mm
.meter
.meter_id
= OFPM13_ALL
;
2696 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
2697 version
= ofputil_protocol_to_ofp_version(protocol
);
2698 transact_noreply(vconn
, ofputil_encode_meter_mod(version
, &mm
));
2703 ofctl_meter_request__(const char *bridge
, const char *str
,
2704 enum ofputil_meter_request_type type
)
2706 struct ofputil_meter_mod mm
;
2707 struct vconn
*vconn
;
2708 enum ofputil_protocol usable_protocols
;
2709 enum ofputil_protocol protocol
;
2710 enum ofp_version version
;
2714 error
= parse_ofp_meter_mod_str(&mm
, str
, -1, &usable_protocols
);
2716 ovs_fatal(0, "%s", error
);
2719 usable_protocols
= OFPUTIL_P_OF13_UP
;
2720 mm
.meter
.meter_id
= OFPM13_ALL
;
2723 protocol
= open_vconn_for_flow_mod(bridge
, &vconn
, usable_protocols
);
2724 version
= ofputil_protocol_to_ofp_version(protocol
);
2725 transact_noreply(vconn
, ofputil_encode_meter_request(version
,
2727 mm
.meter
.meter_id
));
2733 ofctl_add_meter(int argc OVS_UNUSED
, char *argv
[])
2735 ofctl_meter_mod__(argv
[1], argv
[2], OFPMC13_ADD
);
2739 ofctl_mod_meter(int argc OVS_UNUSED
, char *argv
[])
2741 ofctl_meter_mod__(argv
[1], argv
[2], OFPMC13_MODIFY
);
2745 ofctl_del_meters(int argc
, char *argv
[])
2747 ofctl_meter_mod__(argv
[1], argc
> 2 ? argv
[2] : NULL
, OFPMC13_DELETE
);
2751 ofctl_dump_meters(int argc
, char *argv
[])
2753 ofctl_meter_request__(argv
[1], argc
> 2 ? argv
[2] : NULL
,
2754 OFPUTIL_METER_CONFIG
);
2758 ofctl_meter_stats(int argc
, char *argv
[])
2760 ofctl_meter_request__(argv
[1], argc
> 2 ? argv
[2] : NULL
,
2761 OFPUTIL_METER_STATS
);
2765 ofctl_meter_features(int argc OVS_UNUSED
, char *argv
[])
2767 ofctl_meter_request__(argv
[1], NULL
, OFPUTIL_METER_FEATURES
);
2771 /* Undocumented commands for unit testing. */
2774 ofctl_parse_flows__(struct ofputil_flow_mod
*fms
, size_t n_fms
,
2775 enum ofputil_protocol usable_protocols
)
2777 enum ofputil_protocol protocol
= 0;
2781 usable_s
= ofputil_protocols_to_string(usable_protocols
);
2782 printf("usable protocols: %s\n", usable_s
);
2785 if (!(usable_protocols
& allowed_protocols
)) {
2786 ovs_fatal(0, "no usable protocol");
2788 for (i
= 0; i
< sizeof(enum ofputil_protocol
) * CHAR_BIT
; i
++) {
2790 if (protocol
& usable_protocols
& allowed_protocols
) {
2794 ovs_assert(is_pow2(protocol
));
2796 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol
));
2798 for (i
= 0; i
< n_fms
; i
++) {
2799 struct ofputil_flow_mod
*fm
= &fms
[i
];
2802 msg
= ofputil_encode_flow_mod(fm
, protocol
);
2803 ofp_print(stdout
, ofpbuf_data(msg
), ofpbuf_size(msg
), verbosity
);
2806 free(CONST_CAST(struct ofpact
*, fm
->ofpacts
));
2810 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2811 * it back to stdout. */
2813 ofctl_parse_flow(int argc OVS_UNUSED
, char *argv
[])
2815 enum ofputil_protocol usable_protocols
;
2816 struct ofputil_flow_mod fm
;
2819 error
= parse_ofp_flow_mod_str(&fm
, argv
[1], OFPFC_ADD
, &usable_protocols
);
2821 ovs_fatal(0, "%s", error
);
2823 ofctl_parse_flows__(&fm
, 1, usable_protocols
);
2826 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2827 * add-flows) and prints each of the flows back to stdout. */
2829 ofctl_parse_flows(int argc OVS_UNUSED
, char *argv
[])
2831 enum ofputil_protocol usable_protocols
;
2832 struct ofputil_flow_mod
*fms
= NULL
;
2836 error
= parse_ofp_flow_mod_file(argv
[1], OFPFC_ADD
, &fms
, &n_fms
,
2839 ovs_fatal(0, "%s", error
);
2841 ofctl_parse_flows__(fms
, n_fms
, usable_protocols
);
2846 ofctl_parse_nxm__(bool oxm
, enum ofp_version version
)
2851 while (!ds_get_test_line(&in
, stdin
)) {
2852 struct ofpbuf nx_match
;
2854 ovs_be64 cookie
, cookie_mask
;
2858 /* Convert string to nx_match. */
2859 ofpbuf_init(&nx_match
, 0);
2861 match_len
= oxm_match_from_string(ds_cstr(&in
), &nx_match
);
2863 match_len
= nx_match_from_string(ds_cstr(&in
), &nx_match
);
2866 /* Convert nx_match to match. */
2869 error
= oxm_pull_match(&nx_match
, &match
);
2871 error
= nx_pull_match(&nx_match
, match_len
, &match
,
2872 &cookie
, &cookie_mask
);
2876 error
= oxm_pull_match_loose(&nx_match
, &match
);
2878 error
= nx_pull_match_loose(&nx_match
, match_len
, &match
,
2879 &cookie
, &cookie_mask
);
2887 /* Convert match back to nx_match. */
2888 ofpbuf_uninit(&nx_match
);
2889 ofpbuf_init(&nx_match
, 0);
2891 match_len
= oxm_put_match(&nx_match
, &match
, version
);
2892 out
= oxm_match_to_string(&nx_match
, match_len
);
2894 match_len
= nx_put_match(&nx_match
, &match
,
2895 cookie
, cookie_mask
);
2896 out
= nx_match_to_string(ofpbuf_data(&nx_match
), match_len
);
2902 if (verbosity
> 0) {
2903 ovs_hex_dump(stdout
, ofpbuf_data(&nx_match
),
2904 ofpbuf_size(&nx_match
), 0, false);
2907 printf("nx_pull_match() returned error %s\n",
2908 ofperr_get_name(error
));
2911 ofpbuf_uninit(&nx_match
);
2916 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2917 * stdin, does some internal fussing with them, and then prints them back as
2918 * strings on stdout. */
2920 ofctl_parse_nxm(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
2922 ofctl_parse_nxm__(false, 0);
2925 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
2926 * strings from stdin, does some internal fussing with them, and then prints
2927 * them back as strings on stdout. VERSION must specify an OpenFlow version,
2928 * e.g. "OpenFlow12". */
2930 ofctl_parse_oxm(int argc OVS_UNUSED
, char *argv
[])
2932 enum ofp_version version
= ofputil_version_from_string(argv
[1]);
2933 if (version
< OFP12_VERSION
) {
2934 ovs_fatal(0, "%s: not a valid version for OXM", argv
[1]);
2937 ofctl_parse_nxm__(true, version
);
2941 print_differences(const char *prefix
,
2942 const void *a_
, size_t a_len
,
2943 const void *b_
, size_t b_len
)
2945 const uint8_t *a
= a_
;
2946 const uint8_t *b
= b_
;
2949 for (i
= 0; i
< MIN(a_len
, b_len
); i
++) {
2951 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> %02"PRIx8
"\n",
2952 prefix
, i
, a
[i
], b
[i
]);
2955 for (i
= a_len
; i
< b_len
; i
++) {
2956 printf("%s%2"PRIuSIZE
": (none) -> %02"PRIx8
"\n", prefix
, i
, b
[i
]);
2958 for (i
= b_len
; i
< a_len
; i
++) {
2959 printf("%s%2"PRIuSIZE
": %02"PRIx8
" -> (none)\n", prefix
, i
, a
[i
]);
2964 ofctl_parse_actions__(const char *version_s
, bool instructions
)
2966 enum ofp_version version
;
2969 version
= ofputil_version_from_string(version_s
);
2971 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s
);
2975 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
2976 struct ofpbuf of_out
;
2977 struct ofpbuf of_in
;
2978 struct ofpbuf ofpacts
;
2979 const char *table_id
;
2985 /* Parse table_id separated with the follow-up actions by ",", if
2987 actions
= ds_cstr(&in
);
2989 if (strstr(actions
, ",")) {
2990 table_id
= strsep(&actions
, ",");
2993 /* Parse hex bytes. */
2994 ofpbuf_init(&of_in
, 0);
2995 if (ofpbuf_put_hex(&of_in
, actions
, NULL
)[0] != '\0') {
2996 ovs_fatal(0, "Trailing garbage in hex data");
2999 /* Convert to ofpacts. */
3000 ofpbuf_init(&ofpacts
, 0);
3001 size
= ofpbuf_size(&of_in
);
3002 error
= (instructions
3003 ? ofpacts_pull_openflow_instructions
3004 : ofpacts_pull_openflow_actions
)(
3005 &of_in
, ofpbuf_size(&of_in
), version
, &ofpacts
);
3006 if (!error
&& instructions
) {
3007 /* Verify actions, enforce consistency. */
3008 enum ofputil_protocol protocol
;
3011 memset(&flow
, 0, sizeof flow
);
3012 protocol
= ofputil_protocols_from_ofp_version(version
);
3013 error
= ofpacts_check_consistency(ofpbuf_data(&ofpacts
),
3014 ofpbuf_size(&ofpacts
),
3016 table_id
? atoi(table_id
) : 0,
3020 printf("bad %s %s: %s\n\n",
3021 version_s
, instructions
? "instructions" : "actions",
3022 ofperr_get_name(error
));
3023 ofpbuf_uninit(&ofpacts
);
3024 ofpbuf_uninit(&of_in
);
3027 ofpbuf_push_uninit(&of_in
, size
);
3029 /* Print cls_rule. */
3031 ds_put_cstr(&s
, "actions=");
3032 ofpacts_format(ofpbuf_data(&ofpacts
), ofpbuf_size(&ofpacts
), &s
);
3036 /* Convert back to ofp10 actions and print differences from input. */
3037 ofpbuf_init(&of_out
, 0);
3039 ofpacts_put_openflow_instructions( ofpbuf_data(&ofpacts
),
3040 ofpbuf_size(&ofpacts
),
3043 ofpacts_put_openflow_actions( ofpbuf_data(&ofpacts
),
3044 ofpbuf_size(&ofpacts
),
3048 print_differences("", ofpbuf_data(&of_in
), ofpbuf_size(&of_in
),
3049 ofpbuf_data(&of_out
), ofpbuf_size(&of_out
));
3052 ofpbuf_uninit(&ofpacts
);
3053 ofpbuf_uninit(&of_in
);
3054 ofpbuf_uninit(&of_out
);
3059 /* "parse-actions VERSION": reads a series of action specifications for the
3060 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3061 * prints them as strings on stdout, and then converts them back to hex bytes
3062 * and prints any differences from the input. */
3064 ofctl_parse_actions(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
3066 ofctl_parse_actions__(argv
[1], false);
3069 /* "parse-actions VERSION": reads a series of instruction specifications for
3070 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3071 * ofpacts, prints them as strings on stdout, and then converts them back to
3072 * hex bytes and prints any differences from the input. */
3074 ofctl_parse_instructions(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
3076 ofctl_parse_actions__(argv
[1], true);
3079 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3080 * bytes from stdin, converts them to cls_rules, prints them as strings on
3081 * stdout, and then converts them back to hex bytes and prints any differences
3084 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3085 * values are ignored in the input and will be set to zero when OVS converts
3086 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3087 * it does the conversion to hex, to ensure that in fact they are ignored. */
3089 ofctl_parse_ofp10_match(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
3096 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3097 struct ofpbuf match_in
, match_expout
;
3098 struct ofp10_match match_out
;
3099 struct ofp10_match match_normal
;
3103 /* Parse hex bytes to use for expected output. */
3105 ds_put_cstr(&expout
, ds_cstr(&in
));
3106 for (p
= ds_cstr(&expout
); *p
; p
++) {
3111 ofpbuf_init(&match_expout
, 0);
3112 if (ofpbuf_put_hex(&match_expout
, ds_cstr(&expout
), NULL
)[0] != '\0') {
3113 ovs_fatal(0, "Trailing garbage in hex data");
3115 if (ofpbuf_size(&match_expout
) != sizeof(struct ofp10_match
)) {
3116 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3117 ofpbuf_size(&match_expout
), sizeof(struct ofp10_match
));
3120 /* Parse hex bytes for input. */
3121 for (p
= ds_cstr(&in
); *p
; p
++) {
3123 *p
= "0123456789abcdef"[random_uint32() & 0xf];
3126 ofpbuf_init(&match_in
, 0);
3127 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3128 ovs_fatal(0, "Trailing garbage in hex data");
3130 if (ofpbuf_size(&match_in
) != sizeof(struct ofp10_match
)) {
3131 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3132 ofpbuf_size(&match_in
), sizeof(struct ofp10_match
));
3135 /* Convert to cls_rule and print. */
3136 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in
), &match
);
3137 match_print(&match
);
3139 /* Convert back to ofp10_match and print differences from input. */
3140 ofputil_match_to_ofp10_match(&match
, &match_out
);
3141 print_differences("", ofpbuf_data(&match_expout
), ofpbuf_size(&match_expout
),
3142 &match_out
, sizeof match_out
);
3144 /* Normalize, then convert and compare again. */
3145 ofputil_normalize_match(&match
);
3146 ofputil_match_to_ofp10_match(&match
, &match_normal
);
3147 print_differences("normal: ", &match_out
, sizeof match_out
,
3148 &match_normal
, sizeof match_normal
);
3151 ofpbuf_uninit(&match_in
);
3152 ofpbuf_uninit(&match_expout
);
3155 ds_destroy(&expout
);
3158 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3159 * bytes from stdin, converts them to "struct match"es, prints them as strings
3160 * on stdout, and then converts them back to hex bytes and prints any
3161 * differences from the input. */
3163 ofctl_parse_ofp11_match(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
3168 while (!ds_get_preprocessed_line(&in
, stdin
, NULL
)) {
3169 struct ofpbuf match_in
;
3170 struct ofp11_match match_out
;
3174 /* Parse hex bytes. */
3175 ofpbuf_init(&match_in
, 0);
3176 if (ofpbuf_put_hex(&match_in
, ds_cstr(&in
), NULL
)[0] != '\0') {
3177 ovs_fatal(0, "Trailing garbage in hex data");
3179 if (ofpbuf_size(&match_in
) != sizeof(struct ofp11_match
)) {
3180 ovs_fatal(0, "Input is %"PRIu32
" bytes, expected %"PRIuSIZE
,
3181 ofpbuf_size(&match_in
), sizeof(struct ofp11_match
));
3184 /* Convert to match. */
3185 error
= ofputil_match_from_ofp11_match(ofpbuf_data(&match_in
), &match
);
3187 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error
));
3188 ofpbuf_uninit(&match_in
);
3193 match_print(&match
);
3195 /* Convert back to ofp11_match and print differences from input. */
3196 ofputil_match_to_ofp11_match(&match
, &match_out
);
3198 print_differences("", ofpbuf_data(&match_in
), ofpbuf_size(&match_in
),
3199 &match_out
, sizeof match_out
);
3202 ofpbuf_uninit(&match_in
);
3207 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3209 ofctl_parse_pcap(int argc OVS_UNUSED
, char *argv
[])
3213 pcap
= ovs_pcap_open(argv
[1], "rb");
3215 ovs_fatal(errno
, "%s: open failed", argv
[1]);
3219 struct ofpbuf
*packet
;
3221 const struct pkt_metadata md
= PKT_METADATA_INITIALIZER(ODPP_NONE
);
3224 error
= ovs_pcap_read(pcap
, &packet
, NULL
);
3228 ovs_fatal(error
, "%s: read failed", argv
[1]);
3231 flow_extract(packet
, &md
, &flow
);
3232 flow_print(stdout
, &flow
);
3234 ofpbuf_delete(packet
);
3238 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3239 * mask values to and from various formats and prints the results. */
3241 ofctl_check_vlan(int argc OVS_UNUSED
, char *argv
[])
3246 struct ofputil_flow_mod fm
;
3249 struct match nxm_match
;
3253 struct ofp10_match of10_raw
;
3254 struct match of10_match
;
3256 struct ofp11_match of11_raw
;
3257 struct match of11_match
;
3262 enum ofputil_protocol usable_protocols
; /* Unused for now. */
3264 match_init_catchall(&match
);
3265 match
.flow
.vlan_tci
= htons(strtoul(argv
[1], NULL
, 16));
3266 match
.wc
.masks
.vlan_tci
= htons(strtoul(argv
[2], NULL
, 16));
3268 /* Convert to and from string. */
3269 string_s
= match_to_string(&match
, OFP_DEFAULT_PRIORITY
);
3270 printf("%s -> ", string_s
);
3272 error_s
= parse_ofp_str(&fm
, -1, string_s
, &usable_protocols
);
3274 ovs_fatal(0, "%s", error_s
);
3276 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3277 ntohs(fm
.match
.flow
.vlan_tci
),
3278 ntohs(fm
.match
.wc
.masks
.vlan_tci
));
3281 /* Convert to and from NXM. */
3282 ofpbuf_init(&nxm
, 0);
3283 nxm_match_len
= nx_put_match(&nxm
, &match
, htonll(0), htonll(0));
3284 nxm_s
= nx_match_to_string(ofpbuf_data(&nxm
), nxm_match_len
);
3285 error
= nx_pull_match(&nxm
, nxm_match_len
, &nxm_match
, NULL
, NULL
);
3286 printf("NXM: %s -> ", nxm_s
);
3288 printf("%s\n", ofperr_to_string(error
));
3290 printf("%04"PRIx16
"/%04"PRIx16
"\n",
3291 ntohs(nxm_match
.flow
.vlan_tci
),
3292 ntohs(nxm_match
.wc
.masks
.vlan_tci
));
3295 ofpbuf_uninit(&nxm
);
3297 /* Convert to and from OXM. */
3298 ofpbuf_init(&nxm
, 0);
3299 nxm_match_len
= oxm_put_match(&nxm
, &match
, OFP12_VERSION
);
3300 nxm_s
= oxm_match_to_string(&nxm
, nxm_match_len
);
3301 error
= oxm_pull_match(&nxm
, &nxm_match
);
3302 printf("OXM: %s -> ", nxm_s
);
3304 printf("%s\n", ofperr_to_string(error
));
3306 uint16_t vid
= ntohs(nxm_match
.flow
.vlan_tci
) &
3307 (VLAN_VID_MASK
| VLAN_CFI
);
3308 uint16_t mask
= ntohs(nxm_match
.wc
.masks
.vlan_tci
) &
3309 (VLAN_VID_MASK
| VLAN_CFI
);
3311 printf("%04"PRIx16
"/%04"PRIx16
",", vid
, mask
);
3312 if (vid
&& vlan_tci_to_pcp(nxm_match
.wc
.masks
.vlan_tci
)) {
3313 printf("%02"PRIx8
"\n", vlan_tci_to_pcp(nxm_match
.flow
.vlan_tci
));
3319 ofpbuf_uninit(&nxm
);
3321 /* Convert to and from OpenFlow 1.0. */
3322 ofputil_match_to_ofp10_match(&match
, &of10_raw
);
3323 ofputil_match_from_ofp10_match(&of10_raw
, &of10_match
);
3324 printf("OF1.0: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3325 ntohs(of10_raw
.dl_vlan
),
3326 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN
)) != 0,
3327 of10_raw
.dl_vlan_pcp
,
3328 (of10_raw
.wildcards
& htonl(OFPFW10_DL_VLAN_PCP
)) != 0,
3329 ntohs(of10_match
.flow
.vlan_tci
),
3330 ntohs(of10_match
.wc
.masks
.vlan_tci
));
3332 /* Convert to and from OpenFlow 1.1. */
3333 ofputil_match_to_ofp11_match(&match
, &of11_raw
);
3334 ofputil_match_from_ofp11_match(&of11_raw
, &of11_match
);
3335 printf("OF1.1: %04"PRIx16
"/%d,%02"PRIx8
"/%d -> %04"PRIx16
"/%04"PRIx16
"\n",
3336 ntohs(of11_raw
.dl_vlan
),
3337 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN
)) != 0,
3338 of11_raw
.dl_vlan_pcp
,
3339 (of11_raw
.wildcards
& htonl(OFPFW11_DL_VLAN_PCP
)) != 0,
3340 ntohs(of11_match
.flow
.vlan_tci
),
3341 ntohs(of11_match
.wc
.masks
.vlan_tci
));
3344 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3347 ofctl_print_error(int argc OVS_UNUSED
, char *argv
[])
3352 error
= ofperr_from_name(argv
[1]);
3354 ovs_fatal(0, "unknown error \"%s\"", argv
[1]);
3357 for (version
= 0; version
<= UINT8_MAX
; version
++) {
3358 const char *name
= ofperr_domain_get_name(version
);
3360 int vendor
= ofperr_get_vendor(error
, version
);
3361 int type
= ofperr_get_type(error
, version
);
3362 int code
= ofperr_get_code(error
, version
);
3364 if (vendor
!= -1 || type
!= -1 || code
!= -1) {
3365 printf("%s: vendor %#x, type %d, code %d\n",
3366 name
, vendor
, type
, code
);
3372 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3373 * error named ENUM and prints the error reply in hex. */
3375 ofctl_encode_error_reply(int argc OVS_UNUSED
, char *argv
[])
3377 const struct ofp_header
*oh
;
3378 struct ofpbuf request
, *reply
;
3381 error
= ofperr_from_name(argv
[1]);
3383 ovs_fatal(0, "unknown error \"%s\"", argv
[1]);
3386 ofpbuf_init(&request
, 0);
3387 if (ofpbuf_put_hex(&request
, argv
[2], NULL
)[0] != '\0') {
3388 ovs_fatal(0, "Trailing garbage in hex data");
3390 if (ofpbuf_size(&request
) < sizeof(struct ofp_header
)) {
3391 ovs_fatal(0, "Request too short");
3394 oh
= ofpbuf_data(&request
);
3395 if (ofpbuf_size(&request
) != ntohs(oh
->length
)) {
3396 ovs_fatal(0, "Request size inconsistent");
3399 reply
= ofperr_encode_reply(error
, ofpbuf_data(&request
));
3400 ofpbuf_uninit(&request
);
3402 ovs_hex_dump(stdout
, ofpbuf_data(reply
), ofpbuf_size(reply
), 0, false);
3403 ofpbuf_delete(reply
);
3406 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3407 * binary data, interpreting them as an OpenFlow message, and prints the
3408 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3410 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3411 * HEXSTRING_FILE contains the HEXSTRING. */
3413 ofctl_ofp_print(int argc
, char *argv
[])
3415 struct ofpbuf packet
;
3422 if (!strcmp(argv
[argc
-1], "-")) {
3423 if (ds_get_line(&line
, stdin
)) {
3424 VLOG_FATAL("Failed to read stdin");
3427 buffer
= line
.string
;
3428 verbosity
= argc
> 2 ? atoi(argv
[1]) : verbosity
;
3429 } else if (argc
> 2) {
3431 verbosity
= atoi(argv
[2]);
3436 ofpbuf_init(&packet
, strlen(buffer
) / 2);
3437 if (ofpbuf_put_hex(&packet
, buffer
, NULL
)[0] != '\0') {
3438 ovs_fatal(0, "trailing garbage following hex bytes");
3440 ofp_print(stdout
, ofpbuf_data(&packet
), ofpbuf_size(&packet
), verbosity
);
3441 ofpbuf_uninit(&packet
);
3445 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3446 * and dumps each message in hex. */
3448 ofctl_encode_hello(int argc OVS_UNUSED
, char *argv
[])
3450 uint32_t bitmap
= strtol(argv
[1], NULL
, 0);
3451 struct ofpbuf
*hello
;
3453 hello
= ofputil_encode_hello(bitmap
);
3454 ovs_hex_dump(stdout
, ofpbuf_data(hello
), ofpbuf_size(hello
), 0, false);
3455 ofp_print(stdout
, ofpbuf_data(hello
), ofpbuf_size(hello
), verbosity
);
3456 ofpbuf_delete(hello
);
3459 static const struct command all_commands
[] = {
3462 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3463 1, 3, ofctl_monitor
},
3464 { "snoop", "switch",
3465 1, 1, ofctl_snoop
},
3466 { "dump-desc", "switch",
3467 1, 1, ofctl_dump_desc
},
3468 { "dump-tables", "switch",
3469 1, 1, ofctl_dump_tables
},
3470 { "dump-table-features", "switch",
3471 1, 1, ofctl_dump_table_features
},
3472 { "dump-flows", "switch",
3473 1, 2, ofctl_dump_flows
},
3474 { "dump-aggregate", "switch",
3475 1, 2, ofctl_dump_aggregate
},
3476 { "queue-stats", "switch [port [queue]]",
3477 1, 3, ofctl_queue_stats
},
3478 { "queue-get-config", "switch port",
3479 2, 2, ofctl_queue_get_config
},
3480 { "add-flow", "switch flow",
3481 2, 2, ofctl_add_flow
},
3482 { "add-flows", "switch file",
3483 2, 2, ofctl_add_flows
},
3484 { "mod-flows", "switch flow",
3485 2, 2, ofctl_mod_flows
},
3486 { "del-flows", "switch [flow]",
3487 1, 2, ofctl_del_flows
},
3488 { "replace-flows", "switch file",
3489 2, 2, ofctl_replace_flows
},
3490 { "diff-flows", "source1 source2",
3491 2, 2, ofctl_diff_flows
},
3492 { "add-meter", "switch meter",
3493 2, 2, ofctl_add_meter
},
3494 { "mod-meter", "switch meter",
3495 2, 2, ofctl_mod_meter
},
3496 { "del-meter", "switch meter",
3497 2, 2, ofctl_del_meters
},
3498 { "del-meters", "switch",
3499 1, 1, ofctl_del_meters
},
3500 { "dump-meter", "switch meter",
3501 2, 2, ofctl_dump_meters
},
3502 { "dump-meters", "switch",
3503 1, 1, ofctl_dump_meters
},
3504 { "meter-stats", "switch [meter]",
3505 1, 2, ofctl_meter_stats
},
3506 { "meter-features", "switch",
3507 1, 1, ofctl_meter_features
},
3508 { "packet-out", "switch in_port actions packet...",
3509 4, INT_MAX
, ofctl_packet_out
},
3510 { "dump-ports", "switch [port]",
3511 1, 2, ofctl_dump_ports
},
3512 { "dump-ports-desc", "switch [port]",
3513 1, 2, ofctl_dump_ports_desc
},
3514 { "mod-port", "switch iface act",
3515 3, 3, ofctl_mod_port
},
3516 { "mod-table", "switch mod",
3517 3, 3, ofctl_mod_table
},
3518 { "get-frags", "switch",
3519 1, 1, ofctl_get_frags
},
3520 { "set-frags", "switch frag_mode",
3521 2, 2, ofctl_set_frags
},
3522 { "probe", "target",
3523 1, 1, ofctl_probe
},
3524 { "ping", "target [n]",
3526 { "benchmark", "target n count",
3527 3, 3, ofctl_benchmark
},
3529 { "ofp-parse", "file",
3530 1, 1, ofctl_ofp_parse
},
3531 { "ofp-parse-pcap", "pcap",
3532 1, INT_MAX
, ofctl_ofp_parse_pcap
},
3534 { "add-group", "switch group",
3535 1, 2, ofctl_add_group
},
3536 { "add-groups", "switch file",
3537 1, 2, ofctl_add_groups
},
3538 { "mod-group", "switch group",
3539 1, 2, ofctl_mod_group
},
3540 { "del-groups", "switch [group]",
3541 1, 2, ofctl_del_groups
},
3542 { "dump-groups", "switch [group]",
3543 1, 2, ofctl_dump_group_desc
},
3544 { "dump-group-stats", "switch [group]",
3545 1, 2, ofctl_dump_group_stats
},
3546 { "dump-group-features", "switch",
3547 1, 1, ofctl_dump_group_features
},
3548 { "help", NULL
, 0, INT_MAX
, ofctl_help
},
3549 { "list-commands", NULL
, 0, INT_MAX
, ofctl_list_commands
},
3551 /* Undocumented commands for testing. */
3552 { "parse-flow", NULL
, 1, 1, ofctl_parse_flow
},
3553 { "parse-flows", NULL
, 1, 1, ofctl_parse_flows
},
3554 { "parse-nx-match", NULL
, 0, 0, ofctl_parse_nxm
},
3555 { "parse-nxm", NULL
, 0, 0, ofctl_parse_nxm
},
3556 { "parse-oxm", NULL
, 1, 1, ofctl_parse_oxm
},
3557 { "parse-actions", NULL
, 1, 1, ofctl_parse_actions
},
3558 { "parse-instructions", NULL
, 1, 1, ofctl_parse_instructions
},
3559 { "parse-ofp10-match", NULL
, 0, 0, ofctl_parse_ofp10_match
},
3560 { "parse-ofp11-match", NULL
, 0, 0, ofctl_parse_ofp11_match
},
3561 { "parse-pcap", NULL
, 1, 1, ofctl_parse_pcap
},
3562 { "check-vlan", NULL
, 2, 2, ofctl_check_vlan
},
3563 { "print-error", NULL
, 1, 1, ofctl_print_error
},
3564 { "encode-error-reply", NULL
, 2, 2, ofctl_encode_error_reply
},
3565 { "ofp-print", NULL
, 1, 2, ofctl_ofp_print
},
3566 { "encode-hello", NULL
, 1, 1, ofctl_encode_hello
},
3568 { NULL
, NULL
, 0, 0, NULL
},
3571 static const struct command
*get_all_commands(void)
3573 return all_commands
;