dn_sched_prio.c revision 325731
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: stable/10/sys/netpfil/ipfw/dn_sched_prio.c 325731 2017-11-12 01:28:20Z truckman $
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/lock.h>
36#include <sys/mbuf.h>
37#include <sys/module.h>
38#include <sys/rwlock.h>
39#include <net/if.h>	/* IFNAMSIZ */
40#include <netinet/in.h>
41#include <netinet/ip_var.h>		/* ipfw_rule_ref */
42#include <netinet/ip_fw.h>	/* flow_id */
43#include <netinet/ip_dummynet.h>
44#include <netpfil/ipfw/ip_fw_private.h>
45#include <netpfil/ipfw/dn_heap.h>
46#include <netpfil/ipfw/ip_dn_private.h>
47#ifdef NEW_AQM
48#include <netpfil/ipfw/dn_aqm.h>
49#endif
50#include <netpfil/ipfw/dn_sched.h>
51#else
52#include <dn_test.h>
53#endif
54
55#define DN_SCHED_PRIO	5 //XXX
56
57#if !defined(_KERNEL) || !defined(__linux__)
58#define test_bit(ix, pData)	((*pData) & (1<<(ix)))
59#define __set_bit(ix, pData)	(*pData) |= (1<<(ix))
60#define __clear_bit(ix, pData)	(*pData) &= ~(1<<(ix))
61#endif
62
63#ifdef __MIPSEL__
64#define __clear_bit(ix, pData)	(*pData) &= ~(1<<(ix))
65#endif
66
67/* Size of the array of queues pointers. */
68#define BITMAP_T	unsigned long
69#define MAXPRIO		(sizeof(BITMAP_T) * 8)
70
71/*
72 * The scheduler instance contains an array of pointers to queues,
73 * one for each priority, and a bitmap listing backlogged queues.
74 */
75struct prio_si {
76	BITMAP_T bitmap;			/* array bitmap */
77	struct dn_queue *q_array[MAXPRIO];	/* Array of queues pointers */
78};
79
80/*
81 * If a queue with the same priority is already backlogged, use
82 * that one instead of the queue passed as argument.
83 */
84static int
85prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)
86{
87	struct prio_si *si = (struct prio_si *)(_si + 1);
88	int prio = q->fs->fs.par[0];
89
90	if (test_bit(prio, &si->bitmap) == 0) {
91		/* No queue with this priority, insert */
92		__set_bit(prio, &si->bitmap);
93		si->q_array[prio] = q;
94	} else { /* use the existing queue */
95		q = si->q_array[prio];
96	}
97	if (dn_enqueue(q, m, 0))
98		return 1;
99	return 0;
100}
101
102/*
103 * Packets are dequeued only from the highest priority queue.
104 * The function ffs() return the lowest bit in the bitmap that rapresent
105 * the array index (-1) which contains the pointer to the highest priority
106 * queue.
107 * After the dequeue, if this queue become empty, it is index is removed
108 * from the bitmap.
109 * Scheduler is idle if the bitmap is empty
110 *
111 * NOTE: highest priority is 0, lowest is sched->max_prio_q
112 */
113static struct mbuf *
114prio_dequeue(struct dn_sch_inst *_si)
115{
116	struct prio_si *si = (struct prio_si *)(_si + 1);
117	struct mbuf *m;
118	struct dn_queue *q;
119	int prio;
120
121	if (si->bitmap == 0) /* scheduler idle */
122		return NULL;
123
124	prio = ffs(si->bitmap) - 1;
125
126	/* Take the highest priority queue in the scheduler */
127	q = si->q_array[prio];
128	// assert(q)
129
130	m = dn_dequeue(q);
131	if (q->mq.head == NULL) {
132		/* Queue is now empty, remove from scheduler
133		 * and mark it
134		 */
135		si->q_array[prio] = NULL;
136		__clear_bit(prio, &si->bitmap);
137	}
138	return m;
139}
140
141static int
142prio_new_sched(struct dn_sch_inst *_si)
143{
144	struct prio_si *si = (struct prio_si *)(_si + 1);
145
146	bzero(si->q_array, sizeof(si->q_array));
147	si->bitmap = 0;
148
149	return 0;
150}
151
152static int
153prio_new_fsk(struct dn_fsk *fs)
154{
155	/* Check if the prioritiy is between 0 and MAXPRIO-1 */
156	ipdn_bound_var(&fs->fs.par[0], 0, 0, MAXPRIO - 1, "PRIO priority");
157	return 0;
158}
159
160static int
161prio_new_queue(struct dn_queue *q)
162{
163	struct prio_si *si = (struct prio_si *)(q->_si + 1);
164	int prio = q->fs->fs.par[0];
165	struct dn_queue *oldq;
166
167	q->ni.oid.subtype = DN_SCHED_PRIO;
168
169	if (q->mq.head == NULL)
170		return 0;
171
172	/* Queue already full, must insert in the scheduler or append
173	 * mbufs to existing queue. This partly duplicates prio_enqueue
174	 */
175	if (test_bit(prio, &si->bitmap) == 0) {
176		/* No queue with this priority, insert */
177		__set_bit(prio, &si->bitmap);
178		si->q_array[prio] = q;
179	} else if ( (oldq = si->q_array[prio]) != q) {
180		/* must append to the existing queue.
181		 * can simply append q->mq.head to q2->...
182		 * and add the counters to those of q2
183		 */
184		oldq->mq.tail->m_nextpkt = q->mq.head;
185		oldq->mq.tail = q->mq.tail;
186		oldq->ni.length += q->ni.length;
187		q->ni.length = 0;
188		oldq->ni.len_bytes += q->ni.len_bytes;
189		q->ni.len_bytes = 0;
190		q->mq.tail = q->mq.head = NULL;
191	}
192	return 0;
193}
194
195static int
196prio_free_queue(struct dn_queue *q)
197{
198	int prio = q->fs->fs.par[0];
199	struct prio_si *si = (struct prio_si *)(q->_si + 1);
200
201	if (si->q_array[prio] == q) {
202		si->q_array[prio] = NULL;
203		__clear_bit(prio, &si->bitmap);
204	}
205	return 0;
206}
207
208
209static struct dn_alg prio_desc = {
210	_SI( .type = ) DN_SCHED_PRIO,
211	_SI( .name = ) "PRIO",
212	_SI( .flags = ) DN_MULTIQUEUE,
213
214	/* we need extra space in the si and the queue */
215	_SI( .schk_datalen = ) 0,
216	_SI( .si_datalen = ) sizeof(struct prio_si),
217	_SI( .q_datalen = ) 0,
218
219	_SI( .enqueue = ) prio_enqueue,
220	_SI( .dequeue = ) prio_dequeue,
221
222	_SI( .config = )  NULL,
223	_SI( .destroy = )  NULL,
224	_SI( .new_sched = ) prio_new_sched,
225	_SI( .free_sched = ) NULL,
226
227	_SI( .new_fsk = ) prio_new_fsk,
228	_SI( .free_fsk = )  NULL,
229
230	_SI( .new_queue = ) prio_new_queue,
231	_SI( .free_queue = ) prio_free_queue,
232#ifdef NEW_AQM
233	_SI( .getconfig = )  NULL,
234#endif
235};
236
237
238DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc);
239