blob: 0236d95e495db79a86014baef63b217e8848285f [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 Tarreauc8dd77f2012-11-05 17:52:26 +010099 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200100 * 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
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100103 * can be detected by a few left shifts, a xor, and a mask. These operations
104 * detect when W&D are both enabled for either direction, when C&D differ for
105 * either direction and when Error is set. The trick consists in first keeping
106 * only the bits we're interested in, since they don't collide when shifted,
107 * and to perform the AND at the end. In practice, the compiler is able to
108 * replace the last AND with a TEST in boolean conditions. This results in
109 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200110 */
111static inline unsigned int conn_data_polling_changes(const struct connection *c)
112{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100113 unsigned int f = c->flags;
114 f &= CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA | CO_FL_CURR_WR_ENA |
115 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
116
117 f = (f & (f << 2)) | /* test W & D */
118 ((f ^ (f << 1)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ D */
119 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 +0200120}
121
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200122/* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100123 * or if the WAIT flags are set with their respective ENA flags. Additionally,
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200124 * non-zero is also returned if an error was reported on the connection. This
125 * function is used quite often and is inlined. In order to proceed optimally
126 * with very little code and CPU cycles, the bits are arranged so that a change
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100127 * can be detected by a few left shifts, a xor, and a mask. These operations
128 * detect when W&S are both enabled for either direction, when C&S differ for
129 * either direction and when Error is set. The trick consists in first keeping
130 * only the bits we're interested in, since they don't collide when shifted,
131 * and to perform the AND at the end. In practice, the compiler is able to
132 * replace the last AND with a TEST in boolean conditions. This results in
133 * checks that are done in 4-6 cycles and less than 30 bytes.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200134 */
135static inline unsigned int conn_sock_polling_changes(const struct connection *c)
136{
Willy Tarreauc8dd77f2012-11-05 17:52:26 +0100137 unsigned int f = c->flags;
138 f &= CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA | CO_FL_CURR_WR_ENA |
139 CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD;
140
141 f = (f & (f << 3)) | /* test W & S */
142 ((f ^ (f << 2)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ S */
143 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 +0200144}
145
146/* Automatically updates polling on connection <c> depending on the DATA flags
147 * if no handshake is in progress.
148 */
149static inline void conn_cond_update_data_polling(struct connection *c)
150{
151 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
152 conn_update_data_polling(c);
153}
154
155/* Automatically updates polling on connection <c> depending on the SOCK flags
156 * if a handshake is in progress.
157 */
158static inline void conn_cond_update_sock_polling(struct connection *c)
159{
160 if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
161 conn_update_sock_polling(c);
162}
163
164/* Automatically update polling on connection <c> depending on the DATA and
165 * SOCK flags, and on whether a handshake is in progress or not. This may be
166 * called at any moment when there is a doubt about the effectiveness of the
167 * polling state, for instance when entering or leaving the handshake state.
168 */
169static inline void conn_cond_update_polling(struct connection *c)
170{
171 if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c))
172 conn_update_data_polling(c);
173 else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c))
174 conn_update_sock_polling(c);
175}
176
177/***** Event manipulation primitives for use by DATA I/O callbacks *****/
178/* The __conn_* versions do not propagate to lower layers and are only meant
179 * to be used by handlers called by the connection handler. The other ones
180 * may be used anywhere.
181 */
182static inline void __conn_data_want_recv(struct connection *c)
183{
184 c->flags |= CO_FL_DATA_RD_ENA;
185}
186
187static inline void __conn_data_stop_recv(struct connection *c)
188{
189 c->flags &= ~CO_FL_DATA_RD_ENA;
190}
191
192static inline void __conn_data_poll_recv(struct connection *c)
193{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200194 c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200195}
196
197static inline void __conn_data_want_send(struct connection *c)
198{
199 c->flags |= CO_FL_DATA_WR_ENA;
200}
201
202static inline void __conn_data_stop_send(struct connection *c)
203{
204 c->flags &= ~CO_FL_DATA_WR_ENA;
205}
206
207static inline void __conn_data_poll_send(struct connection *c)
208{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200209 c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200210}
211
212static inline void __conn_data_stop_both(struct connection *c)
213{
214 c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA);
215}
216
217static inline void conn_data_want_recv(struct connection *c)
218{
219 __conn_data_want_recv(c);
220 conn_cond_update_data_polling(c);
221}
222
223static inline void conn_data_stop_recv(struct connection *c)
224{
225 __conn_data_stop_recv(c);
226 conn_cond_update_data_polling(c);
227}
228
229static inline void conn_data_poll_recv(struct connection *c)
230{
231 __conn_data_poll_recv(c);
232 conn_cond_update_data_polling(c);
233}
234
235static inline void conn_data_want_send(struct connection *c)
236{
237 __conn_data_want_send(c);
238 conn_cond_update_data_polling(c);
239}
240
241static inline void conn_data_stop_send(struct connection *c)
242{
243 __conn_data_stop_send(c);
244 conn_cond_update_data_polling(c);
245}
246
247static inline void conn_data_poll_send(struct connection *c)
248{
249 __conn_data_poll_send(c);
250 conn_cond_update_data_polling(c);
251}
252
253static inline void conn_data_stop_both(struct connection *c)
254{
255 __conn_data_stop_both(c);
256 conn_cond_update_data_polling(c);
257}
258
259/***** Event manipulation primitives for use by handshake I/O callbacks *****/
260/* The __conn_* versions do not propagate to lower layers and are only meant
261 * to be used by handlers called by the connection handler. The other ones
262 * may be used anywhere.
263 */
264static inline void __conn_sock_want_recv(struct connection *c)
265{
266 c->flags |= CO_FL_SOCK_RD_ENA;
267}
268
269static inline void __conn_sock_stop_recv(struct connection *c)
270{
271 c->flags &= ~CO_FL_SOCK_RD_ENA;
272}
273
274static inline void __conn_sock_poll_recv(struct connection *c)
275{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200276 c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200277}
278
279static inline void __conn_sock_want_send(struct connection *c)
280{
281 c->flags |= CO_FL_SOCK_WR_ENA;
282}
283
284static inline void __conn_sock_stop_send(struct connection *c)
285{
286 c->flags &= ~CO_FL_SOCK_WR_ENA;
287}
288
289static inline void __conn_sock_poll_send(struct connection *c)
290{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200291 c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200292}
293
294static inline void __conn_sock_stop_both(struct connection *c)
295{
296 c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA);
297}
298
299static inline void conn_sock_want_recv(struct connection *c)
300{
301 __conn_sock_want_recv(c);
302 conn_cond_update_sock_polling(c);
303}
304
305static inline void conn_sock_stop_recv(struct connection *c)
306{
307 __conn_sock_stop_recv(c);
308 conn_cond_update_sock_polling(c);
309}
310
311static inline void conn_sock_poll_recv(struct connection *c)
312{
313 __conn_sock_poll_recv(c);
314 conn_cond_update_sock_polling(c);
315}
316
317static inline void conn_sock_want_send(struct connection *c)
318{
319 __conn_sock_want_send(c);
320 conn_cond_update_sock_polling(c);
321}
322
323static inline void conn_sock_stop_send(struct connection *c)
324{
325 __conn_sock_stop_send(c);
326 conn_cond_update_sock_polling(c);
327}
328
329static inline void conn_sock_poll_send(struct connection *c)
330{
331 __conn_sock_poll_send(c);
332 conn_cond_update_sock_polling(c);
333}
334
335static inline void conn_sock_stop_both(struct connection *c)
336{
337 __conn_sock_stop_both(c);
338 conn_cond_update_sock_polling(c);
339}
Willy Tarreau8b117082012-08-06 15:06:49 +0200340
Willy Tarreau3af56a92012-08-20 16:55:48 +0200341/* shutdown management */
342static inline void conn_sock_read0(struct connection *c)
343{
344 c->flags |= CO_FL_SOCK_RD_SH;
345 __conn_sock_stop_recv(c);
346}
347
348static inline void conn_data_read0(struct connection *c)
349{
350 c->flags |= CO_FL_DATA_RD_SH;
351 __conn_data_stop_recv(c);
352}
353
354static inline void conn_sock_shutw(struct connection *c)
355{
356 c->flags |= CO_FL_SOCK_WR_SH;
357 __conn_sock_stop_send(c);
358}
359
360static inline void conn_data_shutw(struct connection *c)
361{
362 c->flags |= CO_FL_DATA_WR_SH;
363 __conn_data_stop_send(c);
364}
365
366/* detect sock->data read0 transition */
367static inline int conn_data_read0_pending(struct connection *c)
368{
369 return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH;
370}
371
372/* detect data->sock shutw transition */
373static inline int conn_sock_shutw_pending(struct connection *c)
374{
375 return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH;
376}
377
Willy Tarreau3cefd522012-08-30 15:49:18 +0200378static inline void clear_target(struct target *dest)
379{
380 dest->type = TARG_TYPE_NONE;
381 dest->ptr.v = NULL;
382}
383
384static inline void set_target_client(struct target *dest, struct listener *l)
385{
386 dest->type = TARG_TYPE_CLIENT;
387 dest->ptr.l = l;
388}
389
390static inline void set_target_server(struct target *dest, struct server *s)
391{
392 dest->type = TARG_TYPE_SERVER;
393 dest->ptr.s = s;
394}
395
396static inline void set_target_proxy(struct target *dest, struct proxy *p)
397{
398 dest->type = TARG_TYPE_PROXY;
399 dest->ptr.p = p;
400}
401
402static inline void set_target_applet(struct target *dest, struct si_applet *a)
403{
404 dest->type = TARG_TYPE_APPLET;
405 dest->ptr.a = a;
406}
407
408static inline void set_target_task(struct target *dest, struct task *t)
409{
410 dest->type = TARG_TYPE_TASK;
411 dest->ptr.t = t;
412}
413
414static inline struct target *copy_target(struct target *dest, struct target *src)
415{
416 *dest = *src;
417 return dest;
418}
419
420static inline int target_match(struct target *a, struct target *b)
421{
422 return a->type == b->type && a->ptr.v == b->ptr.v;
423}
424
425static inline struct server *target_srv(struct target *t)
426{
427 if (!t || t->type != TARG_TYPE_SERVER)
428 return NULL;
429 return t->ptr.s;
430}
431
432static inline struct listener *target_client(struct target *t)
433{
434 if (!t || t->type != TARG_TYPE_CLIENT)
435 return NULL;
436 return t->ptr.l;
437}
438
Willy Tarreau986a9d22012-08-30 21:11:38 +0200439/* Retrieves the connection's source address */
440static inline void conn_get_from_addr(struct connection *conn)
441{
442 if (conn->flags & CO_FL_ADDR_FROM_SET)
443 return;
444
445 if (!conn->ctrl || !conn->ctrl->get_src)
446 return;
447
448 if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from,
449 sizeof(conn->addr.from),
450 conn->target.type != TARG_TYPE_CLIENT) == -1)
451 return;
452 conn->flags |= CO_FL_ADDR_FROM_SET;
453}
454
455/* Retrieves the connection's original destination address */
456static inline void conn_get_to_addr(struct connection *conn)
457{
458 if (conn->flags & CO_FL_ADDR_TO_SET)
459 return;
460
461 if (!conn->ctrl || !conn->ctrl->get_dst)
462 return;
463
464 if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to,
465 sizeof(conn->addr.to),
466 conn->target.type != TARG_TYPE_CLIENT) == -1)
467 return;
468 conn->flags |= CO_FL_ADDR_TO_SET;
469}
470
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200471/* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */
472static inline void conn_assign(struct connection *conn, const struct data_cb *data,
473 const struct protocol *ctrl, const struct xprt_ops *xprt,
474 void *owner)
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200475{
Willy Tarreau74beec32012-10-03 00:41:04 +0200476 conn->data = data;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200477 conn->ctrl = ctrl;
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200478 conn->xprt = xprt;
Willy Tarreaucd379952012-09-27 22:14:33 +0200479 conn->owner = owner;
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200480}
481
482/* prepares a connection with the appropriate data, ctrl, transport layers, and
483 * owner. The transport state and context are set to 0.
484 */
485static inline void conn_prepare(struct connection *conn, const struct data_cb *data,
486 const struct protocol *ctrl, const struct xprt_ops *xprt,
487 void *owner)
488{
489 conn_assign(conn, data, ctrl, xprt, owner);
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200490 conn->xprt_st = 0;
491 conn->xprt_ctx = NULL;
Willy Tarreaudda5e7c2012-09-24 17:15:42 +0200492}
Willy Tarreau986a9d22012-08-30 21:11:38 +0200493
Willy Tarreau59f98392012-07-06 14:13:49 +0200494#endif /* _PROTO_CONNECTION_H */
495
496/*
497 * Local variables:
498 * c-indent-level: 8
499 * c-basic-offset: 8
500 * End:
501 */