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); |
| 53 | listener->state = LI_LISTEN; |
| 54 | } |
| 55 | |
| 56 | /* This function adds all of the protocol's listener's file descriptors to the |
| 57 | * polling lists when they are in the LI_LISTEN state. It is intended to be |
| 58 | * used as a protocol's generic enable_all() primitive, for use after the |
| 59 | * fork(). It puts the listeners into LI_READY or LI_FULL states depending on |
| 60 | * their number of connections. It always returns ERR_NONE. |
| 61 | */ |
| 62 | int enable_all_listeners(struct protocol *proto) |
| 63 | { |
| 64 | struct listener *listener; |
| 65 | |
| 66 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 67 | enable_listener(listener); |
| 68 | return ERR_NONE; |
| 69 | } |
| 70 | |
| 71 | /* This function removes all of the protocol's listener's file descriptors from |
| 72 | * the polling lists when they are in the LI_READY or LI_FULL states. It is |
| 73 | * intended to be used as a protocol's generic disable_all() primitive. It puts |
| 74 | * the listeners into LI_LISTEN, and always returns ERR_NONE. |
| 75 | */ |
| 76 | int disable_all_listeners(struct protocol *proto) |
| 77 | { |
| 78 | struct listener *listener; |
| 79 | |
| 80 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 81 | disable_listener(listener); |
| 82 | return ERR_NONE; |
| 83 | } |
| 84 | |
Willy Tarreau | b648d63 | 2007-10-28 22:13:50 +0100 | [diff] [blame] | 85 | /* This function closes the listening socket for the specified listener, |
| 86 | * provided that it's already in a listening state. The listener enters the |
| 87 | * LI_ASSIGNED state. It always returns ERR_NONE. This function is intended |
| 88 | * to be used as a generic function for standard protocols. |
| 89 | */ |
| 90 | int unbind_listener(struct listener *listener) |
| 91 | { |
| 92 | if (listener->state == LI_READY) |
| 93 | EV_FD_CLR(listener->fd, DIR_RD); |
| 94 | |
| 95 | if (listener->state >= LI_LISTEN) { |
| 96 | fd_delete(listener->fd); |
| 97 | listener->state = LI_ASSIGNED; |
| 98 | } |
| 99 | return ERR_NONE; |
| 100 | } |
| 101 | |
Willy Tarreau | 3acf8c3 | 2007-10-28 22:35:41 +0100 | [diff] [blame] | 102 | /* This function closes all listening sockets bound to the protocol <proto>, |
| 103 | * and the listeners end in LI_ASSIGNED state if they were higher. It does not |
| 104 | * detach them from the protocol. It always returns ERR_NONE. |
| 105 | */ |
| 106 | int unbind_all_listeners(struct protocol *proto) |
| 107 | { |
| 108 | struct listener *listener; |
| 109 | |
| 110 | list_for_each_entry(listener, &proto->listeners, proto_list) |
| 111 | unbind_listener(listener); |
| 112 | return ERR_NONE; |
| 113 | } |
| 114 | |
Willy Tarreau | 1a64d16 | 2007-10-28 22:26:05 +0100 | [diff] [blame] | 115 | /* Delete a listener from its protocol's list of listeners. The listener's |
| 116 | * state is automatically updated from LI_ASSIGNED to LI_INIT. The protocol's |
| 117 | * number of listeners is updated. Note that the listener must have previously |
| 118 | * been unbound. This is the generic function to use to remove a listener. |
| 119 | */ |
| 120 | void delete_listener(struct listener *listener) |
| 121 | { |
| 122 | if (listener->state != LI_ASSIGNED) |
| 123 | return; |
| 124 | listener->state = LI_INIT; |
| 125 | LIST_DEL(&listener->proto_list); |
| 126 | listener->proto->nb_listeners--; |
| 127 | } |
| 128 | |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 129 | /* Registers the protocol <proto> */ |
| 130 | void protocol_register(struct protocol *proto) |
| 131 | { |
| 132 | LIST_ADDQ(&protocols, &proto->list); |
| 133 | } |
| 134 | |
| 135 | /* Unregisters the protocol <proto>. Note that all listeners must have |
| 136 | * previously been unbound. |
| 137 | */ |
| 138 | void protocol_unregister(struct protocol *proto) |
| 139 | { |
| 140 | LIST_DEL(&proto->list); |
| 141 | LIST_INIT(&proto->list); |
| 142 | } |
| 143 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 144 | /* binds all listeners of all registered protocols. Returns a composition |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 145 | * of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 146 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 147 | int protocol_bind_all(char *errmsg, int errlen) |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 148 | { |
| 149 | struct protocol *proto; |
| 150 | int err; |
| 151 | |
| 152 | err = 0; |
| 153 | list_for_each_entry(proto, &protocols, list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 154 | if (proto->bind_all) { |
| 155 | err |= proto->bind_all(proto, errmsg, errlen); |
| 156 | if ( err & ERR_ABORT ) |
| 157 | break; |
| 158 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 159 | } |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 164 | * This must be performed before calling exit() in order to get a chance to |
| 165 | * remove file-system based sockets and pipes. |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 166 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL, ERR_ABORT. |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 167 | */ |
| 168 | int protocol_unbind_all(void) |
| 169 | { |
| 170 | struct protocol *proto; |
| 171 | int err; |
| 172 | |
| 173 | err = 0; |
| 174 | list_for_each_entry(proto, &protocols, list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 175 | if (proto->unbind_all) { |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 176 | err |= proto->unbind_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 177 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 178 | } |
| 179 | return err; |
| 180 | } |
| 181 | |
| 182 | /* enables all listeners of all registered protocols. This is intended to be |
| 183 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 184 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 185 | */ |
| 186 | int protocol_enable_all(void) |
| 187 | { |
| 188 | struct protocol *proto; |
| 189 | int err; |
| 190 | |
| 191 | err = 0; |
| 192 | list_for_each_entry(proto, &protocols, list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 193 | if (proto->enable_all) { |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 194 | err |= proto->enable_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 195 | } |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 196 | } |
| 197 | return err; |
| 198 | } |
| 199 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 200 | /* disables all listeners of all registered protocols. This may be used before |
| 201 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 202 | * ERR_RETRYABLE, ERR_FATAL. |
| 203 | */ |
| 204 | int protocol_disable_all(void) |
| 205 | { |
| 206 | struct protocol *proto; |
| 207 | int err; |
| 208 | |
| 209 | err = 0; |
| 210 | list_for_each_entry(proto, &protocols, list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 211 | if (proto->disable_all) { |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 212 | err |= proto->disable_all(proto); |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 213 | } |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 214 | } |
| 215 | return err; |
| 216 | } |
| 217 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 218 | /************************************************************************/ |
| 219 | /* All supported ACL keywords must be declared here. */ |
| 220 | /************************************************************************/ |
| 221 | |
| 222 | /* set test->i to the number of connexions to the same listening socket */ |
| 223 | static int |
| 224 | acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir, |
| 225 | struct acl_expr *expr, struct acl_test *test) |
| 226 | { |
| 227 | test->i = l4->listener->nbconn; |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | /* set test->i to the id of the socket (listener) */ |
| 232 | static int |
| 233 | acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, int dir, |
| 234 | struct acl_expr *expr, struct acl_test *test) { |
| 235 | |
| 236 | test->flags = ACL_TEST_F_READ_ONLY; |
| 237 | |
| 238 | test->i = l4->listener->luid; |
| 239 | |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 244 | static struct acl_kw_list acl_kws = {{ },{ |
| 245 | { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING }, |
| 246 | { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING }, |
| 247 | { NULL, NULL, NULL, NULL }, |
| 248 | }}; |
| 249 | |
| 250 | __attribute__((constructor)) |
| 251 | static void __protocols_init(void) |
| 252 | { |
| 253 | acl_register_keywords(&acl_kws); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Local variables: |
| 258 | * c-indent-level: 8 |
| 259 | * c-basic-offset: 8 |
| 260 | * End: |
| 261 | */ |