ip_vs_lblcr.c 19 KB

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