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