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 | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 17 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame^] | 18 | #include <proto/session.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 19 | #include <proto/stream_interface.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 20 | |
| 21 | /* 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] | 22 | * 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] | 23 | */ |
| 24 | int conn_fd_handler(int fd) |
| 25 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 26 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 28 | if (unlikely(!conn)) |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame^] | 29 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 31 | process_handshake: |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 32 | /* The handshake callbacks are called in sequence. If either of them is |
| 33 | * missing something, it must enable the required polling at the socket |
| 34 | * layer of the connection. Polling state is not guaranteed when entering |
| 35 | * these handlers, so any handshake handler which does not complete its |
| 36 | * work must explicitly disable events it's not interested in. |
| 37 | */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 38 | while (unlikely(conn->flags & CO_FL_HANDSHAKE)) { |
| 39 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 40 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 42 | if (conn->flags & CO_FL_SI_SEND_PROXY) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 43 | if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY)) |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 44 | goto leave; |
| 45 | } |
| 46 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 47 | /* Once we're purely in the data phase, we disable handshake polling */ |
| 48 | if (!(conn->flags & CO_FL_POLL_SOCK)) |
| 49 | __conn_sock_stop_both(conn); |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame^] | 51 | /* Maybe we need to finish initializing an incoming session. The |
| 52 | * function may fail and cause the connection to be destroyed, thus |
| 53 | * we must not use it anymore and should immediately leave instead. |
| 54 | */ |
| 55 | if ((conn->flags & CO_FL_INIT_SESS) && |
| 56 | conn_session_initialize(conn, CO_FL_INIT_SESS) < 0) |
| 57 | return 0; |
| 58 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 59 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 60 | conn->app_cb->recv(conn); |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 62 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 63 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 65 | /* It may happen during the data phase that a handshake is |
| 66 | * enabled again (eg: SSL) |
| 67 | */ |
| 68 | if (unlikely(conn->flags & CO_FL_HANDSHAKE)) |
| 69 | goto process_handshake; |
| 70 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 71 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
Willy Tarreau | c578891 | 2012-08-24 18:12:41 +0200 | [diff] [blame] | 72 | conn->app_cb->send(conn); |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 74 | if (unlikely(conn->flags & CO_FL_ERROR)) |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 75 | goto leave; |
| 76 | |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 77 | /* It may happen during the data phase that a handshake is |
| 78 | * enabled again (eg: SSL) |
| 79 | */ |
| 80 | if (unlikely(conn->flags & CO_FL_HANDSHAKE)) |
| 81 | goto process_handshake; |
| 82 | |
| 83 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) { |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 84 | /* still waiting for a connection to establish and no data to |
| 85 | * send in order to probe it ? Then let's retry the connect(). |
| 86 | */ |
Willy Tarreau | 239d718 | 2012-07-23 18:53:03 +0200 | [diff] [blame] | 87 | if (!tcp_connect_probe(conn)) |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 88 | goto leave; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 91 | leave: |
Willy Tarreau | 2542b53 | 2012-08-31 16:01:23 +0200 | [diff] [blame^] | 92 | /* we may need to release the connection which is an embryonic session */ |
| 93 | if ((conn->flags & (CO_FL_ERROR|CO_FL_INIT_SESS)) == (CO_FL_ERROR|CO_FL_INIT_SESS)) { |
| 94 | conn->flags |= CO_FL_ERROR; |
| 95 | conn_session_complete(conn, CO_FL_INIT_SESS); |
| 96 | return 0; |
| 97 | } |
| 98 | |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 99 | if (conn->flags & CO_FL_NOTIFY_SI) |
Willy Tarreau | 100c467 | 2012-08-20 12:06:26 +0200 | [diff] [blame] | 100 | conn_notify_si(conn); |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 101 | |
Willy Tarreau | 8f8c92f | 2012-07-23 19:45:44 +0200 | [diff] [blame] | 102 | /* Last check, verify if the connection just established */ |
Willy Tarreau | c76ae33 | 2012-07-12 15:32:13 +0200 | [diff] [blame] | 103 | 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] | 104 | conn->flags |= CO_FL_CONNECTED; |
| 105 | |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 106 | /* remove the events before leaving */ |
| 107 | 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] | 108 | |
| 109 | /* commit polling changes */ |
| 110 | conn_cond_update_polling(conn); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 111 | return 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 112 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 113 | |
| 114 | /* set polling depending on the change between the CURR part of the |
| 115 | * flags and the new flags in connection C. The connection flags are |
| 116 | * updated with the new flags at the end of the operation. Only the bits |
| 117 | * relevant to CO_FL_CURR_* from <flags> are considered. |
| 118 | */ |
| 119 | void conn_set_polling(struct connection *c, unsigned int new) |
| 120 | { |
| 121 | unsigned int old = c->flags; /* for CO_FL_CURR_* */ |
| 122 | |
| 123 | /* update read status if needed */ |
| 124 | if ((old & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) && |
| 125 | (new & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) == (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) |
| 126 | fd_poll_recv(c->t.sock.fd); |
| 127 | else if (!(old & CO_FL_CURR_RD_ENA) && (new & CO_FL_CURR_RD_ENA)) |
| 128 | fd_want_recv(c->t.sock.fd); |
| 129 | else if ((old & CO_FL_CURR_RD_ENA) && !(new & CO_FL_CURR_RD_ENA)) |
| 130 | fd_stop_recv(c->t.sock.fd); |
| 131 | |
| 132 | /* update write status if needed */ |
| 133 | if ((old & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) && |
| 134 | (new & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) == (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) |
| 135 | fd_poll_send(c->t.sock.fd); |
| 136 | else if (!(old & CO_FL_CURR_WR_ENA) && (new & CO_FL_CURR_WR_ENA)) |
| 137 | fd_want_send(c->t.sock.fd); |
| 138 | else if ((old & CO_FL_CURR_WR_ENA) && !(new & CO_FL_CURR_WR_ENA)) |
| 139 | fd_stop_send(c->t.sock.fd); |
| 140 | |
| 141 | c->flags &= ~(CO_FL_CURR_WR_POL|CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_POL|CO_FL_CURR_RD_ENA); |
| 142 | c->flags |= new & (CO_FL_CURR_WR_POL|CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_POL|CO_FL_CURR_RD_ENA); |
| 143 | } |