blob: 0abbe42c41c8ef9c66aa08a70858dc3e7d53ad37 [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 Tarreau2c6be842012-07-06 17:12:34 +020018#include <proto/stream_interface.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020019
20/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020021 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020022 */
23int conn_fd_handler(int fd)
24{
Willy Tarreau80184712012-07-06 14:54:49 +020025 struct connection *conn = fdtab[fd].owner;
Willy Tarreau59f98392012-07-06 14:13:49 +020026
Willy Tarreauc76ae332012-07-12 15:32:13 +020027 if (unlikely(!conn))
Willy Tarreau2c6be842012-07-06 17:12:34 +020028 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020029
Willy Tarreauc76ae332012-07-12 15:32:13 +020030 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020031 /* The handshake callbacks are called in sequence. If either of them is
32 * missing something, it must enable the required polling at the socket
33 * layer of the connection. Polling state is not guaranteed when entering
34 * these handlers, so any handshake handler which does not complete its
35 * work must explicitly disable events it's not interested in.
36 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020037 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
38 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020039 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020040
Willy Tarreauc76ae332012-07-12 15:32:13 +020041 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020042 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020043 goto leave;
44 }
45
Willy Tarreauf9dabec2012-08-17 17:33:53 +020046 /* Once we're purely in the data phase, we disable handshake polling */
47 if (!(conn->flags & CO_FL_POLL_SOCK))
48 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020049
Willy Tarreau59f98392012-07-06 14:13:49 +020050 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
Willy Tarreauc5788912012-08-24 18:12:41 +020051 conn->app_cb->recv(conn);
Willy Tarreau59f98392012-07-06 14:13:49 +020052
Willy Tarreauc76ae332012-07-12 15:32:13 +020053 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2c6be842012-07-06 17:12:34 +020054 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020055
Willy Tarreauc76ae332012-07-12 15:32:13 +020056 /* It may happen during the data phase that a handshake is
57 * enabled again (eg: SSL)
58 */
59 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
60 goto process_handshake;
61
Willy Tarreau59f98392012-07-06 14:13:49 +020062 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
Willy Tarreauc5788912012-08-24 18:12:41 +020063 conn->app_cb->send(conn);
Willy Tarreau2da156f2012-07-23 15:07:23 +020064
Willy Tarreauc76ae332012-07-12 15:32:13 +020065 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +020066 goto leave;
67
Willy Tarreauc76ae332012-07-12 15:32:13 +020068 /* It may happen during the data phase that a handshake is
69 * enabled again (eg: SSL)
70 */
71 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
72 goto process_handshake;
73
74 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN)) {
Willy Tarreau2da156f2012-07-23 15:07:23 +020075 /* still waiting for a connection to establish and no data to
76 * send in order to probe it ? Then let's retry the connect().
77 */
Willy Tarreau239d7182012-07-23 18:53:03 +020078 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +020079 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +020080 }
81
Willy Tarreau2c6be842012-07-06 17:12:34 +020082 leave:
Willy Tarreaufd31e532012-07-23 18:24:25 +020083 if (conn->flags & CO_FL_NOTIFY_SI)
Willy Tarreau100c4672012-08-20 12:06:26 +020084 conn_notify_si(conn);
Willy Tarreaufd31e532012-07-23 18:24:25 +020085
Willy Tarreau8f8c92f2012-07-23 19:45:44 +020086 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +020087 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +020088 conn->flags |= CO_FL_CONNECTED;
89
Willy Tarreau61ace1b2012-07-23 12:14:26 +020090 /* remove the events before leaving */
91 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +020092
93 /* commit polling changes */
94 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +020095 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020096}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020097
98/* set polling depending on the change between the CURR part of the
99 * flags and the new flags in connection C. The connection flags are
100 * updated with the new flags at the end of the operation. Only the bits
101 * relevant to CO_FL_CURR_* from <flags> are considered.
102 */
103void conn_set_polling(struct connection *c, unsigned int new)
104{
105 unsigned int old = c->flags; /* for CO_FL_CURR_* */
106
107 /* update read status if needed */
108 if ((old & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
109 (new & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) == (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL))
110 fd_poll_recv(c->t.sock.fd);
111 else if (!(old & CO_FL_CURR_RD_ENA) && (new & CO_FL_CURR_RD_ENA))
112 fd_want_recv(c->t.sock.fd);
113 else if ((old & CO_FL_CURR_RD_ENA) && !(new & CO_FL_CURR_RD_ENA))
114 fd_stop_recv(c->t.sock.fd);
115
116 /* update write status if needed */
117 if ((old & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
118 (new & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) == (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL))
119 fd_poll_send(c->t.sock.fd);
120 else if (!(old & CO_FL_CURR_WR_ENA) && (new & CO_FL_CURR_WR_ENA))
121 fd_want_send(c->t.sock.fd);
122 else if ((old & CO_FL_CURR_WR_ENA) && !(new & CO_FL_CURR_WR_ENA))
123 fd_stop_send(c->t.sock.fd);
124
125 c->flags &= ~(CO_FL_CURR_WR_POL|CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_POL|CO_FL_CURR_RD_ENA);
126 c->flags |= new & (CO_FL_CURR_WR_POL|CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_POL|CO_FL_CURR_RD_ENA);
127}