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"
53 #define THIS_MODULE VLM_ofctl
55 #define DEFAULT_IDLE_TIMEOUT 60
57 #define MOD_PORT_CMD_UP "up"
58 #define MOD_PORT_CMD_DOWN "down"
59 #define MOD_PORT_CMD_FLOOD "flood"
60 #define MOD_PORT_CMD_NOFLOOD "noflood"
63 /* Settings that may be configured by the user. */
65 bool strict; /* Use strict matching for flow mod commands */
72 void (*handler)(const struct settings *, int argc, char *argv[]);
75 static struct command all_commands[];
77 static void usage(void) NO_RETURN;
78 static void parse_options(int argc, char *argv[], struct settings *);
80 int main(int argc, char *argv[])
85 set_program_name(argv[0]);
88 parse_options(argc, argv, &s);
89 signal(SIGPIPE, SIG_IGN);
94 ovs_fatal(0, "missing command name; use --help for help");
96 for (p = all_commands; p->name != NULL; p++) {
97 if (!strcmp(p->name, argv[0])) {
99 if (n_arg < p->min_args)
100 ovs_fatal(0, "'%s' command requires at least %d arguments",
101 p->name, p->min_args);
102 else if (n_arg > p->max_args)
103 ovs_fatal(0, "'%s' command takes at most %d arguments",
104 p->name, p->max_args);
106 p->handler(&s, argc, argv);
107 if (ferror(stdout)) {
108 ovs_fatal(0, "write to stdout failed");
110 if (ferror(stderr)) {
111 ovs_fatal(0, "write to stderr failed");
117 ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
123 parse_options(int argc, char *argv[], struct settings *s)
126 OPT_STRICT = UCHAR_MAX + 1,
129 static struct option long_options[] = {
130 {"timeout", required_argument, 0, 't'},
131 {"strict", no_argument, 0, OPT_STRICT},
132 {"help", no_argument, 0, 'h'},
133 {"version", no_argument, 0, 'V'},
135 VCONN_SSL_LONG_OPTIONS
138 char *short_options = long_options_to_short_options(long_options);
140 /* Set defaults that we can figure out before parsing options. */
144 unsigned long int timeout;
147 c = getopt_long(argc, argv, short_options, long_options, NULL);
154 timeout = strtoul(optarg, NULL, 10);
156 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
167 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
175 VCONN_SSL_OPTION_HANDLERS
190 printf("%s: OpenFlow switch management utility\n"
191 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
192 "\nFor OpenFlow switches:\n"
193 " show SWITCH show OpenFlow information\n"
194 " status SWITCH [KEY] report statistics (about KEY)\n"
195 " dump-desc SWITCH print switch description\n"
196 " dump-tables SWITCH print table stats\n"
197 " mod-port SWITCH IFACE ACT modify port behavior\n"
198 " dump-ports SWITCH print port statistics\n"
199 " dump-flows SWITCH print all flow entries\n"
200 " dump-flows SWITCH FLOW print matching FLOWs\n"
201 " dump-aggregate SWITCH print aggregate flow statistics\n"
202 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
203 " add-flow SWITCH FLOW add flow described by FLOW\n"
204 " add-flows SWITCH FILE add flows from FILE\n"
205 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
206 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
207 " monitor SWITCH MISSLEN EXP print packets received from SWITCH\n"
208 " execute SWITCH CMD [ARG...] execute CMD with ARGS on SWITCH\n"
209 "\nFor OpenFlow switches and controllers:\n"
210 " probe VCONN probe whether VCONN is up\n"
211 " ping VCONN [N] latency of N-byte echos\n"
212 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
213 "where each SWITCH is an active OpenFlow connection method.\n",
214 program_name, program_name);
215 vconn_usage(true, false, false);
217 printf("\nOther options:\n"
218 " --strict use strict match for flow commands\n"
219 " -t, --timeout=SECS give up after SECS seconds\n"
220 " -h, --help display this help message\n"
221 " -V, --version display version information\n");
225 static void run(int retval, const char *message, ...)
228 static void run(int retval, const char *message, ...)
233 fprintf(stderr, "%s: ", program_name);
234 va_start(args, message);
235 vfprintf(stderr, message, args);
238 fputs(": unexpected end of file\n", stderr);
240 fprintf(stderr, ": %s\n", strerror(retval));
247 /* Generic commands. */
250 open_vconn(const char *name, struct vconn **vconnp)
255 if (strstr(name, ":")) {
256 run(vconn_open_block(name, OFP_VERSION, vconnp),
257 "connecting to %s", name);
258 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
259 char *vconn_name = xasprintf("unix:%s", name);
260 VLOG_INFO("connecting to %s", vconn_name);
261 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
262 "connecting to %s", vconn_name);
264 } else if (!dpif_open(name, &dpif)) {
265 char dpif_name[IF_NAMESIZE + 1];
269 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
270 "obtaining name of %s", dpif_name);
272 if (strcmp(dpif_name, name)) {
273 VLOG_INFO("datapath %s is named %s", name, dpif_name);
276 socket_name = xasprintf("%s/%s.mgmt", ovs_rundir, dpif_name);
277 if (stat(socket_name, &s)) {
278 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
280 } else if (!S_ISSOCK(s.st_mode)) {
281 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
285 vconn_name = xasprintf("unix:%s", socket_name);
286 VLOG_INFO("connecting to %s", vconn_name);
287 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
288 "connecting to %s", vconn_name);
292 ovs_fatal(0, "%s is not a valid connection method", name);
297 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
299 struct ofp_stats_request *rq;
300 rq = make_openflow((offsetof(struct ofp_stats_request, body)
301 + body_len), OFPT_STATS_REQUEST, bufferp);
302 rq->type = htons(type);
303 rq->flags = htons(0);
308 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
310 update_openflow_length(buffer);
311 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
315 dump_transaction(const char *vconn_name, struct ofpbuf *request)
318 struct ofpbuf *reply;
320 update_openflow_length(request);
321 open_vconn(vconn_name, &vconn);
322 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
323 ofp_print(stdout, reply->data, reply->size, 1);
328 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
330 struct ofpbuf *request;
331 make_openflow(sizeof(struct ofp_header), request_type, &request);
332 dump_transaction(vconn_name, request);
336 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
338 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
342 open_vconn(vconn_name, &vconn);
343 send_openflow_buffer(vconn, request);
346 struct ofpbuf *reply;
348 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
349 recv_xid = ((struct ofp_header *) reply->data)->xid;
350 if (send_xid == recv_xid) {
351 struct ofp_stats_reply *osr;
353 ofp_print(stdout, reply->data, reply->size, 1);
355 osr = ofpbuf_at(reply, 0, sizeof *osr);
356 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
358 VLOG_DBG("received reply with xid %08"PRIx32" "
359 "!= expected %08"PRIx32, recv_xid, send_xid);
361 ofpbuf_delete(reply);
367 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
369 struct ofpbuf *request;
370 alloc_stats_request(0, stats_type, &request);
371 dump_stats_transaction(vconn_name, request);
375 do_show(const struct settings *s OVS_UNUSED, int argc OVS_UNUSED, char *argv[])
377 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
378 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
382 do_status(const struct settings *s OVS_UNUSED, int argc, char *argv[])
384 struct nicira_header *request, *reply;
388 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
389 request->vendor = htonl(NX_VENDOR_ID);
390 request->subtype = htonl(NXT_STATUS_REQUEST);
392 ofpbuf_put(b, argv[2], strlen(argv[2]));
393 update_openflow_length(b);
395 open_vconn(argv[1], &vconn);
396 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
399 if (b->size < sizeof *reply) {
400 ovs_fatal(0, "short reply (%zu bytes)", b->size);
403 if (reply->header.type != OFPT_VENDOR
404 || reply->vendor != ntohl(NX_VENDOR_ID)
405 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
406 ofp_print(stderr, b->data, b->size, 2);
407 ovs_fatal(0, "bad reply");
410 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
414 do_dump_desc(const struct settings *s OVS_UNUSED,
415 int argc OVS_UNUSED, char *argv[])
417 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
421 do_dump_tables(const struct settings *s OVS_UNUSED,
422 int argc OVS_UNUSED, char *argv[])
424 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
429 str_to_u32(const char *str)
435 value = strtoul(str, &tail, 0);
436 if (errno == EINVAL || errno == ERANGE || *tail) {
437 ovs_fatal(0, "invalid numeric format %s", str);
443 str_to_mac(const char *str, uint8_t mac[6])
445 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
446 != ETH_ADDR_SCAN_COUNT) {
447 ovs_fatal(0, "invalid mac address %s", str);
452 str_to_ip(const char *str_, uint32_t *ip)
454 char *str = xstrdup(str_);
455 char *save_ptr = NULL;
456 const char *name, *netmask;
457 struct in_addr in_addr;
460 name = strtok_r(str, "/", &save_ptr);
461 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
463 ovs_fatal(0, "%s: could not convert to IP address", str);
465 *ip = in_addr.s_addr;
467 netmask = strtok_r(NULL, "/", &save_ptr);
470 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
471 &o[0], &o[1], &o[2], &o[3]) == 4) {
472 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
475 /* Find first 1-bit. */
476 for (i = 0; i < 32; i++) {
477 if (nm & (1u << i)) {
483 /* Verify that the rest of the bits are 1-bits. */
484 for (; i < 32; i++) {
485 if (!(nm & (1u << i))) {
486 ovs_fatal(0, "%s: %s is not a valid netmask",
491 int prefix = atoi(netmask);
492 if (prefix <= 0 || prefix > 32) {
493 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
496 n_wild = 32 - prefix;
507 put_action(struct ofpbuf *b, size_t size, uint16_t type)
509 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
510 ah->type = htons(type);
511 ah->len = htons(size);
515 static struct ofp_action_output *
516 put_output_action(struct ofpbuf *b, uint16_t port)
518 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
519 oao->port = htons(port);
524 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
526 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
527 str_to_mac(addr, oada->dl_addr);
532 parse_port_name(const char *name, uint16_t *port)
538 static const struct pair pairs[] = {
539 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
545 DEF_PAIR(CONTROLLER),
550 static const int n_pairs = ARRAY_SIZE(pairs);
553 for (i = 0; i < n_pairs; i++) {
554 if (!strcasecmp(name, pairs[i].name)) {
555 *port = pairs[i].value;
563 str_to_action(char *str, struct ofpbuf *b)
566 char *saveptr = NULL;
570 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
571 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
576 ovs_fatal(0, "Drop actions must not be followed by other actions");
579 /* Arguments are separated by colons */
580 arg = strchr(act, ':');
586 if (!strcasecmp(act, "mod_vlan_vid")) {
587 struct ofp_action_vlan_vid *va;
588 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
589 va->vlan_vid = htons(str_to_u32(arg));
590 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
591 struct ofp_action_vlan_pcp *va;
592 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
593 va->vlan_pcp = str_to_u32(arg);
594 } else if (!strcasecmp(act, "strip_vlan")) {
595 struct ofp_action_header *ah;
596 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
597 ah->type = htons(OFPAT_STRIP_VLAN);
598 } else if (!strcasecmp(act, "mod_dl_src")) {
599 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
600 } else if (!strcasecmp(act, "mod_dl_dst")) {
601 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
602 } else if (!strcasecmp(act, "mod_nw_src")) {
603 struct ofp_action_nw_addr *na;
604 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
605 str_to_ip(arg, &na->nw_addr);
606 } else if (!strcasecmp(act, "mod_nw_dst")) {
607 struct ofp_action_nw_addr *na;
608 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
609 str_to_ip(arg, &na->nw_addr);
610 } else if (!strcasecmp(act, "mod_tp_src")) {
611 struct ofp_action_tp_port *ta;
612 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
613 ta->tp_port = htons(str_to_u32(arg));
614 } else if (!strcasecmp(act, "mod_tp_dst")) {
615 struct ofp_action_tp_port *ta;
616 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
617 ta->tp_port = htons(str_to_u32(arg));
618 } else if (!strcasecmp(act, "output")) {
619 put_output_action(b, str_to_u32(arg));
620 } else if (!strcasecmp(act, "drop")) {
621 /* A drop action in OpenFlow occurs by just not setting
625 ovs_fatal(0, "Drop actions must not be preceded by other "
628 } else if (!strcasecmp(act, "CONTROLLER")) {
629 struct ofp_action_output *oao;
630 oao = put_output_action(b, OFPP_CONTROLLER);
632 /* Unless a numeric argument is specified, we send the whole
633 * packet to the controller. */
634 if (arg && (strspn(act, "0123456789") == strlen(act))) {
635 oao->max_len = htons(str_to_u32(arg));
637 oao->max_len = htons(UINT16_MAX);
639 } else if (parse_port_name(act, &port)) {
640 put_output_action(b, port);
641 } else if (strspn(act, "0123456789") == strlen(act)) {
642 put_output_action(b, str_to_u32(act));
644 ovs_fatal(0, "Unknown action: %s", act);
656 parse_protocol(const char *name, const struct protocol **p_out)
658 static const struct protocol protocols[] = {
659 { "ip", ETH_TYPE_IP, 0 },
660 { "arp", ETH_TYPE_ARP, 0 },
661 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
662 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
663 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
665 const struct protocol *p;
667 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
668 if (!strcmp(p->name, name)) {
680 enum { F_U8, F_U16, F_MAC, F_IP } type;
681 size_t offset, shift;
685 parse_field(const char *name, const struct field **f_out)
687 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
688 static const struct field fields[] = {
689 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
690 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
691 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
692 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
693 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
694 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
695 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
696 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
697 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
698 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
699 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
700 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
701 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
702 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
704 const struct field *f;
706 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
707 if (!strcmp(f->name, name)) {
717 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
718 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
719 uint16_t *idle_timeout, uint16_t *hard_timeout)
721 char *save_ptr = NULL;
729 *out_port = OFPP_NONE;
732 *priority = OFP_DEFAULT_PRIORITY;
735 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
738 *hard_timeout = OFP_FLOW_PERMANENT;
741 char *act_str = strstr(string, "action");
743 ovs_fatal(0, "must specify an action");
747 act_str = strchr(act_str, '=');
749 ovs_fatal(0, "must specify an action");
754 str_to_action(act_str, actions);
756 memset(match, 0, sizeof *match);
757 wildcards = OFPFW_ALL;
758 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
759 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
760 const struct protocol *p;
762 if (parse_protocol(name, &p)) {
763 wildcards &= ~OFPFW_DL_TYPE;
764 match->dl_type = htons(p->dl_type);
766 wildcards &= ~OFPFW_NW_PROTO;
767 match->nw_proto = p->nw_proto;
770 const struct field *f;
773 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
775 ovs_fatal(0, "field %s missing value", name);
778 if (table_idx && !strcmp(name, "table")) {
779 *table_idx = atoi(value);
780 } else if (out_port && !strcmp(name, "out_port")) {
781 *out_port = atoi(value);
782 } else if (priority && !strcmp(name, "priority")) {
783 *priority = atoi(value);
784 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
785 *idle_timeout = atoi(value);
786 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
787 *hard_timeout = atoi(value);
788 } else if (parse_field(name, &f)) {
789 void *data = (char *) match + f->offset;
790 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
791 wildcards |= f->wildcard;
793 wildcards &= ~f->wildcard;
794 if (f->wildcard == OFPFW_IN_PORT
795 && parse_port_name(value, (uint16_t *) data)) {
797 } else if (f->type == F_U8) {
798 *(uint8_t *) data = str_to_u32(value);
799 } else if (f->type == F_U16) {
800 *(uint16_t *) data = htons(str_to_u32(value));
801 } else if (f->type == F_MAC) {
802 str_to_mac(value, data);
803 } else if (f->type == F_IP) {
804 wildcards |= str_to_ip(value, data) << f->shift;
810 ovs_fatal(0, "unknown keyword %s", name);
814 match->wildcards = htonl(wildcards);
818 do_dump_flows(const struct settings *s OVS_UNUSED, int argc, char *argv[])
820 struct ofp_flow_stats_request *req;
822 struct ofpbuf *request;
824 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
825 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
826 &req->table_id, &out_port, NULL, NULL, NULL);
827 memset(&req->pad, 0, sizeof req->pad);
828 req->out_port = htons(out_port);
830 dump_stats_transaction(argv[1], request);
834 do_dump_aggregate(const struct settings *s OVS_UNUSED, int argc, char *argv[])
836 struct ofp_aggregate_stats_request *req;
837 struct ofpbuf *request;
840 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
841 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
842 &req->table_id, &out_port, NULL, NULL, NULL);
843 memset(&req->pad, 0, sizeof req->pad);
844 req->out_port = htons(out_port);
846 dump_stats_transaction(argv[1], request);
850 do_add_flow(const struct settings *s OVS_UNUSED, int argc OVS_UNUSED,
854 struct ofpbuf *buffer;
855 struct ofp_flow_mod *ofm;
856 uint16_t priority, idle_timeout, hard_timeout;
857 struct ofp_match match;
859 /* Parse and send. str_to_flow() will expand and reallocate the data in
860 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
861 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
862 str_to_flow(argv[2], &match, buffer,
863 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
866 ofm->command = htons(OFPFC_ADD);
867 ofm->idle_timeout = htons(idle_timeout);
868 ofm->hard_timeout = htons(hard_timeout);
869 ofm->buffer_id = htonl(UINT32_MAX);
870 ofm->priority = htons(priority);
871 ofm->reserved = htonl(0);
873 open_vconn(argv[1], &vconn);
874 send_openflow_buffer(vconn, buffer);
879 do_add_flows(const struct settings *s OVS_UNUSED, int argc OVS_UNUSED,
886 file = fopen(argv[2], "r");
888 ovs_fatal(errno, "%s: open", argv[2]);
891 open_vconn(argv[1], &vconn);
892 while (fgets(line, sizeof line, file)) {
893 struct ofpbuf *buffer;
894 struct ofp_flow_mod *ofm;
895 uint16_t priority, idle_timeout, hard_timeout;
896 struct ofp_match match;
900 /* Delete comments. */
901 comment = strchr(line, '#');
906 /* Drop empty lines. */
907 if (line[strspn(line, " \t\n")] == '\0') {
911 /* Parse and send. str_to_flow() will expand and reallocate the data
912 * in 'buffer', so we can't keep pointers to across the str_to_flow()
914 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
915 str_to_flow(line, &match, buffer,
916 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
919 ofm->command = htons(OFPFC_ADD);
920 ofm->idle_timeout = htons(idle_timeout);
921 ofm->hard_timeout = htons(hard_timeout);
922 ofm->buffer_id = htonl(UINT32_MAX);
923 ofm->priority = htons(priority);
924 ofm->reserved = htonl(0);
926 send_openflow_buffer(vconn, buffer);
933 do_mod_flows(const struct settings *s, int argc OVS_UNUSED, char *argv[])
935 uint16_t priority, idle_timeout, hard_timeout;
937 struct ofpbuf *buffer;
938 struct ofp_flow_mod *ofm;
939 struct ofp_match match;
941 /* Parse and send. str_to_flow() will expand and reallocate the data in
942 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
943 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
944 str_to_flow(argv[2], &match, buffer,
945 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
949 ofm->command = htons(OFPFC_MODIFY_STRICT);
951 ofm->command = htons(OFPFC_MODIFY);
953 ofm->idle_timeout = htons(idle_timeout);
954 ofm->hard_timeout = htons(hard_timeout);
955 ofm->buffer_id = htonl(UINT32_MAX);
956 ofm->priority = htons(priority);
957 ofm->reserved = htonl(0);
959 open_vconn(argv[1], &vconn);
960 send_openflow_buffer(vconn, buffer);
964 static void do_del_flows(const struct settings *s, int argc, char *argv[])
969 struct ofpbuf *buffer;
970 struct ofp_flow_mod *ofm;
972 /* Parse and send. */
973 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
974 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
975 &out_port, &priority, NULL, NULL);
977 ofm->command = htons(OFPFC_DELETE_STRICT);
979 ofm->command = htons(OFPFC_DELETE);
981 ofm->idle_timeout = htons(0);
982 ofm->hard_timeout = htons(0);
983 ofm->buffer_id = htonl(UINT32_MAX);
984 ofm->out_port = htons(out_port);
985 ofm->priority = htons(priority);
986 ofm->reserved = htonl(0);
988 open_vconn(argv[1], &vconn);
989 send_openflow_buffer(vconn, buffer);
994 do_monitor(const struct settings *s OVS_UNUSED,
995 int argc OVS_UNUSED, char *argv[])
999 open_vconn(argv[1], &vconn);
1001 int miss_send_len = atoi(argv[2]);
1002 int send_flow_exp = argc > 3 ? atoi(argv[3]) : 0;
1003 struct ofp_switch_config *osc;
1006 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1007 osc->flags = htons(send_flow_exp ? OFPC_SEND_FLOW_EXP : 0);
1008 osc->miss_send_len = htons(miss_send_len);
1009 send_openflow_buffer(vconn, buf);
1013 run(vconn_recv_block(vconn, &b), "vconn_recv");
1014 ofp_print(stderr, b->data, b->size, 2);
1020 do_dump_ports(const struct settings *s OVS_UNUSED,
1021 int argc OVS_UNUSED, char *argv[])
1023 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1027 do_probe(const struct settings *s OVS_UNUSED,
1028 int argc OVS_UNUSED, char *argv[])
1030 struct ofpbuf *request;
1031 struct vconn *vconn;
1032 struct ofpbuf *reply;
1034 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1035 open_vconn(argv[1], &vconn);
1036 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1037 if (reply->size != sizeof(struct ofp_header)) {
1038 ovs_fatal(0, "reply does not match request");
1040 ofpbuf_delete(reply);
1045 do_mod_port(const struct settings *s OVS_UNUSED,
1046 int argc OVS_UNUSED, char *argv[])
1048 struct ofpbuf *request, *reply;
1049 struct ofp_switch_features *osf;
1050 struct ofp_port_mod *opm;
1051 struct vconn *vconn;
1058 /* Check if the argument is a port index. Otherwise, treat it as
1060 port_no = strtol(argv[2], &endptr, 10);
1061 if (port_no == 0 && endptr == argv[2]) {
1065 /* Send a "Features Request" to get the information we need in order
1066 * to modify the port. */
1067 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1068 open_vconn(argv[1], &vconn);
1069 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1072 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1074 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1075 if (port_no != -1) {
1076 /* Check argument as a port index */
1077 if (osf->ports[port_idx].port_no == htons(port_no)) {
1081 /* Check argument as an interface name */
1082 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1083 sizeof osf->ports[0].name)) {
1089 if (port_idx == n_ports) {
1090 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1093 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1094 opm->port_no = osf->ports[port_idx].port_no;
1095 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1096 opm->config = htonl(0);
1097 opm->mask = htonl(0);
1098 opm->advertise = htonl(0);
1100 printf("modifying port: %s\n", osf->ports[port_idx].name);
1102 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1103 opm->mask |= htonl(OFPPC_PORT_DOWN);
1104 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1105 sizeof MOD_PORT_CMD_DOWN)) {
1106 opm->mask |= htonl(OFPPC_PORT_DOWN);
1107 opm->config |= htonl(OFPPC_PORT_DOWN);
1108 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1109 sizeof MOD_PORT_CMD_FLOOD)) {
1110 opm->mask |= htonl(OFPPC_NO_FLOOD);
1111 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1112 sizeof MOD_PORT_CMD_NOFLOOD)) {
1113 opm->mask |= htonl(OFPPC_NO_FLOOD);
1114 opm->config |= htonl(OFPPC_NO_FLOOD);
1116 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1119 send_openflow_buffer(vconn, request);
1121 ofpbuf_delete(reply);
1126 do_ping(const struct settings *s OVS_UNUSED, int argc, char *argv[])
1128 size_t max_payload = 65535 - sizeof(struct ofp_header);
1129 unsigned int payload;
1130 struct vconn *vconn;
1133 payload = argc > 2 ? atoi(argv[2]) : 64;
1134 if (payload > max_payload) {
1135 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1138 open_vconn(argv[1], &vconn);
1139 for (i = 0; i < 10; i++) {
1140 struct timeval start, end;
1141 struct ofpbuf *request, *reply;
1142 struct ofp_header *rq_hdr, *rpy_hdr;
1144 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1145 OFPT_ECHO_REQUEST, &request);
1146 random_bytes(rq_hdr + 1, payload);
1148 gettimeofday(&start, NULL);
1149 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1150 gettimeofday(&end, NULL);
1152 rpy_hdr = reply->data;
1153 if (reply->size != request->size
1154 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1155 || rpy_hdr->xid != rq_hdr->xid
1156 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1157 printf("Reply does not match request. Request:\n");
1158 ofp_print(stdout, request, request->size, 2);
1160 ofp_print(stdout, reply, reply->size, 2);
1162 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1163 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1164 (1000*(double)(end.tv_sec - start.tv_sec))
1165 + (.001*(end.tv_usec - start.tv_usec)));
1166 ofpbuf_delete(request);
1167 ofpbuf_delete(reply);
1173 do_benchmark(const struct settings *s OVS_UNUSED,
1174 int argc OVS_UNUSED, char *argv[])
1176 size_t max_payload = 65535 - sizeof(struct ofp_header);
1177 struct timeval start, end;
1178 unsigned int payload_size, message_size;
1179 struct vconn *vconn;
1184 payload_size = atoi(argv[2]);
1185 if (payload_size > max_payload) {
1186 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1188 message_size = sizeof(struct ofp_header) + payload_size;
1190 count = atoi(argv[3]);
1192 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1193 count, message_size, count * message_size);
1195 open_vconn(argv[1], &vconn);
1196 gettimeofday(&start, NULL);
1197 for (i = 0; i < count; i++) {
1198 struct ofpbuf *request, *reply;
1199 struct ofp_header *rq_hdr;
1201 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1202 memset(rq_hdr + 1, 0, payload_size);
1203 run(vconn_transact(vconn, request, &reply), "transact");
1204 ofpbuf_delete(reply);
1206 gettimeofday(&end, NULL);
1209 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1210 + (.001*(end.tv_usec - start.tv_usec)));
1211 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1212 duration, count / (duration / 1000.0),
1213 count * message_size / (duration / 1000.0));
1217 do_execute(const struct settings *s OVS_UNUSED, int argc, char *argv[])
1219 struct vconn *vconn;
1220 struct ofpbuf *request;
1221 struct nicira_header *nicira;
1222 struct nx_command_reply *ncr;
1226 nicira = make_openflow(sizeof *nicira, OFPT_VENDOR, &request);
1227 xid = nicira->header.xid;
1228 nicira->vendor = htonl(NX_VENDOR_ID);
1229 nicira->subtype = htonl(NXT_COMMAND_REQUEST);
1230 ofpbuf_put(request, argv[2], strlen(argv[2]));
1231 for (i = 3; i < argc; i++) {
1232 ofpbuf_put_zeros(request, 1);
1233 ofpbuf_put(request, argv[i], strlen(argv[i]));
1235 update_openflow_length(request);
1237 open_vconn(argv[1], &vconn);
1238 run(vconn_send_block(vconn, request), "send");
1241 struct ofpbuf *reply;
1244 run(vconn_recv_xid(vconn, xid, &reply), "recv_xid");
1245 if (reply->size < sizeof *ncr) {
1246 ovs_fatal(0, "reply is too short (%zu bytes < %zu bytes)",
1247 reply->size, sizeof *ncr);
1250 if (ncr->nxh.header.type != OFPT_VENDOR
1251 || ncr->nxh.vendor != htonl(NX_VENDOR_ID)
1252 || ncr->nxh.subtype != htonl(NXT_COMMAND_REPLY)) {
1253 ovs_fatal(0, "reply is invalid");
1256 status = ntohl(ncr->status);
1257 if (status & NXT_STATUS_STARTED) {
1258 /* Wait for a second reply. */
1260 } else if (status & NXT_STATUS_EXITED) {
1261 fprintf(stderr, "process terminated normally with exit code %d",
1262 status & NXT_STATUS_EXITSTATUS);
1263 } else if (status & NXT_STATUS_SIGNALED) {
1264 fprintf(stderr, "process terminated by signal %d",
1265 status & NXT_STATUS_TERMSIG);
1266 } else if (status & NXT_STATUS_ERROR) {
1267 fprintf(stderr, "error executing command");
1269 fprintf(stderr, "process terminated for unknown reason");
1271 if (status & NXT_STATUS_COREDUMP) {
1272 fprintf(stderr, " (core dumped)");
1276 fwrite(ncr + 1, reply->size - sizeof *ncr, 1, stdout);
1282 do_help(const struct settings *s OVS_UNUSED,
1283 int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1288 static struct command all_commands[] = {
1289 { "show", 1, 1, do_show },
1290 { "status", 1, 2, do_status },
1291 { "monitor", 1, 3, do_monitor },
1292 { "dump-desc", 1, 1, do_dump_desc },
1293 { "dump-tables", 1, 1, do_dump_tables },
1294 { "dump-flows", 1, 2, do_dump_flows },
1295 { "dump-aggregate", 1, 2, do_dump_aggregate },
1296 { "add-flow", 2, 2, do_add_flow },
1297 { "add-flows", 2, 2, do_add_flows },
1298 { "mod-flows", 2, 2, do_mod_flows },
1299 { "del-flows", 1, 2, do_del_flows },
1300 { "dump-ports", 1, 1, do_dump_ports },
1301 { "mod-port", 3, 3, do_mod_port },
1302 { "probe", 1, 1, do_probe },
1303 { "ping", 1, 2, do_ping },
1304 { "benchmark", 3, 3, do_benchmark },
1305 { "execute", 2, INT_MAX, do_execute },
1306 { "help", 0, INT_MAX, do_help },
1307 { NULL, 0, 0, NULL },