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, "mod_nw_tos")) {
584 struct ofp_action_nw_tos *nt;
585 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
586 nt->nw_tos = str_to_u32(arg);
587 } else if (!strcasecmp(act, "output")) {
588 put_output_action(b, str_to_u32(arg));
589 } else if (!strcasecmp(act, "drop")) {
590 /* A drop action in OpenFlow occurs by just not setting
594 ovs_fatal(0, "Drop actions must not be preceded by other "
597 } else if (!strcasecmp(act, "CONTROLLER")) {
598 struct ofp_action_output *oao;
599 oao = put_output_action(b, OFPP_CONTROLLER);
601 /* Unless a numeric argument is specified, we send the whole
602 * packet to the controller. */
603 if (arg && (strspn(act, "0123456789") == strlen(act))) {
604 oao->max_len = htons(str_to_u32(arg));
606 oao->max_len = htons(UINT16_MAX);
608 } else if (parse_port_name(act, &port)) {
609 put_output_action(b, port);
610 } else if (strspn(act, "0123456789") == strlen(act)) {
611 put_output_action(b, str_to_u32(act));
613 ovs_fatal(0, "Unknown action: %s", act);
625 parse_protocol(const char *name, const struct protocol **p_out)
627 static const struct protocol protocols[] = {
628 { "ip", ETH_TYPE_IP, 0 },
629 { "arp", ETH_TYPE_ARP, 0 },
630 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
631 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
632 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
634 const struct protocol *p;
636 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
637 if (!strcmp(p->name, name)) {
649 enum { F_U8, F_U16, F_MAC, F_IP } type;
650 size_t offset, shift;
654 parse_field(const char *name, const struct field **f_out)
656 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
657 static const struct field fields[] = {
658 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
659 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
660 { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
661 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
662 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
663 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
664 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
665 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
666 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
667 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
668 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
669 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
670 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
671 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
672 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
674 const struct field *f;
676 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
677 if (!strcmp(f->name, name)) {
687 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
688 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
689 uint16_t *idle_timeout, uint16_t *hard_timeout)
691 char *save_ptr = NULL;
699 *out_port = OFPP_NONE;
702 *priority = OFP_DEFAULT_PRIORITY;
705 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
708 *hard_timeout = OFP_FLOW_PERMANENT;
711 char *act_str = strstr(string, "action");
713 ovs_fatal(0, "must specify an action");
717 act_str = strchr(act_str, '=');
719 ovs_fatal(0, "must specify an action");
724 str_to_action(act_str, actions);
726 memset(match, 0, sizeof *match);
727 wildcards = OFPFW_ALL;
728 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
729 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
730 const struct protocol *p;
732 if (parse_protocol(name, &p)) {
733 wildcards &= ~OFPFW_DL_TYPE;
734 match->dl_type = htons(p->dl_type);
736 wildcards &= ~OFPFW_NW_PROTO;
737 match->nw_proto = p->nw_proto;
740 const struct field *f;
743 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
745 ovs_fatal(0, "field %s missing value", name);
748 if (table_idx && !strcmp(name, "table")) {
749 *table_idx = atoi(value);
750 } else if (out_port && !strcmp(name, "out_port")) {
751 *out_port = atoi(value);
752 } else if (priority && !strcmp(name, "priority")) {
753 *priority = atoi(value);
754 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
755 *idle_timeout = atoi(value);
756 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
757 *hard_timeout = atoi(value);
758 } else if (parse_field(name, &f)) {
759 void *data = (char *) match + f->offset;
760 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
761 wildcards |= f->wildcard;
763 wildcards &= ~f->wildcard;
764 if (f->wildcard == OFPFW_IN_PORT
765 && parse_port_name(value, (uint16_t *) data)) {
767 } else if (f->type == F_U8) {
768 *(uint8_t *) data = str_to_u32(value);
769 } else if (f->type == F_U16) {
770 *(uint16_t *) data = htons(str_to_u32(value));
771 } else if (f->type == F_MAC) {
772 str_to_mac(value, data);
773 } else if (f->type == F_IP) {
774 wildcards |= str_to_ip(value, data) << f->shift;
780 ovs_fatal(0, "unknown keyword %s", name);
784 match->wildcards = htonl(wildcards);
788 do_dump_flows(int argc, char *argv[])
790 struct ofp_flow_stats_request *req;
792 struct ofpbuf *request;
794 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
795 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
796 &req->table_id, &out_port, NULL, NULL, NULL);
797 memset(&req->pad, 0, sizeof req->pad);
798 req->out_port = htons(out_port);
800 dump_stats_transaction(argv[1], request);
804 do_dump_aggregate(int argc, char *argv[])
806 struct ofp_aggregate_stats_request *req;
807 struct ofpbuf *request;
810 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
811 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
812 &req->table_id, &out_port, NULL, NULL, NULL);
813 memset(&req->pad, 0, sizeof req->pad);
814 req->out_port = htons(out_port);
816 dump_stats_transaction(argv[1], request);
820 do_add_flow(int argc OVS_UNUSED, char *argv[])
823 struct ofpbuf *buffer;
824 struct ofp_flow_mod *ofm;
825 uint16_t priority, idle_timeout, hard_timeout;
826 struct ofp_match match;
828 /* Parse and send. str_to_flow() will expand and reallocate the data in
829 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
830 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
831 str_to_flow(argv[2], &match, buffer,
832 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
835 ofm->command = htons(OFPFC_ADD);
836 ofm->idle_timeout = htons(idle_timeout);
837 ofm->hard_timeout = htons(hard_timeout);
838 ofm->buffer_id = htonl(UINT32_MAX);
839 ofm->priority = htons(priority);
840 ofm->reserved = htonl(0);
842 open_vconn(argv[1], &vconn);
843 send_openflow_buffer(vconn, buffer);
848 do_add_flows(int argc OVS_UNUSED, char *argv[])
854 file = fopen(argv[2], "r");
856 ovs_fatal(errno, "%s: open", argv[2]);
859 open_vconn(argv[1], &vconn);
860 while (fgets(line, sizeof line, file)) {
861 struct ofpbuf *buffer;
862 struct ofp_flow_mod *ofm;
863 uint16_t priority, idle_timeout, hard_timeout;
864 struct ofp_match match;
868 /* Delete comments. */
869 comment = strchr(line, '#');
874 /* Drop empty lines. */
875 if (line[strspn(line, " \t\n")] == '\0') {
879 /* Parse and send. str_to_flow() will expand and reallocate the data
880 * in 'buffer', so we can't keep pointers to across the str_to_flow()
882 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
883 str_to_flow(line, &match, buffer,
884 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
887 ofm->command = htons(OFPFC_ADD);
888 ofm->idle_timeout = htons(idle_timeout);
889 ofm->hard_timeout = htons(hard_timeout);
890 ofm->buffer_id = htonl(UINT32_MAX);
891 ofm->priority = htons(priority);
892 ofm->reserved = htonl(0);
894 send_openflow_buffer(vconn, buffer);
901 do_mod_flows(int argc OVS_UNUSED, char *argv[])
903 uint16_t priority, idle_timeout, hard_timeout;
905 struct ofpbuf *buffer;
906 struct ofp_flow_mod *ofm;
907 struct ofp_match match;
909 /* Parse and send. str_to_flow() will expand and reallocate the data in
910 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
911 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
912 str_to_flow(argv[2], &match, buffer,
913 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
917 ofm->command = htons(OFPFC_MODIFY_STRICT);
919 ofm->command = htons(OFPFC_MODIFY);
921 ofm->idle_timeout = htons(idle_timeout);
922 ofm->hard_timeout = htons(hard_timeout);
923 ofm->buffer_id = htonl(UINT32_MAX);
924 ofm->priority = htons(priority);
925 ofm->reserved = htonl(0);
927 open_vconn(argv[1], &vconn);
928 send_openflow_buffer(vconn, buffer);
932 static void do_del_flows(int argc, char *argv[])
937 struct ofpbuf *buffer;
938 struct ofp_flow_mod *ofm;
940 /* Parse and send. */
941 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
942 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
943 &out_port, &priority, NULL, NULL);
945 ofm->command = htons(OFPFC_DELETE_STRICT);
947 ofm->command = htons(OFPFC_DELETE);
949 ofm->idle_timeout = htons(0);
950 ofm->hard_timeout = htons(0);
951 ofm->buffer_id = htonl(UINT32_MAX);
952 ofm->out_port = htons(out_port);
953 ofm->priority = htons(priority);
954 ofm->reserved = htonl(0);
956 open_vconn(argv[1], &vconn);
957 send_openflow_buffer(vconn, buffer);
962 do_monitor(int argc OVS_UNUSED, char *argv[])
966 open_vconn(argv[1], &vconn);
968 int miss_send_len = atoi(argv[2]);
969 struct ofp_switch_config *osc;
972 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
973 osc->miss_send_len = htons(miss_send_len);
974 send_openflow_buffer(vconn, buf);
978 run(vconn_recv_block(vconn, &b), "vconn_recv");
979 ofp_print(stderr, b->data, b->size, 2);
985 do_dump_ports(int argc OVS_UNUSED, char *argv[])
987 dump_trivial_stats_transaction(argv[1], OFPST_PORT);
991 do_probe(int argc OVS_UNUSED, char *argv[])
993 struct ofpbuf *request;
995 struct ofpbuf *reply;
997 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
998 open_vconn(argv[1], &vconn);
999 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1000 if (reply->size != sizeof(struct ofp_header)) {
1001 ovs_fatal(0, "reply does not match request");
1003 ofpbuf_delete(reply);
1008 do_mod_port(int argc OVS_UNUSED, char *argv[])
1010 struct ofpbuf *request, *reply;
1011 struct ofp_switch_features *osf;
1012 struct ofp_port_mod *opm;
1013 struct vconn *vconn;
1020 /* Check if the argument is a port index. Otherwise, treat it as
1022 port_no = strtol(argv[2], &endptr, 10);
1023 if (port_no == 0 && endptr == argv[2]) {
1027 /* Send a "Features Request" to get the information we need in order
1028 * to modify the port. */
1029 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1030 open_vconn(argv[1], &vconn);
1031 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1034 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1036 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1037 if (port_no != -1) {
1038 /* Check argument as a port index */
1039 if (osf->ports[port_idx].port_no == htons(port_no)) {
1043 /* Check argument as an interface name */
1044 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1045 sizeof osf->ports[0].name)) {
1051 if (port_idx == n_ports) {
1052 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1055 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1056 opm->port_no = osf->ports[port_idx].port_no;
1057 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1058 opm->config = htonl(0);
1059 opm->mask = htonl(0);
1060 opm->advertise = htonl(0);
1062 printf("modifying port: %s\n", osf->ports[port_idx].name);
1064 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1065 opm->mask |= htonl(OFPPC_PORT_DOWN);
1066 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1067 sizeof MOD_PORT_CMD_DOWN)) {
1068 opm->mask |= htonl(OFPPC_PORT_DOWN);
1069 opm->config |= htonl(OFPPC_PORT_DOWN);
1070 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1071 sizeof MOD_PORT_CMD_FLOOD)) {
1072 opm->mask |= htonl(OFPPC_NO_FLOOD);
1073 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1074 sizeof MOD_PORT_CMD_NOFLOOD)) {
1075 opm->mask |= htonl(OFPPC_NO_FLOOD);
1076 opm->config |= htonl(OFPPC_NO_FLOOD);
1078 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1081 send_openflow_buffer(vconn, request);
1083 ofpbuf_delete(reply);
1088 do_ping(int argc, char *argv[])
1090 size_t max_payload = 65535 - sizeof(struct ofp_header);
1091 unsigned int payload;
1092 struct vconn *vconn;
1095 payload = argc > 2 ? atoi(argv[2]) : 64;
1096 if (payload > max_payload) {
1097 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1100 open_vconn(argv[1], &vconn);
1101 for (i = 0; i < 10; i++) {
1102 struct timeval start, end;
1103 struct ofpbuf *request, *reply;
1104 struct ofp_header *rq_hdr, *rpy_hdr;
1106 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1107 OFPT_ECHO_REQUEST, &request);
1108 random_bytes(rq_hdr + 1, payload);
1110 gettimeofday(&start, NULL);
1111 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1112 gettimeofday(&end, NULL);
1114 rpy_hdr = reply->data;
1115 if (reply->size != request->size
1116 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1117 || rpy_hdr->xid != rq_hdr->xid
1118 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1119 printf("Reply does not match request. Request:\n");
1120 ofp_print(stdout, request, request->size, 2);
1122 ofp_print(stdout, reply, reply->size, 2);
1124 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1125 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1126 (1000*(double)(end.tv_sec - start.tv_sec))
1127 + (.001*(end.tv_usec - start.tv_usec)));
1128 ofpbuf_delete(request);
1129 ofpbuf_delete(reply);
1135 do_benchmark(int argc OVS_UNUSED, char *argv[])
1137 size_t max_payload = 65535 - sizeof(struct ofp_header);
1138 struct timeval start, end;
1139 unsigned int payload_size, message_size;
1140 struct vconn *vconn;
1145 payload_size = atoi(argv[2]);
1146 if (payload_size > max_payload) {
1147 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1149 message_size = sizeof(struct ofp_header) + payload_size;
1151 count = atoi(argv[3]);
1153 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1154 count, message_size, count * message_size);
1156 open_vconn(argv[1], &vconn);
1157 gettimeofday(&start, NULL);
1158 for (i = 0; i < count; i++) {
1159 struct ofpbuf *request, *reply;
1160 struct ofp_header *rq_hdr;
1162 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1163 memset(rq_hdr + 1, 0, payload_size);
1164 run(vconn_transact(vconn, request, &reply), "transact");
1165 ofpbuf_delete(reply);
1167 gettimeofday(&end, NULL);
1170 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1171 + (.001*(end.tv_usec - start.tv_usec)));
1172 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1173 duration, count / (duration / 1000.0),
1174 count * message_size / (duration / 1000.0));
1178 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1183 static const struct command all_commands[] = {
1184 { "show", 1, 1, do_show },
1185 { "status", 1, 2, do_status },
1186 { "monitor", 1, 3, do_monitor },
1187 { "dump-desc", 1, 1, do_dump_desc },
1188 { "dump-tables", 1, 1, do_dump_tables },
1189 { "dump-flows", 1, 2, do_dump_flows },
1190 { "dump-aggregate", 1, 2, do_dump_aggregate },
1191 { "add-flow", 2, 2, do_add_flow },
1192 { "add-flows", 2, 2, do_add_flows },
1193 { "mod-flows", 2, 2, do_mod_flows },
1194 { "del-flows", 1, 2, do_del_flows },
1195 { "dump-ports", 1, 1, do_dump_ports },
1196 { "mod-port", 3, 3, do_mod_port },
1197 { "probe", 1, 1, do_probe },
1198 { "ping", 1, 2, do_ping },
1199 { "benchmark", 3, 3, do_benchmark },
1200 { "help", 0, INT_MAX, do_help },
1201 { NULL, 0, 0, NULL },