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 service_start(&argc, &argv);
117 parse_options(argc, argv);
118 fatal_ignore_sigpipe();
119 run_command(argc - optind, argv + optind, get_all_commands());
124 add_sort_criterion(enum sort_order order, const char *field)
126 struct sort_criterion *sc;
128 if (n_criteria >= allocated_criteria) {
129 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
132 sc = &criteria[n_criteria++];
133 if (!field || !strcasecmp(field, "priority")) {
136 sc->field = mf_from_name(field);
138 ovs_fatal(0, "%s: unknown field name", field);
145 parse_options(int argc, char *argv[])
148 OPT_STRICT = UCHAR_MAX + 1,
154 OFP_VERSION_OPTION_ENUMS,
157 static const struct option long_options[] = {
158 {"timeout", required_argument, NULL, 't'},
159 {"strict", no_argument, NULL, OPT_STRICT},
160 {"readd", no_argument, NULL, OPT_READD},
161 {"flow-format", required_argument, NULL, 'F'},
162 {"packet-in-format", required_argument, NULL, 'P'},
163 {"more", no_argument, NULL, 'm'},
164 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
165 {"sort", optional_argument, NULL, OPT_SORT},
166 {"rsort", optional_argument, NULL, OPT_RSORT},
167 {"help", no_argument, NULL, 'h'},
169 OFP_VERSION_LONG_OPTIONS,
171 STREAM_SSL_LONG_OPTIONS,
174 char *short_options = long_options_to_short_options(long_options);
176 enum ofputil_protocol version_protocols;
178 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
179 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
180 * requests, but those requests have subtly different semantics in
181 * different OpenFlow versions. For example:
183 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
184 * existing flow to modify adds a new flow.
186 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
187 * existing flow to modify adds a new flow, but only if the mod-flow
188 * did not match on the flow cookie.
190 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
193 set_allowed_ofp_versions("OpenFlow10");
196 unsigned long int timeout;
199 c = getopt_long(argc, argv, short_options, long_options, NULL);
206 timeout = strtoul(optarg, NULL, 10);
208 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
216 allowed_protocols = ofputil_protocols_from_string(optarg);
217 if (!allowed_protocols) {
218 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
223 preferred_packet_in_format =
224 ofputil_packet_in_format_from_string(optarg);
225 if (preferred_packet_in_format < 0) {
226 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
250 add_sort_criterion(SORT_ASC, optarg);
254 add_sort_criterion(SORT_DESC, optarg);
257 DAEMON_OPTION_HANDLERS
258 OFP_VERSION_OPTION_HANDLERS
260 STREAM_SSL_OPTION_HANDLERS
271 /* Always do a final sort pass based on priority. */
272 add_sort_criterion(SORT_DESC, "priority");
277 versions = get_allowed_ofp_versions();
278 version_protocols = ofputil_protocols_from_version_bitmap(versions);
279 if (!(allowed_protocols & version_protocols)) {
280 char *protocols = ofputil_protocols_to_string(allowed_protocols);
281 struct ds version_s = DS_EMPTY_INITIALIZER;
283 ofputil_format_version_bitmap_names(&version_s, versions);
284 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
285 "any of the enabled flow formats (%s). (Use -O to enable "
286 "additional OpenFlow versions or -F to enable additional "
287 "flow formats.)", ds_cstr(&version_s), protocols);
289 allowed_protocols &= version_protocols;
290 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
297 printf("%s: OpenFlow switch management utility\n"
298 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
299 "\nFor OpenFlow switches:\n"
300 " show SWITCH show OpenFlow information\n"
301 " dump-desc SWITCH print switch description\n"
302 " dump-tables SWITCH print table stats\n"
303 " dump-table-features SWITCH print table features\n"
304 " mod-port SWITCH IFACE ACT modify port behavior\n"
305 " mod-table SWITCH MOD modify flow table behavior\n"
306 " get-frags SWITCH print fragment handling behavior\n"
307 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
308 " dump-ports SWITCH [PORT] print port statistics\n"
309 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
310 " dump-flows SWITCH print all flow entries\n"
311 " dump-flows SWITCH FLOW print matching FLOWs\n"
312 " dump-aggregate SWITCH print aggregate flow statistics\n"
313 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
314 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
315 " add-flow SWITCH FLOW add flow described by FLOW\n"
316 " add-flows SWITCH FILE add flows from FILE\n"
317 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
318 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
319 " replace-flows SWITCH FILE replace flows with those in FILE\n"
320 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
321 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
322 " execute ACTIONS on PACKET\n"
323 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
324 " print packets received from SWITCH\n"
325 " snoop SWITCH snoop on SWITCH and its controller\n"
326 " add-group SWITCH GROUP add group described by GROUP\n"
327 " add-group SWITCH FILE add group from FILE\n"
328 " mod-group SWITCH GROUP modify specific group\n"
329 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
330 " dump-group-features SWITCH print group features\n"
331 " dump-groups SWITCH [GROUP] print group description\n"
332 " dump-group-stats SWITCH [GROUP] print group statistics\n"
333 " queue-get-config SWITCH PORT print queue information for port\n"
334 " add-meter SWITCH METER add meter described by METER\n"
335 " mod-meter SWITCH METER modify specific METER\n"
336 " del-meter SWITCH METER delete METER\n"
337 " del-meters SWITCH delete all meters\n"
338 " dump-meter SWITCH METER print METER configuration\n"
339 " dump-meters SWITCH print all meter configuration\n"
340 " meter-stats SWITCH [METER] print meter statistics\n"
341 " meter-features SWITCH print meter features\n"
342 "\nFor OpenFlow switches and controllers:\n"
343 " probe TARGET probe whether TARGET is up\n"
344 " ping TARGET [N] latency of N-byte echos\n"
345 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
346 "SWITCH or TARGET is an active OpenFlow connection method.\n"
347 "\nOther commands:\n"
348 " ofp-parse FILE print messages read from FILE\n"
349 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
350 program_name, program_name);
351 vconn_usage(true, false, false);
355 printf("\nOther options:\n"
356 " --strict use strict match for flow commands\n"
357 " --readd replace flows that haven't changed\n"
358 " -F, --flow-format=FORMAT force particular flow format\n"
359 " -P, --packet-in-format=FRMT force particular packet in format\n"
360 " -m, --more be more verbose printing OpenFlow\n"
361 " --timestamp (monitor, snoop) print timestamps\n"
362 " -t, --timeout=SECS give up after SECS seconds\n"
363 " --sort[=field] sort in ascending order\n"
364 " --rsort[=field] sort in descending order\n"
365 " -h, --help display this help message\n"
366 " -V, --version display version information\n");
371 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
372 const char *argv[] OVS_UNUSED, void *exiting_)
374 bool *exiting = exiting_;
376 unixctl_command_reply(conn, NULL);
379 static void run(int retval, const char *message, ...)
383 run(int retval, const char *message, ...)
388 va_start(args, message);
389 ovs_fatal_valist(retval, message, args);
393 /* Generic commands. */
396 open_vconn_socket(const char *name, struct vconn **vconnp)
398 char *vconn_name = xasprintf("unix:%s", name);
401 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
403 if (error && error != ENOENT) {
404 ovs_fatal(0, "%s: failed to open socket (%s)", name,
405 ovs_strerror(error));
412 enum open_target { MGMT, SNOOP };
414 static enum ofputil_protocol
415 open_vconn__(const char *name, enum open_target target,
416 struct vconn **vconnp)
418 const char *suffix = target == MGMT ? "mgmt" : "snoop";
419 char *datapath_name, *datapath_type, *socket_name;
420 enum ofputil_protocol protocol;
425 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
427 ofproto_parse_name(name, &datapath_name, &datapath_type);
428 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
432 if (strchr(name, ':')) {
433 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
434 "connecting to %s", name);
435 } else if (!open_vconn_socket(name, vconnp)) {
437 } else if (!open_vconn_socket(bridge_path, vconnp)) {
439 } else if (!open_vconn_socket(socket_name, vconnp)) {
442 ovs_fatal(0, "%s is not a bridge or a socket", name);
445 if (target == SNOOP) {
446 vconn_set_recv_any_version(*vconnp);
452 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
453 error = vconn_connect_block(*vconnp);
455 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
456 ovs_strerror(error));
459 ofp_version = vconn_get_version(*vconnp);
460 protocol = ofputil_protocol_from_ofp_version(ofp_version);
462 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
468 static enum ofputil_protocol
469 open_vconn(const char *name, struct vconn **vconnp)
471 return open_vconn__(name, MGMT, vconnp);
475 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
477 ofpmsg_update_length(buffer);
478 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
482 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
484 struct ofpbuf *reply;
486 ofpmsg_update_length(request);
487 run(vconn_transact(vconn, request, &reply), "talking to %s",
488 vconn_get_name(vconn));
489 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
490 ofpbuf_delete(reply);
494 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
496 struct ofpbuf *request;
499 open_vconn(vconn_name, &vconn);
500 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
501 dump_transaction(vconn, request);
506 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
508 const struct ofp_header *request_oh = ofpbuf_data(request);
509 ovs_be32 send_xid = request_oh->xid;
510 enum ofpraw request_raw;
511 enum ofpraw reply_raw;
514 ofpraw_decode_partial(&request_raw, ofpbuf_data(request), ofpbuf_size(request));
515 reply_raw = ofpraw_stats_request_to_reply(request_raw,
516 request_oh->version);
518 send_openflow_buffer(vconn, request);
521 struct ofpbuf *reply;
523 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
524 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
525 if (send_xid == recv_xid) {
528 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
530 ofpraw_decode(&raw, ofpbuf_data(reply));
531 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
533 } else if (raw == reply_raw) {
534 done = !ofpmp_more(ofpbuf_data(reply));
536 ovs_fatal(0, "received bad reply: %s",
537 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
541 VLOG_DBG("received reply with xid %08"PRIx32" "
542 "!= expected %08"PRIx32, recv_xid, send_xid);
544 ofpbuf_delete(reply);
549 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
551 struct ofpbuf *request;
554 open_vconn(vconn_name, &vconn);
555 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
556 dump_stats_transaction(vconn, request);
560 /* Sends all of the 'requests', which should be requests that only have replies
561 * if an error occurs, and waits for them to succeed or fail. If an error does
562 * occur, prints it and exits with an error.
564 * Destroys all of the 'requests'. */
566 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
568 struct ofpbuf *request, *reply;
570 LIST_FOR_EACH (request, list_node, requests) {
571 ofpmsg_update_length(request);
574 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
575 "talking to %s", vconn_get_name(vconn));
577 ofp_print(stderr, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 2);
580 ofpbuf_delete(reply);
583 /* Sends 'request', which should be a request that only has a reply if an error
584 * occurs, and waits for it to succeed or fail. If an error does occur, prints
585 * it and exits with an error.
587 * Destroys 'request'. */
589 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
591 struct list requests;
593 list_init(&requests);
594 list_push_back(&requests, &request->list_node);
595 transact_multiple_noreply(vconn, &requests);
599 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
601 struct ofp_switch_config *config;
602 struct ofpbuf *request;
603 struct ofpbuf *reply;
606 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
607 vconn_get_version(vconn), 0);
608 run(vconn_transact(vconn, request, &reply),
609 "talking to %s", vconn_get_name(vconn));
611 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
612 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
615 config = ofpbuf_pull(reply, sizeof *config);
618 ofpbuf_delete(reply);
622 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
624 struct ofpbuf *request;
626 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
627 ofpbuf_put(request, config, sizeof *config);
629 transact_noreply(vconn, request);
633 ofctl_show(int argc OVS_UNUSED, char *argv[])
635 const char *vconn_name = argv[1];
636 enum ofp_version version;
638 struct ofpbuf *request;
639 struct ofpbuf *reply;
642 open_vconn(vconn_name, &vconn);
643 version = vconn_get_version(vconn);
644 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
645 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
647 has_ports = ofputil_switch_features_has_ports(reply);
648 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
649 ofpbuf_delete(reply);
652 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
653 dump_stats_transaction(vconn, request);
655 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
660 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
662 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
666 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
668 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
672 ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[])
674 struct ofpbuf *request;
677 open_vconn(argv[1], &vconn);
678 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
680 dump_stats_transaction(vconn, request);
686 static bool fetch_port_by_stats(struct vconn *,
687 const char *port_name, ofp_port_t port_no,
688 struct ofputil_phy_port *);
690 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
691 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
692 * successful, false on failure.
694 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
695 * list of ports in OFPT_FEATURES_REPLY. */
697 fetch_port_by_features(struct vconn *vconn,
698 const char *port_name, ofp_port_t port_no,
699 struct ofputil_phy_port *pp)
701 struct ofputil_switch_features features;
702 const struct ofp_header *oh;
703 struct ofpbuf *request, *reply;
709 /* Fetch the switch's ofp_switch_features. */
710 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
711 vconn_get_version(vconn), 0);
712 run(vconn_transact(vconn, request, &reply),
713 "talking to %s", vconn_get_name(vconn));
715 oh = ofpbuf_data(reply);
716 if (ofptype_decode(&type, ofpbuf_data(reply))
717 || type != OFPTYPE_FEATURES_REPLY) {
718 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
720 if (!ofputil_switch_features_has_ports(reply)) {
721 /* The switch features reply does not contain a complete list of ports.
722 * Probably, there are more ports than will fit into a single 64 kB
723 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
725 ofpbuf_delete(reply);
726 return fetch_port_by_stats(vconn, port_name, port_no, pp);
729 error = ofputil_decode_switch_features(oh, &features, &b);
731 ovs_fatal(0, "%s: failed to decode features reply (%s)",
732 vconn_get_name(vconn), ofperr_to_string(error));
735 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
736 if (port_no != OFPP_NONE
737 ? port_no == pp->port_no
738 : !strcmp(pp->name, port_name)) {
743 ofpbuf_delete(reply);
747 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
748 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
749 * successful, false on failure.
751 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
752 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
753 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
754 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
756 fetch_port_by_stats(struct vconn *vconn,
757 const char *port_name, ofp_port_t port_no,
758 struct ofputil_phy_port *pp)
760 struct ofpbuf *request;
765 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
767 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
769 send_openflow_buffer(vconn, request);
772 struct ofpbuf *reply;
774 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
775 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
776 if (send_xid == recv_xid) {
777 struct ofp_header *oh = ofpbuf_data(reply);
782 ofpbuf_use_const(&b, oh, ntohs(oh->length));
783 if (ofptype_pull(&type, &b)
784 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
785 ovs_fatal(0, "received bad reply: %s",
786 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
790 flags = ofpmp_flags(oh);
791 done = !(flags & OFPSF_REPLY_MORE);
794 /* We've already found the port, but we need to drain
795 * the queue of any other replies for this request. */
799 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
800 if (port_no != OFPP_NONE ? port_no == pp->port_no
801 : !strcmp(pp->name, port_name)) {
807 VLOG_DBG("received reply with xid %08"PRIx32" "
808 "!= expected %08"PRIx32, recv_xid, send_xid);
810 ofpbuf_delete(reply);
817 str_to_ofp(const char *s, ofp_port_t *ofp_port)
822 ret = str_to_uint(s, 10, &port_);
823 *ofp_port = u16_to_ofp(port_);
827 /* Opens a connection to 'vconn_name', fetches the port structure for
828 * 'port_name' (which may be a port name or number), and copies it into
831 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
832 struct ofputil_phy_port *pp)
838 /* Try to interpret the argument as a port number. */
839 if (!str_to_ofp(port_name, &port_no)) {
843 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
844 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
845 * into the OFPST_PORT_DESC reply. Try it the correct way. */
846 open_vconn(vconn_name, &vconn);
847 found = (vconn_get_version(vconn) < OFP13_VERSION
848 ? fetch_port_by_features(vconn, port_name, port_no, pp)
849 : fetch_port_by_stats(vconn, port_name, port_no, pp));
853 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
857 /* Returns the port number corresponding to 'port_name' (which may be a port
858 * name or number) within the switch 'vconn_name'. */
860 str_to_port_no(const char *vconn_name, const char *port_name)
864 if (ofputil_port_from_string(port_name, &port_no)) {
867 struct ofputil_phy_port pp;
869 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
875 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
876 enum ofputil_protocol *cur)
879 struct ofpbuf *request, *reply;
880 enum ofputil_protocol next;
882 request = ofputil_encode_set_protocol(*cur, want, &next);
887 run(vconn_transact_noreply(vconn, request, &reply),
888 "talking to %s", vconn_get_name(vconn));
890 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
891 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
892 vconn_get_name(vconn), s);
894 ofpbuf_delete(reply);
902 static enum ofputil_protocol
903 set_protocol_for_flow_dump(struct vconn *vconn,
904 enum ofputil_protocol cur_protocol,
905 enum ofputil_protocol usable_protocols)
910 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
911 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
912 if (f & usable_protocols & allowed_protocols
913 && try_set_protocol(vconn, f, &cur_protocol)) {
918 usable_s = ofputil_protocols_to_string(usable_protocols);
919 if (usable_protocols & allowed_protocols) {
920 ovs_fatal(0, "switch does not support any of the usable flow "
921 "formats (%s)", usable_s);
923 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
924 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
925 "allowed flow formats (%s)", usable_s, allowed_s);
929 static struct vconn *
930 prepare_dump_flows(int argc, char *argv[], bool aggregate,
931 struct ofpbuf **requestp)
933 enum ofputil_protocol usable_protocols, protocol;
934 struct ofputil_flow_stats_request fsr;
938 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
939 argc > 2 ? argv[2] : "",
942 ovs_fatal(0, "%s", error);
945 protocol = open_vconn(argv[1], &vconn);
946 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
947 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
952 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
954 struct ofpbuf *request;
957 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
958 dump_stats_transaction(vconn, request);
963 compare_flows(const void *afs_, const void *bfs_)
965 const struct ofputil_flow_stats *afs = afs_;
966 const struct ofputil_flow_stats *bfs = bfs_;
967 const struct match *a = &afs->match;
968 const struct match *b = &bfs->match;
969 const struct sort_criterion *sc;
971 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
972 const struct mf_field *f = sc->field;
976 unsigned int a_pri = afs->priority;
977 unsigned int b_pri = bfs->priority;
978 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
982 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
983 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
985 /* Skip the test for sc->order, so that missing fields always
986 * sort to the end whether we're sorting in ascending or
987 * descending order. */
990 union mf_value aval, bval;
992 mf_get_value(f, &a->flow, &aval);
993 mf_get_value(f, &b->flow, &bval);
994 ret = memcmp(&aval, &bval, f->n_bytes);
999 return sc->order == SORT_ASC ? ret : -ret;
1007 ofctl_dump_flows(int argc, char *argv[])
1010 return ofctl_dump_flows__(argc, argv, false);
1012 struct ofputil_flow_stats *fses;
1013 size_t n_fses, allocated_fses;
1014 struct ofpbuf *request;
1015 struct ofpbuf ofpacts;
1016 struct ofpbuf *reply;
1017 struct vconn *vconn;
1022 vconn = prepare_dump_flows(argc, argv, false, &request);
1023 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
1024 send_openflow_buffer(vconn, request);
1027 n_fses = allocated_fses = 0;
1029 ofpbuf_init(&ofpacts, 0);
1031 struct ofputil_flow_stats *fs;
1033 if (n_fses >= allocated_fses) {
1034 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1038 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1042 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1045 ofpbuf_uninit(&ofpacts);
1047 qsort(fses, n_fses, sizeof *fses, compare_flows);
1050 for (i = 0; i < n_fses; i++) {
1052 ofp_print_flow_stats(&s, &fses[i]);
1057 for (i = 0; i < n_fses; i++) {
1058 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1067 ofctl_dump_aggregate(int argc, char *argv[])
1069 return ofctl_dump_flows__(argc, argv, true);
1073 ofctl_queue_stats(int argc, char *argv[])
1075 struct ofpbuf *request;
1076 struct vconn *vconn;
1077 struct ofputil_queue_stats_request oqs;
1079 open_vconn(argv[1], &vconn);
1081 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1082 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1084 oqs.port_no = OFPP_ANY;
1086 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1087 oqs.queue_id = atoi(argv[3]);
1089 oqs.queue_id = OFPQ_ALL;
1092 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1093 dump_stats_transaction(vconn, request);
1098 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1100 const char *vconn_name = argv[1];
1101 const char *port_name = argv[2];
1102 enum ofputil_protocol protocol;
1103 enum ofp_version version;
1104 struct ofpbuf *request;
1105 struct vconn *vconn;
1108 port = str_to_port_no(vconn_name, port_name);
1110 protocol = open_vconn(vconn_name, &vconn);
1111 version = ofputil_protocol_to_ofp_version(protocol);
1112 request = ofputil_encode_queue_get_config_request(version, port);
1113 dump_transaction(vconn, request);
1117 static enum ofputil_protocol
1118 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1119 enum ofputil_protocol usable_protocols)
1121 enum ofputil_protocol cur_protocol;
1125 if (!(usable_protocols & allowed_protocols)) {
1126 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1127 usable_s = ofputil_protocols_to_string(usable_protocols);
1128 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1129 "allowed flow formats (%s)", usable_s, allowed_s);
1132 /* If the initial flow format is allowed and usable, keep it. */
1133 cur_protocol = open_vconn(remote, vconnp);
1134 if (usable_protocols & allowed_protocols & cur_protocol) {
1135 return cur_protocol;
1138 /* Otherwise try each flow format in turn. */
1139 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1140 enum ofputil_protocol f = 1 << i;
1142 if (f != cur_protocol
1143 && f & usable_protocols & allowed_protocols
1144 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1149 usable_s = ofputil_protocols_to_string(usable_protocols);
1150 ovs_fatal(0, "switch does not support any of the usable flow "
1151 "formats (%s)", usable_s);
1155 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1156 size_t n_fms, enum ofputil_protocol usable_protocols)
1158 enum ofputil_protocol protocol;
1159 struct vconn *vconn;
1162 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1164 for (i = 0; i < n_fms; i++) {
1165 struct ofputil_flow_mod *fm = &fms[i];
1167 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1168 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1174 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1176 enum ofputil_protocol usable_protocols;
1177 struct ofputil_flow_mod *fms = NULL;
1181 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1184 ovs_fatal(0, "%s", error);
1186 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1191 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1193 if (argc > 2 && !strcmp(argv[2], "-")) {
1194 ofctl_flow_mod_file(argc, argv, command);
1196 struct ofputil_flow_mod fm;
1198 enum ofputil_protocol usable_protocols;
1200 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1203 ovs_fatal(0, "%s", error);
1205 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1210 ofctl_add_flow(int argc, char *argv[])
1212 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1216 ofctl_add_flows(int argc, char *argv[])
1218 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1222 ofctl_mod_flows(int argc, char *argv[])
1224 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1228 ofctl_del_flows(int argc, char *argv[])
1230 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1234 set_packet_in_format(struct vconn *vconn,
1235 enum nx_packet_in_format packet_in_format)
1237 struct ofpbuf *spif;
1239 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1241 transact_noreply(vconn, spif);
1242 VLOG_DBG("%s: using user-specified packet in format %s",
1243 vconn_get_name(vconn),
1244 ofputil_packet_in_format_to_string(packet_in_format));
1248 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1250 struct ofp_switch_config config;
1251 enum ofp_config_flags flags;
1253 fetch_switch_config(vconn, &config);
1254 flags = ntohs(config.flags);
1255 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1256 /* Set the invalid ttl config. */
1257 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1259 config.flags = htons(flags);
1260 set_switch_config(vconn, &config);
1262 /* Then retrieve the configuration to see if it really took. OpenFlow
1263 * doesn't define error reporting for bad modes, so this is all we can
1265 fetch_switch_config(vconn, &config);
1266 flags = ntohs(config.flags);
1267 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1268 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1269 "switch probably doesn't support mode)");
1276 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1277 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1278 * an error message and stores NULL in '*msgp'. */
1280 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1282 struct ofp_header *oh;
1285 msg = ofpbuf_new(strlen(hex) / 2);
1288 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1290 return "Trailing garbage in hex data";
1293 if (ofpbuf_size(msg) < sizeof(struct ofp_header)) {
1295 return "Message too short for OpenFlow";
1298 oh = ofpbuf_data(msg);
1299 if (ofpbuf_size(msg) != ntohs(oh->length)) {
1301 return "Message size does not match length in OpenFlow header";
1309 ofctl_send(struct unixctl_conn *conn, int argc,
1310 const char *argv[], void *vconn_)
1312 struct vconn *vconn = vconn_;
1319 for (i = 1; i < argc; i++) {
1320 const char *error_msg;
1324 error_msg = openflow_from_hex(argv[i], &msg);
1326 ds_put_format(&reply, "%s\n", error_msg);
1331 fprintf(stderr, "send: ");
1332 ofp_print(stderr, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
1334 error = vconn_send_block(vconn, msg);
1337 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1340 ds_put_cstr(&reply, "sent\n");
1345 unixctl_command_reply(conn, ds_cstr(&reply));
1347 unixctl_command_reply_error(conn, ds_cstr(&reply));
1352 struct barrier_aux {
1353 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1354 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1358 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1359 const char *argv[] OVS_UNUSED, void *aux_)
1361 struct barrier_aux *aux = aux_;
1366 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1370 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1371 error = vconn_send_block(aux->vconn, msg);
1374 unixctl_command_reply_error(conn, ovs_strerror(error));
1381 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1382 const char *argv[], void *aux OVS_UNUSED)
1386 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1388 unixctl_command_reply_error(conn, ovs_strerror(errno));
1393 dup2(fd, STDERR_FILENO);
1395 unixctl_command_reply(conn, NULL);
1399 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1400 const char *argv[] OVS_UNUSED, void *blocked_)
1402 bool *blocked = blocked_;
1406 unixctl_command_reply(conn, NULL);
1408 unixctl_command_reply(conn, "already blocking");
1413 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1414 const char *argv[] OVS_UNUSED, void *blocked_)
1416 bool *blocked = blocked_;
1420 unixctl_command_reply(conn, NULL);
1422 unixctl_command_reply(conn, "already unblocked");
1426 /* Prints to stdout all of the messages received on 'vconn'.
1428 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1429 * received on 'vconn'. */
1431 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1433 struct barrier_aux barrier_aux = { vconn, NULL };
1434 struct unixctl_server *server;
1435 bool exiting = false;
1436 bool blocked = false;
1439 daemon_save_fd(STDERR_FILENO);
1441 error = unixctl_server_create(NULL, &server);
1443 ovs_fatal(error, "failed to create unixctl server");
1445 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1446 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1448 unixctl_command_register("ofctl/barrier", "", 0, 0,
1449 ofctl_barrier, &barrier_aux);
1450 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1451 ofctl_set_output_file, NULL);
1453 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1454 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1457 daemonize_complete();
1463 unixctl_server_run(server);
1468 retval = vconn_recv(vconn, &b);
1469 if (retval == EAGAIN) {
1472 run(retval, "vconn_recv");
1475 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1476 time_wall_msec(), true);
1481 ofptype_decode(&type, ofpbuf_data(b));
1482 ofp_print(stderr, ofpbuf_data(b), ofpbuf_size(b), verbosity + 2);
1485 switch ((int) type) {
1486 case OFPTYPE_BARRIER_REPLY:
1487 if (barrier_aux.conn) {
1488 unixctl_command_reply(barrier_aux.conn, NULL);
1489 barrier_aux.conn = NULL;
1493 case OFPTYPE_ECHO_REQUEST:
1494 if (reply_to_echo_requests) {
1495 struct ofpbuf *reply;
1497 reply = make_echo_reply(ofpbuf_data(b));
1498 retval = vconn_send_block(vconn, reply);
1500 ovs_fatal(retval, "failed to send echo reply");
1513 vconn_run_wait(vconn);
1515 vconn_recv_wait(vconn);
1517 unixctl_server_wait(server);
1521 unixctl_server_destroy(server);
1525 ofctl_monitor(int argc, char *argv[])
1527 struct vconn *vconn;
1529 enum ofputil_protocol usable_protocols;
1531 open_vconn(argv[1], &vconn);
1532 for (i = 2; i < argc; i++) {
1533 const char *arg = argv[i];
1535 if (isdigit((unsigned char) *arg)) {
1536 struct ofp_switch_config config;
1538 fetch_switch_config(vconn, &config);
1539 config.miss_send_len = htons(atoi(arg));
1540 set_switch_config(vconn, &config);
1541 } else if (!strcmp(arg, "invalid_ttl")) {
1542 monitor_set_invalid_ttl_to_controller(vconn);
1543 } else if (!strncmp(arg, "watch:", 6)) {
1544 struct ofputil_flow_monitor_request fmr;
1548 error = parse_flow_monitor_request(&fmr, arg + 6,
1551 ovs_fatal(0, "%s", error);
1554 msg = ofpbuf_new(0);
1555 ofputil_append_flow_monitor_request(&fmr, msg);
1556 dump_stats_transaction(vconn, msg);
1559 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1563 if (preferred_packet_in_format >= 0) {
1564 set_packet_in_format(vconn, preferred_packet_in_format);
1566 enum ofp_version version = vconn_get_version(vconn);
1569 case OFP10_VERSION: {
1570 struct ofpbuf *spif, *reply;
1572 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1574 run(vconn_transact_noreply(vconn, spif, &reply),
1575 "talking to %s", vconn_get_name(vconn));
1577 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
1578 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1579 " replied: %s. Falling back to the switch default.",
1580 vconn_get_name(vconn), s);
1582 ofpbuf_delete(reply);
1597 monitor_vconn(vconn, true);
1601 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1603 struct vconn *vconn;
1605 open_vconn__(argv[1], SNOOP, &vconn);
1606 monitor_vconn(vconn, false);
1610 ofctl_dump_ports(int argc, char *argv[])
1612 struct ofpbuf *request;
1613 struct vconn *vconn;
1616 open_vconn(argv[1], &vconn);
1617 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1618 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1619 dump_stats_transaction(vconn, request);
1624 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1626 struct ofpbuf *request;
1627 struct vconn *vconn;
1630 open_vconn(argv[1], &vconn);
1631 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1632 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1634 dump_stats_transaction(vconn, request);
1639 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1641 struct ofpbuf *request;
1642 struct vconn *vconn;
1643 struct ofpbuf *reply;
1645 open_vconn(argv[1], &vconn);
1646 request = make_echo_request(vconn_get_version(vconn));
1647 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1648 if (ofpbuf_size(reply) != sizeof(struct ofp_header)) {
1649 ovs_fatal(0, "reply does not match request");
1651 ofpbuf_delete(reply);
1656 ofctl_packet_out(int argc, char *argv[])
1658 enum ofputil_protocol protocol;
1659 struct ofputil_packet_out po;
1660 struct ofpbuf ofpacts;
1661 struct vconn *vconn;
1664 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1666 ofpbuf_init(&ofpacts, 64);
1667 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1669 ovs_fatal(0, "%s", error);
1672 po.buffer_id = UINT32_MAX;
1673 po.in_port = str_to_port_no(argv[1], argv[2]);
1674 po.ofpacts = ofpbuf_data(&ofpacts);
1675 po.ofpacts_len = ofpbuf_size(&ofpacts);
1677 protocol = open_vconn(argv[1], &vconn);
1678 for (i = 4; i < argc; i++) {
1679 struct ofpbuf *packet, *opo;
1680 const char *error_msg;
1682 error_msg = eth_from_hex(argv[i], &packet);
1684 ovs_fatal(0, "%s", error_msg);
1687 po.packet = ofpbuf_data(packet);
1688 po.packet_len = ofpbuf_size(packet);
1689 opo = ofputil_encode_packet_out(&po, protocol);
1690 transact_noreply(vconn, opo);
1691 ofpbuf_delete(packet);
1694 ofpbuf_uninit(&ofpacts);
1698 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1700 struct ofp_config_flag {
1701 const char *name; /* The flag's name. */
1702 enum ofputil_port_config bit; /* Bit to turn on or off. */
1703 bool on; /* Value to set the bit to. */
1705 static const struct ofp_config_flag flags[] = {
1706 { "up", OFPUTIL_PC_PORT_DOWN, false },
1707 { "down", OFPUTIL_PC_PORT_DOWN, true },
1708 { "stp", OFPUTIL_PC_NO_STP, false },
1709 { "receive", OFPUTIL_PC_NO_RECV, false },
1710 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1711 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1712 { "forward", OFPUTIL_PC_NO_FWD, false },
1713 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1716 const struct ofp_config_flag *flag;
1717 enum ofputil_protocol protocol;
1718 struct ofputil_port_mod pm;
1719 struct ofputil_phy_port pp;
1720 struct vconn *vconn;
1721 const char *command;
1724 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1726 pm.port_no = pp.port_no;
1727 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1732 if (!strncasecmp(argv[3], "no-", 3)) {
1733 command = argv[3] + 3;
1735 } else if (!strncasecmp(argv[3], "no", 2)) {
1736 command = argv[3] + 2;
1742 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1743 if (!strcasecmp(command, flag->name)) {
1744 pm.mask = flag->bit;
1745 pm.config = flag->on ^ not ? flag->bit : 0;
1749 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1752 protocol = open_vconn(argv[1], &vconn);
1753 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1758 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1760 enum ofputil_protocol protocol, usable_protocols;
1761 struct ofputil_table_mod tm;
1762 struct vconn *vconn;
1766 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1768 ovs_fatal(0, "%s", error);
1771 protocol = open_vconn(argv[1], &vconn);
1772 if (!(protocol & usable_protocols)) {
1773 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1774 enum ofputil_protocol f = 1 << i;
1776 && f & usable_protocols
1777 && try_set_protocol(vconn, f, &protocol)) {
1784 if (!(protocol & usable_protocols)) {
1785 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1786 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1789 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1794 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1796 struct ofp_switch_config config;
1797 struct vconn *vconn;
1799 open_vconn(argv[1], &vconn);
1800 fetch_switch_config(vconn, &config);
1801 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1806 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1808 struct ofp_switch_config config;
1809 enum ofp_config_flags mode;
1810 struct vconn *vconn;
1813 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1814 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1817 open_vconn(argv[1], &vconn);
1818 fetch_switch_config(vconn, &config);
1819 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1820 if (flags != config.flags) {
1821 /* Set the configuration. */
1822 config.flags = flags;
1823 set_switch_config(vconn, &config);
1825 /* Then retrieve the configuration to see if it really took. OpenFlow
1826 * doesn't define error reporting for bad modes, so this is all we can
1828 fetch_switch_config(vconn, &config);
1829 if (flags != config.flags) {
1830 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1831 "switch probably doesn't support mode \"%s\")",
1832 argv[1], ofputil_frag_handling_to_string(mode));
1839 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1841 const char *filename = argv[1];
1845 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1847 ovs_fatal(errno, "%s: open", filename);
1850 ofpbuf_init(&b, 65536);
1852 struct ofp_header *oh;
1853 size_t length, tail_len;
1858 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1859 n = fread(oh, 1, sizeof *oh, file);
1862 } else if (n < sizeof *oh) {
1863 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1866 length = ntohs(oh->length);
1867 if (length < sizeof *oh) {
1868 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1872 tail_len = length - sizeof *oh;
1873 tail = ofpbuf_put_uninit(&b, tail_len);
1874 n = fread(tail, 1, tail_len, file);
1876 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1879 ofp_print(stdout, ofpbuf_data(&b), ofpbuf_size(&b), verbosity + 2);
1883 if (file != stdin) {
1889 is_openflow_port(ovs_be16 port_, char *ports[])
1891 uint16_t port = ntohs(port_);
1895 for (i = 0; ports[i]; i++) {
1896 if (port == atoi(ports[i])) {
1902 return port == OFP_PORT || port == OFP_OLD_PORT;
1907 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1909 struct tcp_reader *reader;
1914 file = ovs_pcap_open(argv[1], "rb");
1916 ovs_fatal(errno, "%s: open failed", argv[1]);
1919 reader = tcp_reader_open();
1922 struct ofpbuf *packet;
1925 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1927 error = ovs_pcap_read(file, &packet, &when);
1931 flow_extract(packet, &md, &flow);
1932 if (flow.dl_type == htons(ETH_TYPE_IP)
1933 && flow.nw_proto == IPPROTO_TCP
1934 && (is_openflow_port(flow.tp_src, argv + 2) ||
1935 is_openflow_port(flow.tp_dst, argv + 2))) {
1936 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1938 while (ofpbuf_size(payload) >= sizeof(struct ofp_header)) {
1939 const struct ofp_header *oh;
1940 void *data = ofpbuf_data(payload);
1943 /* Align OpenFlow on 8-byte boundary for safe access. */
1944 ofpbuf_shift(payload, -((intptr_t) data & 7));
1946 oh = ofpbuf_data(payload);
1947 length = ntohs(oh->length);
1948 if (ofpbuf_size(payload) < length) {
1958 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1963 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1964 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1965 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1966 ofp_print(stdout, ofpbuf_data(payload), length, verbosity + 1);
1967 ofpbuf_pull(payload, length);
1971 ofpbuf_delete(packet);
1973 tcp_reader_close(reader);
1977 ofctl_ping(int argc, char *argv[])
1979 size_t max_payload = 65535 - sizeof(struct ofp_header);
1980 unsigned int payload;
1981 struct vconn *vconn;
1984 payload = argc > 2 ? atoi(argv[2]) : 64;
1985 if (payload > max_payload) {
1986 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1989 open_vconn(argv[1], &vconn);
1990 for (i = 0; i < 10; i++) {
1991 struct timeval start, end;
1992 struct ofpbuf *request, *reply;
1993 const struct ofp_header *rpy_hdr;
1996 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1997 vconn_get_version(vconn), payload);
1998 random_bytes(ofpbuf_put_uninit(request, payload), payload);
2000 xgettimeofday(&start);
2001 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2002 xgettimeofday(&end);
2004 rpy_hdr = ofpbuf_data(reply);
2005 if (ofptype_pull(&type, reply)
2006 || type != OFPTYPE_ECHO_REPLY
2007 || ofpbuf_size(reply) != payload
2008 || memcmp(ofpbuf_l3(request), ofpbuf_l3(reply), payload)) {
2009 printf("Reply does not match request. Request:\n");
2010 ofp_print(stdout, request, ofpbuf_size(request), verbosity + 2);
2012 ofp_print(stdout, reply, ofpbuf_size(reply), verbosity + 2);
2014 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2015 ofpbuf_size(reply), argv[1], ntohl(rpy_hdr->xid),
2016 (1000*(double)(end.tv_sec - start.tv_sec))
2017 + (.001*(end.tv_usec - start.tv_usec)));
2018 ofpbuf_delete(request);
2019 ofpbuf_delete(reply);
2025 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
2027 size_t max_payload = 65535 - sizeof(struct ofp_header);
2028 struct timeval start, end;
2029 unsigned int payload_size, message_size;
2030 struct vconn *vconn;
2035 payload_size = atoi(argv[2]);
2036 if (payload_size > max_payload) {
2037 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2039 message_size = sizeof(struct ofp_header) + payload_size;
2041 count = atoi(argv[3]);
2043 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2044 count, message_size, count * message_size);
2046 open_vconn(argv[1], &vconn);
2047 xgettimeofday(&start);
2048 for (i = 0; i < count; i++) {
2049 struct ofpbuf *request, *reply;
2051 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2052 vconn_get_version(vconn), payload_size);
2053 ofpbuf_put_zeros(request, payload_size);
2054 run(vconn_transact(vconn, request, &reply), "transact");
2055 ofpbuf_delete(reply);
2057 xgettimeofday(&end);
2060 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2061 + (.001*(end.tv_usec - start.tv_usec)));
2062 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2063 duration, count / (duration / 1000.0),
2064 count * message_size / (duration / 1000.0));
2068 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2071 struct ofputil_group_mod *gm;
2072 struct ofpbuf *request;
2074 struct vconn *vconn;
2077 open_vconn(remote, &vconn);
2079 for (i = 0; i < n_gms; i++) {
2081 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2083 transact_noreply(vconn, request);
2093 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2095 struct ofputil_group_mod *gms = NULL;
2096 enum ofputil_protocol usable_protocols;
2100 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2103 ovs_fatal(0, "%s", error);
2105 ofctl_group_mod__(argv[1], gms, n_gms);
2110 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2112 if (argc > 2 && !strcmp(argv[2], "-")) {
2113 ofctl_group_mod_file(argc, argv, command);
2115 enum ofputil_protocol usable_protocols;
2116 struct ofputil_group_mod gm;
2119 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2122 ovs_fatal(0, "%s", error);
2124 ofctl_group_mod__(argv[1], &gm, 1);
2129 ofctl_add_group(int argc, char *argv[])
2131 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2135 ofctl_add_groups(int argc, char *argv[])
2137 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2141 ofctl_mod_group(int argc, char *argv[])
2143 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2147 ofctl_del_groups(int argc, char *argv[])
2149 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2153 ofctl_dump_group_stats(int argc, char *argv[])
2155 enum ofputil_protocol usable_protocols;
2156 struct ofputil_group_mod gm;
2157 struct ofpbuf *request;
2158 struct vconn *vconn;
2162 memset(&gm, 0, sizeof gm);
2164 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2165 argc > 2 ? argv[2] : "",
2168 ovs_fatal(0, "%s", error);
2171 group_id = gm.group_id;
2173 open_vconn(argv[1], &vconn);
2174 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2177 dump_stats_transaction(vconn, request);
2184 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2186 struct ofpbuf *request;
2187 struct vconn *vconn;
2190 open_vconn(argv[1], &vconn);
2192 if (argc < 3 || !ofputil_group_from_string(argv[2], &group_id)) {
2193 group_id = OFPG11_ALL;
2196 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2199 dump_stats_transaction(vconn, request);
2206 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2208 struct ofpbuf *request;
2209 struct vconn *vconn;
2211 open_vconn(argv[1], &vconn);
2212 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2214 dump_stats_transaction(vconn, request);
2221 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2226 /* replace-flows and diff-flows commands. */
2228 /* A flow table entry, possibly with two different versions. */
2230 struct cls_rule rule; /* Within a "struct classifier". */
2231 struct fte_version *versions[2];
2234 /* One version of a Flow Table Entry. */
2235 struct fte_version {
2237 uint16_t idle_timeout;
2238 uint16_t hard_timeout;
2240 struct ofpact *ofpacts;
2244 /* Frees 'version' and the data that it owns. */
2246 fte_version_free(struct fte_version *version)
2249 free(CONST_CAST(struct ofpact *, version->ofpacts));
2254 /* Returns true if 'a' and 'b' are the same, false if they differ.
2256 * Ignores differences in 'flags' because there's no way to retrieve flags from
2257 * an OpenFlow switch. We have to assume that they are the same. */
2259 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2261 return (a->cookie == b->cookie
2262 && a->idle_timeout == b->idle_timeout
2263 && a->hard_timeout == b->hard_timeout
2264 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2265 b->ofpacts, b->ofpacts_len));
2268 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2269 * 'index' into 's', followed by a new-line. */
2271 fte_version_format(const struct fte *fte, int index, struct ds *s)
2273 const struct fte_version *version = fte->versions[index];
2280 cls_rule_format(&fte->rule, s);
2281 if (version->cookie != htonll(0)) {
2282 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2284 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2285 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2287 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2288 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2291 ds_put_cstr(s, " actions=");
2292 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2294 ds_put_char(s, '\n');
2298 fte_from_cls_rule(const struct cls_rule *cls_rule)
2300 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2303 /* Frees 'fte' and its versions. */
2305 fte_free(struct fte *fte)
2308 fte_version_free(fte->versions[0]);
2309 fte_version_free(fte->versions[1]);
2310 cls_rule_destroy(&fte->rule);
2315 /* Frees all of the FTEs within 'cls'. */
2317 fte_free_all(struct classifier *cls)
2319 struct cls_cursor cursor;
2320 struct fte *fte, *next;
2322 fat_rwlock_wrlock(&cls->rwlock);
2323 cls_cursor_init(&cursor, cls, NULL);
2324 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2325 classifier_remove(cls, &fte->rule);
2328 fat_rwlock_unlock(&cls->rwlock);
2329 classifier_destroy(cls);
2332 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2333 * necessary. Sets 'version' as the version of that rule with the given
2334 * 'index', replacing any existing version, if any.
2336 * Takes ownership of 'version'. */
2338 fte_insert(struct classifier *cls, const struct match *match,
2339 unsigned int priority, struct fte_version *version, int index)
2341 struct fte *old, *fte;
2343 fte = xzalloc(sizeof *fte);
2344 cls_rule_init(&fte->rule, match, priority);
2345 fte->versions[index] = version;
2347 fat_rwlock_wrlock(&cls->rwlock);
2348 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2349 fat_rwlock_unlock(&cls->rwlock);
2351 fte_version_free(old->versions[index]);
2352 fte->versions[!index] = old->versions[!index];
2353 cls_rule_destroy(&old->rule);
2358 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2359 * with the specified 'index'. Returns the flow formats able to represent the
2360 * flows that were read. */
2361 static enum ofputil_protocol
2362 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2364 enum ofputil_protocol usable_protocols;
2369 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2371 ovs_fatal(errno, "%s: open", filename);
2375 usable_protocols = OFPUTIL_P_ANY;
2377 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2378 struct fte_version *version;
2379 struct ofputil_flow_mod fm;
2381 enum ofputil_protocol usable;
2383 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2385 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2387 usable_protocols &= usable;
2389 version = xmalloc(sizeof *version);
2390 version->cookie = fm.new_cookie;
2391 version->idle_timeout = fm.idle_timeout;
2392 version->hard_timeout = fm.hard_timeout;
2393 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2394 | OFPUTIL_FF_EMERG);
2395 version->ofpacts = fm.ofpacts;
2396 version->ofpacts_len = fm.ofpacts_len;
2398 fte_insert(cls, &fm.match, fm.priority, version, index);
2402 if (file != stdin) {
2406 return usable_protocols;
2410 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2411 struct ofpbuf **replyp,
2412 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2414 struct ofpbuf *reply = *replyp;
2420 /* Get a flow stats reply message, if we don't already have one. */
2426 run(vconn_recv_block(vconn, &reply),
2427 "OpenFlow packet receive failed");
2428 } while (((struct ofp_header *) ofpbuf_data(reply))->xid != send_xid);
2430 error = ofptype_decode(&type, ofpbuf_data(reply));
2431 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2432 ovs_fatal(0, "received bad reply: %s",
2433 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
2438 /* Pull an individual flow stats reply out of the message. */
2439 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2446 more = ofpmp_more(reply->frame);
2447 ofpbuf_delete(reply);
2456 ovs_fatal(0, "parse error in reply (%s)",
2457 ofperr_to_string(retval));
2462 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2463 * format 'protocol', and adds them as flow table entries in 'cls' for the
2464 * version with the specified 'index'. */
2466 read_flows_from_switch(struct vconn *vconn,
2467 enum ofputil_protocol protocol,
2468 struct classifier *cls, int index)
2470 struct ofputil_flow_stats_request fsr;
2471 struct ofputil_flow_stats fs;
2472 struct ofpbuf *request;
2473 struct ofpbuf ofpacts;
2474 struct ofpbuf *reply;
2477 fsr.aggregate = false;
2478 match_init_catchall(&fsr.match);
2479 fsr.out_port = OFPP_ANY;
2480 fsr.table_id = 0xff;
2481 fsr.cookie = fsr.cookie_mask = htonll(0);
2482 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2483 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
2484 send_openflow_buffer(vconn, request);
2487 ofpbuf_init(&ofpacts, 0);
2488 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2489 struct fte_version *version;
2491 version = xmalloc(sizeof *version);
2492 version->cookie = fs.cookie;
2493 version->idle_timeout = fs.idle_timeout;
2494 version->hard_timeout = fs.hard_timeout;
2496 version->ofpacts_len = fs.ofpacts_len;
2497 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2499 fte_insert(cls, &fs.match, fs.priority, version, index);
2501 ofpbuf_uninit(&ofpacts);
2505 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2506 enum ofputil_protocol protocol, struct list *packets)
2508 const struct fte_version *version = fte->versions[index];
2509 struct ofputil_flow_mod fm;
2512 minimatch_expand(&fte->rule.match, &fm.match);
2513 fm.priority = fte->rule.priority;
2514 fm.cookie = htonll(0);
2515 fm.cookie_mask = htonll(0);
2516 fm.new_cookie = version->cookie;
2517 fm.modify_cookie = true;
2519 fm.command = command;
2520 fm.idle_timeout = version->idle_timeout;
2521 fm.hard_timeout = version->hard_timeout;
2522 fm.buffer_id = UINT32_MAX;
2523 fm.out_port = OFPP_ANY;
2524 fm.flags = version->flags;
2525 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2526 command == OFPFC_MODIFY_STRICT) {
2527 fm.ofpacts = version->ofpacts;
2528 fm.ofpacts_len = version->ofpacts_len;
2533 fm.delete_reason = OFPRR_DELETE;
2535 ofm = ofputil_encode_flow_mod(&fm, protocol);
2536 list_push_back(packets, &ofm->list_node);
2540 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2542 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2543 enum ofputil_protocol usable_protocols, protocol;
2544 struct cls_cursor cursor;
2545 struct classifier cls;
2546 struct list requests;
2547 struct vconn *vconn;
2550 classifier_init(&cls, NULL);
2551 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2553 protocol = open_vconn(argv[1], &vconn);
2554 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2556 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2558 list_init(&requests);
2560 /* Delete flows that exist on the switch but not in the file. */
2561 fat_rwlock_rdlock(&cls.rwlock);
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];
2567 if (sw_ver && !file_ver) {
2568 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2569 protocol, &requests);
2573 /* Add flows that exist in the file but not on the switch.
2574 * Update flows that exist in both places but differ. */
2575 cls_cursor_init(&cursor, &cls, NULL);
2576 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2577 struct fte_version *file_ver = fte->versions[FILE_IDX];
2578 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2581 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2582 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2585 fat_rwlock_unlock(&cls.rwlock);
2586 transact_multiple_noreply(vconn, &requests);
2593 read_flows_from_source(const char *source, struct classifier *cls, int index)
2597 if (source[0] == '/' || source[0] == '.'
2598 || (!strchr(source, ':') && !stat(source, &s))) {
2599 read_flows_from_file(source, cls, index);
2601 enum ofputil_protocol protocol;
2602 struct vconn *vconn;
2604 protocol = open_vconn(source, &vconn);
2605 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2606 read_flows_from_switch(vconn, protocol, cls, index);
2612 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2614 bool differences = false;
2615 struct cls_cursor cursor;
2616 struct classifier cls;
2620 classifier_init(&cls, NULL);
2621 read_flows_from_source(argv[1], &cls, 0);
2622 read_flows_from_source(argv[2], &cls, 1);
2627 fat_rwlock_rdlock(&cls.rwlock);
2628 cls_cursor_init(&cursor, &cls, NULL);
2629 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2630 struct fte_version *a = fte->versions[0];
2631 struct fte_version *b = fte->versions[1];
2633 if (!a || !b || !fte_version_equals(a, b)) {
2634 fte_version_format(fte, 0, &a_s);
2635 fte_version_format(fte, 1, &b_s);
2636 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2638 printf("-%s", ds_cstr(&a_s));
2641 printf("+%s", ds_cstr(&b_s));
2647 fat_rwlock_unlock(&cls.rwlock);
2660 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2662 struct ofputil_meter_mod mm;
2663 struct vconn *vconn;
2664 enum ofputil_protocol protocol;
2665 enum ofputil_protocol usable_protocols;
2666 enum ofp_version version;
2670 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2672 ovs_fatal(0, "%s", error);
2675 usable_protocols = OFPUTIL_P_OF13_UP;
2676 mm.command = command;
2677 mm.meter.meter_id = OFPM13_ALL;
2680 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2681 version = ofputil_protocol_to_ofp_version(protocol);
2682 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2687 ofctl_meter_request__(const char *bridge, const char *str,
2688 enum ofputil_meter_request_type type)
2690 struct ofputil_meter_mod mm;
2691 struct vconn *vconn;
2692 enum ofputil_protocol usable_protocols;
2693 enum ofputil_protocol protocol;
2694 enum ofp_version version;
2698 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2700 ovs_fatal(0, "%s", error);
2703 usable_protocols = OFPUTIL_P_OF13_UP;
2704 mm.meter.meter_id = OFPM13_ALL;
2707 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2708 version = ofputil_protocol_to_ofp_version(protocol);
2709 transact_noreply(vconn, ofputil_encode_meter_request(version,
2711 mm.meter.meter_id));
2717 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2719 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2723 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2725 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2729 ofctl_del_meters(int argc, char *argv[])
2731 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2735 ofctl_dump_meters(int argc, char *argv[])
2737 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2738 OFPUTIL_METER_CONFIG);
2742 ofctl_meter_stats(int argc, char *argv[])
2744 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2745 OFPUTIL_METER_STATS);
2749 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2751 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2755 /* Undocumented commands for unit testing. */
2758 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2759 enum ofputil_protocol usable_protocols)
2761 enum ofputil_protocol protocol = 0;
2765 usable_s = ofputil_protocols_to_string(usable_protocols);
2766 printf("usable protocols: %s\n", usable_s);
2769 if (!(usable_protocols & allowed_protocols)) {
2770 ovs_fatal(0, "no usable protocol");
2772 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2774 if (protocol & usable_protocols & allowed_protocols) {
2778 ovs_assert(is_pow2(protocol));
2780 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2782 for (i = 0; i < n_fms; i++) {
2783 struct ofputil_flow_mod *fm = &fms[i];
2786 msg = ofputil_encode_flow_mod(fm, protocol);
2787 ofp_print(stdout, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
2790 free(CONST_CAST(struct ofpact *, fm->ofpacts));
2794 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2795 * it back to stdout. */
2797 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2799 enum ofputil_protocol usable_protocols;
2800 struct ofputil_flow_mod fm;
2803 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2805 ovs_fatal(0, "%s", error);
2807 ofctl_parse_flows__(&fm, 1, usable_protocols);
2810 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2811 * add-flows) and prints each of the flows back to stdout. */
2813 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2815 enum ofputil_protocol usable_protocols;
2816 struct ofputil_flow_mod *fms = NULL;
2820 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2823 ovs_fatal(0, "%s", error);
2825 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2830 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
2835 while (!ds_get_test_line(&in, stdin)) {
2836 struct ofpbuf nx_match;
2838 ovs_be64 cookie, cookie_mask;
2842 /* Convert string to nx_match. */
2843 ofpbuf_init(&nx_match, 0);
2845 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2847 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2850 /* Convert nx_match to match. */
2853 error = oxm_pull_match(&nx_match, &match);
2855 error = nx_pull_match(&nx_match, match_len, &match,
2856 &cookie, &cookie_mask);
2860 error = oxm_pull_match_loose(&nx_match, &match);
2862 error = nx_pull_match_loose(&nx_match, match_len, &match,
2863 &cookie, &cookie_mask);
2871 /* Convert match back to nx_match. */
2872 ofpbuf_uninit(&nx_match);
2873 ofpbuf_init(&nx_match, 0);
2875 match_len = oxm_put_match(&nx_match, &match, version);
2876 out = oxm_match_to_string(&nx_match, match_len);
2878 match_len = nx_put_match(&nx_match, &match,
2879 cookie, cookie_mask);
2880 out = nx_match_to_string(ofpbuf_data(&nx_match), match_len);
2886 printf("nx_pull_match() returned error %s\n",
2887 ofperr_get_name(error));
2890 ofpbuf_uninit(&nx_match);
2895 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2896 * stdin, does some internal fussing with them, and then prints them back as
2897 * strings on stdout. */
2899 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2901 return ofctl_parse_nxm__(false, 0);
2904 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
2905 * strings from stdin, does some internal fussing with them, and then prints
2906 * them back as strings on stdout. VERSION must specify an OpenFlow version,
2907 * e.g. "OpenFlow12". */
2909 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[])
2911 enum ofp_version version = ofputil_version_from_string(argv[1]);
2912 if (version < OFP12_VERSION) {
2913 ovs_fatal(0, "%s: not a valid version for OXM", argv[1]);
2916 return ofctl_parse_nxm__(true, version);
2920 print_differences(const char *prefix,
2921 const void *a_, size_t a_len,
2922 const void *b_, size_t b_len)
2924 const uint8_t *a = a_;
2925 const uint8_t *b = b_;
2928 for (i = 0; i < MIN(a_len, b_len); i++) {
2930 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2931 prefix, i, a[i], b[i]);
2934 for (i = a_len; i < b_len; i++) {
2935 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2937 for (i = b_len; i < a_len; i++) {
2938 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2942 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2943 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2944 * on stdout, and then converts them back to hex bytes and prints any
2945 * differences from the input. */
2947 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2952 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2953 struct ofpbuf of10_out;
2954 struct ofpbuf of10_in;
2955 struct ofpbuf ofpacts;
2960 /* Parse hex bytes. */
2961 ofpbuf_init(&of10_in, 0);
2962 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2963 ovs_fatal(0, "Trailing garbage in hex data");
2966 /* Convert to ofpacts. */
2967 ofpbuf_init(&ofpacts, 0);
2968 size = ofpbuf_size(&of10_in);
2969 error = ofpacts_pull_openflow_actions(&of10_in, ofpbuf_size(&of10_in),
2970 OFP10_VERSION, &ofpacts);
2972 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2973 ofpbuf_uninit(&ofpacts);
2974 ofpbuf_uninit(&of10_in);
2977 ofpbuf_push_uninit(&of10_in, size);
2979 /* Print cls_rule. */
2981 ds_put_cstr(&s, "actions=");
2982 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
2986 /* Convert back to ofp10 actions and print differences from input. */
2987 ofpbuf_init(&of10_out, 0);
2988 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of10_out,
2991 print_differences("", ofpbuf_data(&of10_in), ofpbuf_size(&of10_in),
2992 ofpbuf_data(&of10_out), ofpbuf_size(&of10_out));
2995 ofpbuf_uninit(&ofpacts);
2996 ofpbuf_uninit(&of10_in);
2997 ofpbuf_uninit(&of10_out);
3002 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3003 * bytes from stdin, converts them to cls_rules, prints them as strings on
3004 * stdout, and then converts them back to hex bytes and prints any differences
3007 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3008 * values are ignored in the input and will be set to zero when OVS converts
3009 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3010 * it does the conversion to hex, to ensure that in fact they are ignored. */
3012 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3019 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3020 struct ofpbuf match_in, match_expout;
3021 struct ofp10_match match_out;
3022 struct ofp10_match match_normal;
3026 /* Parse hex bytes to use for expected output. */
3028 ds_put_cstr(&expout, ds_cstr(&in));
3029 for (p = ds_cstr(&expout); *p; p++) {
3034 ofpbuf_init(&match_expout, 0);
3035 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3036 ovs_fatal(0, "Trailing garbage in hex data");
3038 if (ofpbuf_size(&match_expout) != sizeof(struct ofp10_match)) {
3039 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3040 ofpbuf_size(&match_expout), sizeof(struct ofp10_match));
3043 /* Parse hex bytes for input. */
3044 for (p = ds_cstr(&in); *p; p++) {
3046 *p = "0123456789abcdef"[random_uint32() & 0xf];
3049 ofpbuf_init(&match_in, 0);
3050 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3051 ovs_fatal(0, "Trailing garbage in hex data");
3053 if (ofpbuf_size(&match_in) != sizeof(struct ofp10_match)) {
3054 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3055 ofpbuf_size(&match_in), sizeof(struct ofp10_match));
3058 /* Convert to cls_rule and print. */
3059 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in), &match);
3060 match_print(&match);
3062 /* Convert back to ofp10_match and print differences from input. */
3063 ofputil_match_to_ofp10_match(&match, &match_out);
3064 print_differences("", ofpbuf_data(&match_expout), ofpbuf_size(&match_expout),
3065 &match_out, sizeof match_out);
3067 /* Normalize, then convert and compare again. */
3068 ofputil_normalize_match(&match);
3069 ofputil_match_to_ofp10_match(&match, &match_normal);
3070 print_differences("normal: ", &match_out, sizeof match_out,
3071 &match_normal, sizeof match_normal);
3074 ofpbuf_uninit(&match_in);
3075 ofpbuf_uninit(&match_expout);
3078 ds_destroy(&expout);
3081 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3082 * bytes from stdin, converts them to "struct match"es, prints them as strings
3083 * on stdout, and then converts them back to hex bytes and prints any
3084 * differences from the input. */
3086 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3091 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3092 struct ofpbuf match_in;
3093 struct ofp11_match match_out;
3097 /* Parse hex bytes. */
3098 ofpbuf_init(&match_in, 0);
3099 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3100 ovs_fatal(0, "Trailing garbage in hex data");
3102 if (ofpbuf_size(&match_in) != sizeof(struct ofp11_match)) {
3103 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3104 ofpbuf_size(&match_in), sizeof(struct ofp11_match));
3107 /* Convert to match. */
3108 error = ofputil_match_from_ofp11_match(ofpbuf_data(&match_in), &match);
3110 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3111 ofpbuf_uninit(&match_in);
3116 match_print(&match);
3118 /* Convert back to ofp11_match and print differences from input. */
3119 ofputil_match_to_ofp11_match(&match, &match_out);
3121 print_differences("", ofpbuf_data(&match_in), ofpbuf_size(&match_in),
3122 &match_out, sizeof match_out);
3125 ofpbuf_uninit(&match_in);
3130 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3131 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3132 * on stdout, and then converts them back to hex bytes and prints any
3133 * differences from the input. */
3135 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3140 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3141 struct ofpbuf of11_out;
3142 struct ofpbuf of11_in;
3143 struct ofpbuf ofpacts;
3148 /* Parse hex bytes. */
3149 ofpbuf_init(&of11_in, 0);
3150 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3151 ovs_fatal(0, "Trailing garbage in hex data");
3154 /* Convert to ofpacts. */
3155 ofpbuf_init(&ofpacts, 0);
3156 size = ofpbuf_size(&of11_in);
3157 error = ofpacts_pull_openflow_actions(&of11_in, ofpbuf_size(&of11_in),
3158 OFP11_VERSION, &ofpacts);
3160 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3161 ofpbuf_uninit(&ofpacts);
3162 ofpbuf_uninit(&of11_in);
3165 ofpbuf_push_uninit(&of11_in, size);
3167 /* Print cls_rule. */
3169 ds_put_cstr(&s, "actions=");
3170 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3174 /* Convert back to ofp11 actions and print differences from input. */
3175 ofpbuf_init(&of11_out, 0);
3176 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of11_out,
3179 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3180 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3183 ofpbuf_uninit(&ofpacts);
3184 ofpbuf_uninit(&of11_in);
3185 ofpbuf_uninit(&of11_out);
3190 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3191 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3192 * them as strings on stdout, and then converts them back to hex bytes and
3193 * prints any differences from the input. */
3195 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3200 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3201 struct ofpbuf of11_out;
3202 struct ofpbuf of11_in;
3203 struct ofpbuf ofpacts;
3207 const char *table_id;
3210 /* Parse table_id separated with the follow-up instructions by ",", if
3212 instructions = ds_cstr(&in);
3214 if (strstr(instructions, ",")) {
3215 table_id = strsep(&instructions, ",");
3218 /* Parse hex bytes. */
3219 ofpbuf_init(&of11_in, 0);
3220 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3221 ovs_fatal(0, "Trailing garbage in hex data");
3224 /* Convert to ofpacts. */
3225 ofpbuf_init(&ofpacts, 0);
3226 size = ofpbuf_size(&of11_in);
3227 error = ofpacts_pull_openflow_instructions(&of11_in, ofpbuf_size(&of11_in),
3228 OFP11_VERSION, &ofpacts);
3230 /* Verify actions, enforce consistency. */
3232 memset(&flow, 0, sizeof flow);
3233 error = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3235 table_id ? atoi(table_id) : 0,
3236 255, OFPUTIL_P_OF11_STD);
3239 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3240 ofpbuf_uninit(&ofpacts);
3241 ofpbuf_uninit(&of11_in);
3244 ofpbuf_push_uninit(&of11_in, size);
3246 /* Print cls_rule. */
3248 ds_put_cstr(&s, "actions=");
3249 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3253 /* Convert back to ofp11 instructions and print differences from
3255 ofpbuf_init(&of11_out, 0);
3256 ofpacts_put_openflow_instructions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3257 &of11_out, OFP13_VERSION);
3259 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3260 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3263 ofpbuf_uninit(&ofpacts);
3264 ofpbuf_uninit(&of11_in);
3265 ofpbuf_uninit(&of11_out);
3270 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3272 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3276 pcap = ovs_pcap_open(argv[1], "rb");
3278 ovs_fatal(errno, "%s: open failed", argv[1]);
3282 struct ofpbuf *packet;
3284 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3287 error = ovs_pcap_read(pcap, &packet, NULL);
3291 ovs_fatal(error, "%s: read failed", argv[1]);
3294 flow_extract(packet, &md, &flow);
3295 flow_print(stdout, &flow);
3297 ofpbuf_delete(packet);
3301 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3302 * mask values to and from various formats and prints the results. */
3304 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3309 struct ofputil_flow_mod fm;
3312 struct match nxm_match;
3316 struct ofp10_match of10_raw;
3317 struct match of10_match;
3319 struct ofp11_match of11_raw;
3320 struct match of11_match;
3325 enum ofputil_protocol usable_protocols; /* Unused for now. */
3327 match_init_catchall(&match);
3328 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3329 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3331 /* Convert to and from string. */
3332 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3333 printf("%s -> ", string_s);
3335 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3337 ovs_fatal(0, "%s", error_s);
3339 printf("%04"PRIx16"/%04"PRIx16"\n",
3340 ntohs(fm.match.flow.vlan_tci),
3341 ntohs(fm.match.wc.masks.vlan_tci));
3344 /* Convert to and from NXM. */
3345 ofpbuf_init(&nxm, 0);
3346 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3347 nxm_s = nx_match_to_string(ofpbuf_data(&nxm), nxm_match_len);
3348 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3349 printf("NXM: %s -> ", nxm_s);
3351 printf("%s\n", ofperr_to_string(error));
3353 printf("%04"PRIx16"/%04"PRIx16"\n",
3354 ntohs(nxm_match.flow.vlan_tci),
3355 ntohs(nxm_match.wc.masks.vlan_tci));
3358 ofpbuf_uninit(&nxm);
3360 /* Convert to and from OXM. */
3361 ofpbuf_init(&nxm, 0);
3362 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
3363 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3364 error = oxm_pull_match(&nxm, &nxm_match);
3365 printf("OXM: %s -> ", nxm_s);
3367 printf("%s\n", ofperr_to_string(error));
3369 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3370 (VLAN_VID_MASK | VLAN_CFI);
3371 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3372 (VLAN_VID_MASK | VLAN_CFI);
3374 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3375 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3376 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3382 ofpbuf_uninit(&nxm);
3384 /* Convert to and from OpenFlow 1.0. */
3385 ofputil_match_to_ofp10_match(&match, &of10_raw);
3386 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3387 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3388 ntohs(of10_raw.dl_vlan),
3389 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3390 of10_raw.dl_vlan_pcp,
3391 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3392 ntohs(of10_match.flow.vlan_tci),
3393 ntohs(of10_match.wc.masks.vlan_tci));
3395 /* Convert to and from OpenFlow 1.1. */
3396 ofputil_match_to_ofp11_match(&match, &of11_raw);
3397 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3398 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3399 ntohs(of11_raw.dl_vlan),
3400 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3401 of11_raw.dl_vlan_pcp,
3402 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3403 ntohs(of11_match.flow.vlan_tci),
3404 ntohs(of11_match.wc.masks.vlan_tci));
3407 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3410 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3415 error = ofperr_from_name(argv[1]);
3417 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3420 for (version = 0; version <= UINT8_MAX; version++) {
3421 const char *name = ofperr_domain_get_name(version);
3423 int vendor = ofperr_get_vendor(error, version);
3424 int type = ofperr_get_type(error, version);
3425 int code = ofperr_get_code(error, version);
3427 if (vendor != -1 || type != -1 || code != -1) {
3428 printf("%s: vendor %#x, type %d, code %d\n",
3429 name, vendor, type, code);
3435 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3436 * error named ENUM and prints the error reply in hex. */
3438 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3440 const struct ofp_header *oh;
3441 struct ofpbuf request, *reply;
3444 error = ofperr_from_name(argv[1]);
3446 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3449 ofpbuf_init(&request, 0);
3450 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3451 ovs_fatal(0, "Trailing garbage in hex data");
3453 if (ofpbuf_size(&request) < sizeof(struct ofp_header)) {
3454 ovs_fatal(0, "Request too short");
3457 oh = ofpbuf_data(&request);
3458 if (ofpbuf_size(&request) != ntohs(oh->length)) {
3459 ovs_fatal(0, "Request size inconsistent");
3462 reply = ofperr_encode_reply(error, ofpbuf_data(&request));
3463 ofpbuf_uninit(&request);
3465 ovs_hex_dump(stdout, ofpbuf_data(reply), ofpbuf_size(reply), 0, false);
3466 ofpbuf_delete(reply);
3469 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3470 * binary data, interpreting them as an OpenFlow message, and prints the
3471 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3473 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3474 * HEXSTRING_FILE contains the HEXSTRING. */
3476 ofctl_ofp_print(int argc, char *argv[])
3478 struct ofpbuf packet;
3485 if (!strcmp(argv[argc-1], "-")) {
3486 if (ds_get_line(&line, stdin)) {
3487 VLOG_FATAL("Failed to read stdin");
3490 buffer = line.string;
3491 verbosity = argc > 2 ? atoi(argv[1]) : verbosity;
3492 } else if (argc > 2) {
3494 verbosity = atoi(argv[2]);
3499 ofpbuf_init(&packet, strlen(buffer) / 2);
3500 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
3501 ovs_fatal(0, "trailing garbage following hex bytes");
3503 ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), verbosity);
3504 ofpbuf_uninit(&packet);
3508 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3509 * and dumps each message in hex. */
3511 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3513 uint32_t bitmap = strtol(argv[1], NULL, 0);
3514 struct ofpbuf *hello;
3516 hello = ofputil_encode_hello(bitmap);
3517 ovs_hex_dump(stdout, ofpbuf_data(hello), ofpbuf_size(hello), 0, false);
3518 ofp_print(stdout, ofpbuf_data(hello), ofpbuf_size(hello), verbosity);
3519 ofpbuf_delete(hello);
3522 static const struct command all_commands[] = {
3523 { "show", 1, 1, ofctl_show },
3524 { "monitor", 1, 3, ofctl_monitor },
3525 { "snoop", 1, 1, ofctl_snoop },
3526 { "dump-desc", 1, 1, ofctl_dump_desc },
3527 { "dump-tables", 1, 1, ofctl_dump_tables },
3528 { "dump-table-features", 1, 1, ofctl_dump_table_features },
3529 { "dump-flows", 1, 2, ofctl_dump_flows },
3530 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3531 { "queue-stats", 1, 3, ofctl_queue_stats },
3532 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3533 { "add-flow", 2, 2, ofctl_add_flow },
3534 { "add-flows", 2, 2, ofctl_add_flows },
3535 { "mod-flows", 2, 2, ofctl_mod_flows },
3536 { "del-flows", 1, 2, ofctl_del_flows },
3537 { "replace-flows", 2, 2, ofctl_replace_flows },
3538 { "diff-flows", 2, 2, ofctl_diff_flows },
3539 { "add-meter", 2, 2, ofctl_add_meter },
3540 { "mod-meter", 2, 2, ofctl_mod_meter },
3541 { "del-meter", 2, 2, ofctl_del_meters },
3542 { "del-meters", 1, 1, ofctl_del_meters },
3543 { "dump-meter", 2, 2, ofctl_dump_meters },
3544 { "dump-meters", 1, 1, ofctl_dump_meters },
3545 { "meter-stats", 1, 2, ofctl_meter_stats },
3546 { "meter-features", 1, 1, ofctl_meter_features },
3547 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3548 { "dump-ports", 1, 2, ofctl_dump_ports },
3549 { "dump-ports-desc", 1, 2, ofctl_dump_ports_desc },
3550 { "mod-port", 3, 3, ofctl_mod_port },
3551 { "mod-table", 3, 3, ofctl_mod_table },
3552 { "get-frags", 1, 1, ofctl_get_frags },
3553 { "set-frags", 2, 2, ofctl_set_frags },
3554 { "probe", 1, 1, ofctl_probe },
3555 { "ping", 1, 2, ofctl_ping },
3556 { "benchmark", 3, 3, ofctl_benchmark },
3558 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3559 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3561 { "add-group", 1, 2, ofctl_add_group },
3562 { "add-groups", 1, 2, ofctl_add_groups },
3563 { "mod-group", 1, 2, ofctl_mod_group },
3564 { "del-groups", 1, 2, ofctl_del_groups },
3565 { "dump-groups", 1, 2, ofctl_dump_group_desc },
3566 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3567 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3568 { "help", 0, INT_MAX, ofctl_help },
3570 /* Undocumented commands for testing. */
3571 { "parse-flow", 1, 1, ofctl_parse_flow },
3572 { "parse-flows", 1, 1, ofctl_parse_flows },
3573 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3574 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3575 { "parse-oxm", 1, 1, ofctl_parse_oxm },
3576 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3577 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3578 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3579 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3580 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3581 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3582 { "check-vlan", 2, 2, ofctl_check_vlan },
3583 { "print-error", 1, 1, ofctl_print_error },
3584 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3585 { "ofp-print", 1, 2, ofctl_ofp_print },
3586 { "encode-hello", 1, 1, ofctl_encode_hello },
3588 { NULL, 0, 0, NULL },
3591 static const struct command *get_all_commands(void)
3593 return all_commands;