]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/altq/altq_priq.c
Merge libc++ trunk r351319, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / net / altq / altq_priq.c
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 #include <net/altq/altq_priq.h>
59
60 /*
61  * function prototypes
62  */
63 static int priq_clear_interface(struct priq_if *);
64 static int priq_request(struct ifaltq *, int, void *);
65 static void priq_purge(struct priq_if *);
66 static struct priq_class *priq_class_create(struct priq_if *, int, int, int,
67     int);
68 static int priq_class_destroy(struct priq_class *);
69 static int priq_enqueue(struct ifaltq *, struct mbuf *, struct altq_pktattr *);
70 static struct mbuf *priq_dequeue(struct ifaltq *, int);
71
72 static int priq_addq(struct priq_class *, struct mbuf *);
73 static struct mbuf *priq_getq(struct priq_class *);
74 static struct mbuf *priq_pollq(struct priq_class *);
75 static void priq_purgeq(struct priq_class *);
76
77
78 static void get_class_stats(struct priq_classstats *, struct priq_class *);
79 static struct priq_class *clh_to_clp(struct priq_if *, u_int32_t);
80
81
82 int
83 priq_pfattach(struct pf_altq *a)
84 {
85         struct ifnet *ifp;
86         int s, error;
87
88         if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
89                 return (EINVAL);
90         s = splnet();
91         error = altq_attach(&ifp->if_snd, ALTQT_PRIQ, a->altq_disc,
92             priq_enqueue, priq_dequeue, priq_request, NULL, NULL);
93         splx(s);
94         return (error);
95 }
96
97 int
98 priq_add_altq(struct pf_altq *a)
99 {
100         struct priq_if  *pif;
101         struct ifnet    *ifp;
102
103         if ((ifp = ifunit(a->ifname)) == NULL)
104                 return (EINVAL);
105         if (!ALTQ_IS_READY(&ifp->if_snd))
106                 return (ENODEV);
107
108         pif = malloc(sizeof(struct priq_if), M_DEVBUF, M_NOWAIT | M_ZERO);
109         if (pif == NULL)
110                 return (ENOMEM);
111         pif->pif_bandwidth = a->ifbandwidth;
112         pif->pif_maxpri = -1;
113         pif->pif_ifq = &ifp->if_snd;
114
115         /* keep the state in pf_altq */
116         a->altq_disc = pif;
117
118         return (0);
119 }
120
121 int
122 priq_remove_altq(struct pf_altq *a)
123 {
124         struct priq_if *pif;
125
126         if ((pif = a->altq_disc) == NULL)
127                 return (EINVAL);
128         a->altq_disc = NULL;
129
130         (void)priq_clear_interface(pif);
131
132         free(pif, M_DEVBUF);
133         return (0);
134 }
135
136 int
137 priq_add_queue(struct pf_altq *a)
138 {
139         struct priq_if *pif;
140         struct priq_class *cl;
141
142         if ((pif = a->altq_disc) == NULL)
143                 return (EINVAL);
144
145         /* check parameters */
146         if (a->priority >= PRIQ_MAXPRI)
147                 return (EINVAL);
148         if (a->qid == 0)
149                 return (EINVAL);
150         if (pif->pif_classes[a->priority] != NULL)
151                 return (EBUSY);
152         if (clh_to_clp(pif, a->qid) != NULL)
153                 return (EBUSY);
154
155         cl = priq_class_create(pif, a->priority, a->qlimit,
156             a->pq_u.priq_opts.flags, a->qid);
157         if (cl == NULL)
158                 return (ENOMEM);
159
160         return (0);
161 }
162
163 int
164 priq_remove_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         if ((cl = clh_to_clp(pif, a->qid)) == NULL)
173                 return (EINVAL);
174
175         return (priq_class_destroy(cl));
176 }
177
178 int
179 priq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
180 {
181         struct priq_if *pif;
182         struct priq_class *cl;
183         struct priq_classstats stats;
184         int error = 0;
185
186         if ((pif = altq_lookup(a->ifname, ALTQT_PRIQ)) == NULL)
187                 return (EBADF);
188
189         if ((cl = clh_to_clp(pif, a->qid)) == NULL)
190                 return (EINVAL);
191
192         if (*nbytes < sizeof(stats))
193                 return (EINVAL);
194
195         get_class_stats(&stats, cl);
196
197         if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
198                 return (error);
199         *nbytes = sizeof(stats);
200         return (0);
201 }
202
203 /*
204  * bring the interface back to the initial state by discarding
205  * all the filters and classes.
206  */
207 static int
208 priq_clear_interface(struct priq_if *pif)
209 {
210         struct priq_class       *cl;
211         int pri;
212
213 #ifdef ALTQ3_CLFIER_COMPAT
214         /* free the filters for this interface */
215         acc_discard_filters(&pif->pif_classifier, NULL, 1);
216 #endif
217
218         /* clear out the classes */
219         for (pri = 0; pri <= pif->pif_maxpri; pri++)
220                 if ((cl = pif->pif_classes[pri]) != NULL)
221                         priq_class_destroy(cl);
222
223         return (0);
224 }
225
226 static int
227 priq_request(struct ifaltq *ifq, int req, void *arg)
228 {
229         struct priq_if  *pif = (struct priq_if *)ifq->altq_disc;
230
231         IFQ_LOCK_ASSERT(ifq);
232
233         switch (req) {
234         case ALTRQ_PURGE:
235                 priq_purge(pif);
236                 break;
237         }
238         return (0);
239 }
240
241 /* discard all the queued packets on the interface */
242 static void
243 priq_purge(struct priq_if *pif)
244 {
245         struct priq_class *cl;
246         int pri;
247
248         for (pri = 0; pri <= pif->pif_maxpri; pri++) {
249                 if ((cl = pif->pif_classes[pri]) != NULL && !qempty(cl->cl_q))
250                         priq_purgeq(cl);
251         }
252         if (ALTQ_IS_ENABLED(pif->pif_ifq))
253                 pif->pif_ifq->ifq_len = 0;
254 }
255
256 static struct priq_class *
257 priq_class_create(struct priq_if *pif, int pri, int qlimit, int flags, int qid)
258 {
259         struct priq_class *cl;
260         int s;
261
262 #ifndef ALTQ_RED
263         if (flags & PRCF_RED) {
264 #ifdef ALTQ_DEBUG
265                 printf("priq_class_create: RED not configured for PRIQ!\n");
266 #endif
267                 return (NULL);
268         }
269 #endif
270 #ifndef ALTQ_CODEL
271         if (flags & PRCF_CODEL) {
272 #ifdef ALTQ_DEBUG
273                 printf("priq_class_create: CODEL not configured for PRIQ!\n");
274 #endif
275                 return (NULL);
276         }
277 #endif
278
279         if ((cl = pif->pif_classes[pri]) != NULL) {
280                 /* modify the class instead of creating a new one */
281                 s = splnet();
282                 IFQ_LOCK(cl->cl_pif->pif_ifq);
283                 if (!qempty(cl->cl_q))
284                         priq_purgeq(cl);
285                 IFQ_UNLOCK(cl->cl_pif->pif_ifq);
286                 splx(s);
287 #ifdef ALTQ_RIO
288                 if (q_is_rio(cl->cl_q))
289                         rio_destroy((rio_t *)cl->cl_red);
290 #endif
291 #ifdef ALTQ_RED
292                 if (q_is_red(cl->cl_q))
293                         red_destroy(cl->cl_red);
294 #endif
295 #ifdef ALTQ_CODEL
296                 if (q_is_codel(cl->cl_q))
297                         codel_destroy(cl->cl_codel);
298 #endif
299         } else {
300                 cl = malloc(sizeof(struct priq_class), M_DEVBUF,
301                     M_NOWAIT | M_ZERO);
302                 if (cl == NULL)
303                         return (NULL);
304
305                 cl->cl_q = malloc(sizeof(class_queue_t), M_DEVBUF,
306                     M_NOWAIT | M_ZERO);
307                 if (cl->cl_q == NULL)
308                         goto err_ret;
309         }
310
311         pif->pif_classes[pri] = cl;
312         if (flags & PRCF_DEFAULTCLASS)
313                 pif->pif_default = cl;
314         if (qlimit == 0)
315                 qlimit = 50;  /* use default */
316         qlimit(cl->cl_q) = qlimit;
317         qtype(cl->cl_q) = Q_DROPTAIL;
318         qlen(cl->cl_q) = 0;
319         qsize(cl->cl_q) = 0;
320         cl->cl_flags = flags;
321         cl->cl_pri = pri;
322         if (pri > pif->pif_maxpri)
323                 pif->pif_maxpri = pri;
324         cl->cl_pif = pif;
325         cl->cl_handle = qid;
326
327 #ifdef ALTQ_RED
328         if (flags & (PRCF_RED|PRCF_RIO)) {
329                 int red_flags, red_pkttime;
330
331                 red_flags = 0;
332                 if (flags & PRCF_ECN)
333                         red_flags |= REDF_ECN;
334 #ifdef ALTQ_RIO
335                 if (flags & PRCF_CLEARDSCP)
336                         red_flags |= RIOF_CLEARDSCP;
337 #endif
338                 if (pif->pif_bandwidth < 8)
339                         red_pkttime = 1000 * 1000 * 1000; /* 1 sec */
340                 else
341                         red_pkttime = (int64_t)pif->pif_ifq->altq_ifp->if_mtu
342                           * 1000 * 1000 * 1000 / (pif->pif_bandwidth / 8);
343 #ifdef ALTQ_RIO
344                 if (flags & PRCF_RIO) {
345                         cl->cl_red = (red_t *)rio_alloc(0, NULL,
346                                                 red_flags, red_pkttime);
347                         if (cl->cl_red == NULL)
348                                 goto err_ret;
349                         qtype(cl->cl_q) = Q_RIO;
350                 } else
351 #endif
352                 if (flags & PRCF_RED) {
353                         cl->cl_red = red_alloc(0, 0,
354                             qlimit(cl->cl_q) * 10/100,
355                             qlimit(cl->cl_q) * 30/100,
356                             red_flags, red_pkttime);
357                         if (cl->cl_red == NULL)
358                                 goto err_ret;
359                         qtype(cl->cl_q) = Q_RED;
360                 }
361         }
362 #endif /* ALTQ_RED */
363 #ifdef ALTQ_CODEL
364         if (flags & PRCF_CODEL) {
365                 cl->cl_codel = codel_alloc(5, 100, 0);
366                 if (cl->cl_codel != NULL)
367                         qtype(cl->cl_q) = Q_CODEL;
368         }
369 #endif
370
371         return (cl);
372
373  err_ret:
374         if (cl->cl_red != NULL) {
375 #ifdef ALTQ_RIO
376                 if (q_is_rio(cl->cl_q))
377                         rio_destroy((rio_t *)cl->cl_red);
378 #endif
379 #ifdef ALTQ_RED
380                 if (q_is_red(cl->cl_q))
381                         red_destroy(cl->cl_red);
382 #endif
383 #ifdef ALTQ_CODEL
384                 if (q_is_codel(cl->cl_q))
385                         codel_destroy(cl->cl_codel);
386 #endif
387         }
388         if (cl->cl_q != NULL)
389                 free(cl->cl_q, M_DEVBUF);
390         free(cl, M_DEVBUF);
391         return (NULL);
392 }
393
394 static int
395 priq_class_destroy(struct priq_class *cl)
396 {
397         struct priq_if *pif;
398         int s, pri;
399
400         s = splnet();
401         IFQ_LOCK(cl->cl_pif->pif_ifq);
402
403 #ifdef ALTQ3_CLFIER_COMPAT
404         /* delete filters referencing to this class */
405         acc_discard_filters(&cl->cl_pif->pif_classifier, cl, 0);
406 #endif
407
408         if (!qempty(cl->cl_q))
409                 priq_purgeq(cl);
410
411         pif = cl->cl_pif;
412         pif->pif_classes[cl->cl_pri] = NULL;
413         if (pif->pif_maxpri == cl->cl_pri) {
414                 for (pri = cl->cl_pri; pri >= 0; pri--)
415                         if (pif->pif_classes[pri] != NULL) {
416                                 pif->pif_maxpri = pri;
417                                 break;
418                         }
419                 if (pri < 0)
420                         pif->pif_maxpri = -1;
421         }
422         IFQ_UNLOCK(cl->cl_pif->pif_ifq);
423         splx(s);
424
425         if (cl->cl_red != NULL) {
426 #ifdef ALTQ_RIO
427                 if (q_is_rio(cl->cl_q))
428                         rio_destroy((rio_t *)cl->cl_red);
429 #endif
430 #ifdef ALTQ_RED
431                 if (q_is_red(cl->cl_q))
432                         red_destroy(cl->cl_red);
433 #endif
434 #ifdef ALTQ_CODEL
435                 if (q_is_codel(cl->cl_q))
436                         codel_destroy(cl->cl_codel);
437 #endif
438         }
439         free(cl->cl_q, M_DEVBUF);
440         free(cl, M_DEVBUF);
441         return (0);
442 }
443
444 /*
445  * priq_enqueue is an enqueue function to be registered to
446  * (*altq_enqueue) in struct ifaltq.
447  */
448 static int
449 priq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
450 {
451         struct priq_if  *pif = (struct priq_if *)ifq->altq_disc;
452         struct priq_class *cl;
453         struct pf_mtag *t;
454         int len;
455
456         IFQ_LOCK_ASSERT(ifq);
457
458         /* grab class set by classifier */
459         if ((m->m_flags & M_PKTHDR) == 0) {
460                 /* should not happen */
461                 printf("altq: packet for %s does not have pkthdr\n",
462                     ifq->altq_ifp->if_xname);
463                 m_freem(m);
464                 return (ENOBUFS);
465         }
466         cl = NULL;
467         if ((t = pf_find_mtag(m)) != NULL)
468                 cl = clh_to_clp(pif, t->qid);
469         if (cl == NULL) {
470                 cl = pif->pif_default;
471                 if (cl == NULL) {
472                         m_freem(m);
473                         return (ENOBUFS);
474                 }
475         }
476         cl->cl_pktattr = NULL;
477         len = m_pktlen(m);
478         if (priq_addq(cl, m) != 0) {
479                 /* drop occurred.  mbuf was freed in priq_addq. */
480                 PKTCNTR_ADD(&cl->cl_dropcnt, len);
481                 return (ENOBUFS);
482         }
483         IFQ_INC_LEN(ifq);
484
485         /* successfully queued. */
486         return (0);
487 }
488
489 /*
490  * priq_dequeue is a dequeue function to be registered to
491  * (*altq_dequeue) in struct ifaltq.
492  *
493  * note: ALTDQ_POLL returns the next packet without removing the packet
494  *      from the queue.  ALTDQ_REMOVE is a normal dequeue operation.
495  *      ALTDQ_REMOVE must return the same packet if called immediately
496  *      after ALTDQ_POLL.
497  */
498 static struct mbuf *
499 priq_dequeue(struct ifaltq *ifq, int op)
500 {
501         struct priq_if  *pif = (struct priq_if *)ifq->altq_disc;
502         struct priq_class *cl;
503         struct mbuf *m;
504         int pri;
505
506         IFQ_LOCK_ASSERT(ifq);
507
508         if (IFQ_IS_EMPTY(ifq))
509                 /* no packet in the queue */
510                 return (NULL);
511
512         for (pri = pif->pif_maxpri;  pri >= 0; pri--) {
513                 if ((cl = pif->pif_classes[pri]) != NULL &&
514                     !qempty(cl->cl_q)) {
515                         if (op == ALTDQ_POLL)
516                                 return (priq_pollq(cl));
517
518                         m = priq_getq(cl);
519                         if (m != NULL) {
520                                 IFQ_DEC_LEN(ifq);
521                                 if (qempty(cl->cl_q))
522                                         cl->cl_period++;
523                                 PKTCNTR_ADD(&cl->cl_xmitcnt, m_pktlen(m));
524                         }
525                         return (m);
526                 }
527         }
528         return (NULL);
529 }
530
531 static int
532 priq_addq(struct priq_class *cl, struct mbuf *m)
533 {
534
535 #ifdef ALTQ_RIO
536         if (q_is_rio(cl->cl_q))
537                 return rio_addq((rio_t *)cl->cl_red, cl->cl_q, m,
538                                 cl->cl_pktattr);
539 #endif
540 #ifdef ALTQ_RED
541         if (q_is_red(cl->cl_q))
542                 return red_addq(cl->cl_red, cl->cl_q, m, cl->cl_pktattr);
543 #endif
544 #ifdef ALTQ_CODEL
545         if (q_is_codel(cl->cl_q))
546                 return codel_addq(cl->cl_codel, cl->cl_q, m);
547 #endif
548         if (qlen(cl->cl_q) >= qlimit(cl->cl_q)) {
549                 m_freem(m);
550                 return (-1);
551         }
552
553         if (cl->cl_flags & PRCF_CLEARDSCP)
554                 write_dsfield(m, cl->cl_pktattr, 0);
555
556         _addq(cl->cl_q, m);
557
558         return (0);
559 }
560
561 static struct mbuf *
562 priq_getq(struct priq_class *cl)
563 {
564 #ifdef ALTQ_RIO
565         if (q_is_rio(cl->cl_q))
566                 return rio_getq((rio_t *)cl->cl_red, cl->cl_q);
567 #endif
568 #ifdef ALTQ_RED
569         if (q_is_red(cl->cl_q))
570                 return red_getq(cl->cl_red, cl->cl_q);
571 #endif
572 #ifdef ALTQ_CODEL
573         if (q_is_codel(cl->cl_q))
574                 return codel_getq(cl->cl_codel, cl->cl_q);
575 #endif
576         return _getq(cl->cl_q);
577 }
578
579 static struct mbuf *
580 priq_pollq(cl)
581         struct priq_class *cl;
582 {
583         return qhead(cl->cl_q);
584 }
585
586 static void
587 priq_purgeq(struct priq_class *cl)
588 {
589         struct mbuf *m;
590
591         if (qempty(cl->cl_q))
592                 return;
593
594         while ((m = _getq(cl->cl_q)) != NULL) {
595                 PKTCNTR_ADD(&cl->cl_dropcnt, m_pktlen(m));
596                 m_freem(m);
597         }
598         ASSERT(qlen(cl->cl_q) == 0);
599 }
600
601 static void
602 get_class_stats(struct priq_classstats *sp, struct priq_class *cl)
603 {
604         sp->class_handle = cl->cl_handle;
605         sp->qlength = qlen(cl->cl_q);
606         sp->qlimit = qlimit(cl->cl_q);
607         sp->period = cl->cl_period;
608         sp->xmitcnt = cl->cl_xmitcnt;
609         sp->dropcnt = cl->cl_dropcnt;
610
611         sp->qtype = qtype(cl->cl_q);
612 #ifdef ALTQ_RED
613         if (q_is_red(cl->cl_q))
614                 red_getstats(cl->cl_red, &sp->red[0]);
615 #endif
616 #ifdef ALTQ_RIO
617         if (q_is_rio(cl->cl_q))
618                 rio_getstats((rio_t *)cl->cl_red, &sp->red[0]);
619 #endif
620 #ifdef ALTQ_CODEL
621         if (q_is_codel(cl->cl_q))
622                 codel_getstats(cl->cl_codel, &sp->codel);
623 #endif
624 }
625
626 /* convert a class handle to the corresponding class pointer */
627 static struct priq_class *
628 clh_to_clp(struct priq_if *pif, u_int32_t chandle)
629 {
630         struct priq_class *cl;
631         int idx;
632
633         if (chandle == 0)
634                 return (NULL);
635
636         for (idx = pif->pif_maxpri; idx >= 0; idx--)
637                 if ((cl = pif->pif_classes[idx]) != NULL &&
638                     cl->cl_handle == chandle)
639                         return (cl);
640
641         return (NULL);
642 }
643
644
645 #endif /* ALTQ_PRIQ */