Home | History | Annotate | Line # | Download | only in altq
altq_blue.c revision 1.12.10.2
      1 /*	$NetBSD: altq_blue.c,v 1.12.10.2 2006/03/10 13:29:35 elad Exp $	*/
      2 /*	$KAME: altq_blue.c,v 1.8 2002/01/07 11:25:40 kjc Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2000
      6  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  */
     30 /*
     31  * Copyright (c) 1990-1994 Regents of the University of California.
     32  * All rights reserved.
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *	This product includes software developed by the Computer Systems
     45  *	Engineering Group at Lawrence Berkeley Laboratory.
     46  * 4. Neither the name of the University nor of the Laboratory may be used
     47  *    to endorse or promote products derived from this software without
     48  *    specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: altq_blue.c,v 1.12.10.2 2006/03/10 13:29:35 elad Exp $");
     65 
     66 #if defined(__FreeBSD__) || defined(__NetBSD__)
     67 #include "opt_altq.h"
     68 #if (__FreeBSD__ != 2)
     69 #include "opt_inet.h"
     70 #ifdef __FreeBSD__
     71 #include "opt_inet6.h"
     72 #endif
     73 #endif
     74 #endif /* __FreeBSD__ || __NetBSD__ */
     75 #ifdef ALTQ_BLUE	/* blue is enabled by ALTQ_BLUE option in opt_altq.h */
     76 
     77 #include <sys/param.h>
     78 #include <sys/malloc.h>
     79 #include <sys/mbuf.h>
     80 #include <sys/socket.h>
     81 #include <sys/sockio.h>
     82 #include <sys/systm.h>
     83 #include <sys/proc.h>
     84 #include <sys/errno.h>
     85 #include <sys/kernel.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_types.h>
     89 #include <netinet/in.h>
     90 #include <netinet/in_systm.h>
     91 #include <netinet/ip.h>
     92 #ifdef INET6
     93 #include <netinet/ip6.h>
     94 #endif
     95 
     96 #include <altq/altq.h>
     97 #include <altq/altq_conf.h>
     98 #include <altq/altq_blue.h>
     99 
    100 /*
    101  * Blue is proposed and implemented by Wu-chang Feng <wuchang (at) eecs.umich.edu>.
    102  * more information on Blue is available from
    103  * http://www.thefengs.com/wuchang/work/blue/
    104  */
    105 
    106 /* fixed-point uses 12-bit decimal places */
    107 #define	FP_SHIFT	12	/* fixed-point shift */
    108 
    109 #define	BLUE_LIMIT	200	/* default max queue length */
    110 
    111 /* blue_list keeps all blue_state_t's allocated. */
    112 static blue_queue_t *blue_list = NULL;
    113 
    114 /* internal function prototypes */
    115 static int blue_enqueue __P((struct ifaltq *, struct mbuf *,
    116 			     struct altq_pktattr *));
    117 static struct mbuf *blue_dequeue __P((struct ifaltq *, int));
    118 static int drop_early __P((blue_t *));
    119 static int mark_ecn __P((struct mbuf *, struct altq_pktattr *, int));
    120 static int blue_detach __P((blue_queue_t *));
    121 static int blue_request __P((struct ifaltq *, int, void *));
    122 
    123 /*
    124  * blue device interface
    125  */
    126 altqdev_decl(blue);
    127 
    128 int
    129 blueopen(dev, flag, fmt, l)
    130 	dev_t dev;
    131 	int flag, fmt;
    132 	struct lwp *l;
    133 {
    134 	/* everything will be done when the queueing scheme is attached. */
    135 	return 0;
    136 }
    137 
    138 int
    139 blueclose(dev, flag, fmt, l)
    140 	dev_t dev;
    141 	int flag, fmt;
    142 	struct lwp *l;
    143 {
    144 	blue_queue_t *rqp;
    145 	int err, error = 0;
    146 
    147 	while ((rqp = blue_list) != NULL) {
    148 		/* destroy all */
    149 		err = blue_detach(rqp);
    150 		if (err != 0 && error == 0)
    151 			error = err;
    152 	}
    153 
    154 	return error;
    155 }
    156 
    157 int
    158 blueioctl(dev, cmd, addr, flag, l)
    159 	dev_t dev;
    160 	ioctlcmd_t cmd;
    161 	caddr_t addr;
    162 	int flag;
    163 	struct lwp *l;
    164 {
    165 	blue_queue_t *rqp;
    166 	struct blue_interface *ifacep;
    167 	struct ifnet *ifp;
    168 	struct proc *p = l->l_proc;
    169 	int	error = 0;
    170 
    171 	/* check super-user privilege */
    172 	switch (cmd) {
    173 	case BLUE_GETSTATS:
    174 		break;
    175 	default:
    176 #if (__FreeBSD_version > 400000)
    177 		if ((error = suser(p)) != 0)
    178 			return (error);
    179 #else
    180 		if ((error = kauth_authorize_generic(p->p_cred,
    181 					       KAUTH_GENERIC_ISSUSER,
    182 					       &p->p_acflag)) != 0)
    183 			return (error);
    184 #endif
    185 		break;
    186 	}
    187 
    188 	switch (cmd) {
    189 
    190 	case BLUE_ENABLE:
    191 		ifacep = (struct blue_interface *)addr;
    192 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    193 			error = EBADF;
    194 			break;
    195 		}
    196 		error = altq_enable(rqp->rq_ifq);
    197 		break;
    198 
    199 	case BLUE_DISABLE:
    200 		ifacep = (struct blue_interface *)addr;
    201 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    202 			error = EBADF;
    203 			break;
    204 		}
    205 		error = altq_disable(rqp->rq_ifq);
    206 		break;
    207 
    208 	case BLUE_IF_ATTACH:
    209 		ifp = ifunit(((struct blue_interface *)addr)->blue_ifname);
    210 		if (ifp == NULL) {
    211 			error = ENXIO;
    212 			break;
    213 		}
    214 
    215 		/* allocate and initialize blue_state_t */
    216 		MALLOC(rqp, blue_queue_t *, sizeof(blue_queue_t), M_DEVBUF, M_WAITOK);
    217 		(void)memset(rqp, 0, sizeof(blue_queue_t));
    218 
    219 		MALLOC(rqp->rq_q, class_queue_t *, sizeof(class_queue_t),
    220 		       M_DEVBUF, M_WAITOK);
    221 		(void)memset(rqp->rq_q, 0, sizeof(class_queue_t));
    222 
    223 		MALLOC(rqp->rq_blue, blue_t *, sizeof(blue_t), M_DEVBUF, M_WAITOK);
    224 		(void)memset(rqp->rq_blue, 0, sizeof(blue_t));
    225 
    226 		rqp->rq_ifq = &ifp->if_snd;
    227 		qtail(rqp->rq_q) = NULL;
    228 		qlen(rqp->rq_q) = 0;
    229 		qlimit(rqp->rq_q) = BLUE_LIMIT;
    230 
    231 		/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
    232 		blue_init(rqp->rq_blue, 0, 800, 1000, 50000);
    233 
    234 		/*
    235 		 * set BLUE to this ifnet structure.
    236 		 */
    237 		error = altq_attach(rqp->rq_ifq, ALTQT_BLUE, rqp,
    238 				    blue_enqueue, blue_dequeue, blue_request,
    239 				    NULL, NULL);
    240 		if (error) {
    241 			FREE(rqp->rq_blue, M_DEVBUF);
    242 			FREE(rqp->rq_q, M_DEVBUF);
    243 			FREE(rqp, M_DEVBUF);
    244 			break;
    245 		}
    246 
    247 		/* add this state to the blue list */
    248 		rqp->rq_next = blue_list;
    249 		blue_list = rqp;
    250 		break;
    251 
    252 	case BLUE_IF_DETACH:
    253 		ifacep = (struct blue_interface *)addr;
    254 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    255 			error = EBADF;
    256 			break;
    257 		}
    258 		error = blue_detach(rqp);
    259 		break;
    260 
    261 	case BLUE_GETSTATS:
    262 		do {
    263 			struct blue_stats *q_stats;
    264 			blue_t *rp;
    265 
    266 			q_stats = (struct blue_stats *)addr;
    267 			if ((rqp = altq_lookup(q_stats->iface.blue_ifname,
    268 					     ALTQT_BLUE)) == NULL) {
    269 				error = EBADF;
    270 				break;
    271 			}
    272 
    273 			q_stats->q_len 	   = qlen(rqp->rq_q);
    274 			q_stats->q_limit   = qlimit(rqp->rq_q);
    275 
    276 			rp = rqp->rq_blue;
    277 			q_stats->q_pmark = rp->blue_pmark;
    278 			q_stats->xmit_packets  = rp->blue_stats.xmit_packets;
    279 			q_stats->xmit_bytes    = rp->blue_stats.xmit_bytes;
    280 			q_stats->drop_packets  = rp->blue_stats.drop_packets;
    281 			q_stats->drop_bytes    = rp->blue_stats.drop_bytes;
    282 			q_stats->drop_forced   = rp->blue_stats.drop_forced;
    283 			q_stats->drop_unforced = rp->blue_stats.drop_unforced;
    284 			q_stats->marked_packets = rp->blue_stats.marked_packets;
    285 
    286 		} while (0);
    287 		break;
    288 
    289 	case BLUE_CONFIG:
    290 		do {
    291 			struct blue_conf *fc;
    292 			int limit;
    293 
    294 			fc = (struct blue_conf *)addr;
    295 			if ((rqp = altq_lookup(fc->iface.blue_ifname,
    296 					       ALTQT_BLUE)) == NULL) {
    297 				error = EBADF;
    298 				break;
    299 			}
    300 			limit = fc->blue_limit;
    301 			qlimit(rqp->rq_q) = limit;
    302 			fc->blue_limit = limit;	/* write back the new value */
    303 			if (fc->blue_pkttime > 0)
    304 				rqp->rq_blue->blue_pkttime = fc->blue_pkttime;
    305 			if (fc->blue_max_pmark > 0)
    306 				rqp->rq_blue->blue_max_pmark = fc->blue_max_pmark;
    307 			if (fc->blue_hold_time > 0)
    308 				rqp->rq_blue->blue_hold_time = fc->blue_hold_time;
    309 			rqp->rq_blue->blue_flags = fc->blue_flags;
    310 
    311 			blue_init(rqp->rq_blue, rqp->rq_blue->blue_flags,
    312 				  rqp->rq_blue->blue_pkttime,
    313 				  rqp->rq_blue->blue_max_pmark,
    314 				  rqp->rq_blue->blue_hold_time);
    315 		} while (0);
    316 		break;
    317 
    318 	default:
    319 		error = EINVAL;
    320 		break;
    321 	}
    322 	return error;
    323 }
    324 
    325 static int blue_detach(rqp)
    326 	blue_queue_t *rqp;
    327 {
    328 	blue_queue_t *tmp;
    329 	int error = 0;
    330 
    331 	if (ALTQ_IS_ENABLED(rqp->rq_ifq))
    332 		altq_disable(rqp->rq_ifq);
    333 
    334 	if ((error = altq_detach(rqp->rq_ifq)))
    335 		return (error);
    336 
    337 	if (blue_list == rqp)
    338 		blue_list = rqp->rq_next;
    339 	else {
    340 		for (tmp = blue_list; tmp != NULL; tmp = tmp->rq_next)
    341 			if (tmp->rq_next == rqp) {
    342 				tmp->rq_next = rqp->rq_next;
    343 				break;
    344 			}
    345 		if (tmp == NULL)
    346 			printf("blue_detach: no state found in blue_list!\n");
    347 	}
    348 
    349 	FREE(rqp->rq_q, M_DEVBUF);
    350 	FREE(rqp->rq_blue, M_DEVBUF);
    351 	FREE(rqp, M_DEVBUF);
    352 	return (error);
    353 }
    354 
    355 /*
    356  * blue support routines
    357  */
    358 
    359 int
    360 blue_init(rp, flags, pkttime, blue_max_pmark, blue_hold_time)
    361 	blue_t 	*rp;
    362 	int	flags;
    363 	int	pkttime;
    364 	int	blue_max_pmark;
    365 	int	blue_hold_time;
    366 {
    367 	int npkts_per_sec;
    368 
    369 	rp->blue_idle = 1;
    370 	rp->blue_flags = flags;
    371 	rp->blue_pkttime = pkttime;
    372 	rp->blue_max_pmark = blue_max_pmark;
    373 	rp->blue_hold_time = blue_hold_time;
    374 	if (pkttime == 0)
    375 		rp->blue_pkttime = 1;
    376 
    377 	/* when the link is very slow, adjust blue parameters */
    378 	npkts_per_sec = 1000000 / rp->blue_pkttime;
    379 	if (npkts_per_sec < 50) {
    380 	}
    381 	else if (npkts_per_sec < 300) {
    382 	}
    383 
    384 	microtime(&rp->blue_last);
    385 	return (0);
    386 }
    387 
    388 /*
    389  * enqueue routine:
    390  *
    391  *	returns: 0 when successfully queued.
    392  *		 ENOBUFS when drop occurs.
    393  */
    394 static int
    395 blue_enqueue(ifq, m, pktattr)
    396 	struct ifaltq *ifq;
    397 	struct mbuf *m;
    398 	struct altq_pktattr *pktattr;
    399 {
    400 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    401 	int error = 0;
    402 
    403 	if (blue_addq(rqp->rq_blue, rqp->rq_q, m, pktattr) == 0)
    404 		ifq->ifq_len++;
    405 	else
    406 		error = ENOBUFS;
    407 	return error;
    408 }
    409 
    410 #define	DTYPE_NODROP	0	/* no drop */
    411 #define	DTYPE_FORCED	1	/* a "forced" drop */
    412 #define	DTYPE_EARLY	2	/* an "unforced" (early) drop */
    413 
    414 int
    415 blue_addq(rp, q, m, pktattr)
    416 	blue_t *rp;
    417 	class_queue_t *q;
    418 	struct mbuf *m;
    419 	struct altq_pktattr *pktattr;
    420 {
    421 	int droptype;
    422 
    423 	/*
    424 	 * if we were idle, this is an enqueue onto an empty queue
    425 	 * and we should decrement marking probability
    426 	 *
    427 	 */
    428 	if (rp->blue_idle) {
    429 		struct timeval now;
    430 		int t;
    431 		rp->blue_idle = 0;
    432 		microtime(&now);
    433 		t = (now.tv_sec - rp->blue_last.tv_sec);
    434 		if ( t > 1) {
    435 			rp->blue_pmark = 1;
    436 			microtime(&rp->blue_last);
    437 		} else {
    438 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    439 			if (t > rp->blue_hold_time) {
    440 				rp->blue_pmark--;
    441 				if (rp->blue_pmark < 0) rp->blue_pmark = 0;
    442 				microtime(&rp->blue_last);
    443 			}
    444 		}
    445 	}
    446 
    447 	/* see if we drop early */
    448 	droptype = DTYPE_NODROP;
    449 	if (drop_early(rp) && qlen(q) > 1) {
    450 		/* mark or drop by blue */
    451 		if ((rp->blue_flags & BLUEF_ECN) &&
    452 		    mark_ecn(m, pktattr, rp->blue_flags)) {
    453 			/* successfully marked.  do not drop. */
    454 #ifdef BLUE_STATS
    455 			rp->blue_stats.marked_packets++;
    456 #endif
    457 		} else {
    458 			/* unforced drop by blue */
    459 			droptype = DTYPE_EARLY;
    460 		}
    461 	}
    462 
    463 	/*
    464 	 * if the queue length hits the hard limit, it's a forced drop.
    465 	 */
    466 	if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
    467 		droptype = DTYPE_FORCED;
    468 
    469 	/* if successful or forced drop, enqueue this packet. */
    470 	if (droptype != DTYPE_EARLY)
    471 		_addq(q, m);
    472 
    473 	if (droptype != DTYPE_NODROP) {
    474 		if (droptype == DTYPE_EARLY) {
    475 			/* drop the incoming packet */
    476 #ifdef BLUE_STATS
    477 			rp->blue_stats.drop_unforced++;
    478 #endif
    479 		} else {
    480 			struct timeval now;
    481 			int t;
    482 			/* forced drop, select a victim packet in the queue. */
    483 			m = _getq_random(q);
    484 			microtime(&now);
    485 			t = (now.tv_sec - rp->blue_last.tv_sec);
    486 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    487 			if (t > rp->blue_hold_time) {
    488 				rp->blue_pmark += rp->blue_max_pmark >> 3;
    489 				if (rp->blue_pmark > rp->blue_max_pmark)
    490 					rp->blue_pmark = rp->blue_max_pmark;
    491 				microtime(&rp->blue_last);
    492 			}
    493 #ifdef BLUE_STATS
    494 			rp->blue_stats.drop_forced++;
    495 #endif
    496 		}
    497 #ifdef BLUE_STATS
    498 		rp->blue_stats.drop_packets++;
    499 		rp->blue_stats.drop_bytes += m->m_pkthdr.len;
    500 #endif
    501 		m_freem(m);
    502 		return (-1);
    503 	}
    504 	/* successfully queued */
    505 	return (0);
    506 }
    507 
    508 /*
    509  * early-drop probability is kept in blue_pmark
    510  *
    511  */
    512 static int
    513 drop_early(rp)
    514 	blue_t *rp;
    515 {
    516 	if ((random() % rp->blue_max_pmark) < rp->blue_pmark) {
    517 		/* drop or mark */
    518 		return (1);
    519 	}
    520 	/* no drop/mark */
    521 	return (0);
    522 }
    523 
    524 /*
    525  * try to mark CE bit to the packet.
    526  *    returns 1 if successfully marked, 0 otherwise.
    527  */
    528 static int
    529 mark_ecn(m, pktattr, flags)
    530 	struct mbuf *m;
    531 	struct altq_pktattr *pktattr;
    532 	int flags;
    533 {
    534 	struct mbuf *m0;
    535 
    536 	if (pktattr == NULL ||
    537 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
    538 		return (0);
    539 
    540 	/* verify that pattr_hdr is within the mbuf data */
    541 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
    542 		if ((pktattr->pattr_hdr >= m0->m_data) &&
    543 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
    544 			break;
    545 	if (m0 == NULL) {
    546 		/* ick, pattr_hdr is stale */
    547 		pktattr->pattr_af = AF_UNSPEC;
    548 		return (0);
    549 	}
    550 
    551 	switch (pktattr->pattr_af) {
    552 	case AF_INET:
    553 		if (flags & BLUEF_ECN4) {
    554 			struct ip *ip = (struct ip *)pktattr->pattr_hdr;
    555 			u_int8_t otos;
    556 			int sum;
    557 
    558 			if (ip->ip_v != 4)
    559 				return (0);	/* version mismatch! */
    560 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
    561 				return (0);	/* not-ECT */
    562 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
    563 				return (1);	/* already marked */
    564 
    565 			/*
    566 			 * ecn-capable but not marked,
    567 			 * mark CE and update checksum
    568 			 */
    569 			otos = ip->ip_tos;
    570 			ip->ip_tos |= IPTOS_ECN_CE;
    571 			/*
    572 			 * update checksum (from RFC1624)
    573 			 *	   HC' = ~(~HC + ~m + m')
    574 			 */
    575 			sum = ~ntohs(ip->ip_sum) & 0xffff;
    576 			sum += (~otos & 0xffff) + ip->ip_tos;
    577 			sum = (sum >> 16) + (sum & 0xffff);
    578 			sum += (sum >> 16);  /* add carry */
    579 			ip->ip_sum = htons(~sum & 0xffff);
    580 			return (1);
    581 		}
    582 		break;
    583 #ifdef INET6
    584 	case AF_INET6:
    585 		if (flags & BLUEF_ECN6) {
    586 			struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
    587 			u_int32_t flowlabel;
    588 
    589 			flowlabel = ntohl(ip6->ip6_flow);
    590 			if ((flowlabel >> 28) != 6)
    591 				return (0);	/* version mismatch! */
    592 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    593 			    (IPTOS_ECN_NOTECT << 20))
    594 				return (0);	/* not-ECT */
    595 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    596 			    (IPTOS_ECN_CE << 20))
    597 				return (1);	/* already marked */
    598 			/*
    599 			 * ecn-capable but not marked,  mark CE
    600 			 */
    601 			flowlabel |= (IPTOS_ECN_CE << 20);
    602 			ip6->ip6_flow = htonl(flowlabel);
    603 			return (1);
    604 		}
    605 		break;
    606 #endif  /* INET6 */
    607 	}
    608 
    609 	/* not marked */
    610 	return (0);
    611 }
    612 
    613 /*
    614  * dequeue routine:
    615  *	must be called in splnet.
    616  *
    617  *	returns: mbuf dequeued.
    618  *		 NULL when no packet is available in the queue.
    619  */
    620 
    621 static struct mbuf *
    622 blue_dequeue(ifq, op)
    623 	struct ifaltq *ifq;
    624 	int op;
    625 {
    626 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    627 	struct mbuf *m = NULL;
    628 
    629 	if (op == ALTDQ_POLL)
    630 		return (qhead(rqp->rq_q));
    631 
    632 	m = blue_getq(rqp->rq_blue, rqp->rq_q);
    633 	if (m != NULL)
    634 		ifq->ifq_len--;
    635 	return m;
    636 }
    637 
    638 struct mbuf *blue_getq(rp, q)
    639 	blue_t *rp;
    640 	class_queue_t *q;
    641 {
    642 	struct mbuf *m;
    643 
    644 	if ((m = _getq(q)) == NULL) {
    645 		if (rp->blue_idle == 0) {
    646 			rp->blue_idle = 1;
    647 			microtime(&rp->blue_last);
    648 		}
    649 		return NULL;
    650 	}
    651 
    652 	rp->blue_idle = 0;
    653 #ifdef BLUE_STATS
    654 	rp->blue_stats.xmit_packets++;
    655 	rp->blue_stats.xmit_bytes += m->m_pkthdr.len;
    656 #endif
    657 	return (m);
    658 }
    659 
    660 static int
    661 blue_request(ifq, req, arg)
    662 	struct ifaltq *ifq;
    663 	int req;
    664 	void *arg;
    665 {
    666 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    667 
    668 	switch (req) {
    669 	case ALTRQ_PURGE:
    670 		_flushq(rqp->rq_q);
    671 		if (ALTQ_IS_ENABLED(ifq))
    672 			ifq->ifq_len = 0;
    673 		break;
    674 	}
    675 	return (0);
    676 }
    677 
    678 
    679 #ifdef KLD_MODULE
    680 
    681 static struct altqsw blue_sw =
    682 	{"blue", blueopen, blueclose, blueioctl};
    683 
    684 ALTQ_MODULE(altq_blue, ALTQT_BLUE, &blue_sw);
    685 
    686 #endif /* KLD_MODULE */
    687 
    688 #endif /* ALTQ_BLUE */
    689