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