blob: 748e14e9cb893d1c27c5e15f6c888a2a2413cb05 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * Connection management functions
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/compat.h>
14#include <common/config.h>
15
Willy Tarreauc5788912012-08-24 18:12:41 +020016#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <proto/fd.h>
Willy Tarreau2da156f2012-07-23 15:07:23 +020018#include <proto/proto_tcp.h>
Willy Tarreau2542b532012-08-31 16:01:23 +020019#include <proto/session.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020020#include <proto/stream_interface.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020021
22/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020023 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020024 */
25int conn_fd_handler(int fd)
26{
Willy Tarreau80184712012-07-06 14:54:49 +020027 struct connection *conn = fdtab[fd].owner;
Willy Tarreau59f98392012-07-06 14:13:49 +020028
Willy Tarreauc76ae332012-07-12 15:32:13 +020029 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020030 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020031
Willy Tarreaue9dfa792012-09-01 17:26:16 +020032 /* before engaging there, we clear the new WAIT_* flags so that we can
33 * more easily detect an EAGAIN condition from anywhere.
34 */
35 conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
36
Willy Tarreauc76ae332012-07-12 15:32:13 +020037 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020038 /* The handshake callbacks are called in sequence. If either of them is
39 * missing something, it must enable the required polling at the socket
40 * layer of the connection. Polling state is not guaranteed when entering
41 * these handlers, so any handshake handler which does not complete its
42 * work must explicitly disable events it's not interested in.
43 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020044 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020045 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020046 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020047
Willy Tarreau22cda212012-08-31 17:43:29 +020048 if (conn->flags & CO_FL_ACCEPT_PROXY)
49 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
50 goto leave;
51
Willy Tarreauc76ae332012-07-12 15:32:13 +020052 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020053 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020054 goto leave;
55 }
56
Willy Tarreauf9dabec2012-08-17 17:33:53 +020057 /* Once we're purely in the data phase, we disable handshake polling */
58 if (!(conn->flags & CO_FL_POLL_SOCK))
59 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020060
Willy Tarreau2542b532012-08-31 16:01:23 +020061 /* Maybe we need to finish initializing an incoming session. The
62 * function may fail and cause the connection to be destroyed, thus
63 * we must not use it anymore and should immediately leave instead.
64 */
65 if ((conn->flags & CO_FL_INIT_SESS) &&
Willy Tarreau22cda212012-08-31 17:43:29 +020066 conn_session_complete(conn, CO_FL_INIT_SESS) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020067 return 0;
68
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020069 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
70 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM)))
Willy Tarreauc5788912012-08-24 18:12:41 +020071 conn->app_cb->recv(conn);
Willy Tarreau59f98392012-07-06 14:13:49 +020072
Willy Tarreauc76ae332012-07-12 15:32:13 +020073 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020074 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020075
Willy Tarreauc76ae332012-07-12 15:32:13 +020076 /* It may happen during the data phase that a handshake is
77 * enabled again (eg: SSL)
78 */
79 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
80 goto process_handshake;
81
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020082 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
83 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA)))
Willy Tarreauc5788912012-08-24 18:12:41 +020084 conn->app_cb->send(conn);
Willy Tarreau2da156f2012-07-23 15:07:23 +020085
Willy Tarreauc76ae332012-07-12 15:32:13 +020086 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020087 goto leave;
88
Willy Tarreauc76ae332012-07-12 15:32:13 +020089 /* It may happen during the data phase that a handshake is
90 * enabled again (eg: SSL)
91 */
92 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
93 goto process_handshake;
94
Willy Tarreauf8deb0c2012-09-01 17:59:22 +020095 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
96 /* still waiting for a connection to establish and nothing was
97 * attempted yet to probe the connection. Then let's retry the
98 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +020099 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200100 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200101 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200102 }
103
Willy Tarreau2c6be842012-07-06 17:12:34 +0200104 leave:
Willy Tarreau2542b532012-08-31 16:01:23 +0200105 /* we may need to release the connection which is an embryonic session */
106 if ((conn->flags & (CO_FL_ERROR|CO_FL_INIT_SESS)) == (CO_FL_ERROR|CO_FL_INIT_SESS)) {
107 conn->flags |= CO_FL_ERROR;
108 conn_session_complete(conn, CO_FL_INIT_SESS);
109 return 0;
110 }
111
Willy Tarreaufd31e532012-07-23 18:24:25 +0200112 if (conn->flags & CO_FL_NOTIFY_SI)
Willy Tarreau100c4672012-08-20 12:06:26 +0200113 conn_notify_si(conn);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200114
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200115 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200116 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200117 conn->flags |= CO_FL_CONNECTED;
118
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200119 /* remove the events before leaving */
120 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200121
122 /* commit polling changes */
123 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200124 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200125}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200126
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200127/* Update polling on connection <c>'s file descriptor depending on its current
128 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
129 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
130 * The connection flags are updated with the new flags at the end of the
131 * operation.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200132 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200133void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200134{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200135 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200136
137 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200138 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
139 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
140 fd_stop_recv(c->t.sock.fd);
141 }
142 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
143 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
144 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200145 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200146 }
147 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
148 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200149 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200150 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200151
152 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200153 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
154 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
155 fd_stop_send(c->t.sock.fd);
156 }
157 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
158 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
159 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200160 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200161 }
162 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
163 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200165 }
166 c->flags = f;
167}
168
169/* Update polling on connection <c>'s file descriptor depending on its current
170 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
171 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
172 * The connection flags are updated with the new flags at the end of the
173 * operation.
174 */
175void conn_update_sock_polling(struct connection *c)
176{
177 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200178
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200179 /* update read status if needed */
180 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
181 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
182 fd_stop_recv(c->t.sock.fd);
183 }
184 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
185 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
186 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
187 fd_poll_recv(c->t.sock.fd);
188 }
189 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
190 f |= CO_FL_CURR_RD_ENA;
191 fd_want_recv(c->t.sock.fd);
192 }
193
194 /* update write status if needed */
195 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
196 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
197 fd_stop_send(c->t.sock.fd);
198 }
199 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
200 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
201 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
202 fd_poll_send(c->t.sock.fd);
203 }
204 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
205 f |= CO_FL_CURR_WR_ENA;
206 fd_want_send(c->t.sock.fd);
207 }
208 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200209}