FILE
ip_vs_sh.c
File preview with syntax highlighting.
linux-6.19.14/net/netfilter/ipvs/ip_vs_sh.c
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * IPVS: Source Hashing scheduling module 4 * 5 * Authors: Wensong Zhang <wensong@gnuchina.org> 6 * 7 * Changes: 8 */ 9 10/* 11 * The sh algorithm is to select server by the hash key of source IP 12 * address. The pseudo code is as follows: 13 * 14 * n <- servernode[src_ip]; 15 * if (n is dead) OR 16 * (n is overloaded) or (n.weight <= 0) then 17 * return NULL; 18 * 19 * return n; 20 * 21 * Notes that servernode is a 256-bucket hash table that maps the hash 22 * index derived from packet source IP address to the current server 23 * array. If the sh scheduler is used in cache cluster, it is good to 24 * combine it with cache_bypass feature. When the statically assigned 25 * server is dead or overloaded, the load balancer can bypass the cache 26 * server and send requests to the original server directly. 27 * 28 * The weight destination attribute can be used to control the 29 * distribution of connections to the destinations in servernode. The 30 * greater the weight, the more connections the destination 31 * will receive. 32 * 33 */ 34 35#define pr_fmt(fmt) "IPVS: " fmt 36 37#include <linux/ip.h> 38#include <linux/slab.h> 39#include <linux/module.h> 40#include <linux/kernel.h> 41#include <linux/skbuff.h> 42 43#include <net/ip_vs.h> 44 45#include <net/tcp.h> 46#include <linux/udp.h> 47#include <linux/sctp.h> 48 49 50/* 51 * IPVS SH bucket 52 */ 53struct ip_vs_sh_bucket { 54 struct ip_vs_dest __rcu *dest; /* real server (cache) */ 55}; 56 57/* 58 * for IPVS SH entry hash table 59 */ 60#ifndef CONFIG_IP_VS_SH_TAB_BITS 61#define CONFIG_IP_VS_SH_TAB_BITS 8 62#endif 63#define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS 64#define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS) 65#define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1) 66 67struct ip_vs_sh_state { 68 struct rcu_head rcu_head; 69 struct ip_vs_sh_bucket buckets[IP_VS_SH_TAB_SIZE]; 70}; 71 72/* Helper function to determine if server is unavailable */ 73static inline bool is_unavailable(struct ip_vs_dest *dest) 74{ 75 return atomic_read(&dest->weight) <= 0 || 76 dest->flags & IP_VS_DEST_F_OVERLOAD; 77} 78 79/* 80 * Returns hash value for IPVS SH entry 81 */ 82static inline unsigned int 83ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr, 84 __be16 port, unsigned int offset) 85{ 86 __be32 addr_fold = addr->ip; 87 88#ifdef CONFIG_IP_VS_IPV6 89 if (af == AF_INET6) 90 addr_fold = addr->ip6[0]^addr->ip6[1]^ 91 addr->ip6[2]^addr->ip6[3]; 92#endif 93 return (offset + hash_32(ntohs(port) + ntohl(addr_fold), 94 IP_VS_SH_TAB_BITS)) & 95 IP_VS_SH_TAB_MASK; 96} 97 98 99/* 100 * Get ip_vs_dest associated with supplied parameters. 101 */ 102static inline struct ip_vs_dest * 103ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s, 104 const union nf_inet_addr *addr, __be16 port) 105{ 106 unsigned int hash = ip_vs_sh_hashkey(svc->af, addr, port, 0); 107 struct ip_vs_dest *dest = rcu_dereference(s->buckets[hash].dest); 108 109 return (!dest || is_unavailable(dest)) ? NULL : dest; 110} 111 112 113/* As ip_vs_sh_get, but with fallback if selected server is unavailable 114 * 115 * The fallback strategy loops around the table starting from a "random" 116 * point (in fact, it is chosen to be the original hash value to make the 117 * algorithm deterministic) to find a new server. 118 */ 119static inline struct ip_vs_dest * 120ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s, 121 const union nf_inet_addr *addr, __be16 port) 122{ 123 unsigned int offset, roffset; 124 unsigned int hash, ihash; 125 struct ip_vs_dest *dest; 126 127 /* first try the dest it's supposed to go to */ 128 ihash = ip_vs_sh_hashkey(svc->af, addr, port, 0); 129 dest = rcu_dereference(s->buckets[ihash].dest); 130 if (!dest) 131 return NULL; 132 if (!is_unavailable(dest)) 133 return dest; 134 135 IP_VS_DBG_BUF(6, "SH: selected unavailable server %s:%d, reselecting", 136 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port)); 137 138 /* if the original dest is unavailable, loop around the table 139 * starting from ihash to find a new dest 140 */ 141 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) { 142 roffset = (offset + ihash) % IP_VS_SH_TAB_SIZE; 143 hash = ip_vs_sh_hashkey(svc->af, addr, port, roffset); 144 dest = rcu_dereference(s->buckets[hash].dest); 145 if (!dest) 146 break; 147 if (!is_unavailable(dest)) 148 return dest; 149 IP_VS_DBG_BUF(6, "SH: selected unavailable " 150 "server %s:%d (offset %d), reselecting", 151 IP_VS_DBG_ADDR(dest->af, &dest->addr), 152 ntohs(dest->port), roffset); 153 } 154 155 return NULL; 156} 157 158/* 159 * Assign all the hash buckets of the specified table with the service. 160 */ 161static int 162ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc) 163{ 164 int i; 165 struct ip_vs_sh_bucket *b; 166 struct list_head *p; 167 struct ip_vs_dest *dest; 168 int d_count; 169 bool empty; 170 171 b = &s->buckets[0]; 172 p = &svc->destinations; 173 empty = list_empty(p); 174 d_count = 0; 175 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) { 176 dest = rcu_dereference_protected(b->dest, 1); 177 if (dest) 178 ip_vs_dest_put(dest); 179 if (empty) 180 RCU_INIT_POINTER(b->dest, NULL); 181 else { 182 if (p == &svc->destinations) 183 p = p->next; 184 185 dest = list_entry(p, struct ip_vs_dest, n_list); 186 ip_vs_dest_hold(dest); 187 RCU_INIT_POINTER(b->dest, dest); 188 189 IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n", 190 i, IP_VS_DBG_ADDR(dest->af, &dest->addr), 191 atomic_read(&dest->weight)); 192 193 /* Don't move to next dest until filling weight */ 194 if (++d_count >= atomic_read(&dest->weight)) { 195 p = p->next; 196 d_count = 0; 197 } 198 199 } 200 b++; 201 } 202 return 0; 203} 204 205 206/* 207 * Flush all the hash buckets of the specified table. 208 */ 209static void ip_vs_sh_flush(struct ip_vs_sh_state *s) 210{ 211 int i; 212 struct ip_vs_sh_bucket *b; 213 struct ip_vs_dest *dest; 214 215 b = &s->buckets[0]; 216 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) { 217 dest = rcu_dereference_protected(b->dest, 1); 218 if (dest) { 219 ip_vs_dest_put(dest); 220 RCU_INIT_POINTER(b->dest, NULL); 221 } 222 b++; 223 } 224} 225 226 227static int ip_vs_sh_init_svc(struct ip_vs_service *svc) 228{ 229 struct ip_vs_sh_state *s; 230 231 /* allocate the SH table for this service */ 232 s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL); 233 if (s == NULL) 234 return -ENOMEM; 235 236 svc->sched_data = s; 237 IP_VS_DBG(6, "SH hash table (memory=%zdbytes) allocated for " 238 "current service\n", 239 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE); 240 241 /* assign the hash buckets with current dests */ 242 ip_vs_sh_reassign(s, svc); 243 244 return 0; 245} 246 247 248static void ip_vs_sh_done_svc(struct ip_vs_service *svc) 249{ 250 struct ip_vs_sh_state *s = svc->sched_data; 251 252 /* got to clean up hash buckets here */ 253 ip_vs_sh_flush(s); 254 255 /* release the table itself */ 256 kfree_rcu(s, rcu_head); 257 IP_VS_DBG(6, "SH hash table (memory=%zdbytes) released\n", 258 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE); 259} 260 261 262static int ip_vs_sh_dest_changed(struct ip_vs_service *svc, 263 struct ip_vs_dest *dest) 264{ 265 struct ip_vs_sh_state *s = svc->sched_data; 266 267 /* assign the hash buckets with the updated service */ 268 ip_vs_sh_reassign(s, svc); 269 270 return 0; 271} 272 273 274/* Helper function to get port number */ 275static inline __be16 276ip_vs_sh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph) 277{ 278 __be16 _ports[2], *ports; 279 280 /* At this point we know that we have a valid packet of some kind. 281 * Because ICMP packets are only guaranteed to have the first 8 282 * bytes, let's just grab the ports. Fortunately they're in the 283 * same position for all three of the protocols we care about. 284 */ 285 switch (iph->protocol) { 286 case IPPROTO_TCP: 287 case IPPROTO_UDP: 288 case IPPROTO_SCTP: 289 ports = skb_header_pointer(skb, iph->len, sizeof(_ports), 290 &_ports); 291 if (unlikely(!ports)) 292 return 0; 293 294 if (likely(!ip_vs_iph_inverse(iph))) 295 return ports[0]; 296 else 297 return ports[1]; 298 default: 299 return 0; 300 } 301} 302 303 304/* 305 * Source Hashing scheduling 306 */ 307static struct ip_vs_dest * 308ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb, 309 struct ip_vs_iphdr *iph) 310{ 311 struct ip_vs_dest *dest; 312 struct ip_vs_sh_state *s; 313 __be16 port = 0; 314 const union nf_inet_addr *hash_addr; 315 316 hash_addr = ip_vs_iph_inverse(iph) ? &iph->daddr : &iph->saddr; 317 318 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n"); 319 320 if (svc->flags & IP_VS_SVC_F_SCHED_SH_PORT) 321 port = ip_vs_sh_get_port(skb, iph); 322 323 s = (struct ip_vs_sh_state *) svc->sched_data; 324 325 if (svc->flags & IP_VS_SVC_F_SCHED_SH_FALLBACK) 326 dest = ip_vs_sh_get_fallback(svc, s, hash_addr, port); 327 else 328 dest = ip_vs_sh_get(svc, s, hash_addr, port); 329 330 if (!dest) { 331 ip_vs_scheduler_err(svc, "no destination available"); 332 return NULL; 333 } 334 335 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n", 336 IP_VS_DBG_ADDR(svc->af, hash_addr), 337 IP_VS_DBG_ADDR(dest->af, &dest->addr), 338 ntohs(dest->port)); 339 340 return dest; 341} 342 343 344/* 345 * IPVS SH Scheduler structure 346 */ 347static struct ip_vs_scheduler ip_vs_sh_scheduler = 348{ 349 .name = "sh", 350 .refcnt = ATOMIC_INIT(0), 351 .module = THIS_MODULE, 352 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list), 353 .init_service = ip_vs_sh_init_svc, 354 .done_service = ip_vs_sh_done_svc, 355 .add_dest = ip_vs_sh_dest_changed, 356 .del_dest = ip_vs_sh_dest_changed, 357 .upd_dest = ip_vs_sh_dest_changed, 358 .schedule = ip_vs_sh_schedule, 359}; 360 361 362static int __init ip_vs_sh_init(void) 363{ 364 return register_ip_vs_scheduler(&ip_vs_sh_scheduler); 365} 366 367 368static void __exit ip_vs_sh_cleanup(void) 369{ 370 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler); 371 synchronize_rcu(); 372} 373 374 375module_init(ip_vs_sh_init); 376module_exit(ip_vs_sh_cleanup); 377MODULE_LICENSE("GPL"); 378MODULE_DESCRIPTION("ipvs source hashing scheduler");
~/
Read-only file preview.