2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "vconn-provider.h"
22 #include <netinet/in.h>
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
30 #include "ofp-print.h"
33 #include "openflow/nicira-ext.h"
34 #include "openflow/openflow.h"
36 #include "poll-loop.h"
40 #define THIS_MODULE VLM_vconn
43 /* State of an active vconn.*/
45 /* This is the ordinary progression of states. */
46 VCS_CONNECTING, /* Underlying vconn is not connected. */
47 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
48 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
49 VCS_CONNECTED, /* Connection established. */
51 /* These states are entered only when something goes wrong. */
52 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
53 VCS_DISCONNECTED /* Connection failed or connection closed. */
56 static struct vconn_class *vconn_classes[] = {
64 static struct pvconn_class *pvconn_classes[] = {
72 /* Rate limit for individual OpenFlow messages going over the vconn, output at
73 * DBG level. This is very high because, if these are enabled, it is because
74 * we really need to see them. */
75 static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
77 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
78 * in the peer and so there's not much point in showing a lot of them. */
79 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
81 static int do_recv(struct vconn *, struct ofpbuf **);
82 static int do_send(struct vconn *, struct ofpbuf *);
84 /* Check the validity of the vconn class structures. */
86 check_vconn_classes(void)
91 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
92 struct vconn_class *class = vconn_classes[i];
93 assert(class->name != NULL);
94 assert(class->open != NULL);
95 if (class->close || class->recv || class->send
96 || class->run || class->run_wait || class->wait) {
97 assert(class->close != NULL);
98 assert(class->recv != NULL);
99 assert(class->send != NULL);
100 assert(class->wait != NULL);
102 /* This class delegates to another one. */
106 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
107 struct pvconn_class *class = pvconn_classes[i];
108 assert(class->name != NULL);
109 assert(class->listen != NULL);
110 if (class->close || class->accept || class->wait) {
111 assert(class->close != NULL);
112 assert(class->accept != NULL);
113 assert(class->wait != NULL);
115 /* This class delegates to another one. */
121 /* Prints information on active (if 'active') and passive (if 'passive')
122 * connection methods supported by the vconn. If 'bootstrap' is true, also
123 * advertises options to bootstrap the CA certificate. */
125 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
127 /* Really this should be implemented via callbacks into the vconn
128 * providers, but that seems too heavy-weight to bother with at the
133 printf("Active OpenFlow connection methods:\n");
134 printf(" tcp:IP[:PORT] "
135 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
137 printf(" ssl:IP[:PORT] "
138 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
140 printf(" unix:FILE Unix domain socket named FILE\n");
144 printf("Passive OpenFlow connection methods:\n");
145 printf(" ptcp:[PORT][:IP] "
146 "listen to TCP PORT (default: %d) on IP\n",
149 printf(" pssl:[PORT][:IP] "
150 "listen for SSL on PORT (default: %d) on IP\n",
153 printf(" punix:FILE "
154 "listen on Unix domain socket FILE\n");
158 printf("PKI configuration (required to use SSL):\n"
159 " -p, --private-key=FILE file with private key\n"
160 " -c, --certificate=FILE file with certificate for private key\n"
161 " -C, --ca-cert=FILE file with peer CA certificate\n");
163 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
164 "to read or create\n");
169 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
170 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
171 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
174 vconn_lookup_class(const char *name, struct vconn_class **classp)
178 prefix_len = strcspn(name, ":");
179 if (name[prefix_len] != '\0') {
182 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
183 struct vconn_class *class = vconn_classes[i];
184 if (strlen(class->name) == prefix_len
185 && !memcmp(class->name, name, prefix_len)) {
196 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
197 * a supported connection type, otherwise EAFNOSUPPORT. */
199 vconn_verify_name(const char *name)
201 struct vconn_class *class;
202 return vconn_lookup_class(name, &class);
205 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
206 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
207 * are vconn class-specific.
209 * The vconn will automatically negotiate an OpenFlow protocol version
210 * acceptable to both peers on the connection. The version negotiated will be
211 * no lower than 'min_version' and no higher than OFP_VERSION.
213 * Returns 0 if successful, otherwise a positive errno value. If successful,
214 * stores a pointer to the new connection in '*vconnp', otherwise a null
217 vconn_open(const char *name, int min_version, struct vconn **vconnp)
219 struct vconn_class *class;
224 COVERAGE_INC(vconn_open);
225 check_vconn_classes();
227 /* Look up the class. */
228 error = vconn_lookup_class(name, &class);
233 /* Call class's "open" function. */
234 suffix_copy = xstrdup(strchr(name, ':') + 1);
235 error = class->open(name, suffix_copy, &vconn);
242 assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
243 vconn->min_version = min_version;
252 /* Allows 'vconn' to perform maintenance activities, such as flushing output
255 vconn_run(struct vconn *vconn)
257 if (vconn->class->run) {
258 (vconn->class->run)(vconn);
262 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
263 * maintenance activities. */
265 vconn_run_wait(struct vconn *vconn)
267 if (vconn->class->run_wait) {
268 (vconn->class->run_wait)(vconn);
273 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
280 error = vconn_open(name, min_version, &vconn);
281 while (error == EAGAIN) {
283 vconn_run_wait(vconn);
284 vconn_connect_wait(vconn);
286 error = vconn_connect(vconn);
287 assert(error != EINPROGRESS);
298 /* Closes 'vconn'. */
300 vconn_close(struct vconn *vconn)
303 char *name = vconn->name;
304 (vconn->class->close)(vconn);
309 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
311 vconn_get_name(const struct vconn *vconn)
316 /* Returns the IP address of the peer, or 0 if the peer is not connected over
317 * an IP-based protocol or if its IP address is not yet known. */
319 vconn_get_remote_ip(const struct vconn *vconn)
321 return vconn->remote_ip;
324 /* Returns the transport port of the peer, or 0 if the connection does not
325 * contain a port or if the port is not yet known. */
327 vconn_get_remote_port(const struct vconn *vconn)
329 return vconn->remote_port;
332 /* Returns the IP address used to connect to the peer, or 0 if the
333 * connection is not an IP-based protocol or if its IP address is not
336 vconn_get_local_ip(const struct vconn *vconn)
338 return vconn->local_ip;
341 /* Returns the transport port used to connect to the peer, or 0 if the
342 * connection does not contain a port or if the port is not yet known. */
344 vconn_get_local_port(const struct vconn *vconn)
346 return vconn->local_port;
350 vcs_connecting(struct vconn *vconn)
352 int retval = (vconn->class->connect)(vconn);
353 assert(retval != EINPROGRESS);
355 vconn->state = VCS_SEND_HELLO;
356 } else if (retval != EAGAIN) {
357 vconn->state = VCS_DISCONNECTED;
358 vconn->error = retval;
363 vcs_send_hello(struct vconn *vconn)
368 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
369 retval = do_send(vconn, b);
371 vconn->state = VCS_RECV_HELLO;
374 if (retval != EAGAIN) {
375 vconn->state = VCS_DISCONNECTED;
376 vconn->error = retval;
382 vcs_recv_hello(struct vconn *vconn)
387 retval = do_recv(vconn, &b);
389 struct ofp_header *oh = b->data;
391 if (oh->type == OFPT_HELLO) {
392 if (b->size > sizeof *oh) {
393 struct ds msg = DS_EMPTY_INITIALIZER;
394 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
395 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
396 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
400 vconn->version = MIN(OFP_VERSION, oh->version);
401 if (vconn->version < vconn->min_version) {
402 VLOG_WARN_RL(&bad_ofmsg_rl,
403 "%s: version negotiation failed: we support "
404 "versions 0x%02x to 0x%02x inclusive but peer "
405 "supports no later than version 0x%02"PRIx8,
406 vconn->name, vconn->min_version, OFP_VERSION,
408 vconn->state = VCS_SEND_ERROR;
410 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
411 "(we support versions 0x%02x to 0x%02x inclusive, "
412 "peer no later than version 0x%02"PRIx8")",
413 vconn->name, vconn->version, vconn->min_version,
414 OFP_VERSION, oh->version);
415 vconn->state = VCS_CONNECTED;
420 char *s = ofp_to_string(b->data, b->size, 1);
421 VLOG_WARN_RL(&bad_ofmsg_rl,
422 "%s: received message while expecting hello: %s",
430 if (retval != EAGAIN) {
431 vconn->state = VCS_DISCONNECTED;
432 vconn->error = retval == EOF ? ECONNRESET : retval;
437 vcs_send_error(struct vconn *vconn)
439 struct ofp_error_msg *error;
444 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
445 "you support no later than version 0x%02"PRIx8".",
446 vconn->min_version, OFP_VERSION, vconn->version);
447 error = make_openflow(sizeof *error, OFPT_ERROR, &b);
448 error->type = htons(OFPET_HELLO_FAILED);
449 error->code = htons(OFPHFC_INCOMPATIBLE);
450 ofpbuf_put(b, s, strlen(s));
451 update_openflow_length(b);
452 retval = do_send(vconn, b);
456 if (retval != EAGAIN) {
457 vconn->state = VCS_DISCONNECTED;
458 vconn->error = retval ? retval : EPROTO;
462 /* Tries to complete the connection on 'vconn', which must be an active
463 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
464 * was successful or a positive errno value if it failed. If the
465 * connection is still in progress, returns EAGAIN. */
467 vconn_connect(struct vconn *vconn)
469 enum vconn_state last_state;
471 assert(vconn->min_version >= 0);
473 last_state = vconn->state;
474 switch (vconn->state) {
476 vcs_connecting(vconn);
480 vcs_send_hello(vconn);
484 vcs_recv_hello(vconn);
491 vcs_send_error(vconn);
494 case VCS_DISCONNECTED:
500 } while (vconn->state != last_state);
505 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
506 * vconn. If successful, stores the received message into '*msgp' and returns
507 * 0. The caller is responsible for destroying the message with
508 * ofpbuf_delete(). On failure, returns a positive errno value and stores a
509 * null pointer into '*msgp'. On normal connection close, returns EOF.
511 * vconn_recv will not block waiting for a packet to arrive. If no packets
512 * have been received, it returns EAGAIN immediately. */
514 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
516 int retval = vconn_connect(vconn);
518 retval = do_recv(vconn, msgp);
524 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
526 int retval = (vconn->class->recv)(vconn, msgp);
528 struct ofp_header *oh;
530 COVERAGE_INC(vconn_received);
531 if (VLOG_IS_DBG_ENABLED()) {
532 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
533 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
537 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
538 if (oh->version != vconn->version
539 && oh->type != OFPT_HELLO
540 && oh->type != OFPT_ERROR
541 && oh->type != OFPT_ECHO_REQUEST
542 && oh->type != OFPT_ECHO_REPLY
543 && oh->type != OFPT_VENDOR)
545 if (vconn->version < 0) {
546 VLOG_ERR_RL(&bad_ofmsg_rl,
547 "%s: received OpenFlow message type %"PRIu8" "
548 "before version negotiation complete",
549 vconn->name, oh->type);
551 VLOG_ERR_RL(&bad_ofmsg_rl,
552 "%s: received OpenFlow version 0x%02"PRIx8" "
554 vconn->name, oh->version, vconn->version);
556 ofpbuf_delete(*msgp);
566 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
567 * vconn. If successful, returns 0, in which case ownership of 'msg' is
568 * transferred to the vconn. Success does not guarantee that 'msg' has been or
569 * ever will be delivered to the peer, only that it has been queued for
572 * Returns a positive errno value on failure, in which case the caller
573 * retains ownership of 'msg'.
575 * vconn_send will not block. If 'msg' cannot be immediately accepted for
576 * transmission, it returns EAGAIN immediately. */
578 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
580 int retval = vconn_connect(vconn);
582 retval = do_send(vconn, msg);
588 do_send(struct vconn *vconn, struct ofpbuf *msg)
592 assert(msg->size >= sizeof(struct ofp_header));
593 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
594 if (!VLOG_IS_DBG_ENABLED()) {
595 COVERAGE_INC(vconn_sent);
596 retval = (vconn->class->send)(vconn, msg);
598 char *s = ofp_to_string(msg->data, msg->size, 1);
599 retval = (vconn->class->send)(vconn, msg);
600 if (retval != EAGAIN) {
601 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
602 vconn->name, strerror(retval), s);
609 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
611 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
617 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
619 vconn_run_wait(vconn);
620 vconn_send_wait(vconn);
626 /* Same as vconn_recv, except that it waits until a message is received. */
628 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
634 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
636 vconn_run_wait(vconn);
637 vconn_recv_wait(vconn);
643 /* Waits until a message with a transaction ID matching 'xid' is recived on
644 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
645 * '*replyp' for the caller to examine and free. Otherwise returns a positive
646 * errno value, or EOF, and sets '*replyp' to null.
648 * 'request' is always destroyed, regardless of the return value. */
650 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
654 struct ofpbuf *reply;
657 error = vconn_recv_block(vconn, &reply);
662 recv_xid = ((struct ofp_header *) reply->data)->xid;
663 if (xid == recv_xid) {
668 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
669 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
670 ofpbuf_delete(reply);
674 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
675 * matching transaction ID. Returns 0 if successful, in which case the reply
676 * is stored in '*replyp' for the caller to examine and free. Otherwise
677 * returns a positive errno value, or EOF, and sets '*replyp' to null.
679 * 'request' is always destroyed, regardless of the return value. */
681 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
682 struct ofpbuf **replyp)
684 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
688 error = vconn_send_block(vconn, request);
690 ofpbuf_delete(request);
692 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
696 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
698 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
700 switch (vconn->state) {
717 case VCS_DISCONNECTED:
718 poll_immediate_wake();
721 (vconn->class->wait)(vconn, wait);
725 vconn_connect_wait(struct vconn *vconn)
727 vconn_wait(vconn, WAIT_CONNECT);
731 vconn_recv_wait(struct vconn *vconn)
733 vconn_wait(vconn, WAIT_RECV);
737 vconn_send_wait(struct vconn *vconn)
739 vconn_wait(vconn, WAIT_SEND);
742 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
743 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
744 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
747 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
751 prefix_len = strcspn(name, ":");
752 if (name[prefix_len] != '\0') {
755 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
756 struct pvconn_class *class = pvconn_classes[i];
757 if (strlen(class->name) == prefix_len
758 && !memcmp(class->name, name, prefix_len)) {
769 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
770 * a supported connection type, otherwise EAFNOSUPPORT. */
772 pvconn_verify_name(const char *name)
774 struct pvconn_class *class;
775 return pvconn_lookup_class(name, &class);
778 /* Attempts to start listening for OpenFlow connections. 'name' is a
779 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
780 * class's name and ARGS are vconn class-specific.
782 * Returns 0 if successful, otherwise a positive errno value. If successful,
783 * stores a pointer to the new connection in '*pvconnp', otherwise a null
786 pvconn_open(const char *name, struct pvconn **pvconnp)
788 struct pvconn_class *class;
789 struct pvconn *pvconn;
793 check_vconn_classes();
795 /* Look up the class. */
796 error = pvconn_lookup_class(name, &class);
801 /* Call class's "open" function. */
802 suffix_copy = xstrdup(strchr(name, ':') + 1);
803 error = class->listen(name, suffix_copy, &pvconn);
818 /* Returns the name that was used to open 'pvconn'. The caller must not
819 * modify or free the name. */
821 pvconn_get_name(const struct pvconn *pvconn)
826 /* Closes 'pvconn'. */
828 pvconn_close(struct pvconn *pvconn)
830 if (pvconn != NULL) {
831 char *name = pvconn->name;
832 (pvconn->class->close)(pvconn);
837 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
838 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
841 * The new vconn will automatically negotiate an OpenFlow protocol version
842 * acceptable to both peers on the connection. The version negotiated will be
843 * no lower than 'min_version' and no higher than OFP_VERSION.
845 * pvconn_accept() will not block waiting for a connection. If no connection
846 * is ready to be accepted, it returns EAGAIN immediately. */
848 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
850 int retval = (pvconn->class->accept)(pvconn, new_vconn);
854 assert((*new_vconn)->state != VCS_CONNECTING
855 || (*new_vconn)->class->connect);
856 (*new_vconn)->min_version = min_version;
862 pvconn_wait(struct pvconn *pvconn)
864 (pvconn->class->wait)(pvconn);
867 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
868 * The initial connection status, supplied as 'connect_status', is interpreted
871 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
872 * called in the normal fashion.
874 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
875 * function should be called to complete the connection.
877 * - Other positive errno values indicate that the connection failed with
878 * the specified error.
880 * After calling this function, vconn_close() must be used to destroy 'vconn',
881 * otherwise resources will be leaked.
883 * The caller retains ownership of 'name'. */
885 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
888 vconn->class = class;
889 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
890 : !connect_status ? VCS_SEND_HELLO
892 vconn->error = connect_status;
894 vconn->min_version = -1;
895 vconn->remote_ip = 0;
896 vconn->remote_port = 0;
898 vconn->local_port = 0;
899 vconn->name = xstrdup(name);
900 assert(vconn->state != VCS_CONNECTING || class->connect);
904 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
906 vconn->remote_ip = ip;
910 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
912 vconn->remote_port = port;
916 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
918 vconn->local_ip = ip;
922 vconn_set_local_port(struct vconn *vconn, uint16_t port)
924 vconn->local_port = port;
928 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
931 pvconn->class = class;
932 pvconn->name = xstrdup(name);