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 /* --sort, --rsort: Sort order. */
95 enum sort_order { SORT_ASC, SORT_DESC };
96 struct sort_criterion {
97 const struct mf_field *field; /* NULL means to sort by priority. */
98 enum sort_order order;
100 static struct sort_criterion *criteria;
101 static size_t n_criteria, allocated_criteria;
103 static const struct command *get_all_commands(void);
105 static void usage(void) NO_RETURN;
106 static void parse_options(int argc, char *argv[]);
108 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
109 struct ofpbuf **replyp,
110 struct ofputil_flow_stats *,
111 struct ofpbuf *ofpacts);
113 main(int argc, char *argv[])
115 set_program_name(argv[0]);
116 parse_options(argc, argv);
117 fatal_ignore_sigpipe();
118 run_command(argc - optind, argv + optind, get_all_commands());
123 add_sort_criterion(enum sort_order order, const char *field)
125 struct sort_criterion *sc;
127 if (n_criteria >= allocated_criteria) {
128 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
131 sc = &criteria[n_criteria++];
132 if (!field || !strcasecmp(field, "priority")) {
135 sc->field = mf_from_name(field);
137 ovs_fatal(0, "%s: unknown field name", field);
144 parse_options(int argc, char *argv[])
147 OPT_STRICT = UCHAR_MAX + 1,
153 OFP_VERSION_OPTION_ENUMS,
156 static const struct option long_options[] = {
157 {"timeout", required_argument, NULL, 't'},
158 {"strict", no_argument, NULL, OPT_STRICT},
159 {"readd", no_argument, NULL, OPT_READD},
160 {"flow-format", required_argument, NULL, 'F'},
161 {"packet-in-format", required_argument, NULL, 'P'},
162 {"more", no_argument, NULL, 'm'},
163 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
164 {"sort", optional_argument, NULL, OPT_SORT},
165 {"rsort", optional_argument, NULL, OPT_RSORT},
166 {"help", no_argument, NULL, 'h'},
168 OFP_VERSION_LONG_OPTIONS,
170 STREAM_SSL_LONG_OPTIONS,
173 char *short_options = long_options_to_short_options(long_options);
175 enum ofputil_protocol version_protocols;
177 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
178 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
179 * requests, but those requests have subtly different semantics in
180 * different OpenFlow versions. For example:
182 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
183 * existing flow to modify adds a new flow.
185 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
186 * existing flow to modify adds a new flow, but only if the mod-flow
187 * did not match on the flow cookie.
189 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
192 set_allowed_ofp_versions("OpenFlow10");
195 unsigned long int timeout;
198 c = getopt_long(argc, argv, short_options, long_options, NULL);
205 timeout = strtoul(optarg, NULL, 10);
207 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
215 allowed_protocols = ofputil_protocols_from_string(optarg);
216 if (!allowed_protocols) {
217 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
222 preferred_packet_in_format =
223 ofputil_packet_in_format_from_string(optarg);
224 if (preferred_packet_in_format < 0) {
225 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
249 add_sort_criterion(SORT_ASC, optarg);
253 add_sort_criterion(SORT_DESC, optarg);
256 DAEMON_OPTION_HANDLERS
257 OFP_VERSION_OPTION_HANDLERS
259 STREAM_SSL_OPTION_HANDLERS
270 /* Always do a final sort pass based on priority. */
271 add_sort_criterion(SORT_DESC, "priority");
276 versions = get_allowed_ofp_versions();
277 version_protocols = ofputil_protocols_from_version_bitmap(versions);
278 if (!(allowed_protocols & version_protocols)) {
279 char *protocols = ofputil_protocols_to_string(allowed_protocols);
280 struct ds version_s = DS_EMPTY_INITIALIZER;
282 ofputil_format_version_bitmap_names(&version_s, versions);
283 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
284 "any of the enabled flow formats (%s). (Use -O to enable "
285 "additional OpenFlow versions or -F to enable additional "
286 "flow formats.)", ds_cstr(&version_s), protocols);
288 allowed_protocols &= version_protocols;
289 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
296 printf("%s: OpenFlow switch management utility\n"
297 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
298 "\nFor OpenFlow switches:\n"
299 " show SWITCH show OpenFlow information\n"
300 " dump-desc SWITCH print switch description\n"
301 " dump-tables SWITCH print table stats\n"
302 " dump-table-features SWITCH print table features\n"
303 " mod-port SWITCH IFACE ACT modify port behavior\n"
304 " mod-table SWITCH MOD modify flow table behavior\n"
305 " get-frags SWITCH print fragment handling behavior\n"
306 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
307 " dump-ports SWITCH [PORT] print port statistics\n"
308 " dump-ports-desc SWITCH print port descriptions\n"
309 " dump-flows SWITCH print all flow entries\n"
310 " dump-flows SWITCH FLOW print matching FLOWs\n"
311 " dump-aggregate SWITCH print aggregate flow statistics\n"
312 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
313 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
314 " add-flow SWITCH FLOW add flow described by FLOW\n"
315 " add-flows SWITCH FILE add flows from FILE\n"
316 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
317 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
318 " replace-flows SWITCH FILE replace flows with those in FILE\n"
319 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
320 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
321 " execute ACTIONS on PACKET\n"
322 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
323 " print packets received from SWITCH\n"
324 " snoop SWITCH snoop on SWITCH and its controller\n"
325 " add-group SWITCH GROUP add group described by GROUP\n"
326 " add-group SWITCH FILE add group from FILE\n"
327 " mod-group SWITCH GROUP modify specific group\n"
328 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
329 " dump-group-features SWITCH print group features\n"
330 " dump-groups SWITCH [GROUP] print group description\n"
331 " dump-group-stats SWITCH [GROUP] print group statistics\n"
332 " queue-get-config SWITCH PORT print queue information for port\n"
333 " add-meter SWITCH METER add meter described by METER\n"
334 " mod-meter SWITCH METER modify specific METER\n"
335 " del-meter SWITCH METER delete METER\n"
336 " del-meters SWITCH delete all meters\n"
337 " dump-meter SWITCH METER print METER configuration\n"
338 " dump-meters SWITCH print all meter configuration\n"
339 " meter-stats SWITCH [METER] print meter statistics\n"
340 " meter-features SWITCH print meter features\n"
341 "\nFor OpenFlow switches and controllers:\n"
342 " probe TARGET probe whether TARGET is up\n"
343 " ping TARGET [N] latency of N-byte echos\n"
344 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
345 "SWITCH or TARGET is an active OpenFlow connection method.\n"
346 "\nOther commands:\n"
347 " ofp-parse FILE print messages read from FILE\n"
348 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
349 program_name, program_name);
350 vconn_usage(true, false, false);
354 printf("\nOther options:\n"
355 " --strict use strict match for flow commands\n"
356 " --readd replace flows that haven't changed\n"
357 " -F, --flow-format=FORMAT force particular flow format\n"
358 " -P, --packet-in-format=FRMT force particular packet in format\n"
359 " -m, --more be more verbose printing OpenFlow\n"
360 " --timestamp (monitor, snoop) print timestamps\n"
361 " -t, --timeout=SECS give up after SECS seconds\n"
362 " --sort[=field] sort in ascending order\n"
363 " --rsort[=field] sort in descending order\n"
364 " -h, --help display this help message\n"
365 " -V, --version display version information\n");
370 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
371 const char *argv[] OVS_UNUSED, void *exiting_)
373 bool *exiting = exiting_;
375 unixctl_command_reply(conn, NULL);
378 static void run(int retval, const char *message, ...)
382 run(int retval, const char *message, ...)
387 va_start(args, message);
388 ovs_fatal_valist(retval, message, args);
392 /* Generic commands. */
395 open_vconn_socket(const char *name, struct vconn **vconnp)
397 char *vconn_name = xasprintf("unix:%s", name);
400 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
402 if (error && error != ENOENT) {
403 ovs_fatal(0, "%s: failed to open socket (%s)", name,
404 ovs_strerror(error));
411 enum open_target { MGMT, SNOOP };
413 static enum ofputil_protocol
414 open_vconn__(const char *name, enum open_target target,
415 struct vconn **vconnp)
417 const char *suffix = target == MGMT ? "mgmt" : "snoop";
418 char *datapath_name, *datapath_type, *socket_name;
419 enum ofputil_protocol protocol;
424 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
426 ofproto_parse_name(name, &datapath_name, &datapath_type);
427 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
431 if (strchr(name, ':')) {
432 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
433 "connecting to %s", name);
434 } else if (!open_vconn_socket(name, vconnp)) {
436 } else if (!open_vconn_socket(bridge_path, vconnp)) {
438 } else if (!open_vconn_socket(socket_name, vconnp)) {
441 ovs_fatal(0, "%s is not a bridge or a socket", name);
444 if (target == SNOOP) {
445 vconn_set_recv_any_version(*vconnp);
451 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
452 error = vconn_connect_block(*vconnp);
454 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
455 ovs_strerror(error));
458 ofp_version = vconn_get_version(*vconnp);
459 protocol = ofputil_protocol_from_ofp_version(ofp_version);
461 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
467 static enum ofputil_protocol
468 open_vconn(const char *name, struct vconn **vconnp)
470 return open_vconn__(name, MGMT, vconnp);
474 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
476 ofpmsg_update_length(buffer);
477 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
481 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
483 struct ofpbuf *reply;
485 ofpmsg_update_length(request);
486 run(vconn_transact(vconn, request, &reply), "talking to %s",
487 vconn_get_name(vconn));
488 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
489 ofpbuf_delete(reply);
493 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
495 struct ofpbuf *request;
498 open_vconn(vconn_name, &vconn);
499 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
500 dump_transaction(vconn, request);
505 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
507 const struct ofp_header *request_oh = ofpbuf_data(request);
508 ovs_be32 send_xid = request_oh->xid;
509 enum ofpraw request_raw;
510 enum ofpraw reply_raw;
513 ofpraw_decode_partial(&request_raw, ofpbuf_data(request), ofpbuf_size(request));
514 reply_raw = ofpraw_stats_request_to_reply(request_raw,
515 request_oh->version);
517 send_openflow_buffer(vconn, request);
520 struct ofpbuf *reply;
522 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
523 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
524 if (send_xid == recv_xid) {
527 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
529 ofpraw_decode(&raw, ofpbuf_data(reply));
530 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
532 } else if (raw == reply_raw) {
533 done = !ofpmp_more(ofpbuf_data(reply));
535 ovs_fatal(0, "received bad reply: %s",
536 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
540 VLOG_DBG("received reply with xid %08"PRIx32" "
541 "!= expected %08"PRIx32, recv_xid, send_xid);
543 ofpbuf_delete(reply);
548 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
550 struct ofpbuf *request;
553 open_vconn(vconn_name, &vconn);
554 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
555 dump_stats_transaction(vconn, request);
559 /* Sends all of the 'requests', which should be requests that only have replies
560 * if an error occurs, and waits for them to succeed or fail. If an error does
561 * occur, prints it and exits with an error.
563 * Destroys all of the 'requests'. */
565 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
567 struct ofpbuf *request, *reply;
569 LIST_FOR_EACH (request, list_node, requests) {
570 ofpmsg_update_length(request);
573 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
574 "talking to %s", vconn_get_name(vconn));
576 ofp_print(stderr, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 2);
579 ofpbuf_delete(reply);
582 /* Sends 'request', which should be a request that only has a reply if an error
583 * occurs, and waits for it to succeed or fail. If an error does occur, prints
584 * it and exits with an error.
586 * Destroys 'request'. */
588 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
590 struct list requests;
592 list_init(&requests);
593 list_push_back(&requests, &request->list_node);
594 transact_multiple_noreply(vconn, &requests);
598 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
600 struct ofp_switch_config *config;
601 struct ofpbuf *request;
602 struct ofpbuf *reply;
605 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
606 vconn_get_version(vconn), 0);
607 run(vconn_transact(vconn, request, &reply),
608 "talking to %s", vconn_get_name(vconn));
610 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
611 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
614 config = ofpbuf_pull(reply, sizeof *config);
617 ofpbuf_delete(reply);
621 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
623 struct ofpbuf *request;
625 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
626 ofpbuf_put(request, config, sizeof *config);
628 transact_noreply(vconn, request);
632 ofctl_show(int argc OVS_UNUSED, char *argv[])
634 const char *vconn_name = argv[1];
635 enum ofp_version version;
637 struct ofpbuf *request;
638 struct ofpbuf *reply;
641 open_vconn(vconn_name, &vconn);
642 version = vconn_get_version(vconn);
643 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
644 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
646 has_ports = ofputil_switch_features_has_ports(reply);
647 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
648 ofpbuf_delete(reply);
651 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, version, 0);
652 dump_stats_transaction(vconn, request);
654 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
659 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
661 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
665 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
667 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
671 ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[])
673 struct ofpbuf *request;
676 open_vconn(argv[1], &vconn);
677 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
679 dump_stats_transaction(vconn, request);
685 static bool fetch_port_by_stats(struct vconn *,
686 const char *port_name, ofp_port_t port_no,
687 struct ofputil_phy_port *);
689 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
690 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
691 * successful, false on failure.
693 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
694 * list of ports in OFPT_FEATURES_REPLY. */
696 fetch_port_by_features(struct vconn *vconn,
697 const char *port_name, ofp_port_t port_no,
698 struct ofputil_phy_port *pp)
700 struct ofputil_switch_features features;
701 const struct ofp_header *oh;
702 struct ofpbuf *request, *reply;
708 /* Fetch the switch's ofp_switch_features. */
709 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
710 vconn_get_version(vconn), 0);
711 run(vconn_transact(vconn, request, &reply),
712 "talking to %s", vconn_get_name(vconn));
714 oh = ofpbuf_data(reply);
715 if (ofptype_decode(&type, ofpbuf_data(reply))
716 || type != OFPTYPE_FEATURES_REPLY) {
717 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
719 if (!ofputil_switch_features_has_ports(reply)) {
720 /* The switch features reply does not contain a complete list of ports.
721 * Probably, there are more ports than will fit into a single 64 kB
722 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
724 ofpbuf_delete(reply);
725 return fetch_port_by_stats(vconn, port_name, port_no, pp);
728 error = ofputil_decode_switch_features(oh, &features, &b);
730 ovs_fatal(0, "%s: failed to decode features reply (%s)",
731 vconn_get_name(vconn), ofperr_to_string(error));
734 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
735 if (port_no != OFPP_NONE
736 ? port_no == pp->port_no
737 : !strcmp(pp->name, port_name)) {
742 ofpbuf_delete(reply);
746 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
747 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
748 * successful, false on failure.
750 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
751 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
752 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
753 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
755 fetch_port_by_stats(struct vconn *vconn,
756 const char *port_name, ofp_port_t port_no,
757 struct ofputil_phy_port *pp)
759 struct ofpbuf *request;
764 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST,
765 vconn_get_version(vconn), 0);
766 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
768 send_openflow_buffer(vconn, request);
771 struct ofpbuf *reply;
773 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
774 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
775 if (send_xid == recv_xid) {
776 struct ofp_header *oh = ofpbuf_data(reply);
781 ofpbuf_use_const(&b, oh, ntohs(oh->length));
782 if (ofptype_pull(&type, &b)
783 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
784 ovs_fatal(0, "received bad reply: %s",
785 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
789 flags = ofpmp_flags(oh);
790 done = !(flags & OFPSF_REPLY_MORE);
793 /* We've already found the port, but we need to drain
794 * the queue of any other replies for this request. */
798 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
799 if (port_no != OFPP_NONE ? port_no == pp->port_no
800 : !strcmp(pp->name, port_name)) {
806 VLOG_DBG("received reply with xid %08"PRIx32" "
807 "!= expected %08"PRIx32, recv_xid, send_xid);
809 ofpbuf_delete(reply);
816 str_to_ofp(const char *s, ofp_port_t *ofp_port)
821 ret = str_to_uint(s, 10, &port_);
822 *ofp_port = u16_to_ofp(port_);
826 /* Opens a connection to 'vconn_name', fetches the port structure for
827 * 'port_name' (which may be a port name or number), and copies it into
830 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
831 struct ofputil_phy_port *pp)
837 /* Try to interpret the argument as a port number. */
838 if (!str_to_ofp(port_name, &port_no)) {
842 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
843 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
844 * into the OFPST_PORT_DESC reply. Try it the correct way. */
845 open_vconn(vconn_name, &vconn);
846 found = (vconn_get_version(vconn) < OFP13_VERSION
847 ? fetch_port_by_features(vconn, port_name, port_no, pp)
848 : fetch_port_by_stats(vconn, port_name, port_no, pp));
852 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
856 /* Returns the port number corresponding to 'port_name' (which may be a port
857 * name or number) within the switch 'vconn_name'. */
859 str_to_port_no(const char *vconn_name, const char *port_name)
863 if (ofputil_port_from_string(port_name, &port_no)) {
866 struct ofputil_phy_port pp;
868 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
874 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
875 enum ofputil_protocol *cur)
878 struct ofpbuf *request, *reply;
879 enum ofputil_protocol next;
881 request = ofputil_encode_set_protocol(*cur, want, &next);
886 run(vconn_transact_noreply(vconn, request, &reply),
887 "talking to %s", vconn_get_name(vconn));
889 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
890 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
891 vconn_get_name(vconn), s);
893 ofpbuf_delete(reply);
901 static enum ofputil_protocol
902 set_protocol_for_flow_dump(struct vconn *vconn,
903 enum ofputil_protocol cur_protocol,
904 enum ofputil_protocol usable_protocols)
909 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
910 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
911 if (f & usable_protocols & allowed_protocols
912 && try_set_protocol(vconn, f, &cur_protocol)) {
917 usable_s = ofputil_protocols_to_string(usable_protocols);
918 if (usable_protocols & allowed_protocols) {
919 ovs_fatal(0, "switch does not support any of the usable flow "
920 "formats (%s)", usable_s);
922 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
923 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
924 "allowed flow formats (%s)", usable_s, allowed_s);
928 static struct vconn *
929 prepare_dump_flows(int argc, char *argv[], bool aggregate,
930 struct ofpbuf **requestp)
932 enum ofputil_protocol usable_protocols, protocol;
933 struct ofputil_flow_stats_request fsr;
937 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
938 argc > 2 ? argv[2] : "",
941 ovs_fatal(0, "%s", error);
944 protocol = open_vconn(argv[1], &vconn);
945 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
946 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
951 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
953 struct ofpbuf *request;
956 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
957 dump_stats_transaction(vconn, request);
962 compare_flows(const void *afs_, const void *bfs_)
964 const struct ofputil_flow_stats *afs = afs_;
965 const struct ofputil_flow_stats *bfs = bfs_;
966 const struct match *a = &afs->match;
967 const struct match *b = &bfs->match;
968 const struct sort_criterion *sc;
970 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
971 const struct mf_field *f = sc->field;
975 unsigned int a_pri = afs->priority;
976 unsigned int b_pri = bfs->priority;
977 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
981 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
982 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
984 /* Skip the test for sc->order, so that missing fields always
985 * sort to the end whether we're sorting in ascending or
986 * descending order. */
989 union mf_value aval, bval;
991 mf_get_value(f, &a->flow, &aval);
992 mf_get_value(f, &b->flow, &bval);
993 ret = memcmp(&aval, &bval, f->n_bytes);
998 return sc->order == SORT_ASC ? ret : -ret;
1006 ofctl_dump_flows(int argc, char *argv[])
1009 return ofctl_dump_flows__(argc, argv, false);
1011 struct ofputil_flow_stats *fses;
1012 size_t n_fses, allocated_fses;
1013 struct ofpbuf *request;
1014 struct ofpbuf ofpacts;
1015 struct ofpbuf *reply;
1016 struct vconn *vconn;
1021 vconn = prepare_dump_flows(argc, argv, false, &request);
1022 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
1023 send_openflow_buffer(vconn, request);
1026 n_fses = allocated_fses = 0;
1028 ofpbuf_init(&ofpacts, 0);
1030 struct ofputil_flow_stats *fs;
1032 if (n_fses >= allocated_fses) {
1033 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1037 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1041 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1044 ofpbuf_uninit(&ofpacts);
1046 qsort(fses, n_fses, sizeof *fses, compare_flows);
1049 for (i = 0; i < n_fses; i++) {
1051 ofp_print_flow_stats(&s, &fses[i]);
1056 for (i = 0; i < n_fses; i++) {
1057 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1066 ofctl_dump_aggregate(int argc, char *argv[])
1068 return ofctl_dump_flows__(argc, argv, true);
1072 ofctl_queue_stats(int argc, char *argv[])
1074 struct ofpbuf *request;
1075 struct vconn *vconn;
1076 struct ofputil_queue_stats_request oqs;
1078 open_vconn(argv[1], &vconn);
1080 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1081 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1083 oqs.port_no = OFPP_ANY;
1085 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1086 oqs.queue_id = atoi(argv[3]);
1088 oqs.queue_id = OFPQ_ALL;
1091 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1092 dump_stats_transaction(vconn, request);
1097 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1099 const char *vconn_name = argv[1];
1100 const char *port_name = argv[2];
1101 enum ofputil_protocol protocol;
1102 enum ofp_version version;
1103 struct ofpbuf *request;
1104 struct vconn *vconn;
1107 port = str_to_port_no(vconn_name, port_name);
1109 protocol = open_vconn(vconn_name, &vconn);
1110 version = ofputil_protocol_to_ofp_version(protocol);
1111 request = ofputil_encode_queue_get_config_request(version, port);
1112 dump_transaction(vconn, request);
1116 static enum ofputil_protocol
1117 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1118 enum ofputil_protocol usable_protocols)
1120 enum ofputil_protocol cur_protocol;
1124 if (!(usable_protocols & allowed_protocols)) {
1125 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1126 usable_s = ofputil_protocols_to_string(usable_protocols);
1127 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1128 "allowed flow formats (%s)", usable_s, allowed_s);
1131 /* If the initial flow format is allowed and usable, keep it. */
1132 cur_protocol = open_vconn(remote, vconnp);
1133 if (usable_protocols & allowed_protocols & cur_protocol) {
1134 return cur_protocol;
1137 /* Otherwise try each flow format in turn. */
1138 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1139 enum ofputil_protocol f = 1 << i;
1141 if (f != cur_protocol
1142 && f & usable_protocols & allowed_protocols
1143 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1148 usable_s = ofputil_protocols_to_string(usable_protocols);
1149 ovs_fatal(0, "switch does not support any of the usable flow "
1150 "formats (%s)", usable_s);
1154 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1155 size_t n_fms, enum ofputil_protocol usable_protocols)
1157 enum ofputil_protocol protocol;
1158 struct vconn *vconn;
1161 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1163 for (i = 0; i < n_fms; i++) {
1164 struct ofputil_flow_mod *fm = &fms[i];
1166 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1167 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1173 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1175 enum ofputil_protocol usable_protocols;
1176 struct ofputil_flow_mod *fms = NULL;
1180 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1183 ovs_fatal(0, "%s", error);
1185 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1190 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1192 if (argc > 2 && !strcmp(argv[2], "-")) {
1193 ofctl_flow_mod_file(argc, argv, command);
1195 struct ofputil_flow_mod fm;
1197 enum ofputil_protocol usable_protocols;
1199 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1202 ovs_fatal(0, "%s", error);
1204 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1209 ofctl_add_flow(int argc, char *argv[])
1211 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1215 ofctl_add_flows(int argc, char *argv[])
1217 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1221 ofctl_mod_flows(int argc, char *argv[])
1223 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1227 ofctl_del_flows(int argc, char *argv[])
1229 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1233 set_packet_in_format(struct vconn *vconn,
1234 enum nx_packet_in_format packet_in_format)
1236 struct ofpbuf *spif;
1238 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1240 transact_noreply(vconn, spif);
1241 VLOG_DBG("%s: using user-specified packet in format %s",
1242 vconn_get_name(vconn),
1243 ofputil_packet_in_format_to_string(packet_in_format));
1247 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1249 struct ofp_switch_config config;
1250 enum ofp_config_flags flags;
1252 fetch_switch_config(vconn, &config);
1253 flags = ntohs(config.flags);
1254 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1255 /* Set the invalid ttl config. */
1256 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1258 config.flags = htons(flags);
1259 set_switch_config(vconn, &config);
1261 /* Then retrieve the configuration to see if it really took. OpenFlow
1262 * doesn't define error reporting for bad modes, so this is all we can
1264 fetch_switch_config(vconn, &config);
1265 flags = ntohs(config.flags);
1266 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1267 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1268 "switch probably doesn't support mode)");
1275 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1276 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1277 * an error message and stores NULL in '*msgp'. */
1279 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1281 struct ofp_header *oh;
1284 msg = ofpbuf_new(strlen(hex) / 2);
1287 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1289 return "Trailing garbage in hex data";
1292 if (ofpbuf_size(msg) < sizeof(struct ofp_header)) {
1294 return "Message too short for OpenFlow";
1297 oh = ofpbuf_data(msg);
1298 if (ofpbuf_size(msg) != ntohs(oh->length)) {
1300 return "Message size does not match length in OpenFlow header";
1308 ofctl_send(struct unixctl_conn *conn, int argc,
1309 const char *argv[], void *vconn_)
1311 struct vconn *vconn = vconn_;
1318 for (i = 1; i < argc; i++) {
1319 const char *error_msg;
1323 error_msg = openflow_from_hex(argv[i], &msg);
1325 ds_put_format(&reply, "%s\n", error_msg);
1330 fprintf(stderr, "send: ");
1331 ofp_print(stderr, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
1333 error = vconn_send_block(vconn, msg);
1336 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1339 ds_put_cstr(&reply, "sent\n");
1344 unixctl_command_reply(conn, ds_cstr(&reply));
1346 unixctl_command_reply_error(conn, ds_cstr(&reply));
1351 struct barrier_aux {
1352 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1353 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1357 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1358 const char *argv[] OVS_UNUSED, void *aux_)
1360 struct barrier_aux *aux = aux_;
1365 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1369 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1370 error = vconn_send_block(aux->vconn, msg);
1373 unixctl_command_reply_error(conn, ovs_strerror(error));
1380 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1381 const char *argv[], void *aux OVS_UNUSED)
1385 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1387 unixctl_command_reply_error(conn, ovs_strerror(errno));
1392 dup2(fd, STDERR_FILENO);
1394 unixctl_command_reply(conn, NULL);
1398 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1399 const char *argv[] OVS_UNUSED, void *blocked_)
1401 bool *blocked = blocked_;
1405 unixctl_command_reply(conn, NULL);
1407 unixctl_command_reply(conn, "already blocking");
1412 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1413 const char *argv[] OVS_UNUSED, void *blocked_)
1415 bool *blocked = blocked_;
1419 unixctl_command_reply(conn, NULL);
1421 unixctl_command_reply(conn, "already unblocked");
1425 /* Prints to stdout all of the messages received on 'vconn'.
1427 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1428 * received on 'vconn'. */
1430 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1432 struct barrier_aux barrier_aux = { vconn, NULL };
1433 struct unixctl_server *server;
1434 bool exiting = false;
1435 bool blocked = false;
1438 daemon_save_fd(STDERR_FILENO);
1440 error = unixctl_server_create(NULL, &server);
1442 ovs_fatal(error, "failed to create unixctl server");
1444 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1445 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1447 unixctl_command_register("ofctl/barrier", "", 0, 0,
1448 ofctl_barrier, &barrier_aux);
1449 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1450 ofctl_set_output_file, NULL);
1452 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1453 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1456 daemonize_complete();
1462 unixctl_server_run(server);
1467 retval = vconn_recv(vconn, &b);
1468 if (retval == EAGAIN) {
1471 run(retval, "vconn_recv");
1474 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1475 time_wall_msec(), true);
1480 ofptype_decode(&type, ofpbuf_data(b));
1481 ofp_print(stderr, ofpbuf_data(b), ofpbuf_size(b), verbosity + 2);
1483 switch ((int) type) {
1484 case OFPTYPE_BARRIER_REPLY:
1485 if (barrier_aux.conn) {
1486 unixctl_command_reply(barrier_aux.conn, NULL);
1487 barrier_aux.conn = NULL;
1491 case OFPTYPE_ECHO_REQUEST:
1492 if (reply_to_echo_requests) {
1493 struct ofpbuf *reply;
1495 reply = make_echo_reply(ofpbuf_data(b));
1496 retval = vconn_send_block(vconn, reply);
1498 ovs_fatal(retval, "failed to send echo reply");
1511 vconn_run_wait(vconn);
1513 vconn_recv_wait(vconn);
1515 unixctl_server_wait(server);
1519 unixctl_server_destroy(server);
1523 ofctl_monitor(int argc, char *argv[])
1525 struct vconn *vconn;
1527 enum ofputil_protocol usable_protocols;
1529 open_vconn(argv[1], &vconn);
1530 for (i = 2; i < argc; i++) {
1531 const char *arg = argv[i];
1533 if (isdigit((unsigned char) *arg)) {
1534 struct ofp_switch_config config;
1536 fetch_switch_config(vconn, &config);
1537 config.miss_send_len = htons(atoi(arg));
1538 set_switch_config(vconn, &config);
1539 } else if (!strcmp(arg, "invalid_ttl")) {
1540 monitor_set_invalid_ttl_to_controller(vconn);
1541 } else if (!strncmp(arg, "watch:", 6)) {
1542 struct ofputil_flow_monitor_request fmr;
1546 error = parse_flow_monitor_request(&fmr, arg + 6,
1549 ovs_fatal(0, "%s", error);
1552 msg = ofpbuf_new(0);
1553 ofputil_append_flow_monitor_request(&fmr, msg);
1554 dump_stats_transaction(vconn, msg);
1556 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1560 if (preferred_packet_in_format >= 0) {
1561 set_packet_in_format(vconn, preferred_packet_in_format);
1563 enum ofp_version version = vconn_get_version(vconn);
1566 case OFP10_VERSION: {
1567 struct ofpbuf *spif, *reply;
1569 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1571 run(vconn_transact_noreply(vconn, spif, &reply),
1572 "talking to %s", vconn_get_name(vconn));
1574 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
1575 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1576 " replied: %s. Falling back to the switch default.",
1577 vconn_get_name(vconn), s);
1579 ofpbuf_delete(reply);
1594 monitor_vconn(vconn, true);
1598 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1600 struct vconn *vconn;
1602 open_vconn__(argv[1], SNOOP, &vconn);
1603 monitor_vconn(vconn, false);
1607 ofctl_dump_ports(int argc, char *argv[])
1609 struct ofpbuf *request;
1610 struct vconn *vconn;
1613 open_vconn(argv[1], &vconn);
1614 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1615 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1616 dump_stats_transaction(vconn, request);
1621 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1623 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1627 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1629 struct ofpbuf *request;
1630 struct vconn *vconn;
1631 struct ofpbuf *reply;
1633 open_vconn(argv[1], &vconn);
1634 request = make_echo_request(vconn_get_version(vconn));
1635 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1636 if (ofpbuf_size(reply) != sizeof(struct ofp_header)) {
1637 ovs_fatal(0, "reply does not match request");
1639 ofpbuf_delete(reply);
1644 ofctl_packet_out(int argc, char *argv[])
1646 enum ofputil_protocol protocol;
1647 struct ofputil_packet_out po;
1648 struct ofpbuf ofpacts;
1649 struct vconn *vconn;
1652 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1654 ofpbuf_init(&ofpacts, 64);
1655 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1657 ovs_fatal(0, "%s", error);
1660 po.buffer_id = UINT32_MAX;
1661 po.in_port = str_to_port_no(argv[1], argv[2]);
1662 po.ofpacts = ofpbuf_data(&ofpacts);
1663 po.ofpacts_len = ofpbuf_size(&ofpacts);
1665 protocol = open_vconn(argv[1], &vconn);
1666 for (i = 4; i < argc; i++) {
1667 struct ofpbuf *packet, *opo;
1668 const char *error_msg;
1670 error_msg = eth_from_hex(argv[i], &packet);
1672 ovs_fatal(0, "%s", error_msg);
1675 po.packet = ofpbuf_data(packet);
1676 po.packet_len = ofpbuf_size(packet);
1677 opo = ofputil_encode_packet_out(&po, protocol);
1678 transact_noreply(vconn, opo);
1679 ofpbuf_delete(packet);
1682 ofpbuf_uninit(&ofpacts);
1686 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1688 struct ofp_config_flag {
1689 const char *name; /* The flag's name. */
1690 enum ofputil_port_config bit; /* Bit to turn on or off. */
1691 bool on; /* Value to set the bit to. */
1693 static const struct ofp_config_flag flags[] = {
1694 { "up", OFPUTIL_PC_PORT_DOWN, false },
1695 { "down", OFPUTIL_PC_PORT_DOWN, true },
1696 { "stp", OFPUTIL_PC_NO_STP, false },
1697 { "receive", OFPUTIL_PC_NO_RECV, false },
1698 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1699 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1700 { "forward", OFPUTIL_PC_NO_FWD, false },
1701 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1704 const struct ofp_config_flag *flag;
1705 enum ofputil_protocol protocol;
1706 struct ofputil_port_mod pm;
1707 struct ofputil_phy_port pp;
1708 struct vconn *vconn;
1709 const char *command;
1712 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1714 pm.port_no = pp.port_no;
1715 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1720 if (!strncasecmp(argv[3], "no-", 3)) {
1721 command = argv[3] + 3;
1723 } else if (!strncasecmp(argv[3], "no", 2)) {
1724 command = argv[3] + 2;
1730 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1731 if (!strcasecmp(command, flag->name)) {
1732 pm.mask = flag->bit;
1733 pm.config = flag->on ^ not ? flag->bit : 0;
1737 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1740 protocol = open_vconn(argv[1], &vconn);
1741 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1746 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1748 enum ofputil_protocol protocol, usable_protocols;
1749 struct ofputil_table_mod tm;
1750 struct vconn *vconn;
1754 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1756 ovs_fatal(0, "%s", error);
1759 protocol = open_vconn(argv[1], &vconn);
1760 if (!(protocol & usable_protocols)) {
1761 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1762 enum ofputil_protocol f = 1 << i;
1764 && f & usable_protocols
1765 && try_set_protocol(vconn, f, &protocol)) {
1772 if (!(protocol & usable_protocols)) {
1773 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1774 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1777 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1782 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1784 struct ofp_switch_config config;
1785 struct vconn *vconn;
1787 open_vconn(argv[1], &vconn);
1788 fetch_switch_config(vconn, &config);
1789 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1794 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1796 struct ofp_switch_config config;
1797 enum ofp_config_flags mode;
1798 struct vconn *vconn;
1801 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1802 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1805 open_vconn(argv[1], &vconn);
1806 fetch_switch_config(vconn, &config);
1807 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1808 if (flags != config.flags) {
1809 /* Set the configuration. */
1810 config.flags = flags;
1811 set_switch_config(vconn, &config);
1813 /* Then retrieve the configuration to see if it really took. OpenFlow
1814 * doesn't define error reporting for bad modes, so this is all we can
1816 fetch_switch_config(vconn, &config);
1817 if (flags != config.flags) {
1818 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1819 "switch probably doesn't support mode \"%s\")",
1820 argv[1], ofputil_frag_handling_to_string(mode));
1827 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1829 const char *filename = argv[1];
1833 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1835 ovs_fatal(errno, "%s: open", filename);
1838 ofpbuf_init(&b, 65536);
1840 struct ofp_header *oh;
1841 size_t length, tail_len;
1846 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1847 n = fread(oh, 1, sizeof *oh, file);
1850 } else if (n < sizeof *oh) {
1851 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1854 length = ntohs(oh->length);
1855 if (length < sizeof *oh) {
1856 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1860 tail_len = length - sizeof *oh;
1861 tail = ofpbuf_put_uninit(&b, tail_len);
1862 n = fread(tail, 1, tail_len, file);
1864 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1867 ofp_print(stdout, ofpbuf_data(&b), ofpbuf_size(&b), verbosity + 2);
1871 if (file != stdin) {
1877 is_openflow_port(ovs_be16 port_, char *ports[])
1879 uint16_t port = ntohs(port_);
1883 for (i = 0; ports[i]; i++) {
1884 if (port == atoi(ports[i])) {
1890 return port == OFP_PORT || port == OFP_OLD_PORT;
1895 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1897 struct tcp_reader *reader;
1902 file = ovs_pcap_open(argv[1], "rb");
1904 ovs_fatal(errno, "%s: open failed", argv[1]);
1907 reader = tcp_reader_open();
1910 struct ofpbuf *packet;
1913 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1915 error = ovs_pcap_read(file, &packet, &when);
1919 flow_extract(packet, &md, &flow);
1920 if (flow.dl_type == htons(ETH_TYPE_IP)
1921 && flow.nw_proto == IPPROTO_TCP
1922 && (is_openflow_port(flow.tp_src, argv + 2) ||
1923 is_openflow_port(flow.tp_dst, argv + 2))) {
1924 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1926 while (ofpbuf_size(payload) >= sizeof(struct ofp_header)) {
1927 const struct ofp_header *oh;
1928 void *data = ofpbuf_data(payload);
1931 /* Align OpenFlow on 8-byte boundary for safe access. */
1932 ofpbuf_shift(payload, -((intptr_t) data & 7));
1934 oh = ofpbuf_data(payload);
1935 length = ntohs(oh->length);
1936 if (ofpbuf_size(payload) < length) {
1946 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1951 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1952 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1953 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1954 ofp_print(stdout, ofpbuf_data(payload), length, verbosity + 1);
1955 ofpbuf_pull(payload, length);
1959 ofpbuf_delete(packet);
1961 tcp_reader_close(reader);
1965 ofctl_ping(int argc, char *argv[])
1967 size_t max_payload = 65535 - sizeof(struct ofp_header);
1968 unsigned int payload;
1969 struct vconn *vconn;
1972 payload = argc > 2 ? atoi(argv[2]) : 64;
1973 if (payload > max_payload) {
1974 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1977 open_vconn(argv[1], &vconn);
1978 for (i = 0; i < 10; i++) {
1979 struct timeval start, end;
1980 struct ofpbuf *request, *reply;
1981 const struct ofp_header *rpy_hdr;
1984 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1985 vconn_get_version(vconn), payload);
1986 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1988 xgettimeofday(&start);
1989 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1990 xgettimeofday(&end);
1992 rpy_hdr = ofpbuf_data(reply);
1993 if (ofptype_pull(&type, reply)
1994 || type != OFPTYPE_ECHO_REPLY
1995 || ofpbuf_size(reply) != payload
1996 || memcmp(ofpbuf_l3(request), ofpbuf_l3(reply), payload)) {
1997 printf("Reply does not match request. Request:\n");
1998 ofp_print(stdout, request, ofpbuf_size(request), verbosity + 2);
2000 ofp_print(stdout, reply, ofpbuf_size(reply), verbosity + 2);
2002 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2003 ofpbuf_size(reply), argv[1], ntohl(rpy_hdr->xid),
2004 (1000*(double)(end.tv_sec - start.tv_sec))
2005 + (.001*(end.tv_usec - start.tv_usec)));
2006 ofpbuf_delete(request);
2007 ofpbuf_delete(reply);
2013 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
2015 size_t max_payload = 65535 - sizeof(struct ofp_header);
2016 struct timeval start, end;
2017 unsigned int payload_size, message_size;
2018 struct vconn *vconn;
2023 payload_size = atoi(argv[2]);
2024 if (payload_size > max_payload) {
2025 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2027 message_size = sizeof(struct ofp_header) + payload_size;
2029 count = atoi(argv[3]);
2031 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2032 count, message_size, count * message_size);
2034 open_vconn(argv[1], &vconn);
2035 xgettimeofday(&start);
2036 for (i = 0; i < count; i++) {
2037 struct ofpbuf *request, *reply;
2039 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2040 vconn_get_version(vconn), payload_size);
2041 ofpbuf_put_zeros(request, payload_size);
2042 run(vconn_transact(vconn, request, &reply), "transact");
2043 ofpbuf_delete(reply);
2045 xgettimeofday(&end);
2048 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2049 + (.001*(end.tv_usec - start.tv_usec)));
2050 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2051 duration, count / (duration / 1000.0),
2052 count * message_size / (duration / 1000.0));
2056 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2059 struct ofputil_group_mod *gm;
2060 struct ofpbuf *request;
2062 struct vconn *vconn;
2065 open_vconn(remote, &vconn);
2067 for (i = 0; i < n_gms; i++) {
2069 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2071 transact_noreply(vconn, request);
2081 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2083 struct ofputil_group_mod *gms = NULL;
2084 enum ofputil_protocol usable_protocols;
2088 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2091 ovs_fatal(0, "%s", error);
2093 ofctl_group_mod__(argv[1], gms, n_gms);
2098 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2100 if (argc > 2 && !strcmp(argv[2], "-")) {
2101 ofctl_group_mod_file(argc, argv, command);
2103 enum ofputil_protocol usable_protocols;
2104 struct ofputil_group_mod gm;
2107 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2110 ovs_fatal(0, "%s", error);
2112 ofctl_group_mod__(argv[1], &gm, 1);
2117 ofctl_add_group(int argc, char *argv[])
2119 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2123 ofctl_add_groups(int argc, char *argv[])
2125 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2129 ofctl_mod_group(int argc, char *argv[])
2131 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2135 ofctl_del_groups(int argc, char *argv[])
2137 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2141 ofctl_dump_group_stats(int argc, char *argv[])
2143 enum ofputil_protocol usable_protocols;
2144 struct ofputil_group_mod gm;
2145 struct ofpbuf *request;
2146 struct vconn *vconn;
2150 memset(&gm, 0, sizeof gm);
2152 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2153 argc > 2 ? argv[2] : "",
2156 ovs_fatal(0, "%s", error);
2159 group_id = gm.group_id;
2161 open_vconn(argv[1], &vconn);
2162 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2165 dump_stats_transaction(vconn, request);
2172 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2174 struct ofpbuf *request;
2175 struct vconn *vconn;
2178 open_vconn(argv[1], &vconn);
2180 if (argc < 3 || !ofputil_group_from_string(argv[2], &group_id)) {
2181 group_id = OFPG11_ALL;
2184 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2187 dump_stats_transaction(vconn, request);
2194 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2196 struct ofpbuf *request;
2197 struct vconn *vconn;
2199 open_vconn(argv[1], &vconn);
2200 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2202 dump_stats_transaction(vconn, request);
2209 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2214 /* replace-flows and diff-flows commands. */
2216 /* A flow table entry, possibly with two different versions. */
2218 struct cls_rule rule; /* Within a "struct classifier". */
2219 struct fte_version *versions[2];
2222 /* One version of a Flow Table Entry. */
2223 struct fte_version {
2225 uint16_t idle_timeout;
2226 uint16_t hard_timeout;
2228 struct ofpact *ofpacts;
2232 /* Frees 'version' and the data that it owns. */
2234 fte_version_free(struct fte_version *version)
2237 free(CONST_CAST(struct ofpact *, version->ofpacts));
2242 /* Returns true if 'a' and 'b' are the same, false if they differ.
2244 * Ignores differences in 'flags' because there's no way to retrieve flags from
2245 * an OpenFlow switch. We have to assume that they are the same. */
2247 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2249 return (a->cookie == b->cookie
2250 && a->idle_timeout == b->idle_timeout
2251 && a->hard_timeout == b->hard_timeout
2252 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2253 b->ofpacts, b->ofpacts_len));
2256 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2257 * 'index' into 's', followed by a new-line. */
2259 fte_version_format(const struct fte *fte, int index, struct ds *s)
2261 const struct fte_version *version = fte->versions[index];
2268 cls_rule_format(&fte->rule, s);
2269 if (version->cookie != htonll(0)) {
2270 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2272 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2273 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2275 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2276 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2279 ds_put_cstr(s, " actions=");
2280 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2282 ds_put_char(s, '\n');
2286 fte_from_cls_rule(const struct cls_rule *cls_rule)
2288 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2291 /* Frees 'fte' and its versions. */
2293 fte_free(struct fte *fte)
2296 fte_version_free(fte->versions[0]);
2297 fte_version_free(fte->versions[1]);
2298 cls_rule_destroy(&fte->rule);
2303 /* Frees all of the FTEs within 'cls'. */
2305 fte_free_all(struct classifier *cls)
2307 struct cls_cursor cursor;
2308 struct fte *fte, *next;
2310 fat_rwlock_wrlock(&cls->rwlock);
2311 cls_cursor_init(&cursor, cls, NULL);
2312 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2313 classifier_remove(cls, &fte->rule);
2316 fat_rwlock_unlock(&cls->rwlock);
2317 classifier_destroy(cls);
2320 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2321 * necessary. Sets 'version' as the version of that rule with the given
2322 * 'index', replacing any existing version, if any.
2324 * Takes ownership of 'version'. */
2326 fte_insert(struct classifier *cls, const struct match *match,
2327 unsigned int priority, struct fte_version *version, int index)
2329 struct fte *old, *fte;
2331 fte = xzalloc(sizeof *fte);
2332 cls_rule_init(&fte->rule, match, priority);
2333 fte->versions[index] = version;
2335 fat_rwlock_wrlock(&cls->rwlock);
2336 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2337 fat_rwlock_unlock(&cls->rwlock);
2339 fte_version_free(old->versions[index]);
2340 fte->versions[!index] = old->versions[!index];
2341 cls_rule_destroy(&old->rule);
2346 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2347 * with the specified 'index'. Returns the flow formats able to represent the
2348 * flows that were read. */
2349 static enum ofputil_protocol
2350 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2352 enum ofputil_protocol usable_protocols;
2357 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2359 ovs_fatal(errno, "%s: open", filename);
2363 usable_protocols = OFPUTIL_P_ANY;
2365 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2366 struct fte_version *version;
2367 struct ofputil_flow_mod fm;
2369 enum ofputil_protocol usable;
2371 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2373 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2375 usable_protocols &= usable;
2377 version = xmalloc(sizeof *version);
2378 version->cookie = fm.new_cookie;
2379 version->idle_timeout = fm.idle_timeout;
2380 version->hard_timeout = fm.hard_timeout;
2381 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2382 | OFPUTIL_FF_EMERG);
2383 version->ofpacts = fm.ofpacts;
2384 version->ofpacts_len = fm.ofpacts_len;
2386 fte_insert(cls, &fm.match, fm.priority, version, index);
2390 if (file != stdin) {
2394 return usable_protocols;
2398 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2399 struct ofpbuf **replyp,
2400 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2402 struct ofpbuf *reply = *replyp;
2408 /* Get a flow stats reply message, if we don't already have one. */
2414 run(vconn_recv_block(vconn, &reply),
2415 "OpenFlow packet receive failed");
2416 } while (((struct ofp_header *) ofpbuf_data(reply))->xid != send_xid);
2418 error = ofptype_decode(&type, ofpbuf_data(reply));
2419 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2420 ovs_fatal(0, "received bad reply: %s",
2421 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
2426 /* Pull an individual flow stats reply out of the message. */
2427 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2434 more = ofpmp_more(reply->frame);
2435 ofpbuf_delete(reply);
2444 ovs_fatal(0, "parse error in reply (%s)",
2445 ofperr_to_string(retval));
2450 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2451 * format 'protocol', and adds them as flow table entries in 'cls' for the
2452 * version with the specified 'index'. */
2454 read_flows_from_switch(struct vconn *vconn,
2455 enum ofputil_protocol protocol,
2456 struct classifier *cls, int index)
2458 struct ofputil_flow_stats_request fsr;
2459 struct ofputil_flow_stats fs;
2460 struct ofpbuf *request;
2461 struct ofpbuf ofpacts;
2462 struct ofpbuf *reply;
2465 fsr.aggregate = false;
2466 match_init_catchall(&fsr.match);
2467 fsr.out_port = OFPP_ANY;
2468 fsr.table_id = 0xff;
2469 fsr.cookie = fsr.cookie_mask = htonll(0);
2470 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2471 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
2472 send_openflow_buffer(vconn, request);
2475 ofpbuf_init(&ofpacts, 0);
2476 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2477 struct fte_version *version;
2479 version = xmalloc(sizeof *version);
2480 version->cookie = fs.cookie;
2481 version->idle_timeout = fs.idle_timeout;
2482 version->hard_timeout = fs.hard_timeout;
2484 version->ofpacts_len = fs.ofpacts_len;
2485 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2487 fte_insert(cls, &fs.match, fs.priority, version, index);
2489 ofpbuf_uninit(&ofpacts);
2493 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2494 enum ofputil_protocol protocol, struct list *packets)
2496 const struct fte_version *version = fte->versions[index];
2497 struct ofputil_flow_mod fm;
2500 minimatch_expand(&fte->rule.match, &fm.match);
2501 fm.priority = fte->rule.priority;
2502 fm.cookie = htonll(0);
2503 fm.cookie_mask = htonll(0);
2504 fm.new_cookie = version->cookie;
2505 fm.modify_cookie = true;
2507 fm.command = command;
2508 fm.idle_timeout = version->idle_timeout;
2509 fm.hard_timeout = version->hard_timeout;
2510 fm.buffer_id = UINT32_MAX;
2511 fm.out_port = OFPP_ANY;
2512 fm.flags = version->flags;
2513 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2514 command == OFPFC_MODIFY_STRICT) {
2515 fm.ofpacts = version->ofpacts;
2516 fm.ofpacts_len = version->ofpacts_len;
2522 ofm = ofputil_encode_flow_mod(&fm, protocol);
2523 list_push_back(packets, &ofm->list_node);
2527 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2529 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2530 enum ofputil_protocol usable_protocols, protocol;
2531 struct cls_cursor cursor;
2532 struct classifier cls;
2533 struct list requests;
2534 struct vconn *vconn;
2537 classifier_init(&cls, NULL);
2538 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2540 protocol = open_vconn(argv[1], &vconn);
2541 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2543 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2545 list_init(&requests);
2547 /* Delete flows that exist on the switch but not in the file. */
2548 fat_rwlock_rdlock(&cls.rwlock);
2549 cls_cursor_init(&cursor, &cls, NULL);
2550 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2551 struct fte_version *file_ver = fte->versions[FILE_IDX];
2552 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2554 if (sw_ver && !file_ver) {
2555 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2556 protocol, &requests);
2560 /* Add flows that exist in the file but not on the switch.
2561 * Update flows that exist in both places but differ. */
2562 cls_cursor_init(&cursor, &cls, NULL);
2563 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2564 struct fte_version *file_ver = fte->versions[FILE_IDX];
2565 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2568 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2569 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2572 fat_rwlock_unlock(&cls.rwlock);
2573 transact_multiple_noreply(vconn, &requests);
2580 read_flows_from_source(const char *source, struct classifier *cls, int index)
2584 if (source[0] == '/' || source[0] == '.'
2585 || (!strchr(source, ':') && !stat(source, &s))) {
2586 read_flows_from_file(source, cls, index);
2588 enum ofputil_protocol protocol;
2589 struct vconn *vconn;
2591 protocol = open_vconn(source, &vconn);
2592 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2593 read_flows_from_switch(vconn, protocol, cls, index);
2599 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2601 bool differences = false;
2602 struct cls_cursor cursor;
2603 struct classifier cls;
2607 classifier_init(&cls, NULL);
2608 read_flows_from_source(argv[1], &cls, 0);
2609 read_flows_from_source(argv[2], &cls, 1);
2614 fat_rwlock_rdlock(&cls.rwlock);
2615 cls_cursor_init(&cursor, &cls, NULL);
2616 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2617 struct fte_version *a = fte->versions[0];
2618 struct fte_version *b = fte->versions[1];
2620 if (!a || !b || !fte_version_equals(a, b)) {
2621 fte_version_format(fte, 0, &a_s);
2622 fte_version_format(fte, 1, &b_s);
2623 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2625 printf("-%s", ds_cstr(&a_s));
2628 printf("+%s", ds_cstr(&b_s));
2634 fat_rwlock_unlock(&cls.rwlock);
2647 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2649 struct ofputil_meter_mod mm;
2650 struct vconn *vconn;
2651 enum ofputil_protocol protocol;
2652 enum ofputil_protocol usable_protocols;
2653 enum ofp_version version;
2657 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2659 ovs_fatal(0, "%s", error);
2662 usable_protocols = OFPUTIL_P_OF13_UP;
2663 mm.command = command;
2664 mm.meter.meter_id = OFPM13_ALL;
2667 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2668 version = ofputil_protocol_to_ofp_version(protocol);
2669 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2674 ofctl_meter_request__(const char *bridge, const char *str,
2675 enum ofputil_meter_request_type type)
2677 struct ofputil_meter_mod mm;
2678 struct vconn *vconn;
2679 enum ofputil_protocol usable_protocols;
2680 enum ofputil_protocol protocol;
2681 enum ofp_version version;
2685 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2687 ovs_fatal(0, "%s", error);
2690 usable_protocols = OFPUTIL_P_OF13_UP;
2691 mm.meter.meter_id = OFPM13_ALL;
2694 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2695 version = ofputil_protocol_to_ofp_version(protocol);
2696 transact_noreply(vconn, ofputil_encode_meter_request(version,
2698 mm.meter.meter_id));
2704 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2706 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2710 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2712 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2716 ofctl_del_meters(int argc, char *argv[])
2718 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2722 ofctl_dump_meters(int argc, char *argv[])
2724 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2725 OFPUTIL_METER_CONFIG);
2729 ofctl_meter_stats(int argc, char *argv[])
2731 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2732 OFPUTIL_METER_STATS);
2736 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2738 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2742 /* Undocumented commands for unit testing. */
2745 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2746 enum ofputil_protocol usable_protocols)
2748 enum ofputil_protocol protocol = 0;
2752 usable_s = ofputil_protocols_to_string(usable_protocols);
2753 printf("usable protocols: %s\n", usable_s);
2756 if (!(usable_protocols & allowed_protocols)) {
2757 ovs_fatal(0, "no usable protocol");
2759 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2761 if (protocol & usable_protocols & allowed_protocols) {
2765 ovs_assert(is_pow2(protocol));
2767 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2769 for (i = 0; i < n_fms; i++) {
2770 struct ofputil_flow_mod *fm = &fms[i];
2773 msg = ofputil_encode_flow_mod(fm, protocol);
2774 ofp_print(stdout, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
2777 free(CONST_CAST(struct ofpact *, fm->ofpacts));
2781 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2782 * it back to stdout. */
2784 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2786 enum ofputil_protocol usable_protocols;
2787 struct ofputil_flow_mod fm;
2790 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2792 ovs_fatal(0, "%s", error);
2794 ofctl_parse_flows__(&fm, 1, usable_protocols);
2797 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2798 * add-flows) and prints each of the flows back to stdout. */
2800 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2802 enum ofputil_protocol usable_protocols;
2803 struct ofputil_flow_mod *fms = NULL;
2807 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2810 ovs_fatal(0, "%s", error);
2812 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2817 ofctl_parse_nxm__(bool oxm)
2822 while (!ds_get_test_line(&in, stdin)) {
2823 struct ofpbuf nx_match;
2825 ovs_be64 cookie, cookie_mask;
2829 /* Convert string to nx_match. */
2830 ofpbuf_init(&nx_match, 0);
2832 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2834 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2837 /* Convert nx_match to match. */
2840 error = oxm_pull_match(&nx_match, &match);
2842 error = nx_pull_match(&nx_match, match_len, &match,
2843 &cookie, &cookie_mask);
2847 error = oxm_pull_match_loose(&nx_match, &match);
2849 error = nx_pull_match_loose(&nx_match, match_len, &match,
2850 &cookie, &cookie_mask);
2858 /* Convert match back to nx_match. */
2859 ofpbuf_uninit(&nx_match);
2860 ofpbuf_init(&nx_match, 0);
2862 match_len = oxm_put_match(&nx_match, &match);
2863 out = oxm_match_to_string(&nx_match, match_len);
2865 match_len = nx_put_match(&nx_match, &match,
2866 cookie, cookie_mask);
2867 out = nx_match_to_string(ofpbuf_data(&nx_match), match_len);
2873 printf("nx_pull_match() returned error %s\n",
2874 ofperr_get_name(error));
2877 ofpbuf_uninit(&nx_match);
2882 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2883 * stdin, does some internal fussing with them, and then prints them back as
2884 * strings on stdout. */
2886 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2888 return ofctl_parse_nxm__(false);
2891 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2892 * stdin, does some internal fussing with them, and then prints them back as
2893 * strings on stdout. */
2895 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2897 return ofctl_parse_nxm__(true);
2901 print_differences(const char *prefix,
2902 const void *a_, size_t a_len,
2903 const void *b_, size_t b_len)
2905 const uint8_t *a = a_;
2906 const uint8_t *b = b_;
2909 for (i = 0; i < MIN(a_len, b_len); i++) {
2911 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2912 prefix, i, a[i], b[i]);
2915 for (i = a_len; i < b_len; i++) {
2916 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2918 for (i = b_len; i < a_len; i++) {
2919 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2923 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2924 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2925 * on stdout, and then converts them back to hex bytes and prints any
2926 * differences from the input. */
2928 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2933 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2934 struct ofpbuf of10_out;
2935 struct ofpbuf of10_in;
2936 struct ofpbuf ofpacts;
2941 /* Parse hex bytes. */
2942 ofpbuf_init(&of10_in, 0);
2943 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2944 ovs_fatal(0, "Trailing garbage in hex data");
2947 /* Convert to ofpacts. */
2948 ofpbuf_init(&ofpacts, 0);
2949 size = ofpbuf_size(&of10_in);
2950 error = ofpacts_pull_openflow_actions(&of10_in, ofpbuf_size(&of10_in),
2951 OFP10_VERSION, &ofpacts);
2953 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2954 ofpbuf_uninit(&ofpacts);
2955 ofpbuf_uninit(&of10_in);
2958 ofpbuf_push_uninit(&of10_in, size);
2960 /* Print cls_rule. */
2962 ds_put_cstr(&s, "actions=");
2963 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
2967 /* Convert back to ofp10 actions and print differences from input. */
2968 ofpbuf_init(&of10_out, 0);
2969 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of10_out,
2972 print_differences("", ofpbuf_data(&of10_in), ofpbuf_size(&of10_in),
2973 ofpbuf_data(&of10_out), ofpbuf_size(&of10_out));
2976 ofpbuf_uninit(&ofpacts);
2977 ofpbuf_uninit(&of10_in);
2978 ofpbuf_uninit(&of10_out);
2983 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2984 * bytes from stdin, converts them to cls_rules, prints them as strings on
2985 * stdout, and then converts them back to hex bytes and prints any differences
2988 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2989 * values are ignored in the input and will be set to zero when OVS converts
2990 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2991 * it does the conversion to hex, to ensure that in fact they are ignored. */
2993 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3000 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3001 struct ofpbuf match_in, match_expout;
3002 struct ofp10_match match_out;
3003 struct ofp10_match match_normal;
3007 /* Parse hex bytes to use for expected output. */
3009 ds_put_cstr(&expout, ds_cstr(&in));
3010 for (p = ds_cstr(&expout); *p; p++) {
3015 ofpbuf_init(&match_expout, 0);
3016 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3017 ovs_fatal(0, "Trailing garbage in hex data");
3019 if (ofpbuf_size(&match_expout) != sizeof(struct ofp10_match)) {
3020 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3021 ofpbuf_size(&match_expout), sizeof(struct ofp10_match));
3024 /* Parse hex bytes for input. */
3025 for (p = ds_cstr(&in); *p; p++) {
3027 *p = "0123456789abcdef"[random_uint32() & 0xf];
3030 ofpbuf_init(&match_in, 0);
3031 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3032 ovs_fatal(0, "Trailing garbage in hex data");
3034 if (ofpbuf_size(&match_in) != sizeof(struct ofp10_match)) {
3035 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3036 ofpbuf_size(&match_in), sizeof(struct ofp10_match));
3039 /* Convert to cls_rule and print. */
3040 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in), &match);
3041 match_print(&match);
3043 /* Convert back to ofp10_match and print differences from input. */
3044 ofputil_match_to_ofp10_match(&match, &match_out);
3045 print_differences("", ofpbuf_data(&match_expout), ofpbuf_size(&match_expout),
3046 &match_out, sizeof match_out);
3048 /* Normalize, then convert and compare again. */
3049 ofputil_normalize_match(&match);
3050 ofputil_match_to_ofp10_match(&match, &match_normal);
3051 print_differences("normal: ", &match_out, sizeof match_out,
3052 &match_normal, sizeof match_normal);
3055 ofpbuf_uninit(&match_in);
3056 ofpbuf_uninit(&match_expout);
3059 ds_destroy(&expout);
3062 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3063 * bytes from stdin, converts them to "struct match"es, prints them as strings
3064 * on stdout, and then converts them back to hex bytes and prints any
3065 * differences from the input. */
3067 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3072 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3073 struct ofpbuf match_in;
3074 struct ofp11_match match_out;
3078 /* Parse hex bytes. */
3079 ofpbuf_init(&match_in, 0);
3080 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3081 ovs_fatal(0, "Trailing garbage in hex data");
3083 if (ofpbuf_size(&match_in) != sizeof(struct ofp11_match)) {
3084 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3085 ofpbuf_size(&match_in), sizeof(struct ofp11_match));
3088 /* Convert to match. */
3089 error = ofputil_match_from_ofp11_match(ofpbuf_data(&match_in), &match);
3091 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3092 ofpbuf_uninit(&match_in);
3097 match_print(&match);
3099 /* Convert back to ofp11_match and print differences from input. */
3100 ofputil_match_to_ofp11_match(&match, &match_out);
3102 print_differences("", ofpbuf_data(&match_in), ofpbuf_size(&match_in),
3103 &match_out, sizeof match_out);
3106 ofpbuf_uninit(&match_in);
3111 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3112 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3113 * on stdout, and then converts them back to hex bytes and prints any
3114 * differences from the input. */
3116 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3121 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3122 struct ofpbuf of11_out;
3123 struct ofpbuf of11_in;
3124 struct ofpbuf ofpacts;
3129 /* Parse hex bytes. */
3130 ofpbuf_init(&of11_in, 0);
3131 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3132 ovs_fatal(0, "Trailing garbage in hex data");
3135 /* Convert to ofpacts. */
3136 ofpbuf_init(&ofpacts, 0);
3137 size = ofpbuf_size(&of11_in);
3138 error = ofpacts_pull_openflow_actions(&of11_in, ofpbuf_size(&of11_in),
3139 OFP11_VERSION, &ofpacts);
3141 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3142 ofpbuf_uninit(&ofpacts);
3143 ofpbuf_uninit(&of11_in);
3146 ofpbuf_push_uninit(&of11_in, size);
3148 /* Print cls_rule. */
3150 ds_put_cstr(&s, "actions=");
3151 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3155 /* Convert back to ofp11 actions and print differences from input. */
3156 ofpbuf_init(&of11_out, 0);
3157 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of11_out,
3160 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3161 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3164 ofpbuf_uninit(&ofpacts);
3165 ofpbuf_uninit(&of11_in);
3166 ofpbuf_uninit(&of11_out);
3171 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3172 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3173 * them as strings on stdout, and then converts them back to hex bytes and
3174 * prints any differences from the input. */
3176 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3181 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3182 struct ofpbuf of11_out;
3183 struct ofpbuf of11_in;
3184 struct ofpbuf ofpacts;
3188 const char *table_id;
3191 /* Parse table_id separated with the follow-up instructions by ",", if
3193 instructions = ds_cstr(&in);
3195 if (strstr(instructions, ",")) {
3196 table_id = strsep(&instructions, ",");
3199 /* Parse hex bytes. */
3200 ofpbuf_init(&of11_in, 0);
3201 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3202 ovs_fatal(0, "Trailing garbage in hex data");
3205 /* Convert to ofpacts. */
3206 ofpbuf_init(&ofpacts, 0);
3207 size = ofpbuf_size(&of11_in);
3208 error = ofpacts_pull_openflow_instructions(&of11_in, ofpbuf_size(&of11_in),
3209 OFP11_VERSION, &ofpacts);
3211 /* Verify actions, enforce consistency. */
3213 memset(&flow, 0, sizeof flow);
3214 error = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3216 table_id ? atoi(table_id) : 0,
3217 255, OFPUTIL_P_OF11_STD);
3220 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3221 ofpbuf_uninit(&ofpacts);
3222 ofpbuf_uninit(&of11_in);
3225 ofpbuf_push_uninit(&of11_in, size);
3227 /* Print cls_rule. */
3229 ds_put_cstr(&s, "actions=");
3230 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3234 /* Convert back to ofp11 instructions and print differences from
3236 ofpbuf_init(&of11_out, 0);
3237 ofpacts_put_openflow_instructions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3238 &of11_out, OFP13_VERSION);
3240 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3241 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3244 ofpbuf_uninit(&ofpacts);
3245 ofpbuf_uninit(&of11_in);
3246 ofpbuf_uninit(&of11_out);
3251 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3253 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3257 pcap = ovs_pcap_open(argv[1], "rb");
3259 ovs_fatal(errno, "%s: open failed", argv[1]);
3263 struct ofpbuf *packet;
3265 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3268 error = ovs_pcap_read(pcap, &packet, NULL);
3272 ovs_fatal(error, "%s: read failed", argv[1]);
3275 flow_extract(packet, &md, &flow);
3276 flow_print(stdout, &flow);
3278 ofpbuf_delete(packet);
3282 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3283 * mask values to and from various formats and prints the results. */
3285 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3290 struct ofputil_flow_mod fm;
3293 struct match nxm_match;
3297 struct ofp10_match of10_raw;
3298 struct match of10_match;
3300 struct ofp11_match of11_raw;
3301 struct match of11_match;
3306 enum ofputil_protocol usable_protocols; /* Unused for now. */
3308 match_init_catchall(&match);
3309 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3310 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3312 /* Convert to and from string. */
3313 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3314 printf("%s -> ", string_s);
3316 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3318 ovs_fatal(0, "%s", error_s);
3320 printf("%04"PRIx16"/%04"PRIx16"\n",
3321 ntohs(fm.match.flow.vlan_tci),
3322 ntohs(fm.match.wc.masks.vlan_tci));
3325 /* Convert to and from NXM. */
3326 ofpbuf_init(&nxm, 0);
3327 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3328 nxm_s = nx_match_to_string(ofpbuf_data(&nxm), nxm_match_len);
3329 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3330 printf("NXM: %s -> ", nxm_s);
3332 printf("%s\n", ofperr_to_string(error));
3334 printf("%04"PRIx16"/%04"PRIx16"\n",
3335 ntohs(nxm_match.flow.vlan_tci),
3336 ntohs(nxm_match.wc.masks.vlan_tci));
3339 ofpbuf_uninit(&nxm);
3341 /* Convert to and from OXM. */
3342 ofpbuf_init(&nxm, 0);
3343 nxm_match_len = oxm_put_match(&nxm, &match);
3344 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3345 error = oxm_pull_match(&nxm, &nxm_match);
3346 printf("OXM: %s -> ", nxm_s);
3348 printf("%s\n", ofperr_to_string(error));
3350 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3351 (VLAN_VID_MASK | VLAN_CFI);
3352 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3353 (VLAN_VID_MASK | VLAN_CFI);
3355 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3356 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3357 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3363 ofpbuf_uninit(&nxm);
3365 /* Convert to and from OpenFlow 1.0. */
3366 ofputil_match_to_ofp10_match(&match, &of10_raw);
3367 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3368 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3369 ntohs(of10_raw.dl_vlan),
3370 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3371 of10_raw.dl_vlan_pcp,
3372 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3373 ntohs(of10_match.flow.vlan_tci),
3374 ntohs(of10_match.wc.masks.vlan_tci));
3376 /* Convert to and from OpenFlow 1.1. */
3377 ofputil_match_to_ofp11_match(&match, &of11_raw);
3378 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3379 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3380 ntohs(of11_raw.dl_vlan),
3381 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3382 of11_raw.dl_vlan_pcp,
3383 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3384 ntohs(of11_match.flow.vlan_tci),
3385 ntohs(of11_match.wc.masks.vlan_tci));
3388 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3391 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3396 error = ofperr_from_name(argv[1]);
3398 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3401 for (version = 0; version <= UINT8_MAX; version++) {
3402 const char *name = ofperr_domain_get_name(version);
3404 int vendor = ofperr_get_vendor(error, version);
3405 int type = ofperr_get_type(error, version);
3406 int code = ofperr_get_code(error, version);
3408 if (vendor != -1 || type != -1 || code != -1) {
3409 printf("%s: vendor %#x, type %d, code %d\n",
3410 name, vendor, type, code);
3416 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3417 * error named ENUM and prints the error reply in hex. */
3419 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3421 const struct ofp_header *oh;
3422 struct ofpbuf request, *reply;
3425 error = ofperr_from_name(argv[1]);
3427 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3430 ofpbuf_init(&request, 0);
3431 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3432 ovs_fatal(0, "Trailing garbage in hex data");
3434 if (ofpbuf_size(&request) < sizeof(struct ofp_header)) {
3435 ovs_fatal(0, "Request too short");
3438 oh = ofpbuf_data(&request);
3439 if (ofpbuf_size(&request) != ntohs(oh->length)) {
3440 ovs_fatal(0, "Request size inconsistent");
3443 reply = ofperr_encode_reply(error, ofpbuf_data(&request));
3444 ofpbuf_uninit(&request);
3446 ovs_hex_dump(stdout, ofpbuf_data(reply), ofpbuf_size(reply), 0, false);
3447 ofpbuf_delete(reply);
3450 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3451 * binary data, interpreting them as an OpenFlow message, and prints the
3452 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3454 ofctl_ofp_print(int argc, char *argv[])
3456 struct ofpbuf packet;
3458 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3459 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3460 ovs_fatal(0, "trailing garbage following hex bytes");
3462 ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), argc > 2 ? atoi(argv[2]) : 2);
3463 ofpbuf_uninit(&packet);
3466 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3467 * and dumps each message in hex. */
3469 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3471 uint32_t bitmap = strtol(argv[1], NULL, 0);
3472 struct ofpbuf *hello;
3474 hello = ofputil_encode_hello(bitmap);
3475 ovs_hex_dump(stdout, ofpbuf_data(hello), ofpbuf_size(hello), 0, false);
3476 ofp_print(stdout, ofpbuf_data(hello), ofpbuf_size(hello), verbosity);
3477 ofpbuf_delete(hello);
3480 static const struct command all_commands[] = {
3481 { "show", 1, 1, ofctl_show },
3482 { "monitor", 1, 3, ofctl_monitor },
3483 { "snoop", 1, 1, ofctl_snoop },
3484 { "dump-desc", 1, 1, ofctl_dump_desc },
3485 { "dump-tables", 1, 1, ofctl_dump_tables },
3486 { "dump-table-features", 1, 1, ofctl_dump_table_features },
3487 { "dump-flows", 1, 2, ofctl_dump_flows },
3488 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3489 { "queue-stats", 1, 3, ofctl_queue_stats },
3490 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3491 { "add-flow", 2, 2, ofctl_add_flow },
3492 { "add-flows", 2, 2, ofctl_add_flows },
3493 { "mod-flows", 2, 2, ofctl_mod_flows },
3494 { "del-flows", 1, 2, ofctl_del_flows },
3495 { "replace-flows", 2, 2, ofctl_replace_flows },
3496 { "diff-flows", 2, 2, ofctl_diff_flows },
3497 { "add-meter", 2, 2, ofctl_add_meter },
3498 { "mod-meter", 2, 2, ofctl_mod_meter },
3499 { "del-meter", 2, 2, ofctl_del_meters },
3500 { "del-meters", 1, 1, ofctl_del_meters },
3501 { "dump-meter", 2, 2, ofctl_dump_meters },
3502 { "dump-meters", 1, 1, ofctl_dump_meters },
3503 { "meter-stats", 1, 2, ofctl_meter_stats },
3504 { "meter-features", 1, 1, ofctl_meter_features },
3505 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3506 { "dump-ports", 1, 2, ofctl_dump_ports },
3507 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3508 { "mod-port", 3, 3, ofctl_mod_port },
3509 { "mod-table", 3, 3, ofctl_mod_table },
3510 { "get-frags", 1, 1, ofctl_get_frags },
3511 { "set-frags", 2, 2, ofctl_set_frags },
3512 { "probe", 1, 1, ofctl_probe },
3513 { "ping", 1, 2, ofctl_ping },
3514 { "benchmark", 3, 3, ofctl_benchmark },
3516 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3517 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3519 { "add-group", 1, 2, ofctl_add_group },
3520 { "add-groups", 1, 2, ofctl_add_groups },
3521 { "mod-group", 1, 2, ofctl_mod_group },
3522 { "del-groups", 1, 2, ofctl_del_groups },
3523 { "dump-groups", 1, 2, ofctl_dump_group_desc },
3524 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3525 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3526 { "help", 0, INT_MAX, ofctl_help },
3528 /* Undocumented commands for testing. */
3529 { "parse-flow", 1, 1, ofctl_parse_flow },
3530 { "parse-flows", 1, 1, ofctl_parse_flows },
3531 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3532 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3533 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3534 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3535 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3536 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3537 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3538 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3539 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3540 { "check-vlan", 2, 2, ofctl_check_vlan },
3541 { "print-error", 1, 1, ofctl_print_error },
3542 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3543 { "ofp-print", 1, 2, ofctl_ofp_print },
3544 { "encode-hello", 1, 1, ofctl_encode_hello },
3546 { NULL, 0, 0, NULL },
3549 static const struct command *get_all_commands(void)
3551 return all_commands;