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