1 /* See the DRL-LICENSE file for this file's software license. */
4 * ulogd output target for DRL: GRD and FPS
6 * Ken Yocum <kyocum@cs.ucsd.edu>
8 * Original shell of this code from ulogd_NETFLOW
9 * Like that code, we keep track of per-slice data rates
10 * out of this slice. Thus we are rate limiting particular slices
11 * across multiple boxes, ensuring that their outbound rate does not
12 * exceed some fixed limit.
16 * Enforcer: linux drop percentage.
18 * This file reads packets from the netlink socket. It updates all
19 * the hashmaps which track how much data has arrived per flow.
20 * It starts two threads for this limiter.
21 * One thread handles periodic estimation.
22 * The other thread handles communication with other limiters.
27 * ulogd_DRL: attach to netlink socket, accept packets. replaces ratelimit.cc
28 * util.c: generic hashing functions, flow comparisons, sundry items.
29 * gossip.c: Recv gossip, send gossip.
30 * peer_comm.c: Thread to listen for updates from other limiters.
31 * estimate.c: Thread to calculate the local limits.
34 * Ken Yocum <kyocum@cs.ucsd.edu>
37 * Some code appropriated from ulogd_NETFLOW:
39 * Mark Huang <mlhuang@cs.princeton.edu>
40 * Copyright (C) 2004-2005 The Trustees of Princeton University
42 * Based on admindump.pl by Mic Bowman and Paul Brett
43 * Copyright (c) 2002 Intel Corporation
47 /* Enable GNU glibc extensions */
53 /* va_start() and friends */
59 /* strstr() and friends */
62 /* dirname() and basename() */
65 /* fork() and wait() */
66 #include <sys/types.h>
70 /* errno and assert() */
77 /* time() and friends */
82 #include <sys/socket.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
86 /* ICMP definitions */
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h>
93 /* pthread_create() */
99 /* Signal definitions - so that we can catch SIGHUP and update config. */
102 #include <ulogd/ulogd.h>
103 #include <ulogd/conffile.h>
105 /* Perhaps useful for files within vservers? */
106 #if !defined(STANDALONE) && HAVE_LIBPROPER
107 #include <proper/prop.h>
111 * Jenkins hash support
112 * lives in raterouter.h
116 #include "raterouter.h"
118 #include "ratetypes.h" /* needs util and pthread.h */
119 #include "calendar.h"
123 * /etc/ulogd.conf configuration options
124 * Add the config options for DRL.
127 static config_entry_t create_htb = {
130 .type = CONFIG_TYPE_INT,
131 .options = CONFIG_OPT_NONE,
135 static config_entry_t enforce_on = {
138 .type = CONFIG_TYPE_INT,
139 .options = CONFIG_OPT_NONE,
143 static config_entry_t partition = {
145 .key = "partition_set",
146 .type = CONFIG_TYPE_INT,
147 .options = CONFIG_OPT_NONE,
148 .u = { .value = 0xfffffff },
151 static config_entry_t sfq_slice = {
154 .type = CONFIG_TYPE_STRING,
155 .options = CONFIG_OPT_NONE,
156 .u = { .string = "NONE" },
159 static config_entry_t netem_slice = {
161 .key = "netem_slice",
162 .type = CONFIG_TYPE_STRING,
163 .options = CONFIG_OPT_NONE,
164 .u = { .string = "ALL" },
167 static config_entry_t netem_loss = {
168 .next = &netem_slice,
170 .type = CONFIG_TYPE_INT,
171 .options = CONFIG_OPT_NONE,
175 static config_entry_t netem_delay = {
177 .key = "netem_delay",
178 .type = CONFIG_TYPE_INT,
179 .options = CONFIG_OPT_NONE,
183 static config_entry_t drl_configfile = {
184 .next = &netem_delay,
185 .key = "drl_configfile",
186 .type = CONFIG_TYPE_STRING,
187 .options = CONFIG_OPT_MANDATORY,
188 .u = { .string = "drl.xml" },
191 /** The administrative bandwidth limit (mbps) for the local node. The node
192 * will not set a limit higher than this, even when distributed capacity is
193 * available. Set to 0 for no limit. */
194 static config_entry_t nodelimit = {
195 .next = &drl_configfile,
197 .type = CONFIG_TYPE_INT,
198 .options = CONFIG_OPT_MANDATORY,
202 /** Determines the verbosity of logging. */
203 static config_entry_t drl_loglevel = {
205 .key = "drl_loglevel",
206 .type = CONFIG_TYPE_INT,
207 .options = CONFIG_OPT_MANDATORY,
208 .u = { .value = LOG_WARN },
211 /** The path of the logfile. */
212 static config_entry_t drl_logfile = {
213 .next = &drl_loglevel,
214 .key = "drl_logfile",
215 .type = CONFIG_TYPE_STRING,
216 .options = CONFIG_OPT_MANDATORY,
217 .u = { .string = "drl_logfile.log" },
220 /** The choice of DRL protocol. */
221 static config_entry_t policy = {
222 .next = &drl_logfile,
224 .type = CONFIG_TYPE_STRING,
225 .options = CONFIG_OPT_MANDATORY,
226 .u = { .string = "GRD" },
229 /** The estimate interval, in milliseconds. */
230 static config_entry_t estintms = {
233 .type = CONFIG_TYPE_INT,
234 .options = CONFIG_OPT_MANDATORY,
235 .u = { .value = 100 },
238 #define config_entries (&estintms)
241 * Debug functionality
248 #define NIPQUAD(addr) \
249 ((unsigned char *)&addr)[0], \
250 ((unsigned char *)&addr)[1], \
251 ((unsigned char *)&addr)[2], \
252 ((unsigned char *)&addr)[3]
254 #define IPQUAD(addr) \
255 ((unsigned char *)&addr)[3], \
256 ((unsigned char *)&addr)[2], \
257 ((unsigned char *)&addr)[1], \
258 ((unsigned char *)&addr)[0]
262 /* Salt for the hash functions */
266 * Hash slice name lookups on context ID.
269 /* Special context IDs */
270 #define UNKNOWN_XID -1
274 CONNECTION_REFUSED_XID = 65536, /* MAX_S_CONTEXT + 1 */
281 pthread_t estimate_thread;
282 pthread_t signal_thread;
283 pthread_t comm_thread;
284 uint32_t local_ip = 0;
286 extern FILE *logfile;
287 extern uint8_t system_loglevel;
288 extern uint8_t do_enforcement;
290 /* Used to simulate partitions. */
291 int do_partition = 0;
292 int partition_set = 0xfffffff;
296 static inline uint32_t
297 hash_flow(uint8_t protocol, uint32_t src_ip, uint16_t src_port, uint32_t dst_ip, uint16_t dst_port, uint32_t hash_max)
299 unsigned char mybytes[FLOWKEYSIZE];
300 mybytes[0] = protocol;
301 *(uint32_t*)(&(mybytes[1])) = src_ip;
302 *(uint32_t*)(&(mybytes[5])) = dst_ip;
303 *(uint32_t*)(&(mybytes[9])) = (src_port << 16) | dst_port;
304 return jhash(mybytes,FLOWKEYSIZE,salt) & (hash_max - 1);
307 uint32_t sampled_hasher(const key_flow *key) {
308 /* Last arg is UINT_MAX because sampled flow keeps track of its own capacity. */
309 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, UINT_MAX);
312 uint32_t standard_hasher(const key_flow *key) {
313 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, STD_FLOW_HASH_SIZE);
316 uint32_t multiple_hasher(const key_flow *key) {
317 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, MUL_FLOW_HASH_SIZE);
325 /* Interesting keys */
349 #define INTR_IDS (sizeof(intr_ids)/sizeof(intr_ids[0]))
350 static struct intr_id intr_ids[] = {
351 [OOB_TIME_SEC] = { "oob.time.sec", 0 },
352 [OOB_MARK] = { "oob.mark", 0 },
353 [IP_SADDR] = { "ip.saddr", 0 },
354 [IP_DADDR] = { "ip.daddr", 0 },
355 [IP_TOTLEN] = { "ip.totlen", 0 },
356 [IP_PROTOCOL] = { "ip.protocol", 0 },
357 [TCP_SPORT] = { "tcp.sport", 0 },
358 [TCP_DPORT] { "tcp.dport", 0 },
359 [TCP_ACK] = { "tcp.ack", 0 },
360 [TCP_FIN] = { "tcp.fin", 0 },
361 [TCP_SYN] = { "tcp.syn", 0 },
362 [TCP_RST] = { "tcp.rst", 0 },
363 [UDP_SPORT] = { "udp.sport", 0 },
364 [UDP_DPORT] = { "udp.dport", 0 },
365 [ICMP_TYPE] = { "icmp.type", 0 },
366 [ICMP_CODE] = { "icmp.code", 0 },
367 [GRE_FLAG_KEY] = { "gre.flag.key", 0 },
368 [GRE_VERSION] = { "gre.version", 0 },
369 [GRE_KEY] = { "gre.key", 0 },
370 [PPTP_CALLID] = { "pptp.callid", 0 },
373 #define GET_VALUE(x) intr_ids[x].res->value
375 #define DATE(t) ((t) / (24*60*60) * (24*60*60))
377 static int _output_drl(ulog_iret_t *res)
380 uint32_t src_ip, dst_ip;
381 uint16_t src_port, dst_port;
388 protocol = GET_VALUE(IP_PROTOCOL).ui8;
389 src_ip = GET_VALUE(IP_SADDR).ui32;
390 dst_ip = GET_VALUE(IP_DADDR).ui32;
391 xid = GET_VALUE(OOB_MARK).ui32;
396 src_port = GET_VALUE(TCP_SPORT).ui16;
397 dst_port = GET_VALUE(TCP_DPORT).ui16;
401 /* netflow had an issue with many udp flows and set
402 * src_port=0 to handle it. We don't.
404 src_port = GET_VALUE(UDP_SPORT).ui16;
407 * traceroutes create a large number of flows in the db
408 * this is a quick hack to catch the most common form
409 * of traceroute (basically we're mapping any UDP packet
410 * in the 33435-33524 range to the "trace" port, 33524 is
411 * 3 packets * nhops (30).
413 dst_port = GET_VALUE(UDP_DPORT).ui16;
414 if (dst_port >= 33435 && dst_port <= 33524)
419 src_port = GET_VALUE(ICMP_TYPE).ui8;
420 dst_port = GET_VALUE(ICMP_CODE).ui8;
423 * We special case some of the ICMP traffic that the kernel
424 * always generates. Since this is attributed to root, it
425 * creates significant "noise" in the output. We want to be
426 * able to quickly see that root is generating traffic.
428 if (xid == ROOT_XID) {
429 if (src_port == ICMP_ECHOREPLY)
430 xid = ICMP_ECHOREPLY_XID;
431 else if (src_port == ICMP_UNREACH)
432 xid = ICMP_UNREACH_XID;
437 if (GET_VALUE(GRE_FLAG_KEY).b) {
438 if (GET_VALUE(GRE_VERSION).ui8 == 1) {
439 /* Get PPTP call ID */
440 src_port = GET_VALUE(PPTP_CALLID).ui16;
442 /* XXX Truncate GRE keys to 16 bits */
443 src_port = (uint16_t) GET_VALUE(GRE_KEY).ui32;
446 /* No key available */
453 /* This is the default key for packets from unsupported protocols */
459 key.protocol = protocol;
460 key.source_ip = src_ip;
461 key.dest_ip = dst_ip;
462 key.source_port = src_port;
463 key.dest_port = dst_port;
464 key.packet_size = GET_VALUE(IP_TOTLEN).ui16;
465 key.packet_time = (time_t) GET_VALUE(OOB_TIME_SEC).ui32;
467 pthread_rwlock_rdlock(&limiter.limiter_lock); /* CLUNK! */
469 leaf = (leaf_t *) map_search(limiter.stable_instance.leaf_map, &xid, sizeof(xid));
471 /* Even if the packet doesn't match any specific xid, it should still
472 * count in the machine-type tables. This catches root (xid == 0) and
473 * unclassified (xid = fff) packets, which don't have map entries. */
475 ident = limiter.stable_instance.last_machine;
477 ident = leaf->parent;
481 pthread_mutex_lock(&ident->table_mutex);
483 /* Update the identity's table. */
484 ident->table_sample_function(ident->table, &key);
486 #ifdef SHADOW_ACCTING
488 /* Update the shadow perfect copy of the accounting table. */
489 standard_table_sample((standard_flow_table) ident->shadow_table, &key);
493 pthread_mutex_unlock(&ident->table_mutex);
495 ident = ident->parent;
498 pthread_rwlock_unlock(&limiter.limiter_lock); /* CLINK! */
503 /* get all key id's for the keys we are intrested in */
504 static int get_ids(void)
507 struct intr_id *cur_id;
509 for (i = 0; i < INTR_IDS; i++) {
510 cur_id = &intr_ids[i];
511 cur_id->res = keyh_getres(keyh_getid(cur_id->name));
513 ulogd_log(ULOGD_ERROR,
514 "Cannot resolve keyhash id for %s\n",
522 static void free_identity(identity_t *ident) {
524 free_comm(&ident->comm);
527 ident->table_destroy_function(ident->table);
530 if (ident->loop_action) {
531 ident->loop_action->valid = 0;
534 if (ident->comm_action) {
535 ident->comm_action->valid = 0;
538 pthread_mutex_destroy(&ident->table_mutex);
544 static void free_identity_map(map_handle map) {
545 identity_t *tofree = NULL;
547 map_reset_iterate(map);
548 while ((tofree = (identity_t *) map_next(map))) {
549 free_identity(tofree);
555 static void free_instance(drl_instance_t *instance) {
556 if (instance->leaves)
557 free(instance->leaves);
558 if (instance->leaf_map)
559 free_map(instance->leaf_map, 0);
560 if (instance->ident_map)
561 free_identity_map(instance->ident_map);
562 if (instance->machines)
563 free(instance->machines);
565 free(instance->sets);
567 /* FIXME: Drain the calendar first and free all the entries. */
572 memset(instance, 0, sizeof(drl_instance_t));
575 static void free_failed_config(parsed_configs configs, drl_instance_t *instance) {
577 if (configs.machines)
578 free_ident_list(configs.machines);
580 free_ident_list(configs.sets);
584 free_instance(instance);
587 static identity_t *new_identity(ident_config *config) {
588 identity_t *ident = malloc(sizeof(identity_t));
589 remote_node_t *comm_nodes = malloc(sizeof(remote_node_t)*config->peer_count);
590 ident_peer *peer = config->peers;
597 if (comm_nodes == NULL) {
602 memset(ident, 0, sizeof(identity_t));
603 memset(comm_nodes, 0, config->peer_count * sizeof(remote_node_t));
605 ident->id = config->id;
606 ident->limit = (uint32_t) (((double) config->limit * 1000.0) / 8.0);
607 ident->fixed_ewma_weight = config->fixed_ewma_weight;
608 ident->communication_intervals = config->communication_intervals;
609 ident->mainloop_intervals = config->mainloop_intervals;
610 ident->ewma_weight = pow(ident->fixed_ewma_weight,
611 (limiter.estintms/1000.0) * config->mainloop_intervals);
612 ident->parent = NULL;
613 ident->independent = config->independent;
615 pthread_mutex_init(&ident->table_mutex, NULL);
616 switch (config->accounting) {
619 standard_table_create(standard_hasher, &ident->common);
621 /* Ugly function pointer casting. Makes things sufficiently
622 * generic, though. */
623 ident->table_sample_function =
624 (int (*)(void *, const key_flow *)) standard_table_sample;
625 ident->table_cleanup_function =
626 (int (*)(void *)) standard_table_cleanup;
627 ident->table_update_function =
628 (void (*)(void *, struct timeval, double)) standard_table_update_flows;
629 ident->table_destroy_function =
630 (void (*)(void *)) standard_table_destroy;
635 multiple_table_create(multiple_hasher, MUL_INTERVAL_COUNT, &ident->common);
637 ident->table_sample_function =
638 (int (*)(void *, const key_flow *)) multiple_table_sample;
639 ident->table_cleanup_function =
640 (int (*)(void *)) multiple_table_cleanup;
641 ident->table_update_function =
642 (void (*)(void *, struct timeval, double)) multiple_table_update_flows;
643 ident->table_destroy_function =
644 (void (*)(void *)) multiple_table_destroy;
648 ident->table = sampled_table_create(sampled_hasher,
649 ident->limit * IDENT_CLEAN_INTERVAL,
650 SAMPLEHOLD_PERCENTAGE, SAMPLEHOLD_OVERFACTOR, &ident->common);
652 ident->table_sample_function =
653 (int (*)(void *, const key_flow *)) sampled_table_sample;
654 ident->table_cleanup_function =
655 (int (*)(void *)) sampled_table_cleanup;
656 ident->table_update_function =
657 (void (*)(void *, struct timeval, double)) sampled_table_update_flows;
658 ident->table_destroy_function =
659 (void (*)(void *)) sampled_table_destroy;
663 ident->table = simple_table_create(&ident->common);
665 ident->table_sample_function =
666 (int (*)(void *, const key_flow *)) simple_table_sample;
667 ident->table_cleanup_function =
668 (int (*)(void *)) simple_table_cleanup;
669 ident->table_update_function =
670 (void (*)(void *, struct timeval, double)) simple_table_update_flows;
671 ident->table_destroy_function =
672 (void (*)(void *)) simple_table_destroy;
676 #ifdef SHADOW_ACCTING
678 ident->shadow_table = standard_table_create(standard_hasher, &ident->shadow_common);
680 if (ident->shadow_table == NULL) {
681 ident->table_destroy_function(ident->table);
688 /* Make sure the table was allocated. */
689 if (ident->table == NULL) {
695 comm_nodes[peer_slot].addr = peer->ip;
696 comm_nodes[peer_slot].port = htons(LIMITER_LISTEN_PORT);
701 if (new_comm(&ident->comm, config, comm_nodes)) {
702 printlog(LOG_CRITICAL, "Failed to create communication structure.\n");
706 ident->comm.remote_nodes = comm_nodes;
708 if (!create_htb.u.value) {
709 ident->htb_node = config->htb_node;
710 ident->htb_parent = config->htb_parent;
716 /* Determines the validity of the parameters of one ident_config.
721 static int validate_config(ident_config *config) {
722 /* Limit must be a positive integer. */
723 if (config->limit < 1) {
727 /* Commfabric must be a valid choice (COMM_MESH or COMM_GOSSIP). */
728 if (config->commfabric != COMM_MESH &&
729 config->commfabric != COMM_GOSSIP) {
733 /* If commfabric is COMM_GOSSIP, this must be a positive integer. */
734 if (config->commfabric == COMM_GOSSIP && config->branch < 1) {
738 /* Accounting must be a valid choice (ACT_STANDARD, ACT_SAMPLEHOLD,
739 * ACT_SIMPLE, ACT_MULTIPLE). */
740 if (config->accounting != ACT_STANDARD &&
741 config->accounting != ACT_SAMPLEHOLD &&
742 config->accounting != ACT_SIMPLE &&
743 config->accounting != ACT_MULTIPLE) {
747 /* Ewma weight must be greater than or equal to zero. */
748 if (config->fixed_ewma_weight < 0) {
752 if (!create_htb.u.value) {
753 if (config->htb_node < 0 || config->htb_parent < 0) {
754 printlog(LOG_CRITICAL, "When create_htb is disabled in ulogd.conf, an identity must specify the htb_node and htb_parent propertities in its configuration.\n");
758 if (config->htb_node > -1 || config->htb_parent > -1) {
759 printlog(LOG_WARN, "htb_node or htb_parent are configured but ignored because we're configured to create our own htb hierarchy.\n");
763 /* Note: Parsing stage requires that each ident has at least one peer. */
770 static int validate_configs(parsed_configs configs, drl_instance_t *instance) {
772 ident_config *mlist = configs.machines;
773 ident_config *slist = configs.sets;
774 ident_config *tmp = NULL;
778 /* ID must be non-zero and unique. */
779 /* This is ugly and hackish, but this function will be called rarely.
780 * I'm tired of trying to be clever. */
782 printlog(LOG_CRITICAL, "Negative ident id: %d (%x) ?\n", mlist->id, mlist->id);
787 if (mlist->id == tmp->id) {
788 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", mlist->id, mlist->id);
795 if (mlist->id == tmp->id) {
796 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", mlist->id, mlist->id);
802 if (validate_config(mlist)) {
803 printlog(LOG_CRITICAL, "Invalid ident parameters for id: %d (%x)\n", mlist->id, mlist->id);
807 if (mlist->independent) {
808 printlog(LOG_CRITICAL, "Makes no sense to have independent machine node - setting independent to false.\n");
809 mlist->independent = 0;
815 instance->sets = malloc(configs.set_count * sizeof(identity_t *));
816 if (instance->sets == NULL) {
817 printlog(LOG_CRITICAL, "Not enough memory to allocate set identity collection.\n");
821 memset(instance->sets, 0, configs.set_count * sizeof(identity_t *));
822 instance->set_count = configs.set_count;
824 /* For sets, make sure that the hierarchy is valid. */
826 ident_member *members = slist->members;
828 /* ID must be non-zero and unique. */
830 printlog(LOG_CRITICAL, "Negative ident id: %d (%x) ?\n", slist->id, slist->id);
835 if (slist->id == tmp->id) {
836 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", slist->id, slist->id);
842 if (validate_config(slist)) {
843 printlog(LOG_CRITICAL, "Invalid ident parameters for id: %d (%x)\n", slist->id, slist->id);
847 /* Allocate an identity_t for this set-type identity. */
848 instance->sets[i] = new_identity(slist);
850 if (instance->sets[i] == NULL) {
851 printlog(LOG_CRITICAL, "Not enough memory to allocate set identity.\n");
855 /* Loop through children and look up each in leaf or ident map
856 * depending on the type of child. Set the child's parent pointer
857 * to the identity we just created above, unless it is already set,
858 * in which case we have an error. */
860 identity_t *child_ident = NULL;
861 leaf_t *child_leaf = NULL;
863 switch (members->type) {
865 child_leaf = map_search(instance->leaf_map, &members->value,
866 sizeof(members->value));
867 if (child_leaf == NULL) {
868 printlog(LOG_CRITICAL, "xid: child leaf not found.\n");
871 if (child_leaf->parent != NULL) {
872 /* Error - This leaf already has a parent. */
873 printlog(LOG_CRITICAL, "xid: child already has a parent.\n");
876 child_leaf->parent = instance->sets[i];
879 child_ident = map_search(instance->ident_map, &members->value,
880 sizeof(members->value));
881 if (child_ident == NULL) {
882 printlog(LOG_CRITICAL, "guid: child identity not found.\n");
885 if (child_ident->parent != NULL) {
886 /* Error - This identity already has a parent. */
887 printlog(LOG_CRITICAL, "guid: child identity already has a parent.\n");
890 child_ident->parent = instance->sets[i];
893 /* Error - shouldn't be possible. */
896 members = members->next;
899 map_insert(instance->ident_map, &instance->sets[i]->id,
900 sizeof(instance->sets[i]->id), instance->sets[i]);
908 static int fill_set_leaf_pointer(drl_instance_t *instance, identity_t *ident) {
910 identity_t *current_ident;
911 leaf_t *current_leaf;
912 leaf_t **leaves = malloc(instance->leaf_count * sizeof(leaf_t *));
913 if (leaves == NULL) {
917 map_reset_iterate(instance->leaf_map);
918 while ((current_leaf = (leaf_t *) map_next(instance->leaf_map))) {
919 current_ident = current_leaf->parent;
920 while (current_ident != NULL && current_ident != instance->last_machine) {
921 if (current_ident == ident) {
922 /* Found the ident we were looking for - add the leaf. */
923 leaves[count] = current_leaf;
927 current_ident = current_ident->parent;
931 ident->leaves = leaves;
932 ident->leaf_count = count;
937 static int init_identities(parsed_configs configs, drl_instance_t *instance) {
939 ident_config *config = configs.machines;
942 instance->cal = malloc(sizeof(struct ident_calendar) * SCHEDLEN);
944 if (instance->cal == NULL) {
948 for (i = 0; i < SCHEDLEN; ++i) {
949 TAILQ_INIT(instance->cal + i);
951 instance->cal_slot = 0;
953 instance->machines = malloc(configs.machine_count * sizeof(drl_instance_t *));
955 if (instance->machines == NULL) {
959 memset(instance->machines, 0, configs.machine_count * sizeof(drl_instance_t *));
960 instance->machine_count = configs.machine_count;
962 /* Allocate and add the machine identities. */
963 for (i = 0; i < configs.machine_count; ++i) {
964 identity_action *loop_action;
965 identity_action *comm_action;
966 instance->machines[i] = new_identity(config);
968 if (instance->machines[i] == NULL) {
972 loop_action = malloc(sizeof(identity_action));
973 comm_action = malloc(sizeof(identity_action));
975 if (loop_action == NULL || comm_action == NULL) {
979 /* The first has no parent - it is the root. All others have the
980 * previous ident as their parent. */
982 instance->machines[i]->parent = NULL;
984 instance->machines[i]->parent = instance->machines[i - 1];
987 instance->last_machine = instance->machines[i];
989 /* Add the ident to the guid->ident map. */
990 map_insert(instance->ident_map, &instance->machines[i]->id,
991 sizeof(instance->machines[i]->id), instance->machines[i]);
993 config = config->next;
995 memset(loop_action, 0, sizeof(identity_action));
996 memset(comm_action, 0, sizeof(identity_action));
997 loop_action->ident = instance->machines[i];
998 loop_action->action = ACTION_MAINLOOP;
999 loop_action->valid = 1;
1000 comm_action->ident = instance->machines[i];
1001 comm_action->action = ACTION_COMMUNICATE;
1002 comm_action->valid = 1;
1004 instance->machines[i]->loop_action = loop_action;
1005 instance->machines[i]->comm_action = comm_action;
1007 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1008 loop_action, calendar);
1010 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1011 comm_action, calendar);
1013 /* Setup the array of pointers to leaves. This is easy for machines
1014 * because a machine node applies to every leaf. */
1015 instance->machines[i]->leaves =
1016 malloc(instance->leaf_count * sizeof(leaf_t *));
1017 if (instance->machines[i]->leaves == NULL) {
1020 instance->machines[i]->leaf_count = instance->leaf_count;
1021 for (j = 0; j < instance->leaf_count; ++j) {
1022 instance->machines[i]->leaves[j] = &instance->leaves[j];
1026 /* Connect the set subtree to the machines. Any set or leaf without a
1027 * parent will take the last machine as its parent. */
1030 map_reset_iterate(instance->leaf_map);
1031 while ((leaf = (leaf_t *) map_next(instance->leaf_map))) {
1032 if (leaf->parent == NULL) {
1033 leaf->parent = instance->last_machine;
1038 for (i = 0; i < instance->set_count; ++i) {
1039 identity_action *loop_action;
1040 identity_action *comm_action;
1042 if (instance->sets[i]->parent == NULL && instance->sets[i]->independent == 0) {
1043 instance->sets[i]->parent = instance->last_machine;
1046 loop_action = malloc(sizeof(identity_action));
1047 comm_action = malloc(sizeof(identity_action));
1049 if (loop_action == NULL || comm_action == NULL) {
1053 memset(loop_action, 0, sizeof(identity_action));
1054 memset(comm_action, 0, sizeof(identity_action));
1055 loop_action->ident = instance->sets[i];
1056 loop_action->action = ACTION_MAINLOOP;
1057 loop_action->valid = 1;
1058 comm_action->ident = instance->sets[i];
1059 comm_action->action = ACTION_COMMUNICATE;
1060 comm_action->valid = 1;
1062 instance->sets[i]->loop_action = loop_action;
1063 instance->sets[i]->comm_action = comm_action;
1065 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1066 loop_action, calendar);
1068 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1069 comm_action, calendar);
1071 /* Setup the array of pointers to leaves. This is harder for sets,
1072 * but this doesn't need to be super-efficient because it happens
1073 * rarely and it isn't on the critical path for reconfig(). */
1074 if (fill_set_leaf_pointer(instance, instance->sets[i])) {
1083 static void print_instance(drl_instance_t *instance) {
1084 leaf_t *leaf = NULL;
1085 identity_t *ident = NULL;
1087 if (system_loglevel == LOG_DEBUG) {
1088 map_reset_iterate(instance->leaf_map);
1089 while ((leaf = (leaf_t *) map_next(instance->leaf_map))) {
1090 printf("%x:", leaf->xid);
1091 ident = leaf->parent;
1093 printf("%d:",ident->id);
1094 ident = ident->parent;
1096 printf("Leaf's parent pointer is %p\n", leaf->parent);
1099 printf("instance->last_machine is %p\n", instance->last_machine);
1103 static int assign_htb_hierarchy(drl_instance_t *instance) {
1105 int next_node = 0x100;
1107 /* If we're not going to create our own htb hierarchy (for instance,
1108 * if we're going to let PL's node manager do it for us), then we don't
1109 * want this function to do anything. */
1110 if (!create_htb.u.value) {
1111 printlog(LOG_DEBUG, "Skipping assign_htb_hierarchy becase ulogd.conf's create_htb set to 0.\n");
1115 /* Chain machine nodes under 1:10. */
1116 for (i = 0; i < instance->machine_count; ++i) {
1117 if (instance->machines[i]->parent == NULL) {
1119 instance->machines[i]->htb_parent = 0x10;
1122 instance->machines[i]->htb_parent =
1123 instance->machines[i]->parent->htb_node;
1126 instance->machines[i]->htb_node = next_node;
1132 /* Add set nodes under machine nodes. Iterate backwards to ensure parent is
1134 for (j = (instance->set_count - 1); j >= 0; --j) {
1135 if (instance->sets[j]->parent == NULL) {
1136 /* Independent node - goes under 0x10 away from machine nodes. */
1137 instance->sets[j]->htb_parent = 0x10;
1139 instance->sets[j]->htb_parent = instance->sets[j]->parent->htb_node;
1141 instance->sets[j]->htb_node = next_node;
1149 /* Added this so that I could comment one line and kill off all of the
1150 * command execution. */
1151 static inline int execute_cmd(const char *cmd) {
1155 static inline int add_htb_node(const char *iface, const uint32_t parent_major, const uint32_t parent_minor,
1156 const uint32_t classid_major, const uint32_t classid_minor,
1157 const uint64_t rate, const uint64_t ceil) {
1160 sprintf(cmd, "tc class add dev %s parent %x:%x classid %x:%x htb rate %llubit ceil %llubit",
1161 iface, parent_major, parent_minor, classid_major, classid_minor, rate, ceil);
1162 printlog(LOG_WARN, "INIT: HTB_cmd: %s\n", cmd);
1164 return execute_cmd(cmd);
1167 static inline int add_htb_netem(const char *iface, const uint32_t parent_major,
1168 const uint32_t parent_minor, const uint32_t handle,
1169 const int loss, const int delay) {
1172 sprintf(cmd, "/sbin/tc qdisc del dev %s parent %x:%x handle %x pfifo", iface, parent_major,
1173 parent_minor, handle);
1174 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1175 if (execute_cmd(cmd))
1176 printlog(LOG_DEBUG, "HTB_cmd: Previous deletion did not succeed.\n");
1178 sprintf(cmd, "/sbin/tc qdisc replace dev %s parent %x:%x handle %x netem loss %d%% delay %dms",
1179 iface, parent_major, parent_minor, handle, loss, delay);
1180 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1181 return execute_cmd(cmd);
1184 static inline int add_htb_sfq(const char *iface, const uint32_t parent_major,
1185 const uint32_t parent_minor, const uint32_t handle,
1186 const int perturb) {
1189 sprintf(cmd, "/sbin/tc qdisc del dev %s parent %x:%x handle %x pfifo", iface, parent_major,
1190 parent_minor, handle);
1191 printlog(LOG_WARN, "HTB_cmd: %s\n", cmd);
1192 if (execute_cmd(cmd))
1193 printlog(LOG_WARN, "HTB_cmd: Previous deletion did not succeed.\n");
1195 sprintf(cmd, "/sbin/tc qdisc replace dev %s parent %x:%x handle %x sfq perturb %d",
1196 iface, parent_major, parent_minor, handle, perturb);
1197 printlog(LOG_WARN, "HTB_cmd: %s\n", cmd);
1198 return execute_cmd(cmd);
1201 static int create_htb_hierarchy(drl_instance_t *instance) {
1204 uint64_t gigabit = 1024 * 1024 * 1024;
1206 /* If we're not going to create our own htb hierarchy (for instance,
1207 * if we're going to let PL's node manager do it for us), then we don't
1208 * want this function to do anything. */
1209 if (!create_htb.u.value) {
1210 printlog(LOG_DEBUG, "Skipping create_htb_hierarchy becase ulogd.conf's create_htb set to 0.\n");
1214 /* Nuke the hierarchy. */
1215 sprintf(cmd, "tc qdisc del dev eth0 root handle 1: htb");
1217 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1219 /* Re-initialize the basics. */
1220 sprintf(cmd, "tc qdisc add dev eth0 root handle 1: htb default 1fff");
1221 if (execute_cmd(cmd)) {
1224 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1226 if (add_htb_node("eth0", 1, 0, 1, 1, gigabit, gigabit))
1229 /* Add back 1:10. (Nodelimit : kilobits/sec -> bits/second)*/
1230 if (limiter.nodelimit) {
1231 if (add_htb_node("eth0", 1, 1, 1, 0x10, 8, (uint64_t) limiter.nodelimit * 1024))
1234 if (add_htb_node("eth0", 1, 1, 1, 0x10, 8, gigabit))
1239 for (i = 0; i < instance->machine_count; ++i) {
1240 if (add_htb_node("eth0", 1, instance->machines[i]->htb_parent, 1,
1241 instance->machines[i]->htb_node, 8, instance->machines[i]->limit * 1024)) {
1248 /* Add back 1:20. */
1250 if (instance->last_machine == NULL) {
1251 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:1 classid 1:20 htb rate 8bit ceil 1000mbit");
1253 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:%x classid 1:20 htb rate 8bit ceil 1000mbit",
1254 instance->last_machine->htb_node);
1257 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:1 classid 1:20 htb rate 8bit ceil 1000mbit");
1260 if (execute_cmd(cmd)) {
1263 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1266 for (j = (instance->set_count - 1); j >= 0; --j) {
1267 if (add_htb_node("eth0", 1, instance->sets[j]->htb_parent, 1,
1268 instance->sets[j]->htb_node, 8, instance->sets[j]->limit * 1024)) {
1273 /* Add leaves. FIXME: Set static sliver limit as ceil here! */
1274 for (k = 0; k < instance->leaf_count; ++k) {
1275 if (instance->leaves[k].parent == NULL) {
1276 if (add_htb_node("eth0", 1, 0x10, 1, (0x1000 | instance->leaves[k].xid), 8, gigabit))
1279 if (add_htb_node("eth0", 1, instance->leaves[k].parent->htb_node, 1, (0x1000 | instance->leaves[k].xid), 8, gigabit))
1283 /* Add exempt node for the leaf under 1:20 as 1:2<xid> */
1284 if (add_htb_node("eth0", 1, 0x20, 1, (0x2000 | instance->leaves[k].xid), 8, gigabit))
1288 /* Add 1:1000 and 1:2000 */
1289 if (instance->last_machine == NULL) {
1290 if (add_htb_node("eth0", 1, 0x10, 1, 0x1000, 8, gigabit))
1293 if (add_htb_node("eth0", 1, instance->last_machine->htb_node, 1, 0x1000, 8, gigabit))
1297 if (add_htb_node("eth0", 1, 0x20, 1, 0x2000, 8, gigabit))
1300 /* Add 1:1fff and 1:2fff */
1301 if (instance->last_machine == NULL) {
1302 if (add_htb_node("eth0", 1, 0x10, 1, 0x1fff, 8, gigabit))
1305 if (add_htb_node("eth0", 1, instance->last_machine->htb_node, 1, 0x1fff, 8, gigabit))
1309 if (add_htb_node("eth0", 1, 0x20, 1, 0x2fff, 8, gigabit))
1312 /* Artifical delay or loss for experimentation. */
1313 if (netem_delay.u.value || netem_loss.u.value) {
1314 if (!strcmp(netem_slice.u.string, "ALL")) {
1315 /* By default, netem applies to all leaves. */
1316 if (add_htb_netem("eth0", 1, 0x1000, 0x1000, netem_loss.u.value, netem_delay.u.value))
1318 if (add_htb_netem("eth0", 1, 0x1fff, 0x1fff, netem_loss.u.value, netem_delay.u.value))
1321 for (k = 0; k < instance->leaf_count; ++k) {
1322 if (add_htb_netem("eth0", 1, (0x1000 | instance->leaves[k].xid),
1323 (0x1000 | instance->leaves[k].xid), netem_loss.u.value, netem_delay.u.value)) {
1327 //FIXME: add exempt delay/loss here on 0x2000 ... ?
1330 /* netem_slice is not the default ALL value. Only apply netem
1331 * to the slice that is set in netem_slice.u.string. */
1334 sscanf(netem_slice.u.string, "%x", &slice_xid);
1336 if (add_htb_netem("eth0", 1, slice_xid, slice_xid, netem_loss.u.value, netem_delay.u.value))
1341 /* Turn on SFQ for experimentation. */
1342 if (strcmp(sfq_slice.u.string, "NONE")) {
1343 if (!strcmp(sfq_slice.u.string, "ALL")) {
1344 if (add_htb_sfq("eth0", 1, 0x1000, 0x1000, 30))
1346 if (add_htb_sfq("eth0", 1, 0x1fff, 0x1fff, 30))
1349 for (k = 0; k < instance->leaf_count; ++k) {
1350 if (add_htb_sfq("eth0", 1, (0x1000 | instance->leaves[k].xid),
1351 (0x1000 | instance->leaves[k].xid), 30)) {
1358 sscanf(sfq_slice.u.string, "%x", &slice_xid);
1360 if (add_htb_sfq("eth0", 1, slice_xid, slice_xid, 30))
1368 static int setup_tc_grd(drl_instance_t *instance) {
1372 for (i = 0; i < instance->leaf_count; ++i) {
1373 /* Delete the old pfifo qdisc that might have been there before. */
1374 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1%x handle 1%x pfifo",
1375 instance->leaves[i].xid, instance->leaves[i].xid);
1377 if (execute_cmd(cmd)) {
1378 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1381 /* Add the netem qdisc. */
1382 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1%x handle 1%x netem loss 0 delay 0ms",
1383 instance->leaves[i].xid, instance->leaves[i].xid);
1385 if (execute_cmd(cmd)) {
1386 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1391 /* Do the same for 1000 and 1fff. */
1392 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1000 handle 1000 pfifo");
1394 if (execute_cmd(cmd)) {
1395 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1398 /* Add the netem qdisc. */
1399 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1000 handle 1000 netem loss 0 delay 0ms");
1401 if (execute_cmd(cmd)) {
1402 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1406 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1fff handle 1fff pfifo");
1408 if (execute_cmd(cmd)) {
1409 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1412 /* Add the netem qdisc. */
1413 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1fff handle 1fff netem loss 0 delay 0ms");
1415 if (execute_cmd(cmd)) {
1416 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1420 /* Artifical delay or loss for experimentation. */
1421 if (netem_delay.u.value || netem_loss.u.value) {
1422 if (!strcmp(netem_slice.u.string, "ALL")) {
1423 sprintf(cmd, "/sbin/tc qdisc change dev eth0 parent 1:1000 handle 1000 netem loss %d delay %dms", netem_loss.u.value, netem_delay.u.value);
1424 if (execute_cmd(cmd)) {
1425 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1429 sprintf(cmd, "/sbin/tc qdisc change dev eth0 parent 1:1fff handle 1fff netem loss %d delay %dms", netem_loss.u.value, netem_delay.u.value);
1430 if (execute_cmd(cmd)) {
1431 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1435 for (j = 0; j < instance->leaf_count; ++j) {
1436 leaf_t *current = &instance->leaves[j];
1438 current->delay = netem_delay.u.value;
1440 sprintf(cmd, "/sbin/tc qdisc change dev eth0 parent 1:1%x handle 1%x netem loss %d delay %dms", current->xid, current->xid, netem_loss.u.value, netem_delay.u.value);
1442 if (execute_cmd(cmd)) {
1443 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1449 leaf_t *leaf = NULL;
1451 sscanf(netem_slice.u.string, "%x", &slice_xid);
1453 leaf = (leaf_t *) map_search(instance->leaf_map, &slice_xid, sizeof(slice_xid));
1456 /* Leaf not found - invalid selection. */
1457 printf("Your experimental setup is incorrect...\n");
1461 leaf->delay = netem_delay.u.value;
1463 sprintf(cmd, "/sbin/tc qdisc change dev eth0 parent 1:1%x handle 1%x netem loss %d delay %dms", slice_xid, slice_xid, netem_loss.u.value, netem_delay.u.value);
1465 if (execute_cmd(cmd)) {
1466 printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
1477 * Initialize this limiter with options
1478 * Open UDP socket for peer communication
1480 static int init_drl(void) {
1481 parsed_configs configs;
1482 struct sockaddr_in server_address;
1484 memset(&limiter, 0, sizeof(limiter_t));
1486 /* Setup logging. */
1487 system_loglevel = (uint8_t) drl_loglevel.u.value;
1488 logfile = fopen(drl_logfile.u.string, "w");
1490 if (logfile == NULL) {
1491 printf("Couldn't open logfile - ");
1496 printlog(LOG_CRITICAL, "ulogd_DRL initializing . . .\n");
1498 limiter.nodelimit = (uint32_t) (((double) nodelimit.u.value * 1000000.0) / 8.0);
1500 init_hashing(); /* for all hash maps */
1502 pthread_rwlock_init(&limiter.limiter_lock,NULL);
1504 /* determine our local IP by iterating through interfaces */
1505 if ((limiter.ip = get_local_ip())==0) {
1506 printlog(LOG_CRITICAL,
1507 "ulogd_DRL unable to aquire local IP address, not registering.\n");
1510 limiter.localaddr = inet_addr(limiter.ip);
1511 limiter.port = htons(LIMITER_LISTEN_PORT);
1512 limiter.udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
1513 if (limiter.udp_socket < 0) {
1514 printlog(LOG_CRITICAL, "Failed to create UDP socket().\n");
1518 memset(&server_address, 0, sizeof(server_address));
1519 server_address.sin_family = AF_INET;
1520 server_address.sin_addr.s_addr = limiter.localaddr;
1521 server_address.sin_port = limiter.port;
1523 if (bind(limiter.udp_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
1524 printlog(LOG_CRITICAL, "Failed to bind UDP socket.\n");
1528 printlog(LOG_WARN, " POLICY: %s\n",policy.u.string);
1529 if (strcasecmp(policy.u.string,"GRD") == 0) {
1530 limiter.policy = POLICY_GRD;
1531 } else if (strcasecmp(policy.u.string,"FPS") == 0) {
1532 limiter.policy = POLICY_FPS;
1534 printlog(LOG_CRITICAL,
1535 "Unknown DRL policy %s, aborting.\n",policy.u.string);
1539 limiter.estintms = estintms.u.value;
1540 if (limiter.estintms > 1000) {
1541 printlog(LOG_CRITICAL,
1542 "DRL: sorry estimate intervals must be less than 1 second.");
1543 printlog(LOG_CRITICAL,
1544 " Simple source mods will allow larger intervals. Using 1 second.\n");
1545 limiter.estintms = 1000;
1547 printlog(LOG_WARN, " Est interval: %dms\n",limiter.estintms);
1549 /* Acquire the big limiter lock for writing. Prevents pretty much
1550 * anything else from happening while the hierarchy is being changed. */
1551 pthread_rwlock_wrlock(&limiter.limiter_lock);
1553 limiter.stable_instance.ident_map = allocate_map();
1554 if (limiter.stable_instance.ident_map == NULL) {
1555 printlog(LOG_CRITICAL, "Failed to allocate memory for identity map.\n");
1559 if (get_eligible_leaves(&limiter.stable_instance)) {
1560 printlog(LOG_CRITICAL, "Failed to read eligigle leaves.\n");
1564 if (parse_drl_config(drl_configfile.u.string, &configs)) {
1565 /* Parse error occured. Return non-zero to notify init_drl(). */
1566 printlog(LOG_CRITICAL, "Failed to parse the DRL configuration file (%s).\n",
1567 drl_configfile.u.string);
1571 /* Validate identity hierarchy! */
1572 if (validate_configs(configs, &limiter.stable_instance)) {
1573 /* Clean up everything. */
1574 free_failed_config(configs, &limiter.stable_instance);
1575 printlog(LOG_CRITICAL, "Invalid DRL configuration file (%s).\n",
1576 drl_configfile.u.string);
1580 if (init_identities(configs, &limiter.stable_instance)) {
1581 free_failed_config(configs, &limiter.stable_instance);
1582 printlog(LOG_CRITICAL, "Failed to initialize identities.\n");
1586 /* At this point, we should be done with configs. */
1587 free_ident_list(configs.machines);
1588 free_ident_list(configs.sets);
1590 print_instance(&limiter.stable_instance);
1592 switch (limiter.policy) {
1594 if (assign_htb_hierarchy(&limiter.stable_instance)) {
1595 free_instance(&limiter.stable_instance);
1596 printlog(LOG_CRITICAL, "Failed to assign HTB hierarchy.\n");
1600 if (create_htb_hierarchy(&limiter.stable_instance)) {
1601 free_instance(&limiter.stable_instance);
1602 printlog(LOG_CRITICAL, "Failed to create HTB hierarchy.\n");
1608 if (setup_tc_grd(&limiter.stable_instance)) {
1609 free_instance(&limiter.stable_instance);
1610 printlog(LOG_CRITICAL, "Failed to initialize tc calls for GRD.\n");
1619 partition_set = partition.u.value;
1621 pthread_rwlock_unlock(&limiter.limiter_lock);
1623 if (pthread_create(&limiter.udp_recv_thread, NULL, limiter_receive_thread, NULL)) {
1624 printlog(LOG_CRITICAL, "Unable to start UDP receive thread.\n");
1628 printlog(LOG_WARN, "ulogd_DRL init finished.\n");
1633 static void reconfig() {
1634 parsed_configs configs;
1636 printlog(LOG_DEBUG, "--Starting reconfig()--\n");
1639 memset(&configs, 0, sizeof(parsed_configs));
1640 memset(&limiter.new_instance, 0, sizeof(drl_instance_t));
1642 limiter.new_instance.ident_map = allocate_map();
1643 if (limiter.new_instance.ident_map == NULL) {
1644 printlog(LOG_CRITICAL, "Failed to allocate ident_map during reconfig().\n");
1648 if (get_eligible_leaves(&limiter.new_instance)) {
1649 free_failed_config(configs, &limiter.new_instance);
1650 printlog(LOG_CRITICAL, "Failed to read leaves during reconfig().\n");
1654 if (parse_drl_config(drl_configfile.u.string, &configs)) {
1655 free_failed_config(configs, &limiter.new_instance);
1656 printlog(LOG_CRITICAL, "Failed to parse config during reconfig().\n");
1660 if (validate_configs(configs, &limiter.new_instance)) {
1661 free_failed_config(configs, &limiter.new_instance);
1662 printlog(LOG_CRITICAL, "Validation failed during reconfig().\n");
1663 pthread_rwlock_unlock(&limiter.limiter_lock);
1667 if (init_identities(configs, &limiter.new_instance)) {
1668 free_failed_config(configs, &limiter.new_instance);
1669 printlog(LOG_CRITICAL, "Initialization failed during reconfig().\n");
1670 pthread_rwlock_unlock(&limiter.limiter_lock);
1674 free_ident_list(configs.machines);
1675 free_ident_list(configs.sets);
1677 print_instance(&limiter.new_instance);
1680 pthread_rwlock_wrlock(&limiter.limiter_lock);
1682 switch (limiter.policy) {
1684 if (assign_htb_hierarchy(&limiter.new_instance)) {
1685 free_instance(&limiter.new_instance);
1686 printlog(LOG_CRITICAL, "Failed to assign HTB hierarchy during reconfig().\n");
1687 pthread_rwlock_unlock(&limiter.limiter_lock);
1691 if (create_htb_hierarchy(&limiter.new_instance)) {
1692 free_instance(&limiter.new_instance);
1693 printlog(LOG_CRITICAL, "Failed to create HTB hierarchy during reconfig().\n");
1695 /* Re-create old instance. */
1696 if (create_htb_hierarchy(&limiter.stable_instance)) {
1697 /* Error reinstating the old one - big problem. */
1698 printlog(LOG_CRITICAL, "Failed to reinstate HTB hierarchy during reconfig().\n");
1699 printlog(LOG_CRITICAL, "Giving up...\n");
1704 pthread_rwlock_unlock(&limiter.limiter_lock);
1710 if (setup_tc_grd(&limiter.new_instance)) {
1711 free_instance(&limiter.new_instance);
1712 printlog(LOG_CRITICAL, "GRD tc calls failed during reconfig().\n");
1714 /* Try to re-create old instance. */
1715 if (setup_tc_grd(&limiter.stable_instance)) {
1716 printlog(LOG_CRITICAL, "Failed to reinstate old GRD qdiscs during reconfig().\n");
1717 printlog(LOG_CRITICAL, "Giving up...\n");
1725 /* Should be impossible. */
1726 printf("Pigs are flying?\n");
1730 /* Switch over new to stable instance. */
1731 free_instance(&limiter.stable_instance);
1732 memcpy(&limiter.stable_instance, &limiter.new_instance, sizeof(drl_instance_t));
1734 /* Success! - Unlock */
1735 pthread_rwlock_unlock(&limiter.limiter_lock);
1738 static ulog_output_t drl_op = {
1740 .output = &_output_drl,
1741 .signal = NULL, /* This appears to be broken. Using my own handler. */
1746 /* Tests the amount of time it takes to call reconfig(). */
1747 static void time_reconfig(int iterations) {
1748 struct timeval start, end;
1751 gettimeofday(&start, NULL);
1752 for (i = 0; i < iterations; ++i) {
1755 gettimeofday(&end, NULL);
1757 printf("%d reconfigs() took %d seconds and %d microseconds.\n",
1758 iterations, end.tv_sec - start.tv_sec, end.tv_usec - start.tv_usec);
1761 // Seems to take about 85ms / iteration
1764 static int stop_enforcement(drl_instance_t *instance) {
1768 for (i = 0; i < instance->machine_count; ++i) {
1769 sprintf(cmd, "/sbin/tc class change dev eth0 parent 1:%x classid 1:%x htb rate 8bit ceil 100mbit",
1770 instance->machines[i]->htb_parent,
1771 instance->machines[i]->htb_node);
1773 if (execute_cmd(cmd)) {
1778 for (i = 0; i < instance->set_count; ++i) {
1779 sprintf(cmd, "/sbin/tc class change dev eth0 parent 1:%x classid 1:%x htb rate 8bit ceil 100mbit",
1780 instance->sets[i]->htb_parent,
1781 instance->sets[i]->htb_node);
1783 if (execute_cmd(cmd)) {
1791 static void *signal_thread_func(void *args) {
1797 sigaddset(&sigs, SIGHUP);
1798 sigaddset(&sigs, SIGUSR1);
1799 sigaddset(&sigs, SIGUSR2);
1800 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
1804 sigaddset(&sigs, SIGHUP);
1805 sigaddset(&sigs, SIGUSR1);
1806 sigaddset(&sigs, SIGUSR2);
1808 err = sigwait(&sigs, &sig);
1811 printlog(LOG_CRITICAL, "sigwait() returned an error.\n");
1817 printlog(LOG_WARN, "Caught SIGHUP - re-reading XML file.\n");
1819 //time_reconfig(1000); /* instrumentation */
1823 pthread_rwlock_wrlock(&limiter.limiter_lock);
1824 if (do_enforcement) {
1826 stop_enforcement(&limiter.stable_instance);
1827 printlog(LOG_CRITICAL, "--Switching enforcement off.--\n");
1830 printlog(LOG_CRITICAL, "--Switching enforcement on.--\n");
1832 pthread_rwlock_unlock(&limiter.limiter_lock);
1835 do_partition = !do_partition;
1838 /* Intentionally blank. */
1845 /* register output plugin with ulogd */
1846 static void _drl_reg_op(void)
1848 ulog_output_t *op = &drl_op;
1849 sigset_t signal_mask;
1851 sigemptyset(&signal_mask);
1852 sigaddset(&signal_mask, SIGHUP);
1853 sigaddset(&signal_mask, SIGUSR1);
1854 sigaddset(&signal_mask, SIGUSR2);
1855 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
1857 if (pthread_create(&signal_thread, NULL, &signal_thread_func, NULL) != 0) {
1858 printlog(LOG_CRITICAL, "Failed to create signal handling thread.\n");
1859 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1865 printlog(LOG_CRITICAL, "Init failed. :(\n");
1866 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1871 register_output(op);
1873 /* start up the thread that will periodically estimate the
1874 * local rate and set the local limits
1877 if (pthread_create(&estimate_thread, NULL, (void*(*)(void*)) &handle_estimation, &limiter)!=0) {
1878 printlog(LOG_CRITICAL, "Couldn't start estimate thread.\n");
1879 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1883 if (enforce_on.u.value) {
1884 pthread_rwlock_wrlock(&limiter.limiter_lock);
1886 printlog(LOG_CRITICAL, "--Switching enforcement on.--\n");
1887 pthread_rwlock_unlock(&limiter.limiter_lock);
1893 /* have the opts parsed */
1894 config_parse_file("DRL", config_entries);
1897 ulogd_log(ULOGD_ERROR, "can't resolve all keyhash id's\n");
1901 /* Seed the hash function */
1902 salt = getpid() ^ time(NULL);