1/*	$KAME: altq_red.h,v 1.8 2003/07/10 12:07:49 kjc Exp $	*/
2
3/*
4 * Copyright (C) 1997-2003
5 *	Sony Computer Science Laboratories Inc.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _ALTQ_ALTQ_RED_H_
30#define	_ALTQ_ALTQ_RED_H_
31
32#include <altq/altq_classq.h>
33
34#ifdef ALTQ3_COMPAT
35struct red_interface {
36	char	red_ifname[IFNAMSIZ];
37};
38
39struct red_stats {
40	struct red_interface iface;
41	int q_len;
42	int q_avg;
43
44	struct pktcntr	xmit_cnt;
45	struct pktcntr	drop_cnt;
46	u_int		drop_forced;
47	u_int		drop_unforced;
48	u_int		marked_packets;
49
50	/* static red parameters */
51	int q_limit;
52	int weight;
53	int inv_pmax;
54	int th_min;
55	int th_max;
56
57	/* flowvalve related stuff */
58	u_int fv_flows;
59	u_int fv_pass;
60	u_int fv_predrop;
61	u_int fv_alloc;
62	u_int fv_escape;
63};
64
65struct red_conf {
66	struct red_interface iface;
67	int red_weight;		/* weight for EWMA */
68	int red_inv_pmax;	/* inverse of max drop probability */
69	int red_thmin;		/* red min threshold */
70	int red_thmax;		/* red max threshold */
71	int red_limit;		/* max queue length */
72	int red_pkttime;	/* average packet time in usec */
73	int red_flags;		/* see below */
74};
75#endif /* ALTQ3_COMPAT */
76
77/* red flags */
78#define	REDF_ECN4	0x01	/* use packet marking for IPv4 packets */
79#define	REDF_ECN6	0x02	/* use packet marking for IPv6 packets */
80#define	REDF_ECN	(REDF_ECN4 | REDF_ECN6)
81#define	REDF_FLOWVALVE	0x04	/* use flowvalve (aka penalty-box) */
82
83/*
84 * simpler versions of red parameters and statistics used by other
85 * disciplines (e.g., CBQ)
86 */
87struct redparams {
88	int th_min;		/* red min threshold */
89	int th_max;		/* red max threshold */
90	int inv_pmax;		/* inverse of max drop probability */
91};
92
93struct redstats {
94	int		q_avg;
95	struct pktcntr	xmit_cnt;
96	struct pktcntr	drop_cnt;
97	u_int		drop_forced;
98	u_int		drop_unforced;
99	u_int		marked_packets;
100};
101
102#ifdef ALTQ3_COMPAT
103/*
104 * IOCTLs for RED
105 */
106#define	RED_IF_ATTACH		_IOW('Q', 1, struct red_interface)
107#define	RED_IF_DETACH		_IOW('Q', 2, struct red_interface)
108#define	RED_ENABLE		_IOW('Q', 3, struct red_interface)
109#define	RED_DISABLE		_IOW('Q', 4, struct red_interface)
110#define	RED_CONFIG		_IOWR('Q', 6, struct red_conf)
111#define	RED_GETSTATS		_IOWR('Q', 12, struct red_stats)
112#define	RED_SETDEFAULTS		_IOW('Q', 30, struct redparams)
113#endif /* ALTQ3_COMPAT */
114
115#ifdef _KERNEL
116
117#ifdef ALTQ3_COMPAT
118struct flowvalve;
119#endif
120
121/* weight table structure for idle time calibration */
122struct wtab {
123	struct wtab	*w_next;
124	int		 w_weight;
125	int		 w_param_max;
126	int		 w_refcount;
127	int32_t		 w_tab[32];
128};
129
130typedef struct red {
131	int		red_pkttime;	/* average packet time in micro sec
132					   used for idle calibration */
133	int		red_flags;	/* red flags */
134
135	/* red parameters */
136	int		red_weight;	/* weight for EWMA */
137	int		red_inv_pmax;	/* inverse of max drop probability */
138	int		red_thmin;	/* red min threshold */
139	int		red_thmax;	/* red max threshold */
140
141	/* variables for internal use */
142	int		red_wshift;	/* log(red_weight) */
143	int		red_thmin_s;	/* th_min scaled by avgshift */
144	int		red_thmax_s;	/* th_max scaled by avgshift */
145	int		red_probd;	/* drop probability denominator */
146
147	int		red_avg;	/* queue len avg scaled by avgshift */
148	int		red_count;	/* packet count since last dropped/
149					   marked packet */
150	int		red_idle;	/* queue was empty */
151	int		red_old;	/* avg is above th_min */
152	struct wtab	*red_wtab;	/* weight table */
153	struct timeval	 red_last;	/* time when the queue becomes idle */
154
155#ifdef ALTQ3_COMPAT
156	struct flowvalve *red_flowvalve;	/* flowvalve state */
157#endif
158
159	struct {
160		struct pktcntr	xmit_cnt;
161		struct pktcntr	drop_cnt;
162		u_int		drop_forced;
163		u_int		drop_unforced;
164		u_int		marked_packets;
165	} red_stats;
166} red_t;
167
168#ifdef ALTQ3_COMPAT
169typedef struct red_queue {
170	struct red_queue *rq_next;	/* next red_state in the list */
171	struct ifaltq *rq_ifq;		/* backpointer to ifaltq */
172
173	class_queue_t *rq_q;
174
175	red_t *rq_red;
176} red_queue_t;
177#endif /* ALTQ3_COMPAT */
178
179/* red drop types */
180#define	DTYPE_NODROP	0	/* no drop */
181#define	DTYPE_FORCED	1	/* a "forced" drop */
182#define	DTYPE_EARLY	2	/* an "unforced" (early) drop */
183
184extern red_t		*red_alloc(int, int, int, int, int, int);
185extern void		 red_destroy(red_t *);
186extern void		 red_getstats(red_t *, struct redstats *);
187extern int		 red_addq(red_t *, class_queue_t *, struct mbuf *,
188			     struct altq_pktattr *);
189extern struct mbuf	*red_getq(red_t *, class_queue_t *);
190extern int		 drop_early(int, int, int);
191extern int		 mark_ecn(struct mbuf *, struct altq_pktattr *, int);
192extern struct wtab	*wtab_alloc(int);
193extern int		 wtab_destroy(struct wtab *);
194extern int32_t		 pow_w(struct wtab *, int);
195
196#endif /* _KERNEL */
197
198#endif /* _ALTQ_ALTQ_RED_H_ */
199