Home | History | Annotate | Line # | Download | only in altq
altq_blue.c revision 1.15
      1 /*	$NetBSD: altq_blue.c,v 1.15 2006/05/14 21:24:49 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.15 2006/05/14 21:24:49 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 		rqp = malloc(sizeof(blue_queue_t), M_DEVBUF, M_WAITOK|M_ZERO);
    217 		if (rqp == NULL) {
    218 			error = ENOMEM;
    219 			break;
    220 		}
    221 
    222 		rqp->rq_q = malloc(sizeof(class_queue_t), M_DEVBUF,
    223 		    M_WAITOK|M_ZERO);
    224 		if (rqp->rq_q == NULL) {
    225 			free(rqp, M_DEVBUF);
    226 			error = ENOMEM;
    227 			break;
    228 		}
    229 
    230 		rqp->rq_blue = malloc(sizeof(blue_t), M_DEVBUF,
    231 		    M_WAITOK|M_ZERO);
    232 		if (rqp->rq_blue == NULL) {
    233 			free(rqp->rq_q, M_DEVBUF);
    234 			free(rqp, M_DEVBUF);
    235 			error = ENOMEM;
    236 			break;
    237 		}
    238 
    239 		rqp->rq_ifq = &ifp->if_snd;
    240 		qtail(rqp->rq_q) = NULL;
    241 		qlen(rqp->rq_q) = 0;
    242 		qlimit(rqp->rq_q) = BLUE_LIMIT;
    243 
    244 		/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
    245 		blue_init(rqp->rq_blue, 0, 800, 1000, 50000);
    246 
    247 		/*
    248 		 * set BLUE to this ifnet structure.
    249 		 */
    250 		error = altq_attach(rqp->rq_ifq, ALTQT_BLUE, rqp,
    251 				    blue_enqueue, blue_dequeue, blue_request,
    252 				    NULL, NULL);
    253 		if (error) {
    254 			free(rqp->rq_blue, M_DEVBUF);
    255 			free(rqp->rq_q, M_DEVBUF);
    256 			free(rqp, M_DEVBUF);
    257 			break;
    258 		}
    259 
    260 		/* add this state to the blue list */
    261 		rqp->rq_next = blue_list;
    262 		blue_list = rqp;
    263 		break;
    264 
    265 	case BLUE_IF_DETACH:
    266 		ifacep = (struct blue_interface *)addr;
    267 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    268 			error = EBADF;
    269 			break;
    270 		}
    271 		error = blue_detach(rqp);
    272 		break;
    273 
    274 	case BLUE_GETSTATS:
    275 		do {
    276 			struct blue_stats *q_stats;
    277 			blue_t *rp;
    278 
    279 			q_stats = (struct blue_stats *)addr;
    280 			if ((rqp = altq_lookup(q_stats->iface.blue_ifname,
    281 					     ALTQT_BLUE)) == NULL) {
    282 				error = EBADF;
    283 				break;
    284 			}
    285 
    286 			q_stats->q_len 	   = qlen(rqp->rq_q);
    287 			q_stats->q_limit   = qlimit(rqp->rq_q);
    288 
    289 			rp = rqp->rq_blue;
    290 			q_stats->q_pmark = rp->blue_pmark;
    291 			q_stats->xmit_packets  = rp->blue_stats.xmit_packets;
    292 			q_stats->xmit_bytes    = rp->blue_stats.xmit_bytes;
    293 			q_stats->drop_packets  = rp->blue_stats.drop_packets;
    294 			q_stats->drop_bytes    = rp->blue_stats.drop_bytes;
    295 			q_stats->drop_forced   = rp->blue_stats.drop_forced;
    296 			q_stats->drop_unforced = rp->blue_stats.drop_unforced;
    297 			q_stats->marked_packets = rp->blue_stats.marked_packets;
    298 
    299 		} while (0);
    300 		break;
    301 
    302 	case BLUE_CONFIG:
    303 		do {
    304 			struct blue_conf *fc;
    305 			int limit;
    306 
    307 			fc = (struct blue_conf *)addr;
    308 			if ((rqp = altq_lookup(fc->iface.blue_ifname,
    309 					       ALTQT_BLUE)) == NULL) {
    310 				error = EBADF;
    311 				break;
    312 			}
    313 			limit = fc->blue_limit;
    314 			qlimit(rqp->rq_q) = limit;
    315 			fc->blue_limit = limit;	/* write back the new value */
    316 			if (fc->blue_pkttime > 0)
    317 				rqp->rq_blue->blue_pkttime = fc->blue_pkttime;
    318 			if (fc->blue_max_pmark > 0)
    319 				rqp->rq_blue->blue_max_pmark = fc->blue_max_pmark;
    320 			if (fc->blue_hold_time > 0)
    321 				rqp->rq_blue->blue_hold_time = fc->blue_hold_time;
    322 			rqp->rq_blue->blue_flags = fc->blue_flags;
    323 
    324 			blue_init(rqp->rq_blue, rqp->rq_blue->blue_flags,
    325 				  rqp->rq_blue->blue_pkttime,
    326 				  rqp->rq_blue->blue_max_pmark,
    327 				  rqp->rq_blue->blue_hold_time);
    328 		} while (0);
    329 		break;
    330 
    331 	default:
    332 		error = EINVAL;
    333 		break;
    334 	}
    335 	return error;
    336 }
    337 
    338 static int blue_detach(rqp)
    339 	blue_queue_t *rqp;
    340 {
    341 	blue_queue_t *tmp;
    342 	int error = 0;
    343 
    344 	if (ALTQ_IS_ENABLED(rqp->rq_ifq))
    345 		altq_disable(rqp->rq_ifq);
    346 
    347 	if ((error = altq_detach(rqp->rq_ifq)))
    348 		return (error);
    349 
    350 	if (blue_list == rqp)
    351 		blue_list = rqp->rq_next;
    352 	else {
    353 		for (tmp = blue_list; tmp != NULL; tmp = tmp->rq_next)
    354 			if (tmp->rq_next == rqp) {
    355 				tmp->rq_next = rqp->rq_next;
    356 				break;
    357 			}
    358 		if (tmp == NULL)
    359 			printf("blue_detach: no state found in blue_list!\n");
    360 	}
    361 
    362 	free(rqp->rq_q, M_DEVBUF);
    363 	free(rqp->rq_blue, M_DEVBUF);
    364 	free(rqp, M_DEVBUF);
    365 	return (error);
    366 }
    367 
    368 /*
    369  * blue support routines
    370  */
    371 
    372 int
    373 blue_init(rp, flags, pkttime, blue_max_pmark, blue_hold_time)
    374 	blue_t 	*rp;
    375 	int	flags;
    376 	int	pkttime;
    377 	int	blue_max_pmark;
    378 	int	blue_hold_time;
    379 {
    380 	int npkts_per_sec;
    381 
    382 	rp->blue_idle = 1;
    383 	rp->blue_flags = flags;
    384 	rp->blue_pkttime = pkttime;
    385 	rp->blue_max_pmark = blue_max_pmark;
    386 	rp->blue_hold_time = blue_hold_time;
    387 	if (pkttime == 0)
    388 		rp->blue_pkttime = 1;
    389 
    390 	/* when the link is very slow, adjust blue parameters */
    391 	npkts_per_sec = 1000000 / rp->blue_pkttime;
    392 	if (npkts_per_sec < 50) {
    393 	}
    394 	else if (npkts_per_sec < 300) {
    395 	}
    396 
    397 	microtime(&rp->blue_last);
    398 	return (0);
    399 }
    400 
    401 /*
    402  * enqueue routine:
    403  *
    404  *	returns: 0 when successfully queued.
    405  *		 ENOBUFS when drop occurs.
    406  */
    407 static int
    408 blue_enqueue(ifq, m, pktattr)
    409 	struct ifaltq *ifq;
    410 	struct mbuf *m;
    411 	struct altq_pktattr *pktattr;
    412 {
    413 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    414 	int error = 0;
    415 
    416 	if (blue_addq(rqp->rq_blue, rqp->rq_q, m, pktattr) == 0)
    417 		ifq->ifq_len++;
    418 	else
    419 		error = ENOBUFS;
    420 	return error;
    421 }
    422 
    423 #define	DTYPE_NODROP	0	/* no drop */
    424 #define	DTYPE_FORCED	1	/* a "forced" drop */
    425 #define	DTYPE_EARLY	2	/* an "unforced" (early) drop */
    426 
    427 int
    428 blue_addq(rp, q, m, pktattr)
    429 	blue_t *rp;
    430 	class_queue_t *q;
    431 	struct mbuf *m;
    432 	struct altq_pktattr *pktattr;
    433 {
    434 	int droptype;
    435 
    436 	/*
    437 	 * if we were idle, this is an enqueue onto an empty queue
    438 	 * and we should decrement marking probability
    439 	 *
    440 	 */
    441 	if (rp->blue_idle) {
    442 		struct timeval now;
    443 		int t;
    444 		rp->blue_idle = 0;
    445 		microtime(&now);
    446 		t = (now.tv_sec - rp->blue_last.tv_sec);
    447 		if ( t > 1) {
    448 			rp->blue_pmark = 1;
    449 			microtime(&rp->blue_last);
    450 		} else {
    451 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    452 			if (t > rp->blue_hold_time) {
    453 				rp->blue_pmark--;
    454 				if (rp->blue_pmark < 0) rp->blue_pmark = 0;
    455 				microtime(&rp->blue_last);
    456 			}
    457 		}
    458 	}
    459 
    460 	/* see if we drop early */
    461 	droptype = DTYPE_NODROP;
    462 	if (drop_early(rp) && qlen(q) > 1) {
    463 		/* mark or drop by blue */
    464 		if ((rp->blue_flags & BLUEF_ECN) &&
    465 		    mark_ecn(m, pktattr, rp->blue_flags)) {
    466 			/* successfully marked.  do not drop. */
    467 #ifdef BLUE_STATS
    468 			rp->blue_stats.marked_packets++;
    469 #endif
    470 		} else {
    471 			/* unforced drop by blue */
    472 			droptype = DTYPE_EARLY;
    473 		}
    474 	}
    475 
    476 	/*
    477 	 * if the queue length hits the hard limit, it's a forced drop.
    478 	 */
    479 	if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
    480 		droptype = DTYPE_FORCED;
    481 
    482 	/* if successful or forced drop, enqueue this packet. */
    483 	if (droptype != DTYPE_EARLY)
    484 		_addq(q, m);
    485 
    486 	if (droptype != DTYPE_NODROP) {
    487 		if (droptype == DTYPE_EARLY) {
    488 			/* drop the incoming packet */
    489 #ifdef BLUE_STATS
    490 			rp->blue_stats.drop_unforced++;
    491 #endif
    492 		} else {
    493 			struct timeval now;
    494 			int t;
    495 			/* forced drop, select a victim packet in the queue. */
    496 			m = _getq_random(q);
    497 			microtime(&now);
    498 			t = (now.tv_sec - rp->blue_last.tv_sec);
    499 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    500 			if (t > rp->blue_hold_time) {
    501 				rp->blue_pmark += rp->blue_max_pmark >> 3;
    502 				if (rp->blue_pmark > rp->blue_max_pmark)
    503 					rp->blue_pmark = rp->blue_max_pmark;
    504 				microtime(&rp->blue_last);
    505 			}
    506 #ifdef BLUE_STATS
    507 			rp->blue_stats.drop_forced++;
    508 #endif
    509 		}
    510 #ifdef BLUE_STATS
    511 		rp->blue_stats.drop_packets++;
    512 		rp->blue_stats.drop_bytes += m->m_pkthdr.len;
    513 #endif
    514 		m_freem(m);
    515 		return (-1);
    516 	}
    517 	/* successfully queued */
    518 	return (0);
    519 }
    520 
    521 /*
    522  * early-drop probability is kept in blue_pmark
    523  *
    524  */
    525 static int
    526 drop_early(rp)
    527 	blue_t *rp;
    528 {
    529 	if ((random() % rp->blue_max_pmark) < rp->blue_pmark) {
    530 		/* drop or mark */
    531 		return (1);
    532 	}
    533 	/* no drop/mark */
    534 	return (0);
    535 }
    536 
    537 /*
    538  * try to mark CE bit to the packet.
    539  *    returns 1 if successfully marked, 0 otherwise.
    540  */
    541 static int
    542 mark_ecn(m, pktattr, flags)
    543 	struct mbuf *m;
    544 	struct altq_pktattr *pktattr;
    545 	int flags;
    546 {
    547 	struct mbuf *m0;
    548 
    549 	if (pktattr == NULL ||
    550 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
    551 		return (0);
    552 
    553 	/* verify that pattr_hdr is within the mbuf data */
    554 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
    555 		if ((pktattr->pattr_hdr >= m0->m_data) &&
    556 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
    557 			break;
    558 	if (m0 == NULL) {
    559 		/* ick, pattr_hdr is stale */
    560 		pktattr->pattr_af = AF_UNSPEC;
    561 		return (0);
    562 	}
    563 
    564 	switch (pktattr->pattr_af) {
    565 	case AF_INET:
    566 		if (flags & BLUEF_ECN4) {
    567 			struct ip *ip = (struct ip *)pktattr->pattr_hdr;
    568 			u_int8_t otos;
    569 			int sum;
    570 
    571 			if (ip->ip_v != 4)
    572 				return (0);	/* version mismatch! */
    573 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
    574 				return (0);	/* not-ECT */
    575 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
    576 				return (1);	/* already marked */
    577 
    578 			/*
    579 			 * ecn-capable but not marked,
    580 			 * mark CE and update checksum
    581 			 */
    582 			otos = ip->ip_tos;
    583 			ip->ip_tos |= IPTOS_ECN_CE;
    584 			/*
    585 			 * update checksum (from RFC1624)
    586 			 *	   HC' = ~(~HC + ~m + m')
    587 			 */
    588 			sum = ~ntohs(ip->ip_sum) & 0xffff;
    589 			sum += (~otos & 0xffff) + ip->ip_tos;
    590 			sum = (sum >> 16) + (sum & 0xffff);
    591 			sum += (sum >> 16);  /* add carry */
    592 			ip->ip_sum = htons(~sum & 0xffff);
    593 			return (1);
    594 		}
    595 		break;
    596 #ifdef INET6
    597 	case AF_INET6:
    598 		if (flags & BLUEF_ECN6) {
    599 			struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
    600 			u_int32_t flowlabel;
    601 
    602 			flowlabel = ntohl(ip6->ip6_flow);
    603 			if ((flowlabel >> 28) != 6)
    604 				return (0);	/* version mismatch! */
    605 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    606 			    (IPTOS_ECN_NOTECT << 20))
    607 				return (0);	/* not-ECT */
    608 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    609 			    (IPTOS_ECN_CE << 20))
    610 				return (1);	/* already marked */
    611 			/*
    612 			 * ecn-capable but not marked,  mark CE
    613 			 */
    614 			flowlabel |= (IPTOS_ECN_CE << 20);
    615 			ip6->ip6_flow = htonl(flowlabel);
    616 			return (1);
    617 		}
    618 		break;
    619 #endif  /* INET6 */
    620 	}
    621 
    622 	/* not marked */
    623 	return (0);
    624 }
    625 
    626 /*
    627  * dequeue routine:
    628  *	must be called in splnet.
    629  *
    630  *	returns: mbuf dequeued.
    631  *		 NULL when no packet is available in the queue.
    632  */
    633 
    634 static struct mbuf *
    635 blue_dequeue(ifq, op)
    636 	struct ifaltq *ifq;
    637 	int op;
    638 {
    639 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    640 	struct mbuf *m = NULL;
    641 
    642 	if (op == ALTDQ_POLL)
    643 		return (qhead(rqp->rq_q));
    644 
    645 	m = blue_getq(rqp->rq_blue, rqp->rq_q);
    646 	if (m != NULL)
    647 		ifq->ifq_len--;
    648 	return m;
    649 }
    650 
    651 struct mbuf *blue_getq(rp, q)
    652 	blue_t *rp;
    653 	class_queue_t *q;
    654 {
    655 	struct mbuf *m;
    656 
    657 	if ((m = _getq(q)) == NULL) {
    658 		if (rp->blue_idle == 0) {
    659 			rp->blue_idle = 1;
    660 			microtime(&rp->blue_last);
    661 		}
    662 		return NULL;
    663 	}
    664 
    665 	rp->blue_idle = 0;
    666 #ifdef BLUE_STATS
    667 	rp->blue_stats.xmit_packets++;
    668 	rp->blue_stats.xmit_bytes += m->m_pkthdr.len;
    669 #endif
    670 	return (m);
    671 }
    672 
    673 static int
    674 blue_request(ifq, req, arg)
    675 	struct ifaltq *ifq;
    676 	int req;
    677 	void *arg;
    678 {
    679 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    680 
    681 	switch (req) {
    682 	case ALTRQ_PURGE:
    683 		_flushq(rqp->rq_q);
    684 		if (ALTQ_IS_ENABLED(ifq))
    685 			ifq->ifq_len = 0;
    686 		break;
    687 	}
    688 	return (0);
    689 }
    690 
    691 
    692 #ifdef KLD_MODULE
    693 
    694 static struct altqsw blue_sw =
    695 	{"blue", blueopen, blueclose, blueioctl};
    696 
    697 ALTQ_MODULE(altq_blue, ALTQT_BLUE, &blue_sw);
    698 
    699 #endif /* KLD_MODULE */
    700 
    701 #endif /* ALTQ_BLUE */
    702