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>
50 #include "openflow-netlink.h"
54 #include "command-line.h"
57 #include "ofp-print.h"
61 #include "socket-util.h"
64 #include "vconn-ssl.h"
68 #define THIS_MODULE VLM_dpctl
70 #define DEFAULT_IDLE_TIMEOUT 60
71 #define MAX_ADD_ACTS 5
73 #define MOD_PORT_CMD_UP "up"
74 #define MOD_PORT_CMD_DOWN "down"
75 #define MOD_PORT_CMD_FLOOD "flood"
76 #define MOD_PORT_CMD_NOFLOOD "noflood"
82 void (*handler)(int argc, char *argv[]);
85 static struct command all_commands[];
87 static void usage(void) NO_RETURN;
88 static void parse_options(int argc, char *argv[]);
90 int main(int argc, char *argv[])
94 set_program_name(argv[0]);
97 parse_options(argc, argv);
98 signal(SIGPIPE, SIG_IGN);
103 fatal(0, "missing command name; use --help for help");
105 for (p = all_commands; p->name != NULL; p++) {
106 if (!strcmp(p->name, argv[0])) {
107 int n_arg = argc - 1;
108 if (n_arg < p->min_args)
109 fatal(0, "'%s' command requires at least %d arguments",
110 p->name, p->min_args);
111 else if (n_arg > p->max_args)
112 fatal(0, "'%s' command takes at most %d arguments",
113 p->name, p->max_args);
115 p->handler(argc, argv);
120 fatal(0, "unknown command '%s'; use --help for help", argv[0]);
126 parse_options(int argc, char *argv[])
128 static struct option long_options[] = {
129 {"timeout", required_argument, 0, 't'},
130 {"verbose", optional_argument, 0, 'v'},
131 {"help", no_argument, 0, 'h'},
132 {"version", no_argument, 0, 'V'},
133 VCONN_SSL_LONG_OPTIONS
136 char *short_options = long_options_to_short_options(long_options);
139 unsigned long int timeout;
142 c = getopt_long(argc, argv, short_options, long_options, NULL);
149 timeout = strtoul(optarg, NULL, 10);
151 fatal(0, "value %s on -t or --timeout is not at least 1",
162 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
166 vlog_set_verbosity(optarg);
169 VCONN_SSL_OPTION_HANDLERS
184 printf("%s: OpenFlow switch management utility\n"
185 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
187 "\nFor local datapaths only:\n"
188 " adddp nl:DP_ID add a new local datapath DP_ID\n"
189 " deldp nl:DP_ID delete local datapath DP_ID\n"
190 " addif nl:DP_ID IFACE... add each IFACE as a port on DP_ID\n"
191 " delif nl:DP_ID IFACE... delete each IFACE from DP_ID\n"
192 " monitor nl:DP_ID print packets received\n"
194 "\nFor local datapaths and remote switches:\n"
195 " show SWITCH show basic information\n"
196 " status SWITCH [KEY] report statistics (about KEY)\n"
197 " dump-version SWITCH print version information\n"
198 " dump-tables SWITCH print table stats\n"
199 " mod-port SWITCH IFACE ACT modify port behavior\n"
200 " dump-ports SWITCH print port statistics\n"
201 " dump-flows SWITCH print all flow entries\n"
202 " dump-flows SWITCH FLOW print matching FLOWs\n"
203 " dump-aggregate SWITCH print aggregate flow statistics\n"
204 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
205 " add-flow SWITCH FLOW add flow described by FLOW\n"
206 " add-flows SWITCH FILE add flows from FILE\n"
207 " del-flows SWITCH FLOW delete matching FLOWs\n"
208 "\nFor local datapaths, remote switches, and controllers:\n"
209 " probe VCONN probe whether VCONN is up\n"
210 " ping VCONN [N] latency of N-byte echos\n"
211 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
212 "where each SWITCH is an active OpenFlow connection method.\n",
213 program_name, program_name);
214 vconn_usage(true, false);
215 printf("\nOptions:\n"
216 " -t, --timeout=SECS give up after SECS seconds\n"
217 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
218 " -v, --verbose set maximum verbosity level\n"
219 " -h, --help display this help message\n"
220 " -V, --version display version information\n");
224 static void run(int retval, const char *message, ...)
227 static void run(int retval, const char *message, ...)
232 fprintf(stderr, "%s: ", program_name);
233 va_start(args, message);
234 vfprintf(stderr, message, args);
237 fputs(": unexpected end of file\n", stderr);
239 fprintf(stderr, ": %s\n", strerror(retval));
247 /* Netlink-only commands. */
249 static int if_up(const char *netdev_name)
251 struct netdev *netdev;
254 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
256 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
257 netdev_close(netdev);
262 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
264 if (strncmp(name, "nl:", 3)
266 || name[strspn(name + 3, "0123456789") + 3]) {
267 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
269 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
272 static void do_add_dp(int argc UNUSED, char *argv[])
275 open_nl_vconn(argv[1], false, &dp);
276 run(dpif_add_dp(&dp), "add_dp");
280 static void do_del_dp(int argc UNUSED, char *argv[])
283 open_nl_vconn(argv[1], false, &dp);
284 run(dpif_del_dp(&dp), "del_dp");
288 static void add_del_ports(int argc UNUSED, char *argv[],
289 int (*function)(struct dpif *, const char *netdev),
290 const char *operation, const char *preposition)
293 bool failure = false;
296 open_nl_vconn(argv[1], false, &dp);
297 for (i = 2; i < argc; i++) {
298 int retval = function(&dp, argv[i]);
300 error(retval, "failed to %s %s %s %s",
301 operation, argv[i], preposition, argv[1]);
311 static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
313 int retval = if_up(netdev);
314 return retval ? retval : dpif_add_port(dpif, netdev);
317 static void do_add_port(int argc UNUSED, char *argv[])
319 add_del_ports(argc, argv, ifup_and_add_port, "add", "to");
322 static void do_del_port(int argc UNUSED, char *argv[])
324 add_del_ports(argc, argv, dpif_del_port, "remove", "from");
327 static void do_monitor(int argc UNUSED, char *argv[])
330 open_nl_vconn(argv[1], true, &dp);
333 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
334 ofp_print(stderr, b->data, b->size, 2);
338 #endif /* HAVE_NETLINK */
340 /* Generic commands. */
343 alloc_stats_request(size_t body_len, uint16_t type, struct buffer **bufferp)
345 struct ofp_stats_request *rq;
346 rq = make_openflow((offsetof(struct ofp_stats_request, body)
347 + body_len), OFPT_STATS_REQUEST, bufferp);
348 rq->type = htons(type);
349 rq->flags = htons(0);
354 send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
356 update_openflow_length(buffer);
357 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
361 dump_transaction(const char *vconn_name, struct buffer *request)
364 struct buffer *reply;
366 update_openflow_length(request);
367 run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
368 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
369 ofp_print(stdout, reply->data, reply->size, 1);
374 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
376 struct buffer *request;
377 make_openflow(sizeof(struct ofp_header), request_type, &request);
378 dump_transaction(vconn_name, request);
382 dump_stats_transaction(const char *vconn_name, struct buffer *request)
384 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
388 run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
389 send_openflow_buffer(vconn, request);
392 struct buffer *reply;
394 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
395 recv_xid = ((struct ofp_header *) reply->data)->xid;
396 if (send_xid == recv_xid) {
397 struct ofp_stats_reply *osr;
399 ofp_print(stdout, reply->data, reply->size, 1);
401 osr = buffer_at(reply, 0, sizeof *osr);
402 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
404 VLOG_DBG("received reply with xid %08"PRIx32" "
405 "!= expected %08"PRIx32, recv_xid, send_xid);
407 buffer_delete(reply);
413 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
415 struct buffer *request;
416 alloc_stats_request(0, stats_type, &request);
417 dump_stats_transaction(vconn_name, request);
421 do_show(int argc UNUSED, char *argv[])
423 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
424 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
428 do_status(int argc, char *argv[])
430 struct buffer *request;
431 alloc_stats_request(0, OFPST_SWITCH, &request);
433 buffer_put(request, argv[2], strlen(argv[2]));
435 dump_stats_transaction(argv[1], request);
439 do_dump_version(int argc, char *argv[])
441 dump_trivial_stats_transaction(argv[1], OFPST_VERSION);
445 do_dump_tables(int argc, char *argv[])
447 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
452 str_to_int(const char *str)
458 value = strtoul(str, &tail, 0);
459 if (errno == EINVAL || errno == ERANGE || *tail) {
460 fatal(0, "invalid numeric format %s", str);
466 str_to_mac(const char *str, uint8_t mac[6])
468 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
469 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
470 fatal(0, "invalid mac address %s", str);
475 str_to_ip(const char *str_, uint32_t *ip)
477 char *str = xstrdup(str_);
478 char *save_ptr = NULL;
479 const char *name, *netmask;
480 struct in_addr in_addr;
483 name = strtok_r(str, "//", &save_ptr);
484 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
486 fatal(0, "%s: could not convert to IP address", str);
488 *ip = in_addr.s_addr;
490 netmask = strtok_r(NULL, "//", &save_ptr);
493 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
494 &o[0], &o[1], &o[2], &o[3]) == 4) {
495 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
498 /* Find first 1-bit. */
499 for (i = 0; i < 32; i++) {
500 if (nm & (1u << i)) {
506 /* Verify that the rest of the bits are 1-bits. */
507 for (; i < 32; i++) {
508 if (!(nm & (1u << i))) {
509 fatal(0, "%s: %s is not a valid netmask", str, netmask);
513 int prefix = atoi(netmask);
514 if (prefix <= 0 || prefix > 32) {
515 fatal(0, "%s: network prefix bits not between 1 and 32", str);
517 n_wild = 32 - prefix;
528 str_to_action(char *str, struct ofp_action *action, int *n_actions)
532 int max_actions = *n_actions;
534 char *saveptr = NULL;
536 memset(action, 0, sizeof(*action) * max_actions);
537 for (i=0, act = strtok_r(str, ", \t\r\n", &saveptr);
538 i<max_actions && act;
539 i++, act = strtok_r(NULL, ", \t\r\n", &saveptr))
543 /* Arguments are separated by colons */
544 arg = strchr(act, ':');
550 if (!strcasecmp(act, "mod_vlan")) {
551 action[i].type = htons(OFPAT_SET_DL_VLAN);
553 if (!strcasecmp(arg, "strip")) {
554 action[i].arg.vlan_id = htons(OFP_VLAN_NONE);
556 action[i].arg.vlan_id = htons(str_to_int(arg));
558 } else if (!strcasecmp(act, "output")) {
559 port = str_to_int(arg);
560 } else if (!strcasecmp(act, "TABLE")) {
562 } else if (!strcasecmp(act, "NORMAL")) {
564 } else if (!strcasecmp(act, "FLOOD")) {
566 } else if (!strcasecmp(act, "ALL")) {
568 } else if (!strcasecmp(act, "CONTROLLER")) {
569 port = OFPP_CONTROLLER;
571 if (!strcasecmp(arg, "all")) {
572 action[i].arg.output.max_len= htons(0);
574 action[i].arg.output.max_len= htons(str_to_int(arg));
577 } else if (!strcasecmp(act, "LOCAL")) {
579 } else if (strspn(act, "0123456789") == strlen(act)) {
580 port = str_to_int(act);
582 fatal(0, "Unknown action: %s", act);
585 if (port != OFPP_MAX) {
586 action[i].type = htons(OFPAT_OUTPUT);
587 action[i].arg.output.port = htons(port);
601 parse_protocol(const char *name, const struct protocol **p_out)
603 static const struct protocol protocols[] = {
604 { "ip", ETH_TYPE_IP },
605 { "arp", ETH_TYPE_ARP },
606 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
607 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
608 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
610 const struct protocol *p;
612 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
613 if (!strcmp(p->name, name)) {
625 enum { F_U8, F_U16, F_MAC, F_IP } type;
626 size_t offset, shift;
630 parse_field(const char *name, const struct field **f_out)
632 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
633 static const struct field fields[] = {
634 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
635 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
636 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
637 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
638 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
639 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
640 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
641 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
642 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
643 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
644 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
645 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
647 const struct field *f;
649 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
650 if (!strcmp(f->name, name)) {
660 str_to_flow(char *string, struct ofp_match *match,
661 struct ofp_action *action, int *n_actions, uint8_t *table_idx,
662 uint16_t *priority, uint16_t *idle_timeout, uint16_t *hard_timeout)
672 *priority = OFP_DEFAULT_PRIORITY;
675 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
678 *hard_timeout = OFP_FLOW_PERMANENT;
681 char *act_str = strstr(string, "action");
683 fatal(0, "must specify an action");
687 act_str = strchr(act_str, '=');
689 fatal(0, "must specify an action");
694 str_to_action(act_str, action, n_actions);
696 memset(match, 0, sizeof *match);
697 wildcards = OFPFW_ALL;
698 for (name = strtok(string, "=, \t\r\n"); name;
699 name = strtok(NULL, "=, \t\r\n")) {
700 const struct protocol *p;
702 if (parse_protocol(name, &p)) {
703 wildcards &= ~OFPFW_DL_TYPE;
704 match->dl_type = htons(p->dl_type);
706 wildcards &= ~OFPFW_NW_PROTO;
707 match->nw_proto = p->nw_proto;
710 const struct field *f;
713 value = strtok(NULL, ", \t\r\n");
715 fatal(0, "field %s missing value", name);
718 if (table_idx && !strcmp(name, "table")) {
719 *table_idx = atoi(value);
720 } else if (priority && !strcmp(name, "priority")) {
721 *priority = atoi(value);
722 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
723 *idle_timeout = atoi(value);
724 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
725 *hard_timeout = atoi(value);
726 } else if (parse_field(name, &f)) {
727 void *data = (char *) match + f->offset;
728 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
729 wildcards |= f->wildcard;
731 wildcards &= ~f->wildcard;
732 if (f->type == F_U8) {
733 *(uint8_t *) data = str_to_int(value);
734 } else if (f->type == F_U16) {
735 *(uint16_t *) data = htons(str_to_int(value));
736 } else if (f->type == F_MAC) {
737 str_to_mac(value, data);
738 } else if (f->type == F_IP) {
739 wildcards |= str_to_ip(value, data) << f->shift;
745 fatal(0, "unknown keyword %s", name);
749 match->wildcards = htonl(wildcards);
752 static void do_dump_flows(int argc, char *argv[])
754 struct ofp_flow_stats_request *req;
755 struct buffer *request;
757 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
758 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
759 &req->table_id, NULL, NULL, NULL);
760 memset(req->pad, 0, sizeof req->pad);
762 dump_stats_transaction(argv[1], request);
765 static void do_dump_aggregate(int argc, char *argv[])
767 struct ofp_aggregate_stats_request *req;
768 struct buffer *request;
770 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
771 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
772 &req->table_id, NULL, NULL, NULL);
773 memset(req->pad, 0, sizeof req->pad);
775 dump_stats_transaction(argv[1], request);
778 static void do_add_flow(int argc, char *argv[])
781 struct buffer *buffer;
782 struct ofp_flow_mod *ofm;
783 uint16_t priority, idle_timeout, hard_timeout;
785 int n_actions = MAX_ADD_ACTS;
787 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
789 /* Parse and send. */
790 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
791 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
792 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
793 NULL, &priority, &idle_timeout, &hard_timeout);
794 ofm->command = htons(OFPFC_ADD);
795 ofm->idle_timeout = htons(idle_timeout);
796 ofm->hard_timeout = htons(hard_timeout);
797 ofm->buffer_id = htonl(UINT32_MAX);
798 ofm->priority = htons(priority);
799 ofm->reserved = htonl(0);
801 /* xxx Should we use the buffer library? */
802 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
804 send_openflow_buffer(vconn, buffer);
808 static void do_add_flows(int argc, char *argv[])
815 file = fopen(argv[2], "r");
817 fatal(errno, "%s: open", argv[2]);
820 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
821 while (fgets(line, sizeof line, file)) {
822 struct buffer *buffer;
823 struct ofp_flow_mod *ofm;
824 uint16_t priority, idle_timeout, hard_timeout;
826 int n_actions = MAX_ADD_ACTS;
830 /* Delete comments. */
831 comment = strchr(line, '#');
836 /* Drop empty lines. */
837 if (line[strspn(line, " \t\n")] == '\0') {
841 /* Parse and send. */
842 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
843 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
844 str_to_flow(line, &ofm->match, &ofm->actions[0], &n_actions,
845 NULL, &priority, &idle_timeout, &hard_timeout);
846 ofm->command = htons(OFPFC_ADD);
847 ofm->idle_timeout = htons(idle_timeout);
848 ofm->hard_timeout = htons(hard_timeout);
849 ofm->buffer_id = htonl(UINT32_MAX);
850 ofm->priority = htons(priority);
851 ofm->reserved = htonl(0);
853 /* xxx Should we use the buffer library? */
854 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
856 send_openflow_buffer(vconn, buffer);
862 static void do_del_flows(int argc, char *argv[])
867 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
868 struct buffer *buffer;
869 struct ofp_flow_mod *ofm;
873 /* Parse and send. */
875 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
876 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
877 &priority, NULL, NULL);
878 ofm->command = htons(OFPFC_DELETE);
879 ofm->idle_timeout = htons(0);
880 ofm->hard_timeout = htons(0);
881 ofm->buffer_id = htonl(UINT32_MAX);
882 ofm->priority = htons(priority);
883 ofm->reserved = htonl(0);
885 send_openflow_buffer(vconn, buffer);
891 do_dump_ports(int argc, char *argv[])
893 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
897 do_probe(int argc, char *argv[])
899 struct buffer *request;
901 struct buffer *reply;
903 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
904 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
905 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
906 if (reply->size != request->size) {
907 fatal(0, "reply does not match request");
909 buffer_delete(reply);
914 do_mod_port(int argc, char *argv[])
916 struct buffer *request, *reply;
917 struct ofp_switch_features *osf;
918 struct ofp_port_mod *opm;
926 /* Check if the argument is a port index. Otherwise, treat it as
928 port_no = strtol(argv[2], &endptr, 10);
929 if (port_no == 0 && endptr == argv[2]) {
933 /* Send a "Features Request" to get the information we need in order
934 * to modify the port. */
935 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
936 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
937 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
940 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
942 for (port_idx = 0; port_idx < n_ports; port_idx++) {
944 /* Check argument as a port index */
945 if (osf->ports[port_idx].port_no == htons(port_no)) {
949 /* Check argument as an interface name */
950 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
951 sizeof osf->ports[0].name)) {
957 if (port_idx == n_ports) {
958 fatal(0, "couldn't find monitored port: %s", argv[2]);
961 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
962 memcpy(&opm->desc, &osf->ports[port_idx], sizeof osf->ports[0]);
966 printf("modifying port: %s\n", osf->ports[port_idx].name);
968 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
969 opm->mask |= htonl(OFPPFL_PORT_DOWN);
970 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
971 sizeof MOD_PORT_CMD_DOWN)) {
972 opm->mask |= htonl(OFPPFL_PORT_DOWN);
973 opm->desc.flags |= htonl(OFPPFL_PORT_DOWN);
974 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
975 sizeof MOD_PORT_CMD_FLOOD)) {
976 opm->mask |= htonl(OFPPFL_NO_FLOOD);
977 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
978 sizeof MOD_PORT_CMD_NOFLOOD)) {
979 opm->mask |= htonl(OFPPFL_NO_FLOOD);
980 opm->desc.flags |= htonl(OFPPFL_NO_FLOOD);
982 fatal(0, "unknown mod-port command '%s'", argv[3]);
985 send_openflow_buffer(vconn, request);
987 buffer_delete(reply);
992 do_ping(int argc, char *argv[])
994 size_t max_payload = 65535 - sizeof(struct ofp_header);
995 unsigned int payload;
999 payload = argc > 2 ? atoi(argv[2]) : 64;
1000 if (payload > max_payload) {
1001 fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1004 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
1005 for (i = 0; i < 10; i++) {
1006 struct timeval start, end;
1007 struct buffer *request, *reply;
1008 struct ofp_header *rq_hdr, *rpy_hdr;
1010 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1011 OFPT_ECHO_REQUEST, &request);
1012 random_bytes(rq_hdr + 1, payload);
1014 gettimeofday(&start, NULL);
1015 run(vconn_transact(vconn, buffer_clone(request), &reply), "transact");
1016 gettimeofday(&end, NULL);
1018 rpy_hdr = reply->data;
1019 if (reply->size != request->size
1020 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1021 || rpy_hdr->xid != rq_hdr->xid
1022 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1023 printf("Reply does not match request. Request:\n");
1024 ofp_print(stdout, request, request->size, 2);
1026 ofp_print(stdout, reply, reply->size, 2);
1028 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1029 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1030 (1000*(double)(end.tv_sec - start.tv_sec))
1031 + (.001*(end.tv_usec - start.tv_usec)));
1032 buffer_delete(request);
1033 buffer_delete(reply);
1039 do_benchmark(int argc, char *argv[])
1041 size_t max_payload = 65535 - sizeof(struct ofp_header);
1042 struct timeval start, end;
1043 unsigned int payload_size, message_size;
1044 struct vconn *vconn;
1049 payload_size = atoi(argv[2]);
1050 if (payload_size > max_payload) {
1051 fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1053 message_size = sizeof(struct ofp_header) + payload_size;
1055 count = atoi(argv[3]);
1057 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1058 count, message_size, count * message_size);
1060 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
1061 gettimeofday(&start, NULL);
1062 for (i = 0; i < count; i++) {
1063 struct buffer *request, *reply;
1064 struct ofp_header *rq_hdr;
1066 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1067 memset(rq_hdr + 1, 0, payload_size);
1068 run(vconn_transact(vconn, request, &reply), "transact");
1069 buffer_delete(reply);
1071 gettimeofday(&end, NULL);
1074 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1075 + (.001*(end.tv_usec - start.tv_usec)));
1076 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1077 duration, count / (duration / 1000.0),
1078 count * message_size / (duration / 1000.0));
1081 static void do_help(int argc UNUSED, char *argv[] UNUSED)
1086 static struct command all_commands[] = {
1088 { "adddp", 1, 1, do_add_dp },
1089 { "deldp", 1, 1, do_del_dp },
1090 { "addif", 2, INT_MAX, do_add_port },
1091 { "delif", 2, INT_MAX, do_del_port },
1094 { "show", 1, 1, do_show },
1095 { "status", 1, 2, do_status },
1097 { "help", 0, INT_MAX, do_help },
1098 { "monitor", 1, 1, do_monitor },
1099 { "dump-version", 1, 1, do_dump_version },
1100 { "dump-tables", 1, 1, do_dump_tables },
1101 { "dump-flows", 1, 2, do_dump_flows },
1102 { "dump-aggregate", 1, 2, do_dump_aggregate },
1103 { "add-flow", 2, 2, do_add_flow },
1104 { "add-flows", 2, 2, do_add_flows },
1105 { "del-flows", 1, 2, do_del_flows },
1106 { "dump-ports", 1, 1, do_dump_ports },
1107 { "mod-port", 3, 3, do_mod_port },
1108 { "probe", 1, 1, do_probe },
1109 { "ping", 1, 2, do_ping },
1110 { "benchmark", 3, 3, do_benchmark },
1111 { NULL, 0, 0, NULL },