dn_sched_wf2q.c revision 300779
1204591Sluigi/* 2204591Sluigi * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa 3204591Sluigi * Copyright (c) 2000-2002 Luigi Rizzo, Universita` di Pisa 4204591Sluigi * All rights reserved 5204591Sluigi * 6204591Sluigi * Redistribution and use in source and binary forms, with or without 7204591Sluigi * modification, are permitted provided that the following conditions 8204591Sluigi * are met: 9204591Sluigi * 1. Redistributions of source code must retain the above copyright 10204591Sluigi * notice, this list of conditions and the following disclaimer. 11204591Sluigi * 2. Redistributions in binary form must reproduce the above copyright 12204591Sluigi * notice, this list of conditions and the following disclaimer in the 13204591Sluigi * documentation and/or other materials provided with the distribution. 14204591Sluigi * 15204591Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16204591Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17204591Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18204591Sluigi * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19204591Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20204591Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21204591Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22204591Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23204591Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24204591Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25204591Sluigi * SUCH DAMAGE. 26204591Sluigi */ 27204591Sluigi 28204591Sluigi/* 29204591Sluigi * $FreeBSD: head/sys/netpfil/ipfw/dn_sched_wf2q.c 300779 2016-05-26 21:40:13Z truckman $ 30204591Sluigi */ 31204591Sluigi 32204591Sluigi#ifdef _KERNEL 33204591Sluigi#include <sys/malloc.h> 34204591Sluigi#include <sys/socket.h> 35204591Sluigi#include <sys/socketvar.h> 36204591Sluigi#include <sys/kernel.h> 37204591Sluigi#include <sys/mbuf.h> 38204591Sluigi#include <sys/module.h> 39204591Sluigi#include <net/if.h> /* IFNAMSIZ */ 40204591Sluigi#include <netinet/in.h> 41204591Sluigi#include <netinet/ip_var.h> /* ipfw_rule_ref */ 42204591Sluigi#include <netinet/ip_fw.h> /* flow_id */ 43204591Sluigi#include <netinet/ip_dummynet.h> 44240494Sglebius#include <netpfil/ipfw/dn_heap.h> 45240494Sglebius#include <netpfil/ipfw/ip_dn_private.h> 46300779Struckman#ifdef NEW_AQM 47300779Struckman#include <netpfil/ipfw/dn_aqm.h> 48300779Struckman#endif 49240494Sglebius#include <netpfil/ipfw/dn_sched.h> 50204591Sluigi#else 51204591Sluigi#include <dn_test.h> 52204591Sluigi#endif 53204591Sluigi 54204591Sluigi#ifndef MAX64 55204591Sluigi#define MAX64(x,y) (( (int64_t) ( (y)-(x) )) > 0 ) ? (y) : (x) 56204591Sluigi#endif 57204591Sluigi 58204591Sluigi/* 59204591Sluigi * timestamps are computed on 64 bit using fixed point arithmetic. 60204591Sluigi * LMAX_BITS, WMAX_BITS are the max number of bits for the packet len 61204591Sluigi * and sum of weights, respectively. FRAC_BITS is the number of 62204591Sluigi * fractional bits. We want FRAC_BITS >> WMAX_BITS to avoid too large 63204591Sluigi * errors when computing the inverse, FRAC_BITS < 32 so we can do 1/w 64204591Sluigi * using an unsigned 32-bit division, and to avoid wraparounds we need 65204591Sluigi * LMAX_BITS + WMAX_BITS + FRAC_BITS << 64 66204591Sluigi * As an example 67204591Sluigi * FRAC_BITS = 26, LMAX_BITS=14, WMAX_BITS = 19 68204591Sluigi */ 69204591Sluigi#ifndef FRAC_BITS 70204591Sluigi#define FRAC_BITS 28 /* shift for fixed point arithmetic */ 71204591Sluigi#define ONE_FP (1UL << FRAC_BITS) 72204591Sluigi#endif 73204591Sluigi 74204591Sluigi/* 75204591Sluigi * Private information for the scheduler instance: 76204591Sluigi * sch_heap (key is Finish time) returns the next queue to serve 77204591Sluigi * ne_heap (key is Start time) stores not-eligible queues 78204591Sluigi * idle_heap (key=start/finish time) stores idle flows. It must 79204591Sluigi * support extract-from-middle. 80204591Sluigi * A flow is only in 1 of the three heaps. 81204591Sluigi * XXX todo: use a more efficient data structure, e.g. a tree sorted 82204591Sluigi * by F with min_subtree(S) in each node 83204591Sluigi */ 84204591Sluigistruct wf2qp_si { 85204591Sluigi struct dn_heap sch_heap; /* top extract - key Finish time */ 86204591Sluigi struct dn_heap ne_heap; /* top extract - key Start time */ 87204591Sluigi struct dn_heap idle_heap; /* random extract - key Start=Finish time */ 88204591Sluigi uint64_t V; /* virtual time */ 89204591Sluigi uint32_t inv_wsum; /* inverse of sum of weights */ 90204591Sluigi uint32_t wsum; /* sum of weights */ 91204591Sluigi}; 92204591Sluigi 93204591Sluigistruct wf2qp_queue { 94204591Sluigi struct dn_queue _q; 95204591Sluigi uint64_t S, F; /* start time, finish time */ 96204591Sluigi uint32_t inv_w; /* ONE_FP / weight */ 97204591Sluigi int32_t heap_pos; /* position (index) of struct in heap */ 98204591Sluigi}; 99204591Sluigi 100204591Sluigi/* 101204591Sluigi * This file implements a WF2Q+ scheduler as it has been in dummynet 102204591Sluigi * since 2000. 103204591Sluigi * The scheduler supports per-flow queues and has O(log N) complexity. 104204591Sluigi * 105204591Sluigi * WF2Q+ needs to drain entries from the idle heap so that we 106204591Sluigi * can keep the sum of weights up to date. We can do it whenever 107204591Sluigi * we get a chance, or periodically, or following some other 108204591Sluigi * strategy. The function idle_check() drains at most N elements 109204591Sluigi * from the idle heap. 110204591Sluigi */ 111204591Sluigistatic void 112204591Sluigiidle_check(struct wf2qp_si *si, int n, int force) 113204591Sluigi{ 114204591Sluigi struct dn_heap *h = &si->idle_heap; 115204591Sluigi while (n-- > 0 && h->elements > 0 && 116204591Sluigi (force || DN_KEY_LT(HEAP_TOP(h)->key, si->V))) { 117204591Sluigi struct dn_queue *q = HEAP_TOP(h)->object; 118204591Sluigi struct wf2qp_queue *alg_fq = (struct wf2qp_queue *)q; 119204591Sluigi 120204591Sluigi heap_extract(h, NULL); 121204591Sluigi /* XXX to let the flowset delete the queue we should 122204591Sluigi * mark it as 'unused' by the scheduler. 123204591Sluigi */ 124204591Sluigi alg_fq->S = alg_fq->F + 1; /* Mark timestamp as invalid. */ 125204591Sluigi si->wsum -= q->fs->fs.par[0]; /* adjust sum of weights */ 126204591Sluigi if (si->wsum > 0) 127204591Sluigi si->inv_wsum = ONE_FP/si->wsum; 128204591Sluigi } 129204591Sluigi} 130204591Sluigi 131206845Sluigistatic int 132204591Sluigiwf2qp_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m) 133204591Sluigi{ 134204591Sluigi struct dn_fsk *fs = q->fs; 135204591Sluigi struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1); 136204591Sluigi struct wf2qp_queue *alg_fq; 137204591Sluigi uint64_t len = m->m_pkthdr.len; 138204591Sluigi 139204591Sluigi if (m != q->mq.head) { 140204591Sluigi if (dn_enqueue(q, m, 0)) /* packet was dropped */ 141204591Sluigi return 1; 142204591Sluigi if (m != q->mq.head) /* queue was already busy */ 143204591Sluigi return 0; 144204591Sluigi } 145204591Sluigi 146206845Sluigi /* If reach this point, queue q was idle */ 147204591Sluigi alg_fq = (struct wf2qp_queue *)q; 148204591Sluigi 149204591Sluigi if (DN_KEY_LT(alg_fq->F, alg_fq->S)) { 150204591Sluigi /* F<S means timestamps are invalid ->brand new queue. */ 151204591Sluigi alg_fq->S = si->V; /* init start time */ 152204591Sluigi si->wsum += fs->fs.par[0]; /* add weight of new queue. */ 153204591Sluigi si->inv_wsum = ONE_FP/si->wsum; 154204591Sluigi } else { /* if it was idle then it was in the idle heap */ 155204591Sluigi heap_extract(&si->idle_heap, q); 156204591Sluigi alg_fq->S = MAX64(alg_fq->F, si->V); /* compute new S */ 157204591Sluigi } 158204591Sluigi alg_fq->F = alg_fq->S + len * alg_fq->inv_w; 159204591Sluigi 160204591Sluigi /* if nothing is backlogged, make sure this flow is eligible */ 161204591Sluigi if (si->ne_heap.elements == 0 && si->sch_heap.elements == 0) 162204591Sluigi si->V = MAX64(alg_fq->S, si->V); 163204591Sluigi 164204591Sluigi /* 165204591Sluigi * Look at eligibility. A flow is not eligibile if S>V (when 166204591Sluigi * this happens, it means that there is some other flow already 167204591Sluigi * scheduled for the same pipe, so the sch_heap cannot be 168204591Sluigi * empty). If the flow is not eligible we just store it in the 169204591Sluigi * ne_heap. Otherwise, we store in the sch_heap. 170204591Sluigi * Note that for all flows in sch_heap (SCH), S_i <= V, 171204591Sluigi * and for all flows in ne_heap (NEH), S_i > V. 172204591Sluigi * So when we need to compute max(V, min(S_i)) forall i in 173204591Sluigi * SCH+NEH, we only need to look into NEH. 174204591Sluigi */ 175204591Sluigi if (DN_KEY_LT(si->V, alg_fq->S)) { 176204591Sluigi /* S>V means flow Not eligible. */ 177204591Sluigi if (si->sch_heap.elements == 0) 178204591Sluigi D("++ ouch! not eligible but empty scheduler!"); 179204591Sluigi heap_insert(&si->ne_heap, alg_fq->S, q); 180204591Sluigi } else { 181204591Sluigi heap_insert(&si->sch_heap, alg_fq->F, q); 182204591Sluigi } 183204591Sluigi return 0; 184204591Sluigi} 185204591Sluigi 186204591Sluigi/* XXX invariant: sch > 0 || V >= min(S in neh) */ 187204591Sluigistatic struct mbuf * 188204591Sluigiwf2qp_dequeue(struct dn_sch_inst *_si) 189204591Sluigi{ 190204591Sluigi /* Access scheduler instance private data */ 191204591Sluigi struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1); 192204591Sluigi struct mbuf *m; 193204591Sluigi struct dn_queue *q; 194204591Sluigi struct dn_heap *sch = &si->sch_heap; 195204591Sluigi struct dn_heap *neh = &si->ne_heap; 196204591Sluigi struct wf2qp_queue *alg_fq; 197204591Sluigi 198204591Sluigi if (sch->elements == 0 && neh->elements == 0) { 199204591Sluigi /* we have nothing to do. We could kill the idle heap 200204591Sluigi * altogether and reset V 201204591Sluigi */ 202204591Sluigi idle_check(si, 0x7fffffff, 1); 203204591Sluigi si->V = 0; 204204591Sluigi si->wsum = 0; /* should be set already */ 205204591Sluigi return NULL; /* quick return if nothing to do */ 206204591Sluigi } 207204591Sluigi idle_check(si, 1, 0); /* drain something from the idle heap */ 208204591Sluigi 209204591Sluigi /* make sure at least one element is eligible, bumping V 210204591Sluigi * and moving entries that have become eligible. 211204591Sluigi * We need to repeat the first part twice, before and 212204591Sluigi * after extracting the candidate, or enqueue() will 213204591Sluigi * find the data structure in a wrong state. 214204591Sluigi */ 215204591Sluigi m = NULL; 216204591Sluigi for(;;) { 217204591Sluigi /* 218204591Sluigi * Compute V = max(V, min(S_i)). Remember that all elements 219204591Sluigi * in sch have by definition S_i <= V so if sch is not empty, 220204591Sluigi * V is surely the max and we must not update it. Conversely, 221204591Sluigi * if sch is empty we only need to look at neh. 222204591Sluigi * We don't need to move the queues, as it will be done at the 223204591Sluigi * next enqueue 224204591Sluigi */ 225204591Sluigi if (sch->elements == 0 && neh->elements > 0) { 226204591Sluigi si->V = MAX64(si->V, HEAP_TOP(neh)->key); 227204591Sluigi } 228204591Sluigi while (neh->elements > 0 && 229204591Sluigi DN_KEY_LEQ(HEAP_TOP(neh)->key, si->V)) { 230204591Sluigi q = HEAP_TOP(neh)->object; 231204591Sluigi alg_fq = (struct wf2qp_queue *)q; 232204591Sluigi heap_extract(neh, NULL); 233204591Sluigi heap_insert(sch, alg_fq->F, q); 234204591Sluigi } 235204591Sluigi if (m) /* pkt found in previous iteration */ 236204591Sluigi break; 237204591Sluigi /* ok we have at least one eligible pkt */ 238204591Sluigi q = HEAP_TOP(sch)->object; 239204591Sluigi alg_fq = (struct wf2qp_queue *)q; 240204591Sluigi m = dn_dequeue(q); 241204591Sluigi heap_extract(sch, NULL); /* Remove queue from heap. */ 242204591Sluigi si->V += (uint64_t)(m->m_pkthdr.len) * si->inv_wsum; 243204591Sluigi alg_fq->S = alg_fq->F; /* Update start time. */ 244204591Sluigi if (q->mq.head == 0) { /* not backlogged any more. */ 245204591Sluigi heap_insert(&si->idle_heap, alg_fq->F, q); 246204591Sluigi } else { /* Still backlogged. */ 247204591Sluigi /* Update F, store in neh or sch */ 248204591Sluigi uint64_t len = q->mq.head->m_pkthdr.len; 249204591Sluigi alg_fq->F += len * alg_fq->inv_w; 250204591Sluigi if (DN_KEY_LEQ(alg_fq->S, si->V)) { 251204591Sluigi heap_insert(sch, alg_fq->F, q); 252204591Sluigi } else { 253204591Sluigi heap_insert(neh, alg_fq->S, q); 254204591Sluigi } 255204591Sluigi } 256204591Sluigi } 257204591Sluigi return m; 258204591Sluigi} 259204591Sluigi 260204591Sluigistatic int 261204591Sluigiwf2qp_new_sched(struct dn_sch_inst *_si) 262204591Sluigi{ 263204591Sluigi struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1); 264204591Sluigi int ofs = offsetof(struct wf2qp_queue, heap_pos); 265204591Sluigi 266204591Sluigi /* all heaps support extract from middle */ 267204591Sluigi if (heap_init(&si->idle_heap, 16, ofs) || 268204591Sluigi heap_init(&si->sch_heap, 16, ofs) || 269204591Sluigi heap_init(&si->ne_heap, 16, ofs)) { 270204591Sluigi heap_free(&si->ne_heap); 271204591Sluigi heap_free(&si->sch_heap); 272204591Sluigi heap_free(&si->idle_heap); 273204591Sluigi return ENOMEM; 274204591Sluigi } 275204591Sluigi return 0; 276204591Sluigi} 277204591Sluigi 278204591Sluigistatic int 279204591Sluigiwf2qp_free_sched(struct dn_sch_inst *_si) 280204591Sluigi{ 281204591Sluigi struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1); 282204591Sluigi 283204591Sluigi heap_free(&si->sch_heap); 284204591Sluigi heap_free(&si->ne_heap); 285204591Sluigi heap_free(&si->idle_heap); 286204591Sluigi 287204591Sluigi return 0; 288204591Sluigi} 289204591Sluigi 290204591Sluigistatic int 291204591Sluigiwf2qp_new_fsk(struct dn_fsk *fs) 292204591Sluigi{ 293204591Sluigi ipdn_bound_var(&fs->fs.par[0], 1, 294204591Sluigi 1, 100, "WF2Q+ weight"); 295204591Sluigi return 0; 296204591Sluigi} 297204591Sluigi 298204591Sluigistatic int 299204591Sluigiwf2qp_new_queue(struct dn_queue *_q) 300204591Sluigi{ 301204591Sluigi struct wf2qp_queue *q = (struct wf2qp_queue *)_q; 302204591Sluigi 303204591Sluigi _q->ni.oid.subtype = DN_SCHED_WF2QP; 304204591Sluigi q->F = 0; /* not strictly necessary */ 305204591Sluigi q->S = q->F + 1; /* mark timestamp as invalid. */ 306204591Sluigi q->inv_w = ONE_FP / _q->fs->fs.par[0]; 307204591Sluigi if (_q->mq.head != NULL) { 308204591Sluigi wf2qp_enqueue(_q->_si, _q, _q->mq.head); 309204591Sluigi } 310204591Sluigi return 0; 311204591Sluigi} 312204591Sluigi 313204591Sluigi/* 314204591Sluigi * Called when the infrastructure removes a queue (e.g. flowset 315204591Sluigi * is reconfigured). Nothing to do if we did not 'own' the queue, 316204591Sluigi * otherwise remove it from the right heap and adjust the sum 317204591Sluigi * of weights. 318204591Sluigi */ 319204591Sluigistatic int 320204591Sluigiwf2qp_free_queue(struct dn_queue *q) 321204591Sluigi{ 322204591Sluigi struct wf2qp_queue *alg_fq = (struct wf2qp_queue *)q; 323204591Sluigi struct wf2qp_si *si = (struct wf2qp_si *)(q->_si + 1); 324213267Sluigi 325204591Sluigi if (alg_fq->S >= alg_fq->F + 1) 326204591Sluigi return 0; /* nothing to do, not in any heap */ 327204591Sluigi si->wsum -= q->fs->fs.par[0]; 328204591Sluigi if (si->wsum > 0) 329204591Sluigi si->inv_wsum = ONE_FP/si->wsum; 330204591Sluigi 331204591Sluigi /* extract from the heap. XXX TODO we may need to adjust V 332204591Sluigi * to make sure the invariants hold. 333204591Sluigi */ 334204591Sluigi if (q->mq.head == NULL) { 335204591Sluigi heap_extract(&si->idle_heap, q); 336204591Sluigi } else if (DN_KEY_LT(si->V, alg_fq->S)) { 337204591Sluigi heap_extract(&si->ne_heap, q); 338204591Sluigi } else { 339204591Sluigi heap_extract(&si->sch_heap, q); 340204591Sluigi } 341204591Sluigi return 0; 342204591Sluigi} 343204591Sluigi 344204591Sluigi/* 345204591Sluigi * WF2Q+ scheduler descriptor 346204591Sluigi * contains the type of the scheduler, the name, the size of the 347204591Sluigi * structures and function pointers. 348204591Sluigi */ 349204591Sluigistatic struct dn_alg wf2qp_desc = { 350204591Sluigi _SI( .type = ) DN_SCHED_WF2QP, 351204591Sluigi _SI( .name = ) "WF2Q+", 352204591Sluigi _SI( .flags = ) DN_MULTIQUEUE, 353204591Sluigi 354204591Sluigi /* we need extra space in the si and the queue */ 355204591Sluigi _SI( .schk_datalen = ) 0, 356204591Sluigi _SI( .si_datalen = ) sizeof(struct wf2qp_si), 357204591Sluigi _SI( .q_datalen = ) sizeof(struct wf2qp_queue) - 358204591Sluigi sizeof(struct dn_queue), 359204591Sluigi 360204591Sluigi _SI( .enqueue = ) wf2qp_enqueue, 361204591Sluigi _SI( .dequeue = ) wf2qp_dequeue, 362204591Sluigi 363204591Sluigi _SI( .config = ) NULL, 364204591Sluigi _SI( .destroy = ) NULL, 365204591Sluigi _SI( .new_sched = ) wf2qp_new_sched, 366204591Sluigi _SI( .free_sched = ) wf2qp_free_sched, 367206845Sluigi 368204591Sluigi _SI( .new_fsk = ) wf2qp_new_fsk, 369204591Sluigi _SI( .free_fsk = ) NULL, 370204591Sluigi 371204591Sluigi _SI( .new_queue = ) wf2qp_new_queue, 372204591Sluigi _SI( .free_queue = ) wf2qp_free_queue, 373300779Struckman#ifdef NEW_AQM 374300779Struckman _SI( .getconfig = ) NULL, 375300779Struckman#endif 376300779Struckman 377204591Sluigi}; 378204591Sluigi 379204591Sluigi 380204591SluigiDECLARE_DNSCHED_MODULE(dn_wf2qp, &wf2qp_desc); 381