2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
21 #include <netinet/in.h>
26 #include "dynamic-string.h"
27 #include "fatal-signal.h"
29 #include "ofp-errors.h"
31 #include "ofp-print.h"
34 #include "openflow/nicira-ext.h"
35 #include "openflow/openflow.h"
37 #include "poll-loop.h"
41 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(vconn);
45 COVERAGE_DEFINE(vconn_open);
46 COVERAGE_DEFINE(vconn_received);
47 COVERAGE_DEFINE(vconn_sent);
49 /* State of an active vconn.*/
51 /* This is the ordinary progression of states. */
52 VCS_CONNECTING, /* Underlying vconn is not connected. */
53 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
54 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
55 VCS_CONNECTED, /* Connection established. */
57 /* These states are entered only when something goes wrong. */
58 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
59 VCS_DISCONNECTED /* Connection failed or connection closed. */
62 static const struct vconn_class *vconn_classes[] = {
70 static const struct pvconn_class *pvconn_classes[] = {
78 /* Rate limit for individual OpenFlow messages going over the vconn, output at
79 * DBG level. This is very high because, if these are enabled, it is because
80 * we really need to see them. */
81 static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
83 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
84 * in the peer and so there's not much point in showing a lot of them. */
85 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
87 static int do_recv(struct vconn *, struct ofpbuf **);
88 static int do_send(struct vconn *, struct ofpbuf *);
90 /* Check the validity of the vconn class structures. */
92 check_vconn_classes(void)
97 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
98 const struct vconn_class *class = vconn_classes[i];
99 ovs_assert(class->name != NULL);
100 ovs_assert(class->open != NULL);
101 if (class->close || class->recv || class->send
102 || class->run || class->run_wait || class->wait) {
103 ovs_assert(class->close != NULL);
104 ovs_assert(class->recv != NULL);
105 ovs_assert(class->send != NULL);
106 ovs_assert(class->wait != NULL);
108 /* This class delegates to another one. */
112 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
113 const struct pvconn_class *class = pvconn_classes[i];
114 ovs_assert(class->name != NULL);
115 ovs_assert(class->listen != NULL);
116 if (class->close || class->accept || class->wait) {
117 ovs_assert(class->close != NULL);
118 ovs_assert(class->accept != NULL);
119 ovs_assert(class->wait != NULL);
121 /* This class delegates to another one. */
127 /* Prints information on active (if 'active') and passive (if 'passive')
128 * connection methods supported by the vconn. If 'bootstrap' is true, also
129 * advertises options to bootstrap the CA certificate. */
131 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
133 /* Really this should be implemented via callbacks into the vconn
134 * providers, but that seems too heavy-weight to bother with at the
139 printf("Active OpenFlow connection methods:\n");
140 printf(" tcp:IP[:PORT] "
141 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
143 printf(" ssl:IP[:PORT] "
144 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
146 printf(" unix:FILE Unix domain socket named FILE\n");
150 printf("Passive OpenFlow connection methods:\n");
151 printf(" ptcp:[PORT][:IP] "
152 "listen to TCP PORT (default: %d) on IP\n",
155 printf(" pssl:[PORT][:IP] "
156 "listen for SSL on PORT (default: %d) on IP\n",
159 printf(" punix:FILE "
160 "listen on Unix domain socket FILE\n");
164 printf("PKI configuration (required to use SSL):\n"
165 " -p, --private-key=FILE file with private key\n"
166 " -c, --certificate=FILE file with certificate for private key\n"
167 " -C, --ca-cert=FILE file with peer CA certificate\n");
169 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
170 "to read or create\n");
175 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
176 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
177 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
180 vconn_lookup_class(const char *name, const struct vconn_class **classp)
184 prefix_len = strcspn(name, ":");
185 if (name[prefix_len] != '\0') {
188 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
189 const struct vconn_class *class = vconn_classes[i];
190 if (strlen(class->name) == prefix_len
191 && !memcmp(class->name, name, prefix_len)) {
202 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
203 * a supported connection type, otherwise EAFNOSUPPORT. */
205 vconn_verify_name(const char *name)
207 const struct vconn_class *class;
208 return vconn_lookup_class(name, &class);
211 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
212 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
213 * are vconn class-specific.
215 * The vconn will automatically negotiate an OpenFlow protocol version
216 * acceptable to both peers on the connection. The version negotiated will be
217 * one of those in the 'allowed_versions' bitmap: version 'x' is allowed if
218 * allowed_versions & (1 << x) is nonzero. If 'allowed_versions' is zero, then
219 * OFPUTIL_DEFAULT_VERSIONS are allowed.
221 * Returns 0 if successful, otherwise a positive errno value. If successful,
222 * stores a pointer to the new connection in '*vconnp', otherwise a null
225 vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
226 struct vconn **vconnp)
228 const struct vconn_class *class;
233 COVERAGE_INC(vconn_open);
234 check_vconn_classes();
236 if (!allowed_versions) {
237 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
240 /* Look up the class. */
241 error = vconn_lookup_class(name, &class);
246 /* Call class's "open" function. */
247 suffix_copy = xstrdup(strchr(name, ':') + 1);
248 error = class->open(name, allowed_versions, suffix_copy, &vconn, dscp);
255 ovs_assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
264 /* Allows 'vconn' to perform maintenance activities, such as flushing output
267 vconn_run(struct vconn *vconn)
269 if (vconn->state == VCS_CONNECTING ||
270 vconn->state == VCS_SEND_HELLO ||
271 vconn->state == VCS_RECV_HELLO) {
272 vconn_connect(vconn);
275 if (vconn->class->run) {
276 (vconn->class->run)(vconn);
280 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
281 * maintenance activities. */
283 vconn_run_wait(struct vconn *vconn)
285 if (vconn->state == VCS_CONNECTING ||
286 vconn->state == VCS_SEND_HELLO ||
287 vconn->state == VCS_RECV_HELLO) {
288 vconn_connect_wait(vconn);
291 if (vconn->class->run_wait) {
292 (vconn->class->run_wait)(vconn);
297 vconn_open_block(const char *name, uint32_t allowed_versions, uint8_t dscp,
298 struct vconn **vconnp)
305 error = vconn_open(name, allowed_versions, dscp, &vconn);
307 error = vconn_connect_block(vconn);
319 /* Closes 'vconn'. */
321 vconn_close(struct vconn *vconn)
324 char *name = vconn->name;
325 (vconn->class->close)(vconn);
330 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
332 vconn_get_name(const struct vconn *vconn)
337 /* Returns the allowed_versions of 'vconn', that is,
338 * the allowed_versions passed to vconn_open(). */
340 vconn_get_allowed_versions(const struct vconn *vconn)
342 return vconn->allowed_versions;
345 /* Sets the allowed_versions of 'vconn', overriding
346 * the allowed_versions passed to vconn_open(). */
348 vconn_set_allowed_versions(struct vconn *vconn, uint32_t allowed_versions)
350 vconn->allowed_versions = allowed_versions;
353 /* Returns the IP address of the peer, or 0 if the peer is not connected over
354 * an IP-based protocol or if its IP address is not yet known. */
356 vconn_get_remote_ip(const struct vconn *vconn)
358 return vconn->remote_ip;
361 /* Returns the transport port of the peer, or 0 if the connection does not
362 * contain a port or if the port is not yet known. */
364 vconn_get_remote_port(const struct vconn *vconn)
366 return vconn->remote_port;
369 /* Returns the IP address used to connect to the peer, or 0 if the
370 * connection is not an IP-based protocol or if its IP address is not
373 vconn_get_local_ip(const struct vconn *vconn)
375 return vconn->local_ip;
378 /* Returns the transport port used to connect to the peer, or 0 if the
379 * connection does not contain a port or if the port is not yet known. */
381 vconn_get_local_port(const struct vconn *vconn)
383 return vconn->local_port;
386 /* Returns the OpenFlow version negotiated with the peer, or -1 if version
387 * negotiation is not yet complete.
389 * A vconn that has successfully connected (that is, vconn_connect() or
390 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
392 vconn_get_version(const struct vconn *vconn)
394 return vconn->version ? vconn->version : -1;
397 /* By default, a vconn accepts only OpenFlow messages whose version matches the
398 * one negotiated for the connection. A message received with a different
399 * version is an error that causes the vconn to drop the connection.
401 * This functions allows 'vconn' to accept messages with any OpenFlow version.
402 * This is useful in the special case where 'vconn' is used as an rconn
403 * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is
404 * used as a target for mirroring OpenFlow messages for debugging and
407 * This function should be called after a successful vconn_open() or
408 * pvconn_accept() but before the connection completes, that is, before
409 * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn'
410 * beforehand with an unexpected version will the vconn to drop the
413 vconn_set_recv_any_version(struct vconn *vconn)
415 vconn->recv_any_version = true;
419 vcs_connecting(struct vconn *vconn)
421 int retval = (vconn->class->connect)(vconn);
422 ovs_assert(retval != EINPROGRESS);
424 vconn->state = VCS_SEND_HELLO;
425 } else if (retval != EAGAIN) {
426 vconn->state = VCS_DISCONNECTED;
427 vconn->error = retval;
432 vcs_send_hello(struct vconn *vconn)
437 b = ofputil_encode_hello(vconn->allowed_versions);
438 retval = do_send(vconn, b);
440 vconn->state = VCS_RECV_HELLO;
443 if (retval != EAGAIN) {
444 vconn->state = VCS_DISCONNECTED;
445 vconn->error = retval;
451 version_bitmap_to_string(uint32_t bitmap)
457 ds_put_cstr(&s, "no versions");
458 } else if (is_pow2(bitmap)) {
459 ds_put_cstr(&s, "version ");
460 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
461 } else if (is_pow2((bitmap >> 1) + 1)) {
462 ds_put_cstr(&s, "version ");
463 ofputil_format_version(&s, leftmost_1bit_idx(bitmap));
464 ds_put_cstr(&s, " and earlier");
466 ds_put_cstr(&s, "versions ");
467 ofputil_format_version_bitmap(&s, bitmap);
469 return ds_steal_cstr(&s);
473 vcs_recv_hello(struct vconn *vconn)
478 retval = do_recv(vconn, &b);
483 error = ofptype_decode(&type, b->data);
484 if (!error && type == OFPTYPE_HELLO) {
485 char *peer_s, *local_s;
486 uint32_t common_versions;
488 if (!ofputil_decode_hello(b->data, &vconn->peer_versions)) {
489 struct ds msg = DS_EMPTY_INITIALIZER;
490 ds_put_format(&msg, "%s: unknown data in hello:\n",
492 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
493 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
497 local_s = version_bitmap_to_string(vconn->allowed_versions);
498 peer_s = version_bitmap_to_string(vconn->peer_versions);
500 common_versions = vconn->peer_versions & vconn->allowed_versions;
501 if (!common_versions) {
502 vconn->version = leftmost_1bit_idx(vconn->peer_versions);
503 VLOG_WARN_RL(&bad_ofmsg_rl,
504 "%s: version negotiation failed (we support "
505 "%s, peer supports %s)",
506 vconn->name, local_s, peer_s);
507 vconn->state = VCS_SEND_ERROR;
509 vconn->version = leftmost_1bit_idx(common_versions);
510 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
511 "(we support %s, peer supports %s)", vconn->name,
512 vconn->version, local_s, peer_s);
513 vconn->state = VCS_CONNECTED;
522 char *s = ofp_to_string(b->data, b->size, 1);
523 VLOG_WARN_RL(&bad_ofmsg_rl,
524 "%s: received message while expecting hello: %s",
532 if (retval != EAGAIN) {
533 vconn->state = VCS_DISCONNECTED;
534 vconn->error = retval == EOF ? ECONNRESET : retval;
539 vcs_send_error(struct vconn *vconn)
544 char *local_s, *peer_s;
546 local_s = version_bitmap_to_string(vconn->allowed_versions);
547 peer_s = version_bitmap_to_string(vconn->peer_versions);
548 snprintf(s, sizeof s, "We support %s, you support %s, no common versions.",
553 b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE, vconn->version, s);
554 retval = do_send(vconn, b);
558 if (retval != EAGAIN) {
559 vconn->state = VCS_DISCONNECTED;
560 vconn->error = retval ? retval : EPROTO;
564 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
565 * complete, returns 0 if the connection was successful or a positive errno
566 * value if it failed. If the connection is still in progress, returns
569 vconn_connect(struct vconn *vconn)
571 enum vconn_state last_state;
574 last_state = vconn->state;
575 switch (vconn->state) {
577 vcs_connecting(vconn);
581 vcs_send_hello(vconn);
585 vcs_recv_hello(vconn);
592 vcs_send_error(vconn);
595 case VCS_DISCONNECTED:
601 } while (vconn->state != last_state);
606 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
607 * the received message into '*msgp' and returns 0. The caller is responsible
608 * for destroying the message with ofpbuf_delete(). On failure, returns a
609 * positive errno value and stores a null pointer into '*msgp'. On normal
610 * connection close, returns EOF.
612 * vconn_recv will not block waiting for a packet to arrive. If no packets
613 * have been received, it returns EAGAIN immediately. */
615 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
620 retval = vconn_connect(vconn);
622 retval = do_recv(vconn, &msg);
624 if (!retval && !vconn->recv_any_version) {
625 const struct ofp_header *oh = msg->data;
626 if (oh->version != vconn->version) {
629 if (ofptype_decode(&type, msg->data)
630 || (type != OFPTYPE_HELLO &&
631 type != OFPTYPE_ERROR &&
632 type != OFPTYPE_ECHO_REQUEST &&
633 type != OFPTYPE_ECHO_REPLY)) {
634 VLOG_ERR_RL(&bad_ofmsg_rl, "%s: received OpenFlow version "
635 "0x%02"PRIx8" != expected %02x",
636 vconn->name, oh->version, vconn->version);
643 *msgp = retval ? NULL : msg;
648 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
650 int retval = (vconn->class->recv)(vconn, msgp);
652 COVERAGE_INC(vconn_received);
653 if (VLOG_IS_DBG_ENABLED()) {
654 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
655 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
662 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
663 * in which case ownership of 'msg' is transferred to the vconn. Success does
664 * not guarantee that 'msg' has been or ever will be delivered to the peer,
665 * only that it has been queued for transmission.
667 * Returns a positive errno value on failure, in which case the caller
668 * retains ownership of 'msg'.
670 * vconn_send will not block. If 'msg' cannot be immediately accepted for
671 * transmission, it returns EAGAIN immediately. */
673 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
675 int retval = vconn_connect(vconn);
677 retval = do_send(vconn, msg);
683 do_send(struct vconn *vconn, struct ofpbuf *msg)
687 ovs_assert(msg->size >= sizeof(struct ofp_header));
689 ofpmsg_update_length(msg);
690 if (!VLOG_IS_DBG_ENABLED()) {
691 COVERAGE_INC(vconn_sent);
692 retval = (vconn->class->send)(vconn, msg);
694 char *s = ofp_to_string(msg->data, msg->size, 1);
695 retval = (vconn->class->send)(vconn, msg);
696 if (retval != EAGAIN) {
697 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
698 vconn->name, ovs_strerror(retval), s);
705 /* Same as vconn_connect(), except that it waits until the connection on
706 * 'vconn' completes or fails. Thus, it will never return EAGAIN. */
708 vconn_connect_block(struct vconn *vconn)
712 while ((error = vconn_connect(vconn)) == EAGAIN) {
714 vconn_run_wait(vconn);
715 vconn_connect_wait(vconn);
718 ovs_assert(error != EINPROGRESS);
723 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
725 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
731 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
733 vconn_run_wait(vconn);
734 vconn_send_wait(vconn);
740 /* Same as vconn_recv, except that it waits until a message is received. */
742 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
748 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
750 vconn_run_wait(vconn);
751 vconn_recv_wait(vconn);
757 /* Waits until a message with a transaction ID matching 'xid' is received on
758 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
759 * '*replyp' for the caller to examine and free. Otherwise returns a positive
760 * errno value, or EOF, and sets '*replyp' to null.
762 * 'request' is always destroyed, regardless of the return value. */
764 vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp)
768 struct ofpbuf *reply;
771 error = vconn_recv_block(vconn, &reply);
776 recv_xid = ((struct ofp_header *) reply->data)->xid;
777 if (xid == recv_xid) {
782 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
783 " != expected %08"PRIx32,
784 vconn->name, ntohl(recv_xid), ntohl(xid));
785 ofpbuf_delete(reply);
789 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
790 * matching transaction ID. Returns 0 if successful, in which case the reply
791 * is stored in '*replyp' for the caller to examine and free. Otherwise
792 * returns a positive errno value, or EOF, and sets '*replyp' to null.
794 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
795 * if there is no reply, this function can end up blocking forever (or until
796 * the peer drops the connection).
798 * 'request' is always destroyed, regardless of the return value. */
800 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
801 struct ofpbuf **replyp)
803 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
807 error = vconn_send_block(vconn, request);
809 ofpbuf_delete(request);
811 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
814 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
815 * it receives a reply to the barrier. If successful, stores the reply to
816 * 'request' in '*replyp', if one was received, and otherwise NULL, then
817 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
820 * This function is useful for sending an OpenFlow request that doesn't
821 * ordinarily include a reply but might report an error in special
824 * 'request' is always destroyed, regardless of the return value. */
826 vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request,
827 struct ofpbuf **replyp)
829 ovs_be32 request_xid;
830 ovs_be32 barrier_xid;
831 struct ofpbuf *barrier;
837 request_xid = ((struct ofp_header *) request->data)->xid;
838 error = vconn_send_block(vconn, request);
840 ofpbuf_delete(request);
845 barrier = ofputil_encode_barrier_request(vconn_get_version(vconn));
846 barrier_xid = ((struct ofp_header *) barrier->data)->xid;
847 error = vconn_send_block(vconn, barrier);
849 ofpbuf_delete(barrier);
858 error = vconn_recv_block(vconn, &msg);
860 ofpbuf_delete(*replyp);
865 msg_xid = ((struct ofp_header *) msg->data)->xid;
866 if (msg_xid == request_xid) {
868 VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with "
869 "xid %08"PRIx32, vconn->name, ntohl(msg_xid));
870 ofpbuf_delete(*replyp);
875 if (msg_xid == barrier_xid) {
878 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32
879 " != expected %08"PRIx32" or %08"PRIx32,
880 vconn->name, ntohl(msg_xid),
881 ntohl(request_xid), ntohl(barrier_xid));
887 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
888 * All of the requests on 'requests' are always destroyed, regardless of the
891 vconn_transact_multiple_noreply(struct vconn *vconn, struct list *requests,
892 struct ofpbuf **replyp)
894 struct ofpbuf *request, *next;
896 LIST_FOR_EACH_SAFE (request, next, list_node, requests) {
899 list_remove(&request->list_node);
901 error = vconn_transact_noreply(vconn, request, replyp);
902 if (error || *replyp) {
903 ofpbuf_list_delete(requests);
913 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
915 ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
917 switch (vconn->state) {
934 case VCS_DISCONNECTED:
935 poll_immediate_wake();
938 (vconn->class->wait)(vconn, wait);
942 vconn_connect_wait(struct vconn *vconn)
944 vconn_wait(vconn, WAIT_CONNECT);
948 vconn_recv_wait(struct vconn *vconn)
950 vconn_wait(vconn, WAIT_RECV);
954 vconn_send_wait(struct vconn *vconn)
956 vconn_wait(vconn, WAIT_SEND);
959 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
960 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
961 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
964 pvconn_lookup_class(const char *name, const struct pvconn_class **classp)
968 prefix_len = strcspn(name, ":");
969 if (name[prefix_len] != '\0') {
972 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
973 const struct pvconn_class *class = pvconn_classes[i];
974 if (strlen(class->name) == prefix_len
975 && !memcmp(class->name, name, prefix_len)) {
986 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
987 * a supported connection type, otherwise EAFNOSUPPORT. */
989 pvconn_verify_name(const char *name)
991 const struct pvconn_class *class;
992 return pvconn_lookup_class(name, &class);
995 /* Attempts to start listening for OpenFlow connections. 'name' is a
996 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
997 * class's name and ARGS are vconn class-specific.
999 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1000 * protocol version acceptable to both peers on the connection. The version
1001 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1002 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1003 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1005 * Returns 0 if successful, otherwise a positive errno value. If successful,
1006 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1009 pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp,
1010 struct pvconn **pvconnp)
1012 const struct pvconn_class *class;
1013 struct pvconn *pvconn;
1017 check_vconn_classes();
1019 if (!allowed_versions) {
1020 allowed_versions = OFPUTIL_DEFAULT_VERSIONS;
1023 /* Look up the class. */
1024 error = pvconn_lookup_class(name, &class);
1029 /* Call class's "open" function. */
1030 suffix_copy = xstrdup(strchr(name, ':') + 1);
1031 error = class->listen(name, allowed_versions, suffix_copy, &pvconn, dscp);
1046 /* Returns the name that was used to open 'pvconn'. The caller must not
1047 * modify or free the name. */
1049 pvconn_get_name(const struct pvconn *pvconn)
1051 return pvconn->name;
1054 /* Closes 'pvconn'. */
1056 pvconn_close(struct pvconn *pvconn)
1058 if (pvconn != NULL) {
1059 char *name = pvconn->name;
1060 (pvconn->class->close)(pvconn);
1065 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1066 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1069 * The new vconn will automatically negotiate an OpenFlow protocol version
1070 * acceptable to both peers on the connection. The version negotiated will be
1071 * no lower than 'min_version' and no higher than 'max_version'.
1073 * pvconn_accept() will not block waiting for a connection. If no connection
1074 * is ready to be accepted, it returns EAGAIN immediately. */
1076 pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn)
1078 int retval = (pvconn->class->accept)(pvconn, new_vconn);
1082 ovs_assert((*new_vconn)->state != VCS_CONNECTING
1083 || (*new_vconn)->class->connect);
1089 pvconn_wait(struct pvconn *pvconn)
1091 (pvconn->class->wait)(pvconn);
1094 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1095 * The initial connection status, supplied as 'connect_status', is interpreted
1098 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1099 * called in the normal fashion.
1101 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1102 * function should be called to complete the connection.
1104 * - Other positive errno values indicate that the connection failed with
1105 * the specified error.
1107 * After calling this function, vconn_close() must be used to destroy 'vconn',
1108 * otherwise resources will be leaked.
1110 * The caller retains ownership of 'name'. */
1112 vconn_init(struct vconn *vconn, const struct vconn_class *class,
1113 int connect_status, const char *name, uint32_t allowed_versions)
1115 memset(vconn, 0, sizeof *vconn);
1116 vconn->class = class;
1117 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1118 : !connect_status ? VCS_SEND_HELLO
1119 : VCS_DISCONNECTED);
1120 vconn->error = connect_status;
1121 vconn->allowed_versions = allowed_versions;
1122 vconn->name = xstrdup(name);
1123 ovs_assert(vconn->state != VCS_CONNECTING || class->connect);
1127 vconn_set_remote_ip(struct vconn *vconn, ovs_be32 ip)
1129 vconn->remote_ip = ip;
1133 vconn_set_remote_port(struct vconn *vconn, ovs_be16 port)
1135 vconn->remote_port = port;
1139 vconn_set_local_ip(struct vconn *vconn, ovs_be32 ip)
1141 vconn->local_ip = ip;
1145 vconn_set_local_port(struct vconn *vconn, ovs_be16 port)
1147 vconn->local_port = port;
1151 pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class,
1152 const char *name, uint32_t allowed_versions)
1154 pvconn->class = class;
1155 pvconn->name = xstrdup(name);
1156 pvconn->allowed_versions = allowed_versions;