blob: e2f01241e8f21bdcc43a2b0f9493cf00f680c72e [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);
Tim Duesterhusc18e2442023-04-22 17:47:33 +0200572 pool_free(pool_head_sub_taskctx, task_ctx);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100573 }
574 if (hdl.async)
575 pool_free(pool_head_sub_event, new_sub->async_end);
576 pool_free(pool_head_sub, new_sub);
577 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100578
579 return NULL;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100580
581 memory_error:
582 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
583 goto cleanup;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100584}
585
586void event_hdl_take(struct event_hdl_sub *sub)
587{
588 HA_ATOMIC_INC(&sub->refcount);
589}
590
591void event_hdl_drop(struct event_hdl_sub *sub)
592{
593 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
594 return;
595
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100596 /* we were the last one holding a reference to event sub - free required */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100597 if (sub->hdl.private_free) {
598 /* free private data if specified upon registration */
599 sub->hdl.private_free(sub->hdl.private);
600 }
601 pool_free(pool_head_sub, sub);
602}
603
604int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
605{
606 return _event_hdl_resub_async(cur_sub, type);
607}
608
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100609void _event_hdl_pause(struct event_hdl_sub *cur_sub)
610{
611 cur_sub->flags |= EHDL_SUB_F_PAUSED;
612}
613
614void event_hdl_pause(struct event_hdl_sub *cur_sub)
615{
616 struct mt_list lock;
617
618 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
619 if (lock.next != &cur_sub->mt_list)
620 _event_hdl_pause(cur_sub);
621 // else already removed
622 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
623}
624
625void _event_hdl_resume(struct event_hdl_sub *cur_sub)
626{
627 cur_sub->flags &= ~EHDL_SUB_F_PAUSED;
628}
629
630void event_hdl_resume(struct event_hdl_sub *cur_sub)
631{
632 struct mt_list lock;
633
634 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
635 if (lock.next != &cur_sub->mt_list)
636 _event_hdl_resume(cur_sub);
637 // else already removed
638 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
639}
640
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100641void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
642{
643 _event_hdl_unsubscribe_async(del_sub);
644 /* drop refcount, assuming caller no longer use ptr */
645 event_hdl_drop(del_sub);
646}
647
648int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
649{
650 struct event_hdl_sub *sub;
651
652 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
653 if (sub) {
654 /* drop refcount because the user is not willing to hold a reference */
655 event_hdl_drop(sub);
656 return 1;
657 }
658 return 0;
659}
660
661/* Subscription external lookup functions
662 */
663int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
664 uint64_t lookup_id)
665{
666 struct event_hdl_sub *del_sub = NULL;
667 struct mt_list *elt1, elt2;
668 int found = 0;
669
670 if (!sub_list)
671 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
672
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100673 mt_list_for_each_entry_safe(del_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100674 if (lookup_id == del_sub->hdl.id) {
675 /* we found matching registered hdl */
676 MT_LIST_DELETE_SAFE(elt1);
677 _event_hdl_unsubscribe(del_sub);
678 found = 1;
679 break; /* id is unique, stop searching */
680 }
681 }
682 return found;
683}
684
685int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
686 uint64_t lookup_id, struct event_hdl_sub_type type)
687{
688 struct event_hdl_sub *cur_sub = NULL;
689 struct mt_list *elt1, elt2;
690 int status = 0;
691
692 if (!sub_list)
693 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
694
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100695 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100696 if (lookup_id == cur_sub->hdl.id) {
697 /* we found matching registered hdl */
698 status = _event_hdl_resub(cur_sub, type);
699 break; /* id is unique, stop searching */
700 }
701 }
702 return status;
703}
704
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100705int event_hdl_lookup_pause(event_hdl_sub_list *sub_list,
706 uint64_t lookup_id)
707{
708 struct event_hdl_sub *cur_sub = NULL;
709 struct mt_list *elt1, elt2;
710 int found = 0;
711
712 if (!sub_list)
713 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
714
715 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
716 if (lookup_id == cur_sub->hdl.id) {
717 /* we found matching registered hdl */
718 _event_hdl_pause(cur_sub);
719 found = 1;
720 break; /* id is unique, stop searching */
721 }
722 }
723 return found;
724}
725
726int event_hdl_lookup_resume(event_hdl_sub_list *sub_list,
727 uint64_t lookup_id)
728{
729 struct event_hdl_sub *cur_sub = NULL;
730 struct mt_list *elt1, elt2;
731 int found = 0;
732
733 if (!sub_list)
734 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
735
736 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
737 if (lookup_id == cur_sub->hdl.id) {
738 /* we found matching registered hdl */
739 _event_hdl_resume(cur_sub);
740 found = 1;
741 break; /* id is unique, stop searching */
742 }
743 }
744 return found;
745}
746
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100747struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
748 uint64_t lookup_id)
749{
750 struct event_hdl_sub *cur_sub = NULL;
751 struct mt_list *elt1, elt2;
752 uint8_t found = 0;
753
754 if (!sub_list)
755 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
756
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100757 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100758 if (lookup_id == cur_sub->hdl.id) {
759 /* we found matching registered hdl */
760 event_hdl_take(cur_sub);
761 found = 1;
762 break; /* id is unique, stop searching */
763 }
764 }
765 if (found)
766 return cur_sub;
767 return NULL;
768}
769
770/* event publishing functions
771 */
772static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
773 const struct event_hdl_cb_data *data)
774{
775 struct event_hdl_sub *cur_sub;
776 struct mt_list *elt1, elt2;
777 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
778 int error = 0;
779
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100780 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100781 /* notify each function that has subscribed to sub_family.type, unless paused */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100782 if ((cur_sub->sub.family == e_type.family) &&
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100783 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype) &&
784 !(cur_sub->flags & EHDL_SUB_F_PAUSED)) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100785 /* hdl should be notified */
786 if (!cur_sub->hdl.async) {
787 /* sync mode: simply call cb pointer
788 * it is up to the callee to schedule a task if needed or
789 * take specific precautions in order to return as fast as possible
790 * and not use locks that are already held by the caller
791 */
792 struct event_hdl_cb cb;
793 struct event_hdl_sub_mgmt sub_mgmt;
794
795 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
796 cb.e_type = e_type;
797 if (data)
798 cb.e_data = data->_ptr;
799 else
800 cb.e_data = NULL;
801 cb.sub_mgmt = &sub_mgmt;
802 cb._sync = 1;
803
804 /* call user function */
805 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
806
807 if (!sub_mgmt.this) {
808 /* user has performed hdl unsub
809 * we must remove it from the list
810 */
811 MT_LIST_DELETE_SAFE(elt1);
812 /* then free it */
813 _event_hdl_unsubscribe(cur_sub);
814 }
815 } else {
816 /* async mode: here we need to prepare event data
817 * and push it to the event_queue of the task(s)
818 * responsible for consuming the events of current
819 * subscription.
820 * Once the event is pushed, we wake up the associated task.
821 * This feature depends on <haproxy/task> that also
822 * depends on <haproxy/pool>:
823 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
824 * async handler, program may crash.
825 * Hopefully, STG_PREPARE+STG_POOL should be done early in
826 * HAProxy startup sequence.
827 */
828 struct event_hdl_async_event *new_event;
829
830 new_event = pool_alloc(pool_head_sub_event);
831 if (!new_event) {
832 error = 1;
833 break; /* stop on error */
834 }
835 new_event->type = e_type;
836 new_event->private = cur_sub->hdl.private;
Aurelien DARRAGONe9314fb2023-04-04 21:41:10 +0200837 new_event->when = date;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100838 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
839 if (data) {
840 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100841 * event_hdl-t.h file or consider providing dynamic struct members
842 * to reduce overall struct size
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100843 */
844 BUG_ON(data->_size > sizeof(async_data->data));
845 if (!async_data) {
846 /* first async hdl reached - preparing async_data cache */
847 async_data = pool_alloc(pool_head_sub_event_data);
848 if (!async_data) {
849 error = 1;
850 pool_free(pool_head_sub_event, new_event);
851 break; /* stop on error */
852 }
853
854 /* async data assignment */
855 memcpy(async_data->data, data->_ptr, data->_size);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100856 async_data->mfree = data->_mfree;
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100857 /* Initialize refcount, we start at 1 to prevent async
858 * data from being freed by an async handler while we
859 * still use it. We will drop the reference when the
860 * publish is over.
861 *
862 * (first use, atomic operation not required)
863 */
864 async_data->refcount = 1;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100865 }
866 new_event->_data = async_data;
867 new_event->data = async_data->data;
868 /* increment refcount because multiple hdls could
869 * use the same async_data
870 */
871 HA_ATOMIC_INC(&async_data->refcount);
872 } else
873 new_event->data = NULL;
874
875 /* appending new event to event hdl queue */
876 MT_LIST_INIT(&new_event->mt_list);
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100877 HA_ATOMIC_INC(&cur_sub->hdl.async_equeue->size);
878 MT_LIST_APPEND(&cur_sub->hdl.async_equeue->head, &new_event->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100879
880 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100881 event_hdl_task_wakeup(cur_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100882 } /* end async mode */
883 } /* end hdl should be notified */
884 } /* end mt_list */
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100885 if (async_data) {
886 /* we finished publishing, drop the reference on async data */
887 _event_hdl_async_data_drop(async_data);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100888 } else {
889 /* no async subscribers, we are responsible for calling the data
890 * member freeing function if it was provided
891 */
892 if (data && data->_mfree)
893 data->_mfree(data->_ptr);
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100894 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100895 if (error) {
896 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
897 return 0;
898 }
899 return 1;
900}
901
902/* Publish function should not be used from high calling rate or time sensitive
903 * places for now, because list lookup based on e_type is not optimized at
904 * all!
905 * Returns 1 in case of SUCCESS:
906 * Subscribed handlers were notified successfully
907 * Returns 0 in case of FAILURE:
908 * FAILURE means memory error while handling the very first async handler from
909 * the subscription list.
910 * As async handlers are executed first within the list, when such failure occurs
911 * you can safely assume that no events were published for the current call
912 */
913int event_hdl_publish(event_hdl_sub_list *sub_list,
914 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
915{
916 if (!e_type.family) {
917 /* do nothing, these types are reserved for internal use only
918 * (ie: unregistering) */
919 return 0;
920 }
921 if (sub_list) {
922 /* if sublist is provided, first publish event to list subscribers */
923 return _event_hdl_publish(sub_list, e_type, data);
924 } else {
925 /* publish to global list */
926 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
927 }
928}
929
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100930void event_hdl_sub_list_init(event_hdl_sub_list *sub_list)
931{
932 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
933 MT_LIST_INIT(&sub_list->head);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100934 MT_LIST_APPEND(&known_event_hdl_sub_list, &sub_list->known);
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100935}
936
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100937/* internal function, assumes that sub_list ptr is always valid */
938static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100939{
940 struct event_hdl_sub *cur_sub;
941 struct mt_list *elt1, elt2;
942
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100943 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100944 /* remove cur elem from list */
945 MT_LIST_DELETE_SAFE(elt1);
946 /* then free it */
947 _event_hdl_unsubscribe(cur_sub);
948 }
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100949}
950
951/* when a subscription list is no longer used, call this
952 * to do the cleanup and make sure all related subscriptions are
953 * safely ended according to their types
954 */
955void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
956{
957 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
958 if (!MT_LIST_DELETE(&sub_list->known))
959 return; /* already destroyed */
960 _event_hdl_sub_list_destroy(sub_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100961}
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100962
963INITCALL0(STG_INIT, event_hdl_init);