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"
41 #include "ofp-actions.h"
42 #include "ofp-errors.h"
44 #include "ofp-parse.h"
45 #include "ofp-print.h"
47 #include "ofp-version-opt.h"
49 #include "ofproto/ofproto.h"
50 #include "openflow/nicira-ext.h"
51 #include "openflow/openflow.h"
53 #include "pcap-file.h"
54 #include "poll-loop.h"
56 #include "stream-ssl.h"
57 #include "socket-util.h"
63 #include "meta-flow.h"
66 VLOG_DEFINE_THIS_MODULE(ofctl);
68 /* --strict: Use strict matching for flow mod commands? Additionally governs
69 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
73 /* --readd: If true, on replace-flows, re-add even flows that have not changed
74 * (to reset flow counters). */
77 /* -F, --flow-format: Allowed protocols. By default, any protocol is
79 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
81 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
82 * commands. Either one of NXPIF_* to force a particular packet_in format, or
83 * -1 to let ovs-ofctl choose the default. */
84 static int preferred_packet_in_format = -1;
86 /* -m, --more: Additional verbosity for ofp-print functions. */
89 /* --timestamp: Print a timestamp before each received packet on "monitor" and
91 static bool timestamp;
93 /* --sort, --rsort: Sort order. */
94 enum sort_order { SORT_ASC, SORT_DESC };
95 struct sort_criterion {
96 const struct mf_field *field; /* NULL means to sort by priority. */
97 enum sort_order order;
99 static struct sort_criterion *criteria;
100 static size_t n_criteria, allocated_criteria;
102 static const struct command *get_all_commands(void);
104 static void usage(void) NO_RETURN;
105 static void parse_options(int argc, char *argv[]);
107 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
108 struct ofpbuf **replyp,
109 struct ofputil_flow_stats *,
110 struct ofpbuf *ofpacts);
112 main(int argc, char *argv[])
114 set_program_name(argv[0]);
115 parse_options(argc, argv);
116 signal(SIGPIPE, SIG_IGN);
117 run_command(argc - optind, argv + optind, get_all_commands());
122 add_sort_criterion(enum sort_order order, const char *field)
124 struct sort_criterion *sc;
126 if (n_criteria >= allocated_criteria) {
127 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
130 sc = &criteria[n_criteria++];
131 if (!field || !strcasecmp(field, "priority")) {
134 sc->field = mf_from_name(field);
136 ovs_fatal(0, "%s: unknown field name", field);
143 parse_options(int argc, char *argv[])
146 OPT_STRICT = UCHAR_MAX + 1,
152 OFP_VERSION_OPTION_ENUMS,
155 static const struct option long_options[] = {
156 {"timeout", required_argument, NULL, 't'},
157 {"strict", no_argument, NULL, OPT_STRICT},
158 {"readd", no_argument, NULL, OPT_READD},
159 {"flow-format", required_argument, NULL, 'F'},
160 {"packet-in-format", required_argument, NULL, 'P'},
161 {"more", no_argument, NULL, 'm'},
162 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
163 {"sort", optional_argument, NULL, OPT_SORT},
164 {"rsort", optional_argument, NULL, OPT_RSORT},
165 {"help", no_argument, NULL, 'h'},
167 OFP_VERSION_LONG_OPTIONS,
169 STREAM_SSL_LONG_OPTIONS,
172 char *short_options = long_options_to_short_options(long_options);
174 enum ofputil_protocol version_protocols;
177 unsigned long int timeout;
180 c = getopt_long(argc, argv, short_options, long_options, NULL);
187 timeout = strtoul(optarg, NULL, 10);
189 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
197 allowed_protocols = ofputil_protocols_from_string(optarg);
198 if (!allowed_protocols) {
199 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
204 preferred_packet_in_format =
205 ofputil_packet_in_format_from_string(optarg);
206 if (preferred_packet_in_format < 0) {
207 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
231 add_sort_criterion(SORT_ASC, optarg);
235 add_sort_criterion(SORT_DESC, optarg);
238 DAEMON_OPTION_HANDLERS
239 OFP_VERSION_OPTION_HANDLERS
241 STREAM_SSL_OPTION_HANDLERS
252 /* Always do a final sort pass based on priority. */
253 add_sort_criterion(SORT_DESC, "priority");
258 versions = get_allowed_ofp_versions();
259 version_protocols = ofputil_protocols_from_version_bitmap(versions);
260 if (!(allowed_protocols & version_protocols)) {
261 char *protocols = ofputil_protocols_to_string(allowed_protocols);
262 struct ds version_s = DS_EMPTY_INITIALIZER;
264 ofputil_format_version_bitmap_names(&version_s, versions);
265 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
266 "any of the enabled flow formats (%s). (Use -O to enable "
267 "additional OpenFlow versions or -F to enable additional "
268 "flow formats.)", ds_cstr(&version_s), protocols);
270 allowed_protocols &= version_protocols;
271 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
278 printf("%s: OpenFlow switch management utility\n"
279 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
280 "\nFor OpenFlow switches:\n"
281 " show SWITCH show OpenFlow information\n"
282 " dump-desc SWITCH print switch description\n"
283 " dump-tables SWITCH print table stats\n"
284 " mod-port SWITCH IFACE ACT modify port behavior\n"
285 " mod-table SWITCH MOD modify flow table behavior\n"
286 " get-frags SWITCH print fragment handling behavior\n"
287 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
288 " dump-ports SWITCH [PORT] print port statistics\n"
289 " dump-ports-desc SWITCH print port descriptions\n"
290 " dump-flows SWITCH print all flow entries\n"
291 " dump-flows SWITCH FLOW print matching FLOWs\n"
292 " dump-aggregate SWITCH print aggregate flow statistics\n"
293 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
294 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
295 " add-flow SWITCH FLOW add flow described by FLOW\n"
296 " add-flows SWITCH FILE add flows from FILE\n"
297 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
298 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
299 " replace-flows SWITCH FILE replace flows with those in FILE\n"
300 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
301 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
302 " execute ACTIONS on PACKET\n"
303 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
304 " print packets received from SWITCH\n"
305 " snoop SWITCH snoop on SWITCH and its controller\n"
306 " add-group SWITCH GROUP add group described by GROUP\n"
307 " add-group SWITCH FILE add group from FILE\n"
308 " mod-group SWITCH GROUP modify specific group\n"
309 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
310 " dump-group-features SWITCH print group features\n"
311 " dump-groups SWITCH print group description\n"
312 " dump-group-stats SWITCH [GROUP] print group statistics\n"
313 " queue-get-config SWITCH PORT print queue information for port\n"
314 " add-meter SWITCH METER add meter described by METER\n"
315 " mod-meter SWITCH METER modify specific METER\n"
316 " del-meter SWITCH METER delete METER\n"
317 " del-meters SWITCH delete all meters\n"
318 " dump-meter SWITCH METER print METER configuration\n"
319 " dump-meters SWITCH print all meter configuration\n"
320 " meter-stats SWITCH [METER] print meter statistics\n"
321 " meter-features SWITCH print meter features\n"
322 "\nFor OpenFlow switches and controllers:\n"
323 " probe TARGET probe whether TARGET is up\n"
324 " ping TARGET [N] latency of N-byte echos\n"
325 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
326 "SWITCH or TARGET is an active OpenFlow connection method.\n"
327 "\nOther commands:\n"
328 " ofp-parse FILE print messages read from FILE\n"
329 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
330 program_name, program_name);
331 vconn_usage(true, false, false);
335 printf("\nOther options:\n"
336 " --strict use strict match for flow commands\n"
337 " --readd replace flows that haven't changed\n"
338 " -F, --flow-format=FORMAT force particular flow format\n"
339 " -P, --packet-in-format=FRMT force particular packet in format\n"
340 " -m, --more be more verbose printing OpenFlow\n"
341 " --timestamp (monitor, snoop) print timestamps\n"
342 " -t, --timeout=SECS give up after SECS seconds\n"
343 " --sort[=field] sort in ascending order\n"
344 " --rsort[=field] sort in descending order\n"
345 " -h, --help display this help message\n"
346 " -V, --version display version information\n");
351 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
352 const char *argv[] OVS_UNUSED, void *exiting_)
354 bool *exiting = exiting_;
356 unixctl_command_reply(conn, NULL);
359 static void run(int retval, const char *message, ...)
363 run(int retval, const char *message, ...)
368 va_start(args, message);
369 ovs_fatal_valist(retval, message, args);
373 /* Generic commands. */
376 open_vconn_socket(const char *name, struct vconn **vconnp)
378 char *vconn_name = xasprintf("unix:%s", name);
381 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
383 if (error && error != ENOENT) {
384 ovs_fatal(0, "%s: failed to open socket (%s)", name,
385 ovs_strerror(error));
392 enum open_target { MGMT, SNOOP };
394 static enum ofputil_protocol
395 open_vconn__(const char *name, enum open_target target,
396 struct vconn **vconnp)
398 const char *suffix = target == MGMT ? "mgmt" : "snoop";
399 char *datapath_name, *datapath_type, *socket_name;
400 enum ofputil_protocol protocol;
405 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
407 ofproto_parse_name(name, &datapath_name, &datapath_type);
408 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
412 if (strchr(name, ':')) {
413 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
414 "connecting to %s", name);
415 } else if (!open_vconn_socket(name, vconnp)) {
417 } else if (!open_vconn_socket(bridge_path, vconnp)) {
419 } else if (!open_vconn_socket(socket_name, vconnp)) {
422 ovs_fatal(0, "%s is not a bridge or a socket", name);
425 if (target == SNOOP) {
426 vconn_set_recv_any_version(*vconnp);
432 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
433 error = vconn_connect_block(*vconnp);
435 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
436 ovs_strerror(error));
439 ofp_version = vconn_get_version(*vconnp);
440 protocol = ofputil_protocol_from_ofp_version(ofp_version);
442 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
448 static enum ofputil_protocol
449 open_vconn(const char *name, struct vconn **vconnp)
451 return open_vconn__(name, MGMT, vconnp);
455 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
457 ofpmsg_update_length(buffer);
458 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
462 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
464 struct ofpbuf *reply;
466 ofpmsg_update_length(request);
467 run(vconn_transact(vconn, request, &reply), "talking to %s",
468 vconn_get_name(vconn));
469 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
470 ofpbuf_delete(reply);
474 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
476 struct ofpbuf *request;
479 open_vconn(vconn_name, &vconn);
480 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
481 dump_transaction(vconn, request);
486 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
488 const struct ofp_header *request_oh = request->data;
489 ovs_be32 send_xid = request_oh->xid;
490 enum ofpraw request_raw;
491 enum ofpraw reply_raw;
494 ofpraw_decode_partial(&request_raw, request->data, request->size);
495 reply_raw = ofpraw_stats_request_to_reply(request_raw,
496 request_oh->version);
498 send_openflow_buffer(vconn, request);
501 struct ofpbuf *reply;
503 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
504 recv_xid = ((struct ofp_header *) reply->data)->xid;
505 if (send_xid == recv_xid) {
508 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
510 ofpraw_decode(&raw, reply->data);
511 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
513 } else if (raw == reply_raw) {
514 done = !ofpmp_more(reply->data);
516 ovs_fatal(0, "received bad reply: %s",
517 ofp_to_string(reply->data, reply->size,
521 VLOG_DBG("received reply with xid %08"PRIx32" "
522 "!= expected %08"PRIx32, recv_xid, send_xid);
524 ofpbuf_delete(reply);
529 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
531 struct ofpbuf *request;
534 open_vconn(vconn_name, &vconn);
535 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
536 dump_stats_transaction(vconn, request);
540 /* Sends all of the 'requests', which should be requests that only have replies
541 * if an error occurs, and waits for them to succeed or fail. If an error does
542 * occur, prints it and exits with an error.
544 * Destroys all of the 'requests'. */
546 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
548 struct ofpbuf *request, *reply;
550 LIST_FOR_EACH (request, list_node, requests) {
551 ofpmsg_update_length(request);
554 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
555 "talking to %s", vconn_get_name(vconn));
557 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
560 ofpbuf_delete(reply);
563 /* Sends 'request', which should be a request that only has a reply if an error
564 * occurs, and waits for it to succeed or fail. If an error does occur, prints
565 * it and exits with an error.
567 * Destroys 'request'. */
569 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
571 struct list requests;
573 list_init(&requests);
574 list_push_back(&requests, &request->list_node);
575 transact_multiple_noreply(vconn, &requests);
579 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
581 struct ofp_switch_config *config;
582 struct ofpbuf *request;
583 struct ofpbuf *reply;
586 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
587 vconn_get_version(vconn), 0);
588 run(vconn_transact(vconn, request, &reply),
589 "talking to %s", vconn_get_name(vconn));
591 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
592 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
595 config = ofpbuf_pull(reply, sizeof *config);
598 ofpbuf_delete(reply);
602 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
604 struct ofpbuf *request;
606 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
607 ofpbuf_put(request, config, sizeof *config);
609 transact_noreply(vconn, request);
613 ofctl_show(int argc OVS_UNUSED, char *argv[])
615 const char *vconn_name = argv[1];
617 struct ofpbuf *request;
618 struct ofpbuf *reply;
621 open_vconn(vconn_name, &vconn);
622 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
623 vconn_get_version(vconn), 0);
624 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
626 trunc = ofputil_switch_features_ports_trunc(reply);
627 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
629 ofpbuf_delete(reply);
632 /* The Features Reply may not contain all the ports, so send a
633 * Port Description stats request, which doesn't have size
635 dump_trivial_stats_transaction(vconn_name,
636 OFPRAW_OFPST_PORT_DESC_REQUEST);
638 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
643 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
645 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
649 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
651 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
655 fetch_port_by_features(const char *vconn_name,
656 const char *port_name, ofp_port_t port_no,
657 struct ofputil_phy_port *pp, bool *trunc)
659 struct ofputil_switch_features features;
660 const struct ofp_header *oh;
661 struct ofpbuf *request, *reply;
668 /* Fetch the switch's ofp_switch_features. */
669 open_vconn(vconn_name, &vconn);
670 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
671 vconn_get_version(vconn), 0);
672 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
676 if (ofptype_decode(&type, reply->data)
677 || type != OFPTYPE_FEATURES_REPLY) {
678 ovs_fatal(0, "%s: received bad features reply", vconn_name);
682 if (ofputil_switch_features_ports_trunc(reply)) {
687 error = ofputil_decode_switch_features(oh, &features, &b);
689 ovs_fatal(0, "%s: failed to decode features reply (%s)",
690 vconn_name, ofperr_to_string(error));
693 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
694 if (port_no != OFPP_NONE
695 ? port_no == pp->port_no
696 : !strcmp(pp->name, port_name)) {
703 ofpbuf_delete(reply);
708 fetch_port_by_stats(const char *vconn_name,
709 const char *port_name, ofp_port_t port_no,
710 struct ofputil_phy_port *pp)
712 struct ofpbuf *request;
718 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
719 send_xid = ((struct ofp_header *) request->data)->xid;
721 open_vconn(vconn_name, &vconn);
722 send_openflow_buffer(vconn, request);
725 struct ofpbuf *reply;
727 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
728 recv_xid = ((struct ofp_header *) reply->data)->xid;
729 if (send_xid == recv_xid) {
730 struct ofp_header *oh = reply->data;
735 ofpbuf_use_const(&b, oh, ntohs(oh->length));
736 if (ofptype_pull(&type, &b)
737 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
738 ovs_fatal(0, "received bad reply: %s",
739 ofp_to_string(reply->data, reply->size,
743 flags = ofpmp_flags(oh);
744 done = !(flags & OFPSF_REPLY_MORE);
747 /* We've already found the port, but we need to drain
748 * the queue of any other replies for this request. */
752 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
753 if (port_no != OFPP_NONE ? port_no == pp->port_no
754 : !strcmp(pp->name, port_name)) {
760 VLOG_DBG("received reply with xid %08"PRIx32" "
761 "!= expected %08"PRIx32, recv_xid, send_xid);
763 ofpbuf_delete(reply);
771 str_to_ofp(const char *s, ofp_port_t *ofp_port)
776 ret = str_to_uint(s, 10, &port_);
777 *ofp_port = u16_to_ofp(port_);
781 /* Opens a connection to 'vconn_name', fetches the port structure for
782 * 'port_name' (which may be a port name or number), and copies it into
785 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
786 struct ofputil_phy_port *pp)
792 /* Try to interpret the argument as a port number. */
793 if (!str_to_ofp(port_name, &port_no)) {
797 /* Try to find the port based on the Features Reply. If it looks
798 * like the results may be truncated, then use the Port Description
799 * stats message introduced in OVS 1.7. */
800 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
803 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
807 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
811 /* Returns the port number corresponding to 'port_name' (which may be a port
812 * name or number) within the switch 'vconn_name'. */
814 str_to_port_no(const char *vconn_name, const char *port_name)
818 if (ofputil_port_from_string(port_name, &port_no)) {
821 struct ofputil_phy_port pp;
823 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
829 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
830 enum ofputil_protocol *cur)
833 struct ofpbuf *request, *reply;
834 enum ofputil_protocol next;
836 request = ofputil_encode_set_protocol(*cur, want, &next);
841 run(vconn_transact_noreply(vconn, request, &reply),
842 "talking to %s", vconn_get_name(vconn));
844 char *s = ofp_to_string(reply->data, reply->size, 2);
845 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
846 vconn_get_name(vconn), s);
848 ofpbuf_delete(reply);
856 static enum ofputil_protocol
857 set_protocol_for_flow_dump(struct vconn *vconn,
858 enum ofputil_protocol cur_protocol,
859 enum ofputil_protocol usable_protocols)
864 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
865 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
866 if (f & usable_protocols & allowed_protocols
867 && try_set_protocol(vconn, f, &cur_protocol)) {
872 usable_s = ofputil_protocols_to_string(usable_protocols);
873 if (usable_protocols & allowed_protocols) {
874 ovs_fatal(0, "switch does not support any of the usable flow "
875 "formats (%s)", usable_s);
877 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
878 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
879 "allowed flow formats (%s)", usable_s, allowed_s);
883 static struct vconn *
884 prepare_dump_flows(int argc, char *argv[], bool aggregate,
885 struct ofpbuf **requestp)
887 enum ofputil_protocol usable_protocols, protocol;
888 struct ofputil_flow_stats_request fsr;
892 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
893 argc > 2 ? argv[2] : "",
896 ovs_fatal(0, "%s", error);
899 protocol = open_vconn(argv[1], &vconn);
900 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
901 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
906 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
908 struct ofpbuf *request;
911 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
912 dump_stats_transaction(vconn, request);
917 compare_flows(const void *afs_, const void *bfs_)
919 const struct ofputil_flow_stats *afs = afs_;
920 const struct ofputil_flow_stats *bfs = bfs_;
921 const struct match *a = &afs->match;
922 const struct match *b = &bfs->match;
923 const struct sort_criterion *sc;
925 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
926 const struct mf_field *f = sc->field;
930 unsigned int a_pri = afs->priority;
931 unsigned int b_pri = bfs->priority;
932 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
936 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
937 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
939 /* Skip the test for sc->order, so that missing fields always
940 * sort to the end whether we're sorting in ascending or
941 * descending order. */
944 union mf_value aval, bval;
946 mf_get_value(f, &a->flow, &aval);
947 mf_get_value(f, &b->flow, &bval);
948 ret = memcmp(&aval, &bval, f->n_bytes);
953 return sc->order == SORT_ASC ? ret : -ret;
961 ofctl_dump_flows(int argc, char *argv[])
964 return ofctl_dump_flows__(argc, argv, false);
966 struct ofputil_flow_stats *fses;
967 size_t n_fses, allocated_fses;
968 struct ofpbuf *request;
969 struct ofpbuf ofpacts;
970 struct ofpbuf *reply;
976 vconn = prepare_dump_flows(argc, argv, false, &request);
977 send_xid = ((struct ofp_header *) request->data)->xid;
978 send_openflow_buffer(vconn, request);
981 n_fses = allocated_fses = 0;
983 ofpbuf_init(&ofpacts, 0);
985 struct ofputil_flow_stats *fs;
987 if (n_fses >= allocated_fses) {
988 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
992 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
996 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
999 ofpbuf_uninit(&ofpacts);
1001 qsort(fses, n_fses, sizeof *fses, compare_flows);
1004 for (i = 0; i < n_fses; i++) {
1006 ofp_print_flow_stats(&s, &fses[i]);
1011 for (i = 0; i < n_fses; i++) {
1012 free(fses[i].ofpacts);
1021 ofctl_dump_aggregate(int argc, char *argv[])
1023 return ofctl_dump_flows__(argc, argv, true);
1027 ofctl_queue_stats(int argc, char *argv[])
1029 struct ofpbuf *request;
1030 struct vconn *vconn;
1031 struct ofputil_queue_stats_request oqs;
1033 open_vconn(argv[1], &vconn);
1035 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1036 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1038 oqs.port_no = OFPP_ANY;
1040 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1041 oqs.queue_id = atoi(argv[3]);
1043 oqs.queue_id = OFPQ_ALL;
1046 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1047 dump_stats_transaction(vconn, request);
1052 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1054 const char *vconn_name = argv[1];
1055 const char *port_name = argv[2];
1056 enum ofputil_protocol protocol;
1057 enum ofp_version version;
1058 struct ofpbuf *request;
1059 struct vconn *vconn;
1062 port = str_to_port_no(vconn_name, port_name);
1064 protocol = open_vconn(vconn_name, &vconn);
1065 version = ofputil_protocol_to_ofp_version(protocol);
1066 request = ofputil_encode_queue_get_config_request(version, port);
1067 dump_transaction(vconn, request);
1071 static enum ofputil_protocol
1072 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1073 enum ofputil_protocol usable_protocols)
1075 enum ofputil_protocol cur_protocol;
1079 if (!(usable_protocols & allowed_protocols)) {
1080 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1081 usable_s = ofputil_protocols_to_string(usable_protocols);
1082 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1083 "allowed flow formats (%s)", usable_s, allowed_s);
1086 /* If the initial flow format is allowed and usable, keep it. */
1087 cur_protocol = open_vconn(remote, vconnp);
1088 if (usable_protocols & allowed_protocols & cur_protocol) {
1089 return cur_protocol;
1092 /* Otherwise try each flow format in turn. */
1093 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1094 enum ofputil_protocol f = 1 << i;
1096 if (f != cur_protocol
1097 && f & usable_protocols & allowed_protocols
1098 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1103 usable_s = ofputil_protocols_to_string(usable_protocols);
1104 ovs_fatal(0, "switch does not support any of the usable flow "
1105 "formats (%s)", usable_s);
1109 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1110 size_t n_fms, enum ofputil_protocol usable_protocols)
1112 enum ofputil_protocol protocol;
1113 struct vconn *vconn;
1116 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1118 for (i = 0; i < n_fms; i++) {
1119 struct ofputil_flow_mod *fm = &fms[i];
1121 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1128 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1130 enum ofputil_protocol usable_protocols;
1131 struct ofputil_flow_mod *fms = NULL;
1135 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1138 ovs_fatal(0, "%s", error);
1140 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1145 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1147 if (argc > 2 && !strcmp(argv[2], "-")) {
1148 ofctl_flow_mod_file(argc, argv, command);
1150 struct ofputil_flow_mod fm;
1152 enum ofputil_protocol usable_protocols;
1154 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1157 ovs_fatal(0, "%s", error);
1159 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1164 ofctl_add_flow(int argc, char *argv[])
1166 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1170 ofctl_add_flows(int argc, char *argv[])
1172 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1176 ofctl_mod_flows(int argc, char *argv[])
1178 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1182 ofctl_del_flows(int argc, char *argv[])
1184 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1188 set_packet_in_format(struct vconn *vconn,
1189 enum nx_packet_in_format packet_in_format)
1191 struct ofpbuf *spif;
1193 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1195 transact_noreply(vconn, spif);
1196 VLOG_DBG("%s: using user-specified packet in format %s",
1197 vconn_get_name(vconn),
1198 ofputil_packet_in_format_to_string(packet_in_format));
1202 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1204 struct ofp_switch_config config;
1205 enum ofp_config_flags flags;
1207 fetch_switch_config(vconn, &config);
1208 flags = ntohs(config.flags);
1209 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1210 /* Set the invalid ttl config. */
1211 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1213 config.flags = htons(flags);
1214 set_switch_config(vconn, &config);
1216 /* Then retrieve the configuration to see if it really took. OpenFlow
1217 * doesn't define error reporting for bad modes, so this is all we can
1219 fetch_switch_config(vconn, &config);
1220 flags = ntohs(config.flags);
1221 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1222 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1223 "switch probably doesn't support mode)");
1230 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1231 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1232 * an error message and stores NULL in '*msgp'. */
1234 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1236 struct ofp_header *oh;
1239 msg = ofpbuf_new(strlen(hex) / 2);
1242 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1244 return "Trailing garbage in hex data";
1247 if (msg->size < sizeof(struct ofp_header)) {
1249 return "Message too short for OpenFlow";
1253 if (msg->size != ntohs(oh->length)) {
1255 return "Message size does not match length in OpenFlow header";
1263 ofctl_send(struct unixctl_conn *conn, int argc,
1264 const char *argv[], void *vconn_)
1266 struct vconn *vconn = vconn_;
1273 for (i = 1; i < argc; i++) {
1274 const char *error_msg;
1278 error_msg = openflow_from_hex(argv[i], &msg);
1280 ds_put_format(&reply, "%s\n", error_msg);
1285 fprintf(stderr, "send: ");
1286 ofp_print(stderr, msg->data, msg->size, verbosity);
1288 error = vconn_send_block(vconn, msg);
1291 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1294 ds_put_cstr(&reply, "sent\n");
1299 unixctl_command_reply(conn, ds_cstr(&reply));
1301 unixctl_command_reply_error(conn, ds_cstr(&reply));
1306 struct barrier_aux {
1307 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1308 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1312 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1313 const char *argv[] OVS_UNUSED, void *aux_)
1315 struct barrier_aux *aux = aux_;
1320 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1324 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1325 error = vconn_send_block(aux->vconn, msg);
1328 unixctl_command_reply_error(conn, ovs_strerror(error));
1335 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1336 const char *argv[], void *aux OVS_UNUSED)
1340 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1342 unixctl_command_reply_error(conn, ovs_strerror(errno));
1347 dup2(fd, STDERR_FILENO);
1349 unixctl_command_reply(conn, NULL);
1353 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1354 const char *argv[] OVS_UNUSED, void *blocked_)
1356 bool *blocked = blocked_;
1360 unixctl_command_reply(conn, NULL);
1362 unixctl_command_reply(conn, "already blocking");
1367 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1368 const char *argv[] OVS_UNUSED, void *blocked_)
1370 bool *blocked = blocked_;
1374 unixctl_command_reply(conn, NULL);
1376 unixctl_command_reply(conn, "already unblocked");
1380 /* Prints to stdout all of the messages received on 'vconn'.
1382 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1383 * received on 'vconn'. */
1385 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1387 struct barrier_aux barrier_aux = { vconn, NULL };
1388 struct unixctl_server *server;
1389 bool exiting = false;
1390 bool blocked = false;
1393 daemon_save_fd(STDERR_FILENO);
1395 error = unixctl_server_create(NULL, &server);
1397 ovs_fatal(error, "failed to create unixctl server");
1399 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1400 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1402 unixctl_command_register("ofctl/barrier", "", 0, 0,
1403 ofctl_barrier, &barrier_aux);
1404 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1405 ofctl_set_output_file, NULL);
1407 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1408 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1411 daemonize_complete();
1417 unixctl_server_run(server);
1422 retval = vconn_recv(vconn, &b);
1423 if (retval == EAGAIN) {
1426 run(retval, "vconn_recv");
1429 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1430 time_wall_msec(), true);
1435 ofptype_decode(&type, b->data);
1436 ofp_print(stderr, b->data, b->size, verbosity + 2);
1438 switch ((int) type) {
1439 case OFPTYPE_BARRIER_REPLY:
1440 if (barrier_aux.conn) {
1441 unixctl_command_reply(barrier_aux.conn, NULL);
1442 barrier_aux.conn = NULL;
1446 case OFPTYPE_ECHO_REQUEST:
1447 if (reply_to_echo_requests) {
1448 struct ofpbuf *reply;
1450 reply = make_echo_reply(b->data);
1451 retval = vconn_send_block(vconn, reply);
1453 ovs_fatal(retval, "failed to send echo reply");
1466 vconn_run_wait(vconn);
1468 vconn_recv_wait(vconn);
1470 unixctl_server_wait(server);
1474 unixctl_server_destroy(server);
1478 ofctl_monitor(int argc, char *argv[])
1480 struct vconn *vconn;
1482 enum ofputil_protocol usable_protocols;
1484 open_vconn(argv[1], &vconn);
1485 for (i = 2; i < argc; i++) {
1486 const char *arg = argv[i];
1488 if (isdigit((unsigned char) *arg)) {
1489 struct ofp_switch_config config;
1491 fetch_switch_config(vconn, &config);
1492 config.miss_send_len = htons(atoi(arg));
1493 set_switch_config(vconn, &config);
1494 } else if (!strcmp(arg, "invalid_ttl")) {
1495 monitor_set_invalid_ttl_to_controller(vconn);
1496 } else if (!strncmp(arg, "watch:", 6)) {
1497 struct ofputil_flow_monitor_request fmr;
1501 error = parse_flow_monitor_request(&fmr, arg + 6,
1504 ovs_fatal(0, "%s", error);
1507 msg = ofpbuf_new(0);
1508 ofputil_append_flow_monitor_request(&fmr, msg);
1509 dump_stats_transaction(vconn, msg);
1511 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1515 if (preferred_packet_in_format >= 0) {
1516 set_packet_in_format(vconn, preferred_packet_in_format);
1518 enum ofp_version version = vconn_get_version(vconn);
1521 case OFP10_VERSION: {
1522 struct ofpbuf *spif, *reply;
1524 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1526 run(vconn_transact_noreply(vconn, spif, &reply),
1527 "talking to %s", vconn_get_name(vconn));
1529 char *s = ofp_to_string(reply->data, reply->size, 2);
1530 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1531 " replied: %s. Falling back to the switch default.",
1532 vconn_get_name(vconn), s);
1534 ofpbuf_delete(reply);
1547 monitor_vconn(vconn, true);
1551 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1553 struct vconn *vconn;
1555 open_vconn__(argv[1], SNOOP, &vconn);
1556 monitor_vconn(vconn, false);
1560 ofctl_dump_ports(int argc, char *argv[])
1562 struct ofpbuf *request;
1563 struct vconn *vconn;
1566 open_vconn(argv[1], &vconn);
1567 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1568 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1569 dump_stats_transaction(vconn, request);
1574 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1576 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1580 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1582 struct ofpbuf *request;
1583 struct vconn *vconn;
1584 struct ofpbuf *reply;
1586 open_vconn(argv[1], &vconn);
1587 request = make_echo_request(vconn_get_version(vconn));
1588 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1589 if (reply->size != sizeof(struct ofp_header)) {
1590 ovs_fatal(0, "reply does not match request");
1592 ofpbuf_delete(reply);
1597 ofctl_packet_out(int argc, char *argv[])
1599 enum ofputil_protocol protocol;
1600 struct ofputil_packet_out po;
1601 struct ofpbuf ofpacts;
1602 struct vconn *vconn;
1605 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1607 ofpbuf_init(&ofpacts, 64);
1608 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1610 ovs_fatal(0, "%s", error);
1613 po.buffer_id = UINT32_MAX;
1614 po.in_port = str_to_port_no(argv[1], argv[2]);
1615 po.ofpacts = ofpacts.data;
1616 po.ofpacts_len = ofpacts.size;
1618 protocol = open_vconn(argv[1], &vconn);
1619 for (i = 4; i < argc; i++) {
1620 struct ofpbuf *packet, *opo;
1621 const char *error_msg;
1623 error_msg = eth_from_hex(argv[i], &packet);
1625 ovs_fatal(0, "%s", error_msg);
1628 po.packet = packet->data;
1629 po.packet_len = packet->size;
1630 opo = ofputil_encode_packet_out(&po, protocol);
1631 transact_noreply(vconn, opo);
1632 ofpbuf_delete(packet);
1635 ofpbuf_uninit(&ofpacts);
1639 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1641 struct ofp_config_flag {
1642 const char *name; /* The flag's name. */
1643 enum ofputil_port_config bit; /* Bit to turn on or off. */
1644 bool on; /* Value to set the bit to. */
1646 static const struct ofp_config_flag flags[] = {
1647 { "up", OFPUTIL_PC_PORT_DOWN, false },
1648 { "down", OFPUTIL_PC_PORT_DOWN, true },
1649 { "stp", OFPUTIL_PC_NO_STP, false },
1650 { "receive", OFPUTIL_PC_NO_RECV, false },
1651 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1652 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1653 { "forward", OFPUTIL_PC_NO_FWD, false },
1654 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1657 const struct ofp_config_flag *flag;
1658 enum ofputil_protocol protocol;
1659 struct ofputil_port_mod pm;
1660 struct ofputil_phy_port pp;
1661 struct vconn *vconn;
1662 const char *command;
1665 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1667 pm.port_no = pp.port_no;
1668 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1673 if (!strncasecmp(argv[3], "no-", 3)) {
1674 command = argv[3] + 3;
1676 } else if (!strncasecmp(argv[3], "no", 2)) {
1677 command = argv[3] + 2;
1683 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1684 if (!strcasecmp(command, flag->name)) {
1685 pm.mask = flag->bit;
1686 pm.config = flag->on ^ not ? flag->bit : 0;
1690 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1693 protocol = open_vconn(argv[1], &vconn);
1694 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1699 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1701 enum ofputil_protocol protocol, usable_protocols;
1702 struct ofputil_table_mod tm;
1703 struct vconn *vconn;
1707 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1709 ovs_fatal(0, "%s", error);
1712 protocol = open_vconn(argv[1], &vconn);
1713 if (!(protocol & usable_protocols)) {
1714 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1715 enum ofputil_protocol f = 1 << i;
1717 && f & usable_protocols
1718 && try_set_protocol(vconn, f, &protocol)) {
1725 if (!(protocol & usable_protocols)) {
1726 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1727 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1730 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1735 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1737 struct ofp_switch_config config;
1738 struct vconn *vconn;
1740 open_vconn(argv[1], &vconn);
1741 fetch_switch_config(vconn, &config);
1742 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1747 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1749 struct ofp_switch_config config;
1750 enum ofp_config_flags mode;
1751 struct vconn *vconn;
1754 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1755 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1758 open_vconn(argv[1], &vconn);
1759 fetch_switch_config(vconn, &config);
1760 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1761 if (flags != config.flags) {
1762 /* Set the configuration. */
1763 config.flags = flags;
1764 set_switch_config(vconn, &config);
1766 /* Then retrieve the configuration to see if it really took. OpenFlow
1767 * doesn't define error reporting for bad modes, so this is all we can
1769 fetch_switch_config(vconn, &config);
1770 if (flags != config.flags) {
1771 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1772 "switch probably doesn't support mode \"%s\")",
1773 argv[1], ofputil_frag_handling_to_string(mode));
1780 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1782 const char *filename = argv[1];
1786 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1788 ovs_fatal(errno, "%s: open", filename);
1791 ofpbuf_init(&b, 65536);
1793 struct ofp_header *oh;
1794 size_t length, tail_len;
1799 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1800 n = fread(oh, 1, sizeof *oh, file);
1803 } else if (n < sizeof *oh) {
1804 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1807 length = ntohs(oh->length);
1808 if (length < sizeof *oh) {
1809 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1813 tail_len = length - sizeof *oh;
1814 tail = ofpbuf_put_uninit(&b, tail_len);
1815 n = fread(tail, 1, tail_len, file);
1817 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1820 ofp_print(stdout, b.data, b.size, verbosity + 2);
1824 if (file != stdin) {
1830 is_openflow_port(ovs_be16 port_, char *ports[])
1832 uint16_t port = ntohs(port_);
1836 for (i = 0; ports[i]; i++) {
1837 if (port == atoi(ports[i])) {
1843 return port == OFP_PORT || port == OFP_OLD_PORT;
1848 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1850 struct tcp_reader *reader;
1855 file = ovs_pcap_open(argv[1], "rb");
1857 ovs_fatal(errno, "%s: open failed", argv[1]);
1860 reader = tcp_reader_open();
1863 struct ofpbuf *packet;
1867 error = ovs_pcap_read(file, &packet, &when);
1871 flow_extract(packet, 0, 0, NULL, NULL, &flow);
1872 if (flow.dl_type == htons(ETH_TYPE_IP)
1873 && flow.nw_proto == IPPROTO_TCP
1874 && (is_openflow_port(flow.tp_src, argv + 2) ||
1875 is_openflow_port(flow.tp_dst, argv + 2))) {
1876 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1878 while (payload->size >= sizeof(struct ofp_header)) {
1879 const struct ofp_header *oh;
1882 /* Align OpenFlow on 8-byte boundary for safe access. */
1883 ofpbuf_shift(payload, -((intptr_t) payload->data & 7));
1886 length = ntohs(oh->length);
1887 if (payload->size < length) {
1897 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1902 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1903 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1904 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1905 ofp_print(stdout, payload->data, length, verbosity + 1);
1906 ofpbuf_pull(payload, length);
1910 ofpbuf_delete(packet);
1912 tcp_reader_close(reader);
1916 ofctl_ping(int argc, char *argv[])
1918 size_t max_payload = 65535 - sizeof(struct ofp_header);
1919 unsigned int payload;
1920 struct vconn *vconn;
1923 payload = argc > 2 ? atoi(argv[2]) : 64;
1924 if (payload > max_payload) {
1925 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1928 open_vconn(argv[1], &vconn);
1929 for (i = 0; i < 10; i++) {
1930 struct timeval start, end;
1931 struct ofpbuf *request, *reply;
1932 const struct ofp_header *rpy_hdr;
1935 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1936 vconn_get_version(vconn), payload);
1937 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1939 xgettimeofday(&start);
1940 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1941 xgettimeofday(&end);
1943 rpy_hdr = reply->data;
1944 if (ofptype_pull(&type, reply)
1945 || type != OFPTYPE_ECHO_REPLY
1946 || reply->size != payload
1947 || memcmp(request->l3, reply->l3, payload)) {
1948 printf("Reply does not match request. Request:\n");
1949 ofp_print(stdout, request, request->size, verbosity + 2);
1951 ofp_print(stdout, reply, reply->size, verbosity + 2);
1953 printf("%"PRIuSIZE" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1954 reply->size, argv[1], ntohl(rpy_hdr->xid),
1955 (1000*(double)(end.tv_sec - start.tv_sec))
1956 + (.001*(end.tv_usec - start.tv_usec)));
1957 ofpbuf_delete(request);
1958 ofpbuf_delete(reply);
1964 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1966 size_t max_payload = 65535 - sizeof(struct ofp_header);
1967 struct timeval start, end;
1968 unsigned int payload_size, message_size;
1969 struct vconn *vconn;
1974 payload_size = atoi(argv[2]);
1975 if (payload_size > max_payload) {
1976 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1978 message_size = sizeof(struct ofp_header) + payload_size;
1980 count = atoi(argv[3]);
1982 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1983 count, message_size, count * message_size);
1985 open_vconn(argv[1], &vconn);
1986 xgettimeofday(&start);
1987 for (i = 0; i < count; i++) {
1988 struct ofpbuf *request, *reply;
1990 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1991 vconn_get_version(vconn), payload_size);
1992 ofpbuf_put_zeros(request, payload_size);
1993 run(vconn_transact(vconn, request, &reply), "transact");
1994 ofpbuf_delete(reply);
1996 xgettimeofday(&end);
1999 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2000 + (.001*(end.tv_usec - start.tv_usec)));
2001 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2002 duration, count / (duration / 1000.0),
2003 count * message_size / (duration / 1000.0));
2007 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2010 struct ofputil_group_mod *gm;
2011 struct ofpbuf *request;
2013 struct vconn *vconn;
2016 open_vconn(remote, &vconn);
2018 for (i = 0; i < n_gms; i++) {
2020 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2022 transact_noreply(vconn, request);
2032 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2034 struct ofputil_group_mod *gms = NULL;
2035 enum ofputil_protocol usable_protocols;
2039 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2042 ovs_fatal(0, "%s", error);
2044 ofctl_group_mod__(argv[1], gms, n_gms);
2049 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2051 if (argc > 2 && !strcmp(argv[2], "-")) {
2052 ofctl_group_mod_file(argc, argv, command);
2054 enum ofputil_protocol usable_protocols;
2055 struct ofputil_group_mod gm;
2058 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2061 ovs_fatal(0, "%s", error);
2063 ofctl_group_mod__(argv[1], &gm, 1);
2068 ofctl_add_group(int argc, char *argv[])
2070 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2074 ofctl_add_groups(int argc, char *argv[])
2076 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2080 ofctl_mod_group(int argc, char *argv[])
2082 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2086 ofctl_del_groups(int argc, char *argv[])
2088 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2092 ofctl_dump_group_stats(int argc, char *argv[])
2094 enum ofputil_protocol usable_protocols;
2095 struct ofputil_group_mod gm;
2096 struct ofpbuf *request;
2097 struct vconn *vconn;
2101 memset(&gm, 0, sizeof gm);
2103 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2104 argc > 2 ? argv[2] : "",
2107 ovs_fatal(0, "%s", error);
2110 group_id = gm.group_id;
2112 open_vconn(argv[1], &vconn);
2113 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2116 dump_stats_transaction(vconn, request);
2123 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2125 struct ofpbuf *request;
2126 struct vconn *vconn;
2128 open_vconn(argv[1], &vconn);
2130 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2132 dump_stats_transaction(vconn, request);
2139 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2141 struct ofpbuf *request;
2142 struct vconn *vconn;
2144 open_vconn(argv[1], &vconn);
2145 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2147 dump_stats_transaction(vconn, request);
2154 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2159 /* replace-flows and diff-flows commands. */
2161 /* A flow table entry, possibly with two different versions. */
2163 struct cls_rule rule; /* Within a "struct classifier". */
2164 struct fte_version *versions[2];
2167 /* One version of a Flow Table Entry. */
2168 struct fte_version {
2170 uint16_t idle_timeout;
2171 uint16_t hard_timeout;
2173 struct ofpact *ofpacts;
2177 /* Frees 'version' and the data that it owns. */
2179 fte_version_free(struct fte_version *version)
2182 free(version->ofpacts);
2187 /* Returns true if 'a' and 'b' are the same, false if they differ.
2189 * Ignores differences in 'flags' because there's no way to retrieve flags from
2190 * an OpenFlow switch. We have to assume that they are the same. */
2192 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2194 return (a->cookie == b->cookie
2195 && a->idle_timeout == b->idle_timeout
2196 && a->hard_timeout == b->hard_timeout
2197 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2198 b->ofpacts, b->ofpacts_len));
2201 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2202 * 'index' into 's', followed by a new-line. */
2204 fte_version_format(const struct fte *fte, int index, struct ds *s)
2206 const struct fte_version *version = fte->versions[index];
2213 cls_rule_format(&fte->rule, s);
2214 if (version->cookie != htonll(0)) {
2215 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2217 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2218 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2220 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2221 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2224 ds_put_cstr(s, " actions=");
2225 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2227 ds_put_char(s, '\n');
2231 fte_from_cls_rule(const struct cls_rule *cls_rule)
2233 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2236 /* Frees 'fte' and its versions. */
2238 fte_free(struct fte *fte)
2241 fte_version_free(fte->versions[0]);
2242 fte_version_free(fte->versions[1]);
2243 cls_rule_destroy(&fte->rule);
2248 /* Frees all of the FTEs within 'cls'. */
2250 fte_free_all(struct classifier *cls)
2252 struct cls_cursor cursor;
2253 struct fte *fte, *next;
2255 fat_rwlock_wrlock(&cls->rwlock);
2256 cls_cursor_init(&cursor, cls, NULL);
2257 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2258 classifier_remove(cls, &fte->rule);
2261 fat_rwlock_unlock(&cls->rwlock);
2262 classifier_destroy(cls);
2265 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2266 * necessary. Sets 'version' as the version of that rule with the given
2267 * 'index', replacing any existing version, if any.
2269 * Takes ownership of 'version'. */
2271 fte_insert(struct classifier *cls, const struct match *match,
2272 unsigned int priority, struct fte_version *version, int index)
2274 struct fte *old, *fte;
2276 fte = xzalloc(sizeof *fte);
2277 cls_rule_init(&fte->rule, match, priority);
2278 fte->versions[index] = version;
2280 fat_rwlock_wrlock(&cls->rwlock);
2281 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2282 fat_rwlock_unlock(&cls->rwlock);
2284 fte_version_free(old->versions[index]);
2285 fte->versions[!index] = old->versions[!index];
2286 cls_rule_destroy(&old->rule);
2291 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2292 * with the specified 'index'. Returns the flow formats able to represent the
2293 * flows that were read. */
2294 static enum ofputil_protocol
2295 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2297 enum ofputil_protocol usable_protocols;
2302 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2304 ovs_fatal(errno, "%s: open", filename);
2308 usable_protocols = OFPUTIL_P_ANY;
2310 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2311 struct fte_version *version;
2312 struct ofputil_flow_mod fm;
2314 enum ofputil_protocol usable;
2316 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2318 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2320 usable_protocols &= usable;
2322 version = xmalloc(sizeof *version);
2323 version->cookie = fm.new_cookie;
2324 version->idle_timeout = fm.idle_timeout;
2325 version->hard_timeout = fm.hard_timeout;
2326 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2327 | OFPUTIL_FF_EMERG);
2328 version->ofpacts = fm.ofpacts;
2329 version->ofpacts_len = fm.ofpacts_len;
2331 fte_insert(cls, &fm.match, fm.priority, version, index);
2335 if (file != stdin) {
2339 return usable_protocols;
2343 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2344 struct ofpbuf **replyp,
2345 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2347 struct ofpbuf *reply = *replyp;
2353 /* Get a flow stats reply message, if we don't already have one. */
2359 run(vconn_recv_block(vconn, &reply),
2360 "OpenFlow packet receive failed");
2361 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2363 error = ofptype_decode(&type, reply->data);
2364 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2365 ovs_fatal(0, "received bad reply: %s",
2366 ofp_to_string(reply->data, reply->size,
2371 /* Pull an individual flow stats reply out of the message. */
2372 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2379 more = ofpmp_more(reply->l2);
2380 ofpbuf_delete(reply);
2389 ovs_fatal(0, "parse error in reply (%s)",
2390 ofperr_to_string(retval));
2395 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2396 * format 'protocol', and adds them as flow table entries in 'cls' for the
2397 * version with the specified 'index'. */
2399 read_flows_from_switch(struct vconn *vconn,
2400 enum ofputil_protocol protocol,
2401 struct classifier *cls, int index)
2403 struct ofputil_flow_stats_request fsr;
2404 struct ofputil_flow_stats fs;
2405 struct ofpbuf *request;
2406 struct ofpbuf ofpacts;
2407 struct ofpbuf *reply;
2410 fsr.aggregate = false;
2411 match_init_catchall(&fsr.match);
2412 fsr.out_port = OFPP_ANY;
2413 fsr.table_id = 0xff;
2414 fsr.cookie = fsr.cookie_mask = htonll(0);
2415 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2416 send_xid = ((struct ofp_header *) request->data)->xid;
2417 send_openflow_buffer(vconn, request);
2420 ofpbuf_init(&ofpacts, 0);
2421 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2422 struct fte_version *version;
2424 version = xmalloc(sizeof *version);
2425 version->cookie = fs.cookie;
2426 version->idle_timeout = fs.idle_timeout;
2427 version->hard_timeout = fs.hard_timeout;
2429 version->ofpacts_len = fs.ofpacts_len;
2430 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2432 fte_insert(cls, &fs.match, fs.priority, version, index);
2434 ofpbuf_uninit(&ofpacts);
2438 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2439 enum ofputil_protocol protocol, struct list *packets)
2441 const struct fte_version *version = fte->versions[index];
2442 struct ofputil_flow_mod fm;
2445 minimatch_expand(&fte->rule.match, &fm.match);
2446 fm.priority = fte->rule.priority;
2447 fm.cookie = htonll(0);
2448 fm.cookie_mask = htonll(0);
2449 fm.new_cookie = version->cookie;
2450 fm.modify_cookie = true;
2452 fm.command = command;
2453 fm.idle_timeout = version->idle_timeout;
2454 fm.hard_timeout = version->hard_timeout;
2455 fm.buffer_id = UINT32_MAX;
2456 fm.out_port = OFPP_ANY;
2457 fm.flags = version->flags;
2458 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2459 command == OFPFC_MODIFY_STRICT) {
2460 fm.ofpacts = version->ofpacts;
2461 fm.ofpacts_len = version->ofpacts_len;
2467 ofm = ofputil_encode_flow_mod(&fm, protocol);
2468 list_push_back(packets, &ofm->list_node);
2472 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2474 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2475 enum ofputil_protocol usable_protocols, protocol;
2476 struct cls_cursor cursor;
2477 struct classifier cls;
2478 struct list requests;
2479 struct vconn *vconn;
2482 classifier_init(&cls, NULL);
2483 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2485 protocol = open_vconn(argv[1], &vconn);
2486 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2488 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2490 list_init(&requests);
2492 /* Delete flows that exist on the switch but not in the file. */
2493 fat_rwlock_rdlock(&cls.rwlock);
2494 cls_cursor_init(&cursor, &cls, NULL);
2495 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2496 struct fte_version *file_ver = fte->versions[FILE_IDX];
2497 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2499 if (sw_ver && !file_ver) {
2500 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2501 protocol, &requests);
2505 /* Add flows that exist in the file but not on the switch.
2506 * Update flows that exist in both places but differ. */
2507 cls_cursor_init(&cursor, &cls, NULL);
2508 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2509 struct fte_version *file_ver = fte->versions[FILE_IDX];
2510 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2513 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2514 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2517 fat_rwlock_unlock(&cls.rwlock);
2518 transact_multiple_noreply(vconn, &requests);
2525 read_flows_from_source(const char *source, struct classifier *cls, int index)
2529 if (source[0] == '/' || source[0] == '.'
2530 || (!strchr(source, ':') && !stat(source, &s))) {
2531 read_flows_from_file(source, cls, index);
2533 enum ofputil_protocol protocol;
2534 struct vconn *vconn;
2536 protocol = open_vconn(source, &vconn);
2537 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2538 read_flows_from_switch(vconn, protocol, cls, index);
2544 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2546 bool differences = false;
2547 struct cls_cursor cursor;
2548 struct classifier cls;
2552 classifier_init(&cls, NULL);
2553 read_flows_from_source(argv[1], &cls, 0);
2554 read_flows_from_source(argv[2], &cls, 1);
2559 fat_rwlock_rdlock(&cls.rwlock);
2560 cls_cursor_init(&cursor, &cls, NULL);
2561 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2562 struct fte_version *a = fte->versions[0];
2563 struct fte_version *b = fte->versions[1];
2565 if (!a || !b || !fte_version_equals(a, b)) {
2566 fte_version_format(fte, 0, &a_s);
2567 fte_version_format(fte, 1, &b_s);
2568 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2570 printf("-%s", ds_cstr(&a_s));
2573 printf("+%s", ds_cstr(&b_s));
2579 fat_rwlock_unlock(&cls.rwlock);
2592 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2594 struct ofputil_meter_mod mm;
2595 struct vconn *vconn;
2596 enum ofputil_protocol protocol;
2597 enum ofputil_protocol usable_protocols;
2598 enum ofp_version version;
2602 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2604 ovs_fatal(0, "%s", error);
2607 usable_protocols = OFPUTIL_P_OF13_UP;
2608 mm.command = command;
2609 mm.meter.meter_id = OFPM13_ALL;
2612 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2613 version = ofputil_protocol_to_ofp_version(protocol);
2614 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2619 ofctl_meter_request__(const char *bridge, const char *str,
2620 enum ofputil_meter_request_type type)
2622 struct ofputil_meter_mod mm;
2623 struct vconn *vconn;
2624 enum ofputil_protocol usable_protocols;
2625 enum ofputil_protocol protocol;
2626 enum ofp_version version;
2630 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2632 ovs_fatal(0, "%s", error);
2635 usable_protocols = OFPUTIL_P_OF13_UP;
2636 mm.meter.meter_id = OFPM13_ALL;
2639 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2640 version = ofputil_protocol_to_ofp_version(protocol);
2641 transact_noreply(vconn, ofputil_encode_meter_request(version,
2643 mm.meter.meter_id));
2649 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2651 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2655 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2657 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2661 ofctl_del_meters(int argc, char *argv[])
2663 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2667 ofctl_dump_meters(int argc, char *argv[])
2669 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2670 OFPUTIL_METER_CONFIG);
2674 ofctl_meter_stats(int argc, char *argv[])
2676 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2677 OFPUTIL_METER_STATS);
2681 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2683 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2687 /* Undocumented commands for unit testing. */
2690 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2691 enum ofputil_protocol usable_protocols)
2693 enum ofputil_protocol protocol = 0;
2697 usable_s = ofputil_protocols_to_string(usable_protocols);
2698 printf("usable protocols: %s\n", usable_s);
2701 if (!(usable_protocols & allowed_protocols)) {
2702 ovs_fatal(0, "no usable protocol");
2704 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2706 if (protocol & usable_protocols & allowed_protocols) {
2710 ovs_assert(is_pow2(protocol));
2712 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2714 for (i = 0; i < n_fms; i++) {
2715 struct ofputil_flow_mod *fm = &fms[i];
2718 msg = ofputil_encode_flow_mod(fm, protocol);
2719 ofp_print(stdout, msg->data, msg->size, verbosity);
2726 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2727 * it back to stdout. */
2729 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2731 enum ofputil_protocol usable_protocols;
2732 struct ofputil_flow_mod fm;
2735 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2737 ovs_fatal(0, "%s", error);
2739 ofctl_parse_flows__(&fm, 1, usable_protocols);
2742 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2743 * add-flows) and prints each of the flows back to stdout. */
2745 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2747 enum ofputil_protocol usable_protocols;
2748 struct ofputil_flow_mod *fms = NULL;
2752 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2755 ovs_fatal(0, "%s", error);
2757 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2762 ofctl_parse_nxm__(bool oxm)
2767 while (!ds_get_test_line(&in, stdin)) {
2768 struct ofpbuf nx_match;
2770 ovs_be64 cookie, cookie_mask;
2774 /* Convert string to nx_match. */
2775 ofpbuf_init(&nx_match, 0);
2777 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2779 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2782 /* Convert nx_match to match. */
2785 error = oxm_pull_match(&nx_match, &match);
2787 error = nx_pull_match(&nx_match, match_len, &match,
2788 &cookie, &cookie_mask);
2792 error = oxm_pull_match_loose(&nx_match, &match);
2794 error = nx_pull_match_loose(&nx_match, match_len, &match,
2795 &cookie, &cookie_mask);
2803 /* Convert match back to nx_match. */
2804 ofpbuf_uninit(&nx_match);
2805 ofpbuf_init(&nx_match, 0);
2807 match_len = oxm_put_match(&nx_match, &match);
2808 out = oxm_match_to_string(&nx_match, match_len);
2810 match_len = nx_put_match(&nx_match, &match,
2811 cookie, cookie_mask);
2812 out = nx_match_to_string(nx_match.data, match_len);
2818 printf("nx_pull_match() returned error %s\n",
2819 ofperr_get_name(error));
2822 ofpbuf_uninit(&nx_match);
2827 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2828 * stdin, does some internal fussing with them, and then prints them back as
2829 * strings on stdout. */
2831 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2833 return ofctl_parse_nxm__(false);
2836 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2837 * stdin, does some internal fussing with them, and then prints them back as
2838 * strings on stdout. */
2840 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2842 return ofctl_parse_nxm__(true);
2846 print_differences(const char *prefix,
2847 const void *a_, size_t a_len,
2848 const void *b_, size_t b_len)
2850 const uint8_t *a = a_;
2851 const uint8_t *b = b_;
2854 for (i = 0; i < MIN(a_len, b_len); i++) {
2856 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2857 prefix, i, a[i], b[i]);
2860 for (i = a_len; i < b_len; i++) {
2861 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2863 for (i = b_len; i < a_len; i++) {
2864 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2868 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2869 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2870 * on stdout, and then converts them back to hex bytes and prints any
2871 * differences from the input. */
2873 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2878 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2879 struct ofpbuf of10_out;
2880 struct ofpbuf of10_in;
2881 struct ofpbuf ofpacts;
2886 /* Parse hex bytes. */
2887 ofpbuf_init(&of10_in, 0);
2888 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2889 ovs_fatal(0, "Trailing garbage in hex data");
2892 /* Convert to ofpacts. */
2893 ofpbuf_init(&ofpacts, 0);
2894 size = of10_in.size;
2895 error = ofpacts_pull_openflow_actions(&of10_in, of10_in.size,
2896 OFP10_VERSION, &ofpacts);
2898 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2899 ofpbuf_uninit(&ofpacts);
2900 ofpbuf_uninit(&of10_in);
2903 ofpbuf_push_uninit(&of10_in, size);
2905 /* Print cls_rule. */
2907 ds_put_cstr(&s, "actions=");
2908 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2912 /* Convert back to ofp10 actions and print differences from input. */
2913 ofpbuf_init(&of10_out, 0);
2914 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of10_out,
2917 print_differences("", of10_in.data, of10_in.size,
2918 of10_out.data, of10_out.size);
2921 ofpbuf_uninit(&ofpacts);
2922 ofpbuf_uninit(&of10_in);
2923 ofpbuf_uninit(&of10_out);
2928 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2929 * bytes from stdin, converts them to cls_rules, prints them as strings on
2930 * stdout, and then converts them back to hex bytes and prints any differences
2933 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2934 * values are ignored in the input and will be set to zero when OVS converts
2935 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2936 * it does the conversion to hex, to ensure that in fact they are ignored. */
2938 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2945 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2946 struct ofpbuf match_in, match_expout;
2947 struct ofp10_match match_out;
2948 struct ofp10_match match_normal;
2952 /* Parse hex bytes to use for expected output. */
2954 ds_put_cstr(&expout, ds_cstr(&in));
2955 for (p = ds_cstr(&expout); *p; p++) {
2960 ofpbuf_init(&match_expout, 0);
2961 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2962 ovs_fatal(0, "Trailing garbage in hex data");
2964 if (match_expout.size != sizeof(struct ofp10_match)) {
2965 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2966 match_expout.size, sizeof(struct ofp10_match));
2969 /* Parse hex bytes for input. */
2970 for (p = ds_cstr(&in); *p; p++) {
2972 *p = "0123456789abcdef"[random_uint32() & 0xf];
2975 ofpbuf_init(&match_in, 0);
2976 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2977 ovs_fatal(0, "Trailing garbage in hex data");
2979 if (match_in.size != sizeof(struct ofp10_match)) {
2980 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
2981 match_in.size, sizeof(struct ofp10_match));
2984 /* Convert to cls_rule and print. */
2985 ofputil_match_from_ofp10_match(match_in.data, &match);
2986 match_print(&match);
2988 /* Convert back to ofp10_match and print differences from input. */
2989 ofputil_match_to_ofp10_match(&match, &match_out);
2990 print_differences("", match_expout.data, match_expout.size,
2991 &match_out, sizeof match_out);
2993 /* Normalize, then convert and compare again. */
2994 ofputil_normalize_match(&match);
2995 ofputil_match_to_ofp10_match(&match, &match_normal);
2996 print_differences("normal: ", &match_out, sizeof match_out,
2997 &match_normal, sizeof match_normal);
3000 ofpbuf_uninit(&match_in);
3001 ofpbuf_uninit(&match_expout);
3004 ds_destroy(&expout);
3007 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3008 * bytes from stdin, converts them to "struct match"es, prints them as strings
3009 * on stdout, and then converts them back to hex bytes and prints any
3010 * differences from the input. */
3012 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3017 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3018 struct ofpbuf match_in;
3019 struct ofp11_match match_out;
3023 /* Parse hex bytes. */
3024 ofpbuf_init(&match_in, 0);
3025 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3026 ovs_fatal(0, "Trailing garbage in hex data");
3028 if (match_in.size != sizeof(struct ofp11_match)) {
3029 ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE,
3030 match_in.size, sizeof(struct ofp11_match));
3033 /* Convert to match. */
3034 error = ofputil_match_from_ofp11_match(match_in.data, &match);
3036 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3037 ofpbuf_uninit(&match_in);
3042 match_print(&match);
3044 /* Convert back to ofp11_match and print differences from input. */
3045 ofputil_match_to_ofp11_match(&match, &match_out);
3047 print_differences("", match_in.data, match_in.size,
3048 &match_out, sizeof match_out);
3051 ofpbuf_uninit(&match_in);
3056 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3057 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3058 * on stdout, and then converts them back to hex bytes and prints any
3059 * differences from the input. */
3061 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3066 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3067 struct ofpbuf of11_out;
3068 struct ofpbuf of11_in;
3069 struct ofpbuf ofpacts;
3074 /* Parse hex bytes. */
3075 ofpbuf_init(&of11_in, 0);
3076 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3077 ovs_fatal(0, "Trailing garbage in hex data");
3080 /* Convert to ofpacts. */
3081 ofpbuf_init(&ofpacts, 0);
3082 size = of11_in.size;
3083 error = ofpacts_pull_openflow_actions(&of11_in, of11_in.size,
3084 OFP11_VERSION, &ofpacts);
3086 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3087 ofpbuf_uninit(&ofpacts);
3088 ofpbuf_uninit(&of11_in);
3091 ofpbuf_push_uninit(&of11_in, size);
3093 /* Print cls_rule. */
3095 ds_put_cstr(&s, "actions=");
3096 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3100 /* Convert back to ofp11 actions and print differences from input. */
3101 ofpbuf_init(&of11_out, 0);
3102 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, &of11_out,
3105 print_differences("", of11_in.data, of11_in.size,
3106 of11_out.data, of11_out.size);
3109 ofpbuf_uninit(&ofpacts);
3110 ofpbuf_uninit(&of11_in);
3111 ofpbuf_uninit(&of11_out);
3116 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3117 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3118 * them as strings on stdout, and then converts them back to hex bytes and
3119 * prints any differences from the input. */
3121 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3126 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3127 struct ofpbuf of11_out;
3128 struct ofpbuf of11_in;
3129 struct ofpbuf ofpacts;
3133 const char *table_id;
3136 /* Parse table_id separated with the follow-up instructions by ",", if
3138 instructions = ds_cstr(&in);
3140 if (strstr(instructions, ",")) {
3141 table_id = strsep(&instructions, ",");
3144 /* Parse hex bytes. */
3145 ofpbuf_init(&of11_in, 0);
3146 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3147 ovs_fatal(0, "Trailing garbage in hex data");
3150 /* Convert to ofpacts. */
3151 ofpbuf_init(&ofpacts, 0);
3152 size = of11_in.size;
3153 error = ofpacts_pull_openflow_instructions(&of11_in, of11_in.size,
3154 OFP11_VERSION, &ofpacts);
3156 /* Verify actions, enforce consistency. */
3158 memset(&flow, 0, sizeof flow);
3159 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3161 table_id ? atoi(table_id) : 0,
3162 255, OFPUTIL_P_OF11_STD);
3165 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3166 ofpbuf_uninit(&ofpacts);
3167 ofpbuf_uninit(&of11_in);
3170 ofpbuf_push_uninit(&of11_in, size);
3172 /* Print cls_rule. */
3174 ds_put_cstr(&s, "actions=");
3175 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3179 /* Convert back to ofp11 instructions and print differences from
3181 ofpbuf_init(&of11_out, 0);
3182 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3183 &of11_out, OFP13_VERSION);
3185 print_differences("", of11_in.data, of11_in.size,
3186 of11_out.data, of11_out.size);
3189 ofpbuf_uninit(&ofpacts);
3190 ofpbuf_uninit(&of11_in);
3191 ofpbuf_uninit(&of11_out);
3196 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3198 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3202 pcap = ovs_pcap_open(argv[1], "rb");
3204 ovs_fatal(errno, "%s: open failed", argv[1]);
3208 struct ofpbuf *packet;
3212 error = ovs_pcap_read(pcap, &packet, NULL);
3216 ovs_fatal(error, "%s: read failed", argv[1]);
3219 flow_extract(packet, 0, 0, NULL, NULL, &flow);
3220 flow_print(stdout, &flow);
3222 ofpbuf_delete(packet);
3226 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3227 * mask values to and from various formats and prints the results. */
3229 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3234 struct ofputil_flow_mod fm;
3237 struct match nxm_match;
3241 struct ofp10_match of10_raw;
3242 struct match of10_match;
3244 struct ofp11_match of11_raw;
3245 struct match of11_match;
3250 enum ofputil_protocol usable_protocols; /* Unused for now. */
3252 match_init_catchall(&match);
3253 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3254 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3256 /* Convert to and from string. */
3257 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3258 printf("%s -> ", string_s);
3260 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3262 ovs_fatal(0, "%s", error_s);
3264 printf("%04"PRIx16"/%04"PRIx16"\n",
3265 ntohs(fm.match.flow.vlan_tci),
3266 ntohs(fm.match.wc.masks.vlan_tci));
3269 /* Convert to and from NXM. */
3270 ofpbuf_init(&nxm, 0);
3271 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3272 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3273 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3274 printf("NXM: %s -> ", nxm_s);
3276 printf("%s\n", ofperr_to_string(error));
3278 printf("%04"PRIx16"/%04"PRIx16"\n",
3279 ntohs(nxm_match.flow.vlan_tci),
3280 ntohs(nxm_match.wc.masks.vlan_tci));
3283 ofpbuf_uninit(&nxm);
3285 /* Convert to and from OXM. */
3286 ofpbuf_init(&nxm, 0);
3287 nxm_match_len = oxm_put_match(&nxm, &match);
3288 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3289 error = oxm_pull_match(&nxm, &nxm_match);
3290 printf("OXM: %s -> ", nxm_s);
3292 printf("%s\n", ofperr_to_string(error));
3294 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3295 (VLAN_VID_MASK | VLAN_CFI);
3296 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3297 (VLAN_VID_MASK | VLAN_CFI);
3299 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3300 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3301 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3307 ofpbuf_uninit(&nxm);
3309 /* Convert to and from OpenFlow 1.0. */
3310 ofputil_match_to_ofp10_match(&match, &of10_raw);
3311 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3312 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3313 ntohs(of10_raw.dl_vlan),
3314 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3315 of10_raw.dl_vlan_pcp,
3316 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3317 ntohs(of10_match.flow.vlan_tci),
3318 ntohs(of10_match.wc.masks.vlan_tci));
3320 /* Convert to and from OpenFlow 1.1. */
3321 ofputil_match_to_ofp11_match(&match, &of11_raw);
3322 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3323 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3324 ntohs(of11_raw.dl_vlan),
3325 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3326 of11_raw.dl_vlan_pcp,
3327 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3328 ntohs(of11_match.flow.vlan_tci),
3329 ntohs(of11_match.wc.masks.vlan_tci));
3332 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3335 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3340 error = ofperr_from_name(argv[1]);
3342 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3345 for (version = 0; version <= UINT8_MAX; version++) {
3346 const char *name = ofperr_domain_get_name(version);
3348 int vendor = ofperr_get_vendor(error, version);
3349 int type = ofperr_get_type(error, version);
3350 int code = ofperr_get_code(error, version);
3352 if (vendor != -1 || type != -1 || code != -1) {
3353 printf("%s: vendor %#x, type %d, code %d\n",
3354 name, vendor, type, code);
3360 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3361 * error named ENUM and prints the error reply in hex. */
3363 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3365 const struct ofp_header *oh;
3366 struct ofpbuf request, *reply;
3369 error = ofperr_from_name(argv[1]);
3371 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3374 ofpbuf_init(&request, 0);
3375 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3376 ovs_fatal(0, "Trailing garbage in hex data");
3378 if (request.size < sizeof(struct ofp_header)) {
3379 ovs_fatal(0, "Request too short");
3383 if (request.size != ntohs(oh->length)) {
3384 ovs_fatal(0, "Request size inconsistent");
3387 reply = ofperr_encode_reply(error, request.data);
3388 ofpbuf_uninit(&request);
3390 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3391 ofpbuf_delete(reply);
3394 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3395 * binary data, interpreting them as an OpenFlow message, and prints the
3396 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3398 ofctl_ofp_print(int argc, char *argv[])
3400 struct ofpbuf packet;
3402 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3403 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3404 ovs_fatal(0, "trailing garbage following hex bytes");
3406 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
3407 ofpbuf_uninit(&packet);
3410 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3411 * and dumps each message in hex. */
3413 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3415 uint32_t bitmap = strtol(argv[1], NULL, 0);
3416 struct ofpbuf *hello;
3418 hello = ofputil_encode_hello(bitmap);
3419 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3420 ofp_print(stdout, hello->data, hello->size, verbosity);
3421 ofpbuf_delete(hello);
3424 static const struct command all_commands[] = {
3425 { "show", 1, 1, ofctl_show },
3426 { "monitor", 1, 3, ofctl_monitor },
3427 { "snoop", 1, 1, ofctl_snoop },
3428 { "dump-desc", 1, 1, ofctl_dump_desc },
3429 { "dump-tables", 1, 1, ofctl_dump_tables },
3430 { "dump-flows", 1, 2, ofctl_dump_flows },
3431 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3432 { "queue-stats", 1, 3, ofctl_queue_stats },
3433 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3434 { "add-flow", 2, 2, ofctl_add_flow },
3435 { "add-flows", 2, 2, ofctl_add_flows },
3436 { "mod-flows", 2, 2, ofctl_mod_flows },
3437 { "del-flows", 1, 2, ofctl_del_flows },
3438 { "replace-flows", 2, 2, ofctl_replace_flows },
3439 { "diff-flows", 2, 2, ofctl_diff_flows },
3440 { "add-meter", 2, 2, ofctl_add_meter },
3441 { "mod-meter", 2, 2, ofctl_mod_meter },
3442 { "del-meter", 2, 2, ofctl_del_meters },
3443 { "del-meters", 1, 1, ofctl_del_meters },
3444 { "dump-meter", 2, 2, ofctl_dump_meters },
3445 { "dump-meters", 1, 1, ofctl_dump_meters },
3446 { "meter-stats", 1, 2, ofctl_meter_stats },
3447 { "meter-features", 1, 1, ofctl_meter_features },
3448 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3449 { "dump-ports", 1, 2, ofctl_dump_ports },
3450 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3451 { "mod-port", 3, 3, ofctl_mod_port },
3452 { "mod-table", 3, 3, ofctl_mod_table },
3453 { "get-frags", 1, 1, ofctl_get_frags },
3454 { "set-frags", 2, 2, ofctl_set_frags },
3455 { "probe", 1, 1, ofctl_probe },
3456 { "ping", 1, 2, ofctl_ping },
3457 { "benchmark", 3, 3, ofctl_benchmark },
3459 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3460 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3462 { "add-group", 1, 2, ofctl_add_group },
3463 { "add-groups", 1, 2, ofctl_add_groups },
3464 { "mod-group", 1, 2, ofctl_mod_group },
3465 { "del-groups", 1, 2, ofctl_del_groups },
3466 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3467 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3468 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3469 { "help", 0, INT_MAX, ofctl_help },
3471 /* Undocumented commands for testing. */
3472 { "parse-flow", 1, 1, ofctl_parse_flow },
3473 { "parse-flows", 1, 1, ofctl_parse_flows },
3474 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3475 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3476 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3477 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3478 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3479 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3480 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3481 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3482 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3483 { "check-vlan", 2, 2, ofctl_check_vlan },
3484 { "print-error", 1, 1, ofctl_print_error },
3485 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3486 { "ofp-print", 1, 2, ofctl_ofp_print },
3487 { "encode-hello", 1, 1, ofctl_encode_hello },
3489 { NULL, 0, 0, NULL },
3492 static const struct command *get_all_commands(void)
3494 return all_commands;