1 /*-
2 * Copyright (C) 2000-2003
3 * Sony Computer Science Laboratories Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $KAME: altq_priq.c,v 1.11 2003/09/17 14:23:25 kjc Exp $
27 * $FreeBSD$
28 */
29 /*
30 * priority queue
31 */
32
33 #include "opt_altq.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36
37 #ifdef ALTQ_PRIQ /* priq is enabled by ALTQ_PRIQ option in opt_altq.h */
38
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
53
54 #include <netpfil/pf/pf.h>
55 #include <netpfil/pf/pf_altq.h>
56 #include <netpfil/pf/pf_mtag.h>
57 #include <net/altq/altq.h>
58 #ifdef ALTQ3_COMPAT
59 #include <net/altq/altq_conf.h>
60 #endif
61 #include <net/altq/altq_priq.h>
62
63 /*
64 * function prototypes
65 */
66 #ifdef ALTQ3_COMPAT
67 static struct priq_if *priq_attach(struct ifaltq *, u_int);
68 static int priq_detach(struct priq_if *);
69 #endif
70 static int priq_clear_interface(struct priq_if *);
71 static int priq_request(struct ifaltq *, int, void *);
72 static void priq_purge(struct priq_if *);
73 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
74 int);
75 static int priq_class_destroy(struct priq_class *);
76 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
77 static struct mbuf *priq_dequeue(struct ifaltq *, int);
78
79 static int priq_addq(struct priq_class *, struct mbuf *);
80 static struct mbuf *priq_getq(struct priq_class *);
81 static struct mbuf *priq_pollq(struct priq_class *);
82 static void priq_purgeq(struct priq_class *);
83
84 #ifdef ALTQ3_COMPAT
85 static int priqcmd_if_attach(struct priq_interface *);
86 static int priqcmd_if_detach(struct priq_interface *);
87 static int priqcmd_add_class(struct priq_add_class *);
88 static int priqcmd_delete_class(struct priq_delete_class *);
89 static int priqcmd_modify_class(struct priq_modify_class *);
90 static int priqcmd_add_filter(struct priq_add_filter *);
91 static int priqcmd_delete_filter(struct priq_delete_filter *);
92 static int priqcmd_class_stats(struct priq_class_stats *);
93 #endif /* ALTQ3_COMPAT */
94
95 static void get_class_stats(struct priq_classstats *, struct priq_class *);
96 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
97
98 #ifdef ALTQ3_COMPAT
99 altqdev_decl(priq);
100
101 /* pif_list keeps all priq_if's allocated. */
102 static struct priq_if *pif_list = NULL;
103 #endif /* ALTQ3_COMPAT */
104
105 int
priq_pfattach(struct pf_altq * a)106 priq_pfattach(struct pf_altq *a)
107 {
108 struct ifnet *ifp;
109 int s, error;
110
111 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
112 return (EINVAL);
113 s = splnet();
114 error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
115 priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
116 splx(s);
117 return (error);
118 }
119
120 int
priq_add_altq(struct ifnet * ifp,struct pf_altq * a)121 priq_add_altq(struct ifnet * ifp, struct pf_altq *a)
122 {
123 struct priq_if *pif;
124
125 if (ifp == NULL)
126 return (EINVAL);
127 if (!ALTQ_IS_READY(&ifp->if_snd))
128 return (ENODEV);
129
130 pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
131 if (pif == NULL)
132 return (ENOMEM);
133 pif->pif_bandwidth = a->ifbandwidth;
134 pif->pif_maxpri = -1;
135 pif->pif_ifq = &ifp->if_snd;
136
137 /* keep the state in pf_altq */
138 a->altq_disc = pif;
139
140 return (0);
141 }
142
143 int
priq_remove_altq(struct pf_altq * a)144 priq_remove_altq(struct pf_altq *a)
145 {
146 struct priq_if *pif;
147
148 if ((pif = a->altq_disc) == NULL)
149 return (EINVAL);
150 a->altq_disc = NULL;
151
152 (void)priq_clear_interface(pif);
153
154 free(pif, M_DEVBUF);
155 return (0);
156 }
157
158 int
priq_add_queue(struct pf_altq * a)159 priq_add_queue(struct pf_altq *a)
160 {
161 struct priq_if *pif;
162 struct priq_class *cl;
163
164 if ((pif = a->altq_disc) == NULL)
165 return (EINVAL);
166
167 /* check parameters */
168 if (a->priority >= PRIQ_MAXPRI)
169 return (EINVAL);
170 if (a->qid == 0)
171 return (EINVAL);
172 if (pif->pif_classes[a->priority] != NULL)
173 return (EBUSY);
174 if (clh_to_clp(pif, a->qid) != NULL)
175 return (EBUSY);
176
177 cl = priq_class_create(pif, a->priority, a->qlimit,
178 a->pq_u.priq_opts.flags, a->qid);
179 if (cl == NULL)
180 return (ENOMEM);
181
182 return (0);
183 }
184
185 int
priq_remove_queue(struct pf_altq * a)186 priq_remove_queue(struct pf_altq *a)
187 {
188 struct priq_if *pif;
189 struct priq_class *cl;
190
191 if ((pif = a->altq_disc) == NULL)
192 return (EINVAL);
193
194 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
195 return (EINVAL);
196
197 return (priq_class_destroy(cl));
198 }
199
200 int
priq_getqstats(struct pf_altq * a,void * ubuf,int * nbytes,int version)201 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
202 {
203 struct priq_if *pif;
204 struct priq_class *cl;
205 struct priq_classstats stats;
206 int error = 0;
207
208 if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
209 return (EBADF);
210
211 if ((cl = clh_to_clp(pif, a->qid)) == NULL)
212 return (EINVAL);
213
214 if (*nbytes < sizeof(stats))
215 return (EINVAL);
216
217 get_class_stats(&stats, cl);
218
219 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
220 return (error);
221 *nbytes = sizeof(stats);
222 return (0);
223 }
224
225 /*
226 * bring the interface back to the initial state by discarding
227 * all the filters and classes.
228 */
229 static int
priq_clear_interface(struct priq_if * pif)230 priq_clear_interface(struct priq_if *pif)
231 {
232 struct priq_class *cl;
233 int pri;
234
235 #ifdef ALTQ3_CLFIER_COMPAT
236 /* free the filters for this interface */
237 acc_discard_filters(&pif->pif_classifier, NULL, 1);
238 #endif
239
240 /* clear out the classes */
241 for (pri = 0; pri <= pif->pif_maxpri; pri++)
242 if ((cl = pif->pif_classes[pri]) != NULL)
243 priq_class_destroy(cl);
244
245 return (0);
246 }
247
248 static int
priq_request(struct ifaltq * ifq,int req,void * arg)249 priq_request(struct ifaltq *ifq, int req, void *arg)
250 {
251 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
252
253 IFQ_LOCK_ASSERT(ifq);
254
255 switch (req) {
256 case ALTRQ_PURGE:
257 priq_purge(pif);
258 break;
259 }
260 return (0);
261 }
262
263 /* discard all the queued packets on the interface */
264 static void
priq_purge(struct priq_if * pif)265 priq_purge(struct priq_if *pif)
266 {
267 struct priq_class *cl;
268 int pri;
269
270 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
271 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
272 priq_purgeq(cl);
273 }
274 if (ALTQ_IS_ENABLED(pif->pif_ifq))
275 pif->pif_ifq->ifq_len = 0;
276 }
277
278 static struct priq_class *
priq_class_create(struct priq_if * pif,int pri,int qlimit,int flags,int qid)279 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
280 {
281 struct priq_class *cl;
282 int s;
283
284 #ifndef ALTQ_RED
285 if (flags & PRCF_RED) {
286 #ifdef ALTQ_DEBUG
287 printf("priq_class_create: RED not configured for PRIQ!\n");
288 #endif
289 return (NULL);
290 }
291 #endif
292 #ifndef ALTQ_CODEL
293 if (flags & PRCF_CODEL) {
294 #ifdef ALTQ_DEBUG
295 printf("priq_class_create: CODEL not configured for PRIQ!\n");
296 #endif
297 return (NULL);
298 }
299 #endif
300
301 if ((cl = pif->pif_classes[pri]) != NULL) {
302 /* modify the class instead of creating a new one */
303 s = splnet();
304 IFQ_LOCK(cl->cl_pif->pif_ifq);
305 if (!qempty(cl->cl_q))
306 priq_purgeq(cl);
307 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
308 splx(s);
309 #ifdef ALTQ_RIO
310 if (q_is_rio(cl->cl_q))
311 rio_destroy((rio_t *)cl->cl_red);
312 #endif
313 #ifdef ALTQ_RED
314 if (q_is_red(cl->cl_q))
315 red_destroy(cl->cl_red);
316 #endif
317 #ifdef ALTQ_CODEL
318 if (q_is_codel(cl->cl_q))
319 codel_destroy(cl->cl_codel);
320 #endif
321 } else {
322 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
323 M_NOWAIT | M_ZERO);
324 if (cl == NULL)
325 return (NULL);
326
327 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
328 M_NOWAIT | M_ZERO);
329 if (cl->cl_q == NULL)
330 goto err_ret;
331 }
332
333 pif->pif_classes[pri] = cl;
334 if (flags & PRCF_DEFAULTCLASS)
335 pif->pif_default = cl;
336 if (qlimit == 0)
337 qlimit = 50; /* use default */
338 qlimit(cl->cl_q) = qlimit;
339 qtype(cl->cl_q) = Q_DROPTAIL;
340 qlen(cl->cl_q) = 0;
341 qsize(cl->cl_q) = 0;
342 cl->cl_flags = flags;
343 cl->cl_pri = pri;
344 if (pri > pif->pif_maxpri)
345 pif->pif_maxpri = pri;
346 cl->cl_pif = pif;
347 cl->cl_handle = qid;
348
349 #ifdef ALTQ_RED
350 if (flags & (PRCF_RED|PRCF_RIO)) {
351 int red_flags, red_pkttime;
352
353 red_flags = 0;
354 if (flags & PRCF_ECN)
355 red_flags |= REDF_ECN;
356 #ifdef ALTQ_RIO
357 if (flags & PRCF_CLEARDSCP)
358 red_flags |= RIOF_CLEARDSCP;
359 #endif
360 if (pif->pif_bandwidth < 8)
361 red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
362 else
363 red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
364 * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
365 #ifdef ALTQ_RIO
366 if (flags & PRCF_RIO) {
367 cl->cl_red = (red_t *)rio_alloc(0, NULL,
368 red_flags, red_pkttime);
369 if (cl->cl_red == NULL)
370 goto err_ret;
371 qtype(cl->cl_q) = Q_RIO;
372 } else
373 #endif
374 if (flags & PRCF_RED) {
375 cl->cl_red = red_alloc(0, 0,
376 qlimit(cl->cl_q) * 10/100,
377 qlimit(cl->cl_q) * 30/100,
378 red_flags, red_pkttime);
379 if (cl->cl_red == NULL)
380 goto err_ret;
381 qtype(cl->cl_q) = Q_RED;
382 }
383 }
384 #endif /* ALTQ_RED */
385 #ifdef ALTQ_CODEL
386 if (flags & PRCF_CODEL) {
387 cl->cl_codel = codel_alloc(5, 100, 0);
388 if (cl->cl_codel != NULL)
389 qtype(cl->cl_q) = Q_CODEL;
390 }
391 #endif
392
393 return (cl);
394
395 err_ret:
396 if (cl->cl_red != NULL) {
397 #ifdef ALTQ_RIO
398 if (q_is_rio(cl->cl_q))
399 rio_destroy((rio_t *)cl->cl_red);
400 #endif
401 #ifdef ALTQ_RED
402 if (q_is_red(cl->cl_q))
403 red_destroy(cl->cl_red);
404 #endif
405 #ifdef ALTQ_CODEL
406 if (q_is_codel(cl->cl_q))
407 codel_destroy(cl->cl_codel);
408 #endif
409 }
410 if (cl->cl_q != NULL)
411 free(cl->cl_q, M_DEVBUF);
412 free(cl, M_DEVBUF);
413 return (NULL);
414 }
415
416 static int
priq_class_destroy(struct priq_class * cl)417 priq_class_destroy(struct priq_class *cl)
418 {
419 struct priq_if *pif;
420 int s, pri;
421
422 s = splnet();
423 IFQ_LOCK(cl->cl_pif->pif_ifq);
424
425 #ifdef ALTQ3_CLFIER_COMPAT
426 /* delete filters referencing to this class */
427 acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
428 #endif
429
430 if (!qempty(cl->cl_q))
431 priq_purgeq(cl);
432
433 pif = cl->cl_pif;
434 pif->pif_classes[cl->cl_pri] = NULL;
435 if (pif->pif_maxpri == cl->cl_pri) {
436 for (pri = cl->cl_pri; pri >= 0; pri--)
437 if (pif->pif_classes[pri] != NULL) {
438 pif->pif_maxpri = pri;
439 break;
440 }
441 if (pri < 0)
442 pif->pif_maxpri = -1;
443 }
444 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
445 splx(s);
446
447 if (cl->cl_red != NULL) {
448 #ifdef ALTQ_RIO
449 if (q_is_rio(cl->cl_q))
450 rio_destroy((rio_t *)cl->cl_red);
451 #endif
452 #ifdef ALTQ_RED
453 if (q_is_red(cl->cl_q))
454 red_destroy(cl->cl_red);
455 #endif
456 #ifdef ALTQ_CODEL
457 if (q_is_codel(cl->cl_q))
458 codel_destroy(cl->cl_codel);
459 #endif
460 }
461 free(cl->cl_q, M_DEVBUF);
462 free(cl, M_DEVBUF);
463 return (0);
464 }
465
466 /*
467 * priq_enqueue is an enqueue function to be registered to
468 * (*altq_enqueue) in struct ifaltq.
469 */
470 static int
priq_enqueue(struct ifaltq * ifq,struct mbuf * m,struct altq_pktattr * pktattr)471 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
472 {
473 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
474 struct priq_class *cl;
475 struct pf_mtag *t;
476 int len;
477
478 IFQ_LOCK_ASSERT(ifq);
479
480 /* grab class set by classifier */
481 if ((m->m_flags & M_PKTHDR) == 0) {
482 /* should not happen */
483 printf("altq: packet for %s does not have pkthdr\n",
484 ifq->altq_ifp->if_xname);
485 m_freem(m);
486 return (ENOBUFS);
487 }
488 cl = NULL;
489 if ((t = pf_find_mtag(m)) != NULL)
490 cl = clh_to_clp(pif, t->qid);
491 #ifdef ALTQ3_COMPAT
492 else if ((ifq->altq_flags & ALTQF_CLASSIFY) && pktattr != NULL)
493 cl = pktattr->pattr_class;
494 #endif
495 if (cl == NULL) {
496 cl = pif->pif_default;
497 if (cl == NULL) {
498 m_freem(m);
499 return (ENOBUFS);
500 }
501 }
502 #ifdef ALTQ3_COMPAT
503 if (pktattr != NULL)
504 cl->cl_pktattr = pktattr; /* save proto hdr used by ECN */
505 else
506 #endif
507 cl->cl_pktattr = NULL;
508 len = m_pktlen(m);
509 if (priq_addq(cl, m) != 0) {
510 /* drop occurred. mbuf was freed in priq_addq. */
511 PKTCNTR_ADD(&cl->cl_dropcnt, len);
512 return (ENOBUFS);
513 }
514 IFQ_INC_LEN(ifq);
515
516 /* successfully queued. */
517 return (0);
518 }
519
520 /*
521 * priq_dequeue is a dequeue function to be registered to
522 * (*altq_dequeue) in struct ifaltq.
523 *
524 * note: ALTDQ_POLL returns the next packet without removing the packet
525 * from the queue. ALTDQ_REMOVE is a normal dequeue operation.
526 * ALTDQ_REMOVE must return the same packet if called immediately
527 * after ALTDQ_POLL.
528 */
529 static struct mbuf *
priq_dequeue(struct ifaltq * ifq,int op)530 priq_dequeue(struct ifaltq *ifq, int op)
531 {
532 struct priq_if *pif = (struct priq_if *)ifq->altq_disc;
533 struct priq_class *cl;
534 struct mbuf *m;
535 int pri;
536
537 IFQ_LOCK_ASSERT(ifq);
538
539 if (IFQ_IS_EMPTY(ifq))
540 /* no packet in the queue */
541 return (NULL);
542
543 for (pri = pif->pif_maxpri; pri >= 0; pri--) {
544 if ((cl = pif->pif_classes[pri]) != NULL &&
545 !qempty(cl->cl_q)) {
546 if (op == ALTDQ_POLL)
547 return (priq_pollq(cl));
548
549 m = priq_getq(cl);
550 if (m != NULL) {
551 IFQ_DEC_LEN(ifq);
552 if (qempty(cl->cl_q))
553 cl->cl_period++;
554 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
555 }
556 return (m);
557 }
558 }
559 return (NULL);
560 }
561
562 static int
priq_addq(struct priq_class * cl,struct mbuf * m)563 priq_addq(struct priq_class *cl, struct mbuf *m)
564 {
565
566 #ifdef ALTQ_RIO
567 if (q_is_rio(cl->cl_q))
568 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
569 cl->cl_pktattr);
570 #endif
571 #ifdef ALTQ_RED
572 if (q_is_red(cl->cl_q))
573 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
574 #endif
575 #ifdef ALTQ_CODEL
576 if (q_is_codel(cl->cl_q))
577 return codel_addq(cl->cl_codel, cl->cl_q, m);
578 #endif
579 if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
580 m_freem(m);
581 return (-1);
582 }
583
584 if (cl->cl_flags & PRCF_CLEARDSCP)
585 write_dsfield(m, cl->cl_pktattr, 0);
586
587 _addq(cl->cl_q, m);
588
589 return (0);
590 }
591
592 static struct mbuf *
priq_getq(struct priq_class * cl)593 priq_getq(struct priq_class *cl)
594 {
595 #ifdef ALTQ_RIO
596 if (q_is_rio(cl->cl_q))
597 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
598 #endif
599 #ifdef ALTQ_RED
600 if (q_is_red(cl->cl_q))
601 return red_getq(cl->cl_red, cl->cl_q);
602 #endif
603 #ifdef ALTQ_CODEL
604 if (q_is_codel(cl->cl_q))
605 return codel_getq(cl->cl_codel, cl->cl_q);
606 #endif
607 return _getq(cl->cl_q);
608 }
609
610 static struct mbuf *
priq_pollq(cl)611 priq_pollq(cl)
612 struct priq_class *cl;
613 {
614 return qhead(cl->cl_q);
615 }
616
617 static void
priq_purgeq(struct priq_class * cl)618 priq_purgeq(struct priq_class *cl)
619 {
620 struct mbuf *m;
621
622 if (qempty(cl->cl_q))
623 return;
624
625 while ((m = _getq(cl->cl_q)) != NULL) {
626 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
627 m_freem(m);
628 }
629 ASSERT(qlen(cl->cl_q) == 0);
630 }
631
632 static void
get_class_stats(struct priq_classstats * sp,struct priq_class * cl)633 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
634 {
635 sp->class_handle = cl->cl_handle;
636 sp->qlength = qlen(cl->cl_q);
637 sp->qlimit = qlimit(cl->cl_q);
638 sp->period = cl->cl_period;
639 sp->xmitcnt = cl->cl_xmitcnt;
640 sp->dropcnt = cl->cl_dropcnt;
641
642 sp->qtype = qtype(cl->cl_q);
643 #ifdef ALTQ_RED
644 if (q_is_red(cl->cl_q))
645 red_getstats(cl->cl_red, &sp->red[0]);
646 #endif
647 #ifdef ALTQ_RIO
648 if (q_is_rio(cl->cl_q))
649 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
650 #endif
651 #ifdef ALTQ_CODEL
652 if (q_is_codel(cl->cl_q))
653 codel_getstats(cl->cl_codel, &sp->codel);
654 #endif
655 }
656
657 /* convert a class handle to the corresponding class pointer */
658 static struct priq_class *
clh_to_clp(struct priq_if * pif,u_int32_t chandle)659 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
660 {
661 struct priq_class *cl;
662 int idx;
663
664 if (chandle == 0)
665 return (NULL);
666
667 for (idx = pif->pif_maxpri; idx >= 0; idx--)
668 if ((cl = pif->pif_classes[idx]) != NULL &&
669 cl->cl_handle == chandle)
670 return (cl);
671
672 return (NULL);
673 }
674
675
676 #ifdef ALTQ3_COMPAT
677
678 static struct priq_if *
priq_attach(ifq,bandwidth)679 priq_attach(ifq, bandwidth)
680 struct ifaltq *ifq;
681 u_int bandwidth;
682 {
683 struct priq_if *pif;
684
685 pif = malloc(sizeof(struct priq_if),
686 M_DEVBUF, M_WAITOK);
687 if (pif == NULL)
688 return (NULL);
689 bzero(pif, sizeof(struct priq_if));
690 pif->pif_bandwidth = bandwidth;
691 pif->pif_maxpri = -1;
692 pif->pif_ifq = ifq;
693
694 /* add this state to the priq list */
695 pif->pif_next = pif_list;
696 pif_list = pif;
697
698 return (pif);
699 }
700
701 static int
priq_detach(pif)702 priq_detach(pif)
703 struct priq_if *pif;
704 {
705 (void)priq_clear_interface(pif);
706
707 /* remove this interface from the pif list */
708 if (pif_list == pif)
709 pif_list = pif->pif_next;
710 else {
711 struct priq_if *p;
712
713 for (p = pif_list; p != NULL; p = p->pif_next)
714 if (p->pif_next == pif) {
715 p->pif_next = pif->pif_next;
716 break;
717 }
718 ASSERT(p != NULL);
719 }
720
721 free(pif, M_DEVBUF);
722 return (0);
723 }
724
725 /*
726 * priq device interface
727 */
728 int
priqopen(dev,flag,fmt,p)729 priqopen(dev, flag, fmt, p)
730 dev_t dev;
731 int flag, fmt;
732 #if (__FreeBSD_version > 500000)
733 struct thread *p;
734 #else
735 struct proc *p;
736 #endif
737 {
738 /* everything will be done when the queueing scheme is attached. */
739 return 0;
740 }
741
742 int
priqclose(dev,flag,fmt,p)743 priqclose(dev, flag, fmt, p)
744 dev_t dev;
745 int flag, fmt;
746 #if (__FreeBSD_version > 500000)
747 struct thread *p;
748 #else
749 struct proc *p;
750 #endif
751 {
752 struct priq_if *pif;
753 int err, error = 0;
754
755 while ((pif = pif_list) != NULL) {
756 /* destroy all */
757 if (ALTQ_IS_ENABLED(pif->pif_ifq))
758 altq_disable(pif->pif_ifq);
759
760 err = altq_detach(pif->pif_ifq);
761 if (err == 0)
762 err = priq_detach(pif);
763 if (err != 0 && error == 0)
764 error = err;
765 }
766
767 return error;
768 }
769
770 int
priqioctl(dev,cmd,addr,flag,p)771 priqioctl(dev, cmd, addr, flag, p)
772 dev_t dev;
773 ioctlcmd_t cmd;
774 caddr_t addr;
775 int flag;
776 #if (__FreeBSD_version > 500000)
777 struct thread *p;
778 #else
779 struct proc *p;
780 #endif
781 {
782 struct priq_if *pif;
783 struct priq_interface *ifacep;
784 int error = 0;
785
786 /* check super-user privilege */
787 switch (cmd) {
788 case PRIQ_GETSTATS:
789 break;
790 default:
791 #if (__FreeBSD_version > 700000)
792 if ((error = priv_check(p, PRIV_ALTQ_MANAGE)) != 0)
793 return (error);
794 #elsif (__FreeBSD_version > 400000)
795 if ((error = suser(p)) != 0)
796 return (error);
797 #else
798 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
799 return (error);
800 #endif
801 break;
802 }
803
804 switch (cmd) {
805
806 case PRIQ_IF_ATTACH:
807 error = priqcmd_if_attach((struct priq_interface *)addr);
808 break;
809
810 case PRIQ_IF_DETACH:
811 error = priqcmd_if_detach((struct priq_interface *)addr);
812 break;
813
814 case PRIQ_ENABLE:
815 case PRIQ_DISABLE:
816 case PRIQ_CLEAR:
817 ifacep = (struct priq_interface *)addr;
818 if ((pif = altq_lookup(ifacep->ifname,
819 ALTQT_PRIQ)) == NULL) {
820 error = EBADF;
821 break;
822 }
823
824 switch (cmd) {
825 case PRIQ_ENABLE:
826 if (pif->pif_default == NULL) {
827 #ifdef ALTQ_DEBUG
828 printf("priq: no default class\n");
829 #endif
830 error = EINVAL;
831 break;
832 }
833 error = altq_enable(pif->pif_ifq);
834 break;
835
836 case PRIQ_DISABLE:
837 error = altq_disable(pif->pif_ifq);
838 break;
839
840 case PRIQ_CLEAR:
841 priq_clear_interface(pif);
842 break;
843 }
844 break;
845
846 case PRIQ_ADD_CLASS:
847 error = priqcmd_add_class((struct priq_add_class *)addr);
848 break;
849
850 case PRIQ_DEL_CLASS:
851 error = priqcmd_delete_class((struct priq_delete_class *)addr);
852 break;
853
854 case PRIQ_MOD_CLASS:
855 error = priqcmd_modify_class((struct priq_modify_class *)addr);
856 break;
857
858 case PRIQ_ADD_FILTER:
859 error = priqcmd_add_filter((struct priq_add_filter *)addr);
860 break;
861
862 case PRIQ_DEL_FILTER:
863 error = priqcmd_delete_filter((struct priq_delete_filter *)addr);
864 break;
865
866 case PRIQ_GETSTATS:
867 error = priqcmd_class_stats((struct priq_class_stats *)addr);
868 break;
869
870 default:
871 error = EINVAL;
872 break;
873 }
874 return error;
875 }
876
877 static int
priqcmd_if_attach(ap)878 priqcmd_if_attach(ap)
879 struct priq_interface *ap;
880 {
881 struct priq_if *pif;
882 struct ifnet *ifp;
883 int error;
884
885 if ((ifp = ifunit(ap->ifname)) == NULL)
886 return (ENXIO);
887
888 if ((pif = priq_attach(&ifp->if_snd, ap->arg)) == NULL)
889 return (ENOMEM);
890
891 /*
892 * set PRIQ to this ifnet structure.
893 */
894 if ((error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, pif,
895 priq_enqueue, priq_dequeue, priq_request,
896 &pif->pif_classifier, acc_classify)) != 0)
897 (void)priq_detach(pif);
898
899 return (error);
900 }
901
902 static int
priqcmd_if_detach(ap)903 priqcmd_if_detach(ap)
904 struct priq_interface *ap;
905 {
906 struct priq_if *pif;
907 int error;
908
909 if ((pif = altq_lookup(ap->ifname, ALTQT_PRIQ)) == NULL)
910 return (EBADF);
911
912 if (ALTQ_IS_ENABLED(pif->pif_ifq))
913 altq_disable(pif->pif_ifq);
914
915 if ((error = altq_detach(pif->pif_ifq)))
916 return (error);
917
918 return priq_detach(pif);
919 }
920
921 static int
priqcmd_add_class(ap)922 priqcmd_add_class(ap)
923 struct priq_add_class *ap;
924 {
925 struct priq_if *pif;
926 struct priq_class *cl;
927 int qid;
928
929 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
930 return (EBADF);
931
932 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
933 return (EINVAL);
934 if (pif->pif_classes[ap->pri] != NULL)
935 return (EBUSY);
936
937 qid = ap->pri + 1;
938 if ((cl = priq_class_create(pif, ap->pri,
939 ap->qlimit, ap->flags, qid)) == NULL)
940 return (ENOMEM);
941
942 /* return a class handle to the user */
943 ap->class_handle = cl->cl_handle;
944
945 return (0);
946 }
947
948 static int
priqcmd_delete_class(ap)949 priqcmd_delete_class(ap)
950 struct priq_delete_class *ap;
951 {
952 struct priq_if *pif;
953 struct priq_class *cl;
954
955 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
956 return (EBADF);
957
958 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
959 return (EINVAL);
960
961 return priq_class_destroy(cl);
962 }
963
964 static int
priqcmd_modify_class(ap)965 priqcmd_modify_class(ap)
966 struct priq_modify_class *ap;
967 {
968 struct priq_if *pif;
969 struct priq_class *cl;
970
971 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
972 return (EBADF);
973
974 if (ap->pri < 0 || ap->pri >= PRIQ_MAXPRI)
975 return (EINVAL);
976
977 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
978 return (EINVAL);
979
980 /*
981 * if priority is changed, move the class to the new priority
982 */
983 if (pif->pif_classes[ap->pri] != cl) {
984 if (pif->pif_classes[ap->pri] != NULL)
985 return (EEXIST);
986 pif->pif_classes[cl->cl_pri] = NULL;
987 pif->pif_classes[ap->pri] = cl;
988 cl->cl_pri = ap->pri;
989 }
990
991 /* call priq_class_create to change class parameters */
992 if ((cl = priq_class_create(pif, ap->pri,
993 ap->qlimit, ap->flags, ap->class_handle)) == NULL)
994 return (ENOMEM);
995 return 0;
996 }
997
998 static int
priqcmd_add_filter(ap)999 priqcmd_add_filter(ap)
1000 struct priq_add_filter *ap;
1001 {
1002 struct priq_if *pif;
1003 struct priq_class *cl;
1004
1005 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1006 return (EBADF);
1007
1008 if ((cl = clh_to_clp(pif, ap->class_handle)) == NULL)
1009 return (EINVAL);
1010
1011 return acc_add_filter(&pif->pif_classifier, &ap->filter,
1012 cl, &ap->filter_handle);
1013 }
1014
1015 static int
priqcmd_delete_filter(ap)1016 priqcmd_delete_filter(ap)
1017 struct priq_delete_filter *ap;
1018 {
1019 struct priq_if *pif;
1020
1021 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1022 return (EBADF);
1023
1024 return acc_delete_filter(&pif->pif_classifier,
1025 ap->filter_handle);
1026 }
1027
1028 static int
priqcmd_class_stats(ap)1029 priqcmd_class_stats(ap)
1030 struct priq_class_stats *ap;
1031 {
1032 struct priq_if *pif;
1033 struct priq_class *cl;
1034 struct priq_classstats stats, *usp;
1035 int pri, error;
1036
1037 if ((pif = altq_lookup(ap->iface.ifname, ALTQT_PRIQ)) == NULL)
1038 return (EBADF);
1039
1040 ap->maxpri = pif->pif_maxpri;
1041
1042 /* then, read the next N classes in the tree */
1043 usp = ap->stats;
1044 for (pri = 0; pri <= pif->pif_maxpri; pri++) {
1045 cl = pif->pif_classes[pri];
1046 if (cl != NULL)
1047 get_class_stats(&stats, cl);
1048 else
1049 bzero(&stats, sizeof(stats));
1050 if ((error = copyout((caddr_t)&stats, (caddr_t)usp++,
1051 sizeof(stats))) != 0)
1052 return (error);
1053 }
1054 return (0);
1055 }
1056
1057 #ifdef KLD_MODULE
1058
1059 static struct altqsw priq_sw =
1060 {"priq", priqopen, priqclose, priqioctl};
1061
1062 ALTQ_MODULE(altq_priq, ALTQT_PRIQ, &priq_sw);
1063 MODULE_DEPEND(altq_priq, altq_red, 1, 1, 1);
1064 MODULE_DEPEND(altq_priq, altq_rio, 1, 1, 1);
1065
1066 #endif /* KLD_MODULE */
1067
1068 #endif /* ALTQ3_COMPAT */
1069 #endif /* ALTQ_PRIQ */
1070