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