Always #include <sys/socket.h> before <net/if.h>.
[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 "ofpbuf.h"
43 #include "openflow/nicira-ext.h"
44 #include "openflow/openflow.h"
45 #include "packets.h"
46 #include "random.h"
47 #include "socket-util.h"
48 #include "stream-ssl.h"
49 #include "timeval.h"
50 #include "util.h"
51 #include "vconn.h"
52 #include "xtoxll.h"
53
54 #include "vlog.h"
55 #define THIS_MODULE VLM_ofctl
56
57 #define DEFAULT_IDLE_TIMEOUT 60
58
59 #define MOD_PORT_CMD_UP      "up"
60 #define MOD_PORT_CMD_DOWN    "down"
61 #define MOD_PORT_CMD_FLOOD   "flood"
62 #define MOD_PORT_CMD_NOFLOOD "noflood"
63
64 /* Use strict matching for flow mod commands? */
65 static bool strict;
66
67 static const struct command all_commands[];
68
69 static void usage(void) NO_RETURN;
70 static void parse_options(int argc, char *argv[]);
71
72 int
73 main(int argc, char *argv[])
74 {
75     set_program_name(argv[0]);
76     time_init();
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     char *save_ptr = NULL;
772     char *name;
773     uint32_t wildcards;
774
775     if (table_idx) {
776         *table_idx = 0xff;
777     }
778     if (out_port) {
779         *out_port = OFPP_NONE;
780     }
781     if (priority) {
782         *priority = OFP_DEFAULT_PRIORITY;
783     }
784     if (idle_timeout) {
785         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
786     }
787     if (hard_timeout) {
788         *hard_timeout = OFP_FLOW_PERMANENT;
789     }
790     if (cookie) {
791         *cookie = 0;
792     }
793     if (actions) {
794         char *act_str = strstr(string, "action");
795         if (!act_str) {
796             ovs_fatal(0, "must specify an action");
797         }
798         *act_str = '\0';
799
800         act_str = strchr(act_str + 1, '=');
801         if (!act_str) {
802             ovs_fatal(0, "must specify an action");
803         }
804
805         act_str++;
806
807         str_to_action(act_str, actions);
808     }
809     memset(match, 0, sizeof *match);
810     wildcards = OFPFW_ALL;
811     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
812          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
813         const struct protocol *p;
814
815         if (parse_protocol(name, &p)) {
816             wildcards &= ~OFPFW_DL_TYPE;
817             match->dl_type = htons(p->dl_type);
818             if (p->nw_proto) {
819                 wildcards &= ~OFPFW_NW_PROTO;
820                 match->nw_proto = p->nw_proto;
821             }
822         } else {
823             const struct field *f;
824             char *value;
825
826             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
827             if (!value) {
828                 ovs_fatal(0, "field %s missing value", name);
829             }
830         
831             if (table_idx && !strcmp(name, "table")) {
832                 *table_idx = atoi(value);
833             } else if (out_port && !strcmp(name, "out_port")) {
834                 *out_port = atoi(value);
835             } else if (priority && !strcmp(name, "priority")) {
836                 *priority = atoi(value);
837             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
838                 *idle_timeout = atoi(value);
839             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
840                 *hard_timeout = atoi(value);
841             } else if (cookie && !strcmp(name, "cookie")) {
842                 *cookie = str_to_u64(value);
843             } else if (!strcmp(name, "tun_id_wild")) {
844                 wildcards |= NXFW_TUN_ID;
845             } else if (parse_field(name, &f)) {
846                 void *data = (char *) match + f->offset;
847                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
848                     wildcards |= f->wildcard;
849                 } else {
850                     wildcards &= ~f->wildcard;
851                     if (f->wildcard == OFPFW_IN_PORT
852                         && parse_port_name(value, (uint16_t *) data)) {
853                         /* Nothing to do. */
854                     } else if (f->type == F_U8) {
855                         *(uint8_t *) data = str_to_u32(value);
856                     } else if (f->type == F_U16) {
857                         *(uint16_t *) data = htons(str_to_u32(value));
858                     } else if (f->type == F_MAC) {
859                         str_to_mac(value, data);
860                     } else if (f->type == F_IP) {
861                         wildcards |= str_to_ip(value, data) << f->shift;
862                     } else {
863                         NOT_REACHED();
864                     }
865                 }
866             } else {
867                 ovs_fatal(0, "unknown keyword %s", name);
868             }
869         }
870     }
871     match->wildcards = htonl(wildcards);
872 }
873
874 static void
875 do_dump_flows(int argc, char *argv[])
876 {
877     struct ofp_flow_stats_request *req;
878     uint16_t out_port;
879     struct ofpbuf *request;
880
881     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
882     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
883                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
884     memset(&req->pad, 0, sizeof req->pad);
885     req->out_port = htons(out_port);
886
887     dump_stats_transaction(argv[1], request);
888 }
889
890 static void
891 do_dump_aggregate(int argc, char *argv[])
892 {
893     struct ofp_aggregate_stats_request *req;
894     struct ofpbuf *request;
895     uint16_t out_port;
896
897     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
898     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
899                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
900     memset(&req->pad, 0, sizeof req->pad);
901     req->out_port = htons(out_port);
902
903     dump_stats_transaction(argv[1], request);
904 }
905
906 static void
907 do_add_flow(int argc OVS_UNUSED, char *argv[])
908 {
909     struct vconn *vconn;
910     struct ofpbuf *buffer;
911     struct ofp_flow_mod *ofm;
912     uint16_t priority, idle_timeout, hard_timeout;
913     uint64_t cookie;
914     struct ofp_match match;
915
916     /* Parse and send.  str_to_flow() will expand and reallocate the data in
917      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
918     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
919     str_to_flow(argv[2], &match, buffer,
920                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
921                 &cookie);
922     ofm = buffer->data;
923     ofm->match = match;
924     ofm->command = htons(OFPFC_ADD);
925     ofm->cookie = htonll(cookie);
926     ofm->idle_timeout = htons(idle_timeout);
927     ofm->hard_timeout = htons(hard_timeout);
928     ofm->buffer_id = htonl(UINT32_MAX);
929     ofm->priority = htons(priority);
930
931     open_vconn(argv[1], &vconn);
932     send_openflow_buffer(vconn, buffer);
933     vconn_close(vconn);
934 }
935
936 static void
937 do_add_flows(int argc OVS_UNUSED, char *argv[])
938 {
939     struct vconn *vconn;
940     FILE *file;
941     char line[1024];
942
943     file = fopen(argv[2], "r");
944     if (file == NULL) {
945         ovs_fatal(errno, "%s: open", argv[2]);
946     }
947
948     open_vconn(argv[1], &vconn);
949     while (fgets(line, sizeof line, file)) {
950         struct ofpbuf *buffer;
951         struct ofp_flow_mod *ofm;
952         uint16_t priority, idle_timeout, hard_timeout;
953         uint64_t cookie;
954         struct ofp_match match;
955
956         char *comment;
957
958         /* Delete comments. */
959         comment = strchr(line, '#');
960         if (comment) {
961             *comment = '\0';
962         }
963
964         /* Drop empty lines. */
965         if (line[strspn(line, " \t\n")] == '\0') {
966             continue;
967         }
968
969         /* Parse and send.  str_to_flow() will expand and reallocate the data
970          * in 'buffer', so we can't keep pointers to across the str_to_flow()
971          * call. */
972         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
973         str_to_flow(line, &match, buffer,
974                     NULL, NULL, &priority, &idle_timeout, &hard_timeout,
975                     &cookie);
976         ofm = buffer->data;
977         ofm->match = match;
978         ofm->command = htons(OFPFC_ADD);
979         ofm->cookie = htonll(cookie);
980         ofm->idle_timeout = htons(idle_timeout);
981         ofm->hard_timeout = htons(hard_timeout);
982         ofm->buffer_id = htonl(UINT32_MAX);
983         ofm->priority = htons(priority);
984
985         send_openflow_buffer(vconn, buffer);
986     }
987     vconn_close(vconn);
988     fclose(file);
989 }
990
991 static void
992 do_mod_flows(int argc OVS_UNUSED, char *argv[])
993 {
994     uint16_t priority, idle_timeout, hard_timeout;
995     uint64_t cookie;
996     struct vconn *vconn;
997     struct ofpbuf *buffer;
998     struct ofp_flow_mod *ofm;
999     struct ofp_match match;
1000
1001     /* Parse and send.  str_to_flow() will expand and reallocate the data in
1002      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1003     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1004     str_to_flow(argv[2], &match, buffer,
1005                 NULL, NULL, &priority, &idle_timeout, &hard_timeout,
1006                 &cookie);
1007     ofm = buffer->data;
1008     ofm->match = match;
1009     if (strict) {
1010         ofm->command = htons(OFPFC_MODIFY_STRICT);
1011     } else {
1012         ofm->command = htons(OFPFC_MODIFY);
1013     }
1014     ofm->idle_timeout = htons(idle_timeout);
1015     ofm->hard_timeout = htons(hard_timeout);
1016     ofm->cookie = htonll(cookie);
1017     ofm->buffer_id = htonl(UINT32_MAX);
1018     ofm->priority = htons(priority);
1019
1020     open_vconn(argv[1], &vconn);
1021     send_openflow_buffer(vconn, buffer);
1022     vconn_close(vconn);
1023 }
1024
1025 static void do_del_flows(int argc, char *argv[])
1026 {
1027     struct vconn *vconn;
1028     uint16_t priority;
1029     uint16_t out_port;
1030     struct ofpbuf *buffer;
1031     struct ofp_flow_mod *ofm;
1032
1033     /* Parse and send. */
1034     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1035     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
1036                 &out_port, &priority, NULL, NULL, NULL);
1037     if (strict) {
1038         ofm->command = htons(OFPFC_DELETE_STRICT);
1039     } else {
1040         ofm->command = htons(OFPFC_DELETE);
1041     }
1042     ofm->idle_timeout = htons(0);
1043     ofm->hard_timeout = htons(0);
1044     ofm->buffer_id = htonl(UINT32_MAX);
1045     ofm->out_port = htons(out_port);
1046     ofm->priority = htons(priority);
1047
1048     open_vconn(argv[1], &vconn);
1049     send_openflow_buffer(vconn, buffer);
1050     vconn_close(vconn);
1051 }
1052
1053 static void
1054 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1055 {
1056     struct nxt_tun_id_cookie *tun_id_cookie;
1057     struct ofpbuf *buffer;
1058     struct vconn *vconn;
1059
1060     tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1061
1062     tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1063     tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1064     tun_id_cookie->set = !strcmp(argv[2], "true");
1065
1066     open_vconn(argv[1], &vconn);
1067     send_openflow_buffer(vconn, buffer);
1068     vconn_close(vconn);
1069 }
1070
1071 static void
1072 monitor_vconn(struct vconn *vconn)
1073 {
1074     for (;;) {
1075         struct ofpbuf *b;
1076         run(vconn_recv_block(vconn, &b), "vconn_recv");
1077         ofp_print(stderr, b->data, b->size, 2);
1078         ofpbuf_delete(b);
1079     }
1080 }
1081
1082 static void
1083 do_monitor(int argc, char *argv[])
1084 {
1085     struct vconn *vconn;
1086
1087     open_vconn(argv[1], &vconn);
1088     if (argc > 2) {
1089         int miss_send_len = atoi(argv[2]);
1090         struct ofp_switch_config *osc;
1091         struct ofpbuf *buf;
1092
1093         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1094         osc->miss_send_len = htons(miss_send_len);
1095         send_openflow_buffer(vconn, buf);
1096     }
1097     monitor_vconn(vconn);
1098 }
1099
1100 static void
1101 do_snoop(int argc OVS_UNUSED, char *argv[])
1102 {
1103     struct vconn *vconn;
1104
1105     open_vconn__(argv[1], "snoop", &vconn);
1106     monitor_vconn(vconn);
1107 }
1108
1109 static void
1110 do_dump_ports(int argc, char *argv[])
1111 {
1112     struct ofp_port_stats_request *req;
1113     struct ofpbuf *request;
1114     uint16_t port;
1115
1116     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1117     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1118     req->port_no = htons(port);
1119     dump_stats_transaction(argv[1], request);
1120 }
1121
1122 static void
1123 do_probe(int argc OVS_UNUSED, char *argv[])
1124 {
1125     struct ofpbuf *request;
1126     struct vconn *vconn;
1127     struct ofpbuf *reply;
1128
1129     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1130     open_vconn(argv[1], &vconn);
1131     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1132     if (reply->size != sizeof(struct ofp_header)) {
1133         ovs_fatal(0, "reply does not match request");
1134     }
1135     ofpbuf_delete(reply);
1136     vconn_close(vconn);
1137 }
1138
1139 static void
1140 do_mod_port(int argc OVS_UNUSED, char *argv[])
1141 {
1142     struct ofpbuf *request, *reply;
1143     struct ofp_switch_features *osf;
1144     struct ofp_port_mod *opm;
1145     struct vconn *vconn;
1146     char *endptr;
1147     int n_ports;
1148     int port_idx;
1149     int port_no;
1150     
1151
1152     /* Check if the argument is a port index.  Otherwise, treat it as
1153      * the port name. */
1154     port_no = strtol(argv[2], &endptr, 10);
1155     if (port_no == 0 && endptr == argv[2]) {
1156         port_no = -1;
1157     }
1158
1159     /* Send a "Features Request" to get the information we need in order 
1160      * to modify the port. */
1161     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1162     open_vconn(argv[1], &vconn);
1163     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1164
1165     osf = reply->data;
1166     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1167
1168     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1169         if (port_no != -1) {
1170             /* Check argument as a port index */
1171             if (osf->ports[port_idx].port_no == htons(port_no)) {
1172                 break;
1173             }
1174         } else {
1175             /* Check argument as an interface name */
1176             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1177                         sizeof osf->ports[0].name)) {
1178                 break;
1179             }
1180
1181         }
1182     }
1183     if (port_idx == n_ports) {
1184         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1185     }
1186
1187     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1188     opm->port_no = osf->ports[port_idx].port_no;
1189     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1190     opm->config = htonl(0);
1191     opm->mask = htonl(0);
1192     opm->advertise = htonl(0);
1193
1194     printf("modifying port: %s\n", osf->ports[port_idx].name);
1195
1196     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1197         opm->mask |= htonl(OFPPC_PORT_DOWN);
1198     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1199                 sizeof MOD_PORT_CMD_DOWN)) {
1200         opm->mask |= htonl(OFPPC_PORT_DOWN);
1201         opm->config |= htonl(OFPPC_PORT_DOWN);
1202     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1203                 sizeof MOD_PORT_CMD_FLOOD)) {
1204         opm->mask |= htonl(OFPPC_NO_FLOOD);
1205     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1206                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1207         opm->mask |= htonl(OFPPC_NO_FLOOD);
1208         opm->config |= htonl(OFPPC_NO_FLOOD);
1209     } else {
1210         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1211     }
1212
1213     send_openflow_buffer(vconn, request);
1214
1215     ofpbuf_delete(reply);
1216     vconn_close(vconn);
1217 }
1218
1219 static void
1220 do_ping(int argc, char *argv[])
1221 {
1222     size_t max_payload = 65535 - sizeof(struct ofp_header);
1223     unsigned int payload;
1224     struct vconn *vconn;
1225     int i;
1226
1227     payload = argc > 2 ? atoi(argv[2]) : 64;
1228     if (payload > max_payload) {
1229         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1230     }
1231
1232     open_vconn(argv[1], &vconn);
1233     for (i = 0; i < 10; i++) {
1234         struct timeval start, end;
1235         struct ofpbuf *request, *reply;
1236         struct ofp_header *rq_hdr, *rpy_hdr;
1237
1238         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1239                                OFPT_ECHO_REQUEST, &request);
1240         random_bytes(rq_hdr + 1, payload);
1241
1242         gettimeofday(&start, NULL);
1243         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1244         gettimeofday(&end, NULL);
1245
1246         rpy_hdr = reply->data;
1247         if (reply->size != request->size
1248             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1249             || rpy_hdr->xid != rq_hdr->xid
1250             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1251             printf("Reply does not match request.  Request:\n");
1252             ofp_print(stdout, request, request->size, 2);
1253             printf("Reply:\n");
1254             ofp_print(stdout, reply, reply->size, 2);
1255         }
1256         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1257                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1258                    (1000*(double)(end.tv_sec - start.tv_sec))
1259                    + (.001*(end.tv_usec - start.tv_usec)));
1260         ofpbuf_delete(request);
1261         ofpbuf_delete(reply);
1262     }
1263     vconn_close(vconn);
1264 }
1265
1266 static void
1267 do_benchmark(int argc OVS_UNUSED, char *argv[])
1268 {
1269     size_t max_payload = 65535 - sizeof(struct ofp_header);
1270     struct timeval start, end;
1271     unsigned int payload_size, message_size;
1272     struct vconn *vconn;
1273     double duration;
1274     int count;
1275     int i;
1276
1277     payload_size = atoi(argv[2]);
1278     if (payload_size > max_payload) {
1279         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1280     }
1281     message_size = sizeof(struct ofp_header) + payload_size;
1282
1283     count = atoi(argv[3]);
1284
1285     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1286            count, message_size, count * message_size);
1287
1288     open_vconn(argv[1], &vconn);
1289     gettimeofday(&start, NULL);
1290     for (i = 0; i < count; i++) {
1291         struct ofpbuf *request, *reply;
1292         struct ofp_header *rq_hdr;
1293
1294         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1295         memset(rq_hdr + 1, 0, payload_size);
1296         run(vconn_transact(vconn, request, &reply), "transact");
1297         ofpbuf_delete(reply);
1298     }
1299     gettimeofday(&end, NULL);
1300     vconn_close(vconn);
1301
1302     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1303                 + (.001*(end.tv_usec - start.tv_usec)));
1304     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1305            duration, count / (duration / 1000.0),
1306            count * message_size / (duration / 1000.0));
1307 }
1308
1309 static void
1310 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1311 {
1312     usage();
1313 }
1314
1315 static const struct command all_commands[] = {
1316     { "show", 1, 1, do_show },
1317     { "status", 1, 2, do_status },
1318     { "monitor", 1, 2, do_monitor },
1319     { "snoop", 1, 1, do_snoop },
1320     { "dump-desc", 1, 1, do_dump_desc },
1321     { "dump-tables", 1, 1, do_dump_tables },
1322     { "dump-flows", 1, 2, do_dump_flows },
1323     { "dump-aggregate", 1, 2, do_dump_aggregate },
1324     { "add-flow", 2, 2, do_add_flow },
1325     { "add-flows", 2, 2, do_add_flows },
1326     { "mod-flows", 2, 2, do_mod_flows },
1327     { "del-flows", 1, 2, do_del_flows },
1328     { "tun-cookie", 2, 2, do_tun_cookie },
1329     { "dump-ports", 1, 2, do_dump_ports },
1330     { "mod-port", 3, 3, do_mod_port },
1331     { "probe", 1, 1, do_probe },
1332     { "ping", 1, 2, do_ping },
1333     { "benchmark", 3, 3, do_benchmark },
1334     { "help", 0, INT_MAX, do_help },
1335     { NULL, 0, 0, NULL },
1336 };