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 common->zk_host = NULL;
123 common->peers = NULL;
124 common->members = NULL;
127 /* Make sure no required fields are missing. */
128 id = xmlGetProp(ident, (const xmlChar *) "id");
130 printlog(LOG_CRITICAL, "Ident missing globally unique identifier.\n");
133 common->id = atoi((const char *) id);
137 limit = xmlGetProp(ident, (const xmlChar *) "limit");
139 printlog(LOG_CRITICAL, "Ident missing global rate limit.\n");
142 common->limit = atoi((const char *) limit);
146 commfabric = xmlGetProp(ident, (const xmlChar *) "commfabric");
147 if (commfabric == NULL) {
148 printlog(LOG_CRITICAL, "Ident missing comm fabric specifier.\n");
151 if (!xmlStrcmp(commfabric, (const xmlChar *) "MESH")) {
152 common->commfabric = COMM_MESH;
153 } else if (!xmlStrcmp(commfabric, (const xmlChar *) "GOSSIP")) {
154 common->commfabric = COMM_GOSSIP;
156 printlog(LOG_CRITICAL, "Unknown/invalid comm fabric.\n");
163 /* Only care about branching factor and failure detector if we're using gossip. */
164 if (common->commfabric == COMM_GOSSIP) {
165 branch = xmlGetProp(ident, (const xmlChar *) "branch");
166 if (branch == NULL) {
167 printlog(LOG_CRITICAL, "Ident missing gossip branch.\n");
170 common->branch = atoi((const char *) branch);
174 membership = xmlGetProp(ident, (const xmlChar *) "membership");
175 if (membership == NULL) {
176 printlog(LOG_CRITICAL, "Ident missing membership protocol selection.\n");
179 if (!xmlStrcmp(membership, (const xmlChar *) "SWIM")) {
180 common->membership = SWIM;
181 } else if (!xmlStrcmp(membership, (const xmlChar *) "ZOOKEEPER")) {
182 #ifdef BUILD_ZOOKEEPER
183 common->membership = ZOOKEEPER;
185 printlog(LOG_CRITICAL, "Zookeeper requested, but support not compiled into DRL at configure time.\n");
190 printlog(LOG_CRITICAL, "Unknown/invalid gossip group membership protocol.\n");
197 failure_behavior = xmlGetProp(ident, (const xmlChar *) "failure_behavior");
198 if (failure_behavior == NULL) {
199 printlog(LOG_CRITICAL, "Ident missing failure handling behavior.\n");
202 if (!xmlStrcmp(failure_behavior, (const xmlChar *) "PANIC")) {
203 common->failure_behavior = PANIC;
204 } else if (!xmlStrcmp(failure_behavior, (const xmlChar *) "QUORUM")) {
205 common->failure_behavior = QUORUM;
207 printlog(LOG_CRITICAL, "Unknown/invalid gossip failure behavior policy.\n");
208 xmlFree(failure_behavior);
211 xmlFree(failure_behavior);
215 accounting = xmlGetProp(ident, (const xmlChar *) "accounting");
216 if (accounting == NULL) {
217 printlog(LOG_CRITICAL, "Ident missing accounting.\n");
220 if (!xmlStrcmp(accounting, (const xmlChar *) "STANDARD")) {
221 common->accounting = ACT_STANDARD;
222 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SAMPLEHOLD")) {
223 common->accounting = ACT_SAMPLEHOLD;
224 } else if (!xmlStrcmp(accounting, (const xmlChar *) "SIMPLE")) {
225 common->accounting = ACT_SIMPLE;
226 } else if (!xmlStrcmp(accounting, (const xmlChar *) "MULTIPLEINTERVAL")) {
227 common->accounting = ACT_MULTIPLE;
229 printlog(LOG_CRITICAL, "Unknown/invalid accounting table.\n");
236 ewma = xmlGetProp(ident, (const xmlChar *) "ewma");
238 printlog(LOG_CRITICAL, "Ident missing ewma weight.\n");
241 common->fixed_ewma_weight = atof((const char *) ewma);
245 mainloop_intervals = xmlGetProp(ident, (const xmlChar *) "loop_intervals");
246 if (mainloop_intervals == NULL) {
247 printlog(LOG_WARN, "Ident id: %d missing loop_intervals, assuming 1.\n", common->id);
248 common->mainloop_intervals = 1;
250 common->mainloop_intervals = atoi((const char *) mainloop_intervals);
251 xmlFree(mainloop_intervals);
254 communication_intervals = xmlGetProp(ident, (const xmlChar *) "comm_intervals");
255 if (communication_intervals == NULL) {
256 printlog(LOG_WARN, "Ident id: %d missing comm_intervals, assuming 1.\n", common->id);
257 common->communication_intervals = 1;
259 common->communication_intervals = atoi((const char *) communication_intervals);
260 xmlFree(communication_intervals);
263 independent = xmlGetProp(ident, (const xmlChar *) "independent");
264 if (independent == NULL) {
265 common->independent = 0;
267 common->independent = atoi((const char *) independent);
268 xmlFree(independent);
271 htb_node = xmlGetProp(ident, (const xmlChar *) "htb_node");
272 htb_parent = xmlGetProp(ident, (const xmlChar *) "htb_parent");
273 if (htb_node == NULL) {
274 common->htb_node = -1;
276 sscanf((const char *)htb_node, "%x", &common->htb_node);
279 if (htb_parent == NULL) {
280 common->htb_parent = -1;
282 sscanf((const char *)htb_parent, "%x", &common->htb_parent);
286 while (fields != NULL) {
287 if((!xmlStrcmp(fields->name, (const xmlChar *) "peer"))) {
288 xmlChar *ip = xmlNodeListGetString(doc, fields->children, 1);
289 if (current == NULL) {
291 common->peers = malloc(sizeof(ident_peer));
292 if (common->peers == NULL) {
295 common->peers->ip = inet_addr((const char *) ip);
296 common->peers->next = NULL;
297 common->peer_count += 1;
298 current = common->peers;
300 /* Add it to the list. */
301 current->next = malloc(sizeof(ident_peer));
302 if (current->next == NULL) {
305 current = current->next;
306 current->ip = inet_addr((const char *) ip);
307 common->peer_count += 1;
308 current->next = NULL;
311 } else if ((!xmlStrcmp(fields->name, (const xmlChar *) "zkhost"))) {
312 xmlChar *host = xmlNodeListGetString(doc, fields->children, 1);
314 common->zk_host = strdup((const char *) host);
315 if (common->zk_host == NULL) {
321 fields = fields->next;
324 if (common->peers == NULL) {
325 printlog(LOG_CRITICAL, "Must have at least one peer.\n");
329 if (common->membership == ZOOKEEPER && common->zk_host == NULL) {
330 printlog(LOG_CRITICAL, "Group membership protocol ZOOKEEPER requires a zkhost field.\n");
338 static ident_config *parse_machine(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
339 ident_config *common = malloc(sizeof(ident_config));
341 if (common == NULL) {
345 memset(common, 0, sizeof(ident_config));
346 if (parse_common(doc, ident, common)) {
351 /* No further information needed for machine-level identities. */
352 common->type = IDENT_MACHINE;
353 common->members = NULL;
356 if (configs->last_machine == NULL) {
357 configs->machines = common;
358 configs->last_machine = common;
360 configs->last_machine->next = common;
361 configs->last_machine = common;
364 configs->machine_count += 1;
369 static ident_config *parse_set(xmlDocPtr doc, xmlNodePtr ident, parsed_configs *configs) {
370 xmlNodePtr fields = ident->children;
371 ident_config *common = malloc(sizeof(ident_config));
372 ident_member *current = NULL;
374 if (common == NULL) {
378 memset(common, 0, sizeof(ident_config));
379 if (parse_common(doc, ident, common)) {
384 while (fields != NULL) {
385 ident_member *member = NULL;
387 if (!xmlStrcmp(fields->name, (const xmlChar *) "xid")) {
388 xmlChar *xid = xmlNodeListGetString(doc, fields->children, 1);
390 if (atoi((const char *) xid) >= 0) {
391 member = malloc(sizeof(ident_member));
392 if (member == NULL) {
397 member->type = MEMBER_XID;
398 sscanf((const char *) xid, "%x", &member->value);
407 } else if (!xmlStrcmp(fields->name, (const xmlChar *) "guid")) {
408 xmlChar *guid = xmlNodeListGetString(doc, fields->children, 1);
410 if (atoi((const char *) guid) >= 0) {
411 member = malloc(sizeof(ident_member));
412 if (member == NULL) {
417 member->type = MEMBER_GUID;
418 member->value = atoi((const char *) guid);
430 if (common->members == NULL) {
431 common->members = member;
434 current->next = member;
439 fields = fields->next;
442 /* A sliver set must have at least one member (xid or guid) or else it is
444 if (common->members == NULL) {
449 common->type = IDENT_SET;
452 if (configs->last_set == NULL) {
453 configs->sets = common;
454 configs->last_set = common;
456 configs->last_set->next = common;
457 configs->last_set = common;
460 configs->set_count += 1;
465 int parse_drl_config(const char *configfile, parsed_configs *configs) {
467 xmlNodePtr drl, ident;
469 configs->machines = NULL;
470 configs->sets = NULL;
471 configs->last_machine = NULL;
472 configs->last_set = NULL;
473 configs->machine_count = 0;
474 configs->set_count = 0;
476 if(!(doc = xmlParseFile(configfile))){
477 printlog(LOG_CRITICAL, "Config file (%s) not parsed successfully.\n", configfile);
482 if(!(drl = xmlDocGetRootElement(doc))){
483 printlog(LOG_CRITICAL, "Config file (%s) has no root element.\n", configfile);
487 if(xmlStrcmp(drl->name, (const xmlChar *) "drl")){
488 printlog(LOG_CRITICAL, "Config file (%s) of the wrong type, root node != drl\n", configfile);
493 ident = drl->children;
494 while(ident != NULL) {
495 ident_config *new = NULL;
497 if((!xmlStrcmp(ident->name, (const xmlChar *) "machine"))) {
498 new = parse_machine(doc, ident, configs);
499 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "set"))) {
500 new = parse_set(doc, ident, configs);
501 } else if ((!xmlStrcmp(ident->name, (const xmlChar *) "text"))) {
502 /* libxml seems to wrap everything inside two 'text's. */
508 /* FIXME: Make this more descriptive. :) */
509 printlog(LOG_CRITICAL, "Error occurred while parsing...\n");
511 free_ident_list(configs->machines);
512 free_ident_list(configs->sets);
514 configs->machines = NULL;
515 configs->sets = NULL;
516 configs->last_machine = NULL;
517 configs->last_set = NULL;
528 /* Return the list of parsed identities. */