]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
90bf1e07 | 2 | * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. |
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" | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
22 | #include <netinet/in.h> | |
23 | #include <poll.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include "coverage.h" | |
27 | #include "dynamic-string.h" | |
b302749b | 28 | #include "fatal-signal.h" |
064af421 | 29 | #include "flow.h" |
90bf1e07 | 30 | #include "ofp-errors.h" |
064af421 | 31 | #include "ofp-print.h" |
fa37b408 | 32 | #include "ofp-util.h" |
064af421 BP |
33 | #include "ofpbuf.h" |
34 | #include "openflow/nicira-ext.h" | |
35 | #include "openflow/openflow.h" | |
36 | #include "packets.h" | |
37 | #include "poll-loop.h" | |
38 | #include "random.h" | |
39 | #include "util.h" | |
064af421 BP |
40 | #include "vlog.h" |
41 | ||
d98e6007 | 42 | VLOG_DEFINE_THIS_MODULE(vconn); |
5136ce49 | 43 | |
d76f09ea BP |
44 | COVERAGE_DEFINE(vconn_open); |
45 | COVERAGE_DEFINE(vconn_received); | |
46 | COVERAGE_DEFINE(vconn_sent); | |
47 | ||
064af421 BP |
48 | /* State of an active vconn.*/ |
49 | enum vconn_state { | |
50 | /* This is the ordinary progression of states. */ | |
51 | VCS_CONNECTING, /* Underlying vconn is not connected. */ | |
52 | VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */ | |
53 | VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */ | |
54 | VCS_CONNECTED, /* Connection established. */ | |
55 | ||
56 | /* These states are entered only when something goes wrong. */ | |
57 | VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */ | |
58 | VCS_DISCONNECTED /* Connection failed or connection closed. */ | |
59 | }; | |
60 | ||
61 | static struct vconn_class *vconn_classes[] = { | |
62 | &tcp_vconn_class, | |
63 | &unix_vconn_class, | |
64 | #ifdef HAVE_OPENSSL | |
65 | &ssl_vconn_class, | |
66 | #endif | |
67 | }; | |
68 | ||
69 | static struct pvconn_class *pvconn_classes[] = { | |
70 | &ptcp_pvconn_class, | |
71 | &punix_pvconn_class, | |
72 | #ifdef HAVE_OPENSSL | |
73 | &pssl_pvconn_class, | |
74 | #endif | |
75 | }; | |
76 | ||
77 | /* Rate limit for individual OpenFlow messages going over the vconn, output at | |
78 | * DBG level. This is very high because, if these are enabled, it is because | |
79 | * we really need to see them. */ | |
80 | static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600); | |
81 | ||
82 | /* Rate limit for OpenFlow message parse errors. These always indicate a bug | |
83 | * in the peer and so there's not much point in showing a lot of them. */ | |
84 | static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
85 | ||
86 | static int do_recv(struct vconn *, struct ofpbuf **); | |
87 | static int do_send(struct vconn *, struct ofpbuf *); | |
88 | ||
89 | /* Check the validity of the vconn class structures. */ | |
90 | static void | |
91 | check_vconn_classes(void) | |
92 | { | |
93 | #ifndef NDEBUG | |
94 | size_t i; | |
95 | ||
96 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
97 | struct vconn_class *class = vconn_classes[i]; | |
98 | assert(class->name != NULL); | |
99 | assert(class->open != NULL); | |
60cb3eb8 BP |
100 | if (class->close || class->recv || class->send |
101 | || class->run || class->run_wait || class->wait) { | |
064af421 BP |
102 | assert(class->close != NULL); |
103 | assert(class->recv != NULL); | |
104 | assert(class->send != NULL); | |
105 | assert(class->wait != NULL); | |
106 | } else { | |
107 | /* This class delegates to another one. */ | |
108 | } | |
109 | } | |
110 | ||
111 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
112 | struct pvconn_class *class = pvconn_classes[i]; | |
113 | assert(class->name != NULL); | |
114 | assert(class->listen != NULL); | |
115 | if (class->close || class->accept || class->wait) { | |
116 | assert(class->close != NULL); | |
117 | assert(class->accept != NULL); | |
118 | assert(class->wait != NULL); | |
119 | } else { | |
120 | /* This class delegates to another one. */ | |
121 | } | |
122 | } | |
123 | #endif | |
124 | } | |
125 | ||
126 | /* Prints information on active (if 'active') and passive (if 'passive') | |
127 | * connection methods supported by the vconn. If 'bootstrap' is true, also | |
128 | * advertises options to bootstrap the CA certificate. */ | |
129 | void | |
67a4917b | 130 | vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED) |
064af421 BP |
131 | { |
132 | /* Really this should be implemented via callbacks into the vconn | |
133 | * providers, but that seems too heavy-weight to bother with at the | |
134 | * moment. */ | |
d295e8e9 | 135 | |
064af421 BP |
136 | printf("\n"); |
137 | if (active) { | |
138 | printf("Active OpenFlow connection methods:\n"); | |
2b35e147 BP |
139 | printf(" tcp:IP[:PORT] " |
140 | "PORT (default: %d) at remote IP\n", OFP_TCP_PORT); | |
064af421 | 141 | #ifdef HAVE_OPENSSL |
2b35e147 BP |
142 | printf(" ssl:IP[:PORT] " |
143 | "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT); | |
064af421 BP |
144 | #endif |
145 | printf(" unix:FILE Unix domain socket named FILE\n"); | |
146 | } | |
147 | ||
148 | if (passive) { | |
149 | printf("Passive OpenFlow connection methods:\n"); | |
78ff0270 BP |
150 | printf(" ptcp:[PORT][:IP] " |
151 | "listen to TCP PORT (default: %d) on IP\n", | |
064af421 BP |
152 | OFP_TCP_PORT); |
153 | #ifdef HAVE_OPENSSL | |
78ff0270 BP |
154 | printf(" pssl:[PORT][:IP] " |
155 | "listen for SSL on PORT (default: %d) on IP\n", | |
064af421 BP |
156 | OFP_SSL_PORT); |
157 | #endif | |
158 | printf(" punix:FILE " | |
159 | "listen on Unix domain socket FILE\n"); | |
160 | } | |
161 | ||
162 | #ifdef HAVE_OPENSSL | |
163 | printf("PKI configuration (required to use SSL):\n" | |
164 | " -p, --private-key=FILE file with private key\n" | |
165 | " -c, --certificate=FILE file with certificate for private key\n" | |
166 | " -C, --ca-cert=FILE file with peer CA certificate\n"); | |
167 | if (bootstrap) { | |
168 | printf(" --bootstrap-ca-cert=FILE file with peer CA certificate " | |
169 | "to read or create\n"); | |
170 | } | |
171 | #endif | |
172 | } | |
173 | ||
30012c72 BP |
174 | /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class |
175 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
176 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
177 | * class exists. */ | |
178 | static int | |
179 | vconn_lookup_class(const char *name, struct vconn_class **classp) | |
180 | { | |
181 | size_t prefix_len; | |
182 | ||
183 | prefix_len = strcspn(name, ":"); | |
184 | if (name[prefix_len] != '\0') { | |
185 | size_t i; | |
186 | ||
187 | for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { | |
188 | struct vconn_class *class = vconn_classes[i]; | |
189 | if (strlen(class->name) == prefix_len | |
190 | && !memcmp(class->name, name, prefix_len)) { | |
191 | *classp = class; | |
192 | return 0; | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | *classp = NULL; | |
198 | return EAFNOSUPPORT; | |
199 | } | |
200 | ||
201 | /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is | |
202 | * a supported connection type, otherwise EAFNOSUPPORT. */ | |
203 | int | |
204 | vconn_verify_name(const char *name) | |
205 | { | |
206 | struct vconn_class *class; | |
207 | return vconn_lookup_class(name, &class); | |
208 | } | |
209 | ||
064af421 BP |
210 | /* Attempts to connect to an OpenFlow device. 'name' is a connection name in |
211 | * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS | |
212 | * are vconn class-specific. | |
213 | * | |
214 | * The vconn will automatically negotiate an OpenFlow protocol version | |
215 | * acceptable to both peers on the connection. The version negotiated will be | |
216 | * no lower than 'min_version' and no higher than OFP_VERSION. | |
217 | * | |
218 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
219 | * stores a pointer to the new connection in '*vconnp', otherwise a null | |
220 | * pointer. */ | |
221 | int | |
222 | vconn_open(const char *name, int min_version, struct vconn **vconnp) | |
223 | { | |
30012c72 BP |
224 | struct vconn_class *class; |
225 | struct vconn *vconn; | |
226 | char *suffix_copy; | |
227 | int error; | |
064af421 BP |
228 | |
229 | COVERAGE_INC(vconn_open); | |
230 | check_vconn_classes(); | |
231 | ||
30012c72 BP |
232 | /* Look up the class. */ |
233 | error = vconn_lookup_class(name, &class); | |
234 | if (!class) { | |
235 | goto error; | |
064af421 | 236 | } |
30012c72 BP |
237 | |
238 | /* Call class's "open" function. */ | |
239 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
240 | error = class->open(name, suffix_copy, &vconn); | |
241 | free(suffix_copy); | |
242 | if (error) { | |
243 | goto error; | |
064af421 | 244 | } |
30012c72 BP |
245 | |
246 | /* Success. */ | |
247 | assert(vconn->state != VCS_CONNECTING || vconn->class->connect); | |
248 | vconn->min_version = min_version; | |
249 | *vconnp = vconn; | |
250 | return 0; | |
251 | ||
252 | error: | |
253 | *vconnp = NULL; | |
254 | return error; | |
064af421 BP |
255 | } |
256 | ||
60cb3eb8 BP |
257 | /* Allows 'vconn' to perform maintenance activities, such as flushing output |
258 | * buffers. */ | |
259 | void | |
260 | vconn_run(struct vconn *vconn) | |
261 | { | |
262 | if (vconn->class->run) { | |
263 | (vconn->class->run)(vconn); | |
264 | } | |
265 | } | |
266 | ||
267 | /* Arranges for the poll loop to wake up when 'vconn' needs to perform | |
268 | * maintenance activities. */ | |
269 | void | |
270 | vconn_run_wait(struct vconn *vconn) | |
271 | { | |
272 | if (vconn->class->run_wait) { | |
273 | (vconn->class->run_wait)(vconn); | |
274 | } | |
275 | } | |
276 | ||
064af421 BP |
277 | int |
278 | vconn_open_block(const char *name, int min_version, struct vconn **vconnp) | |
279 | { | |
280 | struct vconn *vconn; | |
281 | int error; | |
282 | ||
b302749b BP |
283 | fatal_signal_run(); |
284 | ||
064af421 | 285 | error = vconn_open(name, min_version, &vconn); |
b0bfeb3e | 286 | if (!error) { |
aa31e605 | 287 | while ((error = vconn_connect(vconn)) == EAGAIN) { |
b0bfeb3e BP |
288 | vconn_run(vconn); |
289 | vconn_run_wait(vconn); | |
290 | vconn_connect_wait(vconn); | |
291 | poll_block(); | |
292 | } | |
064af421 BP |
293 | assert(error != EINPROGRESS); |
294 | } | |
b0bfeb3e | 295 | |
064af421 BP |
296 | if (error) { |
297 | vconn_close(vconn); | |
298 | *vconnp = NULL; | |
299 | } else { | |
300 | *vconnp = vconn; | |
301 | } | |
302 | return error; | |
303 | } | |
304 | ||
305 | /* Closes 'vconn'. */ | |
306 | void | |
307 | vconn_close(struct vconn *vconn) | |
308 | { | |
309 | if (vconn != NULL) { | |
310 | char *name = vconn->name; | |
311 | (vconn->class->close)(vconn); | |
312 | free(name); | |
313 | } | |
314 | } | |
315 | ||
316 | /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */ | |
317 | const char * | |
318 | vconn_get_name(const struct vconn *vconn) | |
319 | { | |
320 | return vconn->name; | |
321 | } | |
322 | ||
323 | /* Returns the IP address of the peer, or 0 if the peer is not connected over | |
324 | * an IP-based protocol or if its IP address is not yet known. */ | |
4408d18a | 325 | ovs_be32 |
d295e8e9 | 326 | vconn_get_remote_ip(const struct vconn *vconn) |
064af421 | 327 | { |
193456d5 JP |
328 | return vconn->remote_ip; |
329 | } | |
330 | ||
d295e8e9 | 331 | /* Returns the transport port of the peer, or 0 if the connection does not |
193456d5 | 332 | * contain a port or if the port is not yet known. */ |
4408d18a | 333 | ovs_be16 |
d295e8e9 | 334 | vconn_get_remote_port(const struct vconn *vconn) |
193456d5 JP |
335 | { |
336 | return vconn->remote_port; | |
337 | } | |
338 | ||
d295e8e9 JP |
339 | /* Returns the IP address used to connect to the peer, or 0 if the |
340 | * connection is not an IP-based protocol or if its IP address is not | |
193456d5 | 341 | * yet known. */ |
4408d18a | 342 | ovs_be32 |
d295e8e9 | 343 | vconn_get_local_ip(const struct vconn *vconn) |
193456d5 JP |
344 | { |
345 | return vconn->local_ip; | |
346 | } | |
347 | ||
d295e8e9 | 348 | /* Returns the transport port used to connect to the peer, or 0 if the |
193456d5 | 349 | * connection does not contain a port or if the port is not yet known. */ |
4408d18a | 350 | ovs_be16 |
d295e8e9 | 351 | vconn_get_local_port(const struct vconn *vconn) |
193456d5 JP |
352 | { |
353 | return vconn->local_port; | |
064af421 BP |
354 | } |
355 | ||
356 | static void | |
d295e8e9 | 357 | vcs_connecting(struct vconn *vconn) |
064af421 BP |
358 | { |
359 | int retval = (vconn->class->connect)(vconn); | |
360 | assert(retval != EINPROGRESS); | |
361 | if (!retval) { | |
362 | vconn->state = VCS_SEND_HELLO; | |
363 | } else if (retval != EAGAIN) { | |
364 | vconn->state = VCS_DISCONNECTED; | |
365 | vconn->error = retval; | |
366 | } | |
367 | } | |
368 | ||
369 | static void | |
370 | vcs_send_hello(struct vconn *vconn) | |
371 | { | |
372 | struct ofpbuf *b; | |
373 | int retval; | |
374 | ||
375 | make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b); | |
376 | retval = do_send(vconn, b); | |
377 | if (!retval) { | |
378 | vconn->state = VCS_RECV_HELLO; | |
379 | } else { | |
380 | ofpbuf_delete(b); | |
381 | if (retval != EAGAIN) { | |
382 | vconn->state = VCS_DISCONNECTED; | |
383 | vconn->error = retval; | |
384 | } | |
385 | } | |
386 | } | |
387 | ||
388 | static void | |
389 | vcs_recv_hello(struct vconn *vconn) | |
390 | { | |
391 | struct ofpbuf *b; | |
392 | int retval; | |
393 | ||
394 | retval = do_recv(vconn, &b); | |
395 | if (!retval) { | |
396 | struct ofp_header *oh = b->data; | |
397 | ||
398 | if (oh->type == OFPT_HELLO) { | |
399 | if (b->size > sizeof *oh) { | |
400 | struct ds msg = DS_EMPTY_INITIALIZER; | |
401 | ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name); | |
402 | ds_put_hex_dump(&msg, b->data, b->size, 0, true); | |
403 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg)); | |
404 | ds_destroy(&msg); | |
405 | } | |
406 | ||
407 | vconn->version = MIN(OFP_VERSION, oh->version); | |
408 | if (vconn->version < vconn->min_version) { | |
409 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
410 | "%s: version negotiation failed: we support " | |
411 | "versions 0x%02x to 0x%02x inclusive but peer " | |
412 | "supports no later than version 0x%02"PRIx8, | |
413 | vconn->name, vconn->min_version, OFP_VERSION, | |
414 | oh->version); | |
415 | vconn->state = VCS_SEND_ERROR; | |
416 | } else { | |
417 | VLOG_DBG("%s: negotiated OpenFlow version 0x%02x " | |
418 | "(we support versions 0x%02x to 0x%02x inclusive, " | |
419 | "peer no later than version 0x%02"PRIx8")", | |
420 | vconn->name, vconn->version, vconn->min_version, | |
421 | OFP_VERSION, oh->version); | |
422 | vconn->state = VCS_CONNECTED; | |
423 | } | |
424 | ofpbuf_delete(b); | |
425 | return; | |
426 | } else { | |
427 | char *s = ofp_to_string(b->data, b->size, 1); | |
428 | VLOG_WARN_RL(&bad_ofmsg_rl, | |
429 | "%s: received message while expecting hello: %s", | |
430 | vconn->name, s); | |
431 | free(s); | |
432 | retval = EPROTO; | |
433 | ofpbuf_delete(b); | |
434 | } | |
435 | } | |
436 | ||
437 | if (retval != EAGAIN) { | |
438 | vconn->state = VCS_DISCONNECTED; | |
b7eae257 | 439 | vconn->error = retval == EOF ? ECONNRESET : retval; |
064af421 BP |
440 | } |
441 | } | |
442 | ||
443 | static void | |
444 | vcs_send_error(struct vconn *vconn) | |
445 | { | |
064af421 BP |
446 | struct ofpbuf *b; |
447 | char s[128]; | |
448 | int retval; | |
449 | ||
450 | snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but " | |
451 | "you support no later than version 0x%02"PRIx8".", | |
452 | vconn->min_version, OFP_VERSION, vconn->version); | |
90bf1e07 BP |
453 | b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE, |
454 | ofperr_domain_from_version(vconn->version), s); | |
064af421 BP |
455 | retval = do_send(vconn, b); |
456 | if (retval) { | |
457 | ofpbuf_delete(b); | |
458 | } | |
459 | if (retval != EAGAIN) { | |
460 | vconn->state = VCS_DISCONNECTED; | |
461 | vconn->error = retval ? retval : EPROTO; | |
462 | } | |
463 | } | |
464 | ||
294e9fc8 BP |
465 | /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is |
466 | * complete, returns 0 if the connection was successful or a positive errno | |
467 | * value if it failed. If the connection is still in progress, returns | |
468 | * EAGAIN. */ | |
064af421 BP |
469 | int |
470 | vconn_connect(struct vconn *vconn) | |
471 | { | |
472 | enum vconn_state last_state; | |
473 | ||
474 | assert(vconn->min_version >= 0); | |
475 | do { | |
476 | last_state = vconn->state; | |
477 | switch (vconn->state) { | |
478 | case VCS_CONNECTING: | |
479 | vcs_connecting(vconn); | |
480 | break; | |
481 | ||
482 | case VCS_SEND_HELLO: | |
483 | vcs_send_hello(vconn); | |
484 | break; | |
485 | ||
486 | case VCS_RECV_HELLO: | |
487 | vcs_recv_hello(vconn); | |
488 | break; | |
489 | ||
490 | case VCS_CONNECTED: | |
491 | return 0; | |
492 | ||
493 | case VCS_SEND_ERROR: | |
494 | vcs_send_error(vconn); | |
495 | break; | |
496 | ||
497 | case VCS_DISCONNECTED: | |
498 | return vconn->error; | |
499 | ||
500 | default: | |
501 | NOT_REACHED(); | |
502 | } | |
503 | } while (vconn->state != last_state); | |
504 | ||
505 | return EAGAIN; | |
506 | } | |
507 | ||
294e9fc8 BP |
508 | /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores |
509 | * the received message into '*msgp' and returns 0. The caller is responsible | |
510 | * for destroying the message with ofpbuf_delete(). On failure, returns a | |
511 | * positive errno value and stores a null pointer into '*msgp'. On normal | |
512 | * connection close, returns EOF. | |
064af421 BP |
513 | * |
514 | * vconn_recv will not block waiting for a packet to arrive. If no packets | |
515 | * have been received, it returns EAGAIN immediately. */ | |
516 | int | |
517 | vconn_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
518 | { | |
519 | int retval = vconn_connect(vconn); | |
520 | if (!retval) { | |
521 | retval = do_recv(vconn, msgp); | |
522 | } | |
523 | return retval; | |
524 | } | |
525 | ||
526 | static int | |
527 | do_recv(struct vconn *vconn, struct ofpbuf **msgp) | |
528 | { | |
5fe577eb | 529 | int retval = (vconn->class->recv)(vconn, msgp); |
064af421 BP |
530 | if (!retval) { |
531 | struct ofp_header *oh; | |
532 | ||
533 | COVERAGE_INC(vconn_received); | |
534 | if (VLOG_IS_DBG_ENABLED()) { | |
535 | char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1); | |
536 | VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s); | |
537 | free(s); | |
538 | } | |
539 | ||
540 | oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh); | |
541 | if (oh->version != vconn->version | |
542 | && oh->type != OFPT_HELLO | |
543 | && oh->type != OFPT_ERROR | |
544 | && oh->type != OFPT_ECHO_REQUEST | |
545 | && oh->type != OFPT_ECHO_REPLY | |
546 | && oh->type != OFPT_VENDOR) | |
547 | { | |
548 | if (vconn->version < 0) { | |
064af421 BP |
549 | VLOG_ERR_RL(&bad_ofmsg_rl, |
550 | "%s: received OpenFlow message type %"PRIu8" " | |
551 | "before version negotiation complete", | |
552 | vconn->name, oh->type); | |
553 | } else { | |
554 | VLOG_ERR_RL(&bad_ofmsg_rl, | |
555 | "%s: received OpenFlow version 0x%02"PRIx8" " | |
556 | "!= expected %02x", | |
557 | vconn->name, oh->version, vconn->version); | |
558 | } | |
559 | ofpbuf_delete(*msgp); | |
560 | retval = EPROTO; | |
561 | } | |
562 | } | |
563 | if (retval) { | |
564 | *msgp = NULL; | |
565 | } | |
566 | return retval; | |
567 | } | |
568 | ||
294e9fc8 BP |
569 | /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0, |
570 | * in which case ownership of 'msg' is transferred to the vconn. Success does | |
571 | * not guarantee that 'msg' has been or ever will be delivered to the peer, | |
572 | * only that it has been queued for transmission. | |
064af421 BP |
573 | * |
574 | * Returns a positive errno value on failure, in which case the caller | |
575 | * retains ownership of 'msg'. | |
576 | * | |
577 | * vconn_send will not block. If 'msg' cannot be immediately accepted for | |
578 | * transmission, it returns EAGAIN immediately. */ | |
579 | int | |
580 | vconn_send(struct vconn *vconn, struct ofpbuf *msg) | |
581 | { | |
582 | int retval = vconn_connect(vconn); | |
583 | if (!retval) { | |
584 | retval = do_send(vconn, msg); | |
585 | } | |
586 | return retval; | |
587 | } | |
588 | ||
589 | static int | |
590 | do_send(struct vconn *vconn, struct ofpbuf *msg) | |
591 | { | |
592 | int retval; | |
593 | ||
594 | assert(msg->size >= sizeof(struct ofp_header)); | |
595 | assert(((struct ofp_header *) msg->data)->length == htons(msg->size)); | |
596 | if (!VLOG_IS_DBG_ENABLED()) { | |
597 | COVERAGE_INC(vconn_sent); | |
598 | retval = (vconn->class->send)(vconn, msg); | |
599 | } else { | |
600 | char *s = ofp_to_string(msg->data, msg->size, 1); | |
601 | retval = (vconn->class->send)(vconn, msg); | |
602 | if (retval != EAGAIN) { | |
603 | VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s", | |
604 | vconn->name, strerror(retval), s); | |
605 | } | |
606 | free(s); | |
607 | } | |
608 | return retval; | |
609 | } | |
610 | ||
611 | /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */ | |
612 | int | |
613 | vconn_send_block(struct vconn *vconn, struct ofpbuf *msg) | |
614 | { | |
615 | int retval; | |
b302749b BP |
616 | |
617 | fatal_signal_run(); | |
618 | ||
064af421 | 619 | while ((retval = vconn_send(vconn, msg)) == EAGAIN) { |
60cb3eb8 BP |
620 | vconn_run(vconn); |
621 | vconn_run_wait(vconn); | |
064af421 BP |
622 | vconn_send_wait(vconn); |
623 | poll_block(); | |
624 | } | |
625 | return retval; | |
626 | } | |
627 | ||
628 | /* Same as vconn_recv, except that it waits until a message is received. */ | |
629 | int | |
630 | vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp) | |
631 | { | |
632 | int retval; | |
b302749b BP |
633 | |
634 | fatal_signal_run(); | |
635 | ||
064af421 | 636 | while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) { |
60cb3eb8 BP |
637 | vconn_run(vconn); |
638 | vconn_run_wait(vconn); | |
064af421 BP |
639 | vconn_recv_wait(vconn); |
640 | poll_block(); | |
641 | } | |
642 | return retval; | |
643 | } | |
644 | ||
645 | /* Waits until a message with a transaction ID matching 'xid' is recived on | |
646 | * 'vconn'. Returns 0 if successful, in which case the reply is stored in | |
647 | * '*replyp' for the caller to examine and free. Otherwise returns a positive | |
648 | * errno value, or EOF, and sets '*replyp' to null. | |
649 | * | |
650 | * 'request' is always destroyed, regardless of the return value. */ | |
651 | int | |
4408d18a | 652 | vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp) |
064af421 BP |
653 | { |
654 | for (;;) { | |
4408d18a | 655 | ovs_be32 recv_xid; |
064af421 BP |
656 | struct ofpbuf *reply; |
657 | int error; | |
658 | ||
659 | error = vconn_recv_block(vconn, &reply); | |
660 | if (error) { | |
661 | *replyp = NULL; | |
662 | return error; | |
663 | } | |
664 | recv_xid = ((struct ofp_header *) reply->data)->xid; | |
665 | if (xid == recv_xid) { | |
666 | *replyp = reply; | |
667 | return 0; | |
668 | } | |
669 | ||
670 | VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32 | |
44381c1b BP |
671 | " != expected %08"PRIx32, |
672 | vconn->name, ntohl(recv_xid), ntohl(xid)); | |
064af421 BP |
673 | ofpbuf_delete(reply); |
674 | } | |
675 | } | |
676 | ||
677 | /* Sends 'request' to 'vconn' and blocks until it receives a reply with a | |
678 | * matching transaction ID. Returns 0 if successful, in which case the reply | |
679 | * is stored in '*replyp' for the caller to examine and free. Otherwise | |
680 | * returns a positive errno value, or EOF, and sets '*replyp' to null. | |
681 | * | |
33af7dca BP |
682 | * 'request' should be an OpenFlow request that requires a reply. Otherwise, |
683 | * if there is no reply, this function can end up blocking forever (or until | |
684 | * the peer drops the connection). | |
685 | * | |
064af421 BP |
686 | * 'request' is always destroyed, regardless of the return value. */ |
687 | int | |
688 | vconn_transact(struct vconn *vconn, struct ofpbuf *request, | |
689 | struct ofpbuf **replyp) | |
690 | { | |
4408d18a | 691 | ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid; |
064af421 BP |
692 | int error; |
693 | ||
694 | *replyp = NULL; | |
695 | error = vconn_send_block(vconn, request); | |
696 | if (error) { | |
697 | ofpbuf_delete(request); | |
698 | } | |
699 | return error ? error : vconn_recv_xid(vconn, send_xid, replyp); | |
700 | } | |
701 | ||
33af7dca BP |
702 | /* Sends 'request' followed by a barrier request to 'vconn', then blocks until |
703 | * it receives a reply to the barrier. If successful, stores the reply to | |
704 | * 'request' in '*replyp', if one was received, and otherwise NULL, then | |
705 | * returns 0. Otherwise returns a positive errno value, or EOF, and sets | |
706 | * '*replyp' to null. | |
707 | * | |
708 | * This function is useful for sending an OpenFlow request that doesn't | |
709 | * ordinarily include a reply but might report an error in special | |
710 | * circumstances. | |
711 | * | |
712 | * 'request' is always destroyed, regardless of the return value. */ | |
713 | int | |
714 | vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request, | |
715 | struct ofpbuf **replyp) | |
716 | { | |
717 | ovs_be32 request_xid; | |
718 | ovs_be32 barrier_xid; | |
719 | struct ofpbuf *barrier; | |
720 | int error; | |
721 | ||
722 | *replyp = NULL; | |
723 | ||
724 | /* Send request. */ | |
725 | request_xid = ((struct ofp_header *) request->data)->xid; | |
726 | error = vconn_send_block(vconn, request); | |
727 | if (error) { | |
728 | ofpbuf_delete(request); | |
729 | return error; | |
730 | } | |
731 | ||
732 | /* Send barrier. */ | |
733 | make_openflow(sizeof(struct ofp_header), OFPT_BARRIER_REQUEST, &barrier); | |
734 | barrier_xid = ((struct ofp_header *) barrier->data)->xid; | |
735 | error = vconn_send_block(vconn, barrier); | |
736 | if (error) { | |
737 | ofpbuf_delete(barrier); | |
738 | return error; | |
739 | } | |
740 | ||
741 | for (;;) { | |
742 | struct ofpbuf *msg; | |
743 | ovs_be32 msg_xid; | |
744 | int error; | |
745 | ||
746 | error = vconn_recv_block(vconn, &msg); | |
747 | if (error) { | |
748 | ofpbuf_delete(*replyp); | |
749 | *replyp = NULL; | |
750 | return error; | |
751 | } | |
752 | ||
753 | msg_xid = ((struct ofp_header *) msg->data)->xid; | |
754 | if (msg_xid == request_xid) { | |
755 | if (*replyp) { | |
756 | VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with " | |
757 | "xid %08"PRIx32, vconn->name, ntohl(msg_xid)); | |
758 | ofpbuf_delete(*replyp); | |
759 | } | |
760 | *replyp = msg; | |
761 | } else { | |
762 | ofpbuf_delete(msg); | |
763 | if (msg_xid == barrier_xid) { | |
764 | return 0; | |
765 | } else { | |
766 | VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32 | |
767 | " != expected %08"PRIx32" or %08"PRIx32, | |
768 | vconn->name, ntohl(msg_xid), | |
769 | ntohl(request_xid), ntohl(barrier_xid)); | |
770 | } | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
7f009380 BP |
775 | /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one. |
776 | * All of the requests on 'requests' are always destroyed, regardless of the | |
777 | * return value. */ | |
778 | int | |
779 | vconn_transact_multiple_noreply(struct vconn *vconn, struct list *requests, | |
780 | struct ofpbuf **replyp) | |
781 | { | |
782 | struct ofpbuf *request, *next; | |
783 | ||
784 | LIST_FOR_EACH_SAFE (request, next, list_node, requests) { | |
785 | int error; | |
786 | ||
787 | list_remove(&request->list_node); | |
788 | ||
789 | error = vconn_transact_noreply(vconn, request, replyp); | |
790 | if (error || *replyp) { | |
791 | ofpbuf_list_delete(requests); | |
792 | return error; | |
793 | } | |
794 | } | |
795 | ||
796 | *replyp = NULL; | |
797 | return 0; | |
798 | } | |
799 | ||
064af421 BP |
800 | void |
801 | vconn_wait(struct vconn *vconn, enum vconn_wait_type wait) | |
802 | { | |
803 | assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND); | |
804 | ||
805 | switch (vconn->state) { | |
806 | case VCS_CONNECTING: | |
807 | wait = WAIT_CONNECT; | |
808 | break; | |
809 | ||
810 | case VCS_SEND_HELLO: | |
811 | case VCS_SEND_ERROR: | |
812 | wait = WAIT_SEND; | |
813 | break; | |
814 | ||
815 | case VCS_RECV_HELLO: | |
816 | wait = WAIT_RECV; | |
817 | break; | |
818 | ||
819 | case VCS_CONNECTED: | |
820 | break; | |
821 | ||
822 | case VCS_DISCONNECTED: | |
823 | poll_immediate_wake(); | |
824 | return; | |
825 | } | |
826 | (vconn->class->wait)(vconn, wait); | |
827 | } | |
828 | ||
829 | void | |
830 | vconn_connect_wait(struct vconn *vconn) | |
831 | { | |
832 | vconn_wait(vconn, WAIT_CONNECT); | |
833 | } | |
834 | ||
835 | void | |
836 | vconn_recv_wait(struct vconn *vconn) | |
837 | { | |
838 | vconn_wait(vconn, WAIT_RECV); | |
839 | } | |
840 | ||
841 | void | |
842 | vconn_send_wait(struct vconn *vconn) | |
843 | { | |
844 | vconn_wait(vconn, WAIT_SEND); | |
845 | } | |
846 | ||
30012c72 BP |
847 | /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class |
848 | * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores | |
849 | * a null pointer into '*classp' if 'name' is in the wrong form or if no such | |
850 | * class exists. */ | |
851 | static int | |
852 | pvconn_lookup_class(const char *name, struct pvconn_class **classp) | |
853 | { | |
854 | size_t prefix_len; | |
855 | ||
856 | prefix_len = strcspn(name, ":"); | |
857 | if (name[prefix_len] != '\0') { | |
858 | size_t i; | |
859 | ||
860 | for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { | |
861 | struct pvconn_class *class = pvconn_classes[i]; | |
862 | if (strlen(class->name) == prefix_len | |
863 | && !memcmp(class->name, name, prefix_len)) { | |
864 | *classp = class; | |
865 | return 0; | |
866 | } | |
867 | } | |
868 | } | |
869 | ||
870 | *classp = NULL; | |
871 | return EAFNOSUPPORT; | |
872 | } | |
873 | ||
874 | /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is | |
875 | * a supported connection type, otherwise EAFNOSUPPORT. */ | |
876 | int | |
877 | pvconn_verify_name(const char *name) | |
878 | { | |
879 | struct pvconn_class *class; | |
880 | return pvconn_lookup_class(name, &class); | |
881 | } | |
882 | ||
064af421 BP |
883 | /* Attempts to start listening for OpenFlow connections. 'name' is a |
884 | * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn | |
885 | * class's name and ARGS are vconn class-specific. | |
886 | * | |
887 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
888 | * stores a pointer to the new connection in '*pvconnp', otherwise a null | |
889 | * pointer. */ | |
890 | int | |
891 | pvconn_open(const char *name, struct pvconn **pvconnp) | |
892 | { | |
30012c72 BP |
893 | struct pvconn_class *class; |
894 | struct pvconn *pvconn; | |
895 | char *suffix_copy; | |
896 | int error; | |
064af421 BP |
897 | |
898 | check_vconn_classes(); | |
899 | ||
30012c72 BP |
900 | /* Look up the class. */ |
901 | error = pvconn_lookup_class(name, &class); | |
902 | if (!class) { | |
903 | goto error; | |
064af421 | 904 | } |
30012c72 BP |
905 | |
906 | /* Call class's "open" function. */ | |
907 | suffix_copy = xstrdup(strchr(name, ':') + 1); | |
908 | error = class->listen(name, suffix_copy, &pvconn); | |
909 | free(suffix_copy); | |
910 | if (error) { | |
911 | goto error; | |
064af421 | 912 | } |
30012c72 BP |
913 | |
914 | /* Success. */ | |
915 | *pvconnp = pvconn; | |
916 | return 0; | |
917 | ||
918 | error: | |
919 | *pvconnp = NULL; | |
920 | return error; | |
064af421 BP |
921 | } |
922 | ||
923 | /* Returns the name that was used to open 'pvconn'. The caller must not | |
924 | * modify or free the name. */ | |
925 | const char * | |
926 | pvconn_get_name(const struct pvconn *pvconn) | |
927 | { | |
928 | return pvconn->name; | |
929 | } | |
930 | ||
931 | /* Closes 'pvconn'. */ | |
932 | void | |
933 | pvconn_close(struct pvconn *pvconn) | |
934 | { | |
935 | if (pvconn != NULL) { | |
936 | char *name = pvconn->name; | |
937 | (pvconn->class->close)(pvconn); | |
938 | free(name); | |
939 | } | |
940 | } | |
941 | ||
942 | /* Tries to accept a new connection on 'pvconn'. If successful, stores the new | |
943 | * connection in '*new_vconn' and returns 0. Otherwise, returns a positive | |
944 | * errno value. | |
945 | * | |
946 | * The new vconn will automatically negotiate an OpenFlow protocol version | |
947 | * acceptable to both peers on the connection. The version negotiated will be | |
948 | * no lower than 'min_version' and no higher than OFP_VERSION. | |
949 | * | |
950 | * pvconn_accept() will not block waiting for a connection. If no connection | |
951 | * is ready to be accepted, it returns EAGAIN immediately. */ | |
952 | int | |
953 | pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn) | |
954 | { | |
955 | int retval = (pvconn->class->accept)(pvconn, new_vconn); | |
956 | if (retval) { | |
957 | *new_vconn = NULL; | |
958 | } else { | |
959 | assert((*new_vconn)->state != VCS_CONNECTING | |
960 | || (*new_vconn)->class->connect); | |
961 | (*new_vconn)->min_version = min_version; | |
962 | } | |
963 | return retval; | |
964 | } | |
965 | ||
966 | void | |
967 | pvconn_wait(struct pvconn *pvconn) | |
968 | { | |
969 | (pvconn->class->wait)(pvconn); | |
970 | } | |
971 | ||
85ab0a02 BP |
972 | /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'. |
973 | * The initial connection status, supplied as 'connect_status', is interpreted | |
974 | * as follows: | |
975 | * | |
976 | * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be | |
977 | * called in the normal fashion. | |
978 | * | |
979 | * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect' | |
980 | * function should be called to complete the connection. | |
981 | * | |
982 | * - Other positive errno values indicate that the connection failed with | |
983 | * the specified error. | |
984 | * | |
985 | * After calling this function, vconn_close() must be used to destroy 'vconn', | |
986 | * otherwise resources will be leaked. | |
987 | * | |
988 | * The caller retains ownership of 'name'. */ | |
064af421 BP |
989 | void |
990 | vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status, | |
85ab0a02 | 991 | const char *name) |
064af421 BP |
992 | { |
993 | vconn->class = class; | |
994 | vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING | |
995 | : !connect_status ? VCS_SEND_HELLO | |
996 | : VCS_DISCONNECTED); | |
997 | vconn->error = connect_status; | |
998 | vconn->version = -1; | |
999 | vconn->min_version = -1; | |
d7cca867 BP |
1000 | vconn->remote_ip = 0; |
1001 | vconn->remote_port = 0; | |
193456d5 JP |
1002 | vconn->local_ip = 0; |
1003 | vconn->local_port = 0; | |
064af421 | 1004 | vconn->name = xstrdup(name); |
e0668bd1 | 1005 | assert(vconn->state != VCS_CONNECTING || class->connect); |
064af421 BP |
1006 | } |
1007 | ||
d7cca867 | 1008 | void |
4408d18a | 1009 | vconn_set_remote_ip(struct vconn *vconn, ovs_be32 ip) |
d7cca867 BP |
1010 | { |
1011 | vconn->remote_ip = ip; | |
1012 | } | |
1013 | ||
1014 | void | |
4408d18a | 1015 | vconn_set_remote_port(struct vconn *vconn, ovs_be16 port) |
d7cca867 BP |
1016 | { |
1017 | vconn->remote_port = port; | |
1018 | } | |
1019 | ||
d295e8e9 | 1020 | void |
4408d18a | 1021 | vconn_set_local_ip(struct vconn *vconn, ovs_be32 ip) |
193456d5 JP |
1022 | { |
1023 | vconn->local_ip = ip; | |
1024 | } | |
1025 | ||
d295e8e9 | 1026 | void |
4408d18a | 1027 | vconn_set_local_port(struct vconn *vconn, ovs_be16 port) |
193456d5 JP |
1028 | { |
1029 | vconn->local_port = port; | |
1030 | } | |
1031 | ||
064af421 BP |
1032 | void |
1033 | pvconn_init(struct pvconn *pvconn, struct pvconn_class *class, | |
1034 | const char *name) | |
1035 | { | |
1036 | pvconn->class = class; | |
1037 | pvconn->name = xstrdup(name); | |
1038 | } |