2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
32 #include "command-line.h"
36 #include "dynamic-string.h"
40 #include "ofp-print.h"
42 #include "openflow/nicira-ext.h"
43 #include "openflow/openflow.h"
46 #include "socket-util.h"
49 #include "vconn-ssl.h"
54 #define THIS_MODULE VLM_ofctl
56 #define DEFAULT_IDLE_TIMEOUT 60
58 #define MOD_PORT_CMD_UP "up"
59 #define MOD_PORT_CMD_DOWN "down"
60 #define MOD_PORT_CMD_FLOOD "flood"
61 #define MOD_PORT_CMD_NOFLOOD "noflood"
64 /* Settings that may be configured by the user. */
66 bool strict; /* Use strict matching for flow mod commands */
73 void (*handler)(const struct settings *, int argc, char *argv[]);
76 static struct command all_commands[];
78 static void usage(void) NO_RETURN;
79 static void parse_options(int argc, char *argv[], struct settings *);
81 int main(int argc, char *argv[])
86 set_program_name(argv[0]);
89 parse_options(argc, argv, &s);
90 signal(SIGPIPE, SIG_IGN);
95 ovs_fatal(0, "missing command name; use --help for help");
97 for (p = all_commands; p->name != NULL; p++) {
98 if (!strcmp(p->name, argv[0])) {
100 if (n_arg < p->min_args)
101 ovs_fatal(0, "'%s' command requires at least %d arguments",
102 p->name, p->min_args);
103 else if (n_arg > p->max_args)
104 ovs_fatal(0, "'%s' command takes at most %d arguments",
105 p->name, p->max_args);
107 p->handler(&s, argc, argv);
108 if (ferror(stdout)) {
109 ovs_fatal(0, "write to stdout failed");
111 if (ferror(stderr)) {
112 ovs_fatal(0, "write to stderr failed");
118 ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
124 parse_options(int argc, char *argv[], struct settings *s)
127 OPT_STRICT = UCHAR_MAX + 1,
130 static struct option long_options[] = {
131 {"timeout", required_argument, 0, 't'},
132 {"strict", no_argument, 0, OPT_STRICT},
133 {"help", no_argument, 0, 'h'},
134 {"version", no_argument, 0, 'V'},
136 VCONN_SSL_LONG_OPTIONS
139 char *short_options = long_options_to_short_options(long_options);
141 /* Set defaults that we can figure out before parsing options. */
145 unsigned long int timeout;
148 c = getopt_long(argc, argv, short_options, long_options, NULL);
155 timeout = strtoul(optarg, NULL, 10);
157 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
168 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
176 VCONN_SSL_OPTION_HANDLERS
191 printf("%s: OpenFlow switch management utility\n"
192 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
193 "\nFor OpenFlow switches:\n"
194 " show SWITCH show OpenFlow information\n"
195 " status SWITCH [KEY] report statistics (about KEY)\n"
196 " dump-desc SWITCH print switch description\n"
197 " dump-tables SWITCH print table stats\n"
198 " mod-port SWITCH IFACE ACT modify port behavior\n"
199 " dump-ports SWITCH print port statistics\n"
200 " dump-flows SWITCH print all flow entries\n"
201 " dump-flows SWITCH FLOW print matching FLOWs\n"
202 " dump-aggregate SWITCH print aggregate flow statistics\n"
203 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
204 " add-flow SWITCH FLOW add flow described by FLOW\n"
205 " add-flows SWITCH FILE add flows from FILE\n"
206 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
207 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
208 " monitor SWITCH MISSLEN EXP print packets received from SWITCH\n"
209 " execute SWITCH CMD [ARG...] execute CMD with ARGS on SWITCH\n"
210 "\nFor OpenFlow switches and controllers:\n"
211 " probe VCONN probe whether VCONN is up\n"
212 " ping VCONN [N] latency of N-byte echos\n"
213 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
214 "where each SWITCH is an active OpenFlow connection method.\n",
215 program_name, program_name);
216 vconn_usage(true, false, false);
218 printf("\nOther options:\n"
219 " --strict use strict match for flow commands\n"
220 " -t, --timeout=SECS give up after SECS seconds\n"
221 " -h, --help display this help message\n"
222 " -V, --version display version information\n");
226 static void run(int retval, const char *message, ...)
229 static void run(int retval, const char *message, ...)
234 fprintf(stderr, "%s: ", program_name);
235 va_start(args, message);
236 vfprintf(stderr, message, args);
239 fputs(": unexpected end of file\n", stderr);
241 fprintf(stderr, ": %s\n", strerror(retval));
248 /* Generic commands. */
251 open_vconn(const char *name, struct vconn **vconnp)
256 if (strstr(name, ":")) {
257 run(vconn_open_block(name, OFP_VERSION, vconnp),
258 "connecting to %s", name);
259 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
260 char *vconn_name = xasprintf("unix:%s", name);
261 VLOG_INFO("connecting to %s", vconn_name);
262 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
263 "connecting to %s", vconn_name);
265 } else if (!dpif_open(name, &dpif)) {
266 char dpif_name[IF_NAMESIZE + 1];
270 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
271 "obtaining name of %s", dpif_name);
273 if (strcmp(dpif_name, name)) {
274 VLOG_INFO("datapath %s is named %s", name, dpif_name);
277 socket_name = xasprintf("%s/%s.mgmt", ovs_rundir, dpif_name);
278 if (stat(socket_name, &s)) {
279 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
281 } else if (!S_ISSOCK(s.st_mode)) {
282 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
286 vconn_name = xasprintf("unix:%s", socket_name);
287 VLOG_INFO("connecting to %s", vconn_name);
288 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
289 "connecting to %s", vconn_name);
293 ovs_fatal(0, "%s is not a valid connection method", name);
298 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
300 struct ofp_stats_request *rq;
301 rq = make_openflow((offsetof(struct ofp_stats_request, body)
302 + body_len), OFPT_STATS_REQUEST, bufferp);
303 rq->type = htons(type);
304 rq->flags = htons(0);
309 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
311 update_openflow_length(buffer);
312 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
316 dump_transaction(const char *vconn_name, struct ofpbuf *request)
319 struct ofpbuf *reply;
321 update_openflow_length(request);
322 open_vconn(vconn_name, &vconn);
323 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
324 ofp_print(stdout, reply->data, reply->size, 1);
329 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
331 struct ofpbuf *request;
332 make_openflow(sizeof(struct ofp_header), request_type, &request);
333 dump_transaction(vconn_name, request);
337 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
339 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
343 open_vconn(vconn_name, &vconn);
344 send_openflow_buffer(vconn, request);
347 struct ofpbuf *reply;
349 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
350 recv_xid = ((struct ofp_header *) reply->data)->xid;
351 if (send_xid == recv_xid) {
352 struct ofp_stats_reply *osr;
354 ofp_print(stdout, reply->data, reply->size, 1);
356 osr = ofpbuf_at(reply, 0, sizeof *osr);
357 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
359 VLOG_DBG("received reply with xid %08"PRIx32" "
360 "!= expected %08"PRIx32, recv_xid, send_xid);
362 ofpbuf_delete(reply);
368 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
370 struct ofpbuf *request;
371 alloc_stats_request(0, stats_type, &request);
372 dump_stats_transaction(vconn_name, request);
376 do_show(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
378 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
379 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
383 do_status(const struct settings *s UNUSED, int argc, char *argv[])
385 struct nicira_header *request, *reply;
389 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
390 request->vendor = htonl(NX_VENDOR_ID);
391 request->subtype = htonl(NXT_STATUS_REQUEST);
393 ofpbuf_put(b, argv[2], strlen(argv[2]));
394 update_openflow_length(b);
396 open_vconn(argv[1], &vconn);
397 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
400 if (b->size < sizeof *reply) {
401 ovs_fatal(0, "short reply (%zu bytes)", b->size);
404 if (reply->header.type != OFPT_VENDOR
405 || reply->vendor != ntohl(NX_VENDOR_ID)
406 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
407 ofp_print(stderr, b->data, b->size, 2);
408 ovs_fatal(0, "bad reply");
411 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
415 do_dump_desc(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
417 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
421 do_dump_tables(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
423 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
428 str_to_u32(const char *str)
434 value = strtoul(str, &tail, 0);
435 if (errno == EINVAL || errno == ERANGE || *tail) {
436 ovs_fatal(0, "invalid numeric format %s", str);
442 str_to_mac(const char *str, uint8_t mac[6])
444 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
445 != ETH_ADDR_SCAN_COUNT) {
446 ovs_fatal(0, "invalid mac address %s", str);
451 str_to_ip(const char *str_, uint32_t *ip)
453 char *str = xstrdup(str_);
454 char *save_ptr = NULL;
455 const char *name, *netmask;
456 struct in_addr in_addr;
459 name = strtok_r(str, "/", &save_ptr);
460 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
462 ovs_fatal(0, "%s: could not convert to IP address", str);
464 *ip = in_addr.s_addr;
466 netmask = strtok_r(NULL, "/", &save_ptr);
469 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
470 &o[0], &o[1], &o[2], &o[3]) == 4) {
471 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
474 /* Find first 1-bit. */
475 for (i = 0; i < 32; i++) {
476 if (nm & (1u << i)) {
482 /* Verify that the rest of the bits are 1-bits. */
483 for (; i < 32; i++) {
484 if (!(nm & (1u << i))) {
485 ovs_fatal(0, "%s: %s is not a valid netmask",
490 int prefix = atoi(netmask);
491 if (prefix <= 0 || prefix > 32) {
492 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
495 n_wild = 32 - prefix;
506 put_action(struct ofpbuf *b, size_t size, uint16_t type)
508 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
509 ah->type = htons(type);
510 ah->len = htons(size);
514 static struct ofp_action_output *
515 put_output_action(struct ofpbuf *b, uint16_t port)
517 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
518 oao->port = htons(port);
523 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
525 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
526 str_to_mac(addr, oada->dl_addr);
531 parse_port_name(const char *name, uint16_t *port)
537 static const struct pair pairs[] = {
538 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
544 DEF_PAIR(CONTROLLER),
549 static const int n_pairs = ARRAY_SIZE(pairs);
552 for (i = 0; i < n_pairs; i++) {
553 if (!strcasecmp(name, pairs[i].name)) {
554 *port = pairs[i].value;
562 str_to_action(char *str, struct ofpbuf *b)
565 char *saveptr = NULL;
569 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
570 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
575 ovs_fatal(0, "Drop actions must not be followed by other actions");
578 /* Arguments are separated by colons */
579 arg = strchr(act, ':');
585 if (!strcasecmp(act, "mod_vlan_vid")) {
586 struct ofp_action_vlan_vid *va;
587 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
588 va->vlan_vid = htons(str_to_u32(arg));
589 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
590 struct ofp_action_vlan_pcp *va;
591 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
592 va->vlan_pcp = str_to_u32(arg);
593 } else if (!strcasecmp(act, "strip_vlan")) {
594 struct ofp_action_header *ah;
595 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
596 ah->type = htons(OFPAT_STRIP_VLAN);
597 } else if (!strcasecmp(act, "mod_dl_src")) {
598 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
599 } else if (!strcasecmp(act, "mod_dl_dst")) {
600 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
601 } else if (!strcasecmp(act, "mod_nw_src")) {
602 struct ofp_action_nw_addr *na;
603 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
604 str_to_ip(arg, &na->nw_addr);
605 } else if (!strcasecmp(act, "mod_nw_dst")) {
606 struct ofp_action_nw_addr *na;
607 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
608 str_to_ip(arg, &na->nw_addr);
609 } else if (!strcasecmp(act, "mod_tp_src")) {
610 struct ofp_action_tp_port *ta;
611 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
612 ta->tp_port = htons(str_to_u32(arg));
613 } else if (!strcasecmp(act, "mod_tp_dst")) {
614 struct ofp_action_tp_port *ta;
615 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
616 ta->tp_port = htons(str_to_u32(arg));
617 } else if (!strcasecmp(act, "mod_nw_tos")) {
618 struct ofp_action_nw_tos *nt;
619 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
620 nt->nw_tos = str_to_u32(arg);
621 } else if (!strcasecmp(act, "output")) {
622 put_output_action(b, str_to_u32(arg));
623 } else if (!strcasecmp(act, "drop")) {
624 /* A drop action in OpenFlow occurs by just not setting
628 ovs_fatal(0, "Drop actions must not be preceded by other "
631 } else if (!strcasecmp(act, "CONTROLLER")) {
632 struct ofp_action_output *oao;
633 oao = put_output_action(b, OFPP_CONTROLLER);
635 /* Unless a numeric argument is specified, we send the whole
636 * packet to the controller. */
637 if (arg && (strspn(act, "0123456789") == strlen(act))) {
638 oao->max_len = htons(str_to_u32(arg));
640 oao->max_len = htons(UINT16_MAX);
642 } else if (parse_port_name(act, &port)) {
643 put_output_action(b, port);
644 } else if (strspn(act, "0123456789") == strlen(act)) {
645 put_output_action(b, str_to_u32(act));
647 ovs_fatal(0, "Unknown action: %s", act);
659 parse_protocol(const char *name, const struct protocol **p_out)
661 static const struct protocol protocols[] = {
662 { "ip", ETH_TYPE_IP, 0 },
663 { "arp", ETH_TYPE_ARP, 0 },
664 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
665 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
666 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
668 const struct protocol *p;
670 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
671 if (!strcmp(p->name, name)) {
683 enum { F_U8, F_U16, F_MAC, F_IP } type;
684 size_t offset, shift;
688 parse_field(const char *name, const struct field **f_out)
690 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
691 static const struct field fields[] = {
692 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
693 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
694 { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
695 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
696 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
697 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
698 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
699 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
700 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
701 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
702 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
703 { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
704 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
705 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
706 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
707 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
709 const struct field *f;
711 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
712 if (!strcmp(f->name, name)) {
722 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
723 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
724 uint16_t *idle_timeout, uint16_t *hard_timeout,
727 char *save_ptr = NULL;
735 *out_port = OFPP_NONE;
738 *priority = OFP_DEFAULT_PRIORITY;
741 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
744 *hard_timeout = OFP_FLOW_PERMANENT;
750 char *act_str = strstr(string, "action");
752 ovs_fatal(0, "must specify an action");
756 act_str = strchr(act_str, '=');
758 ovs_fatal(0, "must specify an action");
763 str_to_action(act_str, actions);
765 memset(match, 0, sizeof *match);
766 wildcards = OFPFW_ALL;
767 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
768 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
769 const struct protocol *p;
771 if (parse_protocol(name, &p)) {
772 wildcards &= ~OFPFW_DL_TYPE;
773 match->dl_type = htons(p->dl_type);
775 wildcards &= ~OFPFW_NW_PROTO;
776 match->nw_proto = p->nw_proto;
779 const struct field *f;
782 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
784 ovs_fatal(0, "field %s missing value", name);
787 if (table_idx && !strcmp(name, "table")) {
788 *table_idx = atoi(value);
789 } else if (out_port && !strcmp(name, "out_port")) {
790 *out_port = atoi(value);
791 } else if (priority && !strcmp(name, "priority")) {
792 *priority = atoi(value);
793 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
794 *idle_timeout = atoi(value);
795 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
796 *hard_timeout = atoi(value);
797 } else if (cookie && !strcmp(name, "cookie")) {
798 *cookie = atoi(value);
799 } else if (parse_field(name, &f)) {
800 void *data = (char *) match + f->offset;
801 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
802 wildcards |= f->wildcard;
804 wildcards &= ~f->wildcard;
805 if (f->wildcard == OFPFW_IN_PORT
806 && parse_port_name(value, (uint16_t *) data)) {
808 } else if (f->type == F_U8) {
809 *(uint8_t *) data = str_to_u32(value);
810 } else if (f->type == F_U16) {
811 *(uint16_t *) data = htons(str_to_u32(value));
812 } else if (f->type == F_MAC) {
813 str_to_mac(value, data);
814 } else if (f->type == F_IP) {
815 wildcards |= str_to_ip(value, data) << f->shift;
821 ovs_fatal(0, "unknown keyword %s", name);
825 match->wildcards = htonl(wildcards);
829 do_dump_flows(const struct settings *s UNUSED, int argc, char *argv[])
831 struct ofp_flow_stats_request *req;
833 struct ofpbuf *request;
835 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
836 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
837 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
838 memset(&req->pad, 0, sizeof req->pad);
839 req->out_port = htons(out_port);
841 dump_stats_transaction(argv[1], request);
845 do_dump_aggregate(const struct settings *s UNUSED, int argc, char *argv[])
847 struct ofp_aggregate_stats_request *req;
848 struct ofpbuf *request;
851 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
852 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
853 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
854 memset(&req->pad, 0, sizeof req->pad);
855 req->out_port = htons(out_port);
857 dump_stats_transaction(argv[1], request);
861 do_add_flow(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
864 struct ofpbuf *buffer;
865 struct ofp_flow_mod *ofm;
866 uint16_t priority, idle_timeout, hard_timeout;
868 struct ofp_match match;
870 /* Parse and send. str_to_flow() will expand and reallocate the data in
871 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
872 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
873 str_to_flow(argv[2], &match, buffer,
874 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
878 ofm->command = htons(OFPFC_ADD);
879 ofm->cookie = htonll(cookie);
880 ofm->idle_timeout = htons(idle_timeout);
881 ofm->hard_timeout = htons(hard_timeout);
882 ofm->buffer_id = htonl(UINT32_MAX);
883 ofm->priority = htons(priority);
885 open_vconn(argv[1], &vconn);
886 send_openflow_buffer(vconn, buffer);
891 do_add_flows(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
897 file = fopen(argv[2], "r");
899 ovs_fatal(errno, "%s: open", argv[2]);
902 open_vconn(argv[1], &vconn);
903 while (fgets(line, sizeof line, file)) {
904 struct ofpbuf *buffer;
905 struct ofp_flow_mod *ofm;
906 uint16_t priority, idle_timeout, hard_timeout;
908 struct ofp_match match;
912 /* Delete comments. */
913 comment = strchr(line, '#');
918 /* Drop empty lines. */
919 if (line[strspn(line, " \t\n")] == '\0') {
923 /* Parse and send. str_to_flow() will expand and reallocate the data
924 * in 'buffer', so we can't keep pointers to across the str_to_flow()
926 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
927 str_to_flow(line, &match, buffer,
928 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
932 ofm->command = htons(OFPFC_ADD);
933 ofm->cookie = htonll(cookie);
934 ofm->idle_timeout = htons(idle_timeout);
935 ofm->hard_timeout = htons(hard_timeout);
936 ofm->buffer_id = htonl(UINT32_MAX);
937 ofm->priority = htons(priority);
939 send_openflow_buffer(vconn, buffer);
946 do_mod_flows(const struct settings *s, int argc UNUSED, char *argv[])
948 uint16_t priority, idle_timeout, hard_timeout;
951 struct ofpbuf *buffer;
952 struct ofp_flow_mod *ofm;
953 struct ofp_match match;
955 /* Parse and send. str_to_flow() will expand and reallocate the data in
956 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
957 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
958 str_to_flow(argv[2], &match, buffer,
959 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
964 ofm->command = htons(OFPFC_MODIFY_STRICT);
966 ofm->command = htons(OFPFC_MODIFY);
968 ofm->idle_timeout = htons(idle_timeout);
969 ofm->hard_timeout = htons(hard_timeout);
970 ofm->cookie = htonll(cookie);
971 ofm->buffer_id = htonl(UINT32_MAX);
972 ofm->priority = htons(priority);
974 open_vconn(argv[1], &vconn);
975 send_openflow_buffer(vconn, buffer);
979 static void do_del_flows(const struct settings *s, int argc, char *argv[])
984 struct ofpbuf *buffer;
985 struct ofp_flow_mod *ofm;
987 /* Parse and send. */
988 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
989 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
990 &out_port, &priority, NULL, NULL, NULL);
992 ofm->command = htons(OFPFC_DELETE_STRICT);
994 ofm->command = htons(OFPFC_DELETE);
996 ofm->idle_timeout = htons(0);
997 ofm->hard_timeout = htons(0);
998 ofm->buffer_id = htonl(UINT32_MAX);
999 ofm->out_port = htons(out_port);
1000 ofm->priority = htons(priority);
1002 open_vconn(argv[1], &vconn);
1003 send_openflow_buffer(vconn, buffer);
1008 do_monitor(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1010 struct vconn *vconn;
1012 open_vconn(argv[1], &vconn);
1014 int miss_send_len = atoi(argv[2]);
1015 struct ofp_switch_config *osc;
1018 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1019 osc->miss_send_len = htons(miss_send_len);
1020 send_openflow_buffer(vconn, buf);
1024 run(vconn_recv_block(vconn, &b), "vconn_recv");
1025 ofp_print(stderr, b->data, b->size, 2);
1031 do_dump_ports(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1033 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1037 do_probe(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1039 struct ofpbuf *request;
1040 struct vconn *vconn;
1041 struct ofpbuf *reply;
1043 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1044 open_vconn(argv[1], &vconn);
1045 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1046 if (reply->size != sizeof(struct ofp_header)) {
1047 ovs_fatal(0, "reply does not match request");
1049 ofpbuf_delete(reply);
1054 do_mod_port(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1056 struct ofpbuf *request, *reply;
1057 struct ofp_switch_features *osf;
1058 struct ofp_port_mod *opm;
1059 struct vconn *vconn;
1066 /* Check if the argument is a port index. Otherwise, treat it as
1068 port_no = strtol(argv[2], &endptr, 10);
1069 if (port_no == 0 && endptr == argv[2]) {
1073 /* Send a "Features Request" to get the information we need in order
1074 * to modify the port. */
1075 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1076 open_vconn(argv[1], &vconn);
1077 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1080 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1082 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1083 if (port_no != -1) {
1084 /* Check argument as a port index */
1085 if (osf->ports[port_idx].port_no == htons(port_no)) {
1089 /* Check argument as an interface name */
1090 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1091 sizeof osf->ports[0].name)) {
1097 if (port_idx == n_ports) {
1098 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1101 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1102 opm->port_no = osf->ports[port_idx].port_no;
1103 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1104 opm->config = htonl(0);
1105 opm->mask = htonl(0);
1106 opm->advertise = htonl(0);
1108 printf("modifying port: %s\n", osf->ports[port_idx].name);
1110 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1111 opm->mask |= htonl(OFPPC_PORT_DOWN);
1112 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1113 sizeof MOD_PORT_CMD_DOWN)) {
1114 opm->mask |= htonl(OFPPC_PORT_DOWN);
1115 opm->config |= htonl(OFPPC_PORT_DOWN);
1116 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1117 sizeof MOD_PORT_CMD_FLOOD)) {
1118 opm->mask |= htonl(OFPPC_NO_FLOOD);
1119 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1120 sizeof MOD_PORT_CMD_NOFLOOD)) {
1121 opm->mask |= htonl(OFPPC_NO_FLOOD);
1122 opm->config |= htonl(OFPPC_NO_FLOOD);
1124 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1127 send_openflow_buffer(vconn, request);
1129 ofpbuf_delete(reply);
1134 do_ping(const struct settings *s UNUSED, int argc, char *argv[])
1136 size_t max_payload = 65535 - sizeof(struct ofp_header);
1137 unsigned int payload;
1138 struct vconn *vconn;
1141 payload = argc > 2 ? atoi(argv[2]) : 64;
1142 if (payload > max_payload) {
1143 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1146 open_vconn(argv[1], &vconn);
1147 for (i = 0; i < 10; i++) {
1148 struct timeval start, end;
1149 struct ofpbuf *request, *reply;
1150 struct ofp_header *rq_hdr, *rpy_hdr;
1152 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1153 OFPT_ECHO_REQUEST, &request);
1154 random_bytes(rq_hdr + 1, payload);
1156 gettimeofday(&start, NULL);
1157 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1158 gettimeofday(&end, NULL);
1160 rpy_hdr = reply->data;
1161 if (reply->size != request->size
1162 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1163 || rpy_hdr->xid != rq_hdr->xid
1164 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1165 printf("Reply does not match request. Request:\n");
1166 ofp_print(stdout, request, request->size, 2);
1168 ofp_print(stdout, reply, reply->size, 2);
1170 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1171 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1172 (1000*(double)(end.tv_sec - start.tv_sec))
1173 + (.001*(end.tv_usec - start.tv_usec)));
1174 ofpbuf_delete(request);
1175 ofpbuf_delete(reply);
1181 do_benchmark(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1183 size_t max_payload = 65535 - sizeof(struct ofp_header);
1184 struct timeval start, end;
1185 unsigned int payload_size, message_size;
1186 struct vconn *vconn;
1191 payload_size = atoi(argv[2]);
1192 if (payload_size > max_payload) {
1193 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1195 message_size = sizeof(struct ofp_header) + payload_size;
1197 count = atoi(argv[3]);
1199 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1200 count, message_size, count * message_size);
1202 open_vconn(argv[1], &vconn);
1203 gettimeofday(&start, NULL);
1204 for (i = 0; i < count; i++) {
1205 struct ofpbuf *request, *reply;
1206 struct ofp_header *rq_hdr;
1208 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1209 memset(rq_hdr + 1, 0, payload_size);
1210 run(vconn_transact(vconn, request, &reply), "transact");
1211 ofpbuf_delete(reply);
1213 gettimeofday(&end, NULL);
1216 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1217 + (.001*(end.tv_usec - start.tv_usec)));
1218 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1219 duration, count / (duration / 1000.0),
1220 count * message_size / (duration / 1000.0));
1224 do_execute(const struct settings *s UNUSED, int argc, char *argv[])
1226 struct vconn *vconn;
1227 struct ofpbuf *request;
1228 struct nicira_header *nicira;
1229 struct nx_command_reply *ncr;
1233 nicira = make_openflow(sizeof *nicira, OFPT_VENDOR, &request);
1234 xid = nicira->header.xid;
1235 nicira->vendor = htonl(NX_VENDOR_ID);
1236 nicira->subtype = htonl(NXT_COMMAND_REQUEST);
1237 ofpbuf_put(request, argv[2], strlen(argv[2]));
1238 for (i = 3; i < argc; i++) {
1239 ofpbuf_put_zeros(request, 1);
1240 ofpbuf_put(request, argv[i], strlen(argv[i]));
1242 update_openflow_length(request);
1244 open_vconn(argv[1], &vconn);
1245 run(vconn_send_block(vconn, request), "send");
1248 struct ofpbuf *reply;
1251 run(vconn_recv_xid(vconn, xid, &reply), "recv_xid");
1252 if (reply->size < sizeof *ncr) {
1253 ovs_fatal(0, "reply is too short (%zu bytes < %zu bytes)",
1254 reply->size, sizeof *ncr);
1257 if (ncr->nxh.header.type != OFPT_VENDOR
1258 || ncr->nxh.vendor != htonl(NX_VENDOR_ID)
1259 || ncr->nxh.subtype != htonl(NXT_COMMAND_REPLY)) {
1260 ovs_fatal(0, "reply is invalid");
1263 status = ntohl(ncr->status);
1264 if (status & NXT_STATUS_STARTED) {
1265 /* Wait for a second reply. */
1267 } else if (status & NXT_STATUS_EXITED) {
1268 fprintf(stderr, "process terminated normally with exit code %d",
1269 status & NXT_STATUS_EXITSTATUS);
1270 } else if (status & NXT_STATUS_SIGNALED) {
1271 fprintf(stderr, "process terminated by signal %d",
1272 status & NXT_STATUS_TERMSIG);
1273 } else if (status & NXT_STATUS_ERROR) {
1274 fprintf(stderr, "error executing command");
1276 fprintf(stderr, "process terminated for unknown reason");
1278 if (status & NXT_STATUS_COREDUMP) {
1279 fprintf(stderr, " (core dumped)");
1283 fwrite(ncr + 1, reply->size - sizeof *ncr, 1, stdout);
1289 do_help(const struct settings *s UNUSED, int argc UNUSED, char *argv[] UNUSED)
1294 static struct command all_commands[] = {
1295 { "show", 1, 1, do_show },
1296 { "status", 1, 2, do_status },
1297 { "monitor", 1, 3, do_monitor },
1298 { "dump-desc", 1, 1, do_dump_desc },
1299 { "dump-tables", 1, 1, do_dump_tables },
1300 { "dump-flows", 1, 2, do_dump_flows },
1301 { "dump-aggregate", 1, 2, do_dump_aggregate },
1302 { "add-flow", 2, 2, do_add_flow },
1303 { "add-flows", 2, 2, do_add_flows },
1304 { "mod-flows", 2, 2, do_mod_flows },
1305 { "del-flows", 1, 2, do_del_flows },
1306 { "dump-ports", 1, 1, do_dump_ports },
1307 { "mod-port", 3, 3, do_mod_port },
1308 { "probe", 1, 1, do_probe },
1309 { "ping", 1, 2, do_ping },
1310 { "benchmark", 3, 3, do_benchmark },
1311 { "execute", 2, INT_MAX, do_execute },
1312 { "help", 0, INT_MAX, do_help },
1313 { NULL, 0, 0, NULL },