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