xref: /freebsd/sys/net/altq/altq_hfsc.h (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
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_v0 {
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 struct service_curve_v1 {
53 	u_int64_t	m1;   /* slope of the first segment in bits/sec */
54 	u_int	d;	      /* the x-projection of the first segment in msec */
55 	u_int64_t	m2;   /* slope of the second segment in bits/sec */
56 };
57 
58 /* Latest version of struct service_curve_vX */
59 #define HFSC_SERVICE_CURVE_VERSION	1
60 
61 /* special class handles */
62 #define	HFSC_NULLCLASS_HANDLE	0
63 #define	HFSC_MAX_CLASSES	64
64 
65 /* hfsc class flags */
66 #define	HFCF_RED		0x0001	/* use RED */
67 #define	HFCF_ECN		0x0002  /* use RED/ECN */
68 #define	HFCF_RIO		0x0004  /* use RIO */
69 #define	HFCF_CODEL		0x0008	/* use CoDel */
70 #define	HFCF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
71 #define	HFCF_DEFAULTCLASS	0x1000	/* default class */
72 
73 /* service curve types */
74 #define	HFSC_REALTIMESC		1
75 #define	HFSC_LINKSHARINGSC	2
76 #define	HFSC_UPPERLIMITSC	4
77 #define	HFSC_DEFAULTSC		(HFSC_REALTIMESC|HFSC_LINKSHARINGSC)
78 
79 struct hfsc_classstats_v0 {
80 	u_int			class_id;
81 	u_int32_t		class_handle;
82 	struct service_curve_v0	rsc;
83 	struct service_curve_v0	fsc;
84 	struct service_curve_v0	usc;	/* upper limit service curve */
85 
86 	u_int64_t		total;	/* total work in bytes */
87 	u_int64_t		cumul;	/* cumulative work in bytes
88 					   done by real-time criteria */
89 	u_int64_t		d;		/* deadline */
90 	u_int64_t		e;		/* eligible time */
91 	u_int64_t		vt;		/* virtual time */
92 	u_int64_t		f;		/* fit time for upper-limit */
93 
94 	/* info helpful for debugging */
95 	u_int64_t		initvt;		/* init virtual time */
96 	u_int64_t		vtoff;		/* cl_vt_ipoff */
97 	u_int64_t		cvtmax;		/* cl_maxvt */
98 	u_int64_t		myf;		/* cl_myf */
99 	u_int64_t		cfmin;		/* cl_mincf */
100 	u_int64_t		cvtmin;		/* cl_mincvt */
101 	u_int64_t		myfadj;		/* cl_myfadj */
102 	u_int64_t		vtadj;		/* cl_vtadj */
103 	u_int64_t		cur_time;
104 	u_int32_t		machclk_freq;
105 
106 	u_int			qlength;
107 	u_int			qlimit;
108 	struct pktcntr		xmit_cnt;
109 	struct pktcntr		drop_cnt;
110 	u_int			period;
111 
112 	u_int			vtperiod;	/* vt period sequence no */
113 	u_int			parentperiod;	/* parent's vt period seqno */
114 	int			nactive;	/* number of active children */
115 
116 	/* codel, red and rio related info */
117 	int			qtype;
118 	struct redstats		red[3];
119 	struct codel_stats	codel;
120 };
121 
122 struct hfsc_classstats_v1 {
123 	u_int			class_id;
124 	u_int32_t		class_handle;
125 	struct service_curve_v1	rsc;
126 	struct service_curve_v1	fsc;
127 	struct service_curve_v1	usc;	/* upper limit service curve */
128 
129 	u_int64_t		total;	/* total work in bytes */
130 	u_int64_t		cumul;	/* cumulative work in bytes
131 					   done by real-time criteria */
132 	u_int64_t		d;		/* deadline */
133 	u_int64_t		e;		/* eligible time */
134 	u_int64_t		vt;		/* virtual time */
135 	u_int64_t		f;		/* fit time for upper-limit */
136 
137 	/* info helpful for debugging */
138 	u_int64_t		initvt;		/* init virtual time */
139 	u_int64_t		vtoff;		/* cl_vt_ipoff */
140 	u_int64_t		cvtmax;		/* cl_maxvt */
141 	u_int64_t		myf;		/* cl_myf */
142 	u_int64_t		cfmin;		/* cl_mincf */
143 	u_int64_t		cvtmin;		/* cl_mincvt */
144 	u_int64_t		myfadj;		/* cl_myfadj */
145 	u_int64_t		vtadj;		/* cl_vtadj */
146 	u_int64_t		cur_time;
147 	u_int32_t		machclk_freq;
148 
149 	u_int			qlength;
150 	u_int			qlimit;
151 	struct pktcntr		xmit_cnt;
152 	struct pktcntr		drop_cnt;
153 	u_int			period;
154 
155 	u_int			vtperiod;	/* vt period sequence no */
156 	u_int			parentperiod;	/* parent's vt period seqno */
157 	int			nactive;	/* number of active children */
158 
159 	/* codel, red and rio related info */
160 	int			qtype;
161 	struct redstats		red[3];
162 	struct codel_stats	codel;
163 };
164 
165 /*
166  * HFSC_STATS_VERSION is defined in altq.h to work around issues stemming
167  * from mixing of public-API and internal bits in each scheduler-specific
168  * header.
169  */
170 
171 #ifdef ALTQ3_COMPAT
172 struct hfsc_interface {
173 	char	hfsc_ifname[IFNAMSIZ];  /* interface name (e.g., fxp0) */
174 };
175 
176 struct hfsc_attach {
177 	struct hfsc_interface	iface;
178 	u_int			bandwidth;  /* link bandwidth in bits/sec */
179 };
180 
181 struct hfsc_add_class {
182 	struct hfsc_interface	iface;
183 	u_int32_t		parent_handle;
184 	struct service_curve	service_curve;
185 	int			qlimit;
186 	int			flags;
187 
188 	u_int32_t		class_handle;  /* return value */
189 };
190 
191 struct hfsc_delete_class {
192 	struct hfsc_interface	iface;
193 	u_int32_t		class_handle;
194 };
195 
196 struct hfsc_modify_class {
197 	struct hfsc_interface	iface;
198 	u_int32_t		class_handle;
199 	struct service_curve	service_curve;
200 	int			sctype;
201 };
202 
203 struct hfsc_add_filter {
204 	struct hfsc_interface	iface;
205 	u_int32_t		class_handle;
206 	struct flow_filter	filter;
207 
208 	u_long			filter_handle;  /* return value */
209 };
210 
211 struct hfsc_delete_filter {
212 	struct hfsc_interface	iface;
213 	u_long			filter_handle;
214 };
215 
216 struct hfsc_class_stats {
217 	struct hfsc_interface	iface;
218 	int			nskip;		/* skip # of classes */
219 	int			nclasses;	/* # of class stats (WR) */
220 	u_int64_t		cur_time;	/* current time */
221 	u_int32_t		machclk_freq;	/* machine clock frequency */
222 	u_int			hif_classes;	/* # of classes in the tree */
223 	u_int			hif_packets;	/* # of packets in the tree */
224 	struct hfsc_classstats	*stats;		/* pointer to stats array */
225 };
226 
227 #define	HFSC_IF_ATTACH		_IOW('Q', 1, struct hfsc_attach)
228 #define	HFSC_IF_DETACH		_IOW('Q', 2, struct hfsc_interface)
229 #define	HFSC_ENABLE		_IOW('Q', 3, struct hfsc_interface)
230 #define	HFSC_DISABLE		_IOW('Q', 4, struct hfsc_interface)
231 #define	HFSC_CLEAR_HIERARCHY	_IOW('Q', 5, struct hfsc_interface)
232 #define	HFSC_ADD_CLASS		_IOWR('Q', 7, struct hfsc_add_class)
233 #define	HFSC_DEL_CLASS		_IOW('Q', 8, struct hfsc_delete_class)
234 #define	HFSC_MOD_CLASS		_IOW('Q', 9, struct hfsc_modify_class)
235 #define	HFSC_ADD_FILTER		_IOWR('Q', 10, struct hfsc_add_filter)
236 #define	HFSC_DEL_FILTER		_IOW('Q', 11, struct hfsc_delete_filter)
237 #define	HFSC_GETSTATS		_IOWR('Q', 12, struct hfsc_class_stats)
238 #endif /* ALTQ3_COMPAT */
239 
240 #ifdef _KERNEL
241 /*
242  * kernel internal service curve representation
243  *	coordinates are given by 64 bit unsigned integers.
244  *	x-axis: unit is clock count.  for the intel x86 architecture,
245  *		the raw Pentium TSC (Timestamp Counter) value is used.
246  *		virtual time is also calculated in this time scale.
247  *	y-axis: unit is byte.
248  *
249  *	the service curve parameters are converted to the internal
250  *	representation.
251  *	the slope values are scaled to avoid overflow.
252  *	the inverse slope values as well as the y-projection of the 1st
253  *	segment are kept in order to avoid 64-bit divide operations
254  *	that are expensive on 32-bit architectures.
255  *
256  *  note: Intel Pentium TSC never wraps around in several thousands of years.
257  *	x-axis doesn't wrap around for 1089 years with 1GHz clock.
258  *      y-axis doesn't wrap around for 4358 years with 1Gbps bandwidth.
259  */
260 
261 /* kernel internal representation of a service curve */
262 struct internal_sc {
263 	u_int64_t	sm1;	/* scaled slope of the 1st segment */
264 	u_int64_t	ism1;	/* scaled inverse-slope of the 1st segment */
265 	u_int64_t	dx;	/* the x-projection of the 1st segment */
266 	u_int64_t	dy;	/* the y-projection of the 1st segment */
267 	u_int64_t	sm2;	/* scaled slope of the 2nd segment */
268 	u_int64_t	ism2;	/* scaled inverse-slope of the 2nd segment */
269 };
270 
271 /* runtime service curve */
272 struct runtime_sc {
273 	u_int64_t	x;	/* current starting position on x-axis */
274 	u_int64_t	y;	/* current starting position on x-axis */
275 	u_int64_t	sm1;	/* scaled slope of the 1st segment */
276 	u_int64_t	ism1;	/* scaled inverse-slope of the 1st segment */
277 	u_int64_t	dx;	/* the x-projection of the 1st segment */
278 	u_int64_t	dy;	/* the y-projection of the 1st segment */
279 	u_int64_t	sm2;	/* scaled slope of the 2nd segment */
280 	u_int64_t	ism2;	/* scaled inverse-slope of the 2nd segment */
281 };
282 
283 struct hfsc_class {
284 	u_int		cl_id;		/* class id (just for debug) */
285 	u_int32_t	cl_handle;	/* class handle */
286 	struct hfsc_if	*cl_hif;	/* back pointer to struct hfsc_if */
287 	int		cl_flags;	/* misc flags */
288 
289 	struct hfsc_class *cl_parent;	/* parent class */
290 	struct hfsc_class *cl_siblings;	/* sibling classes */
291 	struct hfsc_class *cl_children;	/* child classes */
292 
293 	class_queue_t	*cl_q;		/* class queue structure */
294 	union {
295 		struct red	*cl_red;	/* RED state */
296 		struct codel	*cl_codel;	/* CoDel state */
297 	} cl_aqm;
298 #define	cl_red			cl_aqm.cl_red
299 #define	cl_codel		cl_aqm.cl_codel
300 	struct altq_pktattr *cl_pktattr; /* saved header used by ECN */
301 
302 	u_int64_t	cl_total;	/* total work in bytes */
303 	u_int64_t	cl_cumul;	/* cumulative work in bytes
304 					   done by real-time criteria */
305 	u_int64_t	cl_d;		/* deadline */
306 	u_int64_t	cl_e;		/* eligible time */
307 	u_int64_t	cl_vt;		/* virtual time */
308 	u_int64_t	cl_f;		/* time when this class will fit for
309 					   link-sharing, max(myf, cfmin) */
310 	u_int64_t	cl_myf;		/* my fit-time (as calculated from this
311 					   class's own upperlimit curve) */
312 	u_int64_t	cl_myfadj;	/* my fit-time adjustment
313 					   (to cancel history dependence) */
314 	u_int64_t	cl_cfmin;	/* earliest children's fit-time (used
315 					   with cl_myf to obtain cl_f) */
316 	u_int64_t	cl_cvtmin;	/* minimal virtual time among the
317 					   children fit for link-sharing
318 					   (monotonic within a period) */
319 	u_int64_t	cl_vtadj;	/* intra-period cumulative vt
320 					   adjustment */
321 	u_int64_t	cl_vtoff;	/* inter-period cumulative vt offset */
322 	u_int64_t	cl_cvtmax;	/* max child's vt in the last period */
323 
324 	u_int64_t	cl_initvt;	/* init virtual time (for debugging) */
325 
326 	struct internal_sc *cl_rsc;	/* internal real-time service curve */
327 	struct internal_sc *cl_fsc;	/* internal fair service curve */
328 	struct internal_sc *cl_usc;	/* internal upperlimit service curve */
329 	struct runtime_sc  cl_deadline;	/* deadline curve */
330 	struct runtime_sc  cl_eligible;	/* eligible curve */
331 	struct runtime_sc  cl_virtual;	/* virtual curve */
332 	struct runtime_sc  cl_ulimit;	/* upperlimit curve */
333 
334 	u_int		cl_vtperiod;	/* vt period sequence no */
335 	u_int		cl_parentperiod;  /* parent's vt period seqno */
336 	int		cl_nactive;	/* number of active children */
337 
338 	TAILQ_HEAD(acthead, hfsc_class) cl_actc; /* active children list */
339 	TAILQ_ENTRY(hfsc_class)	cl_actlist;	/* active children list entry */
340 	TAILQ_ENTRY(hfsc_class)	cl_ellist;	/* eligible list entry */
341 
342 	struct {
343 		struct pktcntr	xmit_cnt;
344 		struct pktcntr	drop_cnt;
345 		u_int period;
346 	} cl_stats;
347 };
348 
349 /*
350  * hfsc interface state
351  */
352 struct hfsc_if {
353 	struct hfsc_if		*hif_next;	/* interface state list */
354 	struct ifaltq		*hif_ifq;	/* backpointer to ifaltq */
355 	struct hfsc_class	*hif_rootclass;		/* root class */
356 	struct hfsc_class	*hif_defaultclass;	/* default class */
357 	struct hfsc_class	*hif_class_tbl[HFSC_MAX_CLASSES];
358 	struct hfsc_class	*hif_pollcache;	/* cache for poll operation */
359 
360 	u_int	hif_classes;			/* # of classes in the tree */
361 	u_int	hif_packets;			/* # of packets in the tree */
362 	u_int	hif_classid;			/* class id sequence number */
363 
364 	TAILQ_HEAD(elighead, hfsc_class) hif_eligible; /* eligible list */
365 
366 #ifdef ALTQ3_CLFIER_COMPAT
367 	struct acc_classifier	hif_classifier;
368 #endif
369 };
370 
371 /*
372  * Kernel code always wants the latest version - avoid a bunch of renames in
373  * the code to the current latest versioned name.
374  */
375 #define	service_curve	__CONCAT(service_curve_v, HFSC_SERVICE_CURVE_VERSION)
376 
377 #else /* _KERNEL */
378 
379 #ifdef PFIOC_USE_LATEST
380 /*
381  * Maintaining in-tree consumers of the ioctl interface is easier when that
382  * code can be written in terms old names that refer to the latest interface
383  * version as that reduces the required changes in the consumers to those
384  * that are functionally necessary to accommodate a new interface version.
385  */
386 #define	hfsc_classstats	__CONCAT(hfsc_classstats_v, HFSC_STATS_VERSION)
387 #define	service_curve	__CONCAT(service_curve_v, HFSC_SERVICE_CURVE_VERSION)
388 
389 #else
390 /*
391  * When building out-of-tree code that is written for the old interface,
392  * such as may exist in ports for example, resolve the old struct tags to
393  * the v0 versions.
394  */
395 #define	hfsc_classstats	__CONCAT(hfsc_classstats_v, 0)
396 #define	service_curve	__CONCAT(service_curve_v, 0)
397 
398 #endif /* PFIOC_USE_LATEST */
399 
400 #endif /* _KERNEL */
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif /* _ALTQ_ALTQ_HFSC_H_ */
407