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