Added option to run without the assumption of PlanetLab Vservers. We still assume...
[distributedratelimiting.git] / drl / ulogd_DRL.c
index 8d9b20c..cb32f3b 100644 (file)
  * Add the config options for DRL. 
  */
 
-static config_entry_t create_htb = {
+static config_entry_t leaves = {
     .next = NULL,
+    .key = "leaves",
+    .type = CONFIG_TYPE_STRING,
+    .options = CONFIG_OPT_NONE,
+    .u = { .string = "PLANETLAB" },
+};
+
+static config_entry_t bind_addr = {
+    .next = &leaves,
+    .key = "bind_addr",
+    .type = CONFIG_TYPE_STRING,
+    .options = CONFIG_OPT_NONE,
+    .u = { .string = "AUTO" },
+};
+
+static config_entry_t create_htb = {
+    .next = &bind_addr,
     .key = "create_htb",
     .type = CONFIG_TYPE_INT,
     .options = CONFIG_OPT_NONE,
@@ -223,7 +239,7 @@ static config_entry_t policy = {
     .key = "policy",
     .type = CONFIG_TYPE_STRING,
     .options = CONFIG_OPT_MANDATORY,
-    .u = { .string = "GRD" },
+    .u = { .string = "FPS" },
 };
 
 /** The estimate interval, in milliseconds. */
@@ -232,7 +248,7 @@ static config_entry_t estintms = {
     .key = "estintms",
     .type = CONFIG_TYPE_INT,
     .options = CONFIG_OPT_MANDATORY,
-    .u = { .value = 100 },
+    .u = { .value = 500 },
 };
 
 #define config_entries (&estintms)
@@ -717,15 +733,29 @@ static int validate_htb_exists(int node, int parent) {
     FILE *pipe = popen("/sbin/tc class show dev eth0", "r");
     char line[200];
 
-    while (fgets(line, 200, pipe) != NULL) {
-        int n, p;
-        char ignore[200];
+    if (parent != 0) {
+        while (fgets(line, 200, pipe) != NULL) {
+            int n, p;
+            char ignore[200];
 
-        sscanf(line, "class htb 1:%x parent 1:%x prio %s", &n, &p, ignore);
-        if (n == node && p == parent) {
-            pclose(pipe);
-            return 0;
+            sscanf(line, "class htb 1:%x parent 1:%x prio %s", &n, &p, ignore);
+            if (n == node && p == parent) {
+                pclose(pipe);
+                return 0;
+            }
         }
+    } else {
+        while (fgets(line, 200, pipe) != NULL) {
+            int n, p;
+            char ignore[200];
+
+            sscanf(line, "class htb 1:%x root prio %d %s", &n, &p, ignore);
+            if (n == node && strstr(line, "root") != NULL) {
+                pclose(pipe);
+                return 0;
+            }
+        }
+
     }
 
     pclose(pipe);
@@ -1526,15 +1556,20 @@ static int init_drl(void) {
     pthread_rwlock_init(&limiter.limiter_lock,NULL);
 
     /* determine our local IP by iterating through interfaces */
-    if ((limiter.ip = get_local_ip())==0) {
-        printlog(LOG_CRITICAL,
-                 "ulogd_DRL unable to aquire local IP address, not registering.\n");
-        return (false);
+    if (strncmp(bind_addr.u.string, "AUTO", 4)) {
+        limiter.ip = bind_addr.u.string;
+    } else {
+        limiter.ip = get_local_ip();
+        if (limiter.ip == NULL) {
+            printlog(LOG_CRITICAL, "ulogd_DRL unable to aquire local IP address, not registering.\n");
+            return false;
+        }
     }
     limiter.localaddr = inet_addr(limiter.ip);
     limiter.port = htons(LIMITER_LISTEN_PORT);
     limiter.udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     if (limiter.udp_socket < 0) {
+        perror("socket()");
         printlog(LOG_CRITICAL, "Failed to create UDP socket().\n");
         return false;
     }
@@ -1545,6 +1580,7 @@ static int init_drl(void) {
     server_address.sin_port = limiter.port;
 
     if (bind(limiter.udp_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
+        perror("bind()");
         printlog(LOG_CRITICAL, "Failed to bind UDP socket.\n");
         return false;
     }
@@ -1580,9 +1616,18 @@ static int init_drl(void) {
         return false;
     }
 
-    if (get_eligible_leaves(&limiter.stable_instance)) {
-        printlog(LOG_CRITICAL, "Failed to read eligigle leaves.\n");
-        return false;
+    /* If no leaves are specified, assume we're on planetlab and read them out
+     * of /proc/virtual.  Otherwise, read the specified line. */
+    if (!strncmp(leaves.u.string, "PLANETLAB", 9)) {
+        if (get_eligible_leaves(&limiter.stable_instance)) {
+            printlog(LOG_CRITICAL, "Failed to read eligigle leaves.\n");
+            return false;
+        }
+    } else {
+        if (parse_leaves(&limiter.stable_instance, leaves.u.string)) {
+            printlog(LOG_CRITICAL, "Failed to parse leaf string.\n");
+            return false;
+        }
     }
 
     if (parse_drl_config(drl_configfile.u.string, &configs)) {
@@ -1669,10 +1714,18 @@ static void reconfig() {
         return;
     }
 
-    if (get_eligible_leaves(&limiter.new_instance)) {
-        free_failed_config(configs, &limiter.new_instance);
-        printlog(LOG_CRITICAL, "Failed to read leaves during reconfig().\n");
-        return;
+    if (!strncmp(leaves.u.string, "PLANETLAB", 9)) {
+        if (get_eligible_leaves(&limiter.new_instance)) {
+            free_failed_config(configs, &limiter.new_instance);
+            printlog(LOG_CRITICAL, "Failed to read eligigle leaves.\n");
+            return false;
+        }
+    } else {
+        if (parse_leaves(&limiter.new_instance, leaves.u.string)) {
+            free_failed_config(configs, &limiter.new_instance);
+            printlog(LOG_CRITICAL, "Failed to parse leaf string.\n");
+            return false;
+        }
     }
 
     if (parse_drl_config(drl_configfile.u.string, &configs)) {
@@ -1795,27 +1848,34 @@ static void *signal_thread_func(void *args) {
     sigaddset(&sigs, SIGHUP);
     sigaddset(&sigs, SIGUSR1);
     sigaddset(&sigs, SIGUSR2);
+    sigaddset(&sigs, SIGRTMAX);
     pthread_sigmask(SIG_BLOCK, &sigs, NULL);
 
     while (1) {
         sigemptyset(&sigs);
-        sigaddset(&sigs, SIGHUP);
+        //sigaddset(&sigs, SIGHUP);
         sigaddset(&sigs, SIGUSR1);
         sigaddset(&sigs, SIGUSR2);
+        sigaddset(&sigs, SIGRTMAX);
 
         err = sigwait(&sigs, &sig);
 
         if (err) {
             printlog(LOG_CRITICAL, "sigwait() returned an error.\n");
             flushlog();
+            continue;
+        }
+
+        if (sig == SIGRTMAX) {
+            printf("Caught SIGRTMAX - toggling fake partitions.\n");
+            do_partition = !do_partition;
+            continue;
         }
 
         switch (sig) {
             case SIGHUP:
-                printlog(LOG_WARN, "Caught SIGHUP - re-reading XML file.\n");
-                reconfig();
-                //time_reconfig(1000); /* instrumentation */
-                flushlog();
+                printlog(LOG_CRITICAL, "Caught SIGHUP in signal_thread_func?!?\n");
+                printf("Caught SIGHUP in signal_thread_func?!?\n");
                 break;
             case SIGUSR1:
                 pthread_rwlock_wrlock(&limiter.limiter_lock);
@@ -1830,7 +1890,10 @@ static void *signal_thread_func(void *args) {
                 pthread_rwlock_unlock(&limiter.limiter_lock);
                 break;
             case SIGUSR2:
-                do_partition = !do_partition;
+                printlog(LOG_WARN, "Caught SIGUSR2 - re-reading XML file.\n");
+                printf("Caught SIGUSR2 - re-reading XML file.\n");
+                reconfig();
+                flushlog();
                 break;
             default:
                 /* Intentionally blank. */
@@ -1843,9 +1906,10 @@ static int drl_plugin_init() {
     sigset_t signal_mask;
 
     sigemptyset(&signal_mask);
-    sigaddset(&signal_mask, SIGHUP);
+    //sigaddset(&signal_mask, SIGHUP);
     sigaddset(&signal_mask, SIGUSR1);
     sigaddset(&signal_mask, SIGUSR2);
+    sigaddset(&signal_mask, SIGRTMAX);
     pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
 
     if (pthread_create(&signal_thread, NULL, &signal_thread_func, NULL) != 0) {
@@ -1882,10 +1946,22 @@ static int drl_plugin_init() {
     return 0;
 }
 
+static void drl_signal(int sig) {
+    if (sig == SIGHUP) {
+        printf("Caught SIGHUP - reopening DRL log file.\n");
+        
+        fclose(logfile);
+        logfile = fopen(drl_logfile.u.string, "a");
+        printlog(LOG_CRITICAL, "Reopened logfile.\n");
+    } else {
+        printlog(LOG_WARN, "Caught unexpected signal %d in drl_signal.\n", sig);
+    }
+}
+
 static ulog_output_t drl_op = {
     .name = "drl",
     .output = &_output_drl,
-    .signal = NULL, /* This appears to be broken. Using my own handler. */
+    .signal = &drl_signal,
     .init = &drl_plugin_init,
     .fini = NULL,
 };