1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include <arpa/inet.h>
39 #include <netinet/in.h>
47 #include "command-line.h"
53 #include "openflow-netlink.h"
56 #include "socket-util.h"
58 #include "ofp-print.h"
63 #include "vconn-ssl.h"
66 #define THIS_MODULE VLM_dpctl
68 #define DEFAULT_IDLE_TIMEOUT 60
69 #define MAX_ADD_ACTS 5
71 static const char* ifconfigbin = "/sbin/ifconfig";
77 void (*handler)(int argc, char *argv[]);
80 static struct command all_commands[];
82 static void usage(void) NO_RETURN;
83 static void parse_options(int argc, char *argv[]);
85 int main(int argc, char *argv[])
89 set_program_name(argv[0]);
92 parse_options(argc, argv);
93 signal(SIGPIPE, SIG_IGN);
98 fatal(0, "missing command name; use --help for help");
100 for (p = all_commands; p->name != NULL; p++) {
101 if (!strcmp(p->name, argv[0])) {
102 int n_arg = argc - 1;
103 if (n_arg < p->min_args)
104 fatal(0, "'%s' command requires at least %d arguments",
105 p->name, p->min_args);
106 else if (n_arg > p->max_args)
107 fatal(0, "'%s' command takes at most %d arguments",
108 p->name, p->max_args);
110 p->handler(argc, argv);
115 fatal(0, "unknown command '%s'; use --help for help", argv[0]);
121 parse_options(int argc, char *argv[])
123 static struct option long_options[] = {
124 {"timeout", required_argument, 0, 't'},
125 {"verbose", optional_argument, 0, 'v'},
126 {"help", no_argument, 0, 'h'},
127 {"version", no_argument, 0, 'V'},
128 VCONN_SSL_LONG_OPTIONS
131 char *short_options = long_options_to_short_options(long_options);
134 unsigned long int timeout;
137 c = getopt_long(argc, argv, short_options, long_options, NULL);
144 timeout = strtoul(optarg, NULL, 10);
146 fatal(0, "value %s on -t or --timeout is not at least 1",
157 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
161 vlog_set_verbosity(optarg);
164 VCONN_SSL_OPTION_HANDLERS
179 printf("%s: OpenFlow switch management utility\n"
180 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
182 "\nFor local datapaths only:\n"
183 " adddp nl:DP_ID add a new local datapath DP_ID\n"
184 " deldp nl:DP_ID delete local datapath DP_ID\n"
185 " addif nl:DP_ID IFACE add IFACE as a port on DP_ID\n"
186 " delif nl:DP_ID IFACE delete IFACE as a port on DP_ID\n"
187 " monitor nl:DP_ID print packets received\n"
189 "\nFor local datapaths and remote switches:\n"
190 " show SWITCH show information\n"
191 " dump-version SWITCH print version information\n"
192 " dump-tables SWITCH print table stats\n"
193 " dump-ports SWITCH print port statistics\n"
194 " dump-flows SWITCH print all flow entries\n"
195 " dump-flows SWITCH FLOW print matching FLOWs\n"
196 " dump-aggregate SWITCH print aggregate flow statistics\n"
197 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
198 " add-flow SWITCH FLOW add flow described by FLOW\n"
199 " add-flows SWITCH FILE add flows from FILE\n"
200 " del-flows SWITCH FLOW delete matching FLOWs\n"
201 "\nFor local datapaths, remote switches, and controllers:\n"
202 " probe VCONN probe whether VCONN is up\n"
203 " ping VCONN [N] latency of N-byte echos\n"
204 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
205 "where each SWITCH is an active OpenFlow connection method.\n",
206 program_name, program_name);
207 vconn_usage(true, false);
208 printf("\nOptions:\n"
209 " -t, --timeout=SECS give up after SECS seconds\n"
210 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
211 " -v, --verbose set maximum verbosity level\n"
212 " -h, --help display this help message\n"
213 " -V, --version display version information\n");
217 static void run(int retval, const char *message, ...)
220 static void run(int retval, const char *message, ...)
225 fprintf(stderr, "%s: ", program_name);
226 va_start(args, message);
227 vfprintf(stderr, message, args);
230 fputs(": unexpected end of file\n", stderr);
232 fprintf(stderr, ": %s\n", strerror(retval));
240 /* Netlink-only commands. */
242 static int if_up(const char* intf)
245 snprintf(command, sizeof command, "%s %s up &> /dev/null",
247 return system(command);
250 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
252 if (strncmp(name, "nl:", 3)
254 || name[strspn(name + 3, "0123456789") + 3]) {
255 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
257 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
260 static void do_add_dp(int argc UNUSED, char *argv[])
263 open_nl_vconn(argv[1], false, &dp);
264 run(dpif_add_dp(&dp), "add_dp");
268 static void do_del_dp(int argc UNUSED, char *argv[])
271 open_nl_vconn(argv[1], false, &dp);
272 run(dpif_del_dp(&dp), "del_dp");
276 static void do_add_port(int argc UNUSED, char *argv[])
280 open_nl_vconn(argv[1], false, &dp);
281 run(dpif_add_port(&dp, argv[2]), "add_port");
285 static void do_del_port(int argc UNUSED, char *argv[])
288 open_nl_vconn(argv[1], false, &dp);
289 run(dpif_del_port(&dp, argv[2]), "del_port");
293 static void do_monitor(int argc UNUSED, char *argv[])
296 open_nl_vconn(argv[1], true, &dp);
299 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
300 ofp_print(stderr, b->data, b->size, 2);
304 #endif /* HAVE_NETLINK */
306 /* Generic commands. */
309 alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
311 struct ofp_stats_request *rq;
312 rq = make_openflow((offsetof(struct ofp_stats_request, body)
313 + body_len), OFPT_STATS_REQUEST, bufferp);
314 rq->type = htons(type);
315 rq->flags = htons(0);
320 send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
322 update_openflow_length(buffer);
323 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
327 dump_transaction(const char *vconn_name, struct buffer *request)
330 struct buffer *reply;
332 update_openflow_length(request);
333 run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
334 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
335 ofp_print(stdout, reply->data, reply->size, 1);
340 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
342 struct buffer *request;
343 make_openflow(sizeof(struct ofp_header), request_type, &request);
344 dump_transaction(vconn_name, request);
348 dump_stats_transaction(const char *vconn_name, struct buffer *request)
350 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
354 run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
355 send_openflow_buffer(vconn, request);
358 struct buffer *reply;
360 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
361 recv_xid = ((struct ofp_header *) reply->data)->xid;
362 if (send_xid == recv_xid) {
363 struct ofp_stats_reply *osr;
365 ofp_print(stdout, reply->data, reply->size, 1);
367 osr = buffer_at(reply, 0, sizeof *osr);
368 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
370 VLOG_DBG("received reply with xid %08"PRIx32" "
371 "!= expected %08"PRIx32, recv_xid, send_xid);
373 buffer_delete(reply);
379 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
381 struct buffer *request;
382 alloc_stats_request(0, stats_type, &request);
383 dump_stats_transaction(vconn_name, request);
387 do_show(int argc UNUSED, char *argv[])
389 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
390 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
395 do_dump_version(int argc, char *argv[])
397 dump_trivial_stats_transaction(argv[1], OFPST_VERSION);
401 do_dump_tables(int argc, char *argv[])
403 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
408 str_to_int(const char *str)
414 value = strtoul(str, &tail, 0);
415 if (errno == EINVAL || errno == ERANGE || *tail) {
416 fatal(0, "invalid numeric format %s", str);
422 str_to_mac(const char *str, uint8_t mac[6])
424 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
425 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
426 fatal(0, "invalid mac address %s", str);
431 str_to_ip(const char *str_, uint32_t *ip)
433 char *str = xstrdup(str_);
434 char *save_ptr = NULL;
435 const char *name, *netmask;
436 struct in_addr in_addr;
439 name = strtok_r(str, "//", &save_ptr);
440 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
442 fatal(0, "%s: could not convert to IP address", str);
444 *ip = in_addr.s_addr;
446 netmask = strtok_r(NULL, "//", &save_ptr);
449 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
450 &o[0], &o[1], &o[2], &o[3]) == 4) {
451 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
454 /* Find first 1-bit. */
455 for (i = 0; i < 32; i++) {
456 if (nm & (1u << i)) {
462 /* Verify that the rest of the bits are 1-bits. */
463 for (; i < 32; i++) {
464 if (!(nm & (1u << i))) {
465 fatal(0, "%s: %s is not a valid netmask", str, netmask);
469 int prefix = atoi(netmask);
470 if (prefix <= 0 || prefix > 32) {
471 fatal(0, "%s: network prefix bits not between 1 and 32", str);
473 n_wild = 32 - prefix;
484 str_to_action(char *str, struct ofp_action *action, int *n_actions)
488 int max_actions = *n_actions;
490 char *saveptr = NULL;
492 memset(action, 0, sizeof(*action) * max_actions);
493 for (i=0, act = strtok_r(str, ", \t\r\n", &saveptr);
494 i<max_actions && act;
495 i++, act = strtok_r(NULL, ", \t\r\n", &saveptr))
499 /* Arguments are separated by colons */
500 arg = strchr(act, ':');
506 if (!strcasecmp(act, "mod_vlan")) {
507 action[i].type = htons(OFPAT_SET_DL_VLAN);
509 if (!strcasecmp(arg, "strip")) {
510 action[i].arg.vlan_id = htons(OFP_VLAN_NONE);
512 action[i].arg.vlan_id = htons(str_to_int(arg));
514 } else if (!strcasecmp(act, "output")) {
515 port = str_to_int(arg);
516 } else if (!strcasecmp(act, "TABLE")) {
518 } else if (!strcasecmp(act, "NORMAL")) {
520 } else if (!strcasecmp(act, "FLOOD")) {
522 } else if (!strcasecmp(act, "ALL")) {
524 } else if (!strcasecmp(act, "CONTROLLER")) {
525 port = OFPP_CONTROLLER;
527 if (!strcasecmp(arg, "all")) {
528 action[i].arg.output.max_len= htons(0);
530 action[i].arg.output.max_len= htons(str_to_int(arg));
533 } else if (!strcasecmp(act, "LOCAL")) {
535 } else if (strspn(act, "0123456789") == strlen(act)) {
536 port = str_to_int(act);
538 fatal(0, "Unknown action: %s", act);
541 if (port != OFPP_MAX) {
542 action[i].type = htons(OFPAT_OUTPUT);
543 action[i].arg.output.port = htons(port);
557 parse_protocol(const char *name, const struct protocol **p_out)
559 static const struct protocol protocols[] = {
560 { "ip", ETH_TYPE_IP },
561 { "arp", ETH_TYPE_ARP },
562 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
563 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
564 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
566 const struct protocol *p;
568 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
569 if (!strcmp(p->name, name)) {
581 enum { F_U8, F_U16, F_MAC, F_IP } type;
582 size_t offset, shift;
586 parse_field(const char *name, const struct field **f_out)
588 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
589 static const struct field fields[] = {
590 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
591 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
592 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
593 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
594 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
595 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
596 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
597 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
598 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
599 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
600 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
601 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
603 const struct field *f;
605 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
606 if (!strcmp(f->name, name)) {
616 str_to_flow(char *string, struct ofp_match *match,
617 struct ofp_action *action, int *n_actions, uint8_t *table_idx,
618 uint16_t *priority, uint16_t *idle_timeout, uint16_t *hard_timeout)
628 *priority = OFP_DEFAULT_PRIORITY;
631 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
634 *hard_timeout = OFP_FLOW_PERMANENT;
637 char *act_str = strstr(string, "action");
639 fatal(0, "must specify an action");
643 act_str = strchr(act_str, '=');
645 fatal(0, "must specify an action");
650 str_to_action(act_str, action, n_actions);
652 memset(match, 0, sizeof *match);
653 wildcards = OFPFW_ALL;
654 for (name = strtok(string, "=, \t\r\n"); name;
655 name = strtok(NULL, "=, \t\r\n")) {
656 const struct protocol *p;
658 if (parse_protocol(name, &p)) {
659 wildcards &= ~OFPFW_DL_TYPE;
660 match->dl_type = htons(p->dl_type);
662 wildcards &= ~OFPFW_NW_PROTO;
663 match->nw_proto = p->nw_proto;
666 const struct field *f;
669 value = strtok(NULL, ", \t\r\n");
671 fatal(0, "field %s missing value", name);
674 if (table_idx && !strcmp(name, "table")) {
675 *table_idx = atoi(value);
676 } else if (priority && !strcmp(name, "priority")) {
677 *priority = atoi(value);
678 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
679 *idle_timeout = atoi(value);
680 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
681 *hard_timeout = atoi(value);
682 } else if (parse_field(name, &f)) {
683 void *data = (char *) match + f->offset;
684 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
685 wildcards |= f->wildcard;
687 wildcards &= ~f->wildcard;
688 if (f->type == F_U8) {
689 *(uint8_t *) data = str_to_int(value);
690 } else if (f->type == F_U16) {
691 *(uint16_t *) data = htons(str_to_int(value));
692 } else if (f->type == F_MAC) {
693 str_to_mac(value, data);
694 } else if (f->type == F_IP) {
695 wildcards |= str_to_ip(value, data) << f->shift;
701 fatal(0, "unknown keyword %s", name);
705 match->wildcards = htonl(wildcards);
708 static void do_dump_flows(int argc, char *argv[])
710 struct ofp_flow_stats_request *req;
711 struct buffer *request;
713 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
714 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
715 &req->table_id, NULL, NULL, NULL);
716 memset(req->pad, 0, sizeof req->pad);
718 dump_stats_transaction(argv[1], request);
721 static void do_dump_aggregate(int argc, char *argv[])
723 struct ofp_aggregate_stats_request *req;
724 struct buffer *request;
726 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
727 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
728 &req->table_id, NULL, NULL, NULL);
729 memset(req->pad, 0, sizeof req->pad);
731 dump_stats_transaction(argv[1], request);
734 static void do_add_flow(int argc, char *argv[])
737 struct buffer *buffer;
738 struct ofp_flow_mod *ofm;
739 uint16_t priority, idle_timeout, hard_timeout;
741 int n_actions = MAX_ADD_ACTS;
743 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
745 /* Parse and send. */
746 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
747 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
748 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
749 NULL, &priority, &idle_timeout, &hard_timeout);
750 ofm->command = htons(OFPFC_ADD);
751 ofm->idle_timeout = htons(idle_timeout);
752 ofm->hard_timeout = htons(hard_timeout);
753 ofm->buffer_id = htonl(UINT32_MAX);
754 ofm->priority = htons(priority);
755 ofm->reserved = htonl(0);
757 /* xxx Should we use the buffer library? */
758 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
760 send_openflow_buffer(vconn, buffer);
764 static void do_add_flows(int argc, char *argv[])
771 file = fopen(argv[2], "r");
773 fatal(errno, "%s: open", argv[2]);
776 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
777 while (fgets(line, sizeof line, file)) {
778 struct buffer *buffer;
779 struct ofp_flow_mod *ofm;
780 uint16_t priority, idle_timeout, hard_timeout;
782 int n_actions = MAX_ADD_ACTS;
786 /* Delete comments. */
787 comment = strchr(line, '#');
792 /* Drop empty lines. */
793 if (line[strspn(line, " \t\n")] == '\0') {
797 /* Parse and send. */
798 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
799 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
800 str_to_flow(line, &ofm->match, &ofm->actions[0], &n_actions,
801 NULL, &priority, &idle_timeout, &hard_timeout);
802 ofm->command = htons(OFPFC_ADD);
803 ofm->idle_timeout = htons(idle_timeout);
804 ofm->hard_timeout = htons(hard_timeout);
805 ofm->buffer_id = htonl(UINT32_MAX);
806 ofm->priority = htons(priority);
807 ofm->reserved = htonl(0);
809 /* xxx Should we use the buffer library? */
810 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
812 send_openflow_buffer(vconn, buffer);
818 static void do_del_flows(int argc, char *argv[])
823 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
824 struct buffer *buffer;
825 struct ofp_flow_mod *ofm;
829 /* Parse and send. */
831 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
832 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
833 &priority, NULL, NULL);
834 ofm->command = htons(OFPFC_DELETE);
835 ofm->idle_timeout = htons(0);
836 ofm->hard_timeout = htons(0);
837 ofm->buffer_id = htonl(UINT32_MAX);
838 ofm->priority = htons(priority);
839 ofm->reserved = htonl(0);
841 send_openflow_buffer(vconn, buffer);
847 do_dump_ports(int argc, char *argv[])
849 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
853 do_probe(int argc, char *argv[])
855 struct buffer *request;
857 struct buffer *reply;
859 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
860 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
861 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
862 if (reply->size != request->size) {
863 fatal(0, "reply does not match request");
865 buffer_delete(reply);
870 do_ping(int argc, char *argv[])
872 size_t max_payload = 65535 - sizeof(struct ofp_header);
873 unsigned int payload;
877 payload = argc > 2 ? atoi(argv[2]) : 64;
878 if (payload > max_payload) {
879 fatal(0, "payload must be between 0 and %zu bytes", max_payload);
882 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
883 for (i = 0; i < 10; i++) {
884 struct timeval start, end;
885 struct buffer *request, *reply;
886 struct ofp_header *rq_hdr, *rpy_hdr;
888 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
889 OFPT_ECHO_REQUEST, &request);
890 random_bytes(rq_hdr + 1, payload);
892 gettimeofday(&start, NULL);
893 run(vconn_transact(vconn, buffer_clone(request), &reply), "transact");
894 gettimeofday(&end, NULL);
896 rpy_hdr = reply->data;
897 if (reply->size != request->size
898 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
899 || rpy_hdr->xid != rq_hdr->xid
900 || rpy_hdr->type != OFPT_ECHO_REPLY) {
901 printf("Reply does not match request. Request:\n");
902 ofp_print(stdout, request, request->size, 2);
904 ofp_print(stdout, reply, reply->size, 2);
906 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
907 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
908 (1000*(double)(end.tv_sec - start.tv_sec))
909 + (.001*(end.tv_usec - start.tv_usec)));
910 buffer_delete(request);
911 buffer_delete(reply);
917 do_benchmark(int argc, char *argv[])
919 size_t max_payload = 65535 - sizeof(struct ofp_header);
920 struct timeval start, end;
921 unsigned int payload_size, message_size;
927 payload_size = atoi(argv[2]);
928 if (payload_size > max_payload) {
929 fatal(0, "payload must be between 0 and %zu bytes", max_payload);
931 message_size = sizeof(struct ofp_header) + payload_size;
933 count = atoi(argv[3]);
935 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
936 count, message_size, count * message_size);
938 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
939 gettimeofday(&start, NULL);
940 for (i = 0; i < count; i++) {
941 struct buffer *request, *reply;
942 struct ofp_header *rq_hdr;
944 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
945 memset(rq_hdr + 1, 0, payload_size);
946 run(vconn_transact(vconn, request, &reply), "transact");
947 buffer_delete(reply);
949 gettimeofday(&end, NULL);
952 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
953 + (.001*(end.tv_usec - start.tv_usec)));
954 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
955 duration, count / (duration / 1000.0),
956 count * message_size / (duration / 1000.0));
959 static void do_help(int argc UNUSED, char *argv[] UNUSED)
964 static struct command all_commands[] = {
966 { "adddp", 1, 1, do_add_dp },
967 { "deldp", 1, 1, do_del_dp },
968 { "addif", 2, 2, do_add_port },
969 { "delif", 2, 2, do_del_port },
972 { "show", 1, 1, do_show },
974 { "help", 0, INT_MAX, do_help },
975 { "monitor", 1, 1, do_monitor },
976 { "dump-version", 1, 1, do_dump_version },
977 { "dump-tables", 1, 1, do_dump_tables },
978 { "dump-flows", 1, 2, do_dump_flows },
979 { "dump-aggregate", 1, 2, do_dump_aggregate },
980 { "add-flow", 2, 2, do_add_flow },
981 { "add-flows", 2, 2, do_add_flows },
982 { "del-flows", 1, 2, do_del_flows },
983 { "dump-ports", 1, 1, do_dump_ports },
984 { "probe", 1, 1, do_probe },
985 { "ping", 1, 2, do_ping },
986 { "benchmark", 3, 3, do_benchmark },
987 { NULL, 0, 0, NULL },