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