2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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-print.h"
33 #include "openflow/nicira-ext.h"
34 #include "openflow/openflow.h"
36 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(vconn
);
43 COVERAGE_DEFINE(vconn_open
);
44 COVERAGE_DEFINE(vconn_received
);
45 COVERAGE_DEFINE(vconn_sent
);
47 /* State of an active vconn.*/
49 /* This is the ordinary progression of states. */
50 VCS_CONNECTING
, /* Underlying vconn is not connected. */
51 VCS_SEND_HELLO
, /* Waiting to send OFPT_HELLO message. */
52 VCS_RECV_HELLO
, /* Waiting to receive OFPT_HELLO message. */
53 VCS_CONNECTED
, /* Connection established. */
55 /* These states are entered only when something goes wrong. */
56 VCS_SEND_ERROR
, /* Sending OFPT_ERROR message. */
57 VCS_DISCONNECTED
/* Connection failed or connection closed. */
60 static struct vconn_class
*vconn_classes
[] = {
68 static struct pvconn_class
*pvconn_classes
[] = {
76 /* Rate limit for individual OpenFlow messages going over the vconn, output at
77 * DBG level. This is very high because, if these are enabled, it is because
78 * we really need to see them. */
79 static struct vlog_rate_limit ofmsg_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
81 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
82 * in the peer and so there's not much point in showing a lot of them. */
83 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
85 static int do_recv(struct vconn
*, struct ofpbuf
**);
86 static int do_send(struct vconn
*, struct ofpbuf
*);
88 /* Check the validity of the vconn class structures. */
90 check_vconn_classes(void)
95 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
96 struct vconn_class
*class = vconn_classes
[i
];
97 assert(class->name
!= NULL
);
98 assert(class->open
!= NULL
);
99 if (class->close
|| class->recv
|| class->send
100 || class->run
|| class->run_wait
|| class->wait
) {
101 assert(class->close
!= NULL
);
102 assert(class->recv
!= NULL
);
103 assert(class->send
!= NULL
);
104 assert(class->wait
!= NULL
);
106 /* This class delegates to another one. */
110 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
111 struct pvconn_class
*class = pvconn_classes
[i
];
112 assert(class->name
!= NULL
);
113 assert(class->listen
!= NULL
);
114 if (class->close
|| class->accept
|| class->wait
) {
115 assert(class->close
!= NULL
);
116 assert(class->accept
!= NULL
);
117 assert(class->wait
!= NULL
);
119 /* This class delegates to another one. */
125 /* Prints information on active (if 'active') and passive (if 'passive')
126 * connection methods supported by the vconn. If 'bootstrap' is true, also
127 * advertises options to bootstrap the CA certificate. */
129 vconn_usage(bool active
, bool passive
, bool bootstrap OVS_UNUSED
)
131 /* Really this should be implemented via callbacks into the vconn
132 * providers, but that seems too heavy-weight to bother with at the
137 printf("Active OpenFlow connection methods:\n");
138 printf(" tcp:IP[:PORT] "
139 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT
);
141 printf(" ssl:IP[:PORT] "
142 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT
);
144 printf(" unix:FILE Unix domain socket named FILE\n");
148 printf("Passive OpenFlow connection methods:\n");
149 printf(" ptcp:[PORT][:IP] "
150 "listen to TCP PORT (default: %d) on IP\n",
153 printf(" pssl:[PORT][:IP] "
154 "listen for SSL on PORT (default: %d) on IP\n",
157 printf(" punix:FILE "
158 "listen on Unix domain socket FILE\n");
162 printf("PKI configuration (required to use SSL):\n"
163 " -p, --private-key=FILE file with private key\n"
164 " -c, --certificate=FILE file with certificate for private key\n"
165 " -C, --ca-cert=FILE file with peer CA certificate\n");
167 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
168 "to read or create\n");
173 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
174 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
175 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
178 vconn_lookup_class(const char *name
, struct vconn_class
**classp
)
182 prefix_len
= strcspn(name
, ":");
183 if (name
[prefix_len
] != '\0') {
186 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
187 struct vconn_class
*class = vconn_classes
[i
];
188 if (strlen(class->name
) == prefix_len
189 && !memcmp(class->name
, name
, prefix_len
)) {
200 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
201 * a supported connection type, otherwise EAFNOSUPPORT. */
203 vconn_verify_name(const char *name
)
205 struct vconn_class
*class;
206 return vconn_lookup_class(name
, &class);
209 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
210 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
211 * are vconn class-specific.
213 * The vconn will automatically negotiate an OpenFlow protocol version
214 * acceptable to both peers on the connection. The version negotiated will be
215 * no lower than 'min_version' and no higher than OFP_VERSION.
217 * Returns 0 if successful, otherwise a positive errno value. If successful,
218 * stores a pointer to the new connection in '*vconnp', otherwise a null
221 vconn_open(const char *name
, int min_version
, struct vconn
**vconnp
)
223 struct vconn_class
*class;
228 COVERAGE_INC(vconn_open
);
229 check_vconn_classes();
231 /* Look up the class. */
232 error
= vconn_lookup_class(name
, &class);
237 /* Call class's "open" function. */
238 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
239 error
= class->open(name
, suffix_copy
, &vconn
);
246 assert(vconn
->state
!= VCS_CONNECTING
|| vconn
->class->connect
);
247 vconn
->min_version
= min_version
;
256 /* Allows 'vconn' to perform maintenance activities, such as flushing output
259 vconn_run(struct vconn
*vconn
)
261 if (vconn
->class->run
) {
262 (vconn
->class->run
)(vconn
);
266 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
267 * maintenance activities. */
269 vconn_run_wait(struct vconn
*vconn
)
271 if (vconn
->class->run_wait
) {
272 (vconn
->class->run_wait
)(vconn
);
277 vconn_open_block(const char *name
, int min_version
, struct vconn
**vconnp
)
284 error
= vconn_open(name
, min_version
, &vconn
);
286 while ((error
= vconn_connect(vconn
)) == EAGAIN
) {
288 vconn_run_wait(vconn
);
289 vconn_connect_wait(vconn
);
292 assert(error
!= EINPROGRESS
);
304 /* Closes 'vconn'. */
306 vconn_close(struct vconn
*vconn
)
309 char *name
= vconn
->name
;
310 (vconn
->class->close
)(vconn
);
315 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
317 vconn_get_name(const struct vconn
*vconn
)
322 /* Returns the IP address of the peer, or 0 if the peer is not connected over
323 * an IP-based protocol or if its IP address is not yet known. */
325 vconn_get_remote_ip(const struct vconn
*vconn
)
327 return vconn
->remote_ip
;
330 /* Returns the transport port of the peer, or 0 if the connection does not
331 * contain a port or if the port is not yet known. */
333 vconn_get_remote_port(const struct vconn
*vconn
)
335 return vconn
->remote_port
;
338 /* Returns the IP address used to connect to the peer, or 0 if the
339 * connection is not an IP-based protocol or if its IP address is not
342 vconn_get_local_ip(const struct vconn
*vconn
)
344 return vconn
->local_ip
;
347 /* Returns the transport port used to connect to the peer, or 0 if the
348 * connection does not contain a port or if the port is not yet known. */
350 vconn_get_local_port(const struct vconn
*vconn
)
352 return vconn
->local_port
;
356 vcs_connecting(struct vconn
*vconn
)
358 int retval
= (vconn
->class->connect
)(vconn
);
359 assert(retval
!= EINPROGRESS
);
361 vconn
->state
= VCS_SEND_HELLO
;
362 } else if (retval
!= EAGAIN
) {
363 vconn
->state
= VCS_DISCONNECTED
;
364 vconn
->error
= retval
;
369 vcs_send_hello(struct vconn
*vconn
)
374 make_openflow(sizeof(struct ofp_header
), OFPT_HELLO
, &b
);
375 retval
= do_send(vconn
, b
);
377 vconn
->state
= VCS_RECV_HELLO
;
380 if (retval
!= EAGAIN
) {
381 vconn
->state
= VCS_DISCONNECTED
;
382 vconn
->error
= retval
;
388 vcs_recv_hello(struct vconn
*vconn
)
393 retval
= do_recv(vconn
, &b
);
395 struct ofp_header
*oh
= b
->data
;
397 if (oh
->type
== OFPT_HELLO
) {
398 if (b
->size
> sizeof *oh
) {
399 struct ds msg
= DS_EMPTY_INITIALIZER
;
400 ds_put_format(&msg
, "%s: extra-long hello:\n", vconn
->name
);
401 ds_put_hex_dump(&msg
, b
->data
, b
->size
, 0, true);
402 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s", ds_cstr(&msg
));
406 vconn
->version
= MIN(OFP_VERSION
, oh
->version
);
407 if (vconn
->version
< vconn
->min_version
) {
408 VLOG_WARN_RL(&bad_ofmsg_rl
,
409 "%s: version negotiation failed: we support "
410 "versions 0x%02x to 0x%02x inclusive but peer "
411 "supports no later than version 0x%02"PRIx8
,
412 vconn
->name
, vconn
->min_version
, OFP_VERSION
,
414 vconn
->state
= VCS_SEND_ERROR
;
416 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
417 "(we support versions 0x%02x to 0x%02x inclusive, "
418 "peer no later than version 0x%02"PRIx8
")",
419 vconn
->name
, vconn
->version
, vconn
->min_version
,
420 OFP_VERSION
, oh
->version
);
421 vconn
->state
= VCS_CONNECTED
;
426 char *s
= ofp_to_string(b
->data
, b
->size
, 1);
427 VLOG_WARN_RL(&bad_ofmsg_rl
,
428 "%s: received message while expecting hello: %s",
436 if (retval
!= EAGAIN
) {
437 vconn
->state
= VCS_DISCONNECTED
;
438 vconn
->error
= retval
== EOF
? ECONNRESET
: retval
;
443 vcs_send_error(struct vconn
*vconn
)
445 struct ofp_error_msg
*error
;
450 snprintf(s
, sizeof s
, "We support versions 0x%02x to 0x%02x inclusive but "
451 "you support no later than version 0x%02"PRIx8
".",
452 vconn
->min_version
, OFP_VERSION
, vconn
->version
);
453 error
= make_openflow(sizeof *error
, OFPT_ERROR
, &b
);
454 error
->type
= htons(OFPET_HELLO_FAILED
);
455 error
->code
= htons(OFPHFC_INCOMPATIBLE
);
456 ofpbuf_put(b
, s
, strlen(s
));
457 update_openflow_length(b
);
458 retval
= do_send(vconn
, b
);
462 if (retval
!= EAGAIN
) {
463 vconn
->state
= VCS_DISCONNECTED
;
464 vconn
->error
= retval
? retval
: EPROTO
;
468 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
469 * complete, returns 0 if the connection was successful or a positive errno
470 * value if it failed. If the connection is still in progress, returns
473 vconn_connect(struct vconn
*vconn
)
475 enum vconn_state last_state
;
477 assert(vconn
->min_version
>= 0);
479 last_state
= vconn
->state
;
480 switch (vconn
->state
) {
482 vcs_connecting(vconn
);
486 vcs_send_hello(vconn
);
490 vcs_recv_hello(vconn
);
497 vcs_send_error(vconn
);
500 case VCS_DISCONNECTED
:
506 } while (vconn
->state
!= last_state
);
511 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
512 * the received message into '*msgp' and returns 0. The caller is responsible
513 * for destroying the message with ofpbuf_delete(). On failure, returns a
514 * positive errno value and stores a null pointer into '*msgp'. On normal
515 * connection close, returns EOF.
517 * vconn_recv will not block waiting for a packet to arrive. If no packets
518 * have been received, it returns EAGAIN immediately. */
520 vconn_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
522 int retval
= vconn_connect(vconn
);
524 retval
= do_recv(vconn
, msgp
);
530 do_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
532 int retval
= (vconn
->class->recv
)(vconn
, msgp
);
534 struct ofp_header
*oh
;
536 COVERAGE_INC(vconn_received
);
537 if (VLOG_IS_DBG_ENABLED()) {
538 char *s
= ofp_to_string((*msgp
)->data
, (*msgp
)->size
, 1);
539 VLOG_DBG_RL(&ofmsg_rl
, "%s: received: %s", vconn
->name
, s
);
543 oh
= ofpbuf_at_assert(*msgp
, 0, sizeof *oh
);
544 if (oh
->version
!= vconn
->version
545 && oh
->type
!= OFPT_HELLO
546 && oh
->type
!= OFPT_ERROR
547 && oh
->type
!= OFPT_ECHO_REQUEST
548 && oh
->type
!= OFPT_ECHO_REPLY
549 && oh
->type
!= OFPT_VENDOR
)
551 if (vconn
->version
< 0) {
552 VLOG_ERR_RL(&bad_ofmsg_rl
,
553 "%s: received OpenFlow message type %"PRIu8
" "
554 "before version negotiation complete",
555 vconn
->name
, oh
->type
);
557 VLOG_ERR_RL(&bad_ofmsg_rl
,
558 "%s: received OpenFlow version 0x%02"PRIx8
" "
560 vconn
->name
, oh
->version
, vconn
->version
);
562 ofpbuf_delete(*msgp
);
572 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
573 * in which case ownership of 'msg' is transferred to the vconn. Success does
574 * not guarantee that 'msg' has been or ever will be delivered to the peer,
575 * only that it has been queued for transmission.
577 * Returns a positive errno value on failure, in which case the caller
578 * retains ownership of 'msg'.
580 * vconn_send will not block. If 'msg' cannot be immediately accepted for
581 * transmission, it returns EAGAIN immediately. */
583 vconn_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
585 int retval
= vconn_connect(vconn
);
587 retval
= do_send(vconn
, msg
);
593 do_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
597 assert(msg
->size
>= sizeof(struct ofp_header
));
598 assert(((struct ofp_header
*) msg
->data
)->length
== htons(msg
->size
));
599 if (!VLOG_IS_DBG_ENABLED()) {
600 COVERAGE_INC(vconn_sent
);
601 retval
= (vconn
->class->send
)(vconn
, msg
);
603 char *s
= ofp_to_string(msg
->data
, msg
->size
, 1);
604 retval
= (vconn
->class->send
)(vconn
, msg
);
605 if (retval
!= EAGAIN
) {
606 VLOG_DBG_RL(&ofmsg_rl
, "%s: sent (%s): %s",
607 vconn
->name
, strerror(retval
), s
);
614 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
616 vconn_send_block(struct vconn
*vconn
, struct ofpbuf
*msg
)
622 while ((retval
= vconn_send(vconn
, msg
)) == EAGAIN
) {
624 vconn_run_wait(vconn
);
625 vconn_send_wait(vconn
);
631 /* Same as vconn_recv, except that it waits until a message is received. */
633 vconn_recv_block(struct vconn
*vconn
, struct ofpbuf
**msgp
)
639 while ((retval
= vconn_recv(vconn
, msgp
)) == EAGAIN
) {
641 vconn_run_wait(vconn
);
642 vconn_recv_wait(vconn
);
648 /* Waits until a message with a transaction ID matching 'xid' is recived on
649 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
650 * '*replyp' for the caller to examine and free. Otherwise returns a positive
651 * errno value, or EOF, and sets '*replyp' to null.
653 * 'request' is always destroyed, regardless of the return value. */
655 vconn_recv_xid(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
)
659 struct ofpbuf
*reply
;
662 error
= vconn_recv_block(vconn
, &reply
);
667 recv_xid
= ((struct ofp_header
*) reply
->data
)->xid
;
668 if (xid
== recv_xid
) {
673 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: received reply with xid %08"PRIx32
674 " != expected %08"PRIx32
,
675 vconn
->name
, ntohl(recv_xid
), ntohl(xid
));
676 ofpbuf_delete(reply
);
680 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
681 * matching transaction ID. Returns 0 if successful, in which case the reply
682 * is stored in '*replyp' for the caller to examine and free. Otherwise
683 * returns a positive errno value, or EOF, and sets '*replyp' to null.
685 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
686 * if there is no reply, this function can end up blocking forever (or until
687 * the peer drops the connection).
689 * 'request' is always destroyed, regardless of the return value. */
691 vconn_transact(struct vconn
*vconn
, struct ofpbuf
*request
,
692 struct ofpbuf
**replyp
)
694 ovs_be32 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
698 error
= vconn_send_block(vconn
, request
);
700 ofpbuf_delete(request
);
702 return error
? error
: vconn_recv_xid(vconn
, send_xid
, replyp
);
705 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
706 * it receives a reply to the barrier. If successful, stores the reply to
707 * 'request' in '*replyp', if one was received, and otherwise NULL, then
708 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
711 * This function is useful for sending an OpenFlow request that doesn't
712 * ordinarily include a reply but might report an error in special
715 * 'request' is always destroyed, regardless of the return value. */
717 vconn_transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
,
718 struct ofpbuf
**replyp
)
720 ovs_be32 request_xid
;
721 ovs_be32 barrier_xid
;
722 struct ofpbuf
*barrier
;
728 request_xid
= ((struct ofp_header
*) request
->data
)->xid
;
729 error
= vconn_send_block(vconn
, request
);
731 ofpbuf_delete(request
);
736 make_openflow(sizeof(struct ofp_header
), OFPT_BARRIER_REQUEST
, &barrier
);
737 barrier_xid
= ((struct ofp_header
*) barrier
->data
)->xid
;
738 error
= vconn_send_block(vconn
, barrier
);
740 ofpbuf_delete(barrier
);
749 error
= vconn_recv_block(vconn
, &msg
);
751 ofpbuf_delete(*replyp
);
756 msg_xid
= ((struct ofp_header
*) msg
->data
)->xid
;
757 if (msg_xid
== request_xid
) {
759 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s: duplicate replies with "
760 "xid %08"PRIx32
, vconn
->name
, ntohl(msg_xid
));
761 ofpbuf_delete(*replyp
);
766 if (msg_xid
== barrier_xid
) {
769 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: reply with xid %08"PRIx32
770 " != expected %08"PRIx32
" or %08"PRIx32
,
771 vconn
->name
, ntohl(msg_xid
),
772 ntohl(request_xid
), ntohl(barrier_xid
));
778 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
779 * All of the requests on 'requests' are always destroyed, regardless of the
782 vconn_transact_multiple_noreply(struct vconn
*vconn
, struct list
*requests
,
783 struct ofpbuf
**replyp
)
785 struct ofpbuf
*request
, *next
;
787 LIST_FOR_EACH_SAFE (request
, next
, list_node
, requests
) {
790 list_remove(&request
->list_node
);
792 error
= vconn_transact_noreply(vconn
, request
, replyp
);
793 if (error
|| *replyp
) {
794 ofpbuf_list_delete(requests
);
804 vconn_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
806 assert(wait
== WAIT_CONNECT
|| wait
== WAIT_RECV
|| wait
== WAIT_SEND
);
808 switch (vconn
->state
) {
825 case VCS_DISCONNECTED
:
826 poll_immediate_wake();
829 (vconn
->class->wait
)(vconn
, wait
);
833 vconn_connect_wait(struct vconn
*vconn
)
835 vconn_wait(vconn
, WAIT_CONNECT
);
839 vconn_recv_wait(struct vconn
*vconn
)
841 vconn_wait(vconn
, WAIT_RECV
);
845 vconn_send_wait(struct vconn
*vconn
)
847 vconn_wait(vconn
, WAIT_SEND
);
850 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
851 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
852 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
855 pvconn_lookup_class(const char *name
, struct pvconn_class
**classp
)
859 prefix_len
= strcspn(name
, ":");
860 if (name
[prefix_len
] != '\0') {
863 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
864 struct pvconn_class
*class = pvconn_classes
[i
];
865 if (strlen(class->name
) == prefix_len
866 && !memcmp(class->name
, name
, prefix_len
)) {
877 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
878 * a supported connection type, otherwise EAFNOSUPPORT. */
880 pvconn_verify_name(const char *name
)
882 struct pvconn_class
*class;
883 return pvconn_lookup_class(name
, &class);
886 /* Attempts to start listening for OpenFlow connections. 'name' is a
887 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
888 * class's name and ARGS are vconn class-specific.
890 * Returns 0 if successful, otherwise a positive errno value. If successful,
891 * stores a pointer to the new connection in '*pvconnp', otherwise a null
894 pvconn_open(const char *name
, struct pvconn
**pvconnp
)
896 struct pvconn_class
*class;
897 struct pvconn
*pvconn
;
901 check_vconn_classes();
903 /* Look up the class. */
904 error
= pvconn_lookup_class(name
, &class);
909 /* Call class's "open" function. */
910 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
911 error
= class->listen(name
, suffix_copy
, &pvconn
);
926 /* Returns the name that was used to open 'pvconn'. The caller must not
927 * modify or free the name. */
929 pvconn_get_name(const struct pvconn
*pvconn
)
934 /* Closes 'pvconn'. */
936 pvconn_close(struct pvconn
*pvconn
)
938 if (pvconn
!= NULL
) {
939 char *name
= pvconn
->name
;
940 (pvconn
->class->close
)(pvconn
);
945 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
946 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
949 * The new vconn will automatically negotiate an OpenFlow protocol version
950 * acceptable to both peers on the connection. The version negotiated will be
951 * no lower than 'min_version' and no higher than OFP_VERSION.
953 * pvconn_accept() will not block waiting for a connection. If no connection
954 * is ready to be accepted, it returns EAGAIN immediately. */
956 pvconn_accept(struct pvconn
*pvconn
, int min_version
, struct vconn
**new_vconn
)
958 int retval
= (pvconn
->class->accept
)(pvconn
, new_vconn
);
962 assert((*new_vconn
)->state
!= VCS_CONNECTING
963 || (*new_vconn
)->class->connect
);
964 (*new_vconn
)->min_version
= min_version
;
970 pvconn_wait(struct pvconn
*pvconn
)
972 (pvconn
->class->wait
)(pvconn
);
975 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
976 * The initial connection status, supplied as 'connect_status', is interpreted
979 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
980 * called in the normal fashion.
982 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
983 * function should be called to complete the connection.
985 * - Other positive errno values indicate that the connection failed with
986 * the specified error.
988 * After calling this function, vconn_close() must be used to destroy 'vconn',
989 * otherwise resources will be leaked.
991 * The caller retains ownership of 'name'. */
993 vconn_init(struct vconn
*vconn
, struct vconn_class
*class, int connect_status
,
996 vconn
->class = class;
997 vconn
->state
= (connect_status
== EAGAIN
? VCS_CONNECTING
998 : !connect_status
? VCS_SEND_HELLO
1000 vconn
->error
= connect_status
;
1001 vconn
->version
= -1;
1002 vconn
->min_version
= -1;
1003 vconn
->remote_ip
= 0;
1004 vconn
->remote_port
= 0;
1005 vconn
->local_ip
= 0;
1006 vconn
->local_port
= 0;
1007 vconn
->name
= xstrdup(name
);
1008 assert(vconn
->state
!= VCS_CONNECTING
|| class->connect
);
1012 vconn_set_remote_ip(struct vconn
*vconn
, ovs_be32 ip
)
1014 vconn
->remote_ip
= ip
;
1018 vconn_set_remote_port(struct vconn
*vconn
, ovs_be16 port
)
1020 vconn
->remote_port
= port
;
1024 vconn_set_local_ip(struct vconn
*vconn
, ovs_be32 ip
)
1026 vconn
->local_ip
= ip
;
1030 vconn_set_local_port(struct vconn
*vconn
, ovs_be16 port
)
1032 vconn
->local_port
= port
;
1036 pvconn_init(struct pvconn
*pvconn
, struct pvconn_class
*class,
1039 pvconn
->class = class;
1040 pvconn
->name
= xstrdup(name
);