|
| 1 | +#include <stdbool.h> |
| 2 | + |
| 3 | +#ifndef __CODEL_IMPL_H |
| 4 | +#define __CODEL_IMPL_H |
| 5 | + |
| 6 | +#ifndef CODEL_TARGET |
| 7 | +#define CODEL_TARGET (10 * 1000 * 1000ULL) /* 10 ms in nanosec */ |
| 8 | +#endif |
| 9 | + |
| 10 | +#ifndef CODEL_EXCEED_INTERVAL |
| 11 | +#define CODEL_EXCEED_INTERVAL (100 * 1000 * 1000ULL) /* 100 ms in ns*/ |
| 12 | +#endif |
| 13 | + |
| 14 | +/* Codel like dropping scheme, inspired by: |
| 15 | + * - RFC: https://queue.acm.org/detail.cfm?id=2209336 |
| 16 | + * - Code: https://queue.acm.org/appendices/codel.html |
| 17 | + * - Kernel: include/net/codel_impl.h |
| 18 | + */ |
| 19 | +struct codel_state { |
| 20 | + /* codel like dropping scheme */ |
| 21 | + __u64 first_above_time; /* Time when above target (0 if below)*/ |
| 22 | + __u64 drop_next; /* Time to drop next packet */ |
| 23 | + __u32 count; /* Packets dropped since going into drop state */ |
| 24 | + __u32 dropping; /* Equal to 1 if in drop state */ |
| 25 | +}; |
| 26 | + |
| 27 | +/* Table lookup for square-root shifted 16 bit */ |
| 28 | +static __always_inline __u32 get_sqrt_sh16(__u64 cnt) |
| 29 | +{ |
| 30 | + switch (cnt) { |
| 31 | + case 1: return 65536; /* 65536 * sqrt(1) */ |
| 32 | + case 2: return 92682; /* 65536 * sqrt(2) */ |
| 33 | + case 3: return 113512; /* 65536 * sqrt(3) */ |
| 34 | + case 4: return 131072; /* 65536 * sqrt(4) */ |
| 35 | + case 5: return 146543; /* 65536 * sqrt(5) */ |
| 36 | + case 6: return 160530; /* 65536 * sqrt(6) */ |
| 37 | + case 7: return 173392; |
| 38 | + case 8: return 185364; |
| 39 | + case 9: return 196608; |
| 40 | + case 10: return 207243; |
| 41 | + case 11: return 217358; |
| 42 | + case 12: return 227023; |
| 43 | + case 13: return 236293; |
| 44 | + case 14: return 245213; |
| 45 | + case 15: return 253820; |
| 46 | + case 16: return 262144; /* 100 ms / sqrt(16) = 25 ms */ |
| 47 | + case 17: return 270212; |
| 48 | + case 18: return 278046; |
| 49 | + case 19: return 285664; |
| 50 | + case 20: return 293086; |
| 51 | + case 21: return 300324; |
| 52 | + case 22: return 307391; |
| 53 | + case 23: return 314300; |
| 54 | + case 24: return 321060; |
| 55 | + case 25: return 327680; /* 100 ms / sqrt(25) = 20 ms */ |
| 56 | + case 26: return 334169; |
| 57 | + case 27: return 340535; |
| 58 | + case 28: return 346784; |
| 59 | + case 29: return 352922; |
| 60 | + case 30: return 358955; |
| 61 | + case 31: return 364889; |
| 62 | + case 32: return 370728; |
| 63 | + case 33: return 376476; |
| 64 | + case 34: return 382137; |
| 65 | + case 35: return 387716; |
| 66 | + case 36: return 393216; /* 100 / sqrt(36) = 16.66 ms */ |
| 67 | + default: |
| 68 | + return 463410; /* 65536*sqrt(50) => 100/sqrt(50) = 14.14 ms */ |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +static __always_inline __u64 get_next_interval_sqrt(__u64 cnt) |
| 73 | +{ |
| 74 | + __u64 val = ((__u64)CODEL_EXCEED_INTERVAL << 16) / get_sqrt_sh16(cnt); |
| 75 | + return val; |
| 76 | +} |
| 77 | + |
| 78 | +static __always_inline __u64 |
| 79 | +codel_control_law(__u64 t, __u64 cnt) |
| 80 | +{ |
| 81 | + return t + get_next_interval_sqrt(cnt); |
| 82 | +} |
| 83 | + |
| 84 | +static __always_inline |
| 85 | +bool codel_should_drop(struct codel_state *codel, __u64 t_queue_sz, __u64 now) |
| 86 | +{ |
| 87 | + __u64 interval = CODEL_EXCEED_INTERVAL; |
| 88 | + |
| 89 | + if (t_queue_sz < CODEL_TARGET) { |
| 90 | + /* went below so we'll stay below for at least interval */ |
| 91 | + codel->first_above_time = 0; |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + if (codel->first_above_time == 0) { |
| 96 | + /* just went above from below. If we stay above |
| 97 | + * for at least interval we'll say it's ok to drop |
| 98 | + */ |
| 99 | + codel->first_above_time = now + interval; |
| 100 | + return false; |
| 101 | + } else if (now >= codel->first_above_time) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +static __always_inline |
| 108 | +bool codel_drop(struct codel_state *codel, __u64 t_queue_sz, __u64 now) |
| 109 | +{ |
| 110 | + __u64 interval = CODEL_EXCEED_INTERVAL; |
| 111 | + |
| 112 | + /* If horizon have been exceed for a while, inc drop intensity*/ |
| 113 | + bool drop = codel_should_drop(codel, t_queue_sz, now); |
| 114 | + |
| 115 | + if (codel->dropping) { /* In dropping state */ |
| 116 | + if (!drop) { |
| 117 | + /* time below target - leave dropping state */ |
| 118 | + codel->dropping = false; |
| 119 | + return false; |
| 120 | + } else if (now >= codel->drop_next) { |
| 121 | + /* It's time for the next drop. Drop the current |
| 122 | + * packet. Schedule the next drop |
| 123 | + */ |
| 124 | + codel->count += 1; |
| 125 | + // schedule the next drop. |
| 126 | + codel->drop_next = |
| 127 | + codel_control_law(codel->drop_next, codel->count); |
| 128 | + return true; |
| 129 | + } |
| 130 | + } else if (drop && |
| 131 | + ((now - codel->drop_next < interval) || |
| 132 | + (now - codel->first_above_time >= interval))) { |
| 133 | + /* If we get here, then we're not in dropping state. |
| 134 | + * Decide whether it's time to enter dropping state. |
| 135 | + */ |
| 136 | + __u32 count = codel->count; |
| 137 | + |
| 138 | + codel->dropping = true; |
| 139 | + |
| 140 | + /* If we're in a drop cycle, drop rate that controlled queue |
| 141 | + * on the last cycle is a good starting point to control it now. |
| 142 | + */ |
| 143 | + if (now - codel->drop_next < interval) |
| 144 | + count = count > 2 ? (count - 2) : 1; |
| 145 | + else |
| 146 | + count = 1; |
| 147 | + |
| 148 | + codel->count = count; |
| 149 | + codel->drop_next = codel_control_law(now, count); |
| 150 | + return true; |
| 151 | + } |
| 152 | + return false; |
| 153 | +} |
| 154 | + |
| 155 | +#endif /* __CODEL_IMPL_H */ |
0 commit comments