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>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
33 #include "command-line.h"
37 #include "dynamic-string.h"
41 #include "ofp-print.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
47 #include "socket-util.h"
48 #include "stream-ssl.h"
55 #define THIS_MODULE VLM_ofctl
57 #define DEFAULT_IDLE_TIMEOUT 60
59 #define MOD_PORT_CMD_UP "up"
60 #define MOD_PORT_CMD_DOWN "down"
61 #define MOD_PORT_CMD_FLOOD "flood"
62 #define MOD_PORT_CMD_NOFLOOD "noflood"
64 /* Use strict matching for flow mod commands? */
67 static const struct command all_commands[];
69 static void usage(void) NO_RETURN;
70 static void parse_options(int argc, char *argv[]);
73 main(int argc, char *argv[])
75 set_program_name(argv[0]);
78 parse_options(argc, argv);
79 signal(SIGPIPE, SIG_IGN);
80 run_command(argc - optind, argv + optind, all_commands);
85 parse_options(int argc, char *argv[])
88 OPT_STRICT = UCHAR_MAX + 1,
91 static struct option long_options[] = {
92 {"timeout", required_argument, 0, 't'},
93 {"strict", no_argument, 0, OPT_STRICT},
94 {"help", no_argument, 0, 'h'},
95 {"version", no_argument, 0, 'V'},
97 STREAM_SSL_LONG_OPTIONS
100 char *short_options = long_options_to_short_options(long_options);
103 unsigned long int timeout;
106 c = getopt_long(argc, argv, short_options, long_options, NULL);
113 timeout = strtoul(optarg, NULL, 10);
115 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
126 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
134 STREAM_SSL_OPTION_HANDLERS
149 printf("%s: OpenFlow switch management utility\n"
150 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
151 "\nFor OpenFlow switches:\n"
152 " show SWITCH show OpenFlow information\n"
153 " status SWITCH [KEY] report statistics (about KEY)\n"
154 " dump-desc SWITCH print switch description\n"
155 " dump-tables SWITCH print table stats\n"
156 " mod-port SWITCH IFACE ACT modify port behavior\n"
157 " dump-ports SWITCH [PORT] print port statistics\n"
158 " dump-flows SWITCH print all flow entries\n"
159 " dump-flows SWITCH FLOW print matching FLOWs\n"
160 " dump-aggregate SWITCH print aggregate flow statistics\n"
161 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
162 " add-flow SWITCH FLOW add flow described by FLOW\n"
163 " add-flows SWITCH FILE add flows from FILE\n"
164 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
165 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
166 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
167 "\nFor OpenFlow switches and controllers:\n"
168 " probe VCONN probe whether VCONN is up\n"
169 " ping VCONN [N] latency of N-byte echos\n"
170 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
171 "where each SWITCH is an active OpenFlow connection method.\n",
172 program_name, program_name);
173 vconn_usage(true, false, false);
175 printf("\nOther options:\n"
176 " --strict use strict match for flow commands\n"
177 " -t, --timeout=SECS give up after SECS seconds\n"
178 " -h, --help display this help message\n"
179 " -V, --version display version information\n");
183 static void run(int retval, const char *message, ...)
186 static void run(int retval, const char *message, ...)
191 fprintf(stderr, "%s: ", program_name);
192 va_start(args, message);
193 vfprintf(stderr, message, args);
196 fputs(": unexpected end of file\n", stderr);
198 fprintf(stderr, ": %s\n", strerror(retval));
205 /* Generic commands. */
208 open_vconn_socket(const char *name, struct vconn **vconnp)
210 char *vconn_name = xasprintf("unix:%s", name);
211 VLOG_INFO("connecting to %s", vconn_name);
212 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
213 "connecting to %s", vconn_name);
218 open_vconn__(const char *name, const char *default_suffix,
219 struct vconn **vconnp)
223 char *bridge_path, *datapath_name, *datapath_type;
225 bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
226 dp_parse_name(name, &datapath_name, &datapath_type);
228 if (strstr(name, ":")) {
229 run(vconn_open_block(name, OFP_VERSION, vconnp),
230 "connecting to %s", name);
231 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
232 open_vconn_socket(name, vconnp);
233 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
234 open_vconn_socket(bridge_path, vconnp);
235 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
236 char dpif_name[IF_NAMESIZE + 1];
239 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
240 "obtaining name of %s", dpif_name);
242 if (strcmp(dpif_name, name)) {
243 VLOG_INFO("datapath %s is named %s", name, dpif_name);
246 socket_name = xasprintf("%s/%s.%s",
247 ovs_rundir, dpif_name, default_suffix);
248 if (stat(socket_name, &s)) {
249 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
251 } else if (!S_ISSOCK(s.st_mode)) {
252 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
256 open_vconn_socket(socket_name, vconnp);
259 ovs_fatal(0, "%s is not a valid connection method", name);
268 open_vconn(const char *name, struct vconn **vconnp)
270 return open_vconn__(name, "mgmt", vconnp);
274 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
276 struct ofp_stats_request *rq;
277 rq = make_openflow((offsetof(struct ofp_stats_request, body)
278 + body_len), OFPT_STATS_REQUEST, bufferp);
279 rq->type = htons(type);
280 rq->flags = htons(0);
285 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
287 update_openflow_length(buffer);
288 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
292 dump_transaction(const char *vconn_name, struct ofpbuf *request)
295 struct ofpbuf *reply;
297 update_openflow_length(request);
298 open_vconn(vconn_name, &vconn);
299 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
300 ofp_print(stdout, reply->data, reply->size, 1);
305 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
307 struct ofpbuf *request;
308 make_openflow(sizeof(struct ofp_header), request_type, &request);
309 dump_transaction(vconn_name, request);
313 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
315 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
319 open_vconn(vconn_name, &vconn);
320 send_openflow_buffer(vconn, request);
323 struct ofpbuf *reply;
325 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
326 recv_xid = ((struct ofp_header *) reply->data)->xid;
327 if (send_xid == recv_xid) {
328 struct ofp_stats_reply *osr;
330 ofp_print(stdout, reply->data, reply->size, 1);
332 osr = ofpbuf_at(reply, 0, sizeof *osr);
333 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
335 VLOG_DBG("received reply with xid %08"PRIx32" "
336 "!= expected %08"PRIx32, recv_xid, send_xid);
338 ofpbuf_delete(reply);
344 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
346 struct ofpbuf *request;
347 alloc_stats_request(0, stats_type, &request);
348 dump_stats_transaction(vconn_name, request);
352 do_show(int argc OVS_UNUSED, char *argv[])
354 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
355 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
359 do_status(int argc, char *argv[])
361 struct nicira_header *request, *reply;
365 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
366 request->vendor = htonl(NX_VENDOR_ID);
367 request->subtype = htonl(NXT_STATUS_REQUEST);
369 ofpbuf_put(b, argv[2], strlen(argv[2]));
370 update_openflow_length(b);
372 open_vconn(argv[1], &vconn);
373 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
376 if (b->size < sizeof *reply) {
377 ovs_fatal(0, "short reply (%zu bytes)", b->size);
380 if (reply->header.type != OFPT_VENDOR
381 || reply->vendor != ntohl(NX_VENDOR_ID)
382 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
383 ofp_print(stderr, b->data, b->size, 2);
384 ovs_fatal(0, "bad reply");
387 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
391 do_dump_desc(int argc OVS_UNUSED, char *argv[])
393 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
397 do_dump_tables(int argc OVS_UNUSED, char *argv[])
399 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
404 str_to_u32(const char *str)
410 value = strtoul(str, &tail, 0);
411 if (errno == EINVAL || errno == ERANGE || *tail) {
412 ovs_fatal(0, "invalid numeric format %s", str);
418 str_to_u64(const char *str)
424 value = strtoull(str, &tail, 0);
425 if (errno == EINVAL || errno == ERANGE || *tail) {
426 ovs_fatal(0, "invalid numeric format %s", str);
432 str_to_mac(const char *str, uint8_t mac[6])
434 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
435 != ETH_ADDR_SCAN_COUNT) {
436 ovs_fatal(0, "invalid mac address %s", str);
441 str_to_ip(const char *str_, uint32_t *ip)
443 char *str = xstrdup(str_);
444 char *save_ptr = NULL;
445 const char *name, *netmask;
446 struct in_addr in_addr;
449 name = strtok_r(str, "/", &save_ptr);
450 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
452 ovs_fatal(0, "%s: could not convert to IP address", str);
454 *ip = in_addr.s_addr;
456 netmask = strtok_r(NULL, "/", &save_ptr);
459 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
460 &o[0], &o[1], &o[2], &o[3]) == 4) {
461 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
464 /* Find first 1-bit. */
465 for (i = 0; i < 32; i++) {
466 if (nm & (1u << i)) {
472 /* Verify that the rest of the bits are 1-bits. */
473 for (; i < 32; i++) {
474 if (!(nm & (1u << i))) {
475 ovs_fatal(0, "%s: %s is not a valid netmask",
480 int prefix = atoi(netmask);
481 if (prefix <= 0 || prefix > 32) {
482 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
485 n_wild = 32 - prefix;
496 str_to_port_no(const char *vconn_name, const char *str)
498 struct ofpbuf *request, *reply;
499 struct ofp_switch_features *osf;
503 unsigned int port_no;
506 /* Check if the argument is a port index. Otherwise, treat it as
508 if (str_to_uint(str, 10, &port_no)) {
512 /* Send a "Features Request" to resolve the name into a number. */
513 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
514 open_vconn(vconn_name, &vconn);
515 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
518 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
520 for (port_idx = 0; port_idx < n_ports; port_idx++) {
521 /* Check argument as an interface name */
522 if (!strncmp((char *)osf->ports[port_idx].name, str,
523 sizeof osf->ports[0].name)) {
527 if (port_idx == n_ports) {
528 ovs_fatal(0, "couldn't find monitored port: %s", str);
531 ofpbuf_delete(reply);
538 put_action(struct ofpbuf *b, size_t size, uint16_t type)
540 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
541 ah->type = htons(type);
542 ah->len = htons(size);
546 static struct ofp_action_output *
547 put_output_action(struct ofpbuf *b, uint16_t port)
549 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
550 oao->port = htons(port);
555 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
557 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
558 str_to_mac(addr, oada->dl_addr);
563 parse_port_name(const char *name, uint16_t *port)
569 static const struct pair pairs[] = {
570 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
576 DEF_PAIR(CONTROLLER),
581 static const int n_pairs = ARRAY_SIZE(pairs);
584 for (i = 0; i < n_pairs; i++) {
585 if (!strcasecmp(name, pairs[i].name)) {
586 *port = pairs[i].value;
594 str_to_action(char *str, struct ofpbuf *b)
597 char *saveptr = NULL;
601 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
602 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
607 ovs_fatal(0, "Drop actions must not be followed by other actions");
610 /* Arguments are separated by colons */
611 arg = strchr(act, ':');
617 if (!strcasecmp(act, "mod_vlan_vid")) {
618 struct ofp_action_vlan_vid *va;
619 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
620 va->vlan_vid = htons(str_to_u32(arg));
621 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
622 struct ofp_action_vlan_pcp *va;
623 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
624 va->vlan_pcp = str_to_u32(arg);
625 } else if (!strcasecmp(act, "strip_vlan")) {
626 struct ofp_action_header *ah;
627 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
628 ah->type = htons(OFPAT_STRIP_VLAN);
629 } else if (!strcasecmp(act, "mod_dl_src")) {
630 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
631 } else if (!strcasecmp(act, "mod_dl_dst")) {
632 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
633 } else if (!strcasecmp(act, "mod_nw_src")) {
634 struct ofp_action_nw_addr *na;
635 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
636 str_to_ip(arg, &na->nw_addr);
637 } else if (!strcasecmp(act, "mod_nw_dst")) {
638 struct ofp_action_nw_addr *na;
639 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
640 str_to_ip(arg, &na->nw_addr);
641 } else if (!strcasecmp(act, "mod_tp_src")) {
642 struct ofp_action_tp_port *ta;
643 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
644 ta->tp_port = htons(str_to_u32(arg));
645 } else if (!strcasecmp(act, "mod_tp_dst")) {
646 struct ofp_action_tp_port *ta;
647 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
648 ta->tp_port = htons(str_to_u32(arg));
649 } else if (!strcasecmp(act, "mod_nw_tos")) {
650 struct ofp_action_nw_tos *nt;
651 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
652 nt->nw_tos = str_to_u32(arg);
653 } else if (!strcasecmp(act, "resubmit")) {
654 struct nx_action_resubmit *nar;
655 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
656 nar->vendor = htonl(NX_VENDOR_ID);
657 nar->subtype = htons(NXAST_RESUBMIT);
658 nar->in_port = htons(str_to_u32(arg));
659 } else if (!strcasecmp(act, "set_tunnel")) {
660 struct nx_action_set_tunnel *nast;
661 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
662 nast->vendor = htonl(NX_VENDOR_ID);
663 nast->subtype = htons(NXAST_SET_TUNNEL);
664 nast->tun_id = htonl(str_to_u32(arg));
665 } else if (!strcasecmp(act, "output")) {
666 put_output_action(b, str_to_u32(arg));
667 } else if (!strcasecmp(act, "drop")) {
668 /* A drop action in OpenFlow occurs by just not setting
672 ovs_fatal(0, "Drop actions must not be preceded by other "
675 } else if (!strcasecmp(act, "CONTROLLER")) {
676 struct ofp_action_output *oao;
677 oao = put_output_action(b, OFPP_CONTROLLER);
679 /* Unless a numeric argument is specified, we send the whole
680 * packet to the controller. */
681 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
682 oao->max_len = htons(str_to_u32(arg));
684 oao->max_len = htons(UINT16_MAX);
686 } else if (parse_port_name(act, &port)) {
687 put_output_action(b, port);
688 } else if (strspn(act, "0123456789") == strlen(act)) {
689 put_output_action(b, str_to_u32(act));
691 ovs_fatal(0, "Unknown action: %s", act);
703 parse_protocol(const char *name, const struct protocol **p_out)
705 static const struct protocol protocols[] = {
706 { "ip", ETH_TYPE_IP, 0 },
707 { "arp", ETH_TYPE_ARP, 0 },
708 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
709 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
710 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
712 const struct protocol *p;
714 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
715 if (!strcmp(p->name, name)) {
727 enum { F_U8, F_U16, F_MAC, F_IP } type;
728 size_t offset, shift;
732 parse_field(const char *name, const struct field **f_out)
734 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
735 static const struct field fields[] = {
736 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
737 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
738 { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
739 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
740 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
741 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
742 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
743 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
744 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
745 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
746 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
747 { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
748 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
749 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
750 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
751 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
753 const struct field *f;
755 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
756 if (!strcmp(f->name, name)) {
766 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
767 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
768 uint16_t *idle_timeout, uint16_t *hard_timeout,
771 char *save_ptr = NULL;
779 *out_port = OFPP_NONE;
782 *priority = OFP_DEFAULT_PRIORITY;
785 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
788 *hard_timeout = OFP_FLOW_PERMANENT;
794 char *act_str = strstr(string, "action");
796 ovs_fatal(0, "must specify an action");
800 act_str = strchr(act_str + 1, '=');
802 ovs_fatal(0, "must specify an action");
807 str_to_action(act_str, actions);
809 memset(match, 0, sizeof *match);
810 wildcards = OFPFW_ALL;
811 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
812 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
813 const struct protocol *p;
815 if (parse_protocol(name, &p)) {
816 wildcards &= ~OFPFW_DL_TYPE;
817 match->dl_type = htons(p->dl_type);
819 wildcards &= ~OFPFW_NW_PROTO;
820 match->nw_proto = p->nw_proto;
823 const struct field *f;
826 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
828 ovs_fatal(0, "field %s missing value", name);
831 if (table_idx && !strcmp(name, "table")) {
832 *table_idx = atoi(value);
833 } else if (out_port && !strcmp(name, "out_port")) {
834 *out_port = atoi(value);
835 } else if (priority && !strcmp(name, "priority")) {
836 *priority = atoi(value);
837 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
838 *idle_timeout = atoi(value);
839 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
840 *hard_timeout = atoi(value);
841 } else if (cookie && !strcmp(name, "cookie")) {
842 *cookie = str_to_u64(value);
843 } else if (!strcmp(name, "tun_id_wild")) {
844 wildcards |= NXFW_TUN_ID;
845 } else if (parse_field(name, &f)) {
846 void *data = (char *) match + f->offset;
847 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
848 wildcards |= f->wildcard;
850 wildcards &= ~f->wildcard;
851 if (f->wildcard == OFPFW_IN_PORT
852 && parse_port_name(value, (uint16_t *) data)) {
854 } else if (f->type == F_U8) {
855 *(uint8_t *) data = str_to_u32(value);
856 } else if (f->type == F_U16) {
857 *(uint16_t *) data = htons(str_to_u32(value));
858 } else if (f->type == F_MAC) {
859 str_to_mac(value, data);
860 } else if (f->type == F_IP) {
861 wildcards |= str_to_ip(value, data) << f->shift;
867 ovs_fatal(0, "unknown keyword %s", name);
871 match->wildcards = htonl(wildcards);
875 do_dump_flows(int argc, char *argv[])
877 struct ofp_flow_stats_request *req;
879 struct ofpbuf *request;
881 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
882 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
883 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
884 memset(&req->pad, 0, sizeof req->pad);
885 req->out_port = htons(out_port);
887 dump_stats_transaction(argv[1], request);
891 do_dump_aggregate(int argc, char *argv[])
893 struct ofp_aggregate_stats_request *req;
894 struct ofpbuf *request;
897 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
898 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
899 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
900 memset(&req->pad, 0, sizeof req->pad);
901 req->out_port = htons(out_port);
903 dump_stats_transaction(argv[1], request);
907 do_add_flow(int argc OVS_UNUSED, char *argv[])
910 struct ofpbuf *buffer;
911 struct ofp_flow_mod *ofm;
912 uint16_t priority, idle_timeout, hard_timeout;
914 struct ofp_match match;
916 /* Parse and send. str_to_flow() will expand and reallocate the data in
917 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
918 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
919 str_to_flow(argv[2], &match, buffer,
920 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
924 ofm->command = htons(OFPFC_ADD);
925 ofm->cookie = htonll(cookie);
926 ofm->idle_timeout = htons(idle_timeout);
927 ofm->hard_timeout = htons(hard_timeout);
928 ofm->buffer_id = htonl(UINT32_MAX);
929 ofm->priority = htons(priority);
931 open_vconn(argv[1], &vconn);
932 send_openflow_buffer(vconn, buffer);
937 do_add_flows(int argc OVS_UNUSED, char *argv[])
943 file = fopen(argv[2], "r");
945 ovs_fatal(errno, "%s: open", argv[2]);
948 open_vconn(argv[1], &vconn);
949 while (fgets(line, sizeof line, file)) {
950 struct ofpbuf *buffer;
951 struct ofp_flow_mod *ofm;
952 uint16_t priority, idle_timeout, hard_timeout;
954 struct ofp_match match;
958 /* Delete comments. */
959 comment = strchr(line, '#');
964 /* Drop empty lines. */
965 if (line[strspn(line, " \t\n")] == '\0') {
969 /* Parse and send. str_to_flow() will expand and reallocate the data
970 * in 'buffer', so we can't keep pointers to across the str_to_flow()
972 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
973 str_to_flow(line, &match, buffer,
974 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
978 ofm->command = htons(OFPFC_ADD);
979 ofm->cookie = htonll(cookie);
980 ofm->idle_timeout = htons(idle_timeout);
981 ofm->hard_timeout = htons(hard_timeout);
982 ofm->buffer_id = htonl(UINT32_MAX);
983 ofm->priority = htons(priority);
985 send_openflow_buffer(vconn, buffer);
992 do_mod_flows(int argc OVS_UNUSED, char *argv[])
994 uint16_t priority, idle_timeout, hard_timeout;
997 struct ofpbuf *buffer;
998 struct ofp_flow_mod *ofm;
999 struct ofp_match match;
1001 /* Parse and send. str_to_flow() will expand and reallocate the data in
1002 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1003 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1004 str_to_flow(argv[2], &match, buffer,
1005 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1010 ofm->command = htons(OFPFC_MODIFY_STRICT);
1012 ofm->command = htons(OFPFC_MODIFY);
1014 ofm->idle_timeout = htons(idle_timeout);
1015 ofm->hard_timeout = htons(hard_timeout);
1016 ofm->cookie = htonll(cookie);
1017 ofm->buffer_id = htonl(UINT32_MAX);
1018 ofm->priority = htons(priority);
1020 open_vconn(argv[1], &vconn);
1021 send_openflow_buffer(vconn, buffer);
1025 static void do_del_flows(int argc, char *argv[])
1027 struct vconn *vconn;
1030 struct ofpbuf *buffer;
1031 struct ofp_flow_mod *ofm;
1033 /* Parse and send. */
1034 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1035 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
1036 &out_port, &priority, NULL, NULL, NULL);
1038 ofm->command = htons(OFPFC_DELETE_STRICT);
1040 ofm->command = htons(OFPFC_DELETE);
1042 ofm->idle_timeout = htons(0);
1043 ofm->hard_timeout = htons(0);
1044 ofm->buffer_id = htonl(UINT32_MAX);
1045 ofm->out_port = htons(out_port);
1046 ofm->priority = htons(priority);
1048 open_vconn(argv[1], &vconn);
1049 send_openflow_buffer(vconn, buffer);
1054 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1056 struct nxt_tun_id_cookie *tun_id_cookie;
1057 struct ofpbuf *buffer;
1058 struct vconn *vconn;
1060 tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1062 tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1063 tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1064 tun_id_cookie->set = !strcmp(argv[2], "true");
1066 open_vconn(argv[1], &vconn);
1067 send_openflow_buffer(vconn, buffer);
1072 monitor_vconn(struct vconn *vconn)
1076 run(vconn_recv_block(vconn, &b), "vconn_recv");
1077 ofp_print(stderr, b->data, b->size, 2);
1083 do_monitor(int argc, char *argv[])
1085 struct vconn *vconn;
1087 open_vconn(argv[1], &vconn);
1089 int miss_send_len = atoi(argv[2]);
1090 struct ofp_switch_config *osc;
1093 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1094 osc->miss_send_len = htons(miss_send_len);
1095 send_openflow_buffer(vconn, buf);
1097 monitor_vconn(vconn);
1101 do_snoop(int argc OVS_UNUSED, char *argv[])
1103 struct vconn *vconn;
1105 open_vconn__(argv[1], "snoop", &vconn);
1106 monitor_vconn(vconn);
1110 do_dump_ports(int argc, char *argv[])
1112 struct ofp_port_stats_request *req;
1113 struct ofpbuf *request;
1116 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1117 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1118 req->port_no = htons(port);
1119 dump_stats_transaction(argv[1], request);
1123 do_probe(int argc OVS_UNUSED, char *argv[])
1125 struct ofpbuf *request;
1126 struct vconn *vconn;
1127 struct ofpbuf *reply;
1129 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1130 open_vconn(argv[1], &vconn);
1131 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1132 if (reply->size != sizeof(struct ofp_header)) {
1133 ovs_fatal(0, "reply does not match request");
1135 ofpbuf_delete(reply);
1140 do_mod_port(int argc OVS_UNUSED, char *argv[])
1142 struct ofpbuf *request, *reply;
1143 struct ofp_switch_features *osf;
1144 struct ofp_port_mod *opm;
1145 struct vconn *vconn;
1152 /* Check if the argument is a port index. Otherwise, treat it as
1154 port_no = strtol(argv[2], &endptr, 10);
1155 if (port_no == 0 && endptr == argv[2]) {
1159 /* Send a "Features Request" to get the information we need in order
1160 * to modify the port. */
1161 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1162 open_vconn(argv[1], &vconn);
1163 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1166 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1168 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1169 if (port_no != -1) {
1170 /* Check argument as a port index */
1171 if (osf->ports[port_idx].port_no == htons(port_no)) {
1175 /* Check argument as an interface name */
1176 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1177 sizeof osf->ports[0].name)) {
1183 if (port_idx == n_ports) {
1184 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1187 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1188 opm->port_no = osf->ports[port_idx].port_no;
1189 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1190 opm->config = htonl(0);
1191 opm->mask = htonl(0);
1192 opm->advertise = htonl(0);
1194 printf("modifying port: %s\n", osf->ports[port_idx].name);
1196 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1197 opm->mask |= htonl(OFPPC_PORT_DOWN);
1198 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1199 sizeof MOD_PORT_CMD_DOWN)) {
1200 opm->mask |= htonl(OFPPC_PORT_DOWN);
1201 opm->config |= htonl(OFPPC_PORT_DOWN);
1202 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1203 sizeof MOD_PORT_CMD_FLOOD)) {
1204 opm->mask |= htonl(OFPPC_NO_FLOOD);
1205 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1206 sizeof MOD_PORT_CMD_NOFLOOD)) {
1207 opm->mask |= htonl(OFPPC_NO_FLOOD);
1208 opm->config |= htonl(OFPPC_NO_FLOOD);
1210 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1213 send_openflow_buffer(vconn, request);
1215 ofpbuf_delete(reply);
1220 do_ping(int argc, char *argv[])
1222 size_t max_payload = 65535 - sizeof(struct ofp_header);
1223 unsigned int payload;
1224 struct vconn *vconn;
1227 payload = argc > 2 ? atoi(argv[2]) : 64;
1228 if (payload > max_payload) {
1229 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1232 open_vconn(argv[1], &vconn);
1233 for (i = 0; i < 10; i++) {
1234 struct timeval start, end;
1235 struct ofpbuf *request, *reply;
1236 struct ofp_header *rq_hdr, *rpy_hdr;
1238 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1239 OFPT_ECHO_REQUEST, &request);
1240 random_bytes(rq_hdr + 1, payload);
1242 gettimeofday(&start, NULL);
1243 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1244 gettimeofday(&end, NULL);
1246 rpy_hdr = reply->data;
1247 if (reply->size != request->size
1248 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1249 || rpy_hdr->xid != rq_hdr->xid
1250 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1251 printf("Reply does not match request. Request:\n");
1252 ofp_print(stdout, request, request->size, 2);
1254 ofp_print(stdout, reply, reply->size, 2);
1256 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1257 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1258 (1000*(double)(end.tv_sec - start.tv_sec))
1259 + (.001*(end.tv_usec - start.tv_usec)));
1260 ofpbuf_delete(request);
1261 ofpbuf_delete(reply);
1267 do_benchmark(int argc OVS_UNUSED, char *argv[])
1269 size_t max_payload = 65535 - sizeof(struct ofp_header);
1270 struct timeval start, end;
1271 unsigned int payload_size, message_size;
1272 struct vconn *vconn;
1277 payload_size = atoi(argv[2]);
1278 if (payload_size > max_payload) {
1279 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1281 message_size = sizeof(struct ofp_header) + payload_size;
1283 count = atoi(argv[3]);
1285 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1286 count, message_size, count * message_size);
1288 open_vconn(argv[1], &vconn);
1289 gettimeofday(&start, NULL);
1290 for (i = 0; i < count; i++) {
1291 struct ofpbuf *request, *reply;
1292 struct ofp_header *rq_hdr;
1294 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1295 memset(rq_hdr + 1, 0, payload_size);
1296 run(vconn_transact(vconn, request, &reply), "transact");
1297 ofpbuf_delete(reply);
1299 gettimeofday(&end, NULL);
1302 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1303 + (.001*(end.tv_usec - start.tv_usec)));
1304 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1305 duration, count / (duration / 1000.0),
1306 count * message_size / (duration / 1000.0));
1310 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1315 static const struct command all_commands[] = {
1316 { "show", 1, 1, do_show },
1317 { "status", 1, 2, do_status },
1318 { "monitor", 1, 2, do_monitor },
1319 { "snoop", 1, 1, do_snoop },
1320 { "dump-desc", 1, 1, do_dump_desc },
1321 { "dump-tables", 1, 1, do_dump_tables },
1322 { "dump-flows", 1, 2, do_dump_flows },
1323 { "dump-aggregate", 1, 2, do_dump_aggregate },
1324 { "add-flow", 2, 2, do_add_flow },
1325 { "add-flows", 2, 2, do_add_flows },
1326 { "mod-flows", 2, 2, do_mod_flows },
1327 { "del-flows", 1, 2, do_del_flows },
1328 { "tun-cookie", 2, 2, do_tun_cookie },
1329 { "dump-ports", 1, 2, do_dump_ports },
1330 { "mod-port", 3, 3, do_mod_port },
1331 { "probe", 1, 1, do_probe },
1332 { "ping", 1, 2, do_ping },
1333 { "benchmark", 3, 3, do_benchmark },
1334 { "help", 0, INT_MAX, do_help },
1335 { NULL, 0, 0, NULL },