Turned enforcement on by default at startup. Added a ulogd.conf config option to...
[distributedratelimiting.git] / drl / ulogd_DRL.c
index 79cb4ef..f9d6d56 100644 (file)
  * Add the config options for DRL. 
  */
 
-static config_entry_t partition = {
+static config_entry_t enforce_on = {
     .next = NULL,
+    .key = "enforce_on",
+    .type = CONFIG_TYPE_INT,
+    .options = CONFIG_OPT_NONE,
+    .u = { .value = 1 },
+};
+
+static config_entry_t partition = {
+    .next = &enforce_on,
     .key = "partition_set",
     .type = CONFIG_TYPE_INT,
     .options = CONFIG_OPT_NONE,
@@ -1269,7 +1277,7 @@ static int create_htb_hierarchy(drl_instance_t *instance) {
 }
 
 static int setup_tc_grd(drl_instance_t *instance) {
-    int i;
+    int i, j;
     char cmd[300];
 
     for (i = 0; i < instance->leaf_count; ++i) {
@@ -1282,15 +1290,11 @@ static int setup_tc_grd(drl_instance_t *instance) {
         }
 
         /* Add the netem qdisc. */
-#ifdef DELAY40MS
-        sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1%x handle 1%x netem loss 0 delay 40ms",
-                instance->leaves[i].xid, instance->leaves[i].xid);
-#else
         sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1%x handle 1%x netem loss 0 delay 0ms",
                 instance->leaves[i].xid, instance->leaves[i].xid);
-#endif
 
         if (execute_cmd(cmd)) {
+            printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
             return 1;
         }
     }
@@ -1303,13 +1307,10 @@ static int setup_tc_grd(drl_instance_t *instance) {
     }
 
     /* Add the netem qdisc. */
-#ifdef DELAY40MS
-    sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1000 handle 1000 netem loss 0 delay 40ms");
-#else
     sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1000 handle 1000 netem loss 0 delay 0ms");
-#endif
 
     if (execute_cmd(cmd)) {
+        printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
         return 1;
     }
 
@@ -1320,16 +1321,65 @@ static int setup_tc_grd(drl_instance_t *instance) {
     }
 
     /* Add the netem qdisc. */
-#ifdef DELAY40MS
-    sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1fff handle 1fff netem loss 0 delay 40ms");
-#else
     sprintf(cmd, "/sbin/tc qdisc replace dev eth0 parent 1:1fff handle 1fff netem loss 0 delay 0ms");
-#endif
 
     if (execute_cmd(cmd)) {
+        printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
         return 1;
     }
 
+    /* Artifical delay or loss for experimentation. */
+    if (netem_delay.u.value || netem_loss.u.value) {
+        if (!strcmp(netem_slice.u.string, "ALL")) {
+            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);
+            if (execute_cmd(cmd)) {
+                printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
+                return 1;
+            }
+
+            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);
+            if (execute_cmd(cmd)) {
+                printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
+                return 1;
+            }
+
+            for (j = 0; j < instance->leaf_count; ++j) {
+                leaf_t *current = &instance->leaves[j];
+
+                current->delay = netem_delay.u.value;
+
+                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);
+
+                if (execute_cmd(cmd)) {
+                    printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
+                    return 1;
+                }
+            }
+        } else {
+            uint32_t slice_xid;
+            leaf_t *leaf = NULL;
+
+            sscanf(netem_slice.u.string, "%x", &slice_xid);
+
+            leaf = (leaf_t *) map_search(instance->leaf_map, &slice_xid, sizeof(slice_xid));
+
+            if (leaf == NULL) {
+                /* Leaf not found - invalid selection. */
+                printf("Your experimental setup is incorrect...\n");
+                return 1;
+            }
+
+            leaf->delay = netem_delay.u.value;
+
+            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);
+
+            if (execute_cmd(cmd)) {
+                printlog(LOG_CRITICAL, "TC GRD call failed: %s\n", cmd);
+                return 1;
+            }
+        }
+    }
+
     return 0;
 }
 
@@ -1740,6 +1790,13 @@ static void _drl_reg_op(void)
         fprintf(stderr, "An error has occured starting ulogd_DRL.  Refer to your logfile (%s) for additional information.\n", drl_logfile.u.string);
         exit(EXIT_FAILURE);
     }
+
+    if (enforce_on.u.value) {
+        pthread_rwlock_wrlock(&limiter.limiter_lock);
+        do_enforcement = 1;
+        printlog(LOG_CRITICAL, "--Switching enforcement on.--\n");
+        pthread_rwlock_unlock(&limiter.limiter_lock);
+    }
 }
 
 void _init(void)