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)
100 /* Really this should be implemented via callbacks into the stream
101 * providers, but that seems too heavy-weight to bother with at the
106 printf("Active %s connection methods:\n", name);
107 printf(" tcp:IP:PORT "
108 "PORT at remote IP\n");
110 "Unix domain socket named FILE\n");
114 printf("Passive %s connection methods:\n", name);
115 printf(" ptcp:PORT[:IP] "
116 "listen to TCP PORT on IP\n");
117 printf(" punix:FILE "
118 "listen on Unix domain socket FILE\n");
122 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
123 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
124 * ARGS are stream class-specific.
126 * Returns 0 if successful, otherwise a positive errno value. If successful,
127 * stores a pointer to the new connection in '*streamp', otherwise a null
130 stream_open(const char *name, struct stream **streamp)
135 COVERAGE_INC(stream_open);
136 check_stream_classes();
139 prefix_len = strcspn(name, ":");
140 if (prefix_len == strlen(name)) {
143 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
144 struct stream_class *class = stream_classes[i];
145 if (strlen(class->name) == prefix_len
146 && !memcmp(class->name, name, prefix_len)) {
147 struct stream *stream;
148 char *suffix_copy = xstrdup(name + prefix_len + 1);
149 int retval = class->open(name, suffix_copy, &stream);
152 assert(stream->state != SCS_CONNECTING
153 || stream->class->connect);
163 stream_open_block(const char *name, struct stream **streamp)
165 struct stream *stream;
168 error = stream_open(name, &stream);
169 while (error == EAGAIN) {
171 stream_run_wait(stream);
172 stream_connect_wait(stream);
174 error = stream_connect(stream);
175 assert(error != EINPROGRESS);
178 stream_close(stream);
186 /* Closes 'stream'. */
188 stream_close(struct stream *stream)
190 if (stream != NULL) {
191 char *name = stream->name;
192 (stream->class->close)(stream);
197 /* Returns the name of 'stream', that is, the string passed to
200 stream_get_name(const struct stream *stream)
202 return stream ? stream->name : "(null)";
205 /* Returns the IP address of the peer, or 0 if the peer is not connected over
206 * an IP-based protocol or if its IP address is not yet known. */
208 stream_get_remote_ip(const struct stream *stream)
210 return stream->remote_ip;
213 /* Returns the transport port of the peer, or 0 if the connection does not
214 * contain a port or if the port is not yet known. */
216 stream_get_remote_port(const struct stream *stream)
218 return stream->remote_port;
221 /* Returns the IP address used to connect to the peer, or 0 if the connection
222 * is not an IP-based protocol or if its IP address is not yet known. */
224 stream_get_local_ip(const struct stream *stream)
226 return stream->local_ip;
229 /* Returns the transport port used to connect to the peer, or 0 if the
230 * connection does not contain a port or if the port is not yet known. */
232 stream_get_local_port(const struct stream *stream)
234 return stream->local_port;
238 scs_connecting(struct stream *stream)
240 int retval = (stream->class->connect)(stream);
241 assert(retval != EINPROGRESS);
243 stream->state = SCS_CONNECTED;
244 } else if (retval != EAGAIN) {
245 stream->state = SCS_DISCONNECTED;
246 stream->error = retval;
250 /* Tries to complete the connection on 'stream', which must be an active
251 * stream. If 'stream''s connection is complete, returns 0 if the connection
252 * was successful or a positive errno value if it failed. If the
253 * connection is still in progress, returns EAGAIN. */
255 stream_connect(struct stream *stream)
257 enum stream_state last_state;
260 last_state = stream->state;
261 switch (stream->state) {
263 scs_connecting(stream);
269 case SCS_DISCONNECTED:
270 return stream->error;
275 } while (stream->state != last_state);
280 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
282 * - If successful, the number of bytes received (between 1 and 'n').
284 * - On error, a negative errno value.
286 * - 0, if the connection has been closed in the normal fashion, or if 'n'
289 * The recv function will not block waiting for a packet to arrive. If no
290 * data have been received, it returns -EAGAIN immediately. */
292 stream_recv(struct stream *stream, void *buffer, size_t n)
294 int retval = stream_connect(stream);
295 return (retval ? -retval
297 : (stream->class->recv)(stream, buffer, n));
300 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
302 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
303 * only a valid return value if 'n' is 0.
305 * - On error, a negative errno value.
307 * The send function will not block. If no bytes can be immediately accepted
308 * for transmission, it returns -EAGAIN immediately. */
310 stream_send(struct stream *stream, const void *buffer, size_t n)
312 int retval = stream_connect(stream);
313 return (retval ? -retval
315 : (stream->class->send)(stream, buffer, n));
318 /* Allows 'stream' to perform maintenance activities, such as flushing
321 stream_run(struct stream *stream)
323 if (stream->class->run) {
324 (stream->class->run)(stream);
328 /* Arranges for the poll loop to wake up when 'stream' needs to perform
329 * maintenance activities. */
331 stream_run_wait(struct stream *stream)
333 if (stream->class->run_wait) {
334 (stream->class->run_wait)(stream);
338 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
339 * action of the given 'type'. */
341 stream_wait(struct stream *stream, enum stream_wait_type wait)
343 assert(wait == STREAM_CONNECT || wait == STREAM_RECV
344 || wait == STREAM_SEND);
346 switch (stream->state) {
348 wait = STREAM_CONNECT;
351 case SCS_DISCONNECTED:
352 poll_immediate_wake();
355 (stream->class->wait)(stream, wait);
359 stream_connect_wait(struct stream *stream)
361 stream_wait(stream, STREAM_CONNECT);
365 stream_recv_wait(struct stream *stream)
367 stream_wait(stream, STREAM_RECV);
371 stream_send_wait(struct stream *stream)
373 stream_wait(stream, STREAM_SEND);
376 /* Attempts to start listening for remote stream connections. 'name' is a
377 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
378 * class's name and ARGS are stream class-specific.
380 * Returns 0 if successful, otherwise a positive errno value. If successful,
381 * stores a pointer to the new connection in '*pstreamp', otherwise a null
384 pstream_open(const char *name, struct pstream **pstreamp)
389 check_stream_classes();
392 prefix_len = strcspn(name, ":");
393 if (prefix_len == strlen(name)) {
396 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
397 struct pstream_class *class = pstream_classes[i];
398 if (strlen(class->name) == prefix_len
399 && !memcmp(class->name, name, prefix_len)) {
400 char *suffix_copy = xstrdup(name + prefix_len + 1);
401 int retval = class->listen(name, suffix_copy, pstreamp);
412 /* Returns the name that was used to open 'pstream'. The caller must not
413 * modify or free the name. */
415 pstream_get_name(const struct pstream *pstream)
417 return pstream->name;
420 /* Closes 'pstream'. */
422 pstream_close(struct pstream *pstream)
424 if (pstream != NULL) {
425 char *name = pstream->name;
426 (pstream->class->close)(pstream);
431 /* Tries to accept a new connection on 'pstream'. If successful, stores the
432 * new connection in '*new_stream' and returns 0. Otherwise, returns a
433 * positive errno value.
435 * pstream_accept() will not block waiting for a connection. If no connection
436 * is ready to be accepted, it returns EAGAIN immediately. */
438 pstream_accept(struct pstream *pstream, struct stream **new_stream)
440 int retval = (pstream->class->accept)(pstream, new_stream);
444 assert((*new_stream)->state != SCS_CONNECTING
445 || (*new_stream)->class->connect);
450 /* Tries to accept a new connection on 'pstream'. If successful, stores the
451 * new connection in '*new_stream' and returns 0. Otherwise, returns a
452 * positive errno value.
454 * pstream_accept_block() blocks until a connection is ready or until an error
455 * occurs. It will not return EAGAIN. */
457 pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
461 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
462 pstream_wait(pstream);
472 pstream_wait(struct pstream *pstream)
474 (pstream->class->wait)(pstream);
477 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
478 * The initial connection status, supplied as 'connect_status', is interpreted
481 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
482 * called in the normal fashion.
484 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
485 * function should be called to complete the connection.
487 * - Other positive errno values indicate that the connection failed with
488 * the specified error.
490 * After calling this function, stream_close() must be used to destroy
491 * 'stream', otherwise resources will be leaked.
493 * The caller retains ownership of 'name'. */
495 stream_init(struct stream *stream, struct stream_class *class,
496 int connect_status, const char *name)
498 stream->class = class;
499 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
500 : !connect_status ? SCS_CONNECTED
502 stream->error = connect_status;
503 stream->name = xstrdup(name);
504 assert(stream->state != SCS_CONNECTING || class->connect);
508 stream_set_remote_ip(struct stream *stream, uint32_t ip)
510 stream->remote_ip = ip;
514 stream_set_remote_port(struct stream *stream, uint16_t port)
516 stream->remote_port = port;
520 stream_set_local_ip(struct stream *stream, uint32_t ip)
522 stream->local_ip = ip;
526 stream_set_local_port(struct stream *stream, uint16_t port)
528 stream->local_port = port;
532 pstream_init(struct pstream *pstream, struct pstream_class *class,
535 pstream->class = class;
536 pstream->name = xstrdup(name);