ip_vs_lblcr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. /* for sysctl */
  45. #include <linux/fs.h>
  46. #include <linux/sysctl.h>
  47. /* for proc_net_create/proc_net_remove */
  48. #include <linux/proc_fs.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. __u32 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(__u32 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(__u32 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, __u32 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. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  461. static struct ip_vs_lblcr_table *lblcr_table_list;
  462. /*
  463. * /proc/net/ip_vs_lblcr to display the mappings of
  464. * destination IP address <==> its serverSet
  465. */
  466. static int
  467. ip_vs_lblcr_getinfo(char *buffer, char **start, off_t offset, int length)
  468. {
  469. off_t pos=0, begin;
  470. int len=0, size;
  471. struct ip_vs_lblcr_table *tbl;
  472. unsigned long now = jiffies;
  473. int i;
  474. struct ip_vs_lblcr_entry *en;
  475. tbl = lblcr_table_list;
  476. size = sprintf(buffer, "LastTime Dest IP address Server set\n");
  477. pos += size;
  478. len += size;
  479. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  480. read_lock_bh(&tbl->lock);
  481. list_for_each_entry(en, &tbl->bucket[i], list) {
  482. char tbuf[16];
  483. struct ip_vs_dest_list *d;
  484. sprintf(tbuf, "%u.%u.%u.%u", NIPQUAD(en->addr));
  485. size = sprintf(buffer+len, "%8lu %-16s ",
  486. now-en->lastuse, tbuf);
  487. read_lock(&en->set.lock);
  488. for (d=en->set.list; d!=NULL; d=d->next) {
  489. size += sprintf(buffer+len+size,
  490. "%u.%u.%u.%u ",
  491. NIPQUAD(d->dest->addr));
  492. }
  493. read_unlock(&en->set.lock);
  494. size += sprintf(buffer+len+size, "\n");
  495. len += size;
  496. pos += size;
  497. if (pos <= offset)
  498. len=0;
  499. if (pos >= offset+length) {
  500. read_unlock_bh(&tbl->lock);
  501. goto done;
  502. }
  503. }
  504. read_unlock_bh(&tbl->lock);
  505. }
  506. done:
  507. begin = len - (pos - offset);
  508. *start = buffer + begin;
  509. len -= begin;
  510. if(len>length)
  511. len = length;
  512. return len;
  513. }
  514. #endif
  515. static int ip_vs_lblcr_init_svc(struct ip_vs_service *svc)
  516. {
  517. int i;
  518. struct ip_vs_lblcr_table *tbl;
  519. /*
  520. * Allocate the ip_vs_lblcr_table for this service
  521. */
  522. tbl = kmalloc(sizeof(struct ip_vs_lblcr_table), GFP_ATOMIC);
  523. if (tbl == NULL) {
  524. IP_VS_ERR("ip_vs_lblcr_init_svc(): no memory\n");
  525. return -ENOMEM;
  526. }
  527. svc->sched_data = tbl;
  528. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) allocated for "
  529. "current service\n",
  530. sizeof(struct ip_vs_lblcr_table));
  531. /*
  532. * Initialize the hash buckets
  533. */
  534. for (i=0; i<IP_VS_LBLCR_TAB_SIZE; i++) {
  535. INIT_LIST_HEAD(&tbl->bucket[i]);
  536. }
  537. rwlock_init(&tbl->lock);
  538. tbl->max_size = IP_VS_LBLCR_TAB_SIZE*16;
  539. tbl->rover = 0;
  540. tbl->counter = 1;
  541. /*
  542. * Hook periodic timer for garbage collection
  543. */
  544. init_timer(&tbl->periodic_timer);
  545. tbl->periodic_timer.data = (unsigned long)tbl;
  546. tbl->periodic_timer.function = ip_vs_lblcr_check_expire;
  547. tbl->periodic_timer.expires = jiffies+CHECK_EXPIRE_INTERVAL;
  548. add_timer(&tbl->periodic_timer);
  549. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  550. lblcr_table_list = tbl;
  551. #endif
  552. return 0;
  553. }
  554. static int ip_vs_lblcr_done_svc(struct ip_vs_service *svc)
  555. {
  556. struct ip_vs_lblcr_table *tbl = svc->sched_data;
  557. /* remove periodic timer */
  558. del_timer_sync(&tbl->periodic_timer);
  559. /* got to clean up table entries here */
  560. ip_vs_lblcr_flush(tbl);
  561. /* release the table itself */
  562. kfree(svc->sched_data);
  563. IP_VS_DBG(6, "LBLCR hash table (memory=%Zdbytes) released\n",
  564. sizeof(struct ip_vs_lblcr_table));
  565. return 0;
  566. }
  567. static int ip_vs_lblcr_update_svc(struct ip_vs_service *svc)
  568. {
  569. return 0;
  570. }
  571. static inline struct ip_vs_dest *
  572. __ip_vs_wlc_schedule(struct ip_vs_service *svc, struct iphdr *iph)
  573. {
  574. struct ip_vs_dest *dest, *least;
  575. int loh, doh;
  576. /*
  577. * We think the overhead of processing active connections is fifty
  578. * times higher than that of inactive connections in average. (This
  579. * fifty times might not be accurate, we will change it later.) We
  580. * use the following formula to estimate the overhead:
  581. * dest->activeconns*50 + dest->inactconns
  582. * and the load:
  583. * (dest overhead) / dest->weight
  584. *
  585. * Remember -- no floats in kernel mode!!!
  586. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  587. * h1/w1 > h2/w2
  588. * if every weight is larger than zero.
  589. *
  590. * The server with weight=0 is quiesced and will not receive any
  591. * new connection.
  592. */
  593. list_for_each_entry(dest, &svc->destinations, n_list) {
  594. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  595. continue;
  596. if (atomic_read(&dest->weight) > 0) {
  597. least = dest;
  598. loh = atomic_read(&least->activeconns) * 50
  599. + atomic_read(&least->inactconns);
  600. goto nextstage;
  601. }
  602. }
  603. return NULL;
  604. /*
  605. * Find the destination with the least load.
  606. */
  607. nextstage:
  608. list_for_each_entry_continue(dest, &svc->destinations, n_list) {
  609. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  610. continue;
  611. doh = atomic_read(&dest->activeconns) * 50
  612. + atomic_read(&dest->inactconns);
  613. if (loh * atomic_read(&dest->weight) >
  614. doh * atomic_read(&least->weight)) {
  615. least = dest;
  616. loh = doh;
  617. }
  618. }
  619. IP_VS_DBG(6, "LBLCR: server %d.%d.%d.%d:%d "
  620. "activeconns %d refcnt %d weight %d overhead %d\n",
  621. NIPQUAD(least->addr), ntohs(least->port),
  622. atomic_read(&least->activeconns),
  623. atomic_read(&least->refcnt),
  624. atomic_read(&least->weight), loh);
  625. return least;
  626. }
  627. /*
  628. * If this destination server is overloaded and there is a less loaded
  629. * server, then return true.
  630. */
  631. static inline int
  632. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  633. {
  634. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  635. struct ip_vs_dest *d;
  636. list_for_each_entry(d, &svc->destinations, n_list) {
  637. if (atomic_read(&d->activeconns)*2
  638. < atomic_read(&d->weight)) {
  639. return 1;
  640. }
  641. }
  642. }
  643. return 0;
  644. }
  645. /*
  646. * Locality-Based (weighted) Least-Connection scheduling
  647. */
  648. static struct ip_vs_dest *
  649. ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  650. {
  651. struct ip_vs_dest *dest;
  652. struct ip_vs_lblcr_table *tbl;
  653. struct ip_vs_lblcr_entry *en;
  654. struct iphdr *iph = skb->nh.iph;
  655. IP_VS_DBG(6, "ip_vs_lblcr_schedule(): Scheduling...\n");
  656. tbl = (struct ip_vs_lblcr_table *)svc->sched_data;
  657. en = ip_vs_lblcr_get(tbl, iph->daddr);
  658. if (en == NULL) {
  659. dest = __ip_vs_wlc_schedule(svc, iph);
  660. if (dest == NULL) {
  661. IP_VS_DBG(1, "no destination available\n");
  662. return NULL;
  663. }
  664. en = ip_vs_lblcr_new(iph->daddr);
  665. if (en == NULL) {
  666. return NULL;
  667. }
  668. ip_vs_dest_set_insert(&en->set, dest);
  669. ip_vs_lblcr_hash(tbl, en);
  670. } else {
  671. dest = ip_vs_dest_set_min(&en->set);
  672. if (!dest || is_overloaded(dest, svc)) {
  673. dest = __ip_vs_wlc_schedule(svc, iph);
  674. if (dest == NULL) {
  675. IP_VS_DBG(1, "no destination available\n");
  676. return NULL;
  677. }
  678. ip_vs_dest_set_insert(&en->set, dest);
  679. }
  680. if (atomic_read(&en->set.size) > 1 &&
  681. jiffies-en->set.lastmod > sysctl_ip_vs_lblcr_expiration) {
  682. struct ip_vs_dest *m;
  683. m = ip_vs_dest_set_max(&en->set);
  684. if (m)
  685. ip_vs_dest_set_erase(&en->set, m);
  686. }
  687. }
  688. en->lastuse = jiffies;
  689. IP_VS_DBG(6, "LBLCR: destination IP address %u.%u.%u.%u "
  690. "--> server %u.%u.%u.%u:%d\n",
  691. NIPQUAD(en->addr),
  692. NIPQUAD(dest->addr),
  693. ntohs(dest->port));
  694. return dest;
  695. }
  696. /*
  697. * IPVS LBLCR Scheduler structure
  698. */
  699. static struct ip_vs_scheduler ip_vs_lblcr_scheduler =
  700. {
  701. .name = "lblcr",
  702. .refcnt = ATOMIC_INIT(0),
  703. .module = THIS_MODULE,
  704. .init_service = ip_vs_lblcr_init_svc,
  705. .done_service = ip_vs_lblcr_done_svc,
  706. .update_service = ip_vs_lblcr_update_svc,
  707. .schedule = ip_vs_lblcr_schedule,
  708. };
  709. static int __init ip_vs_lblcr_init(void)
  710. {
  711. INIT_LIST_HEAD(&ip_vs_lblcr_scheduler.n_list);
  712. sysctl_header = register_sysctl_table(lblcr_root_table, 0);
  713. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  714. proc_net_create("ip_vs_lblcr", 0, ip_vs_lblcr_getinfo);
  715. #endif
  716. return register_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  717. }
  718. static void __exit ip_vs_lblcr_cleanup(void)
  719. {
  720. #ifdef CONFIG_IP_VS_LBLCR_DEBUG
  721. proc_net_remove("ip_vs_lblcr");
  722. #endif
  723. unregister_sysctl_table(sysctl_header);
  724. unregister_ip_vs_scheduler(&ip_vs_lblcr_scheduler);
  725. }
  726. module_init(ip_vs_lblcr_init);
  727. module_exit(ip_vs_lblcr_cleanup);
  728. MODULE_LICENSE("GPL");