]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-ofctl.c
Enable OpenFlow 1.0, 1.1, 1.2, and 1.3 by default.
[mirror_ovs.git] / utilities / ovs-ofctl.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 <ctype.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
35 #include "daemon.h"
36 #include "compiler.h"
37 #include "dirs.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
40 #include "nx-match.h"
41 #include "odp-util.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
44 #include "ofp-msgs.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
47 #include "ofp-util.h"
48 #include "ofp-version-opt.h"
49 #include "ofpbuf.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
53 #include "packets.h"
54 #include "pcap-file.h"
55 #include "poll-loop.h"
56 #include "random.h"
57 #include "stream-ssl.h"
58 #include "socket-util.h"
59 #include "timeval.h"
60 #include "unixctl.h"
61 #include "util.h"
62 #include "vconn.h"
63 #include "vlog.h"
64 #include "meta-flow.h"
65 #include "sort.h"
66
67 VLOG_DEFINE_THIS_MODULE(ofctl);
68
69 /* --strict: Use strict matching for flow mod commands? Additionally governs
70 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
71 */
72 static bool strict;
73
74 /* --readd: If true, on replace-flows, re-add even flows that have not changed
75 * (to reset flow counters). */
76 static bool readd;
77
78 /* -F, --flow-format: Allowed protocols. By default, any protocol is
79 * allowed. */
80 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
81
82 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
83 * commands. Either one of NXPIF_* to force a particular packet_in format, or
84 * -1 to let ovs-ofctl choose the default. */
85 static int preferred_packet_in_format = -1;
86
87 /* -m, --more: Additional verbosity for ofp-print functions. */
88 static int verbosity;
89
90 /* --timestamp: Print a timestamp before each received packet on "monitor" and
91 * "snoop" command? */
92 static bool timestamp;
93
94 /* --sort, --rsort: Sort order. */
95 enum sort_order { SORT_ASC, SORT_DESC };
96 struct sort_criterion {
97 const struct mf_field *field; /* NULL means to sort by priority. */
98 enum sort_order order;
99 };
100 static struct sort_criterion *criteria;
101 static size_t n_criteria, allocated_criteria;
102
103 static const struct command *get_all_commands(void);
104
105 static void usage(void) NO_RETURN;
106 static void parse_options(int argc, char *argv[]);
107
108 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
109 struct ofpbuf **replyp,
110 struct ofputil_flow_stats *,
111 struct ofpbuf *ofpacts);
112 int
113 main(int argc, char *argv[])
114 {
115 set_program_name(argv[0]);
116 parse_options(argc, argv);
117 fatal_ignore_sigpipe();
118 run_command(argc - optind, argv + optind, get_all_commands());
119 return 0;
120 }
121
122 static void
123 add_sort_criterion(enum sort_order order, const char *field)
124 {
125 struct sort_criterion *sc;
126
127 if (n_criteria >= allocated_criteria) {
128 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
129 }
130
131 sc = &criteria[n_criteria++];
132 if (!field || !strcasecmp(field, "priority")) {
133 sc->field = NULL;
134 } else {
135 sc->field = mf_from_name(field);
136 if (!sc->field) {
137 ovs_fatal(0, "%s: unknown field name", field);
138 }
139 }
140 sc->order = order;
141 }
142
143 static void
144 parse_options(int argc, char *argv[])
145 {
146 enum {
147 OPT_STRICT = UCHAR_MAX + 1,
148 OPT_READD,
149 OPT_TIMESTAMP,
150 OPT_SORT,
151 OPT_RSORT,
152 DAEMON_OPTION_ENUMS,
153 OFP_VERSION_OPTION_ENUMS,
154 VLOG_OPTION_ENUMS
155 };
156 static const struct option long_options[] = {
157 {"timeout", required_argument, NULL, 't'},
158 {"strict", no_argument, NULL, OPT_STRICT},
159 {"readd", no_argument, NULL, OPT_READD},
160 {"flow-format", required_argument, NULL, 'F'},
161 {"packet-in-format", required_argument, NULL, 'P'},
162 {"more", no_argument, NULL, 'm'},
163 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
164 {"sort", optional_argument, NULL, OPT_SORT},
165 {"rsort", optional_argument, NULL, OPT_RSORT},
166 {"help", no_argument, NULL, 'h'},
167 DAEMON_LONG_OPTIONS,
168 OFP_VERSION_LONG_OPTIONS,
169 VLOG_LONG_OPTIONS,
170 STREAM_SSL_LONG_OPTIONS,
171 {NULL, 0, NULL, 0},
172 };
173 char *short_options = long_options_to_short_options(long_options);
174 uint32_t versions;
175 enum ofputil_protocol version_protocols;
176
177 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
178 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
179 * requests, but those requests have subtly different semantics in
180 * different OpenFlow versions. For example:
181 *
182 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
183 * existing flow to modify adds a new flow.
184 *
185 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
186 * existing flow to modify adds a new flow, but only if the mod-flow
187 * did not match on the flow cookie.
188 *
189 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
190 * new flow.
191 */
192 set_allowed_ofp_versions("OpenFlow10");
193
194 for (;;) {
195 unsigned long int timeout;
196 int c;
197
198 c = getopt_long(argc, argv, short_options, long_options, NULL);
199 if (c == -1) {
200 break;
201 }
202
203 switch (c) {
204 case 't':
205 timeout = strtoul(optarg, NULL, 10);
206 if (timeout <= 0) {
207 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
208 optarg);
209 } else {
210 time_alarm(timeout);
211 }
212 break;
213
214 case 'F':
215 allowed_protocols = ofputil_protocols_from_string(optarg);
216 if (!allowed_protocols) {
217 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
218 }
219 break;
220
221 case 'P':
222 preferred_packet_in_format =
223 ofputil_packet_in_format_from_string(optarg);
224 if (preferred_packet_in_format < 0) {
225 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
226 }
227 break;
228
229 case 'm':
230 verbosity++;
231 break;
232
233 case 'h':
234 usage();
235
236 case OPT_STRICT:
237 strict = true;
238 break;
239
240 case OPT_READD:
241 readd = true;
242 break;
243
244 case OPT_TIMESTAMP:
245 timestamp = true;
246 break;
247
248 case OPT_SORT:
249 add_sort_criterion(SORT_ASC, optarg);
250 break;
251
252 case OPT_RSORT:
253 add_sort_criterion(SORT_DESC, optarg);
254 break;
255
256 DAEMON_OPTION_HANDLERS
257 OFP_VERSION_OPTION_HANDLERS
258 VLOG_OPTION_HANDLERS
259 STREAM_SSL_OPTION_HANDLERS
260
261 case '?':
262 exit(EXIT_FAILURE);
263
264 default:
265 abort();
266 }
267 }
268
269 if (n_criteria) {
270 /* Always do a final sort pass based on priority. */
271 add_sort_criterion(SORT_DESC, "priority");
272 }
273
274 free(short_options);
275
276 versions = get_allowed_ofp_versions();
277 version_protocols = ofputil_protocols_from_version_bitmap(versions);
278 if (!(allowed_protocols & version_protocols)) {
279 char *protocols = ofputil_protocols_to_string(allowed_protocols);
280 struct ds version_s = DS_EMPTY_INITIALIZER;
281
282 ofputil_format_version_bitmap_names(&version_s, versions);
283 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
284 "any of the enabled flow formats (%s). (Use -O to enable "
285 "additional OpenFlow versions or -F to enable additional "
286 "flow formats.)", ds_cstr(&version_s), protocols);
287 }
288 allowed_protocols &= version_protocols;
289 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
290 allowed_protocols));
291 }
292
293 static void
294 usage(void)
295 {
296 printf("%s: OpenFlow switch management utility\n"
297 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
298 "\nFor OpenFlow switches:\n"
299 " show SWITCH show OpenFlow information\n"
300 " dump-desc SWITCH print switch description\n"
301 " dump-tables SWITCH print table stats\n"
302 " dump-table-features SWITCH print table features\n"
303 " mod-port SWITCH IFACE ACT modify port behavior\n"
304 " mod-table SWITCH MOD modify flow table behavior\n"
305 " get-frags SWITCH print fragment handling behavior\n"
306 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
307 " dump-ports SWITCH [PORT] print port statistics\n"
308 " dump-ports-desc SWITCH print port descriptions\n"
309 " dump-flows SWITCH print all flow entries\n"
310 " dump-flows SWITCH FLOW print matching FLOWs\n"
311 " dump-aggregate SWITCH print aggregate flow statistics\n"
312 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
313 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
314 " add-flow SWITCH FLOW add flow described by FLOW\n"
315 " add-flows SWITCH FILE add flows from FILE\n"
316 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
317 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
318 " replace-flows SWITCH FILE replace flows with those in FILE\n"
319 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
320 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
321 " execute ACTIONS on PACKET\n"
322 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
323 " print packets received from SWITCH\n"
324 " snoop SWITCH snoop on SWITCH and its controller\n"
325 " add-group SWITCH GROUP add group described by GROUP\n"
326 " add-group SWITCH FILE add group from FILE\n"
327 " mod-group SWITCH GROUP modify specific group\n"
328 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
329 " dump-group-features SWITCH print group features\n"
330 " dump-groups SWITCH print group description\n"
331 " dump-group-stats SWITCH [GROUP] print group statistics\n"
332 " queue-get-config SWITCH PORT print queue information for port\n"
333 " add-meter SWITCH METER add meter described by METER\n"
334 " mod-meter SWITCH METER modify specific METER\n"
335 " del-meter SWITCH METER delete METER\n"
336 " del-meters SWITCH delete all meters\n"
337 " dump-meter SWITCH METER print METER configuration\n"
338 " dump-meters SWITCH print all meter configuration\n"
339 " meter-stats SWITCH [METER] print meter statistics\n"
340 " meter-features SWITCH print meter features\n"
341 "\nFor OpenFlow switches and controllers:\n"
342 " probe TARGET probe whether TARGET is up\n"
343 " ping TARGET [N] latency of N-byte echos\n"
344 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
345 "SWITCH or TARGET is an active OpenFlow connection method.\n"
346 "\nOther commands:\n"
347 " ofp-parse FILE print messages read from FILE\n"
348 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
349 program_name, program_name);
350 vconn_usage(true, false, false);
351 daemon_usage();
352 ofp_version_usage();
353 vlog_usage();
354 printf("\nOther options:\n"
355 " --strict use strict match for flow commands\n"
356 " --readd replace flows that haven't changed\n"
357 " -F, --flow-format=FORMAT force particular flow format\n"
358 " -P, --packet-in-format=FRMT force particular packet in format\n"
359 " -m, --more be more verbose printing OpenFlow\n"
360 " --timestamp (monitor, snoop) print timestamps\n"
361 " -t, --timeout=SECS give up after SECS seconds\n"
362 " --sort[=field] sort in ascending order\n"
363 " --rsort[=field] sort in descending order\n"
364 " -h, --help display this help message\n"
365 " -V, --version display version information\n");
366 exit(EXIT_SUCCESS);
367 }
368
369 static void
370 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
371 const char *argv[] OVS_UNUSED, void *exiting_)
372 {
373 bool *exiting = exiting_;
374 *exiting = true;
375 unixctl_command_reply(conn, NULL);
376 }
377
378 static void run(int retval, const char *message, ...)
379 PRINTF_FORMAT(2, 3);
380
381 static void
382 run(int retval, const char *message, ...)
383 {
384 if (retval) {
385 va_list args;
386
387 va_start(args, message);
388 ovs_fatal_valist(retval, message, args);
389 }
390 }
391 \f
392 /* Generic commands. */
393
394 static int
395 open_vconn_socket(const char *name, struct vconn **vconnp)
396 {
397 char *vconn_name = xasprintf("unix:%s", name);
398 int error;
399
400 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
401 vconnp);
402 if (error && error != ENOENT) {
403 ovs_fatal(0, "%s: failed to open socket (%s)", name,
404 ovs_strerror(error));
405 }
406 free(vconn_name);
407
408 return error;
409 }
410
411 enum open_target { MGMT, SNOOP };
412
413 static enum ofputil_protocol
414 open_vconn__(const char *name, enum open_target target,
415 struct vconn **vconnp)
416 {
417 const char *suffix = target == MGMT ? "mgmt" : "snoop";
418 char *datapath_name, *datapath_type, *socket_name;
419 enum ofputil_protocol protocol;
420 char *bridge_path;
421 int ofp_version;
422 int error;
423
424 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
425
426 ofproto_parse_name(name, &datapath_name, &datapath_type);
427 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
428 free(datapath_name);
429 free(datapath_type);
430
431 if (strchr(name, ':')) {
432 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
433 "connecting to %s", name);
434 } else if (!open_vconn_socket(name, vconnp)) {
435 /* Fall Through. */
436 } else if (!open_vconn_socket(bridge_path, vconnp)) {
437 /* Fall Through. */
438 } else if (!open_vconn_socket(socket_name, vconnp)) {
439 /* Fall Through. */
440 } else {
441 ovs_fatal(0, "%s is not a bridge or a socket", name);
442 }
443
444 if (target == SNOOP) {
445 vconn_set_recv_any_version(*vconnp);
446 }
447
448 free(bridge_path);
449 free(socket_name);
450
451 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
452 error = vconn_connect_block(*vconnp);
453 if (error) {
454 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
455 ovs_strerror(error));
456 }
457
458 ofp_version = vconn_get_version(*vconnp);
459 protocol = ofputil_protocol_from_ofp_version(ofp_version);
460 if (!protocol) {
461 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
462 name, ofp_version);
463 }
464 return protocol;
465 }
466
467 static enum ofputil_protocol
468 open_vconn(const char *name, struct vconn **vconnp)
469 {
470 return open_vconn__(name, MGMT, vconnp);
471 }
472
473 static void
474 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
475 {
476 ofpmsg_update_length(buffer);
477 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
478 }
479
480 static void
481 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
482 {
483 struct ofpbuf *reply;
484
485 ofpmsg_update_length(request);
486 run(vconn_transact(vconn, request, &reply), "talking to %s",
487 vconn_get_name(vconn));
488 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
489 ofpbuf_delete(reply);
490 }
491
492 static void
493 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
494 {
495 struct ofpbuf *request;
496 struct vconn *vconn;
497
498 open_vconn(vconn_name, &vconn);
499 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
500 dump_transaction(vconn, request);
501 vconn_close(vconn);
502 }
503
504 static void
505 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
506 {
507 const struct ofp_header *request_oh = ofpbuf_data(request);
508 ovs_be32 send_xid = request_oh->xid;
509 enum ofpraw request_raw;
510 enum ofpraw reply_raw;
511 bool done = false;
512
513 ofpraw_decode_partial(&request_raw, ofpbuf_data(request), ofpbuf_size(request));
514 reply_raw = ofpraw_stats_request_to_reply(request_raw,
515 request_oh->version);
516
517 send_openflow_buffer(vconn, request);
518 while (!done) {
519 ovs_be32 recv_xid;
520 struct ofpbuf *reply;
521
522 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
523 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
524 if (send_xid == recv_xid) {
525 enum ofpraw raw;
526
527 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
528
529 ofpraw_decode(&raw, ofpbuf_data(reply));
530 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
531 done = true;
532 } else if (raw == reply_raw) {
533 done = !ofpmp_more(ofpbuf_data(reply));
534 } else {
535 ovs_fatal(0, "received bad reply: %s",
536 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
537 verbosity + 1));
538 }
539 } else {
540 VLOG_DBG("received reply with xid %08"PRIx32" "
541 "!= expected %08"PRIx32, recv_xid, send_xid);
542 }
543 ofpbuf_delete(reply);
544 }
545 }
546
547 static void
548 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
549 {
550 struct ofpbuf *request;
551 struct vconn *vconn;
552
553 open_vconn(vconn_name, &vconn);
554 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
555 dump_stats_transaction(vconn, request);
556 vconn_close(vconn);
557 }
558
559 /* Sends all of the 'requests', which should be requests that only have replies
560 * if an error occurs, and waits for them to succeed or fail. If an error does
561 * occur, prints it and exits with an error.
562 *
563 * Destroys all of the 'requests'. */
564 static void
565 transact_multiple_noreply(struct vconn *vconn, struct list *requests)
566 {
567 struct ofpbuf *request, *reply;
568
569 LIST_FOR_EACH (request, list_node, requests) {
570 ofpmsg_update_length(request);
571 }
572
573 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
574 "talking to %s", vconn_get_name(vconn));
575 if (reply) {
576 ofp_print(stderr, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 2);
577 exit(1);
578 }
579 ofpbuf_delete(reply);
580 }
581
582 /* Sends 'request', which should be a request that only has a reply if an error
583 * occurs, and waits for it to succeed or fail. If an error does occur, prints
584 * it and exits with an error.
585 *
586 * Destroys 'request'. */
587 static void
588 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
589 {
590 struct list requests;
591
592 list_init(&requests);
593 list_push_back(&requests, &request->list_node);
594 transact_multiple_noreply(vconn, &requests);
595 }
596
597 static void
598 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
599 {
600 struct ofp_switch_config *config;
601 struct ofpbuf *request;
602 struct ofpbuf *reply;
603 enum ofptype type;
604
605 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
606 vconn_get_version(vconn), 0);
607 run(vconn_transact(vconn, request, &reply),
608 "talking to %s", vconn_get_name(vconn));
609
610 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
611 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
612 }
613
614 config = ofpbuf_pull(reply, sizeof *config);
615 *config_ = *config;
616
617 ofpbuf_delete(reply);
618 }
619
620 static void
621 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
622 {
623 struct ofpbuf *request;
624
625 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
626 ofpbuf_put(request, config, sizeof *config);
627
628 transact_noreply(vconn, request);
629 }
630
631 static void
632 ofctl_show(int argc OVS_UNUSED, char *argv[])
633 {
634 const char *vconn_name = argv[1];
635 struct vconn *vconn;
636 struct ofpbuf *request;
637 struct ofpbuf *reply;
638 bool trunc;
639
640 open_vconn(vconn_name, &vconn);
641 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
642 vconn_get_version(vconn), 0);
643 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
644
645 trunc = ofputil_switch_features_ports_trunc(reply);
646 ofp_print(stdout, ofpbuf_data(reply), ofpbuf_size(reply), verbosity + 1);
647
648 ofpbuf_delete(reply);
649
650 if (trunc) {
651 /* The Features Reply may not contain all the ports, so send a
652 * Port Description stats request, which doesn't have size
653 * constraints. */
654 dump_trivial_stats_transaction(vconn_name,
655 OFPRAW_OFPST_PORT_DESC_REQUEST);
656 }
657 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
658 vconn_close(vconn);
659 }
660
661 static void
662 ofctl_dump_desc(int argc OVS_UNUSED, char *argv[])
663 {
664 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST);
665 }
666
667 static void
668 ofctl_dump_tables(int argc OVS_UNUSED, char *argv[])
669 {
670 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST);
671 }
672
673 static void
674 ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[])
675 {
676 struct ofpbuf *request;
677 struct vconn *vconn;
678
679 open_vconn(argv[1], &vconn);
680 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
681 if (request) {
682 dump_stats_transaction(vconn, request);
683 }
684
685 vconn_close(vconn);
686 }
687
688 static bool
689 fetch_port_by_features(const char *vconn_name,
690 const char *port_name, ofp_port_t port_no,
691 struct ofputil_phy_port *pp, bool *trunc)
692 {
693 struct ofputil_switch_features features;
694 const struct ofp_header *oh;
695 struct ofpbuf *request, *reply;
696 struct vconn *vconn;
697 enum ofperr error;
698 enum ofptype type;
699 struct ofpbuf b;
700 bool found = false;
701
702 /* Fetch the switch's ofp_switch_features. */
703 open_vconn(vconn_name, &vconn);
704 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
705 vconn_get_version(vconn), 0);
706 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
707 vconn_close(vconn);
708
709 oh = ofpbuf_data(reply);
710 if (ofptype_decode(&type, ofpbuf_data(reply))
711 || type != OFPTYPE_FEATURES_REPLY) {
712 ovs_fatal(0, "%s: received bad features reply", vconn_name);
713 }
714
715 *trunc = false;
716 if (ofputil_switch_features_ports_trunc(reply)) {
717 *trunc = true;
718 goto exit;
719 }
720
721 error = ofputil_decode_switch_features(oh, &features, &b);
722 if (error) {
723 ovs_fatal(0, "%s: failed to decode features reply (%s)",
724 vconn_name, ofperr_to_string(error));
725 }
726
727 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
728 if (port_no != OFPP_NONE
729 ? port_no == pp->port_no
730 : !strcmp(pp->name, port_name)) {
731 found = true;
732 goto exit;
733 }
734 }
735
736 exit:
737 ofpbuf_delete(reply);
738 return found;
739 }
740
741 static bool
742 fetch_port_by_stats(const char *vconn_name,
743 const char *port_name, ofp_port_t port_no,
744 struct ofputil_phy_port *pp)
745 {
746 struct ofpbuf *request;
747 struct vconn *vconn;
748 ovs_be32 send_xid;
749 bool done = false;
750 bool found = false;
751
752 request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0);
753 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
754
755 open_vconn(vconn_name, &vconn);
756 send_openflow_buffer(vconn, request);
757 while (!done) {
758 ovs_be32 recv_xid;
759 struct ofpbuf *reply;
760
761 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
762 recv_xid = ((struct ofp_header *) ofpbuf_data(reply))->xid;
763 if (send_xid == recv_xid) {
764 struct ofp_header *oh = ofpbuf_data(reply);
765 enum ofptype type;
766 struct ofpbuf b;
767 uint16_t flags;
768
769 ofpbuf_use_const(&b, oh, ntohs(oh->length));
770 if (ofptype_pull(&type, &b)
771 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
772 ovs_fatal(0, "received bad reply: %s",
773 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
774 verbosity + 1));
775 }
776
777 flags = ofpmp_flags(oh);
778 done = !(flags & OFPSF_REPLY_MORE);
779
780 if (found) {
781 /* We've already found the port, but we need to drain
782 * the queue of any other replies for this request. */
783 continue;
784 }
785
786 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
787 if (port_no != OFPP_NONE ? port_no == pp->port_no
788 : !strcmp(pp->name, port_name)) {
789 found = true;
790 break;
791 }
792 }
793 } else {
794 VLOG_DBG("received reply with xid %08"PRIx32" "
795 "!= expected %08"PRIx32, recv_xid, send_xid);
796 }
797 ofpbuf_delete(reply);
798 }
799 vconn_close(vconn);
800
801 return found;
802 }
803
804 static bool
805 str_to_ofp(const char *s, ofp_port_t *ofp_port)
806 {
807 bool ret;
808 uint32_t port_;
809
810 ret = str_to_uint(s, 10, &port_);
811 *ofp_port = u16_to_ofp(port_);
812 return ret;
813 }
814
815 /* Opens a connection to 'vconn_name', fetches the port structure for
816 * 'port_name' (which may be a port name or number), and copies it into
817 * '*pp'. */
818 static void
819 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
820 struct ofputil_phy_port *pp)
821 {
822 ofp_port_t port_no;
823 bool found;
824 bool trunc;
825
826 /* Try to interpret the argument as a port number. */
827 if (!str_to_ofp(port_name, &port_no)) {
828 port_no = OFPP_NONE;
829 }
830
831 /* Try to find the port based on the Features Reply. If it looks
832 * like the results may be truncated, then use the Port Description
833 * stats message introduced in OVS 1.7. */
834 found = fetch_port_by_features(vconn_name, port_name, port_no, pp,
835 &trunc);
836 if (trunc) {
837 found = fetch_port_by_stats(vconn_name, port_name, port_no, pp);
838 }
839
840 if (!found) {
841 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
842 }
843 }
844
845 /* Returns the port number corresponding to 'port_name' (which may be a port
846 * name or number) within the switch 'vconn_name'. */
847 static ofp_port_t
848 str_to_port_no(const char *vconn_name, const char *port_name)
849 {
850 ofp_port_t port_no;
851
852 if (ofputil_port_from_string(port_name, &port_no)) {
853 return port_no;
854 } else {
855 struct ofputil_phy_port pp;
856
857 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
858 return pp.port_no;
859 }
860 }
861
862 static bool
863 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
864 enum ofputil_protocol *cur)
865 {
866 for (;;) {
867 struct ofpbuf *request, *reply;
868 enum ofputil_protocol next;
869
870 request = ofputil_encode_set_protocol(*cur, want, &next);
871 if (!request) {
872 return *cur == want;
873 }
874
875 run(vconn_transact_noreply(vconn, request, &reply),
876 "talking to %s", vconn_get_name(vconn));
877 if (reply) {
878 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
879 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
880 vconn_get_name(vconn), s);
881 free(s);
882 ofpbuf_delete(reply);
883 return false;
884 }
885
886 *cur = next;
887 }
888 }
889
890 static enum ofputil_protocol
891 set_protocol_for_flow_dump(struct vconn *vconn,
892 enum ofputil_protocol cur_protocol,
893 enum ofputil_protocol usable_protocols)
894 {
895 char *usable_s;
896 int i;
897
898 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
899 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
900 if (f & usable_protocols & allowed_protocols
901 && try_set_protocol(vconn, f, &cur_protocol)) {
902 return f;
903 }
904 }
905
906 usable_s = ofputil_protocols_to_string(usable_protocols);
907 if (usable_protocols & allowed_protocols) {
908 ovs_fatal(0, "switch does not support any of the usable flow "
909 "formats (%s)", usable_s);
910 } else {
911 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
912 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
913 "allowed flow formats (%s)", usable_s, allowed_s);
914 }
915 }
916
917 static struct vconn *
918 prepare_dump_flows(int argc, char *argv[], bool aggregate,
919 struct ofpbuf **requestp)
920 {
921 enum ofputil_protocol usable_protocols, protocol;
922 struct ofputil_flow_stats_request fsr;
923 struct vconn *vconn;
924 char *error;
925
926 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
927 argc > 2 ? argv[2] : "",
928 &usable_protocols);
929 if (error) {
930 ovs_fatal(0, "%s", error);
931 }
932
933 protocol = open_vconn(argv[1], &vconn);
934 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
935 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
936 return vconn;
937 }
938
939 static void
940 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
941 {
942 struct ofpbuf *request;
943 struct vconn *vconn;
944
945 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
946 dump_stats_transaction(vconn, request);
947 vconn_close(vconn);
948 }
949
950 static int
951 compare_flows(const void *afs_, const void *bfs_)
952 {
953 const struct ofputil_flow_stats *afs = afs_;
954 const struct ofputil_flow_stats *bfs = bfs_;
955 const struct match *a = &afs->match;
956 const struct match *b = &bfs->match;
957 const struct sort_criterion *sc;
958
959 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
960 const struct mf_field *f = sc->field;
961 int ret;
962
963 if (!f) {
964 unsigned int a_pri = afs->priority;
965 unsigned int b_pri = bfs->priority;
966 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
967 } else {
968 bool ina, inb;
969
970 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
971 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
972 if (ina != inb) {
973 /* Skip the test for sc->order, so that missing fields always
974 * sort to the end whether we're sorting in ascending or
975 * descending order. */
976 return ina ? -1 : 1;
977 } else {
978 union mf_value aval, bval;
979
980 mf_get_value(f, &a->flow, &aval);
981 mf_get_value(f, &b->flow, &bval);
982 ret = memcmp(&aval, &bval, f->n_bytes);
983 }
984 }
985
986 if (ret) {
987 return sc->order == SORT_ASC ? ret : -ret;
988 }
989 }
990
991 return 0;
992 }
993
994 static void
995 ofctl_dump_flows(int argc, char *argv[])
996 {
997 if (!n_criteria) {
998 return ofctl_dump_flows__(argc, argv, false);
999 } else {
1000 struct ofputil_flow_stats *fses;
1001 size_t n_fses, allocated_fses;
1002 struct ofpbuf *request;
1003 struct ofpbuf ofpacts;
1004 struct ofpbuf *reply;
1005 struct vconn *vconn;
1006 ovs_be32 send_xid;
1007 struct ds s;
1008 size_t i;
1009
1010 vconn = prepare_dump_flows(argc, argv, false, &request);
1011 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
1012 send_openflow_buffer(vconn, request);
1013
1014 fses = NULL;
1015 n_fses = allocated_fses = 0;
1016 reply = NULL;
1017 ofpbuf_init(&ofpacts, 0);
1018 for (;;) {
1019 struct ofputil_flow_stats *fs;
1020
1021 if (n_fses >= allocated_fses) {
1022 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1023 }
1024
1025 fs = &fses[n_fses];
1026 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1027 &ofpacts)) {
1028 break;
1029 }
1030 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1031 n_fses++;
1032 }
1033 ofpbuf_uninit(&ofpacts);
1034
1035 qsort(fses, n_fses, sizeof *fses, compare_flows);
1036
1037 ds_init(&s);
1038 for (i = 0; i < n_fses; i++) {
1039 ds_clear(&s);
1040 ofp_print_flow_stats(&s, &fses[i]);
1041 puts(ds_cstr(&s));
1042 }
1043 ds_destroy(&s);
1044
1045 for (i = 0; i < n_fses; i++) {
1046 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1047 }
1048 free(fses);
1049
1050 vconn_close(vconn);
1051 }
1052 }
1053
1054 static void
1055 ofctl_dump_aggregate(int argc, char *argv[])
1056 {
1057 return ofctl_dump_flows__(argc, argv, true);
1058 }
1059
1060 static void
1061 ofctl_queue_stats(int argc, char *argv[])
1062 {
1063 struct ofpbuf *request;
1064 struct vconn *vconn;
1065 struct ofputil_queue_stats_request oqs;
1066
1067 open_vconn(argv[1], &vconn);
1068
1069 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
1070 oqs.port_no = str_to_port_no(argv[1], argv[2]);
1071 } else {
1072 oqs.port_no = OFPP_ANY;
1073 }
1074 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
1075 oqs.queue_id = atoi(argv[3]);
1076 } else {
1077 oqs.queue_id = OFPQ_ALL;
1078 }
1079
1080 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1081 dump_stats_transaction(vconn, request);
1082 vconn_close(vconn);
1083 }
1084
1085 static void
1086 ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[])
1087 {
1088 const char *vconn_name = argv[1];
1089 const char *port_name = argv[2];
1090 enum ofputil_protocol protocol;
1091 enum ofp_version version;
1092 struct ofpbuf *request;
1093 struct vconn *vconn;
1094 ofp_port_t port;
1095
1096 port = str_to_port_no(vconn_name, port_name);
1097
1098 protocol = open_vconn(vconn_name, &vconn);
1099 version = ofputil_protocol_to_ofp_version(protocol);
1100 request = ofputil_encode_queue_get_config_request(version, port);
1101 dump_transaction(vconn, request);
1102 vconn_close(vconn);
1103 }
1104
1105 static enum ofputil_protocol
1106 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1107 enum ofputil_protocol usable_protocols)
1108 {
1109 enum ofputil_protocol cur_protocol;
1110 char *usable_s;
1111 int i;
1112
1113 if (!(usable_protocols & allowed_protocols)) {
1114 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1115 usable_s = ofputil_protocols_to_string(usable_protocols);
1116 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1117 "allowed flow formats (%s)", usable_s, allowed_s);
1118 }
1119
1120 /* If the initial flow format is allowed and usable, keep it. */
1121 cur_protocol = open_vconn(remote, vconnp);
1122 if (usable_protocols & allowed_protocols & cur_protocol) {
1123 return cur_protocol;
1124 }
1125
1126 /* Otherwise try each flow format in turn. */
1127 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1128 enum ofputil_protocol f = 1 << i;
1129
1130 if (f != cur_protocol
1131 && f & usable_protocols & allowed_protocols
1132 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1133 return f;
1134 }
1135 }
1136
1137 usable_s = ofputil_protocols_to_string(usable_protocols);
1138 ovs_fatal(0, "switch does not support any of the usable flow "
1139 "formats (%s)", usable_s);
1140 }
1141
1142 static void
1143 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1144 size_t n_fms, enum ofputil_protocol usable_protocols)
1145 {
1146 enum ofputil_protocol protocol;
1147 struct vconn *vconn;
1148 size_t i;
1149
1150 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1151
1152 for (i = 0; i < n_fms; i++) {
1153 struct ofputil_flow_mod *fm = &fms[i];
1154
1155 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1156 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1157 }
1158 vconn_close(vconn);
1159 }
1160
1161 static void
1162 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1163 {
1164 enum ofputil_protocol usable_protocols;
1165 struct ofputil_flow_mod *fms = NULL;
1166 size_t n_fms = 0;
1167 char *error;
1168
1169 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1170 &usable_protocols);
1171 if (error) {
1172 ovs_fatal(0, "%s", error);
1173 }
1174 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1175 free(fms);
1176 }
1177
1178 static void
1179 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1180 {
1181 if (argc > 2 && !strcmp(argv[2], "-")) {
1182 ofctl_flow_mod_file(argc, argv, command);
1183 } else {
1184 struct ofputil_flow_mod fm;
1185 char *error;
1186 enum ofputil_protocol usable_protocols;
1187
1188 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1189 &usable_protocols);
1190 if (error) {
1191 ovs_fatal(0, "%s", error);
1192 }
1193 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1194 }
1195 }
1196
1197 static void
1198 ofctl_add_flow(int argc, char *argv[])
1199 {
1200 ofctl_flow_mod(argc, argv, OFPFC_ADD);
1201 }
1202
1203 static void
1204 ofctl_add_flows(int argc, char *argv[])
1205 {
1206 ofctl_flow_mod_file(argc, argv, OFPFC_ADD);
1207 }
1208
1209 static void
1210 ofctl_mod_flows(int argc, char *argv[])
1211 {
1212 ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1213 }
1214
1215 static void
1216 ofctl_del_flows(int argc, char *argv[])
1217 {
1218 ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1219 }
1220
1221 static void
1222 set_packet_in_format(struct vconn *vconn,
1223 enum nx_packet_in_format packet_in_format)
1224 {
1225 struct ofpbuf *spif;
1226
1227 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1228 packet_in_format);
1229 transact_noreply(vconn, spif);
1230 VLOG_DBG("%s: using user-specified packet in format %s",
1231 vconn_get_name(vconn),
1232 ofputil_packet_in_format_to_string(packet_in_format));
1233 }
1234
1235 static int
1236 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1237 {
1238 struct ofp_switch_config config;
1239 enum ofp_config_flags flags;
1240
1241 fetch_switch_config(vconn, &config);
1242 flags = ntohs(config.flags);
1243 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1244 /* Set the invalid ttl config. */
1245 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1246
1247 config.flags = htons(flags);
1248 set_switch_config(vconn, &config);
1249
1250 /* Then retrieve the configuration to see if it really took. OpenFlow
1251 * doesn't define error reporting for bad modes, so this is all we can
1252 * do. */
1253 fetch_switch_config(vconn, &config);
1254 flags = ntohs(config.flags);
1255 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1256 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1257 "switch probably doesn't support mode)");
1258 return -EOPNOTSUPP;
1259 }
1260 }
1261 return 0;
1262 }
1263
1264 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1265 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1266 * an error message and stores NULL in '*msgp'. */
1267 static const char *
1268 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1269 {
1270 struct ofp_header *oh;
1271 struct ofpbuf *msg;
1272
1273 msg = ofpbuf_new(strlen(hex) / 2);
1274 *msgp = NULL;
1275
1276 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1277 ofpbuf_delete(msg);
1278 return "Trailing garbage in hex data";
1279 }
1280
1281 if (ofpbuf_size(msg) < sizeof(struct ofp_header)) {
1282 ofpbuf_delete(msg);
1283 return "Message too short for OpenFlow";
1284 }
1285
1286 oh = ofpbuf_data(msg);
1287 if (ofpbuf_size(msg) != ntohs(oh->length)) {
1288 ofpbuf_delete(msg);
1289 return "Message size does not match length in OpenFlow header";
1290 }
1291
1292 *msgp = msg;
1293 return NULL;
1294 }
1295
1296 static void
1297 ofctl_send(struct unixctl_conn *conn, int argc,
1298 const char *argv[], void *vconn_)
1299 {
1300 struct vconn *vconn = vconn_;
1301 struct ds reply;
1302 bool ok;
1303 int i;
1304
1305 ok = true;
1306 ds_init(&reply);
1307 for (i = 1; i < argc; i++) {
1308 const char *error_msg;
1309 struct ofpbuf *msg;
1310 int error;
1311
1312 error_msg = openflow_from_hex(argv[i], &msg);
1313 if (error_msg) {
1314 ds_put_format(&reply, "%s\n", error_msg);
1315 ok = false;
1316 continue;
1317 }
1318
1319 fprintf(stderr, "send: ");
1320 ofp_print(stderr, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
1321
1322 error = vconn_send_block(vconn, msg);
1323 if (error) {
1324 ofpbuf_delete(msg);
1325 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1326 ok = false;
1327 } else {
1328 ds_put_cstr(&reply, "sent\n");
1329 }
1330 }
1331
1332 if (ok) {
1333 unixctl_command_reply(conn, ds_cstr(&reply));
1334 } else {
1335 unixctl_command_reply_error(conn, ds_cstr(&reply));
1336 }
1337 ds_destroy(&reply);
1338 }
1339
1340 struct barrier_aux {
1341 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1342 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1343 };
1344
1345 static void
1346 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1347 const char *argv[] OVS_UNUSED, void *aux_)
1348 {
1349 struct barrier_aux *aux = aux_;
1350 struct ofpbuf *msg;
1351 int error;
1352
1353 if (aux->conn) {
1354 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1355 return;
1356 }
1357
1358 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1359 error = vconn_send_block(aux->vconn, msg);
1360 if (error) {
1361 ofpbuf_delete(msg);
1362 unixctl_command_reply_error(conn, ovs_strerror(error));
1363 } else {
1364 aux->conn = conn;
1365 }
1366 }
1367
1368 static void
1369 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1370 const char *argv[], void *aux OVS_UNUSED)
1371 {
1372 int fd;
1373
1374 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1375 if (fd < 0) {
1376 unixctl_command_reply_error(conn, ovs_strerror(errno));
1377 return;
1378 }
1379
1380 fflush(stderr);
1381 dup2(fd, STDERR_FILENO);
1382 close(fd);
1383 unixctl_command_reply(conn, NULL);
1384 }
1385
1386 static void
1387 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1388 const char *argv[] OVS_UNUSED, void *blocked_)
1389 {
1390 bool *blocked = blocked_;
1391
1392 if (!*blocked) {
1393 *blocked = true;
1394 unixctl_command_reply(conn, NULL);
1395 } else {
1396 unixctl_command_reply(conn, "already blocking");
1397 }
1398 }
1399
1400 static void
1401 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1402 const char *argv[] OVS_UNUSED, void *blocked_)
1403 {
1404 bool *blocked = blocked_;
1405
1406 if (*blocked) {
1407 *blocked = false;
1408 unixctl_command_reply(conn, NULL);
1409 } else {
1410 unixctl_command_reply(conn, "already unblocked");
1411 }
1412 }
1413
1414 /* Prints to stdout all of the messages received on 'vconn'.
1415 *
1416 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1417 * received on 'vconn'. */
1418 static void
1419 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1420 {
1421 struct barrier_aux barrier_aux = { vconn, NULL };
1422 struct unixctl_server *server;
1423 bool exiting = false;
1424 bool blocked = false;
1425 int error;
1426
1427 daemon_save_fd(STDERR_FILENO);
1428 daemonize_start();
1429 error = unixctl_server_create(NULL, &server);
1430 if (error) {
1431 ovs_fatal(error, "failed to create unixctl server");
1432 }
1433 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1434 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1435 ofctl_send, vconn);
1436 unixctl_command_register("ofctl/barrier", "", 0, 0,
1437 ofctl_barrier, &barrier_aux);
1438 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1439 ofctl_set_output_file, NULL);
1440
1441 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1442 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1443 &blocked);
1444
1445 daemonize_complete();
1446
1447 for (;;) {
1448 struct ofpbuf *b;
1449 int retval;
1450
1451 unixctl_server_run(server);
1452
1453 while (!blocked) {
1454 enum ofptype type;
1455
1456 retval = vconn_recv(vconn, &b);
1457 if (retval == EAGAIN) {
1458 break;
1459 }
1460 run(retval, "vconn_recv");
1461
1462 if (timestamp) {
1463 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1464 time_wall_msec(), true);
1465 fputs(s, stderr);
1466 free(s);
1467 }
1468
1469 ofptype_decode(&type, ofpbuf_data(b));
1470 ofp_print(stderr, ofpbuf_data(b), ofpbuf_size(b), verbosity + 2);
1471
1472 switch ((int) type) {
1473 case OFPTYPE_BARRIER_REPLY:
1474 if (barrier_aux.conn) {
1475 unixctl_command_reply(barrier_aux.conn, NULL);
1476 barrier_aux.conn = NULL;
1477 }
1478 break;
1479
1480 case OFPTYPE_ECHO_REQUEST:
1481 if (reply_to_echo_requests) {
1482 struct ofpbuf *reply;
1483
1484 reply = make_echo_reply(ofpbuf_data(b));
1485 retval = vconn_send_block(vconn, reply);
1486 if (retval) {
1487 ovs_fatal(retval, "failed to send echo reply");
1488 }
1489 }
1490 break;
1491 }
1492 ofpbuf_delete(b);
1493 }
1494
1495 if (exiting) {
1496 break;
1497 }
1498
1499 vconn_run(vconn);
1500 vconn_run_wait(vconn);
1501 if (!blocked) {
1502 vconn_recv_wait(vconn);
1503 }
1504 unixctl_server_wait(server);
1505 poll_block();
1506 }
1507 vconn_close(vconn);
1508 unixctl_server_destroy(server);
1509 }
1510
1511 static void
1512 ofctl_monitor(int argc, char *argv[])
1513 {
1514 struct vconn *vconn;
1515 int i;
1516 enum ofputil_protocol usable_protocols;
1517
1518 open_vconn(argv[1], &vconn);
1519 for (i = 2; i < argc; i++) {
1520 const char *arg = argv[i];
1521
1522 if (isdigit((unsigned char) *arg)) {
1523 struct ofp_switch_config config;
1524
1525 fetch_switch_config(vconn, &config);
1526 config.miss_send_len = htons(atoi(arg));
1527 set_switch_config(vconn, &config);
1528 } else if (!strcmp(arg, "invalid_ttl")) {
1529 monitor_set_invalid_ttl_to_controller(vconn);
1530 } else if (!strncmp(arg, "watch:", 6)) {
1531 struct ofputil_flow_monitor_request fmr;
1532 struct ofpbuf *msg;
1533 char *error;
1534
1535 error = parse_flow_monitor_request(&fmr, arg + 6,
1536 &usable_protocols);
1537 if (error) {
1538 ovs_fatal(0, "%s", error);
1539 }
1540
1541 msg = ofpbuf_new(0);
1542 ofputil_append_flow_monitor_request(&fmr, msg);
1543 dump_stats_transaction(vconn, msg);
1544 } else {
1545 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1546 }
1547 }
1548
1549 if (preferred_packet_in_format >= 0) {
1550 set_packet_in_format(vconn, preferred_packet_in_format);
1551 } else {
1552 enum ofp_version version = vconn_get_version(vconn);
1553
1554 switch (version) {
1555 case OFP10_VERSION: {
1556 struct ofpbuf *spif, *reply;
1557
1558 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1559 NXPIF_NXM);
1560 run(vconn_transact_noreply(vconn, spif, &reply),
1561 "talking to %s", vconn_get_name(vconn));
1562 if (reply) {
1563 char *s = ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply), 2);
1564 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1565 " replied: %s. Falling back to the switch default.",
1566 vconn_get_name(vconn), s);
1567 free(s);
1568 ofpbuf_delete(reply);
1569 }
1570 break;
1571 }
1572 case OFP11_VERSION:
1573 case OFP12_VERSION:
1574 case OFP13_VERSION:
1575 case OFP14_VERSION:
1576 break;
1577 default:
1578 OVS_NOT_REACHED();
1579 }
1580 }
1581
1582 monitor_vconn(vconn, true);
1583 }
1584
1585 static void
1586 ofctl_snoop(int argc OVS_UNUSED, char *argv[])
1587 {
1588 struct vconn *vconn;
1589
1590 open_vconn__(argv[1], SNOOP, &vconn);
1591 monitor_vconn(vconn, false);
1592 }
1593
1594 static void
1595 ofctl_dump_ports(int argc, char *argv[])
1596 {
1597 struct ofpbuf *request;
1598 struct vconn *vconn;
1599 ofp_port_t port;
1600
1601 open_vconn(argv[1], &vconn);
1602 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY;
1603 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1604 dump_stats_transaction(vconn, request);
1605 vconn_close(vconn);
1606 }
1607
1608 static void
1609 ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[])
1610 {
1611 dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST);
1612 }
1613
1614 static void
1615 ofctl_probe(int argc OVS_UNUSED, char *argv[])
1616 {
1617 struct ofpbuf *request;
1618 struct vconn *vconn;
1619 struct ofpbuf *reply;
1620
1621 open_vconn(argv[1], &vconn);
1622 request = make_echo_request(vconn_get_version(vconn));
1623 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1624 if (ofpbuf_size(reply) != sizeof(struct ofp_header)) {
1625 ovs_fatal(0, "reply does not match request");
1626 }
1627 ofpbuf_delete(reply);
1628 vconn_close(vconn);
1629 }
1630
1631 static void
1632 ofctl_packet_out(int argc, char *argv[])
1633 {
1634 enum ofputil_protocol protocol;
1635 struct ofputil_packet_out po;
1636 struct ofpbuf ofpacts;
1637 struct vconn *vconn;
1638 char *error;
1639 int i;
1640 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1641
1642 ofpbuf_init(&ofpacts, 64);
1643 error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols);
1644 if (error) {
1645 ovs_fatal(0, "%s", error);
1646 }
1647
1648 po.buffer_id = UINT32_MAX;
1649 po.in_port = str_to_port_no(argv[1], argv[2]);
1650 po.ofpacts = ofpbuf_data(&ofpacts);
1651 po.ofpacts_len = ofpbuf_size(&ofpacts);
1652
1653 protocol = open_vconn(argv[1], &vconn);
1654 for (i = 4; i < argc; i++) {
1655 struct ofpbuf *packet, *opo;
1656 const char *error_msg;
1657
1658 error_msg = eth_from_hex(argv[i], &packet);
1659 if (error_msg) {
1660 ovs_fatal(0, "%s", error_msg);
1661 }
1662
1663 po.packet = ofpbuf_data(packet);
1664 po.packet_len = ofpbuf_size(packet);
1665 opo = ofputil_encode_packet_out(&po, protocol);
1666 transact_noreply(vconn, opo);
1667 ofpbuf_delete(packet);
1668 }
1669 vconn_close(vconn);
1670 ofpbuf_uninit(&ofpacts);
1671 }
1672
1673 static void
1674 ofctl_mod_port(int argc OVS_UNUSED, char *argv[])
1675 {
1676 struct ofp_config_flag {
1677 const char *name; /* The flag's name. */
1678 enum ofputil_port_config bit; /* Bit to turn on or off. */
1679 bool on; /* Value to set the bit to. */
1680 };
1681 static const struct ofp_config_flag flags[] = {
1682 { "up", OFPUTIL_PC_PORT_DOWN, false },
1683 { "down", OFPUTIL_PC_PORT_DOWN, true },
1684 { "stp", OFPUTIL_PC_NO_STP, false },
1685 { "receive", OFPUTIL_PC_NO_RECV, false },
1686 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1687 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1688 { "forward", OFPUTIL_PC_NO_FWD, false },
1689 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1690 };
1691
1692 const struct ofp_config_flag *flag;
1693 enum ofputil_protocol protocol;
1694 struct ofputil_port_mod pm;
1695 struct ofputil_phy_port pp;
1696 struct vconn *vconn;
1697 const char *command;
1698 bool not;
1699
1700 fetch_ofputil_phy_port(argv[1], argv[2], &pp);
1701
1702 pm.port_no = pp.port_no;
1703 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1704 pm.config = 0;
1705 pm.mask = 0;
1706 pm.advertise = 0;
1707
1708 if (!strncasecmp(argv[3], "no-", 3)) {
1709 command = argv[3] + 3;
1710 not = true;
1711 } else if (!strncasecmp(argv[3], "no", 2)) {
1712 command = argv[3] + 2;
1713 not = true;
1714 } else {
1715 command = argv[3];
1716 not = false;
1717 }
1718 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1719 if (!strcasecmp(command, flag->name)) {
1720 pm.mask = flag->bit;
1721 pm.config = flag->on ^ not ? flag->bit : 0;
1722 goto found;
1723 }
1724 }
1725 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1726
1727 found:
1728 protocol = open_vconn(argv[1], &vconn);
1729 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1730 vconn_close(vconn);
1731 }
1732
1733 static void
1734 ofctl_mod_table(int argc OVS_UNUSED, char *argv[])
1735 {
1736 enum ofputil_protocol protocol, usable_protocols;
1737 struct ofputil_table_mod tm;
1738 struct vconn *vconn;
1739 char *error;
1740 int i;
1741
1742 error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols);
1743 if (error) {
1744 ovs_fatal(0, "%s", error);
1745 }
1746
1747 protocol = open_vconn(argv[1], &vconn);
1748 if (!(protocol & usable_protocols)) {
1749 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1750 enum ofputil_protocol f = 1 << i;
1751 if (f != protocol
1752 && f & usable_protocols
1753 && try_set_protocol(vconn, f, &protocol)) {
1754 protocol = f;
1755 break;
1756 }
1757 }
1758 }
1759
1760 if (!(protocol & usable_protocols)) {
1761 char *usable_s = ofputil_protocols_to_string(usable_protocols);
1762 ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1763 }
1764
1765 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1766 vconn_close(vconn);
1767 }
1768
1769 static void
1770 ofctl_get_frags(int argc OVS_UNUSED, char *argv[])
1771 {
1772 struct ofp_switch_config config;
1773 struct vconn *vconn;
1774
1775 open_vconn(argv[1], &vconn);
1776 fetch_switch_config(vconn, &config);
1777 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1778 vconn_close(vconn);
1779 }
1780
1781 static void
1782 ofctl_set_frags(int argc OVS_UNUSED, char *argv[])
1783 {
1784 struct ofp_switch_config config;
1785 enum ofp_config_flags mode;
1786 struct vconn *vconn;
1787 ovs_be16 flags;
1788
1789 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1790 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1791 }
1792
1793 open_vconn(argv[1], &vconn);
1794 fetch_switch_config(vconn, &config);
1795 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1796 if (flags != config.flags) {
1797 /* Set the configuration. */
1798 config.flags = flags;
1799 set_switch_config(vconn, &config);
1800
1801 /* Then retrieve the configuration to see if it really took. OpenFlow
1802 * doesn't define error reporting for bad modes, so this is all we can
1803 * do. */
1804 fetch_switch_config(vconn, &config);
1805 if (flags != config.flags) {
1806 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1807 "switch probably doesn't support mode \"%s\")",
1808 argv[1], ofputil_frag_handling_to_string(mode));
1809 }
1810 }
1811 vconn_close(vconn);
1812 }
1813
1814 static void
1815 ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[])
1816 {
1817 const char *filename = argv[1];
1818 struct ofpbuf b;
1819 FILE *file;
1820
1821 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1822 if (file == NULL) {
1823 ovs_fatal(errno, "%s: open", filename);
1824 }
1825
1826 ofpbuf_init(&b, 65536);
1827 for (;;) {
1828 struct ofp_header *oh;
1829 size_t length, tail_len;
1830 void *tail;
1831 size_t n;
1832
1833 ofpbuf_clear(&b);
1834 oh = ofpbuf_put_uninit(&b, sizeof *oh);
1835 n = fread(oh, 1, sizeof *oh, file);
1836 if (n == 0) {
1837 break;
1838 } else if (n < sizeof *oh) {
1839 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1840 }
1841
1842 length = ntohs(oh->length);
1843 if (length < sizeof *oh) {
1844 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1845 filename, length);
1846 }
1847
1848 tail_len = length - sizeof *oh;
1849 tail = ofpbuf_put_uninit(&b, tail_len);
1850 n = fread(tail, 1, tail_len, file);
1851 if (n < tail_len) {
1852 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1853 }
1854
1855 ofp_print(stdout, ofpbuf_data(&b), ofpbuf_size(&b), verbosity + 2);
1856 }
1857 ofpbuf_uninit(&b);
1858
1859 if (file != stdin) {
1860 fclose(file);
1861 }
1862 }
1863
1864 static bool
1865 is_openflow_port(ovs_be16 port_, char *ports[])
1866 {
1867 uint16_t port = ntohs(port_);
1868 if (ports[0]) {
1869 int i;
1870
1871 for (i = 0; ports[i]; i++) {
1872 if (port == atoi(ports[i])) {
1873 return true;
1874 }
1875 }
1876 return false;
1877 } else {
1878 return port == OFP_PORT || port == OFP_OLD_PORT;
1879 }
1880 }
1881
1882 static void
1883 ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[])
1884 {
1885 struct tcp_reader *reader;
1886 FILE *file;
1887 int error;
1888 bool first;
1889
1890 file = ovs_pcap_open(argv[1], "rb");
1891 if (!file) {
1892 ovs_fatal(errno, "%s: open failed", argv[1]);
1893 }
1894
1895 reader = tcp_reader_open();
1896 first = true;
1897 for (;;) {
1898 struct ofpbuf *packet;
1899 long long int when;
1900 struct flow flow;
1901 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1902
1903 error = ovs_pcap_read(file, &packet, &when);
1904 if (error) {
1905 break;
1906 }
1907 flow_extract(packet, &md, &flow);
1908 if (flow.dl_type == htons(ETH_TYPE_IP)
1909 && flow.nw_proto == IPPROTO_TCP
1910 && (is_openflow_port(flow.tp_src, argv + 2) ||
1911 is_openflow_port(flow.tp_dst, argv + 2))) {
1912 struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet);
1913 if (payload) {
1914 while (ofpbuf_size(payload) >= sizeof(struct ofp_header)) {
1915 const struct ofp_header *oh;
1916 void *data = ofpbuf_data(payload);
1917 int length;
1918
1919 /* Align OpenFlow on 8-byte boundary for safe access. */
1920 ofpbuf_shift(payload, -((intptr_t) data & 7));
1921
1922 oh = ofpbuf_data(payload);
1923 length = ntohs(oh->length);
1924 if (ofpbuf_size(payload) < length) {
1925 break;
1926 }
1927
1928 if (!first) {
1929 putchar('\n');
1930 }
1931 first = false;
1932
1933 if (timestamp) {
1934 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1935 fputs(s, stdout);
1936 free(s);
1937 }
1938
1939 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1940 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1941 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1942 ofp_print(stdout, ofpbuf_data(payload), length, verbosity + 1);
1943 ofpbuf_pull(payload, length);
1944 }
1945 }
1946 }
1947 ofpbuf_delete(packet);
1948 }
1949 tcp_reader_close(reader);
1950 }
1951
1952 static void
1953 ofctl_ping(int argc, char *argv[])
1954 {
1955 size_t max_payload = 65535 - sizeof(struct ofp_header);
1956 unsigned int payload;
1957 struct vconn *vconn;
1958 int i;
1959
1960 payload = argc > 2 ? atoi(argv[2]) : 64;
1961 if (payload > max_payload) {
1962 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
1963 }
1964
1965 open_vconn(argv[1], &vconn);
1966 for (i = 0; i < 10; i++) {
1967 struct timeval start, end;
1968 struct ofpbuf *request, *reply;
1969 const struct ofp_header *rpy_hdr;
1970 enum ofptype type;
1971
1972 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
1973 vconn_get_version(vconn), payload);
1974 random_bytes(ofpbuf_put_uninit(request, payload), payload);
1975
1976 xgettimeofday(&start);
1977 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1978 xgettimeofday(&end);
1979
1980 rpy_hdr = ofpbuf_data(reply);
1981 if (ofptype_pull(&type, reply)
1982 || type != OFPTYPE_ECHO_REPLY
1983 || ofpbuf_size(reply) != payload
1984 || memcmp(ofpbuf_l3(request), ofpbuf_l3(reply), payload)) {
1985 printf("Reply does not match request. Request:\n");
1986 ofp_print(stdout, request, ofpbuf_size(request), verbosity + 2);
1987 printf("Reply:\n");
1988 ofp_print(stdout, reply, ofpbuf_size(reply), verbosity + 2);
1989 }
1990 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1991 ofpbuf_size(reply), argv[1], ntohl(rpy_hdr->xid),
1992 (1000*(double)(end.tv_sec - start.tv_sec))
1993 + (.001*(end.tv_usec - start.tv_usec)));
1994 ofpbuf_delete(request);
1995 ofpbuf_delete(reply);
1996 }
1997 vconn_close(vconn);
1998 }
1999
2000 static void
2001 ofctl_benchmark(int argc OVS_UNUSED, char *argv[])
2002 {
2003 size_t max_payload = 65535 - sizeof(struct ofp_header);
2004 struct timeval start, end;
2005 unsigned int payload_size, message_size;
2006 struct vconn *vconn;
2007 double duration;
2008 int count;
2009 int i;
2010
2011 payload_size = atoi(argv[2]);
2012 if (payload_size > max_payload) {
2013 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2014 }
2015 message_size = sizeof(struct ofp_header) + payload_size;
2016
2017 count = atoi(argv[3]);
2018
2019 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2020 count, message_size, count * message_size);
2021
2022 open_vconn(argv[1], &vconn);
2023 xgettimeofday(&start);
2024 for (i = 0; i < count; i++) {
2025 struct ofpbuf *request, *reply;
2026
2027 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2028 vconn_get_version(vconn), payload_size);
2029 ofpbuf_put_zeros(request, payload_size);
2030 run(vconn_transact(vconn, request, &reply), "transact");
2031 ofpbuf_delete(reply);
2032 }
2033 xgettimeofday(&end);
2034 vconn_close(vconn);
2035
2036 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2037 + (.001*(end.tv_usec - start.tv_usec)));
2038 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2039 duration, count / (duration / 1000.0),
2040 count * message_size / (duration / 1000.0));
2041 }
2042
2043 static void
2044 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2045 size_t n_gms)
2046 {
2047 struct ofputil_group_mod *gm;
2048 struct ofpbuf *request;
2049
2050 struct vconn *vconn;
2051 size_t i;
2052
2053 open_vconn(remote, &vconn);
2054
2055 for (i = 0; i < n_gms; i++) {
2056 gm = &gms[i];
2057 request = ofputil_encode_group_mod(vconn_get_version(vconn), gm);
2058 if (request) {
2059 transact_noreply(vconn, request);
2060 }
2061 }
2062
2063 vconn_close(vconn);
2064
2065 }
2066
2067
2068 static void
2069 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2070 {
2071 struct ofputil_group_mod *gms = NULL;
2072 enum ofputil_protocol usable_protocols;
2073 size_t n_gms = 0;
2074 char *error;
2075
2076 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2077 &usable_protocols);
2078 if (error) {
2079 ovs_fatal(0, "%s", error);
2080 }
2081 ofctl_group_mod__(argv[1], gms, n_gms);
2082 free(gms);
2083 }
2084
2085 static void
2086 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2087 {
2088 if (argc > 2 && !strcmp(argv[2], "-")) {
2089 ofctl_group_mod_file(argc, argv, command);
2090 } else {
2091 enum ofputil_protocol usable_protocols;
2092 struct ofputil_group_mod gm;
2093 char *error;
2094
2095 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2096 &usable_protocols);
2097 if (error) {
2098 ovs_fatal(0, "%s", error);
2099 }
2100 ofctl_group_mod__(argv[1], &gm, 1);
2101 }
2102 }
2103
2104 static void
2105 ofctl_add_group(int argc, char *argv[])
2106 {
2107 ofctl_group_mod(argc, argv, OFPGC11_ADD);
2108 }
2109
2110 static void
2111 ofctl_add_groups(int argc, char *argv[])
2112 {
2113 ofctl_group_mod_file(argc, argv, OFPGC11_ADD);
2114 }
2115
2116 static void
2117 ofctl_mod_group(int argc, char *argv[])
2118 {
2119 ofctl_group_mod(argc, argv, OFPGC11_MODIFY);
2120 }
2121
2122 static void
2123 ofctl_del_groups(int argc, char *argv[])
2124 {
2125 ofctl_group_mod(argc, argv, OFPGC11_DELETE);
2126 }
2127
2128 static void
2129 ofctl_dump_group_stats(int argc, char *argv[])
2130 {
2131 enum ofputil_protocol usable_protocols;
2132 struct ofputil_group_mod gm;
2133 struct ofpbuf *request;
2134 struct vconn *vconn;
2135 uint32_t group_id;
2136 char *error;
2137
2138 memset(&gm, 0, sizeof gm);
2139
2140 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2141 argc > 2 ? argv[2] : "",
2142 &usable_protocols);
2143 if (error) {
2144 ovs_fatal(0, "%s", error);
2145 }
2146
2147 group_id = gm.group_id;
2148
2149 open_vconn(argv[1], &vconn);
2150 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2151 group_id);
2152 if (request) {
2153 dump_stats_transaction(vconn, request);
2154 }
2155
2156 vconn_close(vconn);
2157 }
2158
2159 static void
2160 ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[])
2161 {
2162 struct ofpbuf *request;
2163 struct vconn *vconn;
2164
2165 open_vconn(argv[1], &vconn);
2166
2167 request = ofputil_encode_group_desc_request(vconn_get_version(vconn));
2168 if (request) {
2169 dump_stats_transaction(vconn, request);
2170 }
2171
2172 vconn_close(vconn);
2173 }
2174
2175 static void
2176 ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[])
2177 {
2178 struct ofpbuf *request;
2179 struct vconn *vconn;
2180
2181 open_vconn(argv[1], &vconn);
2182 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2183 if (request) {
2184 dump_stats_transaction(vconn, request);
2185 }
2186
2187 vconn_close(vconn);
2188 }
2189
2190 static void
2191 ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2192 {
2193 usage();
2194 }
2195 \f
2196 /* replace-flows and diff-flows commands. */
2197
2198 /* A flow table entry, possibly with two different versions. */
2199 struct fte {
2200 struct cls_rule rule; /* Within a "struct classifier". */
2201 struct fte_version *versions[2];
2202 };
2203
2204 /* One version of a Flow Table Entry. */
2205 struct fte_version {
2206 ovs_be64 cookie;
2207 uint16_t idle_timeout;
2208 uint16_t hard_timeout;
2209 uint16_t flags;
2210 struct ofpact *ofpacts;
2211 size_t ofpacts_len;
2212 };
2213
2214 /* Frees 'version' and the data that it owns. */
2215 static void
2216 fte_version_free(struct fte_version *version)
2217 {
2218 if (version) {
2219 free(CONST_CAST(struct ofpact *, version->ofpacts));
2220 free(version);
2221 }
2222 }
2223
2224 /* Returns true if 'a' and 'b' are the same, false if they differ.
2225 *
2226 * Ignores differences in 'flags' because there's no way to retrieve flags from
2227 * an OpenFlow switch. We have to assume that they are the same. */
2228 static bool
2229 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2230 {
2231 return (a->cookie == b->cookie
2232 && a->idle_timeout == b->idle_timeout
2233 && a->hard_timeout == b->hard_timeout
2234 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2235 b->ofpacts, b->ofpacts_len));
2236 }
2237
2238 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2239 * 'index' into 's', followed by a new-line. */
2240 static void
2241 fte_version_format(const struct fte *fte, int index, struct ds *s)
2242 {
2243 const struct fte_version *version = fte->versions[index];
2244
2245 ds_clear(s);
2246 if (!version) {
2247 return;
2248 }
2249
2250 cls_rule_format(&fte->rule, s);
2251 if (version->cookie != htonll(0)) {
2252 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2253 }
2254 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2255 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2256 }
2257 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2258 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2259 }
2260
2261 ds_put_cstr(s, " actions=");
2262 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2263
2264 ds_put_char(s, '\n');
2265 }
2266
2267 static struct fte *
2268 fte_from_cls_rule(const struct cls_rule *cls_rule)
2269 {
2270 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2271 }
2272
2273 /* Frees 'fte' and its versions. */
2274 static void
2275 fte_free(struct fte *fte)
2276 {
2277 if (fte) {
2278 fte_version_free(fte->versions[0]);
2279 fte_version_free(fte->versions[1]);
2280 cls_rule_destroy(&fte->rule);
2281 free(fte);
2282 }
2283 }
2284
2285 /* Frees all of the FTEs within 'cls'. */
2286 static void
2287 fte_free_all(struct classifier *cls)
2288 {
2289 struct cls_cursor cursor;
2290 struct fte *fte, *next;
2291
2292 fat_rwlock_wrlock(&cls->rwlock);
2293 cls_cursor_init(&cursor, cls, NULL);
2294 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
2295 classifier_remove(cls, &fte->rule);
2296 fte_free(fte);
2297 }
2298 fat_rwlock_unlock(&cls->rwlock);
2299 classifier_destroy(cls);
2300 }
2301
2302 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2303 * necessary. Sets 'version' as the version of that rule with the given
2304 * 'index', replacing any existing version, if any.
2305 *
2306 * Takes ownership of 'version'. */
2307 static void
2308 fte_insert(struct classifier *cls, const struct match *match,
2309 unsigned int priority, struct fte_version *version, int index)
2310 {
2311 struct fte *old, *fte;
2312
2313 fte = xzalloc(sizeof *fte);
2314 cls_rule_init(&fte->rule, match, priority);
2315 fte->versions[index] = version;
2316
2317 fat_rwlock_wrlock(&cls->rwlock);
2318 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
2319 fat_rwlock_unlock(&cls->rwlock);
2320 if (old) {
2321 fte_version_free(old->versions[index]);
2322 fte->versions[!index] = old->versions[!index];
2323 cls_rule_destroy(&old->rule);
2324 free(old);
2325 }
2326 }
2327
2328 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2329 * with the specified 'index'. Returns the flow formats able to represent the
2330 * flows that were read. */
2331 static enum ofputil_protocol
2332 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2333 {
2334 enum ofputil_protocol usable_protocols;
2335 int line_number;
2336 struct ds s;
2337 FILE *file;
2338
2339 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2340 if (file == NULL) {
2341 ovs_fatal(errno, "%s: open", filename);
2342 }
2343
2344 ds_init(&s);
2345 usable_protocols = OFPUTIL_P_ANY;
2346 line_number = 0;
2347 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2348 struct fte_version *version;
2349 struct ofputil_flow_mod fm;
2350 char *error;
2351 enum ofputil_protocol usable;
2352
2353 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2354 if (error) {
2355 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2356 }
2357 usable_protocols &= usable;
2358
2359 version = xmalloc(sizeof *version);
2360 version->cookie = fm.new_cookie;
2361 version->idle_timeout = fm.idle_timeout;
2362 version->hard_timeout = fm.hard_timeout;
2363 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2364 | OFPUTIL_FF_EMERG);
2365 version->ofpacts = fm.ofpacts;
2366 version->ofpacts_len = fm.ofpacts_len;
2367
2368 fte_insert(cls, &fm.match, fm.priority, version, index);
2369 }
2370 ds_destroy(&s);
2371
2372 if (file != stdin) {
2373 fclose(file);
2374 }
2375
2376 return usable_protocols;
2377 }
2378
2379 static bool
2380 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2381 struct ofpbuf **replyp,
2382 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2383 {
2384 struct ofpbuf *reply = *replyp;
2385
2386 for (;;) {
2387 int retval;
2388 bool more;
2389
2390 /* Get a flow stats reply message, if we don't already have one. */
2391 if (!reply) {
2392 enum ofptype type;
2393 enum ofperr error;
2394
2395 do {
2396 run(vconn_recv_block(vconn, &reply),
2397 "OpenFlow packet receive failed");
2398 } while (((struct ofp_header *) ofpbuf_data(reply))->xid != send_xid);
2399
2400 error = ofptype_decode(&type, ofpbuf_data(reply));
2401 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2402 ovs_fatal(0, "received bad reply: %s",
2403 ofp_to_string(ofpbuf_data(reply), ofpbuf_size(reply),
2404 verbosity + 1));
2405 }
2406 }
2407
2408 /* Pull an individual flow stats reply out of the message. */
2409 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2410 switch (retval) {
2411 case 0:
2412 *replyp = reply;
2413 return true;
2414
2415 case EOF:
2416 more = ofpmp_more(reply->frame);
2417 ofpbuf_delete(reply);
2418 reply = NULL;
2419 if (!more) {
2420 *replyp = NULL;
2421 return false;
2422 }
2423 break;
2424
2425 default:
2426 ovs_fatal(0, "parse error in reply (%s)",
2427 ofperr_to_string(retval));
2428 }
2429 }
2430 }
2431
2432 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2433 * format 'protocol', and adds them as flow table entries in 'cls' for the
2434 * version with the specified 'index'. */
2435 static void
2436 read_flows_from_switch(struct vconn *vconn,
2437 enum ofputil_protocol protocol,
2438 struct classifier *cls, int index)
2439 {
2440 struct ofputil_flow_stats_request fsr;
2441 struct ofputil_flow_stats fs;
2442 struct ofpbuf *request;
2443 struct ofpbuf ofpacts;
2444 struct ofpbuf *reply;
2445 ovs_be32 send_xid;
2446
2447 fsr.aggregate = false;
2448 match_init_catchall(&fsr.match);
2449 fsr.out_port = OFPP_ANY;
2450 fsr.table_id = 0xff;
2451 fsr.cookie = fsr.cookie_mask = htonll(0);
2452 request = ofputil_encode_flow_stats_request(&fsr, protocol);
2453 send_xid = ((struct ofp_header *) ofpbuf_data(request))->xid;
2454 send_openflow_buffer(vconn, request);
2455
2456 reply = NULL;
2457 ofpbuf_init(&ofpacts, 0);
2458 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2459 struct fte_version *version;
2460
2461 version = xmalloc(sizeof *version);
2462 version->cookie = fs.cookie;
2463 version->idle_timeout = fs.idle_timeout;
2464 version->hard_timeout = fs.hard_timeout;
2465 version->flags = 0;
2466 version->ofpacts_len = fs.ofpacts_len;
2467 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2468
2469 fte_insert(cls, &fs.match, fs.priority, version, index);
2470 }
2471 ofpbuf_uninit(&ofpacts);
2472 }
2473
2474 static void
2475 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2476 enum ofputil_protocol protocol, struct list *packets)
2477 {
2478 const struct fte_version *version = fte->versions[index];
2479 struct ofputil_flow_mod fm;
2480 struct ofpbuf *ofm;
2481
2482 minimatch_expand(&fte->rule.match, &fm.match);
2483 fm.priority = fte->rule.priority;
2484 fm.cookie = htonll(0);
2485 fm.cookie_mask = htonll(0);
2486 fm.new_cookie = version->cookie;
2487 fm.modify_cookie = true;
2488 fm.table_id = 0xff;
2489 fm.command = command;
2490 fm.idle_timeout = version->idle_timeout;
2491 fm.hard_timeout = version->hard_timeout;
2492 fm.buffer_id = UINT32_MAX;
2493 fm.out_port = OFPP_ANY;
2494 fm.flags = version->flags;
2495 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2496 command == OFPFC_MODIFY_STRICT) {
2497 fm.ofpacts = version->ofpacts;
2498 fm.ofpacts_len = version->ofpacts_len;
2499 } else {
2500 fm.ofpacts = NULL;
2501 fm.ofpacts_len = 0;
2502 }
2503
2504 ofm = ofputil_encode_flow_mod(&fm, protocol);
2505 list_push_back(packets, &ofm->list_node);
2506 }
2507
2508 static void
2509 ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
2510 {
2511 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2512 enum ofputil_protocol usable_protocols, protocol;
2513 struct cls_cursor cursor;
2514 struct classifier cls;
2515 struct list requests;
2516 struct vconn *vconn;
2517 struct fte *fte;
2518
2519 classifier_init(&cls, NULL);
2520 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
2521
2522 protocol = open_vconn(argv[1], &vconn);
2523 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2524
2525 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2526
2527 list_init(&requests);
2528
2529 /* Delete flows that exist on the switch but not in the file. */
2530 fat_rwlock_rdlock(&cls.rwlock);
2531 cls_cursor_init(&cursor, &cls, NULL);
2532 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2533 struct fte_version *file_ver = fte->versions[FILE_IDX];
2534 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2535
2536 if (sw_ver && !file_ver) {
2537 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2538 protocol, &requests);
2539 }
2540 }
2541
2542 /* Add flows that exist in the file but not on the switch.
2543 * Update flows that exist in both places but differ. */
2544 cls_cursor_init(&cursor, &cls, NULL);
2545 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2546 struct fte_version *file_ver = fte->versions[FILE_IDX];
2547 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2548
2549 if (file_ver
2550 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2551 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2552 }
2553 }
2554 fat_rwlock_unlock(&cls.rwlock);
2555 transact_multiple_noreply(vconn, &requests);
2556 vconn_close(vconn);
2557
2558 fte_free_all(&cls);
2559 }
2560
2561 static void
2562 read_flows_from_source(const char *source, struct classifier *cls, int index)
2563 {
2564 struct stat s;
2565
2566 if (source[0] == '/' || source[0] == '.'
2567 || (!strchr(source, ':') && !stat(source, &s))) {
2568 read_flows_from_file(source, cls, index);
2569 } else {
2570 enum ofputil_protocol protocol;
2571 struct vconn *vconn;
2572
2573 protocol = open_vconn(source, &vconn);
2574 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2575 read_flows_from_switch(vconn, protocol, cls, index);
2576 vconn_close(vconn);
2577 }
2578 }
2579
2580 static void
2581 ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
2582 {
2583 bool differences = false;
2584 struct cls_cursor cursor;
2585 struct classifier cls;
2586 struct ds a_s, b_s;
2587 struct fte *fte;
2588
2589 classifier_init(&cls, NULL);
2590 read_flows_from_source(argv[1], &cls, 0);
2591 read_flows_from_source(argv[2], &cls, 1);
2592
2593 ds_init(&a_s);
2594 ds_init(&b_s);
2595
2596 fat_rwlock_rdlock(&cls.rwlock);
2597 cls_cursor_init(&cursor, &cls, NULL);
2598 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
2599 struct fte_version *a = fte->versions[0];
2600 struct fte_version *b = fte->versions[1];
2601
2602 if (!a || !b || !fte_version_equals(a, b)) {
2603 fte_version_format(fte, 0, &a_s);
2604 fte_version_format(fte, 1, &b_s);
2605 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2606 if (a_s.length) {
2607 printf("-%s", ds_cstr(&a_s));
2608 }
2609 if (b_s.length) {
2610 printf("+%s", ds_cstr(&b_s));
2611 }
2612 differences = true;
2613 }
2614 }
2615 }
2616 fat_rwlock_unlock(&cls.rwlock);
2617
2618 ds_destroy(&a_s);
2619 ds_destroy(&b_s);
2620
2621 fte_free_all(&cls);
2622
2623 if (differences) {
2624 exit(2);
2625 }
2626 }
2627
2628 static void
2629 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2630 {
2631 struct ofputil_meter_mod mm;
2632 struct vconn *vconn;
2633 enum ofputil_protocol protocol;
2634 enum ofputil_protocol usable_protocols;
2635 enum ofp_version version;
2636
2637 if (str) {
2638 char *error;
2639 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2640 if (error) {
2641 ovs_fatal(0, "%s", error);
2642 }
2643 } else {
2644 usable_protocols = OFPUTIL_P_OF13_UP;
2645 mm.command = command;
2646 mm.meter.meter_id = OFPM13_ALL;
2647 }
2648
2649 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2650 version = ofputil_protocol_to_ofp_version(protocol);
2651 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2652 vconn_close(vconn);
2653 }
2654
2655 static void
2656 ofctl_meter_request__(const char *bridge, const char *str,
2657 enum ofputil_meter_request_type type)
2658 {
2659 struct ofputil_meter_mod mm;
2660 struct vconn *vconn;
2661 enum ofputil_protocol usable_protocols;
2662 enum ofputil_protocol protocol;
2663 enum ofp_version version;
2664
2665 if (str) {
2666 char *error;
2667 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2668 if (error) {
2669 ovs_fatal(0, "%s", error);
2670 }
2671 } else {
2672 usable_protocols = OFPUTIL_P_OF13_UP;
2673 mm.meter.meter_id = OFPM13_ALL;
2674 }
2675
2676 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2677 version = ofputil_protocol_to_ofp_version(protocol);
2678 transact_noreply(vconn, ofputil_encode_meter_request(version,
2679 type,
2680 mm.meter.meter_id));
2681 vconn_close(vconn);
2682 }
2683
2684
2685 static void
2686 ofctl_add_meter(int argc OVS_UNUSED, char *argv[])
2687 {
2688 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD);
2689 }
2690
2691 static void
2692 ofctl_mod_meter(int argc OVS_UNUSED, char *argv[])
2693 {
2694 ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY);
2695 }
2696
2697 static void
2698 ofctl_del_meters(int argc, char *argv[])
2699 {
2700 ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE);
2701 }
2702
2703 static void
2704 ofctl_dump_meters(int argc, char *argv[])
2705 {
2706 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2707 OFPUTIL_METER_CONFIG);
2708 }
2709
2710 static void
2711 ofctl_meter_stats(int argc, char *argv[])
2712 {
2713 ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL,
2714 OFPUTIL_METER_STATS);
2715 }
2716
2717 static void
2718 ofctl_meter_features(int argc OVS_UNUSED, char *argv[])
2719 {
2720 ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES);
2721 }
2722
2723 \f
2724 /* Undocumented commands for unit testing. */
2725
2726 static void
2727 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2728 enum ofputil_protocol usable_protocols)
2729 {
2730 enum ofputil_protocol protocol = 0;
2731 char *usable_s;
2732 size_t i;
2733
2734 usable_s = ofputil_protocols_to_string(usable_protocols);
2735 printf("usable protocols: %s\n", usable_s);
2736 free(usable_s);
2737
2738 if (!(usable_protocols & allowed_protocols)) {
2739 ovs_fatal(0, "no usable protocol");
2740 }
2741 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2742 protocol = 1 << i;
2743 if (protocol & usable_protocols & allowed_protocols) {
2744 break;
2745 }
2746 }
2747 ovs_assert(is_pow2(protocol));
2748
2749 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2750
2751 for (i = 0; i < n_fms; i++) {
2752 struct ofputil_flow_mod *fm = &fms[i];
2753 struct ofpbuf *msg;
2754
2755 msg = ofputil_encode_flow_mod(fm, protocol);
2756 ofp_print(stdout, ofpbuf_data(msg), ofpbuf_size(msg), verbosity);
2757 ofpbuf_delete(msg);
2758
2759 free(CONST_CAST(struct ofpact *, fm->ofpacts));
2760 }
2761 }
2762
2763 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2764 * it back to stdout. */
2765 static void
2766 ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
2767 {
2768 enum ofputil_protocol usable_protocols;
2769 struct ofputil_flow_mod fm;
2770 char *error;
2771
2772 error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols);
2773 if (error) {
2774 ovs_fatal(0, "%s", error);
2775 }
2776 ofctl_parse_flows__(&fm, 1, usable_protocols);
2777 }
2778
2779 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2780 * add-flows) and prints each of the flows back to stdout. */
2781 static void
2782 ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
2783 {
2784 enum ofputil_protocol usable_protocols;
2785 struct ofputil_flow_mod *fms = NULL;
2786 size_t n_fms = 0;
2787 char *error;
2788
2789 error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms,
2790 &usable_protocols);
2791 if (error) {
2792 ovs_fatal(0, "%s", error);
2793 }
2794 ofctl_parse_flows__(fms, n_fms, usable_protocols);
2795 free(fms);
2796 }
2797
2798 static void
2799 ofctl_parse_nxm__(bool oxm)
2800 {
2801 struct ds in;
2802
2803 ds_init(&in);
2804 while (!ds_get_test_line(&in, stdin)) {
2805 struct ofpbuf nx_match;
2806 struct match match;
2807 ovs_be64 cookie, cookie_mask;
2808 enum ofperr error;
2809 int match_len;
2810
2811 /* Convert string to nx_match. */
2812 ofpbuf_init(&nx_match, 0);
2813 if (oxm) {
2814 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2815 } else {
2816 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2817 }
2818
2819 /* Convert nx_match to match. */
2820 if (strict) {
2821 if (oxm) {
2822 error = oxm_pull_match(&nx_match, &match);
2823 } else {
2824 error = nx_pull_match(&nx_match, match_len, &match,
2825 &cookie, &cookie_mask);
2826 }
2827 } else {
2828 if (oxm) {
2829 error = oxm_pull_match_loose(&nx_match, &match);
2830 } else {
2831 error = nx_pull_match_loose(&nx_match, match_len, &match,
2832 &cookie, &cookie_mask);
2833 }
2834 }
2835
2836
2837 if (!error) {
2838 char *out;
2839
2840 /* Convert match back to nx_match. */
2841 ofpbuf_uninit(&nx_match);
2842 ofpbuf_init(&nx_match, 0);
2843 if (oxm) {
2844 match_len = oxm_put_match(&nx_match, &match);
2845 out = oxm_match_to_string(&nx_match, match_len);
2846 } else {
2847 match_len = nx_put_match(&nx_match, &match,
2848 cookie, cookie_mask);
2849 out = nx_match_to_string(ofpbuf_data(&nx_match), match_len);
2850 }
2851
2852 puts(out);
2853 free(out);
2854 } else {
2855 printf("nx_pull_match() returned error %s\n",
2856 ofperr_get_name(error));
2857 }
2858
2859 ofpbuf_uninit(&nx_match);
2860 }
2861 ds_destroy(&in);
2862 }
2863
2864 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2865 * stdin, does some internal fussing with them, and then prints them back as
2866 * strings on stdout. */
2867 static void
2868 ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2869 {
2870 return ofctl_parse_nxm__(false);
2871 }
2872
2873 /* "parse-oxm": reads a series of OXM nx_match specifications as strings from
2874 * stdin, does some internal fussing with them, and then prints them back as
2875 * strings on stdout. */
2876 static void
2877 ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2878 {
2879 return ofctl_parse_nxm__(true);
2880 }
2881
2882 static void
2883 print_differences(const char *prefix,
2884 const void *a_, size_t a_len,
2885 const void *b_, size_t b_len)
2886 {
2887 const uint8_t *a = a_;
2888 const uint8_t *b = b_;
2889 size_t i;
2890
2891 for (i = 0; i < MIN(a_len, b_len); i++) {
2892 if (a[i] != b[i]) {
2893 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2894 prefix, i, a[i], b[i]);
2895 }
2896 }
2897 for (i = a_len; i < b_len; i++) {
2898 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2899 }
2900 for (i = b_len; i < a_len; i++) {
2901 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2902 }
2903 }
2904
2905 /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
2906 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
2907 * on stdout, and then converts them back to hex bytes and prints any
2908 * differences from the input. */
2909 static void
2910 ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2911 {
2912 struct ds in;
2913
2914 ds_init(&in);
2915 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2916 struct ofpbuf of10_out;
2917 struct ofpbuf of10_in;
2918 struct ofpbuf ofpacts;
2919 enum ofperr error;
2920 size_t size;
2921 struct ds s;
2922
2923 /* Parse hex bytes. */
2924 ofpbuf_init(&of10_in, 0);
2925 if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
2926 ovs_fatal(0, "Trailing garbage in hex data");
2927 }
2928
2929 /* Convert to ofpacts. */
2930 ofpbuf_init(&ofpacts, 0);
2931 size = ofpbuf_size(&of10_in);
2932 error = ofpacts_pull_openflow_actions(&of10_in, ofpbuf_size(&of10_in),
2933 OFP10_VERSION, &ofpacts);
2934 if (error) {
2935 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
2936 ofpbuf_uninit(&ofpacts);
2937 ofpbuf_uninit(&of10_in);
2938 continue;
2939 }
2940 ofpbuf_push_uninit(&of10_in, size);
2941
2942 /* Print cls_rule. */
2943 ds_init(&s);
2944 ds_put_cstr(&s, "actions=");
2945 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
2946 puts(ds_cstr(&s));
2947 ds_destroy(&s);
2948
2949 /* Convert back to ofp10 actions and print differences from input. */
2950 ofpbuf_init(&of10_out, 0);
2951 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of10_out,
2952 OFP10_VERSION);
2953
2954 print_differences("", ofpbuf_data(&of10_in), ofpbuf_size(&of10_in),
2955 ofpbuf_data(&of10_out), ofpbuf_size(&of10_out));
2956 putchar('\n');
2957
2958 ofpbuf_uninit(&ofpacts);
2959 ofpbuf_uninit(&of10_in);
2960 ofpbuf_uninit(&of10_out);
2961 }
2962 ds_destroy(&in);
2963 }
2964
2965 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
2966 * bytes from stdin, converts them to cls_rules, prints them as strings on
2967 * stdout, and then converts them back to hex bytes and prints any differences
2968 * from the input.
2969 *
2970 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
2971 * values are ignored in the input and will be set to zero when OVS converts
2972 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
2973 * it does the conversion to hex, to ensure that in fact they are ignored. */
2974 static void
2975 ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
2976 {
2977 struct ds expout;
2978 struct ds in;
2979
2980 ds_init(&in);
2981 ds_init(&expout);
2982 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
2983 struct ofpbuf match_in, match_expout;
2984 struct ofp10_match match_out;
2985 struct ofp10_match match_normal;
2986 struct match match;
2987 char *p;
2988
2989 /* Parse hex bytes to use for expected output. */
2990 ds_clear(&expout);
2991 ds_put_cstr(&expout, ds_cstr(&in));
2992 for (p = ds_cstr(&expout); *p; p++) {
2993 if (*p == 'x') {
2994 *p = '0';
2995 }
2996 }
2997 ofpbuf_init(&match_expout, 0);
2998 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
2999 ovs_fatal(0, "Trailing garbage in hex data");
3000 }
3001 if (ofpbuf_size(&match_expout) != sizeof(struct ofp10_match)) {
3002 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3003 ofpbuf_size(&match_expout), sizeof(struct ofp10_match));
3004 }
3005
3006 /* Parse hex bytes for input. */
3007 for (p = ds_cstr(&in); *p; p++) {
3008 if (*p == 'x') {
3009 *p = "0123456789abcdef"[random_uint32() & 0xf];
3010 }
3011 }
3012 ofpbuf_init(&match_in, 0);
3013 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3014 ovs_fatal(0, "Trailing garbage in hex data");
3015 }
3016 if (ofpbuf_size(&match_in) != sizeof(struct ofp10_match)) {
3017 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3018 ofpbuf_size(&match_in), sizeof(struct ofp10_match));
3019 }
3020
3021 /* Convert to cls_rule and print. */
3022 ofputil_match_from_ofp10_match(ofpbuf_data(&match_in), &match);
3023 match_print(&match);
3024
3025 /* Convert back to ofp10_match and print differences from input. */
3026 ofputil_match_to_ofp10_match(&match, &match_out);
3027 print_differences("", ofpbuf_data(&match_expout), ofpbuf_size(&match_expout),
3028 &match_out, sizeof match_out);
3029
3030 /* Normalize, then convert and compare again. */
3031 ofputil_normalize_match(&match);
3032 ofputil_match_to_ofp10_match(&match, &match_normal);
3033 print_differences("normal: ", &match_out, sizeof match_out,
3034 &match_normal, sizeof match_normal);
3035 putchar('\n');
3036
3037 ofpbuf_uninit(&match_in);
3038 ofpbuf_uninit(&match_expout);
3039 }
3040 ds_destroy(&in);
3041 ds_destroy(&expout);
3042 }
3043
3044 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3045 * bytes from stdin, converts them to "struct match"es, prints them as strings
3046 * on stdout, and then converts them back to hex bytes and prints any
3047 * differences from the input. */
3048 static void
3049 ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3050 {
3051 struct ds in;
3052
3053 ds_init(&in);
3054 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3055 struct ofpbuf match_in;
3056 struct ofp11_match match_out;
3057 struct match match;
3058 enum ofperr error;
3059
3060 /* Parse hex bytes. */
3061 ofpbuf_init(&match_in, 0);
3062 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3063 ovs_fatal(0, "Trailing garbage in hex data");
3064 }
3065 if (ofpbuf_size(&match_in) != sizeof(struct ofp11_match)) {
3066 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3067 ofpbuf_size(&match_in), sizeof(struct ofp11_match));
3068 }
3069
3070 /* Convert to match. */
3071 error = ofputil_match_from_ofp11_match(ofpbuf_data(&match_in), &match);
3072 if (error) {
3073 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3074 ofpbuf_uninit(&match_in);
3075 continue;
3076 }
3077
3078 /* Print match. */
3079 match_print(&match);
3080
3081 /* Convert back to ofp11_match and print differences from input. */
3082 ofputil_match_to_ofp11_match(&match, &match_out);
3083
3084 print_differences("", ofpbuf_data(&match_in), ofpbuf_size(&match_in),
3085 &match_out, sizeof match_out);
3086 putchar('\n');
3087
3088 ofpbuf_uninit(&match_in);
3089 }
3090 ds_destroy(&in);
3091 }
3092
3093 /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
3094 * as hex bytes from stdin, converts them to ofpacts, prints them as strings
3095 * on stdout, and then converts them back to hex bytes and prints any
3096 * differences from the input. */
3097 static void
3098 ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3099 {
3100 struct ds in;
3101
3102 ds_init(&in);
3103 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3104 struct ofpbuf of11_out;
3105 struct ofpbuf of11_in;
3106 struct ofpbuf ofpacts;
3107 enum ofperr error;
3108 size_t size;
3109 struct ds s;
3110
3111 /* Parse hex bytes. */
3112 ofpbuf_init(&of11_in, 0);
3113 if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
3114 ovs_fatal(0, "Trailing garbage in hex data");
3115 }
3116
3117 /* Convert to ofpacts. */
3118 ofpbuf_init(&ofpacts, 0);
3119 size = ofpbuf_size(&of11_in);
3120 error = ofpacts_pull_openflow_actions(&of11_in, ofpbuf_size(&of11_in),
3121 OFP11_VERSION, &ofpacts);
3122 if (error) {
3123 printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
3124 ofpbuf_uninit(&ofpacts);
3125 ofpbuf_uninit(&of11_in);
3126 continue;
3127 }
3128 ofpbuf_push_uninit(&of11_in, size);
3129
3130 /* Print cls_rule. */
3131 ds_init(&s);
3132 ds_put_cstr(&s, "actions=");
3133 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3134 puts(ds_cstr(&s));
3135 ds_destroy(&s);
3136
3137 /* Convert back to ofp11 actions and print differences from input. */
3138 ofpbuf_init(&of11_out, 0);
3139 ofpacts_put_openflow_actions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &of11_out,
3140 OFP11_VERSION);
3141
3142 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3143 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3144 putchar('\n');
3145
3146 ofpbuf_uninit(&ofpacts);
3147 ofpbuf_uninit(&of11_in);
3148 ofpbuf_uninit(&of11_out);
3149 }
3150 ds_destroy(&in);
3151 }
3152
3153 /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
3154 * specifications as hex bytes from stdin, converts them to ofpacts, prints
3155 * them as strings on stdout, and then converts them back to hex bytes and
3156 * prints any differences from the input. */
3157 static void
3158 ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
3159 {
3160 struct ds in;
3161
3162 ds_init(&in);
3163 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3164 struct ofpbuf of11_out;
3165 struct ofpbuf of11_in;
3166 struct ofpbuf ofpacts;
3167 enum ofperr error;
3168 size_t size;
3169 struct ds s;
3170 const char *table_id;
3171 char *instructions;
3172
3173 /* Parse table_id separated with the follow-up instructions by ",", if
3174 * any. */
3175 instructions = ds_cstr(&in);
3176 table_id = NULL;
3177 if (strstr(instructions, ",")) {
3178 table_id = strsep(&instructions, ",");
3179 }
3180
3181 /* Parse hex bytes. */
3182 ofpbuf_init(&of11_in, 0);
3183 if (ofpbuf_put_hex(&of11_in, instructions, NULL)[0] != '\0') {
3184 ovs_fatal(0, "Trailing garbage in hex data");
3185 }
3186
3187 /* Convert to ofpacts. */
3188 ofpbuf_init(&ofpacts, 0);
3189 size = ofpbuf_size(&of11_in);
3190 error = ofpacts_pull_openflow_instructions(&of11_in, ofpbuf_size(&of11_in),
3191 OFP11_VERSION, &ofpacts);
3192 if (!error) {
3193 /* Verify actions, enforce consistency. */
3194 struct flow flow;
3195 memset(&flow, 0, sizeof flow);
3196 error = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3197 &flow, OFPP_MAX,
3198 table_id ? atoi(table_id) : 0,
3199 255, OFPUTIL_P_OF11_STD);
3200 }
3201 if (error) {
3202 printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
3203 ofpbuf_uninit(&ofpacts);
3204 ofpbuf_uninit(&of11_in);
3205 continue;
3206 }
3207 ofpbuf_push_uninit(&of11_in, size);
3208
3209 /* Print cls_rule. */
3210 ds_init(&s);
3211 ds_put_cstr(&s, "actions=");
3212 ofpacts_format(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &s);
3213 puts(ds_cstr(&s));
3214 ds_destroy(&s);
3215
3216 /* Convert back to ofp11 instructions and print differences from
3217 * input. */
3218 ofpbuf_init(&of11_out, 0);
3219 ofpacts_put_openflow_instructions(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
3220 &of11_out, OFP13_VERSION);
3221
3222 print_differences("", ofpbuf_data(&of11_in), ofpbuf_size(&of11_in),
3223 ofpbuf_data(&of11_out), ofpbuf_size(&of11_out));
3224 putchar('\n');
3225
3226 ofpbuf_uninit(&ofpacts);
3227 ofpbuf_uninit(&of11_in);
3228 ofpbuf_uninit(&of11_out);
3229 }
3230 ds_destroy(&in);
3231 }
3232
3233 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3234 static void
3235 ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[])
3236 {
3237 FILE *pcap;
3238
3239 pcap = ovs_pcap_open(argv[1], "rb");
3240 if (!pcap) {
3241 ovs_fatal(errno, "%s: open failed", argv[1]);
3242 }
3243
3244 for (;;) {
3245 struct ofpbuf *packet;
3246 struct flow flow;
3247 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3248 int error;
3249
3250 error = ovs_pcap_read(pcap, &packet, NULL);
3251 if (error == EOF) {
3252 break;
3253 } else if (error) {
3254 ovs_fatal(error, "%s: read failed", argv[1]);
3255 }
3256
3257 flow_extract(packet, &md, &flow);
3258 flow_print(stdout, &flow);
3259 putchar('\n');
3260 ofpbuf_delete(packet);
3261 }
3262 }
3263
3264 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3265 * mask values to and from various formats and prints the results. */
3266 static void
3267 ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
3268 {
3269 struct match match;
3270
3271 char *string_s;
3272 struct ofputil_flow_mod fm;
3273
3274 struct ofpbuf nxm;
3275 struct match nxm_match;
3276 int nxm_match_len;
3277 char *nxm_s;
3278
3279 struct ofp10_match of10_raw;
3280 struct match of10_match;
3281
3282 struct ofp11_match of11_raw;
3283 struct match of11_match;
3284
3285 enum ofperr error;
3286 char *error_s;
3287
3288 enum ofputil_protocol usable_protocols; /* Unused for now. */
3289
3290 match_init_catchall(&match);
3291 match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
3292 match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16));
3293
3294 /* Convert to and from string. */
3295 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3296 printf("%s -> ", string_s);
3297 fflush(stdout);
3298 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3299 if (error_s) {
3300 ovs_fatal(0, "%s", error_s);
3301 }
3302 printf("%04"PRIx16"/%04"PRIx16"\n",
3303 ntohs(fm.match.flow.vlan_tci),
3304 ntohs(fm.match.wc.masks.vlan_tci));
3305 free(string_s);
3306
3307 /* Convert to and from NXM. */
3308 ofpbuf_init(&nxm, 0);
3309 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3310 nxm_s = nx_match_to_string(ofpbuf_data(&nxm), nxm_match_len);
3311 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3312 printf("NXM: %s -> ", nxm_s);
3313 if (error) {
3314 printf("%s\n", ofperr_to_string(error));
3315 } else {
3316 printf("%04"PRIx16"/%04"PRIx16"\n",
3317 ntohs(nxm_match.flow.vlan_tci),
3318 ntohs(nxm_match.wc.masks.vlan_tci));
3319 }
3320 free(nxm_s);
3321 ofpbuf_uninit(&nxm);
3322
3323 /* Convert to and from OXM. */
3324 ofpbuf_init(&nxm, 0);
3325 nxm_match_len = oxm_put_match(&nxm, &match);
3326 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3327 error = oxm_pull_match(&nxm, &nxm_match);
3328 printf("OXM: %s -> ", nxm_s);
3329 if (error) {
3330 printf("%s\n", ofperr_to_string(error));
3331 } else {
3332 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3333 (VLAN_VID_MASK | VLAN_CFI);
3334 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3335 (VLAN_VID_MASK | VLAN_CFI);
3336
3337 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3338 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3339 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3340 } else {
3341 printf("--\n");
3342 }
3343 }
3344 free(nxm_s);
3345 ofpbuf_uninit(&nxm);
3346
3347 /* Convert to and from OpenFlow 1.0. */
3348 ofputil_match_to_ofp10_match(&match, &of10_raw);
3349 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3350 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3351 ntohs(of10_raw.dl_vlan),
3352 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3353 of10_raw.dl_vlan_pcp,
3354 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3355 ntohs(of10_match.flow.vlan_tci),
3356 ntohs(of10_match.wc.masks.vlan_tci));
3357
3358 /* Convert to and from OpenFlow 1.1. */
3359 ofputil_match_to_ofp11_match(&match, &of11_raw);
3360 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3361 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3362 ntohs(of11_raw.dl_vlan),
3363 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3364 of11_raw.dl_vlan_pcp,
3365 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3366 ntohs(of11_match.flow.vlan_tci),
3367 ntohs(of11_match.wc.masks.vlan_tci));
3368 }
3369
3370 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3371 * version. */
3372 static void
3373 ofctl_print_error(int argc OVS_UNUSED, char *argv[])
3374 {
3375 enum ofperr error;
3376 int version;
3377
3378 error = ofperr_from_name(argv[1]);
3379 if (!error) {
3380 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3381 }
3382
3383 for (version = 0; version <= UINT8_MAX; version++) {
3384 const char *name = ofperr_domain_get_name(version);
3385 if (name) {
3386 int vendor = ofperr_get_vendor(error, version);
3387 int type = ofperr_get_type(error, version);
3388 int code = ofperr_get_code(error, version);
3389
3390 if (vendor != -1 || type != -1 || code != -1) {
3391 printf("%s: vendor %#x, type %d, code %d\n",
3392 name, vendor, type, code);
3393 }
3394 }
3395 }
3396 }
3397
3398 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3399 * error named ENUM and prints the error reply in hex. */
3400 static void
3401 ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[])
3402 {
3403 const struct ofp_header *oh;
3404 struct ofpbuf request, *reply;
3405 enum ofperr error;
3406
3407 error = ofperr_from_name(argv[1]);
3408 if (!error) {
3409 ovs_fatal(0, "unknown error \"%s\"", argv[1]);
3410 }
3411
3412 ofpbuf_init(&request, 0);
3413 if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') {
3414 ovs_fatal(0, "Trailing garbage in hex data");
3415 }
3416 if (ofpbuf_size(&request) < sizeof(struct ofp_header)) {
3417 ovs_fatal(0, "Request too short");
3418 }
3419
3420 oh = ofpbuf_data(&request);
3421 if (ofpbuf_size(&request) != ntohs(oh->length)) {
3422 ovs_fatal(0, "Request size inconsistent");
3423 }
3424
3425 reply = ofperr_encode_reply(error, ofpbuf_data(&request));
3426 ofpbuf_uninit(&request);
3427
3428 ovs_hex_dump(stdout, ofpbuf_data(reply), ofpbuf_size(reply), 0, false);
3429 ofpbuf_delete(reply);
3430 }
3431
3432 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3433 * binary data, interpreting them as an OpenFlow message, and prints the
3434 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
3435 static void
3436 ofctl_ofp_print(int argc, char *argv[])
3437 {
3438 struct ofpbuf packet;
3439
3440 ofpbuf_init(&packet, strlen(argv[1]) / 2);
3441 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
3442 ovs_fatal(0, "trailing garbage following hex bytes");
3443 }
3444 ofp_print(stdout, ofpbuf_data(&packet), ofpbuf_size(&packet), argc > 2 ? atoi(argv[2]) : 2);
3445 ofpbuf_uninit(&packet);
3446 }
3447
3448 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3449 * and dumps each message in hex. */
3450 static void
3451 ofctl_encode_hello(int argc OVS_UNUSED, char *argv[])
3452 {
3453 uint32_t bitmap = strtol(argv[1], NULL, 0);
3454 struct ofpbuf *hello;
3455
3456 hello = ofputil_encode_hello(bitmap);
3457 ovs_hex_dump(stdout, ofpbuf_data(hello), ofpbuf_size(hello), 0, false);
3458 ofp_print(stdout, ofpbuf_data(hello), ofpbuf_size(hello), verbosity);
3459 ofpbuf_delete(hello);
3460 }
3461
3462 static const struct command all_commands[] = {
3463 { "show", 1, 1, ofctl_show },
3464 { "monitor", 1, 3, ofctl_monitor },
3465 { "snoop", 1, 1, ofctl_snoop },
3466 { "dump-desc", 1, 1, ofctl_dump_desc },
3467 { "dump-tables", 1, 1, ofctl_dump_tables },
3468 { "dump-table-features", 1, 1, ofctl_dump_table_features },
3469 { "dump-flows", 1, 2, ofctl_dump_flows },
3470 { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
3471 { "queue-stats", 1, 3, ofctl_queue_stats },
3472 { "queue-get-config", 2, 2, ofctl_queue_get_config },
3473 { "add-flow", 2, 2, ofctl_add_flow },
3474 { "add-flows", 2, 2, ofctl_add_flows },
3475 { "mod-flows", 2, 2, ofctl_mod_flows },
3476 { "del-flows", 1, 2, ofctl_del_flows },
3477 { "replace-flows", 2, 2, ofctl_replace_flows },
3478 { "diff-flows", 2, 2, ofctl_diff_flows },
3479 { "add-meter", 2, 2, ofctl_add_meter },
3480 { "mod-meter", 2, 2, ofctl_mod_meter },
3481 { "del-meter", 2, 2, ofctl_del_meters },
3482 { "del-meters", 1, 1, ofctl_del_meters },
3483 { "dump-meter", 2, 2, ofctl_dump_meters },
3484 { "dump-meters", 1, 1, ofctl_dump_meters },
3485 { "meter-stats", 1, 2, ofctl_meter_stats },
3486 { "meter-features", 1, 1, ofctl_meter_features },
3487 { "packet-out", 4, INT_MAX, ofctl_packet_out },
3488 { "dump-ports", 1, 2, ofctl_dump_ports },
3489 { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
3490 { "mod-port", 3, 3, ofctl_mod_port },
3491 { "mod-table", 3, 3, ofctl_mod_table },
3492 { "get-frags", 1, 1, ofctl_get_frags },
3493 { "set-frags", 2, 2, ofctl_set_frags },
3494 { "probe", 1, 1, ofctl_probe },
3495 { "ping", 1, 2, ofctl_ping },
3496 { "benchmark", 3, 3, ofctl_benchmark },
3497
3498 { "ofp-parse", 1, 1, ofctl_ofp_parse },
3499 { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap },
3500
3501 { "add-group", 1, 2, ofctl_add_group },
3502 { "add-groups", 1, 2, ofctl_add_groups },
3503 { "mod-group", 1, 2, ofctl_mod_group },
3504 { "del-groups", 1, 2, ofctl_del_groups },
3505 { "dump-groups", 1, 1, ofctl_dump_group_desc },
3506 { "dump-group-stats", 1, 2, ofctl_dump_group_stats },
3507 { "dump-group-features", 1, 1, ofctl_dump_group_features },
3508 { "help", 0, INT_MAX, ofctl_help },
3509
3510 /* Undocumented commands for testing. */
3511 { "parse-flow", 1, 1, ofctl_parse_flow },
3512 { "parse-flows", 1, 1, ofctl_parse_flows },
3513 { "parse-nx-match", 0, 0, ofctl_parse_nxm },
3514 { "parse-nxm", 0, 0, ofctl_parse_nxm },
3515 { "parse-oxm", 0, 0, ofctl_parse_oxm },
3516 { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
3517 { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
3518 { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
3519 { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
3520 { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
3521 { "parse-pcap", 1, 1, ofctl_parse_pcap },
3522 { "check-vlan", 2, 2, ofctl_check_vlan },
3523 { "print-error", 1, 1, ofctl_print_error },
3524 { "encode-error-reply", 2, 2, ofctl_encode_error_reply },
3525 { "ofp-print", 1, 2, ofctl_ofp_print },
3526 { "encode-hello", 1, 1, ofctl_encode_hello },
3527
3528 { NULL, 0, 0, NULL },
3529 };
3530
3531 static const struct command *get_all_commands(void)
3532 {
3533 return all_commands;
3534 }