ip_vs_lblcr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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(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. __be32 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. .procname = "lblcr_expiration",
  261. .data = &sysctl_ip_vs_lblcr_expiration,
  262. .maxlen = sizeof(int),
  263. .mode = 0644,
  264. .proc_handler = &proc_dointvec_jiffies,
  265. },
  266. { .ctl_name = 0 }
  267. };
  268. static struct ctl_table_header * sysctl_header;
  269. /*
  270. * new/free a ip_vs_lblcr_entry, which is a mapping of a destination
  271. * IP address to a server.
  272. */
  273. static inline struct ip_vs_lblcr_entry *ip_vs_lblcr_new(__be32 daddr)
  274. {
  275. struct ip_vs_lblcr_entry *en;
  276. en = kmalloc(sizeof(struct ip_vs_lblcr_entry), GFP_ATOMIC);
  277. if (en == NULL) {
  278. IP_VS_ERR("ip_vs_lblcr_new(): no memory\n");
  279. return NULL;
  280. }
  281. INIT_LIST_HEAD(&en->list);
  282. en->addr = daddr;
  283. /* initilize its dest set */
  284. atomic_set(&(en->set.size), 0);
  285. en->set.list = NULL;
  286. rwlock_init(&en->set.lock);
  287. return en;
  288. }
  289. static inline void ip_vs_lblcr_free(struct ip_vs_lblcr_entry *en)
  290. {
  291. list_del(&en->list);
  292. ip_vs_dest_set_eraseall(&en->set);
  293. kfree(en);
  294. }
  295. /*
  296. * Returns hash value for IPVS LBLCR entry
  297. */
  298. static inline unsigned ip_vs_lblcr_hashkey(__be32 addr)
  299. {
  300. return (ntohl(addr)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
  301. }
  302. /*
  303. * Hash an entry in the ip_vs_lblcr_table.
  304. * returns bool success.
  305. */
  306. static int
  307. ip_vs_lblcr_hash(struct ip_vs_lblcr_table *tbl, struct ip_vs_lblcr_entry *en)
  308. {
  309. unsigned hash;
  310. if (!list_empty(&en->list)) {
  311. IP_VS_ERR("ip_vs_lblcr_hash(): request for already hashed, "
  312. "called from %p\n", __builtin_return_address(0));
  313. return 0;
  314. }
  315. /*
  316. * Hash by destination IP address
  317. */
  318. hash = ip_vs_lblcr_hashkey(en->addr);
  319. write_lock(&tbl->lock);
  320. list_add(&en->list, &tbl->bucket[hash]);
  321. atomic_inc(&tbl->entries);
  322. write_unlock(&tbl->lock);
  323. return 1;
  324. }
  325. /*
  326. * Get ip_vs_lblcr_entry associated with supplied parameters.
  327. */
  328. static inline struct ip_vs_lblcr_entry *
  329. ip_vs_lblcr_get(struct ip_vs_lblcr_table *tbl, __be32 addr)
  330. {
  331. unsigned hash;
  332. struct ip_vs_lblcr_entry *en;
  333. hash = ip_vs_lblcr_hashkey(addr);
  334. read_lock(&tbl->lock);
  335. list_for_each_entry(en, &tbl->bucket[hash], list) {
  336. if (en->addr == addr) {
  337. /* HIT */
  338. read_unlock(&tbl->lock);
  339. return en;
  340. }
  341. }
  342. read_unlock(&tbl->lock);
  343. return NULL;
  344. }
  345. /*
  346. * Flush all the entries of the specified table.
  347. */
  348. static void ip_vs_lblcr_flush(struct ip_vs_lblcr_table *tbl)
  349. {
  350. int i;
  351. struct ip_vs_lblcr_entry *en, *nxt;
  352. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  353. write_lock(&tbl->lock);
  354. list_for_each_entry_safe(en, nxt, &tbl->bucket[i], list) {
  355. ip_vs_lblcr_free(en);
  356. atomic_dec(&tbl->entries);
  357. }
  358. write_unlock(&tbl->lock);
  359. }
  360. }
  361. static inline void ip_vs_lblcr_full_check(struct ip_vs_lblcr_table *tbl)
  362. {
  363. unsigned long now = jiffies;
  364. int i, j;
  365. struct ip_vs_lblcr_entry *en, *nxt;
  366. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  367. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  368. write_lock(&tbl->lock);
  369. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  370. if (time_after(en->lastuse+sysctl_ip_vs_lblcr_expiration,
  371. now))
  372. continue;
  373. ip_vs_lblcr_free(en);
  374. atomic_dec(&tbl->entries);
  375. }
  376. write_unlock(&tbl->lock);
  377. }
  378. tbl->rover = j;
  379. }
  380. /*
  381. * Periodical timer handler for IPVS lblcr table
  382. * It is used to collect stale entries when the number of entries
  383. * exceeds the maximum size of the table.
  384. *
  385. * Fixme: we probably need more complicated algorithm to collect
  386. * entries that have not been used for a long time even
  387. * if the number of entries doesn't exceed the maximum size
  388. * of the table.
  389. * The full expiration check is for this purpose now.
  390. */
  391. static void ip_vs_lblcr_check_expire(unsigned long data)
  392. {
  393. struct ip_vs_lblcr_table *tbl;
  394. unsigned long now = jiffies;
  395. int goal;
  396. int i, j;
  397. struct ip_vs_lblcr_entry *en, *nxt;
  398. tbl = (struct ip_vs_lblcr_table *)data;
  399. if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
  400. /* do full expiration check */
  401. ip_vs_lblcr_full_check(tbl);
  402. tbl->counter = 1;
  403. goto out;
  404. }
  405. if (atomic_read(&tbl->entries) <= tbl->max_size) {
  406. tbl->counter++;
  407. goto out;
  408. }
  409. goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
  410. if (goal > tbl->max_size/2)
  411. goal = tbl->max_size/2;
  412. for (i=0, j=tbl->rover; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  413. j = (j + 1) & IP_VS_LBLCR_TAB_MASK;
  414. write_lock(&tbl->lock);
  415. list_for_each_entry_safe(en, nxt, &tbl->bucket[j], list) {
  416. if (time_before(now, en->lastuse+ENTRY_TIMEOUT))
  417. continue;
  418. ip_vs_lblcr_free(en);
  419. atomic_dec(&tbl->entries);
  420. goal--;
  421. }
  422. write_unlock(&tbl->lock);
  423. if (goal <= 0)
  424. break;
  425. }
  426. tbl->rover = j;
  427. out:
  428. mod_timer(&tbl->periodic_timer, jiffies+CHECK_EXPIRE_INTERVAL);
  429. }
  430. static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
  431. {
  432. int i;
  433. struct ip_vs_lblcr_table *tbl;
  434. /*
  435. * Allocate the ip_vs_lblcr_table for this service
  436. */
  437. tbl = kmalloc(sizeof(struct ip_vs_lblcr_table), GFP_ATOMIC);
  438. if (tbl == NULL) {
  439. IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
  440. return -ENOMEM;
  441. }
  442. svc->sched_data = tbl;
  443. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
  444. "current service\n",
  445. sizeof(struct ip_vs_lblcr_table));
  446. /*
  447. * Initialize the hash buckets
  448. */
  449. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  450. INIT_LIST_HEAD(&tbl->bucket[i]);
  451. }
  452. rwlock_init(&tbl->lock);
  453. tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
  454. tbl->rover = 0;
  455. tbl->counter = 1;
  456. /*
  457. * Hook periodic timer for garbage collection
  458. */
  459. setup_timer(&tbl->periodic_timer, ip_vs_lblcr_check_expire,
  460. (unsigned long)tbl);
  461. tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
  462. add_timer(&tbl->periodic_timer);
  463. return 0;
  464. }
  465. static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
  466. {
  467. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  468. /* remove periodic timer */
  469. del_timer_sync(&tbl->periodic_timer);
  470. /* got to clean up table entries here */
  471. ip_vs_lblcr_flush(tbl);
  472. /* release the table itself */
  473. kfree(svc->sched_data);
  474. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
  475. sizeof(struct ip_vs_lblcr_table));
  476. return 0;
  477. }
  478. static int ip_vs_lblcr_update_svc(struct ip_vs_service *svc)
  479. {
  480. return 0;
  481. }
  482. static inline struct ip_vs_dest *
  483. __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
  484. {
  485. struct ip_vs_dest *dest, *least;
  486. int loh, doh;
  487. /*
  488. * We think the overhead of processing active connections is fifty
  489. * times higher than that of inactive connections in average. (This
  490. * fifty times might not be accurate, we will change it later.) We
  491. * use the following formula to estimate the overhead:
  492. * dest->activeconns*50 + dest->inactconns
  493. * and the load:
  494. * (dest overhead) / dest->weight
  495. *
  496. * Remember -- no floats in kernel mode!!!
  497. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  498. * h1/w1 > h2/w2
  499. * if every weight is larger than zero.
  500. *
  501. * The server with weight=0 is quiesced and will not receive any
  502. * new connection.
  503. */
  504. list_for_each_entry(dest, &svc->destinations, n_list) {
  505. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  506. continue;
  507. if (atomic_read(&dest->weight) > 0) {
  508. least = dest;
  509. loh = atomic_read(&least->activeconns) * 50
  510. + atomic_read(&least->inactconns);
  511. goto nextstage;
  512. }
  513. }
  514. return NULL;
  515. /*
  516. * Find the destination with the least load.
  517. */
  518. nextstage:
  519. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  520. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  521. continue;
  522. doh = atomic_read(&dest->activeconns) * 50
  523. + atomic_read(&dest->inactconns);
  524. if (loh * atomic_read(&dest->weight) >
  525. doh * atomic_read(&least->weight)) {
  526. least = dest;
  527. loh = doh;
  528. }
  529. }
  530. IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
  531. "activeconns %d refcnt %d weight %d overhead %d\n",
  532. NIPQUAD(least->addr), ntohs(least->port),
  533. atomic_read(&least->activeconns),
  534. atomic_read(&least->refcnt),
  535. atomic_read(&least->weight), loh);
  536. return least;
  537. }
  538. /*
  539. * If this destination server is overloaded and there is a less loaded
  540. * server, then return true.
  541. */
  542. static inline int
  543. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  544. {
  545. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  546. struct ip_vs_dest *d;
  547. list_for_each_entry(d, &svc->destinations, n_list) {
  548. if (atomic_read(&d->activeconns)*2
  549. < atomic_read(&d->weight)) {
  550. return 1;
  551. }
  552. }
  553. }
  554. return 0;
  555. }
  556. /*
  557. * Locality-Based (weighted) Least-Connection scheduling
  558. */
  559. static struct ip_vs_dest *
  560. ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  561. {
  562. struct ip_vs_dest *dest;
  563. struct ip_vs_lblcr_table *tbl;
  564. struct ip_vs_lblcr_entry *en;
  565. struct iphdr *iph = ip_hdr(skb);
  566. IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
  567. tbl = (struct ip_vs_lblcr_table *)svc->sched_data;
  568. en = ip_vs_lblcr_get(tbl, iph->daddr);
  569. if (en == NULL) {
  570. dest = __ip_vs_wlc_schedule(svc, iph);
  571. if (dest == NULL) {
  572. IP_VS_DBG(1, "no destination available\n");
  573. return NULL;
  574. }
  575. en = ip_vs_lblcr_new(iph->daddr);
  576. if (en == NULL) {
  577. return NULL;
  578. }
  579. ip_vs_dest_set_insert(&en->set, dest);
  580. ip_vs_lblcr_hash(tbl, en);
  581. } else {
  582. dest = ip_vs_dest_set_min(&en->set);
  583. if (!dest || is_overloaded(dest, svc)) {
  584. dest = __ip_vs_wlc_schedule(svc, iph);
  585. if (dest == NULL) {
  586. IP_VS_DBG(1, "no destination available\n");
  587. return NULL;
  588. }
  589. ip_vs_dest_set_insert(&en->set, dest);
  590. }
  591. if (atomic_read(&en->set.size) > 1 &&
  592. jiffies-en->set.lastmod > sysctl_ip_vs_lblcr_expiration) {
  593. struct ip_vs_dest *m;
  594. m = ip_vs_dest_set_max(&en->set);
  595. if (m)
  596. ip_vs_dest_set_erase(&en->set, m);
  597. }
  598. }
  599. en->lastuse = jiffies;
  600. IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
  601. "--> server %u.%u.%u.%u:%d\n",
  602. NIPQUAD(en->addr),
  603. NIPQUAD(dest->addr),
  604. ntohs(dest->port));
  605. return dest;
  606. }
  607. /*
  608. * IPVS LBLCR Scheduler structure
  609. */
  610. static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
  611. {
  612. .name = "lblcr",
  613. .refcnt = ATOMIC_INIT(0),
  614. .module = THIS_MODULE,
  615. .init_service = ip_vs_lblcr_init_svc,
  616. .done_service = ip_vs_lblcr_done_svc,
  617. .update_service = ip_vs_lblcr_update_svc,
  618. .schedule = ip_vs_lblcr_schedule,
  619. };
  620. static int __init ip_vs_lblcr_init(void)
  621. {
  622. int ret;
  623. INIT_LIST_HEAD(&ip_vs_lblcr_scheduler.n_list);
  624. sysctl_header = register_sysctl_paths(net_vs_ctl_path, vs_vars_table);
  625. ret = register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  626. if (ret)
  627. unregister_sysctl_table(sysctl_header);
  628. return ret;
  629. }
  630. static void __exit ip_vs_lblcr_cleanup(void)
  631. {
  632. unregister_sysctl_table(sysctl_header);
  633. unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  634. }
  635. module_init(ip_vs_lblcr_init);
  636. module_exit(ip_vs_lblcr_cleanup);
  637. MODULE_LICENSE("GPL");