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