ip_vs_lblc.c 14 KB

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