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 /* --readd: If ture, on replace-flows, re-add even flows that have not changed
59 * (to reset flow counters). */
62 /* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
63 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
64 static int preferred_flow_format = -1;
66 /* -m, --more: Additional verbosity for ofp-print functions. */
69 static const struct command all_commands[];
71 static void usage(void) NO_RETURN;
72 static void parse_options(int argc, char *argv[]);
75 main(int argc, char *argv[])
77 set_program_name(argv[0]);
78 parse_options(argc, argv);
79 signal(SIGPIPE, SIG_IGN);
80 run_command(argc - optind, argv + optind, all_commands);
85 parse_options(int argc, char *argv[])
88 OPT_STRICT = UCHAR_MAX + 1,
92 static struct option long_options[] = {
93 {"timeout", required_argument, NULL, 't'},
94 {"strict", no_argument, NULL, OPT_STRICT},
95 {"readd", no_argument, NULL, OPT_READD},
96 {"flow-format", required_argument, NULL, 'F'},
97 {"more", no_argument, NULL, 'm'},
98 {"help", no_argument, NULL, 'h'},
99 {"version", no_argument, NULL, 'V'},
101 STREAM_SSL_LONG_OPTIONS,
104 char *short_options = long_options_to_short_options(long_options);
107 unsigned long int timeout;
110 c = getopt_long(argc, argv, short_options, long_options, NULL);
117 timeout = strtoul(optarg, NULL, 10);
119 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
127 preferred_flow_format = ofputil_flow_format_from_string(optarg);
128 if (preferred_flow_format < 0) {
129 ovs_fatal(0, "unknown flow format `%s'", optarg);
141 ovs_print_version(OFP_VERSION, OFP_VERSION);
153 STREAM_SSL_OPTION_HANDLERS
168 printf("%s: OpenFlow switch management utility\n"
169 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
170 "\nFor OpenFlow switches:\n"
171 " show SWITCH show OpenFlow information\n"
172 " dump-desc SWITCH print switch description\n"
173 " dump-tables SWITCH print table stats\n"
174 " mod-port SWITCH IFACE ACT modify port behavior\n"
175 " get-frags SWITCH print fragment handling behavior\n"
176 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
177 " dump-ports SWITCH [PORT] print port statistics\n"
178 " dump-flows SWITCH print all flow entries\n"
179 " dump-flows SWITCH FLOW print matching FLOWs\n"
180 " dump-aggregate SWITCH print aggregate flow statistics\n"
181 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
182 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
183 " add-flow SWITCH FLOW add flow described by FLOW\n"
184 " add-flows SWITCH FILE add flows from FILE\n"
185 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
186 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
187 " replace-flows SWITCH FILE replace flows with those in FILE\n"
188 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
189 "\nFor OpenFlow switches and controllers:\n"
190 " probe VCONN probe whether VCONN is up\n"
191 " ping VCONN [N] latency of N-byte echos\n"
192 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
193 "where each SWITCH is an active OpenFlow connection method.\n",
194 program_name, program_name);
195 vconn_usage(true, false, false);
197 printf("\nOther options:\n"
198 " --strict use strict match for flow commands\n"
199 " --readd replace flows that haven't changed\n"
200 " -F, --flow-format=FORMAT force particular flow format\n"
201 " -m, --more be more verbose printing OpenFlow\n"
202 " -t, --timeout=SECS give up after SECS seconds\n"
203 " -h, --help display this help message\n"
204 " -V, --version display version information\n");
208 static void run(int retval, const char *message, ...)
211 static void run(int retval, const char *message, ...)
216 va_start(args, message);
217 ovs_fatal_valist(retval, message, args);
221 /* Generic commands. */
224 open_vconn_socket(const char *name, struct vconn **vconnp)
226 char *vconn_name = xasprintf("unix:%s", name);
227 VLOG_DBG("connecting to %s", vconn_name);
228 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
229 "connecting to %s", vconn_name);
234 open_vconn__(const char *name, const char *default_suffix,
235 struct vconn **vconnp)
237 char *datapath_name, *datapath_type, *socket_name;
241 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
243 ofproto_parse_name(name, &datapath_name, &datapath_type);
244 socket_name = xasprintf("%s/%s.%s",
245 ovs_rundir(), datapath_name, default_suffix);
249 if (strchr(name, ':')) {
250 run(vconn_open_block(name, OFP_VERSION, vconnp),
251 "connecting to %s", name);
252 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
253 open_vconn_socket(name, vconnp);
254 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
255 open_vconn_socket(bridge_path, vconnp);
256 } else if (!stat(socket_name, &s)) {
257 if (!S_ISSOCK(s.st_mode)) {
258 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
261 open_vconn_socket(socket_name, vconnp);
263 ovs_fatal(0, "%s is not a bridge or a socket", name);
271 open_vconn(const char *name, struct vconn **vconnp)
273 return open_vconn__(name, "mgmt", vconnp);
277 alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
279 struct ofp_stats_msg *rq;
281 rq = make_openflow(rq_len, OFPT_STATS_REQUEST, bufferp);
282 rq->type = htons(type);
283 rq->flags = htons(0);
288 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
290 update_openflow_length(buffer);
291 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
295 dump_transaction(const char *vconn_name, struct ofpbuf *request)
298 struct ofpbuf *reply;
300 update_openflow_length(request);
301 open_vconn(vconn_name, &vconn);
302 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
303 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
308 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
310 struct ofpbuf *request;
311 make_openflow(sizeof(struct ofp_header), request_type, &request);
312 dump_transaction(vconn_name, request);
316 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
318 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
322 open_vconn(vconn_name, &vconn);
323 send_openflow_buffer(vconn, request);
326 struct ofpbuf *reply;
328 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
329 recv_xid = ((struct ofp_header *) reply->data)->xid;
330 if (send_xid == recv_xid) {
331 struct ofp_stats_msg *osm;
333 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
335 osm = ofpbuf_at(reply, 0, sizeof *osm);
336 done = !osm || !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
338 VLOG_DBG("received reply with xid %08"PRIx32" "
339 "!= expected %08"PRIx32, recv_xid, send_xid);
341 ofpbuf_delete(reply);
347 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
349 struct ofpbuf *request;
350 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
351 dump_stats_transaction(vconn_name, request);
354 /* Sends 'request', which should be a request that only has a reply if an error
355 * occurs, and waits for it to succeed or fail. If an error does occur, prints
356 * it and exits with an error.
358 * Destroys all of the 'requests'. */
360 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
362 struct ofpbuf *request, *reply;
364 LIST_FOR_EACH (request, list_node, requests) {
365 update_openflow_length(request);
368 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
369 "talking to %s", vconn_get_name(vconn));
371 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
374 ofpbuf_delete(reply);
377 /* Sends 'request', which should be a request that only has a reply if an error
378 * occurs, and waits for it to succeed or fail. If an error does occur, prints
379 * it and exits with an error.
381 * Destroys 'request'. */
383 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
385 struct list requests;
387 list_init(&requests);
388 list_push_back(&requests, &request->list_node);
389 transact_multiple_noreply(vconn, &requests);
393 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
395 struct ofp_switch_config *config;
396 struct ofp_header *header;
397 struct ofpbuf *request;
398 struct ofpbuf *reply;
400 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
402 run(vconn_transact(vconn, request, &reply),
403 "talking to %s", vconn_get_name(vconn));
405 header = reply->data;
406 if (header->type != OFPT_GET_CONFIG_REPLY ||
407 header->length != htons(sizeof *config)) {
408 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
411 config = reply->data;
416 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
418 struct ofp_switch_config *config;
419 struct ofp_header save_header;
420 struct ofpbuf *request;
422 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
423 save_header = config->header;
425 config->header = save_header;
427 transact_noreply(vconn, request);
431 do_show(int argc OVS_UNUSED, char *argv[])
433 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
434 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
438 do_dump_desc(int argc OVS_UNUSED, char *argv[])
440 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
444 do_dump_tables(int argc OVS_UNUSED, char *argv[])
446 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
449 /* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
450 * 'port_name' (which may be a port name or number), and copies it into
453 fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
454 struct ofp_phy_port *oppp)
456 struct ofpbuf *request, *reply;
457 struct ofp_switch_features *osf;
458 unsigned int port_no;
463 /* Try to interpret the argument as a port number. */
464 if (!str_to_uint(port_name, 10, &port_no)) {
468 /* Fetch the switch's ofp_switch_features. */
469 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
470 open_vconn(vconn_name, &vconn);
471 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
474 if (reply->size < sizeof *osf) {
475 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
476 vconn_name, reply->size);
478 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
480 for (port_idx = 0; port_idx < n_ports; port_idx++) {
481 const struct ofp_phy_port *opp = &osf->ports[port_idx];
483 if (port_no != UINT_MAX
484 ? htons(port_no) == opp->port_no
485 : !strncmp(opp->name, port_name, sizeof opp->name)) {
487 ofpbuf_delete(reply);
492 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
495 /* Returns the port number corresponding to 'port_name' (which may be a port
496 * name or number) within the switch 'vconn_name'. */
498 str_to_port_no(const char *vconn_name, const char *port_name)
500 unsigned int port_no;
502 if (str_to_uint(port_name, 10, &port_no)) {
505 struct ofp_phy_port opp;
507 fetch_ofp_phy_port(vconn_name, port_name, &opp);
508 return ntohs(opp.port_no);
513 try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
515 struct ofpbuf *sff, *reply;
517 sff = ofputil_make_set_flow_format(flow_format);
518 run(vconn_transact_noreply(vconn, sff, &reply),
519 "talking to %s", vconn_get_name(vconn));
521 char *s = ofp_to_string(reply->data, reply->size, 2);
522 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
523 vconn_get_name(vconn),
524 ofputil_flow_format_to_string(flow_format),
527 ofpbuf_delete(reply);
534 set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
536 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
537 transact_noreply(vconn, sff);
538 VLOG_DBG("%s: using user-specified flow format %s",
539 vconn_get_name(vconn),
540 ofputil_flow_format_to_string(flow_format));
543 static enum nx_flow_format
544 negotiate_highest_flow_format(struct vconn *vconn,
545 enum nx_flow_format min_format)
547 if (preferred_flow_format != -1) {
548 if (preferred_flow_format < min_format) {
549 ovs_fatal(0, "%s: cannot use requested flow format %s for "
550 "specified flow", vconn_get_name(vconn),
551 ofputil_flow_format_to_string(min_format));
554 set_flow_format(vconn, preferred_flow_format);
555 return preferred_flow_format;
557 enum nx_flow_format flow_format;
559 if (try_set_flow_format(vconn, NXFF_NXM)) {
560 flow_format = NXFF_NXM;
562 flow_format = NXFF_OPENFLOW10;
565 if (flow_format < min_format) {
566 ovs_fatal(0, "%s: cannot use switch's most advanced flow format "
567 "%s for specified flow", vconn_get_name(vconn),
568 ofputil_flow_format_to_string(min_format));
571 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
572 ofputil_flow_format_to_string(flow_format));
578 do_dump_flows__(int argc, char *argv[], bool aggregate)
580 enum nx_flow_format min_flow_format, flow_format;
581 struct ofputil_flow_stats_request fsr;
582 struct ofpbuf *request;
585 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
587 open_vconn(argv[1], &vconn);
588 min_flow_format = ofputil_min_flow_format(&fsr.match);
589 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
590 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
591 dump_stats_transaction(argv[1], request);
596 do_dump_flows(int argc, char *argv[])
598 return do_dump_flows__(argc, argv, false);
602 do_dump_aggregate(int argc, char *argv[])
604 return do_dump_flows__(argc, argv, true);
608 do_queue_stats(int argc, char *argv[])
610 struct ofp_queue_stats_request *req;
611 struct ofpbuf *request;
613 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
615 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
616 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
618 req->port_no = htons(OFPP_ALL);
620 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
621 req->queue_id = htonl(atoi(argv[3]));
623 req->queue_id = htonl(OFPQ_ALL);
626 memset(req->pad, 0, sizeof req->pad);
628 dump_stats_transaction(argv[1], request);
631 /* Sets up the flow format for a vconn that will be used to modify the flow
632 * table. Returns the flow format used, after possibly adding an OpenFlow
633 * request to 'requests'.
635 * If 'preferred_flow_format' is -1, returns NXFF_OPENFLOW10 without modifying
636 * 'requests', since NXFF_OPENFLOW10 is the default flow format for any
637 * OpenFlow connection.
639 * If 'preferred_flow_format' is a specific format, adds a request to set that
640 * format to 'requests' and returns the format. */
641 static enum nx_flow_format
642 set_initial_format_for_flow_mod(struct list *requests)
644 if (preferred_flow_format < 0) {
645 return NXFF_OPENFLOW10;
649 sff = ofputil_make_set_flow_format(preferred_flow_format);
650 list_push_back(requests, &sff->list_node);
651 return preferred_flow_format;
655 /* Checks that 'flow_format' is acceptable as a flow format after a flow_mod
656 * operation, given the global 'preferred_flow_format'. */
658 check_final_format_for_flow_mod(enum nx_flow_format flow_format)
660 if (preferred_flow_format >= 0 && flow_format > preferred_flow_format) {
661 ovs_fatal(0, "flow cannot be expressed in flow format %s "
662 "(flow format %s or better is required)",
663 ofputil_flow_format_to_string(preferred_flow_format),
664 ofputil_flow_format_to_string(flow_format));
669 do_flow_mod_file__(int argc OVS_UNUSED, char *argv[], uint16_t command)
671 enum nx_flow_format flow_format;
672 bool flow_mod_table_id;
673 struct list requests;
677 file = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r");
679 ovs_fatal(errno, "%s: open", argv[2]);
682 list_init(&requests);
683 flow_format = set_initial_format_for_flow_mod(&requests);
684 flow_mod_table_id = false;
686 open_vconn(argv[1], &vconn);
687 while (parse_ofp_flow_mod_file(&requests, &flow_format, &flow_mod_table_id,
689 check_final_format_for_flow_mod(flow_format);
690 transact_multiple_noreply(vconn, &requests);
700 do_flow_mod__(int argc, char *argv[], uint16_t command)
702 enum nx_flow_format flow_format;
703 bool flow_mod_table_id;
704 struct list requests;
707 if (argc > 2 && !strcmp(argv[2], "-")) {
708 do_flow_mod_file__(argc, argv, command);
712 list_init(&requests);
713 flow_format = set_initial_format_for_flow_mod(&requests);
714 flow_mod_table_id = false;
716 parse_ofp_flow_mod_str(&requests, &flow_format, &flow_mod_table_id,
717 argc > 2 ? argv[2] : "", command, false);
718 check_final_format_for_flow_mod(flow_format);
720 open_vconn(argv[1], &vconn);
721 transact_multiple_noreply(vconn, &requests);
726 do_add_flow(int argc, char *argv[])
728 do_flow_mod__(argc, argv, OFPFC_ADD);
732 do_add_flows(int argc, char *argv[])
734 do_flow_mod_file__(argc, argv, OFPFC_ADD);
738 do_mod_flows(int argc, char *argv[])
740 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
744 do_del_flows(int argc, char *argv[])
746 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
750 monitor_vconn(struct vconn *vconn)
754 run(vconn_recv_block(vconn, &b), "vconn_recv");
755 ofp_print(stderr, b->data, b->size, verbosity + 2);
761 do_monitor(int argc, char *argv[])
765 open_vconn(argv[1], &vconn);
767 struct ofp_switch_config config;
769 fetch_switch_config(vconn, &config);
770 config.miss_send_len = htons(atoi(argv[2]));
771 set_switch_config(vconn, &config);
773 monitor_vconn(vconn);
777 do_snoop(int argc OVS_UNUSED, char *argv[])
781 open_vconn__(argv[1], "snoop", &vconn);
782 monitor_vconn(vconn);
786 do_dump_ports(int argc, char *argv[])
788 struct ofp_port_stats_request *req;
789 struct ofpbuf *request;
792 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
793 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
794 req->port_no = htons(port);
795 dump_stats_transaction(argv[1], request);
799 do_probe(int argc OVS_UNUSED, char *argv[])
801 struct ofpbuf *request;
803 struct ofpbuf *reply;
805 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
806 open_vconn(argv[1], &vconn);
807 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
808 if (reply->size != sizeof(struct ofp_header)) {
809 ovs_fatal(0, "reply does not match request");
811 ofpbuf_delete(reply);
816 do_mod_port(int argc OVS_UNUSED, char *argv[])
818 struct ofp_port_mod *opm;
819 struct ofp_phy_port opp;
820 struct ofpbuf *request;
823 fetch_ofp_phy_port(argv[1], argv[2], &opp);
825 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
826 opm->port_no = opp.port_no;
827 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
828 opm->config = htonl(0);
829 opm->mask = htonl(0);
830 opm->advertise = htonl(0);
832 if (!strcasecmp(argv[3], "up")) {
833 opm->mask |= htonl(OFPPC_PORT_DOWN);
834 } else if (!strcasecmp(argv[3], "down")) {
835 opm->mask |= htonl(OFPPC_PORT_DOWN);
836 opm->config |= htonl(OFPPC_PORT_DOWN);
837 } else if (!strcasecmp(argv[3], "flood")) {
838 opm->mask |= htonl(OFPPC_NO_FLOOD);
839 } else if (!strcasecmp(argv[3], "noflood")) {
840 opm->mask |= htonl(OFPPC_NO_FLOOD);
841 opm->config |= htonl(OFPPC_NO_FLOOD);
843 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
846 open_vconn(argv[1], &vconn);
847 transact_noreply(vconn, request);
852 do_get_frags(int argc OVS_UNUSED, char *argv[])
854 struct ofp_switch_config config;
857 open_vconn(argv[1], &vconn);
858 fetch_switch_config(vconn, &config);
859 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
864 do_set_frags(int argc OVS_UNUSED, char *argv[])
866 struct ofp_switch_config config;
867 enum ofp_config_flags mode;
871 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
872 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
875 open_vconn(argv[1], &vconn);
876 fetch_switch_config(vconn, &config);
877 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
878 if (flags != config.flags) {
879 /* Set the configuration. */
880 config.flags = flags;
881 set_switch_config(vconn, &config);
883 /* Then retrieve the configuration to see if it really took. OpenFlow
884 * doesn't define error reporting for bad modes, so this is all we can
886 fetch_switch_config(vconn, &config);
887 if (flags != config.flags) {
888 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
889 "switch probably doesn't support mode \"%s\")",
890 argv[1], ofputil_frag_handling_to_string(mode));
897 do_ping(int argc, char *argv[])
899 size_t max_payload = 65535 - sizeof(struct ofp_header);
900 unsigned int payload;
904 payload = argc > 2 ? atoi(argv[2]) : 64;
905 if (payload > max_payload) {
906 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
909 open_vconn(argv[1], &vconn);
910 for (i = 0; i < 10; i++) {
911 struct timeval start, end;
912 struct ofpbuf *request, *reply;
913 struct ofp_header *rq_hdr, *rpy_hdr;
915 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
916 OFPT_ECHO_REQUEST, &request);
917 random_bytes(rq_hdr + 1, payload);
919 xgettimeofday(&start);
920 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
923 rpy_hdr = reply->data;
924 if (reply->size != request->size
925 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
926 || rpy_hdr->xid != rq_hdr->xid
927 || rpy_hdr->type != OFPT_ECHO_REPLY) {
928 printf("Reply does not match request. Request:\n");
929 ofp_print(stdout, request, request->size, verbosity + 2);
931 ofp_print(stdout, reply, reply->size, verbosity + 2);
933 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
934 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
935 (1000*(double)(end.tv_sec - start.tv_sec))
936 + (.001*(end.tv_usec - start.tv_usec)));
937 ofpbuf_delete(request);
938 ofpbuf_delete(reply);
944 do_benchmark(int argc OVS_UNUSED, char *argv[])
946 size_t max_payload = 65535 - sizeof(struct ofp_header);
947 struct timeval start, end;
948 unsigned int payload_size, message_size;
954 payload_size = atoi(argv[2]);
955 if (payload_size > max_payload) {
956 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
958 message_size = sizeof(struct ofp_header) + payload_size;
960 count = atoi(argv[3]);
962 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
963 count, message_size, count * message_size);
965 open_vconn(argv[1], &vconn);
966 xgettimeofday(&start);
967 for (i = 0; i < count; i++) {
968 struct ofpbuf *request, *reply;
969 struct ofp_header *rq_hdr;
971 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
972 memset(rq_hdr + 1, 0, payload_size);
973 run(vconn_transact(vconn, request, &reply), "transact");
974 ofpbuf_delete(reply);
979 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
980 + (.001*(end.tv_usec - start.tv_usec)));
981 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
982 duration, count / (duration / 1000.0),
983 count * message_size / (duration / 1000.0));
987 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
992 /* replace-flows and diff-flows commands. */
994 /* A flow table entry, possibly with two different versions. */
996 struct cls_rule rule; /* Within a "struct classifier". */
997 struct fte_version *versions[2];
1000 /* One version of a Flow Table Entry. */
1001 struct fte_version {
1003 uint16_t idle_timeout;
1004 uint16_t hard_timeout;
1006 union ofp_action *actions;
1010 /* Frees 'version' and the data that it owns. */
1012 fte_version_free(struct fte_version *version)
1015 free(version->actions);
1020 /* Returns true if 'a' and 'b' are the same, false if they differ.
1022 * Ignores differences in 'flags' because there's no way to retrieve flags from
1023 * an OpenFlow switch. We have to assume that they are the same. */
1025 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1027 return (a->cookie == b->cookie
1028 && a->idle_timeout == b->idle_timeout
1029 && a->hard_timeout == b->hard_timeout
1030 && a->n_actions == b->n_actions
1031 && !memcmp(a->actions, b->actions,
1032 a->n_actions * sizeof *a->actions));
1035 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1036 * associated with the version. */
1038 fte_version_print(const struct fte_version *version)
1042 if (version->cookie != htonll(0)) {
1043 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1045 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1046 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1048 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1049 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1053 ofp_print_actions(&s, version->actions, version->n_actions);
1054 printf(" %s\n", ds_cstr(&s));
1059 fte_from_cls_rule(const struct cls_rule *cls_rule)
1061 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1064 /* Frees 'fte' and its versions. */
1066 fte_free(struct fte *fte)
1069 fte_version_free(fte->versions[0]);
1070 fte_version_free(fte->versions[1]);
1075 /* Frees all of the FTEs within 'cls'. */
1077 fte_free_all(struct classifier *cls)
1079 struct cls_cursor cursor;
1080 struct fte *fte, *next;
1082 cls_cursor_init(&cursor, cls, NULL);
1083 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1084 classifier_remove(cls, &fte->rule);
1089 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1090 * necessary. Sets 'version' as the version of that rule with the given
1091 * 'index', replacing any existing version, if any.
1093 * Takes ownership of 'version'. */
1095 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1096 struct fte_version *version, int index)
1098 struct fte *old, *fte;
1100 fte = xzalloc(sizeof *fte);
1102 fte->versions[index] = version;
1104 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1106 fte_version_free(old->versions[index]);
1107 fte->versions[!index] = old->versions[!index];
1112 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1113 * with the specified 'index'. Returns the minimum flow format required to
1114 * represent the flows that were read. */
1115 static enum nx_flow_format
1116 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1118 enum nx_flow_format min_flow_format;
1122 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1124 ovs_fatal(errno, "%s: open", filename);
1128 min_flow_format = NXFF_OPENFLOW10;
1129 while (!ds_get_preprocessed_line(&s, file)) {
1130 struct fte_version *version;
1131 struct ofputil_flow_mod fm;
1132 enum nx_flow_format min_ff;
1134 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1136 version = xmalloc(sizeof *version);
1137 version->cookie = fm.cookie;
1138 version->idle_timeout = fm.idle_timeout;
1139 version->hard_timeout = fm.hard_timeout;
1140 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1141 version->actions = fm.actions;
1142 version->n_actions = fm.n_actions;
1144 min_ff = ofputil_min_flow_format(&fm.cr);
1145 min_flow_format = MAX(min_flow_format, min_ff);
1146 check_final_format_for_flow_mod(min_flow_format);
1148 fte_insert(cls, &fm.cr, version, index);
1152 if (file != stdin) {
1156 return min_flow_format;
1159 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1160 * format 'flow_format', and adds them as flow table entries in 'cls' for the
1161 * version with the specified 'index'. */
1163 read_flows_from_switch(struct vconn *vconn, enum nx_flow_format flow_format,
1164 struct classifier *cls, int index)
1166 struct ofputil_flow_stats_request fsr;
1167 struct ofpbuf *request;
1171 fsr.aggregate = false;
1172 cls_rule_init_catchall(&fsr.match, 0);
1173 fsr.out_port = OFPP_NONE;
1174 fsr.table_id = 0xff;
1175 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
1176 send_xid = ((struct ofp_header *) request->data)->xid;
1177 send_openflow_buffer(vconn, request);
1182 struct ofpbuf *reply;
1184 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1185 recv_xid = ((struct ofp_header *) reply->data)->xid;
1186 if (send_xid == recv_xid) {
1187 const struct ofputil_msg_type *type;
1188 const struct ofp_stats_msg *osm;
1189 enum ofputil_msg_code code;
1191 ofputil_decode_msg_type(reply->data, &type);
1192 code = ofputil_msg_type_code(type);
1193 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1194 code != OFPUTIL_NXST_FLOW_REPLY) {
1195 ovs_fatal(0, "received bad reply: %s",
1196 ofp_to_string(reply->data, reply->size,
1201 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
1206 struct fte_version *version;
1207 struct ofputil_flow_stats fs;
1210 retval = ofputil_decode_flow_stats_reply(&fs, reply);
1212 if (retval != EOF) {
1213 ovs_fatal(0, "parse error in reply");
1218 version = xmalloc(sizeof *version);
1219 version->cookie = fs.cookie;
1220 version->idle_timeout = fs.idle_timeout;
1221 version->hard_timeout = fs.hard_timeout;
1223 version->n_actions = fs.n_actions;
1224 version->actions = xmemdup(fs.actions,
1225 fs.n_actions * sizeof *fs.actions);
1227 fte_insert(cls, &fs.rule, version, index);
1230 VLOG_DBG("received reply with xid %08"PRIx32" "
1231 "!= expected %08"PRIx32, recv_xid, send_xid);
1233 ofpbuf_delete(reply);
1238 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1239 enum nx_flow_format flow_format, struct list *packets)
1241 const struct fte_version *version = fte->versions[index];
1242 struct ofputil_flow_mod fm;
1246 fm.cookie = version->cookie;
1248 fm.command = command;
1249 fm.idle_timeout = version->idle_timeout;
1250 fm.hard_timeout = version->hard_timeout;
1251 fm.buffer_id = UINT32_MAX;
1252 fm.out_port = OFPP_NONE;
1253 fm.flags = version->flags;
1254 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1255 command == OFPFC_MODIFY_STRICT) {
1256 fm.actions = version->actions;
1257 fm.n_actions = version->n_actions;
1263 ofm = ofputil_encode_flow_mod(&fm, flow_format, false);
1264 list_push_back(packets, &ofm->list_node);
1268 do_replace_flows(int argc OVS_UNUSED, char *argv[])
1270 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1271 enum nx_flow_format min_flow_format, flow_format;
1272 struct cls_cursor cursor;
1273 struct classifier cls;
1274 struct list requests;
1275 struct vconn *vconn;
1278 classifier_init(&cls);
1279 min_flow_format = read_flows_from_file(argv[2], &cls, FILE_IDX);
1281 open_vconn(argv[1], &vconn);
1282 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
1283 read_flows_from_switch(vconn, flow_format, &cls, SWITCH_IDX);
1285 list_init(&requests);
1287 /* Delete flows that exist on the switch but not in the file. */
1288 cls_cursor_init(&cursor, &cls, NULL);
1289 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1290 struct fte_version *file_ver = fte->versions[FILE_IDX];
1291 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1293 if (sw_ver && !file_ver) {
1294 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1295 flow_format, &requests);
1299 /* Add flows that exist in the file but not on the switch.
1300 * Update flows that exist in both places but differ. */
1301 cls_cursor_init(&cursor, &cls, NULL);
1302 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1303 struct fte_version *file_ver = fte->versions[FILE_IDX];
1304 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1307 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1308 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, flow_format,
1312 transact_multiple_noreply(vconn, &requests);
1319 read_flows_from_source(const char *source, struct classifier *cls, int index)
1323 if (source[0] == '/' || source[0] == '.'
1324 || (!strchr(source, ':') && !stat(source, &s))) {
1325 read_flows_from_file(source, cls, index);
1327 enum nx_flow_format flow_format;
1328 struct vconn *vconn;
1330 open_vconn(source, &vconn);
1331 flow_format = negotiate_highest_flow_format(vconn, NXFF_OPENFLOW10);
1332 read_flows_from_switch(vconn, flow_format, cls, index);
1338 do_diff_flows(int argc OVS_UNUSED, char *argv[])
1340 bool differences = false;
1341 struct cls_cursor cursor;
1342 struct classifier cls;
1345 classifier_init(&cls);
1346 read_flows_from_source(argv[1], &cls, 0);
1347 read_flows_from_source(argv[2], &cls, 1);
1349 cls_cursor_init(&cursor, &cls, NULL);
1350 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1351 struct fte_version *a = fte->versions[0];
1352 struct fte_version *b = fte->versions[1];
1354 if (!a || !b || !fte_version_equals(a, b)) {
1355 char *rule_s = cls_rule_to_string(&fte->rule);
1357 printf("-%s", rule_s);
1358 fte_version_print(a);
1361 printf("+%s", rule_s);
1362 fte_version_print(b);
1377 /* Undocumented commands for unit testing. */
1380 print_packet_list(struct list *packets)
1382 struct ofpbuf *packet, *next;
1384 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
1385 ofp_print(stdout, packet->data, packet->size, verbosity);
1386 list_remove(&packet->list_node);
1387 ofpbuf_delete(packet);
1391 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1392 * it back to stdout. */
1394 do_parse_flow(int argc OVS_UNUSED, char *argv[])
1396 enum nx_flow_format flow_format;
1397 bool flow_mod_table_id;
1398 struct list packets;
1400 flow_format = NXFF_OPENFLOW10;
1401 if (preferred_flow_format > 0) {
1402 flow_format = preferred_flow_format;
1404 flow_mod_table_id = false;
1406 list_init(&packets);
1407 parse_ofp_flow_mod_str(&packets, &flow_format, &flow_mod_table_id,
1408 argv[1], OFPFC_ADD, false);
1409 print_packet_list(&packets);
1412 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1413 * add-flows) and prints each of the flows back to stdout. */
1415 do_parse_flows(int argc OVS_UNUSED, char *argv[])
1417 enum nx_flow_format flow_format;
1418 bool flow_mod_table_id;
1419 struct list packets;
1422 file = fopen(argv[1], "r");
1424 ovs_fatal(errno, "%s: open", argv[1]);
1427 flow_format = NXFF_OPENFLOW10;
1428 if (preferred_flow_format > 0) {
1429 flow_format = preferred_flow_format;
1431 flow_mod_table_id = false;
1433 list_init(&packets);
1434 while (parse_ofp_flow_mod_file(&packets, &flow_format, &flow_mod_table_id,
1436 print_packet_list(&packets);
1441 /* "parse-nx-match": reads a series of nx_match specifications as strings from
1442 * stdin, does some internal fussing with them, and then prints them back as
1443 * strings on stdout. */
1445 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1450 while (!ds_get_line(&in, stdin)) {
1451 struct ofpbuf nx_match;
1452 struct cls_rule rule;
1457 /* Delete comments, skip blank lines. */
1463 if (strchr(s, '#')) {
1464 *strchr(s, '#') = '\0';
1466 if (s[strspn(s, " ")] == '\0') {
1471 /* Convert string to nx_match. */
1472 ofpbuf_init(&nx_match, 0);
1473 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1475 /* Convert nx_match to cls_rule. */
1476 error = nx_pull_match(&nx_match, match_len, 0, &rule);
1480 /* Convert cls_rule back to nx_match. */
1481 ofpbuf_uninit(&nx_match);
1482 ofpbuf_init(&nx_match, 0);
1483 match_len = nx_put_match(&nx_match, &rule);
1485 /* Convert nx_match to string. */
1486 out = nx_match_to_string(nx_match.data, match_len);
1490 printf("nx_pull_match() returned error %x (%s)\n", error,
1491 ofputil_error_to_string(error));
1494 ofpbuf_uninit(&nx_match);
1499 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1500 * binary data, interpreting them as an OpenFlow message, and prints the
1501 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1503 do_ofp_print(int argc, char *argv[])
1505 struct ofpbuf packet;
1507 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1508 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1509 ovs_fatal(0, "trailing garbage following hex bytes");
1511 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1512 ofpbuf_uninit(&packet);
1515 static const struct command all_commands[] = {
1516 { "show", 1, 1, do_show },
1517 { "monitor", 1, 2, do_monitor },
1518 { "snoop", 1, 1, do_snoop },
1519 { "dump-desc", 1, 1, do_dump_desc },
1520 { "dump-tables", 1, 1, do_dump_tables },
1521 { "dump-flows", 1, 2, do_dump_flows },
1522 { "dump-aggregate", 1, 2, do_dump_aggregate },
1523 { "queue-stats", 1, 3, do_queue_stats },
1524 { "add-flow", 2, 2, do_add_flow },
1525 { "add-flows", 2, 2, do_add_flows },
1526 { "mod-flows", 2, 2, do_mod_flows },
1527 { "del-flows", 1, 2, do_del_flows },
1528 { "replace-flows", 2, 2, do_replace_flows },
1529 { "diff-flows", 2, 2, do_diff_flows },
1530 { "dump-ports", 1, 2, do_dump_ports },
1531 { "mod-port", 3, 3, do_mod_port },
1532 { "get-frags", 1, 1, do_get_frags },
1533 { "set-frags", 2, 2, do_set_frags },
1534 { "probe", 1, 1, do_probe },
1535 { "ping", 1, 2, do_ping },
1536 { "benchmark", 3, 3, do_benchmark },
1537 { "help", 0, INT_MAX, do_help },
1539 /* Undocumented commands for testing. */
1540 { "parse-flow", 1, 1, do_parse_flow },
1541 { "parse-flows", 1, 1, do_parse_flows },
1542 { "parse-nx-match", 0, 0, do_parse_nx_match },
1543 { "ofp-print", 1, 2, do_ofp_print },
1545 { NULL, 0, 0, NULL },