]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
2b07c8b1 | 18 | #include <ctype.h> |
064af421 BP |
19 | #include <errno.h> |
20 | #include <getopt.h> | |
21 | #include <inttypes.h> | |
bd6b7545 | 22 | #include <sys/socket.h> |
064af421 | 23 | #include <net/if.h> |
064af421 | 24 | #include <signal.h> |
064af421 BP |
25 | #include <stdlib.h> |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
1e1d00a5 | 28 | #include <sys/fcntl.h> |
064af421 BP |
29 | #include <sys/stat.h> |
30 | #include <sys/time.h> | |
31 | ||
10a24935 | 32 | #include "byte-order.h" |
09246b99 | 33 | #include "classifier.h" |
064af421 | 34 | #include "command-line.h" |
1eb85ef5 | 35 | #include "daemon.h" |
064af421 BP |
36 | #include "compiler.h" |
37 | #include "dirs.h" | |
09246b99 | 38 | #include "dynamic-string.h" |
09246b99 | 39 | #include "nx-match.h" |
064af421 | 40 | #include "odp-util.h" |
f25d0cf3 | 41 | #include "ofp-actions.h" |
90bf1e07 | 42 | #include "ofp-errors.h" |
982697a4 | 43 | #include "ofp-msgs.h" |
f22716dc | 44 | #include "ofp-parse.h" |
064af421 | 45 | #include "ofp-print.h" |
fa37b408 | 46 | #include "ofp-util.h" |
064af421 | 47 | #include "ofpbuf.h" |
63d347ce | 48 | #include "ofproto/ofproto.h" |
064af421 BP |
49 | #include "openflow/nicira-ext.h" |
50 | #include "openflow/openflow.h" | |
0c3d5fc8 | 51 | #include "packets.h" |
1eb85ef5 | 52 | #include "poll-loop.h" |
064af421 | 53 | #include "random.h" |
fe55ad15 | 54 | #include "stream-ssl.h" |
064af421 | 55 | #include "timeval.h" |
1eb85ef5 | 56 | #include "unixctl.h" |
064af421 | 57 | #include "util.h" |
064af421 | 58 | #include "vconn.h" |
5136ce49 | 59 | #include "vlog.h" |
bdcc5925 BP |
60 | #include "meta-flow.h" |
61 | #include "sort.h" | |
064af421 | 62 | |
d98e6007 | 63 | VLOG_DEFINE_THIS_MODULE(ofctl); |
064af421 | 64 | |
102ce766 EJ |
65 | /* --strict: Use strict matching for flow mod commands? Additionally governs |
66 | * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match. | |
67 | */ | |
675febfa | 68 | static bool strict; |
064af421 | 69 | |
96989efc | 70 | /* --readd: If true, on replace-flows, re-add even flows that have not changed |
c4ea79bf BP |
71 | * (to reset flow counters). */ |
72 | static bool readd; | |
73 | ||
27527aa0 BP |
74 | /* -F, --flow-format: Allowed protocols. By default, any protocol is |
75 | * allowed. */ | |
76 | static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY; | |
88ca35ee | 77 | |
54834960 EJ |
78 | /* -P, --packet-in-format: Packet IN format to use in monitor and snoop |
79 | * commands. Either one of NXPIF_* to force a particular packet_in format, or | |
80 | * -1 to let ovs-ofctl choose the default. */ | |
81 | static int preferred_packet_in_format = -1; | |
82 | ||
4f564f8d BP |
83 | /* -m, --more: Additional verbosity for ofp-print functions. */ |
84 | static int verbosity; | |
85 | ||
0c9560b7 BP |
86 | /* --timestamp: Print a timestamp before each received packet on "monitor" and |
87 | * "snoop" command? */ | |
88 | static bool timestamp; | |
89 | ||
bdcc5925 BP |
90 | /* --sort, --rsort: Sort order. */ |
91 | enum sort_order { SORT_ASC, SORT_DESC }; | |
92 | struct sort_criterion { | |
93 | const struct mf_field *field; /* NULL means to sort by priority. */ | |
94 | enum sort_order order; | |
95 | }; | |
96 | static struct sort_criterion *criteria; | |
97 | static size_t n_criteria, allocated_criteria; | |
98 | ||
675febfa | 99 | static const struct command all_commands[]; |
064af421 BP |
100 | |
101 | static void usage(void) NO_RETURN; | |
675febfa | 102 | static void parse_options(int argc, char *argv[]); |
064af421 | 103 | |
bdcc5925 BP |
104 | static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid, |
105 | struct ofpbuf **replyp, | |
106 | struct ofputil_flow_stats *, | |
107 | struct ofpbuf *ofpacts); | |
675febfa BP |
108 | int |
109 | main(int argc, char *argv[]) | |
064af421 | 110 | { |
064af421 | 111 | set_program_name(argv[0]); |
675febfa | 112 | parse_options(argc, argv); |
064af421 | 113 | signal(SIGPIPE, SIG_IGN); |
675febfa | 114 | run_command(argc - optind, argv + optind, all_commands); |
064af421 BP |
115 | return 0; |
116 | } | |
117 | ||
bdcc5925 BP |
118 | static void |
119 | add_sort_criterion(enum sort_order order, const char *field) | |
120 | { | |
121 | struct sort_criterion *sc; | |
122 | ||
123 | if (n_criteria >= allocated_criteria) { | |
124 | criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria); | |
125 | } | |
126 | ||
127 | sc = &criteria[n_criteria++]; | |
128 | if (!field || !strcasecmp(field, "priority")) { | |
129 | sc->field = NULL; | |
130 | } else { | |
131 | sc->field = mf_from_name(field); | |
132 | if (!sc->field) { | |
133 | ovs_fatal(0, "%s: unknown field name", field); | |
134 | } | |
135 | } | |
136 | sc->order = order; | |
137 | } | |
138 | ||
064af421 | 139 | static void |
675febfa | 140 | parse_options(int argc, char *argv[]) |
064af421 BP |
141 | { |
142 | enum { | |
87c84891 | 143 | OPT_STRICT = UCHAR_MAX + 1, |
c4ea79bf | 144 | OPT_READD, |
0c9560b7 | 145 | OPT_TIMESTAMP, |
bdcc5925 BP |
146 | OPT_SORT, |
147 | OPT_RSORT, | |
1eb85ef5 | 148 | DAEMON_OPTION_ENUMS, |
87c84891 | 149 | VLOG_OPTION_ENUMS |
064af421 BP |
150 | }; |
151 | static struct option long_options[] = { | |
e3c17733 BP |
152 | {"timeout", required_argument, NULL, 't'}, |
153 | {"strict", no_argument, NULL, OPT_STRICT}, | |
c4ea79bf | 154 | {"readd", no_argument, NULL, OPT_READD}, |
e3c17733 | 155 | {"flow-format", required_argument, NULL, 'F'}, |
54834960 | 156 | {"packet-in-format", required_argument, NULL, 'P'}, |
e3c17733 | 157 | {"more", no_argument, NULL, 'm'}, |
0c9560b7 | 158 | {"timestamp", no_argument, NULL, OPT_TIMESTAMP}, |
bdcc5925 BP |
159 | {"sort", optional_argument, NULL, OPT_SORT}, |
160 | {"rsort", optional_argument, NULL, OPT_RSORT}, | |
e3c17733 BP |
161 | {"help", no_argument, NULL, 'h'}, |
162 | {"version", no_argument, NULL, 'V'}, | |
1eb85ef5 | 163 | DAEMON_LONG_OPTIONS, |
87c84891 | 164 | VLOG_LONG_OPTIONS, |
bf8f2167 | 165 | STREAM_SSL_LONG_OPTIONS, |
e3c17733 | 166 | {NULL, 0, NULL, 0}, |
064af421 BP |
167 | }; |
168 | char *short_options = long_options_to_short_options(long_options); | |
169 | ||
064af421 BP |
170 | for (;;) { |
171 | unsigned long int timeout; | |
172 | int c; | |
173 | ||
174 | c = getopt_long(argc, argv, short_options, long_options, NULL); | |
175 | if (c == -1) { | |
176 | break; | |
177 | } | |
178 | ||
179 | switch (c) { | |
180 | case 't': | |
181 | timeout = strtoul(optarg, NULL, 10); | |
182 | if (timeout <= 0) { | |
183 | ovs_fatal(0, "value %s on -t or --timeout is not at least 1", | |
184 | optarg); | |
185 | } else { | |
186 | time_alarm(timeout); | |
187 | } | |
188 | break; | |
189 | ||
88ca35ee | 190 | case 'F': |
27527aa0 BP |
191 | allowed_protocols = ofputil_protocols_from_string(optarg); |
192 | if (!allowed_protocols) { | |
193 | ovs_fatal(0, "%s: invalid flow format(s)", optarg); | |
88ca35ee BP |
194 | } |
195 | break; | |
196 | ||
54834960 EJ |
197 | case 'P': |
198 | preferred_packet_in_format = | |
199 | ofputil_packet_in_format_from_string(optarg); | |
200 | if (preferred_packet_in_format < 0) { | |
201 | ovs_fatal(0, "unknown packet-in format `%s'", optarg); | |
202 | } | |
203 | break; | |
204 | ||
4f564f8d BP |
205 | case 'm': |
206 | verbosity++; | |
207 | break; | |
208 | ||
064af421 BP |
209 | case 'h': |
210 | usage(); | |
211 | ||
212 | case 'V': | |
87ea5e5e | 213 | ovs_print_version(OFP10_VERSION, OFP10_VERSION); |
064af421 BP |
214 | exit(EXIT_SUCCESS); |
215 | ||
064af421 | 216 | case OPT_STRICT: |
675febfa | 217 | strict = true; |
064af421 BP |
218 | break; |
219 | ||
c4ea79bf BP |
220 | case OPT_READD: |
221 | readd = true; | |
222 | break; | |
223 | ||
0c9560b7 BP |
224 | case OPT_TIMESTAMP: |
225 | timestamp = true; | |
226 | break; | |
227 | ||
bdcc5925 BP |
228 | case OPT_SORT: |
229 | add_sort_criterion(SORT_ASC, optarg); | |
230 | break; | |
231 | ||
232 | case OPT_RSORT: | |
233 | add_sort_criterion(SORT_DESC, optarg); | |
234 | break; | |
235 | ||
1eb85ef5 | 236 | DAEMON_OPTION_HANDLERS |
87c84891 | 237 | VLOG_OPTION_HANDLERS |
fe55ad15 | 238 | STREAM_SSL_OPTION_HANDLERS |
064af421 BP |
239 | |
240 | case '?': | |
241 | exit(EXIT_FAILURE); | |
242 | ||
243 | default: | |
244 | abort(); | |
245 | } | |
246 | } | |
bdcc5925 BP |
247 | |
248 | if (n_criteria) { | |
249 | /* Always do a final sort pass based on priority. */ | |
250 | add_sort_criterion(SORT_DESC, "priority"); | |
251 | } | |
252 | ||
064af421 BP |
253 | free(short_options); |
254 | } | |
255 | ||
256 | static void | |
257 | usage(void) | |
258 | { | |
259 | printf("%s: OpenFlow switch management utility\n" | |
260 | "usage: %s [OPTIONS] COMMAND [ARG...]\n" | |
261 | "\nFor OpenFlow switches:\n" | |
262 | " show SWITCH show OpenFlow information\n" | |
064af421 BP |
263 | " dump-desc SWITCH print switch description\n" |
264 | " dump-tables SWITCH print table stats\n" | |
265 | " mod-port SWITCH IFACE ACT modify port behavior\n" | |
7257b535 BP |
266 | " get-frags SWITCH print fragment handling behavior\n" |
267 | " set-frags SWITCH FRAG_MODE set fragment handling behavior\n" | |
abaad8cf | 268 | " dump-ports SWITCH [PORT] print port statistics\n" |
2be393ed | 269 | " dump-ports-desc SWITCH print port descriptions\n" |
064af421 BP |
270 | " dump-flows SWITCH print all flow entries\n" |
271 | " dump-flows SWITCH FLOW print matching FLOWs\n" | |
272 | " dump-aggregate SWITCH print aggregate flow statistics\n" | |
273 | " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n" | |
d2805da2 | 274 | " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n" |
064af421 BP |
275 | " add-flow SWITCH FLOW add flow described by FLOW\n" |
276 | " add-flows SWITCH FILE add flows from FILE\n" | |
277 | " mod-flows SWITCH FLOW modify actions of matching FLOWs\n" | |
278 | " del-flows SWITCH [FLOW] delete matching FLOWs\n" | |
5ff660c6 | 279 | " replace-flows SWITCH FILE replace flows with those in FILE\n" |
1dac118c | 280 | " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n" |
0c3d5fc8 BP |
281 | " packet-out SWITCH IN_PORT ACTIONS PACKET...\n" |
282 | " execute ACTIONS on PACKET\n" | |
2b07c8b1 | 283 | " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n" |
1dac118c BP |
284 | " print packets received from SWITCH\n" |
285 | " snoop SWITCH snoop on SWITCH and its controller\n" | |
064af421 | 286 | "\nFor OpenFlow switches and controllers:\n" |
2daadadd BP |
287 | " probe TARGET probe whether TARGET is up\n" |
288 | " ping TARGET [N] latency of N-byte echos\n" | |
289 | " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n" | |
290 | "where SWITCH or TARGET is an active OpenFlow connection method.\n", | |
064af421 BP |
291 | program_name, program_name); |
292 | vconn_usage(true, false, false); | |
1eb85ef5 | 293 | daemon_usage(); |
064af421 BP |
294 | vlog_usage(); |
295 | printf("\nOther options:\n" | |
296 | " --strict use strict match for flow commands\n" | |
c4ea79bf | 297 | " --readd replace flows that haven't changed\n" |
88ca35ee | 298 | " -F, --flow-format=FORMAT force particular flow format\n" |
54834960 | 299 | " -P, --packet-in-format=FRMT force particular packet in format\n" |
4f564f8d | 300 | " -m, --more be more verbose printing OpenFlow\n" |
0c9560b7 | 301 | " --timestamp (monitor, snoop) print timestamps\n" |
064af421 | 302 | " -t, --timeout=SECS give up after SECS seconds\n" |
bdcc5925 BP |
303 | " --sort[=field] sort in ascending order\n" |
304 | " --rsort[=field] sort in descending order\n" | |
064af421 BP |
305 | " -h, --help display this help message\n" |
306 | " -V, --version display version information\n"); | |
307 | exit(EXIT_SUCCESS); | |
308 | } | |
309 | ||
1eb85ef5 EJ |
310 | static void |
311 | ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
312 | const char *argv[] OVS_UNUSED, void *exiting_) | |
313 | { | |
314 | bool *exiting = exiting_; | |
315 | *exiting = true; | |
bde9f75d | 316 | unixctl_command_reply(conn, NULL); |
1eb85ef5 EJ |
317 | } |
318 | ||
064af421 BP |
319 | static void run(int retval, const char *message, ...) |
320 | PRINTF_FORMAT(2, 3); | |
321 | ||
322 | static void run(int retval, const char *message, ...) | |
323 | { | |
324 | if (retval) { | |
325 | va_list args; | |
326 | ||
064af421 | 327 | va_start(args, message); |
fcaddd4d | 328 | ovs_fatal_valist(retval, message, args); |
064af421 BP |
329 | } |
330 | } | |
331 | \f | |
332 | /* Generic commands. */ | |
333 | ||
1a6f1e2a JG |
334 | static void |
335 | open_vconn_socket(const char *name, struct vconn **vconnp) | |
336 | { | |
337 | char *vconn_name = xasprintf("unix:%s", name); | |
24cd0dee | 338 | VLOG_DBG("connecting to %s", vconn_name); |
87ea5e5e | 339 | run(vconn_open_block(vconn_name, OFP10_VERSION, vconnp), |
1a6f1e2a JG |
340 | "connecting to %s", vconn_name); |
341 | free(vconn_name); | |
342 | } | |
343 | ||
27527aa0 | 344 | static enum ofputil_protocol |
0caf6bde BP |
345 | open_vconn__(const char *name, const char *default_suffix, |
346 | struct vconn **vconnp) | |
064af421 | 347 | { |
63d347ce | 348 | char *datapath_name, *datapath_type, *socket_name; |
27527aa0 | 349 | enum ofputil_protocol protocol; |
63d347ce | 350 | char *bridge_path; |
27527aa0 | 351 | int ofp_version; |
064af421 | 352 | struct stat s; |
1a6f1e2a | 353 | |
b43c6fe2 | 354 | bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, default_suffix); |
63d347ce BP |
355 | |
356 | ofproto_parse_name(name, &datapath_name, &datapath_type); | |
357 | socket_name = xasprintf("%s/%s.%s", | |
358 | ovs_rundir(), datapath_name, default_suffix); | |
359 | free(datapath_name); | |
360 | free(datapath_type); | |
064af421 | 361 | |
3a27375e | 362 | if (strchr(name, ':')) { |
87ea5e5e | 363 | run(vconn_open_block(name, OFP10_VERSION, vconnp), |
064af421 BP |
364 | "connecting to %s", name); |
365 | } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) { | |
1a6f1e2a JG |
366 | open_vconn_socket(name, vconnp); |
367 | } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) { | |
368 | open_vconn_socket(bridge_path, vconnp); | |
63d347ce BP |
369 | } else if (!stat(socket_name, &s)) { |
370 | if (!S_ISSOCK(s.st_mode)) { | |
064af421 BP |
371 | ovs_fatal(0, "cannot connect to %s: %s is not a socket", |
372 | name, socket_name); | |
373 | } | |
1a6f1e2a | 374 | open_vconn_socket(socket_name, vconnp); |
064af421 | 375 | } else { |
2c0e6eb4 | 376 | ovs_fatal(0, "%s is not a bridge or a socket", name); |
064af421 | 377 | } |
1a6f1e2a | 378 | |
1a6f1e2a | 379 | free(bridge_path); |
63d347ce | 380 | free(socket_name); |
27527aa0 BP |
381 | |
382 | ofp_version = vconn_get_version(*vconnp); | |
383 | protocol = ofputil_protocol_from_ofp_version(ofp_version); | |
384 | if (!protocol) { | |
385 | ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x", | |
386 | name, ofp_version); | |
387 | } | |
388 | return protocol; | |
064af421 BP |
389 | } |
390 | ||
27527aa0 | 391 | static enum ofputil_protocol |
0caf6bde BP |
392 | open_vconn(const char *name, struct vconn **vconnp) |
393 | { | |
394 | return open_vconn__(name, "mgmt", vconnp); | |
395 | } | |
396 | ||
064af421 BP |
397 | static void |
398 | send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer) | |
399 | { | |
982697a4 | 400 | ofpmsg_update_length(buffer); |
064af421 BP |
401 | run(vconn_send_block(vconn, buffer), "failed to send packet to switch"); |
402 | } | |
403 | ||
404 | static void | |
405 | dump_transaction(const char *vconn_name, struct ofpbuf *request) | |
406 | { | |
407 | struct vconn *vconn; | |
408 | struct ofpbuf *reply; | |
409 | ||
982697a4 | 410 | ofpmsg_update_length(request); |
064af421 BP |
411 | open_vconn(vconn_name, &vconn); |
412 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
4f564f8d | 413 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); |
e49190c4 | 414 | ofpbuf_delete(reply); |
064af421 BP |
415 | vconn_close(vconn); |
416 | } | |
417 | ||
418 | static void | |
982697a4 | 419 | dump_trivial_transaction(const char *vconn_name, enum ofpraw raw) |
064af421 BP |
420 | { |
421 | struct ofpbuf *request; | |
982697a4 | 422 | request = ofpraw_alloc(raw, OFP10_VERSION, 0); |
064af421 BP |
423 | dump_transaction(vconn_name, request); |
424 | } | |
425 | ||
426 | static void | |
53514387 | 427 | dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request) |
064af421 | 428 | { |
982697a4 BP |
429 | const struct ofp_header *request_oh = request->data; |
430 | ovs_be32 send_xid = request_oh->xid; | |
431 | enum ofpraw request_raw; | |
432 | enum ofpraw reply_raw; | |
064af421 BP |
433 | bool done = false; |
434 | ||
982697a4 BP |
435 | ofpraw_decode_partial(&request_raw, request->data, request->size); |
436 | reply_raw = ofpraw_stats_request_to_reply(request_raw, | |
437 | request_oh->version); | |
438 | ||
064af421 BP |
439 | send_openflow_buffer(vconn, request); |
440 | while (!done) { | |
02833365 | 441 | ovs_be32 recv_xid; |
064af421 BP |
442 | struct ofpbuf *reply; |
443 | ||
444 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
445 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
446 | if (send_xid == recv_xid) { | |
982697a4 | 447 | enum ofpraw raw; |
064af421 | 448 | |
4f564f8d | 449 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); |
064af421 | 450 | |
982697a4 BP |
451 | ofpraw_decode(&raw, reply->data); |
452 | if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) { | |
a76150b1 | 453 | done = true; |
982697a4 BP |
454 | } else if (raw == reply_raw) { |
455 | done = !ofpmp_more(reply->data); | |
a76150b1 BP |
456 | } else { |
457 | ovs_fatal(0, "received bad reply: %s", | |
458 | ofp_to_string(reply->data, reply->size, | |
459 | verbosity + 1)); | |
460 | } | |
064af421 BP |
461 | } else { |
462 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
463 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
464 | } | |
465 | ofpbuf_delete(reply); | |
466 | } | |
9abfe557 BP |
467 | } |
468 | ||
469 | static void | |
53514387 | 470 | dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw) |
9abfe557 | 471 | { |
53514387 | 472 | struct ofpbuf *request; |
9abfe557 BP |
473 | struct vconn *vconn; |
474 | ||
475 | open_vconn(vconn_name, &vconn); | |
53514387 SH |
476 | request = ofpraw_alloc(raw, vconn_get_version(vconn), 0); |
477 | dump_stats_transaction(vconn, request); | |
064af421 BP |
478 | vconn_close(vconn); |
479 | } | |
480 | ||
d12513f7 BP |
481 | /* Sends 'request', which should be a request that only has a reply if an error |
482 | * occurs, and waits for it to succeed or fail. If an error does occur, prints | |
7257b535 BP |
483 | * it and exits with an error. |
484 | * | |
485 | * Destroys all of the 'requests'. */ | |
d12513f7 | 486 | static void |
88ca35ee | 487 | transact_multiple_noreply(struct vconn *vconn, struct list *requests) |
d12513f7 | 488 | { |
88ca35ee | 489 | struct ofpbuf *request, *reply; |
d12513f7 | 490 | |
88ca35ee | 491 | LIST_FOR_EACH (request, list_node, requests) { |
982697a4 | 492 | ofpmsg_update_length(request); |
88ca35ee BP |
493 | } |
494 | ||
495 | run(vconn_transact_multiple_noreply(vconn, requests, &reply), | |
d12513f7 BP |
496 | "talking to %s", vconn_get_name(vconn)); |
497 | if (reply) { | |
4f564f8d | 498 | ofp_print(stderr, reply->data, reply->size, verbosity + 2); |
d12513f7 BP |
499 | exit(1); |
500 | } | |
501 | ofpbuf_delete(reply); | |
502 | } | |
503 | ||
88ca35ee BP |
504 | /* Sends 'request', which should be a request that only has a reply if an error |
505 | * occurs, and waits for it to succeed or fail. If an error does occur, prints | |
7257b535 BP |
506 | * it and exits with an error. |
507 | * | |
508 | * Destroys 'request'. */ | |
88ca35ee BP |
509 | static void |
510 | transact_noreply(struct vconn *vconn, struct ofpbuf *request) | |
511 | { | |
512 | struct list requests; | |
513 | ||
514 | list_init(&requests); | |
515 | list_push_back(&requests, &request->list_node); | |
516 | transact_multiple_noreply(vconn, &requests); | |
517 | } | |
518 | ||
7257b535 BP |
519 | static void |
520 | fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_) | |
521 | { | |
522 | struct ofp_switch_config *config; | |
7257b535 BP |
523 | struct ofpbuf *request; |
524 | struct ofpbuf *reply; | |
982697a4 | 525 | enum ofptype type; |
7257b535 | 526 | |
982697a4 | 527 | request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION, 0); |
7257b535 BP |
528 | run(vconn_transact(vconn, request, &reply), |
529 | "talking to %s", vconn_get_name(vconn)); | |
530 | ||
982697a4 | 531 | if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) { |
7257b535 BP |
532 | ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn)); |
533 | } | |
534 | ||
982697a4 | 535 | config = ofpbuf_pull(reply, sizeof *config); |
7257b535 | 536 | *config_ = *config; |
828c72d0 BP |
537 | |
538 | ofpbuf_delete(reply); | |
7257b535 BP |
539 | } |
540 | ||
541 | static void | |
982697a4 | 542 | set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config) |
7257b535 | 543 | { |
7257b535 BP |
544 | struct ofpbuf *request; |
545 | ||
982697a4 BP |
546 | request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, OFP10_VERSION, 0); |
547 | ofpbuf_put(request, config, sizeof *config); | |
7257b535 BP |
548 | |
549 | transact_noreply(vconn, request); | |
550 | } | |
551 | ||
064af421 | 552 | static void |
e1fef0f9 | 553 | ofctl_show(int argc OVS_UNUSED, char *argv[]) |
064af421 | 554 | { |
ae0e7009 JP |
555 | const char *vconn_name = argv[1]; |
556 | struct vconn *vconn; | |
557 | struct ofpbuf *request; | |
558 | struct ofpbuf *reply; | |
559 | bool trunc; | |
560 | ||
982697a4 | 561 | request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0); |
ae0e7009 JP |
562 | open_vconn(vconn_name, &vconn); |
563 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
564 | ||
565 | trunc = ofputil_switch_features_ports_trunc(reply); | |
566 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
567 | ||
568 | ofpbuf_delete(reply); | |
569 | vconn_close(vconn); | |
570 | ||
571 | if (trunc) { | |
572 | /* The Features Reply may not contain all the ports, so send a | |
573 | * Port Description stats request, which doesn't have size | |
574 | * constraints. */ | |
982697a4 BP |
575 | dump_trivial_stats_transaction(vconn_name, |
576 | OFPRAW_OFPST_PORT_DESC_REQUEST); | |
ae0e7009 | 577 | } |
982697a4 | 578 | dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST); |
064af421 BP |
579 | } |
580 | ||
064af421 | 581 | static void |
e1fef0f9 | 582 | ofctl_dump_desc(int argc OVS_UNUSED, char *argv[]) |
064af421 | 583 | { |
982697a4 | 584 | dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST); |
064af421 BP |
585 | } |
586 | ||
587 | static void | |
e1fef0f9 | 588 | ofctl_dump_tables(int argc OVS_UNUSED, char *argv[]) |
064af421 | 589 | { |
982697a4 | 590 | dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST); |
064af421 BP |
591 | } |
592 | ||
da91750f JP |
593 | static bool |
594 | fetch_port_by_features(const char *vconn_name, | |
595 | const char *port_name, unsigned int port_no, | |
596 | struct ofputil_phy_port *pp, bool *trunc) | |
abaad8cf | 597 | { |
9e1fd49b | 598 | struct ofputil_switch_features features; |
982697a4 | 599 | const struct ofp_header *oh; |
abaad8cf | 600 | struct ofpbuf *request, *reply; |
abaad8cf | 601 | struct vconn *vconn; |
9e1fd49b | 602 | enum ofperr error; |
982697a4 | 603 | enum ofptype type; |
9e1fd49b | 604 | struct ofpbuf b; |
da91750f | 605 | bool found = false; |
abaad8cf | 606 | |
0df0e81d | 607 | /* Fetch the switch's ofp_switch_features. */ |
982697a4 | 608 | request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, OFP10_VERSION, 0); |
abaad8cf JP |
609 | open_vconn(vconn_name, &vconn); |
610 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
da91750f | 611 | vconn_close(vconn); |
abaad8cf | 612 | |
982697a4 BP |
613 | oh = reply->data; |
614 | if (ofptype_decode(&type, reply->data) | |
615 | || type != OFPTYPE_FEATURES_REPLY) { | |
616 | ovs_fatal(0, "%s: received bad features reply", vconn_name); | |
0df0e81d | 617 | } |
da91750f JP |
618 | |
619 | *trunc = false; | |
620 | if (ofputil_switch_features_ports_trunc(reply)) { | |
621 | *trunc = true; | |
622 | goto exit; | |
623 | } | |
624 | ||
982697a4 | 625 | error = ofputil_decode_switch_features(oh, &features, &b); |
9e1fd49b BP |
626 | if (error) { |
627 | ovs_fatal(0, "%s: failed to decode features reply (%s)", | |
628 | vconn_name, ofperr_to_string(error)); | |
629 | } | |
0df0e81d | 630 | |
982697a4 | 631 | while (!ofputil_pull_phy_port(oh->version, &b, pp)) { |
0df0e81d | 632 | if (port_no != UINT_MAX |
9e1fd49b BP |
633 | ? port_no == pp->port_no |
634 | : !strcmp(pp->name, port_name)) { | |
da91750f JP |
635 | found = true; |
636 | goto exit; | |
abaad8cf JP |
637 | } |
638 | } | |
da91750f JP |
639 | |
640 | exit: | |
641 | ofpbuf_delete(reply); | |
642 | return found; | |
643 | } | |
644 | ||
645 | static bool | |
646 | fetch_port_by_stats(const char *vconn_name, | |
647 | const char *port_name, unsigned int port_no, | |
648 | struct ofputil_phy_port *pp) | |
649 | { | |
650 | struct ofpbuf *request; | |
651 | struct vconn *vconn; | |
652 | ovs_be32 send_xid; | |
da91750f JP |
653 | bool done = false; |
654 | bool found = false; | |
655 | ||
982697a4 | 656 | request = ofpraw_alloc(OFPRAW_OFPST_PORT_DESC_REQUEST, OFP10_VERSION, 0); |
da91750f JP |
657 | send_xid = ((struct ofp_header *) request->data)->xid; |
658 | ||
659 | open_vconn(vconn_name, &vconn); | |
660 | send_openflow_buffer(vconn, request); | |
661 | while (!done) { | |
662 | ovs_be32 recv_xid; | |
663 | struct ofpbuf *reply; | |
664 | ||
665 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
666 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
667 | if (send_xid == recv_xid) { | |
982697a4 BP |
668 | struct ofp_header *oh = reply->data; |
669 | enum ofptype type; | |
670 | struct ofpbuf b; | |
671 | uint16_t flags; | |
672 | ||
673 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); | |
674 | if (ofptype_pull(&type, &b) | |
675 | || type != OFPTYPE_PORT_DESC_STATS_REPLY) { | |
da91750f JP |
676 | ovs_fatal(0, "received bad reply: %s", |
677 | ofp_to_string(reply->data, reply->size, | |
678 | verbosity + 1)); | |
679 | } | |
680 | ||
982697a4 BP |
681 | flags = ofpmp_flags(oh); |
682 | done = !(flags & OFPSF_REPLY_MORE); | |
da91750f JP |
683 | |
684 | if (found) { | |
685 | /* We've already found the port, but we need to drain | |
686 | * the queue of any other replies for this request. */ | |
687 | continue; | |
688 | } | |
689 | ||
982697a4 | 690 | while (!ofputil_pull_phy_port(oh->version, &b, pp)) { |
da91750f JP |
691 | if (port_no != UINT_MAX ? port_no == pp->port_no |
692 | : !strcmp(pp->name, port_name)) { | |
693 | found = true; | |
694 | break; | |
695 | } | |
696 | } | |
697 | } else { | |
698 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
699 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
700 | } | |
701 | ofpbuf_delete(reply); | |
702 | } | |
703 | vconn_close(vconn); | |
704 | ||
705 | return found; | |
706 | } | |
707 | ||
708 | ||
709 | /* Opens a connection to 'vconn_name', fetches the port structure for | |
710 | * 'port_name' (which may be a port name or number), and copies it into | |
711 | * '*pp'. */ | |
712 | static void | |
713 | fetch_ofputil_phy_port(const char *vconn_name, const char *port_name, | |
714 | struct ofputil_phy_port *pp) | |
715 | { | |
716 | unsigned int port_no; | |
717 | bool found; | |
718 | bool trunc; | |
719 | ||
720 | /* Try to interpret the argument as a port number. */ | |
721 | if (!str_to_uint(port_name, 10, &port_no)) { | |
722 | port_no = UINT_MAX; | |
723 | } | |
724 | ||
725 | /* Try to find the port based on the Features Reply. If it looks | |
726 | * like the results may be truncated, then use the Port Description | |
727 | * stats message introduced in OVS 1.7. */ | |
728 | found = fetch_port_by_features(vconn_name, port_name, port_no, pp, | |
729 | &trunc); | |
730 | if (trunc) { | |
731 | found = fetch_port_by_stats(vconn_name, port_name, port_no, pp); | |
732 | } | |
733 | ||
734 | if (!found) { | |
735 | ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name); | |
736 | } | |
0df0e81d | 737 | } |
abaad8cf | 738 | |
0df0e81d BP |
739 | /* Returns the port number corresponding to 'port_name' (which may be a port |
740 | * name or number) within the switch 'vconn_name'. */ | |
741 | static uint16_t | |
742 | str_to_port_no(const char *vconn_name, const char *port_name) | |
743 | { | |
c6100d92 BP |
744 | uint16_t port_no = ofputil_port_from_string(port_name); |
745 | if (port_no) { | |
0df0e81d BP |
746 | return port_no; |
747 | } else { | |
9e1fd49b | 748 | struct ofputil_phy_port pp; |
abaad8cf | 749 | |
9e1fd49b BP |
750 | fetch_ofputil_phy_port(vconn_name, port_name, &pp); |
751 | return pp.port_no; | |
0df0e81d | 752 | } |
abaad8cf JP |
753 | } |
754 | ||
88ca35ee | 755 | static bool |
27527aa0 BP |
756 | try_set_protocol(struct vconn *vconn, enum ofputil_protocol want, |
757 | enum ofputil_protocol *cur) | |
88ca35ee | 758 | { |
27527aa0 BP |
759 | for (;;) { |
760 | struct ofpbuf *request, *reply; | |
761 | enum ofputil_protocol next; | |
88ca35ee | 762 | |
27527aa0 BP |
763 | request = ofputil_encode_set_protocol(*cur, want, &next); |
764 | if (!request) { | |
765 | return true; | |
766 | } | |
767 | ||
768 | run(vconn_transact_noreply(vconn, request, &reply), | |
769 | "talking to %s", vconn_get_name(vconn)); | |
770 | if (reply) { | |
771 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
772 | VLOG_DBG("%s: failed to set protocol, switch replied: %s", | |
773 | vconn_get_name(vconn), s); | |
774 | free(s); | |
775 | ofpbuf_delete(reply); | |
776 | return false; | |
777 | } | |
778 | ||
779 | *cur = next; | |
88ca35ee | 780 | } |
88ca35ee BP |
781 | } |
782 | ||
27527aa0 BP |
783 | static enum ofputil_protocol |
784 | set_protocol_for_flow_dump(struct vconn *vconn, | |
785 | enum ofputil_protocol cur_protocol, | |
786 | enum ofputil_protocol usable_protocols) | |
064af421 | 787 | { |
27527aa0 BP |
788 | char *usable_s; |
789 | int i; | |
064af421 | 790 | |
27527aa0 BP |
791 | for (i = 0; i < ofputil_n_flow_dump_protocols; i++) { |
792 | enum ofputil_protocol f = ofputil_flow_dump_protocols[i]; | |
793 | if (f & usable_protocols & allowed_protocols | |
794 | && try_set_protocol(vconn, f, &cur_protocol)) { | |
795 | return f; | |
f9cbfbe4 | 796 | } |
27527aa0 | 797 | } |
f9cbfbe4 | 798 | |
27527aa0 BP |
799 | usable_s = ofputil_protocols_to_string(usable_protocols); |
800 | if (usable_protocols & allowed_protocols) { | |
801 | ovs_fatal(0, "switch does not support any of the usable flow " | |
802 | "formats (%s)", usable_s); | |
f9cbfbe4 | 803 | } else { |
27527aa0 BP |
804 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); |
805 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
806 | "allowed flow formats (%s)", usable_s, allowed_s); | |
88ca35ee | 807 | } |
064af421 BP |
808 | } |
809 | ||
bdcc5925 BP |
810 | static struct vconn * |
811 | prepare_dump_flows(int argc, char *argv[], bool aggregate, | |
812 | struct ofpbuf **requestp) | |
064af421 | 813 | { |
27527aa0 | 814 | enum ofputil_protocol usable_protocols, protocol; |
81d1ea94 | 815 | struct ofputil_flow_stats_request fsr; |
88ca35ee | 816 | struct vconn *vconn; |
064af421 | 817 | |
88ca35ee | 818 | parse_ofp_flow_stats_request_str(&fsr, aggregate, argc > 2 ? argv[2] : ""); |
27527aa0 | 819 | usable_protocols = ofputil_flow_stats_request_usable_protocols(&fsr); |
064af421 | 820 | |
27527aa0 BP |
821 | protocol = open_vconn(argv[1], &vconn); |
822 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
bdcc5925 BP |
823 | *requestp = ofputil_encode_flow_stats_request(&fsr, protocol); |
824 | return vconn; | |
825 | } | |
826 | ||
827 | static void | |
828 | ofctl_dump_flows__(int argc, char *argv[], bool aggregate) | |
829 | { | |
830 | struct ofpbuf *request; | |
831 | struct vconn *vconn; | |
832 | ||
833 | vconn = prepare_dump_flows(argc, argv, aggregate, &request); | |
53514387 | 834 | dump_stats_transaction(vconn, request); |
88ca35ee BP |
835 | vconn_close(vconn); |
836 | } | |
837 | ||
bdcc5925 BP |
838 | static int |
839 | compare_flows(const void *afs_, const void *bfs_) | |
840 | { | |
841 | const struct ofputil_flow_stats *afs = afs_; | |
842 | const struct ofputil_flow_stats *bfs = bfs_; | |
81a76618 BP |
843 | const struct match *a = &afs->match; |
844 | const struct match *b = &bfs->match; | |
bdcc5925 BP |
845 | const struct sort_criterion *sc; |
846 | ||
847 | for (sc = criteria; sc < &criteria[n_criteria]; sc++) { | |
848 | const struct mf_field *f = sc->field; | |
849 | int ret; | |
850 | ||
851 | if (!f) { | |
81a76618 BP |
852 | unsigned int a_pri = afs->priority; |
853 | unsigned int b_pri = bfs->priority; | |
854 | ret = a_pri < b_pri ? -1 : a_pri > b_pri; | |
bdcc5925 BP |
855 | } else { |
856 | bool ina, inb; | |
857 | ||
858 | ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc); | |
859 | inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc); | |
860 | if (ina != inb) { | |
861 | /* Skip the test for sc->order, so that missing fields always | |
862 | * sort to the end whether we're sorting in ascending or | |
863 | * descending order. */ | |
864 | return ina ? -1 : 1; | |
865 | } else { | |
866 | union mf_value aval, bval; | |
867 | ||
868 | mf_get_value(f, &a->flow, &aval); | |
869 | mf_get_value(f, &b->flow, &bval); | |
870 | ret = memcmp(&aval, &bval, f->n_bytes); | |
871 | } | |
872 | } | |
873 | ||
874 | if (ret) { | |
875 | return sc->order == SORT_ASC ? ret : -ret; | |
876 | } | |
877 | } | |
878 | ||
879 | return 0; | |
880 | } | |
881 | ||
88ca35ee | 882 | static void |
e1fef0f9 | 883 | ofctl_dump_flows(int argc, char *argv[]) |
88ca35ee | 884 | { |
bdcc5925 BP |
885 | if (!n_criteria) { |
886 | return ofctl_dump_flows__(argc, argv, false); | |
887 | } else { | |
888 | struct ofputil_flow_stats *fses; | |
889 | size_t n_fses, allocated_fses; | |
890 | struct ofpbuf *request; | |
891 | struct ofpbuf ofpacts; | |
892 | struct ofpbuf *reply; | |
893 | struct vconn *vconn; | |
894 | ovs_be32 send_xid; | |
895 | struct ds s; | |
896 | size_t i; | |
897 | ||
898 | vconn = prepare_dump_flows(argc, argv, false, &request); | |
899 | send_xid = ((struct ofp_header *) request->data)->xid; | |
900 | send_openflow_buffer(vconn, request); | |
901 | ||
902 | fses = NULL; | |
903 | n_fses = allocated_fses = 0; | |
904 | reply = NULL; | |
905 | ofpbuf_init(&ofpacts, 0); | |
906 | for (;;) { | |
907 | struct ofputil_flow_stats *fs; | |
908 | ||
909 | if (n_fses >= allocated_fses) { | |
910 | fses = x2nrealloc(fses, &allocated_fses, sizeof *fses); | |
911 | } | |
912 | ||
913 | fs = &fses[n_fses]; | |
914 | if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs, | |
915 | &ofpacts)) { | |
916 | break; | |
917 | } | |
918 | fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len); | |
919 | n_fses++; | |
920 | } | |
921 | ofpbuf_uninit(&ofpacts); | |
922 | ||
923 | qsort(fses, n_fses, sizeof *fses, compare_flows); | |
924 | ||
925 | ds_init(&s); | |
926 | for (i = 0; i < n_fses; i++) { | |
927 | ds_clear(&s); | |
928 | ofp_print_flow_stats(&s, &fses[i]); | |
929 | puts(ds_cstr(&s)); | |
930 | } | |
931 | ds_destroy(&s); | |
932 | ||
933 | for (i = 0; i < n_fses; i++) { | |
934 | free(fses[i].ofpacts); | |
935 | } | |
936 | free(fses); | |
937 | ||
938 | vconn_close(vconn); | |
939 | } | |
88ca35ee BP |
940 | } |
941 | ||
942 | static void | |
e1fef0f9 | 943 | ofctl_dump_aggregate(int argc, char *argv[]) |
88ca35ee | 944 | { |
e1fef0f9 | 945 | return ofctl_dump_flows__(argc, argv, true); |
064af421 BP |
946 | } |
947 | ||
d2805da2 | 948 | static void |
e1fef0f9 | 949 | ofctl_queue_stats(int argc, char *argv[]) |
d2805da2 | 950 | { |
e2b9ac44 | 951 | struct ofp10_queue_stats_request *req; |
d2805da2 | 952 | struct ofpbuf *request; |
53514387 | 953 | struct vconn *vconn; |
d2805da2 | 954 | |
53514387 SH |
955 | open_vconn(argv[1], &vconn); |
956 | request = ofpraw_alloc(OFPRAW_OFPST_QUEUE_REQUEST, | |
957 | vconn_get_version(vconn), 0); | |
982697a4 | 958 | req = ofpbuf_put_zeros(request, sizeof *req); |
d2805da2 BP |
959 | |
960 | if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) { | |
961 | req->port_no = htons(str_to_port_no(argv[1], argv[2])); | |
962 | } else { | |
963 | req->port_no = htons(OFPP_ALL); | |
964 | } | |
965 | if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) { | |
966 | req->queue_id = htonl(atoi(argv[3])); | |
967 | } else { | |
968 | req->queue_id = htonl(OFPQ_ALL); | |
969 | } | |
970 | ||
971 | memset(req->pad, 0, sizeof req->pad); | |
972 | ||
53514387 SH |
973 | dump_stats_transaction(vconn, request); |
974 | vconn_close(vconn); | |
d2805da2 BP |
975 | } |
976 | ||
27527aa0 BP |
977 | static enum ofputil_protocol |
978 | open_vconn_for_flow_mod(const char *remote, | |
979 | const struct ofputil_flow_mod *fms, size_t n_fms, | |
980 | struct vconn **vconnp) | |
0fbc9f11 | 981 | { |
27527aa0 BP |
982 | enum ofputil_protocol usable_protocols; |
983 | enum ofputil_protocol cur_protocol; | |
984 | char *usable_s; | |
985 | int i; | |
0fbc9f11 | 986 | |
27527aa0 BP |
987 | /* Figure out what flow formats will work. */ |
988 | usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms); | |
989 | if (!(usable_protocols & allowed_protocols)) { | |
990 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); | |
991 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
992 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
993 | "allowed flow formats (%s)", usable_s, allowed_s); | |
0fbc9f11 | 994 | } |
0fbc9f11 | 995 | |
27527aa0 BP |
996 | /* If the initial flow format is allowed and usable, keep it. */ |
997 | cur_protocol = open_vconn(remote, vconnp); | |
998 | if (usable_protocols & allowed_protocols & cur_protocol) { | |
999 | return cur_protocol; | |
1000 | } | |
1001 | ||
1002 | /* Otherwise try each flow format in turn. */ | |
1003 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
1004 | enum ofputil_protocol f = 1 << i; | |
1005 | ||
1006 | if (f != cur_protocol | |
1007 | && f & usable_protocols & allowed_protocols | |
1008 | && try_set_protocol(*vconnp, f, &cur_protocol)) { | |
1009 | return f; | |
1010 | } | |
0fbc9f11 | 1011 | } |
27527aa0 BP |
1012 | |
1013 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
1014 | ovs_fatal(0, "switch does not support any of the usable flow " | |
1015 | "formats (%s)", usable_s); | |
0fbc9f11 BP |
1016 | } |
1017 | ||
064af421 | 1018 | static void |
e1fef0f9 AS |
1019 | ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, |
1020 | size_t n_fms) | |
064af421 | 1021 | { |
27527aa0 | 1022 | enum ofputil_protocol protocol; |
064af421 | 1023 | struct vconn *vconn; |
27527aa0 | 1024 | size_t i; |
4989c59f | 1025 | |
27527aa0 | 1026 | protocol = open_vconn_for_flow_mod(remote, fms, n_fms, &vconn); |
049c8dc2 | 1027 | |
27527aa0 BP |
1028 | for (i = 0; i < n_fms; i++) { |
1029 | struct ofputil_flow_mod *fm = &fms[i]; | |
0fbc9f11 | 1030 | |
27527aa0 | 1031 | transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol)); |
f25d0cf3 | 1032 | free(fm->ofpacts); |
4989c59f | 1033 | } |
064af421 | 1034 | vconn_close(vconn); |
88ca35ee BP |
1035 | } |
1036 | ||
064af421 | 1037 | static void |
e1fef0f9 | 1038 | ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command) |
064af421 | 1039 | { |
27527aa0 BP |
1040 | struct ofputil_flow_mod *fms = NULL; |
1041 | size_t n_fms = 0; | |
064af421 | 1042 | |
27527aa0 | 1043 | parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms); |
e1fef0f9 | 1044 | ofctl_flow_mod__(argv[1], fms, n_fms); |
27527aa0 BP |
1045 | free(fms); |
1046 | } | |
1047 | ||
1048 | static void | |
e1fef0f9 | 1049 | ofctl_flow_mod(int argc, char *argv[], uint16_t command) |
27527aa0 | 1050 | { |
4989c59f | 1051 | if (argc > 2 && !strcmp(argv[2], "-")) { |
e1fef0f9 | 1052 | ofctl_flow_mod_file(argc, argv, command); |
27527aa0 BP |
1053 | } else { |
1054 | struct ofputil_flow_mod fm; | |
1055 | parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, false); | |
e1fef0f9 | 1056 | ofctl_flow_mod__(argv[1], &fm, 1); |
064af421 | 1057 | } |
4989c59f | 1058 | } |
88ca35ee | 1059 | |
4989c59f | 1060 | static void |
e1fef0f9 | 1061 | ofctl_add_flow(int argc, char *argv[]) |
4989c59f | 1062 | { |
e1fef0f9 | 1063 | ofctl_flow_mod(argc, argv, OFPFC_ADD); |
4989c59f BP |
1064 | } |
1065 | ||
1066 | static void | |
e1fef0f9 | 1067 | ofctl_add_flows(int argc, char *argv[]) |
4989c59f | 1068 | { |
e1fef0f9 | 1069 | ofctl_flow_mod_file(argc, argv, OFPFC_ADD); |
064af421 BP |
1070 | } |
1071 | ||
1072 | static void | |
e1fef0f9 | 1073 | ofctl_mod_flows(int argc, char *argv[]) |
064af421 | 1074 | { |
e1fef0f9 | 1075 | ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY); |
064af421 BP |
1076 | } |
1077 | ||
88ca35ee | 1078 | static void |
e1fef0f9 | 1079 | ofctl_del_flows(int argc, char *argv[]) |
064af421 | 1080 | { |
e1fef0f9 | 1081 | ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE); |
064af421 BP |
1082 | } |
1083 | ||
54834960 EJ |
1084 | static void |
1085 | set_packet_in_format(struct vconn *vconn, | |
1086 | enum nx_packet_in_format packet_in_format) | |
1087 | { | |
3f4a1939 SH |
1088 | struct ofpbuf *spif; |
1089 | ||
1090 | spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn), | |
1091 | packet_in_format); | |
54834960 EJ |
1092 | transact_noreply(vconn, spif); |
1093 | VLOG_DBG("%s: using user-specified packet in format %s", | |
1094 | vconn_get_name(vconn), | |
1095 | ofputil_packet_in_format_to_string(packet_in_format)); | |
1096 | } | |
1097 | ||
f0fd1a17 PS |
1098 | static int |
1099 | monitor_set_invalid_ttl_to_controller(struct vconn *vconn) | |
1100 | { | |
1101 | struct ofp_switch_config config; | |
1102 | enum ofp_config_flags flags; | |
1103 | ||
1104 | fetch_switch_config(vconn, &config); | |
1105 | flags = ntohs(config.flags); | |
1106 | if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) { | |
1107 | /* Set the invalid ttl config. */ | |
1108 | flags |= OFPC_INVALID_TTL_TO_CONTROLLER; | |
1109 | ||
1110 | config.flags = htons(flags); | |
1111 | set_switch_config(vconn, &config); | |
1112 | ||
1113 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
1114 | * doesn't define error reporting for bad modes, so this is all we can | |
1115 | * do. */ | |
1116 | fetch_switch_config(vconn, &config); | |
1117 | flags = ntohs(config.flags); | |
1118 | if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) { | |
1119 | ovs_fatal(0, "setting invalid_ttl_to_controller failed (this " | |
1120 | "switch probably doesn't support mode)"); | |
1121 | return -EOPNOTSUPP; | |
1122 | } | |
1123 | } | |
1124 | return 0; | |
1125 | } | |
1126 | ||
96761f58 BP |
1127 | /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The |
1128 | * caller must free '*msgp'. On success, returns NULL. On failure, returns | |
1129 | * an error message and stores NULL in '*msgp'. */ | |
1130 | static const char * | |
1131 | openflow_from_hex(const char *hex, struct ofpbuf **msgp) | |
1132 | { | |
1133 | struct ofp_header *oh; | |
1134 | struct ofpbuf *msg; | |
1135 | ||
1136 | msg = ofpbuf_new(strlen(hex) / 2); | |
1137 | *msgp = NULL; | |
1138 | ||
1139 | if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') { | |
1140 | ofpbuf_delete(msg); | |
1141 | return "Trailing garbage in hex data"; | |
1142 | } | |
1143 | ||
1144 | if (msg->size < sizeof(struct ofp_header)) { | |
1145 | ofpbuf_delete(msg); | |
1146 | return "Message too short for OpenFlow"; | |
1147 | } | |
1148 | ||
1149 | oh = msg->data; | |
1150 | if (msg->size != ntohs(oh->length)) { | |
1151 | ofpbuf_delete(msg); | |
1152 | return "Message size does not match length in OpenFlow header"; | |
1153 | } | |
1154 | ||
1155 | *msgp = msg; | |
1156 | return NULL; | |
1157 | } | |
1158 | ||
1159 | static void | |
1160 | ofctl_send(struct unixctl_conn *conn, int argc, | |
1161 | const char *argv[], void *vconn_) | |
1162 | { | |
1163 | struct vconn *vconn = vconn_; | |
1164 | struct ds reply; | |
1165 | bool ok; | |
1166 | int i; | |
1167 | ||
1168 | ok = true; | |
1169 | ds_init(&reply); | |
1170 | for (i = 1; i < argc; i++) { | |
1171 | const char *error_msg; | |
1172 | struct ofpbuf *msg; | |
1173 | int error; | |
1174 | ||
1175 | error_msg = openflow_from_hex(argv[i], &msg); | |
1176 | if (error_msg) { | |
1177 | ds_put_format(&reply, "%s\n", error_msg); | |
1178 | ok = false; | |
1179 | continue; | |
1180 | } | |
1181 | ||
1182 | fprintf(stderr, "send: "); | |
1183 | ofp_print(stderr, msg->data, msg->size, verbosity); | |
1184 | ||
1185 | error = vconn_send_block(vconn, msg); | |
1186 | if (error) { | |
1187 | ofpbuf_delete(msg); | |
1188 | ds_put_format(&reply, "%s\n", strerror(error)); | |
1189 | ok = false; | |
1190 | } else { | |
1191 | ds_put_cstr(&reply, "sent\n"); | |
1192 | } | |
1193 | } | |
bde9f75d EJ |
1194 | |
1195 | if (ok) { | |
1196 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1197 | } else { | |
1198 | unixctl_command_reply_error(conn, ds_cstr(&reply)); | |
1199 | } | |
96761f58 BP |
1200 | ds_destroy(&reply); |
1201 | } | |
1202 | ||
bb638b9a BP |
1203 | struct barrier_aux { |
1204 | struct vconn *vconn; /* OpenFlow connection for sending barrier. */ | |
1205 | struct unixctl_conn *conn; /* Connection waiting for barrier response. */ | |
1206 | }; | |
1207 | ||
1208 | static void | |
1209 | ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1210 | const char *argv[] OVS_UNUSED, void *aux_) | |
1211 | { | |
1212 | struct barrier_aux *aux = aux_; | |
1213 | struct ofpbuf *msg; | |
1214 | int error; | |
1215 | ||
1216 | if (aux->conn) { | |
bde9f75d | 1217 | unixctl_command_reply_error(conn, "already waiting for barrier reply"); |
bb638b9a BP |
1218 | return; |
1219 | } | |
1220 | ||
a0ae0b6e | 1221 | msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn)); |
bb638b9a BP |
1222 | error = vconn_send_block(aux->vconn, msg); |
1223 | if (error) { | |
1224 | ofpbuf_delete(msg); | |
bde9f75d | 1225 | unixctl_command_reply_error(conn, strerror(error)); |
bb638b9a BP |
1226 | } else { |
1227 | aux->conn = conn; | |
1228 | } | |
1229 | } | |
1230 | ||
1e1d00a5 BP |
1231 | static void |
1232 | ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1233 | const char *argv[], void *aux OVS_UNUSED) | |
1234 | { | |
1235 | int fd; | |
1236 | ||
1237 | fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); | |
1238 | if (fd < 0) { | |
bde9f75d | 1239 | unixctl_command_reply_error(conn, strerror(errno)); |
1e1d00a5 BP |
1240 | return; |
1241 | } | |
1242 | ||
1243 | fflush(stderr); | |
1244 | dup2(fd, STDERR_FILENO); | |
1245 | close(fd); | |
bde9f75d | 1246 | unixctl_command_reply(conn, NULL); |
1e1d00a5 BP |
1247 | } |
1248 | ||
2b07c8b1 BP |
1249 | static void |
1250 | ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
4f8f2439 | 1251 | const char *argv[] OVS_UNUSED, void *blocked_) |
2b07c8b1 | 1252 | { |
4f8f2439 | 1253 | bool *blocked = blocked_; |
2b07c8b1 | 1254 | |
4f8f2439 BP |
1255 | if (!*blocked) { |
1256 | *blocked = true; | |
1257 | unixctl_command_reply(conn, NULL); | |
1258 | } else { | |
2b07c8b1 | 1259 | unixctl_command_reply(conn, "already blocking"); |
2b07c8b1 BP |
1260 | } |
1261 | } | |
1262 | ||
1263 | static void | |
1264 | ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
4f8f2439 | 1265 | const char *argv[] OVS_UNUSED, void *blocked_) |
2b07c8b1 | 1266 | { |
4f8f2439 | 1267 | bool *blocked = blocked_; |
2b07c8b1 | 1268 | |
4f8f2439 BP |
1269 | if (*blocked) { |
1270 | *blocked = false; | |
2b07c8b1 | 1271 | unixctl_command_reply(conn, NULL); |
4f8f2439 BP |
1272 | } else { |
1273 | unixctl_command_reply(conn, "already unblocked"); | |
2b07c8b1 BP |
1274 | } |
1275 | } | |
1276 | ||
064af421 | 1277 | static void |
0caf6bde BP |
1278 | monitor_vconn(struct vconn *vconn) |
1279 | { | |
bb638b9a | 1280 | struct barrier_aux barrier_aux = { vconn, NULL }; |
1eb85ef5 EJ |
1281 | struct unixctl_server *server; |
1282 | bool exiting = false; | |
4f8f2439 | 1283 | bool blocked = false; |
7d0c5973 | 1284 | int error; |
1eb85ef5 | 1285 | |
7d0c5973 | 1286 | daemon_save_fd(STDERR_FILENO); |
1eb85ef5 EJ |
1287 | daemonize_start(); |
1288 | error = unixctl_server_create(NULL, &server); | |
1289 | if (error) { | |
1290 | ovs_fatal(error, "failed to create unixctl server"); | |
1291 | } | |
1292 | unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting); | |
96761f58 BP |
1293 | unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX, |
1294 | ofctl_send, vconn); | |
bb638b9a BP |
1295 | unixctl_command_register("ofctl/barrier", "", 0, 0, |
1296 | ofctl_barrier, &barrier_aux); | |
1e1d00a5 BP |
1297 | unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1, |
1298 | ofctl_set_output_file, NULL); | |
2b07c8b1 | 1299 | |
4f8f2439 BP |
1300 | unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked); |
1301 | unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock, | |
1302 | &blocked); | |
2b07c8b1 | 1303 | |
1eb85ef5 EJ |
1304 | daemonize_complete(); |
1305 | ||
0caf6bde BP |
1306 | for (;;) { |
1307 | struct ofpbuf *b; | |
1eb85ef5 EJ |
1308 | int retval; |
1309 | ||
1310 | unixctl_server_run(server); | |
982697a4 | 1311 | |
4f8f2439 | 1312 | while (!blocked) { |
982697a4 | 1313 | enum ofptype type; |
bb638b9a | 1314 | |
1eb85ef5 EJ |
1315 | retval = vconn_recv(vconn, &b); |
1316 | if (retval == EAGAIN) { | |
1317 | break; | |
1318 | } | |
1eb85ef5 | 1319 | run(retval, "vconn_recv"); |
31c6fcd7 | 1320 | |
0c9560b7 BP |
1321 | if (timestamp) { |
1322 | time_t now = time_wall(); | |
1323 | char s[32]; | |
1324 | ||
3123c8fd | 1325 | strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S: ", gmtime(&now)); |
0c9560b7 BP |
1326 | fputs(s, stderr); |
1327 | } | |
31c6fcd7 | 1328 | |
982697a4 | 1329 | ofptype_decode(&type, b->data); |
1eb85ef5 EJ |
1330 | ofp_print(stderr, b->data, b->size, verbosity + 2); |
1331 | ofpbuf_delete(b); | |
bb638b9a | 1332 | |
982697a4 | 1333 | if (barrier_aux.conn && type == OFPTYPE_BARRIER_REPLY) { |
bde9f75d | 1334 | unixctl_command_reply(barrier_aux.conn, NULL); |
bb638b9a BP |
1335 | barrier_aux.conn = NULL; |
1336 | } | |
1eb85ef5 EJ |
1337 | } |
1338 | ||
1339 | if (exiting) { | |
1340 | break; | |
1341 | } | |
1342 | ||
1343 | vconn_run(vconn); | |
1344 | vconn_run_wait(vconn); | |
4f8f2439 BP |
1345 | if (!blocked) { |
1346 | vconn_recv_wait(vconn); | |
1347 | } | |
1eb85ef5 EJ |
1348 | unixctl_server_wait(server); |
1349 | poll_block(); | |
0caf6bde | 1350 | } |
828c72d0 BP |
1351 | vconn_close(vconn); |
1352 | unixctl_server_destroy(server); | |
0caf6bde BP |
1353 | } |
1354 | ||
1355 | static void | |
e1fef0f9 | 1356 | ofctl_monitor(int argc, char *argv[]) |
064af421 BP |
1357 | { |
1358 | struct vconn *vconn; | |
2b07c8b1 | 1359 | int i; |
064af421 BP |
1360 | |
1361 | open_vconn(argv[1], &vconn); | |
2b07c8b1 BP |
1362 | for (i = 2; i < argc; i++) { |
1363 | const char *arg = argv[i]; | |
064af421 | 1364 | |
2b07c8b1 BP |
1365 | if (isdigit((unsigned char) *arg)) { |
1366 | struct ofp_switch_config config; | |
1367 | ||
1368 | fetch_switch_config(vconn, &config); | |
1369 | config.miss_send_len = htons(atoi(arg)); | |
1370 | set_switch_config(vconn, &config); | |
1371 | } else if (!strcmp(arg, "invalid_ttl")) { | |
f0fd1a17 | 1372 | monitor_set_invalid_ttl_to_controller(vconn); |
2b07c8b1 BP |
1373 | } else if (!strncmp(arg, "watch:", 6)) { |
1374 | struct ofputil_flow_monitor_request fmr; | |
1375 | struct ofpbuf *msg; | |
1376 | ||
1377 | parse_flow_monitor_request(&fmr, arg + 6); | |
1378 | ||
1379 | msg = ofpbuf_new(0); | |
1380 | ofputil_append_flow_monitor_request(&fmr, msg); | |
53514387 | 1381 | dump_stats_transaction(vconn, msg); |
2b07c8b1 BP |
1382 | } else { |
1383 | ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg); | |
f0fd1a17 PS |
1384 | } |
1385 | } | |
2b07c8b1 | 1386 | |
ca8526e0 BP |
1387 | if (preferred_packet_in_format >= 0) { |
1388 | set_packet_in_format(vconn, preferred_packet_in_format); | |
1389 | } else { | |
1390 | struct ofpbuf *spif, *reply; | |
1391 | ||
3f4a1939 SH |
1392 | spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn), |
1393 | NXPIF_NXM); | |
ca8526e0 BP |
1394 | run(vconn_transact_noreply(vconn, spif, &reply), |
1395 | "talking to %s", vconn_get_name(vconn)); | |
1396 | if (reply) { | |
1397 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
1398 | VLOG_DBG("%s: failed to set packet in format to nxm, controller" | |
1399 | " replied: %s. Falling back to the switch default.", | |
1400 | vconn_get_name(vconn), s); | |
1401 | free(s); | |
1402 | ofpbuf_delete(reply); | |
1403 | } | |
1404 | } | |
1405 | ||
0caf6bde BP |
1406 | monitor_vconn(vconn); |
1407 | } | |
1408 | ||
1409 | static void | |
e1fef0f9 | 1410 | ofctl_snoop(int argc OVS_UNUSED, char *argv[]) |
0caf6bde BP |
1411 | { |
1412 | struct vconn *vconn; | |
1413 | ||
1414 | open_vconn__(argv[1], "snoop", &vconn); | |
1415 | monitor_vconn(vconn); | |
064af421 BP |
1416 | } |
1417 | ||
1418 | static void | |
e1fef0f9 | 1419 | ofctl_dump_ports(int argc, char *argv[]) |
064af421 | 1420 | { |
abaad8cf | 1421 | struct ofpbuf *request; |
53514387 | 1422 | struct vconn *vconn; |
abaad8cf JP |
1423 | uint16_t port; |
1424 | ||
53514387 | 1425 | open_vconn(argv[1], &vconn); |
abaad8cf | 1426 | port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_NONE; |
f8e4867e | 1427 | request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port); |
53514387 SH |
1428 | dump_stats_transaction(vconn, request); |
1429 | vconn_close(vconn); | |
064af421 BP |
1430 | } |
1431 | ||
2be393ed | 1432 | static void |
e1fef0f9 | 1433 | ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[]) |
2be393ed | 1434 | { |
982697a4 | 1435 | dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_PORT_DESC_REQUEST); |
2be393ed JP |
1436 | } |
1437 | ||
064af421 | 1438 | static void |
e1fef0f9 | 1439 | ofctl_probe(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
1440 | { |
1441 | struct ofpbuf *request; | |
1442 | struct vconn *vconn; | |
1443 | struct ofpbuf *reply; | |
1444 | ||
064af421 | 1445 | open_vconn(argv[1], &vconn); |
1a126c0c | 1446 | request = make_echo_request(vconn_get_version(vconn)); |
064af421 BP |
1447 | run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]); |
1448 | if (reply->size != sizeof(struct ofp_header)) { | |
1449 | ovs_fatal(0, "reply does not match request"); | |
1450 | } | |
1451 | ofpbuf_delete(reply); | |
1452 | vconn_close(vconn); | |
1453 | } | |
1454 | ||
0c3d5fc8 | 1455 | static void |
e1fef0f9 | 1456 | ofctl_packet_out(int argc, char *argv[]) |
0c3d5fc8 | 1457 | { |
de0f3156 | 1458 | enum ofputil_protocol protocol; |
0c3d5fc8 | 1459 | struct ofputil_packet_out po; |
f25d0cf3 | 1460 | struct ofpbuf ofpacts; |
0c3d5fc8 BP |
1461 | struct vconn *vconn; |
1462 | int i; | |
1463 | ||
f25d0cf3 BP |
1464 | ofpbuf_init(&ofpacts, 64); |
1465 | parse_ofpacts(argv[3], &ofpacts); | |
0c3d5fc8 BP |
1466 | |
1467 | po.buffer_id = UINT32_MAX; | |
c6100d92 | 1468 | po.in_port = str_to_port_no(argv[1], argv[2]); |
f25d0cf3 BP |
1469 | po.ofpacts = ofpacts.data; |
1470 | po.ofpacts_len = ofpacts.size; | |
0c3d5fc8 | 1471 | |
de0f3156 | 1472 | protocol = open_vconn(argv[1], &vconn); |
0c3d5fc8 BP |
1473 | for (i = 4; i < argc; i++) { |
1474 | struct ofpbuf *packet, *opo; | |
1475 | const char *error_msg; | |
1476 | ||
1477 | error_msg = eth_from_hex(argv[i], &packet); | |
1478 | if (error_msg) { | |
1479 | ovs_fatal(0, "%s", error_msg); | |
1480 | } | |
1481 | ||
1482 | po.packet = packet->data; | |
1483 | po.packet_len = packet->size; | |
de0f3156 | 1484 | opo = ofputil_encode_packet_out(&po, protocol); |
0c3d5fc8 BP |
1485 | transact_noreply(vconn, opo); |
1486 | ofpbuf_delete(packet); | |
1487 | } | |
1488 | vconn_close(vconn); | |
f25d0cf3 | 1489 | ofpbuf_uninit(&ofpacts); |
0c3d5fc8 BP |
1490 | } |
1491 | ||
064af421 | 1492 | static void |
e1fef0f9 | 1493 | ofctl_mod_port(int argc OVS_UNUSED, char *argv[]) |
064af421 | 1494 | { |
28124950 BP |
1495 | struct ofp_config_flag { |
1496 | const char *name; /* The flag's name. */ | |
1497 | enum ofputil_port_config bit; /* Bit to turn on or off. */ | |
1498 | bool on; /* Value to set the bit to. */ | |
1499 | }; | |
1500 | static const struct ofp_config_flag flags[] = { | |
1501 | { "up", OFPUTIL_PC_PORT_DOWN, false }, | |
1502 | { "down", OFPUTIL_PC_PORT_DOWN, true }, | |
1503 | { "stp", OFPUTIL_PC_NO_STP, false }, | |
1504 | { "receive", OFPUTIL_PC_NO_RECV, false }, | |
1505 | { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false }, | |
1506 | { "flood", OFPUTIL_PC_NO_FLOOD, false }, | |
1507 | { "forward", OFPUTIL_PC_NO_FWD, false }, | |
1508 | { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false }, | |
1509 | }; | |
1510 | ||
1511 | const struct ofp_config_flag *flag; | |
9e1fd49b BP |
1512 | enum ofputil_protocol protocol; |
1513 | struct ofputil_port_mod pm; | |
1514 | struct ofputil_phy_port pp; | |
064af421 | 1515 | struct vconn *vconn; |
28124950 BP |
1516 | const char *command; |
1517 | bool not; | |
064af421 | 1518 | |
9e1fd49b | 1519 | fetch_ofputil_phy_port(argv[1], argv[2], &pp); |
064af421 | 1520 | |
9e1fd49b BP |
1521 | pm.port_no = pp.port_no; |
1522 | memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN); | |
1523 | pm.config = 0; | |
1524 | pm.mask = 0; | |
1525 | pm.advertise = 0; | |
064af421 | 1526 | |
28124950 BP |
1527 | if (!strncasecmp(argv[3], "no-", 3)) { |
1528 | command = argv[3] + 3; | |
1529 | not = true; | |
1530 | } else if (!strncasecmp(argv[3], "no", 2)) { | |
1531 | command = argv[3] + 2; | |
1532 | not = true; | |
064af421 | 1533 | } else { |
28124950 BP |
1534 | command = argv[3]; |
1535 | not = false; | |
1536 | } | |
1537 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { | |
1538 | if (!strcasecmp(command, flag->name)) { | |
1539 | pm.mask = flag->bit; | |
1540 | pm.config = flag->on ^ not ? flag->bit : 0; | |
1541 | goto found; | |
1542 | } | |
064af421 | 1543 | } |
28124950 | 1544 | ovs_fatal(0, "unknown mod-port command '%s'", argv[3]); |
064af421 | 1545 | |
28124950 | 1546 | found: |
9e1fd49b BP |
1547 | protocol = open_vconn(argv[1], &vconn); |
1548 | transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol)); | |
064af421 BP |
1549 | vconn_close(vconn); |
1550 | } | |
1551 | ||
7257b535 | 1552 | static void |
e1fef0f9 | 1553 | ofctl_get_frags(int argc OVS_UNUSED, char *argv[]) |
7257b535 BP |
1554 | { |
1555 | struct ofp_switch_config config; | |
1556 | struct vconn *vconn; | |
1557 | ||
1558 | open_vconn(argv[1], &vconn); | |
1559 | fetch_switch_config(vconn, &config); | |
1560 | puts(ofputil_frag_handling_to_string(ntohs(config.flags))); | |
1561 | vconn_close(vconn); | |
1562 | } | |
1563 | ||
1564 | static void | |
e1fef0f9 | 1565 | ofctl_set_frags(int argc OVS_UNUSED, char *argv[]) |
7257b535 BP |
1566 | { |
1567 | struct ofp_switch_config config; | |
1568 | enum ofp_config_flags mode; | |
1569 | struct vconn *vconn; | |
1570 | ovs_be16 flags; | |
1571 | ||
1572 | if (!ofputil_frag_handling_from_string(argv[2], &mode)) { | |
1573 | ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]); | |
1574 | } | |
1575 | ||
1576 | open_vconn(argv[1], &vconn); | |
1577 | fetch_switch_config(vconn, &config); | |
1578 | flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK)); | |
1579 | if (flags != config.flags) { | |
1580 | /* Set the configuration. */ | |
1581 | config.flags = flags; | |
1582 | set_switch_config(vconn, &config); | |
1583 | ||
1584 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
1585 | * doesn't define error reporting for bad modes, so this is all we can | |
1586 | * do. */ | |
1587 | fetch_switch_config(vconn, &config); | |
1588 | if (flags != config.flags) { | |
1589 | ovs_fatal(0, "%s: setting fragment handling mode failed (this " | |
1590 | "switch probably doesn't support mode \"%s\")", | |
1591 | argv[1], ofputil_frag_handling_to_string(mode)); | |
1592 | } | |
1593 | } | |
1594 | vconn_close(vconn); | |
1595 | } | |
1596 | ||
064af421 | 1597 | static void |
e1fef0f9 | 1598 | ofctl_ping(int argc, char *argv[]) |
064af421 BP |
1599 | { |
1600 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
1601 | unsigned int payload; | |
1602 | struct vconn *vconn; | |
1603 | int i; | |
1604 | ||
1605 | payload = argc > 2 ? atoi(argv[2]) : 64; | |
1606 | if (payload > max_payload) { | |
1607 | ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); | |
1608 | } | |
1609 | ||
1610 | open_vconn(argv[1], &vconn); | |
1611 | for (i = 0; i < 10; i++) { | |
1612 | struct timeval start, end; | |
1613 | struct ofpbuf *request, *reply; | |
982697a4 BP |
1614 | const struct ofp_header *rpy_hdr; |
1615 | enum ofptype type; | |
064af421 | 1616 | |
982697a4 BP |
1617 | request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION, |
1618 | payload); | |
1619 | random_bytes(ofpbuf_put_uninit(request, payload), payload); | |
064af421 | 1620 | |
279c9e03 | 1621 | xgettimeofday(&start); |
064af421 | 1622 | run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact"); |
279c9e03 | 1623 | xgettimeofday(&end); |
064af421 BP |
1624 | |
1625 | rpy_hdr = reply->data; | |
982697a4 BP |
1626 | if (ofptype_pull(&type, reply) |
1627 | || type != OFPTYPE_ECHO_REPLY | |
1628 | || reply->size != payload | |
1629 | || memcmp(request->l3, reply->l3, payload)) { | |
064af421 | 1630 | printf("Reply does not match request. Request:\n"); |
4f564f8d | 1631 | ofp_print(stdout, request, request->size, verbosity + 2); |
064af421 | 1632 | printf("Reply:\n"); |
4f564f8d | 1633 | ofp_print(stdout, reply, reply->size, verbosity + 2); |
064af421 | 1634 | } |
2886875a | 1635 | printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", |
982697a4 | 1636 | reply->size, argv[1], ntohl(rpy_hdr->xid), |
064af421 BP |
1637 | (1000*(double)(end.tv_sec - start.tv_sec)) |
1638 | + (.001*(end.tv_usec - start.tv_usec))); | |
1639 | ofpbuf_delete(request); | |
1640 | ofpbuf_delete(reply); | |
1641 | } | |
1642 | vconn_close(vconn); | |
1643 | } | |
1644 | ||
1645 | static void | |
e1fef0f9 | 1646 | ofctl_benchmark(int argc OVS_UNUSED, char *argv[]) |
064af421 BP |
1647 | { |
1648 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
1649 | struct timeval start, end; | |
1650 | unsigned int payload_size, message_size; | |
1651 | struct vconn *vconn; | |
1652 | double duration; | |
1653 | int count; | |
1654 | int i; | |
1655 | ||
1656 | payload_size = atoi(argv[2]); | |
1657 | if (payload_size > max_payload) { | |
1658 | ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); | |
1659 | } | |
1660 | message_size = sizeof(struct ofp_header) + payload_size; | |
1661 | ||
1662 | count = atoi(argv[3]); | |
1663 | ||
1664 | printf("Sending %d packets * %u bytes (with header) = %u bytes total\n", | |
1665 | count, message_size, count * message_size); | |
1666 | ||
1667 | open_vconn(argv[1], &vconn); | |
279c9e03 | 1668 | xgettimeofday(&start); |
064af421 BP |
1669 | for (i = 0; i < count; i++) { |
1670 | struct ofpbuf *request, *reply; | |
064af421 | 1671 | |
982697a4 BP |
1672 | request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION, |
1673 | payload_size); | |
1674 | ofpbuf_put_zeros(request, payload_size); | |
064af421 BP |
1675 | run(vconn_transact(vconn, request, &reply), "transact"); |
1676 | ofpbuf_delete(reply); | |
1677 | } | |
279c9e03 | 1678 | xgettimeofday(&end); |
064af421 BP |
1679 | vconn_close(vconn); |
1680 | ||
1681 | duration = ((1000*(double)(end.tv_sec - start.tv_sec)) | |
1682 | + (.001*(end.tv_usec - start.tv_usec))); | |
1683 | printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n", | |
1684 | duration, count / (duration / 1000.0), | |
1685 | count * message_size / (duration / 1000.0)); | |
1686 | } | |
1687 | ||
09246b99 | 1688 | static void |
e1fef0f9 | 1689 | ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
09246b99 BP |
1690 | { |
1691 | usage(); | |
1692 | } | |
1693 | \f | |
0199c526 BP |
1694 | /* replace-flows and diff-flows commands. */ |
1695 | ||
1696 | /* A flow table entry, possibly with two different versions. */ | |
1697 | struct fte { | |
1698 | struct cls_rule rule; /* Within a "struct classifier". */ | |
1699 | struct fte_version *versions[2]; | |
1700 | }; | |
1701 | ||
1702 | /* One version of a Flow Table Entry. */ | |
1703 | struct fte_version { | |
1704 | ovs_be64 cookie; | |
1705 | uint16_t idle_timeout; | |
1706 | uint16_t hard_timeout; | |
1707 | uint16_t flags; | |
f25d0cf3 BP |
1708 | struct ofpact *ofpacts; |
1709 | size_t ofpacts_len; | |
0199c526 BP |
1710 | }; |
1711 | ||
1712 | /* Frees 'version' and the data that it owns. */ | |
1713 | static void | |
1714 | fte_version_free(struct fte_version *version) | |
1715 | { | |
1716 | if (version) { | |
f25d0cf3 | 1717 | free(version->ofpacts); |
0199c526 BP |
1718 | free(version); |
1719 | } | |
1720 | } | |
1721 | ||
1722 | /* Returns true if 'a' and 'b' are the same, false if they differ. | |
1723 | * | |
1724 | * Ignores differences in 'flags' because there's no way to retrieve flags from | |
1725 | * an OpenFlow switch. We have to assume that they are the same. */ | |
1726 | static bool | |
1727 | fte_version_equals(const struct fte_version *a, const struct fte_version *b) | |
1728 | { | |
1729 | return (a->cookie == b->cookie | |
1730 | && a->idle_timeout == b->idle_timeout | |
1731 | && a->hard_timeout == b->hard_timeout | |
f25d0cf3 BP |
1732 | && ofpacts_equal(a->ofpacts, a->ofpacts_len, |
1733 | b->ofpacts, b->ofpacts_len)); | |
0199c526 BP |
1734 | } |
1735 | ||
98f7f427 BP |
1736 | /* Clears 's', then if 's' has a version 'index', formats 'fte' and version |
1737 | * 'index' into 's', followed by a new-line. */ | |
0199c526 | 1738 | static void |
98f7f427 | 1739 | fte_version_format(const struct fte *fte, int index, struct ds *s) |
0199c526 | 1740 | { |
98f7f427 | 1741 | const struct fte_version *version = fte->versions[index]; |
0199c526 | 1742 | |
98f7f427 BP |
1743 | ds_clear(s); |
1744 | if (!version) { | |
1745 | return; | |
1746 | } | |
1747 | ||
1748 | cls_rule_format(&fte->rule, s); | |
0199c526 | 1749 | if (version->cookie != htonll(0)) { |
98f7f427 | 1750 | ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie)); |
0199c526 BP |
1751 | } |
1752 | if (version->idle_timeout != OFP_FLOW_PERMANENT) { | |
98f7f427 | 1753 | ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout); |
0199c526 BP |
1754 | } |
1755 | if (version->hard_timeout != OFP_FLOW_PERMANENT) { | |
98f7f427 | 1756 | ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout); |
0199c526 BP |
1757 | } |
1758 | ||
98f7f427 BP |
1759 | ds_put_char(s, ' '); |
1760 | ofpacts_format(version->ofpacts, version->ofpacts_len, s); | |
1761 | ||
1762 | ds_put_char(s, '\n'); | |
0199c526 BP |
1763 | } |
1764 | ||
1765 | static struct fte * | |
1766 | fte_from_cls_rule(const struct cls_rule *cls_rule) | |
1767 | { | |
1768 | return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL; | |
1769 | } | |
1770 | ||
1771 | /* Frees 'fte' and its versions. */ | |
1772 | static void | |
1773 | fte_free(struct fte *fte) | |
1774 | { | |
1775 | if (fte) { | |
1776 | fte_version_free(fte->versions[0]); | |
1777 | fte_version_free(fte->versions[1]); | |
48d28ac1 | 1778 | cls_rule_destroy(&fte->rule); |
0199c526 BP |
1779 | free(fte); |
1780 | } | |
1781 | } | |
1782 | ||
1783 | /* Frees all of the FTEs within 'cls'. */ | |
1784 | static void | |
1785 | fte_free_all(struct classifier *cls) | |
1786 | { | |
1787 | struct cls_cursor cursor; | |
1788 | struct fte *fte, *next; | |
1789 | ||
1790 | cls_cursor_init(&cursor, cls, NULL); | |
1791 | CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) { | |
1792 | classifier_remove(cls, &fte->rule); | |
1793 | fte_free(fte); | |
1794 | } | |
e49190c4 | 1795 | classifier_destroy(cls); |
0199c526 BP |
1796 | } |
1797 | ||
1798 | /* Searches 'cls' for an FTE matching 'rule', inserting a new one if | |
1799 | * necessary. Sets 'version' as the version of that rule with the given | |
1800 | * 'index', replacing any existing version, if any. | |
1801 | * | |
1802 | * Takes ownership of 'version'. */ | |
1803 | static void | |
81a76618 BP |
1804 | fte_insert(struct classifier *cls, const struct match *match, |
1805 | unsigned int priority, struct fte_version *version, int index) | |
0199c526 BP |
1806 | { |
1807 | struct fte *old, *fte; | |
1808 | ||
1809 | fte = xzalloc(sizeof *fte); | |
81a76618 | 1810 | cls_rule_init(&fte->rule, match, priority); |
0199c526 BP |
1811 | fte->versions[index] = version; |
1812 | ||
08944c1d | 1813 | old = fte_from_cls_rule(classifier_replace(cls, &fte->rule)); |
0199c526 BP |
1814 | if (old) { |
1815 | fte_version_free(old->versions[index]); | |
1816 | fte->versions[!index] = old->versions[!index]; | |
48d28ac1 | 1817 | cls_rule_destroy(&old->rule); |
0199c526 BP |
1818 | free(old); |
1819 | } | |
1820 | } | |
1821 | ||
1822 | /* Reads the flows in 'filename' as flow table entries in 'cls' for the version | |
27527aa0 BP |
1823 | * with the specified 'index'. Returns the flow formats able to represent the |
1824 | * flows that were read. */ | |
1825 | static enum ofputil_protocol | |
0199c526 BP |
1826 | read_flows_from_file(const char *filename, struct classifier *cls, int index) |
1827 | { | |
27527aa0 | 1828 | enum ofputil_protocol usable_protocols; |
0199c526 BP |
1829 | struct ds s; |
1830 | FILE *file; | |
1831 | ||
1832 | file = !strcmp(filename, "-") ? stdin : fopen(filename, "r"); | |
1833 | if (file == NULL) { | |
1834 | ovs_fatal(errno, "%s: open", filename); | |
1835 | } | |
1836 | ||
1837 | ds_init(&s); | |
27527aa0 | 1838 | usable_protocols = OFPUTIL_P_ANY; |
0199c526 BP |
1839 | while (!ds_get_preprocessed_line(&s, file)) { |
1840 | struct fte_version *version; | |
a9a2da38 | 1841 | struct ofputil_flow_mod fm; |
0199c526 | 1842 | |
c821124b | 1843 | parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true); |
0199c526 BP |
1844 | |
1845 | version = xmalloc(sizeof *version); | |
623e1caf | 1846 | version->cookie = fm.new_cookie; |
0199c526 BP |
1847 | version->idle_timeout = fm.idle_timeout; |
1848 | version->hard_timeout = fm.hard_timeout; | |
35805806 | 1849 | version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG); |
f25d0cf3 BP |
1850 | version->ofpacts = fm.ofpacts; |
1851 | version->ofpacts_len = fm.ofpacts_len; | |
0199c526 | 1852 | |
81a76618 | 1853 | usable_protocols &= ofputil_usable_protocols(&fm.match); |
0199c526 | 1854 | |
81a76618 | 1855 | fte_insert(cls, &fm.match, fm.priority, version, index); |
0199c526 BP |
1856 | } |
1857 | ds_destroy(&s); | |
1858 | ||
1859 | if (file != stdin) { | |
1860 | fclose(file); | |
1861 | } | |
1862 | ||
27527aa0 | 1863 | return usable_protocols; |
0199c526 BP |
1864 | } |
1865 | ||
4ce9c315 BP |
1866 | static bool |
1867 | recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid, | |
1868 | struct ofpbuf **replyp, | |
1869 | struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts) | |
1870 | { | |
1871 | struct ofpbuf *reply = *replyp; | |
1872 | ||
1873 | for (;;) { | |
4ce9c315 | 1874 | int retval; |
35805806 | 1875 | bool more; |
4ce9c315 BP |
1876 | |
1877 | /* Get a flow stats reply message, if we don't already have one. */ | |
1878 | if (!reply) { | |
982697a4 BP |
1879 | enum ofptype type; |
1880 | enum ofperr error; | |
4ce9c315 BP |
1881 | |
1882 | do { | |
1883 | run(vconn_recv_block(vconn, &reply), | |
1884 | "OpenFlow packet receive failed"); | |
1885 | } while (((struct ofp_header *) reply->data)->xid != send_xid); | |
1886 | ||
982697a4 BP |
1887 | error = ofptype_decode(&type, reply->data); |
1888 | if (error || type != OFPTYPE_FLOW_STATS_REPLY) { | |
4ce9c315 BP |
1889 | ovs_fatal(0, "received bad reply: %s", |
1890 | ofp_to_string(reply->data, reply->size, | |
1891 | verbosity + 1)); | |
1892 | } | |
1893 | } | |
1894 | ||
1895 | /* Pull an individual flow stats reply out of the message. */ | |
1896 | retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts); | |
1897 | switch (retval) { | |
1898 | case 0: | |
1899 | *replyp = reply; | |
1900 | return true; | |
1901 | ||
1902 | case EOF: | |
35805806 | 1903 | more = ofpmp_more(reply->l2); |
4ce9c315 | 1904 | ofpbuf_delete(reply); |
296ed880 | 1905 | reply = NULL; |
35805806 | 1906 | if (!more) { |
4ce9c315 BP |
1907 | *replyp = NULL; |
1908 | return false; | |
1909 | } | |
1910 | break; | |
1911 | ||
1912 | default: | |
1913 | ovs_fatal(0, "parse error in reply (%s)", | |
1914 | ofperr_to_string(retval)); | |
1915 | } | |
1916 | } | |
1917 | } | |
1918 | ||
0199c526 | 1919 | /* Reads the OpenFlow flow table from 'vconn', which has currently active flow |
27527aa0 | 1920 | * format 'protocol', and adds them as flow table entries in 'cls' for the |
0199c526 BP |
1921 | * version with the specified 'index'. */ |
1922 | static void | |
27527aa0 BP |
1923 | read_flows_from_switch(struct vconn *vconn, |
1924 | enum ofputil_protocol protocol, | |
0199c526 BP |
1925 | struct classifier *cls, int index) |
1926 | { | |
81d1ea94 | 1927 | struct ofputil_flow_stats_request fsr; |
4ce9c315 | 1928 | struct ofputil_flow_stats fs; |
0199c526 | 1929 | struct ofpbuf *request; |
4ce9c315 BP |
1930 | struct ofpbuf ofpacts; |
1931 | struct ofpbuf *reply; | |
0199c526 | 1932 | ovs_be32 send_xid; |
0199c526 BP |
1933 | |
1934 | fsr.aggregate = false; | |
81a76618 | 1935 | match_init_catchall(&fsr.match); |
0199c526 BP |
1936 | fsr.out_port = OFPP_NONE; |
1937 | fsr.table_id = 0xff; | |
ecc798ab | 1938 | fsr.cookie = fsr.cookie_mask = htonll(0); |
27527aa0 | 1939 | request = ofputil_encode_flow_stats_request(&fsr, protocol); |
0199c526 BP |
1940 | send_xid = ((struct ofp_header *) request->data)->xid; |
1941 | send_openflow_buffer(vconn, request); | |
1942 | ||
4ce9c315 BP |
1943 | reply = NULL; |
1944 | ofpbuf_init(&ofpacts, 0); | |
1945 | while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) { | |
1946 | struct fte_version *version; | |
0199c526 | 1947 | |
4ce9c315 BP |
1948 | version = xmalloc(sizeof *version); |
1949 | version->cookie = fs.cookie; | |
1950 | version->idle_timeout = fs.idle_timeout; | |
1951 | version->hard_timeout = fs.hard_timeout; | |
1952 | version->flags = 0; | |
1953 | version->ofpacts_len = fs.ofpacts_len; | |
1954 | version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len); | |
1955 | ||
81a76618 | 1956 | fte_insert(cls, &fs.match, fs.priority, version, index); |
0199c526 | 1957 | } |
4ce9c315 | 1958 | ofpbuf_uninit(&ofpacts); |
0199c526 BP |
1959 | } |
1960 | ||
1961 | static void | |
1962 | fte_make_flow_mod(const struct fte *fte, int index, uint16_t command, | |
27527aa0 | 1963 | enum ofputil_protocol protocol, struct list *packets) |
0199c526 BP |
1964 | { |
1965 | const struct fte_version *version = fte->versions[index]; | |
a9a2da38 | 1966 | struct ofputil_flow_mod fm; |
0199c526 BP |
1967 | struct ofpbuf *ofm; |
1968 | ||
5cb7a798 | 1969 | minimatch_expand(&fte->rule.match, &fm.match); |
81a76618 | 1970 | fm.priority = fte->rule.priority; |
623e1caf JP |
1971 | fm.cookie = htonll(0); |
1972 | fm.cookie_mask = htonll(0); | |
1973 | fm.new_cookie = version->cookie; | |
6c1491fb | 1974 | fm.table_id = 0xff; |
0199c526 BP |
1975 | fm.command = command; |
1976 | fm.idle_timeout = version->idle_timeout; | |
1977 | fm.hard_timeout = version->hard_timeout; | |
1978 | fm.buffer_id = UINT32_MAX; | |
1979 | fm.out_port = OFPP_NONE; | |
1980 | fm.flags = version->flags; | |
1981 | if (command == OFPFC_ADD || command == OFPFC_MODIFY || | |
1982 | command == OFPFC_MODIFY_STRICT) { | |
f25d0cf3 BP |
1983 | fm.ofpacts = version->ofpacts; |
1984 | fm.ofpacts_len = version->ofpacts_len; | |
0199c526 | 1985 | } else { |
f25d0cf3 BP |
1986 | fm.ofpacts = NULL; |
1987 | fm.ofpacts_len = 0; | |
0199c526 BP |
1988 | } |
1989 | ||
27527aa0 | 1990 | ofm = ofputil_encode_flow_mod(&fm, protocol); |
0199c526 BP |
1991 | list_push_back(packets, &ofm->list_node); |
1992 | } | |
1993 | ||
1994 | static void | |
e1fef0f9 | 1995 | ofctl_replace_flows(int argc OVS_UNUSED, char *argv[]) |
0199c526 BP |
1996 | { |
1997 | enum { FILE_IDX = 0, SWITCH_IDX = 1 }; | |
27527aa0 | 1998 | enum ofputil_protocol usable_protocols, protocol; |
0199c526 BP |
1999 | struct cls_cursor cursor; |
2000 | struct classifier cls; | |
2001 | struct list requests; | |
2002 | struct vconn *vconn; | |
2003 | struct fte *fte; | |
2004 | ||
2005 | classifier_init(&cls); | |
27527aa0 | 2006 | usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX); |
0199c526 | 2007 | |
27527aa0 BP |
2008 | protocol = open_vconn(argv[1], &vconn); |
2009 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
2010 | ||
2011 | read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX); | |
0199c526 BP |
2012 | |
2013 | list_init(&requests); | |
2014 | ||
2015 | /* Delete flows that exist on the switch but not in the file. */ | |
2016 | cls_cursor_init(&cursor, &cls, NULL); | |
2017 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
2018 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
2019 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
2020 | ||
2021 | if (sw_ver && !file_ver) { | |
2022 | fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT, | |
27527aa0 | 2023 | protocol, &requests); |
0199c526 BP |
2024 | } |
2025 | } | |
2026 | ||
2027 | /* Add flows that exist in the file but not on the switch. | |
2028 | * Update flows that exist in both places but differ. */ | |
2029 | cls_cursor_init(&cursor, &cls, NULL); | |
2030 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
2031 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
2032 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
2033 | ||
c4ea79bf BP |
2034 | if (file_ver |
2035 | && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) { | |
27527aa0 | 2036 | fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests); |
0199c526 BP |
2037 | } |
2038 | } | |
2039 | transact_multiple_noreply(vconn, &requests); | |
2040 | vconn_close(vconn); | |
2041 | ||
2042 | fte_free_all(&cls); | |
2043 | } | |
2044 | ||
2045 | static void | |
2046 | read_flows_from_source(const char *source, struct classifier *cls, int index) | |
2047 | { | |
2048 | struct stat s; | |
2049 | ||
2050 | if (source[0] == '/' || source[0] == '.' | |
2051 | || (!strchr(source, ':') && !stat(source, &s))) { | |
2052 | read_flows_from_file(source, cls, index); | |
2053 | } else { | |
27527aa0 | 2054 | enum ofputil_protocol protocol; |
0199c526 BP |
2055 | struct vconn *vconn; |
2056 | ||
27527aa0 BP |
2057 | protocol = open_vconn(source, &vconn); |
2058 | protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY); | |
2059 | read_flows_from_switch(vconn, protocol, cls, index); | |
0199c526 BP |
2060 | vconn_close(vconn); |
2061 | } | |
2062 | } | |
2063 | ||
2064 | static void | |
e1fef0f9 | 2065 | ofctl_diff_flows(int argc OVS_UNUSED, char *argv[]) |
0199c526 BP |
2066 | { |
2067 | bool differences = false; | |
2068 | struct cls_cursor cursor; | |
2069 | struct classifier cls; | |
98f7f427 | 2070 | struct ds a_s, b_s; |
0199c526 BP |
2071 | struct fte *fte; |
2072 | ||
2073 | classifier_init(&cls); | |
2074 | read_flows_from_source(argv[1], &cls, 0); | |
2075 | read_flows_from_source(argv[2], &cls, 1); | |
2076 | ||
98f7f427 BP |
2077 | ds_init(&a_s); |
2078 | ds_init(&b_s); | |
2079 | ||
0199c526 BP |
2080 | cls_cursor_init(&cursor, &cls, NULL); |
2081 | CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { | |
2082 | struct fte_version *a = fte->versions[0]; | |
2083 | struct fte_version *b = fte->versions[1]; | |
2084 | ||
2085 | if (!a || !b || !fte_version_equals(a, b)) { | |
98f7f427 BP |
2086 | fte_version_format(fte, 0, &a_s); |
2087 | fte_version_format(fte, 1, &b_s); | |
2088 | if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) { | |
2089 | if (a_s.length) { | |
2090 | printf("-%s", ds_cstr(&a_s)); | |
2091 | } | |
2092 | if (b_s.length) { | |
2093 | printf("+%s", ds_cstr(&b_s)); | |
2094 | } | |
2095 | differences = true; | |
0199c526 | 2096 | } |
0199c526 BP |
2097 | } |
2098 | } | |
2099 | ||
98f7f427 BP |
2100 | ds_destroy(&a_s); |
2101 | ds_destroy(&b_s); | |
2102 | ||
0199c526 BP |
2103 | fte_free_all(&cls); |
2104 | ||
2105 | if (differences) { | |
2106 | exit(2); | |
2107 | } | |
2108 | } | |
2109 | \f | |
09246b99 BP |
2110 | /* Undocumented commands for unit testing. */ |
2111 | ||
770f1f66 | 2112 | static void |
e1fef0f9 | 2113 | ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms) |
770f1f66 | 2114 | { |
27527aa0 BP |
2115 | enum ofputil_protocol usable_protocols; |
2116 | enum ofputil_protocol protocol = 0; | |
2117 | char *usable_s; | |
2118 | size_t i; | |
770f1f66 | 2119 | |
27527aa0 BP |
2120 | usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms); |
2121 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
2122 | printf("usable protocols: %s\n", usable_s); | |
2123 | free(usable_s); | |
2124 | ||
2125 | if (!(usable_protocols & allowed_protocols)) { | |
2126 | ovs_fatal(0, "no usable protocol"); | |
2127 | } | |
2128 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
2129 | protocol = 1 << i; | |
2130 | if (protocol & usable_protocols & allowed_protocols) { | |
2131 | break; | |
2132 | } | |
2133 | } | |
2134 | assert(IS_POW2(protocol)); | |
2135 | ||
2136 | printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol)); | |
2137 | ||
2138 | for (i = 0; i < n_fms; i++) { | |
2139 | struct ofputil_flow_mod *fm = &fms[i]; | |
2140 | struct ofpbuf *msg; | |
2141 | ||
2142 | msg = ofputil_encode_flow_mod(fm, protocol); | |
2143 | ofp_print(stdout, msg->data, msg->size, verbosity); | |
2144 | ofpbuf_delete(msg); | |
2145 | ||
f25d0cf3 | 2146 | free(fm->ofpacts); |
770f1f66 BP |
2147 | } |
2148 | } | |
2149 | ||
2150 | /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints | |
2151 | * it back to stdout. */ | |
2152 | static void | |
e1fef0f9 | 2153 | ofctl_parse_flow(int argc OVS_UNUSED, char *argv[]) |
770f1f66 | 2154 | { |
27527aa0 | 2155 | struct ofputil_flow_mod fm; |
770f1f66 | 2156 | |
27527aa0 | 2157 | parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false); |
e1fef0f9 | 2158 | ofctl_parse_flows__(&fm, 1); |
770f1f66 BP |
2159 | } |
2160 | ||
fec00620 BP |
2161 | /* "parse-flows FILENAME": reads the named file as a sequence of flows (like |
2162 | * add-flows) and prints each of the flows back to stdout. */ | |
0e581146 | 2163 | static void |
e1fef0f9 | 2164 | ofctl_parse_flows(int argc OVS_UNUSED, char *argv[]) |
0e581146 | 2165 | { |
27527aa0 BP |
2166 | struct ofputil_flow_mod *fms = NULL; |
2167 | size_t n_fms = 0; | |
0e581146 | 2168 | |
27527aa0 | 2169 | parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms); |
e1fef0f9 | 2170 | ofctl_parse_flows__(fms, n_fms); |
27527aa0 | 2171 | free(fms); |
0e581146 BP |
2172 | } |
2173 | ||
064af421 | 2174 | static void |
e1fef0f9 | 2175 | ofctl_parse_nxm__(bool oxm) |
09246b99 BP |
2176 | { |
2177 | struct ds in; | |
2178 | ||
2179 | ds_init(&in); | |
06d7ae7d | 2180 | while (!ds_get_test_line(&in, stdin)) { |
09246b99 | 2181 | struct ofpbuf nx_match; |
81a76618 | 2182 | struct match match; |
e729e793 | 2183 | ovs_be64 cookie, cookie_mask; |
90bf1e07 | 2184 | enum ofperr error; |
09246b99 | 2185 | int match_len; |
09246b99 BP |
2186 | |
2187 | /* Convert string to nx_match. */ | |
2188 | ofpbuf_init(&nx_match, 0); | |
7623f4dd SH |
2189 | if (oxm) { |
2190 | match_len = oxm_match_from_string(ds_cstr(&in), &nx_match); | |
2191 | } else { | |
2192 | match_len = nx_match_from_string(ds_cstr(&in), &nx_match); | |
2193 | } | |
09246b99 | 2194 | |
81a76618 | 2195 | /* Convert nx_match to match. */ |
102ce766 | 2196 | if (strict) { |
7623f4dd | 2197 | if (oxm) { |
81a76618 | 2198 | error = oxm_pull_match(&nx_match, &match); |
7623f4dd | 2199 | } else { |
81a76618 | 2200 | error = nx_pull_match(&nx_match, match_len, &match, |
7623f4dd SH |
2201 | &cookie, &cookie_mask); |
2202 | } | |
102ce766 | 2203 | } else { |
7623f4dd | 2204 | if (oxm) { |
81a76618 | 2205 | error = oxm_pull_match_loose(&nx_match, &match); |
7623f4dd | 2206 | } else { |
81a76618 | 2207 | error = nx_pull_match_loose(&nx_match, match_len, &match, |
7623f4dd SH |
2208 | &cookie, &cookie_mask); |
2209 | } | |
102ce766 EJ |
2210 | } |
2211 | ||
7623f4dd | 2212 | |
09246b99 BP |
2213 | if (!error) { |
2214 | char *out; | |
2215 | ||
81a76618 | 2216 | /* Convert match back to nx_match. */ |
09246b99 BP |
2217 | ofpbuf_uninit(&nx_match); |
2218 | ofpbuf_init(&nx_match, 0); | |
7623f4dd | 2219 | if (oxm) { |
81a76618 | 2220 | match_len = oxm_put_match(&nx_match, &match); |
7623f4dd SH |
2221 | out = oxm_match_to_string(nx_match.data, match_len); |
2222 | } else { | |
81a76618 | 2223 | match_len = nx_put_match(&nx_match, &match, |
7623f4dd SH |
2224 | cookie, cookie_mask); |
2225 | out = nx_match_to_string(nx_match.data, match_len); | |
2226 | } | |
09246b99 | 2227 | |
09246b99 BP |
2228 | puts(out); |
2229 | free(out); | |
2230 | } else { | |
90bf1e07 BP |
2231 | printf("nx_pull_match() returned error %s\n", |
2232 | ofperr_get_name(error)); | |
09246b99 BP |
2233 | } |
2234 | ||
2235 | ofpbuf_uninit(&nx_match); | |
2236 | } | |
2237 | ds_destroy(&in); | |
064af421 BP |
2238 | } |
2239 | ||
b5ae8913 SH |
2240 | /* "parse-nxm": reads a series of NXM nx_match specifications as strings from |
2241 | * stdin, does some internal fussing with them, and then prints them back as | |
2242 | * strings on stdout. */ | |
2243 | static void | |
e1fef0f9 | 2244 | ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
b5ae8913 | 2245 | { |
e1fef0f9 | 2246 | return ofctl_parse_nxm__(false); |
b5ae8913 SH |
2247 | } |
2248 | ||
2249 | /* "parse-oxm": reads a series of OXM nx_match specifications as strings from | |
2250 | * stdin, does some internal fussing with them, and then prints them back as | |
2251 | * strings on stdout. */ | |
2252 | static void | |
e1fef0f9 | 2253 | ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
b5ae8913 | 2254 | { |
e1fef0f9 | 2255 | return ofctl_parse_nxm__(true); |
b5ae8913 SH |
2256 | } |
2257 | ||
f25d0cf3 | 2258 | static void |
96628ae8 BP |
2259 | print_differences(const char *prefix, |
2260 | const void *a_, size_t a_len, | |
f25d0cf3 BP |
2261 | const void *b_, size_t b_len) |
2262 | { | |
2263 | const uint8_t *a = a_; | |
2264 | const uint8_t *b = b_; | |
2265 | size_t i; | |
2266 | ||
2267 | for (i = 0; i < MIN(a_len, b_len); i++) { | |
2268 | if (a[i] != b[i]) { | |
96628ae8 BP |
2269 | printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n", |
2270 | prefix, i, a[i], b[i]); | |
f25d0cf3 BP |
2271 | } |
2272 | } | |
2273 | for (i = a_len; i < b_len; i++) { | |
96628ae8 | 2274 | printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]); |
f25d0cf3 BP |
2275 | } |
2276 | for (i = b_len; i < a_len; i++) { | |
96628ae8 | 2277 | printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]); |
f25d0cf3 BP |
2278 | } |
2279 | } | |
2280 | ||
2281 | /* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications | |
2282 | * as hex bytes from stdin, converts them to ofpacts, prints them as strings | |
2283 | * on stdout, and then converts them back to hex bytes and prints any | |
2284 | * differences from the input. */ | |
2285 | static void | |
e1fef0f9 | 2286 | ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
f25d0cf3 BP |
2287 | { |
2288 | struct ds in; | |
2289 | ||
2290 | ds_init(&in); | |
2291 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2292 | struct ofpbuf of10_out; | |
2293 | struct ofpbuf of10_in; | |
2294 | struct ofpbuf ofpacts; | |
2295 | enum ofperr error; | |
2296 | size_t size; | |
2297 | struct ds s; | |
2298 | ||
2299 | /* Parse hex bytes. */ | |
2300 | ofpbuf_init(&of10_in, 0); | |
2301 | if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2302 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2303 | } | |
2304 | ||
2305 | /* Convert to ofpacts. */ | |
2306 | ofpbuf_init(&ofpacts, 0); | |
2307 | size = of10_in.size; | |
d01c980f | 2308 | error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts); |
f25d0cf3 BP |
2309 | if (error) { |
2310 | printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error)); | |
2311 | ofpbuf_uninit(&ofpacts); | |
2312 | ofpbuf_uninit(&of10_in); | |
2313 | continue; | |
2314 | } | |
2315 | ofpbuf_push_uninit(&of10_in, size); | |
2316 | ||
2317 | /* Print cls_rule. */ | |
2318 | ds_init(&s); | |
2319 | ofpacts_format(ofpacts.data, ofpacts.size, &s); | |
2320 | puts(ds_cstr(&s)); | |
2321 | ds_destroy(&s); | |
2322 | ||
2323 | /* Convert back to ofp10 actions and print differences from input. */ | |
2324 | ofpbuf_init(&of10_out, 0); | |
d01c980f | 2325 | ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out); |
f25d0cf3 | 2326 | |
96628ae8 | 2327 | print_differences("", of10_in.data, of10_in.size, |
f25d0cf3 BP |
2328 | of10_out.data, of10_out.size); |
2329 | putchar('\n'); | |
2330 | ||
2331 | ofpbuf_uninit(&ofpacts); | |
2332 | ofpbuf_uninit(&of10_in); | |
2333 | ofpbuf_uninit(&of10_out); | |
2334 | } | |
2335 | ds_destroy(&in); | |
2336 | } | |
2337 | ||
96628ae8 BP |
2338 | /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex |
2339 | * bytes from stdin, converts them to cls_rules, prints them as strings on | |
2340 | * stdout, and then converts them back to hex bytes and prints any differences | |
8812ec2c BP |
2341 | * from the input. |
2342 | * | |
2343 | * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose | |
2344 | * values are ignored in the input and will be set to zero when OVS converts | |
2345 | * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when | |
2346 | * it does the conversion to hex, to ensure that in fact they are ignored. */ | |
96628ae8 BP |
2347 | static void |
2348 | ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) | |
2349 | { | |
8812ec2c | 2350 | struct ds expout; |
96628ae8 BP |
2351 | struct ds in; |
2352 | ||
2353 | ds_init(&in); | |
8812ec2c | 2354 | ds_init(&expout); |
96628ae8 | 2355 | while (!ds_get_preprocessed_line(&in, stdin)) { |
8812ec2c | 2356 | struct ofpbuf match_in, match_expout; |
96628ae8 BP |
2357 | struct ofp10_match match_out; |
2358 | struct ofp10_match match_normal; | |
81a76618 | 2359 | struct match match; |
8812ec2c BP |
2360 | char *p; |
2361 | ||
2362 | /* Parse hex bytes to use for expected output. */ | |
2363 | ds_clear(&expout); | |
2364 | ds_put_cstr(&expout, ds_cstr(&in)); | |
2365 | for (p = ds_cstr(&expout); *p; p++) { | |
2366 | if (*p == 'x') { | |
2367 | *p = '0'; | |
2368 | } | |
2369 | } | |
2370 | ofpbuf_init(&match_expout, 0); | |
2371 | if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') { | |
2372 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2373 | } | |
2374 | if (match_expout.size != sizeof(struct ofp10_match)) { | |
2375 | ovs_fatal(0, "Input is %zu bytes, expected %zu", | |
2376 | match_expout.size, sizeof(struct ofp10_match)); | |
2377 | } | |
96628ae8 | 2378 | |
8812ec2c BP |
2379 | /* Parse hex bytes for input. */ |
2380 | for (p = ds_cstr(&in); *p; p++) { | |
2381 | if (*p == 'x') { | |
2382 | *p = "0123456789abcdef"[random_uint32() & 0xf]; | |
2383 | } | |
2384 | } | |
96628ae8 BP |
2385 | ofpbuf_init(&match_in, 0); |
2386 | if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2387 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2388 | } | |
2389 | if (match_in.size != sizeof(struct ofp10_match)) { | |
2390 | ovs_fatal(0, "Input is %zu bytes, expected %zu", | |
2391 | match_in.size, sizeof(struct ofp10_match)); | |
2392 | } | |
2393 | ||
2394 | /* Convert to cls_rule and print. */ | |
81a76618 BP |
2395 | ofputil_match_from_ofp10_match(match_in.data, &match); |
2396 | match_print(&match); | |
96628ae8 BP |
2397 | |
2398 | /* Convert back to ofp10_match and print differences from input. */ | |
81a76618 | 2399 | ofputil_match_to_ofp10_match(&match, &match_out); |
8812ec2c | 2400 | print_differences("", match_expout.data, match_expout.size, |
96628ae8 BP |
2401 | &match_out, sizeof match_out); |
2402 | ||
2403 | /* Normalize, then convert and compare again. */ | |
81a76618 BP |
2404 | ofputil_normalize_match(&match); |
2405 | ofputil_match_to_ofp10_match(&match, &match_normal); | |
96628ae8 BP |
2406 | print_differences("normal: ", &match_out, sizeof match_out, |
2407 | &match_normal, sizeof match_normal); | |
2408 | putchar('\n'); | |
2409 | ||
2410 | ofpbuf_uninit(&match_in); | |
8812ec2c | 2411 | ofpbuf_uninit(&match_expout); |
96628ae8 BP |
2412 | } |
2413 | ds_destroy(&in); | |
8812ec2c | 2414 | ds_destroy(&expout); |
96628ae8 BP |
2415 | } |
2416 | ||
410698cf | 2417 | /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex |
81a76618 BP |
2418 | * bytes from stdin, converts them to "struct match"es, prints them as strings |
2419 | * on stdout, and then converts them back to hex bytes and prints any | |
2420 | * differences from the input. */ | |
410698cf | 2421 | static void |
e1fef0f9 | 2422 | ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
410698cf BP |
2423 | { |
2424 | struct ds in; | |
2425 | ||
2426 | ds_init(&in); | |
2427 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2428 | struct ofpbuf match_in; | |
2429 | struct ofp11_match match_out; | |
81a76618 | 2430 | struct match match; |
410698cf | 2431 | enum ofperr error; |
410698cf BP |
2432 | |
2433 | /* Parse hex bytes. */ | |
2434 | ofpbuf_init(&match_in, 0); | |
2435 | if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2436 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2437 | } | |
2438 | if (match_in.size != sizeof(struct ofp11_match)) { | |
2439 | ovs_fatal(0, "Input is %zu bytes, expected %zu", | |
2440 | match_in.size, sizeof(struct ofp11_match)); | |
2441 | } | |
2442 | ||
81a76618 BP |
2443 | /* Convert to match. */ |
2444 | error = ofputil_match_from_ofp11_match(match_in.data, &match); | |
410698cf BP |
2445 | if (error) { |
2446 | printf("bad ofp11_match: %s\n\n", ofperr_get_name(error)); | |
2447 | ofpbuf_uninit(&match_in); | |
2448 | continue; | |
2449 | } | |
2450 | ||
81a76618 BP |
2451 | /* Print match. */ |
2452 | match_print(&match); | |
410698cf BP |
2453 | |
2454 | /* Convert back to ofp11_match and print differences from input. */ | |
81a76618 | 2455 | ofputil_match_to_ofp11_match(&match, &match_out); |
410698cf | 2456 | |
96628ae8 | 2457 | print_differences("", match_in.data, match_in.size, |
d01c980f BP |
2458 | &match_out, sizeof match_out); |
2459 | putchar('\n'); | |
410698cf | 2460 | |
d01c980f BP |
2461 | ofpbuf_uninit(&match_in); |
2462 | } | |
2463 | ds_destroy(&in); | |
2464 | } | |
2465 | ||
2466 | /* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications | |
2467 | * as hex bytes from stdin, converts them to ofpacts, prints them as strings | |
2468 | * on stdout, and then converts them back to hex bytes and prints any | |
2469 | * differences from the input. */ | |
2470 | static void | |
e1fef0f9 | 2471 | ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
d01c980f BP |
2472 | { |
2473 | struct ds in; | |
2474 | ||
2475 | ds_init(&in); | |
2476 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2477 | struct ofpbuf of11_out; | |
2478 | struct ofpbuf of11_in; | |
2479 | struct ofpbuf ofpacts; | |
2480 | enum ofperr error; | |
2481 | size_t size; | |
2482 | struct ds s; | |
2483 | ||
2484 | /* Parse hex bytes. */ | |
2485 | ofpbuf_init(&of11_in, 0); | |
2486 | if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2487 | ovs_fatal(0, "Trailing garbage in hex data"); | |
410698cf | 2488 | } |
d01c980f BP |
2489 | |
2490 | /* Convert to ofpacts. */ | |
2491 | ofpbuf_init(&ofpacts, 0); | |
2492 | size = of11_in.size; | |
2493 | error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size, | |
2494 | &ofpacts); | |
2495 | if (error) { | |
2496 | printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error)); | |
2497 | ofpbuf_uninit(&ofpacts); | |
2498 | ofpbuf_uninit(&of11_in); | |
2499 | continue; | |
2500 | } | |
2501 | ofpbuf_push_uninit(&of11_in, size); | |
2502 | ||
2503 | /* Print cls_rule. */ | |
2504 | ds_init(&s); | |
2505 | ofpacts_format(ofpacts.data, ofpacts.size, &s); | |
2506 | puts(ds_cstr(&s)); | |
2507 | ds_destroy(&s); | |
2508 | ||
2509 | /* Convert back to ofp11 actions and print differences from input. */ | |
2510 | ofpbuf_init(&of11_out, 0); | |
2511 | ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out); | |
2512 | ||
96628ae8 | 2513 | print_differences("", of11_in.data, of11_in.size, |
d01c980f | 2514 | of11_out.data, of11_out.size); |
410698cf BP |
2515 | putchar('\n'); |
2516 | ||
d01c980f BP |
2517 | ofpbuf_uninit(&ofpacts); |
2518 | ofpbuf_uninit(&of11_in); | |
2519 | ofpbuf_uninit(&of11_out); | |
2520 | } | |
2521 | ds_destroy(&in); | |
2522 | } | |
2523 | ||
2524 | /* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction | |
2525 | * specifications as hex bytes from stdin, converts them to ofpacts, prints | |
2526 | * them as strings on stdout, and then converts them back to hex bytes and | |
2527 | * prints any differences from the input. */ | |
2528 | static void | |
e1fef0f9 | 2529 | ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) |
d01c980f BP |
2530 | { |
2531 | struct ds in; | |
2532 | ||
2533 | ds_init(&in); | |
2534 | while (!ds_get_preprocessed_line(&in, stdin)) { | |
2535 | struct ofpbuf of11_out; | |
2536 | struct ofpbuf of11_in; | |
2537 | struct ofpbuf ofpacts; | |
2538 | enum ofperr error; | |
2539 | size_t size; | |
2540 | struct ds s; | |
2541 | ||
2542 | /* Parse hex bytes. */ | |
2543 | ofpbuf_init(&of11_in, 0); | |
2544 | if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') { | |
2545 | ovs_fatal(0, "Trailing garbage in hex data"); | |
2546 | } | |
2547 | ||
2548 | /* Convert to ofpacts. */ | |
2549 | ofpbuf_init(&ofpacts, 0); | |
2550 | size = of11_in.size; | |
2551 | error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size, | |
2552 | &ofpacts); | |
2553 | if (error) { | |
2554 | printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error)); | |
2555 | ofpbuf_uninit(&ofpacts); | |
2556 | ofpbuf_uninit(&of11_in); | |
2557 | continue; | |
2558 | } | |
2559 | ofpbuf_push_uninit(&of11_in, size); | |
2560 | ||
2561 | /* Print cls_rule. */ | |
2562 | ds_init(&s); | |
2563 | ofpacts_format(ofpacts.data, ofpacts.size, &s); | |
2564 | puts(ds_cstr(&s)); | |
2565 | ds_destroy(&s); | |
2566 | ||
2567 | /* Convert back to ofp11 instructions and print differences from | |
2568 | * input. */ | |
2569 | ofpbuf_init(&of11_out, 0); | |
2570 | ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size, | |
2571 | &of11_out); | |
2572 | ||
96628ae8 | 2573 | print_differences("", of11_in.data, of11_in.size, |
d01c980f BP |
2574 | of11_out.data, of11_out.size); |
2575 | putchar('\n'); | |
2576 | ||
2577 | ofpbuf_uninit(&ofpacts); | |
2578 | ofpbuf_uninit(&of11_in); | |
2579 | ofpbuf_uninit(&of11_out); | |
410698cf BP |
2580 | } |
2581 | ds_destroy(&in); | |
2582 | } | |
2583 | ||
df778240 BP |
2584 | /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and |
2585 | * mask values to and from various formats and prints the results. */ | |
2586 | static void | |
2587 | ofctl_check_vlan(int argc OVS_UNUSED, char *argv[]) | |
2588 | { | |
81a76618 | 2589 | struct match match; |
df778240 BP |
2590 | |
2591 | char *string_s; | |
2592 | struct ofputil_flow_mod fm; | |
2593 | ||
2594 | struct ofpbuf nxm; | |
81a76618 | 2595 | struct match nxm_match; |
df778240 BP |
2596 | int nxm_match_len; |
2597 | char *nxm_s; | |
2598 | ||
81a76618 BP |
2599 | struct ofp10_match of10_raw; |
2600 | struct match of10_match; | |
df778240 | 2601 | |
81a76618 BP |
2602 | struct ofp11_match of11_raw; |
2603 | struct match of11_match; | |
df778240 BP |
2604 | |
2605 | enum ofperr error; | |
2606 | ||
81a76618 BP |
2607 | match_init_catchall(&match); |
2608 | match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16)); | |
2609 | match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16)); | |
df778240 BP |
2610 | |
2611 | /* Convert to and from string. */ | |
81a76618 | 2612 | string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY); |
df778240 BP |
2613 | printf("%s -> ", string_s); |
2614 | fflush(stdout); | |
2615 | parse_ofp_str(&fm, -1, string_s, false); | |
2616 | printf("%04"PRIx16"/%04"PRIx16"\n", | |
81a76618 BP |
2617 | ntohs(fm.match.flow.vlan_tci), |
2618 | ntohs(fm.match.wc.masks.vlan_tci)); | |
4d859fa9 | 2619 | free(string_s); |
df778240 BP |
2620 | |
2621 | /* Convert to and from NXM. */ | |
2622 | ofpbuf_init(&nxm, 0); | |
81a76618 | 2623 | nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0)); |
df778240 | 2624 | nxm_s = nx_match_to_string(nxm.data, nxm_match_len); |
81a76618 | 2625 | error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL); |
df778240 BP |
2626 | printf("NXM: %s -> ", nxm_s); |
2627 | if (error) { | |
2628 | printf("%s\n", ofperr_to_string(error)); | |
2629 | } else { | |
2630 | printf("%04"PRIx16"/%04"PRIx16"\n", | |
81a76618 BP |
2631 | ntohs(nxm_match.flow.vlan_tci), |
2632 | ntohs(nxm_match.wc.masks.vlan_tci)); | |
df778240 BP |
2633 | } |
2634 | free(nxm_s); | |
2635 | ofpbuf_uninit(&nxm); | |
2636 | ||
476a0e9e SH |
2637 | /* Convert to and from OXM. */ |
2638 | ofpbuf_init(&nxm, 0); | |
81a76618 | 2639 | nxm_match_len = oxm_put_match(&nxm, &match); |
7623f4dd | 2640 | nxm_s = oxm_match_to_string(nxm.data, nxm_match_len); |
81a76618 | 2641 | error = oxm_pull_match(&nxm, &nxm_match); |
476a0e9e SH |
2642 | printf("OXM: %s -> ", nxm_s); |
2643 | if (error) { | |
2644 | printf("%s\n", ofperr_to_string(error)); | |
2645 | } else { | |
81a76618 | 2646 | uint16_t vid = ntohs(nxm_match.flow.vlan_tci) & |
476a0e9e | 2647 | (VLAN_VID_MASK | VLAN_CFI); |
81a76618 | 2648 | uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) & |
476a0e9e SH |
2649 | (VLAN_VID_MASK | VLAN_CFI); |
2650 | ||
2651 | printf("%04"PRIx16"/%04"PRIx16",", vid, mask); | |
81a76618 BP |
2652 | if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) { |
2653 | printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci)); | |
476a0e9e SH |
2654 | } else { |
2655 | printf("--\n"); | |
2656 | } | |
2657 | } | |
2658 | free(nxm_s); | |
2659 | ofpbuf_uninit(&nxm); | |
2660 | ||
df778240 | 2661 | /* Convert to and from OpenFlow 1.0. */ |
81a76618 BP |
2662 | ofputil_match_to_ofp10_match(&match, &of10_raw); |
2663 | ofputil_match_from_ofp10_match(&of10_raw, &of10_match); | |
df778240 | 2664 | printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n", |
81a76618 BP |
2665 | ntohs(of10_raw.dl_vlan), |
2666 | (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0, | |
2667 | of10_raw.dl_vlan_pcp, | |
2668 | (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0, | |
2669 | ntohs(of10_match.flow.vlan_tci), | |
2670 | ntohs(of10_match.wc.masks.vlan_tci)); | |
df778240 BP |
2671 | |
2672 | /* Convert to and from OpenFlow 1.1. */ | |
81a76618 BP |
2673 | ofputil_match_to_ofp11_match(&match, &of11_raw); |
2674 | ofputil_match_from_ofp11_match(&of11_raw, &of11_match); | |
df778240 | 2675 | printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n", |
81a76618 BP |
2676 | ntohs(of11_raw.dl_vlan), |
2677 | (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0, | |
2678 | of11_raw.dl_vlan_pcp, | |
2679 | (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0, | |
2680 | ntohs(of11_match.flow.vlan_tci), | |
2681 | ntohs(of11_match.wc.masks.vlan_tci)); | |
df778240 BP |
2682 | } |
2683 | ||
2e0525bc SH |
2684 | /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow |
2685 | * version. */ | |
2686 | static void | |
e1fef0f9 | 2687 | ofctl_print_error(int argc OVS_UNUSED, char *argv[]) |
2e0525bc SH |
2688 | { |
2689 | enum ofperr error; | |
2690 | int version; | |
2691 | ||
2692 | error = ofperr_from_name(argv[1]); | |
2693 | if (!error) { | |
2694 | ovs_fatal(0, "unknown error \"%s\"", argv[1]); | |
2695 | } | |
2696 | ||
2697 | for (version = 0; version <= UINT8_MAX; version++) { | |
688e86e1 SH |
2698 | const char *name = ofperr_domain_get_name(version); |
2699 | if (!name) { | |
2e0525bc SH |
2700 | continue; |
2701 | } | |
2e0525bc | 2702 | printf("%s: %d,%d\n", |
688e86e1 SH |
2703 | ofperr_domain_get_name(version), |
2704 | ofperr_get_type(error, version), | |
2705 | ofperr_get_code(error, version)); | |
2e0525bc SH |
2706 | } |
2707 | } | |
2708 | ||
fec00620 BP |
2709 | /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into |
2710 | * binary data, interpreting them as an OpenFlow message, and prints the | |
2711 | * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */ | |
2712 | static void | |
e1fef0f9 | 2713 | ofctl_ofp_print(int argc, char *argv[]) |
fec00620 BP |
2714 | { |
2715 | struct ofpbuf packet; | |
2716 | ||
2717 | ofpbuf_init(&packet, strlen(argv[1]) / 2); | |
2718 | if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') { | |
2719 | ovs_fatal(0, "trailing garbage following hex bytes"); | |
2720 | } | |
2721 | ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2); | |
2722 | ofpbuf_uninit(&packet); | |
2723 | } | |
2724 | ||
675febfa | 2725 | static const struct command all_commands[] = { |
e1fef0f9 AS |
2726 | { "show", 1, 1, ofctl_show }, |
2727 | { "monitor", 1, 3, ofctl_monitor }, | |
2728 | { "snoop", 1, 1, ofctl_snoop }, | |
2729 | { "dump-desc", 1, 1, ofctl_dump_desc }, | |
2730 | { "dump-tables", 1, 1, ofctl_dump_tables }, | |
2731 | { "dump-flows", 1, 2, ofctl_dump_flows }, | |
2732 | { "dump-aggregate", 1, 2, ofctl_dump_aggregate }, | |
2733 | { "queue-stats", 1, 3, ofctl_queue_stats }, | |
2734 | { "add-flow", 2, 2, ofctl_add_flow }, | |
2735 | { "add-flows", 2, 2, ofctl_add_flows }, | |
2736 | { "mod-flows", 2, 2, ofctl_mod_flows }, | |
2737 | { "del-flows", 1, 2, ofctl_del_flows }, | |
2738 | { "replace-flows", 2, 2, ofctl_replace_flows }, | |
2739 | { "diff-flows", 2, 2, ofctl_diff_flows }, | |
2740 | { "packet-out", 4, INT_MAX, ofctl_packet_out }, | |
2741 | { "dump-ports", 1, 2, ofctl_dump_ports }, | |
2742 | { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc }, | |
2743 | { "mod-port", 3, 3, ofctl_mod_port }, | |
2744 | { "get-frags", 1, 1, ofctl_get_frags }, | |
2745 | { "set-frags", 2, 2, ofctl_set_frags }, | |
2746 | { "probe", 1, 1, ofctl_probe }, | |
2747 | { "ping", 1, 2, ofctl_ping }, | |
2748 | { "benchmark", 3, 3, ofctl_benchmark }, | |
2749 | { "help", 0, INT_MAX, ofctl_help }, | |
09246b99 BP |
2750 | |
2751 | /* Undocumented commands for testing. */ | |
e1fef0f9 AS |
2752 | { "parse-flow", 1, 1, ofctl_parse_flow }, |
2753 | { "parse-flows", 1, 1, ofctl_parse_flows }, | |
2754 | { "parse-nx-match", 0, 0, ofctl_parse_nxm }, | |
2755 | { "parse-nxm", 0, 0, ofctl_parse_nxm }, | |
2756 | { "parse-oxm", 0, 0, ofctl_parse_oxm }, | |
2757 | { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions }, | |
96628ae8 | 2758 | { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match }, |
e1fef0f9 AS |
2759 | { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match }, |
2760 | { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions }, | |
2761 | { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions }, | |
df778240 | 2762 | { "check-vlan", 2, 2, ofctl_check_vlan }, |
e1fef0f9 AS |
2763 | { "print-error", 1, 1, ofctl_print_error }, |
2764 | { "ofp-print", 1, 2, ofctl_ofp_print }, | |
09246b99 | 2765 | |
064af421 BP |
2766 | { NULL, 0, 0, NULL }, |
2767 | }; |