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