blob: 46c41f6078cb3b14ea093b51f8aa2d7676bcd03e [file] [log] [blame]
Willy Tarreaudd815982007-10-16 12:25:14 +02001/*
Willy Tarreau645513a2010-05-24 20:55:15 +02002 * Protocol registration and listener management functions.
Willy Tarreaudd815982007-10-16 12:25:14 +02003 *
Willy Tarreau645513a2010-05-24 20:55:15 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaudd815982007-10-16 12:25:14 +02005 *
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 <stdio.h>
14#include <string.h>
15
16#include <common/config.h>
Willy Tarreaudabf2e22007-10-28 21:59:24 +010017#include <common/errors.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020018#include <common/mini-clist.h>
19#include <common/standard.h>
20
Willy Tarreau645513a2010-05-24 20:55:15 +020021#include <proto/acl.h>
Willy Tarreaub648d632007-10-28 22:13:50 +010022#include <proto/fd.h>
23
Willy Tarreaudd815982007-10-16 12:25:14 +020024/* List head of all registered protocols */
25static struct list protocols = LIST_HEAD_INIT(protocols);
26
Willy Tarreaudabf2e22007-10-28 21:59:24 +010027/* This function adds the specified listener's file descriptor to the polling
28 * lists if it is in the LI_LISTEN state. The listener enters LI_READY or
29 * LI_FULL state depending on its number of connections.
30 */
31void enable_listener(struct listener *listener)
32{
33 if (listener->state == LI_LISTEN) {
34 if (listener->nbconn < listener->maxconn) {
35 EV_FD_SET(listener->fd, DIR_RD);
36 listener->state = LI_READY;
37 } else {
38 listener->state = LI_FULL;
39 }
40 }
41}
42
43/* This function removes the specified listener's file descriptor from the
44 * polling lists if it is in the LI_READY or in the LI_FULL state. The listener
45 * enters LI_LISTEN.
46 */
47void disable_listener(struct listener *listener)
48{
49 if (listener->state < LI_READY)
50 return;
51 if (listener->state == LI_READY)
52 EV_FD_CLR(listener->fd, DIR_RD);
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020053 if (listener->state == LI_LIMITED)
54 LIST_DEL(&listener->wait_queue);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010055 listener->state = LI_LISTEN;
56}
57
Willy Tarreaube58c382011-07-24 18:28:10 +020058/* This function tries to temporarily disable a listener, depending on the OS
59 * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores
60 * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but
61 * closes upon SHUT_WR and refuses to rebind. So a common validation path
62 * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling
63 * is disabled. It normally returns non-zero, unless an error is reported.
64 */
65int pause_listener(struct listener *l)
66{
67 if (l->state <= LI_PAUSED)
68 return 1;
69
70 if (shutdown(l->fd, SHUT_WR) != 0)
71 return 0; /* Solaris dies here */
72
73 if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
74 return 0; /* OpenBSD dies here */
75
76 if (shutdown(l->fd, SHUT_RD) != 0)
77 return 0; /* should always be OK */
78
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020079 if (l->state == LI_LIMITED)
80 LIST_DEL(&l->wait_queue);
81
Willy Tarreaube58c382011-07-24 18:28:10 +020082 EV_FD_CLR(l->fd, DIR_RD);
83 l->state = LI_PAUSED;
84 return 1;
85}
86
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +020087/* This function tries to resume a temporarily disabled listener. Paused, full,
88 * limited and disabled listeners are handled, which means that this function
89 * may replace enable_listener(). The resulting state will either be LI_READY
90 * or LI_FULL. 0 is returned in case of failure to resume (eg: dead socket).
Willy Tarreaube58c382011-07-24 18:28:10 +020091 */
92int resume_listener(struct listener *l)
93{
94 if (l->state < LI_PAUSED)
95 return 0;
96
97 if (l->state == LI_PAUSED &&
98 listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0)
99 return 0;
100
101 if (l->state == LI_READY)
102 return 1;
103
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200104 if (l->state == LI_LIMITED)
105 LIST_DEL(&l->wait_queue);
106
Willy Tarreaube58c382011-07-24 18:28:10 +0200107 if (l->nbconn >= l->maxconn) {
108 l->state = LI_FULL;
109 return 1;
110 }
111
112 EV_FD_SET(l->fd, DIR_RD);
113 l->state = LI_READY;
114 return 1;
115}
116
Willy Tarreau62793712011-07-24 19:23:38 +0200117/* Marks a ready listener as full so that the session code tries to re-enable
118 * it upon next close() using resume_listener().
119 */
120void listener_full(struct listener *l)
121{
122 if (l->state >= LI_READY) {
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200123 if (l->state == LI_LIMITED)
124 LIST_DEL(&l->wait_queue);
125
Willy Tarreau62793712011-07-24 19:23:38 +0200126 EV_FD_CLR(l->fd, DIR_RD);
127 l->state = LI_FULL;
128 }
129}
130
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200131/* Marks a ready listener as limited so that we only try to re-enable it when
132 * resources are free again. It will be queued into the specified queue.
133 */
134void limit_listener(struct listener *l, struct list *list)
135{
136 if (l->state == LI_READY) {
137 LIST_ADDQ(list, &l->wait_queue);
138 EV_FD_CLR(l->fd, DIR_RD);
139 l->state = LI_LIMITED;
140 }
141}
142
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100143/* This function adds all of the protocol's listener's file descriptors to the
144 * polling lists when they are in the LI_LISTEN state. It is intended to be
145 * used as a protocol's generic enable_all() primitive, for use after the
146 * fork(). It puts the listeners into LI_READY or LI_FULL states depending on
147 * their number of connections. It always returns ERR_NONE.
148 */
149int enable_all_listeners(struct protocol *proto)
150{
151 struct listener *listener;
152
153 list_for_each_entry(listener, &proto->listeners, proto_list)
154 enable_listener(listener);
155 return ERR_NONE;
156}
157
158/* This function removes all of the protocol's listener's file descriptors from
159 * the polling lists when they are in the LI_READY or LI_FULL states. It is
160 * intended to be used as a protocol's generic disable_all() primitive. It puts
161 * the listeners into LI_LISTEN, and always returns ERR_NONE.
162 */
163int disable_all_listeners(struct protocol *proto)
164{
165 struct listener *listener;
166
167 list_for_each_entry(listener, &proto->listeners, proto_list)
168 disable_listener(listener);
169 return ERR_NONE;
170}
171
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200172/* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */
173void dequeue_all_listeners(struct list *list)
174{
175 struct listener *listener, *l_back;
176
177 list_for_each_entry_safe(listener, l_back, list, wait_queue) {
178 /* This cannot fail because the listeners are by definition in
179 * the LI_LIMITED state. The function also removes the entry
180 * from the queue.
181 */
182 resume_listener(listener);
183 }
184}
185
Willy Tarreaub648d632007-10-28 22:13:50 +0100186/* This function closes the listening socket for the specified listener,
187 * provided that it's already in a listening state. The listener enters the
188 * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended
189 * to be used as a generic function for standard protocols.
190 */
191int unbind_listener(struct listener *listener)
192{
193 if (listener->state == LI_READY)
194 EV_FD_CLR(listener->fd, DIR_RD);
195
Willy Tarreaue6ca1fc2011-07-24 22:03:52 +0200196 if (listener->state == LI_LIMITED)
197 LIST_DEL(&listener->wait_queue);
198
Willy Tarreaube58c382011-07-24 18:28:10 +0200199 if (listener->state >= LI_PAUSED) {
Willy Tarreaub648d632007-10-28 22:13:50 +0100200 fd_delete(listener->fd);
201 listener->state = LI_ASSIGNED;
202 }
203 return ERR_NONE;
204}
205
Willy Tarreau3acf8c32007-10-28 22:35:41 +0100206/* This function closes all listening sockets bound to the protocol <proto>,
207 * and the listeners end in LI_ASSIGNED state if they were higher. It does not
208 * detach them from the protocol. It always returns ERR_NONE.
209 */
210int unbind_all_listeners(struct protocol *proto)
211{
212 struct listener *listener;
213
214 list_for_each_entry(listener, &proto->listeners, proto_list)
215 unbind_listener(listener);
216 return ERR_NONE;
217}
218
Willy Tarreau1a64d162007-10-28 22:26:05 +0100219/* Delete a listener from its protocol's list of listeners. The listener's
220 * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's
221 * number of listeners is updated. Note that the listener must have previously
222 * been unbound. This is the generic function to use to remove a listener.
223 */
224void delete_listener(struct listener *listener)
225{
226 if (listener->state != LI_ASSIGNED)
227 return;
228 listener->state = LI_INIT;
229 LIST_DEL(&listener->proto_list);
230 listener->proto->nb_listeners--;
231}
232
Willy Tarreaudd815982007-10-16 12:25:14 +0200233/* Registers the protocol <proto> */
234void protocol_register(struct protocol *proto)
235{
236 LIST_ADDQ(&protocols, &proto->list);
237}
238
239/* Unregisters the protocol <proto>. Note that all listeners must have
240 * previously been unbound.
241 */
242void protocol_unregister(struct protocol *proto)
243{
244 LIST_DEL(&proto->list);
245 LIST_INIT(&proto->list);
246}
247
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100248/* binds all listeners of all registered protocols. Returns a composition
Willy Tarreaudd815982007-10-16 12:25:14 +0200249 * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
250 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200251int protocol_bind_all(char *errmsg, int errlen)
Willy Tarreaudd815982007-10-16 12:25:14 +0200252{
253 struct protocol *proto;
254 int err;
255
256 err = 0;
257 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200258 if (proto->bind_all) {
259 err |= proto->bind_all(proto, errmsg, errlen);
260 if ( err & ERR_ABORT )
261 break;
262 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200263 }
264 return err;
265}
266
267/* unbinds all listeners of all registered protocols. They are also closed.
268 * This must be performed before calling exit() in order to get a chance to
269 * remove file-system based sockets and pipes.
Emeric Bruncf20bf12010-10-22 16:06:11 +0200270 * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT.
Willy Tarreaudd815982007-10-16 12:25:14 +0200271 */
272int protocol_unbind_all(void)
273{
274 struct protocol *proto;
275 int err;
276
277 err = 0;
278 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200279 if (proto->unbind_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200280 err |= proto->unbind_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200281 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200282 }
283 return err;
284}
285
286/* enables all listeners of all registered protocols. This is intended to be
287 * used after a fork() to enable reading on all file descriptors. Returns a
288 * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL.
289 */
290int protocol_enable_all(void)
291{
292 struct protocol *proto;
293 int err;
294
295 err = 0;
296 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200297 if (proto->enable_all) {
Willy Tarreaudd815982007-10-16 12:25:14 +0200298 err |= proto->enable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200299 }
Willy Tarreaudd815982007-10-16 12:25:14 +0200300 }
301 return err;
302}
303
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100304/* disables all listeners of all registered protocols. This may be used before
305 * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE,
306 * ERR_RETRYABLE, ERR_FATAL.
307 */
308int protocol_disable_all(void)
309{
310 struct protocol *proto;
311 int err;
312
313 err = 0;
314 list_for_each_entry(proto, &protocols, list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200315 if (proto->disable_all) {
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100316 err |= proto->disable_all(proto);
Emeric Bruncf20bf12010-10-22 16:06:11 +0200317 }
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100318 }
319 return err;
320}
321
Willy Tarreau645513a2010-05-24 20:55:15 +0200322/************************************************************************/
323/* All supported ACL keywords must be declared here. */
324/************************************************************************/
325
326/* set test->i to the number of connexions to the same listening socket */
327static int
328acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir,
329 struct acl_expr *expr, struct acl_test *test)
330{
331 test->i = l4->listener->nbconn;
332 return 1;
333}
334
335/* set test->i to the id of the socket (listener) */
336static int
337acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, int dir,
338 struct acl_expr *expr, struct acl_test *test) {
339
340 test->flags = ACL_TEST_F_READ_ONLY;
341
342 test->i = l4->listener->luid;
343
344 return 1;
345}
346
347/* Note: must not be declared <const> as its list will be overwritten */
348static struct acl_kw_list acl_kws = {{ },{
349 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING },
350 { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING },
351 { NULL, NULL, NULL, NULL },
352}};
353
354__attribute__((constructor))
355static void __protocols_init(void)
356{
357 acl_register_keywords(&acl_kws);
358}
359
360/*
361 * Local variables:
362 * c-indent-level: 8
363 * c-basic-offset: 8
364 * End:
365 */