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