1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPVS: Destination Hashing scheduling module
5 * Authors: Wensong Zhang <wensong@gnuchina.org>
7 * Inspired by the consistent hashing scheduler patch from
8 * Thomas Proell <proellt@gmx.de>
14 * The dh algorithm is to select server by the hash key of destination IP
15 * address. The pseudo code is as follows:
17 * n <- servernode[dest_ip];
19 * (n is overloaded) OR (n.weight <= 0) then
24 * Notes that servernode is a 256-bucket hash table that maps the hash
25 * index derived from packet destination IP address to the current server
26 * array. If the dh scheduler is used in cache cluster, it is good to
27 * combine it with cache_bypass feature. When the statically assigned
28 * server is dead or overloaded, the load balancer can bypass the cache
29 * server and send requests to the original server directly.
33 #define KMSG_COMPONENT "IPVS"
34 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/skbuff.h>
41 #include <linux/hash.h>
43 #include <net/ip_vs.h>
49 struct ip_vs_dh_bucket {
50 struct ip_vs_dest __rcu *dest; /* real server (cache) */
54 * for IPVS DH entry hash table
56 #ifndef CONFIG_IP_VS_DH_TAB_BITS
57 #define CONFIG_IP_VS_DH_TAB_BITS 8
59 #define IP_VS_DH_TAB_BITS CONFIG_IP_VS_DH_TAB_BITS
60 #define IP_VS_DH_TAB_SIZE (1 << IP_VS_DH_TAB_BITS)
61 #define IP_VS_DH_TAB_MASK (IP_VS_DH_TAB_SIZE - 1)
63 struct ip_vs_dh_state {
64 struct ip_vs_dh_bucket buckets[IP_VS_DH_TAB_SIZE];
65 struct rcu_head rcu_head;
69 * Returns hash value for IPVS DH entry
71 static inline unsigned int ip_vs_dh_hashkey(int af, const union nf_inet_addr *addr)
73 __be32 addr_fold = addr->ip;
75 #ifdef CONFIG_IP_VS_IPV6
77 addr_fold = addr->ip6[0]^addr->ip6[1]^
78 addr->ip6[2]^addr->ip6[3];
80 return hash_32(ntohl(addr_fold), IP_VS_DH_TAB_BITS);
85 * Get ip_vs_dest associated with supplied parameters.
87 static inline struct ip_vs_dest *
88 ip_vs_dh_get(int af, struct ip_vs_dh_state *s, const union nf_inet_addr *addr)
90 return rcu_dereference(s->buckets[ip_vs_dh_hashkey(af, addr)].dest);
95 * Assign all the hash buckets of the specified table with the service.
98 ip_vs_dh_reassign(struct ip_vs_dh_state *s, struct ip_vs_service *svc)
101 struct ip_vs_dh_bucket *b;
103 struct ip_vs_dest *dest;
107 p = &svc->destinations;
108 empty = list_empty(p);
109 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
110 dest = rcu_dereference_protected(b->dest, 1);
112 ip_vs_dest_put(dest);
114 RCU_INIT_POINTER(b->dest, NULL);
116 if (p == &svc->destinations)
119 dest = list_entry(p, struct ip_vs_dest, n_list);
120 ip_vs_dest_hold(dest);
121 RCU_INIT_POINTER(b->dest, dest);
132 * Flush all the hash buckets of the specified table.
134 static void ip_vs_dh_flush(struct ip_vs_dh_state *s)
137 struct ip_vs_dh_bucket *b;
138 struct ip_vs_dest *dest;
141 for (i=0; i<IP_VS_DH_TAB_SIZE; i++) {
142 dest = rcu_dereference_protected(b->dest, 1);
144 ip_vs_dest_put(dest);
145 RCU_INIT_POINTER(b->dest, NULL);
152 static int ip_vs_dh_init_svc(struct ip_vs_service *svc)
154 struct ip_vs_dh_state *s;
156 /* allocate the DH table for this service */
157 s = kzalloc(sizeof(struct ip_vs_dh_state), GFP_KERNEL);
162 IP_VS_DBG(6, "DH hash table (memory=%zdbytes) allocated for "
164 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
166 /* assign the hash buckets with current dests */
167 ip_vs_dh_reassign(s, svc);
173 static void ip_vs_dh_done_svc(struct ip_vs_service *svc)
175 struct ip_vs_dh_state *s = svc->sched_data;
177 /* got to clean up hash buckets here */
180 /* release the table itself */
181 kfree_rcu(s, rcu_head);
182 IP_VS_DBG(6, "DH hash table (memory=%zdbytes) released\n",
183 sizeof(struct ip_vs_dh_bucket)*IP_VS_DH_TAB_SIZE);
187 static int ip_vs_dh_dest_changed(struct ip_vs_service *svc,
188 struct ip_vs_dest *dest)
190 struct ip_vs_dh_state *s = svc->sched_data;
192 /* assign the hash buckets with the updated service */
193 ip_vs_dh_reassign(s, svc);
200 * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
201 * consider that the server is overloaded here.
203 static inline int is_overloaded(struct ip_vs_dest *dest)
205 return dest->flags & IP_VS_DEST_F_OVERLOAD;
210 * Destination hashing scheduling
212 static struct ip_vs_dest *
213 ip_vs_dh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
214 struct ip_vs_iphdr *iph)
216 struct ip_vs_dest *dest;
217 struct ip_vs_dh_state *s;
219 IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
221 s = (struct ip_vs_dh_state *) svc->sched_data;
222 dest = ip_vs_dh_get(svc->af, s, &iph->daddr);
224 || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
225 || atomic_read(&dest->weight) <= 0
226 || is_overloaded(dest)) {
227 ip_vs_scheduler_err(svc, "no destination available");
231 IP_VS_DBG_BUF(6, "DH: destination IP address %s --> server %s:%d\n",
232 IP_VS_DBG_ADDR(svc->af, &iph->daddr),
233 IP_VS_DBG_ADDR(dest->af, &dest->addr),
241 * IPVS DH Scheduler structure
243 static struct ip_vs_scheduler ip_vs_dh_scheduler =
246 .refcnt = ATOMIC_INIT(0),
247 .module = THIS_MODULE,
248 .n_list = LIST_HEAD_INIT(ip_vs_dh_scheduler.n_list),
249 .init_service = ip_vs_dh_init_svc,
250 .done_service = ip_vs_dh_done_svc,
251 .add_dest = ip_vs_dh_dest_changed,
252 .del_dest = ip_vs_dh_dest_changed,
253 .schedule = ip_vs_dh_schedule,
257 static int __init ip_vs_dh_init(void)
259 return register_ip_vs_scheduler(&ip_vs_dh_scheduler);
263 static void __exit ip_vs_dh_cleanup(void)
265 unregister_ip_vs_scheduler(&ip_vs_dh_scheduler);
270 module_init(ip_vs_dh_init);
271 module_exit(ip_vs_dh_cleanup);
272 MODULE_LICENSE("GPL");