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[] = {
53 static struct pstream_class *pstream_classes[] = {
58 /* Check the validity of the stream class structures. */
60 check_stream_classes(void)
65 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
66 struct stream_class *class = stream_classes[i];
67 assert(class->name != NULL);
68 assert(class->open != NULL);
69 if (class->close || class->recv || class->send || class->run
70 || class->run_wait || class->wait) {
71 assert(class->close != NULL);
72 assert(class->recv != NULL);
73 assert(class->send != NULL);
74 assert(class->wait != NULL);
76 /* This class delegates to another one. */
80 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
81 struct pstream_class *class = pstream_classes[i];
82 assert(class->name != NULL);
83 assert(class->listen != NULL);
84 if (class->close || class->accept || class->wait) {
85 assert(class->close != NULL);
86 assert(class->accept != NULL);
87 assert(class->wait != NULL);
89 /* This class delegates to another one. */
95 /* Prints information on active (if 'active') and passive (if 'passive')
96 * connection methods supported by the stream. */
98 stream_usage(const char *name, bool active, bool passive,
99 bool bootstrap UNUSED)
101 /* Really this should be implemented via callbacks into the stream
102 * providers, but that seems too heavy-weight to bother with at the
107 printf("Active %s connection methods:\n", name);
108 printf(" tcp:IP:PORT "
109 "PORT at remote IP\n");
111 printf(" ssl:IP:PORT "
112 "SSL PORT at remote IP\n");
115 "Unix domain socket named FILE\n");
119 printf("Passive %s connection methods:\n", name);
120 printf(" ptcp:PORT[:IP] "
121 "listen to TCP PORT on IP\n");
123 printf(" pssl:PORT[:IP] "
124 "listen for SSL on PORT on IP\n");
126 printf(" punix:FILE "
127 "listen on Unix domain socket FILE\n");
131 printf("PKI configuration (required to use SSL):\n"
132 " -p, --private-key=FILE file with private key\n"
133 " -c, --certificate=FILE file with certificate for private key\n"
134 " -C, --ca-cert=FILE file with peer CA certificate\n");
136 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
137 "to read or create\n");
142 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
143 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
144 * ARGS are stream class-specific.
146 * Returns 0 if successful, otherwise a positive errno value. If successful,
147 * stores a pointer to the new connection in '*streamp', otherwise a null
150 stream_open(const char *name, struct stream **streamp)
155 COVERAGE_INC(stream_open);
156 check_stream_classes();
159 prefix_len = strcspn(name, ":");
160 if (prefix_len == strlen(name)) {
163 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
164 struct stream_class *class = stream_classes[i];
165 if (strlen(class->name) == prefix_len
166 && !memcmp(class->name, name, prefix_len)) {
167 struct stream *stream;
168 char *suffix_copy = xstrdup(name + prefix_len + 1);
169 int retval = class->open(name, suffix_copy, &stream);
172 assert(stream->state != SCS_CONNECTING
173 || stream->class->connect);
183 stream_open_block(const char *name, struct stream **streamp)
185 struct stream *stream;
188 error = stream_open(name, &stream);
189 while (error == EAGAIN) {
191 stream_run_wait(stream);
192 stream_connect_wait(stream);
194 error = stream_connect(stream);
195 assert(error != EINPROGRESS);
198 stream_close(stream);
206 /* Closes 'stream'. */
208 stream_close(struct stream *stream)
210 if (stream != NULL) {
211 char *name = stream->name;
212 (stream->class->close)(stream);
217 /* Returns the name of 'stream', that is, the string passed to
220 stream_get_name(const struct stream *stream)
222 return stream ? stream->name : "(null)";
225 /* Returns the IP address of the peer, or 0 if the peer is not connected over
226 * an IP-based protocol or if its IP address is not yet known. */
228 stream_get_remote_ip(const struct stream *stream)
230 return stream->remote_ip;
233 /* Returns the transport port of the peer, or 0 if the connection does not
234 * contain a port or if the port is not yet known. */
236 stream_get_remote_port(const struct stream *stream)
238 return stream->remote_port;
241 /* Returns the IP address used to connect to the peer, or 0 if the connection
242 * is not an IP-based protocol or if its IP address is not yet known. */
244 stream_get_local_ip(const struct stream *stream)
246 return stream->local_ip;
249 /* Returns the transport port used to connect to the peer, or 0 if the
250 * connection does not contain a port or if the port is not yet known. */
252 stream_get_local_port(const struct stream *stream)
254 return stream->local_port;
258 scs_connecting(struct stream *stream)
260 int retval = (stream->class->connect)(stream);
261 assert(retval != EINPROGRESS);
263 stream->state = SCS_CONNECTED;
264 } else if (retval != EAGAIN) {
265 stream->state = SCS_DISCONNECTED;
266 stream->error = retval;
270 /* Tries to complete the connection on 'stream', which must be an active
271 * stream. If 'stream''s connection is complete, returns 0 if the connection
272 * was successful or a positive errno value if it failed. If the
273 * connection is still in progress, returns EAGAIN. */
275 stream_connect(struct stream *stream)
277 enum stream_state last_state;
280 last_state = stream->state;
281 switch (stream->state) {
283 scs_connecting(stream);
289 case SCS_DISCONNECTED:
290 return stream->error;
295 } while (stream->state != last_state);
300 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
302 * - If successful, the number of bytes received (between 1 and 'n').
304 * - On error, a negative errno value.
306 * - 0, if the connection has been closed in the normal fashion, or if 'n'
309 * The recv function will not block waiting for a packet to arrive. If no
310 * data have been received, it returns -EAGAIN immediately. */
312 stream_recv(struct stream *stream, void *buffer, size_t n)
314 int retval = stream_connect(stream);
315 return (retval ? -retval
317 : (stream->class->recv)(stream, buffer, n));
320 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
322 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
323 * only a valid return value if 'n' is 0.
325 * - On error, a negative errno value.
327 * The send function will not block. If no bytes can be immediately accepted
328 * for transmission, it returns -EAGAIN immediately. */
330 stream_send(struct stream *stream, const void *buffer, size_t n)
332 int retval = stream_connect(stream);
333 return (retval ? -retval
335 : (stream->class->send)(stream, buffer, n));
338 /* Allows 'stream' to perform maintenance activities, such as flushing
341 stream_run(struct stream *stream)
343 if (stream->class->run) {
344 (stream->class->run)(stream);
348 /* Arranges for the poll loop to wake up when 'stream' needs to perform
349 * maintenance activities. */
351 stream_run_wait(struct stream *stream)
353 if (stream->class->run_wait) {
354 (stream->class->run_wait)(stream);
358 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
359 * action of the given 'type'. */
361 stream_wait(struct stream *stream, enum stream_wait_type wait)
363 assert(wait == STREAM_CONNECT || wait == STREAM_RECV
364 || wait == STREAM_SEND);
366 switch (stream->state) {
368 wait = STREAM_CONNECT;
371 case SCS_DISCONNECTED:
372 poll_immediate_wake();
375 (stream->class->wait)(stream, wait);
379 stream_connect_wait(struct stream *stream)
381 stream_wait(stream, STREAM_CONNECT);
385 stream_recv_wait(struct stream *stream)
387 stream_wait(stream, STREAM_RECV);
391 stream_send_wait(struct stream *stream)
393 stream_wait(stream, STREAM_SEND);
396 /* Attempts to start listening for remote stream connections. 'name' is a
397 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
398 * class's name and ARGS are stream class-specific.
400 * Returns 0 if successful, otherwise a positive errno value. If successful,
401 * stores a pointer to the new connection in '*pstreamp', otherwise a null
404 pstream_open(const char *name, struct pstream **pstreamp)
409 check_stream_classes();
412 prefix_len = strcspn(name, ":");
413 if (prefix_len == strlen(name)) {
416 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
417 struct pstream_class *class = pstream_classes[i];
418 if (strlen(class->name) == prefix_len
419 && !memcmp(class->name, name, prefix_len)) {
420 char *suffix_copy = xstrdup(name + prefix_len + 1);
421 int retval = class->listen(name, suffix_copy, pstreamp);
432 /* Returns the name that was used to open 'pstream'. The caller must not
433 * modify or free the name. */
435 pstream_get_name(const struct pstream *pstream)
437 return pstream->name;
440 /* Closes 'pstream'. */
442 pstream_close(struct pstream *pstream)
444 if (pstream != NULL) {
445 char *name = pstream->name;
446 (pstream->class->close)(pstream);
451 /* Tries to accept a new connection on 'pstream'. If successful, stores the
452 * new connection in '*new_stream' and returns 0. Otherwise, returns a
453 * positive errno value.
455 * pstream_accept() will not block waiting for a connection. If no connection
456 * is ready to be accepted, it returns EAGAIN immediately. */
458 pstream_accept(struct pstream *pstream, struct stream **new_stream)
460 int retval = (pstream->class->accept)(pstream, new_stream);
464 assert((*new_stream)->state != SCS_CONNECTING
465 || (*new_stream)->class->connect);
470 /* Tries to accept a new connection on 'pstream'. If successful, stores the
471 * new connection in '*new_stream' and returns 0. Otherwise, returns a
472 * positive errno value.
474 * pstream_accept_block() blocks until a connection is ready or until an error
475 * occurs. It will not return EAGAIN. */
477 pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
481 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
482 pstream_wait(pstream);
492 pstream_wait(struct pstream *pstream)
494 (pstream->class->wait)(pstream);
497 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
498 * The initial connection status, supplied as 'connect_status', is interpreted
501 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
502 * called in the normal fashion.
504 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
505 * function should be called to complete the connection.
507 * - Other positive errno values indicate that the connection failed with
508 * the specified error.
510 * After calling this function, stream_close() must be used to destroy
511 * 'stream', otherwise resources will be leaked.
513 * The caller retains ownership of 'name'. */
515 stream_init(struct stream *stream, struct stream_class *class,
516 int connect_status, const char *name)
518 stream->class = class;
519 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
520 : !connect_status ? SCS_CONNECTED
522 stream->error = connect_status;
523 stream->name = xstrdup(name);
524 assert(stream->state != SCS_CONNECTING || class->connect);
528 stream_set_remote_ip(struct stream *stream, uint32_t ip)
530 stream->remote_ip = ip;
534 stream_set_remote_port(struct stream *stream, uint16_t port)
536 stream->remote_port = port;
540 stream_set_local_ip(struct stream *stream, uint32_t ip)
542 stream->local_ip = ip;
546 stream_set_local_port(struct stream *stream, uint16_t port)
548 stream->local_port = port;
552 pstream_init(struct pstream *pstream, struct pstream_class *class,
555 pstream->class = class;
556 pstream->name = xstrdup(name);