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