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