1 /*-
2 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the SMCC Technology
18 * Development Group at Sun Microsystems, Inc.
19 *
20 * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
25 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is
26 * provided "as is" without express or implied warranty of any kind.
27 *
28 * These notices must be retained in any copies of any part of this software.
29 *
30 * $KAME: altq_cbq.c,v 1.19 2003/09/17 14:23:25 kjc Exp $
31 */
32
33 #include "opt_altq.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ 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/systm.h>
43 #include <sys/proc.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <netinet/in.h>
50
51 #include <netpfil/pf/pf.h>
52 #include <netpfil/pf/pf_altq.h>
53 #include <netpfil/pf/pf_mtag.h>
54 #include <net/altq/altq.h>
55 #include <net/altq/altq_cbq.h>
56
57 /*
58 * Forward Declarations.
59 */
60 static int cbq_class_destroy(cbq_state_t *, struct rm_class *);
61 static struct rm_class *clh_to_clp(cbq_state_t *, u_int32_t);
62 static int cbq_clear_interface(cbq_state_t *);
63 static int cbq_request(struct ifaltq *, int, void *);
64 static int cbq_enqueue(struct ifaltq *, struct mbuf *,
65 struct altq_pktattr *);
66 static struct mbuf *cbq_dequeue(struct ifaltq *, int);
67 static void cbqrestart(struct ifaltq *);
68 static void get_class_stats(class_stats_t *, struct rm_class *);
69 static void cbq_purge(cbq_state_t *);
70
71 /*
72 * int
73 * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
74 * function destroys a given traffic class. Before destroying
75 * the class, all traffic for that class is released.
76 */
77 static int
cbq_class_destroy(cbq_state_t * cbqp,struct rm_class * cl)78 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
79 {
80 int i;
81
82 /* delete the class */
83 rmc_delete_class(&cbqp->ifnp, cl);
84
85 /*
86 * free the class handle
87 */
88 for (i = 0; i < CBQ_MAX_CLASSES; i++)
89 if (cbqp->cbq_class_tbl[i] == cl)
90 cbqp->cbq_class_tbl[i] = NULL;
91
92 if (cl == cbqp->ifnp.root_)
93 cbqp->ifnp.root_ = NULL;
94 if (cl == cbqp->ifnp.default_)
95 cbqp->ifnp.default_ = NULL;
96 return (0);
97 }
98
99 /* convert class handle to class pointer */
100 static struct rm_class *
clh_to_clp(cbq_state_t * cbqp,u_int32_t chandle)101 clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle)
102 {
103 int i;
104 struct rm_class *cl;
105
106 if (chandle == 0)
107 return (NULL);
108 /*
109 * first, try optimistically the slot matching the lower bits of
110 * the handle. if it fails, do the linear table search.
111 */
112 i = chandle % CBQ_MAX_CLASSES;
113 if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
114 cl->stats_.handle == chandle)
115 return (cl);
116 for (i = 0; i < CBQ_MAX_CLASSES; i++)
117 if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
118 cl->stats_.handle == chandle)
119 return (cl);
120 return (NULL);
121 }
122
123 static int
cbq_clear_interface(cbq_state_t * cbqp)124 cbq_clear_interface(cbq_state_t *cbqp)
125 {
126 int again, i;
127 struct rm_class *cl;
128
129 #ifdef ALTQ3_CLFIER_COMPAT
130 /* free the filters for this interface */
131 acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
132 #endif
133
134 /* clear out the classes now */
135 do {
136 again = 0;
137 for (i = 0; i < CBQ_MAX_CLASSES; i++) {
138 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
139 if (is_a_parent_class(cl))
140 again++;
141 else {
142 cbq_class_destroy(cbqp, cl);
143 cbqp->cbq_class_tbl[i] = NULL;
144 if (cl == cbqp->ifnp.root_)
145 cbqp->ifnp.root_ = NULL;
146 if (cl == cbqp->ifnp.default_)
147 cbqp->ifnp.default_ = NULL;
148 }
149 }
150 }
151 } while (again);
152
153 return (0);
154 }
155
156 static int
cbq_request(struct ifaltq * ifq,int req,void * arg)157 cbq_request(struct ifaltq *ifq, int req, void *arg)
158 {
159 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
160
161 IFQ_LOCK_ASSERT(ifq);
162
163 switch (req) {
164 case ALTRQ_PURGE:
165 cbq_purge(cbqp);
166 break;
167 }
168 return (0);
169 }
170
171 /* copy the stats info in rm_class to class_states_t */
172 static void
get_class_stats(class_stats_t * statsp,struct rm_class * cl)173 get_class_stats(class_stats_t *statsp, struct rm_class *cl)
174 {
175 statsp->xmit_cnt = cl->stats_.xmit_cnt;
176 statsp->drop_cnt = cl->stats_.drop_cnt;
177 statsp->over = cl->stats_.over;
178 statsp->borrows = cl->stats_.borrows;
179 statsp->overactions = cl->stats_.overactions;
180 statsp->delays = cl->stats_.delays;
181
182 statsp->depth = cl->depth_;
183 statsp->priority = cl->pri_;
184 statsp->maxidle = cl->maxidle_;
185 statsp->minidle = cl->minidle_;
186 statsp->offtime = cl->offtime_;
187 statsp->qmax = qlimit(cl->q_);
188 statsp->ns_per_byte = cl->ns_per_byte_;
189 statsp->wrr_allot = cl->w_allotment_;
190 statsp->qcnt = qlen(cl->q_);
191 statsp->avgidle = cl->avgidle_;
192
193 statsp->qtype = qtype(cl->q_);
194 #ifdef ALTQ_RED
195 if (q_is_red(cl->q_))
196 red_getstats(cl->red_, &statsp->red[0]);
197 #endif
198 #ifdef ALTQ_RIO
199 if (q_is_rio(cl->q_))
200 rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
201 #endif
202 #ifdef ALTQ_CODEL
203 if (q_is_codel(cl->q_))
204 codel_getstats(cl->codel_, &statsp->codel);
205 #endif
206 }
207
208 int
cbq_pfattach(struct pf_altq * a)209 cbq_pfattach(struct pf_altq *a)
210 {
211 struct ifnet *ifp;
212 int s, error;
213
214 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
215 return (EINVAL);
216 s = splnet();
217 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
218 cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL);
219 splx(s);
220 return (error);
221 }
222
223 int
cbq_add_altq(struct ifnet * ifp,struct pf_altq * a)224 cbq_add_altq(struct ifnet *ifp, struct pf_altq *a)
225 {
226 cbq_state_t *cbqp;
227
228 if (ifp == NULL)
229 return (EINVAL);
230 if (!ALTQ_IS_READY(&ifp->if_snd))
231 return (ENODEV);
232
233 /* allocate and initialize cbq_state_t */
234 cbqp = malloc(sizeof(cbq_state_t), M_DEVBUF, M_NOWAIT | M_ZERO);
235 if (cbqp == NULL)
236 return (ENOMEM);
237 CALLOUT_INIT(&cbqp->cbq_callout);
238 cbqp->cbq_qlen = 0;
239 cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */
240
241 /* keep the state in pf_altq */
242 a->altq_disc = cbqp;
243
244 return (0);
245 }
246
247 int
cbq_remove_altq(struct pf_altq * a)248 cbq_remove_altq(struct pf_altq *a)
249 {
250 cbq_state_t *cbqp;
251
252 if ((cbqp = a->altq_disc) == NULL)
253 return (EINVAL);
254 a->altq_disc = NULL;
255
256 cbq_clear_interface(cbqp);
257
258 if (cbqp->ifnp.default_)
259 cbq_class_destroy(cbqp, cbqp->ifnp.default_);
260 if (cbqp->ifnp.root_)
261 cbq_class_destroy(cbqp, cbqp->ifnp.root_);
262
263 /* deallocate cbq_state_t */
264 free(cbqp, M_DEVBUF);
265
266 return (0);
267 }
268
269 int
cbq_add_queue(struct pf_altq * a)270 cbq_add_queue(struct pf_altq *a)
271 {
272 struct rm_class *borrow, *parent;
273 cbq_state_t *cbqp;
274 struct rm_class *cl;
275 struct cbq_opts *opts;
276 int i;
277
278 if ((cbqp = a->altq_disc) == NULL)
279 return (EINVAL);
280 if (a->qid == 0)
281 return (EINVAL);
282
283 /*
284 * find a free slot in the class table. if the slot matching
285 * the lower bits of qid is free, use this slot. otherwise,
286 * use the first free slot.
287 */
288 i = a->qid % CBQ_MAX_CLASSES;
289 if (cbqp->cbq_class_tbl[i] != NULL) {
290 for (i = 0; i < CBQ_MAX_CLASSES; i++)
291 if (cbqp->cbq_class_tbl[i] == NULL)
292 break;
293 if (i == CBQ_MAX_CLASSES)
294 return (EINVAL);
295 }
296
297 opts = &a->pq_u.cbq_opts;
298 /* check parameters */
299 if (a->priority >= CBQ_MAXPRI)
300 return (EINVAL);
301
302 /* Get pointers to parent and borrow classes. */
303 parent = clh_to_clp(cbqp, a->parent_qid);
304 if (opts->flags & CBQCLF_BORROW)
305 borrow = parent;
306 else
307 borrow = NULL;
308
309 /*
310 * A class must borrow from it's parent or it can not
311 * borrow at all. Hence, borrow can be null.
312 */
313 if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
314 printf("cbq_add_queue: no parent class!\n");
315 return (EINVAL);
316 }
317
318 if ((borrow != parent) && (borrow != NULL)) {
319 printf("cbq_add_class: borrow class != parent\n");
320 return (EINVAL);
321 }
322
323 /*
324 * check parameters
325 */
326 switch (opts->flags & CBQCLF_CLASSMASK) {
327 case CBQCLF_ROOTCLASS:
328 if (parent != NULL)
329 return (EINVAL);
330 if (cbqp->ifnp.root_)
331 return (EINVAL);
332 break;
333 case CBQCLF_DEFCLASS:
334 if (cbqp->ifnp.default_)
335 return (EINVAL);
336 break;
337 case 0:
338 if (a->qid == 0)
339 return (EINVAL);
340 break;
341 default:
342 /* more than two flags bits set */
343 return (EINVAL);
344 }
345
346 /*
347 * create a class. if this is a root class, initialize the
348 * interface.
349 */
350 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
351 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte,
352 cbqrestart, a->qlimit, RM_MAXQUEUED,
353 opts->maxidle, opts->minidle, opts->offtime,
354 opts->flags);
355 cl = cbqp->ifnp.root_;
356 } else {
357 cl = rmc_newclass(a->priority,
358 &cbqp->ifnp, opts->ns_per_byte,
359 rmc_delay_action, a->qlimit, parent, borrow,
360 opts->maxidle, opts->minidle, opts->offtime,
361 opts->pktsize, opts->flags);
362 }
363 if (cl == NULL)
364 return (ENOMEM);
365
366 /* return handle to user space. */
367 cl->stats_.handle = a->qid;
368 cl->stats_.depth = cl->depth_;
369
370 /* save the allocated class */
371 cbqp->cbq_class_tbl[i] = cl;
372
373 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
374 cbqp->ifnp.default_ = cl;
375
376 return (0);
377 }
378
379 int
cbq_remove_queue(struct pf_altq * a)380 cbq_remove_queue(struct pf_altq *a)
381 {
382 struct rm_class *cl;
383 cbq_state_t *cbqp;
384 int i;
385
386 if ((cbqp = a->altq_disc) == NULL)
387 return (EINVAL);
388
389 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
390 return (EINVAL);
391
392 /* if we are a parent class, then return an error. */
393 if (is_a_parent_class(cl))
394 return (EINVAL);
395
396 /* delete the class */
397 rmc_delete_class(&cbqp->ifnp, cl);
398
399 /*
400 * free the class handle
401 */
402 for (i = 0; i < CBQ_MAX_CLASSES; i++)
403 if (cbqp->cbq_class_tbl[i] == cl) {
404 cbqp->cbq_class_tbl[i] = NULL;
405 if (cl == cbqp->ifnp.root_)
406 cbqp->ifnp.root_ = NULL;
407 if (cl == cbqp->ifnp.default_)
408 cbqp->ifnp.default_ = NULL;
409 break;
410 }
411
412 return (0);
413 }
414
415 int
cbq_getqstats(struct pf_altq * a,void * ubuf,int * nbytes,int version)416 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes, int version)
417 {
418 cbq_state_t *cbqp;
419 struct rm_class *cl;
420 class_stats_t stats;
421 int error = 0;
422
423 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
424 return (EBADF);
425
426 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
427 return (EINVAL);
428
429 if (*nbytes < sizeof(stats))
430 return (EINVAL);
431
432 get_class_stats(&stats, cl);
433
434 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
435 return (error);
436 *nbytes = sizeof(stats);
437 return (0);
438 }
439
440 /*
441 * int
442 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr)
443 * - Queue data packets.
444 *
445 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
446 * layer (e.g. ether_output). cbq_enqueue queues the given packet
447 * to the cbq, then invokes the driver's start routine.
448 *
449 * Assumptions: called in splimp
450 * Returns: 0 if the queueing is successful.
451 * ENOBUFS if a packet dropping occurred as a result of
452 * the queueing.
453 */
454
455 static int
cbq_enqueue(struct ifaltq * ifq,struct mbuf * m,struct altq_pktattr * pktattr)456 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
457 {
458 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
459 struct rm_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(cbqp, t->qid);
476 if (cl == NULL) {
477 cl = cbqp->ifnp.default_;
478 if (cl == NULL) {
479 m_freem(m);
480 return (ENOBUFS);
481 }
482 }
483 cl->pktattr_ = NULL;
484 len = m_pktlen(m);
485 if (rmc_queue_packet(cl, m) != 0) {
486 /* drop occurred. some mbuf was freed in rmc_queue_packet. */
487 PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
488 return (ENOBUFS);
489 }
490
491 /* successfully queued. */
492 ++cbqp->cbq_qlen;
493 IFQ_INC_LEN(ifq);
494 return (0);
495 }
496
497 static struct mbuf *
cbq_dequeue(struct ifaltq * ifq,int op)498 cbq_dequeue(struct ifaltq *ifq, int op)
499 {
500 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
501 struct mbuf *m;
502
503 IFQ_LOCK_ASSERT(ifq);
504
505 m = rmc_dequeue_next(&cbqp->ifnp, op);
506
507 if (m && op == ALTDQ_REMOVE) {
508 --cbqp->cbq_qlen; /* decrement # of packets in cbq */
509 IFQ_DEC_LEN(ifq);
510
511 /* Update the class. */
512 rmc_update_class_util(&cbqp->ifnp);
513 }
514 return (m);
515 }
516
517 /*
518 * void
519 * cbqrestart(queue_t *) - Restart sending of data.
520 * called from rmc_restart in splimp via timeout after waking up
521 * a suspended class.
522 * Returns: NONE
523 */
524
525 static void
cbqrestart(struct ifaltq * ifq)526 cbqrestart(struct ifaltq *ifq)
527 {
528 cbq_state_t *cbqp;
529 struct ifnet *ifp;
530
531 IFQ_LOCK_ASSERT(ifq);
532
533 if (!ALTQ_IS_ENABLED(ifq))
534 /* cbq must have been detached */
535 return;
536
537 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
538 /* should not happen */
539 return;
540
541 ifp = ifq->altq_ifp;
542 if (ifp->if_start &&
543 cbqp->cbq_qlen > 0 && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
544 IFQ_UNLOCK(ifq);
545 (*ifp->if_start)(ifp);
546 IFQ_LOCK(ifq);
547 }
548 }
549
cbq_purge(cbq_state_t * cbqp)550 static void cbq_purge(cbq_state_t *cbqp)
551 {
552 struct rm_class *cl;
553 int i;
554
555 for (i = 0; i < CBQ_MAX_CLASSES; i++)
556 if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
557 rmc_dropall(cl);
558 if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
559 cbqp->ifnp.ifq_->ifq_len = 0;
560 }
561
562 #endif /* ALTQ_CBQ */
563