Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 2 | * Protocol registration and listener management functions. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 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 <stdio.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <common/config.h> |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 17 | #include <common/errors.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 18 | #include <common/mini-clist.h> |
| 19 | #include <common/standard.h> |
| 20 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 21 | #include <proto/acl.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 22 | #include <proto/fd.h> |
| 23 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 24 | /* List head of all registered protocols */ |
| 25 | static struct list protocols = LIST_HEAD_INIT(protocols); |
| 26 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 27 | /* 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 | */ |
| 31 | void 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 | */ |
| 47 | void 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 Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 53 | if (listener->state == LI_LIMITED) |
| 54 | LIST_DEL(&listener->wait_queue); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 55 | listener->state = LI_LISTEN; |
| 56 | } |
| 57 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 58 | /* 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 | */ |
| 65 | int 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 Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 79 | if (l->state == LI_LIMITED) |
| 80 | LIST_DEL(&l->wait_queue); |
| 81 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 82 | EV_FD_CLR(l->fd, DIR_RD); |
| 83 | l->state = LI_PAUSED; |
| 84 | return 1; |
| 85 | } |
| 86 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 87 | /* 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 Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 91 | */ |
| 92 | int 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 Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 104 | if (l->state == LI_LIMITED) |
| 105 | LIST_DEL(&l->wait_queue); |
| 106 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 107 | 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 Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 117 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 118 | * it upon next close() using resume_listener(). |
| 119 | */ |
| 120 | void listener_full(struct listener *l) |
| 121 | { |
| 122 | if (l->state >= LI_READY) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 123 | if (l->state == LI_LIMITED) |
| 124 | LIST_DEL(&l->wait_queue); |
| 125 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 126 | EV_FD_CLR(l->fd, DIR_RD); |
| 127 | l->state = LI_FULL; |
| 128 | } |
| 129 | } |
| 130 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 131 | /* 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 | */ |
| 134 | void 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 Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 143 | /* 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 | */ |
| 149 | int 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 | */ |
| 163 | int 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 Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 172 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 173 | void 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 Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 186 | /* 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 | */ |
| 191 | int unbind_listener(struct listener *listener) |
| 192 | { |
| 193 | if (listener->state == LI_READY) |
| 194 | EV_FD_CLR(listener->fd, DIR_RD); |
| 195 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 196 | if (listener->state == LI_LIMITED) |
| 197 | LIST_DEL(&listener->wait_queue); |
| 198 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 199 | if (listener->state >= LI_PAUSED) { |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 200 | fd_delete(listener->fd); |
| 201 | listener->state = LI_ASSIGNED; |
| 202 | } |
| 203 | return ERR_NONE; |
| 204 | } |
| 205 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 206 | /* 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 | */ |
| 210 | int 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 Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 219 | /* 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 | */ |
| 224 | void 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 Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 233 | /* Registers the protocol <proto> */ |
| 234 | void 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 | */ |
| 242 | void protocol_unregister(struct protocol *proto) |
| 243 | { |
| 244 | LIST_DEL(&proto->list); |
| 245 | LIST_INIT(&proto->list); |
| 246 | } |
| 247 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 248 | /* binds all listeners of all registered protocols. Returns a composition |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 249 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 250 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 251 | int protocol_bind_all(char *errmsg, int errlen) |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 252 | { |
| 253 | struct protocol *proto; |
| 254 | int err; |
| 255 | |
| 256 | err = 0; |
| 257 | list_for_each_entry(proto, &protocols, list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 258 | if (proto->bind_all) { |
| 259 | err |= proto->bind_all(proto, errmsg, errlen); |
| 260 | if ( err & ERR_ABORT ) |
| 261 | break; |
| 262 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 263 | } |
| 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 Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 270 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 271 | */ |
| 272 | int 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 Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 279 | if (proto->unbind_all) { |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 280 | err |= proto->unbind_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 281 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 282 | } |
| 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 | */ |
| 290 | int 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 Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 297 | if (proto->enable_all) { |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 298 | err |= proto->enable_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 299 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 300 | } |
| 301 | return err; |
| 302 | } |
| 303 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 304 | /* 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 | */ |
| 308 | int 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 Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 315 | if (proto->disable_all) { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 316 | err |= proto->disable_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 317 | } |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 318 | } |
| 319 | return err; |
| 320 | } |
| 321 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 322 | /************************************************************************/ |
| 323 | /* All supported ACL keywords must be declared here. */ |
| 324 | /************************************************************************/ |
| 325 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 326 | /* set temp integer to the number of connexions to the same listening socket */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 327 | static int |
| 328 | acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir, |
| 329 | struct acl_expr *expr, struct acl_test *test) |
| 330 | { |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 331 | temp_pattern.data.integer = l4->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 332 | return 1; |
| 333 | } |
| 334 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 335 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 336 | static int |
| 337 | acl_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; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 341 | temp_pattern.data.integer = l4->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 346 | static struct acl_kw_list acl_kws = {{ },{ |
| 347 | { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING }, |
| 348 | { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING }, |
| 349 | { NULL, NULL, NULL, NULL }, |
| 350 | }}; |
| 351 | |
| 352 | __attribute__((constructor)) |
| 353 | static void __protocols_init(void) |
| 354 | { |
| 355 | acl_register_keywords(&acl_kws); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Local variables: |
| 360 | * c-indent-level: 8 |
| 361 | * c-basic-offset: 8 |
| 362 | * End: |
| 363 | */ |