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 "stream-provider.h"
22 #include <netinet/in.h>
27 #include "dynamic-string.h"
29 #include "ofp-print.h"
31 #include "openflow/nicira-ext.h"
32 #include "openflow/openflow.h"
34 #include "poll-loop.h"
38 #define THIS_MODULE VLM_stream
41 /* State of an active stream.*/
43 SCS_CONNECTING, /* Underlying stream is not connected. */
44 SCS_CONNECTED, /* Connection established. */
45 SCS_DISCONNECTED /* Connection failed or connection closed. */
48 static struct stream_class *stream_classes[] = {
56 static struct pstream_class *pstream_classes[] = {
64 /* Check the validity of the stream class structures. */
66 check_stream_classes(void)
71 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
72 struct stream_class *class = stream_classes[i];
73 assert(class->name != NULL);
74 assert(class->open != NULL);
75 if (class->close || class->recv || class->send || class->run
76 || class->run_wait || class->wait) {
77 assert(class->close != NULL);
78 assert(class->recv != NULL);
79 assert(class->send != NULL);
80 assert(class->wait != NULL);
82 /* This class delegates to another one. */
86 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
87 struct pstream_class *class = pstream_classes[i];
88 assert(class->name != NULL);
89 assert(class->listen != NULL);
90 if (class->close || class->accept || class->wait) {
91 assert(class->close != NULL);
92 assert(class->accept != NULL);
93 assert(class->wait != NULL);
95 /* This class delegates to another one. */
101 /* Prints information on active (if 'active') and passive (if 'passive')
102 * connection methods supported by the stream. */
104 stream_usage(const char *name, bool active, bool passive,
105 bool bootstrap OVS_UNUSED)
107 /* Really this should be implemented via callbacks into the stream
108 * providers, but that seems too heavy-weight to bother with at the
113 printf("Active %s connection methods:\n", name);
114 printf(" tcp:IP:PORT "
115 "PORT at remote IP\n");
117 printf(" ssl:IP:PORT "
118 "SSL PORT at remote IP\n");
121 "Unix domain socket named FILE\n");
125 printf("Passive %s connection methods:\n", name);
126 printf(" ptcp:PORT[:IP] "
127 "listen to TCP PORT on IP\n");
129 printf(" pssl:PORT[:IP] "
130 "listen for SSL on PORT on IP\n");
132 printf(" punix:FILE "
133 "listen on Unix domain socket FILE\n");
137 printf("PKI configuration (required to use SSL):\n"
138 " -p, --private-key=FILE file with private key\n"
139 " -c, --certificate=FILE file with certificate for private key\n"
140 " -C, --ca-cert=FILE file with peer CA certificate\n");
142 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
143 "to read or create\n");
148 /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class
149 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
150 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
153 stream_lookup_class(const char *name, struct stream_class **classp)
158 check_stream_classes();
161 prefix_len = strcspn(name, ":");
162 if (name[prefix_len] == '\0') {
165 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
166 struct stream_class *class = stream_classes[i];
167 if (strlen(class->name) == prefix_len
168 && !memcmp(class->name, name, prefix_len)) {
176 /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is
177 * a supported stream type, otherwise EAFNOSUPPORT. */
179 stream_verify_name(const char *name)
181 struct stream_class *class;
182 return stream_lookup_class(name, &class);
185 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
186 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
187 * ARGS are stream class-specific.
189 * Returns 0 if successful, otherwise a positive errno value. If successful,
190 * stores a pointer to the new connection in '*streamp', otherwise a null
193 stream_open(const char *name, struct stream **streamp)
195 struct stream_class *class;
196 struct stream *stream;
200 COVERAGE_INC(stream_open);
202 /* Look up the class. */
203 error = stream_lookup_class(name, &class);
208 /* Call class's "open" function. */
209 suffix_copy = xstrdup(strchr(name, ':') + 1);
210 error = class->open(name, suffix_copy, &stream);
225 /* Blocks until a previously started stream connection attempt succeeds or
226 * fails. 'error' should be the value returned by stream_open() and 'streamp'
227 * should point to the stream pointer set by stream_open(). Returns 0 if
228 * successful, otherwise a positive errno value other than EAGAIN or
229 * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes
230 * '*streamp' and sets '*streamp' to null.
233 * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
236 stream_open_block(int error, struct stream **streamp)
238 struct stream *stream = *streamp;
240 while (error == EAGAIN) {
242 stream_run_wait(stream);
243 stream_connect_wait(stream);
245 error = stream_connect(stream);
246 assert(error != EINPROGRESS);
249 stream_close(stream);
257 /* Closes 'stream'. */
259 stream_close(struct stream *stream)
261 if (stream != NULL) {
262 char *name = stream->name;
263 (stream->class->close)(stream);
268 /* Returns the name of 'stream', that is, the string passed to
271 stream_get_name(const struct stream *stream)
273 return stream ? stream->name : "(null)";
276 /* Returns the IP address of the peer, or 0 if the peer is not connected over
277 * an IP-based protocol or if its IP address is not yet known. */
279 stream_get_remote_ip(const struct stream *stream)
281 return stream->remote_ip;
284 /* Returns the transport port of the peer, or 0 if the connection does not
285 * contain a port or if the port is not yet known. */
287 stream_get_remote_port(const struct stream *stream)
289 return stream->remote_port;
292 /* Returns the IP address used to connect to the peer, or 0 if the connection
293 * is not an IP-based protocol or if its IP address is not yet known. */
295 stream_get_local_ip(const struct stream *stream)
297 return stream->local_ip;
300 /* Returns the transport port used to connect to the peer, or 0 if the
301 * connection does not contain a port or if the port is not yet known. */
303 stream_get_local_port(const struct stream *stream)
305 return stream->local_port;
309 scs_connecting(struct stream *stream)
311 int retval = (stream->class->connect)(stream);
312 assert(retval != EINPROGRESS);
314 stream->state = SCS_CONNECTED;
315 } else if (retval != EAGAIN) {
316 stream->state = SCS_DISCONNECTED;
317 stream->error = retval;
321 /* Tries to complete the connection on 'stream', which must be an active
322 * stream. If 'stream''s connection is complete, returns 0 if the connection
323 * was successful or a positive errno value if it failed. If the
324 * connection is still in progress, returns EAGAIN. */
326 stream_connect(struct stream *stream)
328 enum stream_state last_state;
331 last_state = stream->state;
332 switch (stream->state) {
334 scs_connecting(stream);
340 case SCS_DISCONNECTED:
341 return stream->error;
346 } while (stream->state != last_state);
351 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
353 * - If successful, the number of bytes received (between 1 and 'n').
355 * - On error, a negative errno value.
357 * - 0, if the connection has been closed in the normal fashion, or if 'n'
360 * The recv function will not block waiting for a packet to arrive. If no
361 * data have been received, it returns -EAGAIN immediately. */
363 stream_recv(struct stream *stream, void *buffer, size_t n)
365 int retval = stream_connect(stream);
366 return (retval ? -retval
368 : (stream->class->recv)(stream, buffer, n));
371 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
373 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
374 * only a valid return value if 'n' is 0.
376 * - On error, a negative errno value.
378 * The send function will not block. If no bytes can be immediately accepted
379 * for transmission, it returns -EAGAIN immediately. */
381 stream_send(struct stream *stream, const void *buffer, size_t n)
383 int retval = stream_connect(stream);
384 return (retval ? -retval
386 : (stream->class->send)(stream, buffer, n));
389 /* Allows 'stream' to perform maintenance activities, such as flushing
392 stream_run(struct stream *stream)
394 if (stream->class->run) {
395 (stream->class->run)(stream);
399 /* Arranges for the poll loop to wake up when 'stream' needs to perform
400 * maintenance activities. */
402 stream_run_wait(struct stream *stream)
404 if (stream->class->run_wait) {
405 (stream->class->run_wait)(stream);
409 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
410 * action of the given 'type'. */
412 stream_wait(struct stream *stream, enum stream_wait_type wait)
414 assert(wait == STREAM_CONNECT || wait == STREAM_RECV
415 || wait == STREAM_SEND);
417 switch (stream->state) {
419 wait = STREAM_CONNECT;
422 case SCS_DISCONNECTED:
423 poll_immediate_wake();
426 (stream->class->wait)(stream, wait);
430 stream_connect_wait(struct stream *stream)
432 stream_wait(stream, STREAM_CONNECT);
436 stream_recv_wait(struct stream *stream)
438 stream_wait(stream, STREAM_RECV);
442 stream_send_wait(struct stream *stream)
444 stream_wait(stream, STREAM_SEND);
447 /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class
448 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
449 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
452 pstream_lookup_class(const char *name, struct pstream_class **classp)
457 check_stream_classes();
460 prefix_len = strcspn(name, ":");
461 if (name[prefix_len] == '\0') {
464 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
465 struct pstream_class *class = pstream_classes[i];
466 if (strlen(class->name) == prefix_len
467 && !memcmp(class->name, name, prefix_len)) {
475 /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
476 * a supported pstream type, otherwise EAFNOSUPPORT. */
478 pstream_verify_name(const char *name)
480 struct pstream_class *class;
481 return pstream_lookup_class(name, &class);
484 /* Attempts to start listening for remote stream connections. 'name' is a
485 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
486 * class's name and ARGS are stream class-specific.
488 * Returns 0 if successful, otherwise a positive errno value. If successful,
489 * stores a pointer to the new connection in '*pstreamp', otherwise a null
492 pstream_open(const char *name, struct pstream **pstreamp)
494 struct pstream_class *class;
495 struct pstream *pstream;
499 COVERAGE_INC(pstream_open);
501 /* Look up the class. */
502 error = pstream_lookup_class(name, &class);
507 /* Call class's "open" function. */
508 suffix_copy = xstrdup(strchr(name, ':') + 1);
509 error = class->listen(name, suffix_copy, &pstream);
524 /* Returns the name that was used to open 'pstream'. The caller must not
525 * modify or free the name. */
527 pstream_get_name(const struct pstream *pstream)
529 return pstream->name;
532 /* Closes 'pstream'. */
534 pstream_close(struct pstream *pstream)
536 if (pstream != NULL) {
537 char *name = pstream->name;
538 (pstream->class->close)(pstream);
543 /* Tries to accept a new connection on 'pstream'. If successful, stores the
544 * new connection in '*new_stream' and returns 0. Otherwise, returns a
545 * positive errno value.
547 * pstream_accept() will not block waiting for a connection. If no connection
548 * is ready to be accepted, it returns EAGAIN immediately. */
550 pstream_accept(struct pstream *pstream, struct stream **new_stream)
552 int retval = (pstream->class->accept)(pstream, new_stream);
556 assert((*new_stream)->state != SCS_CONNECTING
557 || (*new_stream)->class->connect);
562 /* Tries to accept a new connection on 'pstream'. If successful, stores the
563 * new connection in '*new_stream' and returns 0. Otherwise, returns a
564 * positive errno value.
566 * pstream_accept_block() blocks until a connection is ready or until an error
567 * occurs. It will not return EAGAIN. */
569 pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
573 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
574 pstream_wait(pstream);
584 pstream_wait(struct pstream *pstream)
586 (pstream->class->wait)(pstream);
589 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
590 * The initial connection status, supplied as 'connect_status', is interpreted
593 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
594 * called in the normal fashion.
596 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
597 * function should be called to complete the connection.
599 * - Other positive errno values indicate that the connection failed with
600 * the specified error.
602 * After calling this function, stream_close() must be used to destroy
603 * 'stream', otherwise resources will be leaked.
605 * The caller retains ownership of 'name'. */
607 stream_init(struct stream *stream, struct stream_class *class,
608 int connect_status, const char *name)
610 stream->class = class;
611 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
612 : !connect_status ? SCS_CONNECTED
614 stream->error = connect_status;
615 stream->name = xstrdup(name);
616 assert(stream->state != SCS_CONNECTING || class->connect);
620 stream_set_remote_ip(struct stream *stream, uint32_t ip)
622 stream->remote_ip = ip;
626 stream_set_remote_port(struct stream *stream, uint16_t port)
628 stream->remote_port = port;
632 stream_set_local_ip(struct stream *stream, uint32_t ip)
634 stream->local_ip = ip;
638 stream_set_local_port(struct stream *stream, uint16_t port)
640 stream->local_port = port;
644 pstream_init(struct pstream *pstream, struct pstream_class *class,
647 pstream->class = class;
648 pstream->name = xstrdup(name);