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