Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | a6e3be7 | 2016-08-10 18:24:48 +0200 | [diff] [blame] | 13 | #include <sys/types.h> |
Willy Tarreau | b550d00 | 2015-02-20 16:53:25 +0100 | [diff] [blame] | 14 | #include <sys/socket.h> |
| 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/errors.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 18 | #include <haproxy/list.h> |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame^] | 19 | #include <haproxy/listener.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/protocol.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 21 | #include <haproxy/tools.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 23 | |
| 24 | /* List head of all registered protocols */ |
| 25 | static struct list protocols = LIST_HEAD_INIT(protocols); |
William Lallemand | 2d3f8a4 | 2018-09-11 16:51:28 +0200 | [diff] [blame] | 26 | struct protocol *__protocol_by_family[AF_CUST_MAX] = { }; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 28 | /* This is the global spinlock we may need to register/unregister listeners or |
| 29 | * protocols. Its main purpose is in fact to serialize the rare stop/deinit() |
| 30 | * phases. |
| 31 | */ |
| 32 | __decl_spinlock(proto_lock); |
| 33 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 34 | /* Registers the protocol <proto> */ |
| 35 | void protocol_register(struct protocol *proto) |
| 36 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 37 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 38 | LIST_ADDQ(&protocols, &proto->list); |
William Lallemand | 2d3f8a4 | 2018-09-11 16:51:28 +0200 | [diff] [blame] | 39 | if (proto->sock_domain >= 0 && proto->sock_domain < AF_CUST_MAX) |
Willy Tarreau | b550d00 | 2015-02-20 16:53:25 +0100 | [diff] [blame] | 40 | __protocol_by_family[proto->sock_domain] = proto; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 41 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 45 | * previously been unbound. |
| 46 | */ |
| 47 | void protocol_unregister(struct protocol *proto) |
| 48 | { |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 49 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 50 | LIST_DEL(&proto->list); |
| 51 | LIST_INIT(&proto->list); |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 52 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* binds all listeners of all registered protocols. Returns a composition |
| 56 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 57 | */ |
| 58 | int protocol_bind_all(char *errmsg, int errlen) |
| 59 | { |
| 60 | struct protocol *proto; |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame^] | 61 | struct listener *listener; |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 62 | int err; |
| 63 | |
| 64 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 65 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 66 | list_for_each_entry(proto, &protocols, list) { |
Willy Tarreau | 9432085 | 2020-09-01 18:48:35 +0200 | [diff] [blame^] | 67 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
| 68 | err |= proto->bind(listener, errmsg, errlen); |
| 69 | if (err & ERR_ABORT) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 73 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 74 | return err; |
| 75 | } |
| 76 | |
| 77 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 78 | * This must be performed before calling exit() in order to get a chance to |
| 79 | * remove file-system based sockets and pipes. |
| 80 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
| 81 | */ |
| 82 | int protocol_unbind_all(void) |
| 83 | { |
| 84 | struct protocol *proto; |
| 85 | int err; |
| 86 | |
| 87 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 88 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 89 | list_for_each_entry(proto, &protocols, list) { |
| 90 | if (proto->unbind_all) { |
| 91 | err |= proto->unbind_all(proto); |
| 92 | } |
| 93 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 94 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 95 | return err; |
| 96 | } |
| 97 | |
| 98 | /* enables all listeners of all registered protocols. This is intended to be |
| 99 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 100 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 101 | */ |
| 102 | int protocol_enable_all(void) |
| 103 | { |
| 104 | struct protocol *proto; |
| 105 | int err; |
| 106 | |
| 107 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 108 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 109 | list_for_each_entry(proto, &protocols, list) { |
| 110 | if (proto->enable_all) { |
| 111 | err |= proto->enable_all(proto); |
| 112 | } |
| 113 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 114 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 115 | return err; |
| 116 | } |
| 117 | |
| 118 | /* disables all listeners of all registered protocols. This may be used before |
| 119 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 120 | * ERR_RETRYABLE, ERR_FATAL. |
| 121 | */ |
| 122 | int protocol_disable_all(void) |
| 123 | { |
| 124 | struct protocol *proto; |
| 125 | int err; |
| 126 | |
| 127 | err = 0; |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 128 | HA_SPIN_LOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 129 | list_for_each_entry(proto, &protocols, list) { |
| 130 | if (proto->disable_all) { |
| 131 | err |= proto->disable_all(proto); |
| 132 | } |
| 133 | } |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 134 | HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 135 | return err; |
| 136 | } |
| 137 | |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Local variables: |
| 140 | * c-indent-level: 8 |
| 141 | * c-basic-offset: 8 |
| 142 | * End: |
| 143 | */ |