1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS: Source Hashing scheduling module
5 * Authors: Wensong Zhang <wensong@gnuchina.org>
11 * The sh algorithm is to select server by the hash key of source IP
12 * address. The pseudo code is as follows:
14 * n <- servernode[src_ip];
16 * (n is overloaded) or (n.weight <= 0) then
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.
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
35 #define KMSG_COMPONENT "IPVS"
36 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/skbuff.h>
44 #include <net/ip_vs.h>
47 #include <linux/udp.h>
48 #include <linux/sctp.h>
54 struct ip_vs_sh_bucket {
55 struct ip_vs_dest __rcu *dest; /* real server (cache) */
59 * for IPVS SH entry hash table
61 #ifndef CONFIG_IP_VS_SH_TAB_BITS
62 #define CONFIG_IP_VS_SH_TAB_BITS 8
64 #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
65 #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
66 #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
68 struct ip_vs_sh_state {
69 struct rcu_head rcu_head;
70 struct ip_vs_sh_bucket buckets[IP_VS_SH_TAB_SIZE];
73 /* Helper function to determine if server is unavailable */
74 static inline bool is_unavailable(struct ip_vs_dest *dest)
76 return atomic_read(&dest->weight) <= 0 ||
77 dest->flags & IP_VS_DEST_F_OVERLOAD;
81 * Returns hash value for IPVS SH entry
83 static inline unsigned int
84 ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr,
85 __be16 port, unsigned int offset)
87 __be32 addr_fold = addr->ip;
89 #ifdef CONFIG_IP_VS_IPV6
91 addr_fold = addr->ip6[0]^addr->ip6[1]^
92 addr->ip6[2]^addr->ip6[3];
94 return (offset + hash_32(ntohs(port) + ntohl(addr_fold),
101 * Get ip_vs_dest associated with supplied parameters.
103 static inline struct ip_vs_dest *
104 ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
105 const union nf_inet_addr *addr, __be16 port)
107 unsigned int hash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
108 struct ip_vs_dest *dest = rcu_dereference(s->buckets[hash].dest);
110 return (!dest || is_unavailable(dest)) ? NULL : dest;
114 /* As ip_vs_sh_get, but with fallback if selected server is unavailable
116 * The fallback strategy loops around the table starting from a "random"
117 * point (in fact, it is chosen to be the original hash value to make the
118 * algorithm deterministic) to find a new server.
120 static inline struct ip_vs_dest *
121 ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
122 const union nf_inet_addr *addr, __be16 port)
124 unsigned int offset, roffset;
125 unsigned int hash, ihash;
126 struct ip_vs_dest *dest;
128 /* first try the dest it's supposed to go to */
129 ihash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
130 dest = rcu_dereference(s->buckets[ihash].dest);
133 if (!is_unavailable(dest))
136 IP_VS_DBG_BUF(6, "SH: selected unavailable server %s:%d, reselecting",
137 IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
139 /* if the original dest is unavailable, loop around the table
140 * starting from ihash to find a new dest
142 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) {
143 roffset = (offset + ihash) % IP_VS_SH_TAB_SIZE;
144 hash = ip_vs_sh_hashkey(svc->af, addr, port, roffset);
145 dest = rcu_dereference(s->buckets[hash].dest);
148 if (!is_unavailable(dest))
150 IP_VS_DBG_BUF(6, "SH: selected unavailable "
151 "server %s:%d (offset %d), reselecting",
152 IP_VS_DBG_ADDR(dest->af, &dest->addr),
153 ntohs(dest->port), roffset);
160 * Assign all the hash buckets of the specified table with the service.
163 ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc)
166 struct ip_vs_sh_bucket *b;
168 struct ip_vs_dest *dest;
173 p = &svc->destinations;
174 empty = list_empty(p);
176 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
177 dest = rcu_dereference_protected(b->dest, 1);
179 ip_vs_dest_put(dest);
181 RCU_INIT_POINTER(b->dest, NULL);
183 if (p == &svc->destinations)
186 dest = list_entry(p, struct ip_vs_dest, n_list);
187 ip_vs_dest_hold(dest);
188 RCU_INIT_POINTER(b->dest, dest);
190 IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n",
191 i, IP_VS_DBG_ADDR(dest->af, &dest->addr),
192 atomic_read(&dest->weight));
194 /* Don't move to next dest until filling weight */
195 if (++d_count >= atomic_read(&dest->weight)) {
208 * Flush all the hash buckets of the specified table.
210 static void ip_vs_sh_flush(struct ip_vs_sh_state *s)
213 struct ip_vs_sh_bucket *b;
214 struct ip_vs_dest *dest;
217 for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
218 dest = rcu_dereference_protected(b->dest, 1);
220 ip_vs_dest_put(dest);
221 RCU_INIT_POINTER(b->dest, NULL);
228 static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
230 struct ip_vs_sh_state *s;
232 /* allocate the SH table for this service */
233 s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL);
238 IP_VS_DBG(6, "SH hash table (memory=%zdbytes) allocated for "
240 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
242 /* assign the hash buckets with current dests */
243 ip_vs_sh_reassign(s, svc);
249 static void ip_vs_sh_done_svc(struct ip_vs_service *svc)
251 struct ip_vs_sh_state *s = svc->sched_data;
253 /* got to clean up hash buckets here */
256 /* release the table itself */
257 kfree_rcu(s, rcu_head);
258 IP_VS_DBG(6, "SH hash table (memory=%zdbytes) released\n",
259 sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
263 static int ip_vs_sh_dest_changed(struct ip_vs_service *svc,
264 struct ip_vs_dest *dest)
266 struct ip_vs_sh_state *s = svc->sched_data;
268 /* assign the hash buckets with the updated service */
269 ip_vs_sh_reassign(s, svc);
275 /* Helper function to get port number */
277 ip_vs_sh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph)
279 __be16 _ports[2], *ports;
281 /* At this point we know that we have a valid packet of some kind.
282 * Because ICMP packets are only guaranteed to have the first 8
283 * bytes, let's just grab the ports. Fortunately they're in the
284 * same position for all three of the protocols we care about.
286 switch (iph->protocol) {
290 ports = skb_header_pointer(skb, iph->len, sizeof(_ports),
292 if (unlikely(!ports))
295 if (likely(!ip_vs_iph_inverse(iph)))
306 * Source Hashing scheduling
308 static struct ip_vs_dest *
309 ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
310 struct ip_vs_iphdr *iph)
312 struct ip_vs_dest *dest;
313 struct ip_vs_sh_state *s;
315 const union nf_inet_addr *hash_addr;
317 hash_addr = ip_vs_iph_inverse(iph) ? &iph->daddr : &iph->saddr;
319 IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
321 if (svc->flags & IP_VS_SVC_F_SCHED_SH_PORT)
322 port = ip_vs_sh_get_port(skb, iph);
324 s = (struct ip_vs_sh_state *) svc->sched_data;
326 if (svc->flags & IP_VS_SVC_F_SCHED_SH_FALLBACK)
327 dest = ip_vs_sh_get_fallback(svc, s, hash_addr, port);
329 dest = ip_vs_sh_get(svc, s, hash_addr, port);
332 ip_vs_scheduler_err(svc, "no destination available");
336 IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
337 IP_VS_DBG_ADDR(svc->af, hash_addr),
338 IP_VS_DBG_ADDR(dest->af, &dest->addr),
346 * IPVS SH Scheduler structure
348 static struct ip_vs_scheduler ip_vs_sh_scheduler =
351 .refcnt = ATOMIC_INIT(0),
352 .module = THIS_MODULE,
353 .n_list = LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
354 .init_service = ip_vs_sh_init_svc,
355 .done_service = ip_vs_sh_done_svc,
356 .add_dest = ip_vs_sh_dest_changed,
357 .del_dest = ip_vs_sh_dest_changed,
358 .upd_dest = ip_vs_sh_dest_changed,
359 .schedule = ip_vs_sh_schedule,
363 static int __init ip_vs_sh_init(void)
365 return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
369 static void __exit ip_vs_sh_cleanup(void)
371 unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
376 module_init(ip_vs_sh_init);
377 module_exit(ip_vs_sh_cleanup);
378 MODULE_LICENSE("GPL");
379 MODULE_DESCRIPTION("ipvs source hashing scheduler");