blob: 06f4ae4783003a7ba9a5551c05c1d1b9884d6255 [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>
19#include <haproxy/xxhash.h>
20
21/* event types changes in event_hdl-t.h file should be reflected in the
22 * map below to allow string to type and type to string conversions
23 */
24static struct event_hdl_sub_type_map event_hdl_sub_type_map[] = {
25 {"NONE", EVENT_HDL_SUB_NONE},
26 {"SERVER", EVENT_HDL_SUB_SERVER},
27 {"SERVER_ADD", EVENT_HDL_SUB_SERVER_ADD},
28 {"SERVER_DEL", EVENT_HDL_SUB_SERVER_DEL},
29};
30
31/* internal types (only used in this file) */
32struct event_hdl_async_task_default_ctx
33{
34 event_hdl_async_equeue e_queue; /* event queue list */
35 event_hdl_cb_async func; /* event handling func */
36};
37
38/* memory pools declarations */
39DECLARE_STATIC_POOL(pool_head_sub, "ehdl_sub", sizeof(struct event_hdl_sub));
40DECLARE_STATIC_POOL(pool_head_sub_event, "ehdl_sub_e", sizeof(struct event_hdl_async_event));
41DECLARE_STATIC_POOL(pool_head_sub_event_data, "ehdl_sub_ed", sizeof(struct event_hdl_async_event_data));
42DECLARE_STATIC_POOL(pool_head_sub_taskctx, "ehdl_sub_tctx", sizeof(struct event_hdl_async_task_default_ctx));
43
44/* global subscription list (implicit where NULL is used as sublist argument) */
45static struct mt_list global_event_hdl_sub_list = MT_LIST_HEAD_INIT(global_event_hdl_sub_list);
46
47/* 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
52/* general purpose hashing function when you want to compute
53 * an ID based on <scope> x <name>
54 * It is your responsibility to make sure <scope> is not used
55 * elsewhere in the code (or that you are fine with sharing
56 * the scope).
57 */
58inline uint64_t event_hdl_id(const char *scope, const char *name)
59{
60 XXH64_state_t state;
61
62 XXH64_reset(&state, 0);
63 XXH64_update(&state, scope, strlen(scope));
64 XXH64_update(&state, name, strlen(name));
65 return XXH64_digest(&state);
66}
67
68/* takes a sub_type as input, returns corresponding sub_type
69 * printable string or "N/A" if not found.
70 * If not found, an error will be reported to stderr so the developers
71 * know that a sub_type is missing its associated string in event_hdl-t.h
72 */
73const char *event_hdl_sub_type_to_string(struct event_hdl_sub_type sub_type)
74{
75 int it;
76
77 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
78 if (sub_type.family == event_hdl_sub_type_map[it].type.family &&
79 sub_type.subtype == event_hdl_sub_type_map[it].type.subtype)
80 return event_hdl_sub_type_map[it].name;
81 }
82 ha_alert("event_hdl-t.h: missing sub_type string representation.\n"
83 "Please reflect any changes in event_hdl_sub_type_map.\n");
84 return "N/A";
85}
86
87/* returns the internal sub_type corresponding
88 * to the printable representation <name>
89 * or EVENT_HDL_SUB_NONE if no such event exists
90 * (see event_hdl-t.h for the complete list of supported types)
91 */
92struct event_hdl_sub_type event_hdl_string_to_sub_type(const char *name)
93{
94 int it;
95
96 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
97 if (!strcmp(name, event_hdl_sub_type_map[it].name))
98 return event_hdl_sub_type_map[it].type;
99 }
100 return EVENT_HDL_SUB_NONE;
101}
102
103/* Takes <subscriptions> sub list as input, returns a printable string
104 * containing every sub_types contained in <subscriptions>
105 * separated by '|' char.
106 * Returns NULL if no sub_types are found in <subscriptions>
107 * This functions leverages memprintf, thus it is up to the
108 * caller to free the returned value (if != NULL) when he no longer
109 * uses it.
110 */
111char *event_hdl_sub_type_print(struct event_hdl_sub_type subscriptions)
112{
113 char *out = NULL;
114 int it;
115 uint8_t first = 1;
116
117 for (it = 0; it < (int)(sizeof(event_hdl_sub_type_map) / sizeof(event_hdl_sub_type_map[0])); it++) {
118 if (subscriptions.family == event_hdl_sub_type_map[it].type.family &&
119 ((subscriptions.subtype & event_hdl_sub_type_map[it].type.subtype) ==
120 event_hdl_sub_type_map[it].type.subtype)) {
121 if (first) {
122 memprintf(&out, "%s", event_hdl_sub_type_map[it].name);
123 first--;
124 }
125 else
126 memprintf(&out, "%s%s%s", out, "|", event_hdl_sub_type_map[it].name);
127 }
128 }
129
130 return out;
131}
132
133/* event_hdl debug/reporting function */
134typedef void (*event_hdl_report_hdl_state_func)(const char *fmt, ...);
135static void event_hdl_report_hdl_state(event_hdl_report_hdl_state_func report_func,
136 const struct event_hdl *hdl, const char *what, const char *state)
137{
138 report_func("[event_hdl]:%s (%s)'#%llu@%s': %s\n",
139 what,
140 (hdl->async) ? "ASYNC" : "SYNC",
141 (long long unsigned int)hdl->id,
142 hdl->dorigin,
143 state);
144}
145
146void event_hdl_async_free_event(struct event_hdl_async_event *e)
147{
148 if (unlikely(event_hdl_sub_type_equal(e->type, EVENT_HDL_SUB_END))) {
149 /* last event for hdl, special case */
150 /* free subscription entry as we're the last one still using it
151 * (it is already removed from mt_list, no race can occur)
152 */
153 event_hdl_drop(e->sub_mgmt.this);
154 }
155 else if (e->_data &&
156 HA_ATOMIC_SUB_FETCH(&e->_data->refcount, 1) == 0) {
157 /* we are the last event holding reference to event data - free required */
158 pool_free(pool_head_sub_event_data, e->_data); /* data wrapper */
159 }
160 pool_free(pool_head_sub_event, e);
161}
162
163/* task handler used for normal async subscription mode
164 * if you use advanced async subscription mode, you can use this
165 * as an example to implement your own task wrapper
166 */
167static struct task *event_hdl_async_task_default(struct task *task, void *ctx, unsigned int state)
168{
169 struct tasklet *tl = (struct tasklet *)task;
170 struct event_hdl_async_task_default_ctx *task_ctx = ctx;
171 struct event_hdl_async_event *event;
172 int max_notif_at_once_it = 0;
173 uint8_t done = 0;
174
175 /* run through e_queue, and call func() for each event
176 * if we read END event, it indicates we must stop:
177 * no more events to come (handler is unregistered)
178 * so we must free task_ctx and stop task
179 */
180 while (max_notif_at_once_it < event_hdl_async_max_notif_at_once &&
181 (event = event_hdl_async_equeue_pop(&task_ctx->e_queue)))
182 {
183 if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
184 done = 1;
185 event_hdl_async_free_event(event);
186 /* break is normally not even required, EVENT_HDL_SUB_END
187 * is guaranteed to be last event of e_queue
188 * (because in normal mode one sub == one e_queue)
189 */
190 break;
191 }
192 else {
193 struct event_hdl_cb cb;
194
195 cb.e_type = event->type;
196 cb.e_data = event->data;
197 cb.sub_mgmt = &event->sub_mgmt;
198 cb._sync = 0;
199
200 /* call user function */
201 task_ctx->func(&cb, event->private);
202 max_notif_at_once_it++;
203 }
204 event_hdl_async_free_event(event);
205 }
206
207 if (done) {
208 /* our job is done, subscription is over: no more events to come */
209 pool_free(pool_head_sub_taskctx, task_ctx);
210 tasklet_free(tl);
211 return NULL;
212 }
213 return task;
214}
215
216/* internal subscription mgmt functions */
217static inline struct event_hdl_sub_type _event_hdl_getsub(struct event_hdl_sub *cur_sub)
218{
219 return cur_sub->sub;
220}
221
222static inline struct event_hdl_sub_type _event_hdl_getsub_async(struct event_hdl_sub *cur_sub)
223{
224 struct mt_list lock;
225 struct event_hdl_sub_type type = EVENT_HDL_SUB_NONE;
226
227 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
228 if (lock.next != &cur_sub->mt_list)
229 type = _event_hdl_getsub(cur_sub);
230 // else already removed
231 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
232 return type;
233}
234
235static inline int _event_hdl_resub(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
236{
237 if (!event_hdl_sub_family_equal(cur_sub->sub, type))
238 return 0; /* family types differ, do nothing */
239 cur_sub->sub.subtype = type.subtype; /* new subtype assignment */
240 return 1;
241}
242
243static inline int _event_hdl_resub_async(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
244{
245 int status = 0;
246 struct mt_list lock;
247
248 lock = MT_LIST_LOCK_ELT(&cur_sub->mt_list);
249 if (lock.next != &cur_sub->mt_list)
250 status = _event_hdl_resub(cur_sub, type);
251 // else already removed
252 MT_LIST_UNLOCK_ELT(&cur_sub->mt_list, lock);
253 return status;
254}
255
256static inline void _event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
257{
258 struct mt_list lock;
259
260 if (del_sub->hdl.async) {
261 /* ASYNC SUB MODE */
262 /* push EVENT_HDL_SUB_END (to notify the task that the subscription is dead) */
263
264 /* push END EVENT in busy state so we can safely wakeup
265 * the task before releasing it.
266 * Not doing that would expose us to a race where the task could've already
267 * consumed the END event before the wakeup, and some tasks
268 * kill themselves (ie: normal async mode) when they receive such event
269 */
270 lock = MT_LIST_APPEND_LOCKED(del_sub->hdl.async_equeue, &del_sub->async_end->mt_list);
271
272 /* wake up the task */
273 tasklet_wakeup(del_sub->hdl.async_task);
274
275 /* unlock END EVENT (we're done, the task is now free to consume it) */
276 MT_LIST_UNLOCK_ELT(&del_sub->async_end->mt_list, lock);
277
278 /* we don't free sub here
279 * freeing will be performed by async task so it can safely rely
280 * on the pointer until it notices it
281 */
282 } else {
283 /* SYNC SUB MODE */
284
285 /* we can directly free the subscription:
286 * no other thread can access it since we successfully
287 * removed it from the list
288 */
289 event_hdl_drop(del_sub);
290 }
291}
292
293static inline void _event_hdl_unsubscribe_async(struct event_hdl_sub *del_sub)
294{
295 if (!MT_LIST_DELETE(&del_sub->mt_list))
296 return; /* already removed (but may be pending in e_queues) */
297 _event_hdl_unsubscribe(del_sub);
298}
299
300/* sub_mgmt function pointers (for handlers) */
301static struct event_hdl_sub_type event_hdl_getsub_sync(const struct event_hdl_sub_mgmt *mgmt)
302{
303 if (!mgmt)
304 return EVENT_HDL_SUB_NONE;
305
306 if (!mgmt->this)
307 return EVENT_HDL_SUB_NONE; /* already removed from sync ctx */
308 return _event_hdl_getsub(mgmt->this);
309}
310
311static struct event_hdl_sub_type event_hdl_getsub_async(const struct event_hdl_sub_mgmt *mgmt)
312{
313 if (!mgmt)
314 return EVENT_HDL_SUB_NONE;
315
316 return _event_hdl_getsub_async(mgmt->this);
317}
318
319static int event_hdl_resub_sync(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
320{
321 if (!mgmt)
322 return 0;
323
324 if (!mgmt->this)
325 return 0; /* already removed from sync ctx */
326 return _event_hdl_resub(mgmt->this, type);
327}
328
329static int event_hdl_resub_async(const struct event_hdl_sub_mgmt *mgmt, struct event_hdl_sub_type type)
330{
331 if (!mgmt)
332 return 0;
333
334 return _event_hdl_resub_async(mgmt->this, type);
335}
336
337static void event_hdl_unsubscribe_sync(const struct event_hdl_sub_mgmt *mgmt)
338{
339 if (!mgmt)
340 return;
341
342 if (!mgmt->this)
343 return; /* already removed from sync ctx */
344
345 /* assuming that publish sync code will notice that mgmt->this is NULL
346 * and will perform the list removal using MT_LIST_DELETE_SAFE and
347 * _event_hdl_unsubscribe()
348 * while still owning the lock
349 */
350 ((struct event_hdl_sub_mgmt *)mgmt)->this = NULL;
351}
352
353static void event_hdl_unsubscribe_async(const struct event_hdl_sub_mgmt *mgmt)
354{
355 if (!mgmt)
356 return;
357
358 _event_hdl_unsubscribe_async(mgmt->this);
359}
360
361#define EVENT_HDL_SUB_MGMT_ASYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
362 .getsub = event_hdl_getsub_async, \
363 .resub = event_hdl_resub_async, \
364 .unsub = event_hdl_unsubscribe_async}
365#define EVENT_HDL_SUB_MGMT_SYNC(_sub) (struct event_hdl_sub_mgmt){ .this = _sub, \
366 .getsub = event_hdl_getsub_sync, \
367 .resub = event_hdl_resub_sync, \
368 .unsub = event_hdl_unsubscribe_sync}
369
370struct event_hdl_sub *event_hdl_subscribe_ptr(event_hdl_sub_list *sub_list,
371 struct event_hdl_sub_type e_type, struct event_hdl hdl)
372{
373 struct event_hdl_sub *new_sub;
374 struct mt_list *elt1, elt2;
375 uint8_t found = 0;
376 struct event_hdl_async_task_default_ctx *task_ctx;
377
378 if (!sub_list)
379 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
380
381 /* hdl API consistency check */
382 /*FIXME: do we need to ensure that if private is set, private_free should be set as well? */
383 BUG_ON((!hdl.async && !hdl.sync_ptr) ||
384 (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL && !hdl.async_ptr) ||
385 (hdl.async == EVENT_HDL_ASYNC_MODE_ADVANCED &&
386 (!hdl.async_equeue || !hdl.async_task)));
387
388 /* first check if such identified hdl is not already registered */
389 if (hdl.id) {
390 mt_list_for_each_entry_safe(new_sub, sub_list, mt_list, elt1, elt2) {
391 if (hdl.id == new_sub->hdl.id) {
392 /* we found matching registered hdl */
393 found = 1;
394 break;
395 }
396 }
397 }
398
399 if (found) {
400 /* error already registered */
401 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not subscribe: subscription with this id already exists");
402 return NULL;
403 }
404
405 new_sub = pool_alloc(pool_head_sub);
406 if (new_sub == NULL) {
407 goto new_sub_memory_error;
408 }
409
410 /* assignments */
411 new_sub->sub.family = e_type.family;
412 new_sub->sub.subtype = e_type.subtype;
413 new_sub->hdl = hdl;
414
415 if (hdl.async) {
416 /* async END event pre-allocation */
417 new_sub->async_end = pool_alloc(pool_head_sub_event);
418 if (!new_sub->async_end) {
419 /* memory error */
420 goto new_sub_memory_error_event_end;
421 }
422 if (hdl.async == EVENT_HDL_ASYNC_MODE_NORMAL) {
423 /* normal mode: no task provided, we must initialize it */
424
425 /* initialize task context */
426 task_ctx = pool_alloc(pool_head_sub_taskctx);
427
428 if (!task_ctx) {
429 /* memory error */
430 goto new_sub_memory_error_task_ctx;
431 }
432 MT_LIST_INIT(&task_ctx->e_queue);
433 task_ctx->func = new_sub->hdl.async_ptr;
434
435 new_sub->hdl.async_equeue = &task_ctx->e_queue;
436 new_sub->hdl.async_task = tasklet_new();
437
438 if (!new_sub->hdl.async_task) {
439 /* memory error */
440 goto new_sub_memory_error_task;
441 }
442 new_sub->hdl.async_task->context = task_ctx;
443 new_sub->hdl.async_task->process = event_hdl_async_task_default;
444 }
445 /* registration cannot fail anymore */
446
447 /* initialize END event (used to notify about subscription ending)
448 * used by both normal and advanced mode:
449 * - to safely terminate the task in normal mode
450 * - to safely free subscription and
451 * keep track of active subscriptions in advanced mode
452 */
453 new_sub->async_end->type = EVENT_HDL_SUB_END;
454 new_sub->async_end->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(new_sub);
455 new_sub->async_end->private = new_sub->hdl.private;
456 new_sub->async_end->_data = NULL;
457 MT_LIST_INIT(&new_sub->async_end->mt_list);
458 }
459 /* set refcount to 2:
460 * 1 for handler (because handler can manage the subscription itself)
461 * 1 for caller (will be dropped automatically if caller use the non-ptr version)
462 */
463 new_sub->refcount = 2;
464
465 /* Append in list (global or user specified list).
466 * For now, append when sync mode, and insert when async mode
467 * so that async handlers are executed first
468 */
469 MT_LIST_INIT(&new_sub->mt_list);
470 if (hdl.async) {
471 /* async mode, insert at the beginning of the list */
472 MT_LIST_INSERT(sub_list, &new_sub->mt_list);
473 } else {
474 /* sync mode, append at the end of the list */
475 MT_LIST_APPEND(sub_list, &new_sub->mt_list);
476 }
477
478 return new_sub;
479
480new_sub_memory_error_task:
481 pool_free(pool_head_sub_taskctx, task_ctx);
482new_sub_memory_error_task_ctx:
483 pool_free(pool_head_sub_event, new_sub->async_end);
484new_sub_memory_error_event_end:
485 pool_free(pool_head_sub, new_sub);
486new_sub_memory_error:
487
488 event_hdl_report_hdl_state(ha_warning, &hdl, "SUB", "could not register subscription due to memory error");
489
490 return NULL;
491}
492
493void event_hdl_take(struct event_hdl_sub *sub)
494{
495 HA_ATOMIC_INC(&sub->refcount);
496}
497
498void event_hdl_drop(struct event_hdl_sub *sub)
499{
500 if (HA_ATOMIC_SUB_FETCH(&sub->refcount, 1) != 0)
501 return;
502
503 /* we are the last event holding reference to event data - free required */
504 if (sub->hdl.private_free) {
505 /* free private data if specified upon registration */
506 sub->hdl.private_free(sub->hdl.private);
507 }
508 pool_free(pool_head_sub, sub);
509}
510
511int event_hdl_resubscribe(struct event_hdl_sub *cur_sub, struct event_hdl_sub_type type)
512{
513 return _event_hdl_resub_async(cur_sub, type);
514}
515
516void event_hdl_unsubscribe(struct event_hdl_sub *del_sub)
517{
518 _event_hdl_unsubscribe_async(del_sub);
519 /* drop refcount, assuming caller no longer use ptr */
520 event_hdl_drop(del_sub);
521}
522
523int event_hdl_subscribe(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type, struct event_hdl hdl)
524{
525 struct event_hdl_sub *sub;
526
527 sub = event_hdl_subscribe_ptr(sub_list, e_type, hdl);
528 if (sub) {
529 /* drop refcount because the user is not willing to hold a reference */
530 event_hdl_drop(sub);
531 return 1;
532 }
533 return 0;
534}
535
536/* Subscription external lookup functions
537 */
538int event_hdl_lookup_unsubscribe(event_hdl_sub_list *sub_list,
539 uint64_t lookup_id)
540{
541 struct event_hdl_sub *del_sub = NULL;
542 struct mt_list *elt1, elt2;
543 int found = 0;
544
545 if (!sub_list)
546 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
547
548 mt_list_for_each_entry_safe(del_sub, sub_list, mt_list, elt1, elt2) {
549 if (lookup_id == del_sub->hdl.id) {
550 /* we found matching registered hdl */
551 MT_LIST_DELETE_SAFE(elt1);
552 _event_hdl_unsubscribe(del_sub);
553 found = 1;
554 break; /* id is unique, stop searching */
555 }
556 }
557 return found;
558}
559
560int event_hdl_lookup_resubscribe(event_hdl_sub_list *sub_list,
561 uint64_t lookup_id, struct event_hdl_sub_type type)
562{
563 struct event_hdl_sub *cur_sub = NULL;
564 struct mt_list *elt1, elt2;
565 int status = 0;
566
567 if (!sub_list)
568 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
569
570 mt_list_for_each_entry_safe(cur_sub, sub_list, mt_list, elt1, elt2) {
571 if (lookup_id == cur_sub->hdl.id) {
572 /* we found matching registered hdl */
573 status = _event_hdl_resub(cur_sub, type);
574 break; /* id is unique, stop searching */
575 }
576 }
577 return status;
578}
579
580struct event_hdl_sub *event_hdl_lookup_take(event_hdl_sub_list *sub_list,
581 uint64_t lookup_id)
582{
583 struct event_hdl_sub *cur_sub = NULL;
584 struct mt_list *elt1, elt2;
585 uint8_t found = 0;
586
587 if (!sub_list)
588 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
589
590 mt_list_for_each_entry_safe(cur_sub, sub_list, mt_list, elt1, elt2) {
591 if (lookup_id == cur_sub->hdl.id) {
592 /* we found matching registered hdl */
593 event_hdl_take(cur_sub);
594 found = 1;
595 break; /* id is unique, stop searching */
596 }
597 }
598 if (found)
599 return cur_sub;
600 return NULL;
601}
602
603/* event publishing functions
604 */
605static int _event_hdl_publish(event_hdl_sub_list *sub_list, struct event_hdl_sub_type e_type,
606 const struct event_hdl_cb_data *data)
607{
608 struct event_hdl_sub *cur_sub;
609 struct mt_list *elt1, elt2;
610 struct event_hdl_async_event_data *async_data = NULL; /* reuse async data for multiple async hdls */
611 int error = 0;
612
613 mt_list_for_each_entry_safe(cur_sub, sub_list, mt_list, elt1, elt2) {
614 /* notify each function that has subscribed to sub_family.type */
615 if ((cur_sub->sub.family == e_type.family) &&
616 ((cur_sub->sub.subtype & e_type.subtype) == e_type.subtype)) {
617 /* hdl should be notified */
618 if (!cur_sub->hdl.async) {
619 /* sync mode: simply call cb pointer
620 * it is up to the callee to schedule a task if needed or
621 * take specific precautions in order to return as fast as possible
622 * and not use locks that are already held by the caller
623 */
624 struct event_hdl_cb cb;
625 struct event_hdl_sub_mgmt sub_mgmt;
626
627 sub_mgmt = EVENT_HDL_SUB_MGMT_SYNC(cur_sub);
628 cb.e_type = e_type;
629 if (data)
630 cb.e_data = data->_ptr;
631 else
632 cb.e_data = NULL;
633 cb.sub_mgmt = &sub_mgmt;
634 cb._sync = 1;
635
636 /* call user function */
637 cur_sub->hdl.sync_ptr(&cb, cur_sub->hdl.private);
638
639 if (!sub_mgmt.this) {
640 /* user has performed hdl unsub
641 * we must remove it from the list
642 */
643 MT_LIST_DELETE_SAFE(elt1);
644 /* then free it */
645 _event_hdl_unsubscribe(cur_sub);
646 }
647 } else {
648 /* async mode: here we need to prepare event data
649 * and push it to the event_queue of the task(s)
650 * responsible for consuming the events of current
651 * subscription.
652 * Once the event is pushed, we wake up the associated task.
653 * This feature depends on <haproxy/task> that also
654 * depends on <haproxy/pool>:
655 * If STG_PREPARE+STG_POOL is not performed prior to publishing to
656 * async handler, program may crash.
657 * Hopefully, STG_PREPARE+STG_POOL should be done early in
658 * HAProxy startup sequence.
659 */
660 struct event_hdl_async_event *new_event;
661
662 new_event = pool_alloc(pool_head_sub_event);
663 if (!new_event) {
664 error = 1;
665 break; /* stop on error */
666 }
667 new_event->type = e_type;
668 new_event->private = cur_sub->hdl.private;
669 new_event->sub_mgmt = EVENT_HDL_SUB_MGMT_ASYNC(cur_sub);
670 if (data) {
671 /* if this fails, please adjust EVENT_HDL_ASYNC_EVENT_DATA in
672 * event_hdl-t.h file
673 */
674 BUG_ON(data->_size > sizeof(async_data->data));
675 if (!async_data) {
676 /* first async hdl reached - preparing async_data cache */
677 async_data = pool_alloc(pool_head_sub_event_data);
678 if (!async_data) {
679 error = 1;
680 pool_free(pool_head_sub_event, new_event);
681 break; /* stop on error */
682 }
683
684 /* async data assignment */
685 memcpy(async_data->data, data->_ptr, data->_size);
686 async_data->refcount = 0; /* initialize async->refcount (first use, atomic operation not required) */
687 }
688 new_event->_data = async_data;
689 new_event->data = async_data->data;
690 /* increment refcount because multiple hdls could
691 * use the same async_data
692 */
693 HA_ATOMIC_INC(&async_data->refcount);
694 } else
695 new_event->data = NULL;
696
697 /* appending new event to event hdl queue */
698 MT_LIST_INIT(&new_event->mt_list);
699 MT_LIST_APPEND(cur_sub->hdl.async_equeue, &new_event->mt_list);
700
701 /* wake up the task */
702 tasklet_wakeup(cur_sub->hdl.async_task);
703 } /* end async mode */
704 } /* end hdl should be notified */
705 } /* end mt_list */
706 if (error) {
707 event_hdl_report_hdl_state(ha_warning, &cur_sub->hdl, "PUBLISH", "memory error");
708 return 0;
709 }
710 return 1;
711}
712
713/* Publish function should not be used from high calling rate or time sensitive
714 * places for now, because list lookup based on e_type is not optimized at
715 * all!
716 * Returns 1 in case of SUCCESS:
717 * Subscribed handlers were notified successfully
718 * Returns 0 in case of FAILURE:
719 * FAILURE means memory error while handling the very first async handler from
720 * the subscription list.
721 * As async handlers are executed first within the list, when such failure occurs
722 * you can safely assume that no events were published for the current call
723 */
724int event_hdl_publish(event_hdl_sub_list *sub_list,
725 struct event_hdl_sub_type e_type, const struct event_hdl_cb_data *data)
726{
727 if (!e_type.family) {
728 /* do nothing, these types are reserved for internal use only
729 * (ie: unregistering) */
730 return 0;
731 }
732 if (sub_list) {
733 /* if sublist is provided, first publish event to list subscribers */
734 return _event_hdl_publish(sub_list, e_type, data);
735 } else {
736 /* publish to global list */
737 return _event_hdl_publish(&global_event_hdl_sub_list, e_type, data);
738 }
739}
740
741/* when a subscription list is no longer used, call this
742 * to do the cleanup and make sure all related subscriptions are
743 * safely ended according to their types
744 */
745void event_hdl_sub_list_destroy(event_hdl_sub_list *sub_list)
746{
747 struct event_hdl_sub *cur_sub;
748 struct mt_list *elt1, elt2;
749
750 if (!sub_list)
751 sub_list = &global_event_hdl_sub_list; /* fall back to global list */
752 mt_list_for_each_entry_safe(cur_sub, sub_list, mt_list, elt1, elt2) {
753 /* remove cur elem from list */
754 MT_LIST_DELETE_SAFE(elt1);
755 /* then free it */
756 _event_hdl_unsubscribe(cur_sub);
757 }
758}