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'. If 'vconn''s connection is
465 * complete, returns 0 if the connection was successful or a positive errno
466 * value if it failed. If the connection is still in progress, returns
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'. If successful, stores
508 * the received message into '*msgp' and returns 0. The caller is responsible
509 * for destroying the message with ofpbuf_delete(). On failure, returns a
510 * positive errno value and stores a null pointer into '*msgp'. On normal
511 * 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'. If successful, returns 0,
569 * in which case ownership of 'msg' is transferred to the vconn. Success does
570 * not guarantee that 'msg' has been or ever will be delivered to the peer,
571 * only that it has been queued for transmission.
573 * Returns a positive errno value on failure, in which case the caller
574 * retains ownership of 'msg'.
576 * vconn_send will not block. If 'msg' cannot be immediately accepted for
577 * transmission, it returns EAGAIN immediately. */
579 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
581 int retval = vconn_connect(vconn);
583 retval = do_send(vconn, msg);
589 do_send(struct vconn *vconn, struct ofpbuf *msg)
593 assert(msg->size >= sizeof(struct ofp_header));
594 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
595 if (!VLOG_IS_DBG_ENABLED()) {
596 COVERAGE_INC(vconn_sent);
597 retval = (vconn->class->send)(vconn, msg);
599 char *s = ofp_to_string(msg->data, msg->size, 1);
600 retval = (vconn->class->send)(vconn, msg);
601 if (retval != EAGAIN) {
602 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
603 vconn->name, strerror(retval), s);
610 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
612 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
618 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
620 vconn_run_wait(vconn);
621 vconn_send_wait(vconn);
627 /* Same as vconn_recv, except that it waits until a message is received. */
629 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
635 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
637 vconn_run_wait(vconn);
638 vconn_recv_wait(vconn);
644 /* Waits until a message with a transaction ID matching 'xid' is recived on
645 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
646 * '*replyp' for the caller to examine and free. Otherwise returns a positive
647 * errno value, or EOF, and sets '*replyp' to null.
649 * 'request' is always destroyed, regardless of the return value. */
651 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
655 struct ofpbuf *reply;
658 error = vconn_recv_block(vconn, &reply);
663 recv_xid = ((struct ofp_header *) reply->data)->xid;
664 if (xid == recv_xid) {
669 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
670 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
671 ofpbuf_delete(reply);
675 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
676 * matching transaction ID. Returns 0 if successful, in which case the reply
677 * is stored in '*replyp' for the caller to examine and free. Otherwise
678 * returns a positive errno value, or EOF, and sets '*replyp' to null.
680 * 'request' is always destroyed, regardless of the return value. */
682 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
683 struct ofpbuf **replyp)
685 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
689 error = vconn_send_block(vconn, request);
691 ofpbuf_delete(request);
693 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
697 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
699 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
701 switch (vconn->state) {
718 case VCS_DISCONNECTED:
719 poll_immediate_wake();
722 (vconn->class->wait)(vconn, wait);
726 vconn_connect_wait(struct vconn *vconn)
728 vconn_wait(vconn, WAIT_CONNECT);
732 vconn_recv_wait(struct vconn *vconn)
734 vconn_wait(vconn, WAIT_RECV);
738 vconn_send_wait(struct vconn *vconn)
740 vconn_wait(vconn, WAIT_SEND);
743 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
744 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
745 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
748 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
752 prefix_len = strcspn(name, ":");
753 if (name[prefix_len] != '\0') {
756 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
757 struct pvconn_class *class = pvconn_classes[i];
758 if (strlen(class->name) == prefix_len
759 && !memcmp(class->name, name, prefix_len)) {
770 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
771 * a supported connection type, otherwise EAFNOSUPPORT. */
773 pvconn_verify_name(const char *name)
775 struct pvconn_class *class;
776 return pvconn_lookup_class(name, &class);
779 /* Attempts to start listening for OpenFlow connections. 'name' is a
780 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
781 * class's name and ARGS are vconn class-specific.
783 * Returns 0 if successful, otherwise a positive errno value. If successful,
784 * stores a pointer to the new connection in '*pvconnp', otherwise a null
787 pvconn_open(const char *name, struct pvconn **pvconnp)
789 struct pvconn_class *class;
790 struct pvconn *pvconn;
794 check_vconn_classes();
796 /* Look up the class. */
797 error = pvconn_lookup_class(name, &class);
802 /* Call class's "open" function. */
803 suffix_copy = xstrdup(strchr(name, ':') + 1);
804 error = class->listen(name, suffix_copy, &pvconn);
819 /* Returns the name that was used to open 'pvconn'. The caller must not
820 * modify or free the name. */
822 pvconn_get_name(const struct pvconn *pvconn)
827 /* Closes 'pvconn'. */
829 pvconn_close(struct pvconn *pvconn)
831 if (pvconn != NULL) {
832 char *name = pvconn->name;
833 (pvconn->class->close)(pvconn);
838 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
839 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
842 * The new vconn will automatically negotiate an OpenFlow protocol version
843 * acceptable to both peers on the connection. The version negotiated will be
844 * no lower than 'min_version' and no higher than OFP_VERSION.
846 * pvconn_accept() will not block waiting for a connection. If no connection
847 * is ready to be accepted, it returns EAGAIN immediately. */
849 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
851 int retval = (pvconn->class->accept)(pvconn, new_vconn);
855 assert((*new_vconn)->state != VCS_CONNECTING
856 || (*new_vconn)->class->connect);
857 (*new_vconn)->min_version = min_version;
863 pvconn_wait(struct pvconn *pvconn)
865 (pvconn->class->wait)(pvconn);
868 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
869 * The initial connection status, supplied as 'connect_status', is interpreted
872 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
873 * called in the normal fashion.
875 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
876 * function should be called to complete the connection.
878 * - Other positive errno values indicate that the connection failed with
879 * the specified error.
881 * After calling this function, vconn_close() must be used to destroy 'vconn',
882 * otherwise resources will be leaked.
884 * The caller retains ownership of 'name'. */
886 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
889 vconn->class = class;
890 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
891 : !connect_status ? VCS_SEND_HELLO
893 vconn->error = connect_status;
895 vconn->min_version = -1;
896 vconn->remote_ip = 0;
897 vconn->remote_port = 0;
899 vconn->local_port = 0;
900 vconn->name = xstrdup(name);
901 assert(vconn->state != VCS_CONNECTING || class->connect);
905 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
907 vconn->remote_ip = ip;
911 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
913 vconn->remote_port = port;
917 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
919 vconn->local_ip = ip;
923 vconn_set_local_port(struct vconn *vconn, uint16_t port)
925 vconn->local_port = port;
929 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
932 pvconn->class = class;
933 pvconn->name = xstrdup(name);