-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlk_onload_stub.c
353 lines (287 loc) · 8.59 KB
/
lk_onload_stub.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright 2023 Google LLC
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include <stddef.h>
#include <dlfcn.h>
#include <error.h>
#include <errno.h>
#include <limits.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <linux/errqueue.h> /* after time.h, for timespec */
#include <linux/net_tstamp.h>
#include "lk_onload_stub_ext.h"
/* file scope definitions */
static int lkos_log_fd; /* 0 (STDIN_FILENO) means disabled */
static int (*getsockopt_fn)(int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
static int (*recvmmsg_fn)(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
int flags, struct timespec *timeout);
static ssize_t (*recvmsg_fn)(int sockfd, struct msghdr *msg, int flags);
static int (*setsockopt_fn)(int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
/* library support functions */
static void lkos_log(const char *fmt, ...)
{
if (lkos_log_fd) {
va_list args;
va_start(args, fmt);
vdprintf(lkos_log_fd, fmt, args);
va_end(args);
}
}
static int __attribute__((unused)) __lkos_error(int err, const char *msg, const char *fn, int lineno)
{
if (msg)
lkos_log("%s.%d: %s\n", fn, lineno, msg);
errno = err;
return 1;
}
#define lkos_error(err, msg) __lkos_error(err, msg, __func__, __LINE__)
static void * __attribute__((used)) lkos_dlsym(const char *symbol_str)
{
void *fn;
fn = dlsym(RTLD_NEXT, symbol_str);
if (!fn) {
lkos_log("%s: %s: %s\n", __func__, symbol_str, dlerror());
exit(1);
}
return fn;
};
static void lkos_init_log(void)
{
unsigned long fd_val;
const char *fd_str;
fd_str = getenv("LKOS_LOG_FD");
if (!fd_str)
return;
fd_val = strtoul(fd_str, NULL, 0);
if (fd_val > INT_MAX)
return;
lkos_log_fd = (int) fd_val;
lkos_log("lk_onload_stub loaded\n");
}
static void __attribute__((constructor)) lkos_init(void)
{
lkos_init_log();
getsockopt_fn = lkos_dlsym("getsockopt");
recvmmsg_fn = lkos_dlsym("recvmmsg");
recvmsg_fn = lkos_dlsym("recvmsg");
setsockopt_fn = lkos_dlsym("setsockopt");
}
/* intercepted functions */
static int __getsockopt_timestamping(int sockfd, void *optval, socklen_t *optlen)
{
struct so_timestamping *ts = (struct so_timestamping *)optval;
int ret;
ret = getsockopt_fn(sockfd, SOL_SOCKET, SO_TIMESTAMPING, optval, optlen);
if (ret)
return ret;
if (*optlen != sizeof(*ts) && *optlen != sizeof(ts->flags))
return lkos_error(EINVAL, NULL);
/* See __setsockopt_timestamping for matching code
*
* If hardware timestamp reporting is enabled, but no recording,
* then it is likely a conversion by that function. Invert when
* returning the current flags in getsockopt.
*/
if (ts->flags & SOF_TIMESTAMPING_RAW_HARDWARE &&
!(ts->flags & SOF_TIMESTAMPING_TX_HARDWARE) &&
!(ts->flags & SOF_TIMESTAMPING_RX_HARDWARE)) {
if (ts->flags & SOF_TIMESTAMPING_RX_SOFTWARE) {
ts->flags |= SOF_TIMESTAMPING_RX_HARDWARE;
ts->flags &= ~(SOF_TIMESTAMPING_RX_SOFTWARE |
SOF_TIMESTAMPING_SOFTWARE);
} if (ts->flags & SOF_TIMESTAMPING_TX_SOFTWARE) {
ts->flags |= SOF_TIMESTAMPING_TX_HARDWARE;
ts->flags &= ~(SOF_TIMESTAMPING_TX_SOFTWARE |
SOF_TIMESTAMPING_SOFTWARE);
}
}
return 0;
}
int getsockopt(int sockfd, int level, int optname,
void *optval, socklen_t *optlen)
{
if (level == SOL_SOCKET &&
optname == SO_TIMESTAMPING)
return __getsockopt_timestamping(sockfd, optval, optlen);
return getsockopt_fn(sockfd, level, optname, optval, optlen);
}
int onload_fd_stat(int fd, void *unused)
{
return 0;
}
int onload_is_present(void)
{
return 0;
}
int onload_move_fd(int fd)
{
return 0;
}
int onload_ordered_epoll_wait(int epfd, struct epoll_event *events,
struct onload_ordered_epoll_event *oo_events,
int maxevents, int timeout)
{
int i, ret;
ret = epoll_wait(epfd, events, maxevents, timeout);
/* tv_sec == 0 disables wire-order, defined in onload_extensions.h */
for (i = 0; i < ret; i++)
oo_events[i].ts.tv_sec = 0;
return ret;
}
int onload_set_stackname(int who, int scope, const char* stackname)
{
return 0;
}
int onload_socket_nonaccel(int domain, int type, int protocol)
{
return socket(domain, type, protocol);
}
int onload_stackname_restore(void)
{
return 0;
}
int onload_stackname_save(void)
{
return 0;
}
int onload_stack_opt_get_int(const char* opt, int64_t *val)
{
return -1;
}
int onload_stack_opt_get_str(const char* opt, char* val_out, size_t* val_out_len)
{
return -1;
}
int onload_stack_opt_reset(void)
{
return 0;
}
int onload_stack_opt_set_int(const char* opt, int64_t val)
{
return 0;
}
int onload_stack_opt_set_str(const char* opt, const char* val)
{
return 0;
}
static void __recvmsg_timestamping(struct msghdr *msg)
{
struct scm_timestamping *tss;
struct cmsghdr *cm;
/* Unconditionally copy the sw timestamp from ts[0]
* to the raw hw timestamp field ts[2]
*
* The fields are undefined if the relevant SOF_.. option was not set,
* so it is safe to fill in both fields if only one at a time is set.
*/
for (cm = CMSG_FIRSTHDR(msg);
cm && cm->cmsg_len;
cm = CMSG_NXTHDR(msg, cm)) {
if (cm->cmsg_level == SOL_SOCKET &&
cm->cmsg_type == SCM_TIMESTAMPING) {
tss = (void *) CMSG_DATA(cm);
tss->ts[2] = tss->ts[0];
}
}
}
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
{
ssize_t ret;
ret = recvmsg_fn(sockfd, msg, flags);
if (ret >= 0 && msg->msg_control && msg->msg_controllen)
__recvmsg_timestamping(msg);
return ret;
}
int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
int flags, struct timespec *timeout)
{
int ret, i;
ret = recvmmsg_fn(sockfd, msgvec, vlen, flags, timeout);
for (i = 0; i < ret; i++) {
struct msghdr *mh = &msgvec[i].msg_hdr;
if (mh->msg_control && mh->msg_controllen)
__recvmsg_timestamping(mh);
}
return ret;
}
/* optval is defined as const, but not here, as it may be modified. */
static int __setsockopt_timestamping(int sockfd, void *optval, socklen_t optlen)
{
const int hw_tx = SOF_TIMESTAMPING_TX_HARDWARE |
SOF_TIMESTAMPING_RAW_HARDWARE;
const int sw_tx = SOF_TIMESTAMPING_TX_SOFTWARE |
SOF_TIMESTAMPING_SOFTWARE;
const int hw_rx = SOF_TIMESTAMPING_RX_HARDWARE |
SOF_TIMESTAMPING_RAW_HARDWARE;
const int sw_rx = SOF_TIMESTAMPING_RX_SOFTWARE |
SOF_TIMESTAMPING_SOFTWARE;
struct so_timestamping ts = *(struct so_timestamping *)optval;
if (optlen != sizeof(ts) && optlen != sizeof(ts.flags))
return lkos_error(EINVAL, NULL);
/* Convert hardware timestamp recording requests to software.
*
* SO_TIMESTAMPING combines
* - recording options, such as SOF_TIMESTAMPING_TX_HARDWARE
* - reporting options, such as SOF_TIMESTAMPING_RAW_HARDWARE
* see Documentation/networking/timestamping.rst for details.
*
* If only hardware timestamping is requested,
* - convert the record flag to software and
* - enable the software report flag.
* to start receiving software timestamps.
*
* Keep the hardware report flag. We will use that in getsockopt
* as a hint that hardware timestamping was originally requested
* and we converted it here. This is a hopefully decent heuristic,
* because requesting a report without matching record is a noop,
* and thus not something normal applications would do. That said,
* the approach *is* a heuristic, so not foolproof: getsockopt
* might convert incorrectly in some cases.
*/
if ((ts.flags & (sw_tx | sw_rx)) == 0) {
if ((ts.flags & hw_tx) == hw_tx) {
ts.flags &= ~SOF_TIMESTAMPING_TX_HARDWARE;
ts.flags |= sw_tx;
}
if ((ts.flags & hw_rx) == hw_rx) {
ts.flags &= ~SOF_TIMESTAMPING_RX_HARDWARE;
ts.flags |= sw_rx;
}
}
return setsockopt_fn(sockfd, SOL_SOCKET, SO_TIMESTAMPING, &ts, optlen);
}
int setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen)
{
if (level == SOL_SOCKET &&
optname == SO_TIMESTAMPING)
return __setsockopt_timestamping(sockfd, (void *)optval, optlen);
return setsockopt_fn(sockfd, level, optname, optval, optlen);
}