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"
62 #include "openvswitch/vconn.h"
63 #include "openvswitch/vlog.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 OVS_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 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
346 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
347 " dump-group-features SWITCH print group features\n"
348 " dump-groups SWITCH [GROUP] print group description\n"
349 " dump-group-stats SWITCH [GROUP] print group statistics\n"
350 " queue-get-config SWITCH PORT print queue information for port\n"
351 " add-meter SWITCH METER add meter described by METER\n"
352 " mod-meter SWITCH METER modify specific METER\n"
353 " del-meter SWITCH METER delete METER\n"
354 " del-meters SWITCH delete all meters\n"
355 " dump-meter SWITCH METER print METER configuration\n"
356 " dump-meters SWITCH print all meter configuration\n"
357 " meter-stats SWITCH [METER] print meter statistics\n"
358 " meter-features SWITCH print meter features\n"
359 "\nFor OpenFlow switches and controllers:\n"
360 " probe TARGET probe whether TARGET is up\n"
361 " ping TARGET [N] latency of N-byte echos\n"
362 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
363 "SWITCH or TARGET is an active OpenFlow connection method.\n"
364 "\nOther commands:\n"
365 " ofp-parse FILE print messages read from FILE\n"
366 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
367 program_name, program_name);
368 vconn_usage(true, false, false);
372 printf("\nOther options:\n"
373 " --strict use strict match for flow commands\n"
374 " --readd replace flows that haven't changed\n"
375 " -F, --flow-format=FORMAT force particular flow format\n"
376 " -P, --packet-in-format=FRMT force particular packet in format\n"
377 " -m, --more be more verbose printing OpenFlow\n"
378 " --timestamp (monitor, snoop) print timestamps\n"
379 " -t, --timeout=SECS give up after SECS seconds\n"
380 " --sort[=field] sort in ascending order\n"
381 " --rsort[=field] sort in descending order\n"
382 " --unixctl=SOCKET set control socket name\n"
383 " -h, --help display this help message\n"
384 " -V, --version display version information\n");
389 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
390 const char *argv[] OVS_UNUSED, void *exiting_)
392 bool *exiting = exiting_;
394 unixctl_command_reply(conn, NULL);
397 static void run(int retval, const char *message, ...)
398 OVS_PRINTF_FORMAT(2, 3);
401 run(int retval, const char *message, ...)
406 va_start(args, message);
407 ovs_fatal_valist(retval, message, args);
411 /* Generic commands. */
414 open_vconn_socket(const char *name, struct vconn **vconnp)
416 char *vconn_name = xasprintf("unix:%s", name);
419 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
421 if (error && error != ENOENT) {
422 ovs_fatal(0, "%s: failed to open socket (%s)", name,
423 ovs_strerror(error));
430 enum open_target { MGMT, SNOOP };
432 static enum ofputil_protocol
433 open_vconn__(const char *name, enum open_target target,
434 struct vconn **vconnp)
436 const char *suffix = target == MGMT ? "mgmt" : "snoop";
437 char *datapath_name, *datapath_type, *socket_name;
438 enum ofputil_protocol protocol;
443 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
445 ofproto_parse_name(name, &datapath_name, &datapath_type);
446 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
450 if (strchr(name, ':')) {
451 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
452 "connecting to %s", name);
453 } else if (!open_vconn_socket(name, vconnp)) {
455 } else if (!open_vconn_socket(bridge_path, vconnp)) {
457 } else if (!open_vconn_socket(socket_name, vconnp)) {
460 ovs_fatal(0, "%s is not a bridge or a socket", name);
463 if (target == SNOOP) {
464 vconn_set_recv_any_version(*vconnp);
470 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
471 error = vconn_connect_block(*vconnp);
473 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
474 ovs_strerror(error));
477 ofp_version = vconn_get_version(*vconnp);
478 protocol = ofputil_protocol_from_ofp_version(ofp_version);
480 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
486 static enum ofputil_protocol
487 open_vconn(const char *name, struct vconn **vconnp)
489 return open_vconn__(name, MGMT, vconnp);
493 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
495 ofpmsg_update_length(buffer);
496 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
500 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
502 struct ofpbuf *reply;
504 ofpmsg_update_length(request);
505 run(vconn_transact(vconn, request, &reply), "talking to %s",
506 vconn_get_name(vconn));
507 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
508 ofpbuf_delete(reply);
512 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
514 struct ofpbuf *request;
517 open_vconn(vconn_name, &vconn);
518 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
519 dump_transaction(vconn, request);
524 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
526 const struct ofp_header *request_oh = ofpbuf_data(request);
527 ovs_be32 send_xid = request_oh->xid;
528 enum ofpraw request_raw;
529 enum ofpraw reply_raw;
532 ofpraw_decode_partial(&request_raw, ofpbuf_data(request), ofpbuf_size(request));
533 reply_raw = ofpraw_stats_request_to_reply(request_raw,
534 request_oh->version);
536 send_openflow_buffer(vconn, request);
539 struct ofpbuf *reply;
541 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
542 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
543 if (send_xid == recv_xid) {
546 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
548 ofpraw_decode(&raw, ofpbuf_data(reply));
549 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
551 } else if (raw == reply_raw) {
552 done = !ofpmp_more(ofpbuf_data(reply));
554 ovs_fatal(0, "received bad reply: %s",
555 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
559 VLOG_DBG("received reply with xid %08"PRIx32" "
560 "!= expected %08"PRIx32, recv_xid, send_xid);
562 ofpbuf_delete(reply);
567 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
569 struct ofpbuf *request;
572 open_vconn(vconn_name, &vconn);
573 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
574 dump_stats_transaction(vconn, request);
578 /* Sends all of the 'requests', which should be requests that only have replies
579 * if an error occurs, and waits for them to succeed or fail. If an error does
580 * occur, prints it and exits with an error.
582 * Destroys all of the 'requests'. */
584 transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
586 struct ofpbuf *request, *reply;
588 LIST_FOR_EACH (request, list_node, requests) {
589 ofpmsg_update_length(request);
592 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
593 "talking to %s", vconn_get_name(vconn));
595 ofp_print(stderr, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 2);
598 ofpbuf_delete(reply);
601 /* Sends 'request', which should be a request that only has a reply if an error
602 * occurs, and waits for it to succeed or fail. If an error does occur, prints
603 * it and exits with an error.
605 * Destroys 'request'. */
607 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
609 struct ovs_list requests;
611 list_init(&requests);
612 list_push_back(&requests, &request->list_node);
613 transact_multiple_noreply(vconn, &requests);
617 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
619 struct ofp_switch_config *config;
620 struct ofpbuf *request;
621 struct ofpbuf *reply;
624 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
625 vconn_get_version(vconn), 0);
626 run(vconn_transact(vconn, request, &reply),
627 "talking to %s", vconn_get_name(vconn));
629 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
630 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
633 config = ofpbuf_pull(reply, sizeof *config);
636 ofpbuf_delete(reply);
640 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
642 struct ofpbuf *request;
644 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
645 ofpbuf_put(request, config, sizeof *config);
647 transact_noreply(vconn, request);
651 ofctl_show(int argc OVS_UNUSED, char *argv[])
653 const char *vconn_name = argv[1];
654 enum ofp_version version;
656 struct ofpbuf *request;
657 struct ofpbuf *reply;
660 open_vconn(vconn_name, &vconn);
661 version = vconn_get_version(vconn);
662 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
663 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
665 has_ports = ofputil_switch_features_has_ports(reply);
666 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
667 ofpbuf_delete(reply);
670 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
671 dump_stats_transaction(vconn, request);
673 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
678 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
680 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
684 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
686 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
690 ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[])
692 struct ofpbuf *request;
695 open_vconn(argv[1], &vconn);
696 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
698 dump_stats_transaction(vconn, request);
704 static bool fetch_port_by_stats(struct vconn *,
705 const char *port_name, ofp_port_t port_no,
706 struct ofputil_phy_port *);
708 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
709 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
710 * successful, false on failure.
712 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
713 * list of ports in OFPT_FEATURES_REPLY. */
715 fetch_port_by_features(struct vconn *vconn,
716 const char *port_name, ofp_port_t port_no,
717 struct ofputil_phy_port *pp)
719 struct ofputil_switch_features features;
720 const struct ofp_header *oh;
721 struct ofpbuf *request, *reply;
727 /* Fetch the switch's ofp_switch_features. */
728 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
729 vconn_get_version(vconn), 0);
730 run(vconn_transact(vconn, request, &reply),
731 "talking to %s", vconn_get_name(vconn));
733 oh = ofpbuf_data(reply);
734 if (ofptype_decode(&type, ofpbuf_data(reply))
735 || type != OFPTYPE_FEATURES_REPLY) {
736 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
738 if (!ofputil_switch_features_has_ports(reply)) {
739 /* The switch features reply does not contain a complete list of ports.
740 * Probably, there are more ports than will fit into a single 64 kB
741 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
743 ofpbuf_delete(reply);
744 return fetch_port_by_stats(vconn, port_name, port_no, pp);
747 error = ofputil_decode_switch_features(oh, &features, &b);
749 ovs_fatal(0, "%s: failed to decode features reply (%s)",
750 vconn_get_name(vconn), ofperr_to_string(error));
753 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
754 if (port_no != OFPP_NONE
755 ? port_no == pp->port_no
756 : !strcmp(pp->name, port_name)) {
761 ofpbuf_delete(reply);
765 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
766 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
767 * successful, false on failure.
769 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
770 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
771 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
772 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
774 fetch_port_by_stats(struct vconn *vconn,
775 const char *port_name, ofp_port_t port_no,
776 struct ofputil_phy_port *pp)
778 struct ofpbuf *request;
783 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
785 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
787 send_openflow_buffer(vconn, request);
790 struct ofpbuf *reply;
792 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
793 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
794 if (send_xid == recv_xid) {
795 struct ofp_header *oh = ofpbuf_data(reply);
800 ofpbuf_use_const(&b, oh, ntohs(oh->length));
801 if (ofptype_pull(&type, &b)
802 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
803 ovs_fatal(0, "received bad reply: %s",
804 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
808 flags = ofpmp_flags(oh);
809 done = !(flags & OFPSF_REPLY_MORE);
812 /* We've already found the port, but we need to drain
813 * the queue of any other replies for this request. */
817 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
818 if (port_no != OFPP_NONE ? port_no == pp->port_no
819 : !strcmp(pp->name, port_name)) {
825 VLOG_DBG("received reply with xid %08"PRIx32" "
826 "!= expected %08"PRIx32, recv_xid, send_xid);
828 ofpbuf_delete(reply);
835 str_to_ofp(const char *s, ofp_port_t *ofp_port)
840 ret = str_to_uint(s, 10, &port_);
841 *ofp_port = u16_to_ofp(port_);
845 /* Opens a connection to 'vconn_name', fetches the port structure for
846 * 'port_name' (which may be a port name or number), and copies it into
849 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
850 struct ofputil_phy_port *pp)
856 /* Try to interpret the argument as a port number. */
857 if (!str_to_ofp(port_name, &port_no)) {
861 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
862 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
863 * into the OFPST_PORT_DESC reply. Try it the correct way. */
864 open_vconn(vconn_name, &vconn);
865 found = (vconn_get_version(vconn) < OFP13_VERSION
866 ? fetch_port_by_features(vconn, port_name, port_no, pp)
867 : fetch_port_by_stats(vconn, port_name, port_no, pp));
871 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
875 /* Returns the port number corresponding to 'port_name' (which may be a port
876 * name or number) within the switch 'vconn_name'. */
878 str_to_port_no(const char *vconn_name, const char *port_name)
882 if (ofputil_port_from_string(port_name, &port_no)) {
885 struct ofputil_phy_port pp;
887 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
893 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
894 enum ofputil_protocol *cur)
897 struct ofpbuf *request, *reply;
898 enum ofputil_protocol next;
900 request = ofputil_encode_set_protocol(*cur, want, &next);
905 run(vconn_transact_noreply(vconn, request, &reply),
906 "talking to %s", vconn_get_name(vconn));
908 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
909 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
910 vconn_get_name(vconn), s);
912 ofpbuf_delete(reply);
920 static enum ofputil_protocol
921 set_protocol_for_flow_dump(struct vconn *vconn,
922 enum ofputil_protocol cur_protocol,
923 enum ofputil_protocol usable_protocols)
928 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
929 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
930 if (f & usable_protocols & allowed_protocols
931 && try_set_protocol(vconn, f, &cur_protocol)) {
936 usable_s = ofputil_protocols_to_string(usable_protocols);
937 if (usable_protocols & allowed_protocols) {
938 ovs_fatal(0, "switch does not support any of the usable flow "
939 "formats (%s)", usable_s);
941 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
942 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
943 "allowed flow formats (%s)", usable_s, allowed_s);
947 static struct vconn *
948 prepare_dump_flows(int argc, char *argv[], bool aggregate,
949 struct ofpbuf **requestp)
951 enum ofputil_protocol usable_protocols, protocol;
952 struct ofputil_flow_stats_request fsr;
956 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
957 argc > 2 ? argv[2] : "",
960 ovs_fatal(0, "%s", error);
963 protocol = open_vconn(argv[1], &vconn);
964 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
965 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
970 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
972 struct ofpbuf *request;
975 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
976 dump_stats_transaction(vconn, request);
981 compare_flows(const void *afs_, const void *bfs_)
983 const struct ofputil_flow_stats *afs = afs_;
984 const struct ofputil_flow_stats *bfs = bfs_;
985 const struct match *a = &afs->match;
986 const struct match *b = &bfs->match;
987 const struct sort_criterion *sc;
989 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
990 const struct mf_field *f = sc->field;
994 int a_pri = afs->priority;
995 int b_pri = bfs->priority;
996 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
1000 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
1001 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
1003 /* Skip the test for sc->order, so that missing fields always
1004 * sort to the end whether we're sorting in ascending or
1005 * descending order. */
1006 return ina ? -1 : 1;
1008 union mf_value aval, bval;
1010 mf_get_value(f, &a->flow, &aval);
1011 mf_get_value(f, &b->flow, &bval);
1012 ret = memcmp(&aval, &bval, f->n_bytes);
1017 return sc->order == SORT_ASC ? ret : -ret;
1025 ofctl_dump_flows(int argc, char *argv[])
1028 ofctl_dump_flows__(argc, argv, false);
1031 struct ofputil_flow_stats *fses;
1032 size_t n_fses, allocated_fses;
1033 struct ofpbuf *request;
1034 struct ofpbuf ofpacts;
1035 struct ofpbuf *reply;
1036 struct vconn *vconn;
1041 vconn = prepare_dump_flows(argc, argv, false, &request);
1042 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
1043 send_openflow_buffer(vconn, request);
1046 n_fses = allocated_fses = 0;
1048 ofpbuf_init(&ofpacts, 0);
1050 struct ofputil_flow_stats *fs;
1052 if (n_fses >= allocated_fses) {
1053 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1057 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1061 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1064 ofpbuf_uninit(&ofpacts);
1066 qsort(fses, n_fses, sizeof *fses, compare_flows);
1069 for (i = 0; i < n_fses; i++) {
1071 ofp_print_flow_stats(&s, &fses[i]);
1076 for (i = 0; i < n_fses; i++) {
1077 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1086 ofctl_dump_aggregate(int argc, char *argv[])
1088 ofctl_dump_flows__(argc, argv, true);
1092 ofctl_queue_stats(int argc, char *argv[])
1094 struct ofpbuf *request;
1095 struct vconn *vconn;
1096 struct ofputil_queue_stats_request oqs;
1098 open_vconn(argv[1], &vconn);
1100 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1101 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1103 oqs.port_no = OFPP_ANY;
1105 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1106 oqs.queue_id = atoi(argv[3]);
1108 oqs.queue_id = OFPQ_ALL;
1111 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1112 dump_stats_transaction(vconn, request);
1117 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1119 const char *vconn_name = argv[1];
1120 const char *port_name = argv[2];
1121 enum ofputil_protocol protocol;
1122 enum ofp_version version;
1123 struct ofpbuf *request;
1124 struct vconn *vconn;
1127 port = str_to_port_no(vconn_name, port_name);
1129 protocol = open_vconn(vconn_name, &vconn);
1130 version = ofputil_protocol_to_ofp_version(protocol);
1131 request = ofputil_encode_queue_get_config_request(version, port);
1132 dump_transaction(vconn, request);
1136 static enum ofputil_protocol
1137 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1138 enum ofputil_protocol usable_protocols)
1140 enum ofputil_protocol cur_protocol;
1144 if (!(usable_protocols & allowed_protocols)) {
1145 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1146 usable_s = ofputil_protocols_to_string(usable_protocols);
1147 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1148 "allowed flow formats (%s)", usable_s, allowed_s);
1151 /* If the initial flow format is allowed and usable, keep it. */
1152 cur_protocol = open_vconn(remote, vconnp);
1153 if (usable_protocols & allowed_protocols & cur_protocol) {
1154 return cur_protocol;
1157 /* Otherwise try each flow format in turn. */
1158 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1159 enum ofputil_protocol f = 1 << i;
1161 if (f != cur_protocol
1162 && f & usable_protocols & allowed_protocols
1163 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1168 usable_s = ofputil_protocols_to_string(usable_protocols);
1169 ovs_fatal(0, "switch does not support any of the usable flow "
1170 "formats (%s)", usable_s);
1174 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1175 size_t n_fms, enum ofputil_protocol usable_protocols)
1177 enum ofputil_protocol protocol;
1178 struct vconn *vconn;
1181 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1183 for (i = 0; i < n_fms; i++) {
1184 struct ofputil_flow_mod *fm = &fms[i];
1186 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1187 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1193 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1195 enum ofputil_protocol usable_protocols;
1196 struct ofputil_flow_mod *fms = NULL;
1200 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1203 ovs_fatal(0, "%s", error);
1205 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1210 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1212 if (argc > 2 && !strcmp(argv[2], "-")) {
1213 ofctl_flow_mod_file(argc, argv, command);
1215 struct ofputil_flow_mod fm;
1217 enum ofputil_protocol usable_protocols;
1219 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1222 ovs_fatal(0, "%s", error);
1224 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1229 ofctl_add_flow(int argc, char *argv[])
1231 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1235 ofctl_add_flows(int argc, char *argv[])
1237 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1241 ofctl_mod_flows(int argc, char *argv[])
1243 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1247 ofctl_del_flows(int argc, char *argv[])
1249 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1253 set_packet_in_format(struct vconn *vconn,
1254 enum nx_packet_in_format packet_in_format)
1256 struct ofpbuf *spif;
1258 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1260 transact_noreply(vconn, spif);
1261 VLOG_DBG("%s: using user-specified packet in format %s",
1262 vconn_get_name(vconn),
1263 ofputil_packet_in_format_to_string(packet_in_format));
1267 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1269 struct ofp_switch_config config;
1270 enum ofp_config_flags flags;
1272 fetch_switch_config(vconn, &config);
1273 flags = ntohs(config.flags);
1274 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1275 /* Set the invalid ttl config. */
1276 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1278 config.flags = htons(flags);
1279 set_switch_config(vconn, &config);
1281 /* Then retrieve the configuration to see if it really took. OpenFlow
1282 * doesn't define error reporting for bad modes, so this is all we can
1284 fetch_switch_config(vconn, &config);
1285 flags = ntohs(config.flags);
1286 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1287 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1288 "switch probably doesn't support mode)");
1295 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1296 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1297 * an error message and stores NULL in '*msgp'. */
1299 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1301 struct ofp_header *oh;
1304 msg = ofpbuf_new(strlen(hex) / 2);
1307 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1309 return "Trailing garbage in hex data";
1312 if (ofpbuf_size(msg) < sizeof(struct ofp_header)) {
1314 return "Message too short for OpenFlow";
1317 oh = ofpbuf_data(msg);
1318 if (ofpbuf_size(msg) != ntohs(oh->length)) {
1320 return "Message size does not match length in OpenFlow header";
1328 ofctl_send(struct unixctl_conn *conn, int argc,
1329 const char *argv[], void *vconn_)
1331 struct vconn *vconn = vconn_;
1338 for (i = 1; i < argc; i++) {
1339 const char *error_msg;
1343 error_msg = openflow_from_hex(argv[i], &msg);
1345 ds_put_format(&reply, "%s\n", error_msg);
1350 fprintf(stderr, "send: ");
1351 ofp_print(stderr, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
1353 error = vconn_send_block(vconn, msg);
1356 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1359 ds_put_cstr(&reply, "sent\n");
1364 unixctl_command_reply(conn, ds_cstr(&reply));
1366 unixctl_command_reply_error(conn, ds_cstr(&reply));
1371 struct barrier_aux {
1372 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1373 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1377 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1378 const char *argv[] OVS_UNUSED, void *aux_)
1380 struct barrier_aux *aux = aux_;
1385 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1389 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1390 error = vconn_send_block(aux->vconn, msg);
1393 unixctl_command_reply_error(conn, ovs_strerror(error));
1400 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1401 const char *argv[], void *aux OVS_UNUSED)
1405 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1407 unixctl_command_reply_error(conn, ovs_strerror(errno));
1412 dup2(fd, STDERR_FILENO);
1414 unixctl_command_reply(conn, NULL);
1418 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1419 const char *argv[] OVS_UNUSED, void *blocked_)
1421 bool *blocked = blocked_;
1425 unixctl_command_reply(conn, NULL);
1427 unixctl_command_reply(conn, "already blocking");
1432 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1433 const char *argv[] OVS_UNUSED, void *blocked_)
1435 bool *blocked = blocked_;
1439 unixctl_command_reply(conn, NULL);
1441 unixctl_command_reply(conn, "already unblocked");
1445 /* Prints to stdout all of the messages received on 'vconn'.
1447 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1448 * received on 'vconn'. */
1450 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1452 struct barrier_aux barrier_aux = { vconn, NULL };
1453 struct unixctl_server *server;
1454 bool exiting = false;
1455 bool blocked = false;
1458 daemon_save_fd(STDERR_FILENO);
1460 error = unixctl_server_create(unixctl_path, &server);
1462 ovs_fatal(error, "failed to create unixctl server");
1464 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1465 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1467 unixctl_command_register("ofctl/barrier", "", 0, 0,
1468 ofctl_barrier, &barrier_aux);
1469 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1470 ofctl_set_output_file, NULL);
1472 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1473 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1476 daemonize_complete();
1482 unixctl_server_run(server);
1487 retval = vconn_recv(vconn, &b);
1488 if (retval == EAGAIN) {
1491 run(retval, "vconn_recv");
1494 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1495 time_wall_msec(), true);
1500 ofptype_decode(&type, ofpbuf_data(b));
1501 ofp_print(stderr, ofpbuf_data(b), ofpbuf_size(b), verbosity + 2);
1504 switch ((int) type) {
1505 case OFPTYPE_BARRIER_REPLY:
1506 if (barrier_aux.conn) {
1507 unixctl_command_reply(barrier_aux.conn, NULL);
1508 barrier_aux.conn = NULL;
1512 case OFPTYPE_ECHO_REQUEST:
1513 if (reply_to_echo_requests) {
1514 struct ofpbuf *reply;
1516 reply = make_echo_reply(ofpbuf_data(b));
1517 retval = vconn_send_block(vconn, reply);
1519 ovs_fatal(retval, "failed to send echo reply");
1532 vconn_run_wait(vconn);
1534 vconn_recv_wait(vconn);
1536 unixctl_server_wait(server);
1540 unixctl_server_destroy(server);
1544 ofctl_monitor(int argc, char *argv[])
1546 struct vconn *vconn;
1548 enum ofputil_protocol usable_protocols;
1550 open_vconn(argv[1], &vconn);
1551 for (i = 2; i < argc; i++) {
1552 const char *arg = argv[i];
1554 if (isdigit((unsigned char) *arg)) {
1555 struct ofp_switch_config config;
1557 fetch_switch_config(vconn, &config);
1558 config.miss_send_len = htons(atoi(arg));
1559 set_switch_config(vconn, &config);
1560 } else if (!strcmp(arg, "invalid_ttl")) {
1561 monitor_set_invalid_ttl_to_controller(vconn);
1562 } else if (!strncmp(arg, "watch:", 6)) {
1563 struct ofputil_flow_monitor_request fmr;
1567 error = parse_flow_monitor_request(&fmr, arg + 6,
1570 ovs_fatal(0, "%s", error);
1573 msg = ofpbuf_new(0);
1574 ofputil_append_flow_monitor_request(&fmr, msg);
1575 dump_stats_transaction(vconn, msg);
1578 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1582 if (preferred_packet_in_format >= 0) {
1583 set_packet_in_format(vconn, preferred_packet_in_format);
1585 enum ofp_version version = vconn_get_version(vconn);
1588 case OFP10_VERSION: {
1589 struct ofpbuf *spif, *reply;
1591 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1593 run(vconn_transact_noreply(vconn, spif, &reply),
1594 "talking to %s", vconn_get_name(vconn));
1596 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
1597 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1598 " replied: %s. Falling back to the switch default.",
1599 vconn_get_name(vconn), s);
1601 ofpbuf_delete(reply);
1616 monitor_vconn(vconn, true);
1620 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1622 struct vconn *vconn;
1624 open_vconn__(argv[1], SNOOP, &vconn);
1625 monitor_vconn(vconn, false);
1629 ofctl_dump_ports(int argc, char *argv[])
1631 struct ofpbuf *request;
1632 struct vconn *vconn;
1635 open_vconn(argv[1], &vconn);
1636 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1637 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1638 dump_stats_transaction(vconn, request);
1643 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1645 struct ofpbuf *request;
1646 struct vconn *vconn;
1649 open_vconn(argv[1], &vconn);
1650 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1651 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1653 dump_stats_transaction(vconn, request);
1658 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1660 struct ofpbuf *request;
1661 struct vconn *vconn;
1662 struct ofpbuf *reply;
1664 open_vconn(argv[1], &vconn);
1665 request = make_echo_request(vconn_get_version(vconn));
1666 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1667 if (ofpbuf_size(reply) != sizeof(struct ofp_header)) {
1668 ovs_fatal(0, "reply does not match request");
1670 ofpbuf_delete(reply);
1675 ofctl_packet_out(int argc, char *argv[])
1677 enum ofputil_protocol protocol;
1678 struct ofputil_packet_out po;
1679 struct ofpbuf ofpacts;
1680 struct vconn *vconn;
1683 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1685 ofpbuf_init(&ofpacts, 64);
1686 error = ofpacts_parse_actions(argv[3], &ofpacts, &usable_protocols);
1688 ovs_fatal(0, "%s", error);
1691 po.buffer_id = UINT32_MAX;
1692 po.in_port = str_to_port_no(argv[1], argv[2]);
1693 po.ofpacts = ofpbuf_data(&ofpacts);
1694 po.ofpacts_len = ofpbuf_size(&ofpacts);
1696 protocol = open_vconn(argv[1], &vconn);
1697 for (i = 4; i < argc; i++) {
1698 struct ofpbuf *packet, *opo;
1699 const char *error_msg;
1701 error_msg = eth_from_hex(argv[i], &packet);
1703 ovs_fatal(0, "%s", error_msg);
1706 po.packet = ofpbuf_data(packet);
1707 po.packet_len = ofpbuf_size(packet);
1708 opo = ofputil_encode_packet_out(&po, protocol);
1709 transact_noreply(vconn, opo);
1710 ofpbuf_delete(packet);
1713 ofpbuf_uninit(&ofpacts);
1717 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1719 struct ofp_config_flag {
1720 const char *name; /* The flag's name. */
1721 enum ofputil_port_config bit; /* Bit to turn on or off. */
1722 bool on; /* Value to set the bit to. */
1724 static const struct ofp_config_flag flags[] = {
1725 { "up", OFPUTIL_PC_PORT_DOWN, false },
1726 { "down", OFPUTIL_PC_PORT_DOWN, true },
1727 { "stp", OFPUTIL_PC_NO_STP, false },
1728 { "receive", OFPUTIL_PC_NO_RECV, false },
1729 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1730 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1731 { "forward", OFPUTIL_PC_NO_FWD, false },
1732 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1735 const struct ofp_config_flag *flag;
1736 enum ofputil_protocol protocol;
1737 struct ofputil_port_mod pm;
1738 struct ofputil_phy_port pp;
1739 struct vconn *vconn;
1740 const char *command;
1743 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1745 pm.port_no = pp.port_no;
1746 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1751 if (!strncasecmp(argv[3], "no-", 3)) {
1752 command = argv[3] + 3;
1754 } else if (!strncasecmp(argv[3], "no", 2)) {
1755 command = argv[3] + 2;
1761 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1762 if (!strcasecmp(command, flag->name)) {
1763 pm.mask = flag->bit;
1764 pm.config = flag->on ^ not ? flag->bit : 0;
1768 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1771 protocol = open_vconn(argv[1], &vconn);
1772 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1777 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1779 enum ofputil_protocol protocol, usable_protocols;
1780 struct ofputil_table_mod tm;
1781 struct vconn *vconn;
1785 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1787 ovs_fatal(0, "%s", error);
1790 protocol = open_vconn(argv[1], &vconn);
1791 if (!(protocol & usable_protocols)) {
1792 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1793 enum ofputil_protocol f = 1 << i;
1795 && f & usable_protocols
1796 && try_set_protocol(vconn, f, &protocol)) {
1803 if (!(protocol & usable_protocols)) {
1804 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1805 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1808 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1813 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1815 struct ofp_switch_config config;
1816 struct vconn *vconn;
1818 open_vconn(argv[1], &vconn);
1819 fetch_switch_config(vconn, &config);
1820 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1825 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1827 struct ofp_switch_config config;
1828 enum ofp_config_flags mode;
1829 struct vconn *vconn;
1832 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1833 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1836 open_vconn(argv[1], &vconn);
1837 fetch_switch_config(vconn, &config);
1838 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1839 if (flags != config.flags) {
1840 /* Set the configuration. */
1841 config.flags = flags;
1842 set_switch_config(vconn, &config);
1844 /* Then retrieve the configuration to see if it really took. OpenFlow
1845 * doesn't define error reporting for bad modes, so this is all we can
1847 fetch_switch_config(vconn, &config);
1848 if (flags != config.flags) {
1849 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1850 "switch probably doesn't support mode \"%s\")",
1851 argv[1], ofputil_frag_handling_to_string(mode));
1858 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1860 const char *filename = argv[1];
1864 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1866 ovs_fatal(errno, "%s: open", filename);
1869 ofpbuf_init(&b, 65536);
1871 struct ofp_header *oh;
1872 size_t length, tail_len;
1877 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1878 n = fread(oh, 1, sizeof *oh, file);
1881 } else if (n < sizeof *oh) {
1882 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1885 length = ntohs(oh->length);
1886 if (length < sizeof *oh) {
1887 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1891 tail_len = length - sizeof *oh;
1892 tail = ofpbuf_put_uninit(&b, tail_len);
1893 n = fread(tail, 1, tail_len, file);
1895 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1898 ofp_print(stdout, ofpbuf_data(&b), ofpbuf_size(&b), verbosity + 2);
1902 if (file != stdin) {
1908 is_openflow_port(ovs_be16 port_, char *ports[])
1910 uint16_t port = ntohs(port_);
1914 for (i = 0; ports[i]; i++) {
1915 if (port == atoi(ports[i])) {
1921 return port == OFP_PORT || port == OFP_OLD_PORT;
1926 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1928 struct tcp_reader *reader;
1933 file = ovs_pcap_open(argv[1], "rb");
1935 ovs_fatal(errno, "%s: open failed", argv[1]);
1938 reader = tcp_reader_open();
1941 struct ofpbuf *packet;
1944 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1946 error = ovs_pcap_read(file, &packet, &when);
1950 flow_extract(packet, &md, &flow);
1951 if (flow.dl_type == htons(ETH_TYPE_IP)
1952 && flow.nw_proto == IPPROTO_TCP
1953 && (is_openflow_port(flow.tp_src, argv + 2) ||
1954 is_openflow_port(flow.tp_dst, argv + 2))) {
1955 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1957 while (ofpbuf_size(payload) >= sizeof(struct ofp_header)) {
1958 const struct ofp_header *oh;
1959 void *data = ofpbuf_data(payload);
1962 /* Align OpenFlow on 8-byte boundary for safe access. */
1963 ofpbuf_shift(payload, -((intptr_t) data & 7));
1965 oh = ofpbuf_data(payload);
1966 length = ntohs(oh->length);
1967 if (ofpbuf_size(payload) < length) {
1977 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1982 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1983 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1984 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1985 ofp_print(stdout, ofpbuf_data(payload), length, verbosity + 1);
1986 ofpbuf_pull(payload, length);
1990 ofpbuf_delete(packet);
1992 tcp_reader_close(reader);
1996 ofctl_ping(int argc, char *argv[])
1998 size_t max_payload = 65535 - sizeof(struct ofp_header);
1999 unsigned int payload;
2000 struct vconn *vconn;
2003 payload = argc > 2 ? atoi(argv[2]) : 64;
2004 if (payload > max_payload) {
2005 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2008 open_vconn(argv[1], &vconn);
2009 for (i = 0; i < 10; i++) {
2010 struct timeval start, end;
2011 struct ofpbuf *request, *reply;
2012 const struct ofp_header *rpy_hdr;
2015 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2016 vconn_get_version(vconn), payload);
2017 random_bytes(ofpbuf_put_uninit(request, payload), payload);
2019 xgettimeofday(&start);
2020 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2021 xgettimeofday(&end);
2023 rpy_hdr = ofpbuf_data(reply);
2024 if (ofptype_pull(&type, reply)
2025 || type != OFPTYPE_ECHO_REPLY
2026 || ofpbuf_size(reply) != payload
2027 || memcmp(ofpbuf_l3(request), ofpbuf_l3(reply), payload)) {
2028 printf("Reply does not match request. Request:\n");
2029 ofp_print(stdout, request, ofpbuf_size(request), verbosity + 2);
2031 ofp_print(stdout, reply, ofpbuf_size(reply), verbosity + 2);
2033 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2034 ofpbuf_size(reply), argv[1], ntohl(rpy_hdr->xid),
2035 (1000*(double)(end.tv_sec - start.tv_sec))
2036 + (.001*(end.tv_usec - start.tv_usec)));
2037 ofpbuf_delete(request);
2038 ofpbuf_delete(reply);
2044 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
2046 size_t max_payload = 65535 - sizeof(struct ofp_header);
2047 struct timeval start, end;
2048 unsigned int payload_size, message_size;
2049 struct vconn *vconn;
2054 payload_size = atoi(argv[2]);
2055 if (payload_size > max_payload) {
2056 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2058 message_size = sizeof(struct ofp_header) + payload_size;
2060 count = atoi(argv[3]);
2062 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2063 count, message_size, count * message_size);
2065 open_vconn(argv[1], &vconn);
2066 xgettimeofday(&start);
2067 for (i = 0; i < count; i++) {
2068 struct ofpbuf *request, *reply;
2070 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2071 vconn_get_version(vconn), payload_size);
2072 ofpbuf_put_zeros(request, payload_size);
2073 run(vconn_transact(vconn, request, &reply), "transact");
2074 ofpbuf_delete(reply);
2076 xgettimeofday(&end);
2079 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2080 + (.001*(end.tv_usec - start.tv_usec)));
2081 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2082 duration, count / (duration / 1000.0),
2083 count * message_size / (duration / 1000.0));
2087 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2088 size_t n_gms, enum ofputil_protocol usable_protocols)
2090 enum ofputil_protocol protocol;
2091 struct ofputil_group_mod *gm;
2092 enum ofp_version version;
2093 struct ofpbuf *request;
2095 struct vconn *vconn;
2098 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2099 version = ofputil_protocol_to_ofp_version(protocol);
2101 for (i = 0; i < n_gms; i++) {
2103 request = ofputil_encode_group_mod(version, gm);
2105 transact_noreply(vconn, request);
2115 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2117 struct ofputil_group_mod *gms = NULL;
2118 enum ofputil_protocol usable_protocols;
2123 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2126 ovs_fatal(0, "%s", error);
2128 ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
2129 for (i = 0; i < n_gms; i++) {
2130 ofputil_bucket_list_destroy(&gms[i].buckets);
2136 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2138 if (argc > 2 && !strcmp(argv[2], "-")) {
2139 ofctl_group_mod_file(argc, argv, command);
2141 enum ofputil_protocol usable_protocols;
2142 struct ofputil_group_mod gm;
2145 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2148 ovs_fatal(0, "%s", error);
2150 ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
2151 ofputil_bucket_list_destroy(&gm.buckets);
2156 ofctl_add_group(int argc, char *argv[])
2158 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2162 ofctl_add_groups(int argc, char *argv[])
2164 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2168 ofctl_mod_group(int argc, char *argv[])
2170 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2174 ofctl_del_groups(int argc, char *argv[])
2176 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2180 ofctl_insert_bucket(int argc, char *argv[])
2182 ofctl_group_mod(argc, argv, OFPGC15_INSERT_BUCKET);
2186 ofctl_remove_bucket(int argc, char *argv[])
2188 ofctl_group_mod(argc, argv, OFPGC15_REMOVE_BUCKET);
2192 ofctl_dump_group_stats(int argc, char *argv[])
2194 enum ofputil_protocol usable_protocols;
2195 struct ofputil_group_mod gm;
2196 struct ofpbuf *request;
2197 struct vconn *vconn;
2201 memset(&gm, 0, sizeof gm);
2203 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2204 argc > 2 ? argv[2] : "",
2207 ovs_fatal(0, "%s", error);
2210 group_id = gm.group_id;
2212 open_vconn(argv[1], &vconn);
2213 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2216 dump_stats_transaction(vconn, request);
2223 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2225 struct ofpbuf *request;
2226 struct vconn *vconn;
2229 open_vconn(argv[1], &vconn);
2231 if (argc < 3 || !ofputil_group_from_string(argv[2], &group_id)) {
2232 group_id = OFPG11_ALL;
2235 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2238 dump_stats_transaction(vconn, request);
2245 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2247 struct ofpbuf *request;
2248 struct vconn *vconn;
2250 open_vconn(argv[1], &vconn);
2251 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2253 dump_stats_transaction(vconn, request);
2260 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2266 ofctl_list_commands(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2268 print_commands(get_all_commands());
2271 /* replace-flows and diff-flows commands. */
2273 /* A flow table entry, possibly with two different versions. */
2275 struct cls_rule rule; /* Within a "struct classifier". */
2276 struct fte_version *versions[2];
2279 /* One version of a Flow Table Entry. */
2280 struct fte_version {
2282 uint16_t idle_timeout;
2283 uint16_t hard_timeout;
2284 uint16_t importance;
2286 struct ofpact *ofpacts;
2290 /* Frees 'version' and the data that it owns. */
2292 fte_version_free(struct fte_version *version)
2295 free(CONST_CAST(struct ofpact *, version->ofpacts));
2300 /* Returns true if 'a' and 'b' are the same, false if they differ.
2302 * Ignores differences in 'flags' because there's no way to retrieve flags from
2303 * an OpenFlow switch. We have to assume that they are the same. */
2305 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2307 return (a->cookie == b->cookie
2308 && a->idle_timeout == b->idle_timeout
2309 && a->hard_timeout == b->hard_timeout
2310 && a->importance == b->importance
2311 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2312 b->ofpacts, b->ofpacts_len));
2315 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2316 * 'index' into 's', followed by a new-line. */
2318 fte_version_format(const struct fte *fte, int index, struct ds *s)
2320 const struct fte_version *version = fte->versions[index];
2327 cls_rule_format(&fte->rule, s);
2328 if (version->cookie != htonll(0)) {
2329 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2331 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2332 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2334 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2335 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2337 if (version->importance != 0) {
2338 ds_put_format(s, " importance=%"PRIu16, version->importance);
2341 ds_put_cstr(s, " actions=");
2342 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2344 ds_put_char(s, '\n');
2348 fte_from_cls_rule(const struct cls_rule *cls_rule)
2350 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2353 /* Frees 'fte' and its versions. */
2355 fte_free(struct fte *fte)
2358 fte_version_free(fte->versions[0]);
2359 fte_version_free(fte->versions[1]);
2360 cls_rule_destroy(&fte->rule);
2365 /* Frees all of the FTEs within 'cls'. */
2367 fte_free_all(struct classifier *cls)
2371 classifier_defer(cls);
2372 CLS_FOR_EACH (fte, rule, cls) {
2373 classifier_remove(cls, &fte->rule);
2374 ovsrcu_postpone(fte_free, fte);
2376 classifier_destroy(cls);
2379 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2380 * necessary. Sets 'version' as the version of that rule with the given
2381 * 'index', replacing any existing version, if any.
2383 * Takes ownership of 'version'. */
2385 fte_insert(struct classifier *cls, const struct match *match,
2386 int priority, struct fte_version *version, int index)
2388 struct fte *old, *fte;
2390 fte = xzalloc(sizeof *fte);
2391 cls_rule_init(&fte->rule, match, priority);
2392 fte->versions[index] = version;
2394 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2396 fte->versions[!index] = old->versions[!index];
2397 old->versions[!index] = NULL;
2399 ovsrcu_postpone(fte_free, old);
2403 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2404 * with the specified 'index'. Returns the flow formats able to represent the
2405 * flows that were read. */
2406 static enum ofputil_protocol
2407 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2409 enum ofputil_protocol usable_protocols;
2414 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2416 ovs_fatal(errno, "%s: open", filename);
2420 usable_protocols = OFPUTIL_P_ANY;
2422 classifier_defer(cls);
2423 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2424 struct fte_version *version;
2425 struct ofputil_flow_mod fm;
2427 enum ofputil_protocol usable;
2429 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2431 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2433 usable_protocols &= usable;
2435 version = xmalloc(sizeof *version);
2436 version->cookie = fm.new_cookie;
2437 version->idle_timeout = fm.idle_timeout;
2438 version->hard_timeout = fm.hard_timeout;
2439 version->importance = fm.importance;
2440 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2441 | OFPUTIL_FF_EMERG);
2442 version->ofpacts = fm.ofpacts;
2443 version->ofpacts_len = fm.ofpacts_len;
2445 fte_insert(cls, &fm.match, fm.priority, version, index);
2447 classifier_publish(cls);
2450 if (file != stdin) {
2454 return usable_protocols;
2458 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2459 struct ofpbuf **replyp,
2460 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2462 struct ofpbuf *reply = *replyp;
2468 /* Get a flow stats reply message, if we don't already have one. */
2474 run(vconn_recv_block(vconn, &reply),
2475 "OpenFlow packet receive failed");
2476 } while (((struct ofp_header *) ofpbuf_data(reply))->xid != send_xid);
2478 error = ofptype_decode(&type, ofpbuf_data(reply));
2479 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2480 ovs_fatal(0, "received bad reply: %s",
2481 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
2486 /* Pull an individual flow stats reply out of the message. */
2487 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2494 more = ofpmp_more(reply->frame);
2495 ofpbuf_delete(reply);
2504 ovs_fatal(0, "parse error in reply (%s)",
2505 ofperr_to_string(retval));
2510 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2511 * format 'protocol', and adds them as flow table entries in 'cls' for the
2512 * version with the specified 'index'. */
2514 read_flows_from_switch(struct vconn *vconn,
2515 enum ofputil_protocol protocol,
2516 struct classifier *cls, int index)
2518 struct ofputil_flow_stats_request fsr;
2519 struct ofputil_flow_stats fs;
2520 struct ofpbuf *request;
2521 struct ofpbuf ofpacts;
2522 struct ofpbuf *reply;
2525 fsr.aggregate = false;
2526 match_init_catchall(&fsr.match);
2527 fsr.out_port = OFPP_ANY;
2528 fsr.table_id = 0xff;
2529 fsr.cookie = fsr.cookie_mask = htonll(0);
2530 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2531 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
2532 send_openflow_buffer(vconn, request);
2535 ofpbuf_init(&ofpacts, 0);
2536 classifier_defer(cls);
2537 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2538 struct fte_version *version;
2540 version = xmalloc(sizeof *version);
2541 version->cookie = fs.cookie;
2542 version->idle_timeout = fs.idle_timeout;
2543 version->hard_timeout = fs.hard_timeout;
2544 version->importance = fs.importance;
2546 version->ofpacts_len = fs.ofpacts_len;
2547 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2549 fte_insert(cls, &fs.match, fs.priority, version, index);
2551 classifier_publish(cls);
2552 ofpbuf_uninit(&ofpacts);
2556 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2557 enum ofputil_protocol protocol, struct ovs_list *packets)
2559 const struct fte_version *version = fte->versions[index];
2560 struct ofputil_flow_mod fm;
2563 minimatch_expand(&fte->rule.match, &fm.match);
2564 fm.priority = fte->rule.priority;
2565 fm.cookie = htonll(0);
2566 fm.cookie_mask = htonll(0);
2567 fm.new_cookie = version->cookie;
2568 fm.modify_cookie = true;
2570 fm.command = command;
2571 fm.idle_timeout = version->idle_timeout;
2572 fm.hard_timeout = version->hard_timeout;
2573 fm.importance = version->importance;
2574 fm.buffer_id = UINT32_MAX;
2575 fm.out_port = OFPP_ANY;
2576 fm.flags = version->flags;
2577 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2578 command == OFPFC_MODIFY_STRICT) {
2579 fm.ofpacts = version->ofpacts;
2580 fm.ofpacts_len = version->ofpacts_len;
2585 fm.delete_reason = OFPRR_DELETE;
2587 ofm = ofputil_encode_flow_mod(&fm, protocol);
2588 list_push_back(packets, &ofm->list_node);
2592 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2594 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2595 enum ofputil_protocol usable_protocols, protocol;
2596 struct classifier cls;
2597 struct ovs_list requests;
2598 struct vconn *vconn;
2601 classifier_init(&cls, NULL);
2602 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2604 protocol = open_vconn(argv[1], &vconn);
2605 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2607 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2609 list_init(&requests);
2611 /* Delete flows that exist on the switch but not in the file. */
2612 CLS_FOR_EACH (fte, rule, &cls) {
2613 struct fte_version *file_ver = fte->versions[FILE_IDX];
2614 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2616 if (sw_ver && !file_ver) {
2617 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2618 protocol, &requests);
2622 /* Add flows that exist in the file but not on the switch.
2623 * Update flows that exist in both places but differ. */
2624 CLS_FOR_EACH (fte, rule, &cls) {
2625 struct fte_version *file_ver = fte->versions[FILE_IDX];
2626 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2629 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2630 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2633 transact_multiple_noreply(vconn, &requests);
2640 read_flows_from_source(const char *source, struct classifier *cls, int index)
2644 if (source[0] == '/' || source[0] == '.'
2645 || (!strchr(source, ':') && !stat(source, &s))) {
2646 read_flows_from_file(source, cls, index);
2648 enum ofputil_protocol protocol;
2649 struct vconn *vconn;
2651 protocol = open_vconn(source, &vconn);
2652 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2653 read_flows_from_switch(vconn, protocol, cls, index);
2659 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2661 bool differences = false;
2662 struct classifier cls;
2666 classifier_init(&cls, NULL);
2667 read_flows_from_source(argv[1], &cls, 0);
2668 read_flows_from_source(argv[2], &cls, 1);
2673 CLS_FOR_EACH (fte, rule, &cls) {
2674 struct fte_version *a = fte->versions[0];
2675 struct fte_version *b = fte->versions[1];
2677 if (!a || !b || !fte_version_equals(a, b)) {
2678 fte_version_format(fte, 0, &a_s);
2679 fte_version_format(fte, 1, &b_s);
2680 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2682 printf("-%s", ds_cstr(&a_s));
2685 printf("+%s", ds_cstr(&b_s));
2703 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2705 struct ofputil_meter_mod mm;
2706 struct vconn *vconn;
2707 enum ofputil_protocol protocol;
2708 enum ofputil_protocol usable_protocols;
2709 enum ofp_version version;
2713 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2715 ovs_fatal(0, "%s", error);
2718 usable_protocols = OFPUTIL_P_OF13_UP;
2719 mm.command = command;
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_mod(version, &mm));
2730 ofctl_meter_request__(const char *bridge, const char *str,
2731 enum ofputil_meter_request_type type)
2733 struct ofputil_meter_mod mm;
2734 struct vconn *vconn;
2735 enum ofputil_protocol usable_protocols;
2736 enum ofputil_protocol protocol;
2737 enum ofp_version version;
2741 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2743 ovs_fatal(0, "%s", error);
2746 usable_protocols = OFPUTIL_P_OF13_UP;
2747 mm.meter.meter_id = OFPM13_ALL;
2750 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2751 version = ofputil_protocol_to_ofp_version(protocol);
2752 transact_noreply(vconn, ofputil_encode_meter_request(version,
2754 mm.meter.meter_id));
2760 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2762 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2766 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2768 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2772 ofctl_del_meters(int argc, char *argv[])
2774 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2778 ofctl_dump_meters(int argc, char *argv[])
2780 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2781 OFPUTIL_METER_CONFIG);
2785 ofctl_meter_stats(int argc, char *argv[])
2787 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2788 OFPUTIL_METER_STATS);
2792 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2794 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2798 /* Undocumented commands for unit testing. */
2801 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2802 enum ofputil_protocol usable_protocols)
2804 enum ofputil_protocol protocol = 0;
2808 usable_s = ofputil_protocols_to_string(usable_protocols);
2809 printf("usable protocols: %s\n", usable_s);
2812 if (!(usable_protocols & allowed_protocols)) {
2813 ovs_fatal(0, "no usable protocol");
2815 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2817 if (protocol & usable_protocols & allowed_protocols) {
2821 ovs_assert(is_pow2(protocol));
2823 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2825 for (i = 0; i < n_fms; i++) {
2826 struct ofputil_flow_mod *fm = &fms[i];
2829 msg = ofputil_encode_flow_mod(fm, protocol);
2830 ofp_print(stdout, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
2833 free(CONST_CAST(struct ofpact *, fm->ofpacts));
2837 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2838 * it back to stdout. */
2840 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2842 enum ofputil_protocol usable_protocols;
2843 struct ofputil_flow_mod fm;
2846 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2848 ovs_fatal(0, "%s", error);
2850 ofctl_parse_flows__(&fm, 1, usable_protocols);
2853 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2854 * add-flows) and prints each of the flows back to stdout. */
2856 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2858 enum ofputil_protocol usable_protocols;
2859 struct ofputil_flow_mod *fms = NULL;
2863 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2866 ovs_fatal(0, "%s", error);
2868 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2873 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
2878 while (!ds_get_test_line(&in, stdin)) {
2879 struct ofpbuf nx_match;
2881 ovs_be64 cookie, cookie_mask;
2885 /* Convert string to nx_match. */
2886 ofpbuf_init(&nx_match, 0);
2888 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2890 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2893 /* Convert nx_match to match. */
2896 error = oxm_pull_match(&nx_match, &match);
2898 error = nx_pull_match(&nx_match, match_len, &match,
2899 &cookie, &cookie_mask);
2903 error = oxm_pull_match_loose(&nx_match, &match);
2905 error = nx_pull_match_loose(&nx_match, match_len, &match,
2906 &cookie, &cookie_mask);
2914 /* Convert match back to nx_match. */
2915 ofpbuf_uninit(&nx_match);
2916 ofpbuf_init(&nx_match, 0);
2918 match_len = oxm_put_match(&nx_match, &match, version);
2919 out = oxm_match_to_string(&nx_match, match_len);
2921 match_len = nx_put_match(&nx_match, &match,
2922 cookie, cookie_mask);
2923 out = nx_match_to_string(ofpbuf_data(&nx_match), match_len);
2929 if (verbosity > 0) {
2930 ovs_hex_dump(stdout, ofpbuf_data(&nx_match),
2931 ofpbuf_size(&nx_match), 0, false);
2934 printf("nx_pull_match() returned error %s\n",
2935 ofperr_get_name(error));
2938 ofpbuf_uninit(&nx_match);
2943 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2944 * stdin, does some internal fussing with them, and then prints them back as
2945 * strings on stdout. */
2947 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2949 ofctl_parse_nxm__(false, 0);
2952 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
2953 * strings from stdin, does some internal fussing with them, and then prints
2954 * them back as strings on stdout. VERSION must specify an OpenFlow version,
2955 * e.g. "OpenFlow12". */
2957 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[])
2959 enum ofp_version version = ofputil_version_from_string(argv[1]);
2960 if (version < OFP12_VERSION) {
2961 ovs_fatal(0, "%s: not a valid version for OXM", argv[1]);
2964 ofctl_parse_nxm__(true, version);
2968 print_differences(const char *prefix,
2969 const void *a_, size_t a_len,
2970 const void *b_, size_t b_len)
2972 const uint8_t *a = a_;
2973 const uint8_t *b = b_;
2976 for (i = 0; i < MIN(a_len, b_len); i++) {
2978 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2979 prefix, i, a[i], b[i]);
2982 for (i = a_len; i < b_len; i++) {
2983 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2985 for (i = b_len; i < a_len; i++) {
2986 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2991 ofctl_parse_actions__(const char *version_s, bool instructions)
2993 enum ofp_version version;
2996 version = ofputil_version_from_string(version_s);
2998 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3002 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3003 struct ofpbuf of_out;
3004 struct ofpbuf of_in;
3005 struct ofpbuf ofpacts;
3006 const char *table_id;
3012 /* Parse table_id separated with the follow-up actions by ",", if
3014 actions = ds_cstr(&in);
3016 if (strstr(actions, ",")) {
3017 table_id = strsep(&actions, ",");
3020 /* Parse hex bytes. */
3021 ofpbuf_init(&of_in, 0);
3022 if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
3023 ovs_fatal(0, "Trailing garbage in hex data");
3026 /* Convert to ofpacts. */
3027 ofpbuf_init(&ofpacts, 0);
3028 size = ofpbuf_size(&of_in);
3029 error = (instructions
3030 ? ofpacts_pull_openflow_instructions
3031 : ofpacts_pull_openflow_actions)(
3032 &of_in, ofpbuf_size(&of_in), version, &ofpacts);
3033 if (!error && instructions) {
3034 /* Verify actions, enforce consistency. */
3035 enum ofputil_protocol protocol;
3038 memset(&flow, 0, sizeof flow);
3039 protocol = ofputil_protocols_from_ofp_version(version);
3040 error = ofpacts_check_consistency(ofpbuf_data(&ofpacts),
3041 ofpbuf_size(&ofpacts),
3043 table_id ? atoi(table_id) : 0,
3047 printf("bad %s %s: %s\n\n",
3048 version_s, instructions ? "instructions" : "actions",
3049 ofperr_get_name(error));
3050 ofpbuf_uninit(&ofpacts);
3051 ofpbuf_uninit(&of_in);
3054 ofpbuf_push_uninit(&of_in, size);
3056 /* Print cls_rule. */
3058 ds_put_cstr(&s, "actions=");
3059 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3063 /* Convert back to ofp10 actions and print differences from input. */
3064 ofpbuf_init(&of_out, 0);
3066 ofpacts_put_openflow_instructions( ofpbuf_data(&ofpacts),
3067 ofpbuf_size(&ofpacts),
3070 ofpacts_put_openflow_actions( ofpbuf_data(&ofpacts),
3071 ofpbuf_size(&ofpacts),
3075 print_differences("", ofpbuf_data(&of_in), ofpbuf_size(&of_in),
3076 ofpbuf_data(&of_out), ofpbuf_size(&of_out));
3079 ofpbuf_uninit(&ofpacts);
3080 ofpbuf_uninit(&of_in);
3081 ofpbuf_uninit(&of_out);
3086 /* "parse-actions VERSION": reads a series of action specifications for the
3087 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3088 * prints them as strings on stdout, and then converts them back to hex bytes
3089 * and prints any differences from the input. */
3091 ofctl_parse_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3093 ofctl_parse_actions__(argv[1], false);
3096 /* "parse-actions VERSION": reads a series of instruction specifications for
3097 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3098 * ofpacts, prints them as strings on stdout, and then converts them back to
3099 * hex bytes and prints any differences from the input. */
3101 ofctl_parse_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3103 ofctl_parse_actions__(argv[1], true);
3106 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3107 * bytes from stdin, converts them to cls_rules, prints them as strings on
3108 * stdout, and then converts them back to hex bytes and prints any differences
3111 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3112 * values are ignored in the input and will be set to zero when OVS converts
3113 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3114 * it does the conversion to hex, to ensure that in fact they are ignored. */
3116 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3123 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3124 struct ofpbuf match_in, match_expout;
3125 struct ofp10_match match_out;
3126 struct ofp10_match match_normal;
3130 /* Parse hex bytes to use for expected output. */
3132 ds_put_cstr(&expout, ds_cstr(&in));
3133 for (p = ds_cstr(&expout); *p; p++) {
3138 ofpbuf_init(&match_expout, 0);
3139 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3140 ovs_fatal(0, "Trailing garbage in hex data");
3142 if (ofpbuf_size(&match_expout) != sizeof(struct ofp10_match)) {
3143 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3144 ofpbuf_size(&match_expout), sizeof(struct ofp10_match));
3147 /* Parse hex bytes for input. */
3148 for (p = ds_cstr(&in); *p; p++) {
3150 *p = "0123456789abcdef"[random_uint32() & 0xf];
3153 ofpbuf_init(&match_in, 0);
3154 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3155 ovs_fatal(0, "Trailing garbage in hex data");
3157 if (ofpbuf_size(&match_in) != sizeof(struct ofp10_match)) {
3158 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3159 ofpbuf_size(&match_in), sizeof(struct ofp10_match));
3162 /* Convert to cls_rule and print. */
3163 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in), &match);
3164 match_print(&match);
3166 /* Convert back to ofp10_match and print differences from input. */
3167 ofputil_match_to_ofp10_match(&match, &match_out);
3168 print_differences("", ofpbuf_data(&match_expout), ofpbuf_size(&match_expout),
3169 &match_out, sizeof match_out);
3171 /* Normalize, then convert and compare again. */
3172 ofputil_normalize_match(&match);
3173 ofputil_match_to_ofp10_match(&match, &match_normal);
3174 print_differences("normal: ", &match_out, sizeof match_out,
3175 &match_normal, sizeof match_normal);
3178 ofpbuf_uninit(&match_in);
3179 ofpbuf_uninit(&match_expout);
3182 ds_destroy(&expout);
3185 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3186 * bytes from stdin, converts them to "struct match"es, prints them as strings
3187 * on stdout, and then converts them back to hex bytes and prints any
3188 * differences from the input. */
3190 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3195 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3196 struct ofpbuf match_in;
3197 struct ofp11_match match_out;
3201 /* Parse hex bytes. */
3202 ofpbuf_init(&match_in, 0);
3203 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3204 ovs_fatal(0, "Trailing garbage in hex data");
3206 if (ofpbuf_size(&match_in) != sizeof(struct ofp11_match)) {
3207 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3208 ofpbuf_size(&match_in), sizeof(struct ofp11_match));
3211 /* Convert to match. */
3212 error = ofputil_match_from_ofp11_match(ofpbuf_data(&match_in), &match);
3214 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3215 ofpbuf_uninit(&match_in);
3220 match_print(&match);
3222 /* Convert back to ofp11_match and print differences from input. */
3223 ofputil_match_to_ofp11_match(&match, &match_out);
3225 print_differences("", ofpbuf_data(&match_in), ofpbuf_size(&match_in),
3226 &match_out, sizeof match_out);
3229 ofpbuf_uninit(&match_in);
3234 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3236 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3240 pcap = ovs_pcap_open(argv[1], "rb");
3242 ovs_fatal(errno, "%s: open failed", argv[1]);
3246 struct ofpbuf *packet;
3248 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3251 error = ovs_pcap_read(pcap, &packet, NULL);
3255 ovs_fatal(error, "%s: read failed", argv[1]);
3258 flow_extract(packet, &md, &flow);
3259 flow_print(stdout, &flow);
3261 ofpbuf_delete(packet);
3265 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3266 * mask values to and from various formats and prints the results. */
3268 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3273 struct ofputil_flow_mod fm;
3276 struct match nxm_match;
3280 struct ofp10_match of10_raw;
3281 struct match of10_match;
3283 struct ofp11_match of11_raw;
3284 struct match of11_match;
3289 enum ofputil_protocol usable_protocols; /* Unused for now. */
3291 match_init_catchall(&match);
3292 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3293 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3295 /* Convert to and from string. */
3296 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3297 printf("%s -> ", string_s);
3299 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3301 ovs_fatal(0, "%s", error_s);
3303 printf("%04"PRIx16"/%04"PRIx16"\n",
3304 ntohs(fm.match.flow.vlan_tci),
3305 ntohs(fm.match.wc.masks.vlan_tci));
3308 /* Convert to and from NXM. */
3309 ofpbuf_init(&nxm, 0);
3310 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3311 nxm_s = nx_match_to_string(ofpbuf_data(&nxm), nxm_match_len);
3312 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3313 printf("NXM: %s -> ", nxm_s);
3315 printf("%s\n", ofperr_to_string(error));
3317 printf("%04"PRIx16"/%04"PRIx16"\n",
3318 ntohs(nxm_match.flow.vlan_tci),
3319 ntohs(nxm_match.wc.masks.vlan_tci));
3322 ofpbuf_uninit(&nxm);
3324 /* Convert to and from OXM. */
3325 ofpbuf_init(&nxm, 0);
3326 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
3327 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3328 error = oxm_pull_match(&nxm, &nxm_match);
3329 printf("OXM: %s -> ", nxm_s);
3331 printf("%s\n", ofperr_to_string(error));
3333 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3334 (VLAN_VID_MASK | VLAN_CFI);
3335 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3336 (VLAN_VID_MASK | VLAN_CFI);
3338 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3339 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3340 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3346 ofpbuf_uninit(&nxm);
3348 /* Convert to and from OpenFlow 1.0. */
3349 ofputil_match_to_ofp10_match(&match, &of10_raw);
3350 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3351 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3352 ntohs(of10_raw.dl_vlan),
3353 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3354 of10_raw.dl_vlan_pcp,
3355 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3356 ntohs(of10_match.flow.vlan_tci),
3357 ntohs(of10_match.wc.masks.vlan_tci));
3359 /* Convert to and from OpenFlow 1.1. */
3360 ofputil_match_to_ofp11_match(&match, &of11_raw);
3361 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3362 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3363 ntohs(of11_raw.dl_vlan),
3364 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3365 of11_raw.dl_vlan_pcp,
3366 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3367 ntohs(of11_match.flow.vlan_tci),
3368 ntohs(of11_match.wc.masks.vlan_tci));
3371 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3374 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3379 error = ofperr_from_name(argv[1]);
3381 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3384 for (version = 0; version <= UINT8_MAX; version++) {
3385 const char *name = ofperr_domain_get_name(version);
3387 int vendor = ofperr_get_vendor(error, version);
3388 int type = ofperr_get_type(error, version);
3389 int code = ofperr_get_code(error, version);
3391 if (vendor != -1 || type != -1 || code != -1) {
3392 printf("%s: vendor %#x, type %d, code %d\n",
3393 name, vendor, type, code);
3399 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3400 * error named ENUM and prints the error reply in hex. */
3402 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3404 const struct ofp_header *oh;
3405 struct ofpbuf request, *reply;
3408 error = ofperr_from_name(argv[1]);
3410 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3413 ofpbuf_init(&request, 0);
3414 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3415 ovs_fatal(0, "Trailing garbage in hex data");
3417 if (ofpbuf_size(&request) < sizeof(struct ofp_header)) {
3418 ovs_fatal(0, "Request too short");
3421 oh = ofpbuf_data(&request);
3422 if (ofpbuf_size(&request) != ntohs(oh->length)) {
3423 ovs_fatal(0, "Request size inconsistent");
3426 reply = ofperr_encode_reply(error, ofpbuf_data(&request));
3427 ofpbuf_uninit(&request);
3429 ovs_hex_dump(stdout, ofpbuf_data(reply), ofpbuf_size(reply), 0, false);
3430 ofpbuf_delete(reply);
3433 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3434 * binary data, interpreting them as an OpenFlow message, and prints the
3435 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3437 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3438 * HEXSTRING_FILE contains the HEXSTRING. */
3440 ofctl_ofp_print(int argc, char *argv[])
3442 struct ofpbuf packet;
3449 if (!strcmp(argv[argc-1], "-")) {
3450 if (ds_get_line(&line, stdin)) {
3451 VLOG_FATAL("Failed to read stdin");
3454 buffer = line.string;
3455 verbosity = argc > 2 ? atoi(argv[1]) : verbosity;
3456 } else if (argc > 2) {
3458 verbosity = atoi(argv[2]);
3463 ofpbuf_init(&packet, strlen(buffer) / 2);
3464 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
3465 ovs_fatal(0, "trailing garbage following hex bytes");
3467 ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), verbosity);
3468 ofpbuf_uninit(&packet);
3472 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3473 * and dumps each message in hex. */
3475 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3477 uint32_t bitmap = strtol(argv[1], NULL, 0);
3478 struct ofpbuf *hello;
3480 hello = ofputil_encode_hello(bitmap);
3481 ovs_hex_dump(stdout, ofpbuf_data(hello), ofpbuf_size(hello), 0, false);
3482 ofp_print(stdout, ofpbuf_data(hello), ofpbuf_size(hello), verbosity);
3483 ofpbuf_delete(hello);
3486 static const struct command all_commands[] = {
3489 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3490 1, 3, ofctl_monitor },
3491 { "snoop", "switch",
3492 1, 1, ofctl_snoop },
3493 { "dump-desc", "switch",
3494 1, 1, ofctl_dump_desc },
3495 { "dump-tables", "switch",
3496 1, 1, ofctl_dump_tables },
3497 { "dump-table-features", "switch",
3498 1, 1, ofctl_dump_table_features },
3499 { "dump-flows", "switch",
3500 1, 2, ofctl_dump_flows },
3501 { "dump-aggregate", "switch",
3502 1, 2, ofctl_dump_aggregate },
3503 { "queue-stats", "switch [port [queue]]",
3504 1, 3, ofctl_queue_stats },
3505 { "queue-get-config", "switch port",
3506 2, 2, ofctl_queue_get_config },
3507 { "add-flow", "switch flow",
3508 2, 2, ofctl_add_flow },
3509 { "add-flows", "switch file",
3510 2, 2, ofctl_add_flows },
3511 { "mod-flows", "switch flow",
3512 2, 2, ofctl_mod_flows },
3513 { "del-flows", "switch [flow]",
3514 1, 2, ofctl_del_flows },
3515 { "replace-flows", "switch file",
3516 2, 2, ofctl_replace_flows },
3517 { "diff-flows", "source1 source2",
3518 2, 2, ofctl_diff_flows },
3519 { "add-meter", "switch meter",
3520 2, 2, ofctl_add_meter },
3521 { "mod-meter", "switch meter",
3522 2, 2, ofctl_mod_meter },
3523 { "del-meter", "switch meter",
3524 2, 2, ofctl_del_meters },
3525 { "del-meters", "switch",
3526 1, 1, ofctl_del_meters },
3527 { "dump-meter", "switch meter",
3528 2, 2, ofctl_dump_meters },
3529 { "dump-meters", "switch",
3530 1, 1, ofctl_dump_meters },
3531 { "meter-stats", "switch [meter]",
3532 1, 2, ofctl_meter_stats },
3533 { "meter-features", "switch",
3534 1, 1, ofctl_meter_features },
3535 { "packet-out", "switch in_port actions packet...",
3536 4, INT_MAX, ofctl_packet_out },
3537 { "dump-ports", "switch [port]",
3538 1, 2, ofctl_dump_ports },
3539 { "dump-ports-desc", "switch [port]",
3540 1, 2, ofctl_dump_ports_desc },
3541 { "mod-port", "switch iface act",
3542 3, 3, ofctl_mod_port },
3543 { "mod-table", "switch mod",
3544 3, 3, ofctl_mod_table },
3545 { "get-frags", "switch",
3546 1, 1, ofctl_get_frags },
3547 { "set-frags", "switch frag_mode",
3548 2, 2, ofctl_set_frags },
3549 { "probe", "target",
3550 1, 1, ofctl_probe },
3551 { "ping", "target [n]",
3553 { "benchmark", "target n count",
3554 3, 3, ofctl_benchmark },
3556 { "ofp-parse", "file",
3557 1, 1, ofctl_ofp_parse },
3558 { "ofp-parse-pcap", "pcap",
3559 1, INT_MAX, ofctl_ofp_parse_pcap },
3561 { "add-group", "switch group",
3562 1, 2, ofctl_add_group },
3563 { "add-groups", "switch file",
3564 1, 2, ofctl_add_groups },
3565 { "mod-group", "switch group",
3566 1, 2, ofctl_mod_group },
3567 { "del-groups", "switch [group]",
3568 1, 2, ofctl_del_groups },
3569 { "insert-buckets", "switch [group]",
3570 1, 2, ofctl_insert_bucket },
3571 { "remove-buckets", "switch [group]",
3572 1, 2, ofctl_remove_bucket },
3573 { "dump-groups", "switch [group]",
3574 1, 2, ofctl_dump_group_desc },
3575 { "dump-group-stats", "switch [group]",
3576 1, 2, ofctl_dump_group_stats },
3577 { "dump-group-features", "switch",
3578 1, 1, ofctl_dump_group_features },
3579 { "help", NULL, 0, INT_MAX, ofctl_help },
3580 { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands },
3582 /* Undocumented commands for testing. */
3583 { "parse-flow", NULL, 1, 1, ofctl_parse_flow },
3584 { "parse-flows", NULL, 1, 1, ofctl_parse_flows },
3585 { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm },
3586 { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm },
3587 { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm },
3588 { "parse-actions", NULL, 1, 1, ofctl_parse_actions },
3589 { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions },
3590 { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match },
3591 { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match },
3592 { "parse-pcap", NULL, 1, 1, ofctl_parse_pcap },
3593 { "check-vlan", NULL, 2, 2, ofctl_check_vlan },
3594 { "print-error", NULL, 1, 1, ofctl_print_error },
3595 { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply },
3596 { "ofp-print", NULL, 1, 2, ofctl_ofp_print },
3597 { "encode-hello", NULL, 1, 1, ofctl_encode_hello },
3599 { NULL, NULL, 0, 0, NULL },
3602 static const struct command *get_all_commands(void)
3604 return all_commands;