ip_vs_lblcr.c 21 KB

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