ip_vs_sched.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <linux/interrupt.h>
  25. #include <asm/string.h>
  26. #include <linux/kmod.h>
  27. #include <net/ip_vs.h>
  28. /*
  29. * IPVS scheduler list
  30. */
  31. static LIST_HEAD(ip_vs_schedulers);
  32. /* lock for service table */
  33. static DEFINE_RWLOCK(__ip_vs_sched_lock);
  34. /*
  35. * Bind a service with a scheduler
  36. */
  37. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  38. struct ip_vs_scheduler *scheduler)
  39. {
  40. int ret;
  41. if (svc == NULL) {
  42. IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
  43. return -EINVAL;
  44. }
  45. if (scheduler == NULL) {
  46. IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
  47. return -EINVAL;
  48. }
  49. svc->scheduler = scheduler;
  50. if (scheduler->init_service) {
  51. ret = scheduler->init_service(svc);
  52. if (ret) {
  53. IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
  54. return ret;
  55. }
  56. }
  57. return 0;
  58. }
  59. /*
  60. * Unbind a service with its scheduler
  61. */
  62. int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
  63. {
  64. struct ip_vs_scheduler *sched;
  65. if (svc == NULL) {
  66. IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
  67. return -EINVAL;
  68. }
  69. sched = svc->scheduler;
  70. if (sched == NULL) {
  71. IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
  72. return -EINVAL;
  73. }
  74. if (sched->done_service) {
  75. if (sched->done_service(svc) != 0) {
  76. IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
  77. return -EINVAL;
  78. }
  79. }
  80. svc->scheduler = NULL;
  81. return 0;
  82. }
  83. /*
  84. * Get scheduler in the scheduler list by name
  85. */
  86. static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
  87. {
  88. struct ip_vs_scheduler *sched;
  89. IP_VS_DBG(2, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
  90. sched_name);
  91. read_lock_bh(&__ip_vs_sched_lock);
  92. list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
  93. /*
  94. * Test and get the modules atomically
  95. */
  96. if (sched->module && !try_module_get(sched->module)) {
  97. /*
  98. * This scheduler is just deleted
  99. */
  100. continue;
  101. }
  102. if (strcmp(sched_name, sched->name)==0) {
  103. /* HIT */
  104. read_unlock_bh(&__ip_vs_sched_lock);
  105. return sched;
  106. }
  107. if (sched->module)
  108. module_put(sched->module);
  109. }
  110. read_unlock_bh(&__ip_vs_sched_lock);
  111. return NULL;
  112. }
  113. /*
  114. * Lookup scheduler and try to load it if it doesn't exist
  115. */
  116. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
  117. {
  118. struct ip_vs_scheduler *sched;
  119. /*
  120. * Search for the scheduler by sched_name
  121. */
  122. sched = ip_vs_sched_getbyname(sched_name);
  123. /*
  124. * If scheduler not found, load the module and search again
  125. */
  126. if (sched == NULL) {
  127. request_module("ip_vs_%s", sched_name);
  128. sched = ip_vs_sched_getbyname(sched_name);
  129. }
  130. return sched;
  131. }
  132. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
  133. {
  134. if (scheduler->module)
  135. module_put(scheduler->module);
  136. }
  137. /*
  138. * Register a scheduler in the scheduler list
  139. */
  140. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  141. {
  142. struct ip_vs_scheduler *sched;
  143. if (!scheduler) {
  144. IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
  145. return -EINVAL;
  146. }
  147. if (!scheduler->name) {
  148. IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
  149. return -EINVAL;
  150. }
  151. /* increase the module use count */
  152. ip_vs_use_count_inc();
  153. /*
  154. * Make sure that the scheduler with this name doesn't exist
  155. * in the scheduler list.
  156. */
  157. sched = ip_vs_sched_getbyname(scheduler->name);
  158. if (sched) {
  159. ip_vs_scheduler_put(sched);
  160. ip_vs_use_count_dec();
  161. IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
  162. "already existed in the system\n", scheduler->name);
  163. return -EINVAL;
  164. }
  165. write_lock_bh(&__ip_vs_sched_lock);
  166. if (scheduler->n_list.next != &scheduler->n_list) {
  167. write_unlock_bh(&__ip_vs_sched_lock);
  168. ip_vs_use_count_dec();
  169. IP_VS_ERR("register_ip_vs_scheduler(): [%s] scheduler "
  170. "already linked\n", scheduler->name);
  171. return -EINVAL;
  172. }
  173. /*
  174. * Add it into the d-linked scheduler list
  175. */
  176. list_add(&scheduler->n_list, &ip_vs_schedulers);
  177. write_unlock_bh(&__ip_vs_sched_lock);
  178. IP_VS_INFO("[%s] scheduler registered.\n", scheduler->name);
  179. return 0;
  180. }
  181. /*
  182. * Unregister a scheduler from the scheduler list
  183. */
  184. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  185. {
  186. if (!scheduler) {
  187. IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
  188. return -EINVAL;
  189. }
  190. write_lock_bh(&__ip_vs_sched_lock);
  191. if (scheduler->n_list.next == &scheduler->n_list) {
  192. write_unlock_bh(&__ip_vs_sched_lock);
  193. IP_VS_ERR("unregister_ip_vs_scheduler(): [%s] scheduler "
  194. "is not in the list. failed\n", scheduler->name);
  195. return -EINVAL;
  196. }
  197. /*
  198. * Remove it from the d-linked scheduler list
  199. */
  200. list_del(&scheduler->n_list);
  201. write_unlock_bh(&__ip_vs_sched_lock);
  202. /* decrease the module use count */
  203. ip_vs_use_count_dec();
  204. IP_VS_INFO("[%s] scheduler unregistered.\n", scheduler->name);
  205. return 0;
  206. }