xref: /freebsd/sys/net/altq/altq_fairq.h (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/net/altq/altq_fairq.h,v 1.1 2008/04/06 18:58:15 dillon Exp $
35  * $FreeBSD$
36  */
37 
38 #ifndef _ALTQ_ALTQ_FAIRQ_H_
39 #define	_ALTQ_ALTQ_FAIRQ_H_
40 
41 #include <net/altq/altq.h>
42 #include <net/altq/altq_classq.h>
43 #include <net/altq/altq_codel.h>
44 #include <net/altq/altq_red.h>
45 #include <net/altq/altq_rio.h>
46 #include <net/altq/altq_rmclass.h>
47 
48 #define	FAIRQ_MAX_BUCKETS	2048	/* maximum number of sorting buckets */
49 #define	FAIRQ_MAXPRI		RM_MAXPRIO
50 #define FAIRQ_BITMAP_WIDTH	(sizeof(fairq_bitmap_t)*8)
51 #define FAIRQ_BITMAP_MASK	(FAIRQ_BITMAP_WIDTH - 1)
52 
53 /* fairq class flags */
54 #define	FARF_RED		0x0001	/* use RED */
55 #define	FARF_ECN		0x0002  /* use RED/ECN */
56 #define	FARF_RIO		0x0004  /* use RIO */
57 #define	FARF_CODEL		0x0008	/* use CoDel */
58 #define	FARF_CLEARDSCP		0x0010  /* clear diffserv codepoint */
59 #define	FARF_DEFAULTCLASS	0x1000	/* default class */
60 
61 #define FARF_HAS_PACKETS	0x2000	/* might have queued packets */
62 
63 #define FARF_USERFLAGS		(FARF_RED|FARF_ECN|FARF_RIO|FARF_CLEARDSCP| \
64 				 FARF_DEFAULTCLASS)
65 
66 /* special class handles */
67 #define	FAIRQ_NULLCLASS_HANDLE	0
68 
69 typedef u_int	fairq_bitmap_t;
70 
71 struct fairq_classstats {
72 	uint32_t		class_handle;
73 
74 	u_int			qlength;
75 	u_int			qlimit;
76 	struct pktcntr		xmit_cnt;  /* transmitted packet counter */
77 	struct pktcntr		drop_cnt;  /* dropped packet counter */
78 
79 	/* codel, red and rio related info */
80 	int			qtype;
81 	struct redstats		red[3];	/* rio has 3 red stats */
82 	struct codel_stats	codel;
83 };
84 
85 /*
86  * FAIRQ_STATS_VERSION is defined in altq.h to work around issues stemming
87  * from mixing of public-API and internal bits in each scheduler-specific
88  * header.
89  */
90 
91 #ifdef _KERNEL
92 
93 typedef struct fairq_bucket {
94 	struct fairq_bucket *next;	/* circular list */
95 	struct fairq_bucket *prev;	/* circular list */
96 	class_queue_t	queue;		/* the actual queue */
97 	uint64_t	bw_bytes;	/* statistics used to calculate bw */
98 	uint64_t	bw_delta;	/* statistics used to calculate bw */
99 	uint64_t	last_time;
100 	int		in_use;
101 } fairq_bucket_t;
102 
103 struct fairq_class {
104 	uint32_t	cl_handle;	/* class handle */
105 	u_int		cl_nbuckets;	/* (power of 2) */
106 	u_int		cl_nbucket_mask; /* bucket mask */
107 	fairq_bucket_t	*cl_buckets;
108 	fairq_bucket_t	*cl_head;	/* head of circular bucket list */
109 	fairq_bucket_t	*cl_polled;
110 	union {
111 		struct red	*cl_red;	/* RED state */
112 		struct codel	*cl_codel;	/* CoDel state */
113 	} cl_aqm;
114 #define	cl_red		cl_aqm.cl_red
115 #define	cl_codel	cl_aqm.cl_codel
116 	u_int		cl_hogs_m1;
117 	u_int		cl_lssc_m1;
118 	u_int		cl_bandwidth;
119 	uint64_t	cl_bw_bytes;
120 	uint64_t	cl_bw_delta;
121 	uint64_t	cl_last_time;
122 	int		cl_qtype;	/* rollup */
123 	int		cl_qlimit;
124 	int		cl_pri;		/* priority */
125 	int		cl_flags;	/* class flags */
126 	struct fairq_if	*cl_pif;	/* back pointer to pif */
127 	struct altq_pktattr *cl_pktattr; /* saved header used by ECN */
128 
129 	/* round robin index */
130 
131 	/* statistics */
132 	struct pktcntr  cl_xmitcnt;	/* transmitted packet counter */
133 	struct pktcntr  cl_dropcnt;	/* dropped packet counter */
134 };
135 
136 /*
137  * fairq interface state
138  */
139 struct fairq_if {
140 	struct fairq_if		*pif_next;	/* interface state list */
141 	struct ifaltq		*pif_ifq;	/* backpointer to ifaltq */
142 	u_int			pif_bandwidth;	/* link bandwidth in bps */
143 	int			pif_maxpri;	/* max priority in use */
144 	struct fairq_class	*pif_poll_cache;/* cached poll */
145 	struct fairq_class	*pif_default;	/* default class */
146 	struct fairq_class	*pif_classes[FAIRQ_MAXPRI]; /* classes */
147 };
148 
149 #endif /* _KERNEL */
150 
151 #endif /* _ALTQ_ALTQ_FAIRQ_H_ */
152