ip_vs_lblc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * IPVS: Locality-Based Least-Connection scheduling module
  3. *
  4. * Authors: Wensong Zhang <wensong@gnuchina.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Changes:
  12. * Martin Hamilton : fixed the terrible locking bugs
  13. * *lock(tbl->lock) ==> *lock(&tbl->lock)
  14. * Wensong Zhang : fixed the uninitialized tbl->lock bug
  15. * Wensong Zhang : added doing full expiration check to
  16. * collect stale entries of 24+ hours when
  17. * no partial expire check in a half hour
  18. * Julian Anastasov : replaced del_timer call with del_timer_sync
  19. * to avoid the possible race between timer
  20. * handler and del_timer thread in SMP
  21. *
  22. */
  23. /*
  24. * The lblc algorithm is as follows (pseudo code):
  25. *
  26. * if cachenode[dest_ip] is null then
  27. * n, cachenode[dest_ip] <- {weighted least-conn node};
  28. * else
  29. * n <- cachenode[dest_ip];
  30. * if (n is dead) OR
  31. * (n.conns>n.weight AND
  32. * there is a node m with m.conns<m.weight/2) then
  33. * n, cachenode[dest_ip] <- {weighted least-conn node};
  34. *
  35. * return n;
  36. *
  37. * Thanks must go to Wenzhuo Zhang for talking WCCP to me and pushing
  38. * me to write this module.
  39. */
  40. #define KMSG_COMPONENT "IPVS"
  41. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  42. #include <linux/ip.h>
  43. #include <linux/slab.h>
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/jiffies.h>
  48. /* for sysctl */
  49. #include <linux/fs.h>
  50. #include <linux/sysctl.h>
  51. #include <net/ip_vs.h>
  52. /*
  53. * It is for garbage collection of stale IPVS lblc entries,
  54. * when the table is full.
  55. */
  56. #define CHECK_EXPIRE_INTERVAL (60*HZ)
  57. #define ENTRY_TIMEOUT (6*60*HZ)
  58. #define DEFAULT_EXPIRATION (24*60*60*HZ)
  59. /*
  60. * It is for full expiration check.
  61. * When there is no partial expiration check (garbage collection)
  62. * in a half hour, do a full expiration check to collect stale
  63. * entries that haven't been touched for a day.
  64. */
  65. #define COUNT_FOR_FULL_EXPIRATION 30
  66. /*
  67. * for IPVS lblc entry hash table
  68. */
  69. #ifndef CONFIG_IP_VS_LBLC_TAB_BITS
  70. #define CONFIG_IP_VS_LBLC_TAB_BITS 10
  71. #endif
  72. #define IP_VS_LBLC_TAB_BITS CONFIG_IP_VS_LBLC_TAB_BITS
  73. #define IP_VS_LBLC_TAB_SIZE (1 << IP_VS_LBLC_TAB_BITS)
  74. #define IP_VS_LBLC_TAB_MASK (IP_VS_LBLC_TAB_SIZE - 1)
  75. /*
  76. * IPVS lblc entry represents an association between destination
  77. * IP address and its destination server
  78. */
  79. struct ip_vs_lblc_entry {
  80. struct hlist_node list;
  81. int af; /* address family */
  82. union nf_inet_addr addr; /* destination IP address */
  83. struct ip_vs_dest *dest; /* real server (cache) */
  84. unsigned long lastuse; /* last used time */
  85. struct rcu_head rcu_head;
  86. };
  87. /*
  88. * IPVS lblc hash table
  89. */
  90. struct ip_vs_lblc_table {
  91. struct rcu_head rcu_head;
  92. struct hlist_head bucket[IP_VS_LBLC_TAB_SIZE]; /* hash bucket */
  93. struct timer_list periodic_timer; /* collect stale entries */
  94. atomic_t entries; /* number of entries */
  95. int max_size; /* maximum size of entries */
  96. int rover; /* rover for expire check */
  97. int counter; /* counter for no expire */
  98. bool dead;
  99. };
  100. /*
  101. * IPVS LBLC sysctl table
  102. */
  103. #ifdef CONFIG_SYSCTL
  104. static struct ctl_table vs_vars_table[] = {
  105. {
  106. .procname = "lblc_expiration",
  107. .data = NULL,
  108. .maxlen = sizeof(int),
  109. .mode = 0644,
  110. .proc_handler = proc_dointvec_jiffies,
  111. },
  112. { }
  113. };
  114. #endif
  115. static void ip_vs_lblc_rcu_free(struct rcu_head *head)
  116. {
  117. struct ip_vs_lblc_entry *en = container_of(head,
  118. struct ip_vs_lblc_entry,
  119. rcu_head);
  120. ip_vs_dest_put_and_free(en->dest);
  121. kfree(en);
  122. }
  123. static inline void ip_vs_lblc_del(struct ip_vs_lblc_entry *en)
  124. {
  125. hlist_del_rcu(&en->list);
  126. call_rcu(&en->rcu_head, ip_vs_lblc_rcu_free);
  127. }
  128. /*
  129. * Returns hash value for IPVS LBLC entry
  130. */
  131. static inline unsigned int
  132. ip_vs_lblc_hashkey(int af, const union nf_inet_addr *addr)
  133. {
  134. __be32 addr_fold = addr->ip;
  135. #ifdef CONFIG_IP_VS_IPV6
  136. if (af == AF_INET6)
  137. addr_fold = addr->ip6[0]^addr->ip6[1]^
  138. addr->ip6[2]^addr->ip6[3];
  139. #endif
  140. return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
  141. }
  142. /*
  143. * Hash an entry in the ip_vs_lblc_table.
  144. * returns bool success.
  145. */
  146. static void
  147. ip_vs_lblc_hash(struct ip_vs_lblc_table *tbl, struct ip_vs_lblc_entry *en)
  148. {
  149. unsigned int hash = ip_vs_lblc_hashkey(en->af, &en->addr);
  150. hlist_add_head_rcu(&en->list, &tbl->bucket[hash]);
  151. atomic_inc(&tbl->entries);
  152. }
  153. /* Get ip_vs_lblc_entry associated with supplied parameters. */
  154. static inline struct ip_vs_lblc_entry *
  155. ip_vs_lblc_get(int af, struct ip_vs_lblc_table *tbl,
  156. const union nf_inet_addr *addr)
  157. {
  158. unsigned int hash = ip_vs_lblc_hashkey(af, addr);
  159. struct ip_vs_lblc_entry *en;
  160. hlist_for_each_entry_rcu(en, &tbl->bucket[hash], list)
  161. if (ip_vs_addr_equal(af, &en->addr, addr))
  162. return en;
  163. return NULL;
  164. }
  165. /*
  166. * Create or update an ip_vs_lblc_entry, which is a mapping of a destination IP
  167. * address to a server. Called under spin lock.
  168. */
  169. static inline struct ip_vs_lblc_entry *
  170. ip_vs_lblc_new(struct ip_vs_lblc_table *tbl, const union nf_inet_addr *daddr,
  171. u16 af, struct ip_vs_dest *dest)
  172. {
  173. struct ip_vs_lblc_entry *en;
  174. en = ip_vs_lblc_get(af, tbl, daddr);
  175. if (en) {
  176. if (en->dest == dest)
  177. return en;
  178. ip_vs_lblc_del(en);
  179. }
  180. en = kmalloc(sizeof(*en), GFP_ATOMIC);
  181. if (!en)
  182. return NULL;
  183. en->af = af;
  184. ip_vs_addr_copy(af, &en->addr, daddr);
  185. en->lastuse = jiffies;
  186. ip_vs_dest_hold(dest);
  187. en->dest = dest;
  188. ip_vs_lblc_hash(tbl, en);
  189. return en;
  190. }
  191. /*
  192. * Flush all the entries of the specified table.
  193. */
  194. static void ip_vs_lblc_flush(struct ip_vs_service *svc)
  195. {
  196. struct ip_vs_lblc_table *tbl = svc->sched_data;
  197. struct ip_vs_lblc_entry *en;
  198. struct hlist_node *next;
  199. int i;
  200. spin_lock_bh(&svc->sched_lock);
  201. tbl->dead = 1;
  202. for (i = 0; i < IP_VS_LBLC_TAB_SIZE; i++) {
  203. hlist_for_each_entry_safe(en, next, &tbl->bucket[i], list) {
  204. ip_vs_lblc_del(en);
  205. atomic_dec(&tbl->entries);
  206. }
  207. }
  208. spin_unlock_bh(&svc->sched_lock);
  209. }
  210. static int sysctl_lblc_expiration(struct ip_vs_service *svc)
  211. {
  212. #ifdef CONFIG_SYSCTL
  213. return svc->ipvs->sysctl_lblc_expiration;
  214. #else
  215. return DEFAULT_EXPIRATION;
  216. #endif
  217. }
  218. static inline void ip_vs_lblc_full_check(struct ip_vs_service *svc)
  219. {
  220. struct ip_vs_lblc_table *tbl = svc->sched_data;
  221. struct ip_vs_lblc_entry *en;
  222. struct hlist_node *next;
  223. unsigned long now = jiffies;
  224. int i, j;
  225. for (i = 0, j = tbl->rover; i < IP_VS_LBLC_TAB_SIZE; i++) {
  226. j = (j + 1) & IP_VS_LBLC_TAB_MASK;
  227. spin_lock(&svc->sched_lock);
  228. hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
  229. if (time_before(now,
  230. en->lastuse +
  231. sysctl_lblc_expiration(svc)))
  232. continue;
  233. ip_vs_lblc_del(en);
  234. atomic_dec(&tbl->entries);
  235. }
  236. spin_unlock(&svc->sched_lock);
  237. }
  238. tbl->rover = j;
  239. }
  240. /*
  241. * Periodical timer handler for IPVS lblc table
  242. * It is used to collect stale entries when the number of entries
  243. * exceeds the maximum size of the table.
  244. *
  245. * Fixme: we probably need more complicated algorithm to collect
  246. * entries that have not been used for a long time even
  247. * if the number of entries doesn't exceed the maximum size
  248. * of the table.
  249. * The full expiration check is for this purpose now.
  250. */
  251. static void ip_vs_lblc_check_expire(unsigned long data)
  252. {
  253. struct ip_vs_service *svc = (struct ip_vs_service *) data;
  254. struct ip_vs_lblc_table *tbl = svc->sched_data;
  255. unsigned long now = jiffies;
  256. int goal;
  257. int i, j;
  258. struct ip_vs_lblc_entry *en;
  259. struct hlist_node *next;
  260. if ((tbl->counter % COUNT_FOR_FULL_EXPIRATION) == 0) {
  261. /* do full expiration check */
  262. ip_vs_lblc_full_check(svc);
  263. tbl->counter = 1;
  264. goto out;
  265. }
  266. if (atomic_read(&tbl->entries) <= tbl->max_size) {
  267. tbl->counter++;
  268. goto out;
  269. }
  270. goal = (atomic_read(&tbl->entries) - tbl->max_size)*4/3;
  271. if (goal > tbl->max_size/2)
  272. goal = tbl->max_size/2;
  273. for (i = 0, j = tbl->rover; i < IP_VS_LBLC_TAB_SIZE; i++) {
  274. j = (j + 1) & IP_VS_LBLC_TAB_MASK;
  275. spin_lock(&svc->sched_lock);
  276. hlist_for_each_entry_safe(en, next, &tbl->bucket[j], list) {
  277. if (time_before(now, en->lastuse + ENTRY_TIMEOUT))
  278. continue;
  279. ip_vs_lblc_del(en);
  280. atomic_dec(&tbl->entries);
  281. goal--;
  282. }
  283. spin_unlock(&svc->sched_lock);
  284. if (goal <= 0)
  285. break;
  286. }
  287. tbl->rover = j;
  288. out:
  289. mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
  290. }
  291. static int ip_vs_lblc_init_svc(struct ip_vs_service *svc)
  292. {
  293. int i;
  294. struct ip_vs_lblc_table *tbl;
  295. /*
  296. * Allocate the ip_vs_lblc_table for this service
  297. */
  298. tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
  299. if (tbl == NULL)
  300. return -ENOMEM;
  301. svc->sched_data = tbl;
  302. IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) allocated for "
  303. "current service\n", sizeof(*tbl));
  304. /*
  305. * Initialize the hash buckets
  306. */
  307. for (i = 0; i < IP_VS_LBLC_TAB_SIZE; i++) {
  308. INIT_HLIST_HEAD(&tbl->bucket[i]);
  309. }
  310. tbl->max_size = IP_VS_LBLC_TAB_SIZE*16;
  311. tbl->rover = 0;
  312. tbl->counter = 1;
  313. tbl->dead = 0;
  314. /*
  315. * Hook periodic timer for garbage collection
  316. */
  317. setup_timer(&tbl->periodic_timer, ip_vs_lblc_check_expire,
  318. (unsigned long)svc);
  319. mod_timer(&tbl->periodic_timer, jiffies + CHECK_EXPIRE_INTERVAL);
  320. return 0;
  321. }
  322. static void ip_vs_lblc_done_svc(struct ip_vs_service *svc)
  323. {
  324. struct ip_vs_lblc_table *tbl = svc->sched_data;
  325. /* remove periodic timer */
  326. del_timer_sync(&tbl->periodic_timer);
  327. /* got to clean up table entries here */
  328. ip_vs_lblc_flush(svc);
  329. /* release the table itself */
  330. kfree_rcu(tbl, rcu_head);
  331. IP_VS_DBG(6, "LBLC hash table (memory=%Zdbytes) released\n",
  332. sizeof(*tbl));
  333. }
  334. static inline struct ip_vs_dest *
  335. __ip_vs_lblc_schedule(struct ip_vs_service *svc)
  336. {
  337. struct ip_vs_dest *dest, *least;
  338. int loh, doh;
  339. /*
  340. * We use the following formula to estimate the load:
  341. * (dest overhead) / dest->weight
  342. *
  343. * Remember -- no floats in kernel mode!!!
  344. * The comparison of h1*w2 > h2*w1 is equivalent to that of
  345. * h1/w1 > h2/w2
  346. * if every weight is larger than zero.
  347. *
  348. * The server with weight=0 is quiesced and will not receive any
  349. * new connection.
  350. */
  351. list_for_each_entry_rcu(dest, &svc->destinations, n_list) {
  352. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  353. continue;
  354. if (atomic_read(&dest->weight) > 0) {
  355. least = dest;
  356. loh = ip_vs_dest_conn_overhead(least);
  357. goto nextstage;
  358. }
  359. }
  360. return NULL;
  361. /*
  362. * Find the destination with the least load.
  363. */
  364. nextstage:
  365. list_for_each_entry_continue_rcu(dest, &svc->destinations, n_list) {
  366. if (dest->flags & IP_VS_DEST_F_OVERLOAD)
  367. continue;
  368. doh = ip_vs_dest_conn_overhead(dest);
  369. if ((__s64)loh * atomic_read(&dest->weight) >
  370. (__s64)doh * atomic_read(&least->weight)) {
  371. least = dest;
  372. loh = doh;
  373. }
  374. }
  375. IP_VS_DBG_BUF(6, "LBLC: server %s:%d "
  376. "activeconns %d refcnt %d weight %d overhead %d\n",
  377. IP_VS_DBG_ADDR(least->af, &least->addr),
  378. ntohs(least->port),
  379. atomic_read(&least->activeconns),
  380. atomic_read(&least->refcnt),
  381. atomic_read(&least->weight), loh);
  382. return least;
  383. }
  384. /*
  385. * If this destination server is overloaded and there is a less loaded
  386. * server, then return true.
  387. */
  388. static inline int
  389. is_overloaded(struct ip_vs_dest *dest, struct ip_vs_service *svc)
  390. {
  391. if (atomic_read(&dest->activeconns) > atomic_read(&dest->weight)) {
  392. struct ip_vs_dest *d;
  393. list_for_each_entry_rcu(d, &svc->destinations, n_list) {
  394. if (atomic_read(&d->activeconns)*2
  395. < atomic_read(&d->weight)) {
  396. return 1;
  397. }
  398. }
  399. }
  400. return 0;
  401. }
  402. /*
  403. * Locality-Based (weighted) Least-Connection scheduling
  404. */
  405. static struct ip_vs_dest *
  406. ip_vs_lblc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
  407. struct ip_vs_iphdr *iph)
  408. {
  409. struct ip_vs_lblc_table *tbl = svc->sched_data;
  410. struct ip_vs_dest *dest = NULL;
  411. struct ip_vs_lblc_entry *en;
  412. IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
  413. /* First look in our cache */
  414. en = ip_vs_lblc_get(svc->af, tbl, &iph->daddr);
  415. if (en) {
  416. /* We only hold a read lock, but this is atomic */
  417. en->lastuse = jiffies;
  418. /*
  419. * If the destination is not available, i.e. it's in the trash,
  420. * we must ignore it, as it may be removed from under our feet,
  421. * if someone drops our reference count. Our caller only makes
  422. * sure that destinations, that are not in the trash, are not
  423. * moved to the trash, while we are scheduling. But anyone can
  424. * free up entries from the trash at any time.
  425. */
  426. dest = en->dest;
  427. if ((dest->flags & IP_VS_DEST_F_AVAILABLE) &&
  428. atomic_read(&dest->weight) > 0 && !is_overloaded(dest, svc))
  429. goto out;
  430. }
  431. /* No cache entry or it is invalid, time to schedule */
  432. dest = __ip_vs_lblc_schedule(svc);
  433. if (!dest) {
  434. ip_vs_scheduler_err(svc, "no destination available");
  435. return NULL;
  436. }
  437. /* If we fail to create a cache entry, we'll just use the valid dest */
  438. spin_lock_bh(&svc->sched_lock);
  439. if (!tbl->dead)
  440. ip_vs_lblc_new(tbl, &iph->daddr, svc->af, dest);
  441. spin_unlock_bh(&svc->sched_lock);
  442. out:
  443. IP_VS_DBG_BUF(6, "LBLC: destination IP address %s --> server %s:%d\n",
  444. IP_VS_DBG_ADDR(svc->af, &iph->daddr),
  445. IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port));
  446. return dest;
  447. }
  448. /*
  449. * IPVS LBLC Scheduler structure
  450. */
  451. static struct ip_vs_scheduler ip_vs_lblc_scheduler = {
  452. .name = "lblc",
  453. .refcnt = ATOMIC_INIT(0),
  454. .module = THIS_MODULE,
  455. .n_list = LIST_HEAD_INIT(ip_vs_lblc_scheduler.n_list),
  456. .init_service = ip_vs_lblc_init_svc,
  457. .done_service = ip_vs_lblc_done_svc,
  458. .schedule = ip_vs_lblc_schedule,
  459. };
  460. /*
  461. * per netns init.
  462. */
  463. #ifdef CONFIG_SYSCTL
  464. static int __net_init __ip_vs_lblc_init(struct net *net)
  465. {
  466. struct netns_ipvs *ipvs = net_ipvs(net);
  467. if (!ipvs)
  468. return -ENOENT;
  469. if (!net_eq(net, &init_net)) {
  470. ipvs->lblc_ctl_table = kmemdup(vs_vars_table,
  471. sizeof(vs_vars_table),
  472. GFP_KERNEL);
  473. if (ipvs->lblc_ctl_table == NULL)
  474. return -ENOMEM;
  475. /* Don't export sysctls to unprivileged users */
  476. if (net->user_ns != &init_user_ns)
  477. ipvs->lblc_ctl_table[0].procname = NULL;
  478. } else
  479. ipvs->lblc_ctl_table = vs_vars_table;
  480. ipvs->sysctl_lblc_expiration = DEFAULT_EXPIRATION;
  481. ipvs->lblc_ctl_table[0].data = &ipvs->sysctl_lblc_expiration;
  482. ipvs->lblc_ctl_header =
  483. register_net_sysctl(net, "net/ipv4/vs", ipvs->lblc_ctl_table);
  484. if (!ipvs->lblc_ctl_header) {
  485. if (!net_eq(net, &init_net))
  486. kfree(ipvs->lblc_ctl_table);
  487. return -ENOMEM;
  488. }
  489. return 0;
  490. }
  491. static void __net_exit __ip_vs_lblc_exit(struct net *net)
  492. {
  493. struct netns_ipvs *ipvs = net_ipvs(net);
  494. unregister_net_sysctl_table(ipvs->lblc_ctl_header);
  495. if (!net_eq(net, &init_net))
  496. kfree(ipvs->lblc_ctl_table);
  497. }
  498. #else
  499. static int __net_init __ip_vs_lblc_init(struct net *net) { return 0; }
  500. static void __net_exit __ip_vs_lblc_exit(struct net *net) { }
  501. #endif
  502. static struct pernet_operations ip_vs_lblc_ops = {
  503. .init = __ip_vs_lblc_init,
  504. .exit = __ip_vs_lblc_exit,
  505. };
  506. static int __init ip_vs_lblc_init(void)
  507. {
  508. int ret;
  509. ret = register_pernet_subsys(&ip_vs_lblc_ops);
  510. if (ret)
  511. return ret;
  512. ret = register_ip_vs_scheduler(&ip_vs_lblc_scheduler);
  513. if (ret)
  514. unregister_pernet_subsys(&ip_vs_lblc_ops);
  515. return ret;
  516. }
  517. static void __exit ip_vs_lblc_cleanup(void)
  518. {
  519. unregister_ip_vs_scheduler(&ip_vs_lblc_scheduler);
  520. unregister_pernet_subsys(&ip_vs_lblc_ops);
  521. rcu_barrier();
  522. }
  523. module_init(ip_vs_lblc_init);
  524. module_exit(ip_vs_lblc_cleanup);
  525. MODULE_LICENSE("GPL");