xref: /openbmc/linux/net/netfilter/ipvs/ip_vs_sh.c (revision 2874c5fd)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cb7f6a7bSJulius Volz /*
3cb7f6a7bSJulius Volz  * IPVS:        Source Hashing scheduling module
4cb7f6a7bSJulius Volz  *
5cb7f6a7bSJulius Volz  * Authors:     Wensong Zhang <wensong@gnuchina.org>
6cb7f6a7bSJulius Volz  *
7cb7f6a7bSJulius Volz  * Changes:
8cb7f6a7bSJulius Volz  */
9cb7f6a7bSJulius Volz 
10cb7f6a7bSJulius Volz /*
11cb7f6a7bSJulius Volz  * The sh algorithm is to select server by the hash key of source IP
12cb7f6a7bSJulius Volz  * address. The pseudo code is as follows:
13cb7f6a7bSJulius Volz  *
14cb7f6a7bSJulius Volz  *       n <- servernode[src_ip];
15cb7f6a7bSJulius Volz  *       if (n is dead) OR
16cb7f6a7bSJulius Volz  *          (n is overloaded) or (n.weight <= 0) then
17cb7f6a7bSJulius Volz  *                 return NULL;
18cb7f6a7bSJulius Volz  *
19cb7f6a7bSJulius Volz  *       return n;
20cb7f6a7bSJulius Volz  *
21cb7f6a7bSJulius Volz  * Notes that servernode is a 256-bucket hash table that maps the hash
22cb7f6a7bSJulius Volz  * index derived from packet source IP address to the current server
23cb7f6a7bSJulius Volz  * array. If the sh scheduler is used in cache cluster, it is good to
24cb7f6a7bSJulius Volz  * combine it with cache_bypass feature. When the statically assigned
25cb7f6a7bSJulius Volz  * server is dead or overloaded, the load balancer can bypass the cache
26cb7f6a7bSJulius Volz  * server and send requests to the original server directly.
27cb7f6a7bSJulius Volz  *
2876ad94fcSMichael Maxim  * The weight destination attribute can be used to control the
2976ad94fcSMichael Maxim  * distribution of connections to the destinations in servernode. The
3076ad94fcSMichael Maxim  * greater the weight, the more connections the destination
3176ad94fcSMichael Maxim  * will receive.
3276ad94fcSMichael Maxim  *
33cb7f6a7bSJulius Volz  */
34cb7f6a7bSJulius Volz 
359aada7acSHannes Eder #define KMSG_COMPONENT "IPVS"
369aada7acSHannes Eder #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
379aada7acSHannes Eder 
38cb7f6a7bSJulius Volz #include <linux/ip.h>
395a0e3ad6STejun Heo #include <linux/slab.h>
40cb7f6a7bSJulius Volz #include <linux/module.h>
41cb7f6a7bSJulius Volz #include <linux/kernel.h>
42cb7f6a7bSJulius Volz #include <linux/skbuff.h>
43cb7f6a7bSJulius Volz 
44cb7f6a7bSJulius Volz #include <net/ip_vs.h>
45cb7f6a7bSJulius Volz 
46eba3b5a7SAlexander Frolkin #include <net/tcp.h>
47eba3b5a7SAlexander Frolkin #include <linux/udp.h>
48eba3b5a7SAlexander Frolkin #include <linux/sctp.h>
49eba3b5a7SAlexander Frolkin 
50cb7f6a7bSJulius Volz 
51cb7f6a7bSJulius Volz /*
52cb7f6a7bSJulius Volz  *      IPVS SH bucket
53cb7f6a7bSJulius Volz  */
54cb7f6a7bSJulius Volz struct ip_vs_sh_bucket {
551acb7f67SJulian Anastasov 	struct ip_vs_dest __rcu	*dest;	/* real server (cache) */
56cb7f6a7bSJulius Volz };
57cb7f6a7bSJulius Volz 
58cb7f6a7bSJulius Volz /*
59cb7f6a7bSJulius Volz  *     for IPVS SH entry hash table
60cb7f6a7bSJulius Volz  */
61cb7f6a7bSJulius Volz #ifndef CONFIG_IP_VS_SH_TAB_BITS
62cb7f6a7bSJulius Volz #define CONFIG_IP_VS_SH_TAB_BITS        8
63cb7f6a7bSJulius Volz #endif
64cb7f6a7bSJulius Volz #define IP_VS_SH_TAB_BITS               CONFIG_IP_VS_SH_TAB_BITS
65cb7f6a7bSJulius Volz #define IP_VS_SH_TAB_SIZE               (1 << IP_VS_SH_TAB_BITS)
66cb7f6a7bSJulius Volz #define IP_VS_SH_TAB_MASK               (IP_VS_SH_TAB_SIZE - 1)
67cb7f6a7bSJulius Volz 
681acb7f67SJulian Anastasov struct ip_vs_sh_state {
691acb7f67SJulian Anastasov 	struct rcu_head			rcu_head;
70a70b9641SJan Beulich 	struct ip_vs_sh_bucket		buckets[IP_VS_SH_TAB_SIZE];
711acb7f67SJulian Anastasov };
72cb7f6a7bSJulius Volz 
73eba3b5a7SAlexander Frolkin /* Helper function to determine if server is unavailable */
is_unavailable(struct ip_vs_dest * dest)74eba3b5a7SAlexander Frolkin static inline bool is_unavailable(struct ip_vs_dest *dest)
75eba3b5a7SAlexander Frolkin {
76eba3b5a7SAlexander Frolkin 	return atomic_read(&dest->weight) <= 0 ||
77eba3b5a7SAlexander Frolkin 	       dest->flags & IP_VS_DEST_F_OVERLOAD;
78eba3b5a7SAlexander Frolkin }
79eba3b5a7SAlexander Frolkin 
80cb7f6a7bSJulius Volz /*
81cb7f6a7bSJulius Volz  *	Returns hash value for IPVS SH entry
82cb7f6a7bSJulius Volz  */
83eba3b5a7SAlexander Frolkin static inline unsigned int
ip_vs_sh_hashkey(int af,const union nf_inet_addr * addr,__be16 port,unsigned int offset)84eba3b5a7SAlexander Frolkin ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr,
85eba3b5a7SAlexander Frolkin 		 __be16 port, unsigned int offset)
86cb7f6a7bSJulius Volz {
8720971a0aSJulius Volz 	__be32 addr_fold = addr->ip;
8820971a0aSJulius Volz 
8920971a0aSJulius Volz #ifdef CONFIG_IP_VS_IPV6
9020971a0aSJulius Volz 	if (af == AF_INET6)
9120971a0aSJulius Volz 		addr_fold = addr->ip6[0]^addr->ip6[1]^
9220971a0aSJulius Volz 			    addr->ip6[2]^addr->ip6[3];
9320971a0aSJulius Volz #endif
949a17740eSVincent Bernat 	return (offset + hash_32(ntohs(port) + ntohl(addr_fold),
959a17740eSVincent Bernat 				 IP_VS_SH_TAB_BITS)) &
96eba3b5a7SAlexander Frolkin 		IP_VS_SH_TAB_MASK;
97cb7f6a7bSJulius Volz }
98cb7f6a7bSJulius Volz 
99cb7f6a7bSJulius Volz 
100cb7f6a7bSJulius Volz /*
101cb7f6a7bSJulius Volz  *      Get ip_vs_dest associated with supplied parameters.
102cb7f6a7bSJulius Volz  */
103cb7f6a7bSJulius Volz static inline struct ip_vs_dest *
ip_vs_sh_get(struct ip_vs_service * svc,struct ip_vs_sh_state * s,const union nf_inet_addr * addr,__be16 port)104eba3b5a7SAlexander Frolkin ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
105eba3b5a7SAlexander Frolkin 	     const union nf_inet_addr *addr, __be16 port)
106cb7f6a7bSJulius Volz {
107eba3b5a7SAlexander Frolkin 	unsigned int hash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
108eba3b5a7SAlexander Frolkin 	struct ip_vs_dest *dest = rcu_dereference(s->buckets[hash].dest);
109eba3b5a7SAlexander Frolkin 
110eba3b5a7SAlexander Frolkin 	return (!dest || is_unavailable(dest)) ? NULL : dest;
111cb7f6a7bSJulius Volz }
112cb7f6a7bSJulius Volz 
113cb7f6a7bSJulius Volz 
1141255ce5fSAlexander Frolkin /* As ip_vs_sh_get, but with fallback if selected server is unavailable
1151255ce5fSAlexander Frolkin  *
1161255ce5fSAlexander Frolkin  * The fallback strategy loops around the table starting from a "random"
1171255ce5fSAlexander Frolkin  * point (in fact, it is chosen to be the original hash value to make the
1181255ce5fSAlexander Frolkin  * algorithm deterministic) to find a new server.
1191255ce5fSAlexander Frolkin  */
120eba3b5a7SAlexander Frolkin static inline struct ip_vs_dest *
ip_vs_sh_get_fallback(struct ip_vs_service * svc,struct ip_vs_sh_state * s,const union nf_inet_addr * addr,__be16 port)121eba3b5a7SAlexander Frolkin ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
122eba3b5a7SAlexander Frolkin 		      const union nf_inet_addr *addr, __be16 port)
123eba3b5a7SAlexander Frolkin {
1241255ce5fSAlexander Frolkin 	unsigned int offset, roffset;
1251255ce5fSAlexander Frolkin 	unsigned int hash, ihash;
126eba3b5a7SAlexander Frolkin 	struct ip_vs_dest *dest;
127eba3b5a7SAlexander Frolkin 
1281255ce5fSAlexander Frolkin 	/* first try the dest it's supposed to go to */
1291255ce5fSAlexander Frolkin 	ihash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
1301255ce5fSAlexander Frolkin 	dest = rcu_dereference(s->buckets[ihash].dest);
1311255ce5fSAlexander Frolkin 	if (!dest)
1321255ce5fSAlexander Frolkin 		return NULL;
1331255ce5fSAlexander Frolkin 	if (!is_unavailable(dest))
1341255ce5fSAlexander Frolkin 		return dest;
1351255ce5fSAlexander Frolkin 
1361255ce5fSAlexander Frolkin 	IP_VS_DBG_BUF(6, "SH: selected unavailable server %s:%d, reselecting",
1374d316f3fSJulian Anastasov 		      IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
1381255ce5fSAlexander Frolkin 
1391255ce5fSAlexander Frolkin 	/* if the original dest is unavailable, loop around the table
1401255ce5fSAlexander Frolkin 	 * starting from ihash to find a new dest
1411255ce5fSAlexander Frolkin 	 */
142eba3b5a7SAlexander Frolkin 	for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) {
1431255ce5fSAlexander Frolkin 		roffset = (offset + ihash) % IP_VS_SH_TAB_SIZE;
1441255ce5fSAlexander Frolkin 		hash = ip_vs_sh_hashkey(svc->af, addr, port, roffset);
145eba3b5a7SAlexander Frolkin 		dest = rcu_dereference(s->buckets[hash].dest);
146eba3b5a7SAlexander Frolkin 		if (!dest)
147eba3b5a7SAlexander Frolkin 			break;
1481255ce5fSAlexander Frolkin 		if (!is_unavailable(dest))
149eba3b5a7SAlexander Frolkin 			return dest;
1501255ce5fSAlexander Frolkin 		IP_VS_DBG_BUF(6, "SH: selected unavailable "
1511255ce5fSAlexander Frolkin 			      "server %s:%d (offset %d), reselecting",
1524d316f3fSJulian Anastasov 			      IP_VS_DBG_ADDR(dest->af, &dest->addr),
1531255ce5fSAlexander Frolkin 			      ntohs(dest->port), roffset);
154eba3b5a7SAlexander Frolkin 	}
155eba3b5a7SAlexander Frolkin 
156eba3b5a7SAlexander Frolkin 	return NULL;
157eba3b5a7SAlexander Frolkin }
158eba3b5a7SAlexander Frolkin 
159cb7f6a7bSJulius Volz /*
160cb7f6a7bSJulius Volz  *      Assign all the hash buckets of the specified table with the service.
161cb7f6a7bSJulius Volz  */
162cb7f6a7bSJulius Volz static int
ip_vs_sh_reassign(struct ip_vs_sh_state * s,struct ip_vs_service * svc)1631acb7f67SJulian Anastasov ip_vs_sh_reassign(struct ip_vs_sh_state *s, struct ip_vs_service *svc)
164cb7f6a7bSJulius Volz {
165cb7f6a7bSJulius Volz 	int i;
166cb7f6a7bSJulius Volz 	struct ip_vs_sh_bucket *b;
167cb7f6a7bSJulius Volz 	struct list_head *p;
168cb7f6a7bSJulius Volz 	struct ip_vs_dest *dest;
16976ad94fcSMichael Maxim 	int d_count;
1701acb7f67SJulian Anastasov 	bool empty;
171cb7f6a7bSJulius Volz 
1721acb7f67SJulian Anastasov 	b = &s->buckets[0];
173cb7f6a7bSJulius Volz 	p = &svc->destinations;
1741acb7f67SJulian Anastasov 	empty = list_empty(p);
17576ad94fcSMichael Maxim 	d_count = 0;
176cb7f6a7bSJulius Volz 	for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
1771acb7f67SJulian Anastasov 		dest = rcu_dereference_protected(b->dest, 1);
1781acb7f67SJulian Anastasov 		if (dest)
1791acb7f67SJulian Anastasov 			ip_vs_dest_put(dest);
1801acb7f67SJulian Anastasov 		if (empty)
1811acb7f67SJulian Anastasov 			RCU_INIT_POINTER(b->dest, NULL);
1821acb7f67SJulian Anastasov 		else {
183cb7f6a7bSJulius Volz 			if (p == &svc->destinations)
184cb7f6a7bSJulius Volz 				p = p->next;
185cb7f6a7bSJulius Volz 
186cb7f6a7bSJulius Volz 			dest = list_entry(p, struct ip_vs_dest, n_list);
1871acb7f67SJulian Anastasov 			ip_vs_dest_hold(dest);
1881acb7f67SJulian Anastasov 			RCU_INIT_POINTER(b->dest, dest);
189cb7f6a7bSJulius Volz 
19076ad94fcSMichael Maxim 			IP_VS_DBG_BUF(6, "assigned i: %d dest: %s weight: %d\n",
1914d316f3fSJulian Anastasov 				      i, IP_VS_DBG_ADDR(dest->af, &dest->addr),
19276ad94fcSMichael Maxim 				      atomic_read(&dest->weight));
19376ad94fcSMichael Maxim 
19476ad94fcSMichael Maxim 			/* Don't move to next dest until filling weight */
19576ad94fcSMichael Maxim 			if (++d_count >= atomic_read(&dest->weight)) {
196cb7f6a7bSJulius Volz 				p = p->next;
19776ad94fcSMichael Maxim 				d_count = 0;
19876ad94fcSMichael Maxim 			}
19976ad94fcSMichael Maxim 
200cb7f6a7bSJulius Volz 		}
201cb7f6a7bSJulius Volz 		b++;
202cb7f6a7bSJulius Volz 	}
203cb7f6a7bSJulius Volz 	return 0;
204cb7f6a7bSJulius Volz }
205cb7f6a7bSJulius Volz 
206cb7f6a7bSJulius Volz 
207cb7f6a7bSJulius Volz /*
208cb7f6a7bSJulius Volz  *      Flush all the hash buckets of the specified table.
209cb7f6a7bSJulius Volz  */
ip_vs_sh_flush(struct ip_vs_sh_state * s)2101acb7f67SJulian Anastasov static void ip_vs_sh_flush(struct ip_vs_sh_state *s)
211cb7f6a7bSJulius Volz {
212cb7f6a7bSJulius Volz 	int i;
213cb7f6a7bSJulius Volz 	struct ip_vs_sh_bucket *b;
2141acb7f67SJulian Anastasov 	struct ip_vs_dest *dest;
215cb7f6a7bSJulius Volz 
2161acb7f67SJulian Anastasov 	b = &s->buckets[0];
217cb7f6a7bSJulius Volz 	for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
2181acb7f67SJulian Anastasov 		dest = rcu_dereference_protected(b->dest, 1);
2191acb7f67SJulian Anastasov 		if (dest) {
2201acb7f67SJulian Anastasov 			ip_vs_dest_put(dest);
2211acb7f67SJulian Anastasov 			RCU_INIT_POINTER(b->dest, NULL);
222cb7f6a7bSJulius Volz 		}
223cb7f6a7bSJulius Volz 		b++;
224cb7f6a7bSJulius Volz 	}
225cb7f6a7bSJulius Volz }
226cb7f6a7bSJulius Volz 
227cb7f6a7bSJulius Volz 
ip_vs_sh_init_svc(struct ip_vs_service * svc)228cb7f6a7bSJulius Volz static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
229cb7f6a7bSJulius Volz {
2301acb7f67SJulian Anastasov 	struct ip_vs_sh_state *s;
231cb7f6a7bSJulius Volz 
232cb7f6a7bSJulius Volz 	/* allocate the SH table for this service */
2331acb7f67SJulian Anastasov 	s = kzalloc(sizeof(struct ip_vs_sh_state), GFP_KERNEL);
2341acb7f67SJulian Anastasov 	if (s == NULL)
235cb7f6a7bSJulius Volz 		return -ENOMEM;
2360a9ee813SJoe Perches 
2371acb7f67SJulian Anastasov 	svc->sched_data = s;
2385b5e0928SAlexey Dobriyan 	IP_VS_DBG(6, "SH hash table (memory=%zdbytes) allocated for "
239cb7f6a7bSJulius Volz 		  "current service\n",
240cb7f6a7bSJulius Volz 		  sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
241cb7f6a7bSJulius Volz 
2421acb7f67SJulian Anastasov 	/* assign the hash buckets with current dests */
2431acb7f67SJulian Anastasov 	ip_vs_sh_reassign(s, svc);
244cb7f6a7bSJulius Volz 
245cb7f6a7bSJulius Volz 	return 0;
246cb7f6a7bSJulius Volz }
247cb7f6a7bSJulius Volz 
248cb7f6a7bSJulius Volz 
ip_vs_sh_done_svc(struct ip_vs_service * svc)249ed3ffc4eSJulian Anastasov static void ip_vs_sh_done_svc(struct ip_vs_service *svc)
250cb7f6a7bSJulius Volz {
2511acb7f67SJulian Anastasov 	struct ip_vs_sh_state *s = svc->sched_data;
252cb7f6a7bSJulius Volz 
253cb7f6a7bSJulius Volz 	/* got to clean up hash buckets here */
2541acb7f67SJulian Anastasov 	ip_vs_sh_flush(s);
255cb7f6a7bSJulius Volz 
256cb7f6a7bSJulius Volz 	/* release the table itself */
2571acb7f67SJulian Anastasov 	kfree_rcu(s, rcu_head);
2585b5e0928SAlexey Dobriyan 	IP_VS_DBG(6, "SH hash table (memory=%zdbytes) released\n",
259cb7f6a7bSJulius Volz 		  sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
260cb7f6a7bSJulius Volz }
261cb7f6a7bSJulius Volz 
262cb7f6a7bSJulius Volz 
ip_vs_sh_dest_changed(struct ip_vs_service * svc,struct ip_vs_dest * dest)2631acb7f67SJulian Anastasov static int ip_vs_sh_dest_changed(struct ip_vs_service *svc,
2641acb7f67SJulian Anastasov 				 struct ip_vs_dest *dest)
265cb7f6a7bSJulius Volz {
2661acb7f67SJulian Anastasov 	struct ip_vs_sh_state *s = svc->sched_data;
267cb7f6a7bSJulius Volz 
268cb7f6a7bSJulius Volz 	/* assign the hash buckets with the updated service */
2691acb7f67SJulian Anastasov 	ip_vs_sh_reassign(s, svc);
270cb7f6a7bSJulius Volz 
271cb7f6a7bSJulius Volz 	return 0;
272cb7f6a7bSJulius Volz }
273cb7f6a7bSJulius Volz 
274cb7f6a7bSJulius Volz 
275eba3b5a7SAlexander Frolkin /* Helper function to get port number */
276eba3b5a7SAlexander Frolkin static inline __be16
ip_vs_sh_get_port(const struct sk_buff * skb,struct ip_vs_iphdr * iph)277eba3b5a7SAlexander Frolkin ip_vs_sh_get_port(const struct sk_buff *skb, struct ip_vs_iphdr *iph)
278cb7f6a7bSJulius Volz {
2791471f35eSAlex Gartrell 	__be16 _ports[2], *ports;
280eba3b5a7SAlexander Frolkin 
2811471f35eSAlex Gartrell 	/* At this point we know that we have a valid packet of some kind.
2821471f35eSAlex Gartrell 	 * Because ICMP packets are only guaranteed to have the first 8
2831471f35eSAlex Gartrell 	 * bytes, let's just grab the ports.  Fortunately they're in the
2841471f35eSAlex Gartrell 	 * same position for all three of the protocols we care about.
2851471f35eSAlex Gartrell 	 */
286eba3b5a7SAlexander Frolkin 	switch (iph->protocol) {
287eba3b5a7SAlexander Frolkin 	case IPPROTO_TCP:
288eba3b5a7SAlexander Frolkin 	case IPPROTO_UDP:
289eba3b5a7SAlexander Frolkin 	case IPPROTO_SCTP:
2901471f35eSAlex Gartrell 		ports = skb_header_pointer(skb, iph->len, sizeof(_ports),
2911471f35eSAlex Gartrell 					   &_ports);
2921471f35eSAlex Gartrell 		if (unlikely(!ports))
29354e35cc5SDaniel Borkmann 			return 0;
294eba3b5a7SAlexander Frolkin 
2951471f35eSAlex Gartrell 		if (likely(!ip_vs_iph_inverse(iph)))
2961471f35eSAlex Gartrell 			return ports[0];
2971471f35eSAlex Gartrell 		else
2981471f35eSAlex Gartrell 			return ports[1];
2991471f35eSAlex Gartrell 	default:
3001471f35eSAlex Gartrell 		return 0;
3011471f35eSAlex Gartrell 	}
302cb7f6a7bSJulius Volz }
303cb7f6a7bSJulius Volz 
304cb7f6a7bSJulius Volz 
305cb7f6a7bSJulius Volz /*
306cb7f6a7bSJulius Volz  *      Source Hashing scheduling
307cb7f6a7bSJulius Volz  */
308cb7f6a7bSJulius Volz static struct ip_vs_dest *
ip_vs_sh_schedule(struct ip_vs_service * svc,const struct sk_buff * skb,struct ip_vs_iphdr * iph)309bba54de5SJulian Anastasov ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
310bba54de5SJulian Anastasov 		  struct ip_vs_iphdr *iph)
311cb7f6a7bSJulius Volz {
312cb7f6a7bSJulius Volz 	struct ip_vs_dest *dest;
3131acb7f67SJulian Anastasov 	struct ip_vs_sh_state *s;
314eba3b5a7SAlexander Frolkin 	__be16 port = 0;
3151471f35eSAlex Gartrell 	const union nf_inet_addr *hash_addr;
3161471f35eSAlex Gartrell 
3171471f35eSAlex Gartrell 	hash_addr = ip_vs_iph_inverse(iph) ? &iph->daddr : &iph->saddr;
318cb7f6a7bSJulius Volz 
319cb7f6a7bSJulius Volz 	IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
320cb7f6a7bSJulius Volz 
321eba3b5a7SAlexander Frolkin 	if (svc->flags & IP_VS_SVC_F_SCHED_SH_PORT)
322eba3b5a7SAlexander Frolkin 		port = ip_vs_sh_get_port(skb, iph);
323eba3b5a7SAlexander Frolkin 
3241acb7f67SJulian Anastasov 	s = (struct ip_vs_sh_state *) svc->sched_data;
325eba3b5a7SAlexander Frolkin 
326eba3b5a7SAlexander Frolkin 	if (svc->flags & IP_VS_SVC_F_SCHED_SH_FALLBACK)
3271471f35eSAlex Gartrell 		dest = ip_vs_sh_get_fallback(svc, s, hash_addr, port);
328eba3b5a7SAlexander Frolkin 	else
3291471f35eSAlex Gartrell 		dest = ip_vs_sh_get(svc, s, hash_addr, port);
330eba3b5a7SAlexander Frolkin 
331eba3b5a7SAlexander Frolkin 	if (!dest) {
33241ac51eeSPatrick Schaaf 		ip_vs_scheduler_err(svc, "no destination available");
333cb7f6a7bSJulius Volz 		return NULL;
334cb7f6a7bSJulius Volz 	}
335cb7f6a7bSJulius Volz 
33620971a0aSJulius Volz 	IP_VS_DBG_BUF(6, "SH: source IP address %s --> server %s:%d\n",
3371471f35eSAlex Gartrell 		      IP_VS_DBG_ADDR(svc->af, hash_addr),
3384d316f3fSJulian Anastasov 		      IP_VS_DBG_ADDR(dest->af, &dest->addr),
33920971a0aSJulius Volz 		      ntohs(dest->port));
340cb7f6a7bSJulius Volz 
341cb7f6a7bSJulius Volz 	return dest;
342cb7f6a7bSJulius Volz }
343cb7f6a7bSJulius Volz 
344cb7f6a7bSJulius Volz 
345cb7f6a7bSJulius Volz /*
346cb7f6a7bSJulius Volz  *      IPVS SH Scheduler structure
347cb7f6a7bSJulius Volz  */
348cb7f6a7bSJulius Volz static struct ip_vs_scheduler ip_vs_sh_scheduler =
349cb7f6a7bSJulius Volz {
350cb7f6a7bSJulius Volz 	.name =			"sh",
351cb7f6a7bSJulius Volz 	.refcnt =		ATOMIC_INIT(0),
352cb7f6a7bSJulius Volz 	.module =		THIS_MODULE,
353cb7f6a7bSJulius Volz 	.n_list	 =		LIST_HEAD_INIT(ip_vs_sh_scheduler.n_list),
354cb7f6a7bSJulius Volz 	.init_service =		ip_vs_sh_init_svc,
355cb7f6a7bSJulius Volz 	.done_service =		ip_vs_sh_done_svc,
3561acb7f67SJulian Anastasov 	.add_dest =		ip_vs_sh_dest_changed,
3571acb7f67SJulian Anastasov 	.del_dest =		ip_vs_sh_dest_changed,
3581acb7f67SJulian Anastasov 	.upd_dest =		ip_vs_sh_dest_changed,
359cb7f6a7bSJulius Volz 	.schedule =		ip_vs_sh_schedule,
360cb7f6a7bSJulius Volz };
361cb7f6a7bSJulius Volz 
362cb7f6a7bSJulius Volz 
ip_vs_sh_init(void)363cb7f6a7bSJulius Volz static int __init ip_vs_sh_init(void)
364cb7f6a7bSJulius Volz {
365cb7f6a7bSJulius Volz 	return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
366cb7f6a7bSJulius Volz }
367cb7f6a7bSJulius Volz 
368cb7f6a7bSJulius Volz 
ip_vs_sh_cleanup(void)369cb7f6a7bSJulius Volz static void __exit ip_vs_sh_cleanup(void)
370cb7f6a7bSJulius Volz {
371cb7f6a7bSJulius Volz 	unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
372ceec4c38SJulian Anastasov 	synchronize_rcu();
373cb7f6a7bSJulius Volz }
374cb7f6a7bSJulius Volz 
375cb7f6a7bSJulius Volz 
376cb7f6a7bSJulius Volz module_init(ip_vs_sh_init);
377cb7f6a7bSJulius Volz module_exit(ip_vs_sh_cleanup);
378cb7f6a7bSJulius Volz MODULE_LICENSE("GPL");
379