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