-
Notifications
You must be signed in to change notification settings - Fork 1
/
ku-latency.c
263 lines (207 loc) · 7.95 KB
/
ku-latency.c
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
/*
Copyright (c) 2008, Max Vilimpoc, http://vilimpoc.org/
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _XOPEN_SOURCE 600
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <getopt.h>
typedef enum
{
STRERROR_LEN = 80,
NUM_LATENCIES = 32,
DEFAULT_PORT = 1025,
} CONSTS;
static int inSocket;
static struct sockaddr_in inInAddr;
static int totalUsec;
static int totalPackets;
static int latencies[NUM_LATENCIES];
static int index;
static int rollingAverage;
static bool keepRunning;
void Usage(const char * const progName)
{
printf("\n");
printf("Usage: %s [-i IP address] [-e ethx] [-p port]\n", progName);
printf("\n");
printf(" -h Help.\n");
printf(" -i Specifies IP address of interface you want to listen to.\n");
printf(" -e Specifies the ethernet interface name you want to listen to.\n");
printf(" -p Specifies port number of packets you want to see.\n");
printf("\n");
printf("If no option is specified (they are all optional), then the program\n");
printf("will listen on IPADDR_ANY (all interfaces), port 1025.");
printf("\n");
printf("\n");
}
static void printError(int errorCode, const char * const lastFunction)
{
static char strError[STRERROR_LEN];
memset(strError, 0, STRERROR_LEN);
/* Old school. */
perror(lastFunction);
/* New school. */
strerror_r(errorCode, strError, STRERROR_LEN);
printf(strError);
printf("\n");
}
void catchIntr(int signalNumber)
{
/* Exit the main loop. */
keepRunning = false;
}
int main(int argc, char **argv)
{
int rc = 0;
/* Defaults */
inInAddr.sin_addr.s_addr = INADDR_ANY;
inInAddr.sin_port = htons(DEFAULT_PORT);
inInAddr.sin_family = AF_INET;
inSocket = socket(PF_INET, SOCK_DGRAM, 0);
if (0 > inSocket)
{
printf("socket() call failed.\n");
printError(inSocket, "socket");
rc = -1;
goto socket_failed;
}
/* Process cmdline opts. */
char *shortOpts = "hi:e:p:";
int getoptRet;
while(-1 != (getoptRet = getopt(argc, argv, shortOpts)))
{
switch(getoptRet)
{
case 'i':
inInAddr.sin_addr.s_addr = inet_addr(optarg);
break;
case 'e':
{
struct ifreq fetchIfInfo;
memset(&fetchIfInfo, 0, sizeof(struct ifreq));
memcpy(fetchIfInfo.ifr_name, optarg, IFNAMSIZ - 1);
/* Fetch the IP address to listen to based on interface name. */
ioctl(inSocket, SIOCGIFADDR, &fetchIfInfo);
struct sockaddr_in * const sockInfo = (struct sockaddr_in * const) &fetchIfInfo.ifr_addr;
inInAddr.sin_addr.s_addr = sockInfo->sin_addr.s_addr;
}
break;
case 'p':
inInAddr.sin_port = htons(atoi(optarg));
break;
case 'h':
case '?':
default:
Usage(argv[0]);
goto normal_exit;
break;
}
}
printf("Listening to: %s:%d\n", inet_ntoa(inInAddr.sin_addr),
ntohs(inInAddr.sin_port));
int timestampOn = 1;
rc = setsockopt(inSocket, SOL_SOCKET, SO_TIMESTAMP, (int *) ×tampOn, sizeof(timestampOn));
if (0 > rc)
{
printf("setsockopt(SO_TIMESTAMP) failed.\n");
printError(rc, "setsockopt");
goto setsockopt_failed;
}
rc = bind(inSocket, (struct sockaddr *) &inInAddr, sizeof(struct sockaddr_in));
if (0 > rc)
{
printf("UDP bind() failed.\n");
printError(rc, "bind");
goto bind_failed;
}
struct msghdr msg;
struct iovec iov;
char pktbuf[2048];
char ctrl[CMSG_SPACE(sizeof(struct timeval))];
struct cmsghdr *cmsg = (struct cmsghdr *) &ctrl;
msg.msg_control = (char *) ctrl;
msg.msg_controllen = sizeof(ctrl);
msg.msg_name = &inInAddr;
msg.msg_namelen = sizeof(inInAddr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = pktbuf;
iov.iov_len = sizeof(pktbuf);
struct timeval time_kernel, time_user;
int timediff;
printf("Starting main loop.\n");
for(keepRunning = true; keepRunning;)
{
rc = recvmsg(inSocket, &msg, 0);
gettimeofday(&time_user, NULL);
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_TIMESTAMP &&
cmsg->cmsg_len == CMSG_LEN(sizeof(time_kernel)))
{
memcpy(&time_kernel, CMSG_DATA(cmsg), sizeof(time_kernel));
}
printf("\n");
printf("time_kernel : %d.%06d\n", (int) time_kernel.tv_sec,
(int) time_kernel.tv_usec);
printf("time_user : %d.%06d\n", (int) time_user.tv_sec,
(int) time_user.tv_usec);
timediff = (time_user.tv_sec - time_kernel.tv_sec) * 1000000 +
(time_user.tv_usec - time_kernel.tv_usec);
totalUsec += timediff;
++totalPackets;
rollingAverage += timediff;
rollingAverage -= latencies[index];
latencies[index] = timediff;
index = (index + 1) % NUM_LATENCIES;
printf("Total Average : %d/%d = %.2f us\n", totalUsec,
totalPackets,
(float) totalUsec / totalPackets);
printf("Rolling Average (%d samples) : %.2f us\n", NUM_LATENCIES,
(float) rollingAverage / NUM_LATENCIES);
}
bind_failed:
setsockopt_failed:
close(inSocket);
socket_failed:
normal_exit:
return rc;
}