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