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#include <netpfil/ipfw/dn_sched.h> 45#else 46#include <dn_test.h> 47#endif 48 49#define DN_SCHED_PRIO 5 //XXX 50 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)) 55#endif 56 57#ifdef __MIPSEL__ 58#define __clear_bit(ix, pData) (*pData) &= ~(1<<(ix)) 59#endif 60 61/* Size of the array of queues pointers. */ 62#define BITMAP_T unsigned long 63#define MAXPRIO (sizeof(BITMAP_T) * 8) 64 65/* 66 * The scheduler instance contains an array of pointers to queues, 67 * one for each priority, and a bitmap listing backlogged queues. 68 */ 69struct prio_si { 70 BITMAP_T bitmap; /* array bitmap */ 71 struct dn_queue *q_array[MAXPRIO]; /* Array of queues pointers */ 72}; 73 74/* 75 * If a queue with the same priority is already backlogged, use 76 * that one instead of the queue passed as argument. 77 */ 78static int 79prio_enqueue(struct dn_sch_inst *_si, struct dn_queue *q, struct mbuf *m) 80{ 81 struct prio_si *si = (struct prio_si *)(_si + 1); 82 int prio = q->fs->fs.par[0]; 83 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]; 90 } 91 if (dn_enqueue(q, m, 0)) 92 return 1; 93 return 0; 94} 95 96/* 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 100 * queue. 101 * After the dequeue, if this queue become empty, it is index is removed 102 * from the bitmap. 103 * Scheduler is idle if the bitmap is empty 104 * 105 * NOTE: highest priority is 0, lowest is sched->max_prio_q 106 */ 107static struct mbuf * 108prio_dequeue(struct dn_sch_inst *_si) 109{ 110 struct prio_si *si = (struct prio_si *)(_si + 1); 111 struct mbuf *m; 112 struct dn_queue *q; 113 int prio; 114 115 if (si->bitmap == 0) /* scheduler idle */ 116 return NULL; 117 118 prio = ffs(si->bitmap) - 1; 119 120 /* Take the highest priority queue in the scheduler */ 121 q = si->q_array[prio]; 122 // assert(q) 123 124 m = dn_dequeue(q); 125 if (q->mq.head == NULL) { 126 /* Queue is now empty, remove from scheduler 127 * and mark it 128 */ 129 si->q_array[prio] = NULL; 130 __clear_bit(prio, &si->bitmap); 131 } 132 return m; 133} 134 135static int 136prio_new_sched(struct dn_sch_inst *_si) 137{ 138 struct prio_si *si = (struct prio_si *)(_si + 1); 139 140 bzero(si->q_array, sizeof(si->q_array)); 141 si->bitmap = 0; 142 143 return 0; 144} 145 146static int 147prio_new_fsk(struct dn_fsk *fs) 148{ 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"); 151 return 0; 152} 153 154static int 155prio_new_queue(struct dn_queue *q) 156{ 157 struct prio_si *si = (struct prio_si *)(q->_si + 1); 158 int prio = q->fs->fs.par[0]; 159 struct dn_queue *oldq; 160 161 q->ni.oid.subtype = DN_SCHED_PRIO; 162 163 if (q->mq.head == NULL) 164 return 0; 165 166 /* Queue already full, must insert in the scheduler or append 167 * mbufs to existing queue. This partly duplicates prio_enqueue 168 */ 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 177 */ 178 oldq->mq.tail->m_nextpkt = q->mq.head; 179 oldq->mq.tail = q->mq.tail; 180 oldq->ni.length += q->ni.length; 181 q->ni.length = 0; 182 oldq->ni.len_bytes += q->ni.len_bytes; 183 q->ni.len_bytes = 0; 184 q->mq.tail = q->mq.head = NULL; 185 } 186 return 0; 187} 188 189static int 190prio_free_queue(struct dn_queue *q) 191{ 192 int prio = q->fs->fs.par[0]; 193 struct prio_si *si = (struct prio_si *)(q->_si + 1); 194 195 if (si->q_array[prio] == q) { 196 si->q_array[prio] = NULL; 197 __clear_bit(prio, &si->bitmap); 198 } 199 return 0; 200} 201 202 203static struct dn_alg prio_desc = { 204 _SI( .type = ) DN_SCHED_PRIO, 205 _SI( .name = ) "PRIO", 206 _SI( .flags = ) DN_MULTIQUEUE, 207 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, 212 213 _SI( .enqueue = ) prio_enqueue, 214 _SI( .dequeue = ) prio_dequeue, 215 216 _SI( .config = ) NULL, 217 _SI( .destroy = ) NULL, 218 _SI( .new_sched = ) prio_new_sched, 219 _SI( .free_sched = ) NULL, 220 221 _SI( .new_fsk = ) prio_new_fsk, 222 _SI( .free_fsk = ) NULL, 223 224 _SI( .new_queue = ) prio_new_queue, 225 _SI( .free_queue = ) prio_free_queue, 226}; 227 228 229DECLARE_DNSCHED_MODULE(dn_prio, &prio_desc); 230