]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-ofctl.c
dpctl: Fix usage for most dpctl commands.
[mirror_ovs.git] / utilities / ovs-ofctl.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 "colors.h"
37 #include "compiler.h"
38 #include "dirs.h"
39 #include "dp-packet.h"
40 #include "fatal-signal.h"
41 #include "nx-match.h"
42 #include "odp-util.h"
43 #include "ofp-version-opt.h"
44 #include "ofproto/ofproto.h"
45 #include "openflow/nicira-ext.h"
46 #include "openflow/openflow.h"
47 #include "openvswitch/dynamic-string.h"
48 #include "openvswitch/meta-flow.h"
49 #include "openvswitch/ofp-actions.h"
50 #include "openvswitch/ofp-errors.h"
51 #include "openvswitch/ofp-msgs.h"
52 #include "openvswitch/ofp-print.h"
53 #include "openvswitch/ofp-util.h"
54 #include "openvswitch/ofp-parse.h"
55 #include "openvswitch/ofpbuf.h"
56 #include "openvswitch/vconn.h"
57 #include "openvswitch/vlog.h"
58 #include "packets.h"
59 #include "pcap-file.h"
60 #include "poll-loop.h"
61 #include "random.h"
62 #include "sort.h"
63 #include "stream-ssl.h"
64 #include "socket-util.h"
65 #include "timeval.h"
66 #include "unixctl.h"
67 #include "util.h"
68
69 VLOG_DEFINE_THIS_MODULE(ofctl);
70
71 /* --bundle: Use OpenFlow 1.3+ bundle for making the flow table change atomic.
72 * NOTE: If OpenFlow 1.3 or higher is not selected with the '-O' option,
73 * OpenFlow 1.4 will be implicitly selected. Also the flow mod will use
74 * OpenFlow 1.4, so the semantics may be different (see the comment in
75 * parse_options() for details).
76 */
77 static bool bundle = false;
78
79 /* --color: Use color markers. */
80 static bool enable_color;
81
82 /* --read-only: Do not execute read only commands. */
83 static bool read_only;
84
85 /* --strict: Use strict matching for flow mod commands? Additionally governs
86 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
87 */
88 static bool strict;
89
90 /* --may-create: If true, the mod-group command creates a group that does not
91 * yet exist; otherwise, such a command has no effect. */
92 static bool may_create;
93
94 /* --readd: If true, on replace-flows, re-add even flows that have not changed
95 * (to reset flow counters). */
96 static bool readd;
97
98 /* -F, --flow-format: Allowed protocols. By default, any protocol is
99 * allowed. */
100 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
101
102 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
103 * commands. Either one of NXPIF_* to force a particular packet_in format, or
104 * -1 to let ovs-ofctl choose the default. */
105 static int preferred_packet_in_format = -1;
106
107 /* -m, --more: Additional verbosity for ofp-print functions. */
108 static int verbosity;
109
110 /* --timestamp: Print a timestamp before each received packet on "monitor" and
111 * "snoop" command? */
112 static bool timestamp;
113
114 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
115 commands. */
116 static char *unixctl_path;
117
118 /* --sort, --rsort: Sort order. */
119 enum sort_order { SORT_ASC, SORT_DESC };
120 struct sort_criterion {
121 const struct mf_field *field; /* NULL means to sort by priority. */
122 enum sort_order order;
123 };
124 static struct sort_criterion *criteria;
125 static size_t n_criteria, allocated_criteria;
126
127 static const struct ovs_cmdl_command *get_all_commands(void);
128
129 OVS_NO_RETURN static void usage(void);
130 static void parse_options(int argc, char *argv[]);
131
132 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
133 struct ofpbuf **replyp,
134 struct ofputil_flow_stats *,
135 struct ofpbuf *ofpacts);
136 int
137 main(int argc, char *argv[])
138 {
139 struct ovs_cmdl_context ctx = { .argc = 0, };
140 set_program_name(argv[0]);
141 service_start(&argc, &argv);
142 parse_options(argc, argv);
143 fatal_ignore_sigpipe();
144 ctx.argc = argc - optind;
145 ctx.argv = argv + optind;
146
147 daemon_become_new_user(false);
148 if (read_only) {
149 ovs_cmdl_run_command_read_only(&ctx, get_all_commands());
150 } else {
151 ovs_cmdl_run_command(&ctx, get_all_commands());
152 }
153 return 0;
154 }
155
156 static void
157 add_sort_criterion(enum sort_order order, const char *field)
158 {
159 struct sort_criterion *sc;
160
161 if (n_criteria >= allocated_criteria) {
162 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
163 }
164
165 sc = &criteria[n_criteria++];
166 if (!field || !strcasecmp(field, "priority")) {
167 sc->field = NULL;
168 } else {
169 sc->field = mf_from_name(field);
170 if (!sc->field) {
171 ovs_fatal(0, "%s: unknown field name", field);
172 }
173 }
174 sc->order = order;
175 }
176
177 static void
178 parse_options(int argc, char *argv[])
179 {
180 enum {
181 OPT_STRICT = UCHAR_MAX + 1,
182 OPT_READD,
183 OPT_TIMESTAMP,
184 OPT_SORT,
185 OPT_RSORT,
186 OPT_UNIXCTL,
187 OPT_BUNDLE,
188 OPT_COLOR,
189 OPT_MAY_CREATE,
190 OPT_READ_ONLY,
191 DAEMON_OPTION_ENUMS,
192 OFP_VERSION_OPTION_ENUMS,
193 VLOG_OPTION_ENUMS,
194 SSL_OPTION_ENUMS,
195 };
196 static const struct option long_options[] = {
197 {"timeout", required_argument, NULL, 't'},
198 {"strict", no_argument, NULL, OPT_STRICT},
199 {"readd", no_argument, NULL, OPT_READD},
200 {"flow-format", required_argument, NULL, 'F'},
201 {"packet-in-format", required_argument, NULL, 'P'},
202 {"more", no_argument, NULL, 'm'},
203 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
204 {"sort", optional_argument, NULL, OPT_SORT},
205 {"rsort", optional_argument, NULL, OPT_RSORT},
206 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
207 {"help", no_argument, NULL, 'h'},
208 {"option", no_argument, NULL, 'o'},
209 {"bundle", no_argument, NULL, OPT_BUNDLE},
210 {"color", optional_argument, NULL, OPT_COLOR},
211 {"may-create", no_argument, NULL, OPT_MAY_CREATE},
212 {"read-only", no_argument, NULL, OPT_READ_ONLY},
213 DAEMON_LONG_OPTIONS,
214 OFP_VERSION_LONG_OPTIONS,
215 VLOG_LONG_OPTIONS,
216 STREAM_SSL_LONG_OPTIONS,
217 {NULL, 0, NULL, 0},
218 };
219 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
220 uint32_t versions;
221 enum ofputil_protocol version_protocols;
222
223 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
224 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
225 * requests, but those requests have subtly different semantics in
226 * different OpenFlow versions. For example:
227 *
228 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
229 * existing flow to modify adds a new flow.
230 *
231 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
232 * existing flow to modify adds a new flow, but only if the mod-flow
233 * did not match on the flow cookie.
234 *
235 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
236 * new flow.
237 */
238 set_allowed_ofp_versions("OpenFlow10");
239
240 for (;;) {
241 unsigned long int timeout;
242 int c;
243
244 c = getopt_long(argc, argv, short_options, long_options, NULL);
245 if (c == -1) {
246 break;
247 }
248
249 switch (c) {
250 case 't':
251 timeout = strtoul(optarg, NULL, 10);
252 if (timeout <= 0) {
253 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
254 optarg);
255 } else {
256 time_alarm(timeout);
257 }
258 break;
259
260 case 'F':
261 allowed_protocols = ofputil_protocols_from_string(optarg);
262 if (!allowed_protocols) {
263 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
264 }
265 break;
266
267 case 'P':
268 preferred_packet_in_format =
269 ofputil_packet_in_format_from_string(optarg);
270 if (preferred_packet_in_format < 0) {
271 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
272 }
273 break;
274
275 case 'm':
276 verbosity++;
277 break;
278
279 case 'h':
280 usage();
281
282 case 'o':
283 ovs_cmdl_print_options(long_options);
284 exit(EXIT_SUCCESS);
285
286 case OPT_BUNDLE:
287 bundle = true;
288 break;
289
290 case OPT_STRICT:
291 strict = true;
292 break;
293
294 case OPT_READ_ONLY:
295 read_only = true;
296 break;
297
298 case OPT_READD:
299 readd = true;
300 break;
301
302 case OPT_TIMESTAMP:
303 timestamp = true;
304 break;
305
306 case OPT_SORT:
307 add_sort_criterion(SORT_ASC, optarg);
308 break;
309
310 case OPT_RSORT:
311 add_sort_criterion(SORT_DESC, optarg);
312 break;
313
314 case OPT_UNIXCTL:
315 unixctl_path = optarg;
316 break;
317
318 case OPT_COLOR:
319 if (optarg) {
320 if (!strcasecmp(optarg, "always")
321 || !strcasecmp(optarg, "yes")
322 || !strcasecmp(optarg, "force")) {
323 enable_color = true;
324 } else if (!strcasecmp(optarg, "never")
325 || !strcasecmp(optarg, "no")
326 || !strcasecmp(optarg, "none")) {
327 enable_color = false;
328 } else if (!strcasecmp(optarg, "auto")
329 || !strcasecmp(optarg, "tty")
330 || !strcasecmp(optarg, "if-tty")) {
331 /* Determine whether we need colors, i.e. whether standard
332 * output is a tty. */
333 enable_color = is_stdout_a_tty();
334 } else {
335 ovs_fatal(0, "incorrect value `%s' for --color", optarg);
336 }
337 } else {
338 enable_color = is_stdout_a_tty();
339 }
340 break;
341
342 case OPT_MAY_CREATE:
343 may_create = true;
344 break;
345
346 DAEMON_OPTION_HANDLERS
347 OFP_VERSION_OPTION_HANDLERS
348 VLOG_OPTION_HANDLERS
349 STREAM_SSL_OPTION_HANDLERS
350
351 case '?':
352 exit(EXIT_FAILURE);
353
354 default:
355 abort();
356 }
357 }
358
359 if (n_criteria) {
360 /* Always do a final sort pass based on priority. */
361 add_sort_criterion(SORT_DESC, "priority");
362 }
363
364 free(short_options);
365
366 /* Implicit OpenFlow 1.4 with the '--bundle' option. */
367 if (bundle && !(get_allowed_ofp_versions() &
368 ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP))) {
369 /* Add implicit allowance for OpenFlow 1.4. */
370 add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
371 OFPUTIL_P_OF14_OXM));
372 /* Remove all versions that do not support bundles. */
373 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
374 OFPUTIL_P_OF13_UP));
375 }
376 versions = get_allowed_ofp_versions();
377 version_protocols = ofputil_protocols_from_version_bitmap(versions);
378 if (!(allowed_protocols & version_protocols)) {
379 char *protocols = ofputil_protocols_to_string(allowed_protocols);
380 struct ds version_s = DS_EMPTY_INITIALIZER;
381
382 ofputil_format_version_bitmap_names(&version_s, versions);
383 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
384 "any of the enabled flow formats (%s). (Use -O to enable "
385 "additional OpenFlow versions or -F to enable additional "
386 "flow formats.)", ds_cstr(&version_s), protocols);
387 }
388 allowed_protocols &= version_protocols;
389 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
390 allowed_protocols));
391
392 colors_init(enable_color);
393 }
394
395 static void
396 usage(void)
397 {
398 printf("%s: OpenFlow switch management utility\n"
399 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
400 "\nFor OpenFlow switches:\n"
401 " show SWITCH show OpenFlow information\n"
402 " dump-desc SWITCH print switch description\n"
403 " dump-tables SWITCH print table stats\n"
404 " dump-table-features SWITCH print table features\n"
405 " dump-table-desc SWITCH print table description (OF1.4+)\n"
406 " mod-port SWITCH IFACE ACT modify port behavior\n"
407 " mod-table SWITCH MOD modify flow table behavior\n"
408 " OF1.1/1.2 MOD: controller, continue, drop\n"
409 " OF1.4+ MOD: evict, noevict, vacancy:low,high, novacancy\n"
410 " get-frags SWITCH print fragment handling behavior\n"
411 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
412 " FRAG_MODE: normal, drop, reassemble, nx-match\n"
413 " dump-ports SWITCH [PORT] print port statistics\n"
414 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
415 " dump-flows SWITCH print all flow entries\n"
416 " dump-flows SWITCH FLOW print matching FLOWs\n"
417 " dump-aggregate SWITCH print aggregate flow statistics\n"
418 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
419 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
420 " add-flow SWITCH FLOW add flow described by FLOW\n"
421 " add-flows SWITCH FILE add flows from FILE\n"
422 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
423 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
424 " replace-flows SWITCH FILE replace flows with those in FILE\n"
425 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
426 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
427 " execute ACTIONS on PACKET\n"
428 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
429 " print packets received from SWITCH\n"
430 " snoop SWITCH snoop on SWITCH and its controller\n"
431 " add-group SWITCH GROUP add group described by GROUP\n"
432 " add-groups SWITCH FILE add group from FILE\n"
433 " [--may-create] mod-group SWITCH GROUP modify specific group\n"
434 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
435 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
436 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
437 " dump-group-features SWITCH print group features\n"
438 " dump-groups SWITCH [GROUP] print group description\n"
439 " dump-group-stats SWITCH [GROUP] print group statistics\n"
440 " queue-get-config SWITCH [PORT] print queue config for PORT\n"
441 " add-meter SWITCH METER add meter described by METER\n"
442 " mod-meter SWITCH METER modify specific METER\n"
443 " del-meter SWITCH METER delete METER\n"
444 " del-meters SWITCH delete all meters\n"
445 " dump-meter SWITCH METER print METER configuration\n"
446 " dump-meters SWITCH print all meter configuration\n"
447 " meter-stats SWITCH [METER] print meter statistics\n"
448 " meter-features SWITCH print meter features\n"
449 " add-tlv-map SWITCH MAP add TLV option MAPpings\n"
450 " del-tlv-map SWITCH [MAP] delete TLV option MAPpings\n"
451 " dump-tlv-map SWITCH print TLV option mappings\n"
452 " dump-ipfix-bridge SWITCH print ipfix stats of bridge\n"
453 " dump-ipfix-flow SWITCH print flow ipfix of a bridge\n"
454 " ct-flush-zone SWITCH ZONE flush conntrack entries in ZONE\n"
455 "\nFor OpenFlow switches and controllers:\n"
456 " probe TARGET probe whether TARGET is up\n"
457 " ping TARGET [N] latency of N-byte echos\n"
458 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
459 "SWITCH or TARGET is an active OpenFlow connection method.\n"
460 "\nOther commands:\n"
461 " ofp-parse FILE print messages read from FILE\n"
462 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
463 program_name, program_name);
464 vconn_usage(true, false, false);
465 daemon_usage();
466 ofp_version_usage();
467 vlog_usage();
468 printf("\nOther options:\n"
469 " --strict use strict match for flow commands\n"
470 " --read-only do not execute read/write commands\n"
471 " --readd replace flows that haven't changed\n"
472 " -F, --flow-format=FORMAT force particular flow format\n"
473 " -P, --packet-in-format=FRMT force particular packet in format\n"
474 " -m, --more be more verbose printing OpenFlow\n"
475 " --timestamp (monitor, snoop) print timestamps\n"
476 " -t, --timeout=SECS give up after SECS seconds\n"
477 " --sort[=field] sort in ascending order\n"
478 " --rsort[=field] sort in descending order\n"
479 " --unixctl=SOCKET set control socket name\n"
480 " --color[=always|never|auto] control use of color in output\n"
481 " -h, --help display this help message\n"
482 " -V, --version display version information\n");
483 exit(EXIT_SUCCESS);
484 }
485
486 static void
487 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
488 const char *argv[] OVS_UNUSED, void *exiting_)
489 {
490 bool *exiting = exiting_;
491 *exiting = true;
492 unixctl_command_reply(conn, NULL);
493 }
494
495 static void run(int retval, const char *message, ...)
496 OVS_PRINTF_FORMAT(2, 3);
497
498 static void
499 run(int retval, const char *message, ...)
500 {
501 if (retval) {
502 va_list args;
503
504 va_start(args, message);
505 ovs_fatal_valist(retval, message, args);
506 }
507 }
508 \f
509 /* Generic commands. */
510
511 static int
512 open_vconn_socket(const char *name, struct vconn **vconnp)
513 {
514 char *vconn_name = xasprintf("unix:%s", name);
515 int error;
516
517 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
518 vconnp);
519 if (error && error != ENOENT) {
520 ovs_fatal(0, "%s: failed to open socket (%s)", name,
521 ovs_strerror(error));
522 }
523 free(vconn_name);
524
525 return error;
526 }
527
528 enum open_target { MGMT, SNOOP };
529
530 static enum ofputil_protocol
531 open_vconn__(const char *name, enum open_target target,
532 struct vconn **vconnp)
533 {
534 const char *suffix = target == MGMT ? "mgmt" : "snoop";
535 char *datapath_name, *datapath_type, *socket_name;
536 enum ofputil_protocol protocol;
537 char *bridge_path;
538 int ofp_version;
539 int error;
540
541 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
542
543 ofproto_parse_name(name, &datapath_name, &datapath_type);
544 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
545 free(datapath_name);
546 free(datapath_type);
547
548 if (strchr(name, ':')) {
549 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
550 "connecting to %s", name);
551 } else if (!open_vconn_socket(name, vconnp)) {
552 /* Fall Through. */
553 } else if (!open_vconn_socket(bridge_path, vconnp)) {
554 /* Fall Through. */
555 } else if (!open_vconn_socket(socket_name, vconnp)) {
556 /* Fall Through. */
557 } else {
558 ovs_fatal(0, "%s is not a bridge or a socket", name);
559 }
560
561 if (target == SNOOP) {
562 vconn_set_recv_any_version(*vconnp);
563 }
564
565 free(bridge_path);
566 free(socket_name);
567
568 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
569 error = vconn_connect_block(*vconnp);
570 if (error) {
571 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
572 ovs_strerror(error));
573 }
574
575 ofp_version = vconn_get_version(*vconnp);
576 protocol = ofputil_protocol_from_ofp_version(ofp_version);
577 if (!protocol) {
578 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
579 name, ofp_version);
580 }
581 return protocol;
582 }
583
584 static enum ofputil_protocol
585 open_vconn(const char *name, struct vconn **vconnp)
586 {
587 return open_vconn__(name, MGMT, vconnp);
588 }
589
590 static void
591 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
592 {
593 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
594 }
595
596 static void
597 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
598 {
599 const struct ofp_header *oh = request->data;
600 if (ofpmsg_is_stat_request(oh)) {
601 ovs_be32 send_xid = oh->xid;
602 enum ofpraw request_raw;
603 enum ofpraw reply_raw;
604 bool done = false;
605
606 ofpraw_decode_partial(&request_raw, request->data, request->size);
607 reply_raw = ofpraw_stats_request_to_reply(request_raw, oh->version);
608
609 send_openflow_buffer(vconn, request);
610 while (!done) {
611 ovs_be32 recv_xid;
612 struct ofpbuf *reply;
613
614 run(vconn_recv_block(vconn, &reply),
615 "OpenFlow packet receive failed");
616 recv_xid = ((struct ofp_header *) reply->data)->xid;
617 if (send_xid == recv_xid) {
618 enum ofpraw raw;
619
620 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
621
622 ofpraw_decode(&raw, reply->data);
623 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
624 done = true;
625 } else if (raw == reply_raw) {
626 done = !ofpmp_more(reply->data);
627 } else {
628 ovs_fatal(0, "received bad reply: %s",
629 ofp_to_string(reply->data, reply->size,
630 verbosity + 1));
631 }
632 } else {
633 VLOG_DBG("received reply with xid %08"PRIx32" "
634 "!= expected %08"PRIx32, recv_xid, send_xid);
635 }
636 ofpbuf_delete(reply);
637 }
638 } else {
639 struct ofpbuf *reply;
640
641 run(vconn_transact(vconn, request, &reply), "talking to %s",
642 vconn_get_name(vconn));
643 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
644 ofpbuf_delete(reply);
645 }
646 }
647
648 static void
649 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
650 {
651 struct ofpbuf *request;
652 struct vconn *vconn;
653
654 open_vconn(vconn_name, &vconn);
655 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
656 dump_transaction(vconn, request);
657 vconn_close(vconn);
658 }
659
660 /* Sends all of the 'requests', which should be requests that only have replies
661 * if an error occurs, and waits for them to succeed or fail. If an error does
662 * occur, prints it and exits with an error.
663 *
664 * Destroys all of the 'requests'. */
665 static void
666 transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
667 {
668 struct ofpbuf *reply;
669
670 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
671 "talking to %s", vconn_get_name(vconn));
672 if (reply) {
673 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
674 exit(1);
675 }
676 ofpbuf_delete(reply);
677 }
678
679 /* Frees the error messages as they are printed. */
680 static void
681 bundle_print_errors(struct ovs_list *errors, struct ovs_list *requests)
682 {
683 struct vconn_bundle_error *error, *next;
684 struct ofpbuf *bmsg;
685
686 INIT_CONTAINER(bmsg, requests, list_node);
687
688 LIST_FOR_EACH_SAFE (error, next, list_node, errors) {
689 enum ofperr ofperr;
690 struct ofpbuf payload;
691
692 ofperr = ofperr_decode_msg(&error->ofp_msg, &payload);
693 if (!ofperr) {
694 fprintf(stderr, "***decode error***");
695 } else {
696 /* Default to the likely truncated message. */
697 const struct ofp_header *ofp_msg = payload.data;
698 size_t msg_len = payload.size;
699
700 /* Find the failing message from the requests list to be able to
701 * dump the whole message. We assume the errors are returned in
702 * the same order as in which the messages are sent to get O(n)
703 * rather than O(n^2) processing here. If this heuristics fails we
704 * may print the truncated hexdumps instead. */
705 LIST_FOR_EACH_CONTINUE (bmsg, list_node, requests) {
706 const struct ofp_header *oh = bmsg->data;
707
708 if (oh->xid == error->ofp_msg.xid) {
709 ofp_msg = oh;
710 msg_len = bmsg->size;
711 break;
712 }
713 }
714 fprintf(stderr, "Error %s for: ", ofperr_get_name(ofperr));
715 ofp_print(stderr, ofp_msg, msg_len, verbosity + 1);
716 }
717 ofpbuf_uninit(&payload);
718 free(error);
719 }
720 fflush(stderr);
721 }
722
723 static void
724 bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
725 {
726 struct ovs_list errors;
727 int retval = vconn_bundle_transact(vconn, requests, flags, &errors);
728
729 bundle_print_errors(&errors, requests);
730
731 if (retval) {
732 ovs_fatal(retval, "talking to %s", vconn_get_name(vconn));
733 }
734 }
735
736 /* Sends 'request', which should be a request that only has a reply if an error
737 * occurs, and waits for it to succeed or fail. If an error does occur, prints
738 * it and exits with an error.
739 *
740 * Destroys 'request'. */
741 static void
742 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
743 {
744 struct ovs_list requests;
745
746 ovs_list_init(&requests);
747 ovs_list_push_back(&requests, &request->list_node);
748 transact_multiple_noreply(vconn, &requests);
749 }
750
751 static void
752 fetch_switch_config(struct vconn *vconn, struct ofputil_switch_config *config)
753 {
754 struct ofpbuf *request;
755 struct ofpbuf *reply;
756 enum ofptype type;
757
758 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
759 vconn_get_version(vconn), 0);
760 run(vconn_transact(vconn, request, &reply),
761 "talking to %s", vconn_get_name(vconn));
762
763 if (ofptype_decode(&type, reply->data)
764 || type != OFPTYPE_GET_CONFIG_REPLY) {
765 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
766 }
767 ofputil_decode_get_config_reply(reply->data, config);
768 ofpbuf_delete(reply);
769 }
770
771 static void
772 set_switch_config(struct vconn *vconn,
773 const struct ofputil_switch_config *config)
774 {
775 enum ofp_version version = vconn_get_version(vconn);
776 transact_noreply(vconn, ofputil_encode_set_config(config, version));
777 }
778
779 static void
780 ofctl_show(struct ovs_cmdl_context *ctx)
781 {
782 const char *vconn_name = ctx->argv[1];
783 enum ofp_version version;
784 struct vconn *vconn;
785 struct ofpbuf *request;
786 struct ofpbuf *reply;
787 bool has_ports;
788
789 open_vconn(vconn_name, &vconn);
790 version = vconn_get_version(vconn);
791 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
792 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
793
794 has_ports = ofputil_switch_features_has_ports(reply);
795 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
796 ofpbuf_delete(reply);
797
798 if (!has_ports) {
799 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
800 dump_transaction(vconn, request);
801 }
802 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
803 vconn_close(vconn);
804 }
805
806 static void
807 ofctl_dump_desc(struct ovs_cmdl_context *ctx)
808 {
809 dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
810 }
811
812 static void
813 ofctl_dump_tables(struct ovs_cmdl_context *ctx)
814 {
815 dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
816 }
817
818 static void
819 ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
820 {
821 struct ofpbuf *request;
822 struct vconn *vconn;
823
824 open_vconn(ctx->argv[1], &vconn);
825 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
826
827 /* The following is similar to dump_trivial_transaction(), but it
828 * maintains the previous 'ofputil_table_features' from one stats reply
829 * message to the next, which allows duplication to be eliminated in the
830 * output across messages. Otherwise the output is much larger and harder
831 * to read, because only 17 or so ofputil_table_features elements fit in a
832 * single 64 kB OpenFlow message and therefore you get a ton of repetition
833 * (every 17th element is printed in full instead of abbreviated). */
834
835 const struct ofp_header *request_oh = request->data;
836 ovs_be32 send_xid = request_oh->xid;
837 bool done = false;
838
839 struct ofputil_table_features prev;
840 int n = 0;
841
842 send_openflow_buffer(vconn, request);
843 while (!done) {
844 ovs_be32 recv_xid;
845 struct ofpbuf *reply;
846
847 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
848 recv_xid = ((struct ofp_header *) reply->data)->xid;
849 if (send_xid == recv_xid) {
850 enum ofptype type;
851 enum ofperr error;
852 error = ofptype_decode(&type, reply->data);
853 if (error) {
854 ovs_fatal(0, "decode error: %s", ofperr_get_name(error));
855 } else if (type == OFPTYPE_ERROR) {
856 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
857 done = true;
858 } else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) {
859 done = !ofpmp_more(reply->data);
860 for (;;) {
861 struct ofputil_table_features tf;
862 int retval;
863
864 retval = ofputil_decode_table_features(reply, &tf, true);
865 if (retval) {
866 if (retval != EOF) {
867 ovs_fatal(0, "decode error: %s",
868 ofperr_get_name(retval));
869 }
870 break;
871 }
872
873 struct ds s = DS_EMPTY_INITIALIZER;
874 ofp_print_table_features(&s, &tf, n ? &prev : NULL,
875 NULL, NULL);
876 puts(ds_cstr(&s));
877 ds_destroy(&s);
878
879 prev = tf;
880 n++;
881 }
882 } else {
883 ovs_fatal(0, "received bad reply: %s",
884 ofp_to_string(reply->data, reply->size,
885 verbosity + 1));
886 }
887 } else {
888 VLOG_DBG("received reply with xid %08"PRIx32" "
889 "!= expected %08"PRIx32, recv_xid, send_xid);
890 }
891 ofpbuf_delete(reply);
892 }
893
894 vconn_close(vconn);
895 }
896
897 static void
898 ofctl_dump_table_desc(struct ovs_cmdl_context *ctx)
899 {
900 struct ofpbuf *request;
901 struct vconn *vconn;
902
903 open_vconn(ctx->argv[1], &vconn);
904 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
905 if (request) {
906 dump_transaction(vconn, request);
907 }
908
909 vconn_close(vconn);
910 }
911
912
913 static bool
914 str_to_ofp(const char *s, ofp_port_t *ofp_port)
915 {
916 bool ret;
917 uint32_t port_;
918
919 ret = str_to_uint(s, 10, &port_);
920 *ofp_port = u16_to_ofp(port_);
921 return ret;
922 }
923
924 struct port_iterator {
925 struct vconn *vconn;
926
927 enum { PI_FEATURES, PI_PORT_DESC } variant;
928 struct ofpbuf *reply;
929 ovs_be32 send_xid;
930 bool more;
931 };
932
933 static void
934 port_iterator_fetch_port_desc(struct port_iterator *pi)
935 {
936 pi->variant = PI_PORT_DESC;
937 pi->more = true;
938
939 struct ofpbuf *rq = ofputil_encode_port_desc_stats_request(
940 vconn_get_version(pi->vconn), OFPP_ANY);
941 pi->send_xid = ((struct ofp_header *) rq->data)->xid;
942 send_openflow_buffer(pi->vconn, rq);
943 }
944
945 static void
946 port_iterator_fetch_features(struct port_iterator *pi)
947 {
948 pi->variant = PI_FEATURES;
949
950 /* Fetch the switch's ofp_switch_features. */
951 enum ofp_version version = vconn_get_version(pi->vconn);
952 struct ofpbuf *rq = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
953 run(vconn_transact(pi->vconn, rq, &pi->reply),
954 "talking to %s", vconn_get_name(pi->vconn));
955
956 enum ofptype type;
957 if (ofptype_decode(&type, pi->reply->data)
958 || type != OFPTYPE_FEATURES_REPLY) {
959 ovs_fatal(0, "%s: received bad features reply",
960 vconn_get_name(pi->vconn));
961 }
962 if (!ofputil_switch_features_has_ports(pi->reply)) {
963 /* The switch features reply does not contain a complete list of ports.
964 * Probably, there are more ports than will fit into a single 64 kB
965 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
966 * ports. */
967 ofpbuf_delete(pi->reply);
968 pi->reply = NULL;
969 port_iterator_fetch_port_desc(pi);
970 return;
971 }
972
973 struct ofputil_switch_features features;
974 enum ofperr error = ofputil_pull_switch_features(pi->reply, &features);
975 if (error) {
976 ovs_fatal(0, "%s: failed to decode features reply (%s)",
977 vconn_get_name(pi->vconn), ofperr_to_string(error));
978 }
979 }
980
981 /* Initializes 'pi' to prepare for iterating through all of the ports on the
982 * OpenFlow switch to which 'vconn' is connected.
983 *
984 * During iteration, the client should not make other use of 'vconn', because
985 * that can cause other messages to be interleaved with the replies used by the
986 * iterator and thus some ports may be missed or a hang can occur. */
987 static void
988 port_iterator_init(struct port_iterator *pi, struct vconn *vconn)
989 {
990 memset(pi, 0, sizeof *pi);
991 pi->vconn = vconn;
992 if (vconn_get_version(vconn) < OFP13_VERSION) {
993 port_iterator_fetch_features(pi);
994 } else {
995 port_iterator_fetch_port_desc(pi);
996 }
997 }
998
999 /* Obtains the next port from 'pi'. On success, initializes '*pp' with the
1000 * port's details and returns true, otherwise (if all the ports have already
1001 * been seen), returns false. */
1002 static bool
1003 port_iterator_next(struct port_iterator *pi, struct ofputil_phy_port *pp)
1004 {
1005 for (;;) {
1006 if (pi->reply) {
1007 int retval = ofputil_pull_phy_port(vconn_get_version(pi->vconn),
1008 pi->reply, pp);
1009 if (!retval) {
1010 return true;
1011 } else if (retval != EOF) {
1012 ovs_fatal(0, "received bad reply: %s",
1013 ofp_to_string(pi->reply->data, pi->reply->size,
1014 verbosity + 1));
1015 }
1016 }
1017
1018 if (pi->variant == PI_FEATURES || !pi->more) {
1019 return false;
1020 }
1021
1022 ovs_be32 recv_xid;
1023 do {
1024 ofpbuf_delete(pi->reply);
1025 run(vconn_recv_block(pi->vconn, &pi->reply),
1026 "OpenFlow receive failed");
1027 recv_xid = ((struct ofp_header *) pi->reply->data)->xid;
1028 } while (pi->send_xid != recv_xid);
1029
1030 struct ofp_header *oh = pi->reply->data;
1031 enum ofptype type;
1032 if (ofptype_pull(&type, pi->reply)
1033 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
1034 ovs_fatal(0, "received bad reply: %s",
1035 ofp_to_string(pi->reply->data, pi->reply->size,
1036 verbosity + 1));
1037 }
1038
1039 pi->more = (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0;
1040 }
1041 }
1042
1043 /* Destroys iterator 'pi'. */
1044 static void
1045 port_iterator_destroy(struct port_iterator *pi)
1046 {
1047 if (pi) {
1048 while (pi->variant == PI_PORT_DESC && pi->more) {
1049 /* Drain vconn's queue of any other replies for this request. */
1050 struct ofputil_phy_port pp;
1051 port_iterator_next(pi, &pp);
1052 }
1053
1054 ofpbuf_delete(pi->reply);
1055 }
1056 }
1057
1058 /* Opens a connection to 'vconn_name', fetches the port structure for
1059 * 'port_name' (which may be a port name or number), and copies it into
1060 * '*pp'. */
1061 static void
1062 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
1063 struct ofputil_phy_port *pp)
1064 {
1065 struct vconn *vconn;
1066 ofp_port_t port_no;
1067 bool found = false;
1068
1069 /* Try to interpret the argument as a port number. */
1070 if (!str_to_ofp(port_name, &port_no)) {
1071 port_no = OFPP_NONE;
1072 }
1073
1074 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
1075 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
1076 * into the OFPST_PORT_DESC reply. Try it the correct way. */
1077 open_vconn(vconn_name, &vconn);
1078 struct port_iterator pi;
1079 for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, pp); ) {
1080 if (port_no != OFPP_NONE
1081 ? port_no == pp->port_no
1082 : !strcmp(pp->name, port_name)) {
1083 found = true;
1084 break;
1085 }
1086 }
1087 port_iterator_destroy(&pi);
1088 vconn_close(vconn);
1089
1090 if (!found) {
1091 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
1092 }
1093 }
1094
1095 /* Returns the port number corresponding to 'port_name' (which may be a port
1096 * name or number) within the switch 'vconn_name'. */
1097 static ofp_port_t
1098 str_to_port_no(const char *vconn_name, const char *port_name)
1099 {
1100 ofp_port_t port_no;
1101
1102 if (ofputil_port_from_string(port_name, &port_no)) {
1103 return port_no;
1104 } else {
1105 struct ofputil_phy_port pp;
1106
1107 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
1108 return pp.port_no;
1109 }
1110 }
1111
1112 static bool
1113 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
1114 enum ofputil_protocol *cur)
1115 {
1116 for (;;) {
1117 struct ofpbuf *request, *reply;
1118 enum ofputil_protocol next;
1119
1120 request = ofputil_encode_set_protocol(*cur, want, &next);
1121 if (!request) {
1122 return *cur == want;
1123 }
1124
1125 run(vconn_transact_noreply(vconn, request, &reply),
1126 "talking to %s", vconn_get_name(vconn));
1127 if (reply) {
1128 char *s = ofp_to_string(reply->data, reply->size, 2);
1129 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
1130 vconn_get_name(vconn), s);
1131 free(s);
1132 ofpbuf_delete(reply);
1133 return false;
1134 }
1135
1136 *cur = next;
1137 }
1138 }
1139
1140 static enum ofputil_protocol
1141 set_protocol_for_flow_dump(struct vconn *vconn,
1142 enum ofputil_protocol cur_protocol,
1143 enum ofputil_protocol usable_protocols)
1144 {
1145 char *usable_s;
1146 int i;
1147
1148 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
1149 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
1150 if (f & usable_protocols & allowed_protocols
1151 && try_set_protocol(vconn, f, &cur_protocol)) {
1152 return f;
1153 }
1154 }
1155
1156 usable_s = ofputil_protocols_to_string(usable_protocols);
1157 if (usable_protocols & allowed_protocols) {
1158 ovs_fatal(0, "switch does not support any of the usable flow "
1159 "formats (%s)", usable_s);
1160 } else {
1161 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1162 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1163 "allowed flow formats (%s)", usable_s, allowed_s);
1164 }
1165 }
1166
1167 static struct vconn *
1168 prepare_dump_flows(int argc, char *argv[], bool aggregate,
1169 struct ofpbuf **requestp)
1170 {
1171 enum ofputil_protocol usable_protocols, protocol;
1172 struct ofputil_flow_stats_request fsr;
1173 struct vconn *vconn;
1174 char *error;
1175
1176 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
1177 argc > 2 ? argv[2] : "",
1178 &usable_protocols);
1179 if (error) {
1180 ovs_fatal(0, "%s", error);
1181 }
1182
1183 protocol = open_vconn(argv[1], &vconn);
1184 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1185 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
1186 return vconn;
1187 }
1188
1189 static void
1190 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
1191 {
1192 struct ofpbuf *request;
1193 struct vconn *vconn;
1194
1195 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
1196 dump_transaction(vconn, request);
1197 vconn_close(vconn);
1198 }
1199
1200 static void
1201 get_match_field(const struct mf_field *field, const struct match *match,
1202 union mf_value *value)
1203 {
1204 if (!match->tun_md.valid || (field->id < MFF_TUN_METADATA0 ||
1205 field->id >= MFF_TUN_METADATA0 +
1206 TUN_METADATA_NUM_OPTS)) {
1207 mf_get_value(field, &match->flow, value);
1208 } else {
1209 const struct tun_metadata_loc *loc = &match->tun_md.entry[field->id -
1210 MFF_TUN_METADATA0].loc;
1211
1212 /* Since we don't have a tunnel mapping table, extract the value
1213 * from the locally allocated location in the match. */
1214 memset(value, 0, field->n_bytes - loc->len);
1215 memcpy(value->tun_metadata + field->n_bytes - loc->len,
1216 match->flow.tunnel.metadata.opts.u8 + loc->c.offset, loc->len);
1217 }
1218 }
1219
1220 static int
1221 compare_flows(const void *afs_, const void *bfs_)
1222 {
1223 const struct ofputil_flow_stats *afs = afs_;
1224 const struct ofputil_flow_stats *bfs = bfs_;
1225 const struct match *a = &afs->match;
1226 const struct match *b = &bfs->match;
1227 const struct sort_criterion *sc;
1228
1229 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
1230 const struct mf_field *f = sc->field;
1231 int ret;
1232
1233 if (!f) {
1234 int a_pri = afs->priority;
1235 int b_pri = bfs->priority;
1236 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
1237 } else {
1238 bool ina, inb;
1239
1240 ina = mf_are_prereqs_ok(f, &a->flow, NULL)
1241 && !mf_is_all_wild(f, &a->wc);
1242 inb = mf_are_prereqs_ok(f, &b->flow, NULL)
1243 && !mf_is_all_wild(f, &b->wc);
1244 if (ina != inb) {
1245 /* Skip the test for sc->order, so that missing fields always
1246 * sort to the end whether we're sorting in ascending or
1247 * descending order. */
1248 return ina ? -1 : 1;
1249 } else {
1250 union mf_value aval, bval;
1251
1252 get_match_field(f, a, &aval);
1253 get_match_field(f, b, &bval);
1254 ret = memcmp(&aval, &bval, f->n_bytes);
1255 }
1256 }
1257
1258 if (ret) {
1259 return sc->order == SORT_ASC ? ret : -ret;
1260 }
1261 }
1262
1263 return 0;
1264 }
1265
1266 static void
1267 ofctl_dump_flows(struct ovs_cmdl_context *ctx)
1268 {
1269 if (!n_criteria) {
1270 ofctl_dump_flows__(ctx->argc, ctx->argv, false);
1271 return;
1272 } else {
1273 struct ofputil_flow_stats *fses;
1274 size_t n_fses, allocated_fses;
1275 struct ofpbuf *request;
1276 struct ofpbuf ofpacts;
1277 struct ofpbuf *reply;
1278 struct vconn *vconn;
1279 ovs_be32 send_xid;
1280 struct ds s;
1281 size_t i;
1282
1283 vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request);
1284 send_xid = ((struct ofp_header *) request->data)->xid;
1285 send_openflow_buffer(vconn, request);
1286
1287 fses = NULL;
1288 n_fses = allocated_fses = 0;
1289 reply = NULL;
1290 ofpbuf_init(&ofpacts, 0);
1291 for (;;) {
1292 struct ofputil_flow_stats *fs;
1293
1294 if (n_fses >= allocated_fses) {
1295 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1296 }
1297
1298 fs = &fses[n_fses];
1299 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1300 &ofpacts)) {
1301 break;
1302 }
1303 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1304 n_fses++;
1305 }
1306 ofpbuf_uninit(&ofpacts);
1307
1308 qsort(fses, n_fses, sizeof *fses, compare_flows);
1309
1310 ds_init(&s);
1311 for (i = 0; i < n_fses; i++) {
1312 ds_clear(&s);
1313 ofp_print_flow_stats(&s, &fses[i]);
1314 puts(ds_cstr(&s));
1315 }
1316 ds_destroy(&s);
1317
1318 for (i = 0; i < n_fses; i++) {
1319 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1320 }
1321 free(fses);
1322
1323 vconn_close(vconn);
1324 }
1325 }
1326
1327 static void
1328 ofctl_dump_aggregate(struct ovs_cmdl_context *ctx)
1329 {
1330 ofctl_dump_flows__(ctx->argc, ctx->argv, true);
1331 }
1332
1333 static void
1334 ofctl_queue_stats(struct ovs_cmdl_context *ctx)
1335 {
1336 struct ofpbuf *request;
1337 struct vconn *vconn;
1338 struct ofputil_queue_stats_request oqs;
1339
1340 open_vconn(ctx->argv[1], &vconn);
1341
1342 if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) {
1343 oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1344 } else {
1345 oqs.port_no = OFPP_ANY;
1346 }
1347 if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) {
1348 oqs.queue_id = atoi(ctx->argv[3]);
1349 } else {
1350 oqs.queue_id = OFPQ_ALL;
1351 }
1352
1353 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1354 dump_transaction(vconn, request);
1355 vconn_close(vconn);
1356 }
1357
1358 static void
1359 ofctl_queue_get_config(struct ovs_cmdl_context *ctx)
1360 {
1361 const char *vconn_name = ctx->argv[1];
1362 const char *port_name = ctx->argc > 2 ? ctx->argv[2] : "any";
1363 ofp_port_t port = str_to_port_no(vconn_name, port_name);
1364 const char *queue_name = ctx->argc > 3 ? ctx->argv[3] : "all";
1365 uint32_t queue = (!strcasecmp(queue_name, "all")
1366 ? OFPQ_ALL
1367 : atoi(queue_name));
1368 struct vconn *vconn;
1369
1370 enum ofputil_protocol protocol = open_vconn(vconn_name, &vconn);
1371 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
1372 if (port == OFPP_ANY && version == OFP10_VERSION) {
1373 /* The user requested all queues on all ports. OpenFlow 1.0 only
1374 * supports getting queues for an individual port, so to implement the
1375 * user's request we have to get a list of all the ports.
1376 *
1377 * We use a second vconn to avoid having to accumulate a list of all of
1378 * the ports. */
1379 struct vconn *vconn2;
1380 enum ofputil_protocol protocol2 = open_vconn(vconn_name, &vconn2);
1381 enum ofp_version version2 = ofputil_protocol_to_ofp_version(protocol2);
1382
1383 struct port_iterator pi;
1384 struct ofputil_phy_port pp;
1385 for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, &pp); ) {
1386 if (ofp_to_u16(pp.port_no) < ofp_to_u16(OFPP_MAX)) {
1387 dump_transaction(vconn2,
1388 ofputil_encode_queue_get_config_request(
1389 version2, pp.port_no, queue));
1390 }
1391 }
1392 port_iterator_destroy(&pi);
1393 vconn_close(vconn2);
1394 } else {
1395 dump_transaction(vconn, ofputil_encode_queue_get_config_request(
1396 version, port, queue));
1397 }
1398 vconn_close(vconn);
1399 }
1400
1401 static enum ofputil_protocol
1402 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1403 enum ofputil_protocol usable_protocols)
1404 {
1405 enum ofputil_protocol cur_protocol;
1406 char *usable_s;
1407 int i;
1408
1409 if (!(usable_protocols & allowed_protocols)) {
1410 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1411 usable_s = ofputil_protocols_to_string(usable_protocols);
1412 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1413 "allowed flow formats (%s)", usable_s, allowed_s);
1414 }
1415
1416 /* If the initial flow format is allowed and usable, keep it. */
1417 cur_protocol = open_vconn(remote, vconnp);
1418 if (usable_protocols & allowed_protocols & cur_protocol) {
1419 return cur_protocol;
1420 }
1421
1422 /* Otherwise try each flow format in turn. */
1423 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1424 enum ofputil_protocol f = 1 << i;
1425
1426 if (f != cur_protocol
1427 && f & usable_protocols & allowed_protocols
1428 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1429 return f;
1430 }
1431 }
1432
1433 usable_s = ofputil_protocols_to_string(usable_protocols);
1434 ovs_fatal(0, "switch does not support any of the usable flow "
1435 "formats (%s)", usable_s);
1436 }
1437
1438 static void
1439 bundle_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1440 size_t n_fms, enum ofputil_protocol usable_protocols)
1441 {
1442 enum ofputil_protocol protocol;
1443 struct vconn *vconn;
1444 struct ovs_list requests;
1445 size_t i;
1446
1447 ovs_list_init(&requests);
1448
1449 /* Bundles need OpenFlow 1.3+. */
1450 usable_protocols &= OFPUTIL_P_OF13_UP;
1451 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1452
1453 for (i = 0; i < n_fms; i++) {
1454 struct ofputil_flow_mod *fm = &fms[i];
1455 struct ofpbuf *request = ofputil_encode_flow_mod(fm, protocol);
1456
1457 ovs_list_push_back(&requests, &request->list_node);
1458 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1459 }
1460
1461 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
1462 ofpbuf_list_delete(&requests);
1463 vconn_close(vconn);
1464 }
1465
1466 static void
1467 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1468 size_t n_fms, enum ofputil_protocol usable_protocols)
1469 {
1470 enum ofputil_protocol protocol;
1471 struct vconn *vconn;
1472 size_t i;
1473
1474 if (bundle) {
1475 bundle_flow_mod__(remote, fms, n_fms, usable_protocols);
1476 return;
1477 }
1478
1479 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1480
1481 for (i = 0; i < n_fms; i++) {
1482 struct ofputil_flow_mod *fm = &fms[i];
1483
1484 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1485 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1486 }
1487 vconn_close(vconn);
1488 }
1489
1490 static void
1491 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], int command)
1492 {
1493 enum ofputil_protocol usable_protocols;
1494 struct ofputil_flow_mod *fms = NULL;
1495 size_t n_fms = 0;
1496 char *error;
1497
1498 if (command == OFPFC_ADD) {
1499 /* Allow the file to specify a mix of commands. If none specified at
1500 * the beginning of any given line, then the default is OFPFC_ADD, so
1501 * this is backwards compatible. */
1502 command = -2;
1503 }
1504 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1505 &usable_protocols);
1506 if (error) {
1507 ovs_fatal(0, "%s", error);
1508 }
1509 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1510 free(fms);
1511 }
1512
1513 static void
1514 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1515 {
1516 if (argc > 2 && !strcmp(argv[2], "-")) {
1517 ofctl_flow_mod_file(argc, argv, command);
1518 } else {
1519 struct ofputil_flow_mod fm;
1520 char *error;
1521 enum ofputil_protocol usable_protocols;
1522
1523 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1524 &usable_protocols);
1525 if (error) {
1526 ovs_fatal(0, "%s", error);
1527 }
1528 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1529 }
1530 }
1531
1532 static void
1533 ofctl_add_flow(struct ovs_cmdl_context *ctx)
1534 {
1535 ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD);
1536 }
1537
1538 static void
1539 ofctl_add_flows(struct ovs_cmdl_context *ctx)
1540 {
1541 ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD);
1542 }
1543
1544 static void
1545 ofctl_mod_flows(struct ovs_cmdl_context *ctx)
1546 {
1547 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1548 }
1549
1550 static void
1551 ofctl_del_flows(struct ovs_cmdl_context *ctx)
1552 {
1553 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1554 }
1555
1556 static bool
1557 set_packet_in_format(struct vconn *vconn,
1558 enum nx_packet_in_format packet_in_format,
1559 bool must_succeed)
1560 {
1561 struct ofpbuf *spif;
1562
1563 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1564 packet_in_format);
1565 if (must_succeed) {
1566 transact_noreply(vconn, spif);
1567 } else {
1568 struct ofpbuf *reply;
1569
1570 run(vconn_transact_noreply(vconn, spif, &reply),
1571 "talking to %s", vconn_get_name(vconn));
1572 if (reply) {
1573 char *s = ofp_to_string(reply->data, reply->size, 2);
1574 VLOG_DBG("%s: failed to set packet in format to nx_packet_in, "
1575 "controller replied: %s.",
1576 vconn_get_name(vconn), s);
1577 free(s);
1578 ofpbuf_delete(reply);
1579
1580 return false;
1581 } else {
1582 VLOG_DBG("%s: using user-specified packet in format %s",
1583 vconn_get_name(vconn),
1584 ofputil_packet_in_format_to_string(packet_in_format));
1585 }
1586 }
1587 return true;
1588 }
1589
1590 static int
1591 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1592 {
1593 struct ofputil_switch_config config;
1594
1595 fetch_switch_config(vconn, &config);
1596 if (!config.invalid_ttl_to_controller) {
1597 config.invalid_ttl_to_controller = 1;
1598 set_switch_config(vconn, &config);
1599
1600 /* Then retrieve the configuration to see if it really took. OpenFlow
1601 * has ill-defined error reporting for bad flags, so this is about the
1602 * best we can do. */
1603 fetch_switch_config(vconn, &config);
1604 if (!config.invalid_ttl_to_controller) {
1605 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1606 "switch probably doesn't support this flag)");
1607 }
1608 }
1609 return 0;
1610 }
1611
1612 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1613 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1614 * an error message and stores NULL in '*msgp'. */
1615 static const char *
1616 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1617 {
1618 struct ofp_header *oh;
1619 struct ofpbuf *msg;
1620
1621 msg = ofpbuf_new(strlen(hex) / 2);
1622 *msgp = NULL;
1623
1624 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1625 ofpbuf_delete(msg);
1626 return "Trailing garbage in hex data";
1627 }
1628
1629 if (msg->size < sizeof(struct ofp_header)) {
1630 ofpbuf_delete(msg);
1631 return "Message too short for OpenFlow";
1632 }
1633
1634 oh = msg->data;
1635 if (msg->size != ntohs(oh->length)) {
1636 ofpbuf_delete(msg);
1637 return "Message size does not match length in OpenFlow header";
1638 }
1639
1640 *msgp = msg;
1641 return NULL;
1642 }
1643
1644 static void
1645 ofctl_send(struct unixctl_conn *conn, int argc,
1646 const char *argv[], void *vconn_)
1647 {
1648 struct vconn *vconn = vconn_;
1649 struct ds reply;
1650 bool ok;
1651 int i;
1652
1653 ok = true;
1654 ds_init(&reply);
1655 for (i = 1; i < argc; i++) {
1656 const char *error_msg;
1657 struct ofpbuf *msg;
1658 int error;
1659
1660 error_msg = openflow_from_hex(argv[i], &msg);
1661 if (error_msg) {
1662 ds_put_format(&reply, "%s\n", error_msg);
1663 ok = false;
1664 continue;
1665 }
1666
1667 fprintf(stderr, "send: ");
1668 ofp_print(stderr, msg->data, msg->size, verbosity);
1669
1670 error = vconn_send_block(vconn, msg);
1671 if (error) {
1672 ofpbuf_delete(msg);
1673 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1674 ok = false;
1675 } else {
1676 ds_put_cstr(&reply, "sent\n");
1677 }
1678 }
1679
1680 if (ok) {
1681 unixctl_command_reply(conn, ds_cstr(&reply));
1682 } else {
1683 unixctl_command_reply_error(conn, ds_cstr(&reply));
1684 }
1685 ds_destroy(&reply);
1686 }
1687
1688 static void
1689 unixctl_packet_out(struct unixctl_conn *conn, int OVS_UNUSED argc,
1690 const char *argv[], void *vconn_)
1691 {
1692 struct vconn *vconn = vconn_;
1693 enum ofputil_protocol protocol
1694 = ofputil_protocol_from_ofp_version(vconn_get_version(vconn));
1695 struct ds reply = DS_EMPTY_INITIALIZER;
1696 bool ok = true;
1697
1698 enum ofputil_protocol usable_protocols;
1699 struct ofputil_packet_out po;
1700 char *error_msg;
1701
1702 error_msg = parse_ofp_packet_out_str(&po, argv[1], &usable_protocols);
1703 if (error_msg) {
1704 ds_put_format(&reply, "%s\n", error_msg);
1705 free(error_msg);
1706 ok = false;
1707 }
1708
1709 if (ok && !(usable_protocols & protocol)) {
1710 ds_put_format(&reply, "PACKET_OUT actions are incompatible with the OpenFlow connection.\n");
1711 ok = false;
1712 }
1713
1714 if (ok) {
1715 struct ofpbuf *msg = ofputil_encode_packet_out(&po, protocol);
1716
1717 ofp_print(stderr, msg->data, msg->size, verbosity);
1718
1719 int error = vconn_send_block(vconn, msg);
1720 if (error) {
1721 ofpbuf_delete(msg);
1722 ds_put_format(&reply, "%s\n", ovs_strerror(error));
1723 ok = false;
1724 }
1725 }
1726
1727 if (ok) {
1728 unixctl_command_reply(conn, ds_cstr(&reply));
1729 } else {
1730 unixctl_command_reply_error(conn, ds_cstr(&reply));
1731 }
1732 ds_destroy(&reply);
1733
1734 if (!error_msg) {
1735 free(CONST_CAST(void *, po.packet));
1736 free(po.ofpacts);
1737 }
1738 }
1739
1740 struct barrier_aux {
1741 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1742 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1743 };
1744
1745 static void
1746 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1747 const char *argv[] OVS_UNUSED, void *aux_)
1748 {
1749 struct barrier_aux *aux = aux_;
1750 struct ofpbuf *msg;
1751 int error;
1752
1753 if (aux->conn) {
1754 unixctl_command_reply_error(conn, "already waiting for barrier reply");
1755 return;
1756 }
1757
1758 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1759 error = vconn_send_block(aux->vconn, msg);
1760 if (error) {
1761 ofpbuf_delete(msg);
1762 unixctl_command_reply_error(conn, ovs_strerror(error));
1763 } else {
1764 aux->conn = conn;
1765 }
1766 }
1767
1768 static void
1769 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1770 const char *argv[], void *aux OVS_UNUSED)
1771 {
1772 int fd;
1773
1774 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1775 if (fd < 0) {
1776 unixctl_command_reply_error(conn, ovs_strerror(errno));
1777 return;
1778 }
1779
1780 fflush(stderr);
1781 dup2(fd, STDERR_FILENO);
1782 close(fd);
1783 unixctl_command_reply(conn, NULL);
1784 }
1785
1786 static void
1787 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1788 const char *argv[] OVS_UNUSED, void *blocked_)
1789 {
1790 bool *blocked = blocked_;
1791
1792 if (!*blocked) {
1793 *blocked = true;
1794 unixctl_command_reply(conn, NULL);
1795 } else {
1796 unixctl_command_reply(conn, "already blocking");
1797 }
1798 }
1799
1800 static void
1801 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1802 const char *argv[] OVS_UNUSED, void *blocked_)
1803 {
1804 bool *blocked = blocked_;
1805
1806 if (*blocked) {
1807 *blocked = false;
1808 unixctl_command_reply(conn, NULL);
1809 } else {
1810 unixctl_command_reply(conn, "already unblocked");
1811 }
1812 }
1813
1814 /* Prints to stdout all of the messages received on 'vconn'.
1815 *
1816 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1817 * received on 'vconn'.
1818 *
1819 * If 'resume_continuations' is true, sends an NXT_RESUME in reply to any
1820 * NXT_PACKET_IN2 that includes a continuation. */
1821 static void
1822 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests,
1823 bool resume_continuations)
1824 {
1825 struct barrier_aux barrier_aux = { vconn, NULL };
1826 struct unixctl_server *server;
1827 bool exiting = false;
1828 bool blocked = false;
1829 int error;
1830
1831 daemon_save_fd(STDERR_FILENO);
1832 daemonize_start(false);
1833 error = unixctl_server_create(unixctl_path, &server);
1834 if (error) {
1835 ovs_fatal(error, "failed to create unixctl server");
1836 }
1837 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1838 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1839 ofctl_send, vconn);
1840 unixctl_command_register("ofctl/packet-out", "\"in_port=<port> packet=<hex data> actions=...\"", 1, 1,
1841 unixctl_packet_out, vconn);
1842 unixctl_command_register("ofctl/barrier", "", 0, 0,
1843 ofctl_barrier, &barrier_aux);
1844 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1845 ofctl_set_output_file, NULL);
1846
1847 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1848 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1849 &blocked);
1850
1851 daemonize_complete();
1852
1853 enum ofp_version version = vconn_get_version(vconn);
1854 enum ofputil_protocol protocol
1855 = ofputil_protocol_from_ofp_version(version);
1856
1857 for (;;) {
1858 struct ofpbuf *b;
1859 int retval;
1860
1861 unixctl_server_run(server);
1862
1863 while (!blocked) {
1864 enum ofptype type;
1865
1866 retval = vconn_recv(vconn, &b);
1867 if (retval == EAGAIN) {
1868 break;
1869 }
1870 run(retval, "vconn_recv");
1871
1872 if (timestamp) {
1873 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1874 time_wall_msec(), true);
1875 fputs(s, stderr);
1876 free(s);
1877 }
1878
1879 ofptype_decode(&type, b->data);
1880 ofp_print(stderr, b->data, b->size, verbosity + 2);
1881 fflush(stderr);
1882
1883 switch ((int) type) {
1884 case OFPTYPE_BARRIER_REPLY:
1885 if (barrier_aux.conn) {
1886 unixctl_command_reply(barrier_aux.conn, NULL);
1887 barrier_aux.conn = NULL;
1888 }
1889 break;
1890
1891 case OFPTYPE_ECHO_REQUEST:
1892 if (reply_to_echo_requests) {
1893 struct ofpbuf *reply;
1894
1895 reply = make_echo_reply(b->data);
1896 retval = vconn_send_block(vconn, reply);
1897 if (retval) {
1898 ovs_fatal(retval, "failed to send echo reply");
1899 }
1900 }
1901 break;
1902
1903 case OFPTYPE_PACKET_IN:
1904 if (resume_continuations) {
1905 struct ofputil_packet_in pin;
1906 struct ofpbuf continuation;
1907
1908 error = ofputil_decode_packet_in(b->data, true, NULL, &pin,
1909 NULL, NULL,
1910 &continuation);
1911 if (error) {
1912 fprintf(stderr, "decoding packet-in failed: %s",
1913 ofperr_to_string(error));
1914 } else if (continuation.size) {
1915 struct ofpbuf *reply;
1916
1917 reply = ofputil_encode_resume(&pin, &continuation,
1918 protocol);
1919
1920 fprintf(stderr, "send: ");
1921 ofp_print(stderr, reply->data, reply->size,
1922 verbosity + 2);
1923 fflush(stderr);
1924
1925 retval = vconn_send_block(vconn, reply);
1926 if (retval) {
1927 ovs_fatal(retval, "failed to send NXT_RESUME");
1928 }
1929 }
1930 }
1931 break;
1932 }
1933 ofpbuf_delete(b);
1934 }
1935
1936 if (exiting) {
1937 break;
1938 }
1939
1940 vconn_run(vconn);
1941 vconn_run_wait(vconn);
1942 if (!blocked) {
1943 vconn_recv_wait(vconn);
1944 }
1945 unixctl_server_wait(server);
1946 poll_block();
1947 }
1948 vconn_close(vconn);
1949 unixctl_server_destroy(server);
1950 }
1951
1952 static void
1953 ofctl_monitor(struct ovs_cmdl_context *ctx)
1954 {
1955 struct vconn *vconn;
1956 int i;
1957 enum ofputil_protocol usable_protocols;
1958
1959 /* If the user wants the invalid_ttl_to_controller feature, limit the
1960 * OpenFlow versions to those that support that feature. (Support in
1961 * OpenFlow 1.0 is an Open vSwitch extension.) */
1962 for (i = 2; i < ctx->argc; i++) {
1963 if (!strcmp(ctx->argv[i], "invalid_ttl")) {
1964 uint32_t usable_versions = ((1u << OFP10_VERSION) |
1965 (1u << OFP11_VERSION) |
1966 (1u << OFP12_VERSION));
1967 uint32_t allowed_versions = get_allowed_ofp_versions();
1968 if (!(allowed_versions & usable_versions)) {
1969 struct ds versions = DS_EMPTY_INITIALIZER;
1970 ofputil_format_version_bitmap_names(&versions,
1971 usable_versions);
1972 ovs_fatal(0, "invalid_ttl requires one of the OpenFlow "
1973 "versions %s but none is enabled (use -O)",
1974 ds_cstr(&versions));
1975 }
1976 mask_allowed_ofp_versions(usable_versions);
1977 break;
1978 }
1979 }
1980
1981 open_vconn(ctx->argv[1], &vconn);
1982 bool resume_continuations = false;
1983 for (i = 2; i < ctx->argc; i++) {
1984 const char *arg = ctx->argv[i];
1985
1986 if (isdigit((unsigned char) *arg)) {
1987 struct ofputil_switch_config config;
1988
1989 fetch_switch_config(vconn, &config);
1990 config.miss_send_len = atoi(arg);
1991 set_switch_config(vconn, &config);
1992 } else if (!strcmp(arg, "invalid_ttl")) {
1993 monitor_set_invalid_ttl_to_controller(vconn);
1994 } else if (!strncmp(arg, "watch:", 6)) {
1995 struct ofputil_flow_monitor_request fmr;
1996 struct ofpbuf *msg;
1997 char *error;
1998
1999 error = parse_flow_monitor_request(&fmr, arg + 6,
2000 &usable_protocols);
2001 if (error) {
2002 ovs_fatal(0, "%s", error);
2003 }
2004
2005 msg = ofpbuf_new(0);
2006 ofputil_append_flow_monitor_request(&fmr, msg);
2007 dump_transaction(vconn, msg);
2008 fflush(stdout);
2009 } else if (!strcmp(arg, "resume")) {
2010 /* This option is intentionally undocumented because it is meant
2011 * only for testing. */
2012 resume_continuations = true;
2013
2014 /* Set miss_send_len to ensure that we get packet-ins. */
2015 struct ofputil_switch_config config;
2016 fetch_switch_config(vconn, &config);
2017 config.miss_send_len = UINT16_MAX;
2018 set_switch_config(vconn, &config);
2019 } else {
2020 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
2021 }
2022 }
2023
2024 if (preferred_packet_in_format >= 0) {
2025 /* A particular packet-in format was requested, so we must set it. */
2026 set_packet_in_format(vconn, preferred_packet_in_format, true);
2027 } else {
2028 /* Otherwise, we always prefer NXT_PACKET_IN2. */
2029 if (!set_packet_in_format(vconn, NXPIF_NXT_PACKET_IN2, false)) {
2030 /* We can't get NXT_PACKET_IN2. For OpenFlow 1.0 only, request
2031 * NXT_PACKET_IN. (Before 2.6, Open vSwitch will accept a request
2032 * for NXT_PACKET_IN with OF1.1+, but even after that it still
2033 * sends packet-ins in the OpenFlow native format.) */
2034 if (vconn_get_version(vconn) == OFP10_VERSION) {
2035 set_packet_in_format(vconn, NXPIF_NXT_PACKET_IN, false);
2036 }
2037 }
2038 }
2039
2040 monitor_vconn(vconn, true, resume_continuations);
2041 }
2042
2043 static void
2044 ofctl_snoop(struct ovs_cmdl_context *ctx)
2045 {
2046 struct vconn *vconn;
2047
2048 open_vconn__(ctx->argv[1], SNOOP, &vconn);
2049 monitor_vconn(vconn, false, false);
2050 }
2051
2052 static void
2053 ofctl_dump_ports(struct ovs_cmdl_context *ctx)
2054 {
2055 struct ofpbuf *request;
2056 struct vconn *vconn;
2057 ofp_port_t port;
2058
2059 open_vconn(ctx->argv[1], &vconn);
2060 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
2061 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
2062 dump_transaction(vconn, request);
2063 vconn_close(vconn);
2064 }
2065
2066 static void
2067 ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx)
2068 {
2069 struct ofpbuf *request;
2070 struct vconn *vconn;
2071 ofp_port_t port;
2072
2073 open_vconn(ctx->argv[1], &vconn);
2074 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
2075 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
2076 port);
2077 dump_transaction(vconn, request);
2078 vconn_close(vconn);
2079 }
2080
2081 static void
2082 ofctl_probe(struct ovs_cmdl_context *ctx)
2083 {
2084 struct ofpbuf *request;
2085 struct vconn *vconn;
2086 struct ofpbuf *reply;
2087
2088 open_vconn(ctx->argv[1], &vconn);
2089 request = make_echo_request(vconn_get_version(vconn));
2090 run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]);
2091 if (reply->size != sizeof(struct ofp_header)) {
2092 ovs_fatal(0, "reply does not match request");
2093 }
2094 ofpbuf_delete(reply);
2095 vconn_close(vconn);
2096 }
2097
2098 static void
2099 ofctl_packet_out(struct ovs_cmdl_context *ctx)
2100 {
2101 enum ofputil_protocol usable_protocols;
2102 enum ofputil_protocol protocol;
2103 struct ofputil_packet_out po;
2104 struct vconn *vconn;
2105 struct ofpbuf *opo;
2106 char *error;
2107
2108 /* Use the old syntax when more than 4 arguments are given. */
2109 if (ctx->argc > 4) {
2110 struct ofpbuf ofpacts;
2111 int i;
2112
2113 ofpbuf_init(&ofpacts, 64);
2114 error = ofpacts_parse_actions(ctx->argv[3], &ofpacts,
2115 &usable_protocols);
2116 if (error) {
2117 ovs_fatal(0, "%s", error);
2118 }
2119
2120 po.buffer_id = UINT32_MAX;
2121 po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]);
2122 po.ofpacts = ofpacts.data;
2123 po.ofpacts_len = ofpacts.size;
2124
2125 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn,
2126 usable_protocols);
2127 for (i = 4; i < ctx->argc; i++) {
2128 struct dp_packet *packet;
2129 const char *error_msg;
2130
2131 error_msg = eth_from_hex(ctx->argv[i], &packet);
2132 if (error_msg) {
2133 ovs_fatal(0, "%s", error_msg);
2134 }
2135
2136 po.packet = dp_packet_data(packet);
2137 po.packet_len = dp_packet_size(packet);
2138 opo = ofputil_encode_packet_out(&po, protocol);
2139 transact_noreply(vconn, opo);
2140 dp_packet_delete(packet);
2141 }
2142 vconn_close(vconn);
2143 ofpbuf_uninit(&ofpacts);
2144 } else if (ctx->argc == 3) {
2145 error = parse_ofp_packet_out_str(&po, ctx->argv[2], &usable_protocols);
2146 if (error) {
2147 ovs_fatal(0, "%s", error);
2148 }
2149 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn,
2150 usable_protocols);
2151 opo = ofputil_encode_packet_out(&po, protocol);
2152 transact_noreply(vconn, opo);
2153 vconn_close(vconn);
2154 free(CONST_CAST(void *, po.packet));
2155 free(po.ofpacts);
2156 } else {
2157 ovs_fatal(0, "Too many arguments (%d)", ctx->argc);
2158 }
2159 }
2160
2161 static void
2162 ofctl_mod_port(struct ovs_cmdl_context *ctx)
2163 {
2164 struct ofp_config_flag {
2165 const char *name; /* The flag's name. */
2166 enum ofputil_port_config bit; /* Bit to turn on or off. */
2167 bool on; /* Value to set the bit to. */
2168 };
2169 static const struct ofp_config_flag flags[] = {
2170 { "up", OFPUTIL_PC_PORT_DOWN, false },
2171 { "down", OFPUTIL_PC_PORT_DOWN, true },
2172 { "stp", OFPUTIL_PC_NO_STP, false },
2173 { "receive", OFPUTIL_PC_NO_RECV, false },
2174 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
2175 { "flood", OFPUTIL_PC_NO_FLOOD, false },
2176 { "forward", OFPUTIL_PC_NO_FWD, false },
2177 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
2178 };
2179
2180 const struct ofp_config_flag *flag;
2181 enum ofputil_protocol protocol;
2182 struct ofputil_port_mod pm;
2183 struct ofputil_phy_port pp;
2184 struct vconn *vconn;
2185 const char *command;
2186 bool not;
2187
2188 fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp);
2189
2190 pm.port_no = pp.port_no;
2191 pm.hw_addr = pp.hw_addr;
2192 pm.config = 0;
2193 pm.mask = 0;
2194 pm.advertise = 0;
2195
2196 if (!strncasecmp(ctx->argv[3], "no-", 3)) {
2197 command = ctx->argv[3] + 3;
2198 not = true;
2199 } else if (!strncasecmp(ctx->argv[3], "no", 2)) {
2200 command = ctx->argv[3] + 2;
2201 not = true;
2202 } else {
2203 command = ctx->argv[3];
2204 not = false;
2205 }
2206 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
2207 if (!strcasecmp(command, flag->name)) {
2208 pm.mask = flag->bit;
2209 pm.config = flag->on ^ not ? flag->bit : 0;
2210 goto found;
2211 }
2212 }
2213 ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]);
2214
2215 found:
2216 protocol = open_vconn(ctx->argv[1], &vconn);
2217 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
2218 vconn_close(vconn);
2219 }
2220
2221 /* This function uses OFPMP14_TABLE_DESC request to get the current
2222 * table configuration from switch. The function then modifies
2223 * only that table-config property, which has been requested. */
2224 static void
2225 fetch_table_desc(struct vconn *vconn, struct ofputil_table_mod *tm,
2226 struct ofputil_table_desc *td)
2227 {
2228 struct ofpbuf *request;
2229 ovs_be32 send_xid;
2230 bool done = false;
2231 bool found = false;
2232
2233 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
2234 send_xid = ((struct ofp_header *) request->data)->xid;
2235 send_openflow_buffer(vconn, request);
2236 while (!done) {
2237 ovs_be32 recv_xid;
2238 struct ofpbuf *reply;
2239
2240 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
2241 recv_xid = ((struct ofp_header *) reply->data)->xid;
2242 if (send_xid == recv_xid) {
2243 struct ofp_header *oh = reply->data;
2244 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2245
2246 enum ofptype type;
2247 if (ofptype_pull(&type, &b)
2248 || type != OFPTYPE_TABLE_DESC_REPLY) {
2249 ovs_fatal(0, "received bad reply: %s",
2250 ofp_to_string(reply->data, reply->size,
2251 verbosity + 1));
2252 }
2253 uint16_t flags = ofpmp_flags(oh);
2254 done = !(flags & OFPSF_REPLY_MORE);
2255 if (found) {
2256 /* We've already found the table desc consisting of current
2257 * table configuration, but we need to drain the queue of
2258 * any other replies for this request. */
2259 continue;
2260 }
2261 while (!ofputil_decode_table_desc(&b, td, oh->version)) {
2262 if (td->table_id == tm->table_id) {
2263 found = true;
2264 break;
2265 }
2266 }
2267 } else {
2268 VLOG_DBG("received reply with xid %08"PRIx32" "
2269 "!= expected %08"PRIx32, recv_xid, send_xid);
2270 }
2271 ofpbuf_delete(reply);
2272 }
2273 if (tm->eviction != OFPUTIL_TABLE_EVICTION_DEFAULT) {
2274 tm->vacancy = td->vacancy;
2275 tm->table_vacancy.vacancy_down = td->table_vacancy.vacancy_down;
2276 tm->table_vacancy.vacancy_up = td->table_vacancy.vacancy_up;
2277 } else if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) {
2278 tm->eviction = td->eviction;
2279 tm->eviction_flags = td->eviction_flags;
2280 }
2281 }
2282
2283 static void
2284 ofctl_mod_table(struct ovs_cmdl_context *ctx)
2285 {
2286 uint32_t usable_versions;
2287 struct ofputil_table_mod tm;
2288 struct vconn *vconn;
2289 char *error;
2290 int i;
2291
2292 error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3],
2293 &usable_versions);
2294 if (error) {
2295 ovs_fatal(0, "%s", error);
2296 }
2297
2298 uint32_t allowed_versions = get_allowed_ofp_versions();
2299 if (!(allowed_versions & usable_versions)) {
2300 struct ds versions = DS_EMPTY_INITIALIZER;
2301 ofputil_format_version_bitmap_names(&versions, usable_versions);
2302 ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow "
2303 "versions %s",
2304 ctx->argv[3], ds_cstr(&versions));
2305 }
2306 mask_allowed_ofp_versions(usable_versions);
2307 enum ofputil_protocol protocol = open_vconn(ctx->argv[1], &vconn);
2308
2309 /* For OpenFlow 1.4+, ovs-ofctl mod-table should not affect table-config
2310 * properties that the user didn't ask to change, so it is necessary to
2311 * restore the current configuration of table-config parameters using
2312 * OFPMP14_TABLE_DESC request. */
2313 if ((allowed_versions & (1u << OFP14_VERSION)) ||
2314 (allowed_versions & (1u << OFP15_VERSION))) {
2315 struct ofputil_table_desc td;
2316
2317 if (tm.table_id == OFPTT_ALL) {
2318 for (i = 0; i < OFPTT_MAX; i++) {
2319 tm.table_id = i;
2320 fetch_table_desc(vconn, &tm, &td);
2321 transact_noreply(vconn,
2322 ofputil_encode_table_mod(&tm, protocol));
2323 }
2324 } else {
2325 fetch_table_desc(vconn, &tm, &td);
2326 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2327 }
2328 } else {
2329 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
2330 }
2331 vconn_close(vconn);
2332 }
2333
2334 static void
2335 ofctl_get_frags(struct ovs_cmdl_context *ctx)
2336 {
2337 struct ofputil_switch_config config;
2338 struct vconn *vconn;
2339
2340 open_vconn(ctx->argv[1], &vconn);
2341 fetch_switch_config(vconn, &config);
2342 puts(ofputil_frag_handling_to_string(config.frag));
2343 vconn_close(vconn);
2344 }
2345
2346 static void
2347 ofctl_set_frags(struct ovs_cmdl_context *ctx)
2348 {
2349 struct ofputil_switch_config config;
2350 enum ofputil_frag_handling frag;
2351 struct vconn *vconn;
2352
2353 if (!ofputil_frag_handling_from_string(ctx->argv[2], &frag)) {
2354 ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]);
2355 }
2356
2357 open_vconn(ctx->argv[1], &vconn);
2358 fetch_switch_config(vconn, &config);
2359 if (frag != config.frag) {
2360 /* Set the configuration. */
2361 config.frag = frag;
2362 set_switch_config(vconn, &config);
2363
2364 /* Then retrieve the configuration to see if it really took. OpenFlow
2365 * has ill-defined error reporting for bad flags, so this is about the
2366 * best we can do. */
2367 fetch_switch_config(vconn, &config);
2368 if (frag != config.frag) {
2369 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
2370 "switch probably doesn't support mode \"%s\")",
2371 ctx->argv[1], ofputil_frag_handling_to_string(frag));
2372 }
2373 }
2374 vconn_close(vconn);
2375 }
2376
2377 static void
2378 ofctl_ofp_parse(struct ovs_cmdl_context *ctx)
2379 {
2380 const char *filename = ctx->argv[1];
2381 struct ofpbuf b;
2382 FILE *file;
2383
2384 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2385 if (file == NULL) {
2386 ovs_fatal(errno, "%s: open", filename);
2387 }
2388
2389 ofpbuf_init(&b, 65536);
2390 for (;;) {
2391 struct ofp_header *oh;
2392 size_t length, tail_len;
2393 void *tail;
2394 size_t n;
2395
2396 ofpbuf_clear(&b);
2397 oh = ofpbuf_put_uninit(&b, sizeof *oh);
2398 n = fread(oh, 1, sizeof *oh, file);
2399 if (n == 0) {
2400 break;
2401 } else if (n < sizeof *oh) {
2402 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2403 }
2404
2405 length = ntohs(oh->length);
2406 if (length < sizeof *oh) {
2407 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
2408 filename, length);
2409 }
2410
2411 tail_len = length - sizeof *oh;
2412 tail = ofpbuf_put_uninit(&b, tail_len);
2413 n = fread(tail, 1, tail_len, file);
2414 if (n < tail_len) {
2415 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2416 }
2417
2418 ofp_print(stdout, b.data, b.size, verbosity + 2);
2419 }
2420 ofpbuf_uninit(&b);
2421
2422 if (file != stdin) {
2423 fclose(file);
2424 }
2425 }
2426
2427 static bool
2428 is_openflow_port(ovs_be16 port_, char *ports[])
2429 {
2430 uint16_t port = ntohs(port_);
2431 if (ports[0]) {
2432 int i;
2433
2434 for (i = 0; ports[i]; i++) {
2435 if (port == atoi(ports[i])) {
2436 return true;
2437 }
2438 }
2439 return false;
2440 } else {
2441 return port == OFP_PORT || port == OFP_OLD_PORT;
2442 }
2443 }
2444
2445 static void
2446 ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx)
2447 {
2448 struct tcp_reader *reader;
2449 FILE *file;
2450 int error;
2451 bool first;
2452
2453 file = ovs_pcap_open(ctx->argv[1], "rb");
2454 if (!file) {
2455 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
2456 }
2457
2458 reader = tcp_reader_open();
2459 first = true;
2460 for (;;) {
2461 struct dp_packet *packet;
2462 long long int when;
2463 struct flow flow;
2464
2465 error = ovs_pcap_read(file, &packet, &when);
2466 if (error) {
2467 break;
2468 }
2469 pkt_metadata_init(&packet->md, ODPP_NONE);
2470 flow_extract(packet, &flow);
2471 if (flow.dl_type == htons(ETH_TYPE_IP)
2472 && flow.nw_proto == IPPROTO_TCP
2473 && (is_openflow_port(flow.tp_src, ctx->argv + 2) ||
2474 is_openflow_port(flow.tp_dst, ctx->argv + 2))) {
2475 struct dp_packet *payload = tcp_reader_run(reader, &flow, packet);
2476 if (payload) {
2477 while (dp_packet_size(payload) >= sizeof(struct ofp_header)) {
2478 const struct ofp_header *oh;
2479 void *data = dp_packet_data(payload);
2480 int length;
2481
2482 /* Align OpenFlow on 8-byte boundary for safe access. */
2483 dp_packet_shift(payload, -((intptr_t) data & 7));
2484
2485 oh = dp_packet_data(payload);
2486 length = ntohs(oh->length);
2487 if (dp_packet_size(payload) < length) {
2488 break;
2489 }
2490
2491 if (!first) {
2492 putchar('\n');
2493 }
2494 first = false;
2495
2496 if (timestamp) {
2497 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
2498 fputs(s, stdout);
2499 free(s);
2500 }
2501
2502 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
2503 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
2504 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
2505 ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1);
2506 dp_packet_pull(payload, length);
2507 }
2508 }
2509 }
2510 dp_packet_delete(packet);
2511 }
2512 tcp_reader_close(reader);
2513 }
2514
2515 static void
2516 ofctl_ping(struct ovs_cmdl_context *ctx)
2517 {
2518 size_t max_payload = 65535 - sizeof(struct ofp_header);
2519 unsigned int payload;
2520 struct vconn *vconn;
2521 int i;
2522
2523 payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64;
2524 if (payload > max_payload) {
2525 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2526 }
2527
2528 open_vconn(ctx->argv[1], &vconn);
2529 for (i = 0; i < 10; i++) {
2530 struct timeval start, end;
2531 struct ofpbuf *request, *reply;
2532 const struct ofp_header *rpy_hdr;
2533 enum ofptype type;
2534
2535 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2536 vconn_get_version(vconn), payload);
2537 random_bytes(ofpbuf_put_uninit(request, payload), payload);
2538
2539 xgettimeofday(&start);
2540 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2541 xgettimeofday(&end);
2542
2543 rpy_hdr = reply->data;
2544 if (ofptype_pull(&type, reply)
2545 || type != OFPTYPE_ECHO_REPLY
2546 || reply->size != payload
2547 || memcmp(request->msg, reply->msg, payload)) {
2548 printf("Reply does not match request. Request:\n");
2549 ofp_print(stdout, request, request->size, verbosity + 2);
2550 printf("Reply:\n");
2551 ofp_print(stdout, reply, reply->size, verbosity + 2);
2552 }
2553 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2554 reply->size, ctx->argv[1], ntohl(rpy_hdr->xid),
2555 (1000*(double)(end.tv_sec - start.tv_sec))
2556 + (.001*(end.tv_usec - start.tv_usec)));
2557 ofpbuf_delete(request);
2558 ofpbuf_delete(reply);
2559 }
2560 vconn_close(vconn);
2561 }
2562
2563 static void
2564 ofctl_benchmark(struct ovs_cmdl_context *ctx)
2565 {
2566 size_t max_payload = 65535 - sizeof(struct ofp_header);
2567 struct timeval start, end;
2568 unsigned int payload_size, message_size;
2569 struct vconn *vconn;
2570 double duration;
2571 int count;
2572 int i;
2573
2574 payload_size = atoi(ctx->argv[2]);
2575 if (payload_size > max_payload) {
2576 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2577 }
2578 message_size = sizeof(struct ofp_header) + payload_size;
2579
2580 count = atoi(ctx->argv[3]);
2581
2582 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2583 count, message_size, count * message_size);
2584
2585 open_vconn(ctx->argv[1], &vconn);
2586 xgettimeofday(&start);
2587 for (i = 0; i < count; i++) {
2588 struct ofpbuf *request, *reply;
2589
2590 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2591 vconn_get_version(vconn), payload_size);
2592 ofpbuf_put_zeros(request, payload_size);
2593 run(vconn_transact(vconn, request, &reply), "transact");
2594 ofpbuf_delete(reply);
2595 }
2596 xgettimeofday(&end);
2597 vconn_close(vconn);
2598
2599 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2600 + (.001*(end.tv_usec - start.tv_usec)));
2601 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2602 duration, count / (duration / 1000.0),
2603 count * message_size / (duration / 1000.0));
2604 }
2605
2606 static void
2607 ofctl_dump_ipfix_bridge(struct ovs_cmdl_context *ctx)
2608 {
2609 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXST_IPFIX_BRIDGE_REQUEST);
2610 }
2611
2612 static void
2613 ofctl_ct_flush_zone(struct ovs_cmdl_context *ctx)
2614 {
2615 uint16_t zone_id;
2616 char *error = str_to_u16(ctx->argv[2], "zone_id", &zone_id);
2617 if (error) {
2618 ovs_fatal(0, "%s", error);
2619 }
2620
2621 struct vconn *vconn;
2622 open_vconn(ctx->argv[1], &vconn);
2623 enum ofp_version version = vconn_get_version(vconn);
2624
2625 struct ofpbuf *msg = ofpraw_alloc(OFPRAW_NXT_CT_FLUSH_ZONE, version, 0);
2626 struct nx_zone_id *nzi = ofpbuf_put_zeros(msg, sizeof *nzi);
2627 nzi->zone_id = htons(zone_id);
2628
2629 transact_noreply(vconn, msg);
2630 vconn_close(vconn);
2631 }
2632
2633 static void
2634 ofctl_dump_ipfix_flow(struct ovs_cmdl_context *ctx)
2635 {
2636 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXST_IPFIX_FLOW_REQUEST);
2637 }
2638
2639 static void
2640 bundle_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2641 size_t n_gms, enum ofputil_protocol usable_protocols)
2642 {
2643 enum ofputil_protocol protocol;
2644 enum ofp_version version;
2645 struct vconn *vconn;
2646 struct ovs_list requests;
2647 size_t i;
2648
2649 ovs_list_init(&requests);
2650
2651 /* Bundles need OpenFlow 1.3+. */
2652 usable_protocols &= OFPUTIL_P_OF13_UP;
2653 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2654 version = ofputil_protocol_to_ofp_version(protocol);
2655
2656 for (i = 0; i < n_gms; i++) {
2657 struct ofputil_group_mod *gm = &gms[i];
2658 struct ofpbuf *request = ofputil_encode_group_mod(version, gm);
2659
2660 ovs_list_push_back(&requests, &request->list_node);
2661 ofputil_uninit_group_mod(gm);
2662 }
2663
2664 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
2665 ofpbuf_list_delete(&requests);
2666 vconn_close(vconn);
2667 }
2668
2669 static void
2670 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2671 size_t n_gms, enum ofputil_protocol usable_protocols)
2672 {
2673 enum ofputil_protocol protocol;
2674 struct ofputil_group_mod *gm;
2675 enum ofp_version version;
2676 struct ofpbuf *request;
2677
2678 struct vconn *vconn;
2679 size_t i;
2680
2681 if (bundle) {
2682 bundle_group_mod__(remote, gms, n_gms, usable_protocols);
2683 return;
2684 }
2685
2686 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2687 version = ofputil_protocol_to_ofp_version(protocol);
2688
2689 for (i = 0; i < n_gms; i++) {
2690 gm = &gms[i];
2691 request = ofputil_encode_group_mod(version, gm);
2692 transact_noreply(vconn, request);
2693 ofputil_uninit_group_mod(gm);
2694 }
2695
2696 vconn_close(vconn);
2697 }
2698
2699 static void
2700 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], int command)
2701 {
2702 struct ofputil_group_mod *gms = NULL;
2703 enum ofputil_protocol usable_protocols;
2704 size_t n_gms = 0;
2705 char *error;
2706
2707 if (command == OFPGC11_ADD) {
2708 /* Allow the file to specify a mix of commands. If none specified at
2709 * the beginning of any given line, then the default is OFPGC11_ADD, so
2710 * this is backwards compatible. */
2711 command = -2;
2712 }
2713 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2714 &usable_protocols);
2715 if (error) {
2716 ovs_fatal(0, "%s", error);
2717 }
2718 ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
2719 free(gms);
2720 }
2721
2722 static void
2723 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2724 {
2725 if (argc > 2 && !strcmp(argv[2], "-")) {
2726 ofctl_group_mod_file(argc, argv, command);
2727 } else {
2728 enum ofputil_protocol usable_protocols;
2729 struct ofputil_group_mod gm;
2730 char *error;
2731
2732 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2733 &usable_protocols);
2734 if (error) {
2735 ovs_fatal(0, "%s", error);
2736 }
2737 ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
2738 }
2739 }
2740
2741 static void
2742 ofctl_add_group(struct ovs_cmdl_context *ctx)
2743 {
2744 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD);
2745 }
2746
2747 static void
2748 ofctl_add_groups(struct ovs_cmdl_context *ctx)
2749 {
2750 ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD);
2751 }
2752
2753 static void
2754 ofctl_mod_group(struct ovs_cmdl_context *ctx)
2755 {
2756 ofctl_group_mod(ctx->argc, ctx->argv,
2757 may_create ? OFPGC11_ADD_OR_MOD : OFPGC11_MODIFY);
2758 }
2759
2760 static void
2761 ofctl_del_groups(struct ovs_cmdl_context *ctx)
2762 {
2763 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE);
2764 }
2765
2766 static void
2767 ofctl_insert_bucket(struct ovs_cmdl_context *ctx)
2768 {
2769 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET);
2770 }
2771
2772 static void
2773 ofctl_remove_bucket(struct ovs_cmdl_context *ctx)
2774 {
2775 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET);
2776 }
2777
2778 static void
2779 ofctl_dump_group_stats(struct ovs_cmdl_context *ctx)
2780 {
2781 enum ofputil_protocol usable_protocols;
2782 struct ofputil_group_mod gm;
2783 struct ofpbuf *request;
2784 struct vconn *vconn;
2785 uint32_t group_id;
2786 char *error;
2787
2788 memset(&gm, 0, sizeof gm);
2789
2790 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2791 ctx->argc > 2 ? ctx->argv[2] : "",
2792 &usable_protocols);
2793 if (error) {
2794 ovs_fatal(0, "%s", error);
2795 }
2796
2797 group_id = gm.group_id;
2798
2799 open_vconn(ctx->argv[1], &vconn);
2800 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2801 group_id);
2802 if (request) {
2803 dump_transaction(vconn, request);
2804 }
2805
2806 vconn_close(vconn);
2807 }
2808
2809 static void
2810 ofctl_dump_group_desc(struct ovs_cmdl_context *ctx)
2811 {
2812 struct ofpbuf *request;
2813 struct vconn *vconn;
2814 uint32_t group_id;
2815
2816 open_vconn(ctx->argv[1], &vconn);
2817
2818 if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) {
2819 group_id = OFPG_ALL;
2820 }
2821
2822 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2823 group_id);
2824 if (request) {
2825 dump_transaction(vconn, request);
2826 }
2827
2828 vconn_close(vconn);
2829 }
2830
2831 static void
2832 ofctl_dump_group_features(struct ovs_cmdl_context *ctx)
2833 {
2834 struct ofpbuf *request;
2835 struct vconn *vconn;
2836
2837 open_vconn(ctx->argv[1], &vconn);
2838 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2839 if (request) {
2840 dump_transaction(vconn, request);
2841 }
2842
2843 vconn_close(vconn);
2844 }
2845
2846 static void
2847 ofctl_bundle(struct ovs_cmdl_context *ctx)
2848 {
2849 enum ofputil_protocol protocol, usable_protocols;
2850 struct ofputil_bundle_msg *bms;
2851 struct ovs_list requests;
2852 struct vconn *vconn;
2853 size_t n_bms;
2854 char *error;
2855
2856 error = parse_ofp_bundle_file(ctx->argv[2], &bms, &n_bms,
2857 &usable_protocols);
2858 if (error) {
2859 ovs_fatal(0, "%s", error);
2860 }
2861
2862 /* Implicit OpenFlow 1.4. */
2863 if (!(get_allowed_ofp_versions() &
2864 ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP))) {
2865
2866 /* Add implicit allowance for OpenFlow 1.4. */
2867 add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
2868 OFPUTIL_P_OF14_OXM));
2869 /* Remove all versions that do not support bundles. */
2870 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
2871 OFPUTIL_P_OF13_UP));
2872 allowed_protocols = ofputil_protocols_from_version_bitmap(
2873 get_allowed_ofp_versions());
2874 }
2875
2876 /* Bundles need OpenFlow 1.3+. */
2877 usable_protocols &= OFPUTIL_P_OF13_UP;
2878 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols);
2879
2880 ovs_list_init(&requests);
2881 ofputil_encode_bundle_msgs(bms, n_bms, &requests, protocol);
2882 ofputil_free_bundle_msgs(bms, n_bms);
2883 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
2884 ofpbuf_list_delete(&requests);
2885
2886 vconn_close(vconn);
2887 }
2888
2889 static void
2890 ofctl_tlv_mod(struct ovs_cmdl_context *ctx, uint16_t command)
2891 {
2892 enum ofputil_protocol usable_protocols;
2893 enum ofputil_protocol protocol;
2894 struct ofputil_tlv_table_mod ttm;
2895 char *error;
2896 enum ofp_version version;
2897 struct ofpbuf *request;
2898 struct vconn *vconn;
2899
2900 error = parse_ofp_tlv_table_mod_str(&ttm, command, ctx->argc > 2 ?
2901 ctx->argv[2] : "",
2902 &usable_protocols);
2903 if (error) {
2904 ovs_fatal(0, "%s", error);
2905 }
2906
2907 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols);
2908 version = ofputil_protocol_to_ofp_version(protocol);
2909
2910 request = ofputil_encode_tlv_table_mod(version, &ttm);
2911 if (request) {
2912 transact_noreply(vconn, request);
2913 }
2914
2915 vconn_close(vconn);
2916 ofputil_uninit_tlv_table(&ttm.mappings);
2917 }
2918
2919 static void
2920 ofctl_add_tlv_map(struct ovs_cmdl_context *ctx)
2921 {
2922 ofctl_tlv_mod(ctx, NXTTMC_ADD);
2923 }
2924
2925 static void
2926 ofctl_del_tlv_map(struct ovs_cmdl_context *ctx)
2927 {
2928 ofctl_tlv_mod(ctx, ctx->argc > 2 ? NXTTMC_DELETE : NXTTMC_CLEAR);
2929 }
2930
2931 static void
2932 ofctl_dump_tlv_map(struct ovs_cmdl_context *ctx)
2933 {
2934 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXT_TLV_TABLE_REQUEST);
2935 }
2936
2937 static void
2938 ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
2939 {
2940 usage();
2941 }
2942
2943 static void
2944 ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
2945 {
2946 ovs_cmdl_print_commands(get_all_commands());
2947 }
2948 \f
2949 /* replace-flows and diff-flows commands. */
2950
2951 struct flow_tables {
2952 struct classifier tables[OFPTT_MAX + 1];
2953 };
2954
2955 #define FOR_EACH_TABLE(CLS, TABLES) \
2956 for ((CLS) = (TABLES)->tables; \
2957 (CLS) < &(TABLES)->tables[ARRAY_SIZE((TABLES)->tables)]; \
2958 (CLS)++)
2959
2960 static void
2961 flow_tables_init(struct flow_tables *tables)
2962 {
2963 struct classifier *cls;
2964
2965 FOR_EACH_TABLE (cls, tables) {
2966 classifier_init(cls, NULL);
2967 }
2968 }
2969
2970 static void
2971 flow_tables_defer(struct flow_tables *tables)
2972 {
2973 struct classifier *cls;
2974
2975 FOR_EACH_TABLE (cls, tables) {
2976 classifier_defer(cls);
2977 }
2978 }
2979
2980 static void
2981 flow_tables_publish(struct flow_tables *tables)
2982 {
2983 struct classifier *cls;
2984
2985 FOR_EACH_TABLE (cls, tables) {
2986 classifier_publish(cls);
2987 }
2988 }
2989
2990 /* A flow table entry, possibly with two different versions. */
2991 struct fte {
2992 struct cls_rule rule; /* Within a "struct classifier". */
2993 struct fte_version *versions[2];
2994 };
2995
2996 /* One version of a Flow Table Entry. */
2997 struct fte_version {
2998 ovs_be64 cookie;
2999 uint16_t idle_timeout;
3000 uint16_t hard_timeout;
3001 uint16_t importance;
3002 uint16_t flags;
3003 struct ofpact *ofpacts;
3004 size_t ofpacts_len;
3005 uint8_t table_id;
3006 };
3007
3008 /* A FTE entry that has been queued for later insertion after all
3009 * flows have been scanned to correctly allocation tunnel metadata. */
3010 struct fte_pending {
3011 struct match *match;
3012 int priority;
3013 struct fte_version *version;
3014 int index;
3015
3016 struct ovs_list list_node;
3017 };
3018
3019 /* Processing state during two stage processing of flow table entries.
3020 * Tracks the maximum size seen for each tunnel metadata entry as well
3021 * as a list of the pending FTE entries. */
3022 struct fte_state {
3023 int tun_metadata_size[TUN_METADATA_NUM_OPTS];
3024 struct ovs_list fte_pending_list;
3025
3026 /* The final metadata table that we have constructed. */
3027 struct tun_table *tun_tab;
3028 };
3029
3030 /* Frees 'version' and the data that it owns. */
3031 static void
3032 fte_version_free(struct fte_version *version)
3033 {
3034 if (version) {
3035 free(CONST_CAST(struct ofpact *, version->ofpacts));
3036 free(version);
3037 }
3038 }
3039
3040 /* Returns true if 'a' and 'b' are the same, false if they differ.
3041 *
3042 * Ignores differences in 'flags' because there's no way to retrieve flags from
3043 * an OpenFlow switch. We have to assume that they are the same. */
3044 static bool
3045 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
3046 {
3047 return (a->cookie == b->cookie
3048 && a->idle_timeout == b->idle_timeout
3049 && a->hard_timeout == b->hard_timeout
3050 && a->importance == b->importance
3051 && a->table_id == b->table_id
3052 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
3053 b->ofpacts, b->ofpacts_len));
3054 }
3055
3056 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
3057 * 'index' into 's', followed by a new-line. */
3058 static void
3059 fte_version_format(const struct fte_state *fte_state, const struct fte *fte,
3060 int index, struct ds *s)
3061 {
3062 const struct fte_version *version = fte->versions[index];
3063
3064 ds_clear(s);
3065 if (!version) {
3066 return;
3067 }
3068
3069 if (version->table_id) {
3070 ds_put_format(s, "table=%"PRIu8" ", version->table_id);
3071 }
3072 cls_rule_format(&fte->rule, fte_state->tun_tab, s);
3073 if (version->cookie != htonll(0)) {
3074 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
3075 }
3076 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
3077 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
3078 }
3079 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
3080 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
3081 }
3082 if (version->importance != 0) {
3083 ds_put_format(s, " importance=%"PRIu16, version->importance);
3084 }
3085
3086 ds_put_cstr(s, " actions=");
3087 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
3088
3089 ds_put_char(s, '\n');
3090 }
3091
3092 static struct fte *
3093 fte_from_cls_rule(const struct cls_rule *cls_rule)
3094 {
3095 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
3096 }
3097
3098 /* Frees 'fte' and its versions. */
3099 static void
3100 fte_free(struct fte *fte)
3101 {
3102 if (fte) {
3103 fte_version_free(fte->versions[0]);
3104 fte_version_free(fte->versions[1]);
3105 cls_rule_destroy(&fte->rule);
3106 free(fte);
3107 }
3108 }
3109
3110 /* Frees all of the FTEs within 'tables'. */
3111 static void
3112 fte_free_all(struct flow_tables *tables)
3113 {
3114 struct classifier *cls;
3115
3116 FOR_EACH_TABLE (cls, tables) {
3117 struct fte *fte;
3118
3119 classifier_defer(cls);
3120 CLS_FOR_EACH (fte, rule, cls) {
3121 classifier_remove(cls, &fte->rule);
3122 ovsrcu_postpone(fte_free, fte);
3123 }
3124 classifier_destroy(cls);
3125 }
3126 }
3127
3128 /* Searches 'tables' for an FTE matching 'rule', inserting a new one if
3129 * necessary. Sets 'version' as the version of that rule with the given
3130 * 'index', replacing any existing version, if any.
3131 *
3132 * Takes ownership of 'version'. */
3133 static void
3134 fte_insert(struct flow_tables *tables, const struct match *match,
3135 int priority, struct fte_version *version, int index)
3136 {
3137 struct classifier *cls = &tables->tables[version->table_id];
3138 struct fte *old, *fte;
3139
3140 fte = xzalloc(sizeof *fte);
3141 cls_rule_init(&fte->rule, match, priority);
3142 fte->versions[index] = version;
3143
3144 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule,
3145 OVS_VERSION_MIN, NULL, 0));
3146 if (old) {
3147 fte->versions[!index] = old->versions[!index];
3148 old->versions[!index] = NULL;
3149
3150 ovsrcu_postpone(fte_free, old);
3151 }
3152 }
3153
3154 /* Given a list of the field sizes for each tunnel metadata entry, install
3155 * a mapping table for later operations. */
3156 static void
3157 generate_tun_metadata(struct fte_state *state)
3158 {
3159 struct ofputil_tlv_table_mod ttm;
3160 int i;
3161
3162 ttm.command = NXTTMC_ADD;
3163 ovs_list_init(&ttm.mappings);
3164
3165 for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) {
3166 if (state->tun_metadata_size[i] != -1) {
3167 struct ofputil_tlv_map *map = xmalloc(sizeof *map);
3168
3169 ovs_list_push_back(&ttm.mappings, &map->list_node);
3170
3171 /* We don't care about the actual option class and type since there
3172 * won't be any lookup. We just need to make them unique. */
3173 map->option_class = i / UINT8_MAX;
3174 map->option_type = i;
3175 map->option_len = ROUND_UP(state->tun_metadata_size[i], 4);
3176 map->index = i;
3177 }
3178 }
3179
3180 tun_metadata_table_mod(&ttm, NULL, &state->tun_tab);
3181 ofputil_uninit_tlv_table(&ttm.mappings);
3182 }
3183
3184 /* Once we have created a tunnel mapping table with a consistent overall
3185 * allocation, we need to remap each flow to use this table from its own
3186 * allocation. Since the mapping table has already been installed, we
3187 * can just read the data from the match and rewrite it. On rewrite, it
3188 * will use the new table. */
3189 static void
3190 remap_match(struct fte_state *state, struct match *match)
3191 {
3192 int i;
3193
3194 if (!match->tun_md.valid) {
3195 return;
3196 }
3197
3198 struct tun_metadata flow = match->flow.tunnel.metadata;
3199 struct tun_metadata flow_mask = match->wc.masks.tunnel.metadata;
3200 memset(&match->flow.tunnel.metadata, 0, sizeof match->flow.tunnel.metadata);
3201 memset(&match->wc.masks.tunnel.metadata, 0,
3202 sizeof match->wc.masks.tunnel.metadata);
3203 match->tun_md.valid = false;
3204
3205 match->flow.tunnel.metadata.tab = state->tun_tab;
3206 match->wc.masks.tunnel.metadata.tab = match->flow.tunnel.metadata.tab;
3207
3208 ULLONG_FOR_EACH_1 (i, flow_mask.present.map) {
3209 const struct mf_field *field = mf_from_id(MFF_TUN_METADATA0 + i);
3210 int offset = match->tun_md.entry[i].loc.c.offset;
3211 int len = match->tun_md.entry[i].loc.len;
3212 union mf_value value, mask;
3213
3214 memset(&value, 0, field->n_bytes - len);
3215 memset(&mask, match->tun_md.entry[i].masked ? 0 : 0xff,
3216 field->n_bytes - len);
3217
3218 memcpy(value.tun_metadata + field->n_bytes - len,
3219 flow.opts.u8 + offset, len);
3220 memcpy(mask.tun_metadata + field->n_bytes - len,
3221 flow_mask.opts.u8 + offset, len);
3222 mf_set(field, &value, &mask, match, NULL);
3223 }
3224 }
3225
3226 /* In order to correctly handle tunnel metadata, we need to have
3227 * two passes over the flows. This happens because tunnel metadata
3228 * doesn't have fixed locations in a flow entry but is instead dynamically
3229 * allocated space. In the case of flows coming from a file, we don't
3230 * even know the size of each field when we need to do the allocation.
3231 * When the flows come in, each flow has an individual allocation based
3232 * on its own fields. However, this allocation is not the same across
3233 * different flows and therefore fields are not directly comparable.
3234 *
3235 * In the first pass, we record the maximum size of each tunnel metadata
3236 * field as well as queue FTE entries for later processing.
3237 *
3238 * In the second pass, we use the metadata size information to create a
3239 * tunnel mapping table and set that through the tunnel metadata processing
3240 * code. We then remap all individual flows to use this common allocation
3241 * scheme. Finally, we load the queued entries into the classifier for
3242 * comparison.
3243 *
3244 * fte_state_init() should be called before processing any flows. */
3245 static void
3246 fte_state_init(struct fte_state *state)
3247 {
3248 int i;
3249
3250 for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) {
3251 state->tun_metadata_size[i] = -1;
3252 }
3253
3254 ovs_list_init(&state->fte_pending_list);
3255 state->tun_tab = NULL;
3256 }
3257
3258 static void
3259 fte_state_destroy(struct fte_state *state)
3260 {
3261 tun_metadata_free(state->tun_tab);
3262 }
3263
3264 /* The first pass of the processing described in the comment about
3265 * fte_state_init(). fte_queue() is the first pass to be called as each
3266 * flow is read from its source. */
3267 static void
3268 fte_queue(struct fte_state *state, const struct match *match,
3269 int priority, struct fte_version *version, int index)
3270 {
3271 struct fte_pending *pending = xmalloc(sizeof *pending);
3272 int i;
3273
3274 pending->match = xmemdup(match, sizeof *match);
3275 pending->priority = priority;
3276 pending->version = version;
3277 pending->index = index;
3278 ovs_list_push_back(&state->fte_pending_list, &pending->list_node);
3279
3280 if (!match->tun_md.valid) {
3281 return;
3282 }
3283
3284 ULLONG_FOR_EACH_1 (i, match->wc.masks.tunnel.metadata.present.map) {
3285 if (match->tun_md.entry[i].loc.len > state->tun_metadata_size[i]) {
3286 state->tun_metadata_size[i] = match->tun_md.entry[i].loc.len;
3287 }
3288 }
3289 }
3290
3291 /* The second pass of the processing described in the comment about
3292 * fte_state_init(). This should be called once all flows (from both
3293 * sides of the comparison) have been added through fte_queue(). */
3294 static void
3295 fte_fill(struct fte_state *state, struct flow_tables *tables)
3296 {
3297 struct fte_pending *pending;
3298
3299 generate_tun_metadata(state);
3300
3301 flow_tables_init(tables);
3302 flow_tables_defer(tables);
3303
3304 LIST_FOR_EACH_POP(pending, list_node, &state->fte_pending_list) {
3305 remap_match(state, pending->match);
3306 fte_insert(tables, pending->match, pending->priority, pending->version,
3307 pending->index);
3308 free(pending->match);
3309 free(pending);
3310 }
3311
3312 flow_tables_publish(tables);
3313 }
3314
3315 /* Reads the flows in 'filename' as flow table entries in 'tables' for the
3316 * version with the specified 'index'. Returns the flow formats able to
3317 * represent the flows that were read. */
3318 static enum ofputil_protocol
3319 read_flows_from_file(const char *filename, struct fte_state *state, int index)
3320 {
3321 enum ofputil_protocol usable_protocols;
3322 int line_number;
3323 struct ds s;
3324 FILE *file;
3325
3326 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
3327 if (file == NULL) {
3328 ovs_fatal(errno, "%s: open", filename);
3329 }
3330
3331 ds_init(&s);
3332 usable_protocols = OFPUTIL_P_ANY;
3333 line_number = 0;
3334 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
3335 struct fte_version *version;
3336 struct ofputil_flow_mod fm;
3337 char *error;
3338 enum ofputil_protocol usable;
3339
3340 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
3341 if (error) {
3342 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
3343 }
3344 usable_protocols &= usable;
3345
3346 version = xmalloc(sizeof *version);
3347 version->cookie = fm.new_cookie;
3348 version->idle_timeout = fm.idle_timeout;
3349 version->hard_timeout = fm.hard_timeout;
3350 version->importance = fm.importance;
3351 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
3352 | OFPUTIL_FF_EMERG);
3353 version->ofpacts = fm.ofpacts;
3354 version->ofpacts_len = fm.ofpacts_len;
3355 version->table_id = fm.table_id != OFPTT_ALL ? fm.table_id : 0;
3356
3357 fte_queue(state, &fm.match, fm.priority, version, index);
3358 }
3359 ds_destroy(&s);
3360
3361 if (file != stdin) {
3362 fclose(file);
3363 }
3364
3365 return usable_protocols;
3366 }
3367
3368 static bool
3369 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
3370 struct ofpbuf **replyp,
3371 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
3372 {
3373 struct ofpbuf *reply = *replyp;
3374
3375 for (;;) {
3376 int retval;
3377 bool more;
3378
3379 /* Get a flow stats reply message, if we don't already have one. */
3380 if (!reply) {
3381 enum ofptype type;
3382 enum ofperr error;
3383
3384 do {
3385 run(vconn_recv_block(vconn, &reply),
3386 "OpenFlow packet receive failed");
3387 } while (((struct ofp_header *) reply->data)->xid != send_xid);
3388
3389 error = ofptype_decode(&type, reply->data);
3390 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
3391 ovs_fatal(0, "received bad reply: %s",
3392 ofp_to_string(reply->data, reply->size,
3393 verbosity + 1));
3394 }
3395 }
3396
3397 /* Pull an individual flow stats reply out of the message. */
3398 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
3399 switch (retval) {
3400 case 0:
3401 *replyp = reply;
3402 return true;
3403
3404 case EOF:
3405 more = ofpmp_more(reply->header);
3406 ofpbuf_delete(reply);
3407 reply = NULL;
3408 if (!more) {
3409 *replyp = NULL;
3410 return false;
3411 }
3412 break;
3413
3414 default:
3415 ovs_fatal(0, "parse error in reply (%s)",
3416 ofperr_to_string(retval));
3417 }
3418 }
3419 }
3420
3421 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
3422 * format 'protocol', and adds them as flow table entries in 'tables' for the
3423 * version with the specified 'index'. */
3424 static void
3425 read_flows_from_switch(struct vconn *vconn,
3426 enum ofputil_protocol protocol,
3427 struct fte_state *state, int index)
3428 {
3429 struct ofputil_flow_stats_request fsr;
3430 struct ofputil_flow_stats fs;
3431 struct ofpbuf *request;
3432 struct ofpbuf ofpacts;
3433 struct ofpbuf *reply;
3434 ovs_be32 send_xid;
3435
3436 fsr.aggregate = false;
3437 match_init_catchall(&fsr.match);
3438 fsr.out_port = OFPP_ANY;
3439 fsr.out_group = OFPG_ANY;
3440 fsr.table_id = 0xff;
3441 fsr.cookie = fsr.cookie_mask = htonll(0);
3442 request = ofputil_encode_flow_stats_request(&fsr, protocol);
3443 send_xid = ((struct ofp_header *) request->data)->xid;
3444 send_openflow_buffer(vconn, request);
3445
3446 reply = NULL;
3447 ofpbuf_init(&ofpacts, 0);
3448 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
3449 struct fte_version *version;
3450
3451 version = xmalloc(sizeof *version);
3452 version->cookie = fs.cookie;
3453 version->idle_timeout = fs.idle_timeout;
3454 version->hard_timeout = fs.hard_timeout;
3455 version->importance = fs.importance;
3456 version->flags = 0;
3457 version->ofpacts_len = fs.ofpacts_len;
3458 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
3459 version->table_id = fs.table_id;
3460
3461 fte_queue(state, &fs.match, fs.priority, version, index);
3462 }
3463 ofpbuf_uninit(&ofpacts);
3464 }
3465
3466 static void
3467 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
3468 enum ofputil_protocol protocol, struct ovs_list *packets)
3469 {
3470 const struct fte_version *version = fte->versions[index];
3471 struct ofpbuf *ofm;
3472
3473 struct ofputil_flow_mod fm = {
3474 .priority = fte->rule.priority,
3475 .new_cookie = version->cookie,
3476 .modify_cookie = true,
3477 .table_id = version->table_id,
3478 .command = command,
3479 .idle_timeout = version->idle_timeout,
3480 .hard_timeout = version->hard_timeout,
3481 .importance = version->importance,
3482 .buffer_id = UINT32_MAX,
3483 .out_port = OFPP_ANY,
3484 .out_group = OFPG_ANY,
3485 .flags = version->flags,
3486 };
3487 minimatch_expand(&fte->rule.match, &fm.match);
3488 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
3489 command == OFPFC_MODIFY_STRICT) {
3490 fm.ofpacts = version->ofpacts;
3491 fm.ofpacts_len = version->ofpacts_len;
3492 } else {
3493 fm.ofpacts = NULL;
3494 fm.ofpacts_len = 0;
3495 }
3496
3497 ofm = ofputil_encode_flow_mod(&fm, protocol);
3498 ovs_list_push_back(packets, &ofm->list_node);
3499 }
3500
3501 static void
3502 ofctl_replace_flows(struct ovs_cmdl_context *ctx)
3503 {
3504 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
3505 enum ofputil_protocol usable_protocols, protocol;
3506 struct fte_state fte_state;
3507 struct flow_tables tables;
3508 struct classifier *cls;
3509 struct ovs_list requests;
3510 struct vconn *vconn;
3511 struct fte *fte;
3512
3513 fte_state_init(&fte_state);
3514 usable_protocols = read_flows_from_file(ctx->argv[2], &fte_state, FILE_IDX);
3515
3516 protocol = open_vconn(ctx->argv[1], &vconn);
3517 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
3518
3519 read_flows_from_switch(vconn, protocol, &fte_state, SWITCH_IDX);
3520
3521 fte_fill(&fte_state, &tables);
3522
3523 ovs_list_init(&requests);
3524
3525 FOR_EACH_TABLE (cls, &tables) {
3526 /* Delete flows that exist on the switch but not in the file. */
3527 CLS_FOR_EACH (fte, rule, cls) {
3528 struct fte_version *file_ver = fte->versions[FILE_IDX];
3529 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
3530
3531 if (sw_ver && !file_ver) {
3532 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
3533 protocol, &requests);
3534 }
3535 }
3536
3537 /* Add flows that exist in the file but not on the switch.
3538 * Update flows that exist in both places but differ. */
3539 CLS_FOR_EACH (fte, rule, cls) {
3540 struct fte_version *file_ver = fte->versions[FILE_IDX];
3541 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
3542
3543 if (file_ver &&
3544 (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
3545 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol,
3546 &requests);
3547 }
3548 }
3549 }
3550 if (bundle) {
3551 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
3552 } else {
3553 transact_multiple_noreply(vconn, &requests);
3554 }
3555
3556 ofpbuf_list_delete(&requests);
3557 vconn_close(vconn);
3558
3559 fte_free_all(&tables);
3560 fte_state_destroy(&fte_state);
3561 }
3562
3563 static void
3564 read_flows_from_source(const char *source, struct fte_state *state, int index)
3565 {
3566 struct stat s;
3567
3568 if (source[0] == '/' || source[0] == '.'
3569 || (!strchr(source, ':') && !stat(source, &s))) {
3570 read_flows_from_file(source, state, index);
3571 } else {
3572 enum ofputil_protocol protocol;
3573 struct vconn *vconn;
3574
3575 protocol = open_vconn(source, &vconn);
3576 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
3577 read_flows_from_switch(vconn, protocol, state, index);
3578 vconn_close(vconn);
3579 }
3580 }
3581
3582 static void
3583 ofctl_diff_flows(struct ovs_cmdl_context *ctx)
3584 {
3585 bool differences = false;
3586 struct fte_state fte_state;
3587 struct flow_tables tables;
3588 struct classifier *cls;
3589 struct ds a_s, b_s;
3590 struct fte *fte;
3591
3592 fte_state_init(&fte_state);
3593 read_flows_from_source(ctx->argv[1], &fte_state, 0);
3594 read_flows_from_source(ctx->argv[2], &fte_state, 1);
3595 fte_fill(&fte_state, &tables);
3596
3597 ds_init(&a_s);
3598 ds_init(&b_s);
3599
3600 FOR_EACH_TABLE (cls, &tables) {
3601 CLS_FOR_EACH (fte, rule, cls) {
3602 struct fte_version *a = fte->versions[0];
3603 struct fte_version *b = fte->versions[1];
3604
3605 if (!a || !b || !fte_version_equals(a, b)) {
3606 fte_version_format(&fte_state, fte, 0, &a_s);
3607 fte_version_format(&fte_state, fte, 1, &b_s);
3608 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
3609 if (a_s.length) {
3610 printf("-%s", ds_cstr(&a_s));
3611 }
3612 if (b_s.length) {
3613 printf("+%s", ds_cstr(&b_s));
3614 }
3615 differences = true;
3616 }
3617 }
3618 }
3619 }
3620
3621 ds_destroy(&a_s);
3622 ds_destroy(&b_s);
3623
3624 fte_free_all(&tables);
3625 fte_state_destroy(&fte_state);
3626
3627 if (differences) {
3628 exit(2);
3629 }
3630 }
3631
3632 static void
3633 ofctl_meter_mod__(const char *bridge, const char *str, int command)
3634 {
3635 struct ofputil_meter_mod mm;
3636 struct vconn *vconn;
3637 enum ofputil_protocol protocol;
3638 enum ofputil_protocol usable_protocols;
3639 enum ofp_version version;
3640
3641 if (str) {
3642 char *error;
3643 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
3644 if (error) {
3645 ovs_fatal(0, "%s", error);
3646 }
3647 } else {
3648 usable_protocols = OFPUTIL_P_OF13_UP;
3649 mm.command = command;
3650 mm.meter.meter_id = OFPM13_ALL;
3651 }
3652
3653 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3654 version = ofputil_protocol_to_ofp_version(protocol);
3655 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
3656 vconn_close(vconn);
3657 }
3658
3659 static void
3660 ofctl_meter_request__(const char *bridge, const char *str,
3661 enum ofputil_meter_request_type type)
3662 {
3663 struct ofputil_meter_mod mm;
3664 struct vconn *vconn;
3665 enum ofputil_protocol usable_protocols;
3666 enum ofputil_protocol protocol;
3667 enum ofp_version version;
3668
3669 if (str) {
3670 char *error;
3671 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
3672 if (error) {
3673 ovs_fatal(0, "%s", error);
3674 }
3675 } else {
3676 usable_protocols = OFPUTIL_P_OF13_UP;
3677 mm.meter.meter_id = OFPM13_ALL;
3678 }
3679
3680 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
3681 version = ofputil_protocol_to_ofp_version(protocol);
3682 dump_transaction(vconn, ofputil_encode_meter_request(version, type,
3683 mm.meter.meter_id));
3684 vconn_close(vconn);
3685 }
3686
3687
3688 static void
3689 ofctl_add_meter(struct ovs_cmdl_context *ctx)
3690 {
3691 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD);
3692 }
3693
3694 static void
3695 ofctl_mod_meter(struct ovs_cmdl_context *ctx)
3696 {
3697 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY);
3698 }
3699
3700 static void
3701 ofctl_del_meters(struct ovs_cmdl_context *ctx)
3702 {
3703 ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE);
3704 }
3705
3706 static void
3707 ofctl_dump_meters(struct ovs_cmdl_context *ctx)
3708 {
3709 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3710 OFPUTIL_METER_CONFIG);
3711 }
3712
3713 static void
3714 ofctl_meter_stats(struct ovs_cmdl_context *ctx)
3715 {
3716 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3717 OFPUTIL_METER_STATS);
3718 }
3719
3720 static void
3721 ofctl_meter_features(struct ovs_cmdl_context *ctx)
3722 {
3723 ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES);
3724 }
3725
3726 \f
3727 /* Undocumented commands for unit testing. */
3728
3729 static void
3730 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
3731 enum ofputil_protocol usable_protocols)
3732 {
3733 enum ofputil_protocol protocol = 0;
3734 char *usable_s;
3735 size_t i;
3736
3737 usable_s = ofputil_protocols_to_string(usable_protocols);
3738 printf("usable protocols: %s\n", usable_s);
3739 free(usable_s);
3740
3741 if (!(usable_protocols & allowed_protocols)) {
3742 ovs_fatal(0, "no usable protocol");
3743 }
3744 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
3745 protocol = 1 << i;
3746 if (protocol & usable_protocols & allowed_protocols) {
3747 break;
3748 }
3749 }
3750 ovs_assert(is_pow2(protocol));
3751
3752 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
3753
3754 for (i = 0; i < n_fms; i++) {
3755 struct ofputil_flow_mod *fm = &fms[i];
3756 struct ofpbuf *msg;
3757
3758 msg = ofputil_encode_flow_mod(fm, protocol);
3759 ofp_print(stdout, msg->data, msg->size, verbosity);
3760 ofpbuf_delete(msg);
3761
3762 free(CONST_CAST(struct ofpact *, fm->ofpacts));
3763 }
3764 }
3765
3766 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
3767 * it back to stdout. */
3768 static void
3769 ofctl_parse_flow(struct ovs_cmdl_context *ctx)
3770 {
3771 enum ofputil_protocol usable_protocols;
3772 struct ofputil_flow_mod fm;
3773 char *error;
3774
3775 error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols);
3776 if (error) {
3777 ovs_fatal(0, "%s", error);
3778 }
3779 ofctl_parse_flows__(&fm, 1, usable_protocols);
3780 }
3781
3782 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
3783 * add-flows) and prints each of the flows back to stdout. */
3784 static void
3785 ofctl_parse_flows(struct ovs_cmdl_context *ctx)
3786 {
3787 enum ofputil_protocol usable_protocols;
3788 struct ofputil_flow_mod *fms = NULL;
3789 size_t n_fms = 0;
3790 char *error;
3791
3792 error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms,
3793 &usable_protocols);
3794 if (error) {
3795 ovs_fatal(0, "%s", error);
3796 }
3797 ofctl_parse_flows__(fms, n_fms, usable_protocols);
3798 free(fms);
3799 }
3800
3801 static void
3802 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
3803 {
3804 struct ds in;
3805
3806 ds_init(&in);
3807 while (!ds_get_test_line(&in, stdin)) {
3808 struct ofpbuf nx_match;
3809 struct match match;
3810 ovs_be64 cookie, cookie_mask;
3811 enum ofperr error;
3812 int match_len;
3813
3814 /* Convert string to nx_match. */
3815 ofpbuf_init(&nx_match, 0);
3816 if (oxm) {
3817 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
3818 } else {
3819 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
3820 }
3821
3822 /* Convert nx_match to match. */
3823 if (strict) {
3824 if (oxm) {
3825 error = oxm_pull_match(&nx_match, NULL, &match);
3826 } else {
3827 error = nx_pull_match(&nx_match, match_len, &match,
3828 &cookie, &cookie_mask, NULL);
3829 }
3830 } else {
3831 if (oxm) {
3832 error = oxm_pull_match_loose(&nx_match, NULL, &match);
3833 } else {
3834 error = nx_pull_match_loose(&nx_match, match_len, &match,
3835 &cookie, &cookie_mask, NULL);
3836 }
3837 }
3838
3839
3840 if (!error) {
3841 char *out;
3842
3843 /* Convert match back to nx_match. */
3844 ofpbuf_uninit(&nx_match);
3845 ofpbuf_init(&nx_match, 0);
3846 if (oxm) {
3847 match_len = oxm_put_match(&nx_match, &match, version);
3848 out = oxm_match_to_string(&nx_match, match_len);
3849 } else {
3850 match_len = nx_put_match(&nx_match, &match,
3851 cookie, cookie_mask);
3852 out = nx_match_to_string(nx_match.data, match_len);
3853 }
3854
3855 puts(out);
3856 free(out);
3857
3858 if (verbosity > 0) {
3859 ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false);
3860 }
3861 } else {
3862 printf("nx_pull_match() returned error %s\n",
3863 ofperr_get_name(error));
3864 }
3865
3866 ofpbuf_uninit(&nx_match);
3867 }
3868 ds_destroy(&in);
3869 }
3870
3871 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
3872 * stdin, does some internal fussing with them, and then prints them back as
3873 * strings on stdout. */
3874 static void
3875 ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED)
3876 {
3877 ofctl_parse_nxm__(false, 0);
3878 }
3879
3880 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
3881 * strings from stdin, does some internal fussing with them, and then prints
3882 * them back as strings on stdout. VERSION must specify an OpenFlow version,
3883 * e.g. "OpenFlow12". */
3884 static void
3885 ofctl_parse_oxm(struct ovs_cmdl_context *ctx)
3886 {
3887 enum ofp_version version = ofputil_version_from_string(ctx->argv[1]);
3888 if (version < OFP12_VERSION) {
3889 ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]);
3890 }
3891
3892 ofctl_parse_nxm__(true, version);
3893 }
3894
3895 static void
3896 print_differences(const char *prefix,
3897 const void *a_, size_t a_len,
3898 const void *b_, size_t b_len)
3899 {
3900 const uint8_t *a = a_;
3901 const uint8_t *b = b_;
3902 size_t i;
3903
3904 for (i = 0; i < MIN(a_len, b_len); i++) {
3905 if (a[i] != b[i]) {
3906 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
3907 prefix, i, a[i], b[i]);
3908 }
3909 }
3910 for (i = a_len; i < b_len; i++) {
3911 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
3912 }
3913 for (i = b_len; i < a_len; i++) {
3914 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
3915 }
3916 }
3917
3918 static void
3919 ofctl_parse_actions__(const char *version_s, bool instructions)
3920 {
3921 enum ofp_version version;
3922 struct ds in;
3923
3924 version = ofputil_version_from_string(version_s);
3925 if (!version) {
3926 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3927 }
3928
3929 ds_init(&in);
3930 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3931 struct ofpbuf of_out;
3932 struct ofpbuf of_in;
3933 struct ofpbuf ofpacts;
3934 const char *table_id;
3935 char *actions;
3936 enum ofperr error;
3937 size_t size;
3938 struct ds s;
3939
3940 /* Parse table_id separated with the follow-up actions by ",", if
3941 * any. */
3942 actions = ds_cstr(&in);
3943 table_id = NULL;
3944 if (strstr(actions, ",")) {
3945 table_id = strsep(&actions, ",");
3946 }
3947
3948 /* Parse hex bytes. */
3949 ofpbuf_init(&of_in, 0);
3950 if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
3951 ovs_fatal(0, "Trailing garbage in hex data");
3952 }
3953
3954 /* Convert to ofpacts. */
3955 ofpbuf_init(&ofpacts, 0);
3956 size = of_in.size;
3957 error = (instructions
3958 ? ofpacts_pull_openflow_instructions
3959 : ofpacts_pull_openflow_actions)(
3960 &of_in, of_in.size, version, &ofpacts);
3961 if (!error && instructions) {
3962 /* Verify actions, enforce consistency. */
3963 enum ofputil_protocol protocol;
3964 struct flow flow;
3965
3966 memset(&flow, 0, sizeof flow);
3967 protocol = ofputil_protocols_from_ofp_version(version);
3968 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3969 &flow, OFPP_MAX,
3970 table_id ? atoi(table_id) : 0,
3971 OFPTT_MAX + 1, protocol);
3972 }
3973 if (error) {
3974 printf("bad %s %s: %s\n\n",
3975 version_s, instructions ? "instructions" : "actions",
3976 ofperr_get_name(error));
3977 ofpbuf_uninit(&ofpacts);
3978 ofpbuf_uninit(&of_in);
3979 continue;
3980 }
3981 ofpbuf_push_uninit(&of_in, size);
3982
3983 /* Print cls_rule. */
3984 ds_init(&s);
3985 ds_put_cstr(&s, "actions=");
3986 ofpacts_format(ofpacts.data, ofpacts.size, &s);
3987 puts(ds_cstr(&s));
3988 ds_destroy(&s);
3989
3990 /* Convert back to ofp10 actions and print differences from input. */
3991 ofpbuf_init(&of_out, 0);
3992 if (instructions) {
3993 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3994 &of_out, version);
3995 } else {
3996 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size,
3997 &of_out, version);
3998 }
3999
4000 print_differences("", of_in.data, of_in.size,
4001 of_out.data, of_out.size);
4002 putchar('\n');
4003
4004 ofpbuf_uninit(&ofpacts);
4005 ofpbuf_uninit(&of_in);
4006 ofpbuf_uninit(&of_out);
4007 }
4008 ds_destroy(&in);
4009 }
4010
4011 /* "parse-actions VERSION": reads a series of action specifications for the
4012 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
4013 * prints them as strings on stdout, and then converts them back to hex bytes
4014 * and prints any differences from the input. */
4015 static void
4016 ofctl_parse_actions(struct ovs_cmdl_context *ctx)
4017 {
4018 ofctl_parse_actions__(ctx->argv[1], false);
4019 }
4020
4021 /* "parse-actions VERSION": reads a series of instruction specifications for
4022 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
4023 * ofpacts, prints them as strings on stdout, and then converts them back to
4024 * hex bytes and prints any differences from the input. */
4025 static void
4026 ofctl_parse_instructions(struct ovs_cmdl_context *ctx)
4027 {
4028 ofctl_parse_actions__(ctx->argv[1], true);
4029 }
4030
4031 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
4032 * bytes from stdin, converts them to cls_rules, prints them as strings on
4033 * stdout, and then converts them back to hex bytes and prints any differences
4034 * from the input.
4035 *
4036 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
4037 * values are ignored in the input and will be set to zero when OVS converts
4038 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
4039 * it does the conversion to hex, to ensure that in fact they are ignored. */
4040 static void
4041 ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
4042 {
4043 struct ds expout;
4044 struct ds in;
4045
4046 ds_init(&in);
4047 ds_init(&expout);
4048 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
4049 struct ofpbuf match_in, match_expout;
4050 struct ofp10_match match_out;
4051 struct ofp10_match match_normal;
4052 struct match match;
4053 char *p;
4054
4055 /* Parse hex bytes to use for expected output. */
4056 ds_clear(&expout);
4057 ds_put_cstr(&expout, ds_cstr(&in));
4058 for (p = ds_cstr(&expout); *p; p++) {
4059 if (*p == 'x') {
4060 *p = '0';
4061 }
4062 }
4063 ofpbuf_init(&match_expout, 0);
4064 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
4065 ovs_fatal(0, "Trailing garbage in hex data");
4066 }
4067 if (match_expout.size != sizeof(struct ofp10_match)) {
4068 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
4069 match_expout.size, sizeof(struct ofp10_match));
4070 }
4071
4072 /* Parse hex bytes for input. */
4073 for (p = ds_cstr(&in); *p; p++) {
4074 if (*p == 'x') {
4075 *p = "0123456789abcdef"[random_uint32() & 0xf];
4076 }
4077 }
4078 ofpbuf_init(&match_in, 0);
4079 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
4080 ovs_fatal(0, "Trailing garbage in hex data");
4081 }
4082 if (match_in.size != sizeof(struct ofp10_match)) {
4083 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
4084 match_in.size, sizeof(struct ofp10_match));
4085 }
4086
4087 /* Convert to cls_rule and print. */
4088 ofputil_match_from_ofp10_match(match_in.data, &match);
4089 match_print(&match);
4090
4091 /* Convert back to ofp10_match and print differences from input. */
4092 ofputil_match_to_ofp10_match(&match, &match_out);
4093 print_differences("", match_expout.data, match_expout.size,
4094 &match_out, sizeof match_out);
4095
4096 /* Normalize, then convert and compare again. */
4097 ofputil_normalize_match(&match);
4098 ofputil_match_to_ofp10_match(&match, &match_normal);
4099 print_differences("normal: ", &match_out, sizeof match_out,
4100 &match_normal, sizeof match_normal);
4101 putchar('\n');
4102
4103 ofpbuf_uninit(&match_in);
4104 ofpbuf_uninit(&match_expout);
4105 }
4106 ds_destroy(&in);
4107 ds_destroy(&expout);
4108 }
4109
4110 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
4111 * bytes from stdin, converts them to "struct match"es, prints them as strings
4112 * on stdout, and then converts them back to hex bytes and prints any
4113 * differences from the input. */
4114 static void
4115 ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
4116 {
4117 struct ds in;
4118
4119 ds_init(&in);
4120 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
4121 struct ofpbuf match_in;
4122 struct ofp11_match match_out;
4123 struct match match;
4124 enum ofperr error;
4125
4126 /* Parse hex bytes. */
4127 ofpbuf_init(&match_in, 0);
4128 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
4129 ovs_fatal(0, "Trailing garbage in hex data");
4130 }
4131 if (match_in.size != sizeof(struct ofp11_match)) {
4132 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
4133 match_in.size, sizeof(struct ofp11_match));
4134 }
4135
4136 /* Convert to match. */
4137 error = ofputil_match_from_ofp11_match(match_in.data, &match);
4138 if (error) {
4139 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
4140 ofpbuf_uninit(&match_in);
4141 continue;
4142 }
4143
4144 /* Print match. */
4145 match_print(&match);
4146
4147 /* Convert back to ofp11_match and print differences from input. */
4148 ofputil_match_to_ofp11_match(&match, &match_out);
4149
4150 print_differences("", match_in.data, match_in.size,
4151 &match_out, sizeof match_out);
4152 putchar('\n');
4153
4154 ofpbuf_uninit(&match_in);
4155 }
4156 ds_destroy(&in);
4157 }
4158
4159 /* "parse-pcap PCAP...": read packets from each PCAP file and print their
4160 * flows. */
4161 static void
4162 ofctl_parse_pcap(struct ovs_cmdl_context *ctx)
4163 {
4164 int error = 0;
4165 for (int i = 1; i < ctx->argc; i++) {
4166 const char *filename = ctx->argv[i];
4167 FILE *pcap = ovs_pcap_open(filename, "rb");
4168 if (!pcap) {
4169 error = errno;
4170 ovs_error(error, "%s: open failed", filename);
4171 continue;
4172 }
4173
4174 for (;;) {
4175 struct dp_packet *packet;
4176 struct flow flow;
4177 int retval;
4178
4179 retval = ovs_pcap_read(pcap, &packet, NULL);
4180 if (retval == EOF) {
4181 break;
4182 } else if (retval) {
4183 error = retval;
4184 ovs_error(error, "%s: read failed", filename);
4185 }
4186
4187 pkt_metadata_init(&packet->md, u32_to_odp(ofp_to_u16(OFPP_ANY)));
4188 flow_extract(packet, &flow);
4189 flow_print(stdout, &flow);
4190 putchar('\n');
4191 dp_packet_delete(packet);
4192 }
4193 fclose(pcap);
4194 }
4195 exit(error);
4196 }
4197
4198 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
4199 * mask values to and from various formats and prints the results. */
4200 static void
4201 ofctl_check_vlan(struct ovs_cmdl_context *ctx)
4202 {
4203 struct match match;
4204
4205 char *string_s;
4206 struct ofputil_flow_mod fm;
4207
4208 struct ofpbuf nxm;
4209 struct match nxm_match;
4210 int nxm_match_len;
4211 char *nxm_s;
4212
4213 struct ofp10_match of10_raw;
4214 struct match of10_match;
4215
4216 struct ofp11_match of11_raw;
4217 struct match of11_match;
4218
4219 enum ofperr error;
4220 char *error_s;
4221
4222 enum ofputil_protocol usable_protocols; /* Unused for now. */
4223
4224 match_init_catchall(&match);
4225 match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16));
4226 match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16));
4227
4228 /* Convert to and from string. */
4229 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
4230 printf("%s -> ", string_s);
4231 fflush(stdout);
4232 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
4233 if (error_s) {
4234 ovs_fatal(0, "%s", error_s);
4235 }
4236 printf("%04"PRIx16"/%04"PRIx16"\n",
4237 ntohs(fm.match.flow.vlan_tci),
4238 ntohs(fm.match.wc.masks.vlan_tci));
4239 free(string_s);
4240
4241 /* Convert to and from NXM. */
4242 ofpbuf_init(&nxm, 0);
4243 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
4244 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
4245 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL, NULL);
4246 printf("NXM: %s -> ", nxm_s);
4247 if (error) {
4248 printf("%s\n", ofperr_to_string(error));
4249 } else {
4250 printf("%04"PRIx16"/%04"PRIx16"\n",
4251 ntohs(nxm_match.flow.vlan_tci),
4252 ntohs(nxm_match.wc.masks.vlan_tci));
4253 }
4254 free(nxm_s);
4255 ofpbuf_uninit(&nxm);
4256
4257 /* Convert to and from OXM. */
4258 ofpbuf_init(&nxm, 0);
4259 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
4260 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
4261 error = oxm_pull_match(&nxm, NULL, &nxm_match);
4262 printf("OXM: %s -> ", nxm_s);
4263 if (error) {
4264 printf("%s\n", ofperr_to_string(error));
4265 } else {
4266 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
4267 (VLAN_VID_MASK | VLAN_CFI);
4268 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
4269 (VLAN_VID_MASK | VLAN_CFI);
4270
4271 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
4272 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
4273 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
4274 } else {
4275 printf("--\n");
4276 }
4277 }
4278 free(nxm_s);
4279 ofpbuf_uninit(&nxm);
4280
4281 /* Convert to and from OpenFlow 1.0. */
4282 ofputil_match_to_ofp10_match(&match, &of10_raw);
4283 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
4284 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
4285 ntohs(of10_raw.dl_vlan),
4286 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
4287 of10_raw.dl_vlan_pcp,
4288 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
4289 ntohs(of10_match.flow.vlan_tci),
4290 ntohs(of10_match.wc.masks.vlan_tci));
4291
4292 /* Convert to and from OpenFlow 1.1. */
4293 ofputil_match_to_ofp11_match(&match, &of11_raw);
4294 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
4295 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
4296 ntohs(of11_raw.dl_vlan),
4297 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
4298 of11_raw.dl_vlan_pcp,
4299 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
4300 ntohs(of11_match.flow.vlan_tci),
4301 ntohs(of11_match.wc.masks.vlan_tci));
4302 }
4303
4304 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
4305 * version. */
4306 static void
4307 ofctl_print_error(struct ovs_cmdl_context *ctx)
4308 {
4309 enum ofperr error;
4310 int version;
4311
4312 error = ofperr_from_name(ctx->argv[1]);
4313 if (!error) {
4314 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
4315 }
4316
4317 for (version = 0; version <= UINT8_MAX; version++) {
4318 const char *name = ofperr_domain_get_name(version);
4319 if (name) {
4320 int vendor = ofperr_get_vendor(error, version);
4321 int type = ofperr_get_type(error, version);
4322 int code = ofperr_get_code(error, version);
4323
4324 if (vendor != -1 || type != -1 || code != -1) {
4325 printf("%s: vendor %#x, type %d, code %d\n",
4326 name, vendor, type, code);
4327 }
4328 }
4329 }
4330 }
4331
4332 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
4333 * error named ENUM and prints the error reply in hex. */
4334 static void
4335 ofctl_encode_error_reply(struct ovs_cmdl_context *ctx)
4336 {
4337 const struct ofp_header *oh;
4338 struct ofpbuf request, *reply;
4339 enum ofperr error;
4340
4341 error = ofperr_from_name(ctx->argv[1]);
4342 if (!error) {
4343 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
4344 }
4345
4346 ofpbuf_init(&request, 0);
4347 if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') {
4348 ovs_fatal(0, "Trailing garbage in hex data");
4349 }
4350 if (request.size < sizeof(struct ofp_header)) {
4351 ovs_fatal(0, "Request too short");
4352 }
4353
4354 oh = request.data;
4355 if (request.size != ntohs(oh->length)) {
4356 ovs_fatal(0, "Request size inconsistent");
4357 }
4358
4359 reply = ofperr_encode_reply(error, request.data);
4360 ofpbuf_uninit(&request);
4361
4362 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
4363 ofpbuf_delete(reply);
4364 }
4365
4366 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
4367 * binary data, interpreting them as an OpenFlow message, and prints the
4368 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
4369 *
4370 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
4371 * HEXSTRING_FILE contains the HEXSTRING. */
4372 static void
4373 ofctl_ofp_print(struct ovs_cmdl_context *ctx)
4374 {
4375 struct ofpbuf packet;
4376 char *buffer;
4377 int verbosity = 2;
4378 struct ds line;
4379
4380 ds_init(&line);
4381
4382 if (!strcmp(ctx->argv[ctx->argc-1], "-")) {
4383 if (ds_get_line(&line, stdin)) {
4384 VLOG_FATAL("Failed to read stdin");
4385 }
4386
4387 buffer = line.string;
4388 verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity;
4389 } else if (ctx->argc > 2) {
4390 buffer = ctx->argv[1];
4391 verbosity = atoi(ctx->argv[2]);
4392 } else {
4393 buffer = ctx->argv[1];
4394 }
4395
4396 ofpbuf_init(&packet, strlen(buffer) / 2);
4397 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
4398 ovs_fatal(0, "trailing garbage following hex bytes");
4399 }
4400 ofp_print(stdout, packet.data, packet.size, verbosity);
4401 ofpbuf_uninit(&packet);
4402 ds_destroy(&line);
4403 }
4404
4405 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
4406 * and dumps each message in hex. */
4407 static void
4408 ofctl_encode_hello(struct ovs_cmdl_context *ctx)
4409 {
4410 uint32_t bitmap = strtol(ctx->argv[1], NULL, 0);
4411 struct ofpbuf *hello;
4412
4413 hello = ofputil_encode_hello(bitmap);
4414 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
4415 ofp_print(stdout, hello->data, hello->size, verbosity);
4416 ofpbuf_delete(hello);
4417 }
4418
4419 static void
4420 ofctl_parse_key_value(struct ovs_cmdl_context *ctx)
4421 {
4422 for (size_t i = 1; i < ctx->argc; i++) {
4423 char *s = ctx->argv[i];
4424 char *key, *value;
4425 int j = 0;
4426 while (ofputil_parse_key_value(&s, &key, &value)) {
4427 if (j++) {
4428 fputs(", ", stdout);
4429 }
4430 fputs(key, stdout);
4431 if (value[0]) {
4432 printf("=%s", value);
4433 }
4434 }
4435 putchar('\n');
4436 }
4437 }
4438
4439 static const struct ovs_cmdl_command all_commands[] = {
4440 { "show", "switch",
4441 1, 1, ofctl_show, OVS_RO },
4442 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
4443 1, 3, ofctl_monitor, OVS_RO },
4444 { "snoop", "switch",
4445 1, 1, ofctl_snoop, OVS_RO },
4446 { "dump-desc", "switch",
4447 1, 1, ofctl_dump_desc, OVS_RO },
4448 { "dump-tables", "switch",
4449 1, 1, ofctl_dump_tables, OVS_RO },
4450 { "dump-table-features", "switch",
4451 1, 1, ofctl_dump_table_features, OVS_RO },
4452 { "dump-table-desc", "switch",
4453 1, 1, ofctl_dump_table_desc, OVS_RO },
4454 { "dump-flows", "switch",
4455 1, 2, ofctl_dump_flows, OVS_RO },
4456 { "dump-aggregate", "switch",
4457 1, 2, ofctl_dump_aggregate, OVS_RO },
4458 { "queue-stats", "switch [port [queue]]",
4459 1, 3, ofctl_queue_stats, OVS_RO },
4460 { "queue-get-config", "switch [port [queue]]",
4461 1, 3, ofctl_queue_get_config, OVS_RO },
4462 { "add-flow", "switch flow",
4463 2, 2, ofctl_add_flow, OVS_RW },
4464 { "add-flows", "switch file",
4465 2, 2, ofctl_add_flows, OVS_RW },
4466 { "mod-flows", "switch flow",
4467 2, 2, ofctl_mod_flows, OVS_RW },
4468 { "del-flows", "switch [flow]",
4469 1, 2, ofctl_del_flows, OVS_RW },
4470 { "replace-flows", "switch file",
4471 2, 2, ofctl_replace_flows, OVS_RW },
4472 { "diff-flows", "source1 source2",
4473 2, 2, ofctl_diff_flows, OVS_RW },
4474 { "add-meter", "switch meter",
4475 2, 2, ofctl_add_meter, OVS_RW },
4476 { "mod-meter", "switch meter",
4477 2, 2, ofctl_mod_meter, OVS_RW },
4478 { "del-meter", "switch meter",
4479 2, 2, ofctl_del_meters, OVS_RW },
4480 { "del-meters", "switch",
4481 1, 1, ofctl_del_meters, OVS_RW },
4482 { "dump-meter", "switch meter",
4483 2, 2, ofctl_dump_meters, OVS_RO },
4484 { "dump-meters", "switch",
4485 1, 1, ofctl_dump_meters, OVS_RO },
4486 { "meter-stats", "switch [meter]",
4487 1, 2, ofctl_meter_stats, OVS_RO },
4488 { "meter-features", "switch",
4489 1, 1, ofctl_meter_features, OVS_RO },
4490 { "packet-out", "switch \"in_port=<port> packet=<hex data> actions=...\"",
4491 2, INT_MAX, ofctl_packet_out, OVS_RW },
4492 { "dump-ports", "switch [port]",
4493 1, 2, ofctl_dump_ports, OVS_RO },
4494 { "dump-ports-desc", "switch [port]",
4495 1, 2, ofctl_dump_ports_desc, OVS_RO },
4496 { "mod-port", "switch iface act",
4497 3, 3, ofctl_mod_port, OVS_RW },
4498 { "mod-table", "switch mod",
4499 3, 3, ofctl_mod_table, OVS_RW },
4500 { "get-frags", "switch",
4501 1, 1, ofctl_get_frags, OVS_RO },
4502 { "set-frags", "switch frag_mode",
4503 2, 2, ofctl_set_frags, OVS_RW },
4504 { "probe", "target",
4505 1, 1, ofctl_probe, OVS_RO },
4506 { "ping", "target [n]",
4507 1, 2, ofctl_ping, OVS_RO },
4508 { "benchmark", "target n count",
4509 3, 3, ofctl_benchmark, OVS_RO },
4510
4511 { "dump-ipfix-bridge", "switch",
4512 1, 1, ofctl_dump_ipfix_bridge, OVS_RO },
4513 { "dump-ipfix-flow", "switch",
4514 1, 1, ofctl_dump_ipfix_flow, OVS_RO },
4515
4516 { "ct-flush-zone", "switch zone",
4517 2, 2, ofctl_ct_flush_zone, OVS_RO },
4518
4519 { "ofp-parse", "file",
4520 1, 1, ofctl_ofp_parse, OVS_RW },
4521 { "ofp-parse-pcap", "pcap",
4522 1, INT_MAX, ofctl_ofp_parse_pcap, OVS_RW },
4523
4524 { "add-group", "switch group",
4525 1, 2, ofctl_add_group, OVS_RW },
4526 { "add-groups", "switch file",
4527 1, 2, ofctl_add_groups, OVS_RW },
4528 { "mod-group", "switch group",
4529 1, 2, ofctl_mod_group, OVS_RW },
4530 { "del-groups", "switch [group]",
4531 1, 2, ofctl_del_groups, OVS_RW },
4532 { "insert-buckets", "switch [group]",
4533 1, 2, ofctl_insert_bucket, OVS_RW },
4534 { "remove-buckets", "switch [group]",
4535 1, 2, ofctl_remove_bucket, OVS_RW },
4536 { "dump-groups", "switch [group]",
4537 1, 2, ofctl_dump_group_desc, OVS_RO },
4538 { "dump-group-stats", "switch [group]",
4539 1, 2, ofctl_dump_group_stats, OVS_RO },
4540 { "dump-group-features", "switch",
4541 1, 1, ofctl_dump_group_features, OVS_RO },
4542
4543 { "bundle", "switch file",
4544 2, 2, ofctl_bundle, OVS_RW },
4545
4546 { "add-tlv-map", "switch map",
4547 2, 2, ofctl_add_tlv_map, OVS_RO },
4548 { "del-tlv-map", "switch [map]",
4549 1, 2, ofctl_del_tlv_map, OVS_RO },
4550 { "dump-tlv-map", "switch",
4551 1, 1, ofctl_dump_tlv_map, OVS_RO },
4552 { "help", NULL, 0, INT_MAX, ofctl_help, OVS_RO },
4553 { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands, OVS_RO },
4554
4555 /* Undocumented commands for testing. */
4556 { "parse-flow", NULL, 1, 1, ofctl_parse_flow, OVS_RW },
4557 { "parse-flows", NULL, 1, 1, ofctl_parse_flows, OVS_RW },
4558 { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm, OVS_RW },
4559 { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm, OVS_RW },
4560 { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm, OVS_RW },
4561 { "parse-actions", NULL, 1, 1, ofctl_parse_actions, OVS_RW },
4562 { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions, OVS_RW },
4563 { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match, OVS_RW },
4564 { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match, OVS_RW },
4565 { "parse-pcap", NULL, 1, INT_MAX, ofctl_parse_pcap, OVS_RW },
4566 { "check-vlan", NULL, 2, 2, ofctl_check_vlan, OVS_RW },
4567 { "print-error", NULL, 1, 1, ofctl_print_error, OVS_RW },
4568 { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply, OVS_RW },
4569 { "ofp-print", NULL, 1, 2, ofctl_ofp_print, OVS_RW },
4570 { "encode-hello", NULL, 1, 1, ofctl_encode_hello, OVS_RW },
4571 { "parse-key-value", NULL, 1, INT_MAX, ofctl_parse_key_value, OVS_RW },
4572
4573 { NULL, NULL, 0, 0, NULL, OVS_RO },
4574 };
4575
4576 static const struct ovs_cmdl_command *get_all_commands(void)
4577 {
4578 return all_commands;
4579 }