Lots of changes. In no particular order:
[distributedratelimiting.git] / drl / ratetypes.h
1 /* See the DRL-LICENSE file for this file's software license. */
2
3 /* ratetypes.h
4  *
5  * Defines the main types used by DRL.
6  */
7
8 #ifndef _RATETYPES_H_
9 #define _RATETYPES_H_
10
11 #include <inttypes.h>
12
13 #if 0
14 #include "rate_accounting/common_accounting.h"
15 #include "rate_accounting/standard.h"
16 #include "rate_accounting/samplehold.h"
17 #include "rate_accounting/simple.h"
18 #endif
19
20 #include "bsd_queue.h"
21 #include "config.h"
22 #include "drl_state.h"
23 #include "common_accounting.h"
24 #include "standard.h"
25 #include "samplehold.h"
26 #include "simple.h"
27 #include "multipleinterval.h"
28
29
30 /** Represents a DRL entitiy/group. */
31 typedef struct identity {
32
33     /** A unique id for the identity. */
34     uint32_t id;
35
36     /** The global rate limit. */
37     uint32_t limit;
38
39     /** The local rate limit. */
40     uint32_t locallimit;
41     
42     /** Pointer to the identity's parent in the HTB hierarchy. */
43     struct identity *parent;
44
45     /** Array of the leaves that are limited by this identity. Points into the
46      * leaves array for the identity's instance. */
47     struct leaf **leaves;
48
49     /** The number of leaves for which this identity is responsible. */
50     int leaf_count;
51
52     /** The fixed (per second) EWMA weight. */
53     double fixed_ewma_weight;
54     
55     /** The real EWMA weight, based on the fixed weight and estimate interval.*/
56     double ewma_weight;
57
58     /** Used for average rate graph generation. */
59     double avg_bytes;
60
61     /* Communication */
62     
63     /** Communication data for this identity. */
64     comm_t comm;
65
66     /* FPS */
67
68     /** The node in the HTB hierarchy whose limits will be modified by this
69      * identity.  (1:1<htb_node>)*/
70     uint32_t htb_node;
71
72     /** The parent of this node in the HTB hierarchy. (tc requires that the
73      * parent be specified in all calls the modify a node.) */
74     uint32_t htb_parent;
75
76     /** FPS current weight value. */
77     double localweight;
78
79     /** FPS previous weight value. */
80     double last_localweight;
81
82     double total_weight;
83
84     /** A flag to indicate whether or not the identity is in the flowstart
85      * state.  During flowstart, the identity's limit is raised to allow for
86      * flows to grow before incurring losses. */
87     int flowstart;
88
89     /** Keeps track of the state of FPS dampening for this identity. */
90     enum dampenings dampen_state;
91
92     /* GRD */
93
94     /** GRD drop probability information. */
95     double drop_prob;
96     
97     /** GRD previous drop probability information. */
98     double last_drop_prob;
99
100     /* Flow accounting machinery. */
101
102     /** Flow information that is common to all types of tables.
103      * This includes aggregate rates, update times, etc. */
104     common_accounting_t common;
105
106     /** The actual table.  Uses a void pointer because it could be one
107      * of several different types. (standard, sample&hold, etc. ) */
108     void *table;
109
110     /** Protects the table, as it gets updated in two separate threads:
111      * 1) ulogd_DRL.c: the table is updated as new packet information arrives.
112      * 2) estimate.c: the table is used to determine rates, and it's also
113      * periodically cleaned.
114      */
115     pthread_mutex_t table_mutex;
116
117     /* Function pointers to functions to act on the table. */
118
119     /** Function to call for each packet.  Updates the byte count for flows
120      * that are being tracked. */
121     int (*table_sample_function)(void *, const key_flow *);
122
123     /** Function to call on the table when it is periodically cleaned. */
124     int (*table_cleanup_function)(void *);
125
126     /** Function to call once per estimate interval to update the table's
127      * rate estimation. */
128     void (*table_update_function)(void *, struct timeval, double);
129
130     /** Function to call when the table should be destroyed. */
131     void (*table_destroy_function)(void *);
132
133 #ifdef SHADOW_ACCTING
134
135     common_accounting_t shadow_common;
136
137     void *shadow_table;
138
139     double localweight_copy;
140     double last_localweight_copy;
141
142     enum dampenings dampen_state_copy;
143
144 #endif
145
146     /* Scheduling bookkeeping. */
147
148     /** Scheduling unit that tells the limiter when to execute the main loop.*/
149     struct ident_action *loop_action;
150
151     /** Scheduling unit that tells the limiter when to communicate.*/
152     struct ident_action *comm_action;
153
154     /** The number of limiter ticks that should pass before this identity should
155      * be scheduled to execute its main estimate/allocate/enforce loop. */
156     uint32_t mainloop_intervals; 
157
158     /** The number of limiter ticks that should pass before this identity should
159      * be scheduled for communication. */
160     uint32_t communication_intervals;
161
162 } identity_t;
163
164 enum identity_actions { ACTION_MAINLOOP = 1, ACTION_COMMUNICATE = 2 };
165
166 typedef struct ident_action {
167     struct identity *ident;
168     enum identity_actions action;
169     int valid;
170
171     TAILQ_ENTRY(ident_action) calendar;
172 } identity_action;
173
174 /**
175  * Represents the bottom most entity in the HTB hierarchy.  For PlanetLab,
176  * this corresponds to sliver (identified by Vserver context id, or xid).
177  */
178 typedef struct leaf {
179     /** The leaf identifier. */
180     uint32_t xid;
181
182     /** The leaf's parent in the hierarchy.  This is the identity to which this
183      * leaf belongs. */
184     identity_t *parent;
185
186     /** GRD: The leaf's packet drop probability. */
187     double drop_prob;
188 } leaf_t;
189
190 /**
191  * Contains all of the identity and sliver information associated with a
192  * runnable instance of a local DRL node.
193  */
194 typedef struct drl_instance {
195     /** An array of the node's viable leaves (slivers). */
196     leaf_t *leaves;
197     
198     /** The number of items in the leaf array. */
199     int leaf_count;
200
201     /** Maps sliver xid to the leaf_t in the leaves array. */
202     map_handle leaf_map;
203
204     /** An array of the node's machine-type identities. */
205     identity_t **machines;
206
207     /** The number of items in the machines array. */
208     int machine_count;
209
210     /** An array of the node's set-type identities. */
211     identity_t **sets;
212
213     /** The number of items in the sets array. */
214     int set_count;
215
216     /** Maps identity guid to the identity_t in either the machines or sets
217      * arrays. */
218     map_handle ident_map;
219
220     /** The lowest machine identity in the hierarchy.  This acts as the root
221      * for the set-identity subtree. */
222     identity_t *last_machine;
223
224     /** Acts as a circular array of lists used to schedule identities at
225      * some number of intervals. */
226     struct ident_calendar *cal;
227
228     /** The slot for the "current" tick in the calendar. */
229     uint32_t cal_slot;
230
231 } drl_instance_t;
232
233 /** Represents the local node. */
234 typedef struct limiter {
235     /** The limiter's local address in dotted quad notation. */
236     char *ip;
237
238     /** The node's individual (administrative) limit.  This node should set a
239      * limit above this value, even when DRL says it can do so. */
240     uint32_t nodelimit;
241
242     /** The DRL policy (GRD, FPS) this node is using. */
243     enum policies policy;
244
245     /** The estimate interval (in milliseconds). */
246     int estintms;
247
248     /** A lock to protect the state of identities so that they can be
249      * created/destroyed without harming any other, currently active idents.
250      *
251      * I made this an rwlock because it allows for much better parallelism.
252      * Creating/removing identities is uncommon, and most of the time this
253      * doesn't need to be held for writing. */
254     pthread_rwlock_t limiter_lock;
255
256     /** The currently running DRL instance. */
257     drl_instance_t stable_instance;
258
259     /** The next instance - if it validates.  When the XML config file is
260      * re-read, the new structures will be incorporated into this instance.  If
261      * everything checks out, the old (stable) instance will be freed, and this
262      * will be copied into stable_instance. */
263     drl_instance_t new_instance;
264
265     /* Communication fields. */
266     
267     /** The limiter's local address as an integer (net byte order). */
268     in_addr_t localaddr;
269
270     /** The local port on which to listen (net byte order). */
271     in_port_t port;
272
273     /** Local UDP communication socket. */
274     int udp_socket;
275
276 #if 0
277     /** Local TCP communication socket. */
278     int tcp_socket;
279 #endif
280
281     /** Limiter-wide UDP receive thread. */
282     pthread_t udp_recv_thread;
283
284 #if 0
285     /** Limiter-wide TCP thread for accepting incoming connections. */
286     pthread_t tcp_acpt_thread;
287 #endif
288
289 } limiter_t;
290
291 #endif