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"
53 #include "command-line.h"
56 #include "nicira-ext.h"
57 #include "ofp-print.h"
62 #include "socket-util.h"
65 #include "vconn-ssl.h"
69 #define THIS_MODULE VLM_dpctl
71 #define DEFAULT_IDLE_TIMEOUT 60
72 #define MAX_ADD_ACTS 5
74 #define MOD_PORT_CMD_UP "up"
75 #define MOD_PORT_CMD_DOWN "down"
76 #define MOD_PORT_CMD_FLOOD "flood"
77 #define MOD_PORT_CMD_NOFLOOD "noflood"
80 /* Settings that may be configured by the user. */
82 bool strict; /* Use strict matching for flow mod commands */
89 void (*handler)(const struct settings *, int argc, char *argv[]);
92 static struct command all_commands[];
94 static void usage(void) NO_RETURN;
95 static void parse_options(int argc, char *argv[], struct settings *);
97 int main(int argc, char *argv[])
102 set_program_name(argv[0]);
105 parse_options(argc, argv, &s);
106 signal(SIGPIPE, SIG_IGN);
111 ofp_fatal(0, "missing command name; use --help for help");
113 for (p = all_commands; p->name != NULL; p++) {
114 if (!strcmp(p->name, argv[0])) {
115 int n_arg = argc - 1;
116 if (n_arg < p->min_args)
117 ofp_fatal(0, "'%s' command requires at least %d arguments",
118 p->name, p->min_args);
119 else if (n_arg > p->max_args)
120 ofp_fatal(0, "'%s' command takes at most %d arguments",
121 p->name, p->max_args);
123 p->handler(&s, argc, argv);
128 ofp_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
134 parse_options(int argc, char *argv[], struct settings *s)
137 OPT_STRICT = UCHAR_MAX + 1
139 static struct option long_options[] = {
140 {"timeout", required_argument, 0, 't'},
141 {"verbose", optional_argument, 0, 'v'},
142 {"strict", no_argument, 0, OPT_STRICT},
143 {"help", no_argument, 0, 'h'},
144 {"version", no_argument, 0, 'V'},
145 VCONN_SSL_LONG_OPTIONS
148 char *short_options = long_options_to_short_options(long_options);
150 /* Set defaults that we can figure out before parsing options. */
154 unsigned long int timeout;
157 c = getopt_long(argc, argv, short_options, long_options, NULL);
164 timeout = strtoul(optarg, NULL, 10);
166 ofp_fatal(0, "value %s on -t or --timeout is not at least 1",
177 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
181 vlog_set_verbosity(optarg);
188 VCONN_SSL_OPTION_HANDLERS
203 printf("%s: OpenFlow switch management utility\n"
204 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
206 "\nFor local datapaths only:\n"
207 " adddp nl:DP_ID add a new local datapath DP_ID\n"
208 " deldp nl:DP_ID delete local datapath DP_ID\n"
209 " addif nl:DP_ID IFACE... add each IFACE as a port on DP_ID\n"
210 " delif nl:DP_ID IFACE... delete each IFACE from DP_ID\n"
212 "\nFor local datapaths and remote switches:\n"
213 " show SWITCH show basic information\n"
214 " status SWITCH [KEY] report statistics (about KEY)\n"
215 " dump-desc SWITCH print switch description\n"
216 " dump-tables SWITCH print table stats\n"
217 " mod-port SWITCH IFACE ACT modify port behavior\n"
218 " dump-ports SWITCH print port statistics\n"
219 " dump-flows SWITCH print all flow entries\n"
220 " dump-flows SWITCH FLOW print matching FLOWs\n"
221 " dump-aggregate SWITCH print aggregate flow statistics\n"
222 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
223 " add-flow SWITCH FLOW add flow described by FLOW\n"
224 " add-flows SWITCH FILE add flows from FILE\n"
225 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
226 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
227 " monitor SWITCH print packets received from SWITCH\n"
228 "\nFor local datapaths, remote switches, and controllers:\n"
229 " probe VCONN probe whether VCONN is up\n"
230 " ping VCONN [N] latency of N-byte echos\n"
231 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
232 "where each SWITCH is an active OpenFlow connection method.\n",
233 program_name, program_name);
234 vconn_usage(true, false, false);
235 printf("\nOptions:\n"
236 " --strict use strict match for flow commands\n"
237 " -t, --timeout=SECS give up after SECS seconds\n"
238 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
239 " -v, --verbose set maximum verbosity level\n"
240 " -h, --help display this help message\n"
241 " -V, --version display version information\n");
245 static void run(int retval, const char *message, ...)
248 static void run(int retval, const char *message, ...)
253 fprintf(stderr, "%s: ", program_name);
254 va_start(args, message);
255 vfprintf(stderr, message, args);
258 fputs(": unexpected end of file\n", stderr);
260 fprintf(stderr, ": %s\n", strerror(retval));
268 /* Netlink-only commands. */
270 static int if_up(const char *netdev_name)
272 struct netdev *netdev;
275 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
277 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
278 netdev_close(netdev);
283 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
285 if (strncmp(name, "nl:", 3)
287 || name[strspn(name + 3, "0123456789") + 3]) {
288 ofp_fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
290 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
293 static void do_add_dp(const struct settings *s, int argc UNUSED, char *argv[])
296 open_nl_vconn(argv[1], false, &dp);
297 run(dpif_add_dp(&dp), "add_dp");
301 static void do_del_dp(const struct settings *s, int argc UNUSED, char *argv[])
304 open_nl_vconn(argv[1], false, &dp);
305 run(dpif_del_dp(&dp), "del_dp");
309 static void add_del_ports(int argc UNUSED, char *argv[],
310 int (*function)(struct dpif *, const char *netdev),
311 const char *operation, const char *preposition)
314 bool failure = false;
317 open_nl_vconn(argv[1], false, &dp);
318 for (i = 2; i < argc; i++) {
319 int retval = function(&dp, argv[i]);
321 ofp_error(retval, "failed to %s %s %s %s",
322 operation, argv[i], preposition, argv[1]);
332 static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
334 int retval = if_up(netdev);
335 return retval ? retval : dpif_add_port(dpif, netdev);
338 static void do_add_port(const struct settings *s, int argc UNUSED,
341 add_del_ports(argc, argv, ifup_and_add_port, "add", "to");
344 static void do_del_port(const struct settings *s, int argc UNUSED,
347 add_del_ports(argc, argv, dpif_del_port, "remove", "from");
349 #endif /* HAVE_NETLINK */
351 /* Generic commands. */
354 open_vconn(const char *name, struct vconn **vconnp)
356 run(vconn_open_block(name, OFP_VERSION, vconnp), "connecting to %s", name);
360 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
362 struct ofp_stats_request *rq;
363 rq = make_openflow((offsetof(struct ofp_stats_request, body)
364 + body_len), OFPT_STATS_REQUEST, bufferp);
365 rq->type = htons(type);
366 rq->flags = htons(0);
371 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
373 update_openflow_length(buffer);
374 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
378 dump_transaction(const char *vconn_name, struct ofpbuf *request)
381 struct ofpbuf *reply;
383 update_openflow_length(request);
384 open_vconn(vconn_name, &vconn);
385 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
386 ofp_print(stdout, reply->data, reply->size, 1);
391 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
393 struct ofpbuf *request;
394 make_openflow(sizeof(struct ofp_header), request_type, &request);
395 dump_transaction(vconn_name, request);
399 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
401 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
405 open_vconn(vconn_name, &vconn);
406 send_openflow_buffer(vconn, request);
409 struct ofpbuf *reply;
411 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
412 recv_xid = ((struct ofp_header *) reply->data)->xid;
413 if (send_xid == recv_xid) {
414 struct ofp_stats_reply *osr;
416 ofp_print(stdout, reply->data, reply->size, 1);
418 osr = ofpbuf_at(reply, 0, sizeof *osr);
419 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
421 VLOG_DBG("received reply with xid %08"PRIx32" "
422 "!= expected %08"PRIx32, recv_xid, send_xid);
424 ofpbuf_delete(reply);
430 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
432 struct ofpbuf *request;
433 alloc_stats_request(0, stats_type, &request);
434 dump_stats_transaction(vconn_name, request);
438 do_show(const struct settings *s, int argc UNUSED, char *argv[])
440 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
441 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
445 do_status(const struct settings *s, int argc, char *argv[])
447 struct nicira_header *request, *reply;
451 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
452 request->vendor_id = htonl(NX_VENDOR_ID);
453 request->subtype = htonl(NXT_STATUS_REQUEST);
455 ofpbuf_put(b, argv[2], strlen(argv[2]));
457 open_vconn(argv[1], &vconn);
458 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
461 if (b->size < sizeof *reply) {
462 ofp_fatal(0, "short reply (%zu bytes)", b->size);
465 if (reply->header.type != OFPT_VENDOR
466 || reply->vendor_id != ntohl(NX_VENDOR_ID)
467 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
468 ofp_print(stderr, b->data, b->size, 2);
469 ofp_fatal(0, "bad reply");
472 fwrite(reply + 1, b->size, 1, stdout);
476 do_dump_desc(const struct settings *s, int argc, char *argv[])
478 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
482 do_dump_tables(const struct settings *s, int argc, char *argv[])
484 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
489 str_to_int(const char *str)
495 value = strtoul(str, &tail, 0);
496 if (errno == EINVAL || errno == ERANGE || *tail) {
497 ofp_fatal(0, "invalid numeric format %s", str);
503 str_to_mac(const char *str, uint8_t mac[6])
505 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
506 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
507 ofp_fatal(0, "invalid mac address %s", str);
512 str_to_ip(const char *str_, uint32_t *ip)
514 char *str = xstrdup(str_);
515 char *save_ptr = NULL;
516 const char *name, *netmask;
517 struct in_addr in_addr;
520 name = strtok_r(str, "//", &save_ptr);
521 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
523 ofp_fatal(0, "%s: could not convert to IP address", str);
525 *ip = in_addr.s_addr;
527 netmask = strtok_r(NULL, "//", &save_ptr);
530 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
531 &o[0], &o[1], &o[2], &o[3]) == 4) {
532 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
535 /* Find first 1-bit. */
536 for (i = 0; i < 32; i++) {
537 if (nm & (1u << i)) {
543 /* Verify that the rest of the bits are 1-bits. */
544 for (; i < 32; i++) {
545 if (!(nm & (1u << i))) {
546 ofp_fatal(0, "%s: %s is not a valid netmask",
551 int prefix = atoi(netmask);
552 if (prefix <= 0 || prefix > 32) {
553 ofp_fatal(0, "%s: network prefix bits not between 1 and 32",
556 n_wild = 32 - prefix;
567 str_to_action(char *str, struct ofp_action *action, int *n_actions)
571 int max_actions = *n_actions;
573 char *saveptr = NULL;
575 memset(action, 0, sizeof(*action) * max_actions);
576 for (i=0, act = strtok_r(str, ", \t\r\n", &saveptr);
577 i<max_actions && act;
578 i++, act = strtok_r(NULL, ", \t\r\n", &saveptr))
582 /* Arguments are separated by colons */
583 arg = strchr(act, ':');
589 if (!strcasecmp(act, "mod_vlan")) {
590 action[i].type = htons(OFPAT_SET_DL_VLAN);
592 if (!strcasecmp(arg, "strip")) {
593 action[i].arg.vlan_id = htons(OFP_VLAN_NONE);
595 action[i].arg.vlan_id = htons(str_to_int(arg));
597 } else if (!strcasecmp(act, "output")) {
598 port = str_to_int(arg);
599 } else if (!strcasecmp(act, "TABLE")) {
601 } else if (!strcasecmp(act, "NORMAL")) {
603 } else if (!strcasecmp(act, "FLOOD")) {
605 } else if (!strcasecmp(act, "ALL")) {
607 } else if (!strcasecmp(act, "CONTROLLER")) {
608 port = OFPP_CONTROLLER;
610 if (!strcasecmp(arg, "all")) {
611 action[i].arg.output.max_len= htons(0);
613 action[i].arg.output.max_len= htons(str_to_int(arg));
616 } else if (!strcasecmp(act, "LOCAL")) {
618 } else if (strspn(act, "0123456789") == strlen(act)) {
619 port = str_to_int(act);
621 ofp_fatal(0, "Unknown action: %s", act);
624 if (port != OFPP_MAX) {
625 action[i].type = htons(OFPAT_OUTPUT);
626 action[i].arg.output.port = htons(port);
640 parse_protocol(const char *name, const struct protocol **p_out)
642 static const struct protocol protocols[] = {
643 { "ip", ETH_TYPE_IP },
644 { "arp", ETH_TYPE_ARP },
645 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
646 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
647 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
649 const struct protocol *p;
651 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
652 if (!strcmp(p->name, name)) {
664 enum { F_U8, F_U16, F_MAC, F_IP } type;
665 size_t offset, shift;
669 parse_field(const char *name, const struct field **f_out)
671 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
672 static const struct field fields[] = {
673 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
674 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
675 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
676 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
677 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
678 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
679 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
680 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
681 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
682 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
683 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
684 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
686 const struct field *f;
688 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
689 if (!strcmp(f->name, name)) {
699 str_to_flow(char *string, struct ofp_match *match,
700 struct ofp_action *action, int *n_actions, uint8_t *table_idx,
701 uint16_t *priority, uint16_t *idle_timeout, uint16_t *hard_timeout)
711 *priority = OFP_DEFAULT_PRIORITY;
714 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
717 *hard_timeout = OFP_FLOW_PERMANENT;
720 char *act_str = strstr(string, "action");
722 ofp_fatal(0, "must specify an action");
726 act_str = strchr(act_str, '=');
728 ofp_fatal(0, "must specify an action");
733 str_to_action(act_str, action, n_actions);
735 memset(match, 0, sizeof *match);
736 wildcards = OFPFW_ALL;
737 for (name = strtok(string, "=, \t\r\n"); name;
738 name = strtok(NULL, "=, \t\r\n")) {
739 const struct protocol *p;
741 if (parse_protocol(name, &p)) {
742 wildcards &= ~OFPFW_DL_TYPE;
743 match->dl_type = htons(p->dl_type);
745 wildcards &= ~OFPFW_NW_PROTO;
746 match->nw_proto = p->nw_proto;
749 const struct field *f;
752 value = strtok(NULL, ", \t\r\n");
754 ofp_fatal(0, "field %s missing value", name);
757 if (table_idx && !strcmp(name, "table")) {
758 *table_idx = atoi(value);
759 } else if (priority && !strcmp(name, "priority")) {
760 *priority = atoi(value);
761 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
762 *idle_timeout = atoi(value);
763 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
764 *hard_timeout = atoi(value);
765 } else if (parse_field(name, &f)) {
766 void *data = (char *) match + f->offset;
767 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
768 wildcards |= f->wildcard;
770 wildcards &= ~f->wildcard;
771 if (f->type == F_U8) {
772 *(uint8_t *) data = str_to_int(value);
773 } else if (f->type == F_U16) {
774 *(uint16_t *) data = htons(str_to_int(value));
775 } else if (f->type == F_MAC) {
776 str_to_mac(value, data);
777 } else if (f->type == F_IP) {
778 wildcards |= str_to_ip(value, data) << f->shift;
784 ofp_fatal(0, "unknown keyword %s", name);
788 match->wildcards = htonl(wildcards);
791 static void do_dump_flows(const struct settings *s, int argc, char *argv[])
793 struct ofp_flow_stats_request *req;
794 struct ofpbuf *request;
796 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
797 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
798 &req->table_id, NULL, NULL, NULL);
799 memset(req->pad, 0, sizeof req->pad);
801 dump_stats_transaction(argv[1], request);
804 static void do_dump_aggregate(const struct settings *s, int argc,
807 struct ofp_aggregate_stats_request *req;
808 struct ofpbuf *request;
810 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
811 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
812 &req->table_id, NULL, NULL, NULL);
813 memset(req->pad, 0, sizeof req->pad);
815 dump_stats_transaction(argv[1], request);
818 static void do_add_flow(const struct settings *s, int argc, char *argv[])
821 struct ofpbuf *buffer;
822 struct ofp_flow_mod *ofm;
823 uint16_t priority, idle_timeout, hard_timeout;
825 int n_actions = MAX_ADD_ACTS;
827 /* Parse and send. */
828 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
829 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
830 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
831 NULL, &priority, &idle_timeout, &hard_timeout);
832 ofm->command = htons(OFPFC_ADD);
833 ofm->idle_timeout = htons(idle_timeout);
834 ofm->hard_timeout = htons(hard_timeout);
835 ofm->buffer_id = htonl(UINT32_MAX);
836 ofm->priority = htons(priority);
837 ofm->reserved = htonl(0);
839 /* xxx Should we use the ofpbuf library? */
840 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
842 open_vconn(argv[1], &vconn);
843 send_openflow_buffer(vconn, buffer);
847 static void do_add_flows(const struct settings *s, int argc, char *argv[])
853 file = fopen(argv[2], "r");
855 ofp_fatal(errno, "%s: open", argv[2]);
858 open_vconn(argv[1], &vconn);
859 while (fgets(line, sizeof line, file)) {
860 struct ofpbuf *buffer;
861 struct ofp_flow_mod *ofm;
862 uint16_t priority, idle_timeout, hard_timeout;
864 int n_actions = MAX_ADD_ACTS;
868 /* Delete comments. */
869 comment = strchr(line, '#');
874 /* Drop empty lines. */
875 if (line[strspn(line, " \t\n")] == '\0') {
879 /* Parse and send. */
880 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
881 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
882 str_to_flow(line, &ofm->match, &ofm->actions[0], &n_actions,
883 NULL, &priority, &idle_timeout, &hard_timeout);
884 ofm->command = htons(OFPFC_ADD);
885 ofm->idle_timeout = htons(idle_timeout);
886 ofm->hard_timeout = htons(hard_timeout);
887 ofm->buffer_id = htonl(UINT32_MAX);
888 ofm->priority = htons(priority);
889 ofm->reserved = htonl(0);
891 /* xxx Should we use the ofpbuf library? */
892 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
894 send_openflow_buffer(vconn, buffer);
900 static void do_mod_flows(const struct settings *s, int argc, char *argv[])
902 uint16_t priority, idle_timeout, hard_timeout;
904 struct ofpbuf *buffer;
905 struct ofp_flow_mod *ofm;
907 int n_actions = MAX_ADD_ACTS;
909 /* Parse and send. */
910 size = sizeof *ofm + (sizeof ofm->actions[0] * MAX_ADD_ACTS);
911 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
912 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &n_actions,
913 NULL, &priority, &idle_timeout, &hard_timeout);
915 ofm->command = htons(OFPFC_MODIFY_STRICT);
917 ofm->command = htons(OFPFC_MODIFY);
919 ofm->idle_timeout = htons(idle_timeout);
920 ofm->hard_timeout = htons(hard_timeout);
921 ofm->buffer_id = htonl(UINT32_MAX);
922 ofm->priority = htons(priority);
923 ofm->reserved = htonl(0);
925 /* xxx Should we use the buffer library? */
926 buffer->size -= (MAX_ADD_ACTS - n_actions) * sizeof ofm->actions[0];
928 open_vconn(argv[1], &vconn);
929 send_openflow_buffer(vconn, buffer);
933 static void do_del_flows(const struct settings *s, int argc, char *argv[])
937 struct ofpbuf *buffer;
938 struct ofp_flow_mod *ofm;
941 /* Parse and send. */
943 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
944 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
945 &priority, NULL, NULL);
947 ofm->command = htons(OFPFC_DELETE_STRICT);
949 ofm->command = htons(OFPFC_DELETE);
951 ofm->idle_timeout = htons(0);
952 ofm->hard_timeout = htons(0);
953 ofm->buffer_id = htonl(UINT32_MAX);
954 ofm->priority = htons(priority);
955 ofm->reserved = htonl(0);
957 open_vconn(argv[1], &vconn);
958 send_openflow_buffer(vconn, buffer);
963 do_monitor(const struct settings *s, int argc UNUSED, char *argv[])
968 /* If the user specified, e.g., "nl:0", append ":1" to it to ensure that
969 * the connection will subscribe to listen for asynchronous messages, such
970 * as packet-in messages. */
971 if (!strncmp(argv[1], "nl:", 3) && strrchr(argv[1], ':') == &argv[1][2]) {
972 name = xasprintf("%s:1", argv[1]);
976 open_vconn(argv[1], &vconn);
979 run(vconn_recv_block(vconn, &b), "vconn_recv");
980 ofp_print(stderr, b->data, b->size, 2);
986 do_dump_ports(const struct settings *s, int argc, char *argv[])
988 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
992 do_probe(const struct settings *s, int argc, char *argv[])
994 struct ofpbuf *request;
996 struct ofpbuf *reply;
998 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
999 open_vconn(argv[1], &vconn);
1000 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1001 if (reply->size != request->size) {
1002 ofp_fatal(0, "reply does not match request");
1004 ofpbuf_delete(reply);
1009 do_mod_port(const struct settings *s, int argc, char *argv[])
1011 struct ofpbuf *request, *reply;
1012 struct ofp_switch_features *osf;
1013 struct ofp_port_mod *opm;
1014 struct vconn *vconn;
1021 /* Check if the argument is a port index. Otherwise, treat it as
1023 port_no = strtol(argv[2], &endptr, 10);
1024 if (port_no == 0 && endptr == argv[2]) {
1028 /* Send a "Features Request" to get the information we need in order
1029 * to modify the port. */
1030 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1031 open_vconn(argv[1], &vconn);
1032 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1035 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1037 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1038 if (port_no != -1) {
1039 /* Check argument as a port index */
1040 if (osf->ports[port_idx].port_no == htons(port_no)) {
1044 /* Check argument as an interface name */
1045 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1046 sizeof osf->ports[0].name)) {
1052 if (port_idx == n_ports) {
1053 ofp_fatal(0, "couldn't find monitored port: %s", argv[2]);
1056 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1057 memcpy(&opm->desc, &osf->ports[port_idx], sizeof osf->ports[0]);
1059 opm->desc.flags = 0;
1061 printf("modifying port: %s\n", osf->ports[port_idx].name);
1063 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1064 opm->mask |= htonl(OFPPFL_PORT_DOWN);
1065 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1066 sizeof MOD_PORT_CMD_DOWN)) {
1067 opm->mask |= htonl(OFPPFL_PORT_DOWN);
1068 opm->desc.flags |= htonl(OFPPFL_PORT_DOWN);
1069 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1070 sizeof MOD_PORT_CMD_FLOOD)) {
1071 opm->mask |= htonl(OFPPFL_NO_FLOOD);
1072 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1073 sizeof MOD_PORT_CMD_NOFLOOD)) {
1074 opm->mask |= htonl(OFPPFL_NO_FLOOD);
1075 opm->desc.flags |= htonl(OFPPFL_NO_FLOOD);
1077 ofp_fatal(0, "unknown mod-port command '%s'", argv[3]);
1080 send_openflow_buffer(vconn, request);
1082 ofpbuf_delete(reply);
1087 do_ping(const struct settings *s, int argc, char *argv[])
1089 size_t max_payload = 65535 - sizeof(struct ofp_header);
1090 unsigned int payload;
1091 struct vconn *vconn;
1094 payload = argc > 2 ? atoi(argv[2]) : 64;
1095 if (payload > max_payload) {
1096 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1099 open_vconn(argv[1], &vconn);
1100 for (i = 0; i < 10; i++) {
1101 struct timeval start, end;
1102 struct ofpbuf *request, *reply;
1103 struct ofp_header *rq_hdr, *rpy_hdr;
1105 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1106 OFPT_ECHO_REQUEST, &request);
1107 random_bytes(rq_hdr + 1, payload);
1109 gettimeofday(&start, NULL);
1110 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1111 gettimeofday(&end, NULL);
1113 rpy_hdr = reply->data;
1114 if (reply->size != request->size
1115 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1116 || rpy_hdr->xid != rq_hdr->xid
1117 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1118 printf("Reply does not match request. Request:\n");
1119 ofp_print(stdout, request, request->size, 2);
1121 ofp_print(stdout, reply, reply->size, 2);
1123 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1124 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1125 (1000*(double)(end.tv_sec - start.tv_sec))
1126 + (.001*(end.tv_usec - start.tv_usec)));
1127 ofpbuf_delete(request);
1128 ofpbuf_delete(reply);
1134 do_benchmark(const struct settings *s, int argc, char *argv[])
1136 size_t max_payload = 65535 - sizeof(struct ofp_header);
1137 struct timeval start, end;
1138 unsigned int payload_size, message_size;
1139 struct vconn *vconn;
1144 payload_size = atoi(argv[2]);
1145 if (payload_size > max_payload) {
1146 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1148 message_size = sizeof(struct ofp_header) + payload_size;
1150 count = atoi(argv[3]);
1152 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1153 count, message_size, count * message_size);
1155 open_vconn(argv[1], &vconn);
1156 gettimeofday(&start, NULL);
1157 for (i = 0; i < count; i++) {
1158 struct ofpbuf *request, *reply;
1159 struct ofp_header *rq_hdr;
1161 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1162 memset(rq_hdr + 1, 0, payload_size);
1163 run(vconn_transact(vconn, request, &reply), "transact");
1164 ofpbuf_delete(reply);
1166 gettimeofday(&end, NULL);
1169 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1170 + (.001*(end.tv_usec - start.tv_usec)));
1171 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1172 duration, count / (duration / 1000.0),
1173 count * message_size / (duration / 1000.0));
1176 static void do_help(const struct settings *s, int argc UNUSED,
1177 char *argv[] UNUSED)
1182 static struct command all_commands[] = {
1184 { "adddp", 1, 1, do_add_dp },
1185 { "deldp", 1, 1, do_del_dp },
1186 { "addif", 2, INT_MAX, do_add_port },
1187 { "delif", 2, INT_MAX, do_del_port },
1190 { "show", 1, 1, do_show },
1191 { "status", 1, 2, do_status },
1193 { "help", 0, INT_MAX, do_help },
1194 { "monitor", 1, 1, do_monitor },
1195 { "dump-desc", 1, 1, do_dump_desc },
1196 { "dump-tables", 1, 1, do_dump_tables },
1197 { "dump-flows", 1, 2, do_dump_flows },
1198 { "dump-aggregate", 1, 2, do_dump_aggregate },
1199 { "add-flow", 2, 2, do_add_flow },
1200 { "add-flows", 2, 2, do_add_flows },
1201 { "mod-flows", 2, 2, do_mod_flows },
1202 { "del-flows", 1, 2, do_del_flows },
1203 { "dump-ports", 1, 1, do_dump_ports },
1204 { "mod-port", 3, 3, do_mod_port },
1205 { "probe", 1, 1, do_probe },
1206 { "ping", 1, 2, do_ping },
1207 { "benchmark", 3, 3, do_benchmark },
1208 { NULL, 0, 0, NULL },