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