bbr.c 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
#include "bbr.h"
#include "../util/dbg.h"
#include "../util/common.h"
#include "receiver.h"
#include <math.h>

prrtByteCount_t BBR_Inflight(BBR* bbr, double gain)
{
    if (bbr->rtprop == RTprop_Inf)
        return bbr->initial_cwnd; /* no valid RTT samples yet */
    uint32_t quanta = bbr->mps;
    uint32_t estimated_bdp = (uint32_t) round((((double)bbr->bw) * bbr->rtprop) / (1000 * 1000));
    return (uint32_t)(gain * estimated_bdp + quanta);
}

void BBR_EnterStartup(BBR* bbr)
{
    bbr->state = STARTUP;
    bbr->pacing_gain = BBRHighGain;
    bbr->cwnd_gain = BBRHighGain;
}

void BBR_UpdateBtlBw(BBR* bbr, PrrtRateSample* rs, PrrtPacketTracking* tracking)
{
    if (tracking->delivered >= bbr->next_round_delivered) {
        bbr->next_round_delivered = tracking->delivered;
        bbr->round_count++;
        bbr->round_start = true;
    } else {
        bbr->round_start = false;
    }
    uint32_t delivery_rate_Bps = (uint32_t)((float) rs->delivery_rate);
    if ((delivery_rate_Bps >= bbr->bw || !rs->is_app_limited) && delivery_rate_Bps != 0) {
        bbr->bw = (uint32_t)WindowedFilter_push(bbr->btlBwFilter, delivery_rate_Bps);
        debug(DEBUG_BBR, "Current BtlBw: %u, RS delivery rate: %u", bbr->bw, delivery_rate_Bps);
    }
}

void BBR_CheckFullPipe(BBR* bbr, PrrtRateSample* rs)
{
    if (bbr->filled_pipe || !bbr->round_start || rs->is_app_limited)
        return;  // no need to check for a full pipe now
    if (bbr->bw >= bbr->full_bw * PROBE_GAIN) {  // BBR.BtlBw still growing?
        bbr->full_bw = bbr->bw;    // record new baseline level
        bbr->full_bw_count = 0;
        return;
    }
    bbr->full_bw_count++; // another round w/o much growth
    if (bbr->full_bw_count >= 3)
        bbr->filled_pipe = true;
}

bool BBR_IsNextCyclePhase(BBR* bbr, prrtByteCount_t bytes_lost, prrtByteCount_t prior_inflight)
{
    bool is_full_length = (PrrtClock_get_current_time_us() - bbr->cycle_stamp) > bbr->rtprop;
    if (bbr->pacing_gain == 1)
        return is_full_length;
    if (bbr->pacing_gain > 1)
        return is_full_length && (bytes_lost > 0 || prior_inflight >= BBR_Inflight(bbr, bbr->pacing_gain));
    bool is_max_length = (PrrtClock_get_current_time_us() - bbr->cycle_stamp) > 4 * bbr->rtprop;
    return is_max_length || (prior_inflight <= BBR_Inflight(bbr, 1.0));
}

