blob: 15b94856ffa48c6c4f277cb03872be0eb615549b [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 Tarreau2da156f2012-07-23 15:07:23 +020017#include <proto/proto_tcp.h>
Willy Tarreau2542b532012-08-31 16:01:23 +020018#include <proto/session.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020019#include <proto/stream_interface.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020020
21/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020022 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020023 */
24int conn_fd_handler(int fd)
25{
Willy Tarreau80184712012-07-06 14:54:49 +020026 struct connection *conn = fdtab[fd].owner;
Willy Tarreau59f98392012-07-06 14:13:49 +020027
Willy Tarreauc76ae332012-07-12 15:32:13 +020028 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020029 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020030
Willy Tarreaue9dfa792012-09-01 17:26:16 +020031 /* before engaging there, we clear the new WAIT_* flags so that we can
32 * more easily detect an EAGAIN condition from anywhere.
33 */
34 conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
35
Willy Tarreauc76ae332012-07-12 15:32:13 +020036 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020037 /* The handshake callbacks are called in sequence. If either of them is
38 * missing something, it must enable the required polling at the socket
39 * layer of the connection. Polling state is not guaranteed when entering
40 * these handlers, so any handshake handler which does not complete its
41 * work must explicitly disable events it's not interested in.
42 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020043 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
44 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020045 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020046
Willy Tarreau22cda212012-08-31 17:43:29 +020047 if (conn->flags & CO_FL_ACCEPT_PROXY)
48 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
49 goto leave;
50
Willy Tarreauc76ae332012-07-12 15:32:13 +020051 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020052 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020053 goto leave;
54 }
55
Willy Tarreauf9dabec2012-08-17 17:33:53 +020056 /* Once we're purely in the data phase, we disable handshake polling */
57 if (!(conn->flags & CO_FL_POLL_SOCK))
58 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020059
Willy Tarreau2542b532012-08-31 16:01:23 +020060 /* Maybe we need to finish initializing an incoming session. The
61 * function may fail and cause the connection to be destroyed, thus
62 * we must not use it anymore and should immediately leave instead.
63 */
64 if ((conn->flags & CO_FL_INIT_SESS) &&
Willy Tarreau22cda212012-08-31 17:43:29 +020065 conn_session_complete(conn, CO_FL_INIT_SESS) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020066 return 0;
67
Willy Tarreau59f98392012-07-06 14:13:49 +020068 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
Willy Tarreauc5788912012-08-24 18:12:41 +020069 conn->app_cb->recv(conn);
Willy Tarreau59f98392012-07-06 14:13:49 +020070
Willy Tarreauc76ae332012-07-12 15:32:13 +020071 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020072 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020073
Willy Tarreauc76ae332012-07-12 15:32:13 +020074 /* It may happen during the data phase that a handshake is
75 * enabled again (eg: SSL)
76 */
77 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
78 goto process_handshake;
79
Willy Tarreau59f98392012-07-06 14:13:49 +020080 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
Willy Tarreauc5788912012-08-24 18:12:41 +020081 conn->app_cb->send(conn);
Willy Tarreau2da156f2012-07-23 15:07:23 +020082
Willy Tarreauc76ae332012-07-12 15:32:13 +020083 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020084 goto leave;
85
Willy Tarreauc76ae332012-07-12 15:32:13 +020086 /* It may happen during the data phase that a handshake is
87 * enabled again (eg: SSL)
88 */
89 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
90 goto process_handshake;
91
Willy Tarreauf8deb0c2012-09-01 17:59:22 +020092 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
93 /* still waiting for a connection to establish and nothing was
94 * attempted yet to probe the connection. Then let's retry the
95 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +020096 */
Willy Tarreau239d7182012-07-23 18:53:03 +020097 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +020098 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +020099 }
100
Willy Tarreau2c6be842012-07-06 17:12:34 +0200101 leave:
Willy Tarreau2542b532012-08-31 16:01:23 +0200102 /* we may need to release the connection which is an embryonic session */
103 if ((conn->flags & (CO_FL_ERROR|CO_FL_INIT_SESS)) == (CO_FL_ERROR|CO_FL_INIT_SESS)) {
104 conn->flags |= CO_FL_ERROR;
105 conn_session_complete(conn, CO_FL_INIT_SESS);
106 return 0;
107 }
108
Willy Tarreaufd31e532012-07-23 18:24:25 +0200109 if (conn->flags & CO_FL_NOTIFY_SI)
Willy Tarreau100c4672012-08-20 12:06:26 +0200110 conn_notify_si(conn);
Willy Tarreaufd31e532012-07-23 18:24:25 +0200111
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200112 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200113 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200114 conn->flags |= CO_FL_CONNECTED;
115
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200116 /* remove the events before leaving */
117 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200118
119 /* commit polling changes */
120 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200121 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200122}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200123
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200124/* Update polling on connection <c>'s file descriptor depending on its current
125 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
126 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
127 * The connection flags are updated with the new flags at the end of the
128 * operation.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200129 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200130void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200131{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200132 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200133
134 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200135 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
136 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
137 fd_stop_recv(c->t.sock.fd);
138 }
139 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
140 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
141 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200142 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200143 }
144 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
145 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200146 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200147 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200148
149 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200150 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
151 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
152 fd_stop_send(c->t.sock.fd);
153 }
154 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
155 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
156 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200157 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200158 }
159 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
160 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200161 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200162 }
163 c->flags = f;
164}
165
166/* Update polling on connection <c>'s file descriptor depending on its current
167 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
168 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
169 * The connection flags are updated with the new flags at the end of the
170 * operation.
171 */
172void conn_update_sock_polling(struct connection *c)
173{
174 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200175
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200176 /* update read status if needed */
177 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
178 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
179 fd_stop_recv(c->t.sock.fd);
180 }
181 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
182 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
183 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
184 fd_poll_recv(c->t.sock.fd);
185 }
186 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
187 f |= CO_FL_CURR_RD_ENA;
188 fd_want_recv(c->t.sock.fd);
189 }
190
191 /* update write status if needed */
192 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
193 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
194 fd_stop_send(c->t.sock.fd);
195 }
196 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
197 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
198 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
199 fd_poll_send(c->t.sock.fd);
200 }
201 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
202 f |= CO_FL_CURR_WR_ENA;
203 fd_want_send(c->t.sock.fd);
204 }
205 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200206}