altq_fifoq.c revision 1.7.12.1 1 /* $NetBSD: altq_fifoq.c,v 1.7.12.1 2006/03/18 12:08:18 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.7.12.1 2006/03/18 12:08:18 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
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <netinet/in.h>
59
60 #include <altq/altq.h>
61 #include <altq/altq_conf.h>
62 #include <altq/altq_fifoq.h>
63
64 #ifdef ALTQ3_COMPAT
65
66 #define FIFOQ_STATS /* collect statistics */
67
68 /* fifoq_list keeps all fifoq_state_t's allocated. */
69 static fifoq_state_t *fifoq_list = NULL;
70
71 /* internal function prototypes */
72 static int fifoq_enqueue(struct ifaltq *, struct mbuf *,
73 struct altq_pktattr *);
74 static struct mbuf *fifoq_dequeue(struct ifaltq *, int);
75 static int fifoq_detach(fifoq_state_t *);
76 static int fifoq_request(struct ifaltq *, int, void *);
77 static void fifoq_purge(fifoq_state_t *);
78
79 /*
80 * fifoq device interface
81 */
82 altqdev_decl(fifoq);
83
84 int
85 fifoqopen(dev, flag, fmt, l)
86 dev_t dev;
87 int flag, fmt;
88 struct lwp *l;
89 {
90 /* everything will be done when the queueing scheme is attached. */
91 return 0;
92 }
93
94 /*
95 * there are 2 ways to act on close.
96 * detach-all-on-close:
97 * use for the daemon style approach. if the daemon dies, all the
98 * resource will be released.
99 * no-action-on-close:
100 * use for the command style approach. (e.g. fifoq on/off)
101 *
102 * note: close is called not on every close but when the last reference
103 * is removed (only once with multiple simultaneous references.)
104 */
105 int
106 fifoqclose(dev, flag, fmt, l)
107 dev_t dev;
108 int flag, fmt;
109 struct lwp *l;
110 {
111 fifoq_state_t *q;
112 int err, error = 0;
113
114 while ((q = fifoq_list) != NULL) {
115 /* destroy all */
116 err = fifoq_detach(q);
117 if (err != 0 && error == 0)
118 error = err;
119 }
120
121 return error;
122 }
123
124 int
125 fifoqioctl(dev, cmd, addr, flag, l)
126 dev_t dev;
127 ioctlcmd_t cmd;
128 caddr_t addr;
129 int flag;
130 struct lwp *l;
131 {
132 fifoq_state_t *q;
133 struct fifoq_interface *ifacep;
134 struct ifnet *ifp;
135 struct proc *p = l->l_proc;
136 int error = 0;
137
138 /* check super-user privilege */
139 switch (cmd) {
140 case FIFOQ_GETSTATS:
141 break;
142 default:
143 #if (__FreeBSD_version > 400000)
144 if ((error = suser(p)) != 0)
145 return (error);
146 #else
147 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
148 return (error);
149 #endif
150 break;
151 }
152
153 switch (cmd) {
154 case FIFOQ_ENABLE:
155 ifacep = (struct fifoq_interface *)addr;
156 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
157 == NULL) {
158 error = EBADF;
159 break;
160 }
161 error = altq_enable(q->q_ifq);
162 break;
163
164 case FIFOQ_DISABLE:
165 ifacep = (struct fifoq_interface *)addr;
166 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
167 == NULL) {
168 error = EBADF;
169 break;
170 }
171 error = altq_disable(q->q_ifq);
172 break;
173
174 case FIFOQ_IF_ATTACH:
175 ifp = ifunit(((struct fifoq_interface *)addr)->fifoq_ifname);
176 if (ifp == NULL) {
177 error = ENXIO;
178 break;
179 }
180
181 /* allocate and initialize fifoq_state_t */
182 MALLOC(q, fifoq_state_t *, sizeof(fifoq_state_t),
183 M_DEVBUF, M_WAITOK);
184 if (q == NULL) {
185 error = ENOMEM;
186 break;
187 }
188 (void)memset(q, 0, sizeof(fifoq_state_t));
189
190 q->q_ifq = &ifp->if_snd;
191 q->q_head = q->q_tail = NULL;
192 q->q_len = 0;
193 q->q_limit = FIFOQ_LIMIT;
194
195 /*
196 * set FIFOQ to this ifnet structure.
197 */
198 error = altq_attach(q->q_ifq, ALTQT_FIFOQ, q,
199 fifoq_enqueue, fifoq_dequeue, fifoq_request,
200 NULL, NULL);
201 if (error) {
202 FREE(q, M_DEVBUF);
203 break;
204 }
205
206 /* add this state to the fifoq list */
207 q->q_next = fifoq_list;
208 fifoq_list = q;
209 break;
210
211 case FIFOQ_IF_DETACH:
212 ifacep = (struct fifoq_interface *)addr;
213 if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
214 == NULL) {
215 error = EBADF;
216 break;
217 }
218 error = fifoq_detach(q);
219 break;
220
221 case FIFOQ_GETSTATS:
222 do {
223 struct fifoq_getstats *q_stats;
224
225 q_stats = (struct fifoq_getstats *)addr;
226 if ((q = altq_lookup(q_stats->iface.fifoq_ifname,
227 ALTQT_FIFOQ)) == NULL) {
228 error = EBADF;
229 break;
230 }
231
232 q_stats->q_len = q->q_len;
233 q_stats->q_limit = q->q_limit;
234 q_stats->xmit_cnt = q->q_stats.xmit_cnt;
235 q_stats->drop_cnt = q->q_stats.drop_cnt;
236 q_stats->period = q->q_stats.period;
237 } while (/*CONSTCOND*/ 0);
238 break;
239
240 case FIFOQ_CONFIG:
241 do {
242 struct fifoq_conf *fc;
243 int limit;
244
245 fc = (struct fifoq_conf *)addr;
246 if ((q = altq_lookup(fc->iface.fifoq_ifname,
247 ALTQT_FIFOQ)) == NULL) {
248 error = EBADF;
249 break;
250 }
251 limit = fc->fifoq_limit;
252 if (limit < 0)
253 limit = 0;
254 q->q_limit = limit;
255 fc->fifoq_limit = limit;
256 } while (/*CONSTCOND*/ 0);
257 break;
258
259 default:
260 error = EINVAL;
261 break;
262 }
263 return error;
264 }
265
266 /*
267 * fifoq support routines
268 */
269
270 /*
271 * enqueue routine:
272 *
273 * returns: 0 when successfully queued.
274 * ENOBUFS when drop occurs.
275 */
276 static int
277 fifoq_enqueue(ifq, m, pktattr)
278 struct ifaltq *ifq;
279 struct mbuf *m;
280 struct altq_pktattr *pktattr;
281 {
282 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
283
284 /* if the queue is full, drop the incoming packet(drop-tail) */
285 if (q->q_len >= q->q_limit) {
286 #ifdef FIFOQ_STATS
287 PKTCNTR_ADD(&q->q_stats.drop_cnt, m_pktlen(m));
288 #endif
289 m_freem(m);
290 return (ENOBUFS);
291 }
292
293 /* enqueue the packet at the taile of the queue */
294 m->m_nextpkt = NULL;
295 if (q->q_tail == NULL)
296 q->q_head = m;
297 else
298 q->q_tail->m_nextpkt = m;
299 q->q_tail = m;
300 q->q_len++;
301 ifq->ifq_len++;
302 return 0;
303 }
304
305 /*
306 * dequeue routine:
307 * must be called in splnet.
308 *
309 * returns: mbuf dequeued.
310 * NULL when no packet is available in the queue.
311 */
312 /*
313 * ALTDQ_PEEK is provided for drivers which need to know the next packet
314 * to send in advance.
315 * when ALTDQ_PEEK is specified, the next packet to be dequeued is
316 * returned without dequeueing the packet.
317 * when ALTDQ_DEQUEUE is called *immediately after* an ALTDQ_PEEK
318 * operation, the same packet should be returned.
319 */
320 static struct mbuf *
321 fifoq_dequeue(ifq, op)
322 struct ifaltq *ifq;
323 int op;
324 {
325 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
326 struct mbuf *m = NULL;
327
328 if (op == ALTDQ_POLL)
329 return (q->q_head);
330
331 if ((m = q->q_head) == NULL)
332 return (NULL);
333
334 if ((q->q_head = m->m_nextpkt) == NULL)
335 q->q_tail = NULL;
336 m->m_nextpkt = NULL;
337 q->q_len--;
338 ifq->ifq_len--;
339 #ifdef FIFOQ_STATS
340 PKTCNTR_ADD(&q->q_stats.xmit_cnt, m_pktlen(m));
341 if (q->q_len == 0)
342 q->q_stats.period++;
343 #endif
344 return (m);
345 }
346
347 static int
348 fifoq_request(ifq, req, arg)
349 struct ifaltq *ifq;
350 int req;
351 void *arg;
352 {
353 fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
354
355 switch (req) {
356 case ALTRQ_PURGE:
357 fifoq_purge(q);
358 break;
359 }
360 return (0);
361 }
362
363
364 static int fifoq_detach(q)
365 fifoq_state_t *q;
366 {
367 fifoq_state_t *tmp;
368 int error = 0;
369
370 if (ALTQ_IS_ENABLED(q->q_ifq))
371 altq_disable(q->q_ifq);
372
373 fifoq_purge(q);
374
375 if ((error = altq_detach(q->q_ifq)))
376 return (error);
377
378 if (fifoq_list == q)
379 fifoq_list = q->q_next;
380 else {
381 for (tmp = fifoq_list; tmp != NULL; tmp = tmp->q_next)
382 if (tmp->q_next == q) {
383 tmp->q_next = q->q_next;
384 break;
385 }
386 if (tmp == NULL)
387 printf("fifoq_detach: no state in fifoq_list!\n");
388 }
389
390 FREE(q, M_DEVBUF);
391 return (error);
392 }
393
394 /*
395 * fifoq_purge
396 * should be called in splnet or after disabling the fifoq.
397 */
398 static void fifoq_purge(q)
399 fifoq_state_t *q;
400 {
401 struct mbuf *m;
402
403 while ((m = q->q_head) != NULL) {
404 q->q_head = m->m_nextpkt;
405 m_freem(m);
406 }
407 q->q_tail = NULL;
408 q->q_len = 0;
409 if (ALTQ_IS_ENABLED(q->q_ifq))
410 q->q_ifq->ifq_len = 0;
411 }
412
413 #ifdef KLD_MODULE
414
415 static struct altqsw fifoq_sw =
416 {"fifoq", fifoqopen, fifoqclose, fifoqioctl};
417
418 ALTQ_MODULE(altq_fifoq, ALTQT_FIFOQ, &fifoq_sw);
419
420 #endif /* KLD_MODULE */
421
422 #endif /* ALTQ3_COMPAT */
423 #endif /* ALTQ_FIFOQ */
424