blob: 31979d881ea2b58367c69b88b7dc415b8e32c79a [file] [log] [blame]
Christopher Faulet1329f2a2021-12-16 17:32:56 +01001/*
2 * Conn-stream management functions
3 *
4 * Copyright 2021 Christopher Faulet <cfaulet@haproxy.com>
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 <haproxy/api.h>
14#include <haproxy/connection.h>
15#include <haproxy/conn_stream.h>
16#include <haproxy/pool.h>
Christopher Fauletcda94ac2021-12-23 17:28:17 +010017#include <haproxy/stream_interface.h>
Christopher Faulet1329f2a2021-12-16 17:32:56 +010018
19DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010020DECLARE_POOL(pool_head_cs_endpoint, "cs_endpoint", sizeof(struct cs_endpoint));
Christopher Faulet1329f2a2021-12-16 17:32:56 +010021
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010022void cs_endpoint_init(struct cs_endpoint *endp)
23{
24 endp->target = NULL;
25 endp->ctx = NULL;
26 endp->flags = CS_EP_NONE;
27}
28
29struct cs_endpoint *cs_endpoint_new()
30{
31 struct cs_endpoint *endp;
32
33 endp = pool_alloc(pool_head_cs_endpoint);
34 if (unlikely(!endp))
35 return NULL;
36
37 cs_endpoint_init(endp);
38 return endp;
39}
40
41void cs_endpoint_free(struct cs_endpoint *endp)
42{
43 pool_free(pool_head_cs_endpoint, endp);
44}
Christopher Faulet1329f2a2021-12-16 17:32:56 +010045
Christopher Fauletdd2d0d82021-12-20 09:34:32 +010046/* Tries to allocate a new conn_stream and initialize its main fields. On
47 * failure, nothing is allocated and NULL is returned.
Christopher Faulet1329f2a2021-12-16 17:32:56 +010048 */
Christopher Fauletb669d682022-03-22 18:37:19 +010049struct conn_stream *cs_new(struct cs_endpoint *endp)
Christopher Faulet1329f2a2021-12-16 17:32:56 +010050{
51 struct conn_stream *cs;
52
53 cs = pool_alloc(pool_head_connstream);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010054
Christopher Faulet1329f2a2021-12-16 17:32:56 +010055 if (unlikely(!cs))
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010056 goto alloc_error;
Christopher Fauletbb772d02022-03-22 15:28:36 +010057
58 cs->obj_type = OBJ_TYPE_CS;
59 cs->flags = CS_FL_NONE;
Christopher Faulet62e75742022-03-31 09:16:34 +020060 cs->state = CS_ST_INI;
Christopher Faulet1d987772022-03-29 18:03:35 +020061 cs->hcto = TICK_ETERNITY;
Christopher Fauletbb772d02022-03-22 15:28:36 +010062 cs->app = NULL;
Christopher Fauletbb772d02022-03-22 15:28:36 +010063 cs->si = NULL;
64 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +020065 cs->src = NULL;
66 cs->dst = NULL;
Christopher Fauletb669d682022-03-22 18:37:19 +010067 if (!endp) {
68 endp = cs_endpoint_new();
69 if (unlikely(!endp))
70 goto alloc_error;
71 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010072 cs->endp = endp;
73
Christopher Faulet1329f2a2021-12-16 17:32:56 +010074 return cs;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +010075
76 alloc_error:
77 pool_free(pool_head_connstream, cs);
78 return NULL;
Christopher Faulet1329f2a2021-12-16 17:32:56 +010079}
80
Christopher Fauleta9e8b392022-03-23 11:01:09 +010081struct conn_stream *cs_new_from_mux(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
82{
83 struct conn_stream *cs;
84
85 cs = cs_new(endp);
86 if (unlikely(!cs))
87 return NULL;
88 if (unlikely(!stream_new(sess, cs, input))) {
89 pool_free(pool_head_connstream, cs);
90 cs = NULL;
91 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +010092 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +010093 return cs;
94}
95
96struct conn_stream *cs_new_from_applet(struct cs_endpoint *endp, struct session *sess, struct buffer *input)
97{
98 struct conn_stream *cs;
99 struct appctx *appctx = endp->ctx;
100
101 cs = cs_new(endp);
102 if (unlikely(!cs))
103 return NULL;
104 appctx->owner = cs;
105 if (unlikely(!stream_new(sess, cs, input))) {
106 pool_free(pool_head_connstream, cs);
107 cs = NULL;
108 }
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100109 endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100110 return cs;
111}
112
113struct conn_stream *cs_new_from_strm(struct stream *strm, unsigned int flags)
114{
115 struct conn_stream *cs;
116
117 cs = cs_new(NULL);
118 if (unlikely(!cs))
119 return NULL;
120 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100121 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100122 cs->si = si_new(cs);
123 if (unlikely(!cs->si)) {
124 cs_free(cs);
125 return NULL;
126 }
127 cs->app = &strm->obj_type;
128 cs->si->ops = &si_embedded_ops;
129 cs->data_cb = NULL;
130 return cs;
131}
132
133struct conn_stream *cs_new_from_check(struct check *check, unsigned int flags)
134{
135 struct conn_stream *cs;
136
137 cs = cs_new(NULL);
138 if (unlikely(!cs))
139 return NULL;
140 cs->flags |= flags;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100141 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100142 cs->app = &check->obj_type;
143 cs->data_cb = &check_conn_cb;
144 return cs;
145}
146
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100147/* Releases a conn_stream previously allocated by cs_new(), as well as any
148 * buffer it would still hold.
149 */
150void cs_free(struct conn_stream *cs)
151{
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100152 si_free(cs->si);
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200153 sockaddr_free(&cs->src);
154 sockaddr_free(&cs->dst);
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100155 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100156 BUG_ON(!(cs->endp->flags & CS_EP_DETACHED));
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100157 cs_endpoint_free(cs->endp);
158 }
Christopher Faulet1329f2a2021-12-16 17:32:56 +0100159 pool_free(pool_head_connstream, cs);
160}
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100161
162
Christopher Faulet93882042022-01-19 14:56:50 +0100163/* Attaches a conn_stream to an mux endpoint and sets the endpoint ctx */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100164void cs_attach_mux(struct conn_stream *cs, void *target, void *ctx)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100165{
Christopher Faulet93882042022-01-19 14:56:50 +0100166 struct connection *conn = ctx;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100167
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100168 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100169 cs->endp->ctx = ctx;
170 cs->endp->flags |= CS_EP_T_MUX;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100171 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100172 if (!conn->ctx)
173 conn->ctx = cs;
174 if (cs_strm(cs)) {
175 cs->si->ops = &si_conn_ops;
176 cs->data_cb = &si_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100177 }
Christopher Faulet93882042022-01-19 14:56:50 +0100178 else if (cs_check(cs))
179 cs->data_cb = &check_conn_cb;
Christopher Faulet93882042022-01-19 14:56:50 +0100180}
181
182/* Attaches a conn_stream to an applet endpoint and sets the endpoint ctx */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100183void cs_attach_applet(struct conn_stream *cs, void *target, void *ctx)
Christopher Faulet93882042022-01-19 14:56:50 +0100184{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100185 struct appctx *appctx = target;
Christopher Faulet93882042022-01-19 14:56:50 +0100186
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100187 cs->endp->target = target;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100188 cs->endp->ctx = ctx;
189 cs->endp->flags |= CS_EP_T_APPLET;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100190 cs->endp->flags &= ~CS_EP_DETACHED;
Christopher Faulet93882042022-01-19 14:56:50 +0100191 appctx->owner = cs;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100192 if (cs_strm(cs)) {
Christopher Faulet93882042022-01-19 14:56:50 +0100193 cs->si->ops = &si_applet_ops;
194 cs->data_cb = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100195 }
196}
197
198/* Attaches a conn_stream to a app layer and sets the relevant callbacks */
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100199int cs_attach_strm(struct conn_stream *cs, struct stream *strm)
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100200{
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100201 cs->app = &strm->obj_type;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100202
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100203 cs->si = si_new(cs);
204 if (unlikely(!cs->si))
205 return -1;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100206 cs->endp->flags &= ~CS_EP_ORPHAN;
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100207 if (cs->endp->flags & CS_EP_T_MUX) {
208 cs->si->ops = &si_conn_ops;
209 cs->data_cb = &si_conn_cb;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100210 }
Christopher Fauleta9e8b392022-03-23 11:01:09 +0100211 else if (cs->endp->flags & CS_EP_T_APPLET) {
212 cs->si->ops = &si_applet_ops;
213 cs->data_cb = NULL;
214 }
215 else {
216 cs->si->ops = &si_embedded_ops;
217 cs->data_cb = NULL;
218 }
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100219 return 0;
220}
221
222/* Detach the conn_stream from the endpoint, if any. For a connecrion, if a mux
223 * owns the connection ->detach() callback is called. Otherwise, it means the
224 * conn-stream owns the connection. In this case the connection is closed and
225 * released. For an applet, the appctx is released. At the end, the conn-stream
226 * is not released but some fields a reset.
227 */
228void cs_detach_endp(struct conn_stream *cs)
229{
Christopher Fauletb041b232022-03-24 10:27:02 +0100230 if (!cs->endp)
231 goto reset_cs;
232
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100233 if (cs->endp->flags & CS_EP_T_MUX) {
234 struct connection *conn = cs_conn(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100235
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100236 if (conn->mux) {
Christopher Faulet54e85cb2022-01-06 08:46:56 +0100237 /* TODO: handle unsubscribe for healthchecks too */
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100238 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100239 if (cs->si && cs->si->wait_event.events != 0)
240 conn->mux->unsubscribe(cs, cs->si->wait_event.events, &cs->si->wait_event);
241 conn->mux->detach(cs);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100242 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100243 }
244 else {
245 /* It's too early to have a mux, let's just destroy
246 * the connection
247 */
248 conn_stop_tracking(conn);
249 conn_full_close(conn);
250 if (conn->destroy_cb)
251 conn->destroy_cb(conn);
252 conn_free(conn);
253 }
254 }
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100255 else if (cs->endp->flags & CS_EP_T_APPLET) {
256 struct appctx *appctx = cs_appctx(cs);
257
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100258 cs->endp->flags |= CS_EP_ORPHAN;
Christopher Faulet9affa932022-03-15 11:29:59 +0100259 if (cs->si)
260 si_applet_release(cs->si);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100261 appctx_free(appctx);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100262 cs->endp = NULL;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100263 }
264
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100265 if (cs->endp) {
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100266 /* the cs is the only one one the endpoint */
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100267 cs_endpoint_init(cs->endp);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100268 cs->endp->flags |= CS_EP_DETACHED;
Christopher Fauletdb90f2a2022-03-22 16:06:25 +0100269 }
270
Christopher Fauletb041b232022-03-24 10:27:02 +0100271 reset_cs:
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100272 /* FIXME: Rest CS for now but must be reviewed. CS flags are only
273 * connection related for now but this will evolved
274 */
Christopher Faulet30995112022-03-25 15:32:38 +0100275 cs->flags &= CS_FL_ISBACK;
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100276 if (cs->si)
277 cs->si->ops = &si_embedded_ops;
278 cs->data_cb = NULL;
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100279
280 if (cs->app == NULL)
281 cs_free(cs);
282}
283
284void cs_detach_app(struct conn_stream *cs)
285{
286 si_free(cs->si);
287 cs->app = NULL;
288 cs->si = NULL;
289 cs->data_cb = NULL;
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200290 sockaddr_free(&cs->src);
291 sockaddr_free(&cs->dst);
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100292 if (!cs->endp || (cs->endp->flags & CS_EP_DETACHED))
Christopher Fauletc36de9d2022-01-06 08:44:58 +0100293 cs_free(cs);
Christopher Fauletcda94ac2021-12-23 17:28:17 +0100294}
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100295
296int cs_reset_endp(struct conn_stream *cs)
297{
Christopher Fauletb041b232022-03-24 10:27:02 +0100298 struct cs_endpoint *new_endp;
299
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100300 BUG_ON(!cs->app);
Christopher Fauletb041b232022-03-24 10:27:02 +0100301 if (!__cs_endp_target(cs)) {
302 /* endpoint not attached or attached to a mux with no
303 * target. Thus the endpoint will not be release but just
304 * reset
305 */
306 cs_detach_endp(cs);
307 return 0;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100308 }
Christopher Fauletb041b232022-03-24 10:27:02 +0100309
310 /* allocate the new endpoint first to be able to set error if it
311 * fails */
312 new_endp = cs_endpoint_new();
313 if (!unlikely(new_endp)) {
314 cs->endp->flags |= CS_EP_ERROR;
315 return -1;
316 }
317
318 cs_detach_endp(cs);
319 BUG_ON(cs->endp);
320 cs->endp = new_endp;
321 cs->endp->flags |= CS_EP_DETACHED;
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +0100322 return 0;
323}