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>
28 #include <sys/fcntl.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
41 #include "ofp-actions.h"
42 #include "ofp-errors.h"
44 #include "ofp-parse.h"
45 #include "ofp-print.h"
47 #include "ofp-version-opt.h"
49 #include "ofproto/ofproto.h"
50 #include "openflow/nicira-ext.h"
51 #include "openflow/openflow.h"
53 #include "pcap-file.h"
54 #include "poll-loop.h"
56 #include "stream-ssl.h"
57 #include "socket-util.h"
63 #include "meta-flow.h"
66 VLOG_DEFINE_THIS_MODULE(ofctl);
68 /* --strict: Use strict matching for flow mod commands? Additionally governs
69 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
73 /* --readd: If true, on replace-flows, re-add even flows that have not changed
74 * (to reset flow counters). */
77 /* -F, --flow-format: Allowed protocols. By default, any protocol is
79 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
81 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
82 * commands. Either one of NXPIF_* to force a particular packet_in format, or
83 * -1 to let ovs-ofctl choose the default. */
84 static int preferred_packet_in_format = -1;
86 /* -m, --more: Additional verbosity for ofp-print functions. */
89 /* --timestamp: Print a timestamp before each received packet on "monitor" and
91 static bool timestamp;
93 /* --sort, --rsort: Sort order. */
94 enum sort_order { SORT_ASC, SORT_DESC };
95 struct sort_criterion {
96 const struct mf_field *field; /* NULL means to sort by priority. */
97 enum sort_order order;
99 static struct sort_criterion *criteria;
100 static size_t n_criteria, allocated_criteria;
102 static const struct command *get_all_commands(void);
104 static void usage(void) NO_RETURN;
105 static void parse_options(int argc, char *argv[]);
107 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
108 struct ofpbuf **replyp,
109 struct ofputil_flow_stats *,
110 struct ofpbuf *ofpacts);
112 main(int argc, char *argv[])
114 set_program_name(argv[0]);
115 parse_options(argc, argv);
116 signal(SIGPIPE, SIG_IGN);
117 run_command(argc - optind, argv + optind, get_all_commands());
122 add_sort_criterion(enum sort_order order, const char *field)
124 struct sort_criterion *sc;
126 if (n_criteria >= allocated_criteria) {
127 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
130 sc = &criteria[n_criteria++];
131 if (!field || !strcasecmp(field, "priority")) {
134 sc->field = mf_from_name(field);
136 ovs_fatal(0, "%s: unknown field name", field);
143 parse_options(int argc, char *argv[])
146 OPT_STRICT = UCHAR_MAX + 1,
152 OFP_VERSION_OPTION_ENUMS,
155 static const struct option long_options[] = {
156 {"timeout", required_argument, NULL, 't'},
157 {"strict", no_argument, NULL, OPT_STRICT},
158 {"readd", no_argument, NULL, OPT_READD},
159 {"flow-format", required_argument, NULL, 'F'},
160 {"packet-in-format", required_argument, NULL, 'P'},
161 {"more", no_argument, NULL, 'm'},
162 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
163 {"sort", optional_argument, NULL, OPT_SORT},
164 {"rsort", optional_argument, NULL, OPT_RSORT},
165 {"help", no_argument, NULL, 'h'},
167 OFP_VERSION_LONG_OPTIONS,
169 STREAM_SSL_LONG_OPTIONS,
172 char *short_options = long_options_to_short_options(long_options);
174 enum ofputil_protocol version_protocols;
177 unsigned long int timeout;
180 c = getopt_long(argc, argv, short_options, long_options, NULL);
187 timeout = strtoul(optarg, NULL, 10);
189 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
197 allowed_protocols = ofputil_protocols_from_string(optarg);
198 if (!allowed_protocols) {
199 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
204 preferred_packet_in_format =
205 ofputil_packet_in_format_from_string(optarg);
206 if (preferred_packet_in_format < 0) {
207 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
231 add_sort_criterion(SORT_ASC, optarg);
235 add_sort_criterion(SORT_DESC, optarg);
238 DAEMON_OPTION_HANDLERS
239 OFP_VERSION_OPTION_HANDLERS
241 STREAM_SSL_OPTION_HANDLERS
252 /* Always do a final sort pass based on priority. */
253 add_sort_criterion(SORT_DESC, "priority");
258 versions = get_allowed_ofp_versions();
259 version_protocols = ofputil_protocols_from_version_bitmap(versions);
260 if (!(allowed_protocols & version_protocols)) {
261 char *protocols = ofputil_protocols_to_string(allowed_protocols);
262 struct ds version_s = DS_EMPTY_INITIALIZER;
264 ofputil_format_version_bitmap_names(&version_s, versions);
265 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
266 "any of the enabled flow formats (%s). (Use -O to enable "
267 "additional OpenFlow versions or -F to enable additional "
268 "flow formats.)", ds_cstr(&version_s), protocols);
270 allowed_protocols &= version_protocols;
271 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
278 printf("%s: OpenFlow switch management utility\n"
279 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
280 "\nFor OpenFlow switches:\n"
281 " show SWITCH show OpenFlow information\n"
282 " dump-desc SWITCH print switch description\n"
283 " dump-tables SWITCH print table stats\n"
284 " mod-port SWITCH IFACE ACT modify port behavior\n"
285 " mod-table SWITCH MOD modify flow table behavior\n"
286 " get-frags SWITCH print fragment handling behavior\n"
287 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
288 " dump-ports SWITCH [PORT] print port statistics\n"
289 " dump-ports-desc SWITCH print port descriptions\n"
290 " dump-flows SWITCH print all flow entries\n"
291 " dump-flows SWITCH FLOW print matching FLOWs\n"
292 " dump-aggregate SWITCH print aggregate flow statistics\n"
293 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
294 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
295 " add-flow SWITCH FLOW add flow described by FLOW\n"
296 " add-flows SWITCH FILE add flows from FILE\n"
297 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
298 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
299 " replace-flows SWITCH FILE replace flows with those in FILE\n"
300 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
301 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
302 " execute ACTIONS on PACKET\n"
303 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
304 " print packets received from SWITCH\n"
305 " snoop SWITCH snoop on SWITCH and its controller\n"
306 " add-group SWITCH GROUP add group described by GROUP\n"
307 " add-group SWITCH FILE add group from FILE\n"
308 " mod-group SWITCH GROUP modify specific group\n"
309 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
310 " dump-group-features SWITCH print group features\n"
311 " dump-groups SWITCH print group description\n"
312 " dump-group-stats SWITCH [GROUP] print group statistics\n"
313 " queue-get-config SWITCH PORT print queue information for port\n"
314 " add-meter SWITCH METER add meter described by METER\n"
315 " mod-meter SWITCH METER modify specific METER\n"
316 " del-meter SWITCH METER delete METER\n"
317 " del-meters SWITCH delete all meters\n"
318 " dump-meter SWITCH METER print METER configuration\n"
319 " dump-meters SWITCH print all meter configuration\n"
320 " meter-stats SWITCH [METER] print meter statistics\n"
321 " meter-features SWITCH print meter features\n"
322 "\nFor OpenFlow switches and controllers:\n"
323 " probe TARGET probe whether TARGET is up\n"
324 " ping TARGET [N] latency of N-byte echos\n"
325 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
326 "SWITCH or TARGET is an active OpenFlow connection method.\n"
327 "\nOther commands:\n"
328 " ofp-parse FILE print messages read from FILE\n",
329 program_name, program_name);
330 vconn_usage(true, false, false);
334 printf("\nOther options:\n"
335 " --strict use strict match for flow commands\n"
336 " --readd replace flows that haven't changed\n"
337 " -F, --flow-format=FORMAT force particular flow format\n"
338 " -P, --packet-in-format=FRMT force particular packet in format\n"
339 " -m, --more be more verbose printing OpenFlow\n"
340 " --timestamp (monitor, snoop) print timestamps\n"
341 " -t, --timeout=SECS give up after SECS seconds\n"
342 " --sort[=field] sort in ascending order\n"
343 " --rsort[=field] sort in descending order\n"
344 " -h, --help display this help message\n"
345 " -V, --version display version information\n");
350 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
351 const char *argv[] OVS_UNUSED, void *exiting_)
353 bool *exiting = exiting_;
355 unixctl_command_reply(conn, NULL);
358 static void run(int retval, const char *message, ...)
362 run(int retval, const char *message, ...)
367 va_start(args, message);
368 ovs_fatal_valist(retval, message, args);
372 /* Generic commands. */
375 open_vconn_socket(const char *name, struct vconn **vconnp)
377 char *vconn_name = xasprintf("unix:%s", name);
380 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
382 if (error && error != ENOENT) {
383 ovs_fatal(0, "%s: failed to open socket (%s)", name,
384 ovs_strerror(error));
391 enum open_target { MGMT, SNOOP };
393 static enum ofputil_protocol
394 open_vconn__(const char *name, enum open_target target,
395 struct vconn **vconnp)
397 const char *suffix = target == MGMT ? "mgmt" : "snoop";
398 char *datapath_name, *datapath_type, *socket_name;
399 enum ofputil_protocol protocol;
404 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
406 ofproto_parse_name(name, &datapath_name, &datapath_type);
407 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
411 if (strchr(name, ':')) {
412 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
413 "connecting to %s", name);
414 } else if (!open_vconn_socket(name, vconnp)) {
416 } else if (!open_vconn_socket(bridge_path, vconnp)) {
418 } else if (!open_vconn_socket(socket_name, vconnp)) {
421 ovs_fatal(0, "%s is not a bridge or a socket", name);
424 if (target == SNOOP) {
425 vconn_set_recv_any_version(*vconnp);
431 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
432 error = vconn_connect_block(*vconnp);
434 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
435 ovs_strerror(error));
438 ofp_version = vconn_get_version(*vconnp);
439 protocol = ofputil_protocol_from_ofp_version(ofp_version);
441 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
447 static enum ofputil_protocol
448 open_vconn(const char *name, struct vconn **vconnp)
450 return open_vconn__(name, MGMT, vconnp);
454 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
456 ofpmsg_update_length(buffer);
457 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
461 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
463 struct ofpbuf *reply;
465 ofpmsg_update_length(request);
466 run(vconn_transact(vconn, request, &reply), "talking to %s",
467 vconn_get_name(vconn));
468 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
469 ofpbuf_delete(reply);
473 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
475 struct ofpbuf *request;
478 open_vconn(vconn_name, &vconn);
479 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
480 dump_transaction(vconn, request);
485 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
487 const struct ofp_header *request_oh = request->data;
488 ovs_be32 send_xid = request_oh->xid;
489 enum ofpraw request_raw;
490 enum ofpraw reply_raw;
493 ofpraw_decode_partial(&request_raw, request->data, request->size);
494 reply_raw = ofpraw_stats_request_to_reply(request_raw,
495 request_oh->version);
497 send_openflow_buffer(vconn, request);
500 struct ofpbuf *reply;
502 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
503 recv_xid = ((struct ofp_header *) reply->data)->xid;
504 if (send_xid == recv_xid) {
507 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
509 ofpraw_decode(&raw, reply->data);
510 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
512 } else if (raw == reply_raw) {
513 done = !ofpmp_more(reply->data);
515 ovs_fatal(0, "received bad reply: %s",
516 ofp_to_string(reply->data, reply->size,
520 VLOG_DBG("received reply with xid %08"PRIx32" "
521 "!= expected %08"PRIx32, recv_xid, send_xid);
523 ofpbuf_delete(reply);
528 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
530 struct ofpbuf *request;
533 open_vconn(vconn_name, &vconn);
534 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
535 dump_stats_transaction(vconn, request);
539 /* Sends all of the 'requests', which should be requests that only have replies
540 * if an error occurs, and waits for them to succeed or fail. If an error does
541 * occur, prints it and exits with an error.
543 * Destroys all of the 'requests'. */
545 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
547 struct ofpbuf *request, *reply;
549 LIST_FOR_EACH (request, list_node, requests) {
550 ofpmsg_update_length(request);
553 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
554 "talking to %s", vconn_get_name(vconn));
556 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
559 ofpbuf_delete(reply);
562 /* Sends 'request', which should be a request that only has a reply if an error
563 * occurs, and waits for it to succeed or fail. If an error does occur, prints
564 * it and exits with an error.
566 * Destroys 'request'. */
568 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
570 struct list requests;
572 list_init(&requests);
573 list_push_back(&requests, &request->list_node);
574 transact_multiple_noreply(vconn, &requests);
578 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
580 struct ofp_switch_config *config;
581 struct ofpbuf *request;
582 struct ofpbuf *reply;
585 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
586 vconn_get_version(vconn), 0);
587 run(vconn_transact(vconn, request, &reply),
588 "talking to %s", vconn_get_name(vconn));
590 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
591 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
594 config = ofpbuf_pull(reply, sizeof *config);
597 ofpbuf_delete(reply);
601 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
603 struct ofpbuf *request;
605 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
606 ofpbuf_put(request, config, sizeof *config);
608 transact_noreply(vconn, request);
612 ofctl_show(int argc OVS_UNUSED, char *argv[])
614 const char *vconn_name = argv[1];
616 struct ofpbuf *request;
617 struct ofpbuf *reply;
620 open_vconn(vconn_name, &vconn);
621 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
622 vconn_get_version(vconn), 0);
623 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
625 trunc = ofputil_switch_features_ports_trunc(reply);
626 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
628 ofpbuf_delete(reply);
631 /* The Features Reply may not contain all the ports, so send a
632 * Port Description stats request, which doesn't have size
634 dump_trivial_stats_transaction(vconn_name,
635 OFPRAW_OFPST_PORT_DESC_REQUEST);
637 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
642 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
644 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
648 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
650 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
654 fetch_port_by_features(const char *vconn_name,
655 const char *port_name, ofp_port_t port_no,
656 struct ofputil_phy_port *pp, bool *trunc)
658 struct ofputil_switch_features features;
659 const struct ofp_header *oh;
660 struct ofpbuf *request, *reply;
667 /* Fetch the switch's ofp_switch_features. */
668 open_vconn(vconn_name, &vconn);
669 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
670 vconn_get_version(vconn), 0);
671 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
675 if (ofptype_decode(&type, reply->data)
676 || type != OFPTYPE_FEATURES_REPLY) {
677 ovs_fatal(0, "%s: received bad features reply", vconn_name);
681 if (ofputil_switch_features_ports_trunc(reply)) {
686 error = ofputil_decode_switch_features(oh, &features, &b);
688 ovs_fatal(0, "%s: failed to decode features reply (%s)",
689 vconn_name, ofperr_to_string(error));
692 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
693 if (port_no != OFPP_NONE
694 ? port_no == pp->port_no
695 : !strcmp(pp->name, port_name)) {
702 ofpbuf_delete(reply);
707 fetch_port_by_stats(const char *vconn_name,
708 const char *port_name, ofp_port_t port_no,
709 struct ofputil_phy_port *pp)
711 struct ofpbuf *request;
717 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
718 send_xid = ((struct ofp_header *) request->data)->xid;
720 open_vconn(vconn_name, &vconn);
721 send_openflow_buffer(vconn, request);
724 struct ofpbuf *reply;
726 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
727 recv_xid = ((struct ofp_header *) reply->data)->xid;
728 if (send_xid == recv_xid) {
729 struct ofp_header *oh = reply->data;
734 ofpbuf_use_const(&b, oh, ntohs(oh->length));
735 if (ofptype_pull(&type, &b)
736 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
737 ovs_fatal(0, "received bad reply: %s",
738 ofp_to_string(reply->data, reply->size,
742 flags = ofpmp_flags(oh);
743 done = !(flags & OFPSF_REPLY_MORE);
746 /* We've already found the port, but we need to drain
747 * the queue of any other replies for this request. */
751 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
752 if (port_no != OFPP_NONE ? port_no == pp->port_no
753 : !strcmp(pp->name, port_name)) {
759 VLOG_DBG("received reply with xid %08"PRIx32" "
760 "!= expected %08"PRIx32, recv_xid, send_xid);
762 ofpbuf_delete(reply);
770 str_to_ofp(const char *s, ofp_port_t *ofp_port)
775 ret = str_to_uint(s, 10, &port_);
776 *ofp_port = u16_to_ofp(port_);
780 /* Opens a connection to 'vconn_name', fetches the port structure for
781 * 'port_name' (which may be a port name or number), and copies it into
784 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
785 struct ofputil_phy_port *pp)
791 /* Try to interpret the argument as a port number. */
792 if (!str_to_ofp(port_name, &port_no)) {
796 /* Try to find the port based on the Features Reply. If it looks
797 * like the results may be truncated, then use the Port Description
798 * stats message introduced in OVS 1.7. */
799 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
802 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
806 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
810 /* Returns the port number corresponding to 'port_name' (which may be a port
811 * name or number) within the switch 'vconn_name'. */
813 str_to_port_no(const char *vconn_name, const char *port_name)
817 if (ofputil_port_from_string(port_name, &port_no)) {
820 struct ofputil_phy_port pp;
822 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
828 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
829 enum ofputil_protocol *cur)
832 struct ofpbuf *request, *reply;
833 enum ofputil_protocol next;
835 request = ofputil_encode_set_protocol(*cur, want, &next);
840 run(vconn_transact_noreply(vconn, request, &reply),
841 "talking to %s", vconn_get_name(vconn));
843 char *s = ofp_to_string(reply->data, reply->size, 2);
844 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
845 vconn_get_name(vconn), s);
847 ofpbuf_delete(reply);
855 static enum ofputil_protocol
856 set_protocol_for_flow_dump(struct vconn *vconn,
857 enum ofputil_protocol cur_protocol,
858 enum ofputil_protocol usable_protocols)
863 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
864 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
865 if (f & usable_protocols & allowed_protocols
866 && try_set_protocol(vconn, f, &cur_protocol)) {
871 usable_s = ofputil_protocols_to_string(usable_protocols);
872 if (usable_protocols & allowed_protocols) {
873 ovs_fatal(0, "switch does not support any of the usable flow "
874 "formats (%s)", usable_s);
876 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
877 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
878 "allowed flow formats (%s)", usable_s, allowed_s);
882 static struct vconn *
883 prepare_dump_flows(int argc, char *argv[], bool aggregate,
884 struct ofpbuf **requestp)
886 enum ofputil_protocol usable_protocols, protocol;
887 struct ofputil_flow_stats_request fsr;
891 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
892 argc > 2 ? argv[2] : "",
895 ovs_fatal(0, "%s", error);
898 protocol = open_vconn(argv[1], &vconn);
899 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
900 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
905 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
907 struct ofpbuf *request;
910 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
911 dump_stats_transaction(vconn, request);
916 compare_flows(const void *afs_, const void *bfs_)
918 const struct ofputil_flow_stats *afs = afs_;
919 const struct ofputil_flow_stats *bfs = bfs_;
920 const struct match *a = &afs->match;
921 const struct match *b = &bfs->match;
922 const struct sort_criterion *sc;
924 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
925 const struct mf_field *f = sc->field;
929 unsigned int a_pri = afs->priority;
930 unsigned int b_pri = bfs->priority;
931 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
935 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
936 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
938 /* Skip the test for sc->order, so that missing fields always
939 * sort to the end whether we're sorting in ascending or
940 * descending order. */
943 union mf_value aval, bval;
945 mf_get_value(f, &a->flow, &aval);
946 mf_get_value(f, &b->flow, &bval);
947 ret = memcmp(&aval, &bval, f->n_bytes);
952 return sc->order == SORT_ASC ? ret : -ret;
960 ofctl_dump_flows(int argc, char *argv[])
963 return ofctl_dump_flows__(argc, argv, false);
965 struct ofputil_flow_stats *fses;
966 size_t n_fses, allocated_fses;
967 struct ofpbuf *request;
968 struct ofpbuf ofpacts;
969 struct ofpbuf *reply;
975 vconn = prepare_dump_flows(argc, argv, false, &request);
976 send_xid = ((struct ofp_header *) request->data)->xid;
977 send_openflow_buffer(vconn, request);
980 n_fses = allocated_fses = 0;
982 ofpbuf_init(&ofpacts, 0);
984 struct ofputil_flow_stats *fs;
986 if (n_fses >= allocated_fses) {
987 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
991 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
995 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
998 ofpbuf_uninit(&ofpacts);
1000 qsort(fses, n_fses, sizeof *fses, compare_flows);
1003 for (i = 0; i < n_fses; i++) {
1005 ofp_print_flow_stats(&s, &fses[i]);
1010 for (i = 0; i < n_fses; i++) {
1011 free(fses[i].ofpacts);
1020 ofctl_dump_aggregate(int argc, char *argv[])
1022 return ofctl_dump_flows__(argc, argv, true);
1026 ofctl_queue_stats(int argc, char *argv[])
1028 struct ofpbuf *request;
1029 struct vconn *vconn;
1030 struct ofputil_queue_stats_request oqs;
1032 open_vconn(argv[1], &vconn);
1034 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1035 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1037 oqs.port_no = OFPP_ANY;
1039 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1040 oqs.queue_id = atoi(argv[3]);
1042 oqs.queue_id = OFPQ_ALL;
1045 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1046 dump_stats_transaction(vconn, request);
1051 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1053 const char *vconn_name = argv[1];
1054 const char *port_name = argv[2];
1055 enum ofputil_protocol protocol;
1056 enum ofp_version version;
1057 struct ofpbuf *request;
1058 struct vconn *vconn;
1061 port = str_to_port_no(vconn_name, port_name);
1063 protocol = open_vconn(vconn_name, &vconn);
1064 version = ofputil_protocol_to_ofp_version(protocol);
1065 request = ofputil_encode_queue_get_config_request(version, port);
1066 dump_transaction(vconn, request);
1070 static enum ofputil_protocol
1071 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1072 enum ofputil_protocol usable_protocols)
1074 enum ofputil_protocol cur_protocol;
1078 if (!(usable_protocols & allowed_protocols)) {
1079 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1080 usable_s = ofputil_protocols_to_string(usable_protocols);
1081 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1082 "allowed flow formats (%s)", usable_s, allowed_s);
1085 /* If the initial flow format is allowed and usable, keep it. */
1086 cur_protocol = open_vconn(remote, vconnp);
1087 if (usable_protocols & allowed_protocols & cur_protocol) {
1088 return cur_protocol;
1091 /* Otherwise try each flow format in turn. */
1092 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1093 enum ofputil_protocol f = 1 << i;
1095 if (f != cur_protocol
1096 && f & usable_protocols & allowed_protocols
1097 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1102 usable_s = ofputil_protocols_to_string(usable_protocols);
1103 ovs_fatal(0, "switch does not support any of the usable flow "
1104 "formats (%s)", usable_s);
1108 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1109 size_t n_fms, enum ofputil_protocol usable_protocols)
1111 enum ofputil_protocol protocol;
1112 struct vconn *vconn;
1115 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1117 for (i = 0; i < n_fms; i++) {
1118 struct ofputil_flow_mod *fm = &fms[i];
1120 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1127 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1129 enum ofputil_protocol usable_protocols;
1130 struct ofputil_flow_mod *fms = NULL;
1134 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1137 ovs_fatal(0, "%s", error);
1139 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1144 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1146 if (argc > 2 && !strcmp(argv[2], "-")) {
1147 ofctl_flow_mod_file(argc, argv, command);
1149 struct ofputil_flow_mod fm;
1151 enum ofputil_protocol usable_protocols;
1153 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1156 ovs_fatal(0, "%s", error);
1158 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1163 ofctl_add_flow(int argc, char *argv[])
1165 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1169 ofctl_add_flows(int argc, char *argv[])
1171 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1175 ofctl_mod_flows(int argc, char *argv[])
1177 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1181 ofctl_del_flows(int argc, char *argv[])
1183 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1187 set_packet_in_format(struct vconn *vconn,
1188 enum nx_packet_in_format packet_in_format)
1190 struct ofpbuf *spif;
1192 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1194 transact_noreply(vconn, spif);
1195 VLOG_DBG("%s: using user-specified packet in format %s",
1196 vconn_get_name(vconn),
1197 ofputil_packet_in_format_to_string(packet_in_format));
1201 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1203 struct ofp_switch_config config;
1204 enum ofp_config_flags flags;
1206 fetch_switch_config(vconn, &config);
1207 flags = ntohs(config.flags);
1208 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1209 /* Set the invalid ttl config. */
1210 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1212 config.flags = htons(flags);
1213 set_switch_config(vconn, &config);
1215 /* Then retrieve the configuration to see if it really took. OpenFlow
1216 * doesn't define error reporting for bad modes, so this is all we can
1218 fetch_switch_config(vconn, &config);
1219 flags = ntohs(config.flags);
1220 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1221 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1222 "switch probably doesn't support mode)");
1229 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1230 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1231 * an error message and stores NULL in '*msgp'. */
1233 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1235 struct ofp_header *oh;
1238 msg = ofpbuf_new(strlen(hex) / 2);
1241 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1243 return "Trailing garbage in hex data";
1246 if (msg->size < sizeof(struct ofp_header)) {
1248 return "Message too short for OpenFlow";
1252 if (msg->size != ntohs(oh->length)) {
1254 return "Message size does not match length in OpenFlow header";
1262 ofctl_send(struct unixctl_conn *conn, int argc,
1263 const char *argv[], void *vconn_)
1265 struct vconn *vconn = vconn_;
1272 for (i = 1; i < argc; i++) {
1273 const char *error_msg;
1277 error_msg = openflow_from_hex(argv[i], &msg);
1279 ds_put_format(&reply, "%s\n", error_msg);
1284 fprintf(stderr, "send: ");
1285 ofp_print(stderr, msg->data, msg->size, verbosity);
1287 error = vconn_send_block(vconn, msg);
1290 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1293 ds_put_cstr(&reply, "sent\n");
1298 unixctl_command_reply(conn, ds_cstr(&reply));
1300 unixctl_command_reply_error(conn, ds_cstr(&reply));
1305 struct barrier_aux {
1306 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1307 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1311 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1312 const char *argv[] OVS_UNUSED, void *aux_)
1314 struct barrier_aux *aux = aux_;
1319 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1323 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1324 error = vconn_send_block(aux->vconn, msg);
1327 unixctl_command_reply_error(conn, ovs_strerror(error));
1334 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1335 const char *argv[], void *aux OVS_UNUSED)
1339 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1341 unixctl_command_reply_error(conn, ovs_strerror(errno));
1346 dup2(fd, STDERR_FILENO);
1348 unixctl_command_reply(conn, NULL);
1352 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1353 const char *argv[] OVS_UNUSED, void *blocked_)
1355 bool *blocked = blocked_;
1359 unixctl_command_reply(conn, NULL);
1361 unixctl_command_reply(conn, "already blocking");
1366 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1367 const char *argv[] OVS_UNUSED, void *blocked_)
1369 bool *blocked = blocked_;
1373 unixctl_command_reply(conn, NULL);
1375 unixctl_command_reply(conn, "already unblocked");
1379 /* Prints to stdout all of the messages received on 'vconn'.
1381 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1382 * received on 'vconn'. */
1384 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1386 struct barrier_aux barrier_aux = { vconn, NULL };
1387 struct unixctl_server *server;
1388 bool exiting = false;
1389 bool blocked = false;
1392 daemon_save_fd(STDERR_FILENO);
1394 error = unixctl_server_create(NULL, &server);
1396 ovs_fatal(error, "failed to create unixctl server");
1398 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1399 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1401 unixctl_command_register("ofctl/barrier", "", 0, 0,
1402 ofctl_barrier, &barrier_aux);
1403 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1404 ofctl_set_output_file, NULL);
1406 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1407 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1410 daemonize_complete();
1416 unixctl_server_run(server);
1421 retval = vconn_recv(vconn, &b);
1422 if (retval == EAGAIN) {
1425 run(retval, "vconn_recv");
1428 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1429 time_wall_msec(), true);
1434 ofptype_decode(&type, b->data);
1435 ofp_print(stderr, b->data, b->size, verbosity + 2);
1437 switch ((int) type) {
1438 case OFPTYPE_BARRIER_REPLY:
1439 if (barrier_aux.conn) {
1440 unixctl_command_reply(barrier_aux.conn, NULL);
1441 barrier_aux.conn = NULL;
1445 case OFPTYPE_ECHO_REQUEST:
1446 if (reply_to_echo_requests) {
1447 struct ofpbuf *reply;
1449 reply = make_echo_reply(b->data);
1450 retval = vconn_send_block(vconn, reply);
1452 ovs_fatal(retval, "failed to send echo reply");
1465 vconn_run_wait(vconn);
1467 vconn_recv_wait(vconn);
1469 unixctl_server_wait(server);
1473 unixctl_server_destroy(server);
1477 ofctl_monitor(int argc, char *argv[])
1479 struct vconn *vconn;
1481 enum ofputil_protocol usable_protocols;
1483 open_vconn(argv[1], &vconn);
1484 for (i = 2; i < argc; i++) {
1485 const char *arg = argv[i];
1487 if (isdigit((unsigned char) *arg)) {
1488 struct ofp_switch_config config;
1490 fetch_switch_config(vconn, &config);
1491 config.miss_send_len = htons(atoi(arg));
1492 set_switch_config(vconn, &config);
1493 } else if (!strcmp(arg, "invalid_ttl")) {
1494 monitor_set_invalid_ttl_to_controller(vconn);
1495 } else if (!strncmp(arg, "watch:", 6)) {
1496 struct ofputil_flow_monitor_request fmr;
1500 error = parse_flow_monitor_request(&fmr, arg + 6,
1503 ovs_fatal(0, "%s", error);
1506 msg = ofpbuf_new(0);
1507 ofputil_append_flow_monitor_request(&fmr, msg);
1508 dump_stats_transaction(vconn, msg);
1510 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1514 if (preferred_packet_in_format >= 0) {
1515 set_packet_in_format(vconn, preferred_packet_in_format);
1517 enum ofp_version version = vconn_get_version(vconn);
1520 case OFP10_VERSION: {
1521 struct ofpbuf *spif, *reply;
1523 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1525 run(vconn_transact_noreply(vconn, spif, &reply),
1526 "talking to %s", vconn_get_name(vconn));
1528 char *s = ofp_to_string(reply->data, reply->size, 2);
1529 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1530 " replied: %s. Falling back to the switch default.",
1531 vconn_get_name(vconn), s);
1533 ofpbuf_delete(reply);
1546 monitor_vconn(vconn, true);
1550 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1552 struct vconn *vconn;
1554 open_vconn__(argv[1], SNOOP, &vconn);
1555 monitor_vconn(vconn, false);
1559 ofctl_dump_ports(int argc, char *argv[])
1561 struct ofpbuf *request;
1562 struct vconn *vconn;
1565 open_vconn(argv[1], &vconn);
1566 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1567 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1568 dump_stats_transaction(vconn, request);
1573 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1575 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1579 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1581 struct ofpbuf *request;
1582 struct vconn *vconn;
1583 struct ofpbuf *reply;
1585 open_vconn(argv[1], &vconn);
1586 request = make_echo_request(vconn_get_version(vconn));
1587 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1588 if (reply->size != sizeof(struct ofp_header)) {
1589 ovs_fatal(0, "reply does not match request");
1591 ofpbuf_delete(reply);
1596 ofctl_packet_out(int argc, char *argv[])
1598 enum ofputil_protocol protocol;
1599 struct ofputil_packet_out po;
1600 struct ofpbuf ofpacts;
1601 struct vconn *vconn;
1604 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1606 ofpbuf_init(&ofpacts, 64);
1607 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1609 ovs_fatal(0, "%s", error);
1612 po.buffer_id = UINT32_MAX;
1613 po.in_port = str_to_port_no(argv[1], argv[2]);
1614 po.ofpacts = ofpacts.data;
1615 po.ofpacts_len = ofpacts.size;
1617 protocol = open_vconn(argv[1], &vconn);
1618 for (i = 4; i < argc; i++) {
1619 struct ofpbuf *packet, *opo;
1620 const char *error_msg;
1622 error_msg = eth_from_hex(argv[i], &packet);
1624 ovs_fatal(0, "%s", error_msg);
1627 po.packet = packet->data;
1628 po.packet_len = packet->size;
1629 opo = ofputil_encode_packet_out(&po, protocol);
1630 transact_noreply(vconn, opo);
1631 ofpbuf_delete(packet);
1634 ofpbuf_uninit(&ofpacts);
1638 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1640 struct ofp_config_flag {
1641 const char *name; /* The flag's name. */
1642 enum ofputil_port_config bit; /* Bit to turn on or off. */
1643 bool on; /* Value to set the bit to. */
1645 static const struct ofp_config_flag flags[] = {
1646 { "up", OFPUTIL_PC_PORT_DOWN, false },
1647 { "down", OFPUTIL_PC_PORT_DOWN, true },
1648 { "stp", OFPUTIL_PC_NO_STP, false },
1649 { "receive", OFPUTIL_PC_NO_RECV, false },
1650 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1651 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1652 { "forward", OFPUTIL_PC_NO_FWD, false },
1653 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1656 const struct ofp_config_flag *flag;
1657 enum ofputil_protocol protocol;
1658 struct ofputil_port_mod pm;
1659 struct ofputil_phy_port pp;
1660 struct vconn *vconn;
1661 const char *command;
1664 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1666 pm.port_no = pp.port_no;
1667 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1672 if (!strncasecmp(argv[3], "no-", 3)) {
1673 command = argv[3] + 3;
1675 } else if (!strncasecmp(argv[3], "no", 2)) {
1676 command = argv[3] + 2;
1682 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1683 if (!strcasecmp(command, flag->name)) {
1684 pm.mask = flag->bit;
1685 pm.config = flag->on ^ not ? flag->bit : 0;
1689 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1692 protocol = open_vconn(argv[1], &vconn);
1693 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1698 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1700 enum ofputil_protocol protocol, usable_protocols;
1701 struct ofputil_table_mod tm;
1702 struct vconn *vconn;
1706 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1708 ovs_fatal(0, "%s", error);
1711 protocol = open_vconn(argv[1], &vconn);
1712 if (!(protocol & usable_protocols)) {
1713 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1714 enum ofputil_protocol f = 1 << i;
1716 && f & usable_protocols
1717 && try_set_protocol(vconn, f, &protocol)) {
1724 if (!(protocol & usable_protocols)) {
1725 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1726 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1729 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1734 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1736 struct ofp_switch_config config;
1737 struct vconn *vconn;
1739 open_vconn(argv[1], &vconn);
1740 fetch_switch_config(vconn, &config);
1741 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1746 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1748 struct ofp_switch_config config;
1749 enum ofp_config_flags mode;
1750 struct vconn *vconn;
1753 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1754 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1757 open_vconn(argv[1], &vconn);
1758 fetch_switch_config(vconn, &config);
1759 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1760 if (flags != config.flags) {
1761 /* Set the configuration. */
1762 config.flags = flags;
1763 set_switch_config(vconn, &config);
1765 /* Then retrieve the configuration to see if it really took. OpenFlow
1766 * doesn't define error reporting for bad modes, so this is all we can
1768 fetch_switch_config(vconn, &config);
1769 if (flags != config.flags) {
1770 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1771 "switch probably doesn't support mode \"%s\")",
1772 argv[1], ofputil_frag_handling_to_string(mode));
1779 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1781 const char *filename = argv[1];
1785 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1787 ovs_fatal(errno, "%s: open", filename);
1790 ofpbuf_init(&b, 65536);
1792 struct ofp_header *oh;
1793 size_t length, tail_len;
1798 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1799 n = fread(oh, 1, sizeof *oh, file);
1802 } else if (n < sizeof *oh) {
1803 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1806 length = ntohs(oh->length);
1807 if (length < sizeof *oh) {
1808 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1812 tail_len = length - sizeof *oh;
1813 tail = ofpbuf_put_uninit(&b, tail_len);
1814 n = fread(tail, 1, tail_len, file);
1816 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1819 ofp_print(stdout, b.data, b.size, verbosity + 2);
1823 if (file != stdin) {
1829 ofctl_ping(int argc, char *argv[])
1831 size_t max_payload = 65535 - sizeof(struct ofp_header);
1832 unsigned int payload;
1833 struct vconn *vconn;
1836 payload = argc > 2 ? atoi(argv[2]) : 64;
1837 if (payload > max_payload) {
1838 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1841 open_vconn(argv[1], &vconn);
1842 for (i = 0; i < 10; i++) {
1843 struct timeval start, end;
1844 struct ofpbuf *request, *reply;
1845 const struct ofp_header *rpy_hdr;
1848 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1849 vconn_get_version(vconn), payload);
1850 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1852 xgettimeofday(&start);
1853 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1854 xgettimeofday(&end);
1856 rpy_hdr = reply->data;
1857 if (ofptype_pull(&type, reply)
1858 || type != OFPTYPE_ECHO_REPLY
1859 || reply->size != payload
1860 || memcmp(request->l3, reply->l3, payload)) {
1861 printf("Reply does not match request. Request:\n");
1862 ofp_print(stdout, request, request->size, verbosity + 2);
1864 ofp_print(stdout, reply, reply->size, verbosity + 2);
1866 printf("%"PRIuSIZE" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1867 reply->size, argv[1], ntohl(rpy_hdr->xid),
1868 (1000*(double)(end.tv_sec - start.tv_sec))
1869 + (.001*(end.tv_usec - start.tv_usec)));
1870 ofpbuf_delete(request);
1871 ofpbuf_delete(reply);
1877 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1879 size_t max_payload = 65535 - sizeof(struct ofp_header);
1880 struct timeval start, end;
1881 unsigned int payload_size, message_size;
1882 struct vconn *vconn;
1887 payload_size = atoi(argv[2]);
1888 if (payload_size > max_payload) {
1889 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1891 message_size = sizeof(struct ofp_header) + payload_size;
1893 count = atoi(argv[3]);
1895 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1896 count, message_size, count * message_size);
1898 open_vconn(argv[1], &vconn);
1899 xgettimeofday(&start);
1900 for (i = 0; i < count; i++) {
1901 struct ofpbuf *request, *reply;
1903 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1904 vconn_get_version(vconn), payload_size);
1905 ofpbuf_put_zeros(request, payload_size);
1906 run(vconn_transact(vconn, request, &reply), "transact");
1907 ofpbuf_delete(reply);
1909 xgettimeofday(&end);
1912 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1913 + (.001*(end.tv_usec - start.tv_usec)));
1914 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1915 duration, count / (duration / 1000.0),
1916 count * message_size / (duration / 1000.0));
1920 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
1923 struct ofputil_group_mod *gm;
1924 struct ofpbuf *request;
1926 struct vconn *vconn;
1929 open_vconn(remote, &vconn);
1931 for (i = 0; i < n_gms; i++) {
1933 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
1935 transact_noreply(vconn, request);
1945 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1947 struct ofputil_group_mod *gms = NULL;
1948 enum ofputil_protocol usable_protocols;
1952 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
1955 ovs_fatal(0, "%s", error);
1957 ofctl_group_mod__(argv[1], gms, n_gms);
1962 ofctl_group_mod(int argc, char *argv[], uint16_t command)
1964 if (argc > 2 && !strcmp(argv[2], "-")) {
1965 ofctl_group_mod_file(argc, argv, command);
1967 enum ofputil_protocol usable_protocols;
1968 struct ofputil_group_mod gm;
1971 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
1974 ovs_fatal(0, "%s", error);
1976 ofctl_group_mod__(argv[1], &gm, 1);
1981 ofctl_add_group(int argc, char *argv[])
1983 ofctl_group_mod(argc, argv, OFPGC11_ADD);
1987 ofctl_add_groups(int argc, char *argv[])
1989 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
1993 ofctl_mod_group(int argc, char *argv[])
1995 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
1999 ofctl_del_groups(int argc, char *argv[])
2001 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2005 ofctl_dump_group_stats(int argc, char *argv[])
2007 enum ofputil_protocol usable_protocols;
2008 struct ofputil_group_mod gm;
2009 struct ofpbuf *request;
2010 struct vconn *vconn;
2014 memset(&gm, 0, sizeof gm);
2016 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2017 argc > 2 ? argv[2] : "",
2020 ovs_fatal(0, "%s", error);
2023 group_id = gm.group_id;
2025 open_vconn(argv[1], &vconn);
2026 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2029 dump_stats_transaction(vconn, request);
2036 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2038 struct ofpbuf *request;
2039 struct vconn *vconn;
2041 open_vconn(argv[1], &vconn);
2043 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2045 dump_stats_transaction(vconn, request);
2052 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2054 struct ofpbuf *request;
2055 struct vconn *vconn;
2057 open_vconn(argv[1], &vconn);
2058 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2060 dump_stats_transaction(vconn, request);
2067 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2072 /* replace-flows and diff-flows commands. */
2074 /* A flow table entry, possibly with two different versions. */
2076 struct cls_rule rule; /* Within a "struct classifier". */
2077 struct fte_version *versions[2];
2080 /* One version of a Flow Table Entry. */
2081 struct fte_version {
2083 uint16_t idle_timeout;
2084 uint16_t hard_timeout;
2086 struct ofpact *ofpacts;
2090 /* Frees 'version' and the data that it owns. */
2092 fte_version_free(struct fte_version *version)
2095 free(version->ofpacts);
2100 /* Returns true if 'a' and 'b' are the same, false if they differ.
2102 * Ignores differences in 'flags' because there's no way to retrieve flags from
2103 * an OpenFlow switch. We have to assume that they are the same. */
2105 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2107 return (a->cookie == b->cookie
2108 && a->idle_timeout == b->idle_timeout
2109 && a->hard_timeout == b->hard_timeout
2110 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2111 b->ofpacts, b->ofpacts_len));
2114 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2115 * 'index' into 's', followed by a new-line. */
2117 fte_version_format(const struct fte *fte, int index, struct ds *s)
2119 const struct fte_version *version = fte->versions[index];
2126 cls_rule_format(&fte->rule, s);
2127 if (version->cookie != htonll(0)) {
2128 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2130 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2131 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2133 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2134 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2137 ds_put_cstr(s, " actions=");
2138 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2140 ds_put_char(s, '\n');
2144 fte_from_cls_rule(const struct cls_rule *cls_rule)
2146 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2149 /* Frees 'fte' and its versions. */
2151 fte_free(struct fte *fte)
2154 fte_version_free(fte->versions[0]);
2155 fte_version_free(fte->versions[1]);
2156 cls_rule_destroy(&fte->rule);
2161 /* Frees all of the FTEs within 'cls'. */
2163 fte_free_all(struct classifier *cls)
2165 struct cls_cursor cursor;
2166 struct fte *fte, *next;
2168 ovs_rwlock_wrlock(&cls->rwlock);
2169 cls_cursor_init(&cursor, cls, NULL);
2170 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2171 classifier_remove(cls, &fte->rule);
2174 ovs_rwlock_unlock(&cls->rwlock);
2175 classifier_destroy(cls);
2178 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2179 * necessary. Sets 'version' as the version of that rule with the given
2180 * 'index', replacing any existing version, if any.
2182 * Takes ownership of 'version'. */
2184 fte_insert(struct classifier *cls, const struct match *match,
2185 unsigned int priority, struct fte_version *version, int index)
2187 struct fte *old, *fte;
2189 fte = xzalloc(sizeof *fte);
2190 cls_rule_init(&fte->rule, match, priority);
2191 fte->versions[index] = version;
2193 ovs_rwlock_wrlock(&cls->rwlock);
2194 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2195 ovs_rwlock_unlock(&cls->rwlock);
2197 fte_version_free(old->versions[index]);
2198 fte->versions[!index] = old->versions[!index];
2199 cls_rule_destroy(&old->rule);
2204 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2205 * with the specified 'index'. Returns the flow formats able to represent the
2206 * flows that were read. */
2207 static enum ofputil_protocol
2208 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2210 enum ofputil_protocol usable_protocols;
2215 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2217 ovs_fatal(errno, "%s: open", filename);
2221 usable_protocols = OFPUTIL_P_ANY;
2223 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2224 struct fte_version *version;
2225 struct ofputil_flow_mod fm;
2227 enum ofputil_protocol usable;
2229 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2231 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2233 usable_protocols &= usable;
2235 version = xmalloc(sizeof *version);
2236 version->cookie = fm.new_cookie;
2237 version->idle_timeout = fm.idle_timeout;
2238 version->hard_timeout = fm.hard_timeout;
2239 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2240 | OFPUTIL_FF_EMERG);
2241 version->ofpacts = fm.ofpacts;
2242 version->ofpacts_len = fm.ofpacts_len;
2244 fte_insert(cls, &fm.match, fm.priority, version, index);
2248 if (file != stdin) {
2252 return usable_protocols;
2256 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2257 struct ofpbuf **replyp,
2258 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2260 struct ofpbuf *reply = *replyp;
2266 /* Get a flow stats reply message, if we don't already have one. */
2272 run(vconn_recv_block(vconn, &reply),
2273 "OpenFlow packet receive failed");
2274 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2276 error = ofptype_decode(&type, reply->data);
2277 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2278 ovs_fatal(0, "received bad reply: %s",
2279 ofp_to_string(reply->data, reply->size,
2284 /* Pull an individual flow stats reply out of the message. */
2285 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2292 more = ofpmp_more(reply->l2);
2293 ofpbuf_delete(reply);
2302 ovs_fatal(0, "parse error in reply (%s)",
2303 ofperr_to_string(retval));
2308 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2309 * format 'protocol', and adds them as flow table entries in 'cls' for the
2310 * version with the specified 'index'. */
2312 read_flows_from_switch(struct vconn *vconn,
2313 enum ofputil_protocol protocol,
2314 struct classifier *cls, int index)
2316 struct ofputil_flow_stats_request fsr;
2317 struct ofputil_flow_stats fs;
2318 struct ofpbuf *request;
2319 struct ofpbuf ofpacts;
2320 struct ofpbuf *reply;
2323 fsr.aggregate = false;
2324 match_init_catchall(&fsr.match);
2325 fsr.out_port = OFPP_ANY;
2326 fsr.table_id = 0xff;
2327 fsr.cookie = fsr.cookie_mask = htonll(0);
2328 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2329 send_xid = ((struct ofp_header *) request->data)->xid;
2330 send_openflow_buffer(vconn, request);
2333 ofpbuf_init(&ofpacts, 0);
2334 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2335 struct fte_version *version;
2337 version = xmalloc(sizeof *version);
2338 version->cookie = fs.cookie;
2339 version->idle_timeout = fs.idle_timeout;
2340 version->hard_timeout = fs.hard_timeout;
2342 version->ofpacts_len = fs.ofpacts_len;
2343 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2345 fte_insert(cls, &fs.match, fs.priority, version, index);
2347 ofpbuf_uninit(&ofpacts);
2351 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2352 enum ofputil_protocol protocol, struct list *packets)
2354 const struct fte_version *version = fte->versions[index];
2355 struct ofputil_flow_mod fm;
2358 minimatch_expand(&fte->rule.match, &fm.match);
2359 fm.priority = fte->rule.priority;
2360 fm.cookie = htonll(0);
2361 fm.cookie_mask = htonll(0);
2362 fm.new_cookie = version->cookie;
2363 fm.modify_cookie = true;
2365 fm.command = command;
2366 fm.idle_timeout = version->idle_timeout;
2367 fm.hard_timeout = version->hard_timeout;
2368 fm.buffer_id = UINT32_MAX;
2369 fm.out_port = OFPP_ANY;
2370 fm.flags = version->flags;
2371 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2372 command == OFPFC_MODIFY_STRICT) {
2373 fm.ofpacts = version->ofpacts;
2374 fm.ofpacts_len = version->ofpacts_len;
2380 ofm = ofputil_encode_flow_mod(&fm, protocol);
2381 list_push_back(packets, &ofm->list_node);
2385 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2387 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2388 enum ofputil_protocol usable_protocols, protocol;
2389 struct cls_cursor cursor;
2390 struct classifier cls;
2391 struct list requests;
2392 struct vconn *vconn;
2395 classifier_init(&cls, NULL);
2396 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2398 protocol = open_vconn(argv[1], &vconn);
2399 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2401 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2403 list_init(&requests);
2405 /* Delete flows that exist on the switch but not in the file. */
2406 ovs_rwlock_rdlock(&cls.rwlock);
2407 cls_cursor_init(&cursor, &cls, NULL);
2408 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2409 struct fte_version *file_ver = fte->versions[FILE_IDX];
2410 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2412 if (sw_ver && !file_ver) {
2413 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2414 protocol, &requests);
2418 /* Add flows that exist in the file but not on the switch.
2419 * Update flows that exist in both places but differ. */
2420 cls_cursor_init(&cursor, &cls, NULL);
2421 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2422 struct fte_version *file_ver = fte->versions[FILE_IDX];
2423 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2426 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2427 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2430 ovs_rwlock_unlock(&cls.rwlock);
2431 transact_multiple_noreply(vconn, &requests);
2438 read_flows_from_source(const char *source, struct classifier *cls, int index)
2442 if (source[0] == '/' || source[0] == '.'
2443 || (!strchr(source, ':') && !stat(source, &s))) {
2444 read_flows_from_file(source, cls, index);
2446 enum ofputil_protocol protocol;
2447 struct vconn *vconn;
2449 protocol = open_vconn(source, &vconn);
2450 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2451 read_flows_from_switch(vconn, protocol, cls, index);
2457 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2459 bool differences = false;
2460 struct cls_cursor cursor;
2461 struct classifier cls;
2465 classifier_init(&cls, NULL);
2466 read_flows_from_source(argv[1], &cls, 0);
2467 read_flows_from_source(argv[2], &cls, 1);
2472 ovs_rwlock_rdlock(&cls.rwlock);
2473 cls_cursor_init(&cursor, &cls, NULL);
2474 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2475 struct fte_version *a = fte->versions[0];
2476 struct fte_version *b = fte->versions[1];
2478 if (!a || !b || !fte_version_equals(a, b)) {
2479 fte_version_format(fte, 0, &a_s);
2480 fte_version_format(fte, 1, &b_s);
2481 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2483 printf("-%s", ds_cstr(&a_s));
2486 printf("+%s", ds_cstr(&b_s));
2492 ovs_rwlock_unlock(&cls.rwlock);
2505 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2507 struct ofputil_meter_mod mm;
2508 struct vconn *vconn;
2509 enum ofputil_protocol protocol;
2510 enum ofputil_protocol usable_protocols;
2511 enum ofp_version version;
2515 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2517 ovs_fatal(0, "%s", error);
2520 usable_protocols = OFPUTIL_P_OF13_UP;
2521 mm.command = command;
2522 mm.meter.meter_id = OFPM13_ALL;
2525 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2526 version = ofputil_protocol_to_ofp_version(protocol);
2527 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2532 ofctl_meter_request__(const char *bridge, const char *str,
2533 enum ofputil_meter_request_type type)
2535 struct ofputil_meter_mod mm;
2536 struct vconn *vconn;
2537 enum ofputil_protocol usable_protocols;
2538 enum ofputil_protocol protocol;
2539 enum ofp_version version;
2543 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2545 ovs_fatal(0, "%s", error);
2548 usable_protocols = OFPUTIL_P_OF13_UP;
2549 mm.meter.meter_id = OFPM13_ALL;
2552 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2553 version = ofputil_protocol_to_ofp_version(protocol);
2554 transact_noreply(vconn, ofputil_encode_meter_request(version,
2556 mm.meter.meter_id));
2562 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2564 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2568 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2570 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2574 ofctl_del_meters(int argc, char *argv[])
2576 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2580 ofctl_dump_meters(int argc, char *argv[])
2582 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2583 OFPUTIL_METER_CONFIG);
2587 ofctl_meter_stats(int argc, char *argv[])
2589 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2590 OFPUTIL_METER_STATS);
2594 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2596 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2600 /* Undocumented commands for unit testing. */
2603 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2604 enum ofputil_protocol usable_protocols)
2606 enum ofputil_protocol protocol = 0;
2610 usable_s = ofputil_protocols_to_string(usable_protocols);
2611 printf("usable protocols: %s\n", usable_s);
2614 if (!(usable_protocols & allowed_protocols)) {
2615 ovs_fatal(0, "no usable protocol");
2617 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2619 if (protocol & usable_protocols & allowed_protocols) {
2623 ovs_assert(is_pow2(protocol));
2625 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2627 for (i = 0; i < n_fms; i++) {
2628 struct ofputil_flow_mod *fm = &fms[i];
2631 msg = ofputil_encode_flow_mod(fm, protocol);
2632 ofp_print(stdout, msg->data, msg->size, verbosity);
2639 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2640 * it back to stdout. */
2642 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2644 enum ofputil_protocol usable_protocols;
2645 struct ofputil_flow_mod fm;
2648 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2650 ovs_fatal(0, "%s", error);
2652 ofctl_parse_flows__(&fm, 1, usable_protocols);
2655 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2656 * add-flows) and prints each of the flows back to stdout. */
2658 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2660 enum ofputil_protocol usable_protocols;
2661 struct ofputil_flow_mod *fms = NULL;
2665 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2668 ovs_fatal(0, "%s", error);
2670 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2675 ofctl_parse_nxm__(bool oxm)
2680 while (!ds_get_test_line(&in, stdin)) {
2681 struct ofpbuf nx_match;
2683 ovs_be64 cookie, cookie_mask;
2687 /* Convert string to nx_match. */
2688 ofpbuf_init(&nx_match, 0);
2690 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2692 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2695 /* Convert nx_match to match. */
2698 error = oxm_pull_match(&nx_match, &match);
2700 error = nx_pull_match(&nx_match, match_len, &match,
2701 &cookie, &cookie_mask);
2705 error = oxm_pull_match_loose(&nx_match, &match);
2707 error = nx_pull_match_loose(&nx_match, match_len, &match,
2708 &cookie, &cookie_mask);
2716 /* Convert match back to nx_match. */
2717 ofpbuf_uninit(&nx_match);
2718 ofpbuf_init(&nx_match, 0);
2720 match_len = oxm_put_match(&nx_match, &match);
2721 out = oxm_match_to_string(&nx_match, match_len);
2723 match_len = nx_put_match(&nx_match, &match,
2724 cookie, cookie_mask);
2725 out = nx_match_to_string(nx_match.data, match_len);
2731 printf("nx_pull_match() returned error %s\n",
2732 ofperr_get_name(error));
2735 ofpbuf_uninit(&nx_match);
2740 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2741 * stdin, does some internal fussing with them, and then prints them back as
2742 * strings on stdout. */
2744 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2746 return ofctl_parse_nxm__(false);
2749 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2750 * stdin, does some internal fussing with them, and then prints them back as
2751 * strings on stdout. */
2753 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2755 return ofctl_parse_nxm__(true);
2759 print_differences(const char *prefix,
2760 const void *a_, size_t a_len,
2761 const void *b_, size_t b_len)
2763 const uint8_t *a = a_;
2764 const uint8_t *b = b_;
2767 for (i = 0; i < MIN(a_len, b_len); i++) {
2769 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2770 prefix, i, a[i], b[i]);
2773 for (i = a_len; i < b_len; i++) {
2774 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2776 for (i = b_len; i < a_len; i++) {
2777 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2781 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2782 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2783 * on stdout, and then converts them back to hex bytes and prints any
2784 * differences from the input. */
2786 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2791 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2792 struct ofpbuf of10_out;
2793 struct ofpbuf of10_in;
2794 struct ofpbuf ofpacts;
2799 /* Parse hex bytes. */
2800 ofpbuf_init(&of10_in, 0);
2801 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2802 ovs_fatal(0, "Trailing garbage in hex data");
2805 /* Convert to ofpacts. */
2806 ofpbuf_init(&ofpacts, 0);
2807 size = of10_in.size;
2808 error = ofpacts_pull_openflow_actions(&of10_in, of10_in.size,
2809 OFP10_VERSION, &ofpacts);
2811 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2812 ofpbuf_uninit(&ofpacts);
2813 ofpbuf_uninit(&of10_in);
2816 ofpbuf_push_uninit(&of10_in, size);
2818 /* Print cls_rule. */
2820 ds_put_cstr(&s, "actions=");
2821 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2825 /* Convert back to ofp10 actions and print differences from input. */
2826 ofpbuf_init(&of10_out, 0);
2827 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of10_out,
2830 print_differences("", of10_in.data, of10_in.size,
2831 of10_out.data, of10_out.size);
2834 ofpbuf_uninit(&ofpacts);
2835 ofpbuf_uninit(&of10_in);
2836 ofpbuf_uninit(&of10_out);
2841 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2842 * bytes from stdin, converts them to cls_rules, prints them as strings on
2843 * stdout, and then converts them back to hex bytes and prints any differences
2846 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2847 * values are ignored in the input and will be set to zero when OVS converts
2848 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2849 * it does the conversion to hex, to ensure that in fact they are ignored. */
2851 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2858 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2859 struct ofpbuf match_in, match_expout;
2860 struct ofp10_match match_out;
2861 struct ofp10_match match_normal;
2865 /* Parse hex bytes to use for expected output. */
2867 ds_put_cstr(&expout, ds_cstr(&in));
2868 for (p = ds_cstr(&expout); *p; p++) {
2873 ofpbuf_init(&match_expout, 0);
2874 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2875 ovs_fatal(0, "Trailing garbage in hex data");
2877 if (match_expout.size != sizeof(struct ofp10_match)) {
2878 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2879 match_expout.size, sizeof(struct ofp10_match));
2882 /* Parse hex bytes for input. */
2883 for (p = ds_cstr(&in); *p; p++) {
2885 *p = "0123456789abcdef"[random_uint32() & 0xf];
2888 ofpbuf_init(&match_in, 0);
2889 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2890 ovs_fatal(0, "Trailing garbage in hex data");
2892 if (match_in.size != sizeof(struct ofp10_match)) {
2893 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2894 match_in.size, sizeof(struct ofp10_match));
2897 /* Convert to cls_rule and print. */
2898 ofputil_match_from_ofp10_match(match_in.data, &match);
2899 match_print(&match);
2901 /* Convert back to ofp10_match and print differences from input. */
2902 ofputil_match_to_ofp10_match(&match, &match_out);
2903 print_differences("", match_expout.data, match_expout.size,
2904 &match_out, sizeof match_out);
2906 /* Normalize, then convert and compare again. */
2907 ofputil_normalize_match(&match);
2908 ofputil_match_to_ofp10_match(&match, &match_normal);
2909 print_differences("normal: ", &match_out, sizeof match_out,
2910 &match_normal, sizeof match_normal);
2913 ofpbuf_uninit(&match_in);
2914 ofpbuf_uninit(&match_expout);
2917 ds_destroy(&expout);
2920 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2921 * bytes from stdin, converts them to "struct match"es, prints them as strings
2922 * on stdout, and then converts them back to hex bytes and prints any
2923 * differences from the input. */
2925 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2930 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2931 struct ofpbuf match_in;
2932 struct ofp11_match match_out;
2936 /* Parse hex bytes. */
2937 ofpbuf_init(&match_in, 0);
2938 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2939 ovs_fatal(0, "Trailing garbage in hex data");
2941 if (match_in.size != sizeof(struct ofp11_match)) {
2942 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2943 match_in.size, sizeof(struct ofp11_match));
2946 /* Convert to match. */
2947 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2949 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2950 ofpbuf_uninit(&match_in);
2955 match_print(&match);
2957 /* Convert back to ofp11_match and print differences from input. */
2958 ofputil_match_to_ofp11_match(&match, &match_out);
2960 print_differences("", match_in.data, match_in.size,
2961 &match_out, sizeof match_out);
2964 ofpbuf_uninit(&match_in);
2969 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2970 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2971 * on stdout, and then converts them back to hex bytes and prints any
2972 * differences from the input. */
2974 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2979 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2980 struct ofpbuf of11_out;
2981 struct ofpbuf of11_in;
2982 struct ofpbuf ofpacts;
2987 /* Parse hex bytes. */
2988 ofpbuf_init(&of11_in, 0);
2989 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2990 ovs_fatal(0, "Trailing garbage in hex data");
2993 /* Convert to ofpacts. */
2994 ofpbuf_init(&ofpacts, 0);
2995 size = of11_in.size;
2996 error = ofpacts_pull_openflow_actions(&of11_in, of11_in.size,
2997 OFP11_VERSION, &ofpacts);
2999 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3000 ofpbuf_uninit(&ofpacts);
3001 ofpbuf_uninit(&of11_in);
3004 ofpbuf_push_uninit(&of11_in, size);
3006 /* Print cls_rule. */
3008 ds_put_cstr(&s, "actions=");
3009 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3013 /* Convert back to ofp11 actions and print differences from input. */
3014 ofpbuf_init(&of11_out, 0);
3015 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of11_out,
3018 print_differences("", of11_in.data, of11_in.size,
3019 of11_out.data, of11_out.size);
3022 ofpbuf_uninit(&ofpacts);
3023 ofpbuf_uninit(&of11_in);
3024 ofpbuf_uninit(&of11_out);
3029 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3030 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3031 * them as strings on stdout, and then converts them back to hex bytes and
3032 * prints any differences from the input. */
3034 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3039 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3040 struct ofpbuf of11_out;
3041 struct ofpbuf of11_in;
3042 struct ofpbuf ofpacts;
3046 const char *table_id;
3049 /* Parse table_id separated with the follow-up instructions by ",", if
3051 instructions = ds_cstr(&in);
3053 if (strstr(instructions, ",")) {
3054 table_id = strsep(&instructions, ",");
3057 /* Parse hex bytes. */
3058 ofpbuf_init(&of11_in, 0);
3059 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3060 ovs_fatal(0, "Trailing garbage in hex data");
3063 /* Convert to ofpacts. */
3064 ofpbuf_init(&ofpacts, 0);
3065 size = of11_in.size;
3066 error = ofpacts_pull_openflow_instructions(&of11_in, of11_in.size,
3067 OFP11_VERSION, &ofpacts);
3069 /* Verify actions, enforce consistency. */
3071 memset(&flow, 0, sizeof flow);
3072 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3074 table_id ? atoi(table_id) : 0,
3075 255, OFPUTIL_P_OF11_STD);
3078 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3079 ofpbuf_uninit(&ofpacts);
3080 ofpbuf_uninit(&of11_in);
3083 ofpbuf_push_uninit(&of11_in, size);
3085 /* Print cls_rule. */
3087 ds_put_cstr(&s, "actions=");
3088 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3092 /* Convert back to ofp11 instructions and print differences from
3094 ofpbuf_init(&of11_out, 0);
3095 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3096 &of11_out, OFP13_VERSION);
3098 print_differences("", of11_in.data, of11_in.size,
3099 of11_out.data, of11_out.size);
3102 ofpbuf_uninit(&ofpacts);
3103 ofpbuf_uninit(&of11_in);
3104 ofpbuf_uninit(&of11_out);
3109 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3111 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3115 pcap = pcap_open(argv[1], "rb");
3117 ovs_fatal(errno, "%s: open failed", argv[1]);
3121 struct ofpbuf *packet;
3125 error = pcap_read(pcap, &packet);
3129 ovs_fatal(error, "%s: read failed", argv[1]);
3132 flow_extract(packet, 0, 0, NULL, NULL, &flow);
3133 flow_print(stdout, &flow);
3135 ofpbuf_delete(packet);
3139 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3140 * mask values to and from various formats and prints the results. */
3142 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3147 struct ofputil_flow_mod fm;
3150 struct match nxm_match;
3154 struct ofp10_match of10_raw;
3155 struct match of10_match;
3157 struct ofp11_match of11_raw;
3158 struct match of11_match;
3163 enum ofputil_protocol usable_protocols; /* Unused for now. */
3165 match_init_catchall(&match);
3166 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3167 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3169 /* Convert to and from string. */
3170 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3171 printf("%s -> ", string_s);
3173 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3175 ovs_fatal(0, "%s", error_s);
3177 printf("%04"PRIx16"/%04"PRIx16"\n",
3178 ntohs(fm.match.flow.vlan_tci),
3179 ntohs(fm.match.wc.masks.vlan_tci));
3182 /* Convert to and from NXM. */
3183 ofpbuf_init(&nxm, 0);
3184 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3185 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3186 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3187 printf("NXM: %s -> ", nxm_s);
3189 printf("%s\n", ofperr_to_string(error));
3191 printf("%04"PRIx16"/%04"PRIx16"\n",
3192 ntohs(nxm_match.flow.vlan_tci),
3193 ntohs(nxm_match.wc.masks.vlan_tci));
3196 ofpbuf_uninit(&nxm);
3198 /* Convert to and from OXM. */
3199 ofpbuf_init(&nxm, 0);
3200 nxm_match_len = oxm_put_match(&nxm, &match);
3201 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3202 error = oxm_pull_match(&nxm, &nxm_match);
3203 printf("OXM: %s -> ", nxm_s);
3205 printf("%s\n", ofperr_to_string(error));
3207 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3208 (VLAN_VID_MASK | VLAN_CFI);
3209 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3210 (VLAN_VID_MASK | VLAN_CFI);
3212 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3213 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3214 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3220 ofpbuf_uninit(&nxm);
3222 /* Convert to and from OpenFlow 1.0. */
3223 ofputil_match_to_ofp10_match(&match, &of10_raw);
3224 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3225 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3226 ntohs(of10_raw.dl_vlan),
3227 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3228 of10_raw.dl_vlan_pcp,
3229 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3230 ntohs(of10_match.flow.vlan_tci),
3231 ntohs(of10_match.wc.masks.vlan_tci));
3233 /* Convert to and from OpenFlow 1.1. */
3234 ofputil_match_to_ofp11_match(&match, &of11_raw);
3235 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3236 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3237 ntohs(of11_raw.dl_vlan),
3238 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3239 of11_raw.dl_vlan_pcp,
3240 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3241 ntohs(of11_match.flow.vlan_tci),
3242 ntohs(of11_match.wc.masks.vlan_tci));
3245 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3248 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3253 error = ofperr_from_name(argv[1]);
3255 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3258 for (version = 0; version <= UINT8_MAX; version++) {
3259 const char *name = ofperr_domain_get_name(version);
3261 int vendor = ofperr_get_vendor(error, version);
3262 int type = ofperr_get_type(error, version);
3263 int code = ofperr_get_code(error, version);
3265 if (vendor != -1 || type != -1 || code != -1) {
3266 printf("%s: vendor %#x, type %d, code %d\n",
3267 name, vendor, type, code);
3273 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3274 * error named ENUM and prints the error reply in hex. */
3276 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3278 const struct ofp_header *oh;
3279 struct ofpbuf request, *reply;
3282 error = ofperr_from_name(argv[1]);
3284 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3287 ofpbuf_init(&request, 0);
3288 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3289 ovs_fatal(0, "Trailing garbage in hex data");
3291 if (request.size < sizeof(struct ofp_header)) {
3292 ovs_fatal(0, "Request too short");
3296 if (request.size != ntohs(oh->length)) {
3297 ovs_fatal(0, "Request size inconsistent");
3300 reply = ofperr_encode_reply(error, request.data);
3301 ofpbuf_uninit(&request);
3303 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3304 ofpbuf_delete(reply);
3307 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3308 * binary data, interpreting them as an OpenFlow message, and prints the
3309 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3311 ofctl_ofp_print(int argc, char *argv[])
3313 struct ofpbuf packet;
3315 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3316 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3317 ovs_fatal(0, "trailing garbage following hex bytes");
3319 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
3320 ofpbuf_uninit(&packet);
3323 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3324 * and dumps each message in hex. */
3326 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3328 uint32_t bitmap = strtol(argv[1], NULL, 0);
3329 struct ofpbuf *hello;
3331 hello = ofputil_encode_hello(bitmap);
3332 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3333 ofp_print(stdout, hello->data, hello->size, verbosity);
3334 ofpbuf_delete(hello);
3337 static const struct command all_commands[] = {
3338 { "show", 1, 1, ofctl_show },
3339 { "monitor", 1, 3, ofctl_monitor },
3340 { "snoop", 1, 1, ofctl_snoop },
3341 { "dump-desc", 1, 1, ofctl_dump_desc },
3342 { "dump-tables", 1, 1, ofctl_dump_tables },
3343 { "dump-flows", 1, 2, ofctl_dump_flows },
3344 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3345 { "queue-stats", 1, 3, ofctl_queue_stats },
3346 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3347 { "add-flow", 2, 2, ofctl_add_flow },
3348 { "add-flows", 2, 2, ofctl_add_flows },
3349 { "mod-flows", 2, 2, ofctl_mod_flows },
3350 { "del-flows", 1, 2, ofctl_del_flows },
3351 { "replace-flows", 2, 2, ofctl_replace_flows },
3352 { "diff-flows", 2, 2, ofctl_diff_flows },
3353 { "add-meter", 2, 2, ofctl_add_meter },
3354 { "mod-meter", 2, 2, ofctl_mod_meter },
3355 { "del-meter", 2, 2, ofctl_del_meters },
3356 { "del-meters", 1, 1, ofctl_del_meters },
3357 { "dump-meter", 2, 2, ofctl_dump_meters },
3358 { "dump-meters", 1, 1, ofctl_dump_meters },
3359 { "meter-stats", 1, 2, ofctl_meter_stats },
3360 { "meter-features", 1, 1, ofctl_meter_features },
3361 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3362 { "dump-ports", 1, 2, ofctl_dump_ports },
3363 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3364 { "mod-port", 3, 3, ofctl_mod_port },
3365 { "mod-table", 3, 3, ofctl_mod_table },
3366 { "get-frags", 1, 1, ofctl_get_frags },
3367 { "set-frags", 2, 2, ofctl_set_frags },
3368 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3369 { "probe", 1, 1, ofctl_probe },
3370 { "ping", 1, 2, ofctl_ping },
3371 { "benchmark", 3, 3, ofctl_benchmark },
3373 { "add-group", 1, 2, ofctl_add_group },
3374 { "add-groups", 1, 2, ofctl_add_groups },
3375 { "mod-group", 1, 2, ofctl_mod_group },
3376 { "del-groups", 1, 2, ofctl_del_groups },
3377 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3378 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3379 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3380 { "help", 0, INT_MAX, ofctl_help },
3382 /* Undocumented commands for testing. */
3383 { "parse-flow", 1, 1, ofctl_parse_flow },
3384 { "parse-flows", 1, 1, ofctl_parse_flows },
3385 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3386 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3387 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3388 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3389 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3390 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3391 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3392 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3393 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3394 { "check-vlan", 2, 2, ofctl_check_vlan },
3395 { "print-error", 1, 1, ofctl_print_error },
3396 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3397 { "ofp-print", 1, 2, ofctl_ofp_print },
3398 { "encode-hello", 1, 1, ofctl_encode_hello },
3400 { NULL, 0, 0, NULL },
3403 static const struct command *get_all_commands(void)
3405 return all_commands;