Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2009, 2010, 2012, 2013, 2015 Nicira, Inc.
3 : : *
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:
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
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.
15 : : */
16 : :
17 : : #ifndef STREAM_PROVIDER_H
18 : : #define STREAM_PROVIDER_H 1
19 : :
20 : : #include <sys/types.h>
21 : : #include "stream.h"
22 : :
23 : : /* Active stream connection. */
24 : :
25 : : /* Active stream connection.
26 : : *
27 : : * This structure should be treated as opaque by implementation. */
28 : : struct stream {
29 : : const struct stream_class *class;
30 : : int state;
31 : : int error;
32 : : char *name;
33 : : };
34 : :
35 : : void stream_init(struct stream *, const struct stream_class *,
36 : : int connect_status, const char *name);
37 : 1902763 : static inline void stream_assert_class(const struct stream *stream,
38 : : const struct stream_class *class)
39 : : {
40 [ - + ]: 1902763 : ovs_assert(stream->class == class);
41 : 1902763 : }
42 : :
43 : : struct stream_class {
44 : : /* Prefix for connection names, e.g. "tcp", "ssl", "unix". */
45 : : const char *name;
46 : :
47 : : /* True if this stream needs periodic probes to verify connectivity. For
48 : : * streams which need probes, it can take a long time to notice the
49 : : * connection was dropped. */
50 : : bool needs_probes;
51 : :
52 : : /* Attempts to connect to a peer. 'name' is the full connection name
53 : : * provided by the user, e.g. "tcp:1.2.3.4". This name is useful for error
54 : : * messages but must not be modified.
55 : : *
56 : : * 'suffix' is a copy of 'name' following the colon and may be modified.
57 : : * 'dscp' is the DSCP value that the new connection should use in the IP
58 : : * packets it sends.
59 : : *
60 : : * Returns 0 if successful, otherwise a positive errno value. If
61 : : * successful, stores a pointer to the new connection in '*streamp'.
62 : : *
63 : : * The open function must not block waiting for a connection to complete.
64 : : * If the connection cannot be completed immediately, it should return
65 : : * EAGAIN (not EINPROGRESS, as returned by the connect system call) and
66 : : * continue the connection in the background. */
67 : : int (*open)(const char *name, char *suffix, struct stream **streamp,
68 : : uint8_t dscp);
69 : :
70 : : /* Closes 'stream' and frees associated memory. */
71 : : void (*close)(struct stream *stream);
72 : :
73 : : /* Tries to complete the connection on 'stream'. If 'stream''s connection
74 : : * is complete, returns 0 if the connection was successful or a positive
75 : : * errno value if it failed. If the connection is still in progress,
76 : : * returns EAGAIN.
77 : : *
78 : : * The connect function must not block waiting for the connection to
79 : : * complete; instead, it should return EAGAIN immediately. */
80 : : int (*connect)(struct stream *stream);
81 : :
82 : : /* Tries to receive up to 'n' bytes from 'stream' into 'buffer', and
83 : : * returns:
84 : : *
85 : : * - If successful, the number of bytes received (between 1 and 'n').
86 : : *
87 : : * - On error, a negative errno value.
88 : : *
89 : : * - 0, if the connection has been closed in the normal fashion.
90 : : *
91 : : * The recv function will not be passed a zero 'n'.
92 : : *
93 : : * The recv function must not block waiting for data to arrive. If no data
94 : : * have been received, it should return -EAGAIN immediately. */
95 : : ssize_t (*recv)(struct stream *stream, void *buffer, size_t n);
96 : :
97 : : /* Tries to send up to 'n' bytes of 'buffer' on 'stream', and returns:
98 : : *
99 : : * - If successful, the number of bytes sent (between 1 and 'n').
100 : : *
101 : : * - On error, a negative errno value.
102 : : *
103 : : * - Never returns 0.
104 : : *
105 : : * The send function will not be passed a zero 'n'.
106 : : *
107 : : * The send function must not block. If no bytes can be immediately
108 : : * accepted for transmission, it should return -EAGAIN immediately. */
109 : : ssize_t (*send)(struct stream *stream, const void *buffer, size_t n);
110 : :
111 : : /* Allows 'stream' to perform maintenance activities, such as flushing
112 : : * output buffers.
113 : : *
114 : : * May be null if 'stream' doesn't have anything to do here. */
115 : : void (*run)(struct stream *stream);
116 : :
117 : : /* Arranges for the poll loop to wake up when 'stream' needs to perform
118 : : * maintenance activities.
119 : : *
120 : : * May be null if 'stream' doesn't have anything to do here. */
121 : : void (*run_wait)(struct stream *stream);
122 : :
123 : : /* Arranges for the poll loop to wake up when 'stream' is ready to take an
124 : : * action of the given 'type'. */
125 : : void (*wait)(struct stream *stream, enum stream_wait_type type);
126 : : };
127 : :
128 : : /* Passive listener for incoming stream connections.
129 : : *
130 : : * This structure should be treated as opaque by stream implementations. */
131 : : struct pstream {
132 : : const struct pstream_class *class;
133 : : char *name;
134 : : ovs_be16 bound_port;
135 : : };
136 : :
137 : : void pstream_init(struct pstream *, const struct pstream_class *, const char *name);
138 : : void pstream_set_bound_port(struct pstream *, ovs_be16 bound_port);
139 : 1159958 : static inline void pstream_assert_class(const struct pstream *pstream,
140 : : const struct pstream_class *class)
141 : : {
142 [ - + ]: 1159958 : ovs_assert(pstream->class == class);
143 : 1159958 : }
144 : :
145 : : struct pstream_class {
146 : : /* Prefix for connection names, e.g. "ptcp", "pssl", "punix". */
147 : : const char *name;
148 : :
149 : : /* True if this pstream needs periodic probes to verify connectivity. For
150 : : * pstreams which need probes, it can take a long time to notice the
151 : : * connection was dropped. */
152 : : bool needs_probes;
153 : :
154 : : /* Attempts to start listening for stream connections. 'name' is the full
155 : : * connection name provided by the user, e.g. "ptcp:1234". This name is
156 : : * useful for error messages but must not be modified.
157 : : *
158 : : * 'suffix' is a copy of 'name' following the colon and may be modified.
159 : : * 'dscp' is the DSCP value that the new connection should use in the IP
160 : : * packets it sends.
161 : : *
162 : : * Returns 0 if successful, otherwise a positive errno value. If
163 : : * successful, stores a pointer to the new connection in '*pstreamp'.
164 : : *
165 : : * The listen function must not block. If the connection cannot be
166 : : * completed immediately, it should return EAGAIN (not EINPROGRESS, as
167 : : * returned by the connect system call) and continue the connection in the
168 : : * background. */
169 : : int (*listen)(const char *name, char *suffix, struct pstream **pstreamp,
170 : : uint8_t dscp);
171 : :
172 : : /* Closes 'pstream' and frees associated memory. */
173 : : void (*close)(struct pstream *pstream);
174 : :
175 : : /* Tries to accept a new connection on 'pstream'. If successful, stores
176 : : * the new connection in '*new_streamp' and returns 0. Otherwise, returns
177 : : * a positive errno value.
178 : : *
179 : : * The accept function must not block waiting for a connection. If no
180 : : * connection is ready to be accepted, it should return EAGAIN. */
181 : : int (*accept)(struct pstream *pstream, struct stream **new_streamp);
182 : :
183 : : /* Arranges for the poll loop to wake up when a connection is ready to be
184 : : * accepted on 'pstream'. */
185 : : void (*wait)(struct pstream *pstream);
186 : : };
187 : :
188 : : /* Active and passive stream classes. */
189 : : extern const struct stream_class tcp_stream_class;
190 : : extern const struct pstream_class ptcp_pstream_class;
191 : : #ifndef _WIN32
192 : : extern const struct stream_class unix_stream_class;
193 : : extern const struct pstream_class punix_pstream_class;
194 : : #else
195 : : extern const struct stream_class windows_stream_class;
196 : : extern const struct pstream_class pwindows_pstream_class;
197 : : #endif
198 : : #ifdef HAVE_OPENSSL
199 : : extern const struct stream_class ssl_stream_class;
200 : : extern const struct pstream_class pssl_pstream_class;
201 : : #endif
202 : :
203 : : #endif /* stream-provider.h */
|