ip_vs_lblc.c 15 KB

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