-
Notifications
You must be signed in to change notification settings - Fork 16
/
send_control.cpp
174 lines (158 loc) · 5.4 KB
/
send_control.cpp
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
/*
* send_control.cpp
* congestion control logic for the swift protocol
*
* Created by Victor Grishchenko on 12/10/09.
* Copyright 2009 Delft University of Technology. All rights reserved.
*
*/
#include "swift.h"
using namespace swift;
using namespace std;
tint Channel::MIN_DEV = 50*TINT_MSEC;
tint Channel::MAX_SEND_INTERVAL = TINT_SEC*58;
tint Channel::LEDBAT_TARGET = TINT_MSEC*25;
float Channel::LEDBAT_GAIN = 1.0/LEDBAT_TARGET;
tint Channel::LEDBAT_DELAY_BIN = TINT_SEC*30;
tint Channel::MAX_POSSIBLE_RTT = TINT_SEC*10;
const char* Channel::SEND_CONTROL_MODES[] = {"keepalive", "pingpong",
"slowstart", "standard_aimd", "ledbat", "closing"};
tint Channel::NextSendTime () {
TimeoutDataOut(); // precaution to know free cwnd
switch (send_control_) {
case KEEP_ALIVE_CONTROL: return KeepAliveNextSendTime();
case PING_PONG_CONTROL: return PingPongNextSendTime();
case SLOW_START_CONTROL: return SlowStartNextSendTime();
case AIMD_CONTROL: return AimdNextSendTime();
case LEDBAT_CONTROL: return LedbatNextSendTime();
case CLOSE_CONTROL: return TINT_NEVER;
default: assert(false);
}
}
tint Channel::SwitchSendControl (int control_mode) {
dprintf("%s #%u sendctrl switch %s->%s\n",tintstr(),id(),
SEND_CONTROL_MODES[send_control_],SEND_CONTROL_MODES[control_mode]);
switch (control_mode) {
case KEEP_ALIVE_CONTROL:
send_interval_ = rtt_avg_; //max(TINT_SEC/10,rtt_avg_);
dev_avg_ = max(TINT_SEC,rtt_avg_);
data_out_cap_ = bin64_t::ALL;
cwnd_ = 1;
break;
case PING_PONG_CONTROL:
dev_avg_ = max(TINT_SEC,rtt_avg_);
data_out_cap_ = bin64_t::ALL;
cwnd_ = 1;
break;
case SLOW_START_CONTROL:
cwnd_ = 1;
break;
case AIMD_CONTROL:
break;
case LEDBAT_CONTROL:
break;
case CLOSE_CONTROL:
break;
default:
assert(false);
}
send_control_ = control_mode;
return NextSendTime();
}
tint Channel::KeepAliveNextSendTime () {
if (sent_since_recv_>=3 && last_recv_time_<NOW-TINT_MIN)
return SwitchSendControl(CLOSE_CONTROL);
if (ack_rcvd_recent_)
return SwitchSendControl(SLOW_START_CONTROL);
if (data_in_.time!=TINT_NEVER)
return NOW;
if (last_send_time_==NOW)
send_interval_ <<= 1;
if (send_interval_>MAX_SEND_INTERVAL)
send_interval_ = MAX_SEND_INTERVAL;
return last_send_time_ + send_interval_;
}
tint Channel::PingPongNextSendTime () { // FIXME INFINITE LOOP
if (dgrams_sent_>=10)
return SwitchSendControl(KEEP_ALIVE_CONTROL);
if (ack_rcvd_recent_)
return SwitchSendControl(SLOW_START_CONTROL);
if (data_in_.time!=TINT_NEVER)
return NOW;
if (last_recv_time_>last_send_time_)
return NOW;
if (!last_send_time_)
return NOW;
return last_send_time_ + ack_timeout(); // timeout
}
tint Channel::CwndRateNextSendTime () {
if (data_in_.time!=TINT_NEVER)
return NOW; // TODO: delayed ACKs
//if (last_recv_time_<NOW-rtt_avg_*4)
// return SwitchSendControl(KEEP_ALIVE_CONTROL);
send_interval_ = rtt_avg_/cwnd_;
if (send_interval_>max(rtt_avg_,TINT_SEC)*4)
return SwitchSendControl(KEEP_ALIVE_CONTROL);
if (data_out_.size()<cwnd_) {
dprintf("%s #%u sendctrl next in %llius (cwnd %.2f, data_out %i)\n",
tintstr(),id_,send_interval_,cwnd_,(int)data_out_.size());
return last_data_out_time_ + send_interval_;
} else {
assert(data_out_.front().time!=TINT_NEVER);
return data_out_.front().time + ack_timeout();
}
}
void Channel::BackOffOnLosses (float ratio) {
ack_rcvd_recent_ = 0;
ack_not_rcvd_recent_ = 0;
if (last_loss_time_<NOW-rtt_avg_) {
cwnd_ *= ratio;
last_loss_time_ = NOW;
dprintf("%s #%u sendctrl backoff %3.2f\n",tintstr(),id_,cwnd_);
}
}
tint Channel::SlowStartNextSendTime () {
if (ack_not_rcvd_recent_) {
BackOffOnLosses();
return SwitchSendControl(LEDBAT_CONTROL);//AIMD_CONTROL);
}
if (rtt_avg_/cwnd_<TINT_SEC/10)
return SwitchSendControl(LEDBAT_CONTROL);//AIMD_CONTROL);
cwnd_+=ack_rcvd_recent_;
ack_rcvd_recent_=0;
return CwndRateNextSendTime();
}
tint Channel::AimdNextSendTime () {
if (ack_not_rcvd_recent_)
BackOffOnLosses();
if (ack_rcvd_recent_) {
if (cwnd_>1)
cwnd_ += ack_rcvd_recent_/cwnd_;
else
cwnd_ *= 2;
}
ack_rcvd_recent_=0;
return CwndRateNextSendTime();
}
tint Channel::LedbatNextSendTime () {
tint owd_cur(TINT_NEVER), owd_min(TINT_NEVER);
for(int i=0; i<4; i++) {
if (owd_min>owd_min_bins_[i])
owd_min = owd_min_bins_[i];
if (owd_cur>owd_current_[i])
owd_cur = owd_current_[i];
}
if (ack_not_rcvd_recent_)
BackOffOnLosses(0.8);
ack_rcvd_recent_ = 0;
tint queueing_delay = owd_cur - owd_min;
tint off_target = LEDBAT_TARGET - queueing_delay;
cwnd_ += LEDBAT_GAIN * off_target / cwnd_;
if (cwnd_<1)
cwnd_ = 1;
if (owd_cur==TINT_NEVER || owd_min==TINT_NEVER)
cwnd_ = 1;
dprintf("%s #%u sendctrl ledbat %lli-%lli => %3.2f\n",
tintstr(),id_,owd_cur,owd_min,cwnd_);
return CwndRateNextSendTime();
}