-
Notifications
You must be signed in to change notification settings - Fork 6
/
rx_lat.c
288 lines (233 loc) · 5.7 KB
/
rx_lat.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
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
/*
* Copyright (C) 2017
* Authors: Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/net_tstamp.h>
#include <time.h>
#include <linux/errqueue.h>
#include <net/ethernet.h>
#include <unistd.h>
#include <stdio.h>
#include "plget.h"
#include "stat.h"
#include "rx_lat.h"
#include <errno.h>
#include <poll.h>
#include "xdp_sock.h"
#include <string.h>
#define RATE_INERVAL 1
static int rxlat_handle_ts(struct timespec *ts, __u32 ts_id)
{
struct scm_timestamping *tss = NULL;
struct msghdr *msg = &plget->msg;
struct cmsghdr *cmsg;
int id_offset;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_TIMESTAMPING)
continue;
tss = (struct scm_timestamping *) CMSG_DATA(cmsg);
break;
}
if (!tss) {
fprintf(stderr, "SCM_TIMESTAMPING not found!\n");
return -1;
}
id_offset = ts_id - rx_app_v.id;
if (id_offset && plget->flags & PLF_STRICT_ID_ORDER) {
if (id_offset == 1)
fprintf(stderr, "id order violation, expect %d, but %d\n",
rx_app_v.id, ts_id);
return -1;
}
stats_push_id(&rx_sw_v, tss->ts, ts_id);
stats_push_id(&rx_hw_v, tss->ts + 2, ts_id);
stats_push_id(&rx_app_v, ts, ts_id);
return 0;
}
static int rxlat_recvmsg_start(struct timespec *ts)
{
int flags, psize;
if (plget->pkt_type == PKT_XDP) {
psize = xsk_recvmsg_start(ts);
return psize;
}
flags = (plget->flags & PLF_SW_POLL) ? MSG_DONTWAIT : 0;
do
psize = recvmsg(plget->sfd, &plget->msg, flags);
while (psize <= 0 && flags);
if (clock_gettime(CLOCK_REALTIME, ts))
return -1;
return psize;
}
static int rxlat_recvmsg_raw_filter(int psize)
{
int ptp_pkt;
__u16 proto;
if (plget->pkt_type != PKT_XDP && plget->pkt_type != PKT_RAW)
return 0;
/* drop not expected packets */
memcpy(&proto, plget->rx_pkt + ETH_ALEN * 2, sizeof(proto));
ptp_pkt = (plget->flags & PLF_PTP) && proto == htons(ETH_P_1588);
if (psize < ETH_HLEN || !ptp_pkt) {
if (plget->pkt_type == PKT_XDP)
xsk_recvmsg_fail();
return -1;
}
if (plget->pkt_type == PKT_XDP)
xsk_recvmsg_complete(&plget->msg);
return 0;
}
static int rxlat_recvmsg(struct timespec *ts, __u32 *ts_id)
{
char *magic;
int psize;
for (;;) {
psize = rxlat_recvmsg_start(ts);
if (psize < 0)
return psize;
if (rxlat_recvmsg_raw_filter(psize))
continue;
/* check magic number */
magic = magic_rx_rd();
if (*magic == MAGIC) {
*ts_id = tid_rx_rd();
if (*ts_id > plget->pkt_num)
printf("incorrect ts_id\n");
break;
}
printf("incorrect rx MAGIC number 0x%x\n", *magic);
}
return psize;
}
void rxlat_proc_packet(void)
{
struct timespec ts;
int psize, ret;
__u32 ts_id;
for (;;) {
plget->msg.msg_controllen = sizeof(plget->control);
psize = rxlat_recvmsg(&ts, &ts_id);
if (psize < 0)
return perror("recvmsg");
ret = rxlat_handle_ts(&ts, ts_id);
if (!ret)
break;
}
plget->sk_payload_size = psize;
}
int rxlat(void)
{
plget->inum = plget->pkt_num;
for (plget->icnt = 0; plget->icnt < plget->pkt_num; ++plget->icnt)
rxlat_proc_packet();
return 0;
}
static int rxrate_proc_packet(struct timespec *ts)
{
struct msghdr *msg = &plget->msg;
struct scm_timestamping *tss = NULL;
struct cmsghdr *cmsg;
int psize;
msg->msg_controllen = sizeof(plget->control);
psize = recvmsg(plget->sfd, msg, 0);
if (psize < 0) {
return perror("recvmsg"), -errno;
}
plget->frame_size = psize;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_TIMESTAMPING)
continue;
tss = (struct scm_timestamping *) CMSG_DATA(cmsg);
break;
}
if (!tss) {
fprintf(stderr, "SCM_TIMESTAMPING not found!\n");
return -1;
}
if (ts_correct(tss->ts + 2)) {
ts->tv_sec = (tss->ts + 2)->tv_sec;
ts->tv_nsec = (tss->ts + 2)->tv_nsec;
return 1;
}
ts->tv_sec = tss->ts->tv_sec;
ts->tv_nsec = tss->ts->tv_nsec;
return 0;
}
int rxrate_proc(void)
{
struct timespec interval, first, last;
int dsize = 0, pnum = 0, hw = 0;
int hsize = ETH_HLEN;
struct pollfd fds[2];
uint64_t exps;
int ret;
if (plget->pkt_type == PKT_UDP)
hsize += 28;
ret = plget_start_timer();
if (ret)
return ret;
fds[0].fd = plget->sfd;
fds[0].events = POLLIN;
fds[1].fd = plget->timer_fd;
fds[1].events = POLLIN;
for (;;) {
ret = poll(fds, 2, -1);
if (ret <= 0)
return perror("Some error on poll()"), -errno;
/* receive packet */
if (fds[0].revents & POLLIN) {
hw = rxrate_proc_packet(&last);
if (hw >= 0) {
plget->frame_size += hsize;
dsize += plget->frame_size;
if (!pnum++)
first = last;
}
}
/* print speed */
if (fds[1].revents & POLLIN) {
ret = read(plget->timer_fd, &exps, sizeof(uint64_t));
if (ret < 0)
return perror("Couldn't read timerfd"), -errno;
if (pnum <= 1) {
interval = plget->interval;
} else {
ts_sub(&last, &first, &interval);
dsize -= plget->frame_size;
pnum--;
}
hw ? printf("H/W ") : printf("S/W ");
stats_drate_print(&interval, pnum, dsize);
dsize = 0;
pnum = 0;
}
}
return 0;
}
static int init_rxrate(void)
{
if (!ts_correct(&plget->interval))
plget->interval.tv_sec = 1;
return plget_create_timer();
}
int rxrate(void)
{
int ret;
ret = init_rxrate();
if (ret)
return ret;
ret = rxrate_proc();
close(plget->timer_fd);
return ret;
}