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 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
149 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
150 * ARGS are stream class-specific.
152 * Returns 0 if successful, otherwise a positive errno value. If successful,
153 * stores a pointer to the new connection in '*streamp', otherwise a null
156 stream_open(const char *name, struct stream **streamp)
161 COVERAGE_INC(stream_open);
162 check_stream_classes();
165 prefix_len = strcspn(name, ":");
166 if (prefix_len == strlen(name)) {
169 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
170 struct stream_class *class = stream_classes[i];
171 if (strlen(class->name) == prefix_len
172 && !memcmp(class->name, name, prefix_len)) {
173 struct stream *stream;
174 char *suffix_copy = xstrdup(name + prefix_len + 1);
175 int retval = class->open(name, suffix_copy, &stream);
178 assert(stream->state != SCS_CONNECTING
179 || stream->class->connect);
189 stream_open_block(const char *name, struct stream **streamp)
191 struct stream *stream;
194 error = stream_open(name, &stream);
195 while (error == EAGAIN) {
197 stream_run_wait(stream);
198 stream_connect_wait(stream);
200 error = stream_connect(stream);
201 assert(error != EINPROGRESS);
204 stream_close(stream);
212 /* Closes 'stream'. */
214 stream_close(struct stream *stream)
216 if (stream != NULL) {
217 char *name = stream->name;
218 (stream->class->close)(stream);
223 /* Returns the name of 'stream', that is, the string passed to
226 stream_get_name(const struct stream *stream)
228 return stream ? stream->name : "(null)";
231 /* Returns the IP address of the peer, or 0 if the peer is not connected over
232 * an IP-based protocol or if its IP address is not yet known. */
234 stream_get_remote_ip(const struct stream *stream)
236 return stream->remote_ip;
239 /* Returns the transport port of the peer, or 0 if the connection does not
240 * contain a port or if the port is not yet known. */
242 stream_get_remote_port(const struct stream *stream)
244 return stream->remote_port;
247 /* Returns the IP address used to connect to the peer, or 0 if the connection
248 * is not an IP-based protocol or if its IP address is not yet known. */
250 stream_get_local_ip(const struct stream *stream)
252 return stream->local_ip;
255 /* Returns the transport port used to connect to the peer, or 0 if the
256 * connection does not contain a port or if the port is not yet known. */
258 stream_get_local_port(const struct stream *stream)
260 return stream->local_port;
264 scs_connecting(struct stream *stream)
266 int retval = (stream->class->connect)(stream);
267 assert(retval != EINPROGRESS);
269 stream->state = SCS_CONNECTED;
270 } else if (retval != EAGAIN) {
271 stream->state = SCS_DISCONNECTED;
272 stream->error = retval;
276 /* Tries to complete the connection on 'stream', which must be an active
277 * stream. If 'stream''s connection is complete, returns 0 if the connection
278 * was successful or a positive errno value if it failed. If the
279 * connection is still in progress, returns EAGAIN. */
281 stream_connect(struct stream *stream)
283 enum stream_state last_state;
286 last_state = stream->state;
287 switch (stream->state) {
289 scs_connecting(stream);
295 case SCS_DISCONNECTED:
296 return stream->error;
301 } while (stream->state != last_state);
306 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
308 * - If successful, the number of bytes received (between 1 and 'n').
310 * - On error, a negative errno value.
312 * - 0, if the connection has been closed in the normal fashion, or if 'n'
315 * The recv function will not block waiting for a packet to arrive. If no
316 * data have been received, it returns -EAGAIN immediately. */
318 stream_recv(struct stream *stream, void *buffer, size_t n)
320 int retval = stream_connect(stream);
321 return (retval ? -retval
323 : (stream->class->recv)(stream, buffer, n));
326 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
328 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
329 * only a valid return value if 'n' is 0.
331 * - On error, a negative errno value.
333 * The send function will not block. If no bytes can be immediately accepted
334 * for transmission, it returns -EAGAIN immediately. */
336 stream_send(struct stream *stream, const void *buffer, size_t n)
338 int retval = stream_connect(stream);
339 return (retval ? -retval
341 : (stream->class->send)(stream, buffer, n));
344 /* Allows 'stream' to perform maintenance activities, such as flushing
347 stream_run(struct stream *stream)
349 if (stream->class->run) {
350 (stream->class->run)(stream);
354 /* Arranges for the poll loop to wake up when 'stream' needs to perform
355 * maintenance activities. */
357 stream_run_wait(struct stream *stream)
359 if (stream->class->run_wait) {
360 (stream->class->run_wait)(stream);
364 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
365 * action of the given 'type'. */
367 stream_wait(struct stream *stream, enum stream_wait_type wait)
369 assert(wait == STREAM_CONNECT || wait == STREAM_RECV
370 || wait == STREAM_SEND);
372 switch (stream->state) {
374 wait = STREAM_CONNECT;
377 case SCS_DISCONNECTED:
378 poll_immediate_wake();
381 (stream->class->wait)(stream, wait);
385 stream_connect_wait(struct stream *stream)
387 stream_wait(stream, STREAM_CONNECT);
391 stream_recv_wait(struct stream *stream)
393 stream_wait(stream, STREAM_RECV);
397 stream_send_wait(struct stream *stream)
399 stream_wait(stream, STREAM_SEND);
402 /* Attempts to start listening for remote stream connections. 'name' is a
403 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
404 * class's name and ARGS are stream class-specific.
406 * Returns 0 if successful, otherwise a positive errno value. If successful,
407 * stores a pointer to the new connection in '*pstreamp', otherwise a null
410 pstream_open(const char *name, struct pstream **pstreamp)
415 check_stream_classes();
418 prefix_len = strcspn(name, ":");
419 if (prefix_len == strlen(name)) {
422 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
423 struct pstream_class *class = pstream_classes[i];
424 if (strlen(class->name) == prefix_len
425 && !memcmp(class->name, name, prefix_len)) {
426 char *suffix_copy = xstrdup(name + prefix_len + 1);
427 int retval = class->listen(name, suffix_copy, pstreamp);
438 /* Returns the name that was used to open 'pstream'. The caller must not
439 * modify or free the name. */
441 pstream_get_name(const struct pstream *pstream)
443 return pstream->name;
446 /* Closes 'pstream'. */
448 pstream_close(struct pstream *pstream)
450 if (pstream != NULL) {
451 char *name = pstream->name;
452 (pstream->class->close)(pstream);
457 /* Tries to accept a new connection on 'pstream'. If successful, stores the
458 * new connection in '*new_stream' and returns 0. Otherwise, returns a
459 * positive errno value.
461 * pstream_accept() will not block waiting for a connection. If no connection
462 * is ready to be accepted, it returns EAGAIN immediately. */
464 pstream_accept(struct pstream *pstream, struct stream **new_stream)
466 int retval = (pstream->class->accept)(pstream, new_stream);
470 assert((*new_stream)->state != SCS_CONNECTING
471 || (*new_stream)->class->connect);
476 /* Tries to accept a new connection on 'pstream'. If successful, stores the
477 * new connection in '*new_stream' and returns 0. Otherwise, returns a
478 * positive errno value.
480 * pstream_accept_block() blocks until a connection is ready or until an error
481 * occurs. It will not return EAGAIN. */
483 pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
487 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
488 pstream_wait(pstream);
498 pstream_wait(struct pstream *pstream)
500 (pstream->class->wait)(pstream);
503 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
504 * The initial connection status, supplied as 'connect_status', is interpreted
507 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
508 * called in the normal fashion.
510 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
511 * function should be called to complete the connection.
513 * - Other positive errno values indicate that the connection failed with
514 * the specified error.
516 * After calling this function, stream_close() must be used to destroy
517 * 'stream', otherwise resources will be leaked.
519 * The caller retains ownership of 'name'. */
521 stream_init(struct stream *stream, struct stream_class *class,
522 int connect_status, const char *name)
524 stream->class = class;
525 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
526 : !connect_status ? SCS_CONNECTED
528 stream->error = connect_status;
529 stream->name = xstrdup(name);
530 assert(stream->state != SCS_CONNECTING || class->connect);
534 stream_set_remote_ip(struct stream *stream, uint32_t ip)
536 stream->remote_ip = ip;
540 stream_set_remote_port(struct stream *stream, uint16_t port)
542 stream->remote_port = port;
546 stream_set_local_ip(struct stream *stream, uint32_t ip)
548 stream->local_ip = ip;
552 stream_set_local_port(struct stream *stream, uint16_t port)
554 stream->local_port = port;
558 pstream_init(struct pstream *pstream, struct pstream_class *class,
561 pstream->class = class;
562 pstream->name = xstrdup(name);