PageRenderTime 246ms CodeModel.GetById 5ms RepoModel.GetById 4ms app.codeStats 1ms

/net/ipv4/ipvs/ip_vs_lblc.c

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