ip_vs_sched.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
  9. * Peter Kese <peter.kese@ijs.si>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Changes:
  17. *
  18. */
  19. #define KMSG_COMPONENT "IPVS"
  20. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/string.h>
  25. #include <linux/kmod.h>
  26. #include <linux/sysctl.h>
  27. #include <net/ip_vs.h>
  28. EXPORT_SYMBOL(ip_vs_scheduler_err);
  29. /*
  30. * IPVS scheduler list
  31. */
  32. static LIST_HEAD(ip_vs_schedulers);
  33. /* semaphore for schedulers */
  34. static DEFINE_MUTEX(ip_vs_sched_mutex);
  35. /*
  36. * Bind a service with a scheduler
  37. */
  38. int ip_vs_bind_scheduler(struct ip_vs_service *svc,
  39. struct ip_vs_scheduler *scheduler)
  40. {
  41. int ret;
  42. if (scheduler->init_service) {
  43. ret = scheduler->init_service(svc);
  44. if (ret) {
  45. pr_err("%s(): init error\n", __func__);
  46. return ret;
  47. }
  48. }
  49. rcu_assign_pointer(svc->scheduler, scheduler);
  50. return 0;
  51. }
  52. /*
  53. * Unbind a service with its scheduler
  54. */
  55. void ip_vs_unbind_scheduler(struct ip_vs_service *svc,
  56. struct ip_vs_scheduler *sched)
  57. {
  58. struct ip_vs_scheduler *cur_sched;
  59. cur_sched = rcu_dereference_protected(svc->scheduler, 1);
  60. /* This check proves that old 'sched' was installed */
  61. if (!cur_sched)
  62. return;
  63. if (sched->done_service)
  64. sched->done_service(svc);
  65. /* svc->scheduler can be set to NULL only by caller */
  66. }
  67. /*
  68. * Get scheduler in the scheduler list by name
  69. */
  70. static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
  71. {
  72. struct ip_vs_scheduler *sched;
  73. IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
  74. mutex_lock(&ip_vs_sched_mutex);
  75. list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
  76. /*
  77. * Test and get the modules atomically
  78. */
  79. if (sched->module && !try_module_get(sched->module)) {
  80. /*
  81. * This scheduler is just deleted
  82. */
  83. continue;
  84. }
  85. if (strcmp(sched_name, sched->name)==0) {
  86. /* HIT */
  87. mutex_unlock(&ip_vs_sched_mutex);
  88. return sched;
  89. }
  90. module_put(sched->module);
  91. }
  92. mutex_unlock(&ip_vs_sched_mutex);
  93. return NULL;
  94. }
  95. /*
  96. * Lookup scheduler and try to load it if it doesn't exist
  97. */
  98. struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
  99. {
  100. struct ip_vs_scheduler *sched;
  101. /*
  102. * Search for the scheduler by sched_name
  103. */
  104. sched = ip_vs_sched_getbyname(sched_name);
  105. /*
  106. * If scheduler not found, load the module and search again
  107. */
  108. if (sched == NULL) {
  109. request_module("ip_vs_%s", sched_name);
  110. sched = ip_vs_sched_getbyname(sched_name);
  111. }
  112. return sched;
  113. }
  114. void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
  115. {
  116. if (scheduler)
  117. module_put(scheduler->module);
  118. }
  119. /*
  120. * Common error output helper for schedulers
  121. */
  122. void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
  123. {
  124. struct ip_vs_scheduler *sched = rcu_dereference(svc->scheduler);
  125. char *sched_name = sched ? sched->name : "none";
  126. if (svc->fwmark) {
  127. IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
  128. sched_name, svc->fwmark, svc->fwmark, msg);
  129. #ifdef CONFIG_IP_VS_IPV6
  130. } else if (svc->af == AF_INET6) {
  131. IP_VS_ERR_RL("%s: %s [%pI6c]:%d - %s\n",
  132. sched_name, ip_vs_proto_name(svc->protocol),
  133. &svc->addr.in6, ntohs(svc->port), msg);
  134. #endif
  135. } else {
  136. IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n",
  137. sched_name, ip_vs_proto_name(svc->protocol),
  138. &svc->addr.ip, ntohs(svc->port), msg);
  139. }
  140. }
  141. /*
  142. * Register a scheduler in the scheduler list
  143. */
  144. int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  145. {
  146. struct ip_vs_scheduler *sched;
  147. if (!scheduler) {
  148. pr_err("%s(): NULL arg\n", __func__);
  149. return -EINVAL;
  150. }
  151. if (!scheduler->name) {
  152. pr_err("%s(): NULL scheduler_name\n", __func__);
  153. return -EINVAL;
  154. }
  155. /* increase the module use count */
  156. ip_vs_use_count_inc();
  157. mutex_lock(&ip_vs_sched_mutex);
  158. if (!list_empty(&scheduler->n_list)) {
  159. mutex_unlock(&ip_vs_sched_mutex);
  160. ip_vs_use_count_dec();
  161. pr_err("%s(): [%s] scheduler already linked\n",
  162. __func__, scheduler->name);
  163. return -EINVAL;
  164. }
  165. /*
  166. * Make sure that the scheduler with this name doesn't exist
  167. * in the scheduler list.
  168. */
  169. list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
  170. if (strcmp(scheduler->name, sched->name) == 0) {
  171. mutex_unlock(&ip_vs_sched_mutex);
  172. ip_vs_use_count_dec();
  173. pr_err("%s(): [%s] scheduler already existed "
  174. "in the system\n", __func__, scheduler->name);
  175. return -EINVAL;
  176. }
  177. }
  178. /*
  179. * Add it into the d-linked scheduler list
  180. */
  181. list_add(&scheduler->n_list, &ip_vs_schedulers);
  182. mutex_unlock(&ip_vs_sched_mutex);
  183. pr_info("[%s] scheduler registered.\n", scheduler->name);
  184. return 0;
  185. }
  186. /*
  187. * Unregister a scheduler from the scheduler list
  188. */
  189. int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
  190. {
  191. if (!scheduler) {
  192. pr_err("%s(): NULL arg\n", __func__);
  193. return -EINVAL;
  194. }
  195. mutex_lock(&ip_vs_sched_mutex);
  196. if (list_empty(&scheduler->n_list)) {
  197. mutex_unlock(&ip_vs_sched_mutex);
  198. pr_err("%s(): [%s] scheduler is not in the list. failed\n",
  199. __func__, scheduler->name);
  200. return -EINVAL;
  201. }
  202. /*
  203. * Remove it from the d-linked scheduler list
  204. */
  205. list_del(&scheduler->n_list);
  206. mutex_unlock(&ip_vs_sched_mutex);
  207. /* decrease the module use count */
  208. ip_vs_use_count_dec();
  209. pr_info("[%s] scheduler unregistered.\n", scheduler->name);
  210. return 0;
  211. }