void BBR_AdvanceCyclePhase(BBR* bbr)
{
    bbr->cycle_stamp = PrrtClock_get_current_time_us();
    bbr->cycle_index = (uint8_t )((bbr->cycle_index + 1) % BBRGainCycleLen);
    float pacing_gain_cycle[BBRGainCycleLen] = {PROBE_GAIN, DRAIN_GAIN, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
    bbr->pacing_gain = pacing_gain_cycle[bbr->cycle_index];
    debug(DEBUG_BBR, "Advanced cycle with gain: %f", bbr->pacing_gain);
}

void BBR_CheckCyclePhase(BBR* bbr, prrtByteCount_t bytes_lost, prrtByteCount_t prior_inflight) {
    if (bbr->state == PROBE_BW && BBR_IsNextCyclePhase(bbr, bytes_lost, prior_inflight))
        BBR_AdvanceCyclePhase(bbr);
}

void BBR_EnterProbeBW(BBR* bbr)
{
    bbr->state = PROBE_BW;
    bbr->pacing_gain = 1;
    bbr->cwnd_gain = 2;
    bbr->cycle_index = (uint8_t)(BBRGainCycleLen - 1 - (random() % 7));
    BBR_AdvanceCyclePhase(bbr);
}

void BBR_CheckDrain(BBR* bbr, prrtByteCount_t bytes_inflight)
{
    if (bbr->state == STARTUP && bbr->filled_pipe) {
        //Drain
        bbr->state = DRAIN;
        bbr->pacing_gain = 1 / BBRHighGain;  // pace slowly
        bbr->cwnd_gain = BBRHighGain;    // maintain cwnd
    }
    if (bbr->state == DRAIN && bytes_inflight <= BBR_Inflight(bbr, 1.0))
        BBR_EnterProbeBW(bbr);  // we estimate queue is drained
}

void BBR_ExitProbeRTT(BBR* bbr)
{
    if (bbr->filled_pipe)
        BBR_EnterProbeBW(bbr);
    else
        BBR_EnterStartup(bbr);
}

uint32_t BBR_SaveCwnd(BBR* bbr)
{
    if (!bbr->is_loss_recovery && bbr->state != PROBE_RTT)
        return bbr->cwnd;
    return MAX(bbr->prior_cwnd ,bbr->cwnd);
}

void BBR_RestoreCwnd(BBR* bbr)
{
    bbr->cwnd = MAX(bbr->cwnd, bbr->prior_cwnd);
}

void BBR_UpdateRTprop(BBR* bbr, prrtTimedelta_t rtt)
{
    bbr->rtprop_expired = PrrtClock_get_current_time_us() > (bbr->rtprop_stamp + RTpropFilterLen);
    if (rtt >= 0 && (rtt <= bbr->rtprop || bbr->rtprop_expired)) {
        bbr->rtprop = rtt;
        bbr->rtprop_stamp = PrrtClock_get_current_time_us();
    }
}

void BBR_EnterProbeRTT(BBR *bbr) {
    bbr->state = PROBE_RTT;
    bbr->pacing_gain = 1;
    bbr->cwnd_gain = 1;
}

void BBR_HandleProbeRTT(BBR *bbr, PrrtPacketTracking *tracking) {
    tracking->app_limited = (tracking->delivered + tracking->pipe) ? : 1;
    /* Ignore low rate samples during ProbeRTT: */
    prrtTimestamp_t now = PrrtClock_get_current_time_us();
    if (bbr->probe_rtt_done_stamp == 0 && tracking->pipe <= bbr->min_pipe_cwnd) {
        bbr->probe_rtt_done_stamp = now + ProbeRTTDuration;
        bbr->probe_rtt_round_done = false;
        bbr->next_round_delivered = tracking->delivered;
    } else if (bbr->probe_rtt_done_stamp != 0) {
        if (bbr->round_start) {
            bbr->probe_rtt_round_done = true;
        }
        if (bbr->probe_rtt_round_done && (now > bbr->probe_rtt_done_stamp)) {
            bbr->rtprop_stamp = now;
            BBR_RestoreCwnd(bbr);
            BBR_ExitProbeRTT(bbr);
        }
    }
}

void BBR_CheckProbeRTT(BBR *bbr, PrrtPacketTracking *tracking) {

    if (bbr->state != PROBE_RTT && bbr->rtprop_expired && !bbr->idle_restart) {
        BBR_EnterProbeRTT(bbr);
        BBR_SaveCwnd(bbr);
        bbr->probe_rtt_done_stamp = 0;
    }

    if (bbr->state == PROBE_RTT) {
        BBR_HandleProbeRTT(bbr, tracking);
    }
    bbr->idle_restart = false;
}

void BBR_UpdateModelAndState(BBR *bbr, PrrtRateSample *rs, PrrtPacketTracking *packetTracking, prrtTimedelta_t rtt)
{
    BBR_UpdateBtlBw(bbr, rs, packetTracking);
    BBR_CheckCyclePhase(bbr, packetTracking->bytes_lost, packetTracking->prior_inflight);
    BBR_CheckFullPipe(bbr, rs);
    BBR_CheckDrain(bbr, packetTracking->pipe);
    BBR_UpdateRTprop(bbr, rtt);
    BBR_CheckProbeRTT(bbr, packetTracking);
}

void BBR_UpdateTargetCwnd(BBR* bbr)
{
    bbr->target_cwnd = BBR_Inflight(bbr, bbr->cwnd_gain);
}

void BBR_ModulateCwndForProbeRTT(BBR* bbr)
{
    if (bbr->state == PROBE_RTT)
        bbr->cwnd = MIN(bbr->cwnd, bbr->min_pipe_cwnd);
}

void BBR_ModulateCwndForRecovery(BBR* bbr, prrtByteCount_t bytes_lost, prrtByteCount_t pipe, prrtByteCount_t delivered)
{
    if (bytes_lost > 0) {
        if (bbr->cwnd > bytes_lost) {
            bbr->cwnd = MAX(bbr->cwnd - bytes_lost, bbr->mps);
        } else {
            bbr->cwnd = bbr->mps;
        }
    }

    if (bbr->packet_conservation)
        bbr->cwnd = MAX(bbr->cwnd, pipe + delivered);
}

void BBR_SetCwnd(BBR* bbr, PrrtPacketTracking* packetTracking)
{
    BBR_UpdateTargetCwnd(bbr);
    BBR_ModulateCwndForRecovery(bbr, packetTracking->bytes_lost, packetTracking->pipe, packetTracking->delivered);
    if (!bbr->packet_conservation) {
        if (bbr->filled_pipe)
            bbr->cwnd = MIN(bbr->cwnd + packetTracking->delivered, bbr->target_cwnd);
        else if (bbr->cwnd < bbr->target_cwnd || packetTracking->delivered < bbr->initial_cwnd)
            bbr->cwnd = bbr->cwnd + packetTracking->delivered;
        bbr->cwnd = MAX(bbr->cwnd, bbr->min_pipe_cwnd);
    }
    BBR_ModulateCwndForProbeRTT(bbr);
    debug(DEBUG_BBR, "New cwnd: %u, State: %u", bbr->cwnd, bbr->state);
}

void BBR_SetPacingRateWithGain(BBR* bbr, double pacing_gain)
{
    double rate = (pacing_gain * ((double)bbr->bw));
    debug(DEBUG_BBR, "Current rate: %f, Pacing gain: %f, BtlBw: %u, Calc Rate: %f, Filled pipe: %u", bbr->pacing_rate,
          pacing_gain, bbr->bw, rate, bbr->filled_pipe);
    if (rate != 0 && (bbr->filled_pipe || rate > bbr->pacing_rate))
        bbr->pacing_rate = rate;
}

void BBR_SetPacingRate(BBR* bbr)
{
    BBR_SetPacingRateWithGain(bbr, bbr->pacing_gain);
}

void BBR_SetSendQuantum(BBR* bbr) {
    if(bbr->pacing_rate < 150000) { // 1.2Mbps = 0.15 MBps = 150000 Bps
        bbr->send_quantum = 1 * bbr->mps;
    } else if (bbr->pacing_rate < 3000000) { // 24 Mbps = 20 * 1.2Mbps = 3000000
        bbr->send_quantum = 2 * bbr->mps;
    } else {
        bbr->send_quantum = MIN((prrtByteCount_t) round((double) bbr->pacing_rate / 1000), 64000);
    }
}

void BBR_OnACK(BBR* bbr, PrrtChannelStateInformation* csi, PrrtRateSample* rs, PrrtPacketTracking* packetTracking, prrtTimedelta_t rtt)
{
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    BBR_UpdateModelAndState(bbr, rs, packetTracking, rtt);

    BBR_SetPacingRate(bbr);
    BBR_SetSendQuantum(bbr);
    BBR_SetCwnd(bbr, packetTracking);
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return;
    error:
    PERROR("BBR_OnACK failed.")
}

void BBR_OnSpuriousLoss(BBR *bbr, PrrtPacketTracking *tracking) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    if(!bbr->is_loss_recovery) {
        bbr->is_loss_recovery = true;
        bbr->loss_recovery_stamp = PrrtClock_get_current_time_us();
        bbr->prior_cwnd = BBR_SaveCwnd(bbr);
        bbr->cwnd = tracking->pipe + MAX(tracking->delivered, 1);
        bbr->packet_conservation = true;
    } else if (PrrtClock_get_current_time_us() > bbr->loss_recovery_stamp + bbr->rtprop){
        bbr->packet_conservation = false;
    }
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return;
    error:
    PERROR("BBR_OnACK failed.")
}

