/*
* Copyright (c) Sun Microsystems, Inc. 1993-1998 All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the SMCC Technology
* Development Group at Sun Microsystems, Inc.
*
* 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* SUN MICROSYSTEMS DOES NOT CLAIM MERCHANTABILITY OF THIS SOFTWARE OR THE
* SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE. The software is
* provided "as is" without express or implied warranty of any kind.
*
* These notices must be retained in any copies of any part of this software.
*/
#ifdef ALTQ3_COMPAT
/*
* Local Data structures.
*/
static cbq_state_t *cbq_list = NULL;
#endif
/*
* Forward Declarations.
*/
static int cbq_class_destroy(cbq_state_t *, struct rm_class *);
static struct rm_class *clh_to_clp(cbq_state_t *, u_int32_t);
static int cbq_clear_interface(cbq_state_t *);
static int cbq_request(struct ifaltq *, int, void *);
static int cbq_enqueue(struct ifaltq *, struct mbuf *);
static struct mbuf *cbq_dequeue(struct ifaltq *, int);
static void cbqrestart(struct ifaltq *);
static void get_class_stats(class_stats_t *, struct rm_class *);
static void cbq_purge(cbq_state_t *);
#ifdef ALTQ3_COMPAT
static int cbq_add_class(struct cbq_add_class *);
static int cbq_delete_class(struct cbq_delete_class *);
static int cbq_modify_class(struct cbq_modify_class *);
static int cbq_class_create(cbq_state_t *, struct cbq_add_class *,
struct rm_class *, struct rm_class *);
static int cbq_clear_hierarchy(struct cbq_interface *);
static int cbq_set_enable(struct cbq_interface *, int);
static int cbq_ifattach(struct cbq_interface *);
static int cbq_ifdetach(struct cbq_interface *);
static int cbq_getstats(struct cbq_getstats *);
static int cbq_add_filter(struct cbq_add_filter *);
static int cbq_delete_filter(struct cbq_delete_filter *);
#endif /* ALTQ3_COMPAT */
/*
* int
* cbq_class_destroy(cbq_mod_state_t *, struct rm_class *) - This
* function destroys a given traffic class. Before destroying
* the class, all traffic for that class is released.
*/
static int
cbq_class_destroy(cbq_state_t *cbqp, struct rm_class *cl)
{
int i;
/* delete the class */
rmc_delete_class(&cbqp->ifnp, cl);
/*
* free the class handle
*/
for (i = 0; i < CBQ_MAX_CLASSES; i++)
if (cbqp->cbq_class_tbl[i] == cl)
cbqp->cbq_class_tbl[i] = NULL;
if (cl == cbqp->ifnp.root_)
cbqp->ifnp.root_ = NULL;
if (cl == cbqp->ifnp.default_)
cbqp->ifnp.default_ = NULL;
#ifdef ALTQ3_COMPAT
if (cl == cbqp->ifnp.ctl_)
cbqp->ifnp.ctl_ = NULL;
#endif
return 0;
}
/* convert class handle to class pointer */
static struct rm_class *
clh_to_clp(cbq_state_t *cbqp, u_int32_t chandle)
{
int i;
struct rm_class *cl;
if (chandle == 0)
return NULL;
/*
* first, try optimistically the slot matching the lower bits of
* the handle. if it fails, do the linear table search.
*/
i = chandle % CBQ_MAX_CLASSES;
if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
cl->stats_.handle == chandle)
return (cl);
for (i = 0; i < CBQ_MAX_CLASSES; i++)
if ((cl = cbqp->cbq_class_tbl[i]) != NULL &&
cl->stats_.handle == chandle)
return cl;
return NULL;
}
static int
cbq_clear_interface(cbq_state_t *cbqp)
{
int again, i;
struct rm_class *cl;
#ifdef ALTQ3_CLFIER_COMPAT
/* free the filters for this interface */
acc_discard_filters(&cbqp->cbq_classifier, NULL, 1);
#endif
/* clear out the classes now */
do {
again = 0;
for (i = 0; i < CBQ_MAX_CLASSES; i++) {
if ((cl = cbqp->cbq_class_tbl[i]) != NULL) {
if (is_a_parent_class(cl))
again++;
else {
cbq_class_destroy(cbqp, cl);
cbqp->cbq_class_tbl[i] = NULL;
if (cl == cbqp->ifnp.root_)
cbqp->ifnp.root_ = NULL;
if (cl == cbqp->ifnp.default_)
cbqp->ifnp.default_ = NULL;
#ifdef ALTQ3_COMPAT
if (cl == cbqp->ifnp.ctl_)
cbqp->ifnp.ctl_ = NULL;
#endif
}
}
}
} while (again);
return 0;
}
static int
cbq_request(struct ifaltq *ifq, int req, void *arg)
{
cbq_state_t *cbqp = (cbq_state_t *)ifq->altq_disc;
#define NSEC_TO_PSEC(s) ((uint64_t)(s) * 1000)
int
cbq_add_queue(struct pf_altq *a)
{
struct rm_class *borrow, *parent;
cbq_state_t *cbqp;
struct rm_class *cl;
struct cbq_opts *opts;
int i, error;
if ((cbqp = a->altq_disc) == NULL)
return EINVAL;
if (a->qid == 0)
return EINVAL;
/*
* find a free slot in the class table. if the slot matching
* the lower bits of qid is free, use this slot. otherwise,
* use the first free slot.
*/
i = a->qid % CBQ_MAX_CLASSES;
if (cbqp->cbq_class_tbl[i] != NULL) {
for (i = 0; i < CBQ_MAX_CLASSES; i++)
if (cbqp->cbq_class_tbl[i] == NULL)
break;
if (i == CBQ_MAX_CLASSES)
return (EINVAL);
}
/* Get pointers to parent and borrow classes. */
parent = clh_to_clp(cbqp, a->parent_qid);
if (opts->flags & CBQCLF_BORROW)
borrow = parent;
else
borrow = NULL;
/*
* A class must borrow from its parent or it can not
* borrow at all. Hence, borrow can be null.
*/
if (parent == NULL && (opts->flags & CBQCLF_ROOTCLASS) == 0) {
printf("cbq_add_queue: no parent class!\n");
return EINVAL;
}
if ((borrow != parent) && (borrow != NULL)) {
printf("cbq_add_class: borrow class != parent\n");
return EINVAL;
}
/*
* check parameters
*/
if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
if (parent != NULL)
return EINVAL;
if (cbqp->ifnp.root_)
return EINVAL;
}
if ((opts->flags & CBQCLF_DEFCLASS) != 0) {
if (cbqp->ifnp.default_)
return EINVAL;
}
if ((opts->flags & CBQCLF_CLASSMASK) == 0) {
if (a->qid == 0)
return EINVAL;
}
/*
* create a class. if this is a root class, initialize the
* interface.
*/
if ((opts->flags & CBQCLF_ROOTCLASS) != 0) {
error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
NSEC_TO_PSEC(opts->ns_per_byte), cbqrestart, a->qlimit, RM_MAXQUEUED,
opts->maxidle, opts->minidle, opts->offtime,
opts->flags);
if (error != 0)
return error;
cl = cbqp->ifnp.root_;
} else {
cl = rmc_newclass(a->priority,
&cbqp->ifnp, NSEC_TO_PSEC(opts->ns_per_byte),
rmc_delay_action, a->qlimit, parent, borrow,
opts->maxidle, opts->minidle, opts->offtime,
opts->pktsize, opts->flags);
}
if (cl == NULL)
return ENOMEM;
/* return handle to user space. */
cl->stats_.handle = a->qid;
cl->stats_.depth = cl->depth_;
/* save the allocated class */
cbqp->cbq_class_tbl[i] = cl;
if ((opts->flags & CBQCLF_DEFCLASS) != 0)
cbqp->ifnp.default_ = cl;
return 0;
}
int
cbq_remove_queue(struct pf_altq *a)
{
struct rm_class *cl;
cbq_state_t *cbqp;
int i;
if ((cbqp = a->altq_disc) == NULL)
return EINVAL;
if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
return EINVAL;
/* if we are a parent class, then return an error. */
if (is_a_parent_class(cl))
return EINVAL;
/* delete the class */
rmc_delete_class(&cbqp->ifnp, cl);
/*
* free the class handle
*/
for (i = 0; i < CBQ_MAX_CLASSES; i++)
if (cbqp->cbq_class_tbl[i] == cl) {
cbqp->cbq_class_tbl[i] = NULL;
if (cl == cbqp->ifnp.root_)
cbqp->ifnp.root_ = NULL;
if (cl == cbqp->ifnp.default_)
cbqp->ifnp.default_ = NULL;
break;
}
return 0;
}
int
cbq_getqstats(struct pf_altq *a, void *ubuf, int *nbytes)
{
cbq_state_t *cbqp;
struct rm_class *cl;
class_stats_t stats;
int error = 0;
if ((cbqp = altq_lookup(a->ifname, ALTQT_CBQ)) == NULL)
return EBADF;
if ((cl = clh_to_clp(cbqp, a->qid)) == NULL)
return EINVAL;
/*
* int
* cbq_enqueue(struct ifaltq *ifq, struct mbuf *m)
* - Queue data packets.
*
* cbq_enqueue is set to ifp->if_altqenqueue and called by an upper
* layer (e.g. ether_output). cbq_enqueue queues the given packet
* to the cbq, then invokes the driver's start routine.
*
* Assumptions: called in splnet
* Returns: 0 if the queueing is successful.
* ENOBUFS if a packet dropping occurred as a result of
* the queueing.
*/
/* grab class set by classifier */
if ((m->m_flags & M_PKTHDR) == 0) {
/* should not happen */
printf("altq: packet for %s does not have pkthdr\n",
ifq->altq_ifp->if_xname);
m_freem(m);
return ENOBUFS;
}
cl = NULL;
if ((t = m_tag_find(m, PACKET_TAG_ALTQ_QID)) != NULL)
cl = clh_to_clp(cbqp, ((struct altq_tag *)(t+1))->qid);
#ifdef ALTQ3_COMPAT
else if (ifq->altq_flags & ALTQF_CLASSIFY)
cl = m->m_pkthdr.pattr_class;
#endif
if (cl == NULL) {
cl = cbqp->ifnp.default_;
if (cl == NULL) {
m_freem(m);
return ENOBUFS;
}
}
#ifdef ALTQ3_COMPAT
if (m->m_pkthdr.pattr_af != AF_UNSPEC) {
pktattr.pattr_class = m->m_pkthdr.pattr_class;
pktattr.pattr_af = m->m_pkthdr.pattr_af;
pktattr.pattr_hdr = m->m_pkthdr.pattr_hdr;
cl->pktattr_ = &pktattr; /* save proto hdr used by ECN */
} else
#endif
cl->pktattr_ = NULL;
len = m_pktlen(m);
if (rmc_queue_packet(cl, m) != 0) {
/* drop occurred. some mbuf was freed in rmc_queue_packet. */
PKTCNTR_ADD(&cl->stats_.drop_cnt, len);
return ENOBUFS;
}
if (m && op == ALTDQ_REMOVE) {
--cbqp->cbq_qlen; /* decrement # of packets in cbq */
IFQ_DEC_LEN(ifq);
/* Update the class. */
rmc_update_class_util(&cbqp->ifnp);
}
return m;
}
/*
* void
* cbqrestart(queue_t *) - Restart sending of data.
* called from rmc_restart in splnet via timeout after waking up
* a suspended class.
* Returns: NONE
*/
static void
cbq_purge(cbq_state_t *cbqp)
{
struct rm_class *cl;
int i;
for (i = 0; i < CBQ_MAX_CLASSES; i++)
if ((cl = cbqp->cbq_class_tbl[i]) != NULL)
rmc_dropall(cl);
if (ALTQ_IS_ENABLED(cbqp->ifnp.ifq_))
cbqp->ifnp.ifq_->ifq_len = 0;
}
#ifdef ALTQ3_COMPAT
/* Get pointers to parent and borrow classes. */
parent = clh_to_clp(cbqp, acp->cbq_class.parent_class_handle);
borrow = clh_to_clp(cbqp, acp->cbq_class.borrow_class_handle);
/*
* A class must borrow from its parent or it can not
* borrow at all. Hence, borrow can be null.
*/
if (parent == NULL && (acp->cbq_class.flags & CBQCLF_ROOTCLASS) == 0) {
printf("cbq_add_class: no parent class!\n");
return EINVAL;
}
if ((borrow != parent) && (borrow != NULL)) {
printf("cbq_add_class: borrow class != parent\n");
return EINVAL;
}
/*
* struct rm_class *
* cbq_class_create(cbq_mod_state_t *cbqp, struct cbq_add_class *acp,
* struct rm_class *parent, struct rm_class *borrow)
*
* This function create a new traffic class in the CBQ class hierarchy of
* given parameters. The class that created is either the root, default,
* or a new dynamic class. If CBQ is not initialized, the root class
* will be created.
*/
static int
cbq_class_create(cbq_state_t *cbqp, struct cbq_add_class *acp,
struct rm_class *parent, struct rm_class *borrow)
{
struct rm_class *cl;
cbq_class_spec_t *spec = &acp->cbq_class;
u_int32_t chandle;
int i, error;
/*
* allocate class handle
*/
for (i = 1; i < CBQ_MAX_CLASSES; i++)
if (cbqp->cbq_class_tbl[i] == NULL)
break;
if (i == CBQ_MAX_CLASSES)
return EINVAL;
chandle = i; /* use the slot number as class handle */
/*
* create a class. if this is a root class, initialize the
* interface.
*/
if ((spec->flags & CBQCLF_ROOTCLASS) != 0) {
error = rmc_init(cbqp->ifnp.ifq_, &cbqp->ifnp,
spec->pico_sec_per_byte, cbqrestart, spec->maxq,
RM_MAXQUEUED, spec->maxidle, spec->minidle, spec->offtime,
spec->flags);
if (error)
return error;
cl = cbqp->ifnp.root_;
} else {
cl = rmc_newclass(spec->priority,
&cbqp->ifnp, spec->pico_sec_per_byte,
rmc_delay_action, spec->maxq, parent, borrow,
spec->maxidle, spec->minidle, spec->offtime,
spec->pktsize, spec->flags);
}
if (cl == NULL)
return ENOMEM;
/* return handle to user space. */
acp->cbq_class_handle = chandle;
/*
* cbq_clear_hierarchy deletes all classes and their filters on the
* given interface.
*/
static int
cbq_clear_hierarchy(struct cbq_interface *ifacep)
{
char *ifacename;
cbq_state_t *cbqp;
/*
* static int
* cbq_set_enable(struct cbq_enable *ep) - this function processed the
* ioctl request to enable class based queueing. It searches the list
* of interfaces for the specified interface and then enables CBQ on
* that interface.
*
* Returns: 0, for no error.
* EBADF, for specified interface not found.
*/
static int
cbq_set_enable(struct cbq_interface *ep, int enable)
{
int error = 0;
cbq_state_t *cbqp;
char *ifacename;
switch (enable) {
case ENABLE:
if (cbqp->ifnp.root_ == NULL || cbqp->ifnp.default_ == NULL) {
if (cbqp->ifnp.root_ == NULL)
printf("No Root Class for %s\n", ifacename);
if (cbqp->ifnp.default_ == NULL)
printf("No Default Class for %s\n", ifacename);
error = EINVAL;
} else if ((error = altq_enable(cbqp->ifnp.ifq_)) == 0) {
cbqp->cbq_qlen = 0;
}
break;
case DISABLE:
error = altq_disable(cbqp->ifnp.ifq_);
break;
}
return error;
}
static int
cbq_getstats(struct cbq_getstats *gsp)
{
char *ifacename;
int i, n, nclasses;
cbq_state_t *cbqp;
struct rm_class *cl;
class_stats_t stats, *usp;
int error = 0;
if ((cbqp = altq_lookup(ifacename, ALTQT_CBQ)) == NULL)
return EBADF;
if (nclasses <= 0)
return EINVAL;
for (n = 0, i = 0; n < nclasses && i < CBQ_MAX_CLASSES; n++, i++) {
while ((cl = cbqp->cbq_class_tbl[i]) == NULL)
if (++i >= CBQ_MAX_CLASSES)
goto out;
int
cbqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
struct lwp *l)
{
int error = 0;
/* check cmd for superuser only */
switch (cmd) {
case CBQ_GETSTATS:
/* currently only command that an ordinary user can call */
break;
default:
error = kauth_authorize_network(l->l_cred,
KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_CBQ, NULL, NULL,
NULL);
if (error)
return error;
break;
}
switch (cmd) {
case CBQ_ENABLE:
error = cbq_set_enable((struct cbq_interface *)addr, ENABLE);
break;
case CBQ_DISABLE:
error = cbq_set_enable((struct cbq_interface *)addr, DISABLE);
break;
case CBQ_ADD_FILTER:
error = cbq_add_filter((struct cbq_add_filter *)addr);
break;
case CBQ_DEL_FILTER:
error = cbq_delete_filter((struct cbq_delete_filter *)addr);
break;
case CBQ_ADD_CLASS:
error = cbq_add_class((struct cbq_add_class *)addr);
break;
case CBQ_DEL_CLASS:
error = cbq_delete_class((struct cbq_delete_class *)addr);
break;
case CBQ_MODIFY_CLASS:
error = cbq_modify_class((struct cbq_modify_class *)addr);
break;
case CBQ_CLEAR_HIERARCHY:
error = cbq_clear_hierarchy((struct cbq_interface *)addr);
break;
case CBQ_IF_ATTACH:
error = cbq_ifattach((struct cbq_interface *)addr);
break;
case CBQ_IF_DETACH:
error = cbq_ifdetach((struct cbq_interface *)addr);
break;
case CBQ_GETSTATS:
error = cbq_getstats((struct cbq_getstats *)addr);
break;
default:
error = EINVAL;
break;
}
return error;
}
#if 0
/* for debug */
static void cbq_class_dump(int);