]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-ofctl.c
xenserver: Remove other_config option for setting controller.
[mirror_ovs.git] / utilities / ovs-ofctl.c
CommitLineData
064af421 1/*
fec00620 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#include <config.h>
064af421
BP
18#include <errno.h>
19#include <getopt.h>
20#include <inttypes.h>
21#include <net/if.h>
064af421 22#include <signal.h>
064af421
BP
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28
10a24935 29#include "byte-order.h"
09246b99 30#include "classifier.h"
064af421
BP
31#include "command-line.h"
32#include "compiler.h"
33#include "dirs.h"
34#include "dpif.h"
09246b99 35#include "dynamic-string.h"
064af421 36#include "netlink.h"
09246b99 37#include "nx-match.h"
064af421 38#include "odp-util.h"
f22716dc 39#include "ofp-parse.h"
064af421 40#include "ofp-print.h"
fa37b408 41#include "ofp-util.h"
064af421
BP
42#include "ofpbuf.h"
43#include "openflow/nicira-ext.h"
44#include "openflow/openflow.h"
064af421 45#include "random.h"
fe55ad15 46#include "stream-ssl.h"
064af421
BP
47#include "timeval.h"
48#include "util.h"
064af421 49#include "vconn.h"
5136ce49 50#include "vlog.h"
064af421 51
d98e6007 52VLOG_DEFINE_THIS_MODULE(ofctl);
064af421 53
88ca35ee 54/* --strict: Use strict matching for flow mod commands? */
675febfa 55static bool strict;
064af421 56
88ca35ee
BP
57/* -F, --flow-format: Flow format to use. Either one of NXFF_* to force a
58 * particular flow format or -1 to let ovs-ofctl choose intelligently. */
59static int preferred_flow_format = -1;
60
4f564f8d
BP
61/* -m, --more: Additional verbosity for ofp-print functions. */
62static int verbosity;
63
675febfa 64static const struct command all_commands[];
064af421
BP
65
66static void usage(void) NO_RETURN;
675febfa 67static void parse_options(int argc, char *argv[]);
064af421 68
675febfa
BP
69int
70main(int argc, char *argv[])
064af421 71{
064af421 72 set_program_name(argv[0]);
675febfa 73 parse_options(argc, argv);
064af421 74 signal(SIGPIPE, SIG_IGN);
675febfa 75 run_command(argc - optind, argv + optind, all_commands);
064af421
BP
76 return 0;
77}
78
79static void
675febfa 80parse_options(int argc, char *argv[])
064af421
BP
81{
82 enum {
87c84891
JP
83 OPT_STRICT = UCHAR_MAX + 1,
84 VLOG_OPTION_ENUMS
064af421
BP
85 };
86 static struct option long_options[] = {
87 {"timeout", required_argument, 0, 't'},
064af421 88 {"strict", no_argument, 0, OPT_STRICT},
88ca35ee 89 {"flow-format", required_argument, 0, 'F'},
4f564f8d 90 {"more", no_argument, 0, 'm'},
064af421
BP
91 {"help", no_argument, 0, 'h'},
92 {"version", no_argument, 0, 'V'},
87c84891 93 VLOG_LONG_OPTIONS,
fe55ad15 94 STREAM_SSL_LONG_OPTIONS
064af421
BP
95 {0, 0, 0, 0},
96 };
97 char *short_options = long_options_to_short_options(long_options);
98
064af421
BP
99 for (;;) {
100 unsigned long int timeout;
101 int c;
102
103 c = getopt_long(argc, argv, short_options, long_options, NULL);
104 if (c == -1) {
105 break;
106 }
107
108 switch (c) {
109 case 't':
110 timeout = strtoul(optarg, NULL, 10);
111 if (timeout <= 0) {
112 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
113 optarg);
114 } else {
115 time_alarm(timeout);
116 }
117 break;
118
88ca35ee
BP
119 case 'F':
120 preferred_flow_format = ofputil_flow_format_from_string(optarg);
121 if (preferred_flow_format < 0) {
122 ovs_fatal(0, "unknown flow format `%s'", optarg);
123 }
124 break;
125
4f564f8d
BP
126 case 'm':
127 verbosity++;
128 break;
129
064af421
BP
130 case 'h':
131 usage();
132
133 case 'V':
134 OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
135 exit(EXIT_SUCCESS);
136
064af421 137 case OPT_STRICT:
675febfa 138 strict = true;
064af421
BP
139 break;
140
87c84891 141 VLOG_OPTION_HANDLERS
fe55ad15 142 STREAM_SSL_OPTION_HANDLERS
064af421
BP
143
144 case '?':
145 exit(EXIT_FAILURE);
146
147 default:
148 abort();
149 }
150 }
151 free(short_options);
152}
153
154static void
155usage(void)
156{
157 printf("%s: OpenFlow switch management utility\n"
158 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
159 "\nFor OpenFlow switches:\n"
160 " show SWITCH show OpenFlow information\n"
161 " status SWITCH [KEY] report statistics (about KEY)\n"
162 " dump-desc SWITCH print switch description\n"
163 " dump-tables SWITCH print table stats\n"
164 " mod-port SWITCH IFACE ACT modify port behavior\n"
abaad8cf 165 " dump-ports SWITCH [PORT] print port statistics\n"
064af421
BP
166 " dump-flows SWITCH print all flow entries\n"
167 " dump-flows SWITCH FLOW print matching FLOWs\n"
168 " dump-aggregate SWITCH print aggregate flow statistics\n"
169 " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n"
d2805da2 170 " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n"
064af421
BP
171 " add-flow SWITCH FLOW add flow described by FLOW\n"
172 " add-flows SWITCH FILE add flows from FILE\n"
173 " mod-flows SWITCH FLOW modify actions of matching FLOWs\n"
174 " del-flows SWITCH [FLOW] delete matching FLOWs\n"
a6bc4a03 175 " monitor SWITCH [MISSLEN] print packets received from SWITCH\n"
064af421
BP
176 "\nFor OpenFlow switches and controllers:\n"
177 " probe VCONN probe whether VCONN is up\n"
178 " ping VCONN [N] latency of N-byte echos\n"
179 " benchmark VCONN N COUNT bandwidth of COUNT N-byte echos\n"
180 "where each SWITCH is an active OpenFlow connection method.\n",
181 program_name, program_name);
182 vconn_usage(true, false, false);
183 vlog_usage();
184 printf("\nOther options:\n"
185 " --strict use strict match for flow commands\n"
88ca35ee 186 " -F, --flow-format=FORMAT force particular flow format\n"
4f564f8d 187 " -m, --more be more verbose printing OpenFlow\n"
064af421
BP
188 " -t, --timeout=SECS give up after SECS seconds\n"
189 " -h, --help display this help message\n"
190 " -V, --version display version information\n");
191 exit(EXIT_SUCCESS);
192}
193
194static void run(int retval, const char *message, ...)
195 PRINTF_FORMAT(2, 3);
196
197static void run(int retval, const char *message, ...)
198{
199 if (retval) {
200 va_list args;
201
202 fprintf(stderr, "%s: ", program_name);
203 va_start(args, message);
204 vfprintf(stderr, message, args);
205 va_end(args);
206 if (retval == EOF) {
207 fputs(": unexpected end of file\n", stderr);
208 } else {
209 fprintf(stderr, ": %s\n", strerror(retval));
210 }
211
212 exit(EXIT_FAILURE);
213 }
214}
215\f
216/* Generic commands. */
217
1a6f1e2a
JG
218static void
219open_vconn_socket(const char *name, struct vconn **vconnp)
220{
221 char *vconn_name = xasprintf("unix:%s", name);
24cd0dee 222 VLOG_DBG("connecting to %s", vconn_name);
1a6f1e2a
JG
223 run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
224 "connecting to %s", vconn_name);
225 free(vconn_name);
226}
227
064af421 228static void
0caf6bde
BP
229open_vconn__(const char *name, const char *default_suffix,
230 struct vconn **vconnp)
064af421 231{
c228a364 232 struct dpif *dpif;
064af421 233 struct stat s;
1a6f1e2a
JG
234 char *bridge_path, *datapath_name, *datapath_type;
235
b43c6fe2 236 bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix);
1a6f1e2a 237 dp_parse_name(name, &datapath_name, &datapath_type);
064af421
BP
238
239 if (strstr(name, ":")) {
240 run(vconn_open_block(name, OFP_VERSION, vconnp),
241 "connecting to %s", name);
242 } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
1a6f1e2a
JG
243 open_vconn_socket(name, vconnp);
244 } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
245 open_vconn_socket(bridge_path, vconnp);
246 } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
064af421
BP
247 char dpif_name[IF_NAMESIZE + 1];
248 char *socket_name;
064af421 249
c228a364 250 run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
064af421 251 "obtaining name of %s", dpif_name);
c228a364 252 dpif_close(dpif);
064af421 253 if (strcmp(dpif_name, name)) {
24cd0dee 254 VLOG_DBG("datapath %s is named %s", name, dpif_name);
064af421
BP
255 }
256
0caf6bde 257 socket_name = xasprintf("%s/%s.%s",
b43c6fe2 258 ovs_rundir(), dpif_name, default_suffix);
064af421
BP
259 if (stat(socket_name, &s)) {
260 ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
261 name, socket_name);
262 } else if (!S_ISSOCK(s.st_mode)) {
263 ovs_fatal(0, "cannot connect to %s: %s is not a socket",
264 name, socket_name);
265 }
266
1a6f1e2a 267 open_vconn_socket(socket_name, vconnp);
064af421 268 free(socket_name);
064af421
BP
269 } else {
270 ovs_fatal(0, "%s is not a valid connection method", name);
271 }
1a6f1e2a
JG
272
273 free(datapath_name);
274 free(datapath_type);
275 free(bridge_path);
064af421
BP
276}
277
0caf6bde
BP
278static void
279open_vconn(const char *name, struct vconn **vconnp)
280{
281 return open_vconn__(name, "mgmt", vconnp);
282}
283
064af421
BP
284static void *
285alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
286{
287 struct ofp_stats_request *rq;
288 rq = make_openflow((offsetof(struct ofp_stats_request, body)
289 + body_len), OFPT_STATS_REQUEST, bufferp);
290 rq->type = htons(type);
291 rq->flags = htons(0);
292 return rq->body;
293}
294
295static void
296send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
297{
298 update_openflow_length(buffer);
299 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
300}
301
302static void
303dump_transaction(const char *vconn_name, struct ofpbuf *request)
304{
305 struct vconn *vconn;
306 struct ofpbuf *reply;
307
308 update_openflow_length(request);
309 open_vconn(vconn_name, &vconn);
310 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
4f564f8d 311 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421
BP
312 vconn_close(vconn);
313}
314
315static void
316dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
317{
318 struct ofpbuf *request;
319 make_openflow(sizeof(struct ofp_header), request_type, &request);
320 dump_transaction(vconn_name, request);
321}
322
323static void
324dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
325{
44381c1b 326 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
064af421
BP
327 struct vconn *vconn;
328 bool done = false;
329
330 open_vconn(vconn_name, &vconn);
331 send_openflow_buffer(vconn, request);
332 while (!done) {
333 uint32_t recv_xid;
334 struct ofpbuf *reply;
335
336 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
337 recv_xid = ((struct ofp_header *) reply->data)->xid;
338 if (send_xid == recv_xid) {
339 struct ofp_stats_reply *osr;
340
4f564f8d 341 ofp_print(stdout, reply->data, reply->size, verbosity + 1);
064af421
BP
342
343 osr = ofpbuf_at(reply, 0, sizeof *osr);
344 done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
345 } else {
346 VLOG_DBG("received reply with xid %08"PRIx32" "
347 "!= expected %08"PRIx32, recv_xid, send_xid);
348 }
349 ofpbuf_delete(reply);
350 }
351 vconn_close(vconn);
352}
353
354static void
355dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
356{
357 struct ofpbuf *request;
358 alloc_stats_request(0, stats_type, &request);
359 dump_stats_transaction(vconn_name, request);
360}
361
d12513f7
BP
362/* Sends 'request', which should be a request that only has a reply if an error
363 * occurs, and waits for it to succeed or fail. If an error does occur, prints
364 * it and exits with an error. */
365static void
88ca35ee 366transact_multiple_noreply(struct vconn *vconn, struct list *requests)
d12513f7 367{
88ca35ee 368 struct ofpbuf *request, *reply;
d12513f7 369
88ca35ee
BP
370 LIST_FOR_EACH (request, list_node, requests) {
371 update_openflow_length(request);
372 }
373
374 run(vconn_transact_multiple_noreply(vconn, requests, &reply),
d12513f7
BP
375 "talking to %s", vconn_get_name(vconn));
376 if (reply) {
4f564f8d 377 ofp_print(stderr, reply->data, reply->size, verbosity + 2);
d12513f7
BP
378 exit(1);
379 }
380 ofpbuf_delete(reply);
381}
382
88ca35ee
BP
383/* Sends 'request', which should be a request that only has a reply if an error
384 * occurs, and waits for it to succeed or fail. If an error does occur, prints
385 * it and exits with an error. */
386static void
387transact_noreply(struct vconn *vconn, struct ofpbuf *request)
388{
389 struct list requests;
390
391 list_init(&requests);
392 list_push_back(&requests, &request->list_node);
393 transact_multiple_noreply(vconn, &requests);
394}
395
064af421 396static void
c69ee87c 397do_show(int argc OVS_UNUSED, char *argv[])
064af421
BP
398{
399 dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
400 dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
401}
402
403static void
675febfa 404do_status(int argc, char *argv[])
064af421
BP
405{
406 struct nicira_header *request, *reply;
407 struct vconn *vconn;
408 struct ofpbuf *b;
409
0bd0c660 410 request = make_nxmsg(sizeof *request, NXT_STATUS_REQUEST, &b);
064af421
BP
411 if (argc > 2) {
412 ofpbuf_put(b, argv[2], strlen(argv[2]));
413 update_openflow_length(b);
414 }
415 open_vconn(argv[1], &vconn);
416 run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
417 vconn_close(vconn);
418
419 if (b->size < sizeof *reply) {
420 ovs_fatal(0, "short reply (%zu bytes)", b->size);
421 }
422 reply = b->data;
423 if (reply->header.type != OFPT_VENDOR
424 || reply->vendor != ntohl(NX_VENDOR_ID)
425 || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
4f564f8d 426 ofp_print(stderr, b->data, b->size, verbosity + 2);
064af421
BP
427 ovs_fatal(0, "bad reply");
428 }
429
430 fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
431}
432
433static void
c69ee87c 434do_dump_desc(int argc OVS_UNUSED, char *argv[])
064af421
BP
435{
436 dump_trivial_stats_transaction(argv[1], OFPST_DESC);
437}
438
439static void
c69ee87c 440do_dump_tables(int argc OVS_UNUSED, char *argv[])
064af421
BP
441{
442 dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
443}
444
0df0e81d
BP
445/* Opens a connection to 'vconn_name', fetches the ofp_phy_port structure for
446 * 'port_name' (which may be a port name or number), and copies it into
447 * '*oppp'. */
448static void
449fetch_ofp_phy_port(const char *vconn_name, const char *port_name,
450 struct ofp_phy_port *oppp)
abaad8cf
JP
451{
452 struct ofpbuf *request, *reply;
453 struct ofp_switch_features *osf;
0df0e81d 454 unsigned int port_no;
abaad8cf
JP
455 struct vconn *vconn;
456 int n_ports;
457 int port_idx;
abaad8cf 458
0df0e81d
BP
459 /* Try to interpret the argument as a port number. */
460 if (!str_to_uint(port_name, 10, &port_no)) {
461 port_no = UINT_MAX;
abaad8cf
JP
462 }
463
0df0e81d 464 /* Fetch the switch's ofp_switch_features. */
abaad8cf
JP
465 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
466 open_vconn(vconn_name, &vconn);
467 run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
468
469 osf = reply->data;
0df0e81d
BP
470 if (reply->size < sizeof *osf) {
471 ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)",
472 vconn_name, reply->size);
473 }
abaad8cf
JP
474 n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
475
476 for (port_idx = 0; port_idx < n_ports; port_idx++) {
0df0e81d
BP
477 const struct ofp_phy_port *opp = &osf->ports[port_idx];
478
479 if (port_no != UINT_MAX
480 ? htons(port_no) == opp->port_no
0b61210e 481 : !strncmp(opp->name, port_name, sizeof opp->name)) {
0df0e81d
BP
482 *oppp = *opp;
483 ofpbuf_delete(reply);
484 vconn_close(vconn);
485 return;
abaad8cf
JP
486 }
487 }
0df0e81d
BP
488 ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
489}
abaad8cf 490
0df0e81d
BP
491/* Returns the port number corresponding to 'port_name' (which may be a port
492 * name or number) within the switch 'vconn_name'. */
493static uint16_t
494str_to_port_no(const char *vconn_name, const char *port_name)
495{
496 unsigned int port_no;
497
498 if (str_to_uint(port_name, 10, &port_no)) {
499 return port_no;
500 } else {
501 struct ofp_phy_port opp;
abaad8cf 502
0df0e81d
BP
503 fetch_ofp_phy_port(vconn_name, port_name, &opp);
504 return ntohs(opp.port_no);
505 }
abaad8cf
JP
506}
507
88ca35ee
BP
508static bool
509try_set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
510{
511 struct ofpbuf *sff, *reply;
512
513 sff = ofputil_make_set_flow_format(flow_format);
514 run(vconn_transact_noreply(vconn, sff, &reply),
515 "talking to %s", vconn_get_name(vconn));
516 if (reply) {
517 char *s = ofp_to_string(reply->data, reply->size, 2);
518 VLOG_DBG("%s: failed to set flow format %s, controller replied: %s",
519 vconn_get_name(vconn),
520 ofputil_flow_format_to_string(flow_format),
521 s);
522 free(s);
523 ofpbuf_delete(reply);
524 return false;
525 }
526 return true;
527}
528
064af421 529static void
88ca35ee 530set_flow_format(struct vconn *vconn, enum nx_flow_format flow_format)
064af421 531{
88ca35ee
BP
532 struct ofpbuf *sff = ofputil_make_set_flow_format(flow_format);
533 transact_noreply(vconn, sff);
534 VLOG_DBG("%s: using user-specified flow format %s",
535 vconn_get_name(vconn),
536 ofputil_flow_format_to_string(flow_format));
537}
064af421 538
88ca35ee
BP
539static enum nx_flow_format
540negotiate_highest_flow_format(struct vconn *vconn, const struct cls_rule *rule,
541 bool cookie_support, ovs_be64 cookie)
542{
543 int flow_format;
064af421 544
88ca35ee
BP
545 if (preferred_flow_format != -1) {
546 enum nx_flow_format min_format;
547
548 min_format = ofputil_min_flow_format(rule, cookie_support, cookie);
549 if (preferred_flow_format >= min_format) {
550 set_flow_format(vconn, preferred_flow_format);
551 return preferred_flow_format;
552 }
553
554 VLOG_WARN("%s: cannot use requested flow format %s for "
555 "specified flow", vconn_get_name(vconn),
556 ofputil_flow_format_to_string(min_format));
557 }
558
559 if (try_set_flow_format(vconn, NXFF_NXM)) {
560 flow_format = NXFF_NXM;
561 } else if (try_set_flow_format(vconn, NXFF_TUN_ID_FROM_COOKIE)) {
562 flow_format = NXFF_TUN_ID_FROM_COOKIE;
563 } else {
564 flow_format = NXFF_OPENFLOW10;
565 }
566
567 VLOG_DBG("%s: negotiated flow format %s", vconn_get_name(vconn),
568 ofputil_flow_format_to_string(flow_format));
569 return flow_format;
064af421
BP
570}
571
572static void
88ca35ee 573do_dump_flows__(int argc, char *argv[], bool aggregate)
064af421 574{
88ca35ee
BP
575 enum nx_flow_format flow_format;
576 struct flow_stats_request fsr;
064af421 577 struct ofpbuf *request;
88ca35ee 578 struct vconn *vconn;
064af421 579
88ca35ee 580 parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : "");
064af421 581
88ca35ee
BP
582 open_vconn(argv[1], &vconn);
583 flow_format = negotiate_highest_flow_format(vconn, &fsr.match, false, 0);
584 request = ofputil_encode_flow_stats_request(&fsr, flow_format);
064af421 585 dump_stats_transaction(argv[1], request);
88ca35ee
BP
586 vconn_close(vconn);
587}
588
589static void
590do_dump_flows(int argc, char *argv[])
591{
592 return do_dump_flows__(argc, argv, false);
593}
594
595static void
596do_dump_aggregate(int argc, char *argv[])
597{
598 return do_dump_flows__(argc, argv, true);
064af421
BP
599}
600
d2805da2
BP
601static void
602do_queue_stats(int argc, char *argv[])
603{
604 struct ofp_queue_stats_request *req;
605 struct ofpbuf *request;
606
607 req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
608
609 if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
610 req->port_no = htons(str_to_port_no(argv[1], argv[2]));
611 } else {
612 req->port_no = htons(OFPP_ALL);
613 }
614 if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
615 req->queue_id = htonl(atoi(argv[3]));
616 } else {
617 req->queue_id = htonl(OFPQ_ALL);
618 }
619
620 memset(req->pad, 0, sizeof req->pad);
621
622 dump_stats_transaction(argv[1], request);
623}
624
0fbc9f11
BP
625/* Sets up the flow format for a vconn that will be used to modify the flow
626 * table. Returns the flow format used, after possibly adding an OpenFlow
627 * request to 'requests'.
628 *
629 * If 'preferred_flow_format' is -1, returns NXFF_OPENFLOW10 without modifying
630 * 'requests', since NXFF_OPENFLOW10 is the default flow format for any
631 * OpenFlow connection.
632 *
633 * If 'preferred_flow_format' is a specific format, adds a request to set that
634 * format to 'requests' and returns the format. */
635static enum nx_flow_format
636set_initial_format_for_flow_mod(struct list *requests)
637{
638 if (preferred_flow_format < 0) {
639 return NXFF_OPENFLOW10;
640 } else {
641 struct ofpbuf *sff;
642
643 sff = ofputil_make_set_flow_format(preferred_flow_format);
644 list_push_back(requests, &sff->list_node);
645 return preferred_flow_format;
646 }
647}
648
649/* Checks that 'flow_format' is acceptable as a flow format after a flow_mod
650 * operation, given the global 'preferred_flow_format'. */
651static void
652check_final_format_for_flow_mod(enum nx_flow_format flow_format)
653{
654 if (preferred_flow_format >= 0 && flow_format != preferred_flow_format) {
655 ovs_fatal(0, "flow cannot be expressed in flow format %s "
656 "(flow format %s or better is required)",
657 ofputil_flow_format_to_string(preferred_flow_format),
658 ofputil_flow_format_to_string(flow_format));
659 }
660}
661
064af421 662static void
88ca35ee 663do_flow_mod__(int argc OVS_UNUSED, char *argv[], uint16_t command)
064af421 664{
88ca35ee
BP
665 enum nx_flow_format flow_format;
666 struct list requests;
064af421 667 struct vconn *vconn;
049c8dc2 668
88ca35ee 669 list_init(&requests);
0fbc9f11
BP
670 flow_format = set_initial_format_for_flow_mod(&requests);
671
640c7c94
BP
672 parse_ofp_flow_mod_str(&requests, &flow_format, argc > 2 ? argv[2] : "",
673 command);
0fbc9f11 674 check_final_format_for_flow_mod(flow_format);
064af421
BP
675
676 open_vconn(argv[1], &vconn);
88ca35ee 677 transact_multiple_noreply(vconn, &requests);
064af421
BP
678 vconn_close(vconn);
679}
680
88ca35ee
BP
681static void
682do_add_flow(int argc, char *argv[])
683{
684 do_flow_mod__(argc, argv, OFPFC_ADD);
685}
686
064af421 687static void
c69ee87c 688do_add_flows(int argc OVS_UNUSED, char *argv[])
064af421 689{
88ca35ee
BP
690 enum nx_flow_format flow_format;
691 struct list requests;
064af421
BP
692 struct vconn *vconn;
693 FILE *file;
064af421
BP
694
695 file = fopen(argv[2], "r");
696 if (file == NULL) {
697 ovs_fatal(errno, "%s: open", argv[2]);
698 }
699
88ca35ee 700 list_init(&requests);
0fbc9f11 701 flow_format = set_initial_format_for_flow_mod(&requests);
88ca35ee 702
064af421 703 open_vconn(argv[1], &vconn);
88ca35ee 704 while (parse_ofp_add_flow_file(&requests, &flow_format, file)) {
0fbc9f11 705 check_final_format_for_flow_mod(flow_format);
88ca35ee 706 transact_multiple_noreply(vconn, &requests);
064af421
BP
707 }
708 vconn_close(vconn);
88ca35ee 709
064af421
BP
710 fclose(file);
711}
712
713static void
88ca35ee 714do_mod_flows(int argc, char *argv[])
064af421 715{
88ca35ee 716 do_flow_mod__(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
064af421
BP
717}
718
88ca35ee
BP
719static void
720do_del_flows(int argc, char *argv[])
064af421 721{
88ca35ee 722 do_flow_mod__(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
064af421
BP
723}
724
725static void
0caf6bde
BP
726monitor_vconn(struct vconn *vconn)
727{
728 for (;;) {
729 struct ofpbuf *b;
730 run(vconn_recv_block(vconn, &b), "vconn_recv");
4f564f8d 731 ofp_print(stderr, b->data, b->size, verbosity + 2);
0caf6bde
BP
732 ofpbuf_delete(b);
733 }
734}
735
736static void
737do_monitor(int argc, char *argv[])
064af421
BP
738{
739 struct vconn *vconn;
740
741 open_vconn(argv[1], &vconn);
742 if (argc > 2) {
743 int miss_send_len = atoi(argv[2]);
064af421
BP
744 struct ofp_switch_config *osc;
745 struct ofpbuf *buf;
746
747 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
064af421 748 osc->miss_send_len = htons(miss_send_len);
88ca35ee 749 transact_noreply(vconn, buf);
064af421 750 }
0caf6bde
BP
751 monitor_vconn(vconn);
752}
753
754static void
755do_snoop(int argc OVS_UNUSED, char *argv[])
756{
757 struct vconn *vconn;
758
759 open_vconn__(argv[1], "snoop", &vconn);
760 monitor_vconn(vconn);
064af421
BP
761}
762
763static void
abaad8cf 764do_dump_ports(int argc, char *argv[])
064af421 765{
abaad8cf
JP
766 struct ofp_port_stats_request *req;
767 struct ofpbuf *request;
768 uint16_t port;
769
770 req = alloc_stats_request(sizeof *req, OFPST_PORT, &request);
771 port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE;
772 req->port_no = htons(port);
773 dump_stats_transaction(argv[1], request);
064af421
BP
774}
775
776static void
c69ee87c 777do_probe(int argc OVS_UNUSED, char *argv[])
064af421
BP
778{
779 struct ofpbuf *request;
780 struct vconn *vconn;
781 struct ofpbuf *reply;
782
783 make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
784 open_vconn(argv[1], &vconn);
785 run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
786 if (reply->size != sizeof(struct ofp_header)) {
787 ovs_fatal(0, "reply does not match request");
788 }
789 ofpbuf_delete(reply);
790 vconn_close(vconn);
791}
792
793static void
c69ee87c 794do_mod_port(int argc OVS_UNUSED, char *argv[])
064af421 795{
064af421 796 struct ofp_port_mod *opm;
0df0e81d
BP
797 struct ofp_phy_port opp;
798 struct ofpbuf *request;
064af421 799 struct vconn *vconn;
064af421 800
0df0e81d 801 fetch_ofp_phy_port(argv[1], argv[2], &opp);
064af421
BP
802
803 opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
0df0e81d
BP
804 opm->port_no = opp.port_no;
805 memcpy(opm->hw_addr, opp.hw_addr, sizeof opm->hw_addr);
064af421
BP
806 opm->config = htonl(0);
807 opm->mask = htonl(0);
808 opm->advertise = htonl(0);
809
f4866781 810 if (!strcasecmp(argv[3], "up")) {
064af421 811 opm->mask |= htonl(OFPPC_PORT_DOWN);
f4866781 812 } else if (!strcasecmp(argv[3], "down")) {
064af421
BP
813 opm->mask |= htonl(OFPPC_PORT_DOWN);
814 opm->config |= htonl(OFPPC_PORT_DOWN);
f4866781 815 } else if (!strcasecmp(argv[3], "flood")) {
064af421 816 opm->mask |= htonl(OFPPC_NO_FLOOD);
f4866781 817 } else if (!strcasecmp(argv[3], "noflood")) {
064af421
BP
818 opm->mask |= htonl(OFPPC_NO_FLOOD);
819 opm->config |= htonl(OFPPC_NO_FLOOD);
820 } else {
821 ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
822 }
823
0df0e81d 824 open_vconn(argv[1], &vconn);
88ca35ee 825 transact_noreply(vconn, request);
064af421
BP
826 vconn_close(vconn);
827}
828
829static void
675febfa 830do_ping(int argc, char *argv[])
064af421
BP
831{
832 size_t max_payload = 65535 - sizeof(struct ofp_header);
833 unsigned int payload;
834 struct vconn *vconn;
835 int i;
836
837 payload = argc > 2 ? atoi(argv[2]) : 64;
838 if (payload > max_payload) {
839 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
840 }
841
842 open_vconn(argv[1], &vconn);
843 for (i = 0; i < 10; i++) {
844 struct timeval start, end;
845 struct ofpbuf *request, *reply;
846 struct ofp_header *rq_hdr, *rpy_hdr;
847
848 rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
849 OFPT_ECHO_REQUEST, &request);
850 random_bytes(rq_hdr + 1, payload);
851
852 gettimeofday(&start, NULL);
853 run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
854 gettimeofday(&end, NULL);
855
856 rpy_hdr = reply->data;
857 if (reply->size != request->size
858 || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
859 || rpy_hdr->xid != rq_hdr->xid
860 || rpy_hdr->type != OFPT_ECHO_REPLY) {
861 printf("Reply does not match request. Request:\n");
4f564f8d 862 ofp_print(stdout, request, request->size, verbosity + 2);
064af421 863 printf("Reply:\n");
4f564f8d 864 ofp_print(stdout, reply, reply->size, verbosity + 2);
064af421 865 }
2886875a 866 printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
44381c1b 867 reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid),
064af421
BP
868 (1000*(double)(end.tv_sec - start.tv_sec))
869 + (.001*(end.tv_usec - start.tv_usec)));
870 ofpbuf_delete(request);
871 ofpbuf_delete(reply);
872 }
873 vconn_close(vconn);
874}
875
876static void
c69ee87c 877do_benchmark(int argc OVS_UNUSED, char *argv[])
064af421
BP
878{
879 size_t max_payload = 65535 - sizeof(struct ofp_header);
880 struct timeval start, end;
881 unsigned int payload_size, message_size;
882 struct vconn *vconn;
883 double duration;
884 int count;
885 int i;
886
887 payload_size = atoi(argv[2]);
888 if (payload_size > max_payload) {
889 ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
890 }
891 message_size = sizeof(struct ofp_header) + payload_size;
892
893 count = atoi(argv[3]);
894
895 printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
896 count, message_size, count * message_size);
897
898 open_vconn(argv[1], &vconn);
899 gettimeofday(&start, NULL);
900 for (i = 0; i < count; i++) {
901 struct ofpbuf *request, *reply;
902 struct ofp_header *rq_hdr;
903
904 rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
905 memset(rq_hdr + 1, 0, payload_size);
906 run(vconn_transact(vconn, request, &reply), "transact");
907 ofpbuf_delete(reply);
908 }
909 gettimeofday(&end, NULL);
910 vconn_close(vconn);
911
912 duration = ((1000*(double)(end.tv_sec - start.tv_sec))
913 + (.001*(end.tv_usec - start.tv_usec)));
914 printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
915 duration, count / (duration / 1000.0),
916 count * message_size / (duration / 1000.0));
917}
918
09246b99
BP
919static void
920do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
921{
922 usage();
923}
924\f
925/* Undocumented commands for unit testing. */
926
770f1f66
BP
927static void
928print_packet_list(struct list *packets)
929{
930 struct ofpbuf *packet, *next;
931
932 LIST_FOR_EACH_SAFE (packet, next, list_node, packets) {
933 ofp_print(stdout, packet->data, packet->size, verbosity);
934 list_remove(&packet->list_node);
935 ofpbuf_delete(packet);
936 }
937}
938
939/* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
940 * it back to stdout. */
941static void
942do_parse_flow(int argc OVS_UNUSED, char *argv[])
943{
944 enum nx_flow_format flow_format;
945 struct list packets;
946
947 flow_format = NXFF_OPENFLOW10;
948 if (preferred_flow_format > 0) {
949 flow_format = preferred_flow_format;
950 }
951
952 list_init(&packets);
953 parse_ofp_flow_mod_str(&packets, &flow_format, argv[1], OFPFC_ADD);
954 print_packet_list(&packets);
955}
956
fec00620
BP
957/* "parse-flows FILENAME": reads the named file as a sequence of flows (like
958 * add-flows) and prints each of the flows back to stdout. */
0e581146
BP
959static void
960do_parse_flows(int argc OVS_UNUSED, char *argv[])
961{
88ca35ee
BP
962 enum nx_flow_format flow_format;
963 struct list packets;
0e581146
BP
964 FILE *file;
965
966 file = fopen(argv[1], "r");
967 if (file == NULL) {
968 ovs_fatal(errno, "%s: open", argv[2]);
969 }
970
88ca35ee
BP
971 flow_format = NXFF_OPENFLOW10;
972 if (preferred_flow_format > 0) {
973 flow_format = preferred_flow_format;
974 }
975
770f1f66 976 list_init(&packets);
88ca35ee 977 while (parse_ofp_add_flow_file(&packets, &flow_format, file)) {
770f1f66 978 print_packet_list(&packets);
0e581146
BP
979 }
980 fclose(file);
981}
982
fec00620
BP
983/* "parse-nx-match": reads a series of nx_match specifications as strings from
984 * stdin, does some internal fussing with them, and then prints them back as
985 * strings on stdout. */
064af421 986static void
09246b99
BP
987do_parse_nx_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
988{
989 struct ds in;
990
991 ds_init(&in);
992 while (!ds_get_line(&in, stdin)) {
993 struct ofpbuf nx_match;
994 struct cls_rule rule;
995 int match_len;
996 int error;
997 char *s;
998
999 /* Delete comments, skip blank lines. */
1000 s = ds_cstr(&in);
1001 if (*s == '#') {
1002 puts(s);
1003 continue;
1004 }
1005 if (strchr(s, '#')) {
1006 *strchr(s, '#') = '\0';
1007 }
1008 if (s[strspn(s, " ")] == '\0') {
1009 putchar('\n');
1010 continue;
1011 }
1012
1013 /* Convert string to nx_match. */
1014 ofpbuf_init(&nx_match, 0);
1015 match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
1016
1017 /* Convert nx_match to cls_rule. */
1018 error = nx_pull_match(&nx_match, match_len, 0, &rule);
1019 if (!error) {
1020 char *out;
1021
1022 /* Convert cls_rule back to nx_match. */
1023 ofpbuf_uninit(&nx_match);
1024 ofpbuf_init(&nx_match, 0);
1025 match_len = nx_put_match(&nx_match, &rule);
1026
1027 /* Convert nx_match to string. */
1028 out = nx_match_to_string(nx_match.data, match_len);
1029 puts(out);
1030 free(out);
1031 } else {
1032 printf("nx_pull_match() returned error %x\n", error);
1033 }
1034
1035 ofpbuf_uninit(&nx_match);
1036 }
1037 ds_destroy(&in);
064af421
BP
1038}
1039
fec00620
BP
1040/* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
1041 * binary data, interpreting them as an OpenFlow message, and prints the
1042 * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
1043static void
1044do_ofp_print(int argc, char *argv[])
1045{
1046 struct ofpbuf packet;
1047
1048 ofpbuf_init(&packet, strlen(argv[1]) / 2);
1049 if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
1050 ovs_fatal(0, "trailing garbage following hex bytes");
1051 }
1052 ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
1053 ofpbuf_uninit(&packet);
1054}
1055
675febfa 1056static const struct command all_commands[] = {
064af421
BP
1057 { "show", 1, 1, do_show },
1058 { "status", 1, 2, do_status },
7f1089b5 1059 { "monitor", 1, 2, do_monitor },
0caf6bde 1060 { "snoop", 1, 1, do_snoop },
064af421
BP
1061 { "dump-desc", 1, 1, do_dump_desc },
1062 { "dump-tables", 1, 1, do_dump_tables },
1063 { "dump-flows", 1, 2, do_dump_flows },
1064 { "dump-aggregate", 1, 2, do_dump_aggregate },
d2805da2 1065 { "queue-stats", 1, 3, do_queue_stats },
064af421
BP
1066 { "add-flow", 2, 2, do_add_flow },
1067 { "add-flows", 2, 2, do_add_flows },
1068 { "mod-flows", 2, 2, do_mod_flows },
1069 { "del-flows", 1, 2, do_del_flows },
abaad8cf 1070 { "dump-ports", 1, 2, do_dump_ports },
064af421
BP
1071 { "mod-port", 3, 3, do_mod_port },
1072 { "probe", 1, 1, do_probe },
1073 { "ping", 1, 2, do_ping },
1074 { "benchmark", 3, 3, do_benchmark },
064af421 1075 { "help", 0, INT_MAX, do_help },
09246b99
BP
1076
1077 /* Undocumented commands for testing. */
770f1f66 1078 { "parse-flow", 1, 1, do_parse_flow },
09246b99
BP
1079 { "parse-flows", 1, 1, do_parse_flows },
1080 { "parse-nx-match", 0, 0, do_parse_nx_match },
fec00620 1081 { "ofp-print", 1, 2, do_ofp_print },
09246b99 1082
064af421
BP
1083 { NULL, 0, 0, NULL },
1084};