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"
40 VLOG_DEFINE_THIS_MODULE(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
;
244 while ((error
= stream_connect(stream
)) == EAGAIN
) {
246 stream_run_wait(stream
);
247 stream_connect_wait(stream
);
250 assert(error
!= EINPROGRESS
);
254 stream_close(stream
);
262 /* Closes 'stream'. */
264 stream_close(struct stream
*stream
)
266 if (stream
!= NULL
) {
267 char *name
= stream
->name
;
268 (stream
->class->close
)(stream
);
273 /* Returns the name of 'stream', that is, the string passed to
276 stream_get_name(const struct stream
*stream
)
278 return stream
? stream
->name
: "(null)";
281 /* Returns the IP address of the peer, or 0 if the peer is not connected over
282 * an IP-based protocol or if its IP address is not yet known. */
284 stream_get_remote_ip(const struct stream
*stream
)
286 return stream
->remote_ip
;
289 /* Returns the transport port of the peer, or 0 if the connection does not
290 * contain a port or if the port is not yet known. */
292 stream_get_remote_port(const struct stream
*stream
)
294 return stream
->remote_port
;
297 /* Returns the IP address used to connect to the peer, or 0 if the connection
298 * is not an IP-based protocol or if its IP address is not yet known. */
300 stream_get_local_ip(const struct stream
*stream
)
302 return stream
->local_ip
;
305 /* Returns the transport port used to connect to the peer, or 0 if the
306 * connection does not contain a port or if the port is not yet known. */
308 stream_get_local_port(const struct stream
*stream
)
310 return stream
->local_port
;
314 scs_connecting(struct stream
*stream
)
316 int retval
= (stream
->class->connect
)(stream
);
317 assert(retval
!= EINPROGRESS
);
319 stream
->state
= SCS_CONNECTED
;
320 } else if (retval
!= EAGAIN
) {
321 stream
->state
= SCS_DISCONNECTED
;
322 stream
->error
= retval
;
326 /* Tries to complete the connection on 'stream'. If 'stream''s connection is
327 * complete, returns 0 if the connection was successful or a positive errno
328 * value if it failed. If the connection is still in progress, returns
331 stream_connect(struct stream
*stream
)
333 enum stream_state last_state
;
336 last_state
= stream
->state
;
337 switch (stream
->state
) {
339 scs_connecting(stream
);
345 case SCS_DISCONNECTED
:
346 return stream
->error
;
351 } while (stream
->state
!= last_state
);
356 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
358 * - If successful, the number of bytes received (between 1 and 'n').
360 * - On error, a negative errno value.
362 * - 0, if the connection has been closed in the normal fashion, or if 'n'
365 * The recv function will not block waiting for a packet to arrive. If no
366 * data have been received, it returns -EAGAIN immediately. */
368 stream_recv(struct stream
*stream
, void *buffer
, size_t n
)
370 int retval
= stream_connect(stream
);
371 return (retval
? -retval
373 : (stream
->class->recv
)(stream
, buffer
, n
));
376 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
378 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
379 * only a valid return value if 'n' is 0.
381 * - On error, a negative errno value.
383 * The send function will not block. If no bytes can be immediately accepted
384 * for transmission, it returns -EAGAIN immediately. */
386 stream_send(struct stream
*stream
, const void *buffer
, size_t n
)
388 int retval
= stream_connect(stream
);
389 return (retval
? -retval
391 : (stream
->class->send
)(stream
, buffer
, n
));
394 /* Allows 'stream' to perform maintenance activities, such as flushing
397 stream_run(struct stream
*stream
)
399 if (stream
->class->run
) {
400 (stream
->class->run
)(stream
);
404 /* Arranges for the poll loop to wake up when 'stream' needs to perform
405 * maintenance activities. */
407 stream_run_wait(struct stream
*stream
)
409 if (stream
->class->run_wait
) {
410 (stream
->class->run_wait
)(stream
);
414 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
415 * action of the given 'type'. */
417 stream_wait(struct stream
*stream
, enum stream_wait_type wait
)
419 assert(wait
== STREAM_CONNECT
|| wait
== STREAM_RECV
420 || wait
== STREAM_SEND
);
422 switch (stream
->state
) {
424 wait
= STREAM_CONNECT
;
427 case SCS_DISCONNECTED
:
428 poll_immediate_wake();
431 (stream
->class->wait
)(stream
, wait
);
435 stream_connect_wait(struct stream
*stream
)
437 stream_wait(stream
, STREAM_CONNECT
);
441 stream_recv_wait(struct stream
*stream
)
443 stream_wait(stream
, STREAM_RECV
);
447 stream_send_wait(struct stream
*stream
)
449 stream_wait(stream
, STREAM_SEND
);
452 /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class
453 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
454 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
457 pstream_lookup_class(const char *name
, struct pstream_class
**classp
)
462 check_stream_classes();
465 prefix_len
= strcspn(name
, ":");
466 if (name
[prefix_len
] == '\0') {
469 for (i
= 0; i
< ARRAY_SIZE(pstream_classes
); i
++) {
470 struct pstream_class
*class = pstream_classes
[i
];
471 if (strlen(class->name
) == prefix_len
472 && !memcmp(class->name
, name
, prefix_len
)) {
480 /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
481 * a supported pstream type, otherwise EAFNOSUPPORT. */
483 pstream_verify_name(const char *name
)
485 struct pstream_class
*class;
486 return pstream_lookup_class(name
, &class);
489 /* Attempts to start listening for remote stream connections. 'name' is a
490 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
491 * class's name and ARGS are stream class-specific.
493 * Returns 0 if successful, otherwise a positive errno value. If successful,
494 * stores a pointer to the new connection in '*pstreamp', otherwise a null
497 pstream_open(const char *name
, struct pstream
**pstreamp
)
499 struct pstream_class
*class;
500 struct pstream
*pstream
;
504 COVERAGE_INC(pstream_open
);
506 /* Look up the class. */
507 error
= pstream_lookup_class(name
, &class);
512 /* Call class's "open" function. */
513 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
514 error
= class->listen(name
, suffix_copy
, &pstream
);
529 /* Returns the name that was used to open 'pstream'. The caller must not
530 * modify or free the name. */
532 pstream_get_name(const struct pstream
*pstream
)
534 return pstream
->name
;
537 /* Closes 'pstream'. */
539 pstream_close(struct pstream
*pstream
)
541 if (pstream
!= NULL
) {
542 char *name
= pstream
->name
;
543 (pstream
->class->close
)(pstream
);
548 /* Tries to accept a new connection on 'pstream'. If successful, stores the
549 * new connection in '*new_stream' and returns 0. Otherwise, returns a
550 * positive errno value.
552 * pstream_accept() will not block waiting for a connection. If no connection
553 * is ready to be accepted, it returns EAGAIN immediately. */
555 pstream_accept(struct pstream
*pstream
, struct stream
**new_stream
)
557 int retval
= (pstream
->class->accept
)(pstream
, new_stream
);
561 assert((*new_stream
)->state
!= SCS_CONNECTING
562 || (*new_stream
)->class->connect
);
567 /* Tries to accept a new connection on 'pstream'. If successful, stores the
568 * new connection in '*new_stream' and returns 0. Otherwise, returns a
569 * positive errno value.
571 * pstream_accept_block() blocks until a connection is ready or until an error
572 * occurs. It will not return EAGAIN. */
574 pstream_accept_block(struct pstream
*pstream
, struct stream
**new_stream
)
579 while ((error
= pstream_accept(pstream
, new_stream
)) == EAGAIN
) {
580 pstream_wait(pstream
);
590 pstream_wait(struct pstream
*pstream
)
592 (pstream
->class->wait
)(pstream
);
595 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
596 * The initial connection status, supplied as 'connect_status', is interpreted
599 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
600 * called in the normal fashion.
602 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
603 * function should be called to complete the connection.
605 * - Other positive errno values indicate that the connection failed with
606 * the specified error.
608 * After calling this function, stream_close() must be used to destroy
609 * 'stream', otherwise resources will be leaked.
611 * The caller retains ownership of 'name'. */
613 stream_init(struct stream
*stream
, struct stream_class
*class,
614 int connect_status
, const char *name
)
616 stream
->class = class;
617 stream
->state
= (connect_status
== EAGAIN
? SCS_CONNECTING
618 : !connect_status
? SCS_CONNECTED
620 stream
->error
= connect_status
;
621 stream
->name
= xstrdup(name
);
622 assert(stream
->state
!= SCS_CONNECTING
|| class->connect
);
626 stream_set_remote_ip(struct stream
*stream
, uint32_t ip
)
628 stream
->remote_ip
= ip
;
632 stream_set_remote_port(struct stream
*stream
, uint16_t port
)
634 stream
->remote_port
= port
;
638 stream_set_local_ip(struct stream
*stream
, uint32_t ip
)
640 stream
->local_ip
= ip
;
644 stream_set_local_port(struct stream
*stream
, uint16_t port
)
646 stream
->local_port
= port
;
650 pstream_init(struct pstream
*pstream
, struct pstream_class
*class,
653 pstream
->class = class;
654 pstream
->name
= xstrdup(name
);
658 count_fields(const char *s_
)
660 char *s
, *field
, *save_ptr
;
665 for (field
= strtok_r(s
, ":", &save_ptr
); field
!= NULL
;
666 field
= strtok_r(NULL
, ":", &save_ptr
)) {
674 /* Like stream_open(), but for tcp streams the port defaults to
675 * 'default_tcp_port' if no port number is given and for SSL streams the port
676 * defaults to 'default_ssl_port' if no port number is given. */
678 stream_open_with_default_ports(const char *name_
,
679 uint16_t default_tcp_port
,
680 uint16_t default_ssl_port
,
681 struct stream
**streamp
)
686 if (!strncmp(name_
, "tcp:", 4) && count_fields(name_
) < 3) {
687 name
= xasprintf("%s:%d", name_
, default_tcp_port
);
688 } else if (!strncmp(name_
, "ssl:", 4) && count_fields(name_
) < 3) {
689 name
= xasprintf("%s:%d", name_
, default_ssl_port
);
691 name
= xstrdup(name_
);
693 error
= stream_open(name
, streamp
);
699 /* Like pstream_open(), but for ptcp streams the port defaults to
700 * 'default_ptcp_port' if no port number is given and for passive SSL streams
701 * the port defaults to 'default_pssl_port' if no port number is given. */
703 pstream_open_with_default_ports(const char *name_
,
704 uint16_t default_ptcp_port
,
705 uint16_t default_pssl_port
,
706 struct pstream
**pstreamp
)
711 if (!strncmp(name_
, "ptcp:", 5) && count_fields(name_
) < 2) {
712 name
= xasprintf("%s%d", name_
, default_ptcp_port
);
713 } else if (!strncmp(name_
, "pssl:", 5) && count_fields(name_
) < 2) {
714 name
= xasprintf("%s%d", name_
, default_pssl_port
);
716 name
= xstrdup(name_
);
718 error
= pstream_open(name
, pstreamp
);
724 /* Attempts to guess the content type of a stream whose first few bytes were
725 * the 'size' bytes of 'data'. */
726 static enum stream_content_type
727 stream_guess_content(const uint8_t *data
, size_t size
)
730 #define PAIR(A, B) (((A) << 8) | (B))
731 switch (PAIR(data
[0], data
[1])) {
732 case PAIR(0x16, 0x03): /* Handshake, version 3. */
735 return STREAM_JSONRPC
;
736 case PAIR(OFP_VERSION
, OFPT_HELLO
):
737 return STREAM_OPENFLOW
;
741 return STREAM_UNKNOWN
;
744 /* Returns a string represenation of 'type'. */
746 stream_content_type_to_string(enum stream_content_type type
)
756 case STREAM_OPENFLOW
:
764 /* Attempts to guess the content type of a stream whose first few bytes were
765 * the 'size' bytes of 'data'. If this is done successfully, and the guessed
766 * content type is other than 'expected_type', then log a message in vlog
767 * module 'module', naming 'stream_name' as the source, explaining what
768 * content was expected and what was actually received. */
770 stream_report_content(const void *data
, size_t size
,
771 enum stream_content_type expected_type
,
772 struct vlog_module
*module
, const char *stream_name
)
774 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
775 enum stream_content_type actual_type
;
777 actual_type
= stream_guess_content(data
, size
);
778 if (actual_type
!= expected_type
&& actual_type
!= STREAM_UNKNOWN
) {
779 vlog_rate_limit(module
, VLL_WARN
, &rl
,
780 "%s: received %s data on %s channel",
782 stream_content_type_to_string(expected_type
),
783 stream_content_type_to_string(actual_type
));