void BBR_OnRTOLoss(BBR *bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    if(!bbr->is_loss_recovery) {
        bbr->is_loss_recovery = true;
        bbr->prior_cwnd = BBR_SaveCwnd(bbr);
        bbr->cwnd = bbr->mps;
    }
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return;
    error:
    PERROR("BBR_OnACK failed.")
}

void BBR_OnLossExit(BBR *bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    if(bbr->is_loss_recovery) {
        bbr->is_loss_recovery = false;
        bbr->packet_conservation = false;
        BBR_RestoreCwnd(bbr);
    }
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return;
    error:
    PERROR("BBR_OnACK failed.")
}

BBR* BBR_Init(prrtByteCount_t maximum_payload_size)
{
    BBR* bbr = calloc(1, sizeof(BBR));
    check_mem(bbr);

    pthread_mutexattr_t attr;
    check(pthread_mutexattr_init(&attr) == EXIT_SUCCESS, "Mutex attr init failed.");
    check(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == EXIT_SUCCESS, "Setting type failed.");
    check(pthread_mutex_init(&bbr->lock, &attr) == 0, "lock init failed.");

    bbr->mps = maximum_payload_size;
    bbr->min_pipe_cwnd = 4 * maximum_payload_size;
    bbr->initial_cwnd = 4 * maximum_payload_size;
    bbr->has_seen_rtt = false;
    bbr->btlBwFilter = WindowedFilter_create(true, 10);
    bbr->rtprop = RTprop_Inf;
    bbr->rtprop_stamp = PrrtClock_get_current_time_us();
    bbr->probe_rtt_done_stamp = 0;
    bbr->probe_rtt_round_done = false;
    bbr->packet_conservation = false;
    bbr->prior_cwnd = 0;
    bbr->cwnd = bbr->initial_cwnd;
    bbr->idle_restart = false;
    bbr->is_loss_recovery = false;

    //Init round counting
    bbr->next_round_delivered = 0;
    bbr->round_start = false;
    bbr->round_count = 0;

    //Init full pipe
    bbr->filled_pipe = false;
    bbr->full_bw = 0;
    bbr->full_bw_count = 0;

    //Init pacing rate
    double nominal_bandwidth = bbr->initial_cwnd / (bbr->has_seen_rtt ? bbr->rtprop : 1000);
    bbr->pacing_rate = bbr->pacing_gain * nominal_bandwidth;

    BBR_EnterStartup(bbr);

    return bbr;

    error:
    PERROR("Failed to init BBR%s.", "");
    return NULL;
}

