ip_vs_lblc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * IPVS: Locality-Based Least-Connection scheduling module
  3. *
  4. * Authors: Wensong Zhang <wensong@gnuchina.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Changes:
  12. * Martin Hamilton : fixed the terrible locking bugs
  13. * *lock(tbl->lock) ==> *lock(&tbl->lock)
  14. * Wensong Zhang : fixed the uninitilized tbl->lock bug
  15. * Wensong Zhang : added doing full expiration check to
  16. * collect stale entries of 24+ hours when
  17. * no partial expire check in a half hour
  18. * Julian Anastasov : replaced del_timer call with del_timer_sync
  19. * to avoid the possible race between timer
  20. * handler and del_timer thread in SMP
  21. *
  22. */
  23. /*
  24. * The lblc algorithm is as follows (pseudo code):
  25. *
  26. * if cachenode[dest_ip] is null then
  27. * n, cachenode[dest_ip] <- {weighted least-conn node};
  28. * else
  29. * n <- cachenode[dest_ip];
  30. * if (n is dead) OR
  31. * (n.conns>n.weight AND
  32. * there is a node m with m.conns<m.weight/2) then
  33. * n, cachenode[dest_ip] <- {weighted least-conn node};
  34. *
  35. * return n;
  36. *
  37. * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing
  38. * me to write this module.
  39. */
  40. #include <linux/ip.h>
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/jiffies.h>
  45. /* for sysctl */
  46. #include <linux/fs.h>
  47. #include <linux/sysctl.h>
  48. #include <net/ip_vs.h>
  49. /*
  50. * It is for garbage collection of stale IPVS lblc entries,
  51. * when the table is full.
  52. */
  53. #define CHECK_EXPIRE_INTERVAL (60*HZ)
  54. #define ENTRY_TIMEOUT (6*60*HZ)
  55. /*
  56. * It is for full expiration check.
  57. * When there is no partial expiration check (garbage collection)
  58. * in a half hour, do a full expiration check to collect stale
  59. * entries that haven't been touched for a day.
  60. */
  61. #define COUNT_FOR_FULL_EXPIRATION 30
  62. static int sysctl_ip_vs_lblc_expiration = 24*60*60*HZ;
  63. /*
  64. * for IPVS lblc entry hash table
  65. */
  66. #ifndef CONFIG_IP_VS_LBLC_TAB_BITS
  67. #define CONFIG_IP_VS_LBLC_TAB_BITS 10
  68. #endif
  69. #define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS
  70. #define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)
  71. #define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)
  72. /*
  73. * IPVS lblc entry represents an association between destination
  74. * IP address and its destination server
  75. */
  76. struct ip_vs_lblc_entry {
  77. struct list_head list;
  78. __be32 addr; /* destination IP address */
  79. struct ip_vs_dest *dest; /* real server (cache) */
  80. unsigned long lastuse; /* last used time */
  81. };
  82. /*
  83. * IPVS lblc hash table
  84. */
  85. struct ip_vs_lblc_table {
  86. struct list_head bucket[IP_VS_LBLC_TAB_SIZE]; /* hash bucket */
  87. atomic_t entries; /* number of entries */
  88. int max_size; /* maximum size of entries */
  89. struct timer_list periodic_timer; /* collect stale entries */
  90. int rover; /* rover for expire check */
  91. int counter; /* counter for no expire */
  92. };
  93. /*
  94. * IPVS LBLC sysctl table
  95. */
  96. static ctl_table vs_vars_table[] = {
  97. {
  98. .procname = "lblc_expiration",
  99. .data = &sysctl_ip_vs_lblc_expiration,
  100. .maxlen = sizeof(int),
  101. .mode = 0644,
  102. .proc_handler = &proc_dointvec_jiffies,
  103. },
  104. { .ctl_name = 0 }
  105. };
  106. static struct ctl_table_header * sysctl_header;
  107. static inline void ip_vs_lblc_free(struct ip_vs_lblc_entry *en)
  108. {
  109. list_del(&en->list);
  110. /*
  111. * We don't kfree dest because it is refered either by its service
  112. * or the trash dest list.
  113. */
  114. atomic_dec(&en->dest->refcnt);
  115. kfree(en);
  116. }
  117. /*
  118. * Returns hash value for IPVS LBLC entry
  119. */
  120. static inline unsigned ip_vs_lblc_hashkey(__be32 addr)
  121. {
  122. return (ntohl(addr)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
  123. }
  124. /*
  125. * Hash an entry in the ip_vs_lblc_table.
  126. * returns bool success.
  127. */
  128. static void
  129. ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
  130. {
  131. unsigned hash = ip_vs_lblc_hashkey(en->addr);
  132. list_add(&en->list, &tbl->bucket[hash]);
  133. atomic_inc(&tbl->entries);
  134. }
  135. /*
  136. * Get ip_vs_lblc_entry associated with supplied parameters. Called under read
  137. * lock
  138. */
  139. static inline struct ip_vs_lblc_entry *
  140. ip_vs_lblc_get(struct ip_vs_lblc_table *tbl, __be32 addr)
  141. {
  142. unsigned hash = ip_vs_lblc_hashkey(addr);
  143. struct ip_vs_lblc_entry *en;
  144. list_for_each_entry(en, &tbl->bucket[hash], list)
  145. if (en->addr == addr)
  146. return en;
  147. return NULL;
  148. }
  149. /*
  150. * Create or update an ip_vs_lblc_entry, which is a mapping of a destination IP
  151. * address to a server. Called under write lock.
  152. */
  153. static inline struct ip_vs_lblc_entry *
  154. ip_vs_lblc_new(struct ip_vs_lblc_table *tbl, __be32 daddr,
  155. struct ip_vs_dest *dest)
  156. {
  157. struct ip_vs_lblc_entry *en;
  158. en = ip_vs_lblc_get(tbl, daddr);
  159. if (!en) {
  160. en = kmalloc(sizeof(*en), GFP_ATOMIC);
  161. if (!en) {
  162. IP_VS_ERR("ip_vs_lblc_new(): no memory\n");
  163. return NULL;
  164. }
  165. en->addr = daddr;
  166. en->lastuse = jiffies;
  167. atomic_inc(&dest->refcnt);
  168. en->dest = dest;
  169. ip_vs_lblc_hash(tbl, en);
  170. } else if (en->dest != dest) {
  171. atomic_dec(&en->dest->refcnt);
  172. atomic_inc(&dest->refcnt);
  173. en->dest = dest;
  174. }
  175. return en;
  176. }
  177. /*
  178. * Flush all the entries of the specified table.
  179. */
  180. static void ip_vs_lblc_flush(struct ip_vs_lblc_table *tbl)
  181. {
  182. struct ip_vs_lblc_entry *en, *nxt;
  183. int i;
  184. for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
  185. list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
  186. ip_vs_lblc_free(en);
  187. atomic_dec(&tbl->entries);
  188. }
  189. }
  190. }
  191. static inline void ip_vs_lblc_full_check(struct ip_vs_service *svc)
  192. {
  193. struct ip_vs_lblc_table *tbl = svc->sched_data;
  194. struct ip_vs_lblc_entry *en, *nxt;
  195. unsigned long now = jiffies;
  196. int i, j;
  197. for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
  198. j = (j + 1) & IP_VS_LBLC_TAB_MASK;
  199. write_lock(&svc->sched_lock);
  200. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  201. if (time_before(now,
  202. en->lastuse + sysctl_ip_vs_lblc_expiration))
  203. continue;
  204. ip_vs_lblc_free(en);
  205. atomic_dec(&tbl->entries);
  206. }
  207. write_unlock(&svc->sched_lock);
  208. }
  209. tbl->rover = j;
  210. }
  211. /*
  212. * Periodical timer handler for IPVS lblc table
  213. * It is used to collect stale entries when the number of entries
  214. * exceeds the maximum size of the table.
  215. *
  216. * Fixme: we probably need more complicated algorithm to collect
  217. * entries that have not been used for a long time even
  218. * if the number of entries doesn't exceed the maximum size
  219. * of the table.
  220. * The full expiration check is for this purpose now.
  221. */
  222. static void ip_vs_lblc_check_expire(unsigned long data)
  223. {
  224. struct ip_vs_service *svc = (struct ip_vs_service *) data;
  225. struct ip_vs_lblc_table *tbl = svc->sched_data;
  226. unsigned long now = jiffies;
  227. int goal;
  228. int i, j;
  229. struct ip_vs_lblc_entry *en, *nxt;
  230. if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
  231. /* do full expiration check */
  232. ip_vs_lblc_full_check(svc);
  233. tbl->counter = 1;
  234. goto out;
  235. }
  236. if (atomic_read(&tbl->entries) <= tbl->max_size) {
  237. tbl->counter++;
  238. goto out;
  239. }
  240. goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
  241. if (goal > tbl->max_size/2)
  242. goal = tbl->max_size/2;
  243. for (i=0, j=tbl->rover; i<IP_VS_LBLC_TAB_SIZE; i++) {
  244. j = (j + 1) & IP_VS_LBLC_TAB_MASK;
  245. write_lock(&svc->sched_lock);
  246. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  247. if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
  248. continue;
  249. ip_vs_lblc_free(en);
  250. atomic_dec(&tbl->entries);
  251. goal--;
  252. }
  253. write_unlock(&svc->sched_lock);
  254. if (goal <= 0)
  255. break;
  256. }
  257. tbl->rover = j;
  258. out:
  259. mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
  260. }
  261. static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
  262. {
  263. int i;
  264. struct ip_vs_lblc_table *tbl;
  265. /*
  266. * Allocate the ip_vs_lblc_table for this service
  267. */
  268. tbl = kmalloc(sizeof(*tbl), GFP_ATOMIC);
  269. if (tbl == NULL) {
  270. IP_VS_ERR("ip_vs_lblc_init_svc(): no memory\n");
  271. return -ENOMEM;
  272. }
  273. svc->sched_data = tbl;
  274. IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
  275. "current service\n", sizeof(*tbl));
  276. /*
  277. * Initialize the hash buckets
  278. */
  279. for (i=0; i<IP_VS_LBLC_TAB_SIZE; i++) {
  280. INIT_LIST_HEAD(&tbl->bucket[i]);
  281. }
  282. tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
  283. tbl->rover = 0;
  284. tbl->counter = 1;
  285. /*
  286. * Hook periodic timer for garbage collection
  287. */
  288. setup_timer(&tbl->periodic_timer, ip_vs_lblc_check_expire,
  289. (unsigned long)svc);
  290. mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
  291. return 0;
  292. }
  293. static int ip_vs_lblc_done_svc(struct ip_vs_service *svc)
  294. {
  295. struct ip_vs_lblc_table *tbl = svc->sched_data;
  296. /* remove periodic timer */
  297. del_timer_sync(&tbl->periodic_timer);
  298. /* got to clean up table entries here */
  299. ip_vs_lblc_flush(tbl);
  300. /* release the table itself */
  301. kfree(tbl);
  302. IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
  303. sizeof(*tbl));
  304. return 0;
  305. }
  306. static inline struct ip_vs_dest *
  307. __ip_vs_lblc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
  308. {
  309. struct ip_vs_dest *dest, *least;
  310. int loh, doh;
  311. /*
  312. * We think the overhead of processing active connections is fifty
  313. * times higher than that of inactive connections in average. (This
  314. * fifty times might not be accurate, we will change it later.) We
  315. * use the following formula to estimate the overhead:
  316. * dest->activeconns*50 + dest->inactconns
  317. * and the load:
  318. * (dest overhead) / dest->weight
  319. *
  320. * Remember -- no floats in kernel mode!!!
  321. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  322. * h1/w1 > h2/w2
  323. * if every weight is larger than zero.
  324. *
  325. * The server with weight=0 is quiesced and will not receive any
  326. * new connection.
  327. */
  328. list_for_each_entry(dest, &svc->destinations, n_list) {
  329. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  330. continue;
  331. if (atomic_read(&dest->weight) > 0) {
  332. least = dest;
  333. loh = atomic_read(&least->activeconns) * 50
  334. + atomic_read(&least->inactconns);
  335. goto nextstage;
  336. }
  337. }
  338. return NULL;
  339. /*
  340. * Find the destination with the least load.
  341. */
  342. nextstage:
  343. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  344. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  345. continue;
  346. doh = atomic_read(&dest->activeconns) * 50
  347. + atomic_read(&dest->inactconns);
  348. if (loh * atomic_read(&dest->weight) >
  349. doh * atomic_read(&least->weight)) {
  350. least = dest;
  351. loh = doh;
  352. }
  353. }
  354. IP_VS_DBG(6, "LBLC: server %d.%d.%d.%d:%d "
  355. "activeconns %d refcnt %d weight %d overhead %d\n",
  356. NIPQUAD(least->addr.ip), ntohs(least->port),
  357. atomic_read(&least->activeconns),
  358. atomic_read(&least->refcnt),
  359. atomic_read(&least->weight), loh);
  360. return least;
  361. }
  362. /*
  363. * If this destination server is overloaded and there is a less loaded
  364. * server, then return true.
  365. */
  366. static inline int
  367. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  368. {
  369. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  370. struct ip_vs_dest *d;
  371. list_for_each_entry(d, &svc->destinations, n_list) {
  372. if (atomic_read(&d->activeconns)*2
  373. < atomic_read(&d->weight)) {
  374. return 1;
  375. }
  376. }
  377. }
  378. return 0;
  379. }
  380. /*
  381. * Locality-Based (weighted) Least-Connection scheduling
  382. */
  383. static struct ip_vs_dest *
  384. ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  385. {
  386. struct ip_vs_lblc_table *tbl = svc->sched_data;
  387. struct iphdr *iph = ip_hdr(skb);
  388. struct ip_vs_dest *dest = NULL;
  389. struct ip_vs_lblc_entry *en;
  390. IP_VS_DBG(6, "ip_vs_lblc_schedule(): Scheduling...\n");
  391. /* First look in our cache */
  392. read_lock(&svc->sched_lock);
  393. en = ip_vs_lblc_get(tbl, iph->daddr);
  394. if (en) {
  395. /* We only hold a read lock, but this is atomic */
  396. en->lastuse = jiffies;
  397. /*
  398. * If the destination is not available, i.e. it's in the trash,
  399. * we must ignore it, as it may be removed from under our feet,
  400. * if someone drops our reference count. Our caller only makes
  401. * sure that destinations, that are not in the trash, are not
  402. * moved to the trash, while we are scheduling. But anyone can
  403. * free up entries from the trash at any time.
  404. */
  405. if (en->dest->flags & IP_VS_DEST_F_AVAILABLE)
  406. dest = en->dest;
  407. }
  408. read_unlock(&svc->sched_lock);
  409. /* If the destination has a weight and is not overloaded, use it */
  410. if (dest && atomic_read(&dest->weight) > 0 && !is_overloaded(dest, svc))
  411. goto out;
  412. /* No cache entry or it is invalid, time to schedule */
  413. dest = __ip_vs_lblc_schedule(svc, iph);
  414. if (!dest) {
  415. IP_VS_DBG(1, "no destination available\n");
  416. return NULL;
  417. }
  418. /* If we fail to create a cache entry, we'll just use the valid dest */
  419. write_lock(&svc->sched_lock);
  420. ip_vs_lblc_new(tbl, iph->daddr, dest);
  421. write_unlock(&svc->sched_lock);
  422. out:
  423. IP_VS_DBG(6, "LBLC: destination IP address %u.%u.%u.%u "
  424. "--> server %u.%u.%u.%u:%d\n",
  425. NIPQUAD(iph->daddr),
  426. NIPQUAD(dest->addr.ip),
  427. ntohs(dest->port));
  428. return dest;
  429. }
  430. /*
  431. * IPVS LBLC Scheduler structure
  432. */
  433. static struct ip_vs_scheduler ip_vs_lblc_scheduler =
  434. {
  435. .name = "lblc",
  436. .refcnt = ATOMIC_INIT(0),
  437. .module = THIS_MODULE,
  438. .n_list = LIST_HEAD_INIT(ip_vs_lblc_scheduler.n_list),
  439. #ifdef CONFIG_IP_VS_IPV6
  440. .supports_ipv6 = 0,
  441. #endif
  442. .init_service = ip_vs_lblc_init_svc,
  443. .done_service = ip_vs_lblc_done_svc,
  444. .schedule = ip_vs_lblc_schedule,
  445. };
  446. static int __init ip_vs_lblc_init(void)
  447. {
  448. int ret;
  449. sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table);
  450. ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
  451. if (ret)
  452. unregister_sysctl_table(sysctl_header);
  453. return ret;
  454. }
  455. static void __exit ip_vs_lblc_cleanup(void)
  456. {
  457. unregister_sysctl_table(sysctl_header);
  458. unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
  459. }
  460. module_init(ip_vs_lblc_init);
  461. module_exit(ip_vs_lblc_cleanup);
  462. MODULE_LICENSE("GPL");