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