1/*	$NetBSD: altq_rmclass.h,v 1.13 2022/05/24 20:50:18 andvar Exp $	*/
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the Network Research
19 *	Group at Lawrence Berkeley Laboratory.
20 * 4. Neither the name of the University nor of the Laboratory may be used
21 *    to endorse or promote products derived from this software without
22 *    specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef _ALTQ_ALTQ_RMCLASS_H_
38#define	_ALTQ_ALTQ_RMCLASS_H_
39
40#include <altq/altq_classq.h>
41
42/* #pragma ident "@(#)rm_class.h  1.20     97/10/23 SMI" */
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48#define	RM_MAXPRIO	8	/* Max priority */
49
50#ifdef _KERNEL
51
52typedef struct mbuf		mbuf_t;
53typedef struct rm_ifdat		rm_ifdat_t;
54typedef struct rm_class		rm_class_t;
55
56struct red;
57
58#define	RM_GETTIME(now) nanotime(&now)
59
60#define	TS_LT(a, b) (((a)->tv_sec < (b)->tv_sec) ||  \
61	(((a)->tv_nsec < (b)->tv_nsec) && ((a)->tv_sec <= (b)->tv_sec)))
62
63#define	TS_DELTA(a, b, delta) do { \
64	register int64_t	xxs;	\
65							\
66	delta = (int64_t)((a)->tv_nsec - (b)->tv_nsec); \
67	if ((xxs = (a)->tv_sec - (b)->tv_sec)) { \
68		switch (xxs) { \
69		default: \
70			/* if (xxs < 0) \
71				printf("rm_class: bogus time values\n"); */ \
72			delta = 0; \
73			/* fall through */ \
74		case 2: \
75			delta += 1000000000; \
76			/* fall through */ \
77		case 1: \
78			delta += 1000000000; \
79			break; \
80		} \
81	} \
82} while (0)
83
84#define	TS_ADD_DELTA(a, delta, res) do { \
85	register long xxns = (a)->tv_nsec + (long)(delta); \
86	\
87	(res)->tv_sec = (a)->tv_sec; \
88	while (xxns >= 1000000000) { \
89		++((res)->tv_sec); \
90		xxns -= 1000000000; \
91	} \
92	(res)->tv_nsec = xxns; \
93} while (0)
94
95#define	RM_TIMEOUT	2	/* 1 Clock tick. */
96
97#if 1
98#define	RM_MAXQUEUED	1	/* this isn't used in ALTQ/CBQ */
99#else
100#define	RM_MAXQUEUED	16	/* Max number of packets downstream of CBQ */
101#endif
102#define	RM_MAXQUEUE	64	/* Max queue length */
103#define	RM_FILTER_GAIN	5	/* log2 of gain, e.g., 5 => 31/32 */
104#define	RM_POWER	(1 << RM_FILTER_GAIN)
105#define	RM_MAXDEPTH	32
106#define	RM_NS_PER_SEC	(1000000000)
107#define	RM_PS_PER_SEC	(1000000000000)
108
109typedef struct _rm_class_stats_ {
110	u_int		handle;
111	u_int		depth;
112
113	struct pktcntr	xmit_cnt;	/* packets sent in this class */
114	struct pktcntr	drop_cnt;	/* dropped packets */
115	u_int		over;		/* # times went over limit */
116	u_int		borrows;	/* # times tried to borrow */
117	u_int		overactions;	/* # times invoked overlimit action */
118	u_int		delays;		/* # times invoked delay actions */
119} rm_class_stats_t;
120
121/*
122 * CBQ Class state structure
123 */
124struct rm_class {
125	class_queue_t	*q_;		/* Queue of packets */
126	rm_ifdat_t	*ifdat_;
127	int		pri_;		/* Class priority. */
128	int		depth_;		/* Class depth */
129	uint64_t	ps_per_byte_;	/* PicoSeconds per byte. */
130	u_int		maxrate_;	/* Bytes per second for this class. */
131	u_int		allotment_;	/* Fraction of link bandwidth. */
132	u_int		w_allotment_;	/* Weighted allotment for WRR */
133	int		bytes_alloc_;	/* Allocation for round of WRR */
134
135	int64_t		avgidle_;
136	int64_t		maxidle_;
137	int64_t		minidle_;
138	int64_t		offtime_;
139	int		sleeping_;	/* != 0 if delaying */
140	int		qthresh_;	/* Queue threshold for formal link sharing */
141	int		leaf_;		/* Note whether leaf class or not.*/
142
143	rm_class_t	*children_;	/* Children of this class */
144	rm_class_t	*next_;		/* Next pointer, used if child */
145
146	rm_class_t	*peer_;		/* Peer class */
147	rm_class_t	*borrow_;	/* Borrow class */
148	rm_class_t	*parent_;	/* Parent class */
149
150	void	(*overlimit)(struct rm_class *, struct rm_class *);
151	void	(*drop)(struct rm_class *);       /* Class drop action. */
152
153	struct red	*red_;		/* RED state pointer */
154	struct altq_pktattr *pktattr_;	/* saved hdr used by RED/ECN */
155	int		flags_;
156
157	int64_t		last_pkttime_;	/* saved pkt_time */
158	struct timespec	undertime_;	/* time can next send */
159	struct timespec	last_;		/* time last packet sent */
160	struct timespec	overtime_;
161	struct callout	callout_; 	/* for timeout() calls */
162
163	rm_class_stats_t stats_;	/* Class Statistics */
164};
165
166/*
167 * CBQ Interface state
168 */
169struct rm_ifdat {
170	int		queued_;	/* # pkts queued downstream */
171	int		efficient_;	/* Link Efficiency bit */
172	int		wrr_;		/* Enable Weighted Round-Robin */
173	uint64_t	ps_per_byte_;	/* Link byte speed. */
174	int		maxqueued_;	/* Max packets to queue */
175	int		maxpkt_;	/* Max packet size. */
176	int		qi_;		/* In/out pointers for downstream */
177	int		qo_;		/* packets */
178
179	/*
180	 * Active class state and WRR state.
181	 */
182	rm_class_t	*active_[RM_MAXPRIO];	/* Active cl's in each pri */
183	int		na_[RM_MAXPRIO];	/* # of active cl's in a pri */
184	int		num_[RM_MAXPRIO];	/* # of cl's per pri */
185	int		alloc_[RM_MAXPRIO];	/* Byte Allocation */
186	u_long		M_[RM_MAXPRIO];		/* WRR weights. */
187
188	/*
189	 * Network Interface/Solaris Queue state pointer.
190	 */
191	struct ifaltq	*ifq_;
192	rm_class_t	*default_;	/* Default Pkt class, BE */
193	rm_class_t	*root_;		/* Root Link class. */
194	rm_class_t	*ctl_;		/* Control Traffic class. */
195	void		(*restart)(struct ifaltq *);	/* Restart routine. */
196
197	/*
198	 * Current packet downstream packet state and dynamic state.
199	 */
200	rm_class_t	*borrowed_[RM_MAXQUEUED]; /* Class borrowed last */
201	rm_class_t	*class_[RM_MAXQUEUED];	/* class sending */
202	int		curlen_[RM_MAXQUEUED];	/* Current pktlen */
203	struct timespec	now_[RM_MAXQUEUED];	/* Current packet time. */
204	int		is_overlimit_[RM_MAXQUEUED];/* Current packet time. */
205
206	int		cutoff_;	/* Cut-off depth for borrowing */
207
208	struct timespec	ifnow_;		/* expected xmit completion time */
209#if 1 /* ALTQ4PPP */
210	int		maxiftime_;	/* max delay inside interface */
211#endif
212        rm_class_t	*pollcache_;	/* cached rm_class by poll operation */
213};
214
215/* flags for rmc_init and rmc_newclass */
216/* class flags */
217#define	RMCF_RED		0x0001
218#define	RMCF_ECN		0x0002
219#define	RMCF_RIO		0x0004
220#define	RMCF_FLOWVALVE		0x0008	/* use flowvalve (aka penalty-box) */
221#define	RMCF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
222
223/* flags for rmc_init */
224#define	RMCF_WRR		0x0100
225#define	RMCF_EFFICIENT		0x0200
226
227#define	is_a_parent_class(cl)	((cl)->children_ != NULL)
228
229extern rm_class_t *rmc_newclass(int, struct rm_ifdat *, uint64_t,
230				void (*)(struct rm_class *, struct rm_class *),
231				int, struct rm_class *, struct rm_class *,
232				u_int, int, u_int, int, int);
233extern void	rmc_delete_class(struct rm_ifdat *, struct rm_class *);
234extern int 	rmc_modclass(struct rm_class *, uint64_t, int,
235			     u_int, int, u_int, int);
236extern int	rmc_init(struct ifaltq *, struct rm_ifdat *, uint64_t,
237			 void (*)(struct ifaltq *),
238			 int, int, u_int, int, u_int, int);
239extern int	rmc_queue_packet(struct rm_class *, mbuf_t *);
240extern mbuf_t	*rmc_dequeue_next(struct rm_ifdat *, int);
241extern void	rmc_update_class_util(struct rm_ifdat *);
242extern void	rmc_delay_action(struct rm_class *, struct rm_class *);
243extern void	rmc_dropall(struct rm_class *);
244extern int	rmc_get_weight(struct rm_ifdat *, int);
245
246#endif /* _KERNEL */
247
248#ifdef __cplusplus
249}
250#endif
251
252#endif /* _ALTQ_ALTQ_RMCLASS_H_ */
253