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