dn_sched_wf2q.c revision 240494
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 240494 2012-09-14 11:51:49Z glebius $
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>
46240494Sglebius#include <netpfil/ipfw/dn_sched.h>
47204591Sluigi#else
48204591Sluigi#include <dn_test.h>
49204591Sluigi#endif
50204591Sluigi
51204591Sluigi#ifndef MAX64
52204591Sluigi#define MAX64(x,y)  (( (int64_t) ( (y)-(x) )) > 0 ) ? (y) : (x)
53204591Sluigi#endif
54204591Sluigi
55204591Sluigi/*
56204591Sluigi * timestamps are computed on 64 bit using fixed point arithmetic.
57204591Sluigi * LMAX_BITS, WMAX_BITS are the max number of bits for the packet len
58204591Sluigi * and sum of weights, respectively. FRAC_BITS is the number of
59204591Sluigi * fractional bits. We want FRAC_BITS >> WMAX_BITS to avoid too large
60204591Sluigi * errors when computing the inverse, FRAC_BITS < 32 so we can do 1/w
61204591Sluigi * using an unsigned 32-bit division, and to avoid wraparounds we need
62204591Sluigi * LMAX_BITS + WMAX_BITS + FRAC_BITS << 64
63204591Sluigi * As an example
64204591Sluigi * FRAC_BITS = 26, LMAX_BITS=14, WMAX_BITS = 19
65204591Sluigi */
66204591Sluigi#ifndef FRAC_BITS
67204591Sluigi#define FRAC_BITS    28 /* shift for fixed point arithmetic */
68204591Sluigi#define	ONE_FP	(1UL << FRAC_BITS)
69204591Sluigi#endif
70204591Sluigi
71204591Sluigi/*
72204591Sluigi * Private information for the scheduler instance:
73204591Sluigi * sch_heap (key is Finish time) returns the next queue to serve
74204591Sluigi * ne_heap (key is Start time) stores not-eligible queues
75204591Sluigi * idle_heap (key=start/finish time) stores idle flows. It must
76204591Sluigi *	support extract-from-middle.
77204591Sluigi * A flow is only in 1 of the three heaps.
78204591Sluigi * XXX todo: use a more efficient data structure, e.g. a tree sorted
79204591Sluigi * by F with min_subtree(S) in each node
80204591Sluigi */
81204591Sluigistruct wf2qp_si {
82204591Sluigi    struct dn_heap sch_heap;	/* top extract - key Finish  time */
83204591Sluigi    struct dn_heap ne_heap;	/* top extract - key Start   time */
84204591Sluigi    struct dn_heap idle_heap;	/* random extract - key Start=Finish time */
85204591Sluigi    uint64_t V;			/* virtual time */
86204591Sluigi    uint32_t inv_wsum;		/* inverse of sum of weights */
87204591Sluigi    uint32_t wsum;		/* sum of weights */
88204591Sluigi};
89204591Sluigi
90204591Sluigistruct wf2qp_queue {
91204591Sluigi    struct dn_queue _q;
92204591Sluigi    uint64_t S, F;		/* start time, finish time */
93204591Sluigi    uint32_t inv_w;		/* ONE_FP / weight */
94204591Sluigi    int32_t heap_pos;		/* position (index) of struct in heap */
95204591Sluigi};
96204591Sluigi
97204591Sluigi/*
98204591Sluigi * This file implements a WF2Q+ scheduler as it has been in dummynet
99204591Sluigi * since 2000.
100204591Sluigi * The scheduler supports per-flow queues and has O(log N) complexity.
101204591Sluigi *
102204591Sluigi * WF2Q+ needs to drain entries from the idle heap so that we
103204591Sluigi * can keep the sum of weights up to date. We can do it whenever
104204591Sluigi * we get a chance, or periodically, or following some other
105204591Sluigi * strategy. The function idle_check() drains at most N elements
106204591Sluigi * from the idle heap.
107204591Sluigi */
108204591Sluigistatic void
109204591Sluigiidle_check(struct wf2qp_si *si, int n, int force)
110204591Sluigi{
111204591Sluigi    struct dn_heap *h = &si->idle_heap;
112204591Sluigi    while (n-- > 0 && h->elements > 0 &&
113204591Sluigi		(force || DN_KEY_LT(HEAP_TOP(h)->key, si->V))) {
114204591Sluigi	struct dn_queue *q = HEAP_TOP(h)->object;
115204591Sluigi        struct wf2qp_queue *alg_fq = (struct wf2qp_queue *)q;
116204591Sluigi
117204591Sluigi        heap_extract(h, NULL);
118204591Sluigi        /* XXX to let the flowset delete the queue we should
119204591Sluigi	 * mark it as 'unused' by the scheduler.
120204591Sluigi	 */
121204591Sluigi        alg_fq->S = alg_fq->F + 1; /* Mark timestamp as invalid. */
122204591Sluigi        si->wsum -= q->fs->fs.par[0];	/* adjust sum of weights */
123204591Sluigi	if (si->wsum > 0)
124204591Sluigi		si->inv_wsum = ONE_FP/si->wsum;
125204591Sluigi    }
126204591Sluigi}
127204591Sluigi
128206845Sluigistatic int
129204591Sluigiwf2qp_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
130204591Sluigi{
131204591Sluigi    struct dn_fsk *fs = q->fs;
132204591Sluigi    struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1);
133204591Sluigi    struct wf2qp_queue *alg_fq;
134204591Sluigi    uint64_t len = m->m_pkthdr.len;
135204591Sluigi
136204591Sluigi    if (m != q->mq.head) {
137204591Sluigi	if (dn_enqueue(q, m, 0)) /* packet was dropped */
138204591Sluigi	    return 1;
139204591Sluigi	if (m != q->mq.head)	/* queue was already busy */
140204591Sluigi	    return 0;
141204591Sluigi    }
142204591Sluigi
143206845Sluigi    /* If reach this point, queue q was idle */
144204591Sluigi    alg_fq = (struct wf2qp_queue *)q;
145204591Sluigi
146204591Sluigi    if (DN_KEY_LT(alg_fq->F, alg_fq->S)) {
147204591Sluigi        /* F<S means timestamps are invalid ->brand new queue. */
148204591Sluigi        alg_fq->S = si->V;		/* init start time */
149204591Sluigi        si->wsum += fs->fs.par[0];	/* add weight of new queue. */
150204591Sluigi	si->inv_wsum = ONE_FP/si->wsum;
151204591Sluigi    } else { /* if it was idle then it was in the idle heap */
152204591Sluigi        heap_extract(&si->idle_heap, q);
153204591Sluigi        alg_fq->S = MAX64(alg_fq->F, si->V);	/* compute new S */
154204591Sluigi    }
155204591Sluigi    alg_fq->F = alg_fq->S + len * alg_fq->inv_w;
156204591Sluigi
157204591Sluigi    /* if nothing is backlogged, make sure this flow is eligible */
158204591Sluigi    if (si->ne_heap.elements == 0 && si->sch_heap.elements == 0)
159204591Sluigi        si->V = MAX64(alg_fq->S, si->V);
160204591Sluigi
161204591Sluigi    /*
162204591Sluigi     * Look at eligibility. A flow is not eligibile if S>V (when
163204591Sluigi     * this happens, it means that there is some other flow already
164204591Sluigi     * scheduled for the same pipe, so the sch_heap cannot be
165204591Sluigi     * empty). If the flow is not eligible we just store it in the
166204591Sluigi     * ne_heap. Otherwise, we store in the sch_heap.
167204591Sluigi     * Note that for all flows in sch_heap (SCH), S_i <= V,
168204591Sluigi     * and for all flows in ne_heap (NEH), S_i > V.
169204591Sluigi     * So when we need to compute max(V, min(S_i)) forall i in
170204591Sluigi     * SCH+NEH, we only need to look into NEH.
171204591Sluigi     */
172204591Sluigi    if (DN_KEY_LT(si->V, alg_fq->S)) {
173204591Sluigi        /* S>V means flow Not eligible. */
174204591Sluigi        if (si->sch_heap.elements == 0)
175204591Sluigi            D("++ ouch! not eligible but empty scheduler!");
176204591Sluigi        heap_insert(&si->ne_heap, alg_fq->S, q);
177204591Sluigi    } else {
178204591Sluigi        heap_insert(&si->sch_heap, alg_fq->F, q);
179204591Sluigi    }
180204591Sluigi    return 0;
181204591Sluigi}
182204591Sluigi
183204591Sluigi/* XXX invariant: sch > 0 || V >= min(S in neh) */
184204591Sluigistatic struct mbuf *
185204591Sluigiwf2qp_dequeue(struct dn_sch_inst *_si)
186204591Sluigi{
187204591Sluigi	/* Access scheduler instance private data */
188204591Sluigi	struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1);
189204591Sluigi	struct mbuf *m;
190204591Sluigi	struct dn_queue *q;
191204591Sluigi	struct dn_heap *sch = &si->sch_heap;
192204591Sluigi	struct dn_heap *neh = &si->ne_heap;
193204591Sluigi	struct wf2qp_queue *alg_fq;
194204591Sluigi
195204591Sluigi	if (sch->elements == 0 && neh->elements == 0) {
196204591Sluigi		/* we have nothing to do. We could kill the idle heap
197204591Sluigi		 * altogether and reset V
198204591Sluigi		 */
199204591Sluigi		idle_check(si, 0x7fffffff, 1);
200204591Sluigi		si->V = 0;
201204591Sluigi		si->wsum = 0;	/* should be set already */
202204591Sluigi		return NULL;	/* quick return if nothing to do */
203204591Sluigi	}
204204591Sluigi	idle_check(si, 1, 0);	/* drain something from the idle heap */
205204591Sluigi
206204591Sluigi	/* make sure at least one element is eligible, bumping V
207204591Sluigi	 * and moving entries that have become eligible.
208204591Sluigi	 * We need to repeat the first part twice, before and
209204591Sluigi	 * after extracting the candidate, or enqueue() will
210204591Sluigi	 * find the data structure in a wrong state.
211204591Sluigi	 */
212204591Sluigi  m = NULL;
213204591Sluigi  for(;;) {
214204591Sluigi	/*
215204591Sluigi	 * Compute V = max(V, min(S_i)). Remember that all elements
216204591Sluigi	 * in sch have by definition S_i <= V so if sch is not empty,
217204591Sluigi	 * V is surely the max and we must not update it. Conversely,
218204591Sluigi	 * if sch is empty we only need to look at neh.
219204591Sluigi	 * We don't need to move the queues, as it will be done at the
220204591Sluigi	 * next enqueue
221204591Sluigi	 */
222204591Sluigi	if (sch->elements == 0 && neh->elements > 0) {
223204591Sluigi		si->V = MAX64(si->V, HEAP_TOP(neh)->key);
224204591Sluigi	}
225204591Sluigi	while (neh->elements > 0 &&
226204591Sluigi		    DN_KEY_LEQ(HEAP_TOP(neh)->key, si->V)) {
227204591Sluigi		q = HEAP_TOP(neh)->object;
228204591Sluigi		alg_fq = (struct wf2qp_queue *)q;
229204591Sluigi		heap_extract(neh, NULL);
230204591Sluigi		heap_insert(sch, alg_fq->F, q);
231204591Sluigi	}
232204591Sluigi	if (m) /* pkt found in previous iteration */
233204591Sluigi		break;
234204591Sluigi	/* ok we have at least one eligible pkt */
235204591Sluigi	q = HEAP_TOP(sch)->object;
236204591Sluigi	alg_fq = (struct wf2qp_queue *)q;
237204591Sluigi	m = dn_dequeue(q);
238204591Sluigi	heap_extract(sch, NULL); /* Remove queue from heap. */
239204591Sluigi	si->V += (uint64_t)(m->m_pkthdr.len) * si->inv_wsum;
240204591Sluigi	alg_fq->S = alg_fq->F;  /* Update start time. */
241204591Sluigi	if (q->mq.head == 0) {	/* not backlogged any more. */
242204591Sluigi		heap_insert(&si->idle_heap, alg_fq->F, q);
243204591Sluigi	} else {			/* Still backlogged. */
244204591Sluigi		/* Update F, store in neh or sch */
245204591Sluigi		uint64_t len = q->mq.head->m_pkthdr.len;
246204591Sluigi		alg_fq->F += len * alg_fq->inv_w;
247204591Sluigi		if (DN_KEY_LEQ(alg_fq->S, si->V)) {
248204591Sluigi			heap_insert(sch, alg_fq->F, q);
249204591Sluigi		} else {
250204591Sluigi			heap_insert(neh, alg_fq->S, q);
251204591Sluigi		}
252204591Sluigi	}
253204591Sluigi    }
254204591Sluigi	return m;
255204591Sluigi}
256204591Sluigi
257204591Sluigistatic int
258204591Sluigiwf2qp_new_sched(struct dn_sch_inst *_si)
259204591Sluigi{
260204591Sluigi	struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1);
261204591Sluigi	int ofs = offsetof(struct wf2qp_queue, heap_pos);
262204591Sluigi
263204591Sluigi	/* all heaps support extract from middle */
264204591Sluigi	if (heap_init(&si->idle_heap, 16, ofs) ||
265204591Sluigi	    heap_init(&si->sch_heap, 16, ofs) ||
266204591Sluigi	    heap_init(&si->ne_heap, 16, ofs)) {
267204591Sluigi		heap_free(&si->ne_heap);
268204591Sluigi		heap_free(&si->sch_heap);
269204591Sluigi		heap_free(&si->idle_heap);
270204591Sluigi		return ENOMEM;
271204591Sluigi	}
272204591Sluigi	return 0;
273204591Sluigi}
274204591Sluigi
275204591Sluigistatic int
276204591Sluigiwf2qp_free_sched(struct dn_sch_inst *_si)
277204591Sluigi{
278204591Sluigi	struct wf2qp_si *si = (struct wf2qp_si *)(_si + 1);
279204591Sluigi
280204591Sluigi	heap_free(&si->sch_heap);
281204591Sluigi	heap_free(&si->ne_heap);
282204591Sluigi	heap_free(&si->idle_heap);
283204591Sluigi
284204591Sluigi	return 0;
285204591Sluigi}
286204591Sluigi
287204591Sluigistatic int
288204591Sluigiwf2qp_new_fsk(struct dn_fsk *fs)
289204591Sluigi{
290204591Sluigi	ipdn_bound_var(&fs->fs.par[0], 1,
291204591Sluigi		1, 100, "WF2Q+ weight");
292204591Sluigi	return 0;
293204591Sluigi}
294204591Sluigi
295204591Sluigistatic int
296204591Sluigiwf2qp_new_queue(struct dn_queue *_q)
297204591Sluigi{
298204591Sluigi	struct wf2qp_queue *q = (struct wf2qp_queue *)_q;
299204591Sluigi
300204591Sluigi	_q->ni.oid.subtype = DN_SCHED_WF2QP;
301204591Sluigi	q->F = 0;	/* not strictly necessary */
302204591Sluigi	q->S = q->F + 1;    /* mark timestamp as invalid. */
303204591Sluigi        q->inv_w = ONE_FP / _q->fs->fs.par[0];
304204591Sluigi	if (_q->mq.head != NULL) {
305204591Sluigi		wf2qp_enqueue(_q->_si, _q, _q->mq.head);
306204591Sluigi	}
307204591Sluigi	return 0;
308204591Sluigi}
309204591Sluigi
310204591Sluigi/*
311204591Sluigi * Called when the infrastructure removes a queue (e.g. flowset
312204591Sluigi * is reconfigured). Nothing to do if we did not 'own' the queue,
313204591Sluigi * otherwise remove it from the right heap and adjust the sum
314204591Sluigi * of weights.
315204591Sluigi */
316204591Sluigistatic int
317204591Sluigiwf2qp_free_queue(struct dn_queue *q)
318204591Sluigi{
319204591Sluigi	struct wf2qp_queue *alg_fq = (struct wf2qp_queue *)q;
320204591Sluigi	struct wf2qp_si *si = (struct wf2qp_si *)(q->_si + 1);
321213267Sluigi
322204591Sluigi	if (alg_fq->S >= alg_fq->F + 1)
323204591Sluigi		return 0;	/* nothing to do, not in any heap */
324204591Sluigi	si->wsum -= q->fs->fs.par[0];
325204591Sluigi	if (si->wsum > 0)
326204591Sluigi		si->inv_wsum = ONE_FP/si->wsum;
327204591Sluigi
328204591Sluigi	/* extract from the heap. XXX TODO we may need to adjust V
329204591Sluigi	 * to make sure the invariants hold.
330204591Sluigi	 */
331204591Sluigi	if (q->mq.head == NULL) {
332204591Sluigi		heap_extract(&si->idle_heap, q);
333204591Sluigi	} else if (DN_KEY_LT(si->V, alg_fq->S)) {
334204591Sluigi		heap_extract(&si->ne_heap, q);
335204591Sluigi	} else {
336204591Sluigi		heap_extract(&si->sch_heap, q);
337204591Sluigi	}
338204591Sluigi	return 0;
339204591Sluigi}
340204591Sluigi
341204591Sluigi/*
342204591Sluigi * WF2Q+ scheduler descriptor
343204591Sluigi * contains the type of the scheduler, the name, the size of the
344204591Sluigi * structures and function pointers.
345204591Sluigi */
346204591Sluigistatic struct dn_alg wf2qp_desc = {
347204591Sluigi	_SI( .type = ) DN_SCHED_WF2QP,
348204591Sluigi	_SI( .name = ) "WF2Q+",
349204591Sluigi	_SI( .flags = ) DN_MULTIQUEUE,
350204591Sluigi
351204591Sluigi	/* we need extra space in the si and the queue */
352204591Sluigi	_SI( .schk_datalen = ) 0,
353204591Sluigi	_SI( .si_datalen = ) sizeof(struct wf2qp_si),
354204591Sluigi	_SI( .q_datalen = ) sizeof(struct wf2qp_queue) -
355204591Sluigi				sizeof(struct dn_queue),
356204591Sluigi
357204591Sluigi	_SI( .enqueue = ) wf2qp_enqueue,
358204591Sluigi	_SI( .dequeue = ) wf2qp_dequeue,
359204591Sluigi
360204591Sluigi	_SI( .config = )  NULL,
361204591Sluigi	_SI( .destroy = )  NULL,
362204591Sluigi	_SI( .new_sched = ) wf2qp_new_sched,
363204591Sluigi	_SI( .free_sched = ) wf2qp_free_sched,
364206845Sluigi
365204591Sluigi	_SI( .new_fsk = ) wf2qp_new_fsk,
366204591Sluigi	_SI( .free_fsk = )  NULL,
367204591Sluigi
368204591Sluigi	_SI( .new_queue = ) wf2qp_new_queue,
369204591Sluigi	_SI( .free_queue = ) wf2qp_free_queue,
370204591Sluigi};
371204591Sluigi
372204591Sluigi
373204591SluigiDECLARE_DNSCHED_MODULE(dn_wf2qp, &wf2qp_desc);
374