1 /* $KAME: altq_cbq.c,v 1.20 2004/04/17 10:54:48 kjc Exp $ */
2 /* $DragonFly: src/sys/net/altq/altq_cbq.c,v 1.1 2005/02/11 22:25:57 joerg Exp $ */
5 * Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the SMCC Technology
21 * Development Group at Sun Microsystems, Inc.
23 * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
24 * promote products derived from this software without specific prior
27 * SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
28 * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is
29 * provided "as is" without express or implied warranty of any kind.
31 * These notices must be retained in any copies of any part of this software.
36 #include "opt_inet6.h"
38 #ifdef ALTQ_CBQ /* cbq is enabled by ALTQ_CBQ option in opt_altq.h */
40 #include <sys/param.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/systm.h>
46 #include <sys/callout.h>
47 #include <sys/errno.h>
51 #include <net/ifq_var.h>
52 #include <netinet/in.h>
54 #include <net/pf/pfvar.h>
55 #include <net/altq/altq.h>
56 #include <net/altq/altq_cbq.h>
59 * Forward Declarations.
61 static int cbq_class_destroy(cbq_state_t *, struct rm_class *);
62 static struct rm_class *clh_to_clp(cbq_state_t *, uint32_t);
63 static int cbq_clear_interface(cbq_state_t *);
64 static int cbq_request(struct ifaltq *, int, void *);
65 static int cbq_enqueue(struct ifaltq *, struct mbuf *,
66 struct altq_pktattr *);
67 static struct mbuf *cbq_dequeue(struct ifaltq *, int);
68 static void cbqrestart(struct ifaltq *);
69 static void get_class_stats(class_stats_t *, struct rm_class *);
70 static void cbq_purge(cbq_state_t *);
74 * cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
75 * function destroys a given traffic class. Before destroying
76 * the class, all traffic for that class is released.
79 cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
83 /* delete the class */
84 rmc_delete_class(&cbqp->ifnp, cl);
87 * free the class handle
89 for (i = 0; i < CBQ_MAX_CLASSES; i++)
90 if (cbqp->cbq_class_tbl[i] == cl)
91 cbqp->cbq_class_tbl[i] = NULL;
93 if (cl == cbqp->ifnp.root_)
94 cbqp->ifnp.root_ = NULL;
95 if (cl == cbqp->ifnp.default_)
96 cbqp->ifnp.default_ = NULL;
100 /* convert class handle to class pointer */
101 static struct rm_class *
102 clh_to_clp(cbq_state_t *cbqp, uint32_t chandle)
110 * first, try optimistically the slot matching the lower bits of
111 * the handle. if it fails, do the linear table search.
113 i = chandle % CBQ_MAX_CLASSES;
114 if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
115 cl->stats_.handle == chandle)
117 for (i = 0; i < CBQ_MAX_CLASSES; i++)
118 if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
119 cl->stats_.handle == chandle)
125 cbq_clear_interface(cbq_state_t *cbqp)
130 /* clear out the classes now */
133 for (i = 0; i < CBQ_MAX_CLASSES; i++) {
134 if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
135 if (is_a_parent_class(cl))
138 cbq_class_destroy(cbqp, cl);
139 cbqp->cbq_class_tbl[i] = NULL;
140 if (cl == cbqp->ifnp.root_)
141 cbqp->ifnp.root_ = NULL;
142 if (cl == cbqp->ifnp.default_)
143 cbqp->ifnp.default_ = NULL;
153 cbq_request(struct ifaltq *ifq, int req, void *arg)
155 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
165 /* copy the stats info in rm_class to class_states_t */
167 get_class_stats(class_stats_t *statsp, struct rm_class *cl)
169 statsp->xmit_cnt = cl->stats_.xmit_cnt;
170 statsp->drop_cnt = cl->stats_.drop_cnt;
171 statsp->over = cl->stats_.over;
172 statsp->borrows = cl->stats_.borrows;
173 statsp->overactions = cl->stats_.overactions;
174 statsp->delays = cl->stats_.delays;
176 statsp->depth = cl->depth_;
177 statsp->priority = cl->pri_;
178 statsp->maxidle = cl->maxidle_;
179 statsp->minidle = cl->minidle_;
180 statsp->offtime = cl->offtime_;
181 statsp->qmax = qlimit(cl->q_);
182 statsp->ns_per_byte = cl->ns_per_byte_;
183 statsp->wrr_allot = cl->w_allotment_;
184 statsp->qcnt = qlen(cl->q_);
185 statsp->avgidle = cl->avgidle_;
187 statsp->qtype = qtype(cl->q_);
189 if (q_is_red(cl->q_))
190 red_getstats(cl->red_, &statsp->red[0]);
193 if (q_is_rio(cl->q_))
194 rio_getstats((rio_t *)cl->red_, &statsp->red[0]);
199 cbq_pfattach(struct pf_altq *a)
204 if ((ifp = ifunit(a->ifname)) == NULL || a->altq_disc == NULL)
207 error = altq_attach(&ifp->if_snd, ALTQT_CBQ, a->altq_disc,
208 cbq_enqueue, cbq_dequeue, cbq_request, NULL, NULL);
214 cbq_add_altq(struct pf_altq *a)
219 if ((ifp = ifunit(a->ifname)) == NULL)
221 if (!ifq_is_ready(&ifp->if_snd))
224 /* allocate and initialize cbq_state_t */
225 cbqp = malloc(sizeof(*cbqp), M_ALTQ, M_WAITOK | M_ZERO);
226 callout_init(&cbqp->cbq_callout);
228 cbqp->ifnp.ifq_ = &ifp->if_snd; /* keep the ifq */
230 /* keep the state in pf_altq */
237 cbq_remove_altq(struct pf_altq *a)
241 if ((cbqp = a->altq_disc) == NULL)
245 cbq_clear_interface(cbqp);
247 if (cbqp->ifnp.default_)
248 cbq_class_destroy(cbqp, cbqp->ifnp.default_);
249 if (cbqp->ifnp.root_)
250 cbq_class_destroy(cbqp, cbqp->ifnp.root_);
252 /* deallocate cbq_state_t */
259 cbq_add_queue(struct pf_altq *a)
261 struct rm_class *borrow, *parent;
264 struct cbq_opts *opts;
267 if ((cbqp = a->altq_disc) == NULL)
273 * find a free slot in the class table. if the slot matching
274 * the lower bits of qid is free, use this slot. otherwise,
275 * use the first free slot.
277 i = a->qid % CBQ_MAX_CLASSES;
278 if (cbqp->cbq_class_tbl[i] != NULL) {
279 for (i = 0; i < CBQ_MAX_CLASSES; i++)
280 if (cbqp->cbq_class_tbl[i] == NULL)
282 if (i == CBQ_MAX_CLASSES)
286 opts = &a->pq_u.cbq_opts;
287 /* check parameters */
288 if (a->priority >= CBQ_MAXPRI)
291 /* Get pointers to parent and borrow classes. */
292 parent = clh_to_clp(cbqp, a->parent_qid);
293 if (opts->flags & CBQCLF_BORROW)
299 * A class must borrow from it's parent or it can not
300 * borrow at all. Hence, borrow can be null.
302 if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
303 printf("cbq_add_queue: no parent class!\n");
307 if ((borrow != parent) && (borrow != NULL)) {
308 printf("cbq_add_class: borrow class != parent\n");
315 switch (opts->flags & CBQCLF_CLASSMASK) {
316 case CBQCLF_ROOTCLASS:
319 if (cbqp->ifnp.root_)
322 case CBQCLF_DEFCLASS:
323 if (cbqp->ifnp.default_)
331 /* more than two flags bits set */
336 * create a class. if this is a root class, initialize the
339 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_ROOTCLASS) {
340 rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp, opts->ns_per_byte,
341 cbqrestart, a->qlimit, RM_MAXQUEUED,
342 opts->maxidle, opts->minidle, opts->offtime,
344 cl = cbqp->ifnp.root_;
346 cl = rmc_newclass(a->priority,
347 &cbqp->ifnp, opts->ns_per_byte,
348 rmc_delay_action, a->qlimit, parent, borrow,
349 opts->maxidle, opts->minidle, opts->offtime,
350 opts->pktsize, opts->flags);
355 /* return handle to user space. */
356 cl->stats_.handle = a->qid;
357 cl->stats_.depth = cl->depth_;
359 /* save the allocated class */
360 cbqp->cbq_class_tbl[i] = cl;
362 if ((opts->flags & CBQCLF_CLASSMASK) == CBQCLF_DEFCLASS)
363 cbqp->ifnp.default_ = cl;
369 cbq_remove_queue(struct pf_altq *a)
375 if ((cbqp = a->altq_disc) == NULL)
378 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
381 /* if we are a parent class, then return an error. */
382 if (is_a_parent_class(cl))
385 /* delete the class */
386 rmc_delete_class(&cbqp->ifnp, cl);
389 * free the class handle
391 for (i = 0; i < CBQ_MAX_CLASSES; i++)
392 if (cbqp->cbq_class_tbl[i] == cl) {
393 cbqp->cbq_class_tbl[i] = NULL;
394 if (cl == cbqp->ifnp.root_)
395 cbqp->ifnp.root_ = NULL;
396 if (cl == cbqp->ifnp.default_)
397 cbqp->ifnp.default_ = NULL;
405 cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
412 if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
415 if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
418 if (*nbytes < sizeof(stats))
421 get_class_stats(&stats, cl);
423 if ((error = copyout((caddr_t)&stats, ubuf, sizeof(stats))) != 0)
425 *nbytes = sizeof(stats);
431 * cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pattr)
432 * - Queue data packets.
434 * cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
435 * layer (e.g. ether_output). cbq_enqueue queues the given packet
436 * to the cbq, then invokes the driver's start routine.
438 * Assumptions: called in splimp
439 * Returns: 0 if the queueing is successful.
440 * ENOBUFS if a packet dropping occurred as a result of
445 cbq_enqueue(struct ifaltq *ifq, struct mbuf *m, struct altq_pktattr *pktattr)
447 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
451 /* grab class set by classifier */
452 if ((m->m_flags & M_PKTHDR) == 0) {
453 /* should not happen */
454 if_printf(ifq->altq_ifp, "altq: packet does not have pkthdr\n");
458 if (m->m_pkthdr.fw_flags & ALTQ_MBUF_TAGGED)
459 cl = clh_to_clp(cbqp, m->m_pkthdr.altq_qid);
463 cl = cbqp->ifnp.default_;
471 if (rmc_queue_packet(cl, m) != 0) {
472 /* drop occurred. some mbuf was freed in rmc_queue_packet. */
473 PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
477 /* successfully queued. */
484 cbq_dequeue(struct ifaltq *ifq, int op)
486 cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
489 m = rmc_dequeue_next(&cbqp->ifnp, op);
491 if (m && op == ALTDQ_REMOVE) {
492 --cbqp->cbq_qlen; /* decrement # of packets in cbq */
495 /* Update the class. */
496 rmc_update_class_util(&cbqp->ifnp);
503 * cbqrestart(queue_t *) - Restart sending of data.
504 * called from rmc_restart in splimp via timeout after waking up
510 cbqrestart(struct ifaltq *ifq)
515 if (!ifq_is_enabled(ifq))
516 /* cbq must have been detached */
519 if ((cbqp = (cbq_state_t *)ifq->altq_disc) == NULL)
520 /* should not happen */
525 cbqp->cbq_qlen > 0 && (ifp->if_flags & IFF_OACTIVE) == 0)
526 (*ifp->if_start)(ifp);
530 cbq_purge(cbq_state_t *cbqp)
535 for (i = 0; i < CBQ_MAX_CLASSES; i++)
536 if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
538 if (ifq_is_enabled(cbqp->ifnp.ifq_))
539 cbqp->ifnp.ifq_->ifq_len = 0;
542 #endif /* ALTQ_CBQ */