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