blob: 7641486cf9f92a06c0beb1da3e7861b81990ecfd [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 DARRAGONe3eea292023-04-04 21:28:07 +020032 {"SERVER_STATE", EVENT_HDL_SUB_SERVER_STATE},
Aurelien DARRAGONa163d652023-04-21 18:06:58 +020033 {"SERVER_ADMIN", EVENT_HDL_SUB_SERVER_ADMIN},
Aurelien DARRAGONdcbc2d22023-03-30 10:19:08 +020034 {"SERVER_CHECK", EVENT_HDL_SUB_SERVER_CHECK},
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010035};
36
37/* internal types (only used in this file) */
38struct event_hdl_async_task_default_ctx
39{
40 event_hdl_async_equeue e_queue; /* event queue list */
41 event_hdl_cb_async func; /* event handling func */
42};
43
44/* memory pools declarations */
45DECLARE_STATIC_POOL(pool_head_sub, "ehdl_sub", sizeof(struct event_hdl_sub));
46DECLARE_STATIC_POOL(pool_head_sub_event, "ehdl_sub_e", sizeof(struct event_hdl_async_event));
47DECLARE_STATIC_POOL(pool_head_sub_event_data, "ehdl_sub_ed", sizeof(struct event_hdl_async_event_data));
48DECLARE_STATIC_POOL(pool_head_sub_taskctx, "ehdl_sub_tctx", sizeof(struct event_hdl_async_task_default_ctx));
49
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010050/* TODO: will become a config tunable
51 * ie: tune.events.max-async-notif-at-once
52 */
53static int event_hdl_async_max_notif_at_once = 10;
54
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010055/* global subscription list (implicit where NULL is used as sublist argument) */
56static event_hdl_sub_list global_event_hdl_sub_list;
57
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +010058/* every known subscription lists are tracked in this list (including the global one) */
59static struct mt_list known_event_hdl_sub_list = MT_LIST_HEAD_INIT(known_event_hdl_sub_list);
60
61static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list);
62
63static void event_hdl_deinit(struct sig_handler *sh)
64{
65 event_hdl_sub_list *cur_list;
66 struct mt_list *elt1, elt2;
67
68 /* destroy all known subscription lists */
69 mt_list_for_each_entry_safe(cur_list, &known_event_hdl_sub_list, known, elt1, elt2) {
70 /* remove cur elem from list */
71 MT_LIST_DELETE_SAFE(elt1);
72 /* then destroy it */
73 _event_hdl_sub_list_destroy(cur_list);
74 }
75}
76
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010077static void event_hdl_init(void)
78{
79 /* initialize global subscription list */
80 event_hdl_sub_list_init(&global_event_hdl_sub_list);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +010081 /* register the deinit function, will be called on soft-stop */
82 signal_register_fct(0, event_hdl_deinit, 0);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +010083}
84
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +010085/* general purpose hashing function when you want to compute
86 * an ID based on <scope> x <name>
87 * It is your responsibility to make sure <scope> is not used
88 * elsewhere in the code (or that you are fine with sharing
89 * the scope).
90 */
91inline uint64_t event_hdl_id(const char *scope, const char *name)
92{
93 XXH64_state_t state;
94
95 XXH64_reset(&state, 0);
96 XXH64_update(&state, scope, strlen(scope));
97 XXH64_update(&state, name, strlen(name));
98 return XXH64_digest(&state);
99}
100
101/* takes a sub_type as input, returns corresponding sub_type
102 * printable string or "N/A" if not found.
103 * If not found, an error will be reported to stderr so the developers
104 * know that a sub_type is missing its associated string in event_hdl-t.h
105 */
106const char *event_hdl_sub_type_to_string(struct event_hdl_sub_type sub_type)
107{
108 int it;
109
110 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
111 if (sub_type.family == event_hdl_sub_type_map[it].type.family &&
112 sub_type.subtype == event_hdl_sub_type_map[it].type.subtype)
113 return event_hdl_sub_type_map[it].name;
114 }
115 ha_alert("event_hdl-t.h: missing sub_type string representation.\n"
116 "Please reflect any changes in event_hdl_sub_type_map.\n");
117 return "N/A";
118}
119
120/* returns the internal sub_type corresponding
121 * to the printable representation <name>
122 * or EVENT_HDL_SUB_NONE if no such event exists
123 * (see event_hdl-t.h for the complete list of supported types)
124 */
125struct event_hdl_sub_type event_hdl_string_to_sub_type(const char *name)
126{
127 int it;
128
129 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
130 if (!strcmp(name, event_hdl_sub_type_map[it].name))
131 return event_hdl_sub_type_map[it].type;
132 }
133 return EVENT_HDL_SUB_NONE;
134}
135
136/* Takes <subscriptions> sub list as input, returns a printable string
137 * containing every sub_types contained in <subscriptions>
138 * separated by '|' char.
139 * Returns NULL if no sub_types are found in <subscriptions>
140 * This functions leverages memprintf, thus it is up to the
141 * caller to free the returned value (if != NULL) when he no longer
142 * uses it.
143 */
144char *event_hdl_sub_type_print(struct event_hdl_sub_type subscriptions)
145{
146 char *out = NULL;
147 int it;
148 uint8_t first = 1;
149
150 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
151 if (subscriptions.family == event_hdl_sub_type_map[it].type.family &&
152 ((subscriptions.subtype & event_hdl_sub_type_map[it].type.subtype) ==
153 event_hdl_sub_type_map[it].type.subtype)) {
154 if (first) {
155 memprintf(&out, "%s", event_hdl_sub_type_map[it].name);
156 first--;
157 }
158 else
159 memprintf(&out, "%s%s%s", out, "|", event_hdl_sub_type_map[it].name);
160 }
161 }
162
163 return out;
164}
165
166/* event_hdl debug/reporting function */
167typedef void (*event_hdl_report_hdl_state_func)(const char *fmt, ...);
168static void event_hdl_report_hdl_state(event_hdl_report_hdl_state_func report_func,
169 const struct event_hdl *hdl, const char *what, const char *state)
170{
171 report_func("[event_hdl]:%s (%s)'#%llu@%s': %s\n",
172 what,
173 (hdl->async) ? "ASYNC" : "SYNC",
174 (long long unsigned int)hdl->id,
175 hdl->dorigin,
176 state);
177}
178
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100179static inline void _event_hdl_async_data_drop(struct event_hdl_async_event_data *data)
180{
181 if (HA_ATOMIC_SUB_FETCH(&data->refcount, 1) == 0) {
182 /* we were the last one holding a reference to event data - free required */
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100183 if (data->mfree) {
184 /* Some event data members are dynamically allocated and thus
185 * require specific cleanup using user-provided function.
186 * We directly pass a pointer to internal data storage but
187 * we only expect the cleanup function to typecast it in the
188 * relevant data type to give enough context to the function to
189 * perform the cleanup on data members, and not actually freeing
190 * data pointer since it is our internal buffer :)
191 */
192 data->mfree(&data->data);
193 }
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100194 pool_free(pool_head_sub_event_data, data);
195 }
196}
197
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100198void event_hdl_async_free_event(struct event_hdl_async_event *e)
199{
200 if (unlikely(event_hdl_sub_type_equal(e->type, EVENT_HDL_SUB_END))) {
201 /* last event for hdl, special case */
202 /* free subscription entry as we're the last one still using it
203 * (it is already removed from mt_list, no race can occur)
204 */
205 event_hdl_drop(e->sub_mgmt.this);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100206 HA_ATOMIC_DEC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100207 }
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100208 else if (e->_data)
209 _event_hdl_async_data_drop(e->_data); /* data wrapper */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100210 pool_free(pool_head_sub_event, e);
211}
212
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100213/* wakeup the task depending on its type:
214 * normal async mode internally uses tasklets but advanced async mode
215 * allows both tasks and tasklets.
216 * While tasks and tasklets may be easily casted, we need to use the proper
217 * API to wake them up (the waiting queues are exclusive).
218 */
219static void event_hdl_task_wakeup(struct tasklet *task)
220{
221 if (TASK_IS_TASKLET(task))
222 tasklet_wakeup(task);
223 else
224 task_wakeup((struct task *)task, TASK_WOKEN_OTHER); /* TODO: switch to TASK_WOKEN_EVENT? */
225}
226
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100227/* task handler used for normal async subscription mode
228 * if you use advanced async subscription mode, you can use this
229 * as an example to implement your own task wrapper
230 */
231static struct task *event_hdl_async_task_default(struct task *task, void *ctx, unsigned int state)
232{
233 struct tasklet *tl = (struct tasklet *)task;
234 struct event_hdl_async_task_default_ctx *task_ctx = ctx;
235 struct event_hdl_async_event *event;
236 int max_notif_at_once_it = 0;
237 uint8_t done = 0;
238
239 /* run through e_queue, and call func() for each event
240 * if we read END event, it indicates we must stop:
241 * no more events to come (handler is unregistered)
242 * so we must free task_ctx and stop task
243 */
244 while (max_notif_at_once_it < event_hdl_async_max_notif_at_once &&
245 (event = event_hdl_async_equeue_pop(&task_ctx->e_queue)))
246 {
247 if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
248 done = 1;
249 event_hdl_async_free_event(event);
250 /* break is normally not even required, EVENT_HDL_SUB_END
251 * is guaranteed to be last event of e_queue
252 * (because in normal mode one sub == one e_queue)
253 */
254 break;
255 }
256 else {
257 struct event_hdl_cb cb;
258
259 cb.e_type = event->type;
260 cb.e_data = event->data;
261 cb.sub_mgmt = &event->sub_mgmt;
262 cb._sync = 0;
263
264 /* call user function */
265 task_ctx->func(&cb, event->private);
266 max_notif_at_once_it++;
267 }
268 event_hdl_async_free_event(event);
269 }
270
271 if (done) {
272 /* our job is done, subscription is over: no more events to come */
273 pool_free(pool_head_sub_taskctx, task_ctx);
274 tasklet_free(tl);
275 return NULL;
276 }
277 return task;
278}
279
280/* internal subscription mgmt functions */
281static inline struct event_hdl_sub_type _event_hdl_getsub(struct event_hdl_sub *cur_sub)
282{
283 return cur_sub->sub;
284}
285
286static inline struct event_hdl_sub_type _event_hdl_getsub_async(struct event_hdl_sub *cur_sub)
287{
288 struct mt_list lock;
289 struct event_hdl_sub_type type = EVENT_HDL_SUB_NONE;
290
291 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
292 if (lock.next != &cur_sub->mt_list)
293 type = _event_hdl_getsub(cur_sub);
294 // else already removed
295 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
296 return type;
297}
298
299static inline int _event_hdl_resub(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
300{
301 if (!event_hdl_sub_family_equal(cur_sub->sub, type))
302 return 0; /* family types differ, do nothing */
303 cur_sub->sub.subtype = type.subtype; /* new subtype assignment */
304 return 1;
305}
306
307static inline int _event_hdl_resub_async(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
308{
309 int status = 0;
310 struct mt_list lock;
311
312 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
313 if (lock.next != &cur_sub->mt_list)
314 status = _event_hdl_resub(cur_sub, type);
315 // else already removed
316 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
317 return status;
318}
319
320static inline void _event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
321{
322 struct mt_list lock;
323
324 if (del_sub->hdl.async) {
325 /* ASYNC SUB MODE */
326 /* push EVENT_HDL_SUB_END (to notify the task that the subscription is dead) */
327
328 /* push END EVENT in busy state so we can safely wakeup
329 * the task before releasing it.
330 * Not doing that would expose us to a race where the task could've already
331 * consumed the END event before the wakeup, and some tasks
332 * kill themselves (ie: normal async mode) when they receive such event
333 */
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100334 HA_ATOMIC_INC(&del_sub->hdl.async_equeue->size);
335 lock = MT_LIST_APPEND_LOCKED(&del_sub->hdl.async_equeue->head, &del_sub->async_end->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100336
337 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100338 event_hdl_task_wakeup(del_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100339
340 /* unlock END EVENT (we're done, the task is now free to consume it) */
341 MT_LIST_UNLOCK_ELT(&del_sub->async_end->mt_list, lock);
342
343 /* we don't free sub here
344 * freeing will be performed by async task so it can safely rely
345 * on the pointer until it notices it
346 */
347 } else {
348 /* SYNC SUB MODE */
349
350 /* we can directly free the subscription:
351 * no other thread can access it since we successfully
352 * removed it from the list
353 */
354 event_hdl_drop(del_sub);
355 }
356}
357
358static inline void _event_hdl_unsubscribe_async(struct event_hdl_sub *del_sub)
359{
360 if (!MT_LIST_DELETE(&del_sub->mt_list))
361 return; /* already removed (but may be pending in e_queues) */
362 _event_hdl_unsubscribe(del_sub);
363}
364
365/* sub_mgmt function pointers (for handlers) */
366static struct event_hdl_sub_type event_hdl_getsub_sync(const struct event_hdl_sub_mgmt *mgmt)
367{
368 if (!mgmt)
369 return EVENT_HDL_SUB_NONE;
370
371 if (!mgmt->this)
372 return EVENT_HDL_SUB_NONE; /* already removed from sync ctx */
373 return _event_hdl_getsub(mgmt->this);
374}
375
376static struct event_hdl_sub_type event_hdl_getsub_async(const struct event_hdl_sub_mgmt *mgmt)
377{
378 if (!mgmt)
379 return EVENT_HDL_SUB_NONE;
380
381 return _event_hdl_getsub_async(mgmt->this);
382}
383
384static int event_hdl_resub_sync(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
385{
386 if (!mgmt)
387 return 0;
388
389 if (!mgmt->this)
390 return 0; /* already removed from sync ctx */
391 return _event_hdl_resub(mgmt->this, type);
392}
393
394static int event_hdl_resub_async(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
395{
396 if (!mgmt)
397 return 0;
398
399 return _event_hdl_resub_async(mgmt->this, type);
400}
401
402static void event_hdl_unsubscribe_sync(const struct event_hdl_sub_mgmt *mgmt)
403{
404 if (!mgmt)
405 return;
406
407 if (!mgmt->this)
408 return; /* already removed from sync ctx */
409
410 /* assuming that publish sync code will notice that mgmt->this is NULL
411 * and will perform the list removal using MT_LIST_DELETE_SAFE and
412 * _event_hdl_unsubscribe()
413 * while still owning the lock
414 */
415 ((struct event_hdl_sub_mgmt *)mgmt)->this = NULL;
416}
417
418static void event_hdl_unsubscribe_async(const struct event_hdl_sub_mgmt *mgmt)
419{
420 if (!mgmt)
421 return;
422
423 _event_hdl_unsubscribe_async(mgmt->this);
424}
425
426#define EVENT_HDL_SUB_MGMT_ASYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
427 .getsub = event_hdl_getsub_async, \
428 .resub = event_hdl_resub_async, \
429 .unsub = event_hdl_unsubscribe_async}
430#define EVENT_HDL_SUB_MGMT_SYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
431 .getsub = event_hdl_getsub_sync, \
432 .resub = event_hdl_resub_sync, \
433 .unsub = event_hdl_unsubscribe_sync}
434
435struct event_hdl_sub *event_hdl_subscribe_ptr(event_hdl_sub_list *sub_list,
436 struct event_hdl_sub_type e_type, struct event_hdl hdl)
437{
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100438 struct event_hdl_sub *new_sub = NULL;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100439 struct mt_list *elt1, elt2;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100440 struct event_hdl_async_task_default_ctx *task_ctx = NULL;
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100441 struct mt_list lock;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100442
443 if (!sub_list)
444 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
445
446 /* hdl API consistency check */
447 /*FIXME: do we need to ensure that if private is set, private_free should be set as well? */
448 BUG_ON((!hdl.async && !hdl.sync_ptr) ||
449 (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL && !hdl.async_ptr) ||
450 (hdl.async == EVENT_HDL_ASYNC_MODE_ADVANCED &&
451 (!hdl.async_equeue || !hdl.async_task)));
452
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100453 new_sub = pool_alloc(pool_head_sub);
454 if (new_sub == NULL) {
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100455 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100456 }
457
458 /* assignments */
459 new_sub->sub.family = e_type.family;
460 new_sub->sub.subtype = e_type.subtype;
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100461 new_sub->flags = 0;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100462 new_sub->hdl = hdl;
463
464 if (hdl.async) {
465 /* async END event pre-allocation */
466 new_sub->async_end = pool_alloc(pool_head_sub_event);
467 if (!new_sub->async_end) {
468 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100469 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100470 }
471 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
472 /* normal mode: no task provided, we must initialize it */
473
474 /* initialize task context */
475 task_ctx = pool_alloc(pool_head_sub_taskctx);
476
477 if (!task_ctx) {
478 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100479 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100480 }
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100481 event_hdl_async_equeue_init(&task_ctx->e_queue);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100482 task_ctx->func = new_sub->hdl.async_ptr;
483
484 new_sub->hdl.async_equeue = &task_ctx->e_queue;
485 new_sub->hdl.async_task = tasklet_new();
486
487 if (!new_sub->hdl.async_task) {
488 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100489 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100490 }
491 new_sub->hdl.async_task->context = task_ctx;
492 new_sub->hdl.async_task->process = event_hdl_async_task_default;
493 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100494 /* initialize END event (used to notify about subscription ending)
495 * used by both normal and advanced mode:
496 * - to safely terminate the task in normal mode
497 * - to safely free subscription and
498 * keep track of active subscriptions in advanced mode
499 */
500 new_sub->async_end->type = EVENT_HDL_SUB_END;
501 new_sub->async_end->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(new_sub);
502 new_sub->async_end->private = new_sub->hdl.private;
503 new_sub->async_end->_data = NULL;
504 MT_LIST_INIT(&new_sub->async_end->mt_list);
505 }
506 /* set refcount to 2:
507 * 1 for handler (because handler can manage the subscription itself)
508 * 1 for caller (will be dropped automatically if caller use the non-ptr version)
509 */
510 new_sub->refcount = 2;
511
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100512 /* ready for registration */
513 MT_LIST_INIT(&new_sub->mt_list);
514
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100515 lock = MT_LIST_LOCK_ELT(&sub_list->known);
516
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100517 /* check if such identified hdl is not already registered */
518 if (hdl.id) {
519 struct event_hdl_sub *cur_sub;
520 uint8_t found = 0;
521
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100522 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
523 if (hdl.id == cur_sub->hdl.id) {
524 /* we found matching registered hdl */
525 found = 1;
526 break;
527 }
528 }
529 if (found) {
530 /* error already registered */
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100531 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100532 event_hdl_report_hdl_state(ha_alert, &hdl, "SUB", "could not subscribe: subscription with this id already exists");
533 goto cleanup;
534 }
535 }
536
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100537 if (lock.next == &sub_list->known) {
538 /* this is an expected corner case on de-init path, a subscribe attempt
539 * was made but the subscription list is already destroyed, we pretend
540 * it is a memory/IO error since it should not be long before haproxy
541 * enters the deinit() function anyway
542 */
543 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
544 goto cleanup;
545 }
546
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100547 /* Append in list (global or user specified list).
548 * For now, append when sync mode, and insert when async mode
549 * so that async handlers are executed first
550 */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100551 if (hdl.async) {
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100552 /* Prevent the task from being aborted on soft-stop: let's wait
553 * until the END event is acknowledged by the task.
554 * (decrease is performed in event_hdl_async_free_event())
555 *
556 * If we don't do this, event_hdl API will leak and we won't give
557 * a chance to the event-handling task to perform cleanup
558 */
559 HA_ATOMIC_INC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100560 /* async mode, insert at the beginning of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100561 MT_LIST_INSERT(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100562 } else {
563 /* sync mode, append at the end of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100564 MT_LIST_APPEND(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100565 }
566
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100567 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100568
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100569 return new_sub;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100570
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100571 cleanup:
572 if (new_sub) {
573 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
Tim Duesterhusb1ec21d2023-04-22 17:47:32 +0200574 tasklet_free(new_sub->hdl.async_task);
Tim Duesterhusc18e2442023-04-22 17:47:33 +0200575 pool_free(pool_head_sub_taskctx, task_ctx);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100576 }
577 if (hdl.async)
578 pool_free(pool_head_sub_event, new_sub->async_end);
579 pool_free(pool_head_sub, new_sub);
580 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100581
582 return NULL;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100583
584 memory_error:
585 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
586 goto cleanup;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100587}
588
589void event_hdl_take(struct event_hdl_sub *sub)
590{
591 HA_ATOMIC_INC(&sub->refcount);
592}
593
594void event_hdl_drop(struct event_hdl_sub *sub)
595{
596 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
597 return;
598
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100599 /* we were the last one holding a reference to event sub - free required */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100600 if (sub->hdl.private_free) {
601 /* free private data if specified upon registration */
602 sub->hdl.private_free(sub->hdl.private);
603 }
604 pool_free(pool_head_sub, sub);
605}
606
607int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
608{
609 return _event_hdl_resub_async(cur_sub, type);
610}
611
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100612void _event_hdl_pause(struct event_hdl_sub *cur_sub)
613{
614 cur_sub->flags |= EHDL_SUB_F_PAUSED;
615}
616
617void event_hdl_pause(struct event_hdl_sub *cur_sub)
618{
619 struct mt_list lock;
620
621 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
622 if (lock.next != &cur_sub->mt_list)
623 _event_hdl_pause(cur_sub);
624 // else already removed
625 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
626}
627
628void _event_hdl_resume(struct event_hdl_sub *cur_sub)
629{
630 cur_sub->flags &= ~EHDL_SUB_F_PAUSED;
631}
632
633void event_hdl_resume(struct event_hdl_sub *cur_sub)
634{
635 struct mt_list lock;
636
637 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
638 if (lock.next != &cur_sub->mt_list)
639 _event_hdl_resume(cur_sub);
640 // else already removed
641 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
642}
643
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100644void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
645{
646 _event_hdl_unsubscribe_async(del_sub);
647 /* drop refcount, assuming caller no longer use ptr */
648 event_hdl_drop(del_sub);
649}
650
651int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
652{
653 struct event_hdl_sub *sub;
654
655 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
656 if (sub) {
657 /* drop refcount because the user is not willing to hold a reference */
658 event_hdl_drop(sub);
659 return 1;
660 }
661 return 0;
662}
663
664/* Subscription external lookup functions
665 */
666int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
667 uint64_t lookup_id)
668{
669 struct event_hdl_sub *del_sub = NULL;
670 struct mt_list *elt1, elt2;
671 int found = 0;
672
673 if (!sub_list)
674 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
675
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100676 mt_list_for_each_entry_safe(del_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100677 if (lookup_id == del_sub->hdl.id) {
678 /* we found matching registered hdl */
679 MT_LIST_DELETE_SAFE(elt1);
680 _event_hdl_unsubscribe(del_sub);
681 found = 1;
682 break; /* id is unique, stop searching */
683 }
684 }
685 return found;
686}
687
688int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
689 uint64_t lookup_id, struct event_hdl_sub_type type)
690{
691 struct event_hdl_sub *cur_sub = NULL;
692 struct mt_list *elt1, elt2;
693 int status = 0;
694
695 if (!sub_list)
696 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
697
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100698 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100699 if (lookup_id == cur_sub->hdl.id) {
700 /* we found matching registered hdl */
701 status = _event_hdl_resub(cur_sub, type);
702 break; /* id is unique, stop searching */
703 }
704 }
705 return status;
706}
707
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100708int event_hdl_lookup_pause(event_hdl_sub_list *sub_list,
709 uint64_t lookup_id)
710{
711 struct event_hdl_sub *cur_sub = NULL;
712 struct mt_list *elt1, elt2;
713 int found = 0;
714
715 if (!sub_list)
716 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
717
718 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
719 if (lookup_id == cur_sub->hdl.id) {
720 /* we found matching registered hdl */
721 _event_hdl_pause(cur_sub);
722 found = 1;
723 break; /* id is unique, stop searching */
724 }
725 }
726 return found;
727}
728
729int event_hdl_lookup_resume(event_hdl_sub_list *sub_list,
730 uint64_t lookup_id)
731{
732 struct event_hdl_sub *cur_sub = NULL;
733 struct mt_list *elt1, elt2;
734 int found = 0;
735
736 if (!sub_list)
737 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
738
739 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
740 if (lookup_id == cur_sub->hdl.id) {
741 /* we found matching registered hdl */
742 _event_hdl_resume(cur_sub);
743 found = 1;
744 break; /* id is unique, stop searching */
745 }
746 }
747 return found;
748}
749
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100750struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
751 uint64_t lookup_id)
752{
753 struct event_hdl_sub *cur_sub = NULL;
754 struct mt_list *elt1, elt2;
755 uint8_t found = 0;
756
757 if (!sub_list)
758 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
759
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100760 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100761 if (lookup_id == cur_sub->hdl.id) {
762 /* we found matching registered hdl */
763 event_hdl_take(cur_sub);
764 found = 1;
765 break; /* id is unique, stop searching */
766 }
767 }
768 if (found)
769 return cur_sub;
770 return NULL;
771}
772
773/* event publishing functions
774 */
775static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
776 const struct event_hdl_cb_data *data)
777{
778 struct event_hdl_sub *cur_sub;
779 struct mt_list *elt1, elt2;
780 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
781 int error = 0;
782
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100783 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100784 /* notify each function that has subscribed to sub_family.type, unless paused */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100785 if ((cur_sub->sub.family == e_type.family) &&
Aurelien DARRAGONf751a972023-03-10 10:45:58 +0100786 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype) &&
787 !(cur_sub->flags & EHDL_SUB_F_PAUSED)) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100788 /* hdl should be notified */
789 if (!cur_sub->hdl.async) {
790 /* sync mode: simply call cb pointer
791 * it is up to the callee to schedule a task if needed or
792 * take specific precautions in order to return as fast as possible
793 * and not use locks that are already held by the caller
794 */
795 struct event_hdl_cb cb;
796 struct event_hdl_sub_mgmt sub_mgmt;
797
798 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
799 cb.e_type = e_type;
800 if (data)
801 cb.e_data = data->_ptr;
802 else
803 cb.e_data = NULL;
804 cb.sub_mgmt = &sub_mgmt;
805 cb._sync = 1;
806
807 /* call user function */
808 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
809
810 if (!sub_mgmt.this) {
811 /* user has performed hdl unsub
812 * we must remove it from the list
813 */
814 MT_LIST_DELETE_SAFE(elt1);
815 /* then free it */
816 _event_hdl_unsubscribe(cur_sub);
817 }
818 } else {
819 /* async mode: here we need to prepare event data
820 * and push it to the event_queue of the task(s)
821 * responsible for consuming the events of current
822 * subscription.
823 * Once the event is pushed, we wake up the associated task.
824 * This feature depends on <haproxy/task> that also
825 * depends on <haproxy/pool>:
826 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
827 * async handler, program may crash.
828 * Hopefully, STG_PREPARE+STG_POOL should be done early in
829 * HAProxy startup sequence.
830 */
831 struct event_hdl_async_event *new_event;
832
833 new_event = pool_alloc(pool_head_sub_event);
834 if (!new_event) {
835 error = 1;
836 break; /* stop on error */
837 }
838 new_event->type = e_type;
839 new_event->private = cur_sub->hdl.private;
Aurelien DARRAGONe9314fb2023-04-04 21:41:10 +0200840 new_event->when = date;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100841 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
842 if (data) {
843 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100844 * event_hdl-t.h file or consider providing dynamic struct members
845 * to reduce overall struct size
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100846 */
847 BUG_ON(data->_size > sizeof(async_data->data));
848 if (!async_data) {
849 /* first async hdl reached - preparing async_data cache */
850 async_data = pool_alloc(pool_head_sub_event_data);
851 if (!async_data) {
852 error = 1;
853 pool_free(pool_head_sub_event, new_event);
854 break; /* stop on error */
855 }
856
857 /* async data assignment */
858 memcpy(async_data->data, data->_ptr, data->_size);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100859 async_data->mfree = data->_mfree;
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100860 /* Initialize refcount, we start at 1 to prevent async
861 * data from being freed by an async handler while we
862 * still use it. We will drop the reference when the
863 * publish is over.
864 *
865 * (first use, atomic operation not required)
866 */
867 async_data->refcount = 1;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100868 }
869 new_event->_data = async_data;
870 new_event->data = async_data->data;
871 /* increment refcount because multiple hdls could
872 * use the same async_data
873 */
874 HA_ATOMIC_INC(&async_data->refcount);
875 } else
876 new_event->data = NULL;
877
878 /* appending new event to event hdl queue */
879 MT_LIST_INIT(&new_event->mt_list);
Aurelien DARRAGONb4b73202023-03-01 15:02:04 +0100880 HA_ATOMIC_INC(&cur_sub->hdl.async_equeue->size);
881 MT_LIST_APPEND(&cur_sub->hdl.async_equeue->head, &new_event->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100882
883 /* wake up the task */
Aurelien DARRAGONb289fd12023-02-28 15:06:48 +0100884 event_hdl_task_wakeup(cur_sub->hdl.async_task);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100885 } /* end async mode */
886 } /* end hdl should be notified */
887 } /* end mt_list */
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100888 if (async_data) {
889 /* we finished publishing, drop the reference on async data */
890 _event_hdl_async_data_drop(async_data);
Aurelien DARRAGONebf58e92023-03-23 19:09:15 +0100891 } else {
892 /* no async subscribers, we are responsible for calling the data
893 * member freeing function if it was provided
894 */
895 if (data && data->_mfree)
896 data->_mfree(data->_ptr);
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100897 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100898 if (error) {
899 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
900 return 0;
901 }
902 return 1;
903}
904
905/* Publish function should not be used from high calling rate or time sensitive
906 * places for now, because list lookup based on e_type is not optimized at
907 * all!
908 * Returns 1 in case of SUCCESS:
909 * Subscribed handlers were notified successfully
910 * Returns 0 in case of FAILURE:
911 * FAILURE means memory error while handling the very first async handler from
912 * the subscription list.
913 * As async handlers are executed first within the list, when such failure occurs
914 * you can safely assume that no events were published for the current call
915 */
916int event_hdl_publish(event_hdl_sub_list *sub_list,
917 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
918{
919 if (!e_type.family) {
920 /* do nothing, these types are reserved for internal use only
921 * (ie: unregistering) */
922 return 0;
923 }
924 if (sub_list) {
925 /* if sublist is provided, first publish event to list subscribers */
926 return _event_hdl_publish(sub_list, e_type, data);
927 } else {
928 /* publish to global list */
929 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
930 }
931}
932
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100933void event_hdl_sub_list_init(event_hdl_sub_list *sub_list)
934{
935 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
936 MT_LIST_INIT(&sub_list->head);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100937 MT_LIST_APPEND(&known_event_hdl_sub_list, &sub_list->known);
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100938}
939
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100940/* internal function, assumes that sub_list ptr is always valid */
941static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100942{
943 struct event_hdl_sub *cur_sub;
944 struct mt_list *elt1, elt2;
945
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100946 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100947 /* remove cur elem from list */
948 MT_LIST_DELETE_SAFE(elt1);
949 /* then free it */
950 _event_hdl_unsubscribe(cur_sub);
951 }
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100952}
953
954/* when a subscription list is no longer used, call this
955 * to do the cleanup and make sure all related subscriptions are
956 * safely ended according to their types
957 */
958void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
959{
960 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
961 if (!MT_LIST_DELETE(&sub_list->known))
962 return; /* already destroyed */
963 _event_hdl_sub_list_destroy(sub_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100964}
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100965
966INITCALL0(STG_INIT, event_hdl_init);