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 " add-meter SWITCH METER add meter described by METER\n"
314 " mod-meter SWITCH METER modify specific METER\n"
315 " del-meter SWITCH METER delete METER\n"
316 " del-meters SWITCH delete all meters\n"
317 " dump-meter SWITCH METER print METER configuration\n"
318 " dump-meters SWITCH print all meter configuration\n"
319 " meter-stats SWITCH [METER] print meter statistics\n"
320 " meter-features SWITCH print meter features\n"
321 "\nFor OpenFlow switches and controllers:\n"
322 " probe TARGET probe whether TARGET is up\n"
323 " ping TARGET [N] latency of N-byte echos\n"
324 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
325 "SWITCH or TARGET is an active OpenFlow connection method.\n"
326 "\nOther commands:\n"
327 " ofp-parse FILE print messages read from FILE\n",
328 program_name, program_name);
329 vconn_usage(true, false, false);
333 printf("\nOther options:\n"
334 " --strict use strict match for flow commands\n"
335 " --readd replace flows that haven't changed\n"
336 " -F, --flow-format=FORMAT force particular flow format\n"
337 " -P, --packet-in-format=FRMT force particular packet in format\n"
338 " -m, --more be more verbose printing OpenFlow\n"
339 " --timestamp (monitor, snoop) print timestamps\n"
340 " -t, --timeout=SECS give up after SECS seconds\n"
341 " --sort[=field] sort in ascending order\n"
342 " --rsort[=field] sort in descending order\n"
343 " -h, --help display this help message\n"
344 " -V, --version display version information\n");
349 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
350 const char *argv[] OVS_UNUSED, void *exiting_)
352 bool *exiting = exiting_;
354 unixctl_command_reply(conn, NULL);
357 static void run(int retval, const char *message, ...)
361 run(int retval, const char *message, ...)
366 va_start(args, message);
367 ovs_fatal_valist(retval, message, args);
371 /* Generic commands. */
374 open_vconn_socket(const char *name, struct vconn **vconnp)
376 char *vconn_name = xasprintf("unix:%s", name);
379 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
381 if (error && error != ENOENT) {
382 ovs_fatal(0, "%s: failed to open socket (%s)", name,
383 ovs_strerror(error));
390 enum open_target { MGMT, SNOOP };
392 static enum ofputil_protocol
393 open_vconn__(const char *name, enum open_target target,
394 struct vconn **vconnp)
396 const char *suffix = target == MGMT ? "mgmt" : "snoop";
397 char *datapath_name, *datapath_type, *socket_name;
398 enum ofputil_protocol protocol;
403 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
405 ofproto_parse_name(name, &datapath_name, &datapath_type);
406 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
410 if (strchr(name, ':')) {
411 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
412 "connecting to %s", name);
413 } else if (!open_vconn_socket(name, vconnp)) {
415 } else if (!open_vconn_socket(bridge_path, vconnp)) {
417 } else if (!open_vconn_socket(socket_name, vconnp)) {
420 ovs_fatal(0, "%s is not a bridge or a socket", name);
423 if (target == SNOOP) {
424 vconn_set_recv_any_version(*vconnp);
430 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
431 error = vconn_connect_block(*vconnp);
433 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
434 ovs_strerror(error));
437 ofp_version = vconn_get_version(*vconnp);
438 protocol = ofputil_protocol_from_ofp_version(ofp_version);
440 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
446 static enum ofputil_protocol
447 open_vconn(const char *name, struct vconn **vconnp)
449 return open_vconn__(name, MGMT, vconnp);
453 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
455 ofpmsg_update_length(buffer);
456 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
460 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
462 struct ofpbuf *reply;
464 ofpmsg_update_length(request);
465 run(vconn_transact(vconn, request, &reply), "talking to %s",
466 vconn_get_name(vconn));
467 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
468 ofpbuf_delete(reply);
472 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
474 struct ofpbuf *request;
477 open_vconn(vconn_name, &vconn);
478 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
479 dump_transaction(vconn, request);
484 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
486 const struct ofp_header *request_oh = request->data;
487 ovs_be32 send_xid = request_oh->xid;
488 enum ofpraw request_raw;
489 enum ofpraw reply_raw;
492 ofpraw_decode_partial(&request_raw, request->data, request->size);
493 reply_raw = ofpraw_stats_request_to_reply(request_raw,
494 request_oh->version);
496 send_openflow_buffer(vconn, request);
499 struct ofpbuf *reply;
501 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
502 recv_xid = ((struct ofp_header *) reply->data)->xid;
503 if (send_xid == recv_xid) {
506 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
508 ofpraw_decode(&raw, reply->data);
509 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
511 } else if (raw == reply_raw) {
512 done = !ofpmp_more(reply->data);
514 ovs_fatal(0, "received bad reply: %s",
515 ofp_to_string(reply->data, reply->size,
519 VLOG_DBG("received reply with xid %08"PRIx32" "
520 "!= expected %08"PRIx32, recv_xid, send_xid);
522 ofpbuf_delete(reply);
527 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
529 struct ofpbuf *request;
532 open_vconn(vconn_name, &vconn);
533 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
534 dump_stats_transaction(vconn, request);
538 /* Sends all of the 'requests', which should be requests that only have replies
539 * if an error occurs, and waits for them to succeed or fail. If an error does
540 * occur, prints it and exits with an error.
542 * Destroys all of the 'requests'. */
544 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
546 struct ofpbuf *request, *reply;
548 LIST_FOR_EACH (request, list_node, requests) {
549 ofpmsg_update_length(request);
552 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
553 "talking to %s", vconn_get_name(vconn));
555 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
558 ofpbuf_delete(reply);
561 /* Sends 'request', which should be a request that only has a reply if an error
562 * occurs, and waits for it to succeed or fail. If an error does occur, prints
563 * it and exits with an error.
565 * Destroys 'request'. */
567 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
569 struct list requests;
571 list_init(&requests);
572 list_push_back(&requests, &request->list_node);
573 transact_multiple_noreply(vconn, &requests);
577 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
579 struct ofp_switch_config *config;
580 struct ofpbuf *request;
581 struct ofpbuf *reply;
584 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
585 vconn_get_version(vconn), 0);
586 run(vconn_transact(vconn, request, &reply),
587 "talking to %s", vconn_get_name(vconn));
589 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
590 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
593 config = ofpbuf_pull(reply, sizeof *config);
596 ofpbuf_delete(reply);
600 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
602 struct ofpbuf *request;
604 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
605 ofpbuf_put(request, config, sizeof *config);
607 transact_noreply(vconn, request);
611 ofctl_show(int argc OVS_UNUSED, char *argv[])
613 const char *vconn_name = argv[1];
615 struct ofpbuf *request;
616 struct ofpbuf *reply;
619 open_vconn(vconn_name, &vconn);
620 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
621 vconn_get_version(vconn), 0);
622 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
624 trunc = ofputil_switch_features_ports_trunc(reply);
625 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
627 ofpbuf_delete(reply);
630 /* The Features Reply may not contain all the ports, so send a
631 * Port Description stats request, which doesn't have size
633 dump_trivial_stats_transaction(vconn_name,
634 OFPRAW_OFPST_PORT_DESC_REQUEST);
636 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
641 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
643 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
647 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
649 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
653 fetch_port_by_features(const char *vconn_name,
654 const char *port_name, ofp_port_t port_no,
655 struct ofputil_phy_port *pp, bool *trunc)
657 struct ofputil_switch_features features;
658 const struct ofp_header *oh;
659 struct ofpbuf *request, *reply;
666 /* Fetch the switch's ofp_switch_features. */
667 open_vconn(vconn_name, &vconn);
668 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
669 vconn_get_version(vconn), 0);
670 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
674 if (ofptype_decode(&type, reply->data)
675 || type != OFPTYPE_FEATURES_REPLY) {
676 ovs_fatal(0, "%s: received bad features reply", vconn_name);
680 if (ofputil_switch_features_ports_trunc(reply)) {
685 error = ofputil_decode_switch_features(oh, &features, &b);
687 ovs_fatal(0, "%s: failed to decode features reply (%s)",
688 vconn_name, ofperr_to_string(error));
691 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
692 if (port_no != OFPP_NONE
693 ? port_no == pp->port_no
694 : !strcmp(pp->name, port_name)) {
701 ofpbuf_delete(reply);
706 fetch_port_by_stats(const char *vconn_name,
707 const char *port_name, ofp_port_t port_no,
708 struct ofputil_phy_port *pp)
710 struct ofpbuf *request;
716 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
717 send_xid = ((struct ofp_header *) request->data)->xid;
719 open_vconn(vconn_name, &vconn);
720 send_openflow_buffer(vconn, request);
723 struct ofpbuf *reply;
725 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
726 recv_xid = ((struct ofp_header *) reply->data)->xid;
727 if (send_xid == recv_xid) {
728 struct ofp_header *oh = reply->data;
733 ofpbuf_use_const(&b, oh, ntohs(oh->length));
734 if (ofptype_pull(&type, &b)
735 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
736 ovs_fatal(0, "received bad reply: %s",
737 ofp_to_string(reply->data, reply->size,
741 flags = ofpmp_flags(oh);
742 done = !(flags & OFPSF_REPLY_MORE);
745 /* We've already found the port, but we need to drain
746 * the queue of any other replies for this request. */
750 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
751 if (port_no != OFPP_NONE ? port_no == pp->port_no
752 : !strcmp(pp->name, port_name)) {
758 VLOG_DBG("received reply with xid %08"PRIx32" "
759 "!= expected %08"PRIx32, recv_xid, send_xid);
761 ofpbuf_delete(reply);
769 str_to_ofp(const char *s, ofp_port_t *ofp_port)
774 ret = str_to_uint(s, 10, &port_);
775 *ofp_port = u16_to_ofp(port_);
779 /* Opens a connection to 'vconn_name', fetches the port structure for
780 * 'port_name' (which may be a port name or number), and copies it into
783 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
784 struct ofputil_phy_port *pp)
790 /* Try to interpret the argument as a port number. */
791 if (!str_to_ofp(port_name, &port_no)) {
795 /* Try to find the port based on the Features Reply. If it looks
796 * like the results may be truncated, then use the Port Description
797 * stats message introduced in OVS 1.7. */
798 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
801 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
805 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
809 /* Returns the port number corresponding to 'port_name' (which may be a port
810 * name or number) within the switch 'vconn_name'. */
812 str_to_port_no(const char *vconn_name, const char *port_name)
816 if (ofputil_port_from_string(port_name, &port_no)) {
819 struct ofputil_phy_port pp;
821 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
827 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
828 enum ofputil_protocol *cur)
831 struct ofpbuf *request, *reply;
832 enum ofputil_protocol next;
834 request = ofputil_encode_set_protocol(*cur, want, &next);
839 run(vconn_transact_noreply(vconn, request, &reply),
840 "talking to %s", vconn_get_name(vconn));
842 char *s = ofp_to_string(reply->data, reply->size, 2);
843 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
844 vconn_get_name(vconn), s);
846 ofpbuf_delete(reply);
854 static enum ofputil_protocol
855 set_protocol_for_flow_dump(struct vconn *vconn,
856 enum ofputil_protocol cur_protocol,
857 enum ofputil_protocol usable_protocols)
862 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
863 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
864 if (f & usable_protocols & allowed_protocols
865 && try_set_protocol(vconn, f, &cur_protocol)) {
870 usable_s = ofputil_protocols_to_string(usable_protocols);
871 if (usable_protocols & allowed_protocols) {
872 ovs_fatal(0, "switch does not support any of the usable flow "
873 "formats (%s)", usable_s);
875 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
876 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
877 "allowed flow formats (%s)", usable_s, allowed_s);
881 static struct vconn *
882 prepare_dump_flows(int argc, char *argv[], bool aggregate,
883 struct ofpbuf **requestp)
885 enum ofputil_protocol usable_protocols, protocol;
886 struct ofputil_flow_stats_request fsr;
890 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
891 argc > 2 ? argv[2] : "",
894 & OFPUTIL_P_OF10_ANY));
896 ovs_fatal(0, "%s", error);
899 protocol = open_vconn(argv[1], &vconn);
900 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
901 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
906 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
908 struct ofpbuf *request;
911 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
912 dump_stats_transaction(vconn, request);
917 compare_flows(const void *afs_, const void *bfs_)
919 const struct ofputil_flow_stats *afs = afs_;
920 const struct ofputil_flow_stats *bfs = bfs_;
921 const struct match *a = &afs->match;
922 const struct match *b = &bfs->match;
923 const struct sort_criterion *sc;
925 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
926 const struct mf_field *f = sc->field;
930 unsigned int a_pri = afs->priority;
931 unsigned int b_pri = bfs->priority;
932 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
936 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
937 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
939 /* Skip the test for sc->order, so that missing fields always
940 * sort to the end whether we're sorting in ascending or
941 * descending order. */
944 union mf_value aval, bval;
946 mf_get_value(f, &a->flow, &aval);
947 mf_get_value(f, &b->flow, &bval);
948 ret = memcmp(&aval, &bval, f->n_bytes);
953 return sc->order == SORT_ASC ? ret : -ret;
961 ofctl_dump_flows(int argc, char *argv[])
964 return ofctl_dump_flows__(argc, argv, false);
966 struct ofputil_flow_stats *fses;
967 size_t n_fses, allocated_fses;
968 struct ofpbuf *request;
969 struct ofpbuf ofpacts;
970 struct ofpbuf *reply;
976 vconn = prepare_dump_flows(argc, argv, false, &request);
977 send_xid = ((struct ofp_header *) request->data)->xid;
978 send_openflow_buffer(vconn, request);
981 n_fses = allocated_fses = 0;
983 ofpbuf_init(&ofpacts, 0);
985 struct ofputil_flow_stats *fs;
987 if (n_fses >= allocated_fses) {
988 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
992 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
996 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
999 ofpbuf_uninit(&ofpacts);
1001 qsort(fses, n_fses, sizeof *fses, compare_flows);
1004 for (i = 0; i < n_fses; i++) {
1006 ofp_print_flow_stats(&s, &fses[i]);
1011 for (i = 0; i < n_fses; i++) {
1012 free(fses[i].ofpacts);
1021 ofctl_dump_aggregate(int argc, char *argv[])
1023 return ofctl_dump_flows__(argc, argv, true);
1027 ofctl_queue_stats(int argc, char *argv[])
1029 struct ofpbuf *request;
1030 struct vconn *vconn;
1031 struct ofputil_queue_stats_request oqs;
1033 open_vconn(argv[1], &vconn);
1035 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1036 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1038 oqs.port_no = OFPP_ANY;
1040 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1041 oqs.queue_id = atoi(argv[3]);
1043 oqs.queue_id = OFPQ_ALL;
1046 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1047 dump_stats_transaction(vconn, request);
1051 static enum ofputil_protocol
1052 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1053 enum ofputil_protocol usable_protocols)
1055 enum ofputil_protocol cur_protocol;
1059 if (!(usable_protocols & allowed_protocols)) {
1060 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1061 usable_s = ofputil_protocols_to_string(usable_protocols);
1062 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1063 "allowed flow formats (%s)", usable_s, allowed_s);
1066 /* If the initial flow format is allowed and usable, keep it. */
1067 cur_protocol = open_vconn(remote, vconnp);
1068 if (usable_protocols & allowed_protocols & cur_protocol) {
1069 return cur_protocol;
1072 /* Otherwise try each flow format in turn. */
1073 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1074 enum ofputil_protocol f = 1 << i;
1076 if (f != cur_protocol
1077 && f & usable_protocols & allowed_protocols
1078 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1083 usable_s = ofputil_protocols_to_string(usable_protocols);
1084 ovs_fatal(0, "switch does not support any of the usable flow "
1085 "formats (%s)", usable_s);
1089 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1090 size_t n_fms, enum ofputil_protocol usable_protocols)
1092 enum ofputil_protocol protocol;
1093 struct vconn *vconn;
1096 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1098 for (i = 0; i < n_fms; i++) {
1099 struct ofputil_flow_mod *fm = &fms[i];
1101 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1108 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1110 enum ofputil_protocol usable_protocols;
1111 struct ofputil_flow_mod *fms = NULL;
1115 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1117 !(allowed_protocols & OFPUTIL_P_OF10_ANY));
1119 ovs_fatal(0, "%s", error);
1121 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1126 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1128 if (argc > 2 && !strcmp(argv[2], "-")) {
1129 ofctl_flow_mod_file(argc, argv, command);
1131 struct ofputil_flow_mod fm;
1133 enum ofputil_protocol usable_protocols;
1135 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1138 & OFPUTIL_P_OF10_ANY));
1140 ovs_fatal(0, "%s", error);
1142 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1147 ofctl_add_flow(int argc, char *argv[])
1149 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1153 ofctl_add_flows(int argc, char *argv[])
1155 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1159 ofctl_mod_flows(int argc, char *argv[])
1161 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1165 ofctl_del_flows(int argc, char *argv[])
1167 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1171 set_packet_in_format(struct vconn *vconn,
1172 enum nx_packet_in_format packet_in_format)
1174 struct ofpbuf *spif;
1176 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1178 transact_noreply(vconn, spif);
1179 VLOG_DBG("%s: using user-specified packet in format %s",
1180 vconn_get_name(vconn),
1181 ofputil_packet_in_format_to_string(packet_in_format));
1185 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1187 struct ofp_switch_config config;
1188 enum ofp_config_flags flags;
1190 fetch_switch_config(vconn, &config);
1191 flags = ntohs(config.flags);
1192 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1193 /* Set the invalid ttl config. */
1194 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1196 config.flags = htons(flags);
1197 set_switch_config(vconn, &config);
1199 /* Then retrieve the configuration to see if it really took. OpenFlow
1200 * doesn't define error reporting for bad modes, so this is all we can
1202 fetch_switch_config(vconn, &config);
1203 flags = ntohs(config.flags);
1204 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1205 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1206 "switch probably doesn't support mode)");
1213 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1214 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1215 * an error message and stores NULL in '*msgp'. */
1217 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1219 struct ofp_header *oh;
1222 msg = ofpbuf_new(strlen(hex) / 2);
1225 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1227 return "Trailing garbage in hex data";
1230 if (msg->size < sizeof(struct ofp_header)) {
1232 return "Message too short for OpenFlow";
1236 if (msg->size != ntohs(oh->length)) {
1238 return "Message size does not match length in OpenFlow header";
1246 ofctl_send(struct unixctl_conn *conn, int argc,
1247 const char *argv[], void *vconn_)
1249 struct vconn *vconn = vconn_;
1256 for (i = 1; i < argc; i++) {
1257 const char *error_msg;
1261 error_msg = openflow_from_hex(argv[i], &msg);
1263 ds_put_format(&reply, "%s\n", error_msg);
1268 fprintf(stderr, "send: ");
1269 ofp_print(stderr, msg->data, msg->size, verbosity);
1271 error = vconn_send_block(vconn, msg);
1274 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1277 ds_put_cstr(&reply, "sent\n");
1282 unixctl_command_reply(conn, ds_cstr(&reply));
1284 unixctl_command_reply_error(conn, ds_cstr(&reply));
1289 struct barrier_aux {
1290 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1291 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1295 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1296 const char *argv[] OVS_UNUSED, void *aux_)
1298 struct barrier_aux *aux = aux_;
1303 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1307 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1308 error = vconn_send_block(aux->vconn, msg);
1311 unixctl_command_reply_error(conn, ovs_strerror(error));
1318 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1319 const char *argv[], void *aux OVS_UNUSED)
1323 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1325 unixctl_command_reply_error(conn, ovs_strerror(errno));
1330 dup2(fd, STDERR_FILENO);
1332 unixctl_command_reply(conn, NULL);
1336 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1337 const char *argv[] OVS_UNUSED, void *blocked_)
1339 bool *blocked = blocked_;
1343 unixctl_command_reply(conn, NULL);
1345 unixctl_command_reply(conn, "already blocking");
1350 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1351 const char *argv[] OVS_UNUSED, void *blocked_)
1353 bool *blocked = blocked_;
1357 unixctl_command_reply(conn, NULL);
1359 unixctl_command_reply(conn, "already unblocked");
1363 /* Prints to stdout all of the messages received on 'vconn'.
1365 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1366 * received on 'vconn'. */
1368 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1370 struct barrier_aux barrier_aux = { vconn, NULL };
1371 struct unixctl_server *server;
1372 bool exiting = false;
1373 bool blocked = false;
1376 daemon_save_fd(STDERR_FILENO);
1378 error = unixctl_server_create(NULL, &server);
1380 ovs_fatal(error, "failed to create unixctl server");
1382 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1383 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1385 unixctl_command_register("ofctl/barrier", "", 0, 0,
1386 ofctl_barrier, &barrier_aux);
1387 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1388 ofctl_set_output_file, NULL);
1390 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1391 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1394 daemonize_complete();
1400 unixctl_server_run(server);
1405 retval = vconn_recv(vconn, &b);
1406 if (retval == EAGAIN) {
1409 run(retval, "vconn_recv");
1412 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1413 time_wall_msec(), true);
1418 ofptype_decode(&type, b->data);
1419 ofp_print(stderr, b->data, b->size, verbosity + 2);
1421 switch ((int) type) {
1422 case OFPTYPE_BARRIER_REPLY:
1423 if (barrier_aux.conn) {
1424 unixctl_command_reply(barrier_aux.conn, NULL);
1425 barrier_aux.conn = NULL;
1429 case OFPTYPE_ECHO_REQUEST:
1430 if (reply_to_echo_requests) {
1431 struct ofpbuf *reply;
1433 reply = make_echo_reply(b->data);
1434 retval = vconn_send_block(vconn, reply);
1436 ovs_fatal(retval, "failed to send echo reply");
1449 vconn_run_wait(vconn);
1451 vconn_recv_wait(vconn);
1453 unixctl_server_wait(server);
1457 unixctl_server_destroy(server);
1461 ofctl_monitor(int argc, char *argv[])
1463 struct vconn *vconn;
1465 enum ofputil_protocol usable_protocols;
1467 open_vconn(argv[1], &vconn);
1468 for (i = 2; i < argc; i++) {
1469 const char *arg = argv[i];
1471 if (isdigit((unsigned char) *arg)) {
1472 struct ofp_switch_config config;
1474 fetch_switch_config(vconn, &config);
1475 config.miss_send_len = htons(atoi(arg));
1476 set_switch_config(vconn, &config);
1477 } else if (!strcmp(arg, "invalid_ttl")) {
1478 monitor_set_invalid_ttl_to_controller(vconn);
1479 } else if (!strncmp(arg, "watch:", 6)) {
1480 struct ofputil_flow_monitor_request fmr;
1484 error = parse_flow_monitor_request(&fmr, arg + 6,
1487 ovs_fatal(0, "%s", error);
1490 msg = ofpbuf_new(0);
1491 ofputil_append_flow_monitor_request(&fmr, msg);
1492 dump_stats_transaction(vconn, msg);
1494 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1498 if (preferred_packet_in_format >= 0) {
1499 set_packet_in_format(vconn, preferred_packet_in_format);
1501 enum ofp_version version = vconn_get_version(vconn);
1504 case OFP10_VERSION: {
1505 struct ofpbuf *spif, *reply;
1507 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1509 run(vconn_transact_noreply(vconn, spif, &reply),
1510 "talking to %s", vconn_get_name(vconn));
1512 char *s = ofp_to_string(reply->data, reply->size, 2);
1513 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1514 " replied: %s. Falling back to the switch default.",
1515 vconn_get_name(vconn), s);
1517 ofpbuf_delete(reply);
1530 monitor_vconn(vconn, true);
1534 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1536 struct vconn *vconn;
1538 open_vconn__(argv[1], SNOOP, &vconn);
1539 monitor_vconn(vconn, false);
1543 ofctl_dump_ports(int argc, char *argv[])
1545 struct ofpbuf *request;
1546 struct vconn *vconn;
1549 open_vconn(argv[1], &vconn);
1550 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1551 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1552 dump_stats_transaction(vconn, request);
1557 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1559 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1563 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1565 struct ofpbuf *request;
1566 struct vconn *vconn;
1567 struct ofpbuf *reply;
1569 open_vconn(argv[1], &vconn);
1570 request = make_echo_request(vconn_get_version(vconn));
1571 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1572 if (reply->size != sizeof(struct ofp_header)) {
1573 ovs_fatal(0, "reply does not match request");
1575 ofpbuf_delete(reply);
1580 ofctl_packet_out(int argc, char *argv[])
1582 enum ofputil_protocol protocol;
1583 struct ofputil_packet_out po;
1584 struct ofpbuf ofpacts;
1585 struct vconn *vconn;
1588 enum ofputil_protocol usable_protocols; /* TODO: Use in proto selection */
1590 ofpbuf_init(&ofpacts, 64);
1591 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1593 ovs_fatal(0, "%s", error);
1596 po.buffer_id = UINT32_MAX;
1597 po.in_port = str_to_port_no(argv[1], argv[2]);
1598 po.ofpacts = ofpacts.data;
1599 po.ofpacts_len = ofpacts.size;
1601 protocol = open_vconn(argv[1], &vconn);
1602 for (i = 4; i < argc; i++) {
1603 struct ofpbuf *packet, *opo;
1604 const char *error_msg;
1606 error_msg = eth_from_hex(argv[i], &packet);
1608 ovs_fatal(0, "%s", error_msg);
1611 po.packet = packet->data;
1612 po.packet_len = packet->size;
1613 opo = ofputil_encode_packet_out(&po, protocol);
1614 transact_noreply(vconn, opo);
1615 ofpbuf_delete(packet);
1618 ofpbuf_uninit(&ofpacts);
1622 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1624 struct ofp_config_flag {
1625 const char *name; /* The flag's name. */
1626 enum ofputil_port_config bit; /* Bit to turn on or off. */
1627 bool on; /* Value to set the bit to. */
1629 static const struct ofp_config_flag flags[] = {
1630 { "up", OFPUTIL_PC_PORT_DOWN, false },
1631 { "down", OFPUTIL_PC_PORT_DOWN, true },
1632 { "stp", OFPUTIL_PC_NO_STP, false },
1633 { "receive", OFPUTIL_PC_NO_RECV, false },
1634 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1635 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1636 { "forward", OFPUTIL_PC_NO_FWD, false },
1637 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1640 const struct ofp_config_flag *flag;
1641 enum ofputil_protocol protocol;
1642 struct ofputil_port_mod pm;
1643 struct ofputil_phy_port pp;
1644 struct vconn *vconn;
1645 const char *command;
1648 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1650 pm.port_no = pp.port_no;
1651 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1656 if (!strncasecmp(argv[3], "no-", 3)) {
1657 command = argv[3] + 3;
1659 } else if (!strncasecmp(argv[3], "no", 2)) {
1660 command = argv[3] + 2;
1666 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1667 if (!strcasecmp(command, flag->name)) {
1668 pm.mask = flag->bit;
1669 pm.config = flag->on ^ not ? flag->bit : 0;
1673 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1676 protocol = open_vconn(argv[1], &vconn);
1677 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1682 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1684 enum ofputil_protocol protocol, usable_protocols;
1685 struct ofputil_table_mod tm;
1686 struct vconn *vconn;
1690 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1692 ovs_fatal(0, "%s", error);
1695 protocol = open_vconn(argv[1], &vconn);
1696 if (!(protocol & usable_protocols)) {
1697 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1698 enum ofputil_protocol f = 1 << i;
1700 && f & usable_protocols
1701 && try_set_protocol(vconn, f, &protocol)) {
1708 if (!(protocol & usable_protocols)) {
1709 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1710 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1713 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1718 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1720 struct ofp_switch_config config;
1721 struct vconn *vconn;
1723 open_vconn(argv[1], &vconn);
1724 fetch_switch_config(vconn, &config);
1725 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1730 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1732 struct ofp_switch_config config;
1733 enum ofp_config_flags mode;
1734 struct vconn *vconn;
1737 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1738 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1741 open_vconn(argv[1], &vconn);
1742 fetch_switch_config(vconn, &config);
1743 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1744 if (flags != config.flags) {
1745 /* Set the configuration. */
1746 config.flags = flags;
1747 set_switch_config(vconn, &config);
1749 /* Then retrieve the configuration to see if it really took. OpenFlow
1750 * doesn't define error reporting for bad modes, so this is all we can
1752 fetch_switch_config(vconn, &config);
1753 if (flags != config.flags) {
1754 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1755 "switch probably doesn't support mode \"%s\")",
1756 argv[1], ofputil_frag_handling_to_string(mode));
1763 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1765 const char *filename = argv[1];
1769 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1771 ovs_fatal(errno, "%s: open", filename);
1774 ofpbuf_init(&b, 65536);
1776 struct ofp_header *oh;
1777 size_t length, tail_len;
1782 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1783 n = fread(oh, 1, sizeof *oh, file);
1786 } else if (n < sizeof *oh) {
1787 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1790 length = ntohs(oh->length);
1791 if (length < sizeof *oh) {
1792 ovs_fatal(0, "%s: %zu-byte message is too short for OpenFlow",
1796 tail_len = length - sizeof *oh;
1797 tail = ofpbuf_put_uninit(&b, tail_len);
1798 n = fread(tail, 1, tail_len, file);
1800 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1803 ofp_print(stdout, b.data, b.size, verbosity + 2);
1807 if (file != stdin) {
1813 ofctl_ping(int argc, char *argv[])
1815 size_t max_payload = 65535 - sizeof(struct ofp_header);
1816 unsigned int payload;
1817 struct vconn *vconn;
1820 payload = argc > 2 ? atoi(argv[2]) : 64;
1821 if (payload > max_payload) {
1822 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1825 open_vconn(argv[1], &vconn);
1826 for (i = 0; i < 10; i++) {
1827 struct timeval start, end;
1828 struct ofpbuf *request, *reply;
1829 const struct ofp_header *rpy_hdr;
1832 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1833 vconn_get_version(vconn), payload);
1834 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1836 xgettimeofday(&start);
1837 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1838 xgettimeofday(&end);
1840 rpy_hdr = reply->data;
1841 if (ofptype_pull(&type, reply)
1842 || type != OFPTYPE_ECHO_REPLY
1843 || reply->size != payload
1844 || memcmp(request->l3, reply->l3, payload)) {
1845 printf("Reply does not match request. Request:\n");
1846 ofp_print(stdout, request, request->size, verbosity + 2);
1848 ofp_print(stdout, reply, reply->size, verbosity + 2);
1850 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1851 reply->size, argv[1], ntohl(rpy_hdr->xid),
1852 (1000*(double)(end.tv_sec - start.tv_sec))
1853 + (.001*(end.tv_usec - start.tv_usec)));
1854 ofpbuf_delete(request);
1855 ofpbuf_delete(reply);
1861 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1863 size_t max_payload = 65535 - sizeof(struct ofp_header);
1864 struct timeval start, end;
1865 unsigned int payload_size, message_size;
1866 struct vconn *vconn;
1871 payload_size = atoi(argv[2]);
1872 if (payload_size > max_payload) {
1873 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1875 message_size = sizeof(struct ofp_header) + payload_size;
1877 count = atoi(argv[3]);
1879 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1880 count, message_size, count * message_size);
1882 open_vconn(argv[1], &vconn);
1883 xgettimeofday(&start);
1884 for (i = 0; i < count; i++) {
1885 struct ofpbuf *request, *reply;
1887 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1888 vconn_get_version(vconn), payload_size);
1889 ofpbuf_put_zeros(request, payload_size);
1890 run(vconn_transact(vconn, request, &reply), "transact");
1891 ofpbuf_delete(reply);
1893 xgettimeofday(&end);
1896 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1897 + (.001*(end.tv_usec - start.tv_usec)));
1898 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1899 duration, count / (duration / 1000.0),
1900 count * message_size / (duration / 1000.0));
1904 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
1907 struct ofputil_group_mod *gm;
1908 struct ofpbuf *request;
1910 struct vconn *vconn;
1913 open_vconn(remote, &vconn);
1915 for (i = 0; i < n_gms; i++) {
1917 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
1919 transact_noreply(vconn, request);
1929 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1931 struct ofputil_group_mod *gms = NULL;
1932 enum ofputil_protocol usable_protocols;
1936 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
1939 ovs_fatal(0, "%s", error);
1941 ofctl_group_mod__(argv[1], gms, n_gms);
1946 ofctl_group_mod(int argc, char *argv[], uint16_t command)
1948 if (argc > 2 && !strcmp(argv[2], "-")) {
1949 ofctl_group_mod_file(argc, argv, command);
1951 enum ofputil_protocol usable_protocols;
1952 struct ofputil_group_mod gm;
1955 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
1958 ovs_fatal(0, "%s", error);
1960 ofctl_group_mod__(argv[1], &gm, 1);
1965 ofctl_add_group(int argc, char *argv[])
1967 ofctl_group_mod(argc, argv, OFPGC11_ADD);
1971 ofctl_add_groups(int argc, char *argv[])
1973 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
1977 ofctl_mod_group(int argc, char *argv[])
1979 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
1983 ofctl_del_groups(int argc, char *argv[])
1985 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
1989 ofctl_dump_group_stats(int argc, char *argv[])
1991 enum ofputil_protocol usable_protocols;
1992 struct ofputil_group_mod gm;
1993 struct ofpbuf *request;
1994 struct vconn *vconn;
1998 memset(&gm, 0, sizeof gm);
2000 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2001 argc > 2 ? argv[2] : "",
2004 ovs_fatal(0, "%s", error);
2007 group_id = gm.group_id;
2009 open_vconn(argv[1], &vconn);
2010 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2013 dump_stats_transaction(vconn, request);
2020 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2022 struct ofpbuf *request;
2023 struct vconn *vconn;
2025 open_vconn(argv[1], &vconn);
2027 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2029 dump_stats_transaction(vconn, request);
2036 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2038 struct ofpbuf *request;
2039 struct vconn *vconn;
2041 open_vconn(argv[1], &vconn);
2042 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2044 dump_stats_transaction(vconn, request);
2051 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2056 /* replace-flows and diff-flows commands. */
2058 /* A flow table entry, possibly with two different versions. */
2060 struct cls_rule rule; /* Within a "struct classifier". */
2061 struct fte_version *versions[2];
2064 /* One version of a Flow Table Entry. */
2065 struct fte_version {
2067 uint16_t idle_timeout;
2068 uint16_t hard_timeout;
2070 struct ofpact *ofpacts;
2074 /* Frees 'version' and the data that it owns. */
2076 fte_version_free(struct fte_version *version)
2079 free(version->ofpacts);
2084 /* Returns true if 'a' and 'b' are the same, false if they differ.
2086 * Ignores differences in 'flags' because there's no way to retrieve flags from
2087 * an OpenFlow switch. We have to assume that they are the same. */
2089 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2091 return (a->cookie == b->cookie
2092 && a->idle_timeout == b->idle_timeout
2093 && a->hard_timeout == b->hard_timeout
2094 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2095 b->ofpacts, b->ofpacts_len));
2098 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2099 * 'index' into 's', followed by a new-line. */
2101 fte_version_format(const struct fte *fte, int index, struct ds *s)
2103 const struct fte_version *version = fte->versions[index];
2110 cls_rule_format(&fte->rule, s);
2111 if (version->cookie != htonll(0)) {
2112 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2114 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2115 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2117 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2118 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2121 ds_put_cstr(s, " actions=");
2122 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2124 ds_put_char(s, '\n');
2128 fte_from_cls_rule(const struct cls_rule *cls_rule)
2130 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2133 /* Frees 'fte' and its versions. */
2135 fte_free(struct fte *fte)
2138 fte_version_free(fte->versions[0]);
2139 fte_version_free(fte->versions[1]);
2140 cls_rule_destroy(&fte->rule);
2145 /* Frees all of the FTEs within 'cls'. */
2147 fte_free_all(struct classifier *cls)
2149 struct cls_cursor cursor;
2150 struct fte *fte, *next;
2152 ovs_rwlock_wrlock(&cls->rwlock);
2153 cls_cursor_init(&cursor, cls, NULL);
2154 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2155 classifier_remove(cls, &fte->rule);
2158 ovs_rwlock_unlock(&cls->rwlock);
2159 classifier_destroy(cls);
2162 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2163 * necessary. Sets 'version' as the version of that rule with the given
2164 * 'index', replacing any existing version, if any.
2166 * Takes ownership of 'version'. */
2168 fte_insert(struct classifier *cls, const struct match *match,
2169 unsigned int priority, struct fte_version *version, int index)
2171 struct fte *old, *fte;
2173 fte = xzalloc(sizeof *fte);
2174 cls_rule_init(&fte->rule, match, priority);
2175 fte->versions[index] = version;
2177 ovs_rwlock_wrlock(&cls->rwlock);
2178 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2179 ovs_rwlock_unlock(&cls->rwlock);
2181 fte_version_free(old->versions[index]);
2182 fte->versions[!index] = old->versions[!index];
2183 cls_rule_destroy(&old->rule);
2188 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2189 * with the specified 'index'. Returns the flow formats able to represent the
2190 * flows that were read. */
2191 static enum ofputil_protocol
2192 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2194 enum ofputil_protocol usable_protocols;
2199 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2201 ovs_fatal(errno, "%s: open", filename);
2205 usable_protocols = OFPUTIL_P_ANY;
2207 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2208 struct fte_version *version;
2209 struct ofputil_flow_mod fm;
2211 enum ofputil_protocol usable;
2213 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable,
2214 !(allowed_protocols & OFPUTIL_P_OF10_ANY));
2216 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2218 usable_protocols &= usable;
2220 version = xmalloc(sizeof *version);
2221 version->cookie = fm.new_cookie;
2222 version->idle_timeout = fm.idle_timeout;
2223 version->hard_timeout = fm.hard_timeout;
2224 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2225 | OFPUTIL_FF_EMERG);
2226 version->ofpacts = fm.ofpacts;
2227 version->ofpacts_len = fm.ofpacts_len;
2229 fte_insert(cls, &fm.match, fm.priority, version, index);
2233 if (file != stdin) {
2237 return usable_protocols;
2241 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2242 struct ofpbuf **replyp,
2243 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2245 struct ofpbuf *reply = *replyp;
2251 /* Get a flow stats reply message, if we don't already have one. */
2257 run(vconn_recv_block(vconn, &reply),
2258 "OpenFlow packet receive failed");
2259 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2261 error = ofptype_decode(&type, reply->data);
2262 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2263 ovs_fatal(0, "received bad reply: %s",
2264 ofp_to_string(reply->data, reply->size,
2269 /* Pull an individual flow stats reply out of the message. */
2270 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2277 more = ofpmp_more(reply->l2);
2278 ofpbuf_delete(reply);
2287 ovs_fatal(0, "parse error in reply (%s)",
2288 ofperr_to_string(retval));
2293 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2294 * format 'protocol', and adds them as flow table entries in 'cls' for the
2295 * version with the specified 'index'. */
2297 read_flows_from_switch(struct vconn *vconn,
2298 enum ofputil_protocol protocol,
2299 struct classifier *cls, int index)
2301 struct ofputil_flow_stats_request fsr;
2302 struct ofputil_flow_stats fs;
2303 struct ofpbuf *request;
2304 struct ofpbuf ofpacts;
2305 struct ofpbuf *reply;
2308 fsr.aggregate = false;
2309 match_init_catchall(&fsr.match);
2310 fsr.out_port = OFPP_ANY;
2311 fsr.table_id = 0xff;
2312 fsr.cookie = fsr.cookie_mask = htonll(0);
2313 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2314 send_xid = ((struct ofp_header *) request->data)->xid;
2315 send_openflow_buffer(vconn, request);
2318 ofpbuf_init(&ofpacts, 0);
2319 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2320 struct fte_version *version;
2322 version = xmalloc(sizeof *version);
2323 version->cookie = fs.cookie;
2324 version->idle_timeout = fs.idle_timeout;
2325 version->hard_timeout = fs.hard_timeout;
2327 version->ofpacts_len = fs.ofpacts_len;
2328 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2330 fte_insert(cls, &fs.match, fs.priority, version, index);
2332 ofpbuf_uninit(&ofpacts);
2336 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2337 enum ofputil_protocol protocol, struct list *packets)
2339 const struct fte_version *version = fte->versions[index];
2340 struct ofputil_flow_mod fm;
2343 minimatch_expand(&fte->rule.match, &fm.match);
2344 fm.priority = fte->rule.priority;
2345 fm.cookie = htonll(0);
2346 fm.cookie_mask = htonll(0);
2347 fm.new_cookie = version->cookie;
2348 fm.modify_cookie = true;
2350 fm.command = command;
2351 fm.idle_timeout = version->idle_timeout;
2352 fm.hard_timeout = version->hard_timeout;
2353 fm.buffer_id = UINT32_MAX;
2354 fm.out_port = OFPP_ANY;
2355 fm.flags = version->flags;
2356 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2357 command == OFPFC_MODIFY_STRICT) {
2358 fm.ofpacts = version->ofpacts;
2359 fm.ofpacts_len = version->ofpacts_len;
2365 ofm = ofputil_encode_flow_mod(&fm, protocol);
2366 list_push_back(packets, &ofm->list_node);
2370 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2372 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2373 enum ofputil_protocol usable_protocols, protocol;
2374 struct cls_cursor cursor;
2375 struct classifier cls;
2376 struct list requests;
2377 struct vconn *vconn;
2380 classifier_init(&cls);
2381 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2383 protocol = open_vconn(argv[1], &vconn);
2384 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2386 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2388 list_init(&requests);
2390 /* Delete flows that exist on the switch but not in the file. */
2391 ovs_rwlock_rdlock(&cls.rwlock);
2392 cls_cursor_init(&cursor, &cls, NULL);
2393 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2394 struct fte_version *file_ver = fte->versions[FILE_IDX];
2395 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2397 if (sw_ver && !file_ver) {
2398 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2399 protocol, &requests);
2403 /* Add flows that exist in the file but not on the switch.
2404 * Update flows that exist in both places but differ. */
2405 cls_cursor_init(&cursor, &cls, NULL);
2406 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2407 struct fte_version *file_ver = fte->versions[FILE_IDX];
2408 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2411 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2412 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2415 ovs_rwlock_unlock(&cls.rwlock);
2416 transact_multiple_noreply(vconn, &requests);
2423 read_flows_from_source(const char *source, struct classifier *cls, int index)
2427 if (source[0] == '/' || source[0] == '.'
2428 || (!strchr(source, ':') && !stat(source, &s))) {
2429 read_flows_from_file(source, cls, index);
2431 enum ofputil_protocol protocol;
2432 struct vconn *vconn;
2434 protocol = open_vconn(source, &vconn);
2435 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2436 read_flows_from_switch(vconn, protocol, cls, index);
2442 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2444 bool differences = false;
2445 struct cls_cursor cursor;
2446 struct classifier cls;
2450 classifier_init(&cls);
2451 read_flows_from_source(argv[1], &cls, 0);
2452 read_flows_from_source(argv[2], &cls, 1);
2457 ovs_rwlock_rdlock(&cls.rwlock);
2458 cls_cursor_init(&cursor, &cls, NULL);
2459 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2460 struct fte_version *a = fte->versions[0];
2461 struct fte_version *b = fte->versions[1];
2463 if (!a || !b || !fte_version_equals(a, b)) {
2464 fte_version_format(fte, 0, &a_s);
2465 fte_version_format(fte, 1, &b_s);
2466 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2468 printf("-%s", ds_cstr(&a_s));
2471 printf("+%s", ds_cstr(&b_s));
2477 ovs_rwlock_unlock(&cls.rwlock);
2490 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2492 struct ofputil_meter_mod mm;
2493 struct vconn *vconn;
2494 enum ofputil_protocol protocol;
2495 enum ofputil_protocol usable_protocols;
2496 enum ofp_version version;
2500 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2502 ovs_fatal(0, "%s", error);
2505 usable_protocols = OFPUTIL_P_OF13_UP;
2506 mm.command = command;
2507 mm.meter.meter_id = OFPM13_ALL;
2510 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2511 version = ofputil_protocol_to_ofp_version(protocol);
2512 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2517 ofctl_meter_request__(const char *bridge, const char *str,
2518 enum ofputil_meter_request_type type)
2520 struct ofputil_meter_mod mm;
2521 struct vconn *vconn;
2522 enum ofputil_protocol usable_protocols;
2523 enum ofputil_protocol protocol;
2524 enum ofp_version version;
2528 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2530 ovs_fatal(0, "%s", error);
2533 usable_protocols = OFPUTIL_P_OF13_UP;
2534 mm.meter.meter_id = OFPM13_ALL;
2537 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2538 version = ofputil_protocol_to_ofp_version(protocol);
2539 transact_noreply(vconn, ofputil_encode_meter_request(version,
2541 mm.meter.meter_id));
2547 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2549 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2553 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2555 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2559 ofctl_del_meters(int argc, char *argv[])
2561 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2565 ofctl_dump_meters(int argc, char *argv[])
2567 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2568 OFPUTIL_METER_CONFIG);
2572 ofctl_meter_stats(int argc, char *argv[])
2574 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2575 OFPUTIL_METER_STATS);
2579 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2581 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2585 /* Undocumented commands for unit testing. */
2588 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2589 enum ofputil_protocol usable_protocols)
2591 enum ofputil_protocol protocol = 0;
2595 usable_s = ofputil_protocols_to_string(usable_protocols);
2596 printf("usable protocols: %s\n", usable_s);
2599 if (!(usable_protocols & allowed_protocols)) {
2600 ovs_fatal(0, "no usable protocol");
2602 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2604 if (protocol & usable_protocols & allowed_protocols) {
2608 ovs_assert(is_pow2(protocol));
2610 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2612 for (i = 0; i < n_fms; i++) {
2613 struct ofputil_flow_mod *fm = &fms[i];
2616 msg = ofputil_encode_flow_mod(fm, protocol);
2617 ofp_print(stdout, msg->data, msg->size, verbosity);
2624 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2625 * it back to stdout. */
2627 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2629 enum ofputil_protocol usable_protocols;
2630 struct ofputil_flow_mod fm;
2633 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols,
2634 !(allowed_protocols & OFPUTIL_P_OF10_ANY));
2636 ovs_fatal(0, "%s", error);
2638 ofctl_parse_flows__(&fm, 1, usable_protocols);
2641 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2642 * add-flows) and prints each of the flows back to stdout. */
2644 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2646 enum ofputil_protocol usable_protocols;
2647 struct ofputil_flow_mod *fms = NULL;
2651 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2653 !(allowed_protocols & OFPUTIL_P_OF10_ANY));
2655 ovs_fatal(0, "%s", error);
2657 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2662 ofctl_parse_nxm__(bool oxm)
2667 while (!ds_get_test_line(&in, stdin)) {
2668 struct ofpbuf nx_match;
2670 ovs_be64 cookie, cookie_mask;
2674 /* Convert string to nx_match. */
2675 ofpbuf_init(&nx_match, 0);
2677 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2679 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2682 /* Convert nx_match to match. */
2685 error = oxm_pull_match(&nx_match, &match);
2687 error = nx_pull_match(&nx_match, match_len, &match,
2688 &cookie, &cookie_mask);
2692 error = oxm_pull_match_loose(&nx_match, &match);
2694 error = nx_pull_match_loose(&nx_match, match_len, &match,
2695 &cookie, &cookie_mask);
2703 /* Convert match back to nx_match. */
2704 ofpbuf_uninit(&nx_match);
2705 ofpbuf_init(&nx_match, 0);
2707 match_len = oxm_put_match(&nx_match, &match);
2708 out = oxm_match_to_string(&nx_match, match_len);
2710 match_len = nx_put_match(&nx_match, &match,
2711 cookie, cookie_mask);
2712 out = nx_match_to_string(nx_match.data, match_len);
2718 printf("nx_pull_match() returned error %s\n",
2719 ofperr_get_name(error));
2722 ofpbuf_uninit(&nx_match);
2727 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2728 * stdin, does some internal fussing with them, and then prints them back as
2729 * strings on stdout. */
2731 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2733 return ofctl_parse_nxm__(false);
2736 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2737 * stdin, does some internal fussing with them, and then prints them back as
2738 * strings on stdout. */
2740 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2742 return ofctl_parse_nxm__(true);
2746 print_differences(const char *prefix,
2747 const void *a_, size_t a_len,
2748 const void *b_, size_t b_len)
2750 const uint8_t *a = a_;
2751 const uint8_t *b = b_;
2754 for (i = 0; i < MIN(a_len, b_len); i++) {
2756 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2757 prefix, i, a[i], b[i]);
2760 for (i = a_len; i < b_len; i++) {
2761 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2763 for (i = b_len; i < a_len; i++) {
2764 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2768 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2769 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2770 * on stdout, and then converts them back to hex bytes and prints any
2771 * differences from the input. */
2773 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2778 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2779 struct ofpbuf of10_out;
2780 struct ofpbuf of10_in;
2781 struct ofpbuf ofpacts;
2786 /* Parse hex bytes. */
2787 ofpbuf_init(&of10_in, 0);
2788 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2789 ovs_fatal(0, "Trailing garbage in hex data");
2792 /* Convert to ofpacts. */
2793 ofpbuf_init(&ofpacts, 0);
2794 size = of10_in.size;
2795 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2797 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2798 ofpbuf_uninit(&ofpacts);
2799 ofpbuf_uninit(&of10_in);
2802 ofpbuf_push_uninit(&of10_in, size);
2804 /* Print cls_rule. */
2806 ds_put_cstr(&s, "actions=");
2807 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2811 /* Convert back to ofp10 actions and print differences from input. */
2812 ofpbuf_init(&of10_out, 0);
2813 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2815 print_differences("", of10_in.data, of10_in.size,
2816 of10_out.data, of10_out.size);
2819 ofpbuf_uninit(&ofpacts);
2820 ofpbuf_uninit(&of10_in);
2821 ofpbuf_uninit(&of10_out);
2826 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2827 * bytes from stdin, converts them to cls_rules, prints them as strings on
2828 * stdout, and then converts them back to hex bytes and prints any differences
2831 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2832 * values are ignored in the input and will be set to zero when OVS converts
2833 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2834 * it does the conversion to hex, to ensure that in fact they are ignored. */
2836 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2843 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2844 struct ofpbuf match_in, match_expout;
2845 struct ofp10_match match_out;
2846 struct ofp10_match match_normal;
2850 /* Parse hex bytes to use for expected output. */
2852 ds_put_cstr(&expout, ds_cstr(&in));
2853 for (p = ds_cstr(&expout); *p; p++) {
2858 ofpbuf_init(&match_expout, 0);
2859 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2860 ovs_fatal(0, "Trailing garbage in hex data");
2862 if (match_expout.size != sizeof(struct ofp10_match)) {
2863 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2864 match_expout.size, sizeof(struct ofp10_match));
2867 /* Parse hex bytes for input. */
2868 for (p = ds_cstr(&in); *p; p++) {
2870 *p = "0123456789abcdef"[random_uint32() & 0xf];
2873 ofpbuf_init(&match_in, 0);
2874 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2875 ovs_fatal(0, "Trailing garbage in hex data");
2877 if (match_in.size != sizeof(struct ofp10_match)) {
2878 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2879 match_in.size, sizeof(struct ofp10_match));
2882 /* Convert to cls_rule and print. */
2883 ofputil_match_from_ofp10_match(match_in.data, &match);
2884 match_print(&match);
2886 /* Convert back to ofp10_match and print differences from input. */
2887 ofputil_match_to_ofp10_match(&match, &match_out);
2888 print_differences("", match_expout.data, match_expout.size,
2889 &match_out, sizeof match_out);
2891 /* Normalize, then convert and compare again. */
2892 ofputil_normalize_match(&match);
2893 ofputil_match_to_ofp10_match(&match, &match_normal);
2894 print_differences("normal: ", &match_out, sizeof match_out,
2895 &match_normal, sizeof match_normal);
2898 ofpbuf_uninit(&match_in);
2899 ofpbuf_uninit(&match_expout);
2902 ds_destroy(&expout);
2905 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2906 * bytes from stdin, converts them to "struct match"es, prints them as strings
2907 * on stdout, and then converts them back to hex bytes and prints any
2908 * differences from the input. */
2910 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2915 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2916 struct ofpbuf match_in;
2917 struct ofp11_match match_out;
2921 /* Parse hex bytes. */
2922 ofpbuf_init(&match_in, 0);
2923 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2924 ovs_fatal(0, "Trailing garbage in hex data");
2926 if (match_in.size != sizeof(struct ofp11_match)) {
2927 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2928 match_in.size, sizeof(struct ofp11_match));
2931 /* Convert to match. */
2932 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2934 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2935 ofpbuf_uninit(&match_in);
2940 match_print(&match);
2942 /* Convert back to ofp11_match and print differences from input. */
2943 ofputil_match_to_ofp11_match(&match, &match_out);
2945 print_differences("", match_in.data, match_in.size,
2946 &match_out, sizeof match_out);
2949 ofpbuf_uninit(&match_in);
2954 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2955 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2956 * on stdout, and then converts them back to hex bytes and prints any
2957 * differences from the input. */
2959 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2964 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2965 struct ofpbuf of11_out;
2966 struct ofpbuf of11_in;
2967 struct ofpbuf ofpacts;
2972 /* Parse hex bytes. */
2973 ofpbuf_init(&of11_in, 0);
2974 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2975 ovs_fatal(0, "Trailing garbage in hex data");
2978 /* Convert to ofpacts. */
2979 ofpbuf_init(&ofpacts, 0);
2980 size = of11_in.size;
2981 error = ofpacts_pull_openflow11_actions(&of11_in, OFP11_VERSION,
2982 of11_in.size, &ofpacts);
2984 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2985 ofpbuf_uninit(&ofpacts);
2986 ofpbuf_uninit(&of11_in);
2989 ofpbuf_push_uninit(&of11_in, size);
2991 /* Print cls_rule. */
2993 ds_put_cstr(&s, "actions=");
2994 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2998 /* Convert back to ofp11 actions and print differences from input. */
2999 ofpbuf_init(&of11_out, 0);
3000 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
3002 print_differences("", of11_in.data, of11_in.size,
3003 of11_out.data, of11_out.size);
3006 ofpbuf_uninit(&ofpacts);
3007 ofpbuf_uninit(&of11_in);
3008 ofpbuf_uninit(&of11_out);
3013 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3014 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3015 * them as strings on stdout, and then converts them back to hex bytes and
3016 * prints any differences from the input. */
3018 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3023 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3024 struct ofpbuf of11_out;
3025 struct ofpbuf of11_in;
3026 struct ofpbuf ofpacts;
3030 const char *table_id;
3033 /* Parse table_id separated with the follow-up instructions by ",", if
3035 instructions = ds_cstr(&in);
3037 if (strstr(instructions, ",")) {
3038 table_id = strsep(&instructions, ",");
3041 /* Parse hex bytes. */
3042 ofpbuf_init(&of11_in, 0);
3043 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3044 ovs_fatal(0, "Trailing garbage in hex data");
3047 /* Convert to ofpacts. */
3048 ofpbuf_init(&ofpacts, 0);
3049 size = of11_in.size;
3050 error = ofpacts_pull_openflow11_instructions(&of11_in, OFP11_VERSION,
3051 of11_in.size, &ofpacts);
3053 /* Verify actions, enforce consistency. */
3055 memset(&flow, 0, sizeof flow);
3056 error = ofpacts_check(ofpacts.data, ofpacts.size, &flow, OFPP_MAX,
3057 table_id ? atoi(table_id) : 0, true);
3060 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3061 ofpbuf_uninit(&ofpacts);
3062 ofpbuf_uninit(&of11_in);
3065 ofpbuf_push_uninit(&of11_in, size);
3067 /* Print cls_rule. */
3069 ds_put_cstr(&s, "actions=");
3070 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3074 /* Convert back to ofp11 instructions and print differences from
3076 ofpbuf_init(&of11_out, 0);
3077 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
3080 print_differences("", of11_in.data, of11_in.size,
3081 of11_out.data, of11_out.size);
3084 ofpbuf_uninit(&ofpacts);
3085 ofpbuf_uninit(&of11_in);
3086 ofpbuf_uninit(&of11_out);
3091 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3093 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3097 pcap = pcap_open(argv[1], "rb");
3099 ovs_fatal(errno, "%s: open failed", argv[1]);
3103 struct ofpbuf *packet;
3107 error = pcap_read(pcap, &packet);
3111 ovs_fatal(error, "%s: read failed", argv[1]);
3114 flow_extract(packet, 0, 0, NULL, NULL, &flow);
3115 flow_print(stdout, &flow);
3117 ofpbuf_delete(packet);
3121 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3122 * mask values to and from various formats and prints the results. */
3124 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3129 struct ofputil_flow_mod fm;
3132 struct match nxm_match;
3136 struct ofp10_match of10_raw;
3137 struct match of10_match;
3139 struct ofp11_match of11_raw;
3140 struct match of11_match;
3145 enum ofputil_protocol usable_protocols; /* Unused for now. */
3147 match_init_catchall(&match);
3148 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3149 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3151 /* Convert to and from string. */
3152 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3153 printf("%s -> ", string_s);
3155 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols,
3156 !(allowed_protocols & OFPUTIL_P_OF10_ANY));
3158 ovs_fatal(0, "%s", error_s);
3160 printf("%04"PRIx16"/%04"PRIx16"\n",
3161 ntohs(fm.match.flow.vlan_tci),
3162 ntohs(fm.match.wc.masks.vlan_tci));
3165 /* Convert to and from NXM. */
3166 ofpbuf_init(&nxm, 0);
3167 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3168 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3169 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3170 printf("NXM: %s -> ", nxm_s);
3172 printf("%s\n", ofperr_to_string(error));
3174 printf("%04"PRIx16"/%04"PRIx16"\n",
3175 ntohs(nxm_match.flow.vlan_tci),
3176 ntohs(nxm_match.wc.masks.vlan_tci));
3179 ofpbuf_uninit(&nxm);
3181 /* Convert to and from OXM. */
3182 ofpbuf_init(&nxm, 0);
3183 nxm_match_len = oxm_put_match(&nxm, &match);
3184 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3185 error = oxm_pull_match(&nxm, &nxm_match);
3186 printf("OXM: %s -> ", nxm_s);
3188 printf("%s\n", ofperr_to_string(error));
3190 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3191 (VLAN_VID_MASK | VLAN_CFI);
3192 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3193 (VLAN_VID_MASK | VLAN_CFI);
3195 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3196 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3197 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3203 ofpbuf_uninit(&nxm);
3205 /* Convert to and from OpenFlow 1.0. */
3206 ofputil_match_to_ofp10_match(&match, &of10_raw);
3207 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3208 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3209 ntohs(of10_raw.dl_vlan),
3210 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3211 of10_raw.dl_vlan_pcp,
3212 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3213 ntohs(of10_match.flow.vlan_tci),
3214 ntohs(of10_match.wc.masks.vlan_tci));
3216 /* Convert to and from OpenFlow 1.1. */
3217 ofputil_match_to_ofp11_match(&match, &of11_raw);
3218 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3219 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3220 ntohs(of11_raw.dl_vlan),
3221 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3222 of11_raw.dl_vlan_pcp,
3223 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3224 ntohs(of11_match.flow.vlan_tci),
3225 ntohs(of11_match.wc.masks.vlan_tci));
3228 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3231 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3236 error = ofperr_from_name(argv[1]);
3238 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3241 for (version = 0; version <= UINT8_MAX; version++) {
3242 const char *name = ofperr_domain_get_name(version);
3244 int vendor = ofperr_get_vendor(error, version);
3245 int type = ofperr_get_type(error, version);
3246 int code = ofperr_get_code(error, version);
3248 if (vendor != -1 || type != -1 || code != -1) {
3249 printf("%s: vendor %#x, type %d, code %d\n",
3250 name, vendor, type, code);
3256 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3257 * error named ENUM and prints the error reply in hex. */
3259 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3261 const struct ofp_header *oh;
3262 struct ofpbuf request, *reply;
3265 error = ofperr_from_name(argv[1]);
3267 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3270 ofpbuf_init(&request, 0);
3271 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3272 ovs_fatal(0, "Trailing garbage in hex data");
3274 if (request.size < sizeof(struct ofp_header)) {
3275 ovs_fatal(0, "Request too short");
3279 if (request.size != ntohs(oh->length)) {
3280 ovs_fatal(0, "Request size inconsistent");
3283 reply = ofperr_encode_reply(error, request.data);
3284 ofpbuf_uninit(&request);
3286 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3287 ofpbuf_delete(reply);
3290 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3291 * binary data, interpreting them as an OpenFlow message, and prints the
3292 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3294 ofctl_ofp_print(int argc, char *argv[])
3296 struct ofpbuf packet;
3298 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3299 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3300 ovs_fatal(0, "trailing garbage following hex bytes");
3302 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
3303 ofpbuf_uninit(&packet);
3306 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3307 * and dumps each message in hex. */
3309 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3311 uint32_t bitmap = strtol(argv[1], NULL, 0);
3312 struct ofpbuf *hello;
3314 hello = ofputil_encode_hello(bitmap);
3315 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3316 ofp_print(stdout, hello->data, hello->size, verbosity);
3317 ofpbuf_delete(hello);
3320 static const struct command all_commands[] = {
3321 { "show", 1, 1, ofctl_show },
3322 { "monitor", 1, 3, ofctl_monitor },
3323 { "snoop", 1, 1, ofctl_snoop },
3324 { "dump-desc", 1, 1, ofctl_dump_desc },
3325 { "dump-tables", 1, 1, ofctl_dump_tables },
3326 { "dump-flows", 1, 2, ofctl_dump_flows },
3327 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3328 { "queue-stats", 1, 3, ofctl_queue_stats },
3329 { "add-flow", 2, 2, ofctl_add_flow },
3330 { "add-flows", 2, 2, ofctl_add_flows },
3331 { "mod-flows", 2, 2, ofctl_mod_flows },
3332 { "del-flows", 1, 2, ofctl_del_flows },
3333 { "replace-flows", 2, 2, ofctl_replace_flows },
3334 { "diff-flows", 2, 2, ofctl_diff_flows },
3335 { "add-meter", 2, 2, ofctl_add_meter },
3336 { "mod-meter", 2, 2, ofctl_mod_meter },
3337 { "del-meter", 2, 2, ofctl_del_meters },
3338 { "del-meters", 1, 1, ofctl_del_meters },
3339 { "dump-meter", 2, 2, ofctl_dump_meters },
3340 { "dump-meters", 1, 1, ofctl_dump_meters },
3341 { "meter-stats", 1, 2, ofctl_meter_stats },
3342 { "meter-features", 1, 1, ofctl_meter_features },
3343 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3344 { "dump-ports", 1, 2, ofctl_dump_ports },
3345 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3346 { "mod-port", 3, 3, ofctl_mod_port },
3347 { "mod-table", 3, 3, ofctl_mod_table },
3348 { "get-frags", 1, 1, ofctl_get_frags },
3349 { "set-frags", 2, 2, ofctl_set_frags },
3350 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3351 { "probe", 1, 1, ofctl_probe },
3352 { "ping", 1, 2, ofctl_ping },
3353 { "benchmark", 3, 3, ofctl_benchmark },
3355 { "add-group", 1, 2, ofctl_add_group },
3356 { "add-groups", 1, 2, ofctl_add_groups },
3357 { "mod-group", 1, 2, ofctl_mod_group },
3358 { "del-groups", 1, 2, ofctl_del_groups },
3359 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3360 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3361 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3362 { "help", 0, INT_MAX, ofctl_help },
3364 /* Undocumented commands for testing. */
3365 { "parse-flow", 1, 1, ofctl_parse_flow },
3366 { "parse-flows", 1, 1, ofctl_parse_flows },
3367 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3368 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3369 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3370 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3371 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3372 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3373 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3374 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3375 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3376 { "check-vlan", 2, 2, ofctl_check_vlan },
3377 { "print-error", 1, 1, ofctl_print_error },
3378 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3379 { "ofp-print", 1, 2, ofctl_ofp_print },
3380 { "encode-hello", 1, 1, ofctl_encode_hello },
3382 { NULL, 0, 0, NULL },
3385 static const struct command *get_all_commands(void)
3387 return all_commands;