]> git.proxmox.com Git - ovs.git/blame - utilities/ovs-ofctl.c
openflow: Split OFPAT_* into OFPAT10_* and OFPAT11_*.
[ovs.git] / utilities / ovs-ofctl.c
CommitLineData
064af421 1/*
ca8526e0 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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>
064af421
BP
18#include <errno.h>
19#include <getopt.h>
20#include <inttypes.h>
bd6b7545 21#include <sys/socket.h>
064af421 22#include <net/if.h>
064af421 23#include <signal.h>
064af421
BP
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
1e1d00a5 27#include <sys/fcntl.h>
064af421
BP
28#include <sys/stat.h>
29#include <sys/time.h>
30
10a24935 31#include "byte-order.h"
09246b99 32#include "classifier.h"
064af421 33#include "command-line.h"
1eb85ef5 34#include "daemon.h"
064af421
BP
35#include "compiler.h"
36#include "dirs.h"
09246b99 37#include "dynamic-string.h"
064af421 38#include "netlink.h"
09246b99 39#include "nx-match.h"
064af421 40#include "odp-util.h"
90bf1e07 41#include "ofp-errors.h"
f22716dc 42#include "ofp-parse.h"
064af421 43#include "ofp-print.h"
fa37b408 44#include "ofp-util.h"
064af421 45#include "ofpbuf.h"
63d347ce 46#include "ofproto/ofproto.h"
064af421
BP
47#include "openflow/nicira-ext.h"
48#include "openflow/openflow.h"
0c3d5fc8 49#include "packets.h"
1eb85ef5 50#include "poll-loop.h"
064af421 51#include "random.h"
fe55ad15 52#include "stream-ssl.h"
064af421 53#include "timeval.h"
1eb85ef5 54#include "unixctl.h"
064af421 55#include "util.h"
064af421 56#include "vconn.h"
5136ce49 57#include "vlog.h"
064af421 58
d98e6007 59VLOG_DEFINE_THIS_MODULE(ofctl);
064af421 60
102ce766
EJ
61/* --strict: Use strict matching for flow mod commands? Additionally governs
62 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
63 */
675febfa 64static bool strict;
064af421 65
96989efc 66/* --readd: If true, on replace-flows, re-add even flows that have not changed
c4ea79bf
BP
67 * (to reset flow counters). */
68static bool readd;
69
27527aa0
BP
70/* -F, --flow-format: Allowed protocols. By default, any protocol is
71 * allowed. */
72static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
88ca35ee 73
54834960
EJ
74/* -P, --packet-in-format: Packet IN format to use in monitor and snoop
75 * commands. Either one of NXPIF_* to force a particular packet_in format, or
76 * -1 to let ovs-ofctl choose the default. */
77static int preferred_packet_in_format = -1;
78
4f564f8d
BP
79/* -m, --more: Additional verbosity for ofp-print functions. */
80static int verbosity;
81
0c9560b7
BP
82/* --timestamp: Print a timestamp before each received packet on "monitor" and
83 * "snoop" command? */
84static bool timestamp;
85
675febfa 86static const struct command all_commands[];
064af421
BP
87
88static void usage(void) NO_RETURN;
675febfa 89static void parse_options(int argc, char *argv[]);
064af421 90
675febfa
BP
91int
92main(int argc, char *argv[])
064af421 93{
064af421 94 set_program_name(argv[0]);
675febfa 95 parse_options(argc, argv);
064af421 96 signal(SIGPIPE, SIG_IGN);
675febfa 97 run_command(argc - optind, argv + optind, all_commands);
064af421
BP
98 return 0;
99}
100
101static void
675febfa 102parse_options(int argc, char *argv[])
064af421
BP
103{
104 enum {
87c84891 105 OPT_STRICT = UCHAR_MAX + 1,
c4ea79bf 106 OPT_READD,
0c9560b7 107 OPT_TIMESTAMP,
1eb85ef5 108 DAEMON_OPTION_ENUMS,
87c84891 109 VLOG_OPTION_ENUMS
064af421
BP
110 };
111 static struct option long_options[] = {
e3c17733
BP
112 {"timeout", required_argument, NULL, 't'},
113 {"strict", no_argument, NULL, OPT_STRICT},
c4ea79bf 114 {"readd", no_argument, NULL, OPT_READD},
e3c17733 115 {"flow-format", required_argument, NULL, 'F'},
54834960 116 {"packet-in-format", required_argument, NULL, 'P'},
e3c17733 117 {"more", no_argument, NULL, 'm'},
0c9560b7 118 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
e3c17733
BP
119 {"help", no_argument, NULL, 'h'},
120 {"version", no_argument, NULL, 'V'},
1eb85ef5 121 DAEMON_LONG_OPTIONS,
87c84891 122 VLOG_LONG_OPTIONS,
bf8f2167 123 STREAM_SSL_LONG_OPTIONS,
e3c17733 124 {NULL, 0, NULL, 0},
064af421
BP
125 };
126 char *short_options = long_options_to_short_options(long_options);
127
064af421
BP
128 for (;;) {
129 unsigned long int timeout;
130 int c;
131
132 c = getopt_long(argc, argv, short_options, long_options, NULL);
133 if (c == -1) {
134 break;
135 }
136
137 switch (c) {
138 case 't':
139 timeout = strtoul(optarg, NULL, 10);
140 if (timeout <= 0) {
141 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
142 optarg);
143 } else {
144 time_alarm(timeout);
145 }
146 break;
147
88ca35ee 148 case 'F':
27527aa0
BP
149 allowed_protocols = ofputil_protocols_from_string(optarg);
150 if (!allowed_protocols) {
151 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
88ca35ee
BP
152 }
153 break;
154
54834960
EJ
155 case 'P':
156 preferred_packet_in_format =
157 ofputil_packet_in_format_from_string(optarg);
158 if (preferred_packet_in_format < 0) {
159 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
160 }
161 break;
162
4f564f8d
BP
163 case 'm':
164 verbosity++;
165 break;
166
064af421
BP
167 case 'h':
168 usage();
169
170 case 'V':
87ea5e5e 171 ovs_print_version(OFP10_VERSION, OFP10_VERSION);
064af421
BP
172 exit(EXIT_SUCCESS);
173
064af421 174 case OPT_STRICT:
675febfa 175 strict = true;
064af421
BP
176 break;
177
c4ea79bf
BP
178 case OPT_READD:
179 readd = true;
180 break;
181
0c9560b7
BP
182 case OPT_TIMESTAMP:
183 timestamp = true;
184 break;
185
1eb85ef5 186 DAEMON_OPTION_HANDLERS
87c84891 187 VLOG_OPTION_HANDLERS
fe55ad15 188 STREAM_SSL_OPTION_HANDLERS
064af421
BP
189
190 case '?':
191 exit(EXIT_FAILURE);
192
193 default:
194 abort();
195 }
196 }
197 free(short_options);
198}
199
200static void
201usage(void)
202{
203 printf("%s: OpenFlow switch management utility\n"
204 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
205 "\nFor OpenFlow switches:\n"
206 " show SWITCH show OpenFlow information\n"
064af421
BP
207 " dump-desc SWITCH print switch description\n"
208 " dump-tables SWITCH print table stats\n"
209 " mod-port SWITCH IFACE ACT modify port behavior\n"
7257b535
BP
210 " get-frags SWITCH print fragment handling behavior\n"
211 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
abaad8cf 212 " dump-ports SWITCH [PORT] print port statistics\n"
064af421
BP
213 " dump-flows SWITCH print all flow entries\n"
214 " dump-flows SWITCH FLOW print matching FLOWs\n"
215 " dump-aggregate SWITCH print aggregate flow statistics\n"
216 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
d2805da2 217 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
064af421
BP
218 " add-flow SWITCH FLOW add flow described by FLOW\n"
219 " add-flows SWITCH FILE add flows from FILE\n"
220 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
221 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
5ff660c6 222 " replace-flows SWITCH FILE replace flows with those in FILE\n"
1dac118c 223 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
0c3d5fc8
BP
224 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
225 " execute ACTIONS on PACKET\n"
1dac118c
BP
226 " monitor SWITCH [MISSLEN] [invalid_ttl]\n"
227 " print packets received from SWITCH\n"
228 " snoop SWITCH snoop on SWITCH and its controller\n"
064af421 229 "\nFor OpenFlow switches and controllers:\n"
2daadadd
BP
230 " probe TARGET probe whether TARGET is up\n"
231 " ping TARGET [N] latency of N-byte echos\n"
232 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
233 "where SWITCH or TARGET is an active OpenFlow connection method.\n",
064af421
BP
234 program_name, program_name);
235 vconn_usage(true, false, false);
1eb85ef5 236 daemon_usage();
064af421
BP
237 vlog_usage();
238 printf("\nOther options:\n"
239 " --strict use strict match for flow commands\n"
c4ea79bf 240 " --readd replace flows that haven't changed\n"
88ca35ee 241 " -F, --flow-format=FORMAT force particular flow format\n"
54834960 242 " -P, --packet-in-format=FRMT force particular packet in format\n"
4f564f8d 243 " -m, --more be more verbose printing OpenFlow\n"
0c9560b7 244 " --timestamp (monitor, snoop) print timestamps\n"
064af421
BP
245 " -t, --timeout=SECS give up after SECS seconds\n"
246 " -h, --help display this help message\n"
247 " -V, --version display version information\n");
248 exit(EXIT_SUCCESS);
249}
250
1eb85ef5
EJ
251static void
252ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
253 const char *argv[] OVS_UNUSED, void *exiting_)
254{
255 bool *exiting = exiting_;
256 *exiting = true;
bde9f75d 257 unixctl_command_reply(conn, NULL);
1eb85ef5
EJ
258}
259
064af421
BP
260static void run(int retval, const char *message, ...)
261 PRINTF_FORMAT(2, 3);
262
263static void run(int retval, const char *message, ...)
264{
265 if (retval) {
266 va_list args;
267
064af421 268 va_start(args, message);
fcaddd4d 269 ovs_fatal_valist(retval, message, args);
064af421
BP
270 }
271}
272\f
273/* Generic commands. */
274
1a6f1e2a
JG
275static void
276open_vconn_socket(const char *name, struct vconn **vconnp)
277{
278 char *vconn_name = xasprintf("unix:%s", name);
24cd0dee 279 VLOG_DBG("connecting to %s", vconn_name);
87ea5e5e 280 run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp),
1a6f1e2a
JG
281 "connecting to %s", vconn_name);
282 free(vconn_name);
283}
284
27527aa0 285static enum ofputil_protocol
0caf6bde
BP
286open_vconn__(const char *name, const char *default_suffix,
287 struct vconn **vconnp)
064af421 288{
63d347ce 289 char *datapath_name, *datapath_type, *socket_name;
27527aa0 290 enum ofputil_protocol protocol;
63d347ce 291 char *bridge_path;
27527aa0 292 int ofp_version;
064af421 293 struct stat s;
1a6f1e2a 294
b43c6fe2 295 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
63d347ce
BP
296
297 ofproto_parse_name(name, &datapath_name, &datapath_type);
298 socket_name = xasprintf("%s/%s.%s",
299 ovs_rundir(), datapath_name, default_suffix);
300 free(datapath_name);
301 free(datapath_type);
064af421 302
3a27375e 303 if (strchr(name, ':')) {
87ea5e5e 304 run(vconn_open_block(name, OFP10_VERSION, vconnp),
064af421
BP
305 "connecting to %s", name);
306 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
1a6f1e2a
JG
307 open_vconn_socket(name, vconnp);
308 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
309 open_vconn_socket(bridge_path, vconnp);
63d347ce
BP
310 } else if (!stat(socket_name, &s)) {
311 if (!S_ISSOCK(s.st_mode)) {
064af421
BP
312 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
313 name, socket_name);
314 }
1a6f1e2a 315 open_vconn_socket(socket_name, vconnp);
064af421 316 } else {
2c0e6eb4 317 ovs_fatal(0, "%s is not a bridge or a socket", name);
064af421 318 }
1a6f1e2a 319
1a6f1e2a 320 free(bridge_path);
63d347ce 321 free(socket_name);
27527aa0
BP
322
323 ofp_version = vconn_get_version(*vconnp);
324 protocol = ofputil_protocol_from_ofp_version(ofp_version);
325 if (!protocol) {
326 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
327 name, ofp_version);
328 }
329 return protocol;
064af421
BP
330}
331
27527aa0 332static enum ofputil_protocol
0caf6bde
BP
333open_vconn(const char *name, struct vconn **vconnp)
334{
335 return open_vconn__(name, "mgmt", vconnp);
336}
337
064af421 338static void *
eaa6eb2a 339alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp)
064af421 340{
28c8bad1 341 struct ofp_stats_msg *rq;
eaa6eb2a 342
5293a2e1 343 rq = make_openflow(rq_len, OFPT10_STATS_REQUEST, bufferp);
064af421
BP
344 rq->type = htons(type);
345 rq->flags = htons(0);
eaa6eb2a 346 return rq;
064af421
BP
347}
348
349static void
350send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
351{
352 update_openflow_length(buffer);
353 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
354}
355
356static void
357dump_transaction(const char *vconn_name, struct ofpbuf *request)
358{
359 struct vconn *vconn;
360 struct ofpbuf *reply;
361
362 update_openflow_length(request);
363 open_vconn(vconn_name, &vconn);
364 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
4f564f8d 365 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421
BP
366 vconn_close(vconn);
367}
368
369static void
370dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
371{
372 struct ofpbuf *request;
373 make_openflow(sizeof(struct ofp_header), request_type, &request);
374 dump_transaction(vconn_name, request);
375}
376
377static void
378dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
379{
44381c1b 380 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
064af421
BP
381 struct vconn *vconn;
382 bool done = false;
383
384 open_vconn(vconn_name, &vconn);
385 send_openflow_buffer(vconn, request);
386 while (!done) {
02833365 387 ovs_be32 recv_xid;
064af421
BP
388 struct ofpbuf *reply;
389
390 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
391 recv_xid = ((struct ofp_header *) reply->data)->xid;
392 if (send_xid == recv_xid) {
28c8bad1 393 struct ofp_stats_msg *osm;
064af421 394
4f564f8d 395 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421 396
28c8bad1
BP
397 osm = ofpbuf_at(reply, 0, sizeof *osm);
398 done = !osm || !(ntohs(osm->flags) & OFPSF_REPLY_MORE);
064af421
BP
399 } else {
400 VLOG_DBG("received reply with xid %08"PRIx32" "
401 "!= expected %08"PRIx32, recv_xid, send_xid);
402 }
403 ofpbuf_delete(reply);
404 }
405 vconn_close(vconn);
406}
407
408static void
409dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
410{
411 struct ofpbuf *request;
eaa6eb2a 412 alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request);
064af421
BP
413 dump_stats_transaction(vconn_name, request);
414}
415
d12513f7
BP
416/* Sends 'request', which should be a request that only has a reply if an error
417 * occurs, and waits for it to succeed or fail. If an error does occur, prints
7257b535
BP
418 * it and exits with an error.
419 *
420 * Destroys all of the 'requests'. */
d12513f7 421static void
88ca35ee 422transact_multiple_noreply(struct vconn *vconn, struct list *requests)
d12513f7 423{
88ca35ee 424 struct ofpbuf *request, *reply;
d12513f7 425
88ca35ee
BP
426 LIST_FOR_EACH (request, list_node, requests) {
427 update_openflow_length(request);
428 }
429
430 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
d12513f7
BP
431 "talking to %s", vconn_get_name(vconn));
432 if (reply) {
4f564f8d 433 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
d12513f7
BP
434 exit(1);
435 }
436 ofpbuf_delete(reply);
437}
438
88ca35ee
BP
439/* Sends 'request', which should be a request that only has a reply if an error
440 * occurs, and waits for it to succeed or fail. If an error does occur, prints
7257b535
BP
441 * it and exits with an error.
442 *
443 * Destroys 'request'. */
88ca35ee
BP
444static void
445transact_noreply(struct vconn *vconn, struct ofpbuf *request)
446{
447 struct list requests;
448
449 list_init(&requests);
450 list_push_back(&requests, &request->list_node);
451 transact_multiple_noreply(vconn, &requests);
452}
453
7257b535
BP
454static void
455fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
456{
457 struct ofp_switch_config *config;
458 struct ofp_header *header;
459 struct ofpbuf *request;
460 struct ofpbuf *reply;
461
462 make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST,
463 &request);
464 run(vconn_transact(vconn, request, &reply),
465 "talking to %s", vconn_get_name(vconn));
466
467 header = reply->data;
468 if (header->type != OFPT_GET_CONFIG_REPLY ||
469 header->length != htons(sizeof *config)) {
470 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
471 }
472
473 config = reply->data;
474 *config_ = *config;
828c72d0
BP
475
476 ofpbuf_delete(reply);
7257b535
BP
477}
478
479static void
480set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
481{
482 struct ofp_switch_config *config;
483 struct ofp_header save_header;
484 struct ofpbuf *request;
485
486 config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request);
487 save_header = config->header;
488 *config = *config_;
489 config->header = save_header;
490
491 transact_noreply(vconn, request);
492}
493
064af421 494static void
c69ee87c 495do_show(int argc OVS_UNUSED, char *argv[])
064af421
BP
496{
497 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
498 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
499}
500
064af421 501static void
c69ee87c 502do_dump_desc(int argc OVS_UNUSED, char *argv[])
064af421
BP
503{
504 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
505}
506
507static void
c69ee87c 508do_dump_tables(int argc OVS_UNUSED, char *argv[])
064af421
BP
509{
510 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
511}
512
0df0e81d
BP
513/* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
514 * 'port_name' (which may be a port name or number), and copies it into
515 * '*oppp'. */
516static void
517fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
518 struct ofp_phy_port *oppp)
abaad8cf
JP
519{
520 struct ofpbuf *request, *reply;
521 struct ofp_switch_features *osf;
0df0e81d 522 unsigned int port_no;
abaad8cf
JP
523 struct vconn *vconn;
524 int n_ports;
525 int port_idx;
abaad8cf 526
0df0e81d
BP
527 /* Try to interpret the argument as a port number. */
528 if (!str_to_uint(port_name, 10, &port_no)) {
529 port_no = UINT_MAX;
abaad8cf
JP
530 }
531
0df0e81d 532 /* Fetch the switch's ofp_switch_features. */
abaad8cf
JP
533 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
534 open_vconn(vconn_name, &vconn);
535 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
536
537 osf = reply->data;
0df0e81d
BP
538 if (reply->size < sizeof *osf) {
539 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
540 vconn_name, reply->size);
541 }
abaad8cf
JP
542 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
543
544 for (port_idx = 0; port_idx < n_ports; port_idx++) {
0df0e81d
BP
545 const struct ofp_phy_port *opp = &osf->ports[port_idx];
546
547 if (port_no != UINT_MAX
548 ? htons(port_no) == opp->port_no
0b61210e 549 : !strncmp(opp->name, port_name, sizeof opp->name)) {
0df0e81d
BP
550 *oppp = *opp;
551 ofpbuf_delete(reply);
552 vconn_close(vconn);
553 return;
abaad8cf
JP
554 }
555 }
0df0e81d
BP
556 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
557}
abaad8cf 558
0df0e81d
BP
559/* Returns the port number corresponding to 'port_name' (which may be a port
560 * name or number) within the switch 'vconn_name'. */
561static uint16_t
562str_to_port_no(const char *vconn_name, const char *port_name)
563{
564 unsigned int port_no;
565
566 if (str_to_uint(port_name, 10, &port_no)) {
567 return port_no;
568 } else {
569 struct ofp_phy_port opp;
abaad8cf 570
0df0e81d
BP
571 fetch_ofp_phy_port(vconn_name, port_name, &opp);
572 return ntohs(opp.port_no);
573 }
abaad8cf
JP
574}
575
88ca35ee 576static bool
27527aa0
BP
577try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
578 enum ofputil_protocol *cur)
88ca35ee 579{
27527aa0
BP
580 for (;;) {
581 struct ofpbuf *request, *reply;
582 enum ofputil_protocol next;
88ca35ee 583
27527aa0
BP
584 request = ofputil_encode_set_protocol(*cur, want, &next);
585 if (!request) {
586 return true;
587 }
588
589 run(vconn_transact_noreply(vconn, request, &reply),
590 "talking to %s", vconn_get_name(vconn));
591 if (reply) {
592 char *s = ofp_to_string(reply->data, reply->size, 2);
593 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
594 vconn_get_name(vconn), s);
595 free(s);
596 ofpbuf_delete(reply);
597 return false;
598 }
599
600 *cur = next;
88ca35ee 601 }
88ca35ee
BP
602}
603
27527aa0
BP
604static enum ofputil_protocol
605set_protocol_for_flow_dump(struct vconn *vconn,
606 enum ofputil_protocol cur_protocol,
607 enum ofputil_protocol usable_protocols)
064af421 608{
27527aa0
BP
609 char *usable_s;
610 int i;
064af421 611
27527aa0
BP
612 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
613 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
614 if (f & usable_protocols & allowed_protocols
615 && try_set_protocol(vconn, f, &cur_protocol)) {
616 return f;
f9cbfbe4 617 }
27527aa0 618 }
f9cbfbe4 619
27527aa0
BP
620 usable_s = ofputil_protocols_to_string(usable_protocols);
621 if (usable_protocols & allowed_protocols) {
622 ovs_fatal(0, "switch does not support any of the usable flow "
623 "formats (%s)", usable_s);
f9cbfbe4 624 } else {
27527aa0
BP
625 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
626 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
627 "allowed flow formats (%s)", usable_s, allowed_s);
88ca35ee 628 }
064af421
BP
629}
630
631static void
88ca35ee 632do_dump_flows__(int argc, char *argv[], bool aggregate)
064af421 633{
27527aa0 634 enum ofputil_protocol usable_protocols, protocol;
81d1ea94 635 struct ofputil_flow_stats_request fsr;
064af421 636 struct ofpbuf *request;
88ca35ee 637 struct vconn *vconn;
064af421 638
88ca35ee 639 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
27527aa0 640 usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr);
064af421 641
27527aa0
BP
642 protocol = open_vconn(argv[1], &vconn);
643 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
644 request = ofputil_encode_flow_stats_request(&fsr, protocol);
064af421 645 dump_stats_transaction(argv[1], request);
88ca35ee
BP
646 vconn_close(vconn);
647}
648
649static void
650do_dump_flows(int argc, char *argv[])
651{
652 return do_dump_flows__(argc, argv, false);
653}
654
655static void
656do_dump_aggregate(int argc, char *argv[])
657{
658 return do_dump_flows__(argc, argv, true);
064af421
BP
659}
660
d2805da2
BP
661static void
662do_queue_stats(int argc, char *argv[])
663{
664 struct ofp_queue_stats_request *req;
665 struct ofpbuf *request;
666
667 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
668
669 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
670 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
671 } else {
672 req->port_no = htons(OFPP_ALL);
673 }
674 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
675 req->queue_id = htonl(atoi(argv[3]));
676 } else {
677 req->queue_id = htonl(OFPQ_ALL);
678 }
679
680 memset(req->pad, 0, sizeof req->pad);
681
682 dump_stats_transaction(argv[1], request);
683}
684
27527aa0
BP
685static enum ofputil_protocol
686open_vconn_for_flow_mod(const char *remote,
687 const struct ofputil_flow_mod *fms, size_t n_fms,
688 struct vconn **vconnp)
0fbc9f11 689{
27527aa0
BP
690 enum ofputil_protocol usable_protocols;
691 enum ofputil_protocol cur_protocol;
692 char *usable_s;
693 int i;
0fbc9f11 694
27527aa0
BP
695 /* Figure out what flow formats will work. */
696 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
697 if (!(usable_protocols & allowed_protocols)) {
698 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
699 usable_s = ofputil_protocols_to_string(usable_protocols);
700 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
701 "allowed flow formats (%s)", usable_s, allowed_s);
0fbc9f11 702 }
0fbc9f11 703
27527aa0
BP
704 /* If the initial flow format is allowed and usable, keep it. */
705 cur_protocol = open_vconn(remote, vconnp);
706 if (usable_protocols & allowed_protocols & cur_protocol) {
707 return cur_protocol;
708 }
709
710 /* Otherwise try each flow format in turn. */
711 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
712 enum ofputil_protocol f = 1 << i;
713
714 if (f != cur_protocol
715 && f & usable_protocols & allowed_protocols
716 && try_set_protocol(*vconnp, f, &cur_protocol)) {
717 return f;
718 }
0fbc9f11 719 }
27527aa0
BP
720
721 usable_s = ofputil_protocols_to_string(usable_protocols);
722 ovs_fatal(0, "switch does not support any of the usable flow "
723 "formats (%s)", usable_s);
0fbc9f11
BP
724}
725
064af421 726static void
27527aa0 727do_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, size_t n_fms)
064af421 728{
27527aa0 729 enum ofputil_protocol protocol;
064af421 730 struct vconn *vconn;
27527aa0 731 size_t i;
4989c59f 732
27527aa0 733 protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn);
049c8dc2 734
27527aa0
BP
735 for (i = 0; i < n_fms; i++) {
736 struct ofputil_flow_mod *fm = &fms[i];
0fbc9f11 737
27527aa0
BP
738 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
739 free(fm->actions);
4989c59f 740 }
064af421 741 vconn_close(vconn);
88ca35ee
BP
742}
743
064af421 744static void
27527aa0 745do_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
064af421 746{
27527aa0
BP
747 struct ofputil_flow_mod *fms = NULL;
748 size_t n_fms = 0;
064af421 749
27527aa0
BP
750 parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms);
751 do_flow_mod__(argv[1], fms, n_fms);
752 free(fms);
753}
754
755static void
756do_flow_mod(int argc, char *argv[], uint16_t command)
757{
4989c59f 758 if (argc > 2 && !strcmp(argv[2], "-")) {
27527aa0
BP
759 do_flow_mod_file(argc, argv, command);
760 } else {
761 struct ofputil_flow_mod fm;
762 parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false);
763 do_flow_mod__(argv[1], &fm, 1);
064af421 764 }
4989c59f 765}
88ca35ee 766
4989c59f
BP
767static void
768do_add_flow(int argc, char *argv[])
769{
27527aa0 770 do_flow_mod(argc, argv, OFPFC_ADD);
4989c59f
BP
771}
772
773static void
774do_add_flows(int argc, char *argv[])
775{
27527aa0 776 do_flow_mod_file(argc, argv, OFPFC_ADD);
064af421
BP
777}
778
779static void
88ca35ee 780do_mod_flows(int argc, char *argv[])
064af421 781{
27527aa0 782 do_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
064af421
BP
783}
784
88ca35ee
BP
785static void
786do_del_flows(int argc, char *argv[])
064af421 787{
27527aa0 788 do_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
064af421
BP
789}
790
54834960
EJ
791static void
792set_packet_in_format(struct vconn *vconn,
793 enum nx_packet_in_format packet_in_format)
794{
795 struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format);
796 transact_noreply(vconn, spif);
797 VLOG_DBG("%s: using user-specified packet in format %s",
798 vconn_get_name(vconn),
799 ofputil_packet_in_format_to_string(packet_in_format));
800}
801
f0fd1a17
PS
802static int
803monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
804{
805 struct ofp_switch_config config;
806 enum ofp_config_flags flags;
807
808 fetch_switch_config(vconn, &config);
809 flags = ntohs(config.flags);
810 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
811 /* Set the invalid ttl config. */
812 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
813
814 config.flags = htons(flags);
815 set_switch_config(vconn, &config);
816
817 /* Then retrieve the configuration to see if it really took. OpenFlow
818 * doesn't define error reporting for bad modes, so this is all we can
819 * do. */
820 fetch_switch_config(vconn, &config);
821 flags = ntohs(config.flags);
822 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
823 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
824 "switch probably doesn't support mode)");
825 return -EOPNOTSUPP;
826 }
827 }
828 return 0;
829}
830
96761f58
BP
831/* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
832 * caller must free '*msgp'. On success, returns NULL. On failure, returns
833 * an error message and stores NULL in '*msgp'. */
834static const char *
835openflow_from_hex(const char *hex, struct ofpbuf **msgp)
836{
837 struct ofp_header *oh;
838 struct ofpbuf *msg;
839
840 msg = ofpbuf_new(strlen(hex) / 2);
841 *msgp = NULL;
842
843 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
844 ofpbuf_delete(msg);
845 return "Trailing garbage in hex data";
846 }
847
848 if (msg->size < sizeof(struct ofp_header)) {
849 ofpbuf_delete(msg);
850 return "Message too short for OpenFlow";
851 }
852
853 oh = msg->data;
854 if (msg->size != ntohs(oh->length)) {
855 ofpbuf_delete(msg);
856 return "Message size does not match length in OpenFlow header";
857 }
858
859 *msgp = msg;
860 return NULL;
861}
862
863static void
864ofctl_send(struct unixctl_conn *conn, int argc,
865 const char *argv[], void *vconn_)
866{
867 struct vconn *vconn = vconn_;
868 struct ds reply;
869 bool ok;
870 int i;
871
872 ok = true;
873 ds_init(&reply);
874 for (i = 1; i < argc; i++) {
875 const char *error_msg;
876 struct ofpbuf *msg;
877 int error;
878
879 error_msg = openflow_from_hex(argv[i], &msg);
880 if (error_msg) {
881 ds_put_format(&reply, "%s\n", error_msg);
882 ok = false;
883 continue;
884 }
885
886 fprintf(stderr, "send: ");
887 ofp_print(stderr, msg->data, msg->size, verbosity);
888
889 error = vconn_send_block(vconn, msg);
890 if (error) {
891 ofpbuf_delete(msg);
892 ds_put_format(&reply, "%s\n", strerror(error));
893 ok = false;
894 } else {
895 ds_put_cstr(&reply, "sent\n");
896 }
897 }
bde9f75d
EJ
898
899 if (ok) {
900 unixctl_command_reply(conn, ds_cstr(&reply));
901 } else {
902 unixctl_command_reply_error(conn, ds_cstr(&reply));
903 }
96761f58
BP
904 ds_destroy(&reply);
905}
906
bb638b9a
BP
907struct barrier_aux {
908 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
909 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
910};
911
912static void
913ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
914 const char *argv[] OVS_UNUSED, void *aux_)
915{
916 struct barrier_aux *aux = aux_;
917 struct ofpbuf *msg;
918 int error;
919
920 if (aux->conn) {
bde9f75d 921 unixctl_command_reply_error(conn, "already waiting for barrier reply");
bb638b9a
BP
922 return;
923 }
924
925 msg = ofputil_encode_barrier_request();
926 fprintf(stderr, "send: ");
927 ofp_print(stderr, msg->data, msg->size, verbosity);
928
929 error = vconn_send_block(aux->vconn, msg);
930 if (error) {
931 ofpbuf_delete(msg);
bde9f75d 932 unixctl_command_reply_error(conn, strerror(error));
bb638b9a
BP
933 } else {
934 aux->conn = conn;
935 }
936}
937
1e1d00a5
BP
938static void
939ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
940 const char *argv[], void *aux OVS_UNUSED)
941{
942 int fd;
943
944 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
945 if (fd < 0) {
bde9f75d 946 unixctl_command_reply_error(conn, strerror(errno));
1e1d00a5
BP
947 return;
948 }
949
950 fflush(stderr);
951 dup2(fd, STDERR_FILENO);
952 close(fd);
bde9f75d 953 unixctl_command_reply(conn, NULL);
1e1d00a5
BP
954}
955
064af421 956static void
0caf6bde
BP
957monitor_vconn(struct vconn *vconn)
958{
bb638b9a 959 struct barrier_aux barrier_aux = { vconn, NULL };
1eb85ef5
EJ
960 struct unixctl_server *server;
961 bool exiting = false;
7d0c5973 962 int error;
1eb85ef5 963
7d0c5973 964 daemon_save_fd(STDERR_FILENO);
1eb85ef5
EJ
965 daemonize_start();
966 error = unixctl_server_create(NULL, &server);
967 if (error) {
968 ovs_fatal(error, "failed to create unixctl server");
969 }
970 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
96761f58
BP
971 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
972 ofctl_send, vconn);
bb638b9a
BP
973 unixctl_command_register("ofctl/barrier", "", 0, 0,
974 ofctl_barrier, &barrier_aux);
1e1d00a5
BP
975 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
976 ofctl_set_output_file, NULL);
1eb85ef5
EJ
977 daemonize_complete();
978
0caf6bde
BP
979 for (;;) {
980 struct ofpbuf *b;
1eb85ef5
EJ
981 int retval;
982
983 unixctl_server_run(server);
984
985 for (;;) {
bb638b9a
BP
986 uint8_t msg_type;
987
1eb85ef5
EJ
988 retval = vconn_recv(vconn, &b);
989 if (retval == EAGAIN) {
990 break;
991 }
1eb85ef5 992 run(retval, "vconn_recv");
31c6fcd7 993
0c9560b7
BP
994 if (timestamp) {
995 time_t now = time_wall();
996 char s[32];
997
998 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", localtime(&now));
999 fputs(s, stderr);
1000 }
31c6fcd7
BP
1001
1002 msg_type = ((const struct ofp_header *) b->data)->type;
1eb85ef5
EJ
1003 ofp_print(stderr, b->data, b->size, verbosity + 2);
1004 ofpbuf_delete(b);
bb638b9a 1005
5293a2e1 1006 if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) {
bde9f75d 1007 unixctl_command_reply(barrier_aux.conn, NULL);
bb638b9a
BP
1008 barrier_aux.conn = NULL;
1009 }
1eb85ef5
EJ
1010 }
1011
1012 if (exiting) {
1013 break;
1014 }
1015
1016 vconn_run(vconn);
1017 vconn_run_wait(vconn);
1018 vconn_recv_wait(vconn);
1019 unixctl_server_wait(server);
1020 poll_block();
0caf6bde 1021 }
828c72d0
BP
1022 vconn_close(vconn);
1023 unixctl_server_destroy(server);
0caf6bde
BP
1024}
1025
1026static void
1027do_monitor(int argc, char *argv[])
064af421
BP
1028{
1029 struct vconn *vconn;
1030
1031 open_vconn(argv[1], &vconn);
1032 if (argc > 2) {
7257b535 1033 struct ofp_switch_config config;
064af421 1034
7257b535
BP
1035 fetch_switch_config(vconn, &config);
1036 config.miss_send_len = htons(atoi(argv[2]));
1037 set_switch_config(vconn, &config);
064af421 1038 }
f0fd1a17
PS
1039 if (argc > 3) {
1040 if (!strcmp(argv[3], "invalid_ttl")) {
1041 monitor_set_invalid_ttl_to_controller(vconn);
1042 }
1043 }
ca8526e0
BP
1044 if (preferred_packet_in_format >= 0) {
1045 set_packet_in_format(vconn, preferred_packet_in_format);
1046 } else {
1047 struct ofpbuf *spif, *reply;
1048
1049 spif = ofputil_make_set_packet_in_format(NXPIF_NXM);
1050 run(vconn_transact_noreply(vconn, spif, &reply),
1051 "talking to %s", vconn_get_name(vconn));
1052 if (reply) {
1053 char *s = ofp_to_string(reply->data, reply->size, 2);
1054 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1055 " replied: %s. Falling back to the switch default.",
1056 vconn_get_name(vconn), s);
1057 free(s);
1058 ofpbuf_delete(reply);
1059 }
1060 }
1061
0caf6bde
BP
1062 monitor_vconn(vconn);
1063}
1064
1065static void
1066do_snoop(int argc OVS_UNUSED, char *argv[])
1067{
1068 struct vconn *vconn;
1069
1070 open_vconn__(argv[1], "snoop", &vconn);
1071 monitor_vconn(vconn);
064af421
BP
1072}
1073
1074static void
abaad8cf 1075do_dump_ports(int argc, char *argv[])
064af421 1076{
abaad8cf
JP
1077 struct ofp_port_stats_request *req;
1078 struct ofpbuf *request;
1079 uint16_t port;
1080
1081 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
1082 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
1083 req->port_no = htons(port);
1084 dump_stats_transaction(argv[1], request);
064af421
BP
1085}
1086
1087static void
c69ee87c 1088do_probe(int argc OVS_UNUSED, char *argv[])
064af421
BP
1089{
1090 struct ofpbuf *request;
1091 struct vconn *vconn;
1092 struct ofpbuf *reply;
1093
1094 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1095 open_vconn(argv[1], &vconn);
1096 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1097 if (reply->size != sizeof(struct ofp_header)) {
1098 ovs_fatal(0, "reply does not match request");
1099 }
1100 ofpbuf_delete(reply);
1101 vconn_close(vconn);
1102}
1103
0c3d5fc8
BP
1104static void
1105do_packet_out(int argc, char *argv[])
1106{
1107 struct ofputil_packet_out po;
1108 struct ofpbuf actions;
1109 struct vconn *vconn;
1110 int i;
1111
1112 ofpbuf_init(&actions, sizeof(union ofp_action));
1113 parse_ofp_actions(argv[3], &actions);
1114
1115 po.buffer_id = UINT32_MAX;
1116 po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE
1117 : !strcasecmp(argv[2], "local") ? OFPP_LOCAL
1118 : str_to_port_no(argv[1], argv[2]));
1119 po.actions = actions.data;
1120 po.n_actions = actions.size / sizeof(union ofp_action);
1121
1122 open_vconn(argv[1], &vconn);
1123 for (i = 4; i < argc; i++) {
1124 struct ofpbuf *packet, *opo;
1125 const char *error_msg;
1126
1127 error_msg = eth_from_hex(argv[i], &packet);
1128 if (error_msg) {
1129 ovs_fatal(0, "%s", error_msg);
1130 }
1131
1132 po.packet = packet->data;
1133 po.packet_len = packet->size;
1134 opo = ofputil_encode_packet_out(&po);
1135 transact_noreply(vconn, opo);
1136 ofpbuf_delete(packet);
1137 }
1138 vconn_close(vconn);
1139}
1140
064af421 1141static void
c69ee87c 1142do_mod_port(int argc OVS_UNUSED, char *argv[])
064af421 1143{
064af421 1144 struct ofp_port_mod *opm;
0df0e81d
BP
1145 struct ofp_phy_port opp;
1146 struct ofpbuf *request;
064af421 1147 struct vconn *vconn;
064af421 1148
0df0e81d 1149 fetch_ofp_phy_port(argv[1], argv[2], &opp);
064af421 1150
5293a2e1
BP
1151 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT10_PORT_MOD,
1152 &request);
0df0e81d
BP
1153 opm->port_no = opp.port_no;
1154 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
064af421
BP
1155 opm->config = htonl(0);
1156 opm->mask = htonl(0);
1157 opm->advertise = htonl(0);
1158
f4866781 1159 if (!strcasecmp(argv[3], "up")) {
064af421 1160 opm->mask |= htonl(OFPPC_PORT_DOWN);
f4866781 1161 } else if (!strcasecmp(argv[3], "down")) {
064af421
BP
1162 opm->mask |= htonl(OFPPC_PORT_DOWN);
1163 opm->config |= htonl(OFPPC_PORT_DOWN);
f4866781 1164 } else if (!strcasecmp(argv[3], "flood")) {
064af421 1165 opm->mask |= htonl(OFPPC_NO_FLOOD);
f4866781 1166 } else if (!strcasecmp(argv[3], "noflood")) {
064af421
BP
1167 opm->mask |= htonl(OFPPC_NO_FLOOD);
1168 opm->config |= htonl(OFPPC_NO_FLOOD);
451256f6
EJ
1169 } else if (!strcasecmp(argv[3], "forward")) {
1170 opm->mask |= htonl(OFPPC_NO_FWD);
1171 } else if (!strcasecmp(argv[3], "noforward")) {
1172 opm->mask |= htonl(OFPPC_NO_FWD);
1173 opm->config |= htonl(OFPPC_NO_FWD);
064af421
BP
1174 } else {
1175 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1176 }
1177
0df0e81d 1178 open_vconn(argv[1], &vconn);
88ca35ee 1179 transact_noreply(vconn, request);
064af421
BP
1180 vconn_close(vconn);
1181}
1182
7257b535
BP
1183static void
1184do_get_frags(int argc OVS_UNUSED, char *argv[])
1185{
1186 struct ofp_switch_config config;
1187 struct vconn *vconn;
1188
1189 open_vconn(argv[1], &vconn);
1190 fetch_switch_config(vconn, &config);
1191 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1192 vconn_close(vconn);
1193}
1194
1195static void
1196do_set_frags(int argc OVS_UNUSED, char *argv[])
1197{
1198 struct ofp_switch_config config;
1199 enum ofp_config_flags mode;
1200 struct vconn *vconn;
1201 ovs_be16 flags;
1202
1203 if (!ofputil_frag_handling_from_string(argv[2], &mode)) {
1204 ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]);
1205 }
1206
1207 open_vconn(argv[1], &vconn);
1208 fetch_switch_config(vconn, &config);
1209 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1210 if (flags != config.flags) {
1211 /* Set the configuration. */
1212 config.flags = flags;
1213 set_switch_config(vconn, &config);
1214
1215 /* Then retrieve the configuration to see if it really took. OpenFlow
1216 * doesn't define error reporting for bad modes, so this is all we can
1217 * do. */
1218 fetch_switch_config(vconn, &config);
1219 if (flags != config.flags) {
1220 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1221 "switch probably doesn't support mode \"%s\")",
1222 argv[1], ofputil_frag_handling_to_string(mode));
1223 }
1224 }
1225 vconn_close(vconn);
1226}
1227
064af421 1228static void
675febfa 1229do_ping(int argc, char *argv[])
064af421
BP
1230{
1231 size_t max_payload = 65535 - sizeof(struct ofp_header);
1232 unsigned int payload;
1233 struct vconn *vconn;
1234 int i;
1235
1236 payload = argc > 2 ? atoi(argv[2]) : 64;
1237 if (payload > max_payload) {
1238 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1239 }
1240
1241 open_vconn(argv[1], &vconn);
1242 for (i = 0; i < 10; i++) {
1243 struct timeval start, end;
1244 struct ofpbuf *request, *reply;
1245 struct ofp_header *rq_hdr, *rpy_hdr;
1246
1247 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1248 OFPT_ECHO_REQUEST, &request);
1249 random_bytes(rq_hdr + 1, payload);
1250
279c9e03 1251 xgettimeofday(&start);
064af421 1252 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
279c9e03 1253 xgettimeofday(&end);
064af421
BP
1254
1255 rpy_hdr = reply->data;
1256 if (reply->size != request->size
1257 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1258 || rpy_hdr->xid != rq_hdr->xid
1259 || rpy_hdr->type != OFPT_ECHO_REPLY) {
1260 printf("Reply does not match request. Request:\n");
4f564f8d 1261 ofp_print(stdout, request, request->size, verbosity + 2);
064af421 1262 printf("Reply:\n");
4f564f8d 1263 ofp_print(stdout, reply, reply->size, verbosity + 2);
064af421 1264 }
2886875a 1265 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
44381c1b 1266 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
064af421
BP
1267 (1000*(double)(end.tv_sec - start.tv_sec))
1268 + (.001*(end.tv_usec - start.tv_usec)));
1269 ofpbuf_delete(request);
1270 ofpbuf_delete(reply);
1271 }
1272 vconn_close(vconn);
1273}
1274
1275static void
c69ee87c 1276do_benchmark(int argc OVS_UNUSED, char *argv[])
064af421
BP
1277{
1278 size_t max_payload = 65535 - sizeof(struct ofp_header);
1279 struct timeval start, end;
1280 unsigned int payload_size, message_size;
1281 struct vconn *vconn;
1282 double duration;
1283 int count;
1284 int i;
1285
1286 payload_size = atoi(argv[2]);
1287 if (payload_size > max_payload) {
1288 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1289 }
1290 message_size = sizeof(struct ofp_header) + payload_size;
1291
1292 count = atoi(argv[3]);
1293
1294 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1295 count, message_size, count * message_size);
1296
1297 open_vconn(argv[1], &vconn);
279c9e03 1298 xgettimeofday(&start);
064af421
BP
1299 for (i = 0; i < count; i++) {
1300 struct ofpbuf *request, *reply;
1301 struct ofp_header *rq_hdr;
1302
1303 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1304 memset(rq_hdr + 1, 0, payload_size);
1305 run(vconn_transact(vconn, request, &reply), "transact");
1306 ofpbuf_delete(reply);
1307 }
279c9e03 1308 xgettimeofday(&end);
064af421
BP
1309 vconn_close(vconn);
1310
1311 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1312 + (.001*(end.tv_usec - start.tv_usec)));
1313 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1314 duration, count / (duration / 1000.0),
1315 count * message_size / (duration / 1000.0));
1316}
1317
09246b99
BP
1318static void
1319do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1320{
1321 usage();
1322}
1323\f
0199c526
BP
1324/* replace-flows and diff-flows commands. */
1325
1326/* A flow table entry, possibly with two different versions. */
1327struct fte {
1328 struct cls_rule rule; /* Within a "struct classifier". */
1329 struct fte_version *versions[2];
1330};
1331
1332/* One version of a Flow Table Entry. */
1333struct fte_version {
1334 ovs_be64 cookie;
1335 uint16_t idle_timeout;
1336 uint16_t hard_timeout;
1337 uint16_t flags;
1338 union ofp_action *actions;
1339 size_t n_actions;
1340};
1341
1342/* Frees 'version' and the data that it owns. */
1343static void
1344fte_version_free(struct fte_version *version)
1345{
1346 if (version) {
1347 free(version->actions);
1348 free(version);
1349 }
1350}
1351
1352/* Returns true if 'a' and 'b' are the same, false if they differ.
1353 *
1354 * Ignores differences in 'flags' because there's no way to retrieve flags from
1355 * an OpenFlow switch. We have to assume that they are the same. */
1356static bool
1357fte_version_equals(const struct fte_version *a, const struct fte_version *b)
1358{
1359 return (a->cookie == b->cookie
1360 && a->idle_timeout == b->idle_timeout
1361 && a->hard_timeout == b->hard_timeout
1362 && a->n_actions == b->n_actions
1363 && !memcmp(a->actions, b->actions,
1364 a->n_actions * sizeof *a->actions));
1365}
1366
1367/* Prints 'version' on stdout. Expects the caller to have printed the rule
1368 * associated with the version. */
1369static void
1370fte_version_print(const struct fte_version *version)
1371{
1372 struct ds s;
1373
1374 if (version->cookie != htonll(0)) {
1375 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
1376 }
1377 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
1378 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
1379 }
1380 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
1381 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
1382 }
1383
1384 ds_init(&s);
b4b8c781 1385 ofp_print_actions(&s, version->actions, version->n_actions);
0199c526
BP
1386 printf(" %s\n", ds_cstr(&s));
1387 ds_destroy(&s);
1388}
1389
1390static struct fte *
1391fte_from_cls_rule(const struct cls_rule *cls_rule)
1392{
1393 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
1394}
1395
1396/* Frees 'fte' and its versions. */
1397static void
1398fte_free(struct fte *fte)
1399{
1400 if (fte) {
1401 fte_version_free(fte->versions[0]);
1402 fte_version_free(fte->versions[1]);
1403 free(fte);
1404 }
1405}
1406
1407/* Frees all of the FTEs within 'cls'. */
1408static void
1409fte_free_all(struct classifier *cls)
1410{
1411 struct cls_cursor cursor;
1412 struct fte *fte, *next;
1413
1414 cls_cursor_init(&cursor, cls, NULL);
1415 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
1416 classifier_remove(cls, &fte->rule);
1417 fte_free(fte);
1418 }
1419}
1420
1421/* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1422 * necessary. Sets 'version' as the version of that rule with the given
1423 * 'index', replacing any existing version, if any.
1424 *
1425 * Takes ownership of 'version'. */
1426static void
1427fte_insert(struct classifier *cls, const struct cls_rule *rule,
1428 struct fte_version *version, int index)
1429{
1430 struct fte *old, *fte;
1431
1432 fte = xzalloc(sizeof *fte);
1433 fte->rule = *rule;
1434 fte->versions[index] = version;
1435
08944c1d 1436 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
0199c526
BP
1437 if (old) {
1438 fte_version_free(old->versions[index]);
1439 fte->versions[!index] = old->versions[!index];
1440 free(old);
1441 }
1442}
1443
1444/* Reads the flows in 'filename' as flow table entries in 'cls' for the version
27527aa0
BP
1445 * with the specified 'index'. Returns the flow formats able to represent the
1446 * flows that were read. */
1447static enum ofputil_protocol
0199c526
BP
1448read_flows_from_file(const char *filename, struct classifier *cls, int index)
1449{
27527aa0 1450 enum ofputil_protocol usable_protocols;
0199c526
BP
1451 struct ds s;
1452 FILE *file;
1453
1454 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1455 if (file == NULL) {
1456 ovs_fatal(errno, "%s: open", filename);
1457 }
1458
1459 ds_init(&s);
27527aa0 1460 usable_protocols = OFPUTIL_P_ANY;
0199c526
BP
1461 while (!ds_get_preprocessed_line(&s, file)) {
1462 struct fte_version *version;
a9a2da38 1463 struct ofputil_flow_mod fm;
0199c526 1464
c821124b 1465 parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
0199c526
BP
1466
1467 version = xmalloc(sizeof *version);
1468 version->cookie = fm.cookie;
1469 version->idle_timeout = fm.idle_timeout;
1470 version->hard_timeout = fm.hard_timeout;
1471 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
e89993f3
BP
1472 version->actions = fm.actions;
1473 version->n_actions = fm.n_actions;
0199c526 1474
27527aa0 1475 usable_protocols &= ofputil_usable_protocols(&fm.cr);
0199c526
BP
1476
1477 fte_insert(cls, &fm.cr, version, index);
1478 }
1479 ds_destroy(&s);
1480
1481 if (file != stdin) {
1482 fclose(file);
1483 }
1484
27527aa0 1485 return usable_protocols;
0199c526
BP
1486}
1487
1488/* Reads the OpenFlow flow table from 'vconn', which has currently active flow
27527aa0 1489 * format 'protocol', and adds them as flow table entries in 'cls' for the
0199c526
BP
1490 * version with the specified 'index'. */
1491static void
27527aa0
BP
1492read_flows_from_switch(struct vconn *vconn,
1493 enum ofputil_protocol protocol,
0199c526
BP
1494 struct classifier *cls, int index)
1495{
81d1ea94 1496 struct ofputil_flow_stats_request fsr;
0199c526
BP
1497 struct ofpbuf *request;
1498 ovs_be32 send_xid;
1499 bool done;
1500
1501 fsr.aggregate = false;
1502 cls_rule_init_catchall(&fsr.match, 0);
1503 fsr.out_port = OFPP_NONE;
1504 fsr.table_id = 0xff;
ecc798ab 1505 fsr.cookie = fsr.cookie_mask = htonll(0);
27527aa0 1506 request = ofputil_encode_flow_stats_request(&fsr, protocol);
0199c526
BP
1507 send_xid = ((struct ofp_header *) request->data)->xid;
1508 send_openflow_buffer(vconn, request);
1509
1510 done = false;
1511 while (!done) {
1512 ovs_be32 recv_xid;
1513 struct ofpbuf *reply;
1514
1515 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1516 recv_xid = ((struct ofp_header *) reply->data)->xid;
1517 if (send_xid == recv_xid) {
1518 const struct ofputil_msg_type *type;
28c8bad1 1519 const struct ofp_stats_msg *osm;
0199c526
BP
1520 enum ofputil_msg_code code;
1521
1522 ofputil_decode_msg_type(reply->data, &type);
1523 code = ofputil_msg_type_code(type);
1524 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1525 code != OFPUTIL_NXST_FLOW_REPLY) {
1526 ovs_fatal(0, "received bad reply: %s",
1527 ofp_to_string(reply->data, reply->size,
1528 verbosity + 1));
1529 }
1530
28c8bad1
BP
1531 osm = reply->data;
1532 if (!(osm->flags & htons(OFPSF_REPLY_MORE))) {
0199c526
BP
1533 done = true;
1534 }
1535
1536 for (;;) {
1537 struct fte_version *version;
1538 struct ofputil_flow_stats fs;
1539 int retval;
1540
f27f2134 1541 retval = ofputil_decode_flow_stats_reply(&fs, reply, false);
0199c526
BP
1542 if (retval) {
1543 if (retval != EOF) {
1544 ovs_fatal(0, "parse error in reply");
1545 }
1546 break;
1547 }
1548
1549 version = xmalloc(sizeof *version);
1550 version->cookie = fs.cookie;
1551 version->idle_timeout = fs.idle_timeout;
1552 version->hard_timeout = fs.hard_timeout;
1553 version->flags = 0;
1554 version->n_actions = fs.n_actions;
1555 version->actions = xmemdup(fs.actions,
1556 fs.n_actions * sizeof *fs.actions);
1557
1558 fte_insert(cls, &fs.rule, version, index);
1559 }
0199c526
BP
1560 } else {
1561 VLOG_DBG("received reply with xid %08"PRIx32" "
1562 "!= expected %08"PRIx32, recv_xid, send_xid);
1563 }
1564 ofpbuf_delete(reply);
1565 }
1566}
1567
1568static void
1569fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
27527aa0 1570 enum ofputil_protocol protocol, struct list *packets)
0199c526
BP
1571{
1572 const struct fte_version *version = fte->versions[index];
a9a2da38 1573 struct ofputil_flow_mod fm;
0199c526
BP
1574 struct ofpbuf *ofm;
1575
1576 fm.cr = fte->rule;
1577 fm.cookie = version->cookie;
6c1491fb 1578 fm.table_id = 0xff;
0199c526
BP
1579 fm.command = command;
1580 fm.idle_timeout = version->idle_timeout;
1581 fm.hard_timeout = version->hard_timeout;
1582 fm.buffer_id = UINT32_MAX;
1583 fm.out_port = OFPP_NONE;
1584 fm.flags = version->flags;
1585 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1586 command == OFPFC_MODIFY_STRICT) {
1587 fm.actions = version->actions;
1588 fm.n_actions = version->n_actions;
1589 } else {
1590 fm.actions = NULL;
1591 fm.n_actions = 0;
1592 }
1593
27527aa0 1594 ofm = ofputil_encode_flow_mod(&fm, protocol);
0199c526
BP
1595 list_push_back(packets, &ofm->list_node);
1596}
1597
1598static void
1599do_replace_flows(int argc OVS_UNUSED, char *argv[])
1600{
1601 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
27527aa0 1602 enum ofputil_protocol usable_protocols, protocol;
0199c526
BP
1603 struct cls_cursor cursor;
1604 struct classifier cls;
1605 struct list requests;
1606 struct vconn *vconn;
1607 struct fte *fte;
1608
1609 classifier_init(&cls);
27527aa0 1610 usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
0199c526 1611
27527aa0
BP
1612 protocol = open_vconn(argv[1], &vconn);
1613 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
1614
1615 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
0199c526
BP
1616
1617 list_init(&requests);
1618
1619 /* Delete flows that exist on the switch but not in the file. */
1620 cls_cursor_init(&cursor, &cls, NULL);
1621 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1622 struct fte_version *file_ver = fte->versions[FILE_IDX];
1623 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1624
1625 if (sw_ver && !file_ver) {
1626 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
27527aa0 1627 protocol, &requests);
0199c526
BP
1628 }
1629 }
1630
1631 /* Add flows that exist in the file but not on the switch.
1632 * Update flows that exist in both places but differ. */
1633 cls_cursor_init(&cursor, &cls, NULL);
1634 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1635 struct fte_version *file_ver = fte->versions[FILE_IDX];
1636 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1637
c4ea79bf
BP
1638 if (file_ver
1639 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
27527aa0 1640 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
0199c526
BP
1641 }
1642 }
1643 transact_multiple_noreply(vconn, &requests);
1644 vconn_close(vconn);
1645
1646 fte_free_all(&cls);
1647}
1648
1649static void
1650read_flows_from_source(const char *source, struct classifier *cls, int index)
1651{
1652 struct stat s;
1653
1654 if (source[0] == '/' || source[0] == '.'
1655 || (!strchr(source, ':') && !stat(source, &s))) {
1656 read_flows_from_file(source, cls, index);
1657 } else {
27527aa0 1658 enum ofputil_protocol protocol;
0199c526
BP
1659 struct vconn *vconn;
1660
27527aa0
BP
1661 protocol = open_vconn(source, &vconn);
1662 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
1663 read_flows_from_switch(vconn, protocol, cls, index);
0199c526
BP
1664 vconn_close(vconn);
1665 }
1666}
1667
1668static void
1669do_diff_flows(int argc OVS_UNUSED, char *argv[])
1670{
1671 bool differences = false;
1672 struct cls_cursor cursor;
1673 struct classifier cls;
1674 struct fte *fte;
1675
1676 classifier_init(&cls);
1677 read_flows_from_source(argv[1], &cls, 0);
1678 read_flows_from_source(argv[2], &cls, 1);
1679
1680 cls_cursor_init(&cursor, &cls, NULL);
1681 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1682 struct fte_version *a = fte->versions[0];
1683 struct fte_version *b = fte->versions[1];
1684
1685 if (!a || !b || !fte_version_equals(a, b)) {
1686 char *rule_s = cls_rule_to_string(&fte->rule);
1687 if (a) {
1688 printf("-%s", rule_s);
1689 fte_version_print(a);
1690 }
1691 if (b) {
1692 printf("+%s", rule_s);
1693 fte_version_print(b);
1694 }
1695 free(rule_s);
1696
1697 differences = true;
1698 }
1699 }
1700
1701 fte_free_all(&cls);
1702
1703 if (differences) {
1704 exit(2);
1705 }
1706}
1707\f
09246b99
BP
1708/* Undocumented commands for unit testing. */
1709
770f1f66 1710static void
27527aa0 1711do_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
770f1f66 1712{
27527aa0
BP
1713 enum ofputil_protocol usable_protocols;
1714 enum ofputil_protocol protocol = 0;
1715 char *usable_s;
1716 size_t i;
770f1f66 1717
27527aa0
BP
1718 usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
1719 usable_s = ofputil_protocols_to_string(usable_protocols);
1720 printf("usable protocols: %s\n", usable_s);
1721 free(usable_s);
1722
1723 if (!(usable_protocols & allowed_protocols)) {
1724 ovs_fatal(0, "no usable protocol");
1725 }
1726 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1727 protocol = 1 << i;
1728 if (protocol & usable_protocols & allowed_protocols) {
1729 break;
1730 }
1731 }
1732 assert(IS_POW2(protocol));
1733
1734 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
1735
1736 for (i = 0; i < n_fms; i++) {
1737 struct ofputil_flow_mod *fm = &fms[i];
1738 struct ofpbuf *msg;
1739
1740 msg = ofputil_encode_flow_mod(fm, protocol);
1741 ofp_print(stdout, msg->data, msg->size, verbosity);
1742 ofpbuf_delete(msg);
1743
1744 free(fm->actions);
770f1f66
BP
1745 }
1746}
1747
1748/* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1749 * it back to stdout. */
1750static void
1751do_parse_flow(int argc OVS_UNUSED, char *argv[])
1752{
27527aa0 1753 struct ofputil_flow_mod fm;
770f1f66 1754
27527aa0
BP
1755 parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
1756 do_parse_flows__(&fm, 1);
770f1f66
BP
1757}
1758
fec00620
BP
1759/* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1760 * add-flows) and prints each of the flows back to stdout. */
0e581146
BP
1761static void
1762do_parse_flows(int argc OVS_UNUSED, char *argv[])
1763{
27527aa0
BP
1764 struct ofputil_flow_mod *fms = NULL;
1765 size_t n_fms = 0;
0e581146 1766
27527aa0
BP
1767 parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
1768 do_parse_flows__(fms, n_fms);
1769 free(fms);
0e581146
BP
1770}
1771
fec00620
BP
1772/* "parse-nx-match": reads a series of nx_match specifications as strings from
1773 * stdin, does some internal fussing with them, and then prints them back as
1774 * strings on stdout. */
064af421 1775static void
09246b99
BP
1776do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1777{
1778 struct ds in;
1779
1780 ds_init(&in);
1781 while (!ds_get_line(&in, stdin)) {
1782 struct ofpbuf nx_match;
1783 struct cls_rule rule;
e729e793 1784 ovs_be64 cookie, cookie_mask;
90bf1e07 1785 enum ofperr error;
09246b99 1786 int match_len;
09246b99
BP
1787 char *s;
1788
1789 /* Delete comments, skip blank lines. */
1790 s = ds_cstr(&in);
1791 if (*s == '#') {
1792 puts(s);
1793 continue;
1794 }
1795 if (strchr(s, '#')) {
1796 *strchr(s, '#') = '\0';
1797 }
1798 if (s[strspn(s, " ")] == '\0') {
1799 putchar('\n');
1800 continue;
1801 }
1802
1803 /* Convert string to nx_match. */
1804 ofpbuf_init(&nx_match, 0);
1805 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1806
1807 /* Convert nx_match to cls_rule. */
102ce766
EJ
1808 if (strict) {
1809 error = nx_pull_match(&nx_match, match_len, 0, &rule,
1810 &cookie, &cookie_mask);
1811 } else {
1812 error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
1813 &cookie, &cookie_mask);
1814 }
1815
09246b99
BP
1816 if (!error) {
1817 char *out;
1818
1819 /* Convert cls_rule back to nx_match. */
1820 ofpbuf_uninit(&nx_match);
1821 ofpbuf_init(&nx_match, 0);
e729e793 1822 match_len = nx_put_match(&nx_match, &rule, cookie, cookie_mask);
09246b99
BP
1823
1824 /* Convert nx_match to string. */
1825 out = nx_match_to_string(nx_match.data, match_len);
1826 puts(out);
1827 free(out);
1828 } else {
90bf1e07
BP
1829 printf("nx_pull_match() returned error %s\n",
1830 ofperr_get_name(error));
09246b99
BP
1831 }
1832
1833 ofpbuf_uninit(&nx_match);
1834 }
1835 ds_destroy(&in);
064af421
BP
1836}
1837
fec00620
BP
1838/* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1839 * binary data, interpreting them as an OpenFlow message, and prints the
1840 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1841static void
1842do_ofp_print(int argc, char *argv[])
1843{
1844 struct ofpbuf packet;
1845
1846 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1847 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1848 ovs_fatal(0, "trailing garbage following hex bytes");
1849 }
1850 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1851 ofpbuf_uninit(&packet);
1852}
1853
675febfa 1854static const struct command all_commands[] = {
064af421 1855 { "show", 1, 1, do_show },
f0fd1a17 1856 { "monitor", 1, 3, do_monitor },
0caf6bde 1857 { "snoop", 1, 1, do_snoop },
064af421
BP
1858 { "dump-desc", 1, 1, do_dump_desc },
1859 { "dump-tables", 1, 1, do_dump_tables },
1860 { "dump-flows", 1, 2, do_dump_flows },
1861 { "dump-aggregate", 1, 2, do_dump_aggregate },
d2805da2 1862 { "queue-stats", 1, 3, do_queue_stats },
064af421
BP
1863 { "add-flow", 2, 2, do_add_flow },
1864 { "add-flows", 2, 2, do_add_flows },
1865 { "mod-flows", 2, 2, do_mod_flows },
1866 { "del-flows", 1, 2, do_del_flows },
0199c526
BP
1867 { "replace-flows", 2, 2, do_replace_flows },
1868 { "diff-flows", 2, 2, do_diff_flows },
0c3d5fc8 1869 { "packet-out", 4, INT_MAX, do_packet_out },
abaad8cf 1870 { "dump-ports", 1, 2, do_dump_ports },
064af421 1871 { "mod-port", 3, 3, do_mod_port },
7257b535
BP
1872 { "get-frags", 1, 1, do_get_frags },
1873 { "set-frags", 2, 2, do_set_frags },
064af421
BP
1874 { "probe", 1, 1, do_probe },
1875 { "ping", 1, 2, do_ping },
1876 { "benchmark", 3, 3, do_benchmark },
064af421 1877 { "help", 0, INT_MAX, do_help },
09246b99
BP
1878
1879 /* Undocumented commands for testing. */
770f1f66 1880 { "parse-flow", 1, 1, do_parse_flow },
09246b99
BP
1881 { "parse-flows", 1, 1, do_parse_flows },
1882 { "parse-nx-match", 0, 0, do_parse_nx_match },
fec00620 1883 { "ofp-print", 1, 2, do_ofp_print },
09246b99 1884
064af421
BP
1885 { NULL, 0, 0, NULL },
1886};