qcc_qmux_send_frames() expects each record to be fully accepted by the
transport. A short write hits ABORT_NOW() or BUG_ON(sent !=
b_data(buf)), and a null write returns 1, rejected by a BUG_ON() in
qcc_io_send() as QMux never uses pacing. Any client can crash the
process by not reading the response until the socket buffer is full.

Keep the unsent part of the record in the Tx buffer and subscribe to
complete it later. A frame is consumed once its record is built so that
it is never emitted twice. A blocked transport now returns -1 as for
QUIC, and the buffer is also flushed when no frame is left.

As a negative value no longer means the frame was not taken,
qcs_send_reset() and qcs_send_stop_sending() now check whether their
frame left the list, as qcc_emit_fctl() already does.

This must be backported up to 3.4.

Signed-off-by: William Dauchy <[email protected]>
---
 include/haproxy/qcm_qmux.h |  9 ++++++++
 src/mux_quic.c             | 23 ++++++++++++---------
 src/qcm_qmux.c             | 42 +++++++++++++++++++-------------------
 3 files changed, 43 insertions(+), 31 deletions(-)

diff --git a/include/haproxy/qcm_qmux.h b/include/haproxy/qcm_qmux.h
index 2e50383d7..2eeff09a2 100644
--- a/include/haproxy/qcm_qmux.h
+++ b/include/haproxy/qcm_qmux.h
@@ -1,10 +1,19 @@
 #ifndef _HAPROXY_QCM_QMUX_H
 #define _HAPROXY_QCM_QMUX_H
 
+#include <haproxy/buf.h>
+#include <haproxy/connection.h>
 #include <haproxy/mux_quic.h>
 
 int qcc_qmux_recv(struct qcc *qcc);
 
 int qcc_qmux_send_frames(struct qcc *qcc, struct list *frms);
 
+/* Returns true if <qcc> has QMux record data not yet emitted. */
+static inline int qcc_qmux_pending(const struct qcc *qcc)
+{
+       /* tx.qmux_buf is in a union with QUIC fields. */
+       return !conn_is_quic(qcc->conn) && b_data(&qcc->tx.qmux_buf);
+}
+
 #endif /* _HAPROXY_QCM_QMUX_H */
