2 * Copyright (c) 2008, 2009, 2010, 2011 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 COVERAGE_DEFINE(pstream_open
);
43 COVERAGE_DEFINE(stream_open
);
45 /* State of an active stream.*/
47 SCS_CONNECTING
, /* Underlying stream is not connected. */
48 SCS_CONNECTED
, /* Connection established. */
49 SCS_DISCONNECTED
/* Connection failed or connection closed. */
52 static struct stream_class
*stream_classes
[] = {
60 static struct pstream_class
*pstream_classes
[] = {
68 /* Check the validity of the stream class structures. */
70 check_stream_classes(void)
75 for (i
= 0; i
< ARRAY_SIZE(stream_classes
); i
++) {
76 struct stream_class
*class = stream_classes
[i
];
77 assert(class->name
!= NULL
);
78 assert(class->open
!= NULL
);
79 if (class->close
|| class->recv
|| class->send
|| class->run
80 || class->run_wait
|| class->wait
) {
81 assert(class->close
!= NULL
);
82 assert(class->recv
!= NULL
);
83 assert(class->send
!= NULL
);
84 assert(class->wait
!= NULL
);
86 /* This class delegates to another one. */
90 for (i
= 0; i
< ARRAY_SIZE(pstream_classes
); i
++) {
91 struct pstream_class
*class = pstream_classes
[i
];
92 assert(class->name
!= NULL
);
93 assert(class->listen
!= NULL
);
94 if (class->close
|| class->accept
|| class->wait
) {
95 assert(class->close
!= NULL
);
96 assert(class->accept
!= NULL
);
97 assert(class->wait
!= NULL
);
99 /* This class delegates to another one. */
105 /* Prints information on active (if 'active') and passive (if 'passive')
106 * connection methods supported by the stream. */
108 stream_usage(const char *name
, bool active
, bool passive
,
109 bool bootstrap OVS_UNUSED
)
111 /* Really this should be implemented via callbacks into the stream
112 * providers, but that seems too heavy-weight to bother with at the
117 printf("Active %s connection methods:\n", name
);
118 printf(" tcp:IP:PORT "
119 "PORT at remote IP\n");
121 printf(" ssl:IP:PORT "
122 "SSL PORT at remote IP\n");
125 "Unix domain socket named FILE\n");
129 printf("Passive %s connection methods:\n", name
);
130 printf(" ptcp:PORT[:IP] "
131 "listen to TCP PORT on IP\n");
133 printf(" pssl:PORT[:IP] "
134 "listen for SSL on PORT on IP\n");
136 printf(" punix:FILE "
137 "listen on Unix domain socket FILE\n");
141 printf("PKI configuration (required to use SSL):\n"
142 " -p, --private-key=FILE file with private key\n"
143 " -c, --certificate=FILE file with certificate for private key\n"
144 " -C, --ca-cert=FILE file with peer CA certificate\n");
146 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
147 "to read or create\n");
152 /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class
153 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
154 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
157 stream_lookup_class(const char *name
, struct stream_class
**classp
)
162 check_stream_classes();
165 prefix_len
= strcspn(name
, ":");
166 if (name
[prefix_len
] == '\0') {
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
)) {
180 /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is
181 * a supported stream type, otherwise EAFNOSUPPORT. */
183 stream_verify_name(const char *name
)
185 struct stream_class
*class;
186 return stream_lookup_class(name
, &class);
189 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
190 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
191 * ARGS are stream class-specific.
193 * Returns 0 if successful, otherwise a positive errno value. If successful,
194 * stores a pointer to the new connection in '*streamp', otherwise a null
197 stream_open(const char *name
, struct stream
**streamp
)
199 struct stream_class
*class;
200 struct stream
*stream
;
204 COVERAGE_INC(stream_open
);
206 /* Look up the class. */
207 error
= stream_lookup_class(name
, &class);
212 /* Call class's "open" function. */
213 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
214 error
= class->open(name
, suffix_copy
, &stream
);
229 /* Blocks until a previously started stream connection attempt succeeds or
230 * fails. 'error' should be the value returned by stream_open() and 'streamp'
231 * should point to the stream pointer set by stream_open(). Returns 0 if
232 * successful, otherwise a positive errno value other than EAGAIN or
233 * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes
234 * '*streamp' and sets '*streamp' to null.
237 * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
240 stream_open_block(int error
, struct stream
**streamp
)
242 struct stream
*stream
= *streamp
;
247 while ((error
= stream_connect(stream
)) == EAGAIN
) {
249 stream_run_wait(stream
);
250 stream_connect_wait(stream
);
253 assert(error
!= EINPROGRESS
);
257 stream_close(stream
);
265 /* Closes 'stream'. */
267 stream_close(struct stream
*stream
)
269 if (stream
!= NULL
) {
270 char *name
= stream
->name
;
271 (stream
->class->close
)(stream
);
276 /* Returns the name of 'stream', that is, the string passed to
279 stream_get_name(const struct stream
*stream
)
281 return stream
? stream
->name
: "(null)";
284 /* Returns the IP address of the peer, or 0 if the peer is not connected over
285 * an IP-based protocol or if its IP address is not yet known. */
287 stream_get_remote_ip(const struct stream
*stream
)
289 return stream
->remote_ip
;
292 /* Returns the transport port of the peer, or 0 if the connection does not
293 * contain a port or if the port is not yet known. */
295 stream_get_remote_port(const struct stream
*stream
)
297 return stream
->remote_port
;
300 /* Returns the IP address used to connect to the peer, or 0 if the connection
301 * is not an IP-based protocol or if its IP address is not yet known. */
303 stream_get_local_ip(const struct stream
*stream
)
305 return stream
->local_ip
;
308 /* Returns the transport port used to connect to the peer, or 0 if the
309 * connection does not contain a port or if the port is not yet known. */
311 stream_get_local_port(const struct stream
*stream
)
313 return stream
->local_port
;
317 scs_connecting(struct stream
*stream
)
319 int retval
= (stream
->class->connect
)(stream
);
320 assert(retval
!= EINPROGRESS
);
322 stream
->state
= SCS_CONNECTED
;
323 } else if (retval
!= EAGAIN
) {
324 stream
->state
= SCS_DISCONNECTED
;
325 stream
->error
= retval
;
329 /* Tries to complete the connection on 'stream'. If 'stream''s connection is
330 * complete, returns 0 if the connection was successful or a positive errno
331 * value if it failed. If the connection is still in progress, returns
334 stream_connect(struct stream
*stream
)
336 enum stream_state last_state
;
339 last_state
= stream
->state
;
340 switch (stream
->state
) {
342 scs_connecting(stream
);
348 case SCS_DISCONNECTED
:
349 return stream
->error
;
354 } while (stream
->state
!= last_state
);
359 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
361 * - If successful, the number of bytes received (between 1 and 'n').
363 * - On error, a negative errno value.
365 * - 0, if the connection has been closed in the normal fashion, or if 'n'
368 * The recv function will not block waiting for a packet to arrive. If no
369 * data have been received, it returns -EAGAIN immediately. */
371 stream_recv(struct stream
*stream
, void *buffer
, size_t n
)
373 int retval
= stream_connect(stream
);
374 return (retval
? -retval
376 : (stream
->class->recv
)(stream
, buffer
, n
));
379 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
381 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
382 * only a valid return value if 'n' is 0.
384 * - On error, a negative errno value.
386 * The send function will not block. If no bytes can be immediately accepted
387 * for transmission, it returns -EAGAIN immediately. */
389 stream_send(struct stream
*stream
, const void *buffer
, size_t n
)
391 int retval
= stream_connect(stream
);
392 return (retval
? -retval
394 : (stream
->class->send
)(stream
, buffer
, n
));
397 /* Allows 'stream' to perform maintenance activities, such as flushing
400 stream_run(struct stream
*stream
)
402 if (stream
->class->run
) {
403 (stream
->class->run
)(stream
);
407 /* Arranges for the poll loop to wake up when 'stream' needs to perform
408 * maintenance activities. */
410 stream_run_wait(struct stream
*stream
)
412 if (stream
->class->run_wait
) {
413 (stream
->class->run_wait
)(stream
);
417 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
418 * action of the given 'type'. */
420 stream_wait(struct stream
*stream
, enum stream_wait_type wait
)
422 assert(wait
== STREAM_CONNECT
|| wait
== STREAM_RECV
423 || wait
== STREAM_SEND
);
425 switch (stream
->state
) {
427 wait
= STREAM_CONNECT
;
430 case SCS_DISCONNECTED
:
431 poll_immediate_wake();
434 (stream
->class->wait
)(stream
, wait
);
438 stream_connect_wait(struct stream
*stream
)
440 stream_wait(stream
, STREAM_CONNECT
);
444 stream_recv_wait(struct stream
*stream
)
446 stream_wait(stream
, STREAM_RECV
);
450 stream_send_wait(struct stream
*stream
)
452 stream_wait(stream
, STREAM_SEND
);
455 /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class
456 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
457 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
460 pstream_lookup_class(const char *name
, struct pstream_class
**classp
)
465 check_stream_classes();
468 prefix_len
= strcspn(name
, ":");
469 if (name
[prefix_len
] == '\0') {
472 for (i
= 0; i
< ARRAY_SIZE(pstream_classes
); i
++) {
473 struct pstream_class
*class = pstream_classes
[i
];
474 if (strlen(class->name
) == prefix_len
475 && !memcmp(class->name
, name
, prefix_len
)) {
483 /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
484 * a supported pstream type, otherwise EAFNOSUPPORT. */
486 pstream_verify_name(const char *name
)
488 struct pstream_class
*class;
489 return pstream_lookup_class(name
, &class);
492 /* Attempts to start listening for remote stream connections. 'name' is a
493 * connection name in the form "TYPE:ARGS", where TYPE is an passive stream
494 * class's name and ARGS are stream class-specific.
496 * Returns 0 if successful, otherwise a positive errno value. If successful,
497 * stores a pointer to the new connection in '*pstreamp', otherwise a null
500 pstream_open(const char *name
, struct pstream
**pstreamp
)
502 struct pstream_class
*class;
503 struct pstream
*pstream
;
507 COVERAGE_INC(pstream_open
);
509 /* Look up the class. */
510 error
= pstream_lookup_class(name
, &class);
515 /* Call class's "open" function. */
516 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
517 error
= class->listen(name
, suffix_copy
, &pstream
);
532 /* Returns the name that was used to open 'pstream'. The caller must not
533 * modify or free the name. */
535 pstream_get_name(const struct pstream
*pstream
)
537 return pstream
->name
;
540 /* Closes 'pstream'. */
542 pstream_close(struct pstream
*pstream
)
544 if (pstream
!= NULL
) {
545 char *name
= pstream
->name
;
546 (pstream
->class->close
)(pstream
);
551 /* Tries to accept a new connection on 'pstream'. If successful, stores the
552 * new connection in '*new_stream' and returns 0. Otherwise, returns a
553 * positive errno value.
555 * pstream_accept() will not block waiting for a connection. If no connection
556 * is ready to be accepted, it returns EAGAIN immediately. */
558 pstream_accept(struct pstream
*pstream
, struct stream
**new_stream
)
560 int retval
= (pstream
->class->accept
)(pstream
, new_stream
);
564 assert((*new_stream
)->state
!= SCS_CONNECTING
565 || (*new_stream
)->class->connect
);
570 /* Tries to accept a new connection on 'pstream'. If successful, stores the
571 * new connection in '*new_stream' and returns 0. Otherwise, returns a
572 * positive errno value.
574 * pstream_accept_block() blocks until a connection is ready or until an error
575 * occurs. It will not return EAGAIN. */
577 pstream_accept_block(struct pstream
*pstream
, struct stream
**new_stream
)
582 while ((error
= pstream_accept(pstream
, new_stream
)) == EAGAIN
) {
583 pstream_wait(pstream
);
593 pstream_wait(struct pstream
*pstream
)
595 (pstream
->class->wait
)(pstream
);
598 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
599 * The initial connection status, supplied as 'connect_status', is interpreted
602 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
603 * called in the normal fashion.
605 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
606 * function should be called to complete the connection.
608 * - Other positive errno values indicate that the connection failed with
609 * the specified error.
611 * After calling this function, stream_close() must be used to destroy
612 * 'stream', otherwise resources will be leaked.
614 * The caller retains ownership of 'name'. */
616 stream_init(struct stream
*stream
, struct stream_class
*class,
617 int connect_status
, const char *name
)
619 stream
->class = class;
620 stream
->state
= (connect_status
== EAGAIN
? SCS_CONNECTING
621 : !connect_status
? SCS_CONNECTED
623 stream
->error
= connect_status
;
624 stream
->name
= xstrdup(name
);
625 assert(stream
->state
!= SCS_CONNECTING
|| class->connect
);
629 stream_set_remote_ip(struct stream
*stream
, ovs_be32 ip
)
631 stream
->remote_ip
= ip
;
635 stream_set_remote_port(struct stream
*stream
, ovs_be16 port
)
637 stream
->remote_port
= port
;
641 stream_set_local_ip(struct stream
*stream
, ovs_be32 ip
)
643 stream
->local_ip
= ip
;
647 stream_set_local_port(struct stream
*stream
, ovs_be16 port
)
649 stream
->local_port
= port
;
653 pstream_init(struct pstream
*pstream
, struct pstream_class
*class,
656 pstream
->class = class;
657 pstream
->name
= xstrdup(name
);
661 count_fields(const char *s_
)
663 char *s
, *field
, *save_ptr
;
668 for (field
= strtok_r(s
, ":", &save_ptr
); field
!= NULL
;
669 field
= strtok_r(NULL
, ":", &save_ptr
)) {
677 /* Like stream_open(), but for tcp streams the port defaults to
678 * 'default_tcp_port' if no port number is given and for SSL streams the port
679 * defaults to 'default_ssl_port' if no port number is given. */
681 stream_open_with_default_ports(const char *name_
,
682 uint16_t default_tcp_port
,
683 uint16_t default_ssl_port
,
684 struct stream
**streamp
)
689 if (!strncmp(name_
, "tcp:", 4) && count_fields(name_
) < 3) {
690 name
= xasprintf("%s:%d", name_
, default_tcp_port
);
691 } else if (!strncmp(name_
, "ssl:", 4) && count_fields(name_
) < 3) {
692 name
= xasprintf("%s:%d", name_
, default_ssl_port
);
694 name
= xstrdup(name_
);
696 error
= stream_open(name
, streamp
);
702 /* Like pstream_open(), but for ptcp streams the port defaults to
703 * 'default_ptcp_port' if no port number is given and for passive SSL streams
704 * the port defaults to 'default_pssl_port' if no port number is given. */
706 pstream_open_with_default_ports(const char *name_
,
707 uint16_t default_ptcp_port
,
708 uint16_t default_pssl_port
,
709 struct pstream
**pstreamp
)
714 if (!strncmp(name_
, "ptcp:", 5) && count_fields(name_
) < 2) {
715 name
= xasprintf("%s%d", name_
, default_ptcp_port
);
716 } else if (!strncmp(name_
, "pssl:", 5) && count_fields(name_
) < 2) {
717 name
= xasprintf("%s%d", name_
, default_pssl_port
);
719 name
= xstrdup(name_
);
721 error
= pstream_open(name
, pstreamp
);
727 /* Attempts to guess the content type of a stream whose first few bytes were
728 * the 'size' bytes of 'data'. */
729 static enum stream_content_type
730 stream_guess_content(const uint8_t *data
, size_t size
)
733 #define PAIR(A, B) (((A) << 8) | (B))
734 switch (PAIR(data
[0], data
[1])) {
735 case PAIR(0x16, 0x03): /* Handshake, version 3. */
738 return STREAM_JSONRPC
;
739 case PAIR(OFP_VERSION
, OFPT_HELLO
):
740 return STREAM_OPENFLOW
;
744 return STREAM_UNKNOWN
;
747 /* Returns a string represenation of 'type'. */
749 stream_content_type_to_string(enum stream_content_type type
)
759 case STREAM_OPENFLOW
:
767 /* Attempts to guess the content type of a stream whose first few bytes were
768 * the 'size' bytes of 'data'. If this is done successfully, and the guessed
769 * content type is other than 'expected_type', then log a message in vlog
770 * module 'module', naming 'stream_name' as the source, explaining what
771 * content was expected and what was actually received. */
773 stream_report_content(const void *data
, size_t size
,
774 enum stream_content_type expected_type
,
775 struct vlog_module
*module
, const char *stream_name
)
777 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
778 enum stream_content_type actual_type
;
780 actual_type
= stream_guess_content(data
, size
);
781 if (actual_type
!= expected_type
&& actual_type
!= STREAM_UNKNOWN
) {
782 vlog_rate_limit(module
, VLL_WARN
, &rl
,
783 "%s: received %s data on %s channel",
785 stream_content_type_to_string(actual_type
),
786 stream_content_type_to_string(expected_type
));