ip_vs_lblcr.c 19 KB

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