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