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 | */ |
| 147 | int protocol_bind_all(void) |
| 148 | { |
| 149 | struct protocol *proto; |
| 150 | int err; |
| 151 | |
| 152 | err = 0; |
| 153 | list_for_each_entry(proto, &protocols, list) { |
| 154 | if (proto->bind_all) |
| 155 | err |= proto->bind_all(proto); |
| 156 | } |
| 157 | return err; |
| 158 | } |
| 159 | |
| 160 | /* unbinds all listeners of all registered protocols. They are also closed. |
| 161 | * This must be performed before calling exit() in order to get a chance to |
| 162 | * remove file-system based sockets and pipes. |
| 163 | * Returns a composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 164 | */ |
| 165 | int protocol_unbind_all(void) |
| 166 | { |
| 167 | struct protocol *proto; |
| 168 | int err; |
| 169 | |
| 170 | err = 0; |
| 171 | list_for_each_entry(proto, &protocols, list) { |
| 172 | if (proto->unbind_all) |
| 173 | err |= proto->unbind_all(proto); |
| 174 | } |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | /* enables all listeners of all registered protocols. This is intended to be |
| 179 | * used after a fork() to enable reading on all file descriptors. Returns a |
| 180 | * composition of ERR_NONE, ERR_RETRYABLE, ERR_FATAL. |
| 181 | */ |
| 182 | int protocol_enable_all(void) |
| 183 | { |
| 184 | struct protocol *proto; |
| 185 | int err; |
| 186 | |
| 187 | err = 0; |
| 188 | list_for_each_entry(proto, &protocols, list) { |
| 189 | if (proto->enable_all) |
| 190 | err |= proto->enable_all(proto); |
| 191 | } |
| 192 | return err; |
| 193 | } |
| 194 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 195 | /* disables all listeners of all registered protocols. This may be used before |
| 196 | * a fork() to avoid duplicating poll lists. Returns a composition of ERR_NONE, |
| 197 | * ERR_RETRYABLE, ERR_FATAL. |
| 198 | */ |
| 199 | int protocol_disable_all(void) |
| 200 | { |
| 201 | struct protocol *proto; |
| 202 | int err; |
| 203 | |
| 204 | err = 0; |
| 205 | list_for_each_entry(proto, &protocols, list) { |
| 206 | if (proto->disable_all) |
| 207 | err |= proto->disable_all(proto); |
| 208 | } |
| 209 | return err; |
| 210 | } |
| 211 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 212 | /************************************************************************/ |
| 213 | /* All supported ACL keywords must be declared here. */ |
| 214 | /************************************************************************/ |
| 215 | |
| 216 | /* set test->i to the number of connexions to the same listening socket */ |
| 217 | static int |
| 218 | acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir, |
| 219 | struct acl_expr *expr, struct acl_test *test) |
| 220 | { |
| 221 | test->i = l4->listener->nbconn; |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | /* set test->i to the id of the socket (listener) */ |
| 226 | static int |
| 227 | acl_fetch_so_id(struct proxy *px, struct session *l4, void *l7, int dir, |
| 228 | struct acl_expr *expr, struct acl_test *test) { |
| 229 | |
| 230 | test->flags = ACL_TEST_F_READ_ONLY; |
| 231 | |
| 232 | test->i = l4->listener->luid; |
| 233 | |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 238 | static struct acl_kw_list acl_kws = {{ },{ |
| 239 | { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING }, |
| 240 | { "so_id", acl_parse_int, acl_fetch_so_id, acl_match_int, ACL_USE_NOTHING }, |
| 241 | { NULL, NULL, NULL, NULL }, |
| 242 | }}; |
| 243 | |
| 244 | __attribute__((constructor)) |
| 245 | static void __protocols_init(void) |
| 246 | { |
| 247 | acl_register_keywords(&acl_kws); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Local variables: |
| 252 | * c-indent-level: 8 |
| 253 | * c-basic-offset: 8 |
| 254 | * End: |
| 255 | */ |