2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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>
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"
53 #include "dp-packet.h"
55 #include "pcap-file.h"
56 #include "poll-loop.h"
58 #include "stream-ssl.h"
59 #include "socket-util.h"
63 #include "openvswitch/vconn.h"
64 #include "openvswitch/vlog.h"
65 #include "meta-flow.h"
68 VLOG_DEFINE_THIS_MODULE(ofctl);
70 /* --bundle: Use OpenFlow 1.4 bundle for making the flow table change atomic.
71 * NOTE: Also the flow mod will use OpenFlow 1.4, so the semantics may be
72 * different (see the comment in parse_options() for details).
74 static bool bundle = false;
76 /* --strict: Use strict matching for flow mod commands? Additionally governs
77 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
81 /* --readd: If true, on replace-flows, re-add even flows that have not changed
82 * (to reset flow counters). */
85 /* -F, --flow-format: Allowed protocols. By default, any protocol is
87 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
89 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
90 * commands. Either one of NXPIF_* to force a particular packet_in format, or
91 * -1 to let ovs-ofctl choose the default. */
92 static int preferred_packet_in_format = -1;
94 /* -m, --more: Additional verbosity for ofp-print functions. */
97 /* --timestamp: Print a timestamp before each received packet on "monitor" and
99 static bool timestamp;
101 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
103 static char *unixctl_path;
105 /* --sort, --rsort: Sort order. */
106 enum sort_order { SORT_ASC, SORT_DESC };
107 struct sort_criterion {
108 const struct mf_field *field; /* NULL means to sort by priority. */
109 enum sort_order order;
111 static struct sort_criterion *criteria;
112 static size_t n_criteria, allocated_criteria;
114 static const struct ovs_cmdl_command *get_all_commands(void);
116 OVS_NO_RETURN static void usage(void);
117 static void parse_options(int argc, char *argv[]);
119 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
120 struct ofpbuf **replyp,
121 struct ofputil_flow_stats *,
122 struct ofpbuf *ofpacts);
124 main(int argc, char *argv[])
126 struct ovs_cmdl_context ctx = { .argc = 0, };
127 set_program_name(argv[0]);
128 service_start(&argc, &argv);
129 parse_options(argc, argv);
130 fatal_ignore_sigpipe();
131 ctx.argc = argc - optind;
132 ctx.argv = argv + optind;
134 daemon_become_new_user(false);
135 ovs_cmdl_run_command(&ctx, get_all_commands());
140 add_sort_criterion(enum sort_order order, const char *field)
142 struct sort_criterion *sc;
144 if (n_criteria >= allocated_criteria) {
145 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
148 sc = &criteria[n_criteria++];
149 if (!field || !strcasecmp(field, "priority")) {
152 sc->field = mf_from_name(field);
154 ovs_fatal(0, "%s: unknown field name", field);
161 parse_options(int argc, char *argv[])
164 OPT_STRICT = UCHAR_MAX + 1,
172 OFP_VERSION_OPTION_ENUMS,
175 static const struct option long_options[] = {
176 {"timeout", required_argument, NULL, 't'},
177 {"strict", no_argument, NULL, OPT_STRICT},
178 {"readd", no_argument, NULL, OPT_READD},
179 {"flow-format", required_argument, NULL, 'F'},
180 {"packet-in-format", required_argument, NULL, 'P'},
181 {"more", no_argument, NULL, 'm'},
182 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
183 {"sort", optional_argument, NULL, OPT_SORT},
184 {"rsort", optional_argument, NULL, OPT_RSORT},
185 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
186 {"help", no_argument, NULL, 'h'},
187 {"option", no_argument, NULL, 'o'},
188 {"bundle", no_argument, NULL, OPT_BUNDLE},
190 OFP_VERSION_LONG_OPTIONS,
192 STREAM_SSL_LONG_OPTIONS,
195 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
197 enum ofputil_protocol version_protocols;
199 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
200 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
201 * requests, but those requests have subtly different semantics in
202 * different OpenFlow versions. For example:
204 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
205 * existing flow to modify adds a new flow.
207 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
208 * existing flow to modify adds a new flow, but only if the mod-flow
209 * did not match on the flow cookie.
211 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
214 set_allowed_ofp_versions("OpenFlow10");
217 unsigned long int timeout;
220 c = getopt_long(argc, argv, short_options, long_options, NULL);
227 timeout = strtoul(optarg, NULL, 10);
229 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
237 allowed_protocols = ofputil_protocols_from_string(optarg);
238 if (!allowed_protocols) {
239 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
244 preferred_packet_in_format =
245 ofputil_packet_in_format_from_string(optarg);
246 if (preferred_packet_in_format < 0) {
247 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
259 ovs_cmdl_print_options(long_options);
279 add_sort_criterion(SORT_ASC, optarg);
283 add_sort_criterion(SORT_DESC, optarg);
287 unixctl_path = optarg;
290 DAEMON_OPTION_HANDLERS
291 OFP_VERSION_OPTION_HANDLERS
293 STREAM_SSL_OPTION_HANDLERS
304 /* Always do a final sort pass based on priority. */
305 add_sort_criterion(SORT_DESC, "priority");
310 /* Implicit OpenFlow 1.4 with the '--bundle' option. */
312 /* Add implicit allowance for OpenFlow 1.4. */
313 add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
314 OFPUTIL_P_OF14_OXM));
315 /* Remove all prior versions. */
316 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
319 versions = get_allowed_ofp_versions();
320 version_protocols = ofputil_protocols_from_version_bitmap(versions);
321 if (!(allowed_protocols & version_protocols)) {
322 char *protocols = ofputil_protocols_to_string(allowed_protocols);
323 struct ds version_s = DS_EMPTY_INITIALIZER;
325 ofputil_format_version_bitmap_names(&version_s, versions);
326 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
327 "any of the enabled flow formats (%s). (Use -O to enable "
328 "additional OpenFlow versions or -F to enable additional "
329 "flow formats.)", ds_cstr(&version_s), protocols);
331 allowed_protocols &= version_protocols;
332 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
339 printf("%s: OpenFlow switch management utility\n"
340 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
341 "\nFor OpenFlow switches:\n"
342 " show SWITCH show OpenFlow information\n"
343 " dump-desc SWITCH print switch description\n"
344 " dump-tables SWITCH print table stats\n"
345 " dump-table-features SWITCH print table features\n"
346 " dump-table-desc SWITCH print table description (OF1.4+)\n"
347 " mod-port SWITCH IFACE ACT modify port behavior\n"
348 " mod-table SWITCH MOD modify flow table behavior\n"
349 " OF1.1/1.2 MOD: controller, continue, drop\n"
350 " OF1.4+ MOD: evict, noevict, vacancy:low,high, novacancy\n"
351 " get-frags SWITCH print fragment handling behavior\n"
352 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
353 " FRAG_MODE: normal, drop, reassemble, nx-match\n"
354 " dump-ports SWITCH [PORT] print port statistics\n"
355 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
356 " dump-flows SWITCH print all flow entries\n"
357 " dump-flows SWITCH FLOW print matching FLOWs\n"
358 " dump-aggregate SWITCH print aggregate flow statistics\n"
359 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
360 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
361 " add-flow SWITCH FLOW add flow described by FLOW\n"
362 " add-flows SWITCH FILE add flows from FILE\n"
363 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
364 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
365 " replace-flows SWITCH FILE replace flows with those in FILE\n"
366 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
367 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
368 " execute ACTIONS on PACKET\n"
369 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
370 " print packets received from SWITCH\n"
371 " snoop SWITCH snoop on SWITCH and its controller\n"
372 " add-group SWITCH GROUP add group described by GROUP\n"
373 " add-groups SWITCH FILE add group from FILE\n"
374 " mod-group SWITCH GROUP modify specific group\n"
375 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
376 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
377 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
378 " dump-group-features SWITCH print group features\n"
379 " dump-groups SWITCH [GROUP] print group description\n"
380 " dump-group-stats SWITCH [GROUP] print group statistics\n"
381 " queue-get-config SWITCH [PORT] print queue config for PORT\n"
382 " add-meter SWITCH METER add meter described by METER\n"
383 " mod-meter SWITCH METER modify specific METER\n"
384 " del-meter SWITCH METER delete METER\n"
385 " del-meters SWITCH delete all meters\n"
386 " dump-meter SWITCH METER print METER configuration\n"
387 " dump-meters SWITCH print all meter configuration\n"
388 " meter-stats SWITCH [METER] print meter statistics\n"
389 " meter-features SWITCH print meter features\n"
390 " add-tlv-map SWITCH MAP add TLV option MAPpings\n"
391 " del-tlv-map SWITCH [MAP] delete TLV option MAPpings\n"
392 " dump-tlv-map SWITCH print TLV option mappings\n"
393 "\nFor OpenFlow switches and controllers:\n"
394 " probe TARGET probe whether TARGET is up\n"
395 " ping TARGET [N] latency of N-byte echos\n"
396 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
397 "SWITCH or TARGET is an active OpenFlow connection method.\n"
398 "\nOther commands:\n"
399 " ofp-parse FILE print messages read from FILE\n"
400 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
401 program_name, program_name);
402 vconn_usage(true, false, false);
406 printf("\nOther options:\n"
407 " --strict use strict match for flow commands\n"
408 " --readd replace flows that haven't changed\n"
409 " -F, --flow-format=FORMAT force particular flow format\n"
410 " -P, --packet-in-format=FRMT force particular packet in format\n"
411 " -m, --more be more verbose printing OpenFlow\n"
412 " --timestamp (monitor, snoop) print timestamps\n"
413 " -t, --timeout=SECS give up after SECS seconds\n"
414 " --sort[=field] sort in ascending order\n"
415 " --rsort[=field] sort in descending order\n"
416 " --unixctl=SOCKET set control socket name\n"
417 " -h, --help display this help message\n"
418 " -V, --version display version information\n");
423 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
424 const char *argv[] OVS_UNUSED, void *exiting_)
426 bool *exiting = exiting_;
428 unixctl_command_reply(conn, NULL);
431 static void run(int retval, const char *message, ...)
432 OVS_PRINTF_FORMAT(2, 3);
435 run(int retval, const char *message, ...)
440 va_start(args, message);
441 ovs_fatal_valist(retval, message, args);
445 /* Generic commands. */
448 open_vconn_socket(const char *name, struct vconn **vconnp)
450 char *vconn_name = xasprintf("unix:%s", name);
453 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
455 if (error && error != ENOENT) {
456 ovs_fatal(0, "%s: failed to open socket (%s)", name,
457 ovs_strerror(error));
464 enum open_target { MGMT, SNOOP };
466 static enum ofputil_protocol
467 open_vconn__(const char *name, enum open_target target,
468 struct vconn **vconnp)
470 const char *suffix = target == MGMT ? "mgmt" : "snoop";
471 char *datapath_name, *datapath_type, *socket_name;
472 enum ofputil_protocol protocol;
477 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
479 ofproto_parse_name(name, &datapath_name, &datapath_type);
480 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
484 if (strchr(name, ':')) {
485 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
486 "connecting to %s", name);
487 } else if (!open_vconn_socket(name, vconnp)) {
489 } else if (!open_vconn_socket(bridge_path, vconnp)) {
491 } else if (!open_vconn_socket(socket_name, vconnp)) {
494 ovs_fatal(0, "%s is not a bridge or a socket", name);
497 if (target == SNOOP) {
498 vconn_set_recv_any_version(*vconnp);
504 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
505 error = vconn_connect_block(*vconnp);
507 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
508 ovs_strerror(error));
511 ofp_version = vconn_get_version(*vconnp);
512 protocol = ofputil_protocol_from_ofp_version(ofp_version);
514 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
520 static enum ofputil_protocol
521 open_vconn(const char *name, struct vconn **vconnp)
523 return open_vconn__(name, MGMT, vconnp);
527 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
529 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
533 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
535 const struct ofp_header *oh = request->data;
536 if (ofpmsg_is_stat_request(oh)) {
537 ovs_be32 send_xid = oh->xid;
538 enum ofpraw request_raw;
539 enum ofpraw reply_raw;
542 ofpraw_decode_partial(&request_raw, request->data, request->size);
543 reply_raw = ofpraw_stats_request_to_reply(request_raw, oh->version);
545 send_openflow_buffer(vconn, request);
548 struct ofpbuf *reply;
550 run(vconn_recv_block(vconn, &reply),
551 "OpenFlow packet receive failed");
552 recv_xid = ((struct ofp_header *) reply->data)->xid;
553 if (send_xid == recv_xid) {
556 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
558 ofpraw_decode(&raw, reply->data);
559 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
561 } else if (raw == reply_raw) {
562 done = !ofpmp_more(reply->data);
564 ovs_fatal(0, "received bad reply: %s",
565 ofp_to_string(reply->data, reply->size,
569 VLOG_DBG("received reply with xid %08"PRIx32" "
570 "!= expected %08"PRIx32, recv_xid, send_xid);
572 ofpbuf_delete(reply);
575 struct ofpbuf *reply;
577 run(vconn_transact(vconn, request, &reply), "talking to %s",
578 vconn_get_name(vconn));
579 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
580 ofpbuf_delete(reply);
585 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
587 struct ofpbuf *request;
590 open_vconn(vconn_name, &vconn);
591 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
592 dump_transaction(vconn, request);
596 /* Sends all of the 'requests', which should be requests that only have replies
597 * if an error occurs, and waits for them to succeed or fail. If an error does
598 * occur, prints it and exits with an error.
600 * Destroys all of the 'requests'. */
602 transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
604 struct ofpbuf *reply;
606 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
607 "talking to %s", vconn_get_name(vconn));
609 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
612 ofpbuf_delete(reply);
616 bundle_error_reporter(const struct ofp_header *oh)
618 ofp_print(stderr, oh, ntohs(oh->length), verbosity + 1);
623 bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
625 run(vconn_bundle_transact(vconn, requests, flags, bundle_error_reporter),
626 "talking to %s", vconn_get_name(vconn));
629 /* Sends 'request', which should be a request that only has a reply if an error
630 * occurs, and waits for it to succeed or fail. If an error does occur, prints
631 * it and exits with an error.
633 * Destroys 'request'. */
635 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
637 struct ovs_list requests;
639 list_init(&requests);
640 list_push_back(&requests, &request->list_node);
641 transact_multiple_noreply(vconn, &requests);
645 fetch_switch_config(struct vconn *vconn, struct ofputil_switch_config *config)
647 struct ofpbuf *request;
648 struct ofpbuf *reply;
651 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
652 vconn_get_version(vconn), 0);
653 run(vconn_transact(vconn, request, &reply),
654 "talking to %s", vconn_get_name(vconn));
656 if (ofptype_decode(&type, reply->data)
657 || type != OFPTYPE_GET_CONFIG_REPLY) {
658 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
660 ofputil_decode_get_config_reply(reply->data, config);
661 ofpbuf_delete(reply);
665 set_switch_config(struct vconn *vconn,
666 const struct ofputil_switch_config *config)
668 enum ofp_version version = vconn_get_version(vconn);
669 transact_noreply(vconn, ofputil_encode_set_config(config, version));
673 ofctl_show(struct ovs_cmdl_context *ctx)
675 const char *vconn_name = ctx->argv[1];
676 enum ofp_version version;
678 struct ofpbuf *request;
679 struct ofpbuf *reply;
682 open_vconn(vconn_name, &vconn);
683 version = vconn_get_version(vconn);
684 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
685 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
687 has_ports = ofputil_switch_features_has_ports(reply);
688 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
689 ofpbuf_delete(reply);
692 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
693 dump_transaction(vconn, request);
695 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
700 ofctl_dump_desc(struct ovs_cmdl_context *ctx)
702 dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
706 ofctl_dump_tables(struct ovs_cmdl_context *ctx)
708 dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
712 ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
714 struct ofpbuf *request;
717 open_vconn(ctx->argv[1], &vconn);
718 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
720 /* The following is similar to dump_trivial_transaction(), but it
721 * maintains the previous 'ofputil_table_features' from one stats reply
722 * message to the next, which allows duplication to be eliminated in the
723 * output across messages. Otherwise the output is much larger and harder
724 * to read, because only 17 or so ofputil_table_features elements fit in a
725 * single 64 kB OpenFlow message and therefore you get a ton of repetition
726 * (every 17th element is printed in full instead of abbreviated). */
728 const struct ofp_header *request_oh = request->data;
729 ovs_be32 send_xid = request_oh->xid;
732 struct ofputil_table_features prev;
735 send_openflow_buffer(vconn, request);
738 struct ofpbuf *reply;
740 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
741 recv_xid = ((struct ofp_header *) reply->data)->xid;
742 if (send_xid == recv_xid) {
745 error = ofptype_decode(&type, reply->data);
747 ovs_fatal(0, "decode error: %s", ofperr_get_name(error));
748 } else if (type == OFPTYPE_ERROR) {
749 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
751 } else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) {
752 done = !ofpmp_more(reply->data);
754 struct ofputil_table_features tf;
757 retval = ofputil_decode_table_features(reply, &tf, true);
760 ovs_fatal(0, "decode error: %s",
761 ofperr_get_name(retval));
766 struct ds s = DS_EMPTY_INITIALIZER;
767 ofp_print_table_features(&s, &tf, n ? &prev : NULL,
776 ovs_fatal(0, "received bad reply: %s",
777 ofp_to_string(reply->data, reply->size,
781 VLOG_DBG("received reply with xid %08"PRIx32" "
782 "!= expected %08"PRIx32, recv_xid, send_xid);
784 ofpbuf_delete(reply);
791 ofctl_dump_table_desc(struct ovs_cmdl_context *ctx)
793 struct ofpbuf *request;
796 open_vconn(ctx->argv[1], &vconn);
797 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
799 dump_transaction(vconn, request);
807 str_to_ofp(const char *s, ofp_port_t *ofp_port)
812 ret = str_to_uint(s, 10, &port_);
813 *ofp_port = u16_to_ofp(port_);
817 struct port_iterator {
820 enum { PI_FEATURES, PI_PORT_DESC } variant;
821 struct ofpbuf *reply;
827 port_iterator_fetch_port_desc(struct port_iterator *pi)
829 pi->variant = PI_PORT_DESC;
832 struct ofpbuf *rq = ofputil_encode_port_desc_stats_request(
833 vconn_get_version(pi->vconn), OFPP_ANY);
834 pi->send_xid = ((struct ofp_header *) rq->data)->xid;
835 send_openflow_buffer(pi->vconn, rq);
839 port_iterator_fetch_features(struct port_iterator *pi)
841 pi->variant = PI_FEATURES;
843 /* Fetch the switch's ofp_switch_features. */
844 enum ofp_version version = vconn_get_version(pi->vconn);
845 struct ofpbuf *rq = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
846 run(vconn_transact(pi->vconn, rq, &pi->reply),
847 "talking to %s", vconn_get_name(pi->vconn));
850 if (ofptype_decode(&type, pi->reply->data)
851 || type != OFPTYPE_FEATURES_REPLY) {
852 ovs_fatal(0, "%s: received bad features reply",
853 vconn_get_name(pi->vconn));
855 if (!ofputil_switch_features_has_ports(pi->reply)) {
856 /* The switch features reply does not contain a complete list of ports.
857 * Probably, there are more ports than will fit into a single 64 kB
858 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
860 ofpbuf_delete(pi->reply);
862 port_iterator_fetch_port_desc(pi);
866 struct ofputil_switch_features features;
867 enum ofperr error = ofputil_pull_switch_features(pi->reply, &features);
869 ovs_fatal(0, "%s: failed to decode features reply (%s)",
870 vconn_get_name(pi->vconn), ofperr_to_string(error));
874 /* Initializes 'pi' to prepare for iterating through all of the ports on the
875 * OpenFlow switch to which 'vconn' is connected.
877 * During iteration, the client should not make other use of 'vconn', because
878 * that can cause other messages to be interleaved with the replies used by the
879 * iterator and thus some ports may be missed or a hang can occur. */
881 port_iterator_init(struct port_iterator *pi, struct vconn *vconn)
883 memset(pi, 0, sizeof *pi);
885 if (vconn_get_version(vconn) < OFP13_VERSION) {
886 port_iterator_fetch_features(pi);
888 port_iterator_fetch_port_desc(pi);
892 /* Obtains the next port from 'pi'. On success, initializes '*pp' with the
893 * port's details and returns true, otherwise (if all the ports have already
894 * been seen), returns false. */
896 port_iterator_next(struct port_iterator *pi, struct ofputil_phy_port *pp)
900 int retval = ofputil_pull_phy_port(vconn_get_version(pi->vconn),
904 } else if (retval != EOF) {
905 ovs_fatal(0, "received bad reply: %s",
906 ofp_to_string(pi->reply->data, pi->reply->size,
911 if (pi->variant == PI_FEATURES || !pi->more) {
917 ofpbuf_delete(pi->reply);
918 run(vconn_recv_block(pi->vconn, &pi->reply),
919 "OpenFlow receive failed");
920 recv_xid = ((struct ofp_header *) pi->reply->data)->xid;
921 } while (pi->send_xid != recv_xid);
923 struct ofp_header *oh = pi->reply->data;
925 if (ofptype_pull(&type, pi->reply)
926 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
927 ovs_fatal(0, "received bad reply: %s",
928 ofp_to_string(pi->reply->data, pi->reply->size,
932 pi->more = (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
936 /* Destroys iterator 'pi'. */
938 port_iterator_destroy(struct port_iterator *pi)
941 while (pi->variant == PI_PORT_DESC && pi->more) {
942 /* Drain vconn's queue of any other replies for this request. */
943 struct ofputil_phy_port pp;
944 port_iterator_next(pi, &pp);
947 ofpbuf_delete(pi->reply);
951 /* Opens a connection to 'vconn_name', fetches the port structure for
952 * 'port_name' (which may be a port name or number), and copies it into
955 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
956 struct ofputil_phy_port *pp)
962 /* Try to interpret the argument as a port number. */
963 if (!str_to_ofp(port_name, &port_no)) {
967 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
968 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
969 * into the OFPST_PORT_DESC reply. Try it the correct way. */
970 open_vconn(vconn_name, &vconn);
971 struct port_iterator pi;
972 for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, pp); ) {
973 if (port_no != OFPP_NONE
974 ? port_no == pp->port_no
975 : !strcmp(pp->name, port_name)) {
980 port_iterator_destroy(&pi);
984 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
988 /* Returns the port number corresponding to 'port_name' (which may be a port
989 * name or number) within the switch 'vconn_name'. */
991 str_to_port_no(const char *vconn_name, const char *port_name)
995 if (ofputil_port_from_string(port_name, &port_no)) {
998 struct ofputil_phy_port pp;
1000 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
1006 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
1007 enum ofputil_protocol *cur)
1010 struct ofpbuf *request, *reply;
1011 enum ofputil_protocol next;
1013 request = ofputil_encode_set_protocol(*cur, want, &next);
1015 return *cur == want;
1018 run(vconn_transact_noreply(vconn, request, &reply),
1019 "talking to %s", vconn_get_name(vconn));
1021 char *s = ofp_to_string(reply->data, reply->size, 2);
1022 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
1023 vconn_get_name(vconn), s);
1025 ofpbuf_delete(reply);
1033 static enum ofputil_protocol
1034 set_protocol_for_flow_dump(struct vconn *vconn,
1035 enum ofputil_protocol cur_protocol,
1036 enum ofputil_protocol usable_protocols)
1041 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
1042 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
1043 if (f & usable_protocols & allowed_protocols
1044 && try_set_protocol(vconn, f, &cur_protocol)) {
1049 usable_s = ofputil_protocols_to_string(usable_protocols);
1050 if (usable_protocols & allowed_protocols) {
1051 ovs_fatal(0, "switch does not support any of the usable flow "
1052 "formats (%s)", usable_s);
1054 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1055 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1056 "allowed flow formats (%s)", usable_s, allowed_s);
1060 static struct vconn *
1061 prepare_dump_flows(int argc, char *argv[], bool aggregate,
1062 struct ofpbuf **requestp)
1064 enum ofputil_protocol usable_protocols, protocol;
1065 struct ofputil_flow_stats_request fsr;
1066 struct vconn *vconn;
1069 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
1070 argc > 2 ? argv[2] : "",
1073 ovs_fatal(0, "%s", error);
1076 protocol = open_vconn(argv[1], &vconn);
1077 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1078 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
1083 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
1085 struct ofpbuf *request;
1086 struct vconn *vconn;
1088 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
1089 dump_transaction(vconn, request);
1094 compare_flows(const void *afs_, const void *bfs_)
1096 const struct ofputil_flow_stats *afs = afs_;
1097 const struct ofputil_flow_stats *bfs = bfs_;
1098 const struct match *a = &afs->match;
1099 const struct match *b = &bfs->match;
1100 const struct sort_criterion *sc;
1102 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
1103 const struct mf_field *f = sc->field;
1107 int a_pri = afs->priority;
1108 int b_pri = bfs->priority;
1109 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
1113 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
1114 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
1116 /* Skip the test for sc->order, so that missing fields always
1117 * sort to the end whether we're sorting in ascending or
1118 * descending order. */
1119 return ina ? -1 : 1;
1121 union mf_value aval, bval;
1123 mf_get_value(f, &a->flow, &aval);
1124 mf_get_value(f, &b->flow, &bval);
1125 ret = memcmp(&aval, &bval, f->n_bytes);
1130 return sc->order == SORT_ASC ? ret : -ret;
1138 ofctl_dump_flows(struct ovs_cmdl_context *ctx)
1141 ofctl_dump_flows__(ctx->argc, ctx->argv, false);
1144 struct ofputil_flow_stats *fses;
1145 size_t n_fses, allocated_fses;
1146 struct ofpbuf *request;
1147 struct ofpbuf ofpacts;
1148 struct ofpbuf *reply;
1149 struct vconn *vconn;
1154 vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request);
1155 send_xid = ((struct ofp_header *) request->data)->xid;
1156 send_openflow_buffer(vconn, request);
1159 n_fses = allocated_fses = 0;
1161 ofpbuf_init(&ofpacts, 0);
1163 struct ofputil_flow_stats *fs;
1165 if (n_fses >= allocated_fses) {
1166 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1170 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1174 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1177 ofpbuf_uninit(&ofpacts);
1179 qsort(fses, n_fses, sizeof *fses, compare_flows);
1182 for (i = 0; i < n_fses; i++) {
1184 ofp_print_flow_stats(&s, &fses[i]);
1189 for (i = 0; i < n_fses; i++) {
1190 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1199 ofctl_dump_aggregate(struct ovs_cmdl_context *ctx)
1201 ofctl_dump_flows__(ctx->argc, ctx->argv, true);
1205 ofctl_queue_stats(struct ovs_cmdl_context *ctx)
1207 struct ofpbuf *request;
1208 struct vconn *vconn;
1209 struct ofputil_queue_stats_request oqs;
1211 open_vconn(ctx->argv[1], &vconn);
1213 if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) {
1214 oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1216 oqs.port_no = OFPP_ANY;
1218 if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) {
1219 oqs.queue_id = atoi(ctx->argv[3]);
1221 oqs.queue_id = OFPQ_ALL;
1224 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1225 dump_transaction(vconn, request);
1230 ofctl_queue_get_config(struct ovs_cmdl_context *ctx)
1232 const char *vconn_name = ctx->argv[1];
1233 const char *port_name = ctx->argc > 2 ? ctx->argv[2] : "any";
1234 ofp_port_t port = str_to_port_no(vconn_name, port_name);
1235 const char *queue_name = ctx->argc > 3 ? ctx->argv[3] : "all";
1236 uint32_t queue = (!strcasecmp(queue_name, "all")
1238 : atoi(queue_name));
1239 struct vconn *vconn;
1241 enum ofputil_protocol protocol = open_vconn(vconn_name, &vconn);
1242 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
1243 if (port == OFPP_ANY && version == OFP10_VERSION) {
1244 /* The user requested all queues on all ports. OpenFlow 1.0 only
1245 * supports getting queues for an individual port, so to implement the
1246 * user's request we have to get a list of all the ports.
1248 * We use a second vconn to avoid having to accumulate a list of all of
1250 struct vconn *vconn2;
1251 enum ofputil_protocol protocol2 = open_vconn(vconn_name, &vconn2);
1252 enum ofp_version version2 = ofputil_protocol_to_ofp_version(protocol2);
1254 struct port_iterator pi;
1255 struct ofputil_phy_port pp;
1256 for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, &pp); ) {
1257 if (ofp_to_u16(pp.port_no) < ofp_to_u16(OFPP_MAX)) {
1258 dump_transaction(vconn2,
1259 ofputil_encode_queue_get_config_request(
1260 version2, pp.port_no, queue));
1263 port_iterator_destroy(&pi);
1264 vconn_close(vconn2);
1266 dump_transaction(vconn, ofputil_encode_queue_get_config_request(
1267 version, port, queue));
1272 static enum ofputil_protocol
1273 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1274 enum ofputil_protocol usable_protocols)
1276 enum ofputil_protocol cur_protocol;
1280 if (!(usable_protocols & allowed_protocols)) {
1281 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1282 usable_s = ofputil_protocols_to_string(usable_protocols);
1283 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1284 "allowed flow formats (%s)", usable_s, allowed_s);
1287 /* If the initial flow format is allowed and usable, keep it. */
1288 cur_protocol = open_vconn(remote, vconnp);
1289 if (usable_protocols & allowed_protocols & cur_protocol) {
1290 return cur_protocol;
1293 /* Otherwise try each flow format in turn. */
1294 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1295 enum ofputil_protocol f = 1 << i;
1297 if (f != cur_protocol
1298 && f & usable_protocols & allowed_protocols
1299 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1304 usable_s = ofputil_protocols_to_string(usable_protocols);
1305 ovs_fatal(0, "switch does not support any of the usable flow "
1306 "formats (%s)", usable_s);
1310 bundle_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1311 size_t n_fms, enum ofputil_protocol usable_protocols)
1313 enum ofputil_protocol protocol;
1314 struct vconn *vconn;
1315 struct ovs_list requests;
1318 list_init(&requests);
1320 /* Bundles need OpenFlow 1.4+. */
1321 usable_protocols &= OFPUTIL_P_OF14_UP;
1322 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1324 for (i = 0; i < n_fms; i++) {
1325 struct ofputil_flow_mod *fm = &fms[i];
1326 struct ofpbuf *request = ofputil_encode_flow_mod(fm, protocol);
1328 list_push_back(&requests, &request->list_node);
1329 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1332 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
1333 ofpbuf_list_delete(&requests);
1338 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1339 size_t n_fms, enum ofputil_protocol usable_protocols)
1341 enum ofputil_protocol protocol;
1342 struct vconn *vconn;
1346 bundle_flow_mod__(remote, fms, n_fms, usable_protocols);
1350 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1352 for (i = 0; i < n_fms; i++) {
1353 struct ofputil_flow_mod *fm = &fms[i];
1355 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1356 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1362 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], int command)
1364 enum ofputil_protocol usable_protocols;
1365 struct ofputil_flow_mod *fms = NULL;
1369 if (command == OFPFC_ADD) {
1370 /* Allow the file to specify a mix of commands. If none specified at
1371 * the beginning of any given line, then the default is OFPFC_ADD, so
1372 * this is backwards compatible. */
1375 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1378 ovs_fatal(0, "%s", error);
1380 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1385 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1387 if (argc > 2 && !strcmp(argv[2], "-")) {
1388 ofctl_flow_mod_file(argc, argv, command);
1390 struct ofputil_flow_mod fm;
1392 enum ofputil_protocol usable_protocols;
1394 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1397 ovs_fatal(0, "%s", error);
1399 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1404 ofctl_add_flow(struct ovs_cmdl_context *ctx)
1406 ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD);
1410 ofctl_add_flows(struct ovs_cmdl_context *ctx)
1412 ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD);
1416 ofctl_mod_flows(struct ovs_cmdl_context *ctx)
1418 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1422 ofctl_del_flows(struct ovs_cmdl_context *ctx)
1424 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1428 set_packet_in_format(struct vconn *vconn,
1429 enum nx_packet_in_format packet_in_format)
1431 struct ofpbuf *spif;
1433 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1435 transact_noreply(vconn, spif);
1436 VLOG_DBG("%s: using user-specified packet in format %s",
1437 vconn_get_name(vconn),
1438 ofputil_packet_in_format_to_string(packet_in_format));
1442 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1444 struct ofputil_switch_config config;
1446 fetch_switch_config(vconn, &config);
1447 if (!config.invalid_ttl_to_controller) {
1448 config.invalid_ttl_to_controller = 1;
1449 set_switch_config(vconn, &config);
1451 /* Then retrieve the configuration to see if it really took. OpenFlow
1452 * has ill-defined error reporting for bad flags, so this is about the
1453 * best we can do. */
1454 fetch_switch_config(vconn, &config);
1455 if (!config.invalid_ttl_to_controller) {
1456 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1457 "switch probably doesn't support this flag)");
1463 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1464 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1465 * an error message and stores NULL in '*msgp'. */
1467 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1469 struct ofp_header *oh;
1472 msg = ofpbuf_new(strlen(hex) / 2);
1475 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1477 return "Trailing garbage in hex data";
1480 if (msg->size < sizeof(struct ofp_header)) {
1482 return "Message too short for OpenFlow";
1486 if (msg->size != ntohs(oh->length)) {
1488 return "Message size does not match length in OpenFlow header";
1496 ofctl_send(struct unixctl_conn *conn, int argc,
1497 const char *argv[], void *vconn_)
1499 struct vconn *vconn = vconn_;
1506 for (i = 1; i < argc; i++) {
1507 const char *error_msg;
1511 error_msg = openflow_from_hex(argv[i], &msg);
1513 ds_put_format(&reply, "%s\n", error_msg);
1518 fprintf(stderr, "send: ");
1519 ofp_print(stderr, msg->data, msg->size, verbosity);
1521 error = vconn_send_block(vconn, msg);
1524 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1527 ds_put_cstr(&reply, "sent\n");
1532 unixctl_command_reply(conn, ds_cstr(&reply));
1534 unixctl_command_reply_error(conn, ds_cstr(&reply));
1539 struct barrier_aux {
1540 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1541 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1545 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1546 const char *argv[] OVS_UNUSED, void *aux_)
1548 struct barrier_aux *aux = aux_;
1553 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1557 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1558 error = vconn_send_block(aux->vconn, msg);
1561 unixctl_command_reply_error(conn, ovs_strerror(error));
1568 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1569 const char *argv[], void *aux OVS_UNUSED)
1573 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1575 unixctl_command_reply_error(conn, ovs_strerror(errno));
1580 dup2(fd, STDERR_FILENO);
1582 unixctl_command_reply(conn, NULL);
1586 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1587 const char *argv[] OVS_UNUSED, void *blocked_)
1589 bool *blocked = blocked_;
1593 unixctl_command_reply(conn, NULL);
1595 unixctl_command_reply(conn, "already blocking");
1600 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1601 const char *argv[] OVS_UNUSED, void *blocked_)
1603 bool *blocked = blocked_;
1607 unixctl_command_reply(conn, NULL);
1609 unixctl_command_reply(conn, "already unblocked");
1613 /* Prints to stdout all of the messages received on 'vconn'.
1615 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1616 * received on 'vconn'. */
1618 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1620 struct barrier_aux barrier_aux = { vconn, NULL };
1621 struct unixctl_server *server;
1622 bool exiting = false;
1623 bool blocked = false;
1626 daemon_save_fd(STDERR_FILENO);
1627 daemonize_start(false);
1628 error = unixctl_server_create(unixctl_path, &server);
1630 ovs_fatal(error, "failed to create unixctl server");
1632 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1633 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1635 unixctl_command_register("ofctl/barrier", "", 0, 0,
1636 ofctl_barrier, &barrier_aux);
1637 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1638 ofctl_set_output_file, NULL);
1640 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1641 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1644 daemonize_complete();
1650 unixctl_server_run(server);
1655 retval = vconn_recv(vconn, &b);
1656 if (retval == EAGAIN) {
1659 run(retval, "vconn_recv");
1662 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1663 time_wall_msec(), true);
1668 ofptype_decode(&type, b->data);
1669 ofp_print(stderr, b->data, b->size, verbosity + 2);
1672 switch ((int) type) {
1673 case OFPTYPE_BARRIER_REPLY:
1674 if (barrier_aux.conn) {
1675 unixctl_command_reply(barrier_aux.conn, NULL);
1676 barrier_aux.conn = NULL;
1680 case OFPTYPE_ECHO_REQUEST:
1681 if (reply_to_echo_requests) {
1682 struct ofpbuf *reply;
1684 reply = make_echo_reply(b->data);
1685 retval = vconn_send_block(vconn, reply);
1687 ovs_fatal(retval, "failed to send echo reply");
1700 vconn_run_wait(vconn);
1702 vconn_recv_wait(vconn);
1704 unixctl_server_wait(server);
1708 unixctl_server_destroy(server);
1712 ofctl_monitor(struct ovs_cmdl_context *ctx)
1714 struct vconn *vconn;
1716 enum ofputil_protocol usable_protocols;
1718 /* If the user wants the invalid_ttl_to_controller feature, limit the
1719 * OpenFlow versions to those that support that feature. (Support in
1720 * OpenFlow 1.0 is an Open vSwitch extension.) */
1721 for (i = 2; i < ctx->argc; i++) {
1722 if (!strcmp(ctx->argv[i], "invalid_ttl")) {
1723 uint32_t usable_versions = ((1u << OFP10_VERSION) |
1724 (1u << OFP11_VERSION) |
1725 (1u << OFP12_VERSION));
1726 uint32_t allowed_versions = get_allowed_ofp_versions();
1727 if (!(allowed_versions & usable_versions)) {
1728 struct ds versions = DS_EMPTY_INITIALIZER;
1729 ofputil_format_version_bitmap_names(&versions,
1731 ovs_fatal(0, "invalid_ttl requires one of the OpenFlow "
1732 "versions %s but none is enabled (use -O)",
1733 ds_cstr(&versions));
1735 mask_allowed_ofp_versions(usable_versions);
1740 open_vconn(ctx->argv[1], &vconn);
1741 for (i = 2; i < ctx->argc; i++) {
1742 const char *arg = ctx->argv[i];
1744 if (isdigit((unsigned char) *arg)) {
1745 struct ofputil_switch_config config;
1747 fetch_switch_config(vconn, &config);
1748 config.miss_send_len = atoi(arg);
1749 set_switch_config(vconn, &config);
1750 } else if (!strcmp(arg, "invalid_ttl")) {
1751 monitor_set_invalid_ttl_to_controller(vconn);
1752 } else if (!strncmp(arg, "watch:", 6)) {
1753 struct ofputil_flow_monitor_request fmr;
1757 error = parse_flow_monitor_request(&fmr, arg + 6,
1760 ovs_fatal(0, "%s", error);
1763 msg = ofpbuf_new(0);
1764 ofputil_append_flow_monitor_request(&fmr, msg);
1765 dump_transaction(vconn, msg);
1768 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1772 if (preferred_packet_in_format >= 0) {
1773 set_packet_in_format(vconn, preferred_packet_in_format);
1775 enum ofp_version version = vconn_get_version(vconn);
1778 case OFP10_VERSION: {
1779 struct ofpbuf *spif, *reply;
1781 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1783 run(vconn_transact_noreply(vconn, spif, &reply),
1784 "talking to %s", vconn_get_name(vconn));
1786 char *s = ofp_to_string(reply->data, reply->size, 2);
1787 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1788 " replied: %s. Falling back to the switch default.",
1789 vconn_get_name(vconn), s);
1791 ofpbuf_delete(reply);
1806 monitor_vconn(vconn, true);
1810 ofctl_snoop(struct ovs_cmdl_context *ctx)
1812 struct vconn *vconn;
1814 open_vconn__(ctx->argv[1], SNOOP, &vconn);
1815 monitor_vconn(vconn, false);
1819 ofctl_dump_ports(struct ovs_cmdl_context *ctx)
1821 struct ofpbuf *request;
1822 struct vconn *vconn;
1825 open_vconn(ctx->argv[1], &vconn);
1826 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1827 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1828 dump_transaction(vconn, request);
1833 ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx)
1835 struct ofpbuf *request;
1836 struct vconn *vconn;
1839 open_vconn(ctx->argv[1], &vconn);
1840 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1841 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1843 dump_transaction(vconn, request);
1848 ofctl_probe(struct ovs_cmdl_context *ctx)
1850 struct ofpbuf *request;
1851 struct vconn *vconn;
1852 struct ofpbuf *reply;
1854 open_vconn(ctx->argv[1], &vconn);
1855 request = make_echo_request(vconn_get_version(vconn));
1856 run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]);
1857 if (reply->size != sizeof(struct ofp_header)) {
1858 ovs_fatal(0, "reply does not match request");
1860 ofpbuf_delete(reply);
1865 ofctl_packet_out(struct ovs_cmdl_context *ctx)
1867 enum ofputil_protocol protocol;
1868 struct ofputil_packet_out po;
1869 struct ofpbuf ofpacts;
1870 struct vconn *vconn;
1873 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1875 ofpbuf_init(&ofpacts, 64);
1876 error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, &usable_protocols);
1878 ovs_fatal(0, "%s", error);
1881 po.buffer_id = UINT32_MAX;
1882 po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1883 po.ofpacts = ofpacts.data;
1884 po.ofpacts_len = ofpacts.size;
1886 protocol = open_vconn(ctx->argv[1], &vconn);
1887 for (i = 4; i < ctx->argc; i++) {
1888 struct dp_packet *packet;
1890 const char *error_msg;
1892 error_msg = eth_from_hex(ctx->argv[i], &packet);
1894 ovs_fatal(0, "%s", error_msg);
1897 po.packet = dp_packet_data(packet);
1898 po.packet_len = dp_packet_size(packet);
1899 opo = ofputil_encode_packet_out(&po, protocol);
1900 transact_noreply(vconn, opo);
1901 dp_packet_delete(packet);
1904 ofpbuf_uninit(&ofpacts);
1908 ofctl_mod_port(struct ovs_cmdl_context *ctx)
1910 struct ofp_config_flag {
1911 const char *name; /* The flag's name. */
1912 enum ofputil_port_config bit; /* Bit to turn on or off. */
1913 bool on; /* Value to set the bit to. */
1915 static const struct ofp_config_flag flags[] = {
1916 { "up", OFPUTIL_PC_PORT_DOWN, false },
1917 { "down", OFPUTIL_PC_PORT_DOWN, true },
1918 { "stp", OFPUTIL_PC_NO_STP, false },
1919 { "receive", OFPUTIL_PC_NO_RECV, false },
1920 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1921 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1922 { "forward", OFPUTIL_PC_NO_FWD, false },
1923 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1926 const struct ofp_config_flag *flag;
1927 enum ofputil_protocol protocol;
1928 struct ofputil_port_mod pm;
1929 struct ofputil_phy_port pp;
1930 struct vconn *vconn;
1931 const char *command;
1934 fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp);
1936 pm.port_no = pp.port_no;
1937 pm.hw_addr = pp.hw_addr;
1942 if (!strncasecmp(ctx->argv[3], "no-", 3)) {
1943 command = ctx->argv[3] + 3;
1945 } else if (!strncasecmp(ctx->argv[3], "no", 2)) {
1946 command = ctx->argv[3] + 2;
1949 command = ctx->argv[3];
1952 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1953 if (!strcasecmp(command, flag->name)) {
1954 pm.mask = flag->bit;
1955 pm.config = flag->on ^ not ? flag->bit : 0;
1959 ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]);
1962 protocol = open_vconn(ctx->argv[1], &vconn);
1963 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1967 /* This function uses OFPMP14_TABLE_DESC request to get the current
1968 * table configuration from switch. The function then modifies
1969 * only that table-config property, which has been requested. */
1971 fetch_table_desc(struct vconn *vconn, struct ofputil_table_mod *tm,
1972 struct ofputil_table_desc *td)
1974 struct ofpbuf *request;
1979 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
1980 send_xid = ((struct ofp_header *) request->data)->xid;
1981 send_openflow_buffer(vconn, request);
1984 struct ofpbuf *reply;
1986 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1987 recv_xid = ((struct ofp_header *) reply->data)->xid;
1988 if (send_xid == recv_xid) {
1989 struct ofp_header *oh = reply->data;
1994 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1995 if (ofptype_pull(&type, &b)
1996 || type != OFPTYPE_TABLE_DESC_REPLY) {
1997 ovs_fatal(0, "received bad reply: %s",
1998 ofp_to_string(reply->data, reply->size,
2001 flags = ofpmp_flags(oh);
2002 done = !(flags & OFPSF_REPLY_MORE);
2004 /* We've already found the table desc consisting of current
2005 * table configuration, but we need to drain the queue of
2006 * any other replies for this request. */
2009 while (!ofputil_decode_table_desc(&b, td, oh->version)) {
2010 if (td->table_id == tm->table_id) {
2016 VLOG_DBG("received reply with xid %08"PRIx32" "
2017 "!= expected %08"PRIx32, recv_xid, send_xid);
2019 ofpbuf_delete(reply);
2021 if (tm->eviction != OFPUTIL_TABLE_EVICTION_DEFAULT) {
2022 tm->vacancy = td->vacancy;
2023 tm->table_vacancy.vacancy_down = td->table_vacancy.vacancy_down;
2024 tm->table_vacancy.vacancy_up = td->table_vacancy.vacancy_up;
2025 } else if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
2026 tm->eviction = td->eviction;
2027 tm->eviction_flags = td->eviction_flags;
2032 ofctl_mod_table(struct ovs_cmdl_context *ctx)
2034 uint32_t usable_versions;
2035 struct ofputil_table_mod tm;
2036 struct vconn *vconn;
2040 error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3],
2043 ovs_fatal(0, "%s", error);
2046 uint32_t allowed_versions = get_allowed_ofp_versions();
2047 if (!(allowed_versions & usable_versions)) {
2048 struct ds versions = DS_EMPTY_INITIALIZER;
2049 ofputil_format_version_bitmap_names(&versions, usable_versions);
2050 ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow "
2052 ctx->argv[3], ds_cstr(&versions));
2054 mask_allowed_ofp_versions(usable_versions);
2055 enum ofputil_protocol protocol = open_vconn(ctx->argv[1], &vconn);
2057 /* For OpenFlow 1.4+, ovs-ofctl mod-table should not affect table-config
2058 * properties that the user didn't ask to change, so it is necessary to
2059 * restore the current configuration of table-config parameters using
2060 * OFPMP14_TABLE_DESC request. */
2061 if ((allowed_versions & (1u << OFP14_VERSION)) ||
2062 (allowed_versions & (1u << OFP15_VERSION))) {
2063 struct ofputil_table_desc td;
2065 if (tm.table_id == OFPTT_ALL) {
2066 for (i = 0; i < OFPTT_MAX; i++) {
2068 fetch_table_desc(vconn, &tm, &td);
2069 transact_noreply(vconn,
2070 ofputil_encode_table_mod(&tm, protocol));
2073 fetch_table_desc(vconn, &tm, &td);
2074 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2077 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2083 ofctl_get_frags(struct ovs_cmdl_context *ctx)
2085 struct ofputil_switch_config config;
2086 struct vconn *vconn;
2088 open_vconn(ctx->argv[1], &vconn);
2089 fetch_switch_config(vconn, &config);
2090 puts(ofputil_frag_handling_to_string(config.frag));
2095 ofctl_set_frags(struct ovs_cmdl_context *ctx)
2097 struct ofputil_switch_config config;
2098 enum ofputil_frag_handling frag;
2099 struct vconn *vconn;
2101 if (!ofputil_frag_handling_from_string(ctx->argv[2], &frag)) {
2102 ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]);
2105 open_vconn(ctx->argv[1], &vconn);
2106 fetch_switch_config(vconn, &config);
2107 if (frag != config.frag) {
2108 /* Set the configuration. */
2110 set_switch_config(vconn, &config);
2112 /* Then retrieve the configuration to see if it really took. OpenFlow
2113 * has ill-defined error reporting for bad flags, so this is about the
2114 * best we can do. */
2115 fetch_switch_config(vconn, &config);
2116 if (frag != config.frag) {
2117 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
2118 "switch probably doesn't support mode \"%s\")",
2119 ctx->argv[1], ofputil_frag_handling_to_string(frag));
2126 ofctl_ofp_parse(struct ovs_cmdl_context *ctx)
2128 const char *filename = ctx->argv[1];
2132 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2134 ovs_fatal(errno, "%s: open", filename);
2137 ofpbuf_init(&b, 65536);
2139 struct ofp_header *oh;
2140 size_t length, tail_len;
2145 oh = ofpbuf_put_uninit(&b, sizeof *oh);
2146 n = fread(oh, 1, sizeof *oh, file);
2149 } else if (n < sizeof *oh) {
2150 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2153 length = ntohs(oh->length);
2154 if (length < sizeof *oh) {
2155 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
2159 tail_len = length - sizeof *oh;
2160 tail = ofpbuf_put_uninit(&b, tail_len);
2161 n = fread(tail, 1, tail_len, file);
2163 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2166 ofp_print(stdout, b.data, b.size, verbosity + 2);
2170 if (file != stdin) {
2176 is_openflow_port(ovs_be16 port_, char *ports[])
2178 uint16_t port = ntohs(port_);
2182 for (i = 0; ports[i]; i++) {
2183 if (port == atoi(ports[i])) {
2189 return port == OFP_PORT || port == OFP_OLD_PORT;
2194 ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx)
2196 struct tcp_reader *reader;
2201 file = ovs_pcap_open(ctx->argv[1], "rb");
2203 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
2206 reader = tcp_reader_open();
2209 struct dp_packet *packet;
2213 error = ovs_pcap_read(file, &packet, &when);
2217 pkt_metadata_init(&packet->md, ODPP_NONE);
2218 flow_extract(packet, &flow);
2219 if (flow.dl_type == htons(ETH_TYPE_IP)
2220 && flow.nw_proto == IPPROTO_TCP
2221 && (is_openflow_port(flow.tp_src, ctx->argv + 2) ||
2222 is_openflow_port(flow.tp_dst, ctx->argv + 2))) {
2223 struct dp_packet *payload = tcp_reader_run(reader, &flow, packet);
2225 while (dp_packet_size(payload) >= sizeof(struct ofp_header)) {
2226 const struct ofp_header *oh;
2227 void *data = dp_packet_data(payload);
2230 /* Align OpenFlow on 8-byte boundary for safe access. */
2231 dp_packet_shift(payload, -((intptr_t) data & 7));
2233 oh = dp_packet_data(payload);
2234 length = ntohs(oh->length);
2235 if (dp_packet_size(payload) < length) {
2245 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
2250 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
2251 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
2252 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
2253 ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1);
2254 dp_packet_pull(payload, length);
2258 dp_packet_delete(packet);
2260 tcp_reader_close(reader);
2264 ofctl_ping(struct ovs_cmdl_context *ctx)
2266 size_t max_payload = 65535 - sizeof(struct ofp_header);
2267 unsigned int payload;
2268 struct vconn *vconn;
2271 payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64;
2272 if (payload > max_payload) {
2273 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2276 open_vconn(ctx->argv[1], &vconn);
2277 for (i = 0; i < 10; i++) {
2278 struct timeval start, end;
2279 struct ofpbuf *request, *reply;
2280 const struct ofp_header *rpy_hdr;
2283 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2284 vconn_get_version(vconn), payload);
2285 random_bytes(ofpbuf_put_uninit(request, payload), payload);
2287 xgettimeofday(&start);
2288 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2289 xgettimeofday(&end);
2291 rpy_hdr = reply->data;
2292 if (ofptype_pull(&type, reply)
2293 || type != OFPTYPE_ECHO_REPLY
2294 || reply->size != payload
2295 || memcmp(request->msg, reply->msg, payload)) {
2296 printf("Reply does not match request. Request:\n");
2297 ofp_print(stdout, request, request->size, verbosity + 2);
2299 ofp_print(stdout, reply, reply->size, verbosity + 2);
2301 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2302 reply->size, ctx->argv[1], ntohl(rpy_hdr->xid),
2303 (1000*(double)(end.tv_sec - start.tv_sec))
2304 + (.001*(end.tv_usec - start.tv_usec)));
2305 ofpbuf_delete(request);
2306 ofpbuf_delete(reply);
2312 ofctl_benchmark(struct ovs_cmdl_context *ctx)
2314 size_t max_payload = 65535 - sizeof(struct ofp_header);
2315 struct timeval start, end;
2316 unsigned int payload_size, message_size;
2317 struct vconn *vconn;
2322 payload_size = atoi(ctx->argv[2]);
2323 if (payload_size > max_payload) {
2324 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2326 message_size = sizeof(struct ofp_header) + payload_size;
2328 count = atoi(ctx->argv[3]);
2330 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2331 count, message_size, count * message_size);
2333 open_vconn(ctx->argv[1], &vconn);
2334 xgettimeofday(&start);
2335 for (i = 0; i < count; i++) {
2336 struct ofpbuf *request, *reply;
2338 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2339 vconn_get_version(vconn), payload_size);
2340 ofpbuf_put_zeros(request, payload_size);
2341 run(vconn_transact(vconn, request, &reply), "transact");
2342 ofpbuf_delete(reply);
2344 xgettimeofday(&end);
2347 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2348 + (.001*(end.tv_usec - start.tv_usec)));
2349 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2350 duration, count / (duration / 1000.0),
2351 count * message_size / (duration / 1000.0));
2355 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2356 size_t n_gms, enum ofputil_protocol usable_protocols)
2358 enum ofputil_protocol protocol;
2359 struct ofputil_group_mod *gm;
2360 enum ofp_version version;
2361 struct ofpbuf *request;
2363 struct vconn *vconn;
2366 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2367 version = ofputil_protocol_to_ofp_version(protocol);
2369 for (i = 0; i < n_gms; i++) {
2371 request = ofputil_encode_group_mod(version, gm);
2373 transact_noreply(vconn, request);
2383 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2385 struct ofputil_group_mod *gms = NULL;
2386 enum ofputil_protocol usable_protocols;
2391 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2394 ovs_fatal(0, "%s", error);
2396 ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
2397 for (i = 0; i < n_gms; i++) {
2398 ofputil_bucket_list_destroy(&gms[i].buckets);
2404 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2406 if (argc > 2 && !strcmp(argv[2], "-")) {
2407 ofctl_group_mod_file(argc, argv, command);
2409 enum ofputil_protocol usable_protocols;
2410 struct ofputil_group_mod gm;
2413 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2416 ovs_fatal(0, "%s", error);
2418 ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
2419 ofputil_bucket_list_destroy(&gm.buckets);
2424 ofctl_add_group(struct ovs_cmdl_context *ctx)
2426 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD);
2430 ofctl_add_groups(struct ovs_cmdl_context *ctx)
2432 ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD);
2436 ofctl_mod_group(struct ovs_cmdl_context *ctx)
2438 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_MODIFY);
2442 ofctl_del_groups(struct ovs_cmdl_context *ctx)
2444 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE);
2448 ofctl_insert_bucket(struct ovs_cmdl_context *ctx)
2450 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET);
2454 ofctl_remove_bucket(struct ovs_cmdl_context *ctx)
2456 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET);
2460 ofctl_dump_group_stats(struct ovs_cmdl_context *ctx)
2462 enum ofputil_protocol usable_protocols;
2463 struct ofputil_group_mod gm;
2464 struct ofpbuf *request;
2465 struct vconn *vconn;
2469 memset(&gm, 0, sizeof gm);
2471 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2472 ctx->argc > 2 ? ctx->argv[2] : "",
2475 ovs_fatal(0, "%s", error);
2478 group_id = gm.group_id;
2480 open_vconn(ctx->argv[1], &vconn);
2481 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2484 dump_transaction(vconn, request);
2491 ofctl_dump_group_desc(struct ovs_cmdl_context *ctx)
2493 struct ofpbuf *request;
2494 struct vconn *vconn;
2497 open_vconn(ctx->argv[1], &vconn);
2499 if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) {
2500 group_id = OFPG_ALL;
2503 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2506 dump_transaction(vconn, request);
2513 ofctl_dump_group_features(struct ovs_cmdl_context *ctx)
2515 struct ofpbuf *request;
2516 struct vconn *vconn;
2518 open_vconn(ctx->argv[1], &vconn);
2519 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2521 dump_transaction(vconn, request);
2528 ofctl_tlv_mod(struct ovs_cmdl_context *ctx, uint16_t command)
2530 enum ofputil_protocol usable_protocols;
2531 enum ofputil_protocol protocol;
2532 struct ofputil_tlv_table_mod ttm;
2534 enum ofp_version version;
2535 struct ofpbuf *request;
2536 struct vconn *vconn;
2538 error = parse_ofp_tlv_table_mod_str(&ttm, command, ctx->argc > 2 ?
2542 ovs_fatal(0, "%s", error);
2545 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols);
2546 version = ofputil_protocol_to_ofp_version(protocol);
2548 request = ofputil_encode_tlv_table_mod(version, &ttm);
2550 transact_noreply(vconn, request);
2554 ofputil_uninit_tlv_table(&ttm.mappings);
2558 ofctl_add_tlv_map(struct ovs_cmdl_context *ctx)
2560 ofctl_tlv_mod(ctx, NXTTMC_ADD);
2564 ofctl_del_tlv_map(struct ovs_cmdl_context *ctx)
2566 ofctl_tlv_mod(ctx, ctx->argc > 2 ? NXTTMC_DELETE : NXTTMC_CLEAR);
2570 ofctl_dump_tlv_map(struct ovs_cmdl_context *ctx)
2572 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXT_TLV_TABLE_REQUEST);
2576 ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
2582 ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
2584 ovs_cmdl_print_commands(get_all_commands());
2587 /* replace-flows and diff-flows commands. */
2589 struct flow_tables {
2590 struct classifier tables[OFPTT_MAX + 1];
2593 #define FOR_EACH_TABLE(CLS, TABLES) \
2594 for ((CLS) = (TABLES)->tables; \
2595 (CLS) < &(TABLES)->tables[ARRAY_SIZE((TABLES)->tables)]; \
2599 flow_tables_init(struct flow_tables *tables)
2601 struct classifier *cls;
2603 FOR_EACH_TABLE (cls, tables) {
2604 classifier_init(cls, NULL);
2609 flow_tables_defer(struct flow_tables *tables)
2611 struct classifier *cls;
2613 FOR_EACH_TABLE (cls, tables) {
2614 classifier_defer(cls);
2619 flow_tables_publish(struct flow_tables *tables)
2621 struct classifier *cls;
2623 FOR_EACH_TABLE (cls, tables) {
2624 classifier_publish(cls);
2628 /* A flow table entry, possibly with two different versions. */
2630 struct cls_rule rule; /* Within a "struct classifier". */
2631 struct fte_version *versions[2];
2634 /* One version of a Flow Table Entry. */
2635 struct fte_version {
2637 uint16_t idle_timeout;
2638 uint16_t hard_timeout;
2639 uint16_t importance;
2641 struct ofpact *ofpacts;
2646 /* Frees 'version' and the data that it owns. */
2648 fte_version_free(struct fte_version *version)
2651 free(CONST_CAST(struct ofpact *, version->ofpacts));
2656 /* Returns true if 'a' and 'b' are the same, false if they differ.
2658 * Ignores differences in 'flags' because there's no way to retrieve flags from
2659 * an OpenFlow switch. We have to assume that they are the same. */
2661 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2663 return (a->cookie == b->cookie
2664 && a->idle_timeout == b->idle_timeout
2665 && a->hard_timeout == b->hard_timeout
2666 && a->importance == b->importance
2667 && a->table_id == b->table_id
2668 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2669 b->ofpacts, b->ofpacts_len));
2672 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2673 * 'index' into 's', followed by a new-line. */
2675 fte_version_format(const struct fte *fte, int index, struct ds *s)
2677 const struct fte_version *version = fte->versions[index];
2684 if (version->table_id) {
2685 ds_put_format(s, "table=%"PRIu8" ", version->table_id);
2687 cls_rule_format(&fte->rule, s);
2688 if (version->cookie != htonll(0)) {
2689 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2691 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2692 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2694 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2695 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2697 if (version->importance != 0) {
2698 ds_put_format(s, " importance=%"PRIu16, version->importance);
2701 ds_put_cstr(s, " actions=");
2702 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2704 ds_put_char(s, '\n');
2708 fte_from_cls_rule(const struct cls_rule *cls_rule)
2710 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2713 /* Frees 'fte' and its versions. */
2715 fte_free(struct fte *fte)
2718 fte_version_free(fte->versions[0]);
2719 fte_version_free(fte->versions[1]);
2720 cls_rule_destroy(&fte->rule);
2725 /* Frees all of the FTEs within 'tables'. */
2727 fte_free_all(struct flow_tables *tables)
2729 struct classifier *cls;
2731 FOR_EACH_TABLE (cls, tables) {
2734 classifier_defer(cls);
2735 CLS_FOR_EACH (fte, rule, cls) {
2736 classifier_remove(cls, &fte->rule);
2737 ovsrcu_postpone(fte_free, fte);
2739 classifier_destroy(cls);
2743 /* Searches 'tables' for an FTE matching 'rule', inserting a new one if
2744 * necessary. Sets 'version' as the version of that rule with the given
2745 * 'index', replacing any existing version, if any.
2747 * Takes ownership of 'version'. */
2749 fte_insert(struct flow_tables *tables, const struct match *match,
2750 int priority, struct fte_version *version, int index)
2752 struct classifier *cls = &tables->tables[version->table_id];
2753 struct fte *old, *fte;
2755 fte = xzalloc(sizeof *fte);
2756 cls_rule_init(&fte->rule, match, priority);
2757 fte->versions[index] = version;
2759 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule,
2760 CLS_MIN_VERSION, NULL, 0));
2762 fte->versions[!index] = old->versions[!index];
2763 old->versions[!index] = NULL;
2765 ovsrcu_postpone(fte_free, old);
2769 /* Reads the flows in 'filename' as flow table entries in 'tables' for the
2770 * version with the specified 'index'. Returns the flow formats able to
2771 * represent the flows that were read. */
2772 static enum ofputil_protocol
2773 read_flows_from_file(const char *filename, struct flow_tables *tables,
2776 enum ofputil_protocol usable_protocols;
2781 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2783 ovs_fatal(errno, "%s: open", filename);
2787 usable_protocols = OFPUTIL_P_ANY;
2789 flow_tables_defer(tables);
2790 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2791 struct fte_version *version;
2792 struct ofputil_flow_mod fm;
2794 enum ofputil_protocol usable;
2796 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2798 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2800 usable_protocols &= usable;
2802 version = xmalloc(sizeof *version);
2803 version->cookie = fm.new_cookie;
2804 version->idle_timeout = fm.idle_timeout;
2805 version->hard_timeout = fm.hard_timeout;
2806 version->importance = fm.importance;
2807 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2808 | OFPUTIL_FF_EMERG);
2809 version->ofpacts = fm.ofpacts;
2810 version->ofpacts_len = fm.ofpacts_len;
2811 version->table_id = fm.table_id != OFPTT_ALL ? fm.table_id : 0;
2813 fte_insert(tables, &fm.match, fm.priority, version, index);
2815 flow_tables_publish(tables);
2818 if (file != stdin) {
2822 return usable_protocols;
2826 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2827 struct ofpbuf **replyp,
2828 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2830 struct ofpbuf *reply = *replyp;
2836 /* Get a flow stats reply message, if we don't already have one. */
2842 run(vconn_recv_block(vconn, &reply),
2843 "OpenFlow packet receive failed");
2844 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2846 error = ofptype_decode(&type, reply->data);
2847 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2848 ovs_fatal(0, "received bad reply: %s",
2849 ofp_to_string(reply->data, reply->size,
2854 /* Pull an individual flow stats reply out of the message. */
2855 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2862 more = ofpmp_more(reply->header);
2863 ofpbuf_delete(reply);
2872 ovs_fatal(0, "parse error in reply (%s)",
2873 ofperr_to_string(retval));
2878 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2879 * format 'protocol', and adds them as flow table entries in 'tables' for the
2880 * version with the specified 'index'. */
2882 read_flows_from_switch(struct vconn *vconn,
2883 enum ofputil_protocol protocol,
2884 struct flow_tables *tables, int index)
2886 struct ofputil_flow_stats_request fsr;
2887 struct ofputil_flow_stats fs;
2888 struct ofpbuf *request;
2889 struct ofpbuf ofpacts;
2890 struct ofpbuf *reply;
2893 fsr.aggregate = false;
2894 match_init_catchall(&fsr.match);
2895 fsr.out_port = OFPP_ANY;
2896 fsr.out_group = OFPG_ANY;
2897 fsr.table_id = 0xff;
2898 fsr.cookie = fsr.cookie_mask = htonll(0);
2899 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2900 send_xid = ((struct ofp_header *) request->data)->xid;
2901 send_openflow_buffer(vconn, request);
2904 ofpbuf_init(&ofpacts, 0);
2905 flow_tables_defer(tables);
2906 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2907 struct fte_version *version;
2909 version = xmalloc(sizeof *version);
2910 version->cookie = fs.cookie;
2911 version->idle_timeout = fs.idle_timeout;
2912 version->hard_timeout = fs.hard_timeout;
2913 version->importance = fs.importance;
2915 version->ofpacts_len = fs.ofpacts_len;
2916 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2917 version->table_id = fs.table_id;
2919 fte_insert(tables, &fs.match, fs.priority, version, index);
2921 flow_tables_publish(tables);
2922 ofpbuf_uninit(&ofpacts);
2926 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2927 enum ofputil_protocol protocol, struct ovs_list *packets)
2929 const struct fte_version *version = fte->versions[index];
2932 struct ofputil_flow_mod fm = {
2933 .priority = fte->rule.priority,
2934 .new_cookie = version->cookie,
2935 .modify_cookie = true,
2936 .table_id = version->table_id,
2938 .idle_timeout = version->idle_timeout,
2939 .hard_timeout = version->hard_timeout,
2940 .importance = version->importance,
2941 .buffer_id = UINT32_MAX,
2942 .out_port = OFPP_ANY,
2943 .out_group = OFPG_ANY,
2944 .flags = version->flags,
2945 .delete_reason = OFPRR_DELETE,
2947 minimatch_expand(&fte->rule.match, &fm.match);
2948 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2949 command == OFPFC_MODIFY_STRICT) {
2950 fm.ofpacts = version->ofpacts;
2951 fm.ofpacts_len = version->ofpacts_len;
2957 ofm = ofputil_encode_flow_mod(&fm, protocol);
2958 list_push_back(packets, &ofm->list_node);
2962 ofctl_replace_flows(struct ovs_cmdl_context *ctx)
2964 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2965 enum ofputil_protocol usable_protocols, protocol;
2966 struct flow_tables tables;
2967 struct classifier *cls;
2968 struct ovs_list requests;
2969 struct vconn *vconn;
2972 flow_tables_init(&tables);
2973 usable_protocols = read_flows_from_file(ctx->argv[2], &tables, FILE_IDX);
2975 protocol = open_vconn(ctx->argv[1], &vconn);
2976 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2978 read_flows_from_switch(vconn, protocol, &tables, SWITCH_IDX);
2980 list_init(&requests);
2982 FOR_EACH_TABLE (cls, &tables) {
2983 /* Delete flows that exist on the switch but not in the file. */
2984 CLS_FOR_EACH (fte, rule, cls) {
2985 struct fte_version *file_ver = fte->versions[FILE_IDX];
2986 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2988 if (sw_ver && !file_ver) {
2989 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2990 protocol, &requests);
2994 /* Add flows that exist in the file but not on the switch.
2995 * Update flows that exist in both places but differ. */
2996 CLS_FOR_EACH (fte, rule, cls) {
2997 struct fte_version *file_ver = fte->versions[FILE_IDX];
2998 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
3001 (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
3002 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol,
3008 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
3010 transact_multiple_noreply(vconn, &requests);
3013 ofpbuf_list_delete(&requests);
3016 fte_free_all(&tables);
3020 read_flows_from_source(const char *source, struct flow_tables *tables,
3025 if (source[0] == '/' || source[0] == '.'
3026 || (!strchr(source, ':') && !stat(source, &s))) {
3027 read_flows_from_file(source, tables, index);
3029 enum ofputil_protocol protocol;
3030 struct vconn *vconn;
3032 protocol = open_vconn(source, &vconn);
3033 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
3034 read_flows_from_switch(vconn, protocol, tables, index);
3040 ofctl_diff_flows(struct ovs_cmdl_context *ctx)
3042 bool differences = false;
3043 struct flow_tables tables;
3044 struct classifier *cls;
3048 flow_tables_init(&tables);
3049 read_flows_from_source(ctx->argv[1], &tables, 0);
3050 read_flows_from_source(ctx->argv[2], &tables, 1);
3055 FOR_EACH_TABLE (cls, &tables) {
3056 CLS_FOR_EACH (fte, rule, cls) {
3057 struct fte_version *a = fte->versions[0];
3058 struct fte_version *b = fte->versions[1];
3060 if (!a || !b || !fte_version_equals(a, b)) {
3061 fte_version_format(fte, 0, &a_s);
3062 fte_version_format(fte, 1, &b_s);
3063 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
3065 printf("-%s", ds_cstr(&a_s));
3068 printf("+%s", ds_cstr(&b_s));
3079 fte_free_all(&tables);
3087 ofctl_meter_mod__(const char *bridge, const char *str, int command)
3089 struct ofputil_meter_mod mm;
3090 struct vconn *vconn;
3091 enum ofputil_protocol protocol;
3092 enum ofputil_protocol usable_protocols;
3093 enum ofp_version version;
3097 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
3099 ovs_fatal(0, "%s", error);
3102 usable_protocols = OFPUTIL_P_OF13_UP;
3103 mm.command = command;
3104 mm.meter.meter_id = OFPM13_ALL;
3107 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3108 version = ofputil_protocol_to_ofp_version(protocol);
3109 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
3114 ofctl_meter_request__(const char *bridge, const char *str,
3115 enum ofputil_meter_request_type type)
3117 struct ofputil_meter_mod mm;
3118 struct vconn *vconn;
3119 enum ofputil_protocol usable_protocols;
3120 enum ofputil_protocol protocol;
3121 enum ofp_version version;
3125 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
3127 ovs_fatal(0, "%s", error);
3130 usable_protocols = OFPUTIL_P_OF13_UP;
3131 mm.meter.meter_id = OFPM13_ALL;
3134 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3135 version = ofputil_protocol_to_ofp_version(protocol);
3136 dump_transaction(vconn, ofputil_encode_meter_request(version, type,
3137 mm.meter.meter_id));
3143 ofctl_add_meter(struct ovs_cmdl_context *ctx)
3145 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD);
3149 ofctl_mod_meter(struct ovs_cmdl_context *ctx)
3151 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY);
3155 ofctl_del_meters(struct ovs_cmdl_context *ctx)
3157 ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE);
3161 ofctl_dump_meters(struct ovs_cmdl_context *ctx)
3163 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3164 OFPUTIL_METER_CONFIG);
3168 ofctl_meter_stats(struct ovs_cmdl_context *ctx)
3170 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3171 OFPUTIL_METER_STATS);
3175 ofctl_meter_features(struct ovs_cmdl_context *ctx)
3177 ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES);
3181 /* Undocumented commands for unit testing. */
3184 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
3185 enum ofputil_protocol usable_protocols)
3187 enum ofputil_protocol protocol = 0;
3191 usable_s = ofputil_protocols_to_string(usable_protocols);
3192 printf("usable protocols: %s\n", usable_s);
3195 if (!(usable_protocols & allowed_protocols)) {
3196 ovs_fatal(0, "no usable protocol");
3198 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
3200 if (protocol & usable_protocols & allowed_protocols) {
3204 ovs_assert(is_pow2(protocol));
3206 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
3208 for (i = 0; i < n_fms; i++) {
3209 struct ofputil_flow_mod *fm = &fms[i];
3212 msg = ofputil_encode_flow_mod(fm, protocol);
3213 ofp_print(stdout, msg->data, msg->size, verbosity);
3216 free(CONST_CAST(struct ofpact *, fm->ofpacts));
3220 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
3221 * it back to stdout. */
3223 ofctl_parse_flow(struct ovs_cmdl_context *ctx)
3225 enum ofputil_protocol usable_protocols;
3226 struct ofputil_flow_mod fm;
3229 error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols);
3231 ovs_fatal(0, "%s", error);
3233 ofctl_parse_flows__(&fm, 1, usable_protocols);
3236 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
3237 * add-flows) and prints each of the flows back to stdout. */
3239 ofctl_parse_flows(struct ovs_cmdl_context *ctx)
3241 enum ofputil_protocol usable_protocols;
3242 struct ofputil_flow_mod *fms = NULL;
3246 error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms,
3249 ovs_fatal(0, "%s", error);
3251 ofctl_parse_flows__(fms, n_fms, usable_protocols);
3256 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
3261 while (!ds_get_test_line(&in, stdin)) {
3262 struct ofpbuf nx_match;
3264 ovs_be64 cookie, cookie_mask;
3268 /* Convert string to nx_match. */
3269 ofpbuf_init(&nx_match, 0);
3271 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
3273 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
3276 /* Convert nx_match to match. */
3279 error = oxm_pull_match(&nx_match, &match);
3281 error = nx_pull_match(&nx_match, match_len, &match,
3282 &cookie, &cookie_mask);
3286 error = oxm_pull_match_loose(&nx_match, &match);
3288 error = nx_pull_match_loose(&nx_match, match_len, &match,
3289 &cookie, &cookie_mask);
3297 /* Convert match back to nx_match. */
3298 ofpbuf_uninit(&nx_match);
3299 ofpbuf_init(&nx_match, 0);
3301 match_len = oxm_put_match(&nx_match, &match, version);
3302 out = oxm_match_to_string(&nx_match, match_len);
3304 match_len = nx_put_match(&nx_match, &match,
3305 cookie, cookie_mask);
3306 out = nx_match_to_string(nx_match.data, match_len);
3312 if (verbosity > 0) {
3313 ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false);
3316 printf("nx_pull_match() returned error %s\n",
3317 ofperr_get_name(error));
3320 ofpbuf_uninit(&nx_match);
3325 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
3326 * stdin, does some internal fussing with them, and then prints them back as
3327 * strings on stdout. */
3329 ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED)
3331 ofctl_parse_nxm__(false, 0);
3334 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
3335 * strings from stdin, does some internal fussing with them, and then prints
3336 * them back as strings on stdout. VERSION must specify an OpenFlow version,
3337 * e.g. "OpenFlow12". */
3339 ofctl_parse_oxm(struct ovs_cmdl_context *ctx)
3341 enum ofp_version version = ofputil_version_from_string(ctx->argv[1]);
3342 if (version < OFP12_VERSION) {
3343 ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]);
3346 ofctl_parse_nxm__(true, version);
3350 print_differences(const char *prefix,
3351 const void *a_, size_t a_len,
3352 const void *b_, size_t b_len)
3354 const uint8_t *a = a_;
3355 const uint8_t *b = b_;
3358 for (i = 0; i < MIN(a_len, b_len); i++) {
3360 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
3361 prefix, i, a[i], b[i]);
3364 for (i = a_len; i < b_len; i++) {
3365 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
3367 for (i = b_len; i < a_len; i++) {
3368 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
3373 ofctl_parse_actions__(const char *version_s, bool instructions)
3375 enum ofp_version version;
3378 version = ofputil_version_from_string(version_s);
3380 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3384 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3385 struct ofpbuf of_out;
3386 struct ofpbuf of_in;
3387 struct ofpbuf ofpacts;
3388 const char *table_id;
3394 /* Parse table_id separated with the follow-up actions by ",", if
3396 actions = ds_cstr(&in);
3398 if (strstr(actions, ",")) {
3399 table_id = strsep(&actions, ",");
3402 /* Parse hex bytes. */
3403 ofpbuf_init(&of_in, 0);
3404 if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
3405 ovs_fatal(0, "Trailing garbage in hex data");
3408 /* Convert to ofpacts. */
3409 ofpbuf_init(&ofpacts, 0);
3411 error = (instructions
3412 ? ofpacts_pull_openflow_instructions
3413 : ofpacts_pull_openflow_actions)(
3414 &of_in, of_in.size, version, &ofpacts);
3415 if (!error && instructions) {
3416 /* Verify actions, enforce consistency. */
3417 enum ofputil_protocol protocol;
3420 memset(&flow, 0, sizeof flow);
3421 protocol = ofputil_protocols_from_ofp_version(version);
3422 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3424 table_id ? atoi(table_id) : 0,
3425 OFPTT_MAX + 1, protocol);
3428 printf("bad %s %s: %s\n\n",
3429 version_s, instructions ? "instructions" : "actions",
3430 ofperr_get_name(error));
3431 ofpbuf_uninit(&ofpacts);
3432 ofpbuf_uninit(&of_in);
3435 ofpbuf_push_uninit(&of_in, size);
3437 /* Print cls_rule. */
3439 ds_put_cstr(&s, "actions=");
3440 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3444 /* Convert back to ofp10 actions and print differences from input. */
3445 ofpbuf_init(&of_out, 0);
3447 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3450 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size,
3454 print_differences("", of_in.data, of_in.size,
3455 of_out.data, of_out.size);
3458 ofpbuf_uninit(&ofpacts);
3459 ofpbuf_uninit(&of_in);
3460 ofpbuf_uninit(&of_out);
3465 /* "parse-actions VERSION": reads a series of action specifications for the
3466 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3467 * prints them as strings on stdout, and then converts them back to hex bytes
3468 * and prints any differences from the input. */
3470 ofctl_parse_actions(struct ovs_cmdl_context *ctx)
3472 ofctl_parse_actions__(ctx->argv[1], false);
3475 /* "parse-actions VERSION": reads a series of instruction specifications for
3476 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3477 * ofpacts, prints them as strings on stdout, and then converts them back to
3478 * hex bytes and prints any differences from the input. */
3480 ofctl_parse_instructions(struct ovs_cmdl_context *ctx)
3482 ofctl_parse_actions__(ctx->argv[1], true);
3485 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3486 * bytes from stdin, converts them to cls_rules, prints them as strings on
3487 * stdout, and then converts them back to hex bytes and prints any differences
3490 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3491 * values are ignored in the input and will be set to zero when OVS converts
3492 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3493 * it does the conversion to hex, to ensure that in fact they are ignored. */
3495 ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3502 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3503 struct ofpbuf match_in, match_expout;
3504 struct ofp10_match match_out;
3505 struct ofp10_match match_normal;
3509 /* Parse hex bytes to use for expected output. */
3511 ds_put_cstr(&expout, ds_cstr(&in));
3512 for (p = ds_cstr(&expout); *p; p++) {
3517 ofpbuf_init(&match_expout, 0);
3518 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3519 ovs_fatal(0, "Trailing garbage in hex data");
3521 if (match_expout.size != sizeof(struct ofp10_match)) {
3522 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3523 match_expout.size, sizeof(struct ofp10_match));
3526 /* Parse hex bytes for input. */
3527 for (p = ds_cstr(&in); *p; p++) {
3529 *p = "0123456789abcdef"[random_uint32() & 0xf];
3532 ofpbuf_init(&match_in, 0);
3533 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3534 ovs_fatal(0, "Trailing garbage in hex data");
3536 if (match_in.size != sizeof(struct ofp10_match)) {
3537 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3538 match_in.size, sizeof(struct ofp10_match));
3541 /* Convert to cls_rule and print. */
3542 ofputil_match_from_ofp10_match(match_in.data, &match);
3543 match_print(&match);
3545 /* Convert back to ofp10_match and print differences from input. */
3546 ofputil_match_to_ofp10_match(&match, &match_out);
3547 print_differences("", match_expout.data, match_expout.size,
3548 &match_out, sizeof match_out);
3550 /* Normalize, then convert and compare again. */
3551 ofputil_normalize_match(&match);
3552 ofputil_match_to_ofp10_match(&match, &match_normal);
3553 print_differences("normal: ", &match_out, sizeof match_out,
3554 &match_normal, sizeof match_normal);
3557 ofpbuf_uninit(&match_in);
3558 ofpbuf_uninit(&match_expout);
3561 ds_destroy(&expout);
3564 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3565 * bytes from stdin, converts them to "struct match"es, prints them as strings
3566 * on stdout, and then converts them back to hex bytes and prints any
3567 * differences from the input. */
3569 ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3574 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3575 struct ofpbuf match_in;
3576 struct ofp11_match match_out;
3580 /* Parse hex bytes. */
3581 ofpbuf_init(&match_in, 0);
3582 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3583 ovs_fatal(0, "Trailing garbage in hex data");
3585 if (match_in.size != sizeof(struct ofp11_match)) {
3586 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3587 match_in.size, sizeof(struct ofp11_match));
3590 /* Convert to match. */
3591 error = ofputil_match_from_ofp11_match(match_in.data, &match);
3593 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3594 ofpbuf_uninit(&match_in);
3599 match_print(&match);
3601 /* Convert back to ofp11_match and print differences from input. */
3602 ofputil_match_to_ofp11_match(&match, &match_out);
3604 print_differences("", match_in.data, match_in.size,
3605 &match_out, sizeof match_out);
3608 ofpbuf_uninit(&match_in);
3613 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3615 ofctl_parse_pcap(struct ovs_cmdl_context *ctx)
3619 pcap = ovs_pcap_open(ctx->argv[1], "rb");
3621 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
3625 struct dp_packet *packet;
3629 error = ovs_pcap_read(pcap, &packet, NULL);
3633 ovs_fatal(error, "%s: read failed", ctx->argv[1]);
3636 pkt_metadata_init(&packet->md, ODPP_NONE);
3637 flow_extract(packet, &flow);
3638 flow_print(stdout, &flow);
3640 dp_packet_delete(packet);
3644 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3645 * mask values to and from various formats and prints the results. */
3647 ofctl_check_vlan(struct ovs_cmdl_context *ctx)
3652 struct ofputil_flow_mod fm;
3655 struct match nxm_match;
3659 struct ofp10_match of10_raw;
3660 struct match of10_match;
3662 struct ofp11_match of11_raw;
3663 struct match of11_match;
3668 enum ofputil_protocol usable_protocols; /* Unused for now. */
3670 match_init_catchall(&match);
3671 match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16));
3672 match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16));
3674 /* Convert to and from string. */
3675 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3676 printf("%s -> ", string_s);
3678 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3680 ovs_fatal(0, "%s", error_s);
3682 printf("%04"PRIx16"/%04"PRIx16"\n",
3683 ntohs(fm.match.flow.vlan_tci),
3684 ntohs(fm.match.wc.masks.vlan_tci));
3687 /* Convert to and from NXM. */
3688 ofpbuf_init(&nxm, 0);
3689 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3690 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3691 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3692 printf("NXM: %s -> ", nxm_s);
3694 printf("%s\n", ofperr_to_string(error));
3696 printf("%04"PRIx16"/%04"PRIx16"\n",
3697 ntohs(nxm_match.flow.vlan_tci),
3698 ntohs(nxm_match.wc.masks.vlan_tci));
3701 ofpbuf_uninit(&nxm);
3703 /* Convert to and from OXM. */
3704 ofpbuf_init(&nxm, 0);
3705 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
3706 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3707 error = oxm_pull_match(&nxm, &nxm_match);
3708 printf("OXM: %s -> ", nxm_s);
3710 printf("%s\n", ofperr_to_string(error));
3712 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3713 (VLAN_VID_MASK | VLAN_CFI);
3714 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3715 (VLAN_VID_MASK | VLAN_CFI);
3717 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3718 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3719 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3725 ofpbuf_uninit(&nxm);
3727 /* Convert to and from OpenFlow 1.0. */
3728 ofputil_match_to_ofp10_match(&match, &of10_raw);
3729 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3730 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3731 ntohs(of10_raw.dl_vlan),
3732 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3733 of10_raw.dl_vlan_pcp,
3734 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3735 ntohs(of10_match.flow.vlan_tci),
3736 ntohs(of10_match.wc.masks.vlan_tci));
3738 /* Convert to and from OpenFlow 1.1. */
3739 ofputil_match_to_ofp11_match(&match, &of11_raw);
3740 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3741 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3742 ntohs(of11_raw.dl_vlan),
3743 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3744 of11_raw.dl_vlan_pcp,
3745 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3746 ntohs(of11_match.flow.vlan_tci),
3747 ntohs(of11_match.wc.masks.vlan_tci));
3750 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3753 ofctl_print_error(struct ovs_cmdl_context *ctx)
3758 error = ofperr_from_name(ctx->argv[1]);
3760 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3763 for (version = 0; version <= UINT8_MAX; version++) {
3764 const char *name = ofperr_domain_get_name(version);
3766 int vendor = ofperr_get_vendor(error, version);
3767 int type = ofperr_get_type(error, version);
3768 int code = ofperr_get_code(error, version);
3770 if (vendor != -1 || type != -1 || code != -1) {
3771 printf("%s: vendor %#x, type %d, code %d\n",
3772 name, vendor, type, code);
3778 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3779 * error named ENUM and prints the error reply in hex. */
3781 ofctl_encode_error_reply(struct ovs_cmdl_context *ctx)
3783 const struct ofp_header *oh;
3784 struct ofpbuf request, *reply;
3787 error = ofperr_from_name(ctx->argv[1]);
3789 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3792 ofpbuf_init(&request, 0);
3793 if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') {
3794 ovs_fatal(0, "Trailing garbage in hex data");
3796 if (request.size < sizeof(struct ofp_header)) {
3797 ovs_fatal(0, "Request too short");
3801 if (request.size != ntohs(oh->length)) {
3802 ovs_fatal(0, "Request size inconsistent");
3805 reply = ofperr_encode_reply(error, request.data);
3806 ofpbuf_uninit(&request);
3808 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3809 ofpbuf_delete(reply);
3812 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3813 * binary data, interpreting them as an OpenFlow message, and prints the
3814 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3816 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3817 * HEXSTRING_FILE contains the HEXSTRING. */
3819 ofctl_ofp_print(struct ovs_cmdl_context *ctx)
3821 struct ofpbuf packet;
3828 if (!strcmp(ctx->argv[ctx->argc-1], "-")) {
3829 if (ds_get_line(&line, stdin)) {
3830 VLOG_FATAL("Failed to read stdin");
3833 buffer = line.string;
3834 verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity;
3835 } else if (ctx->argc > 2) {
3836 buffer = ctx->argv[1];
3837 verbosity = atoi(ctx->argv[2]);
3839 buffer = ctx->argv[1];
3842 ofpbuf_init(&packet, strlen(buffer) / 2);
3843 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
3844 ovs_fatal(0, "trailing garbage following hex bytes");
3846 ofp_print(stdout, packet.data, packet.size, verbosity);
3847 ofpbuf_uninit(&packet);
3851 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3852 * and dumps each message in hex. */
3854 ofctl_encode_hello(struct ovs_cmdl_context *ctx)
3856 uint32_t bitmap = strtol(ctx->argv[1], NULL, 0);
3857 struct ofpbuf *hello;
3859 hello = ofputil_encode_hello(bitmap);
3860 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3861 ofp_print(stdout, hello->data, hello->size, verbosity);
3862 ofpbuf_delete(hello);
3865 static const struct ovs_cmdl_command all_commands[] = {
3868 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3869 1, 3, ofctl_monitor },
3870 { "snoop", "switch",
3871 1, 1, ofctl_snoop },
3872 { "dump-desc", "switch",
3873 1, 1, ofctl_dump_desc },
3874 { "dump-tables", "switch",
3875 1, 1, ofctl_dump_tables },
3876 { "dump-table-features", "switch",
3877 1, 1, ofctl_dump_table_features },
3878 { "dump-table-desc", "switch",
3879 1, 1, ofctl_dump_table_desc },
3880 { "dump-flows", "switch",
3881 1, 2, ofctl_dump_flows },
3882 { "dump-aggregate", "switch",
3883 1, 2, ofctl_dump_aggregate },
3884 { "queue-stats", "switch [port [queue]]",
3885 1, 3, ofctl_queue_stats },
3886 { "queue-get-config", "switch [port [queue]]",
3887 1, 3, ofctl_queue_get_config },
3888 { "add-flow", "switch flow",
3889 2, 2, ofctl_add_flow },
3890 { "add-flows", "switch file",
3891 2, 2, ofctl_add_flows },
3892 { "mod-flows", "switch flow",
3893 2, 2, ofctl_mod_flows },
3894 { "del-flows", "switch [flow]",
3895 1, 2, ofctl_del_flows },
3896 { "replace-flows", "switch file",
3897 2, 2, ofctl_replace_flows },
3898 { "diff-flows", "source1 source2",
3899 2, 2, ofctl_diff_flows },
3900 { "add-meter", "switch meter",
3901 2, 2, ofctl_add_meter },
3902 { "mod-meter", "switch meter",
3903 2, 2, ofctl_mod_meter },
3904 { "del-meter", "switch meter",
3905 2, 2, ofctl_del_meters },
3906 { "del-meters", "switch",
3907 1, 1, ofctl_del_meters },
3908 { "dump-meter", "switch meter",
3909 2, 2, ofctl_dump_meters },
3910 { "dump-meters", "switch",
3911 1, 1, ofctl_dump_meters },
3912 { "meter-stats", "switch [meter]",
3913 1, 2, ofctl_meter_stats },
3914 { "meter-features", "switch",
3915 1, 1, ofctl_meter_features },
3916 { "packet-out", "switch in_port actions packet...",
3917 4, INT_MAX, ofctl_packet_out },
3918 { "dump-ports", "switch [port]",
3919 1, 2, ofctl_dump_ports },
3920 { "dump-ports-desc", "switch [port]",
3921 1, 2, ofctl_dump_ports_desc },
3922 { "mod-port", "switch iface act",
3923 3, 3, ofctl_mod_port },
3924 { "mod-table", "switch mod",
3925 3, 3, ofctl_mod_table },
3926 { "get-frags", "switch",
3927 1, 1, ofctl_get_frags },
3928 { "set-frags", "switch frag_mode",
3929 2, 2, ofctl_set_frags },
3930 { "probe", "target",
3931 1, 1, ofctl_probe },
3932 { "ping", "target [n]",
3934 { "benchmark", "target n count",
3935 3, 3, ofctl_benchmark },
3937 { "ofp-parse", "file",
3938 1, 1, ofctl_ofp_parse },
3939 { "ofp-parse-pcap", "pcap",
3940 1, INT_MAX, ofctl_ofp_parse_pcap },
3942 { "add-group", "switch group",
3943 1, 2, ofctl_add_group },
3944 { "add-groups", "switch file",
3945 1, 2, ofctl_add_groups },
3946 { "mod-group", "switch group",
3947 1, 2, ofctl_mod_group },
3948 { "del-groups", "switch [group]",
3949 1, 2, ofctl_del_groups },
3950 { "insert-buckets", "switch [group]",
3951 1, 2, ofctl_insert_bucket },
3952 { "remove-buckets", "switch [group]",
3953 1, 2, ofctl_remove_bucket },
3954 { "dump-groups", "switch [group]",
3955 1, 2, ofctl_dump_group_desc },
3956 { "dump-group-stats", "switch [group]",
3957 1, 2, ofctl_dump_group_stats },
3958 { "dump-group-features", "switch",
3959 1, 1, ofctl_dump_group_features },
3960 { "add-tlv-map", "switch map",
3961 2, 2, ofctl_add_tlv_map },
3962 { "del-tlv-map", "switch [map]",
3963 1, 2, ofctl_del_tlv_map },
3964 { "dump-tlv-map", "switch",
3965 1, 1, ofctl_dump_tlv_map },
3966 { "help", NULL, 0, INT_MAX, ofctl_help },
3967 { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands },
3969 /* Undocumented commands for testing. */
3970 { "parse-flow", NULL, 1, 1, ofctl_parse_flow },
3971 { "parse-flows", NULL, 1, 1, ofctl_parse_flows },
3972 { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm },
3973 { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm },
3974 { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm },
3975 { "parse-actions", NULL, 1, 1, ofctl_parse_actions },
3976 { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions },
3977 { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match },
3978 { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match },
3979 { "parse-pcap", NULL, 1, 1, ofctl_parse_pcap },
3980 { "check-vlan", NULL, 2, 2, ofctl_check_vlan },
3981 { "print-error", NULL, 1, 1, ofctl_print_error },
3982 { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply },
3983 { "ofp-print", NULL, 1, 2, ofctl_ofp_print },
3984 { "encode-hello", NULL, 1, 1, ofctl_encode_hello },
3986 { NULL, NULL, 0, 0, NULL },
3989 static const struct ovs_cmdl_command *get_all_commands(void)
3991 return all_commands;