blob: 14aed7a2f5136d1b5aeca58ca76fa3044e79ecad [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 */
180 pool_free(pool_head_sub_event_data, data);
181 }
182}
183
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100184void event_hdl_async_free_event(struct event_hdl_async_event *e)
185{
186 if (unlikely(event_hdl_sub_type_equal(e->type, EVENT_HDL_SUB_END))) {
187 /* last event for hdl, special case */
188 /* free subscription entry as we're the last one still using it
189 * (it is already removed from mt_list, no race can occur)
190 */
191 event_hdl_drop(e->sub_mgmt.this);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100192 HA_ATOMIC_DEC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100193 }
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100194 else if (e->_data)
195 _event_hdl_async_data_drop(e->_data); /* data wrapper */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100196 pool_free(pool_head_sub_event, e);
197}
198
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100199/* wakeup the task depending on its type:
200 * normal async mode internally uses tasklets but advanced async mode
201 * allows both tasks and tasklets.
202 * While tasks and tasklets may be easily casted, we need to use the proper
203 * API to wake them up (the waiting queues are exclusive).
204 */
205static void event_hdl_task_wakeup(struct tasklet *task)
206{
207 if (TASK_IS_TASKLET(task))
208 tasklet_wakeup(task);
209 else
210 task_wakeup((struct task *)task, TASK_WOKEN_OTHER); /* TODO: switch to TASK_WOKEN_EVENT? */
211}
212
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100213/* task handler used for normal async subscription mode
214 * if you use advanced async subscription mode, you can use this
215 * as an example to implement your own task wrapper
216 */
217static struct task *event_hdl_async_task_default(struct task *task, void *ctx, unsigned int state)
218{
219 struct tasklet *tl = (struct tasklet *)task;
220 struct event_hdl_async_task_default_ctx *task_ctx = ctx;
221 struct event_hdl_async_event *event;
222 int max_notif_at_once_it = 0;
223 uint8_t done = 0;
224
225 /* run through e_queue, and call func() for each event
226 * if we read END event, it indicates we must stop:
227 * no more events to come (handler is unregistered)
228 * so we must free task_ctx and stop task
229 */
230 while (max_notif_at_once_it < event_hdl_async_max_notif_at_once &&
231 (event = event_hdl_async_equeue_pop(&task_ctx->e_queue)))
232 {
233 if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
234 done = 1;
235 event_hdl_async_free_event(event);
236 /* break is normally not even required, EVENT_HDL_SUB_END
237 * is guaranteed to be last event of e_queue
238 * (because in normal mode one sub == one e_queue)
239 */
240 break;
241 }
242 else {
243 struct event_hdl_cb cb;
244
245 cb.e_type = event->type;
246 cb.e_data = event->data;
247 cb.sub_mgmt = &event->sub_mgmt;
248 cb._sync = 0;
249
250 /* call user function */
251 task_ctx->func(&cb, event->private);
252 max_notif_at_once_it++;
253 }
254 event_hdl_async_free_event(event);
255 }
256
257 if (done) {
258 /* our job is done, subscription is over: no more events to come */
259 pool_free(pool_head_sub_taskctx, task_ctx);
260 tasklet_free(tl);
261 return NULL;
262 }
263 return task;
264}
265
266/* internal subscription mgmt functions */
267static inline struct event_hdl_sub_type _event_hdl_getsub(struct event_hdl_sub *cur_sub)
268{
269 return cur_sub->sub;
270}
271
272static inline struct event_hdl_sub_type _event_hdl_getsub_async(struct event_hdl_sub *cur_sub)
273{
274 struct mt_list lock;
275 struct event_hdl_sub_type type = EVENT_HDL_SUB_NONE;
276
277 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
278 if (lock.next != &cur_sub->mt_list)
279 type = _event_hdl_getsub(cur_sub);
280 // else already removed
281 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
282 return type;
283}
284
285static inline int _event_hdl_resub(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
286{
287 if (!event_hdl_sub_family_equal(cur_sub->sub, type))
288 return 0; /* family types differ, do nothing */
289 cur_sub->sub.subtype = type.subtype; /* new subtype assignment */
290 return 1;
291}
292
293static inline int _event_hdl_resub_async(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
294{
295 int status = 0;
296 struct mt_list lock;
297
298 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
299 if (lock.next != &cur_sub->mt_list)
300 status = _event_hdl_resub(cur_sub, type);
301 // else already removed
302 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
303 return status;
304}
305
306static inline void _event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
307{
308 struct mt_list lock;
309
310 if (del_sub->hdl.async) {
311 /* ASYNC SUB MODE */
312 /* push EVENT_HDL_SUB_END (to notify the task that the subscription is dead) */
313
314 /* push END EVENT in busy state so we can safely wakeup
315 * the task before releasing it.
316 * Not doing that would expose us to a race where the task could've already
317 * consumed the END event before the wakeup, and some tasks
318 * kill themselves (ie: normal async mode) when they receive such event
319 */
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100320 HA_ATOMIC_INC(&del_sub->hdl.async_equeue->size);
321 lock = MT_LIST_APPEND_LOCKED(&del_sub->hdl.async_equeue->head, &del_sub->async_end->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100322
323 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100324 event_hdl_task_wakeup(del_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100325
326 /* unlock END EVENT (we're done, the task is now free to consume it) */
327 MT_LIST_UNLOCK_ELT(&del_sub->async_end->mt_list, lock);
328
329 /* we don't free sub here
330 * freeing will be performed by async task so it can safely rely
331 * on the pointer until it notices it
332 */
333 } else {
334 /* SYNC SUB MODE */
335
336 /* we can directly free the subscription:
337 * no other thread can access it since we successfully
338 * removed it from the list
339 */
340 event_hdl_drop(del_sub);
341 }
342}
343
344static inline void _event_hdl_unsubscribe_async(struct event_hdl_sub *del_sub)
345{
346 if (!MT_LIST_DELETE(&del_sub->mt_list))
347 return; /* already removed (but may be pending in e_queues) */
348 _event_hdl_unsubscribe(del_sub);
349}
350
351/* sub_mgmt function pointers (for handlers) */
352static struct event_hdl_sub_type event_hdl_getsub_sync(const struct event_hdl_sub_mgmt *mgmt)
353{
354 if (!mgmt)
355 return EVENT_HDL_SUB_NONE;
356
357 if (!mgmt->this)
358 return EVENT_HDL_SUB_NONE; /* already removed from sync ctx */
359 return _event_hdl_getsub(mgmt->this);
360}
361
362static struct event_hdl_sub_type event_hdl_getsub_async(const struct event_hdl_sub_mgmt *mgmt)
363{
364 if (!mgmt)
365 return EVENT_HDL_SUB_NONE;
366
367 return _event_hdl_getsub_async(mgmt->this);
368}
369
370static int event_hdl_resub_sync(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
371{
372 if (!mgmt)
373 return 0;
374
375 if (!mgmt->this)
376 return 0; /* already removed from sync ctx */
377 return _event_hdl_resub(mgmt->this, type);
378}
379
380static int event_hdl_resub_async(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
381{
382 if (!mgmt)
383 return 0;
384
385 return _event_hdl_resub_async(mgmt->this, type);
386}
387
388static void event_hdl_unsubscribe_sync(const struct event_hdl_sub_mgmt *mgmt)
389{
390 if (!mgmt)
391 return;
392
393 if (!mgmt->this)
394 return; /* already removed from sync ctx */
395
396 /* assuming that publish sync code will notice that mgmt->this is NULL
397 * and will perform the list removal using MT_LIST_DELETE_SAFE and
398 * _event_hdl_unsubscribe()
399 * while still owning the lock
400 */
401 ((struct event_hdl_sub_mgmt *)mgmt)->this = NULL;
402}
403
404static void event_hdl_unsubscribe_async(const struct event_hdl_sub_mgmt *mgmt)
405{
406 if (!mgmt)
407 return;
408
409 _event_hdl_unsubscribe_async(mgmt->this);
410}
411
412#define EVENT_HDL_SUB_MGMT_ASYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
413 .getsub = event_hdl_getsub_async, \
414 .resub = event_hdl_resub_async, \
415 .unsub = event_hdl_unsubscribe_async}
416#define EVENT_HDL_SUB_MGMT_SYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
417 .getsub = event_hdl_getsub_sync, \
418 .resub = event_hdl_resub_sync, \
419 .unsub = event_hdl_unsubscribe_sync}
420
421struct event_hdl_sub *event_hdl_subscribe_ptr(event_hdl_sub_list *sub_list,
422 struct event_hdl_sub_type e_type, struct event_hdl hdl)
423{
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100424 struct event_hdl_sub *new_sub = NULL;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100425 struct mt_list *elt1, elt2;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100426 struct event_hdl_async_task_default_ctx *task_ctx = NULL;
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100427 struct mt_list lock;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100428
429 if (!sub_list)
430 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
431
432 /* hdl API consistency check */
433 /*FIXME: do we need to ensure that if private is set, private_free should be set as well? */
434 BUG_ON((!hdl.async && !hdl.sync_ptr) ||
435 (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL && !hdl.async_ptr) ||
436 (hdl.async == EVENT_HDL_ASYNC_MODE_ADVANCED &&
437 (!hdl.async_equeue || !hdl.async_task)));
438
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100439 new_sub = pool_alloc(pool_head_sub);
440 if (new_sub == NULL) {
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100441 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100442 }
443
444 /* assignments */
445 new_sub->sub.family = e_type.family;
446 new_sub->sub.subtype = e_type.subtype;
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100447 new_sub->flags = 0;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100448 new_sub->hdl = hdl;
449
450 if (hdl.async) {
451 /* async END event pre-allocation */
452 new_sub->async_end = pool_alloc(pool_head_sub_event);
453 if (!new_sub->async_end) {
454 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100455 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100456 }
457 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
458 /* normal mode: no task provided, we must initialize it */
459
460 /* initialize task context */
461 task_ctx = pool_alloc(pool_head_sub_taskctx);
462
463 if (!task_ctx) {
464 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100465 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100466 }
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100467 event_hdl_async_equeue_init(&task_ctx->e_queue);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100468 task_ctx->func = new_sub->hdl.async_ptr;
469
470 new_sub->hdl.async_equeue = &task_ctx->e_queue;
471 new_sub->hdl.async_task = tasklet_new();
472
473 if (!new_sub->hdl.async_task) {
474 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100475 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100476 }
477 new_sub->hdl.async_task->context = task_ctx;
478 new_sub->hdl.async_task->process = event_hdl_async_task_default;
479 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100480 /* initialize END event (used to notify about subscription ending)
481 * used by both normal and advanced mode:
482 * - to safely terminate the task in normal mode
483 * - to safely free subscription and
484 * keep track of active subscriptions in advanced mode
485 */
486 new_sub->async_end->type = EVENT_HDL_SUB_END;
487 new_sub->async_end->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(new_sub);
488 new_sub->async_end->private = new_sub->hdl.private;
489 new_sub->async_end->_data = NULL;
490 MT_LIST_INIT(&new_sub->async_end->mt_list);
491 }
492 /* set refcount to 2:
493 * 1 for handler (because handler can manage the subscription itself)
494 * 1 for caller (will be dropped automatically if caller use the non-ptr version)
495 */
496 new_sub->refcount = 2;
497
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100498 /* ready for registration */
499 MT_LIST_INIT(&new_sub->mt_list);
500
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100501 lock = MT_LIST_LOCK_ELT(&sub_list->known);
502
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100503 /* check if such identified hdl is not already registered */
504 if (hdl.id) {
505 struct event_hdl_sub *cur_sub;
506 uint8_t found = 0;
507
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100508 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
509 if (hdl.id == cur_sub->hdl.id) {
510 /* we found matching registered hdl */
511 found = 1;
512 break;
513 }
514 }
515 if (found) {
516 /* error already registered */
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100517 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100518 event_hdl_report_hdl_state(ha_alert, &hdl, "SUB", "could not subscribe: subscription with this id already exists");
519 goto cleanup;
520 }
521 }
522
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100523 if (lock.next == &sub_list->known) {
524 /* this is an expected corner case on de-init path, a subscribe attempt
525 * was made but the subscription list is already destroyed, we pretend
526 * it is a memory/IO error since it should not be long before haproxy
527 * enters the deinit() function anyway
528 */
529 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
530 goto cleanup;
531 }
532
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100533 /* Append in list (global or user specified list).
534 * For now, append when sync mode, and insert when async mode
535 * so that async handlers are executed first
536 */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100537 if (hdl.async) {
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100538 /* Prevent the task from being aborted on soft-stop: let's wait
539 * until the END event is acknowledged by the task.
540 * (decrease is performed in event_hdl_async_free_event())
541 *
542 * If we don't do this, event_hdl API will leak and we won't give
543 * a chance to the event-handling task to perform cleanup
544 */
545 HA_ATOMIC_INC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100546 /* async mode, insert at the beginning of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100547 MT_LIST_INSERT(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100548 } else {
549 /* sync mode, append at the end of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100550 MT_LIST_APPEND(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100551 }
552
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100553 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100554
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100555 return new_sub;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100556
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100557 cleanup:
558 if (new_sub) {
559 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
560 if (new_sub->hdl.async_task)
561 tasklet_free(new_sub->hdl.async_task);
562 if (task_ctx)
563 pool_free(pool_head_sub_taskctx, task_ctx);
564 }
565 if (hdl.async)
566 pool_free(pool_head_sub_event, new_sub->async_end);
567 pool_free(pool_head_sub, new_sub);
568 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100569
570 return NULL;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100571
572 memory_error:
573 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
574 goto cleanup;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100575}
576
577void event_hdl_take(struct event_hdl_sub *sub)
578{
579 HA_ATOMIC_INC(&sub->refcount);
580}
581
582void event_hdl_drop(struct event_hdl_sub *sub)
583{
584 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
585 return;
586
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100587 /* we were the last one holding a reference to event sub - free required */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100588 if (sub->hdl.private_free) {
589 /* free private data if specified upon registration */
590 sub->hdl.private_free(sub->hdl.private);
591 }
592 pool_free(pool_head_sub, sub);
593}
594
595int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
596{
597 return _event_hdl_resub_async(cur_sub, type);
598}
599
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100600void _event_hdl_pause(struct event_hdl_sub *cur_sub)
601{
602 cur_sub->flags |= EHDL_SUB_F_PAUSED;
603}
604
605void event_hdl_pause(struct event_hdl_sub *cur_sub)
606{
607 struct mt_list lock;
608
609 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
610 if (lock.next != &cur_sub->mt_list)
611 _event_hdl_pause(cur_sub);
612 // else already removed
613 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
614}
615
616void _event_hdl_resume(struct event_hdl_sub *cur_sub)
617{
618 cur_sub->flags &= ~EHDL_SUB_F_PAUSED;
619}
620
621void event_hdl_resume(struct event_hdl_sub *cur_sub)
622{
623 struct mt_list lock;
624
625 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
626 if (lock.next != &cur_sub->mt_list)
627 _event_hdl_resume(cur_sub);
628 // else already removed
629 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
630}
631
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100632void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
633{
634 _event_hdl_unsubscribe_async(del_sub);
635 /* drop refcount, assuming caller no longer use ptr */
636 event_hdl_drop(del_sub);
637}
638
639int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
640{
641 struct event_hdl_sub *sub;
642
643 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
644 if (sub) {
645 /* drop refcount because the user is not willing to hold a reference */
646 event_hdl_drop(sub);
647 return 1;
648 }
649 return 0;
650}
651
652/* Subscription external lookup functions
653 */
654int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
655 uint64_t lookup_id)
656{
657 struct event_hdl_sub *del_sub = NULL;
658 struct mt_list *elt1, elt2;
659 int found = 0;
660
661 if (!sub_list)
662 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
663
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100664 mt_list_for_each_entry_safe(del_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100665 if (lookup_id == del_sub->hdl.id) {
666 /* we found matching registered hdl */
667 MT_LIST_DELETE_SAFE(elt1);
668 _event_hdl_unsubscribe(del_sub);
669 found = 1;
670 break; /* id is unique, stop searching */
671 }
672 }
673 return found;
674}
675
676int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
677 uint64_t lookup_id, struct event_hdl_sub_type type)
678{
679 struct event_hdl_sub *cur_sub = NULL;
680 struct mt_list *elt1, elt2;
681 int status = 0;
682
683 if (!sub_list)
684 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
685
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100686 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100687 if (lookup_id == cur_sub->hdl.id) {
688 /* we found matching registered hdl */
689 status = _event_hdl_resub(cur_sub, type);
690 break; /* id is unique, stop searching */
691 }
692 }
693 return status;
694}
695
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100696int event_hdl_lookup_pause(event_hdl_sub_list *sub_list,
697 uint64_t lookup_id)
698{
699 struct event_hdl_sub *cur_sub = NULL;
700 struct mt_list *elt1, elt2;
701 int found = 0;
702
703 if (!sub_list)
704 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
705
706 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
707 if (lookup_id == cur_sub->hdl.id) {
708 /* we found matching registered hdl */
709 _event_hdl_pause(cur_sub);
710 found = 1;
711 break; /* id is unique, stop searching */
712 }
713 }
714 return found;
715}
716
717int event_hdl_lookup_resume(event_hdl_sub_list *sub_list,
718 uint64_t lookup_id)
719{
720 struct event_hdl_sub *cur_sub = NULL;
721 struct mt_list *elt1, elt2;
722 int found = 0;
723
724 if (!sub_list)
725 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
726
727 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
728 if (lookup_id == cur_sub->hdl.id) {
729 /* we found matching registered hdl */
730 _event_hdl_resume(cur_sub);
731 found = 1;
732 break; /* id is unique, stop searching */
733 }
734 }
735 return found;
736}
737
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100738struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
739 uint64_t lookup_id)
740{
741 struct event_hdl_sub *cur_sub = NULL;
742 struct mt_list *elt1, elt2;
743 uint8_t found = 0;
744
745 if (!sub_list)
746 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
747
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100748 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100749 if (lookup_id == cur_sub->hdl.id) {
750 /* we found matching registered hdl */
751 event_hdl_take(cur_sub);
752 found = 1;
753 break; /* id is unique, stop searching */
754 }
755 }
756 if (found)
757 return cur_sub;
758 return NULL;
759}
760
761/* event publishing functions
762 */
763static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
764 const struct event_hdl_cb_data *data)
765{
766 struct event_hdl_sub *cur_sub;
767 struct mt_list *elt1, elt2;
768 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
769 int error = 0;
770
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100771 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100772 /* notify each function that has subscribed to sub_family.type, unless paused */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100773 if ((cur_sub->sub.family == e_type.family) &&
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100774 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype) &&
775 !(cur_sub->flags & EHDL_SUB_F_PAUSED)) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100776 /* hdl should be notified */
777 if (!cur_sub->hdl.async) {
778 /* sync mode: simply call cb pointer
779 * it is up to the callee to schedule a task if needed or
780 * take specific precautions in order to return as fast as possible
781 * and not use locks that are already held by the caller
782 */
783 struct event_hdl_cb cb;
784 struct event_hdl_sub_mgmt sub_mgmt;
785
786 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
787 cb.e_type = e_type;
788 if (data)
789 cb.e_data = data->_ptr;
790 else
791 cb.e_data = NULL;
792 cb.sub_mgmt = &sub_mgmt;
793 cb._sync = 1;
794
795 /* call user function */
796 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
797
798 if (!sub_mgmt.this) {
799 /* user has performed hdl unsub
800 * we must remove it from the list
801 */
802 MT_LIST_DELETE_SAFE(elt1);
803 /* then free it */
804 _event_hdl_unsubscribe(cur_sub);
805 }
806 } else {
807 /* async mode: here we need to prepare event data
808 * and push it to the event_queue of the task(s)
809 * responsible for consuming the events of current
810 * subscription.
811 * Once the event is pushed, we wake up the associated task.
812 * This feature depends on <haproxy/task> that also
813 * depends on <haproxy/pool>:
814 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
815 * async handler, program may crash.
816 * Hopefully, STG_PREPARE+STG_POOL should be done early in
817 * HAProxy startup sequence.
818 */
819 struct event_hdl_async_event *new_event;
820
821 new_event = pool_alloc(pool_head_sub_event);
822 if (!new_event) {
823 error = 1;
824 break; /* stop on error */
825 }
826 new_event->type = e_type;
827 new_event->private = cur_sub->hdl.private;
828 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
829 if (data) {
830 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
831 * event_hdl-t.h file
832 */
833 BUG_ON(data->_size > sizeof(async_data->data));
834 if (!async_data) {
835 /* first async hdl reached - preparing async_data cache */
836 async_data = pool_alloc(pool_head_sub_event_data);
837 if (!async_data) {
838 error = 1;
839 pool_free(pool_head_sub_event, new_event);
840 break; /* stop on error */
841 }
842
843 /* async data assignment */
844 memcpy(async_data->data, data->_ptr, data->_size);
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100845 /* Initialize refcount, we start at 1 to prevent async
846 * data from being freed by an async handler while we
847 * still use it. We will drop the reference when the
848 * publish is over.
849 *
850 * (first use, atomic operation not required)
851 */
852 async_data->refcount = 1;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100853 }
854 new_event->_data = async_data;
855 new_event->data = async_data->data;
856 /* increment refcount because multiple hdls could
857 * use the same async_data
858 */
859 HA_ATOMIC_INC(&async_data->refcount);
860 } else
861 new_event->data = NULL;
862
863 /* appending new event to event hdl queue */
864 MT_LIST_INIT(&new_event->mt_list);
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100865 HA_ATOMIC_INC(&cur_sub->hdl.async_equeue->size);
866 MT_LIST_APPEND(&cur_sub->hdl.async_equeue->head, &new_event->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100867
868 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100869 event_hdl_task_wakeup(cur_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100870 } /* end async mode */
871 } /* end hdl should be notified */
872 } /* end mt_list */
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100873 if (async_data) {
874 /* we finished publishing, drop the reference on async data */
875 _event_hdl_async_data_drop(async_data);
876 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100877 if (error) {
878 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
879 return 0;
880 }
881 return 1;
882}
883
884/* Publish function should not be used from high calling rate or time sensitive
885 * places for now, because list lookup based on e_type is not optimized at
886 * all!
887 * Returns 1 in case of SUCCESS:
888 * Subscribed handlers were notified successfully
889 * Returns 0 in case of FAILURE:
890 * FAILURE means memory error while handling the very first async handler from
891 * the subscription list.
892 * As async handlers are executed first within the list, when such failure occurs
893 * you can safely assume that no events were published for the current call
894 */
895int event_hdl_publish(event_hdl_sub_list *sub_list,
896 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
897{
898 if (!e_type.family) {
899 /* do nothing, these types are reserved for internal use only
900 * (ie: unregistering) */
901 return 0;
902 }
903 if (sub_list) {
904 /* if sublist is provided, first publish event to list subscribers */
905 return _event_hdl_publish(sub_list, e_type, data);
906 } else {
907 /* publish to global list */
908 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
909 }
910}
911
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100912void event_hdl_sub_list_init(event_hdl_sub_list *sub_list)
913{
914 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
915 MT_LIST_INIT(&sub_list->head);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100916 MT_LIST_APPEND(&known_event_hdl_sub_list, &sub_list->known);
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100917}
918
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100919/* internal function, assumes that sub_list ptr is always valid */
920static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100921{
922 struct event_hdl_sub *cur_sub;
923 struct mt_list *elt1, elt2;
924
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100925 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100926 /* remove cur elem from list */
927 MT_LIST_DELETE_SAFE(elt1);
928 /* then free it */
929 _event_hdl_unsubscribe(cur_sub);
930 }
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100931}
932
933/* when a subscription list is no longer used, call this
934 * to do the cleanup and make sure all related subscriptions are
935 * safely ended according to their types
936 */
937void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
938{
939 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
940 if (!MT_LIST_DELETE(&sub_list->known))
941 return; /* already destroyed */
942 _event_hdl_sub_list_destroy(sub_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100943}
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100944
945INITCALL0(STG_INIT, event_hdl_init);