/* * This function performs a shutdown-read on a stream interface attached to * a connection in a connected or init state (it does nothing for other * states). It either shuts the read side or marks itself as closed. The buffer * flags are updated to reflect the new state. If the stream interface has * SI_FL_NOHALF, we also forward the close to the write side. If a control * layer is defined, then it is supposed to be a socket layer and file * descriptors are then shutdown or closed accordingly. The function * automatically disables polling if needed. */ static void stream_int_shutr_conn(struct stream_interface *si) { struct connection *conn = __objt_conn(si->end); si->ib->flags &= ~CF_SHUTR_NOW; if (si->ib->flags & CF_SHUTR) return; si->ib->flags |= CF_SHUTR; si->ib->rex = TICK_ETERNITY; si->flags &= ~SI_FL_WAIT_ROOM; if (si->state != SI_ST_EST && si->state != SI_ST_CON) return; if (si->ob->flags & CF_SHUTW) { conn_full_close(conn); si->state = SI_ST_DIS; si->exp = TICK_ETERNITY; } else if (si->flags & SI_FL_NOHALF) { /* we want to immediately forward this close to the write side */ return stream_int_shutw_conn(si); } else if (conn->ctrl) { /* we want the caller to disable polling on this FD */ conn_data_stop_recv(conn); } }
/* * This function propagates a null read received on a socket-based connection. * It updates the stream interface. If the stream interface has SI_FL_NOHALF, * the close is also forwarded to the write side as an abort. This function is * still socket-specific as it handles a setsockopt() call to set the SO_LINGER * state on the socket. */ void stream_sock_read0(struct stream_interface *si) { struct connection *conn = __objt_conn(si->end); si->ib->flags &= ~CF_SHUTR_NOW; if (si->ib->flags & CF_SHUTR) return; si->ib->flags |= CF_SHUTR; si->ib->rex = TICK_ETERNITY; si->flags &= ~SI_FL_WAIT_ROOM; if (si->state != SI_ST_EST && si->state != SI_ST_CON) return; if (si->ob->flags & CF_SHUTW) goto do_close; if (si->flags & SI_FL_NOHALF) { /* we want to immediately forward this close to the write side */ /* force flag on ssl to keep session in cache */ if (conn->xprt->shutw) conn->xprt->shutw(conn, 0); goto do_close; } /* otherwise that's just a normal read shutdown */ if (conn_ctrl_ready(conn)) fdtab[conn->t.sock.fd].linger_risk = 0; __conn_data_stop_recv(conn); return; do_close: /* OK we completely close the socket here just as if we went through si_shut[rw]() */ conn_full_close(conn); si->ib->flags &= ~CF_SHUTR_NOW; si->ib->flags |= CF_SHUTR; si->ib->rex = TICK_ETERNITY; si->ob->flags &= ~CF_SHUTW_NOW; si->ob->flags |= CF_SHUTW; si->ob->wex = TICK_ETERNITY; si->flags &= ~(SI_FL_WAIT_DATA | SI_FL_WAIT_ROOM); si->state = SI_ST_DIS; si->exp = TICK_ETERNITY; return; }
/* * This function propagates a null read received on a socket-based connection. * It updates the stream interface. If the stream interface has SI_FL_NOHALF, * the close is also forwarded to the write side as an abort. */ void stream_sock_read0(struct stream_interface *si) { struct connection *conn = __objt_conn(si->end); struct channel *ic = si_ic(si); struct channel *oc = si_oc(si); HA_DBG("stream_sock_read0\n"); ic->flags &= ~CF_SHUTR_NOW; if (ic->flags & CF_SHUTR) return; ic->flags |= CF_SHUTR; ic->rex = TICK_ETERNITY; si->flags &= ~SI_FL_WAIT_ROOM; if (si->state != SI_ST_EST && si->state != SI_ST_CON) return; if (oc->flags & CF_SHUTW) goto do_close; if (si->flags & SI_FL_NOHALF) { /* we want to immediately forward this close to the write side */ /* force flag on ssl to keep stream in cache */ conn_data_shutw_hard(conn); goto do_close; } /* otherwise that's just a normal read shutdown */ __conn_data_stop_recv(conn); return; do_close: /* OK we completely close the socket here just as if we went through si_shut[rw]() */ conn_full_close(conn); ic->flags &= ~CF_SHUTR_NOW; ic->flags |= CF_SHUTR; ic->rex = TICK_ETERNITY; oc->flags &= ~CF_SHUTW_NOW; oc->flags |= CF_SHUTW; oc->wex = TICK_ETERNITY; si->flags &= ~(SI_FL_WAIT_DATA | SI_FL_WAIT_ROOM); si->state = SI_ST_DIS; si->exp = TICK_ETERNITY; return; }
/* This function kills an existing embryonic session. It stops the connection's * transport layer, releases assigned resources, resumes the listener if it was * disabled and finally kills the file descriptor. This function requires that * sess->origin points to the incoming connection. */ static void session_kill_embryonic(struct session *sess) { int level = LOG_INFO; struct connection *conn = __objt_conn(sess->origin); struct task *task = sess->task; unsigned int log = sess->fe->to_log; const char *err_msg; if (sess->fe->options2 & PR_O2_LOGERRORS) level = LOG_ERR; if (log && (sess->fe->options & PR_O_NULLNOLOG)) { /* with "option dontlognull", we don't log connections with no transfer */ if (!conn->err_code || conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT || conn->err_code == CO_ER_CIP_EMPTY || conn->err_code == CO_ER_CIP_ABORT || conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT) log = 0; } if (log) { if (!conn->err_code && (task->state & TASK_WOKEN_TIMER)) { if (conn->flags & CO_FL_ACCEPT_PROXY) conn->err_code = CO_ER_PRX_TIMEOUT; else if (conn->flags & CO_FL_ACCEPT_CIP) conn->err_code = CO_ER_CIP_TIMEOUT; else if (conn->flags & CO_FL_SSL_WAIT_HS) conn->err_code = CO_ER_SSL_TIMEOUT; } session_prepare_log_prefix(sess); err_msg = conn_err_code_str(conn); if (err_msg) send_log(sess->fe, level, "%s: %s\n", trash.str, err_msg); else send_log(sess->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n", trash.str, conn->err_code, conn->flags); } /* kill the connection now */ conn_stop_tracking(conn); conn_full_close(conn); conn_free(conn); task_delete(task); task_free(task); session_free(sess); }
/* This function is used for inter-stream-interface calls. It is called by the * consumer to inform the producer side that it may be interested in checking * for free space in the buffer. Note that it intentionally does not update * timeouts, so that we can still check them later at wake-up. This function is * dedicated to connection-based stream interfaces. */ static void stream_int_chk_rcv_conn(struct stream_interface *si) { struct channel *ib = si->ib; struct connection *conn = __objt_conn(si->end); if (unlikely(si->state > SI_ST_EST || (ib->flags & CF_SHUTR))) return; conn_refresh_polling_flags(conn); if ((ib->flags & CF_DONT_READ) || channel_full(ib)) { /* stop reading */ if (!(ib->flags & CF_DONT_READ)) /* full */ si->flags |= SI_FL_WAIT_ROOM; __conn_data_stop_recv(conn); } else { /* (re)start reading */ si->flags &= ~SI_FL_WAIT_ROOM; __conn_data_want_recv(conn); } conn_cond_update_data_polling(conn); }
/* prepare the trash with a log prefix for session <sess>. It only works with * embryonic sessions based on a real connection. This function requires that * at sess->origin points to the incoming connection. */ static void session_prepare_log_prefix(struct session *sess) { struct tm tm; char pn[INET6_ADDRSTRLEN]; int ret; char *end; struct connection *cli_conn = __objt_conn(sess->origin); ret = addr_to_str(&cli_conn->addr.from, pn, sizeof(pn)); if (ret <= 0) chunk_printf(&trash, "unknown ["); else if (ret == AF_UNIX) chunk_printf(&trash, "%s:%d [", pn, sess->listener->luid); else chunk_printf(&trash, "%s:%d [", pn, get_host_port(&cli_conn->addr.from)); get_localtime(sess->accept_date.tv_sec, &tm); end = date2str_log(trash.str + trash.len, &tm, &(sess->accept_date), trash.size - trash.len); trash.len = end - trash.str; if (sess->listener->name) chunk_appendf(&trash, "] %s/%s", sess->fe->id, sess->listener->name); else chunk_appendf(&trash, "] %s/%d", sess->fe->id, sess->listener->luid); }
/* Updates the polling status of a connection outside of the connection handler * based on the channel's flags and the stream interface's flags. It needs to be * called once after the channels' flags have settled down and the stream has * been updated. It is not designed to be called from within the connection * handler itself. */ void stream_int_update_conn(struct stream_interface *si) { struct channel *ic = si_ic(si); struct channel *oc = si_oc(si); struct connection *conn = __objt_conn(si->end); if (!(ic->flags & CF_SHUTR)) { /* Read not closed */ if ((ic->flags & CF_DONT_READ) || !channel_may_recv(ic)) __conn_data_stop_recv(conn); else __conn_data_want_recv(conn); } if (!(oc->flags & CF_SHUTW)) { /* Write not closed */ if (channel_is_empty(oc)) __conn_data_stop_send(conn); else __conn_data_want_send(conn); } conn_cond_update_data_polling(conn); }
/* This function kills an existing embryonic session. It stops the connection's * transport layer, releases assigned resources, resumes the listener if it was * disabled and finally kills the file descriptor. This function requires that * sess->origin points to the incoming connection. */ static void session_kill_embryonic(struct session *sess) { int level = LOG_INFO; struct connection *conn = __objt_conn(sess->origin); struct task *task = conn->owner; unsigned int log = sess->fe->to_log; const char *err_msg; if (sess->fe->options2 & PR_O2_LOGERRORS) level = LOG_ERR; if (log && (sess->fe->options & PR_O_NULLNOLOG)) { /* with "option dontlognull", we don't log connections with no transfer */ if (!conn->err_code || conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT || conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT) log = 0; } if (log) { if (!conn->err_code && (task->state & TASK_WOKEN_TIMER)) { if (conn->flags & CO_FL_ACCEPT_PROXY) conn->err_code = CO_ER_PRX_TIMEOUT; else if (conn->flags & CO_FL_SSL_WAIT_HS) conn->err_code = CO_ER_SSL_TIMEOUT; } session_prepare_log_prefix(sess); err_msg = conn_err_code_str(conn); if (err_msg) send_log(sess->fe, level, "%s: %s\n", trash.str, err_msg); else send_log(sess->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n", trash.str, conn->err_code, conn->flags); } /* kill the connection now */ conn_force_close(conn); conn_free(conn); sess->fe->feconn--; if (!(sess->listener->options & LI_O_UNLIMITED)) actconn--; jobs--; sess->listener->nbconn--; if (sess->listener->state == LI_FULL) resume_listener(sess->listener); /* Dequeues all of the listeners waiting for a resource */ if (!LIST_ISEMPTY(&global_listener_queue)) dequeue_all_listeners(&global_listener_queue); if (!LIST_ISEMPTY(&sess->fe->listener_queue) && (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0)) dequeue_all_listeners(&sess->fe->listener_queue); task_delete(task); task_free(task); session_free(sess); }
/* This function is used for inter-stream-interface calls. It is called by the * producer to inform the consumer side that it may be interested in checking * for data in the buffer. Note that it intentionally does not update timeouts, * so that we can still check them later at wake-up. */ static void stream_int_chk_snd_conn(struct stream_interface *si) { struct channel *ob = si->ob; struct connection *conn = __objt_conn(si->end); if (unlikely(si->state > SI_ST_EST || (ob->flags & CF_SHUTW))) return; if (unlikely(channel_is_empty(ob))) /* called with nothing to send ! */ return; if (!ob->pipe && /* spliced data wants to be forwarded ASAP */ !(si->flags & SI_FL_WAIT_DATA)) /* not waiting for data */ return; if (conn->flags & (CO_FL_DATA_WR_ENA|CO_FL_CURR_WR_ENA)) { /* already subscribed to write notifications, will be called * anyway, so let's avoid calling it especially if the reader * is not ready. */ return; } /* Before calling the data-level operations, we have to prepare * the polling flags to ensure we properly detect changes. */ conn_refresh_polling_flags(conn); __conn_data_want_send(conn); if (!(conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN))) { si_conn_send(conn); if (conn->flags & CO_FL_ERROR) { /* Write error on the file descriptor */ __conn_data_stop_both(conn); si->flags |= SI_FL_ERR; goto out_wakeup; } } /* OK, so now we know that some data might have been sent, and that we may * have to poll first. We have to do that too if the buffer is not empty. */ if (channel_is_empty(ob)) { /* the connection is established but we can't write. Either the * buffer is empty, or we just refrain from sending because the * ->o limit was reached. Maybe we just wrote the last * chunk and need to close. */ __conn_data_stop_send(conn); if (((ob->flags & (CF_SHUTW|CF_AUTO_CLOSE|CF_SHUTW_NOW)) == (CF_AUTO_CLOSE|CF_SHUTW_NOW)) && (si->state == SI_ST_EST)) { si_shutw(si); goto out_wakeup; } if ((ob->flags & (CF_SHUTW|CF_SHUTW_NOW)) == 0) si->flags |= SI_FL_WAIT_DATA; ob->wex = TICK_ETERNITY; } else { /* Otherwise there are remaining data to be sent in the buffer, * which means we have to poll before doing so. */ __conn_data_want_send(conn); si->flags &= ~SI_FL_WAIT_DATA; if (!tick_isset(ob->wex)) ob->wex = tick_add_ifset(now_ms, ob->wto); } if (likely(ob->flags & CF_WRITE_ACTIVITY)) { /* update timeout if we have written something */ if ((ob->flags & (CF_SHUTW|CF_WRITE_PARTIAL)) == CF_WRITE_PARTIAL && !channel_is_empty(ob)) ob->wex = tick_add_ifset(now_ms, ob->wto); if (tick_isset(si->ib->rex) && !(si->flags & SI_FL_INDEP_STR)) { /* Note: to prevent the client from expiring read timeouts * during writes, we refresh it. We only do this if the * interface is not configured for "independent streams", * because for some applications it's better not to do this, * for instance when continuously exchanging small amounts * of data which can full the socket buffers long before a * write timeout is detected. */ si->ib->rex = tick_add_ifset(now_ms, si->ib->rto); } } /* in case of special condition (error, shutdown, end of write...), we * have to notify the task. */ if (likely((ob->flags & (CF_WRITE_NULL|CF_WRITE_ERROR|CF_SHUTW)) || ((ob->flags & CF_WAKE_WRITE) && ((channel_is_empty(si->ob) && !ob->to_forward) || si->state != SI_ST_EST)))) { out_wakeup: if (!(si->flags & SI_FL_DONT_WAKE) && si->owner) task_wakeup(si->owner, TASK_WOKEN_IO); } /* commit possible polling changes */ conn_cond_update_polling(conn); }
/* * This function performs a shutdown-write on a stream interface attached to * a connection in a connected or init state (it does nothing for other * states). It either shuts the write side or marks itself as closed. The * buffer flags are updated to reflect the new state. It does also close * everything if the SI was marked as being in error state. If there is a * data-layer shutdown, it is called. If a control layer is defined, then it is * supposed to be a socket layer and file descriptors are then shutdown or * closed accordingly. The function automatically disables polling if needed. * Note: at the moment, we continue to check conn->ctrl eventhough we *know* it * is valid. This will help selecting the proper shutdown() and setsockopt() * calls if/when we implement remote sockets later. */ static void stream_int_shutw_conn(struct stream_interface *si) { struct connection *conn = __objt_conn(si->end); si->ob->flags &= ~CF_SHUTW_NOW; if (si->ob->flags & CF_SHUTW) return; si->ob->flags |= CF_SHUTW; si->ob->wex = TICK_ETERNITY; si->flags &= ~SI_FL_WAIT_DATA; switch (si->state) { case SI_ST_EST: /* we have to shut before closing, otherwise some short messages * may never leave the system, especially when there are remaining * unread data in the socket input buffer, or when nolinger is set. * However, if SI_FL_NOLINGER is explicitly set, we know there is * no risk so we close both sides immediately. */ if (si->flags & SI_FL_ERR) { /* quick close, the socket is alredy shut anyway */ } else if (si->flags & SI_FL_NOLINGER) { /* unclean data-layer shutdown */ if (conn->xprt && conn->xprt->shutw) conn->xprt->shutw(conn, 0); } else { /* clean data-layer shutdown */ if (conn->xprt && conn->xprt->shutw) conn->xprt->shutw(conn, 1); /* If the stream interface is configured to disable half-open * connections, we'll skip the shutdown(), but only if the * read size is already closed. Otherwise we can't support * closed write with pending read (eg: abortonclose while * waiting for the server). */ if (!(si->flags & SI_FL_NOHALF) || !(si->ib->flags & (CF_SHUTR|CF_DONT_READ))) { /* We shutdown transport layer */ if (conn_ctrl_ready(conn)) shutdown(conn->t.sock.fd, SHUT_WR); if (!(si->ib->flags & (CF_SHUTR|CF_DONT_READ))) { /* OK just a shutw, but we want the caller * to disable polling on this FD if exists. */ if (conn->ctrl) conn_data_stop_send(conn); return; } } } /* fall through */ case SI_ST_CON: /* we may have to close a pending connection, and mark the * response buffer as shutr */ conn_full_close(conn); /* fall through */ case SI_ST_CER: case SI_ST_QUE: case SI_ST_TAR: si->state = SI_ST_DIS; /* fall through */ default: si->flags &= ~(SI_FL_WAIT_ROOM | SI_FL_NOLINGER); si->ib->flags &= ~CF_SHUTR_NOW; si->ib->flags |= CF_SHUTR; si->ib->rex = TICK_ETERNITY; si->exp = TICK_ETERNITY; } }
/* Updates the timers and flags of a stream interface attached to a connection, * depending on the buffers' flags. It should only be called once after the * buffer flags have settled down, and before they are cleared. It doesn't * harm to call it as often as desired (it just slightly hurts performance). * It is only meant to be called by upper layers after buffer flags have been * manipulated by analysers. */ void stream_int_update_conn(struct stream_interface *si) { struct channel *ib = si->ib; struct channel *ob = si->ob; struct connection *conn = __objt_conn(si->end); /* Check if we need to close the read side */ if (!(ib->flags & CF_SHUTR)) { /* Read not closed, update FD status and timeout for reads */ if ((ib->flags & CF_DONT_READ) || channel_full(ib)) { /* stop reading */ if (!(si->flags & SI_FL_WAIT_ROOM)) { if (!(ib->flags & CF_DONT_READ)) /* full */ si->flags |= SI_FL_WAIT_ROOM; conn_data_stop_recv(conn); ib->rex = TICK_ETERNITY; } } else { /* (re)start reading and update timeout. Note: we don't recompute the timeout * everytime we get here, otherwise it would risk never to expire. We only * update it if is was not yet set. The stream socket handler will already * have updated it if there has been a completed I/O. */ si->flags &= ~SI_FL_WAIT_ROOM; conn_data_want_recv(conn); if (!(ib->flags & (CF_READ_NOEXP|CF_DONT_READ)) && !tick_isset(ib->rex)) ib->rex = tick_add_ifset(now_ms, ib->rto); } } /* Check if we need to close the write side */ if (!(ob->flags & CF_SHUTW)) { /* Write not closed, update FD status and timeout for writes */ if (channel_is_empty(ob)) { /* stop writing */ if (!(si->flags & SI_FL_WAIT_DATA)) { if ((ob->flags & CF_SHUTW_NOW) == 0) si->flags |= SI_FL_WAIT_DATA; conn_data_stop_send(conn); ob->wex = TICK_ETERNITY; } } else { /* (re)start writing and update timeout. Note: we don't recompute the timeout * everytime we get here, otherwise it would risk never to expire. We only * update it if is was not yet set. The stream socket handler will already * have updated it if there has been a completed I/O. */ si->flags &= ~SI_FL_WAIT_DATA; conn_data_want_send(conn); if (!tick_isset(ob->wex)) { ob->wex = tick_add_ifset(now_ms, ob->wto); if (tick_isset(ib->rex) && !(si->flags & SI_FL_INDEP_STR)) { /* Note: depending on the protocol, we don't know if we're waiting * for incoming data or not. So in order to prevent the socket from * expiring read timeouts during writes, we refresh the read timeout, * except if it was already infinite or if we have explicitly setup * independent streams. */ ib->rex = tick_add_ifset(now_ms, ib->rto); } } } } }