blob: 07568aaefa1f6cd4117d2f5c140d2814b65e1847 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * include/proto/connection.h
3 * This file contains connection function prototypes
4 *
5 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _PROTO_CONNECTION_H
23#define _PROTO_CONNECTION_H
24
25#include <common/config.h>
26#include <types/connection.h>
27
28/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020029 * provided by the connection's sock_ops. Returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020030 */
31int conn_fd_handler(int fd);
32
Willy Tarreau8b117082012-08-06 15:06:49 +020033/* Calls the close() function of the data layer if any */
34static inline void conn_data_close(struct connection *conn)
35{
Willy Tarreau4a36b562012-08-06 19:31:45 +020036 if (conn->data && conn->data->close)
Willy Tarreau8b117082012-08-06 15:06:49 +020037 conn->data->close(conn);
38}
39
Willy Tarreaufae44992012-08-20 14:02:10 +020040/* Calls the snd_buf() function of the data layer if any, otherwise
41 * returns 0.
42 */
43static inline int conn_data_snd_buf(struct connection *conn)
44{
45 if (!conn->data->snd_buf)
46 return 0;
47 return conn->data->snd_buf(conn);
48}
49
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020050/* set polling depending on the change between the CURR part of the
51 * flags and the new flags in connection C. The connection flags are
52 * updated with the new flags at the end of the operation. Only the bits
53 * relevant to CO_FL_CURR_* from <flags> are considered.
54 */
55void conn_set_polling(struct connection *c, unsigned int new);
56
57/* update polling depending on the change between the CURR part of the
58 * flags and the DATA part of the flags in connection C. The connection
59 * is assumed to already be in the data phase.
60 */
61static inline void conn_update_data_polling(struct connection *c)
62{
63 conn_set_polling(c, c->flags << 8);
64}
65
66/* update polling depending on the change between the CURR part of the
67 * flags and the SOCK part of the flags in connection C. The connection
68 * is assumed to already be in the handshake phase.
69 */
70static inline void conn_update_sock_polling(struct connection *c)
71{
72 conn_set_polling(c, c->flags << 4);
73}
74
75/* returns non-zero if data flags from c->flags changes from what is in the
76 * current section of c->flags.
77 */
78static inline unsigned int conn_data_polling_changes(const struct connection *c)
79{
80 return ((c->flags << 8) ^ c->flags) & 0xF0000000;
81}
82
83/* returns non-zero if sock flags from c->flags changes from what is in the
84 * current section of c->flags.
85 */
86static inline unsigned int conn_sock_polling_changes(const struct connection *c)
87{
88 return ((c->flags << 4) ^ c->flags) & 0xF0000000;
89}
90
91/* Automatically updates polling on connection <c> depending on the DATA flags
92 * if no handshake is in progress.
93 */
94static inline void conn_cond_update_data_polling(struct connection *c)
95{
96 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
97 conn_update_data_polling(c);
98}
99
100/* Automatically updates polling on connection <c> depending on the SOCK flags
101 * if a handshake is in progress.
102 */
103static inline void conn_cond_update_sock_polling(struct connection *c)
104{
105 if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
106 conn_update_sock_polling(c);
107}
108
109/* Automatically update polling on connection <c> depending on the DATA and
110 * SOCK flags, and on whether a handshake is in progress or not. This may be
111 * called at any moment when there is a doubt about the effectiveness of the
112 * polling state, for instance when entering or leaving the handshake state.
113 */
114static inline void conn_cond_update_polling(struct connection *c)
115{
116 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
117 conn_update_data_polling(c);
118 else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
119 conn_update_sock_polling(c);
120}
121
122/***** Event manipulation primitives for use by DATA I/O callbacks *****/
123/* The __conn_* versions do not propagate to lower layers and are only meant
124 * to be used by handlers called by the connection handler. The other ones
125 * may be used anywhere.
126 */
127static inline void __conn_data_want_recv(struct connection *c)
128{
129 c->flags |= CO_FL_DATA_RD_ENA;
130}
131
132static inline void __conn_data_stop_recv(struct connection *c)
133{
134 c->flags &= ~CO_FL_DATA_RD_ENA;
135}
136
137static inline void __conn_data_poll_recv(struct connection *c)
138{
139 c->flags |= CO_FL_DATA_RD_POL | CO_FL_DATA_RD_ENA;
140}
141
142static inline void __conn_data_want_send(struct connection *c)
143{
144 c->flags |= CO_FL_DATA_WR_ENA;
145}
146
147static inline void __conn_data_stop_send(struct connection *c)
148{
149 c->flags &= ~CO_FL_DATA_WR_ENA;
150}
151
152static inline void __conn_data_poll_send(struct connection *c)
153{
154 c->flags |= CO_FL_DATA_WR_POL | CO_FL_DATA_WR_ENA;
155}
156
157static inline void __conn_data_stop_both(struct connection *c)
158{
159 c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA);
160}
161
162static inline void conn_data_want_recv(struct connection *c)
163{
164 __conn_data_want_recv(c);
165 conn_cond_update_data_polling(c);
166}
167
168static inline void conn_data_stop_recv(struct connection *c)
169{
170 __conn_data_stop_recv(c);
171 conn_cond_update_data_polling(c);
172}
173
174static inline void conn_data_poll_recv(struct connection *c)
175{
176 __conn_data_poll_recv(c);
177 conn_cond_update_data_polling(c);
178}
179
180static inline void conn_data_want_send(struct connection *c)
181{
182 __conn_data_want_send(c);
183 conn_cond_update_data_polling(c);
184}
185
186static inline void conn_data_stop_send(struct connection *c)
187{
188 __conn_data_stop_send(c);
189 conn_cond_update_data_polling(c);
190}
191
192static inline void conn_data_poll_send(struct connection *c)
193{
194 __conn_data_poll_send(c);
195 conn_cond_update_data_polling(c);
196}
197
198static inline void conn_data_stop_both(struct connection *c)
199{
200 __conn_data_stop_both(c);
201 conn_cond_update_data_polling(c);
202}
203
204/***** Event manipulation primitives for use by handshake I/O callbacks *****/
205/* The __conn_* versions do not propagate to lower layers and are only meant
206 * to be used by handlers called by the connection handler. The other ones
207 * may be used anywhere.
208 */
209static inline void __conn_sock_want_recv(struct connection *c)
210{
211 c->flags |= CO_FL_SOCK_RD_ENA;
212}
213
214static inline void __conn_sock_stop_recv(struct connection *c)
215{
216 c->flags &= ~CO_FL_SOCK_RD_ENA;
217}
218
219static inline void __conn_sock_poll_recv(struct connection *c)
220{
221 c->flags |= CO_FL_SOCK_RD_POL | CO_FL_SOCK_RD_ENA;
222}
223
224static inline void __conn_sock_want_send(struct connection *c)
225{
226 c->flags |= CO_FL_SOCK_WR_ENA;
227}
228
229static inline void __conn_sock_stop_send(struct connection *c)
230{
231 c->flags &= ~CO_FL_SOCK_WR_ENA;
232}
233
234static inline void __conn_sock_poll_send(struct connection *c)
235{
236 c->flags |= CO_FL_SOCK_WR_POL | CO_FL_SOCK_WR_ENA;
237}
238
239static inline void __conn_sock_stop_both(struct connection *c)
240{
241 c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA);
242}
243
244static inline void conn_sock_want_recv(struct connection *c)
245{
246 __conn_sock_want_recv(c);
247 conn_cond_update_sock_polling(c);
248}
249
250static inline void conn_sock_stop_recv(struct connection *c)
251{
252 __conn_sock_stop_recv(c);
253 conn_cond_update_sock_polling(c);
254}
255
256static inline void conn_sock_poll_recv(struct connection *c)
257{
258 __conn_sock_poll_recv(c);
259 conn_cond_update_sock_polling(c);
260}
261
262static inline void conn_sock_want_send(struct connection *c)
263{
264 __conn_sock_want_send(c);
265 conn_cond_update_sock_polling(c);
266}
267
268static inline void conn_sock_stop_send(struct connection *c)
269{
270 __conn_sock_stop_send(c);
271 conn_cond_update_sock_polling(c);
272}
273
274static inline void conn_sock_poll_send(struct connection *c)
275{
276 __conn_sock_poll_send(c);
277 conn_cond_update_sock_polling(c);
278}
279
280static inline void conn_sock_stop_both(struct connection *c)
281{
282 __conn_sock_stop_both(c);
283 conn_cond_update_sock_polling(c);
284}
Willy Tarreau8b117082012-08-06 15:06:49 +0200285
Willy Tarreau59f98392012-07-06 14:13:49 +0200286#endif /* _PROTO_CONNECTION_H */
287
288/*
289 * Local variables:
290 * c-indent-level: 8
291 * c-basic-offset: 8
292 * End:
293 */