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;
118 xmlNodePtr fields = ident->children;
119 ident_peer *current = NULL;
121 /* The struct has been memsetted to 0, this is just to be safe. */
122 #ifdef BUILD_ZOOKEEPER
123 common->zk_host = NULL;
125 common->peers = NULL;
126 common->members = NULL;
129 /* Make sure no required fields are missing. */
130 id = xmlGetProp(ident, (const xmlChar *) "id");
132 printlog(LOG_CRITICAL, "Ident missing globally unique identifier.\n");
135 common->id = atoi((const char *) id);
139 limit = xmlGetProp(ident, (const xmlChar *) "limit");
141 printlog(LOG_CRITICAL, "Ident missing global rate limit.\n");
144 common->limit = atoi((const char *) limit);
148 commfabric = xmlGetProp(ident, (const xmlChar *) "commfabric");
149 if (commfabric == NULL) {
150 printlog(LOG_CRITICAL, "Ident missing comm fabric specifier.\n");
153 if (!xmlStrcmp(commfabric, (const xmlChar *) "MESH")) {
154 common->commfabric = COMM_MESH;
155 } else if (!xmlStrcmp(commfabric, (const xmlChar *) "GOSSIP")) {
156 common->commfabric = COMM_GOSSIP;
158 printlog(LOG_CRITICAL, "Unknown/invalid comm fabric.\n");
165 /* Only care about branching factor and failure detector if we're using gossip. */
166 if (common->commfabric == COMM_GOSSIP) {
167 branch = xmlGetProp(ident, (const xmlChar *) "branch");
168 if (branch == NULL) {
169 printlog(LOG_CRITICAL, "Ident missing gossip branch.\n");
172 common->branch = atoi((const char *) branch);
176 membership = xmlGetProp(ident, (const xmlChar *) "membership");
177 if (membership == NULL) {
178 printlog(LOG_CRITICAL, "Ident missing membership protocol selection.\n");
181 if (!xmlStrcmp(membership, (const xmlChar *) "SWIM")) {
182 common->membership = SWIM;
183 } else if (!xmlStrcmp(membership, (const xmlChar *) "ZOOKEEPER")) {
184 #ifdef BUILD_ZOOKEEPER
185 common->membership = ZOOKEEPER;
187 printlog(LOG_CRITICAL, "Zookeeper requested, but support not compiled into DRL at configure time.\n");
192 printlog(LOG_CRITICAL, "Unknown/invalid gossip group membership protocol.\n");
199 failure_behavior = xmlGetProp(ident, (const xmlChar *) "failure_behavior");
200 if (failure_behavior == NULL) {
201 printlog(LOG_CRITICAL, "Ident missing failure handling behavior.\n");
204 if (!xmlStrcmp(failure_behavior, (const xmlChar *) "PANIC")) {
205 common->failure_behavior = PANIC;
206 } else if (!xmlStrcmp(failure_behavior, (const xmlChar *) "QUORUM")) {
207 common->failure_behavior = QUORUM;
209 printlog(LOG_CRITICAL, "Unknown/invalid gossip failure behavior policy.\n");
210 xmlFree(failure_behavior);
213 xmlFree(failure_behavior);
217 accounting = xmlGetProp(ident, (const xmlChar *) "accounting");
218 if (accounting == NULL) {
219 printlog(LOG_CRITICAL, "Ident missing accounting.\n");
222 if (!xmlStrcmp(accounting, (const xmlChar *) "STANDARD")) {
223 common->accounting = ACT_STANDARD;
224 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SAMPLEHOLD")) {
225 common->accounting = ACT_SAMPLEHOLD;
226 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SIMPLE")) {
227 common->accounting = ACT_SIMPLE;
228 } else if (!xmlStrcmp(accounting, (const xmlChar *) "MULTIPLEINTERVAL")) {
229 common->accounting = ACT_MULTIPLE;
231 printlog(LOG_CRITICAL, "Unknown/invalid accounting table.\n");
238 ewma = xmlGetProp(ident, (const xmlChar *) "ewma");
240 printlog(LOG_CRITICAL, "Ident missing ewma weight.\n");
243 common->fixed_ewma_weight = atof((const char *) ewma);
247 mainloop_intervals = xmlGetProp(ident, (const xmlChar *) "loop_intervals");
248 if (mainloop_intervals == NULL) {
249 printlog(LOG_WARN, "Ident id: %d missing loop_intervals, assuming 1.\n", common->id);
250 common->mainloop_intervals = 1;
252 common->mainloop_intervals = atoi((const char *) mainloop_intervals);
253 xmlFree(mainloop_intervals);
256 communication_intervals = xmlGetProp(ident, (const xmlChar *) "comm_intervals");
257 if (communication_intervals == NULL) {
258 printlog(LOG_WARN, "Ident id: %d missing comm_intervals, assuming 1.\n", common->id);
259 common->communication_intervals = 1;
261 common->communication_intervals = atoi((const char *) communication_intervals);
262 xmlFree(communication_intervals);
265 independent = xmlGetProp(ident, (const xmlChar *) "independent");
266 if (independent == NULL) {
267 common->independent = 0;
269 common->independent = atoi((const char *) independent);
270 xmlFree(independent);
273 htb_node = xmlGetProp(ident, (const xmlChar *) "htb_node");
274 htb_parent = xmlGetProp(ident, (const xmlChar *) "htb_parent");
275 if (htb_node == NULL) {
276 common->htb_node = -1;
278 sscanf((const char *)htb_node, "%x", &common->htb_node);
281 if (htb_parent == NULL) {
282 common->htb_parent = -1;
284 sscanf((const char *)htb_parent, "%x", &common->htb_parent);
288 while (fields != NULL) {
289 if((!xmlStrcmp(fields->name, (const xmlChar *) "peer"))) {
290 xmlChar *ip = xmlNodeListGetString(doc, fields->children, 1);
291 if (current == NULL) {
293 common->peers = malloc(sizeof(ident_peer));
294 if (common->peers == NULL) {
297 common->peers->ip = inet_addr((const char *) ip);
298 common->peers->next = NULL;
299 common->peer_count += 1;
300 current = common->peers;
302 /* Add it to the list. */
303 current->next = malloc(sizeof(ident_peer));
304 if (current->next == NULL) {
307 current = current->next;
308 current->ip = inet_addr((const char *) ip);
309 common->peer_count += 1;
310 current->next = NULL;
313 } else if ((!xmlStrcmp(fields->name, (const xmlChar *) "zkhost"))) {
314 xmlChar *host = xmlNodeListGetString(doc, fields->children, 1);
316 #ifdef BUILD_ZOOKEEPER
317 common->zk_host = strdup((const char *) host);
318 if (common->zk_host == NULL) {
324 fields = fields->next;
327 if (common->peers == NULL) {
328 printlog(LOG_CRITICAL, "Must have at least one peer.\n");
332 #ifdef BUILD_ZOOKEEPER
333 if (common->membership == ZOOKEEPER && common->zk_host == NULL) {
334 printlog(LOG_CRITICAL, "Group membership protocol ZOOKEEPER requires a zkhost field.\n");
342 static ident_config *parse_machine(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
343 ident_config *common = malloc(sizeof(ident_config));
345 if (common == NULL) {
349 memset(common, 0, sizeof(ident_config));
350 if (parse_common(doc, ident, common)) {
355 /* No further information needed for machine-level identities. */
356 common->type = IDENT_MACHINE;
357 common->members = NULL;
360 if (configs->last_machine == NULL) {
361 configs->machines = common;
362 configs->last_machine = common;
364 configs->last_machine->next = common;
365 configs->last_machine = common;
368 configs->machine_count += 1;
373 static ident_config *parse_set(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
374 xmlNodePtr fields = ident->children;
375 ident_config *common = malloc(sizeof(ident_config));
376 ident_member *current = NULL;
378 if (common == NULL) {
382 memset(common, 0, sizeof(ident_config));
383 if (parse_common(doc, ident, common)) {
388 while (fields != NULL) {
389 ident_member *member = NULL;
391 if (!xmlStrcmp(fields->name, (const xmlChar *) "xid")) {
392 xmlChar *xid = xmlNodeListGetString(doc, fields->children, 1);
394 if (atoi((const char *) xid) >= 0) {
395 member = malloc(sizeof(ident_member));
396 if (member == NULL) {
401 member->type = MEMBER_XID;
402 sscanf((const char *) xid, "%x", &member->value);
411 } else if (!xmlStrcmp(fields->name, (const xmlChar *) "guid")) {
412 xmlChar *guid = xmlNodeListGetString(doc, fields->children, 1);
414 if (atoi((const char *) guid) >= 0) {
415 member = malloc(sizeof(ident_member));
416 if (member == NULL) {
421 member->type = MEMBER_GUID;
422 member->value = atoi((const char *) guid);
434 if (common->members == NULL) {
435 common->members = member;
438 current->next = member;
443 fields = fields->next;
446 /* A sliver set must have at least one member (xid or guid) or else it is
448 if (common->members == NULL) {
453 common->type = IDENT_SET;
456 if (configs->last_set == NULL) {
457 configs->sets = common;
458 configs->last_set = common;
460 configs->last_set->next = common;
461 configs->last_set = common;
464 configs->set_count += 1;
469 int parse_drl_config(const char *configfile, parsed_configs *configs) {
471 xmlNodePtr drl, ident;
473 configs->machines = NULL;
474 configs->sets = NULL;
475 configs->last_machine = NULL;
476 configs->last_set = NULL;
477 configs->machine_count = 0;
478 configs->set_count = 0;
480 if(!(doc = xmlParseFile(configfile))){
481 printlog(LOG_CRITICAL, "Config file (%s) not parsed successfully.\n", configfile);
486 if(!(drl = xmlDocGetRootElement(doc))){
487 printlog(LOG_CRITICAL, "Config file (%s) has no root element.\n", configfile);
491 if(xmlStrcmp(drl->name, (const xmlChar *) "drl")){
492 printlog(LOG_CRITICAL, "Config file (%s) of the wrong type, root node != drl\n", configfile);
497 ident = drl->children;
498 while(ident != NULL) {
499 ident_config *new = NULL;
501 if((!xmlStrcmp(ident->name, (const xmlChar *) "machine"))) {
502 new = parse_machine(doc, ident, configs);
503 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "set"))) {
504 new = parse_set(doc, ident, configs);
505 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "text"))) {
506 /* libxml seems to wrap everything inside two 'text's. */
512 /* FIXME: Make this more descriptive. :) */
513 printlog(LOG_CRITICAL, "Error occurred while parsing...\n");
515 free_ident_list(configs->machines);
516 free_ident_list(configs->sets);
518 configs->machines = NULL;
519 configs->sets = NULL;
520 configs->last_machine = NULL;
521 configs->last_set = NULL;
532 /* Return the list of parsed identities. */