2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "vconn-provider.h"
22 #include <netinet/in.h>
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
30 #include "ofp-errors.h"
32 #include "ofp-print.h"
35 #include "openflow/nicira-ext.h"
36 #include "openflow/openflow.h"
38 #include "poll-loop.h"
42 #include "socket-util.h"
44 VLOG_DEFINE_THIS_MODULE(vconn
);
46 COVERAGE_DEFINE(vconn_open
);
47 COVERAGE_DEFINE(vconn_received
);
48 COVERAGE_DEFINE(vconn_sent
);
50 /* State of an active vconn.*/
52 /* This is the ordinary progression of states. */
53 VCS_CONNECTING
, /* Underlying vconn is not connected. */
54 VCS_SEND_HELLO
, /* Waiting to send OFPT_HELLO message. */
55 VCS_RECV_HELLO
, /* Waiting to receive OFPT_HELLO message. */
56 VCS_CONNECTED
, /* Connection established. */
58 /* These states are entered only when something goes wrong. */
59 VCS_SEND_ERROR
, /* Sending OFPT_ERROR message. */
60 VCS_DISCONNECTED
/* Connection failed or connection closed. */
63 static struct vconn_class
*vconn_classes
[] = {
71 static struct pvconn_class
*pvconn_classes
[] = {
79 /* Rate limit for individual OpenFlow messages going over the vconn, output at
80 * DBG level. This is very high because, if these are enabled, it is because
81 * we really need to see them. */
82 static struct vlog_rate_limit ofmsg_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
84 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
85 * in the peer and so there's not much point in showing a lot of them. */
86 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
88 static int do_recv(struct vconn
*, struct ofpbuf
**);
89 static int do_send(struct vconn
*, struct ofpbuf
*);
91 /* Check the validity of the vconn class structures. */
93 check_vconn_classes(void)
98 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
99 struct vconn_class
*class = vconn_classes
[i
];
100 assert(class->name
!= NULL
);
101 assert(class->open
!= NULL
);
102 if (class->close
|| class->recv
|| class->send
103 || class->run
|| class->run_wait
|| class->wait
) {
104 assert(class->close
!= NULL
);
105 assert(class->recv
!= NULL
);
106 assert(class->send
!= NULL
);
107 assert(class->wait
!= NULL
);
109 /* This class delegates to another one. */
113 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
114 struct pvconn_class
*class = pvconn_classes
[i
];
115 assert(class->name
!= NULL
);
116 assert(class->listen
!= NULL
);
117 if (class->close
|| class->accept
|| class->wait
) {
118 assert(class->close
!= NULL
);
119 assert(class->accept
!= NULL
);
120 assert(class->wait
!= NULL
);
122 /* This class delegates to another one. */
128 /* Prints information on active (if 'active') and passive (if 'passive')
129 * connection methods supported by the vconn. If 'bootstrap' is true, also
130 * advertises options to bootstrap the CA certificate. */
132 vconn_usage(bool active
, bool passive
, bool bootstrap OVS_UNUSED
)
134 /* Really this should be implemented via callbacks into the vconn
135 * providers, but that seems too heavy-weight to bother with at the
140 printf("Active OpenFlow connection methods:\n");
141 printf(" tcp:IP[:PORT] "
142 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT
);
144 printf(" ssl:IP[:PORT] "
145 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT
);
147 printf(" unix:FILE Unix domain socket named FILE\n");
151 printf("Passive OpenFlow connection methods:\n");
152 printf(" ptcp:[PORT][:IP] "
153 "listen to TCP PORT (default: %d) on IP\n",
156 printf(" pssl:[PORT][:IP] "
157 "listen for SSL on PORT (default: %d) on IP\n",
160 printf(" punix:FILE "
161 "listen on Unix domain socket FILE\n");
165 printf("PKI configuration (required to use SSL):\n"
166 " -p, --private-key=FILE file with private key\n"
167 " -c, --certificate=FILE file with certificate for private key\n"
168 " -C, --ca-cert=FILE file with peer CA certificate\n");
170 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
171 "to read or create\n");
176 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
177 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
178 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
181 vconn_lookup_class(const char *name
, struct vconn_class
**classp
)
185 prefix_len
= strcspn(name
, ":");
186 if (name
[prefix_len
] != '\0') {
189 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
190 struct vconn_class
*class = vconn_classes
[i
];
191 if (strlen(class->name
) == prefix_len
192 && !memcmp(class->name
, name
, prefix_len
)) {
203 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
204 * a supported connection type, otherwise EAFNOSUPPORT. */
206 vconn_verify_name(const char *name
)
208 struct vconn_class
*class;
209 return vconn_lookup_class(name
, &class);
212 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
213 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
214 * are vconn class-specific.
216 * The vconn will automatically negotiate an OpenFlow protocol version
217 * acceptable to both peers on the connection. The version negotiated will be
218 * no lower than 'min_version' and no higher than OFP10_VERSION.
220 * Returns 0 if successful, otherwise a positive errno value. If successful,
221 * stores a pointer to the new connection in '*vconnp', otherwise a null
224 vconn_open(const char *name
, int min_version
, struct vconn
**vconnp
,
227 struct vconn_class
*class;
232 COVERAGE_INC(vconn_open
);
233 check_vconn_classes();
235 /* Look up the class. */
236 error
= vconn_lookup_class(name
, &class);
241 /* Call class's "open" function. */
242 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
243 error
= class->open(name
, suffix_copy
, &vconn
, dscp
);
250 assert(vconn
->state
!= VCS_CONNECTING
|| vconn
->class->connect
);
251 vconn
->min_version
= min_version
;
260 /* Allows 'vconn' to perform maintenance activities, such as flushing output
263 vconn_run(struct vconn
*vconn
)
265 if (vconn
->class->run
) {
266 (vconn
->class->run
)(vconn
);
270 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
271 * maintenance activities. */
273 vconn_run_wait(struct vconn
*vconn
)
275 if (vconn
->class->run_wait
) {
276 (vconn
->class->run_wait
)(vconn
);
281 vconn_open_block(const char *name
, enum ofp_version min_version
,
282 struct vconn
**vconnp
)
289 error
= vconn_open(name
, min_version
, &vconn
, DSCP_DEFAULT
);
291 error
= vconn_connect_block(vconn
);
303 /* Closes 'vconn'. */
305 vconn_close(struct vconn
*vconn
)
308 char *name
= vconn
->name
;
309 (vconn
->class->close
)(vconn
);
314 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
316 vconn_get_name(const struct vconn
*vconn
)
321 /* Returns the IP address of the peer, or 0 if the peer is not connected over
322 * an IP-based protocol or if its IP address is not yet known. */
324 vconn_get_remote_ip(const struct vconn
*vconn
)
326 return vconn
->remote_ip
;
329 /* Returns the transport port of the peer, or 0 if the connection does not
330 * contain a port or if the port is not yet known. */
332 vconn_get_remote_port(const struct vconn
*vconn
)
334 return vconn
->remote_port
;
337 /* Returns the IP address used to connect to the peer, or 0 if the
338 * connection is not an IP-based protocol or if its IP address is not
341 vconn_get_local_ip(const struct vconn
*vconn
)
343 return vconn
->local_ip
;
346 /* Returns the transport port used to connect to the peer, or 0 if the
347 * connection does not contain a port or if the port is not yet known. */
349 vconn_get_local_port(const struct vconn
*vconn
)
351 return vconn
->local_port
;
354 /* Returns the OpenFlow version negotiated with the peer, or -1 if version
355 * negotiation is not yet complete.
357 * A vconn that has successfully connected (that is, vconn_connect() or
358 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
360 vconn_get_version(const struct vconn
*vconn
)
362 return vconn
->version
;
366 vcs_connecting(struct vconn
*vconn
)
368 int retval
= (vconn
->class->connect
)(vconn
);
369 assert(retval
!= EINPROGRESS
);
371 vconn
->state
= VCS_SEND_HELLO
;
372 } else if (retval
!= EAGAIN
) {
373 vconn
->state
= VCS_DISCONNECTED
;
374 vconn
->error
= retval
;
379 vcs_send_hello(struct vconn
*vconn
)
384 b
= ofpraw_alloc(OFPRAW_OFPT_HELLO
, OFP10_VERSION
, 0);
385 retval
= do_send(vconn
, b
);
387 vconn
->state
= VCS_RECV_HELLO
;
390 if (retval
!= EAGAIN
) {
391 vconn
->state
= VCS_DISCONNECTED
;
392 vconn
->error
= retval
;
398 vcs_recv_hello(struct vconn
*vconn
)
403 retval
= do_recv(vconn
, &b
);
405 const struct ofp_header
*oh
= b
->data
;
409 error
= ofptype_decode(&type
, b
->data
);
410 if (!error
&& type
== OFPTYPE_HELLO
) {
411 if (b
->size
> sizeof *oh
) {
412 struct ds msg
= DS_EMPTY_INITIALIZER
;
413 ds_put_format(&msg
, "%s: extra-long hello:\n", vconn
->name
);
414 ds_put_hex_dump(&msg
, b
->data
, b
->size
, 0, true);
415 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s", ds_cstr(&msg
));
419 vconn
->version
= MIN(OFP10_VERSION
, oh
->version
);
420 if (vconn
->version
< vconn
->min_version
) {
421 VLOG_WARN_RL(&bad_ofmsg_rl
,
422 "%s: version negotiation failed: we support "
423 "versions 0x%02x to 0x%02x inclusive but peer "
424 "supports no later than version 0x%02"PRIx8
,
425 vconn
->name
, vconn
->min_version
, OFP10_VERSION
,
427 vconn
->state
= VCS_SEND_ERROR
;
429 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
430 "(we support versions 0x%02x to 0x%02x inclusive, "
431 "peer no later than version 0x%02"PRIx8
")",
432 vconn
->name
, vconn
->version
, vconn
->min_version
,
433 OFP10_VERSION
, oh
->version
);
434 vconn
->state
= VCS_CONNECTED
;
439 char *s
= ofp_to_string(b
->data
, b
->size
, 1);
440 VLOG_WARN_RL(&bad_ofmsg_rl
,
441 "%s: received message while expecting hello: %s",
449 if (retval
!= EAGAIN
) {
450 vconn
->state
= VCS_DISCONNECTED
;
451 vconn
->error
= retval
== EOF
? ECONNRESET
: retval
;
456 vcs_send_error(struct vconn
*vconn
)
462 snprintf(s
, sizeof s
, "We support versions 0x%02x to 0x%02x inclusive but "
463 "you support no later than version 0x%02"PRIx8
".",
464 vconn
->min_version
, OFP10_VERSION
, vconn
->version
);
465 b
= ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE
,
466 ofperr_domain_from_version(vconn
->version
), s
);
467 retval
= do_send(vconn
, b
);
471 if (retval
!= EAGAIN
) {
472 vconn
->state
= VCS_DISCONNECTED
;
473 vconn
->error
= retval
? retval
: EPROTO
;
477 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
478 * complete, returns 0 if the connection was successful or a positive errno
479 * value if it failed. If the connection is still in progress, returns
482 vconn_connect(struct vconn
*vconn
)
484 enum vconn_state last_state
;
486 assert(vconn
->min_version
> 0);
488 last_state
= vconn
->state
;
489 switch (vconn
->state
) {
491 vcs_connecting(vconn
);
495 vcs_send_hello(vconn
);
499 vcs_recv_hello(vconn
);
506 vcs_send_error(vconn
);
509 case VCS_DISCONNECTED
:
515 } while (vconn
->state
!= last_state
);
520 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
521 * the received message into '*msgp' and returns 0. The caller is responsible
522 * for destroying the message with ofpbuf_delete(). On failure, returns a
523 * positive errno value and stores a null pointer into '*msgp'. On normal
524 * connection close, returns EOF.
526 * vconn_recv will not block waiting for a packet to arrive. If no packets
527 * have been received, it returns EAGAIN immediately. */
529 vconn_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
534 retval
= vconn_connect(vconn
);
536 retval
= do_recv(vconn
, &msg
);
539 const struct ofp_header
*oh
= msg
->data
;
540 if (oh
->version
!= vconn
->version
) {
543 if (ofptype_decode(&type
, msg
->data
)
544 || (type
!= OFPTYPE_HELLO
&&
545 type
!= OFPTYPE_ERROR
&&
546 type
!= OFPTYPE_ECHO_REQUEST
&&
547 type
!= OFPTYPE_ECHO_REPLY
)) {
548 VLOG_ERR_RL(&bad_ofmsg_rl
, "%s: received OpenFlow version "
549 "0x%02"PRIx8
" != expected %02x",
550 vconn
->name
, oh
->version
, vconn
->version
);
557 *msgp
= retval
? NULL
: msg
;
562 do_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
564 int retval
= (vconn
->class->recv
)(vconn
, msgp
);
566 COVERAGE_INC(vconn_received
);
567 if (VLOG_IS_DBG_ENABLED()) {
568 char *s
= ofp_to_string((*msgp
)->data
, (*msgp
)->size
, 1);
569 VLOG_DBG_RL(&ofmsg_rl
, "%s: received: %s", vconn
->name
, s
);
576 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
577 * in which case ownership of 'msg' is transferred to the vconn. Success does
578 * not guarantee that 'msg' has been or ever will be delivered to the peer,
579 * only that it has been queued for transmission.
581 * Returns a positive errno value on failure, in which case the caller
582 * retains ownership of 'msg'.
584 * vconn_send will not block. If 'msg' cannot be immediately accepted for
585 * transmission, it returns EAGAIN immediately. */
587 vconn_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
589 int retval
= vconn_connect(vconn
);
591 retval
= do_send(vconn
, msg
);
597 do_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
601 assert(msg
->size
>= sizeof(struct ofp_header
));
603 ofpmsg_update_length(msg
);
604 if (!VLOG_IS_DBG_ENABLED()) {
605 COVERAGE_INC(vconn_sent
);
606 retval
= (vconn
->class->send
)(vconn
, msg
);
608 char *s
= ofp_to_string(msg
->data
, msg
->size
, 1);
609 retval
= (vconn
->class->send
)(vconn
, msg
);
610 if (retval
!= EAGAIN
) {
611 VLOG_DBG_RL(&ofmsg_rl
, "%s: sent (%s): %s",
612 vconn
->name
, strerror(retval
), s
);
619 /* Same as vconn_connect(), except that it waits until the connection on
620 * 'vconn' completes or fails. Thus, it will never return EAGAIN. */
622 vconn_connect_block(struct vconn
*vconn
)
626 while ((error
= vconn_connect(vconn
)) == EAGAIN
) {
628 vconn_run_wait(vconn
);
629 vconn_connect_wait(vconn
);
632 assert(error
!= EINPROGRESS
);
637 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
639 vconn_send_block(struct vconn
*vconn
, struct ofpbuf
*msg
)
645 while ((retval
= vconn_send(vconn
, msg
)) == EAGAIN
) {
647 vconn_run_wait(vconn
);
648 vconn_send_wait(vconn
);
654 /* Same as vconn_recv, except that it waits until a message is received. */
656 vconn_recv_block(struct vconn
*vconn
, struct ofpbuf
**msgp
)
662 while ((retval
= vconn_recv(vconn
, msgp
)) == EAGAIN
) {
664 vconn_run_wait(vconn
);
665 vconn_recv_wait(vconn
);
671 /* Waits until a message with a transaction ID matching 'xid' is recived on
672 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
673 * '*replyp' for the caller to examine and free. Otherwise returns a positive
674 * errno value, or EOF, and sets '*replyp' to null.
676 * 'request' is always destroyed, regardless of the return value. */
678 vconn_recv_xid(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
)
682 struct ofpbuf
*reply
;
685 error
= vconn_recv_block(vconn
, &reply
);
690 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
691 if (xid
== recv_xid
) {
696 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: received reply with xid %08"PRIx32
697 " != expected %08"PRIx32
,
698 vconn
->name
, ntohl(recv_xid
), ntohl(xid
));
699 ofpbuf_delete(reply
);
703 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
704 * matching transaction ID. Returns 0 if successful, in which case the reply
705 * is stored in '*replyp' for the caller to examine and free. Otherwise
706 * returns a positive errno value, or EOF, and sets '*replyp' to null.
708 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
709 * if there is no reply, this function can end up blocking forever (or until
710 * the peer drops the connection).
712 * 'request' is always destroyed, regardless of the return value. */
714 vconn_transact(struct vconn
*vconn
, struct ofpbuf
*request
,
715 struct ofpbuf
**replyp
)
717 ovs_be32 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
721 error
= vconn_send_block(vconn
, request
);
723 ofpbuf_delete(request
);
725 return error
? error
: vconn_recv_xid(vconn
, send_xid
, replyp
);
728 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
729 * it receives a reply to the barrier. If successful, stores the reply to
730 * 'request' in '*replyp', if one was received, and otherwise NULL, then
731 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
734 * This function is useful for sending an OpenFlow request that doesn't
735 * ordinarily include a reply but might report an error in special
738 * 'request' is always destroyed, regardless of the return value. */
740 vconn_transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
,
741 struct ofpbuf
**replyp
)
743 ovs_be32 request_xid
;
744 ovs_be32 barrier_xid
;
745 struct ofpbuf
*barrier
;
751 request_xid
= ((struct ofp_header
*) request
->data
)->xid
;
752 error
= vconn_send_block(vconn
, request
);
754 ofpbuf_delete(request
);
759 barrier
= ofputil_encode_barrier_request();
760 barrier_xid
= ((struct ofp_header
*) barrier
->data
)->xid
;
761 error
= vconn_send_block(vconn
, barrier
);
763 ofpbuf_delete(barrier
);
772 error
= vconn_recv_block(vconn
, &msg
);
774 ofpbuf_delete(*replyp
);
779 msg_xid
= ((struct ofp_header
*) msg
->data
)->xid
;
780 if (msg_xid
== request_xid
) {
782 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s: duplicate replies with "
783 "xid %08"PRIx32
, vconn
->name
, ntohl(msg_xid
));
784 ofpbuf_delete(*replyp
);
789 if (msg_xid
== barrier_xid
) {
792 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: reply with xid %08"PRIx32
793 " != expected %08"PRIx32
" or %08"PRIx32
,
794 vconn
->name
, ntohl(msg_xid
),
795 ntohl(request_xid
), ntohl(barrier_xid
));
801 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
802 * All of the requests on 'requests' are always destroyed, regardless of the
805 vconn_transact_multiple_noreply(struct vconn
*vconn
, struct list
*requests
,
806 struct ofpbuf
**replyp
)
808 struct ofpbuf
*request
, *next
;
810 LIST_FOR_EACH_SAFE (request
, next
, list_node
, requests
) {
813 list_remove(&request
->list_node
);
815 error
= vconn_transact_noreply(vconn
, request
, replyp
);
816 if (error
|| *replyp
) {
817 ofpbuf_list_delete(requests
);
827 vconn_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
829 assert(wait
== WAIT_CONNECT
|| wait
== WAIT_RECV
|| wait
== WAIT_SEND
);
831 switch (vconn
->state
) {
848 case VCS_DISCONNECTED
:
849 poll_immediate_wake();
852 (vconn
->class->wait
)(vconn
, wait
);
856 vconn_connect_wait(struct vconn
*vconn
)
858 vconn_wait(vconn
, WAIT_CONNECT
);
862 vconn_recv_wait(struct vconn
*vconn
)
864 vconn_wait(vconn
, WAIT_RECV
);
868 vconn_send_wait(struct vconn
*vconn
)
870 vconn_wait(vconn
, WAIT_SEND
);
873 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
874 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
875 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
878 pvconn_lookup_class(const char *name
, struct pvconn_class
**classp
)
882 prefix_len
= strcspn(name
, ":");
883 if (name
[prefix_len
] != '\0') {
886 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
887 struct pvconn_class
*class = pvconn_classes
[i
];
888 if (strlen(class->name
) == prefix_len
889 && !memcmp(class->name
, name
, prefix_len
)) {
900 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
901 * a supported connection type, otherwise EAFNOSUPPORT. */
903 pvconn_verify_name(const char *name
)
905 struct pvconn_class
*class;
906 return pvconn_lookup_class(name
, &class);
909 /* Attempts to start listening for OpenFlow connections. 'name' is a
910 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
911 * class's name and ARGS are vconn class-specific.
913 * Returns 0 if successful, otherwise a positive errno value. If successful,
914 * stores a pointer to the new connection in '*pvconnp', otherwise a null
917 pvconn_open(const char *name
, struct pvconn
**pvconnp
, uint8_t dscp
)
919 struct pvconn_class
*class;
920 struct pvconn
*pvconn
;
924 check_vconn_classes();
926 /* Look up the class. */
927 error
= pvconn_lookup_class(name
, &class);
932 /* Call class's "open" function. */
933 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
934 error
= class->listen(name
, suffix_copy
, &pvconn
, dscp
);
949 /* Returns the name that was used to open 'pvconn'. The caller must not
950 * modify or free the name. */
952 pvconn_get_name(const struct pvconn
*pvconn
)
957 /* Closes 'pvconn'. */
959 pvconn_close(struct pvconn
*pvconn
)
961 if (pvconn
!= NULL
) {
962 char *name
= pvconn
->name
;
963 (pvconn
->class->close
)(pvconn
);
968 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
969 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
972 * The new vconn will automatically negotiate an OpenFlow protocol version
973 * acceptable to both peers on the connection. The version negotiated will be
974 * no lower than 'min_version' and no higher than OFP10_VERSION.
976 * pvconn_accept() will not block waiting for a connection. If no connection
977 * is ready to be accepted, it returns EAGAIN immediately. */
979 pvconn_accept(struct pvconn
*pvconn
, int min_version
, struct vconn
**new_vconn
)
981 int retval
= (pvconn
->class->accept
)(pvconn
, new_vconn
);
985 assert((*new_vconn
)->state
!= VCS_CONNECTING
986 || (*new_vconn
)->class->connect
);
987 (*new_vconn
)->min_version
= min_version
;
993 pvconn_wait(struct pvconn
*pvconn
)
995 (pvconn
->class->wait
)(pvconn
);
998 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
999 * The initial connection status, supplied as 'connect_status', is interpreted
1002 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1003 * called in the normal fashion.
1005 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1006 * function should be called to complete the connection.
1008 * - Other positive errno values indicate that the connection failed with
1009 * the specified error.
1011 * After calling this function, vconn_close() must be used to destroy 'vconn',
1012 * otherwise resources will be leaked.
1014 * The caller retains ownership of 'name'. */
1016 vconn_init(struct vconn
*vconn
, struct vconn_class
*class, int connect_status
,
1019 vconn
->class = class;
1020 vconn
->state
= (connect_status
== EAGAIN
? VCS_CONNECTING
1021 : !connect_status
? VCS_SEND_HELLO
1022 : VCS_DISCONNECTED
);
1023 vconn
->error
= connect_status
;
1025 vconn
->min_version
= 0;
1026 vconn
->remote_ip
= 0;
1027 vconn
->remote_port
= 0;
1028 vconn
->local_ip
= 0;
1029 vconn
->local_port
= 0;
1030 vconn
->name
= xstrdup(name
);
1031 assert(vconn
->state
!= VCS_CONNECTING
|| class->connect
);
1035 vconn_set_remote_ip(struct vconn
*vconn
, ovs_be32 ip
)
1037 vconn
->remote_ip
= ip
;
1041 vconn_set_remote_port(struct vconn
*vconn
, ovs_be16 port
)
1043 vconn
->remote_port
= port
;
1047 vconn_set_local_ip(struct vconn
*vconn
, ovs_be32 ip
)
1049 vconn
->local_ip
= ip
;
1053 vconn_set_local_port(struct vconn
*vconn
, ovs_be16 port
)
1055 vconn
->local_port
= port
;
1059 pvconn_init(struct pvconn
*pvconn
, struct pvconn_class
*class,
1062 pvconn
->class = class;
1063 pvconn
->name
= xstrdup(name
);