blob: 5637741089e84b8c9dbc5f25cdca4a6b0df031b3 [file] [log] [blame]
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +01001/*
2 * general purpose event handlers management
3 *
4 * Copyright 2022 HAProxy Technologies
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.1 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <string.h>
14#include <haproxy/event_hdl.h>
15#include <haproxy/compiler.h>
16#include <haproxy/task.h>
17#include <haproxy/tools.h>
18#include <haproxy/errors.h>
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +010019#include <haproxy/signal.h>
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010020#include <haproxy/xxhash.h>
21
22/* event types changes in event_hdl-t.h file should be reflected in the
23 * map below to allow string to type and type to string conversions
24 */
25static struct event_hdl_sub_type_map event_hdl_sub_type_map[] = {
26 {"NONE", EVENT_HDL_SUB_NONE},
27 {"SERVER", EVENT_HDL_SUB_SERVER},
28 {"SERVER_ADD", EVENT_HDL_SUB_SERVER_ADD},
29 {"SERVER_DEL", EVENT_HDL_SUB_SERVER_DEL},
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +010030 {"SERVER_UP", EVENT_HDL_SUB_SERVER_UP},
31 {"SERVER_DOWN", EVENT_HDL_SUB_SERVER_DOWN},
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010032};
33
34/* internal types (only used in this file) */
35struct event_hdl_async_task_default_ctx
36{
37 event_hdl_async_equeue e_queue; /* event queue list */
38 event_hdl_cb_async func; /* event handling func */
39};
40
41/* memory pools declarations */
42DECLARE_STATIC_POOL(pool_head_sub, "ehdl_sub", sizeof(struct event_hdl_sub));
43DECLARE_STATIC_POOL(pool_head_sub_event, "ehdl_sub_e", sizeof(struct event_hdl_async_event));
44DECLARE_STATIC_POOL(pool_head_sub_event_data, "ehdl_sub_ed", sizeof(struct event_hdl_async_event_data));
45DECLARE_STATIC_POOL(pool_head_sub_taskctx, "ehdl_sub_tctx", sizeof(struct event_hdl_async_task_default_ctx));
46
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010047/* TODO: will become a config tunable
48 * ie: tune.events.max-async-notif-at-once
49 */
50static int event_hdl_async_max_notif_at_once = 10;
51
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010052/* global subscription list (implicit where NULL is used as sublist argument) */
53static event_hdl_sub_list global_event_hdl_sub_list;
54
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +010055/* every known subscription lists are tracked in this list (including the global one) */
56static struct mt_list known_event_hdl_sub_list = MT_LIST_HEAD_INIT(known_event_hdl_sub_list);
57
58static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list);
59
60static void event_hdl_deinit(struct sig_handler *sh)
61{
62 event_hdl_sub_list *cur_list;
63 struct mt_list *elt1, elt2;
64
65 /* destroy all known subscription lists */
66 mt_list_for_each_entry_safe(cur_list, &known_event_hdl_sub_list, known, elt1, elt2) {
67 /* remove cur elem from list */
68 MT_LIST_DELETE_SAFE(elt1);
69 /* then destroy it */
70 _event_hdl_sub_list_destroy(cur_list);
71 }
72}
73
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010074static void event_hdl_init(void)
75{
76 /* initialize global subscription list */
77 event_hdl_sub_list_init(&global_event_hdl_sub_list);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +010078 /* register the deinit function, will be called on soft-stop */
79 signal_register_fct(0, event_hdl_deinit, 0);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010080}
81
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010082/* general purpose hashing function when you want to compute
83 * an ID based on <scope> x <name>
84 * It is your responsibility to make sure <scope> is not used
85 * elsewhere in the code (or that you are fine with sharing
86 * the scope).
87 */
88inline uint64_t event_hdl_id(const char *scope, const char *name)
89{
90 XXH64_state_t state;
91
92 XXH64_reset(&state, 0);
93 XXH64_update(&state, scope, strlen(scope));
94 XXH64_update(&state, name, strlen(name));
95 return XXH64_digest(&state);
96}
97
98/* takes a sub_type as input, returns corresponding sub_type
99 * printable string or "N/A" if not found.
100 * If not found, an error will be reported to stderr so the developers
101 * know that a sub_type is missing its associated string in event_hdl-t.h
102 */
103const char *event_hdl_sub_type_to_string(struct event_hdl_sub_type sub_type)
104{
105 int it;
106
107 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
108 if (sub_type.family == event_hdl_sub_type_map[it].type.family &&
109 sub_type.subtype == event_hdl_sub_type_map[it].type.subtype)
110 return event_hdl_sub_type_map[it].name;
111 }
112 ha_alert("event_hdl-t.h: missing sub_type string representation.\n"
113 "Please reflect any changes in event_hdl_sub_type_map.\n");
114 return "N/A";
115}
116
117/* returns the internal sub_type corresponding
118 * to the printable representation <name>
119 * or EVENT_HDL_SUB_NONE if no such event exists
120 * (see event_hdl-t.h for the complete list of supported types)
121 */
122struct event_hdl_sub_type event_hdl_string_to_sub_type(const char *name)
123{
124 int it;
125
126 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
127 if (!strcmp(name, event_hdl_sub_type_map[it].name))
128 return event_hdl_sub_type_map[it].type;
129 }
130 return EVENT_HDL_SUB_NONE;
131}
132
133/* Takes <subscriptions> sub list as input, returns a printable string
134 * containing every sub_types contained in <subscriptions>
135 * separated by '|' char.
136 * Returns NULL if no sub_types are found in <subscriptions>
137 * This functions leverages memprintf, thus it is up to the
138 * caller to free the returned value (if != NULL) when he no longer
139 * uses it.
140 */
141char *event_hdl_sub_type_print(struct event_hdl_sub_type subscriptions)
142{
143 char *out = NULL;
144 int it;
145 uint8_t first = 1;
146
147 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
148 if (subscriptions.family == event_hdl_sub_type_map[it].type.family &&
149 ((subscriptions.subtype & event_hdl_sub_type_map[it].type.subtype) ==
150 event_hdl_sub_type_map[it].type.subtype)) {
151 if (first) {
152 memprintf(&out, "%s", event_hdl_sub_type_map[it].name);
153 first--;
154 }
155 else
156 memprintf(&out, "%s%s%s", out, "|", event_hdl_sub_type_map[it].name);
157 }
158 }
159
160 return out;
161}
162
163/* event_hdl debug/reporting function */
164typedef void (*event_hdl_report_hdl_state_func)(const char *fmt, ...);
165static void event_hdl_report_hdl_state(event_hdl_report_hdl_state_func report_func,
166 const struct event_hdl *hdl, const char *what, const char *state)
167{
168 report_func("[event_hdl]:%s (%s)'#%llu@%s': %s\n",
169 what,
170 (hdl->async) ? "ASYNC" : "SYNC",
171 (long long unsigned int)hdl->id,
172 hdl->dorigin,
173 state);
174}
175
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100176static inline void _event_hdl_async_data_drop(struct event_hdl_async_event_data *data)
177{
178 if (HA_ATOMIC_SUB_FETCH(&data->refcount, 1) == 0) {
179 /* we were the last one holding a reference to event data - free required */
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100180 if (data->mfree) {
181 /* Some event data members are dynamically allocated and thus
182 * require specific cleanup using user-provided function.
183 * We directly pass a pointer to internal data storage but
184 * we only expect the cleanup function to typecast it in the
185 * relevant data type to give enough context to the function to
186 * perform the cleanup on data members, and not actually freeing
187 * data pointer since it is our internal buffer :)
188 */
189 data->mfree(&data->data);
190 }
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100191 pool_free(pool_head_sub_event_data, data);
192 }
193}
194
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100195void event_hdl_async_free_event(struct event_hdl_async_event *e)
196{
197 if (unlikely(event_hdl_sub_type_equal(e->type, EVENT_HDL_SUB_END))) {
198 /* last event for hdl, special case */
199 /* free subscription entry as we're the last one still using it
200 * (it is already removed from mt_list, no race can occur)
201 */
202 event_hdl_drop(e->sub_mgmt.this);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100203 HA_ATOMIC_DEC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100204 }
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100205 else if (e->_data)
206 _event_hdl_async_data_drop(e->_data); /* data wrapper */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100207 pool_free(pool_head_sub_event, e);
208}
209
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100210/* wakeup the task depending on its type:
211 * normal async mode internally uses tasklets but advanced async mode
212 * allows both tasks and tasklets.
213 * While tasks and tasklets may be easily casted, we need to use the proper
214 * API to wake them up (the waiting queues are exclusive).
215 */
216static void event_hdl_task_wakeup(struct tasklet *task)
217{
218 if (TASK_IS_TASKLET(task))
219 tasklet_wakeup(task);
220 else
221 task_wakeup((struct task *)task, TASK_WOKEN_OTHER); /* TODO: switch to TASK_WOKEN_EVENT? */
222}
223
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100224/* task handler used for normal async subscription mode
225 * if you use advanced async subscription mode, you can use this
226 * as an example to implement your own task wrapper
227 */
228static struct task *event_hdl_async_task_default(struct task *task, void *ctx, unsigned int state)
229{
230 struct tasklet *tl = (struct tasklet *)task;
231 struct event_hdl_async_task_default_ctx *task_ctx = ctx;
232 struct event_hdl_async_event *event;
233 int max_notif_at_once_it = 0;
234 uint8_t done = 0;
235
236 /* run through e_queue, and call func() for each event
237 * if we read END event, it indicates we must stop:
238 * no more events to come (handler is unregistered)
239 * so we must free task_ctx and stop task
240 */
241 while (max_notif_at_once_it < event_hdl_async_max_notif_at_once &&
242 (event = event_hdl_async_equeue_pop(&task_ctx->e_queue)))
243 {
244 if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
245 done = 1;
246 event_hdl_async_free_event(event);
247 /* break is normally not even required, EVENT_HDL_SUB_END
248 * is guaranteed to be last event of e_queue
249 * (because in normal mode one sub == one e_queue)
250 */
251 break;
252 }
253 else {
254 struct event_hdl_cb cb;
255
256 cb.e_type = event->type;
257 cb.e_data = event->data;
258 cb.sub_mgmt = &event->sub_mgmt;
259 cb._sync = 0;
260
261 /* call user function */
262 task_ctx->func(&cb, event->private);
263 max_notif_at_once_it++;
264 }
265 event_hdl_async_free_event(event);
266 }
267
268 if (done) {
269 /* our job is done, subscription is over: no more events to come */
270 pool_free(pool_head_sub_taskctx, task_ctx);
271 tasklet_free(tl);
272 return NULL;
273 }
274 return task;
275}
276
277/* internal subscription mgmt functions */
278static inline struct event_hdl_sub_type _event_hdl_getsub(struct event_hdl_sub *cur_sub)
279{
280 return cur_sub->sub;
281}
282
283static inline struct event_hdl_sub_type _event_hdl_getsub_async(struct event_hdl_sub *cur_sub)
284{
285 struct mt_list lock;
286 struct event_hdl_sub_type type = EVENT_HDL_SUB_NONE;
287
288 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
289 if (lock.next != &cur_sub->mt_list)
290 type = _event_hdl_getsub(cur_sub);
291 // else already removed
292 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
293 return type;
294}
295
296static inline int _event_hdl_resub(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
297{
298 if (!event_hdl_sub_family_equal(cur_sub->sub, type))
299 return 0; /* family types differ, do nothing */
300 cur_sub->sub.subtype = type.subtype; /* new subtype assignment */
301 return 1;
302}
303
304static inline int _event_hdl_resub_async(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
305{
306 int status = 0;
307 struct mt_list lock;
308
309 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
310 if (lock.next != &cur_sub->mt_list)
311 status = _event_hdl_resub(cur_sub, type);
312 // else already removed
313 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
314 return status;
315}
316
317static inline void _event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
318{
319 struct mt_list lock;
320
321 if (del_sub->hdl.async) {
322 /* ASYNC SUB MODE */
323 /* push EVENT_HDL_SUB_END (to notify the task that the subscription is dead) */
324
325 /* push END EVENT in busy state so we can safely wakeup
326 * the task before releasing it.
327 * Not doing that would expose us to a race where the task could've already
328 * consumed the END event before the wakeup, and some tasks
329 * kill themselves (ie: normal async mode) when they receive such event
330 */
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100331 HA_ATOMIC_INC(&del_sub->hdl.async_equeue->size);
332 lock = MT_LIST_APPEND_LOCKED(&del_sub->hdl.async_equeue->head, &del_sub->async_end->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100333
334 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100335 event_hdl_task_wakeup(del_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100336
337 /* unlock END EVENT (we're done, the task is now free to consume it) */
338 MT_LIST_UNLOCK_ELT(&del_sub->async_end->mt_list, lock);
339
340 /* we don't free sub here
341 * freeing will be performed by async task so it can safely rely
342 * on the pointer until it notices it
343 */
344 } else {
345 /* SYNC SUB MODE */
346
347 /* we can directly free the subscription:
348 * no other thread can access it since we successfully
349 * removed it from the list
350 */
351 event_hdl_drop(del_sub);
352 }
353}
354
355static inline void _event_hdl_unsubscribe_async(struct event_hdl_sub *del_sub)
356{
357 if (!MT_LIST_DELETE(&del_sub->mt_list))
358 return; /* already removed (but may be pending in e_queues) */
359 _event_hdl_unsubscribe(del_sub);
360}
361
362/* sub_mgmt function pointers (for handlers) */
363static struct event_hdl_sub_type event_hdl_getsub_sync(const struct event_hdl_sub_mgmt *mgmt)
364{
365 if (!mgmt)
366 return EVENT_HDL_SUB_NONE;
367
368 if (!mgmt->this)
369 return EVENT_HDL_SUB_NONE; /* already removed from sync ctx */
370 return _event_hdl_getsub(mgmt->this);
371}
372
373static struct event_hdl_sub_type event_hdl_getsub_async(const struct event_hdl_sub_mgmt *mgmt)
374{
375 if (!mgmt)
376 return EVENT_HDL_SUB_NONE;
377
378 return _event_hdl_getsub_async(mgmt->this);
379}
380
381static int event_hdl_resub_sync(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
382{
383 if (!mgmt)
384 return 0;
385
386 if (!mgmt->this)
387 return 0; /* already removed from sync ctx */
388 return _event_hdl_resub(mgmt->this, type);
389}
390
391static int event_hdl_resub_async(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
392{
393 if (!mgmt)
394 return 0;
395
396 return _event_hdl_resub_async(mgmt->this, type);
397}
398
399static void event_hdl_unsubscribe_sync(const struct event_hdl_sub_mgmt *mgmt)
400{
401 if (!mgmt)
402 return;
403
404 if (!mgmt->this)
405 return; /* already removed from sync ctx */
406
407 /* assuming that publish sync code will notice that mgmt->this is NULL
408 * and will perform the list removal using MT_LIST_DELETE_SAFE and
409 * _event_hdl_unsubscribe()
410 * while still owning the lock
411 */
412 ((struct event_hdl_sub_mgmt *)mgmt)->this = NULL;
413}
414
415static void event_hdl_unsubscribe_async(const struct event_hdl_sub_mgmt *mgmt)
416{
417 if (!mgmt)
418 return;
419
420 _event_hdl_unsubscribe_async(mgmt->this);
421}
422
423#define EVENT_HDL_SUB_MGMT_ASYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
424 .getsub = event_hdl_getsub_async, \
425 .resub = event_hdl_resub_async, \
426 .unsub = event_hdl_unsubscribe_async}
427#define EVENT_HDL_SUB_MGMT_SYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
428 .getsub = event_hdl_getsub_sync, \
429 .resub = event_hdl_resub_sync, \
430 .unsub = event_hdl_unsubscribe_sync}
431
432struct event_hdl_sub *event_hdl_subscribe_ptr(event_hdl_sub_list *sub_list,
433 struct event_hdl_sub_type e_type, struct event_hdl hdl)
434{
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100435 struct event_hdl_sub *new_sub = NULL;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100436 struct mt_list *elt1, elt2;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100437 struct event_hdl_async_task_default_ctx *task_ctx = NULL;
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100438 struct mt_list lock;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100439
440 if (!sub_list)
441 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
442
443 /* hdl API consistency check */
444 /*FIXME: do we need to ensure that if private is set, private_free should be set as well? */
445 BUG_ON((!hdl.async && !hdl.sync_ptr) ||
446 (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL && !hdl.async_ptr) ||
447 (hdl.async == EVENT_HDL_ASYNC_MODE_ADVANCED &&
448 (!hdl.async_equeue || !hdl.async_task)));
449
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100450 new_sub = pool_alloc(pool_head_sub);
451 if (new_sub == NULL) {
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100452 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100453 }
454
455 /* assignments */
456 new_sub->sub.family = e_type.family;
457 new_sub->sub.subtype = e_type.subtype;
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100458 new_sub->flags = 0;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100459 new_sub->hdl = hdl;
460
461 if (hdl.async) {
462 /* async END event pre-allocation */
463 new_sub->async_end = pool_alloc(pool_head_sub_event);
464 if (!new_sub->async_end) {
465 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100466 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100467 }
468 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
469 /* normal mode: no task provided, we must initialize it */
470
471 /* initialize task context */
472 task_ctx = pool_alloc(pool_head_sub_taskctx);
473
474 if (!task_ctx) {
475 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100476 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100477 }
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100478 event_hdl_async_equeue_init(&task_ctx->e_queue);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100479 task_ctx->func = new_sub->hdl.async_ptr;
480
481 new_sub->hdl.async_equeue = &task_ctx->e_queue;
482 new_sub->hdl.async_task = tasklet_new();
483
484 if (!new_sub->hdl.async_task) {
485 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100486 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100487 }
488 new_sub->hdl.async_task->context = task_ctx;
489 new_sub->hdl.async_task->process = event_hdl_async_task_default;
490 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100491 /* initialize END event (used to notify about subscription ending)
492 * used by both normal and advanced mode:
493 * - to safely terminate the task in normal mode
494 * - to safely free subscription and
495 * keep track of active subscriptions in advanced mode
496 */
497 new_sub->async_end->type = EVENT_HDL_SUB_END;
498 new_sub->async_end->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(new_sub);
499 new_sub->async_end->private = new_sub->hdl.private;
500 new_sub->async_end->_data = NULL;
501 MT_LIST_INIT(&new_sub->async_end->mt_list);
502 }
503 /* set refcount to 2:
504 * 1 for handler (because handler can manage the subscription itself)
505 * 1 for caller (will be dropped automatically if caller use the non-ptr version)
506 */
507 new_sub->refcount = 2;
508
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100509 /* ready for registration */
510 MT_LIST_INIT(&new_sub->mt_list);
511
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100512 lock = MT_LIST_LOCK_ELT(&sub_list->known);
513
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100514 /* check if such identified hdl is not already registered */
515 if (hdl.id) {
516 struct event_hdl_sub *cur_sub;
517 uint8_t found = 0;
518
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100519 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
520 if (hdl.id == cur_sub->hdl.id) {
521 /* we found matching registered hdl */
522 found = 1;
523 break;
524 }
525 }
526 if (found) {
527 /* error already registered */
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100528 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100529 event_hdl_report_hdl_state(ha_alert, &hdl, "SUB", "could not subscribe: subscription with this id already exists");
530 goto cleanup;
531 }
532 }
533
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100534 if (lock.next == &sub_list->known) {
535 /* this is an expected corner case on de-init path, a subscribe attempt
536 * was made but the subscription list is already destroyed, we pretend
537 * it is a memory/IO error since it should not be long before haproxy
538 * enters the deinit() function anyway
539 */
540 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
541 goto cleanup;
542 }
543
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100544 /* Append in list (global or user specified list).
545 * For now, append when sync mode, and insert when async mode
546 * so that async handlers are executed first
547 */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100548 if (hdl.async) {
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100549 /* Prevent the task from being aborted on soft-stop: let's wait
550 * until the END event is acknowledged by the task.
551 * (decrease is performed in event_hdl_async_free_event())
552 *
553 * If we don't do this, event_hdl API will leak and we won't give
554 * a chance to the event-handling task to perform cleanup
555 */
556 HA_ATOMIC_INC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100557 /* async mode, insert at the beginning of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100558 MT_LIST_INSERT(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100559 } else {
560 /* sync mode, append at the end of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100561 MT_LIST_APPEND(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100562 }
563
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100564 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100565
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100566 return new_sub;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100567
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100568 cleanup:
569 if (new_sub) {
570 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200571 tasklet_free(new_sub->hdl.async_task);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100572 if (task_ctx)
573 pool_free(pool_head_sub_taskctx, task_ctx);
574 }
575 if (hdl.async)
576 pool_free(pool_head_sub_event, new_sub->async_end);
577 pool_free(pool_head_sub, new_sub);
578 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100579
580 return NULL;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100581
582 memory_error:
583 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
584 goto cleanup;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100585}
586
587void event_hdl_take(struct event_hdl_sub *sub)
588{
589 HA_ATOMIC_INC(&sub->refcount);
590}
591
592void event_hdl_drop(struct event_hdl_sub *sub)
593{
594 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
595 return;
596
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100597 /* we were the last one holding a reference to event sub - free required */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100598 if (sub->hdl.private_free) {
599 /* free private data if specified upon registration */
600 sub->hdl.private_free(sub->hdl.private);
601 }
602 pool_free(pool_head_sub, sub);
603}
604
605int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
606{
607 return _event_hdl_resub_async(cur_sub, type);
608}
609
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100610void _event_hdl_pause(struct event_hdl_sub *cur_sub)
611{
612 cur_sub->flags |= EHDL_SUB_F_PAUSED;
613}
614
615void event_hdl_pause(struct event_hdl_sub *cur_sub)
616{
617 struct mt_list lock;
618
619 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
620 if (lock.next != &cur_sub->mt_list)
621 _event_hdl_pause(cur_sub);
622 // else already removed
623 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
624}
625
626void _event_hdl_resume(struct event_hdl_sub *cur_sub)
627{
628 cur_sub->flags &= ~EHDL_SUB_F_PAUSED;
629}
630
631void event_hdl_resume(struct event_hdl_sub *cur_sub)
632{
633 struct mt_list lock;
634
635 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
636 if (lock.next != &cur_sub->mt_list)
637 _event_hdl_resume(cur_sub);
638 // else already removed
639 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
640}
641
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100642void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
643{
644 _event_hdl_unsubscribe_async(del_sub);
645 /* drop refcount, assuming caller no longer use ptr */
646 event_hdl_drop(del_sub);
647}
648
649int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
650{
651 struct event_hdl_sub *sub;
652
653 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
654 if (sub) {
655 /* drop refcount because the user is not willing to hold a reference */
656 event_hdl_drop(sub);
657 return 1;
658 }
659 return 0;
660}
661
662/* Subscription external lookup functions
663 */
664int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
665 uint64_t lookup_id)
666{
667 struct event_hdl_sub *del_sub = NULL;
668 struct mt_list *elt1, elt2;
669 int found = 0;
670
671 if (!sub_list)
672 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
673
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100674 mt_list_for_each_entry_safe(del_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100675 if (lookup_id == del_sub->hdl.id) {
676 /* we found matching registered hdl */
677 MT_LIST_DELETE_SAFE(elt1);
678 _event_hdl_unsubscribe(del_sub);
679 found = 1;
680 break; /* id is unique, stop searching */
681 }
682 }
683 return found;
684}
685
686int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
687 uint64_t lookup_id, struct event_hdl_sub_type type)
688{
689 struct event_hdl_sub *cur_sub = NULL;
690 struct mt_list *elt1, elt2;
691 int status = 0;
692
693 if (!sub_list)
694 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
695
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100696 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100697 if (lookup_id == cur_sub->hdl.id) {
698 /* we found matching registered hdl */
699 status = _event_hdl_resub(cur_sub, type);
700 break; /* id is unique, stop searching */
701 }
702 }
703 return status;
704}
705
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100706int event_hdl_lookup_pause(event_hdl_sub_list *sub_list,
707 uint64_t lookup_id)
708{
709 struct event_hdl_sub *cur_sub = NULL;
710 struct mt_list *elt1, elt2;
711 int found = 0;
712
713 if (!sub_list)
714 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
715
716 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
717 if (lookup_id == cur_sub->hdl.id) {
718 /* we found matching registered hdl */
719 _event_hdl_pause(cur_sub);
720 found = 1;
721 break; /* id is unique, stop searching */
722 }
723 }
724 return found;
725}
726
727int event_hdl_lookup_resume(event_hdl_sub_list *sub_list,
728 uint64_t lookup_id)
729{
730 struct event_hdl_sub *cur_sub = NULL;
731 struct mt_list *elt1, elt2;
732 int found = 0;
733
734 if (!sub_list)
735 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
736
737 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
738 if (lookup_id == cur_sub->hdl.id) {
739 /* we found matching registered hdl */
740 _event_hdl_resume(cur_sub);
741 found = 1;
742 break; /* id is unique, stop searching */
743 }
744 }
745 return found;
746}
747
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100748struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
749 uint64_t lookup_id)
750{
751 struct event_hdl_sub *cur_sub = NULL;
752 struct mt_list *elt1, elt2;
753 uint8_t found = 0;
754
755 if (!sub_list)
756 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
757
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100758 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100759 if (lookup_id == cur_sub->hdl.id) {
760 /* we found matching registered hdl */
761 event_hdl_take(cur_sub);
762 found = 1;
763 break; /* id is unique, stop searching */
764 }
765 }
766 if (found)
767 return cur_sub;
768 return NULL;
769}
770
771/* event publishing functions
772 */
773static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
774 const struct event_hdl_cb_data *data)
775{
776 struct event_hdl_sub *cur_sub;
777 struct mt_list *elt1, elt2;
778 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
779 int error = 0;
780
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100781 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100782 /* notify each function that has subscribed to sub_family.type, unless paused */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100783 if ((cur_sub->sub.family == e_type.family) &&
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100784 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype) &&
785 !(cur_sub->flags & EHDL_SUB_F_PAUSED)) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100786 /* hdl should be notified */
787 if (!cur_sub->hdl.async) {
788 /* sync mode: simply call cb pointer
789 * it is up to the callee to schedule a task if needed or
790 * take specific precautions in order to return as fast as possible
791 * and not use locks that are already held by the caller
792 */
793 struct event_hdl_cb cb;
794 struct event_hdl_sub_mgmt sub_mgmt;
795
796 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
797 cb.e_type = e_type;
798 if (data)
799 cb.e_data = data->_ptr;
800 else
801 cb.e_data = NULL;
802 cb.sub_mgmt = &sub_mgmt;
803 cb._sync = 1;
804
805 /* call user function */
806 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
807
808 if (!sub_mgmt.this) {
809 /* user has performed hdl unsub
810 * we must remove it from the list
811 */
812 MT_LIST_DELETE_SAFE(elt1);
813 /* then free it */
814 _event_hdl_unsubscribe(cur_sub);
815 }
816 } else {
817 /* async mode: here we need to prepare event data
818 * and push it to the event_queue of the task(s)
819 * responsible for consuming the events of current
820 * subscription.
821 * Once the event is pushed, we wake up the associated task.
822 * This feature depends on <haproxy/task> that also
823 * depends on <haproxy/pool>:
824 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
825 * async handler, program may crash.
826 * Hopefully, STG_PREPARE+STG_POOL should be done early in
827 * HAProxy startup sequence.
828 */
829 struct event_hdl_async_event *new_event;
830
831 new_event = pool_alloc(pool_head_sub_event);
832 if (!new_event) {
833 error = 1;
834 break; /* stop on error */
835 }
836 new_event->type = e_type;
837 new_event->private = cur_sub->hdl.private;
Aurelien DARRAGONe9314fb2023-04-04 21:41:10 +0200838 new_event->when = date;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100839 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
840 if (data) {
841 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100842 * event_hdl-t.h file or consider providing dynamic struct members
843 * to reduce overall struct size
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100844 */
845 BUG_ON(data->_size > sizeof(async_data->data));
846 if (!async_data) {
847 /* first async hdl reached - preparing async_data cache */
848 async_data = pool_alloc(pool_head_sub_event_data);
849 if (!async_data) {
850 error = 1;
851 pool_free(pool_head_sub_event, new_event);
852 break; /* stop on error */
853 }
854
855 /* async data assignment */
856 memcpy(async_data->data, data->_ptr, data->_size);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100857 async_data->mfree = data->_mfree;
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100858 /* Initialize refcount, we start at 1 to prevent async
859 * data from being freed by an async handler while we
860 * still use it. We will drop the reference when the
861 * publish is over.
862 *
863 * (first use, atomic operation not required)
864 */
865 async_data->refcount = 1;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100866 }
867 new_event->_data = async_data;
868 new_event->data = async_data->data;
869 /* increment refcount because multiple hdls could
870 * use the same async_data
871 */
872 HA_ATOMIC_INC(&async_data->refcount);
873 } else
874 new_event->data = NULL;
875
876 /* appending new event to event hdl queue */
877 MT_LIST_INIT(&new_event->mt_list);
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100878 HA_ATOMIC_INC(&cur_sub->hdl.async_equeue->size);
879 MT_LIST_APPEND(&cur_sub->hdl.async_equeue->head, &new_event->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100880
881 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100882 event_hdl_task_wakeup(cur_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100883 } /* end async mode */
884 } /* end hdl should be notified */
885 } /* end mt_list */
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100886 if (async_data) {
887 /* we finished publishing, drop the reference on async data */
888 _event_hdl_async_data_drop(async_data);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100889 } else {
890 /* no async subscribers, we are responsible for calling the data
891 * member freeing function if it was provided
892 */
893 if (data && data->_mfree)
894 data->_mfree(data->_ptr);
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100895 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100896 if (error) {
897 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
898 return 0;
899 }
900 return 1;
901}
902
903/* Publish function should not be used from high calling rate or time sensitive
904 * places for now, because list lookup based on e_type is not optimized at
905 * all!
906 * Returns 1 in case of SUCCESS:
907 * Subscribed handlers were notified successfully
908 * Returns 0 in case of FAILURE:
909 * FAILURE means memory error while handling the very first async handler from
910 * the subscription list.
911 * As async handlers are executed first within the list, when such failure occurs
912 * you can safely assume that no events were published for the current call
913 */
914int event_hdl_publish(event_hdl_sub_list *sub_list,
915 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
916{
917 if (!e_type.family) {
918 /* do nothing, these types are reserved for internal use only
919 * (ie: unregistering) */
920 return 0;
921 }
922 if (sub_list) {
923 /* if sublist is provided, first publish event to list subscribers */
924 return _event_hdl_publish(sub_list, e_type, data);
925 } else {
926 /* publish to global list */
927 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
928 }
929}
930
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100931void event_hdl_sub_list_init(event_hdl_sub_list *sub_list)
932{
933 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
934 MT_LIST_INIT(&sub_list->head);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100935 MT_LIST_APPEND(&known_event_hdl_sub_list, &sub_list->known);
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100936}
937
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100938/* internal function, assumes that sub_list ptr is always valid */
939static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100940{
941 struct event_hdl_sub *cur_sub;
942 struct mt_list *elt1, elt2;
943
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100944 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100945 /* remove cur elem from list */
946 MT_LIST_DELETE_SAFE(elt1);
947 /* then free it */
948 _event_hdl_unsubscribe(cur_sub);
949 }
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100950}
951
952/* when a subscription list is no longer used, call this
953 * to do the cleanup and make sure all related subscriptions are
954 * safely ended according to their types
955 */
956void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
957{
958 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
959 if (!MT_LIST_DELETE(&sub_list->known))
960 return; /* already destroyed */
961 _event_hdl_sub_list_destroy(sub_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100962}
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100963
964INITCALL0(STG_INIT, event_hdl_init);