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 " get-frags SWITCH print fragment handling behavior\n"
285 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
286 " dump-ports SWITCH [PORT] print port statistics\n"
287 " dump-ports-desc SWITCH print port descriptions\n"
288 " dump-flows SWITCH print all flow entries\n"
289 " dump-flows SWITCH FLOW print matching FLOWs\n"
290 " dump-aggregate SWITCH print aggregate flow statistics\n"
291 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
292 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
293 " add-flow SWITCH FLOW add flow described by FLOW\n"
294 " add-flows SWITCH FILE add flows from FILE\n"
295 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
296 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
297 " replace-flows SWITCH FILE replace flows with those in FILE\n"
298 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
299 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
300 " execute ACTIONS on PACKET\n"
301 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
302 " print packets received from SWITCH\n"
303 " snoop SWITCH snoop on SWITCH and its controller\n"
304 "\nFor OpenFlow switches and controllers:\n"
305 " probe TARGET probe whether TARGET is up\n"
306 " ping TARGET [N] latency of N-byte echos\n"
307 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
308 "SWITCH or TARGET is an active OpenFlow connection method.\n"
309 "\nOther commands:\n"
310 " ofp-parse FILE print messages read from FILE\n",
311 program_name, program_name);
312 vconn_usage(true, false, false);
316 printf("\nOther options:\n"
317 " --strict use strict match for flow commands\n"
318 " --readd replace flows that haven't changed\n"
319 " -F, --flow-format=FORMAT force particular flow format\n"
320 " -P, --packet-in-format=FRMT force particular packet in format\n"
321 " -m, --more be more verbose printing OpenFlow\n"
322 " --timestamp (monitor, snoop) print timestamps\n"
323 " -t, --timeout=SECS give up after SECS seconds\n"
324 " --sort[=field] sort in ascending order\n"
325 " --rsort[=field] sort in descending order\n"
326 " -h, --help display this help message\n"
327 " -V, --version display version information\n");
332 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
333 const char *argv[] OVS_UNUSED, void *exiting_)
335 bool *exiting = exiting_;
337 unixctl_command_reply(conn, NULL);
340 static void run(int retval, const char *message, ...)
344 run(int retval, const char *message, ...)
349 va_start(args, message);
350 ovs_fatal_valist(retval, message, args);
354 /* Generic commands. */
357 open_vconn_socket(const char *name, struct vconn **vconnp)
359 char *vconn_name = xasprintf("unix:%s", name);
362 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
364 if (error && error != ENOENT) {
365 ovs_fatal(0, "%s: failed to open socket (%s)", name,
366 ovs_strerror(error));
373 enum open_target { MGMT, SNOOP };
375 static enum ofputil_protocol
376 open_vconn__(const char *name, enum open_target target,
377 struct vconn **vconnp)
379 const char *suffix = target == MGMT ? "mgmt" : "snoop";
380 char *datapath_name, *datapath_type, *socket_name;
381 enum ofputil_protocol protocol;
386 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
388 ofproto_parse_name(name, &datapath_name, &datapath_type);
389 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
393 if (strchr(name, ':')) {
394 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
395 "connecting to %s", name);
396 } else if (!open_vconn_socket(name, vconnp)) {
398 } else if (!open_vconn_socket(bridge_path, vconnp)) {
400 } else if (!open_vconn_socket(socket_name, vconnp)) {
403 ovs_fatal(0, "%s is not a bridge or a socket", name);
406 if (target == SNOOP) {
407 vconn_set_recv_any_version(*vconnp);
413 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
414 error = vconn_connect_block(*vconnp);
416 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
417 ovs_strerror(error));
420 ofp_version = vconn_get_version(*vconnp);
421 protocol = ofputil_protocol_from_ofp_version(ofp_version);
423 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
429 static enum ofputil_protocol
430 open_vconn(const char *name, struct vconn **vconnp)
432 return open_vconn__(name, MGMT, vconnp);
436 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
438 ofpmsg_update_length(buffer);
439 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
443 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
445 struct ofpbuf *reply;
447 ofpmsg_update_length(request);
448 run(vconn_transact(vconn, request, &reply), "talking to %s",
449 vconn_get_name(vconn));
450 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
451 ofpbuf_delete(reply);
455 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
457 struct ofpbuf *request;
460 open_vconn(vconn_name, &vconn);
461 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
462 dump_transaction(vconn, request);
467 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
469 const struct ofp_header *request_oh = request->data;
470 ovs_be32 send_xid = request_oh->xid;
471 enum ofpraw request_raw;
472 enum ofpraw reply_raw;
475 ofpraw_decode_partial(&request_raw, request->data, request->size);
476 reply_raw = ofpraw_stats_request_to_reply(request_raw,
477 request_oh->version);
479 send_openflow_buffer(vconn, request);
482 struct ofpbuf *reply;
484 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
485 recv_xid = ((struct ofp_header *) reply->data)->xid;
486 if (send_xid == recv_xid) {
489 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
491 ofpraw_decode(&raw, reply->data);
492 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
494 } else if (raw == reply_raw) {
495 done = !ofpmp_more(reply->data);
497 ovs_fatal(0, "received bad reply: %s",
498 ofp_to_string(reply->data, reply->size,
502 VLOG_DBG("received reply with xid %08"PRIx32" "
503 "!= expected %08"PRIx32, recv_xid, send_xid);
505 ofpbuf_delete(reply);
510 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
512 struct ofpbuf *request;
515 open_vconn(vconn_name, &vconn);
516 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
517 dump_stats_transaction(vconn, request);
521 /* Sends all of the 'requests', which should be requests that only have replies
522 * if an error occurs, and waits for them to succeed or fail. If an error does
523 * occur, prints it and exits with an error.
525 * Destroys all of the 'requests'. */
527 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
529 struct ofpbuf *request, *reply;
531 LIST_FOR_EACH (request, list_node, requests) {
532 ofpmsg_update_length(request);
535 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
536 "talking to %s", vconn_get_name(vconn));
538 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
541 ofpbuf_delete(reply);
544 /* Sends 'request', which should be a request that only has a reply if an error
545 * occurs, and waits for it to succeed or fail. If an error does occur, prints
546 * it and exits with an error.
548 * Destroys 'request'. */
550 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
552 struct list requests;
554 list_init(&requests);
555 list_push_back(&requests, &request->list_node);
556 transact_multiple_noreply(vconn, &requests);
560 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
562 struct ofp_switch_config *config;
563 struct ofpbuf *request;
564 struct ofpbuf *reply;
567 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
568 vconn_get_version(vconn), 0);
569 run(vconn_transact(vconn, request, &reply),
570 "talking to %s", vconn_get_name(vconn));
572 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
573 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
576 config = ofpbuf_pull(reply, sizeof *config);
579 ofpbuf_delete(reply);
583 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
585 struct ofpbuf *request;
587 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
588 ofpbuf_put(request, config, sizeof *config);
590 transact_noreply(vconn, request);
594 ofctl_show(int argc OVS_UNUSED, char *argv[])
596 const char *vconn_name = argv[1];
598 struct ofpbuf *request;
599 struct ofpbuf *reply;
602 open_vconn(vconn_name, &vconn);
603 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
604 vconn_get_version(vconn), 0);
605 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
607 trunc = ofputil_switch_features_ports_trunc(reply);
608 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
610 ofpbuf_delete(reply);
613 /* The Features Reply may not contain all the ports, so send a
614 * Port Description stats request, which doesn't have size
616 dump_trivial_stats_transaction(vconn_name,
617 OFPRAW_OFPST_PORT_DESC_REQUEST);
619 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
624 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
626 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
630 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
632 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
636 fetch_port_by_features(const char *vconn_name,
637 const char *port_name, ofp_port_t port_no,
638 struct ofputil_phy_port *pp, bool *trunc)
640 struct ofputil_switch_features features;
641 const struct ofp_header *oh;
642 struct ofpbuf *request, *reply;
649 /* Fetch the switch's ofp_switch_features. */
650 open_vconn(vconn_name, &vconn);
651 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
652 vconn_get_version(vconn), 0);
653 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
657 if (ofptype_decode(&type, reply->data)
658 || type != OFPTYPE_FEATURES_REPLY) {
659 ovs_fatal(0, "%s: received bad features reply", vconn_name);
663 if (ofputil_switch_features_ports_trunc(reply)) {
668 error = ofputil_decode_switch_features(oh, &features, &b);
670 ovs_fatal(0, "%s: failed to decode features reply (%s)",
671 vconn_name, ofperr_to_string(error));
674 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
675 if (port_no != OFPP_NONE
676 ? port_no == pp->port_no
677 : !strcmp(pp->name, port_name)) {
684 ofpbuf_delete(reply);
689 fetch_port_by_stats(const char *vconn_name,
690 const char *port_name, ofp_port_t port_no,
691 struct ofputil_phy_port *pp)
693 struct ofpbuf *request;
699 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
700 send_xid = ((struct ofp_header *) request->data)->xid;
702 open_vconn(vconn_name, &vconn);
703 send_openflow_buffer(vconn, request);
706 struct ofpbuf *reply;
708 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
709 recv_xid = ((struct ofp_header *) reply->data)->xid;
710 if (send_xid == recv_xid) {
711 struct ofp_header *oh = reply->data;
716 ofpbuf_use_const(&b, oh, ntohs(oh->length));
717 if (ofptype_pull(&type, &b)
718 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
719 ovs_fatal(0, "received bad reply: %s",
720 ofp_to_string(reply->data, reply->size,
724 flags = ofpmp_flags(oh);
725 done = !(flags & OFPSF_REPLY_MORE);
728 /* We've already found the port, but we need to drain
729 * the queue of any other replies for this request. */
733 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
734 if (port_no != OFPP_NONE ? port_no == pp->port_no
735 : !strcmp(pp->name, port_name)) {
741 VLOG_DBG("received reply with xid %08"PRIx32" "
742 "!= expected %08"PRIx32, recv_xid, send_xid);
744 ofpbuf_delete(reply);
752 str_to_ofp(const char *s, ofp_port_t *ofp_port)
757 ret = str_to_uint(s, 10, &port_);
758 *ofp_port = u16_to_ofp(port_);
762 /* Opens a connection to 'vconn_name', fetches the port structure for
763 * 'port_name' (which may be a port name or number), and copies it into
766 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
767 struct ofputil_phy_port *pp)
773 /* Try to interpret the argument as a port number. */
774 if (!str_to_ofp(port_name, &port_no)) {
778 /* Try to find the port based on the Features Reply. If it looks
779 * like the results may be truncated, then use the Port Description
780 * stats message introduced in OVS 1.7. */
781 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
784 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
788 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
792 /* Returns the port number corresponding to 'port_name' (which may be a port
793 * name or number) within the switch 'vconn_name'. */
795 str_to_port_no(const char *vconn_name, const char *port_name)
799 if (ofputil_port_from_string(port_name, &port_no)) {
802 struct ofputil_phy_port pp;
804 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
810 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
811 enum ofputil_protocol *cur)
814 struct ofpbuf *request, *reply;
815 enum ofputil_protocol next;
817 request = ofputil_encode_set_protocol(*cur, want, &next);
822 run(vconn_transact_noreply(vconn, request, &reply),
823 "talking to %s", vconn_get_name(vconn));
825 char *s = ofp_to_string(reply->data, reply->size, 2);
826 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
827 vconn_get_name(vconn), s);
829 ofpbuf_delete(reply);
837 static enum ofputil_protocol
838 set_protocol_for_flow_dump(struct vconn *vconn,
839 enum ofputil_protocol cur_protocol,
840 enum ofputil_protocol usable_protocols)
845 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
846 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
847 if (f & usable_protocols & allowed_protocols
848 && try_set_protocol(vconn, f, &cur_protocol)) {
853 usable_s = ofputil_protocols_to_string(usable_protocols);
854 if (usable_protocols & allowed_protocols) {
855 ovs_fatal(0, "switch does not support any of the usable flow "
856 "formats (%s)", usable_s);
858 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
859 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
860 "allowed flow formats (%s)", usable_s, allowed_s);
864 static struct vconn *
865 prepare_dump_flows(int argc, char *argv[], bool aggregate,
866 struct ofpbuf **requestp)
868 enum ofputil_protocol usable_protocols, protocol;
869 struct ofputil_flow_stats_request fsr;
873 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
874 argc > 2 ? argv[2] : "");
876 ovs_fatal(0, "%s", error);
879 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
881 protocol = open_vconn(argv[1], &vconn);
882 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
883 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
888 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
890 struct ofpbuf *request;
893 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
894 dump_stats_transaction(vconn, request);
899 compare_flows(const void *afs_, const void *bfs_)
901 const struct ofputil_flow_stats *afs = afs_;
902 const struct ofputil_flow_stats *bfs = bfs_;
903 const struct match *a = &afs->match;
904 const struct match *b = &bfs->match;
905 const struct sort_criterion *sc;
907 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
908 const struct mf_field *f = sc->field;
912 unsigned int a_pri = afs->priority;
913 unsigned int b_pri = bfs->priority;
914 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
918 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
919 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
921 /* Skip the test for sc->order, so that missing fields always
922 * sort to the end whether we're sorting in ascending or
923 * descending order. */
926 union mf_value aval, bval;
928 mf_get_value(f, &a->flow, &aval);
929 mf_get_value(f, &b->flow, &bval);
930 ret = memcmp(&aval, &bval, f->n_bytes);
935 return sc->order == SORT_ASC ? ret : -ret;
943 ofctl_dump_flows(int argc, char *argv[])
946 return ofctl_dump_flows__(argc, argv, false);
948 struct ofputil_flow_stats *fses;
949 size_t n_fses, allocated_fses;
950 struct ofpbuf *request;
951 struct ofpbuf ofpacts;
952 struct ofpbuf *reply;
958 vconn = prepare_dump_flows(argc, argv, false, &request);
959 send_xid = ((struct ofp_header *) request->data)->xid;
960 send_openflow_buffer(vconn, request);
963 n_fses = allocated_fses = 0;
965 ofpbuf_init(&ofpacts, 0);
967 struct ofputil_flow_stats *fs;
969 if (n_fses >= allocated_fses) {
970 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
974 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
978 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
981 ofpbuf_uninit(&ofpacts);
983 qsort(fses, n_fses, sizeof *fses, compare_flows);
986 for (i = 0; i < n_fses; i++) {
988 ofp_print_flow_stats(&s, &fses[i]);
993 for (i = 0; i < n_fses; i++) {
994 free(fses[i].ofpacts);
1003 ofctl_dump_aggregate(int argc, char *argv[])
1005 return ofctl_dump_flows__(argc, argv, true);
1009 ofctl_queue_stats(int argc, char *argv[])
1011 struct ofpbuf *request;
1012 struct vconn *vconn;
1013 struct ofputil_queue_stats_request oqs;
1015 open_vconn(argv[1], &vconn);
1017 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1018 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1020 oqs.port_no = OFPP_ANY;
1022 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1023 oqs.queue_id = atoi(argv[3]);
1025 oqs.queue_id = OFPQ_ALL;
1028 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1029 dump_stats_transaction(vconn, request);
1033 static enum ofputil_protocol
1034 open_vconn_for_flow_mod(const char *remote,
1035 const struct ofputil_flow_mod *fms, size_t n_fms,
1036 struct vconn **vconnp)
1038 enum ofputil_protocol usable_protocols;
1039 enum ofputil_protocol cur_protocol;
1043 /* Figure out what flow formats will work. */
1044 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1045 if (!(usable_protocols & allowed_protocols)) {
1046 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1047 usable_s = ofputil_protocols_to_string(usable_protocols);
1048 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1049 "allowed flow formats (%s)", usable_s, allowed_s);
1052 /* If the initial flow format is allowed and usable, keep it. */
1053 cur_protocol = open_vconn(remote, vconnp);
1054 if (usable_protocols & allowed_protocols & cur_protocol) {
1055 return cur_protocol;
1058 /* Otherwise try each flow format in turn. */
1059 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1060 enum ofputil_protocol f = 1 << i;
1062 if (f != cur_protocol
1063 && f & usable_protocols & allowed_protocols
1064 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1069 usable_s = ofputil_protocols_to_string(usable_protocols);
1070 ovs_fatal(0, "switch does not support any of the usable flow "
1071 "formats (%s)", usable_s);
1075 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1078 enum ofputil_protocol protocol;
1079 struct vconn *vconn;
1082 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
1084 for (i = 0; i < n_fms; i++) {
1085 struct ofputil_flow_mod *fm = &fms[i];
1087 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1094 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1096 struct ofputil_flow_mod *fms = NULL;
1100 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
1102 ovs_fatal(0, "%s", error);
1104 ofctl_flow_mod__(argv[1], fms, n_fms);
1109 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1111 if (argc > 2 && !strcmp(argv[2], "-")) {
1112 ofctl_flow_mod_file(argc, argv, command);
1114 struct ofputil_flow_mod fm;
1117 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command);
1119 ovs_fatal(0, "%s", error);
1121 ofctl_flow_mod__(argv[1], &fm, 1);
1126 ofctl_add_flow(int argc, char *argv[])
1128 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1132 ofctl_add_flows(int argc, char *argv[])
1134 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1138 ofctl_mod_flows(int argc, char *argv[])
1140 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1144 ofctl_del_flows(int argc, char *argv[])
1146 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1150 set_packet_in_format(struct vconn *vconn,
1151 enum nx_packet_in_format packet_in_format)
1153 struct ofpbuf *spif;
1155 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1157 transact_noreply(vconn, spif);
1158 VLOG_DBG("%s: using user-specified packet in format %s",
1159 vconn_get_name(vconn),
1160 ofputil_packet_in_format_to_string(packet_in_format));
1164 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1166 struct ofp_switch_config config;
1167 enum ofp_config_flags flags;
1169 fetch_switch_config(vconn, &config);
1170 flags = ntohs(config.flags);
1171 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1172 /* Set the invalid ttl config. */
1173 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1175 config.flags = htons(flags);
1176 set_switch_config(vconn, &config);
1178 /* Then retrieve the configuration to see if it really took. OpenFlow
1179 * doesn't define error reporting for bad modes, so this is all we can
1181 fetch_switch_config(vconn, &config);
1182 flags = ntohs(config.flags);
1183 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1184 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1185 "switch probably doesn't support mode)");
1192 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1193 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1194 * an error message and stores NULL in '*msgp'. */
1196 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1198 struct ofp_header *oh;
1201 msg = ofpbuf_new(strlen(hex) / 2);
1204 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1206 return "Trailing garbage in hex data";
1209 if (msg->size < sizeof(struct ofp_header)) {
1211 return "Message too short for OpenFlow";
1215 if (msg->size != ntohs(oh->length)) {
1217 return "Message size does not match length in OpenFlow header";
1225 ofctl_send(struct unixctl_conn *conn, int argc,
1226 const char *argv[], void *vconn_)
1228 struct vconn *vconn = vconn_;
1235 for (i = 1; i < argc; i++) {
1236 const char *error_msg;
1240 error_msg = openflow_from_hex(argv[i], &msg);
1242 ds_put_format(&reply, "%s\n", error_msg);
1247 fprintf(stderr, "send: ");
1248 ofp_print(stderr, msg->data, msg->size, verbosity);
1250 error = vconn_send_block(vconn, msg);
1253 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1256 ds_put_cstr(&reply, "sent\n");
1261 unixctl_command_reply(conn, ds_cstr(&reply));
1263 unixctl_command_reply_error(conn, ds_cstr(&reply));
1268 struct barrier_aux {
1269 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1270 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1274 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1275 const char *argv[] OVS_UNUSED, void *aux_)
1277 struct barrier_aux *aux = aux_;
1282 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1286 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1287 error = vconn_send_block(aux->vconn, msg);
1290 unixctl_command_reply_error(conn, ovs_strerror(error));
1297 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1298 const char *argv[], void *aux OVS_UNUSED)
1302 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1304 unixctl_command_reply_error(conn, ovs_strerror(errno));
1309 dup2(fd, STDERR_FILENO);
1311 unixctl_command_reply(conn, NULL);
1315 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1316 const char *argv[] OVS_UNUSED, void *blocked_)
1318 bool *blocked = blocked_;
1322 unixctl_command_reply(conn, NULL);
1324 unixctl_command_reply(conn, "already blocking");
1329 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1330 const char *argv[] OVS_UNUSED, void *blocked_)
1332 bool *blocked = blocked_;
1336 unixctl_command_reply(conn, NULL);
1338 unixctl_command_reply(conn, "already unblocked");
1342 /* Prints to stdout all of the messages received on 'vconn'.
1344 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1345 * received on 'vconn'. */
1347 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1349 struct barrier_aux barrier_aux = { vconn, NULL };
1350 struct unixctl_server *server;
1351 bool exiting = false;
1352 bool blocked = false;
1355 daemon_save_fd(STDERR_FILENO);
1357 error = unixctl_server_create(NULL, &server);
1359 ovs_fatal(error, "failed to create unixctl server");
1361 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1362 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1364 unixctl_command_register("ofctl/barrier", "", 0, 0,
1365 ofctl_barrier, &barrier_aux);
1366 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1367 ofctl_set_output_file, NULL);
1369 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1370 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1373 daemonize_complete();
1379 unixctl_server_run(server);
1384 retval = vconn_recv(vconn, &b);
1385 if (retval == EAGAIN) {
1388 run(retval, "vconn_recv");
1391 char *s = xastrftime("%Y-%m-%d %H:%M:%S: ", time_wall(), true);
1396 ofptype_decode(&type, b->data);
1397 ofp_print(stderr, b->data, b->size, verbosity + 2);
1399 switch ((int) type) {
1400 case OFPTYPE_BARRIER_REPLY:
1401 if (barrier_aux.conn) {
1402 unixctl_command_reply(barrier_aux.conn, NULL);
1403 barrier_aux.conn = NULL;
1407 case OFPTYPE_ECHO_REQUEST:
1408 if (reply_to_echo_requests) {
1409 struct ofpbuf *reply;
1411 reply = make_echo_reply(b->data);
1412 retval = vconn_send_block(vconn, reply);
1414 ovs_fatal(retval, "failed to send echo reply");
1427 vconn_run_wait(vconn);
1429 vconn_recv_wait(vconn);
1431 unixctl_server_wait(server);
1435 unixctl_server_destroy(server);
1439 ofctl_monitor(int argc, char *argv[])
1441 struct vconn *vconn;
1444 open_vconn(argv[1], &vconn);
1445 for (i = 2; i < argc; i++) {
1446 const char *arg = argv[i];
1448 if (isdigit((unsigned char) *arg)) {
1449 struct ofp_switch_config config;
1451 fetch_switch_config(vconn, &config);
1452 config.miss_send_len = htons(atoi(arg));
1453 set_switch_config(vconn, &config);
1454 } else if (!strcmp(arg, "invalid_ttl")) {
1455 monitor_set_invalid_ttl_to_controller(vconn);
1456 } else if (!strncmp(arg, "watch:", 6)) {
1457 struct ofputil_flow_monitor_request fmr;
1461 error = parse_flow_monitor_request(&fmr, arg + 6);
1463 ovs_fatal(0, "%s", error);
1466 msg = ofpbuf_new(0);
1467 ofputil_append_flow_monitor_request(&fmr, msg);
1468 dump_stats_transaction(vconn, msg);
1470 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1474 if (preferred_packet_in_format >= 0) {
1475 set_packet_in_format(vconn, preferred_packet_in_format);
1477 enum ofp_version version = vconn_get_version(vconn);
1480 case OFP10_VERSION: {
1481 struct ofpbuf *spif, *reply;
1483 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1485 run(vconn_transact_noreply(vconn, spif, &reply),
1486 "talking to %s", vconn_get_name(vconn));
1488 char *s = ofp_to_string(reply->data, reply->size, 2);
1489 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1490 " replied: %s. Falling back to the switch default.",
1491 vconn_get_name(vconn), s);
1493 ofpbuf_delete(reply);
1506 monitor_vconn(vconn, true);
1510 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1512 struct vconn *vconn;
1514 open_vconn__(argv[1], SNOOP, &vconn);
1515 monitor_vconn(vconn, false);
1519 ofctl_dump_ports(int argc, char *argv[])
1521 struct ofpbuf *request;
1522 struct vconn *vconn;
1525 open_vconn(argv[1], &vconn);
1526 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1527 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1528 dump_stats_transaction(vconn, request);
1533 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1535 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1539 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1541 struct ofpbuf *request;
1542 struct vconn *vconn;
1543 struct ofpbuf *reply;
1545 open_vconn(argv[1], &vconn);
1546 request = make_echo_request(vconn_get_version(vconn));
1547 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1548 if (reply->size != sizeof(struct ofp_header)) {
1549 ovs_fatal(0, "reply does not match request");
1551 ofpbuf_delete(reply);
1556 ofctl_packet_out(int argc, char *argv[])
1558 enum ofputil_protocol protocol;
1559 struct ofputil_packet_out po;
1560 struct ofpbuf ofpacts;
1561 struct vconn *vconn;
1565 ofpbuf_init(&ofpacts, 64);
1566 error = parse_ofpacts(argv[3], &ofpacts);
1568 ovs_fatal(0, "%s", error);
1571 po.buffer_id = UINT32_MAX;
1572 po.in_port = str_to_port_no(argv[1], argv[2]);
1573 po.ofpacts = ofpacts.data;
1574 po.ofpacts_len = ofpacts.size;
1576 protocol = open_vconn(argv[1], &vconn);
1577 for (i = 4; i < argc; i++) {
1578 struct ofpbuf *packet, *opo;
1579 const char *error_msg;
1581 error_msg = eth_from_hex(argv[i], &packet);
1583 ovs_fatal(0, "%s", error_msg);
1586 po.packet = packet->data;
1587 po.packet_len = packet->size;
1588 opo = ofputil_encode_packet_out(&po, protocol);
1589 transact_noreply(vconn, opo);
1590 ofpbuf_delete(packet);
1593 ofpbuf_uninit(&ofpacts);
1597 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1599 struct ofp_config_flag {
1600 const char *name; /* The flag's name. */
1601 enum ofputil_port_config bit; /* Bit to turn on or off. */
1602 bool on; /* Value to set the bit to. */
1604 static const struct ofp_config_flag flags[] = {
1605 { "up", OFPUTIL_PC_PORT_DOWN, false },
1606 { "down", OFPUTIL_PC_PORT_DOWN, true },
1607 { "stp", OFPUTIL_PC_NO_STP, false },
1608 { "receive", OFPUTIL_PC_NO_RECV, false },
1609 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1610 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1611 { "forward", OFPUTIL_PC_NO_FWD, false },
1612 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1615 const struct ofp_config_flag *flag;
1616 enum ofputil_protocol protocol;
1617 struct ofputil_port_mod pm;
1618 struct ofputil_phy_port pp;
1619 struct vconn *vconn;
1620 const char *command;
1623 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1625 pm.port_no = pp.port_no;
1626 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1631 if (!strncasecmp(argv[3], "no-", 3)) {
1632 command = argv[3] + 3;
1634 } else if (!strncasecmp(argv[3], "no", 2)) {
1635 command = argv[3] + 2;
1641 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1642 if (!strcasecmp(command, flag->name)) {
1643 pm.mask = flag->bit;
1644 pm.config = flag->on ^ not ? flag->bit : 0;
1648 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1651 protocol = open_vconn(argv[1], &vconn);
1652 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1657 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1659 struct ofp_switch_config config;
1660 struct vconn *vconn;
1662 open_vconn(argv[1], &vconn);
1663 fetch_switch_config(vconn, &config);
1664 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1669 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1671 struct ofp_switch_config config;
1672 enum ofp_config_flags mode;
1673 struct vconn *vconn;
1676 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1677 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1680 open_vconn(argv[1], &vconn);
1681 fetch_switch_config(vconn, &config);
1682 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1683 if (flags != config.flags) {
1684 /* Set the configuration. */
1685 config.flags = flags;
1686 set_switch_config(vconn, &config);
1688 /* Then retrieve the configuration to see if it really took. OpenFlow
1689 * doesn't define error reporting for bad modes, so this is all we can
1691 fetch_switch_config(vconn, &config);
1692 if (flags != config.flags) {
1693 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1694 "switch probably doesn't support mode \"%s\")",
1695 argv[1], ofputil_frag_handling_to_string(mode));
1702 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1704 const char *filename = argv[1];
1708 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1710 ovs_fatal(errno, "%s: open", filename);
1713 ofpbuf_init(&b, 65536);
1715 struct ofp_header *oh;
1716 size_t length, tail_len;
1721 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1722 n = fread(oh, 1, sizeof *oh, file);
1725 } else if (n < sizeof *oh) {
1726 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1729 length = ntohs(oh->length);
1730 if (length < sizeof *oh) {
1731 ovs_fatal(0, "%s: %zu-byte message is too short for OpenFlow",
1735 tail_len = length - sizeof *oh;
1736 tail = ofpbuf_put_uninit(&b, tail_len);
1737 n = fread(tail, 1, tail_len, file);
1739 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1742 ofp_print(stdout, b.data, b.size, verbosity + 2);
1746 if (file != stdin) {
1752 ofctl_ping(int argc, char *argv[])
1754 size_t max_payload = 65535 - sizeof(struct ofp_header);
1755 unsigned int payload;
1756 struct vconn *vconn;
1759 payload = argc > 2 ? atoi(argv[2]) : 64;
1760 if (payload > max_payload) {
1761 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1764 open_vconn(argv[1], &vconn);
1765 for (i = 0; i < 10; i++) {
1766 struct timeval start, end;
1767 struct ofpbuf *request, *reply;
1768 const struct ofp_header *rpy_hdr;
1771 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1772 vconn_get_version(vconn), payload);
1773 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1775 xgettimeofday(&start);
1776 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1777 xgettimeofday(&end);
1779 rpy_hdr = reply->data;
1780 if (ofptype_pull(&type, reply)
1781 || type != OFPTYPE_ECHO_REPLY
1782 || reply->size != payload
1783 || memcmp(request->l3, reply->l3, payload)) {
1784 printf("Reply does not match request. Request:\n");
1785 ofp_print(stdout, request, request->size, verbosity + 2);
1787 ofp_print(stdout, reply, reply->size, verbosity + 2);
1789 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1790 reply->size, argv[1], ntohl(rpy_hdr->xid),
1791 (1000*(double)(end.tv_sec - start.tv_sec))
1792 + (.001*(end.tv_usec - start.tv_usec)));
1793 ofpbuf_delete(request);
1794 ofpbuf_delete(reply);
1800 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1802 size_t max_payload = 65535 - sizeof(struct ofp_header);
1803 struct timeval start, end;
1804 unsigned int payload_size, message_size;
1805 struct vconn *vconn;
1810 payload_size = atoi(argv[2]);
1811 if (payload_size > max_payload) {
1812 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1814 message_size = sizeof(struct ofp_header) + payload_size;
1816 count = atoi(argv[3]);
1818 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1819 count, message_size, count * message_size);
1821 open_vconn(argv[1], &vconn);
1822 xgettimeofday(&start);
1823 for (i = 0; i < count; i++) {
1824 struct ofpbuf *request, *reply;
1826 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1827 vconn_get_version(vconn), payload_size);
1828 ofpbuf_put_zeros(request, payload_size);
1829 run(vconn_transact(vconn, request, &reply), "transact");
1830 ofpbuf_delete(reply);
1832 xgettimeofday(&end);
1835 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1836 + (.001*(end.tv_usec - start.tv_usec)));
1837 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1838 duration, count / (duration / 1000.0),
1839 count * message_size / (duration / 1000.0));
1843 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1848 /* replace-flows and diff-flows commands. */
1850 /* A flow table entry, possibly with two different versions. */
1852 struct cls_rule rule; /* Within a "struct classifier". */
1853 struct fte_version *versions[2];
1856 /* One version of a Flow Table Entry. */
1857 struct fte_version {
1859 uint16_t idle_timeout;
1860 uint16_t hard_timeout;
1862 struct ofpact *ofpacts;
1866 /* Frees 'version' and the data that it owns. */
1868 fte_version_free(struct fte_version *version)
1871 free(version->ofpacts);
1876 /* Returns true if 'a' and 'b' are the same, false if they differ.
1878 * Ignores differences in 'flags' because there's no way to retrieve flags from
1879 * an OpenFlow switch. We have to assume that they are the same. */
1881 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1883 return (a->cookie == b->cookie
1884 && a->idle_timeout == b->idle_timeout
1885 && a->hard_timeout == b->hard_timeout
1886 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1887 b->ofpacts, b->ofpacts_len));
1890 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
1891 * 'index' into 's', followed by a new-line. */
1893 fte_version_format(const struct fte *fte, int index, struct ds *s)
1895 const struct fte_version *version = fte->versions[index];
1902 cls_rule_format(&fte->rule, s);
1903 if (version->cookie != htonll(0)) {
1904 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
1906 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1907 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
1909 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1910 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
1913 ds_put_char(s, ' ');
1914 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
1916 ds_put_char(s, '\n');
1920 fte_from_cls_rule(const struct cls_rule *cls_rule)
1922 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1925 /* Frees 'fte' and its versions. */
1927 fte_free(struct fte *fte)
1930 fte_version_free(fte->versions[0]);
1931 fte_version_free(fte->versions[1]);
1932 cls_rule_destroy(&fte->rule);
1937 /* Frees all of the FTEs within 'cls'. */
1939 fte_free_all(struct classifier *cls)
1941 struct cls_cursor cursor;
1942 struct fte *fte, *next;
1944 ovs_rwlock_wrlock(&cls->rwlock);
1945 cls_cursor_init(&cursor, cls, NULL);
1946 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1947 classifier_remove(cls, &fte->rule);
1950 ovs_rwlock_unlock(&cls->rwlock);
1951 classifier_destroy(cls);
1954 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1955 * necessary. Sets 'version' as the version of that rule with the given
1956 * 'index', replacing any existing version, if any.
1958 * Takes ownership of 'version'. */
1960 fte_insert(struct classifier *cls, const struct match *match,
1961 unsigned int priority, struct fte_version *version, int index)
1963 struct fte *old, *fte;
1965 fte = xzalloc(sizeof *fte);
1966 cls_rule_init(&fte->rule, match, priority);
1967 fte->versions[index] = version;
1969 ovs_rwlock_wrlock(&cls->rwlock);
1970 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1971 ovs_rwlock_unlock(&cls->rwlock);
1973 fte_version_free(old->versions[index]);
1974 fte->versions[!index] = old->versions[!index];
1975 cls_rule_destroy(&old->rule);
1980 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1981 * with the specified 'index'. Returns the flow formats able to represent the
1982 * flows that were read. */
1983 static enum ofputil_protocol
1984 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1986 enum ofputil_protocol usable_protocols;
1991 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1993 ovs_fatal(errno, "%s: open", filename);
1997 usable_protocols = OFPUTIL_P_ANY;
1999 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2000 struct fte_version *version;
2001 struct ofputil_flow_mod fm;
2004 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s));
2006 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2009 version = xmalloc(sizeof *version);
2010 version->cookie = fm.new_cookie;
2011 version->idle_timeout = fm.idle_timeout;
2012 version->hard_timeout = fm.hard_timeout;
2013 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2014 | OFPUTIL_FF_EMERG);
2015 version->ofpacts = fm.ofpacts;
2016 version->ofpacts_len = fm.ofpacts_len;
2018 usable_protocols &= ofputil_usable_protocols(&fm.match);
2020 fte_insert(cls, &fm.match, fm.priority, version, index);
2024 if (file != stdin) {
2028 return usable_protocols;
2032 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2033 struct ofpbuf **replyp,
2034 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2036 struct ofpbuf *reply = *replyp;
2042 /* Get a flow stats reply message, if we don't already have one. */
2048 run(vconn_recv_block(vconn, &reply),
2049 "OpenFlow packet receive failed");
2050 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2052 error = ofptype_decode(&type, reply->data);
2053 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2054 ovs_fatal(0, "received bad reply: %s",
2055 ofp_to_string(reply->data, reply->size,
2060 /* Pull an individual flow stats reply out of the message. */
2061 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2068 more = ofpmp_more(reply->l2);
2069 ofpbuf_delete(reply);
2078 ovs_fatal(0, "parse error in reply (%s)",
2079 ofperr_to_string(retval));
2084 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2085 * format 'protocol', and adds them as flow table entries in 'cls' for the
2086 * version with the specified 'index'. */
2088 read_flows_from_switch(struct vconn *vconn,
2089 enum ofputil_protocol protocol,
2090 struct classifier *cls, int index)
2092 struct ofputil_flow_stats_request fsr;
2093 struct ofputil_flow_stats fs;
2094 struct ofpbuf *request;
2095 struct ofpbuf ofpacts;
2096 struct ofpbuf *reply;
2099 fsr.aggregate = false;
2100 match_init_catchall(&fsr.match);
2101 fsr.out_port = OFPP_ANY;
2102 fsr.table_id = 0xff;
2103 fsr.cookie = fsr.cookie_mask = htonll(0);
2104 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2105 send_xid = ((struct ofp_header *) request->data)->xid;
2106 send_openflow_buffer(vconn, request);
2109 ofpbuf_init(&ofpacts, 0);
2110 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2111 struct fte_version *version;
2113 version = xmalloc(sizeof *version);
2114 version->cookie = fs.cookie;
2115 version->idle_timeout = fs.idle_timeout;
2116 version->hard_timeout = fs.hard_timeout;
2118 version->ofpacts_len = fs.ofpacts_len;
2119 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2121 fte_insert(cls, &fs.match, fs.priority, version, index);
2123 ofpbuf_uninit(&ofpacts);
2127 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2128 enum ofputil_protocol protocol, struct list *packets)
2130 const struct fte_version *version = fte->versions[index];
2131 struct ofputil_flow_mod fm;
2134 minimatch_expand(&fte->rule.match, &fm.match);
2135 fm.priority = fte->rule.priority;
2136 fm.cookie = htonll(0);
2137 fm.cookie_mask = htonll(0);
2138 fm.new_cookie = version->cookie;
2139 fm.modify_cookie = true;
2141 fm.command = command;
2142 fm.idle_timeout = version->idle_timeout;
2143 fm.hard_timeout = version->hard_timeout;
2144 fm.buffer_id = UINT32_MAX;
2145 fm.out_port = OFPP_ANY;
2146 fm.flags = version->flags;
2147 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2148 command == OFPFC_MODIFY_STRICT) {
2149 fm.ofpacts = version->ofpacts;
2150 fm.ofpacts_len = version->ofpacts_len;
2156 ofm = ofputil_encode_flow_mod(&fm, protocol);
2157 list_push_back(packets, &ofm->list_node);
2161 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2163 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2164 enum ofputil_protocol usable_protocols, protocol;
2165 struct cls_cursor cursor;
2166 struct classifier cls;
2167 struct list requests;
2168 struct vconn *vconn;
2171 classifier_init(&cls);
2172 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2174 protocol = open_vconn(argv[1], &vconn);
2175 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2177 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2179 list_init(&requests);
2181 /* Delete flows that exist on the switch but not in the file. */
2182 ovs_rwlock_rdlock(&cls.rwlock);
2183 cls_cursor_init(&cursor, &cls, NULL);
2184 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2185 struct fte_version *file_ver = fte->versions[FILE_IDX];
2186 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2188 if (sw_ver && !file_ver) {
2189 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2190 protocol, &requests);
2194 /* Add flows that exist in the file but not on the switch.
2195 * Update flows that exist in both places but differ. */
2196 cls_cursor_init(&cursor, &cls, NULL);
2197 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2198 struct fte_version *file_ver = fte->versions[FILE_IDX];
2199 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2202 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2203 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2206 ovs_rwlock_unlock(&cls.rwlock);
2207 transact_multiple_noreply(vconn, &requests);
2214 read_flows_from_source(const char *source, struct classifier *cls, int index)
2218 if (source[0] == '/' || source[0] == '.'
2219 || (!strchr(source, ':') && !stat(source, &s))) {
2220 read_flows_from_file(source, cls, index);
2222 enum ofputil_protocol protocol;
2223 struct vconn *vconn;
2225 protocol = open_vconn(source, &vconn);
2226 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2227 read_flows_from_switch(vconn, protocol, cls, index);
2233 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2235 bool differences = false;
2236 struct cls_cursor cursor;
2237 struct classifier cls;
2241 classifier_init(&cls);
2242 read_flows_from_source(argv[1], &cls, 0);
2243 read_flows_from_source(argv[2], &cls, 1);
2248 ovs_rwlock_rdlock(&cls.rwlock);
2249 cls_cursor_init(&cursor, &cls, NULL);
2250 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2251 struct fte_version *a = fte->versions[0];
2252 struct fte_version *b = fte->versions[1];
2254 if (!a || !b || !fte_version_equals(a, b)) {
2255 fte_version_format(fte, 0, &a_s);
2256 fte_version_format(fte, 1, &b_s);
2257 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2259 printf("-%s", ds_cstr(&a_s));
2262 printf("+%s", ds_cstr(&b_s));
2268 ovs_rwlock_unlock(&cls.rwlock);
2280 /* Undocumented commands for unit testing. */
2283 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
2285 enum ofputil_protocol usable_protocols;
2286 enum ofputil_protocol protocol = 0;
2290 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
2291 usable_s = ofputil_protocols_to_string(usable_protocols);
2292 printf("usable protocols: %s\n", usable_s);
2295 if (!(usable_protocols & allowed_protocols)) {
2296 ovs_fatal(0, "no usable protocol");
2298 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2300 if (protocol & usable_protocols & allowed_protocols) {
2304 ovs_assert(is_pow2(protocol));
2306 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2308 for (i = 0; i < n_fms; i++) {
2309 struct ofputil_flow_mod *fm = &fms[i];
2312 msg = ofputil_encode_flow_mod(fm, protocol);
2313 ofp_print(stdout, msg->data, msg->size, verbosity);
2320 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2321 * it back to stdout. */
2323 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2325 struct ofputil_flow_mod fm;
2328 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD);
2330 ovs_fatal(0, "%s", error);
2332 ofctl_parse_flows__(&fm, 1);
2335 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2336 * add-flows) and prints each of the flows back to stdout. */
2338 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2340 struct ofputil_flow_mod *fms = NULL;
2344 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
2346 ovs_fatal(0, "%s", error);
2348 ofctl_parse_flows__(fms, n_fms);
2353 ofctl_parse_nxm__(bool oxm)
2358 while (!ds_get_test_line(&in, stdin)) {
2359 struct ofpbuf nx_match;
2361 ovs_be64 cookie, cookie_mask;
2365 /* Convert string to nx_match. */
2366 ofpbuf_init(&nx_match, 0);
2368 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2370 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2373 /* Convert nx_match to match. */
2376 error = oxm_pull_match(&nx_match, &match);
2378 error = nx_pull_match(&nx_match, match_len, &match,
2379 &cookie, &cookie_mask);
2383 error = oxm_pull_match_loose(&nx_match, &match);
2385 error = nx_pull_match_loose(&nx_match, match_len, &match,
2386 &cookie, &cookie_mask);
2394 /* Convert match back to nx_match. */
2395 ofpbuf_uninit(&nx_match);
2396 ofpbuf_init(&nx_match, 0);
2398 match_len = oxm_put_match(&nx_match, &match);
2399 out = oxm_match_to_string(&nx_match, match_len);
2401 match_len = nx_put_match(&nx_match, &match,
2402 cookie, cookie_mask);
2403 out = nx_match_to_string(nx_match.data, match_len);
2409 printf("nx_pull_match() returned error %s\n",
2410 ofperr_get_name(error));
2413 ofpbuf_uninit(&nx_match);
2418 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2419 * stdin, does some internal fussing with them, and then prints them back as
2420 * strings on stdout. */
2422 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2424 return ofctl_parse_nxm__(false);
2427 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2428 * stdin, does some internal fussing with them, and then prints them back as
2429 * strings on stdout. */
2431 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2433 return ofctl_parse_nxm__(true);
2437 print_differences(const char *prefix,
2438 const void *a_, size_t a_len,
2439 const void *b_, size_t b_len)
2441 const uint8_t *a = a_;
2442 const uint8_t *b = b_;
2445 for (i = 0; i < MIN(a_len, b_len); i++) {
2447 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2448 prefix, i, a[i], b[i]);
2451 for (i = a_len; i < b_len; i++) {
2452 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2454 for (i = b_len; i < a_len; i++) {
2455 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2459 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2460 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2461 * on stdout, and then converts them back to hex bytes and prints any
2462 * differences from the input. */
2464 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2469 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2470 struct ofpbuf of10_out;
2471 struct ofpbuf of10_in;
2472 struct ofpbuf ofpacts;
2477 /* Parse hex bytes. */
2478 ofpbuf_init(&of10_in, 0);
2479 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2480 ovs_fatal(0, "Trailing garbage in hex data");
2483 /* Convert to ofpacts. */
2484 ofpbuf_init(&ofpacts, 0);
2485 size = of10_in.size;
2486 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2488 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2489 ofpbuf_uninit(&ofpacts);
2490 ofpbuf_uninit(&of10_in);
2493 ofpbuf_push_uninit(&of10_in, size);
2495 /* Print cls_rule. */
2497 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2501 /* Convert back to ofp10 actions and print differences from input. */
2502 ofpbuf_init(&of10_out, 0);
2503 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2505 print_differences("", of10_in.data, of10_in.size,
2506 of10_out.data, of10_out.size);
2509 ofpbuf_uninit(&ofpacts);
2510 ofpbuf_uninit(&of10_in);
2511 ofpbuf_uninit(&of10_out);
2516 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2517 * bytes from stdin, converts them to cls_rules, prints them as strings on
2518 * stdout, and then converts them back to hex bytes and prints any differences
2521 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2522 * values are ignored in the input and will be set to zero when OVS converts
2523 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2524 * it does the conversion to hex, to ensure that in fact they are ignored. */
2526 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2533 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2534 struct ofpbuf match_in, match_expout;
2535 struct ofp10_match match_out;
2536 struct ofp10_match match_normal;
2540 /* Parse hex bytes to use for expected output. */
2542 ds_put_cstr(&expout, ds_cstr(&in));
2543 for (p = ds_cstr(&expout); *p; p++) {
2548 ofpbuf_init(&match_expout, 0);
2549 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2550 ovs_fatal(0, "Trailing garbage in hex data");
2552 if (match_expout.size != sizeof(struct ofp10_match)) {
2553 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2554 match_expout.size, sizeof(struct ofp10_match));
2557 /* Parse hex bytes for input. */
2558 for (p = ds_cstr(&in); *p; p++) {
2560 *p = "0123456789abcdef"[random_uint32() & 0xf];
2563 ofpbuf_init(&match_in, 0);
2564 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2565 ovs_fatal(0, "Trailing garbage in hex data");
2567 if (match_in.size != sizeof(struct ofp10_match)) {
2568 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2569 match_in.size, sizeof(struct ofp10_match));
2572 /* Convert to cls_rule and print. */
2573 ofputil_match_from_ofp10_match(match_in.data, &match);
2574 match_print(&match);
2576 /* Convert back to ofp10_match and print differences from input. */
2577 ofputil_match_to_ofp10_match(&match, &match_out);
2578 print_differences("", match_expout.data, match_expout.size,
2579 &match_out, sizeof match_out);
2581 /* Normalize, then convert and compare again. */
2582 ofputil_normalize_match(&match);
2583 ofputil_match_to_ofp10_match(&match, &match_normal);
2584 print_differences("normal: ", &match_out, sizeof match_out,
2585 &match_normal, sizeof match_normal);
2588 ofpbuf_uninit(&match_in);
2589 ofpbuf_uninit(&match_expout);
2592 ds_destroy(&expout);
2595 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2596 * bytes from stdin, converts them to "struct match"es, prints them as strings
2597 * on stdout, and then converts them back to hex bytes and prints any
2598 * differences from the input. */
2600 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2605 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2606 struct ofpbuf match_in;
2607 struct ofp11_match match_out;
2611 /* Parse hex bytes. */
2612 ofpbuf_init(&match_in, 0);
2613 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2614 ovs_fatal(0, "Trailing garbage in hex data");
2616 if (match_in.size != sizeof(struct ofp11_match)) {
2617 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2618 match_in.size, sizeof(struct ofp11_match));
2621 /* Convert to match. */
2622 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2624 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2625 ofpbuf_uninit(&match_in);
2630 match_print(&match);
2632 /* Convert back to ofp11_match and print differences from input. */
2633 ofputil_match_to_ofp11_match(&match, &match_out);
2635 print_differences("", match_in.data, match_in.size,
2636 &match_out, sizeof match_out);
2639 ofpbuf_uninit(&match_in);
2644 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2645 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2646 * on stdout, and then converts them back to hex bytes and prints any
2647 * differences from the input. */
2649 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2654 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2655 struct ofpbuf of11_out;
2656 struct ofpbuf of11_in;
2657 struct ofpbuf ofpacts;
2662 /* Parse hex bytes. */
2663 ofpbuf_init(&of11_in, 0);
2664 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2665 ovs_fatal(0, "Trailing garbage in hex data");
2668 /* Convert to ofpacts. */
2669 ofpbuf_init(&ofpacts, 0);
2670 size = of11_in.size;
2671 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2674 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2675 ofpbuf_uninit(&ofpacts);
2676 ofpbuf_uninit(&of11_in);
2679 ofpbuf_push_uninit(&of11_in, size);
2681 /* Print cls_rule. */
2683 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2687 /* Convert back to ofp11 actions and print differences from input. */
2688 ofpbuf_init(&of11_out, 0);
2689 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2691 print_differences("", of11_in.data, of11_in.size,
2692 of11_out.data, of11_out.size);
2695 ofpbuf_uninit(&ofpacts);
2696 ofpbuf_uninit(&of11_in);
2697 ofpbuf_uninit(&of11_out);
2702 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2703 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2704 * them as strings on stdout, and then converts them back to hex bytes and
2705 * prints any differences from the input. */
2707 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2712 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2713 struct ofpbuf of11_out;
2714 struct ofpbuf of11_in;
2715 struct ofpbuf ofpacts;
2719 const char *table_id;
2722 /* Parse table_id separated with the follow-up instructions by ",", if
2724 instructions = ds_cstr(&in);
2726 if (strstr(instructions, ",")) {
2727 table_id = strsep(&instructions, ",");
2730 /* Parse hex bytes. */
2731 ofpbuf_init(&of11_in, 0);
2732 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
2733 ovs_fatal(0, "Trailing garbage in hex data");
2736 /* Convert to ofpacts. */
2737 ofpbuf_init(&ofpacts, 0);
2738 size = of11_in.size;
2739 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2742 /* Verify actions. */
2744 memset(&flow, 0, sizeof flow);
2745 error = ofpacts_check(ofpacts.data, ofpacts.size, &flow, OFPP_MAX,
2746 table_id ? atoi(table_id) : 0);
2749 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2750 ofpbuf_uninit(&ofpacts);
2751 ofpbuf_uninit(&of11_in);
2754 ofpbuf_push_uninit(&of11_in, size);
2756 /* Print cls_rule. */
2758 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2762 /* Convert back to ofp11 instructions and print differences from
2764 ofpbuf_init(&of11_out, 0);
2765 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2768 print_differences("", of11_in.data, of11_in.size,
2769 of11_out.data, of11_out.size);
2772 ofpbuf_uninit(&ofpacts);
2773 ofpbuf_uninit(&of11_in);
2774 ofpbuf_uninit(&of11_out);
2779 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2780 * mask values to and from various formats and prints the results. */
2782 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
2787 struct ofputil_flow_mod fm;
2790 struct match nxm_match;
2794 struct ofp10_match of10_raw;
2795 struct match of10_match;
2797 struct ofp11_match of11_raw;
2798 struct match of11_match;
2803 match_init_catchall(&match);
2804 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
2805 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
2807 /* Convert to and from string. */
2808 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
2809 printf("%s -> ", string_s);
2811 error_s = parse_ofp_str(&fm, -1, string_s);
2813 ovs_fatal(0, "%s", error_s);
2815 printf("%04"PRIx16"/%04"PRIx16"\n",
2816 ntohs(fm.match.flow.vlan_tci),
2817 ntohs(fm.match.wc.masks.vlan_tci));
2820 /* Convert to and from NXM. */
2821 ofpbuf_init(&nxm, 0);
2822 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
2823 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
2824 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
2825 printf("NXM: %s -> ", nxm_s);
2827 printf("%s\n", ofperr_to_string(error));
2829 printf("%04"PRIx16"/%04"PRIx16"\n",
2830 ntohs(nxm_match.flow.vlan_tci),
2831 ntohs(nxm_match.wc.masks.vlan_tci));
2834 ofpbuf_uninit(&nxm);
2836 /* Convert to and from OXM. */
2837 ofpbuf_init(&nxm, 0);
2838 nxm_match_len = oxm_put_match(&nxm, &match);
2839 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
2840 error = oxm_pull_match(&nxm, &nxm_match);
2841 printf("OXM: %s -> ", nxm_s);
2843 printf("%s\n", ofperr_to_string(error));
2845 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
2846 (VLAN_VID_MASK | VLAN_CFI);
2847 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
2848 (VLAN_VID_MASK | VLAN_CFI);
2850 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
2851 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
2852 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
2858 ofpbuf_uninit(&nxm);
2860 /* Convert to and from OpenFlow 1.0. */
2861 ofputil_match_to_ofp10_match(&match, &of10_raw);
2862 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
2863 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2864 ntohs(of10_raw.dl_vlan),
2865 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
2866 of10_raw.dl_vlan_pcp,
2867 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
2868 ntohs(of10_match.flow.vlan_tci),
2869 ntohs(of10_match.wc.masks.vlan_tci));
2871 /* Convert to and from OpenFlow 1.1. */
2872 ofputil_match_to_ofp11_match(&match, &of11_raw);
2873 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
2874 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2875 ntohs(of11_raw.dl_vlan),
2876 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
2877 of11_raw.dl_vlan_pcp,
2878 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
2879 ntohs(of11_match.flow.vlan_tci),
2880 ntohs(of11_match.wc.masks.vlan_tci));
2883 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2886 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2891 error = ofperr_from_name(argv[1]);
2893 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2896 for (version = 0; version <= UINT8_MAX; version++) {
2897 const char *name = ofperr_domain_get_name(version);
2899 int vendor = ofperr_get_vendor(error, version);
2900 int type = ofperr_get_type(error, version);
2901 int code = ofperr_get_code(error, version);
2903 if (vendor != -1 || type != -1 || code != -1) {
2904 printf("%s: vendor %#x, type %d, code %d\n",
2905 name, vendor, type, code);
2911 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
2912 * error named ENUM and prints the error reply in hex. */
2914 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
2916 const struct ofp_header *oh;
2917 struct ofpbuf request, *reply;
2920 error = ofperr_from_name(argv[1]);
2922 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2925 ofpbuf_init(&request, 0);
2926 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
2927 ovs_fatal(0, "Trailing garbage in hex data");
2929 if (request.size < sizeof(struct ofp_header)) {
2930 ovs_fatal(0, "Request too short");
2934 if (request.size != ntohs(oh->length)) {
2935 ovs_fatal(0, "Request size inconsistent");
2938 reply = ofperr_encode_reply(error, request.data);
2939 ofpbuf_uninit(&request);
2941 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
2942 ofpbuf_delete(reply);
2945 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2946 * binary data, interpreting them as an OpenFlow message, and prints the
2947 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2949 ofctl_ofp_print(int argc, char *argv[])
2951 struct ofpbuf packet;
2953 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2954 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2955 ovs_fatal(0, "trailing garbage following hex bytes");
2957 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2958 ofpbuf_uninit(&packet);
2961 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
2962 * and dumps each message in hex. */
2964 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
2966 uint32_t bitmap = strtol(argv[1], NULL, 0);
2967 struct ofpbuf *hello;
2969 hello = ofputil_encode_hello(bitmap);
2970 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
2971 ofp_print(stdout, hello->data, hello->size, verbosity);
2972 ofpbuf_delete(hello);
2975 static const struct command all_commands[] = {
2976 { "show", 1, 1, ofctl_show },
2977 { "monitor", 1, 3, ofctl_monitor },
2978 { "snoop", 1, 1, ofctl_snoop },
2979 { "dump-desc", 1, 1, ofctl_dump_desc },
2980 { "dump-tables", 1, 1, ofctl_dump_tables },
2981 { "dump-flows", 1, 2, ofctl_dump_flows },
2982 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2983 { "queue-stats", 1, 3, ofctl_queue_stats },
2984 { "add-flow", 2, 2, ofctl_add_flow },
2985 { "add-flows", 2, 2, ofctl_add_flows },
2986 { "mod-flows", 2, 2, ofctl_mod_flows },
2987 { "del-flows", 1, 2, ofctl_del_flows },
2988 { "replace-flows", 2, 2, ofctl_replace_flows },
2989 { "diff-flows", 2, 2, ofctl_diff_flows },
2990 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2991 { "dump-ports", 1, 2, ofctl_dump_ports },
2992 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2993 { "mod-port", 3, 3, ofctl_mod_port },
2994 { "get-frags", 1, 1, ofctl_get_frags },
2995 { "set-frags", 2, 2, ofctl_set_frags },
2996 { "ofp-parse", 1, 1, ofctl_ofp_parse },
2997 { "probe", 1, 1, ofctl_probe },
2998 { "ping", 1, 2, ofctl_ping },
2999 { "benchmark", 3, 3, ofctl_benchmark },
3000 { "help", 0, INT_MAX, ofctl_help },
3002 /* Undocumented commands for testing. */
3003 { "parse-flow", 1, 1, ofctl_parse_flow },
3004 { "parse-flows", 1, 1, ofctl_parse_flows },
3005 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3006 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3007 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3008 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3009 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3010 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3011 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3012 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3013 { "check-vlan", 2, 2, ofctl_check_vlan },
3014 { "print-error", 1, 1, ofctl_print_error },
3015 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3016 { "ofp-print", 1, 2, ofctl_ofp_print },
3017 { "encode-hello", 1, 1, ofctl_encode_hello },
3019 { NULL, 0, 0, NULL },
3022 static const struct command *get_all_commands(void)
3024 return all_commands;