Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPVS: Round-Robin Scheduling module |
| 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Authors: Wensong Zhang <wensong@linuxvirtualserver.org> |
| 5 | * Peter Kese <peter.kese@ijs.si> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * Fixes/Changes: |
| 13 | * Wensong Zhang : changed the ip_vs_rr_schedule to return dest |
| 14 | * Julian Anastasov : fixed the NULL pointer access bug in debugging |
| 15 | * Wensong Zhang : changed some comestics things for debugging |
| 16 | * Wensong Zhang : changed for the d-linked destination list |
| 17 | * Wensong Zhang : added the ip_vs_rr_update_svc |
| 18 | * Wensong Zhang : added any dest with weight=0 is quiesced |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | |
| 25 | #include <net/ip_vs.h> |
| 26 | |
| 27 | |
| 28 | static int ip_vs_rr_init_svc(struct ip_vs_service *svc) |
| 29 | { |
| 30 | svc->sched_data = &svc->destinations; |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static int ip_vs_rr_update_svc(struct ip_vs_service *svc) |
| 36 | { |
| 37 | svc->sched_data = &svc->destinations; |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /* |
| 43 | * Round-Robin Scheduling |
| 44 | */ |
| 45 | static struct ip_vs_dest * |
| 46 | ip_vs_rr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb) |
| 47 | { |
| 48 | struct list_head *p, *q; |
| 49 | struct ip_vs_dest *dest; |
| 50 | |
| 51 | IP_VS_DBG(6, "ip_vs_rr_schedule(): Scheduling...\n"); |
| 52 | |
| 53 | write_lock(&svc->sched_lock); |
| 54 | p = (struct list_head *)svc->sched_data; |
| 55 | p = p->next; |
| 56 | q = p; |
| 57 | do { |
| 58 | /* skip list head */ |
| 59 | if (q == &svc->destinations) { |
| 60 | q = q->next; |
| 61 | continue; |
| 62 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | dest = list_entry(q, struct ip_vs_dest, n_list); |
| 65 | if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) && |
| 66 | atomic_read(&dest->weight) > 0) |
| 67 | /* HIT */ |
| 68 | goto out; |
| 69 | q = q->next; |
| 70 | } while (q != p); |
| 71 | write_unlock(&svc->sched_lock); |
| 72 | return NULL; |
| 73 | |
| 74 | out: |
| 75 | svc->sched_data = q; |
| 76 | write_unlock(&svc->sched_lock); |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 77 | IP_VS_DBG_BUF(6, "RR: server %s:%u " |
| 78 | "activeconns %d refcnt %d weight %d\n", |
| 79 | IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port), |
| 80 | atomic_read(&dest->activeconns), |
| 81 | atomic_read(&dest->refcnt), atomic_read(&dest->weight)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | return dest; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | static struct ip_vs_scheduler ip_vs_rr_scheduler = { |
| 88 | .name = "rr", /* name */ |
| 89 | .refcnt = ATOMIC_INIT(0), |
| 90 | .module = THIS_MODULE, |
Sven Wegener | d149ccc | 2008-08-10 09:18:02 +0000 | [diff] [blame] | 91 | .n_list = LIST_HEAD_INIT(ip_vs_rr_scheduler.n_list), |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 92 | #ifdef CONFIG_IP_VS_IPV6 |
| 93 | .supports_ipv6 = 1, |
| 94 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | .init_service = ip_vs_rr_init_svc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | .update_service = ip_vs_rr_update_svc, |
| 97 | .schedule = ip_vs_rr_schedule, |
| 98 | }; |
| 99 | |
| 100 | static int __init ip_vs_rr_init(void) |
| 101 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return register_ip_vs_scheduler(&ip_vs_rr_scheduler); |
| 103 | } |
| 104 | |
| 105 | static void __exit ip_vs_rr_cleanup(void) |
| 106 | { |
| 107 | unregister_ip_vs_scheduler(&ip_vs_rr_scheduler); |
| 108 | } |
| 109 | |
| 110 | module_init(ip_vs_rr_init); |
| 111 | module_exit(ip_vs_rr_cleanup); |
| 112 | MODULE_LICENSE("GPL"); |