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 | |
Hannes Eder | 9aada7a | 2009-07-30 14:29:44 -0700 | [diff] [blame] | 22 | #define KMSG_COMPONENT "IPVS" |
| 23 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | |
| 28 | #include <net/ip_vs.h> |
| 29 | |
| 30 | |
| 31 | static int ip_vs_rr_init_svc(struct ip_vs_service *svc) |
| 32 | { |
| 33 | svc->sched_data = &svc->destinations; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 38 | static int ip_vs_rr_del_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 40 | struct list_head *p; |
| 41 | |
Julian Anastasov | ba3a3ce | 2013-03-22 11:46:51 +0200 | [diff] [blame] | 42 | spin_lock_bh(&svc->sched_lock); |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 43 | p = (struct list_head *) svc->sched_data; |
| 44 | /* dest is already unlinked, so p->prev is not valid but |
| 45 | * p->next is valid, use it to reach previous entry. |
| 46 | */ |
| 47 | if (p == &dest->n_list) |
| 48 | svc->sched_data = p->next->prev; |
Julian Anastasov | ba3a3ce | 2013-03-22 11:46:51 +0200 | [diff] [blame] | 49 | spin_unlock_bh(&svc->sched_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | * Round-Robin Scheduling |
| 56 | */ |
| 57 | static struct ip_vs_dest * |
Julian Anastasov | bba54de | 2013-06-16 09:09:36 +0300 | [diff] [blame] | 58 | ip_vs_rr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb, |
| 59 | struct ip_vs_iphdr *iph) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 61 | struct list_head *p; |
| 62 | struct ip_vs_dest *dest, *last; |
| 63 | int pass = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Hannes Eder | 1e3e238 | 2009-08-02 11:05:41 +0000 | [diff] [blame] | 65 | IP_VS_DBG(6, "%s(): Scheduling...\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 67 | spin_lock_bh(&svc->sched_lock); |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 68 | p = (struct list_head *) svc->sched_data; |
| 69 | last = dest = list_entry(p, struct ip_vs_dest, n_list); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 70 | |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 71 | do { |
| 72 | list_for_each_entry_continue_rcu(dest, |
| 73 | &svc->destinations, |
| 74 | n_list) { |
| 75 | if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) && |
| 76 | atomic_read(&dest->weight) > 0) |
| 77 | /* HIT */ |
| 78 | goto out; |
| 79 | if (dest == last) |
| 80 | goto stop; |
| 81 | } |
| 82 | pass++; |
| 83 | /* Previous dest could be unlinked, do not loop forever. |
| 84 | * If we stay at head there is no need for 2nd pass. |
| 85 | */ |
| 86 | } while (pass < 2 && p != &svc->destinations); |
| 87 | |
| 88 | stop: |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 89 | spin_unlock_bh(&svc->sched_lock); |
Patrick Schaaf | 41ac51e | 2011-02-11 14:01:12 +0100 | [diff] [blame] | 90 | ip_vs_scheduler_err(svc, "no destination available"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return NULL; |
| 92 | |
| 93 | out: |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 94 | svc->sched_data = &dest->n_list; |
Julian Anastasov | ac69269 | 2013-03-22 11:46:54 +0200 | [diff] [blame] | 95 | spin_unlock_bh(&svc->sched_lock); |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 96 | IP_VS_DBG_BUF(6, "RR: server %s:%u " |
| 97 | "activeconns %d refcnt %d weight %d\n", |
Julian Anastasov | 4d316f3 | 2014-09-17 00:09:00 +0300 | [diff] [blame] | 98 | IP_VS_DBG_ADDR(dest->af, &dest->addr), ntohs(dest->port), |
Julius Volz | b14198f | 2008-09-02 15:55:39 +0200 | [diff] [blame] | 99 | atomic_read(&dest->activeconns), |
| 100 | atomic_read(&dest->refcnt), atomic_read(&dest->weight)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | return dest; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static struct ip_vs_scheduler ip_vs_rr_scheduler = { |
| 107 | .name = "rr", /* name */ |
| 108 | .refcnt = ATOMIC_INIT(0), |
| 109 | .module = THIS_MODULE, |
Sven Wegener | d149ccc | 2008-08-10 09:18:02 +0000 | [diff] [blame] | 110 | .n_list = LIST_HEAD_INIT(ip_vs_rr_scheduler.n_list), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | .init_service = ip_vs_rr_init_svc, |
Julian Anastasov | c0d0c0a | 2013-03-22 11:46:44 +0200 | [diff] [blame] | 112 | .add_dest = NULL, |
| 113 | .del_dest = ip_vs_rr_del_dest, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .schedule = ip_vs_rr_schedule, |
| 115 | }; |
| 116 | |
| 117 | static int __init ip_vs_rr_init(void) |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return register_ip_vs_scheduler(&ip_vs_rr_scheduler); |
| 120 | } |
| 121 | |
| 122 | static void __exit ip_vs_rr_cleanup(void) |
| 123 | { |
| 124 | unregister_ip_vs_scheduler(&ip_vs_rr_scheduler); |
Julian Anastasov | ceec4c3 | 2013-03-22 11:46:53 +0200 | [diff] [blame] | 125 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | module_init(ip_vs_rr_init); |
| 129 | module_exit(ip_vs_rr_cleanup); |
| 130 | MODULE_LICENSE("GPL"); |