xref: /freebsd/sys/netpfil/ipfw/dn_sched_fifo.c (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa
5  * All rights reserved
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * $FreeBSD$
31  */
32 
33 #ifdef _KERNEL
34 #include <sys/malloc.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mbuf.h>
40 #include <sys/module.h>
41 #include <sys/rwlock.h>
42 #include <net/if.h>	/* IFNAMSIZ */
43 #include <netinet/in.h>
44 #include <netinet/ip_var.h>		/* ipfw_rule_ref */
45 #include <netinet/ip_fw.h>	/* flow_id */
46 #include <netinet/ip_dummynet.h>
47 #include <netpfil/ipfw/ip_fw_private.h>
48 #include <netpfil/ipfw/dn_heap.h>
49 #include <netpfil/ipfw/ip_dn_private.h>
50 #ifdef NEW_AQM
51 #include <netpfil/ipfw/dn_aqm.h>
52 #endif
53 #include <netpfil/ipfw/dn_sched.h>
54 #else
55 #include <dn_test.h>
56 #endif
57 
58 /*
59  * This file implements a FIFO scheduler for a single queue.
60  * The queue is allocated as part of the scheduler instance,
61  * and there is a single flowset is in the template which stores
62  * queue size and policy.
63  * Enqueue and dequeue use the default library functions.
64  */
65 static int
66 fifo_enqueue(struct dn_sch_inst *si, struct dn_queue *q, struct mbuf *m)
67 {
68 	/* XXX if called with q != NULL and m=NULL, this is a
69 	 * re-enqueue from an existing scheduler, which we should
70 	 * handle.
71 	 */
72 	(void)q;
73 	return dn_enqueue((struct dn_queue *)(si+1), m, 0);
74 }
75 
76 static struct mbuf *
77 fifo_dequeue(struct dn_sch_inst *si)
78 {
79 	return dn_dequeue((struct dn_queue *)(si + 1));
80 }
81 
82 static int
83 fifo_new_sched(struct dn_sch_inst *si)
84 {
85 	/* This scheduler instance contains the queue */
86 	struct dn_queue *q = (struct dn_queue *)(si + 1);
87 
88         set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
89 	q->_si = si;
90 	q->fs = si->sched->fs;
91 	return 0;
92 }
93 
94 static int
95 fifo_free_sched(struct dn_sch_inst *si)
96 {
97 	struct dn_queue *q = (struct dn_queue *)(si + 1);
98 	dn_free_pkts(q->mq.head);
99 	bzero(q, sizeof(*q));
100 	return 0;
101 }
102 
103 /*
104  * FIFO scheduler descriptor
105  * contains the type of the scheduler, the name, the size of extra
106  * data structures, and function pointers.
107  */
108 static struct dn_alg fifo_desc = {
109 	_SI( .type = )  DN_SCHED_FIFO,
110 	_SI( .name = )  "FIFO",
111 	_SI( .flags = ) 0,
112 
113 	_SI( .schk_datalen = ) 0,
114 	_SI( .si_datalen = )  sizeof(struct dn_queue),
115 	_SI( .q_datalen = )  0,
116 
117 	_SI( .enqueue = )  fifo_enqueue,
118 	_SI( .dequeue = )  fifo_dequeue,
119 	_SI( .config = )  NULL,
120 	_SI( .destroy = )  NULL,
121 	_SI( .new_sched = )  fifo_new_sched,
122 	_SI( .free_sched = )  fifo_free_sched,
123 	_SI( .new_fsk = )  NULL,
124 	_SI( .free_fsk = )  NULL,
125 	_SI( .new_queue = )  NULL,
126 	_SI( .free_queue = )  NULL,
127 #ifdef NEW_AQM
128 	_SI( .getconfig = )  NULL,
129 #endif
130 };
131 
132 DECLARE_DNSCHED_MODULE(dn_fifo, &fifo_desc);
133