1 /*
2 * Functions managing stream_interface structures
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 <errno.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <haproxy/api.h>
23 #include <haproxy/applet.h>
24 #include <haproxy/channel.h>
25 #include <haproxy/connection.h>
26 #include <haproxy/dynbuf.h>
27 #include <haproxy/http_htx.h>
28 #include <haproxy/pipe-t.h>
29 #include <haproxy/pipe.h>
30 #include <haproxy/proxy.h>
31 #include <haproxy/stream-t.h>
32 #include <haproxy/stream_interface.h>
33 #include <haproxy/task.h>
34 #include <haproxy/ticks.h>
35 #include <haproxy/time.h>
36 #include <haproxy/tools.h>
37
38
39 /* functions used by default on a detached stream-interface */
40 static void stream_int_shutr(struct stream_interface *si);
41 static void stream_int_shutw(struct stream_interface *si);
42 static void stream_int_chk_rcv(struct stream_interface *si);
43 static void stream_int_chk_snd(struct stream_interface *si);
44
45 /* functions used on a conn_stream-based stream-interface */
46 static void stream_int_shutr_conn(struct stream_interface *si);
47 static void stream_int_shutw_conn(struct stream_interface *si);
48 static void stream_int_chk_rcv_conn(struct stream_interface *si);
49 static void stream_int_chk_snd_conn(struct stream_interface *si);
50
51 /* functions used on an applet-based stream-interface */
52 static void stream_int_shutr_applet(struct stream_interface *si);
53 static void stream_int_shutw_applet(struct stream_interface *si);
54 static void stream_int_chk_rcv_applet(struct stream_interface *si);
55 static void stream_int_chk_snd_applet(struct stream_interface *si);
56
57 /* last read notification */
58 static void stream_int_read0(struct stream_interface *si);
59
60 /* post-IO notification callback */
61 static void stream_int_notify(struct stream_interface *si);
62
63 /* stream-interface operations for embedded tasks */
64 struct si_ops si_embedded_ops = {
65 .chk_rcv = stream_int_chk_rcv,
66 .chk_snd = stream_int_chk_snd,
67 .shutr = stream_int_shutr,
68 .shutw = stream_int_shutw,
69 };
70
71 /* stream-interface operations for connections */
72 struct si_ops si_conn_ops = {
73 .chk_rcv = stream_int_chk_rcv_conn,
74 .chk_snd = stream_int_chk_snd_conn,
75 .shutr = stream_int_shutr_conn,
76 .shutw = stream_int_shutw_conn,
77 };
78
79 /* stream-interface operations for connections */
80 struct si_ops si_applet_ops = {
81 .chk_rcv = stream_int_chk_rcv_applet,
82 .chk_snd = stream_int_chk_snd_applet,
83 .shutr = stream_int_shutr_applet,
84 .shutw = stream_int_shutw_applet,
85 };
86
87
88 /* Functions used to communicate with a conn_stream. The first two may be used
89 * directly, the last one is mostly a wake callback.
90 */
91 int si_cs_recv(struct conn_stream *cs);
92 int si_cs_send(struct conn_stream *cs);
93 static int si_cs_process(struct conn_stream *cs);
94
95
96 struct data_cb si_conn_cb = {
97 .wake = si_cs_process,
98 .name = "STRM",
99 };
100
101 /*
102 * This function only has to be called once after a wakeup event in case of
103 * suspected timeout. It controls the stream interface timeouts and sets
104 * si->flags accordingly. It does NOT close anything, as this timeout may
105 * be used for any purpose. It returns 1 if the timeout fired, otherwise
106 * zero.
107 */
si_check_timeouts(struct stream_interface * si)108 int si_check_timeouts(struct stream_interface *si)
109 {
110 if (tick_is_expired(si->exp, now_ms)) {
111 si->flags |= SI_FL_EXP;
112 return 1;
113 }
114 return 0;
115 }
116
117 /* to be called only when in SI_ST_DIS with SI_FL_ERR */
si_report_error(struct stream_interface * si)118 void si_report_error(struct stream_interface *si)
119 {
120 if (!si->err_type)
121 si->err_type = SI_ET_DATA_ERR;
122
123 si_oc(si)->flags |= CF_WRITE_ERROR;
124 si_ic(si)->flags |= CF_READ_ERROR;
125 }
126
127 /*
128 * Returns a message to the client ; the connection is shut down for read,
129 * and the request is cleared so that no server connection can be initiated.
130 * The buffer is marked for read shutdown on the other side to protect the
131 * message, and the buffer write is enabled. The message is contained in a
132 * "chunk". If it is null, then an empty message is used. The reply buffer does
133 * not need to be empty before this, and its contents will not be overwritten.
134 * The primary goal of this function is to return error messages to a client.
135 */
si_retnclose(struct stream_interface * si,const struct buffer * msg)136 void si_retnclose(struct stream_interface *si,
137 const struct buffer *msg)
138 {
139 struct channel *ic = si_ic(si);
140 struct channel *oc = si_oc(si);
141
142 channel_auto_read(ic);
143 channel_abort(ic);
144 channel_auto_close(ic);
145 channel_erase(ic);
146 channel_truncate(oc);
147
148 if (likely(msg && msg->data))
149 co_inject(oc, msg->area, msg->data);
150
151 oc->wex = tick_add_ifset(now_ms, oc->wto);
152 channel_auto_read(oc);
153 channel_auto_close(oc);
154 channel_shutr_now(oc);
155 }
156
157 /*
158 * This function performs a shutdown-read on a detached stream interface in a
159 * connected or init state (it does nothing for other states). It either shuts
160 * the read side or marks itself as closed. The buffer flags are updated to
161 * reflect the new state. If the stream interface has SI_FL_NOHALF, we also
162 * forward the close to the write side. The owner task is woken up if it exists.
163 */
stream_int_shutr(struct stream_interface * si)164 static void stream_int_shutr(struct stream_interface *si)
165 {
166 struct channel *ic = si_ic(si);
167
168 si_rx_shut_blk(si);
169 if (ic->flags & CF_SHUTR)
170 return;
171 ic->flags |= CF_SHUTR;
172 ic->rex = TICK_ETERNITY;
173
174 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
175 return;
176
177 if (si_oc(si)->flags & CF_SHUTW) {
178 si->state = SI_ST_DIS;
179 si->exp = TICK_ETERNITY;
180 }
181 else if (si->flags & SI_FL_NOHALF) {
182 /* we want to immediately forward this close to the write side */
183 return stream_int_shutw(si);
184 }
185
186 /* note that if the task exists, it must unregister itself once it runs */
187 if (!(si->flags & SI_FL_DONT_WAKE))
188 task_wakeup(si_task(si), TASK_WOKEN_IO);
189 }
190
191 /*
192 * This function performs a shutdown-write on a detached stream interface in a
193 * connected or init state (it does nothing for other states). It either shuts
194 * the write side or marks itself as closed. The buffer flags are updated to
195 * reflect the new state. It does also close everything if the SI was marked as
196 * being in error state. The owner task is woken up if it exists.
197 */
stream_int_shutw(struct stream_interface * si)198 static void stream_int_shutw(struct stream_interface *si)
199 {
200 struct channel *ic = si_ic(si);
201 struct channel *oc = si_oc(si);
202
203 oc->flags &= ~CF_SHUTW_NOW;
204 if (oc->flags & CF_SHUTW)
205 return;
206 oc->flags |= CF_SHUTW;
207 oc->wex = TICK_ETERNITY;
208 si_done_get(si);
209
210 if (tick_isset(si->hcto)) {
211 ic->rto = si->hcto;
212 ic->rex = tick_add(now_ms, ic->rto);
213 }
214
215 switch (si->state) {
216 case SI_ST_RDY:
217 case SI_ST_EST:
218 /* we have to shut before closing, otherwise some short messages
219 * may never leave the system, especially when there are remaining
220 * unread data in the socket input buffer, or when nolinger is set.
221 * However, if SI_FL_NOLINGER is explicitly set, we know there is
222 * no risk so we close both sides immediately.
223 */
224 if (!(si->flags & (SI_FL_ERR | SI_FL_NOLINGER)) &&
225 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
226 return;
227
228 /* fall through */
229 case SI_ST_CON:
230 case SI_ST_CER:
231 case SI_ST_QUE:
232 case SI_ST_TAR:
233 /* Note that none of these states may happen with applets */
234 si->state = SI_ST_DIS;
235 /* fall through */
236 default:
237 si->flags &= ~SI_FL_NOLINGER;
238 si_rx_shut_blk(si);
239 ic->flags |= CF_SHUTR;
240 ic->rex = TICK_ETERNITY;
241 si->exp = TICK_ETERNITY;
242 }
243
244 /* note that if the task exists, it must unregister itself once it runs */
245 if (!(si->flags & SI_FL_DONT_WAKE))
246 task_wakeup(si_task(si), TASK_WOKEN_IO);
247 }
248
249 /* default chk_rcv function for scheduled tasks */
stream_int_chk_rcv(struct stream_interface * si)250 static void stream_int_chk_rcv(struct stream_interface *si)
251 {
252 struct channel *ic = si_ic(si);
253
254 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
255 __FUNCTION__,
256 si, si->state, ic->flags, si_oc(si)->flags);
257
258 if (ic->pipe) {
259 /* stop reading */
260 si_rx_room_blk(si);
261 }
262 else {
263 /* (re)start reading */
264 tasklet_wakeup(si->wait_event.tasklet);
265 if (!(si->flags & SI_FL_DONT_WAKE))
266 task_wakeup(si_task(si), TASK_WOKEN_IO);
267 }
268 }
269
270 /* default chk_snd function for scheduled tasks */
stream_int_chk_snd(struct stream_interface * si)271 static void stream_int_chk_snd(struct stream_interface *si)
272 {
273 struct channel *oc = si_oc(si);
274
275 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
276 __FUNCTION__,
277 si, si->state, si_ic(si)->flags, oc->flags);
278
279 if (unlikely(si->state != SI_ST_EST || (oc->flags & CF_SHUTW)))
280 return;
281
282 if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
283 channel_is_empty(oc)) /* called with nothing to send ! */
284 return;
285
286 /* Otherwise there are remaining data to be sent in the buffer,
287 * so we tell the handler.
288 */
289 si->flags &= ~SI_FL_WAIT_DATA;
290 if (!tick_isset(oc->wex))
291 oc->wex = tick_add_ifset(now_ms, oc->wto);
292
293 if (!(si->flags & SI_FL_DONT_WAKE))
294 task_wakeup(si_task(si), TASK_WOKEN_IO);
295 }
296
297 /* Register an applet to handle a stream_interface as a new appctx. The SI will
298 * wake it up every time it is solicited. The appctx must be deleted by the task
299 * handler using si_release_endpoint(), possibly from within the function itself.
300 * It also pre-initializes the applet's context and returns it (or NULL in case
301 * it could not be allocated).
302 */
si_register_handler(struct stream_interface * si,struct applet * app)303 struct appctx *si_register_handler(struct stream_interface *si, struct applet *app)
304 {
305 struct appctx *appctx;
306
307 DPRINTF(stderr, "registering handler %p for si %p (was %p)\n", app, si, si_task(si));
308
309 appctx = si_alloc_appctx(si, app);
310 if (!appctx)
311 return NULL;
312
313 si_cant_get(si);
314 appctx_wakeup(appctx);
315 return si_appctx(si);
316 }
317
318 /* This callback is used to send a valid PROXY protocol line to a socket being
319 * established. It returns 0 if it fails in a fatal way or needs to poll to go
320 * further, otherwise it returns non-zero and removes itself from the connection's
321 * flags (the bit is provided in <flag> by the caller). It is designed to be
322 * called by the connection handler and relies on it to commit polling changes.
323 * Note that it can emit a PROXY line by relying on the other end's address
324 * when the connection is attached to a stream interface, or by resolving the
325 * local address otherwise (also called a LOCAL line).
326 */
conn_si_send_proxy(struct connection * conn,unsigned int flag)327 int conn_si_send_proxy(struct connection *conn, unsigned int flag)
328 {
329 if (!conn_ctrl_ready(conn))
330 goto out_error;
331
332 /* If we have a PROXY line to send, we'll use this to validate the
333 * connection, in which case the connection is validated only once
334 * we've sent the whole proxy line. Otherwise we use connect().
335 */
336 if (conn->send_proxy_ofs) {
337 const struct conn_stream *cs;
338 int ret;
339
340 /* If there is no mux attached to the connection, it means the
341 * connection context is a conn-stream.
342 */
343 cs = (conn->mux ? cs_get_first(conn) : conn->ctx);
344
345 /* The target server expects a PROXY line to be sent first.
346 * If the send_proxy_ofs is negative, it corresponds to the
347 * offset to start sending from then end of the proxy string
348 * (which is recomputed every time since it's constant). If
349 * it is positive, it means we have to send from the start.
350 * We can only send a "normal" PROXY line when the connection
351 * is attached to a stream interface. Otherwise we can only
352 * send a LOCAL line (eg: for use with health checks).
353 */
354
355 if (cs && cs->data_cb == &si_conn_cb) {
356 struct stream_interface *si = cs->data;
357 struct conn_stream *remote_cs = objt_cs(si_opposite(si)->end);
358 struct stream *strm = si_strm(si);
359
360 ret = make_proxy_line(trash.area, trash.size,
361 objt_server(conn->target),
362 remote_cs ? remote_cs->conn : NULL,
363 strm);
364 }
365 else {
366 /* The target server expects a LOCAL line to be sent first. Retrieving
367 * local or remote addresses may fail until the connection is established.
368 */
369 if (!conn_get_src(conn) || !conn_get_dst(conn))
370 goto out_wait;
371
372 ret = make_proxy_line(trash.area, trash.size,
373 objt_server(conn->target), conn,
374 NULL);
375 }
376
377 if (!ret)
378 goto out_error;
379
380 if (conn->send_proxy_ofs > 0)
381 conn->send_proxy_ofs = -ret; /* first call */
382
383 /* we have to send trash from (ret+sp for -sp bytes). If the
384 * data layer has a pending write, we'll also set MSG_MORE.
385 */
386 ret = conn_ctrl_send(conn,
387 trash.area + ret + conn->send_proxy_ofs,
388 -conn->send_proxy_ofs,
389 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
390
391 if (ret < 0)
392 goto out_error;
393
394 conn->send_proxy_ofs += ret; /* becomes zero once complete */
395 if (conn->send_proxy_ofs != 0)
396 goto out_wait;
397
398 /* OK we've sent the whole line, we're connected */
399 }
400
401 /* The connection is ready now, simply return and let the connection
402 * handler notify upper layers if needed.
403 */
404 conn->flags &= ~CO_FL_WAIT_L4_CONN;
405 conn->flags &= ~flag;
406 return 1;
407
408 out_error:
409 /* Write error on the file descriptor */
410 conn->flags |= CO_FL_ERROR;
411 return 0;
412
413 out_wait:
414 return 0;
415 }
416
417
418 /* This function is the equivalent to si_update() except that it's
419 * designed to be called from outside the stream handlers, typically the lower
420 * layers (applets, connections) after I/O completion. After updating the stream
421 * interface and timeouts, it will try to forward what can be forwarded, then to
422 * wake the associated task up if an important event requires special handling.
423 * It may update SI_FL_WAIT_DATA and/or SI_FL_RXBLK_ROOM, that the callers are
424 * encouraged to watch to take appropriate action.
425 * It should not be called from within the stream itself, si_update()
426 * is designed for this.
427 */
stream_int_notify(struct stream_interface * si)428 static void stream_int_notify(struct stream_interface *si)
429 {
430 struct channel *ic = si_ic(si);
431 struct channel *oc = si_oc(si);
432 struct stream_interface *sio = si_opposite(si);
433 struct task *task = si_task(si);
434
435 /* process consumer side */
436 if (channel_is_empty(oc)) {
437 struct connection *conn = objt_cs(si->end) ? __objt_cs(si->end)->conn : NULL;
438
439 if (((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW) &&
440 (si->state == SI_ST_EST) && (!conn || !(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS))))
441 si_shutw(si);
442 oc->wex = TICK_ETERNITY;
443 }
444
445 /* indicate that we may be waiting for data from the output channel or
446 * we're about to close and can't expect more data if SHUTW_NOW is there.
447 */
448 if (!(oc->flags & (CF_SHUTW|CF_SHUTW_NOW)))
449 si->flags |= SI_FL_WAIT_DATA;
450 else if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW)
451 si->flags &= ~SI_FL_WAIT_DATA;
452
453 /* update OC timeouts and wake the other side up if it's waiting for room */
454 if (oc->flags & CF_WRITE_ACTIVITY) {
455 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
456 !channel_is_empty(oc))
457 if (tick_isset(oc->wex))
458 oc->wex = tick_add_ifset(now_ms, oc->wto);
459
460 if (!(si->flags & SI_FL_INDEP_STR))
461 if (tick_isset(ic->rex))
462 ic->rex = tick_add_ifset(now_ms, ic->rto);
463 }
464
465 if (oc->flags & CF_DONT_READ)
466 si_rx_chan_blk(sio);
467 else
468 si_rx_chan_rdy(sio);
469
470 /* Notify the other side when we've injected data into the IC that
471 * needs to be forwarded. We can do fast-forwarding as soon as there
472 * are output data, but we avoid doing this if some of the data are
473 * not yet scheduled for being forwarded, because it is very likely
474 * that it will be done again immediately afterwards once the following
475 * data are parsed (eg: HTTP chunking). We only SI_FL_RXBLK_ROOM once
476 * we've emptied *some* of the output buffer, and not just when there
477 * is available room, because applets are often forced to stop before
478 * the buffer is full. We must not stop based on input data alone because
479 * an HTTP parser might need more data to complete the parsing.
480 */
481 if (!channel_is_empty(ic) &&
482 (sio->flags & SI_FL_WAIT_DATA) &&
483 (!(ic->flags & CF_EXPECT_MORE) || c_full(ic) || ci_data(ic) == 0 || ic->pipe)) {
484 int new_len, last_len;
485
486 last_len = co_data(ic);
487 if (ic->pipe)
488 last_len += ic->pipe->data;
489
490 si_chk_snd(sio);
491
492 new_len = co_data(ic);
493 if (ic->pipe)
494 new_len += ic->pipe->data;
495
496 /* check if the consumer has freed some space either in the
497 * buffer or in the pipe.
498 */
499 if (new_len < last_len)
500 si_rx_room_rdy(si);
501 }
502
503 if (!(ic->flags & CF_DONT_READ))
504 si_rx_chan_rdy(si);
505
506 si_chk_rcv(si);
507 si_chk_rcv(sio);
508
509 if (si_rx_blocked(si)) {
510 ic->rex = TICK_ETERNITY;
511 }
512 else if ((ic->flags & (CF_SHUTR|CF_READ_PARTIAL)) == CF_READ_PARTIAL) {
513 /* we must re-enable reading if si_chk_snd() has freed some space */
514 if (!(ic->flags & CF_READ_NOEXP) && tick_isset(ic->rex))
515 ic->rex = tick_add_ifset(now_ms, ic->rto);
516 }
517
518 /* wake the task up only when needed */
519 if (/* changes on the production side */
520 (ic->flags & (CF_READ_NULL|CF_READ_ERROR)) ||
521 !si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST) ||
522 (si->flags & SI_FL_ERR) ||
523 ((ic->flags & CF_READ_PARTIAL) &&
524 ((ic->flags & CF_EOI) || !ic->to_forward || sio->state != SI_ST_EST)) ||
525
526 /* changes on the consumption side */
527 (oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR)) ||
528 ((oc->flags & CF_WRITE_ACTIVITY) &&
529 ((oc->flags & CF_SHUTW) ||
530 (((oc->flags & CF_WAKE_WRITE) ||
531 !(oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW|CF_SHUTW))) &&
532 (sio->state != SI_ST_EST ||
533 (channel_is_empty(oc) && !oc->to_forward)))))) {
534 task_wakeup(task, TASK_WOKEN_IO);
535 }
536 else {
537 /* Update expiration date for the task and requeue it */
538 task->expire = tick_first((tick_is_expired(task->expire, now_ms) ? 0 : task->expire),
539 tick_first(tick_first(ic->rex, ic->wex),
540 tick_first(oc->rex, oc->wex)));
541
542 task->expire = tick_first(task->expire, ic->analyse_exp);
543 task->expire = tick_first(task->expire, oc->analyse_exp);
544
545 if (si->exp)
546 task->expire = tick_first(task->expire, si->exp);
547
548 if (sio->exp)
549 task->expire = tick_first(task->expire, sio->exp);
550
551 task_queue(task);
552 }
553 if (ic->flags & CF_READ_ACTIVITY)
554 ic->flags &= ~CF_READ_DONTWAIT;
555 }
556
557
558 /* Called by I/O handlers after completion.. It propagates
559 * connection flags to the stream interface, updates the stream (which may or
560 * may not take this opportunity to try to forward data), then update the
561 * connection's polling based on the channels and stream interface's final
562 * states. The function always returns 0.
563 */
si_cs_process(struct conn_stream * cs)564 static int si_cs_process(struct conn_stream *cs)
565 {
566 struct connection *conn = cs->conn;
567 struct stream_interface *si = cs->data;
568 struct channel *ic = si_ic(si);
569 struct channel *oc = si_oc(si);
570
571 /* If we have data to send, try it now */
572 if (!channel_is_empty(oc) && !(si->wait_event.events & SUB_RETRY_SEND))
573 si_cs_send(cs);
574
575 /* First step, report to the stream-int what was detected at the
576 * connection layer : errors and connection establishment.
577 * Only add SI_FL_ERR if we're connected, or we're attempting to
578 * connect, we may get there because we got woken up, but only run
579 * after process_stream() noticed there were an error, and decided
580 * to retry to connect, the connection may still have CO_FL_ERROR,
581 * and we don't want to add SI_FL_ERR back
582 *
583 * Note: This test is only required because si_cs_process is also the SI
584 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
585 * care of it.
586 */
587 if (si->state >= SI_ST_CON &&
588 (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR))
589 si->flags |= SI_FL_ERR;
590
591 /* If we had early data, and the handshake ended, then
592 * we can remove the flag, and attempt to wake the task up,
593 * in the event there's an analyser waiting for the end of
594 * the handshake.
595 */
596 if (!(conn->flags & (CO_FL_WAIT_XPRT | CO_FL_EARLY_SSL_HS)) &&
597 (cs->flags & CS_FL_WAIT_FOR_HS)) {
598 cs->flags &= ~CS_FL_WAIT_FOR_HS;
599 task_wakeup(si_task(si), TASK_WOKEN_MSG);
600 }
601
602 if (!si_state_in(si->state, SI_SB_EST|SI_SB_DIS|SI_SB_CLO) &&
603 (conn->flags & CO_FL_WAIT_XPRT) == 0) {
604 si->exp = TICK_ETERNITY;
605 oc->flags |= CF_WRITE_NULL;
606 if (si->state == SI_ST_CON)
607 si->state = SI_ST_RDY;
608 }
609
610 /* Report EOS on the channel if it was reached from the mux point of
611 * view.
612 *
613 * Note: This test is only required because si_cs_process is also the SI
614 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
615 * care of it.
616 */
617 if (cs->flags & CS_FL_EOS && !(ic->flags & CF_SHUTR)) {
618 /* we received a shutdown */
619 ic->flags |= CF_READ_NULL;
620 if (ic->flags & CF_AUTO_CLOSE)
621 channel_shutw_now(ic);
622 stream_int_read0(si);
623 }
624
625 /* Report EOI on the channel if it was reached from the mux point of
626 * view.
627 *
628 * Note: This test is only required because si_cs_process is also the SI
629 * wake callback. Otherwise si_cs_recv()/si_cs_send() already take
630 * care of it.
631 */
632 if ((cs->flags & CS_FL_EOI) && !(ic->flags & CF_EOI))
633 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
634
635 /* Second step : update the stream-int and channels, try to forward any
636 * pending data, then possibly wake the stream up based on the new
637 * stream-int status.
638 */
639 stream_int_notify(si);
640 stream_release_buffers(si_strm(si));
641 return 0;
642 }
643
644 /*
645 * This function is called to send buffer data to a stream socket.
646 * It calls the mux layer's snd_buf function. It relies on the
647 * caller to commit polling changes. The caller should check conn->flags
648 * for errors.
649 */
si_cs_send(struct conn_stream * cs)650 int si_cs_send(struct conn_stream *cs)
651 {
652 struct connection *conn = cs->conn;
653 struct stream_interface *si = cs->data;
654 struct channel *oc = si_oc(si);
655 int ret;
656 int did_send = 0;
657
658 if (conn->flags & CO_FL_ERROR || cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING)) {
659 /* We're probably there because the tasklet was woken up,
660 * but process_stream() ran before, detected there were an
661 * error and put the si back to SI_ST_TAR. There's still
662 * CO_FL_ERROR on the connection but we don't want to add
663 * SI_FL_ERR back, so give up
664 */
665 if (si->state < SI_ST_CON)
666 return 0;
667 si->flags |= SI_FL_ERR;
668 return 1;
669 }
670
671 /* We're already waiting to be able to send, give up */
672 if (si->wait_event.events & SUB_RETRY_SEND)
673 return 0;
674
675 /* we might have been called just after an asynchronous shutw */
676 if (oc->flags & CF_SHUTW)
677 return 1;
678
679 /* we must wait because the mux is not installed yet */
680 if (!conn->mux)
681 return 0;
682
683 if (oc->pipe && conn->xprt->snd_pipe && conn->mux->snd_pipe) {
684 ret = conn->mux->snd_pipe(cs, oc->pipe);
685 if (ret > 0)
686 did_send = 1;
687
688 if (!oc->pipe->data) {
689 put_pipe(oc->pipe);
690 oc->pipe = NULL;
691 }
692
693 if (oc->pipe)
694 goto end;
695 }
696
697 /* At this point, the pipe is empty, but we may still have data pending
698 * in the normal buffer.
699 */
700 if (co_data(oc)) {
701 /* when we're here, we already know that there is no spliced
702 * data left, and that there are sendable buffered data.
703 */
704
705 /* check if we want to inform the kernel that we're interested in
706 * sending more data after this call. We want this if :
707 * - we're about to close after this last send and want to merge
708 * the ongoing FIN with the last segment.
709 * - we know we can't send everything at once and must get back
710 * here because of unaligned data
711 * - there is still a finite amount of data to forward
712 * The test is arranged so that the most common case does only 2
713 * tests.
714 */
715 unsigned int send_flag = 0;
716
717 if ((!(oc->flags & (CF_NEVER_WAIT|CF_SEND_DONTWAIT)) &&
718 ((oc->to_forward && oc->to_forward != CHN_INFINITE_FORWARD) ||
719 (oc->flags & CF_EXPECT_MORE) ||
720 (IS_HTX_STRM(si_strm(si)) &&
721 (!(oc->flags & (CF_EOI|CF_SHUTR)) && htx_expect_more(htxbuf(&oc->buf)))))) ||
722 ((oc->flags & CF_ISRESP) &&
723 ((oc->flags & (CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW))))
724 send_flag |= CO_SFL_MSG_MORE;
725
726 if (oc->flags & CF_STREAMER)
727 send_flag |= CO_SFL_STREAMER;
728
729 if ((si->flags & SI_FL_L7_RETRY) && !b_data(&si->l7_buffer)) {
730 struct stream *s = si_strm(si);
731 /* If we want to be able to do L7 retries, copy
732 * the data we're about to send, so that we are able
733 * to resend them if needed
734 */
735 /* Try to allocate a buffer if we had none.
736 * If it fails, the next test will just
737 * disable the l7 retries by setting
738 * l7_conn_retries to 0.
739 */
740 if (!s->txn || (s->txn->req.msg_state != HTTP_MSG_DONE))
741 si->flags &= ~SI_FL_L7_RETRY;
742 else {
743 if (b_alloc(&si->l7_buffer) == NULL)
744 si->flags &= ~SI_FL_L7_RETRY;
745 else {
746 memcpy(b_orig(&si->l7_buffer),
747 b_orig(&oc->buf),
748 b_size(&oc->buf));
749 si->l7_buffer.head = co_data(oc);
750 b_add(&si->l7_buffer, co_data(oc));
751 }
752
753 }
754 }
755
756 ret = cs->conn->mux->snd_buf(cs, &oc->buf, co_data(oc), send_flag);
757 if (ret > 0) {
758 did_send = 1;
759 co_set_data(oc, co_data(oc) - ret);
760 c_realign_if_empty(oc);
761
762 if (!co_data(oc)) {
763 /* Always clear both flags once everything has been sent, they're one-shot */
764 oc->flags &= ~(CF_EXPECT_MORE | CF_SEND_DONTWAIT);
765 }
766 /* if some data remain in the buffer, it's only because the
767 * system buffers are full, we will try next time.
768 */
769 }
770 }
771
772 end:
773 if (did_send) {
774 oc->flags |= CF_WRITE_PARTIAL | CF_WROTE_DATA;
775 if (si->state == SI_ST_CON)
776 si->state = SI_ST_RDY;
777
778 si_rx_room_rdy(si_opposite(si));
779 }
780
781 if (conn->flags & CO_FL_ERROR || cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING)) {
782 si->flags |= SI_FL_ERR;
783 return 1;
784 }
785
786 /* We couldn't send all of our data, let the mux know we'd like to send more */
787 if (!channel_is_empty(oc))
788 conn->mux->subscribe(cs, SUB_RETRY_SEND, &si->wait_event);
789 return did_send;
790 }
791
792 /* This is the ->process() function for any stream-interface's wait_event task.
793 * It's assigned during the stream-interface's initialization, for any type of
794 * stream interface. Thus it is always safe to perform a tasklet_wakeup() on a
795 * stream interface, as the presence of the CS is checked there.
796 */
si_cs_io_cb(struct task * t,void * ctx,unsigned int state)797 struct task *si_cs_io_cb(struct task *t, void *ctx, unsigned int state)
798 {
799 struct stream_interface *si = ctx;
800 struct conn_stream *cs = objt_cs(si->end);
801 int ret = 0;
802
803 if (!cs)
804 return t;
805
806 if (!(si->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
807 ret = si_cs_send(cs);
808 if (!(si->wait_event.events & SUB_RETRY_RECV))
809 ret |= si_cs_recv(cs);
810 if (ret != 0)
811 si_cs_process(cs);
812
813 stream_release_buffers(si_strm(si));
814 return t;
815 }
816
817 /* This function is designed to be called from within the stream handler to
818 * update the input channel's expiration timer and the stream interface's
819 * Rx flags based on the channel's flags. It needs to be called only once
820 * after the channel's flags have settled down, and before they are cleared,
821 * though it doesn't harm to call it as often as desired (it just slightly
822 * hurts performance). It must not be called from outside of the stream
823 * handler, as what it does will be used to compute the stream task's
824 * expiration.
825 */
si_update_rx(struct stream_interface * si)826 void si_update_rx(struct stream_interface *si)
827 {
828 struct channel *ic = si_ic(si);
829
830 if (ic->flags & CF_SHUTR) {
831 si_rx_shut_blk(si);
832 return;
833 }
834
835 /* Read not closed, update FD status and timeout for reads */
836 if (ic->flags & CF_DONT_READ)
837 si_rx_chan_blk(si);
838 else
839 si_rx_chan_rdy(si);
840
841 if (!channel_is_empty(ic) || !channel_may_recv(ic)) {
842 /* stop reading, imposed by channel's policy or contents */
843 si_rx_room_blk(si);
844 }
845 else {
846 /* (re)start reading and update timeout. Note: we don't recompute the timeout
847 * every time we get here, otherwise it would risk never to expire. We only
848 * update it if is was not yet set. The stream socket handler will already
849 * have updated it if there has been a completed I/O.
850 */
851 si_rx_room_rdy(si);
852 }
853 if (si->flags & SI_FL_RXBLK_ANY & ~SI_FL_RX_WAIT_EP)
854 ic->rex = TICK_ETERNITY;
855 else if (!(ic->flags & CF_READ_NOEXP) && !tick_isset(ic->rex))
856 ic->rex = tick_add_ifset(now_ms, ic->rto);
857
858 si_chk_rcv(si);
859 }
860
861 /* This function is designed to be called from within the stream handler to
862 * update the output channel's expiration timer and the stream interface's
863 * Tx flags based on the channel's flags. It needs to be called only once
864 * after the channel's flags have settled down, and before they are cleared,
865 * though it doesn't harm to call it as often as desired (it just slightly
866 * hurts performance). It must not be called from outside of the stream
867 * handler, as what it does will be used to compute the stream task's
868 * expiration.
869 */
si_update_tx(struct stream_interface * si)870 void si_update_tx(struct stream_interface *si)
871 {
872 struct channel *oc = si_oc(si);
873 struct channel *ic = si_ic(si);
874
875 if (oc->flags & CF_SHUTW)
876 return;
877
878 /* Write not closed, update FD status and timeout for writes */
879 if (channel_is_empty(oc)) {
880 /* stop writing */
881 if (!(si->flags & SI_FL_WAIT_DATA)) {
882 if ((oc->flags & CF_SHUTW_NOW) == 0)
883 si->flags |= SI_FL_WAIT_DATA;
884 oc->wex = TICK_ETERNITY;
885 }
886 return;
887 }
888
889 /* (re)start writing and update timeout. Note: we don't recompute the timeout
890 * every time we get here, otherwise it would risk never to expire. We only
891 * update it if is was not yet set. The stream socket handler will already
892 * have updated it if there has been a completed I/O.
893 */
894 si->flags &= ~SI_FL_WAIT_DATA;
895 if (!tick_isset(oc->wex)) {
896 oc->wex = tick_add_ifset(now_ms, oc->wto);
897 if (tick_isset(ic->rex) && !(si->flags & SI_FL_INDEP_STR)) {
898 /* Note: depending on the protocol, we don't know if we're waiting
899 * for incoming data or not. So in order to prevent the socket from
900 * expiring read timeouts during writes, we refresh the read timeout,
901 * except if it was already infinite or if we have explicitly setup
902 * independent streams.
903 */
904 ic->rex = tick_add_ifset(now_ms, ic->rto);
905 }
906 }
907 }
908
909 /* perform a synchronous send() for the stream interface. The CF_WRITE_NULL and
910 * CF_WRITE_PARTIAL flags are cleared prior to the attempt, and will possibly
911 * be updated in case of success.
912 */
si_sync_send(struct stream_interface * si)913 void si_sync_send(struct stream_interface *si)
914 {
915 struct channel *oc = si_oc(si);
916 struct conn_stream *cs;
917
918 oc->flags &= ~(CF_WRITE_NULL|CF_WRITE_PARTIAL);
919
920 if (oc->flags & CF_SHUTW)
921 return;
922
923 if (channel_is_empty(oc))
924 return;
925
926 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
927 return;
928
929 cs = objt_cs(si->end);
930 if (!cs || !cs->conn->mux)
931 return;
932
933 si_cs_send(cs);
934 }
935
936 /* Updates at once the channel flags, and timers of both stream interfaces of a
937 * same stream, to complete the work after the analysers, then updates the data
938 * layer below. This will ensure that any synchronous update performed at the
939 * data layer will be reflected in the channel flags and/or stream-interface.
940 * Note that this does not change the stream interface's current state, though
941 * it updates the previous state to the current one.
942 */
si_update_both(struct stream_interface * si_f,struct stream_interface * si_b)943 void si_update_both(struct stream_interface *si_f, struct stream_interface *si_b)
944 {
945 struct channel *req = si_ic(si_f);
946 struct channel *res = si_oc(si_f);
947
948 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
949 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_READ_ATTACHED|CF_WRITE_NULL|CF_WRITE_PARTIAL);
950
951 si_f->prev_state = si_f->state;
952 si_b->prev_state = si_b->state;
953
954 /* let's recompute both sides states */
955 if (si_state_in(si_f->state, SI_SB_RDY|SI_SB_EST))
956 si_update(si_f);
957
958 if (si_state_in(si_b->state, SI_SB_RDY|SI_SB_EST))
959 si_update(si_b);
960
961 /* stream ints are processed outside of process_stream() and must be
962 * handled at the latest moment.
963 */
964 if (obj_type(si_f->end) == OBJ_TYPE_APPCTX &&
965 ((si_rx_endp_ready(si_f) && !si_rx_blocked(si_f)) ||
966 (si_tx_endp_ready(si_f) && !si_tx_blocked(si_f))))
967 appctx_wakeup(si_appctx(si_f));
968
969 if (obj_type(si_b->end) == OBJ_TYPE_APPCTX &&
970 ((si_rx_endp_ready(si_b) && !si_rx_blocked(si_b)) ||
971 (si_tx_endp_ready(si_b) && !si_tx_blocked(si_b))))
972 appctx_wakeup(si_appctx(si_b));
973 }
974
975 /*
976 * This function performs a shutdown-read on a stream interface attached to
977 * a connection in a connected or init state (it does nothing for other
978 * states). It either shuts the read side or marks itself as closed. The buffer
979 * flags are updated to reflect the new state. If the stream interface has
980 * SI_FL_NOHALF, we also forward the close to the write side. If a control
981 * layer is defined, then it is supposed to be a socket layer and file
982 * descriptors are then shutdown or closed accordingly. The function
983 * automatically disables polling if needed.
984 */
stream_int_shutr_conn(struct stream_interface * si)985 static void stream_int_shutr_conn(struct stream_interface *si)
986 {
987 struct conn_stream *cs = __objt_cs(si->end);
988 struct channel *ic = si_ic(si);
989
990 si_rx_shut_blk(si);
991 if (ic->flags & CF_SHUTR)
992 return;
993 ic->flags |= CF_SHUTR;
994 ic->rex = TICK_ETERNITY;
995
996 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
997 return;
998
999 if (si->flags & SI_FL_KILL_CONN)
1000 cs->flags |= CS_FL_KILL_CONN;
1001
1002 if (si_oc(si)->flags & CF_SHUTW) {
1003 cs_close(cs);
1004 si->state = SI_ST_DIS;
1005 si->exp = TICK_ETERNITY;
1006 }
1007 else if (si->flags & SI_FL_NOHALF) {
1008 /* we want to immediately forward this close to the write side */
1009 return stream_int_shutw_conn(si);
1010 }
1011 }
1012
1013 /*
1014 * This function performs a shutdown-write on a stream interface attached to
1015 * a connection in a connected or init state (it does nothing for other
1016 * states). It either shuts the write side or marks itself as closed. The
1017 * buffer flags are updated to reflect the new state. It does also close
1018 * everything if the SI was marked as being in error state. If there is a
1019 * data-layer shutdown, it is called.
1020 */
stream_int_shutw_conn(struct stream_interface * si)1021 static void stream_int_shutw_conn(struct stream_interface *si)
1022 {
1023 struct conn_stream *cs = __objt_cs(si->end);
1024 struct channel *ic = si_ic(si);
1025 struct channel *oc = si_oc(si);
1026
1027 oc->flags &= ~CF_SHUTW_NOW;
1028 if (oc->flags & CF_SHUTW)
1029 return;
1030 oc->flags |= CF_SHUTW;
1031 oc->wex = TICK_ETERNITY;
1032 si_done_get(si);
1033
1034 if (tick_isset(si->hcto)) {
1035 ic->rto = si->hcto;
1036 ic->rex = tick_add(now_ms, ic->rto);
1037 }
1038
1039 switch (si->state) {
1040 case SI_ST_RDY:
1041 case SI_ST_EST:
1042 /* we have to shut before closing, otherwise some short messages
1043 * may never leave the system, especially when there are remaining
1044 * unread data in the socket input buffer, or when nolinger is set.
1045 * However, if SI_FL_NOLINGER is explicitly set, we know there is
1046 * no risk so we close both sides immediately.
1047 */
1048 if (si->flags & SI_FL_KILL_CONN)
1049 cs->flags |= CS_FL_KILL_CONN;
1050
1051 if (si->flags & SI_FL_ERR) {
1052 /* quick close, the socket is already shut anyway */
1053 }
1054 else if (si->flags & SI_FL_NOLINGER) {
1055 /* unclean data-layer shutdown, typically an aborted request
1056 * or a forwarded shutdown from a client to a server due to
1057 * option abortonclose. No need for the TLS layer to try to
1058 * emit a shutdown message.
1059 */
1060 cs_shutw(cs, CS_SHW_SILENT);
1061 }
1062 else {
1063 /* clean data-layer shutdown. This only happens on the
1064 * frontend side, or on the backend side when forwarding
1065 * a client close in TCP mode or in HTTP TUNNEL mode
1066 * while option abortonclose is set. We want the TLS
1067 * layer to try to signal it to the peer before we close.
1068 */
1069 cs_shutw(cs, CS_SHW_NORMAL);
1070
1071 if (!(ic->flags & (CF_SHUTR|CF_DONT_READ)))
1072 return;
1073 }
1074
1075 /* fall through */
1076 case SI_ST_CON:
1077 /* we may have to close a pending connection, and mark the
1078 * response buffer as shutr
1079 */
1080 if (si->flags & SI_FL_KILL_CONN)
1081 cs->flags |= CS_FL_KILL_CONN;
1082 cs_close(cs);
1083 /* fall through */
1084 case SI_ST_CER:
1085 case SI_ST_QUE:
1086 case SI_ST_TAR:
1087 si->state = SI_ST_DIS;
1088 /* fall through */
1089 default:
1090 si->flags &= ~SI_FL_NOLINGER;
1091 si_rx_shut_blk(si);
1092 ic->flags |= CF_SHUTR;
1093 ic->rex = TICK_ETERNITY;
1094 si->exp = TICK_ETERNITY;
1095 }
1096 }
1097
1098 /* This function is used for inter-stream-interface calls. It is called by the
1099 * consumer to inform the producer side that it may be interested in checking
1100 * for free space in the buffer. Note that it intentionally does not update
1101 * timeouts, so that we can still check them later at wake-up. This function is
1102 * dedicated to connection-based stream interfaces.
1103 */
stream_int_chk_rcv_conn(struct stream_interface * si)1104 static void stream_int_chk_rcv_conn(struct stream_interface *si)
1105 {
1106 /* (re)start reading */
1107 if (si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
1108 tasklet_wakeup(si->wait_event.tasklet);
1109 }
1110
1111
1112 /* This function is used for inter-stream-interface calls. It is called by the
1113 * producer to inform the consumer side that it may be interested in checking
1114 * for data in the buffer. Note that it intentionally does not update timeouts,
1115 * so that we can still check them later at wake-up.
1116 */
stream_int_chk_snd_conn(struct stream_interface * si)1117 static void stream_int_chk_snd_conn(struct stream_interface *si)
1118 {
1119 struct channel *oc = si_oc(si);
1120 struct conn_stream *cs = __objt_cs(si->end);
1121
1122 if (unlikely(!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST) ||
1123 (oc->flags & CF_SHUTW)))
1124 return;
1125
1126 if (unlikely(channel_is_empty(oc))) /* called with nothing to send ! */
1127 return;
1128
1129 if (!oc->pipe && /* spliced data wants to be forwarded ASAP */
1130 !(si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */
1131 return;
1132
1133 if (!(si->wait_event.events & SUB_RETRY_SEND) && !channel_is_empty(si_oc(si)))
1134 si_cs_send(cs);
1135
1136 if (cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING) || cs->conn->flags & CO_FL_ERROR) {
1137 /* Write error on the file descriptor */
1138 if (si->state >= SI_ST_CON)
1139 si->flags |= SI_FL_ERR;
1140 goto out_wakeup;
1141 }
1142
1143 /* OK, so now we know that some data might have been sent, and that we may
1144 * have to poll first. We have to do that too if the buffer is not empty.
1145 */
1146 if (channel_is_empty(oc)) {
1147 /* the connection is established but we can't write. Either the
1148 * buffer is empty, or we just refrain from sending because the
1149 * ->o limit was reached. Maybe we just wrote the last
1150 * chunk and need to close.
1151 */
1152 if (((oc->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) ==
1153 (CF_AUTO_CLOSE|CF_SHUTW_NOW)) &&
1154 si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
1155 si_shutw(si);
1156 goto out_wakeup;
1157 }
1158
1159 if ((oc->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0)
1160 si->flags |= SI_FL_WAIT_DATA;
1161 oc->wex = TICK_ETERNITY;
1162 }
1163 else {
1164 /* Otherwise there are remaining data to be sent in the buffer,
1165 * which means we have to poll before doing so.
1166 */
1167 si->flags &= ~SI_FL_WAIT_DATA;
1168 if (!tick_isset(oc->wex))
1169 oc->wex = tick_add_ifset(now_ms, oc->wto);
1170 }
1171
1172 if (likely(oc->flags & CF_WRITE_ACTIVITY)) {
1173 struct channel *ic = si_ic(si);
1174
1175 /* update timeout if we have written something */
1176 if ((oc->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL &&
1177 !channel_is_empty(oc))
1178 oc->wex = tick_add_ifset(now_ms, oc->wto);
1179
1180 if (tick_isset(ic->rex) && !(si->flags & SI_FL_INDEP_STR)) {
1181 /* Note: to prevent the client from expiring read timeouts
1182 * during writes, we refresh it. We only do this if the
1183 * interface is not configured for "independent streams",
1184 * because for some applications it's better not to do this,
1185 * for instance when continuously exchanging small amounts
1186 * of data which can full the socket buffers long before a
1187 * write timeout is detected.
1188 */
1189 ic->rex = tick_add_ifset(now_ms, ic->rto);
1190 }
1191 }
1192
1193 /* in case of special condition (error, shutdown, end of write...), we
1194 * have to notify the task.
1195 */
1196 if (likely((oc->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) ||
1197 ((oc->flags & CF_WAKE_WRITE) &&
1198 ((channel_is_empty(oc) && !oc->to_forward) ||
1199 !si_state_in(si->state, SI_SB_EST))))) {
1200 out_wakeup:
1201 if (!(si->flags & SI_FL_DONT_WAKE))
1202 task_wakeup(si_task(si), TASK_WOKEN_IO);
1203 }
1204 }
1205
1206 /*
1207 * This is the callback which is called by the connection layer to receive data
1208 * into the buffer from the connection. It iterates over the mux layer's
1209 * rcv_buf function.
1210 */
si_cs_recv(struct conn_stream * cs)1211 int si_cs_recv(struct conn_stream *cs)
1212 {
1213 struct connection *conn = cs->conn;
1214 struct stream_interface *si = cs->data;
1215 struct channel *ic = si_ic(si);
1216 int ret, max, cur_read = 0;
1217 int read_poll = MAX_READ_POLL_LOOPS;
1218 int flags = 0;
1219
1220 /* If not established yet, do nothing. */
1221 if (si->state != SI_ST_EST)
1222 return 0;
1223
1224 /* If another call to si_cs_recv() failed, and we subscribed to
1225 * recv events already, give up now.
1226 */
1227 if (si->wait_event.events & SUB_RETRY_RECV)
1228 return 0;
1229
1230 /* maybe we were called immediately after an asynchronous shutr */
1231 if (ic->flags & CF_SHUTR)
1232 return 1;
1233
1234 /* we must wait because the mux is not installed yet */
1235 if (!conn->mux)
1236 return 0;
1237
1238 /* stop here if we reached the end of data */
1239 if (cs->flags & CS_FL_EOS)
1240 goto end_recv;
1241
1242 /* stop immediately on errors. Note that we DON'T want to stop on
1243 * POLL_ERR, as the poller might report a write error while there
1244 * are still data available in the recv buffer. This typically
1245 * happens when we send too large a request to a backend server
1246 * which rejects it before reading it all.
1247 */
1248 if (!(cs->flags & CS_FL_RCV_MORE)) {
1249 if (!conn_xprt_ready(conn))
1250 return 0;
1251 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR)
1252 goto end_recv;
1253 }
1254
1255 /* prepare to detect if the mux needs more room */
1256 cs->flags &= ~CS_FL_WANT_ROOM;
1257
1258 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) && !co_data(ic) &&
1259 global.tune.idle_timer &&
1260 (unsigned short)(now_ms - ic->last_read) >= global.tune.idle_timer) {
1261 /* The buffer was empty and nothing was transferred for more
1262 * than one second. This was caused by a pause and not by
1263 * congestion. Reset any streaming mode to reduce latency.
1264 */
1265 ic->xfer_small = 0;
1266 ic->xfer_large = 0;
1267 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1268 }
1269
1270 /* First, let's see if we may splice data across the channel without
1271 * using a buffer.
1272 */
1273 if (cs->flags & CS_FL_MAY_SPLICE &&
1274 (ic->pipe || ic->to_forward >= MIN_SPLICE_FORWARD) &&
1275 ic->flags & CF_KERN_SPLICING) {
1276 if (c_data(ic)) {
1277 /* We're embarrassed, there are already data pending in
1278 * the buffer and we don't want to have them at two
1279 * locations at a time. Let's indicate we need some
1280 * place and ask the consumer to hurry.
1281 */
1282 flags |= CO_RFL_BUF_FLUSH;
1283 goto abort_splice;
1284 }
1285
1286 if (unlikely(ic->pipe == NULL)) {
1287 if (pipes_used >= global.maxpipes || !(ic->pipe = get_pipe())) {
1288 ic->flags &= ~CF_KERN_SPLICING;
1289 goto abort_splice;
1290 }
1291 }
1292
1293 ret = conn->mux->rcv_pipe(cs, ic->pipe, ic->to_forward);
1294 if (ret < 0) {
1295 /* splice not supported on this end, let's disable it */
1296 ic->flags &= ~CF_KERN_SPLICING;
1297 goto abort_splice;
1298 }
1299
1300 if (ret > 0) {
1301 if (ic->to_forward != CHN_INFINITE_FORWARD)
1302 ic->to_forward -= ret;
1303 ic->total += ret;
1304 cur_read += ret;
1305 ic->flags |= CF_READ_PARTIAL;
1306 }
1307
1308 if (conn->flags & CO_FL_ERROR || cs->flags & (CS_FL_EOS|CS_FL_ERROR))
1309 goto end_recv;
1310
1311 if (conn->flags & CO_FL_WAIT_ROOM) {
1312 /* the pipe is full or we have read enough data that it
1313 * could soon be full. Let's stop before needing to poll.
1314 */
1315 si_rx_room_blk(si);
1316 goto done_recv;
1317 }
1318
1319 /* splice not possible (anymore), let's go on on standard copy */
1320 }
1321
1322 abort_splice:
1323 if (ic->pipe && unlikely(!ic->pipe->data)) {
1324 put_pipe(ic->pipe);
1325 ic->pipe = NULL;
1326 }
1327
1328 if (ic->pipe && ic->to_forward && !(flags & CO_RFL_BUF_FLUSH) && cs->flags & CS_FL_MAY_SPLICE) {
1329 /* don't break splicing by reading, but still call rcv_buf()
1330 * to pass the flag.
1331 */
1332 goto done_recv;
1333 }
1334
1335 /* now we'll need a input buffer for the stream */
1336 if (!si_alloc_ibuf(si, &(si_strm(si)->buffer_wait)))
1337 goto end_recv;
1338
1339 /* For an HTX stream, if the buffer is stuck (no output data with some
1340 * input data) and if the HTX message is fragmented or if its free space
1341 * wraps, we force an HTX deframentation. It is a way to have a
1342 * contiguous free space nad to let the mux to copy as much data as
1343 * possible.
1344 *
1345 * NOTE: A possible optim may be to let the mux decides if defrag is
1346 * required or not, depending on amount of data to be xferred.
1347 */
1348 if (IS_HTX_STRM(si_strm(si)) && !co_data(ic)) {
1349 struct htx *htx = htxbuf(&ic->buf);
1350
1351 if (htx_is_not_empty(htx) && ((htx->flags & HTX_FL_FRAGMENTED) || htx_space_wraps(htx)))
1352 htx_defrag(htxbuf(&ic->buf), NULL, 0);
1353 }
1354
1355 /* Instruct the mux it must subscribed for read events */
1356 flags |= ((!conn_is_back(conn) && (si_strm(si)->be->options & PR_O_ABRT_CLOSE)) ? CO_RFL_KEEP_RECV : 0);
1357
1358 /* Important note : if we're called with POLL_IN|POLL_HUP, it means the read polling
1359 * was enabled, which implies that the recv buffer was not full. So we have a guarantee
1360 * that if such an event is not handled above in splice, it will be handled here by
1361 * recv().
1362 */
1363 while ((cs->flags & CS_FL_RCV_MORE) ||
1364 (!(conn->flags & (CO_FL_ERROR | CO_FL_HANDSHAKE)) &&
1365 (!(cs->flags & (CS_FL_ERROR|CS_FL_EOS))) && !(ic->flags & CF_SHUTR))) {
1366 int cur_flags = flags;
1367
1368 /* Compute transient CO_RFL_* flags */
1369 if (co_data(ic)) {
1370 cur_flags |= (CO_RFL_BUF_WET | CO_RFL_BUF_NOT_STUCK);
1371 }
1372
1373 /* <max> may be null. This is the mux responsibility to set
1374 * CS_FL_RCV_MORE on the CS if more space is needed.
1375 */
1376 max = channel_recv_max(ic);
1377 ret = cs->conn->mux->rcv_buf(cs, &ic->buf, max, cur_flags);
1378
1379 if (cs->flags & CS_FL_WANT_ROOM) {
1380 si_rx_room_blk(si);
1381 /* Add READ_PARTIAL because some data are pending but
1382 * cannot be xferred to the channel
1383 */
1384 ic->flags |= CF_READ_PARTIAL;
1385 }
1386
1387 if (ret <= 0) {
1388 /* if we refrained from reading because we asked for a
1389 * flush to satisfy rcv_pipe(), we must not subscribe
1390 * and instead report that there's not enough room
1391 * here to proceed.
1392 */
1393 if (flags & CO_RFL_BUF_FLUSH)
1394 si_rx_room_blk(si);
1395 break;
1396 }
1397
1398 cur_read += ret;
1399
1400 /* if we're allowed to directly forward data, we must update ->o */
1401 if (ic->to_forward && !(ic->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
1402 unsigned long fwd = ret;
1403 if (ic->to_forward != CHN_INFINITE_FORWARD) {
1404 if (fwd > ic->to_forward)
1405 fwd = ic->to_forward;
1406 ic->to_forward -= fwd;
1407 }
1408 c_adv(ic, fwd);
1409 }
1410
1411 ic->flags |= CF_READ_PARTIAL;
1412 ic->total += ret;
1413
1414 /* End-of-input reached, we can leave. In this case, it is
1415 * important to break the loop to not block the SI because of
1416 * the channel's policies.This way, we are still able to receive
1417 * shutdowns.
1418 */
1419 if (cs->flags & CS_FL_EOI)
1420 break;
1421
1422 if ((ic->flags & CF_READ_DONTWAIT) || --read_poll <= 0) {
1423 /* we're stopped by the channel's policy */
1424 si_rx_chan_blk(si);
1425 break;
1426 }
1427
1428 /* if too many bytes were missing from last read, it means that
1429 * it's pointless trying to read again because the system does
1430 * not have them in buffers.
1431 */
1432 if (ret < max) {
1433 /* if a streamer has read few data, it may be because we
1434 * have exhausted system buffers. It's not worth trying
1435 * again.
1436 */
1437 if (ic->flags & CF_STREAMER) {
1438 /* we're stopped by the channel's policy */
1439 si_rx_chan_blk(si);
1440 break;
1441 }
1442
1443 /* if we read a large block smaller than what we requested,
1444 * it's almost certain we'll never get anything more.
1445 */
1446 if (ret >= global.tune.recv_enough) {
1447 /* we're stopped by the channel's policy */
1448 si_rx_chan_blk(si);
1449 break;
1450 }
1451 }
1452
1453 /* if we are waiting for more space, don't try to read more data
1454 * right now.
1455 */
1456 if (si_rx_blocked(si))
1457 break;
1458 } /* while !flags */
1459
1460 done_recv:
1461 if (cur_read) {
1462 if ((ic->flags & (CF_STREAMER | CF_STREAMER_FAST)) &&
1463 (cur_read <= ic->buf.size / 2)) {
1464 ic->xfer_large = 0;
1465 ic->xfer_small++;
1466 if (ic->xfer_small >= 3) {
1467 /* we have read less than half of the buffer in
1468 * one pass, and this happened at least 3 times.
1469 * This is definitely not a streamer.
1470 */
1471 ic->flags &= ~(CF_STREAMER | CF_STREAMER_FAST);
1472 }
1473 else if (ic->xfer_small >= 2) {
1474 /* if the buffer has been at least half full twice,
1475 * we receive faster than we send, so at least it
1476 * is not a "fast streamer".
1477 */
1478 ic->flags &= ~CF_STREAMER_FAST;
1479 }
1480 }
1481 else if (!(ic->flags & CF_STREAMER_FAST) &&
1482 (cur_read >= ic->buf.size - global.tune.maxrewrite)) {
1483 /* we read a full buffer at once */
1484 ic->xfer_small = 0;
1485 ic->xfer_large++;
1486 if (ic->xfer_large >= 3) {
1487 /* we call this buffer a fast streamer if it manages
1488 * to be filled in one call 3 consecutive times.
1489 */
1490 ic->flags |= (CF_STREAMER | CF_STREAMER_FAST);
1491 }
1492 }
1493 else {
1494 ic->xfer_small = 0;
1495 ic->xfer_large = 0;
1496 }
1497 ic->last_read = now_ms;
1498 }
1499
1500 end_recv:
1501 ret = (cur_read != 0);
1502
1503 /* Report EOI on the channel if it was reached from the mux point of
1504 * view. */
1505 if ((cs->flags & CS_FL_EOI) && !(ic->flags & CF_EOI)) {
1506 ic->flags |= (CF_EOI|CF_READ_PARTIAL);
1507 ret = 1;
1508 }
1509
1510 if (conn->flags & CO_FL_ERROR || cs->flags & CS_FL_ERROR) {
1511 cs->flags |= CS_FL_ERROR;
1512 si->flags |= SI_FL_ERR;
1513 ret = 1;
1514 }
1515 else if (cs->flags & CS_FL_EOS) {
1516 /* we received a shutdown */
1517 ic->flags |= CF_READ_NULL;
1518 if (ic->flags & CF_AUTO_CLOSE)
1519 channel_shutw_now(ic);
1520 stream_int_read0(si);
1521 ret = 1;
1522 }
1523 else if (!si_rx_blocked(si)) {
1524 /* Subscribe to receive events if we're blocking on I/O */
1525 conn->mux->subscribe(cs, SUB_RETRY_RECV, &si->wait_event);
1526 si_rx_endp_done(si);
1527 } else {
1528 si_rx_endp_more(si);
1529 ret = 1;
1530 }
1531 return ret;
1532 }
1533
1534 /*
1535 * This function propagates a null read received on a socket-based connection.
1536 * It updates the stream interface. If the stream interface has SI_FL_NOHALF,
1537 * the close is also forwarded to the write side as an abort.
1538 */
stream_int_read0(struct stream_interface * si)1539 static void stream_int_read0(struct stream_interface *si)
1540 {
1541 struct conn_stream *cs = __objt_cs(si->end);
1542 struct channel *ic = si_ic(si);
1543 struct channel *oc = si_oc(si);
1544
1545 si_rx_shut_blk(si);
1546 if (ic->flags & CF_SHUTR)
1547 return;
1548 ic->flags |= CF_SHUTR;
1549 ic->rex = TICK_ETERNITY;
1550
1551 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
1552 return;
1553
1554 if (oc->flags & CF_SHUTW)
1555 goto do_close;
1556
1557 if (si->flags & SI_FL_NOHALF) {
1558 /* we want to immediately forward this close to the write side */
1559 /* force flag on ssl to keep stream in cache */
1560 cs_shutw(cs, CS_SHW_SILENT);
1561 goto do_close;
1562 }
1563
1564 /* otherwise that's just a normal read shutdown */
1565 return;
1566
1567 do_close:
1568 /* OK we completely close the socket here just as if we went through si_shut[rw]() */
1569 cs_close(cs);
1570
1571 oc->flags &= ~CF_SHUTW_NOW;
1572 oc->flags |= CF_SHUTW;
1573 oc->wex = TICK_ETERNITY;
1574
1575 si_done_get(si);
1576
1577 si->state = SI_ST_DIS;
1578 si->exp = TICK_ETERNITY;
1579 return;
1580 }
1581
1582 /* Callback to be used by applet handlers upon completion. It updates the stream
1583 * (which may or may not take this opportunity to try to forward data), then
1584 * may re-enable the applet's based on the channels and stream interface's final
1585 * states.
1586 */
si_applet_wake_cb(struct stream_interface * si)1587 void si_applet_wake_cb(struct stream_interface *si)
1588 {
1589 struct channel *ic = si_ic(si);
1590
1591 /* If the applet wants to write and the channel is closed, it's a
1592 * broken pipe and it must be reported.
1593 */
1594 if (!(si->flags & SI_FL_RX_WAIT_EP) && (ic->flags & CF_SHUTR))
1595 si->flags |= SI_FL_ERR;
1596
1597 /* automatically mark the applet having data available if it reported
1598 * begin blocked by the channel.
1599 */
1600 if (si_rx_blocked(si))
1601 si_rx_endp_more(si);
1602
1603 /* update the stream-int, channels, and possibly wake the stream up */
1604 stream_int_notify(si);
1605 stream_release_buffers(si_strm(si));
1606
1607 /* stream_int_notify may have passed through chk_snd and released some
1608 * RXBLK flags. Process_stream will consider those flags to wake up the
1609 * appctx but in the case the task is not in runqueue we may have to
1610 * wakeup the appctx immediately.
1611 */
1612 if ((si_rx_endp_ready(si) && !si_rx_blocked(si)) ||
1613 (si_tx_endp_ready(si) && !si_tx_blocked(si)))
1614 appctx_wakeup(si_appctx(si));
1615 }
1616
1617 /*
1618 * This function performs a shutdown-read on a stream interface attached to an
1619 * applet in a connected or init state (it does nothing for other states). It
1620 * either shuts the read side or marks itself as closed. The buffer flags are
1621 * updated to reflect the new state. If the stream interface has SI_FL_NOHALF,
1622 * we also forward the close to the write side. The owner task is woken up if
1623 * it exists.
1624 */
stream_int_shutr_applet(struct stream_interface * si)1625 static void stream_int_shutr_applet(struct stream_interface *si)
1626 {
1627 struct channel *ic = si_ic(si);
1628
1629 si_rx_shut_blk(si);
1630 if (ic->flags & CF_SHUTR)
1631 return;
1632 ic->flags |= CF_SHUTR;
1633 ic->rex = TICK_ETERNITY;
1634
1635 /* Note: on shutr, we don't call the applet */
1636
1637 if (!si_state_in(si->state, SI_SB_CON|SI_SB_RDY|SI_SB_EST))
1638 return;
1639
1640 if (si_oc(si)->flags & CF_SHUTW) {
1641 si_applet_release(si);
1642 si->state = SI_ST_DIS;
1643 si->exp = TICK_ETERNITY;
1644 }
1645 else if (si->flags & SI_FL_NOHALF) {
1646 /* we want to immediately forward this close to the write side */
1647 return stream_int_shutw_applet(si);
1648 }
1649 }
1650
1651 /*
1652 * This function performs a shutdown-write on a stream interface attached to an
1653 * applet in a connected or init state (it does nothing for other states). It
1654 * either shuts the write side or marks itself as closed. The buffer flags are
1655 * updated to reflect the new state. It does also close everything if the SI
1656 * was marked as being in error state. The owner task is woken up if it exists.
1657 */
stream_int_shutw_applet(struct stream_interface * si)1658 static void stream_int_shutw_applet(struct stream_interface *si)
1659 {
1660 struct channel *ic = si_ic(si);
1661 struct channel *oc = si_oc(si);
1662
1663 oc->flags &= ~CF_SHUTW_NOW;
1664 if (oc->flags & CF_SHUTW)
1665 return;
1666 oc->flags |= CF_SHUTW;
1667 oc->wex = TICK_ETERNITY;
1668 si_done_get(si);
1669
1670 if (tick_isset(si->hcto)) {
1671 ic->rto = si->hcto;
1672 ic->rex = tick_add(now_ms, ic->rto);
1673 }
1674
1675 /* on shutw we always wake the applet up */
1676 appctx_wakeup(si_appctx(si));
1677
1678 switch (si->state) {
1679 case SI_ST_RDY:
1680 case SI_ST_EST:
1681 /* we have to shut before closing, otherwise some short messages
1682 * may never leave the system, especially when there are remaining
1683 * unread data in the socket input buffer, or when nolinger is set.
1684 * However, if SI_FL_NOLINGER is explicitly set, we know there is
1685 * no risk so we close both sides immediately.
1686 */
1687 if (!(si->flags & (SI_FL_ERR | SI_FL_NOLINGER)) &&
1688 !(ic->flags & (CF_SHUTR|CF_DONT_READ)))
1689 return;
1690
1691 /* fall through */
1692 case SI_ST_CON:
1693 case SI_ST_CER:
1694 case SI_ST_QUE:
1695 case SI_ST_TAR:
1696 /* Note that none of these states may happen with applets */
1697 si_applet_release(si);
1698 si->state = SI_ST_DIS;
1699 /* fall through */
1700 default:
1701 si->flags &= ~SI_FL_NOLINGER;
1702 si_rx_shut_blk(si);
1703 ic->flags |= CF_SHUTR;
1704 ic->rex = TICK_ETERNITY;
1705 si->exp = TICK_ETERNITY;
1706 }
1707 }
1708
1709 /* chk_rcv function for applets */
stream_int_chk_rcv_applet(struct stream_interface * si)1710 static void stream_int_chk_rcv_applet(struct stream_interface *si)
1711 {
1712 struct channel *ic = si_ic(si);
1713
1714 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
1715 __FUNCTION__,
1716 si, si->state, ic->flags, si_oc(si)->flags);
1717
1718 if (!ic->pipe) {
1719 /* (re)start reading */
1720 appctx_wakeup(si_appctx(si));
1721 }
1722 }
1723
1724 /* chk_snd function for applets */
stream_int_chk_snd_applet(struct stream_interface * si)1725 static void stream_int_chk_snd_applet(struct stream_interface *si)
1726 {
1727 struct channel *oc = si_oc(si);
1728
1729 DPRINTF(stderr, "%s: si=%p, si->state=%d ic->flags=%08x oc->flags=%08x\n",
1730 __FUNCTION__,
1731 si, si->state, si_ic(si)->flags, oc->flags);
1732
1733 if (unlikely(si->state != SI_ST_EST || (oc->flags & CF_SHUTW)))
1734 return;
1735
1736 /* we only wake the applet up if it was waiting for some data */
1737
1738 if (!(si->flags & SI_FL_WAIT_DATA))
1739 return;
1740
1741 if (!tick_isset(oc->wex))
1742 oc->wex = tick_add_ifset(now_ms, oc->wto);
1743
1744 if (!channel_is_empty(oc)) {
1745 /* (re)start sending */
1746 appctx_wakeup(si_appctx(si));
1747 }
1748 }
1749
1750 /*
1751 * Local variables:
1752 * c-indent-level: 8
1753 * c-basic-offset: 8
1754 * End:
1755 */
1756