blob: 3a7b14b8eda05ffd5bd48328e44d73c396ff41f4 [file] [log] [blame]
Willy Tarreau64a18532019-11-20 16:45:15 +01001Initialization stages aka how to get your code initialized at the right moment
2
3
41. Background
5
6Originally all subsystems were initialized via a dedicated function call
7from the huge main() function. Then some code started to become conditional
8or a bit more modular and the #ifdef placed there became a mess, resulting
9in init code being moved to function constructors in each subsystem's own
10file. Then pools of various things were introduced, starting to make the
11whole init sequence more complicated due to some forms of internal
12dependencies. Later epoll was introduced, requiring a post-fork callback,
13and finally threads arrived also requiring some post-thread init/deinit
14and allocation, marking the old architecture's last breath. Finally the
15whole thing resulted in lots of init code duplication and was simplified
16in 1.9 with the introduction of initcalls and initialization stages.
17
18
192. New architecture
20
21The new architecture relies on two layers :
22 - the registration functions
23 - the INITCALL macros and initialization stages
24
25The first ones are mostly used to add a callback to a list. The second ones
26are used to specify when to call a function. Both are totally independent,
27however they are generally combined via another set consisting in the REGISTER
28macros which make some registration functions be called at some specific points
29during the init sequence.
30
31
323. Registration functions
33
34Registration functions never fail. Or more precisely, if they fail it will only
35be on out-of-memory condition, and they will cause the process to immediately
36exit. As such they do not return any status and the caller doesn't have to care
37about their success.
38
39All available functions are described below in alphanumeric ordering. Please
40make sure to respect this ordering when adding new ones.
41
42- void hap_register_build_opts(const char *str, int must_free)
43
44 This appends the zero-terminated constant string <str> to the list of known
45 build options that will be reported on the output of "haproxy -vv". A line
46 feed character ('\n') will automatically be appended after the string when it
47 is displayed. The <must_free> argument must be zero, unless the string was
48 allocated by any malloc-compatible function such as malloc()/calloc()/
49 realloc()/strdup() or memprintf(), in which case it's better to pass a
50 non-null value so that the string is freed upon exit. Note that despite the
51 function's prototype taking a "const char *", the pointer will actually be
52 cast and freed. The const char* is here to leave more freedom to use consts
53 when making such options lists.
54
55- void hap_register_per_thread_alloc(int (*fct)())
56
57 This adds a call to function <fct> to the list of functions to be called when
58 threads are started, at the beginning of the polling loop. This is also valid
59 for the main thread and will be called even if threads are disabled, so that
60 it is guaranteed that this function will be called in any circumstance. Each
61 thread will first call all these functions exactly once when it starts. Calls
62 are serialized by the init_mutex, so that locking is not necessary in these
63 functions. There is no relation between the thread numbers and the callback
64 ordering. The function is expected to return non-zero on success, or zero on
Ilya Shipitsin1fae8db2020-03-14 17:47:28 +050065 failure. A failure will make the process emit a succinct error message and
Willy Tarreau64a18532019-11-20 16:45:15 +010066 immediately exit. See also hap_register_per_thread_free() for functions
67 called after these ones.
68
69- void hap_register_per_thread_deinit(void (*fct)());
70
71 This adds a call to function <fct> to the list of functions to be called when
72 threads are gracefully stopped, at the end of the polling loop. This is also
73 valid for the main thread and will be called even if threads are disabled, so
74 that it is guaranteed that this function will be called in any circumstance
75 if the process experiences a soft stop. Each thread will call this function
76 exactly once when it stops. However contrary to _alloc() and _init(), the
77 calls are made without any protection, thus if any shared resource if touched
78 by the function, the function is responsible for protecting it. The reason
79 behind this is that such resources are very likely to be still in use in one
80 other thread and that most of the time the functions will in fact only touch
81 a refcount or deinitialize their private resources. See also
82 hap_register_per_thread_free() for functions called after these ones.
83
84- void hap_register_per_thread_free(void (*fct)());
85
86 This adds a call to function <fct> to the list of functions to be called when
87 threads are gracefully stopped, at the end of the polling loop, after all calls
88 to _deinit() callbacks are done for this thread. This is also valid for the
89 main thread and will be called even if threads are disabled, so that it is
90 guaranteed that this function will be called in any circumstance if the
91 process experiences a soft stop. Each thread will call this function exactly
92 once when it stops. However contrary to _alloc() and _init(), the calls are
93 made without any protection, thus if any shared resource if touched by the
94 function, the function is responsible for protecting it. The reason behind
95 this is that such resources are very likely to be still in use in one other
96 thread and that most of the time the functions will in fact only touch a
97 refcount or deinitialize their private resources. See also
98 hap_register_per_thread_deinit() for functions called before these ones.
99
100- void hap_register_per_thread_init(int (*fct)())
101
102 This adds a call to function <fct> to the list of functions to be called when
103 threads are started, at the beginning of the polling loop, right after the
104 list of _alloc() functions. This is also valid for the main thread and will
105 be called even if threads are disabled, so that it is guaranteed that this
106 function will be called in any circumstance. Each thread will call this
107 function exactly once when it starts, and calls are serialized by the
108 init_mutex which is held over all _alloc() and _init() calls, so that locking
109 is not necessary in these functions. In other words for all threads but the
110 current one, the sequence of _alloc() and _init() calls will be atomic. There
111 is no relation between the thread numbers and the callback ordering. The
112 function is expected to return non-zero on success, or zero on failure. A
Ilya Shipitsin1fae8db2020-03-14 17:47:28 +0500113 failure will make the process emit a succinct error message and immediately
Willy Tarreau64a18532019-11-20 16:45:15 +0100114 exit. See also hap_register_per_thread_alloc() for functions called before
115 these ones.
116
117- void hap_register_post_check(int (*fct)())
118
119 This adds a call to function <fct> to the list of functions to be called at
120 the end of the configuration validity checks, just at the point where the
121 program either forks or exits depending whether it's called with "-c" or not.
122 Such calls are suited for memory allocation or internal table pre-computation
123 that would preferably not be done on the fly to avoid inducing extra time to
124 a pure configuration check. Threads are not yet started so no protection is
125 required. The function is expected to return non-zero on success, or zero on
Ilya Shipitsin1fae8db2020-03-14 17:47:28 +0500126 failure. A failure will make the process emit a succinct error message and
Willy Tarreau64a18532019-11-20 16:45:15 +0100127 immediately exit.
128
129- void hap_register_post_deinit(void (*fct)())
130
131 This adds a call to function <fct> to the list of functions to be called when
132 freeing the global sections at the end of deinit(), after everything is
133 stopped. The process is single-threaded at this point, thus these functions
134 are suitable for releasing configuration elements provided that no other
135 _deinit() function uses them, i.e. only close/release what is strictly
136 private to the subsystem. Since such functions are mostly only called during
137 soft stops (reloads) or failed startups, they tend to experience much less
138 test coverage than others despite being more exposed, and as such a lot of
139 care must be taken to test them especially when facing partial subsystem
140 initializations followed by errors.
141
142- void hap_register_post_proxy_check(int (*fct)(struct proxy *))
143
144 This adds a call to function <fct> to the list of functions to be called for
145 each proxy, after the calls to _post_server_check(). This can allow, for
146 example, to pre-configure default values for an option in a frontend based on
147 the "bind" lines or something in a backend based on the "server" lines. It's
148 worth being aware that such a function must be careful not to waste too much
149 time in order not to significantly slow down configurations with tens of
150 thousands of backends. The function is expected to return non-zero on
Ilya Shipitsin1fae8db2020-03-14 17:47:28 +0500151 success, or zero on failure. A failure will make the process emit a succinct
Willy Tarreau64a18532019-11-20 16:45:15 +0100152 error message and immediately exit.
153
154- void hap_register_post_server_check(int (*fct)(struct server *))
155
156 This adds a call to function <fct> to the list of functions to be called for
157 each server, after the call to check_config_validity(). This can allow, for
158 example, to preset a health state on a server or to allocate a protocol-
159 specific memory area. It's worth being aware that such a function must be
160 careful not to waste too much time in order not to significantly slow down
161 configurations with tens of thousands of servers. The function is expected
162 to return non-zero on success, or zero on failure. A failure will make the
Ilya Shipitsin1fae8db2020-03-14 17:47:28 +0500163 process emit a succinct error message and immediately exit.
Willy Tarreau64a18532019-11-20 16:45:15 +0100164
165- void hap_register_proxy_deinit(void (*fct)(struct proxy *))
166
167 This adds a call to function <fct> to the list of functions to be called when
168 freeing the resources during deinit(). These functions will be called as part
169 of the proxy's resource cleanup. Note that some of the proxy's fields will
170 already have been freed and others not, so such a function must not use any
171 information from the proxy that is subject to being released. In particular,
172 all servers have already been deleted. Since such functions are mostly only
173 called during soft stops (reloads) or failed startups, they tend to
174 experience much less test coverage than others despite being more exposed,
175 and as such a lot of care must be taken to test them especially when facing
176 partial subsystem initializations followed by errors. It's worth mentioning
177 that too slow functions could have a significant impact on the configuration
178 check or exit time especially on large configurations.
179
180- void hap_register_server_deinit(void (*fct)(struct server *))
181
182 This adds a call to function <fct> to the list of functions to be called when
183 freeing the resources during deinit(). These functions will be called as part
184 of the server's resource cleanup. Note that some of the server's fields will
185 already have been freed and others not, so such a function must not use any
186 information from the server that is subject to being released. Since such
187 functions are mostly only called during soft stops (reloads) or failed
188 startups, they tend to experience much less test coverage than others despite
189 being more exposed, and as such a lot of care must be taken to test them
190 especially when facing partial subsystem initializations followed by errors.
191 It's worth mentioning that too slow functions could have a significant impact
192 on the configuration check or exit time especially on large configurations.
193
194
1954. Initialization stages
196
197In order to offer some guarantees, the startup of the program is split into
198several stages. Some callbacks can be placed into each of these stages using
199an INITCALL macro, with 0 to 3 arguments, respectively called INITCALL0 to
200INITCALL3. These macros must be placed anywhere at the top level of a C file,
201preferably at the end so that the referenced symbols have already been met,
202but it may also be fine to place them right after the callbacks themselves.
203
204Such callbacks are referenced into small structures containing a pointer to the
205function and 3 arguments. NULL replaces unused arguments. The callbacks are
206cast to (void (*)(void *, void *, void *)) and the arguments to (void *).
207
208The first argument to the INITCALL macro is the initialization stage. The
209second one is the callback function, and others if any are the arguments.
210The init stage must be among the values of the "init_stage" enum, currently,
211and in this execution order:
212
213 - STG_PREPARE : used to preset variables, pre-initialize lookup tables and
214 pre-initialize list heads
215 - STG_LOCK : used to pre-initialize locks
216 - STG_ALLOC : used to allocate the required structures
217 - STG_POOL : used to create pools
218 - STG_REGISTER : used to register static lists such as keywords
219 - STG_INIT : used to initialize subsystems
220
221Each stage is guaranteed that previous stages have successfully completed. This
222means that an INITCALL placed at stage STG_REGISTER is guaranteed that all
223pools were already created and will be usable. Conversely, an INITCALL placed
224at stage STG_PREPARE must not rely on any field that requires preliminary
225allocation nor initialization. A callback cannot rely on other callbacks of the
226same stage, as the execution order within a stage is undefined and essentially
227depends on the linking order.
228
229Example: register a very early call to init_log() with no argument, and another
230 call to cli_register_kw(&cli_kws) much later:
231
232 INITCALL0(STG_PREPARE, init_log);
233 INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
234
235Technically speaking, each call to such a macro adds a distinct local symbol
236whose dynamic name involves the line number. These symbols are placed into a
237separate section and the beginning and end section pointers are provided by the
238linker. When too old a linker is used, a fallback is applied consisting in
239placing them into a linked list which is built by a constructor function for
240each initcall (this takes more room).
241
242Due to the symbols internally using the line number, it is very important not
243to place more than one INITCALL per line in the source file.
244
245It is also strongly recommended that functions and referenced arguments are
246static symbols local to the source file, unless they are global registration
247functions like in the example above with cli_register_kw(), where only the
248argument is a local keywords table.
249
250INITCALLs do not expect the callback function to return anything and as such
251do not perform any error check. As such, they are very similar to constructors
252offered by the compiler except that they are segmented in stages. It is thus
253the responsibility of the called functions to perform their own error checking
254and to exit in case of error. This may change in the future.
255
256
2575. REGISTER family of macros
258
259The association of INITCALLs and registration functions allows to perform some
260early dynamic registration of functions to be used anywhere, as well as values
261to be added to existing lists without having to manipulate list elements. For
262the sake of simplification, these combinations are available as a set of
263REGISTER macros which register calls to certain functions at the appropriate
264init stage. Such macros must be used at the top level in a file, just like
265INITCALL macros. The following macros are currently supported. Please keep them
266alphanumerically ordered:
267
268- REGISTER_BUILD_OPTS(str)
269
270 Adds the constant string <str> to the list of build options. This is done by
271 registering a call to hap_register_build_opts(str, 0) at stage STG_REGISTER.
272 The string will not be freed.
273
274- REGISTER_CONFIG_POSTPARSER(name, parser)
275
276 Adds a call to function <parser> at the end of the config parsing. The
277 function is called at the very end of check_config_validity() and may be used
278 to initialize a subsystem based on global settings for example. This is done
279 by registering a call to cfg_register_postparser(name, parser) at stage
280 STG_REGISTER.
281
282- REGISTER_CONFIG_SECTION(name, parse, post)
283
284 Registers a new config section name <name> which will be parsed by function
285 <parse> (if not null), and with an optional call to function <post> at the
286 end of the section. Function <parse> must be of type (int (*parse)(const char
287 *file, int linenum, char **args, int inv)), and returns 0 on success or an
288 error code among the ERR_* set on failure. The <post> callback takes no
289 argument and returns a similar error code. This is achieved by registering a
290 call to cfg_register_section() with the three arguments at stage
291 STG_REGISTER.
292
293- REGISTER_PER_THREAD_ALLOC(fct)
294
295 Registers a call to register_per_thread_alloc(fct) at stage STG_REGISTER.
296
297- REGISTER_PER_THREAD_DEINIT(fct)
298
299 Registers a call to register_per_thread_deinit(fct) at stage STG_REGISTER.
300
301- REGISTER_PER_THREAD_FREE(fct)
302
303 Registers a call to register_per_thread_free(fct) at stage STG_REGISTER.
304
305- REGISTER_PER_THREAD_INIT(fct)
306
307 Registers a call to register_per_thread_init(fct) at stage STG_REGISTER.
308
309- REGISTER_POOL(ptr, name, size)
310
311 Used internally to declare a new pool. This is made by calling function
312 create_pool_callback() with these arguments at stage STG_POOL. Do not use it
313 directly, use either DECLARE_POOL() or DECLARE_STATIC_POOL() instead (see
314 below).
315
316- REGISTER_POST_CHECK(fct)
317
318 Registers a call to register_post_check(fct) at stage STG_REGISTER.
319
320- REGISTER_POST_DEINIT(fct)
321
322 Registers a call to register_post_deinit(fct) at stage STG_REGISTER.
323
324- REGISTER_POST_PROXY_CHECK(fct)
325
326 Registers a call to register_post_proxy_check(fct) at stage STG_REGISTER.
327
328- REGISTER_POST_SERVER_CHECK(fct)
329
330 Registers a call to register_post_server_check(fct) at stage STG_REGISTER.
331
332- REGISTER_PROXY_DEINIT(fct)
333
334 Registers a call to register_proxy_deinit(fct) at stage STG_REGISTER.
335
336- REGISTER_SERVER_DEINIT(fct)
337
338 Registers a call to register_server_deinit(fct) at stage STG_REGISTER.
339
340
3416. Other initialization macros
342
343On top of the INITCALL family of macros, a few other convenient macros were
344created in order to simplify declarations or allocations:
345
346- DECLARE_POOL(ptr, name, size)
347
348 Placed at the top level of a file, this declares a global memory pool as
349 variable <ptr>, name <name> and size <size> bytes per element. This is made
350 via a call to REGISTER_POOL() and by assigning the resulting pointer to
351 variable <ptr>. <ptr> will be created of type "struct pool_head *". If the
352 pool needs to be visible outside of the function (which is likely), it will
353 also need to be declared somewhere as "extern struct pool_head *<ptr>;". It
354 is recommended to place such declarations very early in the source file so
355 that the variable is already known to all subsequent functions which may use
356 it.
357
358- DECLARE_STATIC_POOL(ptr, name, size)
359
360 Placed at the top level of a file, this declares a static memory pool as
361 variable <ptr>, name <name> and size <size> bytes per element. This is made
362 via a call to REGISTER_POOL() and by assigning the resulting pointer to local
363 variable <ptr>. <ptr> will be created of type "static struct pool_head *". It
364 is recommended to place such declarations very early in the source file so
365 that the variable is already known to all subsequent functions which may use
366 it.