]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-ofctl.c
Rename static functions to increase uniqueness.
[mirror_ovs.git] / utilities / ovs-ofctl.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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>
22 #include <net/if.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/fcntl.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30
31 #include "byte-order.h"
32 #include "classifier.h"
33 #include "command-line.h"
34 #include "daemon.h"
35 #include "compiler.h"
36 #include "dirs.h"
37 #include "dynamic-string.h"
38 #include "nx-match.h"
39 #include "odp-util.h"
40 #include "ofp-actions.h"
41 #include "ofp-errors.h"
42 #include "ofp-parse.h"
43 #include "ofp-print.h"
44 #include "ofp-util.h"
45 #include "ofpbuf.h"
46 #include "ofproto/ofproto.h"
47 #include "openflow/nicira-ext.h"
48 #include "openflow/openflow.h"
49 #include "packets.h"
50 #include "poll-loop.h"
51 #include "random.h"
52 #include "stream-ssl.h"
53 #include "timeval.h"
54 #include "unixctl.h"
55 #include "util.h"
56 #include "vconn.h"
57 #include "vlog.h"
58
59 VLOG_DEFINE_THIS_MODULE(ofctl);
60
61 /* --strict: Use strict matching for flow mod commands? Additionally governs
62 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
63 */
64 static bool strict;
65
66 /* --readd: If true, on replace-flows, re-add even flows that have not changed
67 * (to reset flow counters). */
68 static bool readd;
69
70 /* -F, --flow-format: Allowed protocols. By default, any protocol is
71 * allowed. */
72 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
73
74 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
75 * commands. Either one of NXPIF_* to force a particular packet_in format, or
76 * -1 to let ovs-ofctl choose the default. */
77 static int preferred_packet_in_format = -1;
78
79 /* -m, --more: Additional verbosity for ofp-print functions. */
80 static int verbosity;
81
82 /* --timestamp: Print a timestamp before each received packet on "monitor" and
83 * "snoop" command? */
84 static bool timestamp;
85
86 static const struct command all_commands[];
87
88 static void usage(void) NO_RETURN;
89 static void parse_options(int argc, char *argv[]);
90
91 int
92 main(int argc, char *argv[])
93 {
94 set_program_name(argv[0]);
95 parse_options(argc, argv);
96 signal(SIGPIPE, SIG_IGN);
97 run_command(argc - optind, argv + optind, all_commands);
98 return 0;
99 }
100
101 static void
102 parse_options(int argc, char *argv[])
103 {
104 enum {
105 OPT_STRICT = UCHAR_MAX + 1,
106 OPT_READD,
107 OPT_TIMESTAMP,
108 DAEMON_OPTION_ENUMS,
109 VLOG_OPTION_ENUMS
110 };
111 static struct option long_options[] = {
112 {"timeout", required_argument, NULL, 't'},
113 {"strict", no_argument, NULL, OPT_STRICT},
114 {"readd", no_argument, NULL, OPT_READD},
115 {"flow-format", required_argument, NULL, 'F'},
116 {"packet-in-format", required_argument, NULL, 'P'},
117 {"more", no_argument, NULL, 'm'},
118 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
119 {"help", no_argument, NULL, 'h'},
120 {"version", no_argument, NULL, 'V'},
121 DAEMON_LONG_OPTIONS,
122 VLOG_LONG_OPTIONS,
123 STREAM_SSL_LONG_OPTIONS,
124 {NULL, 0, NULL, 0},
125 };
126 char *short_options = long_options_to_short_options(long_options);
127
128 for (;;) {
129 unsigned long int timeout;
130 int c;
131
132 c = getopt_long(argc, argv, short_options, long_options, NULL);
133 if (c == -1) {
134 break;
135 }
136
137 switch (c) {
138 case 't':
139 timeout = strtoul(optarg, NULL, 10);
140 if (timeout <= 0) {
141 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
142 optarg);
143 } else {
144 time_alarm(timeout);
145 }
146 break;
147
148 case 'F':
149 allowed_protocols = ofputil_protocols_from_string(optarg);
150 if (!allowed_protocols) {
151 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
152 }
153 break;
154
155 case 'P':
156 preferred_packet_in_format =
157 ofputil_packet_in_format_from_string(optarg);
158 if (preferred_packet_in_format < 0) {
159 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
160 }
161 break;
162
163 case 'm':
164 verbosity++;
165 break;
166
167 case 'h':
168 usage();
169
170 case 'V':
171 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
172 exit(EXIT_SUCCESS);
173
174 case OPT_STRICT:
175 strict = true;
176 break;
177
178 case OPT_READD:
179 readd = true;
180 break;
181
182 case OPT_TIMESTAMP:
183 timestamp = true;
184 break;
185
186 DAEMON_OPTION_HANDLERS
187 VLOG_OPTION_HANDLERS
188 STREAM_SSL_OPTION_HANDLERS
189
190 case '?':
191 exit(EXIT_FAILURE);
192
193 default:
194 abort();
195 }
196 }
197 free(short_options);
198 }
199
200 static void
201 usage(void)
202 {
203 printf("%s: OpenFlow switch management utility\n"
204 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
205 "\nFor OpenFlow switches:\n"
206 " show SWITCH show OpenFlow information\n"
207 " dump-desc SWITCH print switch description\n"
208 " dump-tables SWITCH print table stats\n"
209 " mod-port SWITCH IFACE ACT modify port behavior\n"
210 " get-frags SWITCH print fragment handling behavior\n"
211 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
212 " dump-ports SWITCH [PORT] print port statistics\n"
213 " dump-ports-desc SWITCH print port descriptions\n"
214 " dump-flows SWITCH print all flow entries\n"
215 " dump-flows SWITCH FLOW print matching FLOWs\n"
216 " dump-aggregate SWITCH print aggregate flow statistics\n"
217 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
218 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
219 " add-flow SWITCH FLOW add flow described by FLOW\n"
220 " add-flows SWITCH FILE add flows from FILE\n"
221 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
222 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
223 " replace-flows SWITCH FILE replace flows with those in FILE\n"
224 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
225 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
226 " execute ACTIONS on PACKET\n"
227 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
228 " print packets received from SWITCH\n"
229 " snoop SWITCH snoop on SWITCH and its controller\n"
230 "\nFor OpenFlow switches and controllers:\n"
231 " probe TARGET probe whether TARGET is up\n"
232 " ping TARGET [N] latency of N-byte echos\n"
233 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
234 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
235 program_name, program_name);
236 vconn_usage(true, false, false);
237 daemon_usage();
238 vlog_usage();
239 printf("\nOther options:\n"
240 " --strict use strict match for flow commands\n"
241 " --readd replace flows that haven't changed\n"
242 " -F, --flow-format=FORMAT force particular flow format\n"
243 " -P, --packet-in-format=FRMT force particular packet in format\n"
244 " -m, --more be more verbose printing OpenFlow\n"
245 " --timestamp (monitor, snoop) print timestamps\n"
246 " -t, --timeout=SECS give up after SECS seconds\n"
247 " -h, --help display this help message\n"
248 " -V, --version display version information\n");
249 exit(EXIT_SUCCESS);
250 }
251
252 static void
253 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
254 const char *argv[] OVS_UNUSED, void *exiting_)
255 {
256 bool *exiting = exiting_;
257 *exiting = true;
258 unixctl_command_reply(conn, NULL);
259 }
260
261 static void run(int retval, const char *message, ...)
262 PRINTF_FORMAT(2, 3);
263
264 static void run(int retval, const char *message, ...)
265 {
266 if (retval) {
267 va_list args;
268
269 va_start(args, message);
270 ovs_fatal_valist(retval, message, args);
271 }
272 }
273 \f
274 /* Generic commands. */
275
276 static void
277 open_vconn_socket(const char *name, struct vconn **vconnp)
278 {
279 char *vconn_name = xasprintf("unix:%s", name);
280 VLOG_DBG("connecting to %s", vconn_name);
281 run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp),
282 "connecting to %s", vconn_name);
283 free(vconn_name);
284 }
285
286 static enum ofputil_protocol
287 open_vconn__(const char *name, const char *default_suffix,
288 struct vconn **vconnp)
289 {
290 char *datapath_name, *datapath_type, *socket_name;
291 enum ofputil_protocol protocol;
292 char *bridge_path;
293 int ofp_version;
294 struct stat s;
295
296 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
297
298 ofproto_parse_name(name, &datapath_name, &datapath_type);
299 socket_name = xasprintf("%s/%s.%s",
300 ovs_rundir(), datapath_name, default_suffix);
301 free(datapath_name);
302 free(datapath_type);
303
304 if (strchr(name, ':')) {
305 run(vconn_open_block(name, OFP10_VERSION, vconnp),
306 "connecting to %s", name);
307 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
308 open_vconn_socket(name, vconnp);
309 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
310 open_vconn_socket(bridge_path, vconnp);
311 } else if (!stat(socket_name, &s)) {
312 if (!S_ISSOCK(s.st_mode)) {
313 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
314 name, socket_name);
315 }
316 open_vconn_socket(socket_name, vconnp);
317 } else {
318 ovs_fatal(0, "%s is not a bridge or a socket", name);
319 }
320
321 free(bridge_path);
322 free(socket_name);
323
324 ofp_version = vconn_get_version(*vconnp);
325 protocol = ofputil_protocol_from_ofp_version(ofp_version);
326 if (!protocol) {
327 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
328 name, ofp_version);
329 }
330 return protocol;
331 }
332
333 static enum ofputil_protocol
334 open_vconn(const char *name, struct vconn **vconnp)
335 {
336 return open_vconn__(name, "mgmt", vconnp);
337 }
338
339 static void *
340 alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
341 {
342 struct ofp_stats_msg *rq;
343
344 rq = make_openflow(rq_len, OFPT10_STATS_REQUEST, bufferp);
345 rq->type = htons(type);
346 rq->flags = htons(0);
347 return rq;
348 }
349
350 static void
351 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
352 {
353 update_openflow_length(buffer);
354 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
355 }
356
357 static void
358 dump_transaction(const char *vconn_name, struct ofpbuf *request)
359 {
360 struct vconn *vconn;
361 struct ofpbuf *reply;
362
363 update_openflow_length(request);
364 open_vconn(vconn_name, &vconn);
365 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
366 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
367 ofpbuf_delete(reply);
368 vconn_close(vconn);
369 }
370
371 static void
372 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
373 {
374 struct ofpbuf *request;
375 make_openflow(sizeof(struct ofp_header), request_type, &request);
376 dump_transaction(vconn_name, request);
377 }
378
379 static void
380 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
381 {
382 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
383 ovs_be16 stats_type = ((struct ofp_stats_msg *) request->data)->type;
384 struct vconn *vconn;
385 bool done = false;
386
387 open_vconn(vconn_name, &vconn);
388 send_openflow_buffer(vconn, request);
389 while (!done) {
390 ovs_be32 recv_xid;
391 struct ofpbuf *reply;
392
393 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
394 recv_xid = ((struct ofp_header *) reply->data)->xid;
395 if (send_xid == recv_xid) {
396 const struct ofp_stats_msg *osm = reply->data;
397 const struct ofp_header *oh = reply->data;
398
399 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
400
401 if (oh->type == OFPT_ERROR) {
402 done = true;
403 } else if (oh->type == OFPT10_STATS_REPLY
404 && osm->type == stats_type) {
405 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
406 } else {
407 ovs_fatal(0, "received bad reply: %s",
408 ofp_to_string(reply->data, reply->size,
409 verbosity + 1));
410 }
411 } else {
412 VLOG_DBG("received reply with xid %08"PRIx32" "
413 "!= expected %08"PRIx32, recv_xid, send_xid);
414 }
415 ofpbuf_delete(reply);
416 }
417 vconn_close(vconn);
418 }
419
420 static void
421 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
422 {
423 struct ofpbuf *request;
424 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
425 dump_stats_transaction(vconn_name, request);
426 }
427
428 /* Sends 'request', which should be a request that only has a reply if an error
429 * occurs, and waits for it to succeed or fail. If an error does occur, prints
430 * it and exits with an error.
431 *
432 * Destroys all of the 'requests'. */
433 static void
434 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
435 {
436 struct ofpbuf *request, *reply;
437
438 LIST_FOR_EACH (request, list_node, requests) {
439 update_openflow_length(request);
440 }
441
442 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
443 "talking to %s", vconn_get_name(vconn));
444 if (reply) {
445 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
446 exit(1);
447 }
448 ofpbuf_delete(reply);
449 }
450
451 /* Sends 'request', which should be a request that only has a reply if an error
452 * occurs, and waits for it to succeed or fail. If an error does occur, prints
453 * it and exits with an error.
454 *
455 * Destroys 'request'. */
456 static void
457 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
458 {
459 struct list requests;
460
461 list_init(&requests);
462 list_push_back(&requests, &request->list_node);
463 transact_multiple_noreply(vconn, &requests);
464 }
465
466 static void
467 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
468 {
469 struct ofp_switch_config *config;
470 struct ofp_header *header;
471 struct ofpbuf *request;
472 struct ofpbuf *reply;
473
474 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
475 &request);
476 run(vconn_transact(vconn, request, &reply),
477 "talking to %s", vconn_get_name(vconn));
478
479 header = reply->data;
480 if (header->type != OFPT_GET_CONFIG_REPLY ||
481 header->length != htons(sizeof *config)) {
482 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
483 }
484
485 config = reply->data;
486 *config_ = *config;
487
488 ofpbuf_delete(reply);
489 }
490
491 static void
492 set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
493 {
494 struct ofp_switch_config *config;
495 struct ofp_header save_header;
496 struct ofpbuf *request;
497
498 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
499 save_header = config->header;
500 *config = *config_;
501 config->header = save_header;
502
503 transact_noreply(vconn, request);
504 }
505
506 static void
507 ofctl_show(int argc OVS_UNUSED, char *argv[])
508 {
509 const char *vconn_name = argv[1];
510 struct vconn *vconn;
511 struct ofpbuf *request;
512 struct ofpbuf *reply;
513 bool trunc;
514
515 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST,
516 &request);
517 open_vconn(vconn_name, &vconn);
518 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
519
520 trunc = ofputil_switch_features_ports_trunc(reply);
521 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
522
523 ofpbuf_delete(reply);
524 vconn_close(vconn);
525
526 if (trunc) {
527 /* The Features Reply may not contain all the ports, so send a
528 * Port Description stats request, which doesn't have size
529 * constraints. */
530 dump_trivial_stats_transaction(vconn_name, OFPST_PORT_DESC);
531 }
532 dump_trivial_transaction(vconn_name, OFPT_GET_CONFIG_REQUEST);
533 }
534
535 static void
536 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
537 {
538 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
539 }
540
541 static void
542 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
543 {
544 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
545 }
546
547 static bool
548 fetch_port_by_features(const char *vconn_name,
549 const char *port_name, unsigned int port_no,
550 struct ofputil_phy_port *pp, bool *trunc)
551 {
552 struct ofputil_switch_features features;
553 const struct ofp_switch_features *osf;
554 struct ofpbuf *request, *reply;
555 struct vconn *vconn;
556 enum ofperr error;
557 struct ofpbuf b;
558 bool found = false;
559
560 /* Fetch the switch's ofp_switch_features. */
561 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
562 open_vconn(vconn_name, &vconn);
563 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
564 vconn_close(vconn);
565
566 osf = reply->data;
567 if (reply->size < sizeof *osf) {
568 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
569 vconn_name, reply->size);
570 }
571
572 *trunc = false;
573 if (ofputil_switch_features_ports_trunc(reply)) {
574 *trunc = true;
575 goto exit;
576 }
577
578 error = ofputil_decode_switch_features(osf, &features, &b);
579 if (error) {
580 ovs_fatal(0, "%s: failed to decode features reply (%s)",
581 vconn_name, ofperr_to_string(error));
582 }
583
584 while (!ofputil_pull_phy_port(osf->header.version, &b, pp)) {
585 if (port_no != UINT_MAX
586 ? port_no == pp->port_no
587 : !strcmp(pp->name, port_name)) {
588 found = true;
589 goto exit;
590 }
591 }
592
593 exit:
594 ofpbuf_delete(reply);
595 return found;
596 }
597
598 static bool
599 fetch_port_by_stats(const char *vconn_name,
600 const char *port_name, unsigned int port_no,
601 struct ofputil_phy_port *pp)
602 {
603 struct ofpbuf *request;
604 struct vconn *vconn;
605 ovs_be32 send_xid;
606 struct ofpbuf b;
607 bool done = false;
608 bool found = false;
609
610 alloc_stats_request(sizeof(struct ofp_stats_msg), OFPST_PORT_DESC,
611 &request);
612 send_xid = ((struct ofp_header *) request->data)->xid;
613
614 open_vconn(vconn_name, &vconn);
615 send_openflow_buffer(vconn, request);
616 while (!done) {
617 ovs_be32 recv_xid;
618 struct ofpbuf *reply;
619
620 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
621 recv_xid = ((struct ofp_header *) reply->data)->xid;
622 if (send_xid == recv_xid) {
623 const struct ofputil_msg_type *type;
624 struct ofp_stats_msg *osm;
625
626 ofputil_decode_msg_type(reply->data, &type);
627 if (ofputil_msg_type_code(type) != OFPUTIL_OFPST_PORT_DESC_REPLY) {
628 ovs_fatal(0, "received bad reply: %s",
629 ofp_to_string(reply->data, reply->size,
630 verbosity + 1));
631 }
632
633 osm = ofpbuf_at_assert(reply, 0, sizeof *osm);
634 done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
635
636 if (found) {
637 /* We've already found the port, but we need to drain
638 * the queue of any other replies for this request. */
639 continue;
640 }
641
642 ofpbuf_use_const(&b, &osm->header, ntohs(osm->header.length));
643 ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
644
645 while (!ofputil_pull_phy_port(osm->header.version, &b, pp)) {
646 if (port_no != UINT_MAX ? port_no == pp->port_no
647 : !strcmp(pp->name, port_name)) {
648 found = true;
649 break;
650 }
651 }
652 } else {
653 VLOG_DBG("received reply with xid %08"PRIx32" "
654 "!= expected %08"PRIx32, recv_xid, send_xid);
655 }
656 ofpbuf_delete(reply);
657 }
658 vconn_close(vconn);
659
660 return found;
661 }
662
663
664 /* Opens a connection to 'vconn_name', fetches the port structure for
665 * 'port_name' (which may be a port name or number), and copies it into
666 * '*pp'. */
667 static void
668 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
669 struct ofputil_phy_port *pp)
670 {
671 unsigned int port_no;
672 bool found;
673 bool trunc;
674
675 /* Try to interpret the argument as a port number. */
676 if (!str_to_uint(port_name, 10, &port_no)) {
677 port_no = UINT_MAX;
678 }
679
680 /* Try to find the port based on the Features Reply. If it looks
681 * like the results may be truncated, then use the Port Description
682 * stats message introduced in OVS 1.7. */
683 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
684 &trunc);
685 if (trunc) {
686 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
687 }
688
689 if (!found) {
690 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
691 }
692 }
693
694 /* Returns the port number corresponding to 'port_name' (which may be a port
695 * name or number) within the switch 'vconn_name'. */
696 static uint16_t
697 str_to_port_no(const char *vconn_name, const char *port_name)
698 {
699 unsigned int port_no;
700
701 if (str_to_uint(port_name, 10, &port_no)) {
702 return port_no;
703 } else {
704 struct ofputil_phy_port pp;
705
706 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
707 return pp.port_no;
708 }
709 }
710
711 static bool
712 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
713 enum ofputil_protocol *cur)
714 {
715 for (;;) {
716 struct ofpbuf *request, *reply;
717 enum ofputil_protocol next;
718
719 request = ofputil_encode_set_protocol(*cur, want, &next);
720 if (!request) {
721 return true;
722 }
723
724 run(vconn_transact_noreply(vconn, request, &reply),
725 "talking to %s", vconn_get_name(vconn));
726 if (reply) {
727 char *s = ofp_to_string(reply->data, reply->size, 2);
728 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
729 vconn_get_name(vconn), s);
730 free(s);
731 ofpbuf_delete(reply);
732 return false;
733 }
734
735 *cur = next;
736 }
737 }
738
739 static enum ofputil_protocol
740 set_protocol_for_flow_dump(struct vconn *vconn,
741 enum ofputil_protocol cur_protocol,
742 enum ofputil_protocol usable_protocols)
743 {
744 char *usable_s;
745 int i;
746
747 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
748 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
749 if (f & usable_protocols & allowed_protocols
750 && try_set_protocol(vconn, f, &cur_protocol)) {
751 return f;
752 }
753 }
754
755 usable_s = ofputil_protocols_to_string(usable_protocols);
756 if (usable_protocols & allowed_protocols) {
757 ovs_fatal(0, "switch does not support any of the usable flow "
758 "formats (%s)", usable_s);
759 } else {
760 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
761 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
762 "allowed flow formats (%s)", usable_s, allowed_s);
763 }
764 }
765
766 static void
767 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
768 {
769 enum ofputil_protocol usable_protocols, protocol;
770 struct ofputil_flow_stats_request fsr;
771 struct ofpbuf *request;
772 struct vconn *vconn;
773
774 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
775 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
776
777 protocol = open_vconn(argv[1], &vconn);
778 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
779 request = ofputil_encode_flow_stats_request(&fsr, protocol);
780 dump_stats_transaction(argv[1], request);
781 vconn_close(vconn);
782 }
783
784 static void
785 ofctl_dump_flows(int argc, char *argv[])
786 {
787 return ofctl_dump_flows__(argc, argv, false);
788 }
789
790 static void
791 ofctl_dump_aggregate(int argc, char *argv[])
792 {
793 return ofctl_dump_flows__(argc, argv, true);
794 }
795
796 static void
797 ofctl_queue_stats(int argc, char *argv[])
798 {
799 struct ofp_queue_stats_request *req;
800 struct ofpbuf *request;
801
802 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
803
804 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
805 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
806 } else {
807 req->port_no = htons(OFPP_ALL);
808 }
809 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
810 req->queue_id = htonl(atoi(argv[3]));
811 } else {
812 req->queue_id = htonl(OFPQ_ALL);
813 }
814
815 memset(req->pad, 0, sizeof req->pad);
816
817 dump_stats_transaction(argv[1], request);
818 }
819
820 static enum ofputil_protocol
821 open_vconn_for_flow_mod(const char *remote,
822 const struct ofputil_flow_mod *fms, size_t n_fms,
823 struct vconn **vconnp)
824 {
825 enum ofputil_protocol usable_protocols;
826 enum ofputil_protocol cur_protocol;
827 char *usable_s;
828 int i;
829
830 /* Figure out what flow formats will work. */
831 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
832 if (!(usable_protocols & allowed_protocols)) {
833 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
834 usable_s = ofputil_protocols_to_string(usable_protocols);
835 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
836 "allowed flow formats (%s)", usable_s, allowed_s);
837 }
838
839 /* If the initial flow format is allowed and usable, keep it. */
840 cur_protocol = open_vconn(remote, vconnp);
841 if (usable_protocols & allowed_protocols & cur_protocol) {
842 return cur_protocol;
843 }
844
845 /* Otherwise try each flow format in turn. */
846 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
847 enum ofputil_protocol f = 1 << i;
848
849 if (f != cur_protocol
850 && f & usable_protocols & allowed_protocols
851 && try_set_protocol(*vconnp, f, &cur_protocol)) {
852 return f;
853 }
854 }
855
856 usable_s = ofputil_protocols_to_string(usable_protocols);
857 ovs_fatal(0, "switch does not support any of the usable flow "
858 "formats (%s)", usable_s);
859 }
860
861 static void
862 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
863 size_t n_fms)
864 {
865 enum ofputil_protocol protocol;
866 struct vconn *vconn;
867 size_t i;
868
869 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
870
871 for (i = 0; i < n_fms; i++) {
872 struct ofputil_flow_mod *fm = &fms[i];
873
874 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
875 free(fm->ofpacts);
876 }
877 vconn_close(vconn);
878 }
879
880 static void
881 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
882 {
883 struct ofputil_flow_mod *fms = NULL;
884 size_t n_fms = 0;
885
886 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
887 ofctl_flow_mod__(argv[1], fms, n_fms);
888 free(fms);
889 }
890
891 static void
892 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
893 {
894 if (argc > 2 && !strcmp(argv[2], "-")) {
895 ofctl_flow_mod_file(argc, argv, command);
896 } else {
897 struct ofputil_flow_mod fm;
898 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
899 ofctl_flow_mod__(argv[1], &fm, 1);
900 }
901 }
902
903 static void
904 ofctl_add_flow(int argc, char *argv[])
905 {
906 ofctl_flow_mod(argc, argv, OFPFC_ADD);
907 }
908
909 static void
910 ofctl_add_flows(int argc, char *argv[])
911 {
912 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
913 }
914
915 static void
916 ofctl_mod_flows(int argc, char *argv[])
917 {
918 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
919 }
920
921 static void
922 ofctl_del_flows(int argc, char *argv[])
923 {
924 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
925 }
926
927 static void
928 set_packet_in_format(struct vconn *vconn,
929 enum nx_packet_in_format packet_in_format)
930 {
931 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
932 transact_noreply(vconn, spif);
933 VLOG_DBG("%s: using user-specified packet in format %s",
934 vconn_get_name(vconn),
935 ofputil_packet_in_format_to_string(packet_in_format));
936 }
937
938 static int
939 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
940 {
941 struct ofp_switch_config config;
942 enum ofp_config_flags flags;
943
944 fetch_switch_config(vconn, &config);
945 flags = ntohs(config.flags);
946 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
947 /* Set the invalid ttl config. */
948 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
949
950 config.flags = htons(flags);
951 set_switch_config(vconn, &config);
952
953 /* Then retrieve the configuration to see if it really took. OpenFlow
954 * doesn't define error reporting for bad modes, so this is all we can
955 * do. */
956 fetch_switch_config(vconn, &config);
957 flags = ntohs(config.flags);
958 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
959 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
960 "switch probably doesn't support mode)");
961 return -EOPNOTSUPP;
962 }
963 }
964 return 0;
965 }
966
967 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
968 * caller must free '*msgp'. On success, returns NULL. On failure, returns
969 * an error message and stores NULL in '*msgp'. */
970 static const char *
971 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
972 {
973 struct ofp_header *oh;
974 struct ofpbuf *msg;
975
976 msg = ofpbuf_new(strlen(hex) / 2);
977 *msgp = NULL;
978
979 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
980 ofpbuf_delete(msg);
981 return "Trailing garbage in hex data";
982 }
983
984 if (msg->size < sizeof(struct ofp_header)) {
985 ofpbuf_delete(msg);
986 return "Message too short for OpenFlow";
987 }
988
989 oh = msg->data;
990 if (msg->size != ntohs(oh->length)) {
991 ofpbuf_delete(msg);
992 return "Message size does not match length in OpenFlow header";
993 }
994
995 *msgp = msg;
996 return NULL;
997 }
998
999 static void
1000 ofctl_send(struct unixctl_conn *conn, int argc,
1001 const char *argv[], void *vconn_)
1002 {
1003 struct vconn *vconn = vconn_;
1004 struct ds reply;
1005 bool ok;
1006 int i;
1007
1008 ok = true;
1009 ds_init(&reply);
1010 for (i = 1; i < argc; i++) {
1011 const char *error_msg;
1012 struct ofpbuf *msg;
1013 int error;
1014
1015 error_msg = openflow_from_hex(argv[i], &msg);
1016 if (error_msg) {
1017 ds_put_format(&reply, "%s\n", error_msg);
1018 ok = false;
1019 continue;
1020 }
1021
1022 fprintf(stderr, "send: ");
1023 ofp_print(stderr, msg->data, msg->size, verbosity);
1024
1025 error = vconn_send_block(vconn, msg);
1026 if (error) {
1027 ofpbuf_delete(msg);
1028 ds_put_format(&reply, "%s\n", strerror(error));
1029 ok = false;
1030 } else {
1031 ds_put_cstr(&reply, "sent\n");
1032 }
1033 }
1034
1035 if (ok) {
1036 unixctl_command_reply(conn, ds_cstr(&reply));
1037 } else {
1038 unixctl_command_reply_error(conn, ds_cstr(&reply));
1039 }
1040 ds_destroy(&reply);
1041 }
1042
1043 struct barrier_aux {
1044 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1045 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1046 };
1047
1048 static void
1049 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1050 const char *argv[] OVS_UNUSED, void *aux_)
1051 {
1052 struct barrier_aux *aux = aux_;
1053 struct ofpbuf *msg;
1054 int error;
1055
1056 if (aux->conn) {
1057 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1058 return;
1059 }
1060
1061 msg = ofputil_encode_barrier_request();
1062 error = vconn_send_block(aux->vconn, msg);
1063 if (error) {
1064 ofpbuf_delete(msg);
1065 unixctl_command_reply_error(conn, strerror(error));
1066 } else {
1067 aux->conn = conn;
1068 }
1069 }
1070
1071 static void
1072 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1073 const char *argv[], void *aux OVS_UNUSED)
1074 {
1075 int fd;
1076
1077 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1078 if (fd < 0) {
1079 unixctl_command_reply_error(conn, strerror(errno));
1080 return;
1081 }
1082
1083 fflush(stderr);
1084 dup2(fd, STDERR_FILENO);
1085 close(fd);
1086 unixctl_command_reply(conn, NULL);
1087 }
1088
1089 static void
1090 monitor_vconn(struct vconn *vconn)
1091 {
1092 struct barrier_aux barrier_aux = { vconn, NULL };
1093 struct unixctl_server *server;
1094 bool exiting = false;
1095 int error;
1096
1097 daemon_save_fd(STDERR_FILENO);
1098 daemonize_start();
1099 error = unixctl_server_create(NULL, &server);
1100 if (error) {
1101 ovs_fatal(error, "failed to create unixctl server");
1102 }
1103 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1104 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1105 ofctl_send, vconn);
1106 unixctl_command_register("ofctl/barrier", "", 0, 0,
1107 ofctl_barrier, &barrier_aux);
1108 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1109 ofctl_set_output_file, NULL);
1110 daemonize_complete();
1111
1112 for (;;) {
1113 struct ofpbuf *b;
1114 int retval;
1115
1116 unixctl_server_run(server);
1117
1118 for (;;) {
1119 uint8_t msg_type;
1120
1121 retval = vconn_recv(vconn, &b);
1122 if (retval == EAGAIN) {
1123 break;
1124 }
1125 run(retval, "vconn_recv");
1126
1127 if (timestamp) {
1128 time_t now = time_wall();
1129 char s[32];
1130
1131 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now));
1132 fputs(s, stderr);
1133 }
1134
1135 msg_type = ((const struct ofp_header *) b->data)->type;
1136 ofp_print(stderr, b->data, b->size, verbosity + 2);
1137 ofpbuf_delete(b);
1138
1139 if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) {
1140 unixctl_command_reply(barrier_aux.conn, NULL);
1141 barrier_aux.conn = NULL;
1142 }
1143 }
1144
1145 if (exiting) {
1146 break;
1147 }
1148
1149 vconn_run(vconn);
1150 vconn_run_wait(vconn);
1151 vconn_recv_wait(vconn);
1152 unixctl_server_wait(server);
1153 poll_block();
1154 }
1155 vconn_close(vconn);
1156 unixctl_server_destroy(server);
1157 }
1158
1159 static void
1160 ofctl_monitor(int argc, char *argv[])
1161 {
1162 struct vconn *vconn;
1163
1164 open_vconn(argv[1], &vconn);
1165 if (argc > 2) {
1166 struct ofp_switch_config config;
1167
1168 fetch_switch_config(vconn, &config);
1169 config.miss_send_len = htons(atoi(argv[2]));
1170 set_switch_config(vconn, &config);
1171 }
1172 if (argc > 3) {
1173 if (!strcmp(argv[3], "invalid_ttl")) {
1174 monitor_set_invalid_ttl_to_controller(vconn);
1175 }
1176 }
1177 if (preferred_packet_in_format >= 0) {
1178 set_packet_in_format(vconn, preferred_packet_in_format);
1179 } else {
1180 struct ofpbuf *spif, *reply;
1181
1182 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1183 run(vconn_transact_noreply(vconn, spif, &reply),
1184 "talking to %s", vconn_get_name(vconn));
1185 if (reply) {
1186 char *s = ofp_to_string(reply->data, reply->size, 2);
1187 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1188 " replied: %s. Falling back to the switch default.",
1189 vconn_get_name(vconn), s);
1190 free(s);
1191 ofpbuf_delete(reply);
1192 }
1193 }
1194
1195 monitor_vconn(vconn);
1196 }
1197
1198 static void
1199 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1200 {
1201 struct vconn *vconn;
1202
1203 open_vconn__(argv[1], "snoop", &vconn);
1204 monitor_vconn(vconn);
1205 }
1206
1207 static void
1208 ofctl_dump_ports(int argc, char *argv[])
1209 {
1210 struct ofp_port_stats_request *req;
1211 struct ofpbuf *request;
1212 uint16_t port;
1213
1214 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1215 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1216 req->port_no = htons(port);
1217 dump_stats_transaction(argv[1], request);
1218 }
1219
1220 static void
1221 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1222 {
1223 dump_trivial_stats_transaction(argv[1], OFPST_PORT_DESC);
1224 }
1225
1226 static void
1227 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1228 {
1229 struct ofpbuf *request;
1230 struct vconn *vconn;
1231 struct ofpbuf *reply;
1232
1233 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1234 open_vconn(argv[1], &vconn);
1235 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1236 if (reply->size != sizeof(struct ofp_header)) {
1237 ovs_fatal(0, "reply does not match request");
1238 }
1239 ofpbuf_delete(reply);
1240 vconn_close(vconn);
1241 }
1242
1243 static void
1244 ofctl_packet_out(int argc, char *argv[])
1245 {
1246 struct ofputil_packet_out po;
1247 struct ofpbuf ofpacts;
1248 struct vconn *vconn;
1249 int i;
1250
1251 ofpbuf_init(&ofpacts, 64);
1252 parse_ofpacts(argv[3], &ofpacts);
1253
1254 po.buffer_id = UINT32_MAX;
1255 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1256 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1257 : str_to_port_no(argv[1], argv[2]));
1258 po.ofpacts = ofpacts.data;
1259 po.ofpacts_len = ofpacts.size;
1260
1261 open_vconn(argv[1], &vconn);
1262 for (i = 4; i < argc; i++) {
1263 struct ofpbuf *packet, *opo;
1264 const char *error_msg;
1265
1266 error_msg = eth_from_hex(argv[i], &packet);
1267 if (error_msg) {
1268 ovs_fatal(0, "%s", error_msg);
1269 }
1270
1271 po.packet = packet->data;
1272 po.packet_len = packet->size;
1273 opo = ofputil_encode_packet_out(&po);
1274 transact_noreply(vconn, opo);
1275 ofpbuf_delete(packet);
1276 }
1277 vconn_close(vconn);
1278 ofpbuf_uninit(&ofpacts);
1279 }
1280
1281 static void
1282 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1283 {
1284 struct ofp_config_flag {
1285 const char *name; /* The flag's name. */
1286 enum ofputil_port_config bit; /* Bit to turn on or off. */
1287 bool on; /* Value to set the bit to. */
1288 };
1289 static const struct ofp_config_flag flags[] = {
1290 { "up", OFPUTIL_PC_PORT_DOWN, false },
1291 { "down", OFPUTIL_PC_PORT_DOWN, true },
1292 { "stp", OFPUTIL_PC_NO_STP, false },
1293 { "receive", OFPUTIL_PC_NO_RECV, false },
1294 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1295 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1296 { "forward", OFPUTIL_PC_NO_FWD, false },
1297 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1298 };
1299
1300 const struct ofp_config_flag *flag;
1301 enum ofputil_protocol protocol;
1302 struct ofputil_port_mod pm;
1303 struct ofputil_phy_port pp;
1304 struct vconn *vconn;
1305 const char *command;
1306 bool not;
1307
1308 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1309
1310 pm.port_no = pp.port_no;
1311 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1312 pm.config = 0;
1313 pm.mask = 0;
1314 pm.advertise = 0;
1315
1316 if (!strncasecmp(argv[3], "no-", 3)) {
1317 command = argv[3] + 3;
1318 not = true;
1319 } else if (!strncasecmp(argv[3], "no", 2)) {
1320 command = argv[3] + 2;
1321 not = true;
1322 } else {
1323 command = argv[3];
1324 not = false;
1325 }
1326 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1327 if (!strcasecmp(command, flag->name)) {
1328 pm.mask = flag->bit;
1329 pm.config = flag->on ^ not ? flag->bit : 0;
1330 goto found;
1331 }
1332 }
1333 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1334
1335 found:
1336 protocol = open_vconn(argv[1], &vconn);
1337 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1338 vconn_close(vconn);
1339 }
1340
1341 static void
1342 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1343 {
1344 struct ofp_switch_config config;
1345 struct vconn *vconn;
1346
1347 open_vconn(argv[1], &vconn);
1348 fetch_switch_config(vconn, &config);
1349 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1350 vconn_close(vconn);
1351 }
1352
1353 static void
1354 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1355 {
1356 struct ofp_switch_config config;
1357 enum ofp_config_flags mode;
1358 struct vconn *vconn;
1359 ovs_be16 flags;
1360
1361 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1362 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1363 }
1364
1365 open_vconn(argv[1], &vconn);
1366 fetch_switch_config(vconn, &config);
1367 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1368 if (flags != config.flags) {
1369 /* Set the configuration. */
1370 config.flags = flags;
1371 set_switch_config(vconn, &config);
1372
1373 /* Then retrieve the configuration to see if it really took. OpenFlow
1374 * doesn't define error reporting for bad modes, so this is all we can
1375 * do. */
1376 fetch_switch_config(vconn, &config);
1377 if (flags != config.flags) {
1378 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1379 "switch probably doesn't support mode \"%s\")",
1380 argv[1], ofputil_frag_handling_to_string(mode));
1381 }
1382 }
1383 vconn_close(vconn);
1384 }
1385
1386 static void
1387 ofctl_ping(int argc, char *argv[])
1388 {
1389 size_t max_payload = 65535 - sizeof(struct ofp_header);
1390 unsigned int payload;
1391 struct vconn *vconn;
1392 int i;
1393
1394 payload = argc > 2 ? atoi(argv[2]) : 64;
1395 if (payload > max_payload) {
1396 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1397 }
1398
1399 open_vconn(argv[1], &vconn);
1400 for (i = 0; i < 10; i++) {
1401 struct timeval start, end;
1402 struct ofpbuf *request, *reply;
1403 struct ofp_header *rq_hdr, *rpy_hdr;
1404
1405 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1406 OFPT_ECHO_REQUEST, &request);
1407 random_bytes(rq_hdr + 1, payload);
1408
1409 xgettimeofday(&start);
1410 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1411 xgettimeofday(&end);
1412
1413 rpy_hdr = reply->data;
1414 if (reply->size != request->size
1415 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1416 || rpy_hdr->xid != rq_hdr->xid
1417 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1418 printf("Reply does not match request. Request:\n");
1419 ofp_print(stdout, request, request->size, verbosity + 2);
1420 printf("Reply:\n");
1421 ofp_print(stdout, reply, reply->size, verbosity + 2);
1422 }
1423 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1424 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
1425 (1000*(double)(end.tv_sec - start.tv_sec))
1426 + (.001*(end.tv_usec - start.tv_usec)));
1427 ofpbuf_delete(request);
1428 ofpbuf_delete(reply);
1429 }
1430 vconn_close(vconn);
1431 }
1432
1433 static void
1434 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
1435 {
1436 size_t max_payload = 65535 - sizeof(struct ofp_header);
1437 struct timeval start, end;
1438 unsigned int payload_size, message_size;
1439 struct vconn *vconn;
1440 double duration;
1441 int count;
1442 int i;
1443
1444 payload_size = atoi(argv[2]);
1445 if (payload_size > max_payload) {
1446 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1447 }
1448 message_size = sizeof(struct ofp_header) + payload_size;
1449
1450 count = atoi(argv[3]);
1451
1452 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1453 count, message_size, count * message_size);
1454
1455 open_vconn(argv[1], &vconn);
1456 xgettimeofday(&start);
1457 for (i = 0; i < count; i++) {
1458 struct ofpbuf *request, *reply;
1459 struct ofp_header *rq_hdr;
1460
1461 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1462 memset(rq_hdr + 1, 0, payload_size);
1463 run(vconn_transact(vconn, request, &reply), "transact");
1464 ofpbuf_delete(reply);
1465 }
1466 xgettimeofday(&end);
1467 vconn_close(vconn);
1468
1469 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1470 + (.001*(end.tv_usec - start.tv_usec)));
1471 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1472 duration, count / (duration / 1000.0),
1473 count * message_size / (duration / 1000.0));
1474 }
1475
1476 static void
1477 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1478 {
1479 usage();
1480 }
1481 \f
1482 /* replace-flows and diff-flows commands. */
1483
1484 /* A flow table entry, possibly with two different versions. */
1485 struct fte {
1486 struct cls_rule rule; /* Within a "struct classifier". */
1487 struct fte_version *versions[2];
1488 };
1489
1490 /* One version of a Flow Table Entry. */
1491 struct fte_version {
1492 ovs_be64 cookie;
1493 uint16_t idle_timeout;
1494 uint16_t hard_timeout;
1495 uint16_t flags;
1496 struct ofpact *ofpacts;
1497 size_t ofpacts_len;
1498 };
1499
1500 /* Frees 'version' and the data that it owns. */
1501 static void
1502 fte_version_free(struct fte_version *version)
1503 {
1504 if (version) {
1505 free(version->ofpacts);
1506 free(version);
1507 }
1508 }
1509
1510 /* Returns true if 'a' and 'b' are the same, false if they differ.
1511 *
1512 * Ignores differences in 'flags' because there's no way to retrieve flags from
1513 * an OpenFlow switch. We have to assume that they are the same. */
1514 static bool
1515 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1516 {
1517 return (a->cookie == b->cookie
1518 && a->idle_timeout == b->idle_timeout
1519 && a->hard_timeout == b->hard_timeout
1520 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
1521 b->ofpacts, b->ofpacts_len));
1522 }
1523
1524 /* Prints 'version' on stdout. Expects the caller to have printed the rule
1525 * associated with the version. */
1526 static void
1527 fte_version_print(const struct fte_version *version)
1528 {
1529 struct ds s;
1530
1531 if (version->cookie != htonll(0)) {
1532 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1533 }
1534 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1535 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1536 }
1537 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1538 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1539 }
1540
1541 ds_init(&s);
1542 ofpacts_format(version->ofpacts, version->ofpacts_len, &s);
1543 printf(" %s\n", ds_cstr(&s));
1544 ds_destroy(&s);
1545 }
1546
1547 static struct fte *
1548 fte_from_cls_rule(const struct cls_rule *cls_rule)
1549 {
1550 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1551 }
1552
1553 /* Frees 'fte' and its versions. */
1554 static void
1555 fte_free(struct fte *fte)
1556 {
1557 if (fte) {
1558 fte_version_free(fte->versions[0]);
1559 fte_version_free(fte->versions[1]);
1560 free(fte);
1561 }
1562 }
1563
1564 /* Frees all of the FTEs within 'cls'. */
1565 static void
1566 fte_free_all(struct classifier *cls)
1567 {
1568 struct cls_cursor cursor;
1569 struct fte *fte, *next;
1570
1571 cls_cursor_init(&cursor, cls, NULL);
1572 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1573 classifier_remove(cls, &fte->rule);
1574 fte_free(fte);
1575 }
1576 classifier_destroy(cls);
1577 }
1578
1579 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1580 * necessary. Sets 'version' as the version of that rule with the given
1581 * 'index', replacing any existing version, if any.
1582 *
1583 * Takes ownership of 'version'. */
1584 static void
1585 fte_insert(struct classifier *cls, const struct cls_rule *rule,
1586 struct fte_version *version, int index)
1587 {
1588 struct fte *old, *fte;
1589
1590 fte = xzalloc(sizeof *fte);
1591 fte->rule = *rule;
1592 fte->versions[index] = version;
1593
1594 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
1595 if (old) {
1596 fte_version_free(old->versions[index]);
1597 fte->versions[!index] = old->versions[!index];
1598 free(old);
1599 }
1600 }
1601
1602 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1603 * with the specified 'index'. Returns the flow formats able to represent the
1604 * flows that were read. */
1605 static enum ofputil_protocol
1606 read_flows_from_file(const char *filename, struct classifier *cls, int index)
1607 {
1608 enum ofputil_protocol usable_protocols;
1609 struct ds s;
1610 FILE *file;
1611
1612 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1613 if (file == NULL) {
1614 ovs_fatal(errno, "%s: open", filename);
1615 }
1616
1617 ds_init(&s);
1618 usable_protocols = OFPUTIL_P_ANY;
1619 while (!ds_get_preprocessed_line(&s, file)) {
1620 struct fte_version *version;
1621 struct ofputil_flow_mod fm;
1622
1623 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
1624
1625 version = xmalloc(sizeof *version);
1626 version->cookie = fm.new_cookie;
1627 version->idle_timeout = fm.idle_timeout;
1628 version->hard_timeout = fm.hard_timeout;
1629 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1630 version->ofpacts = fm.ofpacts;
1631 version->ofpacts_len = fm.ofpacts_len;
1632
1633 usable_protocols &= ofputil_usable_protocols(&fm.cr);
1634
1635 fte_insert(cls, &fm.cr, version, index);
1636 }
1637 ds_destroy(&s);
1638
1639 if (file != stdin) {
1640 fclose(file);
1641 }
1642
1643 return usable_protocols;
1644 }
1645
1646 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1647 * format 'protocol', and adds them as flow table entries in 'cls' for the
1648 * version with the specified 'index'. */
1649 static void
1650 read_flows_from_switch(struct vconn *vconn,
1651 enum ofputil_protocol protocol,
1652 struct classifier *cls, int index)
1653 {
1654 struct ofputil_flow_stats_request fsr;
1655 struct ofpbuf *request;
1656 ovs_be32 send_xid;
1657 bool done;
1658
1659 fsr.aggregate = false;
1660 cls_rule_init_catchall(&fsr.match, 0);
1661 fsr.out_port = OFPP_NONE;
1662 fsr.table_id = 0xff;
1663 fsr.cookie = fsr.cookie_mask = htonll(0);
1664 request = ofputil_encode_flow_stats_request(&fsr, protocol);
1665 send_xid = ((struct ofp_header *) request->data)->xid;
1666 send_openflow_buffer(vconn, request);
1667
1668 done = false;
1669 while (!done) {
1670 ovs_be32 recv_xid;
1671 struct ofpbuf *reply;
1672
1673 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1674 recv_xid = ((struct ofp_header *) reply->data)->xid;
1675 if (send_xid == recv_xid) {
1676 const struct ofputil_msg_type *type;
1677 const struct ofp_stats_msg *osm;
1678 enum ofputil_msg_code code;
1679
1680 ofputil_decode_msg_type(reply->data, &type);
1681 code = ofputil_msg_type_code(type);
1682 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1683 code != OFPUTIL_NXST_FLOW_REPLY) {
1684 ovs_fatal(0, "received bad reply: %s",
1685 ofp_to_string(reply->data, reply->size,
1686 verbosity + 1));
1687 }
1688
1689 osm = reply->data;
1690 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
1691 done = true;
1692 }
1693
1694 for (;;) {
1695 struct fte_version *version;
1696 struct ofputil_flow_stats fs;
1697 struct ofpbuf ofpacts;
1698 int retval;
1699
1700 ofpbuf_init(&ofpacts, 64);
1701 retval = ofputil_decode_flow_stats_reply(&fs, reply, false,
1702 &ofpacts);
1703 if (retval) {
1704 ofpbuf_uninit(&ofpacts);
1705 if (retval != EOF) {
1706 ovs_fatal(0, "parse error in reply");
1707 }
1708 break;
1709 }
1710
1711 version = xmalloc(sizeof *version);
1712 version->cookie = fs.cookie;
1713 version->idle_timeout = fs.idle_timeout;
1714 version->hard_timeout = fs.hard_timeout;
1715 version->flags = 0;
1716 version->ofpacts = ofpbuf_steal_data(&ofpacts);
1717 version->ofpacts_len = ofpacts.size;
1718
1719 fte_insert(cls, &fs.rule, version, index);
1720 }
1721 } else {
1722 VLOG_DBG("received reply with xid %08"PRIx32" "
1723 "!= expected %08"PRIx32, recv_xid, send_xid);
1724 }
1725 ofpbuf_delete(reply);
1726 }
1727 }
1728
1729 static void
1730 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1731 enum ofputil_protocol protocol, struct list *packets)
1732 {
1733 const struct fte_version *version = fte->versions[index];
1734 struct ofputil_flow_mod fm;
1735 struct ofpbuf *ofm;
1736
1737 fm.cr = fte->rule;
1738 fm.cookie = htonll(0);
1739 fm.cookie_mask = htonll(0);
1740 fm.new_cookie = version->cookie;
1741 fm.table_id = 0xff;
1742 fm.command = command;
1743 fm.idle_timeout = version->idle_timeout;
1744 fm.hard_timeout = version->hard_timeout;
1745 fm.buffer_id = UINT32_MAX;
1746 fm.out_port = OFPP_NONE;
1747 fm.flags = version->flags;
1748 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1749 command == OFPFC_MODIFY_STRICT) {
1750 fm.ofpacts = version->ofpacts;
1751 fm.ofpacts_len = version->ofpacts_len;
1752 } else {
1753 fm.ofpacts = NULL;
1754 fm.ofpacts_len = 0;
1755 }
1756
1757 ofm = ofputil_encode_flow_mod(&fm, protocol);
1758 list_push_back(packets, &ofm->list_node);
1759 }
1760
1761 static void
1762 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
1763 {
1764 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1765 enum ofputil_protocol usable_protocols, protocol;
1766 struct cls_cursor cursor;
1767 struct classifier cls;
1768 struct list requests;
1769 struct vconn *vconn;
1770 struct fte *fte;
1771
1772 classifier_init(&cls);
1773 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
1774
1775 protocol = open_vconn(argv[1], &vconn);
1776 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1777
1778 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
1779
1780 list_init(&requests);
1781
1782 /* Delete flows that exist on the switch but not in the file. */
1783 cls_cursor_init(&cursor, &cls, NULL);
1784 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1785 struct fte_version *file_ver = fte->versions[FILE_IDX];
1786 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1787
1788 if (sw_ver && !file_ver) {
1789 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1790 protocol, &requests);
1791 }
1792 }
1793
1794 /* Add flows that exist in the file but not on the switch.
1795 * Update flows that exist in both places but differ. */
1796 cls_cursor_init(&cursor, &cls, NULL);
1797 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1798 struct fte_version *file_ver = fte->versions[FILE_IDX];
1799 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1800
1801 if (file_ver
1802 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1803 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
1804 }
1805 }
1806 transact_multiple_noreply(vconn, &requests);
1807 vconn_close(vconn);
1808
1809 fte_free_all(&cls);
1810 }
1811
1812 static void
1813 read_flows_from_source(const char *source, struct classifier *cls, int index)
1814 {
1815 struct stat s;
1816
1817 if (source[0] == '/' || source[0] == '.'
1818 || (!strchr(source, ':') && !stat(source, &s))) {
1819 read_flows_from_file(source, cls, index);
1820 } else {
1821 enum ofputil_protocol protocol;
1822 struct vconn *vconn;
1823
1824 protocol = open_vconn(source, &vconn);
1825 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
1826 read_flows_from_switch(vconn, protocol, cls, index);
1827 vconn_close(vconn);
1828 }
1829 }
1830
1831 static void
1832 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
1833 {
1834 bool differences = false;
1835 struct cls_cursor cursor;
1836 struct classifier cls;
1837 struct fte *fte;
1838
1839 classifier_init(&cls);
1840 read_flows_from_source(argv[1], &cls, 0);
1841 read_flows_from_source(argv[2], &cls, 1);
1842
1843 cls_cursor_init(&cursor, &cls, NULL);
1844 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1845 struct fte_version *a = fte->versions[0];
1846 struct fte_version *b = fte->versions[1];
1847
1848 if (!a || !b || !fte_version_equals(a, b)) {
1849 char *rule_s = cls_rule_to_string(&fte->rule);
1850 if (a) {
1851 printf("-%s", rule_s);
1852 fte_version_print(a);
1853 }
1854 if (b) {
1855 printf("+%s", rule_s);
1856 fte_version_print(b);
1857 }
1858 free(rule_s);
1859
1860 differences = true;
1861 }
1862 }
1863
1864 fte_free_all(&cls);
1865
1866 if (differences) {
1867 exit(2);
1868 }
1869 }
1870 \f
1871 /* Undocumented commands for unit testing. */
1872
1873 static void
1874 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
1875 {
1876 enum ofputil_protocol usable_protocols;
1877 enum ofputil_protocol protocol = 0;
1878 char *usable_s;
1879 size_t i;
1880
1881 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1882 usable_s = ofputil_protocols_to_string(usable_protocols);
1883 printf("usable protocols: %s\n", usable_s);
1884 free(usable_s);
1885
1886 if (!(usable_protocols & allowed_protocols)) {
1887 ovs_fatal(0, "no usable protocol");
1888 }
1889 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1890 protocol = 1 << i;
1891 if (protocol & usable_protocols & allowed_protocols) {
1892 break;
1893 }
1894 }
1895 assert(IS_POW2(protocol));
1896
1897 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
1898
1899 for (i = 0; i < n_fms; i++) {
1900 struct ofputil_flow_mod *fm = &fms[i];
1901 struct ofpbuf *msg;
1902
1903 msg = ofputil_encode_flow_mod(fm, protocol);
1904 ofp_print(stdout, msg->data, msg->size, verbosity);
1905 ofpbuf_delete(msg);
1906
1907 free(fm->ofpacts);
1908 }
1909 }
1910
1911 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1912 * it back to stdout. */
1913 static void
1914 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
1915 {
1916 struct ofputil_flow_mod fm;
1917
1918 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
1919 ofctl_parse_flows__(&fm, 1);
1920 }
1921
1922 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1923 * add-flows) and prints each of the flows back to stdout. */
1924 static void
1925 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
1926 {
1927 struct ofputil_flow_mod *fms = NULL;
1928 size_t n_fms = 0;
1929
1930 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
1931 ofctl_parse_flows__(fms, n_fms);
1932 free(fms);
1933 }
1934
1935 static void
1936 ofctl_parse_nxm__(bool oxm)
1937 {
1938 struct ds in;
1939
1940 ds_init(&in);
1941 while (!ds_get_test_line(&in, stdin)) {
1942 struct ofpbuf nx_match;
1943 struct cls_rule rule;
1944 ovs_be64 cookie, cookie_mask;
1945 enum ofperr error;
1946 int match_len;
1947
1948 /* Convert string to nx_match. */
1949 ofpbuf_init(&nx_match, 0);
1950 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1951
1952 /* Convert nx_match to cls_rule. */
1953 if (strict) {
1954 error = nx_pull_match(&nx_match, match_len, 0, &rule,
1955 &cookie, &cookie_mask);
1956 } else {
1957 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
1958 &cookie, &cookie_mask);
1959 }
1960
1961 if (!error) {
1962 char *out;
1963
1964 /* Convert cls_rule back to nx_match. */
1965 ofpbuf_uninit(&nx_match);
1966 ofpbuf_init(&nx_match, 0);
1967 match_len = nx_put_match(&nx_match, oxm, &rule,
1968 cookie, cookie_mask);
1969
1970 /* Convert nx_match to string. */
1971 out = nx_match_to_string(nx_match.data, match_len);
1972 puts(out);
1973 free(out);
1974 } else {
1975 printf("nx_pull_match() returned error %s\n",
1976 ofperr_get_name(error));
1977 }
1978
1979 ofpbuf_uninit(&nx_match);
1980 }
1981 ds_destroy(&in);
1982 }
1983
1984 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
1985 * stdin, does some internal fussing with them, and then prints them back as
1986 * strings on stdout. */
1987 static void
1988 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1989 {
1990 return ofctl_parse_nxm__(false);
1991 }
1992
1993 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
1994 * stdin, does some internal fussing with them, and then prints them back as
1995 * strings on stdout. */
1996 static void
1997 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1998 {
1999 return ofctl_parse_nxm__(true);
2000 }
2001
2002 static void
2003 print_differences(const void *a_, size_t a_len,
2004 const void *b_, size_t b_len)
2005 {
2006 const uint8_t *a = a_;
2007 const uint8_t *b = b_;
2008 size_t i;
2009
2010 for (i = 0; i < MIN(a_len, b_len); i++) {
2011 if (a[i] != b[i]) {
2012 printf("%2zu: %02"PRIx8" -> %02"PRIx8"\n", i, a[i], b[i]);
2013 }
2014 }
2015 for (i = a_len; i < b_len; i++) {
2016 printf("%2zu: (none) -> %02"PRIx8"\n", i, b[i]);
2017 }
2018 for (i = b_len; i < a_len; i++) {
2019 printf("%2zu: %02"PRIx8" -> (none)\n", i, a[i]);
2020 }
2021 }
2022
2023 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2024 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2025 * on stdout, and then converts them back to hex bytes and prints any
2026 * differences from the input. */
2027 static void
2028 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2029 {
2030 struct ds in;
2031
2032 ds_init(&in);
2033 while (!ds_get_preprocessed_line(&in, stdin)) {
2034 struct ofpbuf of10_out;
2035 struct ofpbuf of10_in;
2036 struct ofpbuf ofpacts;
2037 enum ofperr error;
2038 size_t size;
2039 struct ds s;
2040
2041 /* Parse hex bytes. */
2042 ofpbuf_init(&of10_in, 0);
2043 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2044 ovs_fatal(0, "Trailing garbage in hex data");
2045 }
2046
2047 /* Convert to ofpacts. */
2048 ofpbuf_init(&ofpacts, 0);
2049 size = of10_in.size;
2050 error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
2051 if (error) {
2052 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2053 ofpbuf_uninit(&ofpacts);
2054 ofpbuf_uninit(&of10_in);
2055 continue;
2056 }
2057 ofpbuf_push_uninit(&of10_in, size);
2058
2059 /* Print cls_rule. */
2060 ds_init(&s);
2061 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2062 puts(ds_cstr(&s));
2063 ds_destroy(&s);
2064
2065 /* Convert back to ofp10 actions and print differences from input. */
2066 ofpbuf_init(&of10_out, 0);
2067 ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
2068
2069 print_differences(of10_in.data, of10_in.size,
2070 of10_out.data, of10_out.size);
2071 putchar('\n');
2072
2073 ofpbuf_uninit(&ofpacts);
2074 ofpbuf_uninit(&of10_in);
2075 ofpbuf_uninit(&of10_out);
2076 }
2077 ds_destroy(&in);
2078 }
2079
2080 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
2081 * bytes from stdin, converts them to cls_rules, prints them as strings on
2082 * stdout, and then converts them back to hex bytes and prints any differences
2083 * from the input. */
2084 static void
2085 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2086 {
2087 struct ds in;
2088
2089 ds_init(&in);
2090 while (!ds_get_preprocessed_line(&in, stdin)) {
2091 struct ofpbuf match_in;
2092 struct ofp11_match match_out;
2093 struct cls_rule rule;
2094 enum ofperr error;
2095
2096 /* Parse hex bytes. */
2097 ofpbuf_init(&match_in, 0);
2098 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
2099 ovs_fatal(0, "Trailing garbage in hex data");
2100 }
2101 if (match_in.size != sizeof(struct ofp11_match)) {
2102 ovs_fatal(0, "Input is %zu bytes, expected %zu",
2103 match_in.size, sizeof(struct ofp11_match));
2104 }
2105
2106 /* Convert to cls_rule. */
2107 error = ofputil_cls_rule_from_ofp11_match(match_in.data,
2108 OFP_DEFAULT_PRIORITY, &rule);
2109 if (error) {
2110 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
2111 ofpbuf_uninit(&match_in);
2112 continue;
2113 }
2114
2115 /* Print cls_rule. */
2116 cls_rule_print(&rule);
2117
2118 /* Convert back to ofp11_match and print differences from input. */
2119 ofputil_cls_rule_to_ofp11_match(&rule, &match_out);
2120
2121 print_differences(match_in.data, match_in.size,
2122 &match_out, sizeof match_out);
2123 putchar('\n');
2124
2125 ofpbuf_uninit(&match_in);
2126 }
2127 ds_destroy(&in);
2128 }
2129
2130 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
2131 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2132 * on stdout, and then converts them back to hex bytes and prints any
2133 * differences from the input. */
2134 static void
2135 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2136 {
2137 struct ds in;
2138
2139 ds_init(&in);
2140 while (!ds_get_preprocessed_line(&in, stdin)) {
2141 struct ofpbuf of11_out;
2142 struct ofpbuf of11_in;
2143 struct ofpbuf ofpacts;
2144 enum ofperr error;
2145 size_t size;
2146 struct ds s;
2147
2148 /* Parse hex bytes. */
2149 ofpbuf_init(&of11_in, 0);
2150 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2151 ovs_fatal(0, "Trailing garbage in hex data");
2152 }
2153
2154 /* Convert to ofpacts. */
2155 ofpbuf_init(&ofpacts, 0);
2156 size = of11_in.size;
2157 error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
2158 &ofpacts);
2159 if (error) {
2160 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2161 ofpbuf_uninit(&ofpacts);
2162 ofpbuf_uninit(&of11_in);
2163 continue;
2164 }
2165 ofpbuf_push_uninit(&of11_in, size);
2166
2167 /* Print cls_rule. */
2168 ds_init(&s);
2169 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2170 puts(ds_cstr(&s));
2171 ds_destroy(&s);
2172
2173 /* Convert back to ofp11 actions and print differences from input. */
2174 ofpbuf_init(&of11_out, 0);
2175 ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
2176
2177 print_differences(of11_in.data, of11_in.size,
2178 of11_out.data, of11_out.size);
2179 putchar('\n');
2180
2181 ofpbuf_uninit(&ofpacts);
2182 ofpbuf_uninit(&of11_in);
2183 ofpbuf_uninit(&of11_out);
2184 }
2185 ds_destroy(&in);
2186 }
2187
2188 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
2189 * specifications as hex bytes from stdin, converts them to ofpacts, prints
2190 * them as strings on stdout, and then converts them back to hex bytes and
2191 * prints any differences from the input. */
2192 static void
2193 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2194 {
2195 struct ds in;
2196
2197 ds_init(&in);
2198 while (!ds_get_preprocessed_line(&in, stdin)) {
2199 struct ofpbuf of11_out;
2200 struct ofpbuf of11_in;
2201 struct ofpbuf ofpacts;
2202 enum ofperr error;
2203 size_t size;
2204 struct ds s;
2205
2206 /* Parse hex bytes. */
2207 ofpbuf_init(&of11_in, 0);
2208 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
2209 ovs_fatal(0, "Trailing garbage in hex data");
2210 }
2211
2212 /* Convert to ofpacts. */
2213 ofpbuf_init(&ofpacts, 0);
2214 size = of11_in.size;
2215 error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
2216 &ofpacts);
2217 if (error) {
2218 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
2219 ofpbuf_uninit(&ofpacts);
2220 ofpbuf_uninit(&of11_in);
2221 continue;
2222 }
2223 ofpbuf_push_uninit(&of11_in, size);
2224
2225 /* Print cls_rule. */
2226 ds_init(&s);
2227 ofpacts_format(ofpacts.data, ofpacts.size, &s);
2228 puts(ds_cstr(&s));
2229 ds_destroy(&s);
2230
2231 /* Convert back to ofp11 instructions and print differences from
2232 * input. */
2233 ofpbuf_init(&of11_out, 0);
2234 ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
2235 &of11_out);
2236
2237 print_differences(of11_in.data, of11_in.size,
2238 of11_out.data, of11_out.size);
2239 putchar('\n');
2240
2241 ofpbuf_uninit(&ofpacts);
2242 ofpbuf_uninit(&of11_in);
2243 ofpbuf_uninit(&of11_out);
2244 }
2245 ds_destroy(&in);
2246 }
2247
2248 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
2249 * version. */
2250 static void
2251 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
2252 {
2253 enum ofperr error;
2254 int version;
2255
2256 error = ofperr_from_name(argv[1]);
2257 if (!error) {
2258 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
2259 }
2260
2261 for (version = 0; version <= UINT8_MAX; version++) {
2262 const struct ofperr_domain *domain;
2263
2264 domain = ofperr_domain_from_version(version);
2265 if (!domain) {
2266 continue;
2267 }
2268
2269 printf("%s: %d,%d\n",
2270 ofperr_domain_get_name(domain),
2271 ofperr_get_type(error, domain),
2272 ofperr_get_code(error, domain));
2273 }
2274 }
2275
2276 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
2277 * binary data, interpreting them as an OpenFlow message, and prints the
2278 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
2279 static void
2280 ofctl_ofp_print(int argc, char *argv[])
2281 {
2282 struct ofpbuf packet;
2283
2284 ofpbuf_init(&packet, strlen(argv[1]) / 2);
2285 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
2286 ovs_fatal(0, "trailing garbage following hex bytes");
2287 }
2288 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
2289 ofpbuf_uninit(&packet);
2290 }
2291
2292 static const struct command all_commands[] = {
2293 { "show", 1, 1, ofctl_show },
2294 { "monitor", 1, 3, ofctl_monitor },
2295 { "snoop", 1, 1, ofctl_snoop },
2296 { "dump-desc", 1, 1, ofctl_dump_desc },
2297 { "dump-tables", 1, 1, ofctl_dump_tables },
2298 { "dump-flows", 1, 2, ofctl_dump_flows },
2299 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
2300 { "queue-stats", 1, 3, ofctl_queue_stats },
2301 { "add-flow", 2, 2, ofctl_add_flow },
2302 { "add-flows", 2, 2, ofctl_add_flows },
2303 { "mod-flows", 2, 2, ofctl_mod_flows },
2304 { "del-flows", 1, 2, ofctl_del_flows },
2305 { "replace-flows", 2, 2, ofctl_replace_flows },
2306 { "diff-flows", 2, 2, ofctl_diff_flows },
2307 { "packet-out", 4, INT_MAX, ofctl_packet_out },
2308 { "dump-ports", 1, 2, ofctl_dump_ports },
2309 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
2310 { "mod-port", 3, 3, ofctl_mod_port },
2311 { "get-frags", 1, 1, ofctl_get_frags },
2312 { "set-frags", 2, 2, ofctl_set_frags },
2313 { "probe", 1, 1, ofctl_probe },
2314 { "ping", 1, 2, ofctl_ping },
2315 { "benchmark", 3, 3, ofctl_benchmark },
2316 { "help", 0, INT_MAX, ofctl_help },
2317
2318 /* Undocumented commands for testing. */
2319 { "parse-flow", 1, 1, ofctl_parse_flow },
2320 { "parse-flows", 1, 1, ofctl_parse_flows },
2321 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
2322 { "parse-nxm", 0, 0, ofctl_parse_nxm },
2323 { "parse-oxm", 0, 0, ofctl_parse_oxm },
2324 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
2325 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
2326 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
2327 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
2328 { "print-error", 1, 1, ofctl_print_error },
2329 { "ofp-print", 1, 2, ofctl_ofp_print },
2330
2331 { NULL, 0, 0, NULL },
2332 };