/
ipvs-thundering-herd-2.6.patch
392 lines (368 loc) · 12.3 KB
/
ipvs-thundering-herd-2.6.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
diff --git a/include/linux/ip_vs.h b/include/linux/ip_vs.h
index 4deb383..1571f81 100644
--- a/include/linux/ip_vs.h
+++ b/include/linux/ip_vs.h
@@ -28,6 +28,18 @@
#define IP_VS_DEST_F_OVERLOAD 0x0002 /* server is overloaded */
/*
+ * Advisory flags for slow start.
+ *
+ * The absolute value size of the weight change will be stored
+ * in dest->slow_start_data. The flag and slow_start_data may
+ * be used and modified by the scheduler to effect slow start.
+ */
+#define IP_VS_DEST_F_WEIGHT_INC 0x0003 /* Weight has been increaced */
+#define IP_VS_DEST_F_WEIGHT_DEC 0x0004 /* Weight has been increaced */
+#define IP_VS_DEST_F_WEIGHT_MASK \
+ (IP_VS_DEST_F_WEIGHT_INC|IP_VS_DEST_F_WEIGHT_DEC)
+
+/*
* IPVS sync daemon states
*/
#define IP_VS_STATE_NONE 0x0000 /* daemon is stopped */
diff --git a/include/net/ip_vs.h b/include/net/ip_vs.h
index 8fa4430..4e16dda 100644
--- a/include/net/ip_vs.h
+++ b/include/net/ip_vs.h
@@ -673,6 +673,10 @@ struct ip_vs_dest {
__be16 vport; /* virtual port number */
union nf_inet_addr vaddr; /* virtual IP address */
__u32 vfwmark; /* firewall mark of service */
+
+ /* for slow start */
+ atomic_t slow_start_data;
+ atomic_t slow_start_data2;
};
@@ -1210,6 +1214,9 @@ ip_vs_find_dest(struct net *net, int af, const union nf_inet_addr *daddr,
__be16 dport, const union nf_inet_addr *vaddr, __be16 vport,
__u16 protocol, __u32 fwmark);
extern struct ip_vs_dest *ip_vs_try_bind_dest(struct ip_vs_conn *cp);
+extern inline unsigned int
+ip_vs_conn_slow_start_dest_handicap(struct ip_vs_dest *dest,
+ struct ip_vs_service *svc);
/*
@@ -1409,7 +1416,7 @@ static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp)
#endif
static inline unsigned int
-ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
+ip_vs_dest_conn_overhead(struct ip_vs_dest *dest, struct ip_vs_service *svc)
{
/*
* We think the overhead of processing active connections is 256
@@ -1418,7 +1425,8 @@ ip_vs_dest_conn_overhead(struct ip_vs_dest *dest)
* use the following formula to estimate the overhead now:
* dest->activeconns*256 + dest->inactconns
*/
- return (atomic_read(&dest->activeconns) << 8) +
+ return ((atomic_read(&dest->activeconns) +
+ ip_vs_conn_slow_start_dest_handicap(dest, svc)) << 8) +
atomic_read(&dest->inactconns);
}
diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c
index e3be48b..fb4a628 100644
--- a/net/netfilter/ipvs/ip_vs_ctl.c
+++ b/net/netfilter/ipvs/ip_vs_ctl.c
@@ -52,6 +52,8 @@
#include <net/ip_vs.h>
+EXPORT_SYMBOL(ip_vs_conn_slow_start_dest_handicap);
+
/* semaphore for IPVS sockopts. And, [gs]etsockopt may sleep. */
static DEFINE_MUTEX(__ip_vs_mutex);
@@ -764,6 +766,22 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
{
struct netns_ipvs *ipvs = net_ipvs(svc->net);
int conn_flags;
+ int old_weight;
+
+ /* set hints for slow start */
+ dest->flags |= IP_VS_DEST_F_WEIGHT_MASK;
+ dest->flags ^= IP_VS_DEST_F_WEIGHT_MASK;
+
+ old_weight = atomic_read(&dest->weight);
+
+ if (old_weight < udest->weight) {
+ atomic_set(&dest->slow_start_data, udest->weight - old_weight);
+ dest->flags |= IP_VS_DEST_F_WEIGHT_INC;
+ }
+ else if (old_weight > udest->weight) {
+ atomic_set(&dest->slow_start_data, old_weight - udest->weight);
+ dest->flags |= IP_VS_DEST_F_WEIGHT_DEC;
+ }
/* set the weight and the flags */
atomic_set(&dest->weight, udest->weight);
@@ -3510,6 +3528,135 @@ out:
return ret;
}
+static void
+__ip_vs_conn_set_slow_start(struct ip_vs_dest *dest, struct ip_vs_service *svc)
+{
+ __u32 ss_handicap;
+ __u32 ss_shift;
+ __u32 ndest;
+ __u32 w = 0;
+ __u32 dest_w = 0;
+ struct list_head *l, *e;
+ struct ip_vs_dest *d;
+
+ /*
+ * If the weight is zero just set the slow_start hint and data to
+ * zero too as they won't be used.
+ */
+
+ if((dest->flags & IP_VS_DEST_F_WEIGHT_DEC) ||
+ !(dest_w = atomic_read(&dest->weight))) {
+ IP_VS_DBG(1, "slow_start: null\n");
+ atomic_set(&dest->slow_start_data, 0);
+ atomic_set(&dest->slow_start_data2, 0);
+ return;
+ }
+
+ /*
+ * Calculate a weighted number of connections this server would
+ * have if all the currently active connections were redistributed
+ * limited to a maximum of 64k.
+ */
+ l = &svc->destinations;
+
+ ss_handicap = 0;
+ ndest = 0;
+
+ for (e = l->next; e != l; e = e->next) {
+ d = list_entry(e, struct ip_vs_dest, n_list);
+ w = atomic_read(&d->weight);
+
+ if (w < 1 || d == dest) {
+ continue;
+ }
+
+ ndest++;
+
+ /* Try to avoid overflowint ss_handicap */
+ ss_shift = atomic_read(&d->activeconns);
+
+ if (ss_shift & 0xffff0000)
+ ss_shift = 0xffff;
+
+ ss_shift = (ss_shift << 16 ) / (w & 0xffff);
+
+ if (~0L - ss_handicap < ss_shift) {
+ ss_handicap = ~0L;
+ break;
+ }
+
+ ss_handicap += ss_shift;
+ }
+
+ if (ndest)
+ ss_handicap = (ss_handicap * dest_w / ndest) >> 16;
+
+ /* ss_shift = log_2((ss_handicap & 0xfff) >> 3) */
+ if (ss_handicap) {
+ __u32 i;
+ ss_shift = ss_handicap;;
+
+ for (i = 12; i > 0; i--) {
+ if (ss_shift & 0x8000)
+ break;
+
+ ss_shift <<= 1;
+ }
+
+ ss_shift = i;
+ ss_handicap <<= ss_shift;
+ }
+ else
+ ss_shift = 0;
+
+ atomic_set(&dest->slow_start_data, ss_handicap);
+ atomic_set(&dest->slow_start_data2, ss_shift);
+
+#ifdef CONFIG_IP_VS_DEBUG
+ IP_VS_DBG(1, "CONN slow_start_init: server %u.%u.%u.%u:%u "
+ "handicap=%u (%u) shift=%u ndest=%u\n",
+ ((unsigned char *)&dest->addr)[0],
+ ((unsigned char *)&dest->addr)[1],
+ ((unsigned char *)&dest->addr)[2],
+ ((unsigned char *)&dest->addr)[3],
+ ntohs(dest->port), ss_handicap,
+ ss_handicap >> ss_shift, ss_shift, ndest);
+#endif
+}
+
+inline unsigned int
+ip_vs_conn_slow_start_dest_handicap(struct ip_vs_dest *dest,
+ struct ip_vs_service *svc)
+{
+ unsigned int handicap;
+
+ /* Set up slow_start if weight has recently changed */
+ if (unlikely(dest->flags & IP_VS_DEST_F_WEIGHT_MASK)) {
+ __ip_vs_conn_set_slow_start(dest, svc);
+ dest->flags |= IP_VS_DEST_F_WEIGHT_MASK;
+ dest->flags ^= IP_VS_DEST_F_WEIGHT_MASK;
+ }
+
+ handicap = atomic_read(&dest->slow_start_data);
+
+ if (unlikely(!handicap))
+ return 0;
+
+ handicap--;
+ atomic_set(&dest->slow_start_data, handicap);
+
+#ifdef CONFIG_IP_VS_DEBUG
+ if (unlikely(!handicap))
+ IP_VS_DBG(1, "CONN slow_start_end: server %u.%u.%u.%u:%u\n",
+ ((unsigned char *)&dest->addr)[0],
+ ((unsigned char *)&dest->addr)[1],
+ ((unsigned char *)&dest->addr)[2],
+ ((unsigned char *)&dest->addr)[3],
+ ntohs(dest->port));
+#endif
+
+ return handicap >> atomic_read(&dest->slow_start_data2);
+}
static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
{
diff --git a/net/netfilter/ipvs/ip_vs_lblc.c b/net/netfilter/ipvs/ip_vs_lblc.c
index 87e40ea..6a0f611 100644
--- a/net/netfilter/ipvs/ip_vs_lblc.c
+++ b/net/netfilter/ipvs/ip_vs_lblc.c
@@ -416,7 +416,7 @@ __ip_vs_lblc_schedule(struct ip_vs_service *svc)
continue;
if (atomic_read(&dest->weight) > 0) {
least = dest;
- loh = ip_vs_dest_conn_overhead(least);
+ loh = ip_vs_dest_conn_overhead(least, svc);
goto nextstage;
}
}
@@ -430,7 +430,7 @@ __ip_vs_lblc_schedule(struct ip_vs_service *svc)
if (dest->flags & IP_VS_DEST_F_OVERLOAD)
continue;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
if (loh * atomic_read(&dest->weight) >
doh * atomic_read(&least->weight)) {
least = dest;
diff --git a/net/netfilter/ipvs/ip_vs_lblcr.c b/net/netfilter/ipvs/ip_vs_lblcr.c
index 90f618a..a354341 100644
--- a/net/netfilter/ipvs/ip_vs_lblcr.c
+++ b/net/netfilter/ipvs/ip_vs_lblcr.c
@@ -163,7 +163,8 @@ static void ip_vs_dest_set_eraseall(struct ip_vs_dest_set *set)
}
/* get weighted least-connection node in the destination set */
-static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
+static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set,
+ struct ip_vs_service *svc)
{
register struct ip_vs_dest_set_elem *e;
struct ip_vs_dest *dest, *least;
@@ -180,7 +181,7 @@ static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
if ((atomic_read(&least->weight) > 0)
&& (least->flags & IP_VS_DEST_F_AVAILABLE)) {
- loh = ip_vs_dest_conn_overhead(least);
+ loh = ip_vs_dest_conn_overhead(least, svc);
goto nextstage;
}
}
@@ -193,7 +194,7 @@ static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
if (dest->flags & IP_VS_DEST_F_OVERLOAD)
continue;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
if ((loh * atomic_read(&dest->weight) >
doh * atomic_read(&least->weight))
&& (dest->flags & IP_VS_DEST_F_AVAILABLE)) {
@@ -215,7 +216,8 @@ static inline struct ip_vs_dest *ip_vs_dest_set_min(struct ip_vs_dest_set *set)
/* get weighted most-connection node in the destination set */
-static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
+static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set,
+ struct ip_vs_service *svc)
{
register struct ip_vs_dest_set_elem *e;
struct ip_vs_dest *dest, *most;
@@ -228,7 +230,7 @@ static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
list_for_each_entry(e, &set->list, list) {
most = e->dest;
if (atomic_read(&most->weight) > 0) {
- moh = ip_vs_dest_conn_overhead(most);
+ moh = ip_vs_dest_conn_overhead(most, svc);
goto nextstage;
}
}
@@ -238,7 +240,7 @@ static inline struct ip_vs_dest *ip_vs_dest_set_max(struct ip_vs_dest_set *set)
nextstage:
list_for_each_entry(e, &set->list, list) {
dest = e->dest;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
/* moh/mw < doh/dw ==> moh*dw < doh*mw, where mw,dw>0 */
if ((moh * atomic_read(&dest->weight) <
doh * atomic_read(&most->weight))
@@ -588,7 +590,7 @@ __ip_vs_lblcr_schedule(struct ip_vs_service *svc)
if (atomic_read(&dest->weight) > 0) {
least = dest;
- loh = ip_vs_dest_conn_overhead(least);
+ loh = ip_vs_dest_conn_overhead(least, svc);
goto nextstage;
}
}
@@ -602,7 +604,7 @@ __ip_vs_lblcr_schedule(struct ip_vs_service *svc)
if (dest->flags & IP_VS_DEST_F_OVERLOAD)
continue;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
if (loh * atomic_read(&dest->weight) >
doh * atomic_read(&least->weight)) {
least = dest;
@@ -667,7 +669,7 @@ ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
/* Get the least loaded destination */
read_lock(&en->set.lock);
- dest = ip_vs_dest_set_min(&en->set);
+ dest = ip_vs_dest_set_min(&en->set, svc);
read_unlock(&en->set.lock);
/* More than one destination + enough time passed by, cleanup */
@@ -677,7 +679,7 @@ ip_vs_lblcr_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
struct ip_vs_dest *m;
write_lock(&en->set.lock);
- m = ip_vs_dest_set_max(&en->set);
+ m = ip_vs_dest_set_max(&en->set, svc);
if (m)
ip_vs_dest_set_erase(&en->set, m);
write_unlock(&en->set.lock);
diff --git a/net/netfilter/ipvs/ip_vs_lc.c b/net/netfilter/ipvs/ip_vs_lc.c
index f391819..743d7b7 100644
--- a/net/netfilter/ipvs/ip_vs_lc.c
+++ b/net/netfilter/ipvs/ip_vs_lc.c
@@ -46,7 +46,7 @@ ip_vs_lc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
if ((dest->flags & IP_VS_DEST_F_OVERLOAD) ||
atomic_read(&dest->weight) == 0)
continue;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
if (!least || doh < loh) {
least = dest;
loh = doh;
diff --git a/net/netfilter/ipvs/ip_vs_wlc.c b/net/netfilter/ipvs/ip_vs_wlc.c
index bc1bfc4..074e61b 100644
--- a/net/netfilter/ipvs/ip_vs_wlc.c
+++ b/net/netfilter/ipvs/ip_vs_wlc.c
@@ -55,7 +55,7 @@ ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
if (!(dest->flags & IP_VS_DEST_F_OVERLOAD) &&
atomic_read(&dest->weight) > 0) {
least = dest;
- loh = ip_vs_dest_conn_overhead(least);
+ loh = ip_vs_dest_conn_overhead(least, svc);
goto nextstage;
}
}
@@ -69,7 +69,7 @@ ip_vs_wlc_schedule(struct ip_vs_service *svc, const struct sk_buff *skb)
list_for_each_entry_continue(dest, &svc->destinations, n_list) {
if (dest->flags & IP_VS_DEST_F_OVERLOAD)
continue;
- doh = ip_vs_dest_conn_overhead(dest);
+ doh = ip_vs_dest_conn_overhead(dest, svc);
if (loh * atomic_read(&dest->weight) >
doh * atomic_read(&least->weight)) {
least = dest;