blob: a942e44930c65df970a48c85285374ce099c9572 [file] [log] [blame]
Willy Tarreaud1d54542012-09-12 22:58:11 +02001/*
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 Tarreaua6e3be72016-08-10 18:24:48 +020013#include <sys/types.h>
Willy Tarreaub550d002015-02-20 16:53:25 +010014#include <sys/socket.h>
15
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020018#include <haproxy/list.h>
Willy Tarreau94320852020-09-01 18:48:35 +020019#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/protocol.h>
Willy Tarreaue91bff22020-09-02 11:11:43 +020021#include <haproxy/proxy.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020022#include <haproxy/tools.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020023
Willy Tarreaud1d54542012-09-12 22:58:11 +020024
25/* List head of all registered protocols */
26static struct list protocols = LIST_HEAD_INIT(protocols);
William Lallemand2d3f8a42018-09-11 16:51:28 +020027struct protocol *__protocol_by_family[AF_CUST_MAX] = { };
Willy Tarreaud1d54542012-09-12 22:58:11 +020028
Willy Tarreaudaacf362019-07-24 16:45:02 +020029/* This is the global spinlock we may need to register/unregister listeners or
30 * protocols. Its main purpose is in fact to serialize the rare stop/deinit()
31 * phases.
32 */
33__decl_spinlock(proto_lock);
34
Willy Tarreaud1d54542012-09-12 22:58:11 +020035/* Registers the protocol <proto> */
36void protocol_register(struct protocol *proto)
37{
Willy Tarreaudaacf362019-07-24 16:45:02 +020038 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020039 LIST_ADDQ(&protocols, &proto->list);
William Lallemand2d3f8a42018-09-11 16:51:28 +020040 if (proto->sock_domain >= 0 && proto->sock_domain < AF_CUST_MAX)
Willy Tarreaub550d002015-02-20 16:53:25 +010041 __protocol_by_family[proto->sock_domain] = proto;
Willy Tarreaudaacf362019-07-24 16:45:02 +020042 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020043}
44
45/* Unregisters the protocol <proto>. Note that all listeners must have
46 * previously been unbound.
47 */
48void protocol_unregister(struct protocol *proto)
49{
Willy Tarreaudaacf362019-07-24 16:45:02 +020050 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020051 LIST_DEL(&proto->list);
52 LIST_INIT(&proto->list);
Willy Tarreaudaacf362019-07-24 16:45:02 +020053 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020054}
55
56/* binds all listeners of all registered protocols. Returns a composition
57 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
58 */
Willy Tarreaue91bff22020-09-02 11:11:43 +020059int protocol_bind_all(int verbose)
Willy Tarreaud1d54542012-09-12 22:58:11 +020060{
61 struct protocol *proto;
Willy Tarreau94320852020-09-01 18:48:35 +020062 struct listener *listener;
Willy Tarreaue91bff22020-09-02 11:11:43 +020063 char msg[100];
64 int err, lerr;
Willy Tarreaud1d54542012-09-12 22:58:11 +020065
66 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +020067 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020068 list_for_each_entry(proto, &protocols, list) {
Willy Tarreau94320852020-09-01 18:48:35 +020069 list_for_each_entry(listener, &proto->listeners, proto_list) {
Willy Tarreaue91bff22020-09-02 11:11:43 +020070 lerr = proto->bind(listener, msg, sizeof(msg));
71
72 /* errors are reported if <verbose> is set or if they are fatal */
73 if (verbose || (lerr & (ERR_FATAL | ERR_ABORT))) {
74 struct proxy *px = listener->bind_conf->frontend;
75
76 if (lerr & ERR_ALERT)
77 ha_alert("Starting %s %s: %s\n",
78 proxy_type_str(px), px->id, msg);
79 else if (lerr & ERR_WARN)
80 ha_warning("Starting %s %s: %s\n",
81 proxy_type_str(px), px->id, msg);
82 }
83
84 err |= lerr;
85 if (lerr & ERR_ABORT)
Willy Tarreaud1d54542012-09-12 22:58:11 +020086 break;
87 }
Willy Tarreaue91bff22020-09-02 11:11:43 +020088 if (err & ERR_ABORT)
89 break;
Willy Tarreaud1d54542012-09-12 22:58:11 +020090 }
Willy Tarreaudaacf362019-07-24 16:45:02 +020091 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +020092 return err;
93}
94
95/* unbinds all listeners of all registered protocols. They are also closed.
96 * This must be performed before calling exit() in order to get a chance to
97 * remove file-system based sockets and pipes.
98 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
99 */
100int protocol_unbind_all(void)
101{
102 struct protocol *proto;
Willy Tarreauca212622020-09-02 10:31:31 +0200103 struct listener *listener;
Willy Tarreaud1d54542012-09-12 22:58:11 +0200104 int err;
105
106 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200107 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200108 list_for_each_entry(proto, &protocols, list) {
Willy Tarreauca212622020-09-02 10:31:31 +0200109 list_for_each_entry(listener, &proto->listeners, proto_list)
110 unbind_listener(listener);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200111 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200112 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200113 return err;
114}
115
116/* enables all listeners of all registered protocols. This is intended to be
117 * used after a fork() to enable reading on all file descriptors. Returns a
118 * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
119 */
120int protocol_enable_all(void)
121{
122 struct protocol *proto;
123 int err;
124
125 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200126 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200127 list_for_each_entry(proto, &protocols, list) {
128 if (proto->enable_all) {
129 err |= proto->enable_all(proto);
130 }
131 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200132 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200133 return err;
134}
135
136/* disables all listeners of all registered protocols. This may be used before
137 * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE,
138 * ERR_RETRYABLE, ERR_FATAL.
139 */
140int protocol_disable_all(void)
141{
142 struct protocol *proto;
143 int err;
144
145 err = 0;
Willy Tarreaudaacf362019-07-24 16:45:02 +0200146 HA_SPIN_LOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200147 list_for_each_entry(proto, &protocols, list) {
148 if (proto->disable_all) {
149 err |= proto->disable_all(proto);
150 }
151 }
Willy Tarreaudaacf362019-07-24 16:45:02 +0200152 HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock);
Willy Tarreaud1d54542012-09-12 22:58:11 +0200153 return err;
154}
155
Willy Tarreaud1d54542012-09-12 22:58:11 +0200156/*
157 * Local variables:
158 * c-indent-level: 8
159 * c-basic-offset: 8
160 * End:
161 */