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