Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS: Locality-Based Least-Connection with Replication scheduler |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Wensong Zhang <wensong@gnuchina.org> |
| 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 | * Changes: |
| 12 | * Julian Anastasov : Added the missing (dest->weight>0) |
| 13 | * condition in the ip_vs_dest_set_max. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * The lblc/r algorithm is as follows (pseudo code): |
| 19 | * |
| 20 | * if serverSet[dest_ip] is null then |
| 21 | * n, serverSet[dest_ip] <- {weighted least-conn node}; |
| 22 | * else |
| 23 | * n <- {least-conn (alive) node in serverSet[dest_ip]}; |
| 24 | * if (n is null) OR |
| 25 | * (n.conns>n.weight AND |
| 26 | * there is a node m with m.conns<m.weight/2) then |
| 27 | * n <- {weighted least-conn node}; |
| 28 | * add n to serverSet[dest_ip]; |
| 29 | * if |serverSet[dest_ip]| > 1 AND |
| 30 | * now - serverSet[dest_ip].lastMod > T then |
| 31 | * m <- {most conn node in serverSet[dest_ip]}; |
| 32 | * remove m from serverSet[dest_ip]; |
| 33 | * if serverSet[dest_ip] changed then |
| 34 | * serverSet[dest_ip].lastMod <- now; |
| 35 | * |
| 36 | * return n; |
| 37 | * |
| 38 | */ |
| 39 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 40 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> |
| 42 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 43 | #include <linux/skbuff.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 44 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* for sysctl */ |
| 47 | #include <linux/fs.h> |
| 48 | #include <linux/sysctl.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 49 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include <net/ip_vs.h> |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | * It is for garbage collection of stale IPVS lblcr entries, |
| 56 | * when the table is full. |
| 57 | */ |
| 58 | #define CHECK_EXPIRE_INTERVAL (60*HZ) |
| 59 | #define ENTRY_TIMEOUT (6*60*HZ) |
| 60 | |
| 61 | /* |
| 62 | * It is for full expiration check. |
| 63 | * When there is no partial expiration check (garbage collection) |
| 64 | * in a half hour, do a full expiration check to collect stale |
| 65 | * entries that haven't been touched for a day. |
| 66 | */ |
| 67 | #define COUNT_FOR_FULL_EXPIRATION 30 |
| 68 | static int sysctl_ip_vs_lblcr_expiration = 24*60*60*HZ; |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * for IPVS lblcr entry hash table |
| 73 | */ |
| 74 | #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS |
| 75 | #define CONFIG_IP_VS_LBLCR_TAB_BITS 10 |
| 76 | #endif |
| 77 | #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS |
| 78 | #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS) |
| 79 | #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1) |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * IPVS destination set structure and operations |
| 84 | */ |
| 85 | struct ip_vs_dest_list { |
| 86 | struct ip_vs_dest_list *next; /* list link */ |
| 87 | struct ip_vs_dest *dest; /* destination server */ |
| 88 | }; |
| 89 | |
| 90 | struct ip_vs_dest_set { |
| 91 | atomic_t size; /* set size */ |
| 92 | unsigned long lastmod; /* last modified time */ |
| 93 | struct ip_vs_dest_list *list; /* destination list */ |
| 94 | rwlock_t lock; /* lock for this list */ |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | static struct ip_vs_dest_list * |
| 99 | ip_vs_dest_set_insert(struct ip_vs_dest_set *set, struct ip_vs_dest *dest) |
| 100 | { |
| 101 | struct ip_vs_dest_list *e; |
| 102 | |
| 103 | for (e=set->list; e!=NULL; e=e->next) { |
| 104 | if (e->dest == dest) |
| 105 | /* already existed */ |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 109 | e = kmalloc(sizeof(*e), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (e == NULL) { |
| 111 | IP_VS_ERR("ip_vs_dest_set_insert(): no memory\n"); |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | atomic_inc(&dest->refcnt); |
| 116 | e->dest = dest; |
| 117 | |
| 118 | /* link it to the list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | e->next = set->list; |
| 120 | set->list = e; |
| 121 | atomic_inc(&set->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | set->lastmod = jiffies; |
| 124 | return e; |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest) |
| 129 | { |
| 130 | struct ip_vs_dest_list *e, **ep; |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | for (ep=&set->list, e=*ep; e!=NULL; e=*ep) { |
| 133 | if (e->dest == dest) { |
| 134 | /* HIT */ |
| 135 | *ep = e->next; |
| 136 | atomic_dec(&set->size); |
| 137 | set->lastmod = jiffies; |
| 138 | atomic_dec(&e->dest->refcnt); |
| 139 | kfree(e); |
| 140 | break; |
| 141 | } |
| 142 | ep = &e->next; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set) |
| 147 | { |
| 148 | struct ip_vs_dest_list *e, **ep; |
| 149 | |
| 150 | write_lock(&set->lock); |
| 151 | for (ep=&set->list, e=*ep; e!=NULL; e=*ep) { |
| 152 | *ep = e->next; |
| 153 | /* |
| 154 | * We don't kfree dest because it is refered either |
| 155 | * by its service or by the trash dest list. |
| 156 | */ |
| 157 | atomic_dec(&e->dest->refcnt); |
| 158 | kfree(e); |
| 159 | } |
| 160 | write_unlock(&set->lock); |
| 161 | } |
| 162 | |
| 163 | /* get weighted least-connection node in the destination set */ |
| 164 | static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set) |
| 165 | { |
| 166 | register struct ip_vs_dest_list *e; |
| 167 | struct ip_vs_dest *dest, *least; |
| 168 | int loh, doh; |
| 169 | |
| 170 | if (set == NULL) |
| 171 | return NULL; |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | /* select the first destination server, whose weight > 0 */ |
| 174 | for (e=set->list; e!=NULL; e=e->next) { |
| 175 | least = e->dest; |
| 176 | if (least->flags & IP_VS_DEST_F_OVERLOAD) |
| 177 | continue; |
| 178 | |
| 179 | if ((atomic_read(&least->weight) > 0) |
| 180 | && (least->flags & IP_VS_DEST_F_AVAILABLE)) { |
| 181 | loh = atomic_read(&least->activeconns) * 50 |
| 182 | + atomic_read(&least->inactconns); |
| 183 | goto nextstage; |
| 184 | } |
| 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return NULL; |
| 187 | |
| 188 | /* find the destination with the weighted least load */ |
| 189 | nextstage: |
| 190 | for (e=e->next; e!=NULL; e=e->next) { |
| 191 | dest = e->dest; |
| 192 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) |
| 193 | continue; |
| 194 | |
| 195 | doh = atomic_read(&dest->activeconns) * 50 |
| 196 | + atomic_read(&dest->inactconns); |
| 197 | if ((loh * atomic_read(&dest->weight) > |
| 198 | doh * atomic_read(&least->weight)) |
| 199 | && (dest->flags & IP_VS_DEST_F_AVAILABLE)) { |
| 200 | least = dest; |
| 201 | loh = doh; |
| 202 | } |
| 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 205 | IP_VS_DBG_BUF(6, "ip_vs_dest_set_min: server %s:%d " |
| 206 | "activeconns %d refcnt %d weight %d overhead %d\n", |
| 207 | IP_VS_DBG_ADDR(least->af, &least->addr), |
| 208 | ntohs(least->port), |
| 209 | atomic_read(&least->activeconns), |
| 210 | atomic_read(&least->refcnt), |
| 211 | atomic_read(&least->weight), loh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return least; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* get weighted most-connection node in the destination set */ |
| 217 | static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set) |
| 218 | { |
| 219 | register struct ip_vs_dest_list *e; |
| 220 | struct ip_vs_dest *dest, *most; |
| 221 | int moh, doh; |
| 222 | |
| 223 | if (set == NULL) |
| 224 | return NULL; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* select the first destination server, whose weight > 0 */ |
| 227 | for (e=set->list; e!=NULL; e=e->next) { |
| 228 | most = e->dest; |
| 229 | if (atomic_read(&most->weight) > 0) { |
| 230 | moh = atomic_read(&most->activeconns) * 50 |
| 231 | + atomic_read(&most->inactconns); |
| 232 | goto nextstage; |
| 233 | } |
| 234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return NULL; |
| 236 | |
| 237 | /* find the destination with the weighted most load */ |
| 238 | nextstage: |
| 239 | for (e=e->next; e!=NULL; e=e->next) { |
| 240 | dest = e->dest; |
| 241 | doh = atomic_read(&dest->activeconns) * 50 |
| 242 | + atomic_read(&dest->inactconns); |
| 243 | /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */ |
| 244 | if ((moh * atomic_read(&dest->weight) < |
| 245 | doh * atomic_read(&most->weight)) |
| 246 | && (atomic_read(&dest->weight) > 0)) { |
| 247 | most = dest; |
| 248 | moh = doh; |
| 249 | } |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 252 | IP_VS_DBG_BUF(6, "ip_vs_dest_set_max: server %s:%d " |
| 253 | "activeconns %d refcnt %d weight %d overhead %d\n", |
| 254 | IP_VS_DBG_ADDR(most->af, &most->addr), ntohs(most->port), |
| 255 | atomic_read(&most->activeconns), |
| 256 | atomic_read(&most->refcnt), |
| 257 | atomic_read(&most->weight), moh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | return most; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * IPVS lblcr entry represents an association between destination |
| 264 | * IP address and its destination server set |
| 265 | */ |
| 266 | struct ip_vs_lblcr_entry { |
| 267 | struct list_head list; |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 268 | int af; /* address family */ |
| 269 | union nf_inet_addr addr; /* destination IP address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | struct ip_vs_dest_set set; /* destination server set */ |
| 271 | unsigned long lastuse; /* last used time */ |
| 272 | }; |
| 273 | |
| 274 | |
| 275 | /* |
| 276 | * IPVS lblcr hash table |
| 277 | */ |
| 278 | struct ip_vs_lblcr_table { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | struct list_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */ |
| 280 | atomic_t entries; /* number of entries */ |
| 281 | int max_size; /* maximum size of entries */ |
| 282 | struct timer_list periodic_timer; /* collect stale entries */ |
| 283 | int rover; /* rover for expire check */ |
| 284 | int counter; /* counter for no expire */ |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | /* |
| 289 | * IPVS LBLCR sysctl table |
| 290 | */ |
| 291 | |
| 292 | static ctl_table vs_vars_table[] = { |
| 293 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | .procname = "lblcr_expiration", |
| 295 | .data = &sysctl_ip_vs_lblcr_expiration, |
| 296 | .maxlen = sizeof(int), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 297 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 298 | .proc_handler = proc_dointvec_jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | }, |
| 300 | { .ctl_name = 0 } |
| 301 | }; |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static struct ctl_table_header * sysctl_header; |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en) |
| 306 | { |
| 307 | list_del(&en->list); |
| 308 | ip_vs_dest_set_eraseall(&en->set); |
| 309 | kfree(en); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /* |
| 314 | * Returns hash value for IPVS LBLCR entry |
| 315 | */ |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 316 | static inline unsigned |
| 317 | ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 319 | __be32 addr_fold = addr->ip; |
| 320 | |
| 321 | #ifdef CONFIG_IP_VS_IPV6 |
| 322 | if (af == AF_INET6) |
| 323 | addr_fold = addr->ip6[0]^addr->ip6[1]^ |
| 324 | addr->ip6[2]^addr->ip6[3]; |
| 325 | #endif |
| 326 | return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLCR_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | /* |
| 331 | * Hash an entry in the ip_vs_lblcr_table. |
| 332 | * returns bool success. |
| 333 | */ |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 334 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en) |
| 336 | { |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 337 | unsigned hash = ip_vs_lblcr_hashkey(en->af, &en->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | list_add(&en->list, &tbl->bucket[hash]); |
| 340 | atomic_inc(&tbl->entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | /* |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 345 | * Get ip_vs_lblcr_entry associated with supplied parameters. Called under |
| 346 | * read lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | */ |
| 348 | static inline struct ip_vs_lblcr_entry * |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 349 | ip_vs_lblcr_get(int af, struct ip_vs_lblcr_table *tbl, |
| 350 | const union nf_inet_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 352 | unsigned hash = ip_vs_lblcr_hashkey(af, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | struct ip_vs_lblcr_entry *en; |
| 354 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 355 | list_for_each_entry(en, &tbl->bucket[hash], list) |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 356 | if (ip_vs_addr_equal(af, &en->addr, addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return en; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | /* |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 364 | * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination |
| 365 | * IP address to a server. Called under write lock. |
| 366 | */ |
| 367 | static inline struct ip_vs_lblcr_entry * |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 368 | ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, const union nf_inet_addr *daddr, |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 369 | struct ip_vs_dest *dest) |
| 370 | { |
| 371 | struct ip_vs_lblcr_entry *en; |
| 372 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 373 | en = ip_vs_lblcr_get(dest->af, tbl, daddr); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 374 | if (!en) { |
| 375 | en = kmalloc(sizeof(*en), GFP_ATOMIC); |
| 376 | if (!en) { |
| 377 | IP_VS_ERR("ip_vs_lblcr_new(): no memory\n"); |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 381 | en->af = dest->af; |
| 382 | ip_vs_addr_copy(dest->af, &en->addr, daddr); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 383 | en->lastuse = jiffies; |
| 384 | |
| 385 | /* initilize its dest set */ |
| 386 | atomic_set(&(en->set.size), 0); |
| 387 | en->set.list = NULL; |
| 388 | rwlock_init(&en->set.lock); |
| 389 | |
| 390 | ip_vs_lblcr_hash(tbl, en); |
| 391 | } |
| 392 | |
| 393 | write_lock(&en->set.lock); |
| 394 | ip_vs_dest_set_insert(&en->set, dest); |
| 395 | write_unlock(&en->set.lock); |
| 396 | |
| 397 | return en; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | * Flush all the entries of the specified table. |
| 403 | */ |
| 404 | static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl) |
| 405 | { |
| 406 | int i; |
| 407 | struct ip_vs_lblcr_entry *en, *nxt; |
| 408 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 409 | /* No locking required, only called during cleanup. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) { |
| 412 | ip_vs_lblcr_free(en); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 418 | static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | { |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 420 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | unsigned long now = jiffies; |
| 422 | int i, j; |
| 423 | struct ip_vs_lblcr_entry *en, *nxt; |
| 424 | |
| 425 | for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) { |
| 426 | j = (j + 1) & IP_VS_LBLCR_TAB_MASK; |
| 427 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 428 | write_lock(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) { |
| 430 | if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration, |
| 431 | now)) |
| 432 | continue; |
| 433 | |
| 434 | ip_vs_lblcr_free(en); |
| 435 | atomic_dec(&tbl->entries); |
| 436 | } |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 437 | write_unlock(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | tbl->rover = j; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /* |
| 444 | * Periodical timer handler for IPVS lblcr table |
| 445 | * It is used to collect stale entries when the number of entries |
| 446 | * exceeds the maximum size of the table. |
| 447 | * |
| 448 | * Fixme: we probably need more complicated algorithm to collect |
| 449 | * entries that have not been used for a long time even |
| 450 | * if the number of entries doesn't exceed the maximum size |
| 451 | * of the table. |
| 452 | * The full expiration check is for this purpose now. |
| 453 | */ |
| 454 | static void ip_vs_lblcr_check_expire(unsigned long data) |
| 455 | { |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 456 | struct ip_vs_service *svc = (struct ip_vs_service *) data; |
| 457 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | unsigned long now = jiffies; |
| 459 | int goal; |
| 460 | int i, j; |
| 461 | struct ip_vs_lblcr_entry *en, *nxt; |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) { |
| 464 | /* do full expiration check */ |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 465 | ip_vs_lblcr_full_check(svc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | tbl->counter = 1; |
| 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | if (atomic_read(&tbl->entries) <= tbl->max_size) { |
| 471 | tbl->counter++; |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3; |
| 476 | if (goal > tbl->max_size/2) |
| 477 | goal = tbl->max_size/2; |
| 478 | |
| 479 | for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) { |
| 480 | j = (j + 1) & IP_VS_LBLCR_TAB_MASK; |
| 481 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 482 | write_lock(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) { |
| 484 | if (time_before(now, en->lastuse+ENTRY_TIMEOUT)) |
| 485 | continue; |
| 486 | |
| 487 | ip_vs_lblcr_free(en); |
| 488 | atomic_dec(&tbl->entries); |
| 489 | goal--; |
| 490 | } |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 491 | write_unlock(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | if (goal <= 0) |
| 493 | break; |
| 494 | } |
| 495 | tbl->rover = j; |
| 496 | |
| 497 | out: |
| 498 | mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL); |
| 499 | } |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc) |
| 502 | { |
| 503 | int i; |
| 504 | struct ip_vs_lblcr_table *tbl; |
| 505 | |
| 506 | /* |
| 507 | * Allocate the ip_vs_lblcr_table for this service |
| 508 | */ |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 509 | tbl = kmalloc(sizeof(*tbl), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (tbl == NULL) { |
| 511 | IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n"); |
| 512 | return -ENOMEM; |
| 513 | } |
| 514 | svc->sched_data = tbl; |
| 515 | IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for " |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 516 | "current service\n", sizeof(*tbl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Initialize the hash buckets |
| 520 | */ |
| 521 | for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) { |
| 522 | INIT_LIST_HEAD(&tbl->bucket[i]); |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16; |
| 525 | tbl->rover = 0; |
| 526 | tbl->counter = 1; |
| 527 | |
| 528 | /* |
| 529 | * Hook periodic timer for garbage collection |
| 530 | */ |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 531 | setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire, |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 532 | (unsigned long)svc); |
| 533 | mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc) |
| 540 | { |
| 541 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
| 542 | |
| 543 | /* remove periodic timer */ |
| 544 | del_timer_sync(&tbl->periodic_timer); |
| 545 | |
| 546 | /* got to clean up table entries here */ |
| 547 | ip_vs_lblcr_flush(tbl); |
| 548 | |
| 549 | /* release the table itself */ |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 550 | kfree(tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n", |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 552 | sizeof(*tbl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | static inline struct ip_vs_dest * |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 559 | __ip_vs_lblcr_schedule(struct ip_vs_service *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
| 561 | struct ip_vs_dest *dest, *least; |
| 562 | int loh, doh; |
| 563 | |
| 564 | /* |
| 565 | * We think the overhead of processing active connections is fifty |
| 566 | * times higher than that of inactive connections in average. (This |
| 567 | * fifty times might not be accurate, we will change it later.) We |
| 568 | * use the following formula to estimate the overhead: |
| 569 | * dest->activeconns*50 + dest->inactconns |
| 570 | * and the load: |
| 571 | * (dest overhead) / dest->weight |
| 572 | * |
| 573 | * Remember -- no floats in kernel mode!!! |
| 574 | * The comparison of h1*w2 > h2*w1 is equivalent to that of |
| 575 | * h1/w1 > h2/w2 |
| 576 | * if every weight is larger than zero. |
| 577 | * |
| 578 | * The server with weight=0 is quiesced and will not receive any |
| 579 | * new connection. |
| 580 | */ |
| 581 | list_for_each_entry(dest, &svc->destinations, n_list) { |
| 582 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) |
| 583 | continue; |
| 584 | |
| 585 | if (atomic_read(&dest->weight) > 0) { |
| 586 | least = dest; |
| 587 | loh = atomic_read(&least->activeconns) * 50 |
| 588 | + atomic_read(&least->inactconns); |
| 589 | goto nextstage; |
| 590 | } |
| 591 | } |
| 592 | return NULL; |
| 593 | |
| 594 | /* |
| 595 | * Find the destination with the least load. |
| 596 | */ |
| 597 | nextstage: |
| 598 | list_for_each_entry_continue(dest, &svc->destinations, n_list) { |
| 599 | if (dest->flags & IP_VS_DEST_F_OVERLOAD) |
| 600 | continue; |
| 601 | |
| 602 | doh = atomic_read(&dest->activeconns) * 50 |
| 603 | + atomic_read(&dest->inactconns); |
| 604 | if (loh * atomic_read(&dest->weight) > |
| 605 | doh * atomic_read(&least->weight)) { |
| 606 | least = dest; |
| 607 | loh = doh; |
| 608 | } |
| 609 | } |
| 610 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 611 | IP_VS_DBG_BUF(6, "LBLCR: server %s:%d " |
| 612 | "activeconns %d refcnt %d weight %d overhead %d\n", |
| 613 | IP_VS_DBG_ADDR(least->af, &least->addr), |
| 614 | ntohs(least->port), |
| 615 | atomic_read(&least->activeconns), |
| 616 | atomic_read(&least->refcnt), |
| 617 | atomic_read(&least->weight), loh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | return least; |
| 620 | } |
| 621 | |
| 622 | |
| 623 | /* |
| 624 | * If this destination server is overloaded and there is a less loaded |
| 625 | * server, then return true. |
| 626 | */ |
| 627 | static inline int |
| 628 | is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc) |
| 629 | { |
| 630 | if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) { |
| 631 | struct ip_vs_dest *d; |
| 632 | |
| 633 | list_for_each_entry(d, &svc->destinations, n_list) { |
| 634 | if (atomic_read(&d->activeconns)*2 |
| 635 | < atomic_read(&d->weight)) { |
| 636 | return 1; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /* |
| 645 | * Locality-Based (weighted) Least-Connection scheduling |
| 646 | */ |
| 647 | static struct ip_vs_dest * |
| 648 | ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb) |
| 649 | { |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 650 | struct ip_vs_lblcr_table *tbl = svc->sched_data; |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 651 | struct ip_vs_iphdr iph; |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 652 | struct ip_vs_dest *dest = NULL; |
| 653 | struct ip_vs_lblcr_entry *en; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 655 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n"); |
| 658 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 659 | /* First look in our cache */ |
| 660 | read_lock(&svc->sched_lock); |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 661 | en = ip_vs_lblcr_get(svc->af, tbl, &iph.daddr); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 662 | if (en) { |
| 663 | /* We only hold a read lock, but this is atomic */ |
| 664 | en->lastuse = jiffies; |
| 665 | |
| 666 | /* Get the least loaded destination */ |
| 667 | read_lock(&en->set.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | dest = ip_vs_dest_set_min(&en->set); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 669 | read_unlock(&en->set.lock); |
| 670 | |
| 671 | /* More than one destination + enough time passed by, cleanup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | if (atomic_read(&en->set.size) > 1 && |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 673 | time_after(jiffies, en->set.lastmod + |
| 674 | sysctl_ip_vs_lblcr_expiration)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | struct ip_vs_dest *m; |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 676 | |
| 677 | write_lock(&en->set.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | m = ip_vs_dest_set_max(&en->set); |
| 679 | if (m) |
| 680 | ip_vs_dest_set_erase(&en->set, m); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 681 | write_unlock(&en->set.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 684 | /* If the destination is not overloaded, use it */ |
| 685 | if (dest && !is_overloaded(dest, svc)) { |
| 686 | read_unlock(&svc->sched_lock); |
| 687 | goto out; |
| 688 | } |
| 689 | |
| 690 | /* The cache entry is invalid, time to schedule */ |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 691 | dest = __ip_vs_lblcr_schedule(svc); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 692 | if (!dest) { |
| 693 | IP_VS_DBG(1, "no destination available\n"); |
| 694 | read_unlock(&svc->sched_lock); |
| 695 | return NULL; |
| 696 | } |
| 697 | |
| 698 | /* Update our cache entry */ |
| 699 | write_lock(&en->set.lock); |
| 700 | ip_vs_dest_set_insert(&en->set, dest); |
| 701 | write_unlock(&en->set.lock); |
| 702 | } |
| 703 | read_unlock(&svc->sched_lock); |
| 704 | |
| 705 | if (dest) |
| 706 | goto out; |
| 707 | |
| 708 | /* No cache entry, time to schedule */ |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 709 | dest = __ip_vs_lblcr_schedule(svc); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 710 | if (!dest) { |
| 711 | IP_VS_DBG(1, "no destination available\n"); |
| 712 | return NULL; |
| 713 | } |
| 714 | |
| 715 | /* If we fail to create a cache entry, we'll just use the valid dest */ |
| 716 | write_lock(&svc->sched_lock); |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 717 | ip_vs_lblcr_new(tbl, &iph.daddr, dest); |
Sven Wegener | f728baf | 2008-08-19 08:16:19 +0200 | [diff] [blame] | 718 | write_unlock(&svc->sched_lock); |
| 719 | |
| 720 | out: |
Julius Volz | 4454837 | 2008-11-03 17:08:28 -0800 | [diff] [blame] | 721 | IP_VS_DBG_BUF(6, "LBLCR: destination IP address %s --> server %s:%d\n", |
| 722 | IP_VS_DBG_ADDR(svc->af, &iph.daddr), |
| 723 | IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | return dest; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /* |
| 730 | * IPVS LBLCR Scheduler structure |
| 731 | */ |
| 732 | static struct ip_vs_scheduler ip_vs_lblcr_scheduler = |
| 733 | { |
| 734 | .name = "lblcr", |
| 735 | .refcnt = ATOMIC_INIT(0), |
| 736 | .module = THIS_MODULE, |
Sven Wegener | d149ccc | 2008-08-10 09:18:02 +0000 | [diff] [blame] | 737 | .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | .init_service = ip_vs_lblcr_init_svc, |
| 739 | .done_service = ip_vs_lblcr_done_svc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | .schedule = ip_vs_lblcr_schedule, |
| 741 | }; |
| 742 | |
| 743 | |
| 744 | static int __init ip_vs_lblcr_init(void) |
| 745 | { |
Pavel Emelyanov | a014bc8 | 2007-12-04 00:43:24 -0800 | [diff] [blame] | 746 | int ret; |
| 747 | |
Pavel Emelyanov | 90754f8 | 2008-01-12 02:33:50 -0800 | [diff] [blame] | 748 | sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table); |
Pavel Emelyanov | a014bc8 | 2007-12-04 00:43:24 -0800 | [diff] [blame] | 749 | ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler); |
| 750 | if (ret) |
| 751 | unregister_sysctl_table(sysctl_header); |
| 752 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | |
| 756 | static void __exit ip_vs_lblcr_cleanup(void) |
| 757 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | unregister_sysctl_table(sysctl_header); |
| 759 | unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | module_init(ip_vs_lblcr_init); |
| 764 | module_exit(ip_vs_lblcr_cleanup); |
| 765 | MODULE_LICENSE("GPL"); |