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 "poll-loop.h"
55 #include "stream-ssl.h"
56 #include "socket-util.h"
62 #include "meta-flow.h"
65 VLOG_DEFINE_THIS_MODULE(ofctl);
67 /* --strict: Use strict matching for flow mod commands? Additionally governs
68 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
72 /* --readd: If true, on replace-flows, re-add even flows that have not changed
73 * (to reset flow counters). */
76 /* -F, --flow-format: Allowed protocols. By default, any protocol is
78 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
80 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
81 * commands. Either one of NXPIF_* to force a particular packet_in format, or
82 * -1 to let ovs-ofctl choose the default. */
83 static int preferred_packet_in_format = -1;
85 /* -m, --more: Additional verbosity for ofp-print functions. */
88 /* --timestamp: Print a timestamp before each received packet on "monitor" and
90 static bool timestamp;
92 /* --sort, --rsort: Sort order. */
93 enum sort_order { SORT_ASC, SORT_DESC };
94 struct sort_criterion {
95 const struct mf_field *field; /* NULL means to sort by priority. */
96 enum sort_order order;
98 static struct sort_criterion *criteria;
99 static size_t n_criteria, allocated_criteria;
101 static const struct command *get_all_commands(void);
103 static void usage(void) NO_RETURN;
104 static void parse_options(int argc, char *argv[]);
106 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
107 struct ofpbuf **replyp,
108 struct ofputil_flow_stats *,
109 struct ofpbuf *ofpacts);
111 main(int argc, char *argv[])
113 set_program_name(argv[0]);
114 parse_options(argc, argv);
115 signal(SIGPIPE, SIG_IGN);
116 run_command(argc - optind, argv + optind, get_all_commands());
121 add_sort_criterion(enum sort_order order, const char *field)
123 struct sort_criterion *sc;
125 if (n_criteria >= allocated_criteria) {
126 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
129 sc = &criteria[n_criteria++];
130 if (!field || !strcasecmp(field, "priority")) {
133 sc->field = mf_from_name(field);
135 ovs_fatal(0, "%s: unknown field name", field);
142 parse_options(int argc, char *argv[])
145 OPT_STRICT = UCHAR_MAX + 1,
151 OFP_VERSION_OPTION_ENUMS,
154 static const struct option long_options[] = {
155 {"timeout", required_argument, NULL, 't'},
156 {"strict", no_argument, NULL, OPT_STRICT},
157 {"readd", no_argument, NULL, OPT_READD},
158 {"flow-format", required_argument, NULL, 'F'},
159 {"packet-in-format", required_argument, NULL, 'P'},
160 {"more", no_argument, NULL, 'm'},
161 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
162 {"sort", optional_argument, NULL, OPT_SORT},
163 {"rsort", optional_argument, NULL, OPT_RSORT},
164 {"help", no_argument, NULL, 'h'},
166 OFP_VERSION_LONG_OPTIONS,
168 STREAM_SSL_LONG_OPTIONS,
171 char *short_options = long_options_to_short_options(long_options);
173 enum ofputil_protocol version_protocols;
176 unsigned long int timeout;
179 c = getopt_long(argc, argv, short_options, long_options, NULL);
186 timeout = strtoul(optarg, NULL, 10);
188 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
196 allowed_protocols = ofputil_protocols_from_string(optarg);
197 if (!allowed_protocols) {
198 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
203 preferred_packet_in_format =
204 ofputil_packet_in_format_from_string(optarg);
205 if (preferred_packet_in_format < 0) {
206 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
230 add_sort_criterion(SORT_ASC, optarg);
234 add_sort_criterion(SORT_DESC, optarg);
237 DAEMON_OPTION_HANDLERS
238 OFP_VERSION_OPTION_HANDLERS
240 STREAM_SSL_OPTION_HANDLERS
251 /* Always do a final sort pass based on priority. */
252 add_sort_criterion(SORT_DESC, "priority");
257 versions = get_allowed_ofp_versions();
258 version_protocols = ofputil_protocols_from_version_bitmap(versions);
259 if (!(allowed_protocols & version_protocols)) {
260 char *protocols = ofputil_protocols_to_string(allowed_protocols);
261 struct ds version_s = DS_EMPTY_INITIALIZER;
263 ofputil_format_version_bitmap_names(&version_s, versions);
264 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
265 "any of the enabled flow formats (%s). (Use -O to enable "
266 "additional OpenFlow versions or -F to enable additional "
267 "flow formats.)", ds_cstr(&version_s), protocols);
269 allowed_protocols &= version_protocols;
270 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
277 printf("%s: OpenFlow switch management utility\n"
278 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
279 "\nFor OpenFlow switches:\n"
280 " show SWITCH show OpenFlow information\n"
281 " dump-desc SWITCH print switch description\n"
282 " dump-tables SWITCH print table stats\n"
283 " mod-port SWITCH IFACE ACT modify port behavior\n"
284 " mod-table SWITCH MOD modify flow table behavior\n"
285 " get-frags SWITCH print fragment handling behavior\n"
286 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
287 " dump-ports SWITCH [PORT] print port statistics\n"
288 " dump-ports-desc SWITCH print port descriptions\n"
289 " dump-flows SWITCH print all flow entries\n"
290 " dump-flows SWITCH FLOW print matching FLOWs\n"
291 " dump-aggregate SWITCH print aggregate flow statistics\n"
292 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
293 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
294 " add-flow SWITCH FLOW add flow described by FLOW\n"
295 " add-flows SWITCH FILE add flows from FILE\n"
296 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
297 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
298 " replace-flows SWITCH FILE replace flows with those in FILE\n"
299 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
300 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
301 " execute ACTIONS on PACKET\n"
302 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
303 " print packets received from SWITCH\n"
304 " snoop SWITCH snoop on SWITCH and its controller\n"
305 " add-group SWITCH GROUP add group described by GROUP\n"
306 " add-group SWITCH FILE add group from FILE\n"
307 " mod-group SWITCH GROUP modify specific group\n"
308 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
309 " dump-group-features SWITCH print group features\n"
310 " dump-groups SWITCH print group description\n"
311 " dump-group-stats SWITCH [GROUP] print group statistics\n"
312 "\nFor OpenFlow switches and controllers:\n"
313 " probe TARGET probe whether TARGET is up\n"
314 " ping TARGET [N] latency of N-byte echos\n"
315 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
316 "SWITCH or TARGET is an active OpenFlow connection method.\n"
317 "\nOther commands:\n"
318 " ofp-parse FILE print messages read from FILE\n",
319 program_name, program_name);
320 vconn_usage(true, false, false);
324 printf("\nOther options:\n"
325 " --strict use strict match for flow commands\n"
326 " --readd replace flows that haven't changed\n"
327 " -F, --flow-format=FORMAT force particular flow format\n"
328 " -P, --packet-in-format=FRMT force particular packet in format\n"
329 " -m, --more be more verbose printing OpenFlow\n"
330 " --timestamp (monitor, snoop) print timestamps\n"
331 " -t, --timeout=SECS give up after SECS seconds\n"
332 " --sort[=field] sort in ascending order\n"
333 " --rsort[=field] sort in descending order\n"
334 " -h, --help display this help message\n"
335 " -V, --version display version information\n");
340 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
341 const char *argv[] OVS_UNUSED, void *exiting_)
343 bool *exiting = exiting_;
345 unixctl_command_reply(conn, NULL);
348 static void run(int retval, const char *message, ...)
352 run(int retval, const char *message, ...)
357 va_start(args, message);
358 ovs_fatal_valist(retval, message, args);
362 /* Generic commands. */
365 open_vconn_socket(const char *name, struct vconn **vconnp)
367 char *vconn_name = xasprintf("unix:%s", name);
370 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
372 if (error && error != ENOENT) {
373 ovs_fatal(0, "%s: failed to open socket (%s)", name,
374 ovs_strerror(error));
381 enum open_target { MGMT, SNOOP };
383 static enum ofputil_protocol
384 open_vconn__(const char *name, enum open_target target,
385 struct vconn **vconnp)
387 const char *suffix = target == MGMT ? "mgmt" : "snoop";
388 char *datapath_name, *datapath_type, *socket_name;
389 enum ofputil_protocol protocol;
394 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
396 ofproto_parse_name(name, &datapath_name, &datapath_type);
397 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
401 if (strchr(name, ':')) {
402 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
403 "connecting to %s", name);
404 } else if (!open_vconn_socket(name, vconnp)) {
406 } else if (!open_vconn_socket(bridge_path, vconnp)) {
408 } else if (!open_vconn_socket(socket_name, vconnp)) {
411 ovs_fatal(0, "%s is not a bridge or a socket", name);
414 if (target == SNOOP) {
415 vconn_set_recv_any_version(*vconnp);
421 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
422 error = vconn_connect_block(*vconnp);
424 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
425 ovs_strerror(error));
428 ofp_version = vconn_get_version(*vconnp);
429 protocol = ofputil_protocol_from_ofp_version(ofp_version);
431 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
437 static enum ofputil_protocol
438 open_vconn(const char *name, struct vconn **vconnp)
440 return open_vconn__(name, MGMT, vconnp);
444 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
446 ofpmsg_update_length(buffer);
447 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
451 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
453 struct ofpbuf *reply;
455 ofpmsg_update_length(request);
456 run(vconn_transact(vconn, request, &reply), "talking to %s",
457 vconn_get_name(vconn));
458 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
459 ofpbuf_delete(reply);
463 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
465 struct ofpbuf *request;
468 open_vconn(vconn_name, &vconn);
469 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
470 dump_transaction(vconn, request);
475 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
477 const struct ofp_header *request_oh = request->data;
478 ovs_be32 send_xid = request_oh->xid;
479 enum ofpraw request_raw;
480 enum ofpraw reply_raw;
483 ofpraw_decode_partial(&request_raw, request->data, request->size);
484 reply_raw = ofpraw_stats_request_to_reply(request_raw,
485 request_oh->version);
487 send_openflow_buffer(vconn, request);
490 struct ofpbuf *reply;
492 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
493 recv_xid = ((struct ofp_header *) reply->data)->xid;
494 if (send_xid == recv_xid) {
497 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
499 ofpraw_decode(&raw, reply->data);
500 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
502 } else if (raw == reply_raw) {
503 done = !ofpmp_more(reply->data);
505 ovs_fatal(0, "received bad reply: %s",
506 ofp_to_string(reply->data, reply->size,
510 VLOG_DBG("received reply with xid %08"PRIx32" "
511 "!= expected %08"PRIx32, recv_xid, send_xid);
513 ofpbuf_delete(reply);
518 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
520 struct ofpbuf *request;
523 open_vconn(vconn_name, &vconn);
524 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
525 dump_stats_transaction(vconn, request);
529 /* Sends all of the 'requests', which should be requests that only have replies
530 * if an error occurs, and waits for them to succeed or fail. If an error does
531 * occur, prints it and exits with an error.
533 * Destroys all of the 'requests'. */
535 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
537 struct ofpbuf *request, *reply;
539 LIST_FOR_EACH (request, list_node, requests) {
540 ofpmsg_update_length(request);
543 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
544 "talking to %s", vconn_get_name(vconn));
546 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
549 ofpbuf_delete(reply);
552 /* Sends 'request', which should be a request that only has a reply if an error
553 * occurs, and waits for it to succeed or fail. If an error does occur, prints
554 * it and exits with an error.
556 * Destroys 'request'. */
558 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
560 struct list requests;
562 list_init(&requests);
563 list_push_back(&requests, &request->list_node);
564 transact_multiple_noreply(vconn, &requests);
568 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
570 struct ofp_switch_config *config;
571 struct ofpbuf *request;
572 struct ofpbuf *reply;
575 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
576 vconn_get_version(vconn), 0);
577 run(vconn_transact(vconn, request, &reply),
578 "talking to %s", vconn_get_name(vconn));
580 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
581 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
584 config = ofpbuf_pull(reply, sizeof *config);
587 ofpbuf_delete(reply);
591 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
593 struct ofpbuf *request;
595 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
596 ofpbuf_put(request, config, sizeof *config);
598 transact_noreply(vconn, request);
602 ofctl_show(int argc OVS_UNUSED, char *argv[])
604 const char *vconn_name = argv[1];
606 struct ofpbuf *request;
607 struct ofpbuf *reply;
610 open_vconn(vconn_name, &vconn);
611 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
612 vconn_get_version(vconn), 0);
613 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
615 trunc = ofputil_switch_features_ports_trunc(reply);
616 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
618 ofpbuf_delete(reply);
621 /* The Features Reply may not contain all the ports, so send a
622 * Port Description stats request, which doesn't have size
624 dump_trivial_stats_transaction(vconn_name,
625 OFPRAW_OFPST_PORT_DESC_REQUEST);
627 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
632 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
634 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
638 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
640 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
644 fetch_port_by_features(const char *vconn_name,
645 const char *port_name, ofp_port_t port_no,
646 struct ofputil_phy_port *pp, bool *trunc)
648 struct ofputil_switch_features features;
649 const struct ofp_header *oh;
650 struct ofpbuf *request, *reply;
657 /* Fetch the switch's ofp_switch_features. */
658 open_vconn(vconn_name, &vconn);
659 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
660 vconn_get_version(vconn), 0);
661 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
665 if (ofptype_decode(&type, reply->data)
666 || type != OFPTYPE_FEATURES_REPLY) {
667 ovs_fatal(0, "%s: received bad features reply", vconn_name);
671 if (ofputil_switch_features_ports_trunc(reply)) {
676 error = ofputil_decode_switch_features(oh, &features, &b);
678 ovs_fatal(0, "%s: failed to decode features reply (%s)",
679 vconn_name, ofperr_to_string(error));
682 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
683 if (port_no != OFPP_NONE
684 ? port_no == pp->port_no
685 : !strcmp(pp->name, port_name)) {
692 ofpbuf_delete(reply);
697 fetch_port_by_stats(const char *vconn_name,
698 const char *port_name, ofp_port_t port_no,
699 struct ofputil_phy_port *pp)
701 struct ofpbuf *request;
707 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
708 send_xid = ((struct ofp_header *) request->data)->xid;
710 open_vconn(vconn_name, &vconn);
711 send_openflow_buffer(vconn, request);
714 struct ofpbuf *reply;
716 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
717 recv_xid = ((struct ofp_header *) reply->data)->xid;
718 if (send_xid == recv_xid) {
719 struct ofp_header *oh = reply->data;
724 ofpbuf_use_const(&b, oh, ntohs(oh->length));
725 if (ofptype_pull(&type, &b)
726 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
727 ovs_fatal(0, "received bad reply: %s",
728 ofp_to_string(reply->data, reply->size,
732 flags = ofpmp_flags(oh);
733 done = !(flags & OFPSF_REPLY_MORE);
736 /* We've already found the port, but we need to drain
737 * the queue of any other replies for this request. */
741 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
742 if (port_no != OFPP_NONE ? port_no == pp->port_no
743 : !strcmp(pp->name, port_name)) {
749 VLOG_DBG("received reply with xid %08"PRIx32" "
750 "!= expected %08"PRIx32, recv_xid, send_xid);
752 ofpbuf_delete(reply);
760 str_to_ofp(const char *s, ofp_port_t *ofp_port)
765 ret = str_to_uint(s, 10, &port_);
766 *ofp_port = u16_to_ofp(port_);
770 /* Opens a connection to 'vconn_name', fetches the port structure for
771 * 'port_name' (which may be a port name or number), and copies it into
774 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
775 struct ofputil_phy_port *pp)
781 /* Try to interpret the argument as a port number. */
782 if (!str_to_ofp(port_name, &port_no)) {
786 /* Try to find the port based on the Features Reply. If it looks
787 * like the results may be truncated, then use the Port Description
788 * stats message introduced in OVS 1.7. */
789 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
792 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
796 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
800 /* Returns the port number corresponding to 'port_name' (which may be a port
801 * name or number) within the switch 'vconn_name'. */
803 str_to_port_no(const char *vconn_name, const char *port_name)
807 if (ofputil_port_from_string(port_name, &port_no)) {
810 struct ofputil_phy_port pp;
812 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
818 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
819 enum ofputil_protocol *cur)
822 struct ofpbuf *request, *reply;
823 enum ofputil_protocol next;
825 request = ofputil_encode_set_protocol(*cur, want, &next);
830 run(vconn_transact_noreply(vconn, request, &reply),
831 "talking to %s", vconn_get_name(vconn));
833 char *s = ofp_to_string(reply->data, reply->size, 2);
834 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
835 vconn_get_name(vconn), s);
837 ofpbuf_delete(reply);
845 static enum ofputil_protocol
846 set_protocol_for_flow_dump(struct vconn *vconn,
847 enum ofputil_protocol cur_protocol,
848 enum ofputil_protocol usable_protocols)
853 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
854 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
855 if (f & usable_protocols & allowed_protocols
856 && try_set_protocol(vconn, f, &cur_protocol)) {
861 usable_s = ofputil_protocols_to_string(usable_protocols);
862 if (usable_protocols & allowed_protocols) {
863 ovs_fatal(0, "switch does not support any of the usable flow "
864 "formats (%s)", usable_s);
866 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
867 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
868 "allowed flow formats (%s)", usable_s, allowed_s);
872 static struct vconn *
873 prepare_dump_flows(int argc, char *argv[], bool aggregate,
874 struct ofpbuf **requestp)
876 enum ofputil_protocol usable_protocols, protocol;
877 struct ofputil_flow_stats_request fsr;
881 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
882 argc > 2 ? argv[2] : "",
885 ovs_fatal(0, "%s", error);
888 protocol = open_vconn(argv[1], &vconn);
889 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
890 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
895 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
897 struct ofpbuf *request;
900 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
901 dump_stats_transaction(vconn, request);
906 compare_flows(const void *afs_, const void *bfs_)
908 const struct ofputil_flow_stats *afs = afs_;
909 const struct ofputil_flow_stats *bfs = bfs_;
910 const struct match *a = &afs->match;
911 const struct match *b = &bfs->match;
912 const struct sort_criterion *sc;
914 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
915 const struct mf_field *f = sc->field;
919 unsigned int a_pri = afs->priority;
920 unsigned int b_pri = bfs->priority;
921 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
925 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
926 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
928 /* Skip the test for sc->order, so that missing fields always
929 * sort to the end whether we're sorting in ascending or
930 * descending order. */
933 union mf_value aval, bval;
935 mf_get_value(f, &a->flow, &aval);
936 mf_get_value(f, &b->flow, &bval);
937 ret = memcmp(&aval, &bval, f->n_bytes);
942 return sc->order == SORT_ASC ? ret : -ret;
950 ofctl_dump_flows(int argc, char *argv[])
953 return ofctl_dump_flows__(argc, argv, false);
955 struct ofputil_flow_stats *fses;
956 size_t n_fses, allocated_fses;
957 struct ofpbuf *request;
958 struct ofpbuf ofpacts;
959 struct ofpbuf *reply;
965 vconn = prepare_dump_flows(argc, argv, false, &request);
966 send_xid = ((struct ofp_header *) request->data)->xid;
967 send_openflow_buffer(vconn, request);
970 n_fses = allocated_fses = 0;
972 ofpbuf_init(&ofpacts, 0);
974 struct ofputil_flow_stats *fs;
976 if (n_fses >= allocated_fses) {
977 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
981 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
985 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
988 ofpbuf_uninit(&ofpacts);
990 qsort(fses, n_fses, sizeof *fses, compare_flows);
993 for (i = 0; i < n_fses; i++) {
995 ofp_print_flow_stats(&s, &fses[i]);
1000 for (i = 0; i < n_fses; i++) {
1001 free(fses[i].ofpacts);
1010 ofctl_dump_aggregate(int argc, char *argv[])
1012 return ofctl_dump_flows__(argc, argv, true);
1016 ofctl_queue_stats(int argc, char *argv[])
1018 struct ofpbuf *request;
1019 struct vconn *vconn;
1020 struct ofputil_queue_stats_request oqs;
1022 open_vconn(argv[1], &vconn);
1024 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1025 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1027 oqs.port_no = OFPP_ANY;
1029 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1030 oqs.queue_id = atoi(argv[3]);
1032 oqs.queue_id = OFPQ_ALL;
1035 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1036 dump_stats_transaction(vconn, request);
1040 static enum ofputil_protocol
1041 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1042 enum ofputil_protocol usable_protocols)
1044 enum ofputil_protocol cur_protocol;
1048 if (!(usable_protocols & allowed_protocols)) {
1049 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1050 usable_s = ofputil_protocols_to_string(usable_protocols);
1051 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1052 "allowed flow formats (%s)", usable_s, allowed_s);
1055 /* If the initial flow format is allowed and usable, keep it. */
1056 cur_protocol = open_vconn(remote, vconnp);
1057 if (usable_protocols & allowed_protocols & cur_protocol) {
1058 return cur_protocol;
1061 /* Otherwise try each flow format in turn. */
1062 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1063 enum ofputil_protocol f = 1 << i;
1065 if (f != cur_protocol
1066 && f & usable_protocols & allowed_protocols
1067 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1072 usable_s = ofputil_protocols_to_string(usable_protocols);
1073 ovs_fatal(0, "switch does not support any of the usable flow "
1074 "formats (%s)", usable_s);
1078 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1079 size_t n_fms, enum ofputil_protocol usable_protocols)
1081 enum ofputil_protocol protocol;
1082 struct vconn *vconn;
1085 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1087 for (i = 0; i < n_fms; i++) {
1088 struct ofputil_flow_mod *fm = &fms[i];
1090 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1097 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1099 enum ofputil_protocol usable_protocols;
1100 struct ofputil_flow_mod *fms = NULL;
1104 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1107 ovs_fatal(0, "%s", error);
1109 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1114 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1116 enum ofputil_protocol usable_protocols;
1118 if (argc > 2 && !strcmp(argv[2], "-")) {
1119 ofctl_flow_mod_file(argc, argv, command);
1121 struct ofputil_flow_mod fm;
1124 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1127 ovs_fatal(0, "%s", error);
1129 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1134 ofctl_add_flow(int argc, char *argv[])
1136 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1140 ofctl_add_flows(int argc, char *argv[])
1142 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1146 ofctl_mod_flows(int argc, char *argv[])
1148 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1152 ofctl_del_flows(int argc, char *argv[])
1154 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1158 set_packet_in_format(struct vconn *vconn,
1159 enum nx_packet_in_format packet_in_format)
1161 struct ofpbuf *spif;
1163 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1165 transact_noreply(vconn, spif);
1166 VLOG_DBG("%s: using user-specified packet in format %s",
1167 vconn_get_name(vconn),
1168 ofputil_packet_in_format_to_string(packet_in_format));
1172 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1174 struct ofp_switch_config config;
1175 enum ofp_config_flags flags;
1177 fetch_switch_config(vconn, &config);
1178 flags = ntohs(config.flags);
1179 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1180 /* Set the invalid ttl config. */
1181 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1183 config.flags = htons(flags);
1184 set_switch_config(vconn, &config);
1186 /* Then retrieve the configuration to see if it really took. OpenFlow
1187 * doesn't define error reporting for bad modes, so this is all we can
1189 fetch_switch_config(vconn, &config);
1190 flags = ntohs(config.flags);
1191 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1192 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1193 "switch probably doesn't support mode)");
1200 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1201 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1202 * an error message and stores NULL in '*msgp'. */
1204 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1206 struct ofp_header *oh;
1209 msg = ofpbuf_new(strlen(hex) / 2);
1212 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1214 return "Trailing garbage in hex data";
1217 if (msg->size < sizeof(struct ofp_header)) {
1219 return "Message too short for OpenFlow";
1223 if (msg->size != ntohs(oh->length)) {
1225 return "Message size does not match length in OpenFlow header";
1233 ofctl_send(struct unixctl_conn *conn, int argc,
1234 const char *argv[], void *vconn_)
1236 struct vconn *vconn = vconn_;
1243 for (i = 1; i < argc; i++) {
1244 const char *error_msg;
1248 error_msg = openflow_from_hex(argv[i], &msg);
1250 ds_put_format(&reply, "%s\n", error_msg);
1255 fprintf(stderr, "send: ");
1256 ofp_print(stderr, msg->data, msg->size, verbosity);
1258 error = vconn_send_block(vconn, msg);
1261 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1264 ds_put_cstr(&reply, "sent\n");
1269 unixctl_command_reply(conn, ds_cstr(&reply));
1271 unixctl_command_reply_error(conn, ds_cstr(&reply));
1276 struct barrier_aux {
1277 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1278 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1282 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1283 const char *argv[] OVS_UNUSED, void *aux_)
1285 struct barrier_aux *aux = aux_;
1290 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1294 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1295 error = vconn_send_block(aux->vconn, msg);
1298 unixctl_command_reply_error(conn, ovs_strerror(error));
1305 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1306 const char *argv[], void *aux OVS_UNUSED)
1310 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1312 unixctl_command_reply_error(conn, ovs_strerror(errno));
1317 dup2(fd, STDERR_FILENO);
1319 unixctl_command_reply(conn, NULL);
1323 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1324 const char *argv[] OVS_UNUSED, void *blocked_)
1326 bool *blocked = blocked_;
1330 unixctl_command_reply(conn, NULL);
1332 unixctl_command_reply(conn, "already blocking");
1337 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1338 const char *argv[] OVS_UNUSED, void *blocked_)
1340 bool *blocked = blocked_;
1344 unixctl_command_reply(conn, NULL);
1346 unixctl_command_reply(conn, "already unblocked");
1350 /* Prints to stdout all of the messages received on 'vconn'.
1352 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1353 * received on 'vconn'. */
1355 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1357 struct barrier_aux barrier_aux = { vconn, NULL };
1358 struct unixctl_server *server;
1359 bool exiting = false;
1360 bool blocked = false;
1363 daemon_save_fd(STDERR_FILENO);
1365 error = unixctl_server_create(NULL, &server);
1367 ovs_fatal(error, "failed to create unixctl server");
1369 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1370 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1372 unixctl_command_register("ofctl/barrier", "", 0, 0,
1373 ofctl_barrier, &barrier_aux);
1374 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1375 ofctl_set_output_file, NULL);
1377 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1378 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1381 daemonize_complete();
1387 unixctl_server_run(server);
1392 retval = vconn_recv(vconn, &b);
1393 if (retval == EAGAIN) {
1396 run(retval, "vconn_recv");
1399 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1400 time_wall_msec(), true);
1405 ofptype_decode(&type, b->data);
1406 ofp_print(stderr, b->data, b->size, verbosity + 2);
1408 switch ((int) type) {
1409 case OFPTYPE_BARRIER_REPLY:
1410 if (barrier_aux.conn) {
1411 unixctl_command_reply(barrier_aux.conn, NULL);
1412 barrier_aux.conn = NULL;
1416 case OFPTYPE_ECHO_REQUEST:
1417 if (reply_to_echo_requests) {
1418 struct ofpbuf *reply;
1420 reply = make_echo_reply(b->data);
1421 retval = vconn_send_block(vconn, reply);
1423 ovs_fatal(retval, "failed to send echo reply");
1436 vconn_run_wait(vconn);
1438 vconn_recv_wait(vconn);
1440 unixctl_server_wait(server);
1444 unixctl_server_destroy(server);
1448 ofctl_monitor(int argc, char *argv[])
1450 struct vconn *vconn;
1452 enum ofputil_protocol usable_protocols;
1454 open_vconn(argv[1], &vconn);
1455 for (i = 2; i < argc; i++) {
1456 const char *arg = argv[i];
1458 if (isdigit((unsigned char) *arg)) {
1459 struct ofp_switch_config config;
1461 fetch_switch_config(vconn, &config);
1462 config.miss_send_len = htons(atoi(arg));
1463 set_switch_config(vconn, &config);
1464 } else if (!strcmp(arg, "invalid_ttl")) {
1465 monitor_set_invalid_ttl_to_controller(vconn);
1466 } else if (!strncmp(arg, "watch:", 6)) {
1467 struct ofputil_flow_monitor_request fmr;
1471 error = parse_flow_monitor_request(&fmr, arg + 6,
1474 ovs_fatal(0, "%s", error);
1477 msg = ofpbuf_new(0);
1478 ofputil_append_flow_monitor_request(&fmr, msg);
1479 dump_stats_transaction(vconn, msg);
1481 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1485 if (preferred_packet_in_format >= 0) {
1486 set_packet_in_format(vconn, preferred_packet_in_format);
1488 enum ofp_version version = vconn_get_version(vconn);
1491 case OFP10_VERSION: {
1492 struct ofpbuf *spif, *reply;
1494 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1496 run(vconn_transact_noreply(vconn, spif, &reply),
1497 "talking to %s", vconn_get_name(vconn));
1499 char *s = ofp_to_string(reply->data, reply->size, 2);
1500 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1501 " replied: %s. Falling back to the switch default.",
1502 vconn_get_name(vconn), s);
1504 ofpbuf_delete(reply);
1517 monitor_vconn(vconn, true);
1521 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1523 struct vconn *vconn;
1525 open_vconn__(argv[1], SNOOP, &vconn);
1526 monitor_vconn(vconn, false);
1530 ofctl_dump_ports(int argc, char *argv[])
1532 struct ofpbuf *request;
1533 struct vconn *vconn;
1536 open_vconn(argv[1], &vconn);
1537 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1538 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1539 dump_stats_transaction(vconn, request);
1544 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1546 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1550 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1552 struct ofpbuf *request;
1553 struct vconn *vconn;
1554 struct ofpbuf *reply;
1556 open_vconn(argv[1], &vconn);
1557 request = make_echo_request(vconn_get_version(vconn));
1558 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1559 if (reply->size != sizeof(struct ofp_header)) {
1560 ovs_fatal(0, "reply does not match request");
1562 ofpbuf_delete(reply);
1567 ofctl_packet_out(int argc, char *argv[])
1569 enum ofputil_protocol protocol;
1570 struct ofputil_packet_out po;
1571 struct ofpbuf ofpacts;
1572 struct vconn *vconn;
1575 enum ofputil_protocol usable_protocols; /* TODO: Use in proto selection */
1577 ofpbuf_init(&ofpacts, 64);
1578 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1580 ovs_fatal(0, "%s", error);
1583 po.buffer_id = UINT32_MAX;
1584 po.in_port = str_to_port_no(argv[1], argv[2]);
1585 po.ofpacts = ofpacts.data;
1586 po.ofpacts_len = ofpacts.size;
1588 protocol = open_vconn(argv[1], &vconn);
1589 for (i = 4; i < argc; i++) {
1590 struct ofpbuf *packet, *opo;
1591 const char *error_msg;
1593 error_msg = eth_from_hex(argv[i], &packet);
1595 ovs_fatal(0, "%s", error_msg);
1598 po.packet = packet->data;
1599 po.packet_len = packet->size;
1600 opo = ofputil_encode_packet_out(&po, protocol);
1601 transact_noreply(vconn, opo);
1602 ofpbuf_delete(packet);
1605 ofpbuf_uninit(&ofpacts);
1609 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1611 struct ofp_config_flag {
1612 const char *name; /* The flag's name. */
1613 enum ofputil_port_config bit; /* Bit to turn on or off. */
1614 bool on; /* Value to set the bit to. */
1616 static const struct ofp_config_flag flags[] = {
1617 { "up", OFPUTIL_PC_PORT_DOWN, false },
1618 { "down", OFPUTIL_PC_PORT_DOWN, true },
1619 { "stp", OFPUTIL_PC_NO_STP, false },
1620 { "receive", OFPUTIL_PC_NO_RECV, false },
1621 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1622 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1623 { "forward", OFPUTIL_PC_NO_FWD, false },
1624 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1627 const struct ofp_config_flag *flag;
1628 enum ofputil_protocol protocol;
1629 struct ofputil_port_mod pm;
1630 struct ofputil_phy_port pp;
1631 struct vconn *vconn;
1632 const char *command;
1635 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1637 pm.port_no = pp.port_no;
1638 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1643 if (!strncasecmp(argv[3], "no-", 3)) {
1644 command = argv[3] + 3;
1646 } else if (!strncasecmp(argv[3], "no", 2)) {
1647 command = argv[3] + 2;
1653 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1654 if (!strcasecmp(command, flag->name)) {
1655 pm.mask = flag->bit;
1656 pm.config = flag->on ^ not ? flag->bit : 0;
1660 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1663 protocol = open_vconn(argv[1], &vconn);
1664 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1669 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1671 enum ofputil_protocol protocol, usable_protocols;
1672 struct ofputil_table_mod tm;
1673 struct vconn *vconn;
1677 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1679 ovs_fatal(0, "%s", error);
1682 protocol = open_vconn(argv[1], &vconn);
1683 if (!(protocol & usable_protocols)) {
1684 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1685 enum ofputil_protocol f = 1 << i;
1687 && f & usable_protocols
1688 && try_set_protocol(vconn, f, &protocol)) {
1695 if (!(protocol & usable_protocols)) {
1696 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1697 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1700 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1705 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1707 struct ofp_switch_config config;
1708 struct vconn *vconn;
1710 open_vconn(argv[1], &vconn);
1711 fetch_switch_config(vconn, &config);
1712 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1717 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1719 struct ofp_switch_config config;
1720 enum ofp_config_flags mode;
1721 struct vconn *vconn;
1724 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1725 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1728 open_vconn(argv[1], &vconn);
1729 fetch_switch_config(vconn, &config);
1730 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1731 if (flags != config.flags) {
1732 /* Set the configuration. */
1733 config.flags = flags;
1734 set_switch_config(vconn, &config);
1736 /* Then retrieve the configuration to see if it really took. OpenFlow
1737 * doesn't define error reporting for bad modes, so this is all we can
1739 fetch_switch_config(vconn, &config);
1740 if (flags != config.flags) {
1741 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1742 "switch probably doesn't support mode \"%s\")",
1743 argv[1], ofputil_frag_handling_to_string(mode));
1750 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1752 const char *filename = argv[1];
1756 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1758 ovs_fatal(errno, "%s: open", filename);
1761 ofpbuf_init(&b, 65536);
1763 struct ofp_header *oh;
1764 size_t length, tail_len;
1769 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1770 n = fread(oh, 1, sizeof *oh, file);
1773 } else if (n < sizeof *oh) {
1774 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1777 length = ntohs(oh->length);
1778 if (length < sizeof *oh) {
1779 ovs_fatal(0, "%s: %zu-byte message is too short for OpenFlow",
1783 tail_len = length - sizeof *oh;
1784 tail = ofpbuf_put_uninit(&b, tail_len);
1785 n = fread(tail, 1, tail_len, file);
1787 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1790 ofp_print(stdout, b.data, b.size, verbosity + 2);
1794 if (file != stdin) {
1800 ofctl_ping(int argc, char *argv[])
1802 size_t max_payload = 65535 - sizeof(struct ofp_header);
1803 unsigned int payload;
1804 struct vconn *vconn;
1807 payload = argc > 2 ? atoi(argv[2]) : 64;
1808 if (payload > max_payload) {
1809 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1812 open_vconn(argv[1], &vconn);
1813 for (i = 0; i < 10; i++) {
1814 struct timeval start, end;
1815 struct ofpbuf *request, *reply;
1816 const struct ofp_header *rpy_hdr;
1819 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1820 vconn_get_version(vconn), payload);
1821 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1823 xgettimeofday(&start);
1824 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1825 xgettimeofday(&end);
1827 rpy_hdr = reply->data;
1828 if (ofptype_pull(&type, reply)
1829 || type != OFPTYPE_ECHO_REPLY
1830 || reply->size != payload
1831 || memcmp(request->l3, reply->l3, payload)) {
1832 printf("Reply does not match request. Request:\n");
1833 ofp_print(stdout, request, request->size, verbosity + 2);
1835 ofp_print(stdout, reply, reply->size, verbosity + 2);
1837 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1838 reply->size, argv[1], ntohl(rpy_hdr->xid),
1839 (1000*(double)(end.tv_sec - start.tv_sec))
1840 + (.001*(end.tv_usec - start.tv_usec)));
1841 ofpbuf_delete(request);
1842 ofpbuf_delete(reply);
1848 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1850 size_t max_payload = 65535 - sizeof(struct ofp_header);
1851 struct timeval start, end;
1852 unsigned int payload_size, message_size;
1853 struct vconn *vconn;
1858 payload_size = atoi(argv[2]);
1859 if (payload_size > max_payload) {
1860 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1862 message_size = sizeof(struct ofp_header) + payload_size;
1864 count = atoi(argv[3]);
1866 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1867 count, message_size, count * message_size);
1869 open_vconn(argv[1], &vconn);
1870 xgettimeofday(&start);
1871 for (i = 0; i < count; i++) {
1872 struct ofpbuf *request, *reply;
1874 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1875 vconn_get_version(vconn), payload_size);
1876 ofpbuf_put_zeros(request, payload_size);
1877 run(vconn_transact(vconn, request, &reply), "transact");
1878 ofpbuf_delete(reply);
1880 xgettimeofday(&end);
1883 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1884 + (.001*(end.tv_usec - start.tv_usec)));
1885 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1886 duration, count / (duration / 1000.0),
1887 count * message_size / (duration / 1000.0));
1891 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
1894 struct ofputil_group_mod *gm;
1895 struct ofpbuf *request;
1897 struct vconn *vconn;
1900 open_vconn(remote, &vconn);
1902 for (i = 0; i < n_gms; i++) {
1904 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
1906 transact_noreply(vconn, request);
1916 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1918 struct ofputil_group_mod *gms = NULL;
1919 enum ofputil_protocol usable_protocols;
1923 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
1926 ovs_fatal(0, "%s", error);
1928 ofctl_group_mod__(argv[1], gms, n_gms);
1933 ofctl_group_mod(int argc, char *argv[], uint16_t command)
1935 if (argc > 2 && !strcmp(argv[2], "-")) {
1936 ofctl_group_mod_file(argc, argv, command);
1938 enum ofputil_protocol usable_protocols;
1939 struct ofputil_group_mod gm;
1942 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
1945 ovs_fatal(0, "%s", error);
1947 ofctl_group_mod__(argv[1], &gm, 1);
1952 ofctl_add_group(int argc, char *argv[])
1954 ofctl_group_mod(argc, argv, OFPGC11_ADD);
1958 ofctl_add_groups(int argc, char *argv[])
1960 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
1964 ofctl_mod_group(int argc, char *argv[])
1966 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
1970 ofctl_del_groups(int argc, char *argv[])
1972 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
1976 ofctl_dump_group_stats(int argc, char *argv[])
1978 enum ofputil_protocol usable_protocols;
1979 struct ofputil_group_mod gm;
1980 struct ofpbuf *request;
1981 struct vconn *vconn;
1985 memset(&gm, 0, sizeof gm);
1987 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
1988 argc > 2 ? argv[2] : "",
1991 ovs_fatal(0, "%s", error);
1994 group_id = gm.group_id;
1996 open_vconn(argv[1], &vconn);
1997 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2000 dump_stats_transaction(vconn, request);
2007 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2009 struct ofpbuf *request;
2010 struct vconn *vconn;
2012 open_vconn(argv[1], &vconn);
2014 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2016 dump_stats_transaction(vconn, request);
2023 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2025 struct ofpbuf *request;
2026 struct vconn *vconn;
2028 open_vconn(argv[1], &vconn);
2029 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2031 dump_stats_transaction(vconn, request);
2038 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2043 /* replace-flows and diff-flows commands. */
2045 /* A flow table entry, possibly with two different versions. */
2047 struct cls_rule rule; /* Within a "struct classifier". */
2048 struct fte_version *versions[2];
2051 /* One version of a Flow Table Entry. */
2052 struct fte_version {
2054 uint16_t idle_timeout;
2055 uint16_t hard_timeout;
2057 struct ofpact *ofpacts;
2061 /* Frees 'version' and the data that it owns. */
2063 fte_version_free(struct fte_version *version)
2066 free(version->ofpacts);
2071 /* Returns true if 'a' and 'b' are the same, false if they differ.
2073 * Ignores differences in 'flags' because there's no way to retrieve flags from
2074 * an OpenFlow switch. We have to assume that they are the same. */
2076 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2078 return (a->cookie == b->cookie
2079 && a->idle_timeout == b->idle_timeout
2080 && a->hard_timeout == b->hard_timeout
2081 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2082 b->ofpacts, b->ofpacts_len));
2085 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2086 * 'index' into 's', followed by a new-line. */
2088 fte_version_format(const struct fte *fte, int index, struct ds *s)
2090 const struct fte_version *version = fte->versions[index];
2097 cls_rule_format(&fte->rule, s);
2098 if (version->cookie != htonll(0)) {
2099 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2101 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2102 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2104 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2105 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2108 ds_put_char(s, ' ');
2109 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2111 ds_put_char(s, '\n');
2115 fte_from_cls_rule(const struct cls_rule *cls_rule)
2117 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2120 /* Frees 'fte' and its versions. */
2122 fte_free(struct fte *fte)
2125 fte_version_free(fte->versions[0]);
2126 fte_version_free(fte->versions[1]);
2127 cls_rule_destroy(&fte->rule);
2132 /* Frees all of the FTEs within 'cls'. */
2134 fte_free_all(struct classifier *cls)
2136 struct cls_cursor cursor;
2137 struct fte *fte, *next;
2139 ovs_rwlock_wrlock(&cls->rwlock);
2140 cls_cursor_init(&cursor, cls, NULL);
2141 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2142 classifier_remove(cls, &fte->rule);
2145 ovs_rwlock_unlock(&cls->rwlock);
2146 classifier_destroy(cls);
2149 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2150 * necessary. Sets 'version' as the version of that rule with the given
2151 * 'index', replacing any existing version, if any.
2153 * Takes ownership of 'version'. */
2155 fte_insert(struct classifier *cls, const struct match *match,
2156 unsigned int priority, struct fte_version *version, int index)
2158 struct fte *old, *fte;
2160 fte = xzalloc(sizeof *fte);
2161 cls_rule_init(&fte->rule, match, priority);
2162 fte->versions[index] = version;
2164 ovs_rwlock_wrlock(&cls->rwlock);
2165 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2166 ovs_rwlock_unlock(&cls->rwlock);
2168 fte_version_free(old->versions[index]);
2169 fte->versions[!index] = old->versions[!index];
2170 cls_rule_destroy(&old->rule);
2175 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2176 * with the specified 'index'. Returns the flow formats able to represent the
2177 * flows that were read. */
2178 static enum ofputil_protocol
2179 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2181 enum ofputil_protocol usable_protocols;
2186 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2188 ovs_fatal(errno, "%s: open", filename);
2192 usable_protocols = OFPUTIL_P_ANY;
2194 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2195 struct fte_version *version;
2196 struct ofputil_flow_mod fm;
2198 enum ofputil_protocol usable;
2200 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2202 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2204 usable_protocols &= usable;
2206 version = xmalloc(sizeof *version);
2207 version->cookie = fm.new_cookie;
2208 version->idle_timeout = fm.idle_timeout;
2209 version->hard_timeout = fm.hard_timeout;
2210 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2211 | OFPUTIL_FF_EMERG);
2212 version->ofpacts = fm.ofpacts;
2213 version->ofpacts_len = fm.ofpacts_len;
2215 fte_insert(cls, &fm.match, fm.priority, version, index);
2219 if (file != stdin) {
2223 return usable_protocols;
2227 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2228 struct ofpbuf **replyp,
2229 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2231 struct ofpbuf *reply = *replyp;
2237 /* Get a flow stats reply message, if we don't already have one. */
2243 run(vconn_recv_block(vconn, &reply),
2244 "OpenFlow packet receive failed");
2245 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2247 error = ofptype_decode(&type, reply->data);
2248 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2249 ovs_fatal(0, "received bad reply: %s",
2250 ofp_to_string(reply->data, reply->size,
2255 /* Pull an individual flow stats reply out of the message. */
2256 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2263 more = ofpmp_more(reply->l2);
2264 ofpbuf_delete(reply);
2273 ovs_fatal(0, "parse error in reply (%s)",
2274 ofperr_to_string(retval));
2279 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2280 * format 'protocol', and adds them as flow table entries in 'cls' for the
2281 * version with the specified 'index'. */
2283 read_flows_from_switch(struct vconn *vconn,
2284 enum ofputil_protocol protocol,
2285 struct classifier *cls, int index)
2287 struct ofputil_flow_stats_request fsr;
2288 struct ofputil_flow_stats fs;
2289 struct ofpbuf *request;
2290 struct ofpbuf ofpacts;
2291 struct ofpbuf *reply;
2294 fsr.aggregate = false;
2295 match_init_catchall(&fsr.match);
2296 fsr.out_port = OFPP_ANY;
2297 fsr.table_id = 0xff;
2298 fsr.cookie = fsr.cookie_mask = htonll(0);
2299 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2300 send_xid = ((struct ofp_header *) request->data)->xid;
2301 send_openflow_buffer(vconn, request);
2304 ofpbuf_init(&ofpacts, 0);
2305 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2306 struct fte_version *version;
2308 version = xmalloc(sizeof *version);
2309 version->cookie = fs.cookie;
2310 version->idle_timeout = fs.idle_timeout;
2311 version->hard_timeout = fs.hard_timeout;
2313 version->ofpacts_len = fs.ofpacts_len;
2314 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2316 fte_insert(cls, &fs.match, fs.priority, version, index);
2318 ofpbuf_uninit(&ofpacts);
2322 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2323 enum ofputil_protocol protocol, struct list *packets)
2325 const struct fte_version *version = fte->versions[index];
2326 struct ofputil_flow_mod fm;
2329 minimatch_expand(&fte->rule.match, &fm.match);
2330 fm.priority = fte->rule.priority;
2331 fm.cookie = htonll(0);
2332 fm.cookie_mask = htonll(0);
2333 fm.new_cookie = version->cookie;
2334 fm.modify_cookie = true;
2336 fm.command = command;
2337 fm.idle_timeout = version->idle_timeout;
2338 fm.hard_timeout = version->hard_timeout;
2339 fm.buffer_id = UINT32_MAX;
2340 fm.out_port = OFPP_ANY;
2341 fm.flags = version->flags;
2342 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2343 command == OFPFC_MODIFY_STRICT) {
2344 fm.ofpacts = version->ofpacts;
2345 fm.ofpacts_len = version->ofpacts_len;
2351 ofm = ofputil_encode_flow_mod(&fm, protocol);
2352 list_push_back(packets, &ofm->list_node);
2356 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2358 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2359 enum ofputil_protocol usable_protocols, protocol;
2360 struct cls_cursor cursor;
2361 struct classifier cls;
2362 struct list requests;
2363 struct vconn *vconn;
2366 classifier_init(&cls);
2367 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2369 protocol = open_vconn(argv[1], &vconn);
2370 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2372 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2374 list_init(&requests);
2376 /* Delete flows that exist on the switch but not in the file. */
2377 ovs_rwlock_rdlock(&cls.rwlock);
2378 cls_cursor_init(&cursor, &cls, NULL);
2379 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2380 struct fte_version *file_ver = fte->versions[FILE_IDX];
2381 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2383 if (sw_ver && !file_ver) {
2384 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2385 protocol, &requests);
2389 /* Add flows that exist in the file but not on the switch.
2390 * Update flows that exist in both places but differ. */
2391 cls_cursor_init(&cursor, &cls, NULL);
2392 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2393 struct fte_version *file_ver = fte->versions[FILE_IDX];
2394 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2397 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2398 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2401 ovs_rwlock_unlock(&cls.rwlock);
2402 transact_multiple_noreply(vconn, &requests);
2409 read_flows_from_source(const char *source, struct classifier *cls, int index)
2413 if (source[0] == '/' || source[0] == '.'
2414 || (!strchr(source, ':') && !stat(source, &s))) {
2415 read_flows_from_file(source, cls, index);
2417 enum ofputil_protocol protocol;
2418 struct vconn *vconn;
2420 protocol = open_vconn(source, &vconn);
2421 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2422 read_flows_from_switch(vconn, protocol, cls, index);
2428 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2430 bool differences = false;
2431 struct cls_cursor cursor;
2432 struct classifier cls;
2436 classifier_init(&cls);
2437 read_flows_from_source(argv[1], &cls, 0);
2438 read_flows_from_source(argv[2], &cls, 1);
2443 ovs_rwlock_rdlock(&cls.rwlock);
2444 cls_cursor_init(&cursor, &cls, NULL);
2445 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2446 struct fte_version *a = fte->versions[0];
2447 struct fte_version *b = fte->versions[1];
2449 if (!a || !b || !fte_version_equals(a, b)) {
2450 fte_version_format(fte, 0, &a_s);
2451 fte_version_format(fte, 1, &b_s);
2452 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2454 printf("-%s", ds_cstr(&a_s));
2457 printf("+%s", ds_cstr(&b_s));
2463 ovs_rwlock_unlock(&cls.rwlock);
2475 /* Undocumented commands for unit testing. */
2478 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2479 enum ofputil_protocol usable_protocols)
2481 enum ofputil_protocol protocol = 0;
2485 usable_s = ofputil_protocols_to_string(usable_protocols);
2486 printf("usable protocols: %s\n", usable_s);
2489 if (!(usable_protocols & allowed_protocols)) {
2490 ovs_fatal(0, "no usable protocol");
2492 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2494 if (protocol & usable_protocols & allowed_protocols) {
2498 ovs_assert(is_pow2(protocol));
2500 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2502 for (i = 0; i < n_fms; i++) {
2503 struct ofputil_flow_mod *fm = &fms[i];
2506 msg = ofputil_encode_flow_mod(fm, protocol);
2507 ofp_print(stdout, msg->data, msg->size, verbosity);
2514 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2515 * it back to stdout. */
2517 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2519 enum ofputil_protocol usable_protocols;
2520 struct ofputil_flow_mod fm;
2523 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2525 ovs_fatal(0, "%s", error);
2527 ofctl_parse_flows__(&fm, 1, usable_protocols);
2530 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2531 * add-flows) and prints each of the flows back to stdout. */
2533 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2535 enum ofputil_protocol usable_protocols;
2536 struct ofputil_flow_mod *fms = NULL;
2540 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2543 ovs_fatal(0, "%s", error);
2545 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2550 ofctl_parse_nxm__(bool oxm)
2555 while (!ds_get_test_line(&in, stdin)) {
2556 struct ofpbuf nx_match;
2558 ovs_be64 cookie, cookie_mask;
2562 /* Convert string to nx_match. */
2563 ofpbuf_init(&nx_match, 0);
2565 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2567 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2570 /* Convert nx_match to match. */
2573 error = oxm_pull_match(&nx_match, &match);
2575 error = nx_pull_match(&nx_match, match_len, &match,
2576 &cookie, &cookie_mask);
2580 error = oxm_pull_match_loose(&nx_match, &match);
2582 error = nx_pull_match_loose(&nx_match, match_len, &match,
2583 &cookie, &cookie_mask);
2591 /* Convert match back to nx_match. */
2592 ofpbuf_uninit(&nx_match);
2593 ofpbuf_init(&nx_match, 0);
2595 match_len = oxm_put_match(&nx_match, &match);
2596 out = oxm_match_to_string(&nx_match, match_len);
2598 match_len = nx_put_match(&nx_match, &match,
2599 cookie, cookie_mask);
2600 out = nx_match_to_string(nx_match.data, match_len);
2606 printf("nx_pull_match() returned error %s\n",
2607 ofperr_get_name(error));
2610 ofpbuf_uninit(&nx_match);
2615 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2616 * stdin, does some internal fussing with them, and then prints them back as
2617 * strings on stdout. */
2619 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2621 return ofctl_parse_nxm__(false);
2624 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2625 * stdin, does some internal fussing with them, and then prints them back as
2626 * strings on stdout. */
2628 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2630 return ofctl_parse_nxm__(true);
2634 print_differences(const char *prefix,
2635 const void *a_, size_t a_len,
2636 const void *b_, size_t b_len)
2638 const uint8_t *a = a_;
2639 const uint8_t *b = b_;
2642 for (i = 0; i < MIN(a_len, b_len); i++) {
2644 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2645 prefix, i, a[i], b[i]);
2648 for (i = a_len; i < b_len; i++) {
2649 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2651 for (i = b_len; i < a_len; i++) {
2652 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2656 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2657 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2658 * on stdout, and then converts them back to hex bytes and prints any
2659 * differences from the input. */
2661 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2666 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2667 struct ofpbuf of10_out;
2668 struct ofpbuf of10_in;
2669 struct ofpbuf ofpacts;
2674 /* Parse hex bytes. */
2675 ofpbuf_init(&of10_in, 0);
2676 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2677 ovs_fatal(0, "Trailing garbage in hex data");
2680 /* Convert to ofpacts. */
2681 ofpbuf_init(&ofpacts, 0);
2682 size = of10_in.size;
2683 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2685 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2686 ofpbuf_uninit(&ofpacts);
2687 ofpbuf_uninit(&of10_in);
2690 ofpbuf_push_uninit(&of10_in, size);
2692 /* Print cls_rule. */
2694 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2698 /* Convert back to ofp10 actions and print differences from input. */
2699 ofpbuf_init(&of10_out, 0);
2700 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2702 print_differences("", of10_in.data, of10_in.size,
2703 of10_out.data, of10_out.size);
2706 ofpbuf_uninit(&ofpacts);
2707 ofpbuf_uninit(&of10_in);
2708 ofpbuf_uninit(&of10_out);
2713 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2714 * bytes from stdin, converts them to cls_rules, prints them as strings on
2715 * stdout, and then converts them back to hex bytes and prints any differences
2718 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2719 * values are ignored in the input and will be set to zero when OVS converts
2720 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2721 * it does the conversion to hex, to ensure that in fact they are ignored. */
2723 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2730 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2731 struct ofpbuf match_in, match_expout;
2732 struct ofp10_match match_out;
2733 struct ofp10_match match_normal;
2737 /* Parse hex bytes to use for expected output. */
2739 ds_put_cstr(&expout, ds_cstr(&in));
2740 for (p = ds_cstr(&expout); *p; p++) {
2745 ofpbuf_init(&match_expout, 0);
2746 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2747 ovs_fatal(0, "Trailing garbage in hex data");
2749 if (match_expout.size != sizeof(struct ofp10_match)) {
2750 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2751 match_expout.size, sizeof(struct ofp10_match));
2754 /* Parse hex bytes for input. */
2755 for (p = ds_cstr(&in); *p; p++) {
2757 *p = "0123456789abcdef"[random_uint32() & 0xf];
2760 ofpbuf_init(&match_in, 0);
2761 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2762 ovs_fatal(0, "Trailing garbage in hex data");
2764 if (match_in.size != sizeof(struct ofp10_match)) {
2765 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2766 match_in.size, sizeof(struct ofp10_match));
2769 /* Convert to cls_rule and print. */
2770 ofputil_match_from_ofp10_match(match_in.data, &match);
2771 match_print(&match);
2773 /* Convert back to ofp10_match and print differences from input. */
2774 ofputil_match_to_ofp10_match(&match, &match_out);
2775 print_differences("", match_expout.data, match_expout.size,
2776 &match_out, sizeof match_out);
2778 /* Normalize, then convert and compare again. */
2779 ofputil_normalize_match(&match);
2780 ofputil_match_to_ofp10_match(&match, &match_normal);
2781 print_differences("normal: ", &match_out, sizeof match_out,
2782 &match_normal, sizeof match_normal);
2785 ofpbuf_uninit(&match_in);
2786 ofpbuf_uninit(&match_expout);
2789 ds_destroy(&expout);
2792 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2793 * bytes from stdin, converts them to "struct match"es, prints them as strings
2794 * on stdout, and then converts them back to hex bytes and prints any
2795 * differences from the input. */
2797 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2802 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2803 struct ofpbuf match_in;
2804 struct ofp11_match match_out;
2808 /* Parse hex bytes. */
2809 ofpbuf_init(&match_in, 0);
2810 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2811 ovs_fatal(0, "Trailing garbage in hex data");
2813 if (match_in.size != sizeof(struct ofp11_match)) {
2814 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2815 match_in.size, sizeof(struct ofp11_match));
2818 /* Convert to match. */
2819 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2821 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2822 ofpbuf_uninit(&match_in);
2827 match_print(&match);
2829 /* Convert back to ofp11_match and print differences from input. */
2830 ofputil_match_to_ofp11_match(&match, &match_out);
2832 print_differences("", match_in.data, match_in.size,
2833 &match_out, sizeof match_out);
2836 ofpbuf_uninit(&match_in);
2841 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2842 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2843 * on stdout, and then converts them back to hex bytes and prints any
2844 * differences from the input. */
2846 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2851 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2852 struct ofpbuf of11_out;
2853 struct ofpbuf of11_in;
2854 struct ofpbuf ofpacts;
2859 /* Parse hex bytes. */
2860 ofpbuf_init(&of11_in, 0);
2861 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2862 ovs_fatal(0, "Trailing garbage in hex data");
2865 /* Convert to ofpacts. */
2866 ofpbuf_init(&ofpacts, 0);
2867 size = of11_in.size;
2868 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2871 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2872 ofpbuf_uninit(&ofpacts);
2873 ofpbuf_uninit(&of11_in);
2876 ofpbuf_push_uninit(&of11_in, size);
2878 /* Print cls_rule. */
2880 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2884 /* Convert back to ofp11 actions and print differences from input. */
2885 ofpbuf_init(&of11_out, 0);
2886 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2888 print_differences("", of11_in.data, of11_in.size,
2889 of11_out.data, of11_out.size);
2892 ofpbuf_uninit(&ofpacts);
2893 ofpbuf_uninit(&of11_in);
2894 ofpbuf_uninit(&of11_out);
2899 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2900 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2901 * them as strings on stdout, and then converts them back to hex bytes and
2902 * prints any differences from the input. */
2904 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2909 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2910 struct ofpbuf of11_out;
2911 struct ofpbuf of11_in;
2912 struct ofpbuf ofpacts;
2916 const char *table_id;
2919 /* Parse table_id separated with the follow-up instructions by ",", if
2921 instructions = ds_cstr(&in);
2923 if (strstr(instructions, ",")) {
2924 table_id = strsep(&instructions, ",");
2927 /* Parse hex bytes. */
2928 ofpbuf_init(&of11_in, 0);
2929 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
2930 ovs_fatal(0, "Trailing garbage in hex data");
2933 /* Convert to ofpacts. */
2934 ofpbuf_init(&ofpacts, 0);
2935 size = of11_in.size;
2936 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2939 /* Verify actions. */
2941 memset(&flow, 0, sizeof flow);
2942 error = ofpacts_check(ofpacts.data, ofpacts.size, &flow, OFPP_MAX,
2943 table_id ? atoi(table_id) : 0);
2946 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2947 ofpbuf_uninit(&ofpacts);
2948 ofpbuf_uninit(&of11_in);
2951 ofpbuf_push_uninit(&of11_in, size);
2953 /* Print cls_rule. */
2955 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2959 /* Convert back to ofp11 instructions and print differences from
2961 ofpbuf_init(&of11_out, 0);
2962 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2965 print_differences("", of11_in.data, of11_in.size,
2966 of11_out.data, of11_out.size);
2969 ofpbuf_uninit(&ofpacts);
2970 ofpbuf_uninit(&of11_in);
2971 ofpbuf_uninit(&of11_out);
2976 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2977 * mask values to and from various formats and prints the results. */
2979 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
2984 struct ofputil_flow_mod fm;
2987 struct match nxm_match;
2991 struct ofp10_match of10_raw;
2992 struct match of10_match;
2994 struct ofp11_match of11_raw;
2995 struct match of11_match;
3000 enum ofputil_protocol usable_protocols; /* Unused for now. */
3002 match_init_catchall(&match);
3003 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3004 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3006 /* Convert to and from string. */
3007 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3008 printf("%s -> ", string_s);
3010 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3012 ovs_fatal(0, "%s", error_s);
3014 printf("%04"PRIx16"/%04"PRIx16"\n",
3015 ntohs(fm.match.flow.vlan_tci),
3016 ntohs(fm.match.wc.masks.vlan_tci));
3019 /* Convert to and from NXM. */
3020 ofpbuf_init(&nxm, 0);
3021 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3022 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3023 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3024 printf("NXM: %s -> ", nxm_s);
3026 printf("%s\n", ofperr_to_string(error));
3028 printf("%04"PRIx16"/%04"PRIx16"\n",
3029 ntohs(nxm_match.flow.vlan_tci),
3030 ntohs(nxm_match.wc.masks.vlan_tci));
3033 ofpbuf_uninit(&nxm);
3035 /* Convert to and from OXM. */
3036 ofpbuf_init(&nxm, 0);
3037 nxm_match_len = oxm_put_match(&nxm, &match);
3038 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3039 error = oxm_pull_match(&nxm, &nxm_match);
3040 printf("OXM: %s -> ", nxm_s);
3042 printf("%s\n", ofperr_to_string(error));
3044 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3045 (VLAN_VID_MASK | VLAN_CFI);
3046 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3047 (VLAN_VID_MASK | VLAN_CFI);
3049 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3050 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3051 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3057 ofpbuf_uninit(&nxm);
3059 /* Convert to and from OpenFlow 1.0. */
3060 ofputil_match_to_ofp10_match(&match, &of10_raw);
3061 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3062 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3063 ntohs(of10_raw.dl_vlan),
3064 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3065 of10_raw.dl_vlan_pcp,
3066 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3067 ntohs(of10_match.flow.vlan_tci),
3068 ntohs(of10_match.wc.masks.vlan_tci));
3070 /* Convert to and from OpenFlow 1.1. */
3071 ofputil_match_to_ofp11_match(&match, &of11_raw);
3072 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3073 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3074 ntohs(of11_raw.dl_vlan),
3075 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3076 of11_raw.dl_vlan_pcp,
3077 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3078 ntohs(of11_match.flow.vlan_tci),
3079 ntohs(of11_match.wc.masks.vlan_tci));
3082 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3085 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3090 error = ofperr_from_name(argv[1]);
3092 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3095 for (version = 0; version <= UINT8_MAX; version++) {
3096 const char *name = ofperr_domain_get_name(version);
3098 int vendor = ofperr_get_vendor(error, version);
3099 int type = ofperr_get_type(error, version);
3100 int code = ofperr_get_code(error, version);
3102 if (vendor != -1 || type != -1 || code != -1) {
3103 printf("%s: vendor %#x, type %d, code %d\n",
3104 name, vendor, type, code);
3110 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3111 * error named ENUM and prints the error reply in hex. */
3113 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3115 const struct ofp_header *oh;
3116 struct ofpbuf request, *reply;
3119 error = ofperr_from_name(argv[1]);
3121 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3124 ofpbuf_init(&request, 0);
3125 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3126 ovs_fatal(0, "Trailing garbage in hex data");
3128 if (request.size < sizeof(struct ofp_header)) {
3129 ovs_fatal(0, "Request too short");
3133 if (request.size != ntohs(oh->length)) {
3134 ovs_fatal(0, "Request size inconsistent");
3137 reply = ofperr_encode_reply(error, request.data);
3138 ofpbuf_uninit(&request);
3140 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3141 ofpbuf_delete(reply);
3144 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3145 * binary data, interpreting them as an OpenFlow message, and prints the
3146 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3148 ofctl_ofp_print(int argc, char *argv[])
3150 struct ofpbuf packet;
3152 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3153 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3154 ovs_fatal(0, "trailing garbage following hex bytes");
3156 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
3157 ofpbuf_uninit(&packet);
3160 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3161 * and dumps each message in hex. */
3163 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3165 uint32_t bitmap = strtol(argv[1], NULL, 0);
3166 struct ofpbuf *hello;
3168 hello = ofputil_encode_hello(bitmap);
3169 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3170 ofp_print(stdout, hello->data, hello->size, verbosity);
3171 ofpbuf_delete(hello);
3174 static const struct command all_commands[] = {
3175 { "show", 1, 1, ofctl_show },
3176 { "monitor", 1, 3, ofctl_monitor },
3177 { "snoop", 1, 1, ofctl_snoop },
3178 { "dump-desc", 1, 1, ofctl_dump_desc },
3179 { "dump-tables", 1, 1, ofctl_dump_tables },
3180 { "dump-flows", 1, 2, ofctl_dump_flows },
3181 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3182 { "queue-stats", 1, 3, ofctl_queue_stats },
3183 { "add-flow", 2, 2, ofctl_add_flow },
3184 { "add-flows", 2, 2, ofctl_add_flows },
3185 { "mod-flows", 2, 2, ofctl_mod_flows },
3186 { "del-flows", 1, 2, ofctl_del_flows },
3187 { "replace-flows", 2, 2, ofctl_replace_flows },
3188 { "diff-flows", 2, 2, ofctl_diff_flows },
3189 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3190 { "dump-ports", 1, 2, ofctl_dump_ports },
3191 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3192 { "mod-port", 3, 3, ofctl_mod_port },
3193 { "mod-table", 3, 3, ofctl_mod_table },
3194 { "get-frags", 1, 1, ofctl_get_frags },
3195 { "set-frags", 2, 2, ofctl_set_frags },
3196 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3197 { "probe", 1, 1, ofctl_probe },
3198 { "ping", 1, 2, ofctl_ping },
3199 { "benchmark", 3, 3, ofctl_benchmark },
3201 { "add-group", 1, 2, ofctl_add_group },
3202 { "add-groups", 1, 2, ofctl_add_groups },
3203 { "mod-group", 1, 2, ofctl_mod_group },
3204 { "del-groups", 1, 2, ofctl_del_groups },
3205 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3206 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3207 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3208 { "help", 0, INT_MAX, ofctl_help },
3210 /* Undocumented commands for testing. */
3211 { "parse-flow", 1, 1, ofctl_parse_flow },
3212 { "parse-flows", 1, 1, ofctl_parse_flows },
3213 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3214 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3215 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3216 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3217 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3218 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3219 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3220 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3221 { "check-vlan", 2, 2, ofctl_check_vlan },
3222 { "print-error", 1, 1, ofctl_print_error },
3223 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3224 { "ofp-print", 1, 2, ofctl_ofp_print },
3225 { "encode-hello", 1, 1, ofctl_encode_hello },
3227 { NULL, 0, 0, NULL },
3230 static const struct command *get_all_commands(void)
3232 return all_commands;