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
28 * $Id: dn_sched_rr.c 5621 2010-03-04 16:51:27Z luigi $
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 <netinet/ipfw/dn_heap.h>
44 #include <netinet/ipfw/ip_dn_private.h>
45 #include <netinet/ipfw/dn_sched.h>
50 #define DN_SCHED_RR 3 // XXX Where?
53 struct dn_queue q; /* Standard queue */
54 int status; /* 1: queue is in the list */
55 int credit; /* Number of bytes to transmit */
56 int quantum; /* quantum * C */
57 struct rr_queue *qnext; /* */
60 /* struct rr_schk contains global config parameters
61 * and is right after dn_schk
64 int min_q; /* Min quantum */
65 int max_q; /* Max quantum */
66 int q_bytes; /* Bytes per quantum */
69 /* per-instance round robin list, right after dn_sch_inst */
71 struct rr_queue *head, *tail; /* Pointer to current queue */
74 /* Append a queue to the rr list */
76 rr_append(struct rr_queue *q, struct rr_si *si)
78 q->status = 1; /* mark as in-rr_list */
79 q->credit = q->quantum; /* initialize credit */
81 /* append to the tail */
86 si->tail = q; /* advance the tail pointer */
87 q->qnext = si->head; /* make it circular */
90 /* Remove the head queue from circular list. */
92 rr_remove_head(struct rr_si *si)
95 return; /* empty queue */
98 if (si->head == si->tail) {
99 si->head = si->tail = NULL;
103 si->head = si->head->qnext;
104 si->tail->qnext = si->head;
107 /* Remove a queue from circular list.
108 * XXX see if ti can be merge with remove_queue()
111 remove_queue_q(struct rr_queue *q, struct rr_si *si)
113 struct rr_queue *prev;
122 for (prev = si->head; prev; prev = prev->qnext) {
123 if (prev->qnext != q)
125 prev->qnext = q->qnext;
135 next_pointer(struct rr_si *si)
137 if (si->head == NULL)
138 return; /* empty queue */
140 si->head = si->head->qnext;
141 si->tail = si->tail->qnext;
145 rr_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
148 struct rr_queue *rrq;
150 if (m != q->mq.head) {
151 if (dn_enqueue(q, m, 0)) /* packet was dropped */
157 /* If reach this point, queue q was idle */
158 si = (struct rr_si *)(_si + 1);
159 rrq = (struct rr_queue *)q;
161 if (rrq->status == 1) /* Queue is already in the queue list */
164 /* Insert the queue in the queue list */
171 rr_dequeue(struct dn_sch_inst *_si)
173 /* Access scheduler instance private data */
174 struct rr_si *si = (struct rr_si *)(_si + 1);
175 struct rr_queue *rrq;
178 while ( (rrq = si->head) ) {
179 struct mbuf *m = rrq->q.mq.head;
181 /* empty queue, remove from list */
185 len = m->m_pkthdr.len;
187 if (len > rrq->credit) {
189 rrq->credit += rrq->quantum;
194 return dn_dequeue(&rrq->q);
198 /* no packet to dequeue*/
203 rr_config(struct dn_schk *_schk)
205 struct rr_schk *schk = (struct rr_schk *)(_schk + 1);
208 /* use reasonable quantums (64..2k bytes, default 1500) */
211 schk->q_bytes = 1500; /* quantum */
217 rr_new_sched(struct dn_sch_inst *_si)
219 struct rr_si *si = (struct rr_si *)(_si + 1);
222 si->head = si->tail = NULL;
228 rr_free_sched(struct dn_sch_inst *_si)
236 rr_new_fsk(struct dn_fsk *fs)
238 struct rr_schk *schk = (struct rr_schk *)(fs->sched + 1);
239 /* par[0] is the weight, par[1] is the quantum step */
240 ipdn_bound_var(&fs->fs.par[0], 1,
241 1, 65536, "RR weight");
242 ipdn_bound_var(&fs->fs.par[1], schk->q_bytes,
243 schk->min_q, schk->max_q, "RR quantum");
248 rr_new_queue(struct dn_queue *_q)
250 struct rr_queue *q = (struct rr_queue *)_q;
252 _q->ni.oid.subtype = DN_SCHED_RR;
254 q->quantum = _q->fs->fs.par[0] * _q->fs->fs.par[1];
255 ND("called, q->quantum %d", q->quantum);
256 q->credit = q->quantum;
259 if (_q->mq.head != NULL) {
260 /* Queue NOT empty, insert in the queue list */
261 rr_append(q, (struct rr_si *)(_q->_si + 1));
267 rr_free_queue(struct dn_queue *_q)
269 struct rr_queue *q = (struct rr_queue *)_q;
272 if (q->status == 1) {
273 struct rr_si *si = (struct rr_si *)(_q->_si + 1);
274 remove_queue_q(q, si);
280 * RR scheduler descriptor
281 * contains the type of the scheduler, the name, the size of the
282 * structures and function pointers.
284 static struct dn_alg rr_desc = {
285 _SI( .type = ) DN_SCHED_RR,
287 _SI( .flags = ) DN_MULTIQUEUE,
289 _SI( .schk_datalen = ) 0,
290 _SI( .si_datalen = ) sizeof(struct rr_si),
291 _SI( .q_datalen = ) sizeof(struct rr_queue) - sizeof(struct dn_queue),
293 _SI( .enqueue = ) rr_enqueue,
294 _SI( .dequeue = ) rr_dequeue,
296 _SI( .config = ) rr_config,
297 _SI( .destroy = ) NULL,
298 _SI( .new_sched = ) rr_new_sched,
299 _SI( .free_sched = ) rr_free_sched,
300 _SI( .new_fsk = ) rr_new_fsk,
301 _SI( .free_fsk = ) NULL,
302 _SI( .new_queue = ) rr_new_queue,
303 _SI( .free_queue = ) rr_free_queue,
307 DECLARE_DNSCHED_MODULE(dn_rr, &rr_desc);