forked from venkatarun95/genericCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctcp.hh
275 lines (215 loc) · 8.16 KB
/
ctcp.hh
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
#ifndef REMY_TCP_HH
#define REMY_TCP_HH
#include <assert.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>
#include "ccc.hh"
#include "remycc.hh"
#include "tcp-header.hh"
#include "udp-socket.hh"
using namespace std;
#define packet_size 1500
#define data_size (packet_size-sizeof(TCPHeader))
template <class T>
class CTCP {
public:
enum ConnectionType{ SENDER, RECEIVER };
private:
T congctrl;
UDPSocket socket;
ConnectionType conntype;
string dstaddr;
int dstport;
double _last_send_time;
int _largest_ack;
double tot_time_transmitted;
double tot_delay;
int tot_bytes_transmitted;
int tot_packets_transmitted;
void tcp_handshake();
public:
CTCP( T s_congctrl, string ipaddr, int port )
: congctrl( s_congctrl ),
socket(),
conntype( SENDER ),
dstaddr( ipaddr ),
dstport( port ),
_last_send_time( 0.0 ),
_largest_ack( -1 ),
tot_time_transmitted( 0 ),
tot_delay( 0 ),
tot_bytes_transmitted( 0 ),
tot_packets_transmitted( 0 )
{
socket.bindsocket( ipaddr, port );
}
CTCP( CTCP<T> &other )
: congctrl( other.congctrl ),
socket(),
conntype( other.conntype ),
dstaddr( other.dstaddr ),
dstport( other.dstport ),
_last_send_time( 0.0 ),
_largest_ack( -1 ),
tot_time_transmitted( 0 ),
tot_delay( 0 ),
tot_bytes_transmitted( 0 ),
tot_packets_transmitted( 0 )
{
socket.bindsocket( dstaddr, dstport );
}
//duration in milliseconds
void send_data ( double flow_size, bool byte_switched, int flow_id, int src_id );
void listen_for_data ( );
};
#include <string.h>
#include <stdio.h>
#include "configs.hh"
using namespace std;
// use the packet 'pair' trick to measure link rate of the bottleneck link. This parameter controls the number of packets used for that purpose. The constant is defined in configs.hh
const int num_packets_per_link_rate_measurement = NUM_PACKETS_PER_LINK_RATE_MEASUREMENT;
const double link_rate_measurement_alpha = LINK_RATE_MEASUREMENT_ALPHA;
double current_timestamp( chrono::high_resolution_clock::time_point &start_time_point ){
using namespace chrono;
high_resolution_clock::time_point cur_time_point = high_resolution_clock::now();
return duration_cast<duration<double>>(cur_time_point - start_time_point).count()*1000; //convert to milliseconds, because that is the scale on which the rats have been trained
}
template<class T>
void CTCP<T>::tcp_handshake() {
TCPHeader header, ack_header;
// this is the data that is transmitted. A sizeof(TCPHeader) header followed by a sring of dashes
char buf[packet_size];
memset(buf, '-', sizeof(char)*packet_size);
buf[packet_size-1] = '\0';
header.seq_num = -1;
header.flow_id = -1;
header.src_id = -1;
header.sender_timestamp = -1;
header.receiver_timestamp = -1;
memcpy( buf, &header, sizeof(TCPHeader) );
socket.senddata( buf, packet_size, NULL );
sockaddr_in other_addr;
while ( true ) {
if (socket.receivedata( buf, packet_size, 2000, other_addr ) == 0) {
cerr << "Could not establish connection" << endl;
continue;
}
memcpy(&ack_header, buf, sizeof(TCPHeader));
if (ack_header.seq_num != -1 || ack_header.flow_id != -1)
continue;
if (ack_header.sender_timestamp != -1 || ack_header.src_id != -1)
continue;
break;
}
cout << "Connection Established." << endl;
}
// takes flow_size in milliseconds (byte_switched=false) or in bytes (byte_switched=true)
template<class T>
void CTCP<T>::send_data( double flow_size, bool byte_switched, int flow_id, int src_id ){
tcp_handshake();
TCPHeader header, ack_header;
// this is the data that is transmitted. A sizeof(TCPHeader) header followed by a sring of dashes
char buf[packet_size];
memset(buf, '-', sizeof(char)*packet_size);
buf[packet_size-1] = '\0';
// for link logging
ofstream link_logfile;
if( LINK_LOGGING )
link_logfile.open( LINK_LOGGING_FILENAME, ios::out | ios::app );
// for flow control
_last_send_time = 0.0;
double cur_time = 0;
int seq_num = 0;
_last_send_time = 0.0;
_largest_ack = -1;
// for maintaining performance statistics
double delay_sum = 0;
int num_packets_transmitted = 0;
int transmitted_bytes = 0;
cout << "Assuming training link rate of: " << TRAINING_LINK_RATE << " pkts/sec" << endl;
chrono::high_resolution_clock::time_point start_time_point = chrono::high_resolution_clock::now();
cur_time = current_timestamp( start_time_point );
_last_send_time = cur_time;
congctrl.set_timestamp(cur_time);
congctrl.init();
// Get min_rtt from outside
const char* min_rtt_c = getenv("MIN_RTT");
congctrl.set_min_rtt(atof(min_rtt_c));
while ((byte_switched?(num_packets_transmitted*data_size):cur_time) < flow_size) {
cur_time = current_timestamp( start_time_point );
// Warning: The number of unacknowledged packets may exceed the congestion window by num_packets_per_link_rate_measurement
while ((seq_num < _largest_ack + 1 + congctrl.get_the_window()) &&
(_last_send_time + congctrl.get_intersend_time() <= cur_time) &&
(byte_switched?(num_packets_transmitted*data_size):cur_time) < flow_size ) {
header.seq_num = seq_num;
header.flow_id = flow_id;
header.src_id = src_id;
header.sender_timestamp = cur_time;
header.receiver_timestamp = 0;
memcpy( buf, &header, sizeof(TCPHeader) );
socket.senddata( buf, packet_size, NULL );
if ((_last_send_time - cur_time) / congctrl.get_intersend_time() > 10)
// Hopeless. Stop trying to compensate.
_last_send_time = cur_time;
else
_last_send_time += congctrl.get_intersend_time();
congctrl.set_timestamp(cur_time);
congctrl.onPktSent( header.seq_num );
seq_num++;
cur_time = current_timestamp( start_time_point );
}
cur_time = current_timestamp( start_time_point );
double timeout = _last_send_time + congctrl.get_timeout(); // everything in milliseconds
if(congctrl.get_the_window() > 0)
timeout = min( timeout, _last_send_time + congctrl.get_intersend_time()*num_packets_per_link_rate_measurement - cur_time );
sockaddr_in other_addr;
if(socket.receivedata(buf, packet_size, timeout, other_addr) == 0) {
cur_time = current_timestamp(start_time_point);
if(cur_time > _last_send_time + congctrl.get_timeout())
congctrl.onTimeout();
continue;
}
memcpy(&ack_header, buf, sizeof(TCPHeader));
ack_header.seq_num++; // because the receiver doesn't do that for us yet
if (ack_header.src_id != src_id || ack_header.flow_id != flow_id){
if(ack_header.src_id != src_id ){
std::cerr<<"Received incorrect ack for src "<<ack_header.src_id<<" to "<<src_id<<endl;
}
continue;
}
cur_time = current_timestamp( start_time_point );
// Track performance statistics
delay_sum += cur_time - ack_header.sender_timestamp;
this->tot_delay += cur_time - ack_header.sender_timestamp;
transmitted_bytes += data_size;
this->tot_bytes_transmitted += data_size;
num_packets_transmitted += 1;
this->tot_packets_transmitted += 1;
congctrl.set_timestamp(cur_time);
congctrl.onACK(ack_header.seq_num,
ack_header.receiver_timestamp,
ack_header.sender_timestamp);
#ifdef SCALE_SEND_RECEIVE_EWMA
assert(false);
#endif
_largest_ack = max(_largest_ack, ack_header.seq_num);
}
cur_time = current_timestamp( start_time_point );
congctrl.set_timestamp(cur_time);
congctrl.close();
this->tot_time_transmitted += cur_time;
double throughput = transmitted_bytes/( cur_time / 1000.0 );
double delay = (delay_sum / 1000) / num_packets_transmitted;
std::cout<<"\n\nData Successfully Transmitted\n\tThroughput: "<<throughput<<" bytes/sec\n\tAverage Delay: "<<delay<<" sec/packet\n";
double avg_throughput = tot_bytes_transmitted / ( tot_time_transmitted / 1000.0);
double avg_delay = (tot_delay / 1000) / tot_packets_transmitted;
std::cout<<"\n\tAvg. Throughput: "<<avg_throughput<<" bytes/sec\n\tAverage Delay: "<<avg_delay<<" sec/packet\n";
if( LINK_LOGGING )
link_logfile.close();
}
template<class T>
void CTCP<T>::listen_for_data ( ){
}
#endif