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