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"
47 #include "stream-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"
62 /* Use strict matching for flow mod commands? */
65 static const struct command all_commands[];
67 static void usage(void) NO_RETURN;
68 static void parse_options(int argc, char *argv[]);
71 main(int argc, char *argv[])
73 set_program_name(argv[0]);
76 parse_options(argc, argv);
77 signal(SIGPIPE, SIG_IGN);
78 run_command(argc - optind, argv + optind, all_commands);
83 parse_options(int argc, char *argv[])
86 OPT_STRICT = UCHAR_MAX + 1,
89 static struct option long_options[] = {
90 {"timeout", required_argument, 0, 't'},
91 {"strict", no_argument, 0, OPT_STRICT},
92 {"help", no_argument, 0, 'h'},
93 {"version", no_argument, 0, 'V'},
95 STREAM_SSL_LONG_OPTIONS
98 char *short_options = long_options_to_short_options(long_options);
101 unsigned long int timeout;
104 c = getopt_long(argc, argv, short_options, long_options, NULL);
111 timeout = strtoul(optarg, NULL, 10);
113 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
124 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
132 STREAM_SSL_OPTION_HANDLERS
147 printf("%s: OpenFlow switch management utility\n"
148 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
149 "\nFor OpenFlow switches:\n"
150 " show SWITCH show OpenFlow information\n"
151 " status SWITCH [KEY] report statistics (about KEY)\n"
152 " dump-desc SWITCH print switch description\n"
153 " dump-tables SWITCH print table stats\n"
154 " mod-port SWITCH IFACE ACT modify port behavior\n"
155 " dump-ports SWITCH print port statistics\n"
156 " dump-flows SWITCH print all flow entries\n"
157 " dump-flows SWITCH FLOW print matching FLOWs\n"
158 " dump-aggregate SWITCH print aggregate flow statistics\n"
159 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
160 " add-flow SWITCH FLOW add flow described by FLOW\n"
161 " add-flows SWITCH FILE add flows from FILE\n"
162 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
163 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
164 " monitor SWITCH MISSLEN EXP print packets received from SWITCH\n"
165 "\nFor OpenFlow switches and controllers:\n"
166 " probe VCONN probe whether VCONN is up\n"
167 " ping VCONN [N] latency of N-byte echos\n"
168 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
169 "where each SWITCH is an active OpenFlow connection method.\n",
170 program_name, program_name);
171 vconn_usage(true, false, false);
173 printf("\nOther options:\n"
174 " --strict use strict match for flow commands\n"
175 " -t, --timeout=SECS give up after SECS seconds\n"
176 " -h, --help display this help message\n"
177 " -V, --version display version information\n");
181 static void run(int retval, const char *message, ...)
184 static void run(int retval, const char *message, ...)
189 fprintf(stderr, "%s: ", program_name);
190 va_start(args, message);
191 vfprintf(stderr, message, args);
194 fputs(": unexpected end of file\n", stderr);
196 fprintf(stderr, ": %s\n", strerror(retval));
203 /* Generic commands. */
206 open_vconn_socket(const char *name, struct vconn **vconnp)
208 char *vconn_name = xasprintf("unix:%s", name);
209 VLOG_INFO("connecting to %s", vconn_name);
210 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
211 "connecting to %s", vconn_name);
216 open_vconn(const char *name, struct vconn **vconnp)
220 char *bridge_path, *datapath_name, *datapath_type;
222 bridge_path = xasprintf("%s/%s.mgmt", ovs_rundir, name);
223 dp_parse_name(name, &datapath_name, &datapath_type);
225 if (strstr(name, ":")) {
226 run(vconn_open_block(name, OFP_VERSION, vconnp),
227 "connecting to %s", name);
228 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
229 open_vconn_socket(name, vconnp);
230 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
231 open_vconn_socket(bridge_path, vconnp);
232 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
233 char dpif_name[IF_NAMESIZE + 1];
236 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
237 "obtaining name of %s", dpif_name);
239 if (strcmp(dpif_name, name)) {
240 VLOG_INFO("datapath %s is named %s", name, dpif_name);
243 socket_name = xasprintf("%s/%s.mgmt", ovs_rundir, dpif_name);
244 if (stat(socket_name, &s)) {
245 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
247 } else if (!S_ISSOCK(s.st_mode)) {
248 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
252 open_vconn_socket(socket_name, vconnp);
255 ovs_fatal(0, "%s is not a valid connection method", name);
264 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
266 struct ofp_stats_request *rq;
267 rq = make_openflow((offsetof(struct ofp_stats_request, body)
268 + body_len), OFPT_STATS_REQUEST, bufferp);
269 rq->type = htons(type);
270 rq->flags = htons(0);
275 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
277 update_openflow_length(buffer);
278 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
282 dump_transaction(const char *vconn_name, struct ofpbuf *request)
285 struct ofpbuf *reply;
287 update_openflow_length(request);
288 open_vconn(vconn_name, &vconn);
289 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
290 ofp_print(stdout, reply->data, reply->size, 1);
295 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
297 struct ofpbuf *request;
298 make_openflow(sizeof(struct ofp_header), request_type, &request);
299 dump_transaction(vconn_name, request);
303 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
305 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
309 open_vconn(vconn_name, &vconn);
310 send_openflow_buffer(vconn, request);
313 struct ofpbuf *reply;
315 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
316 recv_xid = ((struct ofp_header *) reply->data)->xid;
317 if (send_xid == recv_xid) {
318 struct ofp_stats_reply *osr;
320 ofp_print(stdout, reply->data, reply->size, 1);
322 osr = ofpbuf_at(reply, 0, sizeof *osr);
323 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
325 VLOG_DBG("received reply with xid %08"PRIx32" "
326 "!= expected %08"PRIx32, recv_xid, send_xid);
328 ofpbuf_delete(reply);
334 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
336 struct ofpbuf *request;
337 alloc_stats_request(0, stats_type, &request);
338 dump_stats_transaction(vconn_name, request);
342 do_show(int argc OVS_UNUSED, char *argv[])
344 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
345 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
349 do_status(int argc, char *argv[])
351 struct nicira_header *request, *reply;
355 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
356 request->vendor = htonl(NX_VENDOR_ID);
357 request->subtype = htonl(NXT_STATUS_REQUEST);
359 ofpbuf_put(b, argv[2], strlen(argv[2]));
360 update_openflow_length(b);
362 open_vconn(argv[1], &vconn);
363 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
366 if (b->size < sizeof *reply) {
367 ovs_fatal(0, "short reply (%zu bytes)", b->size);
370 if (reply->header.type != OFPT_VENDOR
371 || reply->vendor != ntohl(NX_VENDOR_ID)
372 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
373 ofp_print(stderr, b->data, b->size, 2);
374 ovs_fatal(0, "bad reply");
377 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
381 do_dump_desc(int argc OVS_UNUSED, char *argv[])
383 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
387 do_dump_tables(int argc OVS_UNUSED, char *argv[])
389 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
394 str_to_u32(const char *str)
400 value = strtoul(str, &tail, 0);
401 if (errno == EINVAL || errno == ERANGE || *tail) {
402 ovs_fatal(0, "invalid numeric format %s", str);
408 str_to_mac(const char *str, uint8_t mac[6])
410 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
411 != ETH_ADDR_SCAN_COUNT) {
412 ovs_fatal(0, "invalid mac address %s", str);
417 str_to_ip(const char *str_, uint32_t *ip)
419 char *str = xstrdup(str_);
420 char *save_ptr = NULL;
421 const char *name, *netmask;
422 struct in_addr in_addr;
425 name = strtok_r(str, "/", &save_ptr);
426 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
428 ovs_fatal(0, "%s: could not convert to IP address", str);
430 *ip = in_addr.s_addr;
432 netmask = strtok_r(NULL, "/", &save_ptr);
435 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
436 &o[0], &o[1], &o[2], &o[3]) == 4) {
437 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
440 /* Find first 1-bit. */
441 for (i = 0; i < 32; i++) {
442 if (nm & (1u << i)) {
448 /* Verify that the rest of the bits are 1-bits. */
449 for (; i < 32; i++) {
450 if (!(nm & (1u << i))) {
451 ovs_fatal(0, "%s: %s is not a valid netmask",
456 int prefix = atoi(netmask);
457 if (prefix <= 0 || prefix > 32) {
458 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
461 n_wild = 32 - prefix;
472 put_action(struct ofpbuf *b, size_t size, uint16_t type)
474 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
475 ah->type = htons(type);
476 ah->len = htons(size);
480 static struct ofp_action_output *
481 put_output_action(struct ofpbuf *b, uint16_t port)
483 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
484 oao->port = htons(port);
489 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
491 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
492 str_to_mac(addr, oada->dl_addr);
497 parse_port_name(const char *name, uint16_t *port)
503 static const struct pair pairs[] = {
504 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
510 DEF_PAIR(CONTROLLER),
515 static const int n_pairs = ARRAY_SIZE(pairs);
518 for (i = 0; i < n_pairs; i++) {
519 if (!strcasecmp(name, pairs[i].name)) {
520 *port = pairs[i].value;
528 str_to_action(char *str, struct ofpbuf *b)
531 char *saveptr = NULL;
535 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
536 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
541 ovs_fatal(0, "Drop actions must not be followed by other actions");
544 /* Arguments are separated by colons */
545 arg = strchr(act, ':');
551 if (!strcasecmp(act, "mod_vlan_vid")) {
552 struct ofp_action_vlan_vid *va;
553 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
554 va->vlan_vid = htons(str_to_u32(arg));
555 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
556 struct ofp_action_vlan_pcp *va;
557 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
558 va->vlan_pcp = str_to_u32(arg);
559 } else if (!strcasecmp(act, "strip_vlan")) {
560 struct ofp_action_header *ah;
561 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
562 ah->type = htons(OFPAT_STRIP_VLAN);
563 } else if (!strcasecmp(act, "mod_dl_src")) {
564 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
565 } else if (!strcasecmp(act, "mod_dl_dst")) {
566 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
567 } else if (!strcasecmp(act, "mod_nw_src")) {
568 struct ofp_action_nw_addr *na;
569 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
570 str_to_ip(arg, &na->nw_addr);
571 } else if (!strcasecmp(act, "mod_nw_dst")) {
572 struct ofp_action_nw_addr *na;
573 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
574 str_to_ip(arg, &na->nw_addr);
575 } else if (!strcasecmp(act, "mod_tp_src")) {
576 struct ofp_action_tp_port *ta;
577 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
578 ta->tp_port = htons(str_to_u32(arg));
579 } else if (!strcasecmp(act, "mod_tp_dst")) {
580 struct ofp_action_tp_port *ta;
581 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
582 ta->tp_port = htons(str_to_u32(arg));
583 } else if (!strcasecmp(act, "output")) {
584 put_output_action(b, str_to_u32(arg));
585 } else if (!strcasecmp(act, "drop")) {
586 /* A drop action in OpenFlow occurs by just not setting
590 ovs_fatal(0, "Drop actions must not be preceded by other "
593 } else if (!strcasecmp(act, "CONTROLLER")) {
594 struct ofp_action_output *oao;
595 oao = put_output_action(b, OFPP_CONTROLLER);
597 /* Unless a numeric argument is specified, we send the whole
598 * packet to the controller. */
599 if (arg && (strspn(act, "0123456789") == strlen(act))) {
600 oao->max_len = htons(str_to_u32(arg));
602 oao->max_len = htons(UINT16_MAX);
604 } else if (parse_port_name(act, &port)) {
605 put_output_action(b, port);
606 } else if (strspn(act, "0123456789") == strlen(act)) {
607 put_output_action(b, str_to_u32(act));
609 ovs_fatal(0, "Unknown action: %s", act);
621 parse_protocol(const char *name, const struct protocol **p_out)
623 static const struct protocol protocols[] = {
624 { "ip", ETH_TYPE_IP, 0 },
625 { "arp", ETH_TYPE_ARP, 0 },
626 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
627 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
628 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
630 const struct protocol *p;
632 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
633 if (!strcmp(p->name, name)) {
645 enum { F_U8, F_U16, F_MAC, F_IP } type;
646 size_t offset, shift;
650 parse_field(const char *name, const struct field **f_out)
652 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
653 static const struct field fields[] = {
654 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
655 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
656 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
657 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
658 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
659 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
660 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
661 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
662 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
663 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
664 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
665 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
666 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
667 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
669 const struct field *f;
671 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
672 if (!strcmp(f->name, name)) {
682 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
683 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
684 uint16_t *idle_timeout, uint16_t *hard_timeout)
686 char *save_ptr = NULL;
694 *out_port = OFPP_NONE;
697 *priority = OFP_DEFAULT_PRIORITY;
700 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
703 *hard_timeout = OFP_FLOW_PERMANENT;
706 char *act_str = strstr(string, "action");
708 ovs_fatal(0, "must specify an action");
712 act_str = strchr(act_str, '=');
714 ovs_fatal(0, "must specify an action");
719 str_to_action(act_str, actions);
721 memset(match, 0, sizeof *match);
722 wildcards = OFPFW_ALL;
723 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
724 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
725 const struct protocol *p;
727 if (parse_protocol(name, &p)) {
728 wildcards &= ~OFPFW_DL_TYPE;
729 match->dl_type = htons(p->dl_type);
731 wildcards &= ~OFPFW_NW_PROTO;
732 match->nw_proto = p->nw_proto;
735 const struct field *f;
738 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
740 ovs_fatal(0, "field %s missing value", name);
743 if (table_idx && !strcmp(name, "table")) {
744 *table_idx = atoi(value);
745 } else if (out_port && !strcmp(name, "out_port")) {
746 *out_port = atoi(value);
747 } else if (priority && !strcmp(name, "priority")) {
748 *priority = atoi(value);
749 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
750 *idle_timeout = atoi(value);
751 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
752 *hard_timeout = atoi(value);
753 } else if (parse_field(name, &f)) {
754 void *data = (char *) match + f->offset;
755 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
756 wildcards |= f->wildcard;
758 wildcards &= ~f->wildcard;
759 if (f->wildcard == OFPFW_IN_PORT
760 && parse_port_name(value, (uint16_t *) data)) {
762 } else if (f->type == F_U8) {
763 *(uint8_t *) data = str_to_u32(value);
764 } else if (f->type == F_U16) {
765 *(uint16_t *) data = htons(str_to_u32(value));
766 } else if (f->type == F_MAC) {
767 str_to_mac(value, data);
768 } else if (f->type == F_IP) {
769 wildcards |= str_to_ip(value, data) << f->shift;
775 ovs_fatal(0, "unknown keyword %s", name);
779 match->wildcards = htonl(wildcards);
783 do_dump_flows(int argc, char *argv[])
785 struct ofp_flow_stats_request *req;
787 struct ofpbuf *request;
789 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
790 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
791 &req->table_id, &out_port, NULL, NULL, NULL);
792 memset(&req->pad, 0, sizeof req->pad);
793 req->out_port = htons(out_port);
795 dump_stats_transaction(argv[1], request);
799 do_dump_aggregate(int argc, char *argv[])
801 struct ofp_aggregate_stats_request *req;
802 struct ofpbuf *request;
805 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
806 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
807 &req->table_id, &out_port, NULL, NULL, NULL);
808 memset(&req->pad, 0, sizeof req->pad);
809 req->out_port = htons(out_port);
811 dump_stats_transaction(argv[1], request);
815 do_add_flow(int argc OVS_UNUSED, char *argv[])
818 struct ofpbuf *buffer;
819 struct ofp_flow_mod *ofm;
820 uint16_t priority, idle_timeout, hard_timeout;
821 struct ofp_match match;
823 /* Parse and send. str_to_flow() will expand and reallocate the data in
824 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
825 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
826 str_to_flow(argv[2], &match, buffer,
827 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
830 ofm->command = htons(OFPFC_ADD);
831 ofm->idle_timeout = htons(idle_timeout);
832 ofm->hard_timeout = htons(hard_timeout);
833 ofm->buffer_id = htonl(UINT32_MAX);
834 ofm->priority = htons(priority);
835 ofm->reserved = htonl(0);
837 open_vconn(argv[1], &vconn);
838 send_openflow_buffer(vconn, buffer);
843 do_add_flows(int argc OVS_UNUSED, char *argv[])
849 file = fopen(argv[2], "r");
851 ovs_fatal(errno, "%s: open", argv[2]);
854 open_vconn(argv[1], &vconn);
855 while (fgets(line, sizeof line, file)) {
856 struct ofpbuf *buffer;
857 struct ofp_flow_mod *ofm;
858 uint16_t priority, idle_timeout, hard_timeout;
859 struct ofp_match match;
863 /* Delete comments. */
864 comment = strchr(line, '#');
869 /* Drop empty lines. */
870 if (line[strspn(line, " \t\n")] == '\0') {
874 /* Parse and send. str_to_flow() will expand and reallocate the data
875 * in 'buffer', so we can't keep pointers to across the str_to_flow()
877 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
878 str_to_flow(line, &match, buffer,
879 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
882 ofm->command = htons(OFPFC_ADD);
883 ofm->idle_timeout = htons(idle_timeout);
884 ofm->hard_timeout = htons(hard_timeout);
885 ofm->buffer_id = htonl(UINT32_MAX);
886 ofm->priority = htons(priority);
887 ofm->reserved = htonl(0);
889 send_openflow_buffer(vconn, buffer);
896 do_mod_flows(int argc OVS_UNUSED, char *argv[])
898 uint16_t priority, idle_timeout, hard_timeout;
900 struct ofpbuf *buffer;
901 struct ofp_flow_mod *ofm;
902 struct ofp_match match;
904 /* Parse and send. str_to_flow() will expand and reallocate the data in
905 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
906 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
907 str_to_flow(argv[2], &match, buffer,
908 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
912 ofm->command = htons(OFPFC_MODIFY_STRICT);
914 ofm->command = htons(OFPFC_MODIFY);
916 ofm->idle_timeout = htons(idle_timeout);
917 ofm->hard_timeout = htons(hard_timeout);
918 ofm->buffer_id = htonl(UINT32_MAX);
919 ofm->priority = htons(priority);
920 ofm->reserved = htonl(0);
922 open_vconn(argv[1], &vconn);
923 send_openflow_buffer(vconn, buffer);
927 static void do_del_flows(int argc, char *argv[])
932 struct ofpbuf *buffer;
933 struct ofp_flow_mod *ofm;
935 /* Parse and send. */
936 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
937 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
938 &out_port, &priority, NULL, NULL);
940 ofm->command = htons(OFPFC_DELETE_STRICT);
942 ofm->command = htons(OFPFC_DELETE);
944 ofm->idle_timeout = htons(0);
945 ofm->hard_timeout = htons(0);
946 ofm->buffer_id = htonl(UINT32_MAX);
947 ofm->out_port = htons(out_port);
948 ofm->priority = htons(priority);
949 ofm->reserved = htonl(0);
951 open_vconn(argv[1], &vconn);
952 send_openflow_buffer(vconn, buffer);
957 do_monitor(int argc OVS_UNUSED, char *argv[])
961 open_vconn(argv[1], &vconn);
963 int miss_send_len = atoi(argv[2]);
964 int send_flow_exp = argc > 3 ? atoi(argv[3]) : 0;
965 struct ofp_switch_config *osc;
968 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
969 osc->flags = htons(send_flow_exp ? OFPC_SEND_FLOW_EXP : 0);
970 osc->miss_send_len = htons(miss_send_len);
971 send_openflow_buffer(vconn, buf);
975 run(vconn_recv_block(vconn, &b), "vconn_recv");
976 ofp_print(stderr, b->data, b->size, 2);
982 do_dump_ports(int argc OVS_UNUSED, char *argv[])
984 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
988 do_probe(int argc OVS_UNUSED, char *argv[])
990 struct ofpbuf *request;
992 struct ofpbuf *reply;
994 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
995 open_vconn(argv[1], &vconn);
996 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
997 if (reply->size != sizeof(struct ofp_header)) {
998 ovs_fatal(0, "reply does not match request");
1000 ofpbuf_delete(reply);
1005 do_mod_port(int argc OVS_UNUSED, char *argv[])
1007 struct ofpbuf *request, *reply;
1008 struct ofp_switch_features *osf;
1009 struct ofp_port_mod *opm;
1010 struct vconn *vconn;
1017 /* Check if the argument is a port index. Otherwise, treat it as
1019 port_no = strtol(argv[2], &endptr, 10);
1020 if (port_no == 0 && endptr == argv[2]) {
1024 /* Send a "Features Request" to get the information we need in order
1025 * to modify the port. */
1026 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1027 open_vconn(argv[1], &vconn);
1028 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1031 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1033 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1034 if (port_no != -1) {
1035 /* Check argument as a port index */
1036 if (osf->ports[port_idx].port_no == htons(port_no)) {
1040 /* Check argument as an interface name */
1041 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1042 sizeof osf->ports[0].name)) {
1048 if (port_idx == n_ports) {
1049 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1052 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1053 opm->port_no = osf->ports[port_idx].port_no;
1054 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1055 opm->config = htonl(0);
1056 opm->mask = htonl(0);
1057 opm->advertise = htonl(0);
1059 printf("modifying port: %s\n", osf->ports[port_idx].name);
1061 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1062 opm->mask |= htonl(OFPPC_PORT_DOWN);
1063 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1064 sizeof MOD_PORT_CMD_DOWN)) {
1065 opm->mask |= htonl(OFPPC_PORT_DOWN);
1066 opm->config |= htonl(OFPPC_PORT_DOWN);
1067 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1068 sizeof MOD_PORT_CMD_FLOOD)) {
1069 opm->mask |= htonl(OFPPC_NO_FLOOD);
1070 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1071 sizeof MOD_PORT_CMD_NOFLOOD)) {
1072 opm->mask |= htonl(OFPPC_NO_FLOOD);
1073 opm->config |= htonl(OFPPC_NO_FLOOD);
1075 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1078 send_openflow_buffer(vconn, request);
1080 ofpbuf_delete(reply);
1085 do_ping(int argc, char *argv[])
1087 size_t max_payload = 65535 - sizeof(struct ofp_header);
1088 unsigned int payload;
1089 struct vconn *vconn;
1092 payload = argc > 2 ? atoi(argv[2]) : 64;
1093 if (payload > max_payload) {
1094 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1097 open_vconn(argv[1], &vconn);
1098 for (i = 0; i < 10; i++) {
1099 struct timeval start, end;
1100 struct ofpbuf *request, *reply;
1101 struct ofp_header *rq_hdr, *rpy_hdr;
1103 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1104 OFPT_ECHO_REQUEST, &request);
1105 random_bytes(rq_hdr + 1, payload);
1107 gettimeofday(&start, NULL);
1108 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1109 gettimeofday(&end, NULL);
1111 rpy_hdr = reply->data;
1112 if (reply->size != request->size
1113 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1114 || rpy_hdr->xid != rq_hdr->xid
1115 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1116 printf("Reply does not match request. Request:\n");
1117 ofp_print(stdout, request, request->size, 2);
1119 ofp_print(stdout, reply, reply->size, 2);
1121 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1122 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1123 (1000*(double)(end.tv_sec - start.tv_sec))
1124 + (.001*(end.tv_usec - start.tv_usec)));
1125 ofpbuf_delete(request);
1126 ofpbuf_delete(reply);
1132 do_benchmark(int argc OVS_UNUSED, char *argv[])
1134 size_t max_payload = 65535 - sizeof(struct ofp_header);
1135 struct timeval start, end;
1136 unsigned int payload_size, message_size;
1137 struct vconn *vconn;
1142 payload_size = atoi(argv[2]);
1143 if (payload_size > max_payload) {
1144 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1146 message_size = sizeof(struct ofp_header) + payload_size;
1148 count = atoi(argv[3]);
1150 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1151 count, message_size, count * message_size);
1153 open_vconn(argv[1], &vconn);
1154 gettimeofday(&start, NULL);
1155 for (i = 0; i < count; i++) {
1156 struct ofpbuf *request, *reply;
1157 struct ofp_header *rq_hdr;
1159 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1160 memset(rq_hdr + 1, 0, payload_size);
1161 run(vconn_transact(vconn, request, &reply), "transact");
1162 ofpbuf_delete(reply);
1164 gettimeofday(&end, NULL);
1167 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1168 + (.001*(end.tv_usec - start.tv_usec)));
1169 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1170 duration, count / (duration / 1000.0),
1171 count * message_size / (duration / 1000.0));
1175 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1180 static const struct command all_commands[] = {
1181 { "show", 1, 1, do_show },
1182 { "status", 1, 2, do_status },
1183 { "monitor", 1, 3, do_monitor },
1184 { "dump-desc", 1, 1, do_dump_desc },
1185 { "dump-tables", 1, 1, do_dump_tables },
1186 { "dump-flows", 1, 2, do_dump_flows },
1187 { "dump-aggregate", 1, 2, do_dump_aggregate },
1188 { "add-flow", 2, 2, do_add_flow },
1189 { "add-flows", 2, 2, do_add_flows },
1190 { "mod-flows", 2, 2, do_mod_flows },
1191 { "del-flows", 1, 2, do_del_flows },
1192 { "dump-ports", 1, 1, do_dump_ports },
1193 { "mod-port", 3, 3, do_mod_port },
1194 { "probe", 1, 1, do_probe },
1195 { "ping", 1, 2, do_ping },
1196 { "benchmark", 3, 3, do_benchmark },
1197 { "help", 0, INT_MAX, do_help },
1198 { NULL, 0, 0, NULL },