1126385Smlaier/*	$FreeBSD$	*/
2126385Smlaier/*	$KAME: altq_red.h,v 1.8 2003/07/10 12:07:49 kjc Exp $	*/
3126385Smlaier
4126385Smlaier/*
5126385Smlaier * Copyright (C) 1997-2003
6126385Smlaier *	Sony Computer Science Laboratories Inc.  All rights reserved.
7126385Smlaier *
8126385Smlaier * Redistribution and use in source and binary forms, with or without
9126385Smlaier * modification, are permitted provided that the following conditions
10126385Smlaier * are met:
11126385Smlaier * 1. Redistributions of source code must retain the above copyright
12126385Smlaier *    notice, this list of conditions and the following disclaimer.
13126385Smlaier * 2. Redistributions in binary form must reproduce the above copyright
14126385Smlaier *    notice, this list of conditions and the following disclaimer in the
15126385Smlaier *    documentation and/or other materials provided with the distribution.
16126385Smlaier *
17126385Smlaier * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18126385Smlaier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19126385Smlaier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20126385Smlaier * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21126385Smlaier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22126385Smlaier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23126385Smlaier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24126385Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25126385Smlaier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26126385Smlaier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27126385Smlaier * SUCH DAMAGE.
28126385Smlaier */
29126385Smlaier
30126385Smlaier#ifndef _ALTQ_ALTQ_RED_H_
31126385Smlaier#define	_ALTQ_ALTQ_RED_H_
32126385Smlaier
33126385Smlaier#include <altq/altq_classq.h>
34126385Smlaier
35126385Smlaier#ifdef ALTQ3_COMPAT
36126385Smlaierstruct red_interface {
37126385Smlaier	char	red_ifname[IFNAMSIZ];
38126385Smlaier};
39126385Smlaier
40126385Smlaierstruct red_stats {
41126385Smlaier	struct red_interface iface;
42126385Smlaier	int q_len;
43126385Smlaier	int q_avg;
44126385Smlaier
45126385Smlaier	struct pktcntr	xmit_cnt;
46126385Smlaier	struct pktcntr	drop_cnt;
47126385Smlaier	u_int		drop_forced;
48126385Smlaier	u_int		drop_unforced;
49126385Smlaier	u_int		marked_packets;
50126385Smlaier
51126385Smlaier	/* static red parameters */
52126385Smlaier	int q_limit;
53126385Smlaier	int weight;
54126385Smlaier	int inv_pmax;
55126385Smlaier	int th_min;
56126385Smlaier	int th_max;
57126385Smlaier
58126385Smlaier	/* flowvalve related stuff */
59126385Smlaier	u_int fv_flows;
60126385Smlaier	u_int fv_pass;
61126385Smlaier	u_int fv_predrop;
62126385Smlaier	u_int fv_alloc;
63126385Smlaier	u_int fv_escape;
64126385Smlaier};
65126385Smlaier
66126385Smlaierstruct red_conf {
67126385Smlaier	struct red_interface iface;
68126385Smlaier	int red_weight;		/* weight for EWMA */
69126385Smlaier	int red_inv_pmax;	/* inverse of max drop probability */
70126385Smlaier	int red_thmin;		/* red min threshold */
71126385Smlaier	int red_thmax;		/* red max threshold */
72126385Smlaier	int red_limit;		/* max queue length */
73126385Smlaier	int red_pkttime;	/* average packet time in usec */
74126385Smlaier	int red_flags;		/* see below */
75126385Smlaier};
76126385Smlaier#endif /* ALTQ3_COMPAT */
77126385Smlaier
78126385Smlaier/* red flags */
79126385Smlaier#define	REDF_ECN4	0x01	/* use packet marking for IPv4 packets */
80126385Smlaier#define	REDF_ECN6	0x02	/* use packet marking for IPv6 packets */
81126385Smlaier#define	REDF_ECN	(REDF_ECN4 | REDF_ECN6)
82126385Smlaier#define	REDF_FLOWVALVE	0x04	/* use flowvalve (aka penalty-box) */
83126385Smlaier
84126385Smlaier/*
85126385Smlaier * simpler versions of red parameters and statistics used by other
86126385Smlaier * disciplines (e.g., CBQ)
87126385Smlaier */
88126385Smlaierstruct redparams {
89126385Smlaier	int th_min;		/* red min threshold */
90126385Smlaier	int th_max;		/* red max threshold */
91126385Smlaier	int inv_pmax;		/* inverse of max drop probability */
92126385Smlaier};
93126385Smlaier
94126385Smlaierstruct redstats {
95126385Smlaier	int		q_avg;
96126385Smlaier	struct pktcntr	xmit_cnt;
97126385Smlaier	struct pktcntr	drop_cnt;
98126385Smlaier	u_int		drop_forced;
99126385Smlaier	u_int		drop_unforced;
100126385Smlaier	u_int		marked_packets;
101126385Smlaier};
102126385Smlaier
103126385Smlaier#ifdef ALTQ3_COMPAT
104126385Smlaier/*
105126385Smlaier * IOCTLs for RED
106126385Smlaier */
107126385Smlaier#define	RED_IF_ATTACH		_IOW('Q', 1, struct red_interface)
108126385Smlaier#define	RED_IF_DETACH		_IOW('Q', 2, struct red_interface)
109126385Smlaier#define	RED_ENABLE		_IOW('Q', 3, struct red_interface)
110126385Smlaier#define	RED_DISABLE		_IOW('Q', 4, struct red_interface)
111126385Smlaier#define	RED_CONFIG		_IOWR('Q', 6, struct red_conf)
112126385Smlaier#define	RED_GETSTATS		_IOWR('Q', 12, struct red_stats)
113126385Smlaier#define	RED_SETDEFAULTS		_IOW('Q', 30, struct redparams)
114126385Smlaier#endif /* ALTQ3_COMPAT */
115126385Smlaier
116126385Smlaier#ifdef _KERNEL
117126385Smlaier
118126385Smlaier#ifdef ALTQ3_COMPAT
119126385Smlaierstruct flowvalve;
120126385Smlaier#endif
121126385Smlaier
122126385Smlaier/* weight table structure for idle time calibration */
123126385Smlaierstruct wtab {
124126385Smlaier	struct wtab	*w_next;
125126385Smlaier	int		 w_weight;
126126385Smlaier	int		 w_param_max;
127126385Smlaier	int		 w_refcount;
128126385Smlaier	int32_t		 w_tab[32];
129126385Smlaier};
130126385Smlaier
131126385Smlaiertypedef struct red {
132126385Smlaier	int		red_pkttime;	/* average packet time in micro sec
133126385Smlaier					   used for idle calibration */
134126385Smlaier	int		red_flags;	/* red flags */
135126385Smlaier
136126385Smlaier	/* red parameters */
137126385Smlaier	int		red_weight;	/* weight for EWMA */
138126385Smlaier	int		red_inv_pmax;	/* inverse of max drop probability */
139126385Smlaier	int		red_thmin;	/* red min threshold */
140126385Smlaier	int		red_thmax;	/* red max threshold */
141126385Smlaier
142126385Smlaier	/* variables for internal use */
143126385Smlaier	int		red_wshift;	/* log(red_weight) */
144126385Smlaier	int		red_thmin_s;	/* th_min scaled by avgshift */
145126385Smlaier	int		red_thmax_s;	/* th_max scaled by avgshift */
146126385Smlaier	int		red_probd;	/* drop probability denominator */
147126385Smlaier
148126385Smlaier	int		red_avg;	/* queue len avg scaled by avgshift */
149126385Smlaier	int		red_count;	/* packet count since last dropped/
150126385Smlaier					   marked packet */
151126385Smlaier	int		red_idle;	/* queue was empty */
152126385Smlaier	int		red_old;	/* avg is above th_min */
153126385Smlaier	struct wtab	*red_wtab;	/* weight table */
154126385Smlaier	struct timeval	 red_last;	/* time when the queue becomes idle */
155126385Smlaier
156126385Smlaier#ifdef ALTQ3_COMPAT
157126385Smlaier	struct flowvalve *red_flowvalve;	/* flowvalve state */
158126385Smlaier#endif
159126385Smlaier
160126385Smlaier	struct {
161126385Smlaier		struct pktcntr	xmit_cnt;
162126385Smlaier		struct pktcntr	drop_cnt;
163126385Smlaier		u_int		drop_forced;
164126385Smlaier		u_int		drop_unforced;
165126385Smlaier		u_int		marked_packets;
166126385Smlaier	} red_stats;
167126385Smlaier} red_t;
168126385Smlaier
169126385Smlaier#ifdef ALTQ3_COMPAT
170126385Smlaiertypedef struct red_queue {
171126385Smlaier	struct red_queue *rq_next;	/* next red_state in the list */
172126385Smlaier	struct ifaltq *rq_ifq;		/* backpointer to ifaltq */
173126385Smlaier
174126385Smlaier	class_queue_t *rq_q;
175126385Smlaier
176126385Smlaier	red_t *rq_red;
177126385Smlaier} red_queue_t;
178126385Smlaier#endif /* ALTQ3_COMPAT */
179126385Smlaier
180126385Smlaier/* red drop types */
181126385Smlaier#define	DTYPE_NODROP	0	/* no drop */
182126385Smlaier#define	DTYPE_FORCED	1	/* a "forced" drop */
183126385Smlaier#define	DTYPE_EARLY	2	/* an "unforced" (early) drop */
184126385Smlaier
185126385Smlaierextern red_t		*red_alloc(int, int, int, int, int, int);
186126385Smlaierextern void		 red_destroy(red_t *);
187126385Smlaierextern void		 red_getstats(red_t *, struct redstats *);
188126385Smlaierextern int		 red_addq(red_t *, class_queue_t *, struct mbuf *,
189126385Smlaier			     struct altq_pktattr *);
190126385Smlaierextern struct mbuf	*red_getq(red_t *, class_queue_t *);
191126385Smlaierextern int		 drop_early(int, int, int);
192126385Smlaierextern int		 mark_ecn(struct mbuf *, struct altq_pktattr *, int);
193126385Smlaierextern struct wtab	*wtab_alloc(int);
194126385Smlaierextern int		 wtab_destroy(struct wtab *);
195126385Smlaierextern int32_t		 pow_w(struct wtab *, int);
196126385Smlaier
197126385Smlaier#endif /* _KERNEL */
198126385Smlaier
199126385Smlaier#endif /* _ALTQ_ALTQ_RED_H_ */
200