Home | History | Annotate | Line # | Download | only in altq
altq_cdnr.c revision 1.10.10.1
      1  1.10.10.1      elad /*	$NetBSD: altq_cdnr.c,v 1.10.10.1 2006/03/08 00:23:46 elad Exp $	*/
      2        1.1   thorpej /*	$KAME: altq_cdnr.c,v 1.8 2000/12/14 08:12:45 thorpej Exp $	*/
      3        1.1   thorpej 
      4        1.1   thorpej /*
      5        1.1   thorpej  * Copyright (C) 1999-2000
      6        1.1   thorpej  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7        1.1   thorpej  *
      8        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      9        1.1   thorpej  * modification, are permitted provided that the following conditions
     10        1.1   thorpej  * are met:
     11        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     15        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     16        1.1   thorpej  *
     17        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19        1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20        1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21        1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22        1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23        1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24        1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25        1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26        1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27        1.1   thorpej  * SUCH DAMAGE.
     28        1.1   thorpej  */
     29        1.4     lukem 
     30        1.4     lukem #include <sys/cdefs.h>
     31  1.10.10.1      elad __KERNEL_RCSID(0, "$NetBSD: altq_cdnr.c,v 1.10.10.1 2006/03/08 00:23:46 elad Exp $");
     32        1.1   thorpej 
     33        1.1   thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
     34        1.1   thorpej #include "opt_altq.h"
     35        1.1   thorpej #if (__FreeBSD__ != 2)
     36        1.1   thorpej #include "opt_inet.h"
     37        1.1   thorpej #ifdef __FreeBSD__
     38        1.1   thorpej #include "opt_inet6.h"
     39        1.1   thorpej #endif
     40        1.1   thorpej #endif
     41        1.1   thorpej #endif /* __FreeBSD__ || __NetBSD__ */
     42        1.1   thorpej 
     43        1.1   thorpej #include <sys/param.h>
     44        1.1   thorpej #include <sys/malloc.h>
     45        1.1   thorpej #include <sys/mbuf.h>
     46        1.1   thorpej #include <sys/socket.h>
     47        1.1   thorpej #include <sys/sockio.h>
     48        1.1   thorpej #include <sys/systm.h>
     49        1.1   thorpej #include <sys/proc.h>
     50        1.1   thorpej #include <sys/errno.h>
     51        1.1   thorpej #include <sys/kernel.h>
     52        1.1   thorpej #include <sys/queue.h>
     53        1.1   thorpej 
     54        1.1   thorpej #include <net/if.h>
     55        1.1   thorpej #include <net/if_types.h>
     56        1.1   thorpej #include <netinet/in.h>
     57        1.1   thorpej #include <netinet/in_systm.h>
     58        1.1   thorpej #include <netinet/ip.h>
     59        1.1   thorpej #ifdef INET6
     60        1.1   thorpej #include <netinet/ip6.h>
     61        1.1   thorpej #endif
     62        1.1   thorpej 
     63        1.1   thorpej #include <altq/altq.h>
     64        1.1   thorpej #include <altq/altq_conf.h>
     65        1.1   thorpej #include <altq/altq_cdnr.h>
     66        1.1   thorpej 
     67        1.1   thorpej /*
     68        1.1   thorpej  * diffserv traffic conditioning module
     69        1.1   thorpej  */
     70        1.1   thorpej 
     71        1.1   thorpej int altq_cdnr_enabled = 0;
     72        1.1   thorpej 
     73        1.1   thorpej /* traffic conditioner is enabled by ALTQ_CDNR option in opt_altq.h */
     74        1.1   thorpej #ifdef ALTQ_CDNR
     75        1.1   thorpej 
     76        1.1   thorpej /* cdnr_list keeps all cdnr's allocated. */
     77        1.1   thorpej static LIST_HEAD(, top_cdnr) tcb_list;
     78        1.1   thorpej 
     79       1.10  christos int cdnropen __P((dev_t, int, int, struct lwp *));
     80       1.10  christos int cdnrclose __P((dev_t, int, int, struct lwp *));
     81       1.10  christos int cdnrioctl __P((dev_t, ioctlcmd_t, caddr_t, int, struct lwp *));
     82        1.1   thorpej 
     83        1.1   thorpej static int altq_cdnr_input __P((struct mbuf *, int));
     84        1.1   thorpej static struct top_cdnr *tcb_lookup __P((char *ifname));
     85        1.1   thorpej static struct cdnr_block *cdnr_handle2cb __P((u_long));
     86        1.1   thorpej static u_long cdnr_cb2handle __P((struct cdnr_block *));
     87        1.1   thorpej static void *cdnr_cballoc __P((struct top_cdnr *, int,
     88        1.1   thorpej        struct tc_action *(*)(struct cdnr_block *, struct cdnr_pktinfo *)));
     89        1.1   thorpej static void cdnr_cbdestroy __P((void *));
     90        1.1   thorpej static int tca_verify_action __P((struct tc_action *));
     91        1.1   thorpej static void tca_import_action __P((struct tc_action *, struct tc_action *));
     92        1.1   thorpej static void tca_invalidate_action __P((struct tc_action *));
     93        1.1   thorpej 
     94        1.1   thorpej static int generic_element_destroy __P((struct cdnr_block *));
     95        1.1   thorpej static struct top_cdnr *top_create __P((struct ifaltq *));
     96        1.1   thorpej static int top_destroy __P((struct top_cdnr *));
     97        1.1   thorpej static struct cdnr_block *element_create __P((struct top_cdnr *,
     98        1.1   thorpej 					      struct tc_action *));
     99        1.1   thorpej static int element_destroy __P((struct cdnr_block *));
    100        1.1   thorpej static void tb_import_profile __P((struct tbe *, struct tb_profile *));
    101        1.1   thorpej static struct tbmeter *tbm_create __P((struct top_cdnr *, struct tb_profile *,
    102        1.1   thorpej 			   struct tc_action *, struct tc_action *));
    103        1.1   thorpej static int tbm_destroy __P((struct tbmeter *));
    104        1.1   thorpej static struct tc_action *tbm_input __P((struct cdnr_block *,
    105        1.1   thorpej 					struct cdnr_pktinfo *));
    106        1.1   thorpej static struct trtcm *trtcm_create __P((struct top_cdnr *,
    107        1.1   thorpej 		  struct tb_profile *, struct tb_profile *,
    108        1.1   thorpej 		  struct tc_action *, struct tc_action *, struct tc_action *,
    109        1.1   thorpej 		  int));
    110        1.1   thorpej static int trtcm_destroy __P((struct trtcm *));
    111        1.1   thorpej static struct tc_action *trtcm_input __P((struct cdnr_block *,
    112        1.1   thorpej 					  struct cdnr_pktinfo *));
    113        1.1   thorpej static struct tswtcm *tswtcm_create __P((struct top_cdnr *,
    114        1.1   thorpej 		  u_int32_t, u_int32_t, u_int32_t,
    115        1.1   thorpej 		  struct tc_action *, struct tc_action *, struct tc_action *));
    116        1.1   thorpej static int tswtcm_destroy __P((struct tswtcm *));
    117        1.1   thorpej static struct tc_action *tswtcm_input __P((struct cdnr_block *,
    118        1.1   thorpej 					   struct cdnr_pktinfo *));
    119        1.1   thorpej 
    120        1.1   thorpej static int cdnrcmd_if_attach __P((char *));
    121        1.1   thorpej static int cdnrcmd_if_detach __P((char *));
    122        1.1   thorpej static int cdnrcmd_add_element __P((struct cdnr_add_element *));
    123        1.1   thorpej static int cdnrcmd_delete_element __P((struct cdnr_delete_element *));
    124        1.1   thorpej static int cdnrcmd_add_filter __P((struct cdnr_add_filter *));
    125        1.1   thorpej static int cdnrcmd_delete_filter __P((struct cdnr_delete_filter *));
    126        1.1   thorpej static int cdnrcmd_add_tbm __P((struct cdnr_add_tbmeter *));
    127        1.1   thorpej static int cdnrcmd_modify_tbm __P((struct cdnr_modify_tbmeter *));
    128        1.1   thorpej static int cdnrcmd_tbm_stats __P((struct cdnr_tbmeter_stats *));
    129        1.1   thorpej static int cdnrcmd_add_trtcm __P((struct cdnr_add_trtcm *));
    130        1.1   thorpej static int cdnrcmd_modify_trtcm __P((struct cdnr_modify_trtcm *));
    131        1.1   thorpej static int cdnrcmd_tcm_stats __P((struct cdnr_tcm_stats *));
    132        1.1   thorpej static int cdnrcmd_add_tswtcm __P((struct cdnr_add_tswtcm *));
    133        1.1   thorpej static int cdnrcmd_modify_tswtcm __P((struct cdnr_modify_tswtcm *));
    134        1.1   thorpej static int cdnrcmd_get_stats __P((struct cdnr_get_stats *));
    135        1.1   thorpej 
    136        1.1   thorpej /*
    137        1.1   thorpej  * top level input function called from ip_input.
    138        1.1   thorpej  * should be called before converting header fields to host-byte-order.
    139        1.1   thorpej  */
    140        1.1   thorpej int
    141        1.1   thorpej altq_cdnr_input(m, af)
    142        1.1   thorpej 	struct mbuf	*m;
    143        1.1   thorpej 	int		af;	/* address family */
    144        1.1   thorpej {
    145        1.1   thorpej 	struct ifnet		*ifp;
    146        1.1   thorpej 	struct ip		*ip;
    147        1.1   thorpej 	struct top_cdnr		*top;
    148        1.1   thorpej 	struct tc_action	*tca;
    149        1.1   thorpej 	struct cdnr_block	*cb;
    150        1.1   thorpej 	struct cdnr_pktinfo	pktinfo;
    151        1.1   thorpej 
    152        1.1   thorpej 	ifp = m->m_pkthdr.rcvif;
    153        1.1   thorpej 	if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
    154        1.1   thorpej 		/* traffic conditioner is not enabled on this interface */
    155        1.1   thorpej 		return (1);
    156        1.1   thorpej 
    157        1.1   thorpej 	top = ifp->if_snd.altq_cdnr;
    158        1.1   thorpej 
    159        1.1   thorpej 	ip = mtod(m, struct ip *);
    160        1.1   thorpej #ifdef INET6
    161        1.1   thorpej 	if (af == AF_INET6) {
    162        1.1   thorpej 		u_int32_t flowlabel;
    163        1.9     perry 
    164        1.1   thorpej 		flowlabel = ((struct ip6_hdr *)ip)->ip6_flow;
    165        1.1   thorpej 		pktinfo.pkt_dscp = (ntohl(flowlabel) >> 20) & DSCP_MASK;
    166        1.1   thorpej 	} else
    167        1.1   thorpej #endif
    168        1.1   thorpej 		pktinfo.pkt_dscp = ip->ip_tos & DSCP_MASK;
    169        1.1   thorpej 	pktinfo.pkt_len = m_pktlen(m);
    170        1.1   thorpej 
    171        1.1   thorpej 	tca = NULL;
    172        1.1   thorpej 
    173        1.1   thorpej 	cb = acc_classify(&top->tc_classifier, m, af);
    174        1.1   thorpej 	if (cb != NULL)
    175        1.1   thorpej 		tca = &cb->cb_action;
    176        1.1   thorpej 
    177        1.1   thorpej 	if (tca == NULL)
    178        1.1   thorpej 		tca = &top->tc_block.cb_action;
    179        1.1   thorpej 
    180        1.1   thorpej 	while (1) {
    181        1.1   thorpej 		PKTCNTR_ADD(&top->tc_cnts[tca->tca_code], pktinfo.pkt_len);
    182        1.9     perry 
    183        1.1   thorpej 		switch (tca->tca_code) {
    184        1.1   thorpej 		case TCACODE_PASS:
    185        1.1   thorpej 			return (1);
    186        1.1   thorpej 		case TCACODE_DROP:
    187        1.1   thorpej 			m_freem(m);
    188        1.1   thorpej 			return (0);
    189        1.1   thorpej 		case TCACODE_RETURN:
    190        1.1   thorpej 			return (0);
    191        1.1   thorpej 		case TCACODE_MARK:
    192        1.1   thorpej #ifdef INET6
    193        1.1   thorpej 			if (af == AF_INET6) {
    194        1.1   thorpej 				struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
    195        1.1   thorpej 				u_int32_t flowlabel;
    196        1.1   thorpej 
    197        1.1   thorpej 				flowlabel = ntohl(ip6->ip6_flow);
    198        1.1   thorpej 				flowlabel = (tca->tca_dscp << 20) |
    199        1.1   thorpej 					(flowlabel & ~(DSCP_MASK << 20));
    200        1.1   thorpej 				ip6->ip6_flow = htonl(flowlabel);
    201        1.1   thorpej 			} else
    202        1.1   thorpej #endif
    203        1.1   thorpej 				ip->ip_tos = tca->tca_dscp |
    204        1.1   thorpej 					(ip->ip_tos & DSCP_CUMASK);
    205        1.1   thorpej 			return (1);
    206        1.1   thorpej 		case TCACODE_NEXT:
    207        1.1   thorpej 			cb = tca->tca_next;
    208        1.1   thorpej 			tca = (*cb->cb_input)(cb, &pktinfo);
    209        1.1   thorpej 			break;
    210        1.1   thorpej 		case TCACODE_NONE:
    211        1.1   thorpej 		default:
    212        1.1   thorpej 			return (1);
    213        1.1   thorpej 		}
    214        1.1   thorpej 	}
    215        1.1   thorpej }
    216        1.1   thorpej 
    217        1.1   thorpej static struct top_cdnr *
    218        1.1   thorpej tcb_lookup(ifname)
    219        1.1   thorpej 	char *ifname;
    220        1.1   thorpej {
    221        1.1   thorpej 	struct top_cdnr *top;
    222        1.1   thorpej 	struct ifnet *ifp;
    223        1.1   thorpej 
    224        1.1   thorpej 	if ((ifp = ifunit(ifname)) != NULL)
    225        1.1   thorpej 		LIST_FOREACH(top, &tcb_list, tc_next)
    226        1.1   thorpej 			if (top->tc_ifq->altq_ifp == ifp)
    227        1.1   thorpej 				return (top);
    228        1.1   thorpej 	return (NULL);
    229        1.1   thorpej }
    230        1.1   thorpej 
    231        1.1   thorpej static struct cdnr_block *
    232        1.1   thorpej cdnr_handle2cb(handle)
    233        1.1   thorpej 	u_long handle;
    234        1.1   thorpej {
    235        1.1   thorpej 	struct cdnr_block *cb;
    236        1.1   thorpej 
    237        1.1   thorpej 	cb = (struct cdnr_block *)handle;
    238        1.1   thorpej 	if (handle != ALIGN(cb))
    239        1.1   thorpej 		return (NULL);
    240        1.9     perry 
    241        1.1   thorpej 	if (cb == NULL || cb->cb_handle != handle)
    242        1.1   thorpej 		return (NULL);
    243        1.1   thorpej 	return (cb);
    244        1.1   thorpej }
    245        1.1   thorpej 
    246        1.1   thorpej static u_long
    247        1.1   thorpej cdnr_cb2handle(cb)
    248        1.1   thorpej 	struct cdnr_block *cb;
    249        1.1   thorpej {
    250        1.1   thorpej 	return (cb->cb_handle);
    251        1.1   thorpej }
    252        1.1   thorpej 
    253        1.1   thorpej static void *
    254        1.1   thorpej cdnr_cballoc(top, type, input_func)
    255        1.1   thorpej 	struct top_cdnr *top;
    256        1.1   thorpej 	int type;
    257        1.1   thorpej 	struct tc_action *(*input_func)(struct cdnr_block *,
    258        1.1   thorpej 					struct cdnr_pktinfo *);
    259        1.1   thorpej {
    260        1.1   thorpej 	struct cdnr_block *cb;
    261        1.1   thorpej 	int size;
    262        1.1   thorpej 
    263        1.1   thorpej 	switch (type) {
    264        1.1   thorpej 	case TCETYPE_TOP:
    265        1.1   thorpej 		size = sizeof(struct top_cdnr);
    266        1.1   thorpej 		break;
    267        1.1   thorpej 	case TCETYPE_ELEMENT:
    268        1.1   thorpej 		size = sizeof(struct cdnr_block);
    269        1.1   thorpej 		break;
    270        1.1   thorpej 	case TCETYPE_TBMETER:
    271        1.1   thorpej 		size = sizeof(struct tbmeter);
    272        1.1   thorpej 		break;
    273        1.1   thorpej 	case TCETYPE_TRTCM:
    274        1.1   thorpej 		size = sizeof(struct trtcm);
    275        1.1   thorpej 		break;
    276        1.1   thorpej 	case TCETYPE_TSWTCM:
    277        1.1   thorpej 		size = sizeof(struct tswtcm);
    278        1.1   thorpej 		break;
    279        1.1   thorpej 	default:
    280        1.1   thorpej 		return (NULL);
    281        1.1   thorpej 	}
    282        1.1   thorpej 
    283        1.1   thorpej 	MALLOC(cb, struct cdnr_block *, size, M_DEVBUF, M_WAITOK);
    284        1.1   thorpej 	if (cb == NULL)
    285        1.1   thorpej 		return (NULL);
    286        1.7  christos 	(void)memset(cb, 0, size);
    287        1.9     perry 
    288        1.1   thorpej 	cb->cb_len = size;
    289        1.1   thorpej 	cb->cb_type = type;
    290        1.1   thorpej 	cb->cb_ref = 0;
    291        1.1   thorpej 	cb->cb_handle = (u_long)cb;
    292        1.1   thorpej 	if (top == NULL)
    293        1.1   thorpej 		cb->cb_top = (struct top_cdnr *)cb;
    294        1.1   thorpej 	else
    295        1.1   thorpej 		cb->cb_top = top;
    296        1.1   thorpej 
    297        1.1   thorpej 	if (input_func != NULL) {
    298        1.1   thorpej 		/*
    299        1.1   thorpej 		 * if this cdnr has an action function,
    300        1.1   thorpej 		 * make tc_action to call itself.
    301        1.1   thorpej 		 */
    302        1.1   thorpej 		cb->cb_action.tca_code = TCACODE_NEXT;
    303        1.1   thorpej 		cb->cb_action.tca_next = cb;
    304        1.1   thorpej 		cb->cb_input = input_func;
    305        1.1   thorpej 	} else
    306        1.1   thorpej 		cb->cb_action.tca_code = TCACODE_NONE;
    307        1.1   thorpej 
    308        1.1   thorpej 	/* if this isn't top, register the element to the top level cdnr */
    309        1.1   thorpej 	if (top != NULL)
    310        1.1   thorpej 		LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
    311        1.1   thorpej 
    312        1.1   thorpej 	return ((void *)cb);
    313        1.1   thorpej }
    314        1.1   thorpej 
    315        1.1   thorpej static void
    316        1.1   thorpej cdnr_cbdestroy(cblock)
    317        1.1   thorpej 	void *cblock;
    318        1.1   thorpej {
    319        1.1   thorpej 	struct cdnr_block *cb = cblock;
    320        1.1   thorpej 
    321        1.1   thorpej 	/* delete filters belonging to this cdnr */
    322        1.1   thorpej 	acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
    323        1.1   thorpej 
    324        1.1   thorpej 	/* remove from the top level cdnr */
    325        1.1   thorpej 	if (cb->cb_top != cblock)
    326        1.1   thorpej 		LIST_REMOVE(cb, cb_next);
    327        1.1   thorpej 
    328        1.1   thorpej 	FREE(cb, M_DEVBUF);
    329        1.1   thorpej }
    330        1.1   thorpej 
    331        1.1   thorpej /*
    332        1.1   thorpej  * conditioner common destroy routine
    333        1.1   thorpej  */
    334        1.1   thorpej static int
    335        1.1   thorpej generic_element_destroy(cb)
    336        1.1   thorpej 	struct cdnr_block *cb;
    337        1.1   thorpej {
    338        1.1   thorpej 	int error = 0;
    339        1.1   thorpej 
    340        1.1   thorpej 	switch (cb->cb_type) {
    341        1.1   thorpej 	case TCETYPE_TOP:
    342        1.1   thorpej 		error = top_destroy((struct top_cdnr *)cb);
    343        1.1   thorpej 		break;
    344        1.1   thorpej 	case TCETYPE_ELEMENT:
    345        1.1   thorpej 		error = element_destroy(cb);
    346        1.1   thorpej 		break;
    347        1.1   thorpej 	case TCETYPE_TBMETER:
    348        1.1   thorpej 		error = tbm_destroy((struct tbmeter *)cb);
    349        1.1   thorpej 		break;
    350        1.1   thorpej 	case TCETYPE_TRTCM:
    351        1.1   thorpej 		error = trtcm_destroy((struct trtcm *)cb);
    352        1.1   thorpej 		break;
    353        1.1   thorpej 	case TCETYPE_TSWTCM:
    354        1.1   thorpej 		error = tswtcm_destroy((struct tswtcm *)cb);
    355        1.1   thorpej 		break;
    356        1.1   thorpej 	default:
    357        1.1   thorpej 		error = EINVAL;
    358        1.1   thorpej 	}
    359        1.1   thorpej 	return (error);
    360        1.1   thorpej }
    361        1.1   thorpej 
    362        1.1   thorpej static int
    363        1.1   thorpej tca_verify_action(utca)
    364        1.1   thorpej 	struct tc_action *utca;
    365        1.1   thorpej {
    366        1.1   thorpej 	switch (utca->tca_code) {
    367        1.1   thorpej 	case TCACODE_PASS:
    368        1.1   thorpej 	case TCACODE_DROP:
    369        1.1   thorpej 	case TCACODE_MARK:
    370        1.1   thorpej 		/* these are ok */
    371        1.1   thorpej 		break;
    372        1.1   thorpej 
    373        1.1   thorpej 	case TCACODE_HANDLE:
    374        1.1   thorpej 		/* verify handle value */
    375        1.1   thorpej 		if (cdnr_handle2cb(utca->tca_handle) == NULL)
    376        1.1   thorpej 			return (-1);
    377        1.1   thorpej 		break;
    378        1.1   thorpej 
    379        1.1   thorpej 	case TCACODE_NONE:
    380        1.1   thorpej 	case TCACODE_RETURN:
    381        1.1   thorpej 	case TCACODE_NEXT:
    382        1.1   thorpej 	default:
    383        1.1   thorpej 		/* should not be passed from a user */
    384        1.1   thorpej 		return (-1);
    385        1.1   thorpej 	}
    386        1.1   thorpej 	return (0);
    387        1.1   thorpej }
    388        1.1   thorpej 
    389        1.1   thorpej static void
    390        1.1   thorpej tca_import_action(ktca, utca)
    391        1.1   thorpej 	struct tc_action *ktca, *utca;
    392        1.1   thorpej {
    393        1.1   thorpej 	struct cdnr_block *cb;
    394        1.1   thorpej 
    395        1.1   thorpej 	*ktca = *utca;
    396        1.1   thorpej 	if (ktca->tca_code == TCACODE_HANDLE) {
    397        1.1   thorpej 		cb = cdnr_handle2cb(ktca->tca_handle);
    398        1.1   thorpej 		if (cb == NULL) {
    399        1.1   thorpej 			ktca->tca_code = TCACODE_NONE;
    400        1.1   thorpej 			return;
    401        1.1   thorpej 		}
    402        1.1   thorpej 		ktca->tca_code = TCACODE_NEXT;
    403        1.1   thorpej 		ktca->tca_next = cb;
    404        1.1   thorpej 		cb->cb_ref++;
    405        1.1   thorpej 	} else if (ktca->tca_code == TCACODE_MARK) {
    406        1.1   thorpej 		ktca->tca_dscp &= DSCP_MASK;
    407        1.1   thorpej 	}
    408        1.1   thorpej 	return;
    409        1.1   thorpej }
    410        1.1   thorpej 
    411        1.1   thorpej static void
    412        1.1   thorpej tca_invalidate_action(tca)
    413        1.1   thorpej 	struct tc_action *tca;
    414        1.1   thorpej {
    415        1.1   thorpej 	struct cdnr_block *cb;
    416        1.1   thorpej 
    417        1.1   thorpej 	if (tca->tca_code == TCACODE_NEXT) {
    418        1.1   thorpej 		cb = tca->tca_next;
    419        1.1   thorpej 		if (cb == NULL)
    420        1.1   thorpej 			return;
    421        1.1   thorpej 		cb->cb_ref--;
    422        1.1   thorpej 	}
    423        1.1   thorpej 	tca->tca_code = TCACODE_NONE;
    424        1.1   thorpej }
    425        1.1   thorpej 
    426        1.1   thorpej /*
    427        1.1   thorpej  * top level traffic conditioner
    428        1.1   thorpej  */
    429        1.1   thorpej static struct top_cdnr *
    430        1.1   thorpej top_create(ifq)
    431        1.1   thorpej 	struct ifaltq *ifq;
    432        1.1   thorpej {
    433        1.1   thorpej 	struct top_cdnr *top;
    434        1.1   thorpej 
    435        1.1   thorpej 	if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
    436        1.1   thorpej 		return (NULL);
    437        1.1   thorpej 
    438        1.1   thorpej 	top->tc_ifq = ifq;
    439        1.1   thorpej 	/* set default action for the top level conditioner */
    440        1.1   thorpej 	top->tc_block.cb_action.tca_code = TCACODE_PASS;
    441        1.1   thorpej 
    442        1.1   thorpej 	LIST_INSERT_HEAD(&tcb_list, top, tc_next);
    443        1.1   thorpej 
    444        1.1   thorpej 	ifq->altq_cdnr = top;
    445        1.1   thorpej 
    446        1.1   thorpej 	return (top);
    447        1.1   thorpej }
    448        1.1   thorpej 
    449        1.1   thorpej static int
    450        1.1   thorpej top_destroy(top)
    451        1.1   thorpej 	struct top_cdnr *top;
    452        1.1   thorpej {
    453        1.1   thorpej 	struct cdnr_block *cb;
    454        1.1   thorpej 
    455        1.1   thorpej 	if (ALTQ_IS_CNDTNING(top->tc_ifq))
    456        1.1   thorpej 		ALTQ_CLEAR_CNDTNING(top->tc_ifq);
    457        1.1   thorpej 	top->tc_ifq->altq_cdnr = NULL;
    458        1.1   thorpej 
    459        1.1   thorpej 	/*
    460        1.1   thorpej 	 * destroy all the conditioner elements belonging to this interface
    461        1.1   thorpej 	 */
    462        1.1   thorpej 	while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
    463        1.1   thorpej 		while (cb != NULL && cb->cb_ref > 0)
    464        1.1   thorpej 			cb = LIST_NEXT(cb, cb_next);
    465        1.1   thorpej 		if (cb != NULL)
    466        1.1   thorpej 			generic_element_destroy(cb);
    467        1.1   thorpej 	}
    468        1.9     perry 
    469        1.1   thorpej 	LIST_REMOVE(top, tc_next);
    470        1.1   thorpej 
    471        1.1   thorpej 	cdnr_cbdestroy(top);
    472        1.1   thorpej 
    473        1.1   thorpej 	/* if there is no active conditioner, remove the input hook */
    474        1.1   thorpej 	if (altq_input != NULL) {
    475        1.1   thorpej 		LIST_FOREACH(top, &tcb_list, tc_next)
    476        1.1   thorpej 			if (ALTQ_IS_CNDTNING(top->tc_ifq))
    477        1.1   thorpej 				break;
    478        1.1   thorpej 		if (top == NULL)
    479        1.1   thorpej 			altq_input = NULL;
    480        1.1   thorpej 	}
    481        1.1   thorpej 
    482        1.1   thorpej 	return (0);
    483        1.1   thorpej }
    484        1.1   thorpej 
    485        1.1   thorpej /*
    486        1.1   thorpej  * simple tc elements without input function (e.g., dropper and makers).
    487        1.1   thorpej  */
    488        1.1   thorpej static struct cdnr_block *
    489        1.1   thorpej element_create(top, action)
    490        1.1   thorpej 	struct top_cdnr *top;
    491        1.1   thorpej 	struct tc_action *action;
    492        1.1   thorpej {
    493        1.1   thorpej 	struct cdnr_block *cb;
    494        1.1   thorpej 
    495        1.1   thorpej 	if (tca_verify_action(action) < 0)
    496        1.1   thorpej 		return (NULL);
    497        1.1   thorpej 
    498        1.1   thorpej 	if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
    499        1.1   thorpej 		return (NULL);
    500        1.1   thorpej 
    501        1.1   thorpej 	tca_import_action(&cb->cb_action, action);
    502        1.1   thorpej 
    503        1.1   thorpej 	return (cb);
    504        1.1   thorpej }
    505        1.1   thorpej 
    506        1.1   thorpej static int
    507        1.1   thorpej element_destroy(cb)
    508        1.1   thorpej 	struct cdnr_block *cb;
    509        1.1   thorpej {
    510        1.1   thorpej 	if (cb->cb_ref > 0)
    511        1.1   thorpej 		return (EBUSY);
    512        1.1   thorpej 
    513        1.1   thorpej 	tca_invalidate_action(&cb->cb_action);
    514        1.1   thorpej 
    515        1.1   thorpej 	cdnr_cbdestroy(cb);
    516        1.1   thorpej 	return (0);
    517        1.1   thorpej }
    518        1.1   thorpej 
    519        1.1   thorpej /*
    520        1.1   thorpej  * internal representation of token bucket parameters
    521        1.1   thorpej  *	rate: 	byte_per_unittime << 32
    522        1.1   thorpej  *		(((bits_per_sec) / 8) << 32) / machclk_freq
    523        1.1   thorpej  *	depth:	byte << 32
    524        1.1   thorpej  *
    525        1.1   thorpej  */
    526        1.1   thorpej #define	TB_SHIFT	32
    527        1.1   thorpej #define	TB_SCALE(x)	((u_int64_t)(x) << TB_SHIFT)
    528        1.1   thorpej #define	TB_UNSCALE(x)	((x) >> TB_SHIFT)
    529        1.1   thorpej 
    530        1.1   thorpej static void
    531        1.1   thorpej tb_import_profile(tb, profile)
    532        1.1   thorpej 	struct tbe *tb;
    533        1.1   thorpej 	struct tb_profile *profile;
    534        1.1   thorpej {
    535        1.1   thorpej 	tb->rate = TB_SCALE(profile->rate / 8) / machclk_freq;
    536        1.1   thorpej 	tb->depth = TB_SCALE(profile->depth);
    537        1.1   thorpej 	if (tb->rate > 0)
    538        1.1   thorpej 		tb->filluptime = tb->depth / tb->rate;
    539        1.1   thorpej 	else
    540        1.1   thorpej 		tb->filluptime = 0xffffffffffffffffLL;
    541        1.1   thorpej 	tb->token = tb->depth;
    542        1.1   thorpej 	tb->last = read_machclk();
    543        1.1   thorpej }
    544        1.1   thorpej 
    545        1.1   thorpej /*
    546        1.1   thorpej  * simple token bucket meter
    547        1.1   thorpej  */
    548        1.1   thorpej static struct tbmeter *
    549        1.1   thorpej tbm_create(top, profile, in_action, out_action)
    550        1.1   thorpej 	struct top_cdnr *top;
    551        1.1   thorpej 	struct tb_profile *profile;
    552        1.1   thorpej 	struct tc_action *in_action, *out_action;
    553        1.1   thorpej {
    554        1.1   thorpej 	struct tbmeter *tbm = NULL;
    555        1.1   thorpej 
    556        1.1   thorpej 	if (tca_verify_action(in_action) < 0
    557        1.1   thorpej 	    || tca_verify_action(out_action) < 0)
    558        1.1   thorpej 		return (NULL);
    559        1.1   thorpej 
    560        1.1   thorpej 	if ((tbm = cdnr_cballoc(top, TCETYPE_TBMETER,
    561        1.1   thorpej 				tbm_input)) == NULL)
    562        1.1   thorpej 		return (NULL);
    563        1.1   thorpej 
    564        1.1   thorpej 	tb_import_profile(&tbm->tb, profile);
    565        1.1   thorpej 
    566        1.1   thorpej 	tca_import_action(&tbm->in_action, in_action);
    567        1.1   thorpej 	tca_import_action(&tbm->out_action, out_action);
    568        1.1   thorpej 
    569        1.1   thorpej 	return (tbm);
    570        1.1   thorpej }
    571        1.1   thorpej 
    572        1.1   thorpej static int
    573        1.1   thorpej tbm_destroy(tbm)
    574        1.1   thorpej 	struct tbmeter *tbm;
    575        1.1   thorpej {
    576        1.1   thorpej 	if (tbm->cdnrblk.cb_ref > 0)
    577        1.1   thorpej 		return (EBUSY);
    578        1.1   thorpej 
    579        1.1   thorpej 	tca_invalidate_action(&tbm->in_action);
    580        1.1   thorpej 	tca_invalidate_action(&tbm->out_action);
    581        1.1   thorpej 
    582        1.1   thorpej 	cdnr_cbdestroy(tbm);
    583        1.1   thorpej 	return (0);
    584        1.1   thorpej }
    585        1.9     perry 
    586        1.1   thorpej static struct tc_action *
    587        1.1   thorpej tbm_input(cb, pktinfo)
    588        1.1   thorpej 	struct cdnr_block *cb;
    589        1.1   thorpej 	struct cdnr_pktinfo *pktinfo;
    590        1.1   thorpej {
    591        1.1   thorpej 	struct tbmeter *tbm = (struct tbmeter *)cb;
    592        1.1   thorpej 	u_int64_t	len;
    593        1.1   thorpej 	u_int64_t	interval, now;
    594        1.9     perry 
    595        1.1   thorpej 	len = TB_SCALE(pktinfo->pkt_len);
    596        1.1   thorpej 
    597        1.1   thorpej 	if (tbm->tb.token < len) {
    598        1.1   thorpej 		now = read_machclk();
    599        1.1   thorpej 		interval = now - tbm->tb.last;
    600        1.1   thorpej 		if (interval >= tbm->tb.filluptime)
    601        1.1   thorpej 			tbm->tb.token = tbm->tb.depth;
    602        1.1   thorpej 		else {
    603        1.1   thorpej 			tbm->tb.token += interval * tbm->tb.rate;
    604        1.1   thorpej 			if (tbm->tb.token > tbm->tb.depth)
    605        1.1   thorpej 				tbm->tb.token = tbm->tb.depth;
    606        1.1   thorpej 		}
    607        1.1   thorpej 		tbm->tb.last = now;
    608        1.1   thorpej 	}
    609        1.9     perry 
    610        1.1   thorpej 	if (tbm->tb.token < len) {
    611        1.1   thorpej 		PKTCNTR_ADD(&tbm->out_cnt, pktinfo->pkt_len);
    612        1.1   thorpej 		return (&tbm->out_action);
    613        1.1   thorpej 	}
    614        1.1   thorpej 
    615        1.1   thorpej 	tbm->tb.token -= len;
    616        1.1   thorpej 	PKTCNTR_ADD(&tbm->in_cnt, pktinfo->pkt_len);
    617        1.1   thorpej 	return (&tbm->in_action);
    618        1.1   thorpej }
    619        1.1   thorpej 
    620        1.1   thorpej /*
    621        1.1   thorpej  * two rate three color marker
    622        1.1   thorpej  * as described in draft-heinanen-diffserv-trtcm-01.txt
    623        1.1   thorpej  */
    624        1.1   thorpej static struct trtcm *
    625        1.1   thorpej trtcm_create(top, cmtd_profile, peak_profile,
    626        1.1   thorpej 	     green_action, yellow_action, red_action, coloraware)
    627        1.1   thorpej 	struct top_cdnr *top;
    628        1.1   thorpej 	struct tb_profile *cmtd_profile, *peak_profile;
    629        1.1   thorpej 	struct tc_action *green_action, *yellow_action, *red_action;
    630        1.1   thorpej 	int	coloraware;
    631        1.1   thorpej {
    632        1.1   thorpej 	struct trtcm *tcm = NULL;
    633        1.1   thorpej 
    634        1.1   thorpej 	if (tca_verify_action(green_action) < 0
    635        1.1   thorpej 	    || tca_verify_action(yellow_action) < 0
    636        1.1   thorpej 	    || tca_verify_action(red_action) < 0)
    637        1.1   thorpej 		return (NULL);
    638        1.1   thorpej 
    639        1.1   thorpej 	if ((tcm = cdnr_cballoc(top, TCETYPE_TRTCM,
    640        1.1   thorpej 				trtcm_input)) == NULL)
    641        1.1   thorpej 		return (NULL);
    642        1.1   thorpej 
    643        1.1   thorpej 	tb_import_profile(&tcm->cmtd_tb, cmtd_profile);
    644        1.1   thorpej 	tb_import_profile(&tcm->peak_tb, peak_profile);
    645        1.1   thorpej 
    646        1.1   thorpej 	tca_import_action(&tcm->green_action, green_action);
    647        1.1   thorpej 	tca_import_action(&tcm->yellow_action, yellow_action);
    648        1.1   thorpej 	tca_import_action(&tcm->red_action, red_action);
    649        1.1   thorpej 
    650        1.1   thorpej 	/* set dscps to use */
    651        1.1   thorpej 	if (tcm->green_action.tca_code == TCACODE_MARK)
    652        1.1   thorpej 		tcm->green_dscp = tcm->green_action.tca_dscp & DSCP_MASK;
    653        1.1   thorpej 	else
    654        1.1   thorpej 		tcm->green_dscp = DSCP_AF11;
    655        1.1   thorpej 	if (tcm->yellow_action.tca_code == TCACODE_MARK)
    656        1.1   thorpej 		tcm->yellow_dscp = tcm->yellow_action.tca_dscp & DSCP_MASK;
    657        1.1   thorpej 	else
    658        1.1   thorpej 		tcm->yellow_dscp = DSCP_AF12;
    659        1.1   thorpej 	if (tcm->red_action.tca_code == TCACODE_MARK)
    660        1.1   thorpej 		tcm->red_dscp = tcm->red_action.tca_dscp & DSCP_MASK;
    661        1.1   thorpej 	else
    662        1.1   thorpej 		tcm->red_dscp = DSCP_AF13;
    663        1.1   thorpej 
    664        1.1   thorpej 	tcm->coloraware = coloraware;
    665        1.1   thorpej 
    666        1.1   thorpej 	return (tcm);
    667        1.1   thorpej }
    668        1.1   thorpej 
    669        1.1   thorpej static int
    670        1.1   thorpej trtcm_destroy(tcm)
    671        1.1   thorpej 	struct trtcm *tcm;
    672        1.1   thorpej {
    673        1.1   thorpej 	if (tcm->cdnrblk.cb_ref > 0)
    674        1.1   thorpej 		return (EBUSY);
    675        1.1   thorpej 
    676        1.1   thorpej 	tca_invalidate_action(&tcm->green_action);
    677        1.1   thorpej 	tca_invalidate_action(&tcm->yellow_action);
    678        1.1   thorpej 	tca_invalidate_action(&tcm->red_action);
    679        1.1   thorpej 
    680        1.1   thorpej 	cdnr_cbdestroy(tcm);
    681        1.1   thorpej 	return (0);
    682        1.1   thorpej }
    683        1.9     perry 
    684        1.1   thorpej static struct tc_action *
    685        1.1   thorpej trtcm_input(cb, pktinfo)
    686        1.1   thorpej 	struct cdnr_block *cb;
    687        1.1   thorpej 	struct cdnr_pktinfo *pktinfo;
    688        1.1   thorpej {
    689        1.1   thorpej 	struct trtcm *tcm = (struct trtcm *)cb;
    690        1.1   thorpej 	u_int64_t	len;
    691        1.1   thorpej 	u_int64_t	interval, now;
    692        1.1   thorpej 	u_int8_t	color;
    693        1.1   thorpej 
    694        1.1   thorpej 	len = TB_SCALE(pktinfo->pkt_len);
    695        1.1   thorpej 	if (tcm->coloraware) {
    696        1.1   thorpej 		color = pktinfo->pkt_dscp;
    697        1.1   thorpej 		if (color != tcm->yellow_dscp && color != tcm->red_dscp)
    698        1.1   thorpej 			color = tcm->green_dscp;
    699        1.1   thorpej 	} else {
    700        1.1   thorpej 		/* if color-blind, precolor it as green */
    701        1.1   thorpej 		color = tcm->green_dscp;
    702        1.1   thorpej 	}
    703        1.1   thorpej 
    704        1.1   thorpej 	now = read_machclk();
    705        1.1   thorpej 	if (tcm->cmtd_tb.token < len) {
    706        1.1   thorpej 		interval = now - tcm->cmtd_tb.last;
    707        1.1   thorpej 		if (interval >= tcm->cmtd_tb.filluptime)
    708        1.1   thorpej 			tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
    709        1.1   thorpej 		else {
    710        1.1   thorpej 			tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
    711        1.1   thorpej 			if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
    712        1.1   thorpej 				tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
    713        1.1   thorpej 		}
    714        1.1   thorpej 		tcm->cmtd_tb.last = now;
    715        1.1   thorpej 	}
    716        1.1   thorpej 	if (tcm->peak_tb.token < len) {
    717        1.1   thorpej 		interval = now - tcm->peak_tb.last;
    718        1.1   thorpej 		if (interval >= tcm->peak_tb.filluptime)
    719        1.1   thorpej 			tcm->peak_tb.token = tcm->peak_tb.depth;
    720        1.1   thorpej 		else {
    721        1.1   thorpej 			tcm->peak_tb.token += interval * tcm->peak_tb.rate;
    722        1.1   thorpej 			if (tcm->peak_tb.token > tcm->peak_tb.depth)
    723        1.1   thorpej 				tcm->peak_tb.token = tcm->peak_tb.depth;
    724        1.1   thorpej 		}
    725        1.1   thorpej 		tcm->peak_tb.last = now;
    726        1.1   thorpej 	}
    727        1.9     perry 
    728        1.1   thorpej 	if (color == tcm->red_dscp || tcm->peak_tb.token < len) {
    729        1.1   thorpej 		pktinfo->pkt_dscp = tcm->red_dscp;
    730        1.1   thorpej 		PKTCNTR_ADD(&tcm->red_cnt, pktinfo->pkt_len);
    731        1.1   thorpej 		return (&tcm->red_action);
    732        1.1   thorpej 	}
    733        1.1   thorpej 
    734        1.1   thorpej 	if (color == tcm->yellow_dscp || tcm->cmtd_tb.token < len) {
    735        1.1   thorpej 		pktinfo->pkt_dscp = tcm->yellow_dscp;
    736        1.1   thorpej 		tcm->peak_tb.token -= len;
    737        1.1   thorpej 		PKTCNTR_ADD(&tcm->yellow_cnt, pktinfo->pkt_len);
    738        1.1   thorpej 		return (&tcm->yellow_action);
    739        1.1   thorpej 	}
    740        1.1   thorpej 
    741        1.1   thorpej 	pktinfo->pkt_dscp = tcm->green_dscp;
    742        1.1   thorpej 	tcm->cmtd_tb.token -= len;
    743        1.1   thorpej 	tcm->peak_tb.token -= len;
    744        1.1   thorpej 	PKTCNTR_ADD(&tcm->green_cnt, pktinfo->pkt_len);
    745        1.1   thorpej 	return (&tcm->green_action);
    746        1.1   thorpej }
    747        1.1   thorpej 
    748        1.1   thorpej /*
    749        1.1   thorpej  * time sliding window three color marker
    750        1.1   thorpej  * as described in draft-fang-diffserv-tc-tswtcm-00.txt
    751        1.1   thorpej  */
    752        1.1   thorpej static struct tswtcm *
    753        1.1   thorpej tswtcm_create(top, cmtd_rate, peak_rate, avg_interval,
    754        1.1   thorpej 	      green_action, yellow_action, red_action)
    755        1.1   thorpej 	struct top_cdnr *top;
    756        1.1   thorpej 	u_int32_t	cmtd_rate, peak_rate, avg_interval;
    757        1.1   thorpej 	struct tc_action *green_action, *yellow_action, *red_action;
    758        1.1   thorpej {
    759        1.1   thorpej 	struct tswtcm *tsw;
    760        1.1   thorpej 
    761        1.1   thorpej 	if (tca_verify_action(green_action) < 0
    762        1.1   thorpej 	    || tca_verify_action(yellow_action) < 0
    763        1.1   thorpej 	    || tca_verify_action(red_action) < 0)
    764        1.1   thorpej 		return (NULL);
    765        1.1   thorpej 
    766        1.1   thorpej 	if ((tsw = cdnr_cballoc(top, TCETYPE_TSWTCM,
    767        1.1   thorpej 				tswtcm_input)) == NULL)
    768        1.1   thorpej 		return (NULL);
    769        1.1   thorpej 
    770        1.1   thorpej 	tca_import_action(&tsw->green_action, green_action);
    771        1.1   thorpej 	tca_import_action(&tsw->yellow_action, yellow_action);
    772        1.1   thorpej 	tca_import_action(&tsw->red_action, red_action);
    773        1.1   thorpej 
    774        1.1   thorpej 	/* set dscps to use */
    775        1.1   thorpej 	if (tsw->green_action.tca_code == TCACODE_MARK)
    776        1.1   thorpej 		tsw->green_dscp = tsw->green_action.tca_dscp & DSCP_MASK;
    777        1.1   thorpej 	else
    778        1.1   thorpej 		tsw->green_dscp = DSCP_AF11;
    779        1.1   thorpej 	if (tsw->yellow_action.tca_code == TCACODE_MARK)
    780        1.1   thorpej 		tsw->yellow_dscp = tsw->yellow_action.tca_dscp & DSCP_MASK;
    781        1.1   thorpej 	else
    782        1.1   thorpej 		tsw->yellow_dscp = DSCP_AF12;
    783        1.1   thorpej 	if (tsw->red_action.tca_code == TCACODE_MARK)
    784        1.1   thorpej 		tsw->red_dscp = tsw->red_action.tca_dscp & DSCP_MASK;
    785        1.1   thorpej 	else
    786        1.1   thorpej 		tsw->red_dscp = DSCP_AF13;
    787        1.1   thorpej 
    788        1.1   thorpej 	/* convert rates from bits/sec to bytes/sec */
    789        1.1   thorpej 	tsw->cmtd_rate = cmtd_rate / 8;
    790        1.1   thorpej 	tsw->peak_rate = peak_rate / 8;
    791        1.1   thorpej 	tsw->avg_rate = 0;
    792        1.1   thorpej 
    793        1.1   thorpej 	/* timewin is converted from msec to machine clock unit */
    794        1.1   thorpej 	tsw->timewin = (u_int64_t)machclk_freq * avg_interval / 1000;
    795        1.1   thorpej 
    796        1.1   thorpej 	return (tsw);
    797        1.1   thorpej }
    798        1.1   thorpej 
    799        1.1   thorpej static int
    800        1.1   thorpej tswtcm_destroy(tsw)
    801        1.1   thorpej 	struct tswtcm *tsw;
    802        1.1   thorpej {
    803        1.1   thorpej 	if (tsw->cdnrblk.cb_ref > 0)
    804        1.1   thorpej 		return (EBUSY);
    805        1.1   thorpej 
    806        1.1   thorpej 	tca_invalidate_action(&tsw->green_action);
    807        1.1   thorpej 	tca_invalidate_action(&tsw->yellow_action);
    808        1.1   thorpej 	tca_invalidate_action(&tsw->red_action);
    809        1.1   thorpej 
    810        1.1   thorpej 	cdnr_cbdestroy(tsw);
    811        1.1   thorpej 	return (0);
    812        1.1   thorpej }
    813        1.1   thorpej 
    814        1.1   thorpej static struct tc_action *
    815        1.1   thorpej tswtcm_input(cb, pktinfo)
    816        1.1   thorpej 	struct cdnr_block *cb;
    817        1.1   thorpej 	struct cdnr_pktinfo *pktinfo;
    818        1.1   thorpej {
    819        1.1   thorpej 	struct tswtcm	*tsw = (struct tswtcm *)cb;
    820        1.1   thorpej 	int		len;
    821        1.1   thorpej 	u_int32_t	avg_rate;
    822        1.1   thorpej 	u_int64_t	interval, now, tmp;
    823        1.1   thorpej 
    824        1.1   thorpej 	/*
    825        1.1   thorpej 	 * rate estimator
    826        1.1   thorpej 	 */
    827        1.1   thorpej 	len = pktinfo->pkt_len;
    828        1.1   thorpej 	now = read_machclk();
    829        1.1   thorpej 
    830        1.1   thorpej 	interval = now - tsw->t_front;
    831        1.1   thorpej 	/*
    832        1.1   thorpej 	 * calculate average rate:
    833        1.1   thorpej 	 *	avg = (avg * timewin + pkt_len)/(timewin + interval)
    834        1.1   thorpej 	 * pkt_len needs to be multiplied by machclk_freq in order to
    835        1.1   thorpej 	 * get (bytes/sec).
    836        1.1   thorpej 	 * note: when avg_rate (bytes/sec) and timewin (machclk unit) are
    837        1.1   thorpej 	 * less than 32 bits, the following 64-bit operation has enough
    838        1.1   thorpej 	 * precision.
    839        1.1   thorpej 	 */
    840        1.1   thorpej 	tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
    841        1.1   thorpej 	       + (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
    842        1.1   thorpej 	tsw->avg_rate = avg_rate = (u_int32_t)tmp;
    843        1.1   thorpej 	tsw->t_front = now;
    844        1.1   thorpej 
    845        1.1   thorpej 	/*
    846        1.1   thorpej 	 * marker
    847        1.1   thorpej 	 */
    848        1.1   thorpej 	if (avg_rate > tsw->cmtd_rate) {
    849        1.6   mycroft 		u_int32_t randval = random() % avg_rate;
    850        1.9     perry 
    851        1.1   thorpej 		if (avg_rate > tsw->peak_rate) {
    852        1.1   thorpej 			if (randval < avg_rate - tsw->peak_rate) {
    853        1.1   thorpej 				/* mark red */
    854        1.1   thorpej 				pktinfo->pkt_dscp = tsw->red_dscp;
    855        1.1   thorpej 				PKTCNTR_ADD(&tsw->red_cnt, len);
    856        1.1   thorpej 				return (&tsw->red_action);
    857        1.1   thorpej 			} else if (randval < avg_rate - tsw->cmtd_rate)
    858        1.1   thorpej 				goto mark_yellow;
    859        1.1   thorpej 		} else {
    860        1.1   thorpej 			/* peak_rate >= avg_rate > cmtd_rate */
    861        1.1   thorpej 			if (randval < avg_rate - tsw->cmtd_rate) {
    862        1.1   thorpej 			mark_yellow:
    863        1.1   thorpej 				pktinfo->pkt_dscp = tsw->yellow_dscp;
    864        1.1   thorpej 				PKTCNTR_ADD(&tsw->yellow_cnt, len);
    865        1.1   thorpej 				return (&tsw->yellow_action);
    866        1.1   thorpej 			}
    867        1.1   thorpej 		}
    868        1.1   thorpej 	}
    869        1.1   thorpej 
    870        1.1   thorpej 	/* mark green */
    871        1.1   thorpej 	pktinfo->pkt_dscp = tsw->green_dscp;
    872        1.1   thorpej 	PKTCNTR_ADD(&tsw->green_cnt, len);
    873        1.1   thorpej 	return (&tsw->green_action);
    874        1.1   thorpej }
    875        1.1   thorpej 
    876        1.1   thorpej /*
    877        1.1   thorpej  * ioctl requests
    878        1.1   thorpej  */
    879        1.1   thorpej static int
    880        1.1   thorpej cdnrcmd_if_attach(ifname)
    881        1.1   thorpej 	char *ifname;
    882        1.1   thorpej {
    883        1.1   thorpej 	struct ifnet *ifp;
    884        1.1   thorpej 	struct top_cdnr *top;
    885        1.1   thorpej 
    886        1.1   thorpej 	if ((ifp = ifunit(ifname)) == NULL)
    887        1.1   thorpej 		return (EBADF);
    888        1.1   thorpej 
    889        1.1   thorpej 	if (ifp->if_snd.altq_cdnr != NULL)
    890        1.1   thorpej 		return (EBUSY);
    891        1.1   thorpej 
    892        1.1   thorpej 	if ((top = top_create(&ifp->if_snd)) == NULL)
    893        1.1   thorpej 		return (ENOMEM);
    894        1.1   thorpej 	return (0);
    895        1.1   thorpej }
    896        1.1   thorpej 
    897        1.1   thorpej static int
    898        1.1   thorpej cdnrcmd_if_detach(ifname)
    899        1.1   thorpej 	char *ifname;
    900        1.1   thorpej {
    901        1.1   thorpej 	struct top_cdnr *top;
    902        1.1   thorpej 
    903        1.1   thorpej 	if ((top = tcb_lookup(ifname)) == NULL)
    904        1.1   thorpej 		return (EBADF);
    905        1.1   thorpej 
    906        1.1   thorpej 	return top_destroy(top);
    907        1.1   thorpej }
    908        1.1   thorpej 
    909        1.1   thorpej static int
    910        1.1   thorpej cdnrcmd_add_element(ap)
    911        1.1   thorpej 	struct cdnr_add_element *ap;
    912        1.1   thorpej {
    913        1.1   thorpej 	struct top_cdnr *top;
    914        1.1   thorpej 	struct cdnr_block *cb;
    915        1.1   thorpej 
    916        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
    917        1.1   thorpej 		return (EBADF);
    918        1.1   thorpej 
    919        1.1   thorpej 	cb = element_create(top, &ap->action);
    920        1.1   thorpej 	if (cb == NULL)
    921        1.1   thorpej 		return (EINVAL);
    922        1.1   thorpej 	/* return a class handle to the user */
    923        1.1   thorpej 	ap->cdnr_handle = cdnr_cb2handle(cb);
    924        1.1   thorpej 	return (0);
    925        1.1   thorpej }
    926        1.1   thorpej 
    927        1.1   thorpej static int
    928        1.1   thorpej cdnrcmd_delete_element(ap)
    929        1.1   thorpej 	struct cdnr_delete_element *ap;
    930        1.1   thorpej {
    931        1.1   thorpej 	struct top_cdnr *top;
    932        1.1   thorpej 	struct cdnr_block *cb;
    933        1.1   thorpej 
    934        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
    935        1.1   thorpej 		return (EBADF);
    936        1.1   thorpej 
    937        1.1   thorpej 	if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
    938        1.1   thorpej 		return (EINVAL);
    939        1.1   thorpej 
    940        1.1   thorpej 	if (cb->cb_type != TCETYPE_ELEMENT)
    941        1.1   thorpej 		return generic_element_destroy(cb);
    942        1.1   thorpej 
    943        1.1   thorpej 	return element_destroy(cb);
    944        1.1   thorpej }
    945        1.1   thorpej 
    946        1.1   thorpej static int
    947        1.1   thorpej cdnrcmd_add_filter(ap)
    948        1.1   thorpej 	struct cdnr_add_filter *ap;
    949        1.1   thorpej {
    950        1.1   thorpej 	struct top_cdnr *top;
    951        1.1   thorpej 	struct cdnr_block *cb;
    952        1.1   thorpej 
    953        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
    954        1.1   thorpej 		return (EBADF);
    955        1.1   thorpej 
    956        1.1   thorpej 	if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
    957        1.1   thorpej 		return (EINVAL);
    958        1.1   thorpej 
    959        1.1   thorpej 	return acc_add_filter(&top->tc_classifier, &ap->filter,
    960        1.1   thorpej 			      cb, &ap->filter_handle);
    961        1.1   thorpej }
    962        1.1   thorpej 
    963        1.1   thorpej static int
    964        1.1   thorpej cdnrcmd_delete_filter(ap)
    965        1.1   thorpej 	struct cdnr_delete_filter *ap;
    966        1.1   thorpej {
    967        1.1   thorpej 	struct top_cdnr *top;
    968        1.1   thorpej 
    969        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
    970        1.1   thorpej 		return (EBADF);
    971        1.1   thorpej 
    972        1.1   thorpej 	return acc_delete_filter(&top->tc_classifier, ap->filter_handle);
    973        1.1   thorpej }
    974        1.1   thorpej 
    975        1.1   thorpej static int
    976        1.1   thorpej cdnrcmd_add_tbm(ap)
    977        1.1   thorpej 	struct cdnr_add_tbmeter *ap;
    978        1.1   thorpej {
    979        1.1   thorpej 	struct top_cdnr *top;
    980        1.1   thorpej 	struct tbmeter *tbm;
    981        1.1   thorpej 
    982        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
    983        1.1   thorpej 		return (EBADF);
    984        1.1   thorpej 
    985        1.1   thorpej 	tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
    986        1.1   thorpej 	if (tbm == NULL)
    987        1.1   thorpej 		return (EINVAL);
    988        1.1   thorpej 	/* return a class handle to the user */
    989        1.1   thorpej 	ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
    990        1.1   thorpej 	return (0);
    991        1.1   thorpej }
    992        1.1   thorpej 
    993        1.1   thorpej static int
    994        1.1   thorpej cdnrcmd_modify_tbm(ap)
    995        1.1   thorpej 	struct cdnr_modify_tbmeter *ap;
    996        1.1   thorpej {
    997        1.1   thorpej 	struct tbmeter *tbm;
    998        1.1   thorpej 
    999        1.1   thorpej 	if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
   1000        1.1   thorpej 		return (EINVAL);
   1001        1.1   thorpej 
   1002        1.1   thorpej 	tb_import_profile(&tbm->tb, &ap->profile);
   1003        1.1   thorpej 
   1004        1.1   thorpej 	return (0);
   1005        1.1   thorpej }
   1006        1.1   thorpej 
   1007        1.1   thorpej static int
   1008        1.1   thorpej cdnrcmd_tbm_stats(ap)
   1009        1.1   thorpej 	struct cdnr_tbmeter_stats *ap;
   1010        1.1   thorpej {
   1011        1.1   thorpej 	struct tbmeter *tbm;
   1012        1.1   thorpej 
   1013        1.1   thorpej 	if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
   1014        1.1   thorpej 		return (EINVAL);
   1015        1.1   thorpej 
   1016        1.1   thorpej 	ap->in_cnt = tbm->in_cnt;
   1017        1.1   thorpej 	ap->out_cnt = tbm->out_cnt;
   1018        1.1   thorpej 
   1019        1.1   thorpej 	return (0);
   1020        1.1   thorpej }
   1021        1.1   thorpej 
   1022        1.1   thorpej static int
   1023        1.1   thorpej cdnrcmd_add_trtcm(ap)
   1024        1.1   thorpej 	struct cdnr_add_trtcm *ap;
   1025        1.1   thorpej {
   1026        1.1   thorpej 	struct top_cdnr *top;
   1027        1.1   thorpej 	struct trtcm *tcm;
   1028        1.1   thorpej 
   1029        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
   1030        1.1   thorpej 		return (EBADF);
   1031        1.1   thorpej 
   1032        1.1   thorpej 	tcm = trtcm_create(top, &ap->cmtd_profile, &ap->peak_profile,
   1033        1.1   thorpej 			   &ap->green_action, &ap->yellow_action,
   1034        1.1   thorpej 			   &ap->red_action, ap->coloraware);
   1035        1.1   thorpej 	if (tcm == NULL)
   1036        1.1   thorpej 		return (EINVAL);
   1037        1.1   thorpej 
   1038        1.1   thorpej 	/* return a class handle to the user */
   1039        1.1   thorpej 	ap->cdnr_handle = cdnr_cb2handle(&tcm->cdnrblk);
   1040        1.1   thorpej 	return (0);
   1041        1.1   thorpej }
   1042        1.1   thorpej 
   1043        1.1   thorpej static int
   1044        1.1   thorpej cdnrcmd_modify_trtcm(ap)
   1045        1.1   thorpej 	struct cdnr_modify_trtcm *ap;
   1046        1.1   thorpej {
   1047        1.1   thorpej 	struct trtcm *tcm;
   1048        1.1   thorpej 
   1049        1.1   thorpej 	if ((tcm = (struct trtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
   1050        1.1   thorpej 		return (EINVAL);
   1051        1.1   thorpej 
   1052        1.1   thorpej 	tb_import_profile(&tcm->cmtd_tb, &ap->cmtd_profile);
   1053        1.1   thorpej 	tb_import_profile(&tcm->peak_tb, &ap->peak_profile);
   1054        1.1   thorpej 
   1055        1.1   thorpej 	return (0);
   1056        1.1   thorpej }
   1057        1.1   thorpej 
   1058        1.1   thorpej static int
   1059        1.1   thorpej cdnrcmd_tcm_stats(ap)
   1060        1.1   thorpej 	struct cdnr_tcm_stats *ap;
   1061        1.1   thorpej {
   1062        1.1   thorpej 	struct cdnr_block *cb;
   1063        1.1   thorpej 
   1064        1.1   thorpej 	if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
   1065        1.1   thorpej 		return (EINVAL);
   1066        1.1   thorpej 
   1067        1.1   thorpej 	if (cb->cb_type == TCETYPE_TRTCM) {
   1068        1.1   thorpej 	    struct trtcm *tcm = (struct trtcm *)cb;
   1069        1.9     perry 
   1070        1.1   thorpej 	    ap->green_cnt = tcm->green_cnt;
   1071        1.1   thorpej 	    ap->yellow_cnt = tcm->yellow_cnt;
   1072        1.1   thorpej 	    ap->red_cnt = tcm->red_cnt;
   1073        1.1   thorpej 	} else if (cb->cb_type == TCETYPE_TSWTCM) {
   1074        1.1   thorpej 	    struct tswtcm *tsw = (struct tswtcm *)cb;
   1075        1.9     perry 
   1076        1.1   thorpej 	    ap->green_cnt = tsw->green_cnt;
   1077        1.1   thorpej 	    ap->yellow_cnt = tsw->yellow_cnt;
   1078        1.1   thorpej 	    ap->red_cnt = tsw->red_cnt;
   1079        1.1   thorpej 	} else
   1080        1.1   thorpej 	    return (EINVAL);
   1081        1.1   thorpej 
   1082        1.1   thorpej 	return (0);
   1083        1.1   thorpej }
   1084        1.1   thorpej 
   1085        1.1   thorpej static int
   1086        1.1   thorpej cdnrcmd_add_tswtcm(ap)
   1087        1.1   thorpej 	struct cdnr_add_tswtcm *ap;
   1088        1.1   thorpej {
   1089        1.1   thorpej 	struct top_cdnr *top;
   1090        1.1   thorpej 	struct tswtcm *tsw;
   1091        1.1   thorpej 
   1092        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
   1093        1.1   thorpej 		return (EBADF);
   1094        1.1   thorpej 
   1095        1.1   thorpej 	if (ap->cmtd_rate > ap->peak_rate)
   1096        1.1   thorpej 		return (EINVAL);
   1097        1.1   thorpej 
   1098        1.1   thorpej 	tsw = tswtcm_create(top, ap->cmtd_rate, ap->peak_rate,
   1099        1.1   thorpej 			    ap->avg_interval, &ap->green_action,
   1100        1.1   thorpej 			    &ap->yellow_action, &ap->red_action);
   1101        1.1   thorpej 	if (tsw == NULL)
   1102        1.1   thorpej 	    return (EINVAL);
   1103        1.1   thorpej 
   1104        1.1   thorpej 	/* return a class handle to the user */
   1105        1.1   thorpej 	ap->cdnr_handle = cdnr_cb2handle(&tsw->cdnrblk);
   1106        1.1   thorpej 	return (0);
   1107        1.1   thorpej }
   1108        1.1   thorpej 
   1109        1.1   thorpej static int
   1110        1.1   thorpej cdnrcmd_modify_tswtcm(ap)
   1111        1.1   thorpej 	struct cdnr_modify_tswtcm *ap;
   1112        1.1   thorpej {
   1113        1.1   thorpej 	struct tswtcm *tsw;
   1114        1.1   thorpej 
   1115        1.1   thorpej 	if ((tsw = (struct tswtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
   1116        1.1   thorpej 		return (EINVAL);
   1117        1.1   thorpej 
   1118        1.1   thorpej 	if (ap->cmtd_rate > ap->peak_rate)
   1119        1.1   thorpej 		return (EINVAL);
   1120        1.1   thorpej 
   1121        1.1   thorpej 	/* convert rates from bits/sec to bytes/sec */
   1122        1.1   thorpej 	tsw->cmtd_rate = ap->cmtd_rate / 8;
   1123        1.1   thorpej 	tsw->peak_rate = ap->peak_rate / 8;
   1124        1.1   thorpej 	tsw->avg_rate = 0;
   1125        1.1   thorpej 
   1126        1.1   thorpej 	/* timewin is converted from msec to machine clock unit */
   1127        1.1   thorpej 	tsw->timewin = (u_int64_t)machclk_freq * ap->avg_interval / 1000;
   1128        1.1   thorpej 
   1129        1.1   thorpej 	return (0);
   1130        1.1   thorpej }
   1131        1.1   thorpej 
   1132        1.1   thorpej static int
   1133        1.1   thorpej cdnrcmd_get_stats(ap)
   1134        1.1   thorpej 	struct cdnr_get_stats *ap;
   1135        1.1   thorpej {
   1136        1.1   thorpej 	struct top_cdnr *top;
   1137        1.1   thorpej 	struct cdnr_block *cb;
   1138        1.1   thorpej 	struct tbmeter *tbm;
   1139        1.1   thorpej 	struct trtcm *tcm;
   1140        1.1   thorpej 	struct tswtcm *tsw;
   1141        1.1   thorpej 	struct tce_stats tce, *usp;
   1142        1.1   thorpej 	int error, n, nskip, nelements;
   1143        1.1   thorpej 
   1144        1.1   thorpej 	if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
   1145        1.1   thorpej 		return (EBADF);
   1146        1.1   thorpej 
   1147        1.1   thorpej 	/* copy action stats */
   1148        1.7  christos 	(void)memcpy(ap->cnts, top->tc_cnts, sizeof(ap->cnts));
   1149        1.1   thorpej 
   1150        1.1   thorpej 	/* stats for each element */
   1151        1.1   thorpej 	nelements = ap->nelements;
   1152        1.1   thorpej 	usp = ap->tce_stats;
   1153        1.1   thorpej 	if (nelements <= 0 || usp == NULL)
   1154        1.1   thorpej 		return (0);
   1155        1.1   thorpej 
   1156        1.1   thorpej 	nskip = ap->nskip;
   1157        1.1   thorpej 	n = 0;
   1158        1.1   thorpej 	LIST_FOREACH(cb, &top->tc_elements, cb_next) {
   1159        1.1   thorpej 		if (nskip > 0) {
   1160        1.1   thorpej 			nskip--;
   1161        1.1   thorpej 			continue;
   1162        1.1   thorpej 		}
   1163        1.1   thorpej 
   1164        1.7  christos 		(void)memset(&tce, 0, sizeof(tce));
   1165        1.1   thorpej 		tce.tce_handle = cb->cb_handle;
   1166        1.1   thorpej 		tce.tce_type = cb->cb_type;
   1167        1.1   thorpej 		switch (cb->cb_type) {
   1168        1.1   thorpej 		case TCETYPE_TBMETER:
   1169        1.1   thorpej 			tbm = (struct tbmeter *)cb;
   1170        1.1   thorpej 			tce.tce_cnts[0] = tbm->in_cnt;
   1171        1.1   thorpej 			tce.tce_cnts[1] = tbm->out_cnt;
   1172        1.1   thorpej 			break;
   1173        1.1   thorpej 		case TCETYPE_TRTCM:
   1174        1.1   thorpej 			tcm = (struct trtcm *)cb;
   1175        1.1   thorpej 			tce.tce_cnts[0] = tcm->green_cnt;
   1176        1.1   thorpej 			tce.tce_cnts[1] = tcm->yellow_cnt;
   1177        1.1   thorpej 			tce.tce_cnts[2] = tcm->red_cnt;
   1178        1.1   thorpej 			break;
   1179        1.1   thorpej 		case TCETYPE_TSWTCM:
   1180        1.1   thorpej 			tsw = (struct tswtcm *)cb;
   1181        1.1   thorpej 			tce.tce_cnts[0] = tsw->green_cnt;
   1182        1.1   thorpej 			tce.tce_cnts[1] = tsw->yellow_cnt;
   1183        1.1   thorpej 			tce.tce_cnts[2] = tsw->red_cnt;
   1184        1.1   thorpej 			break;
   1185        1.1   thorpej 		default:
   1186        1.1   thorpej 			continue;
   1187        1.1   thorpej 		}
   1188        1.1   thorpej 
   1189        1.1   thorpej 		if ((error = copyout((caddr_t)&tce, (caddr_t)usp++,
   1190        1.1   thorpej 				     sizeof(tce))) != 0)
   1191        1.1   thorpej 			return (error);
   1192        1.1   thorpej 
   1193        1.1   thorpej 		if (++n == nelements)
   1194        1.1   thorpej 			break;
   1195        1.1   thorpej 	}
   1196        1.1   thorpej 	ap->nelements = n;
   1197        1.1   thorpej 
   1198        1.1   thorpej 	return (0);
   1199        1.1   thorpej }
   1200        1.1   thorpej 
   1201        1.1   thorpej /*
   1202        1.1   thorpej  * conditioner device interface
   1203        1.1   thorpej  */
   1204        1.1   thorpej int
   1205       1.10  christos cdnropen(dev, flag, fmt, l)
   1206        1.1   thorpej 	dev_t dev;
   1207        1.1   thorpej 	int flag, fmt;
   1208       1.10  christos 	struct lwp *l;
   1209        1.1   thorpej {
   1210        1.1   thorpej 	if (machclk_freq == 0)
   1211        1.1   thorpej 		init_machclk();
   1212        1.1   thorpej 
   1213        1.1   thorpej 	if (machclk_freq == 0) {
   1214        1.8       wiz 		printf("cdnr: no CPU clock available!\n");
   1215        1.1   thorpej 		return (ENXIO);
   1216        1.1   thorpej 	}
   1217        1.1   thorpej 
   1218        1.1   thorpej 	/* everything will be done when the queueing scheme is attached. */
   1219        1.1   thorpej 	return 0;
   1220        1.1   thorpej }
   1221        1.1   thorpej 
   1222        1.1   thorpej int
   1223       1.10  christos cdnrclose(dev, flag, fmt, l)
   1224        1.1   thorpej 	dev_t dev;
   1225        1.1   thorpej 	int flag, fmt;
   1226       1.10  christos 	struct lwp *l;
   1227        1.1   thorpej {
   1228        1.1   thorpej 	struct top_cdnr *top;
   1229        1.1   thorpej 	int err, error = 0;
   1230        1.1   thorpej 
   1231        1.1   thorpej 	while ((top = LIST_FIRST(&tcb_list)) != NULL) {
   1232        1.1   thorpej 		/* destroy all */
   1233        1.1   thorpej 		err = top_destroy(top);
   1234        1.1   thorpej 		if (err != 0 && error == 0)
   1235        1.1   thorpej 			error = err;
   1236        1.1   thorpej 	}
   1237        1.1   thorpej 	altq_input = NULL;
   1238        1.1   thorpej 
   1239        1.1   thorpej 	return (error);
   1240        1.1   thorpej }
   1241        1.1   thorpej 
   1242        1.1   thorpej int
   1243       1.10  christos cdnrioctl(dev, cmd, addr, flag, l)
   1244        1.1   thorpej 	dev_t dev;
   1245        1.1   thorpej 	ioctlcmd_t cmd;
   1246        1.1   thorpej 	caddr_t addr;
   1247        1.1   thorpej 	int flag;
   1248       1.10  christos 	struct lwp *l;
   1249        1.1   thorpej {
   1250        1.1   thorpej 	struct top_cdnr *top;
   1251        1.1   thorpej 	struct cdnr_interface *ifacep;
   1252       1.10  christos 	struct proc *p = l->l_proc;
   1253        1.1   thorpej 	int	s, error = 0;
   1254        1.1   thorpej 
   1255        1.1   thorpej 	/* check super-user privilege */
   1256        1.1   thorpej 	switch (cmd) {
   1257        1.1   thorpej 	case CDNR_GETSTATS:
   1258        1.1   thorpej 		break;
   1259        1.1   thorpej 	default:
   1260        1.1   thorpej #if (__FreeBSD_version > 400000)
   1261        1.1   thorpej 		if ((error = suser(p)) != 0)
   1262        1.1   thorpej #else
   1263  1.10.10.1      elad 		if ((error = generic_authorize(p->p_cred,
   1264  1.10.10.1      elad 					       KAUTH_GENERIC_ISSUSER,
   1265  1.10.10.1      elad 					       &p->p_acflag)) != 0)
   1266        1.1   thorpej #endif
   1267        1.1   thorpej 			return (error);
   1268        1.1   thorpej 		break;
   1269        1.1   thorpej 	}
   1270        1.9     perry 
   1271        1.3   thorpej 	s = splnet();
   1272        1.1   thorpej 	switch (cmd) {
   1273        1.9     perry 
   1274        1.1   thorpej 	case CDNR_IF_ATTACH:
   1275        1.1   thorpej 		ifacep = (struct cdnr_interface *)addr;
   1276        1.1   thorpej 		error = cdnrcmd_if_attach(ifacep->cdnr_ifname);
   1277        1.1   thorpej 		break;
   1278        1.1   thorpej 
   1279        1.1   thorpej 	case CDNR_IF_DETACH:
   1280        1.1   thorpej 		ifacep = (struct cdnr_interface *)addr;
   1281        1.1   thorpej 		error = cdnrcmd_if_detach(ifacep->cdnr_ifname);
   1282        1.1   thorpej 		break;
   1283        1.1   thorpej 
   1284        1.1   thorpej 	case CDNR_ENABLE:
   1285        1.1   thorpej 	case CDNR_DISABLE:
   1286        1.1   thorpej 		ifacep = (struct cdnr_interface *)addr;
   1287        1.1   thorpej 		if ((top = tcb_lookup(ifacep->cdnr_ifname)) == NULL) {
   1288        1.1   thorpej 			error = EBADF;
   1289        1.1   thorpej 			break;
   1290        1.1   thorpej 		}
   1291        1.1   thorpej 
   1292        1.1   thorpej 		switch (cmd) {
   1293        1.1   thorpej 
   1294        1.1   thorpej 		case CDNR_ENABLE:
   1295        1.1   thorpej 			ALTQ_SET_CNDTNING(top->tc_ifq);
   1296        1.1   thorpej 			if (altq_input == NULL)
   1297        1.1   thorpej 				altq_input = altq_cdnr_input;
   1298        1.1   thorpej 			break;
   1299        1.1   thorpej 
   1300        1.1   thorpej 		case CDNR_DISABLE:
   1301        1.1   thorpej 			ALTQ_CLEAR_CNDTNING(top->tc_ifq);
   1302        1.1   thorpej 			LIST_FOREACH(top, &tcb_list, tc_next)
   1303        1.1   thorpej 				if (ALTQ_IS_CNDTNING(top->tc_ifq))
   1304        1.1   thorpej 					break;
   1305        1.1   thorpej 			if (top == NULL)
   1306        1.1   thorpej 				altq_input = NULL;
   1307        1.1   thorpej 			break;
   1308        1.1   thorpej 		}
   1309        1.1   thorpej 		break;
   1310        1.1   thorpej 
   1311        1.1   thorpej 	case CDNR_ADD_ELEM:
   1312        1.1   thorpej 		error = cdnrcmd_add_element((struct cdnr_add_element *)addr);
   1313        1.1   thorpej 		break;
   1314        1.1   thorpej 
   1315        1.1   thorpej 	case CDNR_DEL_ELEM:
   1316        1.1   thorpej 		error = cdnrcmd_delete_element((struct cdnr_delete_element *)addr);
   1317        1.1   thorpej 		break;
   1318        1.1   thorpej 
   1319        1.1   thorpej 	case CDNR_ADD_TBM:
   1320        1.1   thorpej 		error = cdnrcmd_add_tbm((struct cdnr_add_tbmeter *)addr);
   1321        1.1   thorpej 		break;
   1322        1.1   thorpej 
   1323        1.1   thorpej 	case CDNR_MOD_TBM:
   1324        1.1   thorpej 		error = cdnrcmd_modify_tbm((struct cdnr_modify_tbmeter *)addr);
   1325        1.1   thorpej 		break;
   1326        1.1   thorpej 
   1327        1.1   thorpej 	case CDNR_TBM_STATS:
   1328        1.1   thorpej 		error = cdnrcmd_tbm_stats((struct cdnr_tbmeter_stats *)addr);
   1329        1.1   thorpej 		break;
   1330        1.1   thorpej 
   1331        1.1   thorpej 	case CDNR_ADD_TCM:
   1332        1.1   thorpej 		error = cdnrcmd_add_trtcm((struct cdnr_add_trtcm *)addr);
   1333        1.1   thorpej 		break;
   1334        1.1   thorpej 
   1335        1.1   thorpej 	case CDNR_MOD_TCM:
   1336        1.1   thorpej 		error = cdnrcmd_modify_trtcm((struct cdnr_modify_trtcm *)addr);
   1337        1.1   thorpej 		break;
   1338        1.1   thorpej 
   1339        1.1   thorpej 	case CDNR_TCM_STATS:
   1340        1.1   thorpej 		error = cdnrcmd_tcm_stats((struct cdnr_tcm_stats *)addr);
   1341        1.1   thorpej 		break;
   1342        1.1   thorpej 
   1343        1.1   thorpej 	case CDNR_ADD_FILTER:
   1344        1.1   thorpej 		error = cdnrcmd_add_filter((struct cdnr_add_filter *)addr);
   1345        1.1   thorpej 		break;
   1346        1.1   thorpej 
   1347        1.1   thorpej 	case CDNR_DEL_FILTER:
   1348        1.1   thorpej 		error = cdnrcmd_delete_filter((struct cdnr_delete_filter *)addr);
   1349        1.1   thorpej 		break;
   1350        1.1   thorpej 
   1351        1.1   thorpej 	case CDNR_GETSTATS:
   1352        1.1   thorpej 		error = cdnrcmd_get_stats((struct cdnr_get_stats *)addr);
   1353        1.1   thorpej 		break;
   1354        1.1   thorpej 
   1355        1.1   thorpej 	case CDNR_ADD_TSW:
   1356        1.1   thorpej 		error = cdnrcmd_add_tswtcm((struct cdnr_add_tswtcm *)addr);
   1357        1.1   thorpej 		break;
   1358        1.1   thorpej 
   1359        1.1   thorpej 	case CDNR_MOD_TSW:
   1360        1.1   thorpej 		error = cdnrcmd_modify_tswtcm((struct cdnr_modify_tswtcm *)addr);
   1361        1.1   thorpej 		break;
   1362        1.1   thorpej 
   1363        1.1   thorpej 	default:
   1364        1.1   thorpej 		error = EINVAL;
   1365        1.1   thorpej 		break;
   1366        1.1   thorpej 	}
   1367        1.1   thorpej 	splx(s);
   1368        1.1   thorpej 
   1369        1.1   thorpej 	return error;
   1370        1.1   thorpej }
   1371        1.1   thorpej 
   1372        1.1   thorpej #ifdef KLD_MODULE
   1373        1.1   thorpej 
   1374        1.1   thorpej static struct altqsw cdnr_sw =
   1375        1.1   thorpej 	{"cdnr", cdnropen, cdnrclose, cdnrioctl};
   1376        1.1   thorpej 
   1377        1.1   thorpej ALTQ_MODULE(altq_cdnr, ALTQT_CDNR, &cdnr_sw);
   1378        1.1   thorpej 
   1379        1.1   thorpej #endif /* KLD_MODULE */
   1380        1.1   thorpej 
   1381        1.1   thorpej #endif /* ALTQ_CDNR */
   1382