1204591Sluigi/*
2204591Sluigi * Copyright (c) 2010 Riccardo Panicucci, Universita` di Pisa
3204591Sluigi * All rights reserved
4204591Sluigi *
5204591Sluigi * Redistribution and use in source and binary forms, with or without
6204591Sluigi * modification, are permitted provided that the following conditions
7204591Sluigi * are met:
8204591Sluigi * 1. Redistributions of source code must retain the above copyright
9204591Sluigi *    notice, this list of conditions and the following disclaimer.
10204591Sluigi * 2. Redistributions in binary form must reproduce the above copyright
11204591Sluigi *    notice, this list of conditions and the following disclaimer in the
12204591Sluigi *    documentation and/or other materials provided with the distribution.
13204591Sluigi *
14204591Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15204591Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16204591Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17204591Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18204591Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19204591Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20204591Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21204591Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22204591Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23204591Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24204591Sluigi * SUCH DAMAGE.
25204591Sluigi */
26204591Sluigi
27204591Sluigi/*
28204591Sluigi * $FreeBSD$
29204591Sluigi */
30204591Sluigi
31204591Sluigi#ifdef _KERNEL
32204591Sluigi#include <sys/malloc.h>
33204591Sluigi#include <sys/socket.h>
34204591Sluigi#include <sys/socketvar.h>
35204591Sluigi#include <sys/kernel.h>
36204591Sluigi#include <sys/mbuf.h>
37204591Sluigi#include <sys/module.h>
38204591Sluigi#include <net/if.h>	/* IFNAMSIZ */
39204591Sluigi#include <netinet/in.h>
40204591Sluigi#include <netinet/ip_var.h>		/* ipfw_rule_ref */
41204591Sluigi#include <netinet/ip_fw.h>	/* flow_id */
42204591Sluigi#include <netinet/ip_dummynet.h>
43240494Sglebius#include <netpfil/ipfw/dn_heap.h>
44240494Sglebius#include <netpfil/ipfw/ip_dn_private.h>
45240494Sglebius#include <netpfil/ipfw/dn_sched.h>
46204591Sluigi#else
47204591Sluigi#include <dn_test.h>
48204591Sluigi#endif
49204591Sluigi
50204591Sluigi/*
51204591Sluigi * This file implements a FIFO scheduler for a single queue.
52204591Sluigi * The queue is allocated as part of the scheduler instance,
53204591Sluigi * and there is a single flowset is in the template which stores
54204591Sluigi * queue size and policy.
55204591Sluigi * Enqueue and dequeue use the default library functions.
56204591Sluigi */
57204591Sluigistatic int
58204591Sluigififo_enqueue(struct dn_sch_inst *si, struct dn_queue *q, struct mbuf *m)
59204591Sluigi{
60204591Sluigi	/* XXX if called with q != NULL and m=NULL, this is a
61204591Sluigi	 * re-enqueue from an existing scheduler, which we should
62204591Sluigi	 * handle.
63204591Sluigi	 */
64204591Sluigi	return dn_enqueue((struct dn_queue *)(si+1), m, 0);
65204591Sluigi}
66204591Sluigi
67204591Sluigistatic struct mbuf *
68204591Sluigififo_dequeue(struct dn_sch_inst *si)
69204591Sluigi{
70204591Sluigi	return dn_dequeue((struct dn_queue *)(si + 1));
71204591Sluigi}
72204591Sluigi
73204591Sluigistatic int
74204591Sluigififo_new_sched(struct dn_sch_inst *si)
75204591Sluigi{
76204591Sluigi	/* This scheduler instance contains the queue */
77204591Sluigi	struct dn_queue *q = (struct dn_queue *)(si + 1);
78204591Sluigi
79204591Sluigi        set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
80204591Sluigi	q->_si = si;
81204591Sluigi	q->fs = si->sched->fs;
82204591Sluigi	return 0;
83204591Sluigi}
84204591Sluigi
85204591Sluigistatic int
86204591Sluigififo_free_sched(struct dn_sch_inst *si)
87204591Sluigi{
88204591Sluigi	struct dn_queue *q = (struct dn_queue *)(si + 1);
89204591Sluigi	dn_free_pkts(q->mq.head);
90204591Sluigi	bzero(q, sizeof(*q));
91204591Sluigi	return 0;
92204591Sluigi}
93204591Sluigi
94204591Sluigi/*
95204591Sluigi * FIFO scheduler descriptor
96204591Sluigi * contains the type of the scheduler, the name, the size of extra
97204591Sluigi * data structures, and function pointers.
98204591Sluigi */
99204591Sluigistatic struct dn_alg fifo_desc = {
100204591Sluigi	_SI( .type = )  DN_SCHED_FIFO,
101204591Sluigi	_SI( .name = )  "FIFO",
102204591Sluigi	_SI( .flags = ) 0,
103204591Sluigi
104204591Sluigi	_SI( .schk_datalen = ) 0,
105204591Sluigi	_SI( .si_datalen = )  sizeof(struct dn_queue),
106204591Sluigi	_SI( .q_datalen = )  0,
107204591Sluigi
108204591Sluigi	_SI( .enqueue = )  fifo_enqueue,
109204591Sluigi	_SI( .dequeue = )  fifo_dequeue,
110204591Sluigi	_SI( .config = )  NULL,
111204591Sluigi	_SI( .destroy = )  NULL,
112204591Sluigi	_SI( .new_sched = )  fifo_new_sched,
113204591Sluigi	_SI( .free_sched = )  fifo_free_sched,
114204591Sluigi	_SI( .new_fsk = )  NULL,
115204591Sluigi	_SI( .free_fsk = )  NULL,
116204591Sluigi	_SI( .new_queue = )  NULL,
117204591Sluigi	_SI( .free_queue = )  NULL,
118204591Sluigi};
119204591Sluigi
120204591SluigiDECLARE_DNSCHED_MODULE(dn_fifo, &fifo_desc);
121