2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 information 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-geneve-map SWITCH MAP add Geneve option MAPpings\n"
391 " del-geneve-map SWITCH [MAP] delete Geneve option MAPpings\n"
392 " dump-geneve-map SWITCH print Geneve 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 struct ofpbuf *reply;
537 run(vconn_transact(vconn, request, &reply), "talking to %s",
538 vconn_get_name(vconn));
539 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
540 ofpbuf_delete(reply);
544 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
546 struct ofpbuf *request;
549 open_vconn(vconn_name, &vconn);
550 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
551 dump_transaction(vconn, request);
556 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
558 const struct ofp_header *request_oh = request->data;
559 ovs_be32 send_xid = request_oh->xid;
560 enum ofpraw request_raw;
561 enum ofpraw reply_raw;
564 ofpraw_decode_partial(&request_raw, request->data, request->size);
565 reply_raw = ofpraw_stats_request_to_reply(request_raw,
566 request_oh->version);
568 send_openflow_buffer(vconn, request);
571 struct ofpbuf *reply;
573 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
574 recv_xid = ((struct ofp_header *) reply->data)->xid;
575 if (send_xid == recv_xid) {
578 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
580 ofpraw_decode(&raw, reply->data);
581 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
583 } else if (raw == reply_raw) {
584 done = !ofpmp_more(reply->data);
586 ovs_fatal(0, "received bad reply: %s",
587 ofp_to_string(reply->data, reply->size,
591 VLOG_DBG("received reply with xid %08"PRIx32" "
592 "!= expected %08"PRIx32, recv_xid, send_xid);
594 ofpbuf_delete(reply);
599 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
601 struct ofpbuf *request;
604 open_vconn(vconn_name, &vconn);
605 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
606 dump_stats_transaction(vconn, request);
610 /* Sends all of the 'requests', which should be requests that only have replies
611 * if an error occurs, and waits for them to succeed or fail. If an error does
612 * occur, prints it and exits with an error.
614 * Destroys all of the 'requests'. */
616 transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
618 struct ofpbuf *reply;
620 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
621 "talking to %s", vconn_get_name(vconn));
623 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
626 ofpbuf_delete(reply);
630 bundle_error_reporter(const struct ofp_header *oh)
632 ofp_print(stderr, oh, ntohs(oh->length), verbosity + 1);
637 bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
639 run(vconn_bundle_transact(vconn, requests, flags, bundle_error_reporter),
640 "talking to %s", vconn_get_name(vconn));
643 /* Sends 'request', which should be a request that only has a reply if an error
644 * occurs, and waits for it to succeed or fail. If an error does occur, prints
645 * it and exits with an error.
647 * Destroys 'request'. */
649 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
651 struct ovs_list requests;
653 list_init(&requests);
654 list_push_back(&requests, &request->list_node);
655 transact_multiple_noreply(vconn, &requests);
659 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
661 struct ofp_switch_config *config;
662 struct ofpbuf *request;
663 struct ofpbuf *reply;
666 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
667 vconn_get_version(vconn), 0);
668 run(vconn_transact(vconn, request, &reply),
669 "talking to %s", vconn_get_name(vconn));
671 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
672 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
675 config = ofpbuf_pull(reply, sizeof *config);
678 ofpbuf_delete(reply);
682 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
684 struct ofpbuf *request;
686 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
687 ofpbuf_put(request, config, sizeof *config);
689 transact_noreply(vconn, request);
693 ofctl_show(struct ovs_cmdl_context *ctx)
695 const char *vconn_name = ctx->argv[1];
696 enum ofp_version version;
698 struct ofpbuf *request;
699 struct ofpbuf *reply;
702 open_vconn(vconn_name, &vconn);
703 version = vconn_get_version(vconn);
704 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
705 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
707 has_ports = ofputil_switch_features_has_ports(reply);
708 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
709 ofpbuf_delete(reply);
712 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
713 dump_stats_transaction(vconn, request);
715 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
720 ofctl_dump_desc(struct ovs_cmdl_context *ctx)
722 dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
726 ofctl_dump_tables(struct ovs_cmdl_context *ctx)
728 dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
732 ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
734 struct ofpbuf *request;
737 open_vconn(ctx->argv[1], &vconn);
738 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
740 /* The following is similar to dump_trivial_stats_transaction(), but it
741 * maintains the previous 'ofputil_table_features' from one stats reply
742 * message to the next, which allows duplication to be eliminated in the
743 * output across messages. Otherwise the output is much larger and harder
744 * to read, because only 17 or so ofputil_table_features elements fit in a
745 * single 64 kB OpenFlow message and therefore you get a ton of repetition
746 * (every 17th element is printed in full instead of abbreviated). */
748 const struct ofp_header *request_oh = request->data;
749 ovs_be32 send_xid = request_oh->xid;
752 struct ofputil_table_features prev;
755 send_openflow_buffer(vconn, request);
758 struct ofpbuf *reply;
760 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
761 recv_xid = ((struct ofp_header *) reply->data)->xid;
762 if (send_xid == recv_xid) {
765 error = ofptype_decode(&type, reply->data);
767 ovs_fatal(0, "decode error: %s", ofperr_get_name(error));
768 } else if (type == OFPTYPE_ERROR) {
769 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
771 } else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) {
772 done = !ofpmp_more(reply->data);
774 struct ofputil_table_features tf;
777 retval = ofputil_decode_table_features(reply, &tf, true);
780 ovs_fatal(0, "decode error: %s",
781 ofperr_get_name(retval));
786 struct ds s = DS_EMPTY_INITIALIZER;
787 ofp_print_table_features(&s, &tf, n ? &prev : NULL,
796 ovs_fatal(0, "received bad reply: %s",
797 ofp_to_string(reply->data, reply->size,
801 VLOG_DBG("received reply with xid %08"PRIx32" "
802 "!= expected %08"PRIx32, recv_xid, send_xid);
804 ofpbuf_delete(reply);
811 ofctl_dump_table_desc(struct ovs_cmdl_context *ctx)
813 struct ofpbuf *request;
816 open_vconn(ctx->argv[1], &vconn);
817 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
819 dump_stats_transaction(vconn, request);
826 static bool fetch_port_by_stats(struct vconn *,
827 const char *port_name, ofp_port_t port_no,
828 struct ofputil_phy_port *);
830 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
831 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
832 * successful, false on failure.
834 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
835 * list of ports in OFPT_FEATURES_REPLY. */
837 fetch_port_by_features(struct vconn *vconn,
838 const char *port_name, ofp_port_t port_no,
839 struct ofputil_phy_port *pp)
841 struct ofputil_switch_features features;
842 const struct ofp_header *oh;
843 struct ofpbuf *request, *reply;
849 /* Fetch the switch's ofp_switch_features. */
850 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
851 vconn_get_version(vconn), 0);
852 run(vconn_transact(vconn, request, &reply),
853 "talking to %s", vconn_get_name(vconn));
856 if (ofptype_decode(&type, reply->data)
857 || type != OFPTYPE_FEATURES_REPLY) {
858 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
860 if (!ofputil_switch_features_has_ports(reply)) {
861 /* The switch features reply does not contain a complete list of ports.
862 * Probably, there are more ports than will fit into a single 64 kB
863 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
865 ofpbuf_delete(reply);
866 return fetch_port_by_stats(vconn, port_name, port_no, pp);
869 error = ofputil_decode_switch_features(oh, &features, &b);
871 ovs_fatal(0, "%s: failed to decode features reply (%s)",
872 vconn_get_name(vconn), ofperr_to_string(error));
875 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
876 if (port_no != OFPP_NONE
877 ? port_no == pp->port_no
878 : !strcmp(pp->name, port_name)) {
883 ofpbuf_delete(reply);
887 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
888 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
889 * successful, false on failure.
891 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
892 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
893 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
894 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
896 fetch_port_by_stats(struct vconn *vconn,
897 const char *port_name, ofp_port_t port_no,
898 struct ofputil_phy_port *pp)
900 struct ofpbuf *request;
905 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
907 send_xid = ((struct ofp_header *) request->data)->xid;
909 send_openflow_buffer(vconn, request);
912 struct ofpbuf *reply;
914 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
915 recv_xid = ((struct ofp_header *) reply->data)->xid;
916 if (send_xid == recv_xid) {
917 struct ofp_header *oh = reply->data;
922 ofpbuf_use_const(&b, oh, ntohs(oh->length));
923 if (ofptype_pull(&type, &b)
924 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
925 ovs_fatal(0, "received bad reply: %s",
926 ofp_to_string(reply->data, reply->size,
930 flags = ofpmp_flags(oh);
931 done = !(flags & OFPSF_REPLY_MORE);
934 /* We've already found the port, but we need to drain
935 * the queue of any other replies for this request. */
939 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
940 if (port_no != OFPP_NONE ? port_no == pp->port_no
941 : !strcmp(pp->name, port_name)) {
947 VLOG_DBG("received reply with xid %08"PRIx32" "
948 "!= expected %08"PRIx32, recv_xid, send_xid);
950 ofpbuf_delete(reply);
957 str_to_ofp(const char *s, ofp_port_t *ofp_port)
962 ret = str_to_uint(s, 10, &port_);
963 *ofp_port = u16_to_ofp(port_);
967 /* Opens a connection to 'vconn_name', fetches the port structure for
968 * 'port_name' (which may be a port name or number), and copies it into
971 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
972 struct ofputil_phy_port *pp)
978 /* Try to interpret the argument as a port number. */
979 if (!str_to_ofp(port_name, &port_no)) {
983 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
984 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
985 * into the OFPST_PORT_DESC reply. Try it the correct way. */
986 open_vconn(vconn_name, &vconn);
987 found = (vconn_get_version(vconn) < OFP13_VERSION
988 ? fetch_port_by_features(vconn, port_name, port_no, pp)
989 : fetch_port_by_stats(vconn, port_name, port_no, pp));
993 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
997 /* Returns the port number corresponding to 'port_name' (which may be a port
998 * name or number) within the switch 'vconn_name'. */
1000 str_to_port_no(const char *vconn_name, const char *port_name)
1004 if (ofputil_port_from_string(port_name, &port_no)) {
1007 struct ofputil_phy_port pp;
1009 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
1015 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
1016 enum ofputil_protocol *cur)
1019 struct ofpbuf *request, *reply;
1020 enum ofputil_protocol next;
1022 request = ofputil_encode_set_protocol(*cur, want, &next);
1024 return *cur == want;
1027 run(vconn_transact_noreply(vconn, request, &reply),
1028 "talking to %s", vconn_get_name(vconn));
1030 char *s = ofp_to_string(reply->data, reply->size, 2);
1031 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
1032 vconn_get_name(vconn), s);
1034 ofpbuf_delete(reply);
1042 static enum ofputil_protocol
1043 set_protocol_for_flow_dump(struct vconn *vconn,
1044 enum ofputil_protocol cur_protocol,
1045 enum ofputil_protocol usable_protocols)
1050 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
1051 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
1052 if (f & usable_protocols & allowed_protocols
1053 && try_set_protocol(vconn, f, &cur_protocol)) {
1058 usable_s = ofputil_protocols_to_string(usable_protocols);
1059 if (usable_protocols & allowed_protocols) {
1060 ovs_fatal(0, "switch does not support any of the usable flow "
1061 "formats (%s)", usable_s);
1063 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1064 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1065 "allowed flow formats (%s)", usable_s, allowed_s);
1069 static struct vconn *
1070 prepare_dump_flows(int argc, char *argv[], bool aggregate,
1071 struct ofpbuf **requestp)
1073 enum ofputil_protocol usable_protocols, protocol;
1074 struct ofputil_flow_stats_request fsr;
1075 struct vconn *vconn;
1078 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
1079 argc > 2 ? argv[2] : "",
1082 ovs_fatal(0, "%s", error);
1085 protocol = open_vconn(argv[1], &vconn);
1086 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1087 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
1092 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
1094 struct ofpbuf *request;
1095 struct vconn *vconn;
1097 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
1098 dump_stats_transaction(vconn, request);
1103 compare_flows(const void *afs_, const void *bfs_)
1105 const struct ofputil_flow_stats *afs = afs_;
1106 const struct ofputil_flow_stats *bfs = bfs_;
1107 const struct match *a = &afs->match;
1108 const struct match *b = &bfs->match;
1109 const struct sort_criterion *sc;
1111 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
1112 const struct mf_field *f = sc->field;
1116 int a_pri = afs->priority;
1117 int b_pri = bfs->priority;
1118 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
1122 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
1123 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
1125 /* Skip the test for sc->order, so that missing fields always
1126 * sort to the end whether we're sorting in ascending or
1127 * descending order. */
1128 return ina ? -1 : 1;
1130 union mf_value aval, bval;
1132 mf_get_value(f, &a->flow, &aval);
1133 mf_get_value(f, &b->flow, &bval);
1134 ret = memcmp(&aval, &bval, f->n_bytes);
1139 return sc->order == SORT_ASC ? ret : -ret;
1147 ofctl_dump_flows(struct ovs_cmdl_context *ctx)
1150 ofctl_dump_flows__(ctx->argc, ctx->argv, false);
1153 struct ofputil_flow_stats *fses;
1154 size_t n_fses, allocated_fses;
1155 struct ofpbuf *request;
1156 struct ofpbuf ofpacts;
1157 struct ofpbuf *reply;
1158 struct vconn *vconn;
1163 vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request);
1164 send_xid = ((struct ofp_header *) request->data)->xid;
1165 send_openflow_buffer(vconn, request);
1168 n_fses = allocated_fses = 0;
1170 ofpbuf_init(&ofpacts, 0);
1172 struct ofputil_flow_stats *fs;
1174 if (n_fses >= allocated_fses) {
1175 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1179 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1183 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1186 ofpbuf_uninit(&ofpacts);
1188 qsort(fses, n_fses, sizeof *fses, compare_flows);
1191 for (i = 0; i < n_fses; i++) {
1193 ofp_print_flow_stats(&s, &fses[i]);
1198 for (i = 0; i < n_fses; i++) {
1199 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1208 ofctl_dump_aggregate(struct ovs_cmdl_context *ctx)
1210 ofctl_dump_flows__(ctx->argc, ctx->argv, true);
1214 ofctl_queue_stats(struct ovs_cmdl_context *ctx)
1216 struct ofpbuf *request;
1217 struct vconn *vconn;
1218 struct ofputil_queue_stats_request oqs;
1220 open_vconn(ctx->argv[1], &vconn);
1222 if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) {
1223 oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1225 oqs.port_no = OFPP_ANY;
1227 if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) {
1228 oqs.queue_id = atoi(ctx->argv[3]);
1230 oqs.queue_id = OFPQ_ALL;
1233 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1234 dump_stats_transaction(vconn, request);
1239 ofctl_queue_get_config(struct ovs_cmdl_context *ctx)
1241 const char *vconn_name = ctx->argv[1];
1242 const char *port_name = ctx->argv[2];
1243 enum ofputil_protocol protocol;
1244 enum ofp_version version;
1245 struct ofpbuf *request;
1246 struct vconn *vconn;
1249 port = str_to_port_no(vconn_name, port_name);
1251 protocol = open_vconn(vconn_name, &vconn);
1252 version = ofputil_protocol_to_ofp_version(protocol);
1253 request = ofputil_encode_queue_get_config_request(version, port);
1254 dump_transaction(vconn, request);
1258 static enum ofputil_protocol
1259 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1260 enum ofputil_protocol usable_protocols)
1262 enum ofputil_protocol cur_protocol;
1266 if (!(usable_protocols & allowed_protocols)) {
1267 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1268 usable_s = ofputil_protocols_to_string(usable_protocols);
1269 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1270 "allowed flow formats (%s)", usable_s, allowed_s);
1273 /* If the initial flow format is allowed and usable, keep it. */
1274 cur_protocol = open_vconn(remote, vconnp);
1275 if (usable_protocols & allowed_protocols & cur_protocol) {
1276 return cur_protocol;
1279 /* Otherwise try each flow format in turn. */
1280 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1281 enum ofputil_protocol f = 1 << i;
1283 if (f != cur_protocol
1284 && f & usable_protocols & allowed_protocols
1285 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1290 usable_s = ofputil_protocols_to_string(usable_protocols);
1291 ovs_fatal(0, "switch does not support any of the usable flow "
1292 "formats (%s)", usable_s);
1296 bundle_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1297 size_t n_fms, enum ofputil_protocol usable_protocols)
1299 enum ofputil_protocol protocol;
1300 struct vconn *vconn;
1301 struct ovs_list requests;
1304 list_init(&requests);
1306 /* Bundles need OpenFlow 1.4+. */
1307 usable_protocols &= OFPUTIL_P_OF14_UP;
1308 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1310 for (i = 0; i < n_fms; i++) {
1311 struct ofputil_flow_mod *fm = &fms[i];
1312 struct ofpbuf *request = ofputil_encode_flow_mod(fm, protocol);
1314 list_push_back(&requests, &request->list_node);
1315 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1318 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
1323 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1324 size_t n_fms, enum ofputil_protocol usable_protocols)
1326 enum ofputil_protocol protocol;
1327 struct vconn *vconn;
1331 bundle_flow_mod__(remote, fms, n_fms, usable_protocols);
1335 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1337 for (i = 0; i < n_fms; i++) {
1338 struct ofputil_flow_mod *fm = &fms[i];
1340 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1341 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1347 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], int command)
1349 enum ofputil_protocol usable_protocols;
1350 struct ofputil_flow_mod *fms = NULL;
1354 if (command == OFPFC_ADD) {
1355 /* Allow the file to specify a mix of commands. If none specified at
1356 * the beginning of any given line, then the default is OFPFC_ADD, so
1357 * this is backwards compatible. */
1360 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1363 ovs_fatal(0, "%s", error);
1365 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1370 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1372 if (argc > 2 && !strcmp(argv[2], "-")) {
1373 ofctl_flow_mod_file(argc, argv, command);
1375 struct ofputil_flow_mod fm;
1377 enum ofputil_protocol usable_protocols;
1379 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1382 ovs_fatal(0, "%s", error);
1384 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1389 ofctl_add_flow(struct ovs_cmdl_context *ctx)
1391 ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD);
1395 ofctl_add_flows(struct ovs_cmdl_context *ctx)
1397 ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD);
1401 ofctl_mod_flows(struct ovs_cmdl_context *ctx)
1403 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1407 ofctl_del_flows(struct ovs_cmdl_context *ctx)
1409 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1413 set_packet_in_format(struct vconn *vconn,
1414 enum nx_packet_in_format packet_in_format)
1416 struct ofpbuf *spif;
1418 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1420 transact_noreply(vconn, spif);
1421 VLOG_DBG("%s: using user-specified packet in format %s",
1422 vconn_get_name(vconn),
1423 ofputil_packet_in_format_to_string(packet_in_format));
1427 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1429 struct ofp_switch_config config;
1430 enum ofp_config_flags flags;
1432 fetch_switch_config(vconn, &config);
1433 flags = ntohs(config.flags);
1434 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1435 /* Set the invalid ttl config. */
1436 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1438 config.flags = htons(flags);
1439 set_switch_config(vconn, &config);
1441 /* Then retrieve the configuration to see if it really took. OpenFlow
1442 * doesn't define error reporting for bad modes, so this is all we can
1444 fetch_switch_config(vconn, &config);
1445 flags = ntohs(config.flags);
1446 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1447 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1448 "switch probably doesn't support mode)");
1455 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1456 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1457 * an error message and stores NULL in '*msgp'. */
1459 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1461 struct ofp_header *oh;
1464 msg = ofpbuf_new(strlen(hex) / 2);
1467 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1469 return "Trailing garbage in hex data";
1472 if (msg->size < sizeof(struct ofp_header)) {
1474 return "Message too short for OpenFlow";
1478 if (msg->size != ntohs(oh->length)) {
1480 return "Message size does not match length in OpenFlow header";
1488 ofctl_send(struct unixctl_conn *conn, int argc,
1489 const char *argv[], void *vconn_)
1491 struct vconn *vconn = vconn_;
1498 for (i = 1; i < argc; i++) {
1499 const char *error_msg;
1503 error_msg = openflow_from_hex(argv[i], &msg);
1505 ds_put_format(&reply, "%s\n", error_msg);
1510 fprintf(stderr, "send: ");
1511 ofp_print(stderr, msg->data, msg->size, verbosity);
1513 error = vconn_send_block(vconn, msg);
1516 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1519 ds_put_cstr(&reply, "sent\n");
1524 unixctl_command_reply(conn, ds_cstr(&reply));
1526 unixctl_command_reply_error(conn, ds_cstr(&reply));
1531 struct barrier_aux {
1532 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1533 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1537 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1538 const char *argv[] OVS_UNUSED, void *aux_)
1540 struct barrier_aux *aux = aux_;
1545 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1549 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1550 error = vconn_send_block(aux->vconn, msg);
1553 unixctl_command_reply_error(conn, ovs_strerror(error));
1560 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1561 const char *argv[], void *aux OVS_UNUSED)
1565 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1567 unixctl_command_reply_error(conn, ovs_strerror(errno));
1572 dup2(fd, STDERR_FILENO);
1574 unixctl_command_reply(conn, NULL);
1578 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1579 const char *argv[] OVS_UNUSED, void *blocked_)
1581 bool *blocked = blocked_;
1585 unixctl_command_reply(conn, NULL);
1587 unixctl_command_reply(conn, "already blocking");
1592 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1593 const char *argv[] OVS_UNUSED, void *blocked_)
1595 bool *blocked = blocked_;
1599 unixctl_command_reply(conn, NULL);
1601 unixctl_command_reply(conn, "already unblocked");
1605 /* Prints to stdout all of the messages received on 'vconn'.
1607 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1608 * received on 'vconn'. */
1610 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1612 struct barrier_aux barrier_aux = { vconn, NULL };
1613 struct unixctl_server *server;
1614 bool exiting = false;
1615 bool blocked = false;
1618 daemon_save_fd(STDERR_FILENO);
1619 daemonize_start(false);
1620 error = unixctl_server_create(unixctl_path, &server);
1622 ovs_fatal(error, "failed to create unixctl server");
1624 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1625 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1627 unixctl_command_register("ofctl/barrier", "", 0, 0,
1628 ofctl_barrier, &barrier_aux);
1629 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1630 ofctl_set_output_file, NULL);
1632 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1633 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1636 daemonize_complete();
1642 unixctl_server_run(server);
1647 retval = vconn_recv(vconn, &b);
1648 if (retval == EAGAIN) {
1651 run(retval, "vconn_recv");
1654 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1655 time_wall_msec(), true);
1660 ofptype_decode(&type, b->data);
1661 ofp_print(stderr, b->data, b->size, verbosity + 2);
1664 switch ((int) type) {
1665 case OFPTYPE_BARRIER_REPLY:
1666 if (barrier_aux.conn) {
1667 unixctl_command_reply(barrier_aux.conn, NULL);
1668 barrier_aux.conn = NULL;
1672 case OFPTYPE_ECHO_REQUEST:
1673 if (reply_to_echo_requests) {
1674 struct ofpbuf *reply;
1676 reply = make_echo_reply(b->data);
1677 retval = vconn_send_block(vconn, reply);
1679 ovs_fatal(retval, "failed to send echo reply");
1692 vconn_run_wait(vconn);
1694 vconn_recv_wait(vconn);
1696 unixctl_server_wait(server);
1700 unixctl_server_destroy(server);
1704 ofctl_monitor(struct ovs_cmdl_context *ctx)
1706 struct vconn *vconn;
1708 enum ofputil_protocol usable_protocols;
1710 open_vconn(ctx->argv[1], &vconn);
1711 for (i = 2; i < ctx->argc; i++) {
1712 const char *arg = ctx->argv[i];
1714 if (isdigit((unsigned char) *arg)) {
1715 struct ofp_switch_config config;
1717 fetch_switch_config(vconn, &config);
1718 config.miss_send_len = htons(atoi(arg));
1719 set_switch_config(vconn, &config);
1720 } else if (!strcmp(arg, "invalid_ttl")) {
1721 monitor_set_invalid_ttl_to_controller(vconn);
1722 } else if (!strncmp(arg, "watch:", 6)) {
1723 struct ofputil_flow_monitor_request fmr;
1727 error = parse_flow_monitor_request(&fmr, arg + 6,
1730 ovs_fatal(0, "%s", error);
1733 msg = ofpbuf_new(0);
1734 ofputil_append_flow_monitor_request(&fmr, msg);
1735 dump_stats_transaction(vconn, msg);
1738 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1742 if (preferred_packet_in_format >= 0) {
1743 set_packet_in_format(vconn, preferred_packet_in_format);
1745 enum ofp_version version = vconn_get_version(vconn);
1748 case OFP10_VERSION: {
1749 struct ofpbuf *spif, *reply;
1751 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1753 run(vconn_transact_noreply(vconn, spif, &reply),
1754 "talking to %s", vconn_get_name(vconn));
1756 char *s = ofp_to_string(reply->data, reply->size, 2);
1757 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1758 " replied: %s. Falling back to the switch default.",
1759 vconn_get_name(vconn), s);
1761 ofpbuf_delete(reply);
1776 monitor_vconn(vconn, true);
1780 ofctl_snoop(struct ovs_cmdl_context *ctx)
1782 struct vconn *vconn;
1784 open_vconn__(ctx->argv[1], SNOOP, &vconn);
1785 monitor_vconn(vconn, false);
1789 ofctl_dump_ports(struct ovs_cmdl_context *ctx)
1791 struct ofpbuf *request;
1792 struct vconn *vconn;
1795 open_vconn(ctx->argv[1], &vconn);
1796 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1797 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1798 dump_stats_transaction(vconn, request);
1803 ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx)
1805 struct ofpbuf *request;
1806 struct vconn *vconn;
1809 open_vconn(ctx->argv[1], &vconn);
1810 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1811 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1813 dump_stats_transaction(vconn, request);
1818 ofctl_probe(struct ovs_cmdl_context *ctx)
1820 struct ofpbuf *request;
1821 struct vconn *vconn;
1822 struct ofpbuf *reply;
1824 open_vconn(ctx->argv[1], &vconn);
1825 request = make_echo_request(vconn_get_version(vconn));
1826 run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]);
1827 if (reply->size != sizeof(struct ofp_header)) {
1828 ovs_fatal(0, "reply does not match request");
1830 ofpbuf_delete(reply);
1835 ofctl_packet_out(struct ovs_cmdl_context *ctx)
1837 enum ofputil_protocol protocol;
1838 struct ofputil_packet_out po;
1839 struct ofpbuf ofpacts;
1840 struct vconn *vconn;
1843 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1845 ofpbuf_init(&ofpacts, 64);
1846 error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, &usable_protocols);
1848 ovs_fatal(0, "%s", error);
1851 po.buffer_id = UINT32_MAX;
1852 po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1853 po.ofpacts = ofpacts.data;
1854 po.ofpacts_len = ofpacts.size;
1856 protocol = open_vconn(ctx->argv[1], &vconn);
1857 for (i = 4; i < ctx->argc; i++) {
1858 struct dp_packet *packet;
1860 const char *error_msg;
1862 error_msg = eth_from_hex(ctx->argv[i], &packet);
1864 ovs_fatal(0, "%s", error_msg);
1867 po.packet = dp_packet_data(packet);
1868 po.packet_len = dp_packet_size(packet);
1869 opo = ofputil_encode_packet_out(&po, protocol);
1870 transact_noreply(vconn, opo);
1871 dp_packet_delete(packet);
1874 ofpbuf_uninit(&ofpacts);
1878 ofctl_mod_port(struct ovs_cmdl_context *ctx)
1880 struct ofp_config_flag {
1881 const char *name; /* The flag's name. */
1882 enum ofputil_port_config bit; /* Bit to turn on or off. */
1883 bool on; /* Value to set the bit to. */
1885 static const struct ofp_config_flag flags[] = {
1886 { "up", OFPUTIL_PC_PORT_DOWN, false },
1887 { "down", OFPUTIL_PC_PORT_DOWN, true },
1888 { "stp", OFPUTIL_PC_NO_STP, false },
1889 { "receive", OFPUTIL_PC_NO_RECV, false },
1890 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1891 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1892 { "forward", OFPUTIL_PC_NO_FWD, false },
1893 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1896 const struct ofp_config_flag *flag;
1897 enum ofputil_protocol protocol;
1898 struct ofputil_port_mod pm;
1899 struct ofputil_phy_port pp;
1900 struct vconn *vconn;
1901 const char *command;
1904 fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp);
1906 pm.port_no = pp.port_no;
1907 pm.hw_addr = pp.hw_addr;
1912 if (!strncasecmp(ctx->argv[3], "no-", 3)) {
1913 command = ctx->argv[3] + 3;
1915 } else if (!strncasecmp(ctx->argv[3], "no", 2)) {
1916 command = ctx->argv[3] + 2;
1919 command = ctx->argv[3];
1922 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1923 if (!strcasecmp(command, flag->name)) {
1924 pm.mask = flag->bit;
1925 pm.config = flag->on ^ not ? flag->bit : 0;
1929 ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]);
1932 protocol = open_vconn(ctx->argv[1], &vconn);
1933 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1937 /* This function uses OFPMP14_TABLE_DESC request to get the current
1938 * table configuration from switch. The function then modifies
1939 * only that table-config property, which has been requested. */
1941 fetch_table_desc(struct vconn *vconn, struct ofputil_table_mod *tm,
1942 struct ofputil_table_desc *td)
1944 struct ofpbuf *request;
1949 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
1950 send_xid = ((struct ofp_header *) request->data)->xid;
1951 send_openflow_buffer(vconn, request);
1954 struct ofpbuf *reply;
1956 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1957 recv_xid = ((struct ofp_header *) reply->data)->xid;
1958 if (send_xid == recv_xid) {
1959 struct ofp_header *oh = reply->data;
1964 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1965 if (ofptype_pull(&type, &b)
1966 || type != OFPTYPE_TABLE_DESC_REPLY) {
1967 ovs_fatal(0, "received bad reply: %s",
1968 ofp_to_string(reply->data, reply->size,
1971 flags = ofpmp_flags(oh);
1972 done = !(flags & OFPSF_REPLY_MORE);
1974 /* We've already found the table desc consisting of current
1975 * table configuration, but we need to drain the queue of
1976 * any other replies for this request. */
1979 while (!ofputil_decode_table_desc(&b, td, oh->version)) {
1980 if (td->table_id == tm->table_id) {
1986 VLOG_DBG("received reply with xid %08"PRIx32" "
1987 "!= expected %08"PRIx32, recv_xid, send_xid);
1989 ofpbuf_delete(reply);
1991 if (tm->eviction != OFPUTIL_TABLE_EVICTION_DEFAULT) {
1992 tm->vacancy = td->vacancy;
1993 tm->table_vacancy.vacancy_down = td->table_vacancy.vacancy_down;
1994 tm->table_vacancy.vacancy_up = td->table_vacancy.vacancy_up;
1995 } else if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
1996 tm->eviction = td->eviction;
1997 tm->eviction_flags = td->eviction_flags;
2002 ofctl_mod_table(struct ovs_cmdl_context *ctx)
2004 uint32_t usable_versions;
2005 struct ofputil_table_mod tm;
2006 struct vconn *vconn;
2010 error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3],
2013 ovs_fatal(0, "%s", error);
2016 uint32_t allowed_versions = get_allowed_ofp_versions();
2017 if (!(allowed_versions & usable_versions)) {
2018 struct ds versions = DS_EMPTY_INITIALIZER;
2019 ofputil_format_version_bitmap_names(&versions, usable_versions);
2020 ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow "
2022 ctx->argv[3], ds_cstr(&versions));
2024 mask_allowed_ofp_versions(usable_versions);
2025 enum ofputil_protocol protocol = open_vconn(ctx->argv[1], &vconn);
2027 /* For OpenFlow 1.4+, ovs-ofctl mod-table should not affect table-config
2028 * properties that the user didn't ask to change, so it is necessary to
2029 * restore the current configuration of table-config parameters using
2030 * OFPMP14_TABLE_DESC request. */
2031 if ((allowed_versions & (1u << OFP14_VERSION)) ||
2032 (allowed_versions & (1u << OFP15_VERSION))) {
2033 struct ofputil_table_desc td;
2035 if (tm.table_id == OFPTT_ALL) {
2036 for (i = 0; i < OFPTT_MAX; i++) {
2038 fetch_table_desc(vconn, &tm, &td);
2039 transact_noreply(vconn,
2040 ofputil_encode_table_mod(&tm, protocol));
2043 fetch_table_desc(vconn, &tm, &td);
2044 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2047 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2053 ofctl_get_frags(struct ovs_cmdl_context *ctx)
2055 struct ofp_switch_config config;
2056 struct vconn *vconn;
2058 open_vconn(ctx->argv[1], &vconn);
2059 fetch_switch_config(vconn, &config);
2060 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
2065 ofctl_set_frags(struct ovs_cmdl_context *ctx)
2067 struct ofp_switch_config config;
2068 enum ofp_config_flags mode;
2069 struct vconn *vconn;
2072 if (!ofputil_frag_handling_from_string(ctx->argv[2], &mode)) {
2073 ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]);
2076 open_vconn(ctx->argv[1], &vconn);
2077 fetch_switch_config(vconn, &config);
2078 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
2079 if (flags != config.flags) {
2080 /* Set the configuration. */
2081 config.flags = flags;
2082 set_switch_config(vconn, &config);
2084 /* Then retrieve the configuration to see if it really took. OpenFlow
2085 * doesn't define error reporting for bad modes, so this is all we can
2087 fetch_switch_config(vconn, &config);
2088 if (flags != config.flags) {
2089 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
2090 "switch probably doesn't support mode \"%s\")",
2091 ctx->argv[1], ofputil_frag_handling_to_string(mode));
2098 ofctl_ofp_parse(struct ovs_cmdl_context *ctx)
2100 const char *filename = ctx->argv[1];
2104 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2106 ovs_fatal(errno, "%s: open", filename);
2109 ofpbuf_init(&b, 65536);
2111 struct ofp_header *oh;
2112 size_t length, tail_len;
2117 oh = ofpbuf_put_uninit(&b, sizeof *oh);
2118 n = fread(oh, 1, sizeof *oh, file);
2121 } else if (n < sizeof *oh) {
2122 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2125 length = ntohs(oh->length);
2126 if (length < sizeof *oh) {
2127 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
2131 tail_len = length - sizeof *oh;
2132 tail = ofpbuf_put_uninit(&b, tail_len);
2133 n = fread(tail, 1, tail_len, file);
2135 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2138 ofp_print(stdout, b.data, b.size, verbosity + 2);
2142 if (file != stdin) {
2148 is_openflow_port(ovs_be16 port_, char *ports[])
2150 uint16_t port = ntohs(port_);
2154 for (i = 0; ports[i]; i++) {
2155 if (port == atoi(ports[i])) {
2161 return port == OFP_PORT || port == OFP_OLD_PORT;
2166 ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx)
2168 struct tcp_reader *reader;
2173 file = ovs_pcap_open(ctx->argv[1], "rb");
2175 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
2178 reader = tcp_reader_open();
2181 struct dp_packet *packet;
2185 error = ovs_pcap_read(file, &packet, &when);
2189 pkt_metadata_init(&packet->md, ODPP_NONE);
2190 flow_extract(packet, &flow);
2191 if (flow.dl_type == htons(ETH_TYPE_IP)
2192 && flow.nw_proto == IPPROTO_TCP
2193 && (is_openflow_port(flow.tp_src, ctx->argv + 2) ||
2194 is_openflow_port(flow.tp_dst, ctx->argv + 2))) {
2195 struct dp_packet *payload = tcp_reader_run(reader, &flow, packet);
2197 while (dp_packet_size(payload) >= sizeof(struct ofp_header)) {
2198 const struct ofp_header *oh;
2199 void *data = dp_packet_data(payload);
2202 /* Align OpenFlow on 8-byte boundary for safe access. */
2203 dp_packet_shift(payload, -((intptr_t) data & 7));
2205 oh = dp_packet_data(payload);
2206 length = ntohs(oh->length);
2207 if (dp_packet_size(payload) < length) {
2217 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
2222 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
2223 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
2224 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
2225 ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1);
2226 dp_packet_pull(payload, length);
2230 dp_packet_delete(packet);
2232 tcp_reader_close(reader);
2236 ofctl_ping(struct ovs_cmdl_context *ctx)
2238 size_t max_payload = 65535 - sizeof(struct ofp_header);
2239 unsigned int payload;
2240 struct vconn *vconn;
2243 payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64;
2244 if (payload > max_payload) {
2245 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2248 open_vconn(ctx->argv[1], &vconn);
2249 for (i = 0; i < 10; i++) {
2250 struct timeval start, end;
2251 struct ofpbuf *request, *reply;
2252 const struct ofp_header *rpy_hdr;
2255 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2256 vconn_get_version(vconn), payload);
2257 random_bytes(ofpbuf_put_uninit(request, payload), payload);
2259 xgettimeofday(&start);
2260 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2261 xgettimeofday(&end);
2263 rpy_hdr = reply->data;
2264 if (ofptype_pull(&type, reply)
2265 || type != OFPTYPE_ECHO_REPLY
2266 || reply->size != payload
2267 || memcmp(request->msg, reply->msg, payload)) {
2268 printf("Reply does not match request. Request:\n");
2269 ofp_print(stdout, request, request->size, verbosity + 2);
2271 ofp_print(stdout, reply, reply->size, verbosity + 2);
2273 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2274 reply->size, ctx->argv[1], ntohl(rpy_hdr->xid),
2275 (1000*(double)(end.tv_sec - start.tv_sec))
2276 + (.001*(end.tv_usec - start.tv_usec)));
2277 ofpbuf_delete(request);
2278 ofpbuf_delete(reply);
2284 ofctl_benchmark(struct ovs_cmdl_context *ctx)
2286 size_t max_payload = 65535 - sizeof(struct ofp_header);
2287 struct timeval start, end;
2288 unsigned int payload_size, message_size;
2289 struct vconn *vconn;
2294 payload_size = atoi(ctx->argv[2]);
2295 if (payload_size > max_payload) {
2296 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2298 message_size = sizeof(struct ofp_header) + payload_size;
2300 count = atoi(ctx->argv[3]);
2302 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2303 count, message_size, count * message_size);
2305 open_vconn(ctx->argv[1], &vconn);
2306 xgettimeofday(&start);
2307 for (i = 0; i < count; i++) {
2308 struct ofpbuf *request, *reply;
2310 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2311 vconn_get_version(vconn), payload_size);
2312 ofpbuf_put_zeros(request, payload_size);
2313 run(vconn_transact(vconn, request, &reply), "transact");
2314 ofpbuf_delete(reply);
2316 xgettimeofday(&end);
2319 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2320 + (.001*(end.tv_usec - start.tv_usec)));
2321 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2322 duration, count / (duration / 1000.0),
2323 count * message_size / (duration / 1000.0));
2327 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2328 size_t n_gms, enum ofputil_protocol usable_protocols)
2330 enum ofputil_protocol protocol;
2331 struct ofputil_group_mod *gm;
2332 enum ofp_version version;
2333 struct ofpbuf *request;
2335 struct vconn *vconn;
2338 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2339 version = ofputil_protocol_to_ofp_version(protocol);
2341 for (i = 0; i < n_gms; i++) {
2343 request = ofputil_encode_group_mod(version, gm);
2345 transact_noreply(vconn, request);
2355 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2357 struct ofputil_group_mod *gms = NULL;
2358 enum ofputil_protocol usable_protocols;
2363 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2366 ovs_fatal(0, "%s", error);
2368 ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
2369 for (i = 0; i < n_gms; i++) {
2370 ofputil_bucket_list_destroy(&gms[i].buckets);
2376 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2378 if (argc > 2 && !strcmp(argv[2], "-")) {
2379 ofctl_group_mod_file(argc, argv, command);
2381 enum ofputil_protocol usable_protocols;
2382 struct ofputil_group_mod gm;
2385 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2388 ovs_fatal(0, "%s", error);
2390 ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
2391 ofputil_bucket_list_destroy(&gm.buckets);
2396 ofctl_add_group(struct ovs_cmdl_context *ctx)
2398 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD);
2402 ofctl_add_groups(struct ovs_cmdl_context *ctx)
2404 ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD);
2408 ofctl_mod_group(struct ovs_cmdl_context *ctx)
2410 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_MODIFY);
2414 ofctl_del_groups(struct ovs_cmdl_context *ctx)
2416 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE);
2420 ofctl_insert_bucket(struct ovs_cmdl_context *ctx)
2422 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET);
2426 ofctl_remove_bucket(struct ovs_cmdl_context *ctx)
2428 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET);
2432 ofctl_dump_group_stats(struct ovs_cmdl_context *ctx)
2434 enum ofputil_protocol usable_protocols;
2435 struct ofputil_group_mod gm;
2436 struct ofpbuf *request;
2437 struct vconn *vconn;
2441 memset(&gm, 0, sizeof gm);
2443 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2444 ctx->argc > 2 ? ctx->argv[2] : "",
2447 ovs_fatal(0, "%s", error);
2450 group_id = gm.group_id;
2452 open_vconn(ctx->argv[1], &vconn);
2453 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2456 dump_stats_transaction(vconn, request);
2463 ofctl_dump_group_desc(struct ovs_cmdl_context *ctx)
2465 struct ofpbuf *request;
2466 struct vconn *vconn;
2469 open_vconn(ctx->argv[1], &vconn);
2471 if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) {
2472 group_id = OFPG_ALL;
2475 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2478 dump_stats_transaction(vconn, request);
2485 ofctl_dump_group_features(struct ovs_cmdl_context *ctx)
2487 struct ofpbuf *request;
2488 struct vconn *vconn;
2490 open_vconn(ctx->argv[1], &vconn);
2491 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2493 dump_stats_transaction(vconn, request);
2500 ofctl_geneve_mod(struct ovs_cmdl_context *ctx, uint16_t command)
2502 enum ofputil_protocol usable_protocols;
2503 enum ofputil_protocol protocol;
2504 struct ofputil_geneve_table_mod gtm;
2506 enum ofp_version version;
2507 struct ofpbuf *request;
2508 struct vconn *vconn;
2510 error = parse_ofp_geneve_table_mod_str(>m, command, ctx->argc > 2 ?
2514 ovs_fatal(0, "%s", error);
2517 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols);
2518 version = ofputil_protocol_to_ofp_version(protocol);
2520 request = ofputil_encode_geneve_table_mod(version, >m);
2522 transact_noreply(vconn, request);
2526 ofputil_uninit_geneve_table(>m.mappings);
2530 ofctl_add_geneve_map(struct ovs_cmdl_context *ctx)
2532 ofctl_geneve_mod(ctx, NXGTMC_ADD);
2536 ofctl_del_geneve_map(struct ovs_cmdl_context *ctx)
2538 ofctl_geneve_mod(ctx, ctx->argc > 2 ? NXGTMC_DELETE : NXGTMC_CLEAR);
2542 ofctl_dump_geneve_map(struct ovs_cmdl_context *ctx)
2544 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXT_GENEVE_TABLE_REQUEST);
2548 ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
2554 ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
2556 ovs_cmdl_print_commands(get_all_commands());
2559 /* replace-flows and diff-flows commands. */
2561 /* A flow table entry, possibly with two different versions. */
2563 struct cls_rule rule; /* Within a "struct classifier". */
2564 struct fte_version *versions[2];
2567 /* One version of a Flow Table Entry. */
2568 struct fte_version {
2570 uint16_t idle_timeout;
2571 uint16_t hard_timeout;
2572 uint16_t importance;
2574 struct ofpact *ofpacts;
2578 /* Frees 'version' and the data that it owns. */
2580 fte_version_free(struct fte_version *version)
2583 free(CONST_CAST(struct ofpact *, version->ofpacts));
2588 /* Returns true if 'a' and 'b' are the same, false if they differ.
2590 * Ignores differences in 'flags' because there's no way to retrieve flags from
2591 * an OpenFlow switch. We have to assume that they are the same. */
2593 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2595 return (a->cookie == b->cookie
2596 && a->idle_timeout == b->idle_timeout
2597 && a->hard_timeout == b->hard_timeout
2598 && a->importance == b->importance
2599 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2600 b->ofpacts, b->ofpacts_len));
2603 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2604 * 'index' into 's', followed by a new-line. */
2606 fte_version_format(const struct fte *fte, int index, struct ds *s)
2608 const struct fte_version *version = fte->versions[index];
2615 cls_rule_format(&fte->rule, s);
2616 if (version->cookie != htonll(0)) {
2617 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2619 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2620 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2622 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2623 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2625 if (version->importance != 0) {
2626 ds_put_format(s, " importance=%"PRIu16, version->importance);
2629 ds_put_cstr(s, " actions=");
2630 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2632 ds_put_char(s, '\n');
2636 fte_from_cls_rule(const struct cls_rule *cls_rule)
2638 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2641 /* Frees 'fte' and its versions. */
2643 fte_free(struct fte *fte)
2646 fte_version_free(fte->versions[0]);
2647 fte_version_free(fte->versions[1]);
2648 cls_rule_destroy(&fte->rule);
2653 /* Frees all of the FTEs within 'cls'. */
2655 fte_free_all(struct classifier *cls)
2659 classifier_defer(cls);
2660 CLS_FOR_EACH (fte, rule, cls) {
2661 classifier_remove(cls, &fte->rule);
2662 ovsrcu_postpone(fte_free, fte);
2664 classifier_destroy(cls);
2667 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2668 * necessary. Sets 'version' as the version of that rule with the given
2669 * 'index', replacing any existing version, if any.
2671 * Takes ownership of 'version'. */
2673 fte_insert(struct classifier *cls, const struct match *match,
2674 int priority, struct fte_version *version, int index)
2676 struct fte *old, *fte;
2678 fte = xzalloc(sizeof *fte);
2679 cls_rule_init(&fte->rule, match, priority);
2680 fte->versions[index] = version;
2682 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule,
2683 CLS_MIN_VERSION, NULL, 0));
2685 fte->versions[!index] = old->versions[!index];
2686 old->versions[!index] = NULL;
2688 ovsrcu_postpone(fte_free, old);
2692 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2693 * with the specified 'index'. Returns the flow formats able to represent the
2694 * flows that were read. */
2695 static enum ofputil_protocol
2696 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2698 enum ofputil_protocol usable_protocols;
2703 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2705 ovs_fatal(errno, "%s: open", filename);
2709 usable_protocols = OFPUTIL_P_ANY;
2711 classifier_defer(cls);
2712 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2713 struct fte_version *version;
2714 struct ofputil_flow_mod fm;
2716 enum ofputil_protocol usable;
2718 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2720 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2722 usable_protocols &= usable;
2724 version = xmalloc(sizeof *version);
2725 version->cookie = fm.new_cookie;
2726 version->idle_timeout = fm.idle_timeout;
2727 version->hard_timeout = fm.hard_timeout;
2728 version->importance = fm.importance;
2729 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2730 | OFPUTIL_FF_EMERG);
2731 version->ofpacts = fm.ofpacts;
2732 version->ofpacts_len = fm.ofpacts_len;
2734 fte_insert(cls, &fm.match, fm.priority, version, index);
2736 classifier_publish(cls);
2739 if (file != stdin) {
2743 return usable_protocols;
2747 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2748 struct ofpbuf **replyp,
2749 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2751 struct ofpbuf *reply = *replyp;
2757 /* Get a flow stats reply message, if we don't already have one. */
2763 run(vconn_recv_block(vconn, &reply),
2764 "OpenFlow packet receive failed");
2765 } while (((struct ofp_header *) reply->data)->xid != send_xid);
2767 error = ofptype_decode(&type, reply->data);
2768 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2769 ovs_fatal(0, "received bad reply: %s",
2770 ofp_to_string(reply->data, reply->size,
2775 /* Pull an individual flow stats reply out of the message. */
2776 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2783 more = ofpmp_more(reply->header);
2784 ofpbuf_delete(reply);
2793 ovs_fatal(0, "parse error in reply (%s)",
2794 ofperr_to_string(retval));
2799 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2800 * format 'protocol', and adds them as flow table entries in 'cls' for the
2801 * version with the specified 'index'. */
2803 read_flows_from_switch(struct vconn *vconn,
2804 enum ofputil_protocol protocol,
2805 struct classifier *cls, int index)
2807 struct ofputil_flow_stats_request fsr;
2808 struct ofputil_flow_stats fs;
2809 struct ofpbuf *request;
2810 struct ofpbuf ofpacts;
2811 struct ofpbuf *reply;
2814 fsr.aggregate = false;
2815 match_init_catchall(&fsr.match);
2816 fsr.out_port = OFPP_ANY;
2817 fsr.out_group = OFPG_ANY;
2818 fsr.table_id = 0xff;
2819 fsr.cookie = fsr.cookie_mask = htonll(0);
2820 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2821 send_xid = ((struct ofp_header *) request->data)->xid;
2822 send_openflow_buffer(vconn, request);
2825 ofpbuf_init(&ofpacts, 0);
2826 classifier_defer(cls);
2827 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2828 struct fte_version *version;
2830 version = xmalloc(sizeof *version);
2831 version->cookie = fs.cookie;
2832 version->idle_timeout = fs.idle_timeout;
2833 version->hard_timeout = fs.hard_timeout;
2834 version->importance = fs.importance;
2836 version->ofpacts_len = fs.ofpacts_len;
2837 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2839 fte_insert(cls, &fs.match, fs.priority, version, index);
2841 classifier_publish(cls);
2842 ofpbuf_uninit(&ofpacts);
2846 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2847 enum ofputil_protocol protocol, struct ovs_list *packets)
2849 const struct fte_version *version = fte->versions[index];
2850 struct ofputil_flow_mod fm;
2853 minimatch_expand(&fte->rule.match, &fm.match);
2854 fm.priority = fte->rule.priority;
2855 fm.cookie = htonll(0);
2856 fm.cookie_mask = htonll(0);
2857 fm.new_cookie = version->cookie;
2858 fm.modify_cookie = true;
2860 fm.command = command;
2861 fm.idle_timeout = version->idle_timeout;
2862 fm.hard_timeout = version->hard_timeout;
2863 fm.importance = version->importance;
2864 fm.buffer_id = UINT32_MAX;
2865 fm.out_port = OFPP_ANY;
2866 fm.out_group = OFPG_ANY;
2867 fm.flags = version->flags;
2868 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2869 command == OFPFC_MODIFY_STRICT) {
2870 fm.ofpacts = version->ofpacts;
2871 fm.ofpacts_len = version->ofpacts_len;
2876 fm.delete_reason = OFPRR_DELETE;
2878 ofm = ofputil_encode_flow_mod(&fm, protocol);
2879 list_push_back(packets, &ofm->list_node);
2883 ofctl_replace_flows(struct ovs_cmdl_context *ctx)
2885 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2886 enum ofputil_protocol usable_protocols, protocol;
2887 struct classifier cls;
2888 struct ovs_list requests;
2889 struct vconn *vconn;
2892 classifier_init(&cls, NULL);
2893 usable_protocols = read_flows_from_file(ctx->argv[2], &cls, FILE_IDX);
2895 protocol = open_vconn(ctx->argv[1], &vconn);
2896 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2898 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2900 list_init(&requests);
2902 /* Delete flows that exist on the switch but not in the file. */
2903 CLS_FOR_EACH (fte, rule, &cls) {
2904 struct fte_version *file_ver = fte->versions[FILE_IDX];
2905 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2907 if (sw_ver && !file_ver) {
2908 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2909 protocol, &requests);
2913 /* Add flows that exist in the file but not on the switch.
2914 * Update flows that exist in both places but differ. */
2915 CLS_FOR_EACH (fte, rule, &cls) {
2916 struct fte_version *file_ver = fte->versions[FILE_IDX];
2917 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2920 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2921 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2925 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
2927 transact_multiple_noreply(vconn, &requests);
2935 read_flows_from_source(const char *source, struct classifier *cls, int index)
2939 if (source[0] == '/' || source[0] == '.'
2940 || (!strchr(source, ':') && !stat(source, &s))) {
2941 read_flows_from_file(source, cls, index);
2943 enum ofputil_protocol protocol;
2944 struct vconn *vconn;
2946 protocol = open_vconn(source, &vconn);
2947 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2948 read_flows_from_switch(vconn, protocol, cls, index);
2954 ofctl_diff_flows(struct ovs_cmdl_context *ctx)
2956 bool differences = false;
2957 struct classifier cls;
2961 classifier_init(&cls, NULL);
2962 read_flows_from_source(ctx->argv[1], &cls, 0);
2963 read_flows_from_source(ctx->argv[2], &cls, 1);
2968 CLS_FOR_EACH (fte, rule, &cls) {
2969 struct fte_version *a = fte->versions[0];
2970 struct fte_version *b = fte->versions[1];
2972 if (!a || !b || !fte_version_equals(a, b)) {
2973 fte_version_format(fte, 0, &a_s);
2974 fte_version_format(fte, 1, &b_s);
2975 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2977 printf("-%s", ds_cstr(&a_s));
2980 printf("+%s", ds_cstr(&b_s));
2998 ofctl_meter_mod__(const char *bridge, const char *str, int command)
3000 struct ofputil_meter_mod mm;
3001 struct vconn *vconn;
3002 enum ofputil_protocol protocol;
3003 enum ofputil_protocol usable_protocols;
3004 enum ofp_version version;
3008 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
3010 ovs_fatal(0, "%s", error);
3013 usable_protocols = OFPUTIL_P_OF13_UP;
3014 mm.command = command;
3015 mm.meter.meter_id = OFPM13_ALL;
3018 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3019 version = ofputil_protocol_to_ofp_version(protocol);
3020 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
3025 ofctl_meter_request__(const char *bridge, const char *str,
3026 enum ofputil_meter_request_type type)
3028 struct ofputil_meter_mod mm;
3029 struct vconn *vconn;
3030 enum ofputil_protocol usable_protocols;
3031 enum ofputil_protocol protocol;
3032 enum ofp_version version;
3036 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
3038 ovs_fatal(0, "%s", error);
3041 usable_protocols = OFPUTIL_P_OF13_UP;
3042 mm.meter.meter_id = OFPM13_ALL;
3045 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3046 version = ofputil_protocol_to_ofp_version(protocol);
3047 dump_stats_transaction(vconn,
3048 ofputil_encode_meter_request(version, type,
3049 mm.meter.meter_id));
3055 ofctl_add_meter(struct ovs_cmdl_context *ctx)
3057 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD);
3061 ofctl_mod_meter(struct ovs_cmdl_context *ctx)
3063 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY);
3067 ofctl_del_meters(struct ovs_cmdl_context *ctx)
3069 ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE);
3073 ofctl_dump_meters(struct ovs_cmdl_context *ctx)
3075 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3076 OFPUTIL_METER_CONFIG);
3080 ofctl_meter_stats(struct ovs_cmdl_context *ctx)
3082 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3083 OFPUTIL_METER_STATS);
3087 ofctl_meter_features(struct ovs_cmdl_context *ctx)
3089 ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES);
3093 /* Undocumented commands for unit testing. */
3096 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
3097 enum ofputil_protocol usable_protocols)
3099 enum ofputil_protocol protocol = 0;
3103 usable_s = ofputil_protocols_to_string(usable_protocols);
3104 printf("usable protocols: %s\n", usable_s);
3107 if (!(usable_protocols & allowed_protocols)) {
3108 ovs_fatal(0, "no usable protocol");
3110 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
3112 if (protocol & usable_protocols & allowed_protocols) {
3116 ovs_assert(is_pow2(protocol));
3118 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
3120 for (i = 0; i < n_fms; i++) {
3121 struct ofputil_flow_mod *fm = &fms[i];
3124 msg = ofputil_encode_flow_mod(fm, protocol);
3125 ofp_print(stdout, msg->data, msg->size, verbosity);
3128 free(CONST_CAST(struct ofpact *, fm->ofpacts));
3132 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
3133 * it back to stdout. */
3135 ofctl_parse_flow(struct ovs_cmdl_context *ctx)
3137 enum ofputil_protocol usable_protocols;
3138 struct ofputil_flow_mod fm;
3141 error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols);
3143 ovs_fatal(0, "%s", error);
3145 ofctl_parse_flows__(&fm, 1, usable_protocols);
3148 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
3149 * add-flows) and prints each of the flows back to stdout. */
3151 ofctl_parse_flows(struct ovs_cmdl_context *ctx)
3153 enum ofputil_protocol usable_protocols;
3154 struct ofputil_flow_mod *fms = NULL;
3158 error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms,
3161 ovs_fatal(0, "%s", error);
3163 ofctl_parse_flows__(fms, n_fms, usable_protocols);
3168 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
3173 while (!ds_get_test_line(&in, stdin)) {
3174 struct ofpbuf nx_match;
3176 ovs_be64 cookie, cookie_mask;
3180 /* Convert string to nx_match. */
3181 ofpbuf_init(&nx_match, 0);
3183 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
3185 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
3188 /* Convert nx_match to match. */
3191 error = oxm_pull_match(&nx_match, &match);
3193 error = nx_pull_match(&nx_match, match_len, &match,
3194 &cookie, &cookie_mask);
3198 error = oxm_pull_match_loose(&nx_match, &match);
3200 error = nx_pull_match_loose(&nx_match, match_len, &match,
3201 &cookie, &cookie_mask);
3209 /* Convert match back to nx_match. */
3210 ofpbuf_uninit(&nx_match);
3211 ofpbuf_init(&nx_match, 0);
3213 match_len = oxm_put_match(&nx_match, &match, version);
3214 out = oxm_match_to_string(&nx_match, match_len);
3216 match_len = nx_put_match(&nx_match, &match,
3217 cookie, cookie_mask);
3218 out = nx_match_to_string(nx_match.data, match_len);
3224 if (verbosity > 0) {
3225 ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false);
3228 printf("nx_pull_match() returned error %s\n",
3229 ofperr_get_name(error));
3232 ofpbuf_uninit(&nx_match);
3237 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
3238 * stdin, does some internal fussing with them, and then prints them back as
3239 * strings on stdout. */
3241 ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED)
3243 ofctl_parse_nxm__(false, 0);
3246 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
3247 * strings from stdin, does some internal fussing with them, and then prints
3248 * them back as strings on stdout. VERSION must specify an OpenFlow version,
3249 * e.g. "OpenFlow12". */
3251 ofctl_parse_oxm(struct ovs_cmdl_context *ctx)
3253 enum ofp_version version = ofputil_version_from_string(ctx->argv[1]);
3254 if (version < OFP12_VERSION) {
3255 ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]);
3258 ofctl_parse_nxm__(true, version);
3262 print_differences(const char *prefix,
3263 const void *a_, size_t a_len,
3264 const void *b_, size_t b_len)
3266 const uint8_t *a = a_;
3267 const uint8_t *b = b_;
3270 for (i = 0; i < MIN(a_len, b_len); i++) {
3272 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
3273 prefix, i, a[i], b[i]);
3276 for (i = a_len; i < b_len; i++) {
3277 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
3279 for (i = b_len; i < a_len; i++) {
3280 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
3285 ofctl_parse_actions__(const char *version_s, bool instructions)
3287 enum ofp_version version;
3290 version = ofputil_version_from_string(version_s);
3292 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3296 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3297 struct ofpbuf of_out;
3298 struct ofpbuf of_in;
3299 struct ofpbuf ofpacts;
3300 const char *table_id;
3306 /* Parse table_id separated with the follow-up actions by ",", if
3308 actions = ds_cstr(&in);
3310 if (strstr(actions, ",")) {
3311 table_id = strsep(&actions, ",");
3314 /* Parse hex bytes. */
3315 ofpbuf_init(&of_in, 0);
3316 if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
3317 ovs_fatal(0, "Trailing garbage in hex data");
3320 /* Convert to ofpacts. */
3321 ofpbuf_init(&ofpacts, 0);
3323 error = (instructions
3324 ? ofpacts_pull_openflow_instructions
3325 : ofpacts_pull_openflow_actions)(
3326 &of_in, of_in.size, version, &ofpacts);
3327 if (!error && instructions) {
3328 /* Verify actions, enforce consistency. */
3329 enum ofputil_protocol protocol;
3332 memset(&flow, 0, sizeof flow);
3333 protocol = ofputil_protocols_from_ofp_version(version);
3334 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3336 table_id ? atoi(table_id) : 0,
3340 printf("bad %s %s: %s\n\n",
3341 version_s, instructions ? "instructions" : "actions",
3342 ofperr_get_name(error));
3343 ofpbuf_uninit(&ofpacts);
3344 ofpbuf_uninit(&of_in);
3347 ofpbuf_push_uninit(&of_in, size);
3349 /* Print cls_rule. */
3351 ds_put_cstr(&s, "actions=");
3352 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3356 /* Convert back to ofp10 actions and print differences from input. */
3357 ofpbuf_init(&of_out, 0);
3359 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3362 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size,
3366 print_differences("", of_in.data, of_in.size,
3367 of_out.data, of_out.size);
3370 ofpbuf_uninit(&ofpacts);
3371 ofpbuf_uninit(&of_in);
3372 ofpbuf_uninit(&of_out);
3377 /* "parse-actions VERSION": reads a series of action specifications for the
3378 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3379 * prints them as strings on stdout, and then converts them back to hex bytes
3380 * and prints any differences from the input. */
3382 ofctl_parse_actions(struct ovs_cmdl_context *ctx)
3384 ofctl_parse_actions__(ctx->argv[1], false);
3387 /* "parse-actions VERSION": reads a series of instruction specifications for
3388 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3389 * ofpacts, prints them as strings on stdout, and then converts them back to
3390 * hex bytes and prints any differences from the input. */
3392 ofctl_parse_instructions(struct ovs_cmdl_context *ctx)
3394 ofctl_parse_actions__(ctx->argv[1], true);
3397 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3398 * bytes from stdin, converts them to cls_rules, prints them as strings on
3399 * stdout, and then converts them back to hex bytes and prints any differences
3402 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3403 * values are ignored in the input and will be set to zero when OVS converts
3404 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3405 * it does the conversion to hex, to ensure that in fact they are ignored. */
3407 ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3414 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3415 struct ofpbuf match_in, match_expout;
3416 struct ofp10_match match_out;
3417 struct ofp10_match match_normal;
3421 /* Parse hex bytes to use for expected output. */
3423 ds_put_cstr(&expout, ds_cstr(&in));
3424 for (p = ds_cstr(&expout); *p; p++) {
3429 ofpbuf_init(&match_expout, 0);
3430 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3431 ovs_fatal(0, "Trailing garbage in hex data");
3433 if (match_expout.size != sizeof(struct ofp10_match)) {
3434 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3435 match_expout.size, sizeof(struct ofp10_match));
3438 /* Parse hex bytes for input. */
3439 for (p = ds_cstr(&in); *p; p++) {
3441 *p = "0123456789abcdef"[random_uint32() & 0xf];
3444 ofpbuf_init(&match_in, 0);
3445 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3446 ovs_fatal(0, "Trailing garbage in hex data");
3448 if (match_in.size != sizeof(struct ofp10_match)) {
3449 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3450 match_in.size, sizeof(struct ofp10_match));
3453 /* Convert to cls_rule and print. */
3454 ofputil_match_from_ofp10_match(match_in.data, &match);
3455 match_print(&match);
3457 /* Convert back to ofp10_match and print differences from input. */
3458 ofputil_match_to_ofp10_match(&match, &match_out);
3459 print_differences("", match_expout.data, match_expout.size,
3460 &match_out, sizeof match_out);
3462 /* Normalize, then convert and compare again. */
3463 ofputil_normalize_match(&match);
3464 ofputil_match_to_ofp10_match(&match, &match_normal);
3465 print_differences("normal: ", &match_out, sizeof match_out,
3466 &match_normal, sizeof match_normal);
3469 ofpbuf_uninit(&match_in);
3470 ofpbuf_uninit(&match_expout);
3473 ds_destroy(&expout);
3476 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3477 * bytes from stdin, converts them to "struct match"es, prints them as strings
3478 * on stdout, and then converts them back to hex bytes and prints any
3479 * differences from the input. */
3481 ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3486 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3487 struct ofpbuf match_in;
3488 struct ofp11_match match_out;
3492 /* Parse hex bytes. */
3493 ofpbuf_init(&match_in, 0);
3494 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3495 ovs_fatal(0, "Trailing garbage in hex data");
3497 if (match_in.size != sizeof(struct ofp11_match)) {
3498 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3499 match_in.size, sizeof(struct ofp11_match));
3502 /* Convert to match. */
3503 error = ofputil_match_from_ofp11_match(match_in.data, &match);
3505 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3506 ofpbuf_uninit(&match_in);
3511 match_print(&match);
3513 /* Convert back to ofp11_match and print differences from input. */
3514 ofputil_match_to_ofp11_match(&match, &match_out);
3516 print_differences("", match_in.data, match_in.size,
3517 &match_out, sizeof match_out);
3520 ofpbuf_uninit(&match_in);
3525 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3527 ofctl_parse_pcap(struct ovs_cmdl_context *ctx)
3531 pcap = ovs_pcap_open(ctx->argv[1], "rb");
3533 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
3537 struct dp_packet *packet;
3541 error = ovs_pcap_read(pcap, &packet, NULL);
3545 ovs_fatal(error, "%s: read failed", ctx->argv[1]);
3548 pkt_metadata_init(&packet->md, ODPP_NONE);
3549 flow_extract(packet, &flow);
3550 flow_print(stdout, &flow);
3552 dp_packet_delete(packet);
3556 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3557 * mask values to and from various formats and prints the results. */
3559 ofctl_check_vlan(struct ovs_cmdl_context *ctx)
3564 struct ofputil_flow_mod fm;
3567 struct match nxm_match;
3571 struct ofp10_match of10_raw;
3572 struct match of10_match;
3574 struct ofp11_match of11_raw;
3575 struct match of11_match;
3580 enum ofputil_protocol usable_protocols; /* Unused for now. */
3582 match_init_catchall(&match);
3583 match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16));
3584 match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16));
3586 /* Convert to and from string. */
3587 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3588 printf("%s -> ", string_s);
3590 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3592 ovs_fatal(0, "%s", error_s);
3594 printf("%04"PRIx16"/%04"PRIx16"\n",
3595 ntohs(fm.match.flow.vlan_tci),
3596 ntohs(fm.match.wc.masks.vlan_tci));
3599 /* Convert to and from NXM. */
3600 ofpbuf_init(&nxm, 0);
3601 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3602 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3603 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3604 printf("NXM: %s -> ", nxm_s);
3606 printf("%s\n", ofperr_to_string(error));
3608 printf("%04"PRIx16"/%04"PRIx16"\n",
3609 ntohs(nxm_match.flow.vlan_tci),
3610 ntohs(nxm_match.wc.masks.vlan_tci));
3613 ofpbuf_uninit(&nxm);
3615 /* Convert to and from OXM. */
3616 ofpbuf_init(&nxm, 0);
3617 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
3618 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3619 error = oxm_pull_match(&nxm, &nxm_match);
3620 printf("OXM: %s -> ", nxm_s);
3622 printf("%s\n", ofperr_to_string(error));
3624 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3625 (VLAN_VID_MASK | VLAN_CFI);
3626 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3627 (VLAN_VID_MASK | VLAN_CFI);
3629 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3630 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3631 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3637 ofpbuf_uninit(&nxm);
3639 /* Convert to and from OpenFlow 1.0. */
3640 ofputil_match_to_ofp10_match(&match, &of10_raw);
3641 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3642 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3643 ntohs(of10_raw.dl_vlan),
3644 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3645 of10_raw.dl_vlan_pcp,
3646 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3647 ntohs(of10_match.flow.vlan_tci),
3648 ntohs(of10_match.wc.masks.vlan_tci));
3650 /* Convert to and from OpenFlow 1.1. */
3651 ofputil_match_to_ofp11_match(&match, &of11_raw);
3652 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3653 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3654 ntohs(of11_raw.dl_vlan),
3655 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3656 of11_raw.dl_vlan_pcp,
3657 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3658 ntohs(of11_match.flow.vlan_tci),
3659 ntohs(of11_match.wc.masks.vlan_tci));
3662 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3665 ofctl_print_error(struct ovs_cmdl_context *ctx)
3670 error = ofperr_from_name(ctx->argv[1]);
3672 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3675 for (version = 0; version <= UINT8_MAX; version++) {
3676 const char *name = ofperr_domain_get_name(version);
3678 int vendor = ofperr_get_vendor(error, version);
3679 int type = ofperr_get_type(error, version);
3680 int code = ofperr_get_code(error, version);
3682 if (vendor != -1 || type != -1 || code != -1) {
3683 printf("%s: vendor %#x, type %d, code %d\n",
3684 name, vendor, type, code);
3690 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3691 * error named ENUM and prints the error reply in hex. */
3693 ofctl_encode_error_reply(struct ovs_cmdl_context *ctx)
3695 const struct ofp_header *oh;
3696 struct ofpbuf request, *reply;
3699 error = ofperr_from_name(ctx->argv[1]);
3701 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3704 ofpbuf_init(&request, 0);
3705 if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') {
3706 ovs_fatal(0, "Trailing garbage in hex data");
3708 if (request.size < sizeof(struct ofp_header)) {
3709 ovs_fatal(0, "Request too short");
3713 if (request.size != ntohs(oh->length)) {
3714 ovs_fatal(0, "Request size inconsistent");
3717 reply = ofperr_encode_reply(error, request.data);
3718 ofpbuf_uninit(&request);
3720 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3721 ofpbuf_delete(reply);
3724 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3725 * binary data, interpreting them as an OpenFlow message, and prints the
3726 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3728 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3729 * HEXSTRING_FILE contains the HEXSTRING. */
3731 ofctl_ofp_print(struct ovs_cmdl_context *ctx)
3733 struct ofpbuf packet;
3740 if (!strcmp(ctx->argv[ctx->argc-1], "-")) {
3741 if (ds_get_line(&line, stdin)) {
3742 VLOG_FATAL("Failed to read stdin");
3745 buffer = line.string;
3746 verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity;
3747 } else if (ctx->argc > 2) {
3748 buffer = ctx->argv[1];
3749 verbosity = atoi(ctx->argv[2]);
3751 buffer = ctx->argv[1];
3754 ofpbuf_init(&packet, strlen(buffer) / 2);
3755 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
3756 ovs_fatal(0, "trailing garbage following hex bytes");
3758 ofp_print(stdout, packet.data, packet.size, verbosity);
3759 ofpbuf_uninit(&packet);
3763 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3764 * and dumps each message in hex. */
3766 ofctl_encode_hello(struct ovs_cmdl_context *ctx)
3768 uint32_t bitmap = strtol(ctx->argv[1], NULL, 0);
3769 struct ofpbuf *hello;
3771 hello = ofputil_encode_hello(bitmap);
3772 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3773 ofp_print(stdout, hello->data, hello->size, verbosity);
3774 ofpbuf_delete(hello);
3777 static const struct ovs_cmdl_command all_commands[] = {
3780 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3781 1, 3, ofctl_monitor },
3782 { "snoop", "switch",
3783 1, 1, ofctl_snoop },
3784 { "dump-desc", "switch",
3785 1, 1, ofctl_dump_desc },
3786 { "dump-tables", "switch",
3787 1, 1, ofctl_dump_tables },
3788 { "dump-table-features", "switch",
3789 1, 1, ofctl_dump_table_features },
3790 { "dump-table-desc", "switch",
3791 1, 1, ofctl_dump_table_desc },
3792 { "dump-flows", "switch",
3793 1, 2, ofctl_dump_flows },
3794 { "dump-aggregate", "switch",
3795 1, 2, ofctl_dump_aggregate },
3796 { "queue-stats", "switch [port [queue]]",
3797 1, 3, ofctl_queue_stats },
3798 { "queue-get-config", "switch port",
3799 2, 2, ofctl_queue_get_config },
3800 { "add-flow", "switch flow",
3801 2, 2, ofctl_add_flow },
3802 { "add-flows", "switch file",
3803 2, 2, ofctl_add_flows },
3804 { "mod-flows", "switch flow",
3805 2, 2, ofctl_mod_flows },
3806 { "del-flows", "switch [flow]",
3807 1, 2, ofctl_del_flows },
3808 { "replace-flows", "switch file",
3809 2, 2, ofctl_replace_flows },
3810 { "diff-flows", "source1 source2",
3811 2, 2, ofctl_diff_flows },
3812 { "add-meter", "switch meter",
3813 2, 2, ofctl_add_meter },
3814 { "mod-meter", "switch meter",
3815 2, 2, ofctl_mod_meter },
3816 { "del-meter", "switch meter",
3817 2, 2, ofctl_del_meters },
3818 { "del-meters", "switch",
3819 1, 1, ofctl_del_meters },
3820 { "dump-meter", "switch meter",
3821 2, 2, ofctl_dump_meters },
3822 { "dump-meters", "switch",
3823 1, 1, ofctl_dump_meters },
3824 { "meter-stats", "switch [meter]",
3825 1, 2, ofctl_meter_stats },
3826 { "meter-features", "switch",
3827 1, 1, ofctl_meter_features },
3828 { "packet-out", "switch in_port actions packet...",
3829 4, INT_MAX, ofctl_packet_out },
3830 { "dump-ports", "switch [port]",
3831 1, 2, ofctl_dump_ports },
3832 { "dump-ports-desc", "switch [port]",
3833 1, 2, ofctl_dump_ports_desc },
3834 { "mod-port", "switch iface act",
3835 3, 3, ofctl_mod_port },
3836 { "mod-table", "switch mod",
3837 3, 3, ofctl_mod_table },
3838 { "get-frags", "switch",
3839 1, 1, ofctl_get_frags },
3840 { "set-frags", "switch frag_mode",
3841 2, 2, ofctl_set_frags },
3842 { "probe", "target",
3843 1, 1, ofctl_probe },
3844 { "ping", "target [n]",
3846 { "benchmark", "target n count",
3847 3, 3, ofctl_benchmark },
3849 { "ofp-parse", "file",
3850 1, 1, ofctl_ofp_parse },
3851 { "ofp-parse-pcap", "pcap",
3852 1, INT_MAX, ofctl_ofp_parse_pcap },
3854 { "add-group", "switch group",
3855 1, 2, ofctl_add_group },
3856 { "add-groups", "switch file",
3857 1, 2, ofctl_add_groups },
3858 { "mod-group", "switch group",
3859 1, 2, ofctl_mod_group },
3860 { "del-groups", "switch [group]",
3861 1, 2, ofctl_del_groups },
3862 { "insert-buckets", "switch [group]",
3863 1, 2, ofctl_insert_bucket },
3864 { "remove-buckets", "switch [group]",
3865 1, 2, ofctl_remove_bucket },
3866 { "dump-groups", "switch [group]",
3867 1, 2, ofctl_dump_group_desc },
3868 { "dump-group-stats", "switch [group]",
3869 1, 2, ofctl_dump_group_stats },
3870 { "dump-group-features", "switch",
3871 1, 1, ofctl_dump_group_features },
3872 { "add-geneve-map", "switch map",
3873 2, 2, ofctl_add_geneve_map },
3874 { "del-geneve-map", "switch [map]",
3875 1, 2, ofctl_del_geneve_map },
3876 { "dump-geneve-map", "switch",
3877 1, 1, ofctl_dump_geneve_map },
3878 { "help", NULL, 0, INT_MAX, ofctl_help },
3879 { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands },
3881 /* Undocumented commands for testing. */
3882 { "parse-flow", NULL, 1, 1, ofctl_parse_flow },
3883 { "parse-flows", NULL, 1, 1, ofctl_parse_flows },
3884 { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm },
3885 { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm },
3886 { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm },
3887 { "parse-actions", NULL, 1, 1, ofctl_parse_actions },
3888 { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions },
3889 { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match },
3890 { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match },
3891 { "parse-pcap", NULL, 1, 1, ofctl_parse_pcap },
3892 { "check-vlan", NULL, 2, 2, ofctl_check_vlan },
3893 { "print-error", NULL, 1, 1, ofctl_print_error },
3894 { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply },
3895 { "ofp-print", NULL, 1, 2, ofctl_ofp_print },
3896 { "encode-hello", NULL, 1, 1, ofctl_encode_hello },
3898 { NULL, NULL, 0, 0, NULL },
3901 static const struct ovs_cmdl_command *get_all_commands(void)
3903 return all_commands;