2 * Copyright (c) 2008-2017 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 "fatal-signal.h"
28 #include "openflow/nicira-ext.h"
29 #include "openflow/openflow.h"
30 #include "openvswitch/dynamic-string.h"
31 #include "openvswitch/ofp-errors.h"
32 #include "openvswitch/ofp-msgs.h"
33 #include "openvswitch/ofp-print.h"
34 #include "openvswitch/ofp-util.h"
35 #include "openvswitch/ofpbuf.h"
36 #include "openvswitch/vlog.h"
38 #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_PORT
);
143 printf(" ssl:IP[:PORT] "
144 "SSL PORT (default: %d) at remote IP\n", OFP_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
->vclass
->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
->vclass
->run
) {
276 (vconn
->vclass
->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
->vclass
->run_wait
) {
292 (vconn
->vclass
->run_wait
)(vconn
);
296 /* Returns 0 if 'vconn' is healthy (connecting or connected), a positive errno
297 * value if the connection died abnormally (connection failed or aborted), or
298 * EOF if the connection was closed in a normal way. */
300 vconn_get_status(const struct vconn
*vconn
)
302 return vconn
->error
== EAGAIN
? 0 : vconn
->error
;
306 vconn_open_block(const char *name
, uint32_t allowed_versions
, uint8_t dscp
,
307 struct vconn
**vconnp
)
314 error
= vconn_open(name
, allowed_versions
, dscp
, &vconn
);
316 error
= vconn_connect_block(vconn
);
328 /* Closes 'vconn'. */
330 vconn_close(struct vconn
*vconn
)
333 char *name
= vconn
->name
;
334 (vconn
->vclass
->close
)(vconn
);
339 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
341 vconn_get_name(const struct vconn
*vconn
)
346 /* Returns the allowed_versions of 'vconn', that is,
347 * the allowed_versions passed to vconn_open(). */
349 vconn_get_allowed_versions(const struct vconn
*vconn
)
351 return vconn
->allowed_versions
;
354 /* Sets the allowed_versions of 'vconn', overriding
355 * the allowed_versions passed to vconn_open(). */
357 vconn_set_allowed_versions(struct vconn
*vconn
, uint32_t allowed_versions
)
359 vconn
->allowed_versions
= allowed_versions
;
362 /* Returns the OpenFlow version negotiated with the peer, or -1 if version
363 * negotiation is not yet complete.
365 * A vconn that has successfully connected (that is, vconn_connect() or
366 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
368 vconn_get_version(const struct vconn
*vconn
)
370 return vconn
->version
? vconn
->version
: -1;
373 /* By default, a vconn accepts only OpenFlow messages whose version matches the
374 * one negotiated for the connection. A message received with a different
375 * version is an error that causes the vconn to drop the connection.
377 * This functions allows 'vconn' to accept messages with any OpenFlow version.
378 * This is useful in the special case where 'vconn' is used as an rconn
379 * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is
380 * used as a target for mirroring OpenFlow messages for debugging and
383 * This function should be called after a successful vconn_open() or
384 * pvconn_accept() but before the connection completes, that is, before
385 * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn'
386 * beforehand with an unexpected version will the vconn to drop the
389 vconn_set_recv_any_version(struct vconn
*vconn
)
391 vconn
->recv_any_version
= true;
395 vcs_connecting(struct vconn
*vconn
)
397 int retval
= (vconn
->vclass
->connect
)(vconn
);
398 ovs_assert(retval
!= EINPROGRESS
);
400 vconn
->state
= VCS_SEND_HELLO
;
401 } else if (retval
!= EAGAIN
) {
402 vconn
->state
= VCS_DISCONNECTED
;
403 vconn
->error
= retval
;
408 vcs_send_hello(struct vconn
*vconn
)
413 b
= ofputil_encode_hello(vconn
->allowed_versions
);
414 retval
= do_send(vconn
, b
);
416 vconn
->state
= VCS_RECV_HELLO
;
419 if (retval
!= EAGAIN
) {
420 vconn
->state
= VCS_DISCONNECTED
;
421 vconn
->error
= retval
;
427 version_bitmap_to_string(uint32_t bitmap
)
433 ds_put_cstr(&s
, "no versions");
434 } else if (is_pow2(bitmap
)) {
435 ds_put_cstr(&s
, "version ");
436 ofputil_format_version(&s
, leftmost_1bit_idx(bitmap
));
437 } else if (is_pow2((bitmap
>> 1) + 1)) {
438 ds_put_cstr(&s
, "version ");
439 ofputil_format_version(&s
, leftmost_1bit_idx(bitmap
));
440 ds_put_cstr(&s
, " and earlier");
442 ds_put_cstr(&s
, "versions ");
443 ofputil_format_version_bitmap(&s
, bitmap
);
445 return ds_steal_cstr(&s
);
449 vcs_recv_hello(struct vconn
*vconn
)
454 retval
= do_recv(vconn
, &b
);
459 error
= ofptype_decode(&type
, b
->data
);
460 if (!error
&& type
== OFPTYPE_HELLO
) {
461 char *peer_s
, *local_s
;
462 uint32_t common_versions
;
464 if (!ofputil_decode_hello(b
->data
, &vconn
->peer_versions
)) {
465 struct ds msg
= DS_EMPTY_INITIALIZER
;
466 ds_put_format(&msg
, "%s: unknown data in hello:\n",
468 ds_put_hex_dump(&msg
, b
->data
, b
->size
, 0, true);
469 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s", ds_cstr(&msg
));
473 local_s
= version_bitmap_to_string(vconn
->allowed_versions
);
474 peer_s
= version_bitmap_to_string(vconn
->peer_versions
);
476 common_versions
= vconn
->peer_versions
& vconn
->allowed_versions
;
477 if (!common_versions
) {
478 vconn
->version
= leftmost_1bit_idx(vconn
->peer_versions
);
479 VLOG_WARN_RL(&bad_ofmsg_rl
,
480 "%s: version negotiation failed (we support "
481 "%s, peer supports %s)",
482 vconn
->name
, local_s
, peer_s
);
483 vconn
->state
= VCS_SEND_ERROR
;
485 vconn
->version
= leftmost_1bit_idx(common_versions
);
486 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
487 "(we support %s, peer supports %s)", vconn
->name
,
488 vconn
->version
, local_s
, peer_s
);
489 vconn
->state
= VCS_CONNECTED
;
498 char *s
= ofp_to_string(b
->data
, b
->size
, NULL
, 1);
499 VLOG_WARN_RL(&bad_ofmsg_rl
,
500 "%s: received message while expecting hello: %s",
508 if (retval
!= EAGAIN
) {
509 vconn
->state
= VCS_DISCONNECTED
;
510 vconn
->error
= retval
== EOF
? ECONNRESET
: retval
;
515 vcs_send_error(struct vconn
*vconn
)
520 char *local_s
, *peer_s
;
522 local_s
= version_bitmap_to_string(vconn
->allowed_versions
);
523 peer_s
= version_bitmap_to_string(vconn
->peer_versions
);
524 snprintf(s
, sizeof s
, "We support %s, you support %s, no common versions.",
529 b
= ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE
, vconn
->version
, s
);
530 retval
= do_send(vconn
, b
);
534 if (retval
!= EAGAIN
) {
535 vconn
->state
= VCS_DISCONNECTED
;
536 vconn
->error
= retval
? retval
: EPROTO
;
540 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
541 * complete, returns 0 if the connection was successful or a positive errno
542 * value if it failed. If the connection is still in progress, returns
545 vconn_connect(struct vconn
*vconn
)
547 enum vconn_state last_state
;
550 last_state
= vconn
->state
;
551 switch (vconn
->state
) {
553 vcs_connecting(vconn
);
557 vcs_send_hello(vconn
);
561 vcs_recv_hello(vconn
);
568 vcs_send_error(vconn
);
571 case VCS_DISCONNECTED
:
577 } while (vconn
->state
!= last_state
);
582 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
583 * the received message into '*msgp' and returns 0. The caller is responsible
584 * for destroying the message with ofpbuf_delete(). On failure, returns a
585 * positive errno value and stores a null pointer into '*msgp'. On normal
586 * connection close, returns EOF.
588 * vconn_recv will not block waiting for a packet to arrive. If no packets
589 * have been received, it returns EAGAIN immediately. */
591 vconn_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
596 retval
= vconn_connect(vconn
);
598 retval
= do_recv(vconn
, &msg
);
600 if (!retval
&& !vconn
->recv_any_version
) {
601 const struct ofp_header
*oh
= msg
->data
;
602 if (oh
->version
!= vconn
->version
) {
605 if (ofptype_decode(&type
, msg
->data
)
606 || (type
!= OFPTYPE_HELLO
&&
607 type
!= OFPTYPE_ERROR
&&
608 type
!= OFPTYPE_ECHO_REQUEST
&&
609 type
!= OFPTYPE_ECHO_REPLY
)) {
610 struct ofpbuf
*reply
;
612 VLOG_ERR_RL(&bad_ofmsg_rl
, "%s: received OpenFlow version "
613 "0x%02"PRIx8
" != expected %02x",
614 vconn
->name
, oh
->version
, vconn
->version
);
616 /* Send a "bad version" reply, if we can. */
617 reply
= ofperr_encode_reply(OFPERR_OFPBRC_BAD_VERSION
, oh
);
618 retval
= vconn_send(vconn
, reply
);
620 VLOG_INFO_RL(&bad_ofmsg_rl
,
621 "%s: failed to queue error reply (%s)",
622 vconn
->name
, ovs_strerror(retval
));
623 ofpbuf_delete(reply
);
626 /* Suppress the received message, as if it had not arrived. */
633 *msgp
= retval
? NULL
: msg
;
638 do_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
640 int retval
= (vconn
->vclass
->recv
)(vconn
, msgp
);
642 COVERAGE_INC(vconn_received
);
643 if (VLOG_IS_DBG_ENABLED()) {
644 char *s
= ofp_to_string((*msgp
)->data
, (*msgp
)->size
, NULL
, 1);
645 VLOG_DBG_RL(&ofmsg_rl
, "%s: received: %s", vconn
->name
, s
);
652 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
653 * in which case ownership of 'msg' is transferred to the vconn. Success does
654 * not guarantee that 'msg' has been or ever will be delivered to the peer,
655 * only that it has been queued for transmission.
657 * Returns a positive errno value on failure, in which case the caller
658 * retains ownership of 'msg'.
660 * vconn_send will not block. If 'msg' cannot be immediately accepted for
661 * transmission, it returns EAGAIN immediately. */
663 vconn_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
665 int retval
= vconn_connect(vconn
);
667 retval
= do_send(vconn
, msg
);
673 do_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
677 ovs_assert(msg
->size
>= sizeof(struct ofp_header
));
679 ofpmsg_update_length(msg
);
680 if (!VLOG_IS_DBG_ENABLED()) {
681 COVERAGE_INC(vconn_sent
);
682 retval
= (vconn
->vclass
->send
)(vconn
, msg
);
684 char *s
= ofp_to_string(msg
->data
, msg
->size
, NULL
, 1);
685 retval
= (vconn
->vclass
->send
)(vconn
, msg
);
686 if (retval
!= EAGAIN
) {
687 VLOG_DBG_RL(&ofmsg_rl
, "%s: sent (%s): %s",
688 vconn
->name
, ovs_strerror(retval
), s
);
695 /* Same as vconn_connect(), except that it waits until the connection on
696 * 'vconn' completes or fails. Thus, it will never return EAGAIN. */
698 vconn_connect_block(struct vconn
*vconn
)
702 while ((error
= vconn_connect(vconn
)) == EAGAIN
) {
704 vconn_run_wait(vconn
);
705 vconn_connect_wait(vconn
);
708 ovs_assert(error
!= EINPROGRESS
);
713 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
715 vconn_send_block(struct vconn
*vconn
, struct ofpbuf
*msg
)
721 while ((retval
= vconn_send(vconn
, msg
)) == EAGAIN
) {
723 vconn_run_wait(vconn
);
724 vconn_send_wait(vconn
);
730 /* Same as vconn_recv, except that it waits until a message is received. */
732 vconn_recv_block(struct vconn
*vconn
, struct ofpbuf
**msgp
)
738 while ((retval
= vconn_recv(vconn
, msgp
)) == EAGAIN
) {
740 vconn_run_wait(vconn
);
741 vconn_recv_wait(vconn
);
748 vconn_add_bundle_error(const struct ofp_header
*oh
, struct ovs_list
*errors
)
751 struct vconn_bundle_error
*err
= xmalloc(sizeof *err
);
752 size_t len
= ntohs(oh
->length
);
754 memcpy(err
->ofp_msg_data
, oh
, MIN(len
, sizeof err
->ofp_msg_data
));
755 ovs_list_push_back(errors
, &err
->list_node
);
760 vconn_recv_xid__(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
,
761 struct ovs_list
*errors
)
765 struct ofpbuf
*reply
;
766 const struct ofp_header
*oh
;
770 error
= vconn_recv_block(vconn
, &reply
);
777 if (xid
== recv_xid
) {
782 error
= ofptype_decode(&type
, oh
);
783 if (!error
&& type
== OFPTYPE_ERROR
) {
784 vconn_add_bundle_error(oh
, errors
);
786 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: received reply with xid %08"PRIx32
787 " != expected %08"PRIx32
,
788 vconn
->name
, ntohl(recv_xid
), ntohl(xid
));
790 ofpbuf_delete(reply
);
794 /* Waits until a message with a transaction ID matching 'xid' is received on
795 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
796 * '*replyp' for the caller to examine and free. Otherwise returns a positive
797 * errno value, or EOF, and sets '*replyp' to null.
799 * 'request' is always destroyed, regardless of the return value. */
801 vconn_recv_xid(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
)
803 return vconn_recv_xid__(vconn
, xid
, replyp
, NULL
);
807 vconn_transact__(struct vconn
*vconn
, struct ofpbuf
*request
,
808 struct ofpbuf
**replyp
, struct ovs_list
*errors
)
810 ovs_be32 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
814 error
= vconn_send_block(vconn
, request
);
816 ofpbuf_delete(request
);
818 return error
? error
: vconn_recv_xid__(vconn
, send_xid
, replyp
, errors
);
821 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
822 * matching transaction ID. Returns 0 if successful, in which case the reply
823 * is stored in '*replyp' for the caller to examine and free. Otherwise
824 * returns a positive errno value, or EOF, and sets '*replyp' to null.
826 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
827 * if there is no reply, this function can end up blocking forever (or until
828 * the peer drops the connection).
830 * 'request' is always destroyed, regardless of the return value. */
832 vconn_transact(struct vconn
*vconn
, struct ofpbuf
*request
,
833 struct ofpbuf
**replyp
)
835 return vconn_transact__(vconn
, request
, replyp
, NULL
);
839 vconn_send_barrier(struct vconn
*vconn
, ovs_be32
*barrier_xid
)
841 struct ofpbuf
*barrier
;
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
);
854 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
855 * it receives a reply to the barrier. If successful, stores the reply to
856 * 'request' in '*replyp', if one was received, and otherwise NULL, then
857 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
860 * This function is useful for sending an OpenFlow request that doesn't
861 * ordinarily include a reply but might report an error in special
864 * 'request' is always destroyed, regardless of the return value. */
866 vconn_transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
,
867 struct ofpbuf
**replyp
)
869 ovs_be32 request_xid
;
870 ovs_be32 barrier_xid
;
876 request_xid
= ((struct ofp_header
*) request
->data
)->xid
;
877 error
= vconn_send_block(vconn
, request
);
879 ofpbuf_delete(request
);
884 error
= vconn_send_barrier(vconn
, &barrier_xid
);
894 error
= vconn_recv_block(vconn
, &msg
);
896 ofpbuf_delete(*replyp
);
901 msg_xid
= ((struct ofp_header
*) msg
->data
)->xid
;
902 if (msg_xid
== request_xid
) {
904 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s: duplicate replies with "
905 "xid %08"PRIx32
, vconn
->name
, ntohl(msg_xid
));
906 ofpbuf_delete(*replyp
);
911 if (msg_xid
== barrier_xid
) {
914 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: reply with xid %08"PRIx32
915 " != expected %08"PRIx32
" or %08"PRIx32
,
916 vconn
->name
, ntohl(msg_xid
),
917 ntohl(request_xid
), ntohl(barrier_xid
));
923 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
924 * All of the requests on 'requests' are always destroyed, regardless of the
927 vconn_transact_multiple_noreply(struct vconn
*vconn
, struct ovs_list
*requests
,
928 struct ofpbuf
**replyp
)
930 struct ofpbuf
*request
;
932 LIST_FOR_EACH_POP (request
, list_node
, requests
) {
935 error
= vconn_transact_noreply(vconn
, request
, replyp
);
936 if (error
|| *replyp
) {
937 ofpbuf_list_delete(requests
);
947 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
948 struct ofpbuf
**replyp
,
949 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
951 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
952 struct ofpbuf
*reply
= *replyp
;
958 /* Get a flow stats reply message, if we don't already have one. */
964 error
= vconn_recv_block(vconn
, &reply
);
968 } while (((struct ofp_header
*) reply
->data
)->xid
!= send_xid
);
970 error
= ofptype_decode(&type
, reply
->data
);
971 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
972 VLOG_WARN_RL(&rl
, "received bad reply: %s",
973 ofp_to_string(reply
->data
, reply
->size
, NULL
, 1));
978 /* Pull an individual flow stats reply out of the message. */
979 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
986 more
= ofpmp_more(reply
->header
);
987 ofpbuf_delete(reply
);
996 VLOG_WARN_RL(&rl
, "parse error in reply (%s)",
997 ofperr_to_string(retval
));
1003 /* Sends 'fsr' to 'vconn', encoding it with the given 'protocol', and then
1004 * waits for, parses, and accumulates all of the replies into '*fsesp' and
1005 * '*n_fsesp'. The caller is responsible for freeing all of the flows.
1006 * Returns 0 if successful, otherwise a positive errno value. */
1008 vconn_dump_flows(struct vconn
*vconn
,
1009 const struct ofputil_flow_stats_request
*fsr
,
1010 enum ofputil_protocol protocol
,
1011 struct ofputil_flow_stats
**fsesp
, size_t *n_fsesp
)
1013 struct ofputil_flow_stats
*fses
= NULL
;
1015 size_t allocated_fses
= 0;
1017 struct ofpbuf
*request
= ofputil_encode_flow_stats_request(fsr
, protocol
);
1018 const struct ofp_header
*oh
= request
->data
;
1019 ovs_be32 send_xid
= oh
->xid
;
1020 int error
= vconn_send_block(vconn
, request
);
1025 struct ofpbuf
*reply
= NULL
;
1026 struct ofpbuf ofpacts
;
1027 ofpbuf_init(&ofpacts
, 0);
1029 if (n_fses
>= allocated_fses
) {
1030 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
1033 struct ofputil_flow_stats
*fs
= &fses
[n_fses
];
1034 error
= recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
, &ofpacts
);
1041 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
1044 ofpbuf_uninit(&ofpacts
);
1045 ofpbuf_delete(reply
);
1048 for (size_t i
= 0; i
< n_fses
; i
++) {
1049 free(CONST_CAST(struct ofpact
*, fses
[i
].ofpacts
));
1065 vconn_bundle_reply_validate(struct ofpbuf
*reply
,
1066 struct ofputil_bundle_ctrl_msg
*request
,
1067 struct ovs_list
*errors
)
1069 const struct ofp_header
*oh
;
1072 struct ofputil_bundle_ctrl_msg rbc
;
1075 error
= ofptype_decode(&type
, oh
);
1080 if (type
== OFPTYPE_ERROR
) {
1081 vconn_add_bundle_error(oh
, errors
);
1082 return ofperr_decode_msg(oh
, NULL
);
1084 if (type
!= OFPTYPE_BUNDLE_CONTROL
) {
1085 return OFPERR_OFPBRC_BAD_TYPE
;
1088 error
= ofputil_decode_bundle_ctrl(oh
, &rbc
);
1093 if (rbc
.bundle_id
!= request
->bundle_id
) {
1094 return OFPERR_OFPBFC_BAD_ID
;
1097 if (rbc
.type
!= request
->type
+ 1) {
1098 return OFPERR_OFPBFC_BAD_TYPE
;
1104 /* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either
1105 * an error or the corresponding bundle control message response.
1107 * 'errors' is a list to which any OpenFlow errors relating to bundle
1108 * processing are appended. Caller is responsible for releasing the memory of
1109 * each node in the list on return.
1111 * Returns errno value, or 0 when successful. */
1113 vconn_bundle_control_transact(struct vconn
*vconn
,
1114 struct ofputil_bundle_ctrl_msg
*bc
,
1115 uint16_t type
, struct ovs_list
*errors
)
1117 struct ofpbuf
*request
, *reply
;
1122 request
= ofputil_encode_bundle_ctrl_request(vconn
->version
, bc
);
1123 ofpmsg_update_length(request
);
1124 error
= vconn_transact__(vconn
, request
, &reply
, errors
);
1129 ofperr
= vconn_bundle_reply_validate(reply
, bc
, errors
);
1130 ofpbuf_delete(reply
);
1132 return ofperr
? EPROTO
: 0;
1135 /* Checks if error responses can be received on 'vconn'. */
1137 vconn_recv_error(struct vconn
*vconn
, struct ovs_list
*errors
)
1142 struct ofpbuf
*reply
;
1144 error
= vconn_recv(vconn
, &reply
);
1146 const struct ofp_header
*oh
;
1151 ofperr
= ofptype_decode(&type
, oh
);
1152 if (!ofperr
&& type
== OFPTYPE_ERROR
) {
1153 vconn_add_bundle_error(oh
, errors
);
1155 VLOG_DBG_RL(&bad_ofmsg_rl
,
1156 "%s: received unexpected reply with xid %08"PRIx32
,
1157 vconn
->name
, ntohl(oh
->xid
));
1159 ofpbuf_delete(reply
);
1164 /* Sends a barrier and waits for the barrier response and stores any errors
1165 * that are received before the barrier response. */
1167 vconn_bundle_barrier_transact(struct vconn
*vconn
, struct ovs_list
*errors
)
1169 struct ofpbuf
*reply
;
1170 ovs_be32 barrier_xid
;
1173 error
= vconn_send_barrier(vconn
, &barrier_xid
);
1178 error
= vconn_recv_xid__(vconn
, barrier_xid
, &reply
, errors
);
1182 ofpbuf_delete(reply
);
1187 vconn_bundle_add_msg(struct vconn
*vconn
, struct ofputil_bundle_ctrl_msg
*bc
,
1189 struct ovs_list
*errors
)
1191 struct ofputil_bundle_add_msg bam
;
1192 struct ofpbuf
*request
;
1195 ofpmsg_update_length(msg
);
1197 bam
.bundle_id
= bc
->bundle_id
;
1198 bam
.flags
= bc
->flags
;
1199 bam
.msg
= msg
->data
;
1201 request
= ofputil_encode_bundle_add(vconn
->version
, &bam
);
1203 error
= vconn_send_block(vconn
, request
);
1205 /* Check for an error return, so that the socket buffer does not become
1206 * full of errors. */
1207 vconn_recv_error(vconn
, errors
);
1213 vconn_bundle_transact(struct vconn
*vconn
, struct ovs_list
*requests
,
1214 uint16_t flags
, struct ovs_list
*errors
)
1216 struct ofputil_bundle_ctrl_msg bc
;
1217 struct ofpbuf
*request
;
1220 ovs_list_init(errors
);
1222 memset(&bc
, 0, sizeof bc
);
1224 error
= vconn_bundle_control_transact(vconn
, &bc
, OFPBCT_OPEN_REQUEST
,
1230 LIST_FOR_EACH (request
, list_node
, requests
) {
1231 error
= vconn_bundle_add_msg(vconn
, &bc
, request
, errors
);
1238 /* A failing message does not invalidate the bundle, but the message is
1239 * simply not added to the bundle. Since we do not want to commit if
1240 * any of the messages failed, we need to explicitly sync with barrier
1241 * before we issue the commit message. */
1242 error
= vconn_bundle_barrier_transact(vconn
, errors
);
1244 if (!error
&& !ovs_list_is_empty(errors
)) {
1248 /* Commit only if no errors are received. */
1250 error
= vconn_bundle_control_transact(vconn
, &bc
,
1251 OFPBCT_COMMIT_REQUEST
,
1254 vconn_bundle_control_transact(vconn
, &bc
, OFPBCT_DISCARD_REQUEST
,
1261 vconn_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
1263 ovs_assert(wait
== WAIT_CONNECT
|| wait
== WAIT_RECV
|| wait
== WAIT_SEND
);
1265 switch (vconn
->state
) {
1266 case VCS_CONNECTING
:
1267 wait
= WAIT_CONNECT
;
1270 case VCS_SEND_HELLO
:
1271 case VCS_SEND_ERROR
:
1275 case VCS_RECV_HELLO
:
1282 case VCS_DISCONNECTED
:
1283 poll_immediate_wake();
1286 (vconn
->vclass
->wait
)(vconn
, wait
);
1290 vconn_connect_wait(struct vconn
*vconn
)
1292 vconn_wait(vconn
, WAIT_CONNECT
);
1296 vconn_recv_wait(struct vconn
*vconn
)
1298 vconn_wait(vconn
, WAIT_RECV
);
1302 vconn_send_wait(struct vconn
*vconn
)
1304 vconn_wait(vconn
, WAIT_SEND
);
1307 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
1308 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
1309 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
1312 pvconn_lookup_class(const char *name
, const struct pvconn_class
**classp
)
1316 prefix_len
= strcspn(name
, ":");
1317 if (name
[prefix_len
] != '\0') {
1320 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
1321 const struct pvconn_class
*class = pvconn_classes
[i
];
1322 if (strlen(class->name
) == prefix_len
1323 && !memcmp(class->name
, name
, prefix_len
)) {
1331 return EAFNOSUPPORT
;
1334 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
1335 * a supported connection type, otherwise EAFNOSUPPORT. */
1337 pvconn_verify_name(const char *name
)
1339 const struct pvconn_class
*class;
1340 return pvconn_lookup_class(name
, &class);
1343 /* Attempts to start listening for OpenFlow connections. 'name' is a
1344 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
1345 * class's name and ARGS are vconn class-specific.
1347 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1348 * protocol version acceptable to both peers on the connection. The version
1349 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1350 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1351 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1353 * Returns 0 if successful, otherwise a positive errno value. If successful,
1354 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1357 pvconn_open(const char *name
, uint32_t allowed_versions
, uint8_t dscp
,
1358 struct pvconn
**pvconnp
)
1360 const struct pvconn_class
*class;
1361 struct pvconn
*pvconn
;
1365 check_vconn_classes();
1367 if (!allowed_versions
) {
1368 allowed_versions
= OFPUTIL_DEFAULT_VERSIONS
;
1371 /* Look up the class. */
1372 error
= pvconn_lookup_class(name
, &class);
1377 /* Call class's "open" function. */
1378 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
1379 error
= class->listen(name
, allowed_versions
, suffix_copy
, &pvconn
, dscp
);
1394 /* Returns the name that was used to open 'pvconn'. The caller must not
1395 * modify or free the name. */
1397 pvconn_get_name(const struct pvconn
*pvconn
)
1399 return pvconn
->name
;
1402 /* Closes 'pvconn'. */
1404 pvconn_close(struct pvconn
*pvconn
)
1406 if (pvconn
!= NULL
) {
1407 char *name
= pvconn
->name
;
1408 (pvconn
->pvclass
->close
)(pvconn
);
1413 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1414 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1417 * The new vconn will automatically negotiate an OpenFlow protocol version
1418 * acceptable to both peers on the connection. The version negotiated will be
1419 * no lower than 'min_version' and no higher than 'max_version'.
1421 * pvconn_accept() will not block waiting for a connection. If no connection
1422 * is ready to be accepted, it returns EAGAIN immediately. */
1424 pvconn_accept(struct pvconn
*pvconn
, struct vconn
**new_vconn
)
1426 int retval
= (pvconn
->pvclass
->accept
)(pvconn
, new_vconn
);
1430 ovs_assert((*new_vconn
)->state
!= VCS_CONNECTING
1431 || (*new_vconn
)->vclass
->connect
);
1437 pvconn_wait(struct pvconn
*pvconn
)
1439 (pvconn
->pvclass
->wait
)(pvconn
);
1442 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1443 * The initial connection status, supplied as 'connect_status', is interpreted
1446 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1447 * called in the normal fashion.
1449 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1450 * function should be called to complete the connection.
1452 * - Other positive errno values indicate that the connection failed with
1453 * the specified error.
1455 * After calling this function, vconn_close() must be used to destroy 'vconn',
1456 * otherwise resources will be leaked.
1458 * The caller retains ownership of 'name'. */
1460 vconn_init(struct vconn
*vconn
, const struct vconn_class
*class,
1461 int connect_status
, const char *name
, uint32_t allowed_versions
)
1463 memset(vconn
, 0, sizeof *vconn
);
1464 vconn
->vclass
= class;
1465 vconn
->state
= (connect_status
== EAGAIN
? VCS_CONNECTING
1466 : !connect_status
? VCS_SEND_HELLO
1467 : VCS_DISCONNECTED
);
1468 vconn
->error
= connect_status
;
1469 vconn
->allowed_versions
= allowed_versions
;
1470 vconn
->name
= xstrdup(name
);
1471 ovs_assert(vconn
->state
!= VCS_CONNECTING
|| class->connect
);
1475 pvconn_init(struct pvconn
*pvconn
, const struct pvconn_class
*class,
1476 const char *name
, uint32_t allowed_versions
)
1478 pvconn
->pvclass
= class;
1479 pvconn
->name
= xstrdup(name
);
1480 pvconn
->allowed_versions
= allowed_versions
;