2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* IPVS An implementation of the IP virtual server support for the
|
|
|
|
* LINUX operating system. IPVS is now implemented as a module
|
|
|
|
* over the Netfilter framework. IPVS can be used to build a
|
|
|
|
* high-performance and highly available server based on a
|
|
|
|
* cluster of servers.
|
|
|
|
*
|
|
|
|
* Authors: Wensong Zhang <wensong@linuxvirtualserver.org>
|
|
|
|
* Peter Kese <peter.kese@ijs.si>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-07-30 23:29:44 +02:00
|
|
|
#define KMSG_COMPONENT "IPVS"
|
|
|
|
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/spinlock.h>
|
2006-01-05 21:14:43 +01:00
|
|
|
#include <linux/interrupt.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
#include <asm/string.h>
|
|
|
|
#include <linux/kmod.h>
|
2008-01-12 11:33:50 +01:00
|
|
|
#include <linux/sysctl.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
#include <net/ip_vs.h>
|
|
|
|
|
2011-02-11 14:01:12 +01:00
|
|
|
EXPORT_SYMBOL(ip_vs_scheduler_err);
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* IPVS scheduler list
|
|
|
|
*/
|
|
|
|
static LIST_HEAD(ip_vs_schedulers);
|
|
|
|
|
|
|
|
/* lock for service table */
|
2010-08-26 04:54:29 +02:00
|
|
|
static DEFINE_SPINLOCK(ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind a service with a scheduler
|
|
|
|
*/
|
|
|
|
int ip_vs_bind_scheduler(struct ip_vs_service *svc,
|
|
|
|
struct ip_vs_scheduler *scheduler)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
svc->scheduler = scheduler;
|
|
|
|
|
|
|
|
if (scheduler->init_service) {
|
|
|
|
ret = scheduler->init_service(svc);
|
|
|
|
if (ret) {
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): init error\n", __func__);
|
2005-04-17 00:20:36 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unbind a service with its scheduler
|
|
|
|
*/
|
|
|
|
int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
|
|
|
|
{
|
2010-08-22 14:37:52 +02:00
|
|
|
struct ip_vs_scheduler *sched = svc->scheduler;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-08-22 14:37:52 +02:00
|
|
|
if (!sched)
|
|
|
|
return 0;
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
if (sched->done_service) {
|
|
|
|
if (sched->done_service(svc) != 0) {
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): done error\n", __func__);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
svc->scheduler = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get scheduler in the scheduler list by name
|
|
|
|
*/
|
|
|
|
static struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
|
|
|
|
{
|
|
|
|
struct ip_vs_scheduler *sched;
|
|
|
|
|
2009-08-02 13:05:41 +02:00
|
|
|
IP_VS_DBG(2, "%s(): sched_name \"%s\"\n", __func__, sched_name);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_lock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
|
|
|
|
/*
|
|
|
|
* Test and get the modules atomically
|
|
|
|
*/
|
|
|
|
if (sched->module && !try_module_get(sched->module)) {
|
|
|
|
/*
|
|
|
|
* This scheduler is just deleted
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(sched_name, sched->name)==0) {
|
|
|
|
/* HIT */
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
return sched;
|
|
|
|
}
|
|
|
|
if (sched->module)
|
|
|
|
module_put(sched->module);
|
|
|
|
}
|
|
|
|
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup scheduler and try to load it if it doesn't exist
|
|
|
|
*/
|
|
|
|
struct ip_vs_scheduler *ip_vs_scheduler_get(const char *sched_name)
|
|
|
|
{
|
|
|
|
struct ip_vs_scheduler *sched;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the scheduler by sched_name
|
|
|
|
*/
|
|
|
|
sched = ip_vs_sched_getbyname(sched_name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If scheduler not found, load the module and search again
|
|
|
|
*/
|
|
|
|
if (sched == NULL) {
|
|
|
|
request_module("ip_vs_%s", sched_name);
|
|
|
|
sched = ip_vs_sched_getbyname(sched_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sched;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ip_vs_scheduler_put(struct ip_vs_scheduler *scheduler)
|
|
|
|
{
|
2010-08-22 14:37:52 +02:00
|
|
|
if (scheduler && scheduler->module)
|
2005-04-17 00:20:36 +02:00
|
|
|
module_put(scheduler->module);
|
|
|
|
}
|
|
|
|
|
2011-02-11 14:01:12 +01:00
|
|
|
/*
|
|
|
|
* Common error output helper for schedulers
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ip_vs_scheduler_err(struct ip_vs_service *svc, const char *msg)
|
|
|
|
{
|
|
|
|
if (svc->fwmark) {
|
|
|
|
IP_VS_ERR_RL("%s: FWM %u 0x%08X - %s\n",
|
|
|
|
svc->scheduler->name, svc->fwmark,
|
|
|
|
svc->fwmark, msg);
|
|
|
|
#ifdef CONFIG_IP_VS_IPV6
|
|
|
|
} else if (svc->af == AF_INET6) {
|
|
|
|
IP_VS_ERR_RL("%s: %s [%pI6]:%d - %s\n",
|
|
|
|
svc->scheduler->name,
|
|
|
|
ip_vs_proto_name(svc->protocol),
|
|
|
|
&svc->addr.in6, ntohs(svc->port), msg);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
IP_VS_ERR_RL("%s: %s %pI4:%d - %s\n",
|
|
|
|
svc->scheduler->name,
|
|
|
|
ip_vs_proto_name(svc->protocol),
|
|
|
|
&svc->addr.ip, ntohs(svc->port), msg);
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Register a scheduler in the scheduler list
|
|
|
|
*/
|
|
|
|
int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
|
|
|
|
{
|
|
|
|
struct ip_vs_scheduler *sched;
|
|
|
|
|
|
|
|
if (!scheduler) {
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): NULL arg\n", __func__);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!scheduler->name) {
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): NULL scheduler_name\n", __func__);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* increase the module use count */
|
|
|
|
ip_vs_use_count_inc();
|
|
|
|
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_lock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2008-08-10 11:18:02 +02:00
|
|
|
if (!list_empty(&scheduler->n_list)) {
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
ip_vs_use_count_dec();
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): [%s] scheduler already linked\n",
|
|
|
|
__func__, scheduler->name);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2007-12-04 09:45:06 +01:00
|
|
|
/*
|
|
|
|
* Make sure that the scheduler with this name doesn't exist
|
|
|
|
* in the scheduler list.
|
|
|
|
*/
|
|
|
|
list_for_each_entry(sched, &ip_vs_schedulers, n_list) {
|
|
|
|
if (strcmp(scheduler->name, sched->name) == 0) {
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2007-12-04 09:45:06 +01:00
|
|
|
ip_vs_use_count_dec();
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): [%s] scheduler already existed "
|
|
|
|
"in the system\n", __func__, scheduler->name);
|
2007-12-04 09:45:06 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Add it into the d-linked scheduler list
|
|
|
|
*/
|
|
|
|
list_add(&scheduler->n_list, &ip_vs_schedulers);
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_info("[%s] scheduler registered.\n", scheduler->name);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unregister a scheduler from the scheduler list
|
|
|
|
*/
|
|
|
|
int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
|
|
|
|
{
|
|
|
|
if (!scheduler) {
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): NULL arg\n", __func__);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_lock_bh(&ip_vs_sched_lock);
|
2008-08-10 11:18:02 +02:00
|
|
|
if (list_empty(&scheduler->n_list)) {
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_err("%s(): [%s] scheduler is not in the list. failed\n",
|
|
|
|
__func__, scheduler->name);
|
2005-04-17 00:20:36 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove it from the d-linked scheduler list
|
|
|
|
*/
|
|
|
|
list_del(&scheduler->n_list);
|
2010-08-26 04:54:29 +02:00
|
|
|
spin_unlock_bh(&ip_vs_sched_lock);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* decrease the module use count */
|
|
|
|
ip_vs_use_count_dec();
|
|
|
|
|
2009-08-02 13:05:41 +02:00
|
|
|
pr_info("[%s] scheduler unregistered.\n", scheduler->name);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|