ip_vs_lblcr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * IPVS: Locality-Based Least-Connection with Replication scheduler
  3. *
  4. * Version: $Id: ip_vs_lblcr.c,v 1.11 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. * Julian Anastasov : Added the missing (dest->weight>0)
  15. * condition in the ip_vs_dest_set_max.
  16. *
  17. */
  18. /*
  19. * The lblc/r algorithm is as follows (pseudo code):
  20. *
  21. * if serverSet[dest_ip] is null then
  22. * n, serverSet[dest_ip] <- {weighted least-conn node};
  23. * else
  24. * n <- {least-conn (alive) node in serverSet[dest_ip]};
  25. * if (n is null) OR
  26. * (n.conns>n.weight AND
  27. * there is a node m with m.conns<m.weight/2) then
  28. * n <- {weighted least-conn node};
  29. * add n to serverSet[dest_ip];
  30. * if |serverSet[dest_ip]| > 1 AND
  31. * now - serverSet[dest_ip].lastMod > T then
  32. * m <- {most conn node in serverSet[dest_ip]};
  33. * remove m from serverSet[dest_ip];
  34. * if serverSet[dest_ip] changed then
  35. * serverSet[dest_ip].lastMod <- now;
  36. *
  37. * return n;
  38. *
  39. */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. /* for sysctl */
  43. #include <linux/fs.h>
  44. #include <linux/sysctl.h>
  45. /* for proc_net_create/proc_net_remove */
  46. #include <linux/proc_fs.h>
  47. #include <net/ip_vs.h>
  48. /*
  49. * It is for garbage collection of stale IPVS lblcr 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_lblcr_expiration = 24*60*60*HZ;
  62. /*
  63. * for IPVS lblcr entry hash table
  64. */
  65. #ifndef CONFIG_IP_VS_LBLCR_TAB_BITS
  66. #define CONFIG_IP_VS_LBLCR_TAB_BITS 10
  67. #endif
  68. #define IP_VS_LBLCR_TAB_BITS CONFIG_IP_VS_LBLCR_TAB_BITS
  69. #define IP_VS_LBLCR_TAB_SIZE (1 << IP_VS_LBLCR_TAB_BITS)
  70. #define IP_VS_LBLCR_TAB_MASK (IP_VS_LBLCR_TAB_SIZE - 1)
  71. /*
  72. * IPVS destination set structure and operations
  73. */
  74. struct ip_vs_dest_list {
  75. struct ip_vs_dest_list *next; /* list link */
  76. struct ip_vs_dest *dest; /* destination server */
  77. };
  78. struct ip_vs_dest_set {
  79. atomic_t size; /* set size */
  80. unsigned long lastmod; /* last modified time */
  81. struct ip_vs_dest_list *list; /* destination list */
  82. rwlock_t lock; /* lock for this list */
  83. };
  84. static struct ip_vs_dest_list *
  85. ip_vs_dest_set_insert(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
  86. {
  87. struct ip_vs_dest_list *e;
  88. for (e=set->list; e!=NULL; e=e->next) {
  89. if (e->dest == dest)
  90. /* already existed */
  91. return NULL;
  92. }
  93. e = kmalloc(sizeof(struct ip_vs_dest_list), GFP_ATOMIC);
  94. if (e == NULL) {
  95. IP_VS_ERR("ip_vs_dest_set_insert(): no memory\n");
  96. return NULL;
  97. }
  98. atomic_inc(&dest->refcnt);
  99. e->dest = dest;
  100. /* link it to the list */
  101. write_lock(&set->lock);
  102. e->next = set->list;
  103. set->list = e;
  104. atomic_inc(&set->size);
  105. write_unlock(&set->lock);
  106. set->lastmod = jiffies;
  107. return e;
  108. }
  109. static void
  110. ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
  111. {
  112. struct ip_vs_dest_list *e, **ep;
  113. write_lock(&set->lock);
  114. for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
  115. if (e->dest == dest) {
  116. /* HIT */
  117. *ep = e->next;
  118. atomic_dec(&set->size);
  119. set->lastmod = jiffies;
  120. atomic_dec(&e->dest->refcnt);
  121. kfree(e);
  122. break;
  123. }
  124. ep = &e->next;
  125. }
  126. write_unlock(&set->lock);
  127. }
  128. static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
  129. {
  130. struct ip_vs_dest_list *e, **ep;
  131. write_lock(&set->lock);
  132. for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
  133. *ep = e->next;
  134. /*
  135. * We don't kfree dest because it is refered either
  136. * by its service or by the trash dest list.
  137. */
  138. atomic_dec(&e->dest->refcnt);
  139. kfree(e);
  140. }
  141. write_unlock(&set->lock);
  142. }
  143. /* get weighted least-connection node in the destination set */
  144. static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
  145. {
  146. register struct ip_vs_dest_list *e;
  147. struct ip_vs_dest *dest, *least;
  148. int loh, doh;
  149. if (set == NULL)
  150. return NULL;
  151. read_lock(&set->lock);
  152. /* select the first destination server, whose weight > 0 */
  153. for (e=set->list; e!=NULL; e=e->next) {
  154. least = e->dest;
  155. if (least->flags & IP_VS_DEST_F_OVERLOAD)
  156. continue;
  157. if ((atomic_read(&least->weight) > 0)
  158. && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
  159. loh = atomic_read(&least->activeconns) * 50
  160. + atomic_read(&least->inactconns);
  161. goto nextstage;
  162. }
  163. }
  164. read_unlock(&set->lock);
  165. return NULL;
  166. /* find the destination with the weighted least load */
  167. nextstage:
  168. for (e=e->next; e!=NULL; e=e->next) {
  169. dest = e->dest;
  170. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  171. continue;
  172. doh = atomic_read(&dest->activeconns) * 50
  173. + atomic_read(&dest->inactconns);
  174. if ((loh * atomic_read(&dest->weight) >
  175. doh * atomic_read(&least->weight))
  176. && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  177. least = dest;
  178. loh = doh;
  179. }
  180. }
  181. read_unlock(&set->lock);
  182. IP_VS_DBG(6, "ip_vs_dest_set_min: server %d.%d.%d.%d:%d "
  183. "activeconns %d refcnt %d weight %d overhead %d\n",
  184. NIPQUAD(least->addr), ntohs(least->port),
  185. atomic_read(&least->activeconns),
  186. atomic_read(&least->refcnt),
  187. atomic_read(&least->weight), loh);
  188. return least;
  189. }
  190. /* get weighted most-connection node in the destination set */
  191. static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
  192. {
  193. register struct ip_vs_dest_list *e;
  194. struct ip_vs_dest *dest, *most;
  195. int moh, doh;
  196. if (set == NULL)
  197. return NULL;
  198. read_lock(&set->lock);
  199. /* select the first destination server, whose weight > 0 */
  200. for (e=set->list; e!=NULL; e=e->next) {
  201. most = e->dest;
  202. if (atomic_read(&most->weight) > 0) {
  203. moh = atomic_read(&most->activeconns) * 50
  204. + atomic_read(&most->inactconns);
  205. goto nextstage;
  206. }
  207. }
  208. read_unlock(&set->lock);
  209. return NULL;
  210. /* find the destination with the weighted most load */
  211. nextstage:
  212. for (e=e->next; e!=NULL; e=e->next) {
  213. dest = e->dest;
  214. doh = atomic_read(&dest->activeconns) * 50
  215. + atomic_read(&dest->inactconns);
  216. /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
  217. if ((moh * atomic_read(&dest->weight) <
  218. doh * atomic_read(&most->weight))
  219. && (atomic_read(&dest->weight) > 0)) {
  220. most = dest;
  221. moh = doh;
  222. }
  223. }
  224. read_unlock(&set->lock);
  225. IP_VS_DBG(6, "ip_vs_dest_set_max: server %d.%d.%d.%d:%d "
  226. "activeconns %d refcnt %d weight %d overhead %d\n",
  227. NIPQUAD(most->addr), ntohs(most->port),
  228. atomic_read(&most->activeconns),
  229. atomic_read(&most->refcnt),
  230. atomic_read(&most->weight), moh);
  231. return most;
  232. }
  233. /*
  234. * IPVS lblcr entry represents an association between destination
  235. * IP address and its destination server set
  236. */
  237. struct ip_vs_lblcr_entry {
  238. struct list_head list;
  239. __u32 addr; /* destination IP address */
  240. struct ip_vs_dest_set set; /* destination server set */
  241. unsigned long lastuse; /* last used time */
  242. };
  243. /*
  244. * IPVS lblcr hash table
  245. */
  246. struct ip_vs_lblcr_table {
  247. rwlock_t lock; /* lock for this table */
  248. struct list_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */
  249. atomic_t entries; /* number of entries */
  250. int max_size; /* maximum size of entries */
  251. struct timer_list periodic_timer; /* collect stale entries */
  252. int rover; /* rover for expire check */
  253. int counter; /* counter for no expire */
  254. };
  255. /*
  256. * IPVS LBLCR sysctl table
  257. */
  258. static ctl_table vs_vars_table[] = {
  259. {
  260. .ctl_name = NET_IPV4_VS_LBLCR_EXPIRE,
  261. .procname = "lblcr_expiration",
  262. .data = &sysctl_ip_vs_lblcr_expiration,
  263. .maxlen = sizeof(int),
  264. .mode = 0644,
  265. .proc_handler = &proc_dointvec_jiffies,
  266. },
  267. { .ctl_name = 0 }
  268. };
  269. static ctl_table vs_table[] = {
  270. {
  271. .ctl_name = NET_IPV4_VS,
  272. .procname = "vs",
  273. .mode = 0555,
  274. .child = vs_vars_table
  275. },
  276. { .ctl_name = 0 }
  277. };
  278. static ctl_table ipv4_table[] = {
  279. {
  280. .ctl_name = NET_IPV4,
  281. .procname = "ipv4",
  282. .mode = 0555,
  283. .child = vs_table
  284. },
  285. { .ctl_name = 0 }
  286. };
  287. static ctl_table lblcr_root_table[] = {
  288. {
  289. .ctl_name = CTL_NET,
  290. .procname = "net",
  291. .mode = 0555,
  292. .child = ipv4_table
  293. },
  294. { .ctl_name = 0 }
  295. };
  296. static struct ctl_table_header * sysctl_header;
  297. /*
  298. * new/free a ip_vs_lblcr_entry, which is a mapping of a destination
  299. * IP address to a server.
  300. */
  301. static inline struct ip_vs_lblcr_entry *ip_vs_lblcr_new(__u32 daddr)
  302. {
  303. struct ip_vs_lblcr_entry *en;
  304. en = kmalloc(sizeof(struct ip_vs_lblcr_entry), GFP_ATOMIC);
  305. if (en == NULL) {
  306. IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
  307. return NULL;
  308. }
  309. INIT_LIST_HEAD(&en->list);
  310. en->addr = daddr;
  311. /* initilize its dest set */
  312. atomic_set(&(en->set.size), 0);
  313. en->set.list = NULL;
  314. rwlock_init(&en->set.lock);
  315. return en;
  316. }
  317. static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
  318. {
  319. list_del(&en->list);
  320. ip_vs_dest_set_eraseall(&en->set);
  321. kfree(en);
  322. }
  323. /*
  324. * Returns hash value for IPVS LBLCR entry
  325. */
  326. static inline unsigned ip_vs_lblcr_hashkey(__u32 addr)
  327. {
  328. return (ntohl(addr)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
  329. }
  330. /*
  331. * Hash an entry in the ip_vs_lblcr_table.
  332. * returns bool success.
  333. */
  334. static int
  335. ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
  336. {
  337. unsigned hash;
  338. if (!list_empty(&en->list)) {
  339. IP_VS_ERR("ip_vs_lblcr_hash(): request for already hashed, "
  340. "called from %p\n", __builtin_return_address(0));
  341. return 0;
  342. }
  343. /*
  344. * Hash by destination IP address
  345. */
  346. hash = ip_vs_lblcr_hashkey(en->addr);
  347. write_lock(&tbl->lock);
  348. list_add(&en->list, &tbl->bucket[hash]);
  349. atomic_inc(&tbl->entries);
  350. write_unlock(&tbl->lock);
  351. return 1;
  352. }
  353. #if 0000
  354. /*
  355. * Unhash ip_vs_lblcr_entry from ip_vs_lblcr_table.
  356. * returns bool success.
  357. */
  358. static int ip_vs_lblcr_unhash(struct ip_vs_lblcr_table *tbl,
  359. struct ip_vs_lblcr_entry *en)
  360. {
  361. if (list_empty(&en->list)) {
  362. IP_VS_ERR("ip_vs_lblcr_unhash(): request for not hashed entry, "
  363. "called from %p\n", __builtin_return_address(0));
  364. return 0;
  365. }
  366. /*
  367. * Remove it from the table
  368. */
  369. write_lock(&tbl->lock);
  370. list_del(&en->list);
  371. INIT_LIST_HEAD(&en->list);
  372. write_unlock(&tbl->lock);
  373. return 1;
  374. }
  375. #endif
  376. /*
  377. * Get ip_vs_lblcr_entry associated with supplied parameters.
  378. */
  379. static inline struct ip_vs_lblcr_entry *
  380. ip_vs_lblcr_get(struct ip_vs_lblcr_table *tbl, __u32 addr)
  381. {
  382. unsigned hash;
  383. struct ip_vs_lblcr_entry *en;
  384. hash = ip_vs_lblcr_hashkey(addr);
  385. read_lock(&tbl->lock);
  386. list_for_each_entry(en, &tbl->bucket[hash], list) {
  387. if (en->addr == addr) {
  388. /* HIT */
  389. read_unlock(&tbl->lock);
  390. return en;
  391. }
  392. }
  393. read_unlock(&tbl->lock);
  394. return NULL;
  395. }
  396. /*
  397. * Flush all the entries of the specified table.
  398. */
  399. static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
  400. {
  401. int i;
  402. struct ip_vs_lblcr_entry *en, *nxt;
  403. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  404. write_lock(&tbl->lock);
  405. list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
  406. ip_vs_lblcr_free(en);
  407. atomic_dec(&tbl->entries);
  408. }
  409. write_unlock(&tbl->lock);
  410. }
  411. }
  412. static inline void ip_vs_lblcr_full_check(struct ip_vs_lblcr_table *tbl)
  413. {
  414. unsigned long now = jiffies;
  415. int i, j;
  416. struct ip_vs_lblcr_entry *en, *nxt;
  417. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  418. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  419. write_lock(&tbl->lock);
  420. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  421. if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration,
  422. now))
  423. continue;
  424. ip_vs_lblcr_free(en);
  425. atomic_dec(&tbl->entries);
  426. }
  427. write_unlock(&tbl->lock);
  428. }
  429. tbl->rover = j;
  430. }
  431. /*
  432. * Periodical timer handler for IPVS lblcr table
  433. * It is used to collect stale entries when the number of entries
  434. * exceeds the maximum size of the table.
  435. *
  436. * Fixme: we probably need more complicated algorithm to collect
  437. * entries that have not been used for a long time even
  438. * if the number of entries doesn't exceed the maximum size
  439. * of the table.
  440. * The full expiration check is for this purpose now.
  441. */
  442. static void ip_vs_lblcr_check_expire(unsigned long data)
  443. {
  444. struct ip_vs_lblcr_table *tbl;
  445. unsigned long now = jiffies;
  446. int goal;
  447. int i, j;
  448. struct ip_vs_lblcr_entry *en, *nxt;
  449. tbl = (struct ip_vs_lblcr_table *)data;
  450. if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
  451. /* do full expiration check */
  452. ip_vs_lblcr_full_check(tbl);
  453. tbl->counter = 1;
  454. goto out;
  455. }
  456. if (atomic_read(&tbl->entries) <= tbl->max_size) {
  457. tbl->counter++;
  458. goto out;
  459. }
  460. goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
  461. if (goal > tbl->max_size/2)
  462. goal = tbl->max_size/2;
  463. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  464. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  465. write_lock(&tbl->lock);
  466. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  467. if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
  468. continue;
  469. ip_vs_lblcr_free(en);
  470. atomic_dec(&tbl->entries);
  471. goal--;
  472. }
  473. write_unlock(&tbl->lock);
  474. if (goal <= 0)
  475. break;
  476. }
  477. tbl->rover = j;
  478. out:
  479. mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
  480. }
  481. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  482. static struct ip_vs_lblcr_table *lblcr_table_list;
  483. /*
  484. * /proc/net/ip_vs_lblcr to display the mappings of
  485. * destination IP address <==> its serverSet
  486. */
  487. static int
  488. ip_vs_lblcr_getinfo(char *buffer, char **start, off_t offset, int length)
  489. {
  490. off_t pos=0, begin;
  491. int len=0, size;
  492. struct ip_vs_lblcr_table *tbl;
  493. unsigned long now = jiffies;
  494. int i;
  495. struct ip_vs_lblcr_entry *en;
  496. tbl = lblcr_table_list;
  497. size = sprintf(buffer, "LastTime Dest IP address Server set\n");
  498. pos += size;
  499. len += size;
  500. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  501. read_lock_bh(&tbl->lock);
  502. list_for_each_entry(en, &tbl->bucket[i], list) {
  503. char tbuf[16];
  504. struct ip_vs_dest_list *d;
  505. sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(en->addr));
  506. size = sprintf(buffer+len, "%8lu %-16s ",
  507. now-en->lastuse, tbuf);
  508. read_lock(&en->set.lock);
  509. for (d=en->set.list; d!=NULL; d=d->next) {
  510. size += sprintf(buffer+len+size,
  511. "%u.%u.%u.%u ",
  512. NIPQUAD(d->dest->addr));
  513. }
  514. read_unlock(&en->set.lock);
  515. size += sprintf(buffer+len+size, "\n");
  516. len += size;
  517. pos += size;
  518. if (pos <= offset)
  519. len=0;
  520. if (pos >= offset+length) {
  521. read_unlock_bh(&tbl->lock);
  522. goto done;
  523. }
  524. }
  525. read_unlock_bh(&tbl->lock);
  526. }
  527. done:
  528. begin = len - (pos - offset);
  529. *start = buffer + begin;
  530. len -= begin;
  531. if(len>length)
  532. len = length;
  533. return len;
  534. }
  535. #endif
  536. static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
  537. {
  538. int i;
  539. struct ip_vs_lblcr_table *tbl;
  540. /*
  541. * Allocate the ip_vs_lblcr_table for this service
  542. */
  543. tbl = kmalloc(sizeof(struct ip_vs_lblcr_table), GFP_ATOMIC);
  544. if (tbl == NULL) {
  545. IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
  546. return -ENOMEM;
  547. }
  548. svc->sched_data = tbl;
  549. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
  550. "current service\n",
  551. sizeof(struct ip_vs_lblcr_table));
  552. /*
  553. * Initialize the hash buckets
  554. */
  555. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  556. INIT_LIST_HEAD(&tbl->bucket[i]);
  557. }
  558. rwlock_init(&tbl->lock);
  559. tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
  560. tbl->rover = 0;
  561. tbl->counter = 1;
  562. /*
  563. * Hook periodic timer for garbage collection
  564. */
  565. init_timer(&tbl->periodic_timer);
  566. tbl->periodic_timer.data = (unsigned long)tbl;
  567. tbl->periodic_timer.function = ip_vs_lblcr_check_expire;
  568. tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
  569. add_timer(&tbl->periodic_timer);
  570. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  571. lblcr_table_list = tbl;
  572. #endif
  573. return 0;
  574. }
  575. static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
  576. {
  577. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  578. /* remove periodic timer */
  579. del_timer_sync(&tbl->periodic_timer);
  580. /* got to clean up table entries here */
  581. ip_vs_lblcr_flush(tbl);
  582. /* release the table itself */
  583. kfree(svc->sched_data);
  584. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
  585. sizeof(struct ip_vs_lblcr_table));
  586. return 0;
  587. }
  588. static int ip_vs_lblcr_update_svc(struct ip_vs_service *svc)
  589. {
  590. return 0;
  591. }
  592. static inline struct ip_vs_dest *
  593. __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
  594. {
  595. struct ip_vs_dest *dest, *least;
  596. int loh, doh;
  597. /*
  598. * We think the overhead of processing active connections is fifty
  599. * times higher than that of inactive connections in average. (This
  600. * fifty times might not be accurate, we will change it later.) We
  601. * use the following formula to estimate the overhead:
  602. * dest->activeconns*50 + dest->inactconns
  603. * and the load:
  604. * (dest overhead) / dest->weight
  605. *
  606. * Remember -- no floats in kernel mode!!!
  607. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  608. * h1/w1 > h2/w2
  609. * if every weight is larger than zero.
  610. *
  611. * The server with weight=0 is quiesced and will not receive any
  612. * new connection.
  613. */
  614. list_for_each_entry(dest, &svc->destinations, n_list) {
  615. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  616. continue;
  617. if (atomic_read(&dest->weight) > 0) {
  618. least = dest;
  619. loh = atomic_read(&least->activeconns) * 50
  620. + atomic_read(&least->inactconns);
  621. goto nextstage;
  622. }
  623. }
  624. return NULL;
  625. /*
  626. * Find the destination with the least load.
  627. */
  628. nextstage:
  629. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  630. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  631. continue;
  632. doh = atomic_read(&dest->activeconns) * 50
  633. + atomic_read(&dest->inactconns);
  634. if (loh * atomic_read(&dest->weight) >
  635. doh * atomic_read(&least->weight)) {
  636. least = dest;
  637. loh = doh;
  638. }
  639. }
  640. IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
  641. "activeconns %d refcnt %d weight %d overhead %d\n",
  642. NIPQUAD(least->addr), ntohs(least->port),
  643. atomic_read(&least->activeconns),
  644. atomic_read(&least->refcnt),
  645. atomic_read(&least->weight), loh);
  646. return least;
  647. }
  648. /*
  649. * If this destination server is overloaded and there is a less loaded
  650. * server, then return true.
  651. */
  652. static inline int
  653. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  654. {
  655. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  656. struct ip_vs_dest *d;
  657. list_for_each_entry(d, &svc->destinations, n_list) {
  658. if (atomic_read(&d->activeconns)*2
  659. < atomic_read(&d->weight)) {
  660. return 1;
  661. }
  662. }
  663. }
  664. return 0;
  665. }
  666. /*
  667. * Locality-Based (weighted) Least-Connection scheduling
  668. */
  669. static struct ip_vs_dest *
  670. ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  671. {
  672. struct ip_vs_dest *dest;
  673. struct ip_vs_lblcr_table *tbl;
  674. struct ip_vs_lblcr_entry *en;
  675. struct iphdr *iph = skb->nh.iph;
  676. IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
  677. tbl = (struct ip_vs_lblcr_table *)svc->sched_data;
  678. en = ip_vs_lblcr_get(tbl, iph->daddr);
  679. if (en == NULL) {
  680. dest = __ip_vs_wlc_schedule(svc, iph);
  681. if (dest == NULL) {
  682. IP_VS_DBG(1, "no destination available\n");
  683. return NULL;
  684. }
  685. en = ip_vs_lblcr_new(iph->daddr);
  686. if (en == NULL) {
  687. return NULL;
  688. }
  689. ip_vs_dest_set_insert(&en->set, dest);
  690. ip_vs_lblcr_hash(tbl, en);
  691. } else {
  692. dest = ip_vs_dest_set_min(&en->set);
  693. if (!dest || is_overloaded(dest, svc)) {
  694. dest = __ip_vs_wlc_schedule(svc, iph);
  695. if (dest == NULL) {
  696. IP_VS_DBG(1, "no destination available\n");
  697. return NULL;
  698. }
  699. ip_vs_dest_set_insert(&en->set, dest);
  700. }
  701. if (atomic_read(&en->set.size) > 1 &&
  702. jiffies-en->set.lastmod > sysctl_ip_vs_lblcr_expiration) {
  703. struct ip_vs_dest *m;
  704. m = ip_vs_dest_set_max(&en->set);
  705. if (m)
  706. ip_vs_dest_set_erase(&en->set, m);
  707. }
  708. }
  709. en->lastuse = jiffies;
  710. IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
  711. "--> server %u.%u.%u.%u:%d\n",
  712. NIPQUAD(en->addr),
  713. NIPQUAD(dest->addr),
  714. ntohs(dest->port));
  715. return dest;
  716. }
  717. /*
  718. * IPVS LBLCR Scheduler structure
  719. */
  720. static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
  721. {
  722. .name = "lblcr",
  723. .refcnt = ATOMIC_INIT(0),
  724. .module = THIS_MODULE,
  725. .init_service = ip_vs_lblcr_init_svc,
  726. .done_service = ip_vs_lblcr_done_svc,
  727. .update_service = ip_vs_lblcr_update_svc,
  728. .schedule = ip_vs_lblcr_schedule,
  729. };
  730. static int __init ip_vs_lblcr_init(void)
  731. {
  732. INIT_LIST_HEAD(&ip_vs_lblcr_scheduler.n_list);
  733. sysctl_header = register_sysctl_table(lblcr_root_table, 0);
  734. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  735. proc_net_create("ip_vs_lblcr", 0, ip_vs_lblcr_getinfo);
  736. #endif
  737. return register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  738. }
  739. static void __exit ip_vs_lblcr_cleanup(void)
  740. {
  741. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  742. proc_net_remove("ip_vs_lblcr");
  743. #endif
  744. unregister_sysctl_table(sysctl_header);
  745. unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  746. }
  747. module_init(ip_vs_lblcr_init);
  748. module_exit(ip_vs_lblcr_cleanup);
  749. MODULE_LICENSE("GPL");