Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 2 | * Listener management functions. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 4 | * Copyright 2000-2013 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 | |
Willy Tarreau | 4448925 | 2014-01-14 17:52:01 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <string.h> |
Willy Tarreau | 95ccdde | 2014-02-01 09:28:36 +0100 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <fcntl.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 19 | |
Willy Tarreau | 1bc4aab | 2012-10-08 20:11:03 +0200 | [diff] [blame] | 20 | #include <common/accept4.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 21 | #include <common/config.h> |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 22 | #include <common/errors.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 23 | #include <common/mini-clist.h> |
| 24 | #include <common/standard.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
| 26 | |
| 27 | #include <types/global.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 28 | #include <types/protocol.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 30 | #include <proto/acl.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 31 | #include <proto/fd.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 32 | #include <proto/freq_ctr.h> |
| 33 | #include <proto/log.h> |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 34 | #include <proto/sample.h> |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 35 | #include <proto/task.h> |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 36 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 37 | /* List head of all known bind keywords */ |
| 38 | static struct bind_kw_list bind_keywords = { |
| 39 | .list = LIST_HEAD_INIT(bind_keywords.list) |
| 40 | }; |
| 41 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 42 | /* This function adds the specified listener's file descriptor to the polling |
| 43 | * lists if it is in the LI_LISTEN state. The listener enters LI_READY or |
| 44 | * LI_FULL state depending on its number of connections. |
| 45 | */ |
| 46 | void enable_listener(struct listener *listener) |
| 47 | { |
| 48 | if (listener->state == LI_LISTEN) { |
| 49 | if (listener->nbconn < listener->maxconn) { |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 50 | fd_want_recv(listener->fd); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 51 | listener->state = LI_READY; |
| 52 | } else { |
| 53 | listener->state = LI_FULL; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* This function removes the specified listener's file descriptor from the |
| 59 | * polling lists if it is in the LI_READY or in the LI_FULL state. The listener |
| 60 | * enters LI_LISTEN. |
| 61 | */ |
| 62 | void disable_listener(struct listener *listener) |
| 63 | { |
| 64 | if (listener->state < LI_READY) |
| 65 | return; |
| 66 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 67 | fd_stop_recv(listener->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 68 | if (listener->state == LI_LIMITED) |
| 69 | LIST_DEL(&listener->wait_queue); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 70 | listener->state = LI_LISTEN; |
| 71 | } |
| 72 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 73 | /* This function tries to temporarily disable a listener, depending on the OS |
| 74 | * capabilities. Linux unbinds the listen socket after a SHUT_RD, and ignores |
| 75 | * SHUT_WR. Solaris refuses either shutdown(). OpenBSD ignores SHUT_RD but |
| 76 | * closes upon SHUT_WR and refuses to rebind. So a common validation path |
| 77 | * involves SHUT_WR && listen && SHUT_RD. In case of success, the FD's polling |
| 78 | * is disabled. It normally returns non-zero, unless an error is reported. |
| 79 | */ |
| 80 | int pause_listener(struct listener *l) |
| 81 | { |
| 82 | if (l->state <= LI_PAUSED) |
| 83 | return 1; |
| 84 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 85 | if (l->proto->sock_prot == IPPROTO_TCP) { |
| 86 | if (shutdown(l->fd, SHUT_WR) != 0) |
| 87 | return 0; /* Solaris dies here */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 89 | if (listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 90 | return 0; /* OpenBSD dies here */ |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 91 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 92 | if (shutdown(l->fd, SHUT_RD) != 0) |
| 93 | return 0; /* should always be OK */ |
| 94 | } |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 96 | if (l->state == LI_LIMITED) |
| 97 | LIST_DEL(&l->wait_queue); |
| 98 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 99 | fd_stop_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 100 | l->state = LI_PAUSED; |
| 101 | return 1; |
| 102 | } |
| 103 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 104 | /* This function tries to resume a temporarily disabled listener. Paused, full, |
| 105 | * limited and disabled listeners are handled, which means that this function |
| 106 | * may replace enable_listener(). The resulting state will either be LI_READY |
| 107 | * 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] | 108 | */ |
| 109 | int resume_listener(struct listener *l) |
| 110 | { |
| 111 | if (l->state < LI_PAUSED) |
| 112 | return 0; |
| 113 | |
Willy Tarreau | b3fb60b | 2012-10-04 08:56:31 +0200 | [diff] [blame] | 114 | if (l->proto->sock_prot == IPPROTO_TCP && |
| 115 | l->state == LI_PAUSED && |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 116 | listen(l->fd, l->backlog ? l->backlog : l->maxconn) != 0) |
| 117 | return 0; |
| 118 | |
| 119 | if (l->state == LI_READY) |
| 120 | return 1; |
| 121 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 122 | if (l->state == LI_LIMITED) |
| 123 | LIST_DEL(&l->wait_queue); |
| 124 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 125 | if (l->nbconn >= l->maxconn) { |
| 126 | l->state = LI_FULL; |
| 127 | return 1; |
| 128 | } |
| 129 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 130 | fd_want_recv(l->fd); |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 131 | l->state = LI_READY; |
| 132 | return 1; |
| 133 | } |
| 134 | |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 135 | /* Marks a ready listener as full so that the session code tries to re-enable |
| 136 | * it upon next close() using resume_listener(). |
| 137 | */ |
| 138 | void listener_full(struct listener *l) |
| 139 | { |
| 140 | if (l->state >= LI_READY) { |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 141 | if (l->state == LI_LIMITED) |
| 142 | LIST_DEL(&l->wait_queue); |
| 143 | |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 144 | fd_stop_recv(l->fd); |
Willy Tarreau | 6279371 | 2011-07-24 19:23:38 +0200 | [diff] [blame] | 145 | l->state = LI_FULL; |
| 146 | } |
| 147 | } |
| 148 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 149 | /* Marks a ready listener as limited so that we only try to re-enable it when |
| 150 | * resources are free again. It will be queued into the specified queue. |
| 151 | */ |
| 152 | void limit_listener(struct listener *l, struct list *list) |
| 153 | { |
| 154 | if (l->state == LI_READY) { |
| 155 | LIST_ADDQ(list, &l->wait_queue); |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 156 | fd_stop_recv(l->fd); |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 157 | l->state = LI_LIMITED; |
| 158 | } |
| 159 | } |
| 160 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 161 | /* This function adds all of the protocol's listener's file descriptors to the |
| 162 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 163 | * used as a protocol's generic enable_all() primitive, for use after the |
| 164 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 165 | * their number of connections. It always returns ERR_NONE. |
| 166 | */ |
| 167 | int enable_all_listeners(struct protocol *proto) |
| 168 | { |
| 169 | struct listener *listener; |
| 170 | |
| 171 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 172 | enable_listener(listener); |
| 173 | return ERR_NONE; |
| 174 | } |
| 175 | |
| 176 | /* This function removes all of the protocol's listener's file descriptors from |
| 177 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 178 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 179 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 180 | */ |
| 181 | int disable_all_listeners(struct protocol *proto) |
| 182 | { |
| 183 | struct listener *listener; |
| 184 | |
| 185 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 186 | disable_listener(listener); |
| 187 | return ERR_NONE; |
| 188 | } |
| 189 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 190 | /* Dequeues all of the listeners waiting for a resource in wait queue <queue>. */ |
| 191 | void dequeue_all_listeners(struct list *list) |
| 192 | { |
| 193 | struct listener *listener, *l_back; |
| 194 | |
| 195 | list_for_each_entry_safe(listener, l_back, list, wait_queue) { |
| 196 | /* This cannot fail because the listeners are by definition in |
| 197 | * the LI_LIMITED state. The function also removes the entry |
| 198 | * from the queue. |
| 199 | */ |
| 200 | resume_listener(listener); |
| 201 | } |
| 202 | } |
| 203 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 204 | /* This function closes the listening socket for the specified listener, |
| 205 | * provided that it's already in a listening state. The listener enters the |
| 206 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 207 | * to be used as a generic function for standard protocols. |
| 208 | */ |
| 209 | int unbind_listener(struct listener *listener) |
| 210 | { |
| 211 | if (listener->state == LI_READY) |
Willy Tarreau | 49b046d | 2012-08-09 12:11:58 +0200 | [diff] [blame] | 212 | fd_stop_recv(listener->fd); |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 213 | |
Willy Tarreau | e6ca1fc | 2011-07-24 22:03:52 +0200 | [diff] [blame] | 214 | if (listener->state == LI_LIMITED) |
| 215 | LIST_DEL(&listener->wait_queue); |
| 216 | |
Willy Tarreau | be58c38 | 2011-07-24 18:28:10 +0200 | [diff] [blame] | 217 | if (listener->state >= LI_PAUSED) { |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 218 | fd_delete(listener->fd); |
| 219 | listener->state = LI_ASSIGNED; |
| 220 | } |
| 221 | return ERR_NONE; |
| 222 | } |
| 223 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 224 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 225 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 226 | * detach them from the protocol. It always returns ERR_NONE. |
| 227 | */ |
| 228 | int unbind_all_listeners(struct protocol *proto) |
| 229 | { |
| 230 | struct listener *listener; |
| 231 | |
| 232 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 233 | unbind_listener(listener); |
| 234 | return ERR_NONE; |
| 235 | } |
| 236 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 237 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 238 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 239 | * number of listeners is updated. Note that the listener must have previously |
| 240 | * been unbound. This is the generic function to use to remove a listener. |
| 241 | */ |
| 242 | void delete_listener(struct listener *listener) |
| 243 | { |
| 244 | if (listener->state != LI_ASSIGNED) |
| 245 | return; |
| 246 | listener->state = LI_INIT; |
| 247 | LIST_DEL(&listener->proto_list); |
| 248 | listener->proto->nb_listeners--; |
| 249 | } |
| 250 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 251 | /* This function is called on a read event from a listening socket, corresponding |
| 252 | * to an accept. It tries to accept as many connections as possible, and for each |
| 253 | * calls the listener's accept handler (generally the frontend's accept handler). |
| 254 | */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 255 | void listener_accept(int fd) |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 256 | { |
| 257 | struct listener *l = fdtab[fd].owner; |
| 258 | struct proxy *p = l->frontend; |
Willy Tarreau | 50de90a | 2012-11-23 20:11:45 +0100 | [diff] [blame] | 259 | int max_accept = l->maxaccept ? l->maxaccept : 1; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 260 | int cfd; |
| 261 | int ret; |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 262 | #ifdef USE_ACCEPT4 |
| 263 | static int accept4_broken; |
| 264 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 265 | |
| 266 | if (unlikely(l->nbconn >= l->maxconn)) { |
| 267 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 268 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 271 | if (!(l->options & LI_O_UNLIMITED) && global.sps_lim) { |
| 272 | int max = freq_ctr_remain(&global.sess_per_sec, global.sps_lim, 0); |
| 273 | int expire; |
| 274 | |
| 275 | if (unlikely(!max)) { |
| 276 | /* frontend accept rate limit was reached */ |
| 277 | limit_listener(l, &global_listener_queue); |
| 278 | expire = tick_add(now_ms, next_event_delay(&global.sess_per_sec, global.sps_lim, 0)); |
| 279 | task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire)); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (max_accept > max) |
| 284 | max_accept = max; |
| 285 | } |
| 286 | |
| 287 | if (!(l->options & LI_O_UNLIMITED) && global.cps_lim) { |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 288 | int max = freq_ctr_remain(&global.conn_per_sec, global.cps_lim, 0); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 289 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 290 | |
| 291 | if (unlikely(!max)) { |
| 292 | /* frontend accept rate limit was reached */ |
| 293 | limit_listener(l, &global_listener_queue); |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 294 | expire = tick_add(now_ms, next_event_delay(&global.conn_per_sec, global.cps_lim, 0)); |
| 295 | task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire)); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 296 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (max_accept > max) |
| 300 | max_accept = max; |
| 301 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 302 | #ifdef USE_OPENSSL |
| 303 | if (!(l->options & LI_O_UNLIMITED) && global.ssl_lim && l->bind_conf && l->bind_conf->is_ssl) { |
| 304 | int max = freq_ctr_remain(&global.ssl_per_sec, global.ssl_lim, 0); |
| 305 | int expire; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 306 | |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 307 | if (unlikely(!max)) { |
| 308 | /* frontend accept rate limit was reached */ |
| 309 | limit_listener(l, &global_listener_queue); |
| 310 | expire = tick_add(now_ms, next_event_delay(&global.ssl_per_sec, global.ssl_lim, 0)); |
| 311 | task_schedule(global_listener_queue_task, tick_first(expire, global_listener_queue_task->expire)); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | if (max_accept > max) |
| 316 | max_accept = max; |
| 317 | } |
| 318 | #endif |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 319 | if (p && p->fe_sps_lim) { |
| 320 | int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0); |
| 321 | |
| 322 | if (unlikely(!max)) { |
| 323 | /* frontend accept rate limit was reached */ |
| 324 | limit_listener(l, &p->listener_queue); |
| 325 | task_schedule(p->task, tick_add(now_ms, next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 326 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (max_accept > max) |
| 330 | max_accept = max; |
| 331 | } |
| 332 | |
| 333 | /* Note: if we fail to allocate a connection because of configured |
| 334 | * limits, we'll schedule a new attempt worst 1 second later in the |
| 335 | * worst case. If we fail due to system limits or temporary resource |
| 336 | * shortage, we try again 100ms later in the worst case. |
| 337 | */ |
| 338 | while (max_accept--) { |
| 339 | struct sockaddr_storage addr; |
| 340 | socklen_t laddr = sizeof(addr); |
| 341 | |
| 342 | if (unlikely(actconn >= global.maxconn) && !(l->options & LI_O_UNLIMITED)) { |
| 343 | limit_listener(l, &global_listener_queue); |
| 344 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 345 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (unlikely(p && p->feconn >= p->maxconn)) { |
| 349 | limit_listener(l, &p->listener_queue); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 350 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Willy Tarreau | 1bc4aab | 2012-10-08 20:11:03 +0200 | [diff] [blame] | 353 | #ifdef USE_ACCEPT4 |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 354 | /* only call accept4() if it's known to be safe, otherwise |
| 355 | * fallback to the legacy accept() + fcntl(). |
| 356 | */ |
| 357 | if (unlikely(accept4_broken || |
| 358 | ((cfd = accept4(fd, (struct sockaddr *)&addr, &laddr, SOCK_NONBLOCK)) == -1 && |
| 359 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
| 360 | (accept4_broken = 1)))) |
| 361 | #endif |
Willy Tarreau | 6b3b0d4 | 2012-10-22 19:32:55 +0200 | [diff] [blame] | 362 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) != -1) |
| 363 | fcntl(cfd, F_SETFL, O_NONBLOCK); |
Willy Tarreau | 818dca5 | 2014-01-31 19:40:19 +0100 | [diff] [blame] | 364 | |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 365 | if (unlikely(cfd == -1)) { |
| 366 | switch (errno) { |
| 367 | case EAGAIN: |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 368 | fd_cant_recv(fd); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 369 | return; /* nothing more to accept */ |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 370 | case EINTR: |
| 371 | case ECONNABORTED: |
| 372 | continue; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 373 | case ENFILE: |
| 374 | if (p) |
| 375 | send_log(p, LOG_EMERG, |
| 376 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 377 | p->id, maxfd); |
| 378 | limit_listener(l, &global_listener_queue); |
| 379 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 380 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 381 | case EMFILE: |
| 382 | if (p) |
| 383 | send_log(p, LOG_EMERG, |
| 384 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 385 | p->id, maxfd); |
| 386 | limit_listener(l, &global_listener_queue); |
| 387 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 388 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 389 | case ENOBUFS: |
| 390 | case ENOMEM: |
| 391 | if (p) |
| 392 | send_log(p, LOG_EMERG, |
| 393 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 394 | p->id, maxfd); |
| 395 | limit_listener(l, &global_listener_queue); |
| 396 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 397 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 398 | default: |
Willy Tarreau | a593ec5 | 2014-01-20 21:21:30 +0100 | [diff] [blame] | 399 | /* unexpected result, let's give up and let other tasks run */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 400 | goto stop; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | if (unlikely(cfd >= global.maxsock)) { |
| 405 | send_log(p, LOG_EMERG, |
| 406 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 407 | p->id); |
| 408 | close(cfd); |
| 409 | limit_listener(l, &global_listener_queue); |
| 410 | task_schedule(global_listener_queue_task, tick_add(now_ms, 1000)); /* try again in 1 second */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 411 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* increase the per-process number of cumulated connections */ |
| 415 | if (!(l->options & LI_O_UNLIMITED)) { |
| 416 | update_freq_ctr(&global.conn_per_sec, 1); |
| 417 | if (global.conn_per_sec.curr_ctr > global.cps_max) |
| 418 | global.cps_max = global.conn_per_sec.curr_ctr; |
| 419 | actconn++; |
| 420 | } |
| 421 | |
| 422 | jobs++; |
| 423 | totalconn++; |
| 424 | l->nbconn++; |
| 425 | |
| 426 | if (l->counters) { |
| 427 | if (l->nbconn > l->counters->conn_max) |
| 428 | l->counters->conn_max = l->nbconn; |
| 429 | } |
| 430 | |
| 431 | ret = l->accept(l, cfd, &addr); |
| 432 | if (unlikely(ret <= 0)) { |
| 433 | /* The connection was closed by session_accept(). Either |
| 434 | * we just have to ignore it (ret == 0) or it's a critical |
| 435 | * error due to a resource shortage, and we must stop the |
| 436 | * listener (ret < 0). |
| 437 | */ |
| 438 | if (!(l->options & LI_O_UNLIMITED)) |
| 439 | actconn--; |
| 440 | jobs--; |
| 441 | l->nbconn--; |
| 442 | if (ret == 0) /* successful termination */ |
| 443 | continue; |
| 444 | |
| 445 | limit_listener(l, &global_listener_queue); |
| 446 | task_schedule(global_listener_queue_task, tick_add(now_ms, 100)); /* try again in 100 ms */ |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 447 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (l->nbconn >= l->maxconn) { |
| 451 | listener_full(l); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 452 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 455 | /* increase the per-process number of cumulated connections */ |
| 456 | if (!(l->options & LI_O_UNLIMITED)) { |
| 457 | update_freq_ctr(&global.sess_per_sec, 1); |
| 458 | if (global.sess_per_sec.curr_ctr > global.sps_max) |
| 459 | global.sps_max = global.sess_per_sec.curr_ctr; |
| 460 | } |
Willy Tarreau | e43d532 | 2013-10-07 20:01:52 +0200 | [diff] [blame] | 461 | #ifdef USE_OPENSSL |
| 462 | if (!(l->options & LI_O_UNLIMITED) && l->bind_conf && l->bind_conf->is_ssl) { |
| 463 | |
| 464 | update_freq_ctr(&global.ssl_per_sec, 1); |
| 465 | if (global.ssl_per_sec.curr_ctr > global.ssl_max) |
| 466 | global.ssl_max = global.ssl_per_sec.curr_ctr; |
| 467 | } |
| 468 | #endif |
Willy Tarreau | 93e7c00 | 2013-10-07 18:51:07 +0200 | [diff] [blame] | 469 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 470 | } /* end of while (max_accept--) */ |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 471 | |
Willy Tarreau | aece46a | 2012-07-06 12:25:58 +0200 | [diff] [blame] | 472 | /* we've exhausted max_accept, so there is no need to poll again */ |
Willy Tarreau | 6c11bd2 | 2014-01-24 00:54:27 +0100 | [diff] [blame] | 473 | stop: |
| 474 | fd_done_recv(fd); |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 475 | return; |
Willy Tarreau | bbebbbf | 2012-05-07 21:22:09 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Willy Tarreau | 2698266 | 2012-09-12 23:17:10 +0200 | [diff] [blame] | 478 | /* |
| 479 | * Registers the bind keyword list <kwl> as a list of valid keywords for next |
| 480 | * parsing sessions. |
| 481 | */ |
| 482 | void bind_register_keywords(struct bind_kw_list *kwl) |
| 483 | { |
| 484 | LIST_ADDQ(&bind_keywords.list, &kwl->list); |
| 485 | } |
| 486 | |
| 487 | /* Return a pointer to the bind keyword <kw>, or NULL if not found. If the |
| 488 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 489 | * find one with a valid ->parse() function. This way it is possible to declare |
| 490 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 491 | * if some options are met. Note that if the requested keyword contains an |
| 492 | * opening parenthesis, everything from this point is ignored. |
| 493 | */ |
| 494 | struct bind_kw *bind_find_kw(const char *kw) |
| 495 | { |
| 496 | int index; |
| 497 | const char *kwend; |
| 498 | struct bind_kw_list *kwl; |
| 499 | struct bind_kw *ret = NULL; |
| 500 | |
| 501 | kwend = strchr(kw, '('); |
| 502 | if (!kwend) |
| 503 | kwend = kw + strlen(kw); |
| 504 | |
| 505 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 506 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 507 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 508 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 509 | if (kwl->kw[index].parse) |
| 510 | return &kwl->kw[index]; /* found it !*/ |
| 511 | else |
| 512 | ret = &kwl->kw[index]; /* may be OK */ |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | return ret; |
| 517 | } |
| 518 | |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 519 | /* Dumps all registered "bind" keywords to the <out> string pointer. The |
| 520 | * unsupported keywords are only dumped if their supported form was not |
| 521 | * found. |
| 522 | */ |
| 523 | void bind_dump_kws(char **out) |
| 524 | { |
| 525 | struct bind_kw_list *kwl; |
| 526 | int index; |
| 527 | |
| 528 | *out = NULL; |
| 529 | list_for_each_entry(kwl, &bind_keywords.list, list) { |
| 530 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 531 | if (kwl->kw[index].parse || |
| 532 | bind_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 533 | memprintf(out, "%s[%4s] %s%s%s\n", *out ? *out : "", |
| 534 | kwl->scope, |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 535 | kwl->kw[index].kw, |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 536 | kwl->kw[index].skip ? " <arg>" : "", |
| 537 | kwl->kw[index].parse ? "" : " (not supported)"); |
Willy Tarreau | 8638f48 | 2012-09-18 18:01:17 +0200 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 543 | /************************************************************************/ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 544 | /* All supported sample and ACL keywords must be declared here. */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 545 | /************************************************************************/ |
| 546 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 547 | /* 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] | 548 | static int |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 549 | smp_fetch_dconn(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 550 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 551 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 552 | smp->type = SMP_T_UINT; |
| 553 | smp->data.uint = l4->listener->nbconn; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 554 | return 1; |
| 555 | } |
| 556 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 557 | /* set temp integer to the id of the socket (listener) */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 558 | static int |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 559 | smp_fetch_so_id(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
Willy Tarreau | ef38c39 | 2013-07-22 16:29:32 +0200 | [diff] [blame] | 560 | const struct arg *args, struct sample *smp, const char *kw) |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 561 | { |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 562 | smp->type = SMP_T_UINT; |
| 563 | smp->data.uint = l4->listener->luid; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 564 | return 1; |
| 565 | } |
| 566 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 567 | /* parse the "accept-proxy" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 568 | static int bind_parse_accept_proxy(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 569 | { |
| 570 | struct listener *l; |
| 571 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 572 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 573 | l->options |= LI_O_ACC_PROXY; |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /* parse the "backlog" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 579 | static int bind_parse_backlog(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 580 | { |
| 581 | struct listener *l; |
| 582 | int val; |
| 583 | |
| 584 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 585 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 586 | return ERR_ALERT | ERR_FATAL; |
| 587 | } |
| 588 | |
| 589 | val = atol(args[cur_arg + 1]); |
| 590 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 591 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 592 | return ERR_ALERT | ERR_FATAL; |
| 593 | } |
| 594 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 595 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 596 | l->backlog = val; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* parse the "id" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 602 | static int bind_parse_id(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 603 | { |
| 604 | struct eb32_node *node; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 605 | struct listener *l, *new; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 606 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 607 | if (conf->listeners.n != conf->listeners.p) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 608 | memprintf(err, "'%s' can only be used with a single socket", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 609 | return ERR_ALERT | ERR_FATAL; |
| 610 | } |
| 611 | |
| 612 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 613 | memprintf(err, "'%s' : expects an integer argument", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 614 | return ERR_ALERT | ERR_FATAL; |
| 615 | } |
| 616 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 617 | new = LIST_NEXT(&conf->listeners, struct listener *, by_bind); |
| 618 | new->luid = atol(args[cur_arg + 1]); |
| 619 | new->conf.id.key = new->luid; |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 620 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 621 | if (new->luid <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 622 | memprintf(err, "'%s' : custom id has to be > 0", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 623 | return ERR_ALERT | ERR_FATAL; |
| 624 | } |
| 625 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 626 | node = eb32_lookup(&px->conf.used_listener_id, new->luid); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 627 | if (node) { |
| 628 | l = container_of(node, struct listener, conf.id); |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 629 | memprintf(err, "'%s' : custom id %d already used at %s:%d ('bind %s')", |
| 630 | args[cur_arg], l->luid, l->bind_conf->file, l->bind_conf->line, |
| 631 | l->bind_conf->arg); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 632 | return ERR_ALERT | ERR_FATAL; |
| 633 | } |
| 634 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 635 | eb32_insert(&px->conf.used_listener_id, &new->conf.id); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /* parse the "maxconn" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 640 | static int bind_parse_maxconn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 641 | { |
| 642 | struct listener *l; |
| 643 | int val; |
| 644 | |
| 645 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 646 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 647 | return ERR_ALERT | ERR_FATAL; |
| 648 | } |
| 649 | |
| 650 | val = atol(args[cur_arg + 1]); |
| 651 | if (val <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 652 | memprintf(err, "'%s' : invalid value %d, must be > 0", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 653 | return ERR_ALERT | ERR_FATAL; |
| 654 | } |
| 655 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 656 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 657 | l->maxconn = val; |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | /* parse the "name" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 663 | static int bind_parse_name(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 664 | { |
| 665 | struct listener *l; |
| 666 | |
| 667 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 668 | memprintf(err, "'%s' : missing name", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 669 | return ERR_ALERT | ERR_FATAL; |
| 670 | } |
| 671 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 672 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 673 | l->name = strdup(args[cur_arg + 1]); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /* parse the "nice" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 679 | static int bind_parse_nice(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 680 | { |
| 681 | struct listener *l; |
| 682 | int val; |
| 683 | |
| 684 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 685 | memprintf(err, "'%s' : missing value", args[cur_arg]); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 686 | return ERR_ALERT | ERR_FATAL; |
| 687 | } |
| 688 | |
| 689 | val = atol(args[cur_arg + 1]); |
| 690 | if (val < -1024 || val > 1024) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 691 | memprintf(err, "'%s' : invalid value %d, allowed range is -1024..1024", args[cur_arg], val); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 692 | return ERR_ALERT | ERR_FATAL; |
| 693 | } |
| 694 | |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 695 | list_for_each_entry(l, &conf->listeners, by_bind) |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 696 | l->nice = val; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 702 | /* Note: must not be declared <const> as its list will be overwritten. |
| 703 | * Please take care of keeping this list alphabetically sorted. |
| 704 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 705 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 706 | { "dst_conn", smp_fetch_dconn, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, }, |
| 707 | { "so_id", smp_fetch_so_id, 0, NULL, SMP_T_UINT, SMP_USE_FTEND, }, |
| 708 | { /* END */ }, |
| 709 | }}; |
| 710 | |
| 711 | /* Note: must not be declared <const> as its list will be overwritten. |
| 712 | * Please take care of keeping this list alphabetically sorted. |
| 713 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 714 | static struct acl_kw_list acl_kws = {ILH, { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 715 | { /* END */ }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 716 | }}; |
| 717 | |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 718 | /* Note: must not be declared <const> as its list will be overwritten. |
| 719 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 720 | * all code contributors. |
| 721 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 722 | * the config parser can report an appropriate error when a known keyword was |
| 723 | * not enabled. |
| 724 | */ |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 725 | static struct bind_kw_list bind_kws = { "ALL", { }, { |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 726 | { "accept-proxy", bind_parse_accept_proxy, 0 }, /* enable PROXY protocol */ |
| 727 | { "backlog", bind_parse_backlog, 1 }, /* set backlog of listening socket */ |
| 728 | { "id", bind_parse_id, 1 }, /* set id of listening socket */ |
| 729 | { "maxconn", bind_parse_maxconn, 1 }, /* set maxconn of listening socket */ |
| 730 | { "name", bind_parse_name, 1 }, /* set name of listening socket */ |
| 731 | { "nice", bind_parse_nice, 1 }, /* set nice of listening socket */ |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 732 | { /* END */ }, |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 733 | }}; |
| 734 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 735 | __attribute__((constructor)) |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 736 | static void __listener_init(void) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 737 | { |
Willy Tarreau | 0ccb744 | 2013-01-07 22:54:17 +0100 | [diff] [blame] | 738 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 739 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 3dcc341 | 2012-09-18 17:17:28 +0200 | [diff] [blame] | 740 | bind_register_keywords(&bind_kws); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Local variables: |
| 745 | * c-indent-level: 8 |
| 746 | * c-basic-offset: 8 |
| 747 | * End: |
| 748 | */ |