]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. | |
3 | * | |
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: | |
7 | * | |
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. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | #include <ctype.h> | |
19 | #include <errno.h> | |
20 | #include <getopt.h> | |
21 | #include <inttypes.h> | |
22 | #include <sys/socket.h> | |
23 | #include <net/if.h> | |
24 | #include <signal.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <fcntl.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/time.h> | |
31 | ||
32 | #include "byte-order.h" | |
33 | #include "classifier.h" | |
34 | #include "command-line.h" | |
35 | #include "daemon.h" | |
36 | #include "colors.h" | |
37 | #include "compiler.h" | |
38 | #include "dirs.h" | |
39 | #include "dp-packet.h" | |
40 | #include "fatal-signal.h" | |
41 | #include "nx-match.h" | |
42 | #include "odp-util.h" | |
43 | #include "ofp-version-opt.h" | |
44 | #include "ofproto/ofproto.h" | |
45 | #include "openflow/nicira-ext.h" | |
46 | #include "openflow/openflow.h" | |
47 | #include "openvswitch/dynamic-string.h" | |
48 | #include "openvswitch/meta-flow.h" | |
49 | #include "openvswitch/ofp-actions.h" | |
50 | #include "openvswitch/ofp-errors.h" | |
51 | #include "openvswitch/ofp-msgs.h" | |
52 | #include "openvswitch/ofp-print.h" | |
53 | #include "openvswitch/ofp-util.h" | |
54 | #include "openvswitch/ofp-parse.h" | |
55 | #include "openvswitch/ofpbuf.h" | |
56 | #include "openvswitch/vconn.h" | |
57 | #include "openvswitch/vlog.h" | |
58 | #include "packets.h" | |
59 | #include "pcap-file.h" | |
60 | #include "poll-loop.h" | |
61 | #include "random.h" | |
62 | #include "sort.h" | |
63 | #include "stream-ssl.h" | |
64 | #include "socket-util.h" | |
65 | #include "timeval.h" | |
66 | #include "unixctl.h" | |
67 | #include "util.h" | |
68 | ||
69 | VLOG_DEFINE_THIS_MODULE(ofctl); | |
70 | ||
71 | /* --bundle: Use OpenFlow 1.3+ bundle for making the flow table change atomic. | |
72 | * NOTE: If OpenFlow 1.3 or higher is not selected with the '-O' option, | |
73 | * OpenFlow 1.4 will be implicitly selected. Also the flow mod will use | |
74 | * OpenFlow 1.4, so the semantics may be different (see the comment in | |
75 | * parse_options() for details). | |
76 | */ | |
77 | static bool bundle = false; | |
78 | ||
79 | /* --color: Use color markers. */ | |
80 | static bool enable_color; | |
81 | ||
82 | /* --read-only: Do not execute read only commands. */ | |
83 | static bool read_only; | |
84 | ||
85 | /* --strict: Use strict matching for flow mod commands? Additionally governs | |
86 | * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match. | |
87 | */ | |
88 | static bool strict; | |
89 | ||
90 | /* --may-create: If true, the mod-group command creates a group that does not | |
91 | * yet exist; otherwise, such a command has no effect. */ | |
92 | static bool may_create; | |
93 | ||
94 | /* --readd: If true, on replace-flows, re-add even flows that have not changed | |
95 | * (to reset flow counters). */ | |
96 | static bool readd; | |
97 | ||
98 | /* -F, --flow-format: Allowed protocols. By default, any protocol is | |
99 | * allowed. */ | |
100 | static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY; | |
101 | ||
102 | /* -P, --packet-in-format: Packet IN format to use in monitor and snoop | |
103 | * commands. Either one of NXPIF_* to force a particular packet_in format, or | |
104 | * -1 to let ovs-ofctl choose the default. */ | |
105 | static int preferred_packet_in_format = -1; | |
106 | ||
107 | /* -m, --more: Additional verbosity for ofp-print functions. */ | |
108 | static int verbosity; | |
109 | ||
110 | /* --timestamp: Print a timestamp before each received packet on "monitor" and | |
111 | * "snoop" command? */ | |
112 | static bool timestamp; | |
113 | ||
114 | /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop" | |
115 | commands. */ | |
116 | static char *unixctl_path; | |
117 | ||
118 | /* --sort, --rsort: Sort order. */ | |
119 | enum sort_order { SORT_ASC, SORT_DESC }; | |
120 | struct sort_criterion { | |
121 | const struct mf_field *field; /* NULL means to sort by priority. */ | |
122 | enum sort_order order; | |
123 | }; | |
124 | static struct sort_criterion *criteria; | |
125 | static size_t n_criteria, allocated_criteria; | |
126 | ||
127 | static const struct ovs_cmdl_command *get_all_commands(void); | |
128 | ||
129 | OVS_NO_RETURN static void usage(void); | |
130 | static void parse_options(int argc, char *argv[]); | |
131 | ||
132 | static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid, | |
133 | struct ofpbuf **replyp, | |
134 | struct ofputil_flow_stats *, | |
135 | struct ofpbuf *ofpacts); | |
136 | int | |
137 | main(int argc, char *argv[]) | |
138 | { | |
139 | struct ovs_cmdl_context ctx = { .argc = 0, }; | |
140 | set_program_name(argv[0]); | |
141 | service_start(&argc, &argv); | |
142 | parse_options(argc, argv); | |
143 | fatal_ignore_sigpipe(); | |
144 | ctx.argc = argc - optind; | |
145 | ctx.argv = argv + optind; | |
146 | ||
147 | daemon_become_new_user(false); | |
148 | if (read_only) { | |
149 | ovs_cmdl_run_command_read_only(&ctx, get_all_commands()); | |
150 | } else { | |
151 | ovs_cmdl_run_command(&ctx, get_all_commands()); | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static void | |
157 | add_sort_criterion(enum sort_order order, const char *field) | |
158 | { | |
159 | struct sort_criterion *sc; | |
160 | ||
161 | if (n_criteria >= allocated_criteria) { | |
162 | criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria); | |
163 | } | |
164 | ||
165 | sc = &criteria[n_criteria++]; | |
166 | if (!field || !strcasecmp(field, "priority")) { | |
167 | sc->field = NULL; | |
168 | } else { | |
169 | sc->field = mf_from_name(field); | |
170 | if (!sc->field) { | |
171 | ovs_fatal(0, "%s: unknown field name", field); | |
172 | } | |
173 | } | |
174 | sc->order = order; | |
175 | } | |
176 | ||
177 | static void | |
178 | parse_options(int argc, char *argv[]) | |
179 | { | |
180 | enum { | |
181 | OPT_STRICT = UCHAR_MAX + 1, | |
182 | OPT_READD, | |
183 | OPT_TIMESTAMP, | |
184 | OPT_SORT, | |
185 | OPT_RSORT, | |
186 | OPT_UNIXCTL, | |
187 | OPT_BUNDLE, | |
188 | OPT_COLOR, | |
189 | OPT_MAY_CREATE, | |
190 | OPT_READ_ONLY, | |
191 | DAEMON_OPTION_ENUMS, | |
192 | OFP_VERSION_OPTION_ENUMS, | |
193 | VLOG_OPTION_ENUMS | |
194 | }; | |
195 | static const struct option long_options[] = { | |
196 | {"timeout", required_argument, NULL, 't'}, | |
197 | {"strict", no_argument, NULL, OPT_STRICT}, | |
198 | {"readd", no_argument, NULL, OPT_READD}, | |
199 | {"flow-format", required_argument, NULL, 'F'}, | |
200 | {"packet-in-format", required_argument, NULL, 'P'}, | |
201 | {"more", no_argument, NULL, 'm'}, | |
202 | {"timestamp", no_argument, NULL, OPT_TIMESTAMP}, | |
203 | {"sort", optional_argument, NULL, OPT_SORT}, | |
204 | {"rsort", optional_argument, NULL, OPT_RSORT}, | |
205 | {"unixctl", required_argument, NULL, OPT_UNIXCTL}, | |
206 | {"help", no_argument, NULL, 'h'}, | |
207 | {"option", no_argument, NULL, 'o'}, | |
208 | {"bundle", no_argument, NULL, OPT_BUNDLE}, | |
209 | {"color", optional_argument, NULL, OPT_COLOR}, | |
210 | {"may-create", no_argument, NULL, OPT_MAY_CREATE}, | |
211 | {"read-only", no_argument, NULL, OPT_READ_ONLY}, | |
212 | DAEMON_LONG_OPTIONS, | |
213 | OFP_VERSION_LONG_OPTIONS, | |
214 | VLOG_LONG_OPTIONS, | |
215 | STREAM_SSL_LONG_OPTIONS, | |
216 | {NULL, 0, NULL, 0}, | |
217 | }; | |
218 | char *short_options = ovs_cmdl_long_options_to_short_options(long_options); | |
219 | uint32_t versions; | |
220 | enum ofputil_protocol version_protocols; | |
221 | ||
222 | /* For now, ovs-ofctl only enables OpenFlow 1.0 by default. This is | |
223 | * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow | |
224 | * requests, but those requests have subtly different semantics in | |
225 | * different OpenFlow versions. For example: | |
226 | * | |
227 | * - In OpenFlow 1.0, a "mod-flow" operation that does not find any | |
228 | * existing flow to modify adds a new flow. | |
229 | * | |
230 | * - In OpenFlow 1.1, a "mod-flow" operation that does not find any | |
231 | * existing flow to modify adds a new flow, but only if the mod-flow | |
232 | * did not match on the flow cookie. | |
233 | * | |
234 | * - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a | |
235 | * new flow. | |
236 | */ | |
237 | set_allowed_ofp_versions("OpenFlow10"); | |
238 | ||
239 | for (;;) { | |
240 | unsigned long int timeout; | |
241 | int c; | |
242 | ||
243 | c = getopt_long(argc, argv, short_options, long_options, NULL); | |
244 | if (c == -1) { | |
245 | break; | |
246 | } | |
247 | ||
248 | switch (c) { | |
249 | case 't': | |
250 | timeout = strtoul(optarg, NULL, 10); | |
251 | if (timeout <= 0) { | |
252 | ovs_fatal(0, "value %s on -t or --timeout is not at least 1", | |
253 | optarg); | |
254 | } else { | |
255 | time_alarm(timeout); | |
256 | } | |
257 | break; | |
258 | ||
259 | case 'F': | |
260 | allowed_protocols = ofputil_protocols_from_string(optarg); | |
261 | if (!allowed_protocols) { | |
262 | ovs_fatal(0, "%s: invalid flow format(s)", optarg); | |
263 | } | |
264 | break; | |
265 | ||
266 | case 'P': | |
267 | preferred_packet_in_format = | |
268 | ofputil_packet_in_format_from_string(optarg); | |
269 | if (preferred_packet_in_format < 0) { | |
270 | ovs_fatal(0, "unknown packet-in format `%s'", optarg); | |
271 | } | |
272 | break; | |
273 | ||
274 | case 'm': | |
275 | verbosity++; | |
276 | break; | |
277 | ||
278 | case 'h': | |
279 | usage(); | |
280 | ||
281 | case 'o': | |
282 | ovs_cmdl_print_options(long_options); | |
283 | exit(EXIT_SUCCESS); | |
284 | ||
285 | case OPT_BUNDLE: | |
286 | bundle = true; | |
287 | break; | |
288 | ||
289 | case OPT_STRICT: | |
290 | strict = true; | |
291 | break; | |
292 | ||
293 | case OPT_READ_ONLY: | |
294 | read_only = true; | |
295 | break; | |
296 | ||
297 | case OPT_READD: | |
298 | readd = true; | |
299 | break; | |
300 | ||
301 | case OPT_TIMESTAMP: | |
302 | timestamp = true; | |
303 | break; | |
304 | ||
305 | case OPT_SORT: | |
306 | add_sort_criterion(SORT_ASC, optarg); | |
307 | break; | |
308 | ||
309 | case OPT_RSORT: | |
310 | add_sort_criterion(SORT_DESC, optarg); | |
311 | break; | |
312 | ||
313 | case OPT_UNIXCTL: | |
314 | unixctl_path = optarg; | |
315 | break; | |
316 | ||
317 | case OPT_COLOR: | |
318 | if (optarg) { | |
319 | if (!strcasecmp(optarg, "always") | |
320 | || !strcasecmp(optarg, "yes") | |
321 | || !strcasecmp(optarg, "force")) { | |
322 | enable_color = true; | |
323 | } else if (!strcasecmp(optarg, "never") | |
324 | || !strcasecmp(optarg, "no") | |
325 | || !strcasecmp(optarg, "none")) { | |
326 | enable_color = false; | |
327 | } else if (!strcasecmp(optarg, "auto") | |
328 | || !strcasecmp(optarg, "tty") | |
329 | || !strcasecmp(optarg, "if-tty")) { | |
330 | /* Determine whether we need colors, i.e. whether standard | |
331 | * output is a tty. */ | |
332 | enable_color = is_stdout_a_tty(); | |
333 | } else { | |
334 | ovs_fatal(0, "incorrect value `%s' for --color", optarg); | |
335 | } | |
336 | } else { | |
337 | enable_color = is_stdout_a_tty(); | |
338 | } | |
339 | break; | |
340 | ||
341 | case OPT_MAY_CREATE: | |
342 | may_create = true; | |
343 | break; | |
344 | ||
345 | DAEMON_OPTION_HANDLERS | |
346 | OFP_VERSION_OPTION_HANDLERS | |
347 | VLOG_OPTION_HANDLERS | |
348 | STREAM_SSL_OPTION_HANDLERS | |
349 | ||
350 | case '?': | |
351 | exit(EXIT_FAILURE); | |
352 | ||
353 | default: | |
354 | abort(); | |
355 | } | |
356 | } | |
357 | ||
358 | if (n_criteria) { | |
359 | /* Always do a final sort pass based on priority. */ | |
360 | add_sort_criterion(SORT_DESC, "priority"); | |
361 | } | |
362 | ||
363 | free(short_options); | |
364 | ||
365 | /* Implicit OpenFlow 1.4 with the '--bundle' option. */ | |
366 | if (bundle && !(get_allowed_ofp_versions() & | |
367 | ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP))) { | |
368 | /* Add implicit allowance for OpenFlow 1.4. */ | |
369 | add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( | |
370 | OFPUTIL_P_OF14_OXM)); | |
371 | /* Remove all versions that do not support bundles. */ | |
372 | mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( | |
373 | OFPUTIL_P_OF13_UP)); | |
374 | } | |
375 | versions = get_allowed_ofp_versions(); | |
376 | version_protocols = ofputil_protocols_from_version_bitmap(versions); | |
377 | if (!(allowed_protocols & version_protocols)) { | |
378 | char *protocols = ofputil_protocols_to_string(allowed_protocols); | |
379 | struct ds version_s = DS_EMPTY_INITIALIZER; | |
380 | ||
381 | ofputil_format_version_bitmap_names(&version_s, versions); | |
382 | ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports " | |
383 | "any of the enabled flow formats (%s). (Use -O to enable " | |
384 | "additional OpenFlow versions or -F to enable additional " | |
385 | "flow formats.)", ds_cstr(&version_s), protocols); | |
386 | } | |
387 | allowed_protocols &= version_protocols; | |
388 | mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( | |
389 | allowed_protocols)); | |
390 | ||
391 | colors_init(enable_color); | |
392 | } | |
393 | ||
394 | static void | |
395 | usage(void) | |
396 | { | |
397 | printf("%s: OpenFlow switch management utility\n" | |
398 | "usage: %s [OPTIONS] COMMAND [ARG...]\n" | |
399 | "\nFor OpenFlow switches:\n" | |
400 | " show SWITCH show OpenFlow information\n" | |
401 | " dump-desc SWITCH print switch description\n" | |
402 | " dump-tables SWITCH print table stats\n" | |
403 | " dump-table-features SWITCH print table features\n" | |
404 | " dump-table-desc SWITCH print table description (OF1.4+)\n" | |
405 | " mod-port SWITCH IFACE ACT modify port behavior\n" | |
406 | " mod-table SWITCH MOD modify flow table behavior\n" | |
407 | " OF1.1/1.2 MOD: controller, continue, drop\n" | |
408 | " OF1.4+ MOD: evict, noevict, vacancy:low,high, novacancy\n" | |
409 | " get-frags SWITCH print fragment handling behavior\n" | |
410 | " set-frags SWITCH FRAG_MODE set fragment handling behavior\n" | |
411 | " FRAG_MODE: normal, drop, reassemble, nx-match\n" | |
412 | " dump-ports SWITCH [PORT] print port statistics\n" | |
413 | " dump-ports-desc SWITCH [PORT] print port descriptions\n" | |
414 | " dump-flows SWITCH print all flow entries\n" | |
415 | " dump-flows SWITCH FLOW print matching FLOWs\n" | |
416 | " dump-aggregate SWITCH print aggregate flow statistics\n" | |
417 | " dump-aggregate SWITCH FLOW print aggregate stats for FLOWs\n" | |
418 | " queue-stats SWITCH [PORT [QUEUE]] dump queue stats\n" | |
419 | " add-flow SWITCH FLOW add flow described by FLOW\n" | |
420 | " add-flows SWITCH FILE add flows from FILE\n" | |
421 | " mod-flows SWITCH FLOW modify actions of matching FLOWs\n" | |
422 | " del-flows SWITCH [FLOW] delete matching FLOWs\n" | |
423 | " replace-flows SWITCH FILE replace flows with those in FILE\n" | |
424 | " diff-flows SOURCE1 SOURCE2 compare flows from two sources\n" | |
425 | " packet-out SWITCH IN_PORT ACTIONS PACKET...\n" | |
426 | " execute ACTIONS on PACKET\n" | |
427 | " monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n" | |
428 | " print packets received from SWITCH\n" | |
429 | " snoop SWITCH snoop on SWITCH and its controller\n" | |
430 | " add-group SWITCH GROUP add group described by GROUP\n" | |
431 | " add-groups SWITCH FILE add group from FILE\n" | |
432 | " [--may-create] mod-group SWITCH GROUP modify specific group\n" | |
433 | " del-groups SWITCH [GROUP] delete matching GROUPs\n" | |
434 | " insert-buckets SWITCH [GROUP] add buckets to GROUP\n" | |
435 | " remove-buckets SWITCH [GROUP] remove buckets from GROUP\n" | |
436 | " dump-group-features SWITCH print group features\n" | |
437 | " dump-groups SWITCH [GROUP] print group description\n" | |
438 | " dump-group-stats SWITCH [GROUP] print group statistics\n" | |
439 | " queue-get-config SWITCH [PORT] print queue config for PORT\n" | |
440 | " add-meter SWITCH METER add meter described by METER\n" | |
441 | " mod-meter SWITCH METER modify specific METER\n" | |
442 | " del-meter SWITCH METER delete METER\n" | |
443 | " del-meters SWITCH delete all meters\n" | |
444 | " dump-meter SWITCH METER print METER configuration\n" | |
445 | " dump-meters SWITCH print all meter configuration\n" | |
446 | " meter-stats SWITCH [METER] print meter statistics\n" | |
447 | " meter-features SWITCH print meter features\n" | |
448 | " add-tlv-map SWITCH MAP add TLV option MAPpings\n" | |
449 | " del-tlv-map SWITCH [MAP] delete TLV option MAPpings\n" | |
450 | " dump-tlv-map SWITCH print TLV option mappings\n" | |
451 | " dump-ipfix-bridge SWITCH print ipfix stats of bridge\n" | |
452 | " dump-ipfix-flow SWITCH print flow ipfix of a bridge\n" | |
453 | "\nFor OpenFlow switches and controllers:\n" | |
454 | " probe TARGET probe whether TARGET is up\n" | |
455 | " ping TARGET [N] latency of N-byte echos\n" | |
456 | " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n" | |
457 | "SWITCH or TARGET is an active OpenFlow connection method.\n" | |
458 | "\nOther commands:\n" | |
459 | " ofp-parse FILE print messages read from FILE\n" | |
460 | " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n", | |
461 | program_name, program_name); | |
462 | vconn_usage(true, false, false); | |
463 | daemon_usage(); | |
464 | ofp_version_usage(); | |
465 | vlog_usage(); | |
466 | printf("\nOther options:\n" | |
467 | " --strict use strict match for flow commands\n" | |
468 | " --read-only do not execute read/write commands\n" | |
469 | " --readd replace flows that haven't changed\n" | |
470 | " -F, --flow-format=FORMAT force particular flow format\n" | |
471 | " -P, --packet-in-format=FRMT force particular packet in format\n" | |
472 | " -m, --more be more verbose printing OpenFlow\n" | |
473 | " --timestamp (monitor, snoop) print timestamps\n" | |
474 | " -t, --timeout=SECS give up after SECS seconds\n" | |
475 | " --sort[=field] sort in ascending order\n" | |
476 | " --rsort[=field] sort in descending order\n" | |
477 | " --unixctl=SOCKET set control socket name\n" | |
478 | " --color[=always|never|auto] control use of color in output\n" | |
479 | " -h, --help display this help message\n" | |
480 | " -V, --version display version information\n"); | |
481 | exit(EXIT_SUCCESS); | |
482 | } | |
483 | ||
484 | static void | |
485 | ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
486 | const char *argv[] OVS_UNUSED, void *exiting_) | |
487 | { | |
488 | bool *exiting = exiting_; | |
489 | *exiting = true; | |
490 | unixctl_command_reply(conn, NULL); | |
491 | } | |
492 | ||
493 | static void run(int retval, const char *message, ...) | |
494 | OVS_PRINTF_FORMAT(2, 3); | |
495 | ||
496 | static void | |
497 | run(int retval, const char *message, ...) | |
498 | { | |
499 | if (retval) { | |
500 | va_list args; | |
501 | ||
502 | va_start(args, message); | |
503 | ovs_fatal_valist(retval, message, args); | |
504 | } | |
505 | } | |
506 | \f | |
507 | /* Generic commands. */ | |
508 | ||
509 | static int | |
510 | open_vconn_socket(const char *name, struct vconn **vconnp) | |
511 | { | |
512 | char *vconn_name = xasprintf("unix:%s", name); | |
513 | int error; | |
514 | ||
515 | error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT, | |
516 | vconnp); | |
517 | if (error && error != ENOENT) { | |
518 | ovs_fatal(0, "%s: failed to open socket (%s)", name, | |
519 | ovs_strerror(error)); | |
520 | } | |
521 | free(vconn_name); | |
522 | ||
523 | return error; | |
524 | } | |
525 | ||
526 | enum open_target { MGMT, SNOOP }; | |
527 | ||
528 | static enum ofputil_protocol | |
529 | open_vconn__(const char *name, enum open_target target, | |
530 | struct vconn **vconnp) | |
531 | { | |
532 | const char *suffix = target == MGMT ? "mgmt" : "snoop"; | |
533 | char *datapath_name, *datapath_type, *socket_name; | |
534 | enum ofputil_protocol protocol; | |
535 | char *bridge_path; | |
536 | int ofp_version; | |
537 | int error; | |
538 | ||
539 | bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix); | |
540 | ||
541 | ofproto_parse_name(name, &datapath_name, &datapath_type); | |
542 | socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix); | |
543 | free(datapath_name); | |
544 | free(datapath_type); | |
545 | ||
546 | if (strchr(name, ':')) { | |
547 | run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp), | |
548 | "connecting to %s", name); | |
549 | } else if (!open_vconn_socket(name, vconnp)) { | |
550 | /* Fall Through. */ | |
551 | } else if (!open_vconn_socket(bridge_path, vconnp)) { | |
552 | /* Fall Through. */ | |
553 | } else if (!open_vconn_socket(socket_name, vconnp)) { | |
554 | /* Fall Through. */ | |
555 | } else { | |
556 | ovs_fatal(0, "%s is not a bridge or a socket", name); | |
557 | } | |
558 | ||
559 | if (target == SNOOP) { | |
560 | vconn_set_recv_any_version(*vconnp); | |
561 | } | |
562 | ||
563 | free(bridge_path); | |
564 | free(socket_name); | |
565 | ||
566 | VLOG_DBG("connecting to %s", vconn_get_name(*vconnp)); | |
567 | error = vconn_connect_block(*vconnp); | |
568 | if (error) { | |
569 | ovs_fatal(0, "%s: failed to connect to socket (%s)", name, | |
570 | ovs_strerror(error)); | |
571 | } | |
572 | ||
573 | ofp_version = vconn_get_version(*vconnp); | |
574 | protocol = ofputil_protocol_from_ofp_version(ofp_version); | |
575 | if (!protocol) { | |
576 | ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x", | |
577 | name, ofp_version); | |
578 | } | |
579 | return protocol; | |
580 | } | |
581 | ||
582 | static enum ofputil_protocol | |
583 | open_vconn(const char *name, struct vconn **vconnp) | |
584 | { | |
585 | return open_vconn__(name, MGMT, vconnp); | |
586 | } | |
587 | ||
588 | static void | |
589 | send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer) | |
590 | { | |
591 | run(vconn_send_block(vconn, buffer), "failed to send packet to switch"); | |
592 | } | |
593 | ||
594 | static void | |
595 | dump_transaction(struct vconn *vconn, struct ofpbuf *request) | |
596 | { | |
597 | const struct ofp_header *oh = request->data; | |
598 | if (ofpmsg_is_stat_request(oh)) { | |
599 | ovs_be32 send_xid = oh->xid; | |
600 | enum ofpraw request_raw; | |
601 | enum ofpraw reply_raw; | |
602 | bool done = false; | |
603 | ||
604 | ofpraw_decode_partial(&request_raw, request->data, request->size); | |
605 | reply_raw = ofpraw_stats_request_to_reply(request_raw, oh->version); | |
606 | ||
607 | send_openflow_buffer(vconn, request); | |
608 | while (!done) { | |
609 | ovs_be32 recv_xid; | |
610 | struct ofpbuf *reply; | |
611 | ||
612 | run(vconn_recv_block(vconn, &reply), | |
613 | "OpenFlow packet receive failed"); | |
614 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
615 | if (send_xid == recv_xid) { | |
616 | enum ofpraw raw; | |
617 | ||
618 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
619 | ||
620 | ofpraw_decode(&raw, reply->data); | |
621 | if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) { | |
622 | done = true; | |
623 | } else if (raw == reply_raw) { | |
624 | done = !ofpmp_more(reply->data); | |
625 | } else { | |
626 | ovs_fatal(0, "received bad reply: %s", | |
627 | ofp_to_string(reply->data, reply->size, | |
628 | verbosity + 1)); | |
629 | } | |
630 | } else { | |
631 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
632 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
633 | } | |
634 | ofpbuf_delete(reply); | |
635 | } | |
636 | } else { | |
637 | struct ofpbuf *reply; | |
638 | ||
639 | run(vconn_transact(vconn, request, &reply), "talking to %s", | |
640 | vconn_get_name(vconn)); | |
641 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
642 | ofpbuf_delete(reply); | |
643 | } | |
644 | } | |
645 | ||
646 | static void | |
647 | dump_trivial_transaction(const char *vconn_name, enum ofpraw raw) | |
648 | { | |
649 | struct ofpbuf *request; | |
650 | struct vconn *vconn; | |
651 | ||
652 | open_vconn(vconn_name, &vconn); | |
653 | request = ofpraw_alloc(raw, vconn_get_version(vconn), 0); | |
654 | dump_transaction(vconn, request); | |
655 | vconn_close(vconn); | |
656 | } | |
657 | ||
658 | /* Sends all of the 'requests', which should be requests that only have replies | |
659 | * if an error occurs, and waits for them to succeed or fail. If an error does | |
660 | * occur, prints it and exits with an error. | |
661 | * | |
662 | * Destroys all of the 'requests'. */ | |
663 | static void | |
664 | transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests) | |
665 | { | |
666 | struct ofpbuf *reply; | |
667 | ||
668 | run(vconn_transact_multiple_noreply(vconn, requests, &reply), | |
669 | "talking to %s", vconn_get_name(vconn)); | |
670 | if (reply) { | |
671 | ofp_print(stderr, reply->data, reply->size, verbosity + 2); | |
672 | exit(1); | |
673 | } | |
674 | ofpbuf_delete(reply); | |
675 | } | |
676 | ||
677 | /* Frees the error messages as they are printed. */ | |
678 | static void | |
679 | bundle_print_errors(struct ovs_list *errors, struct ovs_list *requests) | |
680 | { | |
681 | struct vconn_bundle_error *error, *next; | |
682 | struct ofpbuf *bmsg; | |
683 | ||
684 | INIT_CONTAINER(bmsg, requests, list_node); | |
685 | ||
686 | LIST_FOR_EACH_SAFE (error, next, list_node, errors) { | |
687 | enum ofperr ofperr; | |
688 | struct ofpbuf payload; | |
689 | ||
690 | ofperr = ofperr_decode_msg(&error->ofp_msg, &payload); | |
691 | if (!ofperr) { | |
692 | fprintf(stderr, "***decode error***"); | |
693 | } else { | |
694 | /* Default to the likely truncated message. */ | |
695 | const struct ofp_header *ofp_msg = payload.data; | |
696 | size_t msg_len = payload.size; | |
697 | ||
698 | /* Find the failing message from the requests list to be able to | |
699 | * dump the whole message. We assume the errors are returned in | |
700 | * the same order as in which the messages are sent to get O(n) | |
701 | * rather than O(n^2) processing here. If this heuristics fails we | |
702 | * may print the truncated hexdumps instead. */ | |
703 | LIST_FOR_EACH_CONTINUE (bmsg, list_node, requests) { | |
704 | const struct ofp_header *oh = bmsg->data; | |
705 | ||
706 | if (oh->xid == error->ofp_msg.xid) { | |
707 | ofp_msg = oh; | |
708 | msg_len = bmsg->size; | |
709 | break; | |
710 | } | |
711 | } | |
712 | fprintf(stderr, "Error %s for: ", ofperr_get_name(ofperr)); | |
713 | ofp_print(stderr, ofp_msg, msg_len, verbosity + 1); | |
714 | } | |
715 | free(error); | |
716 | } | |
717 | fflush(stderr); | |
718 | } | |
719 | ||
720 | static void | |
721 | bundle_transact(struct vconn *vconn, struct ovs_list *requests, uint16_t flags) | |
722 | { | |
723 | struct ovs_list errors; | |
724 | int retval = vconn_bundle_transact(vconn, requests, flags, &errors); | |
725 | ||
726 | bundle_print_errors(&errors, requests); | |
727 | ||
728 | if (retval) { | |
729 | ovs_fatal(retval, "talking to %s", vconn_get_name(vconn)); | |
730 | } | |
731 | } | |
732 | ||
733 | /* Sends 'request', which should be a request that only has a reply if an error | |
734 | * occurs, and waits for it to succeed or fail. If an error does occur, prints | |
735 | * it and exits with an error. | |
736 | * | |
737 | * Destroys 'request'. */ | |
738 | static void | |
739 | transact_noreply(struct vconn *vconn, struct ofpbuf *request) | |
740 | { | |
741 | struct ovs_list requests; | |
742 | ||
743 | ovs_list_init(&requests); | |
744 | ovs_list_push_back(&requests, &request->list_node); | |
745 | transact_multiple_noreply(vconn, &requests); | |
746 | } | |
747 | ||
748 | static void | |
749 | fetch_switch_config(struct vconn *vconn, struct ofputil_switch_config *config) | |
750 | { | |
751 | struct ofpbuf *request; | |
752 | struct ofpbuf *reply; | |
753 | enum ofptype type; | |
754 | ||
755 | request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST, | |
756 | vconn_get_version(vconn), 0); | |
757 | run(vconn_transact(vconn, request, &reply), | |
758 | "talking to %s", vconn_get_name(vconn)); | |
759 | ||
760 | if (ofptype_decode(&type, reply->data) | |
761 | || type != OFPTYPE_GET_CONFIG_REPLY) { | |
762 | ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn)); | |
763 | } | |
764 | ofputil_decode_get_config_reply(reply->data, config); | |
765 | ofpbuf_delete(reply); | |
766 | } | |
767 | ||
768 | static void | |
769 | set_switch_config(struct vconn *vconn, | |
770 | const struct ofputil_switch_config *config) | |
771 | { | |
772 | enum ofp_version version = vconn_get_version(vconn); | |
773 | transact_noreply(vconn, ofputil_encode_set_config(config, version)); | |
774 | } | |
775 | ||
776 | static void | |
777 | ofctl_show(struct ovs_cmdl_context *ctx) | |
778 | { | |
779 | const char *vconn_name = ctx->argv[1]; | |
780 | enum ofp_version version; | |
781 | struct vconn *vconn; | |
782 | struct ofpbuf *request; | |
783 | struct ofpbuf *reply; | |
784 | bool has_ports; | |
785 | ||
786 | open_vconn(vconn_name, &vconn); | |
787 | version = vconn_get_version(vconn); | |
788 | request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0); | |
789 | run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name); | |
790 | ||
791 | has_ports = ofputil_switch_features_has_ports(reply); | |
792 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
793 | ofpbuf_delete(reply); | |
794 | ||
795 | if (!has_ports) { | |
796 | request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY); | |
797 | dump_transaction(vconn, request); | |
798 | } | |
799 | dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST); | |
800 | vconn_close(vconn); | |
801 | } | |
802 | ||
803 | static void | |
804 | ofctl_dump_desc(struct ovs_cmdl_context *ctx) | |
805 | { | |
806 | dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST); | |
807 | } | |
808 | ||
809 | static void | |
810 | ofctl_dump_tables(struct ovs_cmdl_context *ctx) | |
811 | { | |
812 | dump_trivial_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST); | |
813 | } | |
814 | ||
815 | static void | |
816 | ofctl_dump_table_features(struct ovs_cmdl_context *ctx) | |
817 | { | |
818 | struct ofpbuf *request; | |
819 | struct vconn *vconn; | |
820 | ||
821 | open_vconn(ctx->argv[1], &vconn); | |
822 | request = ofputil_encode_table_features_request(vconn_get_version(vconn)); | |
823 | ||
824 | /* The following is similar to dump_trivial_transaction(), but it | |
825 | * maintains the previous 'ofputil_table_features' from one stats reply | |
826 | * message to the next, which allows duplication to be eliminated in the | |
827 | * output across messages. Otherwise the output is much larger and harder | |
828 | * to read, because only 17 or so ofputil_table_features elements fit in a | |
829 | * single 64 kB OpenFlow message and therefore you get a ton of repetition | |
830 | * (every 17th element is printed in full instead of abbreviated). */ | |
831 | ||
832 | const struct ofp_header *request_oh = request->data; | |
833 | ovs_be32 send_xid = request_oh->xid; | |
834 | bool done = false; | |
835 | ||
836 | struct ofputil_table_features prev; | |
837 | int n = 0; | |
838 | ||
839 | send_openflow_buffer(vconn, request); | |
840 | while (!done) { | |
841 | ovs_be32 recv_xid; | |
842 | struct ofpbuf *reply; | |
843 | ||
844 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
845 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
846 | if (send_xid == recv_xid) { | |
847 | enum ofptype type; | |
848 | enum ofperr error; | |
849 | error = ofptype_decode(&type, reply->data); | |
850 | if (error) { | |
851 | ovs_fatal(0, "decode error: %s", ofperr_get_name(error)); | |
852 | } else if (type == OFPTYPE_ERROR) { | |
853 | ofp_print(stdout, reply->data, reply->size, verbosity + 1); | |
854 | done = true; | |
855 | } else if (type == OFPTYPE_TABLE_FEATURES_STATS_REPLY) { | |
856 | done = !ofpmp_more(reply->data); | |
857 | for (;;) { | |
858 | struct ofputil_table_features tf; | |
859 | int retval; | |
860 | ||
861 | retval = ofputil_decode_table_features(reply, &tf, true); | |
862 | if (retval) { | |
863 | if (retval != EOF) { | |
864 | ovs_fatal(0, "decode error: %s", | |
865 | ofperr_get_name(retval)); | |
866 | } | |
867 | break; | |
868 | } | |
869 | ||
870 | struct ds s = DS_EMPTY_INITIALIZER; | |
871 | ofp_print_table_features(&s, &tf, n ? &prev : NULL, | |
872 | NULL, NULL); | |
873 | puts(ds_cstr(&s)); | |
874 | ds_destroy(&s); | |
875 | ||
876 | prev = tf; | |
877 | n++; | |
878 | } | |
879 | } else { | |
880 | ovs_fatal(0, "received bad reply: %s", | |
881 | ofp_to_string(reply->data, reply->size, | |
882 | verbosity + 1)); | |
883 | } | |
884 | } else { | |
885 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
886 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
887 | } | |
888 | ofpbuf_delete(reply); | |
889 | } | |
890 | ||
891 | vconn_close(vconn); | |
892 | } | |
893 | ||
894 | static void | |
895 | ofctl_dump_table_desc(struct ovs_cmdl_context *ctx) | |
896 | { | |
897 | struct ofpbuf *request; | |
898 | struct vconn *vconn; | |
899 | ||
900 | open_vconn(ctx->argv[1], &vconn); | |
901 | request = ofputil_encode_table_desc_request(vconn_get_version(vconn)); | |
902 | if (request) { | |
903 | dump_transaction(vconn, request); | |
904 | } | |
905 | ||
906 | vconn_close(vconn); | |
907 | } | |
908 | ||
909 | ||
910 | static bool | |
911 | str_to_ofp(const char *s, ofp_port_t *ofp_port) | |
912 | { | |
913 | bool ret; | |
914 | uint32_t port_; | |
915 | ||
916 | ret = str_to_uint(s, 10, &port_); | |
917 | *ofp_port = u16_to_ofp(port_); | |
918 | return ret; | |
919 | } | |
920 | ||
921 | struct port_iterator { | |
922 | struct vconn *vconn; | |
923 | ||
924 | enum { PI_FEATURES, PI_PORT_DESC } variant; | |
925 | struct ofpbuf *reply; | |
926 | ovs_be32 send_xid; | |
927 | bool more; | |
928 | }; | |
929 | ||
930 | static void | |
931 | port_iterator_fetch_port_desc(struct port_iterator *pi) | |
932 | { | |
933 | pi->variant = PI_PORT_DESC; | |
934 | pi->more = true; | |
935 | ||
936 | struct ofpbuf *rq = ofputil_encode_port_desc_stats_request( | |
937 | vconn_get_version(pi->vconn), OFPP_ANY); | |
938 | pi->send_xid = ((struct ofp_header *) rq->data)->xid; | |
939 | send_openflow_buffer(pi->vconn, rq); | |
940 | } | |
941 | ||
942 | static void | |
943 | port_iterator_fetch_features(struct port_iterator *pi) | |
944 | { | |
945 | pi->variant = PI_FEATURES; | |
946 | ||
947 | /* Fetch the switch's ofp_switch_features. */ | |
948 | enum ofp_version version = vconn_get_version(pi->vconn); | |
949 | struct ofpbuf *rq = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0); | |
950 | run(vconn_transact(pi->vconn, rq, &pi->reply), | |
951 | "talking to %s", vconn_get_name(pi->vconn)); | |
952 | ||
953 | enum ofptype type; | |
954 | if (ofptype_decode(&type, pi->reply->data) | |
955 | || type != OFPTYPE_FEATURES_REPLY) { | |
956 | ovs_fatal(0, "%s: received bad features reply", | |
957 | vconn_get_name(pi->vconn)); | |
958 | } | |
959 | if (!ofputil_switch_features_has_ports(pi->reply)) { | |
960 | /* The switch features reply does not contain a complete list of ports. | |
961 | * Probably, there are more ports than will fit into a single 64 kB | |
962 | * OpenFlow message. Use OFPST_PORT_DESC to get a complete list of | |
963 | * ports. */ | |
964 | ofpbuf_delete(pi->reply); | |
965 | pi->reply = NULL; | |
966 | port_iterator_fetch_port_desc(pi); | |
967 | return; | |
968 | } | |
969 | ||
970 | struct ofputil_switch_features features; | |
971 | enum ofperr error = ofputil_pull_switch_features(pi->reply, &features); | |
972 | if (error) { | |
973 | ovs_fatal(0, "%s: failed to decode features reply (%s)", | |
974 | vconn_get_name(pi->vconn), ofperr_to_string(error)); | |
975 | } | |
976 | } | |
977 | ||
978 | /* Initializes 'pi' to prepare for iterating through all of the ports on the | |
979 | * OpenFlow switch to which 'vconn' is connected. | |
980 | * | |
981 | * During iteration, the client should not make other use of 'vconn', because | |
982 | * that can cause other messages to be interleaved with the replies used by the | |
983 | * iterator and thus some ports may be missed or a hang can occur. */ | |
984 | static void | |
985 | port_iterator_init(struct port_iterator *pi, struct vconn *vconn) | |
986 | { | |
987 | memset(pi, 0, sizeof *pi); | |
988 | pi->vconn = vconn; | |
989 | if (vconn_get_version(vconn) < OFP13_VERSION) { | |
990 | port_iterator_fetch_features(pi); | |
991 | } else { | |
992 | port_iterator_fetch_port_desc(pi); | |
993 | } | |
994 | } | |
995 | ||
996 | /* Obtains the next port from 'pi'. On success, initializes '*pp' with the | |
997 | * port's details and returns true, otherwise (if all the ports have already | |
998 | * been seen), returns false. */ | |
999 | static bool | |
1000 | port_iterator_next(struct port_iterator *pi, struct ofputil_phy_port *pp) | |
1001 | { | |
1002 | for (;;) { | |
1003 | if (pi->reply) { | |
1004 | int retval = ofputil_pull_phy_port(vconn_get_version(pi->vconn), | |
1005 | pi->reply, pp); | |
1006 | if (!retval) { | |
1007 | return true; | |
1008 | } else if (retval != EOF) { | |
1009 | ovs_fatal(0, "received bad reply: %s", | |
1010 | ofp_to_string(pi->reply->data, pi->reply->size, | |
1011 | verbosity + 1)); | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | if (pi->variant == PI_FEATURES || !pi->more) { | |
1016 | return false; | |
1017 | } | |
1018 | ||
1019 | ovs_be32 recv_xid; | |
1020 | do { | |
1021 | ofpbuf_delete(pi->reply); | |
1022 | run(vconn_recv_block(pi->vconn, &pi->reply), | |
1023 | "OpenFlow receive failed"); | |
1024 | recv_xid = ((struct ofp_header *) pi->reply->data)->xid; | |
1025 | } while (pi->send_xid != recv_xid); | |
1026 | ||
1027 | struct ofp_header *oh = pi->reply->data; | |
1028 | enum ofptype type; | |
1029 | if (ofptype_pull(&type, pi->reply) | |
1030 | || type != OFPTYPE_PORT_DESC_STATS_REPLY) { | |
1031 | ovs_fatal(0, "received bad reply: %s", | |
1032 | ofp_to_string(pi->reply->data, pi->reply->size, | |
1033 | verbosity + 1)); | |
1034 | } | |
1035 | ||
1036 | pi->more = (ofpmp_flags(oh) & OFPSF_REPLY_MORE) != 0; | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | /* Destroys iterator 'pi'. */ | |
1041 | static void | |
1042 | port_iterator_destroy(struct port_iterator *pi) | |
1043 | { | |
1044 | if (pi) { | |
1045 | while (pi->variant == PI_PORT_DESC && pi->more) { | |
1046 | /* Drain vconn's queue of any other replies for this request. */ | |
1047 | struct ofputil_phy_port pp; | |
1048 | port_iterator_next(pi, &pp); | |
1049 | } | |
1050 | ||
1051 | ofpbuf_delete(pi->reply); | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | /* Opens a connection to 'vconn_name', fetches the port structure for | |
1056 | * 'port_name' (which may be a port name or number), and copies it into | |
1057 | * '*pp'. */ | |
1058 | static void | |
1059 | fetch_ofputil_phy_port(const char *vconn_name, const char *port_name, | |
1060 | struct ofputil_phy_port *pp) | |
1061 | { | |
1062 | struct vconn *vconn; | |
1063 | ofp_port_t port_no; | |
1064 | bool found = false; | |
1065 | ||
1066 | /* Try to interpret the argument as a port number. */ | |
1067 | if (!str_to_ofp(port_name, &port_no)) { | |
1068 | port_no = OFPP_NONE; | |
1069 | } | |
1070 | ||
1071 | /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the | |
1072 | * OFPT_FEATURES_REPLY message. OpenFlow 1.3 and later versions put it | |
1073 | * into the OFPST_PORT_DESC reply. Try it the correct way. */ | |
1074 | open_vconn(vconn_name, &vconn); | |
1075 | struct port_iterator pi; | |
1076 | for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, pp); ) { | |
1077 | if (port_no != OFPP_NONE | |
1078 | ? port_no == pp->port_no | |
1079 | : !strcmp(pp->name, port_name)) { | |
1080 | found = true; | |
1081 | break; | |
1082 | } | |
1083 | } | |
1084 | port_iterator_destroy(&pi); | |
1085 | vconn_close(vconn); | |
1086 | ||
1087 | if (!found) { | |
1088 | ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name); | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | /* Returns the port number corresponding to 'port_name' (which may be a port | |
1093 | * name or number) within the switch 'vconn_name'. */ | |
1094 | static ofp_port_t | |
1095 | str_to_port_no(const char *vconn_name, const char *port_name) | |
1096 | { | |
1097 | ofp_port_t port_no; | |
1098 | ||
1099 | if (ofputil_port_from_string(port_name, &port_no)) { | |
1100 | return port_no; | |
1101 | } else { | |
1102 | struct ofputil_phy_port pp; | |
1103 | ||
1104 | fetch_ofputil_phy_port(vconn_name, port_name, &pp); | |
1105 | return pp.port_no; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | static bool | |
1110 | try_set_protocol(struct vconn *vconn, enum ofputil_protocol want, | |
1111 | enum ofputil_protocol *cur) | |
1112 | { | |
1113 | for (;;) { | |
1114 | struct ofpbuf *request, *reply; | |
1115 | enum ofputil_protocol next; | |
1116 | ||
1117 | request = ofputil_encode_set_protocol(*cur, want, &next); | |
1118 | if (!request) { | |
1119 | return *cur == want; | |
1120 | } | |
1121 | ||
1122 | run(vconn_transact_noreply(vconn, request, &reply), | |
1123 | "talking to %s", vconn_get_name(vconn)); | |
1124 | if (reply) { | |
1125 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
1126 | VLOG_DBG("%s: failed to set protocol, switch replied: %s", | |
1127 | vconn_get_name(vconn), s); | |
1128 | free(s); | |
1129 | ofpbuf_delete(reply); | |
1130 | return false; | |
1131 | } | |
1132 | ||
1133 | *cur = next; | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | static enum ofputil_protocol | |
1138 | set_protocol_for_flow_dump(struct vconn *vconn, | |
1139 | enum ofputil_protocol cur_protocol, | |
1140 | enum ofputil_protocol usable_protocols) | |
1141 | { | |
1142 | char *usable_s; | |
1143 | int i; | |
1144 | ||
1145 | for (i = 0; i < ofputil_n_flow_dump_protocols; i++) { | |
1146 | enum ofputil_protocol f = ofputil_flow_dump_protocols[i]; | |
1147 | if (f & usable_protocols & allowed_protocols | |
1148 | && try_set_protocol(vconn, f, &cur_protocol)) { | |
1149 | return f; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
1154 | if (usable_protocols & allowed_protocols) { | |
1155 | ovs_fatal(0, "switch does not support any of the usable flow " | |
1156 | "formats (%s)", usable_s); | |
1157 | } else { | |
1158 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); | |
1159 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
1160 | "allowed flow formats (%s)", usable_s, allowed_s); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | static struct vconn * | |
1165 | prepare_dump_flows(int argc, char *argv[], bool aggregate, | |
1166 | struct ofpbuf **requestp) | |
1167 | { | |
1168 | enum ofputil_protocol usable_protocols, protocol; | |
1169 | struct ofputil_flow_stats_request fsr; | |
1170 | struct vconn *vconn; | |
1171 | char *error; | |
1172 | ||
1173 | error = parse_ofp_flow_stats_request_str(&fsr, aggregate, | |
1174 | argc > 2 ? argv[2] : "", | |
1175 | &usable_protocols); | |
1176 | if (error) { | |
1177 | ovs_fatal(0, "%s", error); | |
1178 | } | |
1179 | ||
1180 | protocol = open_vconn(argv[1], &vconn); | |
1181 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
1182 | *requestp = ofputil_encode_flow_stats_request(&fsr, protocol); | |
1183 | return vconn; | |
1184 | } | |
1185 | ||
1186 | static void | |
1187 | ofctl_dump_flows__(int argc, char *argv[], bool aggregate) | |
1188 | { | |
1189 | struct ofpbuf *request; | |
1190 | struct vconn *vconn; | |
1191 | ||
1192 | vconn = prepare_dump_flows(argc, argv, aggregate, &request); | |
1193 | dump_transaction(vconn, request); | |
1194 | vconn_close(vconn); | |
1195 | } | |
1196 | ||
1197 | static void | |
1198 | get_match_field(const struct mf_field *field, const struct match *match, | |
1199 | union mf_value *value) | |
1200 | { | |
1201 | if (!match->tun_md.valid || (field->id < MFF_TUN_METADATA0 || | |
1202 | field->id >= MFF_TUN_METADATA0 + | |
1203 | TUN_METADATA_NUM_OPTS)) { | |
1204 | mf_get_value(field, &match->flow, value); | |
1205 | } else { | |
1206 | const struct tun_metadata_loc *loc = &match->tun_md.entry[field->id - | |
1207 | MFF_TUN_METADATA0].loc; | |
1208 | ||
1209 | /* Since we don't have a tunnel mapping table, extract the value | |
1210 | * from the locally allocated location in the match. */ | |
1211 | memset(value, 0, field->n_bytes - loc->len); | |
1212 | memcpy(value->tun_metadata + field->n_bytes - loc->len, | |
1213 | match->flow.tunnel.metadata.opts.u8 + loc->c.offset, loc->len); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | static int | |
1218 | compare_flows(const void *afs_, const void *bfs_) | |
1219 | { | |
1220 | const struct ofputil_flow_stats *afs = afs_; | |
1221 | const struct ofputil_flow_stats *bfs = bfs_; | |
1222 | const struct match *a = &afs->match; | |
1223 | const struct match *b = &bfs->match; | |
1224 | const struct sort_criterion *sc; | |
1225 | ||
1226 | for (sc = criteria; sc < &criteria[n_criteria]; sc++) { | |
1227 | const struct mf_field *f = sc->field; | |
1228 | int ret; | |
1229 | ||
1230 | if (!f) { | |
1231 | int a_pri = afs->priority; | |
1232 | int b_pri = bfs->priority; | |
1233 | ret = a_pri < b_pri ? -1 : a_pri > b_pri; | |
1234 | } else { | |
1235 | bool ina, inb; | |
1236 | ||
1237 | ina = mf_are_prereqs_ok(f, &a->flow, NULL) | |
1238 | && !mf_is_all_wild(f, &a->wc); | |
1239 | inb = mf_are_prereqs_ok(f, &b->flow, NULL) | |
1240 | && !mf_is_all_wild(f, &b->wc); | |
1241 | if (ina != inb) { | |
1242 | /* Skip the test for sc->order, so that missing fields always | |
1243 | * sort to the end whether we're sorting in ascending or | |
1244 | * descending order. */ | |
1245 | return ina ? -1 : 1; | |
1246 | } else { | |
1247 | union mf_value aval, bval; | |
1248 | ||
1249 | get_match_field(f, a, &aval); | |
1250 | get_match_field(f, b, &bval); | |
1251 | ret = memcmp(&aval, &bval, f->n_bytes); | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | if (ret) { | |
1256 | return sc->order == SORT_ASC ? ret : -ret; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | static void | |
1264 | ofctl_dump_flows(struct ovs_cmdl_context *ctx) | |
1265 | { | |
1266 | if (!n_criteria) { | |
1267 | ofctl_dump_flows__(ctx->argc, ctx->argv, false); | |
1268 | return; | |
1269 | } else { | |
1270 | struct ofputil_flow_stats *fses; | |
1271 | size_t n_fses, allocated_fses; | |
1272 | struct ofpbuf *request; | |
1273 | struct ofpbuf ofpacts; | |
1274 | struct ofpbuf *reply; | |
1275 | struct vconn *vconn; | |
1276 | ovs_be32 send_xid; | |
1277 | struct ds s; | |
1278 | size_t i; | |
1279 | ||
1280 | vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request); | |
1281 | send_xid = ((struct ofp_header *) request->data)->xid; | |
1282 | send_openflow_buffer(vconn, request); | |
1283 | ||
1284 | fses = NULL; | |
1285 | n_fses = allocated_fses = 0; | |
1286 | reply = NULL; | |
1287 | ofpbuf_init(&ofpacts, 0); | |
1288 | for (;;) { | |
1289 | struct ofputil_flow_stats *fs; | |
1290 | ||
1291 | if (n_fses >= allocated_fses) { | |
1292 | fses = x2nrealloc(fses, &allocated_fses, sizeof *fses); | |
1293 | } | |
1294 | ||
1295 | fs = &fses[n_fses]; | |
1296 | if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs, | |
1297 | &ofpacts)) { | |
1298 | break; | |
1299 | } | |
1300 | fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len); | |
1301 | n_fses++; | |
1302 | } | |
1303 | ofpbuf_uninit(&ofpacts); | |
1304 | ||
1305 | qsort(fses, n_fses, sizeof *fses, compare_flows); | |
1306 | ||
1307 | ds_init(&s); | |
1308 | for (i = 0; i < n_fses; i++) { | |
1309 | ds_clear(&s); | |
1310 | ofp_print_flow_stats(&s, &fses[i]); | |
1311 | puts(ds_cstr(&s)); | |
1312 | } | |
1313 | ds_destroy(&s); | |
1314 | ||
1315 | for (i = 0; i < n_fses; i++) { | |
1316 | free(CONST_CAST(struct ofpact *, fses[i].ofpacts)); | |
1317 | } | |
1318 | free(fses); | |
1319 | ||
1320 | vconn_close(vconn); | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | static void | |
1325 | ofctl_dump_aggregate(struct ovs_cmdl_context *ctx) | |
1326 | { | |
1327 | ofctl_dump_flows__(ctx->argc, ctx->argv, true); | |
1328 | } | |
1329 | ||
1330 | static void | |
1331 | ofctl_queue_stats(struct ovs_cmdl_context *ctx) | |
1332 | { | |
1333 | struct ofpbuf *request; | |
1334 | struct vconn *vconn; | |
1335 | struct ofputil_queue_stats_request oqs; | |
1336 | ||
1337 | open_vconn(ctx->argv[1], &vconn); | |
1338 | ||
1339 | if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) { | |
1340 | oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]); | |
1341 | } else { | |
1342 | oqs.port_no = OFPP_ANY; | |
1343 | } | |
1344 | if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) { | |
1345 | oqs.queue_id = atoi(ctx->argv[3]); | |
1346 | } else { | |
1347 | oqs.queue_id = OFPQ_ALL; | |
1348 | } | |
1349 | ||
1350 | request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs); | |
1351 | dump_transaction(vconn, request); | |
1352 | vconn_close(vconn); | |
1353 | } | |
1354 | ||
1355 | static void | |
1356 | ofctl_queue_get_config(struct ovs_cmdl_context *ctx) | |
1357 | { | |
1358 | const char *vconn_name = ctx->argv[1]; | |
1359 | const char *port_name = ctx->argc > 2 ? ctx->argv[2] : "any"; | |
1360 | ofp_port_t port = str_to_port_no(vconn_name, port_name); | |
1361 | const char *queue_name = ctx->argc > 3 ? ctx->argv[3] : "all"; | |
1362 | uint32_t queue = (!strcasecmp(queue_name, "all") | |
1363 | ? OFPQ_ALL | |
1364 | : atoi(queue_name)); | |
1365 | struct vconn *vconn; | |
1366 | ||
1367 | enum ofputil_protocol protocol = open_vconn(vconn_name, &vconn); | |
1368 | enum ofp_version version = ofputil_protocol_to_ofp_version(protocol); | |
1369 | if (port == OFPP_ANY && version == OFP10_VERSION) { | |
1370 | /* The user requested all queues on all ports. OpenFlow 1.0 only | |
1371 | * supports getting queues for an individual port, so to implement the | |
1372 | * user's request we have to get a list of all the ports. | |
1373 | * | |
1374 | * We use a second vconn to avoid having to accumulate a list of all of | |
1375 | * the ports. */ | |
1376 | struct vconn *vconn2; | |
1377 | enum ofputil_protocol protocol2 = open_vconn(vconn_name, &vconn2); | |
1378 | enum ofp_version version2 = ofputil_protocol_to_ofp_version(protocol2); | |
1379 | ||
1380 | struct port_iterator pi; | |
1381 | struct ofputil_phy_port pp; | |
1382 | for (port_iterator_init(&pi, vconn); port_iterator_next(&pi, &pp); ) { | |
1383 | if (ofp_to_u16(pp.port_no) < ofp_to_u16(OFPP_MAX)) { | |
1384 | dump_transaction(vconn2, | |
1385 | ofputil_encode_queue_get_config_request( | |
1386 | version2, pp.port_no, queue)); | |
1387 | } | |
1388 | } | |
1389 | port_iterator_destroy(&pi); | |
1390 | vconn_close(vconn2); | |
1391 | } else { | |
1392 | dump_transaction(vconn, ofputil_encode_queue_get_config_request( | |
1393 | version, port, queue)); | |
1394 | } | |
1395 | vconn_close(vconn); | |
1396 | } | |
1397 | ||
1398 | static enum ofputil_protocol | |
1399 | open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp, | |
1400 | enum ofputil_protocol usable_protocols) | |
1401 | { | |
1402 | enum ofputil_protocol cur_protocol; | |
1403 | char *usable_s; | |
1404 | int i; | |
1405 | ||
1406 | if (!(usable_protocols & allowed_protocols)) { | |
1407 | char *allowed_s = ofputil_protocols_to_string(allowed_protocols); | |
1408 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
1409 | ovs_fatal(0, "none of the usable flow formats (%s) is among the " | |
1410 | "allowed flow formats (%s)", usable_s, allowed_s); | |
1411 | } | |
1412 | ||
1413 | /* If the initial flow format is allowed and usable, keep it. */ | |
1414 | cur_protocol = open_vconn(remote, vconnp); | |
1415 | if (usable_protocols & allowed_protocols & cur_protocol) { | |
1416 | return cur_protocol; | |
1417 | } | |
1418 | ||
1419 | /* Otherwise try each flow format in turn. */ | |
1420 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
1421 | enum ofputil_protocol f = 1 << i; | |
1422 | ||
1423 | if (f != cur_protocol | |
1424 | && f & usable_protocols & allowed_protocols | |
1425 | && try_set_protocol(*vconnp, f, &cur_protocol)) { | |
1426 | return f; | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
1431 | ovs_fatal(0, "switch does not support any of the usable flow " | |
1432 | "formats (%s)", usable_s); | |
1433 | } | |
1434 | ||
1435 | static void | |
1436 | bundle_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, | |
1437 | size_t n_fms, enum ofputil_protocol usable_protocols) | |
1438 | { | |
1439 | enum ofputil_protocol protocol; | |
1440 | struct vconn *vconn; | |
1441 | struct ovs_list requests; | |
1442 | size_t i; | |
1443 | ||
1444 | ovs_list_init(&requests); | |
1445 | ||
1446 | /* Bundles need OpenFlow 1.3+. */ | |
1447 | usable_protocols &= OFPUTIL_P_OF13_UP; | |
1448 | protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols); | |
1449 | ||
1450 | for (i = 0; i < n_fms; i++) { | |
1451 | struct ofputil_flow_mod *fm = &fms[i]; | |
1452 | struct ofpbuf *request = ofputil_encode_flow_mod(fm, protocol); | |
1453 | ||
1454 | ovs_list_push_back(&requests, &request->list_node); | |
1455 | free(CONST_CAST(struct ofpact *, fm->ofpacts)); | |
1456 | } | |
1457 | ||
1458 | bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC); | |
1459 | ofpbuf_list_delete(&requests); | |
1460 | vconn_close(vconn); | |
1461 | } | |
1462 | ||
1463 | static void | |
1464 | ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms, | |
1465 | size_t n_fms, enum ofputil_protocol usable_protocols) | |
1466 | { | |
1467 | enum ofputil_protocol protocol; | |
1468 | struct vconn *vconn; | |
1469 | size_t i; | |
1470 | ||
1471 | if (bundle) { | |
1472 | bundle_flow_mod__(remote, fms, n_fms, usable_protocols); | |
1473 | return; | |
1474 | } | |
1475 | ||
1476 | protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols); | |
1477 | ||
1478 | for (i = 0; i < n_fms; i++) { | |
1479 | struct ofputil_flow_mod *fm = &fms[i]; | |
1480 | ||
1481 | transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol)); | |
1482 | free(CONST_CAST(struct ofpact *, fm->ofpacts)); | |
1483 | } | |
1484 | vconn_close(vconn); | |
1485 | } | |
1486 | ||
1487 | static void | |
1488 | ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], int command) | |
1489 | { | |
1490 | enum ofputil_protocol usable_protocols; | |
1491 | struct ofputil_flow_mod *fms = NULL; | |
1492 | size_t n_fms = 0; | |
1493 | char *error; | |
1494 | ||
1495 | if (command == OFPFC_ADD) { | |
1496 | /* Allow the file to specify a mix of commands. If none specified at | |
1497 | * the beginning of any given line, then the default is OFPFC_ADD, so | |
1498 | * this is backwards compatible. */ | |
1499 | command = -2; | |
1500 | } | |
1501 | error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms, | |
1502 | &usable_protocols); | |
1503 | if (error) { | |
1504 | ovs_fatal(0, "%s", error); | |
1505 | } | |
1506 | ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols); | |
1507 | free(fms); | |
1508 | } | |
1509 | ||
1510 | static void | |
1511 | ofctl_flow_mod(int argc, char *argv[], uint16_t command) | |
1512 | { | |
1513 | if (argc > 2 && !strcmp(argv[2], "-")) { | |
1514 | ofctl_flow_mod_file(argc, argv, command); | |
1515 | } else { | |
1516 | struct ofputil_flow_mod fm; | |
1517 | char *error; | |
1518 | enum ofputil_protocol usable_protocols; | |
1519 | ||
1520 | error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command, | |
1521 | &usable_protocols); | |
1522 | if (error) { | |
1523 | ovs_fatal(0, "%s", error); | |
1524 | } | |
1525 | ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols); | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | static void | |
1530 | ofctl_add_flow(struct ovs_cmdl_context *ctx) | |
1531 | { | |
1532 | ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD); | |
1533 | } | |
1534 | ||
1535 | static void | |
1536 | ofctl_add_flows(struct ovs_cmdl_context *ctx) | |
1537 | { | |
1538 | ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD); | |
1539 | } | |
1540 | ||
1541 | static void | |
1542 | ofctl_mod_flows(struct ovs_cmdl_context *ctx) | |
1543 | { | |
1544 | ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY); | |
1545 | } | |
1546 | ||
1547 | static void | |
1548 | ofctl_del_flows(struct ovs_cmdl_context *ctx) | |
1549 | { | |
1550 | ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE); | |
1551 | } | |
1552 | ||
1553 | static bool | |
1554 | set_packet_in_format(struct vconn *vconn, | |
1555 | enum nx_packet_in_format packet_in_format, | |
1556 | bool must_succeed) | |
1557 | { | |
1558 | struct ofpbuf *spif; | |
1559 | ||
1560 | spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn), | |
1561 | packet_in_format); | |
1562 | if (must_succeed) { | |
1563 | transact_noreply(vconn, spif); | |
1564 | } else { | |
1565 | struct ofpbuf *reply; | |
1566 | ||
1567 | run(vconn_transact_noreply(vconn, spif, &reply), | |
1568 | "talking to %s", vconn_get_name(vconn)); | |
1569 | if (reply) { | |
1570 | char *s = ofp_to_string(reply->data, reply->size, 2); | |
1571 | VLOG_DBG("%s: failed to set packet in format to nx_packet_in, " | |
1572 | "controller replied: %s.", | |
1573 | vconn_get_name(vconn), s); | |
1574 | free(s); | |
1575 | ofpbuf_delete(reply); | |
1576 | ||
1577 | return false; | |
1578 | } else { | |
1579 | VLOG_DBG("%s: using user-specified packet in format %s", | |
1580 | vconn_get_name(vconn), | |
1581 | ofputil_packet_in_format_to_string(packet_in_format)); | |
1582 | } | |
1583 | } | |
1584 | return true; | |
1585 | } | |
1586 | ||
1587 | static int | |
1588 | monitor_set_invalid_ttl_to_controller(struct vconn *vconn) | |
1589 | { | |
1590 | struct ofputil_switch_config config; | |
1591 | ||
1592 | fetch_switch_config(vconn, &config); | |
1593 | if (!config.invalid_ttl_to_controller) { | |
1594 | config.invalid_ttl_to_controller = 1; | |
1595 | set_switch_config(vconn, &config); | |
1596 | ||
1597 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
1598 | * has ill-defined error reporting for bad flags, so this is about the | |
1599 | * best we can do. */ | |
1600 | fetch_switch_config(vconn, &config); | |
1601 | if (!config.invalid_ttl_to_controller) { | |
1602 | ovs_fatal(0, "setting invalid_ttl_to_controller failed (this " | |
1603 | "switch probably doesn't support this flag)"); | |
1604 | } | |
1605 | } | |
1606 | return 0; | |
1607 | } | |
1608 | ||
1609 | /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'. The | |
1610 | * caller must free '*msgp'. On success, returns NULL. On failure, returns | |
1611 | * an error message and stores NULL in '*msgp'. */ | |
1612 | static const char * | |
1613 | openflow_from_hex(const char *hex, struct ofpbuf **msgp) | |
1614 | { | |
1615 | struct ofp_header *oh; | |
1616 | struct ofpbuf *msg; | |
1617 | ||
1618 | msg = ofpbuf_new(strlen(hex) / 2); | |
1619 | *msgp = NULL; | |
1620 | ||
1621 | if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') { | |
1622 | ofpbuf_delete(msg); | |
1623 | return "Trailing garbage in hex data"; | |
1624 | } | |
1625 | ||
1626 | if (msg->size < sizeof(struct ofp_header)) { | |
1627 | ofpbuf_delete(msg); | |
1628 | return "Message too short for OpenFlow"; | |
1629 | } | |
1630 | ||
1631 | oh = msg->data; | |
1632 | if (msg->size != ntohs(oh->length)) { | |
1633 | ofpbuf_delete(msg); | |
1634 | return "Message size does not match length in OpenFlow header"; | |
1635 | } | |
1636 | ||
1637 | *msgp = msg; | |
1638 | return NULL; | |
1639 | } | |
1640 | ||
1641 | static void | |
1642 | ofctl_send(struct unixctl_conn *conn, int argc, | |
1643 | const char *argv[], void *vconn_) | |
1644 | { | |
1645 | struct vconn *vconn = vconn_; | |
1646 | struct ds reply; | |
1647 | bool ok; | |
1648 | int i; | |
1649 | ||
1650 | ok = true; | |
1651 | ds_init(&reply); | |
1652 | for (i = 1; i < argc; i++) { | |
1653 | const char *error_msg; | |
1654 | struct ofpbuf *msg; | |
1655 | int error; | |
1656 | ||
1657 | error_msg = openflow_from_hex(argv[i], &msg); | |
1658 | if (error_msg) { | |
1659 | ds_put_format(&reply, "%s\n", error_msg); | |
1660 | ok = false; | |
1661 | continue; | |
1662 | } | |
1663 | ||
1664 | fprintf(stderr, "send: "); | |
1665 | ofp_print(stderr, msg->data, msg->size, verbosity); | |
1666 | ||
1667 | error = vconn_send_block(vconn, msg); | |
1668 | if (error) { | |
1669 | ofpbuf_delete(msg); | |
1670 | ds_put_format(&reply, "%s\n", ovs_strerror(error)); | |
1671 | ok = false; | |
1672 | } else { | |
1673 | ds_put_cstr(&reply, "sent\n"); | |
1674 | } | |
1675 | } | |
1676 | ||
1677 | if (ok) { | |
1678 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1679 | } else { | |
1680 | unixctl_command_reply_error(conn, ds_cstr(&reply)); | |
1681 | } | |
1682 | ds_destroy(&reply); | |
1683 | } | |
1684 | ||
1685 | static void | |
1686 | unixctl_packet_out(struct unixctl_conn *conn, int OVS_UNUSED argc, | |
1687 | const char *argv[], void *vconn_) | |
1688 | { | |
1689 | struct vconn *vconn = vconn_; | |
1690 | enum ofputil_protocol protocol | |
1691 | = ofputil_protocol_from_ofp_version(vconn_get_version(vconn)); | |
1692 | struct ds reply = DS_EMPTY_INITIALIZER; | |
1693 | bool ok = true; | |
1694 | ||
1695 | enum ofputil_protocol usable_protocols; | |
1696 | struct ofputil_packet_out po; | |
1697 | char *error_msg; | |
1698 | ||
1699 | error_msg = parse_ofp_packet_out_str(&po, argv[1], &usable_protocols); | |
1700 | if (error_msg) { | |
1701 | ds_put_format(&reply, "%s\n", error_msg); | |
1702 | free(error_msg); | |
1703 | ok = false; | |
1704 | } | |
1705 | ||
1706 | if (ok && !(usable_protocols & protocol)) { | |
1707 | ds_put_format(&reply, "PACKET_OUT actions are incompatible with the OpenFlow connection.\n"); | |
1708 | ok = false; | |
1709 | } | |
1710 | ||
1711 | if (ok) { | |
1712 | struct ofpbuf *msg = ofputil_encode_packet_out(&po, protocol); | |
1713 | ||
1714 | ofp_print(stderr, msg->data, msg->size, verbosity); | |
1715 | ||
1716 | int error = vconn_send_block(vconn, msg); | |
1717 | if (error) { | |
1718 | ofpbuf_delete(msg); | |
1719 | ds_put_format(&reply, "%s\n", ovs_strerror(error)); | |
1720 | ok = false; | |
1721 | } | |
1722 | } | |
1723 | ||
1724 | if (ok) { | |
1725 | unixctl_command_reply(conn, ds_cstr(&reply)); | |
1726 | } else { | |
1727 | unixctl_command_reply_error(conn, ds_cstr(&reply)); | |
1728 | } | |
1729 | ds_destroy(&reply); | |
1730 | ||
1731 | if (!error_msg) { | |
1732 | free(CONST_CAST(void *, po.packet)); | |
1733 | free(po.ofpacts); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | struct barrier_aux { | |
1738 | struct vconn *vconn; /* OpenFlow connection for sending barrier. */ | |
1739 | struct unixctl_conn *conn; /* Connection waiting for barrier response. */ | |
1740 | }; | |
1741 | ||
1742 | static void | |
1743 | ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1744 | const char *argv[] OVS_UNUSED, void *aux_) | |
1745 | { | |
1746 | struct barrier_aux *aux = aux_; | |
1747 | struct ofpbuf *msg; | |
1748 | int error; | |
1749 | ||
1750 | if (aux->conn) { | |
1751 | unixctl_command_reply_error(conn, "already waiting for barrier reply"); | |
1752 | return; | |
1753 | } | |
1754 | ||
1755 | msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn)); | |
1756 | error = vconn_send_block(aux->vconn, msg); | |
1757 | if (error) { | |
1758 | ofpbuf_delete(msg); | |
1759 | unixctl_command_reply_error(conn, ovs_strerror(error)); | |
1760 | } else { | |
1761 | aux->conn = conn; | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | static void | |
1766 | ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1767 | const char *argv[], void *aux OVS_UNUSED) | |
1768 | { | |
1769 | int fd; | |
1770 | ||
1771 | fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); | |
1772 | if (fd < 0) { | |
1773 | unixctl_command_reply_error(conn, ovs_strerror(errno)); | |
1774 | return; | |
1775 | } | |
1776 | ||
1777 | fflush(stderr); | |
1778 | dup2(fd, STDERR_FILENO); | |
1779 | close(fd); | |
1780 | unixctl_command_reply(conn, NULL); | |
1781 | } | |
1782 | ||
1783 | static void | |
1784 | ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1785 | const char *argv[] OVS_UNUSED, void *blocked_) | |
1786 | { | |
1787 | bool *blocked = blocked_; | |
1788 | ||
1789 | if (!*blocked) { | |
1790 | *blocked = true; | |
1791 | unixctl_command_reply(conn, NULL); | |
1792 | } else { | |
1793 | unixctl_command_reply(conn, "already blocking"); | |
1794 | } | |
1795 | } | |
1796 | ||
1797 | static void | |
1798 | ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
1799 | const char *argv[] OVS_UNUSED, void *blocked_) | |
1800 | { | |
1801 | bool *blocked = blocked_; | |
1802 | ||
1803 | if (*blocked) { | |
1804 | *blocked = false; | |
1805 | unixctl_command_reply(conn, NULL); | |
1806 | } else { | |
1807 | unixctl_command_reply(conn, "already unblocked"); | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | /* Prints to stdout all of the messages received on 'vconn'. | |
1812 | * | |
1813 | * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request | |
1814 | * received on 'vconn'. | |
1815 | * | |
1816 | * If 'resume_continuations' is true, sends an NXT_RESUME in reply to any | |
1817 | * NXT_PACKET_IN2 that includes a continuation. */ | |
1818 | static void | |
1819 | monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests, | |
1820 | bool resume_continuations) | |
1821 | { | |
1822 | struct barrier_aux barrier_aux = { vconn, NULL }; | |
1823 | struct unixctl_server *server; | |
1824 | bool exiting = false; | |
1825 | bool blocked = false; | |
1826 | int error; | |
1827 | ||
1828 | daemon_save_fd(STDERR_FILENO); | |
1829 | daemonize_start(false); | |
1830 | error = unixctl_server_create(unixctl_path, &server); | |
1831 | if (error) { | |
1832 | ovs_fatal(error, "failed to create unixctl server"); | |
1833 | } | |
1834 | unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting); | |
1835 | unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX, | |
1836 | ofctl_send, vconn); | |
1837 | unixctl_command_register("ofctl/packet-out", "\"in_port=<port> packet=<hex data> actions=...\"", 1, 1, | |
1838 | unixctl_packet_out, vconn); | |
1839 | unixctl_command_register("ofctl/barrier", "", 0, 0, | |
1840 | ofctl_barrier, &barrier_aux); | |
1841 | unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1, | |
1842 | ofctl_set_output_file, NULL); | |
1843 | ||
1844 | unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked); | |
1845 | unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock, | |
1846 | &blocked); | |
1847 | ||
1848 | daemonize_complete(); | |
1849 | ||
1850 | enum ofp_version version = vconn_get_version(vconn); | |
1851 | enum ofputil_protocol protocol | |
1852 | = ofputil_protocol_from_ofp_version(version); | |
1853 | ||
1854 | for (;;) { | |
1855 | struct ofpbuf *b; | |
1856 | int retval; | |
1857 | ||
1858 | unixctl_server_run(server); | |
1859 | ||
1860 | while (!blocked) { | |
1861 | enum ofptype type; | |
1862 | ||
1863 | retval = vconn_recv(vconn, &b); | |
1864 | if (retval == EAGAIN) { | |
1865 | break; | |
1866 | } | |
1867 | run(retval, "vconn_recv"); | |
1868 | ||
1869 | if (timestamp) { | |
1870 | char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ", | |
1871 | time_wall_msec(), true); | |
1872 | fputs(s, stderr); | |
1873 | free(s); | |
1874 | } | |
1875 | ||
1876 | ofptype_decode(&type, b->data); | |
1877 | ofp_print(stderr, b->data, b->size, verbosity + 2); | |
1878 | fflush(stderr); | |
1879 | ||
1880 | switch ((int) type) { | |
1881 | case OFPTYPE_BARRIER_REPLY: | |
1882 | if (barrier_aux.conn) { | |
1883 | unixctl_command_reply(barrier_aux.conn, NULL); | |
1884 | barrier_aux.conn = NULL; | |
1885 | } | |
1886 | break; | |
1887 | ||
1888 | case OFPTYPE_ECHO_REQUEST: | |
1889 | if (reply_to_echo_requests) { | |
1890 | struct ofpbuf *reply; | |
1891 | ||
1892 | reply = make_echo_reply(b->data); | |
1893 | retval = vconn_send_block(vconn, reply); | |
1894 | if (retval) { | |
1895 | ovs_fatal(retval, "failed to send echo reply"); | |
1896 | } | |
1897 | } | |
1898 | break; | |
1899 | ||
1900 | case OFPTYPE_PACKET_IN: | |
1901 | if (resume_continuations) { | |
1902 | struct ofputil_packet_in pin; | |
1903 | struct ofpbuf continuation; | |
1904 | ||
1905 | error = ofputil_decode_packet_in(b->data, true, &pin, | |
1906 | NULL, NULL, | |
1907 | &continuation); | |
1908 | if (error) { | |
1909 | fprintf(stderr, "decoding packet-in failed: %s", | |
1910 | ofperr_to_string(error)); | |
1911 | } else if (continuation.size) { | |
1912 | struct ofpbuf *reply; | |
1913 | ||
1914 | reply = ofputil_encode_resume(&pin, &continuation, | |
1915 | protocol); | |
1916 | ||
1917 | fprintf(stderr, "send: "); | |
1918 | ofp_print(stderr, reply->data, reply->size, | |
1919 | verbosity + 2); | |
1920 | fflush(stderr); | |
1921 | ||
1922 | retval = vconn_send_block(vconn, reply); | |
1923 | if (retval) { | |
1924 | ovs_fatal(retval, "failed to send NXT_RESUME"); | |
1925 | } | |
1926 | } | |
1927 | } | |
1928 | break; | |
1929 | } | |
1930 | ofpbuf_delete(b); | |
1931 | } | |
1932 | ||
1933 | if (exiting) { | |
1934 | break; | |
1935 | } | |
1936 | ||
1937 | vconn_run(vconn); | |
1938 | vconn_run_wait(vconn); | |
1939 | if (!blocked) { | |
1940 | vconn_recv_wait(vconn); | |
1941 | } | |
1942 | unixctl_server_wait(server); | |
1943 | poll_block(); | |
1944 | } | |
1945 | vconn_close(vconn); | |
1946 | unixctl_server_destroy(server); | |
1947 | } | |
1948 | ||
1949 | static void | |
1950 | ofctl_monitor(struct ovs_cmdl_context *ctx) | |
1951 | { | |
1952 | struct vconn *vconn; | |
1953 | int i; | |
1954 | enum ofputil_protocol usable_protocols; | |
1955 | ||
1956 | /* If the user wants the invalid_ttl_to_controller feature, limit the | |
1957 | * OpenFlow versions to those that support that feature. (Support in | |
1958 | * OpenFlow 1.0 is an Open vSwitch extension.) */ | |
1959 | for (i = 2; i < ctx->argc; i++) { | |
1960 | if (!strcmp(ctx->argv[i], "invalid_ttl")) { | |
1961 | uint32_t usable_versions = ((1u << OFP10_VERSION) | | |
1962 | (1u << OFP11_VERSION) | | |
1963 | (1u << OFP12_VERSION)); | |
1964 | uint32_t allowed_versions = get_allowed_ofp_versions(); | |
1965 | if (!(allowed_versions & usable_versions)) { | |
1966 | struct ds versions = DS_EMPTY_INITIALIZER; | |
1967 | ofputil_format_version_bitmap_names(&versions, | |
1968 | usable_versions); | |
1969 | ovs_fatal(0, "invalid_ttl requires one of the OpenFlow " | |
1970 | "versions %s but none is enabled (use -O)", | |
1971 | ds_cstr(&versions)); | |
1972 | } | |
1973 | mask_allowed_ofp_versions(usable_versions); | |
1974 | break; | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | open_vconn(ctx->argv[1], &vconn); | |
1979 | bool resume_continuations = false; | |
1980 | for (i = 2; i < ctx->argc; i++) { | |
1981 | const char *arg = ctx->argv[i]; | |
1982 | ||
1983 | if (isdigit((unsigned char) *arg)) { | |
1984 | struct ofputil_switch_config config; | |
1985 | ||
1986 | fetch_switch_config(vconn, &config); | |
1987 | config.miss_send_len = atoi(arg); | |
1988 | set_switch_config(vconn, &config); | |
1989 | } else if (!strcmp(arg, "invalid_ttl")) { | |
1990 | monitor_set_invalid_ttl_to_controller(vconn); | |
1991 | } else if (!strncmp(arg, "watch:", 6)) { | |
1992 | struct ofputil_flow_monitor_request fmr; | |
1993 | struct ofpbuf *msg; | |
1994 | char *error; | |
1995 | ||
1996 | error = parse_flow_monitor_request(&fmr, arg + 6, | |
1997 | &usable_protocols); | |
1998 | if (error) { | |
1999 | ovs_fatal(0, "%s", error); | |
2000 | } | |
2001 | ||
2002 | msg = ofpbuf_new(0); | |
2003 | ofputil_append_flow_monitor_request(&fmr, msg); | |
2004 | dump_transaction(vconn, msg); | |
2005 | fflush(stdout); | |
2006 | } else if (!strcmp(arg, "resume")) { | |
2007 | /* This option is intentionally undocumented because it is meant | |
2008 | * only for testing. */ | |
2009 | resume_continuations = true; | |
2010 | ||
2011 | /* Set miss_send_len to ensure that we get packet-ins. */ | |
2012 | struct ofputil_switch_config config; | |
2013 | fetch_switch_config(vconn, &config); | |
2014 | config.miss_send_len = UINT16_MAX; | |
2015 | set_switch_config(vconn, &config); | |
2016 | } else { | |
2017 | ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg); | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | if (preferred_packet_in_format >= 0) { | |
2022 | /* A particular packet-in format was requested, so we must set it. */ | |
2023 | set_packet_in_format(vconn, preferred_packet_in_format, true); | |
2024 | } else { | |
2025 | /* Otherwise, we always prefer NXT_PACKET_IN2. */ | |
2026 | if (!set_packet_in_format(vconn, NXPIF_NXT_PACKET_IN2, false)) { | |
2027 | /* We can't get NXT_PACKET_IN2. For OpenFlow 1.0 only, request | |
2028 | * NXT_PACKET_IN. (Before 2.6, Open vSwitch will accept a request | |
2029 | * for NXT_PACKET_IN with OF1.1+, but even after that it still | |
2030 | * sends packet-ins in the OpenFlow native format.) */ | |
2031 | if (vconn_get_version(vconn) == OFP10_VERSION) { | |
2032 | set_packet_in_format(vconn, NXPIF_NXT_PACKET_IN, false); | |
2033 | } | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | monitor_vconn(vconn, true, resume_continuations); | |
2038 | } | |
2039 | ||
2040 | static void | |
2041 | ofctl_snoop(struct ovs_cmdl_context *ctx) | |
2042 | { | |
2043 | struct vconn *vconn; | |
2044 | ||
2045 | open_vconn__(ctx->argv[1], SNOOP, &vconn); | |
2046 | monitor_vconn(vconn, false, false); | |
2047 | } | |
2048 | ||
2049 | static void | |
2050 | ofctl_dump_ports(struct ovs_cmdl_context *ctx) | |
2051 | { | |
2052 | struct ofpbuf *request; | |
2053 | struct vconn *vconn; | |
2054 | ofp_port_t port; | |
2055 | ||
2056 | open_vconn(ctx->argv[1], &vconn); | |
2057 | port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY; | |
2058 | request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port); | |
2059 | dump_transaction(vconn, request); | |
2060 | vconn_close(vconn); | |
2061 | } | |
2062 | ||
2063 | static void | |
2064 | ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx) | |
2065 | { | |
2066 | struct ofpbuf *request; | |
2067 | struct vconn *vconn; | |
2068 | ofp_port_t port; | |
2069 | ||
2070 | open_vconn(ctx->argv[1], &vconn); | |
2071 | port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY; | |
2072 | request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn), | |
2073 | port); | |
2074 | dump_transaction(vconn, request); | |
2075 | vconn_close(vconn); | |
2076 | } | |
2077 | ||
2078 | static void | |
2079 | ofctl_probe(struct ovs_cmdl_context *ctx) | |
2080 | { | |
2081 | struct ofpbuf *request; | |
2082 | struct vconn *vconn; | |
2083 | struct ofpbuf *reply; | |
2084 | ||
2085 | open_vconn(ctx->argv[1], &vconn); | |
2086 | request = make_echo_request(vconn_get_version(vconn)); | |
2087 | run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]); | |
2088 | if (reply->size != sizeof(struct ofp_header)) { | |
2089 | ovs_fatal(0, "reply does not match request"); | |
2090 | } | |
2091 | ofpbuf_delete(reply); | |
2092 | vconn_close(vconn); | |
2093 | } | |
2094 | ||
2095 | static void | |
2096 | ofctl_packet_out(struct ovs_cmdl_context *ctx) | |
2097 | { | |
2098 | enum ofputil_protocol usable_protocols; | |
2099 | enum ofputil_protocol protocol; | |
2100 | struct ofputil_packet_out po; | |
2101 | struct vconn *vconn; | |
2102 | struct ofpbuf *opo; | |
2103 | char *error; | |
2104 | ||
2105 | /* Use the old syntax when more than 4 arguments are given. */ | |
2106 | if (ctx->argc > 4) { | |
2107 | struct ofpbuf ofpacts; | |
2108 | int i; | |
2109 | ||
2110 | ofpbuf_init(&ofpacts, 64); | |
2111 | error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, | |
2112 | &usable_protocols); | |
2113 | if (error) { | |
2114 | ovs_fatal(0, "%s", error); | |
2115 | } | |
2116 | ||
2117 | po.buffer_id = UINT32_MAX; | |
2118 | po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]); | |
2119 | po.ofpacts = ofpacts.data; | |
2120 | po.ofpacts_len = ofpacts.size; | |
2121 | ||
2122 | protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, | |
2123 | usable_protocols); | |
2124 | for (i = 4; i < ctx->argc; i++) { | |
2125 | struct dp_packet *packet; | |
2126 | const char *error_msg; | |
2127 | ||
2128 | error_msg = eth_from_hex(ctx->argv[i], &packet); | |
2129 | if (error_msg) { | |
2130 | ovs_fatal(0, "%s", error_msg); | |
2131 | } | |
2132 | ||
2133 | po.packet = dp_packet_data(packet); | |
2134 | po.packet_len = dp_packet_size(packet); | |
2135 | opo = ofputil_encode_packet_out(&po, protocol); | |
2136 | transact_noreply(vconn, opo); | |
2137 | dp_packet_delete(packet); | |
2138 | } | |
2139 | vconn_close(vconn); | |
2140 | ofpbuf_uninit(&ofpacts); | |
2141 | } else if (ctx->argc == 3) { | |
2142 | error = parse_ofp_packet_out_str(&po, ctx->argv[2], &usable_protocols); | |
2143 | if (error) { | |
2144 | ovs_fatal(0, "%s", error); | |
2145 | } | |
2146 | protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, | |
2147 | usable_protocols); | |
2148 | opo = ofputil_encode_packet_out(&po, protocol); | |
2149 | transact_noreply(vconn, opo); | |
2150 | free(CONST_CAST(void *, po.packet)); | |
2151 | free(po.ofpacts); | |
2152 | } else { | |
2153 | ovs_fatal(0, "Too many arguments (%d)", ctx->argc); | |
2154 | } | |
2155 | } | |
2156 | ||
2157 | static void | |
2158 | ofctl_mod_port(struct ovs_cmdl_context *ctx) | |
2159 | { | |
2160 | struct ofp_config_flag { | |
2161 | const char *name; /* The flag's name. */ | |
2162 | enum ofputil_port_config bit; /* Bit to turn on or off. */ | |
2163 | bool on; /* Value to set the bit to. */ | |
2164 | }; | |
2165 | static const struct ofp_config_flag flags[] = { | |
2166 | { "up", OFPUTIL_PC_PORT_DOWN, false }, | |
2167 | { "down", OFPUTIL_PC_PORT_DOWN, true }, | |
2168 | { "stp", OFPUTIL_PC_NO_STP, false }, | |
2169 | { "receive", OFPUTIL_PC_NO_RECV, false }, | |
2170 | { "receive-stp", OFPUTIL_PC_NO_RECV_STP, false }, | |
2171 | { "flood", OFPUTIL_PC_NO_FLOOD, false }, | |
2172 | { "forward", OFPUTIL_PC_NO_FWD, false }, | |
2173 | { "packet-in", OFPUTIL_PC_NO_PACKET_IN, false }, | |
2174 | }; | |
2175 | ||
2176 | const struct ofp_config_flag *flag; | |
2177 | enum ofputil_protocol protocol; | |
2178 | struct ofputil_port_mod pm; | |
2179 | struct ofputil_phy_port pp; | |
2180 | struct vconn *vconn; | |
2181 | const char *command; | |
2182 | bool not; | |
2183 | ||
2184 | fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp); | |
2185 | ||
2186 | pm.port_no = pp.port_no; | |
2187 | pm.hw_addr = pp.hw_addr; | |
2188 | pm.config = 0; | |
2189 | pm.mask = 0; | |
2190 | pm.advertise = 0; | |
2191 | ||
2192 | if (!strncasecmp(ctx->argv[3], "no-", 3)) { | |
2193 | command = ctx->argv[3] + 3; | |
2194 | not = true; | |
2195 | } else if (!strncasecmp(ctx->argv[3], "no", 2)) { | |
2196 | command = ctx->argv[3] + 2; | |
2197 | not = true; | |
2198 | } else { | |
2199 | command = ctx->argv[3]; | |
2200 | not = false; | |
2201 | } | |
2202 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { | |
2203 | if (!strcasecmp(command, flag->name)) { | |
2204 | pm.mask = flag->bit; | |
2205 | pm.config = flag->on ^ not ? flag->bit : 0; | |
2206 | goto found; | |
2207 | } | |
2208 | } | |
2209 | ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]); | |
2210 | ||
2211 | found: | |
2212 | protocol = open_vconn(ctx->argv[1], &vconn); | |
2213 | transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol)); | |
2214 | vconn_close(vconn); | |
2215 | } | |
2216 | ||
2217 | /* This function uses OFPMP14_TABLE_DESC request to get the current | |
2218 | * table configuration from switch. The function then modifies | |
2219 | * only that table-config property, which has been requested. */ | |
2220 | static void | |
2221 | fetch_table_desc(struct vconn *vconn, struct ofputil_table_mod *tm, | |
2222 | struct ofputil_table_desc *td) | |
2223 | { | |
2224 | struct ofpbuf *request; | |
2225 | ovs_be32 send_xid; | |
2226 | bool done = false; | |
2227 | bool found = false; | |
2228 | ||
2229 | request = ofputil_encode_table_desc_request(vconn_get_version(vconn)); | |
2230 | send_xid = ((struct ofp_header *) request->data)->xid; | |
2231 | send_openflow_buffer(vconn, request); | |
2232 | while (!done) { | |
2233 | ovs_be32 recv_xid; | |
2234 | struct ofpbuf *reply; | |
2235 | ||
2236 | run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed"); | |
2237 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
2238 | if (send_xid == recv_xid) { | |
2239 | struct ofp_header *oh = reply->data; | |
2240 | struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length)); | |
2241 | ||
2242 | enum ofptype type; | |
2243 | if (ofptype_pull(&type, &b) | |
2244 | || type != OFPTYPE_TABLE_DESC_REPLY) { | |
2245 | ovs_fatal(0, "received bad reply: %s", | |
2246 | ofp_to_string(reply->data, reply->size, | |
2247 | verbosity + 1)); | |
2248 | } | |
2249 | uint16_t flags = ofpmp_flags(oh); | |
2250 | done = !(flags & OFPSF_REPLY_MORE); | |
2251 | if (found) { | |
2252 | /* We've already found the table desc consisting of current | |
2253 | * table configuration, but we need to drain the queue of | |
2254 | * any other replies for this request. */ | |
2255 | continue; | |
2256 | } | |
2257 | while (!ofputil_decode_table_desc(&b, td, oh->version)) { | |
2258 | if (td->table_id == tm->table_id) { | |
2259 | found = true; | |
2260 | break; | |
2261 | } | |
2262 | } | |
2263 | } else { | |
2264 | VLOG_DBG("received reply with xid %08"PRIx32" " | |
2265 | "!= expected %08"PRIx32, recv_xid, send_xid); | |
2266 | } | |
2267 | ofpbuf_delete(reply); | |
2268 | } | |
2269 | if (tm->eviction != OFPUTIL_TABLE_EVICTION_DEFAULT) { | |
2270 | tm->vacancy = td->vacancy; | |
2271 | tm->table_vacancy.vacancy_down = td->table_vacancy.vacancy_down; | |
2272 | tm->table_vacancy.vacancy_up = td->table_vacancy.vacancy_up; | |
2273 | } else if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) { | |
2274 | tm->eviction = td->eviction; | |
2275 | tm->eviction_flags = td->eviction_flags; | |
2276 | } | |
2277 | } | |
2278 | ||
2279 | static void | |
2280 | ofctl_mod_table(struct ovs_cmdl_context *ctx) | |
2281 | { | |
2282 | uint32_t usable_versions; | |
2283 | struct ofputil_table_mod tm; | |
2284 | struct vconn *vconn; | |
2285 | char *error; | |
2286 | int i; | |
2287 | ||
2288 | error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3], | |
2289 | &usable_versions); | |
2290 | if (error) { | |
2291 | ovs_fatal(0, "%s", error); | |
2292 | } | |
2293 | ||
2294 | uint32_t allowed_versions = get_allowed_ofp_versions(); | |
2295 | if (!(allowed_versions & usable_versions)) { | |
2296 | struct ds versions = DS_EMPTY_INITIALIZER; | |
2297 | ofputil_format_version_bitmap_names(&versions, usable_versions); | |
2298 | ovs_fatal(0, "table_mod '%s' requires one of the OpenFlow " | |
2299 | "versions %s", | |
2300 | ctx->argv[3], ds_cstr(&versions)); | |
2301 | } | |
2302 | mask_allowed_ofp_versions(usable_versions); | |
2303 | enum ofputil_protocol protocol = open_vconn(ctx->argv[1], &vconn); | |
2304 | ||
2305 | /* For OpenFlow 1.4+, ovs-ofctl mod-table should not affect table-config | |
2306 | * properties that the user didn't ask to change, so it is necessary to | |
2307 | * restore the current configuration of table-config parameters using | |
2308 | * OFPMP14_TABLE_DESC request. */ | |
2309 | if ((allowed_versions & (1u << OFP14_VERSION)) || | |
2310 | (allowed_versions & (1u << OFP15_VERSION))) { | |
2311 | struct ofputil_table_desc td; | |
2312 | ||
2313 | if (tm.table_id == OFPTT_ALL) { | |
2314 | for (i = 0; i < OFPTT_MAX; i++) { | |
2315 | tm.table_id = i; | |
2316 | fetch_table_desc(vconn, &tm, &td); | |
2317 | transact_noreply(vconn, | |
2318 | ofputil_encode_table_mod(&tm, protocol)); | |
2319 | } | |
2320 | } else { | |
2321 | fetch_table_desc(vconn, &tm, &td); | |
2322 | transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol)); | |
2323 | } | |
2324 | } else { | |
2325 | transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol)); | |
2326 | } | |
2327 | vconn_close(vconn); | |
2328 | } | |
2329 | ||
2330 | static void | |
2331 | ofctl_get_frags(struct ovs_cmdl_context *ctx) | |
2332 | { | |
2333 | struct ofputil_switch_config config; | |
2334 | struct vconn *vconn; | |
2335 | ||
2336 | open_vconn(ctx->argv[1], &vconn); | |
2337 | fetch_switch_config(vconn, &config); | |
2338 | puts(ofputil_frag_handling_to_string(config.frag)); | |
2339 | vconn_close(vconn); | |
2340 | } | |
2341 | ||
2342 | static void | |
2343 | ofctl_set_frags(struct ovs_cmdl_context *ctx) | |
2344 | { | |
2345 | struct ofputil_switch_config config; | |
2346 | enum ofputil_frag_handling frag; | |
2347 | struct vconn *vconn; | |
2348 | ||
2349 | if (!ofputil_frag_handling_from_string(ctx->argv[2], &frag)) { | |
2350 | ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]); | |
2351 | } | |
2352 | ||
2353 | open_vconn(ctx->argv[1], &vconn); | |
2354 | fetch_switch_config(vconn, &config); | |
2355 | if (frag != config.frag) { | |
2356 | /* Set the configuration. */ | |
2357 | config.frag = frag; | |
2358 | set_switch_config(vconn, &config); | |
2359 | ||
2360 | /* Then retrieve the configuration to see if it really took. OpenFlow | |
2361 | * has ill-defined error reporting for bad flags, so this is about the | |
2362 | * best we can do. */ | |
2363 | fetch_switch_config(vconn, &config); | |
2364 | if (frag != config.frag) { | |
2365 | ovs_fatal(0, "%s: setting fragment handling mode failed (this " | |
2366 | "switch probably doesn't support mode \"%s\")", | |
2367 | ctx->argv[1], ofputil_frag_handling_to_string(frag)); | |
2368 | } | |
2369 | } | |
2370 | vconn_close(vconn); | |
2371 | } | |
2372 | ||
2373 | static void | |
2374 | ofctl_ofp_parse(struct ovs_cmdl_context *ctx) | |
2375 | { | |
2376 | const char *filename = ctx->argv[1]; | |
2377 | struct ofpbuf b; | |
2378 | FILE *file; | |
2379 | ||
2380 | file = !strcmp(filename, "-") ? stdin : fopen(filename, "r"); | |
2381 | if (file == NULL) { | |
2382 | ovs_fatal(errno, "%s: open", filename); | |
2383 | } | |
2384 | ||
2385 | ofpbuf_init(&b, 65536); | |
2386 | for (;;) { | |
2387 | struct ofp_header *oh; | |
2388 | size_t length, tail_len; | |
2389 | void *tail; | |
2390 | size_t n; | |
2391 | ||
2392 | ofpbuf_clear(&b); | |
2393 | oh = ofpbuf_put_uninit(&b, sizeof *oh); | |
2394 | n = fread(oh, 1, sizeof *oh, file); | |
2395 | if (n == 0) { | |
2396 | break; | |
2397 | } else if (n < sizeof *oh) { | |
2398 | ovs_fatal(0, "%s: unexpected end of file mid-message", filename); | |
2399 | } | |
2400 | ||
2401 | length = ntohs(oh->length); | |
2402 | if (length < sizeof *oh) { | |
2403 | ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow", | |
2404 | filename, length); | |
2405 | } | |
2406 | ||
2407 | tail_len = length - sizeof *oh; | |
2408 | tail = ofpbuf_put_uninit(&b, tail_len); | |
2409 | n = fread(tail, 1, tail_len, file); | |
2410 | if (n < tail_len) { | |
2411 | ovs_fatal(0, "%s: unexpected end of file mid-message", filename); | |
2412 | } | |
2413 | ||
2414 | ofp_print(stdout, b.data, b.size, verbosity + 2); | |
2415 | } | |
2416 | ofpbuf_uninit(&b); | |
2417 | ||
2418 | if (file != stdin) { | |
2419 | fclose(file); | |
2420 | } | |
2421 | } | |
2422 | ||
2423 | static bool | |
2424 | is_openflow_port(ovs_be16 port_, char *ports[]) | |
2425 | { | |
2426 | uint16_t port = ntohs(port_); | |
2427 | if (ports[0]) { | |
2428 | int i; | |
2429 | ||
2430 | for (i = 0; ports[i]; i++) { | |
2431 | if (port == atoi(ports[i])) { | |
2432 | return true; | |
2433 | } | |
2434 | } | |
2435 | return false; | |
2436 | } else { | |
2437 | return port == OFP_PORT || port == OFP_OLD_PORT; | |
2438 | } | |
2439 | } | |
2440 | ||
2441 | static void | |
2442 | ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx) | |
2443 | { | |
2444 | struct tcp_reader *reader; | |
2445 | FILE *file; | |
2446 | int error; | |
2447 | bool first; | |
2448 | ||
2449 | file = ovs_pcap_open(ctx->argv[1], "rb"); | |
2450 | if (!file) { | |
2451 | ovs_fatal(errno, "%s: open failed", ctx->argv[1]); | |
2452 | } | |
2453 | ||
2454 | reader = tcp_reader_open(); | |
2455 | first = true; | |
2456 | for (;;) { | |
2457 | struct dp_packet *packet; | |
2458 | long long int when; | |
2459 | struct flow flow; | |
2460 | ||
2461 | error = ovs_pcap_read(file, &packet, &when); | |
2462 | if (error) { | |
2463 | break; | |
2464 | } | |
2465 | pkt_metadata_init(&packet->md, ODPP_NONE); | |
2466 | flow_extract(packet, &flow); | |
2467 | if (flow.dl_type == htons(ETH_TYPE_IP) | |
2468 | && flow.nw_proto == IPPROTO_TCP | |
2469 | && (is_openflow_port(flow.tp_src, ctx->argv + 2) || | |
2470 | is_openflow_port(flow.tp_dst, ctx->argv + 2))) { | |
2471 | struct dp_packet *payload = tcp_reader_run(reader, &flow, packet); | |
2472 | if (payload) { | |
2473 | while (dp_packet_size(payload) >= sizeof(struct ofp_header)) { | |
2474 | const struct ofp_header *oh; | |
2475 | void *data = dp_packet_data(payload); | |
2476 | int length; | |
2477 | ||
2478 | /* Align OpenFlow on 8-byte boundary for safe access. */ | |
2479 | dp_packet_shift(payload, -((intptr_t) data & 7)); | |
2480 | ||
2481 | oh = dp_packet_data(payload); | |
2482 | length = ntohs(oh->length); | |
2483 | if (dp_packet_size(payload) < length) { | |
2484 | break; | |
2485 | } | |
2486 | ||
2487 | if (!first) { | |
2488 | putchar('\n'); | |
2489 | } | |
2490 | first = false; | |
2491 | ||
2492 | if (timestamp) { | |
2493 | char *s = xastrftime_msec("%H:%M:%S.### ", when, true); | |
2494 | fputs(s, stdout); | |
2495 | free(s); | |
2496 | } | |
2497 | ||
2498 | printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n", | |
2499 | IP_ARGS(flow.nw_src), ntohs(flow.tp_src), | |
2500 | IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst)); | |
2501 | ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1); | |
2502 | dp_packet_pull(payload, length); | |
2503 | } | |
2504 | } | |
2505 | } | |
2506 | dp_packet_delete(packet); | |
2507 | } | |
2508 | tcp_reader_close(reader); | |
2509 | } | |
2510 | ||
2511 | static void | |
2512 | ofctl_ping(struct ovs_cmdl_context *ctx) | |
2513 | { | |
2514 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
2515 | unsigned int payload; | |
2516 | struct vconn *vconn; | |
2517 | int i; | |
2518 | ||
2519 | payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64; | |
2520 | if (payload > max_payload) { | |
2521 | ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); | |
2522 | } | |
2523 | ||
2524 | open_vconn(ctx->argv[1], &vconn); | |
2525 | for (i = 0; i < 10; i++) { | |
2526 | struct timeval start, end; | |
2527 | struct ofpbuf *request, *reply; | |
2528 | const struct ofp_header *rpy_hdr; | |
2529 | enum ofptype type; | |
2530 | ||
2531 | request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, | |
2532 | vconn_get_version(vconn), payload); | |
2533 | random_bytes(ofpbuf_put_uninit(request, payload), payload); | |
2534 | ||
2535 | xgettimeofday(&start); | |
2536 | run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact"); | |
2537 | xgettimeofday(&end); | |
2538 | ||
2539 | rpy_hdr = reply->data; | |
2540 | if (ofptype_pull(&type, reply) | |
2541 | || type != OFPTYPE_ECHO_REPLY | |
2542 | || reply->size != payload | |
2543 | || memcmp(request->msg, reply->msg, payload)) { | |
2544 | printf("Reply does not match request. Request:\n"); | |
2545 | ofp_print(stdout, request, request->size, verbosity + 2); | |
2546 | printf("Reply:\n"); | |
2547 | ofp_print(stdout, reply, reply->size, verbosity + 2); | |
2548 | } | |
2549 | printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", | |
2550 | reply->size, ctx->argv[1], ntohl(rpy_hdr->xid), | |
2551 | (1000*(double)(end.tv_sec - start.tv_sec)) | |
2552 | + (.001*(end.tv_usec - start.tv_usec))); | |
2553 | ofpbuf_delete(request); | |
2554 | ofpbuf_delete(reply); | |
2555 | } | |
2556 | vconn_close(vconn); | |
2557 | } | |
2558 | ||
2559 | static void | |
2560 | ofctl_benchmark(struct ovs_cmdl_context *ctx) | |
2561 | { | |
2562 | size_t max_payload = 65535 - sizeof(struct ofp_header); | |
2563 | struct timeval start, end; | |
2564 | unsigned int payload_size, message_size; | |
2565 | struct vconn *vconn; | |
2566 | double duration; | |
2567 | int count; | |
2568 | int i; | |
2569 | ||
2570 | payload_size = atoi(ctx->argv[2]); | |
2571 | if (payload_size > max_payload) { | |
2572 | ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); | |
2573 | } | |
2574 | message_size = sizeof(struct ofp_header) + payload_size; | |
2575 | ||
2576 | count = atoi(ctx->argv[3]); | |
2577 | ||
2578 | printf("Sending %d packets * %u bytes (with header) = %u bytes total\n", | |
2579 | count, message_size, count * message_size); | |
2580 | ||
2581 | open_vconn(ctx->argv[1], &vconn); | |
2582 | xgettimeofday(&start); | |
2583 | for (i = 0; i < count; i++) { | |
2584 | struct ofpbuf *request, *reply; | |
2585 | ||
2586 | request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST, | |
2587 | vconn_get_version(vconn), payload_size); | |
2588 | ofpbuf_put_zeros(request, payload_size); | |
2589 | run(vconn_transact(vconn, request, &reply), "transact"); | |
2590 | ofpbuf_delete(reply); | |
2591 | } | |
2592 | xgettimeofday(&end); | |
2593 | vconn_close(vconn); | |
2594 | ||
2595 | duration = ((1000*(double)(end.tv_sec - start.tv_sec)) | |
2596 | + (.001*(end.tv_usec - start.tv_usec))); | |
2597 | printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n", | |
2598 | duration, count / (duration / 1000.0), | |
2599 | count * message_size / (duration / 1000.0)); | |
2600 | } | |
2601 | ||
2602 | static void | |
2603 | ofctl_dump_ipfix_bridge(struct ovs_cmdl_context *ctx) | |
2604 | { | |
2605 | dump_trivial_transaction(ctx->argv[1], OFPRAW_NXST_IPFIX_BRIDGE_REQUEST); | |
2606 | } | |
2607 | ||
2608 | static void | |
2609 | ofctl_dump_ipfix_flow(struct ovs_cmdl_context *ctx) | |
2610 | { | |
2611 | dump_trivial_transaction(ctx->argv[1], OFPRAW_NXST_IPFIX_FLOW_REQUEST); | |
2612 | } | |
2613 | ||
2614 | static void | |
2615 | bundle_group_mod__(const char *remote, struct ofputil_group_mod *gms, | |
2616 | size_t n_gms, enum ofputil_protocol usable_protocols) | |
2617 | { | |
2618 | enum ofputil_protocol protocol; | |
2619 | enum ofp_version version; | |
2620 | struct vconn *vconn; | |
2621 | struct ovs_list requests; | |
2622 | size_t i; | |
2623 | ||
2624 | ovs_list_init(&requests); | |
2625 | ||
2626 | /* Bundles need OpenFlow 1.3+. */ | |
2627 | usable_protocols &= OFPUTIL_P_OF13_UP; | |
2628 | protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols); | |
2629 | version = ofputil_protocol_to_ofp_version(protocol); | |
2630 | ||
2631 | for (i = 0; i < n_gms; i++) { | |
2632 | struct ofputil_group_mod *gm = &gms[i]; | |
2633 | struct ofpbuf *request = ofputil_encode_group_mod(version, gm); | |
2634 | ||
2635 | ovs_list_push_back(&requests, &request->list_node); | |
2636 | ofputil_uninit_group_mod(gm); | |
2637 | } | |
2638 | ||
2639 | bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC); | |
2640 | ofpbuf_list_delete(&requests); | |
2641 | vconn_close(vconn); | |
2642 | } | |
2643 | ||
2644 | static void | |
2645 | ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms, | |
2646 | size_t n_gms, enum ofputil_protocol usable_protocols) | |
2647 | { | |
2648 | enum ofputil_protocol protocol; | |
2649 | struct ofputil_group_mod *gm; | |
2650 | enum ofp_version version; | |
2651 | struct ofpbuf *request; | |
2652 | ||
2653 | struct vconn *vconn; | |
2654 | size_t i; | |
2655 | ||
2656 | if (bundle) { | |
2657 | bundle_group_mod__(remote, gms, n_gms, usable_protocols); | |
2658 | return; | |
2659 | } | |
2660 | ||
2661 | protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols); | |
2662 | version = ofputil_protocol_to_ofp_version(protocol); | |
2663 | ||
2664 | for (i = 0; i < n_gms; i++) { | |
2665 | gm = &gms[i]; | |
2666 | request = ofputil_encode_group_mod(version, gm); | |
2667 | transact_noreply(vconn, request); | |
2668 | ofputil_uninit_group_mod(gm); | |
2669 | } | |
2670 | ||
2671 | vconn_close(vconn); | |
2672 | } | |
2673 | ||
2674 | static void | |
2675 | ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], int command) | |
2676 | { | |
2677 | struct ofputil_group_mod *gms = NULL; | |
2678 | enum ofputil_protocol usable_protocols; | |
2679 | size_t n_gms = 0; | |
2680 | char *error; | |
2681 | ||
2682 | if (command == OFPGC11_ADD) { | |
2683 | /* Allow the file to specify a mix of commands. If none specified at | |
2684 | * the beginning of any given line, then the default is OFPGC11_ADD, so | |
2685 | * this is backwards compatible. */ | |
2686 | command = -2; | |
2687 | } | |
2688 | error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms, | |
2689 | &usable_protocols); | |
2690 | if (error) { | |
2691 | ovs_fatal(0, "%s", error); | |
2692 | } | |
2693 | ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols); | |
2694 | free(gms); | |
2695 | } | |
2696 | ||
2697 | static void | |
2698 | ofctl_group_mod(int argc, char *argv[], uint16_t command) | |
2699 | { | |
2700 | if (argc > 2 && !strcmp(argv[2], "-")) { | |
2701 | ofctl_group_mod_file(argc, argv, command); | |
2702 | } else { | |
2703 | enum ofputil_protocol usable_protocols; | |
2704 | struct ofputil_group_mod gm; | |
2705 | char *error; | |
2706 | ||
2707 | error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "", | |
2708 | &usable_protocols); | |
2709 | if (error) { | |
2710 | ovs_fatal(0, "%s", error); | |
2711 | } | |
2712 | ofctl_group_mod__(argv[1], &gm, 1, usable_protocols); | |
2713 | } | |
2714 | } | |
2715 | ||
2716 | static void | |
2717 | ofctl_add_group(struct ovs_cmdl_context *ctx) | |
2718 | { | |
2719 | ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD); | |
2720 | } | |
2721 | ||
2722 | static void | |
2723 | ofctl_add_groups(struct ovs_cmdl_context *ctx) | |
2724 | { | |
2725 | ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD); | |
2726 | } | |
2727 | ||
2728 | static void | |
2729 | ofctl_mod_group(struct ovs_cmdl_context *ctx) | |
2730 | { | |
2731 | ofctl_group_mod(ctx->argc, ctx->argv, | |
2732 | may_create ? OFPGC11_ADD_OR_MOD : OFPGC11_MODIFY); | |
2733 | } | |
2734 | ||
2735 | static void | |
2736 | ofctl_del_groups(struct ovs_cmdl_context *ctx) | |
2737 | { | |
2738 | ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE); | |
2739 | } | |
2740 | ||
2741 | static void | |
2742 | ofctl_insert_bucket(struct ovs_cmdl_context *ctx) | |
2743 | { | |
2744 | ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET); | |
2745 | } | |
2746 | ||
2747 | static void | |
2748 | ofctl_remove_bucket(struct ovs_cmdl_context *ctx) | |
2749 | { | |
2750 | ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET); | |
2751 | } | |
2752 | ||
2753 | static void | |
2754 | ofctl_dump_group_stats(struct ovs_cmdl_context *ctx) | |
2755 | { | |
2756 | enum ofputil_protocol usable_protocols; | |
2757 | struct ofputil_group_mod gm; | |
2758 | struct ofpbuf *request; | |
2759 | struct vconn *vconn; | |
2760 | uint32_t group_id; | |
2761 | char *error; | |
2762 | ||
2763 | memset(&gm, 0, sizeof gm); | |
2764 | ||
2765 | error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE, | |
2766 | ctx->argc > 2 ? ctx->argv[2] : "", | |
2767 | &usable_protocols); | |
2768 | if (error) { | |
2769 | ovs_fatal(0, "%s", error); | |
2770 | } | |
2771 | ||
2772 | group_id = gm.group_id; | |
2773 | ||
2774 | open_vconn(ctx->argv[1], &vconn); | |
2775 | request = ofputil_encode_group_stats_request(vconn_get_version(vconn), | |
2776 | group_id); | |
2777 | if (request) { | |
2778 | dump_transaction(vconn, request); | |
2779 | } | |
2780 | ||
2781 | vconn_close(vconn); | |
2782 | } | |
2783 | ||
2784 | static void | |
2785 | ofctl_dump_group_desc(struct ovs_cmdl_context *ctx) | |
2786 | { | |
2787 | struct ofpbuf *request; | |
2788 | struct vconn *vconn; | |
2789 | uint32_t group_id; | |
2790 | ||
2791 | open_vconn(ctx->argv[1], &vconn); | |
2792 | ||
2793 | if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) { | |
2794 | group_id = OFPG_ALL; | |
2795 | } | |
2796 | ||
2797 | request = ofputil_encode_group_desc_request(vconn_get_version(vconn), | |
2798 | group_id); | |
2799 | if (request) { | |
2800 | dump_transaction(vconn, request); | |
2801 | } | |
2802 | ||
2803 | vconn_close(vconn); | |
2804 | } | |
2805 | ||
2806 | static void | |
2807 | ofctl_dump_group_features(struct ovs_cmdl_context *ctx) | |
2808 | { | |
2809 | struct ofpbuf *request; | |
2810 | struct vconn *vconn; | |
2811 | ||
2812 | open_vconn(ctx->argv[1], &vconn); | |
2813 | request = ofputil_encode_group_features_request(vconn_get_version(vconn)); | |
2814 | if (request) { | |
2815 | dump_transaction(vconn, request); | |
2816 | } | |
2817 | ||
2818 | vconn_close(vconn); | |
2819 | } | |
2820 | ||
2821 | static void | |
2822 | ofctl_bundle(struct ovs_cmdl_context *ctx) | |
2823 | { | |
2824 | enum ofputil_protocol protocol, usable_protocols; | |
2825 | struct ofputil_bundle_msg *bms; | |
2826 | struct ovs_list requests; | |
2827 | struct vconn *vconn; | |
2828 | size_t n_bms; | |
2829 | char *error; | |
2830 | ||
2831 | error = parse_ofp_bundle_file(ctx->argv[2], &bms, &n_bms, | |
2832 | &usable_protocols); | |
2833 | if (error) { | |
2834 | ovs_fatal(0, "%s", error); | |
2835 | } | |
2836 | ||
2837 | /* Implicit OpenFlow 1.4. */ | |
2838 | if (!(get_allowed_ofp_versions() & | |
2839 | ofputil_protocols_to_version_bitmap(OFPUTIL_P_OF13_UP))) { | |
2840 | ||
2841 | /* Add implicit allowance for OpenFlow 1.4. */ | |
2842 | add_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( | |
2843 | OFPUTIL_P_OF14_OXM)); | |
2844 | /* Remove all versions that do not support bundles. */ | |
2845 | mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap( | |
2846 | OFPUTIL_P_OF13_UP)); | |
2847 | allowed_protocols = ofputil_protocols_from_version_bitmap( | |
2848 | get_allowed_ofp_versions()); | |
2849 | } | |
2850 | ||
2851 | /* Bundles need OpenFlow 1.3+. */ | |
2852 | usable_protocols &= OFPUTIL_P_OF13_UP; | |
2853 | protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols); | |
2854 | ||
2855 | ovs_list_init(&requests); | |
2856 | ofputil_encode_bundle_msgs(bms, n_bms, &requests, protocol); | |
2857 | ofputil_free_bundle_msgs(bms, n_bms); | |
2858 | bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC); | |
2859 | ofpbuf_list_delete(&requests); | |
2860 | ||
2861 | vconn_close(vconn); | |
2862 | } | |
2863 | ||
2864 | static void | |
2865 | ofctl_tlv_mod(struct ovs_cmdl_context *ctx, uint16_t command) | |
2866 | { | |
2867 | enum ofputil_protocol usable_protocols; | |
2868 | enum ofputil_protocol protocol; | |
2869 | struct ofputil_tlv_table_mod ttm; | |
2870 | char *error; | |
2871 | enum ofp_version version; | |
2872 | struct ofpbuf *request; | |
2873 | struct vconn *vconn; | |
2874 | ||
2875 | error = parse_ofp_tlv_table_mod_str(&ttm, command, ctx->argc > 2 ? | |
2876 | ctx->argv[2] : "", | |
2877 | &usable_protocols); | |
2878 | if (error) { | |
2879 | ovs_fatal(0, "%s", error); | |
2880 | } | |
2881 | ||
2882 | protocol = open_vconn_for_flow_mod(ctx->argv[1], &vconn, usable_protocols); | |
2883 | version = ofputil_protocol_to_ofp_version(protocol); | |
2884 | ||
2885 | request = ofputil_encode_tlv_table_mod(version, &ttm); | |
2886 | if (request) { | |
2887 | transact_noreply(vconn, request); | |
2888 | } | |
2889 | ||
2890 | vconn_close(vconn); | |
2891 | ofputil_uninit_tlv_table(&ttm.mappings); | |
2892 | } | |
2893 | ||
2894 | static void | |
2895 | ofctl_add_tlv_map(struct ovs_cmdl_context *ctx) | |
2896 | { | |
2897 | ofctl_tlv_mod(ctx, NXTTMC_ADD); | |
2898 | } | |
2899 | ||
2900 | static void | |
2901 | ofctl_del_tlv_map(struct ovs_cmdl_context *ctx) | |
2902 | { | |
2903 | ofctl_tlv_mod(ctx, ctx->argc > 2 ? NXTTMC_DELETE : NXTTMC_CLEAR); | |
2904 | } | |
2905 | ||
2906 | static void | |
2907 | ofctl_dump_tlv_map(struct ovs_cmdl_context *ctx) | |
2908 | { | |
2909 | dump_trivial_transaction(ctx->argv[1], OFPRAW_NXT_TLV_TABLE_REQUEST); | |
2910 | } | |
2911 | ||
2912 | static void | |
2913 | ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED) | |
2914 | { | |
2915 | usage(); | |
2916 | } | |
2917 | ||
2918 | static void | |
2919 | ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED) | |
2920 | { | |
2921 | ovs_cmdl_print_commands(get_all_commands()); | |
2922 | } | |
2923 | \f | |
2924 | /* replace-flows and diff-flows commands. */ | |
2925 | ||
2926 | struct flow_tables { | |
2927 | struct classifier tables[OFPTT_MAX + 1]; | |
2928 | }; | |
2929 | ||
2930 | #define FOR_EACH_TABLE(CLS, TABLES) \ | |
2931 | for ((CLS) = (TABLES)->tables; \ | |
2932 | (CLS) < &(TABLES)->tables[ARRAY_SIZE((TABLES)->tables)]; \ | |
2933 | (CLS)++) | |
2934 | ||
2935 | static void | |
2936 | flow_tables_init(struct flow_tables *tables) | |
2937 | { | |
2938 | struct classifier *cls; | |
2939 | ||
2940 | FOR_EACH_TABLE (cls, tables) { | |
2941 | classifier_init(cls, NULL); | |
2942 | } | |
2943 | } | |
2944 | ||
2945 | static void | |
2946 | flow_tables_defer(struct flow_tables *tables) | |
2947 | { | |
2948 | struct classifier *cls; | |
2949 | ||
2950 | FOR_EACH_TABLE (cls, tables) { | |
2951 | classifier_defer(cls); | |
2952 | } | |
2953 | } | |
2954 | ||
2955 | static void | |
2956 | flow_tables_publish(struct flow_tables *tables) | |
2957 | { | |
2958 | struct classifier *cls; | |
2959 | ||
2960 | FOR_EACH_TABLE (cls, tables) { | |
2961 | classifier_publish(cls); | |
2962 | } | |
2963 | } | |
2964 | ||
2965 | /* A flow table entry, possibly with two different versions. */ | |
2966 | struct fte { | |
2967 | struct cls_rule rule; /* Within a "struct classifier". */ | |
2968 | struct fte_version *versions[2]; | |
2969 | }; | |
2970 | ||
2971 | /* One version of a Flow Table Entry. */ | |
2972 | struct fte_version { | |
2973 | ovs_be64 cookie; | |
2974 | uint16_t idle_timeout; | |
2975 | uint16_t hard_timeout; | |
2976 | uint16_t importance; | |
2977 | uint16_t flags; | |
2978 | struct ofpact *ofpacts; | |
2979 | size_t ofpacts_len; | |
2980 | uint8_t table_id; | |
2981 | }; | |
2982 | ||
2983 | /* Frees 'version' and the data that it owns. */ | |
2984 | static void | |
2985 | fte_version_free(struct fte_version *version) | |
2986 | { | |
2987 | if (version) { | |
2988 | free(CONST_CAST(struct ofpact *, version->ofpacts)); | |
2989 | free(version); | |
2990 | } | |
2991 | } | |
2992 | ||
2993 | /* Returns true if 'a' and 'b' are the same, false if they differ. | |
2994 | * | |
2995 | * Ignores differences in 'flags' because there's no way to retrieve flags from | |
2996 | * an OpenFlow switch. We have to assume that they are the same. */ | |
2997 | static bool | |
2998 | fte_version_equals(const struct fte_version *a, const struct fte_version *b) | |
2999 | { | |
3000 | return (a->cookie == b->cookie | |
3001 | && a->idle_timeout == b->idle_timeout | |
3002 | && a->hard_timeout == b->hard_timeout | |
3003 | && a->importance == b->importance | |
3004 | && a->table_id == b->table_id | |
3005 | && ofpacts_equal(a->ofpacts, a->ofpacts_len, | |
3006 | b->ofpacts, b->ofpacts_len)); | |
3007 | } | |
3008 | ||
3009 | /* Clears 's', then if 's' has a version 'index', formats 'fte' and version | |
3010 | * 'index' into 's', followed by a new-line. */ | |
3011 | static void | |
3012 | fte_version_format(const struct fte *fte, int index, struct ds *s) | |
3013 | { | |
3014 | const struct fte_version *version = fte->versions[index]; | |
3015 | ||
3016 | ds_clear(s); | |
3017 | if (!version) { | |
3018 | return; | |
3019 | } | |
3020 | ||
3021 | if (version->table_id) { | |
3022 | ds_put_format(s, "table=%"PRIu8" ", version->table_id); | |
3023 | } | |
3024 | cls_rule_format(&fte->rule, s); | |
3025 | if (version->cookie != htonll(0)) { | |
3026 | ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie)); | |
3027 | } | |
3028 | if (version->idle_timeout != OFP_FLOW_PERMANENT) { | |
3029 | ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout); | |
3030 | } | |
3031 | if (version->hard_timeout != OFP_FLOW_PERMANENT) { | |
3032 | ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout); | |
3033 | } | |
3034 | if (version->importance != 0) { | |
3035 | ds_put_format(s, " importance=%"PRIu16, version->importance); | |
3036 | } | |
3037 | ||
3038 | ds_put_cstr(s, " actions="); | |
3039 | ofpacts_format(version->ofpacts, version->ofpacts_len, s); | |
3040 | ||
3041 | ds_put_char(s, '\n'); | |
3042 | } | |
3043 | ||
3044 | static struct fte * | |
3045 | fte_from_cls_rule(const struct cls_rule *cls_rule) | |
3046 | { | |
3047 | return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL; | |
3048 | } | |
3049 | ||
3050 | /* Frees 'fte' and its versions. */ | |
3051 | static void | |
3052 | fte_free(struct fte *fte) | |
3053 | { | |
3054 | if (fte) { | |
3055 | fte_version_free(fte->versions[0]); | |
3056 | fte_version_free(fte->versions[1]); | |
3057 | cls_rule_destroy(&fte->rule); | |
3058 | free(fte); | |
3059 | } | |
3060 | } | |
3061 | ||
3062 | /* Frees all of the FTEs within 'tables'. */ | |
3063 | static void | |
3064 | fte_free_all(struct flow_tables *tables) | |
3065 | { | |
3066 | struct classifier *cls; | |
3067 | ||
3068 | FOR_EACH_TABLE (cls, tables) { | |
3069 | struct fte *fte; | |
3070 | ||
3071 | classifier_defer(cls); | |
3072 | CLS_FOR_EACH (fte, rule, cls) { | |
3073 | classifier_remove(cls, &fte->rule); | |
3074 | ovsrcu_postpone(fte_free, fte); | |
3075 | } | |
3076 | classifier_destroy(cls); | |
3077 | } | |
3078 | } | |
3079 | ||
3080 | /* Searches 'tables' for an FTE matching 'rule', inserting a new one if | |
3081 | * necessary. Sets 'version' as the version of that rule with the given | |
3082 | * 'index', replacing any existing version, if any. | |
3083 | * | |
3084 | * Takes ownership of 'version'. */ | |
3085 | static void | |
3086 | fte_insert(struct flow_tables *tables, const struct match *match, | |
3087 | int priority, struct fte_version *version, int index) | |
3088 | { | |
3089 | struct classifier *cls = &tables->tables[version->table_id]; | |
3090 | struct fte *old, *fte; | |
3091 | ||
3092 | fte = xzalloc(sizeof *fte); | |
3093 | cls_rule_init(&fte->rule, match, priority); | |
3094 | fte->versions[index] = version; | |
3095 | ||
3096 | old = fte_from_cls_rule(classifier_replace(cls, &fte->rule, | |
3097 | OVS_VERSION_MIN, NULL, 0)); | |
3098 | if (old) { | |
3099 | fte->versions[!index] = old->versions[!index]; | |
3100 | old->versions[!index] = NULL; | |
3101 | ||
3102 | ovsrcu_postpone(fte_free, old); | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | /* A FTE entry that has been queued for later insertion after all | |
3107 | * flows have been scanned to correctly allocation tunnel metadata. */ | |
3108 | struct fte_pending { | |
3109 | struct match *match; | |
3110 | int priority; | |
3111 | struct fte_version *version; | |
3112 | int index; | |
3113 | ||
3114 | struct ovs_list list_node; | |
3115 | }; | |
3116 | ||
3117 | /* Processing state during two stage processing of flow table entries. | |
3118 | * Tracks the maximum size seen for each tunnel metadata entry as well | |
3119 | * as a list of the pending FTE entries. */ | |
3120 | struct fte_state { | |
3121 | int tun_metadata_size[TUN_METADATA_NUM_OPTS]; | |
3122 | struct ovs_list fte_pending_list; | |
3123 | }; | |
3124 | ||
3125 | /* Given a list of the field sizes for each tunnel metadata entry, install | |
3126 | * a mapping table for later operations. */ | |
3127 | static void | |
3128 | generate_tun_metadata(struct fte_state *state) | |
3129 | { | |
3130 | struct ofputil_tlv_table_mod ttm; | |
3131 | int i; | |
3132 | ||
3133 | ttm.command = NXTTMC_ADD; | |
3134 | ovs_list_init(&ttm.mappings); | |
3135 | ||
3136 | for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) { | |
3137 | if (state->tun_metadata_size[i] != -1) { | |
3138 | struct ofputil_tlv_map *map = xmalloc(sizeof *map); | |
3139 | ||
3140 | ovs_list_push_back(&ttm.mappings, &map->list_node); | |
3141 | ||
3142 | /* We don't care about the actual option class and type since there | |
3143 | * won't be any lookup. We just need to make them unique. */ | |
3144 | map->option_class = i / UINT8_MAX; | |
3145 | map->option_type = i; | |
3146 | map->option_len = ROUND_UP(state->tun_metadata_size[i], 4); | |
3147 | map->index = i; | |
3148 | } | |
3149 | } | |
3150 | ||
3151 | tun_metadata_table_mod(&ttm); | |
3152 | ofputil_uninit_tlv_table(&ttm.mappings); | |
3153 | } | |
3154 | ||
3155 | /* Once we have created a tunnel mapping table with a consistent overall | |
3156 | * allocation, we need to remap each flow to use this table from its own | |
3157 | * allocation. Since the mapping table has already been installed, we | |
3158 | * can just read the data from the match and rewrite it. On rewrite, it | |
3159 | * will use the new table. */ | |
3160 | static void | |
3161 | remap_match(struct match *match) | |
3162 | { | |
3163 | int i; | |
3164 | ||
3165 | if (!match->tun_md.valid) { | |
3166 | return; | |
3167 | } | |
3168 | ||
3169 | struct tun_metadata flow = match->flow.tunnel.metadata; | |
3170 | struct tun_metadata flow_mask = match->wc.masks.tunnel.metadata; | |
3171 | memset(&match->flow.tunnel.metadata, 0, sizeof match->flow.tunnel.metadata); | |
3172 | memset(&match->wc.masks.tunnel.metadata, 0, | |
3173 | sizeof match->wc.masks.tunnel.metadata); | |
3174 | match->tun_md.valid = false; | |
3175 | ||
3176 | ULLONG_FOR_EACH_1 (i, flow_mask.present.map) { | |
3177 | const struct mf_field *field = mf_from_id(MFF_TUN_METADATA0 + i); | |
3178 | int offset = match->tun_md.entry[i].loc.c.offset; | |
3179 | int len = match->tun_md.entry[i].loc.len; | |
3180 | union mf_value value, mask; | |
3181 | ||
3182 | memset(&value, 0, field->n_bytes - len); | |
3183 | memset(&mask, match->tun_md.entry[i].masked ? 0 : 0xff, | |
3184 | field->n_bytes - len); | |
3185 | ||
3186 | memcpy(value.tun_metadata + field->n_bytes - len, | |
3187 | flow.opts.u8 + offset, len); | |
3188 | memcpy(mask.tun_metadata + field->n_bytes - len, | |
3189 | flow_mask.opts.u8 + offset, len); | |
3190 | mf_set(field, &value, &mask, match, NULL); | |
3191 | } | |
3192 | } | |
3193 | ||
3194 | /* In order to correctly handle tunnel metadata, we need to have | |
3195 | * two passes over the flows. This happens because tunnel metadata | |
3196 | * doesn't have fixed locations in a flow entry but is instead dynamically | |
3197 | * allocated space. In the case of flows coming from a file, we don't | |
3198 | * even know the size of each field when we need to do the allocation. | |
3199 | * When the flows come in, each flow has an individual allocation based | |
3200 | * on its own fields. However, this allocation is not the same across | |
3201 | * different flows and therefore fields are not directly comparable. | |
3202 | * | |
3203 | * In the first pass, we record the maximum size of each tunnel metadata | |
3204 | * field as well as queue FTE entries for later processing. | |
3205 | * | |
3206 | * In the second pass, we use the metadata size information to create a | |
3207 | * tunnel mapping table and set that through the tunnel metadata processing | |
3208 | * code. We then remap all individual flows to use this common allocation | |
3209 | * scheme. Finally, we load the queued entries into the classifier for | |
3210 | * comparison. | |
3211 | * | |
3212 | * fte_state_init() should be called before processing any flows. */ | |
3213 | static void | |
3214 | fte_state_init(struct fte_state *state) | |
3215 | { | |
3216 | int i; | |
3217 | ||
3218 | for (i = 0; i < TUN_METADATA_NUM_OPTS; i++) { | |
3219 | state->tun_metadata_size[i] = -1; | |
3220 | } | |
3221 | ||
3222 | ovs_list_init(&state->fte_pending_list); | |
3223 | } | |
3224 | ||
3225 | /* The first pass of the processing described in the comment about | |
3226 | * fte_state_init(). fte_queue() is the first pass to be called as each | |
3227 | * flow is read from its source. */ | |
3228 | static void | |
3229 | fte_queue(struct fte_state *state, const struct match *match, | |
3230 | int priority, struct fte_version *version, int index) | |
3231 | { | |
3232 | struct fte_pending *pending = xmalloc(sizeof *pending); | |
3233 | int i; | |
3234 | ||
3235 | pending->match = xmemdup(match, sizeof *match); | |
3236 | pending->priority = priority; | |
3237 | pending->version = version; | |
3238 | pending->index = index; | |
3239 | ovs_list_push_back(&state->fte_pending_list, &pending->list_node); | |
3240 | ||
3241 | if (!match->tun_md.valid) { | |
3242 | return; | |
3243 | } | |
3244 | ||
3245 | ULLONG_FOR_EACH_1 (i, match->wc.masks.tunnel.metadata.present.map) { | |
3246 | if (match->tun_md.entry[i].loc.len > state->tun_metadata_size[i]) { | |
3247 | state->tun_metadata_size[i] = match->tun_md.entry[i].loc.len; | |
3248 | } | |
3249 | } | |
3250 | } | |
3251 | ||
3252 | /* The second pass of the processing described in the comment about | |
3253 | * fte_state_init(). This should be called once all flows (from both | |
3254 | * sides of the comparison) have been added through fte_queue(). */ | |
3255 | static void | |
3256 | fte_fill(struct fte_state *state, struct flow_tables *tables) | |
3257 | { | |
3258 | struct fte_pending *pending; | |
3259 | ||
3260 | generate_tun_metadata(state); | |
3261 | ||
3262 | flow_tables_init(tables); | |
3263 | flow_tables_defer(tables); | |
3264 | ||
3265 | LIST_FOR_EACH_POP(pending, list_node, &state->fte_pending_list) { | |
3266 | remap_match(pending->match); | |
3267 | fte_insert(tables, pending->match, pending->priority, pending->version, | |
3268 | pending->index); | |
3269 | free(pending->match); | |
3270 | free(pending); | |
3271 | } | |
3272 | ||
3273 | flow_tables_publish(tables); | |
3274 | } | |
3275 | ||
3276 | /* Reads the flows in 'filename' as flow table entries in 'tables' for the | |
3277 | * version with the specified 'index'. Returns the flow formats able to | |
3278 | * represent the flows that were read. */ | |
3279 | static enum ofputil_protocol | |
3280 | read_flows_from_file(const char *filename, struct fte_state *state, int index) | |
3281 | { | |
3282 | enum ofputil_protocol usable_protocols; | |
3283 | int line_number; | |
3284 | struct ds s; | |
3285 | FILE *file; | |
3286 | ||
3287 | file = !strcmp(filename, "-") ? stdin : fopen(filename, "r"); | |
3288 | if (file == NULL) { | |
3289 | ovs_fatal(errno, "%s: open", filename); | |
3290 | } | |
3291 | ||
3292 | ds_init(&s); | |
3293 | usable_protocols = OFPUTIL_P_ANY; | |
3294 | line_number = 0; | |
3295 | while (!ds_get_preprocessed_line(&s, file, &line_number)) { | |
3296 | struct fte_version *version; | |
3297 | struct ofputil_flow_mod fm; | |
3298 | char *error; | |
3299 | enum ofputil_protocol usable; | |
3300 | ||
3301 | error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable); | |
3302 | if (error) { | |
3303 | ovs_fatal(0, "%s:%d: %s", filename, line_number, error); | |
3304 | } | |
3305 | usable_protocols &= usable; | |
3306 | ||
3307 | version = xmalloc(sizeof *version); | |
3308 | version->cookie = fm.new_cookie; | |
3309 | version->idle_timeout = fm.idle_timeout; | |
3310 | version->hard_timeout = fm.hard_timeout; | |
3311 | version->importance = fm.importance; | |
3312 | version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM | |
3313 | | OFPUTIL_FF_EMERG); | |
3314 | version->ofpacts = fm.ofpacts; | |
3315 | version->ofpacts_len = fm.ofpacts_len; | |
3316 | version->table_id = fm.table_id != OFPTT_ALL ? fm.table_id : 0; | |
3317 | ||
3318 | fte_queue(state, &fm.match, fm.priority, version, index); | |
3319 | } | |
3320 | ds_destroy(&s); | |
3321 | ||
3322 | if (file != stdin) { | |
3323 | fclose(file); | |
3324 | } | |
3325 | ||
3326 | return usable_protocols; | |
3327 | } | |
3328 | ||
3329 | static bool | |
3330 | recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid, | |
3331 | struct ofpbuf **replyp, | |
3332 | struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts) | |
3333 | { | |
3334 | struct ofpbuf *reply = *replyp; | |
3335 | ||
3336 | for (;;) { | |
3337 | int retval; | |
3338 | bool more; | |
3339 | ||
3340 | /* Get a flow stats reply message, if we don't already have one. */ | |
3341 | if (!reply) { | |
3342 | enum ofptype type; | |
3343 | enum ofperr error; | |
3344 | ||
3345 | do { | |
3346 | run(vconn_recv_block(vconn, &reply), | |
3347 | "OpenFlow packet receive failed"); | |
3348 | } while (((struct ofp_header *) reply->data)->xid != send_xid); | |
3349 | ||
3350 | error = ofptype_decode(&type, reply->data); | |
3351 | if (error || type != OFPTYPE_FLOW_STATS_REPLY) { | |
3352 | ovs_fatal(0, "received bad reply: %s", | |
3353 | ofp_to_string(reply->data, reply->size, | |
3354 | verbosity + 1)); | |
3355 | } | |
3356 | } | |
3357 | ||
3358 | /* Pull an individual flow stats reply out of the message. */ | |
3359 | retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts); | |
3360 | switch (retval) { | |
3361 | case 0: | |
3362 | *replyp = reply; | |
3363 | return true; | |
3364 | ||
3365 | case EOF: | |
3366 | more = ofpmp_more(reply->header); | |
3367 | ofpbuf_delete(reply); | |
3368 | reply = NULL; | |
3369 | if (!more) { | |
3370 | *replyp = NULL; | |
3371 | return false; | |
3372 | } | |
3373 | break; | |
3374 | ||
3375 | default: | |
3376 | ovs_fatal(0, "parse error in reply (%s)", | |
3377 | ofperr_to_string(retval)); | |
3378 | } | |
3379 | } | |
3380 | } | |
3381 | ||
3382 | /* Reads the OpenFlow flow table from 'vconn', which has currently active flow | |
3383 | * format 'protocol', and adds them as flow table entries in 'tables' for the | |
3384 | * version with the specified 'index'. */ | |
3385 | static void | |
3386 | read_flows_from_switch(struct vconn *vconn, | |
3387 | enum ofputil_protocol protocol, | |
3388 | struct fte_state *state, int index) | |
3389 | { | |
3390 | struct ofputil_flow_stats_request fsr; | |
3391 | struct ofputil_flow_stats fs; | |
3392 | struct ofpbuf *request; | |
3393 | struct ofpbuf ofpacts; | |
3394 | struct ofpbuf *reply; | |
3395 | ovs_be32 send_xid; | |
3396 | ||
3397 | fsr.aggregate = false; | |
3398 | match_init_catchall(&fsr.match); | |
3399 | fsr.out_port = OFPP_ANY; | |
3400 | fsr.out_group = OFPG_ANY; | |
3401 | fsr.table_id = 0xff; | |
3402 | fsr.cookie = fsr.cookie_mask = htonll(0); | |
3403 | request = ofputil_encode_flow_stats_request(&fsr, protocol); | |
3404 | send_xid = ((struct ofp_header *) request->data)->xid; | |
3405 | send_openflow_buffer(vconn, request); | |
3406 | ||
3407 | reply = NULL; | |
3408 | ofpbuf_init(&ofpacts, 0); | |
3409 | while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) { | |
3410 | struct fte_version *version; | |
3411 | ||
3412 | version = xmalloc(sizeof *version); | |
3413 | version->cookie = fs.cookie; | |
3414 | version->idle_timeout = fs.idle_timeout; | |
3415 | version->hard_timeout = fs.hard_timeout; | |
3416 | version->importance = fs.importance; | |
3417 | version->flags = 0; | |
3418 | version->ofpacts_len = fs.ofpacts_len; | |
3419 | version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len); | |
3420 | version->table_id = fs.table_id; | |
3421 | ||
3422 | fte_queue(state, &fs.match, fs.priority, version, index); | |
3423 | } | |
3424 | ofpbuf_uninit(&ofpacts); | |
3425 | } | |
3426 | ||
3427 | static void | |
3428 | fte_make_flow_mod(const struct fte *fte, int index, uint16_t command, | |
3429 | enum ofputil_protocol protocol, struct ovs_list *packets) | |
3430 | { | |
3431 | const struct fte_version *version = fte->versions[index]; | |
3432 | struct ofpbuf *ofm; | |
3433 | ||
3434 | struct ofputil_flow_mod fm = { | |
3435 | .priority = fte->rule.priority, | |
3436 | .new_cookie = version->cookie, | |
3437 | .modify_cookie = true, | |
3438 | .table_id = version->table_id, | |
3439 | .command = command, | |
3440 | .idle_timeout = version->idle_timeout, | |
3441 | .hard_timeout = version->hard_timeout, | |
3442 | .importance = version->importance, | |
3443 | .buffer_id = UINT32_MAX, | |
3444 | .out_port = OFPP_ANY, | |
3445 | .out_group = OFPG_ANY, | |
3446 | .flags = version->flags, | |
3447 | }; | |
3448 | minimatch_expand(&fte->rule.match, &fm.match); | |
3449 | if (command == OFPFC_ADD || command == OFPFC_MODIFY || | |
3450 | command == OFPFC_MODIFY_STRICT) { | |
3451 | fm.ofpacts = version->ofpacts; | |
3452 | fm.ofpacts_len = version->ofpacts_len; | |
3453 | } else { | |
3454 | fm.ofpacts = NULL; | |
3455 | fm.ofpacts_len = 0; | |
3456 | } | |
3457 | ||
3458 | ofm = ofputil_encode_flow_mod(&fm, protocol); | |
3459 | ovs_list_push_back(packets, &ofm->list_node); | |
3460 | } | |
3461 | ||
3462 | static void | |
3463 | ofctl_replace_flows(struct ovs_cmdl_context *ctx) | |
3464 | { | |
3465 | enum { FILE_IDX = 0, SWITCH_IDX = 1 }; | |
3466 | enum ofputil_protocol usable_protocols, protocol; | |
3467 | struct fte_state fte_state; | |
3468 | struct flow_tables tables; | |
3469 | struct classifier *cls; | |
3470 | struct ovs_list requests; | |
3471 | struct vconn *vconn; | |
3472 | struct fte *fte; | |
3473 | ||
3474 | fte_state_init(&fte_state); | |
3475 | usable_protocols = read_flows_from_file(ctx->argv[2], &fte_state, FILE_IDX); | |
3476 | ||
3477 | protocol = open_vconn(ctx->argv[1], &vconn); | |
3478 | protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); | |
3479 | ||
3480 | read_flows_from_switch(vconn, protocol, &fte_state, SWITCH_IDX); | |
3481 | ||
3482 | fte_fill(&fte_state, &tables); | |
3483 | ||
3484 | ovs_list_init(&requests); | |
3485 | ||
3486 | FOR_EACH_TABLE (cls, &tables) { | |
3487 | /* Delete flows that exist on the switch but not in the file. */ | |
3488 | CLS_FOR_EACH (fte, rule, cls) { | |
3489 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
3490 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
3491 | ||
3492 | if (sw_ver && !file_ver) { | |
3493 | fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT, | |
3494 | protocol, &requests); | |
3495 | } | |
3496 | } | |
3497 | ||
3498 | /* Add flows that exist in the file but not on the switch. | |
3499 | * Update flows that exist in both places but differ. */ | |
3500 | CLS_FOR_EACH (fte, rule, cls) { | |
3501 | struct fte_version *file_ver = fte->versions[FILE_IDX]; | |
3502 | struct fte_version *sw_ver = fte->versions[SWITCH_IDX]; | |
3503 | ||
3504 | if (file_ver && | |
3505 | (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) { | |
3506 | fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, | |
3507 | &requests); | |
3508 | } | |
3509 | } | |
3510 | } | |
3511 | if (bundle) { | |
3512 | bundle_transact(vconn, &requests, OFPBF_ORDERED | OFPBF_ATOMIC); | |
3513 | } else { | |
3514 | transact_multiple_noreply(vconn, &requests); | |
3515 | } | |
3516 | ||
3517 | ofpbuf_list_delete(&requests); | |
3518 | vconn_close(vconn); | |
3519 | ||
3520 | fte_free_all(&tables); | |
3521 | } | |
3522 | ||
3523 | static void | |
3524 | read_flows_from_source(const char *source, struct fte_state *state, int index) | |
3525 | { | |
3526 | struct stat s; | |
3527 | ||
3528 | if (source[0] == '/' || source[0] == '.' | |
3529 | || (!strchr(source, ':') && !stat(source, &s))) { | |
3530 | read_flows_from_file(source, state, index); | |
3531 | } else { | |
3532 | enum ofputil_protocol protocol; | |
3533 | struct vconn *vconn; | |
3534 | ||
3535 | protocol = open_vconn(source, &vconn); | |
3536 | protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY); | |
3537 | read_flows_from_switch(vconn, protocol, state, index); | |
3538 | vconn_close(vconn); | |
3539 | } | |
3540 | } | |
3541 | ||
3542 | static void | |
3543 | ofctl_diff_flows(struct ovs_cmdl_context *ctx) | |
3544 | { | |
3545 | bool differences = false; | |
3546 | struct fte_state fte_state; | |
3547 | struct flow_tables tables; | |
3548 | struct classifier *cls; | |
3549 | struct ds a_s, b_s; | |
3550 | struct fte *fte; | |
3551 | ||
3552 | fte_state_init(&fte_state); | |
3553 | read_flows_from_source(ctx->argv[1], &fte_state, 0); | |
3554 | read_flows_from_source(ctx->argv[2], &fte_state, 1); | |
3555 | fte_fill(&fte_state, &tables); | |
3556 | ||
3557 | ds_init(&a_s); | |
3558 | ds_init(&b_s); | |
3559 | ||
3560 | FOR_EACH_TABLE (cls, &tables) { | |
3561 | CLS_FOR_EACH (fte, rule, cls) { | |
3562 | struct fte_version *a = fte->versions[0]; | |
3563 | struct fte_version *b = fte->versions[1]; | |
3564 | ||
3565 | if (!a || !b || !fte_version_equals(a, b)) { | |
3566 | fte_version_format(fte, 0, &a_s); | |
3567 | fte_version_format(fte, 1, &b_s); | |
3568 | if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) { | |
3569 | if (a_s.length) { | |
3570 | printf("-%s", ds_cstr(&a_s)); | |
3571 | } | |
3572 | if (b_s.length) { | |
3573 | printf("+%s", ds_cstr(&b_s)); | |
3574 | } | |
3575 | differences = true; | |
3576 | } | |
3577 | } | |
3578 | } | |
3579 | } | |
3580 | ||
3581 | ds_destroy(&a_s); | |
3582 | ds_destroy(&b_s); | |
3583 | ||
3584 | fte_free_all(&tables); | |
3585 | ||
3586 | if (differences) { | |
3587 | exit(2); | |
3588 | } | |
3589 | } | |
3590 | ||
3591 | static void | |
3592 | ofctl_meter_mod__(const char *bridge, const char *str, int command) | |
3593 | { | |
3594 | struct ofputil_meter_mod mm; | |
3595 | struct vconn *vconn; | |
3596 | enum ofputil_protocol protocol; | |
3597 | enum ofputil_protocol usable_protocols; | |
3598 | enum ofp_version version; | |
3599 | ||
3600 | if (str) { | |
3601 | char *error; | |
3602 | error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols); | |
3603 | if (error) { | |
3604 | ovs_fatal(0, "%s", error); | |
3605 | } | |
3606 | } else { | |
3607 | usable_protocols = OFPUTIL_P_OF13_UP; | |
3608 | mm.command = command; | |
3609 | mm.meter.meter_id = OFPM13_ALL; | |
3610 | } | |
3611 | ||
3612 | protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols); | |
3613 | version = ofputil_protocol_to_ofp_version(protocol); | |
3614 | transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm)); | |
3615 | vconn_close(vconn); | |
3616 | } | |
3617 | ||
3618 | static void | |
3619 | ofctl_meter_request__(const char *bridge, const char *str, | |
3620 | enum ofputil_meter_request_type type) | |
3621 | { | |
3622 | struct ofputil_meter_mod mm; | |
3623 | struct vconn *vconn; | |
3624 | enum ofputil_protocol usable_protocols; | |
3625 | enum ofputil_protocol protocol; | |
3626 | enum ofp_version version; | |
3627 | ||
3628 | if (str) { | |
3629 | char *error; | |
3630 | error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols); | |
3631 | if (error) { | |
3632 | ovs_fatal(0, "%s", error); | |
3633 | } | |
3634 | } else { | |
3635 | usable_protocols = OFPUTIL_P_OF13_UP; | |
3636 | mm.meter.meter_id = OFPM13_ALL; | |
3637 | } | |
3638 | ||
3639 | protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols); | |
3640 | version = ofputil_protocol_to_ofp_version(protocol); | |
3641 | dump_transaction(vconn, ofputil_encode_meter_request(version, type, | |
3642 | mm.meter.meter_id)); | |
3643 | vconn_close(vconn); | |
3644 | } | |
3645 | ||
3646 | ||
3647 | static void | |
3648 | ofctl_add_meter(struct ovs_cmdl_context *ctx) | |
3649 | { | |
3650 | ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD); | |
3651 | } | |
3652 | ||
3653 | static void | |
3654 | ofctl_mod_meter(struct ovs_cmdl_context *ctx) | |
3655 | { | |
3656 | ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY); | |
3657 | } | |
3658 | ||
3659 | static void | |
3660 | ofctl_del_meters(struct ovs_cmdl_context *ctx) | |
3661 | { | |
3662 | ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE); | |
3663 | } | |
3664 | ||
3665 | static void | |
3666 | ofctl_dump_meters(struct ovs_cmdl_context *ctx) | |
3667 | { | |
3668 | ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, | |
3669 | OFPUTIL_METER_CONFIG); | |
3670 | } | |
3671 | ||
3672 | static void | |
3673 | ofctl_meter_stats(struct ovs_cmdl_context *ctx) | |
3674 | { | |
3675 | ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, | |
3676 | OFPUTIL_METER_STATS); | |
3677 | } | |
3678 | ||
3679 | static void | |
3680 | ofctl_meter_features(struct ovs_cmdl_context *ctx) | |
3681 | { | |
3682 | ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES); | |
3683 | } | |
3684 | ||
3685 | \f | |
3686 | /* Undocumented commands for unit testing. */ | |
3687 | ||
3688 | static void | |
3689 | ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms, | |
3690 | enum ofputil_protocol usable_protocols) | |
3691 | { | |
3692 | enum ofputil_protocol protocol = 0; | |
3693 | char *usable_s; | |
3694 | size_t i; | |
3695 | ||
3696 | usable_s = ofputil_protocols_to_string(usable_protocols); | |
3697 | printf("usable protocols: %s\n", usable_s); | |
3698 | free(usable_s); | |
3699 | ||
3700 | if (!(usable_protocols & allowed_protocols)) { | |
3701 | ovs_fatal(0, "no usable protocol"); | |
3702 | } | |
3703 | for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { | |
3704 | protocol = 1 << i; | |
3705 | if (protocol & usable_protocols & allowed_protocols) { | |
3706 | break; | |
3707 | } | |
3708 | } | |
3709 | ovs_assert(is_pow2(protocol)); | |
3710 | ||
3711 | printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol)); | |
3712 | ||
3713 | for (i = 0; i < n_fms; i++) { | |
3714 | struct ofputil_flow_mod *fm = &fms[i]; | |
3715 | struct ofpbuf *msg; | |
3716 | ||
3717 | msg = ofputil_encode_flow_mod(fm, protocol); | |
3718 | ofp_print(stdout, msg->data, msg->size, verbosity); | |
3719 | ofpbuf_delete(msg); | |
3720 | ||
3721 | free(CONST_CAST(struct ofpact *, fm->ofpacts)); | |
3722 | } | |
3723 | } | |
3724 | ||
3725 | /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints | |
3726 | * it back to stdout. */ | |
3727 | static void | |
3728 | ofctl_parse_flow(struct ovs_cmdl_context *ctx) | |
3729 | { | |
3730 | enum ofputil_protocol usable_protocols; | |
3731 | struct ofputil_flow_mod fm; | |
3732 | char *error; | |
3733 | ||
3734 | error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols); | |
3735 | if (error) { | |
3736 | ovs_fatal(0, "%s", error); | |
3737 | } | |
3738 | ofctl_parse_flows__(&fm, 1, usable_protocols); | |
3739 | } | |
3740 | ||
3741 | /* "parse-flows FILENAME": reads the named file as a sequence of flows (like | |
3742 | * add-flows) and prints each of the flows back to stdout. */ | |
3743 | static void | |
3744 | ofctl_parse_flows(struct ovs_cmdl_context *ctx) | |
3745 | { | |
3746 | enum ofputil_protocol usable_protocols; | |
3747 | struct ofputil_flow_mod *fms = NULL; | |
3748 | size_t n_fms = 0; | |
3749 | char *error; | |
3750 | ||
3751 | error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms, | |
3752 | &usable_protocols); | |
3753 | if (error) { | |
3754 | ovs_fatal(0, "%s", error); | |
3755 | } | |
3756 | ofctl_parse_flows__(fms, n_fms, usable_protocols); | |
3757 | free(fms); | |
3758 | } | |
3759 | ||
3760 | static void | |
3761 | ofctl_parse_nxm__(bool oxm, enum ofp_version version) | |
3762 | { | |
3763 | struct ds in; | |
3764 | ||
3765 | ds_init(&in); | |
3766 | while (!ds_get_test_line(&in, stdin)) { | |
3767 | struct ofpbuf nx_match; | |
3768 | struct match match; | |
3769 | ovs_be64 cookie, cookie_mask; | |
3770 | enum ofperr error; | |
3771 | int match_len; | |
3772 | ||
3773 | /* Convert string to nx_match. */ | |
3774 | ofpbuf_init(&nx_match, 0); | |
3775 | if (oxm) { | |
3776 | match_len = oxm_match_from_string(ds_cstr(&in), &nx_match); | |
3777 | } else { | |
3778 | match_len = nx_match_from_string(ds_cstr(&in), &nx_match); | |
3779 | } | |
3780 | ||
3781 | /* Convert nx_match to match. */ | |
3782 | if (strict) { | |
3783 | if (oxm) { | |
3784 | error = oxm_pull_match(&nx_match, &match); | |
3785 | } else { | |
3786 | error = nx_pull_match(&nx_match, match_len, &match, | |
3787 | &cookie, &cookie_mask); | |
3788 | } | |
3789 | } else { | |
3790 | if (oxm) { | |
3791 | error = oxm_pull_match_loose(&nx_match, &match); | |
3792 | } else { | |
3793 | error = nx_pull_match_loose(&nx_match, match_len, &match, | |
3794 | &cookie, &cookie_mask); | |
3795 | } | |
3796 | } | |
3797 | ||
3798 | ||
3799 | if (!error) { | |
3800 | char *out; | |
3801 | ||
3802 | /* Convert match back to nx_match. */ | |
3803 | ofpbuf_uninit(&nx_match); | |
3804 | ofpbuf_init(&nx_match, 0); | |
3805 | if (oxm) { | |
3806 | match_len = oxm_put_match(&nx_match, &match, version); | |
3807 | out = oxm_match_to_string(&nx_match, match_len); | |
3808 | } else { | |
3809 | match_len = nx_put_match(&nx_match, &match, | |
3810 | cookie, cookie_mask); | |
3811 | out = nx_match_to_string(nx_match.data, match_len); | |
3812 | } | |
3813 | ||
3814 | puts(out); | |
3815 | free(out); | |
3816 | ||
3817 | if (verbosity > 0) { | |
3818 | ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false); | |
3819 | } | |
3820 | } else { | |
3821 | printf("nx_pull_match() returned error %s\n", | |
3822 | ofperr_get_name(error)); | |
3823 | } | |
3824 | ||
3825 | ofpbuf_uninit(&nx_match); | |
3826 | } | |
3827 | ds_destroy(&in); | |
3828 | } | |
3829 | ||
3830 | /* "parse-nxm": reads a series of NXM nx_match specifications as strings from | |
3831 | * stdin, does some internal fussing with them, and then prints them back as | |
3832 | * strings on stdout. */ | |
3833 | static void | |
3834 | ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED) | |
3835 | { | |
3836 | ofctl_parse_nxm__(false, 0); | |
3837 | } | |
3838 | ||
3839 | /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as | |
3840 | * strings from stdin, does some internal fussing with them, and then prints | |
3841 | * them back as strings on stdout. VERSION must specify an OpenFlow version, | |
3842 | * e.g. "OpenFlow12". */ | |
3843 | static void | |
3844 | ofctl_parse_oxm(struct ovs_cmdl_context *ctx) | |
3845 | { | |
3846 | enum ofp_version version = ofputil_version_from_string(ctx->argv[1]); | |
3847 | if (version < OFP12_VERSION) { | |
3848 | ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]); | |
3849 | } | |
3850 | ||
3851 | ofctl_parse_nxm__(true, version); | |
3852 | } | |
3853 | ||
3854 | static void | |
3855 | print_differences(const char *prefix, | |
3856 | const void *a_, size_t a_len, | |
3857 | const void *b_, size_t b_len) | |
3858 | { | |
3859 | const uint8_t *a = a_; | |
3860 | const uint8_t *b = b_; | |
3861 | size_t i; | |
3862 | ||
3863 | for (i = 0; i < MIN(a_len, b_len); i++) { | |
3864 | if (a[i] != b[i]) { | |
3865 | printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n", | |
3866 | prefix, i, a[i], b[i]); | |
3867 | } | |
3868 | } | |
3869 | for (i = a_len; i < b_len; i++) { | |
3870 | printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]); | |
3871 | } | |
3872 | for (i = b_len; i < a_len; i++) { | |
3873 | printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]); | |
3874 | } | |
3875 | } | |
3876 | ||
3877 | static void | |
3878 | ofctl_parse_actions__(const char *version_s, bool instructions) | |
3879 | { | |
3880 | enum ofp_version version; | |
3881 | struct ds in; | |
3882 | ||
3883 | version = ofputil_version_from_string(version_s); | |
3884 | if (!version) { | |
3885 | ovs_fatal(0, "%s: not a valid OpenFlow version", version_s); | |
3886 | } | |
3887 | ||
3888 | ds_init(&in); | |
3889 | while (!ds_get_preprocessed_line(&in, stdin, NULL)) { | |
3890 | struct ofpbuf of_out; | |
3891 | struct ofpbuf of_in; | |
3892 | struct ofpbuf ofpacts; | |
3893 | const char *table_id; | |
3894 | char *actions; | |
3895 | enum ofperr error; | |
3896 | size_t size; | |
3897 | struct ds s; | |
3898 | ||
3899 | /* Parse table_id separated with the follow-up actions by ",", if | |
3900 | * any. */ | |
3901 | actions = ds_cstr(&in); | |
3902 | table_id = NULL; | |
3903 | if (strstr(actions, ",")) { | |
3904 | table_id = strsep(&actions, ","); | |
3905 | } | |
3906 | ||
3907 | /* Parse hex bytes. */ | |
3908 | ofpbuf_init(&of_in, 0); | |
3909 | if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') { | |
3910 | ovs_fatal(0, "Trailing garbage in hex data"); | |
3911 | } | |
3912 | ||
3913 | /* Convert to ofpacts. */ | |
3914 | ofpbuf_init(&ofpacts, 0); | |
3915 | size = of_in.size; | |
3916 | error = (instructions | |
3917 | ? ofpacts_pull_openflow_instructions | |
3918 | : ofpacts_pull_openflow_actions)( | |
3919 | &of_in, of_in.size, version, &ofpacts); | |
3920 | if (!error && instructions) { | |
3921 | /* Verify actions, enforce consistency. */ | |
3922 | enum ofputil_protocol protocol; | |
3923 | struct flow flow; | |
3924 | ||
3925 | memset(&flow, 0, sizeof flow); | |
3926 | protocol = ofputil_protocols_from_ofp_version(version); | |
3927 | error = ofpacts_check_consistency(ofpacts.data, ofpacts.size, | |
3928 | &flow, OFPP_MAX, | |
3929 | table_id ? atoi(table_id) : 0, | |
3930 | OFPTT_MAX + 1, protocol); | |
3931 | } | |
3932 | if (error) { | |
3933 | printf("bad %s %s: %s\n\n", | |
3934 | version_s, instructions ? "instructions" : "actions", | |
3935 | ofperr_get_name(error)); | |
3936 | ofpbuf_uninit(&ofpacts); | |
3937 | ofpbuf_uninit(&of_in); | |
3938 | continue; | |
3939 | } | |
3940 | ofpbuf_push_uninit(&of_in, size); | |
3941 | ||
3942 | /* Print cls_rule. */ | |
3943 | ds_init(&s); | |
3944 | ds_put_cstr(&s, "actions="); | |
3945 | ofpacts_format(ofpacts.data, ofpacts.size, &s); | |
3946 | puts(ds_cstr(&s)); | |
3947 | ds_destroy(&s); | |
3948 | ||
3949 | /* Convert back to ofp10 actions and print differences from input. */ | |
3950 | ofpbuf_init(&of_out, 0); | |
3951 | if (instructions) { | |
3952 | ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size, | |
3953 | &of_out, version); | |
3954 | } else { | |
3955 | ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size, | |
3956 | &of_out, version); | |
3957 | } | |
3958 | ||
3959 | print_differences("", of_in.data, of_in.size, | |
3960 | of_out.data, of_out.size); | |
3961 | putchar('\n'); | |
3962 | ||
3963 | ofpbuf_uninit(&ofpacts); | |
3964 | ofpbuf_uninit(&of_in); | |
3965 | ofpbuf_uninit(&of_out); | |
3966 | } | |
3967 | ds_destroy(&in); | |
3968 | } | |
3969 | ||
3970 | /* "parse-actions VERSION": reads a series of action specifications for the | |
3971 | * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts, | |
3972 | * prints them as strings on stdout, and then converts them back to hex bytes | |
3973 | * and prints any differences from the input. */ | |
3974 | static void | |
3975 | ofctl_parse_actions(struct ovs_cmdl_context *ctx) | |
3976 | { | |
3977 | ofctl_parse_actions__(ctx->argv[1], false); | |
3978 | } | |
3979 | ||
3980 | /* "parse-actions VERSION": reads a series of instruction specifications for | |
3981 | * the given OpenFlow VERSION as hex bytes from stdin, converts them to | |
3982 | * ofpacts, prints them as strings on stdout, and then converts them back to | |
3983 | * hex bytes and prints any differences from the input. */ | |
3984 | static void | |
3985 | ofctl_parse_instructions(struct ovs_cmdl_context *ctx) | |
3986 | { | |
3987 | ofctl_parse_actions__(ctx->argv[1], true); | |
3988 | } | |
3989 | ||
3990 | /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex | |
3991 | * bytes from stdin, converts them to cls_rules, prints them as strings on | |
3992 | * stdout, and then converts them back to hex bytes and prints any differences | |
3993 | * from the input. | |
3994 | * | |
3995 | * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose | |
3996 | * values are ignored in the input and will be set to zero when OVS converts | |
3997 | * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when | |
3998 | * it does the conversion to hex, to ensure that in fact they are ignored. */ | |
3999 | static void | |
4000 | ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED) | |
4001 | { | |
4002 | struct ds expout; | |
4003 | struct ds in; | |
4004 | ||
4005 | ds_init(&in); | |
4006 | ds_init(&expout); | |
4007 | while (!ds_get_preprocessed_line(&in, stdin, NULL)) { | |
4008 | struct ofpbuf match_in, match_expout; | |
4009 | struct ofp10_match match_out; | |
4010 | struct ofp10_match match_normal; | |
4011 | struct match match; | |
4012 | char *p; | |
4013 | ||
4014 | /* Parse hex bytes to use for expected output. */ | |
4015 | ds_clear(&expout); | |
4016 | ds_put_cstr(&expout, ds_cstr(&in)); | |
4017 | for (p = ds_cstr(&expout); *p; p++) { | |
4018 | if (*p == 'x') { | |
4019 | *p = '0'; | |
4020 | } | |
4021 | } | |
4022 | ofpbuf_init(&match_expout, 0); | |
4023 | if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') { | |
4024 | ovs_fatal(0, "Trailing garbage in hex data"); | |
4025 | } | |
4026 | if (match_expout.size != sizeof(struct ofp10_match)) { | |
4027 | ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE, | |
4028 | match_expout.size, sizeof(struct ofp10_match)); | |
4029 | } | |
4030 | ||
4031 | /* Parse hex bytes for input. */ | |
4032 | for (p = ds_cstr(&in); *p; p++) { | |
4033 | if (*p == 'x') { | |
4034 | *p = "0123456789abcdef"[random_uint32() & 0xf]; | |
4035 | } | |
4036 | } | |
4037 | ofpbuf_init(&match_in, 0); | |
4038 | if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') { | |
4039 | ovs_fatal(0, "Trailing garbage in hex data"); | |
4040 | } | |
4041 | if (match_in.size != sizeof(struct ofp10_match)) { | |
4042 | ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE, | |
4043 | match_in.size, sizeof(struct ofp10_match)); | |
4044 | } | |
4045 | ||
4046 | /* Convert to cls_rule and print. */ | |
4047 | ofputil_match_from_ofp10_match(match_in.data, &match); | |
4048 | match_print(&match); | |
4049 | ||
4050 | /* Convert back to ofp10_match and print differences from input. */ | |
4051 | ofputil_match_to_ofp10_match(&match, &match_out); | |
4052 | print_differences("", match_expout.data, match_expout.size, | |
4053 | &match_out, sizeof match_out); | |
4054 | ||
4055 | /* Normalize, then convert and compare again. */ | |
4056 | ofputil_normalize_match(&match); | |
4057 | ofputil_match_to_ofp10_match(&match, &match_normal); | |
4058 | print_differences("normal: ", &match_out, sizeof match_out, | |
4059 | &match_normal, sizeof match_normal); | |
4060 | putchar('\n'); | |
4061 | ||
4062 | ofpbuf_uninit(&match_in); | |
4063 | ofpbuf_uninit(&match_expout); | |
4064 | } | |
4065 | ds_destroy(&in); | |
4066 | ds_destroy(&expout); | |
4067 | } | |
4068 | ||
4069 | /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex | |
4070 | * bytes from stdin, converts them to "struct match"es, prints them as strings | |
4071 | * on stdout, and then converts them back to hex bytes and prints any | |
4072 | * differences from the input. */ | |
4073 | static void | |
4074 | ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED) | |
4075 | { | |
4076 | struct ds in; | |
4077 | ||
4078 | ds_init(&in); | |
4079 | while (!ds_get_preprocessed_line(&in, stdin, NULL)) { | |
4080 | struct ofpbuf match_in; | |
4081 | struct ofp11_match match_out; | |
4082 | struct match match; | |
4083 | enum ofperr error; | |
4084 | ||
4085 | /* Parse hex bytes. */ | |
4086 | ofpbuf_init(&match_in, 0); | |
4087 | if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') { | |
4088 | ovs_fatal(0, "Trailing garbage in hex data"); | |
4089 | } | |
4090 | if (match_in.size != sizeof(struct ofp11_match)) { | |
4091 | ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE, | |
4092 | match_in.size, sizeof(struct ofp11_match)); | |
4093 | } | |
4094 | ||
4095 | /* Convert to match. */ | |
4096 | error = ofputil_match_from_ofp11_match(match_in.data, &match); | |
4097 | if (error) { | |
4098 | printf("bad ofp11_match: %s\n\n", ofperr_get_name(error)); | |
4099 | ofpbuf_uninit(&match_in); | |
4100 | continue; | |
4101 | } | |
4102 | ||
4103 | /* Print match. */ | |
4104 | match_print(&match); | |
4105 | ||
4106 | /* Convert back to ofp11_match and print differences from input. */ | |
4107 | ofputil_match_to_ofp11_match(&match, &match_out); | |
4108 | ||
4109 | print_differences("", match_in.data, match_in.size, | |
4110 | &match_out, sizeof match_out); | |
4111 | putchar('\n'); | |
4112 | ||
4113 | ofpbuf_uninit(&match_in); | |
4114 | } | |
4115 | ds_destroy(&in); | |
4116 | } | |
4117 | ||
4118 | /* "parse-pcap PCAP...": read packets from each PCAP file and print their | |
4119 | * flows. */ | |
4120 | static void | |
4121 | ofctl_parse_pcap(struct ovs_cmdl_context *ctx) | |
4122 | { | |
4123 | int error = 0; | |
4124 | for (int i = 1; i < ctx->argc; i++) { | |
4125 | const char *filename = ctx->argv[i]; | |
4126 | FILE *pcap = ovs_pcap_open(filename, "rb"); | |
4127 | if (!pcap) { | |
4128 | error = errno; | |
4129 | ovs_error(error, "%s: open failed", filename); | |
4130 | continue; | |
4131 | } | |
4132 | ||
4133 | for (;;) { | |
4134 | struct dp_packet *packet; | |
4135 | struct flow flow; | |
4136 | int retval; | |
4137 | ||
4138 | retval = ovs_pcap_read(pcap, &packet, NULL); | |
4139 | if (retval == EOF) { | |
4140 | break; | |
4141 | } else if (retval) { | |
4142 | error = retval; | |
4143 | ovs_error(error, "%s: read failed", filename); | |
4144 | } | |
4145 | ||
4146 | pkt_metadata_init(&packet->md, u32_to_odp(ofp_to_u16(OFPP_ANY))); | |
4147 | flow_extract(packet, &flow); | |
4148 | flow_print(stdout, &flow); | |
4149 | putchar('\n'); | |
4150 | dp_packet_delete(packet); | |
4151 | } | |
4152 | fclose(pcap); | |
4153 | } | |
4154 | exit(error); | |
4155 | } | |
4156 | ||
4157 | /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and | |
4158 | * mask values to and from various formats and prints the results. */ | |
4159 | static void | |
4160 | ofctl_check_vlan(struct ovs_cmdl_context *ctx) | |
4161 | { | |
4162 | struct match match; | |
4163 | ||
4164 | char *string_s; | |
4165 | struct ofputil_flow_mod fm; | |
4166 | ||
4167 | struct ofpbuf nxm; | |
4168 | struct match nxm_match; | |
4169 | int nxm_match_len; | |
4170 | char *nxm_s; | |
4171 | ||
4172 | struct ofp10_match of10_raw; | |
4173 | struct match of10_match; | |
4174 | ||
4175 | struct ofp11_match of11_raw; | |
4176 | struct match of11_match; | |
4177 | ||
4178 | enum ofperr error; | |
4179 | char *error_s; | |
4180 | ||
4181 | enum ofputil_protocol usable_protocols; /* Unused for now. */ | |
4182 | ||
4183 | match_init_catchall(&match); | |
4184 | match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16)); | |
4185 | match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16)); | |
4186 | ||
4187 | /* Convert to and from string. */ | |
4188 | string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY); | |
4189 | printf("%s -> ", string_s); | |
4190 | fflush(stdout); | |
4191 | error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols); | |
4192 | if (error_s) { | |
4193 | ovs_fatal(0, "%s", error_s); | |
4194 | } | |
4195 | printf("%04"PRIx16"/%04"PRIx16"\n", | |
4196 | ntohs(fm.match.flow.vlan_tci), | |
4197 | ntohs(fm.match.wc.masks.vlan_tci)); | |
4198 | free(string_s); | |
4199 | ||
4200 | /* Convert to and from NXM. */ | |
4201 | ofpbuf_init(&nxm, 0); | |
4202 | nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0)); | |
4203 | nxm_s = nx_match_to_string(nxm.data, nxm_match_len); | |
4204 | error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL); | |
4205 | printf("NXM: %s -> ", nxm_s); | |
4206 | if (error) { | |
4207 | printf("%s\n", ofperr_to_string(error)); | |
4208 | } else { | |
4209 | printf("%04"PRIx16"/%04"PRIx16"\n", | |
4210 | ntohs(nxm_match.flow.vlan_tci), | |
4211 | ntohs(nxm_match.wc.masks.vlan_tci)); | |
4212 | } | |
4213 | free(nxm_s); | |
4214 | ofpbuf_uninit(&nxm); | |
4215 | ||
4216 | /* Convert to and from OXM. */ | |
4217 | ofpbuf_init(&nxm, 0); | |
4218 | nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION); | |
4219 | nxm_s = oxm_match_to_string(&nxm, nxm_match_len); | |
4220 | error = oxm_pull_match(&nxm, &nxm_match); | |
4221 | printf("OXM: %s -> ", nxm_s); | |
4222 | if (error) { | |
4223 | printf("%s\n", ofperr_to_string(error)); | |
4224 | } else { | |
4225 | uint16_t vid = ntohs(nxm_match.flow.vlan_tci) & | |
4226 | (VLAN_VID_MASK | VLAN_CFI); | |
4227 | uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) & | |
4228 | (VLAN_VID_MASK | VLAN_CFI); | |
4229 | ||
4230 | printf("%04"PRIx16"/%04"PRIx16",", vid, mask); | |
4231 | if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) { | |
4232 | printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci)); | |
4233 | } else { | |
4234 | printf("--\n"); | |
4235 | } | |
4236 | } | |
4237 | free(nxm_s); | |
4238 | ofpbuf_uninit(&nxm); | |
4239 | ||
4240 | /* Convert to and from OpenFlow 1.0. */ | |
4241 | ofputil_match_to_ofp10_match(&match, &of10_raw); | |
4242 | ofputil_match_from_ofp10_match(&of10_raw, &of10_match); | |
4243 | printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n", | |
4244 | ntohs(of10_raw.dl_vlan), | |
4245 | (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0, | |
4246 | of10_raw.dl_vlan_pcp, | |
4247 | (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0, | |
4248 | ntohs(of10_match.flow.vlan_tci), | |
4249 | ntohs(of10_match.wc.masks.vlan_tci)); | |
4250 | ||
4251 | /* Convert to and from OpenFlow 1.1. */ | |
4252 | ofputil_match_to_ofp11_match(&match, &of11_raw); | |
4253 | ofputil_match_from_ofp11_match(&of11_raw, &of11_match); | |
4254 | printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n", | |
4255 | ntohs(of11_raw.dl_vlan), | |
4256 | (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0, | |
4257 | of11_raw.dl_vlan_pcp, | |
4258 | (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0, | |
4259 | ntohs(of11_match.flow.vlan_tci), | |
4260 | ntohs(of11_match.wc.masks.vlan_tci)); | |
4261 | } | |
4262 | ||
4263 | /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow | |
4264 | * version. */ | |
4265 | static void | |
4266 | ofctl_print_error(struct ovs_cmdl_context *ctx) | |
4267 | { | |
4268 | enum ofperr error; | |
4269 | int version; | |
4270 | ||
4271 | error = ofperr_from_name(ctx->argv[1]); | |
4272 | if (!error) { | |
4273 | ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]); | |
4274 | } | |
4275 | ||
4276 | for (version = 0; version <= UINT8_MAX; version++) { | |
4277 | const char *name = ofperr_domain_get_name(version); | |
4278 | if (name) { | |
4279 | int vendor = ofperr_get_vendor(error, version); | |
4280 | int type = ofperr_get_type(error, version); | |
4281 | int code = ofperr_get_code(error, version); | |
4282 | ||
4283 | if (vendor != -1 || type != -1 || code != -1) { | |
4284 | printf("%s: vendor %#x, type %d, code %d\n", | |
4285 | name, vendor, type, code); | |
4286 | } | |
4287 | } | |
4288 | } | |
4289 | } | |
4290 | ||
4291 | /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the | |
4292 | * error named ENUM and prints the error reply in hex. */ | |
4293 | static void | |
4294 | ofctl_encode_error_reply(struct ovs_cmdl_context *ctx) | |
4295 | { | |
4296 | const struct ofp_header *oh; | |
4297 | struct ofpbuf request, *reply; | |
4298 | enum ofperr error; | |
4299 | ||
4300 | error = ofperr_from_name(ctx->argv[1]); | |
4301 | if (!error) { | |
4302 | ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]); | |
4303 | } | |
4304 | ||
4305 | ofpbuf_init(&request, 0); | |
4306 | if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') { | |
4307 | ovs_fatal(0, "Trailing garbage in hex data"); | |
4308 | } | |
4309 | if (request.size < sizeof(struct ofp_header)) { | |
4310 | ovs_fatal(0, "Request too short"); | |
4311 | } | |
4312 | ||
4313 | oh = request.data; | |
4314 | if (request.size != ntohs(oh->length)) { | |
4315 | ovs_fatal(0, "Request size inconsistent"); | |
4316 | } | |
4317 | ||
4318 | reply = ofperr_encode_reply(error, request.data); | |
4319 | ofpbuf_uninit(&request); | |
4320 | ||
4321 | ovs_hex_dump(stdout, reply->data, reply->size, 0, false); | |
4322 | ofpbuf_delete(reply); | |
4323 | } | |
4324 | ||
4325 | /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into | |
4326 | * binary data, interpreting them as an OpenFlow message, and prints the | |
4327 | * OpenFlow message on stdout, at VERBOSITY (level 2 by default). | |
4328 | * | |
4329 | * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where | |
4330 | * HEXSTRING_FILE contains the HEXSTRING. */ | |
4331 | static void | |
4332 | ofctl_ofp_print(struct ovs_cmdl_context *ctx) | |
4333 | { | |
4334 | struct ofpbuf packet; | |
4335 | char *buffer; | |
4336 | int verbosity = 2; | |
4337 | struct ds line; | |
4338 | ||
4339 | ds_init(&line); | |
4340 | ||
4341 | if (!strcmp(ctx->argv[ctx->argc-1], "-")) { | |
4342 | if (ds_get_line(&line, stdin)) { | |
4343 | VLOG_FATAL("Failed to read stdin"); | |
4344 | } | |
4345 | ||
4346 | buffer = line.string; | |
4347 | verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity; | |
4348 | } else if (ctx->argc > 2) { | |
4349 | buffer = ctx->argv[1]; | |
4350 | verbosity = atoi(ctx->argv[2]); | |
4351 | } else { | |
4352 | buffer = ctx->argv[1]; | |
4353 | } | |
4354 | ||
4355 | ofpbuf_init(&packet, strlen(buffer) / 2); | |
4356 | if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') { | |
4357 | ovs_fatal(0, "trailing garbage following hex bytes"); | |
4358 | } | |
4359 | ofp_print(stdout, packet.data, packet.size, verbosity); | |
4360 | ofpbuf_uninit(&packet); | |
4361 | ds_destroy(&line); | |
4362 | } | |
4363 | ||
4364 | /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message | |
4365 | * and dumps each message in hex. */ | |
4366 | static void | |
4367 | ofctl_encode_hello(struct ovs_cmdl_context *ctx) | |
4368 | { | |
4369 | uint32_t bitmap = strtol(ctx->argv[1], NULL, 0); | |
4370 | struct ofpbuf *hello; | |
4371 | ||
4372 | hello = ofputil_encode_hello(bitmap); | |
4373 | ovs_hex_dump(stdout, hello->data, hello->size, 0, false); | |
4374 | ofp_print(stdout, hello->data, hello->size, verbosity); | |
4375 | ofpbuf_delete(hello); | |
4376 | } | |
4377 | ||
4378 | static void | |
4379 | ofctl_parse_key_value(struct ovs_cmdl_context *ctx) | |
4380 | { | |
4381 | for (size_t i = 1; i < ctx->argc; i++) { | |
4382 | char *s = ctx->argv[i]; | |
4383 | char *key, *value; | |
4384 | int j = 0; | |
4385 | while (ofputil_parse_key_value(&s, &key, &value)) { | |
4386 | if (j++) { | |
4387 | fputs(", ", stdout); | |
4388 | } | |
4389 | fputs(key, stdout); | |
4390 | if (value[0]) { | |
4391 | printf("=%s", value); | |
4392 | } | |
4393 | } | |
4394 | putchar('\n'); | |
4395 | } | |
4396 | } | |
4397 | ||
4398 | static const struct ovs_cmdl_command all_commands[] = { | |
4399 | { "show", "switch", | |
4400 | 1, 1, ofctl_show, OVS_RO }, | |
4401 | { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]", | |
4402 | 1, 3, ofctl_monitor, OVS_RO }, | |
4403 | { "snoop", "switch", | |
4404 | 1, 1, ofctl_snoop, OVS_RO }, | |
4405 | { "dump-desc", "switch", | |
4406 | 1, 1, ofctl_dump_desc, OVS_RO }, | |
4407 | { "dump-tables", "switch", | |
4408 | 1, 1, ofctl_dump_tables, OVS_RO }, | |
4409 | { "dump-table-features", "switch", | |
4410 | 1, 1, ofctl_dump_table_features, OVS_RO }, | |
4411 | { "dump-table-desc", "switch", | |
4412 | 1, 1, ofctl_dump_table_desc, OVS_RO }, | |
4413 | { "dump-flows", "switch", | |
4414 | 1, 2, ofctl_dump_flows, OVS_RO }, | |
4415 | { "dump-aggregate", "switch", | |
4416 | 1, 2, ofctl_dump_aggregate, OVS_RO }, | |
4417 | { "queue-stats", "switch [port [queue]]", | |
4418 | 1, 3, ofctl_queue_stats, OVS_RO }, | |
4419 | { "queue-get-config", "switch [port [queue]]", | |
4420 | 1, 3, ofctl_queue_get_config, OVS_RO }, | |
4421 | { "add-flow", "switch flow", | |
4422 | 2, 2, ofctl_add_flow, OVS_RW }, | |
4423 | { "add-flows", "switch file", | |
4424 | 2, 2, ofctl_add_flows, OVS_RW }, | |
4425 | { "mod-flows", "switch flow", | |
4426 | 2, 2, ofctl_mod_flows, OVS_RW }, | |
4427 | { "del-flows", "switch [flow]", | |
4428 | 1, 2, ofctl_del_flows, OVS_RW }, | |
4429 | { "replace-flows", "switch file", | |
4430 | 2, 2, ofctl_replace_flows, OVS_RW }, | |
4431 | { "diff-flows", "source1 source2", | |
4432 | 2, 2, ofctl_diff_flows, OVS_RW }, | |
4433 | { "add-meter", "switch meter", | |
4434 | 2, 2, ofctl_add_meter, OVS_RW }, | |
4435 | { "mod-meter", "switch meter", | |
4436 | 2, 2, ofctl_mod_meter, OVS_RW }, | |
4437 | { "del-meter", "switch meter", | |
4438 | 2, 2, ofctl_del_meters, OVS_RW }, | |
4439 | { "del-meters", "switch", | |
4440 | 1, 1, ofctl_del_meters, OVS_RW }, | |
4441 | { "dump-meter", "switch meter", | |
4442 | 2, 2, ofctl_dump_meters, OVS_RO }, | |
4443 | { "dump-meters", "switch", | |
4444 | 1, 1, ofctl_dump_meters, OVS_RO }, | |
4445 | { "meter-stats", "switch [meter]", | |
4446 | 1, 2, ofctl_meter_stats, OVS_RO }, | |
4447 | { "meter-features", "switch", | |
4448 | 1, 1, ofctl_meter_features, OVS_RO }, | |
4449 | { "packet-out", "switch \"in_port=<port> packet=<hex data> actions=...\"", | |
4450 | 2, INT_MAX, ofctl_packet_out, OVS_RW }, | |
4451 | { "dump-ports", "switch [port]", | |
4452 | 1, 2, ofctl_dump_ports, OVS_RO }, | |
4453 | { "dump-ports-desc", "switch [port]", | |
4454 | 1, 2, ofctl_dump_ports_desc, OVS_RO }, | |
4455 | { "mod-port", "switch iface act", | |
4456 | 3, 3, ofctl_mod_port, OVS_RW }, | |
4457 | { "mod-table", "switch mod", | |
4458 | 3, 3, ofctl_mod_table, OVS_RW }, | |
4459 | { "get-frags", "switch", | |
4460 | 1, 1, ofctl_get_frags, OVS_RO }, | |
4461 | { "set-frags", "switch frag_mode", | |
4462 | 2, 2, ofctl_set_frags, OVS_RW }, | |
4463 | { "probe", "target", | |
4464 | 1, 1, ofctl_probe, OVS_RO }, | |
4465 | { "ping", "target [n]", | |
4466 | 1, 2, ofctl_ping, OVS_RO }, | |
4467 | { "benchmark", "target n count", | |
4468 | 3, 3, ofctl_benchmark, OVS_RO }, | |
4469 | ||
4470 | { "dump-ipfix-bridge", "switch", | |
4471 | 1, 1, ofctl_dump_ipfix_bridge, OVS_RO }, | |
4472 | { "dump-ipfix-flow", "switch", | |
4473 | 1, 1, ofctl_dump_ipfix_flow, OVS_RO }, | |
4474 | ||
4475 | { "ofp-parse", "file", | |
4476 | 1, 1, ofctl_ofp_parse, OVS_RW }, | |
4477 | { "ofp-parse-pcap", "pcap", | |
4478 | 1, INT_MAX, ofctl_ofp_parse_pcap, OVS_RW }, | |
4479 | ||
4480 | { "add-group", "switch group", | |
4481 | 1, 2, ofctl_add_group, OVS_RW }, | |
4482 | { "add-groups", "switch file", | |
4483 | 1, 2, ofctl_add_groups, OVS_RW }, | |
4484 | { "mod-group", "switch group", | |
4485 | 1, 2, ofctl_mod_group, OVS_RW }, | |
4486 | { "del-groups", "switch [group]", | |
4487 | 1, 2, ofctl_del_groups, OVS_RW }, | |
4488 | { "insert-buckets", "switch [group]", | |
4489 | 1, 2, ofctl_insert_bucket, OVS_RW }, | |
4490 | { "remove-buckets", "switch [group]", | |
4491 | 1, 2, ofctl_remove_bucket, OVS_RW }, | |
4492 | { "dump-groups", "switch [group]", | |
4493 | 1, 2, ofctl_dump_group_desc, OVS_RO }, | |
4494 | { "dump-group-stats", "switch [group]", | |
4495 | 1, 2, ofctl_dump_group_stats, OVS_RO }, | |
4496 | { "dump-group-features", "switch", | |
4497 | 1, 1, ofctl_dump_group_features, OVS_RO }, | |
4498 | ||
4499 | { "bundle", "switch file", | |
4500 | 2, 2, ofctl_bundle, OVS_RW }, | |
4501 | ||
4502 | { "add-tlv-map", "switch map", | |
4503 | 2, 2, ofctl_add_tlv_map, OVS_RO }, | |
4504 | { "del-tlv-map", "switch [map]", | |
4505 | 1, 2, ofctl_del_tlv_map, OVS_RO }, | |
4506 | { "dump-tlv-map", "switch", | |
4507 | 1, 1, ofctl_dump_tlv_map, OVS_RO }, | |
4508 | { "help", NULL, 0, INT_MAX, ofctl_help, OVS_RO }, | |
4509 | { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands, OVS_RO }, | |
4510 | ||
4511 | /* Undocumented commands for testing. */ | |
4512 | { "parse-flow", NULL, 1, 1, ofctl_parse_flow, OVS_RW }, | |
4513 | { "parse-flows", NULL, 1, 1, ofctl_parse_flows, OVS_RW }, | |
4514 | { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm, OVS_RW }, | |
4515 | { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm, OVS_RW }, | |
4516 | { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm, OVS_RW }, | |
4517 | { "parse-actions", NULL, 1, 1, ofctl_parse_actions, OVS_RW }, | |
4518 | { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions, OVS_RW }, | |
4519 | { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match, OVS_RW }, | |
4520 | { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match, OVS_RW }, | |
4521 | { "parse-pcap", NULL, 1, INT_MAX, ofctl_parse_pcap, OVS_RW }, | |
4522 | { "check-vlan", NULL, 2, 2, ofctl_check_vlan, OVS_RW }, | |
4523 | { "print-error", NULL, 1, 1, ofctl_print_error, OVS_RW }, | |
4524 | { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply, OVS_RW }, | |
4525 | { "ofp-print", NULL, 1, 2, ofctl_ofp_print, OVS_RW }, | |
4526 | { "encode-hello", NULL, 1, 1, ofctl_encode_hello, OVS_RW }, | |
4527 | { "parse-key-value", NULL, 1, INT_MAX, ofctl_parse_key_value, OVS_RW }, | |
4528 | ||
4529 | { NULL, NULL, 0, 0, NULL, OVS_RO }, | |
4530 | }; | |
4531 | ||
4532 | static const struct ovs_cmdl_command *get_all_commands(void) | |
4533 | { | |
4534 | return all_commands; | |
4535 | } |