ofp-util: New functions make_nxmsg(), make_nxmsg_xid().
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <getopt.h>
20 #include <inttypes.h>
21 #include <net/if.h>
22 #include <signal.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28
29 #include "byte-order.h"
30 #include "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
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            "  queue-stats SWITCH [PORT [QUEUE]]  dump queue stats\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_nxmsg(sizeof *request, NXT_STATUS_REQUEST, &b);
358     if (argc > 2) {
359         ofpbuf_put(b, argv[2], strlen(argv[2]));
360         update_openflow_length(b);
361     }
362     open_vconn(argv[1], &vconn);
363     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
364     vconn_close(vconn);
365
366     if (b->size < sizeof *reply) {
367         ovs_fatal(0, "short reply (%zu bytes)", b->size);
368     }
369     reply = b->data;
370     if (reply->header.type != OFPT_VENDOR
371         || reply->vendor != ntohl(NX_VENDOR_ID)
372         || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
373         ofp_print(stderr, b->data, b->size, 2);
374         ovs_fatal(0, "bad reply");
375     }
376
377     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
378 }
379
380 static void
381 do_dump_desc(int argc OVS_UNUSED, char *argv[])
382 {
383     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
384 }
385
386 static void
387 do_dump_tables(int argc OVS_UNUSED, char *argv[])
388 {
389     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
390 }
391
392 static uint16_t
393 str_to_port_no(const char *vconn_name, const char *str)
394 {
395     struct ofpbuf *request, *reply;
396     struct ofp_switch_features *osf;
397     struct vconn *vconn;
398     int n_ports;
399     int port_idx;
400     unsigned int port_no;
401
402
403     /* Check if the argument is a port index.  Otherwise, treat it as
404      * the port name. */
405     if (str_to_uint(str, 10, &port_no)) {
406         return port_no;
407     }
408
409     /* Send a "Features Request" to resolve the name into a number. */
410     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
411     open_vconn(vconn_name, &vconn);
412     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
413
414     osf = reply->data;
415     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
416
417     for (port_idx = 0; port_idx < n_ports; port_idx++) {
418         /* Check argument as an interface name */
419         if (!strncmp((char *)osf->ports[port_idx].name, str,
420                     sizeof osf->ports[0].name)) {
421             break;
422         }
423     }
424     if (port_idx == n_ports) {
425         ovs_fatal(0, "couldn't find monitored port: %s", str);
426     }
427
428     ofpbuf_delete(reply);
429     vconn_close(vconn);
430
431     return port_idx;
432 }
433
434 static void
435 do_dump_flows(int argc, char *argv[])
436 {
437     struct ofp_flow_stats_request *req;
438     uint16_t out_port;
439     struct ofpbuf *request;
440
441     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
442     parse_ofp_str(argc > 2 ? argv[2] : "", &req->match, NULL,
443                   &req->table_id, &out_port, NULL, NULL, NULL, NULL);
444     memset(&req->pad, 0, sizeof req->pad);
445     req->out_port = htons(out_port);
446
447     dump_stats_transaction(argv[1], request);
448 }
449
450 static void
451 do_dump_aggregate(int argc, char *argv[])
452 {
453     struct ofp_aggregate_stats_request *req;
454     struct ofpbuf *request;
455     uint16_t out_port;
456
457     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
458     parse_ofp_str(argc > 2 ? argv[2] : "", &req->match, NULL,
459                   &req->table_id, &out_port, NULL, NULL, NULL, NULL);
460     memset(&req->pad, 0, sizeof req->pad);
461     req->out_port = htons(out_port);
462
463     dump_stats_transaction(argv[1], request);
464 }
465
466 static void
467 do_queue_stats(int argc, char *argv[])
468 {
469     struct ofp_queue_stats_request *req;
470     struct ofpbuf *request;
471
472     req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
473
474     if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
475         req->port_no = htons(str_to_port_no(argv[1], argv[2]));
476     } else {
477         req->port_no = htons(OFPP_ALL);
478     }
479     if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
480         req->queue_id = htonl(atoi(argv[3]));
481     } else {
482         req->queue_id = htonl(OFPQ_ALL);
483     }
484
485     memset(req->pad, 0, sizeof req->pad);
486
487     dump_stats_transaction(argv[1], request);
488 }
489
490 static void
491 do_add_flow(int argc OVS_UNUSED, char *argv[])
492 {
493     struct vconn *vconn;
494     struct ofpbuf *buffer;
495     struct ofp_flow_mod *ofm;
496     uint16_t priority, idle_timeout, hard_timeout;
497     uint64_t cookie;
498     struct ofp_match match;
499
500     /* Parse and send.  parse_ofp_str() will expand and reallocate the
501      * data in 'buffer', so we can't keep pointers to across the
502      * parse_ofp_str() call. */
503     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
504     parse_ofp_str(argv[2], &match, buffer,
505                   NULL, NULL, &priority, &idle_timeout, &hard_timeout,
506                   &cookie);
507     ofm = buffer->data;
508     ofm->match = match;
509     ofm->command = htons(OFPFC_ADD);
510     ofm->cookie = htonll(cookie);
511     ofm->idle_timeout = htons(idle_timeout);
512     ofm->hard_timeout = htons(hard_timeout);
513     ofm->buffer_id = htonl(UINT32_MAX);
514     ofm->priority = htons(priority);
515
516     open_vconn(argv[1], &vconn);
517     send_openflow_buffer(vconn, buffer);
518     vconn_close(vconn);
519 }
520
521 static void
522 do_add_flows(int argc OVS_UNUSED, char *argv[])
523 {
524     struct vconn *vconn;
525     struct ofpbuf *b;
526     FILE *file;
527
528     file = fopen(argv[2], "r");
529     if (file == NULL) {
530         ovs_fatal(errno, "%s: open", argv[2]);
531     }
532
533     open_vconn(argv[1], &vconn);
534     while ((b = parse_ofp_add_flow_file(file)) != NULL) {
535         send_openflow_buffer(vconn, b);
536     }
537     vconn_close(vconn);
538     fclose(file);
539 }
540
541 static void
542 do_mod_flows(int argc OVS_UNUSED, char *argv[])
543 {
544     uint16_t priority, idle_timeout, hard_timeout;
545     uint64_t cookie;
546     struct vconn *vconn;
547     struct ofpbuf *buffer;
548     struct ofp_flow_mod *ofm;
549     struct ofp_match match;
550
551     /* Parse and send.  parse_ofp_str() will expand and reallocate the
552      * data in 'buffer', so we can't keep pointers to across the
553      * parse_ofp_str() call. */
554     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
555     parse_ofp_str(argv[2], &match, buffer,
556                   NULL, NULL, &priority, &idle_timeout, &hard_timeout,
557                   &cookie);
558     ofm = buffer->data;
559     ofm->match = match;
560     if (strict) {
561         ofm->command = htons(OFPFC_MODIFY_STRICT);
562     } else {
563         ofm->command = htons(OFPFC_MODIFY);
564     }
565     ofm->idle_timeout = htons(idle_timeout);
566     ofm->hard_timeout = htons(hard_timeout);
567     ofm->cookie = htonll(cookie);
568     ofm->buffer_id = htonl(UINT32_MAX);
569     ofm->priority = htons(priority);
570
571     open_vconn(argv[1], &vconn);
572     send_openflow_buffer(vconn, buffer);
573     vconn_close(vconn);
574 }
575
576 static void do_del_flows(int argc, char *argv[])
577 {
578     struct vconn *vconn;
579     uint16_t priority;
580     uint16_t out_port;
581     struct ofpbuf *buffer;
582     struct ofp_flow_mod *ofm;
583
584     /* Parse and send. */
585     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
586     parse_ofp_str(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
587                   &out_port, &priority, NULL, NULL, NULL);
588     if (strict) {
589         ofm->command = htons(OFPFC_DELETE_STRICT);
590     } else {
591         ofm->command = htons(OFPFC_DELETE);
592     }
593     ofm->idle_timeout = htons(0);
594     ofm->hard_timeout = htons(0);
595     ofm->buffer_id = htonl(UINT32_MAX);
596     ofm->out_port = htons(out_port);
597     ofm->priority = htons(priority);
598
599     open_vconn(argv[1], &vconn);
600     send_openflow_buffer(vconn, buffer);
601     vconn_close(vconn);
602 }
603
604 static void
605 do_tun_cookie(int argc OVS_UNUSED, char *argv[])
606 {
607     struct nxt_tun_id_cookie *tun_id_cookie;
608     struct ofpbuf *buffer;
609     struct vconn *vconn;
610
611     tun_id_cookie = make_nxmsg(sizeof *tun_id_cookie, NXT_TUN_ID_FROM_COOKIE,
612                                &buffer);
613     tun_id_cookie->set = !strcmp(argv[2], "true");
614
615     open_vconn(argv[1], &vconn);
616     send_openflow_buffer(vconn, buffer);
617     vconn_close(vconn);
618 }
619
620 static void
621 monitor_vconn(struct vconn *vconn)
622 {
623     for (;;) {
624         struct ofpbuf *b;
625         run(vconn_recv_block(vconn, &b), "vconn_recv");
626         ofp_print(stderr, b->data, b->size, 2);
627         ofpbuf_delete(b);
628     }
629 }
630
631 static void
632 do_monitor(int argc, char *argv[])
633 {
634     struct vconn *vconn;
635
636     open_vconn(argv[1], &vconn);
637     if (argc > 2) {
638         int miss_send_len = atoi(argv[2]);
639         struct ofp_switch_config *osc;
640         struct ofpbuf *buf;
641
642         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
643         osc->miss_send_len = htons(miss_send_len);
644         send_openflow_buffer(vconn, buf);
645     }
646     monitor_vconn(vconn);
647 }
648
649 static void
650 do_snoop(int argc OVS_UNUSED, char *argv[])
651 {
652     struct vconn *vconn;
653
654     open_vconn__(argv[1], "snoop", &vconn);
655     monitor_vconn(vconn);
656 }
657
658 static void
659 do_dump_ports(int argc, char *argv[])
660 {
661     struct ofp_port_stats_request *req;
662     struct ofpbuf *request;
663     uint16_t port;
664
665     req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
666     port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
667     req->port_no = htons(port);
668     dump_stats_transaction(argv[1], request);
669 }
670
671 static void
672 do_probe(int argc OVS_UNUSED, char *argv[])
673 {
674     struct ofpbuf *request;
675     struct vconn *vconn;
676     struct ofpbuf *reply;
677
678     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
679     open_vconn(argv[1], &vconn);
680     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
681     if (reply->size != sizeof(struct ofp_header)) {
682         ovs_fatal(0, "reply does not match request");
683     }
684     ofpbuf_delete(reply);
685     vconn_close(vconn);
686 }
687
688 static void
689 do_mod_port(int argc OVS_UNUSED, char *argv[])
690 {
691     struct ofpbuf *request, *reply;
692     struct ofp_switch_features *osf;
693     struct ofp_port_mod *opm;
694     struct vconn *vconn;
695     char *endptr;
696     int n_ports;
697     int port_idx;
698     int port_no;
699
700
701     /* Check if the argument is a port index.  Otherwise, treat it as
702      * the port name. */
703     port_no = strtol(argv[2], &endptr, 10);
704     if (port_no == 0 && endptr == argv[2]) {
705         port_no = -1;
706     }
707
708     /* Send a "Features Request" to get the information we need in order
709      * to modify the port. */
710     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
711     open_vconn(argv[1], &vconn);
712     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
713
714     osf = reply->data;
715     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
716
717     for (port_idx = 0; port_idx < n_ports; port_idx++) {
718         if (port_no != -1) {
719             /* Check argument as a port index */
720             if (osf->ports[port_idx].port_no == htons(port_no)) {
721                 break;
722             }
723         } else {
724             /* Check argument as an interface name */
725             if (!strncmp((char *)osf->ports[port_idx].name, argv[2],
726                         sizeof osf->ports[0].name)) {
727                 break;
728             }
729
730         }
731     }
732     if (port_idx == n_ports) {
733         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
734     }
735
736     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
737     opm->port_no = osf->ports[port_idx].port_no;
738     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
739     opm->config = htonl(0);
740     opm->mask = htonl(0);
741     opm->advertise = htonl(0);
742
743     printf("modifying port: %s\n", osf->ports[port_idx].name);
744
745     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
746         opm->mask |= htonl(OFPPC_PORT_DOWN);
747     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN,
748                 sizeof MOD_PORT_CMD_DOWN)) {
749         opm->mask |= htonl(OFPPC_PORT_DOWN);
750         opm->config |= htonl(OFPPC_PORT_DOWN);
751     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD,
752                 sizeof MOD_PORT_CMD_FLOOD)) {
753         opm->mask |= htonl(OFPPC_NO_FLOOD);
754     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD,
755                 sizeof MOD_PORT_CMD_NOFLOOD)) {
756         opm->mask |= htonl(OFPPC_NO_FLOOD);
757         opm->config |= htonl(OFPPC_NO_FLOOD);
758     } else {
759         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
760     }
761
762     send_openflow_buffer(vconn, request);
763
764     ofpbuf_delete(reply);
765     vconn_close(vconn);
766 }
767
768 static void
769 do_ping(int argc, char *argv[])
770 {
771     size_t max_payload = 65535 - sizeof(struct ofp_header);
772     unsigned int payload;
773     struct vconn *vconn;
774     int i;
775
776     payload = argc > 2 ? atoi(argv[2]) : 64;
777     if (payload > max_payload) {
778         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
779     }
780
781     open_vconn(argv[1], &vconn);
782     for (i = 0; i < 10; i++) {
783         struct timeval start, end;
784         struct ofpbuf *request, *reply;
785         struct ofp_header *rq_hdr, *rpy_hdr;
786
787         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
788                                OFPT_ECHO_REQUEST, &request);
789         random_bytes(rq_hdr + 1, payload);
790
791         gettimeofday(&start, NULL);
792         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
793         gettimeofday(&end, NULL);
794
795         rpy_hdr = reply->data;
796         if (reply->size != request->size
797             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
798             || rpy_hdr->xid != rq_hdr->xid
799             || rpy_hdr->type != OFPT_ECHO_REPLY) {
800             printf("Reply does not match request.  Request:\n");
801             ofp_print(stdout, request, request->size, 2);
802             printf("Reply:\n");
803             ofp_print(stdout, reply, reply->size, 2);
804         }
805         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
806                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
807                    (1000*(double)(end.tv_sec - start.tv_sec))
808                    + (.001*(end.tv_usec - start.tv_usec)));
809         ofpbuf_delete(request);
810         ofpbuf_delete(reply);
811     }
812     vconn_close(vconn);
813 }
814
815 static void
816 do_benchmark(int argc OVS_UNUSED, char *argv[])
817 {
818     size_t max_payload = 65535 - sizeof(struct ofp_header);
819     struct timeval start, end;
820     unsigned int payload_size, message_size;
821     struct vconn *vconn;
822     double duration;
823     int count;
824     int i;
825
826     payload_size = atoi(argv[2]);
827     if (payload_size > max_payload) {
828         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
829     }
830     message_size = sizeof(struct ofp_header) + payload_size;
831
832     count = atoi(argv[3]);
833
834     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
835            count, message_size, count * message_size);
836
837     open_vconn(argv[1], &vconn);
838     gettimeofday(&start, NULL);
839     for (i = 0; i < count; i++) {
840         struct ofpbuf *request, *reply;
841         struct ofp_header *rq_hdr;
842
843         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
844         memset(rq_hdr + 1, 0, payload_size);
845         run(vconn_transact(vconn, request, &reply), "transact");
846         ofpbuf_delete(reply);
847     }
848     gettimeofday(&end, NULL);
849     vconn_close(vconn);
850
851     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
852                 + (.001*(end.tv_usec - start.tv_usec)));
853     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
854            duration, count / (duration / 1000.0),
855            count * message_size / (duration / 1000.0));
856 }
857
858 /* This command is really only useful for testing the flow parser (ofp_parse),
859  * so it is undocumented. */
860 static void
861 do_parse_flows(int argc OVS_UNUSED, char *argv[])
862 {
863     struct ofpbuf *b;
864     FILE *file;
865
866     file = fopen(argv[1], "r");
867     if (file == NULL) {
868         ovs_fatal(errno, "%s: open", argv[2]);
869     }
870
871     while ((b = parse_ofp_add_flow_file(file)) != NULL) {
872         ofp_print(stdout, b->data, b->size, 0);
873         ofpbuf_delete(b);
874     }
875     fclose(file);
876 }
877
878 static void
879 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
880 {
881     usage();
882 }
883
884 static const struct command all_commands[] = {
885     { "show", 1, 1, do_show },
886     { "status", 1, 2, do_status },
887     { "monitor", 1, 2, do_monitor },
888     { "snoop", 1, 1, do_snoop },
889     { "dump-desc", 1, 1, do_dump_desc },
890     { "dump-tables", 1, 1, do_dump_tables },
891     { "dump-flows", 1, 2, do_dump_flows },
892     { "dump-aggregate", 1, 2, do_dump_aggregate },
893     { "queue-stats", 1, 3, do_queue_stats },
894     { "add-flow", 2, 2, do_add_flow },
895     { "add-flows", 2, 2, do_add_flows },
896     { "mod-flows", 2, 2, do_mod_flows },
897     { "del-flows", 1, 2, do_del_flows },
898     { "tun-cookie", 2, 2, do_tun_cookie },
899     { "dump-ports", 1, 2, do_dump_ports },
900     { "mod-port", 3, 3, do_mod_port },
901     { "probe", 1, 1, do_probe },
902     { "ping", 1, 2, do_ping },
903     { "benchmark", 3, 3, do_benchmark },
904     { "parse-flows", 1, 1, do_parse_flows },
905     { "help", 0, INT_MAX, do_help },
906     { NULL, 0, 0, NULL },
907 };