blob: 8966bf70f69d8bdaead7bd9782b9b4a15b87fa16 [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 Tarreau59f98392012-07-06 14:13:49 +020032
Willy Tarreauc76ae332012-07-12 15:32:13 +020033 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020034 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020035
Willy Tarreaue9dfa792012-09-01 17:26:16 +020036 /* before engaging there, we clear the new WAIT_* flags so that we can
37 * more easily detect an EAGAIN condition from anywhere.
38 */
39 conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
40
Willy Tarreauc76ae332012-07-12 15:32:13 +020041 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020042 /* The handshake callbacks are called in sequence. If either of them is
43 * missing something, it must enable the required polling at the socket
44 * layer of the connection. Polling state is not guaranteed when entering
45 * these handlers, so any handshake handler which does not complete its
46 * work must explicitly disable events it's not interested in.
47 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020048 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020049 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020050 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020051
Willy Tarreau22cda212012-08-31 17:43:29 +020052 if (conn->flags & CO_FL_ACCEPT_PROXY)
53 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
54 goto leave;
55
Willy Tarreauc76ae332012-07-12 15:32:13 +020056 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020057 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020058 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020059#ifdef USE_OPENSSL
60 if (conn->flags & CO_FL_SSL_WAIT_HS)
61 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
62 goto leave;
63#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020064 }
65
Willy Tarreauf9dabec2012-08-17 17:33:53 +020066 /* Once we're purely in the data phase, we disable handshake polling */
67 if (!(conn->flags & CO_FL_POLL_SOCK))
68 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020069
Willy Tarreau2542b532012-08-31 16:01:23 +020070 /* Maybe we need to finish initializing an incoming session. The
71 * function may fail and cause the connection to be destroyed, thus
72 * we must not use it anymore and should immediately leave instead.
73 */
74 if ((conn->flags & CO_FL_INIT_SESS) &&
Willy Tarreau22cda212012-08-31 17:43:29 +020075 conn_session_complete(conn, CO_FL_INIT_SESS) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020076 return 0;
77
Willy Tarreau58363cf2012-09-06 14:12:03 +020078 /* The data transfer starts here and stops on error and handshakes */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020079 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau58363cf2012-09-06 14:12:03 +020080 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE)))
Willy Tarreauc5788912012-08-24 18:12:41 +020081 conn->app_cb->recv(conn);
Willy Tarreau59f98392012-07-06 14:13:49 +020082
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020083 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau58363cf2012-09-06 14:12:03 +020084 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE)))
Willy Tarreauc5788912012-08-24 18:12:41 +020085 conn->app_cb->send(conn);
Willy Tarreau2da156f2012-07-23 15:07:23 +020086
Willy Tarreauc76ae332012-07-12 15:32:13 +020087 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020088 goto leave;
89
Willy Tarreauc76ae332012-07-12 15:32:13 +020090 /* It may happen during the data phase that a handshake is
91 * enabled again (eg: SSL)
92 */
93 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
94 goto process_handshake;
95
Willy Tarreauf8deb0c2012-09-01 17:59:22 +020096 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
97 /* still waiting for a connection to establish and nothing was
98 * attempted yet to probe the connection. Then let's retry the
99 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200100 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200101 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200102 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200103 }
104
Willy Tarreau2c6be842012-07-06 17:12:34 +0200105 leave:
Willy Tarreau2542b532012-08-31 16:01:23 +0200106 /* we may need to release the connection which is an embryonic session */
107 if ((conn->flags & (CO_FL_ERROR|CO_FL_INIT_SESS)) == (CO_FL_ERROR|CO_FL_INIT_SESS)) {
108 conn->flags |= CO_FL_ERROR;
109 conn_session_complete(conn, CO_FL_INIT_SESS);
110 return 0;
111 }
112
Willy Tarreaufd31e532012-07-23 18:24:25 +0200113 if (conn->flags & CO_FL_NOTIFY_SI)
Willy Tarreau100c4672012-08-20 12:06:26 +0200114 conn_notify_si(conn);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200115
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200116 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200117 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200118 conn->flags |= CO_FL_CONNECTED;
119
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200120 /* remove the events before leaving */
121 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200122
123 /* commit polling changes */
124 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200125 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200126}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200127
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200128/* Update polling on connection <c>'s file descriptor depending on its current
129 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
130 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
131 * The connection flags are updated with the new flags at the end of the
132 * operation.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200133 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200134void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200135{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200136 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200137
138 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200139 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
140 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
141 fd_stop_recv(c->t.sock.fd);
142 }
143 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
144 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
145 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200146 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200147 }
148 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
149 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200150 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200151 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200152
153 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200154 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
155 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
156 fd_stop_send(c->t.sock.fd);
157 }
158 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
159 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
160 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200162 }
163 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
164 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200165 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200166 }
167 c->flags = f;
168}
169
170/* Update polling on connection <c>'s file descriptor depending on its current
171 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
172 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
173 * The connection flags are updated with the new flags at the end of the
174 * operation.
175 */
176void conn_update_sock_polling(struct connection *c)
177{
178 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200179
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200180 /* update read status if needed */
181 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
182 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
183 fd_stop_recv(c->t.sock.fd);
184 }
185 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
186 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
187 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
188 fd_poll_recv(c->t.sock.fd);
189 }
190 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
191 f |= CO_FL_CURR_RD_ENA;
192 fd_want_recv(c->t.sock.fd);
193 }
194
195 /* update write status if needed */
196 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
197 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
198 fd_stop_send(c->t.sock.fd);
199 }
200 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
201 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
202 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
203 fd_poll_send(c->t.sock.fd);
204 }
205 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
206 f |= CO_FL_CURR_WR_ENA;
207 fd_want_send(c->t.sock.fd);
208 }
209 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200210}