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