summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/netpfil/ipfw/dn_aqm.h
blob: d01e98ebeafa00d4480b3fd1618e0225fa3f4cb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-
 * Copyright (C) 2016 Centre for Advanced Internet Architectures,
 *  Swinburne University of Technology, Melbourne, Australia.
 * Portions of this code were made possible in part by a gift from 
 *  The Comcast Innovation Fund.
 * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * API for writing an Active Queue Management algorithm for Dummynet
 *
 * $FreeBSD$
 */

#ifndef _IP_DN_AQM_H
#define _IP_DN_AQM_H


/* NOW is the current time in millisecond*/
#define NOW ((dn_cfg.curr_time * tick) / 1000)

#define AQM_UNOW (dn_cfg.curr_time * tick)
#define AQM_TIME_1US ((aqm_time_t)(1))
#define AQM_TIME_1MS ((aqm_time_t)(1000))
#define AQM_TIME_1S ((aqm_time_t)(AQM_TIME_1MS * 1000))

/* aqm time allows to store up to 4294 seconds */
typedef uint32_t aqm_time_t;
typedef int32_t aqm_stime_t;

#define DN_AQM_MTAG_TS 55345

/* Macro for variable bounding */
#define BOUND_VAR(x,l,h)  ((x) > (h)? (h) : ((x) > (l)? (x) : (l)))

/* sysctl variable to count number of dropped packets */
extern unsigned long io_pkt_drop; 

/*
 * Structure for holding data and function pointers that together represent a
 * AQM algorithm.
 */
 struct dn_aqm {
#define DN_AQM_NAME_MAX 50
	char			name[DN_AQM_NAME_MAX];	/* name of AQM algorithm */
	uint32_t	type;	/* AQM type number */
	
	/* Methods implemented by AQM algorithm:
	 * 
	 * enqueue	enqueue packet 'm' on queue 'q'.
	 * 	Return 0 on success, 1 on drop.
	 * 
	 * dequeue	dequeue a packet from queue 'q'.
	 * 	Return a packet, NULL if no packet available.
	 * 
	 * config	configure AQM algorithm
	 * If required, this function should allocate space to store 
	 * the configurations and set 'fs->aqmcfg' to point to this space.
	 * 'dn_extra_parms' includes array of parameters send
	 * from ipfw userland command.
	 * 	Return 0 on success, non-zero otherwise.
	 * 
	 * deconfig	deconfigure AQM algorithm.
	 * The allocated configuration memory space should be freed here.
	 * 	Return 0 on success, non-zero otherwise.
	 * 
	 * init	initialise AQM status variables of queue 'q'
	 * This function is used to allocate space and init AQM status for a
	 * queue and q->aqm_status to point to this space.
	 * 	Return 0 on success, non-zero otherwise.
	 * 
	 * cleanup	cleanup AQM status variables of queue 'q'
	 * The allocated memory space for AQM status should be freed here.
	 * 	Return 0 on success, non-zero otherwise.
	 * 
	 * getconfig	retrieve AQM configurations 
	 * This function is used to return AQM parameters to userland
	 * command. The function should fill 'dn_extra_parms' struct with 
	 * the AQM configurations using 'par' array.
	 * 
	 */
	
	int (*enqueue)(struct dn_queue *, struct mbuf *);
	struct mbuf * (*dequeue)(struct dn_queue *);
	int (*config)(struct dn_fsk *, struct dn_extra_parms *ep, int);
	int (*deconfig)(struct dn_fsk *);
	int (*init)(struct dn_queue *);
	int (*cleanup)(struct dn_queue *);
	int (*getconfig)(struct dn_fsk *, struct dn_extra_parms *);

	int	ref_count; /*Number of queues instances in the system */
	int	cfg_ref_count;	/*Number of AQM instances in the system */
	SLIST_ENTRY (dn_aqm) next; /* Next AQM in the list */
};

/* Helper function to update queue and scheduler statistics.
 * negative len + drop -> drop
 * negative len -> dequeue
 * positive len -> enqueue
 * positive len + drop -> drop during enqueue
 */
__inline static void
update_stats(struct dn_queue *q, int len, int drop)
{
	int inc = 0;
	struct dn_flow *sni;
	struct dn_flow *qni;
	
	sni = &q->_si->ni;
	qni = &q->ni;

	if (len < 0)
			inc = -1;
	else if(len > 0)
			inc = 1;

	if (drop) {
			qni->drops++;
			sni->drops++;
			io_pkt_drop++;
	} else {
		/*update queue stats */
		qni->length += inc;
		qni->len_bytes += len;

		/*update scheduler instance stats */
		sni->length += inc;
		sni->len_bytes += len;
	}
	/* tot_pkts  is updated in dn_enqueue function */
}


/* kernel module related function */
int
dn_aqm_modevent(module_t mod, int cmd, void *arg);

#define DECLARE_DNAQM_MODULE(name, dnaqm)			\
	static moduledata_t name##_mod = {			\
		#name, dn_aqm_modevent, dnaqm		\
	};							\
	DECLARE_MODULE(name, name##_mod, 			\
		SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 	\
        MODULE_DEPEND(name, dummynet, 3, 3, 3)

#endif