blob: 35b6312bd55c5880153e560b572d213c607b3c21 [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 Tarreau2b199c92012-11-23 17:32:21 +010029#include <proto/fd.h>
Willy Tarreau3fdb3662012-11-12 00:42:33 +010030#include <proto/obj_type.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020031
Willy Tarreauf2943dc2012-10-26 20:10:28 +020032extern struct pool_head *pool2_connection;
33
34/* perform minimal intializations, report 0 in case of error, 1 if OK. */
35int init_connection();
36
Willy Tarreau59f98392012-07-06 14:13:49 +020037/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020038 * provided by the connection's sock_ops. Returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020039 */
40int conn_fd_handler(int fd);
41
Willy Tarreau22cda212012-08-31 17:43:29 +020042/* receive a PROXY protocol header over a connection */
43int conn_recv_proxy(struct connection *conn, int flag);
Willy Tarreaue1e4a612012-10-05 00:10:55 +020044int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
Willy Tarreau22cda212012-08-31 17:43:29 +020045
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020046/* calls the init() function of the transport layer if any.
47 * Returns <0 in case of error.
Willy Tarreau15678ef2012-08-31 13:54:11 +020048 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020049static inline int conn_xprt_init(struct connection *conn)
Willy Tarreau15678ef2012-08-31 13:54:11 +020050{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020051 if (conn->xprt && conn->xprt->init)
52 return conn->xprt->init(conn);
Willy Tarreau15678ef2012-08-31 13:54:11 +020053 return 0;
54}
55
Willy Tarreau6c03a642012-10-12 17:00:05 +020056/* Calls the close() function of the transport layer if any, and always unsets
Willy Tarreau1e954912012-10-12 17:50:05 +020057 * the transport layer. However this is not done if the CO_FL_XPRT_TRACKED flag
58 * is set, which allows logs to take data from the transport layer very late if
59 * needed.
Willy Tarreau6c03a642012-10-12 17:00:05 +020060 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020061static inline void conn_xprt_close(struct connection *conn)
Willy Tarreau8b117082012-08-06 15:06:49 +020062{
Willy Tarreau1e954912012-10-12 17:50:05 +020063 if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) {
Willy Tarreau6c03a642012-10-12 17:00:05 +020064 if (conn->xprt->close)
65 conn->xprt->close(conn);
66 conn->xprt = NULL;
67 }
Willy Tarreau8b117082012-08-06 15:06:49 +020068}
69
Willy Tarreau2b199c92012-11-23 17:32:21 +010070/* If the connection still has a transport layer, then call its close() function
71 * if any, and delete the file descriptor if a control layer is set. This is
72 * used to close everything at once and atomically. However this is not done if
73 * the CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the
74 * transport layer very late if needed.
75 */
76static inline void conn_full_close(struct connection *conn)
77{
78 if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) {
79 if (conn->xprt->close)
80 conn->xprt->close(conn);
81 if (conn->ctrl)
82 fd_delete(conn->t.sock.fd);
83 conn->xprt = NULL;
84 }
85}
86
Willy Tarreaue9dfa792012-09-01 17:26:16 +020087/* Update polling on connection <c>'s file descriptor depending on its current
88 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
89 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
90 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020091 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020092 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020093void conn_update_sock_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020094
Willy Tarreaue9dfa792012-09-01 17:26:16 +020095/* Update polling on connection <c>'s file descriptor depending on its current
96 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
97 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
98 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020099 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200100 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200101void conn_update_data_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200102
Willy Tarreau5f1504f2012-10-04 23:55:57 +0200103/* This callback is used to send a valid PROXY protocol line to a socket being
104 * established from the local machine. It sets the protocol addresses to the
105 * local and remote address. This is typically used with health checks or when
106 * it is not possible to determine the other end's address. It returns 0 if it
107 * fails in a fatal way or needs to poll to go further, otherwise it returns
108 * non-zero and removes itself from the connection's flags (the bit is provided
109 * in <flag> by the caller). It is designed to be called by the connection
110 * handler and relies on it to commit polling changes. Note that this function
111 * expects to be able to send the whole line at once, which should always be
112 * possible since it is supposed to start at the first byte of the outgoing
113 * data segment.
114 */
115int conn_local_send_proxy(struct connection *conn, unsigned int flag);
116
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200117/* inspects c->flags and returns non-zero if DATA ENA changes from the CURR ENA
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100118 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200119 * non-zero is also returned if an error was reported on the connection. This
120 * function is used quite often and is inlined. In order to proceed optimally
121 * with very little code and CPU cycles, the bits are arranged so that a change
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100122 * can be detected by a few left shifts, a xor, and a mask. These operations
123 * detect when W&D are both enabled for either direction, when C&D differ for
124 * either direction and when Error is set. The trick consists in first keeping
125 * only the bits we're interested in, since they don't collide when shifted,
126 * and to perform the AND at the end. In practice, the compiler is able to
127 * replace the last AND with a TEST in boolean conditions. This results in
128 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200129 */
130static inline unsigned int conn_data_polling_changes(const struct connection *c)
131{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100132 unsigned int f = c->flags;
133 f &= CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA | CO_FL_CURR_WR_ENA |
134 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
135
136 f = (f & (f << 2)) | /* test W & D */
137 ((f ^ (f << 1)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ D */
138 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 +0200139}
140
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200141/* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100142 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200143 * non-zero is also returned if an error was reported on the connection. This
144 * function is used quite often and is inlined. In order to proceed optimally
145 * with very little code and CPU cycles, the bits are arranged so that a change
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100146 * can be detected by a few left shifts, a xor, and a mask. These operations
147 * detect when W&S are both enabled for either direction, when C&S differ for
148 * either direction and when Error is set. The trick consists in first keeping
149 * only the bits we're interested in, since they don't collide when shifted,
150 * and to perform the AND at the end. In practice, the compiler is able to
151 * replace the last AND with a TEST in boolean conditions. This results in
152 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200153 */
154static inline unsigned int conn_sock_polling_changes(const struct connection *c)
155{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100156 unsigned int f = c->flags;
157 f &= CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA | CO_FL_CURR_WR_ENA |
158 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
159
160 f = (f & (f << 3)) | /* test W & S */
161 ((f ^ (f << 2)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ S */
162 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 +0200163}
164
165/* Automatically updates polling on connection <c> depending on the DATA flags
166 * if no handshake is in progress.
167 */
168static inline void conn_cond_update_data_polling(struct connection *c)
169{
170 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
171 conn_update_data_polling(c);
172}
173
174/* Automatically updates polling on connection <c> depending on the SOCK flags
175 * if a handshake is in progress.
176 */
177static inline void conn_cond_update_sock_polling(struct connection *c)
178{
179 if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
180 conn_update_sock_polling(c);
181}
182
Willy Tarreau36fb02c2012-11-24 11:09:07 +0100183/* Stop all polling on the fd. This might be used when an error is encountered
184 * for example.
185 */
186static inline void conn_stop_polling(struct connection *c)
187{
188 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
189 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
190 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
191 fd_stop_both(c->t.sock.fd);
192}
193
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200194/* Automatically update polling on connection <c> depending on the DATA and
195 * SOCK flags, and on whether a handshake is in progress or not. This may be
196 * called at any moment when there is a doubt about the effectiveness of the
197 * polling state, for instance when entering or leaving the handshake state.
198 */
199static inline void conn_cond_update_polling(struct connection *c)
200{
Willy Tarreau36fb02c2012-11-24 11:09:07 +0100201 if (unlikely(c->flags & CO_FL_ERROR))
202 conn_stop_polling(c);
203 else if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200204 conn_update_data_polling(c);
205 else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
206 conn_update_sock_polling(c);
207}
208
209/***** Event manipulation primitives for use by DATA I/O callbacks *****/
210/* The __conn_* versions do not propagate to lower layers and are only meant
211 * to be used by handlers called by the connection handler. The other ones
212 * may be used anywhere.
213 */
214static inline void __conn_data_want_recv(struct connection *c)
215{
216 c->flags |= CO_FL_DATA_RD_ENA;
217}
218
219static inline void __conn_data_stop_recv(struct connection *c)
220{
221 c->flags &= ~CO_FL_DATA_RD_ENA;
222}
223
224static inline void __conn_data_poll_recv(struct connection *c)
225{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200226 c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200227}
228
229static inline void __conn_data_want_send(struct connection *c)
230{
231 c->flags |= CO_FL_DATA_WR_ENA;
232}
233
234static inline void __conn_data_stop_send(struct connection *c)
235{
236 c->flags &= ~CO_FL_DATA_WR_ENA;
237}
238
239static inline void __conn_data_poll_send(struct connection *c)
240{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200241 c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200242}
243
244static inline void __conn_data_stop_both(struct connection *c)
245{
246 c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA);
247}
248
249static inline void conn_data_want_recv(struct connection *c)
250{
251 __conn_data_want_recv(c);
252 conn_cond_update_data_polling(c);
253}
254
255static inline void conn_data_stop_recv(struct connection *c)
256{
257 __conn_data_stop_recv(c);
258 conn_cond_update_data_polling(c);
259}
260
261static inline void conn_data_poll_recv(struct connection *c)
262{
263 __conn_data_poll_recv(c);
264 conn_cond_update_data_polling(c);
265}
266
267static inline void conn_data_want_send(struct connection *c)
268{
269 __conn_data_want_send(c);
270 conn_cond_update_data_polling(c);
271}
272
273static inline void conn_data_stop_send(struct connection *c)
274{
275 __conn_data_stop_send(c);
276 conn_cond_update_data_polling(c);
277}
278
279static inline void conn_data_poll_send(struct connection *c)
280{
281 __conn_data_poll_send(c);
282 conn_cond_update_data_polling(c);
283}
284
285static inline void conn_data_stop_both(struct connection *c)
286{
287 __conn_data_stop_both(c);
288 conn_cond_update_data_polling(c);
289}
290
291/***** Event manipulation primitives for use by handshake I/O callbacks *****/
292/* The __conn_* versions do not propagate to lower layers and are only meant
293 * to be used by handlers called by the connection handler. The other ones
294 * may be used anywhere.
295 */
296static inline void __conn_sock_want_recv(struct connection *c)
297{
298 c->flags |= CO_FL_SOCK_RD_ENA;
299}
300
301static inline void __conn_sock_stop_recv(struct connection *c)
302{
303 c->flags &= ~CO_FL_SOCK_RD_ENA;
304}
305
306static inline void __conn_sock_poll_recv(struct connection *c)
307{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200308 c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200309}
310
311static inline void __conn_sock_want_send(struct connection *c)
312{
313 c->flags |= CO_FL_SOCK_WR_ENA;
314}
315
316static inline void __conn_sock_stop_send(struct connection *c)
317{
318 c->flags &= ~CO_FL_SOCK_WR_ENA;
319}
320
321static inline void __conn_sock_poll_send(struct connection *c)
322{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200323 c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200324}
325
326static inline void __conn_sock_stop_both(struct connection *c)
327{
328 c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA);
329}
330
331static inline void conn_sock_want_recv(struct connection *c)
332{
333 __conn_sock_want_recv(c);
334 conn_cond_update_sock_polling(c);
335}
336
337static inline void conn_sock_stop_recv(struct connection *c)
338{
339 __conn_sock_stop_recv(c);
340 conn_cond_update_sock_polling(c);
341}
342
343static inline void conn_sock_poll_recv(struct connection *c)
344{
345 __conn_sock_poll_recv(c);
346 conn_cond_update_sock_polling(c);
347}
348
349static inline void conn_sock_want_send(struct connection *c)
350{
351 __conn_sock_want_send(c);
352 conn_cond_update_sock_polling(c);
353}
354
355static inline void conn_sock_stop_send(struct connection *c)
356{
357 __conn_sock_stop_send(c);
358 conn_cond_update_sock_polling(c);
359}
360
361static inline void conn_sock_poll_send(struct connection *c)
362{
363 __conn_sock_poll_send(c);
364 conn_cond_update_sock_polling(c);
365}
366
367static inline void conn_sock_stop_both(struct connection *c)
368{
369 __conn_sock_stop_both(c);
370 conn_cond_update_sock_polling(c);
371}
Willy Tarreau8b117082012-08-06 15:06:49 +0200372
Willy Tarreau3af56a92012-08-20 16:55:48 +0200373/* shutdown management */
374static inline void conn_sock_read0(struct connection *c)
375{
376 c->flags |= CO_FL_SOCK_RD_SH;
377 __conn_sock_stop_recv(c);
378}
379
380static inline void conn_data_read0(struct connection *c)
381{
382 c->flags |= CO_FL_DATA_RD_SH;
383 __conn_data_stop_recv(c);
384}
385
386static inline void conn_sock_shutw(struct connection *c)
387{
388 c->flags |= CO_FL_SOCK_WR_SH;
389 __conn_sock_stop_send(c);
390}
391
392static inline void conn_data_shutw(struct connection *c)
393{
394 c->flags |= CO_FL_DATA_WR_SH;
395 __conn_data_stop_send(c);
396}
397
398/* detect sock->data read0 transition */
399static inline int conn_data_read0_pending(struct connection *c)
400{
401 return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH;
402}
403
404/* detect data->sock shutw transition */
405static inline int conn_sock_shutw_pending(struct connection *c)
406{
407 return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH;
408}
409
Willy Tarreau986a9d22012-08-30 21:11:38 +0200410/* Retrieves the connection's source address */
411static inline void conn_get_from_addr(struct connection *conn)
412{
413 if (conn->flags & CO_FL_ADDR_FROM_SET)
414 return;
415
416 if (!conn->ctrl || !conn->ctrl->get_src)
417 return;
418
419 if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100420 sizeof(conn->addr.from),
421 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200422 return;
423 conn->flags |= CO_FL_ADDR_FROM_SET;
424}
425
426/* Retrieves the connection's original destination address */
427static inline void conn_get_to_addr(struct connection *conn)
428{
429 if (conn->flags & CO_FL_ADDR_TO_SET)
430 return;
431
432 if (!conn->ctrl || !conn->ctrl->get_dst)
433 return;
434
435 if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to,
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100436 sizeof(conn->addr.to),
437 obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1)
Willy Tarreau986a9d22012-08-30 21:11:38 +0200438 return;
439 conn->flags |= CO_FL_ADDR_TO_SET;
440}
441
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200442/* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */
443static inline void conn_assign(struct connection *conn, const struct data_cb *data,
444 const struct protocol *ctrl, const struct xprt_ops *xprt,
445 void *owner)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200446{
Willy Tarreau74beec32012-10-03 00:41:04 +0200447 conn->data = data;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200448 conn->ctrl = ctrl;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200449 conn->xprt = xprt;
Willy Tarreaucd379952012-09-27 22:14:33 +0200450 conn->owner = owner;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200451}
452
453/* prepares a connection with the appropriate data, ctrl, transport layers, and
454 * owner. The transport state and context are set to 0.
455 */
456static inline void conn_prepare(struct connection *conn, const struct data_cb *data,
457 const struct protocol *ctrl, const struct xprt_ops *xprt,
458 void *owner)
459{
460 conn_assign(conn, data, ctrl, xprt, owner);
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200461 conn->xprt_st = 0;
462 conn->xprt_ctx = NULL;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200463}
Willy Tarreau986a9d22012-08-30 21:11:38 +0200464
Willy Tarreau59f98392012-07-06 14:13:49 +0200465#endif /* _PROTO_CONNECTION_H */
466
467/*
468 * Local variables:
469 * c-indent-level: 8
470 * c-basic-offset: 8
471 * End:
472 */