Jacob Erlbeck | 4f666bc | 2015-07-20 12:40:42 +0200 | [diff] [blame] | 1 | /* gprs_codel.cpp |
| 2 | * |
| 3 | * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "gprs_codel.h" |
| 22 | #include "gprs_debug.h" |
| 23 | |
| 24 | #include <osmocom/core/utils.h> |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <math.h> |
| 29 | |
| 30 | static void control_law(struct gprs_codel *state, struct timeval *delta) |
| 31 | { |
| 32 | /* 256 / sqrt(x), limited to 255 */ |
| 33 | static uint8_t inv_sqrt_tab[] = {255, |
| 34 | 255, 181, 147, 128, 114, 104, 96, 90, 85, 80, 77, 73, 71, 68, |
| 35 | 66, 64, 62, 60, 58, 57, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, |
| 36 | 45, 45, 44, 43, 43, 42, 42, 41, 40, 40, 39, 39, 39, 38, 38, 37, |
| 37 | 37, 36, 36, 36, 35, 35, 35, 34, 34, 34, 33, 33, 33, 33, 32, 32, |
| 38 | 32, 32, 31, 31, 31, 31, 30, 30, 30, 30, 29, 29, 29, 29, 29, 28, |
| 39 | 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 27, 26, 26, 26, 26, 26, |
| 40 | 26, 26, 25, 25, 25, 25, 25, 25, 25, 25, 24, 24, 24, 24, 24, 24, |
| 41 | 24, 24, 24, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 22, 22, 22, |
| 42 | 22, 22, 22, 22, 22, 22, 22, 22, 22, 21, 21, 21, 21, 21, 21, 21, |
| 43 | 21, 21, 21, 21, 21, 21, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 44 | 20, 20, 20, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, |
| 45 | 19, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, |
| 46 | 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 17, 17, 17, |
| 47 | 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, |
| 48 | 17, 17, 17, 17 |
| 49 | }; |
| 50 | uint_fast32_t delta_usecs; |
| 51 | uint_fast32_t inv_sqrt; |
| 52 | div_t q; |
| 53 | |
| 54 | if (state->count >= ARRAY_SIZE(inv_sqrt_tab)) |
| 55 | inv_sqrt = 16; |
| 56 | else |
| 57 | inv_sqrt = inv_sqrt_tab[state->count]; |
| 58 | |
| 59 | /* delta = state->interval / sqrt(count) */ |
| 60 | delta_usecs = state->interval.tv_sec * 1000000 + state->interval.tv_usec; |
| 61 | delta_usecs = delta_usecs * inv_sqrt / 256; |
| 62 | |
| 63 | q = div(delta_usecs, 1000000); |
| 64 | delta->tv_sec = q.quot; |
| 65 | delta->tv_usec = q.rem; |
| 66 | } |
| 67 | |
| 68 | void gprs_codel_init(struct gprs_codel *state) |
| 69 | { |
| 70 | static const struct gprs_codel init_state = {0}; |
| 71 | |
| 72 | *state = init_state; |
| 73 | gprs_codel_set_interval(state, -1); |
| 74 | gprs_codel_set_maxpacket(state, -1); |
| 75 | } |
| 76 | |
| 77 | void gprs_codel_set_interval(struct gprs_codel *state, int interval_ms) |
| 78 | { |
| 79 | div_t q; |
| 80 | |
| 81 | if (interval_ms <= 0) |
| 82 | interval_ms = GPRS_CODEL_DEFAULT_INTERVAL_MS; |
| 83 | |
| 84 | q = div(interval_ms, 1000); |
| 85 | state->interval.tv_sec = q.quot; |
| 86 | state->interval.tv_usec = q.rem * 1000; |
| 87 | |
| 88 | /* target ~ 5% of interval */ |
| 89 | q = div(interval_ms * 13 / 256, 1000); |
| 90 | state->target.tv_sec = q.quot; |
| 91 | state->target.tv_usec = q.rem * 1000; |
| 92 | } |
| 93 | |
| 94 | void gprs_codel_set_maxpacket(struct gprs_codel *state, int maxpacket) |
| 95 | { |
| 96 | |
| 97 | if (maxpacket < 0) |
| 98 | maxpacket = GPRS_CODEL_DEFAULT_MAXPACKET; |
| 99 | |
| 100 | state->maxpacket = maxpacket; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * This is an broken up variant of the algorithm being described in |
| 105 | * http://queue.acm.org/appendices/codel.html |
| 106 | */ |
| 107 | int gprs_codel_control(struct gprs_codel *state, const struct timeval *recv, |
| 108 | const struct timeval *now, int bytes) |
| 109 | { |
| 110 | struct timeval sojourn_time; |
| 111 | struct timeval delta; |
| 112 | |
| 113 | if (recv == NULL) |
| 114 | goto stop_dropping; |
| 115 | |
| 116 | timersub(now, recv, &sojourn_time); |
| 117 | |
| 118 | if (timercmp(&sojourn_time, &state->target, <)) |
| 119 | goto stop_dropping; |
| 120 | |
| 121 | if (bytes >= 0 && (unsigned)bytes <= state->maxpacket) |
| 122 | goto stop_dropping; |
| 123 | |
| 124 | if (!timerisset(&state->first_above_time)) { |
| 125 | timeradd(now, &state->interval, &state->first_above_time); |
| 126 | goto not_ok_to_drop; |
| 127 | } |
| 128 | |
| 129 | if (timercmp(now, &state->first_above_time, <)) |
| 130 | goto not_ok_to_drop; |
| 131 | |
| 132 | /* Ok to drop */ |
| 133 | |
| 134 | if (!state->dropping) { |
| 135 | int recently = 0; |
| 136 | int in_drop_cycle = 0; |
| 137 | if (timerisset(&state->drop_next)) { |
| 138 | timersub(now, &state->drop_next, &delta); |
| 139 | in_drop_cycle = timercmp(&delta, &state->interval, <); |
| 140 | recently = in_drop_cycle; |
| 141 | } |
| 142 | if (!recently) { |
| 143 | timersub(now, &state->first_above_time, &delta); |
| 144 | recently = !timercmp(&delta, &state->interval, <); |
| 145 | }; |
| 146 | if (!recently) |
| 147 | return 0; |
| 148 | |
| 149 | state->dropping = 1; |
| 150 | |
| 151 | if (in_drop_cycle && state->count > 2) |
| 152 | state->count -= 2; |
| 153 | else |
| 154 | state->count = 1; |
| 155 | |
| 156 | state->drop_next = *now; |
| 157 | } else { |
| 158 | if (timercmp(now, &state->drop_next, <)) |
| 159 | return 0; |
| 160 | |
| 161 | state->count += 1; |
| 162 | } |
| 163 | |
| 164 | control_law(state, &delta); |
| 165 | timeradd(&state->drop_next, &delta, &state->drop_next); |
| 166 | |
| 167 | #if 1 |
| 168 | LOGP(DRLCMAC, LOGL_INFO, |
| 169 | "CoDel decided to drop packet, window = %d.%03dms, count = %d\n", |
| 170 | (int)delta.tv_sec, (int)(delta.tv_usec / 1000), state->count); |
| 171 | #endif |
| 172 | return 1; |
| 173 | |
| 174 | stop_dropping: |
| 175 | timerclear(&state->first_above_time); |
| 176 | not_ok_to_drop: |
| 177 | state->dropping = 0; |
| 178 | return 0; |
| 179 | } |