ip_vs_lblc.c 14 KB

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