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