ovs-ofctl: Adding support for table ID.
[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 "dynamic-string.h"
37 #include "netdev.h"
38 #include "netlink.h"
39 #include "xflow-util.h"
40 #include "ofp-print.h"
41 #include "ofp-util.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 "vlog.h"
53 #include "xfif.h"
54 #include "xtoxll.h"
55
56 VLOG_DEFINE_THIS_MODULE(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     parse_options(argc, argv);
78     signal(SIGPIPE, SIG_IGN);
79     run_command(argc - optind, argv + optind, all_commands);
80     return 0;
81 }
82
83 static void
84 parse_options(int argc, char *argv[])
85 {
86     enum {
87         OPT_STRICT = UCHAR_MAX + 1,
88         VLOG_OPTION_ENUMS
89     };
90     static struct option long_options[] = {
91         {"timeout", required_argument, 0, 't'},
92         {"strict", no_argument, 0, OPT_STRICT},
93         {"help", no_argument, 0, 'h'},
94         {"version", no_argument, 0, 'V'},
95         VLOG_LONG_OPTIONS,
96         STREAM_SSL_LONG_OPTIONS
97         {0, 0, 0, 0},
98     };
99     char *short_options = long_options_to_short_options(long_options);
100
101     for (;;) {
102         unsigned long int timeout;
103         int c;
104
105         c = getopt_long(argc, argv, short_options, long_options, NULL);
106         if (c == -1) {
107             break;
108         }
109
110         switch (c) {
111         case 't':
112             timeout = strtoul(optarg, NULL, 10);
113             if (timeout <= 0) {
114                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
115                           optarg);
116             } else {
117                 time_alarm(timeout);
118             }
119             break;
120
121         case 'h':
122             usage();
123
124         case 'V':
125             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
126             exit(EXIT_SUCCESS);
127
128         case OPT_STRICT:
129             strict = true;
130             break;
131
132         VLOG_OPTION_HANDLERS
133         STREAM_SSL_OPTION_HANDLERS
134
135         case '?':
136             exit(EXIT_FAILURE);
137
138         default:
139             abort();
140         }
141     }
142     free(short_options);
143 }
144
145 static void
146 usage(void)
147 {
148     printf("%s: OpenFlow switch management utility\n"
149            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
150            "\nFor OpenFlow switches:\n"
151            "  show SWITCH                 show OpenFlow information\n"
152            "  status SWITCH [KEY]         report statistics (about KEY)\n"
153            "  dump-desc SWITCH            print switch description\n"
154            "  dump-tables SWITCH          print table stats\n"
155            "  mod-port SWITCH IFACE ACT   modify port behavior\n"
156            "  dump-ports SWITCH [PORT]    print port statistics\n"
157            "  dump-flows SWITCH           print all flow entries\n"
158            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
159            "  dump-aggregate SWITCH       print aggregate flow statistics\n"
160            "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
161            "  add-flow SWITCH FLOW        add flow described by FLOW\n"
162            "  add-flows SWITCH FILE       add flows from FILE\n"
163            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
164            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
165            "  monitor SWITCH [MISSLEN]    print packets received from SWITCH\n"
166            "\nFor OpenFlow switches and controllers:\n"
167            "  probe VCONN                 probe whether VCONN is up\n"
168            "  ping VCONN [N]              latency of N-byte echos\n"
169            "  benchmark VCONN N COUNT     bandwidth of COUNT N-byte echos\n"
170            "where each SWITCH is an active OpenFlow connection method.\n",
171            program_name, program_name);
172     vconn_usage(true, false, false);
173     vlog_usage();
174     printf("\nOther options:\n"
175            "  --strict                    use strict match for flow commands\n"
176            "  -t, --timeout=SECS          give up after SECS seconds\n"
177            "  -h, --help                  display this help message\n"
178            "  -V, --version               display version information\n");
179     exit(EXIT_SUCCESS);
180 }
181
182 static void run(int retval, const char *message, ...)
183     PRINTF_FORMAT(2, 3);
184
185 static void run(int retval, const char *message, ...)
186 {
187     if (retval) {
188         va_list args;
189
190         fprintf(stderr, "%s: ", program_name);
191         va_start(args, message);
192         vfprintf(stderr, message, args);
193         va_end(args);
194         if (retval == EOF) {
195             fputs(": unexpected end of file\n", stderr);
196         } else {
197             fprintf(stderr, ": %s\n", strerror(retval));
198         }
199
200         exit(EXIT_FAILURE);
201     }
202 }
203 \f
204 /* Generic commands. */
205
206 static void
207 open_vconn_socket(const char *name, struct vconn **vconnp)
208 {
209     char *vconn_name = xasprintf("unix:%s", name);
210     VLOG_INFO("connecting to %s", vconn_name);
211     run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
212         "connecting to %s", vconn_name);
213     free(vconn_name);
214 }
215
216 static void
217 open_vconn__(const char *name, const char *default_suffix,
218              struct vconn **vconnp)
219 {
220     struct xfif *xfif;
221     struct stat s;
222     char *bridge_path, *datapath_name, *datapath_type;
223
224     bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
225     xf_parse_name(name, &datapath_name, &datapath_type);
226
227     if (strstr(name, ":")) {
228         run(vconn_open_block(name, OFP_VERSION, vconnp),
229             "connecting to %s", name);
230     } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
231         open_vconn_socket(name, vconnp);
232     } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
233         open_vconn_socket(bridge_path, vconnp);
234     } else if (!xfif_open(datapath_name, datapath_type, &xfif)) {
235         char xfif_name[IF_NAMESIZE + 1];
236         char *socket_name;
237
238         run(xfif_port_get_name(xfif, XFLOWP_LOCAL, xfif_name, sizeof xfif_name),
239             "obtaining name of %s", xfif_name);
240         xfif_close(xfif);
241         if (strcmp(xfif_name, name)) {
242             VLOG_INFO("datapath %s is named %s", name, xfif_name);
243         }
244
245         socket_name = xasprintf("%s/%s.%s",
246                                 ovs_rundir, xfif_name, default_suffix);
247         if (stat(socket_name, &s)) {
248             ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
249                       name, socket_name);
250         } else if (!S_ISSOCK(s.st_mode)) {
251             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
252                       name, socket_name);
253         }
254
255         open_vconn_socket(socket_name, vconnp);
256         free(socket_name);
257     } else {
258         ovs_fatal(0, "%s is not a valid connection method", name);
259     }
260
261     free(datapath_name);
262     free(datapath_type);
263     free(bridge_path);
264 }
265
266 static void
267 open_vconn(const char *name, struct vconn **vconnp)
268 {
269     return open_vconn__(name, "mgmt", vconnp);
270 }
271
272 static void *
273 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
274 {
275     struct ofp_stats_request *rq;
276     rq = make_openflow((offsetof(struct ofp_stats_request, body)
277                         + body_len), OFPT_STATS_REQUEST, bufferp);
278     rq->type = htons(type);
279     rq->flags = htons(0);
280     return rq->body;
281 }
282
283 static void
284 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
285 {
286     update_openflow_length(buffer);
287     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
288 }
289
290 static void
291 dump_transaction(const char *vconn_name, struct ofpbuf *request)
292 {
293     struct vconn *vconn;
294     struct ofpbuf *reply;
295
296     update_openflow_length(request);
297     open_vconn(vconn_name, &vconn);
298     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
299     ofp_print(stdout, reply->data, reply->size, 1);
300     vconn_close(vconn);
301 }
302
303 static void
304 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
305 {
306     struct ofpbuf *request;
307     make_openflow(sizeof(struct ofp_header), request_type, &request);
308     dump_transaction(vconn_name, request);
309 }
310
311 static void
312 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
313 {
314     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
315     struct vconn *vconn;
316     bool done = false;
317
318     open_vconn(vconn_name, &vconn);
319     send_openflow_buffer(vconn, request);
320     while (!done) {
321         uint32_t recv_xid;
322         struct ofpbuf *reply;
323
324         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
325         recv_xid = ((struct ofp_header *) reply->data)->xid;
326         if (send_xid == recv_xid) {
327             struct ofp_stats_reply *osr;
328
329             ofp_print(stdout, reply->data, reply->size, 1);
330
331             osr = ofpbuf_at(reply, 0, sizeof *osr);
332             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
333         } else {
334             VLOG_DBG("received reply with xid %08"PRIx32" "
335                      "!= expected %08"PRIx32, recv_xid, send_xid);
336         }
337         ofpbuf_delete(reply);
338     }
339     vconn_close(vconn);
340 }
341
342 static void
343 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
344 {
345     struct ofpbuf *request;
346     alloc_stats_request(0, stats_type, &request);
347     dump_stats_transaction(vconn_name, request);
348 }
349
350 static void
351 do_show(int argc OVS_UNUSED, char *argv[])
352 {
353     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
354     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
355 }
356
357 static void
358 do_status(int argc, char *argv[])
359 {
360     struct nicira_header *request, *reply;
361     struct vconn *vconn;
362     struct ofpbuf *b;
363
364     request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
365     request->vendor = htonl(NX_VENDOR_ID);
366     request->subtype = htonl(NXT_STATUS_REQUEST);
367     if (argc > 2) {
368         ofpbuf_put(b, argv[2], strlen(argv[2]));
369         update_openflow_length(b);
370     }
371     open_vconn(argv[1], &vconn);
372     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
373     vconn_close(vconn);
374
375     if (b->size < sizeof *reply) {
376         ovs_fatal(0, "short reply (%zu bytes)", b->size);
377     }
378     reply = b->data;
379     if (reply->header.type != OFPT_VENDOR
380         || reply->vendor != ntohl(NX_VENDOR_ID)
381         || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
382         ofp_print(stderr, b->data, b->size, 2);
383         ovs_fatal(0, "bad reply");
384     }
385
386     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
387 }
388
389 static void
390 do_dump_desc(int argc OVS_UNUSED, char *argv[])
391 {
392     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
393 }
394
395 static void
396 do_dump_tables(int argc OVS_UNUSED, char *argv[])
397 {
398     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
399 }
400
401
402 static uint32_t
403 str_to_u32(const char *str) 
404 {
405     char *tail;
406     uint32_t value;
407
408     errno = 0;
409     value = strtoul(str, &tail, 0);
410     if (errno == EINVAL || errno == ERANGE || *tail) {
411         ovs_fatal(0, "invalid numeric format %s", str);
412     }
413     return value;
414 }
415
416 static uint64_t
417 str_to_u64(const char *str) 
418 {
419     char *tail;
420     uint64_t value;
421
422     errno = 0;
423     value = strtoull(str, &tail, 0);
424     if (errno == EINVAL || errno == ERANGE || *tail) {
425         ovs_fatal(0, "invalid numeric format %s", str);
426     }
427     return value;
428 }
429
430 static void
431 str_to_mac(const char *str, uint8_t mac[6]) 
432 {
433     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
434         != ETH_ADDR_SCAN_COUNT) {
435         ovs_fatal(0, "invalid mac address %s", str);
436     }
437 }
438
439 static uint32_t
440 str_to_ip(const char *str_, uint32_t *ip)
441 {
442     char *str = xstrdup(str_);
443     char *save_ptr = NULL;
444     const char *name, *netmask;
445     struct in_addr in_addr;
446     int n_wild, retval;
447
448     name = strtok_r(str, "/", &save_ptr);
449     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
450     if (retval) {
451         ovs_fatal(0, "%s: could not convert to IP address", str);
452     }
453     *ip = in_addr.s_addr;
454
455     netmask = strtok_r(NULL, "/", &save_ptr);
456     if (netmask) {
457         uint8_t o[4];
458         if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
459                    &o[0], &o[1], &o[2], &o[3]) == 4) {
460             uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
461             int i;
462
463             /* Find first 1-bit. */
464             for (i = 0; i < 32; i++) {
465                 if (nm & (1u << i)) {
466                     break;
467                 }
468             }
469             n_wild = i;
470
471             /* Verify that the rest of the bits are 1-bits. */
472             for (; i < 32; i++) {
473                 if (!(nm & (1u << i))) {
474                     ovs_fatal(0, "%s: %s is not a valid netmask",
475                               str, netmask);
476                 }
477             }
478         } else {
479             int prefix = atoi(netmask);
480             if (prefix <= 0 || prefix > 32) {
481                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
482                           str);
483             }
484             n_wild = 32 - prefix;
485         }
486     } else {
487         n_wild = 0;
488     }
489
490     free(str);
491     return n_wild;
492 }
493
494 static uint16_t
495 str_to_port_no(const char *vconn_name, const char *str)
496 {
497     struct ofpbuf *request, *reply;
498     struct ofp_switch_features *osf;
499     struct vconn *vconn;
500     int n_ports;
501     int port_idx;
502     unsigned int port_no;
503     
504
505     /* Check if the argument is a port index.  Otherwise, treat it as
506      * the port name. */
507     if (str_to_uint(str, 10, &port_no)) {
508         return port_no;
509     }
510
511     /* Send a "Features Request" to resolve the name into a number. */
512     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
513     open_vconn(vconn_name, &vconn);
514     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
515
516     osf = reply->data;
517     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
518
519     for (port_idx = 0; port_idx < n_ports; port_idx++) {
520         /* Check argument as an interface name */
521         if (!strncmp((char *)osf->ports[port_idx].name, str,
522                     sizeof osf->ports[0].name)) {
523             break;
524         }
525     }
526     if (port_idx == n_ports) {
527         ovs_fatal(0, "couldn't find monitored port: %s", str);
528     }
529
530     ofpbuf_delete(reply);
531     vconn_close(vconn);
532
533     return port_idx;
534 }
535
536 static void *
537 put_action(struct ofpbuf *b, size_t size, uint16_t type)
538 {
539     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
540     ah->type = htons(type);
541     ah->len = htons(size);
542     return ah;
543 }
544
545 static struct ofp_action_output *
546 put_output_action(struct ofpbuf *b, uint16_t port)
547 {
548     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
549     oao->port = htons(port);
550     return oao;
551 }
552
553 static void
554 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
555 {
556     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
557     str_to_mac(addr, oada->dl_addr);
558 }
559
560
561 static bool
562 parse_port_name(const char *name, uint16_t *port)
563 {
564     struct pair {
565         const char *name;
566         uint16_t value;
567     };
568     static const struct pair pairs[] = {
569 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
570         DEF_PAIR(IN_PORT),
571         DEF_PAIR(TABLE),
572         DEF_PAIR(NORMAL),
573         DEF_PAIR(FLOOD),
574         DEF_PAIR(ALL),
575         DEF_PAIR(CONTROLLER),
576         DEF_PAIR(LOCAL),
577         DEF_PAIR(NONE),
578 #undef DEF_PAIR
579     };
580     static const int n_pairs = ARRAY_SIZE(pairs);
581     size_t i;
582
583     for (i = 0; i < n_pairs; i++) {
584         if (!strcasecmp(name, pairs[i].name)) {
585             *port = pairs[i].value;
586             return true;
587         }
588     }
589     return false;
590 }
591
592 static void
593 str_to_action(char *str, struct ofpbuf *b)
594 {
595     char *act, *arg;
596     char *saveptr = NULL;
597     bool drop = false;
598     int n_actions;
599
600     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
601          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++) 
602     {
603         uint16_t port;
604
605         if (drop) {
606             ovs_fatal(0, "Drop actions must not be followed by other actions");
607         }
608
609         /* Arguments are separated by colons */
610         arg = strchr(act, ':');
611         if (arg) {
612             *arg = '\0';
613             arg++;
614         }
615
616         if (!strcasecmp(act, "mod_vlan_vid")) {
617             struct ofp_action_vlan_vid *va;
618             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
619             va->vlan_vid = htons(str_to_u32(arg));
620         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
621             struct ofp_action_vlan_pcp *va;
622             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
623             va->vlan_pcp = str_to_u32(arg);
624         } else if (!strcasecmp(act, "strip_vlan")) {
625             struct ofp_action_header *ah;
626             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
627             ah->type = htons(OFPAT_STRIP_VLAN);
628         } else if (!strcasecmp(act, "mod_dl_src")) {
629             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
630         } else if (!strcasecmp(act, "mod_dl_dst")) {
631             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
632         } else if (!strcasecmp(act, "mod_nw_src")) {
633             struct ofp_action_nw_addr *na;
634             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
635             str_to_ip(arg, &na->nw_addr);
636         } else if (!strcasecmp(act, "mod_nw_dst")) {
637             struct ofp_action_nw_addr *na;
638             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
639             str_to_ip(arg, &na->nw_addr);
640         } else if (!strcasecmp(act, "mod_tp_src")) {
641             struct ofp_action_tp_port *ta;
642             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
643             ta->tp_port = htons(str_to_u32(arg));
644         } else if (!strcasecmp(act, "mod_tp_dst")) {
645             struct ofp_action_tp_port *ta;
646             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
647             ta->tp_port = htons(str_to_u32(arg));
648         } else if (!strcasecmp(act, "mod_nw_tos")) {
649             struct ofp_action_nw_tos *nt;
650             nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
651             nt->nw_tos = str_to_u32(arg);
652         } else if (!strcasecmp(act, "resubmit")) {
653             struct nx_action_resubmit *nar;
654             nar = put_action(b, sizeof *nar, OFPAT_VENDOR);
655             nar->vendor = htonl(NX_VENDOR_ID);
656             nar->subtype = htons(NXAST_RESUBMIT);
657             nar->in_port = htons(str_to_u32(arg));
658         } else if (!strcasecmp(act, "set_tunnel")) {
659             struct nx_action_set_tunnel *nast;
660             nast = put_action(b, sizeof *nast, OFPAT_VENDOR);
661             nast->vendor = htonl(NX_VENDOR_ID);
662             nast->subtype = htons(NXAST_SET_TUNNEL);
663             nast->tun_id = htonl(str_to_u32(arg));
664         } else if (!strcasecmp(act, "output")) {
665             put_output_action(b, str_to_u32(arg));
666         } else if (!strcasecmp(act, "drop")) {
667             /* A drop action in OpenFlow occurs by just not setting 
668              * an action. */
669             drop = true;
670             if (n_actions) {
671                 ovs_fatal(0, "Drop actions must not be preceded by other "
672                           "actions");
673             }
674         } else if (!strcasecmp(act, "CONTROLLER")) {
675             struct ofp_action_output *oao;
676             oao = put_output_action(b, OFPP_CONTROLLER);
677
678             /* Unless a numeric argument is specified, we send the whole
679              * packet to the controller. */
680             if (arg && (strspn(arg, "0123456789") == strlen(arg))) {
681                oao->max_len = htons(str_to_u32(arg));
682             } else {
683                 oao->max_len = htons(UINT16_MAX);
684             }
685         } else if (parse_port_name(act, &port)) {
686             put_output_action(b, port);
687         } else if (strspn(act, "0123456789") == strlen(act)) {
688             put_output_action(b, str_to_u32(act));
689         } else {
690             ovs_fatal(0, "Unknown action: %s", act);
691         }
692     }
693 }
694
695 struct protocol {
696     const char *name;
697     uint16_t dl_type;
698     uint8_t nw_proto;
699 };
700
701 static bool
702 parse_protocol(const char *name, const struct protocol **p_out)
703 {
704     static const struct protocol protocols[] = {
705         { "ip", ETH_TYPE_IP, 0 },
706         { "arp", ETH_TYPE_ARP, 0 },
707         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
708         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
709         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
710     };
711     const struct protocol *p;
712
713     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
714         if (!strcmp(p->name, name)) {
715             *p_out = p;
716             return true;
717         }
718     }
719     *p_out = NULL;
720     return false;
721 }
722
723 struct field {
724     const char *name;
725     uint32_t wildcard;
726     enum { F_U8, F_U16, F_MAC, F_IP } type;
727     size_t offset, shift;
728 };
729
730 static bool
731 parse_field(const char *name, const struct field **f_out) 
732 {
733 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
734     static const struct field fields[] = {
735         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
736         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
737         { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
738         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
739         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
740         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
741         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
742           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
743         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
744           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
745         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
746         { "nw_tos", OFPFW_NW_TOS, F_U8, F_OFS(nw_tos), 0 },
747         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
748         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
749         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
750         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
751     };
752     const struct field *f;
753
754     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
755         if (!strcmp(f->name, name)) {
756             *f_out = f;
757             return true;
758         }
759     }
760     *f_out = NULL;
761     return false;
762 }
763
764 static void
765 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
766             uint8_t *table_idx, uint16_t *out_port, uint16_t *priority, 
767             uint16_t *idle_timeout, uint16_t *hard_timeout, 
768             uint64_t *cookie)
769 {
770     struct ofp_match normalized;
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                 if (*table_idx < 0 || *table_idx > 31) {
834                     ovs_fatal(0, "table %s is invalid, must be between 0 and 31", value);
835                 }
836             } else if (out_port && !strcmp(name, "out_port")) {
837                 *out_port = atoi(value);
838             } else if (priority && !strcmp(name, "priority")) {
839                 *priority = atoi(value);
840             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
841                 *idle_timeout = atoi(value);
842             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
843                 *hard_timeout = atoi(value);
844             } else if (cookie && !strcmp(name, "cookie")) {
845                 *cookie = str_to_u64(value);
846             } else if (!strcmp(name, "tun_id_wild")) {
847                 wildcards |= NXFW_TUN_ID;
848             } else if (parse_field(name, &f)) {
849                 void *data = (char *) match + f->offset;
850                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
851                     wildcards |= f->wildcard;
852                 } else {
853                     wildcards &= ~f->wildcard;
854                     if (f->wildcard == OFPFW_IN_PORT
855                         && parse_port_name(value, (uint16_t *) data)) {
856                         /* Nothing to do. */
857                     } else if (f->type == F_U8) {
858                         *(uint8_t *) data = str_to_u32(value);
859                     } else if (f->type == F_U16) {
860                         *(uint16_t *) data = htons(str_to_u32(value));
861                     } else if (f->type == F_MAC) {
862                         str_to_mac(value, data);
863                     } else if (f->type == F_IP) {
864                         wildcards |= str_to_ip(value, data) << f->shift;
865                     } else {
866                         NOT_REACHED();
867                     }
868                 }
869             } else {
870                 ovs_fatal(0, "unknown keyword %s", name);
871             }
872         }
873     }
874     match->wildcards = htonl(wildcards);
875
876     normalized = *match;
877     normalize_match(&normalized);
878     if (memcmp(match, &normalized, sizeof normalized)) {
879         char *old = ofp_match_to_literal_string(match);
880         char *new = ofp_match_to_literal_string(&normalized);
881         VLOG_WARN("The specified flow is not in normal form:");
882         VLOG_WARN(" as specified: %s", old);
883         VLOG_WARN("as normalized: %s", new);
884         free(old);
885         free(new);
886     }
887 }
888
889 static void
890 do_dump_flows(int argc, char *argv[])
891 {
892     struct ofp_flow_stats_request *req;
893     uint16_t out_port;
894     struct ofpbuf *request;
895
896     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
897     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
898                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
899     memset(&req->pad, 0, sizeof req->pad);
900     req->out_port = htons(out_port);
901
902     dump_stats_transaction(argv[1], request);
903 }
904
905 static void
906 do_dump_aggregate(int argc, char *argv[])
907 {
908     struct ofp_aggregate_stats_request *req;
909     struct ofpbuf *request;
910     uint16_t out_port;
911
912     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
913     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
914                 &req->table_id, &out_port, NULL, NULL, NULL, NULL);
915     memset(&req->pad, 0, sizeof req->pad);
916     req->out_port = htons(out_port);
917
918     dump_stats_transaction(argv[1], request);
919 }
920
921 static void
922 enable_flow_mod_table_id_ext(struct vconn *vconn, uint8_t enable)
923 {
924     struct nxt_flow_mod_table_id *flow_mod_table_id;
925     struct ofpbuf *buffer;
926
927     flow_mod_table_id = make_openflow(sizeof *flow_mod_table_id, OFPT_VENDOR, &buffer);
928
929     flow_mod_table_id->vendor = htonl(NX_VENDOR_ID);
930     flow_mod_table_id->subtype = htonl(NXT_FLOW_MOD_TABLE_ID);
931     flow_mod_table_id->set = enable;
932
933     send_openflow_buffer(vconn, buffer);
934 }
935
936 static void
937 do_add_flow(int argc OVS_UNUSED, char *argv[])
938 {
939     struct vconn *vconn;
940     struct ofpbuf *buffer;
941     struct ofp_flow_mod *ofm;
942     uint16_t priority, idle_timeout, hard_timeout;
943     uint64_t cookie;
944     struct ofp_match match;
945     uint8_t table_idx;
946
947     /* Parse and send.  str_to_flow() will expand and reallocate the data in
948      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
949     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
950     str_to_flow(argv[2], &match, buffer,
951                 &table_idx, NULL, &priority, &idle_timeout, &hard_timeout,
952                 &cookie);
953     ofm = buffer->data;
954     ofm->match = match;
955     ofm->command = htons(OFPFC_ADD);
956     ofm->cookie = htonll(cookie);
957     ofm->idle_timeout = htons(idle_timeout);
958     ofm->hard_timeout = htons(hard_timeout);
959     ofm->buffer_id = htonl(UINT32_MAX);
960     ofm->priority = htons(priority);
961
962     open_vconn(argv[1], &vconn);
963     if (table_idx != 0xff) {
964         enable_flow_mod_table_id_ext(vconn, 1);
965         ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
966     }
967     send_openflow_buffer(vconn, buffer);
968     vconn_close(vconn);
969 }
970
971 static void
972 do_add_flows(int argc OVS_UNUSED, char *argv[])
973 {
974     struct vconn *vconn;
975     FILE *file;
976     char line[1024];
977     uint8_t table_idx;
978     int table_id_enabled = 0;
979
980     file = fopen(argv[2], "r");
981     if (file == NULL) {
982         ovs_fatal(errno, "%s: open", argv[2]);
983     }
984
985     open_vconn(argv[1], &vconn);
986     while (fgets(line, sizeof line, file)) {
987         struct ofpbuf *buffer;
988         struct ofp_flow_mod *ofm;
989         uint16_t priority, idle_timeout, hard_timeout;
990         uint64_t cookie;
991         struct ofp_match match;
992
993         char *comment;
994
995         /* Delete comments. */
996         comment = strchr(line, '#');
997         if (comment) {
998             *comment = '\0';
999         }
1000
1001         /* Drop empty lines. */
1002         if (line[strspn(line, " \t\n")] == '\0') {
1003             continue;
1004         }
1005
1006         /* Parse and send.  str_to_flow() will expand and reallocate the data
1007          * in 'buffer', so we can't keep pointers to across the str_to_flow()
1008          * call. */
1009         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1010         str_to_flow(line, &match, buffer,
1011                     &table_idx, NULL, &priority, &idle_timeout, &hard_timeout,
1012                     &cookie);
1013         ofm = buffer->data;
1014         ofm->match = match;
1015         ofm->command = htons(OFPFC_ADD);
1016         ofm->cookie = htonll(cookie);
1017         ofm->idle_timeout = htons(idle_timeout);
1018         ofm->hard_timeout = htons(hard_timeout);
1019         ofm->buffer_id = htonl(UINT32_MAX);
1020         ofm->priority = htons(priority);
1021
1022         if (table_idx != 0xff) {
1023             if (!table_id_enabled) {
1024                 enable_flow_mod_table_id_ext(vconn, 1);
1025                 table_id_enabled = 1;
1026             }
1027             ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
1028         } else {
1029             if (table_id_enabled) {
1030                 enable_flow_mod_table_id_ext(vconn, 0);
1031                 table_id_enabled = 0;
1032             }
1033         }
1034         send_openflow_buffer(vconn, buffer);
1035     }
1036     vconn_close(vconn);
1037     fclose(file);
1038 }
1039
1040 static void
1041 do_mod_flows(int argc OVS_UNUSED, char *argv[])
1042 {
1043     uint16_t priority, idle_timeout, hard_timeout;
1044     uint64_t cookie;
1045     struct vconn *vconn;
1046     struct ofpbuf *buffer;
1047     struct ofp_flow_mod *ofm;
1048     struct ofp_match match;
1049     uint8_t table_idx;
1050
1051     /* Parse and send.  str_to_flow() will expand and reallocate the data in
1052      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
1053     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1054     str_to_flow(argv[2], &match, buffer,
1055                 &table_idx, NULL, &priority, &idle_timeout, &hard_timeout,
1056                 &cookie);
1057     ofm = buffer->data;
1058     ofm->match = match;
1059     if (strict) {
1060         ofm->command = htons(OFPFC_MODIFY_STRICT);
1061     } else {
1062         ofm->command = htons(OFPFC_MODIFY);
1063     }
1064     ofm->idle_timeout = htons(idle_timeout);
1065     ofm->hard_timeout = htons(hard_timeout);
1066     ofm->cookie = htonll(cookie);
1067     ofm->buffer_id = htonl(UINT32_MAX);
1068     ofm->priority = htons(priority);
1069
1070     open_vconn(argv[1], &vconn);
1071     if (table_idx != 0xff) {
1072         enable_flow_mod_table_id_ext(vconn, 1);
1073         ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
1074     }
1075     send_openflow_buffer(vconn, buffer);
1076     vconn_close(vconn);
1077 }
1078
1079 static void do_del_flows(int argc, char *argv[])
1080 {
1081     struct vconn *vconn;
1082     uint16_t priority;
1083     uint16_t out_port;
1084     struct ofpbuf *buffer;
1085     struct ofp_flow_mod *ofm;
1086     uint8_t table_idx;
1087
1088     /* Parse and send. */
1089     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
1090     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
1091                 &out_port, &priority, NULL, NULL, NULL);
1092     if (strict) {
1093         ofm->command = htons(OFPFC_DELETE_STRICT);
1094     } else {
1095         ofm->command = htons(OFPFC_DELETE);
1096     }
1097     ofm->idle_timeout = htons(0);
1098     ofm->hard_timeout = htons(0);
1099     ofm->buffer_id = htonl(UINT32_MAX);
1100     ofm->out_port = htons(out_port);
1101     ofm->priority = htons(priority);
1102
1103     open_vconn(argv[1], &vconn);
1104     if (table_idx != 0xff) {
1105         enable_flow_mod_table_id_ext(vconn, 1);
1106         ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
1107     }
1108     send_openflow_buffer(vconn, buffer);
1109     vconn_close(vconn);
1110 }
1111
1112 static void
1113 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
1114 {
1115     struct nxt_tun_id_cookie *tun_id_cookie;
1116     struct ofpbuf *buffer;
1117     struct vconn *vconn;
1118
1119     tun_id_cookie = make_openflow(sizeof *tun_id_cookie, OFPT_VENDOR, &buffer);
1120
1121     tun_id_cookie->vendor = htonl(NX_VENDOR_ID);
1122     tun_id_cookie->subtype = htonl(NXT_TUN_ID_FROM_COOKIE);
1123     tun_id_cookie->set = !strcmp(argv[2], "true");
1124
1125     open_vconn(argv[1], &vconn);
1126     send_openflow_buffer(vconn, buffer);
1127     vconn_close(vconn);
1128 }
1129
1130 static void
1131 monitor_vconn(struct vconn *vconn)
1132 {
1133     for (;;) {
1134         struct ofpbuf *b;
1135         run(vconn_recv_block(vconn, &b), "vconn_recv");
1136         ofp_print(stderr, b->data, b->size, 2);
1137         ofpbuf_delete(b);
1138     }
1139 }
1140
1141 static void
1142 do_monitor(int argc, char *argv[])
1143 {
1144     struct vconn *vconn;
1145
1146     open_vconn(argv[1], &vconn);
1147     if (argc > 2) {
1148         int miss_send_len = atoi(argv[2]);
1149         struct ofp_switch_config *osc;
1150         struct ofpbuf *buf;
1151
1152         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1153         osc->miss_send_len = htons(miss_send_len);
1154         send_openflow_buffer(vconn, buf);
1155     }
1156     monitor_vconn(vconn);
1157 }
1158
1159 static void
1160 do_snoop(int argc OVS_UNUSED, char *argv[])
1161 {
1162     struct vconn *vconn;
1163
1164     open_vconn__(argv[1], "snoop", &vconn);
1165     monitor_vconn(vconn);
1166 }
1167
1168 static void
1169 do_dump_ports(int argc, char *argv[])
1170 {
1171     struct ofp_port_stats_request *req;
1172     struct ofpbuf *request;
1173     uint16_t port;
1174
1175     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1176     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1177     req->port_no = htons(port);
1178     dump_stats_transaction(argv[1], request);
1179 }
1180
1181 static void
1182 do_probe(int argc OVS_UNUSED, char *argv[])
1183 {
1184     struct ofpbuf *request;
1185     struct vconn *vconn;
1186     struct ofpbuf *reply;
1187
1188     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1189     open_vconn(argv[1], &vconn);
1190     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1191     if (reply->size != sizeof(struct ofp_header)) {
1192         ovs_fatal(0, "reply does not match request");
1193     }
1194     ofpbuf_delete(reply);
1195     vconn_close(vconn);
1196 }
1197
1198 static void
1199 do_mod_port(int argc OVS_UNUSED, char *argv[])
1200 {
1201     struct ofpbuf *request, *reply;
1202     struct ofp_switch_features *osf;
1203     struct ofp_port_mod *opm;
1204     struct vconn *vconn;
1205     char *endptr;
1206     int n_ports;
1207     int port_idx;
1208     int port_no;
1209     
1210
1211     /* Check if the argument is a port index.  Otherwise, treat it as
1212      * the port name. */
1213     port_no = strtol(argv[2], &endptr, 10);
1214     if (port_no == 0 && endptr == argv[2]) {
1215         port_no = -1;
1216     }
1217
1218     /* Send a "Features Request" to get the information we need in order 
1219      * to modify the port. */
1220     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1221     open_vconn(argv[1], &vconn);
1222     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1223
1224     osf = reply->data;
1225     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1226
1227     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1228         if (port_no != -1) {
1229             /* Check argument as a port index */
1230             if (osf->ports[port_idx].port_no == htons(port_no)) {
1231                 break;
1232             }
1233         } else {
1234             /* Check argument as an interface name */
1235             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1236                         sizeof osf->ports[0].name)) {
1237                 break;
1238             }
1239
1240         }
1241     }
1242     if (port_idx == n_ports) {
1243         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1244     }
1245
1246     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1247     opm->port_no = osf->ports[port_idx].port_no;
1248     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1249     opm->config = htonl(0);
1250     opm->mask = htonl(0);
1251     opm->advertise = htonl(0);
1252
1253     printf("modifying port: %s\n", osf->ports[port_idx].name);
1254
1255     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1256         opm->mask |= htonl(OFPPC_PORT_DOWN);
1257     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1258                 sizeof MOD_PORT_CMD_DOWN)) {
1259         opm->mask |= htonl(OFPPC_PORT_DOWN);
1260         opm->config |= htonl(OFPPC_PORT_DOWN);
1261     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1262                 sizeof MOD_PORT_CMD_FLOOD)) {
1263         opm->mask |= htonl(OFPPC_NO_FLOOD);
1264     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1265                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1266         opm->mask |= htonl(OFPPC_NO_FLOOD);
1267         opm->config |= htonl(OFPPC_NO_FLOOD);
1268     } else {
1269         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1270     }
1271
1272     send_openflow_buffer(vconn, request);
1273
1274     ofpbuf_delete(reply);
1275     vconn_close(vconn);
1276 }
1277
1278 static void
1279 do_ping(int argc, char *argv[])
1280 {
1281     size_t max_payload = 65535 - sizeof(struct ofp_header);
1282     unsigned int payload;
1283     struct vconn *vconn;
1284     int i;
1285
1286     payload = argc > 2 ? atoi(argv[2]) : 64;
1287     if (payload > max_payload) {
1288         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1289     }
1290
1291     open_vconn(argv[1], &vconn);
1292     for (i = 0; i < 10; i++) {
1293         struct timeval start, end;
1294         struct ofpbuf *request, *reply;
1295         struct ofp_header *rq_hdr, *rpy_hdr;
1296
1297         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1298                                OFPT_ECHO_REQUEST, &request);
1299         random_bytes(rq_hdr + 1, payload);
1300
1301         gettimeofday(&start, NULL);
1302         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1303         gettimeofday(&end, NULL);
1304
1305         rpy_hdr = reply->data;
1306         if (reply->size != request->size
1307             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1308             || rpy_hdr->xid != rq_hdr->xid
1309             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1310             printf("Reply does not match request.  Request:\n");
1311             ofp_print(stdout, request, request->size, 2);
1312             printf("Reply:\n");
1313             ofp_print(stdout, reply, reply->size, 2);
1314         }
1315         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1316                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1317                    (1000*(double)(end.tv_sec - start.tv_sec))
1318                    + (.001*(end.tv_usec - start.tv_usec)));
1319         ofpbuf_delete(request);
1320         ofpbuf_delete(reply);
1321     }
1322     vconn_close(vconn);
1323 }
1324
1325 static void
1326 do_benchmark(int argc OVS_UNUSED, char *argv[])
1327 {
1328     size_t max_payload = 65535 - sizeof(struct ofp_header);
1329     struct timeval start, end;
1330     unsigned int payload_size, message_size;
1331     struct vconn *vconn;
1332     double duration;
1333     int count;
1334     int i;
1335
1336     payload_size = atoi(argv[2]);
1337     if (payload_size > max_payload) {
1338         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1339     }
1340     message_size = sizeof(struct ofp_header) + payload_size;
1341
1342     count = atoi(argv[3]);
1343
1344     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1345            count, message_size, count * message_size);
1346
1347     open_vconn(argv[1], &vconn);
1348     gettimeofday(&start, NULL);
1349     for (i = 0; i < count; i++) {
1350         struct ofpbuf *request, *reply;
1351         struct ofp_header *rq_hdr;
1352
1353         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1354         memset(rq_hdr + 1, 0, payload_size);
1355         run(vconn_transact(vconn, request, &reply), "transact");
1356         ofpbuf_delete(reply);
1357     }
1358     gettimeofday(&end, NULL);
1359     vconn_close(vconn);
1360
1361     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1362                 + (.001*(end.tv_usec - start.tv_usec)));
1363     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1364            duration, count / (duration / 1000.0),
1365            count * message_size / (duration / 1000.0));
1366 }
1367
1368 static void
1369 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1370 {
1371     usage();
1372 }
1373
1374 static const struct command all_commands[] = {
1375     { "show", 1, 1, do_show },
1376     { "status", 1, 2, do_status },
1377     { "monitor", 1, 2, do_monitor },
1378     { "snoop", 1, 1, do_snoop },
1379     { "dump-desc", 1, 1, do_dump_desc },
1380     { "dump-tables", 1, 1, do_dump_tables },
1381     { "dump-flows", 1, 2, do_dump_flows },
1382     { "dump-aggregate", 1, 2, do_dump_aggregate },
1383     { "add-flow", 2, 2, do_add_flow },
1384     { "add-flows", 2, 2, do_add_flows },
1385     { "mod-flows", 2, 2, do_mod_flows },
1386     { "del-flows", 1, 2, do_del_flows },
1387     { "tun-cookie", 2, 2, do_tun_cookie },
1388     { "dump-ports", 1, 2, do_dump_ports },
1389     { "mod-port", 3, 3, do_mod_port },
1390     { "probe", 1, 1, do_probe },
1391     { "ping", 1, 2, do_ping },
1392     { "benchmark", 3, 3, do_benchmark },
1393     { "help", 0, INT_MAX, do_help },
1394     { NULL, 0, 0, NULL },
1395 };