ip_vs_lblc.c 14 KB

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