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
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 <netpfil/ipfw/dn_heap.h>
43 #include <netpfil/ipfw/ip_dn_private.h>
45 #include <netpfil/ipfw/dn_aqm.h>
47 #include <netpfil/ipfw/dn_sched.h>
52 #define DN_SCHED_PRIO 5 //XXX
54 #if !defined(_KERNEL) || !defined(__linux__)
55 #define test_bit(ix, pData) ((*pData) & (1<<(ix)))
56 #define __set_bit(ix, pData) (*pData) |= (1<<(ix))
57 #define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix))
61 #define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix))
64 /* Size of the array of queues pointers. */
65 #define BITMAP_T unsigned long
66 #define MAXPRIO (sizeof(BITMAP_T) * 8)
69 * The scheduler instance contains an array of pointers to queues,
70 * one for each priority, and a bitmap listing backlogged queues.
73 BITMAP_T bitmap; /* array bitmap */
74 struct dn_queue *q_array[MAXPRIO]; /* Array of queues pointers */
78 * If a queue with the same priority is already backlogged, use
79 * that one instead of the queue passed as argument.
82 prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
84 struct prio_si *si = (struct prio_si *)(_si + 1);
85 int prio = q->fs->fs.par[0];
87 if (test_bit(prio, &si->bitmap) == 0) {
88 /* No queue with this priority, insert */
89 __set_bit(prio, &si->bitmap);
90 si->q_array[prio] = q;
91 } else { /* use the existing queue */
92 q = si->q_array[prio];
94 if (dn_enqueue(q, m, 0))
100 * Packets are dequeued only from the highest priority queue.
101 * The function ffs() return the lowest bit in the bitmap that rapresent
102 * the array index (-1) which contains the pointer to the highest priority
104 * After the dequeue, if this queue become empty, it is index is removed
106 * Scheduler is idle if the bitmap is empty
108 * NOTE: highest priority is 0, lowest is sched->max_prio_q
111 prio_dequeue(struct dn_sch_inst *_si)
113 struct prio_si *si = (struct prio_si *)(_si + 1);
118 if (si->bitmap == 0) /* scheduler idle */
121 prio = ffs(si->bitmap) - 1;
123 /* Take the highest priority queue in the scheduler */
124 q = si->q_array[prio];
128 if (q->mq.head == NULL) {
129 /* Queue is now empty, remove from scheduler
132 si->q_array[prio] = NULL;
133 __clear_bit(prio, &si->bitmap);
139 prio_new_sched(struct dn_sch_inst *_si)
141 struct prio_si *si = (struct prio_si *)(_si + 1);
143 bzero(si->q_array, sizeof(si->q_array));
150 prio_new_fsk(struct dn_fsk *fs)
152 /* Check if the prioritiy is between 0 and MAXPRIO-1 */
153 ipdn_bound_var(&fs->fs.par[0], 0, 0, MAXPRIO - 1, "PRIO priority");
158 prio_new_queue(struct dn_queue *q)
160 struct prio_si *si = (struct prio_si *)(q->_si + 1);
161 int prio = q->fs->fs.par[0];
162 struct dn_queue *oldq;
164 q->ni.oid.subtype = DN_SCHED_PRIO;
166 if (q->mq.head == NULL)
169 /* Queue already full, must insert in the scheduler or append
170 * mbufs to existing queue. This partly duplicates prio_enqueue
172 if (test_bit(prio, &si->bitmap) == 0) {
173 /* No queue with this priority, insert */
174 __set_bit(prio, &si->bitmap);
175 si->q_array[prio] = q;
176 } else if ( (oldq = si->q_array[prio]) != q) {
177 /* must append to the existing queue.
178 * can simply append q->mq.head to q2->...
179 * and add the counters to those of q2
181 oldq->mq.tail->m_nextpkt = q->mq.head;
182 oldq->mq.tail = q->mq.tail;
183 oldq->ni.length += q->ni.length;
185 oldq->ni.len_bytes += q->ni.len_bytes;
187 q->mq.tail = q->mq.head = NULL;
193 prio_free_queue(struct dn_queue *q)
195 int prio = q->fs->fs.par[0];
196 struct prio_si *si = (struct prio_si *)(q->_si + 1);
198 if (si->q_array[prio] == q) {
199 si->q_array[prio] = NULL;
200 __clear_bit(prio, &si->bitmap);
206 static struct dn_alg prio_desc = {
207 _SI( .type = ) DN_SCHED_PRIO,
208 _SI( .name = ) "PRIO",
209 _SI( .flags = ) DN_MULTIQUEUE,
211 /* we need extra space in the si and the queue */
212 _SI( .schk_datalen = ) 0,
213 _SI( .si_datalen = ) sizeof(struct prio_si),
214 _SI( .q_datalen = ) 0,
216 _SI( .enqueue = ) prio_enqueue,
217 _SI( .dequeue = ) prio_dequeue,
219 _SI( .config = ) NULL,
220 _SI( .destroy = ) NULL,
221 _SI( .new_sched = ) prio_new_sched,
222 _SI( .free_sched = ) NULL,
224 _SI( .new_fsk = ) prio_new_fsk,
225 _SI( .free_fsk = ) NULL,
227 _SI( .new_queue = ) prio_new_queue,
228 _SI( .free_queue = ) prio_free_queue,
230 _SI( .getconfig = ) NULL,
235 DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc);