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