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