/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa4* All rights reserved5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29*/30#ifdef _KERNEL31#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_AQM48#include <netpfil/ipfw/dn_aqm.h>49#endif50#include <netpfil/ipfw/dn_sched.h>51#else52#include <dn_test.h>53#endif5455#define DN_SCHED_PRIO 5 //XXX5657#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#endif6263#ifdef __MIPSEL__64#define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix))65#endif6667/* Size of the array of queues pointers. */68#define BITMAP_T unsigned long69#define MAXPRIO (sizeof(BITMAP_T) * 8)7071/*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 {76BITMAP_T bitmap; /* array bitmap */77struct dn_queue *q_array[MAXPRIO]; /* Array of queues pointers */78};7980/*81* If a queue with the same priority is already backlogged, use82* that one instead of the queue passed as argument.83*/84static int85prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m)86{87struct prio_si *si = (struct prio_si *)(_si + 1);88int prio = q->fs->fs.par[0];8990if (test_bit(prio, &si->bitmap) == 0) {91/* No queue with this priority, insert */92__set_bit(prio, &si->bitmap);93si->q_array[prio] = q;94} else { /* use the existing queue */95q = si->q_array[prio];96}97if (dn_enqueue(q, m, 0))98return 1;99return 0;100}101102/*103* Packets are dequeued only from the highest priority queue.104* The function ffs() return the lowest bit in the bitmap that rapresent105* the array index (-1) which contains the pointer to the highest priority106* queue.107* After the dequeue, if this queue become empty, it is index is removed108* from the bitmap.109* Scheduler is idle if the bitmap is empty110*111* NOTE: highest priority is 0, lowest is sched->max_prio_q112*/113static struct mbuf *114prio_dequeue(struct dn_sch_inst *_si)115{116struct prio_si *si = (struct prio_si *)(_si + 1);117struct mbuf *m;118struct dn_queue *q;119int prio;120121if (si->bitmap == 0) /* scheduler idle */122return NULL;123124prio = ffs(si->bitmap) - 1;125126/* Take the highest priority queue in the scheduler */127q = si->q_array[prio];128// assert(q)129130m = dn_dequeue(q);131if (q->mq.head == NULL) {132/* Queue is now empty, remove from scheduler133* and mark it134*/135si->q_array[prio] = NULL;136__clear_bit(prio, &si->bitmap);137}138return m;139}140141static int142prio_new_sched(struct dn_sch_inst *_si)143{144struct prio_si *si = (struct prio_si *)(_si + 1);145146bzero(si->q_array, sizeof(si->q_array));147si->bitmap = 0;148149return 0;150}151152static int153prio_new_fsk(struct dn_fsk *fs)154{155/* Check if the prioritiy is between 0 and MAXPRIO-1 */156ipdn_bound_var(&fs->fs.par[0], 0, 0, MAXPRIO - 1, "PRIO priority");157return 0;158}159160static int161prio_new_queue(struct dn_queue *q)162{163struct prio_si *si = (struct prio_si *)(q->_si + 1);164int prio = q->fs->fs.par[0];165struct dn_queue *oldq;166167q->ni.oid.subtype = DN_SCHED_PRIO;168169if (q->mq.head == NULL)170return 0;171172/* Queue already full, must insert in the scheduler or append173* mbufs to existing queue. This partly duplicates prio_enqueue174*/175if (test_bit(prio, &si->bitmap) == 0) {176/* No queue with this priority, insert */177__set_bit(prio, &si->bitmap);178si->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 q2183*/184oldq->mq.tail->m_nextpkt = q->mq.head;185oldq->mq.tail = q->mq.tail;186oldq->ni.length += q->ni.length;187q->ni.length = 0;188oldq->ni.len_bytes += q->ni.len_bytes;189q->ni.len_bytes = 0;190q->mq.tail = q->mq.head = NULL;191}192return 0;193}194195static int196prio_free_queue(struct dn_queue *q)197{198int prio = q->fs->fs.par[0];199struct prio_si *si = (struct prio_si *)(q->_si + 1);200201if (si->q_array[prio] == q) {202si->q_array[prio] = NULL;203__clear_bit(prio, &si->bitmap);204}205return 0;206}207208static struct dn_alg prio_desc = {209_SI( .type = ) DN_SCHED_PRIO,210_SI( .name = ) "PRIO",211_SI( .flags = ) DN_MULTIQUEUE,212213/* we need extra space in the si and the queue */214_SI( .schk_datalen = ) 0,215_SI( .si_datalen = ) sizeof(struct prio_si),216_SI( .q_datalen = ) 0,217218_SI( .enqueue = ) prio_enqueue,219_SI( .dequeue = ) prio_dequeue,220221_SI( .config = ) NULL,222_SI( .destroy = ) NULL,223_SI( .new_sched = ) prio_new_sched,224_SI( .free_sched = ) NULL,225226_SI( .new_fsk = ) prio_new_fsk,227_SI( .free_fsk = ) NULL,228229_SI( .new_queue = ) prio_new_queue,230_SI( .free_queue = ) prio_free_queue,231#ifdef NEW_AQM232_SI( .getconfig = ) NULL,233#endif234};235236DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc);237238239