diff --git a/src/mux_quic.c b/src/mux_quic.c
index 9a72d7ca5..cef7567f8 100644
--- a/src/mux_quic.c
+++ b/src/mux_quic.c
@@ -2898,12 +2898,13 @@ static int qcc_quic_send_frames(struct qcc *qcc, struct 
list *frms, int stream)
 /* Wrapper for send on transport layer. Send a list of frames <frms> for the
  * connection <qcc>.
  *
- * Returns 0 if all data sent with success. On fatal error, a negative error
- * code is returned. A positive 1 is used if emission should be paced.
+ * Returns 0 if all data sent with success. A negative error code is returned
+ * on fatal error or if the transport is blocked. A positive 1 is used if
+ * emission should be paced. Frames removed from <frms> are always handled.
  */
 static int qcc_send_frames(struct qcc *qcc, struct list *frms, int stream)
 {
-       if (LIST_ISEMPTY(frms)) {
+       if (LIST_ISEMPTY(frms) && !qcc_qmux_pending(qcc)) {
                TRACE_DEVEL("leaving on no frame to send", QMUX_EV_QCC_SEND, 
qcc->conn);
                return -1;
        }
@@ -2934,9 +2935,10 @@ static int qcs_send_reset(struct qcs *qcs)
        frm->reset_stream.final_size = qcs->tx.fc.off_real;
 
        LIST_APPEND(&frms, &frm->list);
-       if (qcc_send_frames(qcs->qcc, &frms, 0)) {
-               if (!LIST_ISEMPTY(&frms))
-                       qc_frm_free(conn_is_quic(qcs->qcc->conn) ? 
qcs->qcc->conn->handle.qc : NULL, &frm);
+       qcc_send_frames(qcs->qcc, &frms, 0);
+       /* Frame handled by transport once removed from the list. */
+       if (!LIST_ISEMPTY(&frms)) {
+               qc_frm_free(conn_is_quic(qcs->qcc->conn) ? 
qcs->qcc->conn->handle.qc : NULL, &frm);
                TRACE_DEVEL("cannot send RESET_STREAM", QMUX_EV_QCS_SEND, 
qcs->qcc->conn, qcs);
                return 1;
        }
@@ -2985,9 +2987,10 @@ static int qcs_send_stop_sending(struct qcs *qcs)
        frm->stop_sending.app_error_code = qcs->ss_err;
 
        LIST_APPEND(&frms, &frm->list);
-       if (qcc_send_frames(qcs->qcc, &frms, 0)) {
-               if (!LIST_ISEMPTY(&frms))
-                       qc_frm_free(conn_is_quic(qcc->conn) ? 
qcc->conn->handle.qc : NULL, &frm);
+       qcc_send_frames(qcs->qcc, &frms, 0);
+       /* Frame handled by transport once removed from the list. */
+       if (!LIST_ISEMPTY(&frms)) {
+               qc_frm_free(conn_is_quic(qcc->conn) ? qcc->conn->handle.qc : 
NULL, &frm);
                TRACE_DEVEL("cannot send STOP_SENDING", QMUX_EV_QCS_SEND, 
qcs->qcc->conn, qcs);
                return 1;
        }
@@ -3330,7 +3333,7 @@ static int qcc_io_send(struct qcc *qcc)
        /* Encode new STREAM frames if list has been previously cleared. */
        if (LIST_ISEMPTY(frms) && !LIST_ISEMPTY(&qcc->send_list)) {
                total = qcc_build_frms(qcc, &qcs_failed);
-               if (LIST_ISEMPTY(frms))
+               if (LIST_ISEMPTY(frms) && !qcc_qmux_pending(qcc))
                        goto out;
        }
 
diff --git a/src/qcm_qmux.c b/src/qcm_qmux.c
index 2a8cdb9e0..a2b767439 100644
--- a/src/qcm_qmux.c
+++ b/src/qcm_qmux.c
@@ -290,10 +290,11 @@ static void qmux_ctrl_send(struct qcs *qcs, uint64_t data)
        TRACE_LEAVE(QMUX_EV_QCS_SEND, qcc->conn, qcs);
 }
 
-/* Sends <frms> list of frames for <qcc> connection.
+/* Sends <frms> list of frames for <qcc> connection. A frame is removed from
+ * the list once its record is stored in the Tx buffer, even if partially sent.
  *
- * Returns 0 if all data are emitted or a positive value if sending should be
- * retried later. A negative error code is used for a fatal failure.
+ * Returns 0 if all data are emitted, else a negative value on fatal error or
+ * if the transport is blocked.
  */
 int qcc_qmux_send_frames(struct qcc *qcc, struct list *frms)
 {
@@ -320,20 +321,17 @@ int qcc_qmux_send_frames(struct qcc *qcc, struct list 
*frms)
                sent = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, 
b_data(buf), NULL, 0, 0);
                if (conn->flags & CO_FL_ERROR)
                        goto out;
-               if (!sent) {
+               b_del(buf, sent);
+               if (b_data(buf)) {
                        TRACE_DEVEL("snd_buf interrupted", QMUX_EV_QCC_SEND, 
qcc->conn);
                        goto out;
                }
-
-               if (sent != b_data(buf)) {
-                       /* TODO */
-                       ABORT_NOW();
-               }
        }
 
        list_for_each_entry_safe(frm, frm_old, frms, list) {
  loop:
                split_frm = next_frm = NULL;
+               BUG_ON(b_data(buf));
                b_reset(buf);
                /* Reserve space for the record header. */
                old = pos = (unsigned char *)b_orig(buf) + lensz;
@@ -374,15 +372,8 @@ int qcc_qmux_send_frames(struct qcc *qcc, struct list 
*frms)
                sent = conn->xprt->snd_buf(conn, conn->xprt_ctx, buf, 
b_data(buf), NULL, 0, 0);
                if (conn->flags & CO_FL_ERROR)
                        goto out;
-               if (!sent) {
-                       TRACE_DEVEL("snd_buf interrupted", QMUX_EV_QCC_SEND, 
qcc->conn);
-                       if (split_frm)
-                               LIST_INSERT(frms, &split_frm->list);
-                       break;
-               }
 
-               /* TODO */
-               BUG_ON(sent != b_data(buf));
+               /* Record committed: consume frame even on partial write. */
                b_del(buf, sent);
 
                if (frm->type >= QUIC_FT_STREAM_8 && frm->type <= 
QUIC_FT_STREAM_F)
@@ -391,9 +382,18 @@ int qcc_qmux_send_frames(struct qcc *qcc, struct list 
*frms)
                if (split_frm) {
                        qc_frm_free(NULL, &split_frm);
                        frm = next_frm;
-                       goto loop;
                }
-               qc_frm_free(NULL, &frm);
+               else {
+                       qc_frm_free(NULL, &frm);
+               }
+
+               if (b_data(buf)) {
+                       TRACE_DEVEL("snd_buf interrupted", QMUX_EV_QCC_SEND, 
qcc->conn);
+                       break;
+               }
+
+               if (frm)
+                       goto loop;
        }
 
  out:
@@ -401,10 +401,10 @@ int qcc_qmux_send_frames(struct qcc *qcc, struct list 
*frms)
                qcc->flags |= QC_CF_ERR_CONN;
                ret = -1;
        }
-       else if (!LIST_ISEMPTY(frms)) {
+       else if (!LIST_ISEMPTY(frms) || b_data(buf)) {
                if (!(qcc->wait_event.events & SUB_RETRY_SEND))
                        conn->xprt->subscribe(conn, conn->xprt_ctx, 
SUB_RETRY_SEND, &qcc->wait_event);
-               ret = 1;
+               ret = -1;
        }
        else {
                ret = 0;
-- 
2.56.1



Reply via email to