| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* tc_red.c RED maintenance routines. |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <math.h> |
| 13 |
#include <sys/socket.h> |
| 14 |
#include <netinet/in.h> |
| 15 |
#include <arpa/inet.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include "utils.h" |
| 19 |
#include "tc_core.h" |
| 20 |
#include "tc_util.h" |
| 21 |
#include "tc_red.h" |
| 22 |
|
| 23 |
/* |
| 24 |
Plog = log(prob/(qmax - qmin)) |
| 25 |
*/ |
| 26 |
int tc_red_eval_P(unsigned int qmin, unsigned int qmax, double prob) |
| 27 |
{ |
| 28 |
int i = qmax - qmin; |
| 29 |
|
| 30 |
if (!i) |
| 31 |
return 0; |
| 32 |
if (i < 0) |
| 33 |
return -1; |
| 34 |
|
| 35 |
prob /= i; |
| 36 |
|
| 37 |
for (i = 0; i < 32; i++) { |
| 38 |
if (prob > 1.0) |
| 39 |
break; |
| 40 |
prob *= 2; |
| 41 |
} |
| 42 |
if (i >= 32) |
| 43 |
return -1; |
| 44 |
return i; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
burst + 1 - qmin/avpkt < (1-(1-W)^burst)/W |
| 49 |
*/ |
| 50 |
|
| 51 |
int tc_red_eval_ewma(unsigned int qmin, unsigned int burst, unsigned int avpkt) |
| 52 |
{ |
| 53 |
int wlog = 1; |
| 54 |
double W = 0.5; |
| 55 |
double a = (double)burst + 1 - (double)qmin/avpkt; |
| 56 |
|
| 57 |
if (a < 1.0) { |
| 58 |
fprintf(stderr, "tc_red_eval_ewma() burst %u is too small ? Try burst %u\n", |
| 59 |
burst, 1 + qmin/avpkt); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
for (wlog = 1; wlog < 32; wlog++, W /= 2) { |
| 63 |
if (a <= (1 - pow(1-W, burst))/W) |
| 64 |
return wlog; |
| 65 |
} |
| 66 |
return -1; |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
Stab[t>>Scell_log] = -log(1-W) * t/xmit_time |
| 71 |
*/ |
| 72 |
|
| 73 |
int tc_red_eval_idle_damping(int Wlog, unsigned int avpkt, unsigned int bps, __u8 *sbuf) |
| 74 |
{ |
| 75 |
double xmit_time = tc_calc_xmittime(bps, avpkt); |
| 76 |
double lW = -log(1.0 - 1.0/(1<<Wlog))/xmit_time; |
| 77 |
double maxtime = 31/lW; |
| 78 |
int clog; |
| 79 |
int i; |
| 80 |
|
| 81 |
for (clog = 0; clog < 32; clog++) { |
| 82 |
if (maxtime/(1<<clog) < 512) |
| 83 |
break; |
| 84 |
} |
| 85 |
if (clog >= 32) |
| 86 |
return -1; |
| 87 |
|
| 88 |
sbuf[0] = 0; |
| 89 |
for (i = 1; i < 255; i++) { |
| 90 |
sbuf[i] = (i<<clog)*lW; |
| 91 |
if (sbuf[i] > 31) |
| 92 |
sbuf[i] = 31; |
| 93 |
} |
| 94 |
sbuf[255] = 31; |
| 95 |
return clog; |
| 96 |
} |
| 97 |
|
| 98 |
void tc_red_print_flags(__u32 flags) |
| 99 |
{ |
| 100 |
if (flags & TC_RED_ECN) |
| 101 |
print_bool(PRINT_ANY, "ecn", "ecn ", true); |
| 102 |
else |
| 103 |
print_bool(PRINT_ANY, "ecn", NULL, false); |
| 104 |
|
| 105 |
if (flags & TC_RED_HARDDROP) |
| 106 |
print_bool(PRINT_ANY, "harddrop", "harddrop ", true); |
| 107 |
else |
| 108 |
print_bool(PRINT_ANY, "harddrop", NULL, false); |
| 109 |
|
| 110 |
if (flags & TC_RED_ADAPTATIVE) |
| 111 |
print_bool(PRINT_ANY, "adaptive", "adaptive ", true); |
| 112 |
else |
| 113 |
print_bool(PRINT_ANY, "adaptive", NULL, false); |
| 114 |
|
| 115 |
if (flags & TC_RED_NODROP) |
| 116 |
print_bool(PRINT_ANY, "nodrop", "nodrop ", true); |
| 117 |
else |
| 118 |
print_bool(PRINT_ANY, "nodrop", NULL, false); |
| 119 |
} |