ip_vs_lblcr.c 21 KB

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