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