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