ip_vs_sched.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * IPVS An implementation of the IP virtual server support for the
  3. * LINUX operating system. IPVS is now implemented as a module
  4. * over the Netfilter framework. IPVS can be used to build a
  5. * high-performance and highly available server based on a
  6. * cluster of servers.
  7. *
  8. * Version: $Id: ip_vs_sched.c,v 1.13 2003/05/10 03:05:23 wensong Exp $
  9. *
  10. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  11. * Peter Kese <peter.kese@ijs.si>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. * Changes:
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/string.h>
  25. #include <linux/kmod.h>
  26. #include <net/ip_vs.h>
  27. /*
  28. * IPVS scheduler list
  29. */
  30. static LIST_HEAD(ip_vs_schedulers);
  31. /* lock for service table */
  32. static DEFINE_RWLOCK(__ip_vs_sched_lock);
  33. /*
  34. * Bind a service with a scheduler
  35. */
  36. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  37. struct ip_vs_scheduler *scheduler)
  38. {
  39. int ret;
  40. if (svc == NULL) {
  41. IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
  42. return -EINVAL;
  43. }
  44. if (scheduler == NULL) {
  45. IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
  46. return -EINVAL;
  47. }
  48. svc->scheduler = scheduler;
  49. if (scheduler->init_service) {
  50. ret = scheduler->init_service(svc);
  51. if (ret) {
  52. IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
  53. return ret;
  54. }
  55. }
  56. return 0;
  57. }
  58. /*
  59. * Unbind a service with its scheduler
  60. */
  61. int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
  62. {
  63. struct ip_vs_scheduler *sched;
  64. if (svc == NULL) {
  65. IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
  66. return -EINVAL;
  67. }
  68. sched = svc->scheduler;
  69. if (sched == NULL) {
  70. IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
  71. return -EINVAL;
  72. }
  73. if (sched->done_service) {
  74. if (sched->done_service(svc) != 0) {
  75. IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
  76. return -EINVAL;
  77. }
  78. }
  79. svc->scheduler = NULL;
  80. return 0;
  81. }
  82. /*
  83. * Get scheduler in the scheduler list by name
  84. */
  85. static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
  86. {
  87. struct ip_vs_scheduler *sched;
  88. IP_VS_DBG(2, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
  89. sched_name);
  90. read_lock_bh(&__ip_vs_sched_lock);
  91. list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
  92. /*
  93. * Test and get the modules atomically
  94. */
  95. if (sched->module && !try_module_get(sched->module)) {
  96. /*
  97. * This scheduler is just deleted
  98. */
  99. continue;
  100. }
  101. if (strcmp(sched_name, sched->name)==0) {
  102. /* HIT */
  103. read_unlock_bh(&__ip_vs_sched_lock);
  104. return sched;
  105. }
  106. if (sched->module)
  107. module_put(sched->module);
  108. }
  109. read_unlock_bh(&__ip_vs_sched_lock);
  110. return NULL;
  111. }
  112. /*
  113. * Lookup scheduler and try to load it if it doesn't exist
  114. */
  115. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
  116. {
  117. struct ip_vs_scheduler *sched;
  118. /*
  119. * Search for the scheduler by sched_name
  120. */
  121. sched = ip_vs_sched_getbyname(sched_name);
  122. /*
  123. * If scheduler not found, load the module and search again
  124. */
  125. if (sched == NULL) {
  126. request_module("ip_vs_%s", sched_name);
  127. sched = ip_vs_sched_getbyname(sched_name);
  128. }
  129. return sched;
  130. }
  131. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
  132. {
  133. if (scheduler->module)
  134. module_put(scheduler->module);
  135. }
  136. /*
  137. * Register a scheduler in the scheduler list
  138. */
  139. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  140. {
  141. struct ip_vs_scheduler *sched;
  142. if (!scheduler) {
  143. IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
  144. return -EINVAL;
  145. }
  146. if (!scheduler->name) {
  147. IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
  148. return -EINVAL;
  149. }
  150. /* increase the module use count */
  151. ip_vs_use_count_inc();
  152. /*
  153. * Make sure that the scheduler with this name doesn't exist
  154. * in the scheduler list.
  155. */
  156. sched = ip_vs_sched_getbyname(scheduler->name);
  157. if (sched) {
  158. ip_vs_scheduler_put(sched);
  159. ip_vs_use_count_dec();
  160. IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
  161. "already existed in the system\n", scheduler->name);
  162. return -EINVAL;
  163. }
  164. write_lock_bh(&__ip_vs_sched_lock);
  165. if (scheduler->n_list.next != &scheduler->n_list) {
  166. write_unlock_bh(&__ip_vs_sched_lock);
  167. ip_vs_use_count_dec();
  168. IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
  169. "already linked\n", scheduler->name);
  170. return -EINVAL;
  171. }
  172. /*
  173. * Add it into the d-linked scheduler list
  174. */
  175. list_add(&scheduler->n_list, &ip_vs_schedulers);
  176. write_unlock_bh(&__ip_vs_sched_lock);
  177. IP_VS_INFO("[%s] scheduler registered.\n", scheduler->name);
  178. return 0;
  179. }
  180. /*
  181. * Unregister a scheduler from the scheduler list
  182. */
  183. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  184. {
  185. if (!scheduler) {
  186. IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
  187. return -EINVAL;
  188. }
  189. write_lock_bh(&__ip_vs_sched_lock);
  190. if (scheduler->n_list.next == &scheduler->n_list) {
  191. write_unlock_bh(&__ip_vs_sched_lock);
  192. IP_VS_ERR("unregister_ip_vs_scheduler(): [%s] scheduler "
  193. "is not in the list. failed\n", scheduler->name);
  194. return -EINVAL;
  195. }
  196. /*
  197. * Remove it from the d-linked scheduler list
  198. */
  199. list_del(&scheduler->n_list);
  200. write_unlock_bh(&__ip_vs_sched_lock);
  201. /* decrease the module use count */
  202. ip_vs_use_count_dec();
  203. IP_VS_INFO("[%s] scheduler unregistered.\n", scheduler->name);
  204. return 0;
  205. }