ovs-ofctl: Simplify code by using strcasecmp() instead of strncasecmp().
[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 <errno.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <net/if.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28
29 #include "byte-order.h"
30 #include "classifier.h"
31 #include "command-line.h"
32 #include "compiler.h"
33 #include "dirs.h"
34 #include "dpif.h"
35 #include "dynamic-string.h"
36 #include "netlink.h"
37 #include "nx-match.h"
38 #include "odp-util.h"
39 #include "ofp-parse.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 "random.h"
46 #include "stream-ssl.h"
47 #include "timeval.h"
48 #include "util.h"
49 #include "vconn.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(ofctl);
53
54 /* Use strict matching for flow mod commands? */
55 static bool strict;
56
57 static const struct command all_commands[];
58
59 static void usage(void) NO_RETURN;
60 static void parse_options(int argc, char *argv[]);
61
62 int
63 main(int argc, char *argv[])
64 {
65     set_program_name(argv[0]);
66     parse_options(argc, argv);
67     signal(SIGPIPE, SIG_IGN);
68     run_command(argc - optind, argv + optind, all_commands);
69     return 0;
70 }
71
72 static void
73 parse_options(int argc, char *argv[])
74 {
75     enum {
76         OPT_STRICT = UCHAR_MAX + 1,
77         VLOG_OPTION_ENUMS
78     };
79     static struct option long_options[] = {
80         {"timeout", required_argument, 0, 't'},
81         {"strict", no_argument, 0, OPT_STRICT},
82         {"help", no_argument, 0, 'h'},
83         {"version", no_argument, 0, 'V'},
84         VLOG_LONG_OPTIONS,
85         STREAM_SSL_LONG_OPTIONS
86         {0, 0, 0, 0},
87     };
88     char *short_options = long_options_to_short_options(long_options);
89
90     for (;;) {
91         unsigned long int timeout;
92         int c;
93
94         c = getopt_long(argc, argv, short_options, long_options, NULL);
95         if (c == -1) {
96             break;
97         }
98
99         switch (c) {
100         case 't':
101             timeout = strtoul(optarg, NULL, 10);
102             if (timeout <= 0) {
103                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
104                           optarg);
105             } else {
106                 time_alarm(timeout);
107             }
108             break;
109
110         case 'h':
111             usage();
112
113         case 'V':
114             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
115             exit(EXIT_SUCCESS);
116
117         case OPT_STRICT:
118             strict = true;
119             break;
120
121         VLOG_OPTION_HANDLERS
122         STREAM_SSL_OPTION_HANDLERS
123
124         case '?':
125             exit(EXIT_FAILURE);
126
127         default:
128             abort();
129         }
130     }
131     free(short_options);
132 }
133
134 static void
135 usage(void)
136 {
137     printf("%s: OpenFlow switch management utility\n"
138            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
139            "\nFor OpenFlow switches:\n"
140            "  show SWITCH                 show OpenFlow information\n"
141            "  status SWITCH [KEY]         report statistics (about KEY)\n"
142            "  dump-desc SWITCH            print switch description\n"
143            "  dump-tables SWITCH          print table stats\n"
144            "  mod-port SWITCH IFACE ACT   modify port behavior\n"
145            "  dump-ports SWITCH [PORT]    print port statistics\n"
146            "  dump-flows SWITCH           print all flow entries\n"
147            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
148            "  dump-aggregate SWITCH       print aggregate flow statistics\n"
149            "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
150            "  queue-stats SWITCH [PORT [QUEUE]]  dump queue stats\n"
151            "  add-flow SWITCH FLOW        add flow described by FLOW\n"
152            "  add-flows SWITCH FILE       add flows from FILE\n"
153            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
154            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
155            "  monitor SWITCH [MISSLEN]    print packets received from SWITCH\n"
156            "\nFor OpenFlow switches and controllers:\n"
157            "  probe VCONN                 probe whether VCONN is up\n"
158            "  ping VCONN [N]              latency of N-byte echos\n"
159            "  benchmark VCONN N COUNT     bandwidth of COUNT N-byte echos\n"
160            "where each SWITCH is an active OpenFlow connection method.\n",
161            program_name, program_name);
162     vconn_usage(true, false, false);
163     vlog_usage();
164     printf("\nOther options:\n"
165            "  --strict                    use strict match for flow commands\n"
166            "  -t, --timeout=SECS          give up after SECS seconds\n"
167            "  -h, --help                  display this help message\n"
168            "  -V, --version               display version information\n");
169     exit(EXIT_SUCCESS);
170 }
171
172 static void run(int retval, const char *message, ...)
173     PRINTF_FORMAT(2, 3);
174
175 static void run(int retval, const char *message, ...)
176 {
177     if (retval) {
178         va_list args;
179
180         fprintf(stderr, "%s: ", program_name);
181         va_start(args, message);
182         vfprintf(stderr, message, args);
183         va_end(args);
184         if (retval == EOF) {
185             fputs(": unexpected end of file\n", stderr);
186         } else {
187             fprintf(stderr, ": %s\n", strerror(retval));
188         }
189
190         exit(EXIT_FAILURE);
191     }
192 }
193 \f
194 /* Generic commands. */
195
196 static void
197 open_vconn_socket(const char *name, struct vconn **vconnp)
198 {
199     char *vconn_name = xasprintf("unix:%s", name);
200     VLOG_INFO("connecting to %s", vconn_name);
201     run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
202         "connecting to %s", vconn_name);
203     free(vconn_name);
204 }
205
206 static void
207 open_vconn__(const char *name, const char *default_suffix,
208              struct vconn **vconnp)
209 {
210     struct dpif *dpif;
211     struct stat s;
212     char *bridge_path, *datapath_name, *datapath_type;
213
214     bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
215     dp_parse_name(name, &datapath_name, &datapath_type);
216
217     if (strstr(name, ":")) {
218         run(vconn_open_block(name, OFP_VERSION, vconnp),
219             "connecting to %s", name);
220     } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
221         open_vconn_socket(name, vconnp);
222     } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
223         open_vconn_socket(bridge_path, vconnp);
224     } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
225         char dpif_name[IF_NAMESIZE + 1];
226         char *socket_name;
227
228         run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
229             "obtaining name of %s", dpif_name);
230         dpif_close(dpif);
231         if (strcmp(dpif_name, name)) {
232             VLOG_INFO("datapath %s is named %s", name, dpif_name);
233         }
234
235         socket_name = xasprintf("%s/%s.%s",
236                                 ovs_rundir(), dpif_name, default_suffix);
237         if (stat(socket_name, &s)) {
238             ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
239                       name, socket_name);
240         } else if (!S_ISSOCK(s.st_mode)) {
241             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
242                       name, socket_name);
243         }
244
245         open_vconn_socket(socket_name, vconnp);
246         free(socket_name);
247     } else {
248         ovs_fatal(0, "%s is not a valid connection method", name);
249     }
250
251     free(datapath_name);
252     free(datapath_type);
253     free(bridge_path);
254 }
255
256 static void
257 open_vconn(const char *name, struct vconn **vconnp)
258 {
259     return open_vconn__(name, "mgmt", vconnp);
260 }
261
262 static void *
263 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
264 {
265     struct ofp_stats_request *rq;
266     rq = make_openflow((offsetof(struct ofp_stats_request, body)
267                         + body_len), OFPT_STATS_REQUEST, bufferp);
268     rq->type = htons(type);
269     rq->flags = htons(0);
270     return rq->body;
271 }
272
273 static void
274 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
275 {
276     update_openflow_length(buffer);
277     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
278 }
279
280 static void
281 dump_transaction(const char *vconn_name, struct ofpbuf *request)
282 {
283     struct vconn *vconn;
284     struct ofpbuf *reply;
285
286     update_openflow_length(request);
287     open_vconn(vconn_name, &vconn);
288     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
289     ofp_print(stdout, reply->data, reply->size, 1);
290     vconn_close(vconn);
291 }
292
293 static void
294 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
295 {
296     struct ofpbuf *request;
297     make_openflow(sizeof(struct ofp_header), request_type, &request);
298     dump_transaction(vconn_name, request);
299 }
300
301 static void
302 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
303 {
304     ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
305     struct vconn *vconn;
306     bool done = false;
307
308     open_vconn(vconn_name, &vconn);
309     send_openflow_buffer(vconn, request);
310     while (!done) {
311         uint32_t recv_xid;
312         struct ofpbuf *reply;
313
314         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
315         recv_xid = ((struct ofp_header *) reply->data)->xid;
316         if (send_xid == recv_xid) {
317             struct ofp_stats_reply *osr;
318
319             ofp_print(stdout, reply->data, reply->size, 1);
320
321             osr = ofpbuf_at(reply, 0, sizeof *osr);
322             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
323         } else {
324             VLOG_DBG("received reply with xid %08"PRIx32" "
325                      "!= expected %08"PRIx32, recv_xid, send_xid);
326         }
327         ofpbuf_delete(reply);
328     }
329     vconn_close(vconn);
330 }
331
332 static void
333 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
334 {
335     struct ofpbuf *request;
336     alloc_stats_request(0, stats_type, &request);
337     dump_stats_transaction(vconn_name, request);
338 }
339
340 static void
341 do_show(int argc OVS_UNUSED, char *argv[])
342 {
343     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
344     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
345 }
346
347 static void
348 do_status(int argc, char *argv[])
349 {
350     struct nicira_header *request, *reply;
351     struct vconn *vconn;
352     struct ofpbuf *b;
353
354     request = make_nxmsg(sizeof *request, NXT_STATUS_REQUEST, &b);
355     if (argc > 2) {
356         ofpbuf_put(b, argv[2], strlen(argv[2]));
357         update_openflow_length(b);
358     }
359     open_vconn(argv[1], &vconn);
360     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
361     vconn_close(vconn);
362
363     if (b->size < sizeof *reply) {
364         ovs_fatal(0, "short reply (%zu bytes)", b->size);
365     }
366     reply = b->data;
367     if (reply->header.type != OFPT_VENDOR
368         || reply->vendor != ntohl(NX_VENDOR_ID)
369         || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
370         ofp_print(stderr, b->data, b->size, 2);
371         ovs_fatal(0, "bad reply");
372     }
373
374     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
375 }
376
377 static void
378 do_dump_desc(int argc OVS_UNUSED, char *argv[])
379 {
380     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
381 }
382
383 static void
384 do_dump_tables(int argc OVS_UNUSED, char *argv[])
385 {
386     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
387 }
388
389 static uint16_t
390 str_to_port_no(const char *vconn_name, const char *str)
391 {
392     struct ofpbuf *request, *reply;
393     struct ofp_switch_features *osf;
394     struct vconn *vconn;
395     int n_ports;
396     int port_idx;
397     unsigned int port_no;
398
399
400     /* Check if the argument is a port index.  Otherwise, treat it as
401      * the port name. */
402     if (str_to_uint(str, 10, &port_no)) {
403         return port_no;
404     }
405
406     /* Send a "Features Request" to resolve the name into a number. */
407     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
408     open_vconn(vconn_name, &vconn);
409     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
410
411     osf = reply->data;
412     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
413
414     for (port_idx = 0; port_idx < n_ports; port_idx++) {
415         /* Check argument as an interface name */
416         if (!strncmp((char *)osf->ports[port_idx].name, str,
417                     sizeof osf->ports[0].name)) {
418             break;
419         }
420     }
421     if (port_idx == n_ports) {
422         ovs_fatal(0, "couldn't find monitored port: %s", str);
423     }
424
425     ofpbuf_delete(reply);
426     vconn_close(vconn);
427
428     return ntohs(osf->ports[port_idx].port_no);
429 }
430
431 static void
432 do_dump_flows(int argc, char *argv[])
433 {
434     struct ofp_flow_stats_request *req;
435     struct parsed_flow pf;
436     struct ofpbuf *request;
437
438     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
439     parse_ofp_str(&pf, NULL, argc > 2 ? argv[2] : "");
440     ofputil_cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &req->match);
441     memset(&req->pad, 0, sizeof req->pad);
442     req->out_port = htons(pf.out_port);
443
444     dump_stats_transaction(argv[1], request);
445 }
446
447 static void
448 do_dump_aggregate(int argc, char *argv[])
449 {
450     struct ofp_aggregate_stats_request *req;
451     struct ofpbuf *request;
452     struct parsed_flow pf;
453
454     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
455     parse_ofp_str(&pf, NULL, argc > 2 ? argv[2] : "");
456     ofputil_cls_rule_to_match(&pf.rule, NXFF_OPENFLOW10, &req->match);
457     memset(&req->pad, 0, sizeof req->pad);
458     req->out_port = htons(pf.out_port);
459
460     dump_stats_transaction(argv[1], request);
461 }
462
463 static void
464 do_queue_stats(int argc, char *argv[])
465 {
466     struct ofp_queue_stats_request *req;
467     struct ofpbuf *request;
468
469     req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
470
471     if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
472         req->port_no = htons(str_to_port_no(argv[1], argv[2]));
473     } else {
474         req->port_no = htons(OFPP_ALL);
475     }
476     if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
477         req->queue_id = htonl(atoi(argv[3]));
478     } else {
479         req->queue_id = htonl(OFPQ_ALL);
480     }
481
482     memset(req->pad, 0, sizeof req->pad);
483
484     dump_stats_transaction(argv[1], request);
485 }
486
487 static void
488 do_add_flow(int argc OVS_UNUSED, char *argv[])
489 {
490     struct vconn *vconn;
491     struct ofpbuf *buffer;
492
493     buffer = parse_ofp_flow_mod_str(argv[2], OFPFC_ADD);
494
495     open_vconn(argv[1], &vconn);
496     send_openflow_buffer(vconn, buffer);
497     vconn_close(vconn);
498 }
499
500 static void
501 do_add_flows(int argc OVS_UNUSED, char *argv[])
502 {
503     struct vconn *vconn;
504     struct ofpbuf *b;
505     FILE *file;
506
507     file = fopen(argv[2], "r");
508     if (file == NULL) {
509         ovs_fatal(errno, "%s: open", argv[2]);
510     }
511
512     open_vconn(argv[1], &vconn);
513     while ((b = parse_ofp_add_flow_file(file)) != NULL) {
514         send_openflow_buffer(vconn, b);
515     }
516     vconn_close(vconn);
517     fclose(file);
518 }
519
520 static void
521 do_mod_flows(int argc OVS_UNUSED, char *argv[])
522 {
523     struct vconn *vconn;
524     struct ofpbuf *buffer;
525     uint16_t command;
526
527     command = strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY;
528     buffer = parse_ofp_flow_mod_str(argv[2], command);
529     open_vconn(argv[1], &vconn);
530     send_openflow_buffer(vconn, buffer);
531     vconn_close(vconn);
532 }
533
534 static void do_del_flows(int argc, char *argv[])
535 {
536     struct vconn *vconn;
537     struct ofpbuf *buffer;
538     uint16_t command;
539
540     command = strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE;
541     buffer = parse_ofp_flow_mod_str(argc > 2 ? argv[2] : "", command);
542
543     open_vconn(argv[1], &vconn);
544     send_openflow_buffer(vconn, buffer);
545     vconn_close(vconn);
546 }
547
548 static void
549 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
550 {
551     struct nxt_tun_id_cookie *tun_id_cookie;
552     struct ofpbuf *buffer;
553     struct vconn *vconn;
554
555     tun_id_cookie = make_nxmsg(sizeof *tun_id_cookie, NXT_TUN_ID_FROM_COOKIE,
556                                &buffer);
557     tun_id_cookie->set = !strcmp(argv[2], "true");
558
559     open_vconn(argv[1], &vconn);
560     send_openflow_buffer(vconn, buffer);
561     vconn_close(vconn);
562 }
563
564 static void
565 monitor_vconn(struct vconn *vconn)
566 {
567     for (;;) {
568         struct ofpbuf *b;
569         run(vconn_recv_block(vconn, &b), "vconn_recv");
570         ofp_print(stderr, b->data, b->size, 2);
571         ofpbuf_delete(b);
572     }
573 }
574
575 static void
576 do_monitor(int argc, char *argv[])
577 {
578     struct vconn *vconn;
579
580     open_vconn(argv[1], &vconn);
581     if (argc > 2) {
582         int miss_send_len = atoi(argv[2]);
583         struct ofp_switch_config *osc;
584         struct ofpbuf *buf;
585
586         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
587         osc->miss_send_len = htons(miss_send_len);
588         send_openflow_buffer(vconn, buf);
589     }
590     monitor_vconn(vconn);
591 }
592
593 static void
594 do_snoop(int argc OVS_UNUSED, char *argv[])
595 {
596     struct vconn *vconn;
597
598     open_vconn__(argv[1], "snoop", &vconn);
599     monitor_vconn(vconn);
600 }
601
602 static void
603 do_dump_ports(int argc, char *argv[])
604 {
605     struct ofp_port_stats_request *req;
606     struct ofpbuf *request;
607     uint16_t port;
608
609     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
610     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
611     req->port_no = htons(port);
612     dump_stats_transaction(argv[1], request);
613 }
614
615 static void
616 do_probe(int argc OVS_UNUSED, char *argv[])
617 {
618     struct ofpbuf *request;
619     struct vconn *vconn;
620     struct ofpbuf *reply;
621
622     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
623     open_vconn(argv[1], &vconn);
624     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
625     if (reply->size != sizeof(struct ofp_header)) {
626         ovs_fatal(0, "reply does not match request");
627     }
628     ofpbuf_delete(reply);
629     vconn_close(vconn);
630 }
631
632 static void
633 do_mod_port(int argc OVS_UNUSED, char *argv[])
634 {
635     struct ofpbuf *request, *reply;
636     struct ofp_switch_features *osf;
637     struct ofp_port_mod *opm;
638     struct vconn *vconn;
639     char *endptr;
640     int n_ports;
641     int port_idx;
642     int port_no;
643
644
645     /* Check if the argument is a port index.  Otherwise, treat it as
646      * the port name. */
647     port_no = strtol(argv[2], &endptr, 10);
648     if (port_no == 0 && endptr == argv[2]) {
649         port_no = -1;
650     }
651
652     /* Send a "Features Request" to get the information we need in order
653      * to modify the port. */
654     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
655     open_vconn(argv[1], &vconn);
656     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
657
658     osf = reply->data;
659     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
660
661     for (port_idx = 0; port_idx < n_ports; port_idx++) {
662         if (port_no != -1) {
663             /* Check argument as a port index */
664             if (osf->ports[port_idx].port_no == htons(port_no)) {
665                 break;
666             }
667         } else {
668             /* Check argument as an interface name */
669             if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
670                         sizeof osf->ports[0].name)) {
671                 break;
672             }
673
674         }
675     }
676     if (port_idx == n_ports) {
677         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
678     }
679
680     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
681     opm->port_no = osf->ports[port_idx].port_no;
682     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
683     opm->config = htonl(0);
684     opm->mask = htonl(0);
685     opm->advertise = htonl(0);
686
687     printf("modifying port: %s\n", osf->ports[port_idx].name);
688
689     if (!strcasecmp(argv[3], "up")) {
690         opm->mask |= htonl(OFPPC_PORT_DOWN);
691     } else if (!strcasecmp(argv[3], "down")) {
692         opm->mask |= htonl(OFPPC_PORT_DOWN);
693         opm->config |= htonl(OFPPC_PORT_DOWN);
694     } else if (!strcasecmp(argv[3], "flood")) {
695         opm->mask |= htonl(OFPPC_NO_FLOOD);
696     } else if (!strcasecmp(argv[3], "noflood")) {
697         opm->mask |= htonl(OFPPC_NO_FLOOD);
698         opm->config |= htonl(OFPPC_NO_FLOOD);
699     } else {
700         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
701     }
702
703     send_openflow_buffer(vconn, request);
704
705     ofpbuf_delete(reply);
706     vconn_close(vconn);
707 }
708
709 static void
710 do_ping(int argc, char *argv[])
711 {
712     size_t max_payload = 65535 - sizeof(struct ofp_header);
713     unsigned int payload;
714     struct vconn *vconn;
715     int i;
716
717     payload = argc > 2 ? atoi(argv[2]) : 64;
718     if (payload > max_payload) {
719         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
720     }
721
722     open_vconn(argv[1], &vconn);
723     for (i = 0; i < 10; i++) {
724         struct timeval start, end;
725         struct ofpbuf *request, *reply;
726         struct ofp_header *rq_hdr, *rpy_hdr;
727
728         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
729                                OFPT_ECHO_REQUEST, &request);
730         random_bytes(rq_hdr + 1, payload);
731
732         gettimeofday(&start, NULL);
733         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
734         gettimeofday(&end, NULL);
735
736         rpy_hdr = reply->data;
737         if (reply->size != request->size
738             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
739             || rpy_hdr->xid != rq_hdr->xid
740             || rpy_hdr->type != OFPT_ECHO_REPLY) {
741             printf("Reply does not match request.  Request:\n");
742             ofp_print(stdout, request, request->size, 2);
743             printf("Reply:\n");
744             ofp_print(stdout, reply, reply->size, 2);
745         }
746         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
747                reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
748                    (1000*(double)(end.tv_sec - start.tv_sec))
749                    + (.001*(end.tv_usec - start.tv_usec)));
750         ofpbuf_delete(request);
751         ofpbuf_delete(reply);
752     }
753     vconn_close(vconn);
754 }
755
756 static void
757 do_benchmark(int argc OVS_UNUSED, char *argv[])
758 {
759     size_t max_payload = 65535 - sizeof(struct ofp_header);
760     struct timeval start, end;
761     unsigned int payload_size, message_size;
762     struct vconn *vconn;
763     double duration;
764     int count;
765     int i;
766
767     payload_size = atoi(argv[2]);
768     if (payload_size > max_payload) {
769         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
770     }
771     message_size = sizeof(struct ofp_header) + payload_size;
772
773     count = atoi(argv[3]);
774
775     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
776            count, message_size, count * message_size);
777
778     open_vconn(argv[1], &vconn);
779     gettimeofday(&start, NULL);
780     for (i = 0; i < count; i++) {
781         struct ofpbuf *request, *reply;
782         struct ofp_header *rq_hdr;
783
784         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
785         memset(rq_hdr + 1, 0, payload_size);
786         run(vconn_transact(vconn, request, &reply), "transact");
787         ofpbuf_delete(reply);
788     }
789     gettimeofday(&end, NULL);
790     vconn_close(vconn);
791
792     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
793                 + (.001*(end.tv_usec - start.tv_usec)));
794     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
795            duration, count / (duration / 1000.0),
796            count * message_size / (duration / 1000.0));
797 }
798
799 static void
800 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
801 {
802     usage();
803 }
804 \f
805 /* Undocumented commands for unit testing. */
806
807 static void
808 do_parse_flows(int argc OVS_UNUSED, char *argv[])
809 {
810     struct ofpbuf *b;
811     FILE *file;
812
813     file = fopen(argv[1], "r");
814     if (file == NULL) {
815         ovs_fatal(errno, "%s: open", argv[2]);
816     }
817
818     while ((b = parse_ofp_add_flow_file(file)) != NULL) {
819         ofp_print(stdout, b->data, b->size, 0);
820         ofpbuf_delete(b);
821     }
822     fclose(file);
823 }
824
825 static void
826 do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
827 {
828     struct ds in;
829
830     ds_init(&in);
831     while (!ds_get_line(&in, stdin)) {
832         struct ofpbuf nx_match;
833         struct cls_rule rule;
834         int match_len;
835         int error;
836         char *s;
837
838         /* Delete comments, skip blank lines. */
839         s = ds_cstr(&in);
840         if (*s == '#') {
841             puts(s);
842             continue;
843         }
844         if (strchr(s, '#')) {
845             *strchr(s, '#') = '\0';
846         }
847         if (s[strspn(s, " ")] == '\0') {
848             putchar('\n');
849             continue;
850         }
851
852         /* Convert string to nx_match. */
853         ofpbuf_init(&nx_match, 0);
854         match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
855
856         /* Convert nx_match to cls_rule. */
857         error = nx_pull_match(&nx_match, match_len, 0, &rule);
858         if (!error) {
859             char *out;
860
861             /* Convert cls_rule back to nx_match. */
862             ofpbuf_uninit(&nx_match);
863             ofpbuf_init(&nx_match, 0);
864             match_len = nx_put_match(&nx_match, &rule);
865
866             /* Convert nx_match to string. */
867             out = nx_match_to_string(nx_match.data, match_len);
868             puts(out);
869             free(out);
870         } else {
871             printf("nx_pull_match() returned error %x\n", error);
872         }
873
874         ofpbuf_uninit(&nx_match);
875     }
876     ds_destroy(&in);
877 }
878
879 static const struct command all_commands[] = {
880     { "show", 1, 1, do_show },
881     { "status", 1, 2, do_status },
882     { "monitor", 1, 2, do_monitor },
883     { "snoop", 1, 1, do_snoop },
884     { "dump-desc", 1, 1, do_dump_desc },
885     { "dump-tables", 1, 1, do_dump_tables },
886     { "dump-flows", 1, 2, do_dump_flows },
887     { "dump-aggregate", 1, 2, do_dump_aggregate },
888     { "queue-stats", 1, 3, do_queue_stats },
889     { "add-flow", 2, 2, do_add_flow },
890     { "add-flows", 2, 2, do_add_flows },
891     { "mod-flows", 2, 2, do_mod_flows },
892     { "del-flows", 1, 2, do_del_flows },
893     { "tun-cookie", 2, 2, do_tun_cookie },
894     { "dump-ports", 1, 2, do_dump_ports },
895     { "mod-port", 3, 3, do_mod_port },
896     { "probe", 1, 1, do_probe },
897     { "ping", 1, 2, do_ping },
898     { "benchmark", 3, 3, do_benchmark },
899     { "help", 0, INT_MAX, do_help },
900
901     /* Undocumented commands for testing. */
902     { "parse-flows", 1, 1, do_parse_flows },
903     { "parse-nx-match", 0, 0, do_parse_nx_match },
904
905     { NULL, 0, 0, NULL },
906 };