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.
21 #include <sys/socket.h>
27 #include <sys/fcntl.h>
31 #include "byte-order.h"
32 #include "classifier.h"
33 #include "command-line.h"
37 #include "dynamic-string.h"
41 #include "ofp-errors.h"
42 #include "ofp-parse.h"
43 #include "ofp-print.h"
46 #include "ofproto/ofproto.h"
47 #include "openflow/nicira-ext.h"
48 #include "openflow/openflow.h"
50 #include "poll-loop.h"
52 #include "stream-ssl.h"
59 VLOG_DEFINE_THIS_MODULE(ofctl);
61 /* --strict: Use strict matching for flow mod commands? Additionally governs
62 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
66 /* --readd: If true, on replace-flows, re-add even flows that have not changed
67 * (to reset flow counters). */
70 /* -F, --flow-format: Allowed protocols. By default, any protocol is
72 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
74 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
75 * commands. Either one of NXPIF_* to force a particular packet_in format, or
76 * -1 to let ovs-ofctl choose the default. */
77 static int preferred_packet_in_format = -1;
79 /* -m, --more: Additional verbosity for ofp-print functions. */
82 /* --timestamp: Print a timestamp before each received packet on "monitor" and
84 static bool timestamp;
86 static const struct command all_commands[];
88 static void usage(void) NO_RETURN;
89 static void parse_options(int argc, char *argv[]);
92 main(int argc, char *argv[])
94 set_program_name(argv[0]);
95 parse_options(argc, argv);
96 signal(SIGPIPE, SIG_IGN);
97 run_command(argc - optind, argv + optind, all_commands);
102 parse_options(int argc, char *argv[])
105 OPT_STRICT = UCHAR_MAX + 1,
111 static struct option long_options[] = {
112 {"timeout", required_argument, NULL, 't'},
113 {"strict", no_argument, NULL, OPT_STRICT},
114 {"readd", no_argument, NULL, OPT_READD},
115 {"flow-format", required_argument, NULL, 'F'},
116 {"packet-in-format", required_argument, NULL, 'P'},
117 {"more", no_argument, NULL, 'm'},
118 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
119 {"help", no_argument, NULL, 'h'},
120 {"version", no_argument, NULL, 'V'},
123 STREAM_SSL_LONG_OPTIONS,
126 char *short_options = long_options_to_short_options(long_options);
129 unsigned long int timeout;
132 c = getopt_long(argc, argv, short_options, long_options, NULL);
139 timeout = strtoul(optarg, NULL, 10);
141 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
149 allowed_protocols = ofputil_protocols_from_string(optarg);
150 if (!allowed_protocols) {
151 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
156 preferred_packet_in_format =
157 ofputil_packet_in_format_from_string(optarg);
158 if (preferred_packet_in_format < 0) {
159 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
171 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
186 DAEMON_OPTION_HANDLERS
188 STREAM_SSL_OPTION_HANDLERS
203 printf("%s: OpenFlow switch management utility\n"
204 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
205 "\nFor OpenFlow switches:\n"
206 " show SWITCH show OpenFlow information\n"
207 " dump-desc SWITCH print switch description\n"
208 " dump-tables SWITCH print table stats\n"
209 " mod-port SWITCH IFACE ACT modify port behavior\n"
210 " get-frags SWITCH print fragment handling behavior\n"
211 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
212 " dump-ports SWITCH [PORT] print port statistics\n"
213 " dump-ports-desc SWITCH print port descriptions\n"
214 " dump-flows SWITCH print all flow entries\n"
215 " dump-flows SWITCH FLOW print matching FLOWs\n"
216 " dump-aggregate SWITCH print aggregate flow statistics\n"
217 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
218 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
219 " add-flow SWITCH FLOW add flow described by FLOW\n"
220 " add-flows SWITCH FILE add flows from FILE\n"
221 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
222 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
223 " replace-flows SWITCH FILE replace flows with those in FILE\n"
224 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
225 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
226 " execute ACTIONS on PACKET\n"
227 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
228 " print packets received from SWITCH\n"
229 " snoop SWITCH snoop on SWITCH and its controller\n"
230 "\nFor OpenFlow switches and controllers:\n"
231 " probe TARGET probe whether TARGET is up\n"
232 " ping TARGET [N] latency of N-byte echos\n"
233 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
234 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
235 program_name, program_name);
236 vconn_usage(true, false, false);
239 printf("\nOther options:\n"
240 " --strict use strict match for flow commands\n"
241 " --readd replace flows that haven't changed\n"
242 " -F, --flow-format=FORMAT force particular flow format\n"
243 " -P, --packet-in-format=FRMT force particular packet in format\n"
244 " -m, --more be more verbose printing OpenFlow\n"
245 " --timestamp (monitor, snoop) print timestamps\n"
246 " -t, --timeout=SECS give up after SECS seconds\n"
247 " -h, --help display this help message\n"
248 " -V, --version display version information\n");
253 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
254 const char *argv[] OVS_UNUSED, void *exiting_)
256 bool *exiting = exiting_;
258 unixctl_command_reply(conn, NULL);
261 static void run(int retval, const char *message, ...)
264 static void run(int retval, const char *message, ...)
269 va_start(args, message);
270 ovs_fatal_valist(retval, message, args);
274 /* Generic commands. */
277 open_vconn_socket(const char *name, struct vconn **vconnp)
279 char *vconn_name = xasprintf("unix:%s", name);
280 VLOG_DBG("connecting to %s", vconn_name);
281 run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp),
282 "connecting to %s", vconn_name);
286 static enum ofputil_protocol
287 open_vconn__(const char *name, const char *default_suffix,
288 struct vconn **vconnp)
290 char *datapath_name, *datapath_type, *socket_name;
291 enum ofputil_protocol protocol;
296 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
298 ofproto_parse_name(name, &datapath_name, &datapath_type);
299 socket_name = xasprintf("%s/%s.%s",
300 ovs_rundir(), datapath_name, default_suffix);
304 if (strchr(name, ':')) {
305 run(vconn_open_block(name, OFP10_VERSION, vconnp),
306 "connecting to %s", name);
307 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
308 open_vconn_socket(name, vconnp);
309 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
310 open_vconn_socket(bridge_path, vconnp);
311 } else if (!stat(socket_name, &s)) {
312 if (!S_ISSOCK(s.st_mode)) {
313 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
316 open_vconn_socket(socket_name, vconnp);
318 ovs_fatal(0, "%s is not a bridge or a socket", name);
324 ofp_version = vconn_get_version(*vconnp);
325 protocol = ofputil_protocol_from_ofp_version(ofp_version);
327 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
333 static enum ofputil_protocol
334 open_vconn(const char *name, struct vconn **vconnp)
336 return open_vconn__(name, "mgmt", vconnp);
340 alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
342 struct ofp_stats_msg *rq;
344 rq = make_openflow(rq_len, OFPT10_STATS_REQUEST, bufferp);
345 rq->type = htons(type);
346 rq->flags = htons(0);
351 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
353 update_openflow_length(buffer);
354 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
358 dump_transaction(const char *vconn_name, struct ofpbuf *request)
361 struct ofpbuf *reply;
363 update_openflow_length(request);
364 open_vconn(vconn_name, &vconn);
365 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
366 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
367 ofpbuf_delete(reply);
372 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
374 struct ofpbuf *request;
375 make_openflow(sizeof(struct ofp_header), request_type, &request);
376 dump_transaction(vconn_name, request);
380 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
382 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
386 open_vconn(vconn_name, &vconn);
387 send_openflow_buffer(vconn, request);
390 struct ofpbuf *reply;
392 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
393 recv_xid = ((struct ofp_header *) reply->data)->xid;
394 if (send_xid == recv_xid) {
395 struct ofp_stats_msg *osm;
397 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
399 osm = ofpbuf_at(reply, 0, sizeof *osm);
400 done = !osm || !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
402 VLOG_DBG("received reply with xid %08"PRIx32" "
403 "!= expected %08"PRIx32, recv_xid, send_xid);
405 ofpbuf_delete(reply);
411 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
413 struct ofpbuf *request;
414 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
415 dump_stats_transaction(vconn_name, request);
418 /* Sends 'request', which should be a request that only has a reply if an error
419 * occurs, and waits for it to succeed or fail. If an error does occur, prints
420 * it and exits with an error.
422 * Destroys all of the 'requests'. */
424 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
426 struct ofpbuf *request, *reply;
428 LIST_FOR_EACH (request, list_node, requests) {
429 update_openflow_length(request);
432 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
433 "talking to %s", vconn_get_name(vconn));
435 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
438 ofpbuf_delete(reply);
441 /* Sends 'request', which should be a request that only has a reply if an error
442 * occurs, and waits for it to succeed or fail. If an error does occur, prints
443 * it and exits with an error.
445 * Destroys 'request'. */
447 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
449 struct list requests;
451 list_init(&requests);
452 list_push_back(&requests, &request->list_node);
453 transact_multiple_noreply(vconn, &requests);
457 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
459 struct ofp_switch_config *config;
460 struct ofp_header *header;
461 struct ofpbuf *request;
462 struct ofpbuf *reply;
464 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
466 run(vconn_transact(vconn, request, &reply),
467 "talking to %s", vconn_get_name(vconn));
469 header = reply->data;
470 if (header->type != OFPT_GET_CONFIG_REPLY ||
471 header->length != htons(sizeof *config)) {
472 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
475 config = reply->data;
478 ofpbuf_delete(reply);
482 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
484 struct ofp_switch_config *config;
485 struct ofp_header save_header;
486 struct ofpbuf *request;
488 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
489 save_header = config->header;
491 config->header = save_header;
493 transact_noreply(vconn, request);
497 do_show(int argc OVS_UNUSED, char *argv[])
499 const char *vconn_name = argv[1];
501 struct ofpbuf *request;
502 struct ofpbuf *reply;
505 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST,
507 open_vconn(vconn_name, &vconn);
508 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
510 trunc = ofputil_switch_features_ports_trunc(reply);
511 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
513 ofpbuf_delete(reply);
517 /* The Features Reply may not contain all the ports, so send a
518 * Port Description stats request, which doesn't have size
520 dump_trivial_stats_transaction(vconn_name, OFPST_PORT_DESC);
522 dump_trivial_transaction(vconn_name, OFPT_GET_CONFIG_REQUEST);
526 do_dump_desc(int argc OVS_UNUSED, char *argv[])
528 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
532 do_dump_tables(int argc OVS_UNUSED, char *argv[])
534 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
537 /* Opens a connection to 'vconn_name', fetches the port structure for
538 * 'port_name' (which may be a port name or number), and copies it into
541 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
542 struct ofputil_phy_port *pp)
544 struct ofputil_switch_features features;
545 const struct ofp_switch_features *osf;
546 struct ofpbuf *request, *reply;
547 unsigned int port_no;
552 /* Try to interpret the argument as a port number. */
553 if (!str_to_uint(port_name, 10, &port_no)) {
557 /* Fetch the switch's ofp_switch_features. */
558 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
559 open_vconn(vconn_name, &vconn);
560 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
563 if (reply->size < sizeof *osf) {
564 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
565 vconn_name, reply->size);
567 error = ofputil_decode_switch_features(osf, &features, &b);
569 ovs_fatal(0, "%s: failed to decode features reply (%s)",
570 vconn_name, ofperr_to_string(error));
573 while (!ofputil_pull_phy_port(osf->header.version, &b, pp)) {
574 if (port_no != UINT_MAX
575 ? port_no == pp->port_no
576 : !strcmp(pp->name, port_name)) {
577 ofpbuf_delete(reply);
582 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
585 /* Returns the port number corresponding to 'port_name' (which may be a port
586 * name or number) within the switch 'vconn_name'. */
588 str_to_port_no(const char *vconn_name, const char *port_name)
590 unsigned int port_no;
592 if (str_to_uint(port_name, 10, &port_no)) {
595 struct ofputil_phy_port pp;
597 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
603 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
604 enum ofputil_protocol *cur)
607 struct ofpbuf *request, *reply;
608 enum ofputil_protocol next;
610 request = ofputil_encode_set_protocol(*cur, want, &next);
615 run(vconn_transact_noreply(vconn, request, &reply),
616 "talking to %s", vconn_get_name(vconn));
618 char *s = ofp_to_string(reply->data, reply->size, 2);
619 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
620 vconn_get_name(vconn), s);
622 ofpbuf_delete(reply);
630 static enum ofputil_protocol
631 set_protocol_for_flow_dump(struct vconn *vconn,
632 enum ofputil_protocol cur_protocol,
633 enum ofputil_protocol usable_protocols)
638 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
639 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
640 if (f & usable_protocols & allowed_protocols
641 && try_set_protocol(vconn, f, &cur_protocol)) {
646 usable_s = ofputil_protocols_to_string(usable_protocols);
647 if (usable_protocols & allowed_protocols) {
648 ovs_fatal(0, "switch does not support any of the usable flow "
649 "formats (%s)", usable_s);
651 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
652 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
653 "allowed flow formats (%s)", usable_s, allowed_s);
658 do_dump_flows__(int argc, char *argv[], bool aggregate)
660 enum ofputil_protocol usable_protocols, protocol;
661 struct ofputil_flow_stats_request fsr;
662 struct ofpbuf *request;
665 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
666 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
668 protocol = open_vconn(argv[1], &vconn);
669 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
670 request = ofputil_encode_flow_stats_request(&fsr, protocol);
671 dump_stats_transaction(argv[1], request);
676 do_dump_flows(int argc, char *argv[])
678 return do_dump_flows__(argc, argv, false);
682 do_dump_aggregate(int argc, char *argv[])
684 return do_dump_flows__(argc, argv, true);
688 do_queue_stats(int argc, char *argv[])
690 struct ofp_queue_stats_request *req;
691 struct ofpbuf *request;
693 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
695 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
696 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
698 req->port_no = htons(OFPP_ALL);
700 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
701 req->queue_id = htonl(atoi(argv[3]));
703 req->queue_id = htonl(OFPQ_ALL);
706 memset(req->pad, 0, sizeof req->pad);
708 dump_stats_transaction(argv[1], request);
711 static enum ofputil_protocol
712 open_vconn_for_flow_mod(const char *remote,
713 const struct ofputil_flow_mod *fms, size_t n_fms,
714 struct vconn **vconnp)
716 enum ofputil_protocol usable_protocols;
717 enum ofputil_protocol cur_protocol;
721 /* Figure out what flow formats will work. */
722 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
723 if (!(usable_protocols & allowed_protocols)) {
724 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
725 usable_s = ofputil_protocols_to_string(usable_protocols);
726 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
727 "allowed flow formats (%s)", usable_s, allowed_s);
730 /* If the initial flow format is allowed and usable, keep it. */
731 cur_protocol = open_vconn(remote, vconnp);
732 if (usable_protocols & allowed_protocols & cur_protocol) {
736 /* Otherwise try each flow format in turn. */
737 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
738 enum ofputil_protocol f = 1 << i;
740 if (f != cur_protocol
741 && f & usable_protocols & allowed_protocols
742 && try_set_protocol(*vconnp, f, &cur_protocol)) {
747 usable_s = ofputil_protocols_to_string(usable_protocols);
748 ovs_fatal(0, "switch does not support any of the usable flow "
749 "formats (%s)", usable_s);
753 do_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, size_t n_fms)
755 enum ofputil_protocol protocol;
759 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
761 for (i = 0; i < n_fms; i++) {
762 struct ofputil_flow_mod *fm = &fms[i];
764 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
771 do_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
773 struct ofputil_flow_mod *fms = NULL;
776 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
777 do_flow_mod__(argv[1], fms, n_fms);
782 do_flow_mod(int argc, char *argv[], uint16_t command)
784 if (argc > 2 && !strcmp(argv[2], "-")) {
785 do_flow_mod_file(argc, argv, command);
787 struct ofputil_flow_mod fm;
788 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
789 do_flow_mod__(argv[1], &fm, 1);
794 do_add_flow(int argc, char *argv[])
796 do_flow_mod(argc, argv, OFPFC_ADD);
800 do_add_flows(int argc, char *argv[])
802 do_flow_mod_file(argc, argv, OFPFC_ADD);
806 do_mod_flows(int argc, char *argv[])
808 do_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
812 do_del_flows(int argc, char *argv[])
814 do_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
818 set_packet_in_format(struct vconn *vconn,
819 enum nx_packet_in_format packet_in_format)
821 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
822 transact_noreply(vconn, spif);
823 VLOG_DBG("%s: using user-specified packet in format %s",
824 vconn_get_name(vconn),
825 ofputil_packet_in_format_to_string(packet_in_format));
829 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
831 struct ofp_switch_config config;
832 enum ofp_config_flags flags;
834 fetch_switch_config(vconn, &config);
835 flags = ntohs(config.flags);
836 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
837 /* Set the invalid ttl config. */
838 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
840 config.flags = htons(flags);
841 set_switch_config(vconn, &config);
843 /* Then retrieve the configuration to see if it really took. OpenFlow
844 * doesn't define error reporting for bad modes, so this is all we can
846 fetch_switch_config(vconn, &config);
847 flags = ntohs(config.flags);
848 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
849 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
850 "switch probably doesn't support mode)");
857 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
858 * caller must free '*msgp'. On success, returns NULL. On failure, returns
859 * an error message and stores NULL in '*msgp'. */
861 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
863 struct ofp_header *oh;
866 msg = ofpbuf_new(strlen(hex) / 2);
869 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
871 return "Trailing garbage in hex data";
874 if (msg->size < sizeof(struct ofp_header)) {
876 return "Message too short for OpenFlow";
880 if (msg->size != ntohs(oh->length)) {
882 return "Message size does not match length in OpenFlow header";
890 ofctl_send(struct unixctl_conn *conn, int argc,
891 const char *argv[], void *vconn_)
893 struct vconn *vconn = vconn_;
900 for (i = 1; i < argc; i++) {
901 const char *error_msg;
905 error_msg = openflow_from_hex(argv[i], &msg);
907 ds_put_format(&reply, "%s\n", error_msg);
912 fprintf(stderr, "send: ");
913 ofp_print(stderr, msg->data, msg->size, verbosity);
915 error = vconn_send_block(vconn, msg);
918 ds_put_format(&reply, "%s\n", strerror(error));
921 ds_put_cstr(&reply, "sent\n");
926 unixctl_command_reply(conn, ds_cstr(&reply));
928 unixctl_command_reply_error(conn, ds_cstr(&reply));
934 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
935 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
939 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
940 const char *argv[] OVS_UNUSED, void *aux_)
942 struct barrier_aux *aux = aux_;
947 unixctl_command_reply_error(conn, "already waiting for barrier reply");
951 msg = ofputil_encode_barrier_request();
952 error = vconn_send_block(aux->vconn, msg);
955 unixctl_command_reply_error(conn, strerror(error));
962 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
963 const char *argv[], void *aux OVS_UNUSED)
967 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
969 unixctl_command_reply_error(conn, strerror(errno));
974 dup2(fd, STDERR_FILENO);
976 unixctl_command_reply(conn, NULL);
980 monitor_vconn(struct vconn *vconn)
982 struct barrier_aux barrier_aux = { vconn, NULL };
983 struct unixctl_server *server;
984 bool exiting = false;
987 daemon_save_fd(STDERR_FILENO);
989 error = unixctl_server_create(NULL, &server);
991 ovs_fatal(error, "failed to create unixctl server");
993 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
994 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
996 unixctl_command_register("ofctl/barrier", "", 0, 0,
997 ofctl_barrier, &barrier_aux);
998 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
999 ofctl_set_output_file, NULL);
1000 daemonize_complete();
1006 unixctl_server_run(server);
1011 retval = vconn_recv(vconn, &b);
1012 if (retval == EAGAIN) {
1015 run(retval, "vconn_recv");
1018 time_t now = time_wall();
1021 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", localtime(&now));
1025 msg_type = ((const struct ofp_header *) b->data)->type;
1026 ofp_print(stderr, b->data, b->size, verbosity + 2);
1029 if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) {
1030 unixctl_command_reply(barrier_aux.conn, NULL);
1031 barrier_aux.conn = NULL;
1040 vconn_run_wait(vconn);
1041 vconn_recv_wait(vconn);
1042 unixctl_server_wait(server);
1046 unixctl_server_destroy(server);
1050 do_monitor(int argc, char *argv[])
1052 struct vconn *vconn;
1054 open_vconn(argv[1], &vconn);
1056 struct ofp_switch_config config;
1058 fetch_switch_config(vconn, &config);
1059 config.miss_send_len = htons(atoi(argv[2]));
1060 set_switch_config(vconn, &config);
1063 if (!strcmp(argv[3], "invalid_ttl")) {
1064 monitor_set_invalid_ttl_to_controller(vconn);
1067 if (preferred_packet_in_format >= 0) {
1068 set_packet_in_format(vconn, preferred_packet_in_format);
1070 struct ofpbuf *spif, *reply;
1072 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1073 run(vconn_transact_noreply(vconn, spif, &reply),
1074 "talking to %s", vconn_get_name(vconn));
1076 char *s = ofp_to_string(reply->data, reply->size, 2);
1077 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1078 " replied: %s. Falling back to the switch default.",
1079 vconn_get_name(vconn), s);
1081 ofpbuf_delete(reply);
1085 monitor_vconn(vconn);
1089 do_snoop(int argc OVS_UNUSED, char *argv[])
1091 struct vconn *vconn;
1093 open_vconn__(argv[1], "snoop", &vconn);
1094 monitor_vconn(vconn);
1098 do_dump_ports(int argc, char *argv[])
1100 struct ofp_port_stats_request *req;
1101 struct ofpbuf *request;
1104 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1105 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1106 req->port_no = htons(port);
1107 dump_stats_transaction(argv[1], request);
1111 do_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1113 dump_trivial_stats_transaction(argv[1], OFPST_PORT_DESC);
1117 do_probe(int argc OVS_UNUSED, char *argv[])
1119 struct ofpbuf *request;
1120 struct vconn *vconn;
1121 struct ofpbuf *reply;
1123 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1124 open_vconn(argv[1], &vconn);
1125 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1126 if (reply->size != sizeof(struct ofp_header)) {
1127 ovs_fatal(0, "reply does not match request");
1129 ofpbuf_delete(reply);
1134 do_packet_out(int argc, char *argv[])
1136 struct ofputil_packet_out po;
1137 struct ofpbuf actions;
1138 struct vconn *vconn;
1141 ofpbuf_init(&actions, sizeof(union ofp_action));
1142 parse_ofp_actions(argv[3], &actions);
1144 po.buffer_id = UINT32_MAX;
1145 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1146 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1147 : str_to_port_no(argv[1], argv[2]));
1148 po.actions = actions.data;
1149 po.n_actions = actions.size / sizeof(union ofp_action);
1151 open_vconn(argv[1], &vconn);
1152 for (i = 4; i < argc; i++) {
1153 struct ofpbuf *packet, *opo;
1154 const char *error_msg;
1156 error_msg = eth_from_hex(argv[i], &packet);
1158 ovs_fatal(0, "%s", error_msg);
1161 po.packet = packet->data;
1162 po.packet_len = packet->size;
1163 opo = ofputil_encode_packet_out(&po);
1164 transact_noreply(vconn, opo);
1165 ofpbuf_delete(packet);
1168 ofpbuf_uninit(&actions);
1172 do_mod_port(int argc OVS_UNUSED, char *argv[])
1174 enum ofputil_protocol protocol;
1175 struct ofputil_port_mod pm;
1176 struct ofputil_phy_port pp;
1177 struct vconn *vconn;
1179 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1181 pm.port_no = pp.port_no;
1182 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1187 if (!strcasecmp(argv[3], "up")) {
1188 pm.mask |= OFPUTIL_PC_PORT_DOWN;
1189 } else if (!strcasecmp(argv[3], "down")) {
1190 pm.mask |= OFPUTIL_PC_PORT_DOWN;
1191 pm.config |= OFPUTIL_PC_PORT_DOWN;
1192 } else if (!strcasecmp(argv[3], "flood")) {
1193 pm.mask |= OFPUTIL_PC_NO_FLOOD;
1194 } else if (!strcasecmp(argv[3], "noflood")) {
1195 pm.mask |= OFPUTIL_PC_NO_FLOOD;
1196 pm.config |= OFPUTIL_PC_NO_FLOOD;
1197 } else if (!strcasecmp(argv[3], "forward")) {
1198 pm.mask |= OFPUTIL_PC_NO_FWD;
1199 } else if (!strcasecmp(argv[3], "noforward")) {
1200 pm.mask |= OFPUTIL_PC_NO_FWD;
1201 pm.config |= OFPUTIL_PC_NO_FWD;
1203 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1206 protocol = open_vconn(argv[1], &vconn);
1207 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1212 do_get_frags(int argc OVS_UNUSED, char *argv[])
1214 struct ofp_switch_config config;
1215 struct vconn *vconn;
1217 open_vconn(argv[1], &vconn);
1218 fetch_switch_config(vconn, &config);
1219 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1224 do_set_frags(int argc OVS_UNUSED, char *argv[])
1226 struct ofp_switch_config config;
1227 enum ofp_config_flags mode;
1228 struct vconn *vconn;
1231 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1232 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1235 open_vconn(argv[1], &vconn);
1236 fetch_switch_config(vconn, &config);
1237 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1238 if (flags != config.flags) {
1239 /* Set the configuration. */
1240 config.flags = flags;
1241 set_switch_config(vconn, &config);
1243 /* Then retrieve the configuration to see if it really took. OpenFlow
1244 * doesn't define error reporting for bad modes, so this is all we can
1246 fetch_switch_config(vconn, &config);
1247 if (flags != config.flags) {
1248 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1249 "switch probably doesn't support mode \"%s\")",
1250 argv[1], ofputil_frag_handling_to_string(mode));
1257 do_ping(int argc, char *argv[])
1259 size_t max_payload = 65535 - sizeof(struct ofp_header);
1260 unsigned int payload;
1261 struct vconn *vconn;
1264 payload = argc > 2 ? atoi(argv[2]) : 64;
1265 if (payload > max_payload) {
1266 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1269 open_vconn(argv[1], &vconn);
1270 for (i = 0; i < 10; i++) {
1271 struct timeval start, end;
1272 struct ofpbuf *request, *reply;
1273 struct ofp_header *rq_hdr, *rpy_hdr;
1275 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1276 OFPT_ECHO_REQUEST, &request);
1277 random_bytes(rq_hdr + 1, payload);
1279 xgettimeofday(&start);
1280 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1281 xgettimeofday(&end);
1283 rpy_hdr = reply->data;
1284 if (reply->size != request->size
1285 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1286 || rpy_hdr->xid != rq_hdr->xid
1287 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1288 printf("Reply does not match request. Request:\n");
1289 ofp_print(stdout, request, request->size, verbosity + 2);
1291 ofp_print(stdout, reply, reply->size, verbosity + 2);
1293 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1294 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
1295 (1000*(double)(end.tv_sec - start.tv_sec))
1296 + (.001*(end.tv_usec - start.tv_usec)));
1297 ofpbuf_delete(request);
1298 ofpbuf_delete(reply);
1304 do_benchmark(int argc OVS_UNUSED, char *argv[])
1306 size_t max_payload = 65535 - sizeof(struct ofp_header);
1307 struct timeval start, end;
1308 unsigned int payload_size, message_size;
1309 struct vconn *vconn;
1314 payload_size = atoi(argv[2]);
1315 if (payload_size > max_payload) {
1316 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1318 message_size = sizeof(struct ofp_header) + payload_size;
1320 count = atoi(argv[3]);
1322 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1323 count, message_size, count * message_size);
1325 open_vconn(argv[1], &vconn);
1326 xgettimeofday(&start);
1327 for (i = 0; i < count; i++) {
1328 struct ofpbuf *request, *reply;
1329 struct ofp_header *rq_hdr;
1331 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1332 memset(rq_hdr + 1, 0, payload_size);
1333 run(vconn_transact(vconn, request, &reply), "transact");
1334 ofpbuf_delete(reply);
1336 xgettimeofday(&end);
1339 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1340 + (.001*(end.tv_usec - start.tv_usec)));
1341 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1342 duration, count / (duration / 1000.0),
1343 count * message_size / (duration / 1000.0));
1347 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1352 /* replace-flows and diff-flows commands. */
1354 /* A flow table entry, possibly with two different versions. */
1356 struct cls_rule rule; /* Within a "struct classifier". */
1357 struct fte_version *versions[2];
1360 /* One version of a Flow Table Entry. */
1361 struct fte_version {
1363 uint16_t idle_timeout;
1364 uint16_t hard_timeout;
1366 union ofp_action *actions;
1370 /* Frees 'version' and the data that it owns. */
1372 fte_version_free(struct fte_version *version)
1375 free(version->actions);
1380 /* Returns true if 'a' and 'b' are the same, false if they differ.
1382 * Ignores differences in 'flags' because there's no way to retrieve flags from
1383 * an OpenFlow switch. We have to assume that they are the same. */
1385 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1387 return (a->cookie == b->cookie
1388 && a->idle_timeout == b->idle_timeout
1389 && a->hard_timeout == b->hard_timeout
1390 && a->n_actions == b->n_actions
1391 && !memcmp(a->actions, b->actions,
1392 a->n_actions * sizeof *a->actions));
1395 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1396 * associated with the version. */
1398 fte_version_print(const struct fte_version *version)
1402 if (version->cookie != htonll(0)) {
1403 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1405 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1406 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1408 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1409 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1413 ofp_print_actions(&s, version->actions, version->n_actions);
1414 printf(" %s\n", ds_cstr(&s));
1419 fte_from_cls_rule(const struct cls_rule *cls_rule)
1421 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1424 /* Frees 'fte' and its versions. */
1426 fte_free(struct fte *fte)
1429 fte_version_free(fte->versions[0]);
1430 fte_version_free(fte->versions[1]);
1435 /* Frees all of the FTEs within 'cls'. */
1437 fte_free_all(struct classifier *cls)
1439 struct cls_cursor cursor;
1440 struct fte *fte, *next;
1442 cls_cursor_init(&cursor, cls, NULL);
1443 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1444 classifier_remove(cls, &fte->rule);
1447 classifier_destroy(cls);
1450 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1451 * necessary. Sets 'version' as the version of that rule with the given
1452 * 'index', replacing any existing version, if any.
1454 * Takes ownership of 'version'. */
1456 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1457 struct fte_version *version, int index)
1459 struct fte *old, *fte;
1461 fte = xzalloc(sizeof *fte);
1463 fte->versions[index] = version;
1465 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1467 fte_version_free(old->versions[index]);
1468 fte->versions[!index] = old->versions[!index];
1473 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1474 * with the specified 'index'. Returns the flow formats able to represent the
1475 * flows that were read. */
1476 static enum ofputil_protocol
1477 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1479 enum ofputil_protocol usable_protocols;
1483 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1485 ovs_fatal(errno, "%s: open", filename);
1489 usable_protocols = OFPUTIL_P_ANY;
1490 while (!ds_get_preprocessed_line(&s, file)) {
1491 struct fte_version *version;
1492 struct ofputil_flow_mod fm;
1494 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1496 version = xmalloc(sizeof *version);
1497 version->cookie = fm.cookie;
1498 version->idle_timeout = fm.idle_timeout;
1499 version->hard_timeout = fm.hard_timeout;
1500 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1501 version->actions = fm.actions;
1502 version->n_actions = fm.n_actions;
1504 usable_protocols &= ofputil_usable_protocols(&fm.cr);
1506 fte_insert(cls, &fm.cr, version, index);
1510 if (file != stdin) {
1514 return usable_protocols;
1517 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1518 * format 'protocol', and adds them as flow table entries in 'cls' for the
1519 * version with the specified 'index'. */
1521 read_flows_from_switch(struct vconn *vconn,
1522 enum ofputil_protocol protocol,
1523 struct classifier *cls, int index)
1525 struct ofputil_flow_stats_request fsr;
1526 struct ofpbuf *request;
1530 fsr.aggregate = false;
1531 cls_rule_init_catchall(&fsr.match, 0);
1532 fsr.out_port = OFPP_NONE;
1533 fsr.table_id = 0xff;
1534 fsr.cookie = fsr.cookie_mask = htonll(0);
1535 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1536 send_xid = ((struct ofp_header *) request->data)->xid;
1537 send_openflow_buffer(vconn, request);
1542 struct ofpbuf *reply;
1544 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1545 recv_xid = ((struct ofp_header *) reply->data)->xid;
1546 if (send_xid == recv_xid) {
1547 const struct ofputil_msg_type *type;
1548 const struct ofp_stats_msg *osm;
1549 enum ofputil_msg_code code;
1551 ofputil_decode_msg_type(reply->data, &type);
1552 code = ofputil_msg_type_code(type);
1553 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1554 code != OFPUTIL_NXST_FLOW_REPLY) {
1555 ovs_fatal(0, "received bad reply: %s",
1556 ofp_to_string(reply->data, reply->size,
1561 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
1566 struct fte_version *version;
1567 struct ofputil_flow_stats fs;
1570 retval = ofputil_decode_flow_stats_reply(&fs, reply, false);
1572 if (retval != EOF) {
1573 ovs_fatal(0, "parse error in reply");
1578 version = xmalloc(sizeof *version);
1579 version->cookie = fs.cookie;
1580 version->idle_timeout = fs.idle_timeout;
1581 version->hard_timeout = fs.hard_timeout;
1583 version->n_actions = fs.n_actions;
1584 version->actions = xmemdup(fs.actions,
1585 fs.n_actions * sizeof *fs.actions);
1587 fte_insert(cls, &fs.rule, version, index);
1590 VLOG_DBG("received reply with xid %08"PRIx32" "
1591 "!= expected %08"PRIx32, recv_xid, send_xid);
1593 ofpbuf_delete(reply);
1598 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1599 enum ofputil_protocol protocol, struct list *packets)
1601 const struct fte_version *version = fte->versions[index];
1602 struct ofputil_flow_mod fm;
1606 fm.cookie = version->cookie;
1608 fm.command = command;
1609 fm.idle_timeout = version->idle_timeout;
1610 fm.hard_timeout = version->hard_timeout;
1611 fm.buffer_id = UINT32_MAX;
1612 fm.out_port = OFPP_NONE;
1613 fm.flags = version->flags;
1614 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1615 command == OFPFC_MODIFY_STRICT) {
1616 fm.actions = version->actions;
1617 fm.n_actions = version->n_actions;
1623 ofm = ofputil_encode_flow_mod(&fm, protocol);
1624 list_push_back(packets, &ofm->list_node);
1628 do_replace_flows(int argc OVS_UNUSED, char *argv[])
1630 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1631 enum ofputil_protocol usable_protocols, protocol;
1632 struct cls_cursor cursor;
1633 struct classifier cls;
1634 struct list requests;
1635 struct vconn *vconn;
1638 classifier_init(&cls);
1639 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
1641 protocol = open_vconn(argv[1], &vconn);
1642 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1644 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
1646 list_init(&requests);
1648 /* Delete flows that exist on the switch but not in the file. */
1649 cls_cursor_init(&cursor, &cls, NULL);
1650 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1651 struct fte_version *file_ver = fte->versions[FILE_IDX];
1652 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1654 if (sw_ver && !file_ver) {
1655 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1656 protocol, &requests);
1660 /* Add flows that exist in the file but not on the switch.
1661 * Update flows that exist in both places but differ. */
1662 cls_cursor_init(&cursor, &cls, NULL);
1663 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1664 struct fte_version *file_ver = fte->versions[FILE_IDX];
1665 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1668 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1669 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
1672 transact_multiple_noreply(vconn, &requests);
1679 read_flows_from_source(const char *source, struct classifier *cls, int index)
1683 if (source[0] == '/' || source[0] == '.'
1684 || (!strchr(source, ':') && !stat(source, &s))) {
1685 read_flows_from_file(source, cls, index);
1687 enum ofputil_protocol protocol;
1688 struct vconn *vconn;
1690 protocol = open_vconn(source, &vconn);
1691 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
1692 read_flows_from_switch(vconn, protocol, cls, index);
1698 do_diff_flows(int argc OVS_UNUSED, char *argv[])
1700 bool differences = false;
1701 struct cls_cursor cursor;
1702 struct classifier cls;
1705 classifier_init(&cls);
1706 read_flows_from_source(argv[1], &cls, 0);
1707 read_flows_from_source(argv[2], &cls, 1);
1709 cls_cursor_init(&cursor, &cls, NULL);
1710 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1711 struct fte_version *a = fte->versions[0];
1712 struct fte_version *b = fte->versions[1];
1714 if (!a || !b || !fte_version_equals(a, b)) {
1715 char *rule_s = cls_rule_to_string(&fte->rule);
1717 printf("-%s", rule_s);
1718 fte_version_print(a);
1721 printf("+%s", rule_s);
1722 fte_version_print(b);
1737 /* Undocumented commands for unit testing. */
1740 do_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
1742 enum ofputil_protocol usable_protocols;
1743 enum ofputil_protocol protocol = 0;
1747 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1748 usable_s = ofputil_protocols_to_string(usable_protocols);
1749 printf("usable protocols: %s\n", usable_s);
1752 if (!(usable_protocols & allowed_protocols)) {
1753 ovs_fatal(0, "no usable protocol");
1755 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1757 if (protocol & usable_protocols & allowed_protocols) {
1761 assert(IS_POW2(protocol));
1763 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
1765 for (i = 0; i < n_fms; i++) {
1766 struct ofputil_flow_mod *fm = &fms[i];
1769 msg = ofputil_encode_flow_mod(fm, protocol);
1770 ofp_print(stdout, msg->data, msg->size, verbosity);
1777 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1778 * it back to stdout. */
1780 do_parse_flow(int argc OVS_UNUSED, char *argv[])
1782 struct ofputil_flow_mod fm;
1784 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
1785 do_parse_flows__(&fm, 1);
1788 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1789 * add-flows) and prints each of the flows back to stdout. */
1791 do_parse_flows(int argc OVS_UNUSED, char *argv[])
1793 struct ofputil_flow_mod *fms = NULL;
1796 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
1797 do_parse_flows__(fms, n_fms);
1801 /* "parse-nx-match": reads a series of nx_match specifications as strings from
1802 * stdin, does some internal fussing with them, and then prints them back as
1803 * strings on stdout. */
1805 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1810 while (!ds_get_line(&in, stdin)) {
1811 struct ofpbuf nx_match;
1812 struct cls_rule rule;
1813 ovs_be64 cookie, cookie_mask;
1818 /* Delete comments, skip blank lines. */
1824 if (strchr(s, '#')) {
1825 *strchr(s, '#') = '\0';
1827 if (s[strspn(s, " ")] == '\0') {
1832 /* Convert string to nx_match. */
1833 ofpbuf_init(&nx_match, 0);
1834 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1836 /* Convert nx_match to cls_rule. */
1838 error = nx_pull_match(&nx_match, match_len, 0, &rule,
1839 &cookie, &cookie_mask);
1841 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
1842 &cookie, &cookie_mask);
1848 /* Convert cls_rule back to nx_match. */
1849 ofpbuf_uninit(&nx_match);
1850 ofpbuf_init(&nx_match, 0);
1851 match_len = nx_put_match(&nx_match, &rule, cookie, cookie_mask);
1853 /* Convert nx_match to string. */
1854 out = nx_match_to_string(nx_match.data, match_len);
1858 printf("nx_pull_match() returned error %s\n",
1859 ofperr_get_name(error));
1862 ofpbuf_uninit(&nx_match);
1867 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
1870 do_print_error(int argc OVS_UNUSED, char *argv[])
1875 error = ofperr_from_name(argv[1]);
1877 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
1880 for (version = 0; version <= UINT8_MAX; version++) {
1881 const struct ofperr_domain *domain;
1883 domain = ofperr_domain_from_version(version);
1888 printf("%s: %d,%d\n",
1889 ofperr_domain_get_name(domain),
1890 ofperr_get_type(error, domain),
1891 ofperr_get_code(error, domain));
1895 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1896 * binary data, interpreting them as an OpenFlow message, and prints the
1897 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1899 do_ofp_print(int argc, char *argv[])
1901 struct ofpbuf packet;
1903 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1904 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1905 ovs_fatal(0, "trailing garbage following hex bytes");
1907 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1908 ofpbuf_uninit(&packet);
1911 static const struct command all_commands[] = {
1912 { "show", 1, 1, do_show },
1913 { "monitor", 1, 3, do_monitor },
1914 { "snoop", 1, 1, do_snoop },
1915 { "dump-desc", 1, 1, do_dump_desc },
1916 { "dump-tables", 1, 1, do_dump_tables },
1917 { "dump-flows", 1, 2, do_dump_flows },
1918 { "dump-aggregate", 1, 2, do_dump_aggregate },
1919 { "queue-stats", 1, 3, do_queue_stats },
1920 { "add-flow", 2, 2, do_add_flow },
1921 { "add-flows", 2, 2, do_add_flows },
1922 { "mod-flows", 2, 2, do_mod_flows },
1923 { "del-flows", 1, 2, do_del_flows },
1924 { "replace-flows", 2, 2, do_replace_flows },
1925 { "diff-flows", 2, 2, do_diff_flows },
1926 { "packet-out", 4, INT_MAX, do_packet_out },
1927 { "dump-ports", 1, 2, do_dump_ports },
1928 { "dump-ports-desc", 1, 1, do_dump_ports_desc },
1929 { "mod-port", 3, 3, do_mod_port },
1930 { "get-frags", 1, 1, do_get_frags },
1931 { "set-frags", 2, 2, do_set_frags },
1932 { "probe", 1, 1, do_probe },
1933 { "ping", 1, 2, do_ping },
1934 { "benchmark", 3, 3, do_benchmark },
1935 { "help", 0, INT_MAX, do_help },
1937 /* Undocumented commands for testing. */
1938 { "parse-flow", 1, 1, do_parse_flow },
1939 { "parse-flows", 1, 1, do_parse_flows },
1940 { "parse-nx-match", 0, 0, do_parse_nx_match },
1941 { "print-error", 1, 1, do_print_error },
1942 { "ofp-print", 1, 2, do_ofp_print },
1944 { NULL, 0, 0, NULL },