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