ip_vs_sh.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * IPVS: Source Hashing scheduling module
  3. *
  4. * Version: $Id: ip_vs_sh.c,v 1.5 2002/09/15 08:14:08 wensong Exp $
  5. *
  6. * Authors: Wensong Zhang <wensong@gnuchina.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * Changes:
  14. *
  15. */
  16. /*
  17. * The sh algorithm is to select server by the hash key of source IP
  18. * address. The pseudo code is as follows:
  19. *
  20. * n <- servernode[src_ip];
  21. * if (n is dead) OR
  22. * (n is overloaded) or (n.weight <= 0) then
  23. * return NULL;
  24. *
  25. * return n;
  26. *
  27. * Notes that servernode is a 256-bucket hash table that maps the hash
  28. * index derived from packet source IP address to the current server
  29. * array. If the sh scheduler is used in cache cluster, it is good to
  30. * combine it with cache_bypass feature. When the statically assigned
  31. * server is dead or overloaded, the load balancer can bypass the cache
  32. * server and send requests to the original server directly.
  33. *
  34. */
  35. #include <linux/module.h>
  36. #include <linux/kernel.h>
  37. #include <net/ip_vs.h>
  38. /*
  39. * IPVS SH bucket
  40. */
  41. struct ip_vs_sh_bucket {
  42. struct ip_vs_dest *dest; /* real server (cache) */
  43. };
  44. /*
  45. * for IPVS SH entry hash table
  46. */
  47. #ifndef CONFIG_IP_VS_SH_TAB_BITS
  48. #define CONFIG_IP_VS_SH_TAB_BITS 8
  49. #endif
  50. #define IP_VS_SH_TAB_BITS CONFIG_IP_VS_SH_TAB_BITS
  51. #define IP_VS_SH_TAB_SIZE (1 << IP_VS_SH_TAB_BITS)
  52. #define IP_VS_SH_TAB_MASK (IP_VS_SH_TAB_SIZE - 1)
  53. /*
  54. * Returns hash value for IPVS SH entry
  55. */
  56. static inline unsigned ip_vs_sh_hashkey(__u32 addr)
  57. {
  58. return (ntohl(addr)*2654435761UL) & IP_VS_SH_TAB_MASK;
  59. }
  60. /*
  61. * Get ip_vs_dest associated with supplied parameters.
  62. */
  63. static inline struct ip_vs_dest *
  64. ip_vs_sh_get(struct ip_vs_sh_bucket *tbl, __u32 addr)
  65. {
  66. return (tbl[ip_vs_sh_hashkey(addr)]).dest;
  67. }
  68. /*
  69. * Assign all the hash buckets of the specified table with the service.
  70. */
  71. static int
  72. ip_vs_sh_assign(struct ip_vs_sh_bucket *tbl, struct ip_vs_service *svc)
  73. {
  74. int i;
  75. struct ip_vs_sh_bucket *b;
  76. struct list_head *p;
  77. struct ip_vs_dest *dest;
  78. b = tbl;
  79. p = &svc->destinations;
  80. for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
  81. if (list_empty(p)) {
  82. b->dest = NULL;
  83. } else {
  84. if (p == &svc->destinations)
  85. p = p->next;
  86. dest = list_entry(p, struct ip_vs_dest, n_list);
  87. atomic_inc(&dest->refcnt);
  88. b->dest = dest;
  89. p = p->next;
  90. }
  91. b++;
  92. }
  93. return 0;
  94. }
  95. /*
  96. * Flush all the hash buckets of the specified table.
  97. */
  98. static void ip_vs_sh_flush(struct ip_vs_sh_bucket *tbl)
  99. {
  100. int i;
  101. struct ip_vs_sh_bucket *b;
  102. b = tbl;
  103. for (i=0; i<IP_VS_SH_TAB_SIZE; i++) {
  104. if (b->dest) {
  105. atomic_dec(&b->dest->refcnt);
  106. b->dest = NULL;
  107. }
  108. b++;
  109. }
  110. }
  111. static int ip_vs_sh_init_svc(struct ip_vs_service *svc)
  112. {
  113. struct ip_vs_sh_bucket *tbl;
  114. /* allocate the SH table for this service */
  115. tbl = kmalloc(sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE,
  116. GFP_ATOMIC);
  117. if (tbl == NULL) {
  118. IP_VS_ERR("ip_vs_sh_init_svc(): no memory\n");
  119. return -ENOMEM;
  120. }
  121. svc->sched_data = tbl;
  122. IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) allocated for "
  123. "current service\n",
  124. sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
  125. /* assign the hash buckets with the updated service */
  126. ip_vs_sh_assign(tbl, svc);
  127. return 0;
  128. }
  129. static int ip_vs_sh_done_svc(struct ip_vs_service *svc)
  130. {
  131. struct ip_vs_sh_bucket *tbl = svc->sched_data;
  132. /* got to clean up hash buckets here */
  133. ip_vs_sh_flush(tbl);
  134. /* release the table itself */
  135. kfree(svc->sched_data);
  136. IP_VS_DBG(6, "SH hash table (memory=%Zdbytes) released\n",
  137. sizeof(struct ip_vs_sh_bucket)*IP_VS_SH_TAB_SIZE);
  138. return 0;
  139. }
  140. static int ip_vs_sh_update_svc(struct ip_vs_service *svc)
  141. {
  142. struct ip_vs_sh_bucket *tbl = svc->sched_data;
  143. /* got to clean up hash buckets here */
  144. ip_vs_sh_flush(tbl);
  145. /* assign the hash buckets with the updated service */
  146. ip_vs_sh_assign(tbl, svc);
  147. return 0;
  148. }
  149. /*
  150. * If the dest flags is set with IP_VS_DEST_F_OVERLOAD,
  151. * consider that the server is overloaded here.
  152. */
  153. static inline int is_overloaded(struct ip_vs_dest *dest)
  154. {
  155. return dest->flags & IP_VS_DEST_F_OVERLOAD;
  156. }
  157. /*
  158. * Source Hashing scheduling
  159. */
  160. static struct ip_vs_dest *
  161. ip_vs_sh_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  162. {
  163. struct ip_vs_dest *dest;
  164. struct ip_vs_sh_bucket *tbl;
  165. struct iphdr *iph = skb->nh.iph;
  166. IP_VS_DBG(6, "ip_vs_sh_schedule(): Scheduling...\n");
  167. tbl = (struct ip_vs_sh_bucket *)svc->sched_data;
  168. dest = ip_vs_sh_get(tbl, iph->saddr);
  169. if (!dest
  170. || !(dest->flags & IP_VS_DEST_F_AVAILABLE)
  171. || atomic_read(&dest->weight) <= 0
  172. || is_overloaded(dest)) {
  173. return NULL;
  174. }
  175. IP_VS_DBG(6, "SH: source IP address %u.%u.%u.%u "
  176. "--> server %u.%u.%u.%u:%d\n",
  177. NIPQUAD(iph->saddr),
  178. NIPQUAD(dest->addr),
  179. ntohs(dest->port));
  180. return dest;
  181. }
  182. /*
  183. * IPVS SH Scheduler structure
  184. */
  185. static struct ip_vs_scheduler ip_vs_sh_scheduler =
  186. {
  187. .name = "sh",
  188. .refcnt = ATOMIC_INIT(0),
  189. .module = THIS_MODULE,
  190. .init_service = ip_vs_sh_init_svc,
  191. .done_service = ip_vs_sh_done_svc,
  192. .update_service = ip_vs_sh_update_svc,
  193. .schedule = ip_vs_sh_schedule,
  194. };
  195. static int __init ip_vs_sh_init(void)
  196. {
  197. INIT_LIST_HEAD(&ip_vs_sh_scheduler.n_list);
  198. return register_ip_vs_scheduler(&ip_vs_sh_scheduler);
  199. }
  200. static void __exit ip_vs_sh_cleanup(void)
  201. {
  202. unregister_ip_vs_scheduler(&ip_vs_sh_scheduler);
  203. }
  204. module_init(ip_vs_sh_init);
  205. module_exit(ip_vs_sh_cleanup);
  206. MODULE_LICENSE("GPL");