2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "vconn-provider.h"
22 #include <netinet/in.h>
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
30 #include "ofp-print.h"
33 #include "openflow/nicira-ext.h"
34 #include "openflow/openflow.h"
36 #include "poll-loop.h"
41 VLOG_DEFINE_THIS_MODULE(vconn)
43 /* 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);
282 while ((error == vconn_connect(vconn)) == EAGAIN) {
284 vconn_run_wait(vconn);
285 vconn_connect_wait(vconn);
288 assert(error != EINPROGRESS);
300 /* Closes 'vconn'. */
302 vconn_close(struct vconn *vconn)
305 char *name = vconn->name;
306 (vconn->class->close)(vconn);
311 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
313 vconn_get_name(const struct vconn *vconn)
318 /* Returns the IP address of the peer, or 0 if the peer is not connected over
319 * an IP-based protocol or if its IP address is not yet known. */
321 vconn_get_remote_ip(const struct vconn *vconn)
323 return vconn->remote_ip;
326 /* Returns the transport port of the peer, or 0 if the connection does not
327 * contain a port or if the port is not yet known. */
329 vconn_get_remote_port(const struct vconn *vconn)
331 return vconn->remote_port;
334 /* Returns the IP address used to connect to the peer, or 0 if the
335 * connection is not an IP-based protocol or if its IP address is not
338 vconn_get_local_ip(const struct vconn *vconn)
340 return vconn->local_ip;
343 /* Returns the transport port used to connect to the peer, or 0 if the
344 * connection does not contain a port or if the port is not yet known. */
346 vconn_get_local_port(const struct vconn *vconn)
348 return vconn->local_port;
352 vcs_connecting(struct vconn *vconn)
354 int retval = (vconn->class->connect)(vconn);
355 assert(retval != EINPROGRESS);
357 vconn->state = VCS_SEND_HELLO;
358 } else if (retval != EAGAIN) {
359 vconn->state = VCS_DISCONNECTED;
360 vconn->error = retval;
365 vcs_send_hello(struct vconn *vconn)
370 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
371 retval = do_send(vconn, b);
373 vconn->state = VCS_RECV_HELLO;
376 if (retval != EAGAIN) {
377 vconn->state = VCS_DISCONNECTED;
378 vconn->error = retval;
384 vcs_recv_hello(struct vconn *vconn)
389 retval = do_recv(vconn, &b);
391 struct ofp_header *oh = b->data;
393 if (oh->type == OFPT_HELLO) {
394 if (b->size > sizeof *oh) {
395 struct ds msg = DS_EMPTY_INITIALIZER;
396 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
397 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
398 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
402 vconn->version = MIN(OFP_VERSION, oh->version);
403 if (vconn->version < vconn->min_version) {
404 VLOG_WARN_RL(&bad_ofmsg_rl,
405 "%s: version negotiation failed: we support "
406 "versions 0x%02x to 0x%02x inclusive but peer "
407 "supports no later than version 0x%02"PRIx8,
408 vconn->name, vconn->min_version, OFP_VERSION,
410 vconn->state = VCS_SEND_ERROR;
412 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
413 "(we support versions 0x%02x to 0x%02x inclusive, "
414 "peer no later than version 0x%02"PRIx8")",
415 vconn->name, vconn->version, vconn->min_version,
416 OFP_VERSION, oh->version);
417 vconn->state = VCS_CONNECTED;
422 char *s = ofp_to_string(b->data, b->size, 1);
423 VLOG_WARN_RL(&bad_ofmsg_rl,
424 "%s: received message while expecting hello: %s",
432 if (retval != EAGAIN) {
433 vconn->state = VCS_DISCONNECTED;
434 vconn->error = retval == EOF ? ECONNRESET : retval;
439 vcs_send_error(struct vconn *vconn)
441 struct ofp_error_msg *error;
446 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
447 "you support no later than version 0x%02"PRIx8".",
448 vconn->min_version, OFP_VERSION, vconn->version);
449 error = make_openflow(sizeof *error, OFPT_ERROR, &b);
450 error->type = htons(OFPET_HELLO_FAILED);
451 error->code = htons(OFPHFC_INCOMPATIBLE);
452 ofpbuf_put(b, s, strlen(s));
453 update_openflow_length(b);
454 retval = do_send(vconn, b);
458 if (retval != EAGAIN) {
459 vconn->state = VCS_DISCONNECTED;
460 vconn->error = retval ? retval : EPROTO;
464 /* Tries to complete the connection on 'vconn', which must be an active
465 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
466 * was successful or a positive errno value if it failed. If the
467 * connection is still in progress, returns EAGAIN. */
469 vconn_connect(struct vconn *vconn)
471 enum vconn_state last_state;
473 assert(vconn->min_version >= 0);
475 last_state = vconn->state;
476 switch (vconn->state) {
478 vcs_connecting(vconn);
482 vcs_send_hello(vconn);
486 vcs_recv_hello(vconn);
493 vcs_send_error(vconn);
496 case VCS_DISCONNECTED:
502 } while (vconn->state != last_state);
507 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
508 * vconn. If successful, stores the received message into '*msgp' and returns
509 * 0. The caller is responsible for destroying the message with
510 * ofpbuf_delete(). On failure, returns a positive errno value and stores a
511 * null pointer into '*msgp'. On normal connection close, returns EOF.
513 * vconn_recv will not block waiting for a packet to arrive. If no packets
514 * have been received, it returns EAGAIN immediately. */
516 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
518 int retval = vconn_connect(vconn);
520 retval = do_recv(vconn, msgp);
526 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
528 int retval = (vconn->class->recv)(vconn, msgp);
530 struct ofp_header *oh;
532 COVERAGE_INC(vconn_received);
533 if (VLOG_IS_DBG_ENABLED()) {
534 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
535 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
539 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
540 if (oh->version != vconn->version
541 && oh->type != OFPT_HELLO
542 && oh->type != OFPT_ERROR
543 && oh->type != OFPT_ECHO_REQUEST
544 && oh->type != OFPT_ECHO_REPLY
545 && oh->type != OFPT_VENDOR)
547 if (vconn->version < 0) {
548 VLOG_ERR_RL(&bad_ofmsg_rl,
549 "%s: received OpenFlow message type %"PRIu8" "
550 "before version negotiation complete",
551 vconn->name, oh->type);
553 VLOG_ERR_RL(&bad_ofmsg_rl,
554 "%s: received OpenFlow version 0x%02"PRIx8" "
556 vconn->name, oh->version, vconn->version);
558 ofpbuf_delete(*msgp);
568 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
569 * vconn. If successful, returns 0, in which case ownership of 'msg' is
570 * transferred to the vconn. Success does not guarantee that 'msg' has been or
571 * ever will be delivered to the peer, only that it has been queued for
574 * Returns a positive errno value on failure, in which case the caller
575 * retains ownership of 'msg'.
577 * vconn_send will not block. If 'msg' cannot be immediately accepted for
578 * transmission, it returns EAGAIN immediately. */
580 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
582 int retval = vconn_connect(vconn);
584 retval = do_send(vconn, msg);
590 do_send(struct vconn *vconn, struct ofpbuf *msg)
594 assert(msg->size >= sizeof(struct ofp_header));
595 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
596 if (!VLOG_IS_DBG_ENABLED()) {
597 COVERAGE_INC(vconn_sent);
598 retval = (vconn->class->send)(vconn, msg);
600 char *s = ofp_to_string(msg->data, msg->size, 1);
601 retval = (vconn->class->send)(vconn, msg);
602 if (retval != EAGAIN) {
603 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
604 vconn->name, strerror(retval), s);
611 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
613 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
619 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
621 vconn_run_wait(vconn);
622 vconn_send_wait(vconn);
628 /* Same as vconn_recv, except that it waits until a message is received. */
630 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
636 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
638 vconn_run_wait(vconn);
639 vconn_recv_wait(vconn);
645 /* Waits until a message with a transaction ID matching 'xid' is recived on
646 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
647 * '*replyp' for the caller to examine and free. Otherwise returns a positive
648 * errno value, or EOF, and sets '*replyp' to null.
650 * 'request' is always destroyed, regardless of the return value. */
652 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
656 struct ofpbuf *reply;
659 error = vconn_recv_block(vconn, &reply);
664 recv_xid = ((struct ofp_header *) reply->data)->xid;
665 if (xid == recv_xid) {
670 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
671 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
672 ofpbuf_delete(reply);
676 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
677 * matching transaction ID. Returns 0 if successful, in which case the reply
678 * is stored in '*replyp' for the caller to examine and free. Otherwise
679 * returns a positive errno value, or EOF, and sets '*replyp' to null.
681 * 'request' is always destroyed, regardless of the return value. */
683 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
684 struct ofpbuf **replyp)
686 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
690 error = vconn_send_block(vconn, request);
692 ofpbuf_delete(request);
694 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
698 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
700 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
702 switch (vconn->state) {
719 case VCS_DISCONNECTED:
720 poll_immediate_wake();
723 (vconn->class->wait)(vconn, wait);
727 vconn_connect_wait(struct vconn *vconn)
729 vconn_wait(vconn, WAIT_CONNECT);
733 vconn_recv_wait(struct vconn *vconn)
735 vconn_wait(vconn, WAIT_RECV);
739 vconn_send_wait(struct vconn *vconn)
741 vconn_wait(vconn, WAIT_SEND);
744 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
745 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
746 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
749 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
753 prefix_len = strcspn(name, ":");
754 if (name[prefix_len] != '\0') {
757 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
758 struct pvconn_class *class = pvconn_classes[i];
759 if (strlen(class->name) == prefix_len
760 && !memcmp(class->name, name, prefix_len)) {
771 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
772 * a supported connection type, otherwise EAFNOSUPPORT. */
774 pvconn_verify_name(const char *name)
776 struct pvconn_class *class;
777 return pvconn_lookup_class(name, &class);
780 /* Attempts to start listening for OpenFlow connections. 'name' is a
781 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
782 * class's name and ARGS are vconn class-specific.
784 * Returns 0 if successful, otherwise a positive errno value. If successful,
785 * stores a pointer to the new connection in '*pvconnp', otherwise a null
788 pvconn_open(const char *name, struct pvconn **pvconnp)
790 struct pvconn_class *class;
791 struct pvconn *pvconn;
795 check_vconn_classes();
797 /* Look up the class. */
798 error = pvconn_lookup_class(name, &class);
803 /* Call class's "open" function. */
804 suffix_copy = xstrdup(strchr(name, ':') + 1);
805 error = class->listen(name, suffix_copy, &pvconn);
820 /* Returns the name that was used to open 'pvconn'. The caller must not
821 * modify or free the name. */
823 pvconn_get_name(const struct pvconn *pvconn)
828 /* Closes 'pvconn'. */
830 pvconn_close(struct pvconn *pvconn)
832 if (pvconn != NULL) {
833 char *name = pvconn->name;
834 (pvconn->class->close)(pvconn);
839 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
840 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
843 * The new vconn will automatically negotiate an OpenFlow protocol version
844 * acceptable to both peers on the connection. The version negotiated will be
845 * no lower than 'min_version' and no higher than OFP_VERSION.
847 * pvconn_accept() will not block waiting for a connection. If no connection
848 * is ready to be accepted, it returns EAGAIN immediately. */
850 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
852 int retval = (pvconn->class->accept)(pvconn, new_vconn);
856 assert((*new_vconn)->state != VCS_CONNECTING
857 || (*new_vconn)->class->connect);
858 (*new_vconn)->min_version = min_version;
864 pvconn_wait(struct pvconn *pvconn)
866 (pvconn->class->wait)(pvconn);
869 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
870 * The initial connection status, supplied as 'connect_status', is interpreted
873 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
874 * called in the normal fashion.
876 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
877 * function should be called to complete the connection.
879 * - Other positive errno values indicate that the connection failed with
880 * the specified error.
882 * After calling this function, vconn_close() must be used to destroy 'vconn',
883 * otherwise resources will be leaked.
885 * The caller retains ownership of 'name'. */
887 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
890 vconn->class = class;
891 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
892 : !connect_status ? VCS_SEND_HELLO
894 vconn->error = connect_status;
896 vconn->min_version = -1;
897 vconn->remote_ip = 0;
898 vconn->remote_port = 0;
900 vconn->local_port = 0;
901 vconn->name = xstrdup(name);
902 assert(vconn->state != VCS_CONNECTING || class->connect);
906 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
908 vconn->remote_ip = ip;
912 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
914 vconn->remote_port = port;
918 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
920 vconn->local_ip = ip;
924 vconn_set_local_port(struct vconn *vconn, uint16_t port)
926 vconn->local_port = port;
930 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
933 pvconn->class = class;
934 pvconn->name = xstrdup(name);