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 <sys/types.h>
22 #include <netinet/in.h>
27 #include "fatal-signal.h"
29 #include "openflow/nicira-ext.h"
30 #include "openflow/openflow.h"
31 #include "openvswitch/dynamic-string.h"
32 #include "openvswitch/ofp-bundle.h"
33 #include "openvswitch/ofp-errors.h"
34 #include "openvswitch/ofp-msgs.h"
35 #include "openvswitch/ofp-print.h"
36 #include "openvswitch/ofp-util.h"
37 #include "openvswitch/ofpbuf.h"
38 #include "openvswitch/vlog.h"
40 #include "openvswitch/poll-loop.h"
43 #include "socket-util.h"
45 VLOG_DEFINE_THIS_MODULE(vconn
);
47 COVERAGE_DEFINE(vconn_open
);
48 COVERAGE_DEFINE(vconn_received
);
49 COVERAGE_DEFINE(vconn_sent
);
51 /* State of an active vconn.*/
53 /* This is the ordinary progression of states. */
54 VCS_CONNECTING
, /* Underlying vconn is not connected. */
55 VCS_SEND_HELLO
, /* Waiting to send OFPT_HELLO message. */
56 VCS_RECV_HELLO
, /* Waiting to receive OFPT_HELLO message. */
57 VCS_CONNECTED
, /* Connection established. */
59 /* These states are entered only when something goes wrong. */
60 VCS_SEND_ERROR
, /* Sending OFPT_ERROR message. */
61 VCS_DISCONNECTED
/* Connection failed or connection closed. */
64 static const struct vconn_class
*vconn_classes
[] = {
72 static const struct pvconn_class
*pvconn_classes
[] = {
80 /* Rate limit for individual OpenFlow messages going over the vconn, output at
81 * DBG level. This is very high because, if these are enabled, it is because
82 * we really need to see them. */
83 static struct vlog_rate_limit ofmsg_rl
= VLOG_RATE_LIMIT_INIT(600, 600);
85 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
86 * in the peer and so there's not much point in showing a lot of them. */
87 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
89 static int do_recv(struct vconn
*, struct ofpbuf
**);
90 static int do_send(struct vconn
*, struct ofpbuf
*);
92 /* Check the validity of the vconn class structures. */
94 check_vconn_classes(void)
99 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
100 const struct vconn_class
*class = vconn_classes
[i
];
101 ovs_assert(class->name
!= NULL
);
102 ovs_assert(class->open
!= NULL
);
103 if (class->close
|| class->recv
|| class->send
104 || class->run
|| class->run_wait
|| class->wait
) {
105 ovs_assert(class->close
!= NULL
);
106 ovs_assert(class->recv
!= NULL
);
107 ovs_assert(class->send
!= NULL
);
108 ovs_assert(class->wait
!= NULL
);
110 /* This class delegates to another one. */
114 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
115 const struct pvconn_class
*class = pvconn_classes
[i
];
116 ovs_assert(class->name
!= NULL
);
117 ovs_assert(class->listen
!= NULL
);
118 if (class->close
|| class->accept
|| class->wait
) {
119 ovs_assert(class->close
!= NULL
);
120 ovs_assert(class->accept
!= NULL
);
121 ovs_assert(class->wait
!= NULL
);
123 /* This class delegates to another one. */
129 /* Prints information on active (if 'active') and passive (if 'passive')
130 * connection methods supported by the vconn. If 'bootstrap' is true, also
131 * advertises options to bootstrap the CA certificate. */
133 vconn_usage(bool active
, bool passive
, bool bootstrap OVS_UNUSED
)
135 /* Really this should be implemented via callbacks into the vconn
136 * providers, but that seems too heavy-weight to bother with at the
141 printf("Active OpenFlow connection methods:\n");
142 printf(" tcp:HOST[:PORT] "
143 "PORT (default: %d) at remote HOST\n", OFP_PORT
);
145 printf(" ssl:HOST[:PORT] "
146 "SSL PORT (default: %d) at remote HOST\n", OFP_PORT
);
148 printf(" unix:FILE Unix domain socket named FILE\n");
152 printf("Passive OpenFlow connection methods:\n");
153 printf(" ptcp:[PORT][:IP] "
154 "listen to TCP PORT (default: %d) on IP\n",
157 printf(" pssl:[PORT][:IP] "
158 "listen for SSL on PORT (default: %d) on IP\n",
161 printf(" punix:FILE "
162 "listen on Unix domain socket FILE\n");
166 printf("PKI configuration (required to use SSL):\n"
167 " -p, --private-key=FILE file with private key\n"
168 " -c, --certificate=FILE file with certificate for private key\n"
169 " -C, --ca-cert=FILE file with peer CA certificate\n");
171 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
172 "to read or create\n");
177 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
178 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
179 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
182 vconn_lookup_class(const char *name
, const struct vconn_class
**classp
)
186 prefix_len
= strcspn(name
, ":");
187 if (name
[prefix_len
] != '\0') {
190 for (i
= 0; i
< ARRAY_SIZE(vconn_classes
); i
++) {
191 const struct vconn_class
*class = vconn_classes
[i
];
192 if (strlen(class->name
) == prefix_len
193 && !memcmp(class->name
, name
, prefix_len
)) {
204 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
205 * a supported connection type, otherwise EAFNOSUPPORT. */
207 vconn_verify_name(const char *name
)
209 const struct vconn_class
*class;
210 return vconn_lookup_class(name
, &class);
213 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
214 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
215 * are vconn class-specific.
217 * The vconn will automatically negotiate an OpenFlow protocol version
218 * acceptable to both peers on the connection. The version negotiated will be
219 * one of those in the 'allowed_versions' bitmap: version 'x' is allowed if
220 * allowed_versions & (1 << x) is nonzero. If 'allowed_versions' is zero, then
221 * OFPUTIL_DEFAULT_VERSIONS are allowed.
223 * Returns 0 if successful, otherwise a positive errno value. If successful,
224 * stores a pointer to the new connection in '*vconnp', otherwise a null
227 vconn_open(const char *name
, uint32_t allowed_versions
, uint8_t dscp
,
228 struct vconn
**vconnp
)
230 const struct vconn_class
*class;
235 COVERAGE_INC(vconn_open
);
236 check_vconn_classes();
238 if (!allowed_versions
) {
239 allowed_versions
= OFPUTIL_DEFAULT_VERSIONS
;
242 /* Look up the class. */
243 error
= vconn_lookup_class(name
, &class);
248 /* Call class's "open" function. */
249 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
250 error
= class->open(name
, allowed_versions
, suffix_copy
, &vconn
, dscp
);
257 ovs_assert(vconn
->state
!= VCS_CONNECTING
|| vconn
->vclass
->connect
);
266 /* Allows 'vconn' to perform maintenance activities, such as flushing output
269 vconn_run(struct vconn
*vconn
)
271 if (vconn
->state
== VCS_CONNECTING
||
272 vconn
->state
== VCS_SEND_HELLO
||
273 vconn
->state
== VCS_RECV_HELLO
) {
274 vconn_connect(vconn
);
277 if (vconn
->vclass
->run
) {
278 (vconn
->vclass
->run
)(vconn
);
282 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
283 * maintenance activities. */
285 vconn_run_wait(struct vconn
*vconn
)
287 if (vconn
->state
== VCS_CONNECTING
||
288 vconn
->state
== VCS_SEND_HELLO
||
289 vconn
->state
== VCS_RECV_HELLO
) {
290 vconn_connect_wait(vconn
);
293 if (vconn
->vclass
->run_wait
) {
294 (vconn
->vclass
->run_wait
)(vconn
);
298 /* Returns 0 if 'vconn' is healthy (connecting or connected), a positive errno
299 * value if the connection died abnormally (connection failed or aborted), or
300 * EOF if the connection was closed in a normal way. */
302 vconn_get_status(const struct vconn
*vconn
)
304 return vconn
->error
== EAGAIN
? 0 : vconn
->error
;
308 vconn_open_block(const char *name
, uint32_t allowed_versions
, uint8_t dscp
,
309 long long int timeout
, struct vconn
**vconnp
)
316 error
= vconn_open(name
, allowed_versions
, dscp
, &vconn
);
318 error
= vconn_connect_block(vconn
, timeout
);
330 /* Closes 'vconn'. */
332 vconn_close(struct vconn
*vconn
)
335 char *name
= vconn
->name
;
336 (vconn
->vclass
->close
)(vconn
);
341 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
343 vconn_get_name(const struct vconn
*vconn
)
348 /* Returns the allowed_versions of 'vconn', that is,
349 * the allowed_versions passed to vconn_open(). */
351 vconn_get_allowed_versions(const struct vconn
*vconn
)
353 return vconn
->allowed_versions
;
356 /* Sets the allowed_versions of 'vconn', overriding
357 * the allowed_versions passed to vconn_open(). */
359 vconn_set_allowed_versions(struct vconn
*vconn
, uint32_t allowed_versions
)
361 vconn
->allowed_versions
= allowed_versions
;
364 /* Returns the OpenFlow version negotiated with the peer, or -1 if version
365 * negotiation is not yet complete.
367 * A vconn that has successfully connected (that is, vconn_connect() or
368 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
370 vconn_get_version(const struct vconn
*vconn
)
372 return vconn
->version
? vconn
->version
: -1;
375 /* By default, a vconn accepts only OpenFlow messages whose version matches the
376 * one negotiated for the connection. A message received with a different
377 * version is an error that causes the vconn to drop the connection.
379 * This functions allows 'vconn' to accept messages with any OpenFlow version.
380 * This is useful in the special case where 'vconn' is used as an rconn
381 * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is
382 * used as a target for mirroring OpenFlow messages for debugging and
385 * This function should be called after a successful vconn_open() or
386 * pvconn_accept() but before the connection completes, that is, before
387 * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn'
388 * beforehand with an unexpected version will the vconn to drop the
391 vconn_set_recv_any_version(struct vconn
*vconn
)
393 vconn
->recv_any_version
= true;
397 vcs_connecting(struct vconn
*vconn
)
399 int retval
= (vconn
->vclass
->connect
)(vconn
);
400 ovs_assert(retval
!= EINPROGRESS
);
402 vconn
->state
= VCS_SEND_HELLO
;
403 } else if (retval
!= EAGAIN
) {
404 vconn
->state
= VCS_DISCONNECTED
;
405 vconn
->error
= retval
;
410 vcs_send_hello(struct vconn
*vconn
)
415 b
= ofputil_encode_hello(vconn
->allowed_versions
);
416 retval
= do_send(vconn
, b
);
418 vconn
->state
= VCS_RECV_HELLO
;
421 if (retval
!= EAGAIN
) {
422 vconn
->state
= VCS_DISCONNECTED
;
423 vconn
->error
= retval
;
429 version_bitmap_to_string(uint32_t bitmap
)
435 ds_put_cstr(&s
, "no versions");
436 } else if (is_pow2(bitmap
)) {
437 ds_put_cstr(&s
, "version ");
438 ofputil_format_version(&s
, leftmost_1bit_idx(bitmap
));
439 } else if (is_pow2((bitmap
>> 1) + 1)) {
440 ds_put_cstr(&s
, "version ");
441 ofputil_format_version(&s
, leftmost_1bit_idx(bitmap
));
442 ds_put_cstr(&s
, " and earlier");
444 ds_put_cstr(&s
, "versions ");
445 ofputil_format_version_bitmap(&s
, bitmap
);
447 return ds_steal_cstr(&s
);
451 vcs_recv_hello(struct vconn
*vconn
)
456 retval
= do_recv(vconn
, &b
);
461 error
= ofptype_decode(&type
, b
->data
);
462 if (!error
&& type
== OFPTYPE_HELLO
) {
463 char *peer_s
, *local_s
;
464 uint32_t common_versions
;
466 if (!ofputil_decode_hello(b
->data
, &vconn
->peer_versions
)) {
467 struct ds msg
= DS_EMPTY_INITIALIZER
;
468 ds_put_format(&msg
, "%s: unknown data in hello:\n",
470 ds_put_hex_dump(&msg
, b
->data
, b
->size
, 0, true);
471 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s", ds_cstr(&msg
));
475 local_s
= version_bitmap_to_string(vconn
->allowed_versions
);
476 peer_s
= version_bitmap_to_string(vconn
->peer_versions
);
478 common_versions
= vconn
->peer_versions
& vconn
->allowed_versions
;
479 if (!common_versions
) {
480 vconn
->version
= leftmost_1bit_idx(vconn
->peer_versions
);
481 VLOG_WARN_RL(&bad_ofmsg_rl
,
482 "%s: version negotiation failed (we support "
483 "%s, peer supports %s)",
484 vconn
->name
, local_s
, peer_s
);
485 vconn
->state
= VCS_SEND_ERROR
;
487 vconn
->version
= leftmost_1bit_idx(common_versions
);
488 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
489 "(we support %s, peer supports %s)", vconn
->name
,
490 vconn
->version
, local_s
, peer_s
);
491 vconn
->state
= VCS_CONNECTED
;
500 char *s
= ofp_to_string(b
->data
, b
->size
, NULL
, NULL
, 1);
501 VLOG_WARN_RL(&bad_ofmsg_rl
,
502 "%s: received message while expecting hello: %s",
510 if (retval
!= EAGAIN
) {
511 vconn
->state
= VCS_DISCONNECTED
;
512 vconn
->error
= retval
== EOF
? ECONNRESET
: retval
;
517 vcs_send_error(struct vconn
*vconn
)
522 char *local_s
, *peer_s
;
524 local_s
= version_bitmap_to_string(vconn
->allowed_versions
);
525 peer_s
= version_bitmap_to_string(vconn
->peer_versions
);
526 snprintf(s
, sizeof s
, "We support %s, you support %s, no common versions.",
531 b
= ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE
, vconn
->version
, s
);
532 retval
= do_send(vconn
, b
);
536 if (retval
!= EAGAIN
) {
537 vconn
->state
= VCS_DISCONNECTED
;
538 vconn
->error
= retval
? retval
: EPROTO
;
542 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
543 * complete, returns 0 if the connection was successful or a positive errno
544 * value if it failed. If the connection is still in progress, returns
547 vconn_connect(struct vconn
*vconn
)
549 enum vconn_state last_state
;
552 last_state
= vconn
->state
;
553 switch (vconn
->state
) {
555 vcs_connecting(vconn
);
559 vcs_send_hello(vconn
);
563 vcs_recv_hello(vconn
);
570 vcs_send_error(vconn
);
573 case VCS_DISCONNECTED
:
574 ovs_assert(vconn
->error
!= 0);
580 } while (vconn
->state
!= last_state
);
585 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
586 * the received message into '*msgp' and returns 0. The caller is responsible
587 * for destroying the message with ofpbuf_delete(). On failure, returns a
588 * positive errno value and stores a null pointer into '*msgp'. On normal
589 * connection close, returns EOF.
591 * vconn_recv will not block waiting for a packet to arrive. If no packets
592 * have been received, it returns EAGAIN immediately. */
594 vconn_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
599 retval
= vconn_connect(vconn
);
601 retval
= do_recv(vconn
, &msg
);
603 if (!retval
&& !vconn
->recv_any_version
) {
604 const struct ofp_header
*oh
= msg
->data
;
605 if (oh
->version
!= vconn
->version
) {
608 if (ofptype_decode(&type
, msg
->data
)
609 || (type
!= OFPTYPE_HELLO
&&
610 type
!= OFPTYPE_ERROR
&&
611 type
!= OFPTYPE_ECHO_REQUEST
&&
612 type
!= OFPTYPE_ECHO_REPLY
)) {
613 struct ofpbuf
*reply
;
615 VLOG_ERR_RL(&bad_ofmsg_rl
, "%s: received OpenFlow version "
616 "0x%02"PRIx8
" != expected %02x",
617 vconn
->name
, oh
->version
, vconn
->version
);
619 /* Send a "bad version" reply, if we can. */
620 reply
= ofperr_encode_reply(OFPERR_OFPBRC_BAD_VERSION
, oh
);
621 retval
= vconn_send(vconn
, reply
);
623 VLOG_INFO_RL(&bad_ofmsg_rl
,
624 "%s: failed to queue error reply (%s)",
625 vconn
->name
, ovs_strerror(retval
));
626 ofpbuf_delete(reply
);
629 /* Suppress the received message, as if it had not arrived. */
636 *msgp
= retval
? NULL
: msg
;
641 do_recv(struct vconn
*vconn
, struct ofpbuf
**msgp
)
643 int retval
= (vconn
->vclass
->recv
)(vconn
, msgp
);
645 COVERAGE_INC(vconn_received
);
646 if (VLOG_IS_DBG_ENABLED()) {
647 char *s
= ofp_to_string((*msgp
)->data
, (*msgp
)->size
,
649 VLOG_DBG_RL(&ofmsg_rl
, "%s: received: %s", vconn
->name
, s
);
656 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
657 * in which case ownership of 'msg' is transferred to the vconn. Success does
658 * not guarantee that 'msg' has been or ever will be delivered to the peer,
659 * only that it has been queued for transmission.
661 * Returns a positive errno value on failure, in which case the caller
662 * retains ownership of 'msg'.
664 * vconn_send will not block. If 'msg' cannot be immediately accepted for
665 * transmission, it returns EAGAIN immediately. */
667 vconn_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
669 int retval
= vconn_connect(vconn
);
671 retval
= do_send(vconn
, msg
);
677 do_send(struct vconn
*vconn
, struct ofpbuf
*msg
)
681 ovs_assert(msg
->size
>= sizeof(struct ofp_header
));
683 ofpmsg_update_length(msg
);
684 if (!VLOG_IS_DBG_ENABLED()) {
685 COVERAGE_INC(vconn_sent
);
686 retval
= (vconn
->vclass
->send
)(vconn
, msg
);
688 char *s
= ofp_to_string(msg
->data
, msg
->size
, NULL
, NULL
, 1);
689 retval
= (vconn
->vclass
->send
)(vconn
, msg
);
690 if (retval
!= EAGAIN
) {
691 VLOG_DBG_RL(&ofmsg_rl
, "%s: sent (%s): %s",
692 vconn
->name
, ovs_strerror(retval
), s
);
699 /* Same as vconn_connect(), except that it waits until the connection on
700 * 'vconn' completes or fails, but no more than 'timeout' milliseconds.
701 * Thus, it will never return EAGAIN. Negative value of 'timeout' means
702 * infinite waiting.*/
704 vconn_connect_block(struct vconn
*vconn
, long long int timeout
)
706 long long int deadline
= (timeout
>= 0
707 ? time_msec() + timeout
711 while ((error
= vconn_connect(vconn
)) == EAGAIN
) {
712 if (time_msec() > deadline
) {
717 vconn_run_wait(vconn
);
718 vconn_connect_wait(vconn
);
719 if (deadline
!= LLONG_MAX
) {
720 poll_timer_wait_until(deadline
);
724 ovs_assert(error
!= EINPROGRESS
);
729 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
731 vconn_send_block(struct vconn
*vconn
, struct ofpbuf
*msg
)
737 while ((retval
= vconn_send(vconn
, msg
)) == EAGAIN
) {
739 vconn_run_wait(vconn
);
740 vconn_send_wait(vconn
);
746 /* Same as vconn_recv, except that it waits until a message is received. */
748 vconn_recv_block(struct vconn
*vconn
, struct ofpbuf
**msgp
)
754 while ((retval
= vconn_recv(vconn
, msgp
)) == EAGAIN
) {
756 vconn_run_wait(vconn
);
757 vconn_recv_wait(vconn
);
764 vconn_recv_xid__(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
,
765 struct ovs_list
*errors
)
769 struct ofpbuf
*reply
;
770 const struct ofp_header
*oh
;
774 error
= vconn_recv_block(vconn
, &reply
);
781 if (xid
== recv_xid
) {
786 error
= ofptype_decode(&type
, oh
);
787 if (!error
&& type
== OFPTYPE_ERROR
&& errors
) {
788 ovs_list_push_back(errors
, &reply
->list_node
);
790 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: received reply with xid %08"PRIx32
791 " != expected %08"PRIx32
,
792 vconn
->name
, ntohl(recv_xid
), ntohl(xid
));
793 ofpbuf_delete(reply
);
798 /* Waits until a message with a transaction ID matching 'xid' is received on
799 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
800 * '*replyp' for the caller to examine and free. Otherwise returns a positive
801 * errno value, or EOF, and sets '*replyp' to null.
803 * 'request' is always destroyed, regardless of the return value. */
805 vconn_recv_xid(struct vconn
*vconn
, ovs_be32 xid
, struct ofpbuf
**replyp
)
807 return vconn_recv_xid__(vconn
, xid
, replyp
, NULL
);
811 vconn_transact__(struct vconn
*vconn
, struct ofpbuf
*request
,
812 struct ofpbuf
**replyp
, struct ovs_list
*errors
)
814 ovs_be32 send_xid
= ((struct ofp_header
*) request
->data
)->xid
;
818 error
= vconn_send_block(vconn
, request
);
820 ofpbuf_delete(request
);
822 return error
? error
: vconn_recv_xid__(vconn
, send_xid
, replyp
, errors
);
825 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
826 * matching transaction ID. Returns 0 if successful, in which case the reply
827 * is stored in '*replyp' for the caller to examine and free. Otherwise
828 * returns a positive errno value, or EOF, and sets '*replyp' to null.
830 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
831 * if there is no reply, this function can end up blocking forever (or until
832 * the peer drops the connection).
834 * 'request' is always destroyed, regardless of the return value. */
836 vconn_transact(struct vconn
*vconn
, struct ofpbuf
*request
,
837 struct ofpbuf
**replyp
)
839 return vconn_transact__(vconn
, request
, replyp
, NULL
);
843 vconn_send_barrier(struct vconn
*vconn
, ovs_be32
*barrier_xid
)
845 struct ofpbuf
*barrier
;
849 barrier
= ofputil_encode_barrier_request(vconn_get_version(vconn
));
850 *barrier_xid
= ((struct ofp_header
*) barrier
->data
)->xid
;
851 error
= vconn_send_block(vconn
, barrier
);
853 ofpbuf_delete(barrier
);
858 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
859 * it receives a reply to the barrier. If successful, stores the reply to
860 * 'request' in '*replyp', if one was received, and otherwise NULL, then
861 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
864 * This function is useful for sending an OpenFlow request that doesn't
865 * ordinarily include a reply but might report an error in special
868 * 'request' is always destroyed, regardless of the return value. */
870 vconn_transact_noreply(struct vconn
*vconn
, struct ofpbuf
*request
,
871 struct ofpbuf
**replyp
)
873 ovs_be32 request_xid
;
874 ovs_be32 barrier_xid
;
880 request_xid
= ((struct ofp_header
*) request
->data
)->xid
;
881 error
= vconn_send_block(vconn
, request
);
883 ofpbuf_delete(request
);
888 error
= vconn_send_barrier(vconn
, &barrier_xid
);
897 error
= vconn_recv_block(vconn
, &msg
);
899 ofpbuf_delete(*replyp
);
904 msg_xid
= ((struct ofp_header
*) msg
->data
)->xid
;
905 if (msg_xid
== request_xid
) {
907 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s: duplicate replies with "
908 "xid %08"PRIx32
, vconn
->name
, ntohl(msg_xid
));
909 ofpbuf_delete(*replyp
);
914 if (msg_xid
== barrier_xid
) {
917 VLOG_DBG_RL(&bad_ofmsg_rl
, "%s: reply with xid %08"PRIx32
918 " != expected %08"PRIx32
" or %08"PRIx32
,
919 vconn
->name
, ntohl(msg_xid
),
920 ntohl(request_xid
), ntohl(barrier_xid
));
926 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
927 * All of the requests on 'requests' are always destroyed, regardless of the
930 vconn_transact_multiple_noreply(struct vconn
*vconn
, struct ovs_list
*requests
,
931 struct ofpbuf
**replyp
)
933 struct ofpbuf
*request
;
935 LIST_FOR_EACH_POP (request
, list_node
, requests
) {
938 error
= vconn_transact_noreply(vconn
, request
, replyp
);
939 if (error
|| *replyp
) {
940 ofpbuf_list_delete(requests
);
949 /* Sends 'requests' (which should be a multipart request) on 'vconn' and waits
950 * for the replies, which are put into 'replies'. Returns 0 if successful,
951 * otherwise an errno value. */
953 vconn_transact_multipart(struct vconn
*vconn
,
954 struct ovs_list
*requests
,
955 struct ovs_list
*replies
)
957 struct ofpbuf
*rq
= ofpbuf_from_list(ovs_list_front(requests
));
958 ovs_be32 send_xid
= ((struct ofp_header
*) rq
->data
)->xid
;
960 ovs_list_init(replies
);
962 /* Send all the requests. */
963 struct ofpbuf
*b
, *next
;
964 LIST_FOR_EACH_SAFE (b
, next
, list_node
, requests
) {
965 ovs_list_remove(&b
->list_node
);
966 int error
= vconn_send_block(vconn
, b
);
972 /* Receive all the replies. */
975 struct ofpbuf
*reply
;
976 int error
= vconn_recv_xid__(vconn
, send_xid
, &reply
, NULL
);
978 ofpbuf_list_delete(replies
);
982 ovs_list_push_back(replies
, &reply
->list_node
);
983 more
= ofpmsg_is_stat_reply(reply
->data
) && ofpmp_more(reply
->data
);
990 recv_flow_stats_reply(struct vconn
*vconn
, ovs_be32 send_xid
,
991 struct ofpbuf
**replyp
,
992 struct ofputil_flow_stats
*fs
, struct ofpbuf
*ofpacts
)
994 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
995 struct ofpbuf
*reply
= *replyp
;
1001 /* Get a flow stats reply message, if we don't already have one. */
1007 error
= vconn_recv_block(vconn
, &reply
);
1011 } while (((struct ofp_header
*) reply
->data
)->xid
!= send_xid
);
1013 error
= ofptype_decode(&type
, reply
->data
);
1014 if (error
|| type
!= OFPTYPE_FLOW_STATS_REPLY
) {
1015 VLOG_WARN_RL(&rl
, "received bad reply: %s",
1016 ofp_to_string(reply
->data
, reply
->size
,
1022 /* Pull an individual flow stats reply out of the message. */
1023 retval
= ofputil_decode_flow_stats_reply(fs
, reply
, false, ofpacts
);
1030 more
= ofpmp_more(reply
->header
);
1031 ofpbuf_delete(reply
);
1040 VLOG_WARN_RL(&rl
, "parse error in reply (%s)",
1041 ofperr_to_string(retval
));
1047 /* Sends 'fsr' to 'vconn', encoding it with the given 'protocol', and then
1048 * waits for, parses, and accumulates all of the replies into '*fsesp' and
1049 * '*n_fsesp'. The caller is responsible for freeing all of the flows.
1050 * Returns 0 if successful, otherwise a positive errno value. */
1052 vconn_dump_flows(struct vconn
*vconn
,
1053 const struct ofputil_flow_stats_request
*fsr
,
1054 enum ofputil_protocol protocol
,
1055 struct ofputil_flow_stats
**fsesp
, size_t *n_fsesp
)
1057 struct ofputil_flow_stats
*fses
= NULL
;
1059 size_t allocated_fses
= 0;
1061 struct ofpbuf
*request
= ofputil_encode_flow_stats_request(fsr
, protocol
);
1062 const struct ofp_header
*oh
= request
->data
;
1063 ovs_be32 send_xid
= oh
->xid
;
1064 int error
= vconn_send_block(vconn
, request
);
1069 struct ofpbuf
*reply
= NULL
;
1070 struct ofpbuf ofpacts
;
1071 ofpbuf_init(&ofpacts
, 0);
1073 if (n_fses
>= allocated_fses
) {
1074 fses
= x2nrealloc(fses
, &allocated_fses
, sizeof *fses
);
1077 struct ofputil_flow_stats
*fs
= &fses
[n_fses
];
1078 error
= recv_flow_stats_reply(vconn
, send_xid
, &reply
, fs
, &ofpacts
);
1085 fs
->ofpacts
= xmemdup(fs
->ofpacts
, fs
->ofpacts_len
);
1088 ofpbuf_uninit(&ofpacts
);
1089 ofpbuf_delete(reply
);
1092 for (size_t i
= 0; i
< n_fses
; i
++) {
1093 free(CONST_CAST(struct ofpact
*, fses
[i
].ofpacts
));
1109 vconn_bundle_reply_validate(struct ofpbuf
*reply
,
1110 struct ofputil_bundle_ctrl_msg
*request
,
1111 struct ovs_list
*errors
)
1113 const struct ofp_header
*oh
;
1116 struct ofputil_bundle_ctrl_msg rbc
;
1119 error
= ofptype_decode(&type
, oh
);
1124 if (type
== OFPTYPE_ERROR
) {
1125 struct ofpbuf
*copy
= ofpbuf_clone(reply
);
1126 ovs_list_push_back(errors
, ©
->list_node
);
1127 return ofperr_decode_msg(oh
, NULL
);
1129 if (type
!= OFPTYPE_BUNDLE_CONTROL
) {
1130 return OFPERR_OFPBRC_BAD_TYPE
;
1133 error
= ofputil_decode_bundle_ctrl(oh
, &rbc
);
1138 if (rbc
.bundle_id
!= request
->bundle_id
) {
1139 return OFPERR_OFPBFC_BAD_ID
;
1142 if (rbc
.type
!= request
->type
+ 1) {
1143 return OFPERR_OFPBFC_BAD_TYPE
;
1149 /* Send bundle control message 'bc' of 'type' via 'vconn', and wait for either
1150 * an error or the corresponding bundle control message response.
1152 * 'errors' is a list to which any OpenFlow errors relating to bundle
1153 * processing are appended. Caller is responsible for releasing the memory of
1154 * each node in the list on return.
1156 * Returns errno value, or 0 when successful. */
1158 vconn_bundle_control_transact(struct vconn
*vconn
,
1159 struct ofputil_bundle_ctrl_msg
*bc
,
1160 uint16_t type
, struct ovs_list
*errors
)
1162 struct ofpbuf
*request
, *reply
;
1167 request
= ofputil_encode_bundle_ctrl_request(vconn
->version
, bc
);
1168 ofpmsg_update_length(request
);
1169 error
= vconn_transact__(vconn
, request
, &reply
, errors
);
1174 ofperr
= vconn_bundle_reply_validate(reply
, bc
, errors
);
1175 ofpbuf_delete(reply
);
1177 return ofperr
? EPROTO
: 0;
1180 /* Checks if error responses can be received on 'vconn'. */
1182 vconn_recv_error(struct vconn
*vconn
, struct ovs_list
*errors
)
1187 struct ofpbuf
*reply
;
1189 error
= vconn_recv(vconn
, &reply
);
1191 const struct ofp_header
*oh
;
1196 ofperr
= ofptype_decode(&type
, oh
);
1197 if (!ofperr
&& type
== OFPTYPE_ERROR
) {
1198 ovs_list_push_back(errors
, &reply
->list_node
);
1200 VLOG_DBG_RL(&bad_ofmsg_rl
,
1201 "%s: received unexpected reply with xid %08"PRIx32
,
1202 vconn
->name
, ntohl(oh
->xid
));
1203 ofpbuf_delete(reply
);
1209 /* Sends a barrier and waits for the barrier response and stores any errors
1210 * that are received before the barrier response. */
1212 vconn_bundle_barrier_transact(struct vconn
*vconn
, struct ovs_list
*errors
)
1214 struct ofpbuf
*reply
;
1215 ovs_be32 barrier_xid
;
1218 error
= vconn_send_barrier(vconn
, &barrier_xid
);
1223 error
= vconn_recv_xid__(vconn
, barrier_xid
, &reply
, errors
);
1227 ofpbuf_delete(reply
);
1232 vconn_bundle_add_msg(struct vconn
*vconn
, struct ofputil_bundle_ctrl_msg
*bc
,
1234 struct ovs_list
*errors
)
1236 struct ofputil_bundle_add_msg bam
;
1237 struct ofpbuf
*request
;
1240 ofpmsg_update_length(msg
);
1242 bam
.bundle_id
= bc
->bundle_id
;
1243 bam
.flags
= bc
->flags
;
1244 bam
.msg
= msg
->data
;
1246 request
= ofputil_encode_bundle_add(vconn
->version
, &bam
);
1248 error
= vconn_send_block(vconn
, request
);
1250 /* Check for an error return, so that the socket buffer does not become
1251 * full of errors. */
1252 vconn_recv_error(vconn
, errors
);
1257 /* Appends ofpbufs for received errors, if any, to 'errors'. The caller must
1258 * free the received errors. */
1260 vconn_bundle_transact(struct vconn
*vconn
, struct ovs_list
*requests
,
1261 uint16_t flags
, struct ovs_list
*errors
)
1263 struct ofputil_bundle_ctrl_msg bc
;
1264 struct ofpbuf
*request
;
1267 ovs_list_init(errors
);
1269 memset(&bc
, 0, sizeof bc
);
1271 error
= vconn_bundle_control_transact(vconn
, &bc
, OFPBCT_OPEN_REQUEST
,
1277 LIST_FOR_EACH (request
, list_node
, requests
) {
1278 error
= vconn_bundle_add_msg(vconn
, &bc
, request
, errors
);
1285 /* A failing message does not invalidate the bundle, but the message is
1286 * simply not added to the bundle. Since we do not want to commit if
1287 * any of the messages failed, we need to explicitly sync with barrier
1288 * before we issue the commit message. */
1289 error
= vconn_bundle_barrier_transact(vconn
, errors
);
1291 if (!error
&& !ovs_list_is_empty(errors
)) {
1295 /* Commit only if no errors are received. */
1297 error
= vconn_bundle_control_transact(vconn
, &bc
,
1298 OFPBCT_COMMIT_REQUEST
,
1301 vconn_bundle_control_transact(vconn
, &bc
, OFPBCT_DISCARD_REQUEST
,
1308 vconn_wait(struct vconn
*vconn
, enum vconn_wait_type wait
)
1310 ovs_assert(wait
== WAIT_CONNECT
|| wait
== WAIT_RECV
|| wait
== WAIT_SEND
);
1312 switch (vconn
->state
) {
1313 case VCS_CONNECTING
:
1314 wait
= WAIT_CONNECT
;
1317 case VCS_SEND_HELLO
:
1318 case VCS_SEND_ERROR
:
1322 case VCS_RECV_HELLO
:
1329 case VCS_DISCONNECTED
:
1330 poll_immediate_wake();
1333 (vconn
->vclass
->wait
)(vconn
, wait
);
1337 vconn_connect_wait(struct vconn
*vconn
)
1339 vconn_wait(vconn
, WAIT_CONNECT
);
1343 vconn_recv_wait(struct vconn
*vconn
)
1345 vconn_wait(vconn
, WAIT_RECV
);
1349 vconn_send_wait(struct vconn
*vconn
)
1351 vconn_wait(vconn
, WAIT_SEND
);
1354 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
1355 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
1356 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
1359 pvconn_lookup_class(const char *name
, const struct pvconn_class
**classp
)
1363 prefix_len
= strcspn(name
, ":");
1364 if (name
[prefix_len
] != '\0') {
1367 for (i
= 0; i
< ARRAY_SIZE(pvconn_classes
); i
++) {
1368 const struct pvconn_class
*class = pvconn_classes
[i
];
1369 if (strlen(class->name
) == prefix_len
1370 && !memcmp(class->name
, name
, prefix_len
)) {
1378 return EAFNOSUPPORT
;
1381 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
1382 * a supported connection type, otherwise EAFNOSUPPORT. */
1384 pvconn_verify_name(const char *name
)
1386 const struct pvconn_class
*class;
1387 return pvconn_lookup_class(name
, &class);
1390 /* Attempts to start listening for OpenFlow connections. 'name' is a
1391 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
1392 * class's name and ARGS are vconn class-specific.
1394 * vconns accepted by the pvconn will automatically negotiate an OpenFlow
1395 * protocol version acceptable to both peers on the connection. The version
1396 * negotiated will be one of those in the 'allowed_versions' bitmap: version
1397 * 'x' is allowed if allowed_versions & (1 << x) is nonzero. If
1398 * 'allowed_versions' is zero, then OFPUTIL_DEFAULT_VERSIONS are allowed.
1400 * Returns 0 if successful, otherwise a positive errno value. If successful,
1401 * stores a pointer to the new connection in '*pvconnp', otherwise a null
1404 pvconn_open(const char *name
, uint32_t allowed_versions
, uint8_t dscp
,
1405 struct pvconn
**pvconnp
)
1407 const struct pvconn_class
*class;
1408 struct pvconn
*pvconn
;
1412 check_vconn_classes();
1414 if (!allowed_versions
) {
1415 allowed_versions
= OFPUTIL_DEFAULT_VERSIONS
;
1418 /* Look up the class. */
1419 error
= pvconn_lookup_class(name
, &class);
1424 /* Call class's "open" function. */
1425 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
1426 error
= class->listen(name
, allowed_versions
, suffix_copy
, &pvconn
, dscp
);
1441 /* Returns the name that was used to open 'pvconn'. The caller must not
1442 * modify or free the name. */
1444 pvconn_get_name(const struct pvconn
*pvconn
)
1446 return pvconn
->name
;
1449 /* Closes 'pvconn'. */
1451 pvconn_close(struct pvconn
*pvconn
)
1453 if (pvconn
!= NULL
) {
1454 char *name
= pvconn
->name
;
1455 (pvconn
->pvclass
->close
)(pvconn
);
1460 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
1461 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
1464 * pvconn_accept() will not block waiting for a connection. If no connection
1465 * is ready to be accepted, it returns EAGAIN immediately. */
1467 pvconn_accept(struct pvconn
*pvconn
, struct vconn
**new_vconn
)
1469 int retval
= (pvconn
->pvclass
->accept
)(pvconn
, new_vconn
);
1473 ovs_assert((*new_vconn
)->state
!= VCS_CONNECTING
1474 || (*new_vconn
)->vclass
->connect
);
1480 pvconn_wait(struct pvconn
*pvconn
)
1482 (pvconn
->pvclass
->wait
)(pvconn
);
1485 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1486 * The initial connection status, supplied as 'connect_status', is interpreted
1489 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1490 * called in the normal fashion.
1492 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1493 * function should be called to complete the connection.
1495 * - Other positive errno values indicate that the connection failed with
1496 * the specified error.
1498 * After calling this function, vconn_close() must be used to destroy 'vconn',
1499 * otherwise resources will be leaked.
1501 * The caller retains ownership of 'name'. */
1503 vconn_init(struct vconn
*vconn
, const struct vconn_class
*class,
1504 int connect_status
, const char *name
, uint32_t allowed_versions
)
1506 memset(vconn
, 0, sizeof *vconn
);
1507 vconn
->vclass
= class;
1508 vconn
->state
= (connect_status
== EAGAIN
? VCS_CONNECTING
1509 : !connect_status
? VCS_SEND_HELLO
1510 : VCS_DISCONNECTED
);
1511 vconn
->error
= connect_status
;
1512 vconn
->allowed_versions
= allowed_versions
;
1513 vconn
->name
= xstrdup(name
);
1514 ovs_assert(vconn
->state
!= VCS_CONNECTING
|| class->connect
);
1518 pvconn_init(struct pvconn
*pvconn
, const struct pvconn_class
*class,
1519 const char *name
, uint32_t allowed_versions
)
1521 pvconn
->pvclass
= class;
1522 pvconn
->name
= xstrdup(name
);
1523 pvconn
->allowed_versions
= allowed_versions
;