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