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_prio.c 6338 2010-05-26 15:06:34Z svn_panicucci $
31 #include <sys/malloc.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <net/if.h> /* IFNAMSIZ */
38 #include <netinet/in.h>
39 #include <netinet/ip_var.h> /* ipfw_rule_ref */
40 #include <netinet/ip_fw.h> /* flow_id */
41 #include <netinet/ip_dummynet.h>
42 #include <netinet/ipfw/dn_heap.h>
43 #include <netinet/ipfw/ip_dn_private.h>
44 #include <netinet/ipfw/dn_sched.h>
49 #define DN_SCHED_PRIO 5 //XXX
51 #if !defined(_KERNEL) || !defined(__linux__)
52 #define test_bit(ix, pData) ((*pData) & (1<<(ix)))
53 #define __set_bit(ix, pData) (*pData) |= (1<<(ix))
54 #define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix))
58 #define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix))
61 /* Size of the array of queues pointers. */
62 #define BITMAP_T unsigned long
63 #define MAXPRIO (sizeof(BITMAP_T) * 8)
66 * The scheduler instance contains an array of pointers to queues,
67 * one for each priority, and a bitmap listing backlogged queues.
70 BITMAP_T bitmap; /* array bitmap */
71 struct dn_queue *q_array[MAXPRIO]; /* Array of queues pointers */
75 * If a queue with the same priority is already backlogged, use
76 * that one instead of the queue passed as argument.
79 prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
81 struct prio_si *si = (struct prio_si *)(_si + 1);
82 int prio = q->fs->fs.par[0];
84 if (test_bit(prio, &si->bitmap) == 0) {
85 /* No queue with this priority, insert */
86 __set_bit(prio, &si->bitmap);
87 si->q_array[prio] = q;
88 } else { /* use the existing queue */
89 q = si->q_array[prio];
91 if (dn_enqueue(q, m, 0))
97 * Packets are dequeued only from the highest priority queue.
98 * The function ffs() return the lowest bit in the bitmap that rapresent
99 * the array index (-1) which contains the pointer to the highest priority
101 * After the dequeue, if this queue become empty, it is index is removed
103 * Scheduler is idle if the bitmap is empty
105 * NOTE: highest priority is 0, lowest is sched->max_prio_q
108 prio_dequeue(struct dn_sch_inst *_si)
110 struct prio_si *si = (struct prio_si *)(_si + 1);
115 if (si->bitmap == 0) /* scheduler idle */
118 prio = ffs(si->bitmap) - 1;
120 /* Take the highest priority queue in the scheduler */
121 q = si->q_array[prio];
125 if (q->mq.head == NULL) {
126 /* Queue is now empty, remove from scheduler
129 si->q_array[prio] = NULL;
130 __clear_bit(prio, &si->bitmap);
136 prio_new_sched(struct dn_sch_inst *_si)
138 struct prio_si *si = (struct prio_si *)(_si + 1);
140 bzero(si->q_array, sizeof(si->q_array));
147 prio_new_fsk(struct dn_fsk *fs)
149 /* Check if the prioritiy is between 0 and MAXPRIO-1 */
150 ipdn_bound_var(&fs->fs.par[0], 0, 0, MAXPRIO - 1, "PRIO priority");
155 prio_new_queue(struct dn_queue *q)
157 struct prio_si *si = (struct prio_si *)(q->_si + 1);
158 int prio = q->fs->fs.par[0];
159 struct dn_queue *oldq;
161 q->ni.oid.subtype = DN_SCHED_PRIO;
163 if (q->mq.head == NULL)
166 /* Queue already full, must insert in the scheduler or append
167 * mbufs to existing queue. This partly duplicates prio_enqueue
169 if (test_bit(prio, &si->bitmap) == 0) {
170 /* No queue with this priority, insert */
171 __set_bit(prio, &si->bitmap);
172 si->q_array[prio] = q;
173 } else if ( (oldq = si->q_array[prio]) != q) {
174 /* must append to the existing queue.
175 * can simply append q->mq.head to q2->...
176 * and add the counters to those of q2
178 oldq->mq.tail->m_nextpkt = q->mq.head;
179 oldq->mq.tail = q->mq.tail;
180 oldq->ni.length += q->ni.length;
182 oldq->ni.len_bytes += q->ni.len_bytes;
184 q->mq.tail = q->mq.head = NULL;
190 prio_free_queue(struct dn_queue *q, int safe)
192 int prio = q->fs->fs.par[0];
193 struct prio_si *si = (struct prio_si *)(q->_si + 1);
195 if (si->q_array[prio] == q) {
196 si->q_array[prio] = NULL;
197 __clear_bit(prio, &si->bitmap);
203 static struct dn_alg prio_desc = {
204 _SI( .type = ) DN_SCHED_PRIO,
205 _SI( .name = ) "PRIO",
206 _SI( .flags = ) DN_MULTIQUEUE,
208 /* we need extra space in the si and the queue */
209 _SI( .schk_datalen = ) 0,
210 _SI( .si_datalen = ) sizeof(struct prio_si),
211 _SI( .q_datalen = ) 0,
213 _SI( .enqueue = ) prio_enqueue,
214 _SI( .dequeue = ) prio_dequeue,
216 _SI( .config = ) NULL,
217 _SI( .destroy = ) NULL,
218 _SI( .new_sched = ) prio_new_sched,
219 _SI( .free_sched = ) NULL,
221 _SI( .new_fsk = ) prio_new_fsk,
222 _SI( .free_fsk = ) NULL,
224 _SI( .new_queue = ) prio_new_queue,
225 _SI( .free_queue = ) prio_free_queue,
229 DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc);