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 /** For sets, indicates the parent should be 0x10. Meaningless for machines. */
48 /** Array of the leaves that are limited by this identity. Points into the
49 * leaves array for the identity's instance. */
52 /** The number of leaves for which this identity is responsible. */
55 /** The fixed (per second) EWMA weight. */
56 double fixed_ewma_weight;
58 /** The real EWMA weight, based on the fixed weight and estimate interval.*/
61 /** Used for average rate graph generation. */
66 /** Communication data for this identity. */
71 /** The node in the HTB hierarchy whose limits will be modified by this
72 * identity. (1:1<htb_node>)*/
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.) */
79 /** FPS current weight value. */
82 /** FPS previous weight value. */
83 double last_localweight;
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. */
92 /** Keeps track of the state of FPS dampening for this identity. */
93 enum dampenings dampen_state;
97 /** GRD drop probability information. */
100 /** GRD previous drop probability information. */
101 double last_drop_prob;
103 /* Flow accounting machinery. */
105 /** Flow information that is common to all types of tables.
106 * This includes aggregate rates, update times, etc. */
107 common_accounting_t common;
109 /** The actual table. Uses a void pointer because it could be one
110 * of several different types. (standard, sample&hold, etc. ) */
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.
118 pthread_mutex_t table_mutex;
120 /* Function pointers to functions to act on the table. */
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 *);
126 /** Function to call on the table when it is periodically cleaned. */
127 int (*table_cleanup_function)(void *);
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);
133 /** Function to call when the table should be destroyed. */
134 void (*table_destroy_function)(void *);
136 #ifdef SHADOW_ACCTING
138 common_accounting_t shadow_common;
142 double localweight_copy;
143 double last_localweight_copy;
145 enum dampenings dampen_state_copy;
149 /* Scheduling bookkeeping. */
151 /** Scheduling unit that tells the limiter when to execute the main loop.*/
152 struct ident_action *loop_action;
154 /** Scheduling unit that tells the limiter when to communicate.*/
155 struct ident_action *comm_action;
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;
161 /** The number of limiter ticks that should pass before this identity should
162 * be scheduled for communication. */
163 uint32_t communication_intervals;
167 enum identity_actions { ACTION_MAINLOOP = 1, ACTION_COMMUNICATE = 2 };
169 typedef struct ident_action {
170 struct identity *ident;
171 enum identity_actions action;
174 TAILQ_ENTRY(ident_action) calendar;
178 * Represents the bottom most entity in the HTB hierarchy. For PlanetLab,
179 * this corresponds to sliver (identified by Vserver context id, or xid).
181 typedef struct leaf {
182 /** The leaf identifier. */
185 /** The leaf's parent in the hierarchy. This is the identity to which this
189 /** GRD: The leaf's packet drop probability. */
194 * Contains all of the identity and sliver information associated with a
195 * runnable instance of a local DRL node.
197 typedef struct drl_instance {
198 /** An array of the node's viable leaves (slivers). */
201 /** The number of items in the leaf array. */
204 /** Maps sliver xid to the leaf_t in the leaves array. */
207 /** An array of the node's machine-type identities. */
208 identity_t **machines;
210 /** The number of items in the machines array. */
213 /** An array of the node's set-type identities. */
216 /** The number of items in the sets array. */
219 /** Maps identity guid to the identity_t in either the machines or sets
221 map_handle ident_map;
223 /** The lowest machine identity in the hierarchy. This acts as the root
224 * for the set-identity subtree. */
225 identity_t *last_machine;
227 /** Acts as a circular array of lists used to schedule identities at
228 * some number of intervals. */
229 struct ident_calendar *cal;
231 /** The slot for the "current" tick in the calendar. */
236 /** Represents the local node. */
237 typedef struct limiter {
238 /** The limiter's local address in dotted quad notation. */
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. */
245 /** The DRL policy (GRD, FPS) this node is using. */
246 enum policies policy;
248 /** The estimate interval (in milliseconds). */
251 /** A lock to protect the state of identities so that they can be
252 * created/destroyed without harming any other, currently active idents.
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;
259 /** The currently running DRL instance. */
260 drl_instance_t stable_instance;
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;
268 /* Communication fields. */
270 /** The limiter's local address as an integer (net byte order). */
273 /** The local port on which to listen (net byte order). */
276 /** Local UDP communication socket. */
280 /** Local TCP communication socket. */
284 /** Limiter-wide UDP receive thread. */
285 pthread_t udp_recv_thread;
288 /** Limiter-wide TCP thread for accepting incoming connections. */
289 pthread_t tcp_acpt_thread;