2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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>
30 #include "byte-order.h"
31 #include "classifier.h"
32 #include "command-line.h"
35 #include "dynamic-string.h"
39 #include "ofp-parse.h"
40 #include "ofp-print.h"
43 #include "ofproto/ofproto.h"
44 #include "openflow/nicira-ext.h"
45 #include "openflow/openflow.h"
47 #include "stream-ssl.h"
53 VLOG_DEFINE_THIS_MODULE(ofctl);
55 /* --strict: Use strict matching for flow mod commands? */
58 /* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
59 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
60 static int preferred_flow_format = -1;
62 /* -m, --more: Additional verbosity for ofp-print functions. */
65 static const struct command all_commands[];
67 static void usage(void) NO_RETURN;
68 static void parse_options(int argc, char *argv[]);
71 main(int argc, char *argv[])
73 set_program_name(argv[0]);
74 parse_options(argc, argv);
75 signal(SIGPIPE, SIG_IGN);
76 run_command(argc - optind, argv + optind, all_commands);
81 parse_options(int argc, char *argv[])
84 OPT_STRICT = UCHAR_MAX + 1,
87 static struct option long_options[] = {
88 {"timeout", required_argument, 0, 't'},
89 {"strict", no_argument, 0, OPT_STRICT},
90 {"flow-format", required_argument, 0, 'F'},
91 {"more", no_argument, 0, 'm'},
92 {"help", no_argument, 0, 'h'},
93 {"version", no_argument, 0, 'V'},
95 STREAM_SSL_LONG_OPTIONS
98 char *short_options = long_options_to_short_options(long_options);
101 unsigned long int timeout;
104 c = getopt_long(argc, argv, short_options, long_options, NULL);
111 timeout = strtoul(optarg, NULL, 10);
113 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
121 preferred_flow_format = ofputil_flow_format_from_string(optarg);
122 if (preferred_flow_format < 0) {
123 ovs_fatal(0, "unknown flow format `%s'", optarg);
135 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
143 STREAM_SSL_OPTION_HANDLERS
158 printf("%s: OpenFlow switch management utility\n"
159 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
160 "\nFor OpenFlow switches:\n"
161 " show SWITCH show OpenFlow information\n"
162 " dump-desc SWITCH print switch description\n"
163 " dump-tables SWITCH print table stats\n"
164 " mod-port SWITCH IFACE ACT modify port behavior\n"
165 " dump-ports SWITCH [PORT] print port statistics\n"
166 " dump-flows SWITCH print all flow entries\n"
167 " dump-flows SWITCH FLOW print matching FLOWs\n"
168 " dump-aggregate SWITCH print aggregate flow statistics\n"
169 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
170 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
171 " add-flow SWITCH FLOW add flow described by FLOW\n"
172 " add-flows SWITCH FILE add flows from FILE\n"
173 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
174 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
175 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
176 "\nFor OpenFlow switches and controllers:\n"
177 " probe VCONN probe whether VCONN is up\n"
178 " ping VCONN [N] latency of N-byte echos\n"
179 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
180 "where each SWITCH is an active OpenFlow connection method.\n",
181 program_name, program_name);
182 vconn_usage(true, false, false);
184 printf("\nOther options:\n"
185 " --strict use strict match for flow commands\n"
186 " -F, --flow-format=FORMAT force particular flow format\n"
187 " -m, --more be more verbose printing OpenFlow\n"
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n"
190 " -V, --version display version information\n");
194 static void run(int retval, const char *message, ...)
197 static void run(int retval, const char *message, ...)
202 va_start(args, message);
203 ovs_fatal_valist(retval, message, args);
207 /* Generic commands. */
210 open_vconn_socket(const char *name, struct vconn **vconnp)
212 char *vconn_name = xasprintf("unix:%s", name);
213 VLOG_DBG("connecting to %s", vconn_name);
214 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
215 "connecting to %s", vconn_name);
220 open_vconn__(const char *name, const char *default_suffix,
221 struct vconn **vconnp)
223 char *datapath_name, *datapath_type, *socket_name;
227 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
229 ofproto_parse_name(name, &datapath_name, &datapath_type);
230 socket_name = xasprintf("%s/%s.%s",
231 ovs_rundir(), datapath_name, default_suffix);
235 if (strstr(name, ":")) {
236 run(vconn_open_block(name, OFP_VERSION, vconnp),
237 "connecting to %s", name);
238 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
239 open_vconn_socket(name, vconnp);
240 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
241 open_vconn_socket(bridge_path, vconnp);
242 } else if (!stat(socket_name, &s)) {
243 if (!S_ISSOCK(s.st_mode)) {
244 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
247 open_vconn_socket(socket_name, vconnp);
249 ovs_fatal(0, "%s is not a valid connection method", name);
257 open_vconn(const char *name, struct vconn **vconnp)
259 return open_vconn__(name, "mgmt", vconnp);
263 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
265 struct ofp_stats_request *rq;
266 rq = make_openflow((offsetof(struct ofp_stats_request, body)
267 + body_len), OFPT_STATS_REQUEST, bufferp);
268 rq->type = htons(type);
269 rq->flags = htons(0);
274 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
276 update_openflow_length(buffer);
277 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
281 dump_transaction(const char *vconn_name, struct ofpbuf *request)
284 struct ofpbuf *reply;
286 update_openflow_length(request);
287 open_vconn(vconn_name, &vconn);
288 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
289 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
294 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
296 struct ofpbuf *request;
297 make_openflow(sizeof(struct ofp_header), request_type, &request);
298 dump_transaction(vconn_name, request);
302 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
304 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
308 open_vconn(vconn_name, &vconn);
309 send_openflow_buffer(vconn, request);
312 struct ofpbuf *reply;
314 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
315 recv_xid = ((struct ofp_header *) reply->data)->xid;
316 if (send_xid == recv_xid) {
317 struct ofp_stats_reply *osr;
319 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
321 osr = ofpbuf_at(reply, 0, sizeof *osr);
322 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
324 VLOG_DBG("received reply with xid %08"PRIx32" "
325 "!= expected %08"PRIx32, recv_xid, send_xid);
327 ofpbuf_delete(reply);
333 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
335 struct ofpbuf *request;
336 alloc_stats_request(0, stats_type, &request);
337 dump_stats_transaction(vconn_name, request);
340 /* Sends 'request', which should be a request that only has a reply if an error
341 * occurs, and waits for it to succeed or fail. If an error does occur, prints
342 * it and exits with an error. */
344 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
346 struct ofpbuf *request, *reply;
348 LIST_FOR_EACH (request, list_node, requests) {
349 update_openflow_length(request);
352 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
353 "talking to %s", vconn_get_name(vconn));
355 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
358 ofpbuf_delete(reply);
361 /* Sends 'request', which should be a request that only has a reply if an error
362 * occurs, and waits for it to succeed or fail. If an error does occur, prints
363 * it and exits with an error. */
365 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
367 struct list requests;
369 list_init(&requests);
370 list_push_back(&requests, &request->list_node);
371 transact_multiple_noreply(vconn, &requests);
375 do_show(int argc OVS_UNUSED, char *argv[])
377 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
378 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
382 do_dump_desc(int argc OVS_UNUSED, char *argv[])
384 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
388 do_dump_tables(int argc OVS_UNUSED, char *argv[])
390 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
393 /* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
394 * 'port_name' (which may be a port name or number), and copies it into
397 fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
398 struct ofp_phy_port *oppp)
400 struct ofpbuf *request, *reply;
401 struct ofp_switch_features *osf;
402 unsigned int port_no;
407 /* Try to interpret the argument as a port number. */
408 if (!str_to_uint(port_name, 10, &port_no)) {
412 /* Fetch the switch's ofp_switch_features. */
413 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
414 open_vconn(vconn_name, &vconn);
415 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
418 if (reply->size < sizeof *osf) {
419 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
420 vconn_name, reply->size);
422 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
424 for (port_idx = 0; port_idx < n_ports; port_idx++) {
425 const struct ofp_phy_port *opp = &osf->ports[port_idx];
427 if (port_no != UINT_MAX
428 ? htons(port_no) == opp->port_no
429 : !strncmp(opp->name, port_name, sizeof opp->name)) {
431 ofpbuf_delete(reply);
436 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
439 /* Returns the port number corresponding to 'port_name' (which may be a port
440 * name or number) within the switch 'vconn_name'. */
442 str_to_port_no(const char *vconn_name, const char *port_name)
444 unsigned int port_no;
446 if (str_to_uint(port_name, 10, &port_no)) {
449 struct ofp_phy_port opp;
451 fetch_ofp_phy_port(vconn_name, port_name, &opp);
452 return ntohs(opp.port_no);
457 try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
459 struct ofpbuf *sff, *reply;
461 sff = ofputil_make_set_flow_format(flow_format);
462 run(vconn_transact_noreply(vconn, sff, &reply),
463 "talking to %s", vconn_get_name(vconn));
465 char *s = ofp_to_string(reply->data, reply->size, 2);
466 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
467 vconn_get_name(vconn),
468 ofputil_flow_format_to_string(flow_format),
471 ofpbuf_delete(reply);
478 set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
480 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
481 transact_noreply(vconn, sff);
482 VLOG_DBG("%s: using user-specified flow format %s",
483 vconn_get_name(vconn),
484 ofputil_flow_format_to_string(flow_format));
487 static enum nx_flow_format
488 negotiate_highest_flow_format(struct vconn *vconn,
489 enum nx_flow_format min_format)
491 if (preferred_flow_format != -1) {
492 if (preferred_flow_format < min_format) {
493 ovs_fatal(0, "%s: cannot use requested flow format %s for "
494 "specified flow", vconn_get_name(vconn),
495 ofputil_flow_format_to_string(min_format));
498 set_flow_format(vconn, preferred_flow_format);
499 return preferred_flow_format;
501 enum nx_flow_format flow_format;
503 if (try_set_flow_format(vconn, NXFF_NXM)) {
504 flow_format = NXFF_NXM;
505 } else if (try_set_flow_format(vconn, NXFF_TUN_ID_FROM_COOKIE)) {
506 flow_format = NXFF_TUN_ID_FROM_COOKIE;
508 flow_format = NXFF_OPENFLOW10;
511 if (flow_format < min_format) {
512 ovs_fatal(0, "%s: cannot use switch's most advanced flow format "
513 "%s for specified flow", vconn_get_name(vconn),
514 ofputil_flow_format_to_string(min_format));
517 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
518 ofputil_flow_format_to_string(flow_format));
524 do_dump_flows__(int argc, char *argv[], bool aggregate)
526 enum nx_flow_format min_flow_format, flow_format;
527 struct flow_stats_request fsr;
528 struct ofpbuf *request;
531 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
533 open_vconn(argv[1], &vconn);
534 min_flow_format = ofputil_min_flow_format(&fsr.match, false, 0);
535 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
536 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
537 dump_stats_transaction(argv[1], request);
542 do_dump_flows(int argc, char *argv[])
544 return do_dump_flows__(argc, argv, false);
548 do_dump_aggregate(int argc, char *argv[])
550 return do_dump_flows__(argc, argv, true);
554 do_queue_stats(int argc, char *argv[])
556 struct ofp_queue_stats_request *req;
557 struct ofpbuf *request;
559 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
561 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
562 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
564 req->port_no = htons(OFPP_ALL);
566 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
567 req->queue_id = htonl(atoi(argv[3]));
569 req->queue_id = htonl(OFPQ_ALL);
572 memset(req->pad, 0, sizeof req->pad);
574 dump_stats_transaction(argv[1], request);
577 /* Sets up the flow format for a vconn that will be used to modify the flow
578 * table. Returns the flow format used, after possibly adding an OpenFlow
579 * request to 'requests'.
581 * If 'preferred_flow_format' is -1, returns NXFF_OPENFLOW10 without modifying
582 * 'requests', since NXFF_OPENFLOW10 is the default flow format for any
583 * OpenFlow connection.
585 * If 'preferred_flow_format' is a specific format, adds a request to set that
586 * format to 'requests' and returns the format. */
587 static enum nx_flow_format
588 set_initial_format_for_flow_mod(struct list *requests)
590 if (preferred_flow_format < 0) {
591 return NXFF_OPENFLOW10;
595 sff = ofputil_make_set_flow_format(preferred_flow_format);
596 list_push_back(requests, &sff->list_node);
597 return preferred_flow_format;
601 /* Checks that 'flow_format' is acceptable as a flow format after a flow_mod
602 * operation, given the global 'preferred_flow_format'. */
604 check_final_format_for_flow_mod(enum nx_flow_format flow_format)
606 if (preferred_flow_format >= 0 && flow_format > preferred_flow_format) {
607 ovs_fatal(0, "flow cannot be expressed in flow format %s "
608 "(flow format %s or better is required)",
609 ofputil_flow_format_to_string(preferred_flow_format),
610 ofputil_flow_format_to_string(flow_format));
615 do_flow_mod_file__(int argc OVS_UNUSED, char *argv[], uint16_t command)
617 enum nx_flow_format flow_format;
618 bool flow_mod_table_id;
619 struct list requests;
623 file = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r");
625 ovs_fatal(errno, "%s: open", argv[2]);
628 list_init(&requests);
629 flow_format = set_initial_format_for_flow_mod(&requests);
630 flow_mod_table_id = false;
632 open_vconn(argv[1], &vconn);
633 while (parse_ofp_flow_mod_file(&requests, &flow_format, &flow_mod_table_id,
635 check_final_format_for_flow_mod(flow_format);
636 transact_multiple_noreply(vconn, &requests);
646 do_flow_mod__(int argc, char *argv[], uint16_t command)
648 enum nx_flow_format flow_format;
649 bool flow_mod_table_id;
650 struct list requests;
653 if (argc > 2 && !strcmp(argv[2], "-")) {
654 do_flow_mod_file__(argc, argv, command);
658 list_init(&requests);
659 flow_format = set_initial_format_for_flow_mod(&requests);
660 flow_mod_table_id = false;
662 parse_ofp_flow_mod_str(&requests, &flow_format, &flow_mod_table_id,
663 argc > 2 ? argv[2] : "", command);
664 check_final_format_for_flow_mod(flow_format);
666 open_vconn(argv[1], &vconn);
667 transact_multiple_noreply(vconn, &requests);
672 do_add_flow(int argc, char *argv[])
674 do_flow_mod__(argc, argv, OFPFC_ADD);
678 do_add_flows(int argc, char *argv[])
680 do_flow_mod_file__(argc, argv, OFPFC_ADD);
684 do_mod_flows(int argc, char *argv[])
686 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
690 do_del_flows(int argc, char *argv[])
692 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
696 monitor_vconn(struct vconn *vconn)
700 run(vconn_recv_block(vconn, &b), "vconn_recv");
701 ofp_print(stderr, b->data, b->size, verbosity + 2);
707 do_monitor(int argc, char *argv[])
711 open_vconn(argv[1], &vconn);
713 int miss_send_len = atoi(argv[2]);
714 struct ofp_switch_config *osc;
717 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
718 osc->miss_send_len = htons(miss_send_len);
719 transact_noreply(vconn, buf);
721 monitor_vconn(vconn);
725 do_snoop(int argc OVS_UNUSED, char *argv[])
729 open_vconn__(argv[1], "snoop", &vconn);
730 monitor_vconn(vconn);
734 do_dump_ports(int argc, char *argv[])
736 struct ofp_port_stats_request *req;
737 struct ofpbuf *request;
740 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
741 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
742 req->port_no = htons(port);
743 dump_stats_transaction(argv[1], request);
747 do_probe(int argc OVS_UNUSED, char *argv[])
749 struct ofpbuf *request;
751 struct ofpbuf *reply;
753 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
754 open_vconn(argv[1], &vconn);
755 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
756 if (reply->size != sizeof(struct ofp_header)) {
757 ovs_fatal(0, "reply does not match request");
759 ofpbuf_delete(reply);
764 do_mod_port(int argc OVS_UNUSED, char *argv[])
766 struct ofp_port_mod *opm;
767 struct ofp_phy_port opp;
768 struct ofpbuf *request;
771 fetch_ofp_phy_port(argv[1], argv[2], &opp);
773 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
774 opm->port_no = opp.port_no;
775 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
776 opm->config = htonl(0);
777 opm->mask = htonl(0);
778 opm->advertise = htonl(0);
780 if (!strcasecmp(argv[3], "up")) {
781 opm->mask |= htonl(OFPPC_PORT_DOWN);
782 } else if (!strcasecmp(argv[3], "down")) {
783 opm->mask |= htonl(OFPPC_PORT_DOWN);
784 opm->config |= htonl(OFPPC_PORT_DOWN);
785 } else if (!strcasecmp(argv[3], "flood")) {
786 opm->mask |= htonl(OFPPC_NO_FLOOD);
787 } else if (!strcasecmp(argv[3], "noflood")) {
788 opm->mask |= htonl(OFPPC_NO_FLOOD);
789 opm->config |= htonl(OFPPC_NO_FLOOD);
791 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
794 open_vconn(argv[1], &vconn);
795 transact_noreply(vconn, request);
800 do_ping(int argc, char *argv[])
802 size_t max_payload = 65535 - sizeof(struct ofp_header);
803 unsigned int payload;
807 payload = argc > 2 ? atoi(argv[2]) : 64;
808 if (payload > max_payload) {
809 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
812 open_vconn(argv[1], &vconn);
813 for (i = 0; i < 10; i++) {
814 struct timeval start, end;
815 struct ofpbuf *request, *reply;
816 struct ofp_header *rq_hdr, *rpy_hdr;
818 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
819 OFPT_ECHO_REQUEST, &request);
820 random_bytes(rq_hdr + 1, payload);
822 xgettimeofday(&start);
823 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
826 rpy_hdr = reply->data;
827 if (reply->size != request->size
828 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
829 || rpy_hdr->xid != rq_hdr->xid
830 || rpy_hdr->type != OFPT_ECHO_REPLY) {
831 printf("Reply does not match request. Request:\n");
832 ofp_print(stdout, request, request->size, verbosity + 2);
834 ofp_print(stdout, reply, reply->size, verbosity + 2);
836 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
837 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
838 (1000*(double)(end.tv_sec - start.tv_sec))
839 + (.001*(end.tv_usec - start.tv_usec)));
840 ofpbuf_delete(request);
841 ofpbuf_delete(reply);
847 do_benchmark(int argc OVS_UNUSED, char *argv[])
849 size_t max_payload = 65535 - sizeof(struct ofp_header);
850 struct timeval start, end;
851 unsigned int payload_size, message_size;
857 payload_size = atoi(argv[2]);
858 if (payload_size > max_payload) {
859 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
861 message_size = sizeof(struct ofp_header) + payload_size;
863 count = atoi(argv[3]);
865 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
866 count, message_size, count * message_size);
868 open_vconn(argv[1], &vconn);
869 xgettimeofday(&start);
870 for (i = 0; i < count; i++) {
871 struct ofpbuf *request, *reply;
872 struct ofp_header *rq_hdr;
874 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
875 memset(rq_hdr + 1, 0, payload_size);
876 run(vconn_transact(vconn, request, &reply), "transact");
877 ofpbuf_delete(reply);
882 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
883 + (.001*(end.tv_usec - start.tv_usec)));
884 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
885 duration, count / (duration / 1000.0),
886 count * message_size / (duration / 1000.0));
890 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
895 /* replace-flows and diff-flows commands. */
897 /* A flow table entry, possibly with two different versions. */
899 struct cls_rule rule; /* Within a "struct classifier". */
900 struct fte_version *versions[2];
903 /* One version of a Flow Table Entry. */
906 uint16_t idle_timeout;
907 uint16_t hard_timeout;
909 union ofp_action *actions;
913 /* Frees 'version' and the data that it owns. */
915 fte_version_free(struct fte_version *version)
918 free(version->actions);
923 /* Returns true if 'a' and 'b' are the same, false if they differ.
925 * Ignores differences in 'flags' because there's no way to retrieve flags from
926 * an OpenFlow switch. We have to assume that they are the same. */
928 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
930 return (a->cookie == b->cookie
931 && a->idle_timeout == b->idle_timeout
932 && a->hard_timeout == b->hard_timeout
933 && a->n_actions == b->n_actions
934 && !memcmp(a->actions, b->actions,
935 a->n_actions * sizeof *a->actions));
938 /* Prints 'version' on stdout. Expects the caller to have printed the rule
939 * associated with the version. */
941 fte_version_print(const struct fte_version *version)
945 if (version->cookie != htonll(0)) {
946 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
948 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
949 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
951 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
952 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
956 ofp_print_actions(&s, (const struct ofp_action_header *) version->actions,
957 version->n_actions * sizeof *version->actions);
958 printf(" %s\n", ds_cstr(&s));
963 fte_from_cls_rule(const struct cls_rule *cls_rule)
965 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
968 /* Frees 'fte' and its versions. */
970 fte_free(struct fte *fte)
973 fte_version_free(fte->versions[0]);
974 fte_version_free(fte->versions[1]);
979 /* Frees all of the FTEs within 'cls'. */
981 fte_free_all(struct classifier *cls)
983 struct cls_cursor cursor;
984 struct fte *fte, *next;
986 cls_cursor_init(&cursor, cls, NULL);
987 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
988 classifier_remove(cls, &fte->rule);
993 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
994 * necessary. Sets 'version' as the version of that rule with the given
995 * 'index', replacing any existing version, if any.
997 * Takes ownership of 'version'. */
999 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1000 struct fte_version *version, int index)
1002 struct fte *old, *fte;
1004 fte = xzalloc(sizeof *fte);
1006 fte->versions[index] = version;
1008 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1010 fte_version_free(old->versions[index]);
1011 fte->versions[!index] = old->versions[!index];
1016 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1017 * with the specified 'index'. Returns the minimum flow format required to
1018 * represent the flows that were read. */
1019 static enum nx_flow_format
1020 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1022 enum nx_flow_format min_flow_format;
1026 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1028 ovs_fatal(errno, "%s: open", filename);
1032 min_flow_format = NXFF_OPENFLOW10;
1033 while (!ds_get_preprocessed_line(&s, file)) {
1034 struct fte_version *version;
1035 enum nx_flow_format min_ff;
1036 struct ofpbuf actions;
1039 ofpbuf_init(&actions, 64);
1040 parse_ofp_str(&fm, &actions, ds_cstr(&s));
1042 version = xmalloc(sizeof *version);
1043 version->cookie = fm.cookie;
1044 version->idle_timeout = fm.idle_timeout;
1045 version->hard_timeout = fm.hard_timeout;
1046 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1047 version->n_actions = actions.size / sizeof *version->actions;
1048 version->actions = ofpbuf_steal_data(&actions);
1050 min_ff = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
1051 min_flow_format = MAX(min_flow_format, min_ff);
1052 check_final_format_for_flow_mod(min_flow_format);
1054 fte_insert(cls, &fm.cr, version, index);
1058 if (file != stdin) {
1062 return min_flow_format;
1065 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1066 * format 'flow_format', and adds them as flow table entries in 'cls' for the
1067 * version with the specified 'index'. */
1069 read_flows_from_switch(struct vconn *vconn, enum nx_flow_format flow_format,
1070 struct classifier *cls, int index)
1072 struct flow_stats_request fsr;
1073 struct ofpbuf *request;
1077 fsr.aggregate = false;
1078 cls_rule_init_catchall(&fsr.match, 0);
1079 fsr.out_port = OFPP_NONE;
1080 fsr.table_id = 0xff;
1081 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
1082 send_xid = ((struct ofp_header *) request->data)->xid;
1083 send_openflow_buffer(vconn, request);
1088 struct ofpbuf *reply;
1090 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1091 recv_xid = ((struct ofp_header *) reply->data)->xid;
1092 if (send_xid == recv_xid) {
1093 const struct ofputil_msg_type *type;
1094 const struct ofp_stats_reply *osr;
1095 enum ofputil_msg_code code;
1097 ofputil_decode_msg_type(reply->data, &type);
1098 code = ofputil_msg_type_code(type);
1099 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1100 code != OFPUTIL_NXST_FLOW_REPLY) {
1101 ovs_fatal(0, "received bad reply: %s",
1102 ofp_to_string(reply->data, reply->size,
1107 if (!(osr->flags & htons(OFPSF_REPLY_MORE))) {
1112 struct fte_version *version;
1113 struct ofputil_flow_stats fs;
1116 retval = ofputil_decode_flow_stats_reply(&fs, reply,
1119 if (retval != EOF) {
1120 ovs_fatal(0, "parse error in reply");
1125 version = xmalloc(sizeof *version);
1126 version->cookie = fs.cookie;
1127 version->idle_timeout = fs.idle_timeout;
1128 version->hard_timeout = fs.hard_timeout;
1130 version->n_actions = fs.n_actions;
1131 version->actions = xmemdup(fs.actions,
1132 fs.n_actions * sizeof *fs.actions);
1134 fte_insert(cls, &fs.rule, version, index);
1137 VLOG_DBG("received reply with xid %08"PRIx32" "
1138 "!= expected %08"PRIx32, recv_xid, send_xid);
1140 ofpbuf_delete(reply);
1145 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1146 enum nx_flow_format flow_format, struct list *packets)
1148 const struct fte_version *version = fte->versions[index];
1153 fm.cookie = version->cookie;
1155 fm.command = command;
1156 fm.idle_timeout = version->idle_timeout;
1157 fm.hard_timeout = version->hard_timeout;
1158 fm.buffer_id = UINT32_MAX;
1159 fm.out_port = OFPP_NONE;
1160 fm.flags = version->flags;
1161 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1162 command == OFPFC_MODIFY_STRICT) {
1163 fm.actions = version->actions;
1164 fm.n_actions = version->n_actions;
1170 ofm = ofputil_encode_flow_mod(&fm, flow_format, false);
1171 list_push_back(packets, &ofm->list_node);
1175 do_replace_flows(int argc OVS_UNUSED, char *argv[])
1177 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1178 enum nx_flow_format min_flow_format, flow_format;
1179 struct cls_cursor cursor;
1180 struct classifier cls;
1181 struct list requests;
1182 struct vconn *vconn;
1185 classifier_init(&cls);
1186 min_flow_format = read_flows_from_file(argv[2], &cls, FILE_IDX);
1188 open_vconn(argv[1], &vconn);
1189 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
1190 read_flows_from_switch(vconn, flow_format, &cls, SWITCH_IDX);
1192 list_init(&requests);
1194 /* Delete flows that exist on the switch but not in the file. */
1195 cls_cursor_init(&cursor, &cls, NULL);
1196 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1197 struct fte_version *file_ver = fte->versions[FILE_IDX];
1198 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1200 if (sw_ver && !file_ver) {
1201 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1202 flow_format, &requests);
1206 /* Add flows that exist in the file but not on the switch.
1207 * Update flows that exist in both places but differ. */
1208 cls_cursor_init(&cursor, &cls, NULL);
1209 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1210 struct fte_version *file_ver = fte->versions[FILE_IDX];
1211 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1213 if (file_ver && (!sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1214 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, flow_format,
1218 transact_multiple_noreply(vconn, &requests);
1225 read_flows_from_source(const char *source, struct classifier *cls, int index)
1229 if (source[0] == '/' || source[0] == '.'
1230 || (!strchr(source, ':') && !stat(source, &s))) {
1231 read_flows_from_file(source, cls, index);
1233 enum nx_flow_format flow_format;
1234 struct vconn *vconn;
1236 open_vconn(source, &vconn);
1237 flow_format = negotiate_highest_flow_format(vconn, NXFF_OPENFLOW10);
1238 read_flows_from_switch(vconn, flow_format, cls, index);
1244 do_diff_flows(int argc OVS_UNUSED, char *argv[])
1246 bool differences = false;
1247 struct cls_cursor cursor;
1248 struct classifier cls;
1251 classifier_init(&cls);
1252 read_flows_from_source(argv[1], &cls, 0);
1253 read_flows_from_source(argv[2], &cls, 1);
1255 cls_cursor_init(&cursor, &cls, NULL);
1256 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1257 struct fte_version *a = fte->versions[0];
1258 struct fte_version *b = fte->versions[1];
1260 if (!a || !b || !fte_version_equals(a, b)) {
1261 char *rule_s = cls_rule_to_string(&fte->rule);
1263 printf("-%s", rule_s);
1264 fte_version_print(a);
1267 printf("+%s", rule_s);
1268 fte_version_print(b);
1283 /* Undocumented commands for unit testing. */
1286 print_packet_list(struct list *packets)
1288 struct ofpbuf *packet, *next;
1290 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
1291 ofp_print(stdout, packet->data, packet->size, verbosity);
1292 list_remove(&packet->list_node);
1293 ofpbuf_delete(packet);
1297 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1298 * it back to stdout. */
1300 do_parse_flow(int argc OVS_UNUSED, char *argv[])
1302 enum nx_flow_format flow_format;
1303 bool flow_mod_table_id;
1304 struct list packets;
1306 flow_format = NXFF_OPENFLOW10;
1307 if (preferred_flow_format > 0) {
1308 flow_format = preferred_flow_format;
1310 flow_mod_table_id = false;
1312 list_init(&packets);
1313 parse_ofp_flow_mod_str(&packets, &flow_format, &flow_mod_table_id,
1314 argv[1], OFPFC_ADD);
1315 print_packet_list(&packets);
1318 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1319 * add-flows) and prints each of the flows back to stdout. */
1321 do_parse_flows(int argc OVS_UNUSED, char *argv[])
1323 enum nx_flow_format flow_format;
1324 bool flow_mod_table_id;
1325 struct list packets;
1328 file = fopen(argv[1], "r");
1330 ovs_fatal(errno, "%s: open", argv[2]);
1333 flow_format = NXFF_OPENFLOW10;
1334 if (preferred_flow_format > 0) {
1335 flow_format = preferred_flow_format;
1337 flow_mod_table_id = false;
1339 list_init(&packets);
1340 while (parse_ofp_flow_mod_file(&packets, &flow_format, &flow_mod_table_id,
1342 print_packet_list(&packets);
1347 /* "parse-nx-match": reads a series of nx_match specifications as strings from
1348 * stdin, does some internal fussing with them, and then prints them back as
1349 * strings on stdout. */
1351 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1356 while (!ds_get_line(&in, stdin)) {
1357 struct ofpbuf nx_match;
1358 struct cls_rule rule;
1363 /* Delete comments, skip blank lines. */
1369 if (strchr(s, '#')) {
1370 *strchr(s, '#') = '\0';
1372 if (s[strspn(s, " ")] == '\0') {
1377 /* Convert string to nx_match. */
1378 ofpbuf_init(&nx_match, 0);
1379 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1381 /* Convert nx_match to cls_rule. */
1382 error = nx_pull_match(&nx_match, match_len, 0, &rule);
1386 /* Convert cls_rule back to nx_match. */
1387 ofpbuf_uninit(&nx_match);
1388 ofpbuf_init(&nx_match, 0);
1389 match_len = nx_put_match(&nx_match, &rule);
1391 /* Convert nx_match to string. */
1392 out = nx_match_to_string(nx_match.data, match_len);
1396 printf("nx_pull_match() returned error %x\n", error);
1399 ofpbuf_uninit(&nx_match);
1404 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1405 * binary data, interpreting them as an OpenFlow message, and prints the
1406 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1408 do_ofp_print(int argc, char *argv[])
1410 struct ofpbuf packet;
1412 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1413 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1414 ovs_fatal(0, "trailing garbage following hex bytes");
1416 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1417 ofpbuf_uninit(&packet);
1420 static const struct command all_commands[] = {
1421 { "show", 1, 1, do_show },
1422 { "monitor", 1, 2, do_monitor },
1423 { "snoop", 1, 1, do_snoop },
1424 { "dump-desc", 1, 1, do_dump_desc },
1425 { "dump-tables", 1, 1, do_dump_tables },
1426 { "dump-flows", 1, 2, do_dump_flows },
1427 { "dump-aggregate", 1, 2, do_dump_aggregate },
1428 { "queue-stats", 1, 3, do_queue_stats },
1429 { "add-flow", 2, 2, do_add_flow },
1430 { "add-flows", 2, 2, do_add_flows },
1431 { "mod-flows", 2, 2, do_mod_flows },
1432 { "del-flows", 1, 2, do_del_flows },
1433 { "replace-flows", 2, 2, do_replace_flows },
1434 { "diff-flows", 2, 2, do_diff_flows },
1435 { "dump-ports", 1, 2, do_dump_ports },
1436 { "mod-port", 3, 3, do_mod_port },
1437 { "probe", 1, 1, do_probe },
1438 { "ping", 1, 2, do_ping },
1439 { "benchmark", 3, 3, do_benchmark },
1440 { "help", 0, INT_MAX, do_help },
1442 /* Undocumented commands for testing. */
1443 { "parse-flow", 1, 1, do_parse_flow },
1444 { "parse-flows", 1, 1, do_parse_flows },
1445 { "parse-nx-match", 0, 0, do_parse_nx_match },
1446 { "ofp-print", 1, 2, do_ofp_print },
1448 { NULL, 0, 0, NULL },