]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sbin/pfctl/missing/altq/altq_rmclass.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sbin / pfctl / missing / altq / altq_rmclass.h
1 /*      $FreeBSD$       */
2 /*      $KAME: altq_rmclass.h,v 1.10 2003/08/20 23:30:23 itojun Exp $   */
3
4 /*
5  * Copyright (c) 1991-1997 Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor of the Laboratory may be used
17  *    to endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef _ALTQ_ALTQ_RMCLASS_H_
34 #define _ALTQ_ALTQ_RMCLASS_H_
35
36 #include <altq/altq_classq.h>
37
38 /* #pragma ident "@(#)rm_class.h  1.20     97/10/23 SMI" */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #define RM_MAXPRIO      8       /* Max priority */
45
46 #ifdef _KERNEL
47
48 typedef struct mbuf             mbuf_t;
49 typedef struct rm_ifdat         rm_ifdat_t;
50 typedef struct rm_class         rm_class_t;
51
52 struct red;
53
54 /*
55  * Macros for dealing with time values.  We assume all times are
56  * 'timevals'.  `microtime' is used to get the best available clock
57  * resolution.  If `microtime' *doesn't* return a value that's about
58  * ten times smaller than the average packet time on the fastest
59  * link that will use these routines, a slightly different clock
60  * scheme than this one should be used.
61  * (Bias due to truncation error in this scheme will overestimate utilization
62  * and discriminate against high bandwidth classes.  To remove this bias an
63  * integrator needs to be added.  The simplest integrator uses a history of
64  * 10 * avg.packet.time / min.tick.time packet completion entries.  This is
65  * straight forward to add but we don't want to pay the extra memory
66  * traffic to maintain it if it's not necessary (occasionally a vendor
67  * accidentally builds a workstation with a decent clock - e.g., Sun & HP).)
68  */
69
70 #define RM_GETTIME(now) microtime(&now)
71
72 #define TV_LT(a, b) (((a)->tv_sec < (b)->tv_sec) ||  \
73         (((a)->tv_usec < (b)->tv_usec) && ((a)->tv_sec <= (b)->tv_sec)))
74
75 #define TV_DELTA(a, b, delta) { \
76         register int    xxs;    \
77                                                         \
78         delta = (a)->tv_usec - (b)->tv_usec; \
79         if ((xxs = (a)->tv_sec - (b)->tv_sec)) { \
80                 switch (xxs) { \
81                 default: \
82                         /* if (xxs < 0) \
83                                 printf("rm_class: bogus time values\n"); */ \
84                         delta = 0; \
85                         /* fall through */ \
86                 case 2: \
87                         delta += 1000000; \
88                         /* fall through */ \
89                 case 1: \
90                         delta += 1000000; \
91                         break; \
92                 } \
93         } \
94 }
95
96 #define TV_ADD_DELTA(a, delta, res) { \
97         register int xxus = (a)->tv_usec + (delta); \
98         \
99         (res)->tv_sec = (a)->tv_sec; \
100         while (xxus >= 1000000) { \
101                 ++((res)->tv_sec); \
102                 xxus -= 1000000; \
103         } \
104         (res)->tv_usec = xxus; \
105 }
106
107 #define RM_TIMEOUT      2       /* 1 Clock tick. */
108
109 #if 1
110 #define RM_MAXQUEUED    1       /* this isn't used in ALTQ/CBQ */
111 #else
112 #define RM_MAXQUEUED    16      /* Max number of packets downstream of CBQ */
113 #endif
114 #define RM_MAXQUEUE     64      /* Max queue length */
115 #define RM_FILTER_GAIN  5       /* log2 of gain, e.g., 5 => 31/32 */
116 #define RM_POWER        (1 << RM_FILTER_GAIN)
117 #define RM_MAXDEPTH     32
118 #define RM_NS_PER_SEC   (1000000000)
119
120 typedef struct _rm_class_stats_ {
121         u_int           handle;
122         u_int           depth;
123
124         struct pktcntr  xmit_cnt;       /* packets sent in this class */
125         struct pktcntr  drop_cnt;       /* dropped packets */
126         u_int           over;           /* # times went over limit */
127         u_int           borrows;        /* # times tried to borrow */
128         u_int           overactions;    /* # times invoked overlimit action */
129         u_int           delays;         /* # times invoked delay actions */
130 } rm_class_stats_t;
131
132 /*
133  * CBQ Class state structure
134  */
135 struct rm_class {
136         class_queue_t   *q_;            /* Queue of packets */
137         rm_ifdat_t      *ifdat_;
138         int             pri_;           /* Class priority. */
139         int             depth_;         /* Class depth */
140         u_int           ns_per_byte_;   /* NanoSeconds per byte. */
141         u_int           maxrate_;       /* Bytes per second for this class. */
142         u_int           allotment_;     /* Fraction of link bandwidth. */
143         u_int           w_allotment_;   /* Weighted allotment for WRR */
144         int             bytes_alloc_;   /* Allocation for round of WRR */
145
146         int             avgidle_;
147         int             maxidle_;
148         int             minidle_;
149         int             offtime_;
150         int             sleeping_;      /* != 0 if delaying */
151         int             qthresh_;       /* Queue threshold for formal link sharing */
152         int             leaf_;          /* Note whether leaf class or not.*/
153
154         rm_class_t      *children_;     /* Children of this class */
155         rm_class_t      *next_;         /* Next pointer, used if child */
156
157         rm_class_t      *peer_;         /* Peer class */
158         rm_class_t      *borrow_;       /* Borrow class */
159         rm_class_t      *parent_;       /* Parent class */
160
161         void    (*overlimit)(struct rm_class *, struct rm_class *);
162         void    (*drop)(struct rm_class *);       /* Class drop action. */
163
164         struct red      *red_;          /* RED state pointer */
165         struct altq_pktattr *pktattr_;  /* saved hdr used by RED/ECN */
166         int             flags_;
167
168         int             last_pkttime_;  /* saved pkt_time */
169         struct timeval  undertime_;     /* time can next send */
170         struct timeval  last_;          /* time last packet sent */
171         struct timeval  overtime_;
172         struct callout  callout_;       /* for timeout() calls */
173
174         rm_class_stats_t stats_;        /* Class Statistics */
175 };
176
177 /*
178  * CBQ Interface state
179  */
180 struct rm_ifdat {
181         int             queued_;        /* # pkts queued downstream */
182         int             efficient_;     /* Link Efficency bit */
183         int             wrr_;           /* Enable Weighted Round-Robin */
184         u_long          ns_per_byte_;   /* Link byte speed. */
185         int             maxqueued_;     /* Max packets to queue */
186         int             maxpkt_;        /* Max packet size. */
187         int             qi_;            /* In/out pointers for downstream */
188         int             qo_;            /* packets */
189
190         /*
191          * Active class state and WRR state.
192          */
193         rm_class_t      *active_[RM_MAXPRIO];   /* Active cl's in each pri */
194         int             na_[RM_MAXPRIO];        /* # of active cl's in a pri */
195         int             num_[RM_MAXPRIO];       /* # of cl's per pri */
196         int             alloc_[RM_MAXPRIO];     /* Byte Allocation */
197         u_long          M_[RM_MAXPRIO];         /* WRR weights. */
198
199         /*
200          * Network Interface/Solaris Queue state pointer.
201          */
202         struct ifaltq   *ifq_;
203         rm_class_t      *default_;      /* Default Pkt class, BE */
204         rm_class_t      *root_;         /* Root Link class. */
205         rm_class_t      *ctl_;          /* Control Traffic class. */
206         void            (*restart)(struct ifaltq *);    /* Restart routine. */
207
208         /*
209          * Current packet downstream packet state and dynamic state.
210          */
211         rm_class_t      *borrowed_[RM_MAXQUEUED]; /* Class borrowed last */
212         rm_class_t      *class_[RM_MAXQUEUED];  /* class sending */
213         int             curlen_[RM_MAXQUEUED];  /* Current pktlen */
214         struct timeval  now_[RM_MAXQUEUED];     /* Current packet time. */
215         int             is_overlimit_[RM_MAXQUEUED];/* Current packet time. */
216
217         int             cutoff_;        /* Cut-off depth for borrowing */
218
219         struct timeval  ifnow_;         /* expected xmit completion time */
220 #if 1 /* ALTQ4PPP */
221         int             maxiftime_;     /* max delay inside interface */
222 #endif
223         rm_class_t      *pollcache_;    /* cached rm_class by poll operation */
224 };
225
226 /* flags for rmc_init and rmc_newclass */
227 /* class flags */
228 #define RMCF_RED                0x0001
229 #define RMCF_ECN                0x0002
230 #define RMCF_RIO                0x0004
231 #define RMCF_FLOWVALVE          0x0008  /* use flowvalve (aka penalty-box) */
232 #define RMCF_CLEARDSCP          0x0010  /* clear diffserv codepoint */
233
234 /* flags for rmc_init */
235 #define RMCF_WRR                0x0100
236 #define RMCF_EFFICIENT          0x0200
237
238 #define is_a_parent_class(cl)   ((cl)->children_ != NULL)
239
240 extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, u_int,
241                                 void (*)(struct rm_class *, struct rm_class *),
242                                 int, struct rm_class *, struct rm_class *,
243                                 u_int, int, u_int, int, int);
244 extern void     rmc_delete_class(struct rm_ifdat *, struct rm_class *);
245 extern int      rmc_modclass(struct rm_class *, u_int, int,
246                              u_int, int, u_int, int);
247 extern void     rmc_init(struct ifaltq *, struct rm_ifdat *, u_int,
248                          void (*)(struct ifaltq *),
249                          int, int, u_int, int, u_int, int);
250 extern int      rmc_queue_packet(struct rm_class *, mbuf_t *);
251 extern mbuf_t   *rmc_dequeue_next(struct rm_ifdat *, int);
252 extern void     rmc_update_class_util(struct rm_ifdat *);
253 extern void     rmc_delay_action(struct rm_class *, struct rm_class *);
254 extern void     rmc_dropall(struct rm_class *);
255 extern int      rmc_get_weight(struct rm_ifdat *, int);
256
257 #endif /* _KERNEL */
258
259 #ifdef __cplusplus
260 }
261 #endif
262
263 #endif /* _ALTQ_ALTQ_RMCLASS_H_ */