]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-ofctl.c
meta-flow: Minor refactoring.
[mirror_ovs.git] / utilities / ovs-ofctl.c
CommitLineData
064af421 1/*
18080541 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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>
2b07c8b1 18#include <ctype.h>
064af421
BP
19#include <errno.h>
20#include <getopt.h>
21#include <inttypes.h>
bd6b7545 22#include <sys/socket.h>
064af421 23#include <net/if.h>
064af421 24#include <signal.h>
064af421
BP
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
71d1b763 28#include <fcntl.h>
064af421
BP
29#include <sys/stat.h>
30#include <sys/time.h>
31
10a24935 32#include "byte-order.h"
09246b99 33#include "classifier.h"
064af421 34#include "command-line.h"
1eb85ef5 35#include "daemon.h"
064af421
BP
36#include "compiler.h"
37#include "dirs.h"
09246b99 38#include "dynamic-string.h"
8a777cf6 39#include "fatal-signal.h"
09246b99 40#include "nx-match.h"
064af421 41#include "odp-util.h"
f25d0cf3 42#include "ofp-actions.h"
90bf1e07 43#include "ofp-errors.h"
982697a4 44#include "ofp-msgs.h"
f22716dc 45#include "ofp-parse.h"
064af421 46#include "ofp-print.h"
fa37b408 47#include "ofp-util.h"
a53a8efa 48#include "ofp-version-opt.h"
064af421 49#include "ofpbuf.h"
63d347ce 50#include "ofproto/ofproto.h"
064af421
BP
51#include "openflow/nicira-ext.h"
52#include "openflow/openflow.h"
cf62fa4c 53#include "dp-packet.h"
0c3d5fc8 54#include "packets.h"
14249c02 55#include "pcap-file.h"
1eb85ef5 56#include "poll-loop.h"
064af421 57#include "random.h"
fe55ad15 58#include "stream-ssl.h"
c3f25389 59#include "socket-util.h"
064af421 60#include "timeval.h"
1eb85ef5 61#include "unixctl.h"
064af421 62#include "util.h"
4a1f523f 63#include "openvswitch/vconn.h"
e6211adc 64#include "openvswitch/vlog.h"
bdcc5925
BP
65#include "meta-flow.h"
66#include "sort.h"
064af421 67
d98e6007 68VLOG_DEFINE_THIS_MODULE(ofctl);
064af421 69
db5076ee
JR
70/* --bundle: Use OpenFlow 1.4 bundle for making the flow table change atomic.
71 * NOTE: Also the flow mod will use OpenFlow 1.4, so the semantics may be
72 * different (see the comment in parse_options() for details).
73 */
74static bool bundle = false;
75
102ce766
EJ
76/* --strict: Use strict matching for flow mod commands? Additionally governs
77 * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
78 */
675febfa 79static bool strict;
064af421 80
96989efc 81/* --readd: If true, on replace-flows, re-add even flows that have not changed
c4ea79bf
BP
82 * (to reset flow counters). */
83static bool readd;
84
27527aa0
BP
85/* -F, --flow-format: Allowed protocols. By default, any protocol is
86 * allowed. */
87static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
88ca35ee 88
54834960
EJ
89/* -P, --packet-in-format: Packet IN format to use in monitor and snoop
90 * commands. Either one of NXPIF_* to force a particular packet_in format, or
91 * -1 to let ovs-ofctl choose the default. */
92static int preferred_packet_in_format = -1;
93
4f564f8d
BP
94/* -m, --more: Additional verbosity for ofp-print functions. */
95static int verbosity;
96
0c9560b7
BP
97/* --timestamp: Print a timestamp before each received packet on "monitor" and
98 * "snoop" command? */
99static bool timestamp;
100
19945013
BP
101/* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
102 commands. */
103static char *unixctl_path;
104
bdcc5925
BP
105/* --sort, --rsort: Sort order. */
106enum sort_order { SORT_ASC, SORT_DESC };
107struct sort_criterion {
108 const struct mf_field *field; /* NULL means to sort by priority. */
109 enum sort_order order;
110};
111static struct sort_criterion *criteria;
112static size_t n_criteria, allocated_criteria;
113
5f383751 114static const struct ovs_cmdl_command *get_all_commands(void);
064af421 115
cab50449 116OVS_NO_RETURN static void usage(void);
675febfa 117static void parse_options(int argc, char *argv[]);
064af421 118
bdcc5925
BP
119static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
120 struct ofpbuf **replyp,
121 struct ofputil_flow_stats *,
122 struct ofpbuf *ofpacts);
675febfa
BP
123int
124main(int argc, char *argv[])
064af421 125{
1636c761 126 struct ovs_cmdl_context ctx = { .argc = 0, };
064af421 127 set_program_name(argv[0]);
5e3ee29d 128 service_start(&argc, &argv);
675febfa 129 parse_options(argc, argv);
8a777cf6 130 fatal_ignore_sigpipe();
1636c761
RB
131 ctx.argc = argc - optind;
132 ctx.argv = argv + optind;
133 ovs_cmdl_run_command(&ctx, get_all_commands());
064af421
BP
134 return 0;
135}
136
bdcc5925
BP
137static void
138add_sort_criterion(enum sort_order order, const char *field)
139{
140 struct sort_criterion *sc;
141
142 if (n_criteria >= allocated_criteria) {
143 criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
144 }
145
146 sc = &criteria[n_criteria++];
147 if (!field || !strcasecmp(field, "priority")) {
148 sc->field = NULL;
149 } else {
150 sc->field = mf_from_name(field);
151 if (!sc->field) {
152 ovs_fatal(0, "%s: unknown field name", field);
153 }
154 }
155 sc->order = order;
156}
157
064af421 158static void
675febfa 159parse_options(int argc, char *argv[])
064af421
BP
160{
161 enum {
87c84891 162 OPT_STRICT = UCHAR_MAX + 1,
c4ea79bf 163 OPT_READD,
0c9560b7 164 OPT_TIMESTAMP,
bdcc5925
BP
165 OPT_SORT,
166 OPT_RSORT,
19945013 167 OPT_UNIXCTL,
db5076ee 168 OPT_BUNDLE,
1eb85ef5 169 DAEMON_OPTION_ENUMS,
a53a8efa 170 OFP_VERSION_OPTION_ENUMS,
87c84891 171 VLOG_OPTION_ENUMS
064af421 172 };
07fc4ed3 173 static const struct option long_options[] = {
e3c17733
BP
174 {"timeout", required_argument, NULL, 't'},
175 {"strict", no_argument, NULL, OPT_STRICT},
c4ea79bf 176 {"readd", no_argument, NULL, OPT_READD},
e3c17733 177 {"flow-format", required_argument, NULL, 'F'},
54834960 178 {"packet-in-format", required_argument, NULL, 'P'},
e3c17733 179 {"more", no_argument, NULL, 'm'},
0c9560b7 180 {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
bdcc5925
BP
181 {"sort", optional_argument, NULL, OPT_SORT},
182 {"rsort", optional_argument, NULL, OPT_RSORT},
19945013 183 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
e3c17733 184 {"help", no_argument, NULL, 'h'},
66fa2c88 185 {"option", no_argument, NULL, 'o'},
db5076ee 186 {"bundle", no_argument, NULL, OPT_BUNDLE},
1eb85ef5 187 DAEMON_LONG_OPTIONS,
a53a8efa 188 OFP_VERSION_LONG_OPTIONS,
87c84891 189 VLOG_LONG_OPTIONS,
bf8f2167 190 STREAM_SSL_LONG_OPTIONS,
e3c17733 191 {NULL, 0, NULL, 0},
064af421 192 };
5f383751 193 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
37923ac7
BP
194 uint32_t versions;
195 enum ofputil_protocol version_protocols;
064af421 196
6dc53744
BP
197 /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is
198 * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
199 * requests, but those requests have subtly different semantics in
200 * different OpenFlow versions. For example:
201 *
202 * - In OpenFlow 1.0, a "mod-flow" operation that does not find any
203 * existing flow to modify adds a new flow.
204 *
205 * - In OpenFlow 1.1, a "mod-flow" operation that does not find any
206 * existing flow to modify adds a new flow, but only if the mod-flow
207 * did not match on the flow cookie.
208 *
209 * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
210 * new flow.
211 */
212 set_allowed_ofp_versions("OpenFlow10");
213
064af421
BP
214 for (;;) {
215 unsigned long int timeout;
216 int c;
217
218 c = getopt_long(argc, argv, short_options, long_options, NULL);
219 if (c == -1) {
220 break;
221 }
222
223 switch (c) {
224 case 't':
225 timeout = strtoul(optarg, NULL, 10);
226 if (timeout <= 0) {
227 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
228 optarg);
229 } else {
230 time_alarm(timeout);
231 }
232 break;
233
88ca35ee 234 case 'F':
27527aa0
BP
235 allowed_protocols = ofputil_protocols_from_string(optarg);
236 if (!allowed_protocols) {
237 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
88ca35ee
BP
238 }
239 break;
240
54834960
EJ
241 case 'P':
242 preferred_packet_in_format =
243 ofputil_packet_in_format_from_string(optarg);
244 if (preferred_packet_in_format < 0) {
245 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
246 }
247 break;
248
4f564f8d
BP
249 case 'm':
250 verbosity++;
251 break;
252
064af421
BP
253 case 'h':
254 usage();
255
66fa2c88 256 case 'o':
5f383751 257 ovs_cmdl_print_options(long_options);
66fa2c88
AW
258 exit(EXIT_SUCCESS);
259
db5076ee
JR
260 case OPT_BUNDLE:
261 bundle = true;
262 break;
263
064af421 264 case OPT_STRICT:
675febfa 265 strict = true;
064af421
BP
266 break;
267
c4ea79bf
BP
268 case OPT_READD:
269 readd = true;
270 break;
271
0c9560b7
BP
272 case OPT_TIMESTAMP:
273 timestamp = true;
274 break;
275
bdcc5925
BP
276 case OPT_SORT:
277 add_sort_criterion(SORT_ASC, optarg);
278 break;
279
280 case OPT_RSORT:
281 add_sort_criterion(SORT_DESC, optarg);
282 break;
283
19945013
BP
284 case OPT_UNIXCTL:
285 unixctl_path = optarg;
286 break;
287
1eb85ef5 288 DAEMON_OPTION_HANDLERS
a53a8efa 289 OFP_VERSION_OPTION_HANDLERS
87c84891 290 VLOG_OPTION_HANDLERS
fe55ad15 291 STREAM_SSL_OPTION_HANDLERS
064af421
BP
292
293 case '?':
294 exit(EXIT_FAILURE);
295
296 default:
297 abort();
298 }
299 }
bdcc5925
BP
300
301 if (n_criteria) {
302 /* Always do a final sort pass based on priority. */
303 add_sort_criterion(SORT_DESC, "priority");
304 }
305
064af421 306 free(short_options);
37923ac7 307
db5076ee
JR
308 /* Implicit OpenFlow 1.4 with the '--bundle' option. */
309 if (bundle) {
310 /* Add implicit allowance for OpenFlow 1.4. */
311 add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
312 OFPUTIL_P_OF14_OXM));
313 }
37923ac7
BP
314 versions = get_allowed_ofp_versions();
315 version_protocols = ofputil_protocols_from_version_bitmap(versions);
316 if (!(allowed_protocols & version_protocols)) {
317 char *protocols = ofputil_protocols_to_string(allowed_protocols);
318 struct ds version_s = DS_EMPTY_INITIALIZER;
319
320 ofputil_format_version_bitmap_names(&version_s, versions);
321 ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
322 "any of the enabled flow formats (%s). (Use -O to enable "
323 "additional OpenFlow versions or -F to enable additional "
324 "flow formats.)", ds_cstr(&version_s), protocols);
325 }
326 allowed_protocols &= version_protocols;
327 mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
328 allowed_protocols));
064af421
BP
329}
330
331static void
332usage(void)
333{
334 printf("%s: OpenFlow switch management utility\n"
335 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
336 "\nFor OpenFlow switches:\n"
337 " show SWITCH show OpenFlow information\n"
064af421
BP
338 " dump-desc SWITCH print switch description\n"
339 " dump-tables SWITCH print table stats\n"
5deff5aa 340 " dump-table-features SWITCH print table features\n"
03c72922 341 " dump-table-desc SWITCH print table description (OF1.4+)\n"
064af421 342 " mod-port SWITCH IFACE ACT modify port behavior\n"
918f2b82 343 " mod-table SWITCH MOD modify flow table behavior\n"
82c22d34
BP
344 " OF1.1/1.2 MOD: controller, continue, drop\n"
345 " OF1.4+ MOD: evict, noevict\n"
7257b535
BP
346 " get-frags SWITCH print fragment handling behavior\n"
347 " set-frags SWITCH FRAG_MODE set fragment handling behavior\n"
82c22d34 348 " FRAG_MODE: normal, drop, reassemble, nx-match\n"
abaad8cf 349 " dump-ports SWITCH [PORT] print port statistics\n"
70ae4f93 350 " dump-ports-desc SWITCH [PORT] print port descriptions\n"
064af421
BP
351 " dump-flows SWITCH print all flow entries\n"
352 " dump-flows SWITCH FLOW print matching FLOWs\n"
353 " dump-aggregate SWITCH print aggregate flow statistics\n"
354 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
d2805da2 355 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
064af421
BP
356 " add-flow SWITCH FLOW add flow described by FLOW\n"
357 " add-flows SWITCH FILE add flows from FILE\n"
358 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
359 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
5ff660c6 360 " replace-flows SWITCH FILE replace flows with those in FILE\n"
1dac118c 361 " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n"
0c3d5fc8
BP
362 " packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
363 " execute ACTIONS on PACKET\n"
2b07c8b1 364 " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
1dac118c
BP
365 " print packets received from SWITCH\n"
366 " snoop SWITCH snoop on SWITCH and its controller\n"
7395c052 367 " add-group SWITCH GROUP add group described by GROUP\n"
bdcca615 368 " add-groups SWITCH FILE add group from FILE\n"
7395c052
NZ
369 " mod-group SWITCH GROUP modify specific group\n"
370 " del-groups SWITCH [GROUP] delete matching GROUPs\n"
bdbb8426
SH
371 " insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
372 " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
7395c052 373 " dump-group-features SWITCH print group features\n"
19187a71 374 " dump-groups SWITCH [GROUP] print group description\n"
7395c052 375 " dump-group-stats SWITCH [GROUP] print group statistics\n"
e8f9a7bb 376 " queue-get-config SWITCH PORT print queue information for port\n"
3200ed58
JR
377 " add-meter SWITCH METER add meter described by METER\n"
378 " mod-meter SWITCH METER modify specific METER\n"
379 " del-meter SWITCH METER delete METER\n"
380 " del-meters SWITCH delete all meters\n"
381 " dump-meter SWITCH METER print METER configuration\n"
382 " dump-meters SWITCH print all meter configuration\n"
383 " meter-stats SWITCH [METER] print meter statistics\n"
384 " meter-features SWITCH print meter features\n"
6159c531
JG
385 " add-geneve-map SWITCH MAP add Geneve option MAPpings\n"
386 " del-geneve-map SWITCH [MAP] delete Geneve option MAPpings\n"
387 " dump-geneve-map SWITCH print Geneve option mappings\n"
064af421 388 "\nFor OpenFlow switches and controllers:\n"
2daadadd
BP
389 " probe TARGET probe whether TARGET is up\n"
390 " ping TARGET [N] latency of N-byte echos\n"
391 " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n"
1ac0e975
BP
392 "SWITCH or TARGET is an active OpenFlow connection method.\n"
393 "\nOther commands:\n"
f3dd1419
BP
394 " ofp-parse FILE print messages read from FILE\n"
395 " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n",
064af421
BP
396 program_name, program_name);
397 vconn_usage(true, false, false);
1eb85ef5 398 daemon_usage();
a53a8efa 399 ofp_version_usage();
064af421
BP
400 vlog_usage();
401 printf("\nOther options:\n"
402 " --strict use strict match for flow commands\n"
c4ea79bf 403 " --readd replace flows that haven't changed\n"
88ca35ee 404 " -F, --flow-format=FORMAT force particular flow format\n"
54834960 405 " -P, --packet-in-format=FRMT force particular packet in format\n"
4f564f8d 406 " -m, --more be more verbose printing OpenFlow\n"
0c9560b7 407 " --timestamp (monitor, snoop) print timestamps\n"
064af421 408 " -t, --timeout=SECS give up after SECS seconds\n"
bdcc5925
BP
409 " --sort[=field] sort in ascending order\n"
410 " --rsort[=field] sort in descending order\n"
19945013 411 " --unixctl=SOCKET set control socket name\n"
064af421
BP
412 " -h, --help display this help message\n"
413 " -V, --version display version information\n");
414 exit(EXIT_SUCCESS);
415}
416
1eb85ef5
EJ
417static void
418ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
419 const char *argv[] OVS_UNUSED, void *exiting_)
420{
421 bool *exiting = exiting_;
422 *exiting = true;
bde9f75d 423 unixctl_command_reply(conn, NULL);
1eb85ef5
EJ
424}
425
064af421 426static void run(int retval, const char *message, ...)
cab50449 427 OVS_PRINTF_FORMAT(2, 3);
064af421 428
473f65a2
BP
429static void
430run(int retval, const char *message, ...)
064af421
BP
431{
432 if (retval) {
433 va_list args;
434
064af421 435 va_start(args, message);
fcaddd4d 436 ovs_fatal_valist(retval, message, args);
064af421
BP
437 }
438}
439\f
440/* Generic commands. */
441
c3f25389 442static int
1a6f1e2a
JG
443open_vconn_socket(const char *name, struct vconn **vconnp)
444{
445 char *vconn_name = xasprintf("unix:%s", name);
c3f25389
EJ
446 int error;
447
82c8c53c
BP
448 error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
449 vconnp);
c3f25389
EJ
450 if (error && error != ENOENT) {
451 ovs_fatal(0, "%s: failed to open socket (%s)", name,
10a89ef0 452 ovs_strerror(error));
c3f25389 453 }
1a6f1e2a 454 free(vconn_name);
c3f25389
EJ
455
456 return error;
1a6f1e2a
JG
457}
458
4766ce7a
BP
459enum open_target { MGMT, SNOOP };
460
27527aa0 461static enum ofputil_protocol
4766ce7a 462open_vconn__(const char *name, enum open_target target,
0caf6bde 463 struct vconn **vconnp)
064af421 464{
4766ce7a 465 const char *suffix = target == MGMT ? "mgmt" : "snoop";
63d347ce 466 char *datapath_name, *datapath_type, *socket_name;
27527aa0 467 enum ofputil_protocol protocol;
63d347ce 468 char *bridge_path;
27527aa0 469 int ofp_version;
c3f25389 470 int error;
1a6f1e2a 471
4766ce7a 472 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
63d347ce
BP
473
474 ofproto_parse_name(name, &datapath_name, &datapath_type);
4766ce7a 475 socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
63d347ce
BP
476 free(datapath_name);
477 free(datapath_type);
064af421 478
3a27375e 479 if (strchr(name, ':')) {
539b741f 480 run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
a53a8efa 481 "connecting to %s", name);
c3f25389
EJ
482 } else if (!open_vconn_socket(name, vconnp)) {
483 /* Fall Through. */
484 } else if (!open_vconn_socket(bridge_path, vconnp)) {
485 /* Fall Through. */
486 } else if (!open_vconn_socket(socket_name, vconnp)) {
487 /* Fall Through. */
064af421 488 } else {
2c0e6eb4 489 ovs_fatal(0, "%s is not a bridge or a socket", name);
064af421 490 }
1a6f1e2a 491
4766ce7a
BP
492 if (target == SNOOP) {
493 vconn_set_recv_any_version(*vconnp);
494 }
495
1a6f1e2a 496 free(bridge_path);
63d347ce 497 free(socket_name);
27527aa0 498
c3f25389
EJ
499 VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
500 error = vconn_connect_block(*vconnp);
501 if (error) {
502 ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
10a89ef0 503 ovs_strerror(error));
c3f25389
EJ
504 }
505
27527aa0
BP
506 ofp_version = vconn_get_version(*vconnp);
507 protocol = ofputil_protocol_from_ofp_version(ofp_version);
508 if (!protocol) {
509 ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
510 name, ofp_version);
511 }
512 return protocol;
064af421
BP
513}
514
27527aa0 515static enum ofputil_protocol
0caf6bde
BP
516open_vconn(const char *name, struct vconn **vconnp)
517{
4766ce7a 518 return open_vconn__(name, MGMT, vconnp);
0caf6bde
BP
519}
520
064af421
BP
521static void
522send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
523{
064af421
BP
524 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
525}
526
527static void
a53a8efa 528dump_transaction(struct vconn *vconn, struct ofpbuf *request)
064af421 529{
064af421
BP
530 struct ofpbuf *reply;
531
a53a8efa
SH
532 run(vconn_transact(vconn, request, &reply), "talking to %s",
533 vconn_get_name(vconn));
6fd6ed71 534 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
e49190c4 535 ofpbuf_delete(reply);
064af421
BP
536}
537
538static void
982697a4 539dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
064af421
BP
540{
541 struct ofpbuf *request;
a53a8efa
SH
542 struct vconn *vconn;
543
544 open_vconn(vconn_name, &vconn);
545 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
546 dump_transaction(vconn, request);
547 vconn_close(vconn);
064af421
BP
548}
549
550static void
53514387 551dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
064af421 552{
6fd6ed71 553 const struct ofp_header *request_oh = request->data;
982697a4
BP
554 ovs_be32 send_xid = request_oh->xid;
555 enum ofpraw request_raw;
556 enum ofpraw reply_raw;
064af421
BP
557 bool done = false;
558
6fd6ed71 559 ofpraw_decode_partial(&request_raw, request->data, request->size);
982697a4
BP
560 reply_raw = ofpraw_stats_request_to_reply(request_raw,
561 request_oh->version);
562
064af421
BP
563 send_openflow_buffer(vconn, request);
564 while (!done) {
02833365 565 ovs_be32 recv_xid;
064af421
BP
566 struct ofpbuf *reply;
567
568 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
6fd6ed71 569 recv_xid = ((struct ofp_header *) reply->data)->xid;
064af421 570 if (send_xid == recv_xid) {
982697a4 571 enum ofpraw raw;
064af421 572
6fd6ed71 573 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421 574
6fd6ed71 575 ofpraw_decode(&raw, reply->data);
982697a4 576 if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
a76150b1 577 done = true;
982697a4 578 } else if (raw == reply_raw) {
6fd6ed71 579 done = !ofpmp_more(reply->data);
a76150b1
BP
580 } else {
581 ovs_fatal(0, "received bad reply: %s",
6fd6ed71 582 ofp_to_string(reply->data, reply->size,
a76150b1
BP
583 verbosity + 1));
584 }
064af421
BP
585 } else {
586 VLOG_DBG("received reply with xid %08"PRIx32" "
587 "!= expected %08"PRIx32, recv_xid, send_xid);
588 }
589 ofpbuf_delete(reply);
590 }
9abfe557
BP
591}
592
593static void
53514387 594dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
9abfe557 595{
53514387 596 struct ofpbuf *request;
9abfe557
BP
597 struct vconn *vconn;
598
599 open_vconn(vconn_name, &vconn);
53514387
SH
600 request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
601 dump_stats_transaction(vconn, request);
064af421
BP
602 vconn_close(vconn);
603}
604
a8eebd48
BP
605/* Sends all of the 'requests', which should be requests that only have replies
606 * if an error occurs, and waits for them to succeed or fail. If an error does
607 * occur, prints it and exits with an error.
7257b535
BP
608 *
609 * Destroys all of the 'requests'. */
d12513f7 610static void
ca6ba700 611transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
d12513f7 612{
db5076ee 613 struct ofpbuf *reply;
88ca35ee
BP
614
615 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
d12513f7
BP
616 "talking to %s", vconn_get_name(vconn));
617 if (reply) {
6fd6ed71 618 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
d12513f7
BP
619 exit(1);
620 }
621 ofpbuf_delete(reply);
622}
623
db5076ee
JR
624static void
625bundle_error_reporter(const struct ofp_header *oh)
626{
627 ofp_print(stderr, oh, ntohs(oh->length), verbosity + 1);
628 fflush(stderr);
629}
630
631static void
632bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags)
633{
634 run(vconn_bundle_transact(vconn, requests, flags, bundle_error_reporter),
635 "talking to %s", vconn_get_name(vconn));
636}
637
88ca35ee
BP
638/* Sends 'request', which should be a request that only has a reply if an error
639 * occurs, and waits for it to succeed or fail. If an error does occur, prints
7257b535
BP
640 * it and exits with an error.
641 *
642 * Destroys 'request'. */
88ca35ee
BP
643static void
644transact_noreply(struct vconn *vconn, struct ofpbuf *request)
645{
ca6ba700 646 struct ovs_list requests;
88ca35ee
BP
647
648 list_init(&requests);
649 list_push_back(&requests, &request->list_node);
650 transact_multiple_noreply(vconn, &requests);
651}
652
7257b535
BP
653static void
654fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
655{
656 struct ofp_switch_config *config;
7257b535
BP
657 struct ofpbuf *request;
658 struct ofpbuf *reply;
982697a4 659 enum ofptype type;
7257b535 660
a53a8efa
SH
661 request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
662 vconn_get_version(vconn), 0);
7257b535
BP
663 run(vconn_transact(vconn, request, &reply),
664 "talking to %s", vconn_get_name(vconn));
665
982697a4 666 if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
7257b535
BP
667 ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
668 }
669
982697a4 670 config = ofpbuf_pull(reply, sizeof *config);
7257b535 671 *config_ = *config;
828c72d0
BP
672
673 ofpbuf_delete(reply);
7257b535
BP
674}
675
676static void
982697a4 677set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
7257b535 678{
7257b535
BP
679 struct ofpbuf *request;
680
a53a8efa 681 request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
982697a4 682 ofpbuf_put(request, config, sizeof *config);
7257b535
BP
683
684 transact_noreply(vconn, request);
685}
686
064af421 687static void
1636c761 688ofctl_show(struct ovs_cmdl_context *ctx)
064af421 689{
1636c761 690 const char *vconn_name = ctx->argv[1];
fca6d553 691 enum ofp_version version;
ae0e7009
JP
692 struct vconn *vconn;
693 struct ofpbuf *request;
694 struct ofpbuf *reply;
fca6d553 695 bool has_ports;
ae0e7009 696
ae0e7009 697 open_vconn(vconn_name, &vconn);
fca6d553
BP
698 version = vconn_get_version(vconn);
699 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
ae0e7009
JP
700 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
701
fca6d553 702 has_ports = ofputil_switch_features_has_ports(reply);
6fd6ed71 703 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
ae0e7009 704 ofpbuf_delete(reply);
ae0e7009 705
fca6d553 706 if (!has_ports) {
70ae4f93 707 request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
fca6d553 708 dump_stats_transaction(vconn, request);
ae0e7009 709 }
982697a4 710 dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
a53a8efa 711 vconn_close(vconn);
064af421
BP
712}
713
064af421 714static void
1636c761 715ofctl_dump_desc(struct ovs_cmdl_context *ctx)
064af421 716{
1636c761 717 dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
064af421
BP
718}
719
720static void
1636c761 721ofctl_dump_tables(struct ovs_cmdl_context *ctx)
064af421 722{
1636c761 723 dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
064af421
BP
724}
725
5deff5aa 726static void
1636c761 727ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
5deff5aa
AW
728{
729 struct ofpbuf *request;
730 struct vconn *vconn;
731
1636c761 732 open_vconn(ctx->argv[1], &vconn);
5deff5aa 733 request = ofputil_encode_table_features_request(vconn_get_version(vconn));
8811fc0a
BP
734
735 /* The following is similar to dump_trivial_stats_transaction(), but it
736 * maintains the previous 'ofputil_table_features' from one stats reply
737 * message to the next, which allows duplication to be eliminated in the
738 * output across messages. Otherwise the output is much larger and harder
739 * to read, because only 17 or so ofputil_table_features elements fit in a
740 * single 64 kB OpenFlow message and therefore you get a ton of repetition
741 * (every 17th element is printed in full instead of abbreviated). */
742
743 const struct ofp_header *request_oh = request->data;
744 ovs_be32 send_xid = request_oh->xid;
745 bool done = false;
746
747 struct ofputil_table_features prev;
748 int n = 0;
749
750 send_openflow_buffer(vconn, request);
751 while (!done) {
752 ovs_be32 recv_xid;
753 struct ofpbuf *reply;
754
755 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
756 recv_xid = ((struct ofp_header *) reply->data)->xid;
757 if (send_xid == recv_xid) {
758 enum ofptype type;
759 enum ofperr error;
760 error = ofptype_decode(&type, reply->data);
761 if (error) {
762 ovs_fatal(0, "decode error: %s", ofperr_get_name(error));
763 } else if (type == OFPTYPE_ERROR) {
764 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
765 done = true;
766 } else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) {
767 done = !ofpmp_more(reply->data);
768 for (;;) {
769 struct ofputil_table_features tf;
770 int retval;
771
772 retval = ofputil_decode_table_features(reply, &tf, true);
773 if (retval) {
774 if (retval != EOF) {
775 ovs_fatal(0, "decode error: %s",
776 ofperr_get_name(retval));
777 }
778 break;
779 }
780
781 struct ds s = DS_EMPTY_INITIALIZER;
782 ofp_print_table_features(&s, &tf, n ? &prev : NULL,
783 NULL, NULL);
784 puts(ds_cstr(&s));
785 ds_destroy(&s);
786
787 prev = tf;
788 n++;
789 }
790 } else {
791 ovs_fatal(0, "received bad reply: %s",
792 ofp_to_string(reply->data, reply->size,
793 verbosity + 1));
794 }
795 } else {
796 VLOG_DBG("received reply with xid %08"PRIx32" "
797 "!= expected %08"PRIx32, recv_xid, send_xid);
798 }
799 ofpbuf_delete(reply);
5deff5aa
AW
800 }
801
802 vconn_close(vconn);
803}
804
03c72922
BP
805static void
806ofctl_dump_table_desc(struct ovs_cmdl_context *ctx)
807{
808 struct ofpbuf *request;
809 struct vconn *vconn;
810
811 open_vconn(ctx->argv[1], &vconn);
812 request = ofputil_encode_table_desc_request(vconn_get_version(vconn));
813 if (request) {
814 dump_stats_transaction(vconn, request);
815 }
816
817 vconn_close(vconn);
818}
819
820
fca6d553
BP
821static bool fetch_port_by_stats(struct vconn *,
822 const char *port_name, ofp_port_t port_no,
823 struct ofputil_phy_port *);
824
825/* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
826 * named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
827 * successful, false on failure.
828 *
829 * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
830 * list of ports in OFPT_FEATURES_REPLY. */
da91750f 831static bool
fca6d553 832fetch_port_by_features(struct vconn *vconn,
4e022ec0 833 const char *port_name, ofp_port_t port_no,
fca6d553 834 struct ofputil_phy_port *pp)
abaad8cf 835{
9e1fd49b 836 struct ofputil_switch_features features;
982697a4 837 const struct ofp_header *oh;
abaad8cf 838 struct ofpbuf *request, *reply;
9e1fd49b 839 enum ofperr error;
982697a4 840 enum ofptype type;
9e1fd49b 841 struct ofpbuf b;
da91750f 842 bool found = false;
abaad8cf 843
0df0e81d 844 /* Fetch the switch's ofp_switch_features. */
a53a8efa
SH
845 request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
846 vconn_get_version(vconn), 0);
fca6d553
BP
847 run(vconn_transact(vconn, request, &reply),
848 "talking to %s", vconn_get_name(vconn));
abaad8cf 849
6fd6ed71
PS
850 oh = reply->data;
851 if (ofptype_decode(&type, reply->data)
982697a4 852 || type != OFPTYPE_FEATURES_REPLY) {
fca6d553 853 ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
0df0e81d 854 }
fca6d553
BP
855 if (!ofputil_switch_features_has_ports(reply)) {
856 /* The switch features reply does not contain a complete list of ports.
857 * Probably, there are more ports than will fit into a single 64 kB
858 * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of
859 * ports. */
860 ofpbuf_delete(reply);
861 return fetch_port_by_stats(vconn, port_name, port_no, pp);
da91750f
JP
862 }
863
982697a4 864 error = ofputil_decode_switch_features(oh, &features, &b);
9e1fd49b
BP
865 if (error) {
866 ovs_fatal(0, "%s: failed to decode features reply (%s)",
fca6d553 867 vconn_get_name(vconn), ofperr_to_string(error));
9e1fd49b 868 }
0df0e81d 869
982697a4 870 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
4e022ec0 871 if (port_no != OFPP_NONE
9e1fd49b
BP
872 ? port_no == pp->port_no
873 : !strcmp(pp->name, port_name)) {
da91750f 874 found = true;
fca6d553 875 break;
abaad8cf
JP
876 }
877 }
da91750f
JP
878 ofpbuf_delete(reply);
879 return found;
880}
881
fca6d553
BP
882/* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
883 * port named 'port_name' or numbered 'port_no' into '*pp'. Returns true if
884 * successful, false on failure.
885 *
886 * This is most appropriate for OpenFlow 1.3 and later. Open vSwitch 1.7 and
887 * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
888 * 1.1, and 1.2, so this can be used as a fallback in those versions when there
889 * are too many ports than fit in an OFPT_FEATURES_REPLY. */
da91750f 890static bool
fca6d553 891fetch_port_by_stats(struct vconn *vconn,
4e022ec0 892 const char *port_name, ofp_port_t port_no,
da91750f
JP
893 struct ofputil_phy_port *pp)
894{
895 struct ofpbuf *request;
da91750f 896 ovs_be32 send_xid;
da91750f
JP
897 bool done = false;
898 bool found = false;
899
70ae4f93
BP
900 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
901 port_no);
6fd6ed71 902 send_xid = ((struct ofp_header *) request->data)->xid;
da91750f 903
da91750f
JP
904 send_openflow_buffer(vconn, request);
905 while (!done) {
906 ovs_be32 recv_xid;
907 struct ofpbuf *reply;
908
909 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
6fd6ed71 910 recv_xid = ((struct ofp_header *) reply->data)->xid;
da91750f 911 if (send_xid == recv_xid) {
6fd6ed71 912 struct ofp_header *oh = reply->data;
982697a4
BP
913 enum ofptype type;
914 struct ofpbuf b;
915 uint16_t flags;
916
917 ofpbuf_use_const(&b, oh, ntohs(oh->length));
918 if (ofptype_pull(&type, &b)
919 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
da91750f 920 ovs_fatal(0, "received bad reply: %s",
6fd6ed71 921 ofp_to_string(reply->data, reply->size,
da91750f
JP
922 verbosity + 1));
923 }
924
982697a4
BP
925 flags = ofpmp_flags(oh);
926 done = !(flags & OFPSF_REPLY_MORE);
da91750f
JP
927
928 if (found) {
929 /* We've already found the port, but we need to drain
930 * the queue of any other replies for this request. */
931 continue;
932 }
933
982697a4 934 while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
4e022ec0
AW
935 if (port_no != OFPP_NONE ? port_no == pp->port_no
936 : !strcmp(pp->name, port_name)) {
da91750f
JP
937 found = true;
938 break;
939 }
940 }
941 } else {
942 VLOG_DBG("received reply with xid %08"PRIx32" "
943 "!= expected %08"PRIx32, recv_xid, send_xid);
944 }
945 ofpbuf_delete(reply);
946 }
da91750f
JP
947
948 return found;
949}
950
4e022ec0
AW
951static bool
952str_to_ofp(const char *s, ofp_port_t *ofp_port)
953{
954 bool ret;
955 uint32_t port_;
956
957 ret = str_to_uint(s, 10, &port_);
958 *ofp_port = u16_to_ofp(port_);
959 return ret;
960}
da91750f
JP
961
962/* Opens a connection to 'vconn_name', fetches the port structure for
963 * 'port_name' (which may be a port name or number), and copies it into
964 * '*pp'. */
965static void
966fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
967 struct ofputil_phy_port *pp)
968{
fca6d553 969 struct vconn *vconn;
4e022ec0 970 ofp_port_t port_no;
da91750f 971 bool found;
da91750f
JP
972
973 /* Try to interpret the argument as a port number. */
4e022ec0
AW
974 if (!str_to_ofp(port_name, &port_no)) {
975 port_no = OFPP_NONE;
da91750f
JP
976 }
977
fca6d553
BP
978 /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
979 * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it
980 * into the OFPST_PORT_DESC reply. Try it the correct way. */
981 open_vconn(vconn_name, &vconn);
982 found = (vconn_get_version(vconn) < OFP13_VERSION
983 ? fetch_port_by_features(vconn, port_name, port_no, pp)
984 : fetch_port_by_stats(vconn, port_name, port_no, pp));
985 vconn_close(vconn);
da91750f
JP
986
987 if (!found) {
988 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
989 }
0df0e81d 990}
abaad8cf 991
0df0e81d
BP
992/* Returns the port number corresponding to 'port_name' (which may be a port
993 * name or number) within the switch 'vconn_name'. */
4e022ec0 994static ofp_port_t
0df0e81d
BP
995str_to_port_no(const char *vconn_name, const char *port_name)
996{
4e022ec0 997 ofp_port_t port_no;
8010100b
BP
998
999 if (ofputil_port_from_string(port_name, &port_no)) {
0df0e81d
BP
1000 return port_no;
1001 } else {
9e1fd49b 1002 struct ofputil_phy_port pp;
abaad8cf 1003
9e1fd49b
BP
1004 fetch_ofputil_phy_port(vconn_name, port_name, &pp);
1005 return pp.port_no;
0df0e81d 1006 }
abaad8cf
JP
1007}
1008
88ca35ee 1009static bool
27527aa0
BP
1010try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
1011 enum ofputil_protocol *cur)
88ca35ee 1012{
27527aa0
BP
1013 for (;;) {
1014 struct ofpbuf *request, *reply;
1015 enum ofputil_protocol next;
88ca35ee 1016
27527aa0
BP
1017 request = ofputil_encode_set_protocol(*cur, want, &next);
1018 if (!request) {
e43928f2 1019 return *cur == want;
27527aa0
BP
1020 }
1021
1022 run(vconn_transact_noreply(vconn, request, &reply),
1023 "talking to %s", vconn_get_name(vconn));
1024 if (reply) {
6fd6ed71 1025 char *s = ofp_to_string(reply->data, reply->size, 2);
27527aa0
BP
1026 VLOG_DBG("%s: failed to set protocol, switch replied: %s",
1027 vconn_get_name(vconn), s);
1028 free(s);
1029 ofpbuf_delete(reply);
1030 return false;
1031 }
1032
1033 *cur = next;
88ca35ee 1034 }
88ca35ee
BP
1035}
1036
27527aa0
BP
1037static enum ofputil_protocol
1038set_protocol_for_flow_dump(struct vconn *vconn,
1039 enum ofputil_protocol cur_protocol,
1040 enum ofputil_protocol usable_protocols)
064af421 1041{
27527aa0
BP
1042 char *usable_s;
1043 int i;
064af421 1044
27527aa0
BP
1045 for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
1046 enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
1047 if (f & usable_protocols & allowed_protocols
1048 && try_set_protocol(vconn, f, &cur_protocol)) {
1049 return f;
f9cbfbe4 1050 }
27527aa0 1051 }
f9cbfbe4 1052
27527aa0
BP
1053 usable_s = ofputil_protocols_to_string(usable_protocols);
1054 if (usable_protocols & allowed_protocols) {
1055 ovs_fatal(0, "switch does not support any of the usable flow "
1056 "formats (%s)", usable_s);
f9cbfbe4 1057 } else {
27527aa0
BP
1058 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1059 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1060 "allowed flow formats (%s)", usable_s, allowed_s);
88ca35ee 1061 }
064af421
BP
1062}
1063
bdcc5925
BP
1064static struct vconn *
1065prepare_dump_flows(int argc, char *argv[], bool aggregate,
1066 struct ofpbuf **requestp)
064af421 1067{
27527aa0 1068 enum ofputil_protocol usable_protocols, protocol;
81d1ea94 1069 struct ofputil_flow_stats_request fsr;
88ca35ee 1070 struct vconn *vconn;
bdda5aca
BP
1071 char *error;
1072
1073 error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
db0b6c29 1074 argc > 2 ? argv[2] : "",
ba2fe8e9 1075 &usable_protocols);
bdda5aca
BP
1076 if (error) {
1077 ovs_fatal(0, "%s", error);
1078 }
064af421 1079
27527aa0
BP
1080 protocol = open_vconn(argv[1], &vconn);
1081 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
bdcc5925
BP
1082 *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
1083 return vconn;
1084}
1085
1086static void
1087ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
1088{
1089 struct ofpbuf *request;
1090 struct vconn *vconn;
1091
1092 vconn = prepare_dump_flows(argc, argv, aggregate, &request);
53514387 1093 dump_stats_transaction(vconn, request);
88ca35ee
BP
1094 vconn_close(vconn);
1095}
1096
bdcc5925
BP
1097static int
1098compare_flows(const void *afs_, const void *bfs_)
1099{
1100 const struct ofputil_flow_stats *afs = afs_;
1101 const struct ofputil_flow_stats *bfs = bfs_;
81a76618
BP
1102 const struct match *a = &afs->match;
1103 const struct match *b = &bfs->match;
bdcc5925
BP
1104 const struct sort_criterion *sc;
1105
1106 for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
1107 const struct mf_field *f = sc->field;
1108 int ret;
1109
1110 if (!f) {
eb391b76
BP
1111 int a_pri = afs->priority;
1112 int b_pri = bfs->priority;
81a76618 1113 ret = a_pri < b_pri ? -1 : a_pri > b_pri;
bdcc5925
BP
1114 } else {
1115 bool ina, inb;
1116
1117 ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
1118 inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
1119 if (ina != inb) {
1120 /* Skip the test for sc->order, so that missing fields always
1121 * sort to the end whether we're sorting in ascending or
1122 * descending order. */
1123 return ina ? -1 : 1;
1124 } else {
1125 union mf_value aval, bval;
1126
1127 mf_get_value(f, &a->flow, &aval);
1128 mf_get_value(f, &b->flow, &bval);
1129 ret = memcmp(&aval, &bval, f->n_bytes);
1130 }
1131 }
1132
1133 if (ret) {
1134 return sc->order == SORT_ASC ? ret : -ret;
1135 }
1136 }
1137
1138 return 0;
1139}
1140
88ca35ee 1141static void
1636c761 1142ofctl_dump_flows(struct ovs_cmdl_context *ctx)
88ca35ee 1143{
bdcc5925 1144 if (!n_criteria) {
1636c761 1145 ofctl_dump_flows__(ctx->argc, ctx->argv, false);
2f4aba0b 1146 return;
bdcc5925
BP
1147 } else {
1148 struct ofputil_flow_stats *fses;
1149 size_t n_fses, allocated_fses;
1150 struct ofpbuf *request;
1151 struct ofpbuf ofpacts;
1152 struct ofpbuf *reply;
1153 struct vconn *vconn;
1154 ovs_be32 send_xid;
1155 struct ds s;
1156 size_t i;
1157
1636c761 1158 vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request);
6fd6ed71 1159 send_xid = ((struct ofp_header *) request->data)->xid;
bdcc5925
BP
1160 send_openflow_buffer(vconn, request);
1161
1162 fses = NULL;
1163 n_fses = allocated_fses = 0;
1164 reply = NULL;
1165 ofpbuf_init(&ofpacts, 0);
1166 for (;;) {
1167 struct ofputil_flow_stats *fs;
1168
1169 if (n_fses >= allocated_fses) {
1170 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1171 }
1172
1173 fs = &fses[n_fses];
1174 if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1175 &ofpacts)) {
1176 break;
1177 }
1178 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1179 n_fses++;
1180 }
1181 ofpbuf_uninit(&ofpacts);
1182
1183 qsort(fses, n_fses, sizeof *fses, compare_flows);
1184
1185 ds_init(&s);
1186 for (i = 0; i < n_fses; i++) {
1187 ds_clear(&s);
1188 ofp_print_flow_stats(&s, &fses[i]);
1189 puts(ds_cstr(&s));
1190 }
1191 ds_destroy(&s);
1192
1193 for (i = 0; i < n_fses; i++) {
dc723c44 1194 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
bdcc5925
BP
1195 }
1196 free(fses);
1197
1198 vconn_close(vconn);
1199 }
88ca35ee
BP
1200}
1201
1202static void
1636c761 1203ofctl_dump_aggregate(struct ovs_cmdl_context *ctx)
88ca35ee 1204{
1636c761 1205 ofctl_dump_flows__(ctx->argc, ctx->argv, true);
064af421
BP
1206}
1207
d2805da2 1208static void
1636c761 1209ofctl_queue_stats(struct ovs_cmdl_context *ctx)
d2805da2 1210{
d2805da2 1211 struct ofpbuf *request;
53514387 1212 struct vconn *vconn;
64626975 1213 struct ofputil_queue_stats_request oqs;
d2805da2 1214
1636c761 1215 open_vconn(ctx->argv[1], &vconn);
d2805da2 1216
1636c761
RB
1217 if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) {
1218 oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]);
d2805da2 1219 } else {
7f05e7ab 1220 oqs.port_no = OFPP_ANY;
d2805da2 1221 }
1636c761
RB
1222 if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) {
1223 oqs.queue_id = atoi(ctx->argv[3]);
d2805da2 1224 } else {
64626975 1225 oqs.queue_id = OFPQ_ALL;
d2805da2
BP
1226 }
1227
64626975 1228 request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
53514387
SH
1229 dump_stats_transaction(vconn, request);
1230 vconn_close(vconn);
d2805da2
BP
1231}
1232
e8f9a7bb 1233static void
1636c761 1234ofctl_queue_get_config(struct ovs_cmdl_context *ctx)
e8f9a7bb 1235{
1636c761
RB
1236 const char *vconn_name = ctx->argv[1];
1237 const char *port_name = ctx->argv[2];
e8f9a7bb
VG
1238 enum ofputil_protocol protocol;
1239 enum ofp_version version;
1240 struct ofpbuf *request;
1241 struct vconn *vconn;
1242 ofp_port_t port;
1243
1244 port = str_to_port_no(vconn_name, port_name);
1245
1246 protocol = open_vconn(vconn_name, &vconn);
1247 version = ofputil_protocol_to_ofp_version(protocol);
1248 request = ofputil_encode_queue_get_config_request(version, port);
1249 dump_transaction(vconn, request);
1250 vconn_close(vconn);
1251}
1252
27527aa0 1253static enum ofputil_protocol
db0b6c29
JR
1254open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1255 enum ofputil_protocol usable_protocols)
0fbc9f11 1256{
27527aa0
BP
1257 enum ofputil_protocol cur_protocol;
1258 char *usable_s;
1259 int i;
0fbc9f11 1260
27527aa0
BP
1261 if (!(usable_protocols & allowed_protocols)) {
1262 char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1263 usable_s = ofputil_protocols_to_string(usable_protocols);
1264 ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1265 "allowed flow formats (%s)", usable_s, allowed_s);
0fbc9f11 1266 }
0fbc9f11 1267
27527aa0
BP
1268 /* If the initial flow format is allowed and usable, keep it. */
1269 cur_protocol = open_vconn(remote, vconnp);
1270 if (usable_protocols & allowed_protocols & cur_protocol) {
1271 return cur_protocol;
1272 }
1273
1274 /* Otherwise try each flow format in turn. */
1275 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1276 enum ofputil_protocol f = 1 << i;
1277
1278 if (f != cur_protocol
1279 && f & usable_protocols & allowed_protocols
1280 && try_set_protocol(*vconnp, f, &cur_protocol)) {
1281 return f;
1282 }
0fbc9f11 1283 }
27527aa0
BP
1284
1285 usable_s = ofputil_protocols_to_string(usable_protocols);
1286 ovs_fatal(0, "switch does not support any of the usable flow "
1287 "formats (%s)", usable_s);
0fbc9f11
BP
1288}
1289
db5076ee
JR
1290static void
1291bundle_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1292 size_t n_fms, enum ofputil_protocol usable_protocols)
1293{
1294 enum ofputil_protocol protocol;
1295 struct vconn *vconn;
1296 struct ovs_list requests;
1297 size_t i;
1298
1299 list_init(&requests);
1300
1301 /* Bundles need OpenFlow 1.4+. */
1302 usable_protocols &= OFPUTIL_P_OF14_UP;
1303 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1304
1305 for (i = 0; i < n_fms; i++) {
1306 struct ofputil_flow_mod *fm = &fms[i];
1307 struct ofpbuf *request = ofputil_encode_flow_mod(fm, protocol);
1308
1309 list_push_back(&requests, &request->list_node);
1310 free(CONST_CAST(struct ofpact *, fm->ofpacts));
1311 }
1312
39c94593 1313 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
db5076ee
JR
1314 vconn_close(vconn);
1315}
1316
064af421 1317static void
e1fef0f9 1318ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
db0b6c29 1319 size_t n_fms, enum ofputil_protocol usable_protocols)
064af421 1320{
27527aa0 1321 enum ofputil_protocol protocol;
064af421 1322 struct vconn *vconn;
27527aa0 1323 size_t i;
4989c59f 1324
db5076ee
JR
1325 if (bundle) {
1326 bundle_flow_mod__(remote, fms, n_fms, usable_protocols);
1327 return;
1328 }
1329
db0b6c29 1330 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
049c8dc2 1331
27527aa0
BP
1332 for (i = 0; i < n_fms; i++) {
1333 struct ofputil_flow_mod *fm = &fms[i];
0fbc9f11 1334
27527aa0 1335 transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
dc723c44 1336 free(CONST_CAST(struct ofpact *, fm->ofpacts));
4989c59f 1337 }
064af421 1338 vconn_close(vconn);
88ca35ee
BP
1339}
1340
064af421 1341static void
db5076ee 1342ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], int command)
064af421 1343{
db0b6c29 1344 enum ofputil_protocol usable_protocols;
27527aa0
BP
1345 struct ofputil_flow_mod *fms = NULL;
1346 size_t n_fms = 0;
bdda5aca 1347 char *error;
064af421 1348
db5076ee
JR
1349 if (command == OFPFC_ADD) {
1350 /* Allow the file to specify a mix of commands. If none specified at
1351 * the beginning of any given line, then the default is OFPFC_ADD, so
1352 * this is backwards compatible. */
1353 command = -2;
1354 }
db0b6c29 1355 error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
ba2fe8e9 1356 &usable_protocols);
bdda5aca
BP
1357 if (error) {
1358 ovs_fatal(0, "%s", error);
1359 }
db0b6c29 1360 ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
27527aa0
BP
1361 free(fms);
1362}
1363
1364static void
e1fef0f9 1365ofctl_flow_mod(int argc, char *argv[], uint16_t command)
27527aa0 1366{
4989c59f 1367 if (argc > 2 && !strcmp(argv[2], "-")) {
e1fef0f9 1368 ofctl_flow_mod_file(argc, argv, command);
27527aa0
BP
1369 } else {
1370 struct ofputil_flow_mod fm;
bdda5aca 1371 char *error;
3200ed58 1372 enum ofputil_protocol usable_protocols;
bdda5aca 1373
db0b6c29 1374 error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
ba2fe8e9 1375 &usable_protocols);
bdda5aca
BP
1376 if (error) {
1377 ovs_fatal(0, "%s", error);
1378 }
db0b6c29 1379 ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
064af421 1380 }
4989c59f 1381}
88ca35ee 1382
4989c59f 1383static void
1636c761 1384ofctl_add_flow(struct ovs_cmdl_context *ctx)
4989c59f 1385{
1636c761 1386 ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD);
4989c59f
BP
1387}
1388
1389static void
1636c761 1390ofctl_add_flows(struct ovs_cmdl_context *ctx)
4989c59f 1391{
1636c761 1392 ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD);
064af421
BP
1393}
1394
1395static void
1636c761 1396ofctl_mod_flows(struct ovs_cmdl_context *ctx)
064af421 1397{
1636c761 1398 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
064af421
BP
1399}
1400
88ca35ee 1401static void
1636c761 1402ofctl_del_flows(struct ovs_cmdl_context *ctx)
064af421 1403{
1636c761 1404 ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
064af421
BP
1405}
1406
54834960
EJ
1407static void
1408set_packet_in_format(struct vconn *vconn,
1409 enum nx_packet_in_format packet_in_format)
1410{
3f4a1939
SH
1411 struct ofpbuf *spif;
1412
1413 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1414 packet_in_format);
54834960
EJ
1415 transact_noreply(vconn, spif);
1416 VLOG_DBG("%s: using user-specified packet in format %s",
1417 vconn_get_name(vconn),
1418 ofputil_packet_in_format_to_string(packet_in_format));
1419}
1420
f0fd1a17
PS
1421static int
1422monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1423{
1424 struct ofp_switch_config config;
1425 enum ofp_config_flags flags;
1426
1427 fetch_switch_config(vconn, &config);
1428 flags = ntohs(config.flags);
1429 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1430 /* Set the invalid ttl config. */
1431 flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1432
1433 config.flags = htons(flags);
1434 set_switch_config(vconn, &config);
1435
1436 /* Then retrieve the configuration to see if it really took. OpenFlow
1437 * doesn't define error reporting for bad modes, so this is all we can
1438 * do. */
1439 fetch_switch_config(vconn, &config);
1440 flags = ntohs(config.flags);
1441 if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1442 ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1443 "switch probably doesn't support mode)");
1444 return -EOPNOTSUPP;
1445 }
1446 }
1447 return 0;
1448}
1449
96761f58
BP
1450/* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The
1451 * caller must free '*msgp'. On success, returns NULL. On failure, returns
1452 * an error message and stores NULL in '*msgp'. */
1453static const char *
1454openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1455{
1456 struct ofp_header *oh;
1457 struct ofpbuf *msg;
1458
1459 msg = ofpbuf_new(strlen(hex) / 2);
1460 *msgp = NULL;
1461
1462 if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1463 ofpbuf_delete(msg);
1464 return "Trailing garbage in hex data";
1465 }
1466
6fd6ed71 1467 if (msg->size < sizeof(struct ofp_header)) {
96761f58
BP
1468 ofpbuf_delete(msg);
1469 return "Message too short for OpenFlow";
1470 }
1471
6fd6ed71
PS
1472 oh = msg->data;
1473 if (msg->size != ntohs(oh->length)) {
96761f58
BP
1474 ofpbuf_delete(msg);
1475 return "Message size does not match length in OpenFlow header";
1476 }
1477
1478 *msgp = msg;
1479 return NULL;
1480}
1481
1482static void
1483ofctl_send(struct unixctl_conn *conn, int argc,
1484 const char *argv[], void *vconn_)
1485{
1486 struct vconn *vconn = vconn_;
1487 struct ds reply;
1488 bool ok;
1489 int i;
1490
1491 ok = true;
1492 ds_init(&reply);
1493 for (i = 1; i < argc; i++) {
1494 const char *error_msg;
1495 struct ofpbuf *msg;
1496 int error;
1497
1498 error_msg = openflow_from_hex(argv[i], &msg);
1499 if (error_msg) {
1500 ds_put_format(&reply, "%s\n", error_msg);
1501 ok = false;
1502 continue;
1503 }
1504
1505 fprintf(stderr, "send: ");
6fd6ed71 1506 ofp_print(stderr, msg->data, msg->size, verbosity);
96761f58
BP
1507
1508 error = vconn_send_block(vconn, msg);
1509 if (error) {
1510 ofpbuf_delete(msg);
10a89ef0 1511 ds_put_format(&reply, "%s\n", ovs_strerror(error));
96761f58
BP
1512 ok = false;
1513 } else {
1514 ds_put_cstr(&reply, "sent\n");
1515 }
1516 }
bde9f75d
EJ
1517
1518 if (ok) {
1519 unixctl_command_reply(conn, ds_cstr(&reply));
1520 } else {
1521 unixctl_command_reply_error(conn, ds_cstr(&reply));
1522 }
96761f58
BP
1523 ds_destroy(&reply);
1524}
1525
bb638b9a
BP
1526struct barrier_aux {
1527 struct vconn *vconn; /* OpenFlow connection for sending barrier. */
1528 struct unixctl_conn *conn; /* Connection waiting for barrier response. */
1529};
1530
1531static void
1532ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1533 const char *argv[] OVS_UNUSED, void *aux_)
1534{
1535 struct barrier_aux *aux = aux_;
1536 struct ofpbuf *msg;
1537 int error;
1538
1539 if (aux->conn) {
bde9f75d 1540 unixctl_command_reply_error(conn, "already waiting for barrier reply");
bb638b9a
BP
1541 return;
1542 }
1543
a0ae0b6e 1544 msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
bb638b9a
BP
1545 error = vconn_send_block(aux->vconn, msg);
1546 if (error) {
1547 ofpbuf_delete(msg);
10a89ef0 1548 unixctl_command_reply_error(conn, ovs_strerror(error));
bb638b9a
BP
1549 } else {
1550 aux->conn = conn;
1551 }
1552}
1553
1e1d00a5
BP
1554static void
1555ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1556 const char *argv[], void *aux OVS_UNUSED)
1557{
1558 int fd;
1559
1560 fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1561 if (fd < 0) {
10a89ef0 1562 unixctl_command_reply_error(conn, ovs_strerror(errno));
1e1d00a5
BP
1563 return;
1564 }
1565
1566 fflush(stderr);
1567 dup2(fd, STDERR_FILENO);
1568 close(fd);
bde9f75d 1569 unixctl_command_reply(conn, NULL);
1e1d00a5
BP
1570}
1571
2b07c8b1
BP
1572static void
1573ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
4f8f2439 1574 const char *argv[] OVS_UNUSED, void *blocked_)
2b07c8b1 1575{
4f8f2439 1576 bool *blocked = blocked_;
2b07c8b1 1577
4f8f2439
BP
1578 if (!*blocked) {
1579 *blocked = true;
1580 unixctl_command_reply(conn, NULL);
1581 } else {
2b07c8b1 1582 unixctl_command_reply(conn, "already blocking");
2b07c8b1
BP
1583 }
1584}
1585
1586static void
1587ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
4f8f2439 1588 const char *argv[] OVS_UNUSED, void *blocked_)
2b07c8b1 1589{
4f8f2439 1590 bool *blocked = blocked_;
2b07c8b1 1591
4f8f2439
BP
1592 if (*blocked) {
1593 *blocked = false;
2b07c8b1 1594 unixctl_command_reply(conn, NULL);
4f8f2439
BP
1595 } else {
1596 unixctl_command_reply(conn, "already unblocked");
2b07c8b1
BP
1597 }
1598}
1599
05f453a8
BP
1600/* Prints to stdout all of the messages received on 'vconn'.
1601 *
1602 * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1603 * received on 'vconn'. */
064af421 1604static void
05f453a8 1605monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
0caf6bde 1606{
bb638b9a 1607 struct barrier_aux barrier_aux = { vconn, NULL };
1eb85ef5
EJ
1608 struct unixctl_server *server;
1609 bool exiting = false;
4f8f2439 1610 bool blocked = false;
7d0c5973 1611 int error;
1eb85ef5 1612
7d0c5973 1613 daemon_save_fd(STDERR_FILENO);
1eb85ef5 1614 daemonize_start();
19945013 1615 error = unixctl_server_create(unixctl_path, &server);
1eb85ef5
EJ
1616 if (error) {
1617 ovs_fatal(error, "failed to create unixctl server");
1618 }
1619 unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
96761f58
BP
1620 unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1621 ofctl_send, vconn);
bb638b9a
BP
1622 unixctl_command_register("ofctl/barrier", "", 0, 0,
1623 ofctl_barrier, &barrier_aux);
1e1d00a5
BP
1624 unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1625 ofctl_set_output_file, NULL);
2b07c8b1 1626
4f8f2439
BP
1627 unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1628 unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1629 &blocked);
2b07c8b1 1630
1eb85ef5
EJ
1631 daemonize_complete();
1632
0caf6bde
BP
1633 for (;;) {
1634 struct ofpbuf *b;
1eb85ef5
EJ
1635 int retval;
1636
1637 unixctl_server_run(server);
982697a4 1638
4f8f2439 1639 while (!blocked) {
982697a4 1640 enum ofptype type;
bb638b9a 1641
1eb85ef5
EJ
1642 retval = vconn_recv(vconn, &b);
1643 if (retval == EAGAIN) {
1644 break;
1645 }
1eb85ef5 1646 run(retval, "vconn_recv");
31c6fcd7 1647
0c9560b7 1648 if (timestamp) {
2b31d8e7
PI
1649 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1650 time_wall_msec(), true);
0c9560b7 1651 fputs(s, stderr);
3e78870d 1652 free(s);
0c9560b7 1653 }
31c6fcd7 1654
6fd6ed71
PS
1655 ofptype_decode(&type, b->data);
1656 ofp_print(stderr, b->data, b->size, verbosity + 2);
eafa7af8 1657 fflush(stderr);
bb638b9a 1658
05f453a8
BP
1659 switch ((int) type) {
1660 case OFPTYPE_BARRIER_REPLY:
1661 if (barrier_aux.conn) {
1662 unixctl_command_reply(barrier_aux.conn, NULL);
1663 barrier_aux.conn = NULL;
1664 }
1665 break;
1666
1667 case OFPTYPE_ECHO_REQUEST:
1668 if (reply_to_echo_requests) {
1669 struct ofpbuf *reply;
1670
6fd6ed71 1671 reply = make_echo_reply(b->data);
05f453a8
BP
1672 retval = vconn_send_block(vconn, reply);
1673 if (retval) {
1674 ovs_fatal(retval, "failed to send echo reply");
1675 }
1676 }
1677 break;
bb638b9a 1678 }
05f453a8 1679 ofpbuf_delete(b);
1eb85ef5
EJ
1680 }
1681
1682 if (exiting) {
1683 break;
1684 }
1685
1686 vconn_run(vconn);
1687 vconn_run_wait(vconn);
4f8f2439
BP
1688 if (!blocked) {
1689 vconn_recv_wait(vconn);
1690 }
1eb85ef5
EJ
1691 unixctl_server_wait(server);
1692 poll_block();
0caf6bde 1693 }
828c72d0
BP
1694 vconn_close(vconn);
1695 unixctl_server_destroy(server);
0caf6bde
BP
1696}
1697
1698static void
1636c761 1699ofctl_monitor(struct ovs_cmdl_context *ctx)
064af421
BP
1700{
1701 struct vconn *vconn;
2b07c8b1 1702 int i;
db0b6c29 1703 enum ofputil_protocol usable_protocols;
064af421 1704
1636c761
RB
1705 open_vconn(ctx->argv[1], &vconn);
1706 for (i = 2; i < ctx->argc; i++) {
1707 const char *arg = ctx->argv[i];
064af421 1708
2b07c8b1
BP
1709 if (isdigit((unsigned char) *arg)) {
1710 struct ofp_switch_config config;
1711
1712 fetch_switch_config(vconn, &config);
1713 config.miss_send_len = htons(atoi(arg));
1714 set_switch_config(vconn, &config);
1715 } else if (!strcmp(arg, "invalid_ttl")) {
f0fd1a17 1716 monitor_set_invalid_ttl_to_controller(vconn);
2b07c8b1
BP
1717 } else if (!strncmp(arg, "watch:", 6)) {
1718 struct ofputil_flow_monitor_request fmr;
1719 struct ofpbuf *msg;
bdda5aca 1720 char *error;
2b07c8b1 1721
db0b6c29
JR
1722 error = parse_flow_monitor_request(&fmr, arg + 6,
1723 &usable_protocols);
bdda5aca
BP
1724 if (error) {
1725 ovs_fatal(0, "%s", error);
1726 }
2b07c8b1
BP
1727
1728 msg = ofpbuf_new(0);
1729 ofputil_append_flow_monitor_request(&fmr, msg);
53514387 1730 dump_stats_transaction(vconn, msg);
eafa7af8 1731 fflush(stdout);
2b07c8b1
BP
1732 } else {
1733 ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
f0fd1a17
PS
1734 }
1735 }
2b07c8b1 1736
ca8526e0
BP
1737 if (preferred_packet_in_format >= 0) {
1738 set_packet_in_format(vconn, preferred_packet_in_format);
1739 } else {
f02b12c4
SH
1740 enum ofp_version version = vconn_get_version(vconn);
1741
1742 switch (version) {
1743 case OFP10_VERSION: {
1744 struct ofpbuf *spif, *reply;
1745
1746 spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1747 NXPIF_NXM);
1748 run(vconn_transact_noreply(vconn, spif, &reply),
1749 "talking to %s", vconn_get_name(vconn));
1750 if (reply) {
6fd6ed71 1751 char *s = ofp_to_string(reply->data, reply->size, 2);
f02b12c4
SH
1752 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1753 " replied: %s. Falling back to the switch default.",
1754 vconn_get_name(vconn), s);
1755 free(s);
1756 ofpbuf_delete(reply);
1757 }
1758 break;
1759 }
1760 case OFP11_VERSION:
1761 case OFP12_VERSION:
1762 case OFP13_VERSION:
c37c0382 1763 case OFP14_VERSION:
42dccab5 1764 case OFP15_VERSION:
f02b12c4
SH
1765 break;
1766 default:
428b2edd 1767 OVS_NOT_REACHED();
ca8526e0
BP
1768 }
1769 }
1770
05f453a8 1771 monitor_vconn(vconn, true);
0caf6bde
BP
1772}
1773
1774static void
1636c761 1775ofctl_snoop(struct ovs_cmdl_context *ctx)
0caf6bde
BP
1776{
1777 struct vconn *vconn;
1778
1636c761 1779 open_vconn__(ctx->argv[1], SNOOP, &vconn);
05f453a8 1780 monitor_vconn(vconn, false);
064af421
BP
1781}
1782
1783static void
1636c761 1784ofctl_dump_ports(struct ovs_cmdl_context *ctx)
064af421 1785{
abaad8cf 1786 struct ofpbuf *request;
53514387 1787 struct vconn *vconn;
4e022ec0 1788 ofp_port_t port;
abaad8cf 1789
1636c761
RB
1790 open_vconn(ctx->argv[1], &vconn);
1791 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
f8e4867e 1792 request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
53514387
SH
1793 dump_stats_transaction(vconn, request);
1794 vconn_close(vconn);
064af421
BP
1795}
1796
2be393ed 1797static void
1636c761 1798ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx)
2be393ed 1799{
70ae4f93
BP
1800 struct ofpbuf *request;
1801 struct vconn *vconn;
1802 ofp_port_t port;
1803
1636c761
RB
1804 open_vconn(ctx->argv[1], &vconn);
1805 port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
70ae4f93
BP
1806 request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1807 port);
1808 dump_stats_transaction(vconn, request);
1809 vconn_close(vconn);
2be393ed
JP
1810}
1811
064af421 1812static void
1636c761 1813ofctl_probe(struct ovs_cmdl_context *ctx)
064af421
BP
1814{
1815 struct ofpbuf *request;
1816 struct vconn *vconn;
1817 struct ofpbuf *reply;
1818
1636c761 1819 open_vconn(ctx->argv[1], &vconn);
1a126c0c 1820 request = make_echo_request(vconn_get_version(vconn));
1636c761 1821 run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]);
6fd6ed71 1822 if (reply->size != sizeof(struct ofp_header)) {
064af421
BP
1823 ovs_fatal(0, "reply does not match request");
1824 }
1825 ofpbuf_delete(reply);
1826 vconn_close(vconn);
1827}
1828
0c3d5fc8 1829static void
1636c761 1830ofctl_packet_out(struct ovs_cmdl_context *ctx)
0c3d5fc8 1831{
de0f3156 1832 enum ofputil_protocol protocol;
0c3d5fc8 1833 struct ofputil_packet_out po;
f25d0cf3 1834 struct ofpbuf ofpacts;
0c3d5fc8 1835 struct vconn *vconn;
bdda5aca 1836 char *error;
0c3d5fc8 1837 int i;
b420c0cf 1838 enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
0c3d5fc8 1839
f25d0cf3 1840 ofpbuf_init(&ofpacts, 64);
1636c761 1841 error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, &usable_protocols);
bdda5aca
BP
1842 if (error) {
1843 ovs_fatal(0, "%s", error);
1844 }
0c3d5fc8
BP
1845
1846 po.buffer_id = UINT32_MAX;
1636c761 1847 po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]);
6fd6ed71
PS
1848 po.ofpacts = ofpacts.data;
1849 po.ofpacts_len = ofpacts.size;
0c3d5fc8 1850
1636c761
RB
1851 protocol = open_vconn(ctx->argv[1], &vconn);
1852 for (i = 4; i < ctx->argc; i++) {
cf62fa4c
PS
1853 struct dp_packet *packet;
1854 struct ofpbuf *opo;
0c3d5fc8
BP
1855 const char *error_msg;
1856
1636c761 1857 error_msg = eth_from_hex(ctx->argv[i], &packet);
0c3d5fc8
BP
1858 if (error_msg) {
1859 ovs_fatal(0, "%s", error_msg);
1860 }
1861
cf62fa4c
PS
1862 po.packet = dp_packet_data(packet);
1863 po.packet_len = dp_packet_size(packet);
de0f3156 1864 opo = ofputil_encode_packet_out(&po, protocol);
0c3d5fc8 1865 transact_noreply(vconn, opo);
cf62fa4c 1866 dp_packet_delete(packet);
0c3d5fc8
BP
1867 }
1868 vconn_close(vconn);
f25d0cf3 1869 ofpbuf_uninit(&ofpacts);
0c3d5fc8
BP
1870}
1871
064af421 1872static void
1636c761 1873ofctl_mod_port(struct ovs_cmdl_context *ctx)
064af421 1874{
28124950
BP
1875 struct ofp_config_flag {
1876 const char *name; /* The flag's name. */
1877 enum ofputil_port_config bit; /* Bit to turn on or off. */
1878 bool on; /* Value to set the bit to. */
1879 };
1880 static const struct ofp_config_flag flags[] = {
1881 { "up", OFPUTIL_PC_PORT_DOWN, false },
1882 { "down", OFPUTIL_PC_PORT_DOWN, true },
1883 { "stp", OFPUTIL_PC_NO_STP, false },
1884 { "receive", OFPUTIL_PC_NO_RECV, false },
1885 { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false },
1886 { "flood", OFPUTIL_PC_NO_FLOOD, false },
1887 { "forward", OFPUTIL_PC_NO_FWD, false },
1888 { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false },
1889 };
1890
1891 const struct ofp_config_flag *flag;
9e1fd49b
BP
1892 enum ofputil_protocol protocol;
1893 struct ofputil_port_mod pm;
1894 struct ofputil_phy_port pp;
064af421 1895 struct vconn *vconn;
28124950
BP
1896 const char *command;
1897 bool not;
064af421 1898
1636c761 1899 fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp);
064af421 1900
9e1fd49b
BP
1901 pm.port_no = pp.port_no;
1902 memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1903 pm.config = 0;
1904 pm.mask = 0;
1905 pm.advertise = 0;
064af421 1906
1636c761
RB
1907 if (!strncasecmp(ctx->argv[3], "no-", 3)) {
1908 command = ctx->argv[3] + 3;
28124950 1909 not = true;
1636c761
RB
1910 } else if (!strncasecmp(ctx->argv[3], "no", 2)) {
1911 command = ctx->argv[3] + 2;
28124950 1912 not = true;
064af421 1913 } else {
1636c761 1914 command = ctx->argv[3];
28124950
BP
1915 not = false;
1916 }
1917 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1918 if (!strcasecmp(command, flag->name)) {
1919 pm.mask = flag->bit;
1920 pm.config = flag->on ^ not ? flag->bit : 0;
1921 goto found;
1922 }
064af421 1923 }
1636c761 1924 ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]);
064af421 1925
28124950 1926found:
1636c761 1927 protocol = open_vconn(ctx->argv[1], &vconn);
9e1fd49b 1928 transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
064af421
BP
1929 vconn_close(vconn);
1930}
1931
918f2b82 1932static void
1636c761 1933ofctl_mod_table(struct ovs_cmdl_context *ctx)
918f2b82 1934{
82c22d34 1935 uint32_t usable_versions;
918f2b82
AZ
1936 struct ofputil_table_mod tm;
1937 struct vconn *vconn;
1938 char *error;
918f2b82 1939
82c22d34
BP
1940 error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3],
1941 &usable_versions);
918f2b82
AZ
1942 if (error) {
1943 ovs_fatal(0, "%s", error);
1944 }
1945
82c22d34
BP
1946 uint32_t allowed_versions = get_allowed_ofp_versions();
1947 if (!(allowed_versions & usable_versions)) {
1948 struct ds versions = DS_EMPTY_INITIALIZER;
1949 ofputil_format_version_bitmap_names(&versions, allowed_versions);
1950 ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow "
1951 "versions %s but none is enabled (use -O)",
1952 ctx->argv[3], ds_cstr(&versions));
918f2b82 1953 }
82c22d34 1954 mask_allowed_ofp_versions(usable_versions);
918f2b82 1955
82c22d34 1956 enum ofputil_protocol protocol = open_vconn(ctx->argv[1], &vconn);
918f2b82
AZ
1957 transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1958 vconn_close(vconn);
1959}
1960
7257b535 1961static void
1636c761 1962ofctl_get_frags(struct ovs_cmdl_context *ctx)
7257b535
BP
1963{
1964 struct ofp_switch_config config;
1965 struct vconn *vconn;
1966
1636c761 1967 open_vconn(ctx->argv[1], &vconn);
7257b535
BP
1968 fetch_switch_config(vconn, &config);
1969 puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1970 vconn_close(vconn);
1971}
1972
1973static void
1636c761 1974ofctl_set_frags(struct ovs_cmdl_context *ctx)
7257b535
BP
1975{
1976 struct ofp_switch_config config;
1977 enum ofp_config_flags mode;
1978 struct vconn *vconn;
1979 ovs_be16 flags;
1980
1636c761
RB
1981 if (!ofputil_frag_handling_from_string(ctx->argv[2], &mode)) {
1982 ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]);
7257b535
BP
1983 }
1984
1636c761 1985 open_vconn(ctx->argv[1], &vconn);
7257b535
BP
1986 fetch_switch_config(vconn, &config);
1987 flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1988 if (flags != config.flags) {
1989 /* Set the configuration. */
1990 config.flags = flags;
1991 set_switch_config(vconn, &config);
1992
1993 /* Then retrieve the configuration to see if it really took. OpenFlow
1994 * doesn't define error reporting for bad modes, so this is all we can
1995 * do. */
1996 fetch_switch_config(vconn, &config);
1997 if (flags != config.flags) {
1998 ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1999 "switch probably doesn't support mode \"%s\")",
1636c761 2000 ctx->argv[1], ofputil_frag_handling_to_string(mode));
7257b535
BP
2001 }
2002 }
2003 vconn_close(vconn);
2004}
2005
1ac0e975 2006static void
1636c761 2007ofctl_ofp_parse(struct ovs_cmdl_context *ctx)
1ac0e975 2008{
1636c761 2009 const char *filename = ctx->argv[1];
1ac0e975
BP
2010 struct ofpbuf b;
2011 FILE *file;
2012
2013 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2014 if (file == NULL) {
2015 ovs_fatal(errno, "%s: open", filename);
2016 }
2017
2018 ofpbuf_init(&b, 65536);
2019 for (;;) {
2020 struct ofp_header *oh;
2021 size_t length, tail_len;
2022 void *tail;
2023 size_t n;
2024
2025 ofpbuf_clear(&b);
2026 oh = ofpbuf_put_uninit(&b, sizeof *oh);
2027 n = fread(oh, 1, sizeof *oh, file);
2028 if (n == 0) {
2029 break;
2030 } else if (n < sizeof *oh) {
2031 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2032 }
2033
2034 length = ntohs(oh->length);
2035 if (length < sizeof *oh) {
34582733 2036 ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1ac0e975
BP
2037 filename, length);
2038 }
2039
2040 tail_len = length - sizeof *oh;
2041 tail = ofpbuf_put_uninit(&b, tail_len);
2042 n = fread(tail, 1, tail_len, file);
2043 if (n < tail_len) {
2044 ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
2045 }
2046
6fd6ed71 2047 ofp_print(stdout, b.data, b.size, verbosity + 2);
1ac0e975
BP
2048 }
2049 ofpbuf_uninit(&b);
2050
2051 if (file != stdin) {
2052 fclose(file);
2053 }
2054}
2055
f3dd1419
BP
2056static bool
2057is_openflow_port(ovs_be16 port_, char *ports[])
2058{
2059 uint16_t port = ntohs(port_);
2060 if (ports[0]) {
2061 int i;
2062
2063 for (i = 0; ports[i]; i++) {
2064 if (port == atoi(ports[i])) {
2065 return true;
2066 }
2067 }
2068 return false;
2069 } else {
2070 return port == OFP_PORT || port == OFP_OLD_PORT;
2071 }
2072}
2073
2074static void
1636c761 2075ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx)
f3dd1419
BP
2076{
2077 struct tcp_reader *reader;
2078 FILE *file;
2079 int error;
2080 bool first;
2081
1636c761 2082 file = ovs_pcap_open(ctx->argv[1], "rb");
f3dd1419 2083 if (!file) {
1636c761 2084 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
f3dd1419
BP
2085 }
2086
2087 reader = tcp_reader_open();
2088 first = true;
2089 for (;;) {
cf62fa4c 2090 struct dp_packet *packet;
f3dd1419
BP
2091 long long int when;
2092 struct flow flow;
2093
50aa0364 2094 error = ovs_pcap_read(file, &packet, &when);
f3dd1419
BP
2095 if (error) {
2096 break;
2097 }
35303d71 2098 pkt_metadata_init(&packet->md, ODPP_NONE);
cf62fa4c 2099 flow_extract(packet, &flow);
f3dd1419
BP
2100 if (flow.dl_type == htons(ETH_TYPE_IP)
2101 && flow.nw_proto == IPPROTO_TCP
1636c761
RB
2102 && (is_openflow_port(flow.tp_src, ctx->argv + 2) ||
2103 is_openflow_port(flow.tp_dst, ctx->argv + 2))) {
cf62fa4c 2104 struct dp_packet *payload = tcp_reader_run(reader, &flow, packet);
f3dd1419 2105 if (payload) {
cf62fa4c 2106 while (dp_packet_size(payload) >= sizeof(struct ofp_header)) {
f3dd1419 2107 const struct ofp_header *oh;
cf62fa4c 2108 void *data = dp_packet_data(payload);
f3dd1419
BP
2109 int length;
2110
2111 /* Align OpenFlow on 8-byte boundary for safe access. */
cf62fa4c 2112 dp_packet_shift(payload, -((intptr_t) data & 7));
f3dd1419 2113
cf62fa4c 2114 oh = dp_packet_data(payload);
f3dd1419 2115 length = ntohs(oh->length);
cf62fa4c 2116 if (dp_packet_size(payload) < length) {
f3dd1419
BP
2117 break;
2118 }
2119
2120 if (!first) {
2121 putchar('\n');
2122 }
2123 first = false;
2124
2125 if (timestamp) {
2126 char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
2127 fputs(s, stdout);
2128 free(s);
2129 }
2130
2131 printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
2132 IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
2133 IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
cf62fa4c
PS
2134 ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1);
2135 dp_packet_pull(payload, length);
f3dd1419
BP
2136 }
2137 }
2138 }
cf62fa4c 2139 dp_packet_delete(packet);
f3dd1419
BP
2140 }
2141 tcp_reader_close(reader);
2142}
2143
064af421 2144static void
1636c761 2145ofctl_ping(struct ovs_cmdl_context *ctx)
064af421
BP
2146{
2147 size_t max_payload = 65535 - sizeof(struct ofp_header);
2148 unsigned int payload;
2149 struct vconn *vconn;
2150 int i;
2151
1636c761 2152 payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64;
064af421 2153 if (payload > max_payload) {
34582733 2154 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
064af421
BP
2155 }
2156
1636c761 2157 open_vconn(ctx->argv[1], &vconn);
064af421
BP
2158 for (i = 0; i < 10; i++) {
2159 struct timeval start, end;
2160 struct ofpbuf *request, *reply;
982697a4
BP
2161 const struct ofp_header *rpy_hdr;
2162 enum ofptype type;
064af421 2163
a53a8efa
SH
2164 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2165 vconn_get_version(vconn), payload);
982697a4 2166 random_bytes(ofpbuf_put_uninit(request, payload), payload);
064af421 2167
279c9e03 2168 xgettimeofday(&start);
064af421 2169 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
279c9e03 2170 xgettimeofday(&end);
064af421 2171
6fd6ed71 2172 rpy_hdr = reply->data;
982697a4
BP
2173 if (ofptype_pull(&type, reply)
2174 || type != OFPTYPE_ECHO_REPLY
6fd6ed71
PS
2175 || reply->size != payload
2176 || memcmp(request->msg, reply->msg, payload)) {
064af421 2177 printf("Reply does not match request. Request:\n");
6fd6ed71 2178 ofp_print(stdout, request, request->size, verbosity + 2);
064af421 2179 printf("Reply:\n");
6fd6ed71 2180 ofp_print(stdout, reply, reply->size, verbosity + 2);
064af421 2181 }
437d0d22 2182 printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1636c761 2183 reply->size, ctx->argv[1], ntohl(rpy_hdr->xid),
064af421
BP
2184 (1000*(double)(end.tv_sec - start.tv_sec))
2185 + (.001*(end.tv_usec - start.tv_usec)));
2186 ofpbuf_delete(request);
2187 ofpbuf_delete(reply);
2188 }
2189 vconn_close(vconn);
2190}
2191
2192static void
1636c761 2193ofctl_benchmark(struct ovs_cmdl_context *ctx)
064af421
BP
2194{
2195 size_t max_payload = 65535 - sizeof(struct ofp_header);
2196 struct timeval start, end;
2197 unsigned int payload_size, message_size;
2198 struct vconn *vconn;
2199 double duration;
2200 int count;
2201 int i;
2202
1636c761 2203 payload_size = atoi(ctx->argv[2]);
064af421 2204 if (payload_size > max_payload) {
34582733 2205 ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
064af421
BP
2206 }
2207 message_size = sizeof(struct ofp_header) + payload_size;
2208
1636c761 2209 count = atoi(ctx->argv[3]);
064af421
BP
2210
2211 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2212 count, message_size, count * message_size);
2213
1636c761 2214 open_vconn(ctx->argv[1], &vconn);
279c9e03 2215 xgettimeofday(&start);
064af421
BP
2216 for (i = 0; i < count; i++) {
2217 struct ofpbuf *request, *reply;
064af421 2218
a53a8efa
SH
2219 request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2220 vconn_get_version(vconn), payload_size);
982697a4 2221 ofpbuf_put_zeros(request, payload_size);
064af421
BP
2222 run(vconn_transact(vconn, request, &reply), "transact");
2223 ofpbuf_delete(reply);
2224 }
279c9e03 2225 xgettimeofday(&end);
064af421
BP
2226 vconn_close(vconn);
2227
2228 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2229 + (.001*(end.tv_usec - start.tv_usec)));
2230 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2231 duration, count / (duration / 1000.0),
2232 count * message_size / (duration / 1000.0));
2233}
2234
7395c052
NZ
2235static void
2236ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
69185eb2 2237 size_t n_gms, enum ofputil_protocol usable_protocols)
7395c052 2238{
69185eb2 2239 enum ofputil_protocol protocol;
7395c052 2240 struct ofputil_group_mod *gm;
69185eb2 2241 enum ofp_version version;
7395c052
NZ
2242 struct ofpbuf *request;
2243
2244 struct vconn *vconn;
2245 size_t i;
2246
69185eb2
SH
2247 protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2248 version = ofputil_protocol_to_ofp_version(protocol);
7395c052
NZ
2249
2250 for (i = 0; i < n_gms; i++) {
2251 gm = &gms[i];
69185eb2 2252 request = ofputil_encode_group_mod(version, gm);
7395c052
NZ
2253 if (request) {
2254 transact_noreply(vconn, request);
2255 }
2256 }
2257
2258 vconn_close(vconn);
2259
2260}
2261
2262
2263static void
2264ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2265{
2266 struct ofputil_group_mod *gms = NULL;
2267 enum ofputil_protocol usable_protocols;
2268 size_t n_gms = 0;
2269 char *error;
0da32511 2270 int i;
7395c052
NZ
2271
2272 error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2273 &usable_protocols);
2274 if (error) {
2275 ovs_fatal(0, "%s", error);
2276 }
69185eb2 2277 ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
0da32511
SH
2278 for (i = 0; i < n_gms; i++) {
2279 ofputil_bucket_list_destroy(&gms[i].buckets);
2280 }
7395c052
NZ
2281 free(gms);
2282}
2283
2284static void
2285ofctl_group_mod(int argc, char *argv[], uint16_t command)
2286{
2287 if (argc > 2 && !strcmp(argv[2], "-")) {
2288 ofctl_group_mod_file(argc, argv, command);
2289 } else {
2290 enum ofputil_protocol usable_protocols;
2291 struct ofputil_group_mod gm;
2292 char *error;
2293
2294 error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2295 &usable_protocols);
2296 if (error) {
2297 ovs_fatal(0, "%s", error);
2298 }
69185eb2 2299 ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
0da32511 2300 ofputil_bucket_list_destroy(&gm.buckets);
7395c052
NZ
2301 }
2302}
2303
2304static void
1636c761 2305ofctl_add_group(struct ovs_cmdl_context *ctx)
7395c052 2306{
1636c761 2307 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD);
7395c052
NZ
2308}
2309
2310static void
1636c761 2311ofctl_add_groups(struct ovs_cmdl_context *ctx)
7395c052 2312{
1636c761 2313 ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD);
7395c052
NZ
2314}
2315
2316static void
1636c761 2317ofctl_mod_group(struct ovs_cmdl_context *ctx)
7395c052 2318{
1636c761 2319 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_MODIFY);
7395c052
NZ
2320}
2321
2322static void
1636c761 2323ofctl_del_groups(struct ovs_cmdl_context *ctx)
7395c052 2324{
1636c761 2325 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE);
7395c052
NZ
2326}
2327
bdbb8426 2328static void
1636c761 2329ofctl_insert_bucket(struct ovs_cmdl_context *ctx)
bdbb8426 2330{
1636c761 2331 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET);
bdbb8426
SH
2332}
2333
2334static void
1636c761 2335ofctl_remove_bucket(struct ovs_cmdl_context *ctx)
bdbb8426 2336{
1636c761 2337 ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET);
bdbb8426
SH
2338}
2339
7395c052 2340static void
1636c761 2341ofctl_dump_group_stats(struct ovs_cmdl_context *ctx)
7395c052
NZ
2342{
2343 enum ofputil_protocol usable_protocols;
2344 struct ofputil_group_mod gm;
2345 struct ofpbuf *request;
2346 struct vconn *vconn;
2347 uint32_t group_id;
2348 char *error;
2349
2350 memset(&gm, 0, sizeof gm);
2351
2352 error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
1636c761 2353 ctx->argc > 2 ? ctx->argv[2] : "",
7395c052
NZ
2354 &usable_protocols);
2355 if (error) {
2356 ovs_fatal(0, "%s", error);
2357 }
2358
2359 group_id = gm.group_id;
2360
1636c761 2361 open_vconn(ctx->argv[1], &vconn);
7395c052
NZ
2362 request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2363 group_id);
2364 if (request) {
2365 dump_stats_transaction(vconn, request);
2366 }
2367
2368 vconn_close(vconn);
2369}
2370
2371static void
1636c761 2372ofctl_dump_group_desc(struct ovs_cmdl_context *ctx)
7395c052
NZ
2373{
2374 struct ofpbuf *request;
2375 struct vconn *vconn;
19187a71 2376 uint32_t group_id;
7395c052 2377
1636c761 2378 open_vconn(ctx->argv[1], &vconn);
7395c052 2379
1636c761 2380 if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) {
19187a71
BP
2381 group_id = OFPG11_ALL;
2382 }
2383
2384 request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2385 group_id);
7395c052
NZ
2386 if (request) {
2387 dump_stats_transaction(vconn, request);
2388 }
2389
2390 vconn_close(vconn);
2391}
2392
2393static void
1636c761 2394ofctl_dump_group_features(struct ovs_cmdl_context *ctx)
7395c052
NZ
2395{
2396 struct ofpbuf *request;
2397 struct vconn *vconn;
2398
1636c761 2399 open_vconn(ctx->argv[1], &vconn);
7395c052
NZ
2400 request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2401 if (request) {
2402 dump_stats_transaction(vconn, request);
2403 }
2404
2405 vconn_close(vconn);
2406}
2407
6159c531
JG
2408static void
2409ofctl_geneve_mod(struct ovs_cmdl_context *ctx, uint16_t command)
2410{
2411 enum ofputil_protocol usable_protocols;
2412 enum ofputil_protocol protocol;
2413 struct ofputil_geneve_table_mod gtm;
2414 char *error;
2415 enum ofp_version version;
2416 struct ofpbuf *request;
2417 struct vconn *vconn;
2418
2419 error = parse_ofp_geneve_table_mod_str(&gtm, command, ctx->argc > 2 ?
2420 ctx->argv[2] : "",
2421 &usable_protocols);
2422 if (error) {
2423 ovs_fatal(0, "%s", error);
2424 }
2425
2426 protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols);
2427 version = ofputil_protocol_to_ofp_version(protocol);
2428
2429 request = ofputil_encode_geneve_table_mod(version, &gtm);
2430 if (request) {
2431 transact_noreply(vconn, request);
2432 }
2433
2434 vconn_close(vconn);
2435 ofputil_uninit_geneve_table(&gtm.mappings);
2436}
2437
2438static void
2439ofctl_add_geneve_map(struct ovs_cmdl_context *ctx)
2440{
2441 ofctl_geneve_mod(ctx, NXGTMC_ADD);
2442}
2443
2444static void
2445ofctl_del_geneve_map(struct ovs_cmdl_context *ctx)
2446{
2447 ofctl_geneve_mod(ctx, ctx->argc > 2 ? NXGTMC_DELETE : NXGTMC_CLEAR);
2448}
2449
2450static void
2451ofctl_dump_geneve_map(struct ovs_cmdl_context *ctx)
2452{
2453 dump_trivial_transaction(ctx->argv[1], OFPRAW_NXT_GENEVE_TABLE_REQUEST);
2454}
2455
09246b99 2456static void
1636c761 2457ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
09246b99
BP
2458{
2459 usage();
2460}
451de37e
AW
2461
2462static void
1636c761 2463ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
451de37e 2464{
5f383751 2465 ovs_cmdl_print_commands(get_all_commands());
451de37e 2466}
09246b99 2467\f
0199c526
BP
2468/* replace-flows and diff-flows commands. */
2469
2470/* A flow table entry, possibly with two different versions. */
2471struct fte {
2472 struct cls_rule rule; /* Within a "struct classifier". */
2473 struct fte_version *versions[2];
2474};
2475
2476/* One version of a Flow Table Entry. */
2477struct fte_version {
2478 ovs_be64 cookie;
2479 uint16_t idle_timeout;
2480 uint16_t hard_timeout;
ca26eb44 2481 uint16_t importance;
0199c526 2482 uint16_t flags;
f25d0cf3
BP
2483 struct ofpact *ofpacts;
2484 size_t ofpacts_len;
0199c526
BP
2485};
2486
2487/* Frees 'version' and the data that it owns. */
2488static void
2489fte_version_free(struct fte_version *version)
2490{
2491 if (version) {
dc723c44 2492 free(CONST_CAST(struct ofpact *, version->ofpacts));
0199c526
BP
2493 free(version);
2494 }
2495}
2496
2497/* Returns true if 'a' and 'b' are the same, false if they differ.
2498 *
2499 * Ignores differences in 'flags' because there's no way to retrieve flags from
2500 * an OpenFlow switch. We have to assume that they are the same. */
2501static bool
2502fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2503{
2504 return (a->cookie == b->cookie
2505 && a->idle_timeout == b->idle_timeout
2506 && a->hard_timeout == b->hard_timeout
ca26eb44 2507 && a->importance == b->importance
f25d0cf3
BP
2508 && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2509 b->ofpacts, b->ofpacts_len));
0199c526
BP
2510}
2511
98f7f427
BP
2512/* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2513 * 'index' into 's', followed by a new-line. */
0199c526 2514static void
98f7f427 2515fte_version_format(const struct fte *fte, int index, struct ds *s)
0199c526 2516{
98f7f427 2517 const struct fte_version *version = fte->versions[index];
0199c526 2518
98f7f427
BP
2519 ds_clear(s);
2520 if (!version) {
2521 return;
2522 }
2523
2524 cls_rule_format(&fte->rule, s);
0199c526 2525 if (version->cookie != htonll(0)) {
98f7f427 2526 ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
0199c526
BP
2527 }
2528 if (version->idle_timeout != OFP_FLOW_PERMANENT) {
98f7f427 2529 ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
0199c526
BP
2530 }
2531 if (version->hard_timeout != OFP_FLOW_PERMANENT) {
98f7f427 2532 ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
0199c526 2533 }
ca26eb44
RB
2534 if (version->importance != 0) {
2535 ds_put_format(s, " importance=%"PRIu16, version->importance);
2536 }
0199c526 2537
455ecd77 2538 ds_put_cstr(s, " actions=");
98f7f427
BP
2539 ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2540
2541 ds_put_char(s, '\n');
0199c526
BP
2542}
2543
2544static struct fte *
2545fte_from_cls_rule(const struct cls_rule *cls_rule)
2546{
2547 return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2548}
2549
2550/* Frees 'fte' and its versions. */
2551static void
2552fte_free(struct fte *fte)
2553{
2554 if (fte) {
2555 fte_version_free(fte->versions[0]);
2556 fte_version_free(fte->versions[1]);
48d28ac1 2557 cls_rule_destroy(&fte->rule);
0199c526
BP
2558 free(fte);
2559 }
2560}
2561
2562/* Frees all of the FTEs within 'cls'. */
2563static void
2564fte_free_all(struct classifier *cls)
2565{
78c8df12 2566 struct fte *fte;
0199c526 2567
802f84ff 2568 classifier_defer(cls);
de4ad4a2 2569 CLS_FOR_EACH (fte, rule, cls) {
0199c526 2570 classifier_remove(cls, &fte->rule);
de4ad4a2 2571 ovsrcu_postpone(fte_free, fte);
0199c526 2572 }
e49190c4 2573 classifier_destroy(cls);
0199c526
BP
2574}
2575
2576/* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2577 * necessary. Sets 'version' as the version of that rule with the given
2578 * 'index', replacing any existing version, if any.
2579 *
2580 * Takes ownership of 'version'. */
2581static void
81a76618 2582fte_insert(struct classifier *cls, const struct match *match,
eb391b76 2583 int priority, struct fte_version *version, int index)
0199c526
BP
2584{
2585 struct fte *old, *fte;
2586
2587 fte = xzalloc(sizeof *fte);
bd53aa17 2588 cls_rule_init(&fte->rule, match, priority);
0199c526
BP
2589 fte->versions[index] = version;
2590
bd53aa17
JR
2591 old = fte_from_cls_rule(classifier_replace(cls, &fte->rule,
2592 CLS_MIN_VERSION, NULL, 0));
0199c526 2593 if (old) {
0199c526 2594 fte->versions[!index] = old->versions[!index];
de4ad4a2
JR
2595 old->versions[!index] = NULL;
2596
2597 ovsrcu_postpone(fte_free, old);
0199c526
BP
2598 }
2599}
2600
2601/* Reads the flows in 'filename' as flow table entries in 'cls' for the version
27527aa0
BP
2602 * with the specified 'index'. Returns the flow formats able to represent the
2603 * flows that were read. */
2604static enum ofputil_protocol
0199c526
BP
2605read_flows_from_file(const char *filename, struct classifier *cls, int index)
2606{
27527aa0 2607 enum ofputil_protocol usable_protocols;
bdda5aca 2608 int line_number;
0199c526
BP
2609 struct ds s;
2610 FILE *file;
2611
2612 file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2613 if (file == NULL) {
2614 ovs_fatal(errno, "%s: open", filename);
2615 }
2616
2617 ds_init(&s);
27527aa0 2618 usable_protocols = OFPUTIL_P_ANY;
bdda5aca 2619 line_number = 0;
802f84ff 2620 classifier_defer(cls);
bdda5aca 2621 while (!ds_get_preprocessed_line(&s, file, &line_number)) {
0199c526 2622 struct fte_version *version;
a9a2da38 2623 struct ofputil_flow_mod fm;
bdda5aca 2624 char *error;
db0b6c29 2625 enum ofputil_protocol usable;
0199c526 2626
ba2fe8e9 2627 error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
bdda5aca
BP
2628 if (error) {
2629 ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2630 }
db0b6c29 2631 usable_protocols &= usable;
0199c526
BP
2632
2633 version = xmalloc(sizeof *version);
623e1caf 2634 version->cookie = fm.new_cookie;
0199c526
BP
2635 version->idle_timeout = fm.idle_timeout;
2636 version->hard_timeout = fm.hard_timeout;
ca26eb44 2637 version->importance = fm.importance;
0fb88c18
BP
2638 version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2639 | OFPUTIL_FF_EMERG);
f25d0cf3
BP
2640 version->ofpacts = fm.ofpacts;
2641 version->ofpacts_len = fm.ofpacts_len;
0199c526 2642
81a76618 2643 fte_insert(cls, &fm.match, fm.priority, version, index);
0199c526 2644 }
802f84ff 2645 classifier_publish(cls);
0199c526
BP
2646 ds_destroy(&s);
2647
2648 if (file != stdin) {
2649 fclose(file);
2650 }
2651
27527aa0 2652 return usable_protocols;
0199c526
BP
2653}
2654
4ce9c315
BP
2655static bool
2656recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2657 struct ofpbuf **replyp,
2658 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2659{
2660 struct ofpbuf *reply = *replyp;
2661
2662 for (;;) {
4ce9c315 2663 int retval;
35805806 2664 bool more;
4ce9c315
BP
2665
2666 /* Get a flow stats reply message, if we don't already have one. */
2667 if (!reply) {
982697a4
BP
2668 enum ofptype type;
2669 enum ofperr error;
4ce9c315
BP
2670
2671 do {
2672 run(vconn_recv_block(vconn, &reply),
2673 "OpenFlow packet receive failed");
6fd6ed71 2674 } while (((struct ofp_header *) reply->data)->xid != send_xid);
4ce9c315 2675
6fd6ed71 2676 error = ofptype_decode(&type, reply->data);
982697a4 2677 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
4ce9c315 2678 ovs_fatal(0, "received bad reply: %s",
6fd6ed71 2679 ofp_to_string(reply->data, reply->size,
4ce9c315
BP
2680 verbosity + 1));
2681 }
2682 }
2683
2684 /* Pull an individual flow stats reply out of the message. */
2685 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2686 switch (retval) {
2687 case 0:
2688 *replyp = reply;
2689 return true;
2690
2691 case EOF:
6fd6ed71 2692 more = ofpmp_more(reply->header);
4ce9c315 2693 ofpbuf_delete(reply);
296ed880 2694 reply = NULL;
35805806 2695 if (!more) {
4ce9c315
BP
2696 *replyp = NULL;
2697 return false;
2698 }
2699 break;
2700
2701 default:
2702 ovs_fatal(0, "parse error in reply (%s)",
2703 ofperr_to_string(retval));
2704 }
2705 }
2706}
2707
0199c526 2708/* Reads the OpenFlow flow table from 'vconn', which has currently active flow
27527aa0 2709 * format 'protocol', and adds them as flow table entries in 'cls' for the
0199c526
BP
2710 * version with the specified 'index'. */
2711static void
27527aa0
BP
2712read_flows_from_switch(struct vconn *vconn,
2713 enum ofputil_protocol protocol,
0199c526
BP
2714 struct classifier *cls, int index)
2715{
81d1ea94 2716 struct ofputil_flow_stats_request fsr;
4ce9c315 2717 struct ofputil_flow_stats fs;
0199c526 2718 struct ofpbuf *request;
4ce9c315
BP
2719 struct ofpbuf ofpacts;
2720 struct ofpbuf *reply;
0199c526 2721 ovs_be32 send_xid;
0199c526
BP
2722
2723 fsr.aggregate = false;
81a76618 2724 match_init_catchall(&fsr.match);
7f05e7ab 2725 fsr.out_port = OFPP_ANY;
0199c526 2726 fsr.table_id = 0xff;
ecc798ab 2727 fsr.cookie = fsr.cookie_mask = htonll(0);
27527aa0 2728 request = ofputil_encode_flow_stats_request(&fsr, protocol);
6fd6ed71 2729 send_xid = ((struct ofp_header *) request->data)->xid;
0199c526
BP
2730 send_openflow_buffer(vconn, request);
2731
4ce9c315
BP
2732 reply = NULL;
2733 ofpbuf_init(&ofpacts, 0);
802f84ff 2734 classifier_defer(cls);
4ce9c315
BP
2735 while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2736 struct fte_version *version;
0199c526 2737
4ce9c315
BP
2738 version = xmalloc(sizeof *version);
2739 version->cookie = fs.cookie;
2740 version->idle_timeout = fs.idle_timeout;
2741 version->hard_timeout = fs.hard_timeout;
ca26eb44 2742 version->importance = fs.importance;
4ce9c315
BP
2743 version->flags = 0;
2744 version->ofpacts_len = fs.ofpacts_len;
2745 version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2746
81a76618 2747 fte_insert(cls, &fs.match, fs.priority, version, index);
0199c526 2748 }
802f84ff 2749 classifier_publish(cls);
4ce9c315 2750 ofpbuf_uninit(&ofpacts);
0199c526
BP
2751}
2752
2753static void
2754fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
ca6ba700 2755 enum ofputil_protocol protocol, struct ovs_list *packets)
0199c526
BP
2756{
2757 const struct fte_version *version = fte->versions[index];
a9a2da38 2758 struct ofputil_flow_mod fm;
0199c526
BP
2759 struct ofpbuf *ofm;
2760
5cb7a798 2761 minimatch_expand(&fte->rule.match, &fm.match);
81a76618 2762 fm.priority = fte->rule.priority;
623e1caf
JP
2763 fm.cookie = htonll(0);
2764 fm.cookie_mask = htonll(0);
2765 fm.new_cookie = version->cookie;
23342857 2766 fm.modify_cookie = true;
6c1491fb 2767 fm.table_id = 0xff;
0199c526
BP
2768 fm.command = command;
2769 fm.idle_timeout = version->idle_timeout;
2770 fm.hard_timeout = version->hard_timeout;
ca26eb44 2771 fm.importance = version->importance;
0199c526 2772 fm.buffer_id = UINT32_MAX;
7f05e7ab 2773 fm.out_port = OFPP_ANY;
0199c526
BP
2774 fm.flags = version->flags;
2775 if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2776 command == OFPFC_MODIFY_STRICT) {
f25d0cf3
BP
2777 fm.ofpacts = version->ofpacts;
2778 fm.ofpacts_len = version->ofpacts_len;
0199c526 2779 } else {
f25d0cf3
BP
2780 fm.ofpacts = NULL;
2781 fm.ofpacts_len = 0;
0199c526 2782 }
cc40d06b 2783 fm.delete_reason = OFPRR_DELETE;
0199c526 2784
27527aa0 2785 ofm = ofputil_encode_flow_mod(&fm, protocol);
0199c526
BP
2786 list_push_back(packets, &ofm->list_node);
2787}
2788
2789static void
1636c761 2790ofctl_replace_flows(struct ovs_cmdl_context *ctx)
0199c526
BP
2791{
2792 enum { FILE_IDX = 0, SWITCH_IDX = 1 };
27527aa0 2793 enum ofputil_protocol usable_protocols, protocol;
0199c526 2794 struct classifier cls;
ca6ba700 2795 struct ovs_list requests;
0199c526
BP
2796 struct vconn *vconn;
2797 struct fte *fte;
2798
476f36e8 2799 classifier_init(&cls, NULL);
1636c761 2800 usable_protocols = read_flows_from_file(ctx->argv[2], &cls, FILE_IDX);
0199c526 2801
1636c761 2802 protocol = open_vconn(ctx->argv[1], &vconn);
27527aa0
BP
2803 protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2804
2805 read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
0199c526
BP
2806
2807 list_init(&requests);
2808
2809 /* Delete flows that exist on the switch but not in the file. */
5f0476ce 2810 CLS_FOR_EACH (fte, rule, &cls) {
0199c526
BP
2811 struct fte_version *file_ver = fte->versions[FILE_IDX];
2812 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2813
2814 if (sw_ver && !file_ver) {
2815 fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
27527aa0 2816 protocol, &requests);
0199c526
BP
2817 }
2818 }
2819
2820 /* Add flows that exist in the file but not on the switch.
2821 * Update flows that exist in both places but differ. */
5f0476ce 2822 CLS_FOR_EACH (fte, rule, &cls) {
0199c526
BP
2823 struct fte_version *file_ver = fte->versions[FILE_IDX];
2824 struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2825
c4ea79bf
BP
2826 if (file_ver
2827 && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
27527aa0 2828 fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
0199c526
BP
2829 }
2830 }
db5076ee 2831 if (bundle) {
39c94593 2832 bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC);
db5076ee
JR
2833 } else {
2834 transact_multiple_noreply(vconn, &requests);
2835 }
0199c526
BP
2836 vconn_close(vconn);
2837
2838 fte_free_all(&cls);
2839}
2840
2841static void
2842read_flows_from_source(const char *source, struct classifier *cls, int index)
2843{
2844 struct stat s;
2845
2846 if (source[0] == '/' || source[0] == '.'
2847 || (!strchr(source, ':') && !stat(source, &s))) {
2848 read_flows_from_file(source, cls, index);
2849 } else {
27527aa0 2850 enum ofputil_protocol protocol;
0199c526
BP
2851 struct vconn *vconn;
2852
27527aa0
BP
2853 protocol = open_vconn(source, &vconn);
2854 protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2855 read_flows_from_switch(vconn, protocol, cls, index);
0199c526
BP
2856 vconn_close(vconn);
2857 }
2858}
2859
2860static void
1636c761 2861ofctl_diff_flows(struct ovs_cmdl_context *ctx)
0199c526
BP
2862{
2863 bool differences = false;
0199c526 2864 struct classifier cls;
98f7f427 2865 struct ds a_s, b_s;
0199c526
BP
2866 struct fte *fte;
2867
476f36e8 2868 classifier_init(&cls, NULL);
1636c761
RB
2869 read_flows_from_source(ctx->argv[1], &cls, 0);
2870 read_flows_from_source(ctx->argv[2], &cls, 1);
0199c526 2871
98f7f427
BP
2872 ds_init(&a_s);
2873 ds_init(&b_s);
2874
5f0476ce 2875 CLS_FOR_EACH (fte, rule, &cls) {
0199c526
BP
2876 struct fte_version *a = fte->versions[0];
2877 struct fte_version *b = fte->versions[1];
2878
2879 if (!a || !b || !fte_version_equals(a, b)) {
98f7f427
BP
2880 fte_version_format(fte, 0, &a_s);
2881 fte_version_format(fte, 1, &b_s);
2882 if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2883 if (a_s.length) {
2884 printf("-%s", ds_cstr(&a_s));
2885 }
2886 if (b_s.length) {
2887 printf("+%s", ds_cstr(&b_s));
2888 }
2889 differences = true;
0199c526 2890 }
0199c526
BP
2891 }
2892 }
2893
98f7f427
BP
2894 ds_destroy(&a_s);
2895 ds_destroy(&b_s);
2896
0199c526
BP
2897 fte_free_all(&cls);
2898
2899 if (differences) {
2900 exit(2);
2901 }
2902}
3200ed58
JR
2903
2904static void
2905ofctl_meter_mod__(const char *bridge, const char *str, int command)
2906{
2907 struct ofputil_meter_mod mm;
2908 struct vconn *vconn;
2909 enum ofputil_protocol protocol;
2910 enum ofputil_protocol usable_protocols;
2911 enum ofp_version version;
2912
2913 if (str) {
2914 char *error;
2915 error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2916 if (error) {
2917 ovs_fatal(0, "%s", error);
2918 }
2919 } else {
2920 usable_protocols = OFPUTIL_P_OF13_UP;
2921 mm.command = command;
2922 mm.meter.meter_id = OFPM13_ALL;
2923 }
2924
2925 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2926 version = ofputil_protocol_to_ofp_version(protocol);
2927 transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2928 vconn_close(vconn);
2929}
2930
2931static void
2932ofctl_meter_request__(const char *bridge, const char *str,
2933 enum ofputil_meter_request_type type)
2934{
2935 struct ofputil_meter_mod mm;
2936 struct vconn *vconn;
2937 enum ofputil_protocol usable_protocols;
2938 enum ofputil_protocol protocol;
2939 enum ofp_version version;
2940
2941 if (str) {
2942 char *error;
2943 error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2944 if (error) {
2945 ovs_fatal(0, "%s", error);
2946 }
2947 } else {
2948 usable_protocols = OFPUTIL_P_OF13_UP;
2949 mm.meter.meter_id = OFPM13_ALL;
2950 }
2951
2952 protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2953 version = ofputil_protocol_to_ofp_version(protocol);
2954 transact_noreply(vconn, ofputil_encode_meter_request(version,
2955 type,
2956 mm.meter.meter_id));
2957 vconn_close(vconn);
2958}
2959
2960
2961static void
1636c761 2962ofctl_add_meter(struct ovs_cmdl_context *ctx)
3200ed58 2963{
1636c761 2964 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD);
3200ed58
JR
2965}
2966
2967static void
1636c761 2968ofctl_mod_meter(struct ovs_cmdl_context *ctx)
3200ed58 2969{
1636c761 2970 ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY);
3200ed58
JR
2971}
2972
2973static void
1636c761 2974ofctl_del_meters(struct ovs_cmdl_context *ctx)
3200ed58 2975{
1636c761 2976 ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE);
3200ed58
JR
2977}
2978
2979static void
1636c761 2980ofctl_dump_meters(struct ovs_cmdl_context *ctx)
3200ed58 2981{
1636c761 2982 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3200ed58
JR
2983 OFPUTIL_METER_CONFIG);
2984}
2985
2986static void
1636c761 2987ofctl_meter_stats(struct ovs_cmdl_context *ctx)
3200ed58 2988{
1636c761 2989 ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
3200ed58
JR
2990 OFPUTIL_METER_STATS);
2991}
2992
2993static void
1636c761 2994ofctl_meter_features(struct ovs_cmdl_context *ctx)
3200ed58 2995{
1636c761 2996 ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES);
3200ed58
JR
2997}
2998
0199c526 2999\f
09246b99
BP
3000/* Undocumented commands for unit testing. */
3001
770f1f66 3002static void
db0b6c29
JR
3003ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
3004 enum ofputil_protocol usable_protocols)
770f1f66 3005{
27527aa0
BP
3006 enum ofputil_protocol protocol = 0;
3007 char *usable_s;
3008 size_t i;
770f1f66 3009
27527aa0
BP
3010 usable_s = ofputil_protocols_to_string(usable_protocols);
3011 printf("usable protocols: %s\n", usable_s);
3012 free(usable_s);
3013
3014 if (!(usable_protocols & allowed_protocols)) {
3015 ovs_fatal(0, "no usable protocol");
3016 }
3017 for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
3018 protocol = 1 << i;
3019 if (protocol & usable_protocols & allowed_protocols) {
3020 break;
3021 }
3022 }
cc2862a9 3023 ovs_assert(is_pow2(protocol));
27527aa0
BP
3024
3025 printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
3026
3027 for (i = 0; i < n_fms; i++) {
3028 struct ofputil_flow_mod *fm = &fms[i];
3029 struct ofpbuf *msg;
3030
3031 msg = ofputil_encode_flow_mod(fm, protocol);
6fd6ed71 3032 ofp_print(stdout, msg->data, msg->size, verbosity);
27527aa0
BP
3033 ofpbuf_delete(msg);
3034
dc723c44 3035 free(CONST_CAST(struct ofpact *, fm->ofpacts));
770f1f66
BP
3036 }
3037}
3038
3039/* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
3040 * it back to stdout. */
3041static void
1636c761 3042ofctl_parse_flow(struct ovs_cmdl_context *ctx)
770f1f66 3043{
db0b6c29 3044 enum ofputil_protocol usable_protocols;
27527aa0 3045 struct ofputil_flow_mod fm;
bdda5aca 3046 char *error;
770f1f66 3047
1636c761 3048 error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols);
bdda5aca
BP
3049 if (error) {
3050 ovs_fatal(0, "%s", error);
3051 }
db0b6c29 3052 ofctl_parse_flows__(&fm, 1, usable_protocols);
770f1f66
BP
3053}
3054
fec00620
BP
3055/* "parse-flows FILENAME": reads the named file as a sequence of flows (like
3056 * add-flows) and prints each of the flows back to stdout. */
0e581146 3057static void
1636c761 3058ofctl_parse_flows(struct ovs_cmdl_context *ctx)
0e581146 3059{
db0b6c29 3060 enum ofputil_protocol usable_protocols;
27527aa0
BP
3061 struct ofputil_flow_mod *fms = NULL;
3062 size_t n_fms = 0;
bdda5aca 3063 char *error;
0e581146 3064
1636c761 3065 error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms,
ba2fe8e9 3066 &usable_protocols);
bdda5aca
BP
3067 if (error) {
3068 ovs_fatal(0, "%s", error);
3069 }
db0b6c29 3070 ofctl_parse_flows__(fms, n_fms, usable_protocols);
27527aa0 3071 free(fms);
0e581146
BP
3072}
3073
064af421 3074static void
9d84066c 3075ofctl_parse_nxm__(bool oxm, enum ofp_version version)
09246b99
BP
3076{
3077 struct ds in;
3078
3079 ds_init(&in);
06d7ae7d 3080 while (!ds_get_test_line(&in, stdin)) {
09246b99 3081 struct ofpbuf nx_match;
81a76618 3082 struct match match;
e729e793 3083 ovs_be64 cookie, cookie_mask;
90bf1e07 3084 enum ofperr error;
09246b99 3085 int match_len;
09246b99
BP
3086
3087 /* Convert string to nx_match. */
3088 ofpbuf_init(&nx_match, 0);
7623f4dd
SH
3089 if (oxm) {
3090 match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
3091 } else {
3092 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
3093 }
09246b99 3094
81a76618 3095 /* Convert nx_match to match. */
102ce766 3096 if (strict) {
7623f4dd 3097 if (oxm) {
81a76618 3098 error = oxm_pull_match(&nx_match, &match);
7623f4dd 3099 } else {
81a76618 3100 error = nx_pull_match(&nx_match, match_len, &match,
7623f4dd
SH
3101 &cookie, &cookie_mask);
3102 }
102ce766 3103 } else {
7623f4dd 3104 if (oxm) {
81a76618 3105 error = oxm_pull_match_loose(&nx_match, &match);
7623f4dd 3106 } else {
81a76618 3107 error = nx_pull_match_loose(&nx_match, match_len, &match,
7623f4dd
SH
3108 &cookie, &cookie_mask);
3109 }
102ce766
EJ
3110 }
3111
7623f4dd 3112
09246b99
BP
3113 if (!error) {
3114 char *out;
3115
81a76618 3116 /* Convert match back to nx_match. */
09246b99
BP
3117 ofpbuf_uninit(&nx_match);
3118 ofpbuf_init(&nx_match, 0);
7623f4dd 3119 if (oxm) {
9d84066c 3120 match_len = oxm_put_match(&nx_match, &match, version);
db5a1019 3121 out = oxm_match_to_string(&nx_match, match_len);
7623f4dd 3122 } else {
81a76618 3123 match_len = nx_put_match(&nx_match, &match,
7623f4dd 3124 cookie, cookie_mask);
6fd6ed71 3125 out = nx_match_to_string(nx_match.data, match_len);
7623f4dd 3126 }
09246b99 3127
09246b99
BP
3128 puts(out);
3129 free(out);
508a9338
BP
3130
3131 if (verbosity > 0) {
6fd6ed71 3132 ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false);
508a9338 3133 }
09246b99 3134 } else {
90bf1e07
BP
3135 printf("nx_pull_match() returned error %s\n",
3136 ofperr_get_name(error));
09246b99
BP
3137 }
3138
3139 ofpbuf_uninit(&nx_match);
3140 }
3141 ds_destroy(&in);
064af421
BP
3142}
3143
b5ae8913
SH
3144/* "parse-nxm": reads a series of NXM nx_match specifications as strings from
3145 * stdin, does some internal fussing with them, and then prints them back as
3146 * strings on stdout. */
3147static void
1636c761 3148ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED)
b5ae8913 3149{
2f4aba0b 3150 ofctl_parse_nxm__(false, 0);
b5ae8913
SH
3151}
3152
9d84066c
BP
3153/* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
3154 * strings from stdin, does some internal fussing with them, and then prints
3155 * them back as strings on stdout. VERSION must specify an OpenFlow version,
3156 * e.g. "OpenFlow12". */
b5ae8913 3157static void
1636c761 3158ofctl_parse_oxm(struct ovs_cmdl_context *ctx)
b5ae8913 3159{
1636c761 3160 enum ofp_version version = ofputil_version_from_string(ctx->argv[1]);
9d84066c 3161 if (version < OFP12_VERSION) {
1636c761 3162 ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]);
9d84066c
BP
3163 }
3164
2f4aba0b 3165 ofctl_parse_nxm__(true, version);
b5ae8913
SH
3166}
3167
f25d0cf3 3168static void
96628ae8
BP
3169print_differences(const char *prefix,
3170 const void *a_, size_t a_len,
f25d0cf3
BP
3171 const void *b_, size_t b_len)
3172{
3173 const uint8_t *a = a_;
3174 const uint8_t *b = b_;
3175 size_t i;
3176
3177 for (i = 0; i < MIN(a_len, b_len); i++) {
3178 if (a[i] != b[i]) {
34582733 3179 printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
96628ae8 3180 prefix, i, a[i], b[i]);
f25d0cf3
BP
3181 }
3182 }
3183 for (i = a_len; i < b_len; i++) {
34582733 3184 printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
f25d0cf3
BP
3185 }
3186 for (i = b_len; i < a_len; i++) {
34582733 3187 printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
f25d0cf3
BP
3188 }
3189}
3190
f25d0cf3 3191static void
65a8a59e 3192ofctl_parse_actions__(const char *version_s, bool instructions)
f25d0cf3 3193{
65a8a59e 3194 enum ofp_version version;
f25d0cf3
BP
3195 struct ds in;
3196
65a8a59e
BP
3197 version = ofputil_version_from_string(version_s);
3198 if (!version) {
3199 ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3200 }
3201
f25d0cf3 3202 ds_init(&in);
bdda5aca 3203 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
65a8a59e
BP
3204 struct ofpbuf of_out;
3205 struct ofpbuf of_in;
f25d0cf3 3206 struct ofpbuf ofpacts;
65a8a59e
BP
3207 const char *table_id;
3208 char *actions;
f25d0cf3
BP
3209 enum ofperr error;
3210 size_t size;
3211 struct ds s;
3212
65a8a59e
BP
3213 /* Parse table_id separated with the follow-up actions by ",", if
3214 * any. */
3215 actions = ds_cstr(&in);
3216 table_id = NULL;
3217 if (strstr(actions, ",")) {
3218 table_id = strsep(&actions, ",");
3219 }
3220
f25d0cf3 3221 /* Parse hex bytes. */
65a8a59e
BP
3222 ofpbuf_init(&of_in, 0);
3223 if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
f25d0cf3
BP
3224 ovs_fatal(0, "Trailing garbage in hex data");
3225 }
3226
3227 /* Convert to ofpacts. */
3228 ofpbuf_init(&ofpacts, 0);
6fd6ed71 3229 size = of_in.size;
8f2cded4
BP
3230 error = (instructions
3231 ? ofpacts_pull_openflow_instructions
3232 : ofpacts_pull_openflow_actions)(
6fd6ed71 3233 &of_in, of_in.size, version, &ofpacts);
65a8a59e
BP
3234 if (!error && instructions) {
3235 /* Verify actions, enforce consistency. */
3236 enum ofputil_protocol protocol;
3237 struct flow flow;
3238
3239 memset(&flow, 0, sizeof flow);
3240 protocol = ofputil_protocols_from_ofp_version(version);
6fd6ed71 3241 error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
65a8a59e
BP
3242 &flow, OFPP_MAX,
3243 table_id ? atoi(table_id) : 0,
3244 255, protocol);
3245 }
f25d0cf3 3246 if (error) {
65a8a59e
BP
3247 printf("bad %s %s: %s\n\n",
3248 version_s, instructions ? "instructions" : "actions",
3249 ofperr_get_name(error));
f25d0cf3 3250 ofpbuf_uninit(&ofpacts);
65a8a59e 3251 ofpbuf_uninit(&of_in);
f25d0cf3
BP
3252 continue;
3253 }
65a8a59e 3254 ofpbuf_push_uninit(&of_in, size);
f25d0cf3
BP
3255
3256 /* Print cls_rule. */
3257 ds_init(&s);
455ecd77 3258 ds_put_cstr(&s, "actions=");
6fd6ed71 3259 ofpacts_format(ofpacts.data, ofpacts.size, &s);
f25d0cf3
BP
3260 puts(ds_cstr(&s));
3261 ds_destroy(&s);
3262
3263 /* Convert back to ofp10 actions and print differences from input. */
65a8a59e
BP
3264 ofpbuf_init(&of_out, 0);
3265 if (instructions) {
6fd6ed71
PS
3266 ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3267 &of_out, version);
65a8a59e 3268 } else {
6fd6ed71 3269 ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size,
65a8a59e
BP
3270 &of_out, version);
3271 }
f25d0cf3 3272
6fd6ed71
PS
3273 print_differences("", of_in.data, of_in.size,
3274 of_out.data, of_out.size);
f25d0cf3
BP
3275 putchar('\n');
3276
3277 ofpbuf_uninit(&ofpacts);
65a8a59e
BP
3278 ofpbuf_uninit(&of_in);
3279 ofpbuf_uninit(&of_out);
f25d0cf3
BP
3280 }
3281 ds_destroy(&in);
3282}
3283
65a8a59e
BP
3284/* "parse-actions VERSION": reads a series of action specifications for the
3285 * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3286 * prints them as strings on stdout, and then converts them back to hex bytes
3287 * and prints any differences from the input. */
8f2cded4 3288static void
1636c761 3289ofctl_parse_actions(struct ovs_cmdl_context *ctx)
8f2cded4 3290{
1636c761 3291 ofctl_parse_actions__(ctx->argv[1], false);
8f2cded4
BP
3292}
3293
65a8a59e
BP
3294/* "parse-actions VERSION": reads a series of instruction specifications for
3295 * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3296 * ofpacts, prints them as strings on stdout, and then converts them back to
3297 * hex bytes and prints any differences from the input. */
8f2cded4 3298static void
1636c761 3299ofctl_parse_instructions(struct ovs_cmdl_context *ctx)
8f2cded4 3300{
1636c761 3301 ofctl_parse_actions__(ctx->argv[1], true);
8f2cded4
BP
3302}
3303
96628ae8
BP
3304/* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3305 * bytes from stdin, converts them to cls_rules, prints them as strings on
3306 * stdout, and then converts them back to hex bytes and prints any differences
8812ec2c
BP
3307 * from the input.
3308 *
3309 * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3310 * values are ignored in the input and will be set to zero when OVS converts
3311 * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when
3312 * it does the conversion to hex, to ensure that in fact they are ignored. */
96628ae8 3313static void
1636c761 3314ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
96628ae8 3315{
8812ec2c 3316 struct ds expout;
96628ae8
BP
3317 struct ds in;
3318
3319 ds_init(&in);
8812ec2c 3320 ds_init(&expout);
bdda5aca 3321 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
8812ec2c 3322 struct ofpbuf match_in, match_expout;
96628ae8
BP
3323 struct ofp10_match match_out;
3324 struct ofp10_match match_normal;
81a76618 3325 struct match match;
8812ec2c
BP
3326 char *p;
3327
3328 /* Parse hex bytes to use for expected output. */
3329 ds_clear(&expout);
3330 ds_put_cstr(&expout, ds_cstr(&in));
3331 for (p = ds_cstr(&expout); *p; p++) {
3332 if (*p == 'x') {
3333 *p = '0';
3334 }
3335 }
3336 ofpbuf_init(&match_expout, 0);
3337 if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3338 ovs_fatal(0, "Trailing garbage in hex data");
3339 }
6fd6ed71 3340 if (match_expout.size != sizeof(struct ofp10_match)) {
437d0d22 3341 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
6fd6ed71 3342 match_expout.size, sizeof(struct ofp10_match));
8812ec2c 3343 }
96628ae8 3344
8812ec2c
BP
3345 /* Parse hex bytes for input. */
3346 for (p = ds_cstr(&in); *p; p++) {
3347 if (*p == 'x') {
3348 *p = "0123456789abcdef"[random_uint32() & 0xf];
3349 }
3350 }
96628ae8
BP
3351 ofpbuf_init(&match_in, 0);
3352 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3353 ovs_fatal(0, "Trailing garbage in hex data");
3354 }
6fd6ed71 3355 if (match_in.size != sizeof(struct ofp10_match)) {
437d0d22 3356 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
6fd6ed71 3357 match_in.size, sizeof(struct ofp10_match));
96628ae8
BP
3358 }
3359
3360 /* Convert to cls_rule and print. */
6fd6ed71 3361 ofputil_match_from_ofp10_match(match_in.data, &match);
81a76618 3362 match_print(&match);
96628ae8
BP
3363
3364 /* Convert back to ofp10_match and print differences from input. */
81a76618 3365 ofputil_match_to_ofp10_match(&match, &match_out);
6fd6ed71 3366 print_differences("", match_expout.data, match_expout.size,
96628ae8
BP
3367 &match_out, sizeof match_out);
3368
3369 /* Normalize, then convert and compare again. */
81a76618
BP
3370 ofputil_normalize_match(&match);
3371 ofputil_match_to_ofp10_match(&match, &match_normal);
96628ae8
BP
3372 print_differences("normal: ", &match_out, sizeof match_out,
3373 &match_normal, sizeof match_normal);
3374 putchar('\n');
3375
3376 ofpbuf_uninit(&match_in);
8812ec2c 3377 ofpbuf_uninit(&match_expout);
96628ae8
BP
3378 }
3379 ds_destroy(&in);
8812ec2c 3380 ds_destroy(&expout);
96628ae8
BP
3381}
3382
410698cf 3383/* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
81a76618
BP
3384 * bytes from stdin, converts them to "struct match"es, prints them as strings
3385 * on stdout, and then converts them back to hex bytes and prints any
3386 * differences from the input. */
410698cf 3387static void
1636c761 3388ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
410698cf
BP
3389{
3390 struct ds in;
3391
3392 ds_init(&in);
bdda5aca 3393 while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
410698cf
BP
3394 struct ofpbuf match_in;
3395 struct ofp11_match match_out;
81a76618 3396 struct match match;
410698cf 3397 enum ofperr error;
410698cf
BP
3398
3399 /* Parse hex bytes. */
3400 ofpbuf_init(&match_in, 0);
3401 if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3402 ovs_fatal(0, "Trailing garbage in hex data");
3403 }
6fd6ed71 3404 if (match_in.size != sizeof(struct ofp11_match)) {
437d0d22 3405 ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
6fd6ed71 3406 match_in.size, sizeof(struct ofp11_match));
410698cf
BP
3407 }
3408
81a76618 3409 /* Convert to match. */
6fd6ed71 3410 error = ofputil_match_from_ofp11_match(match_in.data, &match);
410698cf
BP
3411 if (error) {
3412 printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3413 ofpbuf_uninit(&match_in);
3414 continue;
3415 }
3416
81a76618
BP
3417 /* Print match. */
3418 match_print(&match);
410698cf
BP
3419
3420 /* Convert back to ofp11_match and print differences from input. */
81a76618 3421 ofputil_match_to_ofp11_match(&match, &match_out);
410698cf 3422
6fd6ed71 3423 print_differences("", match_in.data, match_in.size,
d01c980f
BP
3424 &match_out, sizeof match_out);
3425 putchar('\n');
410698cf 3426
d01c980f
BP
3427 ofpbuf_uninit(&match_in);
3428 }
3429 ds_destroy(&in);
3430}
3431
14249c02
BP
3432/* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3433static void
1636c761 3434ofctl_parse_pcap(struct ovs_cmdl_context *ctx)
14249c02
BP
3435{
3436 FILE *pcap;
3437
1636c761 3438 pcap = ovs_pcap_open(ctx->argv[1], "rb");
14249c02 3439 if (!pcap) {
1636c761 3440 ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
14249c02
BP
3441 }
3442
3443 for (;;) {
cf62fa4c 3444 struct dp_packet *packet;
14249c02
BP
3445 struct flow flow;
3446 int error;
3447
50aa0364 3448 error = ovs_pcap_read(pcap, &packet, NULL);
14249c02
BP
3449 if (error == EOF) {
3450 break;
3451 } else if (error) {
1636c761 3452 ovs_fatal(error, "%s: read failed", ctx->argv[1]);
14249c02
BP
3453 }
3454
35303d71 3455 pkt_metadata_init(&packet->md, ODPP_NONE);
cf62fa4c 3456 flow_extract(packet, &flow);
14249c02
BP
3457 flow_print(stdout, &flow);
3458 putchar('\n');
cf62fa4c 3459 dp_packet_delete(packet);
14249c02
BP
3460 }
3461}
3462
df778240
BP
3463/* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3464 * mask values to and from various formats and prints the results. */
3465static void
1636c761 3466ofctl_check_vlan(struct ovs_cmdl_context *ctx)
df778240 3467{
81a76618 3468 struct match match;
df778240
BP
3469
3470 char *string_s;
3471 struct ofputil_flow_mod fm;
3472
3473 struct ofpbuf nxm;
81a76618 3474 struct match nxm_match;
df778240
BP
3475 int nxm_match_len;
3476 char *nxm_s;
3477
81a76618
BP
3478 struct ofp10_match of10_raw;
3479 struct match of10_match;
df778240 3480
81a76618
BP
3481 struct ofp11_match of11_raw;
3482 struct match of11_match;
df778240
BP
3483
3484 enum ofperr error;
bdda5aca 3485 char *error_s;
df778240 3486
db0b6c29
JR
3487 enum ofputil_protocol usable_protocols; /* Unused for now. */
3488
81a76618 3489 match_init_catchall(&match);
1636c761
RB
3490 match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16));
3491 match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16));
df778240
BP
3492
3493 /* Convert to and from string. */
81a76618 3494 string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
df778240
BP
3495 printf("%s -> ", string_s);
3496 fflush(stdout);
ba2fe8e9 3497 error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
bdda5aca
BP
3498 if (error_s) {
3499 ovs_fatal(0, "%s", error_s);
3500 }
df778240 3501 printf("%04"PRIx16"/%04"PRIx16"\n",
81a76618
BP
3502 ntohs(fm.match.flow.vlan_tci),
3503 ntohs(fm.match.wc.masks.vlan_tci));
4d859fa9 3504 free(string_s);
df778240
BP
3505
3506 /* Convert to and from NXM. */
3507 ofpbuf_init(&nxm, 0);
81a76618 3508 nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
6fd6ed71 3509 nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
81a76618 3510 error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
df778240
BP
3511 printf("NXM: %s -> ", nxm_s);
3512 if (error) {
3513 printf("%s\n", ofperr_to_string(error));
3514 } else {
3515 printf("%04"PRIx16"/%04"PRIx16"\n",
81a76618
BP
3516 ntohs(nxm_match.flow.vlan_tci),
3517 ntohs(nxm_match.wc.masks.vlan_tci));
df778240
BP
3518 }
3519 free(nxm_s);
3520 ofpbuf_uninit(&nxm);
3521
476a0e9e
SH
3522 /* Convert to and from OXM. */
3523 ofpbuf_init(&nxm, 0);
9d84066c 3524 nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
db5a1019 3525 nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
81a76618 3526 error = oxm_pull_match(&nxm, &nxm_match);
476a0e9e
SH
3527 printf("OXM: %s -> ", nxm_s);
3528 if (error) {
3529 printf("%s\n", ofperr_to_string(error));
3530 } else {
81a76618 3531 uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
476a0e9e 3532 (VLAN_VID_MASK | VLAN_CFI);
81a76618 3533 uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
476a0e9e
SH
3534 (VLAN_VID_MASK | VLAN_CFI);
3535
3536 printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
81a76618
BP
3537 if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3538 printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
476a0e9e
SH
3539 } else {
3540 printf("--\n");
3541 }
3542 }
3543 free(nxm_s);
3544 ofpbuf_uninit(&nxm);
3545
df778240 3546 /* Convert to and from OpenFlow 1.0. */
81a76618
BP
3547 ofputil_match_to_ofp10_match(&match, &of10_raw);
3548 ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
df778240 3549 printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
81a76618
BP
3550 ntohs(of10_raw.dl_vlan),
3551 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3552 of10_raw.dl_vlan_pcp,
3553 (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3554 ntohs(of10_match.flow.vlan_tci),
3555 ntohs(of10_match.wc.masks.vlan_tci));
df778240
BP
3556
3557 /* Convert to and from OpenFlow 1.1. */
81a76618
BP
3558 ofputil_match_to_ofp11_match(&match, &of11_raw);
3559 ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
df778240 3560 printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
81a76618
BP
3561 ntohs(of11_raw.dl_vlan),
3562 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3563 of11_raw.dl_vlan_pcp,
3564 (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3565 ntohs(of11_match.flow.vlan_tci),
3566 ntohs(of11_match.wc.masks.vlan_tci));
df778240
BP
3567}
3568
2e0525bc
SH
3569/* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3570 * version. */
3571static void
1636c761 3572ofctl_print_error(struct ovs_cmdl_context *ctx)
2e0525bc
SH
3573{
3574 enum ofperr error;
3575 int version;
3576
1636c761 3577 error = ofperr_from_name(ctx->argv[1]);
2e0525bc 3578 if (!error) {
1636c761 3579 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
2e0525bc
SH
3580 }
3581
3582 for (version = 0; version <= UINT8_MAX; version++) {
688e86e1 3583 const char *name = ofperr_domain_get_name(version);
514887ee
BP
3584 if (name) {
3585 int vendor = ofperr_get_vendor(error, version);
3586 int type = ofperr_get_type(error, version);
3587 int code = ofperr_get_code(error, version);
3588
3589 if (vendor != -1 || type != -1 || code != -1) {
3590 printf("%s: vendor %#x, type %d, code %d\n",
3591 name, vendor, type, code);
3592 }
2e0525bc 3593 }
2e0525bc
SH
3594 }
3595}
3596
edd70aa7
BP
3597/* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3598 * error named ENUM and prints the error reply in hex. */
3599static void
1636c761 3600ofctl_encode_error_reply(struct ovs_cmdl_context *ctx)
edd70aa7
BP
3601{
3602 const struct ofp_header *oh;
3603 struct ofpbuf request, *reply;
3604 enum ofperr error;
3605
1636c761 3606 error = ofperr_from_name(ctx->argv[1]);
edd70aa7 3607 if (!error) {
1636c761 3608 ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
edd70aa7
BP
3609 }
3610
3611 ofpbuf_init(&request, 0);
1636c761 3612 if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') {
edd70aa7
BP
3613 ovs_fatal(0, "Trailing garbage in hex data");
3614 }
6fd6ed71 3615 if (request.size < sizeof(struct ofp_header)) {
edd70aa7
BP
3616 ovs_fatal(0, "Request too short");
3617 }
3618
6fd6ed71
PS
3619 oh = request.data;
3620 if (request.size != ntohs(oh->length)) {
edd70aa7
BP
3621 ovs_fatal(0, "Request size inconsistent");
3622 }
3623
6fd6ed71 3624 reply = ofperr_encode_reply(error, request.data);
edd70aa7
BP
3625 ofpbuf_uninit(&request);
3626
6fd6ed71 3627 ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
edd70aa7
BP
3628 ofpbuf_delete(reply);
3629}
3630
fec00620
BP
3631/* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3632 * binary data, interpreting them as an OpenFlow message, and prints the
8b227701
GS
3633 * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3634 *
3635 * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3636 * HEXSTRING_FILE contains the HEXSTRING. */
fec00620 3637static void
1636c761 3638ofctl_ofp_print(struct ovs_cmdl_context *ctx)
fec00620
BP
3639{
3640 struct ofpbuf packet;
8b227701
GS
3641 char *buffer;
3642 int verbosity = 2;
3643 struct ds line;
3644
3645 ds_init(&line);
3646
1636c761 3647 if (!strcmp(ctx->argv[ctx->argc-1], "-")) {
8b227701
GS
3648 if (ds_get_line(&line, stdin)) {
3649 VLOG_FATAL("Failed to read stdin");
3650 }
3651
3652 buffer = line.string;
1636c761
RB
3653 verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity;
3654 } else if (ctx->argc > 2) {
3655 buffer = ctx->argv[1];
3656 verbosity = atoi(ctx->argv[2]);
8b227701 3657 } else {
1636c761 3658 buffer = ctx->argv[1];
8b227701 3659 }
fec00620 3660
8b227701
GS
3661 ofpbuf_init(&packet, strlen(buffer) / 2);
3662 if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
fec00620
BP
3663 ovs_fatal(0, "trailing garbage following hex bytes");
3664 }
6fd6ed71 3665 ofp_print(stdout, packet.data, packet.size, verbosity);
fec00620 3666 ofpbuf_uninit(&packet);
8b227701 3667 ds_destroy(&line);
fec00620
BP
3668}
3669
681ea7a0
BP
3670/* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3671 * and dumps each message in hex. */
3672static void
1636c761 3673ofctl_encode_hello(struct ovs_cmdl_context *ctx)
681ea7a0 3674{
1636c761 3675 uint32_t bitmap = strtol(ctx->argv[1], NULL, 0);
681ea7a0
BP
3676 struct ofpbuf *hello;
3677
3678 hello = ofputil_encode_hello(bitmap);
6fd6ed71
PS
3679 ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3680 ofp_print(stdout, hello->data, hello->size, verbosity);
681ea7a0
BP
3681 ofpbuf_delete(hello);
3682}
3683
5f383751 3684static const struct ovs_cmdl_command all_commands[] = {
451de37e
AW
3685 { "show", "switch",
3686 1, 1, ofctl_show },
3687 { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3688 1, 3, ofctl_monitor },
3689 { "snoop", "switch",
3690 1, 1, ofctl_snoop },
3691 { "dump-desc", "switch",
3692 1, 1, ofctl_dump_desc },
3693 { "dump-tables", "switch",
3694 1, 1, ofctl_dump_tables },
3695 { "dump-table-features", "switch",
3696 1, 1, ofctl_dump_table_features },
03c72922
BP
3697 { "dump-table-desc", "switch",
3698 1, 1, ofctl_dump_table_desc },
451de37e
AW
3699 { "dump-flows", "switch",
3700 1, 2, ofctl_dump_flows },
3701 { "dump-aggregate", "switch",
3702 1, 2, ofctl_dump_aggregate },
3703 { "queue-stats", "switch [port [queue]]",
3704 1, 3, ofctl_queue_stats },
3705 { "queue-get-config", "switch port",
3706 2, 2, ofctl_queue_get_config },
3707 { "add-flow", "switch flow",
3708 2, 2, ofctl_add_flow },
3709 { "add-flows", "switch file",
3710 2, 2, ofctl_add_flows },
3711 { "mod-flows", "switch flow",
3712 2, 2, ofctl_mod_flows },
3713 { "del-flows", "switch [flow]",
3714 1, 2, ofctl_del_flows },
3715 { "replace-flows", "switch file",
3716 2, 2, ofctl_replace_flows },
3717 { "diff-flows", "source1 source2",
3718 2, 2, ofctl_diff_flows },
3719 { "add-meter", "switch meter",
3720 2, 2, ofctl_add_meter },
3721 { "mod-meter", "switch meter",
3722 2, 2, ofctl_mod_meter },
3723 { "del-meter", "switch meter",
3724 2, 2, ofctl_del_meters },
3725 { "del-meters", "switch",
3726 1, 1, ofctl_del_meters },
3727 { "dump-meter", "switch meter",
3728 2, 2, ofctl_dump_meters },
3729 { "dump-meters", "switch",
3730 1, 1, ofctl_dump_meters },
3731 { "meter-stats", "switch [meter]",
3732 1, 2, ofctl_meter_stats },
3733 { "meter-features", "switch",
3734 1, 1, ofctl_meter_features },
3735 { "packet-out", "switch in_port actions packet...",
3736 4, INT_MAX, ofctl_packet_out },
3737 { "dump-ports", "switch [port]",
3738 1, 2, ofctl_dump_ports },
3739 { "dump-ports-desc", "switch [port]",
3740 1, 2, ofctl_dump_ports_desc },
3741 { "mod-port", "switch iface act",
3742 3, 3, ofctl_mod_port },
3743 { "mod-table", "switch mod",
3744 3, 3, ofctl_mod_table },
3745 { "get-frags", "switch",
3746 1, 1, ofctl_get_frags },
3747 { "set-frags", "switch frag_mode",
3748 2, 2, ofctl_set_frags },
3749 { "probe", "target",
3750 1, 1, ofctl_probe },
3751 { "ping", "target [n]",
3752 1, 2, ofctl_ping },
3753 { "benchmark", "target n count",
3754 3, 3, ofctl_benchmark },
3755
3756 { "ofp-parse", "file",
3757 1, 1, ofctl_ofp_parse },
3758 { "ofp-parse-pcap", "pcap",
3759 1, INT_MAX, ofctl_ofp_parse_pcap },
3760
3761 { "add-group", "switch group",
3762 1, 2, ofctl_add_group },
3763 { "add-groups", "switch file",
3764 1, 2, ofctl_add_groups },
3765 { "mod-group", "switch group",
3766 1, 2, ofctl_mod_group },
3767 { "del-groups", "switch [group]",
3768 1, 2, ofctl_del_groups },
bdbb8426
SH
3769 { "insert-buckets", "switch [group]",
3770 1, 2, ofctl_insert_bucket },
3771 { "remove-buckets", "switch [group]",
3772 1, 2, ofctl_remove_bucket },
451de37e
AW
3773 { "dump-groups", "switch [group]",
3774 1, 2, ofctl_dump_group_desc },
3775 { "dump-group-stats", "switch [group]",
3776 1, 2, ofctl_dump_group_stats },
3777 { "dump-group-features", "switch",
3778 1, 1, ofctl_dump_group_features },
6159c531
JG
3779 { "add-geneve-map", "switch map",
3780 2, 2, ofctl_add_geneve_map },
3781 { "del-geneve-map", "switch [map]",
3782 1, 2, ofctl_del_geneve_map },
3783 { "dump-geneve-map", "switch",
3784 1, 1, ofctl_dump_geneve_map },
451de37e
AW
3785 { "help", NULL, 0, INT_MAX, ofctl_help },
3786 { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands },
09246b99
BP
3787
3788 /* Undocumented commands for testing. */
451de37e
AW
3789 { "parse-flow", NULL, 1, 1, ofctl_parse_flow },
3790 { "parse-flows", NULL, 1, 1, ofctl_parse_flows },
3791 { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm },
3792 { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm },
3793 { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm },
3794 { "parse-actions", NULL, 1, 1, ofctl_parse_actions },
3795 { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions },
3796 { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match },
3797 { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match },
3798 { "parse-pcap", NULL, 1, 1, ofctl_parse_pcap },
3799 { "check-vlan", NULL, 2, 2, ofctl_check_vlan },
3800 { "print-error", NULL, 1, 1, ofctl_print_error },
3801 { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply },
3802 { "ofp-print", NULL, 1, 2, ofctl_ofp_print },
3803 { "encode-hello", NULL, 1, 1, ofctl_encode_hello },
3804
3805 { NULL, NULL, 0, 0, NULL },
064af421 3806};
3815d6c2 3807
5f383751 3808static const struct ovs_cmdl_command *get_all_commands(void)
3815d6c2
LS
3809{
3810 return all_commands;
3811}