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