1 /*
2 * include/proto/stream_interface.h
3 * This file contains stream_interface function prototypes
4 *
5 * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
6 *
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 */
21
22 #ifndef _PROTO_STREAM_INTERFACE_H
23 #define _PROTO_STREAM_INTERFACE_H
24
25 #include <stdlib.h>
26
27 #include <common/config.h>
28 #include <types/server.h>
29 #include <types/stream.h>
30 #include <types/stream_interface.h>
31 #include <proto/applet.h>
32 #include <proto/channel.h>
33 #include <proto/connection.h>
34
35
36 /* main event functions used to move data between sockets and buffers */
37 int stream_int_check_timeouts(struct stream_interface *si);
38 void stream_int_report_error(struct stream_interface *si);
39 void stream_int_retnclose(struct stream_interface *si, const struct chunk *msg);
40 int conn_si_send_proxy(struct connection *conn, unsigned int flag);
41 void stream_sock_read0(struct stream_interface *si);
42
43 extern struct si_ops si_embedded_ops;
44 extern struct si_ops si_conn_ops;
45 extern struct si_ops si_applet_ops;
46 extern struct data_cb si_conn_cb;
47 extern struct data_cb si_idle_conn_cb;
48
49 struct appctx *stream_int_register_handler(struct stream_interface *si, struct applet *app);
50 void si_applet_wake_cb(struct stream_interface *si);
51 void stream_int_update(struct stream_interface *si);
52 void stream_int_update_conn(struct stream_interface *si);
53 void stream_int_update_applet(struct stream_interface *si);
54 void stream_int_notify(struct stream_interface *si);
55
56 /* returns the channel which receives data from this stream interface (input channel) */
si_ic(struct stream_interface * si)57 static inline struct channel *si_ic(struct stream_interface *si)
58 {
59 if (si->flags & SI_FL_ISBACK)
60 return &LIST_ELEM(si, struct stream *, si[1])->res;
61 else
62 return &LIST_ELEM(si, struct stream *, si[0])->req;
63 }
64
65 /* returns the channel which feeds data to this stream interface (output channel) */
si_oc(struct stream_interface * si)66 static inline struct channel *si_oc(struct stream_interface *si)
67 {
68 if (si->flags & SI_FL_ISBACK)
69 return &LIST_ELEM(si, struct stream *, si[1])->req;
70 else
71 return &LIST_ELEM(si, struct stream *, si[0])->res;
72 }
73
74 /* returns the buffer which receives data from this stream interface (input channel's buffer) */
si_ib(struct stream_interface * si)75 static inline struct buffer *si_ib(struct stream_interface *si)
76 {
77 return si_ic(si)->buf;
78 }
79
80 /* returns the buffer which feeds data to this stream interface (output channel's buffer) */
si_ob(struct stream_interface * si)81 static inline struct buffer *si_ob(struct stream_interface *si)
82 {
83 return si_oc(si)->buf;
84 }
85
86 /* returns the stream associated to a stream interface */
si_strm(struct stream_interface * si)87 static inline struct stream *si_strm(struct stream_interface *si)
88 {
89 if (si->flags & SI_FL_ISBACK)
90 return LIST_ELEM(si, struct stream *, si[1]);
91 else
92 return LIST_ELEM(si, struct stream *, si[0]);
93 }
94
95 /* returns the task associated to this stream interface */
si_task(struct stream_interface * si)96 static inline struct task *si_task(struct stream_interface *si)
97 {
98 if (si->flags & SI_FL_ISBACK)
99 return LIST_ELEM(si, struct stream *, si[1])->task;
100 else
101 return LIST_ELEM(si, struct stream *, si[0])->task;
102 }
103
104 /* returns the stream interface on the other side. Used during forwarding. */
si_opposite(struct stream_interface * si)105 static inline struct stream_interface *si_opposite(struct stream_interface *si)
106 {
107 if (si->flags & SI_FL_ISBACK)
108 return &LIST_ELEM(si, struct stream *, si[1])->si[0];
109 else
110 return &LIST_ELEM(si, struct stream *, si[0])->si[1];
111 }
112
113 /* initializes a stream interface in the SI_ST_INI state. It's detached from
114 * any endpoint and only keeps its side which is expected to have already been
115 * set.
116 */
si_reset(struct stream_interface * si)117 static inline void si_reset(struct stream_interface *si)
118 {
119 si->err_type = SI_ET_NONE;
120 si->conn_retries = 0; /* used for logging too */
121 si->exp = TICK_ETERNITY;
122 si->flags &= SI_FL_ISBACK;
123 si->end = NULL;
124 si->state = si->prev_state = SI_ST_INI;
125 si->ops = &si_embedded_ops;
126 }
127
128 /* sets the current and previous state of a stream interface to <state>. This
129 * is mainly used to create one in the established state on incoming
130 * conncetions.
131 */
si_set_state(struct stream_interface * si,int state)132 static inline void si_set_state(struct stream_interface *si, int state)
133 {
134 si->state = si->prev_state = state;
135 }
136
137 /* only detaches the endpoint from the SI, which means that it's set to
138 * NULL and that ->ops is mapped to si_embedded_ops. The previous endpoint
139 * is returned.
140 */
si_detach_endpoint(struct stream_interface * si)141 static inline enum obj_type *si_detach_endpoint(struct stream_interface *si)
142 {
143 enum obj_type *prev = si->end;
144
145 si->end = NULL;
146 si->ops = &si_embedded_ops;
147 return prev;
148 }
149
150 /* Release the endpoint if it's a connection or an applet, then nullify it.
151 * Note: released connections are closed then freed.
152 */
si_release_endpoint(struct stream_interface * si)153 static inline void si_release_endpoint(struct stream_interface *si)
154 {
155 struct conn_stream *cs;
156 struct appctx *appctx;
157
158 if (!si->end)
159 return;
160
161 if ((cs = objt_cs(si->end)))
162 cs_destroy(cs);
163 else if ((appctx = objt_appctx(si->end))) {
164 if (appctx->applet->release && si->state < SI_ST_DIS)
165 appctx->applet->release(appctx);
166 appctx_free(appctx); /* we share the connection pool */
167 }
168 si_detach_endpoint(si);
169 }
170
171 /* Turn an existing connection endpoint of stream interface <si> to idle mode,
172 * which means that the connection will be polled for incoming events and might
173 * be killed by the underlying I/O handler. If <pool> is not null, the
174 * connection will also be added at the head of this list. This connection
175 * remains assigned to the stream interface it is currently attached to.
176 */
si_idle_cs(struct stream_interface * si,struct list * pool)177 static inline void si_idle_cs(struct stream_interface *si, struct list *pool)
178 {
179 struct conn_stream *cs = __objt_cs(si->end);
180 struct connection *conn = cs->conn;
181
182 if (pool)
183 LIST_ADD(pool, &conn->list);
184
185 cs_attach(cs, si, &si_idle_conn_cb);
186 cs_want_recv(cs);
187 }
188
189 /* Attach conn_stream <cs> to the stream interface <si>. The stream interface
190 * is configured to work with a connection and the connection it configured
191 * with a stream interface data layer.
192 */
si_attach_cs(struct stream_interface * si,struct conn_stream * cs)193 static inline void si_attach_cs(struct stream_interface *si, struct conn_stream *cs)
194 {
195 si->ops = &si_conn_ops;
196 si->end = &cs->obj_type;
197 cs_attach(cs, si, &si_conn_cb);
198 }
199
200 /* Returns true if a connection is attached to the stream interface <si> and
201 * if this connection is ready.
202 */
si_conn_ready(struct stream_interface * si)203 static inline int si_conn_ready(struct stream_interface *si)
204 {
205 struct connection *conn = cs_conn(objt_cs(si->end));
206
207 return conn && conn_ctrl_ready(conn) && conn_xprt_ready(conn);
208 }
209
210 /* Attach appctx <appctx> to the stream interface <si>. The stream interface
211 * is configured to work with an applet context.
212 */
si_attach_appctx(struct stream_interface * si,struct appctx * appctx)213 static inline void si_attach_appctx(struct stream_interface *si, struct appctx *appctx)
214 {
215 si->ops = &si_applet_ops;
216 si->end = &appctx->obj_type;
217 appctx->owner = si;
218 }
219
220 /* returns a pointer to the appctx being run in the SI or NULL if none */
si_appctx(struct stream_interface * si)221 static inline struct appctx *si_appctx(struct stream_interface *si)
222 {
223 return objt_appctx(si->end);
224 }
225
226 /* call the applet's release function if any. Needs to be called upon close() */
si_applet_release(struct stream_interface * si)227 static inline void si_applet_release(struct stream_interface *si)
228 {
229 struct appctx *appctx;
230
231 appctx = si_appctx(si);
232 if (appctx && appctx->applet->release && si->state < SI_ST_DIS)
233 appctx->applet->release(appctx);
234 }
235
236 /* let an applet indicate that it wants to put some data into the input buffer */
si_applet_want_put(struct stream_interface * si)237 static inline void si_applet_want_put(struct stream_interface *si)
238 {
239 si->flags |= SI_FL_WANT_PUT;
240 }
241
242 /* let an applet indicate that it wanted to put some data into the input buffer
243 * but it couldn't.
244 */
si_applet_cant_put(struct stream_interface * si)245 static inline void si_applet_cant_put(struct stream_interface *si)
246 {
247 si->flags |= SI_FL_WANT_PUT | SI_FL_WAIT_ROOM;
248 }
249
250 /* let an applet indicate that it doesn't want to put data into the input buffer */
si_applet_stop_put(struct stream_interface * si)251 static inline void si_applet_stop_put(struct stream_interface *si)
252 {
253 si->flags &= ~SI_FL_WANT_PUT;
254 }
255
256 /* let an applet indicate that it wants to get some data from the output buffer */
si_applet_want_get(struct stream_interface * si)257 static inline void si_applet_want_get(struct stream_interface *si)
258 {
259 si->flags |= SI_FL_WANT_GET;
260 }
261
262 /* let an applet indicate that it wanted to get some data from the output buffer
263 * but it couldn't.
264 */
si_applet_cant_get(struct stream_interface * si)265 static inline void si_applet_cant_get(struct stream_interface *si)
266 {
267 si->flags |= SI_FL_WANT_GET | SI_FL_WAIT_DATA;
268 }
269
270 /* let an applet indicate that it doesn't want to get data from the input buffer */
si_applet_stop_get(struct stream_interface * si)271 static inline void si_applet_stop_get(struct stream_interface *si)
272 {
273 si->flags &= ~SI_FL_WANT_GET;
274 }
275
276 /* Try to allocate a new conn_stream and assign it to the interface. If
277 * an endpoint was previously allocated, it is released first. The newly
278 * allocated conn_stream is initialized, assigned to the stream interface,
279 * and returned.
280 */
si_alloc_cs(struct stream_interface * si,struct connection * conn)281 static inline struct conn_stream *si_alloc_cs(struct stream_interface *si, struct connection *conn)
282 {
283 struct conn_stream *cs;
284
285 si_release_endpoint(si);
286
287 cs = cs_new(conn);
288 if (cs)
289 si_attach_cs(si, cs);
290
291 return cs;
292 }
293
294 /* Release the interface's existing endpoint (connection or appctx) and
295 * allocate then initialize a new appctx which is assigned to the interface
296 * and returned. NULL may be returned upon memory shortage. Applet <applet>
297 * is assigned to the appctx, but it may be NULL.
298 */
si_alloc_appctx(struct stream_interface * si,struct applet * applet)299 static inline struct appctx *si_alloc_appctx(struct stream_interface *si, struct applet *applet)
300 {
301 struct appctx *appctx;
302
303 si_release_endpoint(si);
304 appctx = appctx_new(applet, tid_bit);
305 if (appctx)
306 si_attach_appctx(si, appctx);
307
308 return appctx;
309 }
310
311 /* Sends a shutr to the connection using the data layer */
si_shutr(struct stream_interface * si)312 static inline void si_shutr(struct stream_interface *si)
313 {
314 si->ops->shutr(si);
315 }
316
317 /* Sends a shutw to the connection using the data layer */
si_shutw(struct stream_interface * si)318 static inline void si_shutw(struct stream_interface *si)
319 {
320 si->ops->shutw(si);
321 }
322
323 /* Marks on the stream-interface that next shutw must kill the whole connection */
si_must_kill_conn(struct stream_interface * si)324 static inline void si_must_kill_conn(struct stream_interface *si)
325 {
326 si->flags |= SI_FL_KILL_CONN;
327 }
328
329 /* Updates the stream interface and timers, then updates the data layer below */
si_update(struct stream_interface * si)330 static inline void si_update(struct stream_interface *si)
331 {
332 stream_int_update(si);
333 if (si->ops->update)
334 si->ops->update(si);
335 }
336
337 /* Calls chk_rcv on the connection using the data layer */
si_chk_rcv(struct stream_interface * si)338 static inline void si_chk_rcv(struct stream_interface *si)
339 {
340 si->ops->chk_rcv(si);
341 }
342
343 /* Calls chk_snd on the connection using the data layer */
si_chk_snd(struct stream_interface * si)344 static inline void si_chk_snd(struct stream_interface *si)
345 {
346 si->ops->chk_snd(si);
347 }
348
349 /* Calls chk_snd on the connection using the ctrl layer */
si_connect(struct stream_interface * si)350 static inline int si_connect(struct stream_interface *si)
351 {
352 struct conn_stream *cs = objt_cs(si->end);
353 struct connection *conn = cs_conn(cs);
354 int ret = SF_ERR_NONE;
355
356 if (unlikely(!conn || !conn->ctrl || !conn->ctrl->connect))
357 return SF_ERR_INTERNAL;
358
359 if (!conn_ctrl_ready(conn) || !conn_xprt_ready(conn)) {
360 ret = conn->ctrl->connect(conn, !channel_is_empty(si_oc(si)), 0);
361 if (ret != SF_ERR_NONE)
362 return ret;
363
364 /* we're in the process of establishing a connection */
365 si->state = SI_ST_CON;
366 }
367 else {
368 /* reuse the existing connection */
369 if (!channel_is_empty(si_oc(si))) {
370 /* we'll have to send a request there. */
371 cs_want_send(cs);
372 }
373
374 /* the connection is established */
375 si->state = SI_ST_EST;
376 }
377
378 /* needs src ip/port for logging */
379 if (si->flags & SI_FL_SRC_ADDR)
380 conn_get_from_addr(conn);
381
382 return ret;
383 }
384
385 /* for debugging, reports the stream interface state name */
si_state_str(int state)386 static inline const char *si_state_str(int state)
387 {
388 switch (state) {
389 case SI_ST_INI: return "INI";
390 case SI_ST_REQ: return "REQ";
391 case SI_ST_QUE: return "QUE";
392 case SI_ST_TAR: return "TAR";
393 case SI_ST_ASS: return "ASS";
394 case SI_ST_CON: return "CON";
395 case SI_ST_CER: return "CER";
396 case SI_ST_EST: return "EST";
397 case SI_ST_DIS: return "DIS";
398 case SI_ST_CLO: return "CLO";
399 default: return "???";
400 }
401 }
402
403 #endif /* _PROTO_STREAM_INTERFACE_H */
404
405 /*
406 * Local variables:
407 * c-indent-level: 8
408 * c-basic-offset: 8
409 * End:
410 */
411