/*
* Copyright (C) 1999-2002
* Sony Computer Science Laboratories Inc. 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.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
static int cdnrcmd_if_attach(char *);
static int cdnrcmd_if_detach(char *);
static int cdnrcmd_add_element(struct cdnr_add_element *);
static int cdnrcmd_delete_element(struct cdnr_delete_element *);
static int cdnrcmd_add_filter(struct cdnr_add_filter *);
static int cdnrcmd_delete_filter(struct cdnr_delete_filter *);
static int cdnrcmd_add_tbm(struct cdnr_add_tbmeter *);
static int cdnrcmd_modify_tbm(struct cdnr_modify_tbmeter *);
static int cdnrcmd_tbm_stats(struct cdnr_tbmeter_stats *);
static int cdnrcmd_add_trtcm(struct cdnr_add_trtcm *);
static int cdnrcmd_modify_trtcm(struct cdnr_modify_trtcm *);
static int cdnrcmd_tcm_stats(struct cdnr_tcm_stats *);
static int cdnrcmd_add_tswtcm(struct cdnr_add_tswtcm *);
static int cdnrcmd_modify_tswtcm(struct cdnr_modify_tswtcm *);
static int cdnrcmd_get_stats(struct cdnr_get_stats *);
altqdev_decl(cdnr);
/*
* top level input function called from ip_input.
* should be called before converting header fields to host-byte-order.
*/
int
altq_cdnr_input(struct mbuf *m, int af)
{
struct ifnet *ifp;
struct ip *ip;
struct top_cdnr *top;
struct tc_action *tca;
struct cdnr_block *cb;
struct cdnr_pktinfo pktinfo;
ifp = m_get_rcvif_NOMPSAFE(m);
if (!ALTQ_IS_CNDTNING(&ifp->if_snd))
/* traffic conditioner is not enabled on this interface */
return 1;
top = ifp->if_snd.altq_cdnr;
ip = mtod(m, struct ip *);
#ifdef INET6
if (af == AF_INET6) {
u_int32_t flowlabel;
if (input_func != NULL) {
/*
* if this cdnr has an action function,
* make tc_action to call itself.
*/
cb->cb_action.tca_code = TCACODE_NEXT;
cb->cb_action.tca_next = cb;
cb->cb_input = input_func;
} else
cb->cb_action.tca_code = TCACODE_NONE;
/* if this isn't top, register the element to the top level cdnr */
if (top != NULL)
LIST_INSERT_HEAD(&top->tc_elements, cb, cb_next);
/* delete filters belonging to this cdnr */
acc_discard_filters(&cb->cb_top->tc_classifier, cb, 0);
/* remove from the top level cdnr */
if (cb->cb_top != cblock)
LIST_REMOVE(cb, cb_next);
free(cb, M_DEVBUF);
}
/*
* conditioner common destroy routine
*/
static int
generic_element_destroy(struct cdnr_block *cb)
{
int error = 0;
switch (cb->cb_type) {
case TCETYPE_TOP:
error = top_destroy((struct top_cdnr *)cb);
break;
case TCETYPE_ELEMENT:
error = element_destroy(cb);
break;
case TCETYPE_TBMETER:
error = tbm_destroy((struct tbmeter *)cb);
break;
case TCETYPE_TRTCM:
error = trtcm_destroy((struct trtcm *)cb);
break;
case TCETYPE_TSWTCM:
error = tswtcm_destroy((struct tswtcm *)cb);
break;
default:
error = EINVAL;
}
return error;
}
static int
tca_verify_action(struct tc_action *utca)
{
switch (utca->tca_code) {
case TCACODE_PASS:
case TCACODE_DROP:
case TCACODE_MARK:
/* these are ok */
break;
case TCACODE_HANDLE:
/* verify handle value */
if (cdnr_handle2cb(utca->tca_handle) == NULL)
return -1;
break;
case TCACODE_NONE:
case TCACODE_RETURN:
case TCACODE_NEXT:
default:
/* should not be passed from a user */
return -1;
}
return 0;
}
if ((top = cdnr_cballoc(NULL, TCETYPE_TOP, NULL)) == NULL)
return NULL;
top->tc_ifq = ifq;
/* set default action for the top level conditioner */
top->tc_block.cb_action.tca_code = TCACODE_PASS;
LIST_INSERT_HEAD(&tcb_list, top, tc_next);
ifq->altq_cdnr = top;
return top;
}
static int
top_destroy(struct top_cdnr *top)
{
struct cdnr_block *cb;
if (ALTQ_IS_CNDTNING(top->tc_ifq))
ALTQ_CLEAR_CNDTNING(top->tc_ifq);
top->tc_ifq->altq_cdnr = NULL;
/*
* destroy all the conditioner elements belonging to this interface
*/
while ((cb = LIST_FIRST(&top->tc_elements)) != NULL) {
while (cb != NULL && cb->cb_ref > 0)
cb = LIST_NEXT(cb, cb_next);
if (cb != NULL)
generic_element_destroy(cb);
}
LIST_REMOVE(top, tc_next);
cdnr_cbdestroy(top);
/* if there is no active conditioner, remove the input hook */
if (altq_input != NULL) {
LIST_FOREACH(top, &tcb_list, tc_next)
if (ALTQ_IS_CNDTNING(top->tc_ifq))
break;
if (top == NULL)
altq_input = NULL;
}
return 0;
}
/*
* simple tc elements without input function (e.g., dropper and makers).
*/
static struct cdnr_block *
element_create(struct top_cdnr *top, struct tc_action *action)
{
struct cdnr_block *cb;
if (tca_verify_action(action) < 0)
return NULL;
if ((cb = cdnr_cballoc(top, TCETYPE_ELEMENT, NULL)) == NULL)
return NULL;
tca_import_action(&cb->cb_action, action);
return cb;
}
static int
element_destroy(struct cdnr_block *cb)
{
if (cb->cb_ref > 0)
return EBUSY;
len = TB_SCALE(pktinfo->pkt_len);
if (tcm->coloraware) {
color = pktinfo->pkt_dscp;
if (color != tcm->yellow_dscp && color != tcm->red_dscp)
color = tcm->green_dscp;
} else {
/* if color-blind, precolor it as green */
color = tcm->green_dscp;
}
now = read_machclk();
if (tcm->cmtd_tb.token < len) {
interval = now - tcm->cmtd_tb.last;
if (interval >= tcm->cmtd_tb.filluptime)
tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
else {
tcm->cmtd_tb.token += interval * tcm->cmtd_tb.rate;
if (tcm->cmtd_tb.token > tcm->cmtd_tb.depth)
tcm->cmtd_tb.token = tcm->cmtd_tb.depth;
}
tcm->cmtd_tb.last = now;
}
if (tcm->peak_tb.token < len) {
interval = now - tcm->peak_tb.last;
if (interval >= tcm->peak_tb.filluptime)
tcm->peak_tb.token = tcm->peak_tb.depth;
else {
tcm->peak_tb.token += interval * tcm->peak_tb.rate;
if (tcm->peak_tb.token > tcm->peak_tb.depth)
tcm->peak_tb.token = tcm->peak_tb.depth;
}
tcm->peak_tb.last = now;
}
/*
* rate estimator
*/
len = pktinfo->pkt_len;
now = read_machclk();
interval = now - tsw->t_front;
/*
* calculate average rate:
* avg = (avg * timewin + pkt_len)/(timewin + interval)
* pkt_len needs to be multiplied by machclk_freq in order to
* get (bytes/sec).
* note: when avg_rate (bytes/sec) and timewin (machclk unit) are
* less than 32 bits, the following 64-bit operation has enough
* precision.
*/
tmp = ((u_int64_t)tsw->avg_rate * tsw->timewin
+ (u_int64_t)len * machclk_freq) / (tsw->timewin + interval);
tsw->avg_rate = avg_rate = (u_int32_t)tmp;
tsw->t_front = now;
if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
return EBADF;
cb = element_create(top, &ap->action);
if (cb == NULL)
return EINVAL;
/* return a class handle to the user */
ap->cdnr_handle = cdnr_cb2handle(cb);
return 0;
}
if ((top = tcb_lookup(ap->iface.cdnr_ifname)) == NULL)
return EBADF;
tbm = tbm_create(top, &ap->profile, &ap->in_action, &ap->out_action);
if (tbm == NULL)
return EINVAL;
/* return a class handle to the user */
ap->cdnr_handle = cdnr_cb2handle(&tbm->cdnrblk);
return 0;
}
static int
cdnrcmd_modify_tbm(struct cdnr_modify_tbmeter *ap)
{
struct tbmeter *tbm;
if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
return EINVAL;
tb_import_profile(&tbm->tb, &ap->profile);
return 0;
}
static int
cdnrcmd_tbm_stats(struct cdnr_tbmeter_stats *ap)
{
struct tbmeter *tbm;
if ((tbm = (struct tbmeter *)cdnr_handle2cb(ap->cdnr_handle)) == NULL)
return EINVAL;