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