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) {
110 xmlChar *failure_behavior;
113 xmlChar *mainloop_intervals;
114 xmlChar *communication_intervals;
115 xmlChar *independent;
116 xmlNodePtr fields = ident->children;
117 ident_peer *current = NULL;
119 /* The struct has been memsetted to 0, this is just to be safe. */
120 common->zk_host = NULL;
121 common->peers = NULL;
122 common->members = NULL;
125 /* Make sure no required fields are missing. */
126 id = xmlGetProp(ident, (const xmlChar *) "id");
128 printlog(LOG_CRITICAL, "Ident missing globally unique identifier.\n");
131 common->id = atoi((const char *) id);
135 limit = xmlGetProp(ident, (const xmlChar *) "limit");
137 printlog(LOG_CRITICAL, "Ident missing global rate limit.\n");
140 common->limit = atoi((const char *) limit);
144 commfabric = xmlGetProp(ident, (const xmlChar *) "commfabric");
145 if (commfabric == NULL) {
146 printlog(LOG_CRITICAL, "Ident missing comm fabric specifier.\n");
149 if (!xmlStrcmp(commfabric, (const xmlChar *) "MESH")) {
150 common->commfabric = COMM_MESH;
151 } else if (!xmlStrcmp(commfabric, (const xmlChar *) "GOSSIP")) {
152 common->commfabric = COMM_GOSSIP;
154 printlog(LOG_CRITICAL, "Unknown/invalid comm fabric.\n");
161 /* Only care about branching factor and failure detector if we're using gossip. */
162 if (common->commfabric == COMM_GOSSIP) {
163 branch = xmlGetProp(ident, (const xmlChar *) "branch");
164 if (branch == NULL) {
165 printlog(LOG_CRITICAL, "Ident missing gossip branch.\n");
168 common->branch = atoi((const char *) branch);
172 membership = xmlGetProp(ident, (const xmlChar *) "membership");
173 if (membership == NULL) {
174 printlog(LOG_CRITICAL, "Ident missing membership protocol selection.\n");
177 if (!xmlStrcmp(membership, (const xmlChar *) "SWIM")) {
178 common->membership = SWIM;
179 } else if (!xmlStrcmp(membership, (const xmlChar *) "ZOOKEEPER")) {
180 #ifdef BUILD_ZOOKEEPER
181 common->membership = ZOOKEEPER;
183 printlog(LOG_CRITICAL, "Zookeeper requested, but support not compiled into DRL at configure time.\n");
188 printlog(LOG_CRITICAL, "Unknown/invalid gossip group membership protocol.\n");
195 failure_behavior = xmlGetProp(ident, (const xmlChar *) "failure_behavior");
196 if (failure_behavior == NULL) {
197 printlog(LOG_CRITICAL, "Ident missing failure handling behavior.\n");
200 if (!xmlStrcmp(failure_behavior, (const xmlChar *) "PANIC")) {
201 common->failure_behavior = PANIC;
202 } else if (!xmlStrcmp(failure_behavior, (const xmlChar *) "QUORUM")) {
203 common->failure_behavior = QUORUM;
205 printlog(LOG_CRITICAL, "Unknown/invalid gossip failure behavior policy.\n");
206 xmlFree(failure_behavior);
209 xmlFree(failure_behavior);
213 accounting = xmlGetProp(ident, (const xmlChar *) "accounting");
214 if (accounting == NULL) {
215 printlog(LOG_CRITICAL, "Ident missing accounting.\n");
218 if (!xmlStrcmp(accounting, (const xmlChar *) "STANDARD")) {
219 common->accounting = ACT_STANDARD;
220 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SAMPLEHOLD")) {
221 common->accounting = ACT_SAMPLEHOLD;
222 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SIMPLE")) {
223 common->accounting = ACT_SIMPLE;
224 } else if (!xmlStrcmp(accounting, (const xmlChar *) "MULTIPLEINTERVAL")) {
225 common->accounting = ACT_MULTIPLE;
227 printlog(LOG_CRITICAL, "Unknown/invalid accounting table.\n");
234 ewma = xmlGetProp(ident, (const xmlChar *) "ewma");
236 printlog(LOG_CRITICAL, "Ident missing ewma weight.\n");
239 common->fixed_ewma_weight = atof((const char *) ewma);
243 mainloop_intervals = xmlGetProp(ident, (const xmlChar *) "loop_intervals");
244 if (mainloop_intervals == NULL) {
245 printlog(LOG_WARN, "Ident id: %d missing loop_intervals, assuming 1.\n", common->id);
246 common->mainloop_intervals = 1;
248 common->mainloop_intervals = atoi((const char *) mainloop_intervals);
249 xmlFree(mainloop_intervals);
252 communication_intervals = xmlGetProp(ident, (const xmlChar *) "comm_intervals");
253 if (communication_intervals == NULL) {
254 printlog(LOG_WARN, "Ident id: %d missing comm_intervals, assuming 1.\n", common->id);
255 common->communication_intervals = 1;
257 common->communication_intervals = atoi((const char *) communication_intervals);
258 xmlFree(communication_intervals);
261 independent = xmlGetProp(ident, (const xmlChar *) "independent");
262 if (independent == NULL) {
263 common->independent = 0;
265 common->independent = atoi((const char *) independent);
266 xmlFree(independent);
269 while (fields != NULL) {
270 if((!xmlStrcmp(fields->name, (const xmlChar *) "peer"))) {
271 xmlChar *ip = xmlNodeListGetString(doc, fields->children, 1);
272 if (current == NULL) {
274 common->peers = malloc(sizeof(ident_peer));
275 if (common->peers == NULL) {
278 common->peers->ip = inet_addr((const char *) ip);
279 common->peers->next = NULL;
280 common->peer_count += 1;
281 current = common->peers;
283 /* Add it to the list. */
284 current->next = malloc(sizeof(ident_peer));
285 if (current->next == NULL) {
288 current = current->next;
289 current->ip = inet_addr((const char *) ip);
290 common->peer_count += 1;
291 current->next = NULL;
294 } else if ((!xmlStrcmp(fields->name, (const xmlChar *) "zkhost"))) {
295 xmlChar *host = xmlNodeListGetString(doc, fields->children, 1);
297 common->zk_host = strdup((const char *) host);
298 if (common->zk_host == NULL) {
304 fields = fields->next;
307 if (common->peers == NULL) {
308 printlog(LOG_CRITICAL, "Must have at least one peer.\n");
312 if (common->membership == ZOOKEEPER && common->zk_host == NULL) {
313 printlog(LOG_CRITICAL, "Group membership protocol ZOOKEEPER requires a zkhost field.\n");
321 static ident_config *parse_machine(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
322 ident_config *common = malloc(sizeof(ident_config));
324 if (common == NULL) {
328 memset(common, 0, sizeof(ident_config));
329 if (parse_common(doc, ident, common)) {
334 /* No further information needed for machine-level identities. */
335 common->type = IDENT_MACHINE;
336 common->members = NULL;
339 if (configs->last_machine == NULL) {
340 configs->machines = common;
341 configs->last_machine = common;
343 configs->last_machine->next = common;
344 configs->last_machine = common;
347 configs->machine_count += 1;
352 static ident_config *parse_set(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
353 xmlNodePtr fields = ident->children;
354 ident_config *common = malloc(sizeof(ident_config));
355 ident_member *current = NULL;
357 if (common == NULL) {
361 memset(common, 0, sizeof(ident_config));
362 if (parse_common(doc, ident, common)) {
367 while (fields != NULL) {
368 ident_member *member = NULL;
370 if (!xmlStrcmp(fields->name, (const xmlChar *) "xid")) {
371 xmlChar *xid = xmlNodeListGetString(doc, fields->children, 1);
373 if (atoi((const char *) xid) >= 0) {
374 member = malloc(sizeof(ident_member));
375 if (member == NULL) {
380 member->type = MEMBER_XID;
381 sscanf((const char *) xid, "%x", &member->value);
390 } else if (!xmlStrcmp(fields->name, (const xmlChar *) "guid")) {
391 xmlChar *guid = xmlNodeListGetString(doc, fields->children, 1);
393 if (atoi((const char *) guid) >= 0) {
394 member = malloc(sizeof(ident_member));
395 if (member == NULL) {
400 member->type = MEMBER_GUID;
401 member->value = atoi((const char *) guid);
413 if (common->members == NULL) {
414 common->members = member;
417 current->next = member;
422 fields = fields->next;
425 /* A sliver set must have at least one member (xid or guid) or else it is
427 if (common->members == NULL) {
432 common->type = IDENT_SET;
435 if (configs->last_set == NULL) {
436 configs->sets = common;
437 configs->last_set = common;
439 configs->last_set->next = common;
440 configs->last_set = common;
443 configs->set_count += 1;
448 int parse_drl_config(const char *configfile, parsed_configs *configs) {
450 xmlNodePtr drl, ident;
452 configs->machines = NULL;
453 configs->sets = NULL;
454 configs->last_machine = NULL;
455 configs->last_set = NULL;
456 configs->machine_count = 0;
457 configs->set_count = 0;
459 if(!(doc = xmlParseFile(configfile))){
460 printlog(LOG_CRITICAL, "Config file (%s) not parsed successfully.\n", configfile);
465 if(!(drl = xmlDocGetRootElement(doc))){
466 printlog(LOG_CRITICAL, "Config file (%s) has no root element.\n", configfile);
470 if(xmlStrcmp(drl->name, (const xmlChar *) "drl")){
471 printlog(LOG_CRITICAL, "Config file (%s) of the wrong type, root node != drl\n", configfile);
476 ident = drl->children;
477 while(ident != NULL) {
478 ident_config *new = NULL;
480 if((!xmlStrcmp(ident->name, (const xmlChar *) "machine"))) {
481 new = parse_machine(doc, ident, configs);
482 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "set"))) {
483 new = parse_set(doc, ident, configs);
484 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "text"))) {
485 /* libxml seems to wrap everything inside two 'text's. */
491 /* FIXME: Make this more descriptive. :) */
492 printlog(LOG_CRITICAL, "Error occurred while parsing...\n");
494 free_ident_list(configs->machines);
495 free_ident_list(configs->sets);
497 configs->machines = NULL;
498 configs->sets = NULL;
499 configs->last_machine = NULL;
500 configs->last_set = NULL;
511 /* Return the list of parsed identities. */