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