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