ip_vs_lblc.c 14 KB

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