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