vconn: Move OpenFlow utility functions into new file ofp-util.c.
[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     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
875 static void
876 do_dump_flows(int argc, char *argv[])
877 {
878     struct ofp_flow_stats_request *req;
879     uint16_t out_port;
880     struct ofpbuf *request;
881
882     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
883     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
884                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
885     memset(&req->pad, 0, sizeof req->pad);
886     req->out_port = htons(out_port);
887
888     dump_stats_transaction(argv[1], request);
889 }
890
891 static void
892 do_dump_aggregate(int argc, char *argv[])
893 {
894     struct ofp_aggregate_stats_request *req;
895     struct ofpbuf *request;
896     uint16_t out_port;
897
898     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
899     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
900                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
901     memset(&req->pad, 0, sizeof req->pad);
902     req->out_port = htons(out_port);
903
904     dump_stats_transaction(argv[1], request);
905 }
906
907 static void
908 do_add_flow(int argc OVS_UNUSED, char *argv[])
909 {
910     struct vconn *vconn;
911     struct ofpbuf *buffer;
912     struct ofp_flow_mod *ofm;
913     uint16_t priority, idle_timeout, hard_timeout;
914     uint64_t cookie;
915     struct ofp_match match;
916
917     /* Parse and send.  str_to_flow() will expand and reallocate the data in
918      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
919     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
920     str_to_flow(argv[2], &match, buffer,
921                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
922                 &cookie);
923     ofm = buffer->data;
924     ofm->match = match;
925     ofm->command = htons(OFPFC_ADD);
926     ofm->cookie = htonll(cookie);
927     ofm->idle_timeout = htons(idle_timeout);
928     ofm->hard_timeout = htons(hard_timeout);
929     ofm->buffer_id = htonl(UINT32_MAX);
930     ofm->priority = htons(priority);
931
932     open_vconn(argv[1], &vconn);
933     send_openflow_buffer(vconn, buffer);
934     vconn_close(vconn);
935 }
936
937 static void
938 do_add_flows(int argc OVS_UNUSED, char *argv[])
939 {
940     struct vconn *vconn;
941     FILE *file;
942     char line[1024];
943
944     file = fopen(argv[2], "r");
945     if (file == NULL) {
946         ovs_fatal(errno, "%s: open", argv[2]);
947     }
948
949     open_vconn(argv[1], &vconn);
950     while (fgets(line, sizeof line, file)) {
951         struct ofpbuf *buffer;
952         struct ofp_flow_mod *ofm;
953         uint16_t priority, idle_timeout, hard_timeout;
954         uint64_t cookie;
955         struct ofp_match match;
956
957         char *comment;
958
959         /* Delete comments. */
960         comment = strchr(line, '#');
961         if (comment) {
962             *comment = '\0';
963         }
964
965         /* Drop empty lines. */
966         if (line[strspn(line, " \t\n")] == '\0') {
967             continue;
968         }
969
970         /* Parse and send.  str_to_flow() will expand and reallocate the data
971          * in 'buffer', so we can't keep pointers to across the str_to_flow()
972          * call. */
973         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
974         str_to_flow(line, &match, buffer,
975                     NULL, NULL, &priority, &idle_timeout, &hard_timeout,
976                     &cookie);
977         ofm = buffer->data;
978         ofm->match = match;
979         ofm->command = htons(OFPFC_ADD);
980         ofm->cookie = htonll(cookie);
981         ofm->idle_timeout = htons(idle_timeout);
982         ofm->hard_timeout = htons(hard_timeout);
983         ofm->buffer_id = htonl(UINT32_MAX);
984         ofm->priority = htons(priority);
985
986         send_openflow_buffer(vconn, buffer);
987     }
988     vconn_close(vconn);
989     fclose(file);
990 }
991
992 static void
993 do_mod_flows(int argc OVS_UNUSED, char *argv[])
994 {
995     uint16_t priority, idle_timeout, hard_timeout;
996     uint64_t cookie;
997     struct vconn *vconn;
998     struct ofpbuf *buffer;
999     struct ofp_flow_mod *ofm;
1000     struct ofp_match match;
1001
1002     /* Parse and send.  str_to_flow() will expand and reallocate the data in
1003      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1004     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1005     str_to_flow(argv[2], &match, buffer,
1006                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1007                 &cookie);
1008     ofm = buffer->data;
1009     ofm->match = match;
1010     if (strict) {
1011         ofm->command = htons(OFPFC_MODIFY_STRICT);
1012     } else {
1013         ofm->command = htons(OFPFC_MODIFY);
1014     }
1015     ofm->idle_timeout = htons(idle_timeout);
1016     ofm->hard_timeout = htons(hard_timeout);
1017     ofm->cookie = htonll(cookie);
1018     ofm->buffer_id = htonl(UINT32_MAX);
1019     ofm->priority = htons(priority);
1020
1021     open_vconn(argv[1], &vconn);
1022     send_openflow_buffer(vconn, buffer);
1023     vconn_close(vconn);
1024 }
1025
1026 static void do_del_flows(int argc, char *argv[])
1027 {
1028     struct vconn *vconn;
1029     uint16_t priority;
1030     uint16_t out_port;
1031     struct ofpbuf *buffer;
1032     struct ofp_flow_mod *ofm;
1033
1034     /* Parse and send. */
1035     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1036     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
1037                 &out_port, &priority, NULL, NULL, NULL);
1038     if (strict) {
1039         ofm->command = htons(OFPFC_DELETE_STRICT);
1040     } else {
1041         ofm->command = htons(OFPFC_DELETE);
1042     }
1043     ofm->idle_timeout = htons(0);
1044     ofm->hard_timeout = htons(0);
1045     ofm->buffer_id = htonl(UINT32_MAX);
1046     ofm->out_port = htons(out_port);
1047     ofm->priority = htons(priority);
1048
1049     open_vconn(argv[1], &vconn);
1050     send_openflow_buffer(vconn, buffer);
1051     vconn_close(vconn);
1052 }
1053
1054 static void
1055 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1056 {
1057     struct nxt_tun_id_cookie *tun_id_cookie;
1058     struct ofpbuf *buffer;
1059     struct vconn *vconn;
1060
1061     tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1062
1063     tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1064     tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1065     tun_id_cookie->set = !strcmp(argv[2], "true");
1066
1067     open_vconn(argv[1], &vconn);
1068     send_openflow_buffer(vconn, buffer);
1069     vconn_close(vconn);
1070 }
1071
1072 static void
1073 monitor_vconn(struct vconn *vconn)
1074 {
1075     for (;;) {
1076         struct ofpbuf *b;
1077         run(vconn_recv_block(vconn, &b), "vconn_recv");
1078         ofp_print(stderr, b->data, b->size, 2);
1079         ofpbuf_delete(b);
1080     }
1081 }
1082
1083 static void
1084 do_monitor(int argc, char *argv[])
1085 {
1086     struct vconn *vconn;
1087
1088     open_vconn(argv[1], &vconn);
1089     if (argc > 2) {
1090         int miss_send_len = atoi(argv[2]);
1091         struct ofp_switch_config *osc;
1092         struct ofpbuf *buf;
1093
1094         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1095         osc->miss_send_len = htons(miss_send_len);
1096         send_openflow_buffer(vconn, buf);
1097     }
1098     monitor_vconn(vconn);
1099 }
1100
1101 static void
1102 do_snoop(int argc OVS_UNUSED, char *argv[])
1103 {
1104     struct vconn *vconn;
1105
1106     open_vconn__(argv[1], "snoop", &vconn);
1107     monitor_vconn(vconn);
1108 }
1109
1110 static void
1111 do_dump_ports(int argc, char *argv[])
1112 {
1113     struct ofp_port_stats_request *req;
1114     struct ofpbuf *request;
1115     uint16_t port;
1116
1117     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1118     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1119     req->port_no = htons(port);
1120     dump_stats_transaction(argv[1], request);
1121 }
1122
1123 static void
1124 do_probe(int argc OVS_UNUSED, char *argv[])
1125 {
1126     struct ofpbuf *request;
1127     struct vconn *vconn;
1128     struct ofpbuf *reply;
1129
1130     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1131     open_vconn(argv[1], &vconn);
1132     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1133     if (reply->size != sizeof(struct ofp_header)) {
1134         ovs_fatal(0, "reply does not match request");
1135     }
1136     ofpbuf_delete(reply);
1137     vconn_close(vconn);
1138 }
1139
1140 static void
1141 do_mod_port(int argc OVS_UNUSED, char *argv[])
1142 {
1143     struct ofpbuf *request, *reply;
1144     struct ofp_switch_features *osf;
1145     struct ofp_port_mod *opm;
1146     struct vconn *vconn;
1147     char *endptr;
1148     int n_ports;
1149     int port_idx;
1150     int port_no;
1151     
1152
1153     /* Check if the argument is a port index.  Otherwise, treat it as
1154      * the port name. */
1155     port_no = strtol(argv[2], &endptr, 10);
1156     if (port_no == 0 && endptr == argv[2]) {
1157         port_no = -1;
1158     }
1159
1160     /* Send a "Features Request" to get the information we need in order 
1161      * to modify the port. */
1162     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1163     open_vconn(argv[1], &vconn);
1164     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1165
1166     osf = reply->data;
1167     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1168
1169     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1170         if (port_no != -1) {
1171             /* Check argument as a port index */
1172             if (osf->ports[port_idx].port_no == htons(port_no)) {
1173                 break;
1174             }
1175         } else {
1176             /* Check argument as an interface name */
1177             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1178                         sizeof osf->ports[0].name)) {
1179                 break;
1180             }
1181
1182         }
1183     }
1184     if (port_idx == n_ports) {
1185         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1186     }
1187
1188     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1189     opm->port_no = osf->ports[port_idx].port_no;
1190     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1191     opm->config = htonl(0);
1192     opm->mask = htonl(0);
1193     opm->advertise = htonl(0);
1194
1195     printf("modifying port: %s\n", osf->ports[port_idx].name);
1196
1197     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1198         opm->mask |= htonl(OFPPC_PORT_DOWN);
1199     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1200                 sizeof MOD_PORT_CMD_DOWN)) {
1201         opm->mask |= htonl(OFPPC_PORT_DOWN);
1202         opm->config |= htonl(OFPPC_PORT_DOWN);
1203     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1204                 sizeof MOD_PORT_CMD_FLOOD)) {
1205         opm->mask |= htonl(OFPPC_NO_FLOOD);
1206     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1207                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1208         opm->mask |= htonl(OFPPC_NO_FLOOD);
1209         opm->config |= htonl(OFPPC_NO_FLOOD);
1210     } else {
1211         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1212     }
1213
1214     send_openflow_buffer(vconn, request);
1215
1216     ofpbuf_delete(reply);
1217     vconn_close(vconn);
1218 }
1219
1220 static void
1221 do_ping(int argc, char *argv[])
1222 {
1223     size_t max_payload = 65535 - sizeof(struct ofp_header);
1224     unsigned int payload;
1225     struct vconn *vconn;
1226     int i;
1227
1228     payload = argc > 2 ? atoi(argv[2]) : 64;
1229     if (payload > max_payload) {
1230         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1231     }
1232
1233     open_vconn(argv[1], &vconn);
1234     for (i = 0; i < 10; i++) {
1235         struct timeval start, end;
1236         struct ofpbuf *request, *reply;
1237         struct ofp_header *rq_hdr, *rpy_hdr;
1238
1239         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1240                                OFPT_ECHO_REQUEST, &request);
1241         random_bytes(rq_hdr + 1, payload);
1242
1243         gettimeofday(&start, NULL);
1244         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1245         gettimeofday(&end, NULL);
1246
1247         rpy_hdr = reply->data;
1248         if (reply->size != request->size
1249             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1250             || rpy_hdr->xid != rq_hdr->xid
1251             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1252             printf("Reply does not match request.  Request:\n");
1253             ofp_print(stdout, request, request->size, 2);
1254             printf("Reply:\n");
1255             ofp_print(stdout, reply, reply->size, 2);
1256         }
1257         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1258                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1259                    (1000*(double)(end.tv_sec - start.tv_sec))
1260                    + (.001*(end.tv_usec - start.tv_usec)));
1261         ofpbuf_delete(request);
1262         ofpbuf_delete(reply);
1263     }
1264     vconn_close(vconn);
1265 }
1266
1267 static void
1268 do_benchmark(int argc OVS_UNUSED, char *argv[])
1269 {
1270     size_t max_payload = 65535 - sizeof(struct ofp_header);
1271     struct timeval start, end;
1272     unsigned int payload_size, message_size;
1273     struct vconn *vconn;
1274     double duration;
1275     int count;
1276     int i;
1277
1278     payload_size = atoi(argv[2]);
1279     if (payload_size > max_payload) {
1280         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1281     }
1282     message_size = sizeof(struct ofp_header) + payload_size;
1283
1284     count = atoi(argv[3]);
1285
1286     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1287            count, message_size, count * message_size);
1288
1289     open_vconn(argv[1], &vconn);
1290     gettimeofday(&start, NULL);
1291     for (i = 0; i < count; i++) {
1292         struct ofpbuf *request, *reply;
1293         struct ofp_header *rq_hdr;
1294
1295         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1296         memset(rq_hdr + 1, 0, payload_size);
1297         run(vconn_transact(vconn, request, &reply), "transact");
1298         ofpbuf_delete(reply);
1299     }
1300     gettimeofday(&end, NULL);
1301     vconn_close(vconn);
1302
1303     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1304                 + (.001*(end.tv_usec - start.tv_usec)));
1305     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1306            duration, count / (duration / 1000.0),
1307            count * message_size / (duration / 1000.0));
1308 }
1309
1310 static void
1311 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1312 {
1313     usage();
1314 }
1315
1316 static const struct command all_commands[] = {
1317     { "show", 1, 1, do_show },
1318     { "status", 1, 2, do_status },
1319     { "monitor", 1, 2, do_monitor },
1320     { "snoop", 1, 1, do_snoop },
1321     { "dump-desc", 1, 1, do_dump_desc },
1322     { "dump-tables", 1, 1, do_dump_tables },
1323     { "dump-flows", 1, 2, do_dump_flows },
1324     { "dump-aggregate", 1, 2, do_dump_aggregate },
1325     { "add-flow", 2, 2, do_add_flow },
1326     { "add-flows", 2, 2, do_add_flows },
1327     { "mod-flows", 2, 2, do_mod_flows },
1328     { "del-flows", 1, 2, do_del_flows },
1329     { "tun-cookie", 2, 2, do_tun_cookie },
1330     { "dump-ports", 1, 2, do_dump_ports },
1331     { "mod-port", 3, 3, do_mod_port },
1332     { "probe", 1, 1, do_probe },
1333     { "ping", 1, 2, do_ping },
1334     { "benchmark", 3, 3, do_benchmark },
1335     { "help", 0, INT_MAX, do_help },
1336     { NULL, 0, 0, NULL },
1337 };