altq_cdnr.c revision 1.2.2.2 1 1.2.2.2 bouyer /* $NetBSD: altq_cdnr.c,v 1.2.2.2 2001/01/05 17:39:36 bouyer Exp $ */
2 1.2.2.2 bouyer /* $KAME: altq_cdnr.c,v 1.8 2000/12/14 08:12:45 thorpej Exp $ */
3 1.2.2.2 bouyer
4 1.2.2.2 bouyer /*
5 1.2.2.2 bouyer * Copyright (C) 1999-2000
6 1.2.2.2 bouyer * Sony Computer Science Laboratories Inc. All rights reserved.
7 1.2.2.2 bouyer *
8 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
9 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
10 1.2.2.2 bouyer * are met:
11 1.2.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
13 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
16 1.2.2.2 bouyer *
17 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 1.2.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.2.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.2.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 1.2.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.2.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.2.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.2.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.2.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.2.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.2.2.2 bouyer * SUCH DAMAGE.
28 1.2.2.2 bouyer */
29 1.2.2.2 bouyer
30 1.2.2.2 bouyer #if defined(__FreeBSD__) || defined(__NetBSD__)
31 1.2.2.2 bouyer #include "opt_altq.h"
32 1.2.2.2 bouyer #if (__FreeBSD__ != 2)
33 1.2.2.2 bouyer #include "opt_inet.h"
34 1.2.2.2 bouyer #ifdef __FreeBSD__
35 1.2.2.2 bouyer #include "opt_inet6.h"
36 1.2.2.2 bouyer #endif
37 1.2.2.2 bouyer #endif
38 1.2.2.2 bouyer #endif /* __FreeBSD__ || __NetBSD__ */
39 1.2.2.2 bouyer
40 1.2.2.2 bouyer #include <sys/param.h>
41 1.2.2.2 bouyer #include <sys/malloc.h>
42 1.2.2.2 bouyer #include <sys/mbuf.h>
43 1.2.2.2 bouyer #include <sys/socket.h>
44 1.2.2.2 bouyer #include <sys/sockio.h>
45 1.2.2.2 bouyer #include <sys/systm.h>
46 1.2.2.2 bouyer #include <sys/proc.h>
47 1.2.2.2 bouyer #include <sys/errno.h>
48 1.2.2.2 bouyer #include <sys/kernel.h>
49 1.2.2.2 bouyer #include <sys/queue.h>
50 1.2.2.2 bouyer
51 1.2.2.2 bouyer #include <net/if.h>
52 1.2.2.2 bouyer #include <net/if_types.h>
53 1.2.2.2 bouyer #include <netinet/in.h>
54 1.2.2.2 bouyer #include <netinet/in_systm.h>
55 1.2.2.2 bouyer #include <netinet/ip.h>
56 1.2.2.2 bouyer #ifdef INET6
57 1.2.2.2 bouyer #include <netinet/ip6.h>
58 1.2.2.2 bouyer #endif
59 1.2.2.2 bouyer
60 1.2.2.2 bouyer #include <altq/altq.h>
61 1.2.2.2 bouyer #include <altq/altq_conf.h>
62 1.2.2.2 bouyer #include <altq/altq_cdnr.h>
63 1.2.2.2 bouyer
64 1.2.2.2 bouyer /*
65 1.2.2.2 bouyer * diffserv traffic conditioning module
66 1.2.2.2 bouyer */
67 1.2.2.2 bouyer
68 1.2.2.2 bouyer int altq_cdnr_enabled = 0;
69 1.2.2.2 bouyer
70 1.2.2.2 bouyer /* traffic conditioner is enabled by ALTQ_CDNR option in opt_altq.h */
71 1.2.2.2 bouyer #ifdef ALTQ_CDNR
72 1.2.2.2 bouyer
73 1.2.2.2 bouyer /* cdnr_list keeps all cdnr's allocated. */
74 1.2.2.2 bouyer static LIST_HEAD(, top_cdnr) tcb_list;
75 1.2.2.2 bouyer
76 1.2.2.2 bouyer int cdnropen __P((dev_t, int, int, struct proc *));
77 1.2.2.2 bouyer int cdnrclose __P((dev_t, int, int, struct proc *));
78 1.2.2.2 bouyer int cdnrioctl __P((dev_t, ioctlcmd_t, caddr_t, int, struct proc *));
79 1.2.2.2 bouyer
80 1.2.2.2 bouyer static int altq_cdnr_input __P((struct mbuf *, int));
81 1.2.2.2 bouyer static struct top_cdnr *tcb_lookup __P((char *ifname));
82 1.2.2.2 bouyer static struct cdnr_block *cdnr_handle2cb __P((u_long));
83 1.2.2.2 bouyer static u_long cdnr_cb2handle __P((struct cdnr_block *));
84 1.2.2.2 bouyer static void *cdnr_cballoc __P((struct top_cdnr *, int,
85 1.2.2.2 bouyer struct tc_action *(*)(struct cdnr_block *, struct cdnr_pktinfo *)));
86 1.2.2.2 bouyer static void cdnr_cbdestroy __P((void *));
87 1.2.2.2 bouyer static int tca_verify_action __P((struct tc_action *));
88 1.2.2.2 bouyer static void tca_import_action __P((struct tc_action *, struct tc_action *));
89 1.2.2.2 bouyer static void tca_invalidate_action __P((struct tc_action *));
90 1.2.2.2 bouyer
91 1.2.2.2 bouyer static int generic_element_destroy __P((struct cdnr_block *));
92 1.2.2.2 bouyer static struct top_cdnr *top_create __P((struct ifaltq *));
93 1.2.2.2 bouyer static int top_destroy __P((struct top_cdnr *));
94 1.2.2.2 bouyer static struct cdnr_block *element_create __P((struct top_cdnr *,
95 1.2.2.2 bouyer struct tc_action *));
96 1.2.2.2 bouyer static int element_destroy __P((struct cdnr_block *));
97 1.2.2.2 bouyer static void tb_import_profile __P((struct tbe *, struct tb_profile *));
98 1.2.2.2 bouyer static struct tbmeter *tbm_create __P((struct top_cdnr *, struct tb_profile *,
99 1.2.2.2 bouyer struct tc_action *, struct tc_action *));
100 1.2.2.2 bouyer static int tbm_destroy __P((struct tbmeter *));
101 1.2.2.2 bouyer static struct tc_action *tbm_input __P((struct cdnr_block *,
102 1.2.2.2 bouyer struct cdnr_pktinfo *));
103 1.2.2.2 bouyer static struct trtcm *trtcm_create __P((struct top_cdnr *,
104 1.2.2.2 bouyer struct tb_profile *, struct tb_profile *,
105 1.2.2.2 bouyer struct tc_action *, struct tc_action *, struct tc_action *,
106 1.2.2.2 bouyer int));
107 1.2.2.2 bouyer static int trtcm_destroy __P((struct trtcm *));
108 1.2.2.2 bouyer static struct tc_action *trtcm_input __P((struct cdnr_block *,
109 1.2.2.2 bouyer struct cdnr_pktinfo *));
110 1.2.2.2 bouyer static struct tswtcm *tswtcm_create __P((struct top_cdnr *,
111 1.2.2.2 bouyer u_int32_t, u_int32_t, u_int32_t,
112 1.2.2.2 bouyer struct tc_action *, struct tc_action *, struct tc_action *));
113 1.2.2.2 bouyer static int tswtcm_destroy __P((struct tswtcm *));
114 1.2.2.2 bouyer static struct tc_action *tswtcm_input __P((struct cdnr_block *,
115 1.2.2.2 bouyer struct cdnr_pktinfo *));
116 1.2.2.2 bouyer
117 1.2.2.2 bouyer static int cdnrcmd_if_attach __P((char *));
118 1.2.2.2 bouyer static int cdnrcmd_if_detach __P((char *));
119 1.2.2.2 bouyer static int cdnrcmd_add_element __P((struct cdnr_add_element *));
120 1.2.2.2 bouyer static int cdnrcmd_delete_element __P((struct cdnr_delete_element *));
121 1.2.2.2 bouyer static int cdnrcmd_add_filter __P((struct cdnr_add_filter *));
122 1.2.2.2 bouyer static int cdnrcmd_delete_filter __P((struct cdnr_delete_filter *));
123 1.2.2.2 bouyer static int cdnrcmd_add_tbm __P((struct cdnr_add_tbmeter *));
124 1.2.2.2 bouyer static int cdnrcmd_modify_tbm __P((struct cdnr_modify_tbmeter *));
125 1.2.2.2 bouyer static int cdnrcmd_tbm_stats __P((struct cdnr_tbmeter_stats *));
126 1.2.2.2 bouyer static int cdnrcmd_add_trtcm __P((struct cdnr_add_trtcm *));
127 1.2.2.2 bouyer static int cdnrcmd_modify_trtcm __P((struct cdnr_modify_trtcm *));
128 1.2.2.2 bouyer static int cdnrcmd_tcm_stats __P((struct cdnr_tcm_stats *));
129 1.2.2.2 bouyer static int cdnrcmd_add_tswtcm __P((struct cdnr_add_tswtcm *));
130 1.2.2.2 bouyer static int cdnrcmd_modify_tswtcm __P((struct cdnr_modify_tswtcm *));
131 1.2.2.2 bouyer static int cdnrcmd_get_stats __P((struct cdnr_get_stats *));
132 1.2.2.2 bouyer
133 1.2.2.2 bouyer /*
134 1.2.2.2 bouyer * top level input function called from ip_input.
135 1.2.2.2 bouyer * should be called before converting header fields to host-byte-order.
136 1.2.2.2 bouyer */
137 1.2.2.2 bouyer int
138 1.2.2.2 bouyer altq_cdnr_input(m, af)
139 1.2.2.2 bouyer struct mbuf *m;
140 1.2.2.2 bouyer int af; /* address family */
141 1.2.2.2 bouyer {
142 1.2.2.2 bouyer struct ifnet *ifp;
143 1.2.2.2 bouyer struct ip *ip;
144 1.2.2.2 bouyer struct top_cdnr *top;
145 1.2.2.2 bouyer struct tc_action *tca;
146 1.2.2.2 bouyer struct cdnr_block *cb;
147 1.2.2.2 bouyer struct cdnr_pktinfo pktinfo;
148 1.2.2.2 bouyer
149 1.2.2.2 bouyer ifp = m->m_pkthdr.rcvif;
150 1.2.2.2 bouyer if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
151 1.2.2.2 bouyer /* traffic conditioner is not enabled on this interface */
152 1.2.2.2 bouyer return (1);
153 1.2.2.2 bouyer
154 1.2.2.2 bouyer top = ifp->if_snd.altq_cdnr;
155 1.2.2.2 bouyer
156 1.2.2.2 bouyer ip = mtod(m, struct ip *);
157 1.2.2.2 bouyer #ifdef INET6
158 1.2.2.2 bouyer if (af == AF_INET6) {
159 1.2.2.2 bouyer u_int32_t flowlabel;
160 1.2.2.2 bouyer
161 1.2.2.2 bouyer flowlabel = ((struct ip6_hdr *)ip)->ip6_flow;
162 1.2.2.2 bouyer pktinfo.pkt_dscp = (ntohl(flowlabel) >> 20) & DSCP_MASK;
163 1.2.2.2 bouyer } else
164 1.2.2.2 bouyer #endif
165 1.2.2.2 bouyer pktinfo.pkt_dscp = ip->ip_tos & DSCP_MASK;
166 1.2.2.2 bouyer pktinfo.pkt_len = m_pktlen(m);
167 1.2.2.2 bouyer
168 1.2.2.2 bouyer tca = NULL;
169 1.2.2.2 bouyer
170 1.2.2.2 bouyer cb = acc_classify(&top->tc_classifier, m, af);
171 1.2.2.2 bouyer if (cb != NULL)
172 1.2.2.2 bouyer tca = &cb->cb_action;
173 1.2.2.2 bouyer
174 1.2.2.2 bouyer if (tca == NULL)
175 1.2.2.2 bouyer tca = &top->tc_block.cb_action;
176 1.2.2.2 bouyer
177 1.2.2.2 bouyer while (1) {
178 1.2.2.2 bouyer PKTCNTR_ADD(&top->tc_cnts[tca->tca_code], pktinfo.pkt_len);
179 1.2.2.2 bouyer
180 1.2.2.2 bouyer switch (tca->tca_code) {
181 1.2.2.2 bouyer case TCACODE_PASS:
182 1.2.2.2 bouyer return (1);
183 1.2.2.2 bouyer case TCACODE_DROP:
184 1.2.2.2 bouyer m_freem(m);
185 1.2.2.2 bouyer return (0);
186 1.2.2.2 bouyer case TCACODE_RETURN:
187 1.2.2.2 bouyer return (0);
188 1.2.2.2 bouyer case TCACODE_MARK:
189 1.2.2.2 bouyer #ifdef INET6
190 1.2.2.2 bouyer if (af == AF_INET6) {
191 1.2.2.2 bouyer struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
192 1.2.2.2 bouyer u_int32_t flowlabel;
193 1.2.2.2 bouyer
194 1.2.2.2 bouyer flowlabel = ntohl(ip6->ip6_flow);
195 1.2.2.2 bouyer flowlabel = (tca->tca_dscp << 20) |
196 1.2.2.2 bouyer (flowlabel & ~(DSCP_MASK << 20));
197 1.2.2.2 bouyer ip6->ip6_flow = htonl(flowlabel);
198 1.2.2.2 bouyer } else
199 1.2.2.2 bouyer #endif
200 1.2.2.2 bouyer ip->ip_tos = tca->tca_dscp |
201 1.2.2.2 bouyer (ip->ip_tos & DSCP_CUMASK);
202 1.2.2.2 bouyer return (1);
203 1.2.2.2 bouyer case TCACODE_NEXT:
204 1.2.2.2 bouyer cb = tca->tca_next;
205 1.2.2.2 bouyer tca = (*cb->cb_input)(cb, &pktinfo);
206 1.2.2.2 bouyer break;
207 1.2.2.2 bouyer case TCACODE_NONE:
208 1.2.2.2 bouyer default:
209 1.2.2.2 bouyer return (1);
210 1.2.2.2 bouyer }
211 1.2.2.2 bouyer }
212 1.2.2.2 bouyer }
213 1.2.2.2 bouyer
214 1.2.2.2 bouyer static struct top_cdnr *
215 1.2.2.2 bouyer tcb_lookup(ifname)
216 1.2.2.2 bouyer char *ifname;
217 1.2.2.2 bouyer {
218 1.2.2.2 bouyer struct top_cdnr *top;
219 1.2.2.2 bouyer struct ifnet *ifp;
220 1.2.2.2 bouyer
221 1.2.2.2 bouyer if ((ifp = ifunit(ifname)) != NULL)
222 1.2.2.2 bouyer LIST_FOREACH(top, &tcb_list, tc_next)
223 1.2.2.2 bouyer if (top->tc_ifq->altq_ifp == ifp)
224 1.2.2.2 bouyer return (top);
225 1.2.2.2 bouyer return (NULL);
226 1.2.2.2 bouyer }
227 1.2.2.2 bouyer
228 1.2.2.2 bouyer static struct cdnr_block *
229 1.2.2.2 bouyer cdnr_handle2cb(handle)
230 1.2.2.2 bouyer u_long handle;
231 1.2.2.2 bouyer {
232 1.2.2.2 bouyer struct cdnr_block *cb;
233 1.2.2.2 bouyer
234 1.2.2.2 bouyer cb = (struct cdnr_block *)handle;
235 1.2.2.2 bouyer if (handle != ALIGN(cb))
236 1.2.2.2 bouyer return (NULL);
237 1.2.2.2 bouyer
238 1.2.2.2 bouyer if (cb == NULL || cb->cb_handle != handle)
239 1.2.2.2 bouyer return (NULL);
240 1.2.2.2 bouyer return (cb);
241 1.2.2.2 bouyer }
242 1.2.2.2 bouyer
243 1.2.2.2 bouyer static u_long
244 1.2.2.2 bouyer cdnr_cb2handle(cb)
245 1.2.2.2 bouyer struct cdnr_block *cb;
246 1.2.2.2 bouyer {
247 1.2.2.2 bouyer return (cb->cb_handle);
248 1.2.2.2 bouyer }
249 1.2.2.2 bouyer
250 1.2.2.2 bouyer static void *
251 1.2.2.2 bouyer cdnr_cballoc(top, type, input_func)
252 1.2.2.2 bouyer struct top_cdnr *top;
253 1.2.2.2 bouyer int type;
254 1.2.2.2 bouyer struct tc_action *(*input_func)(struct cdnr_block *,
255 1.2.2.2 bouyer struct cdnr_pktinfo *);
256 1.2.2.2 bouyer {
257 1.2.2.2 bouyer struct cdnr_block *cb;
258 1.2.2.2 bouyer int size;
259 1.2.2.2 bouyer
260 1.2.2.2 bouyer switch (type) {
261 1.2.2.2 bouyer case TCETYPE_TOP:
262 1.2.2.2 bouyer size = sizeof(struct top_cdnr);
263 1.2.2.2 bouyer break;
264 1.2.2.2 bouyer case TCETYPE_ELEMENT:
265 1.2.2.2 bouyer size = sizeof(struct cdnr_block);
266 1.2.2.2 bouyer break;
267 1.2.2.2 bouyer case TCETYPE_TBMETER:
268 1.2.2.2 bouyer size = sizeof(struct tbmeter);
269 1.2.2.2 bouyer break;
270 1.2.2.2 bouyer case TCETYPE_TRTCM:
271 1.2.2.2 bouyer size = sizeof(struct trtcm);
272 1.2.2.2 bouyer break;
273 1.2.2.2 bouyer case TCETYPE_TSWTCM:
274 1.2.2.2 bouyer size = sizeof(struct tswtcm);
275 1.2.2.2 bouyer break;
276 1.2.2.2 bouyer default:
277 1.2.2.2 bouyer return (NULL);
278 1.2.2.2 bouyer }
279 1.2.2.2 bouyer
280 1.2.2.2 bouyer MALLOC(cb, struct cdnr_block *, size, M_DEVBUF, M_WAITOK);
281 1.2.2.2 bouyer if (cb == NULL)
282 1.2.2.2 bouyer return (NULL);
283 1.2.2.2 bouyer bzero(cb, size);
284 1.2.2.2 bouyer
285 1.2.2.2 bouyer cb->cb_len = size;
286 1.2.2.2 bouyer cb->cb_type = type;
287 1.2.2.2 bouyer cb->cb_ref = 0;
288 1.2.2.2 bouyer cb->cb_handle = (u_long)cb;
289 1.2.2.2 bouyer if (top == NULL)
290 1.2.2.2 bouyer cb->cb_top = (struct top_cdnr *)cb;
291 1.2.2.2 bouyer else
292 1.2.2.2 bouyer cb->cb_top = top;
293 1.2.2.2 bouyer
294 1.2.2.2 bouyer if (input_func != NULL) {
295 1.2.2.2 bouyer /*
296 1.2.2.2 bouyer * if this cdnr has an action function,
297 1.2.2.2 bouyer * make tc_action to call itself.
298 1.2.2.2 bouyer */
299 1.2.2.2 bouyer cb->cb_action.tca_code = TCACODE_NEXT;
300 1.2.2.2 bouyer cb->cb_action.tca_next = cb;
301 1.2.2.2 bouyer cb->cb_input = input_func;
302 1.2.2.2 bouyer } else
303 1.2.2.2 bouyer cb->cb_action.tca_code = TCACODE_NONE;
304 1.2.2.2 bouyer
305 1.2.2.2 bouyer /* if this isn't top, register the element to the top level cdnr */
306 1.2.2.2 bouyer if (top != NULL)
307 1.2.2.2 bouyer LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
308 1.2.2.2 bouyer
309 1.2.2.2 bouyer return ((void *)cb);
310 1.2.2.2 bouyer }
311 1.2.2.2 bouyer
312 1.2.2.2 bouyer static void
313 1.2.2.2 bouyer cdnr_cbdestroy(cblock)
314 1.2.2.2 bouyer void *cblock;
315 1.2.2.2 bouyer {
316 1.2.2.2 bouyer struct cdnr_block *cb = cblock;
317 1.2.2.2 bouyer
318 1.2.2.2 bouyer /* delete filters belonging to this cdnr */
319 1.2.2.2 bouyer acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
320 1.2.2.2 bouyer
321 1.2.2.2 bouyer /* remove from the top level cdnr */
322 1.2.2.2 bouyer if (cb->cb_top != cblock)
323 1.2.2.2 bouyer LIST_REMOVE(cb, cb_next);
324 1.2.2.2 bouyer
325 1.2.2.2 bouyer FREE(cb, M_DEVBUF);
326 1.2.2.2 bouyer }
327 1.2.2.2 bouyer
328 1.2.2.2 bouyer /*
329 1.2.2.2 bouyer * conditioner common destroy routine
330 1.2.2.2 bouyer */
331 1.2.2.2 bouyer static int
332 1.2.2.2 bouyer generic_element_destroy(cb)
333 1.2.2.2 bouyer struct cdnr_block *cb;
334 1.2.2.2 bouyer {
335 1.2.2.2 bouyer int error = 0;
336 1.2.2.2 bouyer
337 1.2.2.2 bouyer switch (cb->cb_type) {
338 1.2.2.2 bouyer case TCETYPE_TOP:
339 1.2.2.2 bouyer error = top_destroy((struct top_cdnr *)cb);
340 1.2.2.2 bouyer break;
341 1.2.2.2 bouyer case TCETYPE_ELEMENT:
342 1.2.2.2 bouyer error = element_destroy(cb);
343 1.2.2.2 bouyer break;
344 1.2.2.2 bouyer case TCETYPE_TBMETER:
345 1.2.2.2 bouyer error = tbm_destroy((struct tbmeter *)cb);
346 1.2.2.2 bouyer break;
347 1.2.2.2 bouyer case TCETYPE_TRTCM:
348 1.2.2.2 bouyer error = trtcm_destroy((struct trtcm *)cb);
349 1.2.2.2 bouyer break;
350 1.2.2.2 bouyer case TCETYPE_TSWTCM:
351 1.2.2.2 bouyer error = tswtcm_destroy((struct tswtcm *)cb);
352 1.2.2.2 bouyer break;
353 1.2.2.2 bouyer default:
354 1.2.2.2 bouyer error = EINVAL;
355 1.2.2.2 bouyer }
356 1.2.2.2 bouyer return (error);
357 1.2.2.2 bouyer }
358 1.2.2.2 bouyer
359 1.2.2.2 bouyer static int
360 1.2.2.2 bouyer tca_verify_action(utca)
361 1.2.2.2 bouyer struct tc_action *utca;
362 1.2.2.2 bouyer {
363 1.2.2.2 bouyer switch (utca->tca_code) {
364 1.2.2.2 bouyer case TCACODE_PASS:
365 1.2.2.2 bouyer case TCACODE_DROP:
366 1.2.2.2 bouyer case TCACODE_MARK:
367 1.2.2.2 bouyer /* these are ok */
368 1.2.2.2 bouyer break;
369 1.2.2.2 bouyer
370 1.2.2.2 bouyer case TCACODE_HANDLE:
371 1.2.2.2 bouyer /* verify handle value */
372 1.2.2.2 bouyer if (cdnr_handle2cb(utca->tca_handle) == NULL)
373 1.2.2.2 bouyer return (-1);
374 1.2.2.2 bouyer break;
375 1.2.2.2 bouyer
376 1.2.2.2 bouyer case TCACODE_NONE:
377 1.2.2.2 bouyer case TCACODE_RETURN:
378 1.2.2.2 bouyer case TCACODE_NEXT:
379 1.2.2.2 bouyer default:
380 1.2.2.2 bouyer /* should not be passed from a user */
381 1.2.2.2 bouyer return (-1);
382 1.2.2.2 bouyer }
383 1.2.2.2 bouyer return (0);
384 1.2.2.2 bouyer }
385 1.2.2.2 bouyer
386 1.2.2.2 bouyer static void
387 1.2.2.2 bouyer tca_import_action(ktca, utca)
388 1.2.2.2 bouyer struct tc_action *ktca, *utca;
389 1.2.2.2 bouyer {
390 1.2.2.2 bouyer struct cdnr_block *cb;
391 1.2.2.2 bouyer
392 1.2.2.2 bouyer *ktca = *utca;
393 1.2.2.2 bouyer if (ktca->tca_code == TCACODE_HANDLE) {
394 1.2.2.2 bouyer cb = cdnr_handle2cb(ktca->tca_handle);
395 1.2.2.2 bouyer if (cb == NULL) {
396 1.2.2.2 bouyer ktca->tca_code = TCACODE_NONE;
397 1.2.2.2 bouyer return;
398 1.2.2.2 bouyer }
399 1.2.2.2 bouyer ktca->tca_code = TCACODE_NEXT;
400 1.2.2.2 bouyer ktca->tca_next = cb;
401 1.2.2.2 bouyer cb->cb_ref++;
402 1.2.2.2 bouyer } else if (ktca->tca_code == TCACODE_MARK) {
403 1.2.2.2 bouyer ktca->tca_dscp &= DSCP_MASK;
404 1.2.2.2 bouyer }
405 1.2.2.2 bouyer return;
406 1.2.2.2 bouyer }
407 1.2.2.2 bouyer
408 1.2.2.2 bouyer static void
409 1.2.2.2 bouyer tca_invalidate_action(tca)
410 1.2.2.2 bouyer struct tc_action *tca;
411 1.2.2.2 bouyer {
412 1.2.2.2 bouyer struct cdnr_block *cb;
413 1.2.2.2 bouyer
414 1.2.2.2 bouyer if (tca->tca_code == TCACODE_NEXT) {
415 1.2.2.2 bouyer cb = tca->tca_next;
416 1.2.2.2 bouyer if (cb == NULL)
417 1.2.2.2 bouyer return;
418 1.2.2.2 bouyer cb->cb_ref--;
419 1.2.2.2 bouyer }
420 1.2.2.2 bouyer tca->tca_code = TCACODE_NONE;
421 1.2.2.2 bouyer }
422 1.2.2.2 bouyer
423 1.2.2.2 bouyer /*
424 1.2.2.2 bouyer * top level traffic conditioner
425 1.2.2.2 bouyer */
426 1.2.2.2 bouyer static struct top_cdnr *
427 1.2.2.2 bouyer top_create(ifq)
428 1.2.2.2 bouyer struct ifaltq *ifq;
429 1.2.2.2 bouyer {
430 1.2.2.2 bouyer struct top_cdnr *top;
431 1.2.2.2 bouyer
432 1.2.2.2 bouyer if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
433 1.2.2.2 bouyer return (NULL);
434 1.2.2.2 bouyer
435 1.2.2.2 bouyer top->tc_ifq = ifq;
436 1.2.2.2 bouyer /* set default action for the top level conditioner */
437 1.2.2.2 bouyer top->tc_block.cb_action.tca_code = TCACODE_PASS;
438 1.2.2.2 bouyer
439 1.2.2.2 bouyer LIST_INSERT_HEAD(&tcb_list, top, tc_next);
440 1.2.2.2 bouyer
441 1.2.2.2 bouyer ifq->altq_cdnr = top;
442 1.2.2.2 bouyer
443 1.2.2.2 bouyer return (top);
444 1.2.2.2 bouyer }
445 1.2.2.2 bouyer
446 1.2.2.2 bouyer static int
447 1.2.2.2 bouyer top_destroy(top)
448 1.2.2.2 bouyer struct top_cdnr *top;
449 1.2.2.2 bouyer {
450 1.2.2.2 bouyer struct cdnr_block *cb;
451 1.2.2.2 bouyer
452 1.2.2.2 bouyer if (ALTQ_IS_CNDTNING(top->tc_ifq))
453 1.2.2.2 bouyer ALTQ_CLEAR_CNDTNING(top->tc_ifq);
454 1.2.2.2 bouyer top->tc_ifq->altq_cdnr = NULL;
455 1.2.2.2 bouyer
456 1.2.2.2 bouyer /*
457 1.2.2.2 bouyer * destroy all the conditioner elements belonging to this interface
458 1.2.2.2 bouyer */
459 1.2.2.2 bouyer while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
460 1.2.2.2 bouyer while (cb != NULL && cb->cb_ref > 0)
461 1.2.2.2 bouyer cb = LIST_NEXT(cb, cb_next);
462 1.2.2.2 bouyer if (cb != NULL)
463 1.2.2.2 bouyer generic_element_destroy(cb);
464 1.2.2.2 bouyer }
465 1.2.2.2 bouyer
466 1.2.2.2 bouyer LIST_REMOVE(top, tc_next);
467 1.2.2.2 bouyer
468 1.2.2.2 bouyer cdnr_cbdestroy(top);
469 1.2.2.2 bouyer
470 1.2.2.2 bouyer /* if there is no active conditioner, remove the input hook */
471 1.2.2.2 bouyer if (altq_input != NULL) {
472 1.2.2.2 bouyer LIST_FOREACH(top, &tcb_list, tc_next)
473 1.2.2.2 bouyer if (ALTQ_IS_CNDTNING(top->tc_ifq))
474 1.2.2.2 bouyer break;
475 1.2.2.2 bouyer if (top == NULL)
476 1.2.2.2 bouyer altq_input = NULL;
477 1.2.2.2 bouyer }
478 1.2.2.2 bouyer
479 1.2.2.2 bouyer return (0);
480 1.2.2.2 bouyer }
481 1.2.2.2 bouyer
482 1.2.2.2 bouyer /*
483 1.2.2.2 bouyer * simple tc elements without input function (e.g., dropper and makers).
484 1.2.2.2 bouyer */
485 1.2.2.2 bouyer static struct cdnr_block *
486 1.2.2.2 bouyer element_create(top, action)
487 1.2.2.2 bouyer struct top_cdnr *top;
488 1.2.2.2 bouyer struct tc_action *action;
489 1.2.2.2 bouyer {
490 1.2.2.2 bouyer struct cdnr_block *cb;
491 1.2.2.2 bouyer
492 1.2.2.2 bouyer if (tca_verify_action(action) < 0)
493 1.2.2.2 bouyer return (NULL);
494 1.2.2.2 bouyer
495 1.2.2.2 bouyer if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
496 1.2.2.2 bouyer return (NULL);
497 1.2.2.2 bouyer
498 1.2.2.2 bouyer tca_import_action(&cb->cb_action, action);
499 1.2.2.2 bouyer
500 1.2.2.2 bouyer return (cb);
501 1.2.2.2 bouyer }
502 1.2.2.2 bouyer
503 1.2.2.2 bouyer static int
504 1.2.2.2 bouyer element_destroy(cb)
505 1.2.2.2 bouyer struct cdnr_block *cb;
506 1.2.2.2 bouyer {
507 1.2.2.2 bouyer if (cb->cb_ref > 0)
508 1.2.2.2 bouyer return (EBUSY);
509 1.2.2.2 bouyer
510 1.2.2.2 bouyer tca_invalidate_action(&cb->cb_action);
511 1.2.2.2 bouyer
512 1.2.2.2 bouyer cdnr_cbdestroy(cb);
513 1.2.2.2 bouyer return (0);
514 1.2.2.2 bouyer }
515 1.2.2.2 bouyer
516 1.2.2.2 bouyer /*
517 1.2.2.2 bouyer * internal representation of token bucket parameters
518 1.2.2.2 bouyer * rate: byte_per_unittime << 32
519 1.2.2.2 bouyer * (((bits_per_sec) / 8) << 32) / machclk_freq
520 1.2.2.2 bouyer * depth: byte << 32
521 1.2.2.2 bouyer *
522 1.2.2.2 bouyer */
523 1.2.2.2 bouyer #define TB_SHIFT 32
524 1.2.2.2 bouyer #define TB_SCALE(x) ((u_int64_t)(x) << TB_SHIFT)
525 1.2.2.2 bouyer #define TB_UNSCALE(x) ((x) >> TB_SHIFT)
526 1.2.2.2 bouyer
527 1.2.2.2 bouyer static void
528 1.2.2.2 bouyer tb_import_profile(tb, profile)
529 1.2.2.2 bouyer struct tbe *tb;
530 1.2.2.2 bouyer struct tb_profile *profile;
531 1.2.2.2 bouyer {
532 1.2.2.2 bouyer tb->rate = TB_SCALE(profile->rate / 8) / machclk_freq;
533 1.2.2.2 bouyer tb->depth = TB_SCALE(profile->depth);
534 1.2.2.2 bouyer if (tb->rate > 0)
535 1.2.2.2 bouyer tb->filluptime = tb->depth / tb->rate;
536 1.2.2.2 bouyer else
537 1.2.2.2 bouyer tb->filluptime = 0xffffffffffffffffLL;
538 1.2.2.2 bouyer tb->token = tb->depth;
539 1.2.2.2 bouyer tb->last = read_machclk();
540 1.2.2.2 bouyer }
541 1.2.2.2 bouyer
542 1.2.2.2 bouyer /*
543 1.2.2.2 bouyer * simple token bucket meter
544 1.2.2.2 bouyer */
545 1.2.2.2 bouyer static struct tbmeter *
546 1.2.2.2 bouyer tbm_create(top, profile, in_action, out_action)
547 1.2.2.2 bouyer struct top_cdnr *top;
548 1.2.2.2 bouyer struct tb_profile *profile;
549 1.2.2.2 bouyer struct tc_action *in_action, *out_action;
550 1.2.2.2 bouyer {
551 1.2.2.2 bouyer struct tbmeter *tbm = NULL;
552 1.2.2.2 bouyer
553 1.2.2.2 bouyer if (tca_verify_action(in_action) < 0
554 1.2.2.2 bouyer || tca_verify_action(out_action) < 0)
555 1.2.2.2 bouyer return (NULL);
556 1.2.2.2 bouyer
557 1.2.2.2 bouyer if ((tbm = cdnr_cballoc(top, TCETYPE_TBMETER,
558 1.2.2.2 bouyer tbm_input)) == NULL)
559 1.2.2.2 bouyer return (NULL);
560 1.2.2.2 bouyer
561 1.2.2.2 bouyer tb_import_profile(&tbm->tb, profile);
562 1.2.2.2 bouyer
563 1.2.2.2 bouyer tca_import_action(&tbm->in_action, in_action);
564 1.2.2.2 bouyer tca_import_action(&tbm->out_action, out_action);
565 1.2.2.2 bouyer
566 1.2.2.2 bouyer return (tbm);
567 1.2.2.2 bouyer }
568 1.2.2.2 bouyer
569 1.2.2.2 bouyer static int
570 1.2.2.2 bouyer tbm_destroy(tbm)
571 1.2.2.2 bouyer struct tbmeter *tbm;
572 1.2.2.2 bouyer {
573 1.2.2.2 bouyer if (tbm->cdnrblk.cb_ref > 0)
574 1.2.2.2 bouyer return (EBUSY);
575 1.2.2.2 bouyer
576 1.2.2.2 bouyer tca_invalidate_action(&tbm->in_action);
577 1.2.2.2 bouyer tca_invalidate_action(&tbm->out_action);
578 1.2.2.2 bouyer
579 1.2.2.2 bouyer cdnr_cbdestroy(tbm);
580 1.2.2.2 bouyer return (0);
581 1.2.2.2 bouyer }
582 1.2.2.2 bouyer
583 1.2.2.2 bouyer static struct tc_action *
584 1.2.2.2 bouyer tbm_input(cb, pktinfo)
585 1.2.2.2 bouyer struct cdnr_block *cb;
586 1.2.2.2 bouyer struct cdnr_pktinfo *pktinfo;
587 1.2.2.2 bouyer {
588 1.2.2.2 bouyer struct tbmeter *tbm = (struct tbmeter *)cb;
589 1.2.2.2 bouyer u_int64_t len;
590 1.2.2.2 bouyer u_int64_t interval, now;
591 1.2.2.2 bouyer
592 1.2.2.2 bouyer len = TB_SCALE(pktinfo->pkt_len);
593 1.2.2.2 bouyer
594 1.2.2.2 bouyer if (tbm->tb.token < len) {
595 1.2.2.2 bouyer now = read_machclk();
596 1.2.2.2 bouyer interval = now - tbm->tb.last;
597 1.2.2.2 bouyer if (interval >= tbm->tb.filluptime)
598 1.2.2.2 bouyer tbm->tb.token = tbm->tb.depth;
599 1.2.2.2 bouyer else {
600 1.2.2.2 bouyer tbm->tb.token += interval * tbm->tb.rate;
601 1.2.2.2 bouyer if (tbm->tb.token > tbm->tb.depth)
602 1.2.2.2 bouyer tbm->tb.token = tbm->tb.depth;
603 1.2.2.2 bouyer }
604 1.2.2.2 bouyer tbm->tb.last = now;
605 1.2.2.2 bouyer }
606 1.2.2.2 bouyer
607 1.2.2.2 bouyer if (tbm->tb.token < len) {
608 1.2.2.2 bouyer PKTCNTR_ADD(&tbm->out_cnt, pktinfo->pkt_len);
609 1.2.2.2 bouyer return (&tbm->out_action);
610 1.2.2.2 bouyer }
611 1.2.2.2 bouyer
612 1.2.2.2 bouyer tbm->tb.token -= len;
613 1.2.2.2 bouyer PKTCNTR_ADD(&tbm->in_cnt, pktinfo->pkt_len);
614 1.2.2.2 bouyer return (&tbm->in_action);
615 1.2.2.2 bouyer }
616 1.2.2.2 bouyer
617 1.2.2.2 bouyer /*
618 1.2.2.2 bouyer * two rate three color marker
619 1.2.2.2 bouyer * as described in draft-heinanen-diffserv-trtcm-01.txt
620 1.2.2.2 bouyer */
621 1.2.2.2 bouyer static struct trtcm *
622 1.2.2.2 bouyer trtcm_create(top, cmtd_profile, peak_profile,
623 1.2.2.2 bouyer green_action, yellow_action, red_action, coloraware)
624 1.2.2.2 bouyer struct top_cdnr *top;
625 1.2.2.2 bouyer struct tb_profile *cmtd_profile, *peak_profile;
626 1.2.2.2 bouyer struct tc_action *green_action, *yellow_action, *red_action;
627 1.2.2.2 bouyer int coloraware;
628 1.2.2.2 bouyer {
629 1.2.2.2 bouyer struct trtcm *tcm = NULL;
630 1.2.2.2 bouyer
631 1.2.2.2 bouyer if (tca_verify_action(green_action) < 0
632 1.2.2.2 bouyer || tca_verify_action(yellow_action) < 0
633 1.2.2.2 bouyer || tca_verify_action(red_action) < 0)
634 1.2.2.2 bouyer return (NULL);
635 1.2.2.2 bouyer
636 1.2.2.2 bouyer if ((tcm = cdnr_cballoc(top, TCETYPE_TRTCM,
637 1.2.2.2 bouyer trtcm_input)) == NULL)
638 1.2.2.2 bouyer return (NULL);
639 1.2.2.2 bouyer
640 1.2.2.2 bouyer tb_import_profile(&tcm->cmtd_tb, cmtd_profile);
641 1.2.2.2 bouyer tb_import_profile(&tcm->peak_tb, peak_profile);
642 1.2.2.2 bouyer
643 1.2.2.2 bouyer tca_import_action(&tcm->green_action, green_action);
644 1.2.2.2 bouyer tca_import_action(&tcm->yellow_action, yellow_action);
645 1.2.2.2 bouyer tca_import_action(&tcm->red_action, red_action);
646 1.2.2.2 bouyer
647 1.2.2.2 bouyer /* set dscps to use */
648 1.2.2.2 bouyer if (tcm->green_action.tca_code == TCACODE_MARK)
649 1.2.2.2 bouyer tcm->green_dscp = tcm->green_action.tca_dscp & DSCP_MASK;
650 1.2.2.2 bouyer else
651 1.2.2.2 bouyer tcm->green_dscp = DSCP_AF11;
652 1.2.2.2 bouyer if (tcm->yellow_action.tca_code == TCACODE_MARK)
653 1.2.2.2 bouyer tcm->yellow_dscp = tcm->yellow_action.tca_dscp & DSCP_MASK;
654 1.2.2.2 bouyer else
655 1.2.2.2 bouyer tcm->yellow_dscp = DSCP_AF12;
656 1.2.2.2 bouyer if (tcm->red_action.tca_code == TCACODE_MARK)
657 1.2.2.2 bouyer tcm->red_dscp = tcm->red_action.tca_dscp & DSCP_MASK;
658 1.2.2.2 bouyer else
659 1.2.2.2 bouyer tcm->red_dscp = DSCP_AF13;
660 1.2.2.2 bouyer
661 1.2.2.2 bouyer tcm->coloraware = coloraware;
662 1.2.2.2 bouyer
663 1.2.2.2 bouyer return (tcm);
664 1.2.2.2 bouyer }
665 1.2.2.2 bouyer
666 1.2.2.2 bouyer static int
667 1.2.2.2 bouyer trtcm_destroy(tcm)
668 1.2.2.2 bouyer struct trtcm *tcm;
669 1.2.2.2 bouyer {
670 1.2.2.2 bouyer if (tcm->cdnrblk.cb_ref > 0)
671 1.2.2.2 bouyer return (EBUSY);
672 1.2.2.2 bouyer
673 1.2.2.2 bouyer tca_invalidate_action(&tcm->green_action);
674 1.2.2.2 bouyer tca_invalidate_action(&tcm->yellow_action);
675 1.2.2.2 bouyer tca_invalidate_action(&tcm->red_action);
676 1.2.2.2 bouyer
677 1.2.2.2 bouyer cdnr_cbdestroy(tcm);
678 1.2.2.2 bouyer return (0);
679 1.2.2.2 bouyer }
680 1.2.2.2 bouyer
681 1.2.2.2 bouyer static struct tc_action *
682 1.2.2.2 bouyer trtcm_input(cb, pktinfo)
683 1.2.2.2 bouyer struct cdnr_block *cb;
684 1.2.2.2 bouyer struct cdnr_pktinfo *pktinfo;
685 1.2.2.2 bouyer {
686 1.2.2.2 bouyer struct trtcm *tcm = (struct trtcm *)cb;
687 1.2.2.2 bouyer u_int64_t len;
688 1.2.2.2 bouyer u_int64_t interval, now;
689 1.2.2.2 bouyer u_int8_t color;
690 1.2.2.2 bouyer
691 1.2.2.2 bouyer len = TB_SCALE(pktinfo->pkt_len);
692 1.2.2.2 bouyer if (tcm->coloraware) {
693 1.2.2.2 bouyer color = pktinfo->pkt_dscp;
694 1.2.2.2 bouyer if (color != tcm->yellow_dscp && color != tcm->red_dscp)
695 1.2.2.2 bouyer color = tcm->green_dscp;
696 1.2.2.2 bouyer } else {
697 1.2.2.2 bouyer /* if color-blind, precolor it as green */
698 1.2.2.2 bouyer color = tcm->green_dscp;
699 1.2.2.2 bouyer }
700 1.2.2.2 bouyer
701 1.2.2.2 bouyer now = read_machclk();
702 1.2.2.2 bouyer if (tcm->cmtd_tb.token < len) {
703 1.2.2.2 bouyer interval = now - tcm->cmtd_tb.last;
704 1.2.2.2 bouyer if (interval >= tcm->cmtd_tb.filluptime)
705 1.2.2.2 bouyer tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
706 1.2.2.2 bouyer else {
707 1.2.2.2 bouyer tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
708 1.2.2.2 bouyer if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
709 1.2.2.2 bouyer tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
710 1.2.2.2 bouyer }
711 1.2.2.2 bouyer tcm->cmtd_tb.last = now;
712 1.2.2.2 bouyer }
713 1.2.2.2 bouyer if (tcm->peak_tb.token < len) {
714 1.2.2.2 bouyer interval = now - tcm->peak_tb.last;
715 1.2.2.2 bouyer if (interval >= tcm->peak_tb.filluptime)
716 1.2.2.2 bouyer tcm->peak_tb.token = tcm->peak_tb.depth;
717 1.2.2.2 bouyer else {
718 1.2.2.2 bouyer tcm->peak_tb.token += interval * tcm->peak_tb.rate;
719 1.2.2.2 bouyer if (tcm->peak_tb.token > tcm->peak_tb.depth)
720 1.2.2.2 bouyer tcm->peak_tb.token = tcm->peak_tb.depth;
721 1.2.2.2 bouyer }
722 1.2.2.2 bouyer tcm->peak_tb.last = now;
723 1.2.2.2 bouyer }
724 1.2.2.2 bouyer
725 1.2.2.2 bouyer if (color == tcm->red_dscp || tcm->peak_tb.token < len) {
726 1.2.2.2 bouyer pktinfo->pkt_dscp = tcm->red_dscp;
727 1.2.2.2 bouyer PKTCNTR_ADD(&tcm->red_cnt, pktinfo->pkt_len);
728 1.2.2.2 bouyer return (&tcm->red_action);
729 1.2.2.2 bouyer }
730 1.2.2.2 bouyer
731 1.2.2.2 bouyer if (color == tcm->yellow_dscp || tcm->cmtd_tb.token < len) {
732 1.2.2.2 bouyer pktinfo->pkt_dscp = tcm->yellow_dscp;
733 1.2.2.2 bouyer tcm->peak_tb.token -= len;
734 1.2.2.2 bouyer PKTCNTR_ADD(&tcm->yellow_cnt, pktinfo->pkt_len);
735 1.2.2.2 bouyer return (&tcm->yellow_action);
736 1.2.2.2 bouyer }
737 1.2.2.2 bouyer
738 1.2.2.2 bouyer pktinfo->pkt_dscp = tcm->green_dscp;
739 1.2.2.2 bouyer tcm->cmtd_tb.token -= len;
740 1.2.2.2 bouyer tcm->peak_tb.token -= len;
741 1.2.2.2 bouyer PKTCNTR_ADD(&tcm->green_cnt, pktinfo->pkt_len);
742 1.2.2.2 bouyer return (&tcm->green_action);
743 1.2.2.2 bouyer }
744 1.2.2.2 bouyer
745 1.2.2.2 bouyer /*
746 1.2.2.2 bouyer * time sliding window three color marker
747 1.2.2.2 bouyer * as described in draft-fang-diffserv-tc-tswtcm-00.txt
748 1.2.2.2 bouyer */
749 1.2.2.2 bouyer static struct tswtcm *
750 1.2.2.2 bouyer tswtcm_create(top, cmtd_rate, peak_rate, avg_interval,
751 1.2.2.2 bouyer green_action, yellow_action, red_action)
752 1.2.2.2 bouyer struct top_cdnr *top;
753 1.2.2.2 bouyer u_int32_t cmtd_rate, peak_rate, avg_interval;
754 1.2.2.2 bouyer struct tc_action *green_action, *yellow_action, *red_action;
755 1.2.2.2 bouyer {
756 1.2.2.2 bouyer struct tswtcm *tsw;
757 1.2.2.2 bouyer
758 1.2.2.2 bouyer if (tca_verify_action(green_action) < 0
759 1.2.2.2 bouyer || tca_verify_action(yellow_action) < 0
760 1.2.2.2 bouyer || tca_verify_action(red_action) < 0)
761 1.2.2.2 bouyer return (NULL);
762 1.2.2.2 bouyer
763 1.2.2.2 bouyer if ((tsw = cdnr_cballoc(top, TCETYPE_TSWTCM,
764 1.2.2.2 bouyer tswtcm_input)) == NULL)
765 1.2.2.2 bouyer return (NULL);
766 1.2.2.2 bouyer
767 1.2.2.2 bouyer tca_import_action(&tsw->green_action, green_action);
768 1.2.2.2 bouyer tca_import_action(&tsw->yellow_action, yellow_action);
769 1.2.2.2 bouyer tca_import_action(&tsw->red_action, red_action);
770 1.2.2.2 bouyer
771 1.2.2.2 bouyer /* set dscps to use */
772 1.2.2.2 bouyer if (tsw->green_action.tca_code == TCACODE_MARK)
773 1.2.2.2 bouyer tsw->green_dscp = tsw->green_action.tca_dscp & DSCP_MASK;
774 1.2.2.2 bouyer else
775 1.2.2.2 bouyer tsw->green_dscp = DSCP_AF11;
776 1.2.2.2 bouyer if (tsw->yellow_action.tca_code == TCACODE_MARK)
777 1.2.2.2 bouyer tsw->yellow_dscp = tsw->yellow_action.tca_dscp & DSCP_MASK;
778 1.2.2.2 bouyer else
779 1.2.2.2 bouyer tsw->yellow_dscp = DSCP_AF12;
780 1.2.2.2 bouyer if (tsw->red_action.tca_code == TCACODE_MARK)
781 1.2.2.2 bouyer tsw->red_dscp = tsw->red_action.tca_dscp & DSCP_MASK;
782 1.2.2.2 bouyer else
783 1.2.2.2 bouyer tsw->red_dscp = DSCP_AF13;
784 1.2.2.2 bouyer
785 1.2.2.2 bouyer /* convert rates from bits/sec to bytes/sec */
786 1.2.2.2 bouyer tsw->cmtd_rate = cmtd_rate / 8;
787 1.2.2.2 bouyer tsw->peak_rate = peak_rate / 8;
788 1.2.2.2 bouyer tsw->avg_rate = 0;
789 1.2.2.2 bouyer
790 1.2.2.2 bouyer /* timewin is converted from msec to machine clock unit */
791 1.2.2.2 bouyer tsw->timewin = (u_int64_t)machclk_freq * avg_interval / 1000;
792 1.2.2.2 bouyer
793 1.2.2.2 bouyer return (tsw);
794 1.2.2.2 bouyer }
795 1.2.2.2 bouyer
796 1.2.2.2 bouyer static int
797 1.2.2.2 bouyer tswtcm_destroy(tsw)
798 1.2.2.2 bouyer struct tswtcm *tsw;
799 1.2.2.2 bouyer {
800 1.2.2.2 bouyer if (tsw->cdnrblk.cb_ref > 0)
801 1.2.2.2 bouyer return (EBUSY);
802 1.2.2.2 bouyer
803 1.2.2.2 bouyer tca_invalidate_action(&tsw->green_action);
804 1.2.2.2 bouyer tca_invalidate_action(&tsw->yellow_action);
805 1.2.2.2 bouyer tca_invalidate_action(&tsw->red_action);
806 1.2.2.2 bouyer
807 1.2.2.2 bouyer cdnr_cbdestroy(tsw);
808 1.2.2.2 bouyer return (0);
809 1.2.2.2 bouyer }
810 1.2.2.2 bouyer
811 1.2.2.2 bouyer static struct tc_action *
812 1.2.2.2 bouyer tswtcm_input(cb, pktinfo)
813 1.2.2.2 bouyer struct cdnr_block *cb;
814 1.2.2.2 bouyer struct cdnr_pktinfo *pktinfo;
815 1.2.2.2 bouyer {
816 1.2.2.2 bouyer struct tswtcm *tsw = (struct tswtcm *)cb;
817 1.2.2.2 bouyer int len;
818 1.2.2.2 bouyer u_int32_t avg_rate;
819 1.2.2.2 bouyer u_int64_t interval, now, tmp;
820 1.2.2.2 bouyer
821 1.2.2.2 bouyer /*
822 1.2.2.2 bouyer * rate estimator
823 1.2.2.2 bouyer */
824 1.2.2.2 bouyer len = pktinfo->pkt_len;
825 1.2.2.2 bouyer now = read_machclk();
826 1.2.2.2 bouyer
827 1.2.2.2 bouyer interval = now - tsw->t_front;
828 1.2.2.2 bouyer /*
829 1.2.2.2 bouyer * calculate average rate:
830 1.2.2.2 bouyer * avg = (avg * timewin + pkt_len)/(timewin + interval)
831 1.2.2.2 bouyer * pkt_len needs to be multiplied by machclk_freq in order to
832 1.2.2.2 bouyer * get (bytes/sec).
833 1.2.2.2 bouyer * note: when avg_rate (bytes/sec) and timewin (machclk unit) are
834 1.2.2.2 bouyer * less than 32 bits, the following 64-bit operation has enough
835 1.2.2.2 bouyer * precision.
836 1.2.2.2 bouyer */
837 1.2.2.2 bouyer tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
838 1.2.2.2 bouyer + (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
839 1.2.2.2 bouyer tsw->avg_rate = avg_rate = (u_int32_t)tmp;
840 1.2.2.2 bouyer tsw->t_front = now;
841 1.2.2.2 bouyer
842 1.2.2.2 bouyer /*
843 1.2.2.2 bouyer * marker
844 1.2.2.2 bouyer */
845 1.2.2.2 bouyer if (avg_rate > tsw->cmtd_rate) {
846 1.2.2.2 bouyer u_int32_t randval = random() % avg_rate;
847 1.2.2.2 bouyer
848 1.2.2.2 bouyer if (avg_rate > tsw->peak_rate) {
849 1.2.2.2 bouyer if (randval < avg_rate - tsw->peak_rate) {
850 1.2.2.2 bouyer /* mark red */
851 1.2.2.2 bouyer pktinfo->pkt_dscp = tsw->red_dscp;
852 1.2.2.2 bouyer PKTCNTR_ADD(&tsw->red_cnt, len);
853 1.2.2.2 bouyer return (&tsw->red_action);
854 1.2.2.2 bouyer } else if (randval < avg_rate - tsw->cmtd_rate)
855 1.2.2.2 bouyer goto mark_yellow;
856 1.2.2.2 bouyer } else {
857 1.2.2.2 bouyer /* peak_rate >= avg_rate > cmtd_rate */
858 1.2.2.2 bouyer if (randval < avg_rate - tsw->cmtd_rate) {
859 1.2.2.2 bouyer mark_yellow:
860 1.2.2.2 bouyer pktinfo->pkt_dscp = tsw->yellow_dscp;
861 1.2.2.2 bouyer PKTCNTR_ADD(&tsw->yellow_cnt, len);
862 1.2.2.2 bouyer return (&tsw->yellow_action);
863 1.2.2.2 bouyer }
864 1.2.2.2 bouyer }
865 1.2.2.2 bouyer }
866 1.2.2.2 bouyer
867 1.2.2.2 bouyer /* mark green */
868 1.2.2.2 bouyer pktinfo->pkt_dscp = tsw->green_dscp;
869 1.2.2.2 bouyer PKTCNTR_ADD(&tsw->green_cnt, len);
870 1.2.2.2 bouyer return (&tsw->green_action);
871 1.2.2.2 bouyer }
872 1.2.2.2 bouyer
873 1.2.2.2 bouyer /*
874 1.2.2.2 bouyer * ioctl requests
875 1.2.2.2 bouyer */
876 1.2.2.2 bouyer static int
877 1.2.2.2 bouyer cdnrcmd_if_attach(ifname)
878 1.2.2.2 bouyer char *ifname;
879 1.2.2.2 bouyer {
880 1.2.2.2 bouyer struct ifnet *ifp;
881 1.2.2.2 bouyer struct top_cdnr *top;
882 1.2.2.2 bouyer
883 1.2.2.2 bouyer if ((ifp = ifunit(ifname)) == NULL)
884 1.2.2.2 bouyer return (EBADF);
885 1.2.2.2 bouyer
886 1.2.2.2 bouyer if (ifp->if_snd.altq_cdnr != NULL)
887 1.2.2.2 bouyer return (EBUSY);
888 1.2.2.2 bouyer
889 1.2.2.2 bouyer if ((top = top_create(&ifp->if_snd)) == NULL)
890 1.2.2.2 bouyer return (ENOMEM);
891 1.2.2.2 bouyer return (0);
892 1.2.2.2 bouyer }
893 1.2.2.2 bouyer
894 1.2.2.2 bouyer static int
895 1.2.2.2 bouyer cdnrcmd_if_detach(ifname)
896 1.2.2.2 bouyer char *ifname;
897 1.2.2.2 bouyer {
898 1.2.2.2 bouyer struct top_cdnr *top;
899 1.2.2.2 bouyer
900 1.2.2.2 bouyer if ((top = tcb_lookup(ifname)) == NULL)
901 1.2.2.2 bouyer return (EBADF);
902 1.2.2.2 bouyer
903 1.2.2.2 bouyer return top_destroy(top);
904 1.2.2.2 bouyer }
905 1.2.2.2 bouyer
906 1.2.2.2 bouyer static int
907 1.2.2.2 bouyer cdnrcmd_add_element(ap)
908 1.2.2.2 bouyer struct cdnr_add_element *ap;
909 1.2.2.2 bouyer {
910 1.2.2.2 bouyer struct top_cdnr *top;
911 1.2.2.2 bouyer struct cdnr_block *cb;
912 1.2.2.2 bouyer
913 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
914 1.2.2.2 bouyer return (EBADF);
915 1.2.2.2 bouyer
916 1.2.2.2 bouyer cb = element_create(top, &ap->action);
917 1.2.2.2 bouyer if (cb == NULL)
918 1.2.2.2 bouyer return (EINVAL);
919 1.2.2.2 bouyer /* return a class handle to the user */
920 1.2.2.2 bouyer ap->cdnr_handle = cdnr_cb2handle(cb);
921 1.2.2.2 bouyer return (0);
922 1.2.2.2 bouyer }
923 1.2.2.2 bouyer
924 1.2.2.2 bouyer static int
925 1.2.2.2 bouyer cdnrcmd_delete_element(ap)
926 1.2.2.2 bouyer struct cdnr_delete_element *ap;
927 1.2.2.2 bouyer {
928 1.2.2.2 bouyer struct top_cdnr *top;
929 1.2.2.2 bouyer struct cdnr_block *cb;
930 1.2.2.2 bouyer
931 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
932 1.2.2.2 bouyer return (EBADF);
933 1.2.2.2 bouyer
934 1.2.2.2 bouyer if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
935 1.2.2.2 bouyer return (EINVAL);
936 1.2.2.2 bouyer
937 1.2.2.2 bouyer if (cb->cb_type != TCETYPE_ELEMENT)
938 1.2.2.2 bouyer return generic_element_destroy(cb);
939 1.2.2.2 bouyer
940 1.2.2.2 bouyer return element_destroy(cb);
941 1.2.2.2 bouyer }
942 1.2.2.2 bouyer
943 1.2.2.2 bouyer static int
944 1.2.2.2 bouyer cdnrcmd_add_filter(ap)
945 1.2.2.2 bouyer struct cdnr_add_filter *ap;
946 1.2.2.2 bouyer {
947 1.2.2.2 bouyer struct top_cdnr *top;
948 1.2.2.2 bouyer struct cdnr_block *cb;
949 1.2.2.2 bouyer
950 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
951 1.2.2.2 bouyer return (EBADF);
952 1.2.2.2 bouyer
953 1.2.2.2 bouyer if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
954 1.2.2.2 bouyer return (EINVAL);
955 1.2.2.2 bouyer
956 1.2.2.2 bouyer return acc_add_filter(&top->tc_classifier, &ap->filter,
957 1.2.2.2 bouyer cb, &ap->filter_handle);
958 1.2.2.2 bouyer }
959 1.2.2.2 bouyer
960 1.2.2.2 bouyer static int
961 1.2.2.2 bouyer cdnrcmd_delete_filter(ap)
962 1.2.2.2 bouyer struct cdnr_delete_filter *ap;
963 1.2.2.2 bouyer {
964 1.2.2.2 bouyer struct top_cdnr *top;
965 1.2.2.2 bouyer
966 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
967 1.2.2.2 bouyer return (EBADF);
968 1.2.2.2 bouyer
969 1.2.2.2 bouyer return acc_delete_filter(&top->tc_classifier, ap->filter_handle);
970 1.2.2.2 bouyer }
971 1.2.2.2 bouyer
972 1.2.2.2 bouyer static int
973 1.2.2.2 bouyer cdnrcmd_add_tbm(ap)
974 1.2.2.2 bouyer struct cdnr_add_tbmeter *ap;
975 1.2.2.2 bouyer {
976 1.2.2.2 bouyer struct top_cdnr *top;
977 1.2.2.2 bouyer struct tbmeter *tbm;
978 1.2.2.2 bouyer
979 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
980 1.2.2.2 bouyer return (EBADF);
981 1.2.2.2 bouyer
982 1.2.2.2 bouyer tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
983 1.2.2.2 bouyer if (tbm == NULL)
984 1.2.2.2 bouyer return (EINVAL);
985 1.2.2.2 bouyer /* return a class handle to the user */
986 1.2.2.2 bouyer ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
987 1.2.2.2 bouyer return (0);
988 1.2.2.2 bouyer }
989 1.2.2.2 bouyer
990 1.2.2.2 bouyer static int
991 1.2.2.2 bouyer cdnrcmd_modify_tbm(ap)
992 1.2.2.2 bouyer struct cdnr_modify_tbmeter *ap;
993 1.2.2.2 bouyer {
994 1.2.2.2 bouyer struct tbmeter *tbm;
995 1.2.2.2 bouyer
996 1.2.2.2 bouyer if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
997 1.2.2.2 bouyer return (EINVAL);
998 1.2.2.2 bouyer
999 1.2.2.2 bouyer tb_import_profile(&tbm->tb, &ap->profile);
1000 1.2.2.2 bouyer
1001 1.2.2.2 bouyer return (0);
1002 1.2.2.2 bouyer }
1003 1.2.2.2 bouyer
1004 1.2.2.2 bouyer static int
1005 1.2.2.2 bouyer cdnrcmd_tbm_stats(ap)
1006 1.2.2.2 bouyer struct cdnr_tbmeter_stats *ap;
1007 1.2.2.2 bouyer {
1008 1.2.2.2 bouyer struct tbmeter *tbm;
1009 1.2.2.2 bouyer
1010 1.2.2.2 bouyer if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1011 1.2.2.2 bouyer return (EINVAL);
1012 1.2.2.2 bouyer
1013 1.2.2.2 bouyer ap->in_cnt = tbm->in_cnt;
1014 1.2.2.2 bouyer ap->out_cnt = tbm->out_cnt;
1015 1.2.2.2 bouyer
1016 1.2.2.2 bouyer return (0);
1017 1.2.2.2 bouyer }
1018 1.2.2.2 bouyer
1019 1.2.2.2 bouyer static int
1020 1.2.2.2 bouyer cdnrcmd_add_trtcm(ap)
1021 1.2.2.2 bouyer struct cdnr_add_trtcm *ap;
1022 1.2.2.2 bouyer {
1023 1.2.2.2 bouyer struct top_cdnr *top;
1024 1.2.2.2 bouyer struct trtcm *tcm;
1025 1.2.2.2 bouyer
1026 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1027 1.2.2.2 bouyer return (EBADF);
1028 1.2.2.2 bouyer
1029 1.2.2.2 bouyer tcm = trtcm_create(top, &ap->cmtd_profile, &ap->peak_profile,
1030 1.2.2.2 bouyer &ap->green_action, &ap->yellow_action,
1031 1.2.2.2 bouyer &ap->red_action, ap->coloraware);
1032 1.2.2.2 bouyer if (tcm == NULL)
1033 1.2.2.2 bouyer return (EINVAL);
1034 1.2.2.2 bouyer
1035 1.2.2.2 bouyer /* return a class handle to the user */
1036 1.2.2.2 bouyer ap->cdnr_handle = cdnr_cb2handle(&tcm->cdnrblk);
1037 1.2.2.2 bouyer return (0);
1038 1.2.2.2 bouyer }
1039 1.2.2.2 bouyer
1040 1.2.2.2 bouyer static int
1041 1.2.2.2 bouyer cdnrcmd_modify_trtcm(ap)
1042 1.2.2.2 bouyer struct cdnr_modify_trtcm *ap;
1043 1.2.2.2 bouyer {
1044 1.2.2.2 bouyer struct trtcm *tcm;
1045 1.2.2.2 bouyer
1046 1.2.2.2 bouyer if ((tcm = (struct trtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1047 1.2.2.2 bouyer return (EINVAL);
1048 1.2.2.2 bouyer
1049 1.2.2.2 bouyer tb_import_profile(&tcm->cmtd_tb, &ap->cmtd_profile);
1050 1.2.2.2 bouyer tb_import_profile(&tcm->peak_tb, &ap->peak_profile);
1051 1.2.2.2 bouyer
1052 1.2.2.2 bouyer return (0);
1053 1.2.2.2 bouyer }
1054 1.2.2.2 bouyer
1055 1.2.2.2 bouyer static int
1056 1.2.2.2 bouyer cdnrcmd_tcm_stats(ap)
1057 1.2.2.2 bouyer struct cdnr_tcm_stats *ap;
1058 1.2.2.2 bouyer {
1059 1.2.2.2 bouyer struct cdnr_block *cb;
1060 1.2.2.2 bouyer
1061 1.2.2.2 bouyer if ((cb = cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1062 1.2.2.2 bouyer return (EINVAL);
1063 1.2.2.2 bouyer
1064 1.2.2.2 bouyer if (cb->cb_type == TCETYPE_TRTCM) {
1065 1.2.2.2 bouyer struct trtcm *tcm = (struct trtcm *)cb;
1066 1.2.2.2 bouyer
1067 1.2.2.2 bouyer ap->green_cnt = tcm->green_cnt;
1068 1.2.2.2 bouyer ap->yellow_cnt = tcm->yellow_cnt;
1069 1.2.2.2 bouyer ap->red_cnt = tcm->red_cnt;
1070 1.2.2.2 bouyer } else if (cb->cb_type == TCETYPE_TSWTCM) {
1071 1.2.2.2 bouyer struct tswtcm *tsw = (struct tswtcm *)cb;
1072 1.2.2.2 bouyer
1073 1.2.2.2 bouyer ap->green_cnt = tsw->green_cnt;
1074 1.2.2.2 bouyer ap->yellow_cnt = tsw->yellow_cnt;
1075 1.2.2.2 bouyer ap->red_cnt = tsw->red_cnt;
1076 1.2.2.2 bouyer } else
1077 1.2.2.2 bouyer return (EINVAL);
1078 1.2.2.2 bouyer
1079 1.2.2.2 bouyer return (0);
1080 1.2.2.2 bouyer }
1081 1.2.2.2 bouyer
1082 1.2.2.2 bouyer static int
1083 1.2.2.2 bouyer cdnrcmd_add_tswtcm(ap)
1084 1.2.2.2 bouyer struct cdnr_add_tswtcm *ap;
1085 1.2.2.2 bouyer {
1086 1.2.2.2 bouyer struct top_cdnr *top;
1087 1.2.2.2 bouyer struct tswtcm *tsw;
1088 1.2.2.2 bouyer
1089 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1090 1.2.2.2 bouyer return (EBADF);
1091 1.2.2.2 bouyer
1092 1.2.2.2 bouyer if (ap->cmtd_rate > ap->peak_rate)
1093 1.2.2.2 bouyer return (EINVAL);
1094 1.2.2.2 bouyer
1095 1.2.2.2 bouyer tsw = tswtcm_create(top, ap->cmtd_rate, ap->peak_rate,
1096 1.2.2.2 bouyer ap->avg_interval, &ap->green_action,
1097 1.2.2.2 bouyer &ap->yellow_action, &ap->red_action);
1098 1.2.2.2 bouyer if (tsw == NULL)
1099 1.2.2.2 bouyer return (EINVAL);
1100 1.2.2.2 bouyer
1101 1.2.2.2 bouyer /* return a class handle to the user */
1102 1.2.2.2 bouyer ap->cdnr_handle = cdnr_cb2handle(&tsw->cdnrblk);
1103 1.2.2.2 bouyer return (0);
1104 1.2.2.2 bouyer }
1105 1.2.2.2 bouyer
1106 1.2.2.2 bouyer static int
1107 1.2.2.2 bouyer cdnrcmd_modify_tswtcm(ap)
1108 1.2.2.2 bouyer struct cdnr_modify_tswtcm *ap;
1109 1.2.2.2 bouyer {
1110 1.2.2.2 bouyer struct tswtcm *tsw;
1111 1.2.2.2 bouyer
1112 1.2.2.2 bouyer if ((tsw = (struct tswtcm *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
1113 1.2.2.2 bouyer return (EINVAL);
1114 1.2.2.2 bouyer
1115 1.2.2.2 bouyer if (ap->cmtd_rate > ap->peak_rate)
1116 1.2.2.2 bouyer return (EINVAL);
1117 1.2.2.2 bouyer
1118 1.2.2.2 bouyer /* convert rates from bits/sec to bytes/sec */
1119 1.2.2.2 bouyer tsw->cmtd_rate = ap->cmtd_rate / 8;
1120 1.2.2.2 bouyer tsw->peak_rate = ap->peak_rate / 8;
1121 1.2.2.2 bouyer tsw->avg_rate = 0;
1122 1.2.2.2 bouyer
1123 1.2.2.2 bouyer /* timewin is converted from msec to machine clock unit */
1124 1.2.2.2 bouyer tsw->timewin = (u_int64_t)machclk_freq * ap->avg_interval / 1000;
1125 1.2.2.2 bouyer
1126 1.2.2.2 bouyer return (0);
1127 1.2.2.2 bouyer }
1128 1.2.2.2 bouyer
1129 1.2.2.2 bouyer static int
1130 1.2.2.2 bouyer cdnrcmd_get_stats(ap)
1131 1.2.2.2 bouyer struct cdnr_get_stats *ap;
1132 1.2.2.2 bouyer {
1133 1.2.2.2 bouyer struct top_cdnr *top;
1134 1.2.2.2 bouyer struct cdnr_block *cb;
1135 1.2.2.2 bouyer struct tbmeter *tbm;
1136 1.2.2.2 bouyer struct trtcm *tcm;
1137 1.2.2.2 bouyer struct tswtcm *tsw;
1138 1.2.2.2 bouyer struct tce_stats tce, *usp;
1139 1.2.2.2 bouyer int error, n, nskip, nelements;
1140 1.2.2.2 bouyer
1141 1.2.2.2 bouyer if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
1142 1.2.2.2 bouyer return (EBADF);
1143 1.2.2.2 bouyer
1144 1.2.2.2 bouyer /* copy action stats */
1145 1.2.2.2 bouyer bcopy(top->tc_cnts, ap->cnts, sizeof(ap->cnts));
1146 1.2.2.2 bouyer
1147 1.2.2.2 bouyer /* stats for each element */
1148 1.2.2.2 bouyer nelements = ap->nelements;
1149 1.2.2.2 bouyer usp = ap->tce_stats;
1150 1.2.2.2 bouyer if (nelements <= 0 || usp == NULL)
1151 1.2.2.2 bouyer return (0);
1152 1.2.2.2 bouyer
1153 1.2.2.2 bouyer nskip = ap->nskip;
1154 1.2.2.2 bouyer n = 0;
1155 1.2.2.2 bouyer LIST_FOREACH(cb, &top->tc_elements, cb_next) {
1156 1.2.2.2 bouyer if (nskip > 0) {
1157 1.2.2.2 bouyer nskip--;
1158 1.2.2.2 bouyer continue;
1159 1.2.2.2 bouyer }
1160 1.2.2.2 bouyer
1161 1.2.2.2 bouyer bzero(&tce, sizeof(tce));
1162 1.2.2.2 bouyer tce.tce_handle = cb->cb_handle;
1163 1.2.2.2 bouyer tce.tce_type = cb->cb_type;
1164 1.2.2.2 bouyer switch (cb->cb_type) {
1165 1.2.2.2 bouyer case TCETYPE_TBMETER:
1166 1.2.2.2 bouyer tbm = (struct tbmeter *)cb;
1167 1.2.2.2 bouyer tce.tce_cnts[0] = tbm->in_cnt;
1168 1.2.2.2 bouyer tce.tce_cnts[1] = tbm->out_cnt;
1169 1.2.2.2 bouyer break;
1170 1.2.2.2 bouyer case TCETYPE_TRTCM:
1171 1.2.2.2 bouyer tcm = (struct trtcm *)cb;
1172 1.2.2.2 bouyer tce.tce_cnts[0] = tcm->green_cnt;
1173 1.2.2.2 bouyer tce.tce_cnts[1] = tcm->yellow_cnt;
1174 1.2.2.2 bouyer tce.tce_cnts[2] = tcm->red_cnt;
1175 1.2.2.2 bouyer break;
1176 1.2.2.2 bouyer case TCETYPE_TSWTCM:
1177 1.2.2.2 bouyer tsw = (struct tswtcm *)cb;
1178 1.2.2.2 bouyer tce.tce_cnts[0] = tsw->green_cnt;
1179 1.2.2.2 bouyer tce.tce_cnts[1] = tsw->yellow_cnt;
1180 1.2.2.2 bouyer tce.tce_cnts[2] = tsw->red_cnt;
1181 1.2.2.2 bouyer break;
1182 1.2.2.2 bouyer default:
1183 1.2.2.2 bouyer continue;
1184 1.2.2.2 bouyer }
1185 1.2.2.2 bouyer
1186 1.2.2.2 bouyer if ((error = copyout((caddr_t)&tce, (caddr_t)usp++,
1187 1.2.2.2 bouyer sizeof(tce))) != 0)
1188 1.2.2.2 bouyer return (error);
1189 1.2.2.2 bouyer
1190 1.2.2.2 bouyer if (++n == nelements)
1191 1.2.2.2 bouyer break;
1192 1.2.2.2 bouyer }
1193 1.2.2.2 bouyer ap->nelements = n;
1194 1.2.2.2 bouyer
1195 1.2.2.2 bouyer return (0);
1196 1.2.2.2 bouyer }
1197 1.2.2.2 bouyer
1198 1.2.2.2 bouyer /*
1199 1.2.2.2 bouyer * conditioner device interface
1200 1.2.2.2 bouyer */
1201 1.2.2.2 bouyer int
1202 1.2.2.2 bouyer cdnropen(dev, flag, fmt, p)
1203 1.2.2.2 bouyer dev_t dev;
1204 1.2.2.2 bouyer int flag, fmt;
1205 1.2.2.2 bouyer struct proc *p;
1206 1.2.2.2 bouyer {
1207 1.2.2.2 bouyer if (machclk_freq == 0)
1208 1.2.2.2 bouyer init_machclk();
1209 1.2.2.2 bouyer
1210 1.2.2.2 bouyer if (machclk_freq == 0) {
1211 1.2.2.2 bouyer printf("cdnr: no cpu clock available!\n");
1212 1.2.2.2 bouyer return (ENXIO);
1213 1.2.2.2 bouyer }
1214 1.2.2.2 bouyer
1215 1.2.2.2 bouyer /* everything will be done when the queueing scheme is attached. */
1216 1.2.2.2 bouyer return 0;
1217 1.2.2.2 bouyer }
1218 1.2.2.2 bouyer
1219 1.2.2.2 bouyer int
1220 1.2.2.2 bouyer cdnrclose(dev, flag, fmt, p)
1221 1.2.2.2 bouyer dev_t dev;
1222 1.2.2.2 bouyer int flag, fmt;
1223 1.2.2.2 bouyer struct proc *p;
1224 1.2.2.2 bouyer {
1225 1.2.2.2 bouyer struct top_cdnr *top;
1226 1.2.2.2 bouyer int err, error = 0;
1227 1.2.2.2 bouyer
1228 1.2.2.2 bouyer while ((top = LIST_FIRST(&tcb_list)) != NULL) {
1229 1.2.2.2 bouyer /* destroy all */
1230 1.2.2.2 bouyer err = top_destroy(top);
1231 1.2.2.2 bouyer if (err != 0 && error == 0)
1232 1.2.2.2 bouyer error = err;
1233 1.2.2.2 bouyer }
1234 1.2.2.2 bouyer altq_input = NULL;
1235 1.2.2.2 bouyer
1236 1.2.2.2 bouyer return (error);
1237 1.2.2.2 bouyer }
1238 1.2.2.2 bouyer
1239 1.2.2.2 bouyer int
1240 1.2.2.2 bouyer cdnrioctl(dev, cmd, addr, flag, p)
1241 1.2.2.2 bouyer dev_t dev;
1242 1.2.2.2 bouyer ioctlcmd_t cmd;
1243 1.2.2.2 bouyer caddr_t addr;
1244 1.2.2.2 bouyer int flag;
1245 1.2.2.2 bouyer struct proc *p;
1246 1.2.2.2 bouyer {
1247 1.2.2.2 bouyer struct top_cdnr *top;
1248 1.2.2.2 bouyer struct cdnr_interface *ifacep;
1249 1.2.2.2 bouyer int s, error = 0;
1250 1.2.2.2 bouyer
1251 1.2.2.2 bouyer /* check super-user privilege */
1252 1.2.2.2 bouyer switch (cmd) {
1253 1.2.2.2 bouyer case CDNR_GETSTATS:
1254 1.2.2.2 bouyer break;
1255 1.2.2.2 bouyer default:
1256 1.2.2.2 bouyer #if (__FreeBSD_version > 400000)
1257 1.2.2.2 bouyer if ((error = suser(p)) != 0)
1258 1.2.2.2 bouyer #else
1259 1.2.2.2 bouyer if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1260 1.2.2.2 bouyer #endif
1261 1.2.2.2 bouyer return (error);
1262 1.2.2.2 bouyer break;
1263 1.2.2.2 bouyer }
1264 1.2.2.2 bouyer
1265 1.2.2.2 bouyer s = splimp();
1266 1.2.2.2 bouyer switch (cmd) {
1267 1.2.2.2 bouyer
1268 1.2.2.2 bouyer case CDNR_IF_ATTACH:
1269 1.2.2.2 bouyer ifacep = (struct cdnr_interface *)addr;
1270 1.2.2.2 bouyer error = cdnrcmd_if_attach(ifacep->cdnr_ifname);
1271 1.2.2.2 bouyer break;
1272 1.2.2.2 bouyer
1273 1.2.2.2 bouyer case CDNR_IF_DETACH:
1274 1.2.2.2 bouyer ifacep = (struct cdnr_interface *)addr;
1275 1.2.2.2 bouyer error = cdnrcmd_if_detach(ifacep->cdnr_ifname);
1276 1.2.2.2 bouyer break;
1277 1.2.2.2 bouyer
1278 1.2.2.2 bouyer case CDNR_ENABLE:
1279 1.2.2.2 bouyer case CDNR_DISABLE:
1280 1.2.2.2 bouyer ifacep = (struct cdnr_interface *)addr;
1281 1.2.2.2 bouyer if ((top = tcb_lookup(ifacep->cdnr_ifname)) == NULL) {
1282 1.2.2.2 bouyer error = EBADF;
1283 1.2.2.2 bouyer break;
1284 1.2.2.2 bouyer }
1285 1.2.2.2 bouyer
1286 1.2.2.2 bouyer switch (cmd) {
1287 1.2.2.2 bouyer
1288 1.2.2.2 bouyer case CDNR_ENABLE:
1289 1.2.2.2 bouyer ALTQ_SET_CNDTNING(top->tc_ifq);
1290 1.2.2.2 bouyer if (altq_input == NULL)
1291 1.2.2.2 bouyer altq_input = altq_cdnr_input;
1292 1.2.2.2 bouyer break;
1293 1.2.2.2 bouyer
1294 1.2.2.2 bouyer case CDNR_DISABLE:
1295 1.2.2.2 bouyer ALTQ_CLEAR_CNDTNING(top->tc_ifq);
1296 1.2.2.2 bouyer LIST_FOREACH(top, &tcb_list, tc_next)
1297 1.2.2.2 bouyer if (ALTQ_IS_CNDTNING(top->tc_ifq))
1298 1.2.2.2 bouyer break;
1299 1.2.2.2 bouyer if (top == NULL)
1300 1.2.2.2 bouyer altq_input = NULL;
1301 1.2.2.2 bouyer break;
1302 1.2.2.2 bouyer }
1303 1.2.2.2 bouyer break;
1304 1.2.2.2 bouyer
1305 1.2.2.2 bouyer case CDNR_ADD_ELEM:
1306 1.2.2.2 bouyer error = cdnrcmd_add_element((struct cdnr_add_element *)addr);
1307 1.2.2.2 bouyer break;
1308 1.2.2.2 bouyer
1309 1.2.2.2 bouyer case CDNR_DEL_ELEM:
1310 1.2.2.2 bouyer error = cdnrcmd_delete_element((struct cdnr_delete_element *)addr);
1311 1.2.2.2 bouyer break;
1312 1.2.2.2 bouyer
1313 1.2.2.2 bouyer case CDNR_ADD_TBM:
1314 1.2.2.2 bouyer error = cdnrcmd_add_tbm((struct cdnr_add_tbmeter *)addr);
1315 1.2.2.2 bouyer break;
1316 1.2.2.2 bouyer
1317 1.2.2.2 bouyer case CDNR_MOD_TBM:
1318 1.2.2.2 bouyer error = cdnrcmd_modify_tbm((struct cdnr_modify_tbmeter *)addr);
1319 1.2.2.2 bouyer break;
1320 1.2.2.2 bouyer
1321 1.2.2.2 bouyer case CDNR_TBM_STATS:
1322 1.2.2.2 bouyer error = cdnrcmd_tbm_stats((struct cdnr_tbmeter_stats *)addr);
1323 1.2.2.2 bouyer break;
1324 1.2.2.2 bouyer
1325 1.2.2.2 bouyer case CDNR_ADD_TCM:
1326 1.2.2.2 bouyer error = cdnrcmd_add_trtcm((struct cdnr_add_trtcm *)addr);
1327 1.2.2.2 bouyer break;
1328 1.2.2.2 bouyer
1329 1.2.2.2 bouyer case CDNR_MOD_TCM:
1330 1.2.2.2 bouyer error = cdnrcmd_modify_trtcm((struct cdnr_modify_trtcm *)addr);
1331 1.2.2.2 bouyer break;
1332 1.2.2.2 bouyer
1333 1.2.2.2 bouyer case CDNR_TCM_STATS:
1334 1.2.2.2 bouyer error = cdnrcmd_tcm_stats((struct cdnr_tcm_stats *)addr);
1335 1.2.2.2 bouyer break;
1336 1.2.2.2 bouyer
1337 1.2.2.2 bouyer case CDNR_ADD_FILTER:
1338 1.2.2.2 bouyer error = cdnrcmd_add_filter((struct cdnr_add_filter *)addr);
1339 1.2.2.2 bouyer break;
1340 1.2.2.2 bouyer
1341 1.2.2.2 bouyer case CDNR_DEL_FILTER:
1342 1.2.2.2 bouyer error = cdnrcmd_delete_filter((struct cdnr_delete_filter *)addr);
1343 1.2.2.2 bouyer break;
1344 1.2.2.2 bouyer
1345 1.2.2.2 bouyer case CDNR_GETSTATS:
1346 1.2.2.2 bouyer error = cdnrcmd_get_stats((struct cdnr_get_stats *)addr);
1347 1.2.2.2 bouyer break;
1348 1.2.2.2 bouyer
1349 1.2.2.2 bouyer case CDNR_ADD_TSW:
1350 1.2.2.2 bouyer error = cdnrcmd_add_tswtcm((struct cdnr_add_tswtcm *)addr);
1351 1.2.2.2 bouyer break;
1352 1.2.2.2 bouyer
1353 1.2.2.2 bouyer case CDNR_MOD_TSW:
1354 1.2.2.2 bouyer error = cdnrcmd_modify_tswtcm((struct cdnr_modify_tswtcm *)addr);
1355 1.2.2.2 bouyer break;
1356 1.2.2.2 bouyer
1357 1.2.2.2 bouyer default:
1358 1.2.2.2 bouyer error = EINVAL;
1359 1.2.2.2 bouyer break;
1360 1.2.2.2 bouyer }
1361 1.2.2.2 bouyer splx(s);
1362 1.2.2.2 bouyer
1363 1.2.2.2 bouyer return error;
1364 1.2.2.2 bouyer }
1365 1.2.2.2 bouyer
1366 1.2.2.2 bouyer #ifdef KLD_MODULE
1367 1.2.2.2 bouyer
1368 1.2.2.2 bouyer static struct altqsw cdnr_sw =
1369 1.2.2.2 bouyer {"cdnr", cdnropen, cdnrclose, cdnrioctl};
1370 1.2.2.2 bouyer
1371 1.2.2.2 bouyer ALTQ_MODULE(altq_cdnr, ALTQT_CDNR, &cdnr_sw);
1372 1.2.2.2 bouyer
1373 1.2.2.2 bouyer #endif /* KLD_MODULE */
1374 1.2.2.2 bouyer
1375 1.2.2.2 bouyer #endif /* ALTQ_CDNR */
1376