]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn.c
ovsdb-idl: Improve prototypes.
[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 /* Sends 'requests' (which should be a multipart request) on 'vconn' and waits
950 * for the replies, which are put into 'replies'. Returns 0 if successful,
951 * otherwise an errno value. */
952 int
953 vconn_transact_multipart(struct vconn *vconn,
954 struct ovs_list *requests,
955 struct ovs_list *replies)
956 {
957 struct ofpbuf *rq = ofpbuf_from_list(ovs_list_front(requests));
958 ovs_be32 send_xid = ((struct ofp_header *) rq->data)->xid;
959
960 ovs_list_init(replies);
961
962 /* Send all the requests. */
963 struct ofpbuf *b, *next;
964 LIST_FOR_EACH_SAFE (b, next, list_node, requests) {
965 ovs_list_remove(&b->list_node);
966 int error = vconn_send_block(vconn, b);
967 if (error) {
968 ofpbuf_delete(b);
969 }
970 }
971
972 /* Receive all the replies. */
973 bool more;
974 do {
975 struct ofpbuf *reply;
976 int error = vconn_recv_xid__(vconn, send_xid, &reply, NULL);
977 if (error) {
978 ofpbuf_list_delete(replies);
979 return error;
980 }
981
982 ovs_list_push_back(replies, &reply->list_node);
983 more = ofpmsg_is_stat_reply(reply->data) && ofpmp_more(reply->data);
984 } while (more);
985
986 return 0;
987 }
988
989 static int
990 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
991 struct ofpbuf **replyp,
992 struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
993 {
994 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
995 struct ofpbuf *reply = *replyp;
996
997 for (;;) {
998 int retval;
999 bool more;
1000
1001 /* Get a flow stats reply message, if we don't already have one. */
1002 if (!reply) {
1003 enum ofptype type;
1004 enum ofperr error;
1005
1006 do {
1007 error = vconn_recv_block(vconn, &reply);
1008 if (error) {
1009 return error;
1010 }
1011 } while (((struct ofp_header *) reply->data)->xid != send_xid);
1012
1013 error = ofptype_decode(&type, reply->data);
1014 if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
1015 VLOG_WARN_RL(&rl, "received bad reply: %s",
1016 ofp_to_string(reply->data, reply->size,
1017 NULL, NULL, 1));
1018 return EPROTO;
1019 }
1020 }
1021
1022 /* Pull an individual flow stats reply out of the message. */
1023 retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
1024 switch (retval) {
1025 case 0:
1026 *replyp = reply;
1027 return 0;
1028
1029 case EOF:
1030 more = ofpmp_more(reply->header);
1031 ofpbuf_delete(reply);
1032 reply = NULL;
1033 if (!more) {
1034 *replyp = NULL;
1035 return EOF;
1036 }
1037 break;
1038
1039 default:
1040 VLOG_WARN_RL(&rl, "parse error in reply (%s)",
1041 ofperr_to_string(retval));
1042 return EPROTO;
1043 }
1044 }
1045 }
1046
1047 /* Sends 'fsr' to 'vconn', encoding it with the given 'protocol', and then
1048 * waits for, parses, and accumulates all of the replies into '*fsesp' and
1049 * '*n_fsesp'. The caller is responsible for freeing all of the flows.
1050 * Returns 0 if successful, otherwise a positive errno value. */
1051 int
1052 vconn_dump_flows(struct vconn *vconn,
1053 const struct ofputil_flow_stats_request *fsr,
1054 enum ofputil_protocol protocol,
1055 struct ofputil_flow_stats **fsesp, size_t *n_fsesp)
1056 {
1057 struct ofputil_flow_stats *fses = NULL;
1058 size_t n_fses = 0;
1059 size_t allocated_fses = 0;
1060
1061 struct ofpbuf *request = ofputil_encode_flow_stats_request(fsr, protocol);
1062 const struct ofp_header *oh = request->data;
1063 ovs_be32 send_xid = oh->xid;
1064 int error = vconn_send_block(vconn, request);
1065 if (error) {
1066 goto exit;
1067 }
1068
1069 struct ofpbuf *reply = NULL;
1070 struct ofpbuf ofpacts;
1071 ofpbuf_init(&ofpacts, 0);
1072 for (;;) {
1073 if (n_fses >= allocated_fses) {
1074 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1075 }
1076
1077 struct ofputil_flow_stats *fs = &fses[n_fses];
1078 error = recv_flow_stats_reply(vconn, send_xid, &reply, fs, &ofpacts);
1079 if (error) {
1080 if (error == EOF) {
1081 error = 0;
1082 }
1083 break;
1084 }
1085 fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1086 n_fses++;
1087 }
1088 ofpbuf_uninit(&ofpacts);
1089 ofpbuf_delete(reply);
1090
1091 if (error) {
1092 for (size_t i = 0; i < n_fses; i++) {
1093 free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1094 }
1095 free(fses);
1096
1097 fses = NULL;
1098 n_fses = 0;
1099 }
1100
1101 exit:
1102 *fsesp = fses;
1103 *n_fsesp = n_fses;
1104 return error;
1105 }
1106
1107
1108 static enum ofperr
1109 vconn_bundle_reply_validate(struct ofpbuf *reply,
1110 struct ofputil_bundle_ctrl_msg *request,
1111 struct ovs_list *errors)
1112 {
1113 const struct ofp_header *oh;
1114 enum ofptype type;
1115 enum ofperr error;
1116 struct ofputil_bundle_ctrl_msg rbc;
1117
1118 oh = reply->data;
1119 error = ofptype_decode(&type, oh);
1120 if (error) {
1121 return error;
1122 }
1123
1124 if (type == OFPTYPE_ERROR) {
1125 struct ofpbuf *copy = ofpbuf_clone(reply);
1126 ovs_list_push_back(errors, &copy->list_node);
1127 return ofperr_decode_msg(oh, NULL);
1128 }
1129 if (type != OFPTYPE_BUNDLE_CONTROL) {
1130 return OFPERR_OFPBRC_BAD_TYPE;
1131 }
1132
1133 error = ofputil_decode_bundle_ctrl(oh, &rbc);
1134 if (error) {
1135 return error;
1136 }
1137
1138 if (rbc.bundle_id != request->bundle_id) {
1139 return OFPERR_OFPBFC_BAD_ID;
1140 }
1141
1142 if (rbc.type != request->type + 1) {
1143 return OFPERR_OFPBFC_BAD_TYPE;
1144 }
1145
1146 return 0;
1147 }
1148
1149 /* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either
1150 * an error or the corresponding bundle control message response.
1151 *
1152 * 'errors' is a list to which any OpenFlow errors relating to bundle
1153 * processing are appended. Caller is responsible for releasing the memory of
1154 * each node in the list on return.
1155 *
1156 * Returns errno value, or 0 when successful. */
1157 static int
1158 vconn_bundle_control_transact(struct vconn *vconn,
1159 struct ofputil_bundle_ctrl_msg *bc,
1160 uint16_t type, struct ovs_list *errors)
1161 {
1162 struct ofpbuf *request, *reply;
1163 int error;
1164 enum ofperr ofperr;
1165
1166 bc->type = type;
1167 request = ofputil_encode_bundle_ctrl_request(vconn->version, bc);
1168 ofpmsg_update_length(request);
1169 error = vconn_transact__(vconn, request, &reply, errors);
1170 if (error) {
1171 return error;
1172 }
1173
1174 ofperr = vconn_bundle_reply_validate(reply, bc, errors);
1175 ofpbuf_delete(reply);
1176
1177 return ofperr ? EPROTO : 0;
1178 }
1179
1180 /* Checks if error responses can be received on 'vconn'. */
1181 static void
1182 vconn_recv_error(struct vconn *vconn, struct ovs_list *errors)
1183 {
1184 int error;
1185
1186 do {
1187 struct ofpbuf *reply;
1188
1189 error = vconn_recv(vconn, &reply);
1190 if (!error) {
1191 const struct ofp_header *oh;
1192 enum ofptype type;
1193 enum ofperr ofperr;
1194
1195 oh = reply->data;
1196 ofperr = ofptype_decode(&type, oh);
1197 if (!ofperr && type == OFPTYPE_ERROR) {
1198 ovs_list_push_back(errors, &reply->list_node);
1199 } else {
1200 VLOG_DBG_RL(&bad_ofmsg_rl,
1201 "%s: received unexpected reply with xid %08"PRIx32,
1202 vconn->name, ntohl(oh->xid));
1203 ofpbuf_delete(reply);
1204 }
1205 }
1206 } while (!error);
1207 }
1208
1209 /* Sends a barrier and waits for the barrier response and stores any errors
1210 * that are received before the barrier response. */
1211 static int
1212 vconn_bundle_barrier_transact(struct vconn *vconn, struct ovs_list *errors)
1213 {
1214 struct ofpbuf *reply;
1215 ovs_be32 barrier_xid;
1216 int error;
1217
1218 error = vconn_send_barrier(vconn, &barrier_xid);
1219 if (error) {
1220 return error;
1221 }
1222
1223 error = vconn_recv_xid__(vconn, barrier_xid, &reply, errors);
1224 if (error) {
1225 return error;
1226 }
1227 ofpbuf_delete(reply);
1228 return 0;
1229 }
1230
1231 static int
1232 vconn_bundle_add_msg(struct vconn *vconn, struct ofputil_bundle_ctrl_msg *bc,
1233 struct ofpbuf *msg,
1234 struct ovs_list *errors)
1235 {
1236 struct ofputil_bundle_add_msg bam;
1237 struct ofpbuf *request;
1238 int error;
1239
1240 ofpmsg_update_length(msg);
1241
1242 bam.bundle_id = bc->bundle_id;
1243 bam.flags = bc->flags;
1244 bam.msg = msg->data;
1245
1246 request = ofputil_encode_bundle_add(vconn->version, &bam);
1247
1248 error = vconn_send_block(vconn, request);
1249 if (!error) {
1250 /* Check for an error return, so that the socket buffer does not become
1251 * full of errors. */
1252 vconn_recv_error(vconn, errors);
1253 }
1254 return error;
1255 }
1256
1257 /* Appends ofpbufs for received errors, if any, to 'errors'. The caller must
1258 * free the received errors. */
1259 int
1260 vconn_bundle_transact(struct vconn *vconn, struct ovs_list *requests,
1261 uint16_t flags, struct ovs_list *errors)
1262 {
1263 struct ofputil_bundle_ctrl_msg bc;
1264 struct ofpbuf *request;
1265 int error;
1266
1267 ovs_list_init(errors);
1268
1269 memset(&bc, 0, sizeof bc);
1270 bc.flags = flags;
1271 error = vconn_bundle_control_transact(vconn, &bc, OFPBCT_OPEN_REQUEST,
1272 errors);
1273 if (error) {
1274 return error;
1275 }
1276
1277 LIST_FOR_EACH (request, list_node, requests) {
1278 error = vconn_bundle_add_msg(vconn, &bc, request, errors);
1279 if (error) {
1280 break;
1281 }
1282 }
1283
1284 if (!error) {
1285 /* A failing message does not invalidate the bundle, but the message is
1286 * simply not added to the bundle. Since we do not want to commit if
1287 * any of the messages failed, we need to explicitly sync with barrier
1288 * before we issue the commit message. */
1289 error = vconn_bundle_barrier_transact(vconn, errors);
1290 }
1291 if (!error && !ovs_list_is_empty(errors)) {
1292 error = EPROTO;
1293 }
1294
1295 /* Commit only if no errors are received. */
1296 if (!error) {
1297 error = vconn_bundle_control_transact(vconn, &bc,
1298 OFPBCT_COMMIT_REQUEST,
1299 errors);
1300 } else {
1301 vconn_bundle_control_transact(vconn, &bc, OFPBCT_DISCARD_REQUEST,
1302 errors);
1303 }
1304 return error;
1305 }
1306
1307 void
1308 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
1309 {
1310 ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
1311
1312 switch (vconn->state) {
1313 case VCS_CONNECTING:
1314 wait = WAIT_CONNECT;
1315 break;
1316
1317 case VCS_SEND_HELLO:
1318 case VCS_SEND_ERROR:
1319 wait = WAIT_SEND;
1320 break;
1321
1322 case VCS_RECV_HELLO:
1323 wait = WAIT_RECV;
1324 break;
1325
1326 case VCS_CONNECTED:
1327 break;
1328
1329 case VCS_DISCONNECTED:
1330 poll_immediate_wake();
1331 return;
1332 }
1333 (vconn->vclass->wait)(vconn, wait);
1334 }
1335
1336 void
1337 vconn_connect_wait(struct vconn *vconn)
1338 {
1339 vconn_wait(vconn, WAIT_CONNECT);
1340 }
1341
1342 void
1343 vconn_recv_wait(struct vconn *vconn)
1344 {
1345 vconn_wait(vconn, WAIT_RECV);
1346 }
1347
1348 void
1349 vconn_send_wait(struct vconn *vconn)
1350 {
1351 vconn_wait(vconn, WAIT_SEND);
1352 }
1353
1354 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
1355 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
1356 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
1357 * class exists. */
1358 static int
1359 pvconn_lookup_class(const char *name, const struct pvconn_class **classp)
1360 {
1361 size_t prefix_len;
1362
1363 prefix_len = strcspn(name, ":");
1364 if (name[prefix_len] != '\0') {
1365 size_t i;
1366
1367 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
1368 const struct pvconn_class *class = pvconn_classes[i];
1369 if (strlen(class->name) == prefix_len
1370 && !memcmp(class->name, name, prefix_len)) {
1371 *classp = class;
1372 return 0;
1373 }
1374 }
1375 }
1376
1377 *classp = NULL;
1378 return EAFNOSUPPORT;
1379 }
1380
1381 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
1382 * a supported connection type, otherwise EAFNOSUPPORT. */
1383 int
1384 pvconn_verify_name(const char *name)
1385 {
1386 const struct pvconn_class *class;
1387 return pvconn_lookup_class(name, &class);
1388 }
1389
1390 /* Attempts to start listening for OpenFlow connections. 'name' is a
1391 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
1392 * class's name and ARGS are vconn class-specific.
1393 *
1394 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1395 * protocol version acceptable to both peers on the connection. The version
1396 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1397 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1398 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1399 *
1400 * Returns 0 if successful, otherwise a positive errno value. If successful,
1401 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1402 * pointer. */
1403 int
1404 pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
1405 struct pvconn **pvconnp)
1406 {
1407 const struct pvconn_class *class;
1408 struct pvconn *pvconn;
1409 char *suffix_copy;
1410 int error;
1411
1412 check_vconn_classes();
1413
1414 if (!allowed_versions) {
1415 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
1416 }
1417
1418 /* Look up the class. */
1419 error = pvconn_lookup_class(name, &class);
1420 if (!class) {
1421 goto error;
1422 }
1423
1424 /* Call class's "open" function. */
1425 suffix_copy = xstrdup(strchr(name, ':') + 1);
1426 error = class->listen(name, allowed_versions, suffix_copy, &pvconn, dscp);
1427 free(suffix_copy);
1428 if (error) {
1429 goto error;
1430 }
1431
1432 /* Success. */
1433 *pvconnp = pvconn;
1434 return 0;
1435
1436 error:
1437 *pvconnp = NULL;
1438 return error;
1439 }
1440
1441 /* Returns the name that was used to open 'pvconn'. The caller must not
1442 * modify or free the name. */
1443 const char *
1444 pvconn_get_name(const struct pvconn *pvconn)
1445 {
1446 return pvconn->name;
1447 }
1448
1449 /* Closes 'pvconn'. */
1450 void
1451 pvconn_close(struct pvconn *pvconn)
1452 {
1453 if (pvconn != NULL) {
1454 char *name = pvconn->name;
1455 (pvconn->pvclass->close)(pvconn);
1456 free(name);
1457 }
1458 }
1459
1460 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1461 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1462 * errno value.
1463 *
1464 * pvconn_accept() will not block waiting for a connection. If no connection
1465 * is ready to be accepted, it returns EAGAIN immediately. */
1466 int
1467 pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn)
1468 {
1469 int retval = (pvconn->pvclass->accept)(pvconn, new_vconn);
1470 if (retval) {
1471 *new_vconn = NULL;
1472 } else {
1473 ovs_assert((*new_vconn)->state != VCS_CONNECTING
1474 || (*new_vconn)->vclass->connect);
1475 }
1476 return retval;
1477 }
1478
1479 void
1480 pvconn_wait(struct pvconn *pvconn)
1481 {
1482 (pvconn->pvclass->wait)(pvconn);
1483 }
1484
1485 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1486 * The initial connection status, supplied as 'connect_status', is interpreted
1487 * as follows:
1488 *
1489 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1490 * called in the normal fashion.
1491 *
1492 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1493 * function should be called to complete the connection.
1494 *
1495 * - Other positive errno values indicate that the connection failed with
1496 * the specified error.
1497 *
1498 * After calling this function, vconn_close() must be used to destroy 'vconn',
1499 * otherwise resources will be leaked.
1500 *
1501 * The caller retains ownership of 'name'. */
1502 void
1503 vconn_init(struct vconn *vconn, const struct vconn_class *class,
1504 int connect_status, const char *name, uint32_t allowed_versions)
1505 {
1506 memset(vconn, 0, sizeof *vconn);
1507 vconn->vclass = class;
1508 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1509 : !connect_status ? VCS_SEND_HELLO
1510 : VCS_DISCONNECTED);
1511 vconn->error = connect_status;
1512 vconn->allowed_versions = allowed_versions;
1513 vconn->name = xstrdup(name);
1514 ovs_assert(vconn->state != VCS_CONNECTING || class->connect);
1515 }
1516
1517 void
1518 pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class,
1519 const char *name, uint32_t allowed_versions)
1520 {
1521 pvconn->pvclass = class;
1522 pvconn->name = xstrdup(name);
1523 pvconn->allowed_versions = allowed_versions;
1524 }