]> git.proxmox.com Git - ovs.git/blame - utilities/ovs-ofctl.c
tests: Add ovs-openflowd to programs that need valgrind wrappers.
[ovs.git] / utilities / ovs-ofctl.c
CommitLineData
064af421 1/*
fec00620 2 * Copyright (c) 2008, 2009, 2010, 2011 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>
27#include <sys/stat.h>
28#include <sys/time.h>
29
10a24935 30#include "byte-order.h"
09246b99 31#include "classifier.h"
064af421
BP
32#include "command-line.h"
33#include "compiler.h"
34#include "dirs.h"
35#include "dpif.h"
09246b99 36#include "dynamic-string.h"
064af421 37#include "netlink.h"
09246b99 38#include "nx-match.h"
064af421 39#include "odp-util.h"
f22716dc 40#include "ofp-parse.h"
064af421 41#include "ofp-print.h"
fa37b408 42#include "ofp-util.h"
064af421
BP
43#include "ofpbuf.h"
44#include "openflow/nicira-ext.h"
45#include "openflow/openflow.h"
064af421 46#include "random.h"
fe55ad15 47#include "stream-ssl.h"
064af421
BP
48#include "timeval.h"
49#include "util.h"
064af421 50#include "vconn.h"
5136ce49 51#include "vlog.h"
064af421 52
d98e6007 53VLOG_DEFINE_THIS_MODULE(ofctl);
064af421 54
88ca35ee 55/* --strict: Use strict matching for flow mod commands? */
675febfa 56static bool strict;
064af421 57
88ca35ee
BP
58/* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
59 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
60static int preferred_flow_format = -1;
61
4f564f8d
BP
62/* -m, --more: Additional verbosity for ofp-print functions. */
63static int verbosity;
64
675febfa 65static const struct command all_commands[];
064af421
BP
66
67static void usage(void) NO_RETURN;
675febfa 68static void parse_options(int argc, char *argv[]);
064af421 69
675febfa
BP
70int
71main(int argc, char *argv[])
064af421 72{
064af421 73 set_program_name(argv[0]);
675febfa 74 parse_options(argc, argv);
064af421 75 signal(SIGPIPE, SIG_IGN);
675febfa 76 run_command(argc - optind, argv + optind, all_commands);
064af421
BP
77 return 0;
78}
79
80static void
675febfa 81parse_options(int argc, char *argv[])
064af421
BP
82{
83 enum {
87c84891
JP
84 OPT_STRICT = UCHAR_MAX + 1,
85 VLOG_OPTION_ENUMS
064af421
BP
86 };
87 static struct option long_options[] = {
88 {"timeout", required_argument, 0, 't'},
064af421 89 {"strict", no_argument, 0, OPT_STRICT},
88ca35ee 90 {"flow-format", required_argument, 0, 'F'},
4f564f8d 91 {"more", no_argument, 0, 'm'},
064af421
BP
92 {"help", no_argument, 0, 'h'},
93 {"version", no_argument, 0, 'V'},
87c84891 94 VLOG_LONG_OPTIONS,
fe55ad15 95 STREAM_SSL_LONG_OPTIONS
064af421
BP
96 {0, 0, 0, 0},
97 };
98 char *short_options = long_options_to_short_options(long_options);
99
064af421
BP
100 for (;;) {
101 unsigned long int timeout;
102 int c;
103
104 c = getopt_long(argc, argv, short_options, long_options, NULL);
105 if (c == -1) {
106 break;
107 }
108
109 switch (c) {
110 case 't':
111 timeout = strtoul(optarg, NULL, 10);
112 if (timeout <= 0) {
113 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
114 optarg);
115 } else {
116 time_alarm(timeout);
117 }
118 break;
119
88ca35ee
BP
120 case 'F':
121 preferred_flow_format = ofputil_flow_format_from_string(optarg);
122 if (preferred_flow_format < 0) {
123 ovs_fatal(0, "unknown flow format `%s'", optarg);
124 }
125 break;
126
4f564f8d
BP
127 case 'm':
128 verbosity++;
129 break;
130
064af421
BP
131 case 'h':
132 usage();
133
134 case 'V':
135 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
136 exit(EXIT_SUCCESS);
137
064af421 138 case OPT_STRICT:
675febfa 139 strict = true;
064af421
BP
140 break;
141
87c84891 142 VLOG_OPTION_HANDLERS
fe55ad15 143 STREAM_SSL_OPTION_HANDLERS
064af421
BP
144
145 case '?':
146 exit(EXIT_FAILURE);
147
148 default:
149 abort();
150 }
151 }
152 free(short_options);
153}
154
155static void
156usage(void)
157{
158 printf("%s: OpenFlow switch management utility\n"
159 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
160 "\nFor OpenFlow switches:\n"
161 " show SWITCH show OpenFlow information\n"
064af421
BP
162 " dump-desc SWITCH print switch description\n"
163 " dump-tables SWITCH print table stats\n"
164 " mod-port SWITCH IFACE ACT modify port behavior\n"
abaad8cf 165 " dump-ports SWITCH [PORT] print port statistics\n"
064af421
BP
166 " dump-flows SWITCH print all flow entries\n"
167 " dump-flows SWITCH FLOW print matching FLOWs\n"
168 " dump-aggregate SWITCH print aggregate flow statistics\n"
169 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
d2805da2 170 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
064af421
BP
171 " add-flow SWITCH FLOW add flow described by FLOW\n"
172 " add-flows SWITCH FILE add flows from FILE\n"
173 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
174 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
a6bc4a03 175 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
064af421
BP
176 "\nFor OpenFlow switches and controllers:\n"
177 " probe VCONN probe whether VCONN is up\n"
178 " ping VCONN [N] latency of N-byte echos\n"
179 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
180 "where each SWITCH is an active OpenFlow connection method.\n",
181 program_name, program_name);
182 vconn_usage(true, false, false);
183 vlog_usage();
184 printf("\nOther options:\n"
185 " --strict use strict match for flow commands\n"
88ca35ee 186 " -F, --flow-format=FORMAT force particular flow format\n"
4f564f8d 187 " -m, --more be more verbose printing OpenFlow\n"
064af421
BP
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n"
190 " -V, --version display version information\n");
191 exit(EXIT_SUCCESS);
192}
193
194static void run(int retval, const char *message, ...)
195 PRINTF_FORMAT(2, 3);
196
197static void run(int retval, const char *message, ...)
198{
199 if (retval) {
200 va_list args;
201
064af421 202 va_start(args, message);
fcaddd4d 203 ovs_fatal_valist(retval, message, args);
064af421
BP
204 }
205}
206\f
207/* Generic commands. */
208
1a6f1e2a
JG
209static void
210open_vconn_socket(const char *name, struct vconn **vconnp)
211{
212 char *vconn_name = xasprintf("unix:%s", name);
24cd0dee 213 VLOG_DBG("connecting to %s", vconn_name);
1a6f1e2a
JG
214 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
215 "connecting to %s", vconn_name);
216 free(vconn_name);
217}
218
064af421 219static void
0caf6bde
BP
220open_vconn__(const char *name, const char *default_suffix,
221 struct vconn **vconnp)
064af421 222{
c228a364 223 struct dpif *dpif;
064af421 224 struct stat s;
1a6f1e2a
JG
225 char *bridge_path, *datapath_name, *datapath_type;
226
b43c6fe2 227 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
1a6f1e2a 228 dp_parse_name(name, &datapath_name, &datapath_type);
064af421
BP
229
230 if (strstr(name, ":")) {
231 run(vconn_open_block(name, OFP_VERSION, vconnp),
232 "connecting to %s", name);
233 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
1a6f1e2a
JG
234 open_vconn_socket(name, vconnp);
235 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
236 open_vconn_socket(bridge_path, vconnp);
237 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
064af421
BP
238 char dpif_name[IF_NAMESIZE + 1];
239 char *socket_name;
064af421 240
c228a364 241 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
064af421 242 "obtaining name of %s", dpif_name);
c228a364 243 dpif_close(dpif);
064af421 244 if (strcmp(dpif_name, name)) {
24cd0dee 245 VLOG_DBG("datapath %s is named %s", name, dpif_name);
064af421
BP
246 }
247
0caf6bde 248 socket_name = xasprintf("%s/%s.%s",
b43c6fe2 249 ovs_rundir(), dpif_name, default_suffix);
064af421
BP
250 if (stat(socket_name, &s)) {
251 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
252 name, socket_name);
253 } else if (!S_ISSOCK(s.st_mode)) {
254 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
255 name, socket_name);
256 }
257
1a6f1e2a 258 open_vconn_socket(socket_name, vconnp);
064af421 259 free(socket_name);
064af421
BP
260 } else {
261 ovs_fatal(0, "%s is not a valid connection method", name);
262 }
1a6f1e2a
JG
263
264 free(datapath_name);
265 free(datapath_type);
266 free(bridge_path);
064af421
BP
267}
268
0caf6bde
BP
269static void
270open_vconn(const char *name, struct vconn **vconnp)
271{
272 return open_vconn__(name, "mgmt", vconnp);
273}
274
064af421
BP
275static void *
276alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
277{
278 struct ofp_stats_request *rq;
279 rq = make_openflow((offsetof(struct ofp_stats_request, body)
280 + body_len), OFPT_STATS_REQUEST, bufferp);
281 rq->type = htons(type);
282 rq->flags = htons(0);
283 return rq->body;
284}
285
286static void
287send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
288{
289 update_openflow_length(buffer);
290 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
291}
292
293static void
294dump_transaction(const char *vconn_name, struct ofpbuf *request)
295{
296 struct vconn *vconn;
297 struct ofpbuf *reply;
298
299 update_openflow_length(request);
300 open_vconn(vconn_name, &vconn);
301 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
4f564f8d 302 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421
BP
303 vconn_close(vconn);
304}
305
306static void
307dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
308{
309 struct ofpbuf *request;
310 make_openflow(sizeof(struct ofp_header), request_type, &request);
311 dump_transaction(vconn_name, request);
312}
313
314static void
315dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
316{
44381c1b 317 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
064af421
BP
318 struct vconn *vconn;
319 bool done = false;
320
321 open_vconn(vconn_name, &vconn);
322 send_openflow_buffer(vconn, request);
323 while (!done) {
02833365 324 ovs_be32 recv_xid;
064af421
BP
325 struct ofpbuf *reply;
326
327 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
328 recv_xid = ((struct ofp_header *) reply->data)->xid;
329 if (send_xid == recv_xid) {
330 struct ofp_stats_reply *osr;
331
4f564f8d 332 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421
BP
333
334 osr = ofpbuf_at(reply, 0, sizeof *osr);
335 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
336 } else {
337 VLOG_DBG("received reply with xid %08"PRIx32" "
338 "!= expected %08"PRIx32, recv_xid, send_xid);
339 }
340 ofpbuf_delete(reply);
341 }
342 vconn_close(vconn);
343}
344
345static void
346dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
347{
348 struct ofpbuf *request;
349 alloc_stats_request(0, stats_type, &request);
350 dump_stats_transaction(vconn_name, request);
351}
352
d12513f7
BP
353/* Sends 'request', which should be a request that only has a reply if an error
354 * occurs, and waits for it to succeed or fail. If an error does occur, prints
355 * it and exits with an error. */
356static void
88ca35ee 357transact_multiple_noreply(struct vconn *vconn, struct list *requests)
d12513f7 358{
88ca35ee 359 struct ofpbuf *request, *reply;
d12513f7 360
88ca35ee
BP
361 LIST_FOR_EACH (request, list_node, requests) {
362 update_openflow_length(request);
363 }
364
365 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
d12513f7
BP
366 "talking to %s", vconn_get_name(vconn));
367 if (reply) {
4f564f8d 368 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
d12513f7
BP
369 exit(1);
370 }
371 ofpbuf_delete(reply);
372}
373
88ca35ee
BP
374/* Sends 'request', which should be a request that only has a reply if an error
375 * occurs, and waits for it to succeed or fail. If an error does occur, prints
376 * it and exits with an error. */
377static void
378transact_noreply(struct vconn *vconn, struct ofpbuf *request)
379{
380 struct list requests;
381
382 list_init(&requests);
383 list_push_back(&requests, &request->list_node);
384 transact_multiple_noreply(vconn, &requests);
385}
386
064af421 387static void
c69ee87c 388do_show(int argc OVS_UNUSED, char *argv[])
064af421
BP
389{
390 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
391 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
392}
393
064af421 394static void
c69ee87c 395do_dump_desc(int argc OVS_UNUSED, char *argv[])
064af421
BP
396{
397 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
398}
399
400static void
c69ee87c 401do_dump_tables(int argc OVS_UNUSED, char *argv[])
064af421
BP
402{
403 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
404}
405
0df0e81d
BP
406/* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
407 * 'port_name' (which may be a port name or number), and copies it into
408 * '*oppp'. */
409static void
410fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
411 struct ofp_phy_port *oppp)
abaad8cf
JP
412{
413 struct ofpbuf *request, *reply;
414 struct ofp_switch_features *osf;
0df0e81d 415 unsigned int port_no;
abaad8cf
JP
416 struct vconn *vconn;
417 int n_ports;
418 int port_idx;
abaad8cf 419
0df0e81d
BP
420 /* Try to interpret the argument as a port number. */
421 if (!str_to_uint(port_name, 10, &port_no)) {
422 port_no = UINT_MAX;
abaad8cf
JP
423 }
424
0df0e81d 425 /* Fetch the switch's ofp_switch_features. */
abaad8cf
JP
426 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
427 open_vconn(vconn_name, &vconn);
428 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
429
430 osf = reply->data;
0df0e81d
BP
431 if (reply->size < sizeof *osf) {
432 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
433 vconn_name, reply->size);
434 }
abaad8cf
JP
435 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
436
437 for (port_idx = 0; port_idx < n_ports; port_idx++) {
0df0e81d
BP
438 const struct ofp_phy_port *opp = &osf->ports[port_idx];
439
440 if (port_no != UINT_MAX
441 ? htons(port_no) == opp->port_no
0b61210e 442 : !strncmp(opp->name, port_name, sizeof opp->name)) {
0df0e81d
BP
443 *oppp = *opp;
444 ofpbuf_delete(reply);
445 vconn_close(vconn);
446 return;
abaad8cf
JP
447 }
448 }
0df0e81d
BP
449 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
450}
abaad8cf 451
0df0e81d
BP
452/* Returns the port number corresponding to 'port_name' (which may be a port
453 * name or number) within the switch 'vconn_name'. */
454static uint16_t
455str_to_port_no(const char *vconn_name, const char *port_name)
456{
457 unsigned int port_no;
458
459 if (str_to_uint(port_name, 10, &port_no)) {
460 return port_no;
461 } else {
462 struct ofp_phy_port opp;
abaad8cf 463
0df0e81d
BP
464 fetch_ofp_phy_port(vconn_name, port_name, &opp);
465 return ntohs(opp.port_no);
466 }
abaad8cf
JP
467}
468
88ca35ee
BP
469static bool
470try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
471{
472 struct ofpbuf *sff, *reply;
473
474 sff = ofputil_make_set_flow_format(flow_format);
475 run(vconn_transact_noreply(vconn, sff, &reply),
476 "talking to %s", vconn_get_name(vconn));
477 if (reply) {
478 char *s = ofp_to_string(reply->data, reply->size, 2);
479 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
480 vconn_get_name(vconn),
481 ofputil_flow_format_to_string(flow_format),
482 s);
483 free(s);
484 ofpbuf_delete(reply);
485 return false;
486 }
487 return true;
488}
489
064af421 490static void
88ca35ee 491set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
064af421 492{
88ca35ee
BP
493 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
494 transact_noreply(vconn, sff);
495 VLOG_DBG("%s: using user-specified flow format %s",
496 vconn_get_name(vconn),
497 ofputil_flow_format_to_string(flow_format));
498}
064af421 499
88ca35ee 500static enum nx_flow_format
0199c526
BP
501negotiate_highest_flow_format(struct vconn *vconn,
502 enum nx_flow_format min_format)
88ca35ee 503{
88ca35ee 504 if (preferred_flow_format != -1) {
f9cbfbe4
BP
505 if (preferred_flow_format < min_format) {
506 ovs_fatal(0, "%s: cannot use requested flow format %s for "
507 "specified flow", vconn_get_name(vconn),
508 ofputil_flow_format_to_string(min_format));
509 }
510
511 set_flow_format(vconn, preferred_flow_format);
512 return preferred_flow_format;
513 } else {
514 enum nx_flow_format flow_format;
88ca35ee 515
f9cbfbe4
BP
516 if (try_set_flow_format(vconn, NXFF_NXM)) {
517 flow_format = NXFF_NXM;
518 } else if (try_set_flow_format(vconn, NXFF_TUN_ID_FROM_COOKIE)) {
519 flow_format = NXFF_TUN_ID_FROM_COOKIE;
520 } else {
521 flow_format = NXFF_OPENFLOW10;
88ca35ee
BP
522 }
523
f9cbfbe4
BP
524 if (flow_format < min_format) {
525 ovs_fatal(0, "%s: cannot use switch's most advanced flow format "
526 "%s for specified flow", vconn_get_name(vconn),
527 ofputil_flow_format_to_string(min_format));
528 }
88ca35ee 529
f9cbfbe4
BP
530 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
531 ofputil_flow_format_to_string(flow_format));
532 return flow_format;
88ca35ee 533 }
064af421
BP
534}
535
536static void
88ca35ee 537do_dump_flows__(int argc, char *argv[], bool aggregate)
064af421 538{
0199c526 539 enum nx_flow_format min_flow_format, flow_format;
88ca35ee 540 struct flow_stats_request fsr;
064af421 541 struct ofpbuf *request;
88ca35ee 542 struct vconn *vconn;
064af421 543
88ca35ee 544 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
064af421 545
88ca35ee 546 open_vconn(argv[1], &vconn);
0199c526
BP
547 min_flow_format = ofputil_min_flow_format(&fsr.match, false, 0);
548 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
88ca35ee 549 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
064af421 550 dump_stats_transaction(argv[1], request);
88ca35ee
BP
551 vconn_close(vconn);
552}
553
554static void
555do_dump_flows(int argc, char *argv[])
556{
557 return do_dump_flows__(argc, argv, false);
558}
559
560static void
561do_dump_aggregate(int argc, char *argv[])
562{
563 return do_dump_flows__(argc, argv, true);
064af421
BP
564}
565
d2805da2
BP
566static void
567do_queue_stats(int argc, char *argv[])
568{
569 struct ofp_queue_stats_request *req;
570 struct ofpbuf *request;
571
572 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
573
574 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
575 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
576 } else {
577 req->port_no = htons(OFPP_ALL);
578 }
579 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
580 req->queue_id = htonl(atoi(argv[3]));
581 } else {
582 req->queue_id = htonl(OFPQ_ALL);
583 }
584
585 memset(req->pad, 0, sizeof req->pad);
586
587 dump_stats_transaction(argv[1], request);
588}
589
0fbc9f11
BP
590/* Sets up the flow format for a vconn that will be used to modify the flow
591 * table. Returns the flow format used, after possibly adding an OpenFlow
592 * request to 'requests'.
593 *
594 * If 'preferred_flow_format' is -1, returns NXFF_OPENFLOW10 without modifying
595 * 'requests', since NXFF_OPENFLOW10 is the default flow format for any
596 * OpenFlow connection.
597 *
598 * If 'preferred_flow_format' is a specific format, adds a request to set that
599 * format to 'requests' and returns the format. */
600static enum nx_flow_format
601set_initial_format_for_flow_mod(struct list *requests)
602{
603 if (preferred_flow_format < 0) {
604 return NXFF_OPENFLOW10;
605 } else {
606 struct ofpbuf *sff;
607
608 sff = ofputil_make_set_flow_format(preferred_flow_format);
609 list_push_back(requests, &sff->list_node);
610 return preferred_flow_format;
611 }
612}
613
614/* Checks that 'flow_format' is acceptable as a flow format after a flow_mod
615 * operation, given the global 'preferred_flow_format'. */
616static void
617check_final_format_for_flow_mod(enum nx_flow_format flow_format)
618{
0199c526 619 if (preferred_flow_format >= 0 && flow_format > preferred_flow_format) {
0fbc9f11
BP
620 ovs_fatal(0, "flow cannot be expressed in flow format %s "
621 "(flow format %s or better is required)",
622 ofputil_flow_format_to_string(preferred_flow_format),
623 ofputil_flow_format_to_string(flow_format));
624 }
625}
626
064af421 627static void
4989c59f 628do_flow_mod_file__(int argc OVS_UNUSED, char *argv[], uint16_t command)
064af421 629{
88ca35ee
BP
630 enum nx_flow_format flow_format;
631 struct list requests;
064af421 632 struct vconn *vconn;
4989c59f
BP
633 FILE *file;
634
635 file = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "r");
636 if (file == NULL) {
637 ovs_fatal(errno, "%s: open", argv[2]);
638 }
049c8dc2 639
88ca35ee 640 list_init(&requests);
0fbc9f11
BP
641 flow_format = set_initial_format_for_flow_mod(&requests);
642
064af421 643 open_vconn(argv[1], &vconn);
4989c59f
BP
644 while (parse_ofp_flow_mod_file(&requests, &flow_format, file, command)) {
645 check_final_format_for_flow_mod(flow_format);
646 transact_multiple_noreply(vconn, &requests);
647 }
064af421 648 vconn_close(vconn);
064af421 649
4989c59f
BP
650 if (file != stdin) {
651 fclose(file);
652 }
88ca35ee
BP
653}
654
064af421 655static void
4989c59f 656do_flow_mod__(int argc, char *argv[], uint16_t command)
064af421 657{
88ca35ee
BP
658 enum nx_flow_format flow_format;
659 struct list requests;
064af421 660 struct vconn *vconn;
064af421 661
4989c59f
BP
662 if (argc > 2 && !strcmp(argv[2], "-")) {
663 do_flow_mod_file__(argc, argv, command);
664 return;
064af421
BP
665 }
666
88ca35ee 667 list_init(&requests);
0fbc9f11 668 flow_format = set_initial_format_for_flow_mod(&requests);
88ca35ee 669
4989c59f
BP
670 parse_ofp_flow_mod_str(&requests, &flow_format, argc > 2 ? argv[2] : "",
671 command);
672 check_final_format_for_flow_mod(flow_format);
673
064af421 674 open_vconn(argv[1], &vconn);
4989c59f 675 transact_multiple_noreply(vconn, &requests);
064af421 676 vconn_close(vconn);
4989c59f 677}
88ca35ee 678
4989c59f
BP
679static void
680do_add_flow(int argc, char *argv[])
681{
682 do_flow_mod__(argc, argv, OFPFC_ADD);
683}
684
685static void
686do_add_flows(int argc, char *argv[])
687{
688 do_flow_mod_file__(argc, argv, OFPFC_ADD);
064af421
BP
689}
690
691static void
88ca35ee 692do_mod_flows(int argc, char *argv[])
064af421 693{
88ca35ee 694 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
064af421
BP
695}
696
88ca35ee
BP
697static void
698do_del_flows(int argc, char *argv[])
064af421 699{
88ca35ee 700 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
064af421
BP
701}
702
703static void
0caf6bde
BP
704monitor_vconn(struct vconn *vconn)
705{
706 for (;;) {
707 struct ofpbuf *b;
708 run(vconn_recv_block(vconn, &b), "vconn_recv");
4f564f8d 709 ofp_print(stderr, b->data, b->size, verbosity + 2);
0caf6bde
BP
710 ofpbuf_delete(b);
711 }
712}
713
714static void
715do_monitor(int argc, char *argv[])
064af421
BP
716{
717 struct vconn *vconn;
718
719 open_vconn(argv[1], &vconn);
720 if (argc > 2) {
721 int miss_send_len = atoi(argv[2]);
064af421
BP
722 struct ofp_switch_config *osc;
723 struct ofpbuf *buf;
724
725 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
064af421 726 osc->miss_send_len = htons(miss_send_len);
88ca35ee 727 transact_noreply(vconn, buf);
064af421 728 }
0caf6bde
BP
729 monitor_vconn(vconn);
730}
731
732static void
733do_snoop(int argc OVS_UNUSED, char *argv[])
734{
735 struct vconn *vconn;
736
737 open_vconn__(argv[1], "snoop", &vconn);
738 monitor_vconn(vconn);
064af421
BP
739}
740
741static void
abaad8cf 742do_dump_ports(int argc, char *argv[])
064af421 743{
abaad8cf
JP
744 struct ofp_port_stats_request *req;
745 struct ofpbuf *request;
746 uint16_t port;
747
748 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
749 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
750 req->port_no = htons(port);
751 dump_stats_transaction(argv[1], request);
064af421
BP
752}
753
754static void
c69ee87c 755do_probe(int argc OVS_UNUSED, char *argv[])
064af421
BP
756{
757 struct ofpbuf *request;
758 struct vconn *vconn;
759 struct ofpbuf *reply;
760
761 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
762 open_vconn(argv[1], &vconn);
763 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
764 if (reply->size != sizeof(struct ofp_header)) {
765 ovs_fatal(0, "reply does not match request");
766 }
767 ofpbuf_delete(reply);
768 vconn_close(vconn);
769}
770
771static void
c69ee87c 772do_mod_port(int argc OVS_UNUSED, char *argv[])
064af421 773{
064af421 774 struct ofp_port_mod *opm;
0df0e81d
BP
775 struct ofp_phy_port opp;
776 struct ofpbuf *request;
064af421 777 struct vconn *vconn;
064af421 778
0df0e81d 779 fetch_ofp_phy_port(argv[1], argv[2], &opp);
064af421
BP
780
781 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
0df0e81d
BP
782 opm->port_no = opp.port_no;
783 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
064af421
BP
784 opm->config = htonl(0);
785 opm->mask = htonl(0);
786 opm->advertise = htonl(0);
787
f4866781 788 if (!strcasecmp(argv[3], "up")) {
064af421 789 opm->mask |= htonl(OFPPC_PORT_DOWN);
f4866781 790 } else if (!strcasecmp(argv[3], "down")) {
064af421
BP
791 opm->mask |= htonl(OFPPC_PORT_DOWN);
792 opm->config |= htonl(OFPPC_PORT_DOWN);
f4866781 793 } else if (!strcasecmp(argv[3], "flood")) {
064af421 794 opm->mask |= htonl(OFPPC_NO_FLOOD);
f4866781 795 } else if (!strcasecmp(argv[3], "noflood")) {
064af421
BP
796 opm->mask |= htonl(OFPPC_NO_FLOOD);
797 opm->config |= htonl(OFPPC_NO_FLOOD);
798 } else {
799 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
800 }
801
0df0e81d 802 open_vconn(argv[1], &vconn);
88ca35ee 803 transact_noreply(vconn, request);
064af421
BP
804 vconn_close(vconn);
805}
806
807static void
675febfa 808do_ping(int argc, char *argv[])
064af421
BP
809{
810 size_t max_payload = 65535 - sizeof(struct ofp_header);
811 unsigned int payload;
812 struct vconn *vconn;
813 int i;
814
815 payload = argc > 2 ? atoi(argv[2]) : 64;
816 if (payload > max_payload) {
817 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
818 }
819
820 open_vconn(argv[1], &vconn);
821 for (i = 0; i < 10; i++) {
822 struct timeval start, end;
823 struct ofpbuf *request, *reply;
824 struct ofp_header *rq_hdr, *rpy_hdr;
825
826 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
827 OFPT_ECHO_REQUEST, &request);
828 random_bytes(rq_hdr + 1, payload);
829
279c9e03 830 xgettimeofday(&start);
064af421 831 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
279c9e03 832 xgettimeofday(&end);
064af421
BP
833
834 rpy_hdr = reply->data;
835 if (reply->size != request->size
836 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
837 || rpy_hdr->xid != rq_hdr->xid
838 || rpy_hdr->type != OFPT_ECHO_REPLY) {
839 printf("Reply does not match request. Request:\n");
4f564f8d 840 ofp_print(stdout, request, request->size, verbosity + 2);
064af421 841 printf("Reply:\n");
4f564f8d 842 ofp_print(stdout, reply, reply->size, verbosity + 2);
064af421 843 }
2886875a 844 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
44381c1b 845 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
064af421
BP
846 (1000*(double)(end.tv_sec - start.tv_sec))
847 + (.001*(end.tv_usec - start.tv_usec)));
848 ofpbuf_delete(request);
849 ofpbuf_delete(reply);
850 }
851 vconn_close(vconn);
852}
853
854static void
c69ee87c 855do_benchmark(int argc OVS_UNUSED, char *argv[])
064af421
BP
856{
857 size_t max_payload = 65535 - sizeof(struct ofp_header);
858 struct timeval start, end;
859 unsigned int payload_size, message_size;
860 struct vconn *vconn;
861 double duration;
862 int count;
863 int i;
864
865 payload_size = atoi(argv[2]);
866 if (payload_size > max_payload) {
867 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
868 }
869 message_size = sizeof(struct ofp_header) + payload_size;
870
871 count = atoi(argv[3]);
872
873 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
874 count, message_size, count * message_size);
875
876 open_vconn(argv[1], &vconn);
279c9e03 877 xgettimeofday(&start);
064af421
BP
878 for (i = 0; i < count; i++) {
879 struct ofpbuf *request, *reply;
880 struct ofp_header *rq_hdr;
881
882 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
883 memset(rq_hdr + 1, 0, payload_size);
884 run(vconn_transact(vconn, request, &reply), "transact");
885 ofpbuf_delete(reply);
886 }
279c9e03 887 xgettimeofday(&end);
064af421
BP
888 vconn_close(vconn);
889
890 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
891 + (.001*(end.tv_usec - start.tv_usec)));
892 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
893 duration, count / (duration / 1000.0),
894 count * message_size / (duration / 1000.0));
895}
896
09246b99
BP
897static void
898do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
899{
900 usage();
901}
902\f
0199c526
BP
903/* replace-flows and diff-flows commands. */
904
905/* A flow table entry, possibly with two different versions. */
906struct fte {
907 struct cls_rule rule; /* Within a "struct classifier". */
908 struct fte_version *versions[2];
909};
910
911/* One version of a Flow Table Entry. */
912struct fte_version {
913 ovs_be64 cookie;
914 uint16_t idle_timeout;
915 uint16_t hard_timeout;
916 uint16_t flags;
917 union ofp_action *actions;
918 size_t n_actions;
919};
920
921/* Frees 'version' and the data that it owns. */
922static void
923fte_version_free(struct fte_version *version)
924{
925 if (version) {
926 free(version->actions);
927 free(version);
928 }
929}
930
931/* Returns true if 'a' and 'b' are the same, false if they differ.
932 *
933 * Ignores differences in 'flags' because there's no way to retrieve flags from
934 * an OpenFlow switch. We have to assume that they are the same. */
935static bool
936fte_version_equals(const struct fte_version *a, const struct fte_version *b)
937{
938 return (a->cookie == b->cookie
939 && a->idle_timeout == b->idle_timeout
940 && a->hard_timeout == b->hard_timeout
941 && a->n_actions == b->n_actions
942 && !memcmp(a->actions, b->actions,
943 a->n_actions * sizeof *a->actions));
944}
945
946/* Prints 'version' on stdout. Expects the caller to have printed the rule
947 * associated with the version. */
948static void
949fte_version_print(const struct fte_version *version)
950{
951 struct ds s;
952
953 if (version->cookie != htonll(0)) {
954 printf(" cookie=0x%"PRIx64, ntohll(version->cookie));
955 }
956 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
957 printf(" idle_timeout=%"PRIu16, version->idle_timeout);
958 }
959 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
960 printf(" hard_timeout=%"PRIu16, version->hard_timeout);
961 }
962
963 ds_init(&s);
964 ofp_print_actions(&s, (const struct ofp_action_header *) version->actions,
965 version->n_actions * sizeof *version->actions);
966 printf(" %s\n", ds_cstr(&s));
967 ds_destroy(&s);
968}
969
970static struct fte *
971fte_from_cls_rule(const struct cls_rule *cls_rule)
972{
973 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
974}
975
976/* Frees 'fte' and its versions. */
977static void
978fte_free(struct fte *fte)
979{
980 if (fte) {
981 fte_version_free(fte->versions[0]);
982 fte_version_free(fte->versions[1]);
983 free(fte);
984 }
985}
986
987/* Frees all of the FTEs within 'cls'. */
988static void
989fte_free_all(struct classifier *cls)
990{
991 struct cls_cursor cursor;
992 struct fte *fte, *next;
993
994 cls_cursor_init(&cursor, cls, NULL);
995 CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
996 classifier_remove(cls, &fte->rule);
997 fte_free(fte);
998 }
999}
1000
1001/* Searches 'cls' for an FTE matching 'rule', inserting a new one if
1002 * necessary. Sets 'version' as the version of that rule with the given
1003 * 'index', replacing any existing version, if any.
1004 *
1005 * Takes ownership of 'version'. */
1006static void
1007fte_insert(struct classifier *cls, const struct cls_rule *rule,
1008 struct fte_version *version, int index)
1009{
1010 struct fte *old, *fte;
1011
1012 fte = xzalloc(sizeof *fte);
1013 fte->rule = *rule;
1014 fte->versions[index] = version;
1015
1016 old = fte_from_cls_rule(classifier_insert(cls, &fte->rule));
1017 if (old) {
1018 fte_version_free(old->versions[index]);
1019 fte->versions[!index] = old->versions[!index];
1020 free(old);
1021 }
1022}
1023
1024/* Reads the flows in 'filename' as flow table entries in 'cls' for the version
1025 * with the specified 'index'. Returns the minimum flow format required to
1026 * represent the flows that were read. */
1027static enum nx_flow_format
1028read_flows_from_file(const char *filename, struct classifier *cls, int index)
1029{
1030 enum nx_flow_format min_flow_format;
1031 struct ds s;
1032 FILE *file;
1033
1034 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1035 if (file == NULL) {
1036 ovs_fatal(errno, "%s: open", filename);
1037 }
1038
1039 ds_init(&s);
1040 min_flow_format = NXFF_OPENFLOW10;
1041 while (!ds_get_preprocessed_line(&s, file)) {
1042 struct fte_version *version;
1043 enum nx_flow_format min_ff;
1044 struct ofpbuf actions;
1045 struct flow_mod fm;
1046 uint8_t table_idx;
1047
1048 ofpbuf_init(&actions, 64);
1049 parse_ofp_str(&fm, &table_idx, &actions, ds_cstr(&s));
1050
1051 version = xmalloc(sizeof *version);
1052 version->cookie = fm.cookie;
1053 version->idle_timeout = fm.idle_timeout;
1054 version->hard_timeout = fm.hard_timeout;
1055 version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG);
1056 version->n_actions = actions.size / sizeof *version->actions;
1057 version->actions = ofpbuf_steal_data(&actions);
1058
1059 min_ff = ofputil_min_flow_format(&fm.cr, true, fm.cookie);
1060 min_flow_format = MAX(min_flow_format, min_ff);
1061 check_final_format_for_flow_mod(min_flow_format);
1062
1063 fte_insert(cls, &fm.cr, version, index);
1064 }
1065 ds_destroy(&s);
1066
1067 if (file != stdin) {
1068 fclose(file);
1069 }
1070
1071 return min_flow_format;
1072}
1073
1074/* Reads the OpenFlow flow table from 'vconn', which has currently active flow
1075 * format 'flow_format', and adds them as flow table entries in 'cls' for the
1076 * version with the specified 'index'. */
1077static void
1078read_flows_from_switch(struct vconn *vconn, enum nx_flow_format flow_format,
1079 struct classifier *cls, int index)
1080{
1081 struct flow_stats_request fsr;
1082 struct ofpbuf *request;
1083 ovs_be32 send_xid;
1084 bool done;
1085
1086 fsr.aggregate = false;
1087 cls_rule_init_catchall(&fsr.match, 0);
1088 fsr.out_port = OFPP_NONE;
1089 fsr.table_id = 0xff;
1090 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
1091 send_xid = ((struct ofp_header *) request->data)->xid;
1092 send_openflow_buffer(vconn, request);
1093
1094 done = false;
1095 while (!done) {
1096 ovs_be32 recv_xid;
1097 struct ofpbuf *reply;
1098
1099 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
1100 recv_xid = ((struct ofp_header *) reply->data)->xid;
1101 if (send_xid == recv_xid) {
1102 const struct ofputil_msg_type *type;
1103 const struct ofp_stats_reply *osr;
1104 enum ofputil_msg_code code;
1105
1106 ofputil_decode_msg_type(reply->data, &type);
1107 code = ofputil_msg_type_code(type);
1108 if (code != OFPUTIL_OFPST_FLOW_REPLY &&
1109 code != OFPUTIL_NXST_FLOW_REPLY) {
1110 ovs_fatal(0, "received bad reply: %s",
1111 ofp_to_string(reply->data, reply->size,
1112 verbosity + 1));
1113 }
1114
1115 osr = reply->data;
1116 if (!(osr->flags & htons(OFPSF_REPLY_MORE))) {
1117 done = true;
1118 }
1119
1120 for (;;) {
1121 struct fte_version *version;
1122 struct ofputil_flow_stats fs;
1123 int retval;
1124
1125 retval = ofputil_decode_flow_stats_reply(&fs, reply,
1126 flow_format);
1127 if (retval) {
1128 if (retval != EOF) {
1129 ovs_fatal(0, "parse error in reply");
1130 }
1131 break;
1132 }
1133
1134 version = xmalloc(sizeof *version);
1135 version->cookie = fs.cookie;
1136 version->idle_timeout = fs.idle_timeout;
1137 version->hard_timeout = fs.hard_timeout;
1138 version->flags = 0;
1139 version->n_actions = fs.n_actions;
1140 version->actions = xmemdup(fs.actions,
1141 fs.n_actions * sizeof *fs.actions);
1142
1143 fte_insert(cls, &fs.rule, version, index);
1144 }
0199c526
BP
1145 } else {
1146 VLOG_DBG("received reply with xid %08"PRIx32" "
1147 "!= expected %08"PRIx32, recv_xid, send_xid);
1148 }
1149 ofpbuf_delete(reply);
1150 }
1151}
1152
1153static void
1154fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
1155 enum nx_flow_format flow_format, struct list *packets)
1156{
1157 const struct fte_version *version = fte->versions[index];
1158 struct flow_mod fm;
1159 struct ofpbuf *ofm;
1160
1161 fm.cr = fte->rule;
1162 fm.cookie = version->cookie;
1163 fm.command = command;
1164 fm.idle_timeout = version->idle_timeout;
1165 fm.hard_timeout = version->hard_timeout;
1166 fm.buffer_id = UINT32_MAX;
1167 fm.out_port = OFPP_NONE;
1168 fm.flags = version->flags;
1169 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
1170 command == OFPFC_MODIFY_STRICT) {
1171 fm.actions = version->actions;
1172 fm.n_actions = version->n_actions;
1173 } else {
1174 fm.actions = NULL;
1175 fm.n_actions = 0;
1176 }
1177
1178 ofm = ofputil_encode_flow_mod(&fm, flow_format);
1179 list_push_back(packets, &ofm->list_node);
1180}
1181
1182static void
1183do_replace_flows(int argc OVS_UNUSED, char *argv[])
1184{
1185 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
1186 enum nx_flow_format min_flow_format, flow_format;
1187 struct cls_cursor cursor;
1188 struct classifier cls;
1189 struct list requests;
1190 struct vconn *vconn;
1191 struct fte *fte;
1192
1193 classifier_init(&cls);
1194 min_flow_format = read_flows_from_file(argv[2], &cls, FILE_IDX);
1195
1196 open_vconn(argv[1], &vconn);
1197 flow_format = negotiate_highest_flow_format(vconn, min_flow_format);
1198 read_flows_from_switch(vconn, flow_format, &cls, SWITCH_IDX);
1199
1200 list_init(&requests);
1201
1202 /* Delete flows that exist on the switch but not in the file. */
1203 cls_cursor_init(&cursor, &cls, NULL);
1204 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1205 struct fte_version *file_ver = fte->versions[FILE_IDX];
1206 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1207
1208 if (sw_ver && !file_ver) {
1209 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
1210 flow_format, &requests);
1211 }
1212 }
1213
1214 /* Add flows that exist in the file but not on the switch.
1215 * Update flows that exist in both places but differ. */
1216 cls_cursor_init(&cursor, &cls, NULL);
1217 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1218 struct fte_version *file_ver = fte->versions[FILE_IDX];
1219 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
1220
1221 if (file_ver && (!sw_ver || !fte_version_equals(sw_ver, file_ver))) {
1222 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, flow_format,
1223 &requests);
1224 }
1225 }
1226 transact_multiple_noreply(vconn, &requests);
1227 vconn_close(vconn);
1228
1229 fte_free_all(&cls);
1230}
1231
1232static void
1233read_flows_from_source(const char *source, struct classifier *cls, int index)
1234{
1235 struct stat s;
1236
1237 if (source[0] == '/' || source[0] == '.'
1238 || (!strchr(source, ':') && !stat(source, &s))) {
1239 read_flows_from_file(source, cls, index);
1240 } else {
1241 enum nx_flow_format flow_format;
1242 struct vconn *vconn;
1243
1244 open_vconn(source, &vconn);
1245 flow_format = negotiate_highest_flow_format(vconn, NXFF_OPENFLOW10);
1246 read_flows_from_switch(vconn, flow_format, cls, index);
1247 vconn_close(vconn);
1248 }
1249}
1250
1251static void
1252do_diff_flows(int argc OVS_UNUSED, char *argv[])
1253{
1254 bool differences = false;
1255 struct cls_cursor cursor;
1256 struct classifier cls;
1257 struct fte *fte;
1258
1259 classifier_init(&cls);
1260 read_flows_from_source(argv[1], &cls, 0);
1261 read_flows_from_source(argv[2], &cls, 1);
1262
1263 cls_cursor_init(&cursor, &cls, NULL);
1264 CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
1265 struct fte_version *a = fte->versions[0];
1266 struct fte_version *b = fte->versions[1];
1267
1268 if (!a || !b || !fte_version_equals(a, b)) {
1269 char *rule_s = cls_rule_to_string(&fte->rule);
1270 if (a) {
1271 printf("-%s", rule_s);
1272 fte_version_print(a);
1273 }
1274 if (b) {
1275 printf("+%s", rule_s);
1276 fte_version_print(b);
1277 }
1278 free(rule_s);
1279
1280 differences = true;
1281 }
1282 }
1283
1284 fte_free_all(&cls);
1285
1286 if (differences) {
1287 exit(2);
1288 }
1289}
1290\f
09246b99
BP
1291/* Undocumented commands for unit testing. */
1292
770f1f66
BP
1293static void
1294print_packet_list(struct list *packets)
1295{
1296 struct ofpbuf *packet, *next;
1297
1298 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
1299 ofp_print(stdout, packet->data, packet->size, verbosity);
1300 list_remove(&packet->list_node);
1301 ofpbuf_delete(packet);
1302 }
1303}
1304
1305/* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
1306 * it back to stdout. */
1307static void
1308do_parse_flow(int argc OVS_UNUSED, char *argv[])
1309{
1310 enum nx_flow_format flow_format;
1311 struct list packets;
1312
1313 flow_format = NXFF_OPENFLOW10;
1314 if (preferred_flow_format > 0) {
1315 flow_format = preferred_flow_format;
1316 }
1317
1318 list_init(&packets);
1319 parse_ofp_flow_mod_str(&packets, &flow_format, argv[1], OFPFC_ADD);
1320 print_packet_list(&packets);
1321}
1322
fec00620
BP
1323/* "parse-flows FILENAME": reads the named file as a sequence of flows (like
1324 * add-flows) and prints each of the flows back to stdout. */
0e581146
BP
1325static void
1326do_parse_flows(int argc OVS_UNUSED, char *argv[])
1327{
88ca35ee
BP
1328 enum nx_flow_format flow_format;
1329 struct list packets;
0e581146
BP
1330 FILE *file;
1331
1332 file = fopen(argv[1], "r");
1333 if (file == NULL) {
1334 ovs_fatal(errno, "%s: open", argv[2]);
1335 }
1336
88ca35ee
BP
1337 flow_format = NXFF_OPENFLOW10;
1338 if (preferred_flow_format > 0) {
1339 flow_format = preferred_flow_format;
1340 }
1341
770f1f66 1342 list_init(&packets);
02c5617b 1343 while (parse_ofp_flow_mod_file(&packets, &flow_format, file, OFPFC_ADD)) {
770f1f66 1344 print_packet_list(&packets);
0e581146
BP
1345 }
1346 fclose(file);
1347}
1348
fec00620
BP
1349/* "parse-nx-match": reads a series of nx_match specifications as strings from
1350 * stdin, does some internal fussing with them, and then prints them back as
1351 * strings on stdout. */
064af421 1352static void
09246b99
BP
1353do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
1354{
1355 struct ds in;
1356
1357 ds_init(&in);
1358 while (!ds_get_line(&in, stdin)) {
1359 struct ofpbuf nx_match;
1360 struct cls_rule rule;
1361 int match_len;
1362 int error;
1363 char *s;
1364
1365 /* Delete comments, skip blank lines. */
1366 s = ds_cstr(&in);
1367 if (*s == '#') {
1368 puts(s);
1369 continue;
1370 }
1371 if (strchr(s, '#')) {
1372 *strchr(s, '#') = '\0';
1373 }
1374 if (s[strspn(s, " ")] == '\0') {
1375 putchar('\n');
1376 continue;
1377 }
1378
1379 /* Convert string to nx_match. */
1380 ofpbuf_init(&nx_match, 0);
1381 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1382
1383 /* Convert nx_match to cls_rule. */
1384 error = nx_pull_match(&nx_match, match_len, 0, &rule);
1385 if (!error) {
1386 char *out;
1387
1388 /* Convert cls_rule back to nx_match. */
1389 ofpbuf_uninit(&nx_match);
1390 ofpbuf_init(&nx_match, 0);
1391 match_len = nx_put_match(&nx_match, &rule);
1392
1393 /* Convert nx_match to string. */
1394 out = nx_match_to_string(nx_match.data, match_len);
1395 puts(out);
1396 free(out);
1397 } else {
1398 printf("nx_pull_match() returned error %x\n", error);
1399 }
1400
1401 ofpbuf_uninit(&nx_match);
1402 }
1403 ds_destroy(&in);
064af421
BP
1404}
1405
fec00620
BP
1406/* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1407 * binary data, interpreting them as an OpenFlow message, and prints the
1408 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1409static void
1410do_ofp_print(int argc, char *argv[])
1411{
1412 struct ofpbuf packet;
1413
1414 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1415 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1416 ovs_fatal(0, "trailing garbage following hex bytes");
1417 }
1418 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1419 ofpbuf_uninit(&packet);
1420}
1421
675febfa 1422static const struct command all_commands[] = {
064af421 1423 { "show", 1, 1, do_show },
7f1089b5 1424 { "monitor", 1, 2, do_monitor },
0caf6bde 1425 { "snoop", 1, 1, do_snoop },
064af421
BP
1426 { "dump-desc", 1, 1, do_dump_desc },
1427 { "dump-tables", 1, 1, do_dump_tables },
1428 { "dump-flows", 1, 2, do_dump_flows },
1429 { "dump-aggregate", 1, 2, do_dump_aggregate },
d2805da2 1430 { "queue-stats", 1, 3, do_queue_stats },
064af421
BP
1431 { "add-flow", 2, 2, do_add_flow },
1432 { "add-flows", 2, 2, do_add_flows },
1433 { "mod-flows", 2, 2, do_mod_flows },
1434 { "del-flows", 1, 2, do_del_flows },
0199c526
BP
1435 { "replace-flows", 2, 2, do_replace_flows },
1436 { "diff-flows", 2, 2, do_diff_flows },
abaad8cf 1437 { "dump-ports", 1, 2, do_dump_ports },
064af421
BP
1438 { "mod-port", 3, 3, do_mod_port },
1439 { "probe", 1, 1, do_probe },
1440 { "ping", 1, 2, do_ping },
1441 { "benchmark", 3, 3, do_benchmark },
064af421 1442 { "help", 0, INT_MAX, do_help },
09246b99
BP
1443
1444 /* Undocumented commands for testing. */
770f1f66 1445 { "parse-flow", 1, 1, do_parse_flow },
09246b99
BP
1446 { "parse-flows", 1, 1, do_parse_flows },
1447 { "parse-nx-match", 0, 0, do_parse_nx_match },
fec00620 1448 { "ofp-print", 1, 2, do_ofp_print },
09246b99 1449
064af421
BP
1450 { NULL, 0, 0, NULL },
1451};