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