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