ip_vs_lblcr.c 19 KB

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