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 partition = {
129 .key = "partition_set",
130 .type = CONFIG_TYPE_INT,
131 .options = CONFIG_OPT_NONE,
132 .u = { .value = 0xfffffff },
135 static config_entry_t netem_slice = {
137 .key = "netem_slice",
138 .type = CONFIG_TYPE_STRING,
139 .options = CONFIG_OPT_NONE,
140 .u = { .string = "ALL" },
143 static config_entry_t netem_loss = {
144 .next = &netem_slice,
146 .type = CONFIG_TYPE_INT,
147 .options = CONFIG_OPT_NONE,
151 static config_entry_t netem_delay = {
153 .key = "netem_delay",
154 .type = CONFIG_TYPE_INT,
155 .options = CONFIG_OPT_NONE,
159 static config_entry_t drl_configfile = {
160 .next = &netem_delay,
161 .key = "drl_configfile",
162 .type = CONFIG_TYPE_STRING,
163 .options = CONFIG_OPT_MANDATORY,
164 .u = { .string = "drl.xml" },
167 /** The administrative bandwidth limit (mbps) for the local node. The node
168 * will not set a limit higher than this, even when distributed capacity is
169 * available. Set to 0 for no limit. */
170 static config_entry_t nodelimit = {
171 .next = &drl_configfile,
173 .type = CONFIG_TYPE_INT,
174 .options = CONFIG_OPT_MANDATORY,
178 /** Determines the verbosity of logging. */
179 static config_entry_t drl_loglevel = {
181 .key = "drl_loglevel",
182 .type = CONFIG_TYPE_INT,
183 .options = CONFIG_OPT_MANDATORY,
184 .u = { .value = LOG_WARN },
187 /** The path of the logfile. */
188 static config_entry_t drl_logfile = {
189 .next = &drl_loglevel,
190 .key = "drl_logfile",
191 .type = CONFIG_TYPE_STRING,
192 .options = CONFIG_OPT_MANDATORY,
193 .u = { .string = "drl_logfile.log" },
196 /** The choice of DRL protocol. */
197 static config_entry_t policy = {
198 .next = &drl_logfile,
200 .type = CONFIG_TYPE_STRING,
201 .options = CONFIG_OPT_MANDATORY,
202 .u = { .string = "GRD" },
205 /** The estimate interval, in milliseconds. */
206 static config_entry_t estintms = {
209 .type = CONFIG_TYPE_INT,
210 .options = CONFIG_OPT_MANDATORY,
211 .u = { .value = 100 },
214 #define config_entries (&estintms)
217 * Debug functionality
224 #define NIPQUAD(addr) \
225 ((unsigned char *)&addr)[0], \
226 ((unsigned char *)&addr)[1], \
227 ((unsigned char *)&addr)[2], \
228 ((unsigned char *)&addr)[3]
230 #define IPQUAD(addr) \
231 ((unsigned char *)&addr)[3], \
232 ((unsigned char *)&addr)[2], \
233 ((unsigned char *)&addr)[1], \
234 ((unsigned char *)&addr)[0]
238 /* Salt for the hash functions */
242 * Hash slice name lookups on context ID.
245 /* Special context IDs */
246 #define UNKNOWN_XID -1
250 CONNECTION_REFUSED_XID = 65536, /* MAX_S_CONTEXT + 1 */
257 pthread_t estimate_thread;
258 pthread_t signal_thread;
259 pthread_t comm_thread;
260 uint32_t local_ip = 0;
262 extern FILE *logfile;
263 extern uint8_t system_loglevel;
264 extern uint8_t do_enforcement;
266 /* From peer_comm.c - used to simulate partition. */
267 extern int do_partition;
268 extern int partition_set;
272 static inline uint32_t
273 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)
275 unsigned char mybytes[FLOWKEYSIZE];
276 mybytes[0] = protocol;
277 *(uint32_t*)(&(mybytes[1])) = src_ip;
278 *(uint32_t*)(&(mybytes[5])) = dst_ip;
279 *(uint32_t*)(&(mybytes[9])) = (src_port << 16) | dst_port;
280 return jhash(mybytes,FLOWKEYSIZE,salt) & (hash_max - 1);
283 uint32_t sampled_hasher(const key_flow *key) {
284 /* Last arg is UINT_MAX because sampled flow keeps track of its own capacity. */
285 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, UINT_MAX);
288 uint32_t standard_hasher(const key_flow *key) {
289 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, STD_FLOW_HASH_SIZE);
292 uint32_t multiple_hasher(const key_flow *key) {
293 return hash_flow(key->protocol, key->source_ip, key->source_port, key->dest_ip, key->dest_port, MUL_FLOW_HASH_SIZE);
301 /* Interesting keys */
325 #define INTR_IDS (sizeof(intr_ids)/sizeof(intr_ids[0]))
326 static struct intr_id intr_ids[] = {
327 [OOB_TIME_SEC] = { "oob.time.sec", 0 },
328 [OOB_MARK] = { "oob.mark", 0 },
329 [IP_SADDR] = { "ip.saddr", 0 },
330 [IP_DADDR] = { "ip.daddr", 0 },
331 [IP_TOTLEN] = { "ip.totlen", 0 },
332 [IP_PROTOCOL] = { "ip.protocol", 0 },
333 [TCP_SPORT] = { "tcp.sport", 0 },
334 [TCP_DPORT] { "tcp.dport", 0 },
335 [TCP_ACK] = { "tcp.ack", 0 },
336 [TCP_FIN] = { "tcp.fin", 0 },
337 [TCP_SYN] = { "tcp.syn", 0 },
338 [TCP_RST] = { "tcp.rst", 0 },
339 [UDP_SPORT] = { "udp.sport", 0 },
340 [UDP_DPORT] = { "udp.dport", 0 },
341 [ICMP_TYPE] = { "icmp.type", 0 },
342 [ICMP_CODE] = { "icmp.code", 0 },
343 [GRE_FLAG_KEY] = { "gre.flag.key", 0 },
344 [GRE_VERSION] = { "gre.version", 0 },
345 [GRE_KEY] = { "gre.key", 0 },
346 [PPTP_CALLID] = { "pptp.callid", 0 },
349 #define GET_VALUE(x) intr_ids[x].res->value
351 #define DATE(t) ((t) / (24*60*60) * (24*60*60))
353 static int _output_drl(ulog_iret_t *res)
356 uint32_t src_ip, dst_ip;
357 uint16_t src_port, dst_port;
364 protocol = GET_VALUE(IP_PROTOCOL).ui8;
365 src_ip = GET_VALUE(IP_SADDR).ui32;
366 dst_ip = GET_VALUE(IP_DADDR).ui32;
367 xid = GET_VALUE(OOB_MARK).ui32;
372 src_port = GET_VALUE(TCP_SPORT).ui16;
373 dst_port = GET_VALUE(TCP_DPORT).ui16;
377 /* netflow had an issue with many udp flows and set
378 * src_port=0 to handle it. We don't.
380 src_port = GET_VALUE(UDP_SPORT).ui16;
383 * traceroutes create a large number of flows in the db
384 * this is a quick hack to catch the most common form
385 * of traceroute (basically we're mapping any UDP packet
386 * in the 33435-33524 range to the "trace" port, 33524 is
387 * 3 packets * nhops (30).
389 dst_port = GET_VALUE(UDP_DPORT).ui16;
390 if (dst_port >= 33435 && dst_port <= 33524)
395 src_port = GET_VALUE(ICMP_TYPE).ui8;
396 dst_port = GET_VALUE(ICMP_CODE).ui8;
399 * We special case some of the ICMP traffic that the kernel
400 * always generates. Since this is attributed to root, it
401 * creates significant "noise" in the output. We want to be
402 * able to quickly see that root is generating traffic.
404 if (xid == ROOT_XID) {
405 if (src_port == ICMP_ECHOREPLY)
406 xid = ICMP_ECHOREPLY_XID;
407 else if (src_port == ICMP_UNREACH)
408 xid = ICMP_UNREACH_XID;
413 if (GET_VALUE(GRE_FLAG_KEY).b) {
414 if (GET_VALUE(GRE_VERSION).ui8 == 1) {
415 /* Get PPTP call ID */
416 src_port = GET_VALUE(PPTP_CALLID).ui16;
418 /* XXX Truncate GRE keys to 16 bits */
419 src_port = (uint16_t) GET_VALUE(GRE_KEY).ui32;
422 /* No key available */
429 /* This is the default key for packets from unsupported protocols */
435 key.protocol = protocol;
436 key.source_ip = src_ip;
437 key.dest_ip = dst_ip;
438 key.source_port = src_port;
439 key.dest_port = dst_port;
440 key.packet_size = GET_VALUE(IP_TOTLEN).ui16;
441 key.packet_time = (time_t) GET_VALUE(OOB_TIME_SEC).ui32;
443 pthread_rwlock_rdlock(&limiter.limiter_lock); /* CLUNK! */
445 leaf = (leaf_t *) map_search(limiter.stable_instance.leaf_map, &xid, sizeof(xid));
447 /* Even if the packet doesn't match any specific xid, it should still
448 * count in the machine-type tables. This catches root (xid == 0) and
449 * unclassified (xid = fff) packets, which don't have map entries. */
451 ident = limiter.stable_instance.last_machine;
453 ident = leaf->parent;
457 pthread_mutex_lock(&ident->table_mutex);
459 /* Update the identity's table. */
460 ident->table_sample_function(ident->table, &key);
462 #ifdef SHADOW_ACCTING
464 /* Update the shadow perfect copy of the accounting table. */
465 standard_table_sample((standard_flow_table) ident->shadow_table, &key);
469 pthread_mutex_unlock(&ident->table_mutex);
471 ident = ident->parent;
474 pthread_rwlock_unlock(&limiter.limiter_lock); /* CLINK! */
479 /* get all key id's for the keys we are intrested in */
480 static int get_ids(void)
483 struct intr_id *cur_id;
485 for (i = 0; i < INTR_IDS; i++) {
486 cur_id = &intr_ids[i];
487 cur_id->res = keyh_getres(keyh_getid(cur_id->name));
489 ulogd_log(ULOGD_ERROR,
490 "Cannot resolve keyhash id for %s\n",
498 static void free_identity(identity_t *ident) {
500 free_comm(&ident->comm);
503 ident->table_destroy_function(ident->table);
506 if (ident->loop_action) {
507 ident->loop_action->valid = 0;
510 if (ident->comm_action) {
511 ident->comm_action->valid = 0;
514 pthread_mutex_destroy(&ident->table_mutex);
520 static void free_identity_map(map_handle map) {
521 identity_t *tofree = NULL;
523 map_reset_iterate(map);
524 while ((tofree = (identity_t *) map_next(map))) {
525 free_identity(tofree);
531 static void free_instance(drl_instance_t *instance) {
532 if (instance->leaves)
533 free(instance->leaves);
534 if (instance->leaf_map)
535 free_map(instance->leaf_map, 0);
536 if (instance->ident_map)
537 free_identity_map(instance->ident_map);
538 if (instance->machines)
539 free(instance->machines);
541 free(instance->sets);
543 /* FIXME: Drain the calendar first and free all the entries. */
548 memset(instance, 0, sizeof(drl_instance_t));
551 static void free_failed_config(parsed_configs configs, drl_instance_t *instance) {
553 if (configs.machines)
554 free_ident_list(configs.machines);
556 free_ident_list(configs.sets);
560 free_instance(instance);
563 static identity_t *new_identity(ident_config *config) {
564 identity_t *ident = malloc(sizeof(identity_t));
565 remote_node_t *comm_nodes = malloc(sizeof(remote_node_t)*config->peer_count);
566 ident_peer *peer = config->peers;
573 if (comm_nodes == NULL) {
578 memset(ident, 0, sizeof(identity_t));
579 memset(comm_nodes, 0, config->peer_count * sizeof(remote_node_t));
581 ident->id = config->id;
582 ident->limit = (uint32_t) (((double) config->limit * 1000.0) / 8.0);
583 ident->fixed_ewma_weight = config->fixed_ewma_weight;
584 ident->communication_intervals = config->communication_intervals;
585 ident->mainloop_intervals = config->mainloop_intervals;
586 ident->ewma_weight = pow(ident->fixed_ewma_weight,
587 (limiter.estintms/1000.0) * config->mainloop_intervals);
588 ident->parent = NULL;
589 ident->independent = config->independent;
591 pthread_mutex_init(&ident->table_mutex, NULL);
592 switch (config->accounting) {
595 standard_table_create(standard_hasher, &ident->common);
597 /* Ugly function pointer casting. Makes things sufficiently
598 * generic, though. */
599 ident->table_sample_function =
600 (int (*)(void *, const key_flow *)) standard_table_sample;
601 ident->table_cleanup_function =
602 (int (*)(void *)) standard_table_cleanup;
603 ident->table_update_function =
604 (void (*)(void *, struct timeval, double)) standard_table_update_flows;
605 ident->table_destroy_function =
606 (void (*)(void *)) standard_table_destroy;
611 multiple_table_create(multiple_hasher, MUL_INTERVAL_COUNT, &ident->common);
613 ident->table_sample_function =
614 (int (*)(void *, const key_flow *)) multiple_table_sample;
615 ident->table_cleanup_function =
616 (int (*)(void *)) multiple_table_cleanup;
617 ident->table_update_function =
618 (void (*)(void *, struct timeval, double)) multiple_table_update_flows;
619 ident->table_destroy_function =
620 (void (*)(void *)) multiple_table_destroy;
624 ident->table = sampled_table_create(sampled_hasher,
625 ident->limit * IDENT_CLEAN_INTERVAL,
626 SAMPLEHOLD_PERCENTAGE, SAMPLEHOLD_OVERFACTOR, &ident->common);
628 ident->table_sample_function =
629 (int (*)(void *, const key_flow *)) sampled_table_sample;
630 ident->table_cleanup_function =
631 (int (*)(void *)) sampled_table_cleanup;
632 ident->table_update_function =
633 (void (*)(void *, struct timeval, double)) sampled_table_update_flows;
634 ident->table_destroy_function =
635 (void (*)(void *)) sampled_table_destroy;
639 ident->table = simple_table_create(&ident->common);
641 ident->table_sample_function =
642 (int (*)(void *, const key_flow *)) simple_table_sample;
643 ident->table_cleanup_function =
644 (int (*)(void *)) simple_table_cleanup;
645 ident->table_update_function =
646 (void (*)(void *, struct timeval, double)) simple_table_update_flows;
647 ident->table_destroy_function =
648 (void (*)(void *)) simple_table_destroy;
652 #ifdef SHADOW_ACCTING
654 ident->shadow_table = standard_table_create(standard_hasher, &ident->shadow_common);
656 if (ident->shadow_table == NULL) {
657 ident->table_destroy_function(ident->table);
664 /* Make sure the table was allocated. */
665 if (ident->table == NULL) {
671 comm_nodes[peer_slot].addr = peer->ip;
672 comm_nodes[peer_slot].port = htons(LIMITER_LISTEN_PORT);
677 if (new_comm(&ident->comm, config, comm_nodes)) {
678 printlog(LOG_CRITICAL, "Failed to create communication structure.\n");
682 ident->comm.remote_nodes = comm_nodes;
687 /* Determines the validity of the parameters of one ident_config.
692 static int validate_config(ident_config *config) {
693 /* Limit must be a positive integer. */
694 if (config->limit < 1) {
698 /* Commfabric must be a valid choice (COMM_MESH or COMM_GOSSIP). */
699 if (config->commfabric != COMM_MESH &&
700 config->commfabric != COMM_GOSSIP) {
704 /* If commfabric is COMM_GOSSIP, this must be a positive integer. */
705 if (config->commfabric == COMM_GOSSIP && config->branch < 1) {
709 /* Accounting must be a valid choice (ACT_STANDARD, ACT_SAMPLEHOLD,
710 * ACT_SIMPLE, ACT_MULTIPLE). */
711 if (config->accounting != ACT_STANDARD &&
712 config->accounting != ACT_SAMPLEHOLD &&
713 config->accounting != ACT_SIMPLE &&
714 config->accounting != ACT_MULTIPLE) {
718 /* Ewma weight must be greater than or equal to zero. */
719 if (config->fixed_ewma_weight < 0) {
723 /* Note: Parsing stage requires that each ident has at least one peer. */
730 static int validate_configs(parsed_configs configs, drl_instance_t *instance) {
732 ident_config *mlist = configs.machines;
733 ident_config *slist = configs.sets;
734 ident_config *tmp = NULL;
738 /* ID must be non-zero and unique. */
739 /* This is ugly and hackish, but this function will be called rarely.
740 * I'm tired of trying to be clever. */
742 printlog(LOG_CRITICAL, "Negative ident id: %d (%x) ?\n", mlist->id, mlist->id);
747 if (mlist->id == tmp->id) {
748 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", mlist->id, mlist->id);
755 if (mlist->id == tmp->id) {
756 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", mlist->id, mlist->id);
762 if (validate_config(mlist)) {
763 printlog(LOG_CRITICAL, "Invalid ident parameters for id: %d (%x)\n", mlist->id, mlist->id);
767 if (mlist->independent) {
768 printlog(LOG_CRITICAL, "Makes no sense to have independent machine node - setting independent to false.\n");
769 mlist->independent = 0;
775 instance->sets = malloc(configs.set_count * sizeof(identity_t *));
776 if (instance->sets == NULL) {
777 printlog(LOG_CRITICAL, "Not enough memory to allocate set identity collection.\n");
781 memset(instance->sets, 0, configs.set_count * sizeof(identity_t *));
782 instance->set_count = configs.set_count;
784 /* For sets, make sure that the hierarchy is valid. */
786 ident_member *members = slist->members;
788 /* ID must be non-zero and unique. */
790 printlog(LOG_CRITICAL, "Negative ident id: %d (%x) ?\n", slist->id, slist->id);
795 if (slist->id == tmp->id) {
796 printlog(LOG_CRITICAL, "Duplicate ident id: %d (%x)\n", slist->id, slist->id);
802 if (validate_config(slist)) {
803 printlog(LOG_CRITICAL, "Invalid ident parameters for id: %d (%x)\n", slist->id, slist->id);
807 /* Allocate an identity_t for this set-type identity. */
808 instance->sets[i] = new_identity(slist);
810 if (instance->sets[i] == NULL) {
811 printlog(LOG_CRITICAL, "Not enough memory to allocate set identity.\n");
815 /* Loop through children and look up each in leaf or ident map
816 * depending on the type of child. Set the child's parent pointer
817 * to the identity we just created above, unless it is already set,
818 * in which case we have an error. */
820 identity_t *child_ident = NULL;
821 leaf_t *child_leaf = NULL;
823 switch (members->type) {
825 child_leaf = map_search(instance->leaf_map, &members->value,
826 sizeof(members->value));
827 if (child_leaf == NULL) {
828 printlog(LOG_CRITICAL, "xid: child leaf not found.\n");
831 if (child_leaf->parent != NULL) {
832 /* Error - This leaf already has a parent. */
833 printlog(LOG_CRITICAL, "xid: child already has a parent.\n");
836 child_leaf->parent = instance->sets[i];
839 child_ident = map_search(instance->ident_map, &members->value,
840 sizeof(members->value));
841 if (child_ident == NULL) {
842 printlog(LOG_CRITICAL, "guid: child identity not found.\n");
845 if (child_ident->parent != NULL) {
846 /* Error - This identity already has a parent. */
847 printlog(LOG_CRITICAL, "guid: child identity already has a parent.\n");
850 child_ident->parent = instance->sets[i];
853 /* Error - shouldn't be possible. */
856 members = members->next;
859 map_insert(instance->ident_map, &instance->sets[i]->id,
860 sizeof(instance->sets[i]->id), instance->sets[i]);
868 static int fill_set_leaf_pointer(drl_instance_t *instance, identity_t *ident) {
870 identity_t *current_ident;
871 leaf_t *current_leaf;
872 leaf_t **leaves = malloc(instance->leaf_count * sizeof(leaf_t *));
873 if (leaves == NULL) {
877 map_reset_iterate(instance->leaf_map);
878 while ((current_leaf = (leaf_t *) map_next(instance->leaf_map))) {
879 current_ident = current_leaf->parent;
880 while (current_ident != NULL && current_ident != instance->last_machine) {
881 if (current_ident == ident) {
882 /* Found the ident we were looking for - add the leaf. */
883 leaves[count] = current_leaf;
887 current_ident = current_ident->parent;
891 ident->leaves = leaves;
892 ident->leaf_count = count;
897 static int init_identities(parsed_configs configs, drl_instance_t *instance) {
899 ident_config *config = configs.machines;
902 instance->cal = malloc(sizeof(struct ident_calendar) * SCHEDLEN);
904 if (instance->cal == NULL) {
908 for (i = 0; i < SCHEDLEN; ++i) {
909 TAILQ_INIT(instance->cal + i);
911 instance->cal_slot = 0;
913 instance->machines = malloc(configs.machine_count * sizeof(drl_instance_t *));
915 if (instance->machines == NULL) {
919 memset(instance->machines, 0, configs.machine_count * sizeof(drl_instance_t *));
920 instance->machine_count = configs.machine_count;
922 /* Allocate and add the machine identities. */
923 for (i = 0; i < configs.machine_count; ++i) {
924 identity_action *loop_action;
925 identity_action *comm_action;
926 instance->machines[i] = new_identity(config);
928 if (instance->machines[i] == NULL) {
932 loop_action = malloc(sizeof(identity_action));
933 comm_action = malloc(sizeof(identity_action));
935 if (loop_action == NULL || comm_action == NULL) {
939 /* The first has no parent - it is the root. All others have the
940 * previous ident as their parent. */
942 instance->machines[i]->parent = NULL;
944 instance->machines[i]->parent = instance->machines[i - 1];
947 instance->last_machine = instance->machines[i];
949 /* Add the ident to the guid->ident map. */
950 map_insert(instance->ident_map, &instance->machines[i]->id,
951 sizeof(instance->machines[i]->id), instance->machines[i]);
953 config = config->next;
955 memset(loop_action, 0, sizeof(identity_action));
956 memset(comm_action, 0, sizeof(identity_action));
957 loop_action->ident = instance->machines[i];
958 loop_action->action = ACTION_MAINLOOP;
959 loop_action->valid = 1;
960 comm_action->ident = instance->machines[i];
961 comm_action->action = ACTION_COMMUNICATE;
962 comm_action->valid = 1;
964 instance->machines[i]->loop_action = loop_action;
965 instance->machines[i]->comm_action = comm_action;
967 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
968 loop_action, calendar);
970 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
971 comm_action, calendar);
973 /* Setup the array of pointers to leaves. This is easy for machines
974 * because a machine node applies to every leaf. */
975 instance->machines[i]->leaves =
976 malloc(instance->leaf_count * sizeof(leaf_t *));
977 if (instance->machines[i]->leaves == NULL) {
980 instance->machines[i]->leaf_count = instance->leaf_count;
981 for (j = 0; j < instance->leaf_count; ++j) {
982 instance->machines[i]->leaves[j] = &instance->leaves[j];
986 /* Connect the set subtree to the machines. Any set or leaf without a
987 * parent will take the last machine as its parent. */
990 map_reset_iterate(instance->leaf_map);
991 while ((leaf = (leaf_t *) map_next(instance->leaf_map))) {
992 if (leaf->parent == NULL) {
993 leaf->parent = instance->last_machine;
998 for (i = 0; i < instance->set_count; ++i) {
999 identity_action *loop_action;
1000 identity_action *comm_action;
1002 if (instance->sets[i]->parent == NULL && instance->sets[i]->independent == 0) {
1003 instance->sets[i]->parent = instance->last_machine;
1006 loop_action = malloc(sizeof(identity_action));
1007 comm_action = malloc(sizeof(identity_action));
1009 if (loop_action == NULL || comm_action == NULL) {
1013 memset(loop_action, 0, sizeof(identity_action));
1014 memset(comm_action, 0, sizeof(identity_action));
1015 loop_action->ident = instance->sets[i];
1016 loop_action->action = ACTION_MAINLOOP;
1017 loop_action->valid = 1;
1018 comm_action->ident = instance->sets[i];
1019 comm_action->action = ACTION_COMMUNICATE;
1020 comm_action->valid = 1;
1022 instance->sets[i]->loop_action = loop_action;
1023 instance->sets[i]->comm_action = comm_action;
1025 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1026 loop_action, calendar);
1028 TAILQ_INSERT_TAIL(instance->cal + (instance->cal_slot & SCHEDMASK),
1029 comm_action, calendar);
1031 /* Setup the array of pointers to leaves. This is harder for sets,
1032 * but this doesn't need to be super-efficient because it happens
1033 * rarely and it isn't on the critical path for reconfig(). */
1034 if (fill_set_leaf_pointer(instance, instance->sets[i])) {
1043 static void print_instance(drl_instance_t *instance) {
1044 leaf_t *leaf = NULL;
1045 identity_t *ident = NULL;
1047 if (system_loglevel == LOG_DEBUG) {
1048 map_reset_iterate(instance->leaf_map);
1049 while ((leaf = (leaf_t *) map_next(instance->leaf_map))) {
1050 printf("%x:", leaf->xid);
1051 ident = leaf->parent;
1053 printf("%d:",ident->id);
1054 ident = ident->parent;
1056 printf("Leaf's parent pointer is %p\n", leaf->parent);
1059 printf("instance->last_machine is %p\n", instance->last_machine);
1063 static int assign_htb_hierarchy(drl_instance_t *instance) {
1065 int next_node = 0x100;
1067 /* Chain machine nodes under 1:10. */
1068 for (i = 0; i < instance->machine_count; ++i) {
1069 if (instance->machines[i]->parent == NULL) {
1071 instance->machines[i]->htb_parent = 0x10;
1074 instance->machines[i]->htb_parent =
1075 instance->machines[i]->parent->htb_node;
1078 instance->machines[i]->htb_node = next_node;
1084 /* Add set nodes under machine nodes. Iterate backwards to ensure parent is
1086 for (j = (instance->set_count - 1); j >= 0; --j) {
1087 if (instance->sets[j]->parent == NULL) {
1088 /* Independent node - goes under 0x10 away from machine nodes. */
1089 instance->sets[j]->htb_parent = 0x10;
1091 instance->sets[j]->htb_parent = instance->sets[j]->parent->htb_node;
1093 instance->sets[j]->htb_node = next_node;
1101 /* Added this so that I could comment one line and kill off all of the
1102 * command execution. */
1103 static inline int execute_cmd(const char *cmd) {
1107 static inline int add_htb_node(const char *iface, const uint32_t parent_major, const uint32_t parent_minor,
1108 const uint32_t classid_major, const uint32_t classid_minor,
1109 const uint64_t rate, const uint64_t ceil) {
1112 sprintf(cmd, "tc class add dev %s parent %x:%x classid %x:%x htb rate %llubit ceil %llubit",
1113 iface, parent_major, parent_minor, classid_major, classid_minor, rate, ceil);
1114 printlog(LOG_WARN, "INIT: HTB_cmd: %s\n", cmd);
1116 return execute_cmd(cmd);
1119 static inline int add_htb_netem(const char *iface, const uint32_t parent_major,
1120 const uint32_t parent_minor, const uint32_t handle,
1121 const int loss, const int delay) {
1124 sprintf(cmd, "/sbin/tc qdisc del dev %s parent %x:%x handle %x pfifo", iface, parent_major,
1125 parent_minor, handle);
1126 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1127 if (execute_cmd(cmd))
1128 printlog(LOG_DEBUG, "HTB_cmd: Previous deletion did not succeed.\n");
1130 sprintf(cmd, "/sbin/tc qdisc replace dev %s parent %x:%x handle %x netem loss %d%% delay %dms",
1131 iface, parent_major, parent_minor, handle, loss, delay);
1132 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1133 return execute_cmd(cmd);
1136 static int create_htb_hierarchy(drl_instance_t *instance) {
1139 uint64_t gigabit = 1024 * 1024 * 1024;
1141 /* Nuke the hierarchy. */
1142 sprintf(cmd, "tc qdisc del dev eth0 root handle 1: htb");
1144 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1146 /* Re-initialize the basics. */
1147 sprintf(cmd, "tc qdisc add dev eth0 root handle 1: htb default 1fff");
1148 if (execute_cmd(cmd)) {
1151 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1153 if (add_htb_node("eth0", 1, 0, 1, 1, gigabit, gigabit))
1156 /* Add back 1:10. (Nodelimit : kilobits/sec -> bits/second)*/
1157 if (limiter.nodelimit) {
1158 if (add_htb_node("eth0", 1, 1, 1, 0x10, 8, (uint64_t) limiter.nodelimit * 1024))
1161 if (add_htb_node("eth0", 1, 1, 1, 0x10, 8, gigabit))
1166 for (i = 0; i < instance->machine_count; ++i) {
1167 if (add_htb_node("eth0", 1, instance->machines[i]->htb_parent, 1,
1168 instance->machines[i]->htb_node, 8, instance->machines[i]->limit * 1024)) {
1175 /* Add back 1:20. */
1177 if (instance->last_machine == NULL) {
1178 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:1 classid 1:20 htb rate 8bit ceil 1000mbit");
1180 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:%x classid 1:20 htb rate 8bit ceil 1000mbit",
1181 instance->last_machine->htb_node);
1184 sprintf(cmd, "/sbin/tc class add dev eth0 parent 1:1 classid 1:20 htb rate 8bit ceil 1000mbit");
1187 if (execute_cmd(cmd)) {
1190 printlog(LOG_DEBUG, "HTB_cmd: %s\n", cmd);
1193 for (j = (instance->set_count - 1); j >= 0; --j) {
1194 if (add_htb_node("eth0", 1, instance->sets[j]->htb_parent, 1,
1195 instance->sets[j]->htb_node, 8, instance->sets[j]->limit * 1024)) {
1200 /* Add leaves. FIXME: Set static sliver limit as ceil here! */
1201 for (k = 0; k < instance->leaf_count; ++k) {
1202 if (instance->leaves[k].parent == NULL) {
1203 if (add_htb_node("eth0", 1, 0x10, 1, (0x1000 | instance->leaves[k].xid), 8, gigabit))
1206 if (add_htb_node("eth0", 1, instance->leaves[k].parent->htb_node, 1, (0x1000 | instance->leaves[k].xid), 8, gigabit))
1210 /* Add exempt node for the leaf under 1:20 as 1:2<xid> */
1211 if (add_htb_node("eth0", 1, 0x20, 1, (0x2000 | instance->leaves[k].xid), 8, gigabit))
1215 /* Add 1:1000 and 1:2000 */
1216 if (instance->last_machine == NULL) {
1217 if (add_htb_node("eth0", 1, 0x10, 1, 0x1000, 8, gigabit))
1220 if (add_htb_node("eth0", 1, instance->last_machine->htb_node, 1, 0x1000, 8, gigabit))
1224 if (add_htb_node("eth0", 1, 0x20, 1, 0x2000, 8, gigabit))
1227 /* Add 1:1fff and 1:2fff */
1228 if (instance->last_machine == NULL) {
1229 if (add_htb_node("eth0", 1, 0x10, 1, 0x1fff, 8, gigabit))
1232 if (add_htb_node("eth0", 1, instance->last_machine->htb_node, 1, 0x1fff, 8, gigabit))
1236 if (add_htb_node("eth0", 1, 0x20, 1, 0x2fff, 8, gigabit))
1239 /* Artifical delay or loss for experimentation. */
1240 if (netem_delay.u.value || netem_loss.u.value) {
1241 if (!strcmp(netem_slice.u.string, "ALL")) {
1242 /* By default, netem applies to all leaves. */
1243 if (add_htb_netem("eth0", 1, 0x1000, 0x1000, netem_loss.u.value, netem_delay.u.value))
1245 if (add_htb_netem("eth0", 1, 0x1fff, 0x1fff, netem_loss.u.value, netem_delay.u.value))
1248 for (k = 0; k < instance->leaf_count; ++k) {
1249 if (add_htb_netem("eth0", 1, (0x1000 | instance->leaves[k].xid),
1250 (0x1000 | instance->leaves[k].xid), netem_loss.u.value, netem_delay.u.value)) {
1254 //FIXME: add exempt delay/loss here on 0x2000 ... ?
1257 /* netem_slice is not the default ALL value. Only apply netem
1258 * to the slice that is set in netem_slice.u.string. */
1261 sscanf(netem_slice.u.string, "%x", &slice_xid);
1263 if (add_htb_netem("eth0", 1, slice_xid, slice_xid, netem_loss.u.value, netem_delay.u.value))
1271 static int setup_tc_grd(drl_instance_t *instance) {
1275 for (i = 0; i < instance->leaf_count; ++i) {
1276 /* Delete the old pfifo qdisc that might have been there before. */
1277 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1%x handle 1%x pfifo",
1278 instance->leaves[i].xid, instance->leaves[i].xid);
1280 if (execute_cmd(cmd)) {
1281 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1284 /* Add the netem qdisc. */
1286 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1%x handle 1%x netem loss 0 delay 40ms",
1287 instance->leaves[i].xid, instance->leaves[i].xid);
1289 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1%x handle 1%x netem loss 0 delay 0ms",
1290 instance->leaves[i].xid, instance->leaves[i].xid);
1293 if (execute_cmd(cmd)) {
1298 /* Do the same for 1000 and 1fff. */
1299 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1000 handle 1000 pfifo");
1301 if (execute_cmd(cmd)) {
1302 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1305 /* Add the netem qdisc. */
1307 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1000 handle 1000 netem loss 0 delay 40ms");
1309 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1000 handle 1000 netem loss 0 delay 0ms");
1312 if (execute_cmd(cmd)) {
1316 sprintf(cmd, "/sbin/tc qdisc del dev eth0 parent 1:1fff handle 1fff pfifo");
1318 if (execute_cmd(cmd)) {
1319 printlog(LOG_DEBUG, "GRD: pfifo qdisc wasn't there!\n");
1322 /* Add the netem qdisc. */
1324 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1fff handle 1fff netem loss 0 delay 40ms");
1326 sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1fff handle 1fff netem loss 0 delay 0ms");
1329 if (execute_cmd(cmd)) {
1338 * Initialize this limiter with options
1339 * Open UDP socket for peer communication
1341 static int init_drl(void) {
1342 parsed_configs configs;
1343 struct sockaddr_in server_address;
1345 memset(&limiter, 0, sizeof(limiter_t));
1347 /* Setup logging. */
1348 system_loglevel = (uint8_t) drl_loglevel.u.value;
1349 logfile = fopen(drl_logfile.u.string, "w");
1351 if (logfile == NULL) {
1352 printf("Couldn't open logfile - ");
1357 printlog(LOG_CRITICAL, "ulogd_DRL initializing . . .\n");
1359 limiter.nodelimit = (uint32_t) (((double) nodelimit.u.value * 1000000.0) / 8.0);
1361 init_hashing(); /* for all hash maps */
1363 pthread_rwlock_init(&limiter.limiter_lock,NULL);
1365 /* determine our local IP by iterating through interfaces */
1366 if ((limiter.ip = get_local_ip())==0) {
1367 printlog(LOG_CRITICAL,
1368 "ulogd_DRL unable to aquire local IP address, not registering.\n");
1371 limiter.localaddr = inet_addr(limiter.ip);
1372 limiter.port = htons(LIMITER_LISTEN_PORT);
1373 limiter.udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
1374 if (limiter.udp_socket < 0) {
1375 printlog(LOG_CRITICAL, "Failed to create UDP socket().\n");
1379 memset(&server_address, 0, sizeof(server_address));
1380 server_address.sin_family = AF_INET;
1381 server_address.sin_addr.s_addr = limiter.localaddr;
1382 server_address.sin_port = limiter.port;
1384 if (bind(limiter.udp_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
1385 printlog(LOG_CRITICAL, "Failed to bind UDP socket.\n");
1389 printlog(LOG_WARN, " POLICY: %s\n",policy.u.string);
1390 if (strcasecmp(policy.u.string,"GRD") == 0) {
1391 limiter.policy = POLICY_GRD;
1392 } else if (strcasecmp(policy.u.string,"FPS") == 0) {
1393 limiter.policy = POLICY_FPS;
1395 printlog(LOG_CRITICAL,
1396 "Unknown DRL policy %s, aborting.\n",policy.u.string);
1400 limiter.estintms = estintms.u.value;
1401 if (limiter.estintms > 1000) {
1402 printlog(LOG_CRITICAL,
1403 "DRL: sorry estimate intervals must be less than 1 second.");
1404 printlog(LOG_CRITICAL,
1405 " Simple source mods will allow larger intervals. Using 1 second.\n");
1406 limiter.estintms = 1000;
1408 printlog(LOG_WARN, " Est interval: %dms\n",limiter.estintms);
1410 /* Acquire the big limiter lock for writing. Prevents pretty much
1411 * anything else from happening while the hierarchy is being changed. */
1412 pthread_rwlock_wrlock(&limiter.limiter_lock);
1414 limiter.stable_instance.ident_map = allocate_map();
1415 if (limiter.stable_instance.ident_map == NULL) {
1416 printlog(LOG_CRITICAL, "Failed to allocate memory for identity map.\n");
1420 if (get_eligible_leaves(&limiter.stable_instance)) {
1421 printlog(LOG_CRITICAL, "Failed to read eligigle leaves.\n");
1425 if (parse_drl_config(drl_configfile.u.string, &configs)) {
1426 /* Parse error occured. Return non-zero to notify init_drl(). */
1427 printlog(LOG_CRITICAL, "Failed to parse the DRL configuration file (%s).\n",
1428 drl_configfile.u.string);
1432 /* Validate identity hierarchy! */
1433 if (validate_configs(configs, &limiter.stable_instance)) {
1434 /* Clean up everything. */
1435 free_failed_config(configs, &limiter.stable_instance);
1436 printlog(LOG_CRITICAL, "Invalid DRL configuration file (%s).\n",
1437 drl_configfile.u.string);
1441 if (init_identities(configs, &limiter.stable_instance)) {
1442 free_failed_config(configs, &limiter.stable_instance);
1443 printlog(LOG_CRITICAL, "Failed to initialize identities.\n");
1447 /* At this point, we should be done with configs. */
1448 free_ident_list(configs.machines);
1449 free_ident_list(configs.sets);
1451 print_instance(&limiter.stable_instance);
1453 switch (limiter.policy) {
1455 if (assign_htb_hierarchy(&limiter.stable_instance)) {
1456 free_instance(&limiter.stable_instance);
1457 printlog(LOG_CRITICAL, "Failed to assign HTB hierarchy.\n");
1461 if (create_htb_hierarchy(&limiter.stable_instance)) {
1462 free_instance(&limiter.stable_instance);
1463 printlog(LOG_CRITICAL, "Failed to create HTB hierarchy.\n");
1469 if (setup_tc_grd(&limiter.stable_instance)) {
1470 free_instance(&limiter.stable_instance);
1471 printlog(LOG_CRITICAL, "Failed to initialize tc calls for GRD.\n");
1480 partition_set = partition.u.value;
1482 pthread_rwlock_unlock(&limiter.limiter_lock);
1484 if (pthread_create(&limiter.udp_recv_thread, NULL, limiter_receive_thread, NULL)) {
1485 printlog(LOG_CRITICAL, "Unable to start UDP receive thread.\n");
1489 printlog(LOG_WARN, "ulogd_DRL init finished.\n");
1494 static void reconfig() {
1495 parsed_configs configs;
1497 printlog(LOG_DEBUG, "--Starting reconfig()--\n");
1500 memset(&configs, 0, sizeof(parsed_configs));
1501 memset(&limiter.new_instance, 0, sizeof(drl_instance_t));
1503 limiter.new_instance.ident_map = allocate_map();
1504 if (limiter.new_instance.ident_map == NULL) {
1505 printlog(LOG_CRITICAL, "Failed to allocate ident_map during reconfig().\n");
1509 if (get_eligible_leaves(&limiter.new_instance)) {
1510 free_failed_config(configs, &limiter.new_instance);
1511 printlog(LOG_CRITICAL, "Failed to read leaves during reconfig().\n");
1515 if (parse_drl_config(drl_configfile.u.string, &configs)) {
1516 free_failed_config(configs, &limiter.new_instance);
1517 printlog(LOG_CRITICAL, "Failed to parse config during reconfig().\n");
1521 if (validate_configs(configs, &limiter.new_instance)) {
1522 free_failed_config(configs, &limiter.new_instance);
1523 printlog(LOG_CRITICAL, "Validation failed during reconfig().\n");
1524 pthread_rwlock_unlock(&limiter.limiter_lock);
1528 if (init_identities(configs, &limiter.new_instance)) {
1529 free_failed_config(configs, &limiter.new_instance);
1530 printlog(LOG_CRITICAL, "Initialization failed during reconfig().\n");
1531 pthread_rwlock_unlock(&limiter.limiter_lock);
1535 free_ident_list(configs.machines);
1536 free_ident_list(configs.sets);
1538 print_instance(&limiter.new_instance);
1541 pthread_rwlock_wrlock(&limiter.limiter_lock);
1543 switch (limiter.policy) {
1545 if (assign_htb_hierarchy(&limiter.new_instance)) {
1546 free_instance(&limiter.new_instance);
1547 printlog(LOG_CRITICAL, "Failed to assign HTB hierarchy during reconfig().\n");
1548 pthread_rwlock_unlock(&limiter.limiter_lock);
1552 if (create_htb_hierarchy(&limiter.new_instance)) {
1553 free_instance(&limiter.new_instance);
1554 printlog(LOG_CRITICAL, "Failed to create HTB hierarchy during reconfig().\n");
1556 /* Re-create old instance. */
1557 if (create_htb_hierarchy(&limiter.stable_instance)) {
1558 /* Error reinstating the old one - big problem. */
1559 printlog(LOG_CRITICAL, "Failed to reinstate HTB hierarchy during reconfig().\n");
1560 printlog(LOG_CRITICAL, "Giving up...\n");
1565 pthread_rwlock_unlock(&limiter.limiter_lock);
1571 if (setup_tc_grd(&limiter.new_instance)) {
1572 free_instance(&limiter.new_instance);
1573 printlog(LOG_CRITICAL, "GRD tc calls failed during reconfig().\n");
1575 /* Try to re-create old instance. */
1576 if (setup_tc_grd(&limiter.stable_instance)) {
1577 printlog(LOG_CRITICAL, "Failed to reinstate old GRD qdiscs during reconfig().\n");
1578 printlog(LOG_CRITICAL, "Giving up...\n");
1586 /* Should be impossible. */
1587 printf("Pigs are flying?\n");
1591 /* Switch over new to stable instance. */
1592 free_instance(&limiter.stable_instance);
1593 memcpy(&limiter.stable_instance, &limiter.new_instance, sizeof(drl_instance_t));
1595 /* Success! - Unlock */
1596 pthread_rwlock_unlock(&limiter.limiter_lock);
1599 static ulog_output_t drl_op = {
1601 .output = &_output_drl,
1602 .signal = NULL, /* This appears to be broken. Using my own handler. */
1607 /* Tests the amount of time it takes to call reconfig(). */
1608 static void time_reconfig(int iterations) {
1609 struct timeval start, end;
1612 gettimeofday(&start, NULL);
1613 for (i = 0; i < iterations; ++i) {
1616 gettimeofday(&end, NULL);
1618 printf("%d reconfigs() took %d seconds and %d microseconds.\n",
1619 iterations, end.tv_sec - start.tv_sec, end.tv_usec - start.tv_usec);
1622 // Seems to take about 85ms / iteration
1625 static int stop_enforcement(drl_instance_t *instance) {
1629 for (i = 0; i < instance->machine_count; ++i) {
1630 sprintf(cmd, "/sbin/tc class change dev eth0 parent 1:%x classid 1:%x htb rate 8bit ceil 100mbit",
1631 instance->machines[i]->htb_parent,
1632 instance->machines[i]->htb_node);
1634 if (execute_cmd(cmd)) {
1639 for (i = 0; i < instance->set_count; ++i) {
1640 sprintf(cmd, "/sbin/tc class change dev eth0 parent 1:%x classid 1:%x htb rate 8bit ceil 100mbit",
1641 instance->sets[i]->htb_parent,
1642 instance->sets[i]->htb_node);
1644 if (execute_cmd(cmd)) {
1652 static void *signal_thread_func(void *args) {
1658 sigaddset(&sigs, SIGHUP);
1659 sigaddset(&sigs, SIGUSR1);
1660 sigaddset(&sigs, SIGUSR2);
1661 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
1665 sigaddset(&sigs, SIGHUP);
1666 sigaddset(&sigs, SIGUSR1);
1667 sigaddset(&sigs, SIGUSR2);
1669 err = sigwait(&sigs, &sig);
1672 printlog(LOG_CRITICAL, "sigwait() returned an error.\n");
1678 printlog(LOG_WARN, "Caught SIGHUP - re-reading XML file.\n");
1680 //time_reconfig(1000); /* instrumentation */
1684 pthread_rwlock_wrlock(&limiter.limiter_lock);
1685 if (do_enforcement) {
1687 stop_enforcement(&limiter.stable_instance);
1688 printlog(LOG_CRITICAL, "--Switching enforcement off.--\n");
1691 printlog(LOG_CRITICAL, "--Switching enforcement on.--\n");
1693 pthread_rwlock_unlock(&limiter.limiter_lock);
1696 do_partition = !do_partition;
1699 /* Intentionally blank. */
1706 /* register output plugin with ulogd */
1707 static void _drl_reg_op(void)
1709 ulog_output_t *op = &drl_op;
1710 sigset_t signal_mask;
1712 sigemptyset(&signal_mask);
1713 sigaddset(&signal_mask, SIGHUP);
1714 sigaddset(&signal_mask, SIGUSR1);
1715 sigaddset(&signal_mask, SIGUSR2);
1716 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
1718 if (pthread_create(&signal_thread, NULL, &signal_thread_func, NULL) != 0) {
1719 printlog(LOG_CRITICAL, "Failed to create signal handling thread.\n");
1720 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1726 printlog(LOG_CRITICAL, "Init failed. :(\n");
1727 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1732 register_output(op);
1734 /* start up the thread that will periodically estimate the
1735 * local rate and set the local limits
1738 if (pthread_create(&estimate_thread, NULL, (void*(*)(void*)) &handle_estimation, &limiter)!=0) {
1739 printlog(LOG_CRITICAL, "Couldn't start estimate thread.\n");
1740 fprintf(stderr, "An error has occured starting ulogd_DRL. Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
1747 /* have the opts parsed */
1748 config_parse_file("DRL", config_entries);
1751 ulogd_log(ULOGD_ERROR, "can't resolve all keyhash id's\n");
1755 /* Seed the hash function */
1756 salt = getpid() ^ time(NULL);