/*
 * 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.
 *
 * Version:     $Id: ip_vs_sched.c,v 1.3 2000/07/06 14:57:09 wensong Exp $
 *
 * Authors:     Wensong Zhang <wensong@iinchina.net>
 *              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:
 *
 */

#ifdef MODULE
#define EXPORT_SYMTAB
#endif

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <asm/softirq.h>                /* for local_bh_* */
#include <asm/string.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif

#include "ip_vs.h"
//#include <net/ip_vs.h>

EXPORT_SYMBOL(ip_vs_register_scheduler_module);
EXPORT_SYMBOL(ip_vs_unregister_scheduler_module);

/*
 *  IPVS scheduler list
 */
LIST_HEAD(ip_vs_schedulers);

/* lock for service table */
rwlock_t __ip_vs_sched_lock = RW_LOCK_UNLOCKED;


/*
 *  Register a scheduler in the scheduler list
 */
int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
{
	if (!scheduler) {
		IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
		return -EINVAL;
	}

	if (!scheduler->name) {
		IP_VS_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
		return -EINVAL;
	}

	MOD_INC_USE_COUNT;

	write_lock_bh(&__ip_vs_sched_lock);

	if (scheduler->n_list.next != &scheduler->n_list) {
		write_unlock_bh(&__ip_vs_sched_lock);
		MOD_DEC_USE_COUNT;
		IP_VS_ERR("register_ip_vs_scheduler(): scheduler already linked\n");
		return -EINVAL;
	}

	/*
	 *	Add it into the d-linked scheduler list
	 */
	list_add(&scheduler->n_list, &ip_vs_schedulers);
	write_unlock_bh(&__ip_vs_sched_lock);

	return 0;
}


/*
 *  Unregister a scheduler from the scheduler list
 */
int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
{
	if (!scheduler) {
		IP_VS_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
		return -EINVAL;
	}

#if 0
	/*
	 *	Only allow unregistration if it is not referenced
	 */
	if (atomic_read(&scheduler->refcnt))  {
		IP_VS_ERR("unregister_ip_vs_scheduler(): is in use by %d guys. failed\n",
			  atomic_read(&scheduler->refcnt));
		return -EINVAL;
	}
#endif

	write_lock_bh(&__ip_vs_sched_lock);
	if (scheduler->n_list.next == &scheduler->n_list) {
		write_unlock_bh(&__ip_vs_sched_lock);
		IP_VS_ERR("unregister_ip_vs_scheduler(): scheduler is not in the list. failed\n");
		return -EINVAL;
	}

	/*	
	 *	Remove it from the d-linked scheduler list
	 */
	list_del(&scheduler->n_list);
	write_unlock_bh(&__ip_vs_sched_lock);

	MOD_DEC_USE_COUNT;

	return 0;
}


/*
 *  Bind a service with a scheduler
 */
int ip_vs_bind_scheduler(struct ip_vs_service *svc,
			struct ip_vs_scheduler *scheduler)
{
        int ret;

	if (svc == NULL) {
		IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
		return -EINVAL;
	}
	if (scheduler == NULL) {
		IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
		return -EINVAL;
	}

	svc->scheduler = scheduler;
#if 0
	atomic_inc(&scheduler->refcnt);
#endif

	if (scheduler->init_service) {
		ret = scheduler->init_service(svc);
		if (ret) {
			IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
			return ret;
		}
	}

	return 0;
}


/*
 *  Unbind a service with its scheduler
 */
int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
{
	struct ip_vs_scheduler *sched;

	if (svc == NULL) {
		IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
		return -EINVAL;
	}

	sched = svc->scheduler;
	if (sched == NULL) {
		IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
		return -EINVAL;
	}

	if (sched->done_service) {
		if (sched->done_service(svc) != 0) {
			IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
			return -EINVAL;
		}
	}

#if 0
	atomic_dec(&sched->refcnt);
#endif
	svc->scheduler = NULL;

	return 0;
}


/*
 *  Get scheduler in the scheduler list by name
 */
struct ip_vs_scheduler *ip_vs_sched_getbyname(const char *sched_name)
{
	struct ip_vs_scheduler *sched;
	struct list_head *l, *e;

	IP_VS_DBG(1, "ip_vs_sched_getbyname(): sched_name \"%s\"\n",
                  sched_name);
	
	l = &ip_vs_schedulers;

	read_lock_bh(&__ip_vs_sched_lock);

	for (e=l->next; e!=l; e=e->next) {
		sched = list_entry(e, struct ip_vs_scheduler, n_list);
		
		/*
                 * Test and MOD_INC_USE_COUNT atomically
                 */
		if (!try_inc_mod_count(sched->module)) {
			/*
                         * This scheduler is just deleted
                         */
			continue;
		}
		if (strcmp(sched_name, sched->name)==0) {
			/* HIT */
			read_unlock_bh(&__ip_vs_sched_lock);
			return sched;
		}
		__MOD_DEC_USE_COUNT(sched->module);
	}

	read_unlock_bh(&__ip_vs_sched_lock);
	return NULL;
}


/*
 *  Lookup scheduler and try to load it if it doesn't exist
 */
struct ip_vs_scheduler *ip_vs_lookup_scheduler(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) {
		char module_name[IP_VS_SCHEDNAME_MAXLEN+8];
		sprintf(module_name,"ip_vs_%s", sched_name);
#ifdef CONFIG_KMOD
		request_module(module_name);
#endif /* CONFIG_KMOD */
		sched = ip_vs_sched_getbyname(sched_name);
	}
			
	return sched;
}


/*
 *	Register scheduler module
 */
int ip_vs_register_scheduler_module(struct ip_vs_scheduler *sched)
{
        int ret;

	INIT_LIST_HEAD(&sched->n_list);

	ret = register_ip_vs_scheduler(sched);
	if (!ret) {
		IP_VS_INFO("[%s] scheduling module loaded.\n",
                           sched->name);
	}
	return ret;
}


/*
 *	Unregister scheduler module
 */
void ip_vs_unregister_scheduler_module(struct ip_vs_scheduler *sched)
{
	if (unregister_ip_vs_scheduler(sched) != 0) {
		IP_VS_INFO("cannot remove [%s] scheduling module\n",
                           sched->name);
	} else {
		IP_VS_INFO("[%s] scheduling module unloaded.\n",sched->name);
	}
}

