ip_vs_lblcr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * IPVS: Locality-Based Least-Connection with Replication scheduler
  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. * Julian Anastasov : Added the missing (dest->weight>0)
  13. * condition in the ip_vs_dest_set_max.
  14. *
  15. */
  16. /*
  17. * The lblc/r algorithm is as follows (pseudo code):
  18. *
  19. * if serverSet[dest_ip] is null then
  20. * n, serverSet[dest_ip] <- {weighted least-conn node};
  21. * else
  22. * n <- {least-conn (alive) node in serverSet[dest_ip]};
  23. * if (n is null) OR
  24. * (n.conns>n.weight AND
  25. * there is a node m with m.conns<m.weight/2) then
  26. * n <- {weighted least-conn node};
  27. * add n to serverSet[dest_ip];
  28. * if |serverSet[dest_ip]| > 1 AND
  29. * now - serverSet[dest_ip].lastMod > T then
  30. * m <- {most conn node in serverSet[dest_ip]};
  31. * remove m from serverSet[dest_ip];
  32. * if serverSet[dest_ip] changed then
  33. * serverSet[dest_ip].lastMod <- now;
  34. *
  35. * return n;
  36. *
  37. */
  38. #include <linux/ip.h>
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/skbuff.h>
  42. #include <linux/jiffies.h>
  43. /* for sysctl */
  44. #include <linux/fs.h>
  45. #include <linux/sysctl.h>
  46. #include <net/net_namespace.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(*e), 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. e->next = set->list;
  102. set->list = e;
  103. atomic_inc(&set->size);
  104. set->lastmod = jiffies;
  105. return e;
  106. }
  107. static void
  108. ip_vs_dest_set_erase(struct ip_vs_dest_set *set, struct ip_vs_dest *dest)
  109. {
  110. struct ip_vs_dest_list *e, **ep;
  111. for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
  112. if (e->dest == dest) {
  113. /* HIT */
  114. *ep = e->next;
  115. atomic_dec(&set->size);
  116. set->lastmod = jiffies;
  117. atomic_dec(&e->dest->refcnt);
  118. kfree(e);
  119. break;
  120. }
  121. ep = &e->next;
  122. }
  123. }
  124. static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
  125. {
  126. struct ip_vs_dest_list *e, **ep;
  127. write_lock(&set->lock);
  128. for (ep=&set->list, e=*ep; e!=NULL; e=*ep) {
  129. *ep = e->next;
  130. /*
  131. * We don't kfree dest because it is refered either
  132. * by its service or by the trash dest list.
  133. */
  134. atomic_dec(&e->dest->refcnt);
  135. kfree(e);
  136. }
  137. write_unlock(&set->lock);
  138. }
  139. /* get weighted least-connection node in the destination set */
  140. static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
  141. {
  142. register struct ip_vs_dest_list *e;
  143. struct ip_vs_dest *dest, *least;
  144. int loh, doh;
  145. if (set == NULL)
  146. return NULL;
  147. /* select the first destination server, whose weight > 0 */
  148. for (e=set->list; e!=NULL; e=e->next) {
  149. least = e->dest;
  150. if (least->flags & IP_VS_DEST_F_OVERLOAD)
  151. continue;
  152. if ((atomic_read(&least->weight) > 0)
  153. && (least->flags & IP_VS_DEST_F_AVAILABLE)) {
  154. loh = atomic_read(&least->activeconns) * 50
  155. + atomic_read(&least->inactconns);
  156. goto nextstage;
  157. }
  158. }
  159. return NULL;
  160. /* find the destination with the weighted least load */
  161. nextstage:
  162. for (e=e->next; e!=NULL; e=e->next) {
  163. dest = e->dest;
  164. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  165. continue;
  166. doh = atomic_read(&dest->activeconns) * 50
  167. + atomic_read(&dest->inactconns);
  168. if ((loh * atomic_read(&dest->weight) >
  169. doh * atomic_read(&least->weight))
  170. && (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
  171. least = dest;
  172. loh = doh;
  173. }
  174. }
  175. IP_VS_DBG(6, "ip_vs_dest_set_min: server %d.%d.%d.%d:%d "
  176. "activeconns %d refcnt %d weight %d overhead %d\n",
  177. NIPQUAD(least->addr.ip), ntohs(least->port),
  178. atomic_read(&least->activeconns),
  179. atomic_read(&least->refcnt),
  180. atomic_read(&least->weight), loh);
  181. return least;
  182. }
  183. /* get weighted most-connection node in the destination set */
  184. static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
  185. {
  186. register struct ip_vs_dest_list *e;
  187. struct ip_vs_dest *dest, *most;
  188. int moh, doh;
  189. if (set == NULL)
  190. return NULL;
  191. /* select the first destination server, whose weight > 0 */
  192. for (e=set->list; e!=NULL; e=e->next) {
  193. most = e->dest;
  194. if (atomic_read(&most->weight) > 0) {
  195. moh = atomic_read(&most->activeconns) * 50
  196. + atomic_read(&most->inactconns);
  197. goto nextstage;
  198. }
  199. }
  200. return NULL;
  201. /* find the destination with the weighted most load */
  202. nextstage:
  203. for (e=e->next; e!=NULL; e=e->next) {
  204. dest = e->dest;
  205. doh = atomic_read(&dest->activeconns) * 50
  206. + atomic_read(&dest->inactconns);
  207. /* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
  208. if ((moh * atomic_read(&dest->weight) <
  209. doh * atomic_read(&most->weight))
  210. && (atomic_read(&dest->weight) > 0)) {
  211. most = dest;
  212. moh = doh;
  213. }
  214. }
  215. IP_VS_DBG(6, "ip_vs_dest_set_max: server %d.%d.%d.%d:%d "
  216. "activeconns %d refcnt %d weight %d overhead %d\n",
  217. NIPQUAD(most->addr.ip), ntohs(most->port),
  218. atomic_read(&most->activeconns),
  219. atomic_read(&most->refcnt),
  220. atomic_read(&most->weight), moh);
  221. return most;
  222. }
  223. /*
  224. * IPVS lblcr entry represents an association between destination
  225. * IP address and its destination server set
  226. */
  227. struct ip_vs_lblcr_entry {
  228. struct list_head list;
  229. __be32 addr; /* destination IP address */
  230. struct ip_vs_dest_set set; /* destination server set */
  231. unsigned long lastuse; /* last used time */
  232. };
  233. /*
  234. * IPVS lblcr hash table
  235. */
  236. struct ip_vs_lblcr_table {
  237. struct list_head bucket[IP_VS_LBLCR_TAB_SIZE]; /* hash bucket */
  238. atomic_t entries; /* number of entries */
  239. int max_size; /* maximum size of entries */
  240. struct timer_list periodic_timer; /* collect stale entries */
  241. int rover; /* rover for expire check */
  242. int counter; /* counter for no expire */
  243. };
  244. /*
  245. * IPVS LBLCR sysctl table
  246. */
  247. static ctl_table vs_vars_table[] = {
  248. {
  249. .procname = "lblcr_expiration",
  250. .data = &sysctl_ip_vs_lblcr_expiration,
  251. .maxlen = sizeof(int),
  252. .mode = 0644,
  253. .proc_handler = &proc_dointvec_jiffies,
  254. },
  255. { .ctl_name = 0 }
  256. };
  257. static struct ctl_table_header * sysctl_header;
  258. static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
  259. {
  260. list_del(&en->list);
  261. ip_vs_dest_set_eraseall(&en->set);
  262. kfree(en);
  263. }
  264. /*
  265. * Returns hash value for IPVS LBLCR entry
  266. */
  267. static inline unsigned ip_vs_lblcr_hashkey(__be32 addr)
  268. {
  269. return (ntohl(addr)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
  270. }
  271. /*
  272. * Hash an entry in the ip_vs_lblcr_table.
  273. * returns bool success.
  274. */
  275. static void
  276. ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
  277. {
  278. unsigned hash = ip_vs_lblcr_hashkey(en->addr);
  279. list_add(&en->list, &tbl->bucket[hash]);
  280. atomic_inc(&tbl->entries);
  281. }
  282. /*
  283. * Get ip_vs_lblcr_entry associated with supplied parameters. Called under
  284. * read lock.
  285. */
  286. static inline struct ip_vs_lblcr_entry *
  287. ip_vs_lblcr_get(struct ip_vs_lblcr_table *tbl, __be32 addr)
  288. {
  289. unsigned hash = ip_vs_lblcr_hashkey(addr);
  290. struct ip_vs_lblcr_entry *en;
  291. list_for_each_entry(en, &tbl->bucket[hash], list)
  292. if (en->addr == addr)
  293. return en;
  294. return NULL;
  295. }
  296. /*
  297. * Create or update an ip_vs_lblcr_entry, which is a mapping of a destination
  298. * IP address to a server. Called under write lock.
  299. */
  300. static inline struct ip_vs_lblcr_entry *
  301. ip_vs_lblcr_new(struct ip_vs_lblcr_table *tbl, __be32 daddr,
  302. struct ip_vs_dest *dest)
  303. {
  304. struct ip_vs_lblcr_entry *en;
  305. en = ip_vs_lblcr_get(tbl, daddr);
  306. if (!en) {
  307. en = kmalloc(sizeof(*en), GFP_ATOMIC);
  308. if (!en) {
  309. IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
  310. return NULL;
  311. }
  312. en->addr = daddr;
  313. en->lastuse = jiffies;
  314. /* initilize its dest set */
  315. atomic_set(&(en->set.size), 0);
  316. en->set.list = NULL;
  317. rwlock_init(&en->set.lock);
  318. ip_vs_lblcr_hash(tbl, en);
  319. }
  320. write_lock(&en->set.lock);
  321. ip_vs_dest_set_insert(&en->set, dest);
  322. write_unlock(&en->set.lock);
  323. return en;
  324. }
  325. /*
  326. * Flush all the entries of the specified table.
  327. */
  328. static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
  329. {
  330. int i;
  331. struct ip_vs_lblcr_entry *en, *nxt;
  332. /* No locking required, only called during cleanup. */
  333. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  334. list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
  335. ip_vs_lblcr_free(en);
  336. }
  337. }
  338. }
  339. static inline void ip_vs_lblcr_full_check(struct ip_vs_service *svc)
  340. {
  341. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  342. unsigned long now = jiffies;
  343. int i, j;
  344. struct ip_vs_lblcr_entry *en, *nxt;
  345. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  346. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  347. write_lock(&svc->sched_lock);
  348. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  349. if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration,
  350. now))
  351. continue;
  352. ip_vs_lblcr_free(en);
  353. atomic_dec(&tbl->entries);
  354. }
  355. write_unlock(&svc->sched_lock);
  356. }
  357. tbl->rover = j;
  358. }
  359. /*
  360. * Periodical timer handler for IPVS lblcr table
  361. * It is used to collect stale entries when the number of entries
  362. * exceeds the maximum size of the table.
  363. *
  364. * Fixme: we probably need more complicated algorithm to collect
  365. * entries that have not been used for a long time even
  366. * if the number of entries doesn't exceed the maximum size
  367. * of the table.
  368. * The full expiration check is for this purpose now.
  369. */
  370. static void ip_vs_lblcr_check_expire(unsigned long data)
  371. {
  372. struct ip_vs_service *svc = (struct ip_vs_service *) data;
  373. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  374. unsigned long now = jiffies;
  375. int goal;
  376. int i, j;
  377. struct ip_vs_lblcr_entry *en, *nxt;
  378. if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
  379. /* do full expiration check */
  380. ip_vs_lblcr_full_check(svc);
  381. tbl->counter = 1;
  382. goto out;
  383. }
  384. if (atomic_read(&tbl->entries) <= tbl->max_size) {
  385. tbl->counter++;
  386. goto out;
  387. }
  388. goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
  389. if (goal > tbl->max_size/2)
  390. goal = tbl->max_size/2;
  391. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  392. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  393. write_lock(&svc->sched_lock);
  394. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  395. if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
  396. continue;
  397. ip_vs_lblcr_free(en);
  398. atomic_dec(&tbl->entries);
  399. goal--;
  400. }
  401. write_unlock(&svc->sched_lock);
  402. if (goal <= 0)
  403. break;
  404. }
  405. tbl->rover = j;
  406. out:
  407. mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
  408. }
  409. static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
  410. {
  411. int i;
  412. struct ip_vs_lblcr_table *tbl;
  413. /*
  414. * Allocate the ip_vs_lblcr_table for this service
  415. */
  416. tbl = kmalloc(sizeof(*tbl), GFP_ATOMIC);
  417. if (tbl == NULL) {
  418. IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
  419. return -ENOMEM;
  420. }
  421. svc->sched_data = tbl;
  422. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
  423. "current service\n", sizeof(*tbl));
  424. /*
  425. * Initialize the hash buckets
  426. */
  427. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  428. INIT_LIST_HEAD(&tbl->bucket[i]);
  429. }
  430. tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
  431. tbl->rover = 0;
  432. tbl->counter = 1;
  433. /*
  434. * Hook periodic timer for garbage collection
  435. */
  436. setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
  437. (unsigned long)svc);
  438. mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
  439. return 0;
  440. }
  441. static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
  442. {
  443. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  444. /* remove periodic timer */
  445. del_timer_sync(&tbl->periodic_timer);
  446. /* got to clean up table entries here */
  447. ip_vs_lblcr_flush(tbl);
  448. /* release the table itself */
  449. kfree(tbl);
  450. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
  451. sizeof(*tbl));
  452. return 0;
  453. }
  454. static inline struct ip_vs_dest *
  455. __ip_vs_lblcr_schedule(struct ip_vs_service *svc, struct iphdr *iph)
  456. {
  457. struct ip_vs_dest *dest, *least;
  458. int loh, doh;
  459. /*
  460. * We think the overhead of processing active connections is fifty
  461. * times higher than that of inactive connections in average. (This
  462. * fifty times might not be accurate, we will change it later.) We
  463. * use the following formula to estimate the overhead:
  464. * dest->activeconns*50 + dest->inactconns
  465. * and the load:
  466. * (dest overhead) / dest->weight
  467. *
  468. * Remember -- no floats in kernel mode!!!
  469. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  470. * h1/w1 > h2/w2
  471. * if every weight is larger than zero.
  472. *
  473. * The server with weight=0 is quiesced and will not receive any
  474. * new connection.
  475. */
  476. list_for_each_entry(dest, &svc->destinations, n_list) {
  477. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  478. continue;
  479. if (atomic_read(&dest->weight) > 0) {
  480. least = dest;
  481. loh = atomic_read(&least->activeconns) * 50
  482. + atomic_read(&least->inactconns);
  483. goto nextstage;
  484. }
  485. }
  486. return NULL;
  487. /*
  488. * Find the destination with the least load.
  489. */
  490. nextstage:
  491. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  492. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  493. continue;
  494. doh = atomic_read(&dest->activeconns) * 50
  495. + atomic_read(&dest->inactconns);
  496. if (loh * atomic_read(&dest->weight) >
  497. doh * atomic_read(&least->weight)) {
  498. least = dest;
  499. loh = doh;
  500. }
  501. }
  502. IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
  503. "activeconns %d refcnt %d weight %d overhead %d\n",
  504. NIPQUAD(least->addr.ip), ntohs(least->port),
  505. atomic_read(&least->activeconns),
  506. atomic_read(&least->refcnt),
  507. atomic_read(&least->weight), loh);
  508. return least;
  509. }
  510. /*
  511. * If this destination server is overloaded and there is a less loaded
  512. * server, then return true.
  513. */
  514. static inline int
  515. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  516. {
  517. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  518. struct ip_vs_dest *d;
  519. list_for_each_entry(d, &svc->destinations, n_list) {
  520. if (atomic_read(&d->activeconns)*2
  521. < atomic_read(&d->weight)) {
  522. return 1;
  523. }
  524. }
  525. }
  526. return 0;
  527. }
  528. /*
  529. * Locality-Based (weighted) Least-Connection scheduling
  530. */
  531. static struct ip_vs_dest *
  532. ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  533. {
  534. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  535. struct iphdr *iph = ip_hdr(skb);
  536. struct ip_vs_dest *dest = NULL;
  537. struct ip_vs_lblcr_entry *en;
  538. IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
  539. /* First look in our cache */
  540. read_lock(&svc->sched_lock);
  541. en = ip_vs_lblcr_get(tbl, iph->daddr);
  542. if (en) {
  543. /* We only hold a read lock, but this is atomic */
  544. en->lastuse = jiffies;
  545. /* Get the least loaded destination */
  546. read_lock(&en->set.lock);
  547. dest = ip_vs_dest_set_min(&en->set);
  548. read_unlock(&en->set.lock);
  549. /* More than one destination + enough time passed by, cleanup */
  550. if (atomic_read(&en->set.size) > 1 &&
  551. time_after(jiffies, en->set.lastmod +
  552. sysctl_ip_vs_lblcr_expiration)) {
  553. struct ip_vs_dest *m;
  554. write_lock(&en->set.lock);
  555. m = ip_vs_dest_set_max(&en->set);
  556. if (m)
  557. ip_vs_dest_set_erase(&en->set, m);
  558. write_unlock(&en->set.lock);
  559. }
  560. /* If the destination is not overloaded, use it */
  561. if (dest && !is_overloaded(dest, svc)) {
  562. read_unlock(&svc->sched_lock);
  563. goto out;
  564. }
  565. /* The cache entry is invalid, time to schedule */
  566. dest = __ip_vs_lblcr_schedule(svc, iph);
  567. if (!dest) {
  568. IP_VS_DBG(1, "no destination available\n");
  569. read_unlock(&svc->sched_lock);
  570. return NULL;
  571. }
  572. /* Update our cache entry */
  573. write_lock(&en->set.lock);
  574. ip_vs_dest_set_insert(&en->set, dest);
  575. write_unlock(&en->set.lock);
  576. }
  577. read_unlock(&svc->sched_lock);
  578. if (dest)
  579. goto out;
  580. /* No cache entry, time to schedule */
  581. dest = __ip_vs_lblcr_schedule(svc, iph);
  582. if (!dest) {
  583. IP_VS_DBG(1, "no destination available\n");
  584. return NULL;
  585. }
  586. /* If we fail to create a cache entry, we'll just use the valid dest */
  587. write_lock(&svc->sched_lock);
  588. ip_vs_lblcr_new(tbl, iph->daddr, dest);
  589. write_unlock(&svc->sched_lock);
  590. out:
  591. IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
  592. "--> server %u.%u.%u.%u:%d\n",
  593. NIPQUAD(iph->daddr),
  594. NIPQUAD(dest->addr.ip),
  595. ntohs(dest->port));
  596. return dest;
  597. }
  598. /*
  599. * IPVS LBLCR Scheduler structure
  600. */
  601. static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
  602. {
  603. .name = "lblcr",
  604. .refcnt = ATOMIC_INIT(0),
  605. .module = THIS_MODULE,
  606. .n_list = LIST_HEAD_INIT(ip_vs_lblcr_scheduler.n_list),
  607. #ifdef CONFIG_IP_VS_IPV6
  608. .supports_ipv6 = 0,
  609. #endif
  610. .init_service = ip_vs_lblcr_init_svc,
  611. .done_service = ip_vs_lblcr_done_svc,
  612. .schedule = ip_vs_lblcr_schedule,
  613. };
  614. static int __init ip_vs_lblcr_init(void)
  615. {
  616. int ret;
  617. sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table);
  618. ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  619. if (ret)
  620. unregister_sysctl_table(sysctl_header);
  621. return ret;
  622. }
  623. static void __exit ip_vs_lblcr_cleanup(void)
  624. {
  625. unregister_sysctl_table(sysctl_header);
  626. unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  627. }
  628. module_init(ip_vs_lblcr_init);
  629. module_exit(ip_vs_lblcr_cleanup);
  630. MODULE_LICENSE("GPL");