2 * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <net/if.h> /* IFNAMSIZ */
39 #include <netinet/in.h>
40 #include <netinet/ip_var.h> /* ipfw_rule_ref */
41 #include <netinet/ip_fw.h> /* flow_id */
42 #include <netinet/ip_dummynet.h>
43 #include <netpfil/ipfw/dn_heap.h>
44 #include <netpfil/ipfw/ip_dn_private.h>
46 #include <netpfil/ipfw/dn_aqm.h>
48 #include <netpfil/ipfw/dn_sched.h>
53 #define DN_SCHED_RR 3 // XXX Where?
56 struct dn_queue q; /* Standard queue */
57 int status; /* 1: queue is in the list */
58 uint32_t credit; /* max bytes we can transmit */
59 uint32_t quantum; /* quantum * weight */
60 struct rr_queue *qnext; /* */
63 /* struct rr_schk contains global config parameters
64 * and is right after dn_schk
67 uint32_t min_q; /* Min quantum */
68 uint32_t max_q; /* Max quantum */
69 uint32_t q_bytes; /* default quantum in bytes */
72 /* per-instance round robin list, right after dn_sch_inst */
74 struct rr_queue *head, *tail; /* Pointer to current queue */
77 /* Append a queue to the rr list */
79 rr_append(struct rr_queue *q, struct rr_si *si)
81 q->status = 1; /* mark as in-rr_list */
82 q->credit = q->quantum; /* initialize credit */
84 /* append to the tail */
89 si->tail = q; /* advance the tail pointer */
90 q->qnext = si->head; /* make it circular */
93 /* Remove the head queue from circular list. */
95 rr_remove_head(struct rr_si *si)
98 return; /* empty queue */
101 if (si->head == si->tail) {
102 si->head = si->tail = NULL;
106 si->head = si->head->qnext;
107 si->tail->qnext = si->head;
110 /* Remove a queue from circular list.
111 * XXX see if ti can be merge with remove_queue()
114 remove_queue_q(struct rr_queue *q, struct rr_si *si)
116 struct rr_queue *prev;
125 for (prev = si->head; prev; prev = prev->qnext) {
126 if (prev->qnext != q)
128 prev->qnext = q->qnext;
138 next_pointer(struct rr_si *si)
140 if (si->head == NULL)
141 return; /* empty queue */
143 si->head = si->head->qnext;
144 si->tail = si->tail->qnext;
148 rr_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
151 struct rr_queue *rrq;
153 if (m != q->mq.head) {
154 if (dn_enqueue(q, m, 0)) /* packet was dropped */
160 /* If reach this point, queue q was idle */
161 si = (struct rr_si *)(_si + 1);
162 rrq = (struct rr_queue *)q;
164 if (rrq->status == 1) /* Queue is already in the queue list */
167 /* Insert the queue in the queue list */
174 rr_dequeue(struct dn_sch_inst *_si)
176 /* Access scheduler instance private data */
177 struct rr_si *si = (struct rr_si *)(_si + 1);
178 struct rr_queue *rrq;
181 while ( (rrq = si->head) ) {
182 struct mbuf *m = rrq->q.mq.head;
184 /* empty queue, remove from list */
188 len = m->m_pkthdr.len;
190 if (len > rrq->credit) {
192 rrq->credit += rrq->quantum;
197 return dn_dequeue(&rrq->q);
201 /* no packet to dequeue*/
206 rr_config(struct dn_schk *_schk)
208 struct rr_schk *schk = (struct rr_schk *)(_schk + 1);
211 /* use reasonable quantums (64..2k bytes, default 1500) */
214 schk->q_bytes = 1500; /* quantum */
220 rr_new_sched(struct dn_sch_inst *_si)
222 struct rr_si *si = (struct rr_si *)(_si + 1);
225 si->head = si->tail = NULL;
231 rr_free_sched(struct dn_sch_inst *_si)
240 rr_new_fsk(struct dn_fsk *fs)
242 struct rr_schk *schk = (struct rr_schk *)(fs->sched + 1);
243 /* par[0] is the weight, par[1] is the quantum step */
244 /* make sure the product fits an uint32_t */
245 ipdn_bound_var(&fs->fs.par[0], 1,
246 1, 65536, "RR weight");
247 ipdn_bound_var(&fs->fs.par[1], schk->q_bytes,
248 schk->min_q, schk->max_q, "RR quantum");
253 rr_new_queue(struct dn_queue *_q)
255 struct rr_queue *q = (struct rr_queue *)_q;
258 _q->ni.oid.subtype = DN_SCHED_RR;
260 quantum = (uint64_t)_q->fs->fs.par[0] * _q->fs->fs.par[1];
261 if (quantum >= (1ULL<< 32)) {
262 D("quantum too large, truncating to 4G - 1");
263 quantum = (1ULL<< 32) - 1;
265 q->quantum = quantum;
266 ND("called, q->quantum %d", q->quantum);
267 q->credit = q->quantum;
270 if (_q->mq.head != NULL) {
271 /* Queue NOT empty, insert in the queue list */
272 rr_append(q, (struct rr_si *)(_q->_si + 1));
278 rr_free_queue(struct dn_queue *_q)
280 struct rr_queue *q = (struct rr_queue *)_q;
283 if (q->status == 1) {
284 struct rr_si *si = (struct rr_si *)(_q->_si + 1);
285 remove_queue_q(q, si);
291 * RR scheduler descriptor
292 * contains the type of the scheduler, the name, the size of the
293 * structures and function pointers.
295 static struct dn_alg rr_desc = {
296 _SI( .type = ) DN_SCHED_RR,
298 _SI( .flags = ) DN_MULTIQUEUE,
300 _SI( .schk_datalen = ) sizeof(struct rr_schk),
301 _SI( .si_datalen = ) sizeof(struct rr_si),
302 _SI( .q_datalen = ) sizeof(struct rr_queue) - sizeof(struct dn_queue),
304 _SI( .enqueue = ) rr_enqueue,
305 _SI( .dequeue = ) rr_dequeue,
307 _SI( .config = ) rr_config,
308 _SI( .destroy = ) NULL,
309 _SI( .new_sched = ) rr_new_sched,
310 _SI( .free_sched = ) rr_free_sched,
311 _SI( .new_fsk = ) rr_new_fsk,
312 _SI( .free_fsk = ) NULL,
313 _SI( .new_queue = ) rr_new_queue,
314 _SI( .free_queue = ) rr_free_queue,
316 _SI( .getconfig = ) NULL,
321 DECLARE_DNSCHED_MODULE(dn_rr, &rr_desc);