2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
48 #include "ofp-version-opt.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
54 #include "pcap-file.h"
55 #include "poll-loop.h"
57 #include "stream-ssl.h"
58 #include "socket-util.h"
64 #include "meta-flow.h"
67 VLOG_DEFINE_THIS_MODULE(ofctl);
69 /* --strict: Use strict matching for flow mod commands? Additionally governs
70 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
74 /* --readd: If true, on replace-flows, re-add even flows that have not changed
75 * (to reset flow counters). */
78 /* -F, --flow-format: Allowed protocols. By default, any protocol is
80 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
82 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
83 * commands. Either one of NXPIF_* to force a particular packet_in format, or
84 * -1 to let ovs-ofctl choose the default. */
85 static int preferred_packet_in_format = -1;
87 /* -m, --more: Additional verbosity for ofp-print functions. */
90 /* --timestamp: Print a timestamp before each received packet on "monitor" and
92 static bool timestamp;
94 /* --sort, --rsort: Sort order. */
95 enum sort_order { SORT_ASC, SORT_DESC };
96 struct sort_criterion {
97 const struct mf_field *field; /* NULL means to sort by priority. */
98 enum sort_order order;
100 static struct sort_criterion *criteria;
101 static size_t n_criteria, allocated_criteria;
103 static const struct command *get_all_commands(void);
105 static void usage(void) NO_RETURN;
106 static void parse_options(int argc, char *argv[]);
108 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
109 struct ofpbuf **replyp,
110 struct ofputil_flow_stats *,
111 struct ofpbuf *ofpacts);
113 main(int argc, char *argv[])
115 set_program_name(argv[0]);
116 parse_options(argc, argv);
117 fatal_ignore_sigpipe();
118 run_command(argc - optind, argv + optind, get_all_commands());
123 add_sort_criterion(enum sort_order order, const char *field)
125 struct sort_criterion *sc;
127 if (n_criteria >= allocated_criteria) {
128 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
131 sc = &criteria[n_criteria++];
132 if (!field || !strcasecmp(field, "priority")) {
135 sc->field = mf_from_name(field);
137 ovs_fatal(0, "%s: unknown field name", field);
144 parse_options(int argc, char *argv[])
147 OPT_STRICT = UCHAR_MAX + 1,
153 OFP_VERSION_OPTION_ENUMS,
156 static const struct option long_options[] = {
157 {"timeout", required_argument, NULL, 't'},
158 {"strict", no_argument, NULL, OPT_STRICT},
159 {"readd", no_argument, NULL, OPT_READD},
160 {"flow-format", required_argument, NULL, 'F'},
161 {"packet-in-format", required_argument, NULL, 'P'},
162 {"more", no_argument, NULL, 'm'},
163 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
164 {"sort", optional_argument, NULL, OPT_SORT},
165 {"rsort", optional_argument, NULL, OPT_RSORT},
166 {"help", no_argument, NULL, 'h'},
168 OFP_VERSION_LONG_OPTIONS,
170 STREAM_SSL_LONG_OPTIONS,
173 char *short_options = long_options_to_short_options(long_options);
175 enum ofputil_protocol version_protocols;
178 unsigned long int timeout;
181 c = getopt_long(argc, argv, short_options, long_options, NULL);
188 timeout = strtoul(optarg, NULL, 10);
190 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
198 allowed_protocols = ofputil_protocols_from_string(optarg);
199 if (!allowed_protocols) {
200 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
205 preferred_packet_in_format =
206 ofputil_packet_in_format_from_string(optarg);
207 if (preferred_packet_in_format < 0) {
208 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
232 add_sort_criterion(SORT_ASC, optarg);
236 add_sort_criterion(SORT_DESC, optarg);
239 DAEMON_OPTION_HANDLERS
240 OFP_VERSION_OPTION_HANDLERS
242 STREAM_SSL_OPTION_HANDLERS
253 /* Always do a final sort pass based on priority. */
254 add_sort_criterion(SORT_DESC, "priority");
259 versions = get_allowed_ofp_versions();
260 version_protocols = ofputil_protocols_from_version_bitmap(versions);
261 if (!(allowed_protocols & version_protocols)) {
262 char *protocols = ofputil_protocols_to_string(allowed_protocols);
263 struct ds version_s = DS_EMPTY_INITIALIZER;
265 ofputil_format_version_bitmap_names(&version_s, versions);
266 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
267 "any of the enabled flow formats (%s). (Use -O to enable "
268 "additional OpenFlow versions or -F to enable additional "
269 "flow formats.)", ds_cstr(&version_s), protocols);
271 allowed_protocols &= version_protocols;
272 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
279 printf("%s: OpenFlow switch management utility\n"
280 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
281 "\nFor OpenFlow switches:\n"
282 " show SWITCH show OpenFlow information\n"
283 " dump-desc SWITCH print switch description\n"
284 " dump-tables SWITCH print table stats\n"
285 " dump-table-features SWITCH print table features\n"
286 " mod-port SWITCH IFACE ACT modify port behavior\n"
287 " mod-table SWITCH MOD modify flow table behavior\n"
288 " get-frags SWITCH print fragment handling behavior\n"
289 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
290 " dump-ports SWITCH [PORT] print port statistics\n"
291 " dump-ports-desc SWITCH print port descriptions\n"
292 " dump-flows SWITCH print all flow entries\n"
293 " dump-flows SWITCH FLOW print matching FLOWs\n"
294 " dump-aggregate SWITCH print aggregate flow statistics\n"
295 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
296 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
297 " add-flow SWITCH FLOW add flow described by FLOW\n"
298 " add-flows SWITCH FILE add flows from FILE\n"
299 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
300 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
301 " replace-flows SWITCH FILE replace flows with those in FILE\n"
302 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
303 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
304 " execute ACTIONS on PACKET\n"
305 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
306 " print packets received from SWITCH\n"
307 " snoop SWITCH snoop on SWITCH and its controller\n"
308 " add-group SWITCH GROUP add group described by GROUP\n"
309 " add-group SWITCH FILE add group from FILE\n"
310 " mod-group SWITCH GROUP modify specific group\n"
311 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
312 " dump-group-features SWITCH print group features\n"
313 " dump-groups SWITCH print group description\n"
314 " dump-group-stats SWITCH [GROUP] print group statistics\n"
315 " queue-get-config SWITCH PORT print queue information for port\n"
316 " add-meter SWITCH METER add meter described by METER\n"
317 " mod-meter SWITCH METER modify specific METER\n"
318 " del-meter SWITCH METER delete METER\n"
319 " del-meters SWITCH delete all meters\n"
320 " dump-meter SWITCH METER print METER configuration\n"
321 " dump-meters SWITCH print all meter configuration\n"
322 " meter-stats SWITCH [METER] print meter statistics\n"
323 " meter-features SWITCH print meter features\n"
324 "\nFor OpenFlow switches and controllers:\n"
325 " probe TARGET probe whether TARGET is up\n"
326 " ping TARGET [N] latency of N-byte echos\n"
327 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
328 "SWITCH or TARGET is an active OpenFlow connection method.\n"
329 "\nOther commands:\n"
330 " ofp-parse FILE print messages read from FILE\n"
331 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
332 program_name, program_name);
333 vconn_usage(true, false, false);
337 printf("\nOther options:\n"
338 " --strict use strict match for flow commands\n"
339 " --readd replace flows that haven't changed\n"
340 " -F, --flow-format=FORMAT force particular flow format\n"
341 " -P, --packet-in-format=FRMT force particular packet in format\n"
342 " -m, --more be more verbose printing OpenFlow\n"
343 " --timestamp (monitor, snoop) print timestamps\n"
344 " -t, --timeout=SECS give up after SECS seconds\n"
345 " --sort[=field] sort in ascending order\n"
346 " --rsort[=field] sort in descending order\n"
347 " -h, --help display this help message\n"
348 " -V, --version display version information\n");
353 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
354 const char *argv[] OVS_UNUSED, void *exiting_)
356 bool *exiting = exiting_;
358 unixctl_command_reply(conn, NULL);
361 static void run(int retval, const char *message, ...)
365 run(int retval, const char *message, ...)
370 va_start(args, message);
371 ovs_fatal_valist(retval, message, args);
375 /* Generic commands. */
378 open_vconn_socket(const char *name, struct vconn **vconnp)
380 char *vconn_name = xasprintf("unix:%s", name);
383 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
385 if (error && error != ENOENT) {
386 ovs_fatal(0, "%s: failed to open socket (%s)", name,
387 ovs_strerror(error));
394 enum open_target { MGMT, SNOOP };
396 static enum ofputil_protocol
397 open_vconn__(const char *name, enum open_target target,
398 struct vconn **vconnp)
400 const char *suffix = target == MGMT ? "mgmt" : "snoop";
401 char *datapath_name, *datapath_type, *socket_name;
402 enum ofputil_protocol protocol;
407 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
409 ofproto_parse_name(name, &datapath_name, &datapath_type);
410 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
414 if (strchr(name, ':')) {
415 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
416 "connecting to %s", name);
417 } else if (!open_vconn_socket(name, vconnp)) {
419 } else if (!open_vconn_socket(bridge_path, vconnp)) {
421 } else if (!open_vconn_socket(socket_name, vconnp)) {
424 ovs_fatal(0, "%s is not a bridge or a socket", name);
427 if (target == SNOOP) {
428 vconn_set_recv_any_version(*vconnp);
434 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
435 error = vconn_connect_block(*vconnp);
437 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
438 ovs_strerror(error));
441 ofp_version = vconn_get_version(*vconnp);
442 protocol = ofputil_protocol_from_ofp_version(ofp_version);
444 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
450 static enum ofputil_protocol
451 open_vconn(const char *name, struct vconn **vconnp)
453 return open_vconn__(name, MGMT, vconnp);
457 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
459 ofpmsg_update_length(buffer);
460 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
464 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
466 struct ofpbuf *reply;
468 ofpmsg_update_length(request);
469 run(vconn_transact(vconn, request, &reply), "talking to %s",
470 vconn_get_name(vconn));
471 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
472 ofpbuf_delete(reply);
476 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
478 struct ofpbuf *request;
481 open_vconn(vconn_name, &vconn);
482 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
483 dump_transaction(vconn, request);
488 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
490 const struct ofp_header *request_oh = ofpbuf_data(request);
491 ovs_be32 send_xid = request_oh->xid;
492 enum ofpraw request_raw;
493 enum ofpraw reply_raw;
496 ofpraw_decode_partial(&request_raw, ofpbuf_data(request), ofpbuf_size(request));
497 reply_raw = ofpraw_stats_request_to_reply(request_raw,
498 request_oh->version);
500 send_openflow_buffer(vconn, request);
503 struct ofpbuf *reply;
505 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
506 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
507 if (send_xid == recv_xid) {
510 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
512 ofpraw_decode(&raw, ofpbuf_data(reply));
513 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
515 } else if (raw == reply_raw) {
516 done = !ofpmp_more(ofpbuf_data(reply));
518 ovs_fatal(0, "received bad reply: %s",
519 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
523 VLOG_DBG("received reply with xid %08"PRIx32" "
524 "!= expected %08"PRIx32, recv_xid, send_xid);
526 ofpbuf_delete(reply);
531 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
533 struct ofpbuf *request;
536 open_vconn(vconn_name, &vconn);
537 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
538 dump_stats_transaction(vconn, request);
542 /* Sends all of the 'requests', which should be requests that only have replies
543 * if an error occurs, and waits for them to succeed or fail. If an error does
544 * occur, prints it and exits with an error.
546 * Destroys all of the 'requests'. */
548 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
550 struct ofpbuf *request, *reply;
552 LIST_FOR_EACH (request, list_node, requests) {
553 ofpmsg_update_length(request);
556 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
557 "talking to %s", vconn_get_name(vconn));
559 ofp_print(stderr, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 2);
562 ofpbuf_delete(reply);
565 /* Sends 'request', which should be a request that only has a reply if an error
566 * occurs, and waits for it to succeed or fail. If an error does occur, prints
567 * it and exits with an error.
569 * Destroys 'request'. */
571 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
573 struct list requests;
575 list_init(&requests);
576 list_push_back(&requests, &request->list_node);
577 transact_multiple_noreply(vconn, &requests);
581 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
583 struct ofp_switch_config *config;
584 struct ofpbuf *request;
585 struct ofpbuf *reply;
588 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
589 vconn_get_version(vconn), 0);
590 run(vconn_transact(vconn, request, &reply),
591 "talking to %s", vconn_get_name(vconn));
593 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
594 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
597 config = ofpbuf_pull(reply, sizeof *config);
600 ofpbuf_delete(reply);
604 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
606 struct ofpbuf *request;
608 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
609 ofpbuf_put(request, config, sizeof *config);
611 transact_noreply(vconn, request);
615 ofctl_show(int argc OVS_UNUSED, char *argv[])
617 const char *vconn_name = argv[1];
619 struct ofpbuf *request;
620 struct ofpbuf *reply;
623 open_vconn(vconn_name, &vconn);
624 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
625 vconn_get_version(vconn), 0);
626 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
628 trunc = ofputil_switch_features_ports_trunc(reply);
629 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
631 ofpbuf_delete(reply);
634 /* The Features Reply may not contain all the ports, so send a
635 * Port Description stats request, which doesn't have size
637 dump_trivial_stats_transaction(vconn_name,
638 OFPRAW_OFPST_PORT_DESC_REQUEST);
640 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
645 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
647 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
651 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
653 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
657 ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[])
659 struct ofpbuf *request;
662 open_vconn(argv[1], &vconn);
663 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
665 dump_stats_transaction(vconn, request);
672 fetch_port_by_features(const char *vconn_name,
673 const char *port_name, ofp_port_t port_no,
674 struct ofputil_phy_port *pp, bool *trunc)
676 struct ofputil_switch_features features;
677 const struct ofp_header *oh;
678 struct ofpbuf *request, *reply;
685 /* Fetch the switch's ofp_switch_features. */
686 open_vconn(vconn_name, &vconn);
687 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
688 vconn_get_version(vconn), 0);
689 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
692 oh = ofpbuf_data(reply);
693 if (ofptype_decode(&type, ofpbuf_data(reply))
694 || type != OFPTYPE_FEATURES_REPLY) {
695 ovs_fatal(0, "%s: received bad features reply", vconn_name);
699 if (ofputil_switch_features_ports_trunc(reply)) {
704 error = ofputil_decode_switch_features(oh, &features, &b);
706 ovs_fatal(0, "%s: failed to decode features reply (%s)",
707 vconn_name, ofperr_to_string(error));
710 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
711 if (port_no != OFPP_NONE
712 ? port_no == pp->port_no
713 : !strcmp(pp->name, port_name)) {
720 ofpbuf_delete(reply);
725 fetch_port_by_stats(const char *vconn_name,
726 const char *port_name, ofp_port_t port_no,
727 struct ofputil_phy_port *pp)
729 struct ofpbuf *request;
735 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
736 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
738 open_vconn(vconn_name, &vconn);
739 send_openflow_buffer(vconn, request);
742 struct ofpbuf *reply;
744 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
745 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
746 if (send_xid == recv_xid) {
747 struct ofp_header *oh = ofpbuf_data(reply);
752 ofpbuf_use_const(&b, oh, ntohs(oh->length));
753 if (ofptype_pull(&type, &b)
754 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
755 ovs_fatal(0, "received bad reply: %s",
756 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
760 flags = ofpmp_flags(oh);
761 done = !(flags & OFPSF_REPLY_MORE);
764 /* We've already found the port, but we need to drain
765 * the queue of any other replies for this request. */
769 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
770 if (port_no != OFPP_NONE ? port_no == pp->port_no
771 : !strcmp(pp->name, port_name)) {
777 VLOG_DBG("received reply with xid %08"PRIx32" "
778 "!= expected %08"PRIx32, recv_xid, send_xid);
780 ofpbuf_delete(reply);
788 str_to_ofp(const char *s, ofp_port_t *ofp_port)
793 ret = str_to_uint(s, 10, &port_);
794 *ofp_port = u16_to_ofp(port_);
798 /* Opens a connection to 'vconn_name', fetches the port structure for
799 * 'port_name' (which may be a port name or number), and copies it into
802 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
803 struct ofputil_phy_port *pp)
809 /* Try to interpret the argument as a port number. */
810 if (!str_to_ofp(port_name, &port_no)) {
814 /* Try to find the port based on the Features Reply. If it looks
815 * like the results may be truncated, then use the Port Description
816 * stats message introduced in OVS 1.7. */
817 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
820 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
824 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
828 /* Returns the port number corresponding to 'port_name' (which may be a port
829 * name or number) within the switch 'vconn_name'. */
831 str_to_port_no(const char *vconn_name, const char *port_name)
835 if (ofputil_port_from_string(port_name, &port_no)) {
838 struct ofputil_phy_port pp;
840 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
846 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
847 enum ofputil_protocol *cur)
850 struct ofpbuf *request, *reply;
851 enum ofputil_protocol next;
853 request = ofputil_encode_set_protocol(*cur, want, &next);
858 run(vconn_transact_noreply(vconn, request, &reply),
859 "talking to %s", vconn_get_name(vconn));
861 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
862 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
863 vconn_get_name(vconn), s);
865 ofpbuf_delete(reply);
873 static enum ofputil_protocol
874 set_protocol_for_flow_dump(struct vconn *vconn,
875 enum ofputil_protocol cur_protocol,
876 enum ofputil_protocol usable_protocols)
881 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
882 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
883 if (f & usable_protocols & allowed_protocols
884 && try_set_protocol(vconn, f, &cur_protocol)) {
889 usable_s = ofputil_protocols_to_string(usable_protocols);
890 if (usable_protocols & allowed_protocols) {
891 ovs_fatal(0, "switch does not support any of the usable flow "
892 "formats (%s)", usable_s);
894 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
895 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
896 "allowed flow formats (%s)", usable_s, allowed_s);
900 static struct vconn *
901 prepare_dump_flows(int argc, char *argv[], bool aggregate,
902 struct ofpbuf **requestp)
904 enum ofputil_protocol usable_protocols, protocol;
905 struct ofputil_flow_stats_request fsr;
909 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
910 argc > 2 ? argv[2] : "",
913 ovs_fatal(0, "%s", error);
916 protocol = open_vconn(argv[1], &vconn);
917 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
918 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
923 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
925 struct ofpbuf *request;
928 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
929 dump_stats_transaction(vconn, request);
934 compare_flows(const void *afs_, const void *bfs_)
936 const struct ofputil_flow_stats *afs = afs_;
937 const struct ofputil_flow_stats *bfs = bfs_;
938 const struct match *a = &afs->match;
939 const struct match *b = &bfs->match;
940 const struct sort_criterion *sc;
942 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
943 const struct mf_field *f = sc->field;
947 unsigned int a_pri = afs->priority;
948 unsigned int b_pri = bfs->priority;
949 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
953 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
954 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
956 /* Skip the test for sc->order, so that missing fields always
957 * sort to the end whether we're sorting in ascending or
958 * descending order. */
961 union mf_value aval, bval;
963 mf_get_value(f, &a->flow, &aval);
964 mf_get_value(f, &b->flow, &bval);
965 ret = memcmp(&aval, &bval, f->n_bytes);
970 return sc->order == SORT_ASC ? ret : -ret;
978 ofctl_dump_flows(int argc, char *argv[])
981 return ofctl_dump_flows__(argc, argv, false);
983 struct ofputil_flow_stats *fses;
984 size_t n_fses, allocated_fses;
985 struct ofpbuf *request;
986 struct ofpbuf ofpacts;
987 struct ofpbuf *reply;
993 vconn = prepare_dump_flows(argc, argv, false, &request);
994 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
995 send_openflow_buffer(vconn, request);
998 n_fses = allocated_fses = 0;
1000 ofpbuf_init(&ofpacts, 0);
1002 struct ofputil_flow_stats *fs;
1004 if (n_fses >= allocated_fses) {
1005 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1009 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1013 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1016 ofpbuf_uninit(&ofpacts);
1018 qsort(fses, n_fses, sizeof *fses, compare_flows);
1021 for (i = 0; i < n_fses; i++) {
1023 ofp_print_flow_stats(&s, &fses[i]);
1028 for (i = 0; i < n_fses; i++) {
1029 free(fses[i].ofpacts);
1038 ofctl_dump_aggregate(int argc, char *argv[])
1040 return ofctl_dump_flows__(argc, argv, true);
1044 ofctl_queue_stats(int argc, char *argv[])
1046 struct ofpbuf *request;
1047 struct vconn *vconn;
1048 struct ofputil_queue_stats_request oqs;
1050 open_vconn(argv[1], &vconn);
1052 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1053 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1055 oqs.port_no = OFPP_ANY;
1057 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1058 oqs.queue_id = atoi(argv[3]);
1060 oqs.queue_id = OFPQ_ALL;
1063 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1064 dump_stats_transaction(vconn, request);
1069 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1071 const char *vconn_name = argv[1];
1072 const char *port_name = argv[2];
1073 enum ofputil_protocol protocol;
1074 enum ofp_version version;
1075 struct ofpbuf *request;
1076 struct vconn *vconn;
1079 port = str_to_port_no(vconn_name, port_name);
1081 protocol = open_vconn(vconn_name, &vconn);
1082 version = ofputil_protocol_to_ofp_version(protocol);
1083 request = ofputil_encode_queue_get_config_request(version, port);
1084 dump_transaction(vconn, request);
1088 static enum ofputil_protocol
1089 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1090 enum ofputil_protocol usable_protocols)
1092 enum ofputil_protocol cur_protocol;
1096 if (!(usable_protocols & allowed_protocols)) {
1097 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1098 usable_s = ofputil_protocols_to_string(usable_protocols);
1099 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1100 "allowed flow formats (%s)", usable_s, allowed_s);
1103 /* If the initial flow format is allowed and usable, keep it. */
1104 cur_protocol = open_vconn(remote, vconnp);
1105 if (usable_protocols & allowed_protocols & cur_protocol) {
1106 return cur_protocol;
1109 /* Otherwise try each flow format in turn. */
1110 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1111 enum ofputil_protocol f = 1 << i;
1113 if (f != cur_protocol
1114 && f & usable_protocols & allowed_protocols
1115 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1120 usable_s = ofputil_protocols_to_string(usable_protocols);
1121 ovs_fatal(0, "switch does not support any of the usable flow "
1122 "formats (%s)", usable_s);
1126 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1127 size_t n_fms, enum ofputil_protocol usable_protocols)
1129 enum ofputil_protocol protocol;
1130 struct vconn *vconn;
1133 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1135 for (i = 0; i < n_fms; i++) {
1136 struct ofputil_flow_mod *fm = &fms[i];
1138 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1145 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1147 enum ofputil_protocol usable_protocols;
1148 struct ofputil_flow_mod *fms = NULL;
1152 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1155 ovs_fatal(0, "%s", error);
1157 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1162 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1164 if (argc > 2 && !strcmp(argv[2], "-")) {
1165 ofctl_flow_mod_file(argc, argv, command);
1167 struct ofputil_flow_mod fm;
1169 enum ofputil_protocol usable_protocols;
1171 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1174 ovs_fatal(0, "%s", error);
1176 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1181 ofctl_add_flow(int argc, char *argv[])
1183 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1187 ofctl_add_flows(int argc, char *argv[])
1189 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1193 ofctl_mod_flows(int argc, char *argv[])
1195 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1199 ofctl_del_flows(int argc, char *argv[])
1201 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1205 set_packet_in_format(struct vconn *vconn,
1206 enum nx_packet_in_format packet_in_format)
1208 struct ofpbuf *spif;
1210 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1212 transact_noreply(vconn, spif);
1213 VLOG_DBG("%s: using user-specified packet in format %s",
1214 vconn_get_name(vconn),
1215 ofputil_packet_in_format_to_string(packet_in_format));
1219 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1221 struct ofp_switch_config config;
1222 enum ofp_config_flags flags;
1224 fetch_switch_config(vconn, &config);
1225 flags = ntohs(config.flags);
1226 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1227 /* Set the invalid ttl config. */
1228 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1230 config.flags = htons(flags);
1231 set_switch_config(vconn, &config);
1233 /* Then retrieve the configuration to see if it really took. OpenFlow
1234 * doesn't define error reporting for bad modes, so this is all we can
1236 fetch_switch_config(vconn, &config);
1237 flags = ntohs(config.flags);
1238 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1239 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1240 "switch probably doesn't support mode)");
1247 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1248 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1249 * an error message and stores NULL in '*msgp'. */
1251 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1253 struct ofp_header *oh;
1256 msg = ofpbuf_new(strlen(hex) / 2);
1259 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1261 return "Trailing garbage in hex data";
1264 if (ofpbuf_size(msg) < sizeof(struct ofp_header)) {
1266 return "Message too short for OpenFlow";
1269 oh = ofpbuf_data(msg);
1270 if (ofpbuf_size(msg) != ntohs(oh->length)) {
1272 return "Message size does not match length in OpenFlow header";
1280 ofctl_send(struct unixctl_conn *conn, int argc,
1281 const char *argv[], void *vconn_)
1283 struct vconn *vconn = vconn_;
1290 for (i = 1; i < argc; i++) {
1291 const char *error_msg;
1295 error_msg = openflow_from_hex(argv[i], &msg);
1297 ds_put_format(&reply, "%s\n", error_msg);
1302 fprintf(stderr, "send: ");
1303 ofp_print(stderr, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
1305 error = vconn_send_block(vconn, msg);
1308 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1311 ds_put_cstr(&reply, "sent\n");
1316 unixctl_command_reply(conn, ds_cstr(&reply));
1318 unixctl_command_reply_error(conn, ds_cstr(&reply));
1323 struct barrier_aux {
1324 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1325 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1329 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1330 const char *argv[] OVS_UNUSED, void *aux_)
1332 struct barrier_aux *aux = aux_;
1337 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1341 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1342 error = vconn_send_block(aux->vconn, msg);
1345 unixctl_command_reply_error(conn, ovs_strerror(error));
1352 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1353 const char *argv[], void *aux OVS_UNUSED)
1357 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1359 unixctl_command_reply_error(conn, ovs_strerror(errno));
1364 dup2(fd, STDERR_FILENO);
1366 unixctl_command_reply(conn, NULL);
1370 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1371 const char *argv[] OVS_UNUSED, void *blocked_)
1373 bool *blocked = blocked_;
1377 unixctl_command_reply(conn, NULL);
1379 unixctl_command_reply(conn, "already blocking");
1384 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1385 const char *argv[] OVS_UNUSED, void *blocked_)
1387 bool *blocked = blocked_;
1391 unixctl_command_reply(conn, NULL);
1393 unixctl_command_reply(conn, "already unblocked");
1397 /* Prints to stdout all of the messages received on 'vconn'.
1399 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1400 * received on 'vconn'. */
1402 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1404 struct barrier_aux barrier_aux = { vconn, NULL };
1405 struct unixctl_server *server;
1406 bool exiting = false;
1407 bool blocked = false;
1410 daemon_save_fd(STDERR_FILENO);
1412 error = unixctl_server_create(NULL, &server);
1414 ovs_fatal(error, "failed to create unixctl server");
1416 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1417 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1419 unixctl_command_register("ofctl/barrier", "", 0, 0,
1420 ofctl_barrier, &barrier_aux);
1421 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1422 ofctl_set_output_file, NULL);
1424 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1425 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1428 daemonize_complete();
1434 unixctl_server_run(server);
1439 retval = vconn_recv(vconn, &b);
1440 if (retval == EAGAIN) {
1443 run(retval, "vconn_recv");
1446 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1447 time_wall_msec(), true);
1452 ofptype_decode(&type, ofpbuf_data(b));
1453 ofp_print(stderr, ofpbuf_data(b), ofpbuf_size(b), verbosity + 2);
1455 switch ((int) type) {
1456 case OFPTYPE_BARRIER_REPLY:
1457 if (barrier_aux.conn) {
1458 unixctl_command_reply(barrier_aux.conn, NULL);
1459 barrier_aux.conn = NULL;
1463 case OFPTYPE_ECHO_REQUEST:
1464 if (reply_to_echo_requests) {
1465 struct ofpbuf *reply;
1467 reply = make_echo_reply(ofpbuf_data(b));
1468 retval = vconn_send_block(vconn, reply);
1470 ovs_fatal(retval, "failed to send echo reply");
1483 vconn_run_wait(vconn);
1485 vconn_recv_wait(vconn);
1487 unixctl_server_wait(server);
1491 unixctl_server_destroy(server);
1495 ofctl_monitor(int argc, char *argv[])
1497 struct vconn *vconn;
1499 enum ofputil_protocol usable_protocols;
1501 open_vconn(argv[1], &vconn);
1502 for (i = 2; i < argc; i++) {
1503 const char *arg = argv[i];
1505 if (isdigit((unsigned char) *arg)) {
1506 struct ofp_switch_config config;
1508 fetch_switch_config(vconn, &config);
1509 config.miss_send_len = htons(atoi(arg));
1510 set_switch_config(vconn, &config);
1511 } else if (!strcmp(arg, "invalid_ttl")) {
1512 monitor_set_invalid_ttl_to_controller(vconn);
1513 } else if (!strncmp(arg, "watch:", 6)) {
1514 struct ofputil_flow_monitor_request fmr;
1518 error = parse_flow_monitor_request(&fmr, arg + 6,
1521 ovs_fatal(0, "%s", error);
1524 msg = ofpbuf_new(0);
1525 ofputil_append_flow_monitor_request(&fmr, msg);
1526 dump_stats_transaction(vconn, msg);
1528 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1532 if (preferred_packet_in_format >= 0) {
1533 set_packet_in_format(vconn, preferred_packet_in_format);
1535 enum ofp_version version = vconn_get_version(vconn);
1538 case OFP10_VERSION: {
1539 struct ofpbuf *spif, *reply;
1541 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1543 run(vconn_transact_noreply(vconn, spif, &reply),
1544 "talking to %s", vconn_get_name(vconn));
1546 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
1547 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1548 " replied: %s. Falling back to the switch default.",
1549 vconn_get_name(vconn), s);
1551 ofpbuf_delete(reply);
1565 monitor_vconn(vconn, true);
1569 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1571 struct vconn *vconn;
1573 open_vconn__(argv[1], SNOOP, &vconn);
1574 monitor_vconn(vconn, false);
1578 ofctl_dump_ports(int argc, char *argv[])
1580 struct ofpbuf *request;
1581 struct vconn *vconn;
1584 open_vconn(argv[1], &vconn);
1585 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1586 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1587 dump_stats_transaction(vconn, request);
1592 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1594 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1598 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1600 struct ofpbuf *request;
1601 struct vconn *vconn;
1602 struct ofpbuf *reply;
1604 open_vconn(argv[1], &vconn);
1605 request = make_echo_request(vconn_get_version(vconn));
1606 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1607 if (ofpbuf_size(reply) != sizeof(struct ofp_header)) {
1608 ovs_fatal(0, "reply does not match request");
1610 ofpbuf_delete(reply);
1615 ofctl_packet_out(int argc, char *argv[])
1617 enum ofputil_protocol protocol;
1618 struct ofputil_packet_out po;
1619 struct ofpbuf ofpacts;
1620 struct vconn *vconn;
1623 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1625 ofpbuf_init(&ofpacts, 64);
1626 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1628 ovs_fatal(0, "%s", error);
1631 po.buffer_id = UINT32_MAX;
1632 po.in_port = str_to_port_no(argv[1], argv[2]);
1633 po.ofpacts = ofpbuf_data(&ofpacts);
1634 po.ofpacts_len = ofpbuf_size(&ofpacts);
1636 protocol = open_vconn(argv[1], &vconn);
1637 for (i = 4; i < argc; i++) {
1638 struct ofpbuf *packet, *opo;
1639 const char *error_msg;
1641 error_msg = eth_from_hex(argv[i], &packet);
1643 ovs_fatal(0, "%s", error_msg);
1646 po.packet = ofpbuf_data(packet);
1647 po.packet_len = ofpbuf_size(packet);
1648 opo = ofputil_encode_packet_out(&po, protocol);
1649 transact_noreply(vconn, opo);
1650 ofpbuf_delete(packet);
1653 ofpbuf_uninit(&ofpacts);
1657 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1659 struct ofp_config_flag {
1660 const char *name; /* The flag's name. */
1661 enum ofputil_port_config bit; /* Bit to turn on or off. */
1662 bool on; /* Value to set the bit to. */
1664 static const struct ofp_config_flag flags[] = {
1665 { "up", OFPUTIL_PC_PORT_DOWN, false },
1666 { "down", OFPUTIL_PC_PORT_DOWN, true },
1667 { "stp", OFPUTIL_PC_NO_STP, false },
1668 { "receive", OFPUTIL_PC_NO_RECV, false },
1669 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1670 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1671 { "forward", OFPUTIL_PC_NO_FWD, false },
1672 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1675 const struct ofp_config_flag *flag;
1676 enum ofputil_protocol protocol;
1677 struct ofputil_port_mod pm;
1678 struct ofputil_phy_port pp;
1679 struct vconn *vconn;
1680 const char *command;
1683 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1685 pm.port_no = pp.port_no;
1686 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1691 if (!strncasecmp(argv[3], "no-", 3)) {
1692 command = argv[3] + 3;
1694 } else if (!strncasecmp(argv[3], "no", 2)) {
1695 command = argv[3] + 2;
1701 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1702 if (!strcasecmp(command, flag->name)) {
1703 pm.mask = flag->bit;
1704 pm.config = flag->on ^ not ? flag->bit : 0;
1708 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1711 protocol = open_vconn(argv[1], &vconn);
1712 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1717 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1719 enum ofputil_protocol protocol, usable_protocols;
1720 struct ofputil_table_mod tm;
1721 struct vconn *vconn;
1725 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1727 ovs_fatal(0, "%s", error);
1730 protocol = open_vconn(argv[1], &vconn);
1731 if (!(protocol & usable_protocols)) {
1732 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1733 enum ofputil_protocol f = 1 << i;
1735 && f & usable_protocols
1736 && try_set_protocol(vconn, f, &protocol)) {
1743 if (!(protocol & usable_protocols)) {
1744 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1745 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1748 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1753 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1755 struct ofp_switch_config config;
1756 struct vconn *vconn;
1758 open_vconn(argv[1], &vconn);
1759 fetch_switch_config(vconn, &config);
1760 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1765 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1767 struct ofp_switch_config config;
1768 enum ofp_config_flags mode;
1769 struct vconn *vconn;
1772 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1773 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1776 open_vconn(argv[1], &vconn);
1777 fetch_switch_config(vconn, &config);
1778 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1779 if (flags != config.flags) {
1780 /* Set the configuration. */
1781 config.flags = flags;
1782 set_switch_config(vconn, &config);
1784 /* Then retrieve the configuration to see if it really took. OpenFlow
1785 * doesn't define error reporting for bad modes, so this is all we can
1787 fetch_switch_config(vconn, &config);
1788 if (flags != config.flags) {
1789 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1790 "switch probably doesn't support mode \"%s\")",
1791 argv[1], ofputil_frag_handling_to_string(mode));
1798 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1800 const char *filename = argv[1];
1804 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1806 ovs_fatal(errno, "%s: open", filename);
1809 ofpbuf_init(&b, 65536);
1811 struct ofp_header *oh;
1812 size_t length, tail_len;
1817 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1818 n = fread(oh, 1, sizeof *oh, file);
1821 } else if (n < sizeof *oh) {
1822 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1825 length = ntohs(oh->length);
1826 if (length < sizeof *oh) {
1827 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1831 tail_len = length - sizeof *oh;
1832 tail = ofpbuf_put_uninit(&b, tail_len);
1833 n = fread(tail, 1, tail_len, file);
1835 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1838 ofp_print(stdout, ofpbuf_data(&b), ofpbuf_size(&b), verbosity + 2);
1842 if (file != stdin) {
1848 is_openflow_port(ovs_be16 port_, char *ports[])
1850 uint16_t port = ntohs(port_);
1854 for (i = 0; ports[i]; i++) {
1855 if (port == atoi(ports[i])) {
1861 return port == OFP_PORT || port == OFP_OLD_PORT;
1866 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1868 struct tcp_reader *reader;
1873 file = ovs_pcap_open(argv[1], "rb");
1875 ovs_fatal(errno, "%s: open failed", argv[1]);
1878 reader = tcp_reader_open();
1881 struct ofpbuf *packet;
1884 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1886 error = ovs_pcap_read(file, &packet, &when);
1890 flow_extract(packet, &md, &flow);
1891 if (flow.dl_type == htons(ETH_TYPE_IP)
1892 && flow.nw_proto == IPPROTO_TCP
1893 && (is_openflow_port(flow.tp_src, argv + 2) ||
1894 is_openflow_port(flow.tp_dst, argv + 2))) {
1895 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1897 while (ofpbuf_size(payload) >= sizeof(struct ofp_header)) {
1898 const struct ofp_header *oh;
1899 void *data = ofpbuf_data(payload);
1902 /* Align OpenFlow on 8-byte boundary for safe access. */
1903 ofpbuf_shift(payload, -((intptr_t) data & 7));
1905 oh = ofpbuf_data(payload);
1906 length = ntohs(oh->length);
1907 if (ofpbuf_size(payload) < length) {
1917 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1922 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1923 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1924 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1925 ofp_print(stdout, ofpbuf_data(payload), length, verbosity + 1);
1926 ofpbuf_pull(payload, length);
1930 ofpbuf_delete(packet);
1932 tcp_reader_close(reader);
1936 ofctl_ping(int argc, char *argv[])
1938 size_t max_payload = 65535 - sizeof(struct ofp_header);
1939 unsigned int payload;
1940 struct vconn *vconn;
1943 payload = argc > 2 ? atoi(argv[2]) : 64;
1944 if (payload > max_payload) {
1945 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1948 open_vconn(argv[1], &vconn);
1949 for (i = 0; i < 10; i++) {
1950 struct timeval start, end;
1951 struct ofpbuf *request, *reply;
1952 const struct ofp_header *rpy_hdr;
1955 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1956 vconn_get_version(vconn), payload);
1957 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1959 xgettimeofday(&start);
1960 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1961 xgettimeofday(&end);
1963 rpy_hdr = ofpbuf_data(reply);
1964 if (ofptype_pull(&type, reply)
1965 || type != OFPTYPE_ECHO_REPLY
1966 || ofpbuf_size(reply) != payload
1967 || memcmp(ofpbuf_l3(request), ofpbuf_l3(reply), payload)) {
1968 printf("Reply does not match request. Request:\n");
1969 ofp_print(stdout, request, ofpbuf_size(request), verbosity + 2);
1971 ofp_print(stdout, reply, ofpbuf_size(reply), verbosity + 2);
1973 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1974 ofpbuf_size(reply), argv[1], ntohl(rpy_hdr->xid),
1975 (1000*(double)(end.tv_sec - start.tv_sec))
1976 + (.001*(end.tv_usec - start.tv_usec)));
1977 ofpbuf_delete(request);
1978 ofpbuf_delete(reply);
1984 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1986 size_t max_payload = 65535 - sizeof(struct ofp_header);
1987 struct timeval start, end;
1988 unsigned int payload_size, message_size;
1989 struct vconn *vconn;
1994 payload_size = atoi(argv[2]);
1995 if (payload_size > max_payload) {
1996 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1998 message_size = sizeof(struct ofp_header) + payload_size;
2000 count = atoi(argv[3]);
2002 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2003 count, message_size, count * message_size);
2005 open_vconn(argv[1], &vconn);
2006 xgettimeofday(&start);
2007 for (i = 0; i < count; i++) {
2008 struct ofpbuf *request, *reply;
2010 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2011 vconn_get_version(vconn), payload_size);
2012 ofpbuf_put_zeros(request, payload_size);
2013 run(vconn_transact(vconn, request, &reply), "transact");
2014 ofpbuf_delete(reply);
2016 xgettimeofday(&end);
2019 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2020 + (.001*(end.tv_usec - start.tv_usec)));
2021 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2022 duration, count / (duration / 1000.0),
2023 count * message_size / (duration / 1000.0));
2027 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2030 struct ofputil_group_mod *gm;
2031 struct ofpbuf *request;
2033 struct vconn *vconn;
2036 open_vconn(remote, &vconn);
2038 for (i = 0; i < n_gms; i++) {
2040 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2042 transact_noreply(vconn, request);
2052 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2054 struct ofputil_group_mod *gms = NULL;
2055 enum ofputil_protocol usable_protocols;
2059 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2062 ovs_fatal(0, "%s", error);
2064 ofctl_group_mod__(argv[1], gms, n_gms);
2069 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2071 if (argc > 2 && !strcmp(argv[2], "-")) {
2072 ofctl_group_mod_file(argc, argv, command);
2074 enum ofputil_protocol usable_protocols;
2075 struct ofputil_group_mod gm;
2078 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2081 ovs_fatal(0, "%s", error);
2083 ofctl_group_mod__(argv[1], &gm, 1);
2088 ofctl_add_group(int argc, char *argv[])
2090 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2094 ofctl_add_groups(int argc, char *argv[])
2096 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2100 ofctl_mod_group(int argc, char *argv[])
2102 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2106 ofctl_del_groups(int argc, char *argv[])
2108 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2112 ofctl_dump_group_stats(int argc, char *argv[])
2114 enum ofputil_protocol usable_protocols;
2115 struct ofputil_group_mod gm;
2116 struct ofpbuf *request;
2117 struct vconn *vconn;
2121 memset(&gm, 0, sizeof gm);
2123 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2124 argc > 2 ? argv[2] : "",
2127 ovs_fatal(0, "%s", error);
2130 group_id = gm.group_id;
2132 open_vconn(argv[1], &vconn);
2133 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2136 dump_stats_transaction(vconn, request);
2143 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2145 struct ofpbuf *request;
2146 struct vconn *vconn;
2148 open_vconn(argv[1], &vconn);
2150 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2152 dump_stats_transaction(vconn, request);
2159 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2161 struct ofpbuf *request;
2162 struct vconn *vconn;
2164 open_vconn(argv[1], &vconn);
2165 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2167 dump_stats_transaction(vconn, request);
2174 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2179 /* replace-flows and diff-flows commands. */
2181 /* A flow table entry, possibly with two different versions. */
2183 struct cls_rule rule; /* Within a "struct classifier". */
2184 struct fte_version *versions[2];
2187 /* One version of a Flow Table Entry. */
2188 struct fte_version {
2190 uint16_t idle_timeout;
2191 uint16_t hard_timeout;
2193 struct ofpact *ofpacts;
2197 /* Frees 'version' and the data that it owns. */
2199 fte_version_free(struct fte_version *version)
2202 free(version->ofpacts);
2207 /* Returns true if 'a' and 'b' are the same, false if they differ.
2209 * Ignores differences in 'flags' because there's no way to retrieve flags from
2210 * an OpenFlow switch. We have to assume that they are the same. */
2212 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2214 return (a->cookie == b->cookie
2215 && a->idle_timeout == b->idle_timeout
2216 && a->hard_timeout == b->hard_timeout
2217 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2218 b->ofpacts, b->ofpacts_len));
2221 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2222 * 'index' into 's', followed by a new-line. */
2224 fte_version_format(const struct fte *fte, int index, struct ds *s)
2226 const struct fte_version *version = fte->versions[index];
2233 cls_rule_format(&fte->rule, s);
2234 if (version->cookie != htonll(0)) {
2235 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2237 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2238 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2240 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2241 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2244 ds_put_cstr(s, " actions=");
2245 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2247 ds_put_char(s, '\n');
2251 fte_from_cls_rule(const struct cls_rule *cls_rule)
2253 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2256 /* Frees 'fte' and its versions. */
2258 fte_free(struct fte *fte)
2261 fte_version_free(fte->versions[0]);
2262 fte_version_free(fte->versions[1]);
2263 cls_rule_destroy(&fte->rule);
2268 /* Frees all of the FTEs within 'cls'. */
2270 fte_free_all(struct classifier *cls)
2272 struct cls_cursor cursor;
2273 struct fte *fte, *next;
2275 fat_rwlock_wrlock(&cls->rwlock);
2276 cls_cursor_init(&cursor, cls, NULL);
2277 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2278 classifier_remove(cls, &fte->rule);
2281 fat_rwlock_unlock(&cls->rwlock);
2282 classifier_destroy(cls);
2285 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2286 * necessary. Sets 'version' as the version of that rule with the given
2287 * 'index', replacing any existing version, if any.
2289 * Takes ownership of 'version'. */
2291 fte_insert(struct classifier *cls, const struct match *match,
2292 unsigned int priority, struct fte_version *version, int index)
2294 struct fte *old, *fte;
2296 fte = xzalloc(sizeof *fte);
2297 cls_rule_init(&fte->rule, match, priority);
2298 fte->versions[index] = version;
2300 fat_rwlock_wrlock(&cls->rwlock);
2301 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2302 fat_rwlock_unlock(&cls->rwlock);
2304 fte_version_free(old->versions[index]);
2305 fte->versions[!index] = old->versions[!index];
2306 cls_rule_destroy(&old->rule);
2311 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2312 * with the specified 'index'. Returns the flow formats able to represent the
2313 * flows that were read. */
2314 static enum ofputil_protocol
2315 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2317 enum ofputil_protocol usable_protocols;
2322 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2324 ovs_fatal(errno, "%s: open", filename);
2328 usable_protocols = OFPUTIL_P_ANY;
2330 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2331 struct fte_version *version;
2332 struct ofputil_flow_mod fm;
2334 enum ofputil_protocol usable;
2336 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2338 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2340 usable_protocols &= usable;
2342 version = xmalloc(sizeof *version);
2343 version->cookie = fm.new_cookie;
2344 version->idle_timeout = fm.idle_timeout;
2345 version->hard_timeout = fm.hard_timeout;
2346 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2347 | OFPUTIL_FF_EMERG);
2348 version->ofpacts = fm.ofpacts;
2349 version->ofpacts_len = fm.ofpacts_len;
2351 fte_insert(cls, &fm.match, fm.priority, version, index);
2355 if (file != stdin) {
2359 return usable_protocols;
2363 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2364 struct ofpbuf **replyp,
2365 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2367 struct ofpbuf *reply = *replyp;
2373 /* Get a flow stats reply message, if we don't already have one. */
2379 run(vconn_recv_block(vconn, &reply),
2380 "OpenFlow packet receive failed");
2381 } while (((struct ofp_header *) ofpbuf_data(reply))->xid != send_xid);
2383 error = ofptype_decode(&type, ofpbuf_data(reply));
2384 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2385 ovs_fatal(0, "received bad reply: %s",
2386 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
2391 /* Pull an individual flow stats reply out of the message. */
2392 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2399 more = ofpmp_more(reply->frame);
2400 ofpbuf_delete(reply);
2409 ovs_fatal(0, "parse error in reply (%s)",
2410 ofperr_to_string(retval));
2415 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2416 * format 'protocol', and adds them as flow table entries in 'cls' for the
2417 * version with the specified 'index'. */
2419 read_flows_from_switch(struct vconn *vconn,
2420 enum ofputil_protocol protocol,
2421 struct classifier *cls, int index)
2423 struct ofputil_flow_stats_request fsr;
2424 struct ofputil_flow_stats fs;
2425 struct ofpbuf *request;
2426 struct ofpbuf ofpacts;
2427 struct ofpbuf *reply;
2430 fsr.aggregate = false;
2431 match_init_catchall(&fsr.match);
2432 fsr.out_port = OFPP_ANY;
2433 fsr.table_id = 0xff;
2434 fsr.cookie = fsr.cookie_mask = htonll(0);
2435 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2436 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
2437 send_openflow_buffer(vconn, request);
2440 ofpbuf_init(&ofpacts, 0);
2441 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2442 struct fte_version *version;
2444 version = xmalloc(sizeof *version);
2445 version->cookie = fs.cookie;
2446 version->idle_timeout = fs.idle_timeout;
2447 version->hard_timeout = fs.hard_timeout;
2449 version->ofpacts_len = fs.ofpacts_len;
2450 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2452 fte_insert(cls, &fs.match, fs.priority, version, index);
2454 ofpbuf_uninit(&ofpacts);
2458 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2459 enum ofputil_protocol protocol, struct list *packets)
2461 const struct fte_version *version = fte->versions[index];
2462 struct ofputil_flow_mod fm;
2465 minimatch_expand(&fte->rule.match, &fm.match);
2466 fm.priority = fte->rule.priority;
2467 fm.cookie = htonll(0);
2468 fm.cookie_mask = htonll(0);
2469 fm.new_cookie = version->cookie;
2470 fm.modify_cookie = true;
2472 fm.command = command;
2473 fm.idle_timeout = version->idle_timeout;
2474 fm.hard_timeout = version->hard_timeout;
2475 fm.buffer_id = UINT32_MAX;
2476 fm.out_port = OFPP_ANY;
2477 fm.flags = version->flags;
2478 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2479 command == OFPFC_MODIFY_STRICT) {
2480 fm.ofpacts = version->ofpacts;
2481 fm.ofpacts_len = version->ofpacts_len;
2487 ofm = ofputil_encode_flow_mod(&fm, protocol);
2488 list_push_back(packets, &ofm->list_node);
2492 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2494 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2495 enum ofputil_protocol usable_protocols, protocol;
2496 struct cls_cursor cursor;
2497 struct classifier cls;
2498 struct list requests;
2499 struct vconn *vconn;
2502 classifier_init(&cls, NULL);
2503 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2505 protocol = open_vconn(argv[1], &vconn);
2506 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2508 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2510 list_init(&requests);
2512 /* Delete flows that exist on the switch but not in the file. */
2513 fat_rwlock_rdlock(&cls.rwlock);
2514 cls_cursor_init(&cursor, &cls, NULL);
2515 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2516 struct fte_version *file_ver = fte->versions[FILE_IDX];
2517 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2519 if (sw_ver && !file_ver) {
2520 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2521 protocol, &requests);
2525 /* Add flows that exist in the file but not on the switch.
2526 * Update flows that exist in both places but differ. */
2527 cls_cursor_init(&cursor, &cls, NULL);
2528 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2529 struct fte_version *file_ver = fte->versions[FILE_IDX];
2530 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2533 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2534 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2537 fat_rwlock_unlock(&cls.rwlock);
2538 transact_multiple_noreply(vconn, &requests);
2545 read_flows_from_source(const char *source, struct classifier *cls, int index)
2549 if (source[0] == '/' || source[0] == '.'
2550 || (!strchr(source, ':') && !stat(source, &s))) {
2551 read_flows_from_file(source, cls, index);
2553 enum ofputil_protocol protocol;
2554 struct vconn *vconn;
2556 protocol = open_vconn(source, &vconn);
2557 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2558 read_flows_from_switch(vconn, protocol, cls, index);
2564 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2566 bool differences = false;
2567 struct cls_cursor cursor;
2568 struct classifier cls;
2572 classifier_init(&cls, NULL);
2573 read_flows_from_source(argv[1], &cls, 0);
2574 read_flows_from_source(argv[2], &cls, 1);
2579 fat_rwlock_rdlock(&cls.rwlock);
2580 cls_cursor_init(&cursor, &cls, NULL);
2581 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2582 struct fte_version *a = fte->versions[0];
2583 struct fte_version *b = fte->versions[1];
2585 if (!a || !b || !fte_version_equals(a, b)) {
2586 fte_version_format(fte, 0, &a_s);
2587 fte_version_format(fte, 1, &b_s);
2588 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2590 printf("-%s", ds_cstr(&a_s));
2593 printf("+%s", ds_cstr(&b_s));
2599 fat_rwlock_unlock(&cls.rwlock);
2612 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2614 struct ofputil_meter_mod mm;
2615 struct vconn *vconn;
2616 enum ofputil_protocol protocol;
2617 enum ofputil_protocol usable_protocols;
2618 enum ofp_version version;
2622 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2624 ovs_fatal(0, "%s", error);
2627 usable_protocols = OFPUTIL_P_OF13_UP;
2628 mm.command = command;
2629 mm.meter.meter_id = OFPM13_ALL;
2632 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2633 version = ofputil_protocol_to_ofp_version(protocol);
2634 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2639 ofctl_meter_request__(const char *bridge, const char *str,
2640 enum ofputil_meter_request_type type)
2642 struct ofputil_meter_mod mm;
2643 struct vconn *vconn;
2644 enum ofputil_protocol usable_protocols;
2645 enum ofputil_protocol protocol;
2646 enum ofp_version version;
2650 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2652 ovs_fatal(0, "%s", error);
2655 usable_protocols = OFPUTIL_P_OF13_UP;
2656 mm.meter.meter_id = OFPM13_ALL;
2659 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2660 version = ofputil_protocol_to_ofp_version(protocol);
2661 transact_noreply(vconn, ofputil_encode_meter_request(version,
2663 mm.meter.meter_id));
2669 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2671 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2675 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2677 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2681 ofctl_del_meters(int argc, char *argv[])
2683 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2687 ofctl_dump_meters(int argc, char *argv[])
2689 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2690 OFPUTIL_METER_CONFIG);
2694 ofctl_meter_stats(int argc, char *argv[])
2696 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2697 OFPUTIL_METER_STATS);
2701 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2703 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2707 /* Undocumented commands for unit testing. */
2710 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2711 enum ofputil_protocol usable_protocols)
2713 enum ofputil_protocol protocol = 0;
2717 usable_s = ofputil_protocols_to_string(usable_protocols);
2718 printf("usable protocols: %s\n", usable_s);
2721 if (!(usable_protocols & allowed_protocols)) {
2722 ovs_fatal(0, "no usable protocol");
2724 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2726 if (protocol & usable_protocols & allowed_protocols) {
2730 ovs_assert(is_pow2(protocol));
2732 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2734 for (i = 0; i < n_fms; i++) {
2735 struct ofputil_flow_mod *fm = &fms[i];
2738 msg = ofputil_encode_flow_mod(fm, protocol);
2739 ofp_print(stdout, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
2746 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2747 * it back to stdout. */
2749 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2751 enum ofputil_protocol usable_protocols;
2752 struct ofputil_flow_mod fm;
2755 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2757 ovs_fatal(0, "%s", error);
2759 ofctl_parse_flows__(&fm, 1, usable_protocols);
2762 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2763 * add-flows) and prints each of the flows back to stdout. */
2765 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2767 enum ofputil_protocol usable_protocols;
2768 struct ofputil_flow_mod *fms = NULL;
2772 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2775 ovs_fatal(0, "%s", error);
2777 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2782 ofctl_parse_nxm__(bool oxm)
2787 while (!ds_get_test_line(&in, stdin)) {
2788 struct ofpbuf nx_match;
2790 ovs_be64 cookie, cookie_mask;
2794 /* Convert string to nx_match. */
2795 ofpbuf_init(&nx_match, 0);
2797 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2799 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2802 /* Convert nx_match to match. */
2805 error = oxm_pull_match(&nx_match, &match);
2807 error = nx_pull_match(&nx_match, match_len, &match,
2808 &cookie, &cookie_mask);
2812 error = oxm_pull_match_loose(&nx_match, &match);
2814 error = nx_pull_match_loose(&nx_match, match_len, &match,
2815 &cookie, &cookie_mask);
2823 /* Convert match back to nx_match. */
2824 ofpbuf_uninit(&nx_match);
2825 ofpbuf_init(&nx_match, 0);
2827 match_len = oxm_put_match(&nx_match, &match);
2828 out = oxm_match_to_string(&nx_match, match_len);
2830 match_len = nx_put_match(&nx_match, &match,
2831 cookie, cookie_mask);
2832 out = nx_match_to_string(ofpbuf_data(&nx_match), match_len);
2838 printf("nx_pull_match() returned error %s\n",
2839 ofperr_get_name(error));
2842 ofpbuf_uninit(&nx_match);
2847 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2848 * stdin, does some internal fussing with them, and then prints them back as
2849 * strings on stdout. */
2851 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2853 return ofctl_parse_nxm__(false);
2856 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2857 * stdin, does some internal fussing with them, and then prints them back as
2858 * strings on stdout. */
2860 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2862 return ofctl_parse_nxm__(true);
2866 print_differences(const char *prefix,
2867 const void *a_, size_t a_len,
2868 const void *b_, size_t b_len)
2870 const uint8_t *a = a_;
2871 const uint8_t *b = b_;
2874 for (i = 0; i < MIN(a_len, b_len); i++) {
2876 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2877 prefix, i, a[i], b[i]);
2880 for (i = a_len; i < b_len; i++) {
2881 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2883 for (i = b_len; i < a_len; i++) {
2884 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2888 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2889 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2890 * on stdout, and then converts them back to hex bytes and prints any
2891 * differences from the input. */
2893 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2898 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2899 struct ofpbuf of10_out;
2900 struct ofpbuf of10_in;
2901 struct ofpbuf ofpacts;
2906 /* Parse hex bytes. */
2907 ofpbuf_init(&of10_in, 0);
2908 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2909 ovs_fatal(0, "Trailing garbage in hex data");
2912 /* Convert to ofpacts. */
2913 ofpbuf_init(&ofpacts, 0);
2914 size = ofpbuf_size(&of10_in);
2915 error = ofpacts_pull_openflow_actions(&of10_in, ofpbuf_size(&of10_in),
2916 OFP10_VERSION, &ofpacts);
2918 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2919 ofpbuf_uninit(&ofpacts);
2920 ofpbuf_uninit(&of10_in);
2923 ofpbuf_push_uninit(&of10_in, size);
2925 /* Print cls_rule. */
2927 ds_put_cstr(&s, "actions=");
2928 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
2932 /* Convert back to ofp10 actions and print differences from input. */
2933 ofpbuf_init(&of10_out, 0);
2934 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of10_out,
2937 print_differences("", ofpbuf_data(&of10_in), ofpbuf_size(&of10_in),
2938 ofpbuf_data(&of10_out), ofpbuf_size(&of10_out));
2941 ofpbuf_uninit(&ofpacts);
2942 ofpbuf_uninit(&of10_in);
2943 ofpbuf_uninit(&of10_out);
2948 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2949 * bytes from stdin, converts them to cls_rules, prints them as strings on
2950 * stdout, and then converts them back to hex bytes and prints any differences
2953 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2954 * values are ignored in the input and will be set to zero when OVS converts
2955 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2956 * it does the conversion to hex, to ensure that in fact they are ignored. */
2958 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2965 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2966 struct ofpbuf match_in, match_expout;
2967 struct ofp10_match match_out;
2968 struct ofp10_match match_normal;
2972 /* Parse hex bytes to use for expected output. */
2974 ds_put_cstr(&expout, ds_cstr(&in));
2975 for (p = ds_cstr(&expout); *p; p++) {
2980 ofpbuf_init(&match_expout, 0);
2981 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2982 ovs_fatal(0, "Trailing garbage in hex data");
2984 if (ofpbuf_size(&match_expout) != sizeof(struct ofp10_match)) {
2985 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
2986 ofpbuf_size(&match_expout), sizeof(struct ofp10_match));
2989 /* Parse hex bytes for input. */
2990 for (p = ds_cstr(&in); *p; p++) {
2992 *p = "0123456789abcdef"[random_uint32() & 0xf];
2995 ofpbuf_init(&match_in, 0);
2996 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2997 ovs_fatal(0, "Trailing garbage in hex data");
2999 if (ofpbuf_size(&match_in) != sizeof(struct ofp10_match)) {
3000 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3001 ofpbuf_size(&match_in), sizeof(struct ofp10_match));
3004 /* Convert to cls_rule and print. */
3005 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in), &match);
3006 match_print(&match);
3008 /* Convert back to ofp10_match and print differences from input. */
3009 ofputil_match_to_ofp10_match(&match, &match_out);
3010 print_differences("", ofpbuf_data(&match_expout), ofpbuf_size(&match_expout),
3011 &match_out, sizeof match_out);
3013 /* Normalize, then convert and compare again. */
3014 ofputil_normalize_match(&match);
3015 ofputil_match_to_ofp10_match(&match, &match_normal);
3016 print_differences("normal: ", &match_out, sizeof match_out,
3017 &match_normal, sizeof match_normal);
3020 ofpbuf_uninit(&match_in);
3021 ofpbuf_uninit(&match_expout);
3024 ds_destroy(&expout);
3027 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3028 * bytes from stdin, converts them to "struct match"es, prints them as strings
3029 * on stdout, and then converts them back to hex bytes and prints any
3030 * differences from the input. */
3032 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3037 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3038 struct ofpbuf match_in;
3039 struct ofp11_match match_out;
3043 /* Parse hex bytes. */
3044 ofpbuf_init(&match_in, 0);
3045 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3046 ovs_fatal(0, "Trailing garbage in hex data");
3048 if (ofpbuf_size(&match_in) != sizeof(struct ofp11_match)) {
3049 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3050 ofpbuf_size(&match_in), sizeof(struct ofp11_match));
3053 /* Convert to match. */
3054 error = ofputil_match_from_ofp11_match(ofpbuf_data(&match_in), &match);
3056 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3057 ofpbuf_uninit(&match_in);
3062 match_print(&match);
3064 /* Convert back to ofp11_match and print differences from input. */
3065 ofputil_match_to_ofp11_match(&match, &match_out);
3067 print_differences("", ofpbuf_data(&match_in), ofpbuf_size(&match_in),
3068 &match_out, sizeof match_out);
3071 ofpbuf_uninit(&match_in);
3076 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3077 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3078 * on stdout, and then converts them back to hex bytes and prints any
3079 * differences from the input. */
3081 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3086 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3087 struct ofpbuf of11_out;
3088 struct ofpbuf of11_in;
3089 struct ofpbuf ofpacts;
3094 /* Parse hex bytes. */
3095 ofpbuf_init(&of11_in, 0);
3096 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3097 ovs_fatal(0, "Trailing garbage in hex data");
3100 /* Convert to ofpacts. */
3101 ofpbuf_init(&ofpacts, 0);
3102 size = ofpbuf_size(&of11_in);
3103 error = ofpacts_pull_openflow_actions(&of11_in, ofpbuf_size(&of11_in),
3104 OFP11_VERSION, &ofpacts);
3106 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3107 ofpbuf_uninit(&ofpacts);
3108 ofpbuf_uninit(&of11_in);
3111 ofpbuf_push_uninit(&of11_in, size);
3113 /* Print cls_rule. */
3115 ds_put_cstr(&s, "actions=");
3116 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3120 /* Convert back to ofp11 actions and print differences from input. */
3121 ofpbuf_init(&of11_out, 0);
3122 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of11_out,
3125 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3126 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3129 ofpbuf_uninit(&ofpacts);
3130 ofpbuf_uninit(&of11_in);
3131 ofpbuf_uninit(&of11_out);
3136 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3137 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3138 * them as strings on stdout, and then converts them back to hex bytes and
3139 * prints any differences from the input. */
3141 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3146 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3147 struct ofpbuf of11_out;
3148 struct ofpbuf of11_in;
3149 struct ofpbuf ofpacts;
3153 const char *table_id;
3156 /* Parse table_id separated with the follow-up instructions by ",", if
3158 instructions = ds_cstr(&in);
3160 if (strstr(instructions, ",")) {
3161 table_id = strsep(&instructions, ",");
3164 /* Parse hex bytes. */
3165 ofpbuf_init(&of11_in, 0);
3166 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3167 ovs_fatal(0, "Trailing garbage in hex data");
3170 /* Convert to ofpacts. */
3171 ofpbuf_init(&ofpacts, 0);
3172 size = ofpbuf_size(&of11_in);
3173 error = ofpacts_pull_openflow_instructions(&of11_in, ofpbuf_size(&of11_in),
3174 OFP11_VERSION, &ofpacts);
3176 /* Verify actions, enforce consistency. */
3178 memset(&flow, 0, sizeof flow);
3179 error = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3181 table_id ? atoi(table_id) : 0,
3182 255, OFPUTIL_P_OF11_STD);
3185 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3186 ofpbuf_uninit(&ofpacts);
3187 ofpbuf_uninit(&of11_in);
3190 ofpbuf_push_uninit(&of11_in, size);
3192 /* Print cls_rule. */
3194 ds_put_cstr(&s, "actions=");
3195 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3199 /* Convert back to ofp11 instructions and print differences from
3201 ofpbuf_init(&of11_out, 0);
3202 ofpacts_put_openflow_instructions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3203 &of11_out, OFP13_VERSION);
3205 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3206 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3209 ofpbuf_uninit(&ofpacts);
3210 ofpbuf_uninit(&of11_in);
3211 ofpbuf_uninit(&of11_out);
3216 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3218 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3222 pcap = ovs_pcap_open(argv[1], "rb");
3224 ovs_fatal(errno, "%s: open failed", argv[1]);
3228 struct ofpbuf *packet;
3230 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3233 error = ovs_pcap_read(pcap, &packet, NULL);
3237 ovs_fatal(error, "%s: read failed", argv[1]);
3240 flow_extract(packet, &md, &flow);
3241 flow_print(stdout, &flow);
3243 ofpbuf_delete(packet);
3247 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3248 * mask values to and from various formats and prints the results. */
3250 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3255 struct ofputil_flow_mod fm;
3258 struct match nxm_match;
3262 struct ofp10_match of10_raw;
3263 struct match of10_match;
3265 struct ofp11_match of11_raw;
3266 struct match of11_match;
3271 enum ofputil_protocol usable_protocols; /* Unused for now. */
3273 match_init_catchall(&match);
3274 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3275 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3277 /* Convert to and from string. */
3278 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3279 printf("%s -> ", string_s);
3281 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3283 ovs_fatal(0, "%s", error_s);
3285 printf("%04"PRIx16"/%04"PRIx16"\n",
3286 ntohs(fm.match.flow.vlan_tci),
3287 ntohs(fm.match.wc.masks.vlan_tci));
3290 /* Convert to and from NXM. */
3291 ofpbuf_init(&nxm, 0);
3292 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3293 nxm_s = nx_match_to_string(ofpbuf_data(&nxm), nxm_match_len);
3294 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3295 printf("NXM: %s -> ", nxm_s);
3297 printf("%s\n", ofperr_to_string(error));
3299 printf("%04"PRIx16"/%04"PRIx16"\n",
3300 ntohs(nxm_match.flow.vlan_tci),
3301 ntohs(nxm_match.wc.masks.vlan_tci));
3304 ofpbuf_uninit(&nxm);
3306 /* Convert to and from OXM. */
3307 ofpbuf_init(&nxm, 0);
3308 nxm_match_len = oxm_put_match(&nxm, &match);
3309 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3310 error = oxm_pull_match(&nxm, &nxm_match);
3311 printf("OXM: %s -> ", nxm_s);
3313 printf("%s\n", ofperr_to_string(error));
3315 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3316 (VLAN_VID_MASK | VLAN_CFI);
3317 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3318 (VLAN_VID_MASK | VLAN_CFI);
3320 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3321 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3322 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3328 ofpbuf_uninit(&nxm);
3330 /* Convert to and from OpenFlow 1.0. */
3331 ofputil_match_to_ofp10_match(&match, &of10_raw);
3332 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3333 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3334 ntohs(of10_raw.dl_vlan),
3335 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3336 of10_raw.dl_vlan_pcp,
3337 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3338 ntohs(of10_match.flow.vlan_tci),
3339 ntohs(of10_match.wc.masks.vlan_tci));
3341 /* Convert to and from OpenFlow 1.1. */
3342 ofputil_match_to_ofp11_match(&match, &of11_raw);
3343 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3344 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3345 ntohs(of11_raw.dl_vlan),
3346 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3347 of11_raw.dl_vlan_pcp,
3348 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3349 ntohs(of11_match.flow.vlan_tci),
3350 ntohs(of11_match.wc.masks.vlan_tci));
3353 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3356 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3361 error = ofperr_from_name(argv[1]);
3363 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3366 for (version = 0; version <= UINT8_MAX; version++) {
3367 const char *name = ofperr_domain_get_name(version);
3369 int vendor = ofperr_get_vendor(error, version);
3370 int type = ofperr_get_type(error, version);
3371 int code = ofperr_get_code(error, version);
3373 if (vendor != -1 || type != -1 || code != -1) {
3374 printf("%s: vendor %#x, type %d, code %d\n",
3375 name, vendor, type, code);
3381 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3382 * error named ENUM and prints the error reply in hex. */
3384 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3386 const struct ofp_header *oh;
3387 struct ofpbuf request, *reply;
3390 error = ofperr_from_name(argv[1]);
3392 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3395 ofpbuf_init(&request, 0);
3396 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3397 ovs_fatal(0, "Trailing garbage in hex data");
3399 if (ofpbuf_size(&request) < sizeof(struct ofp_header)) {
3400 ovs_fatal(0, "Request too short");
3403 oh = ofpbuf_data(&request);
3404 if (ofpbuf_size(&request) != ntohs(oh->length)) {
3405 ovs_fatal(0, "Request size inconsistent");
3408 reply = ofperr_encode_reply(error, ofpbuf_data(&request));
3409 ofpbuf_uninit(&request);
3411 ovs_hex_dump(stdout, ofpbuf_data(reply), ofpbuf_size(reply), 0, false);
3412 ofpbuf_delete(reply);
3415 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3416 * binary data, interpreting them as an OpenFlow message, and prints the
3417 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3419 ofctl_ofp_print(int argc, char *argv[])
3421 struct ofpbuf packet;
3423 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3424 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3425 ovs_fatal(0, "trailing garbage following hex bytes");
3427 ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), argc > 2 ? atoi(argv[2]) : 2);
3428 ofpbuf_uninit(&packet);
3431 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3432 * and dumps each message in hex. */
3434 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3436 uint32_t bitmap = strtol(argv[1], NULL, 0);
3437 struct ofpbuf *hello;
3439 hello = ofputil_encode_hello(bitmap);
3440 ovs_hex_dump(stdout, ofpbuf_data(hello), ofpbuf_size(hello), 0, false);
3441 ofp_print(stdout, ofpbuf_data(hello), ofpbuf_size(hello), verbosity);
3442 ofpbuf_delete(hello);
3445 static const struct command all_commands[] = {
3446 { "show", 1, 1, ofctl_show },
3447 { "monitor", 1, 3, ofctl_monitor },
3448 { "snoop", 1, 1, ofctl_snoop },
3449 { "dump-desc", 1, 1, ofctl_dump_desc },
3450 { "dump-tables", 1, 1, ofctl_dump_tables },
3451 { "dump-table-features", 1, 1, ofctl_dump_table_features },
3452 { "dump-flows", 1, 2, ofctl_dump_flows },
3453 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3454 { "queue-stats", 1, 3, ofctl_queue_stats },
3455 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3456 { "add-flow", 2, 2, ofctl_add_flow },
3457 { "add-flows", 2, 2, ofctl_add_flows },
3458 { "mod-flows", 2, 2, ofctl_mod_flows },
3459 { "del-flows", 1, 2, ofctl_del_flows },
3460 { "replace-flows", 2, 2, ofctl_replace_flows },
3461 { "diff-flows", 2, 2, ofctl_diff_flows },
3462 { "add-meter", 2, 2, ofctl_add_meter },
3463 { "mod-meter", 2, 2, ofctl_mod_meter },
3464 { "del-meter", 2, 2, ofctl_del_meters },
3465 { "del-meters", 1, 1, ofctl_del_meters },
3466 { "dump-meter", 2, 2, ofctl_dump_meters },
3467 { "dump-meters", 1, 1, ofctl_dump_meters },
3468 { "meter-stats", 1, 2, ofctl_meter_stats },
3469 { "meter-features", 1, 1, ofctl_meter_features },
3470 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3471 { "dump-ports", 1, 2, ofctl_dump_ports },
3472 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3473 { "mod-port", 3, 3, ofctl_mod_port },
3474 { "mod-table", 3, 3, ofctl_mod_table },
3475 { "get-frags", 1, 1, ofctl_get_frags },
3476 { "set-frags", 2, 2, ofctl_set_frags },
3477 { "probe", 1, 1, ofctl_probe },
3478 { "ping", 1, 2, ofctl_ping },
3479 { "benchmark", 3, 3, ofctl_benchmark },
3481 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3482 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3484 { "add-group", 1, 2, ofctl_add_group },
3485 { "add-groups", 1, 2, ofctl_add_groups },
3486 { "mod-group", 1, 2, ofctl_mod_group },
3487 { "del-groups", 1, 2, ofctl_del_groups },
3488 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3489 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3490 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3491 { "help", 0, INT_MAX, ofctl_help },
3493 /* Undocumented commands for testing. */
3494 { "parse-flow", 1, 1, ofctl_parse_flow },
3495 { "parse-flows", 1, 1, ofctl_parse_flows },
3496 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3497 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3498 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3499 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3500 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3501 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3502 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3503 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3504 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3505 { "check-vlan", 2, 2, ofctl_check_vlan },
3506 { "print-error", 1, 1, ofctl_print_error },
3507 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3508 { "ofp-print", 1, 2, ofctl_ofp_print },
3509 { "encode-hello", 1, 1, ofctl_encode_hello },
3511 { NULL, 0, 0, NULL },
3514 static const struct command *get_all_commands(void)
3516 return all_commands;