ip_vs_lblcr.c 19 KB

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