blob: 4686e7f115f37769f2611a4b8076a8e956eaa16b [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>
Willy Tarreauf2943dc2012-10-26 20:10:28 +020026#include <common/memory.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020027#include <types/connection.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020028#include <types/listener.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010029#include <proto/obj_type.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020030
Willy Tarreauf2943dc2012-10-26 20:10:28 +020031extern struct pool_head *pool2_connection;
32
33/* perform minimal intializations, report 0 in case of error, 1 if OK. */
34int init_connection();
35
Willy Tarreau59f98392012-07-06 14:13:49 +020036/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020037 * provided by the connection's sock_ops. Returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020038 */
39int conn_fd_handler(int fd);
40
Willy Tarreau22cda212012-08-31 17:43:29 +020041/* receive a PROXY protocol header over a connection */
42int conn_recv_proxy(struct connection *conn, int flag);
Willy Tarreaue1e4a612012-10-05 00:10:55 +020043int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
Willy Tarreau22cda212012-08-31 17:43:29 +020044
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020045/* calls the init() function of the transport layer if any.
46 * Returns <0 in case of error.
Willy Tarreau15678ef2012-08-31 13:54:11 +020047 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020048static inline int conn_xprt_init(struct connection *conn)
Willy Tarreau15678ef2012-08-31 13:54:11 +020049{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020050 if (conn->xprt && conn->xprt->init)
51 return conn->xprt->init(conn);
Willy Tarreau15678ef2012-08-31 13:54:11 +020052 return 0;
53}
54
Willy Tarreau6c03a642012-10-12 17:00:05 +020055/* Calls the close() function of the transport layer if any, and always unsets
Willy Tarreau1e954912012-10-12 17:50:05 +020056 * the transport layer. However this is not done if the CO_FL_XPRT_TRACKED flag
57 * is set, which allows logs to take data from the transport layer very late if
58 * needed.
Willy Tarreau6c03a642012-10-12 17:00:05 +020059 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020060static inline void conn_xprt_close(struct connection *conn)
Willy Tarreau8b117082012-08-06 15:06:49 +020061{
Willy Tarreau1e954912012-10-12 17:50:05 +020062 if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) {
Willy Tarreau6c03a642012-10-12 17:00:05 +020063 if (conn->xprt->close)
64 conn->xprt->close(conn);
65 conn->xprt = NULL;
66 }
Willy Tarreau8b117082012-08-06 15:06:49 +020067}
68
Willy Tarreaue9dfa792012-09-01 17:26:16 +020069/* Update polling on connection <c>'s file descriptor depending on its current
70 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
71 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
72 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020073 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020074 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020075void conn_update_sock_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020076
Willy Tarreaue9dfa792012-09-01 17:26:16 +020077/* Update polling on connection <c>'s file descriptor depending on its current
78 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
79 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
80 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020081 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020082 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020083void conn_update_data_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020084
Willy Tarreau5f1504f2012-10-04 23:55:57 +020085/* This callback is used to send a valid PROXY protocol line to a socket being
86 * established from the local machine. It sets the protocol addresses to the
87 * local and remote address. This is typically used with health checks or when
88 * it is not possible to determine the other end's address. It returns 0 if it
89 * fails in a fatal way or needs to poll to go further, otherwise it returns
90 * non-zero and removes itself from the connection's flags (the bit is provided
91 * in <flag> by the caller). It is designed to be called by the connection
92 * handler and relies on it to commit polling changes. Note that this function
93 * expects to be able to send the whole line at once, which should always be
94 * possible since it is supposed to start at the first byte of the outgoing
95 * data segment.
96 */
97int conn_local_send_proxy(struct connection *conn, unsigned int flag);
98
Willy Tarreaue9dfa792012-09-01 17:26:16 +020099/* inspects c->flags and returns non-zero if DATA ENA changes from the CURR ENA
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100100 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200101 * non-zero is also returned if an error was reported on the connection. This
102 * function is used quite often and is inlined. In order to proceed optimally
103 * with very little code and CPU cycles, the bits are arranged so that a change
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100104 * can be detected by a few left shifts, a xor, and a mask. These operations
105 * detect when W&D are both enabled for either direction, when C&D differ for
106 * either direction and when Error is set. The trick consists in first keeping
107 * only the bits we're interested in, since they don't collide when shifted,
108 * and to perform the AND at the end. In practice, the compiler is able to
109 * replace the last AND with a TEST in boolean conditions. This results in
110 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200111 */
112static inline unsigned int conn_data_polling_changes(const struct connection *c)
113{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100114 unsigned int f = c->flags;
115 f &= CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA | CO_FL_CURR_WR_ENA |
116 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
117
118 f = (f & (f << 2)) | /* test W & D */
119 ((f ^ (f << 1)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ D */
120 return f & (CO_FL_WAIT_WR | CO_FL_WAIT_RD | CO_FL_CURR_WR_ENA | CO_FL_CURR_RD_ENA | CO_FL_ERROR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200121}
122
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200123/* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100124 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200125 * non-zero is also returned if an error was reported on the connection. This
126 * function is used quite often and is inlined. In order to proceed optimally
127 * with very little code and CPU cycles, the bits are arranged so that a change
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100128 * can be detected by a few left shifts, a xor, and a mask. These operations
129 * detect when W&S are both enabled for either direction, when C&S differ for
130 * either direction and when Error is set. The trick consists in first keeping
131 * only the bits we're interested in, since they don't collide when shifted,
132 * and to perform the AND at the end. In practice, the compiler is able to
133 * replace the last AND with a TEST in boolean conditions. This results in
134 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200135 */
136static inline unsigned int conn_sock_polling_changes(const struct connection *c)
137{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100138 unsigned int f = c->flags;
139 f &= CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA | CO_FL_CURR_WR_ENA |
140 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
141
142 f = (f & (f << 3)) | /* test W & S */
143 ((f ^ (f << 2)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ S */
144 return f & (CO_FL_WAIT_WR | CO_FL_WAIT_RD | CO_FL_CURR_WR_ENA | CO_FL_CURR_RD_ENA | CO_FL_ERROR);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200145}
146
147/* Automatically updates polling on connection <c> depending on the DATA flags
148 * if no handshake is in progress.
149 */
150static inline void conn_cond_update_data_polling(struct connection *c)
151{
152 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
153 conn_update_data_polling(c);
154}
155
156/* Automatically updates polling on connection <c> depending on the SOCK flags
157 * if a handshake is in progress.
158 */
159static inline void conn_cond_update_sock_polling(struct connection *c)
160{
161 if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
162 conn_update_sock_polling(c);
163}
164
165/* Automatically update polling on connection <c> depending on the DATA and
166 * SOCK flags, and on whether a handshake is in progress or not. This may be
167 * called at any moment when there is a doubt about the effectiveness of the
168 * polling state, for instance when entering or leaving the handshake state.
169 */
170static inline void conn_cond_update_polling(struct connection *c)
171{
172 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
173 conn_update_data_polling(c);
174 else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
175 conn_update_sock_polling(c);
176}
177
178/***** Event manipulation primitives for use by DATA I/O callbacks *****/
179/* The __conn_* versions do not propagate to lower layers and are only meant
180 * to be used by handlers called by the connection handler. The other ones
181 * may be used anywhere.
182 */
183static inline void __conn_data_want_recv(struct connection *c)
184{
185 c->flags |= CO_FL_DATA_RD_ENA;
186}
187
188static inline void __conn_data_stop_recv(struct connection *c)
189{
190 c->flags &= ~CO_FL_DATA_RD_ENA;
191}
192
193static inline void __conn_data_poll_recv(struct connection *c)
194{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200195 c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200196}
197
198static inline void __conn_data_want_send(struct connection *c)
199{
200 c->flags |= CO_FL_DATA_WR_ENA;
201}
202
203static inline void __conn_data_stop_send(struct connection *c)
204{
205 c->flags &= ~CO_FL_DATA_WR_ENA;
206}
207
208static inline void __conn_data_poll_send(struct connection *c)
209{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200210 c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200211}
212
213static inline void __conn_data_stop_both(struct connection *c)
214{
215 c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA);
216}
217
218static inline void conn_data_want_recv(struct connection *c)
219{
220 __conn_data_want_recv(c);
221 conn_cond_update_data_polling(c);
222}
223
224static inline void conn_data_stop_recv(struct connection *c)
225{
226 __conn_data_stop_recv(c);
227 conn_cond_update_data_polling(c);
228}
229
230static inline void conn_data_poll_recv(struct connection *c)
231{
232 __conn_data_poll_recv(c);
233 conn_cond_update_data_polling(c);
234}
235
236static inline void conn_data_want_send(struct connection *c)
237{
238 __conn_data_want_send(c);
239 conn_cond_update_data_polling(c);
240}
241
242static inline void conn_data_stop_send(struct connection *c)
243{
244 __conn_data_stop_send(c);
245 conn_cond_update_data_polling(c);
246}
247
248static inline void conn_data_poll_send(struct connection *c)
249{
250 __conn_data_poll_send(c);
251 conn_cond_update_data_polling(c);
252}
253
254static inline void conn_data_stop_both(struct connection *c)
255{
256 __conn_data_stop_both(c);
257 conn_cond_update_data_polling(c);
258}
259
260/***** Event manipulation primitives for use by handshake I/O callbacks *****/
261/* The __conn_* versions do not propagate to lower layers and are only meant
262 * to be used by handlers called by the connection handler. The other ones
263 * may be used anywhere.
264 */
265static inline void __conn_sock_want_recv(struct connection *c)
266{
267 c->flags |= CO_FL_SOCK_RD_ENA;
268}
269
270static inline void __conn_sock_stop_recv(struct connection *c)
271{
272 c->flags &= ~CO_FL_SOCK_RD_ENA;
273}
274
275static inline void __conn_sock_poll_recv(struct connection *c)
276{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200277 c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200278}
279
280static inline void __conn_sock_want_send(struct connection *c)
281{
282 c->flags |= CO_FL_SOCK_WR_ENA;
283}
284
285static inline void __conn_sock_stop_send(struct connection *c)
286{
287 c->flags &= ~CO_FL_SOCK_WR_ENA;
288}
289
290static inline void __conn_sock_poll_send(struct connection *c)
291{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200292 c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200293}
294
295static inline void __conn_sock_stop_both(struct connection *c)
296{
297 c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA);
298}
299
300static inline void conn_sock_want_recv(struct connection *c)
301{
302 __conn_sock_want_recv(c);
303 conn_cond_update_sock_polling(c);
304}
305
306static inline void conn_sock_stop_recv(struct connection *c)
307{
308 __conn_sock_stop_recv(c);
309 conn_cond_update_sock_polling(c);
310}
311
312static inline void conn_sock_poll_recv(struct connection *c)
313{
314 __conn_sock_poll_recv(c);
315 conn_cond_update_sock_polling(c);
316}
317
318static inline void conn_sock_want_send(struct connection *c)
319{
320 __conn_sock_want_send(c);
321 conn_cond_update_sock_polling(c);
322}
323
324static inline void conn_sock_stop_send(struct connection *c)
325{
326 __conn_sock_stop_send(c);
327 conn_cond_update_sock_polling(c);
328}
329
330static inline void conn_sock_poll_send(struct connection *c)
331{
332 __conn_sock_poll_send(c);
333 conn_cond_update_sock_polling(c);
334}
335
336static inline void conn_sock_stop_both(struct connection *c)
337{
338 __conn_sock_stop_both(c);
339 conn_cond_update_sock_polling(c);
340}
Willy Tarreau8b117082012-08-06 15:06:49 +0200341
Willy Tarreau3af56a92012-08-20 16:55:48 +0200342/* shutdown management */
343static inline void conn_sock_read0(struct connection *c)
344{
345 c->flags |= CO_FL_SOCK_RD_SH;
346 __conn_sock_stop_recv(c);
347}
348
349static inline void conn_data_read0(struct connection *c)
350{
351 c->flags |= CO_FL_DATA_RD_SH;
352 __conn_data_stop_recv(c);
353}
354
355static inline void conn_sock_shutw(struct connection *c)
356{
357 c->flags |= CO_FL_SOCK_WR_SH;
358 __conn_sock_stop_send(c);
359}
360
361static inline void conn_data_shutw(struct connection *c)
362{
363 c->flags |= CO_FL_DATA_WR_SH;
364 __conn_data_stop_send(c);
365}
366
367/* detect sock->data read0 transition */
368static inline int conn_data_read0_pending(struct connection *c)
369{
370 return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH;
371}
372
373/* detect data->sock shutw transition */
374static inline int conn_sock_shutw_pending(struct connection *c)
375{
376 return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH;
377}
378
Willy Tarreau986a9d22012-08-30 21:11:38 +0200379/* Retrieves the connection's source address */
380static inline void conn_get_from_addr(struct connection *conn)
381{
382 if (conn->flags & CO_FL_ADDR_FROM_SET)
383 return;
384
385 if (!conn->ctrl || !conn->ctrl->get_src)
386 return;
387
388 if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100389 sizeof(conn->addr.from),
390 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200391 return;
392 conn->flags |= CO_FL_ADDR_FROM_SET;
393}
394
395/* Retrieves the connection's original destination address */
396static inline void conn_get_to_addr(struct connection *conn)
397{
398 if (conn->flags & CO_FL_ADDR_TO_SET)
399 return;
400
401 if (!conn->ctrl || !conn->ctrl->get_dst)
402 return;
403
404 if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100405 sizeof(conn->addr.to),
406 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200407 return;
408 conn->flags |= CO_FL_ADDR_TO_SET;
409}
410
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200411/* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */
412static inline void conn_assign(struct connection *conn, const struct data_cb *data,
413 const struct protocol *ctrl, const struct xprt_ops *xprt,
414 void *owner)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200415{
Willy Tarreau74beec32012-10-03 00:41:04 +0200416 conn->data = data;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200417 conn->ctrl = ctrl;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200418 conn->xprt = xprt;
Willy Tarreaucd379952012-09-27 22:14:33 +0200419 conn->owner = owner;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200420}
421
422/* prepares a connection with the appropriate data, ctrl, transport layers, and
423 * owner. The transport state and context are set to 0.
424 */
425static inline void conn_prepare(struct connection *conn, const struct data_cb *data,
426 const struct protocol *ctrl, const struct xprt_ops *xprt,
427 void *owner)
428{
429 conn_assign(conn, data, ctrl, xprt, owner);
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200430 conn->xprt_st = 0;
431 conn->xprt_ctx = NULL;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200432}
Willy Tarreau986a9d22012-08-30 21:11:38 +0200433
Willy Tarreau59f98392012-07-06 14:13:49 +0200434#endif /* _PROTO_CONNECTION_H */
435
436/*
437 * Local variables:
438 * c-indent-level: 8
439 * c-basic-offset: 8
440 * End:
441 */