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"
226 " add-snat SWITCH IFACE IP add SNAT config to IFACE\n"
227 " del-snat SWITCH IFACE delete SNAT config on IFACE\n"
229 " add-flow SWITCH FLOW add flow described by FLOW\n"
230 " add-flows SWITCH FILE add flows from FILE\n"
231 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
232 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
233 " monitor SWITCH print packets received from SWITCH\n"
234 "\nFor local datapaths, remote switches, and controllers:\n"
235 " probe VCONN probe whether VCONN is up\n"
236 " ping VCONN [N] latency of N-byte echos\n"
237 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
238 "where each SWITCH is an active OpenFlow connection method.\n",
239 program_name, program_name);
240 vconn_usage(true, false, false);
241 printf("\nOptions:\n"
242 " --strict use strict match for flow commands\n"
243 " -t, --timeout=SECS give up after SECS seconds\n"
244 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
245 " -v, --verbose set maximum verbosity level\n"
246 " -h, --help display this help message\n"
247 " -V, --version display version information\n");
251 static void run(int retval, const char *message, ...)
254 static void run(int retval, const char *message, ...)
259 fprintf(stderr, "%s: ", program_name);
260 va_start(args, message);
261 vfprintf(stderr, message, args);
264 fputs(": unexpected end of file\n", stderr);
266 fprintf(stderr, ": %s\n", strerror(retval));
274 /* Netlink-only commands. */
276 static int if_up(const char *netdev_name)
278 struct netdev *netdev;
281 retval = netdev_open(netdev_name, NETDEV_ETH_TYPE_NONE, &netdev);
283 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
284 netdev_close(netdev);
289 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
291 if (strncmp(name, "nl:", 3)
293 || name[strspn(name + 3, "0123456789") + 3]) {
294 ofp_fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
296 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
299 static void do_add_dp(const struct settings *s, int argc UNUSED, char *argv[])
302 open_nl_vconn(argv[1], false, &dp);
303 run(dpif_add_dp(&dp), "add_dp");
307 static void do_del_dp(const struct settings *s, int argc UNUSED, char *argv[])
310 open_nl_vconn(argv[1], false, &dp);
311 run(dpif_del_dp(&dp), "del_dp");
315 static void add_del_ports(int argc UNUSED, char *argv[],
316 int (*function)(struct dpif *, const char *netdev),
317 const char *operation, const char *preposition)
320 bool failure = false;
323 open_nl_vconn(argv[1], false, &dp);
324 for (i = 2; i < argc; i++) {
325 int retval = function(&dp, argv[i]);
327 ofp_error(retval, "failed to %s %s %s %s",
328 operation, argv[i], preposition, argv[1]);
338 static int ifup_and_add_port(struct dpif *dpif, const char *netdev)
340 int retval = if_up(netdev);
341 return retval ? retval : dpif_add_port(dpif, netdev);
344 static void do_add_port(const struct settings *s, int argc UNUSED,
347 add_del_ports(argc, argv, ifup_and_add_port, "add", "to");
350 static void do_del_port(const struct settings *s, int argc UNUSED,
353 add_del_ports(argc, argv, dpif_del_port, "remove", "from");
355 #endif /* HAVE_NETLINK */
357 /* Generic commands. */
360 open_vconn(const char *name, struct vconn **vconnp)
362 run(vconn_open_block(name, OFP_VERSION, vconnp), "connecting to %s", name);
366 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
368 struct ofp_stats_request *rq;
369 rq = make_openflow((offsetof(struct ofp_stats_request, body)
370 + body_len), OFPT_STATS_REQUEST, bufferp);
371 rq->type = htons(type);
372 rq->flags = htons(0);
377 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
379 update_openflow_length(buffer);
380 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
384 dump_transaction(const char *vconn_name, struct ofpbuf *request)
387 struct ofpbuf *reply;
389 update_openflow_length(request);
390 open_vconn(vconn_name, &vconn);
391 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
392 ofp_print(stdout, reply->data, reply->size, 1);
397 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
399 struct ofpbuf *request;
400 make_openflow(sizeof(struct ofp_header), request_type, &request);
401 dump_transaction(vconn_name, request);
405 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
407 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
411 open_vconn(vconn_name, &vconn);
412 send_openflow_buffer(vconn, request);
415 struct ofpbuf *reply;
417 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
418 recv_xid = ((struct ofp_header *) reply->data)->xid;
419 if (send_xid == recv_xid) {
420 struct ofp_stats_reply *osr;
422 ofp_print(stdout, reply->data, reply->size, 1);
424 osr = ofpbuf_at(reply, 0, sizeof *osr);
425 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
427 VLOG_DBG("received reply with xid %08"PRIx32" "
428 "!= expected %08"PRIx32, recv_xid, send_xid);
430 ofpbuf_delete(reply);
436 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
438 struct ofpbuf *request;
439 alloc_stats_request(0, stats_type, &request);
440 dump_stats_transaction(vconn_name, request);
444 do_show(const struct settings *s, int argc UNUSED, char *argv[])
446 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
447 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
451 do_status(const struct settings *s, int argc, char *argv[])
453 struct nicira_header *request, *reply;
457 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
458 request->vendor = htonl(NX_VENDOR_ID);
459 request->subtype = htonl(NXT_STATUS_REQUEST);
461 ofpbuf_put(b, argv[2], strlen(argv[2]));
463 open_vconn(argv[1], &vconn);
464 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
467 if (b->size < sizeof *reply) {
468 ofp_fatal(0, "short reply (%zu bytes)", b->size);
471 if (reply->header.type != OFPT_VENDOR
472 || reply->vendor != ntohl(NX_VENDOR_ID)
473 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
474 ofp_print(stderr, b->data, b->size, 2);
475 ofp_fatal(0, "bad reply");
478 fwrite(reply + 1, b->size, 1, stdout);
482 do_dump_desc(const struct settings *s, int argc, char *argv[])
484 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
488 do_dump_tables(const struct settings *s, int argc, char *argv[])
490 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
495 str_to_int(const char *str)
501 value = strtoul(str, &tail, 0);
502 if (errno == EINVAL || errno == ERANGE || *tail) {
503 ofp_fatal(0, "invalid numeric format %s", str);
509 str_to_mac(const char *str, uint8_t mac[6])
511 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
512 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
513 ofp_fatal(0, "invalid mac address %s", str);
518 str_to_ip(const char *str_, uint32_t *ip)
520 char *str = xstrdup(str_);
521 char *save_ptr = NULL;
522 const char *name, *netmask;
523 struct in_addr in_addr;
526 name = strtok_r(str, "//", &save_ptr);
527 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
529 ofp_fatal(0, "%s: could not convert to IP address", str);
531 *ip = in_addr.s_addr;
533 netmask = strtok_r(NULL, "//", &save_ptr);
536 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
537 &o[0], &o[1], &o[2], &o[3]) == 4) {
538 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
541 /* Find first 1-bit. */
542 for (i = 0; i < 32; i++) {
543 if (nm & (1u << i)) {
549 /* Verify that the rest of the bits are 1-bits. */
550 for (; i < 32; i++) {
551 if (!(nm & (1u << i))) {
552 ofp_fatal(0, "%s: %s is not a valid netmask",
557 int prefix = atoi(netmask);
558 if (prefix <= 0 || prefix > 32) {
559 ofp_fatal(0, "%s: network prefix bits not between 1 and 32",
562 n_wild = 32 - prefix;
573 str_to_action(char *str, struct ofp_action_header *actions,
576 size_t len = *actions_len;
578 char *saveptr = NULL;
579 uint8_t *p = (uint8_t *)actions;
581 memset(actions, 0, len);
582 for (act = strtok_r(str, ", \t\r\n", &saveptr);
583 (len >= sizeof(struct ofp_action_header)) && act;
584 act = strtok_r(NULL, ", \t\r\n", &saveptr))
587 struct ofp_action_header *ah = (struct ofp_action_header *)p;
588 int act_len = sizeof *ah;
591 /* Arguments are separated by colons */
592 arg = strchr(act, ':');
598 if (!strcasecmp(act, "mod_vlan_vid")) {
599 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
601 if (len < sizeof *va) {
602 ofp_fatal(0, "Insufficient room for vlan vid action\n");
605 act_len = sizeof *va;
606 va->type = htons(OFPAT_SET_VLAN_VID);
607 va->vlan_vid = htons(str_to_int(arg));
608 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
609 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
611 if (len < sizeof *va) {
612 ofp_fatal(0, "Insufficient room for vlan pcp action\n");
615 act_len = sizeof *va;
616 va->type = htons(OFPAT_SET_VLAN_PCP);
617 va->vlan_pcp = str_to_int(arg);
618 } else if (!strcasecmp(act, "strip_vlan")) {
619 ah->type = htons(OFPAT_STRIP_VLAN);
620 } else if (!strcasecmp(act, "output")) {
621 port = str_to_int(arg);
623 } else if (!strcasecmp(act, "nat")) {
624 struct nx_action_snat *sa = (struct nx_action_snat *)ah;
626 if (len < sizeof *sa) {
627 ofp_fatal(0, "Insufficient room for SNAT action\n");
630 if (str_to_int(arg) > OFPP_MAX) {
631 ofp_fatal(0, "Invalid nat port: %s\n", arg);
634 act_len = sizeof *sa;
635 sa->type = htons(OFPAT_VENDOR);
636 sa->vendor = htonl(NX_VENDOR_ID);
637 sa->subtype = htons(NXAST_SNAT);
638 sa->port = htons(str_to_int(arg));
640 } else if (!strcasecmp(act, "TABLE")) {
642 } else if (!strcasecmp(act, "NORMAL")) {
644 } else if (!strcasecmp(act, "FLOOD")) {
646 } else if (!strcasecmp(act, "ALL")) {
648 } else if (!strcasecmp(act, "CONTROLLER")) {
649 struct ofp_action_output *ca = (struct ofp_action_output *)ah;
651 if (act_len < sizeof *ca) {
652 ofp_fatal(0, "Insufficient room for controller action\n");
655 act_len = sizeof *ca;
656 ca->type = htons(OFPAT_OUTPUT);
657 ca->port = htons(OFPP_CONTROLLER);
659 /* Unless a numeric argument is specified, we send the whole
660 * packet to the controller. */
661 if (arg && (strspn(act, "0123456789") == strlen(act))) {
662 ca->max_len= htons(str_to_int(arg));
664 } else if (!strcasecmp(act, "LOCAL")) {
666 } else if (strspn(act, "0123456789") == strlen(act)) {
667 port = str_to_int(act);
669 ofp_fatal(0, "Unknown action: %s", act);
672 if (port != OFPP_MAX) {
673 struct ofp_action_output *oa = (struct ofp_action_output *)p;
675 if (act_len < sizeof *oa) {
676 ofp_fatal(0, "Insufficient room for output action\n");
679 act_len = sizeof *oa;
680 oa->type = htons(OFPAT_OUTPUT);
681 oa->port = htons(port);
684 ah->len = htons(act_len);
699 parse_protocol(const char *name, const struct protocol **p_out)
701 static const struct protocol protocols[] = {
702 { "ip", ETH_TYPE_IP },
703 { "arp", ETH_TYPE_ARP },
704 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
705 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
706 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
708 const struct protocol *p;
710 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
711 if (!strcmp(p->name, name)) {
723 enum { F_U8, F_U16, F_MAC, F_IP } type;
724 size_t offset, shift;
728 parse_field(const char *name, const struct field **f_out)
730 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
731 static const struct field fields[] = {
732 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
733 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
734 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
735 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
736 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
737 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
738 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
739 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
740 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
741 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
742 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
743 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
745 const struct field *f;
747 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
748 if (!strcmp(f->name, name)) {
758 str_to_flow(char *string, struct ofp_match *match,
759 struct ofp_action_header *actions, size_t *actions_len,
760 uint8_t *table_idx, uint16_t *priority,
761 uint16_t *idle_timeout, uint16_t *hard_timeout)
771 *priority = OFP_DEFAULT_PRIORITY;
774 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
777 *hard_timeout = OFP_FLOW_PERMANENT;
780 char *act_str = strstr(string, "action");
782 ofp_fatal(0, "must specify an action");
786 act_str = strchr(act_str, '=');
788 ofp_fatal(0, "must specify an action");
793 str_to_action(act_str, actions, actions_len);
795 memset(match, 0, sizeof *match);
796 wildcards = OFPFW_ALL;
797 for (name = strtok(string, "=, \t\r\n"); name;
798 name = strtok(NULL, "=, \t\r\n")) {
799 const struct protocol *p;
801 if (parse_protocol(name, &p)) {
802 wildcards &= ~OFPFW_DL_TYPE;
803 match->dl_type = htons(p->dl_type);
805 wildcards &= ~OFPFW_NW_PROTO;
806 match->nw_proto = p->nw_proto;
809 const struct field *f;
812 value = strtok(NULL, ", \t\r\n");
814 ofp_fatal(0, "field %s missing value", name);
817 if (table_idx && !strcmp(name, "table")) {
818 *table_idx = atoi(value);
819 } else if (priority && !strcmp(name, "priority")) {
820 *priority = atoi(value);
821 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
822 *idle_timeout = atoi(value);
823 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
824 *hard_timeout = atoi(value);
825 } else if (parse_field(name, &f)) {
826 void *data = (char *) match + f->offset;
827 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
828 wildcards |= f->wildcard;
830 wildcards &= ~f->wildcard;
831 if (f->type == F_U8) {
832 *(uint8_t *) data = str_to_int(value);
833 } else if (f->type == F_U16) {
834 *(uint16_t *) data = htons(str_to_int(value));
835 } else if (f->type == F_MAC) {
836 str_to_mac(value, data);
837 } else if (f->type == F_IP) {
838 wildcards |= str_to_ip(value, data) << f->shift;
844 ofp_fatal(0, "unknown keyword %s", name);
848 match->wildcards = htonl(wildcards);
851 static void do_dump_flows(const struct settings *s, int argc, char *argv[])
853 struct ofp_flow_stats_request *req;
854 struct ofpbuf *request;
856 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
857 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
858 &req->table_id, NULL, NULL, NULL);
859 memset(req->pad, 0, sizeof req->pad);
861 dump_stats_transaction(argv[1], request);
864 static void do_dump_aggregate(const struct settings *s, int argc,
867 struct ofp_aggregate_stats_request *req;
868 struct ofpbuf *request;
870 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
871 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, 0,
872 &req->table_id, NULL, NULL, NULL);
873 memset(req->pad, 0, sizeof req->pad);
875 dump_stats_transaction(argv[1], request);
879 static void do_add_snat(const struct settings *s, int argc, char *argv[])
882 struct ofpbuf *buffer;
883 struct nx_act_config *nac;
886 /* Parse and send. */
887 size = sizeof *nac + sizeof nac->snat[0];
888 nac = make_openflow(size, OFPT_VENDOR, &buffer);
890 nac->header.vendor = htonl(NX_VENDOR_ID);
891 nac->header.subtype = htonl(NXT_ACT_SET_CONFIG);
893 nac->type = htons(NXAST_SNAT);
894 nac->snat[0].command = NXSC_ADD;
895 nac->snat[0].port = htons(str_to_int(argv[2]));
896 nac->snat[0].mac_timeout = htons(0);
897 str_to_ip(argv[3], &nac->snat[0].ip_addr_start);
898 str_to_ip(argv[3], &nac->snat[0].ip_addr_end);
900 open_vconn(argv[1], &vconn);
901 send_openflow_buffer(vconn, buffer);
905 static void do_del_snat(const struct settings *s, int argc, char *argv[])
908 struct ofpbuf *buffer;
909 struct nx_act_config *nac;
912 /* Parse and send. */
913 size = sizeof *nac + sizeof nac->snat[0];
914 nac = make_openflow(size, OFPT_VENDOR, &buffer);
916 nac->header.vendor = htonl(NX_VENDOR_ID);
917 nac->header.subtype = htonl(NXT_ACT_SET_CONFIG);
919 nac->type = htons(NXAST_SNAT);
920 nac->snat[0].command = NXSC_DELETE;
921 nac->snat[0].port = htons(str_to_int(argv[2]));
922 nac->snat[0].mac_timeout = htons(0);
924 open_vconn(argv[1], &vconn);
925 send_openflow_buffer(vconn, buffer);
928 #endif /* SUPPORT_SNAT */
930 static void do_add_flow(const struct settings *s, int argc, char *argv[])
933 struct ofpbuf *buffer;
934 struct ofp_flow_mod *ofm;
935 uint16_t priority, idle_timeout, hard_timeout;
937 size_t actions_len = MAX_ACT_LEN;
939 /* Parse and send. */
940 size = sizeof *ofm + actions_len;
941 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
942 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
943 NULL, &priority, &idle_timeout, &hard_timeout);
944 ofm->command = htons(OFPFC_ADD);
945 ofm->idle_timeout = htons(idle_timeout);
946 ofm->hard_timeout = htons(hard_timeout);
947 ofm->buffer_id = htonl(UINT32_MAX);
948 ofm->priority = htons(priority);
949 ofm->reserved = htonl(0);
951 /* xxx Should we use the ofpbuf library? */
952 buffer->size -= MAX_ACT_LEN - actions_len;
954 open_vconn(argv[1], &vconn);
955 send_openflow_buffer(vconn, buffer);
959 static void do_add_flows(const struct settings *s, int argc, char *argv[])
965 file = fopen(argv[2], "r");
967 ofp_fatal(errno, "%s: open", argv[2]);
970 open_vconn(argv[1], &vconn);
971 while (fgets(line, sizeof line, file)) {
972 struct ofpbuf *buffer;
973 struct ofp_flow_mod *ofm;
974 uint16_t priority, idle_timeout, hard_timeout;
976 size_t actions_len = MAX_ACT_LEN;
980 /* Delete comments. */
981 comment = strchr(line, '#');
986 /* Drop empty lines. */
987 if (line[strspn(line, " \t\n")] == '\0') {
991 /* Parse and send. */
992 size = sizeof *ofm + actions_len;
993 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
994 str_to_flow(line, &ofm->match, &ofm->actions[0], &actions_len,
995 NULL, &priority, &idle_timeout, &hard_timeout);
996 ofm->command = htons(OFPFC_ADD);
997 ofm->idle_timeout = htons(idle_timeout);
998 ofm->hard_timeout = htons(hard_timeout);
999 ofm->buffer_id = htonl(UINT32_MAX);
1000 ofm->priority = htons(priority);
1001 ofm->reserved = htonl(0);
1003 /* xxx Should we use the ofpbuf library? */
1004 buffer->size -= MAX_ACT_LEN - actions_len;
1006 send_openflow_buffer(vconn, buffer);
1012 static void do_mod_flows(const struct settings *s, int argc, char *argv[])
1014 uint16_t priority, idle_timeout, hard_timeout;
1015 struct vconn *vconn;
1016 struct ofpbuf *buffer;
1017 struct ofp_flow_mod *ofm;
1019 size_t actions_len = MAX_ACT_LEN;
1021 /* Parse and send. */
1022 size = sizeof *ofm + actions_len;
1023 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
1024 str_to_flow(argv[2], &ofm->match, &ofm->actions[0], &actions_len,
1025 NULL, &priority, &idle_timeout, &hard_timeout);
1027 ofm->command = htons(OFPFC_MODIFY_STRICT);
1029 ofm->command = htons(OFPFC_MODIFY);
1031 ofm->idle_timeout = htons(idle_timeout);
1032 ofm->hard_timeout = htons(hard_timeout);
1033 ofm->buffer_id = htonl(UINT32_MAX);
1034 ofm->priority = htons(priority);
1035 ofm->reserved = htonl(0);
1037 /* xxx Should we use the buffer library? */
1038 buffer->size -= MAX_ACT_LEN - actions_len;
1040 open_vconn(argv[1], &vconn);
1041 send_openflow_buffer(vconn, buffer);
1045 static void do_del_flows(const struct settings *s, int argc, char *argv[])
1047 struct vconn *vconn;
1049 struct ofpbuf *buffer;
1050 struct ofp_flow_mod *ofm;
1053 /* Parse and send. */
1055 ofm = make_openflow(size, OFPT_FLOW_MOD, &buffer);
1056 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, 0, NULL,
1057 &priority, NULL, NULL);
1059 ofm->command = htons(OFPFC_DELETE_STRICT);
1061 ofm->command = htons(OFPFC_DELETE);
1063 ofm->idle_timeout = htons(0);
1064 ofm->hard_timeout = htons(0);
1065 ofm->buffer_id = htonl(UINT32_MAX);
1066 ofm->priority = htons(priority);
1067 ofm->reserved = htonl(0);
1069 open_vconn(argv[1], &vconn);
1070 send_openflow_buffer(vconn, buffer);
1075 do_monitor(const struct settings *s, int argc UNUSED, char *argv[])
1077 struct vconn *vconn;
1080 /* If the user specified, e.g., "nl:0", append ":1" to it to ensure that
1081 * the connection will subscribe to listen for asynchronous messages, such
1082 * as packet-in messages. */
1083 if (!strncmp(argv[1], "nl:", 3) && strrchr(argv[1], ':') == &argv[1][2]) {
1084 name = xasprintf("%s:1", argv[1]);
1088 open_vconn(argv[1], &vconn);
1091 run(vconn_recv_block(vconn, &b), "vconn_recv");
1092 ofp_print(stderr, b->data, b->size, 2);
1098 do_dump_ports(const struct settings *s, int argc, char *argv[])
1100 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1104 do_probe(const struct settings *s, int argc, char *argv[])
1106 struct ofpbuf *request;
1107 struct vconn *vconn;
1108 struct ofpbuf *reply;
1110 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1111 open_vconn(argv[1], &vconn);
1112 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1113 if (reply->size != request->size) {
1114 ofp_fatal(0, "reply does not match request");
1116 ofpbuf_delete(reply);
1121 do_mod_port(const struct settings *s, int argc, char *argv[])
1123 struct ofpbuf *request, *reply;
1124 struct ofp_switch_features *osf;
1125 struct ofp_port_mod *opm;
1126 struct vconn *vconn;
1133 /* Check if the argument is a port index. Otherwise, treat it as
1135 port_no = strtol(argv[2], &endptr, 10);
1136 if (port_no == 0 && endptr == argv[2]) {
1140 /* Send a "Features Request" to get the information we need in order
1141 * to modify the port. */
1142 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1143 open_vconn(argv[1], &vconn);
1144 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1147 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1149 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1150 if (port_no != -1) {
1151 /* Check argument as a port index */
1152 if (osf->ports[port_idx].port_no == htons(port_no)) {
1156 /* Check argument as an interface name */
1157 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1158 sizeof osf->ports[0].name)) {
1164 if (port_idx == n_ports) {
1165 ofp_fatal(0, "couldn't find monitored port: %s", argv[2]);
1168 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1169 opm->port_no = osf->ports[port_idx].port_no;
1170 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1171 opm->config = htonl(0);
1172 opm->mask = htonl(0);
1173 opm->advertise = htonl(0);
1175 printf("modifying port: %s\n", osf->ports[port_idx].name);
1177 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1178 opm->mask |= htonl(OFPPC_PORT_DOWN);
1179 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1180 sizeof MOD_PORT_CMD_DOWN)) {
1181 opm->mask |= htonl(OFPPC_PORT_DOWN);
1182 opm->config |= htonl(OFPPC_PORT_DOWN);
1183 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1184 sizeof MOD_PORT_CMD_FLOOD)) {
1185 opm->mask |= htonl(OFPPC_NO_FLOOD);
1186 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1187 sizeof MOD_PORT_CMD_NOFLOOD)) {
1188 opm->mask |= htonl(OFPPC_NO_FLOOD);
1189 opm->config |= htonl(OFPPC_NO_FLOOD);
1191 ofp_fatal(0, "unknown mod-port command '%s'", argv[3]);
1194 send_openflow_buffer(vconn, request);
1196 ofpbuf_delete(reply);
1201 do_ping(const struct settings *s, int argc, char *argv[])
1203 size_t max_payload = 65535 - sizeof(struct ofp_header);
1204 unsigned int payload;
1205 struct vconn *vconn;
1208 payload = argc > 2 ? atoi(argv[2]) : 64;
1209 if (payload > max_payload) {
1210 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1213 open_vconn(argv[1], &vconn);
1214 for (i = 0; i < 10; i++) {
1215 struct timeval start, end;
1216 struct ofpbuf *request, *reply;
1217 struct ofp_header *rq_hdr, *rpy_hdr;
1219 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1220 OFPT_ECHO_REQUEST, &request);
1221 random_bytes(rq_hdr + 1, payload);
1223 gettimeofday(&start, NULL);
1224 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1225 gettimeofday(&end, NULL);
1227 rpy_hdr = reply->data;
1228 if (reply->size != request->size
1229 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1230 || rpy_hdr->xid != rq_hdr->xid
1231 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1232 printf("Reply does not match request. Request:\n");
1233 ofp_print(stdout, request, request->size, 2);
1235 ofp_print(stdout, reply, reply->size, 2);
1237 printf("%d bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1238 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1239 (1000*(double)(end.tv_sec - start.tv_sec))
1240 + (.001*(end.tv_usec - start.tv_usec)));
1241 ofpbuf_delete(request);
1242 ofpbuf_delete(reply);
1248 do_benchmark(const struct settings *s, int argc, char *argv[])
1250 size_t max_payload = 65535 - sizeof(struct ofp_header);
1251 struct timeval start, end;
1252 unsigned int payload_size, message_size;
1253 struct vconn *vconn;
1258 payload_size = atoi(argv[2]);
1259 if (payload_size > max_payload) {
1260 ofp_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1262 message_size = sizeof(struct ofp_header) + payload_size;
1264 count = atoi(argv[3]);
1266 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1267 count, message_size, count * message_size);
1269 open_vconn(argv[1], &vconn);
1270 gettimeofday(&start, NULL);
1271 for (i = 0; i < count; i++) {
1272 struct ofpbuf *request, *reply;
1273 struct ofp_header *rq_hdr;
1275 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1276 memset(rq_hdr + 1, 0, payload_size);
1277 run(vconn_transact(vconn, request, &reply), "transact");
1278 ofpbuf_delete(reply);
1280 gettimeofday(&end, NULL);
1283 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1284 + (.001*(end.tv_usec - start.tv_usec)));
1285 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1286 duration, count / (duration / 1000.0),
1287 count * message_size / (duration / 1000.0));
1290 static void do_help(const struct settings *s, int argc UNUSED,
1291 char *argv[] UNUSED)
1296 static struct command all_commands[] = {
1298 { "adddp", 1, 1, do_add_dp },
1299 { "deldp", 1, 1, do_del_dp },
1300 { "addif", 2, INT_MAX, do_add_port },
1301 { "delif", 2, INT_MAX, do_del_port },
1304 { "show", 1, 1, do_show },
1305 { "status", 1, 2, do_status },
1307 { "help", 0, INT_MAX, do_help },
1308 { "monitor", 1, 1, do_monitor },
1309 { "dump-desc", 1, 1, do_dump_desc },
1310 { "dump-tables", 1, 1, do_dump_tables },
1311 { "dump-flows", 1, 2, do_dump_flows },
1312 { "dump-aggregate", 1, 2, do_dump_aggregate },
1314 { "add-snat", 3, 3, do_add_snat },
1315 { "del-snat", 2, 2, do_del_snat },
1317 { "add-flow", 2, 2, do_add_flow },
1318 { "add-flows", 2, 2, do_add_flows },
1319 { "mod-flows", 2, 2, do_mod_flows },
1320 { "del-flows", 1, 2, do_del_flows },
1321 { "dump-ports", 1, 1, do_dump_ports },
1322 { "mod-port", 3, 3, do_mod_port },
1323 { "probe", 1, 1, do_probe },
1324 { "ping", 1, 2, do_ping },
1325 { "benchmark", 3, 3, do_benchmark },
1326 { NULL, 0, 0, NULL },