blob: 4c9e610c962c81111a5df89aed17c09df0bbf46f [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 Tarreau59f98392012-07-06 14:13:49 +020029
Willy Tarreauf2943dc2012-10-26 20:10:28 +020030extern struct pool_head *pool2_connection;
31
32/* perform minimal intializations, report 0 in case of error, 1 if OK. */
33int init_connection();
34
Willy Tarreau59f98392012-07-06 14:13:49 +020035/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020036 * provided by the connection's sock_ops. Returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020037 */
38int conn_fd_handler(int fd);
39
Willy Tarreau22cda212012-08-31 17:43:29 +020040/* receive a PROXY protocol header over a connection */
41int conn_recv_proxy(struct connection *conn, int flag);
Willy Tarreaue1e4a612012-10-05 00:10:55 +020042int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst);
Willy Tarreau22cda212012-08-31 17:43:29 +020043
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020044/* calls the init() function of the transport layer if any.
45 * Returns <0 in case of error.
Willy Tarreau15678ef2012-08-31 13:54:11 +020046 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020047static inline int conn_xprt_init(struct connection *conn)
Willy Tarreau15678ef2012-08-31 13:54:11 +020048{
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020049 if (conn->xprt && conn->xprt->init)
50 return conn->xprt->init(conn);
Willy Tarreau15678ef2012-08-31 13:54:11 +020051 return 0;
52}
53
Willy Tarreau6c03a642012-10-12 17:00:05 +020054/* Calls the close() function of the transport layer if any, and always unsets
Willy Tarreau1e954912012-10-12 17:50:05 +020055 * the transport layer. However this is not done if the CO_FL_XPRT_TRACKED flag
56 * is set, which allows logs to take data from the transport layer very late if
57 * needed.
Willy Tarreau6c03a642012-10-12 17:00:05 +020058 */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020059static inline void conn_xprt_close(struct connection *conn)
Willy Tarreau8b117082012-08-06 15:06:49 +020060{
Willy Tarreau1e954912012-10-12 17:50:05 +020061 if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) {
Willy Tarreau6c03a642012-10-12 17:00:05 +020062 if (conn->xprt->close)
63 conn->xprt->close(conn);
64 conn->xprt = NULL;
65 }
Willy Tarreau8b117082012-08-06 15:06:49 +020066}
67
Willy Tarreaue9dfa792012-09-01 17:26:16 +020068/* Update polling on connection <c>'s file descriptor depending on its current
69 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
70 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
71 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020072 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020073 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020074void conn_update_sock_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020075
Willy Tarreaue9dfa792012-09-01 17:26:16 +020076/* Update polling on connection <c>'s file descriptor depending on its current
77 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
78 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
79 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020080 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020081 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020082void conn_update_data_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020083
Willy Tarreau5f1504f2012-10-04 23:55:57 +020084/* This callback is used to send a valid PROXY protocol line to a socket being
85 * established from the local machine. It sets the protocol addresses to the
86 * local and remote address. This is typically used with health checks or when
87 * it is not possible to determine the other end's address. It returns 0 if it
88 * fails in a fatal way or needs to poll to go further, otherwise it returns
89 * non-zero and removes itself from the connection's flags (the bit is provided
90 * in <flag> by the caller). It is designed to be called by the connection
91 * handler and relies on it to commit polling changes. Note that this function
92 * expects to be able to send the whole line at once, which should always be
93 * possible since it is supposed to start at the first byte of the outgoing
94 * data segment.
95 */
96int conn_local_send_proxy(struct connection *conn, unsigned int flag);
97
Willy Tarreaue9dfa792012-09-01 17:26:16 +020098/* inspects c->flags and returns non-zero if DATA ENA changes from the CURR ENA
Willy Tarreau0ffde2c2012-10-04 22:21:15 +020099 * or if the WAIT flags set new flags that were not in CURR POL. Additionally,
100 * non-zero is also returned if an error was reported on the connection. This
101 * function is used quite often and is inlined. In order to proceed optimally
102 * with very little code and CPU cycles, the bits are arranged so that a change
103 * can be detected by a simple left shift, a xor, and a mask. This operation
104 * detects when POLL:DATA differs from WAIT:CURR. In order to detect the ERROR
105 * flag without additional work, we remove it from the copy of the original
106 * flags (unshifted) before doing the XOR. This operation is parallelized with
107 * the shift and does not induce additional cycles. This explains why we check
108 * the error bit shifted left in the mask. Last, the final operation is an AND
109 * which the compiler is able to replace with a TEST in boolean conditions. The
110 * result is that all these checks are done in 5-6 cycles only and less than 20
111 * bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200112 */
113static inline unsigned int conn_data_polling_changes(const struct connection *c)
114{
Willy Tarreauf3a6d7e2012-10-03 20:00:18 +0200115 unsigned int f = c->flags << 2;
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200116 return ((c->flags & ~(CO_FL_ERROR << 2)) ^ f) &
117 ((CO_FL_ERROR<<2)|CO_FL_WAIT_WR|CO_FL_CURR_WR_ENA|CO_FL_WAIT_RD|CO_FL_CURR_RD_ENA) &
Willy Tarreauf3a6d7e2012-10-03 20:00:18 +0200118 ~(f & (CO_FL_WAIT_WR|CO_FL_WAIT_RD));
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200119}
120
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200121/* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200122 * or if the WAIT flags set new flags that were not in CURR POL. Additionally,
123 * non-zero is also returned if an error was reported on the connection. This
124 * function is used quite often and is inlined. In order to proceed optimally
125 * with very little code and CPU cycles, the bits are arranged so that a change
126 * can be detected by a simple left shift, a xor, and a mask. This operation
127 * detects when CURR:POLL differs from SOCK:WAIT. In order to detect the ERROR
128 * flag without additional work, we remove it from the copy of the original
129 * flags (unshifted) before doing the XOR. This operation is parallelized with
130 * the shift and does not induce additional cycles. This explains why we check
131 * the error bit shifted left in the mask. Last, the final operation is an AND
132 * which the compiler is able to replace with a TEST in boolean conditions. The
133 * result is that all these checks are done in 5-6 cycles only and less than 20
134 * bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200135 */
136static inline unsigned int conn_sock_polling_changes(const struct connection *c)
137{
Willy Tarreauf3a6d7e2012-10-03 20:00:18 +0200138 unsigned int f = c->flags << 2;
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200139 return ((c->flags & ~(CO_FL_ERROR << 2)) ^ f) &
140 ((CO_FL_ERROR<<2)|CO_FL_WAIT_WR|CO_FL_SOCK_WR_ENA|CO_FL_WAIT_RD|CO_FL_SOCK_RD_ENA) &
Willy Tarreauf3a6d7e2012-10-03 20:00:18 +0200141 ~(f & (CO_FL_WAIT_WR|CO_FL_WAIT_RD));
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200142}
143
144/* Automatically updates polling on connection <c> depending on the DATA flags
145 * if no handshake is in progress.
146 */
147static inline void conn_cond_update_data_polling(struct connection *c)
148{
149 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
150 conn_update_data_polling(c);
151}
152
153/* Automatically updates polling on connection <c> depending on the SOCK flags
154 * if a handshake is in progress.
155 */
156static inline void conn_cond_update_sock_polling(struct connection *c)
157{
158 if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
159 conn_update_sock_polling(c);
160}
161
162/* Automatically update polling on connection <c> depending on the DATA and
163 * SOCK flags, and on whether a handshake is in progress or not. This may be
164 * called at any moment when there is a doubt about the effectiveness of the
165 * polling state, for instance when entering or leaving the handshake state.
166 */
167static inline void conn_cond_update_polling(struct connection *c)
168{
169 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
170 conn_update_data_polling(c);
171 else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
172 conn_update_sock_polling(c);
173}
174
175/***** Event manipulation primitives for use by DATA I/O callbacks *****/
176/* The __conn_* versions do not propagate to lower layers and are only meant
177 * to be used by handlers called by the connection handler. The other ones
178 * may be used anywhere.
179 */
180static inline void __conn_data_want_recv(struct connection *c)
181{
182 c->flags |= CO_FL_DATA_RD_ENA;
183}
184
185static inline void __conn_data_stop_recv(struct connection *c)
186{
187 c->flags &= ~CO_FL_DATA_RD_ENA;
188}
189
190static inline void __conn_data_poll_recv(struct connection *c)
191{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200192 c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200193}
194
195static inline void __conn_data_want_send(struct connection *c)
196{
197 c->flags |= CO_FL_DATA_WR_ENA;
198}
199
200static inline void __conn_data_stop_send(struct connection *c)
201{
202 c->flags &= ~CO_FL_DATA_WR_ENA;
203}
204
205static inline void __conn_data_poll_send(struct connection *c)
206{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200207 c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200208}
209
210static inline void __conn_data_stop_both(struct connection *c)
211{
212 c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA);
213}
214
215static inline void conn_data_want_recv(struct connection *c)
216{
217 __conn_data_want_recv(c);
218 conn_cond_update_data_polling(c);
219}
220
221static inline void conn_data_stop_recv(struct connection *c)
222{
223 __conn_data_stop_recv(c);
224 conn_cond_update_data_polling(c);
225}
226
227static inline void conn_data_poll_recv(struct connection *c)
228{
229 __conn_data_poll_recv(c);
230 conn_cond_update_data_polling(c);
231}
232
233static inline void conn_data_want_send(struct connection *c)
234{
235 __conn_data_want_send(c);
236 conn_cond_update_data_polling(c);
237}
238
239static inline void conn_data_stop_send(struct connection *c)
240{
241 __conn_data_stop_send(c);
242 conn_cond_update_data_polling(c);
243}
244
245static inline void conn_data_poll_send(struct connection *c)
246{
247 __conn_data_poll_send(c);
248 conn_cond_update_data_polling(c);
249}
250
251static inline void conn_data_stop_both(struct connection *c)
252{
253 __conn_data_stop_both(c);
254 conn_cond_update_data_polling(c);
255}
256
257/***** Event manipulation primitives for use by handshake I/O callbacks *****/
258/* The __conn_* versions do not propagate to lower layers and are only meant
259 * to be used by handlers called by the connection handler. The other ones
260 * may be used anywhere.
261 */
262static inline void __conn_sock_want_recv(struct connection *c)
263{
264 c->flags |= CO_FL_SOCK_RD_ENA;
265}
266
267static inline void __conn_sock_stop_recv(struct connection *c)
268{
269 c->flags &= ~CO_FL_SOCK_RD_ENA;
270}
271
272static inline void __conn_sock_poll_recv(struct connection *c)
273{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200274 c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200275}
276
277static inline void __conn_sock_want_send(struct connection *c)
278{
279 c->flags |= CO_FL_SOCK_WR_ENA;
280}
281
282static inline void __conn_sock_stop_send(struct connection *c)
283{
284 c->flags &= ~CO_FL_SOCK_WR_ENA;
285}
286
287static inline void __conn_sock_poll_send(struct connection *c)
288{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200289 c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200290}
291
292static inline void __conn_sock_stop_both(struct connection *c)
293{
294 c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA);
295}
296
297static inline void conn_sock_want_recv(struct connection *c)
298{
299 __conn_sock_want_recv(c);
300 conn_cond_update_sock_polling(c);
301}
302
303static inline void conn_sock_stop_recv(struct connection *c)
304{
305 __conn_sock_stop_recv(c);
306 conn_cond_update_sock_polling(c);
307}
308
309static inline void conn_sock_poll_recv(struct connection *c)
310{
311 __conn_sock_poll_recv(c);
312 conn_cond_update_sock_polling(c);
313}
314
315static inline void conn_sock_want_send(struct connection *c)
316{
317 __conn_sock_want_send(c);
318 conn_cond_update_sock_polling(c);
319}
320
321static inline void conn_sock_stop_send(struct connection *c)
322{
323 __conn_sock_stop_send(c);
324 conn_cond_update_sock_polling(c);
325}
326
327static inline void conn_sock_poll_send(struct connection *c)
328{
329 __conn_sock_poll_send(c);
330 conn_cond_update_sock_polling(c);
331}
332
333static inline void conn_sock_stop_both(struct connection *c)
334{
335 __conn_sock_stop_both(c);
336 conn_cond_update_sock_polling(c);
337}
Willy Tarreau8b117082012-08-06 15:06:49 +0200338
Willy Tarreau3af56a92012-08-20 16:55:48 +0200339/* shutdown management */
340static inline void conn_sock_read0(struct connection *c)
341{
342 c->flags |= CO_FL_SOCK_RD_SH;
343 __conn_sock_stop_recv(c);
344}
345
346static inline void conn_data_read0(struct connection *c)
347{
348 c->flags |= CO_FL_DATA_RD_SH;
349 __conn_data_stop_recv(c);
350}
351
352static inline void conn_sock_shutw(struct connection *c)
353{
354 c->flags |= CO_FL_SOCK_WR_SH;
355 __conn_sock_stop_send(c);
356}
357
358static inline void conn_data_shutw(struct connection *c)
359{
360 c->flags |= CO_FL_DATA_WR_SH;
361 __conn_data_stop_send(c);
362}
363
364/* detect sock->data read0 transition */
365static inline int conn_data_read0_pending(struct connection *c)
366{
367 return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH;
368}
369
370/* detect data->sock shutw transition */
371static inline int conn_sock_shutw_pending(struct connection *c)
372{
373 return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH;
374}
375
Willy Tarreau3cefd522012-08-30 15:49:18 +0200376static inline void clear_target(struct target *dest)
377{
378 dest->type = TARG_TYPE_NONE;
379 dest->ptr.v = NULL;
380}
381
382static inline void set_target_client(struct target *dest, struct listener *l)
383{
384 dest->type = TARG_TYPE_CLIENT;
385 dest->ptr.l = l;
386}
387
388static inline void set_target_server(struct target *dest, struct server *s)
389{
390 dest->type = TARG_TYPE_SERVER;
391 dest->ptr.s = s;
392}
393
394static inline void set_target_proxy(struct target *dest, struct proxy *p)
395{
396 dest->type = TARG_TYPE_PROXY;
397 dest->ptr.p = p;
398}
399
400static inline void set_target_applet(struct target *dest, struct si_applet *a)
401{
402 dest->type = TARG_TYPE_APPLET;
403 dest->ptr.a = a;
404}
405
406static inline void set_target_task(struct target *dest, struct task *t)
407{
408 dest->type = TARG_TYPE_TASK;
409 dest->ptr.t = t;
410}
411
412static inline struct target *copy_target(struct target *dest, struct target *src)
413{
414 *dest = *src;
415 return dest;
416}
417
418static inline int target_match(struct target *a, struct target *b)
419{
420 return a->type == b->type && a->ptr.v == b->ptr.v;
421}
422
423static inline struct server *target_srv(struct target *t)
424{
425 if (!t || t->type != TARG_TYPE_SERVER)
426 return NULL;
427 return t->ptr.s;
428}
429
430static inline struct listener *target_client(struct target *t)
431{
432 if (!t || t->type != TARG_TYPE_CLIENT)
433 return NULL;
434 return t->ptr.l;
435}
436
Willy Tarreau986a9d22012-08-30 21:11:38 +0200437/* Retrieves the connection's source address */
438static inline void conn_get_from_addr(struct connection *conn)
439{
440 if (conn->flags & CO_FL_ADDR_FROM_SET)
441 return;
442
443 if (!conn->ctrl || !conn->ctrl->get_src)
444 return;
445
446 if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from,
447 sizeof(conn->addr.from),
448 conn->target.type != TARG_TYPE_CLIENT) == -1)
449 return;
450 conn->flags |= CO_FL_ADDR_FROM_SET;
451}
452
453/* Retrieves the connection's original destination address */
454static inline void conn_get_to_addr(struct connection *conn)
455{
456 if (conn->flags & CO_FL_ADDR_TO_SET)
457 return;
458
459 if (!conn->ctrl || !conn->ctrl->get_dst)
460 return;
461
462 if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to,
463 sizeof(conn->addr.to),
464 conn->target.type != TARG_TYPE_CLIENT) == -1)
465 return;
466 conn->flags |= CO_FL_ADDR_TO_SET;
467}
468
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200469/* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */
470static inline void conn_assign(struct connection *conn, const struct data_cb *data,
471 const struct protocol *ctrl, const struct xprt_ops *xprt,
472 void *owner)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200473{
Willy Tarreau74beec32012-10-03 00:41:04 +0200474 conn->data = data;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200475 conn->ctrl = ctrl;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200476 conn->xprt = xprt;
Willy Tarreaucd379952012-09-27 22:14:33 +0200477 conn->owner = owner;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200478}
479
480/* prepares a connection with the appropriate data, ctrl, transport layers, and
481 * owner. The transport state and context are set to 0.
482 */
483static inline void conn_prepare(struct connection *conn, const struct data_cb *data,
484 const struct protocol *ctrl, const struct xprt_ops *xprt,
485 void *owner)
486{
487 conn_assign(conn, data, ctrl, xprt, owner);
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200488 conn->xprt_st = 0;
489 conn->xprt_ctx = NULL;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200490}
Willy Tarreau986a9d22012-08-30 21:11:38 +0200491
Willy Tarreau59f98392012-07-06 14:13:49 +0200492#endif /* _PROTO_CONNECTION_H */
493
494/*
495 * Local variables:
496 * c-indent-level: 8
497 * c-basic-offset: 8
498 * End:
499 */