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-snat SWITCH IFACE IP add SNAT config to IFACE\n"
226 " del-snat SWITCH IFACE delete SNAT config on IFACE\n"
227 " add-flow SWITCH FLOW add flow described by FLOW\n"
228 " add-flows SWITCH FILE add flows from FILE\n"
229 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
230 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
231 " monitor SWITCH print packets received from SWITCH\n"
232 "\nFor local datapaths, remote switches, and controllers:\n"
233 " probe VCONN probe whether VCONN is up\n"
234 " ping VCONN [N] latency of N-byte echos\n"
235 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
236 "where each SWITCH is an active OpenFlow connection method.\n",
237 program_name, program_name);
238 vconn_usage(true, false, false);
239 printf("\nOptions:\n"
240 " --strict use strict match for flow commands\n"
241 " -t, --timeout=SECS give up after SECS seconds\n"
242 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
243 " -v, --verbose set maximum verbosity level\n"
244 " -h, --help display this help message\n"
245 " -V, --version display version information\n");
249 static void run(int retval, const char *message, ...)
252 static void run(int retval, const char *message, ...)
257 fprintf(stderr, "%s: ", program_name);
258 va_start(args, message);
259 vfprintf(stderr, message, args);
262 fputs(": unexpected end of file\n", stderr);
264 fprintf(stderr, ": %s\n", strerror(retval));
272 /* Netlink-only commands. */
274 static int if_up(const char *netdev_name)
276 struct netdev *netdev;
279 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
281 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
282 netdev_close(netdev);
287 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
289 if (strncmp(name, "nl:", 3)
291 || name[strspn(name + 3, "0123456789") + 3]) {
292 ofp_fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
294 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
297 static void do_add_dp(const struct settings *s, int argc UNUSED, char *argv[])
300 open_nl_vconn(argv[1], false, &dp);
301 run(dpif_add_dp(&dp), "add_dp");
305 static void do_del_dp(const struct settings *s, int argc UNUSED, char *argv[])
308 open_nl_vconn(argv[1], false, &dp);
309 run(dpif_del_dp(&dp), "del_dp");
313 static void add_del_ports(int argc UNUSED, char *argv[],
314 int (*function)(struct dpif *, const char *netdev),
315 const char *operation, const char *preposition)
318 bool failure = false;
321 open_nl_vconn(argv[1], false, &dp);
322 for (i = 2; i < argc; i++) {
323 int retval = function(&dp, argv[i]);
325 ofp_error(retval, "failed to %s %s %s %s",
326 operation, argv[i], preposition, argv[1]);
336 static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
338 int retval = if_up(netdev);
339 return retval ? retval : dpif_add_port(dpif, netdev);
342 static void do_add_port(const struct settings *s, int argc UNUSED,
345 add_del_ports(argc, argv, ifup_and_add_port, "add", "to");
348 static void do_del_port(const struct settings *s, int argc UNUSED,
351 add_del_ports(argc, argv, dpif_del_port, "remove", "from");
353 #endif /* HAVE_NETLINK */
355 /* Generic commands. */
358 open_vconn(const char *name, struct vconn **vconnp)
360 run(vconn_open_block(name, OFP_VERSION, vconnp), "connecting to %s", name);
364 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
366 struct ofp_stats_request *rq;
367 rq = make_openflow((offsetof(struct ofp_stats_request, body)
368 + body_len), OFPT_STATS_REQUEST, bufferp);
369 rq->type = htons(type);
370 rq->flags = htons(0);
375 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
377 update_openflow_length(buffer);
378 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
382 dump_transaction(const char *vconn_name, struct ofpbuf *request)
385 struct ofpbuf *reply;
387 update_openflow_length(request);
388 open_vconn(vconn_name, &vconn);
389 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
390 ofp_print(stdout, reply->data, reply->size, 1);
395 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
397 struct ofpbuf *request;
398 make_openflow(sizeof(struct ofp_header), request_type, &request);
399 dump_transaction(vconn_name, request);
403 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
405 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
409 open_vconn(vconn_name, &vconn);
410 send_openflow_buffer(vconn, request);
413 struct ofpbuf *reply;
415 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
416 recv_xid = ((struct ofp_header *) reply->data)->xid;
417 if (send_xid == recv_xid) {
418 struct ofp_stats_reply *osr;
420 ofp_print(stdout, reply->data, reply->size, 1);
422 osr = ofpbuf_at(reply, 0, sizeof *osr);
423 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
425 VLOG_DBG("received reply with xid %08"PRIx32" "
426 "!= expected %08"PRIx32, recv_xid, send_xid);
428 ofpbuf_delete(reply);
434 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
436 struct ofpbuf *request;
437 alloc_stats_request(0, stats_type, &request);
438 dump_stats_transaction(vconn_name, request);
442 do_show(const struct settings *s, int argc UNUSED, char *argv[])
444 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
445 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
449 do_status(const struct settings *s, int argc, char *argv[])
451 struct nicira_header *request, *reply;
455 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
456 request->vendor = htonl(NX_VENDOR_ID);
457 request->subtype = htonl(NXT_STATUS_REQUEST);
459 ofpbuf_put(b, argv[2], strlen(argv[2]));
461 open_vconn(argv[1], &vconn);
462 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
465 if (b->size < sizeof *reply) {
466 ofp_fatal(0, "short reply (%zu bytes)", b->size);
469 if (reply->header.type != OFPT_VENDOR
470 || reply->vendor != ntohl(NX_VENDOR_ID)
471 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
472 ofp_print(stderr, b->data, b->size, 2);
473 ofp_fatal(0, "bad reply");
476 fwrite(reply + 1, b->size, 1, stdout);
480 do_dump_desc(const struct settings *s, int argc, char *argv[])
482 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
486 do_dump_tables(const struct settings *s, int argc, char *argv[])
488 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
493 str_to_int(const char *str)
499 value = strtoul(str, &tail, 0);
500 if (errno == EINVAL || errno == ERANGE || *tail) {
501 ofp_fatal(0, "invalid numeric format %s", str);
507 str_to_mac(const char *str, uint8_t mac[6])
509 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
510 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
511 ofp_fatal(0, "invalid mac address %s", str);
516 str_to_ip(const char *str_, uint32_t *ip)
518 char *str = xstrdup(str_);
519 char *save_ptr = NULL;
520 const char *name, *netmask;
521 struct in_addr in_addr;
524 name = strtok_r(str, "//", &save_ptr);
525 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
527 ofp_fatal(0, "%s: could not convert to IP address", str);
529 *ip = in_addr.s_addr;
531 netmask = strtok_r(NULL, "//", &save_ptr);
534 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
535 &o[0], &o[1], &o[2], &o[3]) == 4) {
536 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
539 /* Find first 1-bit. */
540 for (i = 0; i < 32; i++) {
541 if (nm & (1u << i)) {
547 /* Verify that the rest of the bits are 1-bits. */
548 for (; i < 32; i++) {
549 if (!(nm & (1u << i))) {
550 ofp_fatal(0, "%s: %s is not a valid netmask",
555 int prefix = atoi(netmask);
556 if (prefix <= 0 || prefix > 32) {
557 ofp_fatal(0, "%s: network prefix bits not between 1 and 32",
560 n_wild = 32 - prefix;
571 str_to_action(char *str, struct ofp_action_header *actions,
574 size_t len = *actions_len;
576 char *saveptr = NULL;
577 uint8_t *p = (uint8_t *)actions;
579 memset(actions, 0, len);
580 for (act = strtok_r(str, ", \t\r\n", &saveptr);
581 (len >= sizeof(struct ofp_action_header)) && act;
582 act = strtok_r(NULL, ", \t\r\n", &saveptr))
585 struct ofp_action_header *ah = (struct ofp_action_header *)p;
586 int act_len = sizeof *ah;
589 /* Arguments are separated by colons */
590 arg = strchr(act, ':');
596 if (!strcasecmp(act, "mod_vlan_vid")) {
597 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
599 if (len < sizeof *va) {
600 ofp_fatal(0, "Insufficient room for vlan vid action\n");
603 act_len = sizeof *va;
604 va->type = htons(OFPAT_SET_VLAN_VID);
605 va->vlan_vid = htons(str_to_int(arg));
606 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
607 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
609 if (len < sizeof *va) {
610 ofp_fatal(0, "Insufficient room for vlan pcp action\n");
613 act_len = sizeof *va;
614 va->type = htons(OFPAT_SET_VLAN_PCP);
615 va->vlan_pcp = str_to_int(arg);
616 } else if (!strcasecmp(act, "strip_vlan")) {
617 ah->type = htons(OFPAT_STRIP_VLAN);
618 } else if (!strcasecmp(act, "output")) {
619 port = str_to_int(arg);
620 } else if (!strcasecmp(act, "nat")) {
621 struct nx_action_snat *sa = (struct nx_action_snat *)ah;
623 if (len < sizeof *sa) {
624 ofp_fatal(0, "Insufficient room for SNAT action\n");
627 if (str_to_int(arg) > OFPP_MAX) {
628 ofp_fatal(0, "Invalid nat port: %s\n", arg);
631 act_len = sizeof *sa;
632 sa->type = htons(OFPAT_VENDOR);
633 sa->vendor = htonl(NX_VENDOR_ID);
634 sa->subtype = htons(NXAST_SNAT);
635 sa->port = htons(str_to_int(arg));
636 } else if (!strcasecmp(act, "TABLE")) {
638 } else if (!strcasecmp(act, "NORMAL")) {
640 } else if (!strcasecmp(act, "FLOOD")) {
642 } else if (!strcasecmp(act, "ALL")) {
644 } else if (!strcasecmp(act, "CONTROLLER")) {
645 struct ofp_action_output *ca = (struct ofp_action_output *)ah;
647 if (act_len < sizeof *ca) {
648 ofp_fatal(0, "Insufficient room for controller action\n");
651 act_len = sizeof *ca;
652 ca->type = htons(OFPAT_OUTPUT);
653 ca->port = htons(OFPP_CONTROLLER);
655 /* Unless a numeric argument is specified, we send the whole
656 * packet to the controller. */
657 if (arg && (strspn(act, "0123456789") == strlen(act))) {
658 ca->max_len= htons(str_to_int(arg));
660 } else if (!strcasecmp(act, "LOCAL")) {
662 } else if (strspn(act, "0123456789") == strlen(act)) {
663 port = str_to_int(act);
665 ofp_fatal(0, "Unknown action: %s", act);
668 if (port != OFPP_MAX) {
669 struct ofp_action_output *oa = (struct ofp_action_output *)p;
671 if (act_len < sizeof *oa) {
672 ofp_fatal(0, "Insufficient room for output action\n");
675 act_len = sizeof *oa;
676 oa->type = htons(OFPAT_OUTPUT);
677 oa->port = htons(port);
680 ah->len = htons(act_len);
695 parse_protocol(const char *name, const struct protocol **p_out)
697 static const struct protocol protocols[] = {
698 { "ip", ETH_TYPE_IP },
699 { "arp", ETH_TYPE_ARP },
700 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
701 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
702 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
704 const struct protocol *p;
706 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
707 if (!strcmp(p->name, name)) {
719 enum { F_U8, F_U16, F_MAC, F_IP } type;
720 size_t offset, shift;
724 parse_field(const char *name, const struct field **f_out)
726 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
727 static const struct field fields[] = {
728 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
729 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
730 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
731 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
732 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
733 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
734 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
735 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
736 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
737 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
738 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
739 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
741 const struct field *f;
743 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
744 if (!strcmp(f->name, name)) {
754 str_to_flow(char *string, struct ofp_match *match,
755 struct ofp_action_header *actions, size_t *actions_len,
756 uint8_t *table_idx, uint16_t *priority,
757 uint16_t *idle_timeout, uint16_t *hard_timeout)
767 *priority = OFP_DEFAULT_PRIORITY;
770 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
773 *hard_timeout = OFP_FLOW_PERMANENT;
776 char *act_str = strstr(string, "action");
778 ofp_fatal(0, "must specify an action");
782 act_str = strchr(act_str, '=');
784 ofp_fatal(0, "must specify an action");
789 str_to_action(act_str, actions, actions_len);
791 memset(match, 0, sizeof *match);
792 wildcards = OFPFW_ALL;
793 for (name = strtok(string, "=, \t\r\n"); name;
794 name = strtok(NULL, "=, \t\r\n")) {
795 const struct protocol *p;
797 if (parse_protocol(name, &p)) {
798 wildcards &= ~OFPFW_DL_TYPE;
799 match->dl_type = htons(p->dl_type);
801 wildcards &= ~OFPFW_NW_PROTO;
802 match->nw_proto = p->nw_proto;
805 const struct field *f;
808 value = strtok(NULL, ", \t\r\n");
810 ofp_fatal(0, "field %s missing value", name);
813 if (table_idx && !strcmp(name, "table")) {
814 *table_idx = atoi(value);
815 } else if (priority && !strcmp(name, "priority")) {
816 *priority = atoi(value);
817 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
818 *idle_timeout = atoi(value);
819 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
820 *hard_timeout = atoi(value);
821 } else if (parse_field(name, &f)) {
822 void *data = (char *) match + f->offset;
823 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
824 wildcards |= f->wildcard;
826 wildcards &= ~f->wildcard;
827 if (f->type == F_U8) {
828 *(uint8_t *) data = str_to_int(value);
829 } else if (f->type == F_U16) {
830 *(uint16_t *) data = htons(str_to_int(value));
831 } else if (f->type == F_MAC) {
832 str_to_mac(value, data);
833 } else if (f->type == F_IP) {
834 wildcards |= str_to_ip(value, data) << f->shift;
840 ofp_fatal(0, "unknown keyword %s", name);
844 match->wildcards = htonl(wildcards);
847 static void do_dump_flows(const struct settings *s, int argc, char *argv[])
849 struct ofp_flow_stats_request *req;
850 struct ofpbuf *request;
852 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
853 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
854 &req->table_id, NULL, NULL, NULL);
855 memset(req->pad, 0, sizeof req->pad);
857 dump_stats_transaction(argv[1], request);
860 static void do_dump_aggregate(const struct settings *s, int argc,
863 struct ofp_aggregate_stats_request *req;
864 struct ofpbuf *request;
866 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
867 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
868 &req->table_id, NULL, NULL, NULL);
869 memset(req->pad, 0, sizeof req->pad);
871 dump_stats_transaction(argv[1], request);
874 static void do_add_snat(const struct settings *s, int argc, char *argv[])
877 struct ofpbuf *buffer;
878 struct nx_act_config *nac;
881 /* Parse and send. */
882 size = sizeof *nac + sizeof nac->snat[0];
883 nac = make_openflow(size, OFPT_VENDOR, &buffer);
885 nac->header.vendor = htonl(NX_VENDOR_ID);
886 nac->header.subtype = htonl(NXT_ACT_SET_CONFIG);
888 nac->type = htons(NXAST_SNAT);
889 nac->snat[0].command = NXSC_ADD;
890 nac->snat[0].port = htons(str_to_int(argv[2]));
891 nac->snat[0].mac_timeout = htons(0);
892 str_to_ip(argv[3], &nac->snat[0].ip_addr_start);
893 str_to_ip(argv[3], &nac->snat[0].ip_addr_end);
895 open_vconn(argv[1], &vconn);
896 send_openflow_buffer(vconn, buffer);
900 static void do_del_snat(const struct settings *s, int argc, char *argv[])
903 struct ofpbuf *buffer;
904 struct nx_act_config *nac;
907 /* Parse and send. */
908 size = sizeof *nac + sizeof nac->snat[0];
909 nac = make_openflow(size, OFPT_VENDOR, &buffer);
911 nac->header.vendor = htonl(NX_VENDOR_ID);
912 nac->header.subtype = htonl(NXT_ACT_SET_CONFIG);
914 nac->type = htons(NXAST_SNAT);
915 nac->snat[0].command = NXSC_DELETE;
916 nac->snat[0].port = htons(str_to_int(argv[2]));
917 nac->snat[0].mac_timeout = htons(0);
919 open_vconn(argv[1], &vconn);
920 send_openflow_buffer(vconn, buffer);
924 static void do_add_flow(const struct settings *s, int argc, char *argv[])
927 struct ofpbuf *buffer;
928 struct ofp_flow_mod *ofm;
929 uint16_t priority, idle_timeout, hard_timeout;
931 size_t actions_len = MAX_ACT_LEN;
933 /* Parse and send. */
934 size = sizeof *ofm + actions_len;
935 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
936 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
937 NULL, &priority, &idle_timeout, &hard_timeout);
938 ofm->command = htons(OFPFC_ADD);
939 ofm->idle_timeout = htons(idle_timeout);
940 ofm->hard_timeout = htons(hard_timeout);
941 ofm->buffer_id = htonl(UINT32_MAX);
942 ofm->priority = htons(priority);
943 ofm->reserved = htonl(0);
945 /* xxx Should we use the ofpbuf library? */
946 buffer->size -= MAX_ACT_LEN - actions_len;
948 open_vconn(argv[1], &vconn);
949 send_openflow_buffer(vconn, buffer);
953 static void do_add_flows(const struct settings *s, int argc, char *argv[])
959 file = fopen(argv[2], "r");
961 ofp_fatal(errno, "%s: open", argv[2]);
964 open_vconn(argv[1], &vconn);
965 while (fgets(line, sizeof line, file)) {
966 struct ofpbuf *buffer;
967 struct ofp_flow_mod *ofm;
968 uint16_t priority, idle_timeout, hard_timeout;
970 size_t actions_len = MAX_ACT_LEN;
974 /* Delete comments. */
975 comment = strchr(line, '#');
980 /* Drop empty lines. */
981 if (line[strspn(line, " \t\n")] == '\0') {
985 /* Parse and send. */
986 size = sizeof *ofm + actions_len;
987 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
988 str_to_flow(line, &ofm->match, &ofm->actions[0], &actions_len,
989 NULL, &priority, &idle_timeout, &hard_timeout);
990 ofm->command = htons(OFPFC_ADD);
991 ofm->idle_timeout = htons(idle_timeout);
992 ofm->hard_timeout = htons(hard_timeout);
993 ofm->buffer_id = htonl(UINT32_MAX);
994 ofm->priority = htons(priority);
995 ofm->reserved = htonl(0);
997 /* xxx Should we use the ofpbuf library? */
998 buffer->size -= MAX_ACT_LEN - actions_len;
1000 send_openflow_buffer(vconn, buffer);
1006 static void do_mod_flows(const struct settings *s, int argc, char *argv[])
1008 uint16_t priority, idle_timeout, hard_timeout;
1009 struct vconn *vconn;
1010 struct ofpbuf *buffer;
1011 struct ofp_flow_mod *ofm;
1013 size_t actions_len = MAX_ACT_LEN;
1015 /* Parse and send. */
1016 size = sizeof *ofm + actions_len;
1017 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
1018 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
1019 NULL, &priority, &idle_timeout, &hard_timeout);
1021 ofm->command = htons(OFPFC_MODIFY_STRICT);
1023 ofm->command = htons(OFPFC_MODIFY);
1025 ofm->idle_timeout = htons(idle_timeout);
1026 ofm->hard_timeout = htons(hard_timeout);
1027 ofm->buffer_id = htonl(UINT32_MAX);
1028 ofm->priority = htons(priority);
1029 ofm->reserved = htonl(0);
1031 /* xxx Should we use the buffer library? */
1032 buffer->size -= MAX_ACT_LEN - actions_len;
1034 open_vconn(argv[1], &vconn);
1035 send_openflow_buffer(vconn, buffer);
1039 static void do_del_flows(const struct settings *s, int argc, char *argv[])
1041 struct vconn *vconn;
1043 struct ofpbuf *buffer;
1044 struct ofp_flow_mod *ofm;
1047 /* Parse and send. */
1049 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
1050 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
1051 &priority, NULL, NULL);
1053 ofm->command = htons(OFPFC_DELETE_STRICT);
1055 ofm->command = htons(OFPFC_DELETE);
1057 ofm->idle_timeout = htons(0);
1058 ofm->hard_timeout = htons(0);
1059 ofm->buffer_id = htonl(UINT32_MAX);
1060 ofm->priority = htons(priority);
1061 ofm->reserved = htonl(0);
1063 open_vconn(argv[1], &vconn);
1064 send_openflow_buffer(vconn, buffer);
1069 do_monitor(const struct settings *s, int argc UNUSED, char *argv[])
1071 struct vconn *vconn;
1074 /* If the user specified, e.g., "nl:0", append ":1" to it to ensure that
1075 * the connection will subscribe to listen for asynchronous messages, such
1076 * as packet-in messages. */
1077 if (!strncmp(argv[1], "nl:", 3) && strrchr(argv[1], ':') == &argv[1][2]) {
1078 name = xasprintf("%s:1", argv[1]);
1082 open_vconn(argv[1], &vconn);
1085 run(vconn_recv_block(vconn, &b), "vconn_recv");
1086 ofp_print(stderr, b->data, b->size, 2);
1092 do_dump_ports(const struct settings *s, int argc, char *argv[])
1094 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1098 do_probe(const struct settings *s, int argc, char *argv[])
1100 struct ofpbuf *request;
1101 struct vconn *vconn;
1102 struct ofpbuf *reply;
1104 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1105 open_vconn(argv[1], &vconn);
1106 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1107 if (reply->size != request->size) {
1108 ofp_fatal(0, "reply does not match request");
1110 ofpbuf_delete(reply);
1115 do_mod_port(const struct settings *s, int argc, char *argv[])
1117 struct ofpbuf *request, *reply;
1118 struct ofp_switch_features *osf;
1119 struct ofp_port_mod *opm;
1120 struct vconn *vconn;
1127 /* Check if the argument is a port index. Otherwise, treat it as
1129 port_no = strtol(argv[2], &endptr, 10);
1130 if (port_no == 0 && endptr == argv[2]) {
1134 /* Send a "Features Request" to get the information we need in order
1135 * to modify the port. */
1136 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1137 open_vconn(argv[1], &vconn);
1138 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1141 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1143 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1144 if (port_no != -1) {
1145 /* Check argument as a port index */
1146 if (osf->ports[port_idx].port_no == htons(port_no)) {
1150 /* Check argument as an interface name */
1151 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1152 sizeof osf->ports[0].name)) {
1158 if (port_idx == n_ports) {
1159 ofp_fatal(0, "couldn't find monitored port: %s", argv[2]);
1162 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1163 opm->port_no = osf->ports[port_idx].port_no;
1164 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1165 opm->config = htonl(0);
1166 opm->mask = htonl(0);
1167 opm->advertise = htonl(0);
1169 printf("modifying port: %s\n", osf->ports[port_idx].name);
1171 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1172 opm->mask |= htonl(OFPPC_PORT_DOWN);
1173 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1174 sizeof MOD_PORT_CMD_DOWN)) {
1175 opm->mask |= htonl(OFPPC_PORT_DOWN);
1176 opm->config |= htonl(OFPPC_PORT_DOWN);
1177 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1178 sizeof MOD_PORT_CMD_FLOOD)) {
1179 opm->mask |= htonl(OFPPC_NO_FLOOD);
1180 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1181 sizeof MOD_PORT_CMD_NOFLOOD)) {
1182 opm->mask |= htonl(OFPPC_NO_FLOOD);
1183 opm->config |= htonl(OFPPC_NO_FLOOD);
1185 ofp_fatal(0, "unknown mod-port command '%s'", argv[3]);
1188 send_openflow_buffer(vconn, request);
1190 ofpbuf_delete(reply);
1195 do_ping(const struct settings *s, int argc, char *argv[])
1197 size_t max_payload = 65535 - sizeof(struct ofp_header);
1198 unsigned int payload;
1199 struct vconn *vconn;
1202 payload = argc > 2 ? atoi(argv[2]) : 64;
1203 if (payload > max_payload) {
1204 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1207 open_vconn(argv[1], &vconn);
1208 for (i = 0; i < 10; i++) {
1209 struct timeval start, end;
1210 struct ofpbuf *request, *reply;
1211 struct ofp_header *rq_hdr, *rpy_hdr;
1213 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1214 OFPT_ECHO_REQUEST, &request);
1215 random_bytes(rq_hdr + 1, payload);
1217 gettimeofday(&start, NULL);
1218 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1219 gettimeofday(&end, NULL);
1221 rpy_hdr = reply->data;
1222 if (reply->size != request->size
1223 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1224 || rpy_hdr->xid != rq_hdr->xid
1225 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1226 printf("Reply does not match request. Request:\n");
1227 ofp_print(stdout, request, request->size, 2);
1229 ofp_print(stdout, reply, reply->size, 2);
1231 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1232 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1233 (1000*(double)(end.tv_sec - start.tv_sec))
1234 + (.001*(end.tv_usec - start.tv_usec)));
1235 ofpbuf_delete(request);
1236 ofpbuf_delete(reply);
1242 do_benchmark(const struct settings *s, int argc, char *argv[])
1244 size_t max_payload = 65535 - sizeof(struct ofp_header);
1245 struct timeval start, end;
1246 unsigned int payload_size, message_size;
1247 struct vconn *vconn;
1252 payload_size = atoi(argv[2]);
1253 if (payload_size > max_payload) {
1254 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1256 message_size = sizeof(struct ofp_header) + payload_size;
1258 count = atoi(argv[3]);
1260 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1261 count, message_size, count * message_size);
1263 open_vconn(argv[1], &vconn);
1264 gettimeofday(&start, NULL);
1265 for (i = 0; i < count; i++) {
1266 struct ofpbuf *request, *reply;
1267 struct ofp_header *rq_hdr;
1269 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1270 memset(rq_hdr + 1, 0, payload_size);
1271 run(vconn_transact(vconn, request, &reply), "transact");
1272 ofpbuf_delete(reply);
1274 gettimeofday(&end, NULL);
1277 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1278 + (.001*(end.tv_usec - start.tv_usec)));
1279 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1280 duration, count / (duration / 1000.0),
1281 count * message_size / (duration / 1000.0));
1284 static void do_help(const struct settings *s, int argc UNUSED,
1285 char *argv[] UNUSED)
1290 static struct command all_commands[] = {
1292 { "adddp", 1, 1, do_add_dp },
1293 { "deldp", 1, 1, do_del_dp },
1294 { "addif", 2, INT_MAX, do_add_port },
1295 { "delif", 2, INT_MAX, do_del_port },
1298 { "show", 1, 1, do_show },
1299 { "status", 1, 2, do_status },
1301 { "help", 0, INT_MAX, do_help },
1302 { "monitor", 1, 1, do_monitor },
1303 { "dump-desc", 1, 1, do_dump_desc },
1304 { "dump-tables", 1, 1, do_dump_tables },
1305 { "dump-flows", 1, 2, do_dump_flows },
1306 { "dump-aggregate", 1, 2, do_dump_aggregate },
1307 { "add-snat", 3, 3, do_add_snat },
1308 { "del-snat", 2, 2, do_del_snat },
1309 { "add-flow", 2, 2, do_add_flow },
1310 { "add-flows", 2, 2, do_add_flows },
1311 { "mod-flows", 2, 2, do_mod_flows },
1312 { "del-flows", 1, 2, do_del_flows },
1313 { "dump-ports", 1, 1, do_dump_ports },
1314 { "mod-port", 3, 3, do_mod_port },
1315 { "probe", 1, 1, do_probe },
1316 { "ping", 1, 2, do_ping },
1317 { "benchmark", 3, 3, do_benchmark },
1318 { NULL, 0, 0, NULL },