ip_vs_rr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * IPVS: Round-Robin Scheduling module
  3. *
  4. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  5. * Peter Kese <peter.kese@ijs.si>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Fixes/Changes:
  13. * Wensong Zhang : changed the ip_vs_rr_schedule to return dest
  14. * Julian Anastasov : fixed the NULL pointer access bug in debugging
  15. * Wensong Zhang : changed some comestics things for debugging
  16. * Wensong Zhang : changed for the d-linked destination list
  17. * Wensong Zhang : added the ip_vs_rr_update_svc
  18. * Wensong Zhang : added any dest with weight=0 is quiesced
  19. *
  20. */
  21. #define KMSG_COMPONENT "IPVS"
  22. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <net/ip_vs.h>
  26. static int ip_vs_rr_init_svc(struct ip_vs_service *svc)
  27. {
  28. svc->sched_data = &svc->destinations;
  29. return 0;
  30. }
  31. static int ip_vs_rr_del_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest)
  32. {
  33. struct list_head *p;
  34. spin_lock_bh(&svc->sched_lock);
  35. p = (struct list_head *) svc->sched_data;
  36. /* dest is already unlinked, so p->prev is not valid but
  37. * p->next is valid, use it to reach previous entry.
  38. */
  39. if (p == &dest->n_list)
  40. svc->sched_data = p->next->prev;
  41. spin_unlock_bh(&svc->sched_lock);
  42. return 0;
  43. }
  44. /*
  45. * Round-Robin Scheduling
  46. */
  47. static struct ip_vs_dest *
  48. ip_vs_rr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb,
  49. struct ip_vs_iphdr *iph)
  50. {
  51. struct list_head *p;
  52. struct ip_vs_dest *dest, *last;
  53. int pass = 0;
  54. IP_VS_DBG(6, "%s(): Scheduling...\n", __func__);
  55. spin_lock_bh(&svc->sched_lock);
  56. p = (struct list_head *) svc->sched_data;
  57. last = dest = list_entry(p, struct ip_vs_dest, n_list);
  58. do {
  59. list_for_each_entry_continue_rcu(dest,
  60. &svc->destinations,
  61. n_list) {
  62. if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
  63. atomic_read(&dest->weight) > 0)
  64. /* HIT */
  65. goto out;
  66. if (dest == last)
  67. goto stop;
  68. }
  69. pass++;
  70. /* Previous dest could be unlinked, do not loop forever.
  71. * If we stay at head there is no need for 2nd pass.
  72. */
  73. } while (pass < 2 && p != &svc->destinations);
  74. stop:
  75. spin_unlock_bh(&svc->sched_lock);
  76. ip_vs_scheduler_err(svc, "no destination available");
  77. return NULL;
  78. out:
  79. svc->sched_data = &dest->n_list;
  80. spin_unlock_bh(&svc->sched_lock);
  81. IP_VS_DBG_BUF(6, "RR: server %s:%u "
  82. "activeconns %d refcnt %d weight %d\n",
  83. IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port),
  84. atomic_read(&dest->activeconns),
  85. atomic_read(&dest->refcnt), atomic_read(&dest->weight));
  86. return dest;
  87. }
  88. static struct ip_vs_scheduler ip_vs_rr_scheduler = {
  89. .name = "rr", /* name */
  90. .refcnt = ATOMIC_INIT(0),
  91. .module = THIS_MODULE,
  92. .n_list = LIST_HEAD_INIT(ip_vs_rr_scheduler.n_list),
  93. .init_service = ip_vs_rr_init_svc,
  94. .add_dest = NULL,
  95. .del_dest = ip_vs_rr_del_dest,
  96. .schedule = ip_vs_rr_schedule,
  97. };
  98. static int __init ip_vs_rr_init(void)
  99. {
  100. return register_ip_vs_scheduler(&ip_vs_rr_scheduler);
  101. }
  102. static void __exit ip_vs_rr_cleanup(void)
  103. {
  104. unregister_ip_vs_scheduler(&ip_vs_rr_scheduler);
  105. synchronize_rcu();
  106. }
  107. module_init(ip_vs_rr_init);
  108. module_exit(ip_vs_rr_cleanup);
  109. MODULE_LICENSE("GPL");