blob: 6d4da780680082b31e2e7a18cf2e01b4f189b89b [file] [log] [blame]
Willy Tarreaucff64112008-11-03 06:26:53 +01001/*
Willy Tarreau8e89b842009-10-18 23:56:35 +02002 * include/proto/stream_interface.h
3 * This file contains stream_interface function prototypes
4 *
Willy Tarreauf873d752012-05-11 17:47:17 +02005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau8e89b842009-10-18 23:56:35 +02006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy Tarreaucff64112008-11-03 06:26:53 +010021
22#ifndef _PROTO_STREAM_INTERFACE_H
23#define _PROTO_STREAM_INTERFACE_H
24
25#include <stdlib.h>
26
27#include <common/config.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020028#include <types/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010029#include <types/stream_interface.h>
Willy Tarreau14f8e862012-08-30 22:23:13 +020030#include <proto/channel.h>
Willy Tarreauf9dabec2012-08-17 17:33:53 +020031#include <proto/connection.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010032
33
34/* main event functions used to move data between sockets and buffers */
Willy Tarreau269358d2009-09-20 20:14:49 +020035int stream_int_check_timeouts(struct stream_interface *si);
Willy Tarreaucff64112008-11-03 06:26:53 +010036void stream_int_report_error(struct stream_interface *si);
Willy Tarreaudded32d2008-11-30 19:48:07 +010037void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg);
Willy Tarreau2c6be842012-07-06 17:12:34 +020038int conn_si_send_proxy(struct connection *conn, unsigned int flag);
Willy Tarreau9bf9c142012-08-20 15:38:41 +020039void stream_sock_read0(struct stream_interface *si);
Willy Tarreaucff64112008-11-03 06:26:53 +010040
Willy Tarreauc5788912012-08-24 18:12:41 +020041extern struct si_ops si_embedded_ops;
Willy Tarreauc5788912012-08-24 18:12:41 +020042extern struct si_ops si_conn_ops;
Willy Tarreau74beec32012-10-03 00:41:04 +020043extern struct data_cb si_conn_cb;
Willy Tarreau27375622013-12-17 00:00:28 +010044extern struct data_cb si_idle_conn_cb;
Willy Tarreau5c979a92012-05-07 17:15:39 +020045
Willy Tarreau1fbe1c92013-12-01 09:35:41 +010046struct appctx *stream_int_register_handler(struct stream_interface *si, struct si_applet *app);
Willy Tarreaufb90d942009-09-05 20:57:35 +020047void stream_int_unregister_handler(struct stream_interface *si);
48
Willy Tarreau0a23bcb2013-12-01 11:31:38 +010049/* Initializes all required fields for a new appctx. Note that it does the
50 * minimum acceptable initialization for an appctx. This means only the
51 * 3 integer states st0, st1, st2 are zeroed.
52 */
53static inline void appctx_init(struct appctx *appctx)
54{
55 appctx->st0 = appctx->st1 = appctx->st2 = 0;
56}
57
58/* sets <appctx>'s applet to point to <applet> */
59static inline void appctx_set_applet(struct appctx *appctx, struct si_applet *applet)
60{
61 appctx->applet = applet;
62}
63
64/* Tries to allocate a new appctx and initialize its main fields. The
65 * appctx is returned on success, NULL on failure. The appctx must be
66 * released using pool_free2(connection) or appctx_free(), since it's
67 * allocated from the connection pool.
68 */
69static inline struct appctx *appctx_new()
70{
71 struct appctx *appctx;
72
73 appctx = pool_alloc2(pool2_connection);
74 if (likely(appctx != NULL)) {
75 appctx->obj_type = OBJ_TYPE_APPCTX;
76 appctx->applet = NULL;
77 appctx_init(appctx);
78 }
79 return appctx;
80}
81
82/* Releases an appctx previously allocated by appctx_new(). Note that
83 * we share the connection pool.
84 */
85static inline void appctx_free(struct appctx *appctx)
86{
87 pool_free2(pool2_connection, appctx);
88}
89
Willy Tarreau3ed35ef2013-10-24 11:51:38 +020090/* initializes a stream interface in the SI_ST_INI state. It's detached from
91 * any endpoint and is only attached to an owner (generally a task).
92 */
93static inline void si_reset(struct stream_interface *si, void *owner)
94{
95 si->owner = owner;
96 si->err_type = SI_ET_NONE;
97 si->conn_retries = 0; /* used for logging too */
Willy Tarreau3ed35ef2013-10-24 11:51:38 +020098 si->exp = TICK_ETERNITY;
99 si->flags = SI_FL_NONE;
100 si->end = NULL;
101 si->state = si->prev_state = SI_ST_INI;
102}
103
104/* sets the current and previous state of a stream interface to <state>. This
105 * is mainly used to create one in the established state on incoming
106 * conncetions.
107 */
108static inline void si_set_state(struct stream_interface *si, int state)
109{
110 si->state = si->prev_state = state;
111}
112
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100113/* Release the endpoint if it's a connection or an applet, then nullify it.
114 * Note: released connections are closed then freed.
115 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200116static inline void si_release_endpoint(struct stream_interface *si)
Willy Tarreau372d6702013-09-29 17:19:56 +0200117{
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200118 struct connection *conn;
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100119 struct appctx *appctx;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200120
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100121 if (!si->end)
122 return;
123
124 if ((conn = objt_conn(si->end))) {
125 conn_force_close(conn);
126 conn_free(conn);
127 }
128 else if ((appctx = objt_appctx(si->end))) {
129 if (appctx->applet->release)
130 appctx->applet->release(si);
131 appctx_free(appctx); /* we share the connection pool */
132 }
Willy Tarreau7d67d7b2013-09-29 16:05:22 +0200133 si->end = NULL;
Willy Tarreau372d6702013-09-29 17:19:56 +0200134}
135
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200136static inline void si_detach(struct stream_interface *si)
137{
138 si_release_endpoint(si);
139 si->ops = &si_embedded_ops;
140}
141
Willy Tarreau27375622013-12-17 00:00:28 +0100142/* Turn a possibly existing connection endpoint of stream interface <si> to
143 * idle mode, which means that the connection will be polled for incoming events
144 * and might be killed by the underlying I/O handler.
145 */
146static inline void si_idle_conn(struct stream_interface *si)
147{
148 struct connection *conn = objt_conn(si->end);
149
150 if (!conn)
151 return;
152
153 conn_attach(conn, si, &si_idle_conn_cb);
154 conn_data_want_recv(conn);
155}
156
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200157/* Attach connection <conn> to the stream interface <si>. The stream interface
158 * is configured to work with a connection and the connection it configured
159 * with a stream interface data layer.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200160 */
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200161static inline void si_attach_conn(struct stream_interface *si, struct connection *conn)
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200162{
163 si->ops = &si_conn_ops;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200164 si->end = &conn->obj_type;
Willy Tarreau7abddb52013-10-24 15:31:04 +0200165 conn_attach(conn, si, &si_conn_cb);
Willy Tarreaubd99aab2012-10-02 20:57:19 +0200166}
167
Willy Tarreaud02cdd22013-12-15 10:23:20 +0100168/* Returns true if a connection is attached to the stream interface <si> and
169 * if this connection is ready.
170 */
171static inline int si_conn_ready(struct stream_interface *si)
172{
173 struct connection *conn = objt_conn(si->end);
174
175 return conn && conn_ctrl_ready(conn) && conn_xprt_ready(conn);
176}
177
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100178/* Attach appctx <appctx> to the stream interface <si>. The stream interface
179 * is configured to work with an applet context. It is left to the caller to
180 * call appctx_set_applet() to assign an applet to this context.
181 */
182static inline void si_attach_appctx(struct stream_interface *si, struct appctx *appctx)
Willy Tarreauc5788912012-08-24 18:12:41 +0200183{
184 si->ops = &si_embedded_ops;
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100185 appctx->obj_type = OBJ_TYPE_APPCTX;
186 si->end = &appctx->obj_type;
Willy Tarreauc5788912012-08-24 18:12:41 +0200187}
188
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100189/* returns a pointer to the appctx being run in the SI or NULL if none */
190static inline struct appctx *si_appctx(struct stream_interface *si)
191{
192 return objt_appctx(si->end);
193}
194
Willy Tarreau4a59f2f2013-10-24 20:10:45 +0200195/* returns a pointer to the applet being run in the SI or NULL if none */
196static inline const struct si_applet *si_applet(struct stream_interface *si)
197{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100198 const struct appctx *appctx;
199
200 appctx = si_appctx(si);
201 if (appctx)
202 return appctx->applet;
Willy Tarreaucf644ed2013-09-29 17:19:56 +0200203 return NULL;
204}
205
206/* Call the applet's main function when an appctx is attached to the stream
207 * interface. Returns zero if no call was made, or non-zero if a call was made.
208 */
209static inline int si_applet_call(struct stream_interface *si)
210{
211 const struct si_applet *applet;
212
213 applet = si_applet(si);
214 if (applet) {
215 applet->fct(si);
216 return 1;
217 }
218 return 0;
Willy Tarreau4a59f2f2013-10-24 20:10:45 +0200219}
220
221/* call the applet's release function if any. Needs to be called upon close() */
222static inline void si_applet_release(struct stream_interface *si)
223{
224 const struct si_applet *applet;
225
226 applet = si_applet(si);
227 if (applet && applet->release)
228 applet->release(si);
229}
230
Willy Tarreau9471b8c2013-12-15 13:31:35 +0100231/* Try to allocate a new connection and assign it to the interface. If
232 * a connection was previously allocated and the <reuse> flag is set,
233 * it is returned unmodified. Otherwise it is reset.
234 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200235/* Returns the stream interface's existing connection if one such already
236 * exists, or tries to allocate and initialize a new one which is then
237 * assigned to the stream interface.
238 */
Willy Tarreau9471b8c2013-12-15 13:31:35 +0100239static inline struct connection *si_alloc_conn(struct stream_interface *si, int reuse)
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200240{
241 struct connection *conn;
242
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100243 /* If we find a connection, we return it, otherwise it's an applet
244 * and we start by releasing it.
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200245 */
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100246 if (si->end) {
247 conn = objt_conn(si->end);
Willy Tarreau9471b8c2013-12-15 13:31:35 +0100248 if (conn) {
249 if (!reuse) {
250 conn_force_close(conn);
251 conn_init(conn);
252 }
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100253 return conn;
Willy Tarreau9471b8c2013-12-15 13:31:35 +0100254 }
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100255 /* it was an applet then */
256 si_release_endpoint(si);
257 }
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200258
259 conn = conn_new();
260 if (conn)
261 si_attach_conn(si, conn);
262
263 return conn;
264}
265
Willy Tarreau0a23bcb2013-12-01 11:31:38 +0100266/* Release the interface's existing endpoint (connection or appctx) and
267 * allocate then initialize a new appctx which is assigned to the interface
268 * and returned. NULL may be returned upon memory shortage. It is left to the
269 * caller to call appctx_set_applet() to assign an applet to this context.
270 */
271static inline struct appctx *si_alloc_appctx(struct stream_interface *si)
272{
273 struct appctx *appctx;
274
275 si_release_endpoint(si);
276 appctx = appctx_new();
277 if (appctx)
278 si_attach_appctx(si, appctx);
279
280 return appctx;
281}
282
Willy Tarreau73b013b2012-05-21 16:31:45 +0200283/* Sends a shutr to the connection using the data layer */
284static inline void si_shutr(struct stream_interface *si)
285{
Willy Tarreau6fe15412013-09-29 15:16:03 +0200286 si->ops->shutr(si);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200287}
288
289/* Sends a shutw to the connection using the data layer */
290static inline void si_shutw(struct stream_interface *si)
291{
Willy Tarreau6fe15412013-09-29 15:16:03 +0200292 si->ops->shutw(si);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200293}
294
295/* Calls the data state update on the stream interfaace */
296static inline void si_update(struct stream_interface *si)
297{
Willy Tarreauc5788912012-08-24 18:12:41 +0200298 si->ops->update(si);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200299}
300
301/* Calls chk_rcv on the connection using the data layer */
302static inline void si_chk_rcv(struct stream_interface *si)
303{
Willy Tarreauc5788912012-08-24 18:12:41 +0200304 si->ops->chk_rcv(si);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200305}
306
307/* Calls chk_snd on the connection using the data layer */
308static inline void si_chk_snd(struct stream_interface *si)
309{
Willy Tarreauc5788912012-08-24 18:12:41 +0200310 si->ops->chk_snd(si);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200311}
312
313/* Calls chk_snd on the connection using the ctrl layer */
314static inline int si_connect(struct stream_interface *si)
315{
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200316 struct connection *conn = objt_conn(si->end);
Willy Tarreaub490b4e2013-12-15 16:20:50 +0100317 int ret = SN_ERR_NONE;
Willy Tarreau14f8e862012-08-30 22:23:13 +0200318
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200319 if (unlikely(!conn || !conn->ctrl || !conn->ctrl->connect))
Willy Tarreau73b013b2012-05-21 16:31:45 +0200320 return SN_ERR_INTERNAL;
Willy Tarreau14f8e862012-08-30 22:23:13 +0200321
Willy Tarreaub490b4e2013-12-15 16:20:50 +0100322 if (!conn_ctrl_ready(conn) || !conn_xprt_ready(conn)) {
323 ret = conn->ctrl->connect(conn, !channel_is_empty(si->ob), 0);
324 if (ret != SN_ERR_NONE)
325 return ret;
326 }
327 else if (!channel_is_empty(si->ob)) {
328 /* reuse the existing connection, we'll have to send a
329 * request there.
330 */
331 conn_data_want_send(conn);
332 }
Willy Tarreau14f8e862012-08-30 22:23:13 +0200333
334 /* needs src ip/port for logging */
335 if (si->flags & SI_FL_SRC_ADDR)
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200336 conn_get_from_addr(conn);
Willy Tarreau14f8e862012-08-30 22:23:13 +0200337
Willy Tarreau14f8e862012-08-30 22:23:13 +0200338 /* we need to be notified about connection establishment */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200339 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau14f8e862012-08-30 22:23:13 +0200340
341 /* we're in the process of establishing a connection */
342 si->state = SI_ST_CON;
343
344 return ret;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200345}
Willy Tarreau59b94792012-05-11 16:16:40 +0200346
Willy Tarreau55e4ecd2012-12-08 17:48:47 +0100347/* for debugging, reports the stream interface state name */
348static inline const char *si_state_str(int state)
349{
350 switch (state) {
351 case SI_ST_INI: return "INI";
352 case SI_ST_REQ: return "REQ";
353 case SI_ST_QUE: return "QUE";
354 case SI_ST_TAR: return "TAR";
355 case SI_ST_ASS: return "ASS";
356 case SI_ST_CON: return "CON";
357 case SI_ST_CER: return "CER";
358 case SI_ST_EST: return "EST";
359 case SI_ST_DIS: return "DIS";
360 case SI_ST_CLO: return "CLO";
361 default: return "???";
362 }
363}
364
Willy Tarreaucff64112008-11-03 06:26:53 +0100365#endif /* _PROTO_STREAM_INTERFACE_H */
366
367/*
368 * Local variables:
369 * c-indent-level: 8
370 * c-basic-offset: 8
371 * End:
372 */