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