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