#ifdef _KERNEL
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/ip_fw.h>
#include <netinet/ip_dummynet.h>
#include <sys/lock.h>
#include <sys/proc.h>
#include <sys/rwlock.h>
#include <netpfil/ipfw/ip_fw_private.h>
#include <sys/sysctl.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/queue.h>
#include <sys/hash.h>
#include <netpfil/ipfw/dn_heap.h>
#include <netpfil/ipfw/ip_dn_private.h>
#include <netpfil/ipfw/dn_aqm.h>
#include <netpfil/ipfw/dn_aqm_codel.h>
#include <netpfil/ipfw/dn_sched.h>
#include <netpfil/ipfw/dn_sched_fq_codel.h>
#include <netpfil/ipfw/dn_sched_fq_codel_helper.h>
#else
#include <dn_test.h>
#endif
#define DN_SCHED_FQ_CODEL 6
static struct dn_alg fq_codel_desc;
struct dn_sch_fq_codel_parms
fq_codel_sysctl = {{5000 * AQM_TIME_1US, 100000 * AQM_TIME_1US,
CODEL_ECN_ENABLED}, 1024, 10240, 1514};
static int
fqcodel_sysctl_interval_handler(SYSCTL_HANDLER_ARGS)
{
int error;
long value;
value = fq_codel_sysctl.ccfg.interval;
value /= AQM_TIME_1US;
error = sysctl_handle_long(oidp, &value, 0, req);
if (error != 0 || req->newptr == NULL)
return (error);
if (value < 1 || value > 100 * AQM_TIME_1S)
return (EINVAL);
fq_codel_sysctl.ccfg.interval = value * AQM_TIME_1US ;
return (0);
}
static int
fqcodel_sysctl_target_handler(SYSCTL_HANDLER_ARGS)
{
int error;
long value;
value = fq_codel_sysctl.ccfg.target;
value /= AQM_TIME_1US;
error = sysctl_handle_long(oidp, &value, 0, req);
if (error != 0 || req->newptr == NULL)
return (error);
if (value < 1 || value > 5 * AQM_TIME_1S)
return (EINVAL);
fq_codel_sysctl.ccfg.target = value * AQM_TIME_1US ;
return (0);
}
SYSBEGIN(f4)
SYSCTL_DECL(_net_inet);
SYSCTL_DECL(_net_inet_ip);
SYSCTL_DECL(_net_inet_ip_dummynet);
static SYSCTL_NODE(_net_inet_ip_dummynet, OID_AUTO, fqcodel,
CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
"FQ_CODEL");
#ifdef SYSCTL_NODE
SYSCTL_PROC(_net_inet_ip_dummynet_fqcodel, OID_AUTO, target,
CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
NULL, 0, fqcodel_sysctl_target_handler, "L",
"FQ_CoDel target in microsecond");
SYSCTL_PROC(_net_inet_ip_dummynet_fqcodel, OID_AUTO, interval,
CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
NULL, 0, fqcodel_sysctl_interval_handler, "L",
"FQ_CoDel interval in microsecond");
SYSCTL_UINT(_net_inet_ip_dummynet_fqcodel, OID_AUTO, quantum,
CTLFLAG_RW, &fq_codel_sysctl.quantum, 1514, "FQ_CoDel quantum");
SYSCTL_UINT(_net_inet_ip_dummynet_fqcodel, OID_AUTO, flows,
CTLFLAG_RW, &fq_codel_sysctl.flows_cnt, 1024,
"Number of queues for FQ_CoDel");
SYSCTL_UINT(_net_inet_ip_dummynet_fqcodel, OID_AUTO, limit,
CTLFLAG_RW, &fq_codel_sysctl.limit, 10240, "FQ_CoDel queues size limit");
#endif
static void
codel_drop_head(struct fq_codel_flow *q, struct fq_codel_si *si)
{
struct mbuf *m = q->mq.head;
if (m == NULL)
return;
q->mq.head = m->m_nextpkt;
fq_update_stats(q, si, -m->m_pkthdr.len, 1);
if (si->main_q.ni.length == 0)
si->main_q.q_time = V_dn_cfg.curr_time;
FREE_PKT(m);
}
static int
codel_enqueue(struct fq_codel_flow *q, struct mbuf *m, struct fq_codel_si *si)
{
uint64_t len;
len = m->m_pkthdr.len;
if (len > q->cst.maxpkt_size)
q->cst.maxpkt_size = len;
struct m_tag *mtag;
mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
if (mtag == NULL)
mtag = m_tag_alloc(MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, sizeof(aqm_time_t),
M_NOWAIT);
if (mtag == NULL)
goto drop;
*(aqm_time_t *)(mtag + 1) = AQM_UNOW;
m_tag_prepend(m, mtag);
if (m->m_pkthdr.rcvif != NULL)
m_rcvif_serialize(m);
mq_append(&q->mq, m);
fq_update_stats(q, si, len, 0);
return 0;
drop:
fq_update_stats(q, si, len, 1);
m_freem(m);
return 1;
}
static inline int
fq_codel_classify_flow(struct mbuf *m, uint16_t fcount, struct fq_codel_si *si)
{
struct ip *ip;
struct tcphdr *th;
struct udphdr *uh;
uint8_t tuple[41];
uint16_t hash=0;
ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
struct ip6_hdr *ip6;
int isip6;
isip6 = (ip->ip_v == 6);
if(isip6) {
ip6 = (struct ip6_hdr *)ip;
*((uint8_t *) &tuple[0]) = ip6->ip6_nxt;
*((uint32_t *) &tuple[1]) = si->perturbation;
memcpy(&tuple[5], ip6->ip6_src.s6_addr, 16);
memcpy(&tuple[21], ip6->ip6_dst.s6_addr, 16);
switch (ip6->ip6_nxt) {
case IPPROTO_TCP:
th = (struct tcphdr *)(ip6 + 1);
*((uint16_t *) &tuple[37]) = th->th_dport;
*((uint16_t *) &tuple[39]) = th->th_sport;
break;
case IPPROTO_UDP:
uh = (struct udphdr *)(ip6 + 1);
*((uint16_t *) &tuple[37]) = uh->uh_dport;
*((uint16_t *) &tuple[39]) = uh->uh_sport;
break;
default:
memset(&tuple[37], 0, 4);
}
hash = jenkins_hash(tuple, 41, HASHINIT) % fcount;
return hash;
}
*((uint8_t *) &tuple[0]) = ip->ip_p;
*((uint32_t *) &tuple[1]) = si->perturbation;
*((uint32_t *) &tuple[5]) = ip->ip_src.s_addr;
*((uint32_t *) &tuple[9]) = ip->ip_dst.s_addr;
switch (ip->ip_p) {
case IPPROTO_TCP:
th = (struct tcphdr *)(ip + 1);
*((uint16_t *) &tuple[13]) = th->th_dport;
*((uint16_t *) &tuple[15]) = th->th_sport;
break;
case IPPROTO_UDP:
uh = (struct udphdr *)(ip + 1);
*((uint16_t *) &tuple[13]) = uh->uh_dport;
*((uint16_t *) &tuple[15]) = uh->uh_sport;
break;
default:
memset(&tuple[13], 0, 4);
}
hash = jenkins_hash(tuple, 17, HASHINIT) % fcount;
return hash;
}
static int
fq_codel_enqueue(struct dn_sch_inst *_si, struct dn_queue *_q,
struct mbuf *m)
{
struct fq_codel_si *si;
struct fq_codel_schk *schk;
struct dn_sch_fq_codel_parms *param;
struct dn_queue *mainq;
int idx, drop, i, maxidx;
mainq = (struct dn_queue *)(_si + 1);
si = (struct fq_codel_si *)_si;
schk = (struct fq_codel_schk *)(si->_si.sched+1);
param = &schk->cfg;
idx = fq_codel_classify_flow(m, param->flows_cnt, si);
drop = codel_enqueue(&si->flows[idx], m, si);
if (drop)
return 1;
if (!si->flows[idx].active ) {
STAILQ_INSERT_TAIL(&si->newflows, &si->flows[idx], flowchain);
si->flows[idx].deficit = param->quantum;
si->flows[idx].cst.dropping = false;
si->flows[idx].cst.first_above_time = 0;
si->flows[idx].active = 1;
}
if (mainq->ni.length > schk->cfg.limit) { D("over limit");
for (maxidx = 0; maxidx < schk->cfg.flows_cnt; maxidx++)
if (si->flows[maxidx].active)
break;
if (maxidx < schk->cfg.flows_cnt) {
for (i = maxidx + 1; i < schk->cfg.flows_cnt; i++)
if (si->flows[i].active && si->flows[i].stats.length >
si->flows[maxidx].stats.length)
maxidx = i;
codel_drop_head(&si->flows[maxidx], si);
D("maxidx = %d",maxidx);
drop = 1;
}
}
return drop;
}
static struct mbuf *
fq_codel_dequeue(struct dn_sch_inst *_si)
{
struct fq_codel_si *si;
struct fq_codel_schk *schk;
struct dn_sch_fq_codel_parms *param;
struct fq_codel_flow *f;
struct mbuf *mbuf;
struct fq_codel_list *fq_codel_flowlist;
si = (struct fq_codel_si *)_si;
schk = (struct fq_codel_schk *)(si->_si.sched+1);
param = &schk->cfg;
do {
if (STAILQ_EMPTY(&si->newflows))
fq_codel_flowlist = &si->oldflows;
else
fq_codel_flowlist = &si->newflows;
if (STAILQ_EMPTY(fq_codel_flowlist))
return NULL;
f = STAILQ_FIRST(fq_codel_flowlist);
while (f != NULL) {
if (f->deficit < 0) {
f->deficit += param->quantum;
STAILQ_REMOVE_HEAD(fq_codel_flowlist, flowchain);
STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
} else
break;
f = STAILQ_FIRST(fq_codel_flowlist);
}
if (STAILQ_EMPTY(fq_codel_flowlist))
continue;
mbuf = fqc_codel_dequeue(f, si);
if (!mbuf) {
if (fq_codel_flowlist == &si->newflows) {
STAILQ_REMOVE_HEAD(fq_codel_flowlist, flowchain);
STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
} else {
f->active = 0;
STAILQ_REMOVE_HEAD(fq_codel_flowlist, flowchain);
}
continue;
}
f->deficit -= mbuf->m_pkthdr.len;
return mbuf;
} while (1);
return NULL;
}
static int
fq_codel_new_sched(struct dn_sch_inst *_si)
{
struct fq_codel_si *si;
struct dn_queue *q;
struct fq_codel_schk *schk;
int i;
si = (struct fq_codel_si *)_si;
schk = (struct fq_codel_schk *)(_si->sched+1);
if(si->flows) {
D("si already configured!");
return 0;
}
q = &si->main_q;
set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
q->_si = _si;
q->fs = _si->sched->fs;
si->flows = mallocarray(schk->cfg.flows_cnt,
sizeof(struct fq_codel_flow), M_DUMMYNET, M_NOWAIT | M_ZERO);
if (si->flows == NULL) {
D("cannot allocate memory for fq_codel configuration parameters");
return ENOMEM ;
}
si->perturbation = random();
STAILQ_INIT(&si->newflows);
STAILQ_INIT(&si->oldflows);
for (i = 0; i < schk->cfg.flows_cnt; i++) {
si->flows[i].cst.maxpkt_size = 500;
}
fq_codel_desc.ref_count++;
return 0;
}
static int
fq_codel_free_sched(struct dn_sch_inst *_si)
{
struct fq_codel_si *si = (struct fq_codel_si *)_si ;
free(si->flows , M_DUMMYNET);
si->flows = NULL;
fq_codel_desc.ref_count--;
return 0;
}
static int
fq_codel_config(struct dn_schk *_schk)
{
struct fq_codel_schk *schk;
struct dn_extra_parms *ep;
struct dn_sch_fq_codel_parms *fqc_cfg;
schk = (struct fq_codel_schk *)(_schk+1);
ep = (struct dn_extra_parms *) _schk->cfg;
if (ep && ep->oid.len ==sizeof(*ep) &&
ep->oid.subtype == DN_SCH_PARAMS) {
fqc_cfg = &schk->cfg;
if (ep->par[0] < 0)
fqc_cfg->ccfg.target = fq_codel_sysctl.ccfg.target;
else
fqc_cfg->ccfg.target = ep->par[0] * AQM_TIME_1US;
if (ep->par[1] < 0)
fqc_cfg->ccfg.interval = fq_codel_sysctl.ccfg.interval;
else
fqc_cfg->ccfg.interval = ep->par[1] * AQM_TIME_1US;
if (ep->par[2] < 0)
fqc_cfg->ccfg.flags = 0;
else
fqc_cfg->ccfg.flags = ep->par[2];
if (ep->par[3] < 0)
fqc_cfg->quantum = fq_codel_sysctl.quantum;
else
fqc_cfg->quantum = ep->par[3];
if (ep->par[4] < 0)
fqc_cfg->limit = fq_codel_sysctl.limit;
else
fqc_cfg->limit = ep->par[4];
if (ep->par[5] < 0)
fqc_cfg->flows_cnt = fq_codel_sysctl.flows_cnt;
else
fqc_cfg->flows_cnt = ep->par[5];
fqc_cfg->ccfg.target = BOUND_VAR(fqc_cfg->ccfg.target, 1 ,
5 * AQM_TIME_1S); ;
fqc_cfg->ccfg.interval = BOUND_VAR(fqc_cfg->ccfg.interval, 1,
100 * AQM_TIME_1S);
fqc_cfg->quantum = BOUND_VAR(fqc_cfg->quantum,1, 9000);
fqc_cfg->limit= BOUND_VAR(fqc_cfg->limit,1,20480);
fqc_cfg->flows_cnt= BOUND_VAR(fqc_cfg->flows_cnt,1,65536);
}
else
return 1;
return 0;
}
static int
fq_codel_getconfig (struct dn_schk *_schk, struct dn_extra_parms *ep) {
struct fq_codel_schk *schk = (struct fq_codel_schk *)(_schk+1);
struct dn_sch_fq_codel_parms *fqc_cfg;
fqc_cfg = &schk->cfg;
strcpy(ep->name, fq_codel_desc.name);
ep->par[0] = fqc_cfg->ccfg.target / AQM_TIME_1US;
ep->par[1] = fqc_cfg->ccfg.interval / AQM_TIME_1US;
ep->par[2] = fqc_cfg->ccfg.flags;
ep->par[3] = fqc_cfg->quantum;
ep->par[4] = fqc_cfg->limit;
ep->par[5] = fqc_cfg->flows_cnt;
return 0;
}
static struct dn_alg fq_codel_desc = {
_SI( .type = ) DN_SCHED_FQ_CODEL,
_SI( .name = ) "FQ_CODEL",
_SI( .flags = ) 0,
_SI( .schk_datalen = ) sizeof(struct fq_codel_schk),
_SI( .si_datalen = ) sizeof(struct fq_codel_si) - sizeof(struct dn_sch_inst),
_SI( .q_datalen = ) 0,
_SI( .enqueue = ) fq_codel_enqueue,
_SI( .dequeue = ) fq_codel_dequeue,
_SI( .config = ) fq_codel_config,
_SI( .destroy = ) NULL,
_SI( .new_sched = ) fq_codel_new_sched,
_SI( .free_sched = ) fq_codel_free_sched,
_SI( .new_fsk = ) NULL,
_SI( .free_fsk = ) NULL,
_SI( .new_queue = ) NULL,
_SI( .free_queue = ) NULL,
_SI( .getconfig = ) fq_codel_getconfig,
_SI( .ref_count = ) 0
};
DECLARE_DNSCHED_MODULE(dn_fq_codel, &fq_codel_desc);