This source file includes following definitions.
- red_alloc
- red_destroy
- red_getstats
- red_addq
- drop_early
- mark_ecn
- red_getq
- wtab_alloc
- wtab_destroy
- pow_w
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 #include <sys/param.h>
64 #include <sys/malloc.h>
65 #include <sys/mbuf.h>
66 #include <sys/socket.h>
67 #include <sys/systm.h>
68 #include <sys/errno.h>
69
70 #include <net/if.h>
71 #include <net/if_types.h>
72
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #ifdef INET6
77 #include <netinet/ip6.h>
78 #endif
79
80 #include <net/pfvar.h>
81 #include <altq/altq.h>
82 #include <altq/altq_red.h>
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 #define W_WEIGHT 512
121
122
123
124 #define W_WEIGHT_1 128
125
126
127
128 #define W_WEIGHT_2 64
129
130
131
132 #define FP_SHIFT 12
133
134
135 #define INV_P_MAX 10
136 #define TH_MIN 5
137 #define TH_MAX 15
138
139 #define RED_LIMIT 60
140 #define RED_STATS
141
142
143
144
145
146
147
148
149
150 static int default_th_min = TH_MIN;
151 static int default_th_max = TH_MAX;
152 static int default_inv_pmax = INV_P_MAX;
153
154
155
156
157 red_t *
158 red_alloc(int weight, int inv_pmax, int th_min, int th_max, int flags,
159 int pkttime)
160 {
161 red_t *rp;
162 int w, i;
163 int npkts_per_sec;
164
165 MALLOC(rp, red_t *, sizeof(red_t), M_DEVBUF, M_WAITOK);
166 if (rp == NULL)
167 return (NULL);
168 bzero(rp, sizeof(red_t));
169
170 rp->red_avg = 0;
171 rp->red_idle = 1;
172
173 if (weight == 0)
174 rp->red_weight = W_WEIGHT;
175 else
176 rp->red_weight = weight;
177 if (inv_pmax == 0)
178 rp->red_inv_pmax = default_inv_pmax;
179 else
180 rp->red_inv_pmax = inv_pmax;
181 if (th_min == 0)
182 rp->red_thmin = default_th_min;
183 else
184 rp->red_thmin = th_min;
185 if (th_max == 0)
186 rp->red_thmax = default_th_max;
187 else
188 rp->red_thmax = th_max;
189
190 rp->red_flags = flags;
191
192 if (pkttime == 0)
193
194 rp->red_pkttime = 800;
195 else
196 rp->red_pkttime = pkttime;
197
198 if (weight == 0) {
199
200 npkts_per_sec = 1000000 / rp->red_pkttime;
201 if (npkts_per_sec < 50) {
202
203 rp->red_weight = W_WEIGHT_2;
204 } else if (npkts_per_sec < 300) {
205
206 rp->red_weight = W_WEIGHT_1;
207 }
208 }
209
210
211 w = rp->red_weight;
212 for (i = 0; w > 1; i++)
213 w = w >> 1;
214 rp->red_wshift = i;
215 w = 1 << rp->red_wshift;
216 if (w != rp->red_weight) {
217 printf("invalid weight value %d for red! use %d\n",
218 rp->red_weight, w);
219 rp->red_weight = w;
220 }
221
222
223
224
225
226 rp->red_thmin_s = rp->red_thmin << (rp->red_wshift + FP_SHIFT);
227 rp->red_thmax_s = rp->red_thmax << (rp->red_wshift + FP_SHIFT);
228
229
230
231
232
233 rp->red_probd = (2 * (rp->red_thmax - rp->red_thmin)
234 * rp->red_inv_pmax) << FP_SHIFT;
235
236
237 rp->red_wtab = wtab_alloc(rp->red_weight);
238
239 microtime(&rp->red_last);
240 return (rp);
241 }
242
243 void
244 red_destroy(red_t *rp)
245 {
246 wtab_destroy(rp->red_wtab);
247 FREE(rp, M_DEVBUF);
248 }
249
250 void
251 red_getstats(red_t *rp, struct redstats *sp)
252 {
253 sp->q_avg = rp->red_avg >> rp->red_wshift;
254 sp->xmit_cnt = rp->red_stats.xmit_cnt;
255 sp->drop_cnt = rp->red_stats.drop_cnt;
256 sp->drop_forced = rp->red_stats.drop_forced;
257 sp->drop_unforced = rp->red_stats.drop_unforced;
258 sp->marked_packets = rp->red_stats.marked_packets;
259 }
260
261 int
262 red_addq(red_t *rp, class_queue_t *q, struct mbuf *m,
263 struct altq_pktattr *pktattr)
264 {
265 int avg, droptype;
266 int n;
267
268 avg = rp->red_avg;
269
270
271
272
273
274 if (rp->red_idle) {
275 struct timeval now;
276 int t;
277
278 rp->red_idle = 0;
279 microtime(&now);
280 t = (now.tv_sec - rp->red_last.tv_sec);
281 if (t > 60) {
282
283
284
285
286 avg = 0;
287 } else {
288 t = t * 1000000 + (now.tv_usec - rp->red_last.tv_usec);
289 n = t / rp->red_pkttime - 1;
290
291
292 if (n > 0)
293 avg = (avg >> FP_SHIFT) *
294 pow_w(rp->red_wtab, n);
295 }
296 }
297
298
299 avg += (qlen(q) << FP_SHIFT) - (avg >> rp->red_wshift);
300 rp->red_avg = avg;
301
302
303
304
305
306 rp->red_count++;
307
308
309 droptype = DTYPE_NODROP;
310 if (avg >= rp->red_thmin_s && qlen(q) > 1) {
311 if (avg >= rp->red_thmax_s) {
312
313 droptype = DTYPE_FORCED;
314 } else if (rp->red_old == 0) {
315
316 rp->red_count = 1;
317 rp->red_old = 1;
318 } else if (drop_early((avg - rp->red_thmin_s) >> rp->red_wshift,
319 rp->red_probd, rp->red_count)) {
320
321 if ((rp->red_flags & REDF_ECN) &&
322 mark_ecn(m, pktattr, rp->red_flags)) {
323
324 rp->red_count = 0;
325 #ifdef RED_STATS
326 rp->red_stats.marked_packets++;
327 #endif
328 } else {
329
330 droptype = DTYPE_EARLY;
331 }
332 }
333 } else {
334
335 rp->red_old = 0;
336 }
337
338
339
340
341 if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
342 droptype = DTYPE_FORCED;
343
344 #ifdef RED_RANDOM_DROP
345
346 if (droptype != DTYPE_EARLY)
347 _addq(q, m);
348 #else
349
350 if (droptype == DTYPE_NODROP)
351 _addq(q, m);
352 #endif
353 if (droptype != DTYPE_NODROP) {
354 if (droptype == DTYPE_EARLY) {
355
356 #ifdef RED_STATS
357 rp->red_stats.drop_unforced++;
358 #endif
359 } else {
360
361 #ifdef RED_RANDOM_DROP
362 m = _getq_random(q);
363 #endif
364 #ifdef RED_STATS
365 rp->red_stats.drop_forced++;
366 #endif
367 }
368 #ifdef RED_STATS
369 PKTCNTR_ADD(&rp->red_stats.drop_cnt, m_pktlen(m));
370 #endif
371 rp->red_count = 0;
372 m_freem(m);
373 return (-1);
374 }
375
376 #ifdef RED_STATS
377 PKTCNTR_ADD(&rp->red_stats.xmit_cnt, m_pktlen(m));
378 #endif
379 return (0);
380 }
381
382
383
384
385
386
387
388
389
390
391 int
392 drop_early(int fp_len, int fp_probd, int count)
393 {
394 int d;
395
396 d = fp_probd - count * fp_len;
397 if (d <= 0)
398
399 return (1);
400
401
402
403
404
405
406
407 if ((random() % d) < fp_len) {
408
409 return (1);
410 }
411
412 return (0);
413 }
414
415
416
417
418
419 int
420 mark_ecn(struct mbuf *m, struct altq_pktattr *pktattr, int flags)
421 {
422 struct mbuf *m0;
423 void *hdr;
424
425 hdr = m->m_pkthdr.pf.hdr;
426
427
428 for (m0 = m; m0 != NULL; m0 = m0->m_next)
429 if (((caddr_t)(hdr) >= m0->m_data) &&
430 ((caddr_t)(hdr) < m0->m_data + m0->m_len))
431 break;
432 if (m0 == NULL) {
433
434 return (0);
435 }
436
437 switch (((struct ip *)hdr)->ip_v) {
438 case 4:
439 if (flags & REDF_ECN4) {
440 struct ip *ip = hdr;
441 u_int8_t otos;
442 int sum;
443
444 if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
445 return (0);
446 if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
447 return (1);
448
449
450
451
452
453 otos = ip->ip_tos;
454 ip->ip_tos |= IPTOS_ECN_CE;
455
456
457
458
459 sum = ~ntohs(ip->ip_sum) & 0xffff;
460 sum += (~otos & 0xffff) + ip->ip_tos;
461 sum = (sum >> 16) + (sum & 0xffff);
462 sum += (sum >> 16);
463 ip->ip_sum = htons(~sum & 0xffff);
464 return (1);
465 }
466 break;
467 #ifdef INET6
468 case 6:
469 if (flags & REDF_ECN6) {
470 struct ip6_hdr *ip6 = hdr;
471 u_int32_t flowlabel;
472
473 flowlabel = ntohl(ip6->ip6_flow);
474 if ((flowlabel >> 28) != 6)
475 return (0);
476 if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
477 (IPTOS_ECN_NOTECT << 20))
478 return (0);
479 if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
480 (IPTOS_ECN_CE << 20))
481 return (1);
482
483
484
485 flowlabel |= (IPTOS_ECN_CE << 20);
486 ip6->ip6_flow = htonl(flowlabel);
487 return (1);
488 }
489 break;
490 #endif
491 }
492
493
494 return (0);
495 }
496
497 struct mbuf *
498 red_getq(rp, q)
499 red_t *rp;
500 class_queue_t *q;
501 {
502 struct mbuf *m;
503
504 if ((m = _getq(q)) == NULL) {
505 if (rp->red_idle == 0) {
506 rp->red_idle = 1;
507 microtime(&rp->red_last);
508 }
509 return NULL;
510 }
511
512 rp->red_idle = 0;
513 return (m);
514 }
515
516
517
518
519
520
521
522
523 static struct wtab *wtab_list = NULL;
524
525 struct wtab *
526 wtab_alloc(int weight)
527 {
528 struct wtab *w;
529 int i;
530
531 for (w = wtab_list; w != NULL; w = w->w_next)
532 if (w->w_weight == weight) {
533 w->w_refcount++;
534 return (w);
535 }
536
537 MALLOC(w, struct wtab *, sizeof(struct wtab), M_DEVBUF, M_WAITOK);
538 if (w == NULL)
539 panic("wtab_alloc: malloc failed!");
540 bzero(w, sizeof(struct wtab));
541 w->w_weight = weight;
542 w->w_refcount = 1;
543 w->w_next = wtab_list;
544 wtab_list = w;
545
546
547 w->w_tab[0] = ((weight - 1) << FP_SHIFT) / weight;
548 for (i = 1; i < 32; i++) {
549 w->w_tab[i] = (w->w_tab[i-1] * w->w_tab[i-1]) >> FP_SHIFT;
550 if (w->w_tab[i] == 0 && w->w_param_max == 0)
551 w->w_param_max = 1 << i;
552 }
553
554 return (w);
555 }
556
557 int
558 wtab_destroy(struct wtab *w)
559 {
560 struct wtab *prev;
561
562 if (--w->w_refcount > 0)
563 return (0);
564
565 if (wtab_list == w)
566 wtab_list = w->w_next;
567 else for (prev = wtab_list; prev->w_next != NULL; prev = prev->w_next)
568 if (prev->w_next == w) {
569 prev->w_next = w->w_next;
570 break;
571 }
572
573 FREE(w, M_DEVBUF);
574 return (0);
575 }
576
577 int32_t
578 pow_w(struct wtab *w, int n)
579 {
580 int i, bit;
581 int32_t val;
582
583 if (n >= w->w_param_max)
584 return (0);
585
586 val = 1 << FP_SHIFT;
587 if (n <= 0)
588 return (val);
589
590 bit = 1;
591 i = 0;
592 while (n) {
593 if (n & bit) {
594 val = (val * w->w_tab[i]) >> FP_SHIFT;
595 n &= ~bit;
596 }
597 i++;
598 bit <<= 1;
599 }
600 return (val);
601 }