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