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"
22 #include "drl_state.h"
23 #include "common_accounting.h"
25 #include "samplehold.h"
29 /** Represents a DRL entitiy/group. */
30 typedef struct identity {
32 /** A unique id for the identity. */
35 /** The global rate limit. */
38 /** The local rate limit. */
41 /** Pointer to the identity's parent in the HTB hierarchy. */
42 struct identity *parent;
44 /** The fixed (per second) EWMA weight. */
45 double fixed_ewma_weight;
47 /** The real EWMA weight, based on the fixed weight and estimate interval.*/
50 /** Used for average rate graph generation. */
55 /** Communication data for this identity. */
60 /** The node in the HTB hierarchy whose limits will be modified by this
61 * identity. (1:1<htb_node>)*/
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.) */
68 /** FPS current weight value. */
71 /** FPS previous weight value. */
72 double last_localweight;
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. */
81 /** GRD drop probability information. */
84 /** GRD previous drop probability information. */
85 double last_localdropprob;
87 /* Flow accounting machinery. */
89 /** Flow information that is common to all types of tables.
90 * This includes aggregate rates, update times, etc. */
91 common_accounting_t common;
93 /** The actual table. Uses a void pointer because it could be one
94 * of several different types. (standard, sample&hold, etc. ) */
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.
102 pthread_mutex_t table_mutex;
104 /* Function pointers to functions to act on the table. */
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 *);
110 /** Function to call on the table when it is periodically cleaned. */
111 int (*table_cleanup_function)(void *);
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);
117 /** Function to call when the table should be destroyed. */
118 void (*table_destroy_function)(void *);
120 /* Scheduling bookkeeping. */
122 /* Pointers to other identities in the scheduling calendar. */
123 TAILQ_ENTRY(identity) calendar;
125 /* The number of limiter ticks at which this identity should be scheduled.
132 * Represents the bottom most entity in the HTB hierarchy. For PlanetLab,
133 * this corresponds to sliver (identified by Vserver context id, or xid).
135 typedef struct leaf {
136 /** The leaf identifier. */
139 /** The leaf's parent in the hierarchy. This is the identity to which this
145 * Contains all of the identity and sliver information associated with a
146 * runnable instance of a local DRL node.
148 typedef struct drl_instance {
149 /** An array of the node's viable leaves (slivers). */
152 /** The number of items in the leaf array. */
155 /** Maps sliver xid to the leaf_t in the leaves array. */
158 /** An array of the node's machine-type identities. */
159 identity_t **machines;
161 /** The number of items in the machines array. */
164 /** An array of the node's set-type identities. */
167 /** The number of items in the sets array. */
170 /** Maps identity guid to the identity_t in either the machines or sets
172 map_handle ident_map;
174 /** The lowest machine identity in the hierarchy. This acts as the root
175 * for the set-identity subtree. */
176 identity_t *last_machine;
178 /** Acts as a circular array of lists used to schedule identities at
179 * some number of intervals. */
180 struct ident_calendar *cal;
182 /** The slot for the "current" tick in the calendar. */
187 /** Represents the local node. */
188 typedef struct limiter {
189 /** The limiter's local address in dotted quad notation. */
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. */
196 /** The DRL policy (GRD, FPS) this node is using. */
197 enum policies policynum;
199 /** The estimate interval (in milliseconds). */
202 /** A lock to protect the state of identities so that they can be
203 * created/destroyed without harming any other, currently active idents.
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;
210 /** The currently running DRL instance. */
211 drl_instance_t stable_instance;
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;
219 /* Communication fields. */
221 /** The limiter's local address as an integer (net byte order). */
224 /** The local port on which to listen (net byte order). */
227 /** Local UDP communication socket. */
231 /** Local TCP communication socket. */
235 /** Limiter-wide UDP receive thread. */
236 pthread_t udp_recv_thread;
239 /** Limiter-wide TCP thread for accepting incoming connections. */
240 pthread_t tcp_acpt_thread;