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