]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
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> | |
1e1d00a5 | 27 | #include <sys/fcntl.h> |
064af421 BP |
28 | #include <sys/stat.h> |
29 | #include <sys/time.h> | |
30 | ||
10a24935 | 31 | #include "byte-order.h" |
09246b99 | 32 | #include "classifier.h" |
064af421 | 33 | #include "command-line.h" |
1eb85ef5 | 34 | #include "daemon.h" |
064af421 BP |
35 | #include "compiler.h" |
36 | #include "dirs.h" | |
09246b99 | 37 | #include "dynamic-string.h" |
09246b99 | 38 | #include "nx-match.h" |
064af421 | 39 | #include "odp-util.h" |
f25d0cf3 | 40 | #include "ofp-actions.h" |
90bf1e07 | 41 | #include "ofp-errors.h" |
f22716dc | 42 | #include "ofp-parse.h" |
064af421 | 43 | #include "ofp-print.h" |
fa37b408 | 44 | #include "ofp-util.h" |
064af421 | 45 | #include "ofpbuf.h" |
63d347ce | 46 | #include "ofproto/ofproto.h" |
064af421 BP |
47 | #include "openflow/nicira-ext.h" |
48 | #include "openflow/openflow.h" | |
0c3d5fc8 | 49 | #include "packets.h" |
1eb85ef5 | 50 | #include "poll-loop.h" |
064af421 | 51 | #include "random.h" |
fe55ad15 | 52 | #include "stream-ssl.h" |
064af421 | 53 | #include "timeval.h" |
1eb85ef5 | 54 | #include "unixctl.h" |
064af421 | 55 | #include "util.h" |
064af421 | 56 | #include "vconn.h" |
5136ce49 | 57 | #include "vlog.h" |
064af421 | 58 | |
d98e6007 | 59 | VLOG_DEFINE_THIS_MODULE(ofctl); |
064af421 | 60 | |
102ce766 EJ |
61 | /* --strict: Use strict matching for flow mod commands? Additionally governs |
62 | * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match. | |
63 | */ | |
675febfa | 64 | static bool strict; |
064af421 | 65 | |
96989efc | 66 | /* --readd: If true, on replace-flows, re-add even flows that have not changed |
c4ea79bf BP |
67 | * (to reset flow counters). */ |
68 | static bool readd; | |
69 | ||
27527aa0 BP |
70 | /* -F, --flow-format: Allowed protocols. By default, any protocol is |
71 | * allowed. */ | |
72 | static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY; | |
88ca35ee | 73 | |
54834960 EJ |
74 | /* -P, --packet-in-format: Packet IN format to use in monitor and snoop |
75 | * commands. Either one of NXPIF_* to force a particular packet_in format, or | |
76 | * -1 to let ovs-ofctl choose the default. */ | |
77 | static int preferred_packet_in_format = -1; | |
78 | ||
4f564f8d BP |
79 | /* -m, --more: Additional verbosity for ofp-print functions. */ |
80 | static int verbosity; | |
81 | ||
0c9560b7 BP |
82 | /* --timestamp: Print a timestamp before each received packet on "monitor" and |
83 | * "snoop" command? */ | |
84 | static bool timestamp; | |
85 | ||
675febfa | 86 | static const struct command all_commands[]; |
064af421 BP |
87 | |
88 | static void usage(void) NO_RETURN; | |
675febfa | 89 | static void parse_options(int argc, char *argv[]); |
064af421 | 90 | |
675febfa BP |
91 | int |
92 | main(int argc, char *argv[]) | |
064af421 | 93 | { |
064af421 | 94 | set_program_name(argv[0]); |
675febfa | 95 | parse_options(argc, argv); |
064af421 | 96 | signal(SIGPIPE, SIG_IGN); |
675febfa | 97 | run_command(argc - optind, argv + optind, all_commands); |
064af421 BP |
98 | return 0; |
99 | } | |
100 | ||
101 | static void | |
675febfa | 102 | parse_options(int argc, char *argv[]) |
064af421 BP |
103 | { |
104 | enum { | |
87c84891 | 105 | OPT_STRICT = UCHAR_MAX + 1, |
c4ea79bf | 106 | OPT_READD, |
0c9560b7 | 107 | OPT_TIMESTAMP, |
1eb85ef5 | 108 | DAEMON_OPTION_ENUMS, |
87c84891 | 109 | VLOG_OPTION_ENUMS |
064af421 BP |
110 | }; |
111 | static struct option long_options[] = { | |
e3c17733 BP |
112 | {"timeout", required_argument, NULL, 't'}, |
113 | {"strict", no_argument, NULL, OPT_STRICT}, | |
c4ea79bf | 114 | {"readd", no_argument, NULL, OPT_READD}, |
e3c17733 | 115 | {"flow-format", required_argument, NULL, 'F'}, |
54834960 | 116 | {"packet-in-format", required_argument, NULL, 'P'}, |
e3c17733 | 117 | {"more", no_argument, NULL, 'm'}, |
0c9560b7 | 118 | {"timestamp", no_argument, NULL, OPT_TIMESTAMP}, |
e3c17733 BP |
119 | {"help", no_argument, NULL, 'h'}, |
120 | {"version", no_argument, NULL, 'V'}, | |
1eb85ef5 | 121 | DAEMON_LONG_OPTIONS, |
87c84891 | 122 | VLOG_LONG_OPTIONS, |
bf8f2167 | 123 | STREAM_SSL_LONG_OPTIONS, |
e3c17733 | 124 | {NULL, 0, NULL, 0}, |
064af421 BP |
125 | }; |
126 | char *short_options = long_options_to_short_options(long_options); | |
127 | ||
064af421 BP |
128 | for (;;) { |
129 | unsigned long int timeout; | |
130 | int c; | |
131 | ||
132 | c = getopt_long(argc, argv, short_options, long_options, NULL); | |
133 | if (c == -1) { | |
134 | break; | |
135 | } | |
136 | ||
137 | switch (c) { | |
138 | case 't': | |
139 | timeout = strtoul(optarg, NULL, 10); | |
140 | if (timeout <= 0) { | |
141 | ovs_fatal(0, "value %s on -t or --timeout is not at least 1", | |
142 | optarg); | |
143 | } else { | |
144 | time_alarm(timeout); | |
145 | } | |
146 | break; | |
147 | ||
88ca35ee | 148 | case 'F': |
27527aa0 BP |
149 | allowed_protocols = ofputil_protocols_from_string(optarg); |
150 | if (!allowed_protocols) { | |
151 | ovs_fatal(0, "%s: invalid flow format(s)", optarg); | |
88ca35ee BP |
152 | } |
153 | break; | |
154 | ||
54834960 EJ |
155 | case 'P': |
156 | preferred_packet_in_format = | |
157 | ofputil_packet_in_format_from_string(optarg); | |
158 | if (preferred_packet_in_format < 0) { | |
159 | ovs_fatal(0, "unknown packet-in format `%s'", optarg); | |
160 | } | |
161 | break; | |
162 | ||
4f564f8d BP |
163 | case 'm': |
164 | verbosity++; | |
165 | break; | |
166 | ||
064af421 BP |
167 | case 'h': |
168 | usage(); | |
169 | ||
170 | case 'V': | |
87ea5e5e | 171 | ovs_print_version(OFP10_VERSION, OFP10_VERSION); |
064af421 BP |
172 | exit(EXIT_SUCCESS); |
173 | ||
064af421 | 174 | case OPT_STRICT: |
675febfa | 175 | strict = true; |
064af421 BP |
176 | break; |
177 | ||
c4ea79bf BP |
178 | case OPT_READD: |
179 | readd = true; | |
180 | break; | |
181 | ||
0c9560b7 BP |
182 | case OPT_TIMESTAMP: |
183 | timestamp = true; | |
184 | break; | |
185 | ||
1eb85ef5 | 186 | DAEMON_OPTION_HANDLERS |
87c84891 | 187 | VLOG_OPTION_HANDLERS |
fe55ad15 | 188 | STREAM_SSL_OPTION_HANDLERS |
064af421 BP |
189 | |
190 | case '?': | |
191 | exit(EXIT_FAILURE); | |
192 | ||
193 | default: | |
194 | abort(); | |
195 | } | |
196 | } | |
197 | free(short_options); | |
198 | } | |
199 | ||
200 | static void | |
201 | usage(void) | |
202 | { | |
203 | printf("%s: OpenFlow switch management utility\n" | |
204 | "usage: %s [OPTIONS] COMMAND [ARG...]\n" | |
205 | "\nFor OpenFlow switches:\n" | |
206 | " show SWITCH show OpenFlow information\n" | |
064af421 BP |
207 | " dump-desc SWITCH print switch description\n" |
208 | " dump-tables SWITCH print table stats\n" | |
209 | " mod-port SWITCH IFACE ACT modify port behavior\n" | |
7257b535 BP |
210 | " get-frags SWITCH print fragment handling behavior\n" |
211 | " set-frags SWITCH FRAG_MODE set fragment handling behavior\n" | |
abaad8cf | 212 | " dump-ports SWITCH [PORT] print port statistics\n" |
2be393ed | 213 | " dump-ports-desc SWITCH print port descriptions\n" |
064af421 BP |
214 | " dump-flows SWITCH print all flow entries\n" |
215 | " dump-flows SWITCH FLOW print matching FLOWs\n" | |
216 | " dump-aggregate SWITCH print aggregate flow statistics\n" | |
217 | " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n" | |
d2805da2 | 218 | " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n" |
064af421 BP |
219 | " add-flow SWITCH FLOW add flow described by FLOW\n" |
220 | " add-flows SWITCH FILE add flows from FILE\n" | |
221 | " mod-flows SWITCH FLOW modify actions of matching FLOWs\n" | |
222 | " del-flows SWITCH [FLOW] delete matching FLOWs\n" | |
5ff660c6 | 223 | " replace-flows SWITCH FILE replace flows with those in FILE\n" |
1dac118c | 224 | " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n" |
0c3d5fc8 BP |
225 | " packet-out SWITCH IN_PORT ACTIONS PACKET...\n" |
226 | " execute ACTIONS on PACKET\n" | |
1dac118c BP |
227 | " monitor SWITCH [MISSLEN] [invalid_ttl]\n" |
228 | " print packets received from SWITCH\n" | |
229 | " snoop SWITCH snoop on SWITCH and its controller\n" | |
064af421 | 230 | "\nFor OpenFlow switches and controllers:\n" |
2daadadd BP |
231 | " probe TARGET probe whether TARGET is up\n" |
232 | " ping TARGET [N] latency of N-byte echos\n" | |
233 | " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n" | |
234 | "where SWITCH or TARGET is an active OpenFlow connection method.\n", | |
064af421 BP |
235 | program_name, program_name); |
236 | vconn_usage(true, false, false); | |
1eb85ef5 | 237 | daemon_usage(); |
064af421 BP |
238 | vlog_usage(); |
239 | printf("\nOther options:\n" | |
240 | " --strict use strict match for flow commands\n" | |
c4ea79bf | 241 | " --readd replace flows that haven't changed\n" |
88ca35ee | 242 | " -F, --flow-format=FORMAT force particular flow format\n" |
54834960 | 243 | " -P, --packet-in-format=FRMT force particular packet in format\n" |
4f564f8d | 244 | " -m, --more be more verbose printing OpenFlow\n" |
0c9560b7 | 245 | " --timestamp (monitor, snoop) print timestamps\n" |
064af421 BP |
246 | " -t, --timeout=SECS give up after SECS seconds\n" |
247 | " -h, --help display this help message\n" | |
248 | " -V, --version display version information\n"); | |
249 | exit(EXIT_SUCCESS); | |
250 | } | |
251 | ||
1eb85ef5 EJ |
252 | static void |
253 | ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
254 | const char *argv[] OVS_UNUSED, void *exiting_) | |
255 | { | |
256 | bool *exiting = exiting_; | |
257 | *exiting = true; | |
bde9f75d | 258 | unixctl_command_reply(conn, NULL); |
1eb85ef5 EJ |
259 | } |
260 | ||
064af421 BP |
261 | static void run(int retval, const char *message, ...) |
262 | PRINTF_FORMAT(2, 3); | |
263 | ||
264 | static void run(int retval, const char *message, ...) | |
265 | { | |
266 | if (retval) { | |
267 | va_list args; | |
268 | ||
064af421 | 269 | va_start(args, message); |
fcaddd4d | 270 | ovs_fatal_valist(retval, message, args); |
064af421 BP |
271 | } |
272 | } | |
273 | \f | |
274 | /* Generic commands. */ | |
275 | ||
1a6f1e2a JG |
276 | static void |
277 | open_vconn_socket(const char *name, struct vconn **vconnp) | |
278 | { | |
279 | char *vconn_name = xasprintf("unix:%s", name); | |
24cd0dee | 280 | VLOG_DBG("connecting to %s", vconn_name); |
87ea5e5e | 281 | run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp), |
1a6f1e2a JG |
282 | "connecting to %s", vconn_name); |
283 | free(vconn_name); | |
284 | } | |
285 | ||
27527aa0 | 286 | static enum ofputil_protocol |
0caf6bde BP |
287 | open_vconn__(const char *name, const char *default_suffix, |
288 | struct vconn **vconnp) | |
064af421 | 289 | { |
63d347ce | 290 | char *datapath_name, *datapath_type, *socket_name; |
27527aa0 | 291 | enum ofputil_protocol protocol; |
63d347ce | 292 | char *bridge_path; |
27527aa0 | 293 | int ofp_version; |
064af421 | 294 | struct stat s; |
1a6f1e2a | 295 | |
b43c6fe2 | 296 | bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix); |
63d347ce BP |
297 | |
298 | ofproto_parse_name(name, &datapath_name, &datapath_type); | |
299 | socket_name = xasprintf("%s/%s.%s", | |
300 | ovs_rundir(), datapath_name, default_suffix); | |
301 | free(datapath_name); | |
302 | free(datapath_type); | |
064af421 | 303 | |
3a27375e | 304 | if (strchr(name, ':')) { |
87ea5e5e | 305 | run(vconn_open_block(name, OFP10_VERSION, vconnp), |
064af421 BP |
306 | "connecting to %s", name); |
307 | } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) { | |
1a6f1e2a JG |
308 | open_vconn_socket(name, vconnp); |
309 | } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) { | |
310 | open_vconn_socket(bridge_path, vconnp); | |
63d347ce BP |
311 | } else if (!stat(socket_name, &s)) { |
312 | if (!S_ISSOCK(s.st_mode)) { | |
064af421 BP |
313 | ovs_fatal(0, "cannot connect to %s: %s is not a socket", |
314 | name, socket_name); | |
315 | } | |
1a6f1e2a | 316 | open_vconn_socket(socket_name, vconnp); |
064af421 | 317 | } else { |
2c0e6eb4 | 318 | ovs_fatal(0, "%s is not a bridge or a socket", name); |
064af421 | 319 | } |
1a6f1e2a | 320 | |
1a6f1e2a | 321 | free(bridge_path); |
63d347ce | 322 | free(socket_name); |
27527aa0 BP |
323 | |
324 | ofp_version = vconn_get_version(*vconnp); | |
325 | protocol = ofputil_protocol_from_ofp_version(ofp_version); | |
326 | if (!protocol) { | |
327 | ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x", | |
328 | name, ofp_version); | |
329 | } | |
330 | return protocol; | |
064af421 BP |
331 | } |
332 | ||
27527aa0 | 333 | static enum ofputil_protocol |
0caf6bde BP |
334 | open_vconn(const char *name, struct vconn **vconnp) |
335 | { | |
336 | return open_vconn__(name, "mgmt", vconnp); | |
337 | } | |
338 | ||
064af421 | 339 | static void * |
eaa6eb2a | 340 | alloc_stats_request(size_t rq_len, uint16_t type, struct ofpbuf **bufferp) |
064af421 | 341 | { |
28c8bad1 | 342 | struct ofp_stats_msg *rq; |
eaa6eb2a | 343 | |
5293a2e1 | 344 | rq = make_openflow(rq_len, OFPT10_STATS_REQUEST, bufferp); |
064af421 BP |
345 | rq->type = htons(type); |
346 | rq->flags = htons(0); | |
eaa6eb2a | 347 | return rq; |
064af421 BP |
348 | } |
349 | ||
350 | static void | |
351 | send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer) | |
352 | { | |
353 | update_openflow_length(buffer); | |
354 | run(vconn_send_block(vconn, buffer), "failed to send packet to switch"); | |
355 | } | |
356 | ||
357 | static void | |
358 | dump_transaction(const char *vconn_name, struct ofpbuf *request) | |
359 | { | |
360 | struct vconn *vconn; | |
361 | struct ofpbuf *reply; | |
362 | ||
363 | update_openflow_length(request); | |
364 | open_vconn(vconn_name, &vconn); | |
365 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
4f564f8d | 366 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); |
e49190c4 | 367 | ofpbuf_delete(reply); |
064af421 BP |
368 | vconn_close(vconn); |
369 | } | |
370 | ||
371 | static void | |
372 | dump_trivial_transaction(const char *vconn_name, uint8_t request_type) | |
373 | { | |
374 | struct ofpbuf *request; | |
375 | make_openflow(sizeof(struct ofp_header), request_type, &request); | |
376 | dump_transaction(vconn_name, request); | |
377 | } | |
378 | ||
379 | static void | |
380 | dump_stats_transaction(const char *vconn_name, struct ofpbuf *request) | |
381 | { | |
44381c1b | 382 | ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid; |
a76150b1 | 383 | ovs_be16 stats_type = ((struct ofp_stats_msg *) request->data)->type; |
064af421 BP |
384 | struct vconn *vconn; |
385 | bool done = false; | |
386 | ||
387 | open_vconn(vconn_name, &vconn); | |
388 | send_openflow_buffer(vconn, request); | |
389 | while (!done) { | |
02833365 | 390 | ovs_be32 recv_xid; |
064af421 BP |
391 | struct ofpbuf *reply; |
392 | ||
393 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
394 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
395 | if (send_xid == recv_xid) { | |
a76150b1 BP |
396 | const struct ofp_stats_msg *osm = reply->data; |
397 | const struct ofp_header *oh = reply->data; | |
064af421 | 398 | |
4f564f8d | 399 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); |
064af421 | 400 | |
a76150b1 BP |
401 | if (oh->type == OFPT_ERROR) { |
402 | done = true; | |
403 | } else if (oh->type == OFPT10_STATS_REPLY | |
404 | && osm->type == stats_type) { | |
405 | done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE); | |
406 | } else { | |
407 | ovs_fatal(0, "received bad reply: %s", | |
408 | ofp_to_string(reply->data, reply->size, | |
409 | verbosity + 1)); | |
410 | } | |
064af421 BP |
411 | } else { |
412 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
413 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
414 | } | |
415 | ofpbuf_delete(reply); | |
416 | } | |
417 | vconn_close(vconn); | |
418 | } | |
419 | ||
420 | static void | |
421 | dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type) | |
422 | { | |
423 | struct ofpbuf *request; | |
eaa6eb2a | 424 | alloc_stats_request(sizeof(struct ofp_stats_msg), stats_type, &request); |
064af421 BP |
425 | dump_stats_transaction(vconn_name, request); |
426 | } | |
427 | ||
d12513f7 BP |
428 | /* Sends 'request', which should be a request that only has a reply if an error |
429 | * occurs, and waits for it to succeed or fail. If an error does occur, prints | |
7257b535 BP |
430 | * it and exits with an error. |
431 | * | |
432 | * Destroys all of the 'requests'. */ | |
d12513f7 | 433 | static void |
88ca35ee | 434 | transact_multiple_noreply(struct vconn *vconn, struct list *requests) |
d12513f7 | 435 | { |
88ca35ee | 436 | struct ofpbuf *request, *reply; |
d12513f7 | 437 | |
88ca35ee BP |
438 | LIST_FOR_EACH (request, list_node, requests) { |
439 | update_openflow_length(request); | |
440 | } | |
441 | ||
442 | run(vconn_transact_multiple_noreply(vconn, requests, &reply), | |
d12513f7 BP |
443 | "talking to %s", vconn_get_name(vconn)); |
444 | if (reply) { | |
4f564f8d | 445 | ofp_print(stderr, reply->data, reply->size, verbosity + 2); |
d12513f7 BP |
446 | exit(1); |
447 | } | |
448 | ofpbuf_delete(reply); | |
449 | } | |
450 | ||
88ca35ee BP |
451 | /* Sends 'request', which should be a request that only has a reply if an error |
452 | * occurs, and waits for it to succeed or fail. If an error does occur, prints | |
7257b535 BP |
453 | * it and exits with an error. |
454 | * | |
455 | * Destroys 'request'. */ | |
88ca35ee BP |
456 | static void |
457 | transact_noreply(struct vconn *vconn, struct ofpbuf *request) | |
458 | { | |
459 | struct list requests; | |
460 | ||
461 | list_init(&requests); | |
462 | list_push_back(&requests, &request->list_node); | |
463 | transact_multiple_noreply(vconn, &requests); | |
464 | } | |
465 | ||
7257b535 BP |
466 | static void |
467 | fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_) | |
468 | { | |
469 | struct ofp_switch_config *config; | |
470 | struct ofp_header *header; | |
471 | struct ofpbuf *request; | |
472 | struct ofpbuf *reply; | |
473 | ||
474 | make_openflow(sizeof(struct ofp_header), OFPT_GET_CONFIG_REQUEST, | |
475 | &request); | |
476 | run(vconn_transact(vconn, request, &reply), | |
477 | "talking to %s", vconn_get_name(vconn)); | |
478 | ||
479 | header = reply->data; | |
480 | if (header->type != OFPT_GET_CONFIG_REPLY || | |
481 | header->length != htons(sizeof *config)) { | |
482 | ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn)); | |
483 | } | |
484 | ||
485 | config = reply->data; | |
486 | *config_ = *config; | |
828c72d0 BP |
487 | |
488 | ofpbuf_delete(reply); | |
7257b535 BP |
489 | } |
490 | ||
491 | static void | |
492 | set_switch_config(struct vconn *vconn, struct ofp_switch_config *config_) | |
493 | { | |
494 | struct ofp_switch_config *config; | |
495 | struct ofp_header save_header; | |
496 | struct ofpbuf *request; | |
497 | ||
498 | config = make_openflow(sizeof *config, OFPT_SET_CONFIG, &request); | |
499 | save_header = config->header; | |
500 | *config = *config_; | |
501 | config->header = save_header; | |
502 | ||
503 | transact_noreply(vconn, request); | |
504 | } | |
505 | ||
064af421 | 506 | static void |
c69ee87c | 507 | do_show(int argc OVS_UNUSED, char *argv[]) |
064af421 | 508 | { |
ae0e7009 JP |
509 | const char *vconn_name = argv[1]; |
510 | struct vconn *vconn; | |
511 | struct ofpbuf *request; | |
512 | struct ofpbuf *reply; | |
513 | bool trunc; | |
514 | ||
515 | make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, | |
516 | &request); | |
517 | open_vconn(vconn_name, &vconn); | |
518 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
519 | ||
520 | trunc = ofputil_switch_features_ports_trunc(reply); | |
521 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
522 | ||
523 | ofpbuf_delete(reply); | |
524 | vconn_close(vconn); | |
525 | ||
526 | if (trunc) { | |
527 | /* The Features Reply may not contain all the ports, so send a | |
528 | * Port Description stats request, which doesn't have size | |
529 | * constraints. */ | |
530 | dump_trivial_stats_transaction(vconn_name, OFPST_PORT_DESC); | |
531 | } | |
532 | dump_trivial_transaction(vconn_name, OFPT_GET_CONFIG_REQUEST); | |
064af421 BP |
533 | } |
534 | ||
064af421 | 535 | static void |
c69ee87c | 536 | do_dump_desc(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
537 | { |
538 | dump_trivial_stats_transaction(argv[1], OFPST_DESC); | |
539 | } | |
540 | ||
541 | static void | |
c69ee87c | 542 | do_dump_tables(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
543 | { |
544 | dump_trivial_stats_transaction(argv[1], OFPST_TABLE); | |
545 | } | |
546 | ||
da91750f JP |
547 | static bool |
548 | fetch_port_by_features(const char *vconn_name, | |
549 | const char *port_name, unsigned int port_no, | |
550 | struct ofputil_phy_port *pp, bool *trunc) | |
abaad8cf | 551 | { |
9e1fd49b BP |
552 | struct ofputil_switch_features features; |
553 | const struct ofp_switch_features *osf; | |
abaad8cf | 554 | struct ofpbuf *request, *reply; |
abaad8cf | 555 | struct vconn *vconn; |
9e1fd49b BP |
556 | enum ofperr error; |
557 | struct ofpbuf b; | |
da91750f | 558 | bool found = false; |
abaad8cf | 559 | |
0df0e81d | 560 | /* Fetch the switch's ofp_switch_features. */ |
abaad8cf JP |
561 | make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request); |
562 | open_vconn(vconn_name, &vconn); | |
563 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
da91750f | 564 | vconn_close(vconn); |
abaad8cf JP |
565 | |
566 | osf = reply->data; | |
0df0e81d BP |
567 | if (reply->size < sizeof *osf) { |
568 | ovs_fatal(0, "%s: received too-short features reply (only %zu bytes)", | |
569 | vconn_name, reply->size); | |
570 | } | |
da91750f JP |
571 | |
572 | *trunc = false; | |
573 | if (ofputil_switch_features_ports_trunc(reply)) { | |
574 | *trunc = true; | |
575 | goto exit; | |
576 | } | |
577 | ||
9e1fd49b BP |
578 | error = ofputil_decode_switch_features(osf, &features, &b); |
579 | if (error) { | |
580 | ovs_fatal(0, "%s: failed to decode features reply (%s)", | |
581 | vconn_name, ofperr_to_string(error)); | |
582 | } | |
0df0e81d | 583 | |
2be393ed | 584 | while (!ofputil_pull_phy_port(osf->header.version, &b, pp)) { |
0df0e81d | 585 | if (port_no != UINT_MAX |
9e1fd49b BP |
586 | ? port_no == pp->port_no |
587 | : !strcmp(pp->name, port_name)) { | |
da91750f JP |
588 | found = true; |
589 | goto exit; | |
abaad8cf JP |
590 | } |
591 | } | |
da91750f JP |
592 | |
593 | exit: | |
594 | ofpbuf_delete(reply); | |
595 | return found; | |
596 | } | |
597 | ||
598 | static bool | |
599 | fetch_port_by_stats(const char *vconn_name, | |
600 | const char *port_name, unsigned int port_no, | |
601 | struct ofputil_phy_port *pp) | |
602 | { | |
603 | struct ofpbuf *request; | |
604 | struct vconn *vconn; | |
605 | ovs_be32 send_xid; | |
606 | struct ofpbuf b; | |
607 | bool done = false; | |
608 | bool found = false; | |
609 | ||
610 | alloc_stats_request(sizeof(struct ofp_stats_msg), OFPST_PORT_DESC, | |
611 | &request); | |
612 | send_xid = ((struct ofp_header *) request->data)->xid; | |
613 | ||
614 | open_vconn(vconn_name, &vconn); | |
615 | send_openflow_buffer(vconn, request); | |
616 | while (!done) { | |
617 | ovs_be32 recv_xid; | |
618 | struct ofpbuf *reply; | |
619 | ||
620 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
621 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
622 | if (send_xid == recv_xid) { | |
623 | const struct ofputil_msg_type *type; | |
624 | struct ofp_stats_msg *osm; | |
625 | ||
626 | ofputil_decode_msg_type(reply->data, &type); | |
627 | if (ofputil_msg_type_code(type) != OFPUTIL_OFPST_PORT_DESC_REPLY) { | |
628 | ovs_fatal(0, "received bad reply: %s", | |
629 | ofp_to_string(reply->data, reply->size, | |
630 | verbosity + 1)); | |
631 | } | |
632 | ||
f4dace9c BP |
633 | osm = ofpbuf_at_assert(reply, 0, sizeof *osm); |
634 | done = !(ntohs(osm->flags) & OFPSF_REPLY_MORE); | |
da91750f JP |
635 | |
636 | if (found) { | |
637 | /* We've already found the port, but we need to drain | |
638 | * the queue of any other replies for this request. */ | |
639 | continue; | |
640 | } | |
641 | ||
642 | ofpbuf_use_const(&b, &osm->header, ntohs(osm->header.length)); | |
643 | ofpbuf_pull(&b, sizeof(struct ofp_stats_msg)); | |
644 | ||
645 | while (!ofputil_pull_phy_port(osm->header.version, &b, pp)) { | |
646 | if (port_no != UINT_MAX ? port_no == pp->port_no | |
647 | : !strcmp(pp->name, port_name)) { | |
648 | found = true; | |
649 | break; | |
650 | } | |
651 | } | |
652 | } else { | |
653 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
654 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
655 | } | |
656 | ofpbuf_delete(reply); | |
657 | } | |
658 | vconn_close(vconn); | |
659 | ||
660 | return found; | |
661 | } | |
662 | ||
663 | ||
664 | /* Opens a connection to 'vconn_name', fetches the port structure for | |
665 | * 'port_name' (which may be a port name or number), and copies it into | |
666 | * '*pp'. */ | |
667 | static void | |
668 | fetch_ofputil_phy_port(const char *vconn_name, const char *port_name, | |
669 | struct ofputil_phy_port *pp) | |
670 | { | |
671 | unsigned int port_no; | |
672 | bool found; | |
673 | bool trunc; | |
674 | ||
675 | /* Try to interpret the argument as a port number. */ | |
676 | if (!str_to_uint(port_name, 10, &port_no)) { | |
677 | port_no = UINT_MAX; | |
678 | } | |
679 | ||
680 | /* Try to find the port based on the Features Reply. If it looks | |
681 | * like the results may be truncated, then use the Port Description | |
682 | * stats message introduced in OVS 1.7. */ | |
683 | found = fetch_port_by_features(vconn_name, port_name, port_no, pp, | |
684 | &trunc); | |
685 | if (trunc) { | |
686 | found = fetch_port_by_stats(vconn_name, port_name, port_no, pp); | |
687 | } | |
688 | ||
689 | if (!found) { | |
690 | ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name); | |
691 | } | |
0df0e81d | 692 | } |
abaad8cf | 693 | |
0df0e81d BP |
694 | /* Returns the port number corresponding to 'port_name' (which may be a port |
695 | * name or number) within the switch 'vconn_name'. */ | |
696 | static uint16_t | |
697 | str_to_port_no(const char *vconn_name, const char *port_name) | |
698 | { | |
699 | unsigned int port_no; | |
700 | ||
701 | if (str_to_uint(port_name, 10, &port_no)) { | |
702 | return port_no; | |
703 | } else { | |
9e1fd49b | 704 | struct ofputil_phy_port pp; |
abaad8cf | 705 | |
9e1fd49b BP |
706 | fetch_ofputil_phy_port(vconn_name, port_name, &pp); |
707 | return pp.port_no; | |
0df0e81d | 708 | } |
abaad8cf JP |
709 | } |
710 | ||
88ca35ee | 711 | static bool |
27527aa0 BP |
712 | try_set_protocol(struct vconn *vconn, enum ofputil_protocol want, |
713 | enum ofputil_protocol *cur) | |
88ca35ee | 714 | { |
27527aa0 BP |
715 | for (;;) { |
716 | struct ofpbuf *request, *reply; | |
717 | enum ofputil_protocol next; | |
88ca35ee | 718 | |
27527aa0 BP |
719 | request = ofputil_encode_set_protocol(*cur, want, &next); |
720 | if (!request) { | |
721 | return true; | |
722 | } | |
723 | ||
724 | run(vconn_transact_noreply(vconn, request, &reply), | |
725 | "talking to %s", vconn_get_name(vconn)); | |
726 | if (reply) { | |
727 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
728 | VLOG_DBG("%s: failed to set protocol, switch replied: %s", | |
729 | vconn_get_name(vconn), s); | |
730 | free(s); | |
731 | ofpbuf_delete(reply); | |
732 | return false; | |
733 | } | |
734 | ||
735 | *cur = next; | |
88ca35ee | 736 | } |
88ca35ee BP |
737 | } |
738 | ||
27527aa0 BP |
739 | static enum ofputil_protocol |
740 | set_protocol_for_flow_dump(struct vconn *vconn, | |
741 | enum ofputil_protocol cur_protocol, | |
742 | enum ofputil_protocol usable_protocols) | |
064af421 | 743 | { |
27527aa0 BP |
744 | char *usable_s; |
745 | int i; | |
064af421 | 746 | |
27527aa0 BP |
747 | for (i = 0; i < ofputil_n_flow_dump_protocols; i++) { |
748 | enum ofputil_protocol f = ofputil_flow_dump_protocols[i]; | |
749 | if (f & usable_protocols & allowed_protocols | |
750 | && try_set_protocol(vconn, f, &cur_protocol)) { | |
751 | return f; | |
f9cbfbe4 | 752 | } |
27527aa0 | 753 | } |
f9cbfbe4 | 754 | |
27527aa0 BP |
755 | usable_s = ofputil_protocols_to_string(usable_protocols); |
756 | if (usable_protocols & allowed_protocols) { | |
757 | ovs_fatal(0, "switch does not support any of the usable flow " | |
758 | "formats (%s)", usable_s); | |
f9cbfbe4 | 759 | } else { |
27527aa0 BP |
760 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); |
761 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
762 | "allowed flow formats (%s)", usable_s, allowed_s); | |
88ca35ee | 763 | } |
064af421 BP |
764 | } |
765 | ||
766 | static void | |
88ca35ee | 767 | do_dump_flows__(int argc, char *argv[], bool aggregate) |
064af421 | 768 | { |
27527aa0 | 769 | enum ofputil_protocol usable_protocols, protocol; |
81d1ea94 | 770 | struct ofputil_flow_stats_request fsr; |
064af421 | 771 | struct ofpbuf *request; |
88ca35ee | 772 | struct vconn *vconn; |
064af421 | 773 | |
88ca35ee | 774 | parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : ""); |
27527aa0 | 775 | usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr); |
064af421 | 776 | |
27527aa0 BP |
777 | protocol = open_vconn(argv[1], &vconn); |
778 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
779 | request = ofputil_encode_flow_stats_request(&fsr, protocol); | |
064af421 | 780 | dump_stats_transaction(argv[1], request); |
88ca35ee BP |
781 | vconn_close(vconn); |
782 | } | |
783 | ||
784 | static void | |
785 | do_dump_flows(int argc, char *argv[]) | |
786 | { | |
787 | return do_dump_flows__(argc, argv, false); | |
788 | } | |
789 | ||
790 | static void | |
791 | do_dump_aggregate(int argc, char *argv[]) | |
792 | { | |
793 | return do_dump_flows__(argc, argv, true); | |
064af421 BP |
794 | } |
795 | ||
d2805da2 BP |
796 | static void |
797 | do_queue_stats(int argc, char *argv[]) | |
798 | { | |
799 | struct ofp_queue_stats_request *req; | |
800 | struct ofpbuf *request; | |
801 | ||
802 | req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request); | |
803 | ||
804 | if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) { | |
805 | req->port_no = htons(str_to_port_no(argv[1], argv[2])); | |
806 | } else { | |
807 | req->port_no = htons(OFPP_ALL); | |
808 | } | |
809 | if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) { | |
810 | req->queue_id = htonl(atoi(argv[3])); | |
811 | } else { | |
812 | req->queue_id = htonl(OFPQ_ALL); | |
813 | } | |
814 | ||
815 | memset(req->pad, 0, sizeof req->pad); | |
816 | ||
817 | dump_stats_transaction(argv[1], request); | |
818 | } | |
819 | ||
27527aa0 BP |
820 | static enum ofputil_protocol |
821 | open_vconn_for_flow_mod(const char *remote, | |
822 | const struct ofputil_flow_mod *fms, size_t n_fms, | |
823 | struct vconn **vconnp) | |
0fbc9f11 | 824 | { |
27527aa0 BP |
825 | enum ofputil_protocol usable_protocols; |
826 | enum ofputil_protocol cur_protocol; | |
827 | char *usable_s; | |
828 | int i; | |
0fbc9f11 | 829 | |
27527aa0 BP |
830 | /* Figure out what flow formats will work. */ |
831 | usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms); | |
832 | if (!(usable_protocols & allowed_protocols)) { | |
833 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); | |
834 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
835 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
836 | "allowed flow formats (%s)", usable_s, allowed_s); | |
0fbc9f11 | 837 | } |
0fbc9f11 | 838 | |
27527aa0 BP |
839 | /* If the initial flow format is allowed and usable, keep it. */ |
840 | cur_protocol = open_vconn(remote, vconnp); | |
841 | if (usable_protocols & allowed_protocols & cur_protocol) { | |
842 | return cur_protocol; | |
843 | } | |
844 | ||
845 | /* Otherwise try each flow format in turn. */ | |
846 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
847 | enum ofputil_protocol f = 1 << i; | |
848 | ||
849 | if (f != cur_protocol | |
850 | && f & usable_protocols & allowed_protocols | |
851 | && try_set_protocol(*vconnp, f, &cur_protocol)) { | |
852 | return f; | |
853 | } | |
0fbc9f11 | 854 | } |
27527aa0 BP |
855 | |
856 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
857 | ovs_fatal(0, "switch does not support any of the usable flow " | |
858 | "formats (%s)", usable_s); | |
0fbc9f11 BP |
859 | } |
860 | ||
064af421 | 861 | static void |
27527aa0 | 862 | do_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, size_t n_fms) |
064af421 | 863 | { |
27527aa0 | 864 | enum ofputil_protocol protocol; |
064af421 | 865 | struct vconn *vconn; |
27527aa0 | 866 | size_t i; |
4989c59f | 867 | |
27527aa0 | 868 | protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn); |
049c8dc2 | 869 | |
27527aa0 BP |
870 | for (i = 0; i < n_fms; i++) { |
871 | struct ofputil_flow_mod *fm = &fms[i]; | |
0fbc9f11 | 872 | |
27527aa0 | 873 | transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol)); |
f25d0cf3 | 874 | free(fm->ofpacts); |
4989c59f | 875 | } |
064af421 | 876 | vconn_close(vconn); |
88ca35ee BP |
877 | } |
878 | ||
064af421 | 879 | static void |
27527aa0 | 880 | do_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command) |
064af421 | 881 | { |
27527aa0 BP |
882 | struct ofputil_flow_mod *fms = NULL; |
883 | size_t n_fms = 0; | |
064af421 | 884 | |
27527aa0 BP |
885 | parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms); |
886 | do_flow_mod__(argv[1], fms, n_fms); | |
887 | free(fms); | |
888 | } | |
889 | ||
890 | static void | |
891 | do_flow_mod(int argc, char *argv[], uint16_t command) | |
892 | { | |
4989c59f | 893 | if (argc > 2 && !strcmp(argv[2], "-")) { |
27527aa0 BP |
894 | do_flow_mod_file(argc, argv, command); |
895 | } else { | |
896 | struct ofputil_flow_mod fm; | |
897 | parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false); | |
898 | do_flow_mod__(argv[1], &fm, 1); | |
064af421 | 899 | } |
4989c59f | 900 | } |
88ca35ee | 901 | |
4989c59f BP |
902 | static void |
903 | do_add_flow(int argc, char *argv[]) | |
904 | { | |
27527aa0 | 905 | do_flow_mod(argc, argv, OFPFC_ADD); |
4989c59f BP |
906 | } |
907 | ||
908 | static void | |
909 | do_add_flows(int argc, char *argv[]) | |
910 | { | |
27527aa0 | 911 | do_flow_mod_file(argc, argv, OFPFC_ADD); |
064af421 BP |
912 | } |
913 | ||
914 | static void | |
88ca35ee | 915 | do_mod_flows(int argc, char *argv[]) |
064af421 | 916 | { |
27527aa0 | 917 | do_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY); |
064af421 BP |
918 | } |
919 | ||
88ca35ee BP |
920 | static void |
921 | do_del_flows(int argc, char *argv[]) | |
064af421 | 922 | { |
27527aa0 | 923 | do_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE); |
064af421 BP |
924 | } |
925 | ||
54834960 EJ |
926 | static void |
927 | set_packet_in_format(struct vconn *vconn, | |
928 | enum nx_packet_in_format packet_in_format) | |
929 | { | |
930 | struct ofpbuf *spif = ofputil_make_set_packet_in_format(packet_in_format); | |
931 | transact_noreply(vconn, spif); | |
932 | VLOG_DBG("%s: using user-specified packet in format %s", | |
933 | vconn_get_name(vconn), | |
934 | ofputil_packet_in_format_to_string(packet_in_format)); | |
935 | } | |
936 | ||
f0fd1a17 PS |
937 | static int |
938 | monitor_set_invalid_ttl_to_controller(struct vconn *vconn) | |
939 | { | |
940 | struct ofp_switch_config config; | |
941 | enum ofp_config_flags flags; | |
942 | ||
943 | fetch_switch_config(vconn, &config); | |
944 | flags = ntohs(config.flags); | |
945 | if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) { | |
946 | /* Set the invalid ttl config. */ | |
947 | flags |= OFPC_INVALID_TTL_TO_CONTROLLER; | |
948 | ||
949 | config.flags = htons(flags); | |
950 | set_switch_config(vconn, &config); | |
951 | ||
952 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
953 | * doesn't define error reporting for bad modes, so this is all we can | |
954 | * do. */ | |
955 | fetch_switch_config(vconn, &config); | |
956 | flags = ntohs(config.flags); | |
957 | if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) { | |
958 | ovs_fatal(0, "setting invalid_ttl_to_controller failed (this " | |
959 | "switch probably doesn't support mode)"); | |
960 | return -EOPNOTSUPP; | |
961 | } | |
962 | } | |
963 | return 0; | |
964 | } | |
965 | ||
96761f58 BP |
966 | /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The |
967 | * caller must free '*msgp'. On success, returns NULL. On failure, returns | |
968 | * an error message and stores NULL in '*msgp'. */ | |
969 | static const char * | |
970 | openflow_from_hex(const char *hex, struct ofpbuf **msgp) | |
971 | { | |
972 | struct ofp_header *oh; | |
973 | struct ofpbuf *msg; | |
974 | ||
975 | msg = ofpbuf_new(strlen(hex) / 2); | |
976 | *msgp = NULL; | |
977 | ||
978 | if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') { | |
979 | ofpbuf_delete(msg); | |
980 | return "Trailing garbage in hex data"; | |
981 | } | |
982 | ||
983 | if (msg->size < sizeof(struct ofp_header)) { | |
984 | ofpbuf_delete(msg); | |
985 | return "Message too short for OpenFlow"; | |
986 | } | |
987 | ||
988 | oh = msg->data; | |
989 | if (msg->size != ntohs(oh->length)) { | |
990 | ofpbuf_delete(msg); | |
991 | return "Message size does not match length in OpenFlow header"; | |
992 | } | |
993 | ||
994 | *msgp = msg; | |
995 | return NULL; | |
996 | } | |
997 | ||
998 | static void | |
999 | ofctl_send(struct unixctl_conn *conn, int argc, | |
1000 | const char *argv[], void *vconn_) | |
1001 | { | |
1002 | struct vconn *vconn = vconn_; | |
1003 | struct ds reply; | |
1004 | bool ok; | |
1005 | int i; | |
1006 | ||
1007 | ok = true; | |
1008 | ds_init(&reply); | |
1009 | for (i = 1; i < argc; i++) { | |
1010 | const char *error_msg; | |
1011 | struct ofpbuf *msg; | |
1012 | int error; | |
1013 | ||
1014 | error_msg = openflow_from_hex(argv[i], &msg); | |
1015 | if (error_msg) { | |
1016 | ds_put_format(&reply, "%s\n", error_msg); | |
1017 | ok = false; | |
1018 | continue; | |
1019 | } | |
1020 | ||
1021 | fprintf(stderr, "send: "); | |
1022 | ofp_print(stderr, msg->data, msg->size, verbosity); | |
1023 | ||
1024 | error = vconn_send_block(vconn, msg); | |
1025 | if (error) { | |
1026 | ofpbuf_delete(msg); | |
1027 | ds_put_format(&reply, "%s\n", strerror(error)); | |
1028 | ok = false; | |
1029 | } else { | |
1030 | ds_put_cstr(&reply, "sent\n"); | |
1031 | } | |
1032 | } | |
bde9f75d EJ |
1033 | |
1034 | if (ok) { | |
1035 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1036 | } else { | |
1037 | unixctl_command_reply_error(conn, ds_cstr(&reply)); | |
1038 | } | |
96761f58 BP |
1039 | ds_destroy(&reply); |
1040 | } | |
1041 | ||
bb638b9a BP |
1042 | struct barrier_aux { |
1043 | struct vconn *vconn; /* OpenFlow connection for sending barrier. */ | |
1044 | struct unixctl_conn *conn; /* Connection waiting for barrier response. */ | |
1045 | }; | |
1046 | ||
1047 | static void | |
1048 | ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1049 | const char *argv[] OVS_UNUSED, void *aux_) | |
1050 | { | |
1051 | struct barrier_aux *aux = aux_; | |
1052 | struct ofpbuf *msg; | |
1053 | int error; | |
1054 | ||
1055 | if (aux->conn) { | |
bde9f75d | 1056 | unixctl_command_reply_error(conn, "already waiting for barrier reply"); |
bb638b9a BP |
1057 | return; |
1058 | } | |
1059 | ||
1060 | msg = ofputil_encode_barrier_request(); | |
bb638b9a BP |
1061 | error = vconn_send_block(aux->vconn, msg); |
1062 | if (error) { | |
1063 | ofpbuf_delete(msg); | |
bde9f75d | 1064 | unixctl_command_reply_error(conn, strerror(error)); |
bb638b9a BP |
1065 | } else { |
1066 | aux->conn = conn; | |
1067 | } | |
1068 | } | |
1069 | ||
1e1d00a5 BP |
1070 | static void |
1071 | ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1072 | const char *argv[], void *aux OVS_UNUSED) | |
1073 | { | |
1074 | int fd; | |
1075 | ||
1076 | fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); | |
1077 | if (fd < 0) { | |
bde9f75d | 1078 | unixctl_command_reply_error(conn, strerror(errno)); |
1e1d00a5 BP |
1079 | return; |
1080 | } | |
1081 | ||
1082 | fflush(stderr); | |
1083 | dup2(fd, STDERR_FILENO); | |
1084 | close(fd); | |
bde9f75d | 1085 | unixctl_command_reply(conn, NULL); |
1e1d00a5 BP |
1086 | } |
1087 | ||
064af421 | 1088 | static void |
0caf6bde BP |
1089 | monitor_vconn(struct vconn *vconn) |
1090 | { | |
bb638b9a | 1091 | struct barrier_aux barrier_aux = { vconn, NULL }; |
1eb85ef5 EJ |
1092 | struct unixctl_server *server; |
1093 | bool exiting = false; | |
7d0c5973 | 1094 | int error; |
1eb85ef5 | 1095 | |
7d0c5973 | 1096 | daemon_save_fd(STDERR_FILENO); |
1eb85ef5 EJ |
1097 | daemonize_start(); |
1098 | error = unixctl_server_create(NULL, &server); | |
1099 | if (error) { | |
1100 | ovs_fatal(error, "failed to create unixctl server"); | |
1101 | } | |
1102 | unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting); | |
96761f58 BP |
1103 | unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX, |
1104 | ofctl_send, vconn); | |
bb638b9a BP |
1105 | unixctl_command_register("ofctl/barrier", "", 0, 0, |
1106 | ofctl_barrier, &barrier_aux); | |
1e1d00a5 BP |
1107 | unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1, |
1108 | ofctl_set_output_file, NULL); | |
1eb85ef5 EJ |
1109 | daemonize_complete(); |
1110 | ||
0caf6bde BP |
1111 | for (;;) { |
1112 | struct ofpbuf *b; | |
1eb85ef5 EJ |
1113 | int retval; |
1114 | ||
1115 | unixctl_server_run(server); | |
1116 | ||
1117 | for (;;) { | |
bb638b9a BP |
1118 | uint8_t msg_type; |
1119 | ||
1eb85ef5 EJ |
1120 | retval = vconn_recv(vconn, &b); |
1121 | if (retval == EAGAIN) { | |
1122 | break; | |
1123 | } | |
1eb85ef5 | 1124 | run(retval, "vconn_recv"); |
31c6fcd7 | 1125 | |
0c9560b7 BP |
1126 | if (timestamp) { |
1127 | time_t now = time_wall(); | |
1128 | char s[32]; | |
1129 | ||
3123c8fd | 1130 | strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now)); |
0c9560b7 BP |
1131 | fputs(s, stderr); |
1132 | } | |
31c6fcd7 BP |
1133 | |
1134 | msg_type = ((const struct ofp_header *) b->data)->type; | |
1eb85ef5 EJ |
1135 | ofp_print(stderr, b->data, b->size, verbosity + 2); |
1136 | ofpbuf_delete(b); | |
bb638b9a | 1137 | |
5293a2e1 | 1138 | if (barrier_aux.conn && msg_type == OFPT10_BARRIER_REPLY) { |
bde9f75d | 1139 | unixctl_command_reply(barrier_aux.conn, NULL); |
bb638b9a BP |
1140 | barrier_aux.conn = NULL; |
1141 | } | |
1eb85ef5 EJ |
1142 | } |
1143 | ||
1144 | if (exiting) { | |
1145 | break; | |
1146 | } | |
1147 | ||
1148 | vconn_run(vconn); | |
1149 | vconn_run_wait(vconn); | |
1150 | vconn_recv_wait(vconn); | |
1151 | unixctl_server_wait(server); | |
1152 | poll_block(); | |
0caf6bde | 1153 | } |
828c72d0 BP |
1154 | vconn_close(vconn); |
1155 | unixctl_server_destroy(server); | |
0caf6bde BP |
1156 | } |
1157 | ||
1158 | static void | |
1159 | do_monitor(int argc, char *argv[]) | |
064af421 BP |
1160 | { |
1161 | struct vconn *vconn; | |
1162 | ||
1163 | open_vconn(argv[1], &vconn); | |
1164 | if (argc > 2) { | |
7257b535 | 1165 | struct ofp_switch_config config; |
064af421 | 1166 | |
7257b535 BP |
1167 | fetch_switch_config(vconn, &config); |
1168 | config.miss_send_len = htons(atoi(argv[2])); | |
1169 | set_switch_config(vconn, &config); | |
064af421 | 1170 | } |
f0fd1a17 PS |
1171 | if (argc > 3) { |
1172 | if (!strcmp(argv[3], "invalid_ttl")) { | |
1173 | monitor_set_invalid_ttl_to_controller(vconn); | |
1174 | } | |
1175 | } | |
ca8526e0 BP |
1176 | if (preferred_packet_in_format >= 0) { |
1177 | set_packet_in_format(vconn, preferred_packet_in_format); | |
1178 | } else { | |
1179 | struct ofpbuf *spif, *reply; | |
1180 | ||
1181 | spif = ofputil_make_set_packet_in_format(NXPIF_NXM); | |
1182 | run(vconn_transact_noreply(vconn, spif, &reply), | |
1183 | "talking to %s", vconn_get_name(vconn)); | |
1184 | if (reply) { | |
1185 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
1186 | VLOG_DBG("%s: failed to set packet in format to nxm, controller" | |
1187 | " replied: %s. Falling back to the switch default.", | |
1188 | vconn_get_name(vconn), s); | |
1189 | free(s); | |
1190 | ofpbuf_delete(reply); | |
1191 | } | |
1192 | } | |
1193 | ||
0caf6bde BP |
1194 | monitor_vconn(vconn); |
1195 | } | |
1196 | ||
1197 | static void | |
1198 | do_snoop(int argc OVS_UNUSED, char *argv[]) | |
1199 | { | |
1200 | struct vconn *vconn; | |
1201 | ||
1202 | open_vconn__(argv[1], "snoop", &vconn); | |
1203 | monitor_vconn(vconn); | |
064af421 BP |
1204 | } |
1205 | ||
1206 | static void | |
abaad8cf | 1207 | do_dump_ports(int argc, char *argv[]) |
064af421 | 1208 | { |
abaad8cf JP |
1209 | struct ofp_port_stats_request *req; |
1210 | struct ofpbuf *request; | |
1211 | uint16_t port; | |
1212 | ||
1213 | req = alloc_stats_request(sizeof *req, OFPST_PORT, &request); | |
1214 | port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE; | |
1215 | req->port_no = htons(port); | |
1216 | dump_stats_transaction(argv[1], request); | |
064af421 BP |
1217 | } |
1218 | ||
2be393ed JP |
1219 | static void |
1220 | do_dump_ports_desc(int argc OVS_UNUSED, char *argv[]) | |
1221 | { | |
1222 | dump_trivial_stats_transaction(argv[1], OFPST_PORT_DESC); | |
1223 | } | |
1224 | ||
064af421 | 1225 | static void |
c69ee87c | 1226 | do_probe(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
1227 | { |
1228 | struct ofpbuf *request; | |
1229 | struct vconn *vconn; | |
1230 | struct ofpbuf *reply; | |
1231 | ||
1232 | make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request); | |
1233 | open_vconn(argv[1], &vconn); | |
1234 | run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]); | |
1235 | if (reply->size != sizeof(struct ofp_header)) { | |
1236 | ovs_fatal(0, "reply does not match request"); | |
1237 | } | |
1238 | ofpbuf_delete(reply); | |
1239 | vconn_close(vconn); | |
1240 | } | |
1241 | ||
0c3d5fc8 BP |
1242 | static void |
1243 | do_packet_out(int argc, char *argv[]) | |
1244 | { | |
1245 | struct ofputil_packet_out po; | |
f25d0cf3 | 1246 | struct ofpbuf ofpacts; |
0c3d5fc8 BP |
1247 | struct vconn *vconn; |
1248 | int i; | |
1249 | ||
f25d0cf3 BP |
1250 | ofpbuf_init(&ofpacts, 64); |
1251 | parse_ofpacts(argv[3], &ofpacts); | |
0c3d5fc8 BP |
1252 | |
1253 | po.buffer_id = UINT32_MAX; | |
1254 | po.in_port = (!strcasecmp(argv[2], "none") ? OFPP_NONE | |
1255 | : !strcasecmp(argv[2], "local") ? OFPP_LOCAL | |
1256 | : str_to_port_no(argv[1], argv[2])); | |
f25d0cf3 BP |
1257 | po.ofpacts = ofpacts.data; |
1258 | po.ofpacts_len = ofpacts.size; | |
0c3d5fc8 BP |
1259 | |
1260 | open_vconn(argv[1], &vconn); | |
1261 | for (i = 4; i < argc; i++) { | |
1262 | struct ofpbuf *packet, *opo; | |
1263 | const char *error_msg; | |
1264 | ||
1265 | error_msg = eth_from_hex(argv[i], &packet); | |
1266 | if (error_msg) { | |
1267 | ovs_fatal(0, "%s", error_msg); | |
1268 | } | |
1269 | ||
1270 | po.packet = packet->data; | |
1271 | po.packet_len = packet->size; | |
1272 | opo = ofputil_encode_packet_out(&po); | |
1273 | transact_noreply(vconn, opo); | |
1274 | ofpbuf_delete(packet); | |
1275 | } | |
1276 | vconn_close(vconn); | |
f25d0cf3 | 1277 | ofpbuf_uninit(&ofpacts); |
0c3d5fc8 BP |
1278 | } |
1279 | ||
064af421 | 1280 | static void |
c69ee87c | 1281 | do_mod_port(int argc OVS_UNUSED, char *argv[]) |
064af421 | 1282 | { |
28124950 BP |
1283 | struct ofp_config_flag { |
1284 | const char *name; /* The flag's name. */ | |
1285 | enum ofputil_port_config bit; /* Bit to turn on or off. */ | |
1286 | bool on; /* Value to set the bit to. */ | |
1287 | }; | |
1288 | static const struct ofp_config_flag flags[] = { | |
1289 | { "up", OFPUTIL_PC_PORT_DOWN, false }, | |
1290 | { "down", OFPUTIL_PC_PORT_DOWN, true }, | |
1291 | { "stp", OFPUTIL_PC_NO_STP, false }, | |
1292 | { "receive", OFPUTIL_PC_NO_RECV, false }, | |
1293 | { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false }, | |
1294 | { "flood", OFPUTIL_PC_NO_FLOOD, false }, | |
1295 | { "forward", OFPUTIL_PC_NO_FWD, false }, | |
1296 | { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false }, | |
1297 | }; | |
1298 | ||
1299 | const struct ofp_config_flag *flag; | |
9e1fd49b BP |
1300 | enum ofputil_protocol protocol; |
1301 | struct ofputil_port_mod pm; | |
1302 | struct ofputil_phy_port pp; | |
064af421 | 1303 | struct vconn *vconn; |
28124950 BP |
1304 | const char *command; |
1305 | bool not; | |
064af421 | 1306 | |
9e1fd49b | 1307 | fetch_ofputil_phy_port(argv[1], argv[2], &pp); |
064af421 | 1308 | |
9e1fd49b BP |
1309 | pm.port_no = pp.port_no; |
1310 | memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN); | |
1311 | pm.config = 0; | |
1312 | pm.mask = 0; | |
1313 | pm.advertise = 0; | |
064af421 | 1314 | |
28124950 BP |
1315 | if (!strncasecmp(argv[3], "no-", 3)) { |
1316 | command = argv[3] + 3; | |
1317 | not = true; | |
1318 | } else if (!strncasecmp(argv[3], "no", 2)) { | |
1319 | command = argv[3] + 2; | |
1320 | not = true; | |
064af421 | 1321 | } else { |
28124950 BP |
1322 | command = argv[3]; |
1323 | not = false; | |
1324 | } | |
1325 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { | |
1326 | if (!strcasecmp(command, flag->name)) { | |
1327 | pm.mask = flag->bit; | |
1328 | pm.config = flag->on ^ not ? flag->bit : 0; | |
1329 | goto found; | |
1330 | } | |
064af421 | 1331 | } |
28124950 | 1332 | ovs_fatal(0, "unknown mod-port command '%s'", argv[3]); |
064af421 | 1333 | |
28124950 | 1334 | found: |
9e1fd49b BP |
1335 | protocol = open_vconn(argv[1], &vconn); |
1336 | transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol)); | |
064af421 BP |
1337 | vconn_close(vconn); |
1338 | } | |
1339 | ||
7257b535 BP |
1340 | static void |
1341 | do_get_frags(int argc OVS_UNUSED, char *argv[]) | |
1342 | { | |
1343 | struct ofp_switch_config config; | |
1344 | struct vconn *vconn; | |
1345 | ||
1346 | open_vconn(argv[1], &vconn); | |
1347 | fetch_switch_config(vconn, &config); | |
1348 | puts(ofputil_frag_handling_to_string(ntohs(config.flags))); | |
1349 | vconn_close(vconn); | |
1350 | } | |
1351 | ||
1352 | static void | |
1353 | do_set_frags(int argc OVS_UNUSED, char *argv[]) | |
1354 | { | |
1355 | struct ofp_switch_config config; | |
1356 | enum ofp_config_flags mode; | |
1357 | struct vconn *vconn; | |
1358 | ovs_be16 flags; | |
1359 | ||
1360 | if (!ofputil_frag_handling_from_string(argv[2], &mode)) { | |
1361 | ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]); | |
1362 | } | |
1363 | ||
1364 | open_vconn(argv[1], &vconn); | |
1365 | fetch_switch_config(vconn, &config); | |
1366 | flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK)); | |
1367 | if (flags != config.flags) { | |
1368 | /* Set the configuration. */ | |
1369 | config.flags = flags; | |
1370 | set_switch_config(vconn, &config); | |
1371 | ||
1372 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
1373 | * doesn't define error reporting for bad modes, so this is all we can | |
1374 | * do. */ | |
1375 | fetch_switch_config(vconn, &config); | |
1376 | if (flags != config.flags) { | |
1377 | ovs_fatal(0, "%s: setting fragment handling mode failed (this " | |
1378 | "switch probably doesn't support mode \"%s\")", | |
1379 | argv[1], ofputil_frag_handling_to_string(mode)); | |
1380 | } | |
1381 | } | |
1382 | vconn_close(vconn); | |
1383 | } | |
1384 | ||
064af421 | 1385 | static void |
675febfa | 1386 | do_ping(int argc, char *argv[]) |
064af421 BP |
1387 | { |
1388 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
1389 | unsigned int payload; | |
1390 | struct vconn *vconn; | |
1391 | int i; | |
1392 | ||
1393 | payload = argc > 2 ? atoi(argv[2]) : 64; | |
1394 | if (payload > max_payload) { | |
1395 | ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); | |
1396 | } | |
1397 | ||
1398 | open_vconn(argv[1], &vconn); | |
1399 | for (i = 0; i < 10; i++) { | |
1400 | struct timeval start, end; | |
1401 | struct ofpbuf *request, *reply; | |
1402 | struct ofp_header *rq_hdr, *rpy_hdr; | |
1403 | ||
1404 | rq_hdr = make_openflow(sizeof(struct ofp_header) + payload, | |
1405 | OFPT_ECHO_REQUEST, &request); | |
1406 | random_bytes(rq_hdr + 1, payload); | |
1407 | ||
279c9e03 | 1408 | xgettimeofday(&start); |
064af421 | 1409 | run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact"); |
279c9e03 | 1410 | xgettimeofday(&end); |
064af421 BP |
1411 | |
1412 | rpy_hdr = reply->data; | |
1413 | if (reply->size != request->size | |
1414 | || memcmp(rpy_hdr + 1, rq_hdr + 1, payload) | |
1415 | || rpy_hdr->xid != rq_hdr->xid | |
1416 | || rpy_hdr->type != OFPT_ECHO_REPLY) { | |
1417 | printf("Reply does not match request. Request:\n"); | |
4f564f8d | 1418 | ofp_print(stdout, request, request->size, verbosity + 2); |
064af421 | 1419 | printf("Reply:\n"); |
4f564f8d | 1420 | ofp_print(stdout, reply, reply->size, verbosity + 2); |
064af421 | 1421 | } |
2886875a | 1422 | printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", |
44381c1b | 1423 | reply->size - sizeof *rpy_hdr, argv[1], ntohl(rpy_hdr->xid), |
064af421 BP |
1424 | (1000*(double)(end.tv_sec - start.tv_sec)) |
1425 | + (.001*(end.tv_usec - start.tv_usec))); | |
1426 | ofpbuf_delete(request); | |
1427 | ofpbuf_delete(reply); | |
1428 | } | |
1429 | vconn_close(vconn); | |
1430 | } | |
1431 | ||
1432 | static void | |
c69ee87c | 1433 | do_benchmark(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
1434 | { |
1435 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
1436 | struct timeval start, end; | |
1437 | unsigned int payload_size, message_size; | |
1438 | struct vconn *vconn; | |
1439 | double duration; | |
1440 | int count; | |
1441 | int i; | |
1442 | ||
1443 | payload_size = atoi(argv[2]); | |
1444 | if (payload_size > max_payload) { | |
1445 | ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); | |
1446 | } | |
1447 | message_size = sizeof(struct ofp_header) + payload_size; | |
1448 | ||
1449 | count = atoi(argv[3]); | |
1450 | ||
1451 | printf("Sending %d packets * %u bytes (with header) = %u bytes total\n", | |
1452 | count, message_size, count * message_size); | |
1453 | ||
1454 | open_vconn(argv[1], &vconn); | |
279c9e03 | 1455 | xgettimeofday(&start); |
064af421 BP |
1456 | for (i = 0; i < count; i++) { |
1457 | struct ofpbuf *request, *reply; | |
1458 | struct ofp_header *rq_hdr; | |
1459 | ||
1460 | rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request); | |
1461 | memset(rq_hdr + 1, 0, payload_size); | |
1462 | run(vconn_transact(vconn, request, &reply), "transact"); | |
1463 | ofpbuf_delete(reply); | |
1464 | } | |
279c9e03 | 1465 | xgettimeofday(&end); |
064af421 BP |
1466 | vconn_close(vconn); |
1467 | ||
1468 | duration = ((1000*(double)(end.tv_sec - start.tv_sec)) | |
1469 | + (.001*(end.tv_usec - start.tv_usec))); | |
1470 | printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n", | |
1471 | duration, count / (duration / 1000.0), | |
1472 | count * message_size / (duration / 1000.0)); | |
1473 | } | |
1474 | ||
09246b99 BP |
1475 | static void |
1476 | do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
1477 | { | |
1478 | usage(); | |
1479 | } | |
1480 | \f | |
0199c526 BP |
1481 | /* replace-flows and diff-flows commands. */ |
1482 | ||
1483 | /* A flow table entry, possibly with two different versions. */ | |
1484 | struct fte { | |
1485 | struct cls_rule rule; /* Within a "struct classifier". */ | |
1486 | struct fte_version *versions[2]; | |
1487 | }; | |
1488 | ||
1489 | /* One version of a Flow Table Entry. */ | |
1490 | struct fte_version { | |
1491 | ovs_be64 cookie; | |
1492 | uint16_t idle_timeout; | |
1493 | uint16_t hard_timeout; | |
1494 | uint16_t flags; | |
f25d0cf3 BP |
1495 | struct ofpact *ofpacts; |
1496 | size_t ofpacts_len; | |
0199c526 BP |
1497 | }; |
1498 | ||
1499 | /* Frees 'version' and the data that it owns. */ | |
1500 | static void | |
1501 | fte_version_free(struct fte_version *version) | |
1502 | { | |
1503 | if (version) { | |
f25d0cf3 | 1504 | free(version->ofpacts); |
0199c526 BP |
1505 | free(version); |
1506 | } | |
1507 | } | |
1508 | ||
1509 | /* Returns true if 'a' and 'b' are the same, false if they differ. | |
1510 | * | |
1511 | * Ignores differences in 'flags' because there's no way to retrieve flags from | |
1512 | * an OpenFlow switch. We have to assume that they are the same. */ | |
1513 | static bool | |
1514 | fte_version_equals(const struct fte_version *a, const struct fte_version *b) | |
1515 | { | |
1516 | return (a->cookie == b->cookie | |
1517 | && a->idle_timeout == b->idle_timeout | |
1518 | && a->hard_timeout == b->hard_timeout | |
f25d0cf3 BP |
1519 | && ofpacts_equal(a->ofpacts, a->ofpacts_len, |
1520 | b->ofpacts, b->ofpacts_len)); | |
0199c526 BP |
1521 | } |
1522 | ||
1523 | /* Prints 'version' on stdout. Expects the caller to have printed the rule | |
1524 | * associated with the version. */ | |
1525 | static void | |
1526 | fte_version_print(const struct fte_version *version) | |
1527 | { | |
1528 | struct ds s; | |
1529 | ||
1530 | if (version->cookie != htonll(0)) { | |
1531 | printf(" cookie=0x%"PRIx64, ntohll(version->cookie)); | |
1532 | } | |
1533 | if (version->idle_timeout != OFP_FLOW_PERMANENT) { | |
1534 | printf(" idle_timeout=%"PRIu16, version->idle_timeout); | |
1535 | } | |
1536 | if (version->hard_timeout != OFP_FLOW_PERMANENT) { | |
1537 | printf(" hard_timeout=%"PRIu16, version->hard_timeout); | |
1538 | } | |
1539 | ||
1540 | ds_init(&s); | |
f25d0cf3 | 1541 | ofpacts_format(version->ofpacts, version->ofpacts_len, &s); |
0199c526 BP |
1542 | printf(" %s\n", ds_cstr(&s)); |
1543 | ds_destroy(&s); | |
1544 | } | |
1545 | ||
1546 | static struct fte * | |
1547 | fte_from_cls_rule(const struct cls_rule *cls_rule) | |
1548 | { | |
1549 | return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL; | |
1550 | } | |
1551 | ||
1552 | /* Frees 'fte' and its versions. */ | |
1553 | static void | |
1554 | fte_free(struct fte *fte) | |
1555 | { | |
1556 | if (fte) { | |
1557 | fte_version_free(fte->versions[0]); | |
1558 | fte_version_free(fte->versions[1]); | |
1559 | free(fte); | |
1560 | } | |
1561 | } | |
1562 | ||
1563 | /* Frees all of the FTEs within 'cls'. */ | |
1564 | static void | |
1565 | fte_free_all(struct classifier *cls) | |
1566 | { | |
1567 | struct cls_cursor cursor; | |
1568 | struct fte *fte, *next; | |
1569 | ||
1570 | cls_cursor_init(&cursor, cls, NULL); | |
1571 | CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) { | |
1572 | classifier_remove(cls, &fte->rule); | |
1573 | fte_free(fte); | |
1574 | } | |
e49190c4 | 1575 | classifier_destroy(cls); |
0199c526 BP |
1576 | } |
1577 | ||
1578 | /* Searches 'cls' for an FTE matching 'rule', inserting a new one if | |
1579 | * necessary. Sets 'version' as the version of that rule with the given | |
1580 | * 'index', replacing any existing version, if any. | |
1581 | * | |
1582 | * Takes ownership of 'version'. */ | |
1583 | static void | |
1584 | fte_insert(struct classifier *cls, const struct cls_rule *rule, | |
1585 | struct fte_version *version, int index) | |
1586 | { | |
1587 | struct fte *old, *fte; | |
1588 | ||
1589 | fte = xzalloc(sizeof *fte); | |
1590 | fte->rule = *rule; | |
1591 | fte->versions[index] = version; | |
1592 | ||
08944c1d | 1593 | old = fte_from_cls_rule(classifier_replace(cls, &fte->rule)); |
0199c526 BP |
1594 | if (old) { |
1595 | fte_version_free(old->versions[index]); | |
1596 | fte->versions[!index] = old->versions[!index]; | |
1597 | free(old); | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | /* Reads the flows in 'filename' as flow table entries in 'cls' for the version | |
27527aa0 BP |
1602 | * with the specified 'index'. Returns the flow formats able to represent the |
1603 | * flows that were read. */ | |
1604 | static enum ofputil_protocol | |
0199c526 BP |
1605 | read_flows_from_file(const char *filename, struct classifier *cls, int index) |
1606 | { | |
27527aa0 | 1607 | enum ofputil_protocol usable_protocols; |
0199c526 BP |
1608 | struct ds s; |
1609 | FILE *file; | |
1610 | ||
1611 | file = !strcmp(filename, "-") ? stdin : fopen(filename, "r"); | |
1612 | if (file == NULL) { | |
1613 | ovs_fatal(errno, "%s: open", filename); | |
1614 | } | |
1615 | ||
1616 | ds_init(&s); | |
27527aa0 | 1617 | usable_protocols = OFPUTIL_P_ANY; |
0199c526 BP |
1618 | while (!ds_get_preprocessed_line(&s, file)) { |
1619 | struct fte_version *version; | |
a9a2da38 | 1620 | struct ofputil_flow_mod fm; |
0199c526 | 1621 | |
c821124b | 1622 | parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true); |
0199c526 BP |
1623 | |
1624 | version = xmalloc(sizeof *version); | |
623e1caf | 1625 | version->cookie = fm.new_cookie; |
0199c526 BP |
1626 | version->idle_timeout = fm.idle_timeout; |
1627 | version->hard_timeout = fm.hard_timeout; | |
1628 | version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF_EMERG); | |
f25d0cf3 BP |
1629 | version->ofpacts = fm.ofpacts; |
1630 | version->ofpacts_len = fm.ofpacts_len; | |
0199c526 | 1631 | |
27527aa0 | 1632 | usable_protocols &= ofputil_usable_protocols(&fm.cr); |
0199c526 BP |
1633 | |
1634 | fte_insert(cls, &fm.cr, version, index); | |
1635 | } | |
1636 | ds_destroy(&s); | |
1637 | ||
1638 | if (file != stdin) { | |
1639 | fclose(file); | |
1640 | } | |
1641 | ||
27527aa0 | 1642 | return usable_protocols; |
0199c526 BP |
1643 | } |
1644 | ||
1645 | /* Reads the OpenFlow flow table from 'vconn', which has currently active flow | |
27527aa0 | 1646 | * format 'protocol', and adds them as flow table entries in 'cls' for the |
0199c526 BP |
1647 | * version with the specified 'index'. */ |
1648 | static void | |
27527aa0 BP |
1649 | read_flows_from_switch(struct vconn *vconn, |
1650 | enum ofputil_protocol protocol, | |
0199c526 BP |
1651 | struct classifier *cls, int index) |
1652 | { | |
81d1ea94 | 1653 | struct ofputil_flow_stats_request fsr; |
0199c526 BP |
1654 | struct ofpbuf *request; |
1655 | ovs_be32 send_xid; | |
1656 | bool done; | |
1657 | ||
1658 | fsr.aggregate = false; | |
1659 | cls_rule_init_catchall(&fsr.match, 0); | |
1660 | fsr.out_port = OFPP_NONE; | |
1661 | fsr.table_id = 0xff; | |
ecc798ab | 1662 | fsr.cookie = fsr.cookie_mask = htonll(0); |
27527aa0 | 1663 | request = ofputil_encode_flow_stats_request(&fsr, protocol); |
0199c526 BP |
1664 | send_xid = ((struct ofp_header *) request->data)->xid; |
1665 | send_openflow_buffer(vconn, request); | |
1666 | ||
1667 | done = false; | |
1668 | while (!done) { | |
1669 | ovs_be32 recv_xid; | |
1670 | struct ofpbuf *reply; | |
1671 | ||
1672 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
1673 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
1674 | if (send_xid == recv_xid) { | |
1675 | const struct ofputil_msg_type *type; | |
28c8bad1 | 1676 | const struct ofp_stats_msg *osm; |
0199c526 BP |
1677 | enum ofputil_msg_code code; |
1678 | ||
1679 | ofputil_decode_msg_type(reply->data, &type); | |
1680 | code = ofputil_msg_type_code(type); | |
1681 | if (code != OFPUTIL_OFPST_FLOW_REPLY && | |
1682 | code != OFPUTIL_NXST_FLOW_REPLY) { | |
1683 | ovs_fatal(0, "received bad reply: %s", | |
1684 | ofp_to_string(reply->data, reply->size, | |
1685 | verbosity + 1)); | |
1686 | } | |
1687 | ||
28c8bad1 BP |
1688 | osm = reply->data; |
1689 | if (!(osm->flags & htons(OFPSF_REPLY_MORE))) { | |
0199c526 BP |
1690 | done = true; |
1691 | } | |
1692 | ||
1693 | for (;;) { | |
1694 | struct fte_version *version; | |
1695 | struct ofputil_flow_stats fs; | |
f25d0cf3 | 1696 | struct ofpbuf ofpacts; |
0199c526 BP |
1697 | int retval; |
1698 | ||
f25d0cf3 BP |
1699 | ofpbuf_init(&ofpacts, 64); |
1700 | retval = ofputil_decode_flow_stats_reply(&fs, reply, false, | |
1701 | &ofpacts); | |
0199c526 | 1702 | if (retval) { |
f25d0cf3 | 1703 | ofpbuf_uninit(&ofpacts); |
0199c526 BP |
1704 | if (retval != EOF) { |
1705 | ovs_fatal(0, "parse error in reply"); | |
1706 | } | |
1707 | break; | |
1708 | } | |
1709 | ||
1710 | version = xmalloc(sizeof *version); | |
1711 | version->cookie = fs.cookie; | |
1712 | version->idle_timeout = fs.idle_timeout; | |
1713 | version->hard_timeout = fs.hard_timeout; | |
1714 | version->flags = 0; | |
f25d0cf3 BP |
1715 | version->ofpacts = ofpbuf_steal_data(&ofpacts); |
1716 | version->ofpacts_len = ofpacts.size; | |
0199c526 BP |
1717 | |
1718 | fte_insert(cls, &fs.rule, version, index); | |
1719 | } | |
0199c526 BP |
1720 | } else { |
1721 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
1722 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
1723 | } | |
1724 | ofpbuf_delete(reply); | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | static void | |
1729 | fte_make_flow_mod(const struct fte *fte, int index, uint16_t command, | |
27527aa0 | 1730 | enum ofputil_protocol protocol, struct list *packets) |
0199c526 BP |
1731 | { |
1732 | const struct fte_version *version = fte->versions[index]; | |
a9a2da38 | 1733 | struct ofputil_flow_mod fm; |
0199c526 BP |
1734 | struct ofpbuf *ofm; |
1735 | ||
1736 | fm.cr = fte->rule; | |
623e1caf JP |
1737 | fm.cookie = htonll(0); |
1738 | fm.cookie_mask = htonll(0); | |
1739 | fm.new_cookie = version->cookie; | |
6c1491fb | 1740 | fm.table_id = 0xff; |
0199c526 BP |
1741 | fm.command = command; |
1742 | fm.idle_timeout = version->idle_timeout; | |
1743 | fm.hard_timeout = version->hard_timeout; | |
1744 | fm.buffer_id = UINT32_MAX; | |
1745 | fm.out_port = OFPP_NONE; | |
1746 | fm.flags = version->flags; | |
1747 | if (command == OFPFC_ADD || command == OFPFC_MODIFY || | |
1748 | command == OFPFC_MODIFY_STRICT) { | |
f25d0cf3 BP |
1749 | fm.ofpacts = version->ofpacts; |
1750 | fm.ofpacts_len = version->ofpacts_len; | |
0199c526 | 1751 | } else { |
f25d0cf3 BP |
1752 | fm.ofpacts = NULL; |
1753 | fm.ofpacts_len = 0; | |
0199c526 BP |
1754 | } |
1755 | ||
27527aa0 | 1756 | ofm = ofputil_encode_flow_mod(&fm, protocol); |
0199c526 BP |
1757 | list_push_back(packets, &ofm->list_node); |
1758 | } | |
1759 | ||
1760 | static void | |
1761 | do_replace_flows(int argc OVS_UNUSED, char *argv[]) | |
1762 | { | |
1763 | enum { FILE_IDX = 0, SWITCH_IDX = 1 }; | |
27527aa0 | 1764 | enum ofputil_protocol usable_protocols, protocol; |
0199c526 BP |
1765 | struct cls_cursor cursor; |
1766 | struct classifier cls; | |
1767 | struct list requests; | |
1768 | struct vconn *vconn; | |
1769 | struct fte *fte; | |
1770 | ||
1771 | classifier_init(&cls); | |
27527aa0 | 1772 | usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX); |
0199c526 | 1773 | |
27527aa0 BP |
1774 | protocol = open_vconn(argv[1], &vconn); |
1775 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
1776 | ||
1777 | read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX); | |
0199c526 BP |
1778 | |
1779 | list_init(&requests); | |
1780 | ||
1781 | /* Delete flows that exist on the switch but not in the file. */ | |
1782 | cls_cursor_init(&cursor, &cls, NULL); | |
1783 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
1784 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
1785 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
1786 | ||
1787 | if (sw_ver && !file_ver) { | |
1788 | fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT, | |
27527aa0 | 1789 | protocol, &requests); |
0199c526 BP |
1790 | } |
1791 | } | |
1792 | ||
1793 | /* Add flows that exist in the file but not on the switch. | |
1794 | * Update flows that exist in both places but differ. */ | |
1795 | cls_cursor_init(&cursor, &cls, NULL); | |
1796 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
1797 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
1798 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
1799 | ||
c4ea79bf BP |
1800 | if (file_ver |
1801 | && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) { | |
27527aa0 | 1802 | fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests); |
0199c526 BP |
1803 | } |
1804 | } | |
1805 | transact_multiple_noreply(vconn, &requests); | |
1806 | vconn_close(vconn); | |
1807 | ||
1808 | fte_free_all(&cls); | |
1809 | } | |
1810 | ||
1811 | static void | |
1812 | read_flows_from_source(const char *source, struct classifier *cls, int index) | |
1813 | { | |
1814 | struct stat s; | |
1815 | ||
1816 | if (source[0] == '/' || source[0] == '.' | |
1817 | || (!strchr(source, ':') && !stat(source, &s))) { | |
1818 | read_flows_from_file(source, cls, index); | |
1819 | } else { | |
27527aa0 | 1820 | enum ofputil_protocol protocol; |
0199c526 BP |
1821 | struct vconn *vconn; |
1822 | ||
27527aa0 BP |
1823 | protocol = open_vconn(source, &vconn); |
1824 | protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY); | |
1825 | read_flows_from_switch(vconn, protocol, cls, index); | |
0199c526 BP |
1826 | vconn_close(vconn); |
1827 | } | |
1828 | } | |
1829 | ||
1830 | static void | |
1831 | do_diff_flows(int argc OVS_UNUSED, char *argv[]) | |
1832 | { | |
1833 | bool differences = false; | |
1834 | struct cls_cursor cursor; | |
1835 | struct classifier cls; | |
1836 | struct fte *fte; | |
1837 | ||
1838 | classifier_init(&cls); | |
1839 | read_flows_from_source(argv[1], &cls, 0); | |
1840 | read_flows_from_source(argv[2], &cls, 1); | |
1841 | ||
1842 | cls_cursor_init(&cursor, &cls, NULL); | |
1843 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
1844 | struct fte_version *a = fte->versions[0]; | |
1845 | struct fte_version *b = fte->versions[1]; | |
1846 | ||
1847 | if (!a || !b || !fte_version_equals(a, b)) { | |
1848 | char *rule_s = cls_rule_to_string(&fte->rule); | |
1849 | if (a) { | |
1850 | printf("-%s", rule_s); | |
1851 | fte_version_print(a); | |
1852 | } | |
1853 | if (b) { | |
1854 | printf("+%s", rule_s); | |
1855 | fte_version_print(b); | |
1856 | } | |
1857 | free(rule_s); | |
1858 | ||
1859 | differences = true; | |
1860 | } | |
1861 | } | |
1862 | ||
1863 | fte_free_all(&cls); | |
1864 | ||
1865 | if (differences) { | |
1866 | exit(2); | |
1867 | } | |
1868 | } | |
1869 | \f | |
09246b99 BP |
1870 | /* Undocumented commands for unit testing. */ |
1871 | ||
770f1f66 | 1872 | static void |
27527aa0 | 1873 | do_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms) |
770f1f66 | 1874 | { |
27527aa0 BP |
1875 | enum ofputil_protocol usable_protocols; |
1876 | enum ofputil_protocol protocol = 0; | |
1877 | char *usable_s; | |
1878 | size_t i; | |
770f1f66 | 1879 | |
27527aa0 BP |
1880 | usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms); |
1881 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
1882 | printf("usable protocols: %s\n", usable_s); | |
1883 | free(usable_s); | |
1884 | ||
1885 | if (!(usable_protocols & allowed_protocols)) { | |
1886 | ovs_fatal(0, "no usable protocol"); | |
1887 | } | |
1888 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
1889 | protocol = 1 << i; | |
1890 | if (protocol & usable_protocols & allowed_protocols) { | |
1891 | break; | |
1892 | } | |
1893 | } | |
1894 | assert(IS_POW2(protocol)); | |
1895 | ||
1896 | printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol)); | |
1897 | ||
1898 | for (i = 0; i < n_fms; i++) { | |
1899 | struct ofputil_flow_mod *fm = &fms[i]; | |
1900 | struct ofpbuf *msg; | |
1901 | ||
1902 | msg = ofputil_encode_flow_mod(fm, protocol); | |
1903 | ofp_print(stdout, msg->data, msg->size, verbosity); | |
1904 | ofpbuf_delete(msg); | |
1905 | ||
f25d0cf3 | 1906 | free(fm->ofpacts); |
770f1f66 BP |
1907 | } |
1908 | } | |
1909 | ||
1910 | /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints | |
1911 | * it back to stdout. */ | |
1912 | static void | |
1913 | do_parse_flow(int argc OVS_UNUSED, char *argv[]) | |
1914 | { | |
27527aa0 | 1915 | struct ofputil_flow_mod fm; |
770f1f66 | 1916 | |
27527aa0 BP |
1917 | parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false); |
1918 | do_parse_flows__(&fm, 1); | |
770f1f66 BP |
1919 | } |
1920 | ||
fec00620 BP |
1921 | /* "parse-flows FILENAME": reads the named file as a sequence of flows (like |
1922 | * add-flows) and prints each of the flows back to stdout. */ | |
0e581146 BP |
1923 | static void |
1924 | do_parse_flows(int argc OVS_UNUSED, char *argv[]) | |
1925 | { | |
27527aa0 BP |
1926 | struct ofputil_flow_mod *fms = NULL; |
1927 | size_t n_fms = 0; | |
0e581146 | 1928 | |
27527aa0 BP |
1929 | parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms); |
1930 | do_parse_flows__(fms, n_fms); | |
1931 | free(fms); | |
0e581146 BP |
1932 | } |
1933 | ||
064af421 | 1934 | static void |
b5ae8913 | 1935 | do_parse_nxm__(bool oxm) |
09246b99 BP |
1936 | { |
1937 | struct ds in; | |
1938 | ||
1939 | ds_init(&in); | |
06d7ae7d | 1940 | while (!ds_get_test_line(&in, stdin)) { |
09246b99 BP |
1941 | struct ofpbuf nx_match; |
1942 | struct cls_rule rule; | |
e729e793 | 1943 | ovs_be64 cookie, cookie_mask; |
90bf1e07 | 1944 | enum ofperr error; |
09246b99 | 1945 | int match_len; |
09246b99 BP |
1946 | |
1947 | /* Convert string to nx_match. */ | |
1948 | ofpbuf_init(&nx_match, 0); | |
1949 | match_len = nx_match_from_string(ds_cstr(&in), &nx_match); | |
1950 | ||
1951 | /* Convert nx_match to cls_rule. */ | |
102ce766 EJ |
1952 | if (strict) { |
1953 | error = nx_pull_match(&nx_match, match_len, 0, &rule, | |
1954 | &cookie, &cookie_mask); | |
1955 | } else { | |
1956 | error = nx_pull_match_loose(&nx_match, match_len, 0, &rule, | |
1957 | &cookie, &cookie_mask); | |
1958 | } | |
1959 | ||
09246b99 BP |
1960 | if (!error) { |
1961 | char *out; | |
1962 | ||
1963 | /* Convert cls_rule back to nx_match. */ | |
1964 | ofpbuf_uninit(&nx_match); | |
1965 | ofpbuf_init(&nx_match, 0); | |
b5ae8913 SH |
1966 | match_len = nx_put_match(&nx_match, oxm, &rule, |
1967 | cookie, cookie_mask); | |
09246b99 BP |
1968 | |
1969 | /* Convert nx_match to string. */ | |
1970 | out = nx_match_to_string(nx_match.data, match_len); | |
1971 | puts(out); | |
1972 | free(out); | |
1973 | } else { | |
90bf1e07 BP |
1974 | printf("nx_pull_match() returned error %s\n", |
1975 | ofperr_get_name(error)); | |
09246b99 BP |
1976 | } |
1977 | ||
1978 | ofpbuf_uninit(&nx_match); | |
1979 | } | |
1980 | ds_destroy(&in); | |
064af421 BP |
1981 | } |
1982 | ||
b5ae8913 SH |
1983 | /* "parse-nxm": reads a series of NXM nx_match specifications as strings from |
1984 | * stdin, does some internal fussing with them, and then prints them back as | |
1985 | * strings on stdout. */ | |
1986 | static void | |
1987 | do_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
1988 | { | |
1989 | return do_parse_nxm__(false); | |
1990 | } | |
1991 | ||
1992 | /* "parse-oxm": reads a series of OXM nx_match specifications as strings from | |
1993 | * stdin, does some internal fussing with them, and then prints them back as | |
1994 | * strings on stdout. */ | |
1995 | static void | |
1996 | do_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
1997 | { | |
1998 | return do_parse_nxm__(true); | |
1999 | } | |
2000 | ||
f25d0cf3 BP |
2001 | static void |
2002 | print_differences(const void *a_, size_t a_len, | |
2003 | const void *b_, size_t b_len) | |
2004 | { | |
2005 | const uint8_t *a = a_; | |
2006 | const uint8_t *b = b_; | |
2007 | size_t i; | |
2008 | ||
2009 | for (i = 0; i < MIN(a_len, b_len); i++) { | |
2010 | if (a[i] != b[i]) { | |
2011 | printf("%2zu: %02"PRIx8" -> %02"PRIx8"\n", i, a[i], b[i]); | |
2012 | } | |
2013 | } | |
2014 | for (i = a_len; i < b_len; i++) { | |
2015 | printf("%2zu: (none) -> %02"PRIx8"\n", i, b[i]); | |
2016 | } | |
2017 | for (i = b_len; i < a_len; i++) { | |
2018 | printf("%2zu: %02"PRIx8" -> (none)\n", i, a[i]); | |
2019 | } | |
2020 | } | |
2021 | ||
2022 | /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications | |
2023 | * as hex bytes from stdin, converts them to ofpacts, prints them as strings | |
2024 | * on stdout, and then converts them back to hex bytes and prints any | |
2025 | * differences from the input. */ | |
2026 | static void | |
2027 | do_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
2028 | { | |
2029 | struct ds in; | |
2030 | ||
2031 | ds_init(&in); | |
2032 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2033 | struct ofpbuf of10_out; | |
2034 | struct ofpbuf of10_in; | |
2035 | struct ofpbuf ofpacts; | |
2036 | enum ofperr error; | |
2037 | size_t size; | |
2038 | struct ds s; | |
2039 | ||
2040 | /* Parse hex bytes. */ | |
2041 | ofpbuf_init(&of10_in, 0); | |
2042 | if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2043 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2044 | } | |
2045 | ||
2046 | /* Convert to ofpacts. */ | |
2047 | ofpbuf_init(&ofpacts, 0); | |
2048 | size = of10_in.size; | |
2049 | error = ofpacts_pull_openflow(&of10_in, of10_in.size, &ofpacts); | |
2050 | if (error) { | |
2051 | printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error)); | |
2052 | ofpbuf_uninit(&ofpacts); | |
2053 | ofpbuf_uninit(&of10_in); | |
2054 | continue; | |
2055 | } | |
2056 | ofpbuf_push_uninit(&of10_in, size); | |
2057 | ||
2058 | /* Print cls_rule. */ | |
2059 | ds_init(&s); | |
2060 | ofpacts_format(ofpacts.data, ofpacts.size, &s); | |
2061 | puts(ds_cstr(&s)); | |
2062 | ds_destroy(&s); | |
2063 | ||
2064 | /* Convert back to ofp10 actions and print differences from input. */ | |
2065 | ofpbuf_init(&of10_out, 0); | |
2066 | ofpacts_to_openflow(ofpacts.data, ofpacts.size, &of10_out); | |
2067 | ||
2068 | print_differences(of10_in.data, of10_in.size, | |
2069 | of10_out.data, of10_out.size); | |
2070 | putchar('\n'); | |
2071 | ||
2072 | ofpbuf_uninit(&ofpacts); | |
2073 | ofpbuf_uninit(&of10_in); | |
2074 | ofpbuf_uninit(&of10_out); | |
2075 | } | |
2076 | ds_destroy(&in); | |
2077 | } | |
2078 | ||
410698cf BP |
2079 | /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex |
2080 | * bytes from stdin, converts them to cls_rules, prints them as strings on | |
2081 | * stdout, and then converts them back to hex bytes and prints any differences | |
2082 | * from the input. */ | |
2083 | static void | |
2084 | do_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
2085 | { | |
2086 | struct ds in; | |
2087 | ||
2088 | ds_init(&in); | |
2089 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2090 | struct ofpbuf match_in; | |
2091 | struct ofp11_match match_out; | |
2092 | struct cls_rule rule; | |
2093 | enum ofperr error; | |
2094 | int i; | |
2095 | ||
2096 | /* Parse hex bytes. */ | |
2097 | ofpbuf_init(&match_in, 0); | |
2098 | if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2099 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2100 | } | |
2101 | if (match_in.size != sizeof(struct ofp11_match)) { | |
2102 | ovs_fatal(0, "Input is %zu bytes, expected %zu", | |
2103 | match_in.size, sizeof(struct ofp11_match)); | |
2104 | } | |
2105 | ||
2106 | /* Convert to cls_rule. */ | |
2107 | error = ofputil_cls_rule_from_ofp11_match(match_in.data, | |
2108 | OFP_DEFAULT_PRIORITY, &rule); | |
2109 | if (error) { | |
2110 | printf("bad ofp11_match: %s\n\n", ofperr_get_name(error)); | |
2111 | ofpbuf_uninit(&match_in); | |
2112 | continue; | |
2113 | } | |
2114 | ||
2115 | /* Print cls_rule. */ | |
2116 | cls_rule_print(&rule); | |
2117 | ||
2118 | /* Convert back to ofp11_match and print differences from input. */ | |
2119 | ofputil_cls_rule_to_ofp11_match(&rule, &match_out); | |
2120 | ||
2121 | for (i = 0; i < sizeof match_out; i++) { | |
2122 | uint8_t in = ((const uint8_t *) match_in.data)[i]; | |
2123 | uint8_t out = ((const uint8_t *) &match_out)[i]; | |
2124 | ||
2125 | if (in != out) { | |
2126 | printf("%2d: %02"PRIx8" -> %02"PRIx8"\n", i, in, out); | |
2127 | } | |
2128 | } | |
2129 | putchar('\n'); | |
2130 | ||
2131 | ofpbuf_uninit(&match_in); | |
2132 | } | |
2133 | ds_destroy(&in); | |
2134 | } | |
2135 | ||
2e0525bc SH |
2136 | /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow |
2137 | * version. */ | |
2138 | static void | |
2139 | do_print_error(int argc OVS_UNUSED, char *argv[]) | |
2140 | { | |
2141 | enum ofperr error; | |
2142 | int version; | |
2143 | ||
2144 | error = ofperr_from_name(argv[1]); | |
2145 | if (!error) { | |
2146 | ovs_fatal(0, "unknown error \"%s\"", argv[1]); | |
2147 | } | |
2148 | ||
2149 | for (version = 0; version <= UINT8_MAX; version++) { | |
2150 | const struct ofperr_domain *domain; | |
2151 | ||
2152 | domain = ofperr_domain_from_version(version); | |
2153 | if (!domain) { | |
2154 | continue; | |
2155 | } | |
2156 | ||
2157 | printf("%s: %d,%d\n", | |
2158 | ofperr_domain_get_name(domain), | |
2159 | ofperr_get_type(error, domain), | |
2160 | ofperr_get_code(error, domain)); | |
2161 | } | |
2162 | } | |
2163 | ||
fec00620 BP |
2164 | /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into |
2165 | * binary data, interpreting them as an OpenFlow message, and prints the | |
2166 | * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */ | |
2167 | static void | |
2168 | do_ofp_print(int argc, char *argv[]) | |
2169 | { | |
2170 | struct ofpbuf packet; | |
2171 | ||
2172 | ofpbuf_init(&packet, strlen(argv[1]) / 2); | |
2173 | if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') { | |
2174 | ovs_fatal(0, "trailing garbage following hex bytes"); | |
2175 | } | |
2176 | ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2); | |
2177 | ofpbuf_uninit(&packet); | |
2178 | } | |
2179 | ||
675febfa | 2180 | static const struct command all_commands[] = { |
064af421 | 2181 | { "show", 1, 1, do_show }, |
f0fd1a17 | 2182 | { "monitor", 1, 3, do_monitor }, |
0caf6bde | 2183 | { "snoop", 1, 1, do_snoop }, |
064af421 BP |
2184 | { "dump-desc", 1, 1, do_dump_desc }, |
2185 | { "dump-tables", 1, 1, do_dump_tables }, | |
2186 | { "dump-flows", 1, 2, do_dump_flows }, | |
2187 | { "dump-aggregate", 1, 2, do_dump_aggregate }, | |
d2805da2 | 2188 | { "queue-stats", 1, 3, do_queue_stats }, |
064af421 BP |
2189 | { "add-flow", 2, 2, do_add_flow }, |
2190 | { "add-flows", 2, 2, do_add_flows }, | |
2191 | { "mod-flows", 2, 2, do_mod_flows }, | |
2192 | { "del-flows", 1, 2, do_del_flows }, | |
0199c526 BP |
2193 | { "replace-flows", 2, 2, do_replace_flows }, |
2194 | { "diff-flows", 2, 2, do_diff_flows }, | |
0c3d5fc8 | 2195 | { "packet-out", 4, INT_MAX, do_packet_out }, |
abaad8cf | 2196 | { "dump-ports", 1, 2, do_dump_ports }, |
2be393ed | 2197 | { "dump-ports-desc", 1, 1, do_dump_ports_desc }, |
064af421 | 2198 | { "mod-port", 3, 3, do_mod_port }, |
7257b535 BP |
2199 | { "get-frags", 1, 1, do_get_frags }, |
2200 | { "set-frags", 2, 2, do_set_frags }, | |
064af421 BP |
2201 | { "probe", 1, 1, do_probe }, |
2202 | { "ping", 1, 2, do_ping }, | |
2203 | { "benchmark", 3, 3, do_benchmark }, | |
064af421 | 2204 | { "help", 0, INT_MAX, do_help }, |
09246b99 BP |
2205 | |
2206 | /* Undocumented commands for testing. */ | |
770f1f66 | 2207 | { "parse-flow", 1, 1, do_parse_flow }, |
09246b99 | 2208 | { "parse-flows", 1, 1, do_parse_flows }, |
b5ae8913 SH |
2209 | { "parse-nx-match", 0, 0, do_parse_nxm }, |
2210 | { "parse-nxm", 0, 0, do_parse_nxm }, | |
2211 | { "parse-oxm", 0, 0, do_parse_oxm }, | |
f25d0cf3 | 2212 | { "parse-ofp10-actions", 0, 0, do_parse_ofp10_actions }, |
410698cf | 2213 | { "parse-ofp11-match", 0, 0, do_parse_ofp11_match }, |
2e0525bc | 2214 | { "print-error", 1, 1, do_print_error }, |
fec00620 | 2215 | { "ofp-print", 1, 2, do_ofp_print }, |
09246b99 | 2216 | |
064af421 BP |
2217 | { NULL, 0, 0, NULL }, |
2218 | }; |