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"
48 #include "ofproto/ofproto.h"
49 #include "openflow/nicira-ext.h"
50 #include "openflow/openflow.h"
52 #include "poll-loop.h"
54 #include "stream-ssl.h"
55 #include "socket-util.h"
61 #include "meta-flow.h"
64 VLOG_DEFINE_THIS_MODULE(ofctl);
66 /* --strict: Use strict matching for flow mod commands? Additionally governs
67 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
71 /* --readd: If true, on replace-flows, re-add even flows that have not changed
72 * (to reset flow counters). */
75 /* -F, --flow-format: Allowed protocols. By default, any protocol is
77 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
79 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
80 * commands. Either one of NXPIF_* to force a particular packet_in format, or
81 * -1 to let ovs-ofctl choose the default. */
82 static int preferred_packet_in_format = -1;
84 /* -m, --more: Additional verbosity for ofp-print functions. */
87 /* --timestamp: Print a timestamp before each received packet on "monitor" and
89 static bool timestamp;
91 /* --sort, --rsort: Sort order. */
92 enum sort_order { SORT_ASC, SORT_DESC };
93 struct sort_criterion {
94 const struct mf_field *field; /* NULL means to sort by priority. */
95 enum sort_order order;
97 static struct sort_criterion *criteria;
98 static size_t n_criteria, allocated_criteria;
100 static const struct command all_commands[];
102 static void usage(void) NO_RETURN;
103 static void parse_options(int argc, char *argv[]);
105 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
106 struct ofpbuf **replyp,
107 struct ofputil_flow_stats *,
108 struct ofpbuf *ofpacts);
110 main(int argc, char *argv[])
112 set_program_name(argv[0]);
113 parse_options(argc, argv);
114 signal(SIGPIPE, SIG_IGN);
115 run_command(argc - optind, argv + optind, all_commands);
120 add_sort_criterion(enum sort_order order, const char *field)
122 struct sort_criterion *sc;
124 if (n_criteria >= allocated_criteria) {
125 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
128 sc = &criteria[n_criteria++];
129 if (!field || !strcasecmp(field, "priority")) {
132 sc->field = mf_from_name(field);
134 ovs_fatal(0, "%s: unknown field name", field);
141 parse_options(int argc, char *argv[])
144 OPT_STRICT = UCHAR_MAX + 1,
152 static struct option long_options[] = {
153 {"timeout", required_argument, NULL, 't'},
154 {"strict", no_argument, NULL, OPT_STRICT},
155 {"readd", no_argument, NULL, OPT_READD},
156 {"flow-format", required_argument, NULL, 'F'},
157 {"packet-in-format", required_argument, NULL, 'P'},
158 {"more", no_argument, NULL, 'm'},
159 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
160 {"sort", optional_argument, NULL, OPT_SORT},
161 {"rsort", optional_argument, NULL, OPT_RSORT},
162 {"help", no_argument, NULL, 'h'},
163 {"version", no_argument, NULL, 'V'},
166 STREAM_SSL_LONG_OPTIONS,
169 char *short_options = long_options_to_short_options(long_options);
172 unsigned long int timeout;
175 c = getopt_long(argc, argv, short_options, long_options, NULL);
182 timeout = strtoul(optarg, NULL, 10);
184 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
192 allowed_protocols = ofputil_protocols_from_string(optarg);
193 if (!allowed_protocols) {
194 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
199 preferred_packet_in_format =
200 ofputil_packet_in_format_from_string(optarg);
201 if (preferred_packet_in_format < 0) {
202 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
214 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
230 add_sort_criterion(SORT_ASC, optarg);
234 add_sort_criterion(SORT_DESC, optarg);
237 DAEMON_OPTION_HANDLERS
239 STREAM_SSL_OPTION_HANDLERS
250 /* Always do a final sort pass based on priority. */
251 add_sort_criterion(SORT_DESC, "priority");
260 printf("%s: OpenFlow switch management utility\n"
261 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
262 "\nFor OpenFlow switches:\n"
263 " show SWITCH show OpenFlow information\n"
264 " dump-desc SWITCH print switch description\n"
265 " dump-tables SWITCH print table stats\n"
266 " mod-port SWITCH IFACE ACT modify port behavior\n"
267 " get-frags SWITCH print fragment handling behavior\n"
268 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
269 " dump-ports SWITCH [PORT] print port statistics\n"
270 " dump-ports-desc SWITCH print port descriptions\n"
271 " dump-flows SWITCH print all flow entries\n"
272 " dump-flows SWITCH FLOW print matching FLOWs\n"
273 " dump-aggregate SWITCH print aggregate flow statistics\n"
274 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
275 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
276 " add-flow SWITCH FLOW add flow described by FLOW\n"
277 " add-flows SWITCH FILE add flows from FILE\n"
278 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
279 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
280 " replace-flows SWITCH FILE replace flows with those in FILE\n"
281 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
282 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
283 " execute ACTIONS on PACKET\n"
284 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
285 " print packets received from SWITCH\n"
286 " snoop SWITCH snoop on SWITCH and its controller\n"
287 "\nFor OpenFlow switches and controllers:\n"
288 " probe TARGET probe whether TARGET is up\n"
289 " ping TARGET [N] latency of N-byte echos\n"
290 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
291 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
292 program_name, program_name);
293 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, 0, vconnp, DSCP_DEFAULT);
343 if (error && error != ENOENT) {
344 ovs_fatal(0, "%s: failed to open socket (%s)", name,
352 static enum ofputil_protocol
353 open_vconn__(const char *name, const char *default_suffix,
354 struct vconn **vconnp)
356 char *datapath_name, *datapath_type, *socket_name;
357 enum ofputil_protocol protocol;
362 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
364 ofproto_parse_name(name, &datapath_name, &datapath_type);
365 socket_name = xasprintf("%s/%s.%s",
366 ovs_rundir(), datapath_name, default_suffix);
370 if (strchr(name, ':')) {
371 run(vconn_open_block(name, 0, vconnp), "connecting to %s", name);
372 } else if (!open_vconn_socket(name, vconnp)) {
374 } else if (!open_vconn_socket(bridge_path, vconnp)) {
376 } else if (!open_vconn_socket(socket_name, vconnp)) {
379 ovs_fatal(0, "%s is not a bridge or a socket", name);
385 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
386 error = vconn_connect_block(*vconnp);
388 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
392 ofp_version = vconn_get_version(*vconnp);
393 protocol = ofputil_protocol_from_ofp_version(ofp_version);
395 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
401 static enum ofputil_protocol
402 open_vconn(const char *name, struct vconn **vconnp)
404 return open_vconn__(name, "mgmt", vconnp);
408 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
410 ofpmsg_update_length(buffer);
411 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
415 dump_transaction(const char *vconn_name, struct ofpbuf *request)
418 struct ofpbuf *reply;
420 ofpmsg_update_length(request);
421 open_vconn(vconn_name, &vconn);
422 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
423 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
424 ofpbuf_delete(reply);
429 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
431 struct ofpbuf *request;
432 request = ofpraw_alloc(raw, OFP10_VERSION, 0);
433 dump_transaction(vconn_name, request);
437 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
439 const struct ofp_header *request_oh = request->data;
440 ovs_be32 send_xid = request_oh->xid;
441 enum ofpraw request_raw;
442 enum ofpraw reply_raw;
445 ofpraw_decode_partial(&request_raw, request->data, request->size);
446 reply_raw = ofpraw_stats_request_to_reply(request_raw,
447 request_oh->version);
449 send_openflow_buffer(vconn, request);
452 struct ofpbuf *reply;
454 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
455 recv_xid = ((struct ofp_header *) reply->data)->xid;
456 if (send_xid == recv_xid) {
459 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
461 ofpraw_decode(&raw, reply->data);
462 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
464 } else if (raw == reply_raw) {
465 done = !ofpmp_more(reply->data);
467 ovs_fatal(0, "received bad reply: %s",
468 ofp_to_string(reply->data, reply->size,
472 VLOG_DBG("received reply with xid %08"PRIx32" "
473 "!= expected %08"PRIx32, recv_xid, send_xid);
475 ofpbuf_delete(reply);
480 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
482 struct ofpbuf *request;
485 open_vconn(vconn_name, &vconn);
486 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
487 dump_stats_transaction(vconn, request);
491 /* Sends 'request', which should be a request that only has a reply if an error
492 * occurs, and waits for it to succeed or fail. If an error does occur, prints
493 * it and exits with an error.
495 * Destroys all of the 'requests'. */
497 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
499 struct ofpbuf *request, *reply;
501 LIST_FOR_EACH (request, list_node, requests) {
502 ofpmsg_update_length(request);
505 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
506 "talking to %s", vconn_get_name(vconn));
508 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
511 ofpbuf_delete(reply);
514 /* Sends 'request', which should be a request that only has a reply if an error
515 * occurs, and waits for it to succeed or fail. If an error does occur, prints
516 * it and exits with an error.
518 * Destroys 'request'. */
520 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
522 struct list requests;
524 list_init(&requests);
525 list_push_back(&requests, &request->list_node);
526 transact_multiple_noreply(vconn, &requests);
530 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
532 struct ofp_switch_config *config;
533 struct ofpbuf *request;
534 struct ofpbuf *reply;
537 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION, 0);
538 run(vconn_transact(vconn, request, &reply),
539 "talking to %s", vconn_get_name(vconn));
541 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
542 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
545 config = ofpbuf_pull(reply, sizeof *config);
548 ofpbuf_delete(reply);
552 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
554 struct ofpbuf *request;
556 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, OFP10_VERSION, 0);
557 ofpbuf_put(request, config, sizeof *config);
559 transact_noreply(vconn, request);
563 ofctl_show(int argc OVS_UNUSED, char *argv[])
565 const char *vconn_name = argv[1];
567 struct ofpbuf *request;
568 struct ofpbuf *reply;
571 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0);
572 open_vconn(vconn_name, &vconn);
573 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
575 trunc = ofputil_switch_features_ports_trunc(reply);
576 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
578 ofpbuf_delete(reply);
582 /* The Features Reply may not contain all the ports, so send a
583 * Port Description stats request, which doesn't have size
585 dump_trivial_stats_transaction(vconn_name,
586 OFPRAW_OFPST_PORT_DESC_REQUEST);
588 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
592 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
594 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
598 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
600 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
604 fetch_port_by_features(const char *vconn_name,
605 const char *port_name, unsigned int port_no,
606 struct ofputil_phy_port *pp, bool *trunc)
608 struct ofputil_switch_features features;
609 const struct ofp_header *oh;
610 struct ofpbuf *request, *reply;
617 /* Fetch the switch's ofp_switch_features. */
618 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0);
619 open_vconn(vconn_name, &vconn);
620 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
624 if (ofptype_decode(&type, reply->data)
625 || type != OFPTYPE_FEATURES_REPLY) {
626 ovs_fatal(0, "%s: received bad features reply", vconn_name);
630 if (ofputil_switch_features_ports_trunc(reply)) {
635 error = ofputil_decode_switch_features(oh, &features, &b);
637 ovs_fatal(0, "%s: failed to decode features reply (%s)",
638 vconn_name, ofperr_to_string(error));
641 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
642 if (port_no != UINT_MAX
643 ? port_no == pp->port_no
644 : !strcmp(pp->name, port_name)) {
651 ofpbuf_delete(reply);
656 fetch_port_by_stats(const char *vconn_name,
657 const char *port_name, unsigned int port_no,
658 struct ofputil_phy_port *pp)
660 struct ofpbuf *request;
666 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
667 send_xid = ((struct ofp_header *) request->data)->xid;
669 open_vconn(vconn_name, &vconn);
670 send_openflow_buffer(vconn, request);
673 struct ofpbuf *reply;
675 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
676 recv_xid = ((struct ofp_header *) reply->data)->xid;
677 if (send_xid == recv_xid) {
678 struct ofp_header *oh = reply->data;
683 ofpbuf_use_const(&b, oh, ntohs(oh->length));
684 if (ofptype_pull(&type, &b)
685 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
686 ovs_fatal(0, "received bad reply: %s",
687 ofp_to_string(reply->data, reply->size,
691 flags = ofpmp_flags(oh);
692 done = !(flags & OFPSF_REPLY_MORE);
695 /* We've already found the port, but we need to drain
696 * the queue of any other replies for this request. */
700 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
701 if (port_no != UINT_MAX ? port_no == pp->port_no
702 : !strcmp(pp->name, port_name)) {
708 VLOG_DBG("received reply with xid %08"PRIx32" "
709 "!= expected %08"PRIx32, recv_xid, send_xid);
711 ofpbuf_delete(reply);
719 /* Opens a connection to 'vconn_name', fetches the port structure for
720 * 'port_name' (which may be a port name or number), and copies it into
723 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
724 struct ofputil_phy_port *pp)
726 unsigned int port_no;
730 /* Try to interpret the argument as a port number. */
731 if (!str_to_uint(port_name, 10, &port_no)) {
735 /* Try to find the port based on the Features Reply. If it looks
736 * like the results may be truncated, then use the Port Description
737 * stats message introduced in OVS 1.7. */
738 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
741 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
745 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
749 /* Returns the port number corresponding to 'port_name' (which may be a port
750 * name or number) within the switch 'vconn_name'. */
752 str_to_port_no(const char *vconn_name, const char *port_name)
756 if (ofputil_port_from_string(port_name, &port_no)) {
759 struct ofputil_phy_port pp;
761 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
767 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
768 enum ofputil_protocol *cur)
771 struct ofpbuf *request, *reply;
772 enum ofputil_protocol next;
774 request = ofputil_encode_set_protocol(*cur, want, &next);
779 run(vconn_transact_noreply(vconn, request, &reply),
780 "talking to %s", vconn_get_name(vconn));
782 char *s = ofp_to_string(reply->data, reply->size, 2);
783 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
784 vconn_get_name(vconn), s);
786 ofpbuf_delete(reply);
794 static enum ofputil_protocol
795 set_protocol_for_flow_dump(struct vconn *vconn,
796 enum ofputil_protocol cur_protocol,
797 enum ofputil_protocol usable_protocols)
802 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
803 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
804 if (f & usable_protocols & allowed_protocols
805 && try_set_protocol(vconn, f, &cur_protocol)) {
810 usable_s = ofputil_protocols_to_string(usable_protocols);
811 if (usable_protocols & allowed_protocols) {
812 ovs_fatal(0, "switch does not support any of the usable flow "
813 "formats (%s)", usable_s);
815 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
816 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
817 "allowed flow formats (%s)", usable_s, allowed_s);
821 static struct vconn *
822 prepare_dump_flows(int argc, char *argv[], bool aggregate,
823 struct ofpbuf **requestp)
825 enum ofputil_protocol usable_protocols, protocol;
826 struct ofputil_flow_stats_request fsr;
829 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
830 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
832 protocol = open_vconn(argv[1], &vconn);
833 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
834 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
839 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
841 struct ofpbuf *request;
844 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
845 dump_stats_transaction(vconn, request);
850 compare_flows(const void *afs_, const void *bfs_)
852 const struct ofputil_flow_stats *afs = afs_;
853 const struct ofputil_flow_stats *bfs = bfs_;
854 const struct match *a = &afs->match;
855 const struct match *b = &bfs->match;
856 const struct sort_criterion *sc;
858 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
859 const struct mf_field *f = sc->field;
863 unsigned int a_pri = afs->priority;
864 unsigned int b_pri = bfs->priority;
865 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
869 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
870 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
872 /* Skip the test for sc->order, so that missing fields always
873 * sort to the end whether we're sorting in ascending or
874 * descending order. */
877 union mf_value aval, bval;
879 mf_get_value(f, &a->flow, &aval);
880 mf_get_value(f, &b->flow, &bval);
881 ret = memcmp(&aval, &bval, f->n_bytes);
886 return sc->order == SORT_ASC ? ret : -ret;
894 ofctl_dump_flows(int argc, char *argv[])
897 return ofctl_dump_flows__(argc, argv, false);
899 struct ofputil_flow_stats *fses;
900 size_t n_fses, allocated_fses;
901 struct ofpbuf *request;
902 struct ofpbuf ofpacts;
903 struct ofpbuf *reply;
909 vconn = prepare_dump_flows(argc, argv, false, &request);
910 send_xid = ((struct ofp_header *) request->data)->xid;
911 send_openflow_buffer(vconn, request);
914 n_fses = allocated_fses = 0;
916 ofpbuf_init(&ofpacts, 0);
918 struct ofputil_flow_stats *fs;
920 if (n_fses >= allocated_fses) {
921 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
925 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
929 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
932 ofpbuf_uninit(&ofpacts);
934 qsort(fses, n_fses, sizeof *fses, compare_flows);
937 for (i = 0; i < n_fses; i++) {
939 ofp_print_flow_stats(&s, &fses[i]);
944 for (i = 0; i < n_fses; i++) {
945 free(fses[i].ofpacts);
954 ofctl_dump_aggregate(int argc, char *argv[])
956 return ofctl_dump_flows__(argc, argv, true);
960 ofctl_queue_stats(int argc, char *argv[])
962 struct ofpbuf *request;
964 struct ofputil_queue_stats_request oqs;
966 open_vconn(argv[1], &vconn);
968 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
969 oqs.port_no = str_to_port_no(argv[1], argv[2]);
971 oqs.port_no = OFPP_ALL;
973 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
974 oqs.queue_id = atoi(argv[3]);
976 oqs.queue_id = OFPQ_ALL;
979 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
980 dump_stats_transaction(vconn, request);
984 static enum ofputil_protocol
985 open_vconn_for_flow_mod(const char *remote,
986 const struct ofputil_flow_mod *fms, size_t n_fms,
987 struct vconn **vconnp)
989 enum ofputil_protocol usable_protocols;
990 enum ofputil_protocol cur_protocol;
994 /* Figure out what flow formats will work. */
995 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
996 if (!(usable_protocols & allowed_protocols)) {
997 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
998 usable_s = ofputil_protocols_to_string(usable_protocols);
999 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1000 "allowed flow formats (%s)", usable_s, allowed_s);
1003 /* If the initial flow format is allowed and usable, keep it. */
1004 cur_protocol = open_vconn(remote, vconnp);
1005 if (usable_protocols & allowed_protocols & cur_protocol) {
1006 return cur_protocol;
1009 /* Otherwise try each flow format in turn. */
1010 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1011 enum ofputil_protocol f = 1 << i;
1013 if (f != cur_protocol
1014 && f & usable_protocols & allowed_protocols
1015 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1020 usable_s = ofputil_protocols_to_string(usable_protocols);
1021 ovs_fatal(0, "switch does not support any of the usable flow "
1022 "formats (%s)", usable_s);
1026 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1029 enum ofputil_protocol protocol;
1030 struct vconn *vconn;
1033 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
1035 for (i = 0; i < n_fms; i++) {
1036 struct ofputil_flow_mod *fm = &fms[i];
1038 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1045 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1047 struct ofputil_flow_mod *fms = NULL;
1050 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
1051 ofctl_flow_mod__(argv[1], fms, n_fms);
1056 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1058 if (argc > 2 && !strcmp(argv[2], "-")) {
1059 ofctl_flow_mod_file(argc, argv, command);
1061 struct ofputil_flow_mod fm;
1062 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
1063 ofctl_flow_mod__(argv[1], &fm, 1);
1068 ofctl_add_flow(int argc, char *argv[])
1070 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1074 ofctl_add_flows(int argc, char *argv[])
1076 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1080 ofctl_mod_flows(int argc, char *argv[])
1082 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1086 ofctl_del_flows(int argc, char *argv[])
1088 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1092 set_packet_in_format(struct vconn *vconn,
1093 enum nx_packet_in_format packet_in_format)
1095 struct ofpbuf *spif;
1097 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1099 transact_noreply(vconn, spif);
1100 VLOG_DBG("%s: using user-specified packet in format %s",
1101 vconn_get_name(vconn),
1102 ofputil_packet_in_format_to_string(packet_in_format));
1106 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1108 struct ofp_switch_config config;
1109 enum ofp_config_flags flags;
1111 fetch_switch_config(vconn, &config);
1112 flags = ntohs(config.flags);
1113 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1114 /* Set the invalid ttl config. */
1115 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1117 config.flags = htons(flags);
1118 set_switch_config(vconn, &config);
1120 /* Then retrieve the configuration to see if it really took. OpenFlow
1121 * doesn't define error reporting for bad modes, so this is all we can
1123 fetch_switch_config(vconn, &config);
1124 flags = ntohs(config.flags);
1125 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1126 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1127 "switch probably doesn't support mode)");
1134 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1135 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1136 * an error message and stores NULL in '*msgp'. */
1138 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1140 struct ofp_header *oh;
1143 msg = ofpbuf_new(strlen(hex) / 2);
1146 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1148 return "Trailing garbage in hex data";
1151 if (msg->size < sizeof(struct ofp_header)) {
1153 return "Message too short for OpenFlow";
1157 if (msg->size != ntohs(oh->length)) {
1159 return "Message size does not match length in OpenFlow header";
1167 ofctl_send(struct unixctl_conn *conn, int argc,
1168 const char *argv[], void *vconn_)
1170 struct vconn *vconn = vconn_;
1177 for (i = 1; i < argc; i++) {
1178 const char *error_msg;
1182 error_msg = openflow_from_hex(argv[i], &msg);
1184 ds_put_format(&reply, "%s\n", error_msg);
1189 fprintf(stderr, "send: ");
1190 ofp_print(stderr, msg->data, msg->size, verbosity);
1192 error = vconn_send_block(vconn, msg);
1195 ds_put_format(&reply, "%s\n", strerror(error));
1198 ds_put_cstr(&reply, "sent\n");
1203 unixctl_command_reply(conn, ds_cstr(&reply));
1205 unixctl_command_reply_error(conn, ds_cstr(&reply));
1210 struct barrier_aux {
1211 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1212 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1216 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1217 const char *argv[] OVS_UNUSED, void *aux_)
1219 struct barrier_aux *aux = aux_;
1224 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1228 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1229 error = vconn_send_block(aux->vconn, msg);
1232 unixctl_command_reply_error(conn, strerror(error));
1239 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1240 const char *argv[], void *aux OVS_UNUSED)
1244 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1246 unixctl_command_reply_error(conn, strerror(errno));
1251 dup2(fd, STDERR_FILENO);
1253 unixctl_command_reply(conn, NULL);
1257 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1258 const char *argv[] OVS_UNUSED, void *blocked_)
1260 bool *blocked = blocked_;
1264 unixctl_command_reply(conn, NULL);
1266 unixctl_command_reply(conn, "already blocking");
1271 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1272 const char *argv[] OVS_UNUSED, void *blocked_)
1274 bool *blocked = blocked_;
1278 unixctl_command_reply(conn, NULL);
1280 unixctl_command_reply(conn, "already unblocked");
1285 monitor_vconn(struct vconn *vconn)
1287 struct barrier_aux barrier_aux = { vconn, NULL };
1288 struct unixctl_server *server;
1289 bool exiting = false;
1290 bool blocked = false;
1293 daemon_save_fd(STDERR_FILENO);
1295 error = unixctl_server_create(NULL, &server);
1297 ovs_fatal(error, "failed to create unixctl server");
1299 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1300 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1302 unixctl_command_register("ofctl/barrier", "", 0, 0,
1303 ofctl_barrier, &barrier_aux);
1304 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1305 ofctl_set_output_file, NULL);
1307 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1308 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1311 daemonize_complete();
1317 unixctl_server_run(server);
1322 retval = vconn_recv(vconn, &b);
1323 if (retval == EAGAIN) {
1326 run(retval, "vconn_recv");
1329 time_t now = time_wall();
1332 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1336 ofptype_decode(&type, b->data);
1337 ofp_print(stderr, b->data, b->size, verbosity + 2);
1340 if (barrier_aux.conn && type == OFPTYPE_BARRIER_REPLY) {
1341 unixctl_command_reply(barrier_aux.conn, NULL);
1342 barrier_aux.conn = NULL;
1351 vconn_run_wait(vconn);
1353 vconn_recv_wait(vconn);
1355 unixctl_server_wait(server);
1359 unixctl_server_destroy(server);
1363 ofctl_monitor(int argc, char *argv[])
1365 struct vconn *vconn;
1368 open_vconn(argv[1], &vconn);
1369 for (i = 2; i < argc; i++) {
1370 const char *arg = argv[i];
1372 if (isdigit((unsigned char) *arg)) {
1373 struct ofp_switch_config config;
1375 fetch_switch_config(vconn, &config);
1376 config.miss_send_len = htons(atoi(arg));
1377 set_switch_config(vconn, &config);
1378 } else if (!strcmp(arg, "invalid_ttl")) {
1379 monitor_set_invalid_ttl_to_controller(vconn);
1380 } else if (!strncmp(arg, "watch:", 6)) {
1381 struct ofputil_flow_monitor_request fmr;
1384 parse_flow_monitor_request(&fmr, arg + 6);
1386 msg = ofpbuf_new(0);
1387 ofputil_append_flow_monitor_request(&fmr, msg);
1388 dump_stats_transaction(vconn, msg);
1390 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1394 if (preferred_packet_in_format >= 0) {
1395 set_packet_in_format(vconn, preferred_packet_in_format);
1397 struct ofpbuf *spif, *reply;
1399 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1401 run(vconn_transact_noreply(vconn, spif, &reply),
1402 "talking to %s", vconn_get_name(vconn));
1404 char *s = ofp_to_string(reply->data, reply->size, 2);
1405 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1406 " replied: %s. Falling back to the switch default.",
1407 vconn_get_name(vconn), s);
1409 ofpbuf_delete(reply);
1413 monitor_vconn(vconn);
1417 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1419 struct vconn *vconn;
1421 open_vconn__(argv[1], "snoop", &vconn);
1422 monitor_vconn(vconn);
1426 ofctl_dump_ports(int argc, char *argv[])
1428 struct ofpbuf *request;
1429 struct vconn *vconn;
1432 open_vconn(argv[1], &vconn);
1433 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1434 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1435 dump_stats_transaction(vconn, request);
1440 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1442 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1446 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1448 struct ofpbuf *request;
1449 struct vconn *vconn;
1450 struct ofpbuf *reply;
1452 open_vconn(argv[1], &vconn);
1453 request = make_echo_request(vconn_get_version(vconn));
1454 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1455 if (reply->size != sizeof(struct ofp_header)) {
1456 ovs_fatal(0, "reply does not match request");
1458 ofpbuf_delete(reply);
1463 ofctl_packet_out(int argc, char *argv[])
1465 enum ofputil_protocol protocol;
1466 struct ofputil_packet_out po;
1467 struct ofpbuf ofpacts;
1468 struct vconn *vconn;
1471 ofpbuf_init(&ofpacts, 64);
1472 parse_ofpacts(argv[3], &ofpacts);
1474 po.buffer_id = UINT32_MAX;
1475 po.in_port = str_to_port_no(argv[1], argv[2]);
1476 po.ofpacts = ofpacts.data;
1477 po.ofpacts_len = ofpacts.size;
1479 protocol = open_vconn(argv[1], &vconn);
1480 for (i = 4; i < argc; i++) {
1481 struct ofpbuf *packet, *opo;
1482 const char *error_msg;
1484 error_msg = eth_from_hex(argv[i], &packet);
1486 ovs_fatal(0, "%s", error_msg);
1489 po.packet = packet->data;
1490 po.packet_len = packet->size;
1491 opo = ofputil_encode_packet_out(&po, protocol);
1492 transact_noreply(vconn, opo);
1493 ofpbuf_delete(packet);
1496 ofpbuf_uninit(&ofpacts);
1500 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1502 struct ofp_config_flag {
1503 const char *name; /* The flag's name. */
1504 enum ofputil_port_config bit; /* Bit to turn on or off. */
1505 bool on; /* Value to set the bit to. */
1507 static const struct ofp_config_flag flags[] = {
1508 { "up", OFPUTIL_PC_PORT_DOWN, false },
1509 { "down", OFPUTIL_PC_PORT_DOWN, true },
1510 { "stp", OFPUTIL_PC_NO_STP, false },
1511 { "receive", OFPUTIL_PC_NO_RECV, false },
1512 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1513 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1514 { "forward", OFPUTIL_PC_NO_FWD, false },
1515 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1518 const struct ofp_config_flag *flag;
1519 enum ofputil_protocol protocol;
1520 struct ofputil_port_mod pm;
1521 struct ofputil_phy_port pp;
1522 struct vconn *vconn;
1523 const char *command;
1526 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1528 pm.port_no = pp.port_no;
1529 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1534 if (!strncasecmp(argv[3], "no-", 3)) {
1535 command = argv[3] + 3;
1537 } else if (!strncasecmp(argv[3], "no", 2)) {
1538 command = argv[3] + 2;
1544 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1545 if (!strcasecmp(command, flag->name)) {
1546 pm.mask = flag->bit;
1547 pm.config = flag->on ^ not ? flag->bit : 0;
1551 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1554 protocol = open_vconn(argv[1], &vconn);
1555 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1560 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1562 struct ofp_switch_config config;
1563 struct vconn *vconn;
1565 open_vconn(argv[1], &vconn);
1566 fetch_switch_config(vconn, &config);
1567 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1572 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1574 struct ofp_switch_config config;
1575 enum ofp_config_flags mode;
1576 struct vconn *vconn;
1579 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1580 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1583 open_vconn(argv[1], &vconn);
1584 fetch_switch_config(vconn, &config);
1585 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1586 if (flags != config.flags) {
1587 /* Set the configuration. */
1588 config.flags = flags;
1589 set_switch_config(vconn, &config);
1591 /* Then retrieve the configuration to see if it really took. OpenFlow
1592 * doesn't define error reporting for bad modes, so this is all we can
1594 fetch_switch_config(vconn, &config);
1595 if (flags != config.flags) {
1596 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1597 "switch probably doesn't support mode \"%s\")",
1598 argv[1], ofputil_frag_handling_to_string(mode));
1605 ofctl_ping(int argc, char *argv[])
1607 size_t max_payload = 65535 - sizeof(struct ofp_header);
1608 unsigned int payload;
1609 struct vconn *vconn;
1612 payload = argc > 2 ? atoi(argv[2]) : 64;
1613 if (payload > max_payload) {
1614 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1617 open_vconn(argv[1], &vconn);
1618 for (i = 0; i < 10; i++) {
1619 struct timeval start, end;
1620 struct ofpbuf *request, *reply;
1621 const struct ofp_header *rpy_hdr;
1624 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
1626 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1628 xgettimeofday(&start);
1629 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1630 xgettimeofday(&end);
1632 rpy_hdr = reply->data;
1633 if (ofptype_pull(&type, reply)
1634 || type != OFPTYPE_ECHO_REPLY
1635 || reply->size != payload
1636 || memcmp(request->l3, reply->l3, payload)) {
1637 printf("Reply does not match request. Request:\n");
1638 ofp_print(stdout, request, request->size, verbosity + 2);
1640 ofp_print(stdout, reply, reply->size, verbosity + 2);
1642 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1643 reply->size, argv[1], ntohl(rpy_hdr->xid),
1644 (1000*(double)(end.tv_sec - start.tv_sec))
1645 + (.001*(end.tv_usec - start.tv_usec)));
1646 ofpbuf_delete(request);
1647 ofpbuf_delete(reply);
1653 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1655 size_t max_payload = 65535 - sizeof(struct ofp_header);
1656 struct timeval start, end;
1657 unsigned int payload_size, message_size;
1658 struct vconn *vconn;
1663 payload_size = atoi(argv[2]);
1664 if (payload_size > max_payload) {
1665 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1667 message_size = sizeof(struct ofp_header) + payload_size;
1669 count = atoi(argv[3]);
1671 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1672 count, message_size, count * message_size);
1674 open_vconn(argv[1], &vconn);
1675 xgettimeofday(&start);
1676 for (i = 0; i < count; i++) {
1677 struct ofpbuf *request, *reply;
1679 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
1681 ofpbuf_put_zeros(request, payload_size);
1682 run(vconn_transact(vconn, request, &reply), "transact");
1683 ofpbuf_delete(reply);
1685 xgettimeofday(&end);
1688 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1689 + (.001*(end.tv_usec - start.tv_usec)));
1690 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1691 duration, count / (duration / 1000.0),
1692 count * message_size / (duration / 1000.0));
1696 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1701 /* replace-flows and diff-flows commands. */
1703 /* A flow table entry, possibly with two different versions. */
1705 struct cls_rule rule; /* Within a "struct classifier". */
1706 struct fte_version *versions[2];
1709 /* One version of a Flow Table Entry. */
1710 struct fte_version {
1712 uint16_t idle_timeout;
1713 uint16_t hard_timeout;
1715 struct ofpact *ofpacts;
1719 /* Frees 'version' and the data that it owns. */
1721 fte_version_free(struct fte_version *version)
1724 free(version->ofpacts);
1729 /* Returns true if 'a' and 'b' are the same, false if they differ.
1731 * Ignores differences in 'flags' because there's no way to retrieve flags from
1732 * an OpenFlow switch. We have to assume that they are the same. */
1734 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1736 return (a->cookie == b->cookie
1737 && a->idle_timeout == b->idle_timeout
1738 && a->hard_timeout == b->hard_timeout
1739 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1740 b->ofpacts, b->ofpacts_len));
1743 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
1744 * 'index' into 's', followed by a new-line. */
1746 fte_version_format(const struct fte *fte, int index, struct ds *s)
1748 const struct fte_version *version = fte->versions[index];
1755 cls_rule_format(&fte->rule, s);
1756 if (version->cookie != htonll(0)) {
1757 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
1759 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1760 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
1762 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1763 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
1766 ds_put_char(s, ' ');
1767 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
1769 ds_put_char(s, '\n');
1773 fte_from_cls_rule(const struct cls_rule *cls_rule)
1775 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1778 /* Frees 'fte' and its versions. */
1780 fte_free(struct fte *fte)
1783 fte_version_free(fte->versions[0]);
1784 fte_version_free(fte->versions[1]);
1785 cls_rule_destroy(&fte->rule);
1790 /* Frees all of the FTEs within 'cls'. */
1792 fte_free_all(struct classifier *cls)
1794 struct cls_cursor cursor;
1795 struct fte *fte, *next;
1797 cls_cursor_init(&cursor, cls, NULL);
1798 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1799 classifier_remove(cls, &fte->rule);
1802 classifier_destroy(cls);
1805 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1806 * necessary. Sets 'version' as the version of that rule with the given
1807 * 'index', replacing any existing version, if any.
1809 * Takes ownership of 'version'. */
1811 fte_insert(struct classifier *cls, const struct match *match,
1812 unsigned int priority, struct fte_version *version, int index)
1814 struct fte *old, *fte;
1816 fte = xzalloc(sizeof *fte);
1817 cls_rule_init(&fte->rule, match, priority);
1818 fte->versions[index] = version;
1820 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1822 fte_version_free(old->versions[index]);
1823 fte->versions[!index] = old->versions[!index];
1824 cls_rule_destroy(&old->rule);
1829 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1830 * with the specified 'index'. Returns the flow formats able to represent the
1831 * flows that were read. */
1832 static enum ofputil_protocol
1833 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1835 enum ofputil_protocol usable_protocols;
1839 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1841 ovs_fatal(errno, "%s: open", filename);
1845 usable_protocols = OFPUTIL_P_ANY;
1846 while (!ds_get_preprocessed_line(&s, file)) {
1847 struct fte_version *version;
1848 struct ofputil_flow_mod fm;
1850 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1852 version = xmalloc(sizeof *version);
1853 version->cookie = fm.new_cookie;
1854 version->idle_timeout = fm.idle_timeout;
1855 version->hard_timeout = fm.hard_timeout;
1856 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG);
1857 version->ofpacts = fm.ofpacts;
1858 version->ofpacts_len = fm.ofpacts_len;
1860 usable_protocols &= ofputil_usable_protocols(&fm.match);
1862 fte_insert(cls, &fm.match, fm.priority, version, index);
1866 if (file != stdin) {
1870 return usable_protocols;
1874 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
1875 struct ofpbuf **replyp,
1876 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
1878 struct ofpbuf *reply = *replyp;
1884 /* Get a flow stats reply message, if we don't already have one. */
1890 run(vconn_recv_block(vconn, &reply),
1891 "OpenFlow packet receive failed");
1892 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1894 error = ofptype_decode(&type, reply->data);
1895 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
1896 ovs_fatal(0, "received bad reply: %s",
1897 ofp_to_string(reply->data, reply->size,
1902 /* Pull an individual flow stats reply out of the message. */
1903 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1910 more = ofpmp_more(reply->l2);
1911 ofpbuf_delete(reply);
1920 ovs_fatal(0, "parse error in reply (%s)",
1921 ofperr_to_string(retval));
1926 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1927 * format 'protocol', and adds them as flow table entries in 'cls' for the
1928 * version with the specified 'index'. */
1930 read_flows_from_switch(struct vconn *vconn,
1931 enum ofputil_protocol protocol,
1932 struct classifier *cls, int index)
1934 struct ofputil_flow_stats_request fsr;
1935 struct ofputil_flow_stats fs;
1936 struct ofpbuf *request;
1937 struct ofpbuf ofpacts;
1938 struct ofpbuf *reply;
1941 fsr.aggregate = false;
1942 match_init_catchall(&fsr.match);
1943 fsr.out_port = OFPP_NONE;
1944 fsr.table_id = 0xff;
1945 fsr.cookie = fsr.cookie_mask = htonll(0);
1946 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1947 send_xid = ((struct ofp_header *) request->data)->xid;
1948 send_openflow_buffer(vconn, request);
1951 ofpbuf_init(&ofpacts, 0);
1952 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
1953 struct fte_version *version;
1955 version = xmalloc(sizeof *version);
1956 version->cookie = fs.cookie;
1957 version->idle_timeout = fs.idle_timeout;
1958 version->hard_timeout = fs.hard_timeout;
1960 version->ofpacts_len = fs.ofpacts_len;
1961 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
1963 fte_insert(cls, &fs.match, fs.priority, version, index);
1965 ofpbuf_uninit(&ofpacts);
1969 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1970 enum ofputil_protocol protocol, struct list *packets)
1972 const struct fte_version *version = fte->versions[index];
1973 struct ofputil_flow_mod fm;
1976 minimatch_expand(&fte->rule.match, &fm.match);
1977 fm.priority = fte->rule.priority;
1978 fm.cookie = htonll(0);
1979 fm.cookie_mask = htonll(0);
1980 fm.new_cookie = version->cookie;
1982 fm.command = command;
1983 fm.idle_timeout = version->idle_timeout;
1984 fm.hard_timeout = version->hard_timeout;
1985 fm.buffer_id = UINT32_MAX;
1986 fm.out_port = OFPP_NONE;
1987 fm.flags = version->flags;
1988 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1989 command == OFPFC_MODIFY_STRICT) {
1990 fm.ofpacts = version->ofpacts;
1991 fm.ofpacts_len = version->ofpacts_len;
1997 ofm = ofputil_encode_flow_mod(&fm, protocol);
1998 list_push_back(packets, &ofm->list_node);
2002 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2004 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2005 enum ofputil_protocol usable_protocols, protocol;
2006 struct cls_cursor cursor;
2007 struct classifier cls;
2008 struct list requests;
2009 struct vconn *vconn;
2012 classifier_init(&cls);
2013 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2015 protocol = open_vconn(argv[1], &vconn);
2016 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2018 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2020 list_init(&requests);
2022 /* Delete flows that exist on the switch but not in the file. */
2023 cls_cursor_init(&cursor, &cls, NULL);
2024 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2025 struct fte_version *file_ver = fte->versions[FILE_IDX];
2026 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2028 if (sw_ver && !file_ver) {
2029 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2030 protocol, &requests);
2034 /* Add flows that exist in the file but not on the switch.
2035 * Update flows that exist in both places but differ. */
2036 cls_cursor_init(&cursor, &cls, NULL);
2037 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2038 struct fte_version *file_ver = fte->versions[FILE_IDX];
2039 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2042 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2043 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2046 transact_multiple_noreply(vconn, &requests);
2053 read_flows_from_source(const char *source, struct classifier *cls, int index)
2057 if (source[0] == '/' || source[0] == '.'
2058 || (!strchr(source, ':') && !stat(source, &s))) {
2059 read_flows_from_file(source, cls, index);
2061 enum ofputil_protocol protocol;
2062 struct vconn *vconn;
2064 protocol = open_vconn(source, &vconn);
2065 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2066 read_flows_from_switch(vconn, protocol, cls, index);
2072 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2074 bool differences = false;
2075 struct cls_cursor cursor;
2076 struct classifier cls;
2080 classifier_init(&cls);
2081 read_flows_from_source(argv[1], &cls, 0);
2082 read_flows_from_source(argv[2], &cls, 1);
2087 cls_cursor_init(&cursor, &cls, NULL);
2088 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2089 struct fte_version *a = fte->versions[0];
2090 struct fte_version *b = fte->versions[1];
2092 if (!a || !b || !fte_version_equals(a, b)) {
2093 fte_version_format(fte, 0, &a_s);
2094 fte_version_format(fte, 1, &b_s);
2095 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2097 printf("-%s", ds_cstr(&a_s));
2100 printf("+%s", ds_cstr(&b_s));
2117 /* Undocumented commands for unit testing. */
2120 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
2122 enum ofputil_protocol usable_protocols;
2123 enum ofputil_protocol protocol = 0;
2127 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
2128 usable_s = ofputil_protocols_to_string(usable_protocols);
2129 printf("usable protocols: %s\n", usable_s);
2132 if (!(usable_protocols & allowed_protocols)) {
2133 ovs_fatal(0, "no usable protocol");
2135 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2137 if (protocol & usable_protocols & allowed_protocols) {
2141 assert(IS_POW2(protocol));
2143 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2145 for (i = 0; i < n_fms; i++) {
2146 struct ofputil_flow_mod *fm = &fms[i];
2149 msg = ofputil_encode_flow_mod(fm, protocol);
2150 ofp_print(stdout, msg->data, msg->size, verbosity);
2157 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2158 * it back to stdout. */
2160 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2162 struct ofputil_flow_mod fm;
2164 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
2165 ofctl_parse_flows__(&fm, 1);
2168 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2169 * add-flows) and prints each of the flows back to stdout. */
2171 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2173 struct ofputil_flow_mod *fms = NULL;
2176 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
2177 ofctl_parse_flows__(fms, n_fms);
2182 ofctl_parse_nxm__(bool oxm)
2187 while (!ds_get_test_line(&in, stdin)) {
2188 struct ofpbuf nx_match;
2190 ovs_be64 cookie, cookie_mask;
2194 /* Convert string to nx_match. */
2195 ofpbuf_init(&nx_match, 0);
2197 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2199 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2202 /* Convert nx_match to match. */
2205 error = oxm_pull_match(&nx_match, &match);
2207 error = nx_pull_match(&nx_match, match_len, &match,
2208 &cookie, &cookie_mask);
2212 error = oxm_pull_match_loose(&nx_match, &match);
2214 error = nx_pull_match_loose(&nx_match, match_len, &match,
2215 &cookie, &cookie_mask);
2223 /* Convert match back to nx_match. */
2224 ofpbuf_uninit(&nx_match);
2225 ofpbuf_init(&nx_match, 0);
2227 match_len = oxm_put_match(&nx_match, &match);
2228 out = oxm_match_to_string(nx_match.data, match_len);
2230 match_len = nx_put_match(&nx_match, &match,
2231 cookie, cookie_mask);
2232 out = nx_match_to_string(nx_match.data, match_len);
2238 printf("nx_pull_match() returned error %s\n",
2239 ofperr_get_name(error));
2242 ofpbuf_uninit(&nx_match);
2247 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2248 * stdin, does some internal fussing with them, and then prints them back as
2249 * strings on stdout. */
2251 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2253 return ofctl_parse_nxm__(false);
2256 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2257 * stdin, does some internal fussing with them, and then prints them back as
2258 * strings on stdout. */
2260 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2262 return ofctl_parse_nxm__(true);
2266 print_differences(const char *prefix,
2267 const void *a_, size_t a_len,
2268 const void *b_, size_t b_len)
2270 const uint8_t *a = a_;
2271 const uint8_t *b = b_;
2274 for (i = 0; i < MIN(a_len, b_len); i++) {
2276 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2277 prefix, i, a[i], b[i]);
2280 for (i = a_len; i < b_len; i++) {
2281 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2283 for (i = b_len; i < a_len; i++) {
2284 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2288 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2289 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2290 * on stdout, and then converts them back to hex bytes and prints any
2291 * differences from the input. */
2293 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2298 while (!ds_get_preprocessed_line(&in, stdin)) {
2299 struct ofpbuf of10_out;
2300 struct ofpbuf of10_in;
2301 struct ofpbuf ofpacts;
2306 /* Parse hex bytes. */
2307 ofpbuf_init(&of10_in, 0);
2308 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2309 ovs_fatal(0, "Trailing garbage in hex data");
2312 /* Convert to ofpacts. */
2313 ofpbuf_init(&ofpacts, 0);
2314 size = of10_in.size;
2315 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2317 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2318 ofpbuf_uninit(&ofpacts);
2319 ofpbuf_uninit(&of10_in);
2322 ofpbuf_push_uninit(&of10_in, size);
2324 /* Print cls_rule. */
2326 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2330 /* Convert back to ofp10 actions and print differences from input. */
2331 ofpbuf_init(&of10_out, 0);
2332 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2334 print_differences("", of10_in.data, of10_in.size,
2335 of10_out.data, of10_out.size);
2338 ofpbuf_uninit(&ofpacts);
2339 ofpbuf_uninit(&of10_in);
2340 ofpbuf_uninit(&of10_out);
2345 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2346 * bytes from stdin, converts them to cls_rules, prints them as strings on
2347 * stdout, and then converts them back to hex bytes and prints any differences
2350 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2351 * values are ignored in the input and will be set to zero when OVS converts
2352 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2353 * it does the conversion to hex, to ensure that in fact they are ignored. */
2355 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2362 while (!ds_get_preprocessed_line(&in, stdin)) {
2363 struct ofpbuf match_in, match_expout;
2364 struct ofp10_match match_out;
2365 struct ofp10_match match_normal;
2369 /* Parse hex bytes to use for expected output. */
2371 ds_put_cstr(&expout, ds_cstr(&in));
2372 for (p = ds_cstr(&expout); *p; p++) {
2377 ofpbuf_init(&match_expout, 0);
2378 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2379 ovs_fatal(0, "Trailing garbage in hex data");
2381 if (match_expout.size != sizeof(struct ofp10_match)) {
2382 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2383 match_expout.size, sizeof(struct ofp10_match));
2386 /* Parse hex bytes for input. */
2387 for (p = ds_cstr(&in); *p; p++) {
2389 *p = "0123456789abcdef"[random_uint32() & 0xf];
2392 ofpbuf_init(&match_in, 0);
2393 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2394 ovs_fatal(0, "Trailing garbage in hex data");
2396 if (match_in.size != sizeof(struct ofp10_match)) {
2397 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2398 match_in.size, sizeof(struct ofp10_match));
2401 /* Convert to cls_rule and print. */
2402 ofputil_match_from_ofp10_match(match_in.data, &match);
2403 match_print(&match);
2405 /* Convert back to ofp10_match and print differences from input. */
2406 ofputil_match_to_ofp10_match(&match, &match_out);
2407 print_differences("", match_expout.data, match_expout.size,
2408 &match_out, sizeof match_out);
2410 /* Normalize, then convert and compare again. */
2411 ofputil_normalize_match(&match);
2412 ofputil_match_to_ofp10_match(&match, &match_normal);
2413 print_differences("normal: ", &match_out, sizeof match_out,
2414 &match_normal, sizeof match_normal);
2417 ofpbuf_uninit(&match_in);
2418 ofpbuf_uninit(&match_expout);
2421 ds_destroy(&expout);
2424 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2425 * bytes from stdin, converts them to "struct match"es, prints them as strings
2426 * on stdout, and then converts them back to hex bytes and prints any
2427 * differences from the input. */
2429 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2434 while (!ds_get_preprocessed_line(&in, stdin)) {
2435 struct ofpbuf match_in;
2436 struct ofp11_match match_out;
2440 /* Parse hex bytes. */
2441 ofpbuf_init(&match_in, 0);
2442 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2443 ovs_fatal(0, "Trailing garbage in hex data");
2445 if (match_in.size != sizeof(struct ofp11_match)) {
2446 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2447 match_in.size, sizeof(struct ofp11_match));
2450 /* Convert to match. */
2451 error = ofputil_match_from_ofp11_match(match_in.data, &match);
2453 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2454 ofpbuf_uninit(&match_in);
2459 match_print(&match);
2461 /* Convert back to ofp11_match and print differences from input. */
2462 ofputil_match_to_ofp11_match(&match, &match_out);
2464 print_differences("", match_in.data, match_in.size,
2465 &match_out, sizeof match_out);
2468 ofpbuf_uninit(&match_in);
2473 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2474 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2475 * on stdout, and then converts them back to hex bytes and prints any
2476 * differences from the input. */
2478 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2483 while (!ds_get_preprocessed_line(&in, stdin)) {
2484 struct ofpbuf of11_out;
2485 struct ofpbuf of11_in;
2486 struct ofpbuf ofpacts;
2491 /* Parse hex bytes. */
2492 ofpbuf_init(&of11_in, 0);
2493 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2494 ovs_fatal(0, "Trailing garbage in hex data");
2497 /* Convert to ofpacts. */
2498 ofpbuf_init(&ofpacts, 0);
2499 size = of11_in.size;
2500 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2503 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2504 ofpbuf_uninit(&ofpacts);
2505 ofpbuf_uninit(&of11_in);
2508 ofpbuf_push_uninit(&of11_in, size);
2510 /* Print cls_rule. */
2512 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2516 /* Convert back to ofp11 actions and print differences from input. */
2517 ofpbuf_init(&of11_out, 0);
2518 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2520 print_differences("", of11_in.data, of11_in.size,
2521 of11_out.data, of11_out.size);
2524 ofpbuf_uninit(&ofpacts);
2525 ofpbuf_uninit(&of11_in);
2526 ofpbuf_uninit(&of11_out);
2531 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2532 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2533 * them as strings on stdout, and then converts them back to hex bytes and
2534 * prints any differences from the input. */
2536 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2541 while (!ds_get_preprocessed_line(&in, stdin)) {
2542 struct ofpbuf of11_out;
2543 struct ofpbuf of11_in;
2544 struct ofpbuf ofpacts;
2549 /* Parse hex bytes. */
2550 ofpbuf_init(&of11_in, 0);
2551 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2552 ovs_fatal(0, "Trailing garbage in hex data");
2555 /* Convert to ofpacts. */
2556 ofpbuf_init(&ofpacts, 0);
2557 size = of11_in.size;
2558 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2561 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2562 ofpbuf_uninit(&ofpacts);
2563 ofpbuf_uninit(&of11_in);
2566 ofpbuf_push_uninit(&of11_in, size);
2568 /* Print cls_rule. */
2570 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2574 /* Convert back to ofp11 instructions and print differences from
2576 ofpbuf_init(&of11_out, 0);
2577 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2580 print_differences("", of11_in.data, of11_in.size,
2581 of11_out.data, of11_out.size);
2584 ofpbuf_uninit(&ofpacts);
2585 ofpbuf_uninit(&of11_in);
2586 ofpbuf_uninit(&of11_out);
2591 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2592 * mask values to and from various formats and prints the results. */
2594 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
2599 struct ofputil_flow_mod fm;
2602 struct match nxm_match;
2606 struct ofp10_match of10_raw;
2607 struct match of10_match;
2609 struct ofp11_match of11_raw;
2610 struct match of11_match;
2614 match_init_catchall(&match);
2615 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
2616 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
2618 /* Convert to and from string. */
2619 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
2620 printf("%s -> ", string_s);
2622 parse_ofp_str(&fm, -1, string_s, false);
2623 printf("%04"PRIx16"/%04"PRIx16"\n",
2624 ntohs(fm.match.flow.vlan_tci),
2625 ntohs(fm.match.wc.masks.vlan_tci));
2628 /* Convert to and from NXM. */
2629 ofpbuf_init(&nxm, 0);
2630 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
2631 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
2632 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
2633 printf("NXM: %s -> ", nxm_s);
2635 printf("%s\n", ofperr_to_string(error));
2637 printf("%04"PRIx16"/%04"PRIx16"\n",
2638 ntohs(nxm_match.flow.vlan_tci),
2639 ntohs(nxm_match.wc.masks.vlan_tci));
2642 ofpbuf_uninit(&nxm);
2644 /* Convert to and from OXM. */
2645 ofpbuf_init(&nxm, 0);
2646 nxm_match_len = oxm_put_match(&nxm, &match);
2647 nxm_s = oxm_match_to_string(nxm.data, nxm_match_len);
2648 error = oxm_pull_match(&nxm, &nxm_match);
2649 printf("OXM: %s -> ", nxm_s);
2651 printf("%s\n", ofperr_to_string(error));
2653 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
2654 (VLAN_VID_MASK | VLAN_CFI);
2655 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
2656 (VLAN_VID_MASK | VLAN_CFI);
2658 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
2659 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
2660 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
2666 ofpbuf_uninit(&nxm);
2668 /* Convert to and from OpenFlow 1.0. */
2669 ofputil_match_to_ofp10_match(&match, &of10_raw);
2670 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
2671 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2672 ntohs(of10_raw.dl_vlan),
2673 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
2674 of10_raw.dl_vlan_pcp,
2675 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
2676 ntohs(of10_match.flow.vlan_tci),
2677 ntohs(of10_match.wc.masks.vlan_tci));
2679 /* Convert to and from OpenFlow 1.1. */
2680 ofputil_match_to_ofp11_match(&match, &of11_raw);
2681 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
2682 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2683 ntohs(of11_raw.dl_vlan),
2684 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
2685 of11_raw.dl_vlan_pcp,
2686 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
2687 ntohs(of11_match.flow.vlan_tci),
2688 ntohs(of11_match.wc.masks.vlan_tci));
2691 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2694 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2699 error = ofperr_from_name(argv[1]);
2701 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2704 for (version = 0; version <= UINT8_MAX; version++) {
2705 const char *name = ofperr_domain_get_name(version);
2709 printf("%s: %d,%d\n",
2710 ofperr_domain_get_name(version),
2711 ofperr_get_type(error, version),
2712 ofperr_get_code(error, version));
2716 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2717 * binary data, interpreting them as an OpenFlow message, and prints the
2718 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2720 ofctl_ofp_print(int argc, char *argv[])
2722 struct ofpbuf packet;
2724 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2725 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2726 ovs_fatal(0, "trailing garbage following hex bytes");
2728 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2729 ofpbuf_uninit(&packet);
2732 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
2733 * and dumps each message in hex. */
2735 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
2737 uint32_t bitmap = strtol(argv[1], NULL, 0);
2738 struct ofpbuf *hello;
2740 hello = ofputil_encode_hello(bitmap);
2741 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
2742 ofp_print(stdout, hello->data, hello->size, verbosity);
2743 ofpbuf_delete(hello);
2746 static const struct command all_commands[] = {
2747 { "show", 1, 1, ofctl_show },
2748 { "monitor", 1, 3, ofctl_monitor },
2749 { "snoop", 1, 1, ofctl_snoop },
2750 { "dump-desc", 1, 1, ofctl_dump_desc },
2751 { "dump-tables", 1, 1, ofctl_dump_tables },
2752 { "dump-flows", 1, 2, ofctl_dump_flows },
2753 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2754 { "queue-stats", 1, 3, ofctl_queue_stats },
2755 { "add-flow", 2, 2, ofctl_add_flow },
2756 { "add-flows", 2, 2, ofctl_add_flows },
2757 { "mod-flows", 2, 2, ofctl_mod_flows },
2758 { "del-flows", 1, 2, ofctl_del_flows },
2759 { "replace-flows", 2, 2, ofctl_replace_flows },
2760 { "diff-flows", 2, 2, ofctl_diff_flows },
2761 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2762 { "dump-ports", 1, 2, ofctl_dump_ports },
2763 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2764 { "mod-port", 3, 3, ofctl_mod_port },
2765 { "get-frags", 1, 1, ofctl_get_frags },
2766 { "set-frags", 2, 2, ofctl_set_frags },
2767 { "probe", 1, 1, ofctl_probe },
2768 { "ping", 1, 2, ofctl_ping },
2769 { "benchmark", 3, 3, ofctl_benchmark },
2770 { "help", 0, INT_MAX, ofctl_help },
2772 /* Undocumented commands for testing. */
2773 { "parse-flow", 1, 1, ofctl_parse_flow },
2774 { "parse-flows", 1, 1, ofctl_parse_flows },
2775 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
2776 { "parse-nxm", 0, 0, ofctl_parse_nxm },
2777 { "parse-oxm", 0, 0, ofctl_parse_oxm },
2778 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
2779 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
2780 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
2781 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
2782 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
2783 { "check-vlan", 2, 2, ofctl_check_vlan },
2784 { "print-error", 1, 1, ofctl_print_error },
2785 { "ofp-print", 1, 2, ofctl_ofp_print },
2786 { "encode-hello", 1, 1, ofctl_encode_hello },
2788 { NULL, 0, 0, NULL },