2 * Copyright (c) 2008, 2009, 2010, 2011 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.
29 #include "byte-order.h"
30 #include "classifier.h"
31 #include "command-line.h"
35 #include "dynamic-string.h"
39 #include "ofp-parse.h"
40 #include "ofp-print.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
46 #include "stream-ssl.h"
52 VLOG_DEFINE_THIS_MODULE(ofctl);
54 /* --strict: Use strict matching for flow mod commands? */
57 /* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
58 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
59 static int preferred_flow_format = -1;
61 /* -m, --more: Additional verbosity for ofp-print functions. */
64 static const struct command all_commands[];
66 static void usage(void) NO_RETURN;
67 static void parse_options(int argc, char *argv[]);
70 main(int argc, char *argv[])
72 set_program_name(argv[0]);
73 parse_options(argc, argv);
74 signal(SIGPIPE, SIG_IGN);
75 run_command(argc - optind, argv + optind, all_commands);
80 parse_options(int argc, char *argv[])
83 OPT_STRICT = UCHAR_MAX + 1,
86 static struct option long_options[] = {
87 {"timeout", required_argument, 0, 't'},
88 {"strict", no_argument, 0, OPT_STRICT},
89 {"flow-format", required_argument, 0, 'F'},
90 {"more", no_argument, 0, 'm'},
91 {"help", no_argument, 0, 'h'},
92 {"version", no_argument, 0, 'V'},
94 STREAM_SSL_LONG_OPTIONS
97 char *short_options = long_options_to_short_options(long_options);
100 unsigned long int timeout;
103 c = getopt_long(argc, argv, short_options, long_options, NULL);
110 timeout = strtoul(optarg, NULL, 10);
112 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
120 preferred_flow_format = ofputil_flow_format_from_string(optarg);
121 if (preferred_flow_format < 0) {
122 ovs_fatal(0, "unknown flow format `%s'", optarg);
134 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
142 STREAM_SSL_OPTION_HANDLERS
157 printf("%s: OpenFlow switch management utility\n"
158 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
159 "\nFor OpenFlow switches:\n"
160 " show SWITCH show OpenFlow information\n"
161 " status SWITCH [KEY] report statistics (about KEY)\n"
162 " dump-desc SWITCH print switch description\n"
163 " dump-tables SWITCH print table stats\n"
164 " mod-port SWITCH IFACE ACT modify port behavior\n"
165 " dump-ports SWITCH [PORT] print port statistics\n"
166 " dump-flows SWITCH print all flow entries\n"
167 " dump-flows SWITCH FLOW print matching FLOWs\n"
168 " dump-aggregate SWITCH print aggregate flow statistics\n"
169 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
170 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
171 " add-flow SWITCH FLOW add flow described by FLOW\n"
172 " add-flows SWITCH FILE add flows from FILE\n"
173 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
174 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
175 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
176 "\nFor OpenFlow switches and controllers:\n"
177 " probe VCONN probe whether VCONN is up\n"
178 " ping VCONN [N] latency of N-byte echos\n"
179 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
180 "where each SWITCH is an active OpenFlow connection method.\n",
181 program_name, program_name);
182 vconn_usage(true, false, false);
184 printf("\nOther options:\n"
185 " --strict use strict match for flow commands\n"
186 " -F, --flow-format=FORMAT force particular flow format\n"
187 " -m, --more be more verbose printing OpenFlow\n"
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n"
190 " -V, --version display version information\n");
194 static void run(int retval, const char *message, ...)
197 static void run(int retval, const char *message, ...)
202 fprintf(stderr, "%s: ", program_name);
203 va_start(args, message);
204 vfprintf(stderr, message, args);
207 fputs(": unexpected end of file\n", stderr);
209 fprintf(stderr, ": %s\n", strerror(retval));
216 /* Generic commands. */
219 open_vconn_socket(const char *name, struct vconn **vconnp)
221 char *vconn_name = xasprintf("unix:%s", name);
222 VLOG_DBG("connecting to %s", vconn_name);
223 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
224 "connecting to %s", vconn_name);
229 open_vconn__(const char *name, const char *default_suffix,
230 struct vconn **vconnp)
234 char *bridge_path, *datapath_name, *datapath_type;
236 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
237 dp_parse_name(name, &datapath_name, &datapath_type);
239 if (strstr(name, ":")) {
240 run(vconn_open_block(name, OFP_VERSION, vconnp),
241 "connecting to %s", name);
242 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
243 open_vconn_socket(name, vconnp);
244 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
245 open_vconn_socket(bridge_path, vconnp);
246 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
247 char dpif_name[IF_NAMESIZE + 1];
250 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
251 "obtaining name of %s", dpif_name);
253 if (strcmp(dpif_name, name)) {
254 VLOG_DBG("datapath %s is named %s", name, dpif_name);
257 socket_name = xasprintf("%s/%s.%s",
258 ovs_rundir(), dpif_name, default_suffix);
259 if (stat(socket_name, &s)) {
260 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
262 } else if (!S_ISSOCK(s.st_mode)) {
263 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
267 open_vconn_socket(socket_name, vconnp);
270 ovs_fatal(0, "%s is not a valid connection method", name);
279 open_vconn(const char *name, struct vconn **vconnp)
281 return open_vconn__(name, "mgmt", vconnp);
285 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
287 struct ofp_stats_request *rq;
288 rq = make_openflow((offsetof(struct ofp_stats_request, body)
289 + body_len), OFPT_STATS_REQUEST, bufferp);
290 rq->type = htons(type);
291 rq->flags = htons(0);
296 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
298 update_openflow_length(buffer);
299 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
303 dump_transaction(const char *vconn_name, struct ofpbuf *request)
306 struct ofpbuf *reply;
308 update_openflow_length(request);
309 open_vconn(vconn_name, &vconn);
310 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
311 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
316 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
318 struct ofpbuf *request;
319 make_openflow(sizeof(struct ofp_header), request_type, &request);
320 dump_transaction(vconn_name, request);
324 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
326 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
330 open_vconn(vconn_name, &vconn);
331 send_openflow_buffer(vconn, request);
334 struct ofpbuf *reply;
336 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
337 recv_xid = ((struct ofp_header *) reply->data)->xid;
338 if (send_xid == recv_xid) {
339 struct ofp_stats_reply *osr;
341 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
343 osr = ofpbuf_at(reply, 0, sizeof *osr);
344 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
346 VLOG_DBG("received reply with xid %08"PRIx32" "
347 "!= expected %08"PRIx32, recv_xid, send_xid);
349 ofpbuf_delete(reply);
355 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
357 struct ofpbuf *request;
358 alloc_stats_request(0, stats_type, &request);
359 dump_stats_transaction(vconn_name, request);
362 /* Sends 'request', which should be a request that only has a reply if an error
363 * occurs, and waits for it to succeed or fail. If an error does occur, prints
364 * it and exits with an error. */
366 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
368 struct ofpbuf *request, *reply;
370 LIST_FOR_EACH (request, list_node, requests) {
371 update_openflow_length(request);
374 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
375 "talking to %s", vconn_get_name(vconn));
377 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
380 ofpbuf_delete(reply);
383 /* Sends 'request', which should be a request that only has a reply if an error
384 * occurs, and waits for it to succeed or fail. If an error does occur, prints
385 * it and exits with an error. */
387 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
389 struct list requests;
391 list_init(&requests);
392 list_push_back(&requests, &request->list_node);
393 transact_multiple_noreply(vconn, &requests);
397 do_show(int argc OVS_UNUSED, char *argv[])
399 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
400 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
404 do_status(int argc, char *argv[])
406 struct nicira_header *request, *reply;
410 request = make_nxmsg(sizeof *request, NXT_STATUS_REQUEST, &b);
412 ofpbuf_put(b, argv[2], strlen(argv[2]));
413 update_openflow_length(b);
415 open_vconn(argv[1], &vconn);
416 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
419 if (b->size < sizeof *reply) {
420 ovs_fatal(0, "short reply (%zu bytes)", b->size);
423 if (reply->header.type != OFPT_VENDOR
424 || reply->vendor != ntohl(NX_VENDOR_ID)
425 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
426 ofp_print(stderr, b->data, b->size, verbosity + 2);
427 ovs_fatal(0, "bad reply");
430 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
434 do_dump_desc(int argc OVS_UNUSED, char *argv[])
436 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
440 do_dump_tables(int argc OVS_UNUSED, char *argv[])
442 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
445 /* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
446 * 'port_name' (which may be a port name or number), and copies it into
449 fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
450 struct ofp_phy_port *oppp)
452 struct ofpbuf *request, *reply;
453 struct ofp_switch_features *osf;
454 unsigned int port_no;
459 /* Try to interpret the argument as a port number. */
460 if (!str_to_uint(port_name, 10, &port_no)) {
464 /* Fetch the switch's ofp_switch_features. */
465 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
466 open_vconn(vconn_name, &vconn);
467 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
470 if (reply->size < sizeof *osf) {
471 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
472 vconn_name, reply->size);
474 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
476 for (port_idx = 0; port_idx < n_ports; port_idx++) {
477 const struct ofp_phy_port *opp = &osf->ports[port_idx];
479 if (port_no != UINT_MAX
480 ? htons(port_no) == opp->port_no
481 : !strncmp(opp->name, port_name, sizeof opp->name)) {
483 ofpbuf_delete(reply);
488 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
491 /* Returns the port number corresponding to 'port_name' (which may be a port
492 * name or number) within the switch 'vconn_name'. */
494 str_to_port_no(const char *vconn_name, const char *port_name)
496 unsigned int port_no;
498 if (str_to_uint(port_name, 10, &port_no)) {
501 struct ofp_phy_port opp;
503 fetch_ofp_phy_port(vconn_name, port_name, &opp);
504 return ntohs(opp.port_no);
509 try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
511 struct ofpbuf *sff, *reply;
513 sff = ofputil_make_set_flow_format(flow_format);
514 run(vconn_transact_noreply(vconn, sff, &reply),
515 "talking to %s", vconn_get_name(vconn));
517 char *s = ofp_to_string(reply->data, reply->size, 2);
518 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
519 vconn_get_name(vconn),
520 ofputil_flow_format_to_string(flow_format),
523 ofpbuf_delete(reply);
530 set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
532 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
533 transact_noreply(vconn, sff);
534 VLOG_DBG("%s: using user-specified flow format %s",
535 vconn_get_name(vconn),
536 ofputil_flow_format_to_string(flow_format));
539 static enum nx_flow_format
540 negotiate_highest_flow_format(struct vconn *vconn, const struct cls_rule *rule,
541 bool cookie_support, ovs_be64 cookie)
545 if (preferred_flow_format != -1) {
546 enum nx_flow_format min_format;
548 min_format = ofputil_min_flow_format(rule, cookie_support, cookie);
549 if (preferred_flow_format >= min_format) {
550 set_flow_format(vconn, preferred_flow_format);
551 return preferred_flow_format;
554 VLOG_WARN("%s: cannot use requested flow format %s for "
555 "specified flow", vconn_get_name(vconn),
556 ofputil_flow_format_to_string(min_format));
559 if (try_set_flow_format(vconn, NXFF_NXM)) {
560 flow_format = NXFF_NXM;
561 } else if (try_set_flow_format(vconn, NXFF_TUN_ID_FROM_COOKIE)) {
562 flow_format = NXFF_TUN_ID_FROM_COOKIE;
564 flow_format = NXFF_OPENFLOW10;
567 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
568 ofputil_flow_format_to_string(flow_format));
573 do_dump_flows__(int argc, char *argv[], bool aggregate)
575 enum nx_flow_format flow_format;
576 struct flow_stats_request fsr;
577 struct ofpbuf *request;
580 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
582 open_vconn(argv[1], &vconn);
583 flow_format = negotiate_highest_flow_format(vconn, &fsr.match, false, 0);
584 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
585 dump_stats_transaction(argv[1], request);
590 do_dump_flows(int argc, char *argv[])
592 return do_dump_flows__(argc, argv, false);
596 do_dump_aggregate(int argc, char *argv[])
598 return do_dump_flows__(argc, argv, true);
602 do_queue_stats(int argc, char *argv[])
604 struct ofp_queue_stats_request *req;
605 struct ofpbuf *request;
607 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
609 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
610 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
612 req->port_no = htons(OFPP_ALL);
614 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
615 req->queue_id = htonl(atoi(argv[3]));
617 req->queue_id = htonl(OFPQ_ALL);
620 memset(req->pad, 0, sizeof req->pad);
622 dump_stats_transaction(argv[1], request);
626 do_flow_mod__(int argc OVS_UNUSED, char *argv[], uint16_t command)
628 enum nx_flow_format flow_format;
629 struct list requests;
632 list_init(&requests);
633 flow_format = NXFF_OPENFLOW10;
634 parse_ofp_flow_mod_str(&requests, &flow_format, argc > 2 ? argv[2] : "",
637 open_vconn(argv[1], &vconn);
638 transact_multiple_noreply(vconn, &requests);
643 do_add_flow(int argc, char *argv[])
645 do_flow_mod__(argc, argv, OFPFC_ADD);
649 do_add_flows(int argc OVS_UNUSED, char *argv[])
651 enum nx_flow_format flow_format;
652 struct list requests;
656 file = fopen(argv[2], "r");
658 ovs_fatal(errno, "%s: open", argv[2]);
661 list_init(&requests);
662 flow_format = NXFF_OPENFLOW10;
664 open_vconn(argv[1], &vconn);
665 while (parse_ofp_add_flow_file(&requests, &flow_format, file)) {
666 transact_multiple_noreply(vconn, &requests);
674 do_mod_flows(int argc, char *argv[])
676 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
680 do_del_flows(int argc, char *argv[])
682 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
686 monitor_vconn(struct vconn *vconn)
690 run(vconn_recv_block(vconn, &b), "vconn_recv");
691 ofp_print(stderr, b->data, b->size, verbosity + 2);
697 do_monitor(int argc, char *argv[])
701 open_vconn(argv[1], &vconn);
703 int miss_send_len = atoi(argv[2]);
704 struct ofp_switch_config *osc;
707 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
708 osc->miss_send_len = htons(miss_send_len);
709 transact_noreply(vconn, buf);
711 monitor_vconn(vconn);
715 do_snoop(int argc OVS_UNUSED, char *argv[])
719 open_vconn__(argv[1], "snoop", &vconn);
720 monitor_vconn(vconn);
724 do_dump_ports(int argc, char *argv[])
726 struct ofp_port_stats_request *req;
727 struct ofpbuf *request;
730 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
731 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
732 req->port_no = htons(port);
733 dump_stats_transaction(argv[1], request);
737 do_probe(int argc OVS_UNUSED, char *argv[])
739 struct ofpbuf *request;
741 struct ofpbuf *reply;
743 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
744 open_vconn(argv[1], &vconn);
745 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
746 if (reply->size != sizeof(struct ofp_header)) {
747 ovs_fatal(0, "reply does not match request");
749 ofpbuf_delete(reply);
754 do_mod_port(int argc OVS_UNUSED, char *argv[])
756 struct ofp_port_mod *opm;
757 struct ofp_phy_port opp;
758 struct ofpbuf *request;
761 fetch_ofp_phy_port(argv[1], argv[2], &opp);
763 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
764 opm->port_no = opp.port_no;
765 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
766 opm->config = htonl(0);
767 opm->mask = htonl(0);
768 opm->advertise = htonl(0);
770 if (!strcasecmp(argv[3], "up")) {
771 opm->mask |= htonl(OFPPC_PORT_DOWN);
772 } else if (!strcasecmp(argv[3], "down")) {
773 opm->mask |= htonl(OFPPC_PORT_DOWN);
774 opm->config |= htonl(OFPPC_PORT_DOWN);
775 } else if (!strcasecmp(argv[3], "flood")) {
776 opm->mask |= htonl(OFPPC_NO_FLOOD);
777 } else if (!strcasecmp(argv[3], "noflood")) {
778 opm->mask |= htonl(OFPPC_NO_FLOOD);
779 opm->config |= htonl(OFPPC_NO_FLOOD);
781 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
784 open_vconn(argv[1], &vconn);
785 transact_noreply(vconn, request);
790 do_ping(int argc, char *argv[])
792 size_t max_payload = 65535 - sizeof(struct ofp_header);
793 unsigned int payload;
797 payload = argc > 2 ? atoi(argv[2]) : 64;
798 if (payload > max_payload) {
799 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
802 open_vconn(argv[1], &vconn);
803 for (i = 0; i < 10; i++) {
804 struct timeval start, end;
805 struct ofpbuf *request, *reply;
806 struct ofp_header *rq_hdr, *rpy_hdr;
808 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
809 OFPT_ECHO_REQUEST, &request);
810 random_bytes(rq_hdr + 1, payload);
812 gettimeofday(&start, NULL);
813 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
814 gettimeofday(&end, NULL);
816 rpy_hdr = reply->data;
817 if (reply->size != request->size
818 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
819 || rpy_hdr->xid != rq_hdr->xid
820 || rpy_hdr->type != OFPT_ECHO_REPLY) {
821 printf("Reply does not match request. Request:\n");
822 ofp_print(stdout, request, request->size, verbosity + 2);
824 ofp_print(stdout, reply, reply->size, verbosity + 2);
826 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
827 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
828 (1000*(double)(end.tv_sec - start.tv_sec))
829 + (.001*(end.tv_usec - start.tv_usec)));
830 ofpbuf_delete(request);
831 ofpbuf_delete(reply);
837 do_benchmark(int argc OVS_UNUSED, char *argv[])
839 size_t max_payload = 65535 - sizeof(struct ofp_header);
840 struct timeval start, end;
841 unsigned int payload_size, message_size;
847 payload_size = atoi(argv[2]);
848 if (payload_size > max_payload) {
849 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
851 message_size = sizeof(struct ofp_header) + payload_size;
853 count = atoi(argv[3]);
855 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
856 count, message_size, count * message_size);
858 open_vconn(argv[1], &vconn);
859 gettimeofday(&start, NULL);
860 for (i = 0; i < count; i++) {
861 struct ofpbuf *request, *reply;
862 struct ofp_header *rq_hdr;
864 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
865 memset(rq_hdr + 1, 0, payload_size);
866 run(vconn_transact(vconn, request, &reply), "transact");
867 ofpbuf_delete(reply);
869 gettimeofday(&end, NULL);
872 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
873 + (.001*(end.tv_usec - start.tv_usec)));
874 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
875 duration, count / (duration / 1000.0),
876 count * message_size / (duration / 1000.0));
880 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
885 /* Undocumented commands for unit testing. */
888 print_packet_list(struct list *packets)
890 struct ofpbuf *packet, *next;
892 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
893 ofp_print(stdout, packet->data, packet->size, verbosity);
894 list_remove(&packet->list_node);
895 ofpbuf_delete(packet);
899 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
900 * it back to stdout. */
902 do_parse_flow(int argc OVS_UNUSED, char *argv[])
904 enum nx_flow_format flow_format;
907 flow_format = NXFF_OPENFLOW10;
908 if (preferred_flow_format > 0) {
909 flow_format = preferred_flow_format;
913 parse_ofp_flow_mod_str(&packets, &flow_format, argv[1], OFPFC_ADD);
914 print_packet_list(&packets);
917 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
918 * add-flows) and prints each of the flows back to stdout. */
920 do_parse_flows(int argc OVS_UNUSED, char *argv[])
922 enum nx_flow_format flow_format;
926 file = fopen(argv[1], "r");
928 ovs_fatal(errno, "%s: open", argv[2]);
931 flow_format = NXFF_OPENFLOW10;
932 if (preferred_flow_format > 0) {
933 flow_format = preferred_flow_format;
937 while (parse_ofp_add_flow_file(&packets, &flow_format, file)) {
938 print_packet_list(&packets);
943 /* "parse-nx-match": reads a series of nx_match specifications as strings from
944 * stdin, does some internal fussing with them, and then prints them back as
945 * strings on stdout. */
947 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
952 while (!ds_get_line(&in, stdin)) {
953 struct ofpbuf nx_match;
954 struct cls_rule rule;
959 /* Delete comments, skip blank lines. */
965 if (strchr(s, '#')) {
966 *strchr(s, '#') = '\0';
968 if (s[strspn(s, " ")] == '\0') {
973 /* Convert string to nx_match. */
974 ofpbuf_init(&nx_match, 0);
975 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
977 /* Convert nx_match to cls_rule. */
978 error = nx_pull_match(&nx_match, match_len, 0, &rule);
982 /* Convert cls_rule back to nx_match. */
983 ofpbuf_uninit(&nx_match);
984 ofpbuf_init(&nx_match, 0);
985 match_len = nx_put_match(&nx_match, &rule);
987 /* Convert nx_match to string. */
988 out = nx_match_to_string(nx_match.data, match_len);
992 printf("nx_pull_match() returned error %x\n", error);
995 ofpbuf_uninit(&nx_match);
1000 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1001 * binary data, interpreting them as an OpenFlow message, and prints the
1002 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1004 do_ofp_print(int argc, char *argv[])
1006 struct ofpbuf packet;
1008 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1009 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1010 ovs_fatal(0, "trailing garbage following hex bytes");
1012 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1013 ofpbuf_uninit(&packet);
1016 static const struct command all_commands[] = {
1017 { "show", 1, 1, do_show },
1018 { "status", 1, 2, do_status },
1019 { "monitor", 1, 2, do_monitor },
1020 { "snoop", 1, 1, do_snoop },
1021 { "dump-desc", 1, 1, do_dump_desc },
1022 { "dump-tables", 1, 1, do_dump_tables },
1023 { "dump-flows", 1, 2, do_dump_flows },
1024 { "dump-aggregate", 1, 2, do_dump_aggregate },
1025 { "queue-stats", 1, 3, do_queue_stats },
1026 { "add-flow", 2, 2, do_add_flow },
1027 { "add-flows", 2, 2, do_add_flows },
1028 { "mod-flows", 2, 2, do_mod_flows },
1029 { "del-flows", 1, 2, do_del_flows },
1030 { "dump-ports", 1, 2, do_dump_ports },
1031 { "mod-port", 3, 3, do_mod_port },
1032 { "probe", 1, 1, do_probe },
1033 { "ping", 1, 2, do_ping },
1034 { "benchmark", 3, 3, do_benchmark },
1035 { "help", 0, INT_MAX, do_help },
1037 /* Undocumented commands for testing. */
1038 { "parse-flow", 1, 1, do_parse_flow },
1039 { "parse-flows", 1, 1, do_parse_flows },
1040 { "parse-nx-match", 0, 0, do_parse_nx_match },
1041 { "ofp-print", 1, 2, do_ofp_print },
1043 { NULL, 0, 0, NULL },