"Fossies" - the Fresh Open Source Software Archive

Member "haproxy-1.9.8/src/stream_interface.c" (13 May 2019, 50547 Bytes) of package /linux/misc/haproxy-1.9.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stream_interface.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.9.6_vs_1.9.7.

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