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"
44 #include "openflow/nicira-ext.h"
45 #include "openflow/openflow.h"
48 #include "socket-util.h"
49 #include "stream-ssl.h"
56 #define THIS_MODULE VLM_ofctl
58 #define DEFAULT_IDLE_TIMEOUT 60
60 #define MOD_PORT_CMD_UP "up"
61 #define MOD_PORT_CMD_DOWN "down"
62 #define MOD_PORT_CMD_FLOOD "flood"
63 #define MOD_PORT_CMD_NOFLOOD "noflood"
65 /* Use strict matching for flow mod commands? */
68 static const struct command all_commands[];
70 static void usage(void) NO_RETURN;
71 static void parse_options(int argc, char *argv[]);
74 main(int argc, char *argv[])
76 set_program_name(argv[0]);
79 parse_options(argc, argv);
80 signal(SIGPIPE, SIG_IGN);
81 run_command(argc - optind, argv + optind, all_commands);
86 parse_options(int argc, char *argv[])
89 OPT_STRICT = UCHAR_MAX + 1,
92 static struct option long_options[] = {
93 {"timeout", required_argument, 0, 't'},
94 {"strict", no_argument, 0, OPT_STRICT},
95 {"help", no_argument, 0, 'h'},
96 {"version", no_argument, 0, 'V'},
98 STREAM_SSL_LONG_OPTIONS
101 char *short_options = long_options_to_short_options(long_options);
104 unsigned long int timeout;
107 c = getopt_long(argc, argv, short_options, long_options, NULL);
114 timeout = strtoul(optarg, NULL, 10);
116 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
127 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
135 STREAM_SSL_OPTION_HANDLERS
150 printf("%s: OpenFlow switch management utility\n"
151 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
152 "\nFor OpenFlow switches:\n"
153 " show SWITCH show OpenFlow information\n"
154 " status SWITCH [KEY] report statistics (about KEY)\n"
155 " dump-desc SWITCH print switch description\n"
156 " dump-tables SWITCH print table stats\n"
157 " mod-port SWITCH IFACE ACT modify port behavior\n"
158 " dump-ports SWITCH [PORT] print port statistics\n"
159 " dump-flows SWITCH print all flow entries\n"
160 " dump-flows SWITCH FLOW print matching FLOWs\n"
161 " dump-aggregate SWITCH print aggregate flow statistics\n"
162 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
163 " add-flow SWITCH FLOW add flow described by FLOW\n"
164 " add-flows SWITCH FILE add flows from FILE\n"
165 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
166 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
167 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
168 "\nFor OpenFlow switches and controllers:\n"
169 " probe VCONN probe whether VCONN is up\n"
170 " ping VCONN [N] latency of N-byte echos\n"
171 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
172 "where each SWITCH is an active OpenFlow connection method.\n",
173 program_name, program_name);
174 vconn_usage(true, false, false);
176 printf("\nOther options:\n"
177 " --strict use strict match for flow commands\n"
178 " -t, --timeout=SECS give up after SECS seconds\n"
179 " -h, --help display this help message\n"
180 " -V, --version display version information\n");
184 static void run(int retval, const char *message, ...)
187 static void run(int retval, const char *message, ...)
192 fprintf(stderr, "%s: ", program_name);
193 va_start(args, message);
194 vfprintf(stderr, message, args);
197 fputs(": unexpected end of file\n", stderr);
199 fprintf(stderr, ": %s\n", strerror(retval));
206 /* Generic commands. */
209 open_vconn_socket(const char *name, struct vconn **vconnp)
211 char *vconn_name = xasprintf("unix:%s", name);
212 VLOG_INFO("connecting to %s", vconn_name);
213 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
214 "connecting to %s", vconn_name);
219 open_vconn__(const char *name, const char *default_suffix,
220 struct vconn **vconnp)
224 char *bridge_path, *datapath_name, *datapath_type;
226 bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
227 dp_parse_name(name, &datapath_name, &datapath_type);
229 if (strstr(name, ":")) {
230 run(vconn_open_block(name, OFP_VERSION, vconnp),
231 "connecting to %s", name);
232 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
233 open_vconn_socket(name, vconnp);
234 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
235 open_vconn_socket(bridge_path, vconnp);
236 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
237 char dpif_name[IF_NAMESIZE + 1];
240 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
241 "obtaining name of %s", dpif_name);
243 if (strcmp(dpif_name, name)) {
244 VLOG_INFO("datapath %s is named %s", name, dpif_name);
247 socket_name = xasprintf("%s/%s.%s",
248 ovs_rundir, dpif_name, default_suffix);
249 if (stat(socket_name, &s)) {
250 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
252 } else if (!S_ISSOCK(s.st_mode)) {
253 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
257 open_vconn_socket(socket_name, vconnp);
260 ovs_fatal(0, "%s is not a valid connection method", name);
269 open_vconn(const char *name, struct vconn **vconnp)
271 return open_vconn__(name, "mgmt", vconnp);
275 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
277 struct ofp_stats_request *rq;
278 rq = make_openflow((offsetof(struct ofp_stats_request, body)
279 + body_len), OFPT_STATS_REQUEST, bufferp);
280 rq->type = htons(type);
281 rq->flags = htons(0);
286 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
288 update_openflow_length(buffer);
289 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
293 dump_transaction(const char *vconn_name, struct ofpbuf *request)
296 struct ofpbuf *reply;
298 update_openflow_length(request);
299 open_vconn(vconn_name, &vconn);
300 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
301 ofp_print(stdout, reply->data, reply->size, 1);
306 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
308 struct ofpbuf *request;
309 make_openflow(sizeof(struct ofp_header), request_type, &request);
310 dump_transaction(vconn_name, request);
314 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
316 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
320 open_vconn(vconn_name, &vconn);
321 send_openflow_buffer(vconn, request);
324 struct ofpbuf *reply;
326 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
327 recv_xid = ((struct ofp_header *) reply->data)->xid;
328 if (send_xid == recv_xid) {
329 struct ofp_stats_reply *osr;
331 ofp_print(stdout, reply->data, reply->size, 1);
333 osr = ofpbuf_at(reply, 0, sizeof *osr);
334 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
336 VLOG_DBG("received reply with xid %08"PRIx32" "
337 "!= expected %08"PRIx32, recv_xid, send_xid);
339 ofpbuf_delete(reply);
345 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
347 struct ofpbuf *request;
348 alloc_stats_request(0, stats_type, &request);
349 dump_stats_transaction(vconn_name, request);
353 do_show(int argc OVS_UNUSED, char *argv[])
355 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
356 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
360 do_status(int argc, char *argv[])
362 struct nicira_header *request, *reply;
366 request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
367 request->vendor = htonl(NX_VENDOR_ID);
368 request->subtype = htonl(NXT_STATUS_REQUEST);
370 ofpbuf_put(b, argv[2], strlen(argv[2]));
371 update_openflow_length(b);
373 open_vconn(argv[1], &vconn);
374 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
377 if (b->size < sizeof *reply) {
378 ovs_fatal(0, "short reply (%zu bytes)", b->size);
381 if (reply->header.type != OFPT_VENDOR
382 || reply->vendor != ntohl(NX_VENDOR_ID)
383 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
384 ofp_print(stderr, b->data, b->size, 2);
385 ovs_fatal(0, "bad reply");
388 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
392 do_dump_desc(int argc OVS_UNUSED, char *argv[])
394 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
398 do_dump_tables(int argc OVS_UNUSED, char *argv[])
400 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
405 str_to_u32(const char *str)
411 value = strtoul(str, &tail, 0);
412 if (errno == EINVAL || errno == ERANGE || *tail) {
413 ovs_fatal(0, "invalid numeric format %s", str);
419 str_to_u64(const char *str)
425 value = strtoull(str, &tail, 0);
426 if (errno == EINVAL || errno == ERANGE || *tail) {
427 ovs_fatal(0, "invalid numeric format %s", str);
433 str_to_mac(const char *str, uint8_t mac[6])
435 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
436 != ETH_ADDR_SCAN_COUNT) {
437 ovs_fatal(0, "invalid mac address %s", str);
442 str_to_ip(const char *str_, uint32_t *ip)
444 char *str = xstrdup(str_);
445 char *save_ptr = NULL;
446 const char *name, *netmask;
447 struct in_addr in_addr;
450 name = strtok_r(str, "/", &save_ptr);
451 retval = name ? lookup_ip(name, &in_addr) : EINVAL;
453 ovs_fatal(0, "%s: could not convert to IP address", str);
455 *ip = in_addr.s_addr;
457 netmask = strtok_r(NULL, "/", &save_ptr);
460 if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
461 &o[0], &o[1], &o[2], &o[3]) == 4) {
462 uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
465 /* Find first 1-bit. */
466 for (i = 0; i < 32; i++) {
467 if (nm & (1u << i)) {
473 /* Verify that the rest of the bits are 1-bits. */
474 for (; i < 32; i++) {
475 if (!(nm & (1u << i))) {
476 ovs_fatal(0, "%s: %s is not a valid netmask",
481 int prefix = atoi(netmask);
482 if (prefix <= 0 || prefix > 32) {
483 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
486 n_wild = 32 - prefix;
497 str_to_port_no(const char *vconn_name, const char *str)
499 struct ofpbuf *request, *reply;
500 struct ofp_switch_features *osf;
504 unsigned int port_no;
507 /* Check if the argument is a port index. Otherwise, treat it as
509 if (str_to_uint(str, 10, &port_no)) {
513 /* Send a "Features Request" to resolve the name into a number. */
514 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
515 open_vconn(vconn_name, &vconn);
516 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
519 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
521 for (port_idx = 0; port_idx < n_ports; port_idx++) {
522 /* Check argument as an interface name */
523 if (!strncmp((char *)osf->ports[port_idx].name, str,
524 sizeof osf->ports[0].name)) {
528 if (port_idx == n_ports) {
529 ovs_fatal(0, "couldn't find monitored port: %s", str);
532 ofpbuf_delete(reply);
539 put_action(struct ofpbuf *b, size_t size, uint16_t type)
541 struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
542 ah->type = htons(type);
543 ah->len = htons(size);
547 static struct ofp_action_output *
548 put_output_action(struct ofpbuf *b, uint16_t port)
550 struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
551 oao->port = htons(port);
556 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
558 struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
559 str_to_mac(addr, oada->dl_addr);
564 parse_port_name(const char *name, uint16_t *port)
570 static const struct pair pairs[] = {
571 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
577 DEF_PAIR(CONTROLLER),
582 static const int n_pairs = ARRAY_SIZE(pairs);
585 for (i = 0; i < n_pairs; i++) {
586 if (!strcasecmp(name, pairs[i].name)) {
587 *port = pairs[i].value;
595 str_to_action(char *str, struct ofpbuf *b)
598 char *saveptr = NULL;
602 for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
603 act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++)
608 ovs_fatal(0, "Drop actions must not be followed by other actions");
611 /* Arguments are separated by colons */
612 arg = strchr(act, ':');
618 if (!strcasecmp(act, "mod_vlan_vid")) {
619 struct ofp_action_vlan_vid *va;
620 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
621 va->vlan_vid = htons(str_to_u32(arg));
622 } else if (!strcasecmp(act, "mod_vlan_pcp")) {
623 struct ofp_action_vlan_pcp *va;
624 va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
625 va->vlan_pcp = str_to_u32(arg);
626 } else if (!strcasecmp(act, "strip_vlan")) {
627 struct ofp_action_header *ah;
628 ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
629 ah->type = htons(OFPAT_STRIP_VLAN);
630 } else if (!strcasecmp(act, "mod_dl_src")) {
631 put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
632 } else if (!strcasecmp(act, "mod_dl_dst")) {
633 put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
634 } else if (!strcasecmp(act, "mod_nw_src")) {
635 struct ofp_action_nw_addr *na;
636 na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
637 str_to_ip(arg, &na->nw_addr);
638 } else if (!strcasecmp(act, "mod_nw_dst")) {
639 struct ofp_action_nw_addr *na;
640 na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
641 str_to_ip(arg, &na->nw_addr);
642 } else if (!strcasecmp(act, "mod_tp_src")) {
643 struct ofp_action_tp_port *ta;
644 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
645 ta->tp_port = htons(str_to_u32(arg));
646 } else if (!strcasecmp(act, "mod_tp_dst")) {
647 struct ofp_action_tp_port *ta;
648 ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
649 ta->tp_port = htons(str_to_u32(arg));
650 } else if (!strcasecmp(act, "mod_nw_tos")) {
651 struct ofp_action_nw_tos *nt;
652 nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
653 nt->nw_tos = str_to_u32(arg);
654 } else if (!strcasecmp(act, "resubmit")) {
655 struct nx_action_resubmit *nar;
656 nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
657 nar->vendor = htonl(NX_VENDOR_ID);
658 nar->subtype = htons(NXAST_RESUBMIT);
659 nar->in_port = htons(str_to_u32(arg));
660 } else if (!strcasecmp(act, "set_tunnel")) {
661 struct nx_action_set_tunnel *nast;
662 nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
663 nast->vendor = htonl(NX_VENDOR_ID);
664 nast->subtype = htons(NXAST_SET_TUNNEL);
665 nast->tun_id = htonl(str_to_u32(arg));
666 } else if (!strcasecmp(act, "output")) {
667 put_output_action(b, str_to_u32(arg));
668 } else if (!strcasecmp(act, "drop")) {
669 /* A drop action in OpenFlow occurs by just not setting
673 ovs_fatal(0, "Drop actions must not be preceded by other "
676 } else if (!strcasecmp(act, "CONTROLLER")) {
677 struct ofp_action_output *oao;
678 oao = put_output_action(b, OFPP_CONTROLLER);
680 /* Unless a numeric argument is specified, we send the whole
681 * packet to the controller. */
682 if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
683 oao->max_len = htons(str_to_u32(arg));
685 oao->max_len = htons(UINT16_MAX);
687 } else if (parse_port_name(act, &port)) {
688 put_output_action(b, port);
689 } else if (strspn(act, "0123456789") == strlen(act)) {
690 put_output_action(b, str_to_u32(act));
692 ovs_fatal(0, "Unknown action: %s", act);
704 parse_protocol(const char *name, const struct protocol **p_out)
706 static const struct protocol protocols[] = {
707 { "ip", ETH_TYPE_IP, 0 },
708 { "arp", ETH_TYPE_ARP, 0 },
709 { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
710 { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
711 { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
713 const struct protocol *p;
715 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
716 if (!strcmp(p->name, name)) {
728 enum { F_U8, F_U16, F_MAC, F_IP } type;
729 size_t offset, shift;
733 parse_field(const char *name, const struct field **f_out)
735 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
736 static const struct field fields[] = {
737 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
738 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
739 { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
740 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
741 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
742 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
743 { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
744 F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
745 { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
746 F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
747 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
748 { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
749 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
750 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
751 { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
752 { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
754 const struct field *f;
756 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
757 if (!strcmp(f->name, name)) {
767 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
768 uint8_t *table_idx, uint16_t *out_port, uint16_t *priority,
769 uint16_t *idle_timeout, uint16_t *hard_timeout,
772 char *save_ptr = NULL;
780 *out_port = OFPP_NONE;
783 *priority = OFP_DEFAULT_PRIORITY;
786 *idle_timeout = DEFAULT_IDLE_TIMEOUT;
789 *hard_timeout = OFP_FLOW_PERMANENT;
795 char *act_str = strstr(string, "action");
797 ovs_fatal(0, "must specify an action");
801 act_str = strchr(act_str + 1, '=');
803 ovs_fatal(0, "must specify an action");
808 str_to_action(act_str, actions);
810 memset(match, 0, sizeof *match);
811 wildcards = OFPFW_ALL;
812 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
813 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
814 const struct protocol *p;
816 if (parse_protocol(name, &p)) {
817 wildcards &= ~OFPFW_DL_TYPE;
818 match->dl_type = htons(p->dl_type);
820 wildcards &= ~OFPFW_NW_PROTO;
821 match->nw_proto = p->nw_proto;
824 const struct field *f;
827 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
829 ovs_fatal(0, "field %s missing value", name);
832 if (table_idx && !strcmp(name, "table")) {
833 *table_idx = atoi(value);
834 } else if (out_port && !strcmp(name, "out_port")) {
835 *out_port = atoi(value);
836 } else if (priority && !strcmp(name, "priority")) {
837 *priority = atoi(value);
838 } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
839 *idle_timeout = atoi(value);
840 } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
841 *hard_timeout = atoi(value);
842 } else if (cookie && !strcmp(name, "cookie")) {
843 *cookie = str_to_u64(value);
844 } else if (!strcmp(name, "tun_id_wild")) {
845 wildcards |= NXFW_TUN_ID;
846 } else if (parse_field(name, &f)) {
847 void *data = (char *) match + f->offset;
848 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
849 wildcards |= f->wildcard;
851 wildcards &= ~f->wildcard;
852 if (f->wildcard == OFPFW_IN_PORT
853 && parse_port_name(value, (uint16_t *) data)) {
855 } else if (f->type == F_U8) {
856 *(uint8_t *) data = str_to_u32(value);
857 } else if (f->type == F_U16) {
858 *(uint16_t *) data = htons(str_to_u32(value));
859 } else if (f->type == F_MAC) {
860 str_to_mac(value, data);
861 } else if (f->type == F_IP) {
862 wildcards |= str_to_ip(value, data) << f->shift;
868 ovs_fatal(0, "unknown keyword %s", name);
872 match->wildcards = htonl(wildcards);
876 do_dump_flows(int argc, char *argv[])
878 struct ofp_flow_stats_request *req;
880 struct ofpbuf *request;
882 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
883 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
884 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
885 memset(&req->pad, 0, sizeof req->pad);
886 req->out_port = htons(out_port);
888 dump_stats_transaction(argv[1], request);
892 do_dump_aggregate(int argc, char *argv[])
894 struct ofp_aggregate_stats_request *req;
895 struct ofpbuf *request;
898 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
899 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
900 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
901 memset(&req->pad, 0, sizeof req->pad);
902 req->out_port = htons(out_port);
904 dump_stats_transaction(argv[1], request);
908 do_add_flow(int argc OVS_UNUSED, char *argv[])
911 struct ofpbuf *buffer;
912 struct ofp_flow_mod *ofm;
913 uint16_t priority, idle_timeout, hard_timeout;
915 struct ofp_match match;
917 /* Parse and send. str_to_flow() will expand and reallocate the data in
918 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
919 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
920 str_to_flow(argv[2], &match, buffer,
921 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
925 ofm->command = htons(OFPFC_ADD);
926 ofm->cookie = htonll(cookie);
927 ofm->idle_timeout = htons(idle_timeout);
928 ofm->hard_timeout = htons(hard_timeout);
929 ofm->buffer_id = htonl(UINT32_MAX);
930 ofm->priority = htons(priority);
932 open_vconn(argv[1], &vconn);
933 send_openflow_buffer(vconn, buffer);
938 do_add_flows(int argc OVS_UNUSED, char *argv[])
944 file = fopen(argv[2], "r");
946 ovs_fatal(errno, "%s: open", argv[2]);
949 open_vconn(argv[1], &vconn);
950 while (fgets(line, sizeof line, file)) {
951 struct ofpbuf *buffer;
952 struct ofp_flow_mod *ofm;
953 uint16_t priority, idle_timeout, hard_timeout;
955 struct ofp_match match;
959 /* Delete comments. */
960 comment = strchr(line, '#');
965 /* Drop empty lines. */
966 if (line[strspn(line, " \t\n")] == '\0') {
970 /* Parse and send. str_to_flow() will expand and reallocate the data
971 * in 'buffer', so we can't keep pointers to across the str_to_flow()
973 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
974 str_to_flow(line, &match, buffer,
975 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
979 ofm->command = htons(OFPFC_ADD);
980 ofm->cookie = htonll(cookie);
981 ofm->idle_timeout = htons(idle_timeout);
982 ofm->hard_timeout = htons(hard_timeout);
983 ofm->buffer_id = htonl(UINT32_MAX);
984 ofm->priority = htons(priority);
986 send_openflow_buffer(vconn, buffer);
993 do_mod_flows(int argc OVS_UNUSED, char *argv[])
995 uint16_t priority, idle_timeout, hard_timeout;
998 struct ofpbuf *buffer;
999 struct ofp_flow_mod *ofm;
1000 struct ofp_match match;
1002 /* Parse and send. str_to_flow() will expand and reallocate the data in
1003 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1004 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1005 str_to_flow(argv[2], &match, buffer,
1006 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1011 ofm->command = htons(OFPFC_MODIFY_STRICT);
1013 ofm->command = htons(OFPFC_MODIFY);
1015 ofm->idle_timeout = htons(idle_timeout);
1016 ofm->hard_timeout = htons(hard_timeout);
1017 ofm->cookie = htonll(cookie);
1018 ofm->buffer_id = htonl(UINT32_MAX);
1019 ofm->priority = htons(priority);
1021 open_vconn(argv[1], &vconn);
1022 send_openflow_buffer(vconn, buffer);
1026 static void do_del_flows(int argc, char *argv[])
1028 struct vconn *vconn;
1031 struct ofpbuf *buffer;
1032 struct ofp_flow_mod *ofm;
1034 /* Parse and send. */
1035 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1036 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
1037 &out_port, &priority, NULL, NULL, NULL);
1039 ofm->command = htons(OFPFC_DELETE_STRICT);
1041 ofm->command = htons(OFPFC_DELETE);
1043 ofm->idle_timeout = htons(0);
1044 ofm->hard_timeout = htons(0);
1045 ofm->buffer_id = htonl(UINT32_MAX);
1046 ofm->out_port = htons(out_port);
1047 ofm->priority = htons(priority);
1049 open_vconn(argv[1], &vconn);
1050 send_openflow_buffer(vconn, buffer);
1055 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1057 struct nxt_tun_id_cookie *tun_id_cookie;
1058 struct ofpbuf *buffer;
1059 struct vconn *vconn;
1061 tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1063 tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1064 tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1065 tun_id_cookie->set = !strcmp(argv[2], "true");
1067 open_vconn(argv[1], &vconn);
1068 send_openflow_buffer(vconn, buffer);
1073 monitor_vconn(struct vconn *vconn)
1077 run(vconn_recv_block(vconn, &b), "vconn_recv");
1078 ofp_print(stderr, b->data, b->size, 2);
1084 do_monitor(int argc, char *argv[])
1086 struct vconn *vconn;
1088 open_vconn(argv[1], &vconn);
1090 int miss_send_len = atoi(argv[2]);
1091 struct ofp_switch_config *osc;
1094 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1095 osc->miss_send_len = htons(miss_send_len);
1096 send_openflow_buffer(vconn, buf);
1098 monitor_vconn(vconn);
1102 do_snoop(int argc OVS_UNUSED, char *argv[])
1104 struct vconn *vconn;
1106 open_vconn__(argv[1], "snoop", &vconn);
1107 monitor_vconn(vconn);
1111 do_dump_ports(int argc, char *argv[])
1113 struct ofp_port_stats_request *req;
1114 struct ofpbuf *request;
1117 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1118 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1119 req->port_no = htons(port);
1120 dump_stats_transaction(argv[1], request);
1124 do_probe(int argc OVS_UNUSED, char *argv[])
1126 struct ofpbuf *request;
1127 struct vconn *vconn;
1128 struct ofpbuf *reply;
1130 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1131 open_vconn(argv[1], &vconn);
1132 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1133 if (reply->size != sizeof(struct ofp_header)) {
1134 ovs_fatal(0, "reply does not match request");
1136 ofpbuf_delete(reply);
1141 do_mod_port(int argc OVS_UNUSED, char *argv[])
1143 struct ofpbuf *request, *reply;
1144 struct ofp_switch_features *osf;
1145 struct ofp_port_mod *opm;
1146 struct vconn *vconn;
1153 /* Check if the argument is a port index. Otherwise, treat it as
1155 port_no = strtol(argv[2], &endptr, 10);
1156 if (port_no == 0 && endptr == argv[2]) {
1160 /* Send a "Features Request" to get the information we need in order
1161 * to modify the port. */
1162 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1163 open_vconn(argv[1], &vconn);
1164 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1167 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1169 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1170 if (port_no != -1) {
1171 /* Check argument as a port index */
1172 if (osf->ports[port_idx].port_no == htons(port_no)) {
1176 /* Check argument as an interface name */
1177 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1178 sizeof osf->ports[0].name)) {
1184 if (port_idx == n_ports) {
1185 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1188 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1189 opm->port_no = osf->ports[port_idx].port_no;
1190 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1191 opm->config = htonl(0);
1192 opm->mask = htonl(0);
1193 opm->advertise = htonl(0);
1195 printf("modifying port: %s\n", osf->ports[port_idx].name);
1197 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1198 opm->mask |= htonl(OFPPC_PORT_DOWN);
1199 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1200 sizeof MOD_PORT_CMD_DOWN)) {
1201 opm->mask |= htonl(OFPPC_PORT_DOWN);
1202 opm->config |= htonl(OFPPC_PORT_DOWN);
1203 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1204 sizeof MOD_PORT_CMD_FLOOD)) {
1205 opm->mask |= htonl(OFPPC_NO_FLOOD);
1206 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1207 sizeof MOD_PORT_CMD_NOFLOOD)) {
1208 opm->mask |= htonl(OFPPC_NO_FLOOD);
1209 opm->config |= htonl(OFPPC_NO_FLOOD);
1211 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1214 send_openflow_buffer(vconn, request);
1216 ofpbuf_delete(reply);
1221 do_ping(int argc, char *argv[])
1223 size_t max_payload = 65535 - sizeof(struct ofp_header);
1224 unsigned int payload;
1225 struct vconn *vconn;
1228 payload = argc > 2 ? atoi(argv[2]) : 64;
1229 if (payload > max_payload) {
1230 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1233 open_vconn(argv[1], &vconn);
1234 for (i = 0; i < 10; i++) {
1235 struct timeval start, end;
1236 struct ofpbuf *request, *reply;
1237 struct ofp_header *rq_hdr, *rpy_hdr;
1239 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1240 OFPT_ECHO_REQUEST, &request);
1241 random_bytes(rq_hdr + 1, payload);
1243 gettimeofday(&start, NULL);
1244 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1245 gettimeofday(&end, NULL);
1247 rpy_hdr = reply->data;
1248 if (reply->size != request->size
1249 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1250 || rpy_hdr->xid != rq_hdr->xid
1251 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1252 printf("Reply does not match request. Request:\n");
1253 ofp_print(stdout, request, request->size, 2);
1255 ofp_print(stdout, reply, reply->size, 2);
1257 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1258 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1259 (1000*(double)(end.tv_sec - start.tv_sec))
1260 + (.001*(end.tv_usec - start.tv_usec)));
1261 ofpbuf_delete(request);
1262 ofpbuf_delete(reply);
1268 do_benchmark(int argc OVS_UNUSED, char *argv[])
1270 size_t max_payload = 65535 - sizeof(struct ofp_header);
1271 struct timeval start, end;
1272 unsigned int payload_size, message_size;
1273 struct vconn *vconn;
1278 payload_size = atoi(argv[2]);
1279 if (payload_size > max_payload) {
1280 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1282 message_size = sizeof(struct ofp_header) + payload_size;
1284 count = atoi(argv[3]);
1286 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1287 count, message_size, count * message_size);
1289 open_vconn(argv[1], &vconn);
1290 gettimeofday(&start, NULL);
1291 for (i = 0; i < count; i++) {
1292 struct ofpbuf *request, *reply;
1293 struct ofp_header *rq_hdr;
1295 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1296 memset(rq_hdr + 1, 0, payload_size);
1297 run(vconn_transact(vconn, request, &reply), "transact");
1298 ofpbuf_delete(reply);
1300 gettimeofday(&end, NULL);
1303 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1304 + (.001*(end.tv_usec - start.tv_usec)));
1305 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1306 duration, count / (duration / 1000.0),
1307 count * message_size / (duration / 1000.0));
1311 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1316 static const struct command all_commands[] = {
1317 { "show", 1, 1, do_show },
1318 { "status", 1, 2, do_status },
1319 { "monitor", 1, 2, do_monitor },
1320 { "snoop", 1, 1, do_snoop },
1321 { "dump-desc", 1, 1, do_dump_desc },
1322 { "dump-tables", 1, 1, do_dump_tables },
1323 { "dump-flows", 1, 2, do_dump_flows },
1324 { "dump-aggregate", 1, 2, do_dump_aggregate },
1325 { "add-flow", 2, 2, do_add_flow },
1326 { "add-flows", 2, 2, do_add_flows },
1327 { "mod-flows", 2, 2, do_mod_flows },
1328 { "del-flows", 1, 2, do_del_flows },
1329 { "tun-cookie", 2, 2, do_tun_cookie },
1330 { "dump-ports", 1, 2, do_dump_ports },
1331 { "mod-port", 3, 3, do_mod_port },
1332 { "probe", 1, 1, do_probe },
1333 { "ping", 1, 2, do_ping },
1334 { "benchmark", 3, 3, do_benchmark },
1335 { "help", 0, INT_MAX, do_help },
1336 { NULL, 0, 0, NULL },