]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: conn_stream: Add a pointer to the app object into the conn-stream
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 21 Dec 2021 13:35:17 +0000 (14:35 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 24 Feb 2022 10:00:02 +0000 (11:00 +0100)
In the same way the conn-stream has a pointer to the stream endpoint , this
patch adds a pointer to the application entity in the conn-stream
structure. For now, it is a stream or a health-check. It is mandatory to
merge the stream-interface with the conn-stream.

18 files changed:
include/haproxy/conn_stream-t.h
include/haproxy/conn_stream.h
include/haproxy/stream_interface.h
src/check.c
src/conn_stream.c
src/dns.c
src/flt_spoe.c
src/hlua.c
src/http_client.c
src/mux_fcgi.c
src/mux_h1.c
src/mux_h2.c
src/mux_pt.c
src/peers.c
src/sink.c
src/stream.c
src/stream_interface.c
src/tcpcheck.c

index fcf893f5de11c00b8adc09d5d54c0e8baf519586..88775e9178af91aea8b0a2e512840b87e1c52f64 100644 (file)
@@ -91,6 +91,7 @@ struct conn_stream {
        /* 3 bytes hole here */
        unsigned int flags;                  /* CS_FL_* */
        enum obj_type *end;                  /* points to the end point (connection or appctx) */
+       enum obj_type *app;                  /* points to the applicative point (stream or check) */
        void *data;                          /* pointer to upper layer's entity (eg: stream interface) */
        const struct data_cb *data_cb;       /* data layer callbacks. Must be set before xprt->init() */
        void *ctx;                           /* mux-specific context */
index c7f736d2e42036cbbd6abb86a115012ba71ab626..7a7df2aff4a8a6c6a115c38212ffced060233e6f 100644 (file)
 #include <haproxy/obj_type.h>
 #include <haproxy/pool-t.h>
 
+struct stream;
+struct check;
+
 extern struct pool_head *pool_head_connstream;
 
 #define IS_HTX_CS(cs)     (cs_conn(cs) && IS_HTX_CONN(cs_conn(cs)))
 
-struct conn_stream *cs_new(enum obj_type *endp);
+struct conn_stream *cs_new(enum obj_type *endp, void *ctx, enum obj_type *app, void *data, const struct data_cb *data_cb);
 void cs_free(struct conn_stream *cs);
 
+
 /*
  * Initializes all required fields for a new conn_strema.
  */
-static inline void cs_init(struct conn_stream *cs, enum obj_type *endp)
+static inline void cs_init(struct conn_stream *cs)
 {
-       struct connection *conn = objt_conn(endp);
-       struct appctx *appctx = objt_appctx(endp);
-
        cs->obj_type = OBJ_TYPE_CS;
        cs->flags = CS_FL_NONE;
-       cs->end = endp;
-       if (conn) {
-               cs->ctx = conn;
-               if (!conn->ctx)
-                       conn->ctx = cs;
-       }
-       else if (appctx) {
-               cs->ctx = appctx;
-               /* appctx->owner must be set by the caller for now */
-       }
+       cs->end = NULL;
+       cs->app = NULL;
+       cs->ctx = NULL;
        cs->data = NULL;
        cs->data_cb = NULL;
 }
@@ -88,14 +82,32 @@ static inline struct appctx *cs_appctx(const struct conn_stream *cs)
 
 static inline struct stream_interface *cs_si(const struct conn_stream *cs)
 {
-       return ((cs_conn(cs) || cs_appctx(cs)) ? cs->data : NULL);
+       return (cs ? cs->data : NULL);
 }
 
-/* Attaches a conn_stream to a data layer and sets the relevant callbacks */
-static inline void cs_attach(struct conn_stream *cs, void *data, const struct data_cb *data_cb)
+static inline struct stream *cs_strm(const struct conn_stream *cs)
 {
-       cs->data_cb = data_cb;
+       return (cs ? objt_stream(cs->app) : NULL);
+}
+
+static inline struct check *cs_check(const struct conn_stream *cs)
+{
+       return (cs ? objt_check(cs->app) : NULL);
+}
+
+/* Attaches a conn_stream to an endpoint and sets the endpoint ctx */
+static inline void cs_attach_endp(struct conn_stream *cs, enum obj_type *endp, void *ctx)
+{
+       cs->end = endp;
+       cs->ctx = ctx;
+}
+
+/* Attaches a conn_stream to a app layer and sets the relevant callbacks */
+static inline void cs_attach_app(struct conn_stream *cs, enum obj_type *app, void *data, const struct data_cb *data_cb)
+{
+       cs->app = app;
        cs->data = data;
+       cs->data_cb = data_cb;
 }
 
 /* Detach the conn_stream from the connection, if any. If a mux owns the
@@ -127,7 +139,7 @@ static inline void cs_detach(struct conn_stream *cs)
                        appctx->applet->release(appctx);
                appctx_free(appctx);
        }
-       cs_init(cs, NULL);
+       cs_init(cs);
 }
 
 /* Release a conn_stream */
index 314f54c5a042b6e078ff94e3f12046d798c8f0ce..6da05bcb54a581d4b67945e1fa9a3bb74f860af1 100644 (file)
@@ -187,18 +187,18 @@ static inline void si_attach_cs(struct stream_interface *si, struct conn_stream
        si->cs = cs;
        if (cs_conn(cs)) {
                si->ops = &si_conn_ops;
-               cs_attach(cs, si, &si_conn_cb);
+               cs_attach_app(cs, &si_strm(si)->obj_type, si, &si_conn_cb);
        }
        else if (cs_appctx(cs)) {
                struct appctx *appctx = cs_appctx(cs);
 
                si->ops = &si_applet_ops;
                appctx->owner = cs;
-               cs_attach(cs, si, NULL);
+               cs_attach_app(cs, &si_strm(si)->obj_type, si, NULL);
        }
        else {
                si->ops = &si_embedded_ops;
-               cs_attach(cs, si, NULL);
+               cs_attach_app(cs, &si_strm(si)->obj_type, si, NULL);
        }
 }
 
@@ -208,10 +208,11 @@ static inline void si_attach_cs(struct stream_interface *si, struct conn_stream
 static inline void si_attach_conn(struct stream_interface *si, struct connection *conn)
 {
        si_reset_endpoint(si);
-       cs_init(si->cs, &conn->obj_type);
        if (!conn->ctx)
                conn->ctx = si->cs;
-       si_attach_cs(si, si->cs);
+       si->ops = &si_conn_ops;
+       cs_attach_endp(si->cs, &conn->obj_type, conn);
+       cs_attach_app(si->cs, &si_strm(si)->obj_type, si, &si_conn_cb);
 }
 
 /* Attach appctx <appctx> to the stream interface <si>. The stream interface
@@ -220,9 +221,10 @@ static inline void si_attach_conn(struct stream_interface *si, struct connection
 static inline void si_attach_appctx(struct stream_interface *si, struct appctx *appctx)
 {
        si_reset_endpoint(si);
-       cs_init(si->cs, &appctx->obj_type);
        appctx->owner = si->cs;
-       si_attach_cs(si, si->cs);
+       si->ops = &si_applet_ops;
+       cs_attach_endp(si->cs, &appctx->obj_type, appctx);
+       cs_attach_app(si->cs, &si_strm(si)->obj_type, si, NULL);
 }
 
 /* call the applet's release function if any. Needs to be called upon close() */
index 97d340a57e6b1078e08578c933b3a9bc986222a6..cbdf620a9bd4b3abdbd668e9f64c0c652c60cce1 100644 (file)
@@ -1019,7 +1019,7 @@ int httpchk_build_status_header(struct server *s, struct buffer *buf)
 static int wake_srv_chk(struct conn_stream *cs)
 {
        struct connection *conn;
-       struct check *check = cs->data;
+       struct check *check = cs_check(cs);
        struct email_alertq *q = container_of(check, typeof(*q), check);
        int ret = 0;
 
index 7b53eccae769da39586d16a84afc32b6cc5bffc8..41d4a2e5c07bd1d916c7b56b315decdfd72709de 100644 (file)
@@ -22,14 +22,16 @@ DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
 /* Tries to allocate a new conn_stream and initialize its main fields. On
  * failure, nothing is allocated and NULL is returned.
  */
-struct conn_stream *cs_new(enum obj_type *endp)
+struct conn_stream *cs_new(enum obj_type *endp, void *ctx, enum obj_type *app, void *data, const struct data_cb *data_cb)
 {
        struct conn_stream *cs;
 
        cs = pool_alloc(pool_head_connstream);
        if (unlikely(!cs))
                return NULL;
-       cs_init(cs, endp);
+       cs_init(cs);
+       cs_attach_endp(cs, endp, ctx);
+       cs_attach_app(cs, app, data, data_cb);
        return cs;
 }
 
index 719aed6f0a88027fa18c681f154305abed5c3209..ed2f875c41705c976c6e42cf0740622b368cbbc6 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -903,7 +903,7 @@ static struct appctx *dns_session_create(struct dns_session *ds)
                goto out_free_appctx;
        }
 
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs) {
                ha_alert("out of memory in dns_session_create().\n");
                goto out_free_sess;
index a12d75daabcfde3b8c9dd49ce7da13a39d111e5f..6c4c9f8ab983462b76cab429d77876a71ddce24a 100644 (file)
@@ -2024,7 +2024,7 @@ spoe_create_appctx(struct spoe_config *conf)
        if (!sess)
                goto out_free_spoe;
 
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs)
                goto out_free_sess;
 
index 35ed2e5bb6a1dc77b98e330db3c328f696a6455b..219cd260ccc6452cb5606627ea434983fee948e6 100644 (file)
@@ -2961,7 +2961,7 @@ __LJMP static int hlua_socket_new(lua_State *L)
                goto out_fail_appctx;
        }
 
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs) {
                hlua_pusherror(L, "socket: out of memory");
                goto out_fail_sess;
index 0149eeb885b3e78754f3c2d5ee1a6617164737b2..6b060a14234a1330956a44c1ce175c841eae0e5b 100644 (file)
@@ -486,7 +486,7 @@ struct appctx *httpclient_start(struct httpclient *hc)
                ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__);
                goto out_free_appctx;
        }
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs) {
                ha_alert("httpclient: out of memory in %s:%d.\n", __FUNCTION__, __LINE__);
                goto out_free_sess;
index 3a4f7c03976151ce71f3a61725e735cc28f7f55a..d14a166e0b6c8d561a5ba7ed538d67475c8b96dc 100644 (file)
@@ -1230,8 +1230,8 @@ static int fcgi_set_default_param(struct fcgi_conn *fconn, struct fcgi_strm *fst
                                  struct fcgi_strm_params *params)
 {
        struct connection *cli_conn = objt_conn(fstrm->sess->origin);
-       const struct sockaddr_storage *src = si_src(si_opposite(fstrm->cs->data));
-       const struct sockaddr_storage *dst = si_dst(si_opposite(fstrm->cs->data));
+       const struct sockaddr_storage *src = si_src(si_opposite(cs_si(fstrm->cs)));
+       const struct sockaddr_storage *dst = si_dst(si_opposite(cs_si(fstrm->cs)));
        struct ist p;
 
        if (!sl)
@@ -3312,11 +3312,11 @@ static void fcgi_strm_capture_bad_message(struct fcgi_conn *fconn, struct fcgi_s
        struct proxy *other_end;
        union error_snapshot_ctx ctx;
 
-       if (fstrm->cs && fstrm->cs->data) {
+       if (fstrm->cs && cs_strm(fstrm->cs)) {
                if (sess == NULL)
-                       sess = si_strm(fstrm->cs->data)->sess;
+                       sess = cs_strm(fstrm->cs)->sess;
                if (!(h1m->flags & H1_MF_RESP))
-                       other_end = si_strm(fstrm->cs->data)->be;
+                       other_end = cs_strm(fstrm->cs)->be;
                else
                        other_end = sess->fe;
        } else
@@ -4189,8 +4189,8 @@ static int fcgi_show_fd(struct buffer *msg, struct connection *conn)
                              (unsigned int)b_head_ofs(&fstrm->rxbuf), (unsigned int)b_size(&fstrm->rxbuf),
                              fstrm->cs);
                if (fstrm->cs)
-                       chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
-                                     fstrm->cs->flags, fstrm->cs->data);
+                       chunk_appendf(msg, " .cs.flg=0x%08x .cs.app=%p",
+                                     fstrm->cs->flags, fstrm->cs->app);
                chunk_appendf(&trash, " .subs=%p", fstrm->subs);
                if (fstrm->subs) {
                        chunk_appendf(&trash, "(ev=%d tl=%p", fstrm->subs->events, fstrm->subs->tasklet);
index 5fdcd468d4d089ba7a7dabcb64225e11d9433201..48ba0a322931126435b8279923f8fe7dae32384c 100644 (file)
@@ -682,13 +682,12 @@ static struct conn_stream *h1s_new_cs(struct h1s *h1s, struct buffer *input)
        struct conn_stream *cs;
 
        TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s);
-       cs = cs_new(&h1c->conn->obj_type);
+       cs = cs_new(&h1c->conn->obj_type, h1s, NULL, NULL, NULL);
        if (!cs) {
                TRACE_ERROR("CS allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s);
                goto err;
        }
        h1s->cs = cs;
-       cs->ctx = h1s;
 
        if (h1s->flags & H1S_F_NOT_FIRST)
                cs->flags |= CS_FL_NOT_FIRST;
@@ -1351,11 +1350,11 @@ static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s,
        struct proxy *other_end;
        union error_snapshot_ctx ctx;
 
-       if ((h1c->flags & H1C_F_ST_ATTACHED) && h1s->cs->data) {
+       if ((h1c->flags & H1C_F_ST_ATTACHED) && cs_strm(h1s->cs)) {
                if (sess == NULL)
-                       sess = si_strm(h1s->cs->data)->sess;
+                       sess = cs_strm(h1s->cs)->sess;
                if (!(h1m->flags & H1_MF_RESP))
-                       other_end = si_strm(h1s->cs->data)->be;
+                       other_end = cs_strm(h1s->cs)->be;
                else
                        other_end = sess->fe;
        } else
@@ -3828,8 +3827,8 @@ static int h1_show_fd(struct buffer *msg, struct connection *conn)
                              h1m_state_str(h1s->req.state),
                              h1m_state_str(h1s->res.state), method, h1s->status);
                if (h1s->cs)
-                       chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
-                                     h1s->cs->flags, h1s->cs->data);
+                       chunk_appendf(msg, " .cs.flg=0x%08x .cs.app=%p",
+                                     h1s->cs->flags, h1s->cs->app);
 
                chunk_appendf(&trash, " .subs=%p", h1s->subs);
                if (h1s->subs) {
index 3a98e5d2c06a18be80f0e62938653bb7ee524d4b..2049b5105264ebb99a0e51a5bad941890ccfa16b 100644 (file)
@@ -1529,13 +1529,12 @@ static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id, struct buffer *in
        if (!h2s)
                goto out;
 
-       cs = cs_new(&h2c->conn->obj_type);
+       cs = cs_new(&h2c->conn->obj_type, h2s, NULL, NULL, NULL);
        if (!cs)
                goto out_close;
 
        cs->flags |= CS_FL_NOT_FIRST;
        h2s->cs = cs;
-       cs->ctx = h2s;
        h2c->nb_cs++;
 
        /* FIXME wrong analogy between ext-connect and websocket, this need to
@@ -6652,8 +6651,8 @@ static int h2_show_fd(struct buffer *msg, struct connection *conn)
                              (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
                              h2s->cs);
                if (h2s->cs)
-                       chunk_appendf(msg, "(.flg=0x%08x .data=%p)",
-                                     h2s->cs->flags, h2s->cs->data);
+                       chunk_appendf(msg, "(.flg=0x%08x .app=%p)",
+                                     h2s->cs->flags, h2s->cs->app);
 
                chunk_appendf(&trash, " .subs=%p", h2s->subs);
                if (h2s->subs) {
index 13e60d84da52b7133829c15a254e93146ea6c3c3..7ba9da92e51ebf3b9587449355e320eec33b04b3 100644 (file)
@@ -291,7 +291,7 @@ static int mux_pt_init(struct connection *conn, struct proxy *prx, struct sessio
        ctx->conn = conn;
 
        if (!cs) {
-               cs = cs_new(&conn->obj_type);
+               cs = cs_new(&conn->obj_type, NULL, NULL, NULL, NULL);
                if (!cs) {
                        TRACE_ERROR("CS allocation failure", PT_EV_STRM_NEW|PT_EV_STRM_END|PT_EV_STRM_ERR, conn);
                        goto fail_free_ctx;
index 1b8045273913f3554df9825b7c8862b781661ee8..76a091722b07a5d3c7f085e1730d3f6380dc10da 100644 (file)
@@ -3204,7 +3204,7 @@ static struct appctx *peer_session_create(struct peers *peers, struct peer *peer
                goto out_free_appctx;
        }
 
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs) {
                ha_alert("out of memory in peer_session_create().\n");
                goto out_free_sess;
index a3464abbadfe8d05bf82ae9d736d9a96868a0e10..b09b8959ab8ce6ceee37d3857de2fc9f9a86773c 100644 (file)
@@ -655,7 +655,7 @@ static struct appctx *sink_forward_session_create(struct sink *sink, struct sink
                goto out_free_appctx;
        }
 
-       cs = cs_new(&appctx->obj_type);
+       cs = cs_new(&appctx->obj_type, appctx, NULL, NULL, NULL);
        if (!cs) {
                ha_alert("out of memory in sink_forward_session_create");
                goto out_free_sess;
index dd6ae6f8aff0080465b2b6f7b877e996f659f156..f018c378c4c8c3145373f45c3cc9b92eab6fb94e 100644 (file)
@@ -275,8 +275,7 @@ static void strm_trace(enum trace_level level, uint64_t mask, const struct trace
  */
 int stream_upgrade_from_cs(struct conn_stream *cs, struct buffer *input)
 {
-       struct stream_interface *si = cs->data;
-       struct stream *s = si_strm(si);
+       struct stream *s = cs_strm(cs);
 
        if (cs_conn_mux(cs)) {
                const struct mux_ops *mux = cs_conn_mux(cs);
@@ -475,7 +474,7 @@ struct stream *stream_new(struct session *sess, struct conn_stream *cs, struct b
        if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
                s->si[1].flags |= SI_FL_INDEP_STR;
 
-       s->si[1].cs = cs_new(NULL);
+       s->si[1].cs = cs_new(NULL, NULL, &s->obj_type, &s->si[1], NULL);
        if (!s->si[1].cs)
                goto out_fail_alloc_cs;
 
index ae07404fcf119cfce3ea0ff9524cbf0a783cdbe3..5b86bf8d91a659dc1428a1f0cec5ad1fb20a1486 100644 (file)
@@ -354,12 +354,11 @@ int conn_si_send_proxy(struct connection *conn, unsigned int flag)
                 */
 
                if (cs && cs->data_cb == &si_conn_cb) {
-                       struct stream_interface *si = cs->data;
-                       struct stream *strm = si_strm(si);
+                       struct stream *strm = cs_strm(cs);
 
                        ret = make_proxy_line(trash.area, trash.size,
                                              objt_server(conn->target),
-                                             cs_conn(si_opposite(si)->cs),
+                                             cs_conn(si_opposite(cs_si(cs))->cs),
                                              strm);
                }
                else {
@@ -564,7 +563,7 @@ static void stream_int_notify(struct stream_interface *si)
 static int si_cs_process(struct conn_stream *cs)
 {
        struct connection *conn = cs_conn(cs);
-       struct stream_interface *si = cs->data;
+       struct stream_interface *si = cs_si(cs);
        struct channel *ic = si_ic(si);
        struct channel *oc = si_oc(si);
 
@@ -652,7 +651,7 @@ static int si_cs_process(struct conn_stream *cs)
 int si_cs_send(struct conn_stream *cs)
 {
        struct connection *conn = cs_conn(cs);
-       struct stream_interface *si = cs->data;
+       struct stream_interface *si = cs_si(cs);
        struct channel *oc = si_oc(si);
        int ret;
        int did_send = 0;
@@ -1220,7 +1219,7 @@ static void stream_int_chk_snd_conn(struct stream_interface *si)
 int si_cs_recv(struct conn_stream *cs)
 {
        struct connection *conn = cs_conn(cs);
-       struct stream_interface *si = cs->data;
+       struct stream_interface *si = cs_si(cs);
        struct channel *ic = si_ic(si);
        int ret, max, cur_read = 0;
        int read_poll = MAX_READ_POLL_LOOPS;
index 5fec53e2184fe6bf016844e9dba5ea106f0caad9..3697f41f635731dddd3c8497d37eed0c4a998c87 100644 (file)
@@ -1093,7 +1093,7 @@ enum tcpcheck_eval_ret tcpcheck_eval_connect(struct check *check, struct tcpchec
        /* No connection, prepare a new one */
        conn = conn_new((s ? &s->obj_type : &proxy->obj_type));
        if (conn)
-               cs = cs_new(&conn->obj_type);
+               cs = cs_new(&conn->obj_type, conn, &check->obj_type, NULL, &check_conn_cb);
        if (!conn || !cs) {
                chunk_printf(&trash, "TCPCHK error allocating connection at step %d",
                             tcpcheck_get_step_id(check, rule));
@@ -1166,8 +1166,6 @@ enum tcpcheck_eval_ret tcpcheck_eval_connect(struct check *check, struct tcpchec
                goto fail_check;
        }
 
-       cs_attach(cs, check, &check_conn_cb);
-
        if ((connect->options & TCPCHK_OPT_SOCKS4) && s && (s->flags & SRV_F_SOCKS4_PROXY)) {
                conn->send_proxy_ofs = 1;
                conn->flags |= CO_FL_SOCKS4;