xref: /freebsd/sys/netpfil/ipfw/dn_sched_fifo.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1fe267a55SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa
53b3a8eb9SGleb Smirnoff  * All rights reserved
63b3a8eb9SGleb Smirnoff  *
73b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
83b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
93b3a8eb9SGleb Smirnoff  * are met:
103b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
113b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
123b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
133b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
143b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
153b3a8eb9SGleb Smirnoff  *
163b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
173b3a8eb9SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193b3a8eb9SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
203b3a8eb9SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
213b3a8eb9SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
223b3a8eb9SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
233b3a8eb9SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
243b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253b3a8eb9SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263b3a8eb9SGleb Smirnoff  * SUCH DAMAGE.
273b3a8eb9SGleb Smirnoff  */
283b3a8eb9SGleb Smirnoff 
293b3a8eb9SGleb Smirnoff /*
303b3a8eb9SGleb Smirnoff  */
313b3a8eb9SGleb Smirnoff 
323b3a8eb9SGleb Smirnoff #ifdef _KERNEL
333b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
343b3a8eb9SGleb Smirnoff #include <sys/socket.h>
353b3a8eb9SGleb Smirnoff #include <sys/socketvar.h>
363b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
374001fcbeSDon Lewis #include <sys/lock.h>
383b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
393b3a8eb9SGleb Smirnoff #include <sys/module.h>
404001fcbeSDon Lewis #include <sys/rwlock.h>
413b3a8eb9SGleb Smirnoff #include <net/if.h>	/* IFNAMSIZ */
423b3a8eb9SGleb Smirnoff #include <netinet/in.h>
433b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h>		/* ipfw_rule_ref */
443b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h>	/* flow_id */
453b3a8eb9SGleb Smirnoff #include <netinet/ip_dummynet.h>
464001fcbeSDon Lewis #include <netpfil/ipfw/ip_fw_private.h>
473b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_heap.h>
483b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_dn_private.h>
4991336b40SDon Lewis #ifdef NEW_AQM
5091336b40SDon Lewis #include <netpfil/ipfw/dn_aqm.h>
5191336b40SDon Lewis #endif
523b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/dn_sched.h>
533b3a8eb9SGleb Smirnoff #else
543b3a8eb9SGleb Smirnoff #include <dn_test.h>
553b3a8eb9SGleb Smirnoff #endif
563b3a8eb9SGleb Smirnoff 
573b3a8eb9SGleb Smirnoff /*
583b3a8eb9SGleb Smirnoff  * This file implements a FIFO scheduler for a single queue.
593b3a8eb9SGleb Smirnoff  * The queue is allocated as part of the scheduler instance,
603b3a8eb9SGleb Smirnoff  * and there is a single flowset is in the template which stores
613b3a8eb9SGleb Smirnoff  * queue size and policy.
623b3a8eb9SGleb Smirnoff  * Enqueue and dequeue use the default library functions.
633b3a8eb9SGleb Smirnoff  */
643b3a8eb9SGleb Smirnoff static int
fifo_enqueue(struct dn_sch_inst * si,struct dn_queue * q,struct mbuf * m)653b3a8eb9SGleb Smirnoff fifo_enqueue(struct dn_sch_inst *si, struct dn_queue *q, struct mbuf *m)
663b3a8eb9SGleb Smirnoff {
673b3a8eb9SGleb Smirnoff 	/* XXX if called with q != NULL and m=NULL, this is a
683b3a8eb9SGleb Smirnoff 	 * re-enqueue from an existing scheduler, which we should
693b3a8eb9SGleb Smirnoff 	 * handle.
703b3a8eb9SGleb Smirnoff 	 */
71e72cd9a7SLuigi Rizzo 	(void)q;
723b3a8eb9SGleb Smirnoff 	return dn_enqueue((struct dn_queue *)(si+1), m, 0);
733b3a8eb9SGleb Smirnoff }
743b3a8eb9SGleb Smirnoff 
753b3a8eb9SGleb Smirnoff static struct mbuf *
fifo_dequeue(struct dn_sch_inst * si)763b3a8eb9SGleb Smirnoff fifo_dequeue(struct dn_sch_inst *si)
773b3a8eb9SGleb Smirnoff {
783b3a8eb9SGleb Smirnoff 	return dn_dequeue((struct dn_queue *)(si + 1));
793b3a8eb9SGleb Smirnoff }
803b3a8eb9SGleb Smirnoff 
813b3a8eb9SGleb Smirnoff static int
fifo_new_sched(struct dn_sch_inst * si)823b3a8eb9SGleb Smirnoff fifo_new_sched(struct dn_sch_inst *si)
833b3a8eb9SGleb Smirnoff {
843b3a8eb9SGleb Smirnoff 	/* This scheduler instance contains the queue */
853b3a8eb9SGleb Smirnoff 	struct dn_queue *q = (struct dn_queue *)(si + 1);
863b3a8eb9SGleb Smirnoff 
873b3a8eb9SGleb Smirnoff         set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
883b3a8eb9SGleb Smirnoff 	q->_si = si;
893b3a8eb9SGleb Smirnoff 	q->fs = si->sched->fs;
903b3a8eb9SGleb Smirnoff 	return 0;
913b3a8eb9SGleb Smirnoff }
923b3a8eb9SGleb Smirnoff 
933b3a8eb9SGleb Smirnoff static int
fifo_free_sched(struct dn_sch_inst * si)943b3a8eb9SGleb Smirnoff fifo_free_sched(struct dn_sch_inst *si)
953b3a8eb9SGleb Smirnoff {
963b3a8eb9SGleb Smirnoff 	struct dn_queue *q = (struct dn_queue *)(si + 1);
973b3a8eb9SGleb Smirnoff 	dn_free_pkts(q->mq.head);
983b3a8eb9SGleb Smirnoff 	bzero(q, sizeof(*q));
993b3a8eb9SGleb Smirnoff 	return 0;
1003b3a8eb9SGleb Smirnoff }
1013b3a8eb9SGleb Smirnoff 
1023b3a8eb9SGleb Smirnoff /*
1033b3a8eb9SGleb Smirnoff  * FIFO scheduler descriptor
1043b3a8eb9SGleb Smirnoff  * contains the type of the scheduler, the name, the size of extra
1053b3a8eb9SGleb Smirnoff  * data structures, and function pointers.
1063b3a8eb9SGleb Smirnoff  */
1073b3a8eb9SGleb Smirnoff static struct dn_alg fifo_desc = {
1083b3a8eb9SGleb Smirnoff 	_SI( .type = )  DN_SCHED_FIFO,
1093b3a8eb9SGleb Smirnoff 	_SI( .name = )  "FIFO",
1103b3a8eb9SGleb Smirnoff 	_SI( .flags = ) 0,
1113b3a8eb9SGleb Smirnoff 
1123b3a8eb9SGleb Smirnoff 	_SI( .schk_datalen = ) 0,
1133b3a8eb9SGleb Smirnoff 	_SI( .si_datalen = )  sizeof(struct dn_queue),
1143b3a8eb9SGleb Smirnoff 	_SI( .q_datalen = )  0,
1153b3a8eb9SGleb Smirnoff 
1163b3a8eb9SGleb Smirnoff 	_SI( .enqueue = )  fifo_enqueue,
1173b3a8eb9SGleb Smirnoff 	_SI( .dequeue = )  fifo_dequeue,
1183b3a8eb9SGleb Smirnoff 	_SI( .config = )  NULL,
1193b3a8eb9SGleb Smirnoff 	_SI( .destroy = )  NULL,
1203b3a8eb9SGleb Smirnoff 	_SI( .new_sched = )  fifo_new_sched,
1213b3a8eb9SGleb Smirnoff 	_SI( .free_sched = )  fifo_free_sched,
1223b3a8eb9SGleb Smirnoff 	_SI( .new_fsk = )  NULL,
1233b3a8eb9SGleb Smirnoff 	_SI( .free_fsk = )  NULL,
1243b3a8eb9SGleb Smirnoff 	_SI( .new_queue = )  NULL,
1253b3a8eb9SGleb Smirnoff 	_SI( .free_queue = )  NULL,
12691336b40SDon Lewis #ifdef NEW_AQM
12791336b40SDon Lewis 	_SI( .getconfig = )  NULL,
12891336b40SDon Lewis #endif
1293b3a8eb9SGleb Smirnoff };
1303b3a8eb9SGleb Smirnoff 
1313b3a8eb9SGleb Smirnoff DECLARE_DNSCHED_MODULE(dn_fifo, &fifo_desc);
132