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