Lots of changes. In no particular order:
[distributedratelimiting.git] / drl / estimate.c
1 /* See the DRL-LICENSE file for this file's software license. */
2
3 /*
4  * Thread to periodically calculate the estimated local limits
5  * Barath Raghavan 2006/2007
6  * Ken Yocum 2007
7  * Kevin Webb 2007/2008
8  */
9
10 #include <assert.h>
11
12 /** The size of the buffer we use to hold tc commands. */
13 #define CMD_BUFFER_SIZE 200
14
15 /* DRL specifics */
16 #include "raterouter.h" 
17 #include "util.h"
18 #include "ratetypes.h" /* needs util and pthread.h */
19 #include "calendar.h"
20 #include "logging.h"
21
22 extern uint8_t system_loglevel;
23
24 uint8_t do_enforcement = 0;
25
26 /**
27  * Called for each identity each estimate interval.  Uses flow table information
28  * to estimate the current aggregate rate and the rate of the individual flows
29  * in the table.
30  */
31 static void estimate(identity_t *ident, const double estintms) {
32     struct timeval now;
33     double time_difference;
34
35     pthread_mutex_lock(&ident->table_mutex); /* CLUNK ! */
36     
37     gettimeofday(&now, NULL);
38
39     time_difference = timeval_subtract(now, ident->common.last_update);
40
41     if (time_difference > 1.05 * (estintms / 1000 * ident->mainloop_intervals)) {
42         printlog(LOG_WARN, "Missed interval: Scheduled for %.2f ms, actual %.2fms\n",
43                  estintms * ident->mainloop_intervals, time_difference * 1000);
44     }
45
46     ident->table_update_function(ident->table, now, ident->ewma_weight);
47
48 #ifdef SHADOW_ACCTING
49
50     standard_table_update_flows((standard_flow_table) ident->shadow_table, now,
51                                 ident->ewma_weight);
52
53 #endif
54
55     pthread_mutex_unlock(&ident->table_mutex); /* CLINK ! */
56 }
57
58 /**
59  * Determines the FPS weight allocation when the identity is under its current
60  * local rate limit.
61  */
62 static double allocate_fps_under_limit(identity_t *ident, uint32_t target, double peer_weights) {
63     double ideal_weight;
64     double total_weight = peer_weights + ident->last_localweight;
65
66     if (target >= ident->limit) {
67         ideal_weight = total_weight;
68     } else if (target <= 0) {
69         ideal_weight = 0; // no flows here
70     } else {
71         ideal_weight = ((double)target / (double)ident->limit) * total_weight;
72     }
73
74 #if 0
75     else if (peer_weights <= 0) {
76 #if 0
77         // doesn't matter what we pick as our weight, so pick 1 / N.
78         ideal_weight = MAX_FLOW_SCALING_FACTOR / (remote_count(ident->i_handle) + 1);
79 #endif
80         ideal_weight = ((double)target / (double)ident->limit) * total_weight;
81     } else {
82 #if 0
83         double divisor = (double) ident->limit - (double) target;
84         ideal_weight = ((double) target * peer_weights) / divisor;
85 #else
86         ideal_weight = ((double)target / (double)ident->limit) * total_weight;
87 #endif
88     }
89 #endif
90
91     return ideal_weight;
92 }
93
94 /**
95  * Determines the FPS weight allocation when the identity is over its current
96  * local rate limit.
97  */
98 static double allocate_fps_over_limit(identity_t *ident) {
99     double ideal_weight;
100     double total_over_max;
101
102     if (ident->common.max_flow_rate > 0) {
103         ideal_weight = (double) ident->locallimit / (double) ident->common.max_flow_rate;
104         total_over_max = (double) ident->common.rate / (double) ident->common.max_flow_rate;
105
106         printlog(LOG_DEBUG, "ideal_over: %.3f, limit: %d, max_flow_rate: %d, total_rate: %d, total/max: %.3f\n",
107                  ideal_weight, ident->locallimit, ident->common.max_flow_rate, ident->common.rate, total_over_max);
108     } else {
109         ideal_weight = 1;
110     }
111
112     return ideal_weight;
113 }
114
115 /**
116  * When FPS checks to see which mode it should be operating in
117  * (over limit vs under limit), we don't want it to actually look to
118  * see if we're at the limit.  Instead, we want to see if we're getting
119  * close to the limit.  This defines how close is "close enough".
120  *
121  * For example, if the limit is 50000 and we're sending 49000, we probably
122  * want to be in the over limit mode, even if we aren't actually over the limit
123  * in order to switch to the more aggressive weight calculations.
124  */
125 static inline uint32_t close_enough(uint32_t limit) {
126     uint32_t difference = limit - (limit * CLOSE_ENOUGH);
127
128     if (difference < 2500) {
129         return (limit - 2500);
130     } else {
131         return (limit * CLOSE_ENOUGH);
132     }
133 }
134
135 static void print_statistics(identity_t *ident, const double ideal_weight,
136                              const double total_weight, const double localweight,
137                              const char *identifier, common_accounting_t *table,
138                              const uint32_t resulting_limit) {
139     struct timeval tv;
140     double time_now;
141
142     gettimeofday(&tv, NULL);
143     time_now = (double) tv.tv_sec + (double) ((double) tv.tv_usec / (double) 1000000);
144
145     printlog(LOG_WARN, "%.2f %d %.2f %.2f %.2f %d %d %d %d %d %d %d %d %d %s:%d ",
146              time_now, table->inst_rate, ideal_weight, localweight, total_weight,
147              table->num_flows, table->num_flows_5k, table->num_flows_10k,
148              table->num_flows_20k, table->num_flows_50k, table->avg_rate,
149              table->max_flow_rate, table->max_flow_rate_flow_hash, resulting_limit,
150              identifier, ident->id);
151
152     if (table->max_flow_rate > 0) {
153         printlog(LOG_WARN, "%.3f\n", (double) table->rate / (double) table->max_flow_rate);
154     } else {
155         printlog(LOG_WARN, "0\n");
156     }
157
158     /* Print to the screen in debug mode. */
159     if (system_loglevel == LOG_DEBUG) {
160         printf("Local Rate: %d, Ideal Weight: %.3f, Local Weight: %.3f, Total Weight: %.3f\n",
161                table->rate, ideal_weight, ident->localweight, total_weight);
162     }
163 }
164
165 static uint32_t allocate_fps(identity_t *ident, double total_weight,
166                              common_accounting_t *table, const char *identifier) {
167
168     uint32_t resulting_limit = 0;
169     double ideal_weight = 0.0;
170     double peer_weights = total_weight - ident->last_localweight;
171
172     /* Keep track of these for measurements & comparisons only. */
173     double ideal_under = 0.0;
174     double ideal_over = 0.0;
175
176     /* Weight sanity. */
177     if (peer_weights < 0.0) {
178         peer_weights = 0.0;
179     }
180
181     if (ident->dampen_state == DAMPEN_TEST) {
182         int64_t rate_delta = (int64_t) table->inst_rate - (int64_t) table->last_inst_rate;
183         double threshold = (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
184
185         if (rate_delta > threshold) {
186             ident->dampen_state = DAMPEN_PASSED;
187         } else {
188             ident->dampen_state = DAMPEN_FAILED;
189         }
190     }
191
192     /* Rate/weight sanity. */
193     if (table->rate <= 0) {
194         ideal_weight = 0.0;
195     }
196
197     /* Under the limit OR we failed our dampening test OR our current
198      * outgoing traffic rate is under the low "flowstart" watermark. */
199     else if (ident->dampen_state == DAMPEN_FAILED ||
200              table->rate < close_enough(ident->locallimit)) {
201 #if 0
202              || ident->flowstart) {
203         uint32_t target_rate = table->rate;
204
205         if (ident->flowstart) {
206             target_rate *= 4;
207
208             if (table->rate >= FLOW_START_THRESHOLD) {
209                 ident->flowstart = false;
210             }
211         } else {
212             /* June 16, 2008 (KCW)
213              * ident->flowstart gets set initially to one, but it is never set again.  However,
214              * if a limiter gets flows and then the number of flows drops to zero, it has trouble
215              * increasing the limit again. */
216             if (table->rate < FLOW_START_THRESHOLD) {
217                 ident->flowstart = true;
218             }
219         }
220         Old flowstart code.
221 #endif
222
223         /* Boost low-limits so that they have room to grow. */
224         if (table->rate < FLOW_START_THRESHOLD) {
225             ideal_weight = ideal_under = allocate_fps_under_limit(ident, table->rate * 4, peer_weights);
226         } else {
227             ideal_weight = ideal_under = allocate_fps_under_limit(ident, table->rate, peer_weights);
228         }
229
230         ideal_over = allocate_fps_over_limit(ident);
231
232         if (ideal_over < ideal_under) {
233             /* Degenerate case in which the agressive weight calculation was
234              * actually less than the under-the-limit case.  Use it instead
235              * and skip the dampening check in the next interval. */
236             ideal_weight = ideal_over;
237             ident->dampen_state = DAMPEN_SKIP;
238         } else {
239             ident->dampen_state = DAMPEN_NONE;
240         }
241
242         /* Apply EWMA. */
243         ident->localweight = (ident->localweight * ident->ewma_weight +
244                               ideal_weight * (1 - ident->ewma_weight));
245     }
246
247     /* At or over the limit.  Use the aggressive weight calculation. */
248     else {
249         double portion_last_interval = 0.0;
250         double portion_this_interval = 0.0;
251
252         ideal_weight = ideal_over = allocate_fps_over_limit(ident);
253         ideal_under = allocate_fps_under_limit(ident, table->rate, peer_weights);
254
255         /* Apply EWMA. */
256         ident->localweight = (ident->localweight * ident->ewma_weight +
257                               ideal_weight * (1 - ident->ewma_weight));
258
259         /* Now check whether the result of the aggressive weight calculation
260          * increases our portion of the weight "too much", in which case we
261          * dampen it. */
262
263         /* Our portion of weight in the whole system during the last interval.*/
264         portion_last_interval = ident->last_localweight / total_weight;
265
266         /* Our proposed portion of weight for the current interval. */
267         portion_this_interval = ident->localweight / (peer_weights + ident->localweight);
268
269         if (ident->dampen_state == DAMPEN_NONE &&
270             (portion_this_interval - portion_last_interval > LARGE_INCREASE_PERCENTAGE)) {
271             ident->localweight = ident->last_localweight + (LARGE_INCREASE_PERCENTAGE * total_weight);
272             ident->dampen_state = DAMPEN_TEST;
273         } else {
274             ident->dampen_state = DAMPEN_SKIP;
275         }
276     }
277
278     /* Add the weight calculated in this interval to the total. */
279     ident->total_weight = total_weight = ident->localweight + peer_weights;
280
281     /* Convert weight value into a rate limit.  If there is no measureable
282      * weight, do a L/n allocation. */
283     if (total_weight > 0) {
284         resulting_limit = (uint32_t) (ident->localweight * ident->limit / total_weight);
285     } else {
286         resulting_limit = (uint32_t) (ident->limit / (ident->comm.remote_node_count + 1));
287     }
288
289     print_statistics(ident, ideal_weight, total_weight, ident->localweight,
290                      identifier, table, resulting_limit);
291
292     return resulting_limit;
293 }
294
295 #ifdef SHADOW_ACCTING
296
297 /* Runs through the allocate functionality without making any state changes to
298  * the identity.  Useful for comparisons, especially for comparing standard
299  * and sample&hold accounting schemes. */
300 static void allocate_fps_pretend(identity_t *ident, double total_weight,
301                                  common_accounting_t *table, const char *identifier) {
302
303     uint32_t resulting_limit = 0;
304     double ideal_weight = 0.0;
305     double peer_weights = total_weight - ident->last_localweight_copy;
306     double ideal_under = 0.0;
307     double ideal_over = 0.0;
308
309     if (peer_weights < 0.0) {
310         peer_weights = 0.0;
311     }
312
313     if (ident->dampen_state_copy == DAMPEN_TEST) {
314         int64_t rate_delta = (int64_t) table->inst_rate - (int64_t) table->last_inst_rate;
315         double threshold = (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
316
317         if (rate_delta > threshold) {
318             ident->dampen_state_copy = DAMPEN_PASSED;
319         } else {
320             ident->dampen_state_copy = DAMPEN_FAILED;
321         }
322     }
323
324     /* Rate/weight sanity. */
325     if (table->rate <= 0) {
326         ideal_weight = 0.0;
327     }
328
329     /* Under the limit OR we failed our dampening test OR our current
330      * outgoing traffic rate is under the low "flowstart" watermark. */
331     else if (ident->dampen_state_copy == DAMPEN_FAILED ||
332              table->rate < close_enough(ident->locallimit)) {
333
334         /* Boost low-limits so that they have room to grow. */
335         if (table->rate < FLOW_START_THRESHOLD) {
336             ideal_weight = ideal_under = allocate_fps_under_limit(ident, table->rate * 4, peer_weights);
337         } else {
338             ideal_weight = ideal_under = allocate_fps_under_limit(ident, table->rate, peer_weights);
339         }
340
341         ideal_over = allocate_fps_over_limit(ident);
342
343         if (ideal_over < ideal_under) {
344             /* Degenerate case in which the agressive weight calculation was
345              * actually less than the under-the-limit case.  Use it instead
346              * and skip the dampening check in the next interval. */
347             ideal_weight = ideal_over;
348             ident->dampen_state_copy = DAMPEN_SKIP;
349         } else {
350             ident->dampen_state_copy = DAMPEN_NONE;
351         }
352
353         /* Apply EWMA. */
354         ident->localweight_copy = (ident->localweight_copy * ident->ewma_weight +
355                               ideal_weight * (1 - ident->ewma_weight));
356     }
357
358     /* At or over the limit.  Use the aggressive weight calculation. */
359     else {
360         double portion_last_interval = 0.0;
361         double portion_this_interval = 0.0;
362
363         ideal_weight = ideal_over = allocate_fps_over_limit(ident);
364         ideal_under = allocate_fps_under_limit(ident, table->rate, peer_weights);
365
366         /* Apply EWMA. */
367         ident->localweight_copy = (ident->localweight_copy * ident->ewma_weight +
368                               ideal_weight * (1 - ident->ewma_weight));
369
370         /* Now check whether the result of the aggressive weight calculation
371          * increases our portion of the weight "too much", in which case we
372          * dampen it. */
373
374         /* Our portion of weight in the whole system during the last interval.*/
375         portion_last_interval = ident->last_localweight / total_weight;
376
377         /* Our proposed portion of weight for the current interval. */
378         portion_this_interval = ident->localweight_copy / (peer_weights + ident->localweight_copy);
379
380         if (ident->dampen_state_copy == DAMPEN_NONE &&
381             (portion_this_interval - portion_last_interval > LARGE_INCREASE_PERCENTAGE)) {
382             ident->localweight_copy = ident->last_localweight + (LARGE_INCREASE_PERCENTAGE * total_weight);
383             ident->dampen_state_copy = DAMPEN_TEST;
384         } else {
385             ident->dampen_state_copy = DAMPEN_SKIP;
386         }
387     }
388
389     /* Add the weight calculated in this interval to the total. */
390     total_weight = ident->localweight_copy + peer_weights;
391
392     /* Convert weight value into a rate limit.  If there is no measureable
393      * weight, do a L/n allocation. */
394     if (total_weight > 0) {
395         resulting_limit = (uint32_t) (ident->localweight_copy * ident->limit / total_weight);
396     } else {
397         resulting_limit = (uint32_t) (ident->limit / (ident->comm.remote_node_count + 1));
398     }
399
400     print_statistics(ident, ideal_weight, total_weight, ident->localweight_copy,
401                      identifier, table, resulting_limit);
402 }
403
404 #endif
405
406 /**
407  * Determines the amount of FPS weight to allocate to the identity during each
408  * estimate interval.  Note that total_weight includes local weight.
409  */
410 static uint32_t allocate_fps_old(identity_t *ident, double total_weight) {
411     common_accounting_t *ftable = &ident->common; /* Common flow table info */
412     uint32_t local_rate = ftable->rate;
413     uint32_t ideallocal = 0;
414     double peer_weights; /* sum of weights of all other limiters */
415     double idealweight = 0;
416     double last_portion = 0;
417     double this_portion = 0;
418
419     static int dampen = 0;
420     int dampen_increase = 0;
421
422     double ideal_under = 0;
423     double ideal_over = 0;
424
425     int regime = 0;
426
427     /* two cases:
428        1. the aggregate is < limit
429        2. the aggregate is >= limit
430        */
431     peer_weights = total_weight - ident->last_localweight;
432     if (peer_weights < 0) {
433         peer_weights = 0;
434     }
435
436     if (dampen == 1) {
437         int64_t rate_delta =
438             (int64_t) ftable->inst_rate - (int64_t) ftable->last_inst_rate;
439         double threshold =
440             (double) ident->limit * (double) LARGE_INCREASE_PERCENTAGE / 10;
441
442         if (rate_delta > threshold) {
443             dampen_increase = 1;
444             printlog(LOG_DEBUG, "DAMPEN: delta(%.3f) thresh(%.3f)\n",
445                      rate_delta, threshold);
446         }
447     }
448
449     if (local_rate <= 0) {
450         idealweight = 0;
451     } else if (dampen_increase == 0 &&
452                (ident->locallimit <= 0 || local_rate < close_enough(ident->locallimit) || ident->flowstart)) {
453         /* We're under the limit - all flows are bottlenecked. */
454         idealweight = allocate_fps_under_limit(ident, local_rate, peer_weights);
455         ideal_over = allocate_fps_over_limit(ident);
456         ideal_under = idealweight;
457
458         if (ideal_over < idealweight) {
459             idealweight = ideal_over;
460             regime = 3;
461             dampen = 2;
462         } else {
463             regime = 1;
464             dampen = 0;
465         }
466
467         /* Apply EWMA */
468         ident->localweight = (ident->localweight * ident->ewma_weight +
469                               idealweight * (1 - ident->ewma_weight));
470         
471     } else {
472         idealweight = allocate_fps_over_limit(ident);
473         
474         /* Apply EWMA */
475         ident->localweight = (ident->localweight * ident->ewma_weight +
476                               idealweight * (1 - ident->ewma_weight));
477
478         /* This is the portion of the total weight in the system that was caused
479          * by this limiter in the last interval. */
480         last_portion = ident->last_localweight / total_weight;
481
482         /* This is the fraction of the total weight in the system that our
483          * proposed value for idealweight would use. */
484         this_portion = ident->localweight / (peer_weights + ident->localweight);
485
486         /* Dampen the large increase the first time... */
487         if (dampen == 0 && (this_portion - last_portion > LARGE_INCREASE_PERCENTAGE)) {
488             ident->localweight = ident->last_localweight + (LARGE_INCREASE_PERCENTAGE * total_weight);
489             dampen = 1;
490         } else {
491             dampen = 2;
492         }
493
494         ideal_under = allocate_fps_under_limit(ident, local_rate, peer_weights);
495         ideal_over = idealweight;
496
497         regime = 2;
498     }
499
500     /* Convert weight into a rate - add in our new local weight */
501     ident->total_weight = total_weight = ident->localweight + peer_weights;
502
503     /* compute local allocation:
504        if there is traffic elsewhere, use the weights
505        otherwise do a L/n allocation */
506     if (total_weight > 0) {
507     //if (peer_weights > 0) {
508         ideallocal = (uint32_t) (ident->localweight * ident->limit / total_weight);
509     } else {
510         ideallocal = ident->limit / (ident->comm.remote_node_count + 1);
511     }
512
513     printlog(LOG_DEBUG, "%.3f ActualWeight\n", ident->localweight);
514
515     printlog(LOG_DEBUG, "%.3f %.3f %.3f %.3f  Under / Over / Actual / Rate\n",
516             ideal_under / (ideal_under + peer_weights),
517             ideal_over / (ideal_over + peer_weights),
518             ident->localweight / (ident->localweight + peer_weights),
519             (double) local_rate / (double) ident->limit);
520
521     printlog(LOG_DEBUG, "%.3f %.3f IdealUnd IdealOve\n",ideal_under,ideal_over);
522
523     if (system_loglevel == LOG_DEBUG) {
524         printf("local_rate: %d, idealweight: %.3f, localweight: %.3f, total_weight: %.3f\n",
525             local_rate, idealweight, ident->localweight, total_weight);
526     }
527
528 #if 0
529     if (printcounter <= 0) {
530         struct timeval tv;
531         double time_now;
532
533         gettimeofday(&tv, NULL);
534         time_now = (double) tv.tv_sec + (double) ((double) tv.tv_usec / (double) 1000000);
535
536         printlog(LOG_WARN, "%.2f %d %.2f %.2f %.2f %d %d %d %d %d %d %d %d ", time_now, ftable->inst_rate,
537             idealweight, ident->localweight, total_weight, ftable->num_flows, ftable->num_flows_5k,
538             ftable->num_flows_10k, ftable->num_flows_20k, ftable->num_flows_50k, ftable->avg_rate,
539             ftable->max_flow_rate, ftable->max_flow_rate_flow_hash);
540
541         printcounter = PRINT_COUNTER_RESET;
542     } else {
543         printcounter -= 1;
544     }
545
546     //printf("Dampen: %d, dampen_increase: %d, peer_weights: %.3f, regime: %d\n",
547     //       dampen, dampen_increase, peer_weights, regime);
548
549     if (regime == 3) {
550         printlog(LOG_DEBUG, "MIN: min said to use flow counting, which was %.3f when other method said %.3f.\n",
551                  ideal_over, ideal_under);
552     }
553     See print_statistics()
554 #endif
555
556     printlog(LOG_DEBUG, "ideallocal is %d\n", ideallocal);
557
558     return(ideallocal);
559 }
560
561 /**
562  * Determines the local drop probability for a GRD identity every estimate
563  * interval.
564  */
565 static double allocate_grd(identity_t *ident, double aggdemand) {
566     double dropprob;
567     double global_limit = (double) (ident->limit);
568
569     if (aggdemand > global_limit) {
570         dropprob = (aggdemand-global_limit)/aggdemand;
571     } else {
572         dropprob = 0.0;
573     }
574     
575     if (system_loglevel == LOG_DEBUG) {
576         printf("local rate: %d, aggregate demand: %.3f, drop prob: %.3f\n",
577            ident->common.rate, aggdemand, dropprob);
578     }
579
580     return dropprob;
581 }
582
583 /** 
584  * Given current estimates of local rate (weight) and remote rates (weights)
585  * use GRD or FPS to calculate a new local limit. 
586  */
587 static void allocate(limiter_t *limiter, identity_t *ident) {
588     /* Represents aggregate rate for GRD and aggregate weight for FPS. */
589     double comm_val = 0;
590
591     /* Read comm_val from comm layer. */
592     if (limiter->policy == POLICY_FPS) {
593         read_comm(&ident->comm, &comm_val,
594                 ident->total_weight / (double) (ident->comm.remote_node_count + 1));
595     } else {
596         read_comm(&ident->comm, &comm_val,
597                 (double) (ident->limit / (double) (ident->comm.remote_node_count + 1)));
598     }
599     printlog(LOG_DEBUG, "%.3f Aggregate weight/rate (FPS/GRD)\n", comm_val);
600
601     /* Experimental printing. */
602     printlog(LOG_DEBUG, "%.3f \t Kbps used rate. ID:%d\n",
603              (double) ident->common.rate / (double) 128, ident->id);
604     ident->avg_bytes += ident->common.rate;
605     
606     if (limiter->policy == POLICY_FPS) {
607 #ifdef SHADOW_ACCTING
608
609         allocate_fps_pretend(ident, comm_val, &ident->shadow_common, "SHADOW-ID");
610
611         ident->last_localweight_copy = ident->localweight_copy;
612 #endif
613
614         ident->locallimit = allocate_fps(ident, comm_val, &ident->common, "ID");
615         ident->last_localweight = ident->localweight;
616
617         /* Update other limiters with our weight by writing to comm layer. */
618         write_local_value(&ident->comm, ident->localweight);
619     } else {
620         ident->locallimit = 0; /* Unused with GRD. */
621         ident->last_drop_prob = ident->drop_prob;
622         ident->drop_prob = allocate_grd(ident, comm_val);
623         
624         /* Update other limiters with our rate by writing to comm layer. */
625         write_local_value(&ident->comm, ident->common.rate);
626     }
627
628     /* Update identity state. */
629     ident->common.last_rate = ident->common.rate;
630 }
631
632 /**
633  * Traces all of the parent pointers of a leaf all the way to the root in
634  * order to find the maximum drop probability in the chain.
635  */
636 static double find_leaf_drop_prob(leaf_t *leaf) {
637     identity_t *current = leaf->parent;
638     double result = 0;
639
640     assert(current);
641
642     while (current != NULL) {
643         if (current->drop_prob > result) {
644             result = current->drop_prob;
645         }
646         current = current->parent;
647     }
648
649     return result;
650 }
651
652 /**
653  * This is called once per estimate interval to enforce the rate that allocate
654  * has decided upon.  It makes calls to tc using system().
655  */
656 static void enforce(limiter_t *limiter, identity_t *ident) {
657     char cmd[CMD_BUFFER_SIZE];
658     int ret = 0;
659     int i = 0;
660
661     switch (limiter->policy) {
662         case POLICY_FPS:
663
664             /* TC treats limits of 0 (8bit) as unlimited, which causes the
665              * entire rate limiting system to become unpredictable.  In
666              * reality, we also don't want any limiter to be able to set its
667              * limit so low that it chokes all of the flows to the point that
668              * they can't increase.  Thus, when we're setting a low limit, we
669              * make sure that it isn't too low by using the
670              * FLOW_START_THRESHOLD. */
671
672             if (ident->locallimit < FLOW_START_THRESHOLD) {
673                 ident->locallimit = FLOW_START_THRESHOLD;
674             }
675
676             /* Do not allow the node to set a limit higher than its
677              * administratively assigned upper limit (bwcap). */
678             if (limiter->nodelimit != 0 && ident->locallimit > limiter->nodelimit) {
679                 ident->locallimit = limiter->nodelimit;
680             }
681
682             if (system_loglevel == LOG_DEBUG) {
683                 printf("FPS: Setting local limit to %d\n", ident->locallimit);
684             }
685             printlog(LOG_DEBUG, "%d Limit ID:%d\n", ident->locallimit, ident->id);
686
687 #if 0
688             if (printcounter == PRINT_COUNTER_RESET) {
689                 if (ident->common.max_flow_rate > 0) {
690                     printlog(LOG_WARN, "%d ID:%d %.3f\n", ident->locallimit, ident->id,
691                              (double) ident->common.rate / (double) ident->common.max_flow_rate);
692                 } else {
693                     printlog(LOG_WARN, "%d ID:%d 0\n", ident->locallimit, ident->id);
694                 }
695             }
696             This is now done in print_statistics()
697 #endif
698
699             snprintf(cmd, CMD_BUFFER_SIZE,
700                      "/sbin/tc class change dev eth0 parent 1:%x classid 1:%x htb rate 8bit ceil %dbps quantum 1600",
701                      ident->htb_parent, ident->htb_node, ident->locallimit);
702
703             if (do_enforcement) {
704                 ret = system(cmd);
705
706                 if (ret) {
707                     /* FIXME: call failed.  What to do? */
708                     printlog(LOG_CRITICAL, "***TC call failed? Call was: %s***\n", cmd);
709                 }
710             }
711             break;
712
713         case POLICY_GRD:
714             for (i = 0; i < ident->leaf_count; ++i) {
715                 if (ident->drop_prob >= ident->leaves[i]->drop_prob) {
716                     /* The new drop probability for this identity is greater
717                      * than or equal to the leaf's current drop probability.
718                      * We can safely use the larger value at this leaf
719                      * immediately. */
720                     ident->leaves[i]->drop_prob = ident->drop_prob;
721                 } else if (ident->last_drop_prob < ident->leaves[i]->drop_prob) {
722                     /* The old drop probability for this identity is less than
723                      * the leaf's current drop probability.  This means that
724                      * this identity couldn't have been the limiting ident,
725                      * so nothing needs to be done because the old limiting
726                      * ident is still the limiting factor. */
727
728                     /* Intentionally blank. */
729                 } else {
730                     /* If neither of the above are true, then...
731                      * 1) The new drop probability for the identity is less
732                      * than what it previously was, and
733                      * 2) This ident may have had the maximum drop probability
734                      * of all idents limiting this leaf, and therefore we need
735                      * to follow the leaf's parents up to the root to find the
736                      * new leaf drop probability safely. */
737                     ident->leaves[i]->drop_prob =
738                             find_leaf_drop_prob(ident->leaves[i]);
739                 }
740
741                 /* Make the call to tc. */
742 #ifdef DELAY40MS
743                 snprintf(cmd, CMD_BUFFER_SIZE,
744                          "/sbin/tc qdisc change dev eth0 parent 1:1%x handle 1%x netem loss %.4f delay 40ms",
745                          ident->leaves[i]->xid, ident->leaves[i]->xid,
746                          (100 * ident->leaves[i]->drop_prob));
747 #else
748                 snprintf(cmd, CMD_BUFFER_SIZE,
749                          "/sbin/tc qdisc change dev eth0 parent 1:1%x handle 1%x netem loss %.4f delay 0ms",
750                          ident->leaves[i]->xid, ident->leaves[i]->xid,
751                          (100 * ident->leaves[i]->drop_prob));
752 #endif
753                 if (do_enforcement) {
754                     ret = system(cmd);
755
756                     if (ret) {
757                         /* FIXME: call failed.  What to do? */
758                         printlog(LOG_CRITICAL, "***TC call failed?***\n");
759                     }
760                 }
761             }
762
763             break;
764
765         default: 
766             printlog(LOG_CRITICAL, "DRL enforce: unknown policy %d\n",limiter->policy);
767             break;
768     }
769
770     return;
771 }
772
773 /**
774  * This function is periodically called to clean the stable instance's flow
775  * accounting tables for each identity.
776  */
777 static void clean(drl_instance_t *instance) {
778     identity_t *ident = NULL;
779
780     map_reset_iterate(instance->ident_map);
781     while ((ident = map_next(instance->ident_map)) != NULL) {
782         pthread_mutex_lock(&ident->table_mutex);
783
784         ident->table_cleanup_function(ident->table);
785
786 #ifdef SHADOW_ACCTING
787
788         standard_table_cleanup((standard_flow_table) ident->shadow_table);
789
790 #endif
791
792         pthread_mutex_unlock(&ident->table_mutex);
793     }
794
795     /* Periodically flush the log file. */
796     flushlog();
797 }
798
799 static void print_averages(drl_instance_t *instance, int print_interval) {
800     identity_t *ident = NULL;
801
802     map_reset_iterate(instance->ident_map);
803     while ((ident = map_next(instance->ident_map)) != NULL) {
804         ident->avg_bytes /= (double) print_interval;
805         //printf("avg_bytes = %f, print_interval = %d\n", ident->avg_bytes, print_interval);
806         printlog(LOG_DEBUG, "%.3f \t Avg rate. ID:%d\n",
807                  ident->avg_bytes / 128, ident->id);
808         //printf("%.3f \t Avg rate. ID:%d\n",
809         //         ident->avg_bytes / 128, ident->id);
810         ident->avg_bytes = 0;
811     }
812 }
813
814 /** Thread function to handle local rate estimation.
815  *
816  * None of our simple hashmap functions are thread safe, so we lock the limiter
817  * with an rwlock to prevent another thread from attempting to modify the set
818  * of identities.
819  *
820  * Each identity also has a private lock for its table.  This gets locked by
821  * table-modifying functions such as estimate and clean. It's also locked in
822  * ulogd_DRL.c when the table is being updated with new packets.
823  */
824 void handle_estimation(void *arg) {
825     limiter_t *limiter = (limiter_t *) arg;
826     int clean_timer, clean_wait_intervals;
827     useconds_t sleep_time = limiter->estintms * 1000;
828     uint32_t cal_slot = 0;
829     int print_interval = 1000 / (limiter->estintms);
830
831     sigset_t signal_mask;
832
833     sigemptyset(&signal_mask);
834     sigaddset(&signal_mask, SIGHUP);
835     sigaddset(&signal_mask, SIGUSR1);
836     sigaddset(&signal_mask, SIGUSR2);
837     pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
838
839     /* Determine the number of intervals we should wait before hitting the
840      * specified clean interval. (Converts seconds -> intervals). */
841     clean_wait_intervals = IDENT_CLEAN_INTERVAL * (1000.0 / limiter->estintms);
842     clean_timer = clean_wait_intervals;
843
844     while (true) {
845         printlog(LOG_DEBUG, "--Beginning new tick.--\n");
846
847         /* Sleep according to the delay of the estimate interval. */
848         usleep(sleep_time);
849
850         /* Grab the limiter lock for reading.  This prevents identities from
851          * disappearing beneath our feet. */
852         pthread_rwlock_rdlock(&limiter->limiter_lock);
853
854         cal_slot = limiter->stable_instance.cal_slot & SCHEDMASK;
855
856         /* Service all the identities that are scheduled to run during this
857          * tick. */
858         while (!TAILQ_EMPTY(limiter->stable_instance.cal + cal_slot)) {
859             identity_action *iaction = TAILQ_FIRST(limiter->stable_instance.cal + cal_slot);
860             TAILQ_REMOVE(limiter->stable_instance.cal + cal_slot, iaction, calendar);
861
862             /* Only execute the action if it is valid. */
863             if (iaction->valid == 0) {
864                 free(iaction);
865                 continue;
866             }
867
868             switch (iaction->action) {
869                 case ACTION_MAINLOOP:
870
871                     printlog(LOG_DEBUG, "Main loop: identity %d\n", iaction->ident->id);
872
873                     /* Update the ident's flow accouting table with the latest info. */
874                     estimate(iaction->ident, limiter->estintms);
875
876                     /* Determine its share of the rate allocation. */
877                     allocate(limiter, iaction->ident);
878
879                     /* Make tc calls to enforce the rate we decided upon. */
880                     enforce(limiter, iaction->ident);
881
882                     /* Add ident back to the queue at a future time slot. */
883                     TAILQ_INSERT_TAIL(limiter->stable_instance.cal +
884                             ((cal_slot + iaction->ident->mainloop_intervals) & SCHEDMASK),
885                             iaction, calendar);
886                     break;
887
888                 case ACTION_COMMUNICATE:
889
890                     printlog(LOG_DEBUG, "Communicating: identity %d\n", iaction->ident->id);
891
892                     /* Tell the comm library to propagate this identity's result for
893                      * this interval.*/
894                     send_update(&iaction->ident->comm, iaction->ident->id);
895
896                     /* Add ident back to the queue at a future time slot. */
897                     TAILQ_INSERT_TAIL(limiter->stable_instance.cal +
898                             ((cal_slot + iaction->ident->communication_intervals) & SCHEDMASK),
899                             iaction, calendar);
900                 break;
901
902                 default:
903                     printlog(LOG_CRITICAL, "Unknown identity action!?!\n");
904                     exit(EXIT_FAILURE);
905             }
906         }
907
908         print_interval--;
909         if (loglevel() == LOG_DEBUG && print_interval <= 0) {
910             print_interval = 1000 / (limiter->estintms);
911             print_averages(&limiter->stable_instance, print_interval);
912         }
913
914         /* Check if enough intervals have passed for cleaning. */
915         if (clean_timer <= 0) {
916             clean(&limiter->stable_instance);
917             clean_timer = clean_wait_intervals;
918         } else {
919             clean_timer--;
920         }
921
922         limiter->stable_instance.cal_slot += 1;
923
924         pthread_rwlock_unlock(&limiter->limiter_lock); 
925     }
926 }