1 /* See the DRL-LICENSE file for this file's software license. */
5 * Defines the main types used by DRL.
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"
20 #include "bsd_queue.h"
22 #include "drl_state.h"
23 #include "common_accounting.h"
25 #include "samplehold.h"
27 #include "multipleinterval.h"
30 /** Represents a DRL entitiy/group. */
31 typedef struct identity {
33 /** A unique id for the identity. */
36 /** The global rate limit. */
39 /** The local rate limit. */
42 /** Pointer to the identity's parent in the HTB hierarchy. */
43 struct identity *parent;
45 /** Array of the leaves that are limited by this identity. Points into the
46 * leaves array for the identity's instance. */
49 /** The number of leaves for which this identity is responsible. */
52 /** The fixed (per second) EWMA weight. */
53 double fixed_ewma_weight;
55 /** The real EWMA weight, based on the fixed weight and estimate interval.*/
58 /** Used for average rate graph generation. */
63 /** Communication data for this identity. */
68 /** The node in the HTB hierarchy whose limits will be modified by this
69 * identity. (1:1<htb_node>)*/
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.) */
76 /** FPS current weight value. */
79 /** FPS previous weight value. */
80 double last_localweight;
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. */
89 /** Keeps track of the state of FPS dampening for this identity. */
90 enum dampenings dampen_state;
94 /** GRD drop probability information. */
97 /** GRD previous drop probability information. */
98 double last_drop_prob;
100 /* Flow accounting machinery. */
102 /** Flow information that is common to all types of tables.
103 * This includes aggregate rates, update times, etc. */
104 common_accounting_t common;
106 /** The actual table. Uses a void pointer because it could be one
107 * of several different types. (standard, sample&hold, etc. ) */
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.
115 pthread_mutex_t table_mutex;
117 /* Function pointers to functions to act on the table. */
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 *);
123 /** Function to call on the table when it is periodically cleaned. */
124 int (*table_cleanup_function)(void *);
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);
130 /** Function to call when the table should be destroyed. */
131 void (*table_destroy_function)(void *);
133 #ifdef SHADOW_ACCTING
135 common_accounting_t shadow_common;
139 double localweight_copy;
140 double last_localweight_copy;
142 enum dampenings dampen_state_copy;
146 /* Scheduling bookkeeping. */
148 /** Scheduling unit that tells the limiter when to execute the main loop.*/
149 struct ident_action *loop_action;
151 /** Scheduling unit that tells the limiter when to communicate.*/
152 struct ident_action *comm_action;
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;
158 /** The number of limiter ticks that should pass before this identity should
159 * be scheduled for communication. */
160 uint32_t communication_intervals;
164 enum identity_actions { ACTION_MAINLOOP = 1, ACTION_COMMUNICATE = 2 };
166 typedef struct ident_action {
167 struct identity *ident;
168 enum identity_actions action;
171 TAILQ_ENTRY(ident_action) calendar;
175 * Represents the bottom most entity in the HTB hierarchy. For PlanetLab,
176 * this corresponds to sliver (identified by Vserver context id, or xid).
178 typedef struct leaf {
179 /** The leaf identifier. */
182 /** The leaf's parent in the hierarchy. This is the identity to which this
186 /** GRD: The leaf's packet drop probability. */
191 * Contains all of the identity and sliver information associated with a
192 * runnable instance of a local DRL node.
194 typedef struct drl_instance {
195 /** An array of the node's viable leaves (slivers). */
198 /** The number of items in the leaf array. */
201 /** Maps sliver xid to the leaf_t in the leaves array. */
204 /** An array of the node's machine-type identities. */
205 identity_t **machines;
207 /** The number of items in the machines array. */
210 /** An array of the node's set-type identities. */
213 /** The number of items in the sets array. */
216 /** Maps identity guid to the identity_t in either the machines or sets
218 map_handle ident_map;
220 /** The lowest machine identity in the hierarchy. This acts as the root
221 * for the set-identity subtree. */
222 identity_t *last_machine;
224 /** Acts as a circular array of lists used to schedule identities at
225 * some number of intervals. */
226 struct ident_calendar *cal;
228 /** The slot for the "current" tick in the calendar. */
233 /** Represents the local node. */
234 typedef struct limiter {
235 /** The limiter's local address in dotted quad notation. */
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. */
242 /** The DRL policy (GRD, FPS) this node is using. */
243 enum policies policy;
245 /** The estimate interval (in milliseconds). */
248 /** A lock to protect the state of identities so that they can be
249 * created/destroyed without harming any other, currently active idents.
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;
256 /** The currently running DRL instance. */
257 drl_instance_t stable_instance;
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;
265 /* Communication fields. */
267 /** The limiter's local address as an integer (net byte order). */
270 /** The local port on which to listen (net byte order). */
273 /** Local UDP communication socket. */
277 /** Local TCP communication socket. */
281 /** Limiter-wide UDP receive thread. */
282 pthread_t udp_recv_thread;
285 /** Limiter-wide TCP thread for accepting incoming connections. */
286 pthread_t tcp_acpt_thread;