Commit | Line | Data |
---|---|---|
4d723e5a | 1 | /* $KAME: if_altq.h,v 1.11 2003/07/10 12:07:50 kjc Exp $ */ |
4d723e5a JS |
2 | |
3 | /* | |
4 | * Copyright (C) 1997-2003 | |
5 | * Sony Computer Science Laboratories Inc. All rights reserved. | |
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 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND | |
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE | |
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
26 | * SUCH DAMAGE. | |
27 | */ | |
1bd40720 MD |
28 | #ifndef _NET_ALTQ_IF_ALTQ_H_ |
29 | #define _NET_ALTQ_IF_ALTQ_H_ | |
4d723e5a | 30 | |
e7d68516 | 31 | #include <sys/mbuf.h> |
9db4b353 | 32 | #include <sys/serialize.h> |
e7d68516 | 33 | #include <net/altq/if_classq.h> |
9db4b353 | 34 | |
f0a26983 SZ |
35 | /* Default subqueue */ |
36 | #define ALTQ_SUBQ_INDEX_DEFAULT 0 | |
37 | ||
38 | struct mbuf; | |
4d723e5a JS |
39 | struct altq_pktattr; |
40 | ||
f0a26983 | 41 | struct ifaltq_subque; |
28cc0c29 SZ |
42 | struct ifaltq; |
43 | ||
2cc2f639 SZ |
44 | typedef int (*altq_mapsubq_t)(struct ifaltq *, int); |
45 | ||
f0a26983 SZ |
46 | typedef int (*ifsq_enqueue_t)(struct ifaltq_subque *, struct mbuf *, |
47 | struct altq_pktattr *); | |
6dadc833 | 48 | typedef struct mbuf *(*ifsq_dequeue_t)(struct ifaltq_subque *, int); |
f0a26983 SZ |
49 | typedef int (*ifsq_request_t)(struct ifaltq_subque *, int, void *); |
50 | ||
51 | struct ifsubq_stage { | |
52 | struct ifaltq_subque *stg_subq; | |
53 | int stg_cnt; | |
54 | int stg_len; | |
55 | uint32_t stg_flags; | |
56 | TAILQ_ENTRY(ifsubq_stage) stg_link; | |
57 | } __cachealign; | |
58 | ||
59 | #define IFSQ_STAGE_FLAG_QUED 0x1 | |
60 | #define IFSQ_STAGE_FLAG_SCHED 0x2 | |
61 | ||
62 | struct ifaltq_subque { | |
63 | struct lwkt_serialize ifsq_lock; | |
64 | int ifsq_index; | |
65 | ||
66 | struct ifaltq *ifsq_altq; | |
67 | struct ifnet *ifsq_ifp; | |
68 | void *ifsq_hw_priv; /* hw private data */ | |
69 | ||
e7d68516 SZ |
70 | struct if_classq ifsq_norm; |
71 | struct if_classq ifsq_prio; | |
4cc8caef SZ |
72 | int ifsq_prio_len; |
73 | int ifsq_prio_bcnt; | |
68dc1916 | 74 | int ifsq_len; /* packet counter */ |
b21c2105 | 75 | int ifsq_maxlen; |
68dc1916 SZ |
76 | int ifsq_bcnt; /* byte counter */ |
77 | int ifsq_maxbcnt; | |
f0a26983 SZ |
78 | |
79 | ifsq_enqueue_t ifsq_enqueue; | |
80 | ifsq_dequeue_t ifsq_dequeue; | |
81 | ifsq_request_t ifsq_request; | |
82 | ||
bfefe4a6 SZ |
83 | struct lwkt_serialize *ifsq_hw_serialize; |
84 | /* hw serializer */ | |
f0a26983 SZ |
85 | struct mbuf *ifsq_prepended;/* mbuf dequeued, but not yet xmit */ |
86 | int ifsq_started; /* ifnet.if_start interlock */ | |
87 | int ifsq_hw_oactive;/* hw too busy, protected by driver */ | |
88 | int ifsq_cpuid; /* owner cpu */ | |
89 | struct ifsubq_stage *ifsq_stage;/* packet staging information */ | |
90 | struct netmsg_base *ifsq_ifstart_nmsg; | |
91 | /* percpu msgs to sched if_start */ | |
28cc0c29 SZ |
92 | } __cachealign; |
93 | ||
f0a26983 | 94 | #ifdef _KERNEL |
bfefe4a6 | 95 | |
f0a26983 SZ |
96 | #define ALTQ_SQ_ASSERT_LOCKED(ifsq) ASSERT_SERIALIZED(&(ifsq)->ifsq_lock) |
97 | #define ALTQ_SQ_LOCK_INIT(ifsq) lwkt_serialize_init(&(ifsq)->ifsq_lock) | |
98 | #define ALTQ_SQ_LOCK(ifsq) \ | |
99 | lwkt_serialize_adaptive_enter(&(ifsq)->ifsq_lock) | |
100 | #define ALTQ_SQ_UNLOCK(ifsq) lwkt_serialize_exit(&(ifsq)->ifsq_lock) | |
bfefe4a6 SZ |
101 | |
102 | #define ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq) \ | |
103 | ASSERT_SERIALIZED((ifsq)->ifsq_hw_serialize) | |
104 | #define ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq) \ | |
105 | ASSERT_NOT_SERIALIZED((ifsq)->ifsq_hw_serialize) | |
106 | ||
5a24ba58 | 107 | #define ALTQ_SQ_PKTCNT_INC(ifsq) \ |
68dc1916 SZ |
108 | do { \ |
109 | (ifsq)->ifsq_len++; \ | |
68dc1916 SZ |
110 | } while (0) |
111 | ||
5a24ba58 | 112 | #define ALTQ_SQ_PKTCNT_DEC(ifsq) \ |
68dc1916 SZ |
113 | do { \ |
114 | KASSERT((ifsq)->ifsq_len > 0, ("invalid packet count")); \ | |
115 | (ifsq)->ifsq_len--; \ | |
5a24ba58 SZ |
116 | } while (0) |
117 | ||
118 | #define ALTQ_SQ_CNTR_INC(ifsq, bcnt) \ | |
119 | do { \ | |
120 | ALTQ_SQ_PKTCNT_INC((ifsq)); \ | |
121 | (ifsq)->ifsq_bcnt += (bcnt); \ | |
122 | } while (0) | |
123 | ||
124 | #define ALTQ_SQ_CNTR_DEC(ifsq, bcnt) \ | |
125 | do { \ | |
126 | ALTQ_SQ_PKTCNT_DEC((ifsq)); \ | |
4cc8caef | 127 | KASSERT((ifsq)->ifsq_bcnt >= (bcnt), ("invalid byte count")); \ |
68dc1916 SZ |
128 | (ifsq)->ifsq_bcnt -= (bcnt); \ |
129 | } while (0) | |
130 | ||
131 | #define ALTQ_SQ_CNTR_RESET(ifsq) \ | |
132 | do { \ | |
133 | (ifsq)->ifsq_len = 0; \ | |
134 | (ifsq)->ifsq_bcnt = 0; \ | |
135 | } while (0) | |
136 | ||
4cc8caef SZ |
137 | #define ALTQ_SQ_PRIO_CNTR_INC(ifsq, bcnt) \ |
138 | do { \ | |
139 | (ifsq)->ifsq_prio_len++; \ | |
140 | (ifsq)->ifsq_prio_bcnt += (bcnt); \ | |
141 | } while (0) | |
142 | ||
143 | #define ALTQ_SQ_PRIO_CNTR_DEC(ifsq, bcnt) \ | |
144 | do { \ | |
145 | KASSERT((ifsq)->ifsq_prio_len > 0, \ | |
146 | ("invalid prio packet count")); \ | |
147 | (ifsq)->ifsq_prio_len--; \ | |
148 | KASSERT((ifsq)->ifsq_prio_bcnt >= (bcnt), \ | |
149 | ("invalid prio byte count")); \ | |
150 | (ifsq)->ifsq_prio_bcnt -= (bcnt); \ | |
151 | } while (0) | |
152 | ||
bfefe4a6 | 153 | #endif /* _KERNEL */ |
28cc0c29 | 154 | |
4d723e5a JS |
155 | /* |
156 | * Structure defining a queue for a network interface. | |
157 | */ | |
158 | struct ifaltq { | |
4d723e5a JS |
159 | /* alternate queueing related fields */ |
160 | int altq_type; /* discipline type */ | |
161 | int altq_flags; /* flags (e.g. ready, in-use) */ | |
162 | void *altq_disc; /* for discipline-specific use */ | |
163 | struct ifnet *altq_ifp; /* back pointer to interface */ | |
164 | ||
4d723e5a JS |
165 | /* classifier fields */ |
166 | void *altq_clfier; /* classifier-specific use */ | |
167 | void *(*altq_classify)(struct ifaltq *, struct mbuf *, | |
168 | struct altq_pktattr *); | |
169 | ||
170 | /* token bucket regulator */ | |
171 | struct tb_regulator *altq_tbr; | |
9db4b353 | 172 | |
2cc2f639 SZ |
173 | /* Sub-queues mapping */ |
174 | altq_mapsubq_t altq_mapsubq; | |
68732d8f | 175 | uint32_t altq_subq_mappriv; |
2cc2f639 | 176 | |
f0a26983 SZ |
177 | /* Sub-queues */ |
178 | int altq_subq_cnt; | |
179 | struct ifaltq_subque *altq_subq; | |
180 | ||
181 | int altq_maxlen; | |
4d723e5a JS |
182 | }; |
183 | ||
66c898fe | 184 | #ifdef _KERNEL |
f0a26983 SZ |
185 | /* COMPAT */ |
186 | #define ALTQ_LOCK(ifq) \ | |
187 | ALTQ_SQ_LOCK(&(ifq)->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT]) | |
188 | /* COMPAT */ | |
189 | #define ALTQ_UNLOCK(ifq) \ | |
190 | ALTQ_SQ_UNLOCK(&(ifq)->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT]) | |
191 | #endif | |
4d723e5a JS |
192 | |
193 | #ifdef _KERNEL | |
194 | ||
195 | /* | |
196 | * packet attributes used by queueing disciplines. | |
197 | * pattr_class is a discipline-dependent scheduling class that is | |
198 | * set by a classifier. | |
199 | * pattr_hdr and pattr_af may be used by a discipline to access | |
200 | * the header within a mbuf. (e.g. ECN needs to update the CE bit) | |
201 | * note that pattr_hdr could be stale after m_pullup, though link | |
202 | * layer output routines usually don't use m_pullup. link-level | |
203 | * compression also invalidates these fields. thus, pattr_hdr needs | |
204 | * to be verified when a discipline touches the header. | |
205 | */ | |
206 | struct altq_pktattr { | |
207 | void *pattr_class; /* sched class set by classifier */ | |
208 | int pattr_af; /* address family */ | |
209 | caddr_t pattr_hdr; /* saved header position in mbuf */ | |
210 | }; | |
211 | ||
212 | /* | |
213 | * a token-bucket regulator limits the rate that a network driver can | |
214 | * dequeue packets from the output queue. | |
215 | * modern cards are able to buffer a large amount of packets and dequeue | |
216 | * too many packets at a time. this bursty dequeue behavior makes it | |
217 | * impossible to schedule packets by queueing disciplines. | |
218 | * a token-bucket is used to control the burst size in a device | |
219 | * independent manner. | |
220 | */ | |
221 | struct tb_regulator { | |
222 | int64_t tbr_rate; /* (scaled) token bucket rate */ | |
223 | int64_t tbr_depth; /* (scaled) token bucket depth */ | |
224 | ||
225 | int64_t tbr_token; /* (scaled) current token */ | |
226 | int64_t tbr_filluptime; /* (scaled) time to fill up bucket */ | |
227 | uint64_t tbr_last; /* last time token was updated */ | |
228 | ||
229 | int tbr_lastop; /* last dequeue operation type | |
230 | needed for poll-and-dequeue */ | |
231 | }; | |
232 | ||
233 | /* if_altqflags */ | |
234 | #define ALTQF_READY 0x01 /* driver supports alternate queueing */ | |
235 | #define ALTQF_ENABLED 0x02 /* altq is in use */ | |
236 | #define ALTQF_CLASSIFY 0x04 /* classify packets */ | |
237 | #define ALTQF_DRIVER1 0x40 /* driver specific */ | |
238 | ||
239 | /* if_altqflags set internally only: */ | |
240 | #define ALTQF_CANTCHANGE (ALTQF_READY) | |
241 | ||
242 | /* altq_dequeue 2nd arg */ | |
243 | #define ALTDQ_REMOVE 1 /* dequeue mbuf from the queue */ | |
244 | #define ALTDQ_POLL 2 /* don't dequeue mbuf from the queue */ | |
245 | ||
246 | /* altq request types (currently only purge is defined) */ | |
247 | #define ALTRQ_PURGE 1 /* purge all packets */ | |
248 | ||
2cc2f639 | 249 | int altq_attach(struct ifaltq *, int, void *, altq_mapsubq_t, |
f0a26983 SZ |
250 | ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t, void *, |
251 | void *(*)(struct ifaltq *, struct mbuf *, struct altq_pktattr *)); | |
4d723e5a JS |
252 | int altq_detach(struct ifaltq *); |
253 | int altq_enable(struct ifaltq *); | |
254 | int altq_disable(struct ifaltq *); | |
ac9843a1 | 255 | struct mbuf *tbr_dequeue(struct ifaltq_subque *, int); |
4d723e5a JS |
256 | extern int (*altq_input)(struct mbuf *, int); |
257 | #endif /* _KERNEL */ | |
258 | ||
1bd40720 | 259 | #endif /* _NET_ALTQ_IF_ALTQ_H_ */ |