2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
28 #include <sys/fcntl.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
48 #include "ofp-version-opt.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
54 #include "pcap-file.h"
55 #include "poll-loop.h"
57 #include "stream-ssl.h"
58 #include "socket-util.h"
64 #include "meta-flow.h"
67 VLOG_DEFINE_THIS_MODULE(ofctl);
69 /* --strict: Use strict matching for flow mod commands? Additionally governs
70 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
74 /* --readd: If true, on replace-flows, re-add even flows that have not changed
75 * (to reset flow counters). */
78 /* -F, --flow-format: Allowed protocols. By default, any protocol is
80 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
82 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
83 * commands. Either one of NXPIF_* to force a particular packet_in format, or
84 * -1 to let ovs-ofctl choose the default. */
85 static int preferred_packet_in_format = -1;
87 /* -m, --more: Additional verbosity for ofp-print functions. */
90 /* --timestamp: Print a timestamp before each received packet on "monitor" and
92 static bool timestamp;
94 /* --sort, --rsort: Sort order. */
95 enum sort_order { SORT_ASC, SORT_DESC };
96 struct sort_criterion {
97 const struct mf_field *field; /* NULL means to sort by priority. */
98 enum sort_order order;
100 static struct sort_criterion *criteria;
101 static size_t n_criteria, allocated_criteria;
103 static const struct command *get_all_commands(void);
105 static void usage(void) NO_RETURN;
106 static void parse_options(int argc, char *argv[]);
108 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
109 struct ofpbuf **replyp,
110 struct ofputil_flow_stats *,
111 struct ofpbuf *ofpacts);
113 main(int argc, char *argv[])
115 set_program_name(argv[0]);
116 parse_options(argc, argv);
117 fatal_ignore_sigpipe();
118 run_command(argc - optind, argv + optind, get_all_commands());
123 add_sort_criterion(enum sort_order order, const char *field)
125 struct sort_criterion *sc;
127 if (n_criteria >= allocated_criteria) {
128 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
131 sc = &criteria[n_criteria++];
132 if (!field || !strcasecmp(field, "priority")) {
135 sc->field = mf_from_name(field);
137 ovs_fatal(0, "%s: unknown field name", field);
144 parse_options(int argc, char *argv[])
147 OPT_STRICT = UCHAR_MAX + 1,
153 OFP_VERSION_OPTION_ENUMS,
156 static const struct option long_options[] = {
157 {"timeout", required_argument, NULL, 't'},
158 {"strict", no_argument, NULL, OPT_STRICT},
159 {"readd", no_argument, NULL, OPT_READD},
160 {"flow-format", required_argument, NULL, 'F'},
161 {"packet-in-format", required_argument, NULL, 'P'},
162 {"more", no_argument, NULL, 'm'},
163 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
164 {"sort", optional_argument, NULL, OPT_SORT},
165 {"rsort", optional_argument, NULL, OPT_RSORT},
166 {"help", no_argument, NULL, 'h'},
168 OFP_VERSION_LONG_OPTIONS,
170 STREAM_SSL_LONG_OPTIONS,
173 char *short_options = long_options_to_short_options(long_options);
175 enum ofputil_protocol version_protocols;
178 unsigned long int timeout;
181 c = getopt_long(argc, argv, short_options, long_options, NULL);
188 timeout = strtoul(optarg, NULL, 10);
190 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
198 allowed_protocols = ofputil_protocols_from_string(optarg);
199 if (!allowed_protocols) {
200 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
205 preferred_packet_in_format =
206 ofputil_packet_in_format_from_string(optarg);
207 if (preferred_packet_in_format < 0) {
208 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
232 add_sort_criterion(SORT_ASC, optarg);
236 add_sort_criterion(SORT_DESC, optarg);
239 DAEMON_OPTION_HANDLERS
240 OFP_VERSION_OPTION_HANDLERS
242 STREAM_SSL_OPTION_HANDLERS
253 /* Always do a final sort pass based on priority. */
254 add_sort_criterion(SORT_DESC, "priority");
259 versions = get_allowed_ofp_versions();
260 version_protocols = ofputil_protocols_from_version_bitmap(versions);
261 if (!(allowed_protocols & version_protocols)) {
262 char *protocols = ofputil_protocols_to_string(allowed_protocols);
263 struct ds version_s = DS_EMPTY_INITIALIZER;
265 ofputil_format_version_bitmap_names(&version_s, versions);
266 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
267 "any of the enabled flow formats (%s). (Use -O to enable "
268 "additional OpenFlow versions or -F to enable additional "
269 "flow formats.)", ds_cstr(&version_s), protocols);
271 allowed_protocols &= version_protocols;
272 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
279 printf("%s: OpenFlow switch management utility\n"
280 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
281 "\nFor OpenFlow switches:\n"
282 " show SWITCH show OpenFlow information\n"
283 " dump-desc SWITCH print switch description\n"
284 " dump-tables SWITCH print table stats\n"
285 " mod-port SWITCH IFACE ACT modify port behavior\n"
286 " mod-table SWITCH MOD modify flow table behavior\n"
287 " get-frags SWITCH print fragment handling behavior\n"
288 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
289 " dump-ports SWITCH [PORT] print port statistics\n"
290 " dump-ports-desc SWITCH print port descriptions\n"
291 " dump-flows SWITCH print all flow entries\n"
292 " dump-flows SWITCH FLOW print matching FLOWs\n"
293 " dump-aggregate SWITCH print aggregate flow statistics\n"
294 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
295 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
296 " add-flow SWITCH FLOW add flow described by FLOW\n"
297 " add-flows SWITCH FILE add flows from FILE\n"
298 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
299 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
300 " replace-flows SWITCH FILE replace flows with those in FILE\n"
301 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
302 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
303 " execute ACTIONS on PACKET\n"
304 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
305 " print packets received from SWITCH\n"
306 " snoop SWITCH snoop on SWITCH and its controller\n"
307 " add-group SWITCH GROUP add group described by GROUP\n"
308 " add-group SWITCH FILE add group from FILE\n"
309 " mod-group SWITCH GROUP modify specific group\n"
310 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
311 " dump-group-features SWITCH print group features\n"
312 " dump-groups SWITCH print group description\n"
313 " dump-group-stats SWITCH [GROUP] print group statistics\n"
314 " queue-get-config SWITCH PORT print queue information for port\n"
315 " add-meter SWITCH METER add meter described by METER\n"
316 " mod-meter SWITCH METER modify specific METER\n"
317 " del-meter SWITCH METER delete METER\n"
318 " del-meters SWITCH delete all meters\n"
319 " dump-meter SWITCH METER print METER configuration\n"
320 " dump-meters SWITCH print all meter configuration\n"
321 " meter-stats SWITCH [METER] print meter statistics\n"
322 " meter-features SWITCH print meter features\n"
323 "\nFor OpenFlow switches and controllers:\n"
324 " probe TARGET probe whether TARGET is up\n"
325 " ping TARGET [N] latency of N-byte echos\n"
326 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
327 "SWITCH or TARGET is an active OpenFlow connection method.\n"
328 "\nOther commands:\n"
329 " ofp-parse FILE print messages read from FILE\n"
330 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
331 program_name, program_name);
332 vconn_usage(true, false, false);
336 printf("\nOther options:\n"
337 " --strict use strict match for flow commands\n"
338 " --readd replace flows that haven't changed\n"
339 " -F, --flow-format=FORMAT force particular flow format\n"
340 " -P, --packet-in-format=FRMT force particular packet in format\n"
341 " -m, --more be more verbose printing OpenFlow\n"
342 " --timestamp (monitor, snoop) print timestamps\n"
343 " -t, --timeout=SECS give up after SECS seconds\n"
344 " --sort[=field] sort in ascending order\n"
345 " --rsort[=field] sort in descending order\n"
346 " -h, --help display this help message\n"
347 " -V, --version display version information\n");
352 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
353 const char *argv[] OVS_UNUSED, void *exiting_)
355 bool *exiting = exiting_;
357 unixctl_command_reply(conn, NULL);
360 static void run(int retval, const char *message, ...)
364 run(int retval, const char *message, ...)
369 va_start(args, message);
370 ovs_fatal_valist(retval, message, args);
374 /* Generic commands. */
377 open_vconn_socket(const char *name, struct vconn **vconnp)
379 char *vconn_name = xasprintf("unix:%s", name);
382 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
384 if (error && error != ENOENT) {
385 ovs_fatal(0, "%s: failed to open socket (%s)", name,
386 ovs_strerror(error));
393 enum open_target { MGMT, SNOOP };
395 static enum ofputil_protocol
396 open_vconn__(const char *name, enum open_target target,
397 struct vconn **vconnp)
399 const char *suffix = target == MGMT ? "mgmt" : "snoop";
400 char *datapath_name, *datapath_type, *socket_name;
401 enum ofputil_protocol protocol;
406 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
408 ofproto_parse_name(name, &datapath_name, &datapath_type);
409 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
413 if (strchr(name, ':')) {
414 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
415 "connecting to %s", name);
416 } else if (!open_vconn_socket(name, vconnp)) {
418 } else if (!open_vconn_socket(bridge_path, vconnp)) {
420 } else if (!open_vconn_socket(socket_name, vconnp)) {
423 ovs_fatal(0, "%s is not a bridge or a socket", name);
426 if (target == SNOOP) {
427 vconn_set_recv_any_version(*vconnp);
433 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
434 error = vconn_connect_block(*vconnp);
436 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
437 ovs_strerror(error));
440 ofp_version = vconn_get_version(*vconnp);
441 protocol = ofputil_protocol_from_ofp_version(ofp_version);
443 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
449 static enum ofputil_protocol
450 open_vconn(const char *name, struct vconn **vconnp)
452 return open_vconn__(name, MGMT, vconnp);
456 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
458 ofpmsg_update_length(buffer);
459 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
463 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
465 struct ofpbuf *reply;
467 ofpmsg_update_length(request);
468 run(vconn_transact(vconn, request, &reply), "talking to %s",
469 vconn_get_name(vconn));
470 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
471 ofpbuf_delete(reply);
475 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
477 struct ofpbuf *request;
480 open_vconn(vconn_name, &vconn);
481 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
482 dump_transaction(vconn, request);
487 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
489 const struct ofp_header *request_oh = request->data;
490 ovs_be32 send_xid = request_oh->xid;
491 enum ofpraw request_raw;
492 enum ofpraw reply_raw;
495 ofpraw_decode_partial(&request_raw, request->data, request->size);
496 reply_raw = ofpraw_stats_request_to_reply(request_raw,
497 request_oh->version);
499 send_openflow_buffer(vconn, request);
502 struct ofpbuf *reply;
504 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
505 recv_xid = ((struct ofp_header *) reply->data)->xid;
506 if (send_xid == recv_xid) {
509 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
511 ofpraw_decode(&raw, reply->data);
512 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
514 } else if (raw == reply_raw) {
515 done = !ofpmp_more(reply->data);
517 ovs_fatal(0, "received bad reply: %s",
518 ofp_to_string(reply->data, reply->size,
522 VLOG_DBG("received reply with xid %08"PRIx32" "
523 "!= expected %08"PRIx32, recv_xid, send_xid);
525 ofpbuf_delete(reply);
530 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
532 struct ofpbuf *request;
535 open_vconn(vconn_name, &vconn);
536 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
537 dump_stats_transaction(vconn, request);
541 /* Sends all of the 'requests', which should be requests that only have replies
542 * if an error occurs, and waits for them to succeed or fail. If an error does
543 * occur, prints it and exits with an error.
545 * Destroys all of the 'requests'. */
547 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
549 struct ofpbuf *request, *reply;
551 LIST_FOR_EACH (request, list_node, requests) {
552 ofpmsg_update_length(request);
555 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
556 "talking to %s", vconn_get_name(vconn));
558 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
561 ofpbuf_delete(reply);
564 /* Sends 'request', which should be a request that only has a reply if an error
565 * occurs, and waits for it to succeed or fail. If an error does occur, prints
566 * it and exits with an error.
568 * Destroys 'request'. */
570 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
572 struct list requests;
574 list_init(&requests);
575 list_push_back(&requests, &request->list_node);
576 transact_multiple_noreply(vconn, &requests);
580 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
582 struct ofp_switch_config *config;
583 struct ofpbuf *request;
584 struct ofpbuf *reply;
587 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
588 vconn_get_version(vconn), 0);
589 run(vconn_transact(vconn, request, &reply),
590 "talking to %s", vconn_get_name(vconn));
592 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
593 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
596 config = ofpbuf_pull(reply, sizeof *config);
599 ofpbuf_delete(reply);
603 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
605 struct ofpbuf *request;
607 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
608 ofpbuf_put(request, config, sizeof *config);
610 transact_noreply(vconn, request);
614 ofctl_show(int argc OVS_UNUSED, char *argv[])
616 const char *vconn_name = argv[1];
618 struct ofpbuf *request;
619 struct ofpbuf *reply;
622 open_vconn(vconn_name, &vconn);
623 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
624 vconn_get_version(vconn), 0);
625 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
627 trunc = ofputil_switch_features_ports_trunc(reply);
628 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
630 ofpbuf_delete(reply);
633 /* The Features Reply may not contain all the ports, so send a
634 * Port Description stats request, which doesn't have size
636 dump_trivial_stats_transaction(vconn_name,
637 OFPRAW_OFPST_PORT_DESC_REQUEST);
639 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
644 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
646 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
650 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
652 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
656 fetch_port_by_features(const char *vconn_name,
657 const char *port_name, ofp_port_t port_no,
658 struct ofputil_phy_port *pp, bool *trunc)
660 struct ofputil_switch_features features;
661 const struct ofp_header *oh;
662 struct ofpbuf *request, *reply;
669 /* Fetch the switch's ofp_switch_features. */
670 open_vconn(vconn_name, &vconn);
671 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
672 vconn_get_version(vconn), 0);
673 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
677 if (ofptype_decode(&type, reply->data)
678 || type != OFPTYPE_FEATURES_REPLY) {
679 ovs_fatal(0, "%s: received bad features reply", vconn_name);
683 if (ofputil_switch_features_ports_trunc(reply)) {
688 error = ofputil_decode_switch_features(oh, &features, &b);
690 ovs_fatal(0, "%s: failed to decode features reply (%s)",
691 vconn_name, ofperr_to_string(error));
694 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
695 if (port_no != OFPP_NONE
696 ? port_no == pp->port_no
697 : !strcmp(pp->name, port_name)) {
704 ofpbuf_delete(reply);
709 fetch_port_by_stats(const char *vconn_name,
710 const char *port_name, ofp_port_t port_no,
711 struct ofputil_phy_port *pp)
713 struct ofpbuf *request;
719 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
720 send_xid = ((struct ofp_header *) request->data)->xid;
722 open_vconn(vconn_name, &vconn);
723 send_openflow_buffer(vconn, request);
726 struct ofpbuf *reply;
728 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
729 recv_xid = ((struct ofp_header *) reply->data)->xid;
730 if (send_xid == recv_xid) {
731 struct ofp_header *oh = reply->data;
736 ofpbuf_use_const(&b, oh, ntohs(oh->length));
737 if (ofptype_pull(&type, &b)
738 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
739 ovs_fatal(0, "received bad reply: %s",
740 ofp_to_string(reply->data, reply->size,
744 flags = ofpmp_flags(oh);
745 done = !(flags & OFPSF_REPLY_MORE);
748 /* We've already found the port, but we need to drain
749 * the queue of any other replies for this request. */
753 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
754 if (port_no != OFPP_NONE ? port_no == pp->port_no
755 : !strcmp(pp->name, port_name)) {
761 VLOG_DBG("received reply with xid %08"PRIx32" "
762 "!= expected %08"PRIx32, recv_xid, send_xid);
764 ofpbuf_delete(reply);
772 str_to_ofp(const char *s, ofp_port_t *ofp_port)
777 ret = str_to_uint(s, 10, &port_);
778 *ofp_port = u16_to_ofp(port_);
782 /* Opens a connection to 'vconn_name', fetches the port structure for
783 * 'port_name' (which may be a port name or number), and copies it into
786 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
787 struct ofputil_phy_port *pp)
793 /* Try to interpret the argument as a port number. */
794 if (!str_to_ofp(port_name, &port_no)) {
798 /* Try to find the port based on the Features Reply. If it looks
799 * like the results may be truncated, then use the Port Description
800 * stats message introduced in OVS 1.7. */
801 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
804 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
808 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
812 /* Returns the port number corresponding to 'port_name' (which may be a port
813 * name or number) within the switch 'vconn_name'. */
815 str_to_port_no(const char *vconn_name, const char *port_name)
819 if (ofputil_port_from_string(port_name, &port_no)) {
822 struct ofputil_phy_port pp;
824 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
830 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
831 enum ofputil_protocol *cur)
834 struct ofpbuf *request, *reply;
835 enum ofputil_protocol next;
837 request = ofputil_encode_set_protocol(*cur, want, &next);
842 run(vconn_transact_noreply(vconn, request, &reply),
843 "talking to %s", vconn_get_name(vconn));
845 char *s = ofp_to_string(reply->data, reply->size, 2);
846 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
847 vconn_get_name(vconn), s);
849 ofpbuf_delete(reply);
857 static enum ofputil_protocol
858 set_protocol_for_flow_dump(struct vconn *vconn,
859 enum ofputil_protocol cur_protocol,
860 enum ofputil_protocol usable_protocols)
865 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
866 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
867 if (f & usable_protocols & allowed_protocols
868 && try_set_protocol(vconn, f, &cur_protocol)) {
873 usable_s = ofputil_protocols_to_string(usable_protocols);
874 if (usable_protocols & allowed_protocols) {
875 ovs_fatal(0, "switch does not support any of the usable flow "
876 "formats (%s)", usable_s);
878 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
879 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
880 "allowed flow formats (%s)", usable_s, allowed_s);
884 static struct vconn *
885 prepare_dump_flows(int argc, char *argv[], bool aggregate,
886 struct ofpbuf **requestp)
888 enum ofputil_protocol usable_protocols, protocol;
889 struct ofputil_flow_stats_request fsr;
893 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
894 argc > 2 ? argv[2] : "",
897 ovs_fatal(0, "%s", error);
900 protocol = open_vconn(argv[1], &vconn);
901 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
902 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
907 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
909 struct ofpbuf *request;
912 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
913 dump_stats_transaction(vconn, request);
918 compare_flows(const void *afs_, const void *bfs_)
920 const struct ofputil_flow_stats *afs = afs_;
921 const struct ofputil_flow_stats *bfs = bfs_;
922 const struct match *a = &afs->match;
923 const struct match *b = &bfs->match;
924 const struct sort_criterion *sc;
926 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
927 const struct mf_field *f = sc->field;
931 unsigned int a_pri = afs->priority;
932 unsigned int b_pri = bfs->priority;
933 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
937 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
938 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
940 /* Skip the test for sc->order, so that missing fields always
941 * sort to the end whether we're sorting in ascending or
942 * descending order. */
945 union mf_value aval, bval;
947 mf_get_value(f, &a->flow, &aval);
948 mf_get_value(f, &b->flow, &bval);
949 ret = memcmp(&aval, &bval, f->n_bytes);
954 return sc->order == SORT_ASC ? ret : -ret;
962 ofctl_dump_flows(int argc, char *argv[])
965 return ofctl_dump_flows__(argc, argv, false);
967 struct ofputil_flow_stats *fses;
968 size_t n_fses, allocated_fses;
969 struct ofpbuf *request;
970 struct ofpbuf ofpacts;
971 struct ofpbuf *reply;
977 vconn = prepare_dump_flows(argc, argv, false, &request);
978 send_xid = ((struct ofp_header *) request->data)->xid;
979 send_openflow_buffer(vconn, request);
982 n_fses = allocated_fses = 0;
984 ofpbuf_init(&ofpacts, 0);
986 struct ofputil_flow_stats *fs;
988 if (n_fses >= allocated_fses) {
989 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
993 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
997 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1000 ofpbuf_uninit(&ofpacts);
1002 qsort(fses, n_fses, sizeof *fses, compare_flows);
1005 for (i = 0; i < n_fses; i++) {
1007 ofp_print_flow_stats(&s, &fses[i]);
1012 for (i = 0; i < n_fses; i++) {
1013 free(fses[i].ofpacts);
1022 ofctl_dump_aggregate(int argc, char *argv[])
1024 return ofctl_dump_flows__(argc, argv, true);
1028 ofctl_queue_stats(int argc, char *argv[])
1030 struct ofpbuf *request;
1031 struct vconn *vconn;
1032 struct ofputil_queue_stats_request oqs;
1034 open_vconn(argv[1], &vconn);
1036 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1037 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1039 oqs.port_no = OFPP_ANY;
1041 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1042 oqs.queue_id = atoi(argv[3]);
1044 oqs.queue_id = OFPQ_ALL;
1047 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1048 dump_stats_transaction(vconn, request);
1053 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1055 const char *vconn_name = argv[1];
1056 const char *port_name = argv[2];
1057 enum ofputil_protocol protocol;
1058 enum ofp_version version;
1059 struct ofpbuf *request;
1060 struct vconn *vconn;
1063 port = str_to_port_no(vconn_name, port_name);
1065 protocol = open_vconn(vconn_name, &vconn);
1066 version = ofputil_protocol_to_ofp_version(protocol);
1067 request = ofputil_encode_queue_get_config_request(version, port);
1068 dump_transaction(vconn, request);
1072 static enum ofputil_protocol
1073 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1074 enum ofputil_protocol usable_protocols)
1076 enum ofputil_protocol cur_protocol;
1080 if (!(usable_protocols & allowed_protocols)) {
1081 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1082 usable_s = ofputil_protocols_to_string(usable_protocols);
1083 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1084 "allowed flow formats (%s)", usable_s, allowed_s);
1087 /* If the initial flow format is allowed and usable, keep it. */
1088 cur_protocol = open_vconn(remote, vconnp);
1089 if (usable_protocols & allowed_protocols & cur_protocol) {
1090 return cur_protocol;
1093 /* Otherwise try each flow format in turn. */
1094 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1095 enum ofputil_protocol f = 1 << i;
1097 if (f != cur_protocol
1098 && f & usable_protocols & allowed_protocols
1099 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1104 usable_s = ofputil_protocols_to_string(usable_protocols);
1105 ovs_fatal(0, "switch does not support any of the usable flow "
1106 "formats (%s)", usable_s);
1110 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1111 size_t n_fms, enum ofputil_protocol usable_protocols)
1113 enum ofputil_protocol protocol;
1114 struct vconn *vconn;
1117 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1119 for (i = 0; i < n_fms; i++) {
1120 struct ofputil_flow_mod *fm = &fms[i];
1122 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1129 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1131 enum ofputil_protocol usable_protocols;
1132 struct ofputil_flow_mod *fms = NULL;
1136 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1139 ovs_fatal(0, "%s", error);
1141 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1146 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1148 if (argc > 2 && !strcmp(argv[2], "-")) {
1149 ofctl_flow_mod_file(argc, argv, command);
1151 struct ofputil_flow_mod fm;
1153 enum ofputil_protocol usable_protocols;
1155 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1158 ovs_fatal(0, "%s", error);
1160 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1165 ofctl_add_flow(int argc, char *argv[])
1167 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1171 ofctl_add_flows(int argc, char *argv[])
1173 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1177 ofctl_mod_flows(int argc, char *argv[])
1179 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1183 ofctl_del_flows(int argc, char *argv[])
1185 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1189 set_packet_in_format(struct vconn *vconn,
1190 enum nx_packet_in_format packet_in_format)
1192 struct ofpbuf *spif;
1194 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1196 transact_noreply(vconn, spif);
1197 VLOG_DBG("%s: using user-specified packet in format %s",
1198 vconn_get_name(vconn),
1199 ofputil_packet_in_format_to_string(packet_in_format));
1203 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1205 struct ofp_switch_config config;
1206 enum ofp_config_flags flags;
1208 fetch_switch_config(vconn, &config);
1209 flags = ntohs(config.flags);
1210 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1211 /* Set the invalid ttl config. */
1212 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1214 config.flags = htons(flags);
1215 set_switch_config(vconn, &config);
1217 /* Then retrieve the configuration to see if it really took. OpenFlow
1218 * doesn't define error reporting for bad modes, so this is all we can
1220 fetch_switch_config(vconn, &config);
1221 flags = ntohs(config.flags);
1222 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1223 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1224 "switch probably doesn't support mode)");
1231 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1232 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1233 * an error message and stores NULL in '*msgp'. */
1235 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1237 struct ofp_header *oh;
1240 msg = ofpbuf_new(strlen(hex) / 2);
1243 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1245 return "Trailing garbage in hex data";
1248 if (msg->size < sizeof(struct ofp_header)) {
1250 return "Message too short for OpenFlow";
1254 if (msg->size != ntohs(oh->length)) {
1256 return "Message size does not match length in OpenFlow header";
1264 ofctl_send(struct unixctl_conn *conn, int argc,
1265 const char *argv[], void *vconn_)
1267 struct vconn *vconn = vconn_;
1274 for (i = 1; i < argc; i++) {
1275 const char *error_msg;
1279 error_msg = openflow_from_hex(argv[i], &msg);
1281 ds_put_format(&reply, "%s\n", error_msg);
1286 fprintf(stderr, "send: ");
1287 ofp_print(stderr, msg->data, msg->size, verbosity);
1289 error = vconn_send_block(vconn, msg);
1292 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1295 ds_put_cstr(&reply, "sent\n");
1300 unixctl_command_reply(conn, ds_cstr(&reply));
1302 unixctl_command_reply_error(conn, ds_cstr(&reply));
1307 struct barrier_aux {
1308 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1309 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1313 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1314 const char *argv[] OVS_UNUSED, void *aux_)
1316 struct barrier_aux *aux = aux_;
1321 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1325 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1326 error = vconn_send_block(aux->vconn, msg);
1329 unixctl_command_reply_error(conn, ovs_strerror(error));
1336 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1337 const char *argv[], void *aux OVS_UNUSED)
1341 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1343 unixctl_command_reply_error(conn, ovs_strerror(errno));
1348 dup2(fd, STDERR_FILENO);
1350 unixctl_command_reply(conn, NULL);
1354 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1355 const char *argv[] OVS_UNUSED, void *blocked_)
1357 bool *blocked = blocked_;
1361 unixctl_command_reply(conn, NULL);
1363 unixctl_command_reply(conn, "already blocking");
1368 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1369 const char *argv[] OVS_UNUSED, void *blocked_)
1371 bool *blocked = blocked_;
1375 unixctl_command_reply(conn, NULL);
1377 unixctl_command_reply(conn, "already unblocked");
1381 /* Prints to stdout all of the messages received on 'vconn'.
1383 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1384 * received on 'vconn'. */
1386 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1388 struct barrier_aux barrier_aux = { vconn, NULL };
1389 struct unixctl_server *server;
1390 bool exiting = false;
1391 bool blocked = false;
1394 daemon_save_fd(STDERR_FILENO);
1396 error = unixctl_server_create(NULL, &server);
1398 ovs_fatal(error, "failed to create unixctl server");
1400 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1401 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1403 unixctl_command_register("ofctl/barrier", "", 0, 0,
1404 ofctl_barrier, &barrier_aux);
1405 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1406 ofctl_set_output_file, NULL);
1408 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1409 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1412 daemonize_complete();
1418 unixctl_server_run(server);
1423 retval = vconn_recv(vconn, &b);
1424 if (retval == EAGAIN) {
1427 run(retval, "vconn_recv");
1430 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1431 time_wall_msec(), true);
1436 ofptype_decode(&type, b->data);
1437 ofp_print(stderr, b->data, b->size, verbosity + 2);
1439 switch ((int) type) {
1440 case OFPTYPE_BARRIER_REPLY:
1441 if (barrier_aux.conn) {
1442 unixctl_command_reply(barrier_aux.conn, NULL);
1443 barrier_aux.conn = NULL;
1447 case OFPTYPE_ECHO_REQUEST:
1448 if (reply_to_echo_requests) {
1449 struct ofpbuf *reply;
1451 reply = make_echo_reply(b->data);
1452 retval = vconn_send_block(vconn, reply);
1454 ovs_fatal(retval, "failed to send echo reply");
1467 vconn_run_wait(vconn);
1469 vconn_recv_wait(vconn);
1471 unixctl_server_wait(server);
1475 unixctl_server_destroy(server);
1479 ofctl_monitor(int argc, char *argv[])
1481 struct vconn *vconn;
1483 enum ofputil_protocol usable_protocols;
1485 open_vconn(argv[1], &vconn);
1486 for (i = 2; i < argc; i++) {
1487 const char *arg = argv[i];
1489 if (isdigit((unsigned char) *arg)) {
1490 struct ofp_switch_config config;
1492 fetch_switch_config(vconn, &config);
1493 config.miss_send_len = htons(atoi(arg));
1494 set_switch_config(vconn, &config);
1495 } else if (!strcmp(arg, "invalid_ttl")) {
1496 monitor_set_invalid_ttl_to_controller(vconn);
1497 } else if (!strncmp(arg, "watch:", 6)) {
1498 struct ofputil_flow_monitor_request fmr;
1502 error = parse_flow_monitor_request(&fmr, arg + 6,
1505 ovs_fatal(0, "%s", error);
1508 msg = ofpbuf_new(0);
1509 ofputil_append_flow_monitor_request(&fmr, msg);
1510 dump_stats_transaction(vconn, msg);
1512 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1516 if (preferred_packet_in_format >= 0) {
1517 set_packet_in_format(vconn, preferred_packet_in_format);
1519 enum ofp_version version = vconn_get_version(vconn);
1522 case OFP10_VERSION: {
1523 struct ofpbuf *spif, *reply;
1525 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1527 run(vconn_transact_noreply(vconn, spif, &reply),
1528 "talking to %s", vconn_get_name(vconn));
1530 char *s = ofp_to_string(reply->data, reply->size, 2);
1531 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1532 " replied: %s. Falling back to the switch default.",
1533 vconn_get_name(vconn), s);
1535 ofpbuf_delete(reply);
1548 monitor_vconn(vconn, true);
1552 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1554 struct vconn *vconn;
1556 open_vconn__(argv[1], SNOOP, &vconn);
1557 monitor_vconn(vconn, false);
1561 ofctl_dump_ports(int argc, char *argv[])
1563 struct ofpbuf *request;
1564 struct vconn *vconn;
1567 open_vconn(argv[1], &vconn);
1568 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1569 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1570 dump_stats_transaction(vconn, request);
1575 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1577 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1581 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1583 struct ofpbuf *request;
1584 struct vconn *vconn;
1585 struct ofpbuf *reply;
1587 open_vconn(argv[1], &vconn);
1588 request = make_echo_request(vconn_get_version(vconn));
1589 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1590 if (reply->size != sizeof(struct ofp_header)) {
1591 ovs_fatal(0, "reply does not match request");
1593 ofpbuf_delete(reply);
1598 ofctl_packet_out(int argc, char *argv[])
1600 enum ofputil_protocol protocol;
1601 struct ofputil_packet_out po;
1602 struct ofpbuf ofpacts;
1603 struct vconn *vconn;
1606 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1608 ofpbuf_init(&ofpacts, 64);
1609 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1611 ovs_fatal(0, "%s", error);
1614 po.buffer_id = UINT32_MAX;
1615 po.in_port = str_to_port_no(argv[1], argv[2]);
1616 po.ofpacts = ofpacts.data;
1617 po.ofpacts_len = ofpacts.size;
1619 protocol = open_vconn(argv[1], &vconn);
1620 for (i = 4; i < argc; i++) {
1621 struct ofpbuf *packet, *opo;
1622 const char *error_msg;
1624 error_msg = eth_from_hex(argv[i], &packet);
1626 ovs_fatal(0, "%s", error_msg);
1629 po.packet = packet->data;
1630 po.packet_len = packet->size;
1631 opo = ofputil_encode_packet_out(&po, protocol);
1632 transact_noreply(vconn, opo);
1633 ofpbuf_delete(packet);
1636 ofpbuf_uninit(&ofpacts);
1640 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1642 struct ofp_config_flag {
1643 const char *name; /* The flag's name. */
1644 enum ofputil_port_config bit; /* Bit to turn on or off. */
1645 bool on; /* Value to set the bit to. */
1647 static const struct ofp_config_flag flags[] = {
1648 { "up", OFPUTIL_PC_PORT_DOWN, false },
1649 { "down", OFPUTIL_PC_PORT_DOWN, true },
1650 { "stp", OFPUTIL_PC_NO_STP, false },
1651 { "receive", OFPUTIL_PC_NO_RECV, false },
1652 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1653 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1654 { "forward", OFPUTIL_PC_NO_FWD, false },
1655 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1658 const struct ofp_config_flag *flag;
1659 enum ofputil_protocol protocol;
1660 struct ofputil_port_mod pm;
1661 struct ofputil_phy_port pp;
1662 struct vconn *vconn;
1663 const char *command;
1666 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1668 pm.port_no = pp.port_no;
1669 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1674 if (!strncasecmp(argv[3], "no-", 3)) {
1675 command = argv[3] + 3;
1677 } else if (!strncasecmp(argv[3], "no", 2)) {
1678 command = argv[3] + 2;
1684 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1685 if (!strcasecmp(command, flag->name)) {
1686 pm.mask = flag->bit;
1687 pm.config = flag->on ^ not ? flag->bit : 0;
1691 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1694 protocol = open_vconn(argv[1], &vconn);
1695 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1700 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1702 enum ofputil_protocol protocol, usable_protocols;
1703 struct ofputil_table_mod tm;
1704 struct vconn *vconn;
1708 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1710 ovs_fatal(0, "%s", error);
1713 protocol = open_vconn(argv[1], &vconn);
1714 if (!(protocol & usable_protocols)) {
1715 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1716 enum ofputil_protocol f = 1 << i;
1718 && f & usable_protocols
1719 && try_set_protocol(vconn, f, &protocol)) {
1726 if (!(protocol & usable_protocols)) {
1727 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1728 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1731 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1736 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1738 struct ofp_switch_config config;
1739 struct vconn *vconn;
1741 open_vconn(argv[1], &vconn);
1742 fetch_switch_config(vconn, &config);
1743 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1748 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1750 struct ofp_switch_config config;
1751 enum ofp_config_flags mode;
1752 struct vconn *vconn;
1755 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1756 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1759 open_vconn(argv[1], &vconn);
1760 fetch_switch_config(vconn, &config);
1761 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1762 if (flags != config.flags) {
1763 /* Set the configuration. */
1764 config.flags = flags;
1765 set_switch_config(vconn, &config);
1767 /* Then retrieve the configuration to see if it really took. OpenFlow
1768 * doesn't define error reporting for bad modes, so this is all we can
1770 fetch_switch_config(vconn, &config);
1771 if (flags != config.flags) {
1772 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1773 "switch probably doesn't support mode \"%s\")",
1774 argv[1], ofputil_frag_handling_to_string(mode));
1781 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1783 const char *filename = argv[1];
1787 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1789 ovs_fatal(errno, "%s: open", filename);
1792 ofpbuf_init(&b, 65536);
1794 struct ofp_header *oh;
1795 size_t length, tail_len;
1800 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1801 n = fread(oh, 1, sizeof *oh, file);
1804 } else if (n < sizeof *oh) {
1805 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1808 length = ntohs(oh->length);
1809 if (length < sizeof *oh) {
1810 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1814 tail_len = length - sizeof *oh;
1815 tail = ofpbuf_put_uninit(&b, tail_len);
1816 n = fread(tail, 1, tail_len, file);
1818 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1821 ofp_print(stdout, b.data, b.size, verbosity + 2);
1825 if (file != stdin) {
1831 is_openflow_port(ovs_be16 port_, char *ports[])
1833 uint16_t port = ntohs(port_);
1837 for (i = 0; ports[i]; i++) {
1838 if (port == atoi(ports[i])) {
1844 return port == OFP_PORT || port == OFP_OLD_PORT;
1849 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1851 struct tcp_reader *reader;
1856 file = ovs_pcap_open(argv[1], "rb");
1858 ovs_fatal(errno, "%s: open failed", argv[1]);
1861 reader = tcp_reader_open();
1864 struct ofpbuf *packet;
1867 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1869 error = ovs_pcap_read(file, &packet, &when);
1873 flow_extract(packet, &md, &flow);
1874 if (flow.dl_type == htons(ETH_TYPE_IP)
1875 && flow.nw_proto == IPPROTO_TCP
1876 && (is_openflow_port(flow.tp_src, argv + 2) ||
1877 is_openflow_port(flow.tp_dst, argv + 2))) {
1878 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1880 while (payload->size >= sizeof(struct ofp_header)) {
1881 const struct ofp_header *oh;
1884 /* Align OpenFlow on 8-byte boundary for safe access. */
1885 ofpbuf_shift(payload, -((intptr_t) payload->data & 7));
1888 length = ntohs(oh->length);
1889 if (payload->size < length) {
1899 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1904 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1905 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1906 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1907 ofp_print(stdout, payload->data, length, verbosity + 1);
1908 ofpbuf_pull(payload, length);
1912 ofpbuf_delete(packet);
1914 tcp_reader_close(reader);
1918 ofctl_ping(int argc, char *argv[])
1920 size_t max_payload = 65535 - sizeof(struct ofp_header);
1921 unsigned int payload;
1922 struct vconn *vconn;
1925 payload = argc > 2 ? atoi(argv[2]) : 64;
1926 if (payload > max_payload) {
1927 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1930 open_vconn(argv[1], &vconn);
1931 for (i = 0; i < 10; i++) {
1932 struct timeval start, end;
1933 struct ofpbuf *request, *reply;
1934 const struct ofp_header *rpy_hdr;
1937 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1938 vconn_get_version(vconn), payload);
1939 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1941 xgettimeofday(&start);
1942 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1943 xgettimeofday(&end);
1945 rpy_hdr = reply->data;
1946 if (ofptype_pull(&type, reply)
1947 || type != OFPTYPE_ECHO_REPLY
1948 || reply->size != payload
1949 || memcmp(request->l3, reply->l3, payload)) {
1950 printf("Reply does not match request. Request:\n");
1951 ofp_print(stdout, request, request->size, verbosity + 2);
1953 ofp_print(stdout, reply, reply->size, verbosity + 2);
1955 printf("%"PRIuSIZE" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1956 reply->size, argv[1], ntohl(rpy_hdr->xid),
1957 (1000*(double)(end.tv_sec - start.tv_sec))
1958 + (.001*(end.tv_usec - start.tv_usec)));
1959 ofpbuf_delete(request);
1960 ofpbuf_delete(reply);
1966 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1968 size_t max_payload = 65535 - sizeof(struct ofp_header);
1969 struct timeval start, end;
1970 unsigned int payload_size, message_size;
1971 struct vconn *vconn;
1976 payload_size = atoi(argv[2]);
1977 if (payload_size > max_payload) {
1978 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1980 message_size = sizeof(struct ofp_header) + payload_size;
1982 count = atoi(argv[3]);
1984 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1985 count, message_size, count * message_size);
1987 open_vconn(argv[1], &vconn);
1988 xgettimeofday(&start);
1989 for (i = 0; i < count; i++) {
1990 struct ofpbuf *request, *reply;
1992 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1993 vconn_get_version(vconn), payload_size);
1994 ofpbuf_put_zeros(request, payload_size);
1995 run(vconn_transact(vconn, request, &reply), "transact");
1996 ofpbuf_delete(reply);
1998 xgettimeofday(&end);
2001 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2002 + (.001*(end.tv_usec - start.tv_usec)));
2003 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2004 duration, count / (duration / 1000.0),
2005 count * message_size / (duration / 1000.0));
2009 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2012 struct ofputil_group_mod *gm;
2013 struct ofpbuf *request;
2015 struct vconn *vconn;
2018 open_vconn(remote, &vconn);
2020 for (i = 0; i < n_gms; i++) {
2022 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2024 transact_noreply(vconn, request);
2034 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2036 struct ofputil_group_mod *gms = NULL;
2037 enum ofputil_protocol usable_protocols;
2041 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2044 ovs_fatal(0, "%s", error);
2046 ofctl_group_mod__(argv[1], gms, n_gms);
2051 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2053 if (argc > 2 && !strcmp(argv[2], "-")) {
2054 ofctl_group_mod_file(argc, argv, command);
2056 enum ofputil_protocol usable_protocols;
2057 struct ofputil_group_mod gm;
2060 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2063 ovs_fatal(0, "%s", error);
2065 ofctl_group_mod__(argv[1], &gm, 1);
2070 ofctl_add_group(int argc, char *argv[])
2072 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2076 ofctl_add_groups(int argc, char *argv[])
2078 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2082 ofctl_mod_group(int argc, char *argv[])
2084 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2088 ofctl_del_groups(int argc, char *argv[])
2090 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2094 ofctl_dump_group_stats(int argc, char *argv[])
2096 enum ofputil_protocol usable_protocols;
2097 struct ofputil_group_mod gm;
2098 struct ofpbuf *request;
2099 struct vconn *vconn;
2103 memset(&gm, 0, sizeof gm);
2105 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2106 argc > 2 ? argv[2] : "",
2109 ovs_fatal(0, "%s", error);
2112 group_id = gm.group_id;
2114 open_vconn(argv[1], &vconn);
2115 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2118 dump_stats_transaction(vconn, request);
2125 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2127 struct ofpbuf *request;
2128 struct vconn *vconn;
2130 open_vconn(argv[1], &vconn);
2132 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2134 dump_stats_transaction(vconn, request);
2141 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2143 struct ofpbuf *request;
2144 struct vconn *vconn;
2146 open_vconn(argv[1], &vconn);
2147 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2149 dump_stats_transaction(vconn, request);
2156 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2161 /* replace-flows and diff-flows commands. */
2163 /* A flow table entry, possibly with two different versions. */
2165 struct cls_rule rule; /* Within a "struct classifier". */
2166 struct fte_version *versions[2];
2169 /* One version of a Flow Table Entry. */
2170 struct fte_version {
2172 uint16_t idle_timeout;
2173 uint16_t hard_timeout;
2175 struct ofpact *ofpacts;
2179 /* Frees 'version' and the data that it owns. */
2181 fte_version_free(struct fte_version *version)
2184 free(version->ofpacts);
2189 /* Returns true if 'a' and 'b' are the same, false if they differ.
2191 * Ignores differences in 'flags' because there's no way to retrieve flags from
2192 * an OpenFlow switch. We have to assume that they are the same. */
2194 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2196 return (a->cookie == b->cookie
2197 && a->idle_timeout == b->idle_timeout
2198 && a->hard_timeout == b->hard_timeout
2199 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2200 b->ofpacts, b->ofpacts_len));
2203 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2204 * 'index' into 's', followed by a new-line. */
2206 fte_version_format(const struct fte *fte, int index, struct ds *s)
2208 const struct fte_version *version = fte->versions[index];
2215 cls_rule_format(&fte->rule, s);
2216 if (version->cookie != htonll(0)) {
2217 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2219 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2220 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2222 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2223 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2226 ds_put_cstr(s, " actions=");
2227 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2229 ds_put_char(s, '\n');
2233 fte_from_cls_rule(const struct cls_rule *cls_rule)
2235 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2238 /* Frees 'fte' and its versions. */
2240 fte_free(struct fte *fte)
2243 fte_version_free(fte->versions[0]);
2244 fte_version_free(fte->versions[1]);
2245 cls_rule_destroy(&fte->rule);
2250 /* Frees all of the FTEs within 'cls'. */
2252 fte_free_all(struct classifier *cls)
2254 struct cls_cursor cursor;
2255 struct fte *fte, *next;
2257 fat_rwlock_wrlock(&cls->rwlock);
2258 cls_cursor_init(&cursor, cls, NULL);
2259 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2260 classifier_remove(cls, &fte->rule);
2263 fat_rwlock_unlock(&cls->rwlock);
2264 classifier_destroy(cls);
2267 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2268 * necessary. Sets 'version' as the version of that rule with the given
2269 * 'index', replacing any existing version, if any.
2271 * Takes ownership of 'version'. */
2273 fte_insert(struct classifier *cls, const struct match *match,
2274 unsigned int priority, struct fte_version *version, int index)
2276 struct fte *old, *fte;
2278 fte = xzalloc(sizeof *fte);
2279 cls_rule_init(&fte->rule, match, priority);
2280 fte->versions[index] = version;
2282 fat_rwlock_wrlock(&cls->rwlock);
2283 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2284 fat_rwlock_unlock(&cls->rwlock);
2286 fte_version_free(old->versions[index]);
2287 fte->versions[!index] = old->versions[!index];
2288 cls_rule_destroy(&old->rule);
2293 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2294 * with the specified 'index'. Returns the flow formats able to represent the
2295 * flows that were read. */
2296 static enum ofputil_protocol
2297 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2299 enum ofputil_protocol usable_protocols;
2304 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2306 ovs_fatal(errno, "%s: open", filename);
2310 usable_protocols = OFPUTIL_P_ANY;
2312 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2313 struct fte_version *version;
2314 struct ofputil_flow_mod fm;
2316 enum ofputil_protocol usable;
2318 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2320 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2322 usable_protocols &= usable;
2324 version = xmalloc(sizeof *version);
2325 version->cookie = fm.new_cookie;
2326 version->idle_timeout = fm.idle_timeout;
2327 version->hard_timeout = fm.hard_timeout;
2328 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2329 | OFPUTIL_FF_EMERG);
2330 version->ofpacts = fm.ofpacts;
2331 version->ofpacts_len = fm.ofpacts_len;
2333 fte_insert(cls, &fm.match, fm.priority, version, index);
2337 if (file != stdin) {
2341 return usable_protocols;
2345 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2346 struct ofpbuf **replyp,
2347 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2349 struct ofpbuf *reply = *replyp;
2355 /* Get a flow stats reply message, if we don't already have one. */
2361 run(vconn_recv_block(vconn, &reply),
2362 "OpenFlow packet receive failed");
2363 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2365 error = ofptype_decode(&type, reply->data);
2366 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2367 ovs_fatal(0, "received bad reply: %s",
2368 ofp_to_string(reply->data, reply->size,
2373 /* Pull an individual flow stats reply out of the message. */
2374 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2381 more = ofpmp_more(reply->l2);
2382 ofpbuf_delete(reply);
2391 ovs_fatal(0, "parse error in reply (%s)",
2392 ofperr_to_string(retval));
2397 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2398 * format 'protocol', and adds them as flow table entries in 'cls' for the
2399 * version with the specified 'index'. */
2401 read_flows_from_switch(struct vconn *vconn,
2402 enum ofputil_protocol protocol,
2403 struct classifier *cls, int index)
2405 struct ofputil_flow_stats_request fsr;
2406 struct ofputil_flow_stats fs;
2407 struct ofpbuf *request;
2408 struct ofpbuf ofpacts;
2409 struct ofpbuf *reply;
2412 fsr.aggregate = false;
2413 match_init_catchall(&fsr.match);
2414 fsr.out_port = OFPP_ANY;
2415 fsr.table_id = 0xff;
2416 fsr.cookie = fsr.cookie_mask = htonll(0);
2417 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2418 send_xid = ((struct ofp_header *) request->data)->xid;
2419 send_openflow_buffer(vconn, request);
2422 ofpbuf_init(&ofpacts, 0);
2423 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2424 struct fte_version *version;
2426 version = xmalloc(sizeof *version);
2427 version->cookie = fs.cookie;
2428 version->idle_timeout = fs.idle_timeout;
2429 version->hard_timeout = fs.hard_timeout;
2431 version->ofpacts_len = fs.ofpacts_len;
2432 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2434 fte_insert(cls, &fs.match, fs.priority, version, index);
2436 ofpbuf_uninit(&ofpacts);
2440 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2441 enum ofputil_protocol protocol, struct list *packets)
2443 const struct fte_version *version = fte->versions[index];
2444 struct ofputil_flow_mod fm;
2447 minimatch_expand(&fte->rule.match, &fm.match);
2448 fm.priority = fte->rule.priority;
2449 fm.cookie = htonll(0);
2450 fm.cookie_mask = htonll(0);
2451 fm.new_cookie = version->cookie;
2452 fm.modify_cookie = true;
2454 fm.command = command;
2455 fm.idle_timeout = version->idle_timeout;
2456 fm.hard_timeout = version->hard_timeout;
2457 fm.buffer_id = UINT32_MAX;
2458 fm.out_port = OFPP_ANY;
2459 fm.flags = version->flags;
2460 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2461 command == OFPFC_MODIFY_STRICT) {
2462 fm.ofpacts = version->ofpacts;
2463 fm.ofpacts_len = version->ofpacts_len;
2469 ofm = ofputil_encode_flow_mod(&fm, protocol);
2470 list_push_back(packets, &ofm->list_node);
2474 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2476 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2477 enum ofputil_protocol usable_protocols, protocol;
2478 struct cls_cursor cursor;
2479 struct classifier cls;
2480 struct list requests;
2481 struct vconn *vconn;
2484 classifier_init(&cls, NULL);
2485 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2487 protocol = open_vconn(argv[1], &vconn);
2488 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2490 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2492 list_init(&requests);
2494 /* Delete flows that exist on the switch but not in the file. */
2495 fat_rwlock_rdlock(&cls.rwlock);
2496 cls_cursor_init(&cursor, &cls, NULL);
2497 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2498 struct fte_version *file_ver = fte->versions[FILE_IDX];
2499 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2501 if (sw_ver && !file_ver) {
2502 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2503 protocol, &requests);
2507 /* Add flows that exist in the file but not on the switch.
2508 * Update flows that exist in both places but differ. */
2509 cls_cursor_init(&cursor, &cls, NULL);
2510 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2511 struct fte_version *file_ver = fte->versions[FILE_IDX];
2512 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2515 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2516 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2519 fat_rwlock_unlock(&cls.rwlock);
2520 transact_multiple_noreply(vconn, &requests);
2527 read_flows_from_source(const char *source, struct classifier *cls, int index)
2531 if (source[0] == '/' || source[0] == '.'
2532 || (!strchr(source, ':') && !stat(source, &s))) {
2533 read_flows_from_file(source, cls, index);
2535 enum ofputil_protocol protocol;
2536 struct vconn *vconn;
2538 protocol = open_vconn(source, &vconn);
2539 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2540 read_flows_from_switch(vconn, protocol, cls, index);
2546 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2548 bool differences = false;
2549 struct cls_cursor cursor;
2550 struct classifier cls;
2554 classifier_init(&cls, NULL);
2555 read_flows_from_source(argv[1], &cls, 0);
2556 read_flows_from_source(argv[2], &cls, 1);
2561 fat_rwlock_rdlock(&cls.rwlock);
2562 cls_cursor_init(&cursor, &cls, NULL);
2563 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2564 struct fte_version *a = fte->versions[0];
2565 struct fte_version *b = fte->versions[1];
2567 if (!a || !b || !fte_version_equals(a, b)) {
2568 fte_version_format(fte, 0, &a_s);
2569 fte_version_format(fte, 1, &b_s);
2570 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2572 printf("-%s", ds_cstr(&a_s));
2575 printf("+%s", ds_cstr(&b_s));
2581 fat_rwlock_unlock(&cls.rwlock);
2594 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2596 struct ofputil_meter_mod mm;
2597 struct vconn *vconn;
2598 enum ofputil_protocol protocol;
2599 enum ofputil_protocol usable_protocols;
2600 enum ofp_version version;
2604 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2606 ovs_fatal(0, "%s", error);
2609 usable_protocols = OFPUTIL_P_OF13_UP;
2610 mm.command = command;
2611 mm.meter.meter_id = OFPM13_ALL;
2614 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2615 version = ofputil_protocol_to_ofp_version(protocol);
2616 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2621 ofctl_meter_request__(const char *bridge, const char *str,
2622 enum ofputil_meter_request_type type)
2624 struct ofputil_meter_mod mm;
2625 struct vconn *vconn;
2626 enum ofputil_protocol usable_protocols;
2627 enum ofputil_protocol protocol;
2628 enum ofp_version version;
2632 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2634 ovs_fatal(0, "%s", error);
2637 usable_protocols = OFPUTIL_P_OF13_UP;
2638 mm.meter.meter_id = OFPM13_ALL;
2641 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2642 version = ofputil_protocol_to_ofp_version(protocol);
2643 transact_noreply(vconn, ofputil_encode_meter_request(version,
2645 mm.meter.meter_id));
2651 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2653 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2657 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2659 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2663 ofctl_del_meters(int argc, char *argv[])
2665 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2669 ofctl_dump_meters(int argc, char *argv[])
2671 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2672 OFPUTIL_METER_CONFIG);
2676 ofctl_meter_stats(int argc, char *argv[])
2678 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2679 OFPUTIL_METER_STATS);
2683 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2685 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2689 /* Undocumented commands for unit testing. */
2692 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2693 enum ofputil_protocol usable_protocols)
2695 enum ofputil_protocol protocol = 0;
2699 usable_s = ofputil_protocols_to_string(usable_protocols);
2700 printf("usable protocols: %s\n", usable_s);
2703 if (!(usable_protocols & allowed_protocols)) {
2704 ovs_fatal(0, "no usable protocol");
2706 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2708 if (protocol & usable_protocols & allowed_protocols) {
2712 ovs_assert(is_pow2(protocol));
2714 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2716 for (i = 0; i < n_fms; i++) {
2717 struct ofputil_flow_mod *fm = &fms[i];
2720 msg = ofputil_encode_flow_mod(fm, protocol);
2721 ofp_print(stdout, msg->data, msg->size, verbosity);
2728 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2729 * it back to stdout. */
2731 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2733 enum ofputil_protocol usable_protocols;
2734 struct ofputil_flow_mod fm;
2737 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2739 ovs_fatal(0, "%s", error);
2741 ofctl_parse_flows__(&fm, 1, usable_protocols);
2744 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2745 * add-flows) and prints each of the flows back to stdout. */
2747 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2749 enum ofputil_protocol usable_protocols;
2750 struct ofputil_flow_mod *fms = NULL;
2754 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2757 ovs_fatal(0, "%s", error);
2759 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2764 ofctl_parse_nxm__(bool oxm)
2769 while (!ds_get_test_line(&in, stdin)) {
2770 struct ofpbuf nx_match;
2772 ovs_be64 cookie, cookie_mask;
2776 /* Convert string to nx_match. */
2777 ofpbuf_init(&nx_match, 0);
2779 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2781 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2784 /* Convert nx_match to match. */
2787 error = oxm_pull_match(&nx_match, &match);
2789 error = nx_pull_match(&nx_match, match_len, &match,
2790 &cookie, &cookie_mask);
2794 error = oxm_pull_match_loose(&nx_match, &match);
2796 error = nx_pull_match_loose(&nx_match, match_len, &match,
2797 &cookie, &cookie_mask);
2805 /* Convert match back to nx_match. */
2806 ofpbuf_uninit(&nx_match);
2807 ofpbuf_init(&nx_match, 0);
2809 match_len = oxm_put_match(&nx_match, &match);
2810 out = oxm_match_to_string(&nx_match, match_len);
2812 match_len = nx_put_match(&nx_match, &match,
2813 cookie, cookie_mask);
2814 out = nx_match_to_string(nx_match.data, match_len);
2820 printf("nx_pull_match() returned error %s\n",
2821 ofperr_get_name(error));
2824 ofpbuf_uninit(&nx_match);
2829 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2830 * stdin, does some internal fussing with them, and then prints them back as
2831 * strings on stdout. */
2833 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2835 return ofctl_parse_nxm__(false);
2838 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2839 * stdin, does some internal fussing with them, and then prints them back as
2840 * strings on stdout. */
2842 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2844 return ofctl_parse_nxm__(true);
2848 print_differences(const char *prefix,
2849 const void *a_, size_t a_len,
2850 const void *b_, size_t b_len)
2852 const uint8_t *a = a_;
2853 const uint8_t *b = b_;
2856 for (i = 0; i < MIN(a_len, b_len); i++) {
2858 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2859 prefix, i, a[i], b[i]);
2862 for (i = a_len; i < b_len; i++) {
2863 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2865 for (i = b_len; i < a_len; i++) {
2866 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2870 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2871 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2872 * on stdout, and then converts them back to hex bytes and prints any
2873 * differences from the input. */
2875 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2880 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2881 struct ofpbuf of10_out;
2882 struct ofpbuf of10_in;
2883 struct ofpbuf ofpacts;
2888 /* Parse hex bytes. */
2889 ofpbuf_init(&of10_in, 0);
2890 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2891 ovs_fatal(0, "Trailing garbage in hex data");
2894 /* Convert to ofpacts. */
2895 ofpbuf_init(&ofpacts, 0);
2896 size = of10_in.size;
2897 error = ofpacts_pull_openflow_actions(&of10_in, of10_in.size,
2898 OFP10_VERSION, &ofpacts);
2900 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2901 ofpbuf_uninit(&ofpacts);
2902 ofpbuf_uninit(&of10_in);
2905 ofpbuf_push_uninit(&of10_in, size);
2907 /* Print cls_rule. */
2909 ds_put_cstr(&s, "actions=");
2910 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2914 /* Convert back to ofp10 actions and print differences from input. */
2915 ofpbuf_init(&of10_out, 0);
2916 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of10_out,
2919 print_differences("", of10_in.data, of10_in.size,
2920 of10_out.data, of10_out.size);
2923 ofpbuf_uninit(&ofpacts);
2924 ofpbuf_uninit(&of10_in);
2925 ofpbuf_uninit(&of10_out);
2930 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2931 * bytes from stdin, converts them to cls_rules, prints them as strings on
2932 * stdout, and then converts them back to hex bytes and prints any differences
2935 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2936 * values are ignored in the input and will be set to zero when OVS converts
2937 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2938 * it does the conversion to hex, to ensure that in fact they are ignored. */
2940 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2947 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2948 struct ofpbuf match_in, match_expout;
2949 struct ofp10_match match_out;
2950 struct ofp10_match match_normal;
2954 /* Parse hex bytes to use for expected output. */
2956 ds_put_cstr(&expout, ds_cstr(&in));
2957 for (p = ds_cstr(&expout); *p; p++) {
2962 ofpbuf_init(&match_expout, 0);
2963 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2964 ovs_fatal(0, "Trailing garbage in hex data");
2966 if (match_expout.size != sizeof(struct ofp10_match)) {
2967 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2968 match_expout.size, sizeof(struct ofp10_match));
2971 /* Parse hex bytes for input. */
2972 for (p = ds_cstr(&in); *p; p++) {
2974 *p = "0123456789abcdef"[random_uint32() & 0xf];
2977 ofpbuf_init(&match_in, 0);
2978 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2979 ovs_fatal(0, "Trailing garbage in hex data");
2981 if (match_in.size != sizeof(struct ofp10_match)) {
2982 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2983 match_in.size, sizeof(struct ofp10_match));
2986 /* Convert to cls_rule and print. */
2987 ofputil_match_from_ofp10_match(match_in.data, &match);
2988 match_print(&match);
2990 /* Convert back to ofp10_match and print differences from input. */
2991 ofputil_match_to_ofp10_match(&match, &match_out);
2992 print_differences("", match_expout.data, match_expout.size,
2993 &match_out, sizeof match_out);
2995 /* Normalize, then convert and compare again. */
2996 ofputil_normalize_match(&match);
2997 ofputil_match_to_ofp10_match(&match, &match_normal);
2998 print_differences("normal: ", &match_out, sizeof match_out,
2999 &match_normal, sizeof match_normal);
3002 ofpbuf_uninit(&match_in);
3003 ofpbuf_uninit(&match_expout);
3006 ds_destroy(&expout);
3009 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3010 * bytes from stdin, converts them to "struct match"es, prints them as strings
3011 * on stdout, and then converts them back to hex bytes and prints any
3012 * differences from the input. */
3014 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3019 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3020 struct ofpbuf match_in;
3021 struct ofp11_match match_out;
3025 /* Parse hex bytes. */
3026 ofpbuf_init(&match_in, 0);
3027 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3028 ovs_fatal(0, "Trailing garbage in hex data");
3030 if (match_in.size != sizeof(struct ofp11_match)) {
3031 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
3032 match_in.size, sizeof(struct ofp11_match));
3035 /* Convert to match. */
3036 error = ofputil_match_from_ofp11_match(match_in.data, &match);
3038 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3039 ofpbuf_uninit(&match_in);
3044 match_print(&match);
3046 /* Convert back to ofp11_match and print differences from input. */
3047 ofputil_match_to_ofp11_match(&match, &match_out);
3049 print_differences("", match_in.data, match_in.size,
3050 &match_out, sizeof match_out);
3053 ofpbuf_uninit(&match_in);
3058 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3059 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3060 * on stdout, and then converts them back to hex bytes and prints any
3061 * differences from the input. */
3063 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3068 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3069 struct ofpbuf of11_out;
3070 struct ofpbuf of11_in;
3071 struct ofpbuf ofpacts;
3076 /* Parse hex bytes. */
3077 ofpbuf_init(&of11_in, 0);
3078 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3079 ovs_fatal(0, "Trailing garbage in hex data");
3082 /* Convert to ofpacts. */
3083 ofpbuf_init(&ofpacts, 0);
3084 size = of11_in.size;
3085 error = ofpacts_pull_openflow_actions(&of11_in, of11_in.size,
3086 OFP11_VERSION, &ofpacts);
3088 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3089 ofpbuf_uninit(&ofpacts);
3090 ofpbuf_uninit(&of11_in);
3093 ofpbuf_push_uninit(&of11_in, size);
3095 /* Print cls_rule. */
3097 ds_put_cstr(&s, "actions=");
3098 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3102 /* Convert back to ofp11 actions and print differences from input. */
3103 ofpbuf_init(&of11_out, 0);
3104 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of11_out,
3107 print_differences("", of11_in.data, of11_in.size,
3108 of11_out.data, of11_out.size);
3111 ofpbuf_uninit(&ofpacts);
3112 ofpbuf_uninit(&of11_in);
3113 ofpbuf_uninit(&of11_out);
3118 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3119 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3120 * them as strings on stdout, and then converts them back to hex bytes and
3121 * prints any differences from the input. */
3123 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3128 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3129 struct ofpbuf of11_out;
3130 struct ofpbuf of11_in;
3131 struct ofpbuf ofpacts;
3135 const char *table_id;
3138 /* Parse table_id separated with the follow-up instructions by ",", if
3140 instructions = ds_cstr(&in);
3142 if (strstr(instructions, ",")) {
3143 table_id = strsep(&instructions, ",");
3146 /* Parse hex bytes. */
3147 ofpbuf_init(&of11_in, 0);
3148 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3149 ovs_fatal(0, "Trailing garbage in hex data");
3152 /* Convert to ofpacts. */
3153 ofpbuf_init(&ofpacts, 0);
3154 size = of11_in.size;
3155 error = ofpacts_pull_openflow_instructions(&of11_in, of11_in.size,
3156 OFP11_VERSION, &ofpacts);
3158 /* Verify actions, enforce consistency. */
3160 memset(&flow, 0, sizeof flow);
3161 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3163 table_id ? atoi(table_id) : 0,
3164 255, OFPUTIL_P_OF11_STD);
3167 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3168 ofpbuf_uninit(&ofpacts);
3169 ofpbuf_uninit(&of11_in);
3172 ofpbuf_push_uninit(&of11_in, size);
3174 /* Print cls_rule. */
3176 ds_put_cstr(&s, "actions=");
3177 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3181 /* Convert back to ofp11 instructions and print differences from
3183 ofpbuf_init(&of11_out, 0);
3184 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3185 &of11_out, OFP13_VERSION);
3187 print_differences("", of11_in.data, of11_in.size,
3188 of11_out.data, of11_out.size);
3191 ofpbuf_uninit(&ofpacts);
3192 ofpbuf_uninit(&of11_in);
3193 ofpbuf_uninit(&of11_out);
3198 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3200 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3204 pcap = ovs_pcap_open(argv[1], "rb");
3206 ovs_fatal(errno, "%s: open failed", argv[1]);
3210 struct ofpbuf *packet;
3212 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3215 error = ovs_pcap_read(pcap, &packet, NULL);
3219 ovs_fatal(error, "%s: read failed", argv[1]);
3222 flow_extract(packet, &md, &flow);
3223 flow_print(stdout, &flow);
3225 ofpbuf_delete(packet);
3229 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3230 * mask values to and from various formats and prints the results. */
3232 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3237 struct ofputil_flow_mod fm;
3240 struct match nxm_match;
3244 struct ofp10_match of10_raw;
3245 struct match of10_match;
3247 struct ofp11_match of11_raw;
3248 struct match of11_match;
3253 enum ofputil_protocol usable_protocols; /* Unused for now. */
3255 match_init_catchall(&match);
3256 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3257 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3259 /* Convert to and from string. */
3260 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3261 printf("%s -> ", string_s);
3263 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3265 ovs_fatal(0, "%s", error_s);
3267 printf("%04"PRIx16"/%04"PRIx16"\n",
3268 ntohs(fm.match.flow.vlan_tci),
3269 ntohs(fm.match.wc.masks.vlan_tci));
3272 /* Convert to and from NXM. */
3273 ofpbuf_init(&nxm, 0);
3274 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3275 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3276 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3277 printf("NXM: %s -> ", nxm_s);
3279 printf("%s\n", ofperr_to_string(error));
3281 printf("%04"PRIx16"/%04"PRIx16"\n",
3282 ntohs(nxm_match.flow.vlan_tci),
3283 ntohs(nxm_match.wc.masks.vlan_tci));
3286 ofpbuf_uninit(&nxm);
3288 /* Convert to and from OXM. */
3289 ofpbuf_init(&nxm, 0);
3290 nxm_match_len = oxm_put_match(&nxm, &match);
3291 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3292 error = oxm_pull_match(&nxm, &nxm_match);
3293 printf("OXM: %s -> ", nxm_s);
3295 printf("%s\n", ofperr_to_string(error));
3297 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3298 (VLAN_VID_MASK | VLAN_CFI);
3299 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3300 (VLAN_VID_MASK | VLAN_CFI);
3302 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3303 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3304 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3310 ofpbuf_uninit(&nxm);
3312 /* Convert to and from OpenFlow 1.0. */
3313 ofputil_match_to_ofp10_match(&match, &of10_raw);
3314 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3315 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3316 ntohs(of10_raw.dl_vlan),
3317 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3318 of10_raw.dl_vlan_pcp,
3319 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3320 ntohs(of10_match.flow.vlan_tci),
3321 ntohs(of10_match.wc.masks.vlan_tci));
3323 /* Convert to and from OpenFlow 1.1. */
3324 ofputil_match_to_ofp11_match(&match, &of11_raw);
3325 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3326 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3327 ntohs(of11_raw.dl_vlan),
3328 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3329 of11_raw.dl_vlan_pcp,
3330 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3331 ntohs(of11_match.flow.vlan_tci),
3332 ntohs(of11_match.wc.masks.vlan_tci));
3335 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3338 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3343 error = ofperr_from_name(argv[1]);
3345 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3348 for (version = 0; version <= UINT8_MAX; version++) {
3349 const char *name = ofperr_domain_get_name(version);
3351 int vendor = ofperr_get_vendor(error, version);
3352 int type = ofperr_get_type(error, version);
3353 int code = ofperr_get_code(error, version);
3355 if (vendor != -1 || type != -1 || code != -1) {
3356 printf("%s: vendor %#x, type %d, code %d\n",
3357 name, vendor, type, code);
3363 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3364 * error named ENUM and prints the error reply in hex. */
3366 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3368 const struct ofp_header *oh;
3369 struct ofpbuf request, *reply;
3372 error = ofperr_from_name(argv[1]);
3374 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3377 ofpbuf_init(&request, 0);
3378 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3379 ovs_fatal(0, "Trailing garbage in hex data");
3381 if (request.size < sizeof(struct ofp_header)) {
3382 ovs_fatal(0, "Request too short");
3386 if (request.size != ntohs(oh->length)) {
3387 ovs_fatal(0, "Request size inconsistent");
3390 reply = ofperr_encode_reply(error, request.data);
3391 ofpbuf_uninit(&request);
3393 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3394 ofpbuf_delete(reply);
3397 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3398 * binary data, interpreting them as an OpenFlow message, and prints the
3399 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3401 ofctl_ofp_print(int argc, char *argv[])
3403 struct ofpbuf packet;
3405 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3406 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3407 ovs_fatal(0, "trailing garbage following hex bytes");
3409 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
3410 ofpbuf_uninit(&packet);
3413 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3414 * and dumps each message in hex. */
3416 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3418 uint32_t bitmap = strtol(argv[1], NULL, 0);
3419 struct ofpbuf *hello;
3421 hello = ofputil_encode_hello(bitmap);
3422 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3423 ofp_print(stdout, hello->data, hello->size, verbosity);
3424 ofpbuf_delete(hello);
3427 static const struct command all_commands[] = {
3428 { "show", 1, 1, ofctl_show },
3429 { "monitor", 1, 3, ofctl_monitor },
3430 { "snoop", 1, 1, ofctl_snoop },
3431 { "dump-desc", 1, 1, ofctl_dump_desc },
3432 { "dump-tables", 1, 1, ofctl_dump_tables },
3433 { "dump-flows", 1, 2, ofctl_dump_flows },
3434 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3435 { "queue-stats", 1, 3, ofctl_queue_stats },
3436 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3437 { "add-flow", 2, 2, ofctl_add_flow },
3438 { "add-flows", 2, 2, ofctl_add_flows },
3439 { "mod-flows", 2, 2, ofctl_mod_flows },
3440 { "del-flows", 1, 2, ofctl_del_flows },
3441 { "replace-flows", 2, 2, ofctl_replace_flows },
3442 { "diff-flows", 2, 2, ofctl_diff_flows },
3443 { "add-meter", 2, 2, ofctl_add_meter },
3444 { "mod-meter", 2, 2, ofctl_mod_meter },
3445 { "del-meter", 2, 2, ofctl_del_meters },
3446 { "del-meters", 1, 1, ofctl_del_meters },
3447 { "dump-meter", 2, 2, ofctl_dump_meters },
3448 { "dump-meters", 1, 1, ofctl_dump_meters },
3449 { "meter-stats", 1, 2, ofctl_meter_stats },
3450 { "meter-features", 1, 1, ofctl_meter_features },
3451 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3452 { "dump-ports", 1, 2, ofctl_dump_ports },
3453 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3454 { "mod-port", 3, 3, ofctl_mod_port },
3455 { "mod-table", 3, 3, ofctl_mod_table },
3456 { "get-frags", 1, 1, ofctl_get_frags },
3457 { "set-frags", 2, 2, ofctl_set_frags },
3458 { "probe", 1, 1, ofctl_probe },
3459 { "ping", 1, 2, ofctl_ping },
3460 { "benchmark", 3, 3, ofctl_benchmark },
3462 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3463 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3465 { "add-group", 1, 2, ofctl_add_group },
3466 { "add-groups", 1, 2, ofctl_add_groups },
3467 { "mod-group", 1, 2, ofctl_mod_group },
3468 { "del-groups", 1, 2, ofctl_del_groups },
3469 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3470 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3471 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3472 { "help", 0, INT_MAX, ofctl_help },
3474 /* Undocumented commands for testing. */
3475 { "parse-flow", 1, 1, ofctl_parse_flow },
3476 { "parse-flows", 1, 1, ofctl_parse_flows },
3477 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3478 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3479 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3480 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3481 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3482 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3483 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3484 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3485 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3486 { "check-vlan", 2, 2, ofctl_check_vlan },
3487 { "print-error", 1, 1, ofctl_print_error },
3488 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3489 { "ofp-print", 1, 2, ofctl_ofp_print },
3490 { "encode-hello", 1, 1, ofctl_encode_hello },
3492 { NULL, 0, 0, NULL },
3495 static const struct command *get_all_commands(void)
3497 return all_commands;