-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_socket.c
More file actions
319 lines (274 loc) · 8.34 KB
/
tcp_socket.c
File metadata and controls
319 lines (274 loc) · 8.34 KB
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
#define _GNU_SOURCE
#include <sched.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "hwtimer.h"
#define BACKLOG 50
#define DEFAULT_PORT 9000
#define LATENCY_RUNS 100
#define die(msg) \
do { \
char str[100]; \
snprintf(str, 100, "%s(%d): %s", __func__, __LINE__, msg); \
perror(str); \
exit(1); \
} while(0) \
void set_affinity(int cpuid) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpuid, &set);
if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
perror("sched_affinity");
}
void parent(int port, unsigned long int bufsize, int tput) {
int fd, confd, flag = 1;
struct sockaddr_in my;
struct sockaddr_storage their;
socklen_t sz = sizeof(their);
int info;
char *buffer = calloc(sizeof(char), bufsize);
if (buffer == NULL) {
die("cannot alloc memory for rx");
}
set_affinity(0);
fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd < 0) {
die("Socket open failed");
}
// So that we dont have to wait for around 1min for OS
// to recycle the socket
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag,
sizeof(int)) == -1) {
close(fd);
die("Unable to set REUSEADDR");
}
my.sin_addr.s_addr = INADDR_ANY;
my.sin_family = AF_INET;
my.sin_port = htons(port);
if (bind(fd, (struct sockaddr *)&my, sizeof(my)) != 0) {
close(fd);
die("Socket bind failed");
}
// We want the packet to be sent immediately, disable Nagle
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
sizeof(int)) == -1) {
close(fd);
die("Cannot disable Nagle");
}
if (listen(fd, BACKLOG) == -1) {
close(fd);
die("Cannot listen");
}
confd = accept(fd, (struct sockaddr *)&their, &sz);
if (confd == -1) {
close(fd);
die("cannot accept connection");
}
if (setsockopt(confd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
sizeof(int)) == -1) {
close(fd);
close(confd);
die("Cannot disable Nagle!");
}
if (tput == 0) {
int i;
for (i = 0; i < LATENCY_RUNS; i++) {
ssize_t r = 0, ret;
// maybe do buffer + r and bufsize -r here...
while ((ret = recv(confd, buffer, bufsize, 0)) > 0) {
r += ret;
if (r == bufsize) {
send(confd, (void *)buffer, bufsize, 0);
break;
}
}
if (ret == -1) {
perror("Error in receiving packets");
} else if (ret == 0) {
printf("Remote connection closed\n");
}
}
} else {
// TPUT test, we will receive atleast a 100MB of data
int num_pkts = (100 * 1024 * 1024) / bufsize;
// avoid errors due to remainder
int tot_size = num_pkts * bufsize;
ssize_t r = 0, ret;
// naive receive...
while ((ret = recv(confd, buffer, bufsize, 0)) > 0) {
r += ret;
if (r == tot_size) {
send(confd, (void *)buffer, 1, 0);
break;
}
}
if (ret == -1) {
perror("Error in receiving packets");
} else if (ret == 0) {
printf("Remote connection closed\n");
}
}
wait(&info);
close(fd);
close(confd);
}
void child(int port, unsigned long int bufsize, int tput) {
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET_ADDRSTRLEN];
int socketfd;
int flag;
char s_port[10];
char *buffer = calloc(sizeof(char), bufsize);
if (buffer == NULL) {
die("cannot alloc memory for tx");
}
set_affinity(1);
snprintf(s_port, sizeof(s_port), "%d", port);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo("localhost", s_port, &hints, &res)) != 0) {
printf("getaddrinfo: %s\n", gai_strerror(status));
die("Cannot get remote address info");
}
for (p = res; p != NULL; p = p->ai_next) {
void *addr;
if (p->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *) p->ai_addr;
addr = &(ipv4->sin_addr);
} else {
// Sorry, no IPV6
continue;
}
// convert the IP to a string and print it if required
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
break;
}
socketfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (socketfd < 0) {
die("Socket open failed");
}
int ret = setsockopt(socketfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
sizeof(int));
if (ret < 0) {
die("Cannot disable Nagle!");
}
if (connect(socketfd, p->ai_addr, p->ai_addrlen) == -1) {
close(socketfd);
die("client connect");
return;
}
if (tput == 0) {
int i;
hwtimer_t tsc_t;
uint64_t ns_time[LATENCY_RUNS];
for (i = 0; i < LATENCY_RUNS; i++) {
ssize_t r = 0, ret;
init_timer(&tsc_t);
start_timer(&tsc_t);
if (send(socketfd, (void *)buffer, bufsize, 0) == -1) {
perror("Send error in child");
}
// Do a round trip
while ((ret = recv(socketfd, buffer, bufsize, 0)) > 0) {
r += ret;
if (r == bufsize)
break;
}
stop_timer(&tsc_t);
ns_time[i]=get_timer_ns(&tsc_t)/2;
if (ret == -1) {
perror("Child: Error in receiving packets");
} else if (ret == 0) {
printf("Child: Remote connection closed\n");
}
}
for (i = 0;i < LATENCY_RUNS; i++) {
printf("%lu", ns_time[i]);
if(i!=LATENCY_RUNS-1){
printf("\n");
}
}
} else {
// TPUT test, send atleast a 100MB of data
int num_pkts = (100 * 1024 * 1024) / bufsize;
ssize_t tot_size = num_pkts * bufsize;
int i = 0;
ssize_t ret;
hwtimer_t tsct;
uint64_t ns_time;
init_timer(&tsct);
start_timer(&tsct);
for (i = 0; i < num_pkts; i++) {
if (send(socketfd, (void *)buffer, bufsize, 0) == -1) {
perror("Send error in child");
break;
}
}
// receive ack
if ((ret = recv(socketfd, buffer, bufsize, 0)) > 0) {
// calculate timings
}
stop_timer(&tsct);
ns_time = get_timer_ns(&tsct);
printf("%f \n", (1000)*((float)tot_size/ns_time));
if (ret == -1) {
perror("Child: Error in receiving packets");
} else if (ret == 0) {
printf("Child: Remote connection closed\n");
}
}
}
int main(int argc, char **argv) {
int pid;
int ch;
unsigned long int port = DEFAULT_PORT;
int tput = 0;
unsigned long int size = 4;
// make provision to specify the port in case
// DEFAULT_PORT(9000) does not work
while ((ch = getopt(argc, argv, "p:s:t")) != -1) {
switch (ch) {
case 'p':
port = strtoul(optarg, NULL, 10);
if (port <= 1024 || port > 65536) {
printf("Invalid Port\n");
exit(1);
}
break;
case 't':
tput = 1;
break;
case 's':
size = strtoul(optarg, NULL, 10);
break;
case '?':
default:
printf("Not enough arguments\n");
}
}
//printf("Operating on port %lu, size %lu\n", port, size);
pid = fork();
if (pid == -1) {
die("Unable to fork");
} else if (pid != 0) {
// In parent, do parent related stuff
parent(port, size, tput);
} else {
// In child process
// wait for parent to initialize the port
// sleep(2);
child(port, size, tput);
}
return 0;
}