1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
|
-----------------------------------------
event_hdl Guide - version 2.8
( Last update: 2022-11-14 )
------------------------------------------
ABSTRACT
--------
The event_hdl support is a new feature of HAProxy 2.7. It is a way to easily
handle general events in a simple to maintain fashion, while keeping core code
impact to the bare minimum.
This document first describes how to use already supported events,
then how to add support for your very own events.
This feature is quite new for now. The API is not frozen and will be
updated/modified/improved/extended as needed.
SUMMARY
-------
1. event_hdl introduction
2. How to handle existing events
2.1 SYNC mode
2.2 ASYNC mode
2.2.1 normal version
2.2.2 task version
2.3 Advanced features
2.3.1 sub_mgmt
2.3.2 subscription external lookups
2.3.3 subscription ptr
2.3.4 private_free
3. How to add support for new events
3.1 Declaring a new event data structure
3.2 Publishing an event
4. Subscription lists
5. misc/helper functions
1. EVENT_HDL INTRODUCTION
-----------------------
EVENT_HDL provides two complementary APIs, both are implemented
in src/event_hdl.c and include/haproxy/event_hdl(-t).h:
One API targeting developers that want to register event
handlers that will be notified when specific events occur in the process.
(See section 2.)
One API targeting developers that want to notify registered handlers about
an event that is happening in the process.
(See section 3.)
2. HOW TO HANDLE EXISTING EVENTS
---------------------
To handle existing events, you must first decide which events you're
interested in.
event types are defined as follow:
```
/* type for storing event subscription type */
typedef struct event_hdl_sub_type
{
/* up to 256 families, non cumulative, adjust if needed */
uint8_t family;
/* up to 16 sub types using bitmasks, adjust if needed */
uint16_t subtype;
} event_hdl_sub_type;
```
For an up to date list of already supported events,
please refer to include/haproxy/event_hdl-t.h
At the end of the file you will find existing event types.
Each event family provides an unique data structure that will
be provided to the event handler (registered to one or more
event subtypes) when such events occur.
An event handler can subscribe to a single event family type at a time, but
within the family type it can subscribe to multiple event subtypes.
For example, let's consider the SERVER family type.
Let's assume it provides the event_hdl_cb_data_server data structure.
We can register a handler that will be notified for
every SERVER event types using:
EVENT_HDL_SUB_SERVER
This will include EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_SUB_SERVER_DEL [...]
But we can also subscribe to a specific subtype only,
for example server deletion:
EVENT_HDL_SUB_SERVER_DEL
You can even combine multiple SERVER subtypes using
event_hdl_sub_type_add function helper:
event_hdl_sub_type_add(EVENT_HDL_SUB_SERVER_DEL,
EVENT_HDL_SUB_SERVER_ADD)
(will refer to server deletion as well as server addition)
Registering a handler comes into multiple flavors:
SYNC mode:
handler is called in a blocking manner directly from the
thread that publishes the event.
This mode should be used with precaution because it could
slow the caller or cause deadlocks if used improperly.
Sync mode is useful when you directly depend on data or
state consistency from the caller.
Sync mode gives you access to unsafe elements in the data structure
provided by the caller (again, see event_hdl-t.h for more details).
The data structure may provide lock hints in the unsafe section
so that you know which locks are already held within the
calling context, hopefully preventing you from relocking
an already locked element and preventing deadlocks.
ASYNC mode:
handler is called in a non-blocking manner
(in a dedicated tasklet),
thus, the caller (that published the event) is not affected
by the handler. (time wise and data wise)
This is the safest way to handle events,
but it also comes with a limitation:
unsafe elements in the data structure provided by
the caller SHOULD be used under NO circumstances.
Indeed, only safe elements are meant to be used
when handling the event in async mode.
ASYNC mode is declined in 2 different versions:
normal:
handler is simply a function pointer
(same prototype as sync mode),
that is called asynchronously with relevant data
when the event is published. Only difference with
sync mode here is that 'unsafe' data provided
by the data structure may not be used.
task:
handler is a user defined task(let) that uses an event
queue to consume pending events.
This mode is interesting when you need to perform
advanced operations or you need to handle the event
in an already existing task context.
It is a bit more complicated to setup, but really
nothing to worry about, some examples will be
provided later in this document.
event subscription is performed using the function:
event_hdl_subscribe(list, event, hdl);
The function returns 1 in case of success,
and 0 in case of failure (bad arguments, or memory error)
The function may BUG_ON if used improperly (invalid arguments)
<list> is either user specified list used to store the
new subscription, or NULL if you want to store the subscription
in the process global list.
<list> is also asked when publishing an event,
so specifying list could be useful, if, for example,
you only want to subscribe to a specific subscription list
(see this as a scope for example, NULL being full scope,
and specific list being limited scope)
We will use server events as an example:
You could register to events for ALL servers by using the
global list (NULL), or only to a specific server events
by using the subscription list dedicated to a single server.
<event> are the events (family.subtypes) you're subscribing to
<hdl> contains required handler options, it must be provided using
EVENT_HDL_(TASK_)(A)SYNC() and EVENT_HDL_ID_(TASK_)(A)SYNC()
helper macros.
See include/haproxy/event_hdl.h or below to know which macro
best suits your needs.
When registering a handler, you have the ability to provide an
unique ID (using EVENT_HDL_ID_ macro family) that could be used
later to perform lookups on the subscription.
ID is stored as an uint64_t hash that is expected to be computed using
general purpose event_hdl_id inline function provided by event_hdl.h.
Not providing an ID (using EVENT_HDL_ macro family)
results in the subscription being considered as anonymous.
As the name implies, anonymous subscriptions don't support lookups.
2.1 SYNC MODE
---------------------
Example, you want to register a sync handler that will be called when
a new server is added.
Here is what the handler function will look like:
```
void my_sync_handler(const struct event_hdl_cb *cb, void *private)
{
const struct event_hdl_cb_data_server *server = cb->e_data;
/* using EVENT_HDL_ASSERT_SYNC is a good practice to ensure
* that the function breaks if used in async mode
* (because we will access unsafe data in this function that
* is sync mode only)
*/
EVENT_HDL_ASSERT_SYNC(cb);
printf("I've been called for '%s', private = %p\n",
event_hdl_sub_type_to_string(cb->e_type), private);
printf("server name is '%s'\n", server->safe.name);
/* here it is safe to use unsafe data */
printf("server ptr is '%p'\n", server->unsafe.ptr);
/* from here you have the possibility to manage the subscription
* cb->sub_mgmt->unsub(cb->sub_mgmt);
* // hdl will be removed from the subscription list
*/
}
```
Here is how you perform the subscription:
anonymous subscription:
```
int private = 10;
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_SYNC(my_sync_handler, &private, NULL));
```
identified subscription:
```
int private = 10;
uint64_t id = event_hdl_id("test", "sync");
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_SYNC(id,
my_sync_handler,
&private,
NULL));
```
identified subscription where freeing private is required when subscription ends:
(also works for anonymous)
(more on this feature in 2.3.4)
```
int *private = malloc(sizeof(*private));
uint64_t id = event_hdl_id("test", "sync_free");
BUG_ON(!private);
*private = 10;
/* passing free as 'private_free' function so that
* private can be freed when unregistering is performed
*/
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_SYNC(id,
my_sync_handler,
private,
free));
/* ... */
// unregistering the identified hdl
if (event_hdl_lookup_unsubscribe(NULL, id)) {
printf("private will automatically be freed!\n");
}
```
2.2 ASYNC MODE
---------------------
As mentioned before, async mode comes in 2 flavors, normal and task.
2.2.1 NORMAL VERSION
---------------------
Normal is meant to be really easy to use, and highly compatible with sync mode.
(Handler can easily be converted or copy pasted from async to sync mode
and vice versa)
Quick warning about sync to async handler conversion:
please always use EVENT_HDL_ASSERT_SYNC whenever you develop a
sync handler that performs unsafe data access.
This way, if the handler were to be converted or copy pasted as is to
async mode without removing unsafe data accesses,
the handler will forcefully fail to indicate an error so that you
know something has to be fixed in your handler code.
Back to our async handler, let's say you want to declare an
async handler that will be called when a new server is added.
Here is what the handler function will look like:
```
void my_async_handler(const struct event_hdl_cb *cb, void *private)
{
const struct event_hdl_cb_data_server *server = cb->e_data;
printf("I've been called for '%s', private = %p\n",
event_hdl_sub_type_to_string(cb->e_type), private);
printf("server name is '%s'\n", server->safe.name);
/* here it is not safe to use unsafe data */
/* from here you have the possibility to manage the subscription
* cb->sub_mgmt->unsub(cb->sub_mgmt);
* // hdl will be removed from the subscription list
*/
}
```
Note that it is pretty similar to sync handler, except
for unsafe data access.
Here is how you declare the subscription:
anonymous subscription:
```
int private = 10;
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ASYNC(my_async_handler, &private, NULL));
```
identified subscription:
```
int private = 10;
uint64_t id = event_hdl_id("test", "async");
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_ASYNC(id,
my_async_handler,
&private,
NULL));
```
identified subscription where freeing private is required when subscription ends:
(also works for anonymous)
```
int *private = malloc(sizeof(*private));
uint64_t id = event_hdl_id("test", "async_free");
BUG_ON(!private);
*private = 10;
/* passing free as 'private_free' function so that
* private can be freed when unregistering is performed
*/
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_ASYNC(id,
my_async_handler,
private,
free));
/* ... */
// unregistering the identified hdl
if (event_hdl_lookup_unsubscribe(NULL, id)) {
printf("private will automatically be freed when "
"all pending events referencing private "
"are consumed!\n");
}
```
2.2.2 TASK VERSION
---------------------
task version requires a bit more setup, but it's pretty
straightforward actually.
First, you need to initialize an event queue that will be used
by event_hdl facility to push you events according to your subscription:
```
event_hdl_async_equeue my_q;
event_hdl_async_equeue_init(&my_q);
```
Then, you need to declare a task(let) (or reuse existing task(let))
It is your responsibility to make sure that the task(let) still exists
(is not freed) when calling the subscribe function
(and that the task remains valid as long as the subscription is).
When a subscription referencing your task is over
(either ended because of list purge, external code or from the handler itself),
you will receive the EVENT_HDL_SUB_END event.
When you receive this event, you must free it as usual and you can safely
assume that the related subscription won't be sending you any more events.
Here is what your task will look like (involving a single event queue):
```
struct task *event_hdl_async_task_my(struct task *task,
void *ctx, unsigned int state)
{
struct tasklet *tl = (struct tasklet *)task;
event_hdl_async_equeue *queue = ctx;
struct event_hdl_async_event *event;
struct event_hdl_cb_data_server *srv;
uint8_t done = 0;
while ((event = event_hdl_async_equeue_pop(queue)))
{
if (event_hdl_sub_type_equal(event->type, EVENT_HDL_SUB_END)) {
done = 1;
event_hdl_async_free_event(event);
printf("no more events to come, "
"subscription is over\n");
break;
}
srv = event->data;
printf("task event %s, %d (name = %s)\n",
event_hdl_sub_type_to_string(event->type),
*((int *)event->private), srv->safe.name);
event_hdl_async_free_event(event);
}
if (done) {
/* our job is done, subscription is over:
* no more events to come
*/
tasklet_free(tl);
return NULL;
}
return task;
}
```
Here is how we would initialize the task event_hdl_async_task_my:
```
struct tasklet *my_task;
my_task = tasklet_new();
BUG_ON(!my_task);
my_task->context = &my_q; // we declared my_q previously in this example
/* we declared event_hdl_async_task_my previously
* in this example
*/
my_task->process = event_hdl_async_task_my;
```
Given our task and our previously initialized event queue, here is how
to perform the subscription:
```
int test_val = 11;
uint64_t id = event_hdl_id("test", "my_task");
/* anonymous variant */
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ASYNC_TASK(&my_q,
my_task,
&test_val,
NULL));
/* identified variant */
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_ASYNC_TASK(id,
&my_q,
my_task,
&test_val,
NULL));
```
Note: it is not recommended to perform multiple subscriptions
that share the same event queue or same task(let) (or both)
That is, having more than one subscription waking a task(let)
and/or feeding the same event queue.
No check is performed on this when registering, so the API
won't prevent you from doing it.
If you are going to do this anyway despite this warning:
In the case you need to stop the task prematurely
(if this is not going to happen please skip this paragraph):
You are responsible for acknowledging the end of every
active subscriptions that refer to your task or
your event queue(s).
And you really don't want a subscription associated with
your task or event queue to keep going when the task
is not active anymore because:
1: there will be memory leak
(event queue might continue to receive new events)
2: there is a 100% chance of process crash in case of event
because we will try to wake a task (your task)
that might already be freed. Thus UAF will occur.
2.3 ADVANCED FEATURES
-----------------------
We've already covered some of these features in the previous examples.
Here is a documented recap.
2.3.1 SUB MGMT
-----------------------
From an event handler context, either sync or async mode:
You have the ability to directly manage the subscription
that provided the event.
As of today, these actions are supported:
- Consulting the subscription.
- Modifying the subscription (resubscribing within same family)
- Unregistering the subscription (unsubscribing).
To do this, consider the following structure:
```
struct event_hdl_sub_mgmt
{
/* manage subscriptions from event
* this must not be used directly because
* locking might be required
*/
struct event_hdl_sub *this;
/* safe functions than can be used from
* event context (sync and async mode)
*/
struct event_hdl_sub_type (*getsub)(const struct event_hdl_sub_mgmt *);
int (*resub)(const struct event_hdl_sub_mgmt *, struct event_hdl_sub_type);
void (*unsub)(const struct event_hdl_sub_mgmt *);
};
```
A reference to this structure is provided in every handler mode.
Sync mode and normal async mode (directly from the callback data pointer):
```
const struct event_hdl_cb *cb;
// cb->sub_mgmt
// cb->sub_mgmt->getsub(cb->sub_mgmt);
// cb->sub_mgmt->unsub(cb->sub_mgmt);
```
task and notify async modes (from the event):
```
struct event_hdl_async_event *event;
// event->sub_mgmt
// event->sub_mgmt.getsub(&event->sub_mgmt);
// event->sub_mgmt.unsub(&event->sub_mgmt);
```
2.3.2 SUBSCRIPTION EXTERNAL LOOKUPS
-----------------------
As you've seen in 2.3.1, managing the subscription directly
from the handler is a possibility.
But for identified subscriptions, you also have the ability to
perform lookups and management operations on specific subscriptions
within a list based on their ID, anywhere in the code.
/!\ This feature is not available for anonymous subscriptions /!\
Here are the actions already supported:
- unregistering a subscription (unsubscribing)
- updating a subscription (resubscribing within same family)
- getting a ptr/reference to the subscription
Those functions are documented in event_hdl.h
(search for EVENT_HDL_LOOKUP section).
To select a specific subscription, you must provide
the unique identifier (uint64_t hash) that was provided when subscribing.
(using event_hdl_id(scope, name) function)
Notes:
"id" is only unique within a given subscription list.
When using event_hdl_id to provide the id:
It is your responsibility to make sure that you "own"
the scope if you rely on name to be "free".
As ID computation is backed by xxhash hash API,
you should be aware that hash collisions could occur,
but are extremely rare and are thus considered safe
enough for this usage.
(see event_hdl.h for implementation details)
Please consider ptr based subscription management if
these limitations don't fit your requirements.
Here are some examples:
unsubscribing:
```
/* registering "scope":"name" subscription */
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_SYNC(event_hdl_id("scope", "name"),
my_sync_handler,
NULL,
NULL));
/* unregistering "scope":"name" subscription */
event_hdl_lookup_unsubscribe(NULL, event_hdl_id("scope", "name"));
```
2.3.3 SUBSCRIPTION PTR
-----------------------
To manage existing subscriptions from external code,
we already talked about identified subscriptions that
allow lookups within list.
But there is another way to accomplish this.
When subscribing, you can use the event_hdl_subscribe_ptr() function
variant (same arguments as event_hdl_subscribe()).
What this function does, is instead of returning 1 in case of
success and 0 in case of failure: it returns a valid subscription ptr
for success and NULL for failure.
Returned ptr is guaranteed to remain valid even if subscription
is ended meanwhile because the ptr is internally guarded with a refcount.
Thus, as long as you don't explicitly unregister the subscription with
event_hdl_unsubscribe() or drop the reference using event_hdl_drop(),
subscription ptr won't be freed.
This ptr will allow you to use the following subscription
management functions from external code:
- event_hdl_take() to increment subscription ptr refcount
(automatically incremented when using event_hdl_subscribe_ptr)
- event_hdl_drop() to decrement subscription ptr refcount
- event_hdl_resubscribe() to modify subscription subtype
- event_hdl_unsubscribe() to end the subscription
(refcount will be automatically decremented)
Here is an example:
```
struct event_hdl_sub *sub_ptr;
/* registering a subscription with subscribe_ptr */
sub_ptr = event_hdl_subscribe_ptr(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_SYNC(my_sync_handler,
NULL,
NULL));
/* ... */
/* unregistering the subscription */
event_hdl_unsubscribe(sub_ptr);
```
Regarding identified subscriptions that were registered using the non ptr
subscribe function:
You still have the ability to get a reference to the related subscription
(if it still exists), by using event_hdl_lookup_take(list, id) function.
event_hdl_lookup_take will return a subscription ptr in case of success
and NULL in case of failure.
Returned ptr reference is automatically incremented, so it is safe to use.
Please don't forget to drop the reference
when holding the ptr is no longer needed.
Example:
```
struct event_hdl_sub *sub_ptr = NULL;
/* registering subscription id "test":"ptr" with normal subscribe */
if (event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_ADD,
EVENT_HDL_ID_SYNC(event_hdl_id("test", "ptr"),
my_sync_handler,
NULL,
NULL))) {
/* fetch ref to subscription "test":"ptr" */
sub_ptr = event_hdl_lookup_take(NULL,
event_hdl_id("test", "ptr"));
/* unregister the subscription using lookup */
event_hdl_lookup_unsubscribe(NULL,
event_hdl_id("test", "ptr"));
}
/* ... */
/* unregistering the subscription with ptr
* will do nothing because subscription was
* already ended by lookup_unsubscribe, but
* here the catch is that sub_ptr is still
* valid so this won't crash the program
*/
if (sub_ptr) {
event_hdl_unsubscribe(sub_ptr);
/* unsubscribe will also result in subscription
* reference drop, thus subscription will be freed here
* because sub_ptr was the last active reference.
* You must not use sub_ptr anymore past this point
* or UAF could occur
*/
}
```
2.3.4 PRIVATE FREE
-----------------------
Upon handler subscription, you have the ability to provide
a private data pointer that will be passed to the handler
when subscribed events occur.
Sometimes this private data pointer will rely on dynamically allocated memory.
And in such cases, you have no way of knowing when
freeing this pointer can be done safely.
You could be tempted to think that freeing right after performing
the unsubscription could be safe.
But this is not the case, remember we could be dealing with async handlers
that might still consume pending events even though unsubscription
has been performed from external code.
To deal with this, you may want to provide the private_free
function pointer upon subscription.
This way, private_free function will automatically be called
(with private as argument) when private is no longer be used.
Example:
First we declare our private free function:
```
void my_private_free(void *my_private_data) {
/* here we only call free,
* but you could do more sophisticated stuff
*/
free(my_private_data);
}
```
Then:
```
char *my_private_data = strdup("this string needs to be freed");
BUG_ON(!my_private_data);
event_hdl_subscribe(NULL, EVENT_HDL_SUB_SERVER_DEL,
EVENT_HDL_ID_ASYNC(event_hdl_id("test", "private"),
my_async_handler,
my_private_data,
my_private_free));
/* freeing my_private_data is not required anymore,
* it will be automatically freed by our private free
* function when subscription ends
*/
/* unregistering "test":"private" subscription */
event_hdl_lookup_unsubscribe(NULL, event_hdl_id("test", "private"));
/* my_private_free will be automatically summoned when my_private_data
* is not referenced anymore
*/
```
3 HOW TO ADD SUPPORT FOR NEW EVENTS
-----------------------
Adding support for a new event is pretty straightforward.
First, you need to declare a new event subtype in event_hdl-t.h file
(bottom of the file).
You might want to declare a whole new event family, in which case
you declare both the new family and the associated subtypes (if any).
```
#define EVENT_HDL_SUB_NEW_FAMILY EVENT_HDL_SUB_FAMILY(4)
#define EVENT_HDL_SUB_NEW_FAMILY_SUBTYPE_1 EVENT_HDL_SUB_TYPE(4,0)
```
Then, you need to update the event_hdl_sub_type_map map,
defined in src/event_hdl.c file (top of the file)
to add string to event type and event type to string conversion support.
You just need to add the missing entries corresponding to
the event family / subtypes you've defined.
Please follow this procedure:
You only added a new subtype to existing family: go to section 3.2
You added a new family: go to section 3.1
3.1 DECLARING A NEW EVENT DATA STRUCTURE
-----------------------
You have the ability to provide additional data for a given
event family when such events occur.
Note that it is not mandatory: you could simply declare a new event family
that does not provide any data.
If this is your case, you can skip this section and go to 3.2 section.
Now, take a look at this event data structure template
(also defined at the top of event_hdl-t.h file):
```
/* event data struct are defined as followed */
struct event_hdl_cb_data_template {
struct {
/* safe data can be safely used from both
* sync and async functions
* data consistency is guaranteed
*/
} safe;
struct {
/* unsafe data may only be used from sync functions:
* in async mode, data consistency cannot be guaranteed
* and unsafe data may already be stale, thus using
* it is highly discouraged because it
* could lead to undefined behavior
* (UAF, null dereference...)
*/
} unsafe;
};
```
This structure template allows you to easily create a new event
data structure that can be provided with your new event family.
You should name it after 'struct event_hdl_cb_data_new_family' so that it is
easy to guess the event family it relates to.
Indeed, each event data structure is to be associated with an
unique event family type.
For each subtypes within a family type, the associated data structure
should be provided when publishing the event.
The event data struct declaration should not be performed
directly under event_hdl-t.h file:
It should be done in the header files of the corresponding
facility that will publish/provide this event.
Example: struct event_hdl_cb_data_server, provided for the
EVENT_HDL_SUB_SERVER event family, is going to be declared in
include/haproxy/server-t.h file.
However, in event_hdl-t.h, where you declare event family/subtypes,
you should add comments or links to the file containing the relevant
data struct declaration. This way we make sure all events related
information is centralized in event_hdl-t.h while keeping it clean
and not depending on any additional includes (you are free to
depend on specific data types within your custom event data structure).
Please make sure that EVENT_HDL_ASYNC_EVENT_DATA (defined in event_hdl-t.h)
is greater than sizeof(event_hdl_cb_data_new_family).
It is required for async handlers to properly consume event data.
You are free to adjust EVENT_HDL_ASYNC_EVENT_DATA size if needed.
If EVENT_HDL_ASYNC_EVENT_DATA is not big enough to store your new
event family struct, a compilation assert triggered by EVENT_HDL_CB_DATA
will occur. In addition to this, an extra runtime BUG_ON will make
sure the condition is met when publishing the event.
The goal here is to force haproxy to fail explicitly so you know that
something must be done on your side.
3.1 PUBLISHING AN EVENT
-----------------------
Publishing an event is really simple.
It relies on the event_hdl_publish function.
The function is defined as follow:
```
int event_hdl_publish(event_hdl_sub_list *sub_list,
event_hdl_sub_type e_type,
const struct event_hdl_cb_data *data);
```
We will ignore sub_list argument for now.
In the examples below, we will use sub_list = NULL.
Go to section 4 for a full picture about this feature.
<e_type>: the event type that should be published.
All subscriptions referring to this event within
a subscription list context will be notified about the event.
<data>: data provided for the event family of <e_type>
If <e_type>.family does not provide additional data,
data should be set to NULL.
If <e_type>.family does provide additional data, data should be set
using EVENT_HDL_CB_DATA macro.
(see the example below)
The function returns 1 in case of SUCCESS (handlers successfully notified)
and 0 in case of FAILURE (no handlers notified, because of memory error).
Event publishing can be performed from anywhere in the code.
(this example does not compile)
```
struct event_hdl_cb_data_new_family event_data;
/* first we need to prepare event data
* that will be provided to event handlers
*/
/* safe data, available from both sync and async contexts */
event_data.safe.my_custom_data = x;
/* unsafe data, only available from sync contexts */
event_data.unsafe.my_unsafe_data = y;
/* once data is prepared, we can publish the event */
event_hdl_publish(NULL,
EVENT_HDL_SUB_NEW_FAMILY_SUBTYPE_1,
EVENT_HDL_CB_DATA(&event_data));
/* EVENT_HDL_SUB_NEW_FAMILY_SUBTYPE_1 event was
* successfully published in global subscription list
*/
```
--------------------------------------------------------------------------------
|You should know that there is currently a limitation about publish function: |
|The function should not be used from critical places |
|(where the calling frequency is high |
|or where timing sensitivity is high). |
| |
|Because in current implementation, subscription list lookups are not |
|optimized for such uses cases. |
--------------------------------------------------------------------------------
4 SUBSCRIPTION LISTS
-----------------------
As you may already know, EVENT_HDL API main functions rely on
subscription lists.
Providing NULL where subscription list argument is required
allows to use the implicit global subscription list.
But you can also provide a specific subscription list, example:
subscription list associated with a single entity so that you only
subscribe to events of this single entity
A subscription list is of type event_hdl_sub_list.
It is defined in event_hdl-t.h
To make use of this feature, you should know about these 2 functions:
event_hdl_sub_list_init(list): use this fcn to initialize
a new subscription list.
Example:
```
event_hdl_sub_list my_custom_list;
event_hdl_sub_list_init(&my_custom_list);
```
event_hdl_sub_list_destroy(list): use this fcn to destroy
an existing subscription list.
Example:
```
event_hdl_sub_list_init(&my_custom_list);
```
Using this function will cause all the existing subscriptions
within the provided sub_list to be properly unregistered
and deleted according to their types.
Now we'll take another quick look at event_hdl_publish() function:
Remember that the function is defined as follow:
```
int event_hdl_publish(event_hdl_sub_list *sub_list,
event_hdl_sub_type e_type,
const struct event_hdl_cb_data *data);
```
In the previous examples, we used sub_list = NULL.
if sub_list is NULL:
event will be published in in global list
else
event will be published in user specified sub_list
5 MISC/HELPER FUNCTIONS
-----------------------
Don't forget to take a look at MISC/HELPER FUNCTIONS in
include/haproxy/event_hdl.h (end of the file) for a
complete list of helper functions / macros.
We've already used some, if not the vast majority
in the examples shown in this document.
This includes, to name a few:
- event types manipulation
- event types comparison
- lookup id computing
- subscriber list management (covered in section 4)
- sync/async handler helpers
|