]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
50f96b10 | 2 | * Copyright (c) 2008-2017 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> | |
18 | #include "vconn-provider.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
21 | #include <netinet/in.h> | |
22 | #include <poll.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include "coverage.h" | |
b302749b | 26 | #include "fatal-signal.h" |
064af421 | 27 | #include "flow.h" |
064af421 BP |
28 | #include "openflow/nicira-ext.h" |
29 | #include "openflow/openflow.h" | |
d271907f BW |
30 | #include "openvswitch/dynamic-string.h" |
31 | #include "openvswitch/ofp-errors.h" | |
32 | #include "openvswitch/ofp-msgs.h" | |
25d436fb | 33 | #include "openvswitch/ofp-print.h" |
d271907f BW |
34 | #include "openvswitch/ofp-util.h" |
35 | #include "openvswitch/ofpbuf.h" | |
36 | #include "openvswitch/vlog.h" | |
064af421 BP |
37 | #include "packets.h" |
38 | #include "poll-loop.h" | |
39 | #include "random.h" | |
40 | #include "util.h" | |
f125905c | 41 | #include "socket-util.h" |
064af421 | 42 | |
d98e6007 | 43 | VLOG_DEFINE_THIS_MODULE(vconn); |
5136ce49 | 44 | |
d76f09ea BP |
45 | COVERAGE_DEFINE(vconn_open); |
46 | COVERAGE_DEFINE(vconn_received); | |
47 | COVERAGE_DEFINE(vconn_sent); | |
48 | ||
064af421 BP |
49 | /* State of an active vconn.*/ |
50 | enum vconn_state { | |
51 | /* This is the ordinary progression of states. */ | |
52 | VCS_CONNECTING, /* Underlying vconn is not connected. */ | |
53 | VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */ | |
54 | VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */ | |
55 | VCS_CONNECTED, /* Connection established. */ | |
56 | ||
57 | /* These states are entered only when something goes wrong. */ | |
58 | VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */ | |
59 | VCS_DISCONNECTED /* Connection failed or connection closed. */ | |
60 | }; | |
61 | ||
5b712636 | 62 | static const struct vconn_class *vconn_classes[] = { |
064af421 BP |
63 | &tcp_vconn_class, |
64 | &unix_vconn_class, | |
65 | #ifdef HAVE_OPENSSL | |
66 | &ssl_vconn_class, | |
67 | #endif | |
68 | }; | |
69 | ||
5b712636 | 70 | static const struct pvconn_class *pvconn_classes[] = { |
064af421 BP |
71 | &ptcp_pvconn_class, |
72 | &punix_pvconn_class, | |
73 | #ifdef HAVE_OPENSSL | |
74 | &pssl_pvconn_class, | |
75 | #endif | |
76 | }; | |
77 | ||
78 | /* Rate limit for individual OpenFlow messages going over the vconn, output at | |
79 | * DBG level. This is very high because, if these are enabled, it is because | |
80 | * we really need to see them. */ | |
81 | static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
82 | ||
83 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
84 | * in the peer and so there's not much point in showing a lot of them. */ | |
85 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
86 | ||
87 | static int do_recv(struct vconn *, struct ofpbuf **); | |
88 | static int do_send(struct vconn *, struct ofpbuf *); | |
89 | ||
90 | /* Check the validity of the vconn class structures. */ | |
91 | static void | |
92 | check_vconn_classes(void) | |
93 | { | |
94 | #ifndef NDEBUG | |
95 | size_t i; | |
96 | ||
97 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
5b712636 | 98 | const struct vconn_class *class = vconn_classes[i]; |
cb22974d BP |
99 | ovs_assert(class->name != NULL); |
100 | ovs_assert(class->open != NULL); | |
60cb3eb8 BP |
101 | if (class->close || class->recv || class->send |
102 | || class->run || class->run_wait || class->wait) { | |
cb22974d BP |
103 | ovs_assert(class->close != NULL); |
104 | ovs_assert(class->recv != NULL); | |
105 | ovs_assert(class->send != NULL); | |
106 | ovs_assert(class->wait != NULL); | |
064af421 BP |
107 | } else { |
108 | /* This class delegates to another one. */ | |
109 | } | |
110 | } | |
111 | ||
112 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
5b712636 | 113 | const struct pvconn_class *class = pvconn_classes[i]; |
cb22974d BP |
114 | ovs_assert(class->name != NULL); |
115 | ovs_assert(class->listen != NULL); | |
064af421 | 116 | if (class->close || class->accept || class->wait) { |
cb22974d BP |
117 | ovs_assert(class->close != NULL); |
118 | ovs_assert(class->accept != NULL); | |
119 | ovs_assert(class->wait != NULL); | |
064af421 BP |
120 | } else { |
121 | /* This class delegates to another one. */ | |
122 | } | |
123 | } | |
124 | #endif | |
125 | } | |
126 | ||
127 | /* Prints information on active (if 'active') and passive (if 'passive') | |
128 | * connection methods supported by the vconn. If 'bootstrap' is true, also | |
129 | * advertises options to bootstrap the CA certificate. */ | |
130 | void | |
67a4917b | 131 | vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED) |
064af421 BP |
132 | { |
133 | /* Really this should be implemented via callbacks into the vconn | |
134 | * providers, but that seems too heavy-weight to bother with at the | |
135 | * moment. */ | |
d295e8e9 | 136 | |
064af421 BP |
137 | printf("\n"); |
138 | if (active) { | |
139 | printf("Active OpenFlow connection methods:\n"); | |
6e797088 | 140 | printf(" tcp:IP[:PORT] " |
d4763d1d | 141 | "PORT (default: %d) at remote IP\n", OFP_PORT); |
064af421 | 142 | #ifdef HAVE_OPENSSL |
6e797088 | 143 | printf(" ssl:IP[:PORT] " |
d4763d1d | 144 | "SSL PORT (default: %d) at remote IP\n", OFP_PORT); |
064af421 BP |
145 | #endif |
146 | printf(" unix:FILE Unix domain socket named FILE\n"); | |
147 | } | |
148 | ||
149 | if (passive) { | |
150 | printf("Passive OpenFlow connection methods:\n"); | |
78ff0270 BP |
151 | printf(" ptcp:[PORT][:IP] " |
152 | "listen to TCP PORT (default: %d) on IP\n", | |
d4763d1d | 153 | OFP_PORT); |
064af421 | 154 | #ifdef HAVE_OPENSSL |
78ff0270 BP |
155 | printf(" pssl:[PORT][:IP] " |
156 | "listen for SSL on PORT (default: %d) on IP\n", | |
d4763d1d | 157 | OFP_PORT); |
064af421 BP |
158 | #endif |
159 | printf(" punix:FILE " | |
160 | "listen on Unix domain socket FILE\n"); | |
161 | } | |
162 | ||
163 | #ifdef HAVE_OPENSSL | |
164 | printf("PKI configuration (required to use SSL):\n" | |
165 | " -p, --private-key=FILE file with private key\n" | |
166 | " -c, --certificate=FILE file with certificate for private key\n" | |
167 | " -C, --ca-cert=FILE file with peer CA certificate\n"); | |
168 | if (bootstrap) { | |
169 | printf(" --bootstrap-ca-cert=FILE file with peer CA certificate " | |
170 | "to read or create\n"); | |
171 | } | |
172 | #endif | |
173 | } | |
174 | ||
30012c72 BP |
175 | /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class |
176 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
177 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
178 | * class exists. */ | |
179 | static int | |
5b712636 | 180 | vconn_lookup_class(const char *name, const struct vconn_class **classp) |
30012c72 BP |
181 | { |
182 | size_t prefix_len; | |
183 | ||
184 | prefix_len = strcspn(name, ":"); | |
185 | if (name[prefix_len] != '\0') { | |
186 | size_t i; | |
187 | ||
188 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
5b712636 | 189 | const struct vconn_class *class = vconn_classes[i]; |
30012c72 BP |
190 | if (strlen(class->name) == prefix_len |
191 | && !memcmp(class->name, name, prefix_len)) { | |
192 | *classp = class; | |
193 | return 0; | |
194 | } | |
195 | } | |
196 | } | |
197 | ||
198 | *classp = NULL; | |
199 | return EAFNOSUPPORT; | |
200 | } | |
201 | ||
202 | /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is | |
203 | * a supported connection type, otherwise EAFNOSUPPORT. */ | |
204 | int | |
205 | vconn_verify_name(const char *name) | |
206 | { | |
5b712636 | 207 | const struct vconn_class *class; |
30012c72 BP |
208 | return vconn_lookup_class(name, &class); |
209 | } | |
210 | ||
064af421 BP |
211 | /* Attempts to connect to an OpenFlow device. 'name' is a connection name in |
212 | * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS | |
213 | * are vconn class-specific. | |
214 | * | |
215 | * The vconn will automatically negotiate an OpenFlow protocol version | |
216 | * acceptable to both peers on the connection. The version negotiated will be | |
7a25bd99 SH |
217 | * one of those in the 'allowed_versions' bitmap: version 'x' is allowed if |
218 | * allowed_versions & (1 << x) is nonzero. If 'allowed_versions' is zero, then | |
219 | * OFPUTIL_DEFAULT_VERSIONS are allowed. | |
064af421 BP |
220 | * |
221 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
222 | * stores a pointer to the new connection in '*vconnp', otherwise a null | |
223 | * pointer. */ | |
224 | int | |
82c8c53c BP |
225 | vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, |
226 | struct vconn **vconnp) | |
064af421 | 227 | { |
5b712636 | 228 | const struct vconn_class *class; |
30012c72 BP |
229 | struct vconn *vconn; |
230 | char *suffix_copy; | |
231 | int error; | |
064af421 BP |
232 | |
233 | COVERAGE_INC(vconn_open); | |
234 | check_vconn_classes(); | |
235 | ||
7a25bd99 SH |
236 | if (!allowed_versions) { |
237 | allowed_versions = OFPUTIL_DEFAULT_VERSIONS; | |
238 | } | |
239 | ||
30012c72 BP |
240 | /* Look up the class. */ |
241 | error = vconn_lookup_class(name, &class); | |
242 | if (!class) { | |
243 | goto error; | |
064af421 | 244 | } |
30012c72 BP |
245 | |
246 | /* Call class's "open" function. */ | |
247 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
7a25bd99 | 248 | error = class->open(name, allowed_versions, suffix_copy, &vconn, dscp); |
30012c72 BP |
249 | free(suffix_copy); |
250 | if (error) { | |
251 | goto error; | |
064af421 | 252 | } |
30012c72 BP |
253 | |
254 | /* Success. */ | |
a445a8d8 | 255 | ovs_assert(vconn->state != VCS_CONNECTING || vconn->vclass->connect); |
30012c72 BP |
256 | *vconnp = vconn; |
257 | return 0; | |
258 | ||
259 | error: | |
260 | *vconnp = NULL; | |
261 | return error; | |
064af421 BP |
262 | } |
263 | ||
60cb3eb8 BP |
264 | /* Allows 'vconn' to perform maintenance activities, such as flushing output |
265 | * buffers. */ | |
266 | void | |
267 | vconn_run(struct vconn *vconn) | |
268 | { | |
48d84b17 BP |
269 | if (vconn->state == VCS_CONNECTING || |
270 | vconn->state == VCS_SEND_HELLO || | |
271 | vconn->state == VCS_RECV_HELLO) { | |
272 | vconn_connect(vconn); | |
273 | } | |
274 | ||
a445a8d8 AB |
275 | if (vconn->vclass->run) { |
276 | (vconn->vclass->run)(vconn); | |
60cb3eb8 BP |
277 | } |
278 | } | |
279 | ||
280 | /* Arranges for the poll loop to wake up when 'vconn' needs to perform | |
281 | * maintenance activities. */ | |
282 | void | |
283 | vconn_run_wait(struct vconn *vconn) | |
284 | { | |
48d84b17 BP |
285 | if (vconn->state == VCS_CONNECTING || |
286 | vconn->state == VCS_SEND_HELLO || | |
287 | vconn->state == VCS_RECV_HELLO) { | |
288 | vconn_connect_wait(vconn); | |
289 | } | |
290 | ||
a445a8d8 AB |
291 | if (vconn->vclass->run_wait) { |
292 | (vconn->vclass->run_wait)(vconn); | |
60cb3eb8 BP |
293 | } |
294 | } | |
295 | ||
accaecc4 BP |
296 | /* Returns 0 if 'vconn' is healthy (connecting or connected), a positive errno |
297 | * value if the connection died abnormally (connection failed or aborted), or | |
298 | * EOF if the connection was closed in a normal way. */ | |
299 | int | |
300 | vconn_get_status(const struct vconn *vconn) | |
301 | { | |
77587e0f | 302 | return vconn->error == EAGAIN ? 0 : vconn->error; |
accaecc4 BP |
303 | } |
304 | ||
064af421 | 305 | int |
82c8c53c | 306 | vconn_open_block(const char *name, uint32_t allowed_versions, uint8_t dscp, |
2e3fa633 | 307 | struct vconn **vconnp) |
064af421 BP |
308 | { |
309 | struct vconn *vconn; | |
310 | int error; | |
311 | ||
b302749b BP |
312 | fatal_signal_run(); |
313 | ||
82c8c53c | 314 | error = vconn_open(name, allowed_versions, dscp, &vconn); |
b0bfeb3e | 315 | if (!error) { |
6d1fb217 | 316 | error = vconn_connect_block(vconn); |
064af421 | 317 | } |
b0bfeb3e | 318 | |
064af421 BP |
319 | if (error) { |
320 | vconn_close(vconn); | |
321 | *vconnp = NULL; | |
322 | } else { | |
323 | *vconnp = vconn; | |
324 | } | |
325 | return error; | |
326 | } | |
327 | ||
328 | /* Closes 'vconn'. */ | |
329 | void | |
330 | vconn_close(struct vconn *vconn) | |
331 | { | |
332 | if (vconn != NULL) { | |
333 | char *name = vconn->name; | |
a445a8d8 | 334 | (vconn->vclass->close)(vconn); |
064af421 BP |
335 | free(name); |
336 | } | |
337 | } | |
338 | ||
339 | /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */ | |
340 | const char * | |
341 | vconn_get_name(const struct vconn *vconn) | |
342 | { | |
343 | return vconn->name; | |
344 | } | |
345 | ||
7a25bd99 SH |
346 | /* Returns the allowed_versions of 'vconn', that is, |
347 | * the allowed_versions passed to vconn_open(). */ | |
348 | uint32_t | |
349 | vconn_get_allowed_versions(const struct vconn *vconn) | |
350 | { | |
351 | return vconn->allowed_versions; | |
352 | } | |
353 | ||
e182670b SH |
354 | /* Sets the allowed_versions of 'vconn', overriding |
355 | * the allowed_versions passed to vconn_open(). */ | |
356 | void | |
357 | vconn_set_allowed_versions(struct vconn *vconn, uint32_t allowed_versions) | |
358 | { | |
359 | vconn->allowed_versions = allowed_versions; | |
360 | } | |
361 | ||
27527aa0 BP |
362 | /* Returns the OpenFlow version negotiated with the peer, or -1 if version |
363 | * negotiation is not yet complete. | |
364 | * | |
365 | * A vconn that has successfully connected (that is, vconn_connect() or | |
366 | * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */ | |
733c8ed3 | 367 | int |
27527aa0 BP |
368 | vconn_get_version(const struct vconn *vconn) |
369 | { | |
733c8ed3 | 370 | return vconn->version ? vconn->version : -1; |
27527aa0 BP |
371 | } |
372 | ||
4766ce7a BP |
373 | /* By default, a vconn accepts only OpenFlow messages whose version matches the |
374 | * one negotiated for the connection. A message received with a different | |
375 | * version is an error that causes the vconn to drop the connection. | |
376 | * | |
377 | * This functions allows 'vconn' to accept messages with any OpenFlow version. | |
378 | * This is useful in the special case where 'vconn' is used as an rconn | |
379 | * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is | |
380 | * used as a target for mirroring OpenFlow messages for debugging and | |
381 | * troubleshooting. | |
382 | * | |
383 | * This function should be called after a successful vconn_open() or | |
384 | * pvconn_accept() but before the connection completes, that is, before | |
385 | * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn' | |
386 | * beforehand with an unexpected version will the vconn to drop the | |
387 | * connection. */ | |
388 | void | |
389 | vconn_set_recv_any_version(struct vconn *vconn) | |
390 | { | |
391 | vconn->recv_any_version = true; | |
392 | } | |
393 | ||
064af421 | 394 | static void |
d295e8e9 | 395 | vcs_connecting(struct vconn *vconn) |
064af421 | 396 | { |
a445a8d8 | 397 | int retval = (vconn->vclass->connect)(vconn); |
cb22974d | 398 | ovs_assert(retval != EINPROGRESS); |
064af421 BP |
399 | if (!retval) { |
400 | vconn->state = VCS_SEND_HELLO; | |
401 | } else if (retval != EAGAIN) { | |
402 | vconn->state = VCS_DISCONNECTED; | |
403 | vconn->error = retval; | |
404 | } | |
405 | } | |
406 | ||
407 | static void | |
408 | vcs_send_hello(struct vconn *vconn) | |
409 | { | |
410 | struct ofpbuf *b; | |
411 | int retval; | |
412 | ||
de6c85b0 | 413 | b = ofputil_encode_hello(vconn->allowed_versions); |
064af421 BP |
414 | retval = do_send(vconn, b); |
415 | if (!retval) { | |
416 | vconn->state = VCS_RECV_HELLO; | |
417 | } else { | |
418 | ofpbuf_delete(b); | |
419 | if (retval != EAGAIN) { | |
420 | vconn->state = VCS_DISCONNECTED; | |
421 | vconn->error = retval; | |
422 | } | |
423 | } | |
424 | } | |
425 | ||
7a25bd99 SH |
426 | static char * |
427 | version_bitmap_to_string(uint32_t bitmap) | |
428 | { | |
429 | struct ds s; | |
430 | ||
431 | ds_init(&s); | |
432 | if (!bitmap) { | |
433 | ds_put_cstr(&s, "no versions"); | |
434 | } else if (is_pow2(bitmap)) { | |
435 | ds_put_cstr(&s, "version "); | |
436 | ofputil_format_version(&s, leftmost_1bit_idx(bitmap)); | |
437 | } else if (is_pow2((bitmap >> 1) + 1)) { | |
438 | ds_put_cstr(&s, "version "); | |
439 | ofputil_format_version(&s, leftmost_1bit_idx(bitmap)); | |
6cca5c20 | 440 | ds_put_cstr(&s, " and earlier"); |
7a25bd99 SH |
441 | } else { |
442 | ds_put_cstr(&s, "versions "); | |
443 | ofputil_format_version_bitmap(&s, bitmap); | |
444 | } | |
445 | return ds_steal_cstr(&s); | |
446 | } | |
447 | ||
064af421 BP |
448 | static void |
449 | vcs_recv_hello(struct vconn *vconn) | |
450 | { | |
451 | struct ofpbuf *b; | |
452 | int retval; | |
453 | ||
454 | retval = do_recv(vconn, &b); | |
455 | if (!retval) { | |
982697a4 BP |
456 | enum ofptype type; |
457 | enum ofperr error; | |
064af421 | 458 | |
6fd6ed71 | 459 | error = ofptype_decode(&type, b->data); |
982697a4 | 460 | if (!error && type == OFPTYPE_HELLO) { |
7a25bd99 SH |
461 | char *peer_s, *local_s; |
462 | uint32_t common_versions; | |
463 | ||
6fd6ed71 | 464 | if (!ofputil_decode_hello(b->data, &vconn->peer_versions)) { |
064af421 | 465 | struct ds msg = DS_EMPTY_INITIALIZER; |
de6c85b0 SH |
466 | ds_put_format(&msg, "%s: unknown data in hello:\n", |
467 | vconn->name); | |
6fd6ed71 | 468 | ds_put_hex_dump(&msg, b->data, b->size, 0, true); |
064af421 BP |
469 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg)); |
470 | ds_destroy(&msg); | |
471 | } | |
472 | ||
7a25bd99 SH |
473 | local_s = version_bitmap_to_string(vconn->allowed_versions); |
474 | peer_s = version_bitmap_to_string(vconn->peer_versions); | |
475 | ||
476 | common_versions = vconn->peer_versions & vconn->allowed_versions; | |
477 | if (!common_versions) { | |
478 | vconn->version = leftmost_1bit_idx(vconn->peer_versions); | |
064af421 | 479 | VLOG_WARN_RL(&bad_ofmsg_rl, |
7a25bd99 SH |
480 | "%s: version negotiation failed (we support " |
481 | "%s, peer supports %s)", | |
482 | vconn->name, local_s, peer_s); | |
064af421 BP |
483 | vconn->state = VCS_SEND_ERROR; |
484 | } else { | |
7a25bd99 | 485 | vconn->version = leftmost_1bit_idx(common_versions); |
064af421 | 486 | VLOG_DBG("%s: negotiated OpenFlow version 0x%02x " |
7a25bd99 SH |
487 | "(we support %s, peer supports %s)", vconn->name, |
488 | vconn->version, local_s, peer_s); | |
064af421 BP |
489 | vconn->state = VCS_CONNECTED; |
490 | } | |
7a25bd99 SH |
491 | |
492 | free(local_s); | |
493 | free(peer_s); | |
494 | ||
064af421 BP |
495 | ofpbuf_delete(b); |
496 | return; | |
497 | } else { | |
50f96b10 | 498 | char *s = ofp_to_string(b->data, b->size, NULL, 1); |
064af421 BP |
499 | VLOG_WARN_RL(&bad_ofmsg_rl, |
500 | "%s: received message while expecting hello: %s", | |
501 | vconn->name, s); | |
502 | free(s); | |
503 | retval = EPROTO; | |
504 | ofpbuf_delete(b); | |
505 | } | |
506 | } | |
507 | ||
508 | if (retval != EAGAIN) { | |
509 | vconn->state = VCS_DISCONNECTED; | |
b7eae257 | 510 | vconn->error = retval == EOF ? ECONNRESET : retval; |
064af421 BP |
511 | } |
512 | } | |
513 | ||
514 | static void | |
515 | vcs_send_error(struct vconn *vconn) | |
516 | { | |
064af421 BP |
517 | struct ofpbuf *b; |
518 | char s[128]; | |
519 | int retval; | |
7a25bd99 SH |
520 | char *local_s, *peer_s; |
521 | ||
522 | local_s = version_bitmap_to_string(vconn->allowed_versions); | |
523 | peer_s = version_bitmap_to_string(vconn->peer_versions); | |
524 | snprintf(s, sizeof s, "We support %s, you support %s, no common versions.", | |
525 | local_s, peer_s); | |
526 | free(peer_s); | |
527 | free(local_s); | |
064af421 | 528 | |
9b7e2112 | 529 | b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE, vconn->version, s); |
064af421 BP |
530 | retval = do_send(vconn, b); |
531 | if (retval) { | |
532 | ofpbuf_delete(b); | |
533 | } | |
534 | if (retval != EAGAIN) { | |
535 | vconn->state = VCS_DISCONNECTED; | |
536 | vconn->error = retval ? retval : EPROTO; | |
537 | } | |
538 | } | |
539 | ||
294e9fc8 BP |
540 | /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is |
541 | * complete, returns 0 if the connection was successful or a positive errno | |
542 | * value if it failed. If the connection is still in progress, returns | |
543 | * EAGAIN. */ | |
064af421 BP |
544 | int |
545 | vconn_connect(struct vconn *vconn) | |
546 | { | |
547 | enum vconn_state last_state; | |
548 | ||
064af421 BP |
549 | do { |
550 | last_state = vconn->state; | |
551 | switch (vconn->state) { | |
552 | case VCS_CONNECTING: | |
553 | vcs_connecting(vconn); | |
554 | break; | |
555 | ||
556 | case VCS_SEND_HELLO: | |
557 | vcs_send_hello(vconn); | |
558 | break; | |
559 | ||
560 | case VCS_RECV_HELLO: | |
561 | vcs_recv_hello(vconn); | |
562 | break; | |
563 | ||
564 | case VCS_CONNECTED: | |
565 | return 0; | |
566 | ||
567 | case VCS_SEND_ERROR: | |
568 | vcs_send_error(vconn); | |
569 | break; | |
570 | ||
571 | case VCS_DISCONNECTED: | |
572 | return vconn->error; | |
573 | ||
574 | default: | |
428b2edd | 575 | OVS_NOT_REACHED(); |
064af421 BP |
576 | } |
577 | } while (vconn->state != last_state); | |
578 | ||
579 | return EAGAIN; | |
580 | } | |
581 | ||
294e9fc8 BP |
582 | /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores |
583 | * the received message into '*msgp' and returns 0. The caller is responsible | |
584 | * for destroying the message with ofpbuf_delete(). On failure, returns a | |
585 | * positive errno value and stores a null pointer into '*msgp'. On normal | |
586 | * connection close, returns EOF. | |
064af421 BP |
587 | * |
588 | * vconn_recv will not block waiting for a packet to arrive. If no packets | |
589 | * have been received, it returns EAGAIN immediately. */ | |
590 | int | |
591 | vconn_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
592 | { | |
982697a4 BP |
593 | struct ofpbuf *msg; |
594 | int retval; | |
595 | ||
596 | retval = vconn_connect(vconn); | |
597 | if (!retval) { | |
598 | retval = do_recv(vconn, &msg); | |
599 | } | |
4766ce7a | 600 | if (!retval && !vconn->recv_any_version) { |
6fd6ed71 | 601 | const struct ofp_header *oh = msg->data; |
982697a4 BP |
602 | if (oh->version != vconn->version) { |
603 | enum ofptype type; | |
604 | ||
6fd6ed71 | 605 | if (ofptype_decode(&type, msg->data) |
982697a4 BP |
606 | || (type != OFPTYPE_HELLO && |
607 | type != OFPTYPE_ERROR && | |
608 | type != OFPTYPE_ECHO_REQUEST && | |
609 | type != OFPTYPE_ECHO_REPLY)) { | |
2b4cca6f BP |
610 | struct ofpbuf *reply; |
611 | ||
982697a4 BP |
612 | VLOG_ERR_RL(&bad_ofmsg_rl, "%s: received OpenFlow version " |
613 | "0x%02"PRIx8" != expected %02x", | |
614 | vconn->name, oh->version, vconn->version); | |
2b4cca6f BP |
615 | |
616 | /* Send a "bad version" reply, if we can. */ | |
617 | reply = ofperr_encode_reply(OFPERR_OFPBRC_BAD_VERSION, oh); | |
618 | retval = vconn_send(vconn, reply); | |
619 | if (retval) { | |
620 | VLOG_INFO_RL(&bad_ofmsg_rl, | |
621 | "%s: failed to queue error reply (%s)", | |
622 | vconn->name, ovs_strerror(retval)); | |
623 | ofpbuf_delete(reply); | |
624 | } | |
625 | ||
626 | /* Suppress the received message, as if it had not arrived. */ | |
627 | retval = EAGAIN; | |
982697a4 | 628 | ofpbuf_delete(msg); |
982697a4 BP |
629 | } |
630 | } | |
064af421 | 631 | } |
982697a4 BP |
632 | |
633 | *msgp = retval ? NULL : msg; | |
064af421 BP |
634 | return retval; |
635 | } | |
636 | ||
637 | static int | |
638 | do_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
639 | { | |
a445a8d8 | 640 | int retval = (vconn->vclass->recv)(vconn, msgp); |
064af421 | 641 | if (!retval) { |
064af421 BP |
642 | COVERAGE_INC(vconn_received); |
643 | if (VLOG_IS_DBG_ENABLED()) { | |
50f96b10 | 644 | char *s = ofp_to_string((*msgp)->data, (*msgp)->size, NULL, 1); |
064af421 BP |
645 | VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s); |
646 | free(s); | |
647 | } | |
064af421 BP |
648 | } |
649 | return retval; | |
650 | } | |
651 | ||
294e9fc8 BP |
652 | /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0, |
653 | * in which case ownership of 'msg' is transferred to the vconn. Success does | |
654 | * not guarantee that 'msg' has been or ever will be delivered to the peer, | |
655 | * only that it has been queued for transmission. | |
064af421 BP |
656 | * |
657 | * Returns a positive errno value on failure, in which case the caller | |
658 | * retains ownership of 'msg'. | |
659 | * | |
660 | * vconn_send will not block. If 'msg' cannot be immediately accepted for | |
661 | * transmission, it returns EAGAIN immediately. */ | |
662 | int | |
663 | vconn_send(struct vconn *vconn, struct ofpbuf *msg) | |
664 | { | |
665 | int retval = vconn_connect(vconn); | |
666 | if (!retval) { | |
667 | retval = do_send(vconn, msg); | |
668 | } | |
669 | return retval; | |
670 | } | |
671 | ||
672 | static int | |
673 | do_send(struct vconn *vconn, struct ofpbuf *msg) | |
674 | { | |
675 | int retval; | |
676 | ||
6fd6ed71 | 677 | ovs_assert(msg->size >= sizeof(struct ofp_header)); |
982697a4 BP |
678 | |
679 | ofpmsg_update_length(msg); | |
064af421 BP |
680 | if (!VLOG_IS_DBG_ENABLED()) { |
681 | COVERAGE_INC(vconn_sent); | |
a445a8d8 | 682 | retval = (vconn->vclass->send)(vconn, msg); |
064af421 | 683 | } else { |
50f96b10 | 684 | char *s = ofp_to_string(msg->data, msg->size, NULL, 1); |
a445a8d8 | 685 | retval = (vconn->vclass->send)(vconn, msg); |
064af421 BP |
686 | if (retval != EAGAIN) { |
687 | VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s", | |
10a89ef0 | 688 | vconn->name, ovs_strerror(retval), s); |
064af421 BP |
689 | } |
690 | free(s); | |
691 | } | |
692 | return retval; | |
693 | } | |
694 | ||
6d1fb217 BP |
695 | /* Same as vconn_connect(), except that it waits until the connection on |
696 | * 'vconn' completes or fails. Thus, it will never return EAGAIN. */ | |
697 | int | |
698 | vconn_connect_block(struct vconn *vconn) | |
699 | { | |
700 | int error; | |
701 | ||
702 | while ((error = vconn_connect(vconn)) == EAGAIN) { | |
703 | vconn_run(vconn); | |
704 | vconn_run_wait(vconn); | |
705 | vconn_connect_wait(vconn); | |
706 | poll_block(); | |
707 | } | |
cb22974d | 708 | ovs_assert(error != EINPROGRESS); |
6d1fb217 BP |
709 | |
710 | return error; | |
711 | } | |
712 | ||
064af421 BP |
713 | /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */ |
714 | int | |
715 | vconn_send_block(struct vconn *vconn, struct ofpbuf *msg) | |
716 | { | |
717 | int retval; | |
b302749b BP |
718 | |
719 | fatal_signal_run(); | |
720 | ||
064af421 | 721 | while ((retval = vconn_send(vconn, msg)) == EAGAIN) { |
60cb3eb8 BP |
722 | vconn_run(vconn); |
723 | vconn_run_wait(vconn); | |
064af421 BP |
724 | vconn_send_wait(vconn); |
725 | poll_block(); | |
726 | } | |
727 | return retval; | |
728 | } | |
729 | ||
730 | /* Same as vconn_recv, except that it waits until a message is received. */ | |
731 | int | |
732 | vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp) | |
733 | { | |
734 | int retval; | |
b302749b BP |
735 | |
736 | fatal_signal_run(); | |
737 | ||
064af421 | 738 | while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) { |
60cb3eb8 BP |
739 | vconn_run(vconn); |
740 | vconn_run_wait(vconn); | |
064af421 BP |
741 | vconn_recv_wait(vconn); |
742 | poll_block(); | |
743 | } | |
744 | return retval; | |
745 | } | |
746 | ||
db5076ee JR |
747 | static int |
748 | vconn_recv_xid__(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp, | |
506c1ddb | 749 | struct ovs_list *errors) |
064af421 BP |
750 | { |
751 | for (;;) { | |
4408d18a | 752 | ovs_be32 recv_xid; |
064af421 | 753 | struct ofpbuf *reply; |
db5076ee JR |
754 | const struct ofp_header *oh; |
755 | enum ofptype type; | |
064af421 BP |
756 | int error; |
757 | ||
758 | error = vconn_recv_block(vconn, &reply); | |
759 | if (error) { | |
760 | *replyp = NULL; | |
761 | return error; | |
762 | } | |
db5076ee JR |
763 | oh = reply->data; |
764 | recv_xid = oh->xid; | |
064af421 BP |
765 | if (xid == recv_xid) { |
766 | *replyp = reply; | |
767 | return 0; | |
768 | } | |
769 | ||
db5076ee | 770 | error = ofptype_decode(&type, oh); |
506c1ddb | 771 | if (!error && type == OFPTYPE_ERROR) { |
209aa4ad | 772 | ovs_list_push_back(errors, &reply->list_node); |
db5076ee JR |
773 | } else { |
774 | VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32 | |
775 | " != expected %08"PRIx32, | |
776 | vconn->name, ntohl(recv_xid), ntohl(xid)); | |
209aa4ad | 777 | ofpbuf_delete(reply); |
db5076ee | 778 | } |
064af421 BP |
779 | } |
780 | } | |
781 | ||
db5076ee JR |
782 | /* Waits until a message with a transaction ID matching 'xid' is received on |
783 | * 'vconn'. Returns 0 if successful, in which case the reply is stored in | |
784 | * '*replyp' for the caller to examine and free. Otherwise returns a positive | |
785 | * errno value, or EOF, and sets '*replyp' to null. | |
786 | * | |
787 | * 'request' is always destroyed, regardless of the return value. */ | |
788 | int | |
789 | vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp) | |
790 | { | |
791 | return vconn_recv_xid__(vconn, xid, replyp, NULL); | |
792 | } | |
793 | ||
794 | static int | |
795 | vconn_transact__(struct vconn *vconn, struct ofpbuf *request, | |
506c1ddb | 796 | struct ofpbuf **replyp, struct ovs_list *errors) |
db5076ee JR |
797 | { |
798 | ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid; | |
799 | int error; | |
800 | ||
801 | *replyp = NULL; | |
802 | error = vconn_send_block(vconn, request); | |
803 | if (error) { | |
804 | ofpbuf_delete(request); | |
805 | } | |
506c1ddb | 806 | return error ? error : vconn_recv_xid__(vconn, send_xid, replyp, errors); |
db5076ee JR |
807 | } |
808 | ||
064af421 BP |
809 | /* Sends 'request' to 'vconn' and blocks until it receives a reply with a |
810 | * matching transaction ID. Returns 0 if successful, in which case the reply | |
811 | * is stored in '*replyp' for the caller to examine and free. Otherwise | |
812 | * returns a positive errno value, or EOF, and sets '*replyp' to null. | |
813 | * | |
33af7dca BP |
814 | * 'request' should be an OpenFlow request that requires a reply. Otherwise, |
815 | * if there is no reply, this function can end up blocking forever (or until | |
816 | * the peer drops the connection). | |
817 | * | |
064af421 BP |
818 | * 'request' is always destroyed, regardless of the return value. */ |
819 | int | |
820 | vconn_transact(struct vconn *vconn, struct ofpbuf *request, | |
821 | struct ofpbuf **replyp) | |
822 | { | |
db5076ee | 823 | return vconn_transact__(vconn, request, replyp, NULL); |
064af421 BP |
824 | } |
825 | ||
506c1ddb JR |
826 | static int |
827 | vconn_send_barrier(struct vconn *vconn, ovs_be32 *barrier_xid) | |
828 | { | |
829 | struct ofpbuf *barrier; | |
830 | int error; | |
831 | ||
832 | /* Send barrier. */ | |
833 | barrier = ofputil_encode_barrier_request(vconn_get_version(vconn)); | |
834 | *barrier_xid = ((struct ofp_header *) barrier->data)->xid; | |
835 | error = vconn_send_block(vconn, barrier); | |
836 | if (error) { | |
837 | ofpbuf_delete(barrier); | |
838 | } | |
839 | return error; | |
840 | } | |
841 | ||
33af7dca BP |
842 | /* Sends 'request' followed by a barrier request to 'vconn', then blocks until |
843 | * it receives a reply to the barrier. If successful, stores the reply to | |
844 | * 'request' in '*replyp', if one was received, and otherwise NULL, then | |
845 | * returns 0. Otherwise returns a positive errno value, or EOF, and sets | |
846 | * '*replyp' to null. | |
847 | * | |
848 | * This function is useful for sending an OpenFlow request that doesn't | |
849 | * ordinarily include a reply but might report an error in special | |
850 | * circumstances. | |
851 | * | |
852 | * 'request' is always destroyed, regardless of the return value. */ | |
853 | int | |
854 | vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request, | |
855 | struct ofpbuf **replyp) | |
856 | { | |
857 | ovs_be32 request_xid; | |
858 | ovs_be32 barrier_xid; | |
33af7dca BP |
859 | int error; |
860 | ||
861 | *replyp = NULL; | |
862 | ||
863 | /* Send request. */ | |
6fd6ed71 | 864 | request_xid = ((struct ofp_header *) request->data)->xid; |
33af7dca BP |
865 | error = vconn_send_block(vconn, request); |
866 | if (error) { | |
867 | ofpbuf_delete(request); | |
868 | return error; | |
869 | } | |
870 | ||
871 | /* Send barrier. */ | |
506c1ddb | 872 | error = vconn_send_barrier(vconn, &barrier_xid); |
33af7dca | 873 | if (error) { |
33af7dca BP |
874 | return error; |
875 | } | |
876 | ||
877 | for (;;) { | |
878 | struct ofpbuf *msg; | |
879 | ovs_be32 msg_xid; | |
880 | int error; | |
881 | ||
882 | error = vconn_recv_block(vconn, &msg); | |
883 | if (error) { | |
884 | ofpbuf_delete(*replyp); | |
885 | *replyp = NULL; | |
886 | return error; | |
887 | } | |
888 | ||
6fd6ed71 | 889 | msg_xid = ((struct ofp_header *) msg->data)->xid; |
33af7dca BP |
890 | if (msg_xid == request_xid) { |
891 | if (*replyp) { | |
892 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with " | |
893 | "xid %08"PRIx32, vconn->name, ntohl(msg_xid)); | |
894 | ofpbuf_delete(*replyp); | |
895 | } | |
896 | *replyp = msg; | |
897 | } else { | |
898 | ofpbuf_delete(msg); | |
899 | if (msg_xid == barrier_xid) { | |
900 | return 0; | |
901 | } else { | |
902 | VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32 | |
903 | " != expected %08"PRIx32" or %08"PRIx32, | |
904 | vconn->name, ntohl(msg_xid), | |
905 | ntohl(request_xid), ntohl(barrier_xid)); | |
906 | } | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
7f009380 BP |
911 | /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one. |
912 | * All of the requests on 'requests' are always destroyed, regardless of the | |
913 | * return value. */ | |
914 | int | |
ca6ba700 | 915 | vconn_transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests, |
7f009380 BP |
916 | struct ofpbuf **replyp) |
917 | { | |
5f03c983 | 918 | struct ofpbuf *request; |
7f009380 | 919 | |
5f03c983 | 920 | LIST_FOR_EACH_POP (request, list_node, requests) { |
7f009380 BP |
921 | int error; |
922 | ||
7f009380 BP |
923 | error = vconn_transact_noreply(vconn, request, replyp); |
924 | if (error || *replyp) { | |
925 | ofpbuf_list_delete(requests); | |
926 | return error; | |
927 | } | |
928 | } | |
929 | ||
930 | *replyp = NULL; | |
931 | return 0; | |
932 | } | |
933 | ||
d444a914 BP |
934 | static int |
935 | recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid, | |
936 | struct ofpbuf **replyp, | |
937 | struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts) | |
938 | { | |
939 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
940 | struct ofpbuf *reply = *replyp; | |
941 | ||
942 | for (;;) { | |
943 | int retval; | |
944 | bool more; | |
945 | ||
946 | /* Get a flow stats reply message, if we don't already have one. */ | |
947 | if (!reply) { | |
948 | enum ofptype type; | |
949 | enum ofperr error; | |
950 | ||
951 | do { | |
952 | error = vconn_recv_block(vconn, &reply); | |
953 | if (error) { | |
954 | return error; | |
955 | } | |
956 | } while (((struct ofp_header *) reply->data)->xid != send_xid); | |
957 | ||
958 | error = ofptype_decode(&type, reply->data); | |
959 | if (error || type != OFPTYPE_FLOW_STATS_REPLY) { | |
960 | VLOG_WARN_RL(&rl, "received bad reply: %s", | |
50f96b10 | 961 | ofp_to_string(reply->data, reply->size, NULL, 1)); |
d444a914 BP |
962 | return EPROTO; |
963 | } | |
964 | } | |
965 | ||
966 | /* Pull an individual flow stats reply out of the message. */ | |
967 | retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts); | |
968 | switch (retval) { | |
969 | case 0: | |
970 | *replyp = reply; | |
971 | return 0; | |
972 | ||
973 | case EOF: | |
974 | more = ofpmp_more(reply->header); | |
975 | ofpbuf_delete(reply); | |
976 | reply = NULL; | |
977 | if (!more) { | |
978 | *replyp = NULL; | |
979 | return EOF; | |
980 | } | |
981 | break; | |
982 | ||
983 | default: | |
984 | VLOG_WARN_RL(&rl, "parse error in reply (%s)", | |
985 | ofperr_to_string(retval)); | |
986 | return EPROTO; | |
987 | } | |
988 | } | |
989 | } | |
990 | ||
991 | /* Sends 'fsr' to 'vconn', encoding it with the given 'protocol', and then | |
992 | * waits for, parses, and accumulates all of the replies into '*fsesp' and | |
993 | * '*n_fsesp'. The caller is responsible for freeing all of the flows. | |
994 | * Returns 0 if successful, otherwise a positive errno value. */ | |
995 | int | |
996 | vconn_dump_flows(struct vconn *vconn, | |
997 | const struct ofputil_flow_stats_request *fsr, | |
998 | enum ofputil_protocol protocol, | |
999 | struct ofputil_flow_stats **fsesp, size_t *n_fsesp) | |
1000 | { | |
1001 | struct ofputil_flow_stats *fses = NULL; | |
1002 | size_t n_fses = 0; | |
1003 | size_t allocated_fses = 0; | |
1004 | ||
1005 | struct ofpbuf *request = ofputil_encode_flow_stats_request(fsr, protocol); | |
1006 | const struct ofp_header *oh = request->data; | |
1007 | ovs_be32 send_xid = oh->xid; | |
1008 | int error = vconn_send_block(vconn, request); | |
1009 | if (error) { | |
1010 | goto exit; | |
1011 | } | |
1012 | ||
1013 | struct ofpbuf *reply = NULL; | |
1014 | struct ofpbuf ofpacts; | |
1015 | ofpbuf_init(&ofpacts, 0); | |
1016 | for (;;) { | |
1017 | if (n_fses >= allocated_fses) { | |
1018 | fses = x2nrealloc(fses, &allocated_fses, sizeof *fses); | |
1019 | } | |
1020 | ||
1021 | struct ofputil_flow_stats *fs = &fses[n_fses]; | |
1022 | error = recv_flow_stats_reply(vconn, send_xid, &reply, fs, &ofpacts); | |
1023 | if (error) { | |
1024 | if (error == EOF) { | |
1025 | error = 0; | |
1026 | } | |
1027 | break; | |
1028 | } | |
1029 | fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len); | |
1030 | n_fses++; | |
1031 | } | |
1032 | ofpbuf_uninit(&ofpacts); | |
1033 | ofpbuf_delete(reply); | |
1034 | ||
1035 | if (error) { | |
1036 | for (size_t i = 0; i < n_fses; i++) { | |
1037 | free(CONST_CAST(struct ofpact *, fses[i].ofpacts)); | |
1038 | } | |
1039 | free(fses); | |
1040 | ||
1041 | fses = NULL; | |
1042 | n_fses = 0; | |
1043 | } | |
1044 | ||
1045 | exit: | |
1046 | *fsesp = fses; | |
1047 | *n_fsesp = n_fses; | |
1048 | return error; | |
1049 | } | |
1050 | ||
1051 | ||
db5076ee JR |
1052 | static enum ofperr |
1053 | vconn_bundle_reply_validate(struct ofpbuf *reply, | |
1054 | struct ofputil_bundle_ctrl_msg *request, | |
506c1ddb | 1055 | struct ovs_list *errors) |
db5076ee JR |
1056 | { |
1057 | const struct ofp_header *oh; | |
1058 | enum ofptype type; | |
1059 | enum ofperr error; | |
1060 | struct ofputil_bundle_ctrl_msg rbc; | |
1061 | ||
1062 | oh = reply->data; | |
1063 | error = ofptype_decode(&type, oh); | |
1064 | if (error) { | |
1065 | return error; | |
1066 | } | |
1067 | ||
1068 | if (type == OFPTYPE_ERROR) { | |
209aa4ad BP |
1069 | struct ofpbuf *copy = ofpbuf_clone(reply); |
1070 | ovs_list_push_back(errors, ©->list_node); | |
db5076ee JR |
1071 | return ofperr_decode_msg(oh, NULL); |
1072 | } | |
1073 | if (type != OFPTYPE_BUNDLE_CONTROL) { | |
1074 | return OFPERR_OFPBRC_BAD_TYPE; | |
1075 | } | |
1076 | ||
1077 | error = ofputil_decode_bundle_ctrl(oh, &rbc); | |
1078 | if (error) { | |
1079 | return error; | |
1080 | } | |
1081 | ||
1082 | if (rbc.bundle_id != request->bundle_id) { | |
1083 | return OFPERR_OFPBFC_BAD_ID; | |
1084 | } | |
1085 | ||
1086 | if (rbc.type != request->type + 1) { | |
1087 | return OFPERR_OFPBFC_BAD_TYPE; | |
1088 | } | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
1093 | /* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either | |
1094 | * an error or the corresponding bundle control message response. | |
1095 | * | |
506c1ddb JR |
1096 | * 'errors' is a list to which any OpenFlow errors relating to bundle |
1097 | * processing are appended. Caller is responsible for releasing the memory of | |
1098 | * each node in the list on return. | |
db5076ee JR |
1099 | * |
1100 | * Returns errno value, or 0 when successful. */ | |
1101 | static int | |
1102 | vconn_bundle_control_transact(struct vconn *vconn, | |
1103 | struct ofputil_bundle_ctrl_msg *bc, | |
506c1ddb | 1104 | uint16_t type, struct ovs_list *errors) |
db5076ee JR |
1105 | { |
1106 | struct ofpbuf *request, *reply; | |
1107 | int error; | |
1108 | enum ofperr ofperr; | |
1109 | ||
1110 | bc->type = type; | |
1111 | request = ofputil_encode_bundle_ctrl_request(vconn->version, bc); | |
1112 | ofpmsg_update_length(request); | |
506c1ddb | 1113 | error = vconn_transact__(vconn, request, &reply, errors); |
db5076ee JR |
1114 | if (error) { |
1115 | return error; | |
1116 | } | |
1117 | ||
506c1ddb | 1118 | ofperr = vconn_bundle_reply_validate(reply, bc, errors); |
db5076ee JR |
1119 | ofpbuf_delete(reply); |
1120 | ||
1121 | return ofperr ? EPROTO : 0; | |
1122 | } | |
1123 | ||
1124 | /* Checks if error responses can be received on 'vconn'. */ | |
1125 | static void | |
506c1ddb | 1126 | vconn_recv_error(struct vconn *vconn, struct ovs_list *errors) |
db5076ee JR |
1127 | { |
1128 | int error; | |
1129 | ||
1130 | do { | |
1131 | struct ofpbuf *reply; | |
1132 | ||
1133 | error = vconn_recv(vconn, &reply); | |
1134 | if (!error) { | |
1135 | const struct ofp_header *oh; | |
1136 | enum ofptype type; | |
1137 | enum ofperr ofperr; | |
1138 | ||
1139 | oh = reply->data; | |
1140 | ofperr = ofptype_decode(&type, oh); | |
1141 | if (!ofperr && type == OFPTYPE_ERROR) { | |
209aa4ad | 1142 | ovs_list_push_back(errors, &reply->list_node); |
db5076ee JR |
1143 | } else { |
1144 | VLOG_DBG_RL(&bad_ofmsg_rl, | |
1145 | "%s: received unexpected reply with xid %08"PRIx32, | |
1146 | vconn->name, ntohl(oh->xid)); | |
209aa4ad | 1147 | ofpbuf_delete(reply); |
db5076ee | 1148 | } |
db5076ee JR |
1149 | } |
1150 | } while (!error); | |
1151 | } | |
1152 | ||
506c1ddb JR |
1153 | /* Sends a barrier and waits for the barrier response and stores any errors |
1154 | * that are received before the barrier response. */ | |
1155 | static int | |
1156 | vconn_bundle_barrier_transact(struct vconn *vconn, struct ovs_list *errors) | |
1157 | { | |
1158 | struct ofpbuf *reply; | |
1159 | ovs_be32 barrier_xid; | |
1160 | int error; | |
1161 | ||
1162 | error = vconn_send_barrier(vconn, &barrier_xid); | |
1163 | if (error) { | |
1164 | return error; | |
1165 | } | |
1166 | ||
1167 | error = vconn_recv_xid__(vconn, barrier_xid, &reply, errors); | |
1168 | if (error) { | |
1169 | return error; | |
1170 | } | |
1171 | ofpbuf_delete(reply); | |
1172 | return 0; | |
1173 | } | |
1174 | ||
db5076ee JR |
1175 | static int |
1176 | vconn_bundle_add_msg(struct vconn *vconn, struct ofputil_bundle_ctrl_msg *bc, | |
1177 | struct ofpbuf *msg, | |
506c1ddb | 1178 | struct ovs_list *errors) |
db5076ee JR |
1179 | { |
1180 | struct ofputil_bundle_add_msg bam; | |
1181 | struct ofpbuf *request; | |
1182 | int error; | |
1183 | ||
bcf899f7 JR |
1184 | ofpmsg_update_length(msg); |
1185 | ||
db5076ee JR |
1186 | bam.bundle_id = bc->bundle_id; |
1187 | bam.flags = bc->flags; | |
1188 | bam.msg = msg->data; | |
1189 | ||
1190 | request = ofputil_encode_bundle_add(vconn->version, &bam); | |
db5076ee JR |
1191 | |
1192 | error = vconn_send_block(vconn, request); | |
1193 | if (!error) { | |
1194 | /* Check for an error return, so that the socket buffer does not become | |
1195 | * full of errors. */ | |
506c1ddb | 1196 | vconn_recv_error(vconn, errors); |
db5076ee JR |
1197 | } |
1198 | return error; | |
1199 | } | |
1200 | ||
209aa4ad BP |
1201 | /* Appends ofpbufs for received errors, if any, to 'errors'. The caller must |
1202 | * free the received errors. */ | |
db5076ee JR |
1203 | int |
1204 | vconn_bundle_transact(struct vconn *vconn, struct ovs_list *requests, | |
506c1ddb | 1205 | uint16_t flags, struct ovs_list *errors) |
db5076ee JR |
1206 | { |
1207 | struct ofputil_bundle_ctrl_msg bc; | |
1208 | struct ofpbuf *request; | |
1209 | int error; | |
1210 | ||
506c1ddb JR |
1211 | ovs_list_init(errors); |
1212 | ||
db5076ee JR |
1213 | memset(&bc, 0, sizeof bc); |
1214 | bc.flags = flags; | |
1215 | error = vconn_bundle_control_transact(vconn, &bc, OFPBCT_OPEN_REQUEST, | |
506c1ddb | 1216 | errors); |
db5076ee JR |
1217 | if (error) { |
1218 | return error; | |
1219 | } | |
1220 | ||
1221 | LIST_FOR_EACH (request, list_node, requests) { | |
506c1ddb | 1222 | error = vconn_bundle_add_msg(vconn, &bc, request, errors); |
db5076ee JR |
1223 | if (error) { |
1224 | break; | |
1225 | } | |
1226 | } | |
1227 | ||
506c1ddb JR |
1228 | if (!error) { |
1229 | /* A failing message does not invalidate the bundle, but the message is | |
1230 | * simply not added to the bundle. Since we do not want to commit if | |
1231 | * any of the messages failed, we need to explicitly sync with barrier | |
1232 | * before we issue the commit message. */ | |
1233 | error = vconn_bundle_barrier_transact(vconn, errors); | |
1234 | } | |
1235 | if (!error && !ovs_list_is_empty(errors)) { | |
1236 | error = EPROTO; | |
1237 | } | |
1238 | ||
1239 | /* Commit only if no errors are received. */ | |
db5076ee JR |
1240 | if (!error) { |
1241 | error = vconn_bundle_control_transact(vconn, &bc, | |
1242 | OFPBCT_COMMIT_REQUEST, | |
506c1ddb | 1243 | errors); |
db5076ee | 1244 | } else { |
db5076ee | 1245 | vconn_bundle_control_transact(vconn, &bc, OFPBCT_DISCARD_REQUEST, |
506c1ddb | 1246 | errors); |
db5076ee JR |
1247 | } |
1248 | return error; | |
1249 | } | |
1250 | ||
064af421 BP |
1251 | void |
1252 | vconn_wait(struct vconn *vconn, enum vconn_wait_type wait) | |
1253 | { | |
cb22974d | 1254 | ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND); |
064af421 BP |
1255 | |
1256 | switch (vconn->state) { | |
1257 | case VCS_CONNECTING: | |
1258 | wait = WAIT_CONNECT; | |
1259 | break; | |
1260 | ||
1261 | case VCS_SEND_HELLO: | |
1262 | case VCS_SEND_ERROR: | |
1263 | wait = WAIT_SEND; | |
1264 | break; | |
1265 | ||
1266 | case VCS_RECV_HELLO: | |
1267 | wait = WAIT_RECV; | |
1268 | break; | |
1269 | ||
1270 | case VCS_CONNECTED: | |
1271 | break; | |
1272 | ||
1273 | case VCS_DISCONNECTED: | |
1274 | poll_immediate_wake(); | |
1275 | return; | |
1276 | } | |
a445a8d8 | 1277 | (vconn->vclass->wait)(vconn, wait); |
064af421 BP |
1278 | } |
1279 | ||
1280 | void | |
1281 | vconn_connect_wait(struct vconn *vconn) | |
1282 | { | |
1283 | vconn_wait(vconn, WAIT_CONNECT); | |
1284 | } | |
1285 | ||
1286 | void | |
1287 | vconn_recv_wait(struct vconn *vconn) | |
1288 | { | |
1289 | vconn_wait(vconn, WAIT_RECV); | |
1290 | } | |
1291 | ||
1292 | void | |
1293 | vconn_send_wait(struct vconn *vconn) | |
1294 | { | |
1295 | vconn_wait(vconn, WAIT_SEND); | |
1296 | } | |
1297 | ||
30012c72 BP |
1298 | /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class |
1299 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
1300 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
1301 | * class exists. */ | |
1302 | static int | |
5b712636 | 1303 | pvconn_lookup_class(const char *name, const struct pvconn_class **classp) |
30012c72 BP |
1304 | { |
1305 | size_t prefix_len; | |
1306 | ||
1307 | prefix_len = strcspn(name, ":"); | |
1308 | if (name[prefix_len] != '\0') { | |
1309 | size_t i; | |
1310 | ||
1311 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
5b712636 | 1312 | const struct pvconn_class *class = pvconn_classes[i]; |
30012c72 BP |
1313 | if (strlen(class->name) == prefix_len |
1314 | && !memcmp(class->name, name, prefix_len)) { | |
1315 | *classp = class; | |
1316 | return 0; | |
1317 | } | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | *classp = NULL; | |
1322 | return EAFNOSUPPORT; | |
1323 | } | |
1324 | ||
1325 | /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is | |
1326 | * a supported connection type, otherwise EAFNOSUPPORT. */ | |
1327 | int | |
1328 | pvconn_verify_name(const char *name) | |
1329 | { | |
5b712636 | 1330 | const struct pvconn_class *class; |
30012c72 BP |
1331 | return pvconn_lookup_class(name, &class); |
1332 | } | |
1333 | ||
064af421 BP |
1334 | /* Attempts to start listening for OpenFlow connections. 'name' is a |
1335 | * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn | |
1336 | * class's name and ARGS are vconn class-specific. | |
1337 | * | |
7a25bd99 SH |
1338 | * vconns accepted by the pvconn will automatically negotiate an OpenFlow |
1339 | * protocol version acceptable to both peers on the connection. The version | |
1340 | * negotiated will be one of those in the 'allowed_versions' bitmap: version | |
1341 | * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If | |
1342 | * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed. | |
1343 | * | |
064af421 BP |
1344 | * Returns 0 if successful, otherwise a positive errno value. If successful, |
1345 | * stores a pointer to the new connection in '*pvconnp', otherwise a null | |
1346 | * pointer. */ | |
1347 | int | |
82c8c53c BP |
1348 | pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, |
1349 | struct pvconn **pvconnp) | |
064af421 | 1350 | { |
5b712636 | 1351 | const struct pvconn_class *class; |
30012c72 BP |
1352 | struct pvconn *pvconn; |
1353 | char *suffix_copy; | |
1354 | int error; | |
064af421 BP |
1355 | |
1356 | check_vconn_classes(); | |
1357 | ||
7a25bd99 SH |
1358 | if (!allowed_versions) { |
1359 | allowed_versions = OFPUTIL_DEFAULT_VERSIONS; | |
1360 | } | |
1361 | ||
30012c72 BP |
1362 | /* Look up the class. */ |
1363 | error = pvconn_lookup_class(name, &class); | |
1364 | if (!class) { | |
1365 | goto error; | |
064af421 | 1366 | } |
30012c72 BP |
1367 | |
1368 | /* Call class's "open" function. */ | |
1369 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
7a25bd99 | 1370 | error = class->listen(name, allowed_versions, suffix_copy, &pvconn, dscp); |
30012c72 BP |
1371 | free(suffix_copy); |
1372 | if (error) { | |
1373 | goto error; | |
064af421 | 1374 | } |
30012c72 BP |
1375 | |
1376 | /* Success. */ | |
1377 | *pvconnp = pvconn; | |
1378 | return 0; | |
1379 | ||
1380 | error: | |
1381 | *pvconnp = NULL; | |
1382 | return error; | |
064af421 BP |
1383 | } |
1384 | ||
1385 | /* Returns the name that was used to open 'pvconn'. The caller must not | |
1386 | * modify or free the name. */ | |
1387 | const char * | |
1388 | pvconn_get_name(const struct pvconn *pvconn) | |
1389 | { | |
1390 | return pvconn->name; | |
1391 | } | |
1392 | ||
1393 | /* Closes 'pvconn'. */ | |
1394 | void | |
1395 | pvconn_close(struct pvconn *pvconn) | |
1396 | { | |
1397 | if (pvconn != NULL) { | |
1398 | char *name = pvconn->name; | |
a445a8d8 | 1399 | (pvconn->pvclass->close)(pvconn); |
064af421 BP |
1400 | free(name); |
1401 | } | |
1402 | } | |
1403 | ||
1404 | /* Tries to accept a new connection on 'pvconn'. If successful, stores the new | |
1405 | * connection in '*new_vconn' and returns 0. Otherwise, returns a positive | |
1406 | * errno value. | |
1407 | * | |
1408 | * The new vconn will automatically negotiate an OpenFlow protocol version | |
1409 | * acceptable to both peers on the connection. The version negotiated will be | |
7a25bd99 | 1410 | * no lower than 'min_version' and no higher than 'max_version'. |
064af421 BP |
1411 | * |
1412 | * pvconn_accept() will not block waiting for a connection. If no connection | |
1413 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
1414 | int | |
7a25bd99 | 1415 | pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn) |
064af421 | 1416 | { |
a445a8d8 | 1417 | int retval = (pvconn->pvclass->accept)(pvconn, new_vconn); |
064af421 BP |
1418 | if (retval) { |
1419 | *new_vconn = NULL; | |
1420 | } else { | |
cb22974d | 1421 | ovs_assert((*new_vconn)->state != VCS_CONNECTING |
a445a8d8 | 1422 | || (*new_vconn)->vclass->connect); |
064af421 BP |
1423 | } |
1424 | return retval; | |
1425 | } | |
1426 | ||
1427 | void | |
1428 | pvconn_wait(struct pvconn *pvconn) | |
1429 | { | |
a445a8d8 | 1430 | (pvconn->pvclass->wait)(pvconn); |
064af421 BP |
1431 | } |
1432 | ||
85ab0a02 BP |
1433 | /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'. |
1434 | * The initial connection status, supplied as 'connect_status', is interpreted | |
1435 | * as follows: | |
1436 | * | |
1437 | * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be | |
1438 | * called in the normal fashion. | |
1439 | * | |
1440 | * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect' | |
1441 | * function should be called to complete the connection. | |
1442 | * | |
1443 | * - Other positive errno values indicate that the connection failed with | |
1444 | * the specified error. | |
1445 | * | |
1446 | * After calling this function, vconn_close() must be used to destroy 'vconn', | |
1447 | * otherwise resources will be leaked. | |
1448 | * | |
1449 | * The caller retains ownership of 'name'. */ | |
064af421 | 1450 | void |
5b712636 BP |
1451 | vconn_init(struct vconn *vconn, const struct vconn_class *class, |
1452 | int connect_status, const char *name, uint32_t allowed_versions) | |
064af421 | 1453 | { |
267e915f | 1454 | memset(vconn, 0, sizeof *vconn); |
a445a8d8 | 1455 | vconn->vclass = class; |
064af421 BP |
1456 | vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING |
1457 | : !connect_status ? VCS_SEND_HELLO | |
1458 | : VCS_DISCONNECTED); | |
1459 | vconn->error = connect_status; | |
7a25bd99 | 1460 | vconn->allowed_versions = allowed_versions; |
064af421 | 1461 | vconn->name = xstrdup(name); |
cb22974d | 1462 | ovs_assert(vconn->state != VCS_CONNECTING || class->connect); |
064af421 BP |
1463 | } |
1464 | ||
1465 | void | |
5b712636 | 1466 | pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class, |
7a25bd99 | 1467 | const char *name, uint32_t allowed_versions) |
064af421 | 1468 | { |
a445a8d8 | 1469 | pvconn->pvclass = class; |
064af421 | 1470 | pvconn->name = xstrdup(name); |
7a25bd99 | 1471 | pvconn->allowed_versions = allowed_versions; |
064af421 | 1472 | } |