]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/contrib/altq/altq/altq_hfsc.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / contrib / altq / altq / altq_hfsc.c
1 /*      $FreeBSD$       */
2 /*      $KAME: altq_hfsc.c,v 1.24 2003/12/05 05:40:46 kjc Exp $ */
3
4 /*
5  * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation is hereby granted (including for commercial or
9  * for-profit use), provided that both the copyright notice and this
10  * permission notice appear in all copies of the software, derivative
11  * works, or modified versions, and any portions thereof.
12  *
13  * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
14  * WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON PROVIDES THIS
15  * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26  * DAMAGE.
27  *
28  * Carnegie Mellon encourages (but does not require) users of this
29  * software to return any improvements or extensions that they make,
30  * and to grant Carnegie Mellon the rights to redistribute these
31  * changes without encumbrance.
32  */
33 /*
34  * H-FSC is described in Proceedings of SIGCOMM'97,
35  * "A Hierarchical Fair Service Curve Algorithm for Link-Sharing,
36  * Real-Time and Priority Service"
37  * by Ion Stoica, Hui Zhang, and T. S. Eugene Ng.
38  *
39  * Oleg Cherevko <olwi@aq.ml.com.ua> added the upperlimit for link-sharing.
40  * when a class has an upperlimit, the fit-time is computed from the
41  * upperlimit service curve.  the link-sharing scheduler does not schedule
42  * a class whose fit-time exceeds the current time.
43  */
44
45 #if defined(__FreeBSD__) || defined(__NetBSD__)
46 #include "opt_altq.h"
47 #if (__FreeBSD__ != 2)
48 #include "opt_inet.h"
49 #ifdef __FreeBSD__
50 #include "opt_inet6.h"
51 #endif
52 #endif
53 #endif /* __FreeBSD__ || __NetBSD__ */
54
55 #ifdef ALTQ_HFSC  /* hfsc is enabled by ALTQ_HFSC option in opt_altq.h */
56
57 #include <sys/param.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/systm.h>
62 #include <sys/errno.h>
63 #include <sys/queue.h>
64 #if 1 /* ALTQ3_COMPAT */
65 #include <sys/sockio.h>
66 #include <sys/proc.h>
67 #include <sys/kernel.h>
68 #endif /* ALTQ3_COMPAT */
69
70 #include <net/if.h>
71 #include <netinet/in.h>
72
73 #include <net/pfvar.h>
74 #include <altq/altq.h>
75 #include <altq/altq_hfsc.h>
76 #ifdef ALTQ3_COMPAT
77 #include <altq/altq_conf.h>
78 #endif
79
80 /*
81  * function prototypes
82  */
83 static int                       hfsc_clear_interface(struct hfsc_if *);
84 static int                       hfsc_request(struct ifaltq *, int, void *);
85 static void                      hfsc_purge(struct hfsc_if *);
86 static struct hfsc_class        *hfsc_class_create(struct hfsc_if *,
87     struct service_curve *, struct service_curve *, struct service_curve *,
88     struct hfsc_class *, int, int, int);
89 static int                       hfsc_class_destroy(struct hfsc_class *);
90 static struct hfsc_class        *hfsc_nextclass(struct hfsc_class *);
91 static int                       hfsc_enqueue(struct ifaltq *, struct mbuf *,
92                                     struct altq_pktattr *);
93 static struct mbuf              *hfsc_dequeue(struct ifaltq *, int);
94
95 static int               hfsc_addq(struct hfsc_class *, struct mbuf *);
96 static struct mbuf      *hfsc_getq(struct hfsc_class *);
97 static struct mbuf      *hfsc_pollq(struct hfsc_class *);
98 static void              hfsc_purgeq(struct hfsc_class *);
99
100 static void              update_cfmin(struct hfsc_class *);
101 static void              set_active(struct hfsc_class *, int);
102 static void              set_passive(struct hfsc_class *);
103
104 static void              init_ed(struct hfsc_class *, int);
105 static void              update_ed(struct hfsc_class *, int);
106 static void              update_d(struct hfsc_class *, int);
107 static void              init_vf(struct hfsc_class *, int);
108 static void              update_vf(struct hfsc_class *, int, u_int64_t);
109 static ellist_t         *ellist_alloc(void);
110 static void              ellist_destroy(ellist_t *);
111 static void              ellist_insert(struct hfsc_class *);
112 static void              ellist_remove(struct hfsc_class *);
113 static void              ellist_update(struct hfsc_class *);
114 struct hfsc_class       *ellist_get_mindl(ellist_t *, u_int64_t);
115 static actlist_t        *actlist_alloc(void);
116 static void              actlist_destroy(actlist_t *);
117 static void              actlist_insert(struct hfsc_class *);
118 static void              actlist_remove(struct hfsc_class *);
119 static void              actlist_update(struct hfsc_class *);
120
121 static struct hfsc_class        *actlist_firstfit(struct hfsc_class *,
122                                     u_int64_t);
123
124 static __inline u_int64_t       seg_x2y(u_int64_t, u_int64_t);
125 static __inline u_int64_t       seg_y2x(u_int64_t, u_int64_t);
126 static __inline u_int64_t       m2sm(u_int);
127 static __inline u_int64_t       m2ism(u_int);
128 static __inline u_int64_t       d2dx(u_int);
129 static u_int                    sm2m(u_int64_t);
130 static u_int                    dx2d(u_int64_t);
131
132 static void             sc2isc(struct service_curve *, struct internal_sc *);
133 static void             rtsc_init(struct runtime_sc *, struct internal_sc *,
134                             u_int64_t, u_int64_t);
135 static u_int64_t        rtsc_y2x(struct runtime_sc *, u_int64_t);
136 static u_int64_t        rtsc_x2y(struct runtime_sc *, u_int64_t);
137 static void             rtsc_min(struct runtime_sc *, struct internal_sc *,
138                             u_int64_t, u_int64_t);
139
140 static void                      get_class_stats(struct hfsc_classstats *,
141                                     struct hfsc_class *);
142 static struct hfsc_class        *clh_to_clp(struct hfsc_if *, u_int32_t);
143
144
145 #ifdef ALTQ3_COMPAT
146 static struct hfsc_if *hfsc_attach(struct ifaltq *, u_int);
147 static int hfsc_detach(struct hfsc_if *);
148 static int hfsc_class_modify(struct hfsc_class *, struct service_curve *,
149     struct service_curve *, struct service_curve *);
150
151 static int hfsccmd_if_attach(struct hfsc_attach *);
152 static int hfsccmd_if_detach(struct hfsc_interface *);
153 static int hfsccmd_add_class(struct hfsc_add_class *);
154 static int hfsccmd_delete_class(struct hfsc_delete_class *);
155 static int hfsccmd_modify_class(struct hfsc_modify_class *);
156 static int hfsccmd_add_filter(struct hfsc_add_filter *);
157 static int hfsccmd_delete_filter(struct hfsc_delete_filter *);
158 static int hfsccmd_class_stats(struct hfsc_class_stats *);
159
160 altqdev_decl(hfsc);
161 #endif /* ALTQ3_COMPAT */
162
163 /*
164  * macros
165  */
166 #define is_a_parent_class(cl)   ((cl)->cl_children != NULL)
167
168 #define HT_INFINITY     0xffffffffffffffffLL    /* infinite time value */
169
170 #ifdef ALTQ3_COMPAT
171 /* hif_list keeps all hfsc_if's allocated. */
172 static struct hfsc_if *hif_list = NULL;
173 #endif /* ALTQ3_COMPAT */
174
175 int
176 hfsc_pfattach(struct pf_altq *a)
177 {
178         struct ifnet *ifp;
179         int s, error;
180
181         if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
182                 return (EINVAL);
183 #ifdef __NetBSD__
184         s = splnet();
185 #else
186         s = splimp();
187 #endif
188         error = altq_attach(&ifp->if_snd, ALTQT_HFSC, a->altq_disc,
189             hfsc_enqueue, hfsc_dequeue, hfsc_request, NULL, NULL);
190         splx(s);
191         return (error);
192 }
193
194 int
195 hfsc_add_altq(struct pf_altq *a)
196 {
197         struct hfsc_if *hif;
198         struct ifnet *ifp;
199
200         if ((ifp = ifunit(a->ifname)) == NULL)
201                 return (EINVAL);
202         if (!ALTQ_IS_READY(&ifp->if_snd))
203                 return (ENODEV);
204
205         MALLOC(hif, struct hfsc_if *, sizeof(struct hfsc_if),
206             M_DEVBUF, M_WAITOK);
207         if (hif == NULL)
208                 return (ENOMEM);
209         bzero(hif, sizeof(struct hfsc_if));
210
211         hif->hif_eligible = ellist_alloc();
212         if (hif->hif_eligible == NULL) {
213                 FREE(hif, M_DEVBUF);
214                 return (ENOMEM);
215         }
216
217         hif->hif_ifq = &ifp->if_snd;
218
219         /* keep the state in pf_altq */
220         a->altq_disc = hif;
221
222         return (0);
223 }
224
225 int
226 hfsc_remove_altq(struct pf_altq *a)
227 {
228         struct hfsc_if *hif;
229
230         if ((hif = a->altq_disc) == NULL)
231                 return (EINVAL);
232         a->altq_disc = NULL;
233
234         (void)hfsc_clear_interface(hif);
235         (void)hfsc_class_destroy(hif->hif_rootclass);
236
237         ellist_destroy(hif->hif_eligible);
238
239         FREE(hif, M_DEVBUF);
240
241         return (0);
242 }
243
244 int
245 hfsc_add_queue(struct pf_altq *a)
246 {
247         struct hfsc_if *hif;
248         struct hfsc_class *cl, *parent;
249         struct hfsc_opts *opts;
250         struct service_curve rtsc, lssc, ulsc;
251
252         if ((hif = a->altq_disc) == NULL)
253                 return (EINVAL);
254
255         opts = &a->pq_u.hfsc_opts;
256
257         if (a->parent_qid == HFSC_NULLCLASS_HANDLE &&
258             hif->hif_rootclass == NULL)
259                 parent = NULL;
260         else if ((parent = clh_to_clp(hif, a->parent_qid)) == NULL)
261                 return (EINVAL);
262
263         if (a->qid == 0)
264                 return (EINVAL);
265
266         if (clh_to_clp(hif, a->qid) != NULL)
267                 return (EBUSY);
268
269         rtsc.m1 = opts->rtsc_m1;
270         rtsc.d  = opts->rtsc_d;
271         rtsc.m2 = opts->rtsc_m2;
272         lssc.m1 = opts->lssc_m1;
273         lssc.d  = opts->lssc_d;
274         lssc.m2 = opts->lssc_m2;
275         ulsc.m1 = opts->ulsc_m1;
276         ulsc.d  = opts->ulsc_d;
277         ulsc.m2 = opts->ulsc_m2;
278
279         cl = hfsc_class_create(hif, &rtsc, &lssc, &ulsc,
280             parent, a->qlimit, opts->flags, a->qid);
281         if (cl == NULL)
282                 return (ENOMEM);
283
284         return (0);
285 }
286
287 int
288 hfsc_remove_queue(struct pf_altq *a)
289 {
290         struct hfsc_if *hif;
291         struct hfsc_class *cl;
292
293         if ((hif = a->altq_disc) == NULL)
294                 return (EINVAL);
295
296         if ((cl = clh_to_clp(hif, a->qid)) == NULL)
297                 return (EINVAL);
298
299         return (hfsc_class_destroy(cl));
300 }
301
302 int
303 hfsc_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
304 {
305         struct hfsc_if *hif;
306         struct hfsc_class *cl;
307         struct hfsc_classstats stats;
308         int error = 0;
309
310         if ((hif = altq_lookup(a->ifname, ALTQT_HFSC)) == NULL)
311                 return (EBADF);
312
313         if ((cl = clh_to_clp(hif, a->qid)) == NULL)
314                 return (EINVAL);
315
316         if (*nbytes < sizeof(stats))
317                 return (EINVAL);
318
319         get_class_stats(&stats, cl);
320
321         if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
322                 return (error);
323         *nbytes = sizeof(stats);
324         return (0);
325 }
326
327 /*
328  * bring the interface back to the initial state by discarding
329  * all the filters and classes except the root class.
330  */
331 static int
332 hfsc_clear_interface(struct hfsc_if *hif)
333 {
334         struct hfsc_class       *cl;
335
336 #ifdef ALTQ3_COMPAT
337         /* free the filters for this interface */
338         acc_discard_filters(&hif->hif_classifier, NULL, 1);
339 #endif
340
341         /* clear out the classes */
342         while (hif->hif_rootclass != NULL &&
343             (cl = hif->hif_rootclass->cl_children) != NULL) {
344                 /*
345                  * remove the first leaf class found in the hierarchy
346                  * then start over
347                  */
348                 for (; cl != NULL; cl = hfsc_nextclass(cl)) {
349                         if (!is_a_parent_class(cl)) {
350                                 (void)hfsc_class_destroy(cl);
351                                 break;
352                         }
353                 }
354         }
355
356         return (0);
357 }
358
359 static int
360 hfsc_request(struct ifaltq *ifq, int req, void *arg)
361 {
362         struct hfsc_if  *hif = (struct hfsc_if *)ifq->altq_disc;
363
364         IFQ_LOCK_ASSERT(ifq);
365
366         switch (req) {
367         case ALTRQ_PURGE:
368                 hfsc_purge(hif);
369                 break;
370         }
371         return (0);
372 }
373
374 /* discard all the queued packets on the interface */
375 static void
376 hfsc_purge(struct hfsc_if *hif)
377 {
378         struct hfsc_class *cl;
379
380         for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
381                 if (!qempty(cl->cl_q))
382                         hfsc_purgeq(cl);
383         if (ALTQ_IS_ENABLED(hif->hif_ifq))
384                 hif->hif_ifq->ifq_len = 0;
385 }
386
387 struct hfsc_class *
388 hfsc_class_create(struct hfsc_if *hif, struct service_curve *rsc,
389     struct service_curve *fsc, struct service_curve *usc,
390     struct hfsc_class *parent, int qlimit, int flags, int qid)
391 {
392         struct hfsc_class *cl, *p;
393         int i, s;
394
395         if (hif->hif_classes >= HFSC_MAX_CLASSES)
396                 return (NULL);
397
398 #ifndef ALTQ_RED
399         if (flags & HFCF_RED) {
400 #ifdef ALTQ_DEBUG
401                 printf("hfsc_class_create: RED not configured for HFSC!\n");
402 #endif
403                 return (NULL);
404         }
405 #endif
406
407         MALLOC(cl, struct hfsc_class *, sizeof(struct hfsc_class),
408                M_DEVBUF, M_WAITOK);
409         if (cl == NULL)
410                 return (NULL);
411         bzero(cl, sizeof(struct hfsc_class));
412
413         MALLOC(cl->cl_q, class_queue_t *, sizeof(class_queue_t),
414                M_DEVBUF, M_WAITOK);
415         if (cl->cl_q == NULL)
416                 goto err_ret;
417         bzero(cl->cl_q, sizeof(class_queue_t));
418
419         cl->cl_actc = actlist_alloc();
420         if (cl->cl_actc == NULL)
421                 goto err_ret;
422
423         if (qlimit == 0)
424                 qlimit = 50;  /* use default */
425         qlimit(cl->cl_q) = qlimit;
426         qtype(cl->cl_q) = Q_DROPTAIL;
427         qlen(cl->cl_q) = 0;
428         cl->cl_flags = flags;
429 #ifdef ALTQ_RED
430         if (flags & (HFCF_RED|HFCF_RIO)) {
431                 int red_flags, red_pkttime;
432                 u_int m2;
433
434                 m2 = 0;
435                 if (rsc != NULL && rsc->m2 > m2)
436                         m2 = rsc->m2;
437                 if (fsc != NULL && fsc->m2 > m2)
438                         m2 = fsc->m2;
439                 if (usc != NULL && usc->m2 > m2)
440                         m2 = usc->m2;
441
442                 red_flags = 0;
443                 if (flags & HFCF_ECN)
444                         red_flags |= REDF_ECN;
445 #ifdef ALTQ_RIO
446                 if (flags & HFCF_CLEARDSCP)
447                         red_flags |= RIOF_CLEARDSCP;
448 #endif
449                 if (m2 < 8)
450                         red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
451                 else
452                         red_pkttime = (int64_t)hif->hif_ifq->altq_ifp->if_mtu
453                                 * 1000 * 1000 * 1000 / (m2 / 8);
454                 if (flags & HFCF_RED) {
455                         cl->cl_red = red_alloc(0, 0,
456                             qlimit(cl->cl_q) * 10/100,
457                             qlimit(cl->cl_q) * 30/100,
458                             red_flags, red_pkttime);
459                         if (cl->cl_red != NULL)
460                                 qtype(cl->cl_q) = Q_RED;
461                 }
462 #ifdef ALTQ_RIO
463                 else {
464                         cl->cl_red = (red_t *)rio_alloc(0, NULL,
465                             red_flags, red_pkttime);
466                         if (cl->cl_red != NULL)
467                                 qtype(cl->cl_q) = Q_RIO;
468                 }
469 #endif
470         }
471 #endif /* ALTQ_RED */
472
473         if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0)) {
474                 MALLOC(cl->cl_rsc, struct internal_sc *,
475                     sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
476                 if (cl->cl_rsc == NULL)
477                         goto err_ret;
478                 sc2isc(rsc, cl->cl_rsc);
479                 rtsc_init(&cl->cl_deadline, cl->cl_rsc, 0, 0);
480                 rtsc_init(&cl->cl_eligible, cl->cl_rsc, 0, 0);
481         }
482         if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0)) {
483                 MALLOC(cl->cl_fsc, struct internal_sc *,
484                     sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
485                 if (cl->cl_fsc == NULL)
486                         goto err_ret;
487                 sc2isc(fsc, cl->cl_fsc);
488                 rtsc_init(&cl->cl_virtual, cl->cl_fsc, 0, 0);
489         }
490         if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0)) {
491                 MALLOC(cl->cl_usc, struct internal_sc *,
492                     sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
493                 if (cl->cl_usc == NULL)
494                         goto err_ret;
495                 sc2isc(usc, cl->cl_usc);
496                 rtsc_init(&cl->cl_ulimit, cl->cl_usc, 0, 0);
497         }
498
499         cl->cl_id = hif->hif_classid++;
500         cl->cl_handle = qid;
501         cl->cl_hif = hif;
502         cl->cl_parent = parent;
503
504 #ifdef __NetBSD__
505         s = splnet();
506 #else
507         s = splimp();
508 #endif
509         IFQ_LOCK(hif->hif_ifq);
510         hif->hif_classes++;
511
512         /*
513          * find a free slot in the class table.  if the slot matching
514          * the lower bits of qid is free, use this slot.  otherwise,
515          * use the first free slot.
516          */
517         i = qid % HFSC_MAX_CLASSES;
518         if (hif->hif_class_tbl[i] == NULL)
519                 hif->hif_class_tbl[i] = cl;
520         else {
521                 for (i = 0; i < HFSC_MAX_CLASSES; i++)
522                         if (hif->hif_class_tbl[i] == NULL) {
523                                 hif->hif_class_tbl[i] = cl;
524                                 break;
525                         }
526                 if (i == HFSC_MAX_CLASSES) {
527                         IFQ_UNLOCK(hif->hif_ifq);
528                         splx(s);
529                         goto err_ret;
530                 }
531         }
532
533         if (flags & HFCF_DEFAULTCLASS)
534                 hif->hif_defaultclass = cl;
535
536         if (parent == NULL) {
537                 /* this is root class */
538                 hif->hif_rootclass = cl;
539         } else {
540                 /* add this class to the children list of the parent */
541                 if ((p = parent->cl_children) == NULL)
542                         parent->cl_children = cl;
543                 else {
544                         while (p->cl_siblings != NULL)
545                                 p = p->cl_siblings;
546                         p->cl_siblings = cl;
547                 }
548         }
549         IFQ_UNLOCK(hif->hif_ifq);
550         splx(s);
551
552         return (cl);
553
554  err_ret:
555         if (cl->cl_actc != NULL)
556                 actlist_destroy(cl->cl_actc);
557         if (cl->cl_red != NULL) {
558 #ifdef ALTQ_RIO
559                 if (q_is_rio(cl->cl_q))
560                         rio_destroy((rio_t *)cl->cl_red);
561 #endif
562 #ifdef ALTQ_RED
563                 if (q_is_red(cl->cl_q))
564                         red_destroy(cl->cl_red);
565 #endif
566         }
567         if (cl->cl_fsc != NULL)
568                 FREE(cl->cl_fsc, M_DEVBUF);
569         if (cl->cl_rsc != NULL)
570                 FREE(cl->cl_rsc, M_DEVBUF);
571         if (cl->cl_usc != NULL)
572                 FREE(cl->cl_usc, M_DEVBUF);
573         if (cl->cl_q != NULL)
574                 FREE(cl->cl_q, M_DEVBUF);
575         FREE(cl, M_DEVBUF);
576         return (NULL);
577 }
578
579 static int
580 hfsc_class_destroy(struct hfsc_class *cl)
581 {
582         int i, s;
583
584         if (cl == NULL)
585                 return (0);
586
587         if (is_a_parent_class(cl))
588                 return (EBUSY);
589
590 #ifdef __NetBSD__
591         s = splnet();
592 #else
593         s = splimp();
594 #endif
595         IFQ_LOCK(cl->cl_hif->hif_ifq);
596
597 #ifdef ALTQ3_COMPAT
598         /* delete filters referencing to this class */
599         acc_discard_filters(&cl->cl_hif->hif_classifier, cl, 0);
600 #endif /* ALTQ3_COMPAT */
601
602         if (!qempty(cl->cl_q))
603                 hfsc_purgeq(cl);
604
605         if (cl->cl_parent == NULL) {
606                 /* this is root class */
607         } else {
608                 struct hfsc_class *p = cl->cl_parent->cl_children;
609
610                 if (p == cl)
611                         cl->cl_parent->cl_children = cl->cl_siblings;
612                 else do {
613                         if (p->cl_siblings == cl) {
614                                 p->cl_siblings = cl->cl_siblings;
615                                 break;
616                         }
617                 } while ((p = p->cl_siblings) != NULL);
618                 ASSERT(p != NULL);
619         }
620
621         for (i = 0; i < HFSC_MAX_CLASSES; i++)
622                 if (cl->cl_hif->hif_class_tbl[i] == cl) {
623                         cl->cl_hif->hif_class_tbl[i] = NULL;
624                         break;
625                 }
626
627         cl->cl_hif->hif_classes--;
628         IFQ_UNLOCK(cl->cl_hif->hif_ifq);
629         splx(s);
630
631         actlist_destroy(cl->cl_actc);
632
633         if (cl->cl_red != NULL) {
634 #ifdef ALTQ_RIO
635                 if (q_is_rio(cl->cl_q))
636                         rio_destroy((rio_t *)cl->cl_red);
637 #endif
638 #ifdef ALTQ_RED
639                 if (q_is_red(cl->cl_q))
640                         red_destroy(cl->cl_red);
641 #endif
642         }
643
644         IFQ_LOCK(cl->cl_hif->hif_ifq);
645         if (cl == cl->cl_hif->hif_rootclass)
646                 cl->cl_hif->hif_rootclass = NULL;
647         if (cl == cl->cl_hif->hif_defaultclass)
648                 cl->cl_hif->hif_defaultclass = NULL;
649         IFQ_UNLOCK(cl->cl_hif->hif_ifq);
650
651         if (cl->cl_usc != NULL)
652                 FREE(cl->cl_usc, M_DEVBUF);
653         if (cl->cl_fsc != NULL)
654                 FREE(cl->cl_fsc, M_DEVBUF);
655         if (cl->cl_rsc != NULL)
656                 FREE(cl->cl_rsc, M_DEVBUF);
657         FREE(cl->cl_q, M_DEVBUF);
658         FREE(cl, M_DEVBUF);
659
660         return (0);
661 }
662
663 /*
664  * hfsc_nextclass returns the next class in the tree.
665  *   usage:
666  *      for (cl = hif->hif_rootclass; cl != NULL; cl = hfsc_nextclass(cl))
667  *              do_something;
668  */
669 static struct hfsc_class *
670 hfsc_nextclass(struct hfsc_class *cl)
671 {
672         if (cl->cl_children != NULL)
673                 cl = cl->cl_children;
674         else if (cl->cl_siblings != NULL)
675                 cl = cl->cl_siblings;
676         else {
677                 while ((cl = cl->cl_parent) != NULL)
678                         if (cl->cl_siblings) {
679                                 cl = cl->cl_siblings;
680                                 break;
681                         }
682         }
683
684         return (cl);
685 }
686
687 /*
688  * hfsc_enqueue is an enqueue function to be registered to
689  * (*altq_enqueue) in struct ifaltq.
690  */
691 static int
692 hfsc_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
693 {
694         struct hfsc_if  *hif = (struct hfsc_if *)ifq->altq_disc;
695         struct hfsc_class *cl;
696         struct pf_mtag *t;
697         int len;
698
699         IFQ_LOCK_ASSERT(ifq);
700
701         /* grab class set by classifier */
702         if ((m->m_flags & M_PKTHDR) == 0) {
703                 /* should not happen */
704 #if defined(__NetBSD__) || defined(__OpenBSD__)\
705     || (defined(__FreeBSD__) && __FreeBSD_version >= 501113)
706                 printf("altq: packet for %s does not have pkthdr\n",
707                     ifq->altq_ifp->if_xname);
708 #else
709                 printf("altq: packet for %s%d does not have pkthdr\n",
710                     ifq->altq_ifp->if_name, ifq->altq_ifp->if_unit);
711 #endif
712                 m_freem(m);
713                 return (ENOBUFS);
714         }
715         cl = NULL;
716         if ((t = pf_find_mtag(m)) != NULL)
717                 cl = clh_to_clp(hif, t->qid);
718 #ifdef ALTQ3_COMPAT
719         else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
720                 cl = pktattr->pattr_class;
721 #endif
722         if (cl == NULL || is_a_parent_class(cl)) {
723                 cl = hif->hif_defaultclass;
724                 if (cl == NULL) {
725                         m_freem(m);
726                         return (ENOBUFS);
727                 }
728         }
729 #ifdef ALTQ3_COMPAT
730         if (pktattr != NULL)
731                 cl->cl_pktattr = pktattr;  /* save proto hdr used by ECN */
732         else
733 #endif
734                 cl->cl_pktattr = NULL;
735         len = m_pktlen(m);
736         if (hfsc_addq(cl, m) != 0) {
737                 /* drop occurred.  mbuf was freed in hfsc_addq. */
738                 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, len);
739                 return (ENOBUFS);
740         }
741         IFQ_INC_LEN(ifq);
742         cl->cl_hif->hif_packets++;
743
744         /* successfully queued. */
745         if (qlen(cl->cl_q) == 1)
746                 set_active(cl, m_pktlen(m));
747
748         return (0);
749 }
750
751 /*
752  * hfsc_dequeue is a dequeue function to be registered to
753  * (*altq_dequeue) in struct ifaltq.
754  *
755  * note: ALTDQ_POLL returns the next packet without removing the packet
756  *      from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
757  *      ALTDQ_REMOVE must return the same packet if called immediately
758  *      after ALTDQ_POLL.
759  */
760 static struct mbuf *
761 hfsc_dequeue(struct ifaltq *ifq, int op)
762 {
763         struct hfsc_if  *hif = (struct hfsc_if *)ifq->altq_disc;
764         struct hfsc_class *cl;
765         struct mbuf *m;
766         int len, next_len;
767         int realtime = 0;
768         u_int64_t cur_time;
769
770         IFQ_LOCK_ASSERT(ifq);
771
772         if (hif->hif_packets == 0)
773                 /* no packet in the tree */
774                 return (NULL);
775
776         cur_time = read_machclk();
777
778         if (op == ALTDQ_REMOVE && hif->hif_pollcache != NULL) {
779
780                 cl = hif->hif_pollcache;
781                 hif->hif_pollcache = NULL;
782                 /* check if the class was scheduled by real-time criteria */
783                 if (cl->cl_rsc != NULL)
784                         realtime = (cl->cl_e <= cur_time);
785         } else {
786                 /*
787                  * if there are eligible classes, use real-time criteria.
788                  * find the class with the minimum deadline among
789                  * the eligible classes.
790                  */
791                 if ((cl = ellist_get_mindl(hif->hif_eligible, cur_time))
792                     != NULL) {
793                         realtime = 1;
794                 } else {
795 #ifdef ALTQ_DEBUG
796                         int fits = 0;
797 #endif
798                         /*
799                          * use link-sharing criteria
800                          * get the class with the minimum vt in the hierarchy
801                          */
802                         cl = hif->hif_rootclass;
803                         while (is_a_parent_class(cl)) {
804
805                                 cl = actlist_firstfit(cl, cur_time);
806                                 if (cl == NULL) {
807 #ifdef ALTQ_DEBUG
808                                         if (fits > 0)
809                                                 printf("%d fit but none found\n",fits);
810 #endif
811                                         return (NULL);
812                                 }
813                                 /*
814                                  * update parent's cl_cvtmin.
815                                  * don't update if the new vt is smaller.
816                                  */
817                                 if (cl->cl_parent->cl_cvtmin < cl->cl_vt)
818                                         cl->cl_parent->cl_cvtmin = cl->cl_vt;
819 #ifdef ALTQ_DEBUG
820                                 fits++;
821 #endif
822                         }
823                 }
824
825                 if (op == ALTDQ_POLL) {
826                         hif->hif_pollcache = cl;
827                         m = hfsc_pollq(cl);
828                         return (m);
829                 }
830         }
831
832         m = hfsc_getq(cl);
833         if (m == NULL)
834                 panic("hfsc_dequeue:");
835         len = m_pktlen(m);
836         cl->cl_hif->hif_packets--;
837         IFQ_DEC_LEN(ifq);
838         PKTCNTR_ADD(&cl->cl_stats.xmit_cnt, len);
839
840         update_vf(cl, len, cur_time);
841         if (realtime)
842                 cl->cl_cumul += len;
843
844         if (!qempty(cl->cl_q)) {
845                 if (cl->cl_rsc != NULL) {
846                         /* update ed */
847                         next_len = m_pktlen(qhead(cl->cl_q));
848
849                         if (realtime)
850                                 update_ed(cl, next_len);
851                         else
852                                 update_d(cl, next_len);
853                 }
854         } else {
855                 /* the class becomes passive */
856                 set_passive(cl);
857         }
858
859         return (m);
860 }
861
862 static int
863 hfsc_addq(struct hfsc_class *cl, struct mbuf *m)
864 {
865
866 #ifdef ALTQ_RIO
867         if (q_is_rio(cl->cl_q))
868                 return rio_addq((rio_t *)cl->cl_red, cl->cl_q,
869                                 m, cl->cl_pktattr);
870 #endif
871 #ifdef ALTQ_RED
872         if (q_is_red(cl->cl_q))
873                 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
874 #endif
875         if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
876                 m_freem(m);
877                 return (-1);
878         }
879
880         if (cl->cl_flags & HFCF_CLEARDSCP)
881                 write_dsfield(m, cl->cl_pktattr, 0);
882
883         _addq(cl->cl_q, m);
884
885         return (0);
886 }
887
888 static struct mbuf *
889 hfsc_getq(struct hfsc_class *cl)
890 {
891 #ifdef ALTQ_RIO
892         if (q_is_rio(cl->cl_q))
893                 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
894 #endif
895 #ifdef ALTQ_RED
896         if (q_is_red(cl->cl_q))
897                 return red_getq(cl->cl_red, cl->cl_q);
898 #endif
899         return _getq(cl->cl_q);
900 }
901
902 static struct mbuf *
903 hfsc_pollq(struct hfsc_class *cl)
904 {
905         return qhead(cl->cl_q);
906 }
907
908 static void
909 hfsc_purgeq(struct hfsc_class *cl)
910 {
911         struct mbuf *m;
912
913         if (qempty(cl->cl_q))
914                 return;
915
916         while ((m = _getq(cl->cl_q)) != NULL) {
917                 PKTCNTR_ADD(&cl->cl_stats.drop_cnt, m_pktlen(m));
918                 m_freem(m);
919                 cl->cl_hif->hif_packets--;
920                 IFQ_DEC_LEN(cl->cl_hif->hif_ifq);
921         }
922         ASSERT(qlen(cl->cl_q) == 0);
923
924         update_vf(cl, 0, 0);    /* remove cl from the actlist */
925         set_passive(cl);
926 }
927
928 static void
929 set_active(struct hfsc_class *cl, int len)
930 {
931         if (cl->cl_rsc != NULL)
932                 init_ed(cl, len);
933         if (cl->cl_fsc != NULL)
934                 init_vf(cl, len);
935
936         cl->cl_stats.period++;
937 }
938
939 static void
940 set_passive(struct hfsc_class *cl)
941 {
942         if (cl->cl_rsc != NULL)
943                 ellist_remove(cl);
944
945         /*
946          * actlist is now handled in update_vf() so that update_vf(cl, 0, 0)
947          * needs to be called explicitly to remove a class from actlist
948          */
949 }
950
951 static void
952 init_ed(struct hfsc_class *cl, int next_len)
953 {
954         u_int64_t cur_time;
955
956         cur_time = read_machclk();
957
958         /* update the deadline curve */
959         rtsc_min(&cl->cl_deadline, cl->cl_rsc, cur_time, cl->cl_cumul);
960
961         /*
962          * update the eligible curve.
963          * for concave, it is equal to the deadline curve.
964          * for convex, it is a linear curve with slope m2.
965          */
966         cl->cl_eligible = cl->cl_deadline;
967         if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
968                 cl->cl_eligible.dx = 0;
969                 cl->cl_eligible.dy = 0;
970         }
971
972         /* compute e and d */
973         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
974         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
975
976         ellist_insert(cl);
977 }
978
979 static void
980 update_ed(struct hfsc_class *cl, int next_len)
981 {
982         cl->cl_e = rtsc_y2x(&cl->cl_eligible, cl->cl_cumul);
983         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
984
985         ellist_update(cl);
986 }
987
988 static void
989 update_d(struct hfsc_class *cl, int next_len)
990 {
991         cl->cl_d = rtsc_y2x(&cl->cl_deadline, cl->cl_cumul + next_len);
992 }
993
994 static void
995 init_vf(struct hfsc_class *cl, int len)
996 {
997         struct hfsc_class *max_cl, *p;
998         u_int64_t vt, f, cur_time;
999         int go_active;
1000
1001         cur_time = 0;
1002         go_active = 1;
1003         for ( ; cl->cl_parent != NULL; cl = cl->cl_parent) {
1004
1005                 if (go_active && cl->cl_nactive++ == 0)
1006                         go_active = 1;
1007                 else
1008                         go_active = 0;
1009
1010                 if (go_active) {
1011                         max_cl = actlist_last(cl->cl_parent->cl_actc);
1012                         if (max_cl != NULL) {
1013                                 /*
1014                                  * set vt to the average of the min and max
1015                                  * classes.  if the parent's period didn't
1016                                  * change, don't decrease vt of the class.
1017                                  */
1018                                 vt = max_cl->cl_vt;
1019                                 if (cl->cl_parent->cl_cvtmin != 0)
1020                                         vt = (cl->cl_parent->cl_cvtmin + vt)/2;
1021
1022                                 if (cl->cl_parent->cl_vtperiod !=
1023                                     cl->cl_parentperiod || vt > cl->cl_vt)
1024                                         cl->cl_vt = vt;
1025                         } else {
1026                                 /*
1027                                  * first child for a new parent backlog period.
1028                                  * add parent's cvtmax to vtoff of children
1029                                  * to make a new vt (vtoff + vt) larger than
1030                                  * the vt in the last period for all children.
1031                                  */
1032                                 vt = cl->cl_parent->cl_cvtmax;
1033                                 for (p = cl->cl_parent->cl_children; p != NULL;
1034                                      p = p->cl_siblings)
1035                                         p->cl_vtoff += vt;
1036                                 cl->cl_vt = 0;
1037                                 cl->cl_parent->cl_cvtmax = 0;
1038                                 cl->cl_parent->cl_cvtmin = 0;
1039                         }
1040                         cl->cl_initvt = cl->cl_vt;
1041
1042                         /* update the virtual curve */
1043                         vt = cl->cl_vt + cl->cl_vtoff;
1044                         rtsc_min(&cl->cl_virtual, cl->cl_fsc, vt, cl->cl_total);
1045                         if (cl->cl_virtual.x == vt) {
1046                                 cl->cl_virtual.x -= cl->cl_vtoff;
1047                                 cl->cl_vtoff = 0;
1048                         }
1049                         cl->cl_vtadj = 0;
1050
1051                         cl->cl_vtperiod++;  /* increment vt period */
1052                         cl->cl_parentperiod = cl->cl_parent->cl_vtperiod;
1053                         if (cl->cl_parent->cl_nactive == 0)
1054                                 cl->cl_parentperiod++;
1055                         cl->cl_f = 0;
1056
1057                         actlist_insert(cl);
1058
1059                         if (cl->cl_usc != NULL) {
1060                                 /* class has upper limit curve */
1061                                 if (cur_time == 0)
1062                                         cur_time = read_machclk();
1063
1064                                 /* update the ulimit curve */
1065                                 rtsc_min(&cl->cl_ulimit, cl->cl_usc, cur_time,
1066                                     cl->cl_total);
1067                                 /* compute myf */
1068                                 cl->cl_myf = rtsc_y2x(&cl->cl_ulimit,
1069                                     cl->cl_total);
1070                                 cl->cl_myfadj = 0;
1071                         }
1072                 }
1073
1074                 if (cl->cl_myf > cl->cl_cfmin)
1075                         f = cl->cl_myf;
1076                 else
1077                         f = cl->cl_cfmin;
1078                 if (f != cl->cl_f) {
1079                         cl->cl_f = f;
1080                         update_cfmin(cl->cl_parent);
1081                 }
1082         }
1083 }
1084
1085 static void
1086 update_vf(struct hfsc_class *cl, int len, u_int64_t cur_time)
1087 {
1088         u_int64_t f, myf_bound, delta;
1089         int go_passive;
1090
1091         go_passive = qempty(cl->cl_q);
1092
1093         for (; cl->cl_parent != NULL; cl = cl->cl_parent) {
1094
1095                 cl->cl_total += len;
1096
1097                 if (cl->cl_fsc == NULL || cl->cl_nactive == 0)
1098                         continue;
1099
1100                 if (go_passive && --cl->cl_nactive == 0)
1101                         go_passive = 1;
1102                 else
1103                         go_passive = 0;
1104
1105                 if (go_passive) {
1106                         /* no more active child, going passive */
1107
1108                         /* update cvtmax of the parent class */
1109                         if (cl->cl_vt > cl->cl_parent->cl_cvtmax)
1110                                 cl->cl_parent->cl_cvtmax = cl->cl_vt;
1111
1112                         /* remove this class from the vt list */
1113                         actlist_remove(cl);
1114
1115                         update_cfmin(cl->cl_parent);
1116
1117                         continue;
1118                 }
1119
1120                 /*
1121                  * update vt and f
1122                  */
1123                 cl->cl_vt = rtsc_y2x(&cl->cl_virtual, cl->cl_total)
1124                     - cl->cl_vtoff + cl->cl_vtadj;
1125
1126                 /*
1127                  * if vt of the class is smaller than cvtmin,
1128                  * the class was skipped in the past due to non-fit.
1129                  * if so, we need to adjust vtadj.
1130                  */
1131                 if (cl->cl_vt < cl->cl_parent->cl_cvtmin) {
1132                         cl->cl_vtadj += cl->cl_parent->cl_cvtmin - cl->cl_vt;
1133                         cl->cl_vt = cl->cl_parent->cl_cvtmin;
1134                 }
1135
1136                 /* update the vt list */
1137                 actlist_update(cl);
1138
1139                 if (cl->cl_usc != NULL) {
1140                         cl->cl_myf = cl->cl_myfadj
1141                             + rtsc_y2x(&cl->cl_ulimit, cl->cl_total);
1142
1143                         /*
1144                          * if myf lags behind by more than one clock tick
1145                          * from the current time, adjust myfadj to prevent
1146                          * a rate-limited class from going greedy.
1147                          * in a steady state under rate-limiting, myf
1148                          * fluctuates within one clock tick.
1149                          */
1150                         myf_bound = cur_time - machclk_per_tick;
1151                         if (cl->cl_myf < myf_bound) {
1152                                 delta = cur_time - cl->cl_myf;
1153                                 cl->cl_myfadj += delta;
1154                                 cl->cl_myf += delta;
1155                         }
1156                 }
1157
1158                 /* cl_f is max(cl_myf, cl_cfmin) */
1159                 if (cl->cl_myf > cl->cl_cfmin)
1160                         f = cl->cl_myf;
1161                 else
1162                         f = cl->cl_cfmin;
1163                 if (f != cl->cl_f) {
1164                         cl->cl_f = f;
1165                         update_cfmin(cl->cl_parent);
1166                 }
1167         }
1168 }
1169
1170 static void
1171 update_cfmin(struct hfsc_class *cl)
1172 {
1173         struct hfsc_class *p;
1174         u_int64_t cfmin;
1175
1176         if (TAILQ_EMPTY(cl->cl_actc)) {
1177                 cl->cl_cfmin = 0;
1178                 return;
1179         }
1180         cfmin = HT_INFINITY;
1181         TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1182                 if (p->cl_f == 0) {
1183                         cl->cl_cfmin = 0;
1184                         return;
1185                 }
1186                 if (p->cl_f < cfmin)
1187                         cfmin = p->cl_f;
1188         }
1189         cl->cl_cfmin = cfmin;
1190 }
1191
1192 /*
1193  * TAILQ based ellist and actlist implementation
1194  * (ion wanted to make a calendar queue based implementation)
1195  */
1196 /*
1197  * eligible list holds backlogged classes being sorted by their eligible times.
1198  * there is one eligible list per interface.
1199  */
1200
1201 static ellist_t *
1202 ellist_alloc(void)
1203 {
1204         ellist_t *head;
1205
1206         MALLOC(head, ellist_t *, sizeof(ellist_t), M_DEVBUF, M_WAITOK);
1207         TAILQ_INIT(head);
1208         return (head);
1209 }
1210
1211 static void
1212 ellist_destroy(ellist_t *head)
1213 {
1214         FREE(head, M_DEVBUF);
1215 }
1216
1217 static void
1218 ellist_insert(struct hfsc_class *cl)
1219 {
1220         struct hfsc_if  *hif = cl->cl_hif;
1221         struct hfsc_class *p;
1222
1223         /* check the last entry first */
1224         if ((p = TAILQ_LAST(hif->hif_eligible, _eligible)) == NULL ||
1225             p->cl_e <= cl->cl_e) {
1226                 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1227                 return;
1228         }
1229
1230         TAILQ_FOREACH(p, hif->hif_eligible, cl_ellist) {
1231                 if (cl->cl_e < p->cl_e) {
1232                         TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1233                         return;
1234                 }
1235         }
1236         ASSERT(0); /* should not reach here */
1237 }
1238
1239 static void
1240 ellist_remove(struct hfsc_class *cl)
1241 {
1242         struct hfsc_if  *hif = cl->cl_hif;
1243
1244         TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1245 }
1246
1247 static void
1248 ellist_update(struct hfsc_class *cl)
1249 {
1250         struct hfsc_if  *hif = cl->cl_hif;
1251         struct hfsc_class *p, *last;
1252
1253         /*
1254          * the eligible time of a class increases monotonically.
1255          * if the next entry has a larger eligible time, nothing to do.
1256          */
1257         p = TAILQ_NEXT(cl, cl_ellist);
1258         if (p == NULL || cl->cl_e <= p->cl_e)
1259                 return;
1260
1261         /* check the last entry */
1262         last = TAILQ_LAST(hif->hif_eligible, _eligible);
1263         ASSERT(last != NULL);
1264         if (last->cl_e <= cl->cl_e) {
1265                 TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1266                 TAILQ_INSERT_TAIL(hif->hif_eligible, cl, cl_ellist);
1267                 return;
1268         }
1269
1270         /*
1271          * the new position must be between the next entry
1272          * and the last entry
1273          */
1274         while ((p = TAILQ_NEXT(p, cl_ellist)) != NULL) {
1275                 if (cl->cl_e < p->cl_e) {
1276                         TAILQ_REMOVE(hif->hif_eligible, cl, cl_ellist);
1277                         TAILQ_INSERT_BEFORE(p, cl, cl_ellist);
1278                         return;
1279                 }
1280         }
1281         ASSERT(0); /* should not reach here */
1282 }
1283
1284 /* find the class with the minimum deadline among the eligible classes */
1285 struct hfsc_class *
1286 ellist_get_mindl(ellist_t *head, u_int64_t cur_time)
1287 {
1288         struct hfsc_class *p, *cl = NULL;
1289
1290         TAILQ_FOREACH(p, head, cl_ellist) {
1291                 if (p->cl_e > cur_time)
1292                         break;
1293                 if (cl == NULL || p->cl_d < cl->cl_d)
1294                         cl = p;
1295         }
1296         return (cl);
1297 }
1298
1299 /*
1300  * active children list holds backlogged child classes being sorted
1301  * by their virtual time.
1302  * each intermediate class has one active children list.
1303  */
1304 static actlist_t *
1305 actlist_alloc(void)
1306 {
1307         actlist_t *head;
1308
1309         MALLOC(head, actlist_t *, sizeof(actlist_t), M_DEVBUF, M_WAITOK);
1310         TAILQ_INIT(head);
1311         return (head);
1312 }
1313
1314 static void
1315 actlist_destroy(actlist_t *head)
1316 {
1317         FREE(head, M_DEVBUF);
1318 }
1319 static void
1320 actlist_insert(struct hfsc_class *cl)
1321 {
1322         struct hfsc_class *p;
1323
1324         /* check the last entry first */
1325         if ((p = TAILQ_LAST(cl->cl_parent->cl_actc, _active)) == NULL
1326             || p->cl_vt <= cl->cl_vt) {
1327                 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1328                 return;
1329         }
1330
1331         TAILQ_FOREACH(p, cl->cl_parent->cl_actc, cl_actlist) {
1332                 if (cl->cl_vt < p->cl_vt) {
1333                         TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1334                         return;
1335                 }
1336         }
1337         ASSERT(0); /* should not reach here */
1338 }
1339
1340 static void
1341 actlist_remove(struct hfsc_class *cl)
1342 {
1343         TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1344 }
1345
1346 static void
1347 actlist_update(struct hfsc_class *cl)
1348 {
1349         struct hfsc_class *p, *last;
1350
1351         /*
1352          * the virtual time of a class increases monotonically during its
1353          * backlogged period.
1354          * if the next entry has a larger virtual time, nothing to do.
1355          */
1356         p = TAILQ_NEXT(cl, cl_actlist);
1357         if (p == NULL || cl->cl_vt < p->cl_vt)
1358                 return;
1359
1360         /* check the last entry */
1361         last = TAILQ_LAST(cl->cl_parent->cl_actc, _active);
1362         ASSERT(last != NULL);
1363         if (last->cl_vt <= cl->cl_vt) {
1364                 TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1365                 TAILQ_INSERT_TAIL(cl->cl_parent->cl_actc, cl, cl_actlist);
1366                 return;
1367         }
1368
1369         /*
1370          * the new position must be between the next entry
1371          * and the last entry
1372          */
1373         while ((p = TAILQ_NEXT(p, cl_actlist)) != NULL) {
1374                 if (cl->cl_vt < p->cl_vt) {
1375                         TAILQ_REMOVE(cl->cl_parent->cl_actc, cl, cl_actlist);
1376                         TAILQ_INSERT_BEFORE(p, cl, cl_actlist);
1377                         return;
1378                 }
1379         }
1380         ASSERT(0); /* should not reach here */
1381 }
1382
1383 static struct hfsc_class *
1384 actlist_firstfit(struct hfsc_class *cl, u_int64_t cur_time)
1385 {
1386         struct hfsc_class *p;
1387
1388         TAILQ_FOREACH(p, cl->cl_actc, cl_actlist) {
1389                 if (p->cl_f <= cur_time)
1390                         return (p);
1391         }
1392         return (NULL);
1393 }
1394
1395 /*
1396  * service curve support functions
1397  *
1398  *  external service curve parameters
1399  *      m: bits/sec
1400  *      d: msec
1401  *  internal service curve parameters
1402  *      sm: (bytes/tsc_interval) << SM_SHIFT
1403  *      ism: (tsc_count/byte) << ISM_SHIFT
1404  *      dx: tsc_count
1405  *
1406  * SM_SHIFT and ISM_SHIFT are scaled in order to keep effective digits.
1407  * we should be able to handle 100K-1Gbps linkspeed with 200Hz-1GHz CPU
1408  * speed.  SM_SHIFT and ISM_SHIFT are selected to have at least 3 effective
1409  * digits in decimal using the following table.
1410  *
1411  *  bits/sec    100Kbps     1Mbps     10Mbps     100Mbps    1Gbps
1412  *  ----------+-------------------------------------------------------
1413  *  bytes/nsec  12.5e-6    125e-6     1250e-6    12500e-6   125000e-6
1414  *  sm(500MHz)  25.0e-6    250e-6     2500e-6    25000e-6   250000e-6
1415  *  sm(200MHz)  62.5e-6    625e-6     6250e-6    62500e-6   625000e-6
1416  *
1417  *  nsec/byte   80000      8000       800        80         8
1418  *  ism(500MHz) 40000      4000       400        40         4
1419  *  ism(200MHz) 16000      1600       160        16         1.6
1420  */
1421 #define SM_SHIFT        24
1422 #define ISM_SHIFT       10
1423
1424 #define SM_MASK         ((1LL << SM_SHIFT) - 1)
1425 #define ISM_MASK        ((1LL << ISM_SHIFT) - 1)
1426
1427 static __inline u_int64_t
1428 seg_x2y(u_int64_t x, u_int64_t sm)
1429 {
1430         u_int64_t y;
1431
1432         /*
1433          * compute
1434          *      y = x * sm >> SM_SHIFT
1435          * but divide it for the upper and lower bits to avoid overflow
1436          */
1437         y = (x >> SM_SHIFT) * sm + (((x & SM_MASK) * sm) >> SM_SHIFT);
1438         return (y);
1439 }
1440
1441 static __inline u_int64_t
1442 seg_y2x(u_int64_t y, u_int64_t ism)
1443 {
1444         u_int64_t x;
1445
1446         if (y == 0)
1447                 x = 0;
1448         else if (ism == HT_INFINITY)
1449                 x = HT_INFINITY;
1450         else {
1451                 x = (y >> ISM_SHIFT) * ism
1452                     + (((y & ISM_MASK) * ism) >> ISM_SHIFT);
1453         }
1454         return (x);
1455 }
1456
1457 static __inline u_int64_t
1458 m2sm(u_int m)
1459 {
1460         u_int64_t sm;
1461
1462         sm = ((u_int64_t)m << SM_SHIFT) / 8 / machclk_freq;
1463         return (sm);
1464 }
1465
1466 static __inline u_int64_t
1467 m2ism(u_int m)
1468 {
1469         u_int64_t ism;
1470
1471         if (m == 0)
1472                 ism = HT_INFINITY;
1473         else
1474                 ism = ((u_int64_t)machclk_freq << ISM_SHIFT) * 8 / m;
1475         return (ism);
1476 }
1477
1478 static __inline u_int64_t
1479 d2dx(u_int d)
1480 {
1481         u_int64_t dx;
1482
1483         dx = ((u_int64_t)d * machclk_freq) / 1000;
1484         return (dx);
1485 }
1486
1487 static u_int
1488 sm2m(u_int64_t sm)
1489 {
1490         u_int64_t m;
1491
1492         m = (sm * 8 * machclk_freq) >> SM_SHIFT;
1493         return ((u_int)m);
1494 }
1495
1496 static u_int
1497 dx2d(u_int64_t dx)
1498 {
1499         u_int64_t d;
1500
1501         d = dx * 1000 / machclk_freq;
1502         return ((u_int)d);
1503 }
1504
1505 static void
1506 sc2isc(struct service_curve *sc, struct internal_sc *isc)
1507 {
1508         isc->sm1 = m2sm(sc->m1);
1509         isc->ism1 = m2ism(sc->m1);
1510         isc->dx = d2dx(sc->d);
1511         isc->dy = seg_x2y(isc->dx, isc->sm1);
1512         isc->sm2 = m2sm(sc->m2);
1513         isc->ism2 = m2ism(sc->m2);
1514 }
1515
1516 /*
1517  * initialize the runtime service curve with the given internal
1518  * service curve starting at (x, y).
1519  */
1520 static void
1521 rtsc_init(struct runtime_sc *rtsc, struct internal_sc * isc, u_int64_t x,
1522     u_int64_t y)
1523 {
1524         rtsc->x =       x;
1525         rtsc->y =       y;
1526         rtsc->sm1 =     isc->sm1;
1527         rtsc->ism1 =    isc->ism1;
1528         rtsc->dx =      isc->dx;
1529         rtsc->dy =      isc->dy;
1530         rtsc->sm2 =     isc->sm2;
1531         rtsc->ism2 =    isc->ism2;
1532 }
1533
1534 /*
1535  * calculate the y-projection of the runtime service curve by the
1536  * given x-projection value
1537  */
1538 static u_int64_t
1539 rtsc_y2x(struct runtime_sc *rtsc, u_int64_t y)
1540 {
1541         u_int64_t       x;
1542
1543         if (y < rtsc->y)
1544                 x = rtsc->x;
1545         else if (y <= rtsc->y + rtsc->dy) {
1546                 /* x belongs to the 1st segment */
1547                 if (rtsc->dy == 0)
1548                         x = rtsc->x + rtsc->dx;
1549                 else
1550                         x = rtsc->x + seg_y2x(y - rtsc->y, rtsc->ism1);
1551         } else {
1552                 /* x belongs to the 2nd segment */
1553                 x = rtsc->x + rtsc->dx
1554                     + seg_y2x(y - rtsc->y - rtsc->dy, rtsc->ism2);
1555         }
1556         return (x);
1557 }
1558
1559 static u_int64_t
1560 rtsc_x2y(struct runtime_sc *rtsc, u_int64_t x)
1561 {
1562         u_int64_t       y;
1563
1564         if (x <= rtsc->x)
1565                 y = rtsc->y;
1566         else if (x <= rtsc->x + rtsc->dx)
1567                 /* y belongs to the 1st segment */
1568                 y = rtsc->y + seg_x2y(x - rtsc->x, rtsc->sm1);
1569         else
1570                 /* y belongs to the 2nd segment */
1571                 y = rtsc->y + rtsc->dy
1572                     + seg_x2y(x - rtsc->x - rtsc->dx, rtsc->sm2);
1573         return (y);
1574 }
1575
1576 /*
1577  * update the runtime service curve by taking the minimum of the current
1578  * runtime service curve and the service curve starting at (x, y).
1579  */
1580 static void
1581 rtsc_min(struct runtime_sc *rtsc, struct internal_sc *isc, u_int64_t x,
1582     u_int64_t y)
1583 {
1584         u_int64_t       y1, y2, dx, dy;
1585
1586         if (isc->sm1 <= isc->sm2) {
1587                 /* service curve is convex */
1588                 y1 = rtsc_x2y(rtsc, x);
1589                 if (y1 < y)
1590                         /* the current rtsc is smaller */
1591                         return;
1592                 rtsc->x = x;
1593                 rtsc->y = y;
1594                 return;
1595         }
1596
1597         /*
1598          * service curve is concave
1599          * compute the two y values of the current rtsc
1600          *      y1: at x
1601          *      y2: at (x + dx)
1602          */
1603         y1 = rtsc_x2y(rtsc, x);
1604         if (y1 <= y) {
1605                 /* rtsc is below isc, no change to rtsc */
1606                 return;
1607         }
1608
1609         y2 = rtsc_x2y(rtsc, x + isc->dx);
1610         if (y2 >= y + isc->dy) {
1611                 /* rtsc is above isc, replace rtsc by isc */
1612                 rtsc->x = x;
1613                 rtsc->y = y;
1614                 rtsc->dx = isc->dx;
1615                 rtsc->dy = isc->dy;
1616                 return;
1617         }
1618
1619         /*
1620          * the two curves intersect
1621          * compute the offsets (dx, dy) using the reverse
1622          * function of seg_x2y()
1623          *      seg_x2y(dx, sm1) == seg_x2y(dx, sm2) + (y1 - y)
1624          */
1625         dx = ((y1 - y) << SM_SHIFT) / (isc->sm1 - isc->sm2);
1626         /*
1627          * check if (x, y1) belongs to the 1st segment of rtsc.
1628          * if so, add the offset.
1629          */
1630         if (rtsc->x + rtsc->dx > x)
1631                 dx += rtsc->x + rtsc->dx - x;
1632         dy = seg_x2y(dx, isc->sm1);
1633
1634         rtsc->x = x;
1635         rtsc->y = y;
1636         rtsc->dx = dx;
1637         rtsc->dy = dy;
1638         return;
1639 }
1640
1641 static void
1642 get_class_stats(struct hfsc_classstats *sp, struct hfsc_class *cl)
1643 {
1644         sp->class_id = cl->cl_id;
1645         sp->class_handle = cl->cl_handle;
1646
1647         if (cl->cl_rsc != NULL) {
1648                 sp->rsc.m1 = sm2m(cl->cl_rsc->sm1);
1649                 sp->rsc.d = dx2d(cl->cl_rsc->dx);
1650                 sp->rsc.m2 = sm2m(cl->cl_rsc->sm2);
1651         } else {
1652                 sp->rsc.m1 = 0;
1653                 sp->rsc.d = 0;
1654                 sp->rsc.m2 = 0;
1655         }
1656         if (cl->cl_fsc != NULL) {
1657                 sp->fsc.m1 = sm2m(cl->cl_fsc->sm1);
1658                 sp->fsc.d = dx2d(cl->cl_fsc->dx);
1659                 sp->fsc.m2 = sm2m(cl->cl_fsc->sm2);
1660         } else {
1661                 sp->fsc.m1 = 0;
1662                 sp->fsc.d = 0;
1663                 sp->fsc.m2 = 0;
1664         }
1665         if (cl->cl_usc != NULL) {
1666                 sp->usc.m1 = sm2m(cl->cl_usc->sm1);
1667                 sp->usc.d = dx2d(cl->cl_usc->dx);
1668                 sp->usc.m2 = sm2m(cl->cl_usc->sm2);
1669         } else {
1670                 sp->usc.m1 = 0;
1671                 sp->usc.d = 0;
1672                 sp->usc.m2 = 0;
1673         }
1674
1675         sp->total = cl->cl_total;
1676         sp->cumul = cl->cl_cumul;
1677
1678         sp->d = cl->cl_d;
1679         sp->e = cl->cl_e;
1680         sp->vt = cl->cl_vt;
1681         sp->f = cl->cl_f;
1682
1683         sp->initvt = cl->cl_initvt;
1684         sp->vtperiod = cl->cl_vtperiod;
1685         sp->parentperiod = cl->cl_parentperiod;
1686         sp->nactive = cl->cl_nactive;
1687         sp->vtoff = cl->cl_vtoff;
1688         sp->cvtmax = cl->cl_cvtmax;
1689         sp->myf = cl->cl_myf;
1690         sp->cfmin = cl->cl_cfmin;
1691         sp->cvtmin = cl->cl_cvtmin;
1692         sp->myfadj = cl->cl_myfadj;
1693         sp->vtadj = cl->cl_vtadj;
1694
1695         sp->cur_time = read_machclk();
1696         sp->machclk_freq = machclk_freq;
1697
1698         sp->qlength = qlen(cl->cl_q);
1699         sp->qlimit = qlimit(cl->cl_q);
1700         sp->xmit_cnt = cl->cl_stats.xmit_cnt;
1701         sp->drop_cnt = cl->cl_stats.drop_cnt;
1702         sp->period = cl->cl_stats.period;
1703
1704         sp->qtype = qtype(cl->cl_q);
1705 #ifdef ALTQ_RED
1706         if (q_is_red(cl->cl_q))
1707                 red_getstats(cl->cl_red, &sp->red[0]);
1708 #endif
1709 #ifdef ALTQ_RIO
1710         if (q_is_rio(cl->cl_q))
1711                 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
1712 #endif
1713 }
1714
1715 /* convert a class handle to the corresponding class pointer */
1716 static struct hfsc_class *
1717 clh_to_clp(struct hfsc_if *hif, u_int32_t chandle)
1718 {
1719         int i;
1720         struct hfsc_class *cl;
1721
1722         if (chandle == 0)
1723                 return (NULL);
1724         /*
1725          * first, try optimistically the slot matching the lower bits of
1726          * the handle.  if it fails, do the linear table search.
1727          */
1728         i = chandle % HFSC_MAX_CLASSES;
1729         if ((cl = hif->hif_class_tbl[i]) != NULL && cl->cl_handle == chandle)
1730                 return (cl);
1731         for (i = 0; i < HFSC_MAX_CLASSES; i++)
1732                 if ((cl = hif->hif_class_tbl[i]) != NULL &&
1733                     cl->cl_handle == chandle)
1734                         return (cl);
1735         return (NULL);
1736 }
1737
1738 #ifdef ALTQ3_COMPAT
1739 static struct hfsc_if *
1740 hfsc_attach(ifq, bandwidth)
1741         struct ifaltq *ifq;
1742         u_int bandwidth;
1743 {
1744         struct hfsc_if *hif;
1745
1746         MALLOC(hif, struct hfsc_if *, sizeof(struct hfsc_if),
1747                M_DEVBUF, M_WAITOK);
1748         if (hif == NULL)
1749                 return (NULL);
1750         bzero(hif, sizeof(struct hfsc_if));
1751
1752         hif->hif_eligible = ellist_alloc();
1753         if (hif->hif_eligible == NULL) {
1754                 FREE(hif, M_DEVBUF);
1755                 return NULL;
1756         }
1757
1758         hif->hif_ifq = ifq;
1759
1760         /* add this state to the hfsc list */
1761         hif->hif_next = hif_list;
1762         hif_list = hif;
1763
1764         return (hif);
1765 }
1766
1767 static int
1768 hfsc_detach(hif)
1769         struct hfsc_if *hif;
1770 {
1771         (void)hfsc_clear_interface(hif);
1772         (void)hfsc_class_destroy(hif->hif_rootclass);
1773
1774         /* remove this interface from the hif list */
1775         if (hif_list == hif)
1776                 hif_list = hif->hif_next;
1777         else {
1778                 struct hfsc_if *h;
1779
1780                 for (h = hif_list; h != NULL; h = h->hif_next)
1781                         if (h->hif_next == hif) {
1782                                 h->hif_next = hif->hif_next;
1783                                 break;
1784                         }
1785                 ASSERT(h != NULL);
1786         }
1787
1788         ellist_destroy(hif->hif_eligible);
1789
1790         FREE(hif, M_DEVBUF);
1791
1792         return (0);
1793 }
1794
1795 static int
1796 hfsc_class_modify(cl, rsc, fsc, usc)
1797         struct hfsc_class *cl;
1798         struct service_curve *rsc, *fsc, *usc;
1799 {
1800         struct internal_sc *rsc_tmp, *fsc_tmp, *usc_tmp;
1801         u_int64_t cur_time;
1802         int s;
1803
1804         rsc_tmp = fsc_tmp = usc_tmp = NULL;
1805         if (rsc != NULL && (rsc->m1 != 0 || rsc->m2 != 0) &&
1806             cl->cl_rsc == NULL) {
1807                 MALLOC(rsc_tmp, struct internal_sc *,
1808                        sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
1809                 if (rsc_tmp == NULL)
1810                         return (ENOMEM);
1811         }
1812         if (fsc != NULL && (fsc->m1 != 0 || fsc->m2 != 0) &&
1813             cl->cl_fsc == NULL) {
1814                 MALLOC(fsc_tmp, struct internal_sc *,
1815                        sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
1816                 if (fsc_tmp == NULL)
1817                         return (ENOMEM);
1818         }
1819         if (usc != NULL && (usc->m1 != 0 || usc->m2 != 0) &&
1820             cl->cl_usc == NULL) {
1821                 MALLOC(usc_tmp, struct internal_sc *,
1822                        sizeof(struct internal_sc), M_DEVBUF, M_WAITOK);
1823                 if (usc_tmp == NULL)
1824                         return (ENOMEM);
1825         }
1826
1827         cur_time = read_machclk();
1828 #ifdef __NetBSD__
1829         s = splnet();
1830 #else
1831         s = splimp();
1832 #endif
1833         IFQ_LOCK(cl->cl_hif->hif_ifq);
1834
1835         if (rsc != NULL) {
1836                 if (rsc->m1 == 0 && rsc->m2 == 0) {
1837                         if (cl->cl_rsc != NULL) {
1838                                 if (!qempty(cl->cl_q))
1839                                         hfsc_purgeq(cl);
1840                                 FREE(cl->cl_rsc, M_DEVBUF);
1841                                 cl->cl_rsc = NULL;
1842                         }
1843                 } else {
1844                         if (cl->cl_rsc == NULL)
1845                                 cl->cl_rsc = rsc_tmp;
1846                         sc2isc(rsc, cl->cl_rsc);
1847                         rtsc_init(&cl->cl_deadline, cl->cl_rsc, cur_time,
1848                             cl->cl_cumul);
1849                         cl->cl_eligible = cl->cl_deadline;
1850                         if (cl->cl_rsc->sm1 <= cl->cl_rsc->sm2) {
1851                                 cl->cl_eligible.dx = 0;
1852                                 cl->cl_eligible.dy = 0;
1853                         }
1854                 }
1855         }
1856
1857         if (fsc != NULL) {
1858                 if (fsc->m1 == 0 && fsc->m2 == 0) {
1859                         if (cl->cl_fsc != NULL) {
1860                                 if (!qempty(cl->cl_q))
1861                                         hfsc_purgeq(cl);
1862                                 FREE(cl->cl_fsc, M_DEVBUF);
1863                                 cl->cl_fsc = NULL;
1864                         }
1865                 } else {
1866                         if (cl->cl_fsc == NULL)
1867                                 cl->cl_fsc = fsc_tmp;
1868                         sc2isc(fsc, cl->cl_fsc);
1869                         rtsc_init(&cl->cl_virtual, cl->cl_fsc, cl->cl_vt,
1870                             cl->cl_total);
1871                 }
1872         }
1873
1874         if (usc != NULL) {
1875                 if (usc->m1 == 0 && usc->m2 == 0) {
1876                         if (cl->cl_usc != NULL) {
1877                                 FREE(cl->cl_usc, M_DEVBUF);
1878                                 cl->cl_usc = NULL;
1879                                 cl->cl_myf = 0;
1880                         }
1881                 } else {
1882                         if (cl->cl_usc == NULL)
1883                                 cl->cl_usc = usc_tmp;
1884                         sc2isc(usc, cl->cl_usc);
1885                         rtsc_init(&cl->cl_ulimit, cl->cl_usc, cur_time,
1886                             cl->cl_total);
1887                 }
1888         }
1889
1890         if (!qempty(cl->cl_q)) {
1891                 if (cl->cl_rsc != NULL)
1892                         update_ed(cl, m_pktlen(qhead(cl->cl_q)));
1893                 if (cl->cl_fsc != NULL)
1894                         update_vf(cl, 0, cur_time);
1895                 /* is this enough? */
1896         }
1897
1898         IFQ_UNLOCK(cl->cl_hif->hif_ifq);
1899         splx(s);
1900
1901         return (0);
1902 }
1903
1904 /*
1905  * hfsc device interface
1906  */
1907 int
1908 hfscopen(dev, flag, fmt, p)
1909         dev_t dev;
1910         int flag, fmt;
1911 #if (__FreeBSD_version > 500000)
1912         struct thread *p;
1913 #else
1914         struct proc *p;
1915 #endif
1916 {
1917         if (machclk_freq == 0)
1918                 init_machclk();
1919
1920         if (machclk_freq == 0) {
1921                 printf("hfsc: no cpu clock available!\n");
1922                 return (ENXIO);
1923         }
1924
1925         /* everything will be done when the queueing scheme is attached. */
1926         return 0;
1927 }
1928
1929 int
1930 hfscclose(dev, flag, fmt, p)
1931         dev_t dev;
1932         int flag, fmt;
1933 #if (__FreeBSD_version > 500000)
1934         struct thread *p;
1935 #else
1936         struct proc *p;
1937 #endif
1938 {
1939         struct hfsc_if *hif;
1940         int err, error = 0;
1941
1942         while ((hif = hif_list) != NULL) {
1943                 /* destroy all */
1944                 if (ALTQ_IS_ENABLED(hif->hif_ifq))
1945                         altq_disable(hif->hif_ifq);
1946
1947                 err = altq_detach(hif->hif_ifq);
1948                 if (err == 0)
1949                         err = hfsc_detach(hif);
1950                 if (err != 0 && error == 0)
1951                         error = err;
1952         }
1953
1954         return error;
1955 }
1956
1957 int
1958 hfscioctl(dev, cmd, addr, flag, p)
1959         dev_t dev;
1960         ioctlcmd_t cmd;
1961         caddr_t addr;
1962         int flag;
1963 #if (__FreeBSD_version > 500000)
1964         struct thread *p;
1965 #else
1966         struct proc *p;
1967 #endif
1968 {
1969         struct hfsc_if *hif;
1970         struct hfsc_interface *ifacep;
1971         int     error = 0;
1972
1973         /* check super-user privilege */
1974         switch (cmd) {
1975         case HFSC_GETSTATS:
1976                 break;
1977         default:
1978 #if (__FreeBSD_version > 700000)
1979                 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
1980                         return (error);
1981 #elsif (__FreeBSD_version > 400000)
1982                 if ((error = suser(p)) != 0)
1983                         return (error);
1984 #else
1985                 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1986                         return (error);
1987 #endif
1988                 break;
1989         }
1990
1991         switch (cmd) {
1992
1993         case HFSC_IF_ATTACH:
1994                 error = hfsccmd_if_attach((struct hfsc_attach *)addr);
1995                 break;
1996
1997         case HFSC_IF_DETACH:
1998                 error = hfsccmd_if_detach((struct hfsc_interface *)addr);
1999                 break;
2000
2001         case HFSC_ENABLE:
2002         case HFSC_DISABLE:
2003         case HFSC_CLEAR_HIERARCHY:
2004                 ifacep = (struct hfsc_interface *)addr;
2005                 if ((hif = altq_lookup(ifacep->hfsc_ifname,
2006                                        ALTQT_HFSC)) == NULL) {
2007                         error = EBADF;
2008                         break;
2009                 }
2010
2011                 switch (cmd) {
2012
2013                 case HFSC_ENABLE:
2014                         if (hif->hif_defaultclass == NULL) {
2015 #ifdef ALTQ_DEBUG
2016                                 printf("hfsc: no default class\n");
2017 #endif
2018                                 error = EINVAL;
2019                                 break;
2020                         }
2021                         error = altq_enable(hif->hif_ifq);
2022                         break;
2023
2024                 case HFSC_DISABLE:
2025                         error = altq_disable(hif->hif_ifq);
2026                         break;
2027
2028                 case HFSC_CLEAR_HIERARCHY:
2029                         hfsc_clear_interface(hif);
2030                         break;
2031                 }
2032                 break;
2033
2034         case HFSC_ADD_CLASS:
2035                 error = hfsccmd_add_class((struct hfsc_add_class *)addr);
2036                 break;
2037
2038         case HFSC_DEL_CLASS:
2039                 error = hfsccmd_delete_class((struct hfsc_delete_class *)addr);
2040                 break;
2041
2042         case HFSC_MOD_CLASS:
2043                 error = hfsccmd_modify_class((struct hfsc_modify_class *)addr);
2044                 break;
2045
2046         case HFSC_ADD_FILTER:
2047                 error = hfsccmd_add_filter((struct hfsc_add_filter *)addr);
2048                 break;
2049
2050         case HFSC_DEL_FILTER:
2051                 error = hfsccmd_delete_filter((struct hfsc_delete_filter *)addr);
2052                 break;
2053
2054         case HFSC_GETSTATS:
2055                 error = hfsccmd_class_stats((struct hfsc_class_stats *)addr);
2056                 break;
2057
2058         default:
2059                 error = EINVAL;
2060                 break;
2061         }
2062         return error;
2063 }
2064
2065 static int
2066 hfsccmd_if_attach(ap)
2067         struct hfsc_attach *ap;
2068 {
2069         struct hfsc_if *hif;
2070         struct ifnet *ifp;
2071         int error;
2072
2073         if ((ifp = ifunit(ap->iface.hfsc_ifname)) == NULL)
2074                 return (ENXIO);
2075
2076         if ((hif = hfsc_attach(&ifp->if_snd, ap->bandwidth)) == NULL)
2077                 return (ENOMEM);
2078
2079         /*
2080          * set HFSC to this ifnet structure.
2081          */
2082         if ((error = altq_attach(&ifp->if_snd, ALTQT_HFSC, hif,
2083                                  hfsc_enqueue, hfsc_dequeue, hfsc_request,
2084                                  &hif->hif_classifier, acc_classify)) != 0)
2085                 (void)hfsc_detach(hif);
2086
2087         return (error);
2088 }
2089
2090 static int
2091 hfsccmd_if_detach(ap)
2092         struct hfsc_interface *ap;
2093 {
2094         struct hfsc_if *hif;
2095         int error;
2096
2097         if ((hif = altq_lookup(ap->hfsc_ifname, ALTQT_HFSC)) == NULL)
2098                 return (EBADF);
2099
2100         if (ALTQ_IS_ENABLED(hif->hif_ifq))
2101                 altq_disable(hif->hif_ifq);
2102
2103         if ((error = altq_detach(hif->hif_ifq)))
2104                 return (error);
2105
2106         return hfsc_detach(hif);
2107 }
2108
2109 static int
2110 hfsccmd_add_class(ap)
2111         struct hfsc_add_class *ap;
2112 {
2113         struct hfsc_if *hif;
2114         struct hfsc_class *cl, *parent;
2115         int     i;
2116
2117         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2118                 return (EBADF);
2119
2120         if (ap->parent_handle == HFSC_NULLCLASS_HANDLE &&
2121             hif->hif_rootclass == NULL)
2122                 parent = NULL;
2123         else if ((parent = clh_to_clp(hif, ap->parent_handle)) == NULL)
2124                 return (EINVAL);
2125
2126         /* assign a class handle (use a free slot number for now) */
2127         for (i = 1; i < HFSC_MAX_CLASSES; i++)
2128                 if (hif->hif_class_tbl[i] == NULL)
2129                         break;
2130         if (i == HFSC_MAX_CLASSES)
2131                 return (EBUSY);
2132
2133         if ((cl = hfsc_class_create(hif, &ap->service_curve, NULL, NULL,
2134             parent, ap->qlimit, ap->flags, i)) == NULL)
2135                 return (ENOMEM);
2136
2137         /* return a class handle to the user */
2138         ap->class_handle = i;
2139
2140         return (0);
2141 }
2142
2143 static int
2144 hfsccmd_delete_class(ap)
2145         struct hfsc_delete_class *ap;
2146 {
2147         struct hfsc_if *hif;
2148         struct hfsc_class *cl;
2149
2150         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2151                 return (EBADF);
2152
2153         if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2154                 return (EINVAL);
2155
2156         return hfsc_class_destroy(cl);
2157 }
2158
2159 static int
2160 hfsccmd_modify_class(ap)
2161         struct hfsc_modify_class *ap;
2162 {
2163         struct hfsc_if *hif;
2164         struct hfsc_class *cl;
2165         struct service_curve *rsc = NULL;
2166         struct service_curve *fsc = NULL;
2167         struct service_curve *usc = NULL;
2168
2169         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2170                 return (EBADF);
2171
2172         if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2173                 return (EINVAL);
2174
2175         if (ap->sctype & HFSC_REALTIMESC)
2176                 rsc = &ap->service_curve;
2177         if (ap->sctype & HFSC_LINKSHARINGSC)
2178                 fsc = &ap->service_curve;
2179         if (ap->sctype & HFSC_UPPERLIMITSC)
2180                 usc = &ap->service_curve;
2181
2182         return hfsc_class_modify(cl, rsc, fsc, usc);
2183 }
2184
2185 static int
2186 hfsccmd_add_filter(ap)
2187         struct hfsc_add_filter *ap;
2188 {
2189         struct hfsc_if *hif;
2190         struct hfsc_class *cl;
2191
2192         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2193                 return (EBADF);
2194
2195         if ((cl = clh_to_clp(hif, ap->class_handle)) == NULL)
2196                 return (EINVAL);
2197
2198         if (is_a_parent_class(cl)) {
2199 #ifdef ALTQ_DEBUG
2200                 printf("hfsccmd_add_filter: not a leaf class!\n");
2201 #endif
2202                 return (EINVAL);
2203         }
2204
2205         return acc_add_filter(&hif->hif_classifier, &ap->filter,
2206                               cl, &ap->filter_handle);
2207 }
2208
2209 static int
2210 hfsccmd_delete_filter(ap)
2211         struct hfsc_delete_filter *ap;
2212 {
2213         struct hfsc_if *hif;
2214
2215         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2216                 return (EBADF);
2217
2218         return acc_delete_filter(&hif->hif_classifier,
2219                                  ap->filter_handle);
2220 }
2221
2222 static int
2223 hfsccmd_class_stats(ap)
2224         struct hfsc_class_stats *ap;
2225 {
2226         struct hfsc_if *hif;
2227         struct hfsc_class *cl;
2228         struct hfsc_classstats stats, *usp;
2229         int     n, nclasses, error;
2230
2231         if ((hif = altq_lookup(ap->iface.hfsc_ifname, ALTQT_HFSC)) == NULL)
2232                 return (EBADF);
2233
2234         ap->cur_time = read_machclk();
2235         ap->machclk_freq = machclk_freq;
2236         ap->hif_classes = hif->hif_classes;
2237         ap->hif_packets = hif->hif_packets;
2238
2239         /* skip the first N classes in the tree */
2240         nclasses = ap->nskip;
2241         for (cl = hif->hif_rootclass, n = 0; cl != NULL && n < nclasses;
2242              cl = hfsc_nextclass(cl), n++)
2243                 ;
2244         if (n != nclasses)
2245                 return (EINVAL);
2246
2247         /* then, read the next N classes in the tree */
2248         nclasses = ap->nclasses;
2249         usp = ap->stats;
2250         for (n = 0; cl != NULL && n < nclasses; cl = hfsc_nextclass(cl), n++) {
2251
2252                 get_class_stats(&stats, cl);
2253
2254                 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
2255                                      sizeof(stats))) != 0)
2256                         return (error);
2257         }
2258
2259         ap->nclasses = n;
2260
2261         return (0);
2262 }
2263
2264 #ifdef KLD_MODULE
2265
2266 static struct altqsw hfsc_sw =
2267         {"hfsc", hfscopen, hfscclose, hfscioctl};
2268
2269 ALTQ_MODULE(altq_hfsc, ALTQT_HFSC, &hfsc_sw);
2270 MODULE_DEPEND(altq_hfsc, altq_red, 1, 1, 1);
2271 MODULE_DEPEND(altq_hfsc, altq_rio, 1, 1, 1);
2272
2273 #endif /* KLD_MODULE */
2274 #endif /* ALTQ3_COMPAT */
2275
2276 #endif /* ALTQ_HFSC */