Home | History | Annotate | Line # | Download | only in altq
altq_blue.c revision 1.11.4.1
      1  1.11.4.1     yamt /*	$NetBSD: altq_blue.c,v 1.11.4.1 2006/06/21 14:47:46 yamt Exp $	*/
      2       1.5   itojun /*	$KAME: altq_blue.c,v 1.8 2002/01/07 11:25:40 kjc Exp $	*/
      3       1.1  thorpej 
      4       1.1  thorpej /*
      5       1.1  thorpej  * Copyright (C) 1997-2000
      6       1.1  thorpej  *	Sony Computer Science Laboratories Inc.  All rights reserved.
      7       1.1  thorpej  *
      8       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      9       1.1  thorpej  * modification, are permitted provided that the following conditions
     10       1.1  thorpej  * are met:
     11       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     12       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     13       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     16       1.1  thorpej  *
     17       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
     18       1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19       1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20       1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
     21       1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1  thorpej  * SUCH DAMAGE.
     28       1.1  thorpej  *
     29       1.1  thorpej  */
     30       1.1  thorpej /*
     31       1.1  thorpej  * Copyright (c) 1990-1994 Regents of the University of California.
     32       1.1  thorpej  * All rights reserved.
     33       1.1  thorpej  *
     34       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     35       1.1  thorpej  * modification, are permitted provided that the following conditions
     36       1.1  thorpej  * are met:
     37       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     38       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     39       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     40       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     41       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     42       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     43       1.1  thorpej  *    must display the following acknowledgement:
     44       1.1  thorpej  *	This product includes software developed by the Computer Systems
     45       1.1  thorpej  *	Engineering Group at Lawrence Berkeley Laboratory.
     46       1.1  thorpej  * 4. Neither the name of the University nor of the Laboratory may be used
     47       1.1  thorpej  *    to endorse or promote products derived from this software without
     48       1.1  thorpej  *    specific prior written permission.
     49       1.1  thorpej  *
     50       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51       1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52       1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53       1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54       1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55       1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56       1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57       1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58       1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59       1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60       1.1  thorpej  * SUCH DAMAGE.
     61       1.1  thorpej  */
     62       1.4    lukem 
     63       1.4    lukem #include <sys/cdefs.h>
     64  1.11.4.1     yamt __KERNEL_RCSID(0, "$NetBSD: altq_blue.c,v 1.11.4.1 2006/06/21 14:47:46 yamt Exp $");
     65       1.1  thorpej 
     66       1.1  thorpej #if defined(__FreeBSD__) || defined(__NetBSD__)
     67       1.1  thorpej #include "opt_altq.h"
     68       1.1  thorpej #if (__FreeBSD__ != 2)
     69       1.1  thorpej #include "opt_inet.h"
     70       1.1  thorpej #ifdef __FreeBSD__
     71       1.1  thorpej #include "opt_inet6.h"
     72       1.1  thorpej #endif
     73       1.1  thorpej #endif
     74       1.1  thorpej #endif /* __FreeBSD__ || __NetBSD__ */
     75       1.1  thorpej #ifdef ALTQ_BLUE	/* blue is enabled by ALTQ_BLUE option in opt_altq.h */
     76       1.1  thorpej 
     77       1.1  thorpej #include <sys/param.h>
     78       1.1  thorpej #include <sys/malloc.h>
     79       1.1  thorpej #include <sys/mbuf.h>
     80       1.1  thorpej #include <sys/socket.h>
     81       1.1  thorpej #include <sys/sockio.h>
     82       1.1  thorpej #include <sys/systm.h>
     83       1.1  thorpej #include <sys/proc.h>
     84       1.1  thorpej #include <sys/errno.h>
     85       1.1  thorpej #include <sys/kernel.h>
     86  1.11.4.1     yamt #include <sys/kauth.h>
     87       1.1  thorpej 
     88       1.1  thorpej #include <net/if.h>
     89       1.1  thorpej #include <net/if_types.h>
     90       1.1  thorpej #include <netinet/in.h>
     91       1.1  thorpej #include <netinet/in_systm.h>
     92       1.1  thorpej #include <netinet/ip.h>
     93       1.1  thorpej #ifdef INET6
     94       1.1  thorpej #include <netinet/ip6.h>
     95       1.1  thorpej #endif
     96       1.1  thorpej 
     97       1.1  thorpej #include <altq/altq.h>
     98       1.1  thorpej #include <altq/altq_conf.h>
     99       1.1  thorpej #include <altq/altq_blue.h>
    100       1.1  thorpej 
    101       1.1  thorpej /*
    102       1.1  thorpej  * Blue is proposed and implemented by Wu-chang Feng <wuchang (at) eecs.umich.edu>.
    103       1.1  thorpej  * more information on Blue is available from
    104      1.10      abs  * http://www.thefengs.com/wuchang/work/blue/
    105       1.1  thorpej  */
    106       1.1  thorpej 
    107       1.1  thorpej /* fixed-point uses 12-bit decimal places */
    108       1.1  thorpej #define	FP_SHIFT	12	/* fixed-point shift */
    109       1.1  thorpej 
    110       1.9      wiz #define	BLUE_LIMIT	200	/* default max queue length */
    111       1.1  thorpej 
    112       1.1  thorpej /* blue_list keeps all blue_state_t's allocated. */
    113       1.1  thorpej static blue_queue_t *blue_list = NULL;
    114       1.1  thorpej 
    115       1.1  thorpej /* internal function prototypes */
    116       1.1  thorpej static int blue_enqueue __P((struct ifaltq *, struct mbuf *,
    117       1.1  thorpej 			     struct altq_pktattr *));
    118       1.1  thorpej static struct mbuf *blue_dequeue __P((struct ifaltq *, int));
    119       1.1  thorpej static int drop_early __P((blue_t *));
    120       1.1  thorpej static int mark_ecn __P((struct mbuf *, struct altq_pktattr *, int));
    121       1.1  thorpej static int blue_detach __P((blue_queue_t *));
    122       1.1  thorpej static int blue_request __P((struct ifaltq *, int, void *));
    123       1.1  thorpej 
    124       1.1  thorpej /*
    125       1.1  thorpej  * blue device interface
    126       1.1  thorpej  */
    127       1.1  thorpej altqdev_decl(blue);
    128       1.1  thorpej 
    129       1.1  thorpej int
    130  1.11.4.1     yamt blueopen(dev, flag, fmt, l)
    131       1.1  thorpej 	dev_t dev;
    132       1.1  thorpej 	int flag, fmt;
    133  1.11.4.1     yamt 	struct lwp *l;
    134       1.1  thorpej {
    135       1.1  thorpej 	/* everything will be done when the queueing scheme is attached. */
    136       1.1  thorpej 	return 0;
    137       1.1  thorpej }
    138       1.1  thorpej 
    139       1.1  thorpej int
    140  1.11.4.1     yamt blueclose(dev, flag, fmt, l)
    141       1.1  thorpej 	dev_t dev;
    142       1.1  thorpej 	int flag, fmt;
    143  1.11.4.1     yamt 	struct lwp *l;
    144       1.1  thorpej {
    145       1.1  thorpej 	blue_queue_t *rqp;
    146       1.1  thorpej 	int err, error = 0;
    147       1.1  thorpej 
    148       1.1  thorpej 	while ((rqp = blue_list) != NULL) {
    149       1.1  thorpej 		/* destroy all */
    150       1.1  thorpej 		err = blue_detach(rqp);
    151       1.1  thorpej 		if (err != 0 && error == 0)
    152       1.1  thorpej 			error = err;
    153       1.1  thorpej 	}
    154       1.1  thorpej 
    155       1.1  thorpej 	return error;
    156       1.1  thorpej }
    157       1.1  thorpej 
    158       1.1  thorpej int
    159  1.11.4.1     yamt blueioctl(dev, cmd, addr, flag, l)
    160       1.1  thorpej 	dev_t dev;
    161       1.1  thorpej 	ioctlcmd_t cmd;
    162       1.1  thorpej 	caddr_t addr;
    163       1.1  thorpej 	int flag;
    164  1.11.4.1     yamt 	struct lwp *l;
    165       1.1  thorpej {
    166       1.1  thorpej 	blue_queue_t *rqp;
    167       1.1  thorpej 	struct blue_interface *ifacep;
    168       1.1  thorpej 	struct ifnet *ifp;
    169  1.11.4.1     yamt 	struct proc *p = l->l_proc;
    170       1.1  thorpej 	int	error = 0;
    171       1.1  thorpej 
    172       1.1  thorpej 	/* check super-user privilege */
    173       1.1  thorpej 	switch (cmd) {
    174       1.1  thorpej 	case BLUE_GETSTATS:
    175       1.1  thorpej 		break;
    176       1.1  thorpej 	default:
    177       1.1  thorpej #if (__FreeBSD_version > 400000)
    178       1.1  thorpej 		if ((error = suser(p)) != 0)
    179       1.1  thorpej 			return (error);
    180       1.1  thorpej #else
    181  1.11.4.1     yamt 		if ((error = kauth_authorize_generic(p->p_cred,
    182  1.11.4.1     yamt 					       KAUTH_GENERIC_ISSUSER,
    183  1.11.4.1     yamt 					       &p->p_acflag)) != 0)
    184       1.1  thorpej 			return (error);
    185       1.1  thorpej #endif
    186       1.1  thorpej 		break;
    187       1.1  thorpej 	}
    188      1.11    perry 
    189       1.1  thorpej 	switch (cmd) {
    190       1.1  thorpej 
    191       1.1  thorpej 	case BLUE_ENABLE:
    192       1.1  thorpej 		ifacep = (struct blue_interface *)addr;
    193       1.1  thorpej 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    194       1.1  thorpej 			error = EBADF;
    195       1.1  thorpej 			break;
    196       1.1  thorpej 		}
    197       1.1  thorpej 		error = altq_enable(rqp->rq_ifq);
    198       1.1  thorpej 		break;
    199       1.1  thorpej 
    200       1.1  thorpej 	case BLUE_DISABLE:
    201       1.1  thorpej 		ifacep = (struct blue_interface *)addr;
    202       1.1  thorpej 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    203       1.1  thorpej 			error = EBADF;
    204       1.1  thorpej 			break;
    205       1.1  thorpej 		}
    206       1.1  thorpej 		error = altq_disable(rqp->rq_ifq);
    207       1.1  thorpej 		break;
    208       1.1  thorpej 
    209       1.1  thorpej 	case BLUE_IF_ATTACH:
    210       1.1  thorpej 		ifp = ifunit(((struct blue_interface *)addr)->blue_ifname);
    211       1.1  thorpej 		if (ifp == NULL) {
    212       1.1  thorpej 			error = ENXIO;
    213       1.1  thorpej 			break;
    214       1.1  thorpej 		}
    215       1.1  thorpej 
    216       1.1  thorpej 		/* allocate and initialize blue_state_t */
    217  1.11.4.1     yamt 		rqp = malloc(sizeof(blue_queue_t), M_DEVBUF, M_WAITOK|M_ZERO);
    218  1.11.4.1     yamt 		if (rqp == NULL) {
    219  1.11.4.1     yamt 			error = ENOMEM;
    220  1.11.4.1     yamt 			break;
    221  1.11.4.1     yamt 		}
    222       1.1  thorpej 
    223  1.11.4.1     yamt 		rqp->rq_q = malloc(sizeof(class_queue_t), M_DEVBUF,
    224  1.11.4.1     yamt 		    M_WAITOK|M_ZERO);
    225  1.11.4.1     yamt 		if (rqp->rq_q == NULL) {
    226  1.11.4.1     yamt 			free(rqp, M_DEVBUF);
    227  1.11.4.1     yamt 			error = ENOMEM;
    228  1.11.4.1     yamt 			break;
    229  1.11.4.1     yamt 		}
    230       1.1  thorpej 
    231  1.11.4.1     yamt 		rqp->rq_blue = malloc(sizeof(blue_t), M_DEVBUF,
    232  1.11.4.1     yamt 		    M_WAITOK|M_ZERO);
    233  1.11.4.1     yamt 		if (rqp->rq_blue == NULL) {
    234  1.11.4.1     yamt 			free(rqp->rq_q, M_DEVBUF);
    235  1.11.4.1     yamt 			free(rqp, M_DEVBUF);
    236  1.11.4.1     yamt 			error = ENOMEM;
    237  1.11.4.1     yamt 			break;
    238  1.11.4.1     yamt 		}
    239       1.1  thorpej 
    240       1.1  thorpej 		rqp->rq_ifq = &ifp->if_snd;
    241       1.1  thorpej 		qtail(rqp->rq_q) = NULL;
    242       1.1  thorpej 		qlen(rqp->rq_q) = 0;
    243       1.1  thorpej 		qlimit(rqp->rq_q) = BLUE_LIMIT;
    244       1.1  thorpej 
    245       1.1  thorpej 		/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */
    246       1.1  thorpej 		blue_init(rqp->rq_blue, 0, 800, 1000, 50000);
    247       1.1  thorpej 
    248       1.1  thorpej 		/*
    249       1.1  thorpej 		 * set BLUE to this ifnet structure.
    250       1.1  thorpej 		 */
    251       1.1  thorpej 		error = altq_attach(rqp->rq_ifq, ALTQT_BLUE, rqp,
    252       1.1  thorpej 				    blue_enqueue, blue_dequeue, blue_request,
    253       1.1  thorpej 				    NULL, NULL);
    254       1.1  thorpej 		if (error) {
    255  1.11.4.1     yamt 			free(rqp->rq_blue, M_DEVBUF);
    256  1.11.4.1     yamt 			free(rqp->rq_q, M_DEVBUF);
    257  1.11.4.1     yamt 			free(rqp, M_DEVBUF);
    258       1.1  thorpej 			break;
    259       1.1  thorpej 		}
    260       1.1  thorpej 
    261       1.1  thorpej 		/* add this state to the blue list */
    262       1.1  thorpej 		rqp->rq_next = blue_list;
    263       1.1  thorpej 		blue_list = rqp;
    264       1.1  thorpej 		break;
    265       1.1  thorpej 
    266       1.1  thorpej 	case BLUE_IF_DETACH:
    267       1.1  thorpej 		ifacep = (struct blue_interface *)addr;
    268       1.1  thorpej 		if ((rqp = altq_lookup(ifacep->blue_ifname, ALTQT_BLUE)) == NULL) {
    269       1.1  thorpej 			error = EBADF;
    270       1.1  thorpej 			break;
    271       1.1  thorpej 		}
    272       1.1  thorpej 		error = blue_detach(rqp);
    273       1.1  thorpej 		break;
    274       1.1  thorpej 
    275       1.1  thorpej 	case BLUE_GETSTATS:
    276       1.1  thorpej 		do {
    277       1.1  thorpej 			struct blue_stats *q_stats;
    278       1.1  thorpej 			blue_t *rp;
    279       1.1  thorpej 
    280       1.1  thorpej 			q_stats = (struct blue_stats *)addr;
    281       1.1  thorpej 			if ((rqp = altq_lookup(q_stats->iface.blue_ifname,
    282       1.1  thorpej 					     ALTQT_BLUE)) == NULL) {
    283       1.1  thorpej 				error = EBADF;
    284       1.1  thorpej 				break;
    285       1.1  thorpej 			}
    286       1.1  thorpej 
    287       1.1  thorpej 			q_stats->q_len 	   = qlen(rqp->rq_q);
    288       1.1  thorpej 			q_stats->q_limit   = qlimit(rqp->rq_q);
    289       1.1  thorpej 
    290       1.1  thorpej 			rp = rqp->rq_blue;
    291       1.1  thorpej 			q_stats->q_pmark = rp->blue_pmark;
    292       1.1  thorpej 			q_stats->xmit_packets  = rp->blue_stats.xmit_packets;
    293       1.1  thorpej 			q_stats->xmit_bytes    = rp->blue_stats.xmit_bytes;
    294       1.1  thorpej 			q_stats->drop_packets  = rp->blue_stats.drop_packets;
    295       1.1  thorpej 			q_stats->drop_bytes    = rp->blue_stats.drop_bytes;
    296       1.1  thorpej 			q_stats->drop_forced   = rp->blue_stats.drop_forced;
    297       1.1  thorpej 			q_stats->drop_unforced = rp->blue_stats.drop_unforced;
    298       1.1  thorpej 			q_stats->marked_packets = rp->blue_stats.marked_packets;
    299       1.1  thorpej 
    300       1.1  thorpej 		} while (0);
    301       1.1  thorpej 		break;
    302       1.1  thorpej 
    303       1.1  thorpej 	case BLUE_CONFIG:
    304       1.1  thorpej 		do {
    305       1.1  thorpej 			struct blue_conf *fc;
    306       1.1  thorpej 			int limit;
    307       1.1  thorpej 
    308       1.1  thorpej 			fc = (struct blue_conf *)addr;
    309       1.1  thorpej 			if ((rqp = altq_lookup(fc->iface.blue_ifname,
    310       1.1  thorpej 					       ALTQT_BLUE)) == NULL) {
    311       1.1  thorpej 				error = EBADF;
    312       1.1  thorpej 				break;
    313       1.1  thorpej 			}
    314       1.1  thorpej 			limit = fc->blue_limit;
    315       1.1  thorpej 			qlimit(rqp->rq_q) = limit;
    316       1.1  thorpej 			fc->blue_limit = limit;	/* write back the new value */
    317       1.1  thorpej 			if (fc->blue_pkttime > 0)
    318       1.1  thorpej 				rqp->rq_blue->blue_pkttime = fc->blue_pkttime;
    319       1.1  thorpej 			if (fc->blue_max_pmark > 0)
    320       1.1  thorpej 				rqp->rq_blue->blue_max_pmark = fc->blue_max_pmark;
    321       1.1  thorpej 			if (fc->blue_hold_time > 0)
    322       1.1  thorpej 				rqp->rq_blue->blue_hold_time = fc->blue_hold_time;
    323       1.1  thorpej 			rqp->rq_blue->blue_flags = fc->blue_flags;
    324      1.11    perry 
    325       1.1  thorpej 			blue_init(rqp->rq_blue, rqp->rq_blue->blue_flags,
    326       1.1  thorpej 				  rqp->rq_blue->blue_pkttime,
    327       1.1  thorpej 				  rqp->rq_blue->blue_max_pmark,
    328       1.1  thorpej 				  rqp->rq_blue->blue_hold_time);
    329       1.1  thorpej 		} while (0);
    330       1.1  thorpej 		break;
    331       1.1  thorpej 
    332       1.1  thorpej 	default:
    333       1.1  thorpej 		error = EINVAL;
    334       1.1  thorpej 		break;
    335       1.1  thorpej 	}
    336       1.1  thorpej 	return error;
    337       1.1  thorpej }
    338       1.1  thorpej 
    339       1.1  thorpej static int blue_detach(rqp)
    340       1.1  thorpej 	blue_queue_t *rqp;
    341       1.1  thorpej {
    342       1.1  thorpej 	blue_queue_t *tmp;
    343       1.1  thorpej 	int error = 0;
    344       1.1  thorpej 
    345       1.1  thorpej 	if (ALTQ_IS_ENABLED(rqp->rq_ifq))
    346       1.1  thorpej 		altq_disable(rqp->rq_ifq);
    347       1.1  thorpej 
    348       1.1  thorpej 	if ((error = altq_detach(rqp->rq_ifq)))
    349       1.1  thorpej 		return (error);
    350       1.1  thorpej 
    351       1.1  thorpej 	if (blue_list == rqp)
    352       1.1  thorpej 		blue_list = rqp->rq_next;
    353       1.1  thorpej 	else {
    354       1.1  thorpej 		for (tmp = blue_list; tmp != NULL; tmp = tmp->rq_next)
    355       1.1  thorpej 			if (tmp->rq_next == rqp) {
    356       1.1  thorpej 				tmp->rq_next = rqp->rq_next;
    357       1.1  thorpej 				break;
    358       1.1  thorpej 			}
    359       1.1  thorpej 		if (tmp == NULL)
    360       1.1  thorpej 			printf("blue_detach: no state found in blue_list!\n");
    361       1.1  thorpej 	}
    362       1.1  thorpej 
    363  1.11.4.1     yamt 	free(rqp->rq_q, M_DEVBUF);
    364  1.11.4.1     yamt 	free(rqp->rq_blue, M_DEVBUF);
    365  1.11.4.1     yamt 	free(rqp, M_DEVBUF);
    366       1.1  thorpej 	return (error);
    367       1.1  thorpej }
    368       1.1  thorpej 
    369       1.1  thorpej /*
    370       1.1  thorpej  * blue support routines
    371       1.1  thorpej  */
    372       1.1  thorpej 
    373      1.11    perry int
    374       1.1  thorpej blue_init(rp, flags, pkttime, blue_max_pmark, blue_hold_time)
    375       1.1  thorpej 	blue_t 	*rp;
    376       1.1  thorpej 	int	flags;
    377       1.1  thorpej 	int	pkttime;
    378       1.1  thorpej 	int	blue_max_pmark;
    379       1.1  thorpej 	int	blue_hold_time;
    380       1.1  thorpej {
    381       1.1  thorpej 	int npkts_per_sec;
    382      1.11    perry 
    383       1.1  thorpej 	rp->blue_idle = 1;
    384       1.1  thorpej 	rp->blue_flags = flags;
    385       1.1  thorpej 	rp->blue_pkttime = pkttime;
    386       1.1  thorpej 	rp->blue_max_pmark = blue_max_pmark;
    387       1.1  thorpej 	rp->blue_hold_time = blue_hold_time;
    388       1.1  thorpej 	if (pkttime == 0)
    389       1.1  thorpej 		rp->blue_pkttime = 1;
    390       1.1  thorpej 
    391       1.1  thorpej 	/* when the link is very slow, adjust blue parameters */
    392       1.1  thorpej 	npkts_per_sec = 1000000 / rp->blue_pkttime;
    393       1.1  thorpej 	if (npkts_per_sec < 50) {
    394       1.1  thorpej 	}
    395       1.1  thorpej 	else if (npkts_per_sec < 300) {
    396       1.1  thorpej 	}
    397       1.1  thorpej 
    398       1.1  thorpej 	microtime(&rp->blue_last);
    399       1.1  thorpej 	return (0);
    400       1.1  thorpej }
    401       1.1  thorpej 
    402       1.1  thorpej /*
    403       1.1  thorpej  * enqueue routine:
    404       1.1  thorpej  *
    405       1.1  thorpej  *	returns: 0 when successfully queued.
    406       1.1  thorpej  *		 ENOBUFS when drop occurs.
    407       1.1  thorpej  */
    408       1.1  thorpej static int
    409       1.1  thorpej blue_enqueue(ifq, m, pktattr)
    410       1.1  thorpej 	struct ifaltq *ifq;
    411       1.1  thorpej 	struct mbuf *m;
    412       1.1  thorpej 	struct altq_pktattr *pktattr;
    413       1.1  thorpej {
    414       1.1  thorpej 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    415       1.1  thorpej 	int error = 0;
    416       1.1  thorpej 
    417       1.1  thorpej 	if (blue_addq(rqp->rq_blue, rqp->rq_q, m, pktattr) == 0)
    418       1.1  thorpej 		ifq->ifq_len++;
    419       1.1  thorpej 	else
    420       1.1  thorpej 		error = ENOBUFS;
    421       1.1  thorpej 	return error;
    422       1.1  thorpej }
    423       1.1  thorpej 
    424       1.1  thorpej #define	DTYPE_NODROP	0	/* no drop */
    425       1.1  thorpej #define	DTYPE_FORCED	1	/* a "forced" drop */
    426       1.1  thorpej #define	DTYPE_EARLY	2	/* an "unforced" (early) drop */
    427       1.1  thorpej 
    428       1.1  thorpej int
    429       1.1  thorpej blue_addq(rp, q, m, pktattr)
    430       1.1  thorpej 	blue_t *rp;
    431       1.1  thorpej 	class_queue_t *q;
    432       1.1  thorpej 	struct mbuf *m;
    433       1.1  thorpej 	struct altq_pktattr *pktattr;
    434       1.1  thorpej {
    435       1.1  thorpej 	int droptype;
    436      1.11    perry 
    437       1.1  thorpej 	/*
    438       1.1  thorpej 	 * if we were idle, this is an enqueue onto an empty queue
    439       1.1  thorpej 	 * and we should decrement marking probability
    440      1.11    perry 	 *
    441       1.1  thorpej 	 */
    442       1.1  thorpej 	if (rp->blue_idle) {
    443       1.1  thorpej 		struct timeval now;
    444       1.1  thorpej 		int t;
    445       1.1  thorpej 		rp->blue_idle = 0;
    446       1.1  thorpej 		microtime(&now);
    447       1.1  thorpej 		t = (now.tv_sec - rp->blue_last.tv_sec);
    448       1.1  thorpej 		if ( t > 1) {
    449       1.1  thorpej 			rp->blue_pmark = 1;
    450       1.1  thorpej 			microtime(&rp->blue_last);
    451       1.1  thorpej 		} else {
    452       1.1  thorpej 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    453       1.1  thorpej 			if (t > rp->blue_hold_time) {
    454       1.1  thorpej 				rp->blue_pmark--;
    455       1.1  thorpej 				if (rp->blue_pmark < 0) rp->blue_pmark = 0;
    456       1.1  thorpej 				microtime(&rp->blue_last);
    457       1.1  thorpej 			}
    458       1.1  thorpej 		}
    459       1.1  thorpej 	}
    460       1.1  thorpej 
    461       1.1  thorpej 	/* see if we drop early */
    462       1.1  thorpej 	droptype = DTYPE_NODROP;
    463       1.1  thorpej 	if (drop_early(rp) && qlen(q) > 1) {
    464       1.1  thorpej 		/* mark or drop by blue */
    465       1.1  thorpej 		if ((rp->blue_flags & BLUEF_ECN) &&
    466       1.1  thorpej 		    mark_ecn(m, pktattr, rp->blue_flags)) {
    467       1.1  thorpej 			/* successfully marked.  do not drop. */
    468       1.1  thorpej #ifdef BLUE_STATS
    469       1.1  thorpej 			rp->blue_stats.marked_packets++;
    470       1.1  thorpej #endif
    471      1.11    perry 		} else {
    472       1.1  thorpej 			/* unforced drop by blue */
    473       1.1  thorpej 			droptype = DTYPE_EARLY;
    474       1.1  thorpej 		}
    475       1.1  thorpej 	}
    476       1.1  thorpej 
    477       1.1  thorpej 	/*
    478       1.1  thorpej 	 * if the queue length hits the hard limit, it's a forced drop.
    479       1.1  thorpej 	 */
    480       1.1  thorpej 	if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))
    481       1.1  thorpej 		droptype = DTYPE_FORCED;
    482       1.1  thorpej 
    483       1.1  thorpej 	/* if successful or forced drop, enqueue this packet. */
    484       1.1  thorpej 	if (droptype != DTYPE_EARLY)
    485       1.1  thorpej 		_addq(q, m);
    486       1.1  thorpej 
    487       1.1  thorpej 	if (droptype != DTYPE_NODROP) {
    488       1.1  thorpej 		if (droptype == DTYPE_EARLY) {
    489       1.1  thorpej 			/* drop the incoming packet */
    490       1.1  thorpej #ifdef BLUE_STATS
    491       1.1  thorpej 			rp->blue_stats.drop_unforced++;
    492       1.1  thorpej #endif
    493       1.1  thorpej 		} else {
    494       1.1  thorpej 			struct timeval now;
    495       1.1  thorpej 			int t;
    496       1.1  thorpej 			/* forced drop, select a victim packet in the queue. */
    497       1.1  thorpej 			m = _getq_random(q);
    498       1.1  thorpej 			microtime(&now);
    499       1.1  thorpej 			t = (now.tv_sec - rp->blue_last.tv_sec);
    500       1.1  thorpej 			t = t * 1000000 + (now.tv_usec - rp->blue_last.tv_usec);
    501       1.1  thorpej 			if (t > rp->blue_hold_time) {
    502       1.1  thorpej 				rp->blue_pmark += rp->blue_max_pmark >> 3;
    503       1.1  thorpej 				if (rp->blue_pmark > rp->blue_max_pmark)
    504       1.1  thorpej 					rp->blue_pmark = rp->blue_max_pmark;
    505       1.1  thorpej 				microtime(&rp->blue_last);
    506       1.1  thorpej 			}
    507       1.1  thorpej #ifdef BLUE_STATS
    508       1.1  thorpej 			rp->blue_stats.drop_forced++;
    509       1.1  thorpej #endif
    510       1.1  thorpej 		}
    511       1.1  thorpej #ifdef BLUE_STATS
    512       1.1  thorpej 		rp->blue_stats.drop_packets++;
    513       1.1  thorpej 		rp->blue_stats.drop_bytes += m->m_pkthdr.len;
    514       1.1  thorpej #endif
    515       1.1  thorpej 		m_freem(m);
    516       1.1  thorpej 		return (-1);
    517       1.1  thorpej 	}
    518       1.1  thorpej 	/* successfully queued */
    519       1.1  thorpej 	return (0);
    520       1.1  thorpej }
    521       1.1  thorpej 
    522       1.1  thorpej /*
    523       1.1  thorpej  * early-drop probability is kept in blue_pmark
    524       1.1  thorpej  *
    525       1.1  thorpej  */
    526       1.1  thorpej static int
    527       1.1  thorpej drop_early(rp)
    528       1.1  thorpej 	blue_t *rp;
    529       1.1  thorpej {
    530       1.7  mycroft 	if ((random() % rp->blue_max_pmark) < rp->blue_pmark) {
    531       1.1  thorpej 		/* drop or mark */
    532       1.1  thorpej 		return (1);
    533       1.1  thorpej 	}
    534       1.1  thorpej 	/* no drop/mark */
    535       1.1  thorpej 	return (0);
    536       1.1  thorpej }
    537       1.1  thorpej 
    538       1.1  thorpej /*
    539       1.1  thorpej  * try to mark CE bit to the packet.
    540       1.1  thorpej  *    returns 1 if successfully marked, 0 otherwise.
    541       1.1  thorpej  */
    542       1.1  thorpej static int
    543       1.1  thorpej mark_ecn(m, pktattr, flags)
    544       1.1  thorpej 	struct mbuf *m;
    545       1.1  thorpej 	struct altq_pktattr *pktattr;
    546       1.1  thorpej 	int flags;
    547       1.1  thorpej {
    548       1.1  thorpej 	struct mbuf *m0;
    549       1.1  thorpej 
    550       1.1  thorpej 	if (pktattr == NULL ||
    551       1.1  thorpej 	    (pktattr->pattr_af != AF_INET && pktattr->pattr_af != AF_INET6))
    552       1.1  thorpej 		return (0);
    553       1.1  thorpej 
    554       1.1  thorpej 	/* verify that pattr_hdr is within the mbuf data */
    555       1.1  thorpej 	for (m0 = m; m0 != NULL; m0 = m0->m_next)
    556       1.1  thorpej 		if ((pktattr->pattr_hdr >= m0->m_data) &&
    557       1.1  thorpej 		    (pktattr->pattr_hdr < m0->m_data + m0->m_len))
    558       1.1  thorpej 			break;
    559       1.1  thorpej 	if (m0 == NULL) {
    560       1.1  thorpej 		/* ick, pattr_hdr is stale */
    561       1.1  thorpej 		pktattr->pattr_af = AF_UNSPEC;
    562       1.1  thorpej 		return (0);
    563       1.1  thorpej 	}
    564       1.1  thorpej 
    565       1.1  thorpej 	switch (pktattr->pattr_af) {
    566       1.1  thorpej 	case AF_INET:
    567       1.1  thorpej 		if (flags & BLUEF_ECN4) {
    568       1.1  thorpej 			struct ip *ip = (struct ip *)pktattr->pattr_hdr;
    569       1.5   itojun 			u_int8_t otos;
    570       1.5   itojun 			int sum;
    571      1.11    perry 
    572       1.1  thorpej 			if (ip->ip_v != 4)
    573       1.1  thorpej 				return (0);	/* version mismatch! */
    574       1.5   itojun 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT)
    575       1.5   itojun 				return (0);	/* not-ECT */
    576       1.5   itojun 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE)
    577       1.5   itojun 				return (1);	/* already marked */
    578       1.5   itojun 
    579       1.5   itojun 			/*
    580       1.5   itojun 			 * ecn-capable but not marked,
    581       1.5   itojun 			 * mark CE and update checksum
    582       1.5   itojun 			 */
    583       1.5   itojun 			otos = ip->ip_tos;
    584       1.5   itojun 			ip->ip_tos |= IPTOS_ECN_CE;
    585       1.5   itojun 			/*
    586       1.5   itojun 			 * update checksum (from RFC1624)
    587       1.5   itojun 			 *	   HC' = ~(~HC + ~m + m')
    588       1.5   itojun 			 */
    589       1.5   itojun 			sum = ~ntohs(ip->ip_sum) & 0xffff;
    590       1.5   itojun 			sum += (~otos & 0xffff) + ip->ip_tos;
    591       1.5   itojun 			sum = (sum >> 16) + (sum & 0xffff);
    592       1.5   itojun 			sum += (sum >> 16);  /* add carry */
    593       1.5   itojun 			ip->ip_sum = htons(~sum & 0xffff);
    594       1.5   itojun 			return (1);
    595       1.1  thorpej 		}
    596       1.1  thorpej 		break;
    597       1.1  thorpej #ifdef INET6
    598       1.1  thorpej 	case AF_INET6:
    599       1.1  thorpej 		if (flags & BLUEF_ECN6) {
    600       1.1  thorpej 			struct ip6_hdr *ip6 = (struct ip6_hdr *)pktattr->pattr_hdr;
    601       1.1  thorpej 			u_int32_t flowlabel;
    602       1.1  thorpej 
    603       1.1  thorpej 			flowlabel = ntohl(ip6->ip6_flow);
    604       1.1  thorpej 			if ((flowlabel >> 28) != 6)
    605       1.1  thorpej 				return (0);	/* version mismatch! */
    606       1.5   itojun 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    607       1.5   itojun 			    (IPTOS_ECN_NOTECT << 20))
    608       1.5   itojun 				return (0);	/* not-ECT */
    609       1.5   itojun 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
    610       1.5   itojun 			    (IPTOS_ECN_CE << 20))
    611       1.5   itojun 				return (1);	/* already marked */
    612       1.5   itojun 			/*
    613       1.5   itojun 			 * ecn-capable but not marked,  mark CE
    614       1.5   itojun 			 */
    615       1.5   itojun 			flowlabel |= (IPTOS_ECN_CE << 20);
    616       1.5   itojun 			ip6->ip6_flow = htonl(flowlabel);
    617       1.5   itojun 			return (1);
    618       1.1  thorpej 		}
    619       1.1  thorpej 		break;
    620       1.1  thorpej #endif  /* INET6 */
    621       1.1  thorpej 	}
    622       1.1  thorpej 
    623       1.1  thorpej 	/* not marked */
    624       1.1  thorpej 	return (0);
    625       1.1  thorpej }
    626       1.1  thorpej 
    627       1.1  thorpej /*
    628       1.1  thorpej  * dequeue routine:
    629       1.3  thorpej  *	must be called in splnet.
    630       1.1  thorpej  *
    631       1.1  thorpej  *	returns: mbuf dequeued.
    632       1.1  thorpej  *		 NULL when no packet is available in the queue.
    633       1.1  thorpej  */
    634       1.1  thorpej 
    635       1.1  thorpej static struct mbuf *
    636       1.1  thorpej blue_dequeue(ifq, op)
    637       1.1  thorpej 	struct ifaltq *ifq;
    638       1.1  thorpej 	int op;
    639       1.1  thorpej {
    640       1.1  thorpej 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    641       1.1  thorpej 	struct mbuf *m = NULL;
    642       1.1  thorpej 
    643       1.1  thorpej 	if (op == ALTDQ_POLL)
    644       1.1  thorpej 		return (qhead(rqp->rq_q));
    645      1.11    perry 
    646       1.1  thorpej 	m = blue_getq(rqp->rq_blue, rqp->rq_q);
    647       1.1  thorpej 	if (m != NULL)
    648       1.1  thorpej 		ifq->ifq_len--;
    649       1.1  thorpej 	return m;
    650       1.1  thorpej }
    651       1.1  thorpej 
    652       1.1  thorpej struct mbuf *blue_getq(rp, q)
    653       1.1  thorpej 	blue_t *rp;
    654       1.1  thorpej 	class_queue_t *q;
    655       1.1  thorpej {
    656       1.1  thorpej 	struct mbuf *m;
    657      1.11    perry 
    658       1.1  thorpej 	if ((m = _getq(q)) == NULL) {
    659       1.1  thorpej 		if (rp->blue_idle == 0) {
    660       1.1  thorpej 			rp->blue_idle = 1;
    661       1.1  thorpej 			microtime(&rp->blue_last);
    662       1.1  thorpej 		}
    663       1.1  thorpej 		return NULL;
    664       1.1  thorpej 	}
    665       1.1  thorpej 
    666       1.1  thorpej 	rp->blue_idle = 0;
    667       1.1  thorpej #ifdef BLUE_STATS
    668       1.1  thorpej 	rp->blue_stats.xmit_packets++;
    669       1.1  thorpej 	rp->blue_stats.xmit_bytes += m->m_pkthdr.len;
    670       1.1  thorpej #endif
    671       1.1  thorpej 	return (m);
    672       1.1  thorpej }
    673       1.1  thorpej 
    674       1.1  thorpej static int
    675       1.1  thorpej blue_request(ifq, req, arg)
    676       1.1  thorpej 	struct ifaltq *ifq;
    677       1.1  thorpej 	int req;
    678       1.1  thorpej 	void *arg;
    679       1.1  thorpej {
    680       1.1  thorpej 	blue_queue_t *rqp = (blue_queue_t *)ifq->altq_disc;
    681       1.1  thorpej 
    682       1.1  thorpej 	switch (req) {
    683       1.1  thorpej 	case ALTRQ_PURGE:
    684       1.1  thorpej 		_flushq(rqp->rq_q);
    685       1.1  thorpej 		if (ALTQ_IS_ENABLED(ifq))
    686       1.1  thorpej 			ifq->ifq_len = 0;
    687       1.1  thorpej 		break;
    688       1.1  thorpej 	}
    689       1.1  thorpej 	return (0);
    690       1.1  thorpej }
    691       1.1  thorpej 
    692       1.1  thorpej 
    693       1.1  thorpej #ifdef KLD_MODULE
    694       1.1  thorpej 
    695       1.1  thorpej static struct altqsw blue_sw =
    696       1.1  thorpej 	{"blue", blueopen, blueclose, blueioctl};
    697       1.1  thorpej 
    698       1.1  thorpej ALTQ_MODULE(altq_blue, ALTQT_BLUE, &blue_sw);
    699       1.1  thorpej 
    700       1.1  thorpej #endif /* KLD_MODULE */
    701       1.1  thorpej 
    702       1.1  thorpej #endif /* ALTQ_BLUE */
    703