timeval: Make time_init() static and remove calls to it.
[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     vlog_init();
78     parse_options(argc, argv);
79     signal(SIGPIPE, SIG_IGN);
80     run_command(argc - optind, argv + optind, all_commands);
81     return 0;
82 }
83
84 static void
85 parse_options(int argc, char *argv[])
86 {
87     enum {
88         OPT_STRICT = UCHAR_MAX + 1,
89         VLOG_OPTION_ENUMS
90     };
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'},
96         VLOG_LONG_OPTIONS,
97         STREAM_SSL_LONG_OPTIONS
98         {0, 0, 0, 0},
99     };
100     char *short_options = long_options_to_short_options(long_options);
101
102     for (;;) {
103         unsigned long int timeout;
104         int c;
105
106         c = getopt_long(argc, argv, short_options, long_options, NULL);
107         if (c == -1) {
108             break;
109         }
110
111         switch (c) {
112         case 't':
113             timeout = strtoul(optarg, NULL, 10);
114             if (timeout <= 0) {
115                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
116                           optarg);
117             } else {
118                 time_alarm(timeout);
119             }
120             break;
121
122         case 'h':
123             usage();
124
125         case 'V':
126             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
127             exit(EXIT_SUCCESS);
128
129         case OPT_STRICT:
130             strict = true;
131             break;
132
133         VLOG_OPTION_HANDLERS
134         STREAM_SSL_OPTION_HANDLERS
135
136         case '?':
137             exit(EXIT_FAILURE);
138
139         default:
140             abort();
141         }
142     }
143     free(short_options);
144 }
145
146 static void
147 usage(void)
148 {
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);
174     vlog_usage();
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");
180     exit(EXIT_SUCCESS);
181 }
182
183 static void run(int retval, const char *message, ...)
184     PRINTF_FORMAT(2, 3);
185
186 static void run(int retval, const char *message, ...)
187 {
188     if (retval) {
189         va_list args;
190
191         fprintf(stderr, "%s: ", program_name);
192         va_start(args, message);
193         vfprintf(stderr, message, args);
194         va_end(args);
195         if (retval == EOF) {
196             fputs(": unexpected end of file\n", stderr);
197         } else {
198             fprintf(stderr, ": %s\n", strerror(retval));
199         }
200
201         exit(EXIT_FAILURE);
202     }
203 }
204 \f
205 /* Generic commands. */
206
207 static void
208 open_vconn_socket(const char *name, struct vconn **vconnp)
209 {
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);
214     free(vconn_name);
215 }
216
217 static void
218 open_vconn__(const char *name, const char *default_suffix,
219              struct vconn **vconnp)
220 {
221     struct dpif *dpif;
222     struct stat s;
223     char *bridge_path, *datapath_name, *datapath_type;
224
225     bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
226     dp_parse_name(name, &datapath_name, &datapath_type);
227
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];
237         char *socket_name;
238
239         run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
240             "obtaining name of %s", dpif_name);
241         dpif_close(dpif);
242         if (strcmp(dpif_name, name)) {
243             VLOG_INFO("datapath %s is named %s", name, dpif_name);
244         }
245
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",
250                       name, socket_name);
251         } else if (!S_ISSOCK(s.st_mode)) {
252             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
253                       name, socket_name);
254         }
255
256         open_vconn_socket(socket_name, vconnp);
257         free(socket_name);
258     } else {
259         ovs_fatal(0, "%s is not a valid connection method", name);
260     }
261
262     free(datapath_name);
263     free(datapath_type);
264     free(bridge_path);
265 }
266
267 static void
268 open_vconn(const char *name, struct vconn **vconnp)
269 {
270     return open_vconn__(name, "mgmt", vconnp);
271 }
272
273 static void *
274 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
275 {
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);
281     return rq->body;
282 }
283
284 static void
285 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
286 {
287     update_openflow_length(buffer);
288     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
289 }
290
291 static void
292 dump_transaction(const char *vconn_name, struct ofpbuf *request)
293 {
294     struct vconn *vconn;
295     struct ofpbuf *reply;
296
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);
301     vconn_close(vconn);
302 }
303
304 static void
305 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
306 {
307     struct ofpbuf *request;
308     make_openflow(sizeof(struct ofp_header), request_type, &request);
309     dump_transaction(vconn_name, request);
310 }
311
312 static void
313 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
314 {
315     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
316     struct vconn *vconn;
317     bool done = false;
318
319     open_vconn(vconn_name, &vconn);
320     send_openflow_buffer(vconn, request);
321     while (!done) {
322         uint32_t recv_xid;
323         struct ofpbuf *reply;
324
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;
329
330             ofp_print(stdout, reply->data, reply->size, 1);
331
332             osr = ofpbuf_at(reply, 0, sizeof *osr);
333             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
334         } else {
335             VLOG_DBG("received reply with xid %08"PRIx32" "
336                      "!= expected %08"PRIx32, recv_xid, send_xid);
337         }
338         ofpbuf_delete(reply);
339     }
340     vconn_close(vconn);
341 }
342
343 static void
344 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
345 {
346     struct ofpbuf *request;
347     alloc_stats_request(0, stats_type, &request);
348     dump_stats_transaction(vconn_name, request);
349 }
350
351 static void
352 do_show(int argc OVS_UNUSED, char *argv[])
353 {
354     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
355     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
356 }
357
358 static void
359 do_status(int argc, char *argv[])
360 {
361     struct nicira_header *request, *reply;
362     struct vconn *vconn;
363     struct ofpbuf *b;
364
365     request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
366     request->vendor = htonl(NX_VENDOR_ID);
367     request->subtype = htonl(NXT_STATUS_REQUEST);
368     if (argc > 2) {
369         ofpbuf_put(b, argv[2], strlen(argv[2]));
370         update_openflow_length(b);
371     }
372     open_vconn(argv[1], &vconn);
373     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
374     vconn_close(vconn);
375
376     if (b->size < sizeof *reply) {
377         ovs_fatal(0, "short reply (%zu bytes)", b->size);
378     }
379     reply = b->data;
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");
385     }
386
387     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
388 }
389
390 static void
391 do_dump_desc(int argc OVS_UNUSED, char *argv[])
392 {
393     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
394 }
395
396 static void
397 do_dump_tables(int argc OVS_UNUSED, char *argv[])
398 {
399     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
400 }
401
402
403 static uint32_t
404 str_to_u32(const char *str) 
405 {
406     char *tail;
407     uint32_t value;
408
409     errno = 0;
410     value = strtoul(str, &tail, 0);
411     if (errno == EINVAL || errno == ERANGE || *tail) {
412         ovs_fatal(0, "invalid numeric format %s", str);
413     }
414     return value;
415 }
416
417 static uint64_t
418 str_to_u64(const char *str) 
419 {
420     char *tail;
421     uint64_t value;
422
423     errno = 0;
424     value = strtoull(str, &tail, 0);
425     if (errno == EINVAL || errno == ERANGE || *tail) {
426         ovs_fatal(0, "invalid numeric format %s", str);
427     }
428     return value;
429 }
430
431 static void
432 str_to_mac(const char *str, uint8_t mac[6]) 
433 {
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);
437     }
438 }
439
440 static uint32_t
441 str_to_ip(const char *str_, uint32_t *ip)
442 {
443     char *str = xstrdup(str_);
444     char *save_ptr = NULL;
445     const char *name, *netmask;
446     struct in_addr in_addr;
447     int n_wild, retval;
448
449     name = strtok_r(str, "/", &save_ptr);
450     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
451     if (retval) {
452         ovs_fatal(0, "%s: could not convert to IP address", str);
453     }
454     *ip = in_addr.s_addr;
455
456     netmask = strtok_r(NULL, "/", &save_ptr);
457     if (netmask) {
458         uint8_t o[4];
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];
462             int i;
463
464             /* Find first 1-bit. */
465             for (i = 0; i < 32; i++) {
466                 if (nm & (1u << i)) {
467                     break;
468                 }
469             }
470             n_wild = i;
471
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",
476                               str, netmask);
477                 }
478             }
479         } else {
480             int prefix = atoi(netmask);
481             if (prefix <= 0 || prefix > 32) {
482                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
483                           str);
484             }
485             n_wild = 32 - prefix;
486         }
487     } else {
488         n_wild = 0;
489     }
490
491     free(str);
492     return n_wild;
493 }
494
495 static uint16_t
496 str_to_port_no(const char *vconn_name, const char *str)
497 {
498     struct ofpbuf *request, *reply;
499     struct ofp_switch_features *osf;
500     struct vconn *vconn;
501     int n_ports;
502     int port_idx;
503     unsigned int port_no;
504     
505
506     /* Check if the argument is a port index.  Otherwise, treat it as
507      * the port name. */
508     if (str_to_uint(str, 10, &port_no)) {
509         return port_no;
510     }
511
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);
516
517     osf = reply->data;
518     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
519
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)) {
524             break;
525         }
526     }
527     if (port_idx == n_ports) {
528         ovs_fatal(0, "couldn't find monitored port: %s", str);
529     }
530
531     ofpbuf_delete(reply);
532     vconn_close(vconn);
533
534     return port_idx;
535 }
536
537 static void *
538 put_action(struct ofpbuf *b, size_t size, uint16_t type)
539 {
540     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
541     ah->type = htons(type);
542     ah->len = htons(size);
543     return ah;
544 }
545
546 static struct ofp_action_output *
547 put_output_action(struct ofpbuf *b, uint16_t port)
548 {
549     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
550     oao->port = htons(port);
551     return oao;
552 }
553
554 static void
555 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
556 {
557     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
558     str_to_mac(addr, oada->dl_addr);
559 }
560
561
562 static bool
563 parse_port_name(const char *name, uint16_t *port)
564 {
565     struct pair {
566         const char *name;
567         uint16_t value;
568     };
569     static const struct pair pairs[] = {
570 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
571         DEF_PAIR(IN_PORT),
572         DEF_PAIR(TABLE),
573         DEF_PAIR(NORMAL),
574         DEF_PAIR(FLOOD),
575         DEF_PAIR(ALL),
576         DEF_PAIR(CONTROLLER),
577         DEF_PAIR(LOCAL),
578         DEF_PAIR(NONE),
579 #undef DEF_PAIR
580     };
581     static const int n_pairs = ARRAY_SIZE(pairs);
582     size_t i;
583
584     for (i = 0; i < n_pairs; i++) {
585         if (!strcasecmp(name, pairs[i].name)) {
586             *port = pairs[i].value;
587             return true;
588         }
589     }
590     return false;
591 }
592
593 static void
594 str_to_action(char *str, struct ofpbuf *b)
595 {
596     char *act, *arg;
597     char *saveptr = NULL;
598     bool drop = false;
599     int n_actions;
600
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++) 
603     {
604         uint16_t port;
605
606         if (drop) {
607             ovs_fatal(0, "Drop actions must not be followed by other actions");
608         }
609
610         /* Arguments are separated by colons */
611         arg = strchr(act, ':');
612         if (arg) {
613             *arg = '\0';
614             arg++;
615         }
616
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 
669              * an action. */
670             drop = true;
671             if (n_actions) {
672                 ovs_fatal(0, "Drop actions must not be preceded by other "
673                           "actions");
674             }
675         } else if (!strcasecmp(act, "CONTROLLER")) {
676             struct ofp_action_output *oao;
677             oao = put_output_action(b, OFPP_CONTROLLER);
678
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));
683             } else {
684                 oao->max_len = htons(UINT16_MAX);
685             }
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));
690         } else {
691             ovs_fatal(0, "Unknown action: %s", act);
692         }
693     }
694 }
695
696 struct protocol {
697     const char *name;
698     uint16_t dl_type;
699     uint8_t nw_proto;
700 };
701
702 static bool
703 parse_protocol(const char *name, const struct protocol **p_out)
704 {
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 },
711     };
712     const struct protocol *p;
713
714     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
715         if (!strcmp(p->name, name)) {
716             *p_out = p;
717             return true;
718         }
719     }
720     *p_out = NULL;
721     return false;
722 }
723
724 struct field {
725     const char *name;
726     uint32_t wildcard;
727     enum { F_U8, F_U16, F_MAC, F_IP } type;
728     size_t offset, shift;
729 };
730
731 static bool
732 parse_field(const char *name, const struct field **f_out) 
733 {
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 }
752     };
753     const struct field *f;
754
755     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
756         if (!strcmp(f->name, name)) {
757             *f_out = f;
758             return true;
759         }
760     }
761     *f_out = NULL;
762     return false;
763 }
764
765 static void
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, 
769             uint64_t *cookie)
770 {
771     struct ofp_match normalized;
772     char *save_ptr = NULL;
773     char *name;
774     uint32_t wildcards;
775
776     if (table_idx) {
777         *table_idx = 0xff;
778     }
779     if (out_port) {
780         *out_port = OFPP_NONE;
781     }
782     if (priority) {
783         *priority = OFP_DEFAULT_PRIORITY;
784     }
785     if (idle_timeout) {
786         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
787     }
788     if (hard_timeout) {
789         *hard_timeout = OFP_FLOW_PERMANENT;
790     }
791     if (cookie) {
792         *cookie = 0;
793     }
794     if (actions) {
795         char *act_str = strstr(string, "action");
796         if (!act_str) {
797             ovs_fatal(0, "must specify an action");
798         }
799         *act_str = '\0';
800
801         act_str = strchr(act_str + 1, '=');
802         if (!act_str) {
803             ovs_fatal(0, "must specify an action");
804         }
805
806         act_str++;
807
808         str_to_action(act_str, actions);
809     }
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;
815
816         if (parse_protocol(name, &p)) {
817             wildcards &= ~OFPFW_DL_TYPE;
818             match->dl_type = htons(p->dl_type);
819             if (p->nw_proto) {
820                 wildcards &= ~OFPFW_NW_PROTO;
821                 match->nw_proto = p->nw_proto;
822             }
823         } else {
824             const struct field *f;
825             char *value;
826
827             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
828             if (!value) {
829                 ovs_fatal(0, "field %s missing value", name);
830             }
831         
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;
850                 } else {
851                     wildcards &= ~f->wildcard;
852                     if (f->wildcard == OFPFW_IN_PORT
853                         && parse_port_name(value, (uint16_t *) data)) {
854                         /* Nothing to do. */
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;
863                     } else {
864                         NOT_REACHED();
865                     }
866                 }
867             } else {
868                 ovs_fatal(0, "unknown keyword %s", name);
869             }
870         }
871     }
872     match->wildcards = htonl(wildcards);
873
874     normalized = *match;
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);
882         free(old);
883         free(new);
884     }
885 }
886
887 static void
888 do_dump_flows(int argc, char *argv[])
889 {
890     struct ofp_flow_stats_request *req;
891     uint16_t out_port;
892     struct ofpbuf *request;
893
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);
899
900     dump_stats_transaction(argv[1], request);
901 }
902
903 static void
904 do_dump_aggregate(int argc, char *argv[])
905 {
906     struct ofp_aggregate_stats_request *req;
907     struct ofpbuf *request;
908     uint16_t out_port;
909
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);
915
916     dump_stats_transaction(argv[1], request);
917 }
918
919 static void
920 do_add_flow(int argc OVS_UNUSED, char *argv[])
921 {
922     struct vconn *vconn;
923     struct ofpbuf *buffer;
924     struct ofp_flow_mod *ofm;
925     uint16_t priority, idle_timeout, hard_timeout;
926     uint64_t cookie;
927     struct ofp_match match;
928
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,
934                 &cookie);
935     ofm = buffer->data;
936     ofm->match = match;
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);
943
944     open_vconn(argv[1], &vconn);
945     send_openflow_buffer(vconn, buffer);
946     vconn_close(vconn);
947 }
948
949 static void
950 do_add_flows(int argc OVS_UNUSED, char *argv[])
951 {
952     struct vconn *vconn;
953     FILE *file;
954     char line[1024];
955
956     file = fopen(argv[2], "r");
957     if (file == NULL) {
958         ovs_fatal(errno, "%s: open", argv[2]);
959     }
960
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;
966         uint64_t cookie;
967         struct ofp_match match;
968
969         char *comment;
970
971         /* Delete comments. */
972         comment = strchr(line, '#');
973         if (comment) {
974             *comment = '\0';
975         }
976
977         /* Drop empty lines. */
978         if (line[strspn(line, " \t\n")] == '\0') {
979             continue;
980         }
981
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()
984          * call. */
985         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
986         str_to_flow(line, &match, buffer,
987                     NULL, NULL, &priority, &idle_timeout, &hard_timeout,
988                     &cookie);
989         ofm = buffer->data;
990         ofm->match = match;
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);
997
998         send_openflow_buffer(vconn, buffer);
999     }
1000     vconn_close(vconn);
1001     fclose(file);
1002 }
1003
1004 static void
1005 do_mod_flows(int argc OVS_UNUSED, char *argv[])
1006 {
1007     uint16_t priority, idle_timeout, hard_timeout;
1008     uint64_t cookie;
1009     struct vconn *vconn;
1010     struct ofpbuf *buffer;
1011     struct ofp_flow_mod *ofm;
1012     struct ofp_match match;
1013
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,
1019                 &cookie);
1020     ofm = buffer->data;
1021     ofm->match = match;
1022     if (strict) {
1023         ofm->command = htons(OFPFC_MODIFY_STRICT);
1024     } else {
1025         ofm->command = htons(OFPFC_MODIFY);
1026     }
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);
1032
1033     open_vconn(argv[1], &vconn);
1034     send_openflow_buffer(vconn, buffer);
1035     vconn_close(vconn);
1036 }
1037
1038 static void do_del_flows(int argc, char *argv[])
1039 {
1040     struct vconn *vconn;
1041     uint16_t priority;
1042     uint16_t out_port;
1043     struct ofpbuf *buffer;
1044     struct ofp_flow_mod *ofm;
1045
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);
1050     if (strict) {
1051         ofm->command = htons(OFPFC_DELETE_STRICT);
1052     } else {
1053         ofm->command = htons(OFPFC_DELETE);
1054     }
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);
1060
1061     open_vconn(argv[1], &vconn);
1062     send_openflow_buffer(vconn, buffer);
1063     vconn_close(vconn);
1064 }
1065
1066 static void
1067 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1068 {
1069     struct nxt_tun_id_cookie *tun_id_cookie;
1070     struct ofpbuf *buffer;
1071     struct vconn *vconn;
1072
1073     tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1074
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");
1078
1079     open_vconn(argv[1], &vconn);
1080     send_openflow_buffer(vconn, buffer);
1081     vconn_close(vconn);
1082 }
1083
1084 static void
1085 monitor_vconn(struct vconn *vconn)
1086 {
1087     for (;;) {
1088         struct ofpbuf *b;
1089         run(vconn_recv_block(vconn, &b), "vconn_recv");
1090         ofp_print(stderr, b->data, b->size, 2);
1091         ofpbuf_delete(b);
1092     }
1093 }
1094
1095 static void
1096 do_monitor(int argc, char *argv[])
1097 {
1098     struct vconn *vconn;
1099
1100     open_vconn(argv[1], &vconn);
1101     if (argc > 2) {
1102         int miss_send_len = atoi(argv[2]);
1103         struct ofp_switch_config *osc;
1104         struct ofpbuf *buf;
1105
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);
1109     }
1110     monitor_vconn(vconn);
1111 }
1112
1113 static void
1114 do_snoop(int argc OVS_UNUSED, char *argv[])
1115 {
1116     struct vconn *vconn;
1117
1118     open_vconn__(argv[1], "snoop", &vconn);
1119     monitor_vconn(vconn);
1120 }
1121
1122 static void
1123 do_dump_ports(int argc, char *argv[])
1124 {
1125     struct ofp_port_stats_request *req;
1126     struct ofpbuf *request;
1127     uint16_t port;
1128
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);
1133 }
1134
1135 static void
1136 do_probe(int argc OVS_UNUSED, char *argv[])
1137 {
1138     struct ofpbuf *request;
1139     struct vconn *vconn;
1140     struct ofpbuf *reply;
1141
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");
1147     }
1148     ofpbuf_delete(reply);
1149     vconn_close(vconn);
1150 }
1151
1152 static void
1153 do_mod_port(int argc OVS_UNUSED, char *argv[])
1154 {
1155     struct ofpbuf *request, *reply;
1156     struct ofp_switch_features *osf;
1157     struct ofp_port_mod *opm;
1158     struct vconn *vconn;
1159     char *endptr;
1160     int n_ports;
1161     int port_idx;
1162     int port_no;
1163     
1164
1165     /* Check if the argument is a port index.  Otherwise, treat it as
1166      * the port name. */
1167     port_no = strtol(argv[2], &endptr, 10);
1168     if (port_no == 0 && endptr == argv[2]) {
1169         port_no = -1;
1170     }
1171
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]);
1177
1178     osf = reply->data;
1179     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1180
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)) {
1185                 break;
1186             }
1187         } else {
1188             /* Check argument as an interface name */
1189             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1190                         sizeof osf->ports[0].name)) {
1191                 break;
1192             }
1193
1194         }
1195     }
1196     if (port_idx == n_ports) {
1197         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1198     }
1199
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);
1206
1207     printf("modifying port: %s\n", osf->ports[port_idx].name);
1208
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);
1222     } else {
1223         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1224     }
1225
1226     send_openflow_buffer(vconn, request);
1227
1228     ofpbuf_delete(reply);
1229     vconn_close(vconn);
1230 }
1231
1232 static void
1233 do_ping(int argc, char *argv[])
1234 {
1235     size_t max_payload = 65535 - sizeof(struct ofp_header);
1236     unsigned int payload;
1237     struct vconn *vconn;
1238     int i;
1239
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);
1243     }
1244
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;
1250
1251         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1252                                OFPT_ECHO_REQUEST, &request);
1253         random_bytes(rq_hdr + 1, payload);
1254
1255         gettimeofday(&start, NULL);
1256         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1257         gettimeofday(&end, NULL);
1258
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);
1266             printf("Reply:\n");
1267             ofp_print(stdout, reply, reply->size, 2);
1268         }
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);
1275     }
1276     vconn_close(vconn);
1277 }
1278
1279 static void
1280 do_benchmark(int argc OVS_UNUSED, char *argv[])
1281 {
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;
1286     double duration;
1287     int count;
1288     int i;
1289
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);
1293     }
1294     message_size = sizeof(struct ofp_header) + payload_size;
1295
1296     count = atoi(argv[3]);
1297
1298     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1299            count, message_size, count * message_size);
1300
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;
1306
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);
1311     }
1312     gettimeofday(&end, NULL);
1313     vconn_close(vconn);
1314
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));
1320 }
1321
1322 static void
1323 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1324 {
1325     usage();
1326 }
1327
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 },
1349 };