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 | 58363cf | 2012-09-06 14:12:03 +0200 | [diff] [blame] | 78 | /* The data transfer starts here and stops on error and handshakes */ |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 79 | if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) && |
Willy Tarreau | 58363cf | 2012-09-06 14:12:03 +0200 | [diff] [blame] | 80 | !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 81 | conn->app_cb->recv(conn); |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | d9de7ca | 2012-09-02 18:48:46 +0200 | [diff] [blame] | 83 | if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) && |
Willy Tarreau | 58363cf | 2012-09-06 14:12:03 +0200 | [diff] [blame] | 84 | !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 85 | conn->app_cb->send(conn); |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 86 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 87 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 88 | goto leave; |
| 89 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 90 | /* 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 Tarreau | f8deb0c | 2012-09-01 17:59:22 +0200 | [diff] [blame] | 96 | 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 Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 100 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 101 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 102 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 105 | leave: |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame] | 106 | /* 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 Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 113 | if (conn->flags & CO_FL_NOTIFY_SI) |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 114 | conn_notify_si(conn); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 115 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 116 | /* Last check, verify if the connection just established */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 117 | 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] | 118 | conn->flags |= CO_FL_CONNECTED; |
| 119 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 120 | /* remove the events before leaving */ |
| 121 | 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] | 122 | |
| 123 | /* commit polling changes */ |
| 124 | conn_cond_update_polling(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 125 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 126 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 128 | /* 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 133 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 134 | void conn_update_data_polling(struct connection *c) |
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 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 137 | |
| 138 | /* update read status if needed */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 139 | 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 146 | fd_poll_recv(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 147 | } |
| 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 150 | fd_want_recv(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 151 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 152 | |
| 153 | /* update write status if needed */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 154 | 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 161 | fd_poll_send(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 162 | } |
| 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 165 | fd_want_send(c->t.sock.fd); |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 166 | } |
| 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 | */ |
| 176 | void conn_update_sock_polling(struct connection *c) |
| 177 | { |
| 178 | unsigned int f = c->flags; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 180 | /* 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 Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 210 | } |