blob: 90283fc5e320a3c3663e6da1f4a2801fc51b0e00 [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 Tarreaud9de7ca2012-09-02 18:48:46 +020078 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
79 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM)))
Willy Tarreauc5788912012-08-24 18:12:41 +020080 conn->app_cb->recv(conn);
Willy Tarreau59f98392012-07-06 14:13:49 +020081
Willy Tarreauc76ae332012-07-12 15:32:13 +020082 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020083 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020084
Willy Tarreauc76ae332012-07-12 15:32:13 +020085 /* It may happen during the data phase that a handshake is
86 * enabled again (eg: SSL)
87 */
88 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
89 goto process_handshake;
90
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020091 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
92 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA)))
Willy Tarreauc5788912012-08-24 18:12:41 +020093 conn->app_cb->send(conn);
Willy Tarreau2da156f2012-07-23 15:07:23 +020094
Willy Tarreauc76ae332012-07-12 15:32:13 +020095 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020096 goto leave;
97
Willy Tarreauc76ae332012-07-12 15:32:13 +020098 /* It may happen during the data phase that a handshake is
99 * enabled again (eg: SSL)
100 */
101 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
102 goto process_handshake;
103
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200104 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
105 /* still waiting for a connection to establish and nothing was
106 * attempted yet to probe the connection. Then let's retry the
107 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200108 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200109 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200110 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200111 }
112
Willy Tarreau2c6be842012-07-06 17:12:34 +0200113 leave:
Willy Tarreau2542b532012-08-31 16:01:23 +0200114 /* we may need to release the connection which is an embryonic session */
115 if ((conn->flags & (CO_FL_ERROR|CO_FL_INIT_SESS)) == (CO_FL_ERROR|CO_FL_INIT_SESS)) {
116 conn->flags |= CO_FL_ERROR;
117 conn_session_complete(conn, CO_FL_INIT_SESS);
118 return 0;
119 }
120
Willy Tarreaufd31e532012-07-23 18:24:25 +0200121 if (conn->flags & CO_FL_NOTIFY_SI)
Willy Tarreau100c4672012-08-20 12:06:26 +0200122 conn_notify_si(conn);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200123
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200124 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200125 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200126 conn->flags |= CO_FL_CONNECTED;
127
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200128 /* remove the events before leaving */
129 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200130
131 /* commit polling changes */
132 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200133 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200134}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200135
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200136/* Update polling on connection <c>'s file descriptor depending on its current
137 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
138 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
139 * The connection flags are updated with the new flags at the end of the
140 * operation.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200141 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200142void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200143{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200144 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200145
146 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200147 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
148 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
149 fd_stop_recv(c->t.sock.fd);
150 }
151 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
152 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
153 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200154 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200155 }
156 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
157 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200158 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200159 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200160
161 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200162 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
163 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
164 fd_stop_send(c->t.sock.fd);
165 }
166 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
167 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
168 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200169 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200170 }
171 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
172 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200173 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200174 }
175 c->flags = f;
176}
177
178/* Update polling on connection <c>'s file descriptor depending on its current
179 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
180 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
181 * The connection flags are updated with the new flags at the end of the
182 * operation.
183 */
184void conn_update_sock_polling(struct connection *c)
185{
186 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200187
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200188 /* update read status if needed */
189 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
190 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
191 fd_stop_recv(c->t.sock.fd);
192 }
193 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
194 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
195 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
196 fd_poll_recv(c->t.sock.fd);
197 }
198 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
199 f |= CO_FL_CURR_RD_ENA;
200 fd_want_recv(c->t.sock.fd);
201 }
202
203 /* update write status if needed */
204 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
205 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
206 fd_stop_send(c->t.sock.fd);
207 }
208 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
209 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
210 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
211 fd_poll_send(c->t.sock.fd);
212 }
213 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
214 f |= CO_FL_CURR_WR_ENA;
215 fd_want_send(c->t.sock.fd);
216 }
217 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200218}