2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
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"
21 #include <netinet/in.h>
26 #include "dynamic-string.h"
27 #include "fatal-signal.h"
30 #include "ofp-print.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
34 #include "ovs-thread.h"
36 #include "poll-loop.h"
38 #include "socket-util.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(stream
);
44 COVERAGE_DEFINE(pstream_open
);
45 COVERAGE_DEFINE(stream_open
);
47 /* State of an active stream.*/
49 SCS_CONNECTING
, /* Underlying stream is not connected. */
50 SCS_CONNECTED
, /* Connection established. */
51 SCS_DISCONNECTED
/* Connection failed or connection closed. */
54 static const struct stream_class
*stream_classes
[] = {
59 &windows_stream_class
,
66 static const struct pstream_class
*pstream_classes
[] = {
71 &pwindows_pstream_class
,
78 /* Check the validity of the stream class structures. */
80 check_stream_classes(void)
85 for (i
= 0; i
< ARRAY_SIZE(stream_classes
); i
++) {
86 const struct stream_class
*class = stream_classes
[i
];
87 ovs_assert(class->name
!= NULL
);
88 ovs_assert(class->open
!= NULL
);
89 if (class->close
|| class->recv
|| class->send
|| class->run
90 || class->run_wait
|| class->wait
) {
91 ovs_assert(class->close
!= NULL
);
92 ovs_assert(class->recv
!= NULL
);
93 ovs_assert(class->send
!= NULL
);
94 ovs_assert(class->wait
!= NULL
);
96 /* This class delegates to another one. */
100 for (i
= 0; i
< ARRAY_SIZE(pstream_classes
); i
++) {
101 const struct pstream_class
*class = pstream_classes
[i
];
102 ovs_assert(class->name
!= NULL
);
103 ovs_assert(class->listen
!= NULL
);
104 if (class->close
|| class->accept
|| class->wait
) {
105 ovs_assert(class->close
!= NULL
);
106 ovs_assert(class->accept
!= NULL
);
107 ovs_assert(class->wait
!= NULL
);
109 /* This class delegates to another one. */
115 /* Prints information on active (if 'active') and passive (if 'passive')
116 * connection methods supported by the stream. */
118 stream_usage(const char *name
, bool active
, bool passive
,
119 bool bootstrap OVS_UNUSED
)
121 /* Really this should be implemented via callbacks into the stream
122 * providers, but that seems too heavy-weight to bother with at the
127 printf("Active %s connection methods:\n", name
);
128 printf(" tcp:IP:PORT "
129 "PORT at remote IP\n");
131 printf(" ssl:IP:PORT "
132 "SSL PORT at remote IP\n");
135 "Unix domain socket named FILE\n");
139 printf("Passive %s connection methods:\n", name
);
140 printf(" ptcp:PORT[:IP] "
141 "listen to TCP PORT on IP\n");
143 printf(" pssl:PORT[:IP] "
144 "listen for SSL on PORT on IP\n");
146 printf(" punix:FILE "
147 "listen on Unix domain socket FILE\n");
151 printf("PKI configuration (required to use SSL):\n"
152 " -p, --private-key=FILE file with private key\n"
153 " -c, --certificate=FILE file with certificate for private key\n"
154 " -C, --ca-cert=FILE file with peer CA certificate\n");
156 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
157 "to read or create\n");
162 /* Given 'name', a stream name in the form "TYPE:ARGS", stores the class
163 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
164 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
167 stream_lookup_class(const char *name
, const struct stream_class
**classp
)
172 check_stream_classes();
175 prefix_len
= strcspn(name
, ":");
176 if (name
[prefix_len
] == '\0') {
179 for (i
= 0; i
< ARRAY_SIZE(stream_classes
); i
++) {
180 const struct stream_class
*class = stream_classes
[i
];
181 if (strlen(class->name
) == prefix_len
182 && !memcmp(class->name
, name
, prefix_len
)) {
190 /* Returns 0 if 'name' is a stream name in the form "TYPE:ARGS" and TYPE is
191 * a supported stream type, otherwise EAFNOSUPPORT. */
193 stream_verify_name(const char *name
)
195 const struct stream_class
*class;
196 return stream_lookup_class(name
, &class);
199 /* Attempts to connect a stream to a remote peer. 'name' is a connection name
200 * in the form "TYPE:ARGS", where TYPE is an active stream class's name and
201 * ARGS are stream class-specific.
203 * Returns 0 if successful, otherwise a positive errno value. If successful,
204 * stores a pointer to the new connection in '*streamp', otherwise a null
207 stream_open(const char *name
, struct stream
**streamp
, uint8_t dscp
)
209 const struct stream_class
*class;
210 struct stream
*stream
;
214 COVERAGE_INC(stream_open
);
216 /* Look up the class. */
217 error
= stream_lookup_class(name
, &class);
222 /* Call class's "open" function. */
223 suffix_copy
= xstrdup(strchr(name
, ':') + 1);
224 error
= class->open(name
, suffix_copy
, &stream
, dscp
);
239 /* Blocks until a previously started stream connection attempt succeeds or
240 * fails. 'error' should be the value returned by stream_open() and 'streamp'
241 * should point to the stream pointer set by stream_open(). Returns 0 if
242 * successful, otherwise a positive errno value other than EAGAIN or
243 * EINPROGRESS. If successful, leaves '*streamp' untouched; on error, closes
244 * '*streamp' and sets '*streamp' to null.
247 * error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
250 stream_open_block(int error
, struct stream
**streamp
)
252 struct stream
*stream
= *streamp
;
257 while ((error
= stream_connect(stream
)) == EAGAIN
) {
259 stream_run_wait(stream
);
260 stream_connect_wait(stream
);
263 ovs_assert(error
!= EINPROGRESS
);
267 stream_close(stream
);
275 /* Closes 'stream'. */
277 stream_close(struct stream
*stream
)
279 if (stream
!= NULL
) {
280 char *name
= stream
->name
;
281 (stream
->class->close
)(stream
);
286 /* Returns the name of 'stream', that is, the string passed to
289 stream_get_name(const struct stream
*stream
)
291 return stream
? stream
->name
: "(null)";
295 scs_connecting(struct stream
*stream
)
297 int retval
= (stream
->class->connect
)(stream
);
298 ovs_assert(retval
!= EINPROGRESS
);
300 stream
->state
= SCS_CONNECTED
;
301 } else if (retval
!= EAGAIN
) {
302 stream
->state
= SCS_DISCONNECTED
;
303 stream
->error
= retval
;
307 /* Tries to complete the connection on 'stream'. If 'stream''s connection is
308 * complete, returns 0 if the connection was successful or a positive errno
309 * value if it failed. If the connection is still in progress, returns
312 stream_connect(struct stream
*stream
)
314 enum stream_state last_state
;
317 last_state
= stream
->state
;
318 switch (stream
->state
) {
320 scs_connecting(stream
);
326 case SCS_DISCONNECTED
:
327 return stream
->error
;
332 } while (stream
->state
!= last_state
);
337 /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and returns:
339 * - If successful, the number of bytes received (between 1 and 'n').
341 * - On error, a negative errno value.
343 * - 0, if the connection has been closed in the normal fashion, or if 'n'
346 * The recv function will not block waiting for a packet to arrive. If no
347 * data have been received, it returns -EAGAIN immediately. */
349 stream_recv(struct stream
*stream
, void *buffer
, size_t n
)
351 int retval
= stream_connect(stream
);
352 return (retval
? -retval
354 : (stream
->class->recv
)(stream
, buffer
, n
));
357 /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
359 * - If successful, the number of bytes sent (between 1 and 'n'). 0 is
360 * only a valid return value if 'n' is 0.
362 * - On error, a negative errno value.
364 * The send function will not block. If no bytes can be immediately accepted
365 * for transmission, it returns -EAGAIN immediately. */
367 stream_send(struct stream
*stream
, const void *buffer
, size_t n
)
369 int retval
= stream_connect(stream
);
370 return (retval
? -retval
372 : (stream
->class->send
)(stream
, buffer
, n
));
375 /* Allows 'stream' to perform maintenance activities, such as flushing
378 stream_run(struct stream
*stream
)
380 if (stream
->class->run
) {
381 (stream
->class->run
)(stream
);
385 /* Arranges for the poll loop to wake up when 'stream' needs to perform
386 * maintenance activities. */
388 stream_run_wait(struct stream
*stream
)
390 if (stream
->class->run_wait
) {
391 (stream
->class->run_wait
)(stream
);
395 /* Arranges for the poll loop to wake up when 'stream' is ready to take an
396 * action of the given 'type'. */
398 stream_wait(struct stream
*stream
, enum stream_wait_type wait
)
400 ovs_assert(wait
== STREAM_CONNECT
|| wait
== STREAM_RECV
401 || wait
== STREAM_SEND
);
403 switch (stream
->state
) {
405 wait
= STREAM_CONNECT
;
408 case SCS_DISCONNECTED
:
409 poll_immediate_wake();
412 (stream
->class->wait
)(stream
, wait
);
416 stream_connect_wait(struct stream
*stream
)
418 stream_wait(stream
, STREAM_CONNECT
);
422 stream_recv_wait(struct stream
*stream
)
424 stream_wait(stream
, STREAM_RECV
);
428 stream_send_wait(struct stream
*stream
)
430 stream_wait(stream
, STREAM_SEND
);
433 /* Given 'name', a pstream name in the form "TYPE:ARGS", stores the class
434 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
435 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
438 pstream_lookup_class(const char *name
, const struct pstream_class
**classp
)
443 check_stream_classes();
446 prefix_len
= strcspn(name
, ":");
447 if (name
[prefix_len
] == '\0') {
450 for (i
= 0; i
< ARRAY_SIZE(pstream_classes
); i
++) {
451 const struct pstream_class
*class = pstream_classes
[i
];
452 if (strlen(class->name
) == prefix_len
453 && !memcmp(class->name
, name
, prefix_len
)) {
461 /* Returns 0 if 'name' is a pstream name in the form "TYPE:ARGS" and TYPE is
462 * a supported pstream type, otherwise EAFNOSUPPORT. */
464 pstream_verify_name(const char *name
)
466 const struct pstream_class
*class;
467 return pstream_lookup_class(name
, &class);
470 /* Returns 1 if the stream or pstream specified by 'name' needs periodic probes
471 * to verify connectivity. For [p]streams which need probes, it can take a
472 * long time to notice the connection has been dropped. Returns 0 if the
473 * stream or pstream does not need probes, and -1 if 'name' is not valid. */
475 stream_or_pstream_needs_probes(const char *name
)
477 const struct pstream_class
*pclass
;
478 const struct stream_class
*class;
480 if (!stream_lookup_class(name
, &class)) {
481 return class->needs_probes
;
482 } else if (!pstream_lookup_class(name
, &pclass
)) {
483 return pclass
->needs_probes
;
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
, uint8_t dscp
)
499 const 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
, dscp
);
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 ovs_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 /* Returns the transport port on which 'pstream' is listening, or 0 if the
596 * concept doesn't apply. */
598 pstream_get_bound_port(const struct pstream
*pstream
)
600 return pstream
->bound_port
;
603 /* Initializes 'stream' as a new stream named 'name', implemented via 'class'.
604 * The initial connection status, supplied as 'connect_status', is interpreted
607 * - 0: 'stream' is connected. Its 'send' and 'recv' functions may be
608 * called in the normal fashion.
610 * - EAGAIN: 'stream' is trying to complete a connection. Its 'connect'
611 * function should be called to complete the connection.
613 * - Other positive errno values indicate that the connection failed with
614 * the specified error.
616 * After calling this function, stream_close() must be used to destroy
617 * 'stream', otherwise resources will be leaked.
619 * The caller retains ownership of 'name'. */
621 stream_init(struct stream
*stream
, const struct stream_class
*class,
622 int connect_status
, const char *name
)
624 memset(stream
, 0, sizeof *stream
);
625 stream
->class = class;
626 stream
->state
= (connect_status
== EAGAIN
? SCS_CONNECTING
627 : !connect_status
? SCS_CONNECTED
629 stream
->error
= connect_status
;
630 stream
->name
= xstrdup(name
);
631 ovs_assert(stream
->state
!= SCS_CONNECTING
|| class->connect
);
635 pstream_init(struct pstream
*pstream
, const struct pstream_class
*class,
638 memset(pstream
, 0, sizeof *pstream
);
639 pstream
->class = class;
640 pstream
->name
= xstrdup(name
);
644 pstream_set_bound_port(struct pstream
*pstream
, ovs_be16 port
)
646 pstream
->bound_port
= port
;
650 count_fields(const char *s_
)
652 char *s
, *field
, *save_ptr
;
657 for (field
= strtok_r(s
, ":", &save_ptr
); field
!= NULL
;
658 field
= strtok_r(NULL
, ":", &save_ptr
)) {
666 /* Like stream_open(), but the port defaults to 'default_port' if no port
667 * number is given. */
669 stream_open_with_default_port(const char *name_
,
670 uint16_t default_port
,
671 struct stream
**streamp
,
677 if ((!strncmp(name_
, "tcp:", 4) || !strncmp(name_
, "ssl:", 4))
678 && count_fields(name_
) < 3) {
679 if (default_port
== OFP_OLD_PORT
) {
680 VLOG_WARN_ONCE("The default OpenFlow port number will change "
681 "from %d to %d in a future release",
682 OFP_OLD_PORT
, OFP_PORT
);
683 } else if (default_port
== OVSDB_OLD_PORT
) {
684 VLOG_WARN_ONCE("The default OVSDB port number will change "
685 "from %d to %d in a future release",
686 OVSDB_OLD_PORT
, OVSDB_PORT
);
688 name
= xasprintf("%s:%d", name_
, default_port
);
690 name
= xstrdup(name_
);
692 error
= stream_open(name
, streamp
, dscp
);
698 /* Like pstream_open(), but port defaults to 'default_port' if no port
699 * number is given. */
701 pstream_open_with_default_port(const char *name_
,
702 uint16_t default_port
,
703 struct pstream
**pstreamp
,
709 if ((!strncmp(name_
, "ptcp:", 5) || !strncmp(name_
, "pssl:", 5))
710 && count_fields(name_
) < 2) {
711 name
= xasprintf("%s%d", name_
, default_port
);
713 name
= xstrdup(name_
);
715 error
= pstream_open(name
, pstreamp
, dscp
);
722 * This function extracts IP address and port from the target string.
724 * - On success, function returns true and fills *ss structure with port
725 * and IP address. If port was absent in target string then it will use
726 * corresponding default port value.
727 * - On error, function returns false and *ss contains garbage.
730 stream_parse_target_with_default_port(const char *target
,
731 uint16_t default_port
,
732 struct sockaddr_storage
*ss
)
734 return ((!strncmp(target
, "tcp:", 4) || !strncmp(target
, "ssl:", 4))
735 && inet_parse_active(target
+ 4, default_port
, ss
));
738 /* Attempts to guess the content type of a stream whose first few bytes were
739 * the 'size' bytes of 'data'. */
740 static enum stream_content_type
741 stream_guess_content(const uint8_t *data
, ssize_t size
)
744 #define PAIR(A, B) (((A) << 8) | (B))
745 switch (PAIR(data
[0], data
[1])) {
746 case PAIR(0x16, 0x03): /* Handshake, version 3. */
749 return STREAM_JSONRPC
;
750 case PAIR(OFP10_VERSION
, 0 /* OFPT_HELLO */):
751 return STREAM_OPENFLOW
;
755 return STREAM_UNKNOWN
;
758 /* Returns a string represenation of 'type'. */
760 stream_content_type_to_string(enum stream_content_type type
)
770 case STREAM_OPENFLOW
:
778 /* Attempts to guess the content type of a stream whose first few bytes were
779 * the 'size' bytes of 'data'. If this is done successfully, and the guessed
780 * content type is other than 'expected_type', then log a message in vlog
781 * module 'module', naming 'stream_name' as the source, explaining what
782 * content was expected and what was actually received. */
784 stream_report_content(const void *data
, ssize_t size
,
785 enum stream_content_type expected_type
,
786 struct vlog_module
*module
, const char *stream_name
)
788 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 5);
789 enum stream_content_type actual_type
;
791 actual_type
= stream_guess_content(data
, size
);
792 if (actual_type
!= expected_type
&& actual_type
!= STREAM_UNKNOWN
) {
793 vlog_rate_limit(module
, VLL_WARN
, &rl
,
794 "%s: received %s data on %s channel",
796 stream_content_type_to_string(actual_type
),
797 stream_content_type_to_string(expected_type
));