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