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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
|
/* $OpenBSD: altq_wfq.c,v 1.3 2002/03/14 01:26:26 millert Exp $ */
/* $KAME: altq_wfq.c,v 1.7 2000/12/14 08:12:46 thorpej Exp $ */
/*
* Copyright (C) 1997-2000
* 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.
*/
/*
* March 27, 1997. Written by Hiroshi Kyusojin of Keio University
* (kyu@mt.cs.keio.ac.jp).
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <altq/altq.h>
#include <altq/altq_conf.h>
#include <altq/altq_wfq.h>
/*
#define WFQ_DEBUG
*/
static int wfq_setenable(struct wfq_interface *, int);
static int wfq_ifattach(struct wfq_interface *);
static int wfq_ifdetach(struct wfq_interface *);
static int wfq_ifenqueue(struct ifaltq *, struct mbuf *,
struct altq_pktattr *);
static u_long wfq_hash(struct flowinfo *, int);
static __inline u_long wfq_hashbydstaddr(struct flowinfo *, int);
static __inline u_long wfq_hashbysrcport(struct flowinfo *, int);
static wfq *wfq_maxqueue(wfq_state_t *);
static struct mbuf *wfq_ifdequeue(struct ifaltq *, int);
static int wfq_getqid(struct wfq_getqid *);
static int wfq_setweight(struct wfq_setweight *);
static int wfq_getstats(struct wfq_getstats *);
static int wfq_config(struct wfq_conf *);
static int wfq_request(struct ifaltq *, int, void *);
static int wfq_flush(struct ifaltq *);
static void *wfq_classify(void *, struct mbuf *, int);
/* global value : pointer to wfq queue list */
static wfq_state_t *wfq_list = NULL;
static int
wfq_setenable(ifacep, flag)
struct wfq_interface *ifacep;
int flag;
{
wfq_state_t *wfqp;
int error = 0;
if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
return (EBADF);
switch(flag){
case ENABLE:
error = altq_enable(wfqp->ifq);
break;
case DISABLE:
error = altq_disable(wfqp->ifq);
break;
}
return error;
}
static int
wfq_ifattach(ifacep)
struct wfq_interface *ifacep;
{
int error = 0, i;
struct ifnet *ifp;
wfq_state_t *new_wfqp;
wfq *queue;
if ((ifp = ifunit(ifacep->wfq_ifacename)) == NULL) {
#ifdef WFQ_DEBUG
printf("wfq_ifattach()...no ifp found\n");
#endif
return (ENXIO);
}
if (!ALTQ_IS_READY(&ifp->if_snd)) {
#ifdef WFQ_DEBUG
printf("wfq_ifattach()...altq is not ready\n");
#endif
return (ENXIO);
}
/* allocate and initialize wfq_state_t */
MALLOC(new_wfqp, wfq_state_t *, sizeof(wfq_state_t),
M_DEVBUF, M_WAITOK);
if (new_wfqp == NULL)
return (ENOMEM);
bzero(new_wfqp, sizeof(wfq_state_t));
MALLOC(queue, wfq *, sizeof(wfq) * DEFAULT_QSIZE,
M_DEVBUF, M_WAITOK);
if (queue == NULL) {
FREE(new_wfqp, M_DEVBUF);
return (ENOMEM);
}
bzero(queue, sizeof(wfq) * DEFAULT_QSIZE);
/* keep the ifq */
new_wfqp->ifq = &ifp->if_snd;
new_wfqp->nums = DEFAULT_QSIZE;
new_wfqp->hwm = HWM;
new_wfqp->bytes = 0;
new_wfqp->rrp = NULL;
new_wfqp->queue = queue;
new_wfqp->hash_func = wfq_hashbydstaddr;
new_wfqp->fbmask = FIMB4_DADDR;
for (i = 0; i < new_wfqp->nums; i++, queue++) {
queue->next = queue->prev = NULL;
queue->head = queue->tail = NULL;
queue->bytes = queue->quota = 0;
queue->weight = 100;
}
/*
* set WFQ to this ifnet structure.
*/
if ((error = altq_attach(&ifp->if_snd, ALTQT_WFQ, new_wfqp,
wfq_ifenqueue, wfq_ifdequeue, wfq_request,
new_wfqp, wfq_classify)) != 0) {
FREE(queue, M_DEVBUF);
FREE(new_wfqp, M_DEVBUF);
return (error);
}
new_wfqp->next = wfq_list;
wfq_list = new_wfqp;
return (error);
}
static int
wfq_ifdetach(ifacep)
struct wfq_interface *ifacep;
{
int error = 0;
wfq_state_t *wfqp;
if ((wfqp = altq_lookup(ifacep->wfq_ifacename, ALTQT_WFQ)) == NULL)
return (EBADF);
/* free queued mbuf */
wfq_flush(wfqp->ifq);
/* remove WFQ from the ifnet structure. */
(void)altq_disable(wfqp->ifq);
(void)altq_detach(wfqp->ifq);
/* remove from the wfqstate list */
if (wfq_list == wfqp)
wfq_list = wfqp->next;
else {
wfq_state_t *wp = wfq_list;
do {
if (wp->next == wfqp) {
wp->next = wfqp->next;
break;
}
} while ((wp = wp->next) != NULL);
}
/* deallocate wfq_state_t */
FREE(wfqp->queue, M_DEVBUF);
FREE(wfqp, M_DEVBUF);
return (error);
}
static int
wfq_request(ifq, req, arg)
struct ifaltq *ifq;
int req;
void *arg;
{
wfq_state_t *wfqp = (wfq_state_t *)ifq->altq_disc;
switch (req) {
case ALTRQ_PURGE:
wfq_flush(wfqp->ifq);
break;
}
return (0);
}
static int
wfq_flush(ifq)
struct ifaltq *ifq;
{
struct mbuf *mp;
while ((mp = wfq_ifdequeue(ifq, ALTDQ_REMOVE)) != NULL)
m_freem(mp);
if (ALTQ_IS_ENABLED(ifq))
ifq->ifq_len = 0;
return 0;
}
static void *
wfq_classify(clfier, m, af)
void *clfier;
struct mbuf *m;
int af;
{
wfq_state_t *wfqp = (wfq_state_t *)clfier;
struct flowinfo flow;
altq_extractflow(m, af, &flow, wfqp->fbmask);
return (&wfqp->queue[(*wfqp->hash_func)(&flow, wfqp->nums)]);
}
static int
wfq_ifenqueue(ifq, mp, pktattr)
struct ifaltq *ifq;
struct mbuf *mp;
struct altq_pktattr *pktattr;
{
wfq_state_t *wfqp;
wfq *queue;
int byte, error = 0;
wfqp = (wfq_state_t *)ifq->altq_disc;
mp->m_nextpkt = NULL;
/* grab a queue selected by classifier */
if (pktattr == NULL || (queue = pktattr->pattr_class) == NULL)
queue = &wfqp->queue[0];
if (queue->tail == NULL)
queue->head = mp;
else
queue->tail->m_nextpkt = mp;
queue->tail = mp;
byte = mp->m_pkthdr.len;
queue->bytes += byte;
wfqp->bytes += byte;
ifq->ifq_len++;
if (queue->next == NULL) {
/* this queue gets active. add the queue to the active list */
if (wfqp->rrp == NULL){
/* no queue in the active list */
queue->next = queue->prev = queue;
wfqp->rrp = queue;
WFQ_ADDQUOTA(queue);
} else {
/* insert the queue at the tail of the active list */
queue->prev = wfqp->rrp->prev;
wfqp->rrp->prev->next = queue;
wfqp->rrp->prev = queue;
queue->next = wfqp->rrp;
queue->quota = 0;
}
}
/* check overflow. if the total size exceeds the high water mark,
drop packets from the longest queue. */
while (wfqp->bytes > wfqp->hwm) {
wfq *drop_queue = wfq_maxqueue(wfqp);
/* drop the packet at the head. */
mp = drop_queue->head;
if ((drop_queue->head = mp->m_nextpkt) == NULL)
drop_queue->tail = NULL;
mp->m_nextpkt = NULL;
byte = mp->m_pkthdr.len;
drop_queue->bytes -= byte;
PKTCNTR_ADD(&drop_queue->drop_cnt, byte);
wfqp->bytes -= byte;
m_freem(mp);
ifq->ifq_len--;
if(drop_queue == queue)
/* the queue for this flow is selected to drop */
error = ENOBUFS;
}
return error;
}
static u_long wfq_hash(flow, n)
struct flowinfo *flow;
int n;
{
u_long val = 0;
if (flow != NULL) {
if (flow->fi_family == AF_INET) {
struct flowinfo_in *fp = (struct flowinfo_in *)flow;
u_long val2;
val = fp->fi_dst.s_addr ^ fp->fi_src.s_addr;
val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
val2 = fp->fi_dport ^ fp->fi_sport ^ fp->fi_proto;
val2 = val2 ^ (val2 >> 8);
val = val ^ val2;
}
#ifdef INET6
else if (flow->fi_family == AF_INET6) {
struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
val = ntohl(fp6->fi6_flowlabel);
}
#endif
}
return (val % n);
}
static __inline u_long wfq_hashbydstaddr(flow, n)
struct flowinfo *flow;
int n;
{
u_long val = 0;
if (flow != NULL) {
if (flow->fi_family == AF_INET) {
struct flowinfo_in *fp = (struct flowinfo_in *)flow;
val = fp->fi_dst.s_addr;
val = val ^ (val >> 8) ^ (val >> 16) ^ (val >> 24);
}
#ifdef INET6
else if (flow->fi_family == AF_INET6) {
struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
val = ntohl(fp6->fi6_flowlabel);
}
#endif
}
return (val % n);
}
static __inline u_long wfq_hashbysrcport(flow, n)
struct flowinfo *flow;
int n;
{
u_long val = 0;
if (flow != NULL) {
if (flow->fi_family == AF_INET) {
struct flowinfo_in *fp = (struct flowinfo_in *)flow;
val = fp->fi_sport;
}
#ifdef INET6
else if (flow->fi_family == AF_INET6) {
struct flowinfo_in6 *fp6 = (struct flowinfo_in6 *)flow;
val = fp6->fi6_sport;
}
#endif
}
val = val ^ (val >> 8);
return (val % n);
}
static wfq *wfq_maxqueue(wfqp)
wfq_state_t *wfqp;
{
int byte, max_byte = 0;
wfq *queue, *max_queue = NULL;
if((queue = wfqp->rrp) == NULL)
/* never happens */
return NULL;
do{
if ((byte = queue->bytes * 100 / queue->weight) > max_byte) {
max_queue = queue;
max_byte = byte;
}
} while ((queue = queue->next) != wfqp->rrp);
return max_queue;
}
static struct mbuf *
wfq_ifdequeue(ifq, op)
struct ifaltq *ifq;
int op;
{
wfq_state_t *wfqp;
wfq *queue;
struct mbuf *mp;
int byte;
wfqp = (wfq_state_t *)ifq->altq_disc;
if ((wfqp->bytes == 0) || ((queue = wfqp->rrp) == NULL))
/* no packet in the queues */
return NULL;
while (1) {
if (queue->quota > 0) {
if (queue->bytes <= 0) {
/* this queue no longer has packet.
remove the queue from the active list. */
if (queue->next == queue){
/* no other active queue
-- this case never happens in
this algorithm. */
queue->next = queue->prev = NULL;
wfqp->rrp = NULL;
return NULL;
} else {
queue->prev->next = queue->next;
queue->next->prev = queue->prev;
/* the round-robin pointer points
to this queue, advance the rrp */
wfqp->rrp = queue->next;
queue->next = queue->prev = NULL;
queue = wfqp->rrp;
WFQ_ADDQUOTA(queue);
continue;
}
}
/* dequeue a packet from this queue */
mp = queue->head;
if (op == ALTDQ_REMOVE) {
if((queue->head = mp->m_nextpkt) == NULL)
queue->tail = NULL;
byte = mp->m_pkthdr.len;
mp->m_nextpkt = NULL;
queue->quota -= byte;
queue->bytes -= byte;
PKTCNTR_ADD(&queue->xmit_cnt, byte);
wfqp->bytes -= byte;
if (ALTQ_IS_ENABLED(ifq))
ifq->ifq_len--;
}
return mp;
/* if the queue gets empty by this dequeueing,
the queue will be removed from the active list
at the next round */
}
/* advance the round-robin pointer */
queue = wfqp->rrp = queue->next;
WFQ_ADDQUOTA(queue);
}
}
static int
wfq_getqid(gqidp)
struct wfq_getqid *gqidp;
{
wfq_state_t *wfqp;
if ((wfqp = altq_lookup(gqidp->iface.wfq_ifacename, ALTQT_WFQ))
== NULL)
return (EBADF);
gqidp->qid = (*wfqp->hash_func)(&gqidp->flow, wfqp->nums);
return 0;
}
static int
wfq_setweight(swp)
struct wfq_setweight *swp;
{
wfq_state_t *wfqp;
wfq *queue;
int old;
if (swp->weight < 0) {
printf("set weight in natural number\n");
return (EINVAL);
}
if ((wfqp = altq_lookup(swp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
return (EBADF);
queue = &wfqp->queue[swp->qid];
old = queue->weight;
queue->weight = swp->weight;
swp->weight = old;
return 0;
}
static int
wfq_getstats(gsp)
struct wfq_getstats *gsp;
{
wfq_state_t *wfqp;
wfq *queue;
queue_stats *stats;
if ((wfqp = altq_lookup(gsp->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
return (EBADF);
if (gsp->qid >= wfqp->nums)
return (EINVAL);
queue = &wfqp->queue[gsp->qid];
stats = &gsp->stats;
stats->bytes = queue->bytes;
stats->weight = queue->weight;
stats->xmit_cnt = queue->xmit_cnt;
stats->drop_cnt = queue->drop_cnt;
return 0;
}
static int
wfq_config(cf)
struct wfq_conf *cf;
{
wfq_state_t *wfqp;
wfq *queue;
int i, error = 0;
if ((wfqp = altq_lookup(cf->iface.wfq_ifacename, ALTQT_WFQ)) == NULL)
return (EBADF);
if(cf->nqueues <= 0 || MAX_QSIZE < cf->nqueues)
cf->nqueues = DEFAULT_QSIZE;
if (cf->nqueues != wfqp->nums) {
/* free queued mbuf */
wfq_flush(wfqp->ifq);
FREE(wfqp->queue, M_DEVBUF);
MALLOC(queue, wfq *, sizeof(wfq) * cf->nqueues,
M_DEVBUF, M_WAITOK);
if (queue == NULL)
return (ENOMEM);
bzero(queue, sizeof(wfq) * cf->nqueues);
wfqp->nums = cf->nqueues;
wfqp->bytes = 0;
wfqp->rrp = NULL;
wfqp->queue = queue;
for (i = 0; i < wfqp->nums; i++, queue++) {
queue->next = queue->prev = NULL;
queue->head = queue->tail = NULL;
queue->bytes = queue->quota = 0;
queue->weight = 100;
}
}
if (cf->qlimit != 0)
wfqp->hwm = cf->qlimit;
switch (cf->hash_policy) {
case WFQ_HASH_DSTADDR:
wfqp->hash_func = wfq_hashbydstaddr;
wfqp->fbmask = FIMB4_DADDR;
#ifdef INET6
wfqp->fbmask |= FIMB6_FLABEL; /* use flowlabel for ipv6 */
#endif
break;
case WFQ_HASH_SRCPORT:
wfqp->hash_func = wfq_hashbysrcport;
wfqp->fbmask = FIMB4_SPORT;
#ifdef INET6
wfqp->fbmask |= FIMB6_SPORT;
#endif
break;
case WFQ_HASH_FULL:
wfqp->hash_func = wfq_hash;
wfqp->fbmask = FIMB4_ALL;
#ifdef INET6
wfqp->fbmask |= FIMB6_FLABEL; /* use flowlabel for ipv6 */
#endif
break;
default:
error = EINVAL;
break;
}
return error;
}
/*
* wfq device interface
*/
altqdev_decl(wfq);
int
wfqopen(dev, flag, fmt, p)
dev_t dev;
int flag, fmt;
struct proc *p;
{
return 0;
}
int
wfqclose(dev, flag, fmt, p)
dev_t dev;
int flag, fmt;
struct proc *p;
{
struct ifnet *ifp;
struct wfq_interface iface;
wfq_state_t *wfqp;
int s;
s = splimp();
while ((wfqp = wfq_list) != NULL) {
ifp = wfqp->ifq->altq_ifp;
#if defined(__NetBSD__) || defined(__OpenBSD__)
sprintf(iface.wfq_ifacename, "%s", ifp->if_xname);
#else
sprintf(iface.wfq_ifacename, "%s%d",
ifp->if_name, ifp->if_unit);
#endif
wfq_ifdetach(&iface);
}
splx(s);
return 0;
}
int
wfqioctl(dev, cmd, addr, flag, p)
dev_t dev;
ioctlcmd_t cmd;
caddr_t addr;
int flag;
struct proc *p;
{
int error = 0;
int s;
/* check cmd for superuser only */
switch (cmd) {
case WFQ_GET_QID:
case WFQ_GET_STATS:
break;
default:
#if (__FreeBSD_version > 400000)
if ((error = suser(p)) != 0)
#else
if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
#endif
return (error);
break;
}
s = splimp();
switch (cmd) {
case WFQ_ENABLE:
error = wfq_setenable((struct wfq_interface *)addr, ENABLE);
break;
case WFQ_DISABLE:
error = wfq_setenable((struct wfq_interface *)addr, DISABLE);
break;
case WFQ_IF_ATTACH:
error = wfq_ifattach((struct wfq_interface *)addr);
break;
case WFQ_IF_DETACH:
error = wfq_ifdetach((struct wfq_interface *)addr);
break;
case WFQ_GET_QID:
error = wfq_getqid((struct wfq_getqid *)addr);
break;
case WFQ_SET_WEIGHT:
error = wfq_setweight((struct wfq_setweight *)addr);
break;
case WFQ_GET_STATS:
error = wfq_getstats((struct wfq_getstats *)addr);
break;
case WFQ_CONFIG:
error = wfq_config((struct wfq_conf *)addr);
break;
default:
error = EINVAL;
break;
}
splx(s);
return error;
}
#ifdef KLD_MODULE
static struct altqsw wfq_sw =
{"wfq", wfqopen, wfqclose, wfqioctl};
ALTQ_MODULE(altq_wfq, ALTQT_WFQ, &wfq_sw);
#endif /* KLD_MODULE */
|