Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS: Source Hashing scheduling module |
| 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 | * |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * The sh algorithm is to select server by the hash key of source IP |
| 17 | * address. The pseudo code is as follows: |
| 18 | * |
| 19 | * n <- servernode[src_ip]; |
| 20 | * if (n is dead) OR |
| 21 | * (n is overloaded) or (n.weight <= 0) then |
| 22 | * return NULL; |
| 23 | * |
| 24 | * return n; |
| 25 | * |
| 26 | * Notes that servernode is a 256-bucket hash table that maps the hash |
| 27 | * index derived from packet source IP address to the current server |
| 28 | * array. If the sh scheduler is used in cache cluster, it is good to |
| 29 | * combine it with cache_bypass feature. When the statically assigned |
| 30 | * server is dead or overloaded, the load balancer can bypass the cache |
| 31 | * server and send requests to the original server directly. |
| 32 | * |
| 33 | */ |
| 34 | |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 35 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 38 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <net/ip_vs.h> |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * IPVS SH bucket |
| 45 | */ |
| 46 | struct ip_vs_sh_bucket { |
| 47 | struct ip_vs_dest *dest; /* real server (cache) */ |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * for IPVS SH entry hash table |
| 52 | */ |
| 53 | #ifndef CONFIG_IP_VS_SH_TAB_BITS |
| 54 | #define CONFIG_IP_VS_SH_TAB_BITS 8 |
| 55 | #endif |
| 56 | #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS |
| 57 | #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS) |
| 58 | #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1) |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | * Returns hash value for IPVS SH entry |
| 63 | */ |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 64 | static inline unsigned ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 66 | __be32 addr_fold = addr->ip; |
| 67 | |
| 68 | #ifdef CONFIG_IP_VS_IPV6 |
| 69 | if (af == AF_INET6) |
| 70 | addr_fold = addr->ip6[0]^addr->ip6[1]^ |
| 71 | addr->ip6[2]^addr->ip6[3]; |
| 72 | #endif |
| 73 | return (ntohl(addr_fold)*2654435761UL) & IP_VS_SH_TAB_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Get ip_vs_dest associated with supplied parameters. |
| 79 | */ |
| 80 | static inline struct ip_vs_dest * |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 81 | ip_vs_sh_get(int af, struct ip_vs_sh_bucket *tbl, |
| 82 | const union nf_inet_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 84 | return (tbl[ip_vs_sh_hashkey(af, addr)]).dest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * Assign all the hash buckets of the specified table with the service. |
| 90 | */ |
| 91 | static int |
| 92 | ip_vs_sh_assign(struct ip_vs_sh_bucket *tbl, struct ip_vs_service *svc) |
| 93 | { |
| 94 | int i; |
| 95 | struct ip_vs_sh_bucket *b; |
| 96 | struct list_head *p; |
| 97 | struct ip_vs_dest *dest; |
| 98 | |
| 99 | b = tbl; |
| 100 | p = &svc->destinations; |
| 101 | for (i=0; i<IP_VS_SH_TAB_SIZE; i++) { |
| 102 | if (list_empty(p)) { |
| 103 | b->dest = NULL; |
| 104 | } else { |
| 105 | if (p == &svc->destinations) |
| 106 | p = p->next; |
| 107 | |
| 108 | dest = list_entry(p, struct ip_vs_dest, n_list); |
| 109 | atomic_inc(&dest->refcnt); |
| 110 | b->dest = dest; |
| 111 | |
| 112 | p = p->next; |
| 113 | } |
| 114 | b++; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | * Flush all the hash buckets of the specified table. |
| 122 | */ |
| 123 | static void ip_vs_sh_flush(struct ip_vs_sh_bucket *tbl) |
| 124 | { |
| 125 | int i; |
| 126 | struct ip_vs_sh_bucket *b; |
| 127 | |
| 128 | b = tbl; |
| 129 | for (i=0; i<IP_VS_SH_TAB_SIZE; i++) { |
| 130 | if (b->dest) { |
| 131 | atomic_dec(&b->dest->refcnt); |
| 132 | b->dest = NULL; |
| 133 | } |
| 134 | b++; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static int ip_vs_sh_init_svc(struct ip_vs_service *svc) |
| 140 | { |
| 141 | struct ip_vs_sh_bucket *tbl; |
| 142 | |
| 143 | /* allocate the SH table for this service */ |
| 144 | tbl = kmalloc(sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE, |
| 145 | GFP_ATOMIC); |
| 146 | if (tbl == NULL) { |
| 147 | IP_VS_ERR("ip_vs_sh_init_svc(): no memory\n"); |
| 148 | return -ENOMEM; |
| 149 | } |
| 150 | svc->sched_data = tbl; |
| 151 | IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for " |
| 152 | "current service\n", |
| 153 | sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE); |
| 154 | |
| 155 | /* assign the hash buckets with the updated service */ |
| 156 | ip_vs_sh_assign(tbl, svc); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static int ip_vs_sh_done_svc(struct ip_vs_service *svc) |
| 163 | { |
| 164 | struct ip_vs_sh_bucket *tbl = svc->sched_data; |
| 165 | |
| 166 | /* got to clean up hash buckets here */ |
| 167 | ip_vs_sh_flush(tbl); |
| 168 | |
| 169 | /* release the table itself */ |
| 170 | kfree(svc->sched_data); |
| 171 | IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n", |
| 172 | sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | static int ip_vs_sh_update_svc(struct ip_vs_service *svc) |
| 179 | { |
| 180 | struct ip_vs_sh_bucket *tbl = svc->sched_data; |
| 181 | |
| 182 | /* got to clean up hash buckets here */ |
| 183 | ip_vs_sh_flush(tbl); |
| 184 | |
| 185 | /* assign the hash buckets with the updated service */ |
| 186 | ip_vs_sh_assign(tbl, svc); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /* |
| 193 | * If the dest flags is set with IP_VS_DEST_F_OVERLOAD, |
| 194 | * consider that the server is overloaded here. |
| 195 | */ |
| 196 | static inline int is_overloaded(struct ip_vs_dest *dest) |
| 197 | { |
| 198 | return dest->flags & IP_VS_DEST_F_OVERLOAD; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* |
| 203 | * Source Hashing scheduling |
| 204 | */ |
| 205 | static struct ip_vs_dest * |
| 206 | ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb) |
| 207 | { |
| 208 | struct ip_vs_dest *dest; |
| 209 | struct ip_vs_sh_bucket *tbl; |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 210 | struct ip_vs_iphdr iph; |
| 211 | |
| 212 | ip_vs_fill_iphdr(svc->af, skb_network_header(skb), &iph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n"); |
| 215 | |
| 216 | tbl = (struct ip_vs_sh_bucket *)svc->sched_data; |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 217 | dest = ip_vs_sh_get(svc->af, tbl, &iph.saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (!dest |
| 219 | || !(dest->flags & IP_VS_DEST_F_AVAILABLE) |
| 220 | || atomic_read(&dest->weight) <= 0 |
| 221 | || is_overloaded(dest)) { |
Simon Horman | 68888d1 | 2008-12-29 18:37:36 -0800 | [diff] [blame^] | 222 | IP_VS_ERR_RL("SH: no destination available\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return NULL; |
| 224 | } |
| 225 | |
Julius Volz | 20971a0 | 2008-11-01 13:13:19 +0000 | [diff] [blame] | 226 | IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n", |
| 227 | IP_VS_DBG_ADDR(svc->af, &iph.saddr), |
| 228 | IP_VS_DBG_ADDR(svc->af, &dest->addr), |
| 229 | ntohs(dest->port)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | return dest; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* |
| 236 | * IPVS SH Scheduler structure |
| 237 | */ |
| 238 | static struct ip_vs_scheduler ip_vs_sh_scheduler = |
| 239 | { |
| 240 | .name = "sh", |
| 241 | .refcnt = ATOMIC_INIT(0), |
| 242 | .module = THIS_MODULE, |
Sven Wegener | d149ccc | 2008-08-10 09:18:02 +0000 | [diff] [blame] | 243 | .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | .init_service = ip_vs_sh_init_svc, |
| 245 | .done_service = ip_vs_sh_done_svc, |
| 246 | .update_service = ip_vs_sh_update_svc, |
| 247 | .schedule = ip_vs_sh_schedule, |
| 248 | }; |
| 249 | |
| 250 | |
| 251 | static int __init ip_vs_sh_init(void) |
| 252 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return register_ip_vs_scheduler(&ip_vs_sh_scheduler); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static void __exit ip_vs_sh_cleanup(void) |
| 258 | { |
| 259 | unregister_ip_vs_scheduler(&ip_vs_sh_scheduler); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | module_init(ip_vs_sh_init); |
| 264 | module_exit(ip_vs_sh_cleanup); |
| 265 | MODULE_LICENSE("GPL"); |