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