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