xref: /freebsd/sys/net/altq/altq_hfsc.h (revision 325151a3)
1 /*-
2  * Copyright (c) 1997-1999 Carnegie Mellon University. All Rights Reserved.
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation is hereby granted (including for commercial or
6  * for-profit use), provided that both the copyright notice and this
7  * permission notice appear in all copies of the software, derivative
8  * works, or modified versions, and any portions thereof.
9  *
10  * THIS SOFTWARE IS EXPERIMENTAL AND IS KNOWN TO HAVE BUGS, SOME OF
11  * WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON PROVIDES THIS
12  * SOFTWARE IN ITS ``AS IS'' CONDITION, AND ANY EXPRESS OR IMPLIED
13  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
16  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  *
25  * Carnegie Mellon encourages (but does not require) users of this
26  * software to return any improvements or extensions that they make,
27  * and to grant Carnegie Mellon the rights to redistribute these
28  * changes without encumbrance.
29  *
30  * $KAME: altq_hfsc.h,v 1.12 2003/12/05 05:40:46 kjc Exp $
31  * $FreeBSD$
32  */
33 #ifndef _ALTQ_ALTQ_HFSC_H_
34 #define	_ALTQ_ALTQ_HFSC_H_
35 
36 #include <net/altq/altq.h>
37 #include <net/altq/altq_classq.h>
38 #include <net/altq/altq_codel.h>
39 #include <net/altq/altq_red.h>
40 #include <net/altq/altq_rio.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 struct service_curve {
47 	u_int	m1;	/* slope of the first segment in bits/sec */
48 	u_int	d;	/* the x-projection of the first segment in msec */
49 	u_int	m2;	/* slope of the second segment in bits/sec */
50 };
51 
52 /* special class handles */
53 #define	HFSC_NULLCLASS_HANDLE	0
54 #define	HFSC_MAX_CLASSES	64
55 
56 /* hfsc class flags */
57 #define	HFCF_RED		0x0001	/* use RED */
58 #define	HFCF_ECN		0x0002  /* use RED/ECN */
59 #define	HFCF_RIO		0x0004  /* use RIO */
60 #define	HFCF_CODEL		0x0008	/* use CoDel */
61 #define	HFCF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
62 #define	HFCF_DEFAULTCLASS	0x1000	/* default class */
63 
64 /* service curve types */
65 #define	HFSC_REALTIMESC		1
66 #define	HFSC_LINKSHARINGSC	2
67 #define	HFSC_UPPERLIMITSC	4
68 #define	HFSC_DEFAULTSC		(HFSC_REALTIMESC|HFSC_LINKSHARINGSC)
69 
70 struct hfsc_classstats {
71 	u_int			class_id;
72 	u_int32_t		class_handle;
73 	struct service_curve	rsc;
74 	struct service_curve	fsc;
75 	struct service_curve	usc;	/* upper limit service curve */
76 
77 	u_int64_t		total;	/* total work in bytes */
78 	u_int64_t		cumul;	/* cumulative work in bytes
79 					   done by real-time criteria */
80 	u_int64_t		d;		/* deadline */
81 	u_int64_t		e;		/* eligible time */
82 	u_int64_t		vt;		/* virtual time */
83 	u_int64_t		f;		/* fit time for upper-limit */
84 
85 	/* info helpful for debugging */
86 	u_int64_t		initvt;		/* init virtual time */
87 	u_int64_t		vtoff;		/* cl_vt_ipoff */
88 	u_int64_t		cvtmax;		/* cl_maxvt */
89 	u_int64_t		myf;		/* cl_myf */
90 	u_int64_t		cfmin;		/* cl_mincf */
91 	u_int64_t		cvtmin;		/* cl_mincvt */
92 	u_int64_t		myfadj;		/* cl_myfadj */
93 	u_int64_t		vtadj;		/* cl_vtadj */
94 	u_int64_t		cur_time;
95 	u_int32_t		machclk_freq;
96 
97 	u_int			qlength;
98 	u_int			qlimit;
99 	struct pktcntr		xmit_cnt;
100 	struct pktcntr		drop_cnt;
101 	u_int			period;
102 
103 	u_int			vtperiod;	/* vt period sequence no */
104 	u_int			parentperiod;	/* parent's vt period seqno */
105 	int			nactive;	/* number of active children */
106 
107 	/* codel, red and rio related info */
108 	int			qtype;
109 	struct redstats		red[3];
110 	struct codel_stats	codel;
111 };
112 
113 #ifdef ALTQ3_COMPAT
114 struct hfsc_interface {
115 	char	hfsc_ifname[IFNAMSIZ];  /* interface name (e.g., fxp0) */
116 };
117 
118 struct hfsc_attach {
119 	struct hfsc_interface	iface;
120 	u_int			bandwidth;  /* link bandwidth in bits/sec */
121 };
122 
123 struct hfsc_add_class {
124 	struct hfsc_interface	iface;
125 	u_int32_t		parent_handle;
126 	struct service_curve	service_curve;
127 	int			qlimit;
128 	int			flags;
129 
130 	u_int32_t		class_handle;  /* return value */
131 };
132 
133 struct hfsc_delete_class {
134 	struct hfsc_interface	iface;
135 	u_int32_t		class_handle;
136 };
137 
138 struct hfsc_modify_class {
139 	struct hfsc_interface	iface;
140 	u_int32_t		class_handle;
141 	struct service_curve	service_curve;
142 	int			sctype;
143 };
144 
145 struct hfsc_add_filter {
146 	struct hfsc_interface	iface;
147 	u_int32_t		class_handle;
148 	struct flow_filter	filter;
149 
150 	u_long			filter_handle;  /* return value */
151 };
152 
153 struct hfsc_delete_filter {
154 	struct hfsc_interface	iface;
155 	u_long			filter_handle;
156 };
157 
158 struct hfsc_class_stats {
159 	struct hfsc_interface	iface;
160 	int			nskip;		/* skip # of classes */
161 	int			nclasses;	/* # of class stats (WR) */
162 	u_int64_t		cur_time;	/* current time */
163 	u_int32_t		machclk_freq;	/* machine clock frequency */
164 	u_int			hif_classes;	/* # of classes in the tree */
165 	u_int			hif_packets;	/* # of packets in the tree */
166 	struct hfsc_classstats	*stats;		/* pointer to stats array */
167 };
168 
169 #define	HFSC_IF_ATTACH		_IOW('Q', 1, struct hfsc_attach)
170 #define	HFSC_IF_DETACH		_IOW('Q', 2, struct hfsc_interface)
171 #define	HFSC_ENABLE		_IOW('Q', 3, struct hfsc_interface)
172 #define	HFSC_DISABLE		_IOW('Q', 4, struct hfsc_interface)
173 #define	HFSC_CLEAR_HIERARCHY	_IOW('Q', 5, struct hfsc_interface)
174 #define	HFSC_ADD_CLASS		_IOWR('Q', 7, struct hfsc_add_class)
175 #define	HFSC_DEL_CLASS		_IOW('Q', 8, struct hfsc_delete_class)
176 #define	HFSC_MOD_CLASS		_IOW('Q', 9, struct hfsc_modify_class)
177 #define	HFSC_ADD_FILTER		_IOWR('Q', 10, struct hfsc_add_filter)
178 #define	HFSC_DEL_FILTER		_IOW('Q', 11, struct hfsc_delete_filter)
179 #define	HFSC_GETSTATS		_IOWR('Q', 12, struct hfsc_class_stats)
180 #endif /* ALTQ3_COMPAT */
181 
182 #ifdef _KERNEL
183 /*
184  * kernel internal service curve representation
185  *	coordinates are given by 64 bit unsigned integers.
186  *	x-axis: unit is clock count.  for the intel x86 architecture,
187  *		the raw Pentium TSC (Timestamp Counter) value is used.
188  *		virtual time is also calculated in this time scale.
189  *	y-axis: unit is byte.
190  *
191  *	the service curve parameters are converted to the internal
192  *	representation.
193  *	the slope values are scaled to avoid overflow.
194  *	the inverse slope values as well as the y-projection of the 1st
195  *	segment are kept in order to to avoid 64-bit divide operations
196  *	that are expensive on 32-bit architectures.
197  *
198  *  note: Intel Pentium TSC never wraps around in several thousands of years.
199  *	x-axis doesn't wrap around for 1089 years with 1GHz clock.
200  *      y-axis doesn't wrap around for 4358 years with 1Gbps bandwidth.
201  */
202 
203 /* kernel internal representation of a service curve */
204 struct internal_sc {
205 	u_int64_t	sm1;	/* scaled slope of the 1st segment */
206 	u_int64_t	ism1;	/* scaled inverse-slope of the 1st segment */
207 	u_int64_t	dx;	/* the x-projection of the 1st segment */
208 	u_int64_t	dy;	/* the y-projection of the 1st segment */
209 	u_int64_t	sm2;	/* scaled slope of the 2nd segment */
210 	u_int64_t	ism2;	/* scaled inverse-slope of the 2nd segment */
211 };
212 
213 /* runtime service curve */
214 struct runtime_sc {
215 	u_int64_t	x;	/* current starting position on x-axis */
216 	u_int64_t	y;	/* current starting position on x-axis */
217 	u_int64_t	sm1;	/* scaled slope of the 1st segment */
218 	u_int64_t	ism1;	/* scaled inverse-slope of the 1st segment */
219 	u_int64_t	dx;	/* the x-projection of the 1st segment */
220 	u_int64_t	dy;	/* the y-projection of the 1st segment */
221 	u_int64_t	sm2;	/* scaled slope of the 2nd segment */
222 	u_int64_t	ism2;	/* scaled inverse-slope of the 2nd segment */
223 };
224 
225 struct hfsc_class {
226 	u_int		cl_id;		/* class id (just for debug) */
227 	u_int32_t	cl_handle;	/* class handle */
228 	struct hfsc_if	*cl_hif;	/* back pointer to struct hfsc_if */
229 	int		cl_flags;	/* misc flags */
230 
231 	struct hfsc_class *cl_parent;	/* parent class */
232 	struct hfsc_class *cl_siblings;	/* sibling classes */
233 	struct hfsc_class *cl_children;	/* child classes */
234 
235 	class_queue_t	*cl_q;		/* class queue structure */
236 	union {
237 		struct red	*cl_red;	/* RED state */
238 		struct codel	*cl_codel;	/* CoDel state */
239 	} cl_aqm;
240 #define	cl_red			cl_aqm.cl_red
241 #define	cl_codel		cl_aqm.cl_codel
242 	struct altq_pktattr *cl_pktattr; /* saved header used by ECN */
243 
244 	u_int64_t	cl_total;	/* total work in bytes */
245 	u_int64_t	cl_cumul;	/* cumulative work in bytes
246 					   done by real-time criteria */
247 	u_int64_t	cl_d;		/* deadline */
248 	u_int64_t	cl_e;		/* eligible time */
249 	u_int64_t	cl_vt;		/* virtual time */
250 	u_int64_t	cl_f;		/* time when this class will fit for
251 					   link-sharing, max(myf, cfmin) */
252 	u_int64_t	cl_myf;		/* my fit-time (as calculated from this
253 					   class's own upperlimit curve) */
254 	u_int64_t	cl_myfadj;	/* my fit-time adjustment
255 					   (to cancel history dependence) */
256 	u_int64_t	cl_cfmin;	/* earliest children's fit-time (used
257 					   with cl_myf to obtain cl_f) */
258 	u_int64_t	cl_cvtmin;	/* minimal virtual time among the
259 					   children fit for link-sharing
260 					   (monotonic within a period) */
261 	u_int64_t	cl_vtadj;	/* intra-period cumulative vt
262 					   adjustment */
263 	u_int64_t	cl_vtoff;	/* inter-period cumulative vt offset */
264 	u_int64_t	cl_cvtmax;	/* max child's vt in the last period */
265 
266 	u_int64_t	cl_initvt;	/* init virtual time (for debugging) */
267 
268 	struct internal_sc *cl_rsc;	/* internal real-time service curve */
269 	struct internal_sc *cl_fsc;	/* internal fair service curve */
270 	struct internal_sc *cl_usc;	/* internal upperlimit service curve */
271 	struct runtime_sc  cl_deadline;	/* deadline curve */
272 	struct runtime_sc  cl_eligible;	/* eligible curve */
273 	struct runtime_sc  cl_virtual;	/* virtual curve */
274 	struct runtime_sc  cl_ulimit;	/* upperlimit curve */
275 
276 	u_int		cl_vtperiod;	/* vt period sequence no */
277 	u_int		cl_parentperiod;  /* parent's vt period seqno */
278 	int		cl_nactive;	/* number of active children */
279 
280 	TAILQ_HEAD(acthead, hfsc_class) cl_actc; /* active children list */
281 	TAILQ_ENTRY(hfsc_class)	cl_actlist;	/* active children list entry */
282 	TAILQ_ENTRY(hfsc_class)	cl_ellist;	/* eligible list entry */
283 
284 	struct {
285 		struct pktcntr	xmit_cnt;
286 		struct pktcntr	drop_cnt;
287 		u_int period;
288 	} cl_stats;
289 };
290 
291 /*
292  * hfsc interface state
293  */
294 struct hfsc_if {
295 	struct hfsc_if		*hif_next;	/* interface state list */
296 	struct ifaltq		*hif_ifq;	/* backpointer to ifaltq */
297 	struct hfsc_class	*hif_rootclass;		/* root class */
298 	struct hfsc_class	*hif_defaultclass;	/* default class */
299 	struct hfsc_class	*hif_class_tbl[HFSC_MAX_CLASSES];
300 	struct hfsc_class	*hif_pollcache;	/* cache for poll operation */
301 
302 	u_int	hif_classes;			/* # of classes in the tree */
303 	u_int	hif_packets;			/* # of packets in the tree */
304 	u_int	hif_classid;			/* class id sequence number */
305 
306 	TAILQ_HEAD(elighead, hfsc_class) hif_eligible; /* eligible list */
307 
308 #ifdef ALTQ3_CLFIER_COMPAT
309 	struct acc_classifier	hif_classifier;
310 #endif
311 };
312 
313 #endif /* _KERNEL */
314 
315 #ifdef __cplusplus
316 }
317 #endif
318 
319 #endif /* _ALTQ_ALTQ_HFSC_H_ */
320