1/*
2 * Copyright (c) 2011-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*	$NetBSD: altq_cbq.h,v 1.8 2006/10/12 19:59:08 peter Exp $	*/
30/*	$KAME: altq_cbq.h,v 1.12 2003/10/03 05:05:15 kjc Exp $	*/
31
32/*
33 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 *
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 *
46 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *      This product includes software developed by the SMCC Technology
49 *      Development Group at Sun Microsystems, Inc.
50 *
51 * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
52 *      promote products derived from this software without specific prior
53 *      written permission.
54 *
55 * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
56 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is
57 * provided "as is" without express or implied warranty of any kind.
58 *
59 * These notices must be retained in any copies of any part of this software.
60 */
61
62#ifndef _NET_PKTSCHED_PKTSCHED_CBQ_H_
63#define	_NET_PKTSCHED_PKTSCHED_CBQ_H_
64
65#ifdef PRIVATE
66#include <net/pktsched/pktsched_rmclass.h>
67#include <net/classq/classq.h>
68#include <net/classq/classq_red.h>
69#include <net/classq/classq_rio.h>
70#include <net/classq/classq_blue.h>
71#include <net/classq/classq_sfb.h>
72
73#ifdef __cplusplus
74extern "C" {
75#endif
76
77/* class flags should be same as class flags in rm_class.h */
78#define	CBQCLF_RED		RMCF_RED	/* use RED */
79#define	CBQCLF_ECN		RMCF_ECN	/* use ECN with RED/BLUE/SFB */
80#define	CBQCLF_RIO		RMCF_RIO	/* use RIO */
81#define	CBQCLF_FLOWVALVE	RMCF_FLOWVALVE	/* use flowvalve/penalty-box */
82#define	CBQCLF_CLEARDSCP	RMCF_CLEARDSCP	/* clear diffserv codepoint */
83#define	CBQCLF_BORROW		0x0020		/* borrow from parent */
84
85/* class flags only for root class */
86#define	CBQCLF_WRR		RMCF_WRR	/* weighted-round robin */
87#define	CBQCLF_EFFICIENT	RMCF_EFFICIENT	/* work-conserving */
88
89/* class flags for special classes */
90#define	CBQCLF_ROOTCLASS	0x1000		/* root class */
91#define	CBQCLF_DEFCLASS		0x2000		/* default class */
92#define	CBQCLF_CLASSMASK	0xf000		/* class mask */
93
94#define	CBQCLF_BLUE		RMCF_BLUE	/* use BLUE */
95#define	CBQCLF_SFB		RMCF_SFB	/* use SFB */
96#define	CBQCLF_FLOWCTL		RMCF_FLOWCTL	/* enable flow ctl advisories */
97
98#ifdef BSD_KERNEL_PRIVATE
99#define	CBQCLF_LAZY		0x10000000 /* on-demand resource allocation */
100#endif /* BSD_KERNEL_PRIVATE */
101
102#define	CBQCLF_USERFLAGS						   \
103	(CBQCLF_RED | CBQCLF_ECN | CBQCLF_RIO | CBQCLF_FLOWVALVE |	   \
104	CBQCLF_CLEARDSCP | CBQCLF_BORROW | CBQCLF_WRR | CBQCLF_EFFICIENT | \
105	CBQCLF_ROOTCLASS | CBQCLF_DEFCLASS | CBQCLF_BLUE | CBQCLF_SFB |	   \
106	CBQCLF_FLOWCTL)
107
108#ifdef BSD_KERNEL_PRIVATE
109#define	CBQCLF_BITS \
110	"\020\1RED\2ECN\3RIO\4FLOWVALVE\5CLEARDSCP\6BORROW" \
111	"\11WRR\12EFFICIENT\15ROOT\16DEFAULT\21BLUE\22SFB\23FLOWCTL\35LAZY"
112#else
113#define	CBQCLF_BITS \
114	"\020\1RED\2ECN\3RIO\4FLOWVALVE\5CLEARDSCP\6BORROW" \
115	"\11WRR\12EFFICIENT\15ROOT\16DEFAULT\21BLUE\22SFB\23FLOWCTL"
116#endif /* !BSD_KERNEL_PRIVATE */
117
118#define	CBQ_MAXQSIZE		200
119#define	CBQ_MAXPRI		RM_MAXPRIO
120
121typedef struct cbq_classstats {
122	u_int32_t	handle;
123	u_int32_t	depth;
124
125	struct pktcntr	xmit_cnt;	/* packets sent in this class */
126	struct pktcntr	drop_cnt;	/* dropped packets */
127	u_int32_t	over;		/* # times went over limit */
128	u_int32_t	borrows;	/* # times tried to borrow */
129	u_int32_t	overactions;	/* # times invoked overlimit action */
130	u_int32_t	delays;		/* # times invoked delay actions */
131
132	/* other static class parameters useful for debugging */
133	int		priority;
134	int		maxidle;
135	int		minidle;
136	int		offtime;
137	int		qmax;
138	int		ns_per_byte;
139	int		wrr_allot;
140
141	int		qcnt;		/* # packets in queue */
142	int		avgidle;
143
144	/* RED, RIO, BLUE, SFB related info */
145	classq_type_t	qtype;
146	union {
147		/* RIO has 3 red stats */
148		struct red_stats	red[RIO_NDROPPREC];
149		struct blue_stats	blue;
150		struct sfb_stats	sfb;
151	};
152	classq_state_t	qstate;
153} class_stats_t;
154
155#ifdef BSD_KERNEL_PRIVATE
156/*
157 * Define macros only good for kernel drivers and modules.
158 */
159#define	CBQ_WATCHDOG		(hz / 20)
160#define	CBQ_TIMEOUT		10
161#define	CBQ_LS_TIMEOUT		(20 * hz / 1000)
162
163#define	CBQ_MAX_CLASSES	256
164
165/* cbqstate flags */
166#define	CBQSF_ALTQ		0x1	/* configured via PF/ALTQ */
167
168/*
169 * Define State structures.
170 */
171typedef struct cbqstate {
172	int			 cbq_qlen;	/* # of packets in cbq */
173	u_int32_t		 cbq_flags;	/* flags */
174	struct rm_class		*cbq_class_tbl[CBQ_MAX_CLASSES];
175
176	struct rm_ifdat		 ifnp;
177	struct callout		 cbq_callout;	/* for timeouts */
178} cbq_state_t;
179
180#define	CBQS_IFP(_cs)		((_cs)->ifnp.ifq_->ifcq_ifp)
181
182extern void cbq_init(void);
183extern cbq_state_t *cbq_alloc(struct ifnet *, int, boolean_t);
184extern int cbq_destroy(cbq_state_t *);
185extern void cbq_purge(cbq_state_t *);
186extern void cbq_event(cbq_state_t *, cqev_t);
187extern int cbq_add_queue(cbq_state_t *, u_int32_t, u_int32_t, u_int32_t,
188    u_int32_t, u_int32_t, u_int32_t, u_int32_t, u_int32_t, int, u_int32_t,
189    u_int32_t, u_int32_t, u_int32_t, struct rm_class **);
190extern int cbq_remove_queue(cbq_state_t *, u_int32_t);
191extern int cbq_get_class_stats(cbq_state_t *, u_int32_t, class_stats_t *);
192extern int cbq_enqueue(cbq_state_t *, struct rm_class *, struct mbuf *,
193    struct pf_mtag *);
194extern struct mbuf *cbq_dequeue(cbq_state_t *, cqdq_op_t);
195extern int cqb_setup_ifclassq(struct ifclassq *, u_int32_t);
196extern int cbq_teardown_ifclassq(struct ifclassq *);
197extern int cbq_getqstats_ifclassq(struct ifclassq *, u_int32_t,
198    struct if_ifclassq_stats *);
199#endif /* BSD_KERNEL_PRIVATE */
200#ifdef __cplusplus
201}
202#endif
203#endif /* PRIVATE */
204#endif /* !_NET_PKTSCHED_PKTSCHED_CBQ_H_ */
205