2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <sys/socket.h>
28 #include <sys/fcntl.h>
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
38 #include "dynamic-string.h"
41 #include "ofp-actions.h"
42 #include "ofp-errors.h"
44 #include "ofp-parse.h"
45 #include "ofp-print.h"
48 #include "ofproto/ofproto.h"
49 #include "openflow/nicira-ext.h"
50 #include "openflow/openflow.h"
52 #include "poll-loop.h"
54 #include "stream-ssl.h"
60 #include "meta-flow.h"
63 VLOG_DEFINE_THIS_MODULE(ofctl);
65 /* --strict: Use strict matching for flow mod commands? Additionally governs
66 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
70 /* --readd: If true, on replace-flows, re-add even flows that have not changed
71 * (to reset flow counters). */
74 /* -F, --flow-format: Allowed protocols. By default, any protocol is
76 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
78 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
79 * commands. Either one of NXPIF_* to force a particular packet_in format, or
80 * -1 to let ovs-ofctl choose the default. */
81 static int preferred_packet_in_format = -1;
83 /* -m, --more: Additional verbosity for ofp-print functions. */
86 /* --timestamp: Print a timestamp before each received packet on "monitor" and
88 static bool timestamp;
90 /* --sort, --rsort: Sort order. */
91 enum sort_order { SORT_ASC, SORT_DESC };
92 struct sort_criterion {
93 const struct mf_field *field; /* NULL means to sort by priority. */
94 enum sort_order order;
96 static struct sort_criterion *criteria;
97 static size_t n_criteria, allocated_criteria;
99 static const struct command all_commands[];
101 static void usage(void) NO_RETURN;
102 static void parse_options(int argc, char *argv[]);
104 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
105 struct ofpbuf **replyp,
106 struct ofputil_flow_stats *,
107 struct ofpbuf *ofpacts);
109 main(int argc, char *argv[])
111 set_program_name(argv[0]);
112 parse_options(argc, argv);
113 signal(SIGPIPE, SIG_IGN);
114 run_command(argc - optind, argv + optind, all_commands);
119 add_sort_criterion(enum sort_order order, const char *field)
121 struct sort_criterion *sc;
123 if (n_criteria >= allocated_criteria) {
124 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
127 sc = &criteria[n_criteria++];
128 if (!field || !strcasecmp(field, "priority")) {
131 sc->field = mf_from_name(field);
133 ovs_fatal(0, "%s: unknown field name", field);
140 parse_options(int argc, char *argv[])
143 OPT_STRICT = UCHAR_MAX + 1,
151 static struct option long_options[] = {
152 {"timeout", required_argument, NULL, 't'},
153 {"strict", no_argument, NULL, OPT_STRICT},
154 {"readd", no_argument, NULL, OPT_READD},
155 {"flow-format", required_argument, NULL, 'F'},
156 {"packet-in-format", required_argument, NULL, 'P'},
157 {"more", no_argument, NULL, 'm'},
158 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
159 {"sort", optional_argument, NULL, OPT_SORT},
160 {"rsort", optional_argument, NULL, OPT_RSORT},
161 {"help", no_argument, NULL, 'h'},
162 {"version", no_argument, NULL, 'V'},
165 STREAM_SSL_LONG_OPTIONS,
168 char *short_options = long_options_to_short_options(long_options);
171 unsigned long int timeout;
174 c = getopt_long(argc, argv, short_options, long_options, NULL);
181 timeout = strtoul(optarg, NULL, 10);
183 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
191 allowed_protocols = ofputil_protocols_from_string(optarg);
192 if (!allowed_protocols) {
193 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
198 preferred_packet_in_format =
199 ofputil_packet_in_format_from_string(optarg);
200 if (preferred_packet_in_format < 0) {
201 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
213 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
229 add_sort_criterion(SORT_ASC, optarg);
233 add_sort_criterion(SORT_DESC, optarg);
236 DAEMON_OPTION_HANDLERS
238 STREAM_SSL_OPTION_HANDLERS
249 /* Always do a final sort pass based on priority. */
250 add_sort_criterion(SORT_DESC, "priority");
259 printf("%s: OpenFlow switch management utility\n"
260 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
261 "\nFor OpenFlow switches:\n"
262 " show SWITCH show OpenFlow information\n"
263 " dump-desc SWITCH print switch description\n"
264 " dump-tables SWITCH print table stats\n"
265 " mod-port SWITCH IFACE ACT modify port behavior\n"
266 " get-frags SWITCH print fragment handling behavior\n"
267 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
268 " dump-ports SWITCH [PORT] print port statistics\n"
269 " dump-ports-desc SWITCH print port descriptions\n"
270 " dump-flows SWITCH print all flow entries\n"
271 " dump-flows SWITCH FLOW print matching FLOWs\n"
272 " dump-aggregate SWITCH print aggregate flow statistics\n"
273 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
274 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
275 " add-flow SWITCH FLOW add flow described by FLOW\n"
276 " add-flows SWITCH FILE add flows from FILE\n"
277 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
278 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
279 " replace-flows SWITCH FILE replace flows with those in FILE\n"
280 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
281 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
282 " execute ACTIONS on PACKET\n"
283 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
284 " print packets received from SWITCH\n"
285 " snoop SWITCH snoop on SWITCH and its controller\n"
286 "\nFor OpenFlow switches and controllers:\n"
287 " probe TARGET probe whether TARGET is up\n"
288 " ping TARGET [N] latency of N-byte echos\n"
289 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
290 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
291 program_name, program_name);
292 vconn_usage(true, false, false);
295 printf("\nOther options:\n"
296 " --strict use strict match for flow commands\n"
297 " --readd replace flows that haven't changed\n"
298 " -F, --flow-format=FORMAT force particular flow format\n"
299 " -P, --packet-in-format=FRMT force particular packet in format\n"
300 " -m, --more be more verbose printing OpenFlow\n"
301 " --timestamp (monitor, snoop) print timestamps\n"
302 " -t, --timeout=SECS give up after SECS seconds\n"
303 " --sort[=field] sort in ascending order\n"
304 " --rsort[=field] sort in descending order\n"
305 " -h, --help display this help message\n"
306 " -V, --version display version information\n");
311 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
312 const char *argv[] OVS_UNUSED, void *exiting_)
314 bool *exiting = exiting_;
316 unixctl_command_reply(conn, NULL);
319 static void run(int retval, const char *message, ...)
322 static void run(int retval, const char *message, ...)
327 va_start(args, message);
328 ovs_fatal_valist(retval, message, args);
332 /* Generic commands. */
335 open_vconn_socket(const char *name, struct vconn **vconnp)
337 char *vconn_name = xasprintf("unix:%s", name);
338 VLOG_DBG("connecting to %s", vconn_name);
339 run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp),
340 "connecting to %s", vconn_name);
344 static enum ofputil_protocol
345 open_vconn__(const char *name, const char *default_suffix,
346 struct vconn **vconnp)
348 char *datapath_name, *datapath_type, *socket_name;
349 enum ofputil_protocol protocol;
354 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
356 ofproto_parse_name(name, &datapath_name, &datapath_type);
357 socket_name = xasprintf("%s/%s.%s",
358 ovs_rundir(), datapath_name, default_suffix);
362 if (strchr(name, ':')) {
363 run(vconn_open_block(name, OFP10_VERSION, vconnp),
364 "connecting to %s", name);
365 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
366 open_vconn_socket(name, vconnp);
367 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
368 open_vconn_socket(bridge_path, vconnp);
369 } else if (!stat(socket_name, &s)) {
370 if (!S_ISSOCK(s.st_mode)) {
371 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
374 open_vconn_socket(socket_name, vconnp);
376 ovs_fatal(0, "%s is not a bridge or a socket", name);
382 ofp_version = vconn_get_version(*vconnp);
383 protocol = ofputil_protocol_from_ofp_version(ofp_version);
385 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
391 static enum ofputil_protocol
392 open_vconn(const char *name, struct vconn **vconnp)
394 return open_vconn__(name, "mgmt", vconnp);
398 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
400 ofpmsg_update_length(buffer);
401 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
405 dump_transaction(const char *vconn_name, struct ofpbuf *request)
408 struct ofpbuf *reply;
410 ofpmsg_update_length(request);
411 open_vconn(vconn_name, &vconn);
412 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
413 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
414 ofpbuf_delete(reply);
419 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
421 struct ofpbuf *request;
422 request = ofpraw_alloc(raw, OFP10_VERSION, 0);
423 dump_transaction(vconn_name, request);
427 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
429 const struct ofp_header *request_oh = request->data;
430 ovs_be32 send_xid = request_oh->xid;
431 enum ofpraw request_raw;
432 enum ofpraw reply_raw;
435 ofpraw_decode_partial(&request_raw, request->data, request->size);
436 reply_raw = ofpraw_stats_request_to_reply(request_raw,
437 request_oh->version);
439 send_openflow_buffer(vconn, request);
442 struct ofpbuf *reply;
444 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
445 recv_xid = ((struct ofp_header *) reply->data)->xid;
446 if (send_xid == recv_xid) {
449 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
451 ofpraw_decode(&raw, reply->data);
452 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
454 } else if (raw == reply_raw) {
455 done = !ofpmp_more(reply->data);
457 ovs_fatal(0, "received bad reply: %s",
458 ofp_to_string(reply->data, reply->size,
462 VLOG_DBG("received reply with xid %08"PRIx32" "
463 "!= expected %08"PRIx32, recv_xid, send_xid);
465 ofpbuf_delete(reply);
470 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
472 struct ofpbuf *request;
475 open_vconn(vconn_name, &vconn);
476 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
477 dump_stats_transaction(vconn, request);
481 /* Sends 'request', which should be a request that only has a reply if an error
482 * occurs, and waits for it to succeed or fail. If an error does occur, prints
483 * it and exits with an error.
485 * Destroys all of the 'requests'. */
487 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
489 struct ofpbuf *request, *reply;
491 LIST_FOR_EACH (request, list_node, requests) {
492 ofpmsg_update_length(request);
495 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
496 "talking to %s", vconn_get_name(vconn));
498 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
501 ofpbuf_delete(reply);
504 /* Sends 'request', which should be a request that only has a reply if an error
505 * occurs, and waits for it to succeed or fail. If an error does occur, prints
506 * it and exits with an error.
508 * Destroys 'request'. */
510 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
512 struct list requests;
514 list_init(&requests);
515 list_push_back(&requests, &request->list_node);
516 transact_multiple_noreply(vconn, &requests);
520 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
522 struct ofp_switch_config *config;
523 struct ofpbuf *request;
524 struct ofpbuf *reply;
527 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION, 0);
528 run(vconn_transact(vconn, request, &reply),
529 "talking to %s", vconn_get_name(vconn));
531 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
532 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
535 config = ofpbuf_pull(reply, sizeof *config);
538 ofpbuf_delete(reply);
542 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
544 struct ofpbuf *request;
546 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, OFP10_VERSION, 0);
547 ofpbuf_put(request, config, sizeof *config);
549 transact_noreply(vconn, request);
553 ofctl_show(int argc OVS_UNUSED, char *argv[])
555 const char *vconn_name = argv[1];
557 struct ofpbuf *request;
558 struct ofpbuf *reply;
561 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0);
562 open_vconn(vconn_name, &vconn);
563 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
565 trunc = ofputil_switch_features_ports_trunc(reply);
566 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
568 ofpbuf_delete(reply);
572 /* The Features Reply may not contain all the ports, so send a
573 * Port Description stats request, which doesn't have size
575 dump_trivial_stats_transaction(vconn_name,
576 OFPRAW_OFPST_PORT_DESC_REQUEST);
578 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
582 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
584 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
588 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
590 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
594 fetch_port_by_features(const char *vconn_name,
595 const char *port_name, unsigned int port_no,
596 struct ofputil_phy_port *pp, bool *trunc)
598 struct ofputil_switch_features features;
599 const struct ofp_header *oh;
600 struct ofpbuf *request, *reply;
607 /* Fetch the switch's ofp_switch_features. */
608 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0);
609 open_vconn(vconn_name, &vconn);
610 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
614 if (ofptype_decode(&type, reply->data)
615 || type != OFPTYPE_FEATURES_REPLY) {
616 ovs_fatal(0, "%s: received bad features reply", vconn_name);
620 if (ofputil_switch_features_ports_trunc(reply)) {
625 error = ofputil_decode_switch_features(oh, &features, &b);
627 ovs_fatal(0, "%s: failed to decode features reply (%s)",
628 vconn_name, ofperr_to_string(error));
631 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
632 if (port_no != UINT_MAX
633 ? port_no == pp->port_no
634 : !strcmp(pp->name, port_name)) {
641 ofpbuf_delete(reply);
646 fetch_port_by_stats(const char *vconn_name,
647 const char *port_name, unsigned int port_no,
648 struct ofputil_phy_port *pp)
650 struct ofpbuf *request;
656 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
657 send_xid = ((struct ofp_header *) request->data)->xid;
659 open_vconn(vconn_name, &vconn);
660 send_openflow_buffer(vconn, request);
663 struct ofpbuf *reply;
665 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
666 recv_xid = ((struct ofp_header *) reply->data)->xid;
667 if (send_xid == recv_xid) {
668 struct ofp_header *oh = reply->data;
673 ofpbuf_use_const(&b, oh, ntohs(oh->length));
674 if (ofptype_pull(&type, &b)
675 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
676 ovs_fatal(0, "received bad reply: %s",
677 ofp_to_string(reply->data, reply->size,
681 flags = ofpmp_flags(oh);
682 done = !(flags & OFPSF_REPLY_MORE);
685 /* We've already found the port, but we need to drain
686 * the queue of any other replies for this request. */
690 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
691 if (port_no != UINT_MAX ? port_no == pp->port_no
692 : !strcmp(pp->name, port_name)) {
698 VLOG_DBG("received reply with xid %08"PRIx32" "
699 "!= expected %08"PRIx32, recv_xid, send_xid);
701 ofpbuf_delete(reply);
709 /* Opens a connection to 'vconn_name', fetches the port structure for
710 * 'port_name' (which may be a port name or number), and copies it into
713 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
714 struct ofputil_phy_port *pp)
716 unsigned int port_no;
720 /* Try to interpret the argument as a port number. */
721 if (!str_to_uint(port_name, 10, &port_no)) {
725 /* Try to find the port based on the Features Reply. If it looks
726 * like the results may be truncated, then use the Port Description
727 * stats message introduced in OVS 1.7. */
728 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
731 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
735 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
739 /* Returns the port number corresponding to 'port_name' (which may be a port
740 * name or number) within the switch 'vconn_name'. */
742 str_to_port_no(const char *vconn_name, const char *port_name)
744 unsigned int port_no;
746 if (str_to_uint(port_name, 10, &port_no)) {
749 struct ofputil_phy_port pp;
751 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
757 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
758 enum ofputil_protocol *cur)
761 struct ofpbuf *request, *reply;
762 enum ofputil_protocol next;
764 request = ofputil_encode_set_protocol(*cur, want, &next);
769 run(vconn_transact_noreply(vconn, request, &reply),
770 "talking to %s", vconn_get_name(vconn));
772 char *s = ofp_to_string(reply->data, reply->size, 2);
773 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
774 vconn_get_name(vconn), s);
776 ofpbuf_delete(reply);
784 static enum ofputil_protocol
785 set_protocol_for_flow_dump(struct vconn *vconn,
786 enum ofputil_protocol cur_protocol,
787 enum ofputil_protocol usable_protocols)
792 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
793 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
794 if (f & usable_protocols & allowed_protocols
795 && try_set_protocol(vconn, f, &cur_protocol)) {
800 usable_s = ofputil_protocols_to_string(usable_protocols);
801 if (usable_protocols & allowed_protocols) {
802 ovs_fatal(0, "switch does not support any of the usable flow "
803 "formats (%s)", usable_s);
805 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
806 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
807 "allowed flow formats (%s)", usable_s, allowed_s);
811 static struct vconn *
812 prepare_dump_flows(int argc, char *argv[], bool aggregate,
813 struct ofpbuf **requestp)
815 enum ofputil_protocol usable_protocols, protocol;
816 struct ofputil_flow_stats_request fsr;
819 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
820 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
822 protocol = open_vconn(argv[1], &vconn);
823 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
824 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
829 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
831 struct ofpbuf *request;
834 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
835 dump_stats_transaction(vconn, request);
840 compare_flows(const void *afs_, const void *bfs_)
842 const struct ofputil_flow_stats *afs = afs_;
843 const struct ofputil_flow_stats *bfs = bfs_;
844 const struct cls_rule *a = &afs->rule;
845 const struct cls_rule *b = &bfs->rule;
846 const struct sort_criterion *sc;
848 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
849 const struct mf_field *f = sc->field;
853 ret = a->priority < b->priority ? -1 : a->priority > b->priority;
857 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
858 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
860 /* Skip the test for sc->order, so that missing fields always
861 * sort to the end whether we're sorting in ascending or
862 * descending order. */
865 union mf_value aval, bval;
867 mf_get_value(f, &a->flow, &aval);
868 mf_get_value(f, &b->flow, &bval);
869 ret = memcmp(&aval, &bval, f->n_bytes);
874 return sc->order == SORT_ASC ? ret : -ret;
882 ofctl_dump_flows(int argc, char *argv[])
885 return ofctl_dump_flows__(argc, argv, false);
887 struct ofputil_flow_stats *fses;
888 size_t n_fses, allocated_fses;
889 struct ofpbuf *request;
890 struct ofpbuf ofpacts;
891 struct ofpbuf *reply;
897 vconn = prepare_dump_flows(argc, argv, false, &request);
898 send_xid = ((struct ofp_header *) request->data)->xid;
899 send_openflow_buffer(vconn, request);
902 n_fses = allocated_fses = 0;
904 ofpbuf_init(&ofpacts, 0);
906 struct ofputil_flow_stats *fs;
908 if (n_fses >= allocated_fses) {
909 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
913 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
917 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
920 ofpbuf_uninit(&ofpacts);
922 qsort(fses, n_fses, sizeof *fses, compare_flows);
925 for (i = 0; i < n_fses; i++) {
927 ofp_print_flow_stats(&s, &fses[i]);
932 for (i = 0; i < n_fses; i++) {
933 free(fses[i].ofpacts);
942 ofctl_dump_aggregate(int argc, char *argv[])
944 return ofctl_dump_flows__(argc, argv, true);
948 ofctl_queue_stats(int argc, char *argv[])
950 struct ofp10_queue_stats_request *req;
951 struct ofpbuf *request;
954 open_vconn(argv[1], &vconn);
955 request = ofpraw_alloc(OFPRAW_OFPST_QUEUE_REQUEST,
956 vconn_get_version(vconn), 0);
957 req = ofpbuf_put_zeros(request, sizeof *req);
959 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
960 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
962 req->port_no = htons(OFPP_ALL);
964 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
965 req->queue_id = htonl(atoi(argv[3]));
967 req->queue_id = htonl(OFPQ_ALL);
970 memset(req->pad, 0, sizeof req->pad);
972 dump_stats_transaction(vconn, request);
976 static enum ofputil_protocol
977 open_vconn_for_flow_mod(const char *remote,
978 const struct ofputil_flow_mod *fms, size_t n_fms,
979 struct vconn **vconnp)
981 enum ofputil_protocol usable_protocols;
982 enum ofputil_protocol cur_protocol;
986 /* Figure out what flow formats will work. */
987 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
988 if (!(usable_protocols & allowed_protocols)) {
989 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
990 usable_s = ofputil_protocols_to_string(usable_protocols);
991 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
992 "allowed flow formats (%s)", usable_s, allowed_s);
995 /* If the initial flow format is allowed and usable, keep it. */
996 cur_protocol = open_vconn(remote, vconnp);
997 if (usable_protocols & allowed_protocols & cur_protocol) {
1001 /* Otherwise try each flow format in turn. */
1002 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1003 enum ofputil_protocol f = 1 << i;
1005 if (f != cur_protocol
1006 && f & usable_protocols & allowed_protocols
1007 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1012 usable_s = ofputil_protocols_to_string(usable_protocols);
1013 ovs_fatal(0, "switch does not support any of the usable flow "
1014 "formats (%s)", usable_s);
1018 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1021 enum ofputil_protocol protocol;
1022 struct vconn *vconn;
1025 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
1027 for (i = 0; i < n_fms; i++) {
1028 struct ofputil_flow_mod *fm = &fms[i];
1030 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1037 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1039 struct ofputil_flow_mod *fms = NULL;
1042 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
1043 ofctl_flow_mod__(argv[1], fms, n_fms);
1048 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1050 if (argc > 2 && !strcmp(argv[2], "-")) {
1051 ofctl_flow_mod_file(argc, argv, command);
1053 struct ofputil_flow_mod fm;
1054 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
1055 ofctl_flow_mod__(argv[1], &fm, 1);
1060 ofctl_add_flow(int argc, char *argv[])
1062 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1066 ofctl_add_flows(int argc, char *argv[])
1068 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1072 ofctl_mod_flows(int argc, char *argv[])
1074 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1078 ofctl_del_flows(int argc, char *argv[])
1080 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1084 set_packet_in_format(struct vconn *vconn,
1085 enum nx_packet_in_format packet_in_format)
1087 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
1088 transact_noreply(vconn, spif);
1089 VLOG_DBG("%s: using user-specified packet in format %s",
1090 vconn_get_name(vconn),
1091 ofputil_packet_in_format_to_string(packet_in_format));
1095 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1097 struct ofp_switch_config config;
1098 enum ofp_config_flags flags;
1100 fetch_switch_config(vconn, &config);
1101 flags = ntohs(config.flags);
1102 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1103 /* Set the invalid ttl config. */
1104 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1106 config.flags = htons(flags);
1107 set_switch_config(vconn, &config);
1109 /* Then retrieve the configuration to see if it really took. OpenFlow
1110 * doesn't define error reporting for bad modes, so this is all we can
1112 fetch_switch_config(vconn, &config);
1113 flags = ntohs(config.flags);
1114 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1115 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1116 "switch probably doesn't support mode)");
1123 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1124 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1125 * an error message and stores NULL in '*msgp'. */
1127 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1129 struct ofp_header *oh;
1132 msg = ofpbuf_new(strlen(hex) / 2);
1135 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1137 return "Trailing garbage in hex data";
1140 if (msg->size < sizeof(struct ofp_header)) {
1142 return "Message too short for OpenFlow";
1146 if (msg->size != ntohs(oh->length)) {
1148 return "Message size does not match length in OpenFlow header";
1156 ofctl_send(struct unixctl_conn *conn, int argc,
1157 const char *argv[], void *vconn_)
1159 struct vconn *vconn = vconn_;
1166 for (i = 1; i < argc; i++) {
1167 const char *error_msg;
1171 error_msg = openflow_from_hex(argv[i], &msg);
1173 ds_put_format(&reply, "%s\n", error_msg);
1178 fprintf(stderr, "send: ");
1179 ofp_print(stderr, msg->data, msg->size, verbosity);
1181 error = vconn_send_block(vconn, msg);
1184 ds_put_format(&reply, "%s\n", strerror(error));
1187 ds_put_cstr(&reply, "sent\n");
1192 unixctl_command_reply(conn, ds_cstr(&reply));
1194 unixctl_command_reply_error(conn, ds_cstr(&reply));
1199 struct barrier_aux {
1200 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1201 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1205 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1206 const char *argv[] OVS_UNUSED, void *aux_)
1208 struct barrier_aux *aux = aux_;
1213 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1217 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1218 error = vconn_send_block(aux->vconn, msg);
1221 unixctl_command_reply_error(conn, strerror(error));
1228 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1229 const char *argv[], void *aux OVS_UNUSED)
1233 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1235 unixctl_command_reply_error(conn, strerror(errno));
1240 dup2(fd, STDERR_FILENO);
1242 unixctl_command_reply(conn, NULL);
1246 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1247 const char *argv[] OVS_UNUSED, void *blocked_)
1249 bool *blocked = blocked_;
1253 unixctl_command_reply(conn, NULL);
1255 unixctl_command_reply(conn, "already blocking");
1260 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1261 const char *argv[] OVS_UNUSED, void *blocked_)
1263 bool *blocked = blocked_;
1267 unixctl_command_reply(conn, NULL);
1269 unixctl_command_reply(conn, "already unblocked");
1274 monitor_vconn(struct vconn *vconn)
1276 struct barrier_aux barrier_aux = { vconn, NULL };
1277 struct unixctl_server *server;
1278 bool exiting = false;
1279 bool blocked = false;
1282 daemon_save_fd(STDERR_FILENO);
1284 error = unixctl_server_create(NULL, &server);
1286 ovs_fatal(error, "failed to create unixctl server");
1288 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1289 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1291 unixctl_command_register("ofctl/barrier", "", 0, 0,
1292 ofctl_barrier, &barrier_aux);
1293 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1294 ofctl_set_output_file, NULL);
1296 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1297 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1300 daemonize_complete();
1306 unixctl_server_run(server);
1311 retval = vconn_recv(vconn, &b);
1312 if (retval == EAGAIN) {
1315 run(retval, "vconn_recv");
1318 time_t now = time_wall();
1321 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1325 ofptype_decode(&type, b->data);
1326 ofp_print(stderr, b->data, b->size, verbosity + 2);
1329 if (barrier_aux.conn && type == OFPTYPE_BARRIER_REPLY) {
1330 unixctl_command_reply(barrier_aux.conn, NULL);
1331 barrier_aux.conn = NULL;
1340 vconn_run_wait(vconn);
1342 vconn_recv_wait(vconn);
1344 unixctl_server_wait(server);
1348 unixctl_server_destroy(server);
1352 ofctl_monitor(int argc, char *argv[])
1354 struct vconn *vconn;
1357 open_vconn(argv[1], &vconn);
1358 for (i = 2; i < argc; i++) {
1359 const char *arg = argv[i];
1361 if (isdigit((unsigned char) *arg)) {
1362 struct ofp_switch_config config;
1364 fetch_switch_config(vconn, &config);
1365 config.miss_send_len = htons(atoi(arg));
1366 set_switch_config(vconn, &config);
1367 } else if (!strcmp(arg, "invalid_ttl")) {
1368 monitor_set_invalid_ttl_to_controller(vconn);
1369 } else if (!strncmp(arg, "watch:", 6)) {
1370 struct ofputil_flow_monitor_request fmr;
1373 parse_flow_monitor_request(&fmr, arg + 6);
1375 msg = ofpbuf_new(0);
1376 ofputil_append_flow_monitor_request(&fmr, msg);
1377 dump_stats_transaction(vconn, msg);
1379 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1383 if (preferred_packet_in_format >= 0) {
1384 set_packet_in_format(vconn, preferred_packet_in_format);
1386 struct ofpbuf *spif, *reply;
1388 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1389 run(vconn_transact_noreply(vconn, spif, &reply),
1390 "talking to %s", vconn_get_name(vconn));
1392 char *s = ofp_to_string(reply->data, reply->size, 2);
1393 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1394 " replied: %s. Falling back to the switch default.",
1395 vconn_get_name(vconn), s);
1397 ofpbuf_delete(reply);
1401 monitor_vconn(vconn);
1405 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1407 struct vconn *vconn;
1409 open_vconn__(argv[1], "snoop", &vconn);
1410 monitor_vconn(vconn);
1414 ofctl_dump_ports(int argc, char *argv[])
1416 struct ofp10_port_stats_request *req;
1417 struct ofpbuf *request;
1418 struct vconn *vconn;
1421 open_vconn(argv[1], &vconn);
1422 request = ofpraw_alloc(OFPRAW_OFPST_PORT_REQUEST,
1423 vconn_get_version(vconn), 0);
1424 req = ofpbuf_put_zeros(request, sizeof *req);
1425 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1426 req->port_no = htons(port);
1427 dump_stats_transaction(vconn, request);
1432 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1434 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1438 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1440 struct ofpbuf *request;
1441 struct vconn *vconn;
1442 struct ofpbuf *reply;
1444 open_vconn(argv[1], &vconn);
1445 request = make_echo_request(vconn_get_version(vconn));
1446 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1447 if (reply->size != sizeof(struct ofp_header)) {
1448 ovs_fatal(0, "reply does not match request");
1450 ofpbuf_delete(reply);
1455 ofctl_packet_out(int argc, char *argv[])
1457 enum ofputil_protocol protocol;
1458 struct ofputil_packet_out po;
1459 struct ofpbuf ofpacts;
1460 struct vconn *vconn;
1463 ofpbuf_init(&ofpacts, 64);
1464 parse_ofpacts(argv[3], &ofpacts);
1466 po.buffer_id = UINT32_MAX;
1467 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1468 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1469 : str_to_port_no(argv[1], argv[2]));
1470 po.ofpacts = ofpacts.data;
1471 po.ofpacts_len = ofpacts.size;
1473 protocol = open_vconn(argv[1], &vconn);
1474 for (i = 4; i < argc; i++) {
1475 struct ofpbuf *packet, *opo;
1476 const char *error_msg;
1478 error_msg = eth_from_hex(argv[i], &packet);
1480 ovs_fatal(0, "%s", error_msg);
1483 po.packet = packet->data;
1484 po.packet_len = packet->size;
1485 opo = ofputil_encode_packet_out(&po, protocol);
1486 transact_noreply(vconn, opo);
1487 ofpbuf_delete(packet);
1490 ofpbuf_uninit(&ofpacts);
1494 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1496 struct ofp_config_flag {
1497 const char *name; /* The flag's name. */
1498 enum ofputil_port_config bit; /* Bit to turn on or off. */
1499 bool on; /* Value to set the bit to. */
1501 static const struct ofp_config_flag flags[] = {
1502 { "up", OFPUTIL_PC_PORT_DOWN, false },
1503 { "down", OFPUTIL_PC_PORT_DOWN, true },
1504 { "stp", OFPUTIL_PC_NO_STP, false },
1505 { "receive", OFPUTIL_PC_NO_RECV, false },
1506 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1507 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1508 { "forward", OFPUTIL_PC_NO_FWD, false },
1509 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1512 const struct ofp_config_flag *flag;
1513 enum ofputil_protocol protocol;
1514 struct ofputil_port_mod pm;
1515 struct ofputil_phy_port pp;
1516 struct vconn *vconn;
1517 const char *command;
1520 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1522 pm.port_no = pp.port_no;
1523 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1528 if (!strncasecmp(argv[3], "no-", 3)) {
1529 command = argv[3] + 3;
1531 } else if (!strncasecmp(argv[3], "no", 2)) {
1532 command = argv[3] + 2;
1538 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1539 if (!strcasecmp(command, flag->name)) {
1540 pm.mask = flag->bit;
1541 pm.config = flag->on ^ not ? flag->bit : 0;
1545 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1548 protocol = open_vconn(argv[1], &vconn);
1549 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1554 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1556 struct ofp_switch_config config;
1557 struct vconn *vconn;
1559 open_vconn(argv[1], &vconn);
1560 fetch_switch_config(vconn, &config);
1561 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1566 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1568 struct ofp_switch_config config;
1569 enum ofp_config_flags mode;
1570 struct vconn *vconn;
1573 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1574 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1577 open_vconn(argv[1], &vconn);
1578 fetch_switch_config(vconn, &config);
1579 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1580 if (flags != config.flags) {
1581 /* Set the configuration. */
1582 config.flags = flags;
1583 set_switch_config(vconn, &config);
1585 /* Then retrieve the configuration to see if it really took. OpenFlow
1586 * doesn't define error reporting for bad modes, so this is all we can
1588 fetch_switch_config(vconn, &config);
1589 if (flags != config.flags) {
1590 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1591 "switch probably doesn't support mode \"%s\")",
1592 argv[1], ofputil_frag_handling_to_string(mode));
1599 ofctl_ping(int argc, char *argv[])
1601 size_t max_payload = 65535 - sizeof(struct ofp_header);
1602 unsigned int payload;
1603 struct vconn *vconn;
1606 payload = argc > 2 ? atoi(argv[2]) : 64;
1607 if (payload > max_payload) {
1608 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1611 open_vconn(argv[1], &vconn);
1612 for (i = 0; i < 10; i++) {
1613 struct timeval start, end;
1614 struct ofpbuf *request, *reply;
1615 const struct ofp_header *rpy_hdr;
1618 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
1620 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1622 xgettimeofday(&start);
1623 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1624 xgettimeofday(&end);
1626 rpy_hdr = reply->data;
1627 if (ofptype_pull(&type, reply)
1628 || type != OFPTYPE_ECHO_REPLY
1629 || reply->size != payload
1630 || memcmp(request->l3, reply->l3, payload)) {
1631 printf("Reply does not match request. Request:\n");
1632 ofp_print(stdout, request, request->size, verbosity + 2);
1634 ofp_print(stdout, reply, reply->size, verbosity + 2);
1636 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1637 reply->size, argv[1], ntohl(rpy_hdr->xid),
1638 (1000*(double)(end.tv_sec - start.tv_sec))
1639 + (.001*(end.tv_usec - start.tv_usec)));
1640 ofpbuf_delete(request);
1641 ofpbuf_delete(reply);
1647 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1649 size_t max_payload = 65535 - sizeof(struct ofp_header);
1650 struct timeval start, end;
1651 unsigned int payload_size, message_size;
1652 struct vconn *vconn;
1657 payload_size = atoi(argv[2]);
1658 if (payload_size > max_payload) {
1659 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1661 message_size = sizeof(struct ofp_header) + payload_size;
1663 count = atoi(argv[3]);
1665 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1666 count, message_size, count * message_size);
1668 open_vconn(argv[1], &vconn);
1669 xgettimeofday(&start);
1670 for (i = 0; i < count; i++) {
1671 struct ofpbuf *request, *reply;
1673 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
1675 ofpbuf_put_zeros(request, payload_size);
1676 run(vconn_transact(vconn, request, &reply), "transact");
1677 ofpbuf_delete(reply);
1679 xgettimeofday(&end);
1682 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1683 + (.001*(end.tv_usec - start.tv_usec)));
1684 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1685 duration, count / (duration / 1000.0),
1686 count * message_size / (duration / 1000.0));
1690 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1695 /* replace-flows and diff-flows commands. */
1697 /* A flow table entry, possibly with two different versions. */
1699 struct cls_rule rule; /* Within a "struct classifier". */
1700 struct fte_version *versions[2];
1703 /* One version of a Flow Table Entry. */
1704 struct fte_version {
1706 uint16_t idle_timeout;
1707 uint16_t hard_timeout;
1709 struct ofpact *ofpacts;
1713 /* Frees 'version' and the data that it owns. */
1715 fte_version_free(struct fte_version *version)
1718 free(version->ofpacts);
1723 /* Returns true if 'a' and 'b' are the same, false if they differ.
1725 * Ignores differences in 'flags' because there's no way to retrieve flags from
1726 * an OpenFlow switch. We have to assume that they are the same. */
1728 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1730 return (a->cookie == b->cookie
1731 && a->idle_timeout == b->idle_timeout
1732 && a->hard_timeout == b->hard_timeout
1733 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1734 b->ofpacts, b->ofpacts_len));
1737 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
1738 * 'index' into 's', followed by a new-line. */
1740 fte_version_format(const struct fte *fte, int index, struct ds *s)
1742 const struct fte_version *version = fte->versions[index];
1749 cls_rule_format(&fte->rule, s);
1750 if (version->cookie != htonll(0)) {
1751 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
1753 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1754 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
1756 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1757 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
1760 ds_put_char(s, ' ');
1761 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
1763 ds_put_char(s, '\n');
1767 fte_from_cls_rule(const struct cls_rule *cls_rule)
1769 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1772 /* Frees 'fte' and its versions. */
1774 fte_free(struct fte *fte)
1777 fte_version_free(fte->versions[0]);
1778 fte_version_free(fte->versions[1]);
1783 /* Frees all of the FTEs within 'cls'. */
1785 fte_free_all(struct classifier *cls)
1787 struct cls_cursor cursor;
1788 struct fte *fte, *next;
1790 cls_cursor_init(&cursor, cls, NULL);
1791 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1792 classifier_remove(cls, &fte->rule);
1795 classifier_destroy(cls);
1798 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1799 * necessary. Sets 'version' as the version of that rule with the given
1800 * 'index', replacing any existing version, if any.
1802 * Takes ownership of 'version'. */
1804 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1805 struct fte_version *version, int index)
1807 struct fte *old, *fte;
1809 fte = xzalloc(sizeof *fte);
1811 fte->versions[index] = version;
1813 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1815 fte_version_free(old->versions[index]);
1816 fte->versions[!index] = old->versions[!index];
1821 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1822 * with the specified 'index'. Returns the flow formats able to represent the
1823 * flows that were read. */
1824 static enum ofputil_protocol
1825 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1827 enum ofputil_protocol usable_protocols;
1831 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1833 ovs_fatal(errno, "%s: open", filename);
1837 usable_protocols = OFPUTIL_P_ANY;
1838 while (!ds_get_preprocessed_line(&s, file)) {
1839 struct fte_version *version;
1840 struct ofputil_flow_mod fm;
1842 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1844 version = xmalloc(sizeof *version);
1845 version->cookie = fm.new_cookie;
1846 version->idle_timeout = fm.idle_timeout;
1847 version->hard_timeout = fm.hard_timeout;
1848 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG);
1849 version->ofpacts = fm.ofpacts;
1850 version->ofpacts_len = fm.ofpacts_len;
1852 usable_protocols &= ofputil_usable_protocols(&fm.cr);
1854 fte_insert(cls, &fm.cr, version, index);
1858 if (file != stdin) {
1862 return usable_protocols;
1866 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
1867 struct ofpbuf **replyp,
1868 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
1870 struct ofpbuf *reply = *replyp;
1876 /* Get a flow stats reply message, if we don't already have one. */
1882 run(vconn_recv_block(vconn, &reply),
1883 "OpenFlow packet receive failed");
1884 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1886 error = ofptype_decode(&type, reply->data);
1887 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
1888 ovs_fatal(0, "received bad reply: %s",
1889 ofp_to_string(reply->data, reply->size,
1894 /* Pull an individual flow stats reply out of the message. */
1895 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1902 more = ofpmp_more(reply->l2);
1903 ofpbuf_delete(reply);
1912 ovs_fatal(0, "parse error in reply (%s)",
1913 ofperr_to_string(retval));
1918 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1919 * format 'protocol', and adds them as flow table entries in 'cls' for the
1920 * version with the specified 'index'. */
1922 read_flows_from_switch(struct vconn *vconn,
1923 enum ofputil_protocol protocol,
1924 struct classifier *cls, int index)
1926 struct ofputil_flow_stats_request fsr;
1927 struct ofputil_flow_stats fs;
1928 struct ofpbuf *request;
1929 struct ofpbuf ofpacts;
1930 struct ofpbuf *reply;
1933 fsr.aggregate = false;
1934 cls_rule_init_catchall(&fsr.match, 0);
1935 fsr.out_port = OFPP_NONE;
1936 fsr.table_id = 0xff;
1937 fsr.cookie = fsr.cookie_mask = htonll(0);
1938 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1939 send_xid = ((struct ofp_header *) request->data)->xid;
1940 send_openflow_buffer(vconn, request);
1943 ofpbuf_init(&ofpacts, 0);
1944 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
1945 struct fte_version *version;
1947 version = xmalloc(sizeof *version);
1948 version->cookie = fs.cookie;
1949 version->idle_timeout = fs.idle_timeout;
1950 version->hard_timeout = fs.hard_timeout;
1952 version->ofpacts_len = fs.ofpacts_len;
1953 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
1955 fte_insert(cls, &fs.rule, version, index);
1957 ofpbuf_uninit(&ofpacts);
1961 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1962 enum ofputil_protocol protocol, struct list *packets)
1964 const struct fte_version *version = fte->versions[index];
1965 struct ofputil_flow_mod fm;
1969 fm.cookie = htonll(0);
1970 fm.cookie_mask = htonll(0);
1971 fm.new_cookie = version->cookie;
1973 fm.command = command;
1974 fm.idle_timeout = version->idle_timeout;
1975 fm.hard_timeout = version->hard_timeout;
1976 fm.buffer_id = UINT32_MAX;
1977 fm.out_port = OFPP_NONE;
1978 fm.flags = version->flags;
1979 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1980 command == OFPFC_MODIFY_STRICT) {
1981 fm.ofpacts = version->ofpacts;
1982 fm.ofpacts_len = version->ofpacts_len;
1988 ofm = ofputil_encode_flow_mod(&fm, protocol);
1989 list_push_back(packets, &ofm->list_node);
1993 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
1995 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1996 enum ofputil_protocol usable_protocols, protocol;
1997 struct cls_cursor cursor;
1998 struct classifier cls;
1999 struct list requests;
2000 struct vconn *vconn;
2003 classifier_init(&cls);
2004 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2006 protocol = open_vconn(argv[1], &vconn);
2007 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2009 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2011 list_init(&requests);
2013 /* Delete flows that exist on the switch but not in the file. */
2014 cls_cursor_init(&cursor, &cls, NULL);
2015 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2016 struct fte_version *file_ver = fte->versions[FILE_IDX];
2017 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2019 if (sw_ver && !file_ver) {
2020 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2021 protocol, &requests);
2025 /* Add flows that exist in the file but not on the switch.
2026 * Update flows that exist in both places but differ. */
2027 cls_cursor_init(&cursor, &cls, NULL);
2028 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2029 struct fte_version *file_ver = fte->versions[FILE_IDX];
2030 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2033 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2034 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2037 transact_multiple_noreply(vconn, &requests);
2044 read_flows_from_source(const char *source, struct classifier *cls, int index)
2048 if (source[0] == '/' || source[0] == '.'
2049 || (!strchr(source, ':') && !stat(source, &s))) {
2050 read_flows_from_file(source, cls, index);
2052 enum ofputil_protocol protocol;
2053 struct vconn *vconn;
2055 protocol = open_vconn(source, &vconn);
2056 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2057 read_flows_from_switch(vconn, protocol, cls, index);
2063 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2065 bool differences = false;
2066 struct cls_cursor cursor;
2067 struct classifier cls;
2071 classifier_init(&cls);
2072 read_flows_from_source(argv[1], &cls, 0);
2073 read_flows_from_source(argv[2], &cls, 1);
2078 cls_cursor_init(&cursor, &cls, NULL);
2079 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2080 struct fte_version *a = fte->versions[0];
2081 struct fte_version *b = fte->versions[1];
2083 if (!a || !b || !fte_version_equals(a, b)) {
2084 fte_version_format(fte, 0, &a_s);
2085 fte_version_format(fte, 1, &b_s);
2086 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2088 printf("-%s", ds_cstr(&a_s));
2091 printf("+%s", ds_cstr(&b_s));
2108 /* Undocumented commands for unit testing. */
2111 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
2113 enum ofputil_protocol usable_protocols;
2114 enum ofputil_protocol protocol = 0;
2118 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
2119 usable_s = ofputil_protocols_to_string(usable_protocols);
2120 printf("usable protocols: %s\n", usable_s);
2123 if (!(usable_protocols & allowed_protocols)) {
2124 ovs_fatal(0, "no usable protocol");
2126 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2128 if (protocol & usable_protocols & allowed_protocols) {
2132 assert(IS_POW2(protocol));
2134 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2136 for (i = 0; i < n_fms; i++) {
2137 struct ofputil_flow_mod *fm = &fms[i];
2140 msg = ofputil_encode_flow_mod(fm, protocol);
2141 ofp_print(stdout, msg->data, msg->size, verbosity);
2148 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2149 * it back to stdout. */
2151 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2153 struct ofputil_flow_mod fm;
2155 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
2156 ofctl_parse_flows__(&fm, 1);
2159 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2160 * add-flows) and prints each of the flows back to stdout. */
2162 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2164 struct ofputil_flow_mod *fms = NULL;
2167 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
2168 ofctl_parse_flows__(fms, n_fms);
2173 ofctl_parse_nxm__(bool oxm)
2178 while (!ds_get_test_line(&in, stdin)) {
2179 struct ofpbuf nx_match;
2180 struct cls_rule rule;
2181 ovs_be64 cookie, cookie_mask;
2185 /* Convert string to nx_match. */
2186 ofpbuf_init(&nx_match, 0);
2188 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2190 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2193 /* Convert nx_match to cls_rule. */
2196 error = oxm_pull_match(&nx_match, 0, &rule);
2198 error = nx_pull_match(&nx_match, match_len, 0, &rule,
2199 &cookie, &cookie_mask);
2203 error = oxm_pull_match_loose(&nx_match, 0, &rule);
2205 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
2206 &cookie, &cookie_mask);
2214 /* Convert cls_rule back to nx_match. */
2215 ofpbuf_uninit(&nx_match);
2216 ofpbuf_init(&nx_match, 0);
2218 match_len = oxm_put_match(&nx_match, &rule);
2219 out = oxm_match_to_string(nx_match.data, match_len);
2221 match_len = nx_put_match(&nx_match, &rule,
2222 cookie, cookie_mask);
2223 out = nx_match_to_string(nx_match.data, match_len);
2229 printf("nx_pull_match() returned error %s\n",
2230 ofperr_get_name(error));
2233 ofpbuf_uninit(&nx_match);
2238 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2239 * stdin, does some internal fussing with them, and then prints them back as
2240 * strings on stdout. */
2242 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2244 return ofctl_parse_nxm__(false);
2247 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2248 * stdin, does some internal fussing with them, and then prints them back as
2249 * strings on stdout. */
2251 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2253 return ofctl_parse_nxm__(true);
2257 print_differences(const char *prefix,
2258 const void *a_, size_t a_len,
2259 const void *b_, size_t b_len)
2261 const uint8_t *a = a_;
2262 const uint8_t *b = b_;
2265 for (i = 0; i < MIN(a_len, b_len); i++) {
2267 printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
2268 prefix, i, a[i], b[i]);
2271 for (i = a_len; i < b_len; i++) {
2272 printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2274 for (i = b_len; i < a_len; i++) {
2275 printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2279 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2280 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2281 * on stdout, and then converts them back to hex bytes and prints any
2282 * differences from the input. */
2284 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2289 while (!ds_get_preprocessed_line(&in, stdin)) {
2290 struct ofpbuf of10_out;
2291 struct ofpbuf of10_in;
2292 struct ofpbuf ofpacts;
2297 /* Parse hex bytes. */
2298 ofpbuf_init(&of10_in, 0);
2299 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2300 ovs_fatal(0, "Trailing garbage in hex data");
2303 /* Convert to ofpacts. */
2304 ofpbuf_init(&ofpacts, 0);
2305 size = of10_in.size;
2306 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2308 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2309 ofpbuf_uninit(&ofpacts);
2310 ofpbuf_uninit(&of10_in);
2313 ofpbuf_push_uninit(&of10_in, size);
2315 /* Print cls_rule. */
2317 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2321 /* Convert back to ofp10 actions and print differences from input. */
2322 ofpbuf_init(&of10_out, 0);
2323 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2325 print_differences("", of10_in.data, of10_in.size,
2326 of10_out.data, of10_out.size);
2329 ofpbuf_uninit(&ofpacts);
2330 ofpbuf_uninit(&of10_in);
2331 ofpbuf_uninit(&of10_out);
2336 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2337 * bytes from stdin, converts them to cls_rules, prints them as strings on
2338 * stdout, and then converts them back to hex bytes and prints any differences
2341 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2342 * values are ignored in the input and will be set to zero when OVS converts
2343 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2344 * it does the conversion to hex, to ensure that in fact they are ignored. */
2346 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2353 while (!ds_get_preprocessed_line(&in, stdin)) {
2354 struct ofpbuf match_in, match_expout;
2355 struct ofp10_match match_out;
2356 struct ofp10_match match_normal;
2357 struct cls_rule rule;
2360 /* Parse hex bytes to use for expected output. */
2362 ds_put_cstr(&expout, ds_cstr(&in));
2363 for (p = ds_cstr(&expout); *p; p++) {
2368 ofpbuf_init(&match_expout, 0);
2369 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2370 ovs_fatal(0, "Trailing garbage in hex data");
2372 if (match_expout.size != sizeof(struct ofp10_match)) {
2373 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2374 match_expout.size, sizeof(struct ofp10_match));
2377 /* Parse hex bytes for input. */
2378 for (p = ds_cstr(&in); *p; p++) {
2380 *p = "0123456789abcdef"[random_uint32() & 0xf];
2383 ofpbuf_init(&match_in, 0);
2384 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2385 ovs_fatal(0, "Trailing garbage in hex data");
2387 if (match_in.size != sizeof(struct ofp10_match)) {
2388 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2389 match_in.size, sizeof(struct ofp10_match));
2392 /* Convert to cls_rule and print. */
2393 ofputil_cls_rule_from_ofp10_match(match_in.data, OFP_DEFAULT_PRIORITY,
2395 cls_rule_print(&rule);
2397 /* Convert back to ofp10_match and print differences from input. */
2398 ofputil_cls_rule_to_ofp10_match(&rule, &match_out);
2399 print_differences("", match_expout.data, match_expout.size,
2400 &match_out, sizeof match_out);
2402 /* Normalize, then convert and compare again. */
2403 ofputil_normalize_rule(&rule);
2404 ofputil_cls_rule_to_ofp10_match(&rule, &match_normal);
2405 print_differences("normal: ", &match_out, sizeof match_out,
2406 &match_normal, sizeof match_normal);
2409 ofpbuf_uninit(&match_in);
2410 ofpbuf_uninit(&match_expout);
2413 ds_destroy(&expout);
2416 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2417 * bytes from stdin, converts them to cls_rules, prints them as strings on
2418 * stdout, and then converts them back to hex bytes and prints any differences
2419 * from the input. */
2421 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2426 while (!ds_get_preprocessed_line(&in, stdin)) {
2427 struct ofpbuf match_in;
2428 struct ofp11_match match_out;
2429 struct cls_rule rule;
2432 /* Parse hex bytes. */
2433 ofpbuf_init(&match_in, 0);
2434 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2435 ovs_fatal(0, "Trailing garbage in hex data");
2437 if (match_in.size != sizeof(struct ofp11_match)) {
2438 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2439 match_in.size, sizeof(struct ofp11_match));
2442 /* Convert to cls_rule. */
2443 error = ofputil_cls_rule_from_ofp11_match(match_in.data,
2444 OFP_DEFAULT_PRIORITY, &rule);
2446 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2447 ofpbuf_uninit(&match_in);
2451 /* Print cls_rule. */
2452 cls_rule_print(&rule);
2454 /* Convert back to ofp11_match and print differences from input. */
2455 ofputil_cls_rule_to_ofp11_match(&rule, &match_out);
2457 print_differences("", match_in.data, match_in.size,
2458 &match_out, sizeof match_out);
2461 ofpbuf_uninit(&match_in);
2466 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2467 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2468 * on stdout, and then converts them back to hex bytes and prints any
2469 * differences from the input. */
2471 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2476 while (!ds_get_preprocessed_line(&in, stdin)) {
2477 struct ofpbuf of11_out;
2478 struct ofpbuf of11_in;
2479 struct ofpbuf ofpacts;
2484 /* Parse hex bytes. */
2485 ofpbuf_init(&of11_in, 0);
2486 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2487 ovs_fatal(0, "Trailing garbage in hex data");
2490 /* Convert to ofpacts. */
2491 ofpbuf_init(&ofpacts, 0);
2492 size = of11_in.size;
2493 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2496 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2497 ofpbuf_uninit(&ofpacts);
2498 ofpbuf_uninit(&of11_in);
2501 ofpbuf_push_uninit(&of11_in, size);
2503 /* Print cls_rule. */
2505 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2509 /* Convert back to ofp11 actions and print differences from input. */
2510 ofpbuf_init(&of11_out, 0);
2511 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2513 print_differences("", of11_in.data, of11_in.size,
2514 of11_out.data, of11_out.size);
2517 ofpbuf_uninit(&ofpacts);
2518 ofpbuf_uninit(&of11_in);
2519 ofpbuf_uninit(&of11_out);
2524 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2525 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2526 * them as strings on stdout, and then converts them back to hex bytes and
2527 * prints any differences from the input. */
2529 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2534 while (!ds_get_preprocessed_line(&in, stdin)) {
2535 struct ofpbuf of11_out;
2536 struct ofpbuf of11_in;
2537 struct ofpbuf ofpacts;
2542 /* Parse hex bytes. */
2543 ofpbuf_init(&of11_in, 0);
2544 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2545 ovs_fatal(0, "Trailing garbage in hex data");
2548 /* Convert to ofpacts. */
2549 ofpbuf_init(&ofpacts, 0);
2550 size = of11_in.size;
2551 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2554 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2555 ofpbuf_uninit(&ofpacts);
2556 ofpbuf_uninit(&of11_in);
2559 ofpbuf_push_uninit(&of11_in, size);
2561 /* Print cls_rule. */
2563 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2567 /* Convert back to ofp11 instructions and print differences from
2569 ofpbuf_init(&of11_out, 0);
2570 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2573 print_differences("", of11_in.data, of11_in.size,
2574 of11_out.data, of11_out.size);
2577 ofpbuf_uninit(&ofpacts);
2578 ofpbuf_uninit(&of11_in);
2579 ofpbuf_uninit(&of11_out);
2584 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
2585 * mask values to and from various formats and prints the results. */
2587 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
2589 struct cls_rule rule;
2592 struct ofputil_flow_mod fm;
2595 struct cls_rule nxm_rule;
2599 struct ofp10_match of10_match;
2600 struct cls_rule of10_rule;
2602 struct ofp11_match of11_match;
2603 struct cls_rule of11_rule;
2607 cls_rule_init_catchall(&rule, OFP_DEFAULT_PRIORITY);
2608 rule.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
2609 rule.wc.vlan_tci_mask = htons(strtoul(argv[2], NULL, 16));
2611 /* Convert to and from string. */
2612 string_s = cls_rule_to_string(&rule);
2613 printf("%s -> ", string_s);
2615 parse_ofp_str(&fm, -1, string_s, false);
2616 printf("%04"PRIx16"/%04"PRIx16"\n",
2617 ntohs(fm.cr.flow.vlan_tci),
2618 ntohs(fm.cr.wc.vlan_tci_mask));
2621 /* Convert to and from NXM. */
2622 ofpbuf_init(&nxm, 0);
2623 nxm_match_len = nx_put_match(&nxm, &rule, htonll(0), htonll(0));
2624 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
2625 error = nx_pull_match(&nxm, nxm_match_len, 0, &nxm_rule, NULL, NULL);
2626 printf("NXM: %s -> ", nxm_s);
2628 printf("%s\n", ofperr_to_string(error));
2630 printf("%04"PRIx16"/%04"PRIx16"\n",
2631 ntohs(nxm_rule.flow.vlan_tci),
2632 ntohs(nxm_rule.wc.vlan_tci_mask));
2635 ofpbuf_uninit(&nxm);
2637 /* Convert to and from OXM. */
2638 ofpbuf_init(&nxm, 0);
2639 nxm_match_len = oxm_put_match(&nxm, &rule);
2640 nxm_s = oxm_match_to_string(nxm.data, nxm_match_len);
2641 error = oxm_pull_match(&nxm, 0, &nxm_rule);
2642 printf("OXM: %s -> ", nxm_s);
2644 printf("%s\n", ofperr_to_string(error));
2646 uint16_t vid = ntohs(nxm_rule.flow.vlan_tci) &
2647 (VLAN_VID_MASK | VLAN_CFI);
2648 uint16_t mask = ntohs(nxm_rule.wc.vlan_tci_mask) &
2649 (VLAN_VID_MASK | VLAN_CFI);
2651 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
2652 if (vid && vlan_tci_to_pcp(nxm_rule.wc.vlan_tci_mask)) {
2653 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_rule.flow.vlan_tci));
2659 ofpbuf_uninit(&nxm);
2661 /* Convert to and from OpenFlow 1.0. */
2662 ofputil_cls_rule_to_ofp10_match(&rule, &of10_match);
2663 ofputil_cls_rule_from_ofp10_match(&of10_match, 0, &of10_rule);
2664 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2665 ntohs(of10_match.dl_vlan),
2666 (of10_match.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
2667 of10_match.dl_vlan_pcp,
2668 (of10_match.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
2669 ntohs(of10_rule.flow.vlan_tci),
2670 ntohs(of10_rule.wc.vlan_tci_mask));
2672 /* Convert to and from OpenFlow 1.1. */
2673 ofputil_cls_rule_to_ofp11_match(&rule, &of11_match);
2674 ofputil_cls_rule_from_ofp11_match(&of11_match, 0, &of11_rule);
2675 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
2676 ntohs(of11_match.dl_vlan),
2677 (of11_match.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
2678 of11_match.dl_vlan_pcp,
2679 (of11_match.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
2680 ntohs(of11_rule.flow.vlan_tci),
2681 ntohs(of11_rule.wc.vlan_tci_mask));
2684 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2687 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2692 error = ofperr_from_name(argv[1]);
2694 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2697 for (version = 0; version <= UINT8_MAX; version++) {
2698 const char *name = ofperr_domain_get_name(version);
2702 printf("%s: %d,%d\n",
2703 ofperr_domain_get_name(version),
2704 ofperr_get_type(error, version),
2705 ofperr_get_code(error, version));
2709 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2710 * binary data, interpreting them as an OpenFlow message, and prints the
2711 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2713 ofctl_ofp_print(int argc, char *argv[])
2715 struct ofpbuf packet;
2717 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2718 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2719 ovs_fatal(0, "trailing garbage following hex bytes");
2721 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2722 ofpbuf_uninit(&packet);
2725 static const struct command all_commands[] = {
2726 { "show", 1, 1, ofctl_show },
2727 { "monitor", 1, 3, ofctl_monitor },
2728 { "snoop", 1, 1, ofctl_snoop },
2729 { "dump-desc", 1, 1, ofctl_dump_desc },
2730 { "dump-tables", 1, 1, ofctl_dump_tables },
2731 { "dump-flows", 1, 2, ofctl_dump_flows },
2732 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2733 { "queue-stats", 1, 3, ofctl_queue_stats },
2734 { "add-flow", 2, 2, ofctl_add_flow },
2735 { "add-flows", 2, 2, ofctl_add_flows },
2736 { "mod-flows", 2, 2, ofctl_mod_flows },
2737 { "del-flows", 1, 2, ofctl_del_flows },
2738 { "replace-flows", 2, 2, ofctl_replace_flows },
2739 { "diff-flows", 2, 2, ofctl_diff_flows },
2740 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2741 { "dump-ports", 1, 2, ofctl_dump_ports },
2742 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2743 { "mod-port", 3, 3, ofctl_mod_port },
2744 { "get-frags", 1, 1, ofctl_get_frags },
2745 { "set-frags", 2, 2, ofctl_set_frags },
2746 { "probe", 1, 1, ofctl_probe },
2747 { "ping", 1, 2, ofctl_ping },
2748 { "benchmark", 3, 3, ofctl_benchmark },
2749 { "help", 0, INT_MAX, ofctl_help },
2751 /* Undocumented commands for testing. */
2752 { "parse-flow", 1, 1, ofctl_parse_flow },
2753 { "parse-flows", 1, 1, ofctl_parse_flows },
2754 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
2755 { "parse-nxm", 0, 0, ofctl_parse_nxm },
2756 { "parse-oxm", 0, 0, ofctl_parse_oxm },
2757 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
2758 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
2759 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
2760 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
2761 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
2762 { "check-vlan", 2, 2, ofctl_check_vlan },
2763 { "print-error", 1, 1, ofctl_print_error },
2764 { "ofp-print", 1, 2, ofctl_ofp_print },
2766 { NULL, 0, 0, NULL },