xref: /freebsd/sys/netpfil/ipfw/dn_sched_prio.c (revision 0f7f3352c8bc463607912e2463d13e52d44a4cae)
1 /*
2  * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * $FreeBSD$
29  */
30 #ifdef _KERNEL
31 #include <sys/malloc.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/kernel.h>
35 #include <sys/mbuf.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>
44 #ifdef NEW_AQM
45 #include <netpfil/ipfw/dn_aqm.h>
46 #endif
47 #include <netpfil/ipfw/dn_sched.h>
48 #else
49 #include <dn_test.h>
50 #endif
51 
52 #define DN_SCHED_PRIO	5 //XXX
53 
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))
58 #endif
59 
60 #ifdef __MIPSEL__
61 #define __clear_bit(ix, pData)	(*pData) &= ~(1<<(ix))
62 #endif
63 
64 /* Size of the array of queues pointers. */
65 #define BITMAP_T	unsigned long
66 #define MAXPRIO		(sizeof(BITMAP_T) * 8)
67 
68 /*
69  * The scheduler instance contains an array of pointers to queues,
70  * one for each priority, and a bitmap listing backlogged queues.
71  */
72 struct prio_si {
73 	BITMAP_T bitmap;			/* array bitmap */
74 	struct dn_queue *q_array[MAXPRIO];	/* Array of queues pointers */
75 };
76 
77 /*
78  * If a queue with the same priority is already backlogged, use
79  * that one instead of the queue passed as argument.
80  */
81 static int
82 prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
83 {
84 	struct prio_si *si = (struct prio_si *)(_si + 1);
85 	int prio = q->fs->fs.par[0];
86 
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];
93 	}
94 	if (dn_enqueue(q, m, 0))
95 		return 1;
96 	return 0;
97 }
98 
99 /*
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
103  * queue.
104  * After the dequeue, if this queue become empty, it is index is removed
105  * from the bitmap.
106  * Scheduler is idle if the bitmap is empty
107  *
108  * NOTE: highest priority is 0, lowest is sched->max_prio_q
109  */
110 static struct mbuf *
111 prio_dequeue(struct dn_sch_inst *_si)
112 {
113 	struct prio_si *si = (struct prio_si *)(_si + 1);
114 	struct mbuf *m;
115 	struct dn_queue *q;
116 	int prio;
117 
118 	if (si->bitmap == 0) /* scheduler idle */
119 		return NULL;
120 
121 	prio = ffs(si->bitmap) - 1;
122 
123 	/* Take the highest priority queue in the scheduler */
124 	q = si->q_array[prio];
125 	// assert(q)
126 
127 	m = dn_dequeue(q);
128 	if (q->mq.head == NULL) {
129 		/* Queue is now empty, remove from scheduler
130 		 * and mark it
131 		 */
132 		si->q_array[prio] = NULL;
133 		__clear_bit(prio, &si->bitmap);
134 	}
135 	return m;
136 }
137 
138 static int
139 prio_new_sched(struct dn_sch_inst *_si)
140 {
141 	struct prio_si *si = (struct prio_si *)(_si + 1);
142 
143 	bzero(si->q_array, sizeof(si->q_array));
144 	si->bitmap = 0;
145 
146 	return 0;
147 }
148 
149 static int
150 prio_new_fsk(struct dn_fsk *fs)
151 {
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");
154 	return 0;
155 }
156 
157 static int
158 prio_new_queue(struct dn_queue *q)
159 {
160 	struct prio_si *si = (struct prio_si *)(q->_si + 1);
161 	int prio = q->fs->fs.par[0];
162 	struct dn_queue *oldq;
163 
164 	q->ni.oid.subtype = DN_SCHED_PRIO;
165 
166 	if (q->mq.head == NULL)
167 		return 0;
168 
169 	/* Queue already full, must insert in the scheduler or append
170 	 * mbufs to existing queue. This partly duplicates prio_enqueue
171 	 */
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
180 		 */
181 		oldq->mq.tail->m_nextpkt = q->mq.head;
182 		oldq->mq.tail = q->mq.tail;
183 		oldq->ni.length += q->ni.length;
184 		q->ni.length = 0;
185 		oldq->ni.len_bytes += q->ni.len_bytes;
186 		q->ni.len_bytes = 0;
187 		q->mq.tail = q->mq.head = NULL;
188 	}
189 	return 0;
190 }
191 
192 static int
193 prio_free_queue(struct dn_queue *q)
194 {
195 	int prio = q->fs->fs.par[0];
196 	struct prio_si *si = (struct prio_si *)(q->_si + 1);
197 
198 	if (si->q_array[prio] == q) {
199 		si->q_array[prio] = NULL;
200 		__clear_bit(prio, &si->bitmap);
201 	}
202 	return 0;
203 }
204 
205 
206 static struct dn_alg prio_desc = {
207 	_SI( .type = ) DN_SCHED_PRIO,
208 	_SI( .name = ) "PRIO",
209 	_SI( .flags = ) DN_MULTIQUEUE,
210 
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,
215 
216 	_SI( .enqueue = ) prio_enqueue,
217 	_SI( .dequeue = ) prio_dequeue,
218 
219 	_SI( .config = )  NULL,
220 	_SI( .destroy = )  NULL,
221 	_SI( .new_sched = ) prio_new_sched,
222 	_SI( .free_sched = ) NULL,
223 
224 	_SI( .new_fsk = ) prio_new_fsk,
225 	_SI( .free_fsk = )  NULL,
226 
227 	_SI( .new_queue = ) prio_new_queue,
228 	_SI( .free_queue = ) prio_free_queue,
229 #ifdef NEW_AQM
230 	_SI( .getconfig = )  NULL,
231 #endif
232 };
233 
234 
235 DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc);
236