Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 16 | #include <proto/connection.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 17 | #include <proto/fd.h> |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 18 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 19 | #include <proto/session.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 20 | #include <proto/stream_interface.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 21 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 22 | #ifdef USE_OPENSSL |
| 23 | #include <proto/ssl_sock.h> |
| 24 | #endif |
| 25 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 26 | /* I/O callback for fd-based connections. It calls the read/write handlers |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 27 | * provided by the connection's sock_ops, which must be valid. It returns 0. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 28 | */ |
| 29 | int conn_fd_handler(int fd) |
| 30 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 31 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 33 | if (unlikely(!conn)) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 34 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 36 | /* 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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 41 | process_handshake: |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 42 | /* 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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 48 | while (unlikely(conn->flags & CO_FL_HANDSHAKE)) { |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 49 | if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR))) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 50 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 51 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 52 | if (conn->flags & CO_FL_ACCEPT_PROXY) |
| 53 | if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY)) |
| 54 | goto leave; |
| 55 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 56 | if (conn->flags & CO_FL_SI_SEND_PROXY) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 57 | if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY)) |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 58 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | #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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 66 | /* 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 Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 69 | |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 70 | /* 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 Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 75 | conn_session_complete(conn, CO_FL_INIT_SESS) < 0) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 76 | return 0; |
| 77 | |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 78 | 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 Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 80 | conn->app_cb->recv(conn); |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 82 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 83 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 85 | /* 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 Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 91 | if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) && |
| 92 | !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA))) |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 93 | conn->app_cb->send(conn); |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 95 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 96 | goto leave; |
| 97 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 98 | /* 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 Tarreau | f8deb0c | 2012-09-01 17:59:22 +0200 | [diff] [blame] | 104 | 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 Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 108 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 109 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 110 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 113 | leave: |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 114 | /* 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 Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 121 | if (conn->flags & CO_FL_NOTIFY_SI) |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 122 | conn_notify_si(conn); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 123 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 124 | /* Last check, verify if the connection just established */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 125 | if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED)))) |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 126 | conn->flags |= CO_FL_CONNECTED; |
| 127 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 128 | /* remove the events before leaving */ |
| 129 | fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR); |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 130 | |
| 131 | /* commit polling changes */ |
| 132 | conn_cond_update_polling(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 133 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 134 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 135 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 136 | /* 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 141 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 142 | void conn_update_data_polling(struct connection *c) |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 143 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 144 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 145 | |
| 146 | /* update read status if needed */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 147 | 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 154 | fd_poll_recv(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 155 | } |
| 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 158 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 159 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 160 | |
| 161 | /* update write status if needed */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 162 | 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 169 | fd_poll_send(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 170 | } |
| 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 173 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 174 | } |
| 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 | */ |
| 184 | void conn_update_sock_polling(struct connection *c) |
| 185 | { |
| 186 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 188 | /* 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 218 | } |