/src/haproxy/src/protocol.c
Line | Count | Source |
1 | | /* |
2 | | * Protocol registration functions. |
3 | | * |
4 | | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
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 of the License, or (at your option) any later version. |
10 | | * |
11 | | */ |
12 | | |
13 | | #include <unistd.h> |
14 | | |
15 | | #include <sys/types.h> |
16 | | #include <sys/socket.h> |
17 | | |
18 | | #include <haproxy/api.h> |
19 | | #include <haproxy/errors.h> |
20 | | #include <haproxy/global.h> |
21 | | #include <haproxy/list.h> |
22 | | #include <haproxy/listener.h> |
23 | | #include <haproxy/proto_quic.h> |
24 | | #include <haproxy/protocol.h> |
25 | | #include <haproxy/proxy.h> |
26 | | #include <haproxy/quic_tune.h> |
27 | | #include <haproxy/sock.h> |
28 | | #include <haproxy/tools.h> |
29 | | |
30 | | |
31 | | /* List head of all registered protocols */ |
32 | | static struct list protocols = LIST_HEAD_INIT(protocols); |
33 | | struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2] __read_mostly = { }; |
34 | | const struct proto_fam *__proto_fam_by_family[AF_CUST_MAX] = { }; |
35 | | |
36 | | /* This is the global spinlock we may need to register/unregister listeners or |
37 | | * protocols. Its main purpose is in fact to serialize the rare stop/deinit() |
38 | | * phases. |
39 | | */ |
40 | | __decl_spinlock(proto_lock); |
41 | | |
42 | | /* Registers the protocol <proto> */ |
43 | | void protocol_register(struct protocol *proto) |
44 | 0 | { |
45 | 0 | int sock_family = proto->fam->sock_family; |
46 | |
|
47 | 0 | BUG_ON(sock_family < 0 || sock_family >= AF_CUST_MAX); |
48 | 0 | BUG_ON(proto->proto_type >= PROTO_NUM_TYPES); |
49 | |
|
50 | 0 | LIST_INIT(&proto->receivers); |
51 | 0 | proto->nb_receivers = 0; |
52 | |
|
53 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
54 | 0 | LIST_APPEND(&protocols, &proto->list); |
55 | 0 | __protocol_by_family[sock_family] |
56 | 0 | [proto->proto_type] |
57 | 0 | [proto->xprt_type == PROTO_TYPE_DGRAM || |
58 | 0 | proto->sock_prot == IPPROTO_MPTCP] = proto; |
59 | 0 | __proto_fam_by_family[sock_family] = proto->fam; |
60 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
61 | 0 | } |
62 | | |
63 | | /* Unregisters the protocol <proto>. Note that all listeners must have |
64 | | * previously been unbound. |
65 | | */ |
66 | | void protocol_unregister(struct protocol *proto) |
67 | 0 | { |
68 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
69 | 0 | LIST_DELETE(&proto->list); |
70 | 0 | LIST_INIT(&proto->list); |
71 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
72 | 0 | } |
73 | | |
74 | | /* clears flag <flag> on all protocols. */ |
75 | | void protocol_clrf_all(uint flag) |
76 | 0 | { |
77 | 0 | struct protocol *proto; |
78 | |
|
79 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
80 | 0 | list_for_each_entry(proto, &protocols, list) |
81 | 0 | proto->flags &= ~flag; |
82 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
83 | 0 | } |
84 | | |
85 | | /* sets flag <flag> on all protocols. */ |
86 | | void protocol_setf_all(uint flag) |
87 | 0 | { |
88 | 0 | struct protocol *proto; |
89 | |
|
90 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
91 | 0 | list_for_each_entry(proto, &protocols, list) |
92 | 0 | proto->flags |= flag; |
93 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
94 | 0 | } |
95 | | |
96 | | /* Checks if protocol <proto> supports PROTO_F flag <flag>. Returns zero if not, |
97 | | * non-zero if supported. It may return a cached value from a previous test, |
98 | | * and may run live tests then update the proto's flags to cache a result. It's |
99 | | * better to call it only if needed so that it doesn't result in modules being |
100 | | * loaded in case of a live test. It is only supposed to be used during boot. |
101 | | */ |
102 | | int protocol_supports_flag(struct protocol *proto, uint flag) |
103 | 0 | { |
104 | 0 | if (flag == PROTO_F_REUSEPORT_SUPPORTED) { |
105 | 0 | int ret = 0; |
106 | | |
107 | | /* check if the protocol supports SO_REUSEPORT */ |
108 | 0 | if (!(_HA_ATOMIC_LOAD(&proto->flags) & PROTO_F_REUSEPORT_SUPPORTED)) |
109 | 0 | return 0; |
110 | | |
111 | | /* TESTED is set, assume supported (live test already ran and passed) */ |
112 | 0 | if (_HA_ATOMIC_LOAD(&proto->flags) & PROTO_F_REUSEPORT_TESTED) |
113 | 0 | return 1; |
114 | | |
115 | | /* run a live check */ |
116 | 0 | ret = _sock_supports_reuseport(proto->fam, proto->sock_type, proto->sock_prot); |
117 | 0 | if (!ret) |
118 | 0 | _HA_ATOMIC_AND(&proto->flags, ~PROTO_F_REUSEPORT_SUPPORTED); |
119 | |
|
120 | 0 | _HA_ATOMIC_OR(&proto->flags, PROTO_F_REUSEPORT_TESTED); |
121 | 0 | return ret; |
122 | 0 | } |
123 | 0 | return 0; |
124 | 0 | } |
125 | | |
126 | | #ifdef USE_QUIC |
127 | | /* Return 1 if QUIC protocol may be bound, 0 if no, depending on the tuning |
128 | | * parameters. |
129 | | */ |
130 | | static inline int protocol_may_bind_quic(struct listener *l) |
131 | | { |
132 | | return !(quic_tune.fe.opts & QUIC_TUNE_FE_LISTEN_OFF); |
133 | | } |
134 | | #endif |
135 | | |
136 | | void protocol_init_rx_agents(void) |
137 | 0 | { |
138 | 0 | struct protocol *proto; |
139 | 0 | struct receiver *rx; |
140 | |
|
141 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
142 | 0 | list_for_each_entry(proto, &protocols, list) { |
143 | 0 | list_for_each_entry(rx, &proto->receivers, proto_list) { |
144 | 0 | MT_LIST_INIT(&rx->agent.link.list); |
145 | 0 | rx->agent.link.rx = rx; |
146 | 0 | rx->agent.close_fd = -1; |
147 | 0 | rx->agent.xfer_fd = -1; |
148 | 0 | rx->agent.getsocks_fd = -1; |
149 | 0 | } |
150 | 0 | } |
151 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
152 | 0 | } |
153 | | |
154 | | /* |
155 | | * With per-thread-group FD tables, gives up the calling thread's group's |
156 | | * inherited copies of the receiver FDs it does not own: a receiver's FD is |
157 | | * only ever used from its owner group, and keeping the other groups' copies |
158 | | * alive would make every release require all groups' cooperation. |
159 | | */ |
160 | | void protocol_localize_rx_fds(void) |
161 | 0 | { |
162 | 0 | struct protocol *proto; |
163 | 0 | struct receiver *rx; |
164 | |
|
165 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
166 | 0 | list_for_each_entry(proto, &protocols, list) { |
167 | 0 | list_for_each_entry(rx, &proto->receivers, proto_list) { |
168 | 0 | int fd = rx->fd; |
169 | |
|
170 | 0 | if (fd < 0 || rx_owner_tgid(rx) == tgid) |
171 | 0 | continue; |
172 | | |
173 | 0 | fdtab[fd].owner = NULL; |
174 | 0 | fdtab[fd].state = 0; |
175 | 0 | fdtab[fd].thread_mask = 0; |
176 | 0 | fdtab[fd].update_mask = 0; |
177 | 0 | fdtab[fd].running_mask = 0; |
178 | 0 | HA_ATOMIC_STORE(&fdtab[fd].refc_tgid, 0); |
179 | 0 | close(fd); |
180 | 0 | } |
181 | 0 | } |
182 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * Collect file descriptor from other thread groups, if they have their |
187 | | * own file descriptors table. |
188 | | */ |
189 | | int protocol_getsocks_foreign_fds(struct receiver ***orxs, int **ofds) |
190 | 0 | { |
191 | 0 | struct protocol *proto; |
192 | 0 | struct receiver *rx; |
193 | 0 | struct receiver **rxs; |
194 | 0 | int *fds; |
195 | 0 | int nb, filled, done, i; |
196 | |
|
197 | 0 | *orxs = NULL; |
198 | 0 | *ofds = NULL; |
199 | |
|
200 | 0 | if (MAX_TGROUPS < 2 || !(global.tune.options & GTUNE_NO_TG_FD_SHARING) || global.nbtgroups < 2) |
201 | 0 | return 0; |
202 | | |
203 | | /* first pass: count the candidates */ |
204 | 0 | nb = 0; |
205 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
206 | 0 | list_for_each_entry(proto, &protocols, list) { |
207 | 0 | list_for_each_entry(rx, &proto->receivers, proto_list) { |
208 | 0 | if (rx->fd < 0 || rx_owner_tgid(rx) == tgid) |
209 | 0 | continue; |
210 | 0 | if (!(ha_tgroup_ctx[rx_owner_tgid(rx) - 1].fdtab[rx->fd].state & FD_EXPORTED)) |
211 | 0 | continue; |
212 | 0 | nb++; |
213 | 0 | } |
214 | 0 | } |
215 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
216 | |
|
217 | 0 | if (!nb) |
218 | 0 | return 0; |
219 | | |
220 | 0 | rxs = calloc(nb, sizeof(*rxs)); |
221 | 0 | fds = calloc(nb, sizeof(*fds)); |
222 | 0 | if (!rxs || !fds) { |
223 | 0 | free(rxs); |
224 | 0 | free(fds); |
225 | 0 | return -1; |
226 | 0 | } |
227 | | |
228 | | /* second pass: arm the requests. A copy left over by a previous |
229 | | * timed-out collection is closed on the way. |
230 | | */ |
231 | 0 | filled = 0; |
232 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
233 | 0 | list_for_each_entry(proto, &protocols, list) { |
234 | 0 | list_for_each_entry(rx, &proto->receivers, proto_list) { |
235 | 0 | int old; |
236 | |
|
237 | 0 | if (filled >= nb) |
238 | 0 | break; |
239 | 0 | if (rx->fd < 0 || rx_owner_tgid(rx) == tgid) |
240 | 0 | continue; |
241 | 0 | if (!(ha_tgroup_ctx[rx_owner_tgid(rx) - 1].fdtab[rx->fd].state & FD_EXPORTED)) |
242 | 0 | continue; |
243 | | |
244 | 0 | old = rx->agent.getsocks_fd; |
245 | 0 | if (old >= 0) |
246 | 0 | close(old); |
247 | 0 | HA_ATOMIC_STORE(&rx->agent.getsocks_fd, -1); |
248 | 0 | rxs[filled++] = rx; |
249 | 0 | rx_agent_getsocks_request(rx, tgid); |
250 | 0 | } |
251 | 0 | } |
252 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
253 | 0 | nb = filled; |
254 | |
|
255 | 0 | for (i = 0; i < 50; i++) { |
256 | 0 | rx_xfer_drain(tgid - 1); |
257 | 0 | done = 1; |
258 | 0 | for (filled = 0; filled < nb; filled++) { |
259 | 0 | if (HA_ATOMIC_LOAD(&rxs[filled]->agent.getsocks_fd) < 0) |
260 | 0 | done = 0; |
261 | 0 | } |
262 | 0 | if (done) |
263 | 0 | break; |
264 | | /* XXX: we're waiting for other thread groups to send their |
265 | | * fds, let's sleep for a bit. |
266 | | */ |
267 | 0 | usleep(10000); |
268 | 0 | } |
269 | |
|
270 | 0 | done = 0; |
271 | 0 | for (i = 0; i < nb; i++) { |
272 | 0 | int f = HA_ATOMIC_LOAD(&rxs[i]->agent.getsocks_fd); |
273 | |
|
274 | 0 | if (f < 0) { |
275 | 0 | ha_warning("_getsocks: could not get a copy of a listener FD from thread group %u, the new process will have to bind it itself.\n", |
276 | 0 | rx_owner_tgid(rxs[i])); |
277 | 0 | continue; |
278 | 0 | } |
279 | 0 | HA_ATOMIC_STORE(&rxs[i]->agent.getsocks_fd, -1); |
280 | 0 | rxs[done] = rxs[i]; |
281 | 0 | fds[done] = f; |
282 | 0 | done++; |
283 | 0 | } |
284 | |
|
285 | 0 | if (!done) { |
286 | 0 | free(rxs); |
287 | 0 | free(fds); |
288 | 0 | return 0; |
289 | 0 | } |
290 | 0 | *orxs = rxs; |
291 | 0 | *ofds = fds; |
292 | 0 | return done; |
293 | 0 | } |
294 | | |
295 | | /* binds all listeners of all registered protocols. Returns a composition |
296 | | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
297 | | */ |
298 | | int protocol_bind_all(int verbose) |
299 | 0 | { |
300 | 0 | struct protocol *proto; |
301 | 0 | struct listener *listener; |
302 | 0 | struct receiver *receiver; |
303 | 0 | char msg[1000]; |
304 | 0 | char *errmsg; |
305 | 0 | int err, lerr; |
306 | |
|
307 | 0 | err = 0; |
308 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
309 | 0 | list_for_each_entry(proto, &protocols, list) { |
310 | 0 | list_for_each_entry(receiver, &proto->receivers, proto_list) { |
311 | 0 | listener = LIST_ELEM(receiver, struct listener *, rx); |
312 | | #ifdef USE_QUIC |
313 | | if ((proto == &proto_quic4 || proto == &proto_quic6) && |
314 | | !protocol_may_bind_quic(listener)) |
315 | | continue; |
316 | | #endif |
317 | |
|
318 | 0 | lerr = proto->fam->bind(receiver, &errmsg); |
319 | 0 | err |= lerr; |
320 | | |
321 | | /* errors are reported if <verbose> is set or if they are fatal */ |
322 | 0 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
323 | 0 | struct proxy *px = listener->bind_conf->frontend; |
324 | |
|
325 | 0 | if (lerr & ERR_ALERT) |
326 | 0 | ha_alert("Binding [%s:%d] for %s %s: protocol %s: %s.\n", |
327 | 0 | listener->bind_conf->file, listener->bind_conf->line, |
328 | 0 | proxy_type_str(px), px->id, proto->name, errmsg); |
329 | 0 | else if (lerr & ERR_WARN) |
330 | 0 | ha_warning("Binding [%s:%d] for %s %s: protocol %s: %s.\n", |
331 | 0 | listener->bind_conf->file, listener->bind_conf->line, |
332 | 0 | proxy_type_str(px), px->id, proto->name, errmsg); |
333 | 0 | } |
334 | 0 | if (lerr != ERR_NONE) |
335 | 0 | ha_free(&errmsg); |
336 | |
|
337 | 0 | if (lerr & ERR_ABORT) |
338 | 0 | break; |
339 | | |
340 | 0 | if (lerr & ~ERR_WARN) |
341 | 0 | continue; |
342 | | |
343 | | /* for now there's still always a listening function */ |
344 | 0 | BUG_ON(!proto->listen); |
345 | 0 | lerr = proto->listen(listener, msg, sizeof(msg)); |
346 | 0 | err |= lerr; |
347 | |
|
348 | 0 | if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) { |
349 | 0 | struct proxy *px = listener->bind_conf->frontend; |
350 | |
|
351 | 0 | if (lerr & ERR_ALERT) |
352 | 0 | ha_alert("Starting [%s:%d] for %s %s: protocol %s: %s.\n", |
353 | 0 | listener->bind_conf->file, listener->bind_conf->line, |
354 | 0 | proxy_type_str(px), px->id, proto->name, msg); |
355 | 0 | else if (lerr & ERR_WARN) |
356 | 0 | ha_warning("Starting [%s:%d] for %s %s: protocol %s: %s.\n", |
357 | 0 | listener->bind_conf->file, listener->bind_conf->line, |
358 | 0 | proxy_type_str(px), px->id, proto->name, msg); |
359 | 0 | } |
360 | 0 | if (lerr & ERR_ABORT) |
361 | 0 | break; |
362 | 0 | } |
363 | 0 | if (err & ERR_ABORT) |
364 | 0 | break; |
365 | 0 | } |
366 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
367 | 0 | return err; |
368 | 0 | } |
369 | | |
370 | | /* unbinds all listeners of all registered protocols. They are also closed. |
371 | | * This must be performed before calling exit() in order to get a chance to |
372 | | * remove file-system based sockets and pipes. |
373 | | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
374 | | */ |
375 | | int protocol_unbind_all(void) |
376 | 0 | { |
377 | 0 | struct protocol *proto; |
378 | 0 | struct listener *listener; |
379 | 0 | int err; |
380 | |
|
381 | 0 | err = 0; |
382 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
383 | 0 | list_for_each_entry(proto, &protocols, list) { |
384 | 0 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
385 | 0 | unbind_listener(listener); |
386 | 0 | } |
387 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
388 | 0 | return err; |
389 | 0 | } |
390 | | |
391 | | /* stops all listeners of all registered protocols. This will normally catch |
392 | | * every single listener, all protocols included. This is to be used during |
393 | | * soft_stop() only. It does not return any error. |
394 | | */ |
395 | | void protocol_stop_now(void) |
396 | 0 | { |
397 | 0 | struct protocol *proto; |
398 | 0 | struct listener *listener, *lback; |
399 | |
|
400 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
401 | 0 | list_for_each_entry(proto, &protocols, list) { |
402 | 0 | list_for_each_entry_safe(listener, lback, &proto->receivers, rx.proto_list) |
403 | 0 | stop_listener(listener, 0, 1, 0); |
404 | 0 | } |
405 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
406 | 0 | } |
407 | | |
408 | | /* suspends all listeners of all registered protocols. This is typically |
409 | | * used on SIG_TTOU to release all listening sockets for the time needed to |
410 | | * try to bind a new process. The listeners enter LI_PAUSED or LI_ASSIGNED. |
411 | | * It returns ERR_NONE, with ERR_FATAL on failure. |
412 | | */ |
413 | | int protocol_pause_all(void) |
414 | 0 | { |
415 | 0 | struct protocol *proto; |
416 | 0 | struct listener *listener; |
417 | 0 | int err; |
418 | |
|
419 | 0 | err = 0; |
420 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
421 | 0 | list_for_each_entry(proto, &protocols, list) { |
422 | 0 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
423 | 0 | if (!suspend_listener(listener, 0, 0)) |
424 | 0 | err |= ERR_FATAL; |
425 | 0 | } |
426 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
427 | 0 | return err; |
428 | 0 | } |
429 | | |
430 | | /* resumes all listeners of all registered protocols. This is typically used on |
431 | | * SIG_TTIN to re-enable listening sockets after a new process failed to bind. |
432 | | * The listeners switch to LI_READY/LI_FULL. It returns ERR_NONE, with ERR_FATAL |
433 | | * on failure. |
434 | | */ |
435 | | int protocol_resume_all(void) |
436 | 0 | { |
437 | 0 | struct protocol *proto; |
438 | 0 | struct listener *listener; |
439 | 0 | int err; |
440 | |
|
441 | 0 | err = 0; |
442 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
443 | 0 | list_for_each_entry(proto, &protocols, list) { |
444 | 0 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
445 | 0 | if (!resume_listener(listener, 0, 0)) |
446 | 0 | err |= ERR_FATAL; |
447 | 0 | } |
448 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
449 | 0 | return err; |
450 | 0 | } |
451 | | |
452 | | /* enables all listeners of all registered protocols. This is intended to be |
453 | | * used after a fork() to enable reading on all file descriptors. Returns ERR_NONE. |
454 | | */ |
455 | | int protocol_enable_all(void) |
456 | 0 | { |
457 | 0 | struct protocol *proto; |
458 | 0 | struct listener *listener; |
459 | |
|
460 | 0 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
461 | 0 | list_for_each_entry(proto, &protocols, list) { |
462 | 0 | list_for_each_entry(listener, &proto->receivers, rx.proto_list) |
463 | 0 | enable_listener(listener); |
464 | 0 | } |
465 | 0 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
466 | 0 | return ERR_NONE; |
467 | 0 | } |
468 | | |
469 | | /* |
470 | | * Local variables: |
471 | | * c-indent-level: 8 |
472 | | * c-basic-offset: 8 |
473 | | * End: |
474 | | */ |