blob: a9fa93a893a35726455239bf8686d3b1137988b5 [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
199/* task handler used for normal async subscription mode
200 * if you use advanced async subscription mode, you can use this
201 * as an example to implement your own task wrapper
202 */
203static struct task *event_hdl_async_task_default(struct task *task, void *ctx, unsigned int state)
204{
205 struct tasklet *tl = (struct tasklet *)task;
206 struct event_hdl_async_task_default_ctx *task_ctx = ctx;
207 struct event_hdl_async_event *event;
208 int max_notif_at_once_it = 0;
209 uint8_t done = 0;
210
211 /* run through e_queue, and call func() for each event
212 * if we read END event, it indicates we must stop:
213 * no more events to come (handler is unregistered)
214 * so we must free task_ctx and stop task
215 */
216 while (max_notif_at_once_it < event_hdl_async_max_notif_at_once &&
217 (event = event_hdl_async_equeue_pop(&task_ctx->e_queue)))
218 {
219 if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
220 done = 1;
221 event_hdl_async_free_event(event);
222 /* break is normally not even required, EVENT_HDL_SUB_END
223 * is guaranteed to be last event of e_queue
224 * (because in normal mode one sub == one e_queue)
225 */
226 break;
227 }
228 else {
229 struct event_hdl_cb cb;
230
231 cb.e_type = event->type;
232 cb.e_data = event->data;
233 cb.sub_mgmt = &event->sub_mgmt;
234 cb._sync = 0;
235
236 /* call user function */
237 task_ctx->func(&cb, event->private);
238 max_notif_at_once_it++;
239 }
240 event_hdl_async_free_event(event);
241 }
242
243 if (done) {
244 /* our job is done, subscription is over: no more events to come */
245 pool_free(pool_head_sub_taskctx, task_ctx);
246 tasklet_free(tl);
247 return NULL;
248 }
249 return task;
250}
251
252/* internal subscription mgmt functions */
253static inline struct event_hdl_sub_type _event_hdl_getsub(struct event_hdl_sub *cur_sub)
254{
255 return cur_sub->sub;
256}
257
258static inline struct event_hdl_sub_type _event_hdl_getsub_async(struct event_hdl_sub *cur_sub)
259{
260 struct mt_list lock;
261 struct event_hdl_sub_type type = EVENT_HDL_SUB_NONE;
262
263 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
264 if (lock.next != &cur_sub->mt_list)
265 type = _event_hdl_getsub(cur_sub);
266 // else already removed
267 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
268 return type;
269}
270
271static inline int _event_hdl_resub(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
272{
273 if (!event_hdl_sub_family_equal(cur_sub->sub, type))
274 return 0; /* family types differ, do nothing */
275 cur_sub->sub.subtype = type.subtype; /* new subtype assignment */
276 return 1;
277}
278
279static inline int _event_hdl_resub_async(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
280{
281 int status = 0;
282 struct mt_list lock;
283
284 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
285 if (lock.next != &cur_sub->mt_list)
286 status = _event_hdl_resub(cur_sub, type);
287 // else already removed
288 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
289 return status;
290}
291
292static inline void _event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
293{
294 struct mt_list lock;
295
296 if (del_sub->hdl.async) {
297 /* ASYNC SUB MODE */
298 /* push EVENT_HDL_SUB_END (to notify the task that the subscription is dead) */
299
300 /* push END EVENT in busy state so we can safely wakeup
301 * the task before releasing it.
302 * Not doing that would expose us to a race where the task could've already
303 * consumed the END event before the wakeup, and some tasks
304 * kill themselves (ie: normal async mode) when they receive such event
305 */
306 lock = MT_LIST_APPEND_LOCKED(del_sub->hdl.async_equeue, &del_sub->async_end->mt_list);
307
308 /* wake up the task */
309 tasklet_wakeup(del_sub->hdl.async_task);
310
311 /* unlock END EVENT (we're done, the task is now free to consume it) */
312 MT_LIST_UNLOCK_ELT(&del_sub->async_end->mt_list, lock);
313
314 /* we don't free sub here
315 * freeing will be performed by async task so it can safely rely
316 * on the pointer until it notices it
317 */
318 } else {
319 /* SYNC SUB MODE */
320
321 /* we can directly free the subscription:
322 * no other thread can access it since we successfully
323 * removed it from the list
324 */
325 event_hdl_drop(del_sub);
326 }
327}
328
329static inline void _event_hdl_unsubscribe_async(struct event_hdl_sub *del_sub)
330{
331 if (!MT_LIST_DELETE(&del_sub->mt_list))
332 return; /* already removed (but may be pending in e_queues) */
333 _event_hdl_unsubscribe(del_sub);
334}
335
336/* sub_mgmt function pointers (for handlers) */
337static struct event_hdl_sub_type event_hdl_getsub_sync(const struct event_hdl_sub_mgmt *mgmt)
338{
339 if (!mgmt)
340 return EVENT_HDL_SUB_NONE;
341
342 if (!mgmt->this)
343 return EVENT_HDL_SUB_NONE; /* already removed from sync ctx */
344 return _event_hdl_getsub(mgmt->this);
345}
346
347static struct event_hdl_sub_type event_hdl_getsub_async(const struct event_hdl_sub_mgmt *mgmt)
348{
349 if (!mgmt)
350 return EVENT_HDL_SUB_NONE;
351
352 return _event_hdl_getsub_async(mgmt->this);
353}
354
355static int event_hdl_resub_sync(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
356{
357 if (!mgmt)
358 return 0;
359
360 if (!mgmt->this)
361 return 0; /* already removed from sync ctx */
362 return _event_hdl_resub(mgmt->this, type);
363}
364
365static int event_hdl_resub_async(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
366{
367 if (!mgmt)
368 return 0;
369
370 return _event_hdl_resub_async(mgmt->this, type);
371}
372
373static void event_hdl_unsubscribe_sync(const struct event_hdl_sub_mgmt *mgmt)
374{
375 if (!mgmt)
376 return;
377
378 if (!mgmt->this)
379 return; /* already removed from sync ctx */
380
381 /* assuming that publish sync code will notice that mgmt->this is NULL
382 * and will perform the list removal using MT_LIST_DELETE_SAFE and
383 * _event_hdl_unsubscribe()
384 * while still owning the lock
385 */
386 ((struct event_hdl_sub_mgmt *)mgmt)->this = NULL;
387}
388
389static void event_hdl_unsubscribe_async(const struct event_hdl_sub_mgmt *mgmt)
390{
391 if (!mgmt)
392 return;
393
394 _event_hdl_unsubscribe_async(mgmt->this);
395}
396
397#define EVENT_HDL_SUB_MGMT_ASYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
398 .getsub = event_hdl_getsub_async, \
399 .resub = event_hdl_resub_async, \
400 .unsub = event_hdl_unsubscribe_async}
401#define EVENT_HDL_SUB_MGMT_SYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
402 .getsub = event_hdl_getsub_sync, \
403 .resub = event_hdl_resub_sync, \
404 .unsub = event_hdl_unsubscribe_sync}
405
406struct event_hdl_sub *event_hdl_subscribe_ptr(event_hdl_sub_list *sub_list,
407 struct event_hdl_sub_type e_type, struct event_hdl hdl)
408{
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100409 struct event_hdl_sub *new_sub = NULL;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100410 struct mt_list *elt1, elt2;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100411 struct event_hdl_async_task_default_ctx *task_ctx = NULL;
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100412 struct mt_list lock;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100413
414 if (!sub_list)
415 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
416
417 /* hdl API consistency check */
418 /*FIXME: do we need to ensure that if private is set, private_free should be set as well? */
419 BUG_ON((!hdl.async && !hdl.sync_ptr) ||
420 (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL && !hdl.async_ptr) ||
421 (hdl.async == EVENT_HDL_ASYNC_MODE_ADVANCED &&
422 (!hdl.async_equeue || !hdl.async_task)));
423
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100424 new_sub = pool_alloc(pool_head_sub);
425 if (new_sub == NULL) {
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100426 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100427 }
428
429 /* assignments */
430 new_sub->sub.family = e_type.family;
431 new_sub->sub.subtype = e_type.subtype;
432 new_sub->hdl = hdl;
433
434 if (hdl.async) {
435 /* async END event pre-allocation */
436 new_sub->async_end = pool_alloc(pool_head_sub_event);
437 if (!new_sub->async_end) {
438 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100439 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100440 }
441 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
442 /* normal mode: no task provided, we must initialize it */
443
444 /* initialize task context */
445 task_ctx = pool_alloc(pool_head_sub_taskctx);
446
447 if (!task_ctx) {
448 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100449 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100450 }
451 MT_LIST_INIT(&task_ctx->e_queue);
452 task_ctx->func = new_sub->hdl.async_ptr;
453
454 new_sub->hdl.async_equeue = &task_ctx->e_queue;
455 new_sub->hdl.async_task = tasklet_new();
456
457 if (!new_sub->hdl.async_task) {
458 /* memory error */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100459 goto memory_error;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100460 }
461 new_sub->hdl.async_task->context = task_ctx;
462 new_sub->hdl.async_task->process = event_hdl_async_task_default;
463 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100464 /* initialize END event (used to notify about subscription ending)
465 * used by both normal and advanced mode:
466 * - to safely terminate the task in normal mode
467 * - to safely free subscription and
468 * keep track of active subscriptions in advanced mode
469 */
470 new_sub->async_end->type = EVENT_HDL_SUB_END;
471 new_sub->async_end->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(new_sub);
472 new_sub->async_end->private = new_sub->hdl.private;
473 new_sub->async_end->_data = NULL;
474 MT_LIST_INIT(&new_sub->async_end->mt_list);
475 }
476 /* set refcount to 2:
477 * 1 for handler (because handler can manage the subscription itself)
478 * 1 for caller (will be dropped automatically if caller use the non-ptr version)
479 */
480 new_sub->refcount = 2;
481
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100482 /* ready for registration */
483 MT_LIST_INIT(&new_sub->mt_list);
484
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100485 lock = MT_LIST_LOCK_ELT(&sub_list->known);
486
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100487 /* check if such identified hdl is not already registered */
488 if (hdl.id) {
489 struct event_hdl_sub *cur_sub;
490 uint8_t found = 0;
491
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100492 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
493 if (hdl.id == cur_sub->hdl.id) {
494 /* we found matching registered hdl */
495 found = 1;
496 break;
497 }
498 }
499 if (found) {
500 /* error already registered */
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100501 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100502 event_hdl_report_hdl_state(ha_alert, &hdl, "SUB", "could not subscribe: subscription with this id already exists");
503 goto cleanup;
504 }
505 }
506
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100507 if (lock.next == &sub_list->known) {
508 /* this is an expected corner case on de-init path, a subscribe attempt
509 * was made but the subscription list is already destroyed, we pretend
510 * it is a memory/IO error since it should not be long before haproxy
511 * enters the deinit() function anyway
512 */
513 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
514 goto cleanup;
515 }
516
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100517 /* Append in list (global or user specified list).
518 * For now, append when sync mode, and insert when async mode
519 * so that async handlers are executed first
520 */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100521 if (hdl.async) {
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100522 /* Prevent the task from being aborted on soft-stop: let's wait
523 * until the END event is acknowledged by the task.
524 * (decrease is performed in event_hdl_async_free_event())
525 *
526 * If we don't do this, event_hdl API will leak and we won't give
527 * a chance to the event-handling task to perform cleanup
528 */
529 HA_ATOMIC_INC(&jobs);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100530 /* async mode, insert at the beginning of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100531 MT_LIST_INSERT(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100532 } else {
533 /* sync mode, append at the end of the list */
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100534 MT_LIST_APPEND(&sub_list->head, &new_sub->mt_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100535 }
536
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100537 MT_LIST_UNLOCK_ELT(&sub_list->known, lock);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100538
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100539 return new_sub;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100540
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100541 cleanup:
542 if (new_sub) {
543 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
544 if (new_sub->hdl.async_task)
545 tasklet_free(new_sub->hdl.async_task);
546 if (task_ctx)
547 pool_free(pool_head_sub_taskctx, task_ctx);
548 }
549 if (hdl.async)
550 pool_free(pool_head_sub_event, new_sub->async_end);
551 pool_free(pool_head_sub, new_sub);
552 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100553
554 return NULL;
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100555
556 memory_error:
557 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
558 goto cleanup;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100559}
560
561void event_hdl_take(struct event_hdl_sub *sub)
562{
563 HA_ATOMIC_INC(&sub->refcount);
564}
565
566void event_hdl_drop(struct event_hdl_sub *sub)
567{
568 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
569 return;
570
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100571 /* we were the last one holding a reference to event sub - free required */
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100572 if (sub->hdl.private_free) {
573 /* free private data if specified upon registration */
574 sub->hdl.private_free(sub->hdl.private);
575 }
576 pool_free(pool_head_sub, sub);
577}
578
579int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
580{
581 return _event_hdl_resub_async(cur_sub, type);
582}
583
584void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
585{
586 _event_hdl_unsubscribe_async(del_sub);
587 /* drop refcount, assuming caller no longer use ptr */
588 event_hdl_drop(del_sub);
589}
590
591int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
592{
593 struct event_hdl_sub *sub;
594
595 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
596 if (sub) {
597 /* drop refcount because the user is not willing to hold a reference */
598 event_hdl_drop(sub);
599 return 1;
600 }
601 return 0;
602}
603
604/* Subscription external lookup functions
605 */
606int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
607 uint64_t lookup_id)
608{
609 struct event_hdl_sub *del_sub = NULL;
610 struct mt_list *elt1, elt2;
611 int found = 0;
612
613 if (!sub_list)
614 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
615
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100616 mt_list_for_each_entry_safe(del_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100617 if (lookup_id == del_sub->hdl.id) {
618 /* we found matching registered hdl */
619 MT_LIST_DELETE_SAFE(elt1);
620 _event_hdl_unsubscribe(del_sub);
621 found = 1;
622 break; /* id is unique, stop searching */
623 }
624 }
625 return found;
626}
627
628int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
629 uint64_t lookup_id, struct event_hdl_sub_type type)
630{
631 struct event_hdl_sub *cur_sub = NULL;
632 struct mt_list *elt1, elt2;
633 int status = 0;
634
635 if (!sub_list)
636 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
637
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100638 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100639 if (lookup_id == cur_sub->hdl.id) {
640 /* we found matching registered hdl */
641 status = _event_hdl_resub(cur_sub, type);
642 break; /* id is unique, stop searching */
643 }
644 }
645 return status;
646}
647
648struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
649 uint64_t lookup_id)
650{
651 struct event_hdl_sub *cur_sub = NULL;
652 struct mt_list *elt1, elt2;
653 uint8_t found = 0;
654
655 if (!sub_list)
656 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
657
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100658 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100659 if (lookup_id == cur_sub->hdl.id) {
660 /* we found matching registered hdl */
661 event_hdl_take(cur_sub);
662 found = 1;
663 break; /* id is unique, stop searching */
664 }
665 }
666 if (found)
667 return cur_sub;
668 return NULL;
669}
670
671/* event publishing functions
672 */
673static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
674 const struct event_hdl_cb_data *data)
675{
676 struct event_hdl_sub *cur_sub;
677 struct mt_list *elt1, elt2;
678 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
679 int error = 0;
680
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100681 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100682 /* notify each function that has subscribed to sub_family.type */
683 if ((cur_sub->sub.family == e_type.family) &&
684 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype)) {
685 /* hdl should be notified */
686 if (!cur_sub->hdl.async) {
687 /* sync mode: simply call cb pointer
688 * it is up to the callee to schedule a task if needed or
689 * take specific precautions in order to return as fast as possible
690 * and not use locks that are already held by the caller
691 */
692 struct event_hdl_cb cb;
693 struct event_hdl_sub_mgmt sub_mgmt;
694
695 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
696 cb.e_type = e_type;
697 if (data)
698 cb.e_data = data->_ptr;
699 else
700 cb.e_data = NULL;
701 cb.sub_mgmt = &sub_mgmt;
702 cb._sync = 1;
703
704 /* call user function */
705 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
706
707 if (!sub_mgmt.this) {
708 /* user has performed hdl unsub
709 * we must remove it from the list
710 */
711 MT_LIST_DELETE_SAFE(elt1);
712 /* then free it */
713 _event_hdl_unsubscribe(cur_sub);
714 }
715 } else {
716 /* async mode: here we need to prepare event data
717 * and push it to the event_queue of the task(s)
718 * responsible for consuming the events of current
719 * subscription.
720 * Once the event is pushed, we wake up the associated task.
721 * This feature depends on <haproxy/task> that also
722 * depends on <haproxy/pool>:
723 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
724 * async handler, program may crash.
725 * Hopefully, STG_PREPARE+STG_POOL should be done early in
726 * HAProxy startup sequence.
727 */
728 struct event_hdl_async_event *new_event;
729
730 new_event = pool_alloc(pool_head_sub_event);
731 if (!new_event) {
732 error = 1;
733 break; /* stop on error */
734 }
735 new_event->type = e_type;
736 new_event->private = cur_sub->hdl.private;
737 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
738 if (data) {
739 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
740 * event_hdl-t.h file
741 */
742 BUG_ON(data->_size > sizeof(async_data->data));
743 if (!async_data) {
744 /* first async hdl reached - preparing async_data cache */
745 async_data = pool_alloc(pool_head_sub_event_data);
746 if (!async_data) {
747 error = 1;
748 pool_free(pool_head_sub_event, new_event);
749 break; /* stop on error */
750 }
751
752 /* async data assignment */
753 memcpy(async_data->data, data->_ptr, data->_size);
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100754 /* Initialize refcount, we start at 1 to prevent async
755 * data from being freed by an async handler while we
756 * still use it. We will drop the reference when the
757 * publish is over.
758 *
759 * (first use, atomic operation not required)
760 */
761 async_data->refcount = 1;
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100762 }
763 new_event->_data = async_data;
764 new_event->data = async_data->data;
765 /* increment refcount because multiple hdls could
766 * use the same async_data
767 */
768 HA_ATOMIC_INC(&async_data->refcount);
769 } else
770 new_event->data = NULL;
771
772 /* appending new event to event hdl queue */
773 MT_LIST_INIT(&new_event->mt_list);
774 MT_LIST_APPEND(cur_sub->hdl.async_equeue, &new_event->mt_list);
775
776 /* wake up the task */
777 tasklet_wakeup(cur_sub->hdl.async_task);
778 } /* end async mode */
779 } /* end hdl should be notified */
780 } /* end mt_list */
Aurelien DARRAGONafcfc202023-03-22 10:42:20 +0100781 if (async_data) {
782 /* we finished publishing, drop the reference on async data */
783 _event_hdl_async_data_drop(async_data);
784 }
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100785 if (error) {
786 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
787 return 0;
788 }
789 return 1;
790}
791
792/* Publish function should not be used from high calling rate or time sensitive
793 * places for now, because list lookup based on e_type is not optimized at
794 * all!
795 * Returns 1 in case of SUCCESS:
796 * Subscribed handlers were notified successfully
797 * Returns 0 in case of FAILURE:
798 * FAILURE means memory error while handling the very first async handler from
799 * the subscription list.
800 * As async handlers are executed first within the list, when such failure occurs
801 * you can safely assume that no events were published for the current call
802 */
803int event_hdl_publish(event_hdl_sub_list *sub_list,
804 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
805{
806 if (!e_type.family) {
807 /* do nothing, these types are reserved for internal use only
808 * (ie: unregistering) */
809 return 0;
810 }
811 if (sub_list) {
812 /* if sublist is provided, first publish event to list subscribers */
813 return _event_hdl_publish(sub_list, e_type, data);
814 } else {
815 /* publish to global list */
816 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
817 }
818}
819
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100820void event_hdl_sub_list_init(event_hdl_sub_list *sub_list)
821{
822 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
823 MT_LIST_INIT(&sub_list->head);
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100824 MT_LIST_APPEND(&known_event_hdl_sub_list, &sub_list->known);
Aurelien DARRAGON3a81e992023-03-16 11:16:05 +0100825}
826
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100827/* internal function, assumes that sub_list ptr is always valid */
828static void _event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100829{
830 struct event_hdl_sub *cur_sub;
831 struct mt_list *elt1, elt2;
832
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100833 mt_list_for_each_entry_safe(cur_sub, &sub_list->head, mt_list, elt1, elt2) {
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100834 /* remove cur elem from list */
835 MT_LIST_DELETE_SAFE(elt1);
836 /* then free it */
837 _event_hdl_unsubscribe(cur_sub);
838 }
Aurelien DARRAGONef6ca672023-03-16 10:54:24 +0100839}
840
841/* when a subscription list is no longer used, call this
842 * to do the cleanup and make sure all related subscriptions are
843 * safely ended according to their types
844 */
845void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
846{
847 BUG_ON(!sub_list); /* unexpected, global sublist is managed internally */
848 if (!MT_LIST_DELETE(&sub_list->known))
849 return; /* already destroyed */
850 _event_hdl_sub_list_destroy(sub_list);
Aurelien DARRAGON68e692d2022-11-16 18:06:28 +0100851}
Aurelien DARRAGONd514ca42023-02-23 19:12:49 +0100852
853INITCALL0(STG_INIT, event_hdl_init);