ovs-ofctl: Warn about flows not in normal form.
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32
33 #include "command-line.h"
34 #include "compiler.h"
35 #include "dirs.h"
36 #include "dpif.h"
37 #include "dynamic-string.h"
38 #include "netdev.h"
39 #include "netlink.h"
40 #include "odp-util.h"
41 #include "ofp-print.h"
42 #include "ofp-util.h"
43 #include "ofpbuf.h"
44 #include "openflow/nicira-ext.h"
45 #include "openflow/openflow.h"
46 #include "packets.h"
47 #include "random.h"
48 #include "socket-util.h"
49 #include "stream-ssl.h"
50 #include "timeval.h"
51 #include "util.h"
52 #include "vconn.h"
53 #include "xtoxll.h"
54
55 #include "vlog.h"
56 #define THIS_MODULE VLM_ofctl
57
58 #define DEFAULT_IDLE_TIMEOUT 60
59
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"
64
65 /* Use strict matching for flow mod commands? */
66 static bool strict;
67
68 static const struct command all_commands[];
69
70 static void usage(void) NO_RETURN;
71 static void parse_options(int argc, char *argv[]);
72
73 int
74 main(int argc, char *argv[])
75 {
76     set_program_name(argv[0]);
77     time_init();
78     vlog_init();
79     parse_options(argc, argv);
80     signal(SIGPIPE, SIG_IGN);
81     run_command(argc - optind, argv + optind, all_commands);
82     return 0;
83 }
84
85 static void
86 parse_options(int argc, char *argv[])
87 {
88     enum {
89         OPT_STRICT = UCHAR_MAX + 1,
90         VLOG_OPTION_ENUMS
91     };
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'},
97         VLOG_LONG_OPTIONS,
98         STREAM_SSL_LONG_OPTIONS
99         {0, 0, 0, 0},
100     };
101     char *short_options = long_options_to_short_options(long_options);
102
103     for (;;) {
104         unsigned long int timeout;
105         int c;
106
107         c = getopt_long(argc, argv, short_options, long_options, NULL);
108         if (c == -1) {
109             break;
110         }
111
112         switch (c) {
113         case 't':
114             timeout = strtoul(optarg, NULL, 10);
115             if (timeout <= 0) {
116                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
117                           optarg);
118             } else {
119                 time_alarm(timeout);
120             }
121             break;
122
123         case 'h':
124             usage();
125
126         case 'V':
127             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
128             exit(EXIT_SUCCESS);
129
130         case OPT_STRICT:
131             strict = true;
132             break;
133
134         VLOG_OPTION_HANDLERS
135         STREAM_SSL_OPTION_HANDLERS
136
137         case '?':
138             exit(EXIT_FAILURE);
139
140         default:
141             abort();
142         }
143     }
144     free(short_options);
145 }
146
147 static void
148 usage(void)
149 {
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);
175     vlog_usage();
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");
181     exit(EXIT_SUCCESS);
182 }
183
184 static void run(int retval, const char *message, ...)
185     PRINTF_FORMAT(2, 3);
186
187 static void run(int retval, const char *message, ...)
188 {
189     if (retval) {
190         va_list args;
191
192         fprintf(stderr, "%s: ", program_name);
193         va_start(args, message);
194         vfprintf(stderr, message, args);
195         va_end(args);
196         if (retval == EOF) {
197             fputs(": unexpected end of file\n", stderr);
198         } else {
199             fprintf(stderr, ": %s\n", strerror(retval));
200         }
201
202         exit(EXIT_FAILURE);
203     }
204 }
205 \f
206 /* Generic commands. */
207
208 static void
209 open_vconn_socket(const char *name, struct vconn **vconnp)
210 {
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);
215     free(vconn_name);
216 }
217
218 static void
219 open_vconn__(const char *name, const char *default_suffix,
220              struct vconn **vconnp)
221 {
222     struct dpif *dpif;
223     struct stat s;
224     char *bridge_path, *datapath_name, *datapath_type;
225
226     bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
227     dp_parse_name(name, &datapath_name, &datapath_type);
228
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];
238         char *socket_name;
239
240         run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
241             "obtaining name of %s", dpif_name);
242         dpif_close(dpif);
243         if (strcmp(dpif_name, name)) {
244             VLOG_INFO("datapath %s is named %s", name, dpif_name);
245         }
246
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",
251                       name, socket_name);
252         } else if (!S_ISSOCK(s.st_mode)) {
253             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
254                       name, socket_name);
255         }
256
257         open_vconn_socket(socket_name, vconnp);
258         free(socket_name);
259     } else {
260         ovs_fatal(0, "%s is not a valid connection method", name);
261     }
262
263     free(datapath_name);
264     free(datapath_type);
265     free(bridge_path);
266 }
267
268 static void
269 open_vconn(const char *name, struct vconn **vconnp)
270 {
271     return open_vconn__(name, "mgmt", vconnp);
272 }
273
274 static void *
275 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
276 {
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);
282     return rq->body;
283 }
284
285 static void
286 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
287 {
288     update_openflow_length(buffer);
289     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
290 }
291
292 static void
293 dump_transaction(const char *vconn_name, struct ofpbuf *request)
294 {
295     struct vconn *vconn;
296     struct ofpbuf *reply;
297
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);
302     vconn_close(vconn);
303 }
304
305 static void
306 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
307 {
308     struct ofpbuf *request;
309     make_openflow(sizeof(struct ofp_header), request_type, &request);
310     dump_transaction(vconn_name, request);
311 }
312
313 static void
314 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
315 {
316     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
317     struct vconn *vconn;
318     bool done = false;
319
320     open_vconn(vconn_name, &vconn);
321     send_openflow_buffer(vconn, request);
322     while (!done) {
323         uint32_t recv_xid;
324         struct ofpbuf *reply;
325
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;
330
331             ofp_print(stdout, reply->data, reply->size, 1);
332
333             osr = ofpbuf_at(reply, 0, sizeof *osr);
334             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
335         } else {
336             VLOG_DBG("received reply with xid %08"PRIx32" "
337                      "!= expected %08"PRIx32, recv_xid, send_xid);
338         }
339         ofpbuf_delete(reply);
340     }
341     vconn_close(vconn);
342 }
343
344 static void
345 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
346 {
347     struct ofpbuf *request;
348     alloc_stats_request(0, stats_type, &request);
349     dump_stats_transaction(vconn_name, request);
350 }
351
352 static void
353 do_show(int argc OVS_UNUSED, char *argv[])
354 {
355     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
356     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
357 }
358
359 static void
360 do_status(int argc, char *argv[])
361 {
362     struct nicira_header *request, *reply;
363     struct vconn *vconn;
364     struct ofpbuf *b;
365
366     request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
367     request->vendor = htonl(NX_VENDOR_ID);
368     request->subtype = htonl(NXT_STATUS_REQUEST);
369     if (argc > 2) {
370         ofpbuf_put(b, argv[2], strlen(argv[2]));
371         update_openflow_length(b);
372     }
373     open_vconn(argv[1], &vconn);
374     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
375     vconn_close(vconn);
376
377     if (b->size < sizeof *reply) {
378         ovs_fatal(0, "short reply (%zu bytes)", b->size);
379     }
380     reply = b->data;
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");
386     }
387
388     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
389 }
390
391 static void
392 do_dump_desc(int argc OVS_UNUSED, char *argv[])
393 {
394     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
395 }
396
397 static void
398 do_dump_tables(int argc OVS_UNUSED, char *argv[])
399 {
400     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
401 }
402
403
404 static uint32_t
405 str_to_u32(const char *str) 
406 {
407     char *tail;
408     uint32_t value;
409
410     errno = 0;
411     value = strtoul(str, &tail, 0);
412     if (errno == EINVAL || errno == ERANGE || *tail) {
413         ovs_fatal(0, "invalid numeric format %s", str);
414     }
415     return value;
416 }
417
418 static uint64_t
419 str_to_u64(const char *str) 
420 {
421     char *tail;
422     uint64_t value;
423
424     errno = 0;
425     value = strtoull(str, &tail, 0);
426     if (errno == EINVAL || errno == ERANGE || *tail) {
427         ovs_fatal(0, "invalid numeric format %s", str);
428     }
429     return value;
430 }
431
432 static void
433 str_to_mac(const char *str, uint8_t mac[6]) 
434 {
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);
438     }
439 }
440
441 static uint32_t
442 str_to_ip(const char *str_, uint32_t *ip)
443 {
444     char *str = xstrdup(str_);
445     char *save_ptr = NULL;
446     const char *name, *netmask;
447     struct in_addr in_addr;
448     int n_wild, retval;
449
450     name = strtok_r(str, "/", &save_ptr);
451     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
452     if (retval) {
453         ovs_fatal(0, "%s: could not convert to IP address", str);
454     }
455     *ip = in_addr.s_addr;
456
457     netmask = strtok_r(NULL, "/", &save_ptr);
458     if (netmask) {
459         uint8_t o[4];
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];
463             int i;
464
465             /* Find first 1-bit. */
466             for (i = 0; i < 32; i++) {
467                 if (nm & (1u << i)) {
468                     break;
469                 }
470             }
471             n_wild = i;
472
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",
477                               str, netmask);
478                 }
479             }
480         } else {
481             int prefix = atoi(netmask);
482             if (prefix <= 0 || prefix > 32) {
483                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
484                           str);
485             }
486             n_wild = 32 - prefix;
487         }
488     } else {
489         n_wild = 0;
490     }
491
492     free(str);
493     return n_wild;
494 }
495
496 static uint16_t
497 str_to_port_no(const char *vconn_name, const char *str)
498 {
499     struct ofpbuf *request, *reply;
500     struct ofp_switch_features *osf;
501     struct vconn *vconn;
502     int n_ports;
503     int port_idx;
504     unsigned int port_no;
505     
506
507     /* Check if the argument is a port index.  Otherwise, treat it as
508      * the port name. */
509     if (str_to_uint(str, 10, &port_no)) {
510         return port_no;
511     }
512
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);
517
518     osf = reply->data;
519     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
520
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)) {
525             break;
526         }
527     }
528     if (port_idx == n_ports) {
529         ovs_fatal(0, "couldn't find monitored port: %s", str);
530     }
531
532     ofpbuf_delete(reply);
533     vconn_close(vconn);
534
535     return port_idx;
536 }
537
538 static void *
539 put_action(struct ofpbuf *b, size_t size, uint16_t type)
540 {
541     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
542     ah->type = htons(type);
543     ah->len = htons(size);
544     return ah;
545 }
546
547 static struct ofp_action_output *
548 put_output_action(struct ofpbuf *b, uint16_t port)
549 {
550     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
551     oao->port = htons(port);
552     return oao;
553 }
554
555 static void
556 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
557 {
558     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
559     str_to_mac(addr, oada->dl_addr);
560 }
561
562
563 static bool
564 parse_port_name(const char *name, uint16_t *port)
565 {
566     struct pair {
567         const char *name;
568         uint16_t value;
569     };
570     static const struct pair pairs[] = {
571 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
572         DEF_PAIR(IN_PORT),
573         DEF_PAIR(TABLE),
574         DEF_PAIR(NORMAL),
575         DEF_PAIR(FLOOD),
576         DEF_PAIR(ALL),
577         DEF_PAIR(CONTROLLER),
578         DEF_PAIR(LOCAL),
579         DEF_PAIR(NONE),
580 #undef DEF_PAIR
581     };
582     static const int n_pairs = ARRAY_SIZE(pairs);
583     size_t i;
584
585     for (i = 0; i < n_pairs; i++) {
586         if (!strcasecmp(name, pairs[i].name)) {
587             *port = pairs[i].value;
588             return true;
589         }
590     }
591     return false;
592 }
593
594 static void
595 str_to_action(char *str, struct ofpbuf *b)
596 {
597     char *act, *arg;
598     char *saveptr = NULL;
599     bool drop = false;
600     int n_actions;
601
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++) 
604     {
605         uint16_t port;
606
607         if (drop) {
608             ovs_fatal(0, "Drop actions must not be followed by other actions");
609         }
610
611         /* Arguments are separated by colons */
612         arg = strchr(act, ':');
613         if (arg) {
614             *arg = '\0';
615             arg++;
616         }
617
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 
670              * an action. */
671             drop = true;
672             if (n_actions) {
673                 ovs_fatal(0, "Drop actions must not be preceded by other "
674                           "actions");
675             }
676         } else if (!strcasecmp(act, "CONTROLLER")) {
677             struct ofp_action_output *oao;
678             oao = put_output_action(b, OFPP_CONTROLLER);
679
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));
684             } else {
685                 oao->max_len = htons(UINT16_MAX);
686             }
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));
691         } else {
692             ovs_fatal(0, "Unknown action: %s", act);
693         }
694     }
695 }
696
697 struct protocol {
698     const char *name;
699     uint16_t dl_type;
700     uint8_t nw_proto;
701 };
702
703 static bool
704 parse_protocol(const char *name, const struct protocol **p_out)
705 {
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 },
712     };
713     const struct protocol *p;
714
715     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
716         if (!strcmp(p->name, name)) {
717             *p_out = p;
718             return true;
719         }
720     }
721     *p_out = NULL;
722     return false;
723 }
724
725 struct field {
726     const char *name;
727     uint32_t wildcard;
728     enum { F_U8, F_U16, F_MAC, F_IP } type;
729     size_t offset, shift;
730 };
731
732 static bool
733 parse_field(const char *name, const struct field **f_out) 
734 {
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 }
753     };
754     const struct field *f;
755
756     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
757         if (!strcmp(f->name, name)) {
758             *f_out = f;
759             return true;
760         }
761     }
762     *f_out = NULL;
763     return false;
764 }
765
766 static void
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, 
770             uint64_t *cookie)
771 {
772     struct ofp_match normalized;
773     char *save_ptr = NULL;
774     char *name;
775     uint32_t wildcards;
776
777     if (table_idx) {
778         *table_idx = 0xff;
779     }
780     if (out_port) {
781         *out_port = OFPP_NONE;
782     }
783     if (priority) {
784         *priority = OFP_DEFAULT_PRIORITY;
785     }
786     if (idle_timeout) {
787         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
788     }
789     if (hard_timeout) {
790         *hard_timeout = OFP_FLOW_PERMANENT;
791     }
792     if (cookie) {
793         *cookie = 0;
794     }
795     if (actions) {
796         char *act_str = strstr(string, "action");
797         if (!act_str) {
798             ovs_fatal(0, "must specify an action");
799         }
800         *act_str = '\0';
801
802         act_str = strchr(act_str + 1, '=');
803         if (!act_str) {
804             ovs_fatal(0, "must specify an action");
805         }
806
807         act_str++;
808
809         str_to_action(act_str, actions);
810     }
811     memset(match, 0, sizeof *match);
812     wildcards = OFPFW_ALL;
813     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
814          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
815         const struct protocol *p;
816
817         if (parse_protocol(name, &p)) {
818             wildcards &= ~OFPFW_DL_TYPE;
819             match->dl_type = htons(p->dl_type);
820             if (p->nw_proto) {
821                 wildcards &= ~OFPFW_NW_PROTO;
822                 match->nw_proto = p->nw_proto;
823             }
824         } else {
825             const struct field *f;
826             char *value;
827
828             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
829             if (!value) {
830                 ovs_fatal(0, "field %s missing value", name);
831             }
832         
833             if (table_idx && !strcmp(name, "table")) {
834                 *table_idx = atoi(value);
835             } else if (out_port && !strcmp(name, "out_port")) {
836                 *out_port = atoi(value);
837             } else if (priority && !strcmp(name, "priority")) {
838                 *priority = atoi(value);
839             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
840                 *idle_timeout = atoi(value);
841             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
842                 *hard_timeout = atoi(value);
843             } else if (cookie && !strcmp(name, "cookie")) {
844                 *cookie = str_to_u64(value);
845             } else if (!strcmp(name, "tun_id_wild")) {
846                 wildcards |= NXFW_TUN_ID;
847             } else if (parse_field(name, &f)) {
848                 void *data = (char *) match + f->offset;
849                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
850                     wildcards |= f->wildcard;
851                 } else {
852                     wildcards &= ~f->wildcard;
853                     if (f->wildcard == OFPFW_IN_PORT
854                         && parse_port_name(value, (uint16_t *) data)) {
855                         /* Nothing to do. */
856                     } else if (f->type == F_U8) {
857                         *(uint8_t *) data = str_to_u32(value);
858                     } else if (f->type == F_U16) {
859                         *(uint16_t *) data = htons(str_to_u32(value));
860                     } else if (f->type == F_MAC) {
861                         str_to_mac(value, data);
862                     } else if (f->type == F_IP) {
863                         wildcards |= str_to_ip(value, data) << f->shift;
864                     } else {
865                         NOT_REACHED();
866                     }
867                 }
868             } else {
869                 ovs_fatal(0, "unknown keyword %s", name);
870             }
871         }
872     }
873     match->wildcards = htonl(wildcards);
874
875     normalized = *match;
876     normalize_match(&normalized);
877     if (memcmp(match, &normalized, sizeof normalized)) {
878         char *old = ofp_match_to_literal_string(match);
879         char *new = ofp_match_to_literal_string(&normalized);
880         VLOG_WARN("The specified flow is not in normal form:");
881         VLOG_WARN(" as specified: %s", old);
882         VLOG_WARN("as normalized: %s", new);
883         free(old);
884         free(new);
885     }
886 }
887
888 static void
889 do_dump_flows(int argc, char *argv[])
890 {
891     struct ofp_flow_stats_request *req;
892     uint16_t out_port;
893     struct ofpbuf *request;
894
895     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
896     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
897                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
898     memset(&req->pad, 0, sizeof req->pad);
899     req->out_port = htons(out_port);
900
901     dump_stats_transaction(argv[1], request);
902 }
903
904 static void
905 do_dump_aggregate(int argc, char *argv[])
906 {
907     struct ofp_aggregate_stats_request *req;
908     struct ofpbuf *request;
909     uint16_t out_port;
910
911     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
912     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
913                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
914     memset(&req->pad, 0, sizeof req->pad);
915     req->out_port = htons(out_port);
916
917     dump_stats_transaction(argv[1], request);
918 }
919
920 static void
921 do_add_flow(int argc OVS_UNUSED, char *argv[])
922 {
923     struct vconn *vconn;
924     struct ofpbuf *buffer;
925     struct ofp_flow_mod *ofm;
926     uint16_t priority, idle_timeout, hard_timeout;
927     uint64_t cookie;
928     struct ofp_match match;
929
930     /* Parse and send.  str_to_flow() will expand and reallocate the data in
931      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
932     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
933     str_to_flow(argv[2], &match, buffer,
934                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
935                 &cookie);
936     ofm = buffer->data;
937     ofm->match = match;
938     ofm->command = htons(OFPFC_ADD);
939     ofm->cookie = htonll(cookie);
940     ofm->idle_timeout = htons(idle_timeout);
941     ofm->hard_timeout = htons(hard_timeout);
942     ofm->buffer_id = htonl(UINT32_MAX);
943     ofm->priority = htons(priority);
944
945     open_vconn(argv[1], &vconn);
946     send_openflow_buffer(vconn, buffer);
947     vconn_close(vconn);
948 }
949
950 static void
951 do_add_flows(int argc OVS_UNUSED, char *argv[])
952 {
953     struct vconn *vconn;
954     FILE *file;
955     char line[1024];
956
957     file = fopen(argv[2], "r");
958     if (file == NULL) {
959         ovs_fatal(errno, "%s: open", argv[2]);
960     }
961
962     open_vconn(argv[1], &vconn);
963     while (fgets(line, sizeof line, file)) {
964         struct ofpbuf *buffer;
965         struct ofp_flow_mod *ofm;
966         uint16_t priority, idle_timeout, hard_timeout;
967         uint64_t cookie;
968         struct ofp_match match;
969
970         char *comment;
971
972         /* Delete comments. */
973         comment = strchr(line, '#');
974         if (comment) {
975             *comment = '\0';
976         }
977
978         /* Drop empty lines. */
979         if (line[strspn(line, " \t\n")] == '\0') {
980             continue;
981         }
982
983         /* Parse and send.  str_to_flow() will expand and reallocate the data
984          * in 'buffer', so we can't keep pointers to across the str_to_flow()
985          * call. */
986         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
987         str_to_flow(line, &match, buffer,
988                     NULL, NULL, &priority, &idle_timeout, &hard_timeout,
989                     &cookie);
990         ofm = buffer->data;
991         ofm->match = match;
992         ofm->command = htons(OFPFC_ADD);
993         ofm->cookie = htonll(cookie);
994         ofm->idle_timeout = htons(idle_timeout);
995         ofm->hard_timeout = htons(hard_timeout);
996         ofm->buffer_id = htonl(UINT32_MAX);
997         ofm->priority = htons(priority);
998
999         send_openflow_buffer(vconn, buffer);
1000     }
1001     vconn_close(vconn);
1002     fclose(file);
1003 }
1004
1005 static void
1006 do_mod_flows(int argc OVS_UNUSED, char *argv[])
1007 {
1008     uint16_t priority, idle_timeout, hard_timeout;
1009     uint64_t cookie;
1010     struct vconn *vconn;
1011     struct ofpbuf *buffer;
1012     struct ofp_flow_mod *ofm;
1013     struct ofp_match match;
1014
1015     /* Parse and send.  str_to_flow() will expand and reallocate the data in
1016      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1017     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1018     str_to_flow(argv[2], &match, buffer,
1019                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1020                 &cookie);
1021     ofm = buffer->data;
1022     ofm->match = match;
1023     if (strict) {
1024         ofm->command = htons(OFPFC_MODIFY_STRICT);
1025     } else {
1026         ofm->command = htons(OFPFC_MODIFY);
1027     }
1028     ofm->idle_timeout = htons(idle_timeout);
1029     ofm->hard_timeout = htons(hard_timeout);
1030     ofm->cookie = htonll(cookie);
1031     ofm->buffer_id = htonl(UINT32_MAX);
1032     ofm->priority = htons(priority);
1033
1034     open_vconn(argv[1], &vconn);
1035     send_openflow_buffer(vconn, buffer);
1036     vconn_close(vconn);
1037 }
1038
1039 static void do_del_flows(int argc, char *argv[])
1040 {
1041     struct vconn *vconn;
1042     uint16_t priority;
1043     uint16_t out_port;
1044     struct ofpbuf *buffer;
1045     struct ofp_flow_mod *ofm;
1046
1047     /* Parse and send. */
1048     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1049     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
1050                 &out_port, &priority, NULL, NULL, NULL);
1051     if (strict) {
1052         ofm->command = htons(OFPFC_DELETE_STRICT);
1053     } else {
1054         ofm->command = htons(OFPFC_DELETE);
1055     }
1056     ofm->idle_timeout = htons(0);
1057     ofm->hard_timeout = htons(0);
1058     ofm->buffer_id = htonl(UINT32_MAX);
1059     ofm->out_port = htons(out_port);
1060     ofm->priority = htons(priority);
1061
1062     open_vconn(argv[1], &vconn);
1063     send_openflow_buffer(vconn, buffer);
1064     vconn_close(vconn);
1065 }
1066
1067 static void
1068 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1069 {
1070     struct nxt_tun_id_cookie *tun_id_cookie;
1071     struct ofpbuf *buffer;
1072     struct vconn *vconn;
1073
1074     tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1075
1076     tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1077     tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1078     tun_id_cookie->set = !strcmp(argv[2], "true");
1079
1080     open_vconn(argv[1], &vconn);
1081     send_openflow_buffer(vconn, buffer);
1082     vconn_close(vconn);
1083 }
1084
1085 static void
1086 monitor_vconn(struct vconn *vconn)
1087 {
1088     for (;;) {
1089         struct ofpbuf *b;
1090         run(vconn_recv_block(vconn, &b), "vconn_recv");
1091         ofp_print(stderr, b->data, b->size, 2);
1092         ofpbuf_delete(b);
1093     }
1094 }
1095
1096 static void
1097 do_monitor(int argc, char *argv[])
1098 {
1099     struct vconn *vconn;
1100
1101     open_vconn(argv[1], &vconn);
1102     if (argc > 2) {
1103         int miss_send_len = atoi(argv[2]);
1104         struct ofp_switch_config *osc;
1105         struct ofpbuf *buf;
1106
1107         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1108         osc->miss_send_len = htons(miss_send_len);
1109         send_openflow_buffer(vconn, buf);
1110     }
1111     monitor_vconn(vconn);
1112 }
1113
1114 static void
1115 do_snoop(int argc OVS_UNUSED, char *argv[])
1116 {
1117     struct vconn *vconn;
1118
1119     open_vconn__(argv[1], "snoop", &vconn);
1120     monitor_vconn(vconn);
1121 }
1122
1123 static void
1124 do_dump_ports(int argc, char *argv[])
1125 {
1126     struct ofp_port_stats_request *req;
1127     struct ofpbuf *request;
1128     uint16_t port;
1129
1130     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1131     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1132     req->port_no = htons(port);
1133     dump_stats_transaction(argv[1], request);
1134 }
1135
1136 static void
1137 do_probe(int argc OVS_UNUSED, char *argv[])
1138 {
1139     struct ofpbuf *request;
1140     struct vconn *vconn;
1141     struct ofpbuf *reply;
1142
1143     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1144     open_vconn(argv[1], &vconn);
1145     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1146     if (reply->size != sizeof(struct ofp_header)) {
1147         ovs_fatal(0, "reply does not match request");
1148     }
1149     ofpbuf_delete(reply);
1150     vconn_close(vconn);
1151 }
1152
1153 static void
1154 do_mod_port(int argc OVS_UNUSED, char *argv[])
1155 {
1156     struct ofpbuf *request, *reply;
1157     struct ofp_switch_features *osf;
1158     struct ofp_port_mod *opm;
1159     struct vconn *vconn;
1160     char *endptr;
1161     int n_ports;
1162     int port_idx;
1163     int port_no;
1164     
1165
1166     /* Check if the argument is a port index.  Otherwise, treat it as
1167      * the port name. */
1168     port_no = strtol(argv[2], &endptr, 10);
1169     if (port_no == 0 && endptr == argv[2]) {
1170         port_no = -1;
1171     }
1172
1173     /* Send a "Features Request" to get the information we need in order 
1174      * to modify the port. */
1175     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1176     open_vconn(argv[1], &vconn);
1177     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1178
1179     osf = reply->data;
1180     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1181
1182     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1183         if (port_no != -1) {
1184             /* Check argument as a port index */
1185             if (osf->ports[port_idx].port_no == htons(port_no)) {
1186                 break;
1187             }
1188         } else {
1189             /* Check argument as an interface name */
1190             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1191                         sizeof osf->ports[0].name)) {
1192                 break;
1193             }
1194
1195         }
1196     }
1197     if (port_idx == n_ports) {
1198         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1199     }
1200
1201     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1202     opm->port_no = osf->ports[port_idx].port_no;
1203     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1204     opm->config = htonl(0);
1205     opm->mask = htonl(0);
1206     opm->advertise = htonl(0);
1207
1208     printf("modifying port: %s\n", osf->ports[port_idx].name);
1209
1210     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1211         opm->mask |= htonl(OFPPC_PORT_DOWN);
1212     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1213                 sizeof MOD_PORT_CMD_DOWN)) {
1214         opm->mask |= htonl(OFPPC_PORT_DOWN);
1215         opm->config |= htonl(OFPPC_PORT_DOWN);
1216     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1217                 sizeof MOD_PORT_CMD_FLOOD)) {
1218         opm->mask |= htonl(OFPPC_NO_FLOOD);
1219     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1220                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1221         opm->mask |= htonl(OFPPC_NO_FLOOD);
1222         opm->config |= htonl(OFPPC_NO_FLOOD);
1223     } else {
1224         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1225     }
1226
1227     send_openflow_buffer(vconn, request);
1228
1229     ofpbuf_delete(reply);
1230     vconn_close(vconn);
1231 }
1232
1233 static void
1234 do_ping(int argc, char *argv[])
1235 {
1236     size_t max_payload = 65535 - sizeof(struct ofp_header);
1237     unsigned int payload;
1238     struct vconn *vconn;
1239     int i;
1240
1241     payload = argc > 2 ? atoi(argv[2]) : 64;
1242     if (payload > max_payload) {
1243         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1244     }
1245
1246     open_vconn(argv[1], &vconn);
1247     for (i = 0; i < 10; i++) {
1248         struct timeval start, end;
1249         struct ofpbuf *request, *reply;
1250         struct ofp_header *rq_hdr, *rpy_hdr;
1251
1252         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1253                                OFPT_ECHO_REQUEST, &request);
1254         random_bytes(rq_hdr + 1, payload);
1255
1256         gettimeofday(&start, NULL);
1257         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1258         gettimeofday(&end, NULL);
1259
1260         rpy_hdr = reply->data;
1261         if (reply->size != request->size
1262             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1263             || rpy_hdr->xid != rq_hdr->xid
1264             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1265             printf("Reply does not match request.  Request:\n");
1266             ofp_print(stdout, request, request->size, 2);
1267             printf("Reply:\n");
1268             ofp_print(stdout, reply, reply->size, 2);
1269         }
1270         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1271                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1272                    (1000*(double)(end.tv_sec - start.tv_sec))
1273                    + (.001*(end.tv_usec - start.tv_usec)));
1274         ofpbuf_delete(request);
1275         ofpbuf_delete(reply);
1276     }
1277     vconn_close(vconn);
1278 }
1279
1280 static void
1281 do_benchmark(int argc OVS_UNUSED, char *argv[])
1282 {
1283     size_t max_payload = 65535 - sizeof(struct ofp_header);
1284     struct timeval start, end;
1285     unsigned int payload_size, message_size;
1286     struct vconn *vconn;
1287     double duration;
1288     int count;
1289     int i;
1290
1291     payload_size = atoi(argv[2]);
1292     if (payload_size > max_payload) {
1293         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1294     }
1295     message_size = sizeof(struct ofp_header) + payload_size;
1296
1297     count = atoi(argv[3]);
1298
1299     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1300            count, message_size, count * message_size);
1301
1302     open_vconn(argv[1], &vconn);
1303     gettimeofday(&start, NULL);
1304     for (i = 0; i < count; i++) {
1305         struct ofpbuf *request, *reply;
1306         struct ofp_header *rq_hdr;
1307
1308         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1309         memset(rq_hdr + 1, 0, payload_size);
1310         run(vconn_transact(vconn, request, &reply), "transact");
1311         ofpbuf_delete(reply);
1312     }
1313     gettimeofday(&end, NULL);
1314     vconn_close(vconn);
1315
1316     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1317                 + (.001*(end.tv_usec - start.tv_usec)));
1318     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1319            duration, count / (duration / 1000.0),
1320            count * message_size / (duration / 1000.0));
1321 }
1322
1323 static void
1324 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1325 {
1326     usage();
1327 }
1328
1329 static const struct command all_commands[] = {
1330     { "show", 1, 1, do_show },
1331     { "status", 1, 2, do_status },
1332     { "monitor", 1, 2, do_monitor },
1333     { "snoop", 1, 1, do_snoop },
1334     { "dump-desc", 1, 1, do_dump_desc },
1335     { "dump-tables", 1, 1, do_dump_tables },
1336     { "dump-flows", 1, 2, do_dump_flows },
1337     { "dump-aggregate", 1, 2, do_dump_aggregate },
1338     { "add-flow", 2, 2, do_add_flow },
1339     { "add-flows", 2, 2, do_add_flows },
1340     { "mod-flows", 2, 2, do_mod_flows },
1341     { "del-flows", 1, 2, do_del_flows },
1342     { "tun-cookie", 2, 2, do_tun_cookie },
1343     { "dump-ports", 1, 2, do_dump_ports },
1344     { "mod-port", 3, 3, do_mod_port },
1345     { "probe", 1, 1, do_probe },
1346     { "ping", 1, 2, do_ping },
1347     { "benchmark", 3, 3, do_benchmark },
1348     { "help", 0, INT_MAX, do_help },
1349     { NULL, 0, 0, NULL },
1350 };