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;
383 ovs_be16 stats_type = ((struct ofp_stats_msg *) request->data)->type;
387 open_vconn(vconn_name, &vconn);
388 send_openflow_buffer(vconn, request);
391 struct ofpbuf *reply;
393 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
394 recv_xid = ((struct ofp_header *) reply->data)->xid;
395 if (send_xid == recv_xid) {
396 const struct ofp_stats_msg *osm = reply->data;
397 const struct ofp_header *oh = reply->data;
399 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
401 if (oh->type == OFPT_ERROR) {
403 } else if (oh->type == OFPT10_STATS_REPLY
404 && osm->type == stats_type) {
405 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
407 ovs_fatal(0, "received bad reply: %s",
408 ofp_to_string(reply->data, reply->size,
412 VLOG_DBG("received reply with xid %08"PRIx32" "
413 "!= expected %08"PRIx32, recv_xid, send_xid);
415 ofpbuf_delete(reply);
421 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
423 struct ofpbuf *request;
424 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
425 dump_stats_transaction(vconn_name, request);
428 /* Sends 'request', which should be a request that only has a reply if an error
429 * occurs, and waits for it to succeed or fail. If an error does occur, prints
430 * it and exits with an error.
432 * Destroys all of the 'requests'. */
434 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
436 struct ofpbuf *request, *reply;
438 LIST_FOR_EACH (request, list_node, requests) {
439 update_openflow_length(request);
442 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
443 "talking to %s", vconn_get_name(vconn));
445 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
448 ofpbuf_delete(reply);
451 /* Sends 'request', which should be a request that only has a reply if an error
452 * occurs, and waits for it to succeed or fail. If an error does occur, prints
453 * it and exits with an error.
455 * Destroys 'request'. */
457 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
459 struct list requests;
461 list_init(&requests);
462 list_push_back(&requests, &request->list_node);
463 transact_multiple_noreply(vconn, &requests);
467 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
469 struct ofp_switch_config *config;
470 struct ofp_header *header;
471 struct ofpbuf *request;
472 struct ofpbuf *reply;
474 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
476 run(vconn_transact(vconn, request, &reply),
477 "talking to %s", vconn_get_name(vconn));
479 header = reply->data;
480 if (header->type != OFPT_GET_CONFIG_REPLY ||
481 header->length != htons(sizeof *config)) {
482 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
485 config = reply->data;
488 ofpbuf_delete(reply);
492 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
494 struct ofp_switch_config *config;
495 struct ofp_header save_header;
496 struct ofpbuf *request;
498 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
499 save_header = config->header;
501 config->header = save_header;
503 transact_noreply(vconn, request);
507 do_show(int argc OVS_UNUSED, char *argv[])
509 const char *vconn_name = argv[1];
511 struct ofpbuf *request;
512 struct ofpbuf *reply;
515 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST,
517 open_vconn(vconn_name, &vconn);
518 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
520 trunc = ofputil_switch_features_ports_trunc(reply);
521 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
523 ofpbuf_delete(reply);
527 /* The Features Reply may not contain all the ports, so send a
528 * Port Description stats request, which doesn't have size
530 dump_trivial_stats_transaction(vconn_name, OFPST_PORT_DESC);
532 dump_trivial_transaction(vconn_name, OFPT_GET_CONFIG_REQUEST);
536 do_dump_desc(int argc OVS_UNUSED, char *argv[])
538 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
542 do_dump_tables(int argc OVS_UNUSED, char *argv[])
544 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
548 fetch_port_by_features(const char *vconn_name,
549 const char *port_name, unsigned int port_no,
550 struct ofputil_phy_port *pp, bool *trunc)
552 struct ofputil_switch_features features;
553 const struct ofp_switch_features *osf;
554 struct ofpbuf *request, *reply;
560 /* Fetch the switch's ofp_switch_features. */
561 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
562 open_vconn(vconn_name, &vconn);
563 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
567 if (reply->size < sizeof *osf) {
568 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
569 vconn_name, reply->size);
573 if (ofputil_switch_features_ports_trunc(reply)) {
578 error = ofputil_decode_switch_features(osf, &features, &b);
580 ovs_fatal(0, "%s: failed to decode features reply (%s)",
581 vconn_name, ofperr_to_string(error));
584 while (!ofputil_pull_phy_port(osf->header.version, &b, pp)) {
585 if (port_no != UINT_MAX
586 ? port_no == pp->port_no
587 : !strcmp(pp->name, port_name)) {
594 ofpbuf_delete(reply);
599 fetch_port_by_stats(const char *vconn_name,
600 const char *port_name, unsigned int port_no,
601 struct ofputil_phy_port *pp)
603 struct ofpbuf *request;
610 alloc_stats_request(sizeof(struct ofp_stats_msg), OFPST_PORT_DESC,
612 send_xid = ((struct ofp_header *) request->data)->xid;
614 open_vconn(vconn_name, &vconn);
615 send_openflow_buffer(vconn, request);
618 struct ofpbuf *reply;
620 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
621 recv_xid = ((struct ofp_header *) reply->data)->xid;
622 if (send_xid == recv_xid) {
623 const struct ofputil_msg_type *type;
624 struct ofp_stats_msg *osm;
626 ofputil_decode_msg_type(reply->data, &type);
627 if (ofputil_msg_type_code(type) != OFPUTIL_OFPST_PORT_DESC_REPLY) {
628 ovs_fatal(0, "received bad reply: %s",
629 ofp_to_string(reply->data, reply->size,
633 osm = ofpbuf_at_assert(reply, 0, sizeof *osm);
634 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
637 /* We've already found the port, but we need to drain
638 * the queue of any other replies for this request. */
642 ofpbuf_use_const(&b, &osm->header, ntohs(osm->header.length));
643 ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
645 while (!ofputil_pull_phy_port(osm->header.version, &b, pp)) {
646 if (port_no != UINT_MAX ? port_no == pp->port_no
647 : !strcmp(pp->name, port_name)) {
653 VLOG_DBG("received reply with xid %08"PRIx32" "
654 "!= expected %08"PRIx32, recv_xid, send_xid);
656 ofpbuf_delete(reply);
664 /* Opens a connection to 'vconn_name', fetches the port structure for
665 * 'port_name' (which may be a port name or number), and copies it into
668 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
669 struct ofputil_phy_port *pp)
671 unsigned int port_no;
675 /* Try to interpret the argument as a port number. */
676 if (!str_to_uint(port_name, 10, &port_no)) {
680 /* Try to find the port based on the Features Reply. If it looks
681 * like the results may be truncated, then use the Port Description
682 * stats message introduced in OVS 1.7. */
683 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
686 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
690 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
694 /* Returns the port number corresponding to 'port_name' (which may be a port
695 * name or number) within the switch 'vconn_name'. */
697 str_to_port_no(const char *vconn_name, const char *port_name)
699 unsigned int port_no;
701 if (str_to_uint(port_name, 10, &port_no)) {
704 struct ofputil_phy_port pp;
706 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
712 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
713 enum ofputil_protocol *cur)
716 struct ofpbuf *request, *reply;
717 enum ofputil_protocol next;
719 request = ofputil_encode_set_protocol(*cur, want, &next);
724 run(vconn_transact_noreply(vconn, request, &reply),
725 "talking to %s", vconn_get_name(vconn));
727 char *s = ofp_to_string(reply->data, reply->size, 2);
728 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
729 vconn_get_name(vconn), s);
731 ofpbuf_delete(reply);
739 static enum ofputil_protocol
740 set_protocol_for_flow_dump(struct vconn *vconn,
741 enum ofputil_protocol cur_protocol,
742 enum ofputil_protocol usable_protocols)
747 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
748 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
749 if (f & usable_protocols & allowed_protocols
750 && try_set_protocol(vconn, f, &cur_protocol)) {
755 usable_s = ofputil_protocols_to_string(usable_protocols);
756 if (usable_protocols & allowed_protocols) {
757 ovs_fatal(0, "switch does not support any of the usable flow "
758 "formats (%s)", usable_s);
760 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
761 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
762 "allowed flow formats (%s)", usable_s, allowed_s);
767 do_dump_flows__(int argc, char *argv[], bool aggregate)
769 enum ofputil_protocol usable_protocols, protocol;
770 struct ofputil_flow_stats_request fsr;
771 struct ofpbuf *request;
774 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
775 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
777 protocol = open_vconn(argv[1], &vconn);
778 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
779 request = ofputil_encode_flow_stats_request(&fsr, protocol);
780 dump_stats_transaction(argv[1], request);
785 do_dump_flows(int argc, char *argv[])
787 return do_dump_flows__(argc, argv, false);
791 do_dump_aggregate(int argc, char *argv[])
793 return do_dump_flows__(argc, argv, true);
797 do_queue_stats(int argc, char *argv[])
799 struct ofp_queue_stats_request *req;
800 struct ofpbuf *request;
802 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
804 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
805 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
807 req->port_no = htons(OFPP_ALL);
809 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
810 req->queue_id = htonl(atoi(argv[3]));
812 req->queue_id = htonl(OFPQ_ALL);
815 memset(req->pad, 0, sizeof req->pad);
817 dump_stats_transaction(argv[1], request);
820 static enum ofputil_protocol
821 open_vconn_for_flow_mod(const char *remote,
822 const struct ofputil_flow_mod *fms, size_t n_fms,
823 struct vconn **vconnp)
825 enum ofputil_protocol usable_protocols;
826 enum ofputil_protocol cur_protocol;
830 /* Figure out what flow formats will work. */
831 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
832 if (!(usable_protocols & allowed_protocols)) {
833 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
834 usable_s = ofputil_protocols_to_string(usable_protocols);
835 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
836 "allowed flow formats (%s)", usable_s, allowed_s);
839 /* If the initial flow format is allowed and usable, keep it. */
840 cur_protocol = open_vconn(remote, vconnp);
841 if (usable_protocols & allowed_protocols & cur_protocol) {
845 /* Otherwise try each flow format in turn. */
846 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
847 enum ofputil_protocol f = 1 << i;
849 if (f != cur_protocol
850 && f & usable_protocols & allowed_protocols
851 && try_set_protocol(*vconnp, f, &cur_protocol)) {
856 usable_s = ofputil_protocols_to_string(usable_protocols);
857 ovs_fatal(0, "switch does not support any of the usable flow "
858 "formats (%s)", usable_s);
862 do_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, size_t n_fms)
864 enum ofputil_protocol protocol;
868 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
870 for (i = 0; i < n_fms; i++) {
871 struct ofputil_flow_mod *fm = &fms[i];
873 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
880 do_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
882 struct ofputil_flow_mod *fms = NULL;
885 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
886 do_flow_mod__(argv[1], fms, n_fms);
891 do_flow_mod(int argc, char *argv[], uint16_t command)
893 if (argc > 2 && !strcmp(argv[2], "-")) {
894 do_flow_mod_file(argc, argv, command);
896 struct ofputil_flow_mod fm;
897 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
898 do_flow_mod__(argv[1], &fm, 1);
903 do_add_flow(int argc, char *argv[])
905 do_flow_mod(argc, argv, OFPFC_ADD);
909 do_add_flows(int argc, char *argv[])
911 do_flow_mod_file(argc, argv, OFPFC_ADD);
915 do_mod_flows(int argc, char *argv[])
917 do_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
921 do_del_flows(int argc, char *argv[])
923 do_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
927 set_packet_in_format(struct vconn *vconn,
928 enum nx_packet_in_format packet_in_format)
930 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
931 transact_noreply(vconn, spif);
932 VLOG_DBG("%s: using user-specified packet in format %s",
933 vconn_get_name(vconn),
934 ofputil_packet_in_format_to_string(packet_in_format));
938 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
940 struct ofp_switch_config config;
941 enum ofp_config_flags flags;
943 fetch_switch_config(vconn, &config);
944 flags = ntohs(config.flags);
945 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
946 /* Set the invalid ttl config. */
947 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
949 config.flags = htons(flags);
950 set_switch_config(vconn, &config);
952 /* Then retrieve the configuration to see if it really took. OpenFlow
953 * doesn't define error reporting for bad modes, so this is all we can
955 fetch_switch_config(vconn, &config);
956 flags = ntohs(config.flags);
957 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
958 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
959 "switch probably doesn't support mode)");
966 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
967 * caller must free '*msgp'. On success, returns NULL. On failure, returns
968 * an error message and stores NULL in '*msgp'. */
970 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
972 struct ofp_header *oh;
975 msg = ofpbuf_new(strlen(hex) / 2);
978 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
980 return "Trailing garbage in hex data";
983 if (msg->size < sizeof(struct ofp_header)) {
985 return "Message too short for OpenFlow";
989 if (msg->size != ntohs(oh->length)) {
991 return "Message size does not match length in OpenFlow header";
999 ofctl_send(struct unixctl_conn *conn, int argc,
1000 const char *argv[], void *vconn_)
1002 struct vconn *vconn = vconn_;
1009 for (i = 1; i < argc; i++) {
1010 const char *error_msg;
1014 error_msg = openflow_from_hex(argv[i], &msg);
1016 ds_put_format(&reply, "%s\n", error_msg);
1021 fprintf(stderr, "send: ");
1022 ofp_print(stderr, msg->data, msg->size, verbosity);
1024 error = vconn_send_block(vconn, msg);
1027 ds_put_format(&reply, "%s\n", strerror(error));
1030 ds_put_cstr(&reply, "sent\n");
1035 unixctl_command_reply(conn, ds_cstr(&reply));
1037 unixctl_command_reply_error(conn, ds_cstr(&reply));
1042 struct barrier_aux {
1043 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1044 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1048 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1049 const char *argv[] OVS_UNUSED, void *aux_)
1051 struct barrier_aux *aux = aux_;
1056 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1060 msg = ofputil_encode_barrier_request();
1061 error = vconn_send_block(aux->vconn, msg);
1064 unixctl_command_reply_error(conn, strerror(error));
1071 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1072 const char *argv[], void *aux OVS_UNUSED)
1076 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1078 unixctl_command_reply_error(conn, strerror(errno));
1083 dup2(fd, STDERR_FILENO);
1085 unixctl_command_reply(conn, NULL);
1089 monitor_vconn(struct vconn *vconn)
1091 struct barrier_aux barrier_aux = { vconn, NULL };
1092 struct unixctl_server *server;
1093 bool exiting = false;
1096 daemon_save_fd(STDERR_FILENO);
1098 error = unixctl_server_create(NULL, &server);
1100 ovs_fatal(error, "failed to create unixctl server");
1102 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1103 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1105 unixctl_command_register("ofctl/barrier", "", 0, 0,
1106 ofctl_barrier, &barrier_aux);
1107 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1108 ofctl_set_output_file, NULL);
1109 daemonize_complete();
1115 unixctl_server_run(server);
1120 retval = vconn_recv(vconn, &b);
1121 if (retval == EAGAIN) {
1124 run(retval, "vconn_recv");
1127 time_t now = time_wall();
1130 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1134 msg_type = ((const struct ofp_header *) b->data)->type;
1135 ofp_print(stderr, b->data, b->size, verbosity + 2);
1138 if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) {
1139 unixctl_command_reply(barrier_aux.conn, NULL);
1140 barrier_aux.conn = NULL;
1149 vconn_run_wait(vconn);
1150 vconn_recv_wait(vconn);
1151 unixctl_server_wait(server);
1155 unixctl_server_destroy(server);
1159 do_monitor(int argc, char *argv[])
1161 struct vconn *vconn;
1163 open_vconn(argv[1], &vconn);
1165 struct ofp_switch_config config;
1167 fetch_switch_config(vconn, &config);
1168 config.miss_send_len = htons(atoi(argv[2]));
1169 set_switch_config(vconn, &config);
1172 if (!strcmp(argv[3], "invalid_ttl")) {
1173 monitor_set_invalid_ttl_to_controller(vconn);
1176 if (preferred_packet_in_format >= 0) {
1177 set_packet_in_format(vconn, preferred_packet_in_format);
1179 struct ofpbuf *spif, *reply;
1181 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1182 run(vconn_transact_noreply(vconn, spif, &reply),
1183 "talking to %s", vconn_get_name(vconn));
1185 char *s = ofp_to_string(reply->data, reply->size, 2);
1186 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1187 " replied: %s. Falling back to the switch default.",
1188 vconn_get_name(vconn), s);
1190 ofpbuf_delete(reply);
1194 monitor_vconn(vconn);
1198 do_snoop(int argc OVS_UNUSED, char *argv[])
1200 struct vconn *vconn;
1202 open_vconn__(argv[1], "snoop", &vconn);
1203 monitor_vconn(vconn);
1207 do_dump_ports(int argc, char *argv[])
1209 struct ofp_port_stats_request *req;
1210 struct ofpbuf *request;
1213 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1214 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1215 req->port_no = htons(port);
1216 dump_stats_transaction(argv[1], request);
1220 do_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1222 dump_trivial_stats_transaction(argv[1], OFPST_PORT_DESC);
1226 do_probe(int argc OVS_UNUSED, char *argv[])
1228 struct ofpbuf *request;
1229 struct vconn *vconn;
1230 struct ofpbuf *reply;
1232 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1233 open_vconn(argv[1], &vconn);
1234 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1235 if (reply->size != sizeof(struct ofp_header)) {
1236 ovs_fatal(0, "reply does not match request");
1238 ofpbuf_delete(reply);
1243 do_packet_out(int argc, char *argv[])
1245 struct ofputil_packet_out po;
1246 struct ofpbuf actions;
1247 struct vconn *vconn;
1250 ofpbuf_init(&actions, sizeof(union ofp_action));
1251 parse_ofp_actions(argv[3], &actions);
1253 po.buffer_id = UINT32_MAX;
1254 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1255 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1256 : str_to_port_no(argv[1], argv[2]));
1257 po.actions = actions.data;
1258 po.n_actions = actions.size / sizeof(union ofp_action);
1260 open_vconn(argv[1], &vconn);
1261 for (i = 4; i < argc; i++) {
1262 struct ofpbuf *packet, *opo;
1263 const char *error_msg;
1265 error_msg = eth_from_hex(argv[i], &packet);
1267 ovs_fatal(0, "%s", error_msg);
1270 po.packet = packet->data;
1271 po.packet_len = packet->size;
1272 opo = ofputil_encode_packet_out(&po);
1273 transact_noreply(vconn, opo);
1274 ofpbuf_delete(packet);
1277 ofpbuf_uninit(&actions);
1281 do_mod_port(int argc OVS_UNUSED, char *argv[])
1283 struct ofp_config_flag {
1284 const char *name; /* The flag's name. */
1285 enum ofputil_port_config bit; /* Bit to turn on or off. */
1286 bool on; /* Value to set the bit to. */
1288 static const struct ofp_config_flag flags[] = {
1289 { "up", OFPUTIL_PC_PORT_DOWN, false },
1290 { "down", OFPUTIL_PC_PORT_DOWN, true },
1291 { "stp", OFPUTIL_PC_NO_STP, false },
1292 { "receive", OFPUTIL_PC_NO_RECV, false },
1293 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1294 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1295 { "forward", OFPUTIL_PC_NO_FWD, false },
1296 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1299 const struct ofp_config_flag *flag;
1300 enum ofputil_protocol protocol;
1301 struct ofputil_port_mod pm;
1302 struct ofputil_phy_port pp;
1303 struct vconn *vconn;
1304 const char *command;
1307 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1309 pm.port_no = pp.port_no;
1310 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1315 if (!strncasecmp(argv[3], "no-", 3)) {
1316 command = argv[3] + 3;
1318 } else if (!strncasecmp(argv[3], "no", 2)) {
1319 command = argv[3] + 2;
1325 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1326 if (!strcasecmp(command, flag->name)) {
1327 pm.mask = flag->bit;
1328 pm.config = flag->on ^ not ? flag->bit : 0;
1332 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1335 protocol = open_vconn(argv[1], &vconn);
1336 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1341 do_get_frags(int argc OVS_UNUSED, char *argv[])
1343 struct ofp_switch_config config;
1344 struct vconn *vconn;
1346 open_vconn(argv[1], &vconn);
1347 fetch_switch_config(vconn, &config);
1348 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1353 do_set_frags(int argc OVS_UNUSED, char *argv[])
1355 struct ofp_switch_config config;
1356 enum ofp_config_flags mode;
1357 struct vconn *vconn;
1360 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1361 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1364 open_vconn(argv[1], &vconn);
1365 fetch_switch_config(vconn, &config);
1366 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1367 if (flags != config.flags) {
1368 /* Set the configuration. */
1369 config.flags = flags;
1370 set_switch_config(vconn, &config);
1372 /* Then retrieve the configuration to see if it really took. OpenFlow
1373 * doesn't define error reporting for bad modes, so this is all we can
1375 fetch_switch_config(vconn, &config);
1376 if (flags != config.flags) {
1377 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1378 "switch probably doesn't support mode \"%s\")",
1379 argv[1], ofputil_frag_handling_to_string(mode));
1386 do_ping(int argc, char *argv[])
1388 size_t max_payload = 65535 - sizeof(struct ofp_header);
1389 unsigned int payload;
1390 struct vconn *vconn;
1393 payload = argc > 2 ? atoi(argv[2]) : 64;
1394 if (payload > max_payload) {
1395 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1398 open_vconn(argv[1], &vconn);
1399 for (i = 0; i < 10; i++) {
1400 struct timeval start, end;
1401 struct ofpbuf *request, *reply;
1402 struct ofp_header *rq_hdr, *rpy_hdr;
1404 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1405 OFPT_ECHO_REQUEST, &request);
1406 random_bytes(rq_hdr + 1, payload);
1408 xgettimeofday(&start);
1409 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1410 xgettimeofday(&end);
1412 rpy_hdr = reply->data;
1413 if (reply->size != request->size
1414 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1415 || rpy_hdr->xid != rq_hdr->xid
1416 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1417 printf("Reply does not match request. Request:\n");
1418 ofp_print(stdout, request, request->size, verbosity + 2);
1420 ofp_print(stdout, reply, reply->size, verbosity + 2);
1422 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1423 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
1424 (1000*(double)(end.tv_sec - start.tv_sec))
1425 + (.001*(end.tv_usec - start.tv_usec)));
1426 ofpbuf_delete(request);
1427 ofpbuf_delete(reply);
1433 do_benchmark(int argc OVS_UNUSED, char *argv[])
1435 size_t max_payload = 65535 - sizeof(struct ofp_header);
1436 struct timeval start, end;
1437 unsigned int payload_size, message_size;
1438 struct vconn *vconn;
1443 payload_size = atoi(argv[2]);
1444 if (payload_size > max_payload) {
1445 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1447 message_size = sizeof(struct ofp_header) + payload_size;
1449 count = atoi(argv[3]);
1451 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1452 count, message_size, count * message_size);
1454 open_vconn(argv[1], &vconn);
1455 xgettimeofday(&start);
1456 for (i = 0; i < count; i++) {
1457 struct ofpbuf *request, *reply;
1458 struct ofp_header *rq_hdr;
1460 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1461 memset(rq_hdr + 1, 0, payload_size);
1462 run(vconn_transact(vconn, request, &reply), "transact");
1463 ofpbuf_delete(reply);
1465 xgettimeofday(&end);
1468 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1469 + (.001*(end.tv_usec - start.tv_usec)));
1470 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1471 duration, count / (duration / 1000.0),
1472 count * message_size / (duration / 1000.0));
1476 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1481 /* replace-flows and diff-flows commands. */
1483 /* A flow table entry, possibly with two different versions. */
1485 struct cls_rule rule; /* Within a "struct classifier". */
1486 struct fte_version *versions[2];
1489 /* One version of a Flow Table Entry. */
1490 struct fte_version {
1492 uint16_t idle_timeout;
1493 uint16_t hard_timeout;
1495 union ofp_action *actions;
1499 /* Frees 'version' and the data that it owns. */
1501 fte_version_free(struct fte_version *version)
1504 free(version->actions);
1509 /* Returns true if 'a' and 'b' are the same, false if they differ.
1511 * Ignores differences in 'flags' because there's no way to retrieve flags from
1512 * an OpenFlow switch. We have to assume that they are the same. */
1514 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1516 return (a->cookie == b->cookie
1517 && a->idle_timeout == b->idle_timeout
1518 && a->hard_timeout == b->hard_timeout
1519 && a->n_actions == b->n_actions
1520 && !memcmp(a->actions, b->actions,
1521 a->n_actions * sizeof *a->actions));
1524 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1525 * associated with the version. */
1527 fte_version_print(const struct fte_version *version)
1531 if (version->cookie != htonll(0)) {
1532 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1534 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1535 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1537 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1538 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1542 ofp_print_actions(&s, version->actions, version->n_actions);
1543 printf(" %s\n", ds_cstr(&s));
1548 fte_from_cls_rule(const struct cls_rule *cls_rule)
1550 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1553 /* Frees 'fte' and its versions. */
1555 fte_free(struct fte *fte)
1558 fte_version_free(fte->versions[0]);
1559 fte_version_free(fte->versions[1]);
1564 /* Frees all of the FTEs within 'cls'. */
1566 fte_free_all(struct classifier *cls)
1568 struct cls_cursor cursor;
1569 struct fte *fte, *next;
1571 cls_cursor_init(&cursor, cls, NULL);
1572 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1573 classifier_remove(cls, &fte->rule);
1576 classifier_destroy(cls);
1579 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1580 * necessary. Sets 'version' as the version of that rule with the given
1581 * 'index', replacing any existing version, if any.
1583 * Takes ownership of 'version'. */
1585 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1586 struct fte_version *version, int index)
1588 struct fte *old, *fte;
1590 fte = xzalloc(sizeof *fte);
1592 fte->versions[index] = version;
1594 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1596 fte_version_free(old->versions[index]);
1597 fte->versions[!index] = old->versions[!index];
1602 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1603 * with the specified 'index'. Returns the flow formats able to represent the
1604 * flows that were read. */
1605 static enum ofputil_protocol
1606 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1608 enum ofputil_protocol usable_protocols;
1612 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1614 ovs_fatal(errno, "%s: open", filename);
1618 usable_protocols = OFPUTIL_P_ANY;
1619 while (!ds_get_preprocessed_line(&s, file)) {
1620 struct fte_version *version;
1621 struct ofputil_flow_mod fm;
1623 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1625 version = xmalloc(sizeof *version);
1626 version->cookie = fm.new_cookie;
1627 version->idle_timeout = fm.idle_timeout;
1628 version->hard_timeout = fm.hard_timeout;
1629 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1630 version->actions = fm.actions;
1631 version->n_actions = fm.n_actions;
1633 usable_protocols &= ofputil_usable_protocols(&fm.cr);
1635 fte_insert(cls, &fm.cr, version, index);
1639 if (file != stdin) {
1643 return usable_protocols;
1646 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1647 * format 'protocol', and adds them as flow table entries in 'cls' for the
1648 * version with the specified 'index'. */
1650 read_flows_from_switch(struct vconn *vconn,
1651 enum ofputil_protocol protocol,
1652 struct classifier *cls, int index)
1654 struct ofputil_flow_stats_request fsr;
1655 struct ofpbuf *request;
1659 fsr.aggregate = false;
1660 cls_rule_init_catchall(&fsr.match, 0);
1661 fsr.out_port = OFPP_NONE;
1662 fsr.table_id = 0xff;
1663 fsr.cookie = fsr.cookie_mask = htonll(0);
1664 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1665 send_xid = ((struct ofp_header *) request->data)->xid;
1666 send_openflow_buffer(vconn, request);
1671 struct ofpbuf *reply;
1673 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1674 recv_xid = ((struct ofp_header *) reply->data)->xid;
1675 if (send_xid == recv_xid) {
1676 const struct ofputil_msg_type *type;
1677 const struct ofp_stats_msg *osm;
1678 enum ofputil_msg_code code;
1680 ofputil_decode_msg_type(reply->data, &type);
1681 code = ofputil_msg_type_code(type);
1682 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1683 code != OFPUTIL_NXST_FLOW_REPLY) {
1684 ovs_fatal(0, "received bad reply: %s",
1685 ofp_to_string(reply->data, reply->size,
1690 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
1695 struct fte_version *version;
1696 struct ofputil_flow_stats fs;
1699 retval = ofputil_decode_flow_stats_reply(&fs, reply, false);
1701 if (retval != EOF) {
1702 ovs_fatal(0, "parse error in reply");
1707 version = xmalloc(sizeof *version);
1708 version->cookie = fs.cookie;
1709 version->idle_timeout = fs.idle_timeout;
1710 version->hard_timeout = fs.hard_timeout;
1712 version->n_actions = fs.n_actions;
1713 version->actions = xmemdup(fs.actions,
1714 fs.n_actions * sizeof *fs.actions);
1716 fte_insert(cls, &fs.rule, version, index);
1719 VLOG_DBG("received reply with xid %08"PRIx32" "
1720 "!= expected %08"PRIx32, recv_xid, send_xid);
1722 ofpbuf_delete(reply);
1727 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1728 enum ofputil_protocol protocol, struct list *packets)
1730 const struct fte_version *version = fte->versions[index];
1731 struct ofputil_flow_mod fm;
1735 fm.cookie = htonll(0);
1736 fm.cookie_mask = htonll(0);
1737 fm.new_cookie = version->cookie;
1739 fm.command = command;
1740 fm.idle_timeout = version->idle_timeout;
1741 fm.hard_timeout = version->hard_timeout;
1742 fm.buffer_id = UINT32_MAX;
1743 fm.out_port = OFPP_NONE;
1744 fm.flags = version->flags;
1745 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1746 command == OFPFC_MODIFY_STRICT) {
1747 fm.actions = version->actions;
1748 fm.n_actions = version->n_actions;
1754 ofm = ofputil_encode_flow_mod(&fm, protocol);
1755 list_push_back(packets, &ofm->list_node);
1759 do_replace_flows(int argc OVS_UNUSED, char *argv[])
1761 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1762 enum ofputil_protocol usable_protocols, protocol;
1763 struct cls_cursor cursor;
1764 struct classifier cls;
1765 struct list requests;
1766 struct vconn *vconn;
1769 classifier_init(&cls);
1770 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
1772 protocol = open_vconn(argv[1], &vconn);
1773 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1775 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
1777 list_init(&requests);
1779 /* Delete flows that exist on the switch but not in the file. */
1780 cls_cursor_init(&cursor, &cls, NULL);
1781 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1782 struct fte_version *file_ver = fte->versions[FILE_IDX];
1783 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1785 if (sw_ver && !file_ver) {
1786 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1787 protocol, &requests);
1791 /* Add flows that exist in the file but not on the switch.
1792 * Update flows that exist in both places but differ. */
1793 cls_cursor_init(&cursor, &cls, NULL);
1794 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1795 struct fte_version *file_ver = fte->versions[FILE_IDX];
1796 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1799 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1800 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
1803 transact_multiple_noreply(vconn, &requests);
1810 read_flows_from_source(const char *source, struct classifier *cls, int index)
1814 if (source[0] == '/' || source[0] == '.'
1815 || (!strchr(source, ':') && !stat(source, &s))) {
1816 read_flows_from_file(source, cls, index);
1818 enum ofputil_protocol protocol;
1819 struct vconn *vconn;
1821 protocol = open_vconn(source, &vconn);
1822 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
1823 read_flows_from_switch(vconn, protocol, cls, index);
1829 do_diff_flows(int argc OVS_UNUSED, char *argv[])
1831 bool differences = false;
1832 struct cls_cursor cursor;
1833 struct classifier cls;
1836 classifier_init(&cls);
1837 read_flows_from_source(argv[1], &cls, 0);
1838 read_flows_from_source(argv[2], &cls, 1);
1840 cls_cursor_init(&cursor, &cls, NULL);
1841 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1842 struct fte_version *a = fte->versions[0];
1843 struct fte_version *b = fte->versions[1];
1845 if (!a || !b || !fte_version_equals(a, b)) {
1846 char *rule_s = cls_rule_to_string(&fte->rule);
1848 printf("-%s", rule_s);
1849 fte_version_print(a);
1852 printf("+%s", rule_s);
1853 fte_version_print(b);
1868 /* Undocumented commands for unit testing. */
1871 do_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
1873 enum ofputil_protocol usable_protocols;
1874 enum ofputil_protocol protocol = 0;
1878 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1879 usable_s = ofputil_protocols_to_string(usable_protocols);
1880 printf("usable protocols: %s\n", usable_s);
1883 if (!(usable_protocols & allowed_protocols)) {
1884 ovs_fatal(0, "no usable protocol");
1886 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1888 if (protocol & usable_protocols & allowed_protocols) {
1892 assert(IS_POW2(protocol));
1894 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
1896 for (i = 0; i < n_fms; i++) {
1897 struct ofputil_flow_mod *fm = &fms[i];
1900 msg = ofputil_encode_flow_mod(fm, protocol);
1901 ofp_print(stdout, msg->data, msg->size, verbosity);
1908 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1909 * it back to stdout. */
1911 do_parse_flow(int argc OVS_UNUSED, char *argv[])
1913 struct ofputil_flow_mod fm;
1915 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
1916 do_parse_flows__(&fm, 1);
1919 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1920 * add-flows) and prints each of the flows back to stdout. */
1922 do_parse_flows(int argc OVS_UNUSED, char *argv[])
1924 struct ofputil_flow_mod *fms = NULL;
1927 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
1928 do_parse_flows__(fms, n_fms);
1933 do_parse_nxm__(bool oxm)
1938 while (!ds_get_test_line(&in, stdin)) {
1939 struct ofpbuf nx_match;
1940 struct cls_rule rule;
1941 ovs_be64 cookie, cookie_mask;
1945 /* Convert string to nx_match. */
1946 ofpbuf_init(&nx_match, 0);
1947 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1949 /* Convert nx_match to cls_rule. */
1951 error = nx_pull_match(&nx_match, match_len, 0, &rule,
1952 &cookie, &cookie_mask);
1954 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
1955 &cookie, &cookie_mask);
1961 /* Convert cls_rule back to nx_match. */
1962 ofpbuf_uninit(&nx_match);
1963 ofpbuf_init(&nx_match, 0);
1964 match_len = nx_put_match(&nx_match, oxm, &rule,
1965 cookie, cookie_mask);
1967 /* Convert nx_match to string. */
1968 out = nx_match_to_string(nx_match.data, match_len);
1972 printf("nx_pull_match() returned error %s\n",
1973 ofperr_get_name(error));
1976 ofpbuf_uninit(&nx_match);
1981 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
1982 * stdin, does some internal fussing with them, and then prints them back as
1983 * strings on stdout. */
1985 do_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1987 return do_parse_nxm__(false);
1990 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
1991 * stdin, does some internal fussing with them, and then prints them back as
1992 * strings on stdout. */
1994 do_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1996 return do_parse_nxm__(true);
1999 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2000 * bytes from stdin, converts them to cls_rules, prints them as strings on
2001 * stdout, and then converts them back to hex bytes and prints any differences
2002 * from the input. */
2004 do_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2009 while (!ds_get_preprocessed_line(&in, stdin)) {
2010 struct ofpbuf match_in;
2011 struct ofp11_match match_out;
2012 struct cls_rule rule;
2016 /* Parse hex bytes. */
2017 ofpbuf_init(&match_in, 0);
2018 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2019 ovs_fatal(0, "Trailing garbage in hex data");
2021 if (match_in.size != sizeof(struct ofp11_match)) {
2022 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2023 match_in.size, sizeof(struct ofp11_match));
2026 /* Convert to cls_rule. */
2027 error = ofputil_cls_rule_from_ofp11_match(match_in.data,
2028 OFP_DEFAULT_PRIORITY, &rule);
2030 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2031 ofpbuf_uninit(&match_in);
2035 /* Print cls_rule. */
2036 cls_rule_print(&rule);
2038 /* Convert back to ofp11_match and print differences from input. */
2039 ofputil_cls_rule_to_ofp11_match(&rule, &match_out);
2041 for (i = 0; i < sizeof match_out; i++) {
2042 uint8_t in = ((const uint8_t *) match_in.data)[i];
2043 uint8_t out = ((const uint8_t *) &match_out)[i];
2046 printf("%2d: %02"PRIx8" -> %02"PRIx8"\n", i, in, out);
2051 ofpbuf_uninit(&match_in);
2056 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2059 do_print_error(int argc OVS_UNUSED, char *argv[])
2064 error = ofperr_from_name(argv[1]);
2066 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2069 for (version = 0; version <= UINT8_MAX; version++) {
2070 const struct ofperr_domain *domain;
2072 domain = ofperr_domain_from_version(version);
2077 printf("%s: %d,%d\n",
2078 ofperr_domain_get_name(domain),
2079 ofperr_get_type(error, domain),
2080 ofperr_get_code(error, domain));
2084 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2085 * binary data, interpreting them as an OpenFlow message, and prints the
2086 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2088 do_ofp_print(int argc, char *argv[])
2090 struct ofpbuf packet;
2092 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2093 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2094 ovs_fatal(0, "trailing garbage following hex bytes");
2096 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2097 ofpbuf_uninit(&packet);
2100 static const struct command all_commands[] = {
2101 { "show", 1, 1, do_show },
2102 { "monitor", 1, 3, do_monitor },
2103 { "snoop", 1, 1, do_snoop },
2104 { "dump-desc", 1, 1, do_dump_desc },
2105 { "dump-tables", 1, 1, do_dump_tables },
2106 { "dump-flows", 1, 2, do_dump_flows },
2107 { "dump-aggregate", 1, 2, do_dump_aggregate },
2108 { "queue-stats", 1, 3, do_queue_stats },
2109 { "add-flow", 2, 2, do_add_flow },
2110 { "add-flows", 2, 2, do_add_flows },
2111 { "mod-flows", 2, 2, do_mod_flows },
2112 { "del-flows", 1, 2, do_del_flows },
2113 { "replace-flows", 2, 2, do_replace_flows },
2114 { "diff-flows", 2, 2, do_diff_flows },
2115 { "packet-out", 4, INT_MAX, do_packet_out },
2116 { "dump-ports", 1, 2, do_dump_ports },
2117 { "dump-ports-desc", 1, 1, do_dump_ports_desc },
2118 { "mod-port", 3, 3, do_mod_port },
2119 { "get-frags", 1, 1, do_get_frags },
2120 { "set-frags", 2, 2, do_set_frags },
2121 { "probe", 1, 1, do_probe },
2122 { "ping", 1, 2, do_ping },
2123 { "benchmark", 3, 3, do_benchmark },
2124 { "help", 0, INT_MAX, do_help },
2126 /* Undocumented commands for testing. */
2127 { "parse-flow", 1, 1, do_parse_flow },
2128 { "parse-flows", 1, 1, do_parse_flows },
2129 { "parse-nx-match", 0, 0, do_parse_nxm },
2130 { "parse-nxm", 0, 0, do_parse_nxm },
2131 { "parse-oxm", 0, 0, do_parse_oxm },
2132 { "parse-ofp11-match", 0, 0, do_parse_ofp11_match },
2133 { "print-error", 1, 1, do_print_error },
2134 { "ofp-print", 1, 2, do_ofp_print },
2136 { NULL, 0, 0, NULL },