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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
/* $OpenBSD: altq_red.c,v 1.8 2002/12/16 09:18:05 kjc Exp $ */
/* $KAME: altq_red.c,v 1.10 2002/04/03 05:38:51 kjc Exp $ */
/*
* Copyright (C) 1997-2002
* Sony Computer Science Laboratories Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Copyright (c) 1990-1994 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifdef INET6
#include <netinet/ip6.h>
#endif
#include <net/pfvar.h>
#include <altq/altq.h>
#include <altq/altq_red.h>
/*
* ALTQ/RED (Random Early Detection) implementation using 32-bit
* fixed-point calculation.
*
* written by kjc using the ns code as a reference.
* you can learn more about red and ns from Sally's home page at
* http://www-nrg.ee.lbl.gov/floyd/
*
* most of the red parameter values are fixed in this implementation
* to prevent fixed-point overflow/underflow.
* if you change the parameters, watch out for overflow/underflow!
*
* the parameters used are recommended values by Sally.
* the corresponding ns config looks:
* q_weight=0.00195
* minthresh=5 maxthresh=15 queue-size=60
* linterm=30
* dropmech=drop-tail
* bytes=false (can't be handled by 32-bit fixed-point)
* doubleq=false dqthresh=false
* wait=true
*/
/*
* alternative red parameters for a slow link.
*
* assume the queue length becomes from zero to L and keeps L, it takes
* N packets for q_avg to reach 63% of L.
* when q_weight is 0.002, N is about 500 packets.
* for a slow link like dial-up, 500 packets takes more than 1 minute!
* when q_weight is 0.008, N is about 127 packets.
* when q_weight is 0.016, N is about 63 packets.
* bursts of 50 packets are allowd for 0.002, bursts of 25 packets
* are allowed for 0.016.
* see Sally's paper for more details.
*/
/* normal red parameters */
#define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */
/* q_weight = 0.00195 */
/* red parameters for a slow link */
#define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */
/* q_weight = 0.0078125 */
/* red parameters for a very slow link (e.g., dialup) */
#define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */
/* q_weight = 0.015625 */
/* fixed-point uses 12-bit decimal places */
#define FP_SHIFT 12 /* fixed-point shift */
/* red parameters for drop probability */
#define INV_P_MAX 10 /* inverse of max drop probability */
#define TH_MIN 5 /* min threshold */
#define TH_MAX 15 /* max threshold */
#define RED_LIMIT 60 /* default max queue length */
#define RED_STATS /* collect statistics */
/*
* our default policy for forced-drop is drop-tail.
* (in altq-1.1.2 or earlier, the default was random-drop.
* but it makes more sense to punish the cause of the surge.)
* to switch to the random-drop policy, define "RED_RANDOM_DROP".
*/
/* default red parameter values */
static int default_th_min = TH_MIN;
static int default_th_max = TH_MAX;
static int default_inv_pmax = INV_P_MAX;
/*
* red support routines
*/
red_t *
red_alloc(weight, inv_pmax, th_min, th_max, flags, pkttime)
int weight, inv_pmax, th_min, th_max;
int flags, pkttime;
{
red_t *rp;
int w, i;
int npkts_per_sec;
MALLOC(rp, red_t *, sizeof(red_t), M_DEVBUF, M_WAITOK);
if (rp == NULL)
return (NULL);
bzero(rp, sizeof(red_t));
rp->red_avg = 0;
rp->red_idle = 1;
if (weight == 0)
rp->red_weight = W_WEIGHT;
else
rp->red_weight = weight;
if (inv_pmax == 0)
rp->red_inv_pmax = default_inv_pmax;
else
rp->red_inv_pmax = inv_pmax;
if (th_min == 0)
rp->red_thmin = default_th_min;
else
rp->red_thmin = th_min;
if (th_max == 0)
rp->red_thmax = default_th_max;
else
rp->red_thmax = th_max;
rp->red_flags = flags;
if (pkttime == 0)
/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
rp->red_pkttime = 800;
else
rp->red_pkttime = pkttime;
if (weight == 0) {
/* when the link is very slow, adjust red parameters */
npkts_per_sec = 1000000 / rp->red_pkttime;
if (npkts_per_sec < 50) {
/* up to about 400Kbps */
rp->red_weight = W_WEIGHT_2;
} else if (npkts_per_sec < 300) {
/* up to about 2.4Mbps */
rp->red_weight = W_WEIGHT_1;
}
}
/* calculate wshift. weight must be power of 2 */
w = rp->red_weight;
for (i = 0; w > 1; i++)
w = w >> 1;
rp->red_wshift = i;
w = 1 << rp->red_wshift;
if (w != rp->red_weight) {
printf("invalid weight value %d for red! use %d\n",
rp->red_weight, w);
rp->red_weight = w;
}
/*
* thmin_s and thmax_s are scaled versions of th_min and th_max
* to be compared with avg.
*/
rp->red_thmin_s = rp->red_thmin << (rp->red_wshift + FP_SHIFT);
rp->red_thmax_s = rp->red_thmax << (rp->red_wshift + FP_SHIFT);
/*
* precompute probability denominator
* probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point
*/
rp->red_probd = (2 * (rp->red_thmax - rp->red_thmin)
* rp->red_inv_pmax) << FP_SHIFT;
/* allocate weight table */
rp->red_wtab = wtab_alloc(rp->red_weight);
microtime(&rp->red_last);
return (rp);
}
void
red_destroy(rp)
red_t *rp;
{
wtab_destroy(rp->red_wtab);
FREE(rp, M_DEVBUF);
}
void
red_getstats(rp, sp)
red_t *rp;
struct redstats *sp;
{
sp->q_avg = rp->red_avg >> rp->red_wshift;
sp->xmit_cnt = rp->red_stats.xmit_cnt;
sp->drop_cnt = rp->red_stats.drop_cnt;
sp->drop_forced = rp->red_stats.drop_forced;
sp->drop_unforced = rp->red_stats.drop_unforced;
sp->marked_packets = rp->red_stats.marked_packets;
}
int
red_addq(rp, q, m, pktattr)
red_t *rp;
class_queue_t *q;
struct mbuf *m;
struct altq_pktattr *pktattr;
{
int avg, droptype;
int n;
avg = rp->red_avg;
/*
* if we were idle, we pretend that n packets arrived during
* the idle period.
*/
if (rp->red_idle) {
struct timeval now;
int t;
rp->red_idle = 0;
microtime(&now);
t = (now.tv_sec - rp->red_last.tv_sec);
if (t > 60) {
/*
* being idle for more than 1 minute, set avg to zero.
* this prevents t from overflow.
*/
avg = 0;
} else {
t = t * 1000000 + (now.tv_usec - rp->red_last.tv_usec);
n = t / rp->red_pkttime - 1;
/* the following line does (avg = (1 - Wq)^n * avg) */
if (n > 0)
avg = (avg >> FP_SHIFT) *
pow_w(rp->red_wtab, n);
}
}
/* run estimator. (note: avg is scaled by WEIGHT in fixed-point) */
avg += (qlen(q) << FP_SHIFT) - (avg >> rp->red_wshift);
rp->red_avg = avg; /* save the new value */
/*
* red_count keeps a tally of arriving traffic that has not
* been dropped.
*/
rp->red_count++;
/* see if we drop early */
droptype = DTYPE_NODROP;
if (avg >= rp->red_thmin_s && qlen(q) > 1) {
if (avg >= rp->red_thmax_s) {
/* avg >= th_max: forced drop */
droptype = DTYPE_FORCED;
} else if (rp->red_old == 0) {
/* first exceeds th_min */
rp->red_count = 1;
rp->red_old = 1;
} else if (drop_early((avg - rp->red_thmin_s) >> rp->red_wshift,
rp->red_probd, rp->red_count)) {
/* mark or drop by red */
if ((rp->red_flags & REDF_ECN) &&
mark_ecn(m, pktattr, rp->red_flags)) {
/* successfully marked. do not drop. */
rp->red_count = 0;
#ifdef RED_STATS
rp->red_stats.marked_packets++;
#endif
} else {
/* unforced drop by red */
droptype = DTYPE_EARLY;
}
}
} else {
/* avg < th_min */
rp->red_old = 0;
}
/*
* if the queue length hits the hard limit, it's a forced drop.
*/
if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
droptype = DTYPE_FORCED;
#ifdef RED_RANDOM_DROP
/* if successful or forced drop, enqueue this packet. */
if (droptype != DTYPE_EARLY)
_addq(q, m);
#else
/* if successful, enqueue this packet. */
if (droptype == DTYPE_NODROP)
_addq(q, m);
#endif
if (droptype != DTYPE_NODROP) {
if (droptype == DTYPE_EARLY) {
/* drop the incoming packet */
#ifdef RED_STATS
rp->red_stats.drop_unforced++;
#endif
} else {
/* forced drop, select a victim packet in the queue. */
#ifdef RED_RANDOM_DROP
m = _getq_random(q);
#endif
#ifdef RED_STATS
rp->red_stats.drop_forced++;
#endif
}
#ifdef RED_STATS
PKTCNTR_ADD(&rp->red_stats.drop_cnt, m_pktlen(m));
#endif
rp->red_count = 0;
m_freem(m);
return (-1);
}
/* successfully queued */
#ifdef RED_STATS
PKTCNTR_ADD(&rp->red_stats.xmit_cnt, m_pktlen(m));
#endif
return (0);
}
/*
* early-drop probability is calculated as follows:
* prob = p_max * (avg - th_min) / (th_max - th_min)
* prob_a = prob / (2 - count*prob)
* = (avg-th_min) / (2*(th_max-th_min)*inv_p_max - count*(avg-th_min))
* here prob_a increases as successive undrop count increases.
* (prob_a starts from prob/2, becomes prob when (count == (1 / prob)),
* becomes 1 when (count >= (2 / prob))).
*/
int
drop_early(fp_len, fp_probd, count)
int fp_len; /* (avg - TH_MIN) in fixed-point */
int fp_probd; /* (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point */
int count; /* how many successive undropped packets */
{
int d; /* denominator of drop-probability */
d = fp_probd - count * fp_len;
if (d <= 0)
/* count exceeds the hard limit: drop or mark */
return (1);
/*
* now the range of d is [1..600] in fixed-point. (when
* th_max-th_min=10 and p_max=1/30)
* drop probability = (avg - TH_MIN) / d
*/
if ((random() % d) < fp_len) {
/* drop or mark */
return (1);
}
/* no drop/mark */
return (0);
}
/*
* try to mark CE bit to the packet.
* returns 1 if successfully marked, 0 otherwise.
*/
int
mark_ecn(m, pktattr, flags)
struct mbuf *m;
struct altq_pktattr *pktattr;
int flags;
{
struct mbuf *m0;
struct m_tag *t;
struct altq_tag *at;
void *hdr;
int af;
t = m_tag_find(m, PACKET_TAG_PF_QID, NULL);
if (t == NULL)
return (0);
at = (struct altq_tag *)(t + 1);
if (at == NULL)
return (0);
af = at->af;
hdr = at->hdr;
if (af != AF_INET && af != AF_INET6)
return (0);
/* verify that pattr_hdr is within the mbuf data */
for (m0 = m; m0 != NULL; m0 = m0->m_next)
if (((caddr_t)hdr >= m0->m_data) &&
((caddr_t)hdr < m0->m_data + m0->m_len))
break;
if (m0 == NULL) {
/* ick, tag info is stale */
return (0);
}
switch (af) {
case AF_INET:
if (flags & REDF_ECN4) {
struct ip *ip = hdr;
u_int8_t otos;
int sum;
if (ip->ip_v != 4)
return (0); /* version mismatch! */
if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
return (0); /* not-ECT */
if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
return (1); /* already marked */
/*
* ecn-capable but not marked,
* mark CE and update checksum
*/
otos = ip->ip_tos;
ip->ip_tos |= IPTOS_ECN_CE;
/*
* update checksum (from RFC1624)
* HC' = ~(~HC + ~m + m')
*/
sum = ~ntohs(ip->ip_sum) & 0xffff;
sum += (~otos & 0xffff) + ip->ip_tos;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16); /* add carry */
ip->ip_sum = htons(~sum & 0xffff);
return (1);
}
break;
#ifdef INET6
case AF_INET6:
if (flags & REDF_ECN6) {
struct ip6_hdr *ip6 = hdr;
u_int32_t flowlabel;
flowlabel = ntohl(ip6->ip6_flow);
if ((flowlabel >> 28) != 6)
return (0); /* version mismatch! */
if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
(IPTOS_ECN_NOTECT << 20))
return (0); /* not-ECT */
if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
(IPTOS_ECN_CE << 20))
return (1); /* already marked */
/*
* ecn-capable but not marked, mark CE
*/
flowlabel |= (IPTOS_ECN_CE << 20);
ip6->ip6_flow = htonl(flowlabel);
return (1);
}
break;
#endif /* INET6 */
}
/* not marked */
return (0);
}
struct mbuf *
red_getq(rp, q)
red_t *rp;
class_queue_t *q;
{
struct mbuf *m;
if ((m = _getq(q)) == NULL) {
if (rp->red_idle == 0) {
rp->red_idle = 1;
microtime(&rp->red_last);
}
return NULL;
}
rp->red_idle = 0;
return (m);
}
/*
* helper routine to calibrate avg during idle.
* pow_w(wtab, n) returns (1 - Wq)^n in fixed-point
* here Wq = 1/weight and the code assumes Wq is close to zero.
*
* w_tab[n] holds ((1 - Wq)^(2^n)) in fixed-point.
*/
static struct wtab *wtab_list = NULL; /* pointer to wtab list */
struct wtab *
wtab_alloc(weight)
int weight;
{
struct wtab *w;
int i;
for (w = wtab_list; w != NULL; w = w->w_next)
if (w->w_weight == weight) {
w->w_refcount++;
return (w);
}
MALLOC(w, struct wtab *, sizeof(struct wtab), M_DEVBUF, M_WAITOK);
if (w == NULL)
panic("wtab_alloc: malloc failed!");
bzero(w, sizeof(struct wtab));
w->w_weight = weight;
w->w_refcount = 1;
w->w_next = wtab_list;
wtab_list = w;
/* initialize the weight table */
w->w_tab[0] = ((weight - 1) << FP_SHIFT) / weight;
for (i = 1; i < 32; i++) {
w->w_tab[i] = (w->w_tab[i-1] * w->w_tab[i-1]) >> FP_SHIFT;
if (w->w_tab[i] == 0 && w->w_param_max == 0)
w->w_param_max = 1 << i;
}
return (w);
}
int
wtab_destroy(w)
struct wtab *w;
{
struct wtab *prev;
if (--w->w_refcount > 0)
return (0);
if (wtab_list == w)
wtab_list = w->w_next;
else for (prev = wtab_list; prev->w_next != NULL; prev = prev->w_next)
if (prev->w_next == w) {
prev->w_next = w->w_next;
break;
}
FREE(w, M_DEVBUF);
return (0);
}
int32_t
pow_w(w, n)
struct wtab *w;
int n;
{
int i, bit;
int32_t val;
if (n >= w->w_param_max)
return (0);
val = 1 << FP_SHIFT;
if (n <= 0)
return (val);
bit = 1;
i = 0;
while (n) {
if (n & bit) {
val = (val * w->w_tab[i]) >> FP_SHIFT;
n &= ~bit;
}
i++;
bit <<= 1;
}
return (val);
}
|