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