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
73 /* Maximum size of action buffer for adding and modify flows */
74 #define MAX_ACT_LEN 60
76 #define MOD_PORT_CMD_UP "up"
77 #define MOD_PORT_CMD_DOWN "down"
78 #define MOD_PORT_CMD_FLOOD "flood"
79 #define MOD_PORT_CMD_NOFLOOD "noflood"
82 /* Settings that may be configured by the user. */
84 bool strict; /* Use strict matching for flow mod commands */
91 void (*handler)(const struct settings *, int argc, char *argv[]);
94 static struct command all_commands[];
96 static void usage(void) NO_RETURN;
97 static void parse_options(int argc, char *argv[], struct settings *);
99 int main(int argc, char *argv[])
104 set_program_name(argv[0]);
107 parse_options(argc, argv, &s);
108 signal(SIGPIPE, SIG_IGN);
113 ofp_fatal(0, "missing command name; use --help for help");
115 for (p = all_commands; p->name != NULL; p++) {
116 if (!strcmp(p->name, argv[0])) {
117 int n_arg = argc - 1;
118 if (n_arg < p->min_args)
119 ofp_fatal(0, "'%s' command requires at least %d arguments",
120 p->name, p->min_args);
121 else if (n_arg > p->max_args)
122 ofp_fatal(0, "'%s' command takes at most %d arguments",
123 p->name, p->max_args);
125 p->handler(&s, argc, argv);
130 ofp_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
136 parse_options(int argc, char *argv[], struct settings *s)
139 OPT_STRICT = UCHAR_MAX + 1
141 static struct option long_options[] = {
142 {"timeout", required_argument, 0, 't'},
143 {"verbose", optional_argument, 0, 'v'},
144 {"strict", no_argument, 0, OPT_STRICT},
145 {"help", no_argument, 0, 'h'},
146 {"version", no_argument, 0, 'V'},
147 VCONN_SSL_LONG_OPTIONS
150 char *short_options = long_options_to_short_options(long_options);
152 /* Set defaults that we can figure out before parsing options. */
156 unsigned long int timeout;
159 c = getopt_long(argc, argv, short_options, long_options, NULL);
166 timeout = strtoul(optarg, NULL, 10);
168 ofp_fatal(0, "value %s on -t or --timeout is not at least 1",
179 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
183 vlog_set_verbosity(optarg);
190 VCONN_SSL_OPTION_HANDLERS
205 printf("%s: OpenFlow switch management utility\n"
206 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
208 "\nFor local datapaths only:\n"
209 " adddp nl:DP_ID add a new local datapath DP_ID\n"
210 " deldp nl:DP_ID delete local datapath DP_ID\n"
211 " addif nl:DP_ID IFACE... add each IFACE as a port on DP_ID\n"
212 " delif nl:DP_ID IFACE... delete each IFACE from DP_ID\n"
214 "\nFor local datapaths and remote switches:\n"
215 " show SWITCH show basic information\n"
216 " status SWITCH [KEY] report statistics (about KEY)\n"
217 " dump-desc SWITCH print switch description\n"
218 " dump-tables SWITCH print table stats\n"
219 " mod-port SWITCH IFACE ACT modify port behavior\n"
220 " dump-ports SWITCH print port statistics\n"
221 " dump-flows SWITCH print all flow entries\n"
222 " dump-flows SWITCH FLOW print matching FLOWs\n"
223 " dump-aggregate SWITCH print aggregate flow statistics\n"
224 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
225 " add-flow SWITCH FLOW add flow described by FLOW\n"
226 " add-flows SWITCH FILE add flows from FILE\n"
227 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
228 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
229 " monitor SWITCH print packets received from SWITCH\n"
230 "\nFor local datapaths, remote switches, and controllers:\n"
231 " probe VCONN probe whether VCONN is up\n"
232 " ping VCONN [N] latency of N-byte echos\n"
233 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
234 "where each SWITCH is an active OpenFlow connection method.\n",
235 program_name, program_name);
236 vconn_usage(true, false, false);
237 printf("\nOptions:\n"
238 " --strict use strict match for flow commands\n"
239 " -t, --timeout=SECS give up after SECS seconds\n"
240 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
241 " -v, --verbose set maximum verbosity level\n"
242 " -h, --help display this help message\n"
243 " -V, --version display version information\n");
247 static void run(int retval, const char *message, ...)
250 static void run(int retval, const char *message, ...)
255 fprintf(stderr, "%s: ", program_name);
256 va_start(args, message);
257 vfprintf(stderr, message, args);
260 fputs(": unexpected end of file\n", stderr);
262 fprintf(stderr, ": %s\n", strerror(retval));
270 /* Netlink-only commands. */
272 static int if_up(const char *netdev_name)
274 struct netdev *netdev;
277 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
279 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
280 netdev_close(netdev);
285 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
287 if (strncmp(name, "nl:", 3)
289 || name[strspn(name + 3, "0123456789") + 3]) {
290 ofp_fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
292 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
295 static void do_add_dp(const struct settings *s, int argc UNUSED, char *argv[])
298 open_nl_vconn(argv[1], false, &dp);
299 run(dpif_add_dp(&dp), "add_dp");
303 static void do_del_dp(const struct settings *s, int argc UNUSED, char *argv[])
306 open_nl_vconn(argv[1], false, &dp);
307 run(dpif_del_dp(&dp), "del_dp");
311 static void add_del_ports(int argc UNUSED, char *argv[],
312 int (*function)(struct dpif *, const char *netdev),
313 const char *operation, const char *preposition)
316 bool failure = false;
319 open_nl_vconn(argv[1], false, &dp);
320 for (i = 2; i < argc; i++) {
321 int retval = function(&dp, argv[i]);
323 ofp_error(retval, "failed to %s %s %s %s",
324 operation, argv[i], preposition, argv[1]);
334 static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
336 int retval = if_up(netdev);
337 return retval ? retval : dpif_add_port(dpif, netdev);
340 static void do_add_port(const struct settings *s, int argc UNUSED,
343 add_del_ports(argc, argv, ifup_and_add_port, "add", "to");
346 static void do_del_port(const struct settings *s, int argc UNUSED,
349 add_del_ports(argc, argv, dpif_del_port, "remove", "from");
351 #endif /* HAVE_NETLINK */
353 /* Generic commands. */
356 open_vconn(const char *name, struct vconn **vconnp)
358 run(vconn_open_block(name, OFP_VERSION, vconnp), "connecting to %s", name);
362 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
364 struct ofp_stats_request *rq;
365 rq = make_openflow((offsetof(struct ofp_stats_request, body)
366 + body_len), OFPT_STATS_REQUEST, bufferp);
367 rq->type = htons(type);
368 rq->flags = htons(0);
373 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
375 update_openflow_length(buffer);
376 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
380 dump_transaction(const char *vconn_name, struct ofpbuf *request)
383 struct ofpbuf *reply;
385 update_openflow_length(request);
386 open_vconn(vconn_name, &vconn);
387 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
388 ofp_print(stdout, reply->data, reply->size, 1);
393 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
395 struct ofpbuf *request;
396 make_openflow(sizeof(struct ofp_header), request_type, &request);
397 dump_transaction(vconn_name, request);
401 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
403 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
407 open_vconn(vconn_name, &vconn);
408 send_openflow_buffer(vconn, request);
411 struct ofpbuf *reply;
413 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
414 recv_xid = ((struct ofp_header *) reply->data)->xid;
415 if (send_xid == recv_xid) {
416 struct ofp_stats_reply *osr;
418 ofp_print(stdout, reply->data, reply->size, 1);
420 osr = ofpbuf_at(reply, 0, sizeof *osr);
421 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
423 VLOG_DBG("received reply with xid %08"PRIx32" "
424 "!= expected %08"PRIx32, recv_xid, send_xid);
426 ofpbuf_delete(reply);
432 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
434 struct ofpbuf *request;
435 alloc_stats_request(0, stats_type, &request);
436 dump_stats_transaction(vconn_name, request);
440 do_show(const struct settings *s, int argc UNUSED, char *argv[])
442 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
443 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
447 do_status(const struct settings *s, int argc, char *argv[])
449 struct nicira_header *request, *reply;
453 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
454 request->vendor_id = htonl(NX_VENDOR_ID);
455 request->subtype = htonl(NXT_STATUS_REQUEST);
457 ofpbuf_put(b, argv[2], strlen(argv[2]));
459 open_vconn(argv[1], &vconn);
460 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
463 if (b->size < sizeof *reply) {
464 ofp_fatal(0, "short reply (%zu bytes)", b->size);
467 if (reply->header.type != OFPT_VENDOR
468 || reply->vendor_id != ntohl(NX_VENDOR_ID)
469 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
470 ofp_print(stderr, b->data, b->size, 2);
471 ofp_fatal(0, "bad reply");
474 fwrite(reply + 1, b->size, 1, stdout);
478 do_dump_desc(const struct settings *s, int argc, char *argv[])
480 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
484 do_dump_tables(const struct settings *s, int argc, char *argv[])
486 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
491 str_to_int(const char *str)
497 value = strtoul(str, &tail, 0);
498 if (errno == EINVAL || errno == ERANGE || *tail) {
499 ofp_fatal(0, "invalid numeric format %s", str);
505 str_to_mac(const char *str, uint8_t mac[6])
507 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
508 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
509 ofp_fatal(0, "invalid mac address %s", str);
514 str_to_ip(const char *str_, uint32_t *ip)
516 char *str = xstrdup(str_);
517 char *save_ptr = NULL;
518 const char *name, *netmask;
519 struct in_addr in_addr;
522 name = strtok_r(str, "//", &save_ptr);
523 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
525 ofp_fatal(0, "%s: could not convert to IP address", str);
527 *ip = in_addr.s_addr;
529 netmask = strtok_r(NULL, "//", &save_ptr);
532 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
533 &o[0], &o[1], &o[2], &o[3]) == 4) {
534 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
537 /* Find first 1-bit. */
538 for (i = 0; i < 32; i++) {
539 if (nm & (1u << i)) {
545 /* Verify that the rest of the bits are 1-bits. */
546 for (; i < 32; i++) {
547 if (!(nm & (1u << i))) {
548 ofp_fatal(0, "%s: %s is not a valid netmask",
553 int prefix = atoi(netmask);
554 if (prefix <= 0 || prefix > 32) {
555 ofp_fatal(0, "%s: network prefix bits not between 1 and 32",
558 n_wild = 32 - prefix;
569 str_to_action(char *str, struct ofp_action_header *actions,
572 size_t len = *actions_len;
574 char *saveptr = NULL;
575 uint8_t *p = (uint8_t *)actions;
577 memset(actions, 0, len);
578 for (act = strtok_r(str, ", \t\r\n", &saveptr);
579 (len >= sizeof(struct ofp_action_header)) && act;
580 act = strtok_r(NULL, ", \t\r\n", &saveptr))
583 struct ofp_action_header *ah = (struct ofp_action_header *)p;
584 int act_len = sizeof *ah;
587 /* Arguments are separated by colons */
588 arg = strchr(act, ':');
594 if (!strcasecmp(act, "mod_vlan_vid")) {
595 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
597 if (len < sizeof *va) {
598 ofp_fatal(0, "Insufficient room for vlan vid action\n");
601 act_len = sizeof *va;
602 va->type = htons(OFPAT_SET_VLAN_VID);
603 va->vlan_vid = htons(str_to_int(arg));
604 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
605 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
607 if (len < sizeof *va) {
608 ofp_fatal(0, "Insufficient room for vlan pcp action\n");
611 act_len = sizeof *va;
612 va->type = htons(OFPAT_SET_VLAN_PCP);
613 va->vlan_pcp = str_to_int(arg);
614 } else if (!strcasecmp(act, "strip_vlan")) {
615 ah->type = htons(OFPAT_STRIP_VLAN);
616 } else if (!strcasecmp(act, "output")) {
617 port = str_to_int(arg);
618 } else if (!strcasecmp(act, "TABLE")) {
620 } else if (!strcasecmp(act, "NORMAL")) {
622 } else if (!strcasecmp(act, "FLOOD")) {
624 } else if (!strcasecmp(act, "ALL")) {
626 } else if (!strcasecmp(act, "CONTROLLER")) {
627 struct ofp_action_output *ca = (struct ofp_action_output *)ah;
629 if (act_len < sizeof *ca) {
630 ofp_fatal(0, "Insufficient room for controller action\n");
633 act_len = sizeof *ca;
634 ca->type = htons(OFPAT_OUTPUT);
635 ca->port = htons(OFPP_CONTROLLER);
637 /* Unless a numeric argument is specified, we send the whole
638 * packet to the controller. */
639 if (arg && (strspn(act, "0123456789") == strlen(act))) {
640 ca->max_len= htons(str_to_int(arg));
642 } else if (!strcasecmp(act, "LOCAL")) {
644 } else if (strspn(act, "0123456789") == strlen(act)) {
645 port = str_to_int(act);
647 ofp_fatal(0, "Unknown action: %s", act);
650 if (port != OFPP_MAX) {
651 struct ofp_action_output *oa = (struct ofp_action_output *)p;
653 if (act_len < sizeof *oa) {
654 ofp_fatal(0, "Insufficient room for output action\n");
657 act_len = sizeof *oa;
658 oa->type = htons(OFPAT_OUTPUT);
659 oa->port = htons(port);
662 ah->len = htons(act_len);
677 parse_protocol(const char *name, const struct protocol **p_out)
679 static const struct protocol protocols[] = {
680 { "ip", ETH_TYPE_IP },
681 { "arp", ETH_TYPE_ARP },
682 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
683 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
684 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
686 const struct protocol *p;
688 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
689 if (!strcmp(p->name, name)) {
701 enum { F_U8, F_U16, F_MAC, F_IP } type;
702 size_t offset, shift;
706 parse_field(const char *name, const struct field **f_out)
708 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
709 static const struct field fields[] = {
710 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
711 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
712 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
713 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
714 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
715 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
716 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
717 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
718 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
719 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
720 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
721 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
723 const struct field *f;
725 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
726 if (!strcmp(f->name, name)) {
736 str_to_flow(char *string, struct ofp_match *match,
737 struct ofp_action_header *actions, size_t *actions_len,
738 uint8_t *table_idx, uint16_t *priority,
739 uint16_t *idle_timeout, uint16_t *hard_timeout)
749 *priority = OFP_DEFAULT_PRIORITY;
752 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
755 *hard_timeout = OFP_FLOW_PERMANENT;
758 char *act_str = strstr(string, "action");
760 ofp_fatal(0, "must specify an action");
764 act_str = strchr(act_str, '=');
766 ofp_fatal(0, "must specify an action");
771 str_to_action(act_str, actions, actions_len);
773 memset(match, 0, sizeof *match);
774 wildcards = OFPFW_ALL;
775 for (name = strtok(string, "=, \t\r\n"); name;
776 name = strtok(NULL, "=, \t\r\n")) {
777 const struct protocol *p;
779 if (parse_protocol(name, &p)) {
780 wildcards &= ~OFPFW_DL_TYPE;
781 match->dl_type = htons(p->dl_type);
783 wildcards &= ~OFPFW_NW_PROTO;
784 match->nw_proto = p->nw_proto;
787 const struct field *f;
790 value = strtok(NULL, ", \t\r\n");
792 ofp_fatal(0, "field %s missing value", name);
795 if (table_idx && !strcmp(name, "table")) {
796 *table_idx = atoi(value);
797 } else if (priority && !strcmp(name, "priority")) {
798 *priority = atoi(value);
799 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
800 *idle_timeout = atoi(value);
801 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
802 *hard_timeout = atoi(value);
803 } else if (parse_field(name, &f)) {
804 void *data = (char *) match + f->offset;
805 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
806 wildcards |= f->wildcard;
808 wildcards &= ~f->wildcard;
809 if (f->type == F_U8) {
810 *(uint8_t *) data = str_to_int(value);
811 } else if (f->type == F_U16) {
812 *(uint16_t *) data = htons(str_to_int(value));
813 } else if (f->type == F_MAC) {
814 str_to_mac(value, data);
815 } else if (f->type == F_IP) {
816 wildcards |= str_to_ip(value, data) << f->shift;
822 ofp_fatal(0, "unknown keyword %s", name);
826 match->wildcards = htonl(wildcards);
829 static void do_dump_flows(const struct settings *s, int argc, char *argv[])
831 struct ofp_flow_stats_request *req;
832 struct ofpbuf *request;
834 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
835 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
836 &req->table_id, NULL, NULL, NULL);
837 memset(req->pad, 0, sizeof req->pad);
839 dump_stats_transaction(argv[1], request);
842 static void do_dump_aggregate(const struct settings *s, int argc,
845 struct ofp_aggregate_stats_request *req;
846 struct ofpbuf *request;
848 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
849 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
850 &req->table_id, NULL, NULL, NULL);
851 memset(req->pad, 0, sizeof req->pad);
853 dump_stats_transaction(argv[1], request);
856 static void do_add_flow(const struct settings *s, int argc, char *argv[])
859 struct ofpbuf *buffer;
860 struct ofp_flow_mod *ofm;
861 uint16_t priority, idle_timeout, hard_timeout;
863 size_t actions_len = MAX_ACT_LEN;
865 /* Parse and send. */
866 size = sizeof *ofm + actions_len;
867 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
868 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
869 NULL, &priority, &idle_timeout, &hard_timeout);
870 ofm->command = htons(OFPFC_ADD);
871 ofm->idle_timeout = htons(idle_timeout);
872 ofm->hard_timeout = htons(hard_timeout);
873 ofm->buffer_id = htonl(UINT32_MAX);
874 ofm->priority = htons(priority);
875 ofm->reserved = htonl(0);
877 /* xxx Should we use the ofpbuf library? */
878 buffer->size -= MAX_ACT_LEN - actions_len;
880 open_vconn(argv[1], &vconn);
881 send_openflow_buffer(vconn, buffer);
885 static void do_add_flows(const struct settings *s, int argc, char *argv[])
891 file = fopen(argv[2], "r");
893 ofp_fatal(errno, "%s: open", argv[2]);
896 open_vconn(argv[1], &vconn);
897 while (fgets(line, sizeof line, file)) {
898 struct ofpbuf *buffer;
899 struct ofp_flow_mod *ofm;
900 uint16_t priority, idle_timeout, hard_timeout;
902 size_t actions_len = MAX_ACT_LEN;
906 /* Delete comments. */
907 comment = strchr(line, '#');
912 /* Drop empty lines. */
913 if (line[strspn(line, " \t\n")] == '\0') {
917 /* Parse and send. */
918 size = sizeof *ofm + actions_len;
919 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
920 str_to_flow(line, &ofm->match, &ofm->actions[0], &actions_len,
921 NULL, &priority, &idle_timeout, &hard_timeout);
922 ofm->command = htons(OFPFC_ADD);
923 ofm->idle_timeout = htons(idle_timeout);
924 ofm->hard_timeout = htons(hard_timeout);
925 ofm->buffer_id = htonl(UINT32_MAX);
926 ofm->priority = htons(priority);
927 ofm->reserved = htonl(0);
929 /* xxx Should we use the ofpbuf library? */
930 buffer->size -= MAX_ACT_LEN - actions_len;
932 send_openflow_buffer(vconn, buffer);
938 static void do_mod_flows(const struct settings *s, int argc, char *argv[])
940 uint16_t priority, idle_timeout, hard_timeout;
942 struct ofpbuf *buffer;
943 struct ofp_flow_mod *ofm;
945 size_t actions_len = MAX_ACT_LEN;
947 /* Parse and send. */
948 size = sizeof *ofm + actions_len;
949 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
950 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
951 NULL, &priority, &idle_timeout, &hard_timeout);
953 ofm->command = htons(OFPFC_MODIFY_STRICT);
955 ofm->command = htons(OFPFC_MODIFY);
957 ofm->idle_timeout = htons(idle_timeout);
958 ofm->hard_timeout = htons(hard_timeout);
959 ofm->buffer_id = htonl(UINT32_MAX);
960 ofm->priority = htons(priority);
961 ofm->reserved = htonl(0);
963 /* xxx Should we use the buffer library? */
964 buffer->size -= MAX_ACT_LEN - actions_len;
966 open_vconn(argv[1], &vconn);
967 send_openflow_buffer(vconn, buffer);
971 static void do_del_flows(const struct settings *s, int argc, char *argv[])
975 struct ofpbuf *buffer;
976 struct ofp_flow_mod *ofm;
979 /* Parse and send. */
981 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
982 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
983 &priority, NULL, NULL);
985 ofm->command = htons(OFPFC_DELETE_STRICT);
987 ofm->command = htons(OFPFC_DELETE);
989 ofm->idle_timeout = htons(0);
990 ofm->hard_timeout = htons(0);
991 ofm->buffer_id = htonl(UINT32_MAX);
992 ofm->priority = htons(priority);
993 ofm->reserved = htonl(0);
995 open_vconn(argv[1], &vconn);
996 send_openflow_buffer(vconn, buffer);
1001 do_monitor(const struct settings *s, int argc UNUSED, char *argv[])
1003 struct vconn *vconn;
1006 /* If the user specified, e.g., "nl:0", append ":1" to it to ensure that
1007 * the connection will subscribe to listen for asynchronous messages, such
1008 * as packet-in messages. */
1009 if (!strncmp(argv[1], "nl:", 3) && strrchr(argv[1], ':') == &argv[1][2]) {
1010 name = xasprintf("%s:1", argv[1]);
1014 open_vconn(argv[1], &vconn);
1017 run(vconn_recv_block(vconn, &b), "vconn_recv");
1018 ofp_print(stderr, b->data, b->size, 2);
1024 do_dump_ports(const struct settings *s, int argc, char *argv[])
1026 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1030 do_probe(const struct settings *s, int argc, char *argv[])
1032 struct ofpbuf *request;
1033 struct vconn *vconn;
1034 struct ofpbuf *reply;
1036 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1037 open_vconn(argv[1], &vconn);
1038 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1039 if (reply->size != request->size) {
1040 ofp_fatal(0, "reply does not match request");
1042 ofpbuf_delete(reply);
1047 do_mod_port(const struct settings *s, int argc, char *argv[])
1049 struct ofpbuf *request, *reply;
1050 struct ofp_switch_features *osf;
1051 struct ofp_port_mod *opm;
1052 struct vconn *vconn;
1059 /* Check if the argument is a port index. Otherwise, treat it as
1061 port_no = strtol(argv[2], &endptr, 10);
1062 if (port_no == 0 && endptr == argv[2]) {
1066 /* Send a "Features Request" to get the information we need in order
1067 * to modify the port. */
1068 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1069 open_vconn(argv[1], &vconn);
1070 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1073 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1075 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1076 if (port_no != -1) {
1077 /* Check argument as a port index */
1078 if (osf->ports[port_idx].port_no == htons(port_no)) {
1082 /* Check argument as an interface name */
1083 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1084 sizeof osf->ports[0].name)) {
1090 if (port_idx == n_ports) {
1091 ofp_fatal(0, "couldn't find monitored port: %s", argv[2]);
1094 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1095 opm->port_no = osf->ports[port_idx].port_no;
1096 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1097 opm->config = htonl(0);
1098 opm->mask = htonl(0);
1099 opm->advertise = htonl(0);
1101 printf("modifying port: %s\n", osf->ports[port_idx].name);
1103 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1104 opm->mask |= htonl(OFPPC_PORT_DOWN);
1105 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1106 sizeof MOD_PORT_CMD_DOWN)) {
1107 opm->mask |= htonl(OFPPC_PORT_DOWN);
1108 opm->config |= htonl(OFPPC_PORT_DOWN);
1109 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1110 sizeof MOD_PORT_CMD_FLOOD)) {
1111 opm->mask |= htonl(OFPPC_NO_FLOOD);
1112 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1113 sizeof MOD_PORT_CMD_NOFLOOD)) {
1114 opm->mask |= htonl(OFPPC_NO_FLOOD);
1115 opm->config |= htonl(OFPPC_NO_FLOOD);
1117 ofp_fatal(0, "unknown mod-port command '%s'", argv[3]);
1120 send_openflow_buffer(vconn, request);
1122 ofpbuf_delete(reply);
1127 do_ping(const struct settings *s, int argc, char *argv[])
1129 size_t max_payload = 65535 - sizeof(struct ofp_header);
1130 unsigned int payload;
1131 struct vconn *vconn;
1134 payload = argc > 2 ? atoi(argv[2]) : 64;
1135 if (payload > max_payload) {
1136 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1139 open_vconn(argv[1], &vconn);
1140 for (i = 0; i < 10; i++) {
1141 struct timeval start, end;
1142 struct ofpbuf *request, *reply;
1143 struct ofp_header *rq_hdr, *rpy_hdr;
1145 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1146 OFPT_ECHO_REQUEST, &request);
1147 random_bytes(rq_hdr + 1, payload);
1149 gettimeofday(&start, NULL);
1150 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1151 gettimeofday(&end, NULL);
1153 rpy_hdr = reply->data;
1154 if (reply->size != request->size
1155 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1156 || rpy_hdr->xid != rq_hdr->xid
1157 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1158 printf("Reply does not match request. Request:\n");
1159 ofp_print(stdout, request, request->size, 2);
1161 ofp_print(stdout, reply, reply->size, 2);
1163 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1164 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1165 (1000*(double)(end.tv_sec - start.tv_sec))
1166 + (.001*(end.tv_usec - start.tv_usec)));
1167 ofpbuf_delete(request);
1168 ofpbuf_delete(reply);
1174 do_benchmark(const struct settings *s, int argc, char *argv[])
1176 size_t max_payload = 65535 - sizeof(struct ofp_header);
1177 struct timeval start, end;
1178 unsigned int payload_size, message_size;
1179 struct vconn *vconn;
1184 payload_size = atoi(argv[2]);
1185 if (payload_size > max_payload) {
1186 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1188 message_size = sizeof(struct ofp_header) + payload_size;
1190 count = atoi(argv[3]);
1192 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1193 count, message_size, count * message_size);
1195 open_vconn(argv[1], &vconn);
1196 gettimeofday(&start, NULL);
1197 for (i = 0; i < count; i++) {
1198 struct ofpbuf *request, *reply;
1199 struct ofp_header *rq_hdr;
1201 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1202 memset(rq_hdr + 1, 0, payload_size);
1203 run(vconn_transact(vconn, request, &reply), "transact");
1204 ofpbuf_delete(reply);
1206 gettimeofday(&end, NULL);
1209 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1210 + (.001*(end.tv_usec - start.tv_usec)));
1211 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1212 duration, count / (duration / 1000.0),
1213 count * message_size / (duration / 1000.0));
1216 static void do_help(const struct settings *s, int argc UNUSED,
1217 char *argv[] UNUSED)
1222 static struct command all_commands[] = {
1224 { "adddp", 1, 1, do_add_dp },
1225 { "deldp", 1, 1, do_del_dp },
1226 { "addif", 2, INT_MAX, do_add_port },
1227 { "delif", 2, INT_MAX, do_del_port },
1230 { "show", 1, 1, do_show },
1231 { "status", 1, 2, do_status },
1233 { "help", 0, INT_MAX, do_help },
1234 { "monitor", 1, 1, do_monitor },
1235 { "dump-desc", 1, 1, do_dump_desc },
1236 { "dump-tables", 1, 1, do_dump_tables },
1237 { "dump-flows", 1, 2, do_dump_flows },
1238 { "dump-aggregate", 1, 2, do_dump_aggregate },
1239 { "add-flow", 2, 2, do_add_flow },
1240 { "add-flows", 2, 2, do_add_flows },
1241 { "mod-flows", 2, 2, do_mod_flows },
1242 { "del-flows", 1, 2, do_del_flows },
1243 { "dump-ports", 1, 1, do_dump_ports },
1244 { "mod-port", 3, 3, do_mod_port },
1245 { "probe", 1, 1, do_probe },
1246 { "ping", 1, 2, do_ping },
1247 { "benchmark", 3, 3, do_benchmark },
1248 { NULL, 0, 0, NULL },