xref: /freebsd/sys/netpfil/ipfw/dn_aqm.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1 /*-
2  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
3  *  Swinburne University of Technology, Melbourne, Australia.
4  * Portions of this code were made possible in part by a gift from
5  *  The Comcast Innovation Fund.
6  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * API for writing an Active Queue Management algorithm for Dummynet
32  */
33 
34 #ifndef _IP_DN_AQM_H
35 #define _IP_DN_AQM_H
36 
37 #include <sys/ck.h>
38 
39 /* NOW is the current time in millisecond*/
40 #define NOW ((V_dn_cfg.curr_time * tick) / 1000)
41 
42 #define AQM_UNOW (V_dn_cfg.curr_time * tick)
43 #define AQM_TIME_1US ((aqm_time_t)(1))
44 #define AQM_TIME_1MS ((aqm_time_t)(1000))
45 #define AQM_TIME_1S ((aqm_time_t)(AQM_TIME_1MS * 1000))
46 
47 /* aqm time allows to store up to 4294 seconds */
48 typedef uint32_t aqm_time_t;
49 typedef int32_t aqm_stime_t;
50 
51 #define DN_AQM_MTAG_TS 55345
52 
53 /* Macro for variable bounding */
54 #define BOUND_VAR(x,l,h)  ((x) > (h)? (h) : ((x) > (l)? (x) : (l)))
55 
56 /*
57  * Structure for holding data and function pointers that together represent a
58  * AQM algorithm.
59  */
60  struct dn_aqm {
61 #define DN_AQM_NAME_MAX 50
62 	char			name[DN_AQM_NAME_MAX];	/* name of AQM algorithm */
63 	uint32_t	type;	/* AQM type number */
64 
65 	/* Methods implemented by AQM algorithm:
66 	 *
67 	 * enqueue	enqueue packet 'm' on queue 'q'.
68 	 * 	Return 0 on success, 1 on drop.
69 	 *
70 	 * dequeue	dequeue a packet from queue 'q'.
71 	 * 	Return a packet, NULL if no packet available.
72 	 *
73 	 * config	configure AQM algorithm
74 	 * If required, this function should allocate space to store
75 	 * the configurations and set 'fs->aqmcfg' to point to this space.
76 	 * 'dn_extra_parms' includes array of parameters send
77 	 * from ipfw userland command.
78 	 * 	Return 0 on success, non-zero otherwise.
79 	 *
80 	 * deconfig	deconfigure AQM algorithm.
81 	 * The allocated configuration memory space should be freed here.
82 	 * 	Return 0 on success, non-zero otherwise.
83 	 *
84 	 * init	initialise AQM status variables of queue 'q'
85 	 * This function is used to allocate space and init AQM status for a
86 	 * queue and q->aqm_status to point to this space.
87 	 * 	Return 0 on success, non-zero otherwise.
88 	 *
89 	 * cleanup	cleanup AQM status variables of queue 'q'
90 	 * The allocated memory space for AQM status should be freed here.
91 	 * 	Return 0 on success, non-zero otherwise.
92 	 *
93 	 * getconfig	retrieve AQM configurations
94 	 * This function is used to return AQM parameters to userland
95 	 * command. The function should fill 'dn_extra_parms' struct with
96 	 * the AQM configurations using 'par' array.
97 	 *
98 	 */
99 
100 	int (*enqueue)(struct dn_queue *, struct mbuf *);
101 	struct mbuf * (*dequeue)(struct dn_queue *);
102 	int (*config)(struct dn_fsk *, struct dn_extra_parms *ep, int);
103 	int (*deconfig)(struct dn_fsk *);
104 	int (*init)(struct dn_queue *);
105 	int (*cleanup)(struct dn_queue *);
106 	int (*getconfig)(struct dn_fsk *, struct dn_extra_parms *);
107 
108 	int	ref_count; /*Number of queues instances in the system */
109 	int	cfg_ref_count;	/*Number of AQM instances in the system */
110 	CK_LIST_ENTRY(dn_aqm) next; /* Next AQM in the list */
111 };
112 
113 /* Helper function to update queue and scheduler statistics.
114  * negative len + drop -> drop
115  * negative len -> dequeue
116  * positive len -> enqueue
117  * positive len + drop -> drop during enqueue
118  */
119 __inline static void
update_stats(struct dn_queue * q,int len,int drop)120 update_stats(struct dn_queue *q, int len, int drop)
121 {
122 	int inc = 0;
123 	struct dn_flow *sni;
124 	struct dn_flow *qni;
125 
126 	sni = &q->_si->ni;
127 	qni = &q->ni;
128 
129 	if (len < 0)
130 			inc = -1;
131 	else if(len > 0)
132 			inc = 1;
133 
134 	if (drop) {
135 			qni->drops++;
136 			sni->drops++;
137 			V_dn_cfg.io_pkt_drop++;
138 	} else {
139 		/*update queue stats */
140 		qni->length += inc;
141 		qni->len_bytes += len;
142 
143 		/*update scheduler instance stats */
144 		sni->length += inc;
145 		sni->len_bytes += len;
146 	}
147 	/* tot_pkts  is updated in dn_enqueue function */
148 }
149 
150 /* kernel module related function */
151 int
152 dn_aqm_modevent(module_t mod, int cmd, void *arg);
153 
154 #define DECLARE_DNAQM_MODULE(name, dnaqm)			\
155 	static moduledata_t name##_mod = {			\
156 		#name, dn_aqm_modevent, dnaqm		\
157 	};							\
158 	DECLARE_MODULE(name, name##_mod, 			\
159 		SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 	\
160         MODULE_DEPEND(name, dummynet, 3, 3, 3)
161 
162 #endif
163