2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 all_commands[];
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, 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 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);
174 unsigned long int timeout;
177 c = getopt_long(argc, argv, short_options, long_options, NULL);
184 timeout = strtoul(optarg, NULL, 10);
186 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
194 allowed_protocols = ofputil_protocols_from_string(optarg);
195 if (!allowed_protocols) {
196 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
201 preferred_packet_in_format =
202 ofputil_packet_in_format_from_string(optarg);
203 if (preferred_packet_in_format < 0) {
204 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
228 add_sort_criterion(SORT_ASC, optarg);
232 add_sort_criterion(SORT_DESC, optarg);
235 DAEMON_OPTION_HANDLERS
236 OFP_VERSION_OPTION_HANDLERS
238 STREAM_SSL_OPTION_HANDLERS
249 /* Always do a final sort pass based on priority. */
250 add_sort_criterion(SORT_DESC, "priority");
259 printf("%s: OpenFlow switch management utility\n"
260 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
261 "\nFor OpenFlow switches:\n"
262 " show SWITCH show OpenFlow information\n"
263 " dump-desc SWITCH print switch description\n"
264 " dump-tables SWITCH print table stats\n"
265 " mod-port SWITCH IFACE ACT modify port behavior\n"
266 " get-frags SWITCH print fragment handling behavior\n"
267 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
268 " dump-ports SWITCH [PORT] print port statistics\n"
269 " dump-ports-desc SWITCH print port descriptions\n"
270 " dump-flows SWITCH print all flow entries\n"
271 " dump-flows SWITCH FLOW print matching FLOWs\n"
272 " dump-aggregate SWITCH print aggregate flow statistics\n"
273 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
274 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
275 " add-flow SWITCH FLOW add flow described by FLOW\n"
276 " add-flows SWITCH FILE add flows from FILE\n"
277 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
278 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
279 " replace-flows SWITCH FILE replace flows with those in FILE\n"
280 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
281 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
282 " execute ACTIONS on PACKET\n"
283 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
284 " print packets received from SWITCH\n"
285 " snoop SWITCH snoop on SWITCH and its controller\n"
286 "\nFor OpenFlow switches and controllers:\n"
287 " probe TARGET probe whether TARGET is up\n"
288 " ping TARGET [N] latency of N-byte echos\n"
289 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
290 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
291 program_name, program_name);
292 vconn_usage(true, false, false);
296 printf("\nOther options:\n"
297 " --strict use strict match for flow commands\n"
298 " --readd replace flows that haven't changed\n"
299 " -F, --flow-format=FORMAT force particular flow format\n"
300 " -P, --packet-in-format=FRMT force particular packet in format\n"
301 " -m, --more be more verbose printing OpenFlow\n"
302 " --timestamp (monitor, snoop) print timestamps\n"
303 " -t, --timeout=SECS give up after SECS seconds\n"
304 " --sort[=field] sort in ascending order\n"
305 " --rsort[=field] sort in descending order\n"
306 " -h, --help display this help message\n"
307 " -V, --version display version information\n");
312 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
313 const char *argv[] OVS_UNUSED, void *exiting_)
315 bool *exiting = exiting_;
317 unixctl_command_reply(conn, NULL);
320 static void run(int retval, const char *message, ...)
324 run(int retval, const char *message, ...)
329 va_start(args, message);
330 ovs_fatal_valist(retval, message, args);
334 /* Generic commands. */
337 open_vconn_socket(const char *name, struct vconn **vconnp)
339 char *vconn_name = xasprintf("unix:%s", name);
342 error = vconn_open(vconn_name, get_allowed_ofp_versions(), vconnp,
344 if (error && error != ENOENT) {
345 ovs_fatal(0, "%s: failed to open socket (%s)", name,
353 static enum ofputil_protocol
354 open_vconn__(const char *name, const char *default_suffix,
355 struct vconn **vconnp)
357 char *datapath_name, *datapath_type, *socket_name;
358 enum ofputil_protocol protocol;
363 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
365 ofproto_parse_name(name, &datapath_name, &datapath_type);
366 socket_name = xasprintf("%s/%s.%s",
367 ovs_rundir(), datapath_name, default_suffix);
371 if (strchr(name, ':')) {
372 run(vconn_open_block(name, get_allowed_ofp_versions(), vconnp),
373 "connecting to %s", name);
374 } else if (!open_vconn_socket(name, vconnp)) {
376 } else if (!open_vconn_socket(bridge_path, vconnp)) {
378 } else if (!open_vconn_socket(socket_name, vconnp)) {
381 ovs_fatal(0, "%s is not a bridge or a socket", name);
387 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
388 error = vconn_connect_block(*vconnp);
390 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
394 ofp_version = vconn_get_version(*vconnp);
395 protocol = ofputil_protocol_from_ofp_version(ofp_version);
397 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
403 static enum ofputil_protocol
404 open_vconn(const char *name, struct vconn **vconnp)
406 return open_vconn__(name, "mgmt", vconnp);
410 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
412 ofpmsg_update_length(buffer);
413 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
417 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
419 struct ofpbuf *reply;
421 ofpmsg_update_length(request);
422 run(vconn_transact(vconn, request, &reply), "talking to %s",
423 vconn_get_name(vconn));
424 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
425 ofpbuf_delete(reply);
429 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
431 struct ofpbuf *request;
434 open_vconn(vconn_name, &vconn);
435 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
436 dump_transaction(vconn, request);
441 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
443 const struct ofp_header *request_oh = request->data;
444 ovs_be32 send_xid = request_oh->xid;
445 enum ofpraw request_raw;
446 enum ofpraw reply_raw;
449 ofpraw_decode_partial(&request_raw, request->data, request->size);
450 reply_raw = ofpraw_stats_request_to_reply(request_raw,
451 request_oh->version);
453 send_openflow_buffer(vconn, request);
456 struct ofpbuf *reply;
458 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
459 recv_xid = ((struct ofp_header *) reply->data)->xid;
460 if (send_xid == recv_xid) {
463 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
465 ofpraw_decode(&raw, reply->data);
466 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
468 } else if (raw == reply_raw) {
469 done = !ofpmp_more(reply->data);
471 ovs_fatal(0, "received bad reply: %s",
472 ofp_to_string(reply->data, reply->size,
476 VLOG_DBG("received reply with xid %08"PRIx32" "
477 "!= expected %08"PRIx32, recv_xid, send_xid);
479 ofpbuf_delete(reply);
484 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
486 struct ofpbuf *request;
489 open_vconn(vconn_name, &vconn);
490 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
491 dump_stats_transaction(vconn, request);
495 /* Sends 'request', which should be a request that only has a reply if an error
496 * occurs, and waits for it to succeed or fail. If an error does occur, prints
497 * it and exits with an error.
499 * Destroys all of the 'requests'. */
501 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
503 struct ofpbuf *request, *reply;
505 LIST_FOR_EACH (request, list_node, requests) {
506 ofpmsg_update_length(request);
509 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
510 "talking to %s", vconn_get_name(vconn));
512 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
515 ofpbuf_delete(reply);
518 /* Sends 'request', which should be a request that only has a reply if an error
519 * occurs, and waits for it to succeed or fail. If an error does occur, prints
520 * it and exits with an error.
522 * Destroys 'request'. */
524 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
526 struct list requests;
528 list_init(&requests);
529 list_push_back(&requests, &request->list_node);
530 transact_multiple_noreply(vconn, &requests);
534 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
536 struct ofp_switch_config *config;
537 struct ofpbuf *request;
538 struct ofpbuf *reply;
541 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
542 vconn_get_version(vconn), 0);
543 run(vconn_transact(vconn, request, &reply),
544 "talking to %s", vconn_get_name(vconn));
546 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
547 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
550 config = ofpbuf_pull(reply, sizeof *config);
553 ofpbuf_delete(reply);
557 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
559 struct ofpbuf *request;
561 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
562 ofpbuf_put(request, config, sizeof *config);
564 transact_noreply(vconn, request);
568 ofctl_show(int argc OVS_UNUSED, char *argv[])
570 const char *vconn_name = argv[1];
572 struct ofpbuf *request;
573 struct ofpbuf *reply;
576 open_vconn(vconn_name, &vconn);
577 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
578 vconn_get_version(vconn), 0);
579 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
581 trunc = ofputil_switch_features_ports_trunc(reply);
582 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
584 ofpbuf_delete(reply);
587 /* The Features Reply may not contain all the ports, so send a
588 * Port Description stats request, which doesn't have size
590 dump_trivial_stats_transaction(vconn_name,
591 OFPRAW_OFPST_PORT_DESC_REQUEST);
593 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
598 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
600 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
604 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
606 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
610 fetch_port_by_features(const char *vconn_name,
611 const char *port_name, unsigned int port_no,
612 struct ofputil_phy_port *pp, bool *trunc)
614 struct ofputil_switch_features features;
615 const struct ofp_header *oh;
616 struct ofpbuf *request, *reply;
623 /* Fetch the switch's ofp_switch_features. */
624 open_vconn(vconn_name, &vconn);
625 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
626 vconn_get_version(vconn), 0);
627 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
631 if (ofptype_decode(&type, reply->data)
632 || type != OFPTYPE_FEATURES_REPLY) {
633 ovs_fatal(0, "%s: received bad features reply", vconn_name);
637 if (ofputil_switch_features_ports_trunc(reply)) {
642 error = ofputil_decode_switch_features(oh, &features, &b);
644 ovs_fatal(0, "%s: failed to decode features reply (%s)",
645 vconn_name, ofperr_to_string(error));
648 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
649 if (port_no != UINT_MAX
650 ? port_no == pp->port_no
651 : !strcmp(pp->name, port_name)) {
658 ofpbuf_delete(reply);
663 fetch_port_by_stats(const char *vconn_name,
664 const char *port_name, unsigned int port_no,
665 struct ofputil_phy_port *pp)
667 struct ofpbuf *request;
673 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
674 send_xid = ((struct ofp_header *) request->data)->xid;
676 open_vconn(vconn_name, &vconn);
677 send_openflow_buffer(vconn, request);
680 struct ofpbuf *reply;
682 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
683 recv_xid = ((struct ofp_header *) reply->data)->xid;
684 if (send_xid == recv_xid) {
685 struct ofp_header *oh = reply->data;
690 ofpbuf_use_const(&b, oh, ntohs(oh->length));
691 if (ofptype_pull(&type, &b)
692 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
693 ovs_fatal(0, "received bad reply: %s",
694 ofp_to_string(reply->data, reply->size,
698 flags = ofpmp_flags(oh);
699 done = !(flags & OFPSF_REPLY_MORE);
702 /* We've already found the port, but we need to drain
703 * the queue of any other replies for this request. */
707 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
708 if (port_no != UINT_MAX ? port_no == pp->port_no
709 : !strcmp(pp->name, port_name)) {
715 VLOG_DBG("received reply with xid %08"PRIx32" "
716 "!= expected %08"PRIx32, recv_xid, send_xid);
718 ofpbuf_delete(reply);
726 /* Opens a connection to 'vconn_name', fetches the port structure for
727 * 'port_name' (which may be a port name or number), and copies it into
730 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
731 struct ofputil_phy_port *pp)
733 unsigned int port_no;
737 /* Try to interpret the argument as a port number. */
738 if (!str_to_uint(port_name, 10, &port_no)) {
742 /* Try to find the port based on the Features Reply. If it looks
743 * like the results may be truncated, then use the Port Description
744 * stats message introduced in OVS 1.7. */
745 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
748 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
752 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
756 /* Returns the port number corresponding to 'port_name' (which may be a port
757 * name or number) within the switch 'vconn_name'. */
759 str_to_port_no(const char *vconn_name, const char *port_name)
763 if (ofputil_port_from_string(port_name, &port_no)) {
766 struct ofputil_phy_port pp;
768 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
774 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
775 enum ofputil_protocol *cur)
778 struct ofpbuf *request, *reply;
779 enum ofputil_protocol next;
781 request = ofputil_encode_set_protocol(*cur, want, &next);
786 run(vconn_transact_noreply(vconn, request, &reply),
787 "talking to %s", vconn_get_name(vconn));
789 char *s = ofp_to_string(reply->data, reply->size, 2);
790 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
791 vconn_get_name(vconn), s);
793 ofpbuf_delete(reply);
801 static enum ofputil_protocol
802 set_protocol_for_flow_dump(struct vconn *vconn,
803 enum ofputil_protocol cur_protocol,
804 enum ofputil_protocol usable_protocols)
809 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
810 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
811 if (f & usable_protocols & allowed_protocols
812 && try_set_protocol(vconn, f, &cur_protocol)) {
817 usable_s = ofputil_protocols_to_string(usable_protocols);
818 if (usable_protocols & allowed_protocols) {
819 ovs_fatal(0, "switch does not support any of the usable flow "
820 "formats (%s)", usable_s);
822 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
823 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
824 "allowed flow formats (%s)", usable_s, allowed_s);
828 static struct vconn *
829 prepare_dump_flows(int argc, char *argv[], bool aggregate,
830 struct ofpbuf **requestp)
832 enum ofputil_protocol usable_protocols, protocol;
833 struct ofputil_flow_stats_request fsr;
836 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
837 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
839 protocol = open_vconn(argv[1], &vconn);
840 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
841 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
846 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
848 struct ofpbuf *request;
851 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
852 dump_stats_transaction(vconn, request);
857 compare_flows(const void *afs_, const void *bfs_)
859 const struct ofputil_flow_stats *afs = afs_;
860 const struct ofputil_flow_stats *bfs = bfs_;
861 const struct match *a = &afs->match;
862 const struct match *b = &bfs->match;
863 const struct sort_criterion *sc;
865 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
866 const struct mf_field *f = sc->field;
870 unsigned int a_pri = afs->priority;
871 unsigned int b_pri = bfs->priority;
872 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
876 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
877 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
879 /* Skip the test for sc->order, so that missing fields always
880 * sort to the end whether we're sorting in ascending or
881 * descending order. */
884 union mf_value aval, bval;
886 mf_get_value(f, &a->flow, &aval);
887 mf_get_value(f, &b->flow, &bval);
888 ret = memcmp(&aval, &bval, f->n_bytes);
893 return sc->order == SORT_ASC ? ret : -ret;
901 ofctl_dump_flows(int argc, char *argv[])
904 return ofctl_dump_flows__(argc, argv, false);
906 struct ofputil_flow_stats *fses;
907 size_t n_fses, allocated_fses;
908 struct ofpbuf *request;
909 struct ofpbuf ofpacts;
910 struct ofpbuf *reply;
916 vconn = prepare_dump_flows(argc, argv, false, &request);
917 send_xid = ((struct ofp_header *) request->data)->xid;
918 send_openflow_buffer(vconn, request);
921 n_fses = allocated_fses = 0;
923 ofpbuf_init(&ofpacts, 0);
925 struct ofputil_flow_stats *fs;
927 if (n_fses >= allocated_fses) {
928 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
932 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
936 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
939 ofpbuf_uninit(&ofpacts);
941 qsort(fses, n_fses, sizeof *fses, compare_flows);
944 for (i = 0; i < n_fses; i++) {
946 ofp_print_flow_stats(&s, &fses[i]);
951 for (i = 0; i < n_fses; i++) {
952 free(fses[i].ofpacts);
961 ofctl_dump_aggregate(int argc, char *argv[])
963 return ofctl_dump_flows__(argc, argv, true);
967 ofctl_queue_stats(int argc, char *argv[])
969 struct ofpbuf *request;
971 struct ofputil_queue_stats_request oqs;
973 open_vconn(argv[1], &vconn);
975 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
976 oqs.port_no = str_to_port_no(argv[1], argv[2]);
978 oqs.port_no = OFPP_ANY;
980 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
981 oqs.queue_id = atoi(argv[3]);
983 oqs.queue_id = OFPQ_ALL;
986 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
987 dump_stats_transaction(vconn, request);
991 static enum ofputil_protocol
992 open_vconn_for_flow_mod(const char *remote,
993 const struct ofputil_flow_mod *fms, size_t n_fms,
994 struct vconn **vconnp)
996 enum ofputil_protocol usable_protocols;
997 enum ofputil_protocol cur_protocol;
1001 /* Figure out what flow formats will work. */
1002 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1003 if (!(usable_protocols & allowed_protocols)) {
1004 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1005 usable_s = ofputil_protocols_to_string(usable_protocols);
1006 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1007 "allowed flow formats (%s)", usable_s, allowed_s);
1010 /* If the initial flow format is allowed and usable, keep it. */
1011 cur_protocol = open_vconn(remote, vconnp);
1012 if (usable_protocols & allowed_protocols & cur_protocol) {
1013 return cur_protocol;
1016 /* Otherwise try each flow format in turn. */
1017 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1018 enum ofputil_protocol f = 1 << i;
1020 if (f != cur_protocol
1021 && f & usable_protocols & allowed_protocols
1022 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1027 usable_s = ofputil_protocols_to_string(usable_protocols);
1028 ovs_fatal(0, "switch does not support any of the usable flow "
1029 "formats (%s)", usable_s);
1033 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1036 enum ofputil_protocol protocol;
1037 struct vconn *vconn;
1040 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
1042 for (i = 0; i < n_fms; i++) {
1043 struct ofputil_flow_mod *fm = &fms[i];
1045 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1052 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1054 struct ofputil_flow_mod *fms = NULL;
1057 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
1058 ofctl_flow_mod__(argv[1], fms, n_fms);
1063 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1065 if (argc > 2 && !strcmp(argv[2], "-")) {
1066 ofctl_flow_mod_file(argc, argv, command);
1068 struct ofputil_flow_mod fm;
1069 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
1070 ofctl_flow_mod__(argv[1], &fm, 1);
1075 ofctl_add_flow(int argc, char *argv[])
1077 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1081 ofctl_add_flows(int argc, char *argv[])
1083 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1087 ofctl_mod_flows(int argc, char *argv[])
1089 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1093 ofctl_del_flows(int argc, char *argv[])
1095 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1099 set_packet_in_format(struct vconn *vconn,
1100 enum nx_packet_in_format packet_in_format)
1102 struct ofpbuf *spif;
1104 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1106 transact_noreply(vconn, spif);
1107 VLOG_DBG("%s: using user-specified packet in format %s",
1108 vconn_get_name(vconn),
1109 ofputil_packet_in_format_to_string(packet_in_format));
1113 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1115 struct ofp_switch_config config;
1116 enum ofp_config_flags flags;
1118 fetch_switch_config(vconn, &config);
1119 flags = ntohs(config.flags);
1120 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1121 /* Set the invalid ttl config. */
1122 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1124 config.flags = htons(flags);
1125 set_switch_config(vconn, &config);
1127 /* Then retrieve the configuration to see if it really took. OpenFlow
1128 * doesn't define error reporting for bad modes, so this is all we can
1130 fetch_switch_config(vconn, &config);
1131 flags = ntohs(config.flags);
1132 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1133 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1134 "switch probably doesn't support mode)");
1141 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1142 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1143 * an error message and stores NULL in '*msgp'. */
1145 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1147 struct ofp_header *oh;
1150 msg = ofpbuf_new(strlen(hex) / 2);
1153 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1155 return "Trailing garbage in hex data";
1158 if (msg->size < sizeof(struct ofp_header)) {
1160 return "Message too short for OpenFlow";
1164 if (msg->size != ntohs(oh->length)) {
1166 return "Message size does not match length in OpenFlow header";
1174 ofctl_send(struct unixctl_conn *conn, int argc,
1175 const char *argv[], void *vconn_)
1177 struct vconn *vconn = vconn_;
1184 for (i = 1; i < argc; i++) {
1185 const char *error_msg;
1189 error_msg = openflow_from_hex(argv[i], &msg);
1191 ds_put_format(&reply, "%s\n", error_msg);
1196 fprintf(stderr, "send: ");
1197 ofp_print(stderr, msg->data, msg->size, verbosity);
1199 error = vconn_send_block(vconn, msg);
1202 ds_put_format(&reply, "%s\n", strerror(error));
1205 ds_put_cstr(&reply, "sent\n");
1210 unixctl_command_reply(conn, ds_cstr(&reply));
1212 unixctl_command_reply_error(conn, ds_cstr(&reply));
1217 struct barrier_aux {
1218 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1219 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1223 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1224 const char *argv[] OVS_UNUSED, void *aux_)
1226 struct barrier_aux *aux = aux_;
1231 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1235 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1236 error = vconn_send_block(aux->vconn, msg);
1239 unixctl_command_reply_error(conn, strerror(error));
1246 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1247 const char *argv[], void *aux OVS_UNUSED)
1251 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1253 unixctl_command_reply_error(conn, strerror(errno));
1258 dup2(fd, STDERR_FILENO);
1260 unixctl_command_reply(conn, NULL);
1264 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1265 const char *argv[] OVS_UNUSED, void *blocked_)
1267 bool *blocked = blocked_;
1271 unixctl_command_reply(conn, NULL);
1273 unixctl_command_reply(conn, "already blocking");
1278 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1279 const char *argv[] OVS_UNUSED, void *blocked_)
1281 bool *blocked = blocked_;
1285 unixctl_command_reply(conn, NULL);
1287 unixctl_command_reply(conn, "already unblocked");
1292 monitor_vconn(struct vconn *vconn)
1294 struct barrier_aux barrier_aux = { vconn, NULL };
1295 struct unixctl_server *server;
1296 bool exiting = false;
1297 bool blocked = false;
1300 daemon_save_fd(STDERR_FILENO);
1302 error = unixctl_server_create(NULL, &server);
1304 ovs_fatal(error, "failed to create unixctl server");
1306 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1307 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1309 unixctl_command_register("ofctl/barrier", "", 0, 0,
1310 ofctl_barrier, &barrier_aux);
1311 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1312 ofctl_set_output_file, NULL);
1314 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1315 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1318 daemonize_complete();
1324 unixctl_server_run(server);
1329 retval = vconn_recv(vconn, &b);
1330 if (retval == EAGAIN) {
1333 run(retval, "vconn_recv");
1336 time_t now = time_wall();
1339 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1343 ofptype_decode(&type, b->data);
1344 ofp_print(stderr, b->data, b->size, verbosity + 2);
1347 if (barrier_aux.conn && type == OFPTYPE_BARRIER_REPLY) {
1348 unixctl_command_reply(barrier_aux.conn, NULL);
1349 barrier_aux.conn = NULL;
1358 vconn_run_wait(vconn);
1360 vconn_recv_wait(vconn);
1362 unixctl_server_wait(server);
1366 unixctl_server_destroy(server);
1370 ofctl_monitor(int argc, char *argv[])
1372 struct vconn *vconn;
1375 open_vconn(argv[1], &vconn);
1376 for (i = 2; i < argc; i++) {
1377 const char *arg = argv[i];
1379 if (isdigit((unsigned char) *arg)) {
1380 struct ofp_switch_config config;
1382 fetch_switch_config(vconn, &config);
1383 config.miss_send_len = htons(atoi(arg));
1384 set_switch_config(vconn, &config);
1385 } else if (!strcmp(arg, "invalid_ttl")) {
1386 monitor_set_invalid_ttl_to_controller(vconn);
1387 } else if (!strncmp(arg, "watch:", 6)) {
1388 struct ofputil_flow_monitor_request fmr;
1391 parse_flow_monitor_request(&fmr, arg + 6);
1393 msg = ofpbuf_new(0);
1394 ofputil_append_flow_monitor_request(&fmr, msg);
1395 dump_stats_transaction(vconn, msg);
1397 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1401 if (preferred_packet_in_format >= 0) {
1402 set_packet_in_format(vconn, preferred_packet_in_format);
1404 struct ofpbuf *spif, *reply;
1406 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1408 run(vconn_transact_noreply(vconn, spif, &reply),
1409 "talking to %s", vconn_get_name(vconn));
1411 char *s = ofp_to_string(reply->data, reply->size, 2);
1412 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1413 " replied: %s. Falling back to the switch default.",
1414 vconn_get_name(vconn), s);
1416 ofpbuf_delete(reply);
1420 monitor_vconn(vconn);
1424 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1426 struct vconn *vconn;
1428 open_vconn__(argv[1], "snoop", &vconn);
1429 monitor_vconn(vconn);
1433 ofctl_dump_ports(int argc, char *argv[])
1435 struct ofpbuf *request;
1436 struct vconn *vconn;
1439 open_vconn(argv[1], &vconn);
1440 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1441 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1442 dump_stats_transaction(vconn, request);
1447 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1449 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1453 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1455 struct ofpbuf *request;
1456 struct vconn *vconn;
1457 struct ofpbuf *reply;
1459 open_vconn(argv[1], &vconn);
1460 request = make_echo_request(vconn_get_version(vconn));
1461 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1462 if (reply->size != sizeof(struct ofp_header)) {
1463 ovs_fatal(0, "reply does not match request");
1465 ofpbuf_delete(reply);
1470 ofctl_packet_out(int argc, char *argv[])
1472 enum ofputil_protocol protocol;
1473 struct ofputil_packet_out po;
1474 struct ofpbuf ofpacts;
1475 struct vconn *vconn;
1478 ofpbuf_init(&ofpacts, 64);
1479 parse_ofpacts(argv[3], &ofpacts);
1481 po.buffer_id = UINT32_MAX;
1482 po.in_port = str_to_port_no(argv[1], argv[2]);
1483 po.ofpacts = ofpacts.data;
1484 po.ofpacts_len = ofpacts.size;
1486 protocol = open_vconn(argv[1], &vconn);
1487 for (i = 4; i < argc; i++) {
1488 struct ofpbuf *packet, *opo;
1489 const char *error_msg;
1491 error_msg = eth_from_hex(argv[i], &packet);
1493 ovs_fatal(0, "%s", error_msg);
1496 po.packet = packet->data;
1497 po.packet_len = packet->size;
1498 opo = ofputil_encode_packet_out(&po, protocol);
1499 transact_noreply(vconn, opo);
1500 ofpbuf_delete(packet);
1503 ofpbuf_uninit(&ofpacts);
1507 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1509 struct ofp_config_flag {
1510 const char *name; /* The flag's name. */
1511 enum ofputil_port_config bit; /* Bit to turn on or off. */
1512 bool on; /* Value to set the bit to. */
1514 static const struct ofp_config_flag flags[] = {
1515 { "up", OFPUTIL_PC_PORT_DOWN, false },
1516 { "down", OFPUTIL_PC_PORT_DOWN, true },
1517 { "stp", OFPUTIL_PC_NO_STP, false },
1518 { "receive", OFPUTIL_PC_NO_RECV, false },
1519 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1520 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1521 { "forward", OFPUTIL_PC_NO_FWD, false },
1522 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1525 const struct ofp_config_flag *flag;
1526 enum ofputil_protocol protocol;
1527 struct ofputil_port_mod pm;
1528 struct ofputil_phy_port pp;
1529 struct vconn *vconn;
1530 const char *command;
1533 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1535 pm.port_no = pp.port_no;
1536 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1541 if (!strncasecmp(argv[3], "no-", 3)) {
1542 command = argv[3] + 3;
1544 } else if (!strncasecmp(argv[3], "no", 2)) {
1545 command = argv[3] + 2;
1551 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1552 if (!strcasecmp(command, flag->name)) {
1553 pm.mask = flag->bit;
1554 pm.config = flag->on ^ not ? flag->bit : 0;
1558 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1561 protocol = open_vconn(argv[1], &vconn);
1562 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1567 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1569 struct ofp_switch_config config;
1570 struct vconn *vconn;
1572 open_vconn(argv[1], &vconn);
1573 fetch_switch_config(vconn, &config);
1574 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1579 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1581 struct ofp_switch_config config;
1582 enum ofp_config_flags mode;
1583 struct vconn *vconn;
1586 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1587 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1590 open_vconn(argv[1], &vconn);
1591 fetch_switch_config(vconn, &config);
1592 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1593 if (flags != config.flags) {
1594 /* Set the configuration. */
1595 config.flags = flags;
1596 set_switch_config(vconn, &config);
1598 /* Then retrieve the configuration to see if it really took. OpenFlow
1599 * doesn't define error reporting for bad modes, so this is all we can
1601 fetch_switch_config(vconn, &config);
1602 if (flags != config.flags) {
1603 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1604 "switch probably doesn't support mode \"%s\")",
1605 argv[1], ofputil_frag_handling_to_string(mode));
1612 ofctl_ping(int argc, char *argv[])
1614 size_t max_payload = 65535 - sizeof(struct ofp_header);
1615 unsigned int payload;
1616 struct vconn *vconn;
1619 payload = argc > 2 ? atoi(argv[2]) : 64;
1620 if (payload > max_payload) {
1621 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1624 open_vconn(argv[1], &vconn);
1625 for (i = 0; i < 10; i++) {
1626 struct timeval start, end;
1627 struct ofpbuf *request, *reply;
1628 const struct ofp_header *rpy_hdr;
1631 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1632 vconn_get_version(vconn), payload);
1633 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1635 xgettimeofday(&start);
1636 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1637 xgettimeofday(&end);
1639 rpy_hdr = reply->data;
1640 if (ofptype_pull(&type, reply)
1641 || type != OFPTYPE_ECHO_REPLY
1642 || reply->size != payload
1643 || memcmp(request->l3, reply->l3, payload)) {
1644 printf("Reply does not match request. Request:\n");
1645 ofp_print(stdout, request, request->size, verbosity + 2);
1647 ofp_print(stdout, reply, reply->size, verbosity + 2);
1649 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1650 reply->size, argv[1], ntohl(rpy_hdr->xid),
1651 (1000*(double)(end.tv_sec - start.tv_sec))
1652 + (.001*(end.tv_usec - start.tv_usec)));
1653 ofpbuf_delete(request);
1654 ofpbuf_delete(reply);
1660 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1662 size_t max_payload = 65535 - sizeof(struct ofp_header);
1663 struct timeval start, end;
1664 unsigned int payload_size, message_size;
1665 struct vconn *vconn;
1670 payload_size = atoi(argv[2]);
1671 if (payload_size > max_payload) {
1672 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1674 message_size = sizeof(struct ofp_header) + payload_size;
1676 count = atoi(argv[3]);
1678 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1679 count, message_size, count * message_size);
1681 open_vconn(argv[1], &vconn);
1682 xgettimeofday(&start);
1683 for (i = 0; i < count; i++) {
1684 struct ofpbuf *request, *reply;
1686 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1687 vconn_get_version(vconn), payload_size);
1688 ofpbuf_put_zeros(request, payload_size);
1689 run(vconn_transact(vconn, request, &reply), "transact");
1690 ofpbuf_delete(reply);
1692 xgettimeofday(&end);
1695 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1696 + (.001*(end.tv_usec - start.tv_usec)));
1697 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1698 duration, count / (duration / 1000.0),
1699 count * message_size / (duration / 1000.0));
1703 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1708 /* replace-flows and diff-flows commands. */
1710 /* A flow table entry, possibly with two different versions. */
1712 struct cls_rule rule; /* Within a "struct classifier". */
1713 struct fte_version *versions[2];
1716 /* One version of a Flow Table Entry. */
1717 struct fte_version {
1719 uint16_t idle_timeout;
1720 uint16_t hard_timeout;
1722 struct ofpact *ofpacts;
1726 /* Frees 'version' and the data that it owns. */
1728 fte_version_free(struct fte_version *version)
1731 free(version->ofpacts);
1736 /* Returns true if 'a' and 'b' are the same, false if they differ.
1738 * Ignores differences in 'flags' because there's no way to retrieve flags from
1739 * an OpenFlow switch. We have to assume that they are the same. */
1741 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1743 return (a->cookie == b->cookie
1744 && a->idle_timeout == b->idle_timeout
1745 && a->hard_timeout == b->hard_timeout
1746 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1747 b->ofpacts, b->ofpacts_len));
1750 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
1751 * 'index' into 's', followed by a new-line. */
1753 fte_version_format(const struct fte *fte, int index, struct ds *s)
1755 const struct fte_version *version = fte->versions[index];
1762 cls_rule_format(&fte->rule, s);
1763 if (version->cookie != htonll(0)) {
1764 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
1766 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1767 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
1769 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1770 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
1773 ds_put_char(s, ' ');
1774 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
1776 ds_put_char(s, '\n');
1780 fte_from_cls_rule(const struct cls_rule *cls_rule)
1782 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1785 /* Frees 'fte' and its versions. */
1787 fte_free(struct fte *fte)
1790 fte_version_free(fte->versions[0]);
1791 fte_version_free(fte->versions[1]);
1792 cls_rule_destroy(&fte->rule);
1797 /* Frees all of the FTEs within 'cls'. */
1799 fte_free_all(struct classifier *cls)
1801 struct cls_cursor cursor;
1802 struct fte *fte, *next;
1804 cls_cursor_init(&cursor, cls, NULL);
1805 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1806 classifier_remove(cls, &fte->rule);
1809 classifier_destroy(cls);
1812 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1813 * necessary. Sets 'version' as the version of that rule with the given
1814 * 'index', replacing any existing version, if any.
1816 * Takes ownership of 'version'. */
1818 fte_insert(struct classifier *cls, const struct match *match,
1819 unsigned int priority, struct fte_version *version, int index)
1821 struct fte *old, *fte;
1823 fte = xzalloc(sizeof *fte);
1824 cls_rule_init(&fte->rule, match, priority);
1825 fte->versions[index] = version;
1827 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1829 fte_version_free(old->versions[index]);
1830 fte->versions[!index] = old->versions[!index];
1831 cls_rule_destroy(&old->rule);
1836 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1837 * with the specified 'index'. Returns the flow formats able to represent the
1838 * flows that were read. */
1839 static enum ofputil_protocol
1840 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1842 enum ofputil_protocol usable_protocols;
1846 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1848 ovs_fatal(errno, "%s: open", filename);
1852 usable_protocols = OFPUTIL_P_ANY;
1853 while (!ds_get_preprocessed_line(&s, file)) {
1854 struct fte_version *version;
1855 struct ofputil_flow_mod fm;
1857 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1859 version = xmalloc(sizeof *version);
1860 version->cookie = fm.new_cookie;
1861 version->idle_timeout = fm.idle_timeout;
1862 version->hard_timeout = fm.hard_timeout;
1863 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG);
1864 version->ofpacts = fm.ofpacts;
1865 version->ofpacts_len = fm.ofpacts_len;
1867 usable_protocols &= ofputil_usable_protocols(&fm.match);
1869 fte_insert(cls, &fm.match, fm.priority, version, index);
1873 if (file != stdin) {
1877 return usable_protocols;
1881 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
1882 struct ofpbuf **replyp,
1883 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
1885 struct ofpbuf *reply = *replyp;
1891 /* Get a flow stats reply message, if we don't already have one. */
1897 run(vconn_recv_block(vconn, &reply),
1898 "OpenFlow packet receive failed");
1899 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1901 error = ofptype_decode(&type, reply->data);
1902 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
1903 ovs_fatal(0, "received bad reply: %s",
1904 ofp_to_string(reply->data, reply->size,
1909 /* Pull an individual flow stats reply out of the message. */
1910 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1917 more = ofpmp_more(reply->l2);
1918 ofpbuf_delete(reply);
1927 ovs_fatal(0, "parse error in reply (%s)",
1928 ofperr_to_string(retval));
1933 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1934 * format 'protocol', and adds them as flow table entries in 'cls' for the
1935 * version with the specified 'index'. */
1937 read_flows_from_switch(struct vconn *vconn,
1938 enum ofputil_protocol protocol,
1939 struct classifier *cls, int index)
1941 struct ofputil_flow_stats_request fsr;
1942 struct ofputil_flow_stats fs;
1943 struct ofpbuf *request;
1944 struct ofpbuf ofpacts;
1945 struct ofpbuf *reply;
1948 fsr.aggregate = false;
1949 match_init_catchall(&fsr.match);
1950 fsr.out_port = OFPP_ANY;
1951 fsr.table_id = 0xff;
1952 fsr.cookie = fsr.cookie_mask = htonll(0);
1953 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1954 send_xid = ((struct ofp_header *) request->data)->xid;
1955 send_openflow_buffer(vconn, request);
1958 ofpbuf_init(&ofpacts, 0);
1959 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
1960 struct fte_version *version;
1962 version = xmalloc(sizeof *version);
1963 version->cookie = fs.cookie;
1964 version->idle_timeout = fs.idle_timeout;
1965 version->hard_timeout = fs.hard_timeout;
1967 version->ofpacts_len = fs.ofpacts_len;
1968 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
1970 fte_insert(cls, &fs.match, fs.priority, version, index);
1972 ofpbuf_uninit(&ofpacts);
1976 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1977 enum ofputil_protocol protocol, struct list *packets)
1979 const struct fte_version *version = fte->versions[index];
1980 struct ofputil_flow_mod fm;
1983 minimatch_expand(&fte->rule.match, &fm.match);
1984 fm.priority = fte->rule.priority;
1985 fm.cookie = htonll(0);
1986 fm.cookie_mask = htonll(0);
1987 fm.new_cookie = version->cookie;
1989 fm.command = command;
1990 fm.idle_timeout = version->idle_timeout;
1991 fm.hard_timeout = version->hard_timeout;
1992 fm.buffer_id = UINT32_MAX;
1993 fm.out_port = OFPP_ANY;
1994 fm.flags = version->flags;
1995 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1996 command == OFPFC_MODIFY_STRICT) {
1997 fm.ofpacts = version->ofpacts;
1998 fm.ofpacts_len = version->ofpacts_len;
2004 ofm = ofputil_encode_flow_mod(&fm, protocol);
2005 list_push_back(packets, &ofm->list_node);
2009 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2011 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2012 enum ofputil_protocol usable_protocols, protocol;
2013 struct cls_cursor cursor;
2014 struct classifier cls;
2015 struct list requests;
2016 struct vconn *vconn;
2019 classifier_init(&cls);
2020 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2022 protocol = open_vconn(argv[1], &vconn);
2023 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2025 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2027 list_init(&requests);
2029 /* Delete flows that exist on the switch but not in the file. */
2030 cls_cursor_init(&cursor, &cls, NULL);
2031 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2032 struct fte_version *file_ver = fte->versions[FILE_IDX];
2033 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2035 if (sw_ver && !file_ver) {
2036 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2037 protocol, &requests);
2041 /* Add flows that exist in the file but not on the switch.
2042 * Update flows that exist in both places but differ. */
2043 cls_cursor_init(&cursor, &cls, NULL);
2044 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2045 struct fte_version *file_ver = fte->versions[FILE_IDX];
2046 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2049 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2050 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2053 transact_multiple_noreply(vconn, &requests);
2060 read_flows_from_source(const char *source, struct classifier *cls, int index)
2064 if (source[0] == '/' || source[0] == '.'
2065 || (!strchr(source, ':') && !stat(source, &s))) {
2066 read_flows_from_file(source, cls, index);
2068 enum ofputil_protocol protocol;
2069 struct vconn *vconn;
2071 protocol = open_vconn(source, &vconn);
2072 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2073 read_flows_from_switch(vconn, protocol, cls, index);
2079 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2081 bool differences = false;
2082 struct cls_cursor cursor;
2083 struct classifier cls;
2087 classifier_init(&cls);
2088 read_flows_from_source(argv[1], &cls, 0);
2089 read_flows_from_source(argv[2], &cls, 1);
2094 cls_cursor_init(&cursor, &cls, NULL);
2095 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2096 struct fte_version *a = fte->versions[0];
2097 struct fte_version *b = fte->versions[1];
2099 if (!a || !b || !fte_version_equals(a, b)) {
2100 fte_version_format(fte, 0, &a_s);
2101 fte_version_format(fte, 1, &b_s);
2102 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2104 printf("-%s", ds_cstr(&a_s));
2107 printf("+%s", ds_cstr(&b_s));
2124 /* Undocumented commands for unit testing. */
2127 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
2129 enum ofputil_protocol usable_protocols;
2130 enum ofputil_protocol protocol = 0;
2134 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
2135 usable_s = ofputil_protocols_to_string(usable_protocols);
2136 printf("usable protocols: %s\n", usable_s);
2139 if (!(usable_protocols & allowed_protocols)) {
2140 ovs_fatal(0, "no usable protocol");
2142 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2144 if (protocol & usable_protocols & allowed_protocols) {
2148 assert(IS_POW2(protocol));
2150 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2152 for (i = 0; i < n_fms; i++) {
2153 struct ofputil_flow_mod *fm = &fms[i];
2156 msg = ofputil_encode_flow_mod(fm, protocol);
2157 ofp_print(stdout, msg->data, msg->size, verbosity);
2164 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2165 * it back to stdout. */
2167 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2169 struct ofputil_flow_mod fm;
2171 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
2172 ofctl_parse_flows__(&fm, 1);
2175 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2176 * add-flows) and prints each of the flows back to stdout. */
2178 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2180 struct ofputil_flow_mod *fms = NULL;
2183 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
2184 ofctl_parse_flows__(fms, n_fms);
2189 ofctl_parse_nxm__(bool oxm)
2194 while (!ds_get_test_line(&in, stdin)) {
2195 struct ofpbuf nx_match;
2197 ovs_be64 cookie, cookie_mask;
2201 /* Convert string to nx_match. */
2202 ofpbuf_init(&nx_match, 0);
2204 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2206 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2209 /* Convert nx_match to match. */
2212 error = oxm_pull_match(&nx_match, &match);
2214 error = nx_pull_match(&nx_match, match_len, &match,
2215 &cookie, &cookie_mask);
2219 error = oxm_pull_match_loose(&nx_match, &match);
2221 error = nx_pull_match_loose(&nx_match, match_len, &match,
2222 &cookie, &cookie_mask);
2230 /* Convert match back to nx_match. */
2231 ofpbuf_uninit(&nx_match);
2232 ofpbuf_init(&nx_match, 0);
2234 match_len = oxm_put_match(&nx_match, &match);
2235 out = oxm_match_to_string(nx_match.data, match_len);
2237 match_len = nx_put_match(&nx_match, &match,
2238 cookie, cookie_mask);
2239 out = nx_match_to_string(nx_match.data, match_len);
2245 printf("nx_pull_match() returned error %s\n",
2246 ofperr_get_name(error));
2249 ofpbuf_uninit(&nx_match);
2254 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2255 * stdin, does some internal fussing with them, and then prints them back as
2256 * strings on stdout. */
2258 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2260 return ofctl_parse_nxm__(false);
2263 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2264 * stdin, does some internal fussing with them, and then prints them back as
2265 * strings on stdout. */
2267 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2269 return ofctl_parse_nxm__(true);
2273 print_differences(const char *prefix,
2274 const void *a_, size_t a_len,
2275 const void *b_, size_t b_len)
2277 const uint8_t *a = a_;
2278 const uint8_t *b = b_;
2281 for (i = 0; i < MIN(a_len, b_len); i++) {
2283 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2284 prefix, i, a[i], b[i]);
2287 for (i = a_len; i < b_len; i++) {
2288 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2290 for (i = b_len; i < a_len; i++) {
2291 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2295 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2296 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2297 * on stdout, and then converts them back to hex bytes and prints any
2298 * differences from the input. */
2300 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2305 while (!ds_get_preprocessed_line(&in, stdin)) {
2306 struct ofpbuf of10_out;
2307 struct ofpbuf of10_in;
2308 struct ofpbuf ofpacts;
2313 /* Parse hex bytes. */
2314 ofpbuf_init(&of10_in, 0);
2315 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2316 ovs_fatal(0, "Trailing garbage in hex data");
2319 /* Convert to ofpacts. */
2320 ofpbuf_init(&ofpacts, 0);
2321 size = of10_in.size;
2322 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2324 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2325 ofpbuf_uninit(&ofpacts);
2326 ofpbuf_uninit(&of10_in);
2329 ofpbuf_push_uninit(&of10_in, size);
2331 /* Print cls_rule. */
2333 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2337 /* Convert back to ofp10 actions and print differences from input. */
2338 ofpbuf_init(&of10_out, 0);
2339 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2341 print_differences("", of10_in.data, of10_in.size,
2342 of10_out.data, of10_out.size);
2345 ofpbuf_uninit(&ofpacts);
2346 ofpbuf_uninit(&of10_in);
2347 ofpbuf_uninit(&of10_out);
2352 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2353 * bytes from stdin, converts them to cls_rules, prints them as strings on
2354 * stdout, and then converts them back to hex bytes and prints any differences
2357 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2358 * values are ignored in the input and will be set to zero when OVS converts
2359 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2360 * it does the conversion to hex, to ensure that in fact they are ignored. */
2362 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2369 while (!ds_get_preprocessed_line(&in, stdin)) {
2370 struct ofpbuf match_in, match_expout;
2371 struct ofp10_match match_out;
2372 struct ofp10_match match_normal;
2376 /* Parse hex bytes to use for expected output. */
2378 ds_put_cstr(&expout, ds_cstr(&in));
2379 for (p = ds_cstr(&expout); *p; p++) {
2384 ofpbuf_init(&match_expout, 0);
2385 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2386 ovs_fatal(0, "Trailing garbage in hex data");
2388 if (match_expout.size != sizeof(struct ofp10_match)) {
2389 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2390 match_expout.size, sizeof(struct ofp10_match));
2393 /* Parse hex bytes for input. */
2394 for (p = ds_cstr(&in); *p; p++) {
2396 *p = "0123456789abcdef"[random_uint32() & 0xf];
2399 ofpbuf_init(&match_in, 0);
2400 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2401 ovs_fatal(0, "Trailing garbage in hex data");
2403 if (match_in.size != sizeof(struct ofp10_match)) {
2404 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2405 match_in.size, sizeof(struct ofp10_match));
2408 /* Convert to cls_rule and print. */
2409 ofputil_match_from_ofp10_match(match_in.data, &match);
2410 match_print(&match);
2412 /* Convert back to ofp10_match and print differences from input. */
2413 ofputil_match_to_ofp10_match(&match, &match_out);
2414 print_differences("", match_expout.data, match_expout.size,
2415 &match_out, sizeof match_out);
2417 /* Normalize, then convert and compare again. */
2418 ofputil_normalize_match(&match);
2419 ofputil_match_to_ofp10_match(&match, &match_normal);
2420 print_differences("normal: ", &match_out, sizeof match_out,
2421 &match_normal, sizeof match_normal);
2424 ofpbuf_uninit(&match_in);
2425 ofpbuf_uninit(&match_expout);
2428 ds_destroy(&expout);
2431 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2432 * bytes from stdin, converts them to "struct match"es, prints them as strings
2433 * on stdout, and then converts them back to hex bytes and prints any
2434 * differences from the input. */
2436 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2441 while (!ds_get_preprocessed_line(&in, stdin)) {
2442 struct ofpbuf match_in;
2443 struct ofp11_match match_out;
2447 /* Parse hex bytes. */
2448 ofpbuf_init(&match_in, 0);
2449 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2450 ovs_fatal(0, "Trailing garbage in hex data");
2452 if (match_in.size != sizeof(struct ofp11_match)) {
2453 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2454 match_in.size, sizeof(struct ofp11_match));
2457 /* Convert to match. */
2458 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2460 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2461 ofpbuf_uninit(&match_in);
2466 match_print(&match);
2468 /* Convert back to ofp11_match and print differences from input. */
2469 ofputil_match_to_ofp11_match(&match, &match_out);
2471 print_differences("", match_in.data, match_in.size,
2472 &match_out, sizeof match_out);
2475 ofpbuf_uninit(&match_in);
2480 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2481 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2482 * on stdout, and then converts them back to hex bytes and prints any
2483 * differences from the input. */
2485 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2490 while (!ds_get_preprocessed_line(&in, stdin)) {
2491 struct ofpbuf of11_out;
2492 struct ofpbuf of11_in;
2493 struct ofpbuf ofpacts;
2498 /* Parse hex bytes. */
2499 ofpbuf_init(&of11_in, 0);
2500 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2501 ovs_fatal(0, "Trailing garbage in hex data");
2504 /* Convert to ofpacts. */
2505 ofpbuf_init(&ofpacts, 0);
2506 size = of11_in.size;
2507 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2510 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2511 ofpbuf_uninit(&ofpacts);
2512 ofpbuf_uninit(&of11_in);
2515 ofpbuf_push_uninit(&of11_in, size);
2517 /* Print cls_rule. */
2519 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2523 /* Convert back to ofp11 actions and print differences from input. */
2524 ofpbuf_init(&of11_out, 0);
2525 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2527 print_differences("", of11_in.data, of11_in.size,
2528 of11_out.data, of11_out.size);
2531 ofpbuf_uninit(&ofpacts);
2532 ofpbuf_uninit(&of11_in);
2533 ofpbuf_uninit(&of11_out);
2538 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2539 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2540 * them as strings on stdout, and then converts them back to hex bytes and
2541 * prints any differences from the input. */
2543 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2548 while (!ds_get_preprocessed_line(&in, stdin)) {
2549 struct ofpbuf of11_out;
2550 struct ofpbuf of11_in;
2551 struct ofpbuf ofpacts;
2556 /* Parse hex bytes. */
2557 ofpbuf_init(&of11_in, 0);
2558 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2559 ovs_fatal(0, "Trailing garbage in hex data");
2562 /* Convert to ofpacts. */
2563 ofpbuf_init(&ofpacts, 0);
2564 size = of11_in.size;
2565 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2568 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2569 ofpbuf_uninit(&ofpacts);
2570 ofpbuf_uninit(&of11_in);
2573 ofpbuf_push_uninit(&of11_in, size);
2575 /* Print cls_rule. */
2577 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2581 /* Convert back to ofp11 instructions and print differences from
2583 ofpbuf_init(&of11_out, 0);
2584 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2587 print_differences("", of11_in.data, of11_in.size,
2588 of11_out.data, of11_out.size);
2591 ofpbuf_uninit(&ofpacts);
2592 ofpbuf_uninit(&of11_in);
2593 ofpbuf_uninit(&of11_out);
2598 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2599 * mask values to and from various formats and prints the results. */
2601 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
2606 struct ofputil_flow_mod fm;
2609 struct match nxm_match;
2613 struct ofp10_match of10_raw;
2614 struct match of10_match;
2616 struct ofp11_match of11_raw;
2617 struct match of11_match;
2621 match_init_catchall(&match);
2622 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
2623 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
2625 /* Convert to and from string. */
2626 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
2627 printf("%s -> ", string_s);
2629 parse_ofp_str(&fm, -1, string_s, false);
2630 printf("%04"PRIx16"/%04"PRIx16"\n",
2631 ntohs(fm.match.flow.vlan_tci),
2632 ntohs(fm.match.wc.masks.vlan_tci));
2635 /* Convert to and from NXM. */
2636 ofpbuf_init(&nxm, 0);
2637 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
2638 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
2639 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
2640 printf("NXM: %s -> ", nxm_s);
2642 printf("%s\n", ofperr_to_string(error));
2644 printf("%04"PRIx16"/%04"PRIx16"\n",
2645 ntohs(nxm_match.flow.vlan_tci),
2646 ntohs(nxm_match.wc.masks.vlan_tci));
2649 ofpbuf_uninit(&nxm);
2651 /* Convert to and from OXM. */
2652 ofpbuf_init(&nxm, 0);
2653 nxm_match_len = oxm_put_match(&nxm, &match);
2654 nxm_s = oxm_match_to_string(nxm.data, nxm_match_len);
2655 error = oxm_pull_match(&nxm, &nxm_match);
2656 printf("OXM: %s -> ", nxm_s);
2658 printf("%s\n", ofperr_to_string(error));
2660 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
2661 (VLAN_VID_MASK | VLAN_CFI);
2662 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
2663 (VLAN_VID_MASK | VLAN_CFI);
2665 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
2666 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
2667 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
2673 ofpbuf_uninit(&nxm);
2675 /* Convert to and from OpenFlow 1.0. */
2676 ofputil_match_to_ofp10_match(&match, &of10_raw);
2677 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
2678 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2679 ntohs(of10_raw.dl_vlan),
2680 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
2681 of10_raw.dl_vlan_pcp,
2682 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
2683 ntohs(of10_match.flow.vlan_tci),
2684 ntohs(of10_match.wc.masks.vlan_tci));
2686 /* Convert to and from OpenFlow 1.1. */
2687 ofputil_match_to_ofp11_match(&match, &of11_raw);
2688 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
2689 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2690 ntohs(of11_raw.dl_vlan),
2691 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
2692 of11_raw.dl_vlan_pcp,
2693 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
2694 ntohs(of11_match.flow.vlan_tci),
2695 ntohs(of11_match.wc.masks.vlan_tci));
2698 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2701 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2706 error = ofperr_from_name(argv[1]);
2708 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2711 for (version = 0; version <= UINT8_MAX; version++) {
2712 const char *name = ofperr_domain_get_name(version);
2716 printf("%s: %d,%d\n",
2717 ofperr_domain_get_name(version),
2718 ofperr_get_type(error, version),
2719 ofperr_get_code(error, version));
2723 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2724 * binary data, interpreting them as an OpenFlow message, and prints the
2725 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2727 ofctl_ofp_print(int argc, char *argv[])
2729 struct ofpbuf packet;
2731 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2732 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2733 ovs_fatal(0, "trailing garbage following hex bytes");
2735 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2736 ofpbuf_uninit(&packet);
2739 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
2740 * and dumps each message in hex. */
2742 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
2744 uint32_t bitmap = strtol(argv[1], NULL, 0);
2745 struct ofpbuf *hello;
2747 hello = ofputil_encode_hello(bitmap);
2748 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
2749 ofp_print(stdout, hello->data, hello->size, verbosity);
2750 ofpbuf_delete(hello);
2753 static const struct command all_commands[] = {
2754 { "show", 1, 1, ofctl_show },
2755 { "monitor", 1, 3, ofctl_monitor },
2756 { "snoop", 1, 1, ofctl_snoop },
2757 { "dump-desc", 1, 1, ofctl_dump_desc },
2758 { "dump-tables", 1, 1, ofctl_dump_tables },
2759 { "dump-flows", 1, 2, ofctl_dump_flows },
2760 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2761 { "queue-stats", 1, 3, ofctl_queue_stats },
2762 { "add-flow", 2, 2, ofctl_add_flow },
2763 { "add-flows", 2, 2, ofctl_add_flows },
2764 { "mod-flows", 2, 2, ofctl_mod_flows },
2765 { "del-flows", 1, 2, ofctl_del_flows },
2766 { "replace-flows", 2, 2, ofctl_replace_flows },
2767 { "diff-flows", 2, 2, ofctl_diff_flows },
2768 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2769 { "dump-ports", 1, 2, ofctl_dump_ports },
2770 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2771 { "mod-port", 3, 3, ofctl_mod_port },
2772 { "get-frags", 1, 1, ofctl_get_frags },
2773 { "set-frags", 2, 2, ofctl_set_frags },
2774 { "probe", 1, 1, ofctl_probe },
2775 { "ping", 1, 2, ofctl_ping },
2776 { "benchmark", 3, 3, ofctl_benchmark },
2777 { "help", 0, INT_MAX, ofctl_help },
2779 /* Undocumented commands for testing. */
2780 { "parse-flow", 1, 1, ofctl_parse_flow },
2781 { "parse-flows", 1, 1, ofctl_parse_flows },
2782 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
2783 { "parse-nxm", 0, 0, ofctl_parse_nxm },
2784 { "parse-oxm", 0, 0, ofctl_parse_oxm },
2785 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
2786 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
2787 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
2788 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
2789 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
2790 { "check-vlan", 2, 2, ofctl_check_vlan },
2791 { "print-error", 1, 1, ofctl_print_error },
2792 { "ofp-print", 1, 2, ofctl_ofp_print },
2793 { "encode-hello", 1, 1, ofctl_encode_hello },
2795 { NULL, 0, 0, NULL },