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"
28 #include "fatal-signal.h"
30 #include "ofp-print.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
35 #include "poll-loop.h"
39 #define THIS_MODULE VLM_stream
42 /* State of an active stream.*/
44 SCS_CONNECTING, /* Underlying stream is not connected. */
45 SCS_CONNECTED, /* Connection established. */
46 SCS_DISCONNECTED /* Connection failed or connection closed. */
49 static struct stream_class *stream_classes[] = {
57 static struct pstream_class *pstream_classes[] = {
65 /* Check the validity of the stream class structures. */
67 check_stream_classes(void)
72 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
73 struct stream_class *class = stream_classes[i];
74 assert(class->name != NULL);
75 assert(class->open != NULL);
76 if (class->close || class->recv || class->send || class->run
77 || class->run_wait || class->wait) {
78 assert(class->close != NULL);
79 assert(class->recv != NULL);
80 assert(class->send != NULL);
81 assert(class->wait != NULL);
83 /* This class delegates to another one. */
87 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
88 struct pstream_class *class = pstream_classes[i];
89 assert(class->name != NULL);
90 assert(class->listen != NULL);
91 if (class->close || class->accept || class->wait) {
92 assert(class->close != NULL);
93 assert(class->accept != NULL);
94 assert(class->wait != NULL);
96 /* This class delegates to another one. */
102 /* Prints information on active (if 'active') and passive (if 'passive')
103 * connection methods supported by the stream. */
105 stream_usage(const char *name, bool active, bool passive,
106 bool bootstrap OVS_UNUSED)
108 /* Really this should be implemented via callbacks into the stream
109 * providers, but that seems too heavy-weight to bother with at the
114 printf("Active %s connection methods:\n", name);
115 printf(" tcp:IP:PORT "
116 "PORT at remote IP\n");
118 printf(" ssl:IP:PORT "
119 "SSL PORT at remote IP\n");
122 "Unix domain socket named FILE\n");
126 printf("Passive %s connection methods:\n", name);
127 printf(" ptcp:PORT[:IP] "
128 "listen to TCP PORT on IP\n");
130 printf(" pssl:PORT[:IP] "
131 "listen for SSL on PORT on IP\n");
133 printf(" punix:FILE "
134 "listen on Unix domain socket FILE\n");
138 printf("PKI configuration (required to use SSL):\n"
139 " -p, --private-key=FILE file with private key\n"
140 " -c, --certificate=FILE file with certificate for private key\n"
141 " -C, --ca-cert=FILE file with peer CA certificate\n");
143 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
144 "to read or create\n");
149 /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class
150 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
151 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
154 stream_lookup_class(const char *name, struct stream_class **classp)
159 check_stream_classes();
162 prefix_len = strcspn(name, ":");
163 if (name[prefix_len] == '\0') {
166 for (i = 0; i < ARRAY_SIZE(stream_classes); i++) {
167 struct stream_class *class = stream_classes[i];
168 if (strlen(class->name) == prefix_len
169 && !memcmp(class->name, name, prefix_len)) {
177 /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is
178 * a supported stream type, otherwise EAFNOSUPPORT. */
180 stream_verify_name(const char *name)
182 struct stream_class *class;
183 return stream_lookup_class(name, &class);
186 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
187 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
188 * ARGS are stream class-specific.
190 * Returns 0 if successful, otherwise a positive errno value. If successful,
191 * stores a pointer to the new connection in '*streamp', otherwise a null
194 stream_open(const char *name, struct stream **streamp)
196 struct stream_class *class;
197 struct stream *stream;
201 COVERAGE_INC(stream_open);
203 /* Look up the class. */
204 error = stream_lookup_class(name, &class);
209 /* Call class's "open" function. */
210 suffix_copy = xstrdup(strchr(name, ':') + 1);
211 error = class->open(name, suffix_copy, &stream);
226 /* Blocks until a previously started stream connection attempt succeeds or
227 * fails. 'error' should be the value returned by stream_open() and 'streamp'
228 * should point to the stream pointer set by stream_open(). Returns 0 if
229 * successful, otherwise a positive errno value other than EAGAIN or
230 * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes
231 * '*streamp' and sets '*streamp' to null.
234 * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
237 stream_open_block(int error, struct stream **streamp)
239 struct stream *stream = *streamp;
243 while (error == EAGAIN) {
245 stream_run_wait(stream);
246 stream_connect_wait(stream);
248 error = stream_connect(stream);
249 assert(error != EINPROGRESS);
252 stream_close(stream);
260 /* Closes 'stream'. */
262 stream_close(struct stream *stream)
264 if (stream != NULL) {
265 char *name = stream->name;
266 (stream->class->close)(stream);
271 /* Returns the name of 'stream', that is, the string passed to
274 stream_get_name(const struct stream *stream)
276 return stream ? stream->name : "(null)";
279 /* Returns the IP address of the peer, or 0 if the peer is not connected over
280 * an IP-based protocol or if its IP address is not yet known. */
282 stream_get_remote_ip(const struct stream *stream)
284 return stream->remote_ip;
287 /* Returns the transport port of the peer, or 0 if the connection does not
288 * contain a port or if the port is not yet known. */
290 stream_get_remote_port(const struct stream *stream)
292 return stream->remote_port;
295 /* Returns the IP address used to connect to the peer, or 0 if the connection
296 * is not an IP-based protocol or if its IP address is not yet known. */
298 stream_get_local_ip(const struct stream *stream)
300 return stream->local_ip;
303 /* Returns the transport port used to connect to the peer, or 0 if the
304 * connection does not contain a port or if the port is not yet known. */
306 stream_get_local_port(const struct stream *stream)
308 return stream->local_port;
312 scs_connecting(struct stream *stream)
314 int retval = (stream->class->connect)(stream);
315 assert(retval != EINPROGRESS);
317 stream->state = SCS_CONNECTED;
318 } else if (retval != EAGAIN) {
319 stream->state = SCS_DISCONNECTED;
320 stream->error = retval;
324 /* Tries to complete the connection on 'stream', which must be an active
325 * stream. If 'stream''s connection is complete, returns 0 if the connection
326 * was successful or a positive errno value if it failed. If the
327 * connection is still in progress, returns EAGAIN. */
329 stream_connect(struct stream *stream)
331 enum stream_state last_state;
334 last_state = stream->state;
335 switch (stream->state) {
337 scs_connecting(stream);
343 case SCS_DISCONNECTED:
344 return stream->error;
349 } while (stream->state != last_state);
354 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
356 * - If successful, the number of bytes received (between 1 and 'n').
358 * - On error, a negative errno value.
360 * - 0, if the connection has been closed in the normal fashion, or if 'n'
363 * The recv function will not block waiting for a packet to arrive. If no
364 * data have been received, it returns -EAGAIN immediately. */
366 stream_recv(struct stream *stream, void *buffer, size_t n)
368 int retval = stream_connect(stream);
369 return (retval ? -retval
371 : (stream->class->recv)(stream, buffer, n));
374 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
376 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
377 * only a valid return value if 'n' is 0.
379 * - On error, a negative errno value.
381 * The send function will not block. If no bytes can be immediately accepted
382 * for transmission, it returns -EAGAIN immediately. */
384 stream_send(struct stream *stream, const void *buffer, size_t n)
386 int retval = stream_connect(stream);
387 return (retval ? -retval
389 : (stream->class->send)(stream, buffer, n));
392 /* Allows 'stream' to perform maintenance activities, such as flushing
395 stream_run(struct stream *stream)
397 if (stream->class->run) {
398 (stream->class->run)(stream);
402 /* Arranges for the poll loop to wake up when 'stream' needs to perform
403 * maintenance activities. */
405 stream_run_wait(struct stream *stream)
407 if (stream->class->run_wait) {
408 (stream->class->run_wait)(stream);
412 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
413 * action of the given 'type'. */
415 stream_wait(struct stream *stream, enum stream_wait_type wait)
417 assert(wait == STREAM_CONNECT || wait == STREAM_RECV
418 || wait == STREAM_SEND);
420 switch (stream->state) {
422 wait = STREAM_CONNECT;
425 case SCS_DISCONNECTED:
426 poll_immediate_wake();
429 (stream->class->wait)(stream, wait);
433 stream_connect_wait(struct stream *stream)
435 stream_wait(stream, STREAM_CONNECT);
439 stream_recv_wait(struct stream *stream)
441 stream_wait(stream, STREAM_RECV);
445 stream_send_wait(struct stream *stream)
447 stream_wait(stream, STREAM_SEND);
450 /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class
451 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
452 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
455 pstream_lookup_class(const char *name, struct pstream_class **classp)
460 check_stream_classes();
463 prefix_len = strcspn(name, ":");
464 if (name[prefix_len] == '\0') {
467 for (i = 0; i < ARRAY_SIZE(pstream_classes); i++) {
468 struct pstream_class *class = pstream_classes[i];
469 if (strlen(class->name) == prefix_len
470 && !memcmp(class->name, name, prefix_len)) {
478 /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
479 * a supported pstream type, otherwise EAFNOSUPPORT. */
481 pstream_verify_name(const char *name)
483 struct pstream_class *class;
484 return pstream_lookup_class(name, &class);
487 /* Attempts to start listening for remote stream connections. 'name' is a
488 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
489 * class's name and ARGS are stream class-specific.
491 * Returns 0 if successful, otherwise a positive errno value. If successful,
492 * stores a pointer to the new connection in '*pstreamp', otherwise a null
495 pstream_open(const char *name, struct pstream **pstreamp)
497 struct pstream_class *class;
498 struct pstream *pstream;
502 COVERAGE_INC(pstream_open);
504 /* Look up the class. */
505 error = pstream_lookup_class(name, &class);
510 /* Call class's "open" function. */
511 suffix_copy = xstrdup(strchr(name, ':') + 1);
512 error = class->listen(name, suffix_copy, &pstream);
527 /* Returns the name that was used to open 'pstream'. The caller must not
528 * modify or free the name. */
530 pstream_get_name(const struct pstream *pstream)
532 return pstream->name;
535 /* Closes 'pstream'. */
537 pstream_close(struct pstream *pstream)
539 if (pstream != NULL) {
540 char *name = pstream->name;
541 (pstream->class->close)(pstream);
546 /* Tries to accept a new connection on 'pstream'. If successful, stores the
547 * new connection in '*new_stream' and returns 0. Otherwise, returns a
548 * positive errno value.
550 * pstream_accept() will not block waiting for a connection. If no connection
551 * is ready to be accepted, it returns EAGAIN immediately. */
553 pstream_accept(struct pstream *pstream, struct stream **new_stream)
555 int retval = (pstream->class->accept)(pstream, new_stream);
559 assert((*new_stream)->state != SCS_CONNECTING
560 || (*new_stream)->class->connect);
565 /* Tries to accept a new connection on 'pstream'. If successful, stores the
566 * new connection in '*new_stream' and returns 0. Otherwise, returns a
567 * positive errno value.
569 * pstream_accept_block() blocks until a connection is ready or until an error
570 * occurs. It will not return EAGAIN. */
572 pstream_accept_block(struct pstream *pstream, struct stream **new_stream)
577 while ((error = pstream_accept(pstream, new_stream)) == EAGAIN) {
578 pstream_wait(pstream);
588 pstream_wait(struct pstream *pstream)
590 (pstream->class->wait)(pstream);
593 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
594 * The initial connection status, supplied as 'connect_status', is interpreted
597 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
598 * called in the normal fashion.
600 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
601 * function should be called to complete the connection.
603 * - Other positive errno values indicate that the connection failed with
604 * the specified error.
606 * After calling this function, stream_close() must be used to destroy
607 * 'stream', otherwise resources will be leaked.
609 * The caller retains ownership of 'name'. */
611 stream_init(struct stream *stream, struct stream_class *class,
612 int connect_status, const char *name)
614 stream->class = class;
615 stream->state = (connect_status == EAGAIN ? SCS_CONNECTING
616 : !connect_status ? SCS_CONNECTED
618 stream->error = connect_status;
619 stream->name = xstrdup(name);
620 assert(stream->state != SCS_CONNECTING || class->connect);
624 stream_set_remote_ip(struct stream *stream, uint32_t ip)
626 stream->remote_ip = ip;
630 stream_set_remote_port(struct stream *stream, uint16_t port)
632 stream->remote_port = port;
636 stream_set_local_ip(struct stream *stream, uint32_t ip)
638 stream->local_ip = ip;
642 stream_set_local_port(struct stream *stream, uint16_t port)
644 stream->local_port = port;
648 pstream_init(struct pstream *pstream, struct pstream_class *class,
651 pstream->class = class;
652 pstream->name = xstrdup(name);
656 count_fields(const char *s_)
658 char *s, *field, *save_ptr;
663 for (field = strtok_r(s, ":", &save_ptr); field != NULL;
664 field = strtok_r(NULL, ":", &save_ptr)) {
672 /* Like stream_open(), but for tcp streams the port defaults to
673 * 'default_tcp_port' if no port number is given and for SSL streams the port
674 * defaults to 'default_ssl_port' if no port number is given. */
676 stream_open_with_default_ports(const char *name_,
677 uint16_t default_tcp_port,
678 uint16_t default_ssl_port,
679 struct stream **streamp)
684 if (!strncmp(name_, "tcp:", 4) && count_fields(name_) < 3) {
685 name = xasprintf("%s:%d", name_, default_tcp_port);
686 } else if (!strncmp(name_, "ssl:", 4) && count_fields(name_) < 3) {
687 name = xasprintf("%s:%d", name_, default_ssl_port);
689 name = xstrdup(name_);
691 error = stream_open(name, streamp);
697 /* Like pstream_open(), but for ptcp streams the port defaults to
698 * 'default_ptcp_port' if no port number is given and for passive SSL streams
699 * the port defaults to 'default_pssl_port' if no port number is given. */
701 pstream_open_with_default_ports(const char *name_,
702 uint16_t default_ptcp_port,
703 uint16_t default_pssl_port,
704 struct pstream **pstreamp)
709 if (!strncmp(name_, "ptcp:", 5) && count_fields(name_) < 2) {
710 name = xasprintf("%s%d", name_, default_ptcp_port);
711 } else if (!strncmp(name_, "pssl:", 5) && count_fields(name_) < 2) {
712 name = xasprintf("%s%d", name_, default_pssl_port);
714 name = xstrdup(name_);
716 error = pstream_open(name, pstreamp);
722 /* Attempts to guess the content type of a stream whose first few bytes were
723 * the 'size' bytes of 'data'. */
724 static enum stream_content_type
725 stream_guess_content(const uint8_t *data, size_t size)
728 #define PAIR(A, B) (((A) << 8) | (B))
729 switch (PAIR(data[0], data[1])) {
730 case PAIR(0x16, 0x03): /* Handshake, version 3. */
733 return STREAM_JSONRPC;
734 case PAIR(OFP_VERSION, OFPT_HELLO):
735 return STREAM_OPENFLOW;
739 return STREAM_UNKNOWN;
742 /* Returns a string represenation of 'type'. */
744 stream_content_type_to_string(enum stream_content_type type)
754 case STREAM_OPENFLOW:
762 /* Attempts to guess the content type of a stream whose first few bytes were
763 * the 'size' bytes of 'data'. If this is done successfully, and the guessed
764 * content type is other than 'expected_type', then log a message in vlog
765 * module 'module', naming 'stream_name' as the source, explaining what
766 * content was expected and what was actually received. */
768 stream_report_content(const void *data, size_t size,
769 enum stream_content_type expected_type,
770 enum vlog_module module, const char *stream_name)
772 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
773 enum stream_content_type actual_type;
775 actual_type = stream_guess_content(data, size);
776 if (actual_type != expected_type && actual_type != STREAM_UNKNOWN) {
777 vlog_rate_limit(module, VLL_WARN, &rl,
778 "%s: received %s data on %s channel",
780 stream_content_type_to_string(expected_type),
781 stream_content_type_to_string(actual_type));