99b6e7b0de0116e304a676e8bbb5e60360590bcb
[sliver-openvswitch.git] / lib / vconn.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "vconn-provider.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <poll.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "coverage.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
29 #include "flow.h"
30 #include "ofp-print.h"
31 #include "ofp-util.h"
32 #include "ofpbuf.h"
33 #include "openflow/nicira-ext.h"
34 #include "openflow/openflow.h"
35 #include "packets.h"
36 #include "poll-loop.h"
37 #include "random.h"
38 #include "util.h"
39 #include "vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(vconn)
42
43 /* State of an active vconn.*/
44 enum vconn_state {
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. */
50
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. */
54 };
55
56 static struct vconn_class *vconn_classes[] = {
57     &tcp_vconn_class,
58     &unix_vconn_class,
59 #ifdef HAVE_OPENSSL
60     &ssl_vconn_class,
61 #endif
62 };
63
64 static struct pvconn_class *pvconn_classes[] = {
65     &ptcp_pvconn_class,
66     &punix_pvconn_class,
67 #ifdef HAVE_OPENSSL
68     &pssl_pvconn_class,
69 #endif
70 };
71
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);
76
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);
80
81 static int do_recv(struct vconn *, struct ofpbuf **);
82 static int do_send(struct vconn *, struct ofpbuf *);
83
84 /* Check the validity of the vconn class structures. */
85 static void
86 check_vconn_classes(void)
87 {
88 #ifndef NDEBUG
89     size_t i;
90
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);
101         } else {
102             /* This class delegates to another one. */
103         }
104     }
105
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);
114         } else {
115             /* This class delegates to another one. */
116         }
117     }
118 #endif
119 }
120
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. */
124 void
125 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
126 {
127     /* Really this should be implemented via callbacks into the vconn
128      * providers, but that seems too heavy-weight to bother with at the
129      * moment. */
130     
131     printf("\n");
132     if (active) {
133         printf("Active OpenFlow connection methods:\n");
134         printf("  tcp:IP[:PORT]         "
135                "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
136 #ifdef HAVE_OPENSSL
137         printf("  ssl:IP[:PORT]         "
138                "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
139 #endif
140         printf("  unix:FILE               Unix domain socket named FILE\n");
141     }
142
143     if (passive) {
144         printf("Passive OpenFlow connection methods:\n");
145         printf("  ptcp:[PORT][:IP]        "
146                "listen to TCP PORT (default: %d) on IP\n",
147                OFP_TCP_PORT);
148 #ifdef HAVE_OPENSSL
149         printf("  pssl:[PORT][:IP]        "
150                "listen for SSL on PORT (default: %d) on IP\n",
151                OFP_SSL_PORT);
152 #endif
153         printf("  punix:FILE              "
154                "listen on Unix domain socket FILE\n");
155     }
156
157 #ifdef HAVE_OPENSSL
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");
162     if (bootstrap) {
163         printf("  --bootstrap-ca-cert=FILE  file with peer CA certificate "
164                "to read or create\n");
165     }
166 #endif
167 }
168
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
172  * class exists. */
173 static int
174 vconn_lookup_class(const char *name, struct vconn_class **classp)
175 {
176     size_t prefix_len;
177
178     prefix_len = strcspn(name, ":");
179     if (name[prefix_len] != '\0') {
180         size_t i;
181
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)) {
186                 *classp = class;
187                 return 0;
188             }
189         }
190     }
191
192     *classp = NULL;
193     return EAFNOSUPPORT;
194 }
195
196 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
197  * a supported connection type, otherwise EAFNOSUPPORT.  */
198 int
199 vconn_verify_name(const char *name)
200 {
201     struct vconn_class *class;
202     return vconn_lookup_class(name, &class);
203 }
204
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.
208  *
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.
212  *
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
215  * pointer.  */
216 int
217 vconn_open(const char *name, int min_version, struct vconn **vconnp)
218 {
219     struct vconn_class *class;
220     struct vconn *vconn;
221     char *suffix_copy;
222     int error;
223
224     COVERAGE_INC(vconn_open);
225     check_vconn_classes();
226
227     /* Look up the class. */
228     error = vconn_lookup_class(name, &class);
229     if (!class) {
230         goto error;
231     }
232
233     /* Call class's "open" function. */
234     suffix_copy = xstrdup(strchr(name, ':') + 1);
235     error = class->open(name, suffix_copy, &vconn);
236     free(suffix_copy);
237     if (error) {
238         goto error;
239     }
240
241     /* Success. */
242     assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
243     vconn->min_version = min_version;
244     *vconnp = vconn;
245     return 0;
246
247 error:
248     *vconnp = NULL;
249     return error;
250 }
251
252 /* Allows 'vconn' to perform maintenance activities, such as flushing output
253  * buffers. */
254 void
255 vconn_run(struct vconn *vconn)
256 {
257     if (vconn->class->run) {
258         (vconn->class->run)(vconn);
259     }
260 }
261
262 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
263  * maintenance activities. */
264 void
265 vconn_run_wait(struct vconn *vconn)
266 {
267     if (vconn->class->run_wait) {
268         (vconn->class->run_wait)(vconn);
269     }
270 }
271
272 int
273 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
274 {
275     struct vconn *vconn;
276     int error;
277
278     fatal_signal_run();
279
280     error = vconn_open(name, min_version, &vconn);
281     if (!error) {
282         while ((error == vconn_connect(vconn)) == EAGAIN) {
283             vconn_run(vconn);
284             vconn_run_wait(vconn);
285             vconn_connect_wait(vconn);
286             poll_block();
287         }
288         assert(error != EINPROGRESS);
289     }
290
291     if (error) {
292         vconn_close(vconn);
293         *vconnp = NULL;
294     } else {
295         *vconnp = vconn;
296     }
297     return error;
298 }
299
300 /* Closes 'vconn'. */
301 void
302 vconn_close(struct vconn *vconn)
303 {
304     if (vconn != NULL) {
305         char *name = vconn->name;
306         (vconn->class->close)(vconn);
307         free(name);
308     }
309 }
310
311 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
312 const char *
313 vconn_get_name(const struct vconn *vconn)
314 {
315     return vconn->name;
316 }
317
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. */
320 uint32_t
321 vconn_get_remote_ip(const struct vconn *vconn) 
322 {
323     return vconn->remote_ip;
324 }
325
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. */
328 uint16_t
329 vconn_get_remote_port(const struct vconn *vconn) 
330 {
331     return vconn->remote_port;
332 }
333
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 
336  * yet known. */
337 uint32_t
338 vconn_get_local_ip(const struct vconn *vconn) 
339 {
340     return vconn->local_ip;
341 }
342
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. */
345 uint16_t
346 vconn_get_local_port(const struct vconn *vconn) 
347 {
348     return vconn->local_port;
349 }
350
351 static void
352 vcs_connecting(struct vconn *vconn) 
353 {
354     int retval = (vconn->class->connect)(vconn);
355     assert(retval != EINPROGRESS);
356     if (!retval) {
357         vconn->state = VCS_SEND_HELLO;
358     } else if (retval != EAGAIN) {
359         vconn->state = VCS_DISCONNECTED;
360         vconn->error = retval;
361     }
362 }
363
364 static void
365 vcs_send_hello(struct vconn *vconn)
366 {
367     struct ofpbuf *b;
368     int retval;
369
370     make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
371     retval = do_send(vconn, b);
372     if (!retval) {
373         vconn->state = VCS_RECV_HELLO;
374     } else {
375         ofpbuf_delete(b);
376         if (retval != EAGAIN) {
377             vconn->state = VCS_DISCONNECTED;
378             vconn->error = retval;
379         }
380     }
381 }
382
383 static void
384 vcs_recv_hello(struct vconn *vconn)
385 {
386     struct ofpbuf *b;
387     int retval;
388
389     retval = do_recv(vconn, &b);
390     if (!retval) {
391         struct ofp_header *oh = b->data;
392
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));
399                 ds_destroy(&msg);
400             }
401
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,
409                              oh->version);
410                 vconn->state = VCS_SEND_ERROR;
411             } else {
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;
418             }
419             ofpbuf_delete(b);
420             return;
421         } else {
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",
425                          vconn->name, s);
426             free(s);
427             retval = EPROTO;
428             ofpbuf_delete(b);
429         }
430     }
431
432     if (retval != EAGAIN) {
433         vconn->state = VCS_DISCONNECTED;
434         vconn->error = retval == EOF ? ECONNRESET : retval;
435     }
436 }
437
438 static void
439 vcs_send_error(struct vconn *vconn)
440 {
441     struct ofp_error_msg *error;
442     struct ofpbuf *b;
443     char s[128];
444     int retval;
445
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);
455     if (retval) {
456         ofpbuf_delete(b);
457     }
458     if (retval != EAGAIN) {
459         vconn->state = VCS_DISCONNECTED;
460         vconn->error = retval ? retval : EPROTO;
461     }
462 }
463
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. */
468 int
469 vconn_connect(struct vconn *vconn)
470 {
471     enum vconn_state last_state;
472
473     assert(vconn->min_version >= 0);
474     do {
475         last_state = vconn->state;
476         switch (vconn->state) {
477         case VCS_CONNECTING:
478             vcs_connecting(vconn);
479             break;
480
481         case VCS_SEND_HELLO:
482             vcs_send_hello(vconn);
483             break;
484
485         case VCS_RECV_HELLO:
486             vcs_recv_hello(vconn);
487             break;
488
489         case VCS_CONNECTED:
490             return 0;
491
492         case VCS_SEND_ERROR:
493             vcs_send_error(vconn);
494             break;
495
496         case VCS_DISCONNECTED:
497             return vconn->error;
498
499         default:
500             NOT_REACHED();
501         }
502     } while (vconn->state != last_state);
503
504     return EAGAIN;
505 }
506
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.
512  *
513  * vconn_recv will not block waiting for a packet to arrive.  If no packets
514  * have been received, it returns EAGAIN immediately. */
515 int
516 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
517 {
518     int retval = vconn_connect(vconn);
519     if (!retval) {
520         retval = do_recv(vconn, msgp);
521     }
522     return retval;
523 }
524
525 static int
526 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
527 {
528     int retval = (vconn->class->recv)(vconn, msgp);
529     if (!retval) {
530         struct ofp_header *oh;
531
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);
536             free(s);
537         }
538
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)
546         {
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);
552             } else {
553                 VLOG_ERR_RL(&bad_ofmsg_rl,
554                             "%s: received OpenFlow version 0x%02"PRIx8" "
555                             "!= expected %02x",
556                             vconn->name, oh->version, vconn->version);
557             }
558             ofpbuf_delete(*msgp);
559             retval = EPROTO;
560         }
561     }
562     if (retval) {
563         *msgp = NULL;
564     }
565     return retval;
566 }
567
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
572  * transmission.
573  *
574  * Returns a positive errno value on failure, in which case the caller
575  * retains ownership of 'msg'.
576  *
577  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
578  * transmission, it returns EAGAIN immediately. */
579 int
580 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
581 {
582     int retval = vconn_connect(vconn);
583     if (!retval) {
584         retval = do_send(vconn, msg);
585     }
586     return retval;
587 }
588
589 static int
590 do_send(struct vconn *vconn, struct ofpbuf *msg)
591 {
592     int retval;
593
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);
599     } else {
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);
605         }
606         free(s);
607     }
608     return retval;
609 }
610
611 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
612 int
613 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
614 {
615     int retval;
616
617     fatal_signal_run();
618
619     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
620         vconn_run(vconn);
621         vconn_run_wait(vconn);
622         vconn_send_wait(vconn);
623         poll_block();
624     }
625     return retval;
626 }
627
628 /* Same as vconn_recv, except that it waits until a message is received. */
629 int
630 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
631 {
632     int retval;
633
634     fatal_signal_run();
635
636     while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
637         vconn_run(vconn);
638         vconn_run_wait(vconn);
639         vconn_recv_wait(vconn);
640         poll_block();
641     }
642     return retval;
643 }
644
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.
649  *
650  * 'request' is always destroyed, regardless of the return value. */
651 int
652 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
653 {
654     for (;;) {
655         uint32_t recv_xid;
656         struct ofpbuf *reply;
657         int error;
658
659         error = vconn_recv_block(vconn, &reply);
660         if (error) {
661             *replyp = NULL;
662             return error;
663         }
664         recv_xid = ((struct ofp_header *) reply->data)->xid;
665         if (xid == recv_xid) {
666             *replyp = reply;
667             return 0;
668         }
669
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);
673     }
674 }
675
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.
680  *
681  * 'request' is always destroyed, regardless of the return value. */
682 int
683 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
684                struct ofpbuf **replyp)
685 {
686     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
687     int error;
688
689     *replyp = NULL;
690     error = vconn_send_block(vconn, request);
691     if (error) {
692         ofpbuf_delete(request);
693     }
694     return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
695 }
696
697 void
698 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
699 {
700     assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
701
702     switch (vconn->state) {
703     case VCS_CONNECTING:
704         wait = WAIT_CONNECT;
705         break;
706
707     case VCS_SEND_HELLO:
708     case VCS_SEND_ERROR:
709         wait = WAIT_SEND;
710         break;
711
712     case VCS_RECV_HELLO:
713         wait = WAIT_RECV;
714         break;
715
716     case VCS_CONNECTED:
717         break;
718
719     case VCS_DISCONNECTED:
720         poll_immediate_wake();
721         return;
722     }
723     (vconn->class->wait)(vconn, wait);
724 }
725
726 void
727 vconn_connect_wait(struct vconn *vconn)
728 {
729     vconn_wait(vconn, WAIT_CONNECT);
730 }
731
732 void
733 vconn_recv_wait(struct vconn *vconn)
734 {
735     vconn_wait(vconn, WAIT_RECV);
736 }
737
738 void
739 vconn_send_wait(struct vconn *vconn)
740 {
741     vconn_wait(vconn, WAIT_SEND);
742 }
743
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
747  * class exists. */
748 static int
749 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
750 {
751     size_t prefix_len;
752
753     prefix_len = strcspn(name, ":");
754     if (name[prefix_len] != '\0') {
755         size_t i;
756
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)) {
761                 *classp = class;
762                 return 0;
763             }
764         }
765     }
766
767     *classp = NULL;
768     return EAFNOSUPPORT;
769 }
770
771 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
772  * a supported connection type, otherwise EAFNOSUPPORT.  */
773 int
774 pvconn_verify_name(const char *name)
775 {
776     struct pvconn_class *class;
777     return pvconn_lookup_class(name, &class);
778 }
779
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.
783  *
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
786  * pointer.  */
787 int
788 pvconn_open(const char *name, struct pvconn **pvconnp)
789 {
790     struct pvconn_class *class;
791     struct pvconn *pvconn;
792     char *suffix_copy;
793     int error;
794
795     check_vconn_classes();
796
797     /* Look up the class. */
798     error = pvconn_lookup_class(name, &class);
799     if (!class) {
800         goto error;
801     }
802
803     /* Call class's "open" function. */
804     suffix_copy = xstrdup(strchr(name, ':') + 1);
805     error = class->listen(name, suffix_copy, &pvconn);
806     free(suffix_copy);
807     if (error) {
808         goto error;
809     }
810
811     /* Success. */
812     *pvconnp = pvconn;
813     return 0;
814
815 error:
816     *pvconnp = NULL;
817     return error;
818 }
819
820 /* Returns the name that was used to open 'pvconn'.  The caller must not
821  * modify or free the name. */
822 const char *
823 pvconn_get_name(const struct pvconn *pvconn)
824 {
825     return pvconn->name;
826 }
827
828 /* Closes 'pvconn'. */
829 void
830 pvconn_close(struct pvconn *pvconn)
831 {
832     if (pvconn != NULL) {
833         char *name = pvconn->name;
834         (pvconn->class->close)(pvconn);
835         free(name);
836     }
837 }
838
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
841  * errno value.
842  *
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.
846  *
847  * pvconn_accept() will not block waiting for a connection.  If no connection
848  * is ready to be accepted, it returns EAGAIN immediately. */
849 int
850 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
851 {
852     int retval = (pvconn->class->accept)(pvconn, new_vconn);
853     if (retval) {
854         *new_vconn = NULL;
855     } else {
856         assert((*new_vconn)->state != VCS_CONNECTING
857                || (*new_vconn)->class->connect);
858         (*new_vconn)->min_version = min_version;
859     }
860     return retval;
861 }
862
863 void
864 pvconn_wait(struct pvconn *pvconn)
865 {
866     (pvconn->class->wait)(pvconn);
867 }
868
869 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
870  * The initial connection status, supplied as 'connect_status', is interpreted
871  * as follows:
872  *
873  *      - 0: 'vconn' is connected.  Its 'send' and 'recv' functions may be
874  *        called in the normal fashion.
875  *
876  *      - EAGAIN: 'vconn' is trying to complete a connection.  Its 'connect'
877  *        function should be called to complete the connection.
878  *
879  *      - Other positive errno values indicate that the connection failed with
880  *        the specified error.
881  *
882  * After calling this function, vconn_close() must be used to destroy 'vconn',
883  * otherwise resources will be leaked.
884  *
885  * The caller retains ownership of 'name'. */
886 void
887 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
888            const char *name)
889 {
890     vconn->class = class;
891     vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
892                     : !connect_status ? VCS_SEND_HELLO
893                     : VCS_DISCONNECTED);
894     vconn->error = connect_status;
895     vconn->version = -1;
896     vconn->min_version = -1;
897     vconn->remote_ip = 0;
898     vconn->remote_port = 0;
899     vconn->local_ip = 0;
900     vconn->local_port = 0;
901     vconn->name = xstrdup(name);
902     assert(vconn->state != VCS_CONNECTING || class->connect);
903 }
904
905 void
906 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
907 {
908     vconn->remote_ip = ip;
909 }
910
911 void
912 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
913 {
914     vconn->remote_port = port;
915 }
916
917 void 
918 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
919 {
920     vconn->local_ip = ip;
921 }
922
923 void 
924 vconn_set_local_port(struct vconn *vconn, uint16_t port)
925 {
926     vconn->local_port = port;
927 }
928
929 void
930 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
931             const char *name)
932 {
933     pvconn->class = class;
934     pvconn->name = xstrdup(name);
935 }