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]);
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 struct ofp_match normalized;
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);
875 normalize_match(&normalized);
876 if (memcmp(match, &normalized, sizeof normalized)) {
877 char *old = ofp_match_to_literal_string(match);
878 char *new = ofp_match_to_literal_string(&normalized);
879 VLOG_WARN("The specified flow is not in normal form:");
880 VLOG_WARN(" as specified: %s", old);
881 VLOG_WARN("as normalized: %s", new);
888 do_dump_flows(int argc, char *argv[])
890 struct ofp_flow_stats_request *req;
892 struct ofpbuf *request;
894 req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
895 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
896 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
897 memset(&req->pad, 0, sizeof req->pad);
898 req->out_port = htons(out_port);
900 dump_stats_transaction(argv[1], request);
904 do_dump_aggregate(int argc, char *argv[])
906 struct ofp_aggregate_stats_request *req;
907 struct ofpbuf *request;
910 req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
911 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
912 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
913 memset(&req->pad, 0, sizeof req->pad);
914 req->out_port = htons(out_port);
916 dump_stats_transaction(argv[1], request);
920 do_add_flow(int argc OVS_UNUSED, char *argv[])
923 struct ofpbuf *buffer;
924 struct ofp_flow_mod *ofm;
925 uint16_t priority, idle_timeout, hard_timeout;
927 struct ofp_match match;
929 /* Parse and send. str_to_flow() will expand and reallocate the data in
930 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
931 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
932 str_to_flow(argv[2], &match, buffer,
933 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
937 ofm->command = htons(OFPFC_ADD);
938 ofm->cookie = htonll(cookie);
939 ofm->idle_timeout = htons(idle_timeout);
940 ofm->hard_timeout = htons(hard_timeout);
941 ofm->buffer_id = htonl(UINT32_MAX);
942 ofm->priority = htons(priority);
944 open_vconn(argv[1], &vconn);
945 send_openflow_buffer(vconn, buffer);
950 do_add_flows(int argc OVS_UNUSED, char *argv[])
956 file = fopen(argv[2], "r");
958 ovs_fatal(errno, "%s: open", argv[2]);
961 open_vconn(argv[1], &vconn);
962 while (fgets(line, sizeof line, file)) {
963 struct ofpbuf *buffer;
964 struct ofp_flow_mod *ofm;
965 uint16_t priority, idle_timeout, hard_timeout;
967 struct ofp_match match;
971 /* Delete comments. */
972 comment = strchr(line, '#');
977 /* Drop empty lines. */
978 if (line[strspn(line, " \t\n")] == '\0') {
982 /* Parse and send. str_to_flow() will expand and reallocate the data
983 * in 'buffer', so we can't keep pointers to across the str_to_flow()
985 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
986 str_to_flow(line, &match, buffer,
987 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
991 ofm->command = htons(OFPFC_ADD);
992 ofm->cookie = htonll(cookie);
993 ofm->idle_timeout = htons(idle_timeout);
994 ofm->hard_timeout = htons(hard_timeout);
995 ofm->buffer_id = htonl(UINT32_MAX);
996 ofm->priority = htons(priority);
998 send_openflow_buffer(vconn, buffer);
1005 do_mod_flows(int argc OVS_UNUSED, char *argv[])
1007 uint16_t priority, idle_timeout, hard_timeout;
1009 struct vconn *vconn;
1010 struct ofpbuf *buffer;
1011 struct ofp_flow_mod *ofm;
1012 struct ofp_match match;
1014 /* Parse and send. str_to_flow() will expand and reallocate the data in
1015 * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1016 make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1017 str_to_flow(argv[2], &match, buffer,
1018 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1023 ofm->command = htons(OFPFC_MODIFY_STRICT);
1025 ofm->command = htons(OFPFC_MODIFY);
1027 ofm->idle_timeout = htons(idle_timeout);
1028 ofm->hard_timeout = htons(hard_timeout);
1029 ofm->cookie = htonll(cookie);
1030 ofm->buffer_id = htonl(UINT32_MAX);
1031 ofm->priority = htons(priority);
1033 open_vconn(argv[1], &vconn);
1034 send_openflow_buffer(vconn, buffer);
1038 static void do_del_flows(int argc, char *argv[])
1040 struct vconn *vconn;
1043 struct ofpbuf *buffer;
1044 struct ofp_flow_mod *ofm;
1046 /* Parse and send. */
1047 ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1048 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
1049 &out_port, &priority, NULL, NULL, NULL);
1051 ofm->command = htons(OFPFC_DELETE_STRICT);
1053 ofm->command = htons(OFPFC_DELETE);
1055 ofm->idle_timeout = htons(0);
1056 ofm->hard_timeout = htons(0);
1057 ofm->buffer_id = htonl(UINT32_MAX);
1058 ofm->out_port = htons(out_port);
1059 ofm->priority = htons(priority);
1061 open_vconn(argv[1], &vconn);
1062 send_openflow_buffer(vconn, buffer);
1067 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1069 struct nxt_tun_id_cookie *tun_id_cookie;
1070 struct ofpbuf *buffer;
1071 struct vconn *vconn;
1073 tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1075 tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1076 tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1077 tun_id_cookie->set = !strcmp(argv[2], "true");
1079 open_vconn(argv[1], &vconn);
1080 send_openflow_buffer(vconn, buffer);
1085 monitor_vconn(struct vconn *vconn)
1089 run(vconn_recv_block(vconn, &b), "vconn_recv");
1090 ofp_print(stderr, b->data, b->size, 2);
1096 do_monitor(int argc, char *argv[])
1098 struct vconn *vconn;
1100 open_vconn(argv[1], &vconn);
1102 int miss_send_len = atoi(argv[2]);
1103 struct ofp_switch_config *osc;
1106 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1107 osc->miss_send_len = htons(miss_send_len);
1108 send_openflow_buffer(vconn, buf);
1110 monitor_vconn(vconn);
1114 do_snoop(int argc OVS_UNUSED, char *argv[])
1116 struct vconn *vconn;
1118 open_vconn__(argv[1], "snoop", &vconn);
1119 monitor_vconn(vconn);
1123 do_dump_ports(int argc, char *argv[])
1125 struct ofp_port_stats_request *req;
1126 struct ofpbuf *request;
1129 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1130 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1131 req->port_no = htons(port);
1132 dump_stats_transaction(argv[1], request);
1136 do_probe(int argc OVS_UNUSED, char *argv[])
1138 struct ofpbuf *request;
1139 struct vconn *vconn;
1140 struct ofpbuf *reply;
1142 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1143 open_vconn(argv[1], &vconn);
1144 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1145 if (reply->size != sizeof(struct ofp_header)) {
1146 ovs_fatal(0, "reply does not match request");
1148 ofpbuf_delete(reply);
1153 do_mod_port(int argc OVS_UNUSED, char *argv[])
1155 struct ofpbuf *request, *reply;
1156 struct ofp_switch_features *osf;
1157 struct ofp_port_mod *opm;
1158 struct vconn *vconn;
1165 /* Check if the argument is a port index. Otherwise, treat it as
1167 port_no = strtol(argv[2], &endptr, 10);
1168 if (port_no == 0 && endptr == argv[2]) {
1172 /* Send a "Features Request" to get the information we need in order
1173 * to modify the port. */
1174 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1175 open_vconn(argv[1], &vconn);
1176 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1179 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1181 for (port_idx = 0; port_idx < n_ports; port_idx++) {
1182 if (port_no != -1) {
1183 /* Check argument as a port index */
1184 if (osf->ports[port_idx].port_no == htons(port_no)) {
1188 /* Check argument as an interface name */
1189 if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
1190 sizeof osf->ports[0].name)) {
1196 if (port_idx == n_ports) {
1197 ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1200 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1201 opm->port_no = osf->ports[port_idx].port_no;
1202 memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1203 opm->config = htonl(0);
1204 opm->mask = htonl(0);
1205 opm->advertise = htonl(0);
1207 printf("modifying port: %s\n", osf->ports[port_idx].name);
1209 if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1210 opm->mask |= htonl(OFPPC_PORT_DOWN);
1211 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
1212 sizeof MOD_PORT_CMD_DOWN)) {
1213 opm->mask |= htonl(OFPPC_PORT_DOWN);
1214 opm->config |= htonl(OFPPC_PORT_DOWN);
1215 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
1216 sizeof MOD_PORT_CMD_FLOOD)) {
1217 opm->mask |= htonl(OFPPC_NO_FLOOD);
1218 } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
1219 sizeof MOD_PORT_CMD_NOFLOOD)) {
1220 opm->mask |= htonl(OFPPC_NO_FLOOD);
1221 opm->config |= htonl(OFPPC_NO_FLOOD);
1223 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1226 send_openflow_buffer(vconn, request);
1228 ofpbuf_delete(reply);
1233 do_ping(int argc, char *argv[])
1235 size_t max_payload = 65535 - sizeof(struct ofp_header);
1236 unsigned int payload;
1237 struct vconn *vconn;
1240 payload = argc > 2 ? atoi(argv[2]) : 64;
1241 if (payload > max_payload) {
1242 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1245 open_vconn(argv[1], &vconn);
1246 for (i = 0; i < 10; i++) {
1247 struct timeval start, end;
1248 struct ofpbuf *request, *reply;
1249 struct ofp_header *rq_hdr, *rpy_hdr;
1251 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1252 OFPT_ECHO_REQUEST, &request);
1253 random_bytes(rq_hdr + 1, payload);
1255 gettimeofday(&start, NULL);
1256 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1257 gettimeofday(&end, NULL);
1259 rpy_hdr = reply->data;
1260 if (reply->size != request->size
1261 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1262 || rpy_hdr->xid != rq_hdr->xid
1263 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1264 printf("Reply does not match request. Request:\n");
1265 ofp_print(stdout, request, request->size, 2);
1267 ofp_print(stdout, reply, reply->size, 2);
1269 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1270 reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1271 (1000*(double)(end.tv_sec - start.tv_sec))
1272 + (.001*(end.tv_usec - start.tv_usec)));
1273 ofpbuf_delete(request);
1274 ofpbuf_delete(reply);
1280 do_benchmark(int argc OVS_UNUSED, char *argv[])
1282 size_t max_payload = 65535 - sizeof(struct ofp_header);
1283 struct timeval start, end;
1284 unsigned int payload_size, message_size;
1285 struct vconn *vconn;
1290 payload_size = atoi(argv[2]);
1291 if (payload_size > max_payload) {
1292 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1294 message_size = sizeof(struct ofp_header) + payload_size;
1296 count = atoi(argv[3]);
1298 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1299 count, message_size, count * message_size);
1301 open_vconn(argv[1], &vconn);
1302 gettimeofday(&start, NULL);
1303 for (i = 0; i < count; i++) {
1304 struct ofpbuf *request, *reply;
1305 struct ofp_header *rq_hdr;
1307 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1308 memset(rq_hdr + 1, 0, payload_size);
1309 run(vconn_transact(vconn, request, &reply), "transact");
1310 ofpbuf_delete(reply);
1312 gettimeofday(&end, NULL);
1315 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1316 + (.001*(end.tv_usec - start.tv_usec)));
1317 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1318 duration, count / (duration / 1000.0),
1319 count * message_size / (duration / 1000.0));
1323 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1328 static const struct command all_commands[] = {
1329 { "show", 1, 1, do_show },
1330 { "status", 1, 2, do_status },
1331 { "monitor", 1, 2, do_monitor },
1332 { "snoop", 1, 1, do_snoop },
1333 { "dump-desc", 1, 1, do_dump_desc },
1334 { "dump-tables", 1, 1, do_dump_tables },
1335 { "dump-flows", 1, 2, do_dump_flows },
1336 { "dump-aggregate", 1, 2, do_dump_aggregate },
1337 { "add-flow", 2, 2, do_add_flow },
1338 { "add-flows", 2, 2, do_add_flows },
1339 { "mod-flows", 2, 2, do_mod_flows },
1340 { "del-flows", 1, 2, do_del_flows },
1341 { "tun-cookie", 2, 2, do_tun_cookie },
1342 { "dump-ports", 1, 2, do_dump_ports },
1343 { "mod-port", 3, 3, do_mod_port },
1344 { "probe", 1, 1, do_probe },
1345 { "ping", 1, 2, do_ping },
1346 { "benchmark", 3, 3, do_benchmark },
1347 { "help", 0, INT_MAX, do_help },
1348 { NULL, 0, 0, NULL },