blob: 6cb124f3f8dbc7910ab031f8745755e1b1348331 [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
Emeric Brun46591952012-05-18 15:47:34 +020022#ifdef USE_OPENSSL
23#include <proto/ssl_sock.h>
24#endif
25
Willy Tarreau59f98392012-07-06 14:13:49 +020026/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020027 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020028 */
29int conn_fd_handler(int fd)
30{
Willy Tarreau80184712012-07-06 14:54:49 +020031 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020032 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020033
Willy Tarreauc76ae332012-07-12 15:32:13 +020034 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020035 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020036
Willy Tarreaue9dfa792012-09-01 17:26:16 +020037 /* before engaging there, we clear the new WAIT_* flags so that we can
38 * more easily detect an EAGAIN condition from anywhere.
39 */
Willy Tarreau9e272bf2012-10-03 21:04:48 +020040 flags = conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +020041
Willy Tarreauc76ae332012-07-12 15:32:13 +020042 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020043 /* The handshake callbacks are called in sequence. If either of them is
44 * missing something, it must enable the required polling at the socket
45 * layer of the connection. Polling state is not guaranteed when entering
46 * these handlers, so any handshake handler which does not complete its
47 * work must explicitly disable events it's not interested in.
48 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020049 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020050 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020051 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020052
Willy Tarreau22cda212012-08-31 17:43:29 +020053 if (conn->flags & CO_FL_ACCEPT_PROXY)
54 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
55 goto leave;
56
Willy Tarreauc76ae332012-07-12 15:32:13 +020057 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020058 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020059 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020060#ifdef USE_OPENSSL
61 if (conn->flags & CO_FL_SSL_WAIT_HS)
62 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
63 goto leave;
64#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020065 }
66
Willy Tarreauf9dabec2012-08-17 17:33:53 +020067 /* Once we're purely in the data phase, we disable handshake polling */
68 if (!(conn->flags & CO_FL_POLL_SOCK))
69 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020070
Willy Tarreau071e1372012-10-03 01:39:48 +020071 /* The data layer might not be ready yet (eg: when using embryonic
72 * sessions). If we're about to move data, we must initialize it first.
73 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020074 * we must not use it anymore and should immediately leave instead.
75 */
Willy Tarreau071e1372012-10-03 01:39:48 +020076 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020077 return 0;
78
Willy Tarreau58363cf2012-09-06 14:12:03 +020079 /* The data transfer starts here and stops on error and handshakes */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020080 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020081 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
82 /* force detection of a flag change : if any I/O succeeds, we're
83 * forced to have at least one of the CONN_* flags in conn->flags.
84 */
85 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +020086 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020087 }
Willy Tarreau59f98392012-07-06 14:13:49 +020088
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020089 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020090 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
91 /* force detection of a flag change : if any I/O succeeds, we're
92 * forced to have at least one of the CONN_* flags in conn->flags.
93 */
94 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +020095 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020096 }
Willy Tarreau2da156f2012-07-23 15:07:23 +020097
Willy Tarreauc76ae332012-07-12 15:32:13 +020098 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020099 goto leave;
100
Willy Tarreauc76ae332012-07-12 15:32:13 +0200101 /* It may happen during the data phase that a handshake is
102 * enabled again (eg: SSL)
103 */
104 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
105 goto process_handshake;
106
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200107 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
108 /* still waiting for a connection to establish and nothing was
109 * attempted yet to probe the connection. Then let's retry the
110 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200111 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200112 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200113 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200114 }
115
Willy Tarreau2c6be842012-07-06 17:12:34 +0200116 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200117 /* The wake callback may be used to process a critical error and abort the
118 * connection. If so, we don't want to go further as the connection will
119 * have been released and the FD destroyed.
120 */
121 if ((conn->flags & CO_FL_WAKE_DATA) &&
122 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
123 conn->data->wake(conn) < 0)
124 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200125
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200126 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200127 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200128 conn->flags |= CO_FL_CONNECTED;
129
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200130 /* remove the events before leaving */
131 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200132
133 /* commit polling changes */
134 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200135 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200136}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200137
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200138/* Update polling on connection <c>'s file descriptor depending on its current
139 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
140 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
141 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200142 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200143 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200144void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200145{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200146 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200147
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200148 if (unlikely(f & CO_FL_ERROR)) {
149 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
150 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
151 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
152 fd_stop_both(c->t.sock.fd);
153 return;
154 }
155
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200156 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200157 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
158 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
159 fd_stop_recv(c->t.sock.fd);
160 }
161 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
162 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
163 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200164 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200165 }
166 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
167 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200168 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200169 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200170
171 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200172 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
173 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
174 fd_stop_send(c->t.sock.fd);
175 }
176 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
177 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
178 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200179 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200180 }
181 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
182 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200183 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200184 }
185 c->flags = f;
186}
187
188/* Update polling on connection <c>'s file descriptor depending on its current
189 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
190 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
191 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200192 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200193 */
194void conn_update_sock_polling(struct connection *c)
195{
196 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200197
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200198 if (unlikely(f & CO_FL_ERROR)) {
199 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
200 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
201 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
202 fd_stop_both(c->t.sock.fd);
203 return;
204 }
205
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200206 /* update read status if needed */
207 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
208 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
209 fd_stop_recv(c->t.sock.fd);
210 }
211 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
212 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
213 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
214 fd_poll_recv(c->t.sock.fd);
215 }
216 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
217 f |= CO_FL_CURR_RD_ENA;
218 fd_want_recv(c->t.sock.fd);
219 }
220
221 /* update write status if needed */
222 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
223 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
224 fd_stop_send(c->t.sock.fd);
225 }
226 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
227 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
228 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
229 fd_poll_send(c->t.sock.fd);
230 }
231 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
232 f |= CO_FL_CURR_WR_ENA;
233 fd_want_send(c->t.sock.fd);
234 }
235 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200236}