void BBR_destroy(BBR* bbr)
{
    WindowedFilter_destroy(bbr->btlBwFilter);
    check(pthread_mutex_destroy(&bbr->lock) == 0, "lock destroy failed.");
    free(bbr);
    return;
    error:
    PERROR("BBR_destroy failed%s.", "");
}

double BBR_getPacingRate(BBR* bbr)
{
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    double res = bbr->pacing_rate;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getPacingRate failed.")
    return 0;
}

prrtByteCount_t BBR_getCwnd(BBR* bbr)
{
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    prrtByteCount_t res = bbr->cwnd;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getCwnd failed.")
    return 0;
}

prrtDeliveryRate_t BBR_getBtlBw(BBR* bbr)
{
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    prrtDeliveryRate_t res = bbr->bw;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getBtlBw failed.")
    return 0;
}

uint32_t BBR_getRTProp(BBR* bbr)
{
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    uint32_t res = bbr->rtprop;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getRTProp failed.")
    return 0;
}

uint32_t BBR_getState(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    uint32_t res = bbr->state;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getState failed.")
    return 0;
}

uint32_t BBR_getCycleIndex(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    uint32_t res = bbr->cycle_index;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getCycleIndex failed.")
    return 0;
}

double BBR_getPacingGain(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    double res = bbr->pacing_gain;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getPacingGain failed.")
    return 0;
}

bool BBR_getFilledPipe(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    bool res = bbr->filled_pipe;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getFilledPipe failed.")
    return 0;
}

bool BBR_getRoundStart(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    bool res = bbr->round_start;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getRoundStart failed.")
    return 0;
}

prrtByteCount_t BBR_getFullBw(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    prrtByteCount_t res = bbr->full_bw;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getInflight failed.")
    return 0;
}

prrtByteCount_t BBR_getInflight(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    prrtByteCount_t res = BBR_Inflight(bbr, bbr->pacing_gain);
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getInflight failed.")
    return 0;
}

prrtByteCount_t BBR_getSendQuantum(BBR* bbr) {
    check(pthread_mutex_lock(&bbr->lock) == 0, "Lock failed.");
    prrtByteCount_t res = bbr->send_quantum;
    check(pthread_mutex_unlock(&bbr->lock) == 0, "Unlock failed.");
    return res;
    error:
    PERROR("BBR_getSendQuantum failed.")
    return 0;
}