1 /* See the DRL-LICENSE file for this file's software license. */
6 #include <libxml/xmlmemory.h>
7 #include <libxml/parser.h>
8 #include <netinet/in.h>
12 #include <sys/socket.h>
14 #include "raterouter.h"
15 #include "ratetypes.h"
20 void free_ident(ident_config *ident) {
22 while (ident->peers) {
23 ident_peer *tofree = ident->peers;
24 ident->peers = ident->peers->next;
29 while (ident->members) {
30 ident_member *tofree = ident->members;
31 ident->members = ident->members->next;
39 void free_ident_list(ident_config *list) {
49 static int xid_filter(const struct dirent *d) {
50 if (atoi(d->d_name) > 0)
56 int get_eligible_leaves(drl_instance_t *instance) {
57 struct dirent **names;
59 leaf_t *leaves = NULL;
60 map_handle leaf_map = allocate_map();
62 if (leaf_map == NULL) {
66 count = scandir("/proc/virtual", &names, xid_filter, alphasort);
72 leaves = malloc(count * sizeof(leaf_t));
74 /* Couldn't allocate leaves array. Need to free names memory. */
82 memset(leaves, 0, count * sizeof(leaf_t));
84 for (i = 0; i < count; ++i) {
85 leaves[i].xid = atoi(names[i]->d_name);
86 leaves[i].parent = NULL;
87 leaves[i].drop_prob = 0.0;
92 map_insert(leaf_map, &leaves[i].xid, sizeof(leaves[i].xid), &leaves[i]);
97 instance->leaf_map = leaf_map;
98 instance->leaves = leaves;
99 instance->leaf_count = count;
104 static int parse_common(xmlDocPtr doc, xmlNodePtr ident, ident_config *common) {
111 xmlChar *mainloop_intervals;
112 xmlChar *communication_intervals;
113 xmlChar *independent;
114 xmlNodePtr fields = ident->children;
115 ident_peer *current = NULL;
117 /* Make sure no required fields are missing. */
118 id = xmlGetProp(ident, (const xmlChar *) "id");
120 printlog(LOG_CRITICAL, "Ident missing globally unique identifier.\n");
123 common->id = atoi((const char *) id);
127 limit = xmlGetProp(ident, (const xmlChar *) "limit");
129 printlog(LOG_CRITICAL, "Ident missing global rate limit.\n");
132 common->limit = atoi((const char *) limit);
136 commfabric = xmlGetProp(ident, (const xmlChar *) "commfabric");
137 if (commfabric == NULL) {
138 printlog(LOG_CRITICAL, "Ident missing comm fabric specifier.\n");
141 if (!xmlStrcmp(commfabric, (const xmlChar *) "MESH")) {
142 common->commfabric = COMM_MESH;
143 } else if (!xmlStrcmp(commfabric, (const xmlChar *) "GOSSIP")) {
144 common->commfabric = COMM_GOSSIP;
146 printlog(LOG_CRITICAL, "Unknown/invalid comm fabric.\n");
153 /* Only care about branching factor if we're using gossip. */
154 if (common->commfabric == COMM_GOSSIP) {
155 branch = xmlGetProp(ident, (const xmlChar *) "branch");
156 if (branch == NULL) {
157 printlog(LOG_CRITICAL, "Ident missing gossip branch.\n");
160 common->branch = atoi((const char *) branch);
165 accounting = xmlGetProp(ident, (const xmlChar *) "accounting");
166 if (accounting == NULL) {
167 printlog(LOG_CRITICAL, "Ident missing accounting.\n");
170 if (!xmlStrcmp(accounting, (const xmlChar *) "STANDARD")) {
171 common->accounting = ACT_STANDARD;
172 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SAMPLEHOLD")) {
173 common->accounting = ACT_SAMPLEHOLD;
174 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SIMPLE")) {
175 common->accounting = ACT_SIMPLE;
176 } else if (!xmlStrcmp(accounting, (const xmlChar *) "MULTIPLEINTERVAL")) {
177 common->accounting = ACT_MULTIPLE;
179 printlog(LOG_CRITICAL, "Unknown/invalid accounting table.\n");
186 ewma = xmlGetProp(ident, (const xmlChar *) "ewma");
188 printlog(LOG_CRITICAL, "Ident missing ewma weight.\n");
191 common->fixed_ewma_weight = atof((const char *) ewma);
195 mainloop_intervals = xmlGetProp(ident, (const xmlChar *) "loop_intervals");
196 if (mainloop_intervals == NULL) {
197 printlog(LOG_WARN, "Ident id: %d missing loop_intervals, assuming 1.\n", common->id);
198 common->mainloop_intervals = 1;
200 common->mainloop_intervals = atoi((const char *) mainloop_intervals);
201 xmlFree(mainloop_intervals);
204 communication_intervals = xmlGetProp(ident, (const xmlChar *) "comm_intervals");
205 if (communication_intervals == NULL) {
206 printlog(LOG_WARN, "Ident id: %d missing comm_intervals, assuming 1.\n", common->id);
207 common->communication_intervals = 1;
209 common->communication_intervals = atoi((const char *) communication_intervals);
210 xmlFree(communication_intervals);
213 independent = xmlGetProp(ident, (const xmlChar *) "independent");
214 if (independent == NULL) {
215 common->independent = 0;
217 common->independent = atoi((const char *) independent);
218 xmlFree(independent);
221 while (fields != NULL) {
222 if((!xmlStrcmp(fields->name, (const xmlChar *) "peer"))) {
223 xmlChar *ip = xmlNodeListGetString(doc, fields->children, 1);
224 if (current == NULL) {
226 common->peers = malloc(sizeof(ident_peer));
227 if (common->peers == NULL) {
230 common->peers->ip = inet_addr((const char *) ip);
231 common->peers->next = NULL;
232 common->peer_count += 1;
233 current = common->peers;
235 /* Add it to the list. */
236 current->next = malloc(sizeof(ident_peer));
237 if (current->next == NULL) {
240 current = current->next;
241 current->ip = inet_addr((const char *) ip);
242 common->peer_count += 1;
243 current->next = NULL;
247 fields = fields->next;
250 if (common->peers == NULL) {
251 printlog(LOG_CRITICAL, "Must have at least one peer.\n");
259 static ident_config *parse_machine(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
260 ident_config *common = malloc(sizeof(ident_config));
262 if (common == NULL) {
266 memset(common, 0, sizeof(ident_config));
267 if (parse_common(doc, ident, common)) {
272 /* No further information needed for machine-level identities. */
273 common->type = IDENT_MACHINE;
274 common->members = NULL;
277 if (configs->last_machine == NULL) {
278 configs->machines = common;
279 configs->last_machine = common;
281 configs->last_machine->next = common;
282 configs->last_machine = common;
285 configs->machine_count += 1;
290 static ident_config *parse_set(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
291 xmlNodePtr fields = ident->children;
292 ident_config *common = malloc(sizeof(ident_config));
293 ident_member *current = NULL;
295 if (common == NULL) {
299 memset(common, 0, sizeof(ident_config));
300 if (parse_common(doc, ident, common)) {
305 while (fields != NULL) {
306 ident_member *member = NULL;
308 if (!xmlStrcmp(fields->name, (const xmlChar *) "xid")) {
309 xmlChar *xid = xmlNodeListGetString(doc, fields->children, 1);
311 if (atoi((const char *) xid) >= 0) {
312 member = malloc(sizeof(ident_member));
313 if (member == NULL) {
318 member->type = MEMBER_XID;
319 sscanf((const char *) xid, "%x", &member->value);
328 } else if (!xmlStrcmp(fields->name, (const xmlChar *) "guid")) {
329 xmlChar *guid = xmlNodeListGetString(doc, fields->children, 1);
331 if (atoi((const char *) guid) >= 0) {
332 member = malloc(sizeof(ident_member));
333 if (member == NULL) {
338 member->type = MEMBER_GUID;
339 member->value = atoi((const char *) guid);
351 if (common->members == NULL) {
352 common->members = member;
355 current->next = member;
360 fields = fields->next;
363 /* A sliver set must have at least one member (xid or guid) or else it is
365 if (common->members == NULL) {
370 common->type = IDENT_SET;
373 if (configs->last_set == NULL) {
374 configs->sets = common;
375 configs->last_set = common;
377 configs->last_set->next = common;
378 configs->last_set = common;
381 configs->set_count += 1;
386 int parse_drl_config(const char *configfile, parsed_configs *configs) {
388 xmlNodePtr drl, ident;
390 configs->machines = NULL;
391 configs->sets = NULL;
392 configs->last_machine = NULL;
393 configs->last_set = NULL;
394 configs->machine_count = 0;
395 configs->set_count = 0;
397 if(!(doc = xmlParseFile(configfile))){
398 printlog(LOG_CRITICAL, "Config file (%s) not parsed successfully.\n", configfile);
403 if(!(drl = xmlDocGetRootElement(doc))){
404 printlog(LOG_CRITICAL, "Config file (%s) has no root element.\n", configfile);
408 if(xmlStrcmp(drl->name, (const xmlChar *) "drl")){
409 printlog(LOG_CRITICAL, "Config file (%s) of the wrong type, root node != drl\n", configfile);
414 ident = drl->children;
415 while(ident != NULL) {
416 ident_config *new = NULL;
418 if((!xmlStrcmp(ident->name, (const xmlChar *) "machine"))) {
419 new = parse_machine(doc, ident, configs);
420 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "set"))) {
421 new = parse_set(doc, ident, configs);
422 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "text"))) {
423 /* libxml seems to wrap everything inside two 'text's. */
429 /* FIXME: Make this more descriptive. :) */
430 printlog(LOG_CRITICAL, "Error occurred while parsing...\n");
432 free_ident_list(configs->machines);
433 free_ident_list(configs->sets);
435 configs->machines = NULL;
436 configs->sets = NULL;
437 configs->last_machine = NULL;
438 configs->last_set = NULL;
449 /* Return the list of parsed identities. */