linux/net/netfilter/ipvs/ip_vs_lc.c
<<
>>
Prefs
   1/*
   2 * IPVS:        Least-Connection Scheduling module
   3 *
   4 * Authors:     Wensong Zhang <wensong@linuxvirtualserver.org>
   5 *
   6 *              This program is free software; you can redistribute it and/or
   7 *              modify it under the terms of the GNU General Public License
   8 *              as published by the Free Software Foundation; either version
   9 *              2 of the License, or (at your option) any later version.
  10 *
  11 * Changes:
  12 *     Wensong Zhang            :     added the ip_vs_lc_update_svc
  13 *     Wensong Zhang            :     added any dest with weight=0 is quiesced
  14 *
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/kernel.h>
  19
  20#include <net/ip_vs.h>
  21
  22
  23static inline unsigned int
  24ip_vs_lc_dest_overhead(struct ip_vs_dest *dest)
  25{
  26        /*
  27         * We think the overhead of processing active connections is 256
  28         * times higher than that of inactive connections in average. (This
  29         * 256 times might not be accurate, we will change it later) We
  30         * use the following formula to estimate the overhead now:
  31         *                dest->activeconns*256 + dest->inactconns
  32         */
  33        return (atomic_read(&dest->activeconns) << 8) +
  34                atomic_read(&dest->inactconns);
  35}
  36
  37
  38/*
  39 *      Least Connection scheduling
  40 */
  41static struct ip_vs_dest *
  42ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
  43{
  44        struct ip_vs_dest *dest, *least = NULL;
  45        unsigned int loh = 0, doh;
  46
  47        IP_VS_DBG(6, "ip_vs_lc_schedule(): Scheduling...\n");
  48
  49        /*
  50         * Simply select the server with the least number of
  51         *        (activeconns<<5) + inactconns
  52         * Except whose weight is equal to zero.
  53         * If the weight is equal to zero, it means that the server is
  54         * quiesced, the existing connections to the server still get
  55         * served, but no new connection is assigned to the server.
  56         */
  57
  58        list_for_each_entry(dest, &svc->destinations, n_list) {
  59                if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
  60                    atomic_read(&dest->weight) == 0)
  61                        continue;
  62                doh = ip_vs_lc_dest_overhead(dest);
  63                if (!least || doh < loh) {
  64                        least = dest;
  65                        loh = doh;
  66                }
  67        }
  68
  69        if (least)
  70        IP_VS_DBG_BUF(6, "LC: server %s:%u activeconns %d inactconns %d\n",
  71                      IP_VS_DBG_ADDR(svc->af, &least->addr), ntohs(least->port),
  72                      atomic_read(&least->activeconns),
  73                      atomic_read(&least->inactconns));
  74
  75        return least;
  76}
  77
  78
  79static struct ip_vs_scheduler ip_vs_lc_scheduler = {
  80        .name =                 "lc",
  81        .refcnt =               ATOMIC_INIT(0),
  82        .module =               THIS_MODULE,
  83        .n_list =               LIST_HEAD_INIT(ip_vs_lc_scheduler.n_list),
  84#ifdef CONFIG_IP_VS_IPV6
  85        .supports_ipv6 =        1,
  86#endif
  87        .schedule =             ip_vs_lc_schedule,
  88};
  89
  90
  91static int __init ip_vs_lc_init(void)
  92{
  93        return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
  94}
  95
  96static void __exit ip_vs_lc_cleanup(void)
  97{
  98        unregister_ip_vs_scheduler(&ip_vs_lc_scheduler);
  99}
 100
 101module_init(ip_vs_lc_init);
 102module_exit(ip_vs_lc_cleanup);
 103MODULE_LICENSE("GPL");
 104