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