]> git.proxmox.com Git - ovs.git/blame - lib/vconn.c
vlog: Make client supply semicolon for VLOG_DEFINE_THIS_MODULE.
[ovs.git] / lib / vconn.c
CommitLineData
064af421 1/*
60cb3eb8 2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
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"
b302749b 28#include "fatal-signal.h"
064af421
BP
29#include "flow.h"
30#include "ofp-print.h"
fa37b408 31#include "ofp-util.h"
064af421
BP
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"
064af421
BP
39#include "vlog.h"
40
d98e6007 41VLOG_DEFINE_THIS_MODULE(vconn);
5136ce49 42
064af421
BP
43/* State of an active vconn.*/
44enum 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
56static 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
64static 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. */
75static 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. */
79static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
80
81static int do_recv(struct vconn *, struct ofpbuf **);
82static int do_send(struct vconn *, struct ofpbuf *);
83
84/* Check the validity of the vconn class structures. */
85static void
86check_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);
60cb3eb8
BP
95 if (class->close || class->recv || class->send
96 || class->run || class->run_wait || class->wait) {
064af421
BP
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. */
124void
67a4917b 125vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
064af421
BP
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. */
d295e8e9 130
064af421
BP
131 printf("\n");
132 if (active) {
133 printf("Active OpenFlow connection methods:\n");
2b35e147
BP
134 printf(" tcp:IP[:PORT] "
135 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
064af421 136#ifdef HAVE_OPENSSL
2b35e147
BP
137 printf(" ssl:IP[:PORT] "
138 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
064af421
BP
139#endif
140 printf(" unix:FILE Unix domain socket named FILE\n");
141 }
142
143 if (passive) {
144 printf("Passive OpenFlow connection methods:\n");
78ff0270
BP
145 printf(" ptcp:[PORT][:IP] "
146 "listen to TCP PORT (default: %d) on IP\n",
064af421
BP
147 OFP_TCP_PORT);
148#ifdef HAVE_OPENSSL
78ff0270
BP
149 printf(" pssl:[PORT][:IP] "
150 "listen for SSL on PORT (default: %d) on IP\n",
064af421
BP
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
30012c72
BP
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. */
173static int
174vconn_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. */
198int
199vconn_verify_name(const char *name)
200{
201 struct vconn_class *class;
202 return vconn_lookup_class(name, &class);
203}
204
064af421
BP
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. */
216int
217vconn_open(const char *name, int min_version, struct vconn **vconnp)
218{
30012c72
BP
219 struct vconn_class *class;
220 struct vconn *vconn;
221 char *suffix_copy;
222 int error;
064af421
BP
223
224 COVERAGE_INC(vconn_open);
225 check_vconn_classes();
226
30012c72
BP
227 /* Look up the class. */
228 error = vconn_lookup_class(name, &class);
229 if (!class) {
230 goto error;
064af421 231 }
30012c72
BP
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;
064af421 239 }
30012c72
BP
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
247error:
248 *vconnp = NULL;
249 return error;
064af421
BP
250}
251
60cb3eb8
BP
252/* Allows 'vconn' to perform maintenance activities, such as flushing output
253 * buffers. */
254void
255vconn_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. */
264void
265vconn_run_wait(struct vconn *vconn)
266{
267 if (vconn->class->run_wait) {
268 (vconn->class->run_wait)(vconn);
269 }
270}
271
064af421
BP
272int
273vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
274{
275 struct vconn *vconn;
276 int error;
277
b302749b
BP
278 fatal_signal_run();
279
064af421 280 error = vconn_open(name, min_version, &vconn);
b0bfeb3e
BP
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 }
064af421
BP
288 assert(error != EINPROGRESS);
289 }
b0bfeb3e 290
064af421
BP
291 if (error) {
292 vconn_close(vconn);
293 *vconnp = NULL;
294 } else {
295 *vconnp = vconn;
296 }
297 return error;
298}
299
300/* Closes 'vconn'. */
301void
302vconn_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(). */
312const char *
313vconn_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. */
320uint32_t
d295e8e9 321vconn_get_remote_ip(const struct vconn *vconn)
064af421 322{
193456d5
JP
323 return vconn->remote_ip;
324}
325
d295e8e9 326/* Returns the transport port of the peer, or 0 if the connection does not
193456d5
JP
327 * contain a port or if the port is not yet known. */
328uint16_t
d295e8e9 329vconn_get_remote_port(const struct vconn *vconn)
193456d5
JP
330{
331 return vconn->remote_port;
332}
333
d295e8e9
JP
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
193456d5
JP
336 * yet known. */
337uint32_t
d295e8e9 338vconn_get_local_ip(const struct vconn *vconn)
193456d5
JP
339{
340 return vconn->local_ip;
341}
342
d295e8e9 343/* Returns the transport port used to connect to the peer, or 0 if the
193456d5
JP
344 * connection does not contain a port or if the port is not yet known. */
345uint16_t
d295e8e9 346vconn_get_local_port(const struct vconn *vconn)
193456d5
JP
347{
348 return vconn->local_port;
064af421
BP
349}
350
351static void
d295e8e9 352vcs_connecting(struct vconn *vconn)
064af421
BP
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
364static void
365vcs_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
383static void
384vcs_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;
b7eae257 434 vconn->error = retval == EOF ? ECONNRESET : retval;
064af421
BP
435 }
436}
437
438static void
439vcs_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
294e9fc8
BP
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
467 * EAGAIN. */
064af421
BP
468int
469vconn_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
294e9fc8
BP
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.
064af421
BP
512 *
513 * vconn_recv will not block waiting for a packet to arrive. If no packets
514 * have been received, it returns EAGAIN immediately. */
515int
516vconn_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
525static int
526do_recv(struct vconn *vconn, struct ofpbuf **msgp)
527{
5fe577eb 528 int retval = (vconn->class->recv)(vconn, msgp);
064af421
BP
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) {
064af421
BP
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
294e9fc8
BP
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.
064af421
BP
572 *
573 * Returns a positive errno value on failure, in which case the caller
574 * retains ownership of 'msg'.
575 *
576 * vconn_send will not block. If 'msg' cannot be immediately accepted for
577 * transmission, it returns EAGAIN immediately. */
578int
579vconn_send(struct vconn *vconn, struct ofpbuf *msg)
580{
581 int retval = vconn_connect(vconn);
582 if (!retval) {
583 retval = do_send(vconn, msg);
584 }
585 return retval;
586}
587
588static int
589do_send(struct vconn *vconn, struct ofpbuf *msg)
590{
591 int retval;
592
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);
598 } else {
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);
604 }
605 free(s);
606 }
607 return retval;
608}
609
610/* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
611int
612vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
613{
614 int retval;
b302749b
BP
615
616 fatal_signal_run();
617
064af421 618 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
60cb3eb8
BP
619 vconn_run(vconn);
620 vconn_run_wait(vconn);
064af421
BP
621 vconn_send_wait(vconn);
622 poll_block();
623 }
624 return retval;
625}
626
627/* Same as vconn_recv, except that it waits until a message is received. */
628int
629vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
630{
631 int retval;
b302749b
BP
632
633 fatal_signal_run();
634
064af421 635 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
60cb3eb8
BP
636 vconn_run(vconn);
637 vconn_run_wait(vconn);
064af421
BP
638 vconn_recv_wait(vconn);
639 poll_block();
640 }
641 return retval;
642}
643
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.
648 *
649 * 'request' is always destroyed, regardless of the return value. */
650int
651vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
652{
653 for (;;) {
654 uint32_t recv_xid;
655 struct ofpbuf *reply;
656 int error;
657
658 error = vconn_recv_block(vconn, &reply);
659 if (error) {
660 *replyp = NULL;
661 return error;
662 }
663 recv_xid = ((struct ofp_header *) reply->data)->xid;
664 if (xid == recv_xid) {
665 *replyp = reply;
666 return 0;
667 }
668
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);
672 }
673}
674
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.
679 *
680 * 'request' is always destroyed, regardless of the return value. */
681int
682vconn_transact(struct vconn *vconn, struct ofpbuf *request,
683 struct ofpbuf **replyp)
684{
685 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
686 int error;
687
688 *replyp = NULL;
689 error = vconn_send_block(vconn, request);
690 if (error) {
691 ofpbuf_delete(request);
692 }
693 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
694}
695
696void
697vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
698{
699 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
700
701 switch (vconn->state) {
702 case VCS_CONNECTING:
703 wait = WAIT_CONNECT;
704 break;
705
706 case VCS_SEND_HELLO:
707 case VCS_SEND_ERROR:
708 wait = WAIT_SEND;
709 break;
710
711 case VCS_RECV_HELLO:
712 wait = WAIT_RECV;
713 break;
714
715 case VCS_CONNECTED:
716 break;
717
718 case VCS_DISCONNECTED:
719 poll_immediate_wake();
720 return;
721 }
722 (vconn->class->wait)(vconn, wait);
723}
724
725void
726vconn_connect_wait(struct vconn *vconn)
727{
728 vconn_wait(vconn, WAIT_CONNECT);
729}
730
731void
732vconn_recv_wait(struct vconn *vconn)
733{
734 vconn_wait(vconn, WAIT_RECV);
735}
736
737void
738vconn_send_wait(struct vconn *vconn)
739{
740 vconn_wait(vconn, WAIT_SEND);
741}
742
30012c72
BP
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
746 * class exists. */
747static int
748pvconn_lookup_class(const char *name, struct pvconn_class **classp)
749{
750 size_t prefix_len;
751
752 prefix_len = strcspn(name, ":");
753 if (name[prefix_len] != '\0') {
754 size_t i;
755
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)) {
760 *classp = class;
761 return 0;
762 }
763 }
764 }
765
766 *classp = NULL;
767 return EAFNOSUPPORT;
768}
769
770/* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
771 * a supported connection type, otherwise EAFNOSUPPORT. */
772int
773pvconn_verify_name(const char *name)
774{
775 struct pvconn_class *class;
776 return pvconn_lookup_class(name, &class);
777}
778
064af421
BP
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.
782 *
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
785 * pointer. */
786int
787pvconn_open(const char *name, struct pvconn **pvconnp)
788{
30012c72
BP
789 struct pvconn_class *class;
790 struct pvconn *pvconn;
791 char *suffix_copy;
792 int error;
064af421
BP
793
794 check_vconn_classes();
795
30012c72
BP
796 /* Look up the class. */
797 error = pvconn_lookup_class(name, &class);
798 if (!class) {
799 goto error;
064af421 800 }
30012c72
BP
801
802 /* Call class's "open" function. */
803 suffix_copy = xstrdup(strchr(name, ':') + 1);
804 error = class->listen(name, suffix_copy, &pvconn);
805 free(suffix_copy);
806 if (error) {
807 goto error;
064af421 808 }
30012c72
BP
809
810 /* Success. */
811 *pvconnp = pvconn;
812 return 0;
813
814error:
815 *pvconnp = NULL;
816 return error;
064af421
BP
817}
818
819/* Returns the name that was used to open 'pvconn'. The caller must not
820 * modify or free the name. */
821const char *
822pvconn_get_name(const struct pvconn *pvconn)
823{
824 return pvconn->name;
825}
826
827/* Closes 'pvconn'. */
828void
829pvconn_close(struct pvconn *pvconn)
830{
831 if (pvconn != NULL) {
832 char *name = pvconn->name;
833 (pvconn->class->close)(pvconn);
834 free(name);
835 }
836}
837
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
840 * errno value.
841 *
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.
845 *
846 * pvconn_accept() will not block waiting for a connection. If no connection
847 * is ready to be accepted, it returns EAGAIN immediately. */
848int
849pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
850{
851 int retval = (pvconn->class->accept)(pvconn, new_vconn);
852 if (retval) {
853 *new_vconn = NULL;
854 } else {
855 assert((*new_vconn)->state != VCS_CONNECTING
856 || (*new_vconn)->class->connect);
857 (*new_vconn)->min_version = min_version;
858 }
859 return retval;
860}
861
862void
863pvconn_wait(struct pvconn *pvconn)
864{
865 (pvconn->class->wait)(pvconn);
866}
867
85ab0a02
BP
868/* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
869 * The initial connection status, supplied as 'connect_status', is interpreted
870 * as follows:
871 *
872 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
873 * called in the normal fashion.
874 *
875 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
876 * function should be called to complete the connection.
877 *
878 * - Other positive errno values indicate that the connection failed with
879 * the specified error.
880 *
881 * After calling this function, vconn_close() must be used to destroy 'vconn',
882 * otherwise resources will be leaked.
883 *
884 * The caller retains ownership of 'name'. */
064af421
BP
885void
886vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
85ab0a02 887 const char *name)
064af421
BP
888{
889 vconn->class = class;
890 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
891 : !connect_status ? VCS_SEND_HELLO
892 : VCS_DISCONNECTED);
893 vconn->error = connect_status;
894 vconn->version = -1;
895 vconn->min_version = -1;
d7cca867
BP
896 vconn->remote_ip = 0;
897 vconn->remote_port = 0;
193456d5
JP
898 vconn->local_ip = 0;
899 vconn->local_port = 0;
064af421 900 vconn->name = xstrdup(name);
e0668bd1 901 assert(vconn->state != VCS_CONNECTING || class->connect);
064af421
BP
902}
903
d7cca867
BP
904void
905vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
906{
907 vconn->remote_ip = ip;
908}
909
910void
911vconn_set_remote_port(struct vconn *vconn, uint16_t port)
912{
913 vconn->remote_port = port;
914}
915
d295e8e9 916void
193456d5
JP
917vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
918{
919 vconn->local_ip = ip;
920}
921
d295e8e9 922void
193456d5
JP
923vconn_set_local_port(struct vconn *vconn, uint16_t port)
924{
925 vconn->local_port = port;
926}
927
064af421
BP
928void
929pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
930 const char *name)
931{
932 pvconn->class = class;
933 pvconn->name = xstrdup(name);
934}