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