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