altq_fifoq.c revision 1.13 1 /* $NetBSD: altq_fifoq.c,v 1.13 2006/10/12 19:59:08 peter Exp $ */
2 /* $KAME: altq_fifoq.c,v 1.12 2003/07/10 12:07:48 kjc Exp $ */
3
4 /*
5 * Copyright (C) 1997-2002
6 * Sony Computer Science Laboratories Inc. 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 *
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: altq_fifoq.c,v 1.13 2006/10/12 19:59:08 peter Exp $");
32
33 #ifdef _KERNEL_OPT
34 #include "opt_altq.h"
35 #endif
36
37 #ifdef ALTQ_FIFOQ /* fifoq is enabled by ALTQ_FIFOQ option in opt_altq.h */
38
39 /*
40 * FIFOQ is an altq sample implementation. There will be little
41 * need to use FIFOQ as an alternative queueing scheme.
42 * But this code is provided as a template for those who want to
43 * write their own queueing schemes.
44 */
45
46 #include <sys/param.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/errno.h>
54 #include <sys/kernel.h>
55 #include <sys/kauth.h>
56
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <netinet/in.h>
60
61 #include <altq/altq.h>
62 #include <altq/altq_conf.h>
63 #include <altq/altq_fifoq.h>
64
65 #ifdef ALTQ3_COMPAT
66
67 #define FIFOQ_STATS /* collect statistics */
68
69 /* fifoq_list keeps all fifoq_state_t's allocated. */
70 static fifoq_state_t *fifoq_list = NULL;
71
72 /* internal function prototypes */
73 static int fifoq_enqueue(struct ifaltq *, struct mbuf *,
74 struct altq_pktattr *);
75 static struct mbuf *fifoq_dequeue(struct ifaltq *, int);
76 static int fifoq_detach(fifoq_state_t *);
77 static int fifoq_request(struct ifaltq *, int, void *);
78 static void fifoq_purge(fifoq_state_t *);
79
80 /*
81 * fifoq device interface
82 */
83 altqdev_decl(fifoq);
84
85 int
86 fifoqopen(dev_t dev __unused, int flag __unused, int fmt __unused,
87 struct lwp *l __unused)
88 {
89 /* everything will be done when the queueing scheme is attached. */
90 return 0;
91 }
92
93 /*
94 * there are 2 ways to act on close.
95 * detach-all-on-close:
96 * use for the daemon style approach. if the daemon dies, all the
97 * resource will be released.
98 * no-action-on-close:
99 * use for the command style approach. (e.g. fifoq on/off)
100 *
101 * note: close is called not on every close but when the last reference
102 * is removed (only once with multiple simultaneous references.)
103 */
104 int
105 fifoqclose(dev_t dev __unused, int flag __unused, int fmt __unused,
106 struct lwp *l __unused)
107 {
108 fifoq_state_t *q;
109 int err, error = 0;
110
111 while ((q = fifoq_list) != NULL) {
112 /* destroy all */
113 err = fifoq_detach(q);
114 if (err != 0 && error == 0)
115 error = err;
116 }
117
118 return error;
119 }
120
121 int
122 fifoqioctl(dev_t dev __unused, ioctlcmd_t cmd, caddr_t addr, int flag __unused,
123 struct lwp *l)
124 {
125 fifoq_state_t *q;
126 struct fifoq_interface *ifacep;
127 struct ifnet *ifp;
128 int error = 0;
129
130 /* check super-user privilege */
131 switch (cmd) {
132 case FIFOQ_GETSTATS:
133 break;
134 default:
135 #if (__FreeBSD_version > 400000)
136 if ((error = suser(p)) != 0)
137 return (error);
138 #else
139 if ((error = kauth_authorize_generic(l->l_cred,
140 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
141 return (error);
142 #endif
143 break;
144 }
145
146 switch (cmd) {
147 case FIFOQ_ENABLE:
148 ifacep = (struct fifoq_interface *)addr;
149 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
150 == NULL) {
151 error = EBADF;
152 break;
153 }
154 error = altq_enable(q->q_ifq);
155 break;
156
157 case FIFOQ_DISABLE:
158 ifacep = (struct fifoq_interface *)addr;
159 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
160 == NULL) {
161 error = EBADF;
162 break;
163 }
164 error = altq_disable(q->q_ifq);
165 break;
166
167 case FIFOQ_IF_ATTACH:
168 ifp = ifunit(((struct fifoq_interface *)addr)->fifoq_ifname);
169 if (ifp == NULL) {
170 error = ENXIO;
171 break;
172 }
173
174 /* allocate and initialize fifoq_state_t */
175 q = malloc(sizeof(fifoq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
176 if (q == NULL) {
177 error = ENOMEM;
178 break;
179 }
180
181 q->q_ifq = &ifp->if_snd;
182 q->q_head = q->q_tail = NULL;
183 q->q_len = 0;
184 q->q_limit = FIFOQ_LIMIT;
185
186 /*
187 * set FIFOQ to this ifnet structure.
188 */
189 error = altq_attach(q->q_ifq, ALTQT_FIFOQ, q,
190 fifoq_enqueue, fifoq_dequeue, fifoq_request,
191 NULL, NULL);
192 if (error) {
193 free(q, M_DEVBUF);
194 break;
195 }
196
197 /* add this state to the fifoq list */
198 q->q_next = fifoq_list;
199 fifoq_list = q;
200 break;
201
202 case FIFOQ_IF_DETACH:
203 ifacep = (struct fifoq_interface *)addr;
204 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
205 == NULL) {
206 error = EBADF;
207 break;
208 }
209 error = fifoq_detach(q);
210 break;
211
212 case FIFOQ_GETSTATS:
213 do {
214 struct fifoq_getstats *q_stats;
215
216 q_stats = (struct fifoq_getstats *)addr;
217 if ((q = altq_lookup(q_stats->iface.fifoq_ifname,
218 ALTQT_FIFOQ)) == NULL) {
219 error = EBADF;
220 break;
221 }
222
223 q_stats->q_len = q->q_len;
224 q_stats->q_limit = q->q_limit;
225 q_stats->xmit_cnt = q->q_stats.xmit_cnt;
226 q_stats->drop_cnt = q->q_stats.drop_cnt;
227 q_stats->period = q->q_stats.period;
228 } while (/*CONSTCOND*/ 0);
229 break;
230
231 case FIFOQ_CONFIG:
232 do {
233 struct fifoq_conf *fc;
234 int limit;
235
236 fc = (struct fifoq_conf *)addr;
237 if ((q = altq_lookup(fc->iface.fifoq_ifname,
238 ALTQT_FIFOQ)) == NULL) {
239 error = EBADF;
240 break;
241 }
242 limit = fc->fifoq_limit;
243 if (limit < 0)
244 limit = 0;
245 q->q_limit = limit;
246 fc->fifoq_limit = limit;
247 } while (/*CONSTCOND*/ 0);
248 break;
249
250 default:
251 error = EINVAL;
252 break;
253 }
254 return error;
255 }
256
257 /*
258 * fifoq support routines
259 */
260
261 /*
262 * enqueue routine:
263 *
264 * returns: 0 when successfully queued.
265 * ENOBUFS when drop occurs.
266 */
267 static int
268 fifoq_enqueue(struct ifaltq *ifq, struct mbuf *m,
269 struct altq_pktattr *pktattr __unused)
270 {
271 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
272
273 /* if the queue is full, drop the incoming packet(drop-tail) */
274 if (q->q_len >= q->q_limit) {
275 #ifdef FIFOQ_STATS
276 PKTCNTR_ADD(&q->q_stats.drop_cnt, m_pktlen(m));
277 #endif
278 m_freem(m);
279 return (ENOBUFS);
280 }
281
282 /* enqueue the packet at the taile of the queue */
283 m->m_nextpkt = NULL;
284 if (q->q_tail == NULL)
285 q->q_head = m;
286 else
287 q->q_tail->m_nextpkt = m;
288 q->q_tail = m;
289 q->q_len++;
290 ifq->ifq_len++;
291 return 0;
292 }
293
294 /*
295 * dequeue routine:
296 * must be called in splnet.
297 *
298 * returns: mbuf dequeued.
299 * NULL when no packet is available in the queue.
300 */
301 /*
302 * ALTDQ_PEEK is provided for drivers which need to know the next packet
303 * to send in advance.
304 * when ALTDQ_PEEK is specified, the next packet to be dequeued is
305 * returned without dequeueing the packet.
306 * when ALTDQ_DEQUEUE is called *immediately after* an ALTDQ_PEEK
307 * operation, the same packet should be returned.
308 */
309 static struct mbuf *
310 fifoq_dequeue(struct ifaltq *ifq, int op)
311 {
312 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
313 struct mbuf *m = NULL;
314
315 if (op == ALTDQ_POLL)
316 return (q->q_head);
317
318 if ((m = q->q_head) == NULL)
319 return (NULL);
320
321 if ((q->q_head = m->m_nextpkt) == NULL)
322 q->q_tail = NULL;
323 m->m_nextpkt = NULL;
324 q->q_len--;
325 ifq->ifq_len--;
326 #ifdef FIFOQ_STATS
327 PKTCNTR_ADD(&q->q_stats.xmit_cnt, m_pktlen(m));
328 if (q->q_len == 0)
329 q->q_stats.period++;
330 #endif
331 return (m);
332 }
333
334 static int
335 fifoq_request(struct ifaltq *ifq, int req, void *arg __unused)
336 {
337 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
338
339 switch (req) {
340 case ALTRQ_PURGE:
341 fifoq_purge(q);
342 break;
343 }
344 return (0);
345 }
346
347
348 static int
349 fifoq_detach(fifoq_state_t *q)
350 {
351 fifoq_state_t *tmp;
352 int error = 0;
353
354 if (ALTQ_IS_ENABLED(q->q_ifq))
355 altq_disable(q->q_ifq);
356
357 fifoq_purge(q);
358
359 if ((error = altq_detach(q->q_ifq)))
360 return (error);
361
362 if (fifoq_list == q)
363 fifoq_list = q->q_next;
364 else {
365 for (tmp = fifoq_list; tmp != NULL; tmp = tmp->q_next)
366 if (tmp->q_next == q) {
367 tmp->q_next = q->q_next;
368 break;
369 }
370 if (tmp == NULL)
371 printf("fifoq_detach: no state in fifoq_list!\n");
372 }
373
374 free(q, M_DEVBUF);
375 return (error);
376 }
377
378 /*
379 * fifoq_purge
380 * should be called in splnet or after disabling the fifoq.
381 */
382 static void
383 fifoq_purge(fifoq_state_t *q)
384 {
385 struct mbuf *m;
386
387 while ((m = q->q_head) != NULL) {
388 q->q_head = m->m_nextpkt;
389 m_freem(m);
390 }
391 q->q_tail = NULL;
392 q->q_len = 0;
393 if (ALTQ_IS_ENABLED(q->q_ifq))
394 q->q_ifq->ifq_len = 0;
395 }
396
397 #ifdef KLD_MODULE
398
399 static struct altqsw fifoq_sw =
400 {"fifoq", fifoqopen, fifoqclose, fifoqioctl};
401
402 ALTQ_MODULE(altq_fifoq, ALTQT_FIFOQ, &fifoq_sw);
403
404 #endif /* KLD_MODULE */
405
406 #endif /* ALTQ3_COMPAT */
407 #endif /* ALTQ_FIFOQ */
408