2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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.
21 #include <sys/socket.h>
30 #include "byte-order.h"
31 #include "classifier.h"
32 #include "command-line.h"
36 #include "dynamic-string.h"
40 #include "ofp-errors.h"
41 #include "ofp-parse.h"
42 #include "ofp-print.h"
45 #include "ofproto/ofproto.h"
46 #include "openflow/nicira-ext.h"
47 #include "openflow/openflow.h"
49 #include "poll-loop.h"
51 #include "stream-ssl.h"
58 VLOG_DEFINE_THIS_MODULE(ofctl);
60 /* --strict: Use strict matching for flow mod commands? Additionally governs
61 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
65 /* --readd: If true, on replace-flows, re-add even flows that have not changed
66 * (to reset flow counters). */
69 /* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
70 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
71 static int preferred_flow_format = -1;
73 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
74 * commands. Either one of NXPIF_* to force a particular packet_in format, or
75 * -1 to let ovs-ofctl choose the default. */
76 static int preferred_packet_in_format = -1;
78 /* -m, --more: Additional verbosity for ofp-print functions. */
81 static const struct command all_commands[];
83 static void usage(void) NO_RETURN;
84 static void parse_options(int argc, char *argv[]);
87 main(int argc, char *argv[])
89 set_program_name(argv[0]);
90 parse_options(argc, argv);
91 signal(SIGPIPE, SIG_IGN);
92 run_command(argc - optind, argv + optind, all_commands);
97 parse_options(int argc, char *argv[])
100 OPT_STRICT = UCHAR_MAX + 1,
105 static struct option long_options[] = {
106 {"timeout", required_argument, NULL, 't'},
107 {"strict", no_argument, NULL, OPT_STRICT},
108 {"readd", no_argument, NULL, OPT_READD},
109 {"flow-format", required_argument, NULL, 'F'},
110 {"packet-in-format", required_argument, NULL, 'P'},
111 {"more", no_argument, NULL, 'm'},
112 {"help", no_argument, NULL, 'h'},
113 {"version", no_argument, NULL, 'V'},
116 STREAM_SSL_LONG_OPTIONS,
119 char *short_options = long_options_to_short_options(long_options);
122 unsigned long int timeout;
125 c = getopt_long(argc, argv, short_options, long_options, NULL);
132 timeout = strtoul(optarg, NULL, 10);
134 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
142 preferred_flow_format = ofputil_flow_format_from_string(optarg);
143 if (preferred_flow_format < 0) {
144 ovs_fatal(0, "unknown flow format `%s'", optarg);
149 preferred_packet_in_format =
150 ofputil_packet_in_format_from_string(optarg);
151 if (preferred_packet_in_format < 0) {
152 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
164 ovs_print_version(OFP_VERSION, OFP_VERSION);
175 DAEMON_OPTION_HANDLERS
177 STREAM_SSL_OPTION_HANDLERS
192 printf("%s: OpenFlow switch management utility\n"
193 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
194 "\nFor OpenFlow switches:\n"
195 " show SWITCH show OpenFlow information\n"
196 " dump-desc SWITCH print switch description\n"
197 " dump-tables SWITCH print table stats\n"
198 " mod-port SWITCH IFACE ACT modify port behavior\n"
199 " get-frags SWITCH print fragment handling behavior\n"
200 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
201 " dump-ports SWITCH [PORT] print port statistics\n"
202 " dump-flows SWITCH print all flow entries\n"
203 " dump-flows SWITCH FLOW print matching FLOWs\n"
204 " dump-aggregate SWITCH print aggregate flow statistics\n"
205 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
206 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
207 " add-flow SWITCH FLOW add flow described by FLOW\n"
208 " add-flows SWITCH FILE add flows from FILE\n"
209 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
210 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
211 " replace-flows SWITCH FILE replace flows with those in FILE\n"
212 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
213 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
214 " execute ACTIONS on PACKET\n"
215 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
216 " print packets received from SWITCH\n"
217 " snoop SWITCH snoop on SWITCH and its controller\n"
218 "\nFor OpenFlow switches and controllers:\n"
219 " probe TARGET probe whether TARGET is up\n"
220 " ping TARGET [N] latency of N-byte echos\n"
221 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
222 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
223 program_name, program_name);
224 vconn_usage(true, false, false);
227 printf("\nOther options:\n"
228 " --strict use strict match for flow commands\n"
229 " --readd replace flows that haven't changed\n"
230 " -F, --flow-format=FORMAT force particular flow format\n"
231 " -P, --packet-in-format=FRMT force particular packet in format\n"
232 " -m, --more be more verbose printing OpenFlow\n"
233 " -t, --timeout=SECS give up after SECS seconds\n"
234 " -h, --help display this help message\n"
235 " -V, --version display version information\n");
240 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
241 const char *argv[] OVS_UNUSED, void *exiting_)
243 bool *exiting = exiting_;
245 unixctl_command_reply(conn, 200, "");
248 static void run(int retval, const char *message, ...)
251 static void run(int retval, const char *message, ...)
256 va_start(args, message);
257 ovs_fatal_valist(retval, message, args);
261 /* Generic commands. */
264 open_vconn_socket(const char *name, struct vconn **vconnp)
266 char *vconn_name = xasprintf("unix:%s", name);
267 VLOG_DBG("connecting to %s", vconn_name);
268 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
269 "connecting to %s", vconn_name);
274 open_vconn__(const char *name, const char *default_suffix,
275 struct vconn **vconnp)
277 char *datapath_name, *datapath_type, *socket_name;
281 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
283 ofproto_parse_name(name, &datapath_name, &datapath_type);
284 socket_name = xasprintf("%s/%s.%s",
285 ovs_rundir(), datapath_name, default_suffix);
289 if (strchr(name, ':')) {
290 run(vconn_open_block(name, OFP_VERSION, vconnp),
291 "connecting to %s", name);
292 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
293 open_vconn_socket(name, vconnp);
294 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
295 open_vconn_socket(bridge_path, vconnp);
296 } else if (!stat(socket_name, &s)) {
297 if (!S_ISSOCK(s.st_mode)) {
298 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
301 open_vconn_socket(socket_name, vconnp);
303 ovs_fatal(0, "%s is not a bridge or a socket", name);
311 open_vconn(const char *name, struct vconn **vconnp)
313 return open_vconn__(name, "mgmt", vconnp);
317 alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
319 struct ofp_stats_msg *rq;
321 rq = make_openflow(rq_len, OFPT_STATS_REQUEST, bufferp);
322 rq->type = htons(type);
323 rq->flags = htons(0);
328 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
330 update_openflow_length(buffer);
331 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
335 dump_transaction(const char *vconn_name, struct ofpbuf *request)
338 struct ofpbuf *reply;
340 update_openflow_length(request);
341 open_vconn(vconn_name, &vconn);
342 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
343 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
348 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
350 struct ofpbuf *request;
351 make_openflow(sizeof(struct ofp_header), request_type, &request);
352 dump_transaction(vconn_name, request);
356 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
358 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
362 open_vconn(vconn_name, &vconn);
363 send_openflow_buffer(vconn, request);
366 struct ofpbuf *reply;
368 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
369 recv_xid = ((struct ofp_header *) reply->data)->xid;
370 if (send_xid == recv_xid) {
371 struct ofp_stats_msg *osm;
373 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
375 osm = ofpbuf_at(reply, 0, sizeof *osm);
376 done = !osm || !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
378 VLOG_DBG("received reply with xid %08"PRIx32" "
379 "!= expected %08"PRIx32, recv_xid, send_xid);
381 ofpbuf_delete(reply);
387 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
389 struct ofpbuf *request;
390 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
391 dump_stats_transaction(vconn_name, request);
394 /* Sends 'request', which should be a request that only has a reply if an error
395 * occurs, and waits for it to succeed or fail. If an error does occur, prints
396 * it and exits with an error.
398 * Destroys all of the 'requests'. */
400 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
402 struct ofpbuf *request, *reply;
404 LIST_FOR_EACH (request, list_node, requests) {
405 update_openflow_length(request);
408 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
409 "talking to %s", vconn_get_name(vconn));
411 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
414 ofpbuf_delete(reply);
417 /* Sends 'request', which should be a request that only has a reply if an error
418 * occurs, and waits for it to succeed or fail. If an error does occur, prints
419 * it and exits with an error.
421 * Destroys 'request'. */
423 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
425 struct list requests;
427 list_init(&requests);
428 list_push_back(&requests, &request->list_node);
429 transact_multiple_noreply(vconn, &requests);
433 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
435 struct ofp_switch_config *config;
436 struct ofp_header *header;
437 struct ofpbuf *request;
438 struct ofpbuf *reply;
440 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
442 run(vconn_transact(vconn, request, &reply),
443 "talking to %s", vconn_get_name(vconn));
445 header = reply->data;
446 if (header->type != OFPT_GET_CONFIG_REPLY ||
447 header->length != htons(sizeof *config)) {
448 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
451 config = reply->data;
454 ofpbuf_delete(reply);
458 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
460 struct ofp_switch_config *config;
461 struct ofp_header save_header;
462 struct ofpbuf *request;
464 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
465 save_header = config->header;
467 config->header = save_header;
469 transact_noreply(vconn, request);
473 do_show(int argc OVS_UNUSED, char *argv[])
475 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
476 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
480 do_dump_desc(int argc OVS_UNUSED, char *argv[])
482 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
486 do_dump_tables(int argc OVS_UNUSED, char *argv[])
488 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
491 /* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
492 * 'port_name' (which may be a port name or number), and copies it into
495 fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
496 struct ofp_phy_port *oppp)
498 struct ofpbuf *request, *reply;
499 struct ofp_switch_features *osf;
500 unsigned int port_no;
505 /* Try to interpret the argument as a port number. */
506 if (!str_to_uint(port_name, 10, &port_no)) {
510 /* Fetch the switch's ofp_switch_features. */
511 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
512 open_vconn(vconn_name, &vconn);
513 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
516 if (reply->size < sizeof *osf) {
517 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
518 vconn_name, reply->size);
520 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
522 for (port_idx = 0; port_idx < n_ports; port_idx++) {
523 const struct ofp_phy_port *opp = &osf->ports[port_idx];
525 if (port_no != UINT_MAX
526 ? htons(port_no) == opp->port_no
527 : !strncmp(opp->name, port_name, sizeof opp->name)) {
529 ofpbuf_delete(reply);
534 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
537 /* Returns the port number corresponding to 'port_name' (which may be a port
538 * name or number) within the switch 'vconn_name'. */
540 str_to_port_no(const char *vconn_name, const char *port_name)
542 unsigned int port_no;
544 if (str_to_uint(port_name, 10, &port_no)) {
547 struct ofp_phy_port opp;
549 fetch_ofp_phy_port(vconn_name, port_name, &opp);
550 return ntohs(opp.port_no);
555 try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
557 struct ofpbuf *sff, *reply;
559 sff = ofputil_make_set_flow_format(flow_format);
560 run(vconn_transact_noreply(vconn, sff, &reply),
561 "talking to %s", vconn_get_name(vconn));
563 char *s = ofp_to_string(reply->data, reply->size, 2);
564 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
565 vconn_get_name(vconn),
566 ofputil_flow_format_to_string(flow_format),
569 ofpbuf_delete(reply);
576 set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
578 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
579 transact_noreply(vconn, sff);
580 VLOG_DBG("%s: using user-specified flow format %s",
581 vconn_get_name(vconn),
582 ofputil_flow_format_to_string(flow_format));
585 static enum nx_flow_format
586 negotiate_highest_flow_format(struct vconn *vconn,
587 enum nx_flow_format min_format)
589 if (preferred_flow_format != -1) {
590 if (preferred_flow_format < min_format) {
591 ovs_fatal(0, "%s: cannot use requested flow format %s for "
592 "specified flow", vconn_get_name(vconn),
593 ofputil_flow_format_to_string(min_format));
596 set_flow_format(vconn, preferred_flow_format);
597 return preferred_flow_format;
599 enum nx_flow_format flow_format;
601 if (try_set_flow_format(vconn, NXFF_NXM)) {
602 flow_format = NXFF_NXM;
604 flow_format = NXFF_OPENFLOW10;
607 if (flow_format < min_format) {
608 ovs_fatal(0, "%s: cannot use switch's most advanced flow format "
609 "%s for specified flow", vconn_get_name(vconn),
610 ofputil_flow_format_to_string(min_format));
613 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
614 ofputil_flow_format_to_string(flow_format));
620 do_dump_flows__(int argc, char *argv[], bool aggregate)
622 enum nx_flow_format min_flow_format, flow_format;
623 struct ofputil_flow_stats_request fsr;
624 struct ofpbuf *request;
627 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
629 open_vconn(argv[1], &vconn);
630 min_flow_format = ofputil_min_flow_format(&fsr.match);
631 if (fsr.cookie_mask != htonll(0)) {
632 min_flow_format = NXFF_NXM;
634 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
635 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
636 dump_stats_transaction(argv[1], request);
641 do_dump_flows(int argc, char *argv[])
643 return do_dump_flows__(argc, argv, false);
647 do_dump_aggregate(int argc, char *argv[])
649 return do_dump_flows__(argc, argv, true);
653 do_queue_stats(int argc, char *argv[])
655 struct ofp_queue_stats_request *req;
656 struct ofpbuf *request;
658 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
660 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
661 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
663 req->port_no = htons(OFPP_ALL);
665 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
666 req->queue_id = htonl(atoi(argv[3]));
668 req->queue_id = htonl(OFPQ_ALL);
671 memset(req->pad, 0, sizeof req->pad);
673 dump_stats_transaction(argv[1], request);
676 /* Sets up the flow format for a vconn that will be used to modify the flow
677 * table. Returns the flow format used, after possibly adding an OpenFlow
678 * request to 'requests'.
680 * If 'preferred_flow_format' is -1, returns NXFF_OPENFLOW10 without modifying
681 * 'requests', since NXFF_OPENFLOW10 is the default flow format for any
682 * OpenFlow connection.
684 * If 'preferred_flow_format' is a specific format, adds a request to set that
685 * format to 'requests' and returns the format. */
686 static enum nx_flow_format
687 set_initial_format_for_flow_mod(struct list *requests)
689 if (preferred_flow_format < 0) {
690 return NXFF_OPENFLOW10;
694 sff = ofputil_make_set_flow_format(preferred_flow_format);
695 list_push_back(requests, &sff->list_node);
696 return preferred_flow_format;
700 /* Checks that 'flow_format' is acceptable as a flow format after a flow_mod
701 * operation, given the global 'preferred_flow_format'. */
703 check_final_format_for_flow_mod(enum nx_flow_format flow_format)
705 if (preferred_flow_format >= 0 && flow_format > preferred_flow_format) {
706 ovs_fatal(0, "flow cannot be expressed in flow format %s "
707 "(flow format %s or better is required)",
708 ofputil_flow_format_to_string(preferred_flow_format),
709 ofputil_flow_format_to_string(flow_format));
714 do_flow_mod_file__(int argc OVS_UNUSED, char *argv[], uint16_t command)
716 enum nx_flow_format flow_format;
717 bool flow_mod_table_id;
718 struct list requests;
722 file = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r");
724 ovs_fatal(errno, "%s: open", argv[2]);
727 list_init(&requests);
728 flow_format = set_initial_format_for_flow_mod(&requests);
729 flow_mod_table_id = false;
731 open_vconn(argv[1], &vconn);
732 while (parse_ofp_flow_mod_file(&requests, &flow_format, &flow_mod_table_id,
734 check_final_format_for_flow_mod(flow_format);
735 transact_multiple_noreply(vconn, &requests);
745 do_flow_mod__(int argc, char *argv[], uint16_t command)
747 enum nx_flow_format flow_format;
748 bool flow_mod_table_id;
749 struct list requests;
752 if (argc > 2 && !strcmp(argv[2], "-")) {
753 do_flow_mod_file__(argc, argv, command);
757 list_init(&requests);
758 flow_format = set_initial_format_for_flow_mod(&requests);
759 flow_mod_table_id = false;
761 parse_ofp_flow_mod_str(&requests, &flow_format, &flow_mod_table_id,
762 argc > 2 ? argv[2] : "", command, false);
763 check_final_format_for_flow_mod(flow_format);
765 open_vconn(argv[1], &vconn);
766 transact_multiple_noreply(vconn, &requests);
771 do_add_flow(int argc, char *argv[])
773 do_flow_mod__(argc, argv, OFPFC_ADD);
777 do_add_flows(int argc, char *argv[])
779 do_flow_mod_file__(argc, argv, OFPFC_ADD);
783 do_mod_flows(int argc, char *argv[])
785 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
789 do_del_flows(int argc, char *argv[])
791 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
795 set_packet_in_format(struct vconn *vconn,
796 enum nx_packet_in_format packet_in_format)
798 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
799 transact_noreply(vconn, spif);
800 VLOG_DBG("%s: using user-specified packet in format %s",
801 vconn_get_name(vconn),
802 ofputil_packet_in_format_to_string(packet_in_format));
806 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
808 struct ofp_switch_config config;
809 enum ofp_config_flags flags;
811 fetch_switch_config(vconn, &config);
812 flags = ntohs(config.flags);
813 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
814 /* Set the invalid ttl config. */
815 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
817 config.flags = htons(flags);
818 set_switch_config(vconn, &config);
820 /* Then retrieve the configuration to see if it really took. OpenFlow
821 * doesn't define error reporting for bad modes, so this is all we can
823 fetch_switch_config(vconn, &config);
824 flags = ntohs(config.flags);
825 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
826 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
827 "switch probably doesn't support mode)");
834 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
835 * caller must free '*msgp'. On success, returns NULL. On failure, returns
836 * an error message and stores NULL in '*msgp'. */
838 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
840 struct ofp_header *oh;
843 msg = ofpbuf_new(strlen(hex) / 2);
846 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
848 return "Trailing garbage in hex data";
851 if (msg->size < sizeof(struct ofp_header)) {
853 return "Message too short for OpenFlow";
857 if (msg->size != ntohs(oh->length)) {
859 return "Message size does not match length in OpenFlow header";
867 ofctl_send(struct unixctl_conn *conn, int argc,
868 const char *argv[], void *vconn_)
870 struct vconn *vconn = vconn_;
877 for (i = 1; i < argc; i++) {
878 const char *error_msg;
882 error_msg = openflow_from_hex(argv[i], &msg);
884 ds_put_format(&reply, "%s\n", error_msg);
889 fprintf(stderr, "send: ");
890 ofp_print(stderr, msg->data, msg->size, verbosity);
892 error = vconn_send_block(vconn, msg);
895 ds_put_format(&reply, "%s\n", strerror(error));
898 ds_put_cstr(&reply, "sent\n");
901 unixctl_command_reply(conn, ok ? 200 : 501, ds_cstr(&reply));
906 monitor_vconn(struct vconn *vconn)
908 struct unixctl_server *server;
909 bool exiting = false;
912 daemon_save_fd(STDERR_FILENO);
914 error = unixctl_server_create(NULL, &server);
916 ovs_fatal(error, "failed to create unixctl server");
918 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
919 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
921 daemonize_complete();
927 unixctl_server_run(server);
930 retval = vconn_recv(vconn, &b);
931 if (retval == EAGAIN) {
935 run(retval, "vconn_recv");
936 ofp_print(stderr, b->data, b->size, verbosity + 2);
945 vconn_run_wait(vconn);
946 vconn_recv_wait(vconn);
947 unixctl_server_wait(server);
951 unixctl_server_destroy(server);
955 do_monitor(int argc, char *argv[])
959 open_vconn(argv[1], &vconn);
961 struct ofp_switch_config config;
963 fetch_switch_config(vconn, &config);
964 config.miss_send_len = htons(atoi(argv[2]));
965 set_switch_config(vconn, &config);
968 if (!strcmp(argv[3], "invalid_ttl")) {
969 monitor_set_invalid_ttl_to_controller(vconn);
972 if (preferred_packet_in_format >= 0) {
973 set_packet_in_format(vconn, preferred_packet_in_format);
975 struct ofpbuf *spif, *reply;
977 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
978 run(vconn_transact_noreply(vconn, spif, &reply),
979 "talking to %s", vconn_get_name(vconn));
981 char *s = ofp_to_string(reply->data, reply->size, 2);
982 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
983 " replied: %s. Falling back to the switch default.",
984 vconn_get_name(vconn), s);
986 ofpbuf_delete(reply);
990 monitor_vconn(vconn);
994 do_snoop(int argc OVS_UNUSED, char *argv[])
998 open_vconn__(argv[1], "snoop", &vconn);
999 monitor_vconn(vconn);
1003 do_dump_ports(int argc, char *argv[])
1005 struct ofp_port_stats_request *req;
1006 struct ofpbuf *request;
1009 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1010 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1011 req->port_no = htons(port);
1012 dump_stats_transaction(argv[1], request);
1016 do_probe(int argc OVS_UNUSED, char *argv[])
1018 struct ofpbuf *request;
1019 struct vconn *vconn;
1020 struct ofpbuf *reply;
1022 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1023 open_vconn(argv[1], &vconn);
1024 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1025 if (reply->size != sizeof(struct ofp_header)) {
1026 ovs_fatal(0, "reply does not match request");
1028 ofpbuf_delete(reply);
1033 do_packet_out(int argc, char *argv[])
1035 struct ofputil_packet_out po;
1036 struct ofpbuf actions;
1037 struct vconn *vconn;
1040 ofpbuf_init(&actions, sizeof(union ofp_action));
1041 parse_ofp_actions(argv[3], &actions);
1043 po.buffer_id = UINT32_MAX;
1044 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1045 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1046 : str_to_port_no(argv[1], argv[2]));
1047 po.actions = actions.data;
1048 po.n_actions = actions.size / sizeof(union ofp_action);
1050 open_vconn(argv[1], &vconn);
1051 for (i = 4; i < argc; i++) {
1052 struct ofpbuf *packet, *opo;
1053 const char *error_msg;
1055 error_msg = eth_from_hex(argv[i], &packet);
1057 ovs_fatal(0, "%s", error_msg);
1060 po.packet = packet->data;
1061 po.packet_len = packet->size;
1062 opo = ofputil_encode_packet_out(&po);
1063 transact_noreply(vconn, opo);
1064 ofpbuf_delete(packet);
1070 do_mod_port(int argc OVS_UNUSED, char *argv[])
1072 struct ofp_port_mod *opm;
1073 struct ofp_phy_port opp;
1074 struct ofpbuf *request;
1075 struct vconn *vconn;
1077 fetch_ofp_phy_port(argv[1], argv[2], &opp);
1079 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1080 opm->port_no = opp.port_no;
1081 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
1082 opm->config = htonl(0);
1083 opm->mask = htonl(0);
1084 opm->advertise = htonl(0);
1086 if (!strcasecmp(argv[3], "up")) {
1087 opm->mask |= htonl(OFPPC_PORT_DOWN);
1088 } else if (!strcasecmp(argv[3], "down")) {
1089 opm->mask |= htonl(OFPPC_PORT_DOWN);
1090 opm->config |= htonl(OFPPC_PORT_DOWN);
1091 } else if (!strcasecmp(argv[3], "flood")) {
1092 opm->mask |= htonl(OFPPC_NO_FLOOD);
1093 } else if (!strcasecmp(argv[3], "noflood")) {
1094 opm->mask |= htonl(OFPPC_NO_FLOOD);
1095 opm->config |= htonl(OFPPC_NO_FLOOD);
1096 } else if (!strcasecmp(argv[3], "forward")) {
1097 opm->mask |= htonl(OFPPC_NO_FWD);
1098 } else if (!strcasecmp(argv[3], "noforward")) {
1099 opm->mask |= htonl(OFPPC_NO_FWD);
1100 opm->config |= htonl(OFPPC_NO_FWD);
1102 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1105 open_vconn(argv[1], &vconn);
1106 transact_noreply(vconn, request);
1111 do_get_frags(int argc OVS_UNUSED, char *argv[])
1113 struct ofp_switch_config config;
1114 struct vconn *vconn;
1116 open_vconn(argv[1], &vconn);
1117 fetch_switch_config(vconn, &config);
1118 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1123 do_set_frags(int argc OVS_UNUSED, char *argv[])
1125 struct ofp_switch_config config;
1126 enum ofp_config_flags mode;
1127 struct vconn *vconn;
1130 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1131 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1134 open_vconn(argv[1], &vconn);
1135 fetch_switch_config(vconn, &config);
1136 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1137 if (flags != config.flags) {
1138 /* Set the configuration. */
1139 config.flags = flags;
1140 set_switch_config(vconn, &config);
1142 /* Then retrieve the configuration to see if it really took. OpenFlow
1143 * doesn't define error reporting for bad modes, so this is all we can
1145 fetch_switch_config(vconn, &config);
1146 if (flags != config.flags) {
1147 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1148 "switch probably doesn't support mode \"%s\")",
1149 argv[1], ofputil_frag_handling_to_string(mode));
1156 do_ping(int argc, char *argv[])
1158 size_t max_payload = 65535 - sizeof(struct ofp_header);
1159 unsigned int payload;
1160 struct vconn *vconn;
1163 payload = argc > 2 ? atoi(argv[2]) : 64;
1164 if (payload > max_payload) {
1165 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1168 open_vconn(argv[1], &vconn);
1169 for (i = 0; i < 10; i++) {
1170 struct timeval start, end;
1171 struct ofpbuf *request, *reply;
1172 struct ofp_header *rq_hdr, *rpy_hdr;
1174 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1175 OFPT_ECHO_REQUEST, &request);
1176 random_bytes(rq_hdr + 1, payload);
1178 xgettimeofday(&start);
1179 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1180 xgettimeofday(&end);
1182 rpy_hdr = reply->data;
1183 if (reply->size != request->size
1184 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1185 || rpy_hdr->xid != rq_hdr->xid
1186 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1187 printf("Reply does not match request. Request:\n");
1188 ofp_print(stdout, request, request->size, verbosity + 2);
1190 ofp_print(stdout, reply, reply->size, verbosity + 2);
1192 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1193 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
1194 (1000*(double)(end.tv_sec - start.tv_sec))
1195 + (.001*(end.tv_usec - start.tv_usec)));
1196 ofpbuf_delete(request);
1197 ofpbuf_delete(reply);
1203 do_benchmark(int argc OVS_UNUSED, char *argv[])
1205 size_t max_payload = 65535 - sizeof(struct ofp_header);
1206 struct timeval start, end;
1207 unsigned int payload_size, message_size;
1208 struct vconn *vconn;
1213 payload_size = atoi(argv[2]);
1214 if (payload_size > max_payload) {
1215 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1217 message_size = sizeof(struct ofp_header) + payload_size;
1219 count = atoi(argv[3]);
1221 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1222 count, message_size, count * message_size);
1224 open_vconn(argv[1], &vconn);
1225 xgettimeofday(&start);
1226 for (i = 0; i < count; i++) {
1227 struct ofpbuf *request, *reply;
1228 struct ofp_header *rq_hdr;
1230 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1231 memset(rq_hdr + 1, 0, payload_size);
1232 run(vconn_transact(vconn, request, &reply), "transact");
1233 ofpbuf_delete(reply);
1235 xgettimeofday(&end);
1238 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1239 + (.001*(end.tv_usec - start.tv_usec)));
1240 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1241 duration, count / (duration / 1000.0),
1242 count * message_size / (duration / 1000.0));
1246 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1251 /* replace-flows and diff-flows commands. */
1253 /* A flow table entry, possibly with two different versions. */
1255 struct cls_rule rule; /* Within a "struct classifier". */
1256 struct fte_version *versions[2];
1259 /* One version of a Flow Table Entry. */
1260 struct fte_version {
1262 uint16_t idle_timeout;
1263 uint16_t hard_timeout;
1265 union ofp_action *actions;
1269 /* Frees 'version' and the data that it owns. */
1271 fte_version_free(struct fte_version *version)
1274 free(version->actions);
1279 /* Returns true if 'a' and 'b' are the same, false if they differ.
1281 * Ignores differences in 'flags' because there's no way to retrieve flags from
1282 * an OpenFlow switch. We have to assume that they are the same. */
1284 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1286 return (a->cookie == b->cookie
1287 && a->idle_timeout == b->idle_timeout
1288 && a->hard_timeout == b->hard_timeout
1289 && a->n_actions == b->n_actions
1290 && !memcmp(a->actions, b->actions,
1291 a->n_actions * sizeof *a->actions));
1294 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1295 * associated with the version. */
1297 fte_version_print(const struct fte_version *version)
1301 if (version->cookie != htonll(0)) {
1302 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1304 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1305 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1307 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1308 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1312 ofp_print_actions(&s, version->actions, version->n_actions);
1313 printf(" %s\n", ds_cstr(&s));
1318 fte_from_cls_rule(const struct cls_rule *cls_rule)
1320 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1323 /* Frees 'fte' and its versions. */
1325 fte_free(struct fte *fte)
1328 fte_version_free(fte->versions[0]);
1329 fte_version_free(fte->versions[1]);
1334 /* Frees all of the FTEs within 'cls'. */
1336 fte_free_all(struct classifier *cls)
1338 struct cls_cursor cursor;
1339 struct fte *fte, *next;
1341 cls_cursor_init(&cursor, cls, NULL);
1342 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1343 classifier_remove(cls, &fte->rule);
1348 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1349 * necessary. Sets 'version' as the version of that rule with the given
1350 * 'index', replacing any existing version, if any.
1352 * Takes ownership of 'version'. */
1354 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1355 struct fte_version *version, int index)
1357 struct fte *old, *fte;
1359 fte = xzalloc(sizeof *fte);
1361 fte->versions[index] = version;
1363 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1365 fte_version_free(old->versions[index]);
1366 fte->versions[!index] = old->versions[!index];
1371 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1372 * with the specified 'index'. Returns the minimum flow format required to
1373 * represent the flows that were read. */
1374 static enum nx_flow_format
1375 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1377 enum nx_flow_format min_flow_format;
1381 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1383 ovs_fatal(errno, "%s: open", filename);
1387 min_flow_format = NXFF_OPENFLOW10;
1388 while (!ds_get_preprocessed_line(&s, file)) {
1389 struct fte_version *version;
1390 struct ofputil_flow_mod fm;
1391 enum nx_flow_format min_ff;
1393 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1395 version = xmalloc(sizeof *version);
1396 version->cookie = fm.cookie;
1397 version->idle_timeout = fm.idle_timeout;
1398 version->hard_timeout = fm.hard_timeout;
1399 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1400 version->actions = fm.actions;
1401 version->n_actions = fm.n_actions;
1403 min_ff = ofputil_min_flow_format(&fm.cr);
1404 min_flow_format = MAX(min_flow_format, min_ff);
1405 check_final_format_for_flow_mod(min_flow_format);
1407 fte_insert(cls, &fm.cr, version, index);
1411 if (file != stdin) {
1415 return min_flow_format;
1418 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1419 * format 'flow_format', and adds them as flow table entries in 'cls' for the
1420 * version with the specified 'index'. */
1422 read_flows_from_switch(struct vconn *vconn, enum nx_flow_format flow_format,
1423 struct classifier *cls, int index)
1425 struct ofputil_flow_stats_request fsr;
1426 struct ofpbuf *request;
1430 fsr.aggregate = false;
1431 cls_rule_init_catchall(&fsr.match, 0);
1432 fsr.out_port = OFPP_NONE;
1433 fsr.table_id = 0xff;
1434 fsr.cookie = fsr.cookie_mask = htonll(0);
1435 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
1436 send_xid = ((struct ofp_header *) request->data)->xid;
1437 send_openflow_buffer(vconn, request);
1442 struct ofpbuf *reply;
1444 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1445 recv_xid = ((struct ofp_header *) reply->data)->xid;
1446 if (send_xid == recv_xid) {
1447 const struct ofputil_msg_type *type;
1448 const struct ofp_stats_msg *osm;
1449 enum ofputil_msg_code code;
1451 ofputil_decode_msg_type(reply->data, &type);
1452 code = ofputil_msg_type_code(type);
1453 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1454 code != OFPUTIL_NXST_FLOW_REPLY) {
1455 ovs_fatal(0, "received bad reply: %s",
1456 ofp_to_string(reply->data, reply->size,
1461 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
1466 struct fte_version *version;
1467 struct ofputil_flow_stats fs;
1470 retval = ofputil_decode_flow_stats_reply(&fs, reply, false);
1472 if (retval != EOF) {
1473 ovs_fatal(0, "parse error in reply");
1478 version = xmalloc(sizeof *version);
1479 version->cookie = fs.cookie;
1480 version->idle_timeout = fs.idle_timeout;
1481 version->hard_timeout = fs.hard_timeout;
1483 version->n_actions = fs.n_actions;
1484 version->actions = xmemdup(fs.actions,
1485 fs.n_actions * sizeof *fs.actions);
1487 fte_insert(cls, &fs.rule, version, index);
1490 VLOG_DBG("received reply with xid %08"PRIx32" "
1491 "!= expected %08"PRIx32, recv_xid, send_xid);
1493 ofpbuf_delete(reply);
1498 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1499 enum nx_flow_format flow_format, struct list *packets)
1501 const struct fte_version *version = fte->versions[index];
1502 struct ofputil_flow_mod fm;
1506 fm.cookie = version->cookie;
1508 fm.command = command;
1509 fm.idle_timeout = version->idle_timeout;
1510 fm.hard_timeout = version->hard_timeout;
1511 fm.buffer_id = UINT32_MAX;
1512 fm.out_port = OFPP_NONE;
1513 fm.flags = version->flags;
1514 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1515 command == OFPFC_MODIFY_STRICT) {
1516 fm.actions = version->actions;
1517 fm.n_actions = version->n_actions;
1523 ofm = ofputil_encode_flow_mod(&fm, flow_format, false);
1524 list_push_back(packets, &ofm->list_node);
1528 do_replace_flows(int argc OVS_UNUSED, char *argv[])
1530 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1531 enum nx_flow_format min_flow_format, flow_format;
1532 struct cls_cursor cursor;
1533 struct classifier cls;
1534 struct list requests;
1535 struct vconn *vconn;
1538 classifier_init(&cls);
1539 min_flow_format = read_flows_from_file(argv[2], &cls, FILE_IDX);
1541 open_vconn(argv[1], &vconn);
1542 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
1543 read_flows_from_switch(vconn, flow_format, &cls, SWITCH_IDX);
1545 list_init(&requests);
1547 /* Delete flows that exist on the switch but not in the file. */
1548 cls_cursor_init(&cursor, &cls, NULL);
1549 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1550 struct fte_version *file_ver = fte->versions[FILE_IDX];
1551 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1553 if (sw_ver && !file_ver) {
1554 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1555 flow_format, &requests);
1559 /* Add flows that exist in the file but not on the switch.
1560 * Update flows that exist in both places but differ. */
1561 cls_cursor_init(&cursor, &cls, NULL);
1562 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1563 struct fte_version *file_ver = fte->versions[FILE_IDX];
1564 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1567 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1568 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, flow_format,
1572 transact_multiple_noreply(vconn, &requests);
1579 read_flows_from_source(const char *source, struct classifier *cls, int index)
1583 if (source[0] == '/' || source[0] == '.'
1584 || (!strchr(source, ':') && !stat(source, &s))) {
1585 read_flows_from_file(source, cls, index);
1587 enum nx_flow_format flow_format;
1588 struct vconn *vconn;
1590 open_vconn(source, &vconn);
1591 flow_format = negotiate_highest_flow_format(vconn, NXFF_OPENFLOW10);
1592 read_flows_from_switch(vconn, flow_format, cls, index);
1598 do_diff_flows(int argc OVS_UNUSED, char *argv[])
1600 bool differences = false;
1601 struct cls_cursor cursor;
1602 struct classifier cls;
1605 classifier_init(&cls);
1606 read_flows_from_source(argv[1], &cls, 0);
1607 read_flows_from_source(argv[2], &cls, 1);
1609 cls_cursor_init(&cursor, &cls, NULL);
1610 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1611 struct fte_version *a = fte->versions[0];
1612 struct fte_version *b = fte->versions[1];
1614 if (!a || !b || !fte_version_equals(a, b)) {
1615 char *rule_s = cls_rule_to_string(&fte->rule);
1617 printf("-%s", rule_s);
1618 fte_version_print(a);
1621 printf("+%s", rule_s);
1622 fte_version_print(b);
1637 /* Undocumented commands for unit testing. */
1640 print_packet_list(struct list *packets)
1642 struct ofpbuf *packet, *next;
1644 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
1645 ofp_print(stdout, packet->data, packet->size, verbosity);
1646 list_remove(&packet->list_node);
1647 ofpbuf_delete(packet);
1651 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1652 * it back to stdout. */
1654 do_parse_flow(int argc OVS_UNUSED, char *argv[])
1656 enum nx_flow_format flow_format;
1657 bool flow_mod_table_id;
1658 struct list packets;
1660 flow_format = NXFF_OPENFLOW10;
1661 if (preferred_flow_format > 0) {
1662 flow_format = preferred_flow_format;
1664 flow_mod_table_id = false;
1666 list_init(&packets);
1667 parse_ofp_flow_mod_str(&packets, &flow_format, &flow_mod_table_id,
1668 argv[1], OFPFC_ADD, false);
1669 print_packet_list(&packets);
1672 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1673 * add-flows) and prints each of the flows back to stdout. */
1675 do_parse_flows(int argc OVS_UNUSED, char *argv[])
1677 enum nx_flow_format flow_format;
1678 bool flow_mod_table_id;
1679 struct list packets;
1682 file = fopen(argv[1], "r");
1684 ovs_fatal(errno, "%s: open", argv[1]);
1687 flow_format = NXFF_OPENFLOW10;
1688 if (preferred_flow_format > 0) {
1689 flow_format = preferred_flow_format;
1691 flow_mod_table_id = false;
1693 list_init(&packets);
1694 while (parse_ofp_flow_mod_file(&packets, &flow_format, &flow_mod_table_id,
1696 print_packet_list(&packets);
1701 /* "parse-nx-match": reads a series of nx_match specifications as strings from
1702 * stdin, does some internal fussing with them, and then prints them back as
1703 * strings on stdout. */
1705 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1710 while (!ds_get_line(&in, stdin)) {
1711 struct ofpbuf nx_match;
1712 struct cls_rule rule;
1713 ovs_be64 cookie, cookie_mask;
1718 /* Delete comments, skip blank lines. */
1724 if (strchr(s, '#')) {
1725 *strchr(s, '#') = '\0';
1727 if (s[strspn(s, " ")] == '\0') {
1732 /* Convert string to nx_match. */
1733 ofpbuf_init(&nx_match, 0);
1734 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1736 /* Convert nx_match to cls_rule. */
1738 error = nx_pull_match(&nx_match, match_len, 0, &rule,
1739 &cookie, &cookie_mask);
1741 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
1742 &cookie, &cookie_mask);
1748 /* Convert cls_rule back to nx_match. */
1749 ofpbuf_uninit(&nx_match);
1750 ofpbuf_init(&nx_match, 0);
1751 match_len = nx_put_match(&nx_match, &rule, cookie, cookie_mask);
1753 /* Convert nx_match to string. */
1754 out = nx_match_to_string(nx_match.data, match_len);
1758 printf("nx_pull_match() returned error %s\n",
1759 ofperr_get_name(error));
1762 ofpbuf_uninit(&nx_match);
1767 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1768 * binary data, interpreting them as an OpenFlow message, and prints the
1769 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1771 do_ofp_print(int argc, char *argv[])
1773 struct ofpbuf packet;
1775 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1776 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1777 ovs_fatal(0, "trailing garbage following hex bytes");
1779 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1780 ofpbuf_uninit(&packet);
1783 static const struct command all_commands[] = {
1784 { "show", 1, 1, do_show },
1785 { "monitor", 1, 3, do_monitor },
1786 { "snoop", 1, 1, do_snoop },
1787 { "dump-desc", 1, 1, do_dump_desc },
1788 { "dump-tables", 1, 1, do_dump_tables },
1789 { "dump-flows", 1, 2, do_dump_flows },
1790 { "dump-aggregate", 1, 2, do_dump_aggregate },
1791 { "queue-stats", 1, 3, do_queue_stats },
1792 { "add-flow", 2, 2, do_add_flow },
1793 { "add-flows", 2, 2, do_add_flows },
1794 { "mod-flows", 2, 2, do_mod_flows },
1795 { "del-flows", 1, 2, do_del_flows },
1796 { "replace-flows", 2, 2, do_replace_flows },
1797 { "diff-flows", 2, 2, do_diff_flows },
1798 { "packet-out", 4, INT_MAX, do_packet_out },
1799 { "dump-ports", 1, 2, do_dump_ports },
1800 { "mod-port", 3, 3, do_mod_port },
1801 { "get-frags", 1, 1, do_get_frags },
1802 { "set-frags", 2, 2, do_set_frags },
1803 { "probe", 1, 1, do_probe },
1804 { "ping", 1, 2, do_ping },
1805 { "benchmark", 3, 3, do_benchmark },
1806 { "help", 0, INT_MAX, do_help },
1808 /* Undocumented commands for testing. */
1809 { "parse-flow", 1, 1, do_parse_flow },
1810 { "parse-flows", 1, 1, do_parse_flows },
1811 { "parse-nx-match", 0, 0, do_parse_nx_match },
1812 { "ofp-print", 1, 2, do_ofp_print },
1814 { NULL, 0, 0, NULL },