demikernel 1.5.13

Kernel-Bypass LibOS Architecture
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// NOTE: libc requires this for RTLD_NEXT.
#define _GNU_SOURCE

#include "epoll.h"
#include "error.h"
#include "qman.h"
#include "utils.h"
#include <assert.h>
#include <demi/types.h>
#include <dlfcn.h>
#include <errno.h>
#include <glue.h>
#include <hooks.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>

#define INTERPOSE_CALL2(type, fn_libc, fn_demi, ...) \
    {                                                \
        int last_errno = errno;                      \
        errno = 0;                                   \
                                                     \
        type ret = fn_demi(__VA_ARGS__);             \
                                                     \
        if (ret == -1)                               \
        {                                            \
            if (errno == EBADF)                      \
            {                                        \
                errno = last_errno;                  \
                return fn_libc(__VA_ARGS__);         \
            }                                        \
            else                                     \
            {                                        \
                return ret;                          \
            }                                        \
        }                                            \
                                                     \
        errno = last_errno;                          \
                                                     \
        return ret;                                  \
    }

#define INTERPOSE_CALL(type, fn_libc, fn_demi, ...)                  \
    {                                                                \
        init_libc();                                                 \
                                                                     \
        if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call())) \
        {                                                            \
            return (fn_libc(__VA_ARGS__));                           \
        }                                                            \
                                                                     \
        init();                                                      \
                                                                     \
        INTERPOSE_CALL2(type, fn_libc, fn_demi, __VA_ARGS__);        \
    }

// System calls that we interpose.
static int (*libc_socket)(int, int, int) = NULL;
static int (*libc_close)(int) = NULL;
static int (*libc_shutdown)(int, int) = NULL;
static int (*libc_bind)(int, const struct sockaddr *, socklen_t) = NULL;
static int (*libc_connect)(int, const struct sockaddr *, socklen_t) = NULL;
static int (*libc_fcntl)(int, int, ...) = NULL;
static int (*libc_listen)(int, int) = NULL;
static int (*libc_accept4)(int, struct sockaddr *, socklen_t *, int) = NULL;
static int (*libc_accept)(int, struct sockaddr *, socklen_t *) = NULL;
static int (*libc_getsockopt)(int, int, int, void *, socklen_t *) = NULL;
static int (*libc_setsockopt)(int, int, int, const void *, socklen_t) = NULL;
static int (*libc_getsockname)(int, struct sockaddr *, socklen_t *) = NULL;
static int (*libc_getpeername)(int, struct sockaddr *, socklen_t *) = NULL;
static ssize_t (*libc_read)(int, void *, size_t) = NULL;
static ssize_t (*libc_recv)(int, void *, size_t, int) = NULL;
static ssize_t (*libc_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *) = NULL;
static ssize_t (*libc_recvmsg)(int, struct msghdr *, int) = NULL;
static ssize_t (*libc_readv)(int, const struct iovec *, int) = NULL;
static ssize_t (*libc_pread)(int, void *, size_t, off_t) = NULL;
static ssize_t (*libc_write)(int, const void *, size_t) = NULL;
static ssize_t (*libc_send)(int, const void *, size_t, int) = NULL;
static ssize_t (*libc_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t) = NULL;
static ssize_t (*libc_sendmsg)(int, const struct msghdr *, int) = NULL;
static ssize_t (*libc_writev)(int, const struct iovec *, int) = NULL;
static ssize_t (*libc_pwrite)(int, const void *, size_t, off_t) = NULL;
static int (*libc_epoll_create)(int) = NULL;
static int (*libc_epoll_create1)(int) = NULL;
static int (*libc_epoll_ctl)(int, int, int, struct epoll_event *) = NULL;
static int (*libc_epoll_wait)(int, struct epoll_event *, int, int) = NULL;

static volatile uint8_t initialized_libc = 0;
static volatile uint8_t in_init_libc = 0;

static volatile uint8_t initialized = 0;
static volatile uint8_t in_init = 0;

static inline void init_libc(void)
{
    if (UNLIKELY(initialized_libc == 0))
    {

        if (__sync_val_compare_and_swap(&in_init_libc, 0, 1) == 0)
        {
            // Initialize libc functions
            assert((libc_socket = dlsym(RTLD_NEXT, "socket")) != NULL);
            assert((libc_shutdown = dlsym(RTLD_NEXT, "shutdown")) != NULL);
            assert((libc_bind = dlsym(RTLD_NEXT, "bind")) != NULL);
            assert((libc_connect = dlsym(RTLD_NEXT, "connect")) != NULL);
            assert((libc_fcntl = dlsym(RTLD_NEXT, "fcntl")) != NULL);
            assert((libc_listen = dlsym(RTLD_NEXT, "listen")) != NULL);
            assert((libc_accept4 = dlsym(RTLD_NEXT, "accept4")) != NULL);
            assert((libc_accept = dlsym(RTLD_NEXT, "accept")) != NULL);
            assert((libc_getsockopt = dlsym(RTLD_NEXT, "getsockopt")) != NULL);
            assert((libc_setsockopt = dlsym(RTLD_NEXT, "setsockopt")) != NULL);
            assert((libc_getsockname = dlsym(RTLD_NEXT, "getsockname")) != NULL);
            assert((libc_getpeername = dlsym(RTLD_NEXT, "getpeername")) != NULL);
            assert((libc_read = dlsym(RTLD_NEXT, "read")) != NULL);
            assert((libc_recv = dlsym(RTLD_NEXT, "recv")) != NULL);
            assert((libc_recvfrom = dlsym(RTLD_NEXT, "recvfrom")) != NULL);
            assert((libc_recvmsg = dlsym(RTLD_NEXT, "recvmsg")) != NULL);
            assert((libc_readv = dlsym(RTLD_NEXT, "readv")) != NULL);
            assert((libc_pread = dlsym(RTLD_NEXT, "pread")) != NULL);
            assert((libc_write = dlsym(RTLD_NEXT, "write")) != NULL);
            assert((libc_send = dlsym(RTLD_NEXT, "send")) != NULL);
            assert((libc_sendto = dlsym(RTLD_NEXT, "sendto")) != NULL);
            assert((libc_sendmsg = dlsym(RTLD_NEXT, "sendmsg")) != NULL);
            assert((libc_writev = dlsym(RTLD_NEXT, "writev")) != NULL);
            assert((libc_pwrite = dlsym(RTLD_NEXT, "pwrite")) != NULL);
            assert((libc_close = dlsym(RTLD_NEXT, "close")) != NULL);
            assert((libc_epoll_create = dlsym(RTLD_NEXT, "epoll_create")) != NULL);
            assert((libc_epoll_create1 = dlsym(RTLD_NEXT, "epoll_create1")) != NULL);
            assert((libc_epoll_ctl = dlsym(RTLD_NEXT, "epoll_ctl")) != NULL);
            assert((libc_epoll_wait = dlsym(RTLD_NEXT, "epoll_wait")) != NULL);

            // Initialize this here so that we can use the reentrancy guard
            init_reent_guards();

            initialized_libc = 1;
            assert(__sync_val_compare_and_swap(&in_init_libc, 1, 0) == 1);
            MEM_BARRIER();
        }
        else
        {
            while (initialized_libc == 0)
            {
                sched_yield();
                MEM_BARRIER();
            }
        }
    }
}

static inline void init(void)
{
    if (UNLIKELY(initialized == 0))
    {
        if (__sync_val_compare_and_swap(&in_init, 0, 1) == 0)
        {
            int ret = __init();
            if (ret != 0 && ret != EEXIST)
                abort();

            initialized = 1;
            assert(__sync_val_compare_and_swap(&in_init, 1, 0) == 1);
            MEM_BARRIER();
        }
        else
        {
            while (initialized == 0)
            {
                sched_yield();
                MEM_BARRIER();
            }
        }
    }
}

int close(int sockfd)
{
    INTERPOSE_CALL(int, libc_close, __close, sockfd);
}

int shutdown(int sockfd, int how)
{
    INTERPOSE_CALL(int, libc_shutdown, __shutdown, sockfd, how);
}

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    INTERPOSE_CALL(int, libc_bind, __bind, sockfd, addr, addrlen);
}

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    INTERPOSE_CALL(int, libc_connect, __connect, sockfd, addr, addrlen);
}

static int vfcntl(int sockfd, int cmd, va_list val)
{
    int ret = -1;

    init_libc();

    // Variadic functions cannot be easily interposed.
    // We need to parse the command and call the underlying function accordingly.
    switch (cmd)
    {
    // The following commands take no argument.
    case F_GETFD:
    case F_GETFL:
    case F_GETOWN:
    case F_GETSIG:
    case F_GETLEASE:
    case F_GETPIPE_SZ:
#ifdef F_GET_SEALS
    case F_GET_SEALS:
#endif
    {
        if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
        {
            return (libc_fcntl(sockfd, cmd));
        }

        init();

        INTERPOSE_CALL2(int, libc_fcntl, __fcntl, sockfd, cmd);
    }
    break;

    // The following commands take an integer as an argument.
    case F_DUPFD:
    case F_DUPFD_CLOEXEC:
    case F_SETFD:
    case F_SETFL:
    case F_SETOWN:
    case F_SETSIG:
    case F_SETLEASE:
    case F_NOTIFY:
    case F_SETPIPE_SZ:
#ifdef F_ADD_SEALS
    case F_ADD_SEALS:
#endif
    {
        int arg_i = va_arg(val, int);

        if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
        {
            return (libc_fcntl(sockfd, cmd, arg_i));
        }

        init();

        INTERPOSE_CALL2(int, libc_fcntl, __fcntl, sockfd, cmd, arg_i);
    }
    break;

    // The following commands take a pointer as an argument.
    case F_SETLK:
    case F_SETLKW:
    case F_GETLK:
    case F_OFD_SETLK:
    case F_OFD_SETLKW:
    case F_OFD_GETLK:
    case F_GETOWN_EX:
    case F_SETOWN_EX:
#ifdef F_GET_RW_HINT
    case F_GET_RW_HINT:
    case F_SET_RW_HINT:
#endif
#ifdef F_GET_FILE_RW_HINT
    case F_GET_FILE_RW_HINT:
    case F_SET_FILE_RW_HINT:
#endif
    {
        void *arg_p = va_arg(val, void *);

        if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
        {
            return (libc_fcntl(sockfd, cmd, arg_p));
        }

        init();

        INTERPOSE_CALL2(int, libc_fcntl, __fcntl, sockfd, cmd, arg_p);
    }
    break;

    // Unsupported.
    default:
        ERROR("unsupported cmd (%u)\n", cmd);
        errno = EINVAL;
        ret = -1;
        break;
    }

    return ret;
}

int fcntl(int fd, int cmd, ...)
{
    va_list val;
    va_start(val, cmd);
    int ret = vfcntl(fd, cmd, val);
    va_end(val);

    return ret;
}

int listen(int sockfd, int backlog)
{
    INTERPOSE_CALL(int, libc_listen, __listen, sockfd, backlog);
}

int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
    INTERPOSE_CALL(int, libc_accept4, __accept4, sockfd, addr, addrlen, flags);
}

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    INTERPOSE_CALL(int, libc_accept, __accept, sockfd, addr, addrlen);
}

int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
{
    INTERPOSE_CALL(int, libc_getsockopt, __getsockopt, sockfd, level, optname, optval, optlen);
}

int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
{
    INTERPOSE_CALL(int, libc_setsockopt, __setsockopt, sockfd, level, optname, optval, optlen);
}

int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    INTERPOSE_CALL(int, libc_getsockname, __getsockname, sockfd, addr, addrlen);
}

int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
    INTERPOSE_CALL(int, libc_getpeername, __getpeername, sockfd, addr, addrlen);
}

ssize_t read(int sockfd, void *buf, size_t count)
{
    INTERPOSE_CALL(ssize_t, libc_read, __read, sockfd, buf, count);
}

ssize_t recv(int sockfd, void *buf, size_t len, int flags)
{
    INTERPOSE_CALL(ssize_t, libc_recv, __recv, sockfd, buf, len, flags);
}

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
{
    INTERPOSE_CALL(ssize_t, libc_recvfrom, __recvfrom, sockfd, buf, len, flags, src_addr, addrlen);
}

ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
{
    INTERPOSE_CALL(ssize_t, libc_recvmsg, __recvmsg, sockfd, msg, flags);
}

ssize_t readv(int sockfd, const struct iovec *iov, int iovcnt)
{
    INTERPOSE_CALL(ssize_t, libc_readv, __readv, sockfd, iov, iovcnt);
}

ssize_t write(int sockfd, const void *buf, size_t count)
{
    INTERPOSE_CALL(ssize_t, libc_write, __write, sockfd, buf, count);
}

ssize_t send(int sockfd, const void *buf, size_t len, int flags)
{
    INTERPOSE_CALL(ssize_t, libc_send, __send, sockfd, buf, len, flags);
}

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
{
    INTERPOSE_CALL(ssize_t, libc_sendto, __sendto, sockfd, buf, len, flags, dest_addr, addrlen);
}

ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)
{
    INTERPOSE_CALL(ssize_t, libc_sendmsg, __sendmsg, sockfd, msg, flags);
}

ssize_t writev(int sockfd, const struct iovec *iov, int iovcnt)
{
    INTERPOSE_CALL(ssize_t, libc_writev, __writev, sockfd, iov, iovcnt);
}

ssize_t pread(int sockfd, void *buf, size_t count, off_t offset)
{
    INTERPOSE_CALL(ssize_t, libc_pread, __pread, sockfd, buf, count, offset);
}

ssize_t pwrite(int sockfd, const void *buf, size_t count, off_t offset)
{
    INTERPOSE_CALL(ssize_t, libc_pwrite, __pwrite, sockfd, buf, count, offset);
}

int epoll_create1(int flags)
{
    if (flags != 0)
        WARN("flags != 0");

    return (epoll_create(EPOLL_MAX_FDS));
}

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
    init_libc();

    if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
    {
        return (libc_epoll_ctl(epfd, op, fd, event));
    }

    init();

    int last_errno = errno;
    errno = 0;

    int ret = __epoll_ctl(epfd, op, fd, event);

    if (ret == -1)
    {
        if (errno == EBADF)
        {
            errno = last_errno;
            if (epfd >= EPOLL_MAX_FDS)
                epfd -= EPOLL_MAX_FDS;
            return (libc_epoll_ctl(epfd, op, fd, event));
        }
        else
        {
            return ret;
        }
    }

    errno = last_errno;

    return ret;
}

int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
{
    init_libc();

    if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
    {
        return (libc_epoll_wait(epfd, events, maxevents, timeout));
    }

    init();

    int last_errno = errno;
    errno = 0;

    int ret = __epoll_wait(epfd, events, maxevents, timeout);

    if (ret == -1)
    {
        if (errno == EBADF)
        {
            errno = last_errno;
            if (epfd >= EPOLL_MAX_FDS)
                epfd -= EPOLL_MAX_FDS;

            return (libc_epoll_wait(epfd, events, maxevents, timeout));
        }
        else
        {
            return ret;
        }
    }

    errno = last_errno;

    return ret;
}

int socket(int domain, int type, int protocol)
{
    INTERPOSE_CALL(int, libc_socket, __socket, domain, type, protocol);
}

int epoll_create(int size)
{
    init_libc();

    if (UNLIKELY(in_init) || UNLIKELY(is_reentrant_demi_call()))
    {
        return (libc_epoll_create(size));
    }

    init();

    // Create epoll on kernel side.
    int ret = libc_epoll_create(size);
    if (ret == -1)
    {
        ERROR("epoll_create() failed - %s", strerror(errno));
        return (ret);
    }

    int linux_epfd = ret;

    int last_errno = errno;
    errno = 0;

    // Create epoll on demikernel side.
    if (((ret = __epoll_create(size)) == -1) && (errno == EBADF))
    {
        errno = last_errno;
        return linux_epfd;
    }

    int demikernel_epfd = ret;

    queue_man_register_linux_epfd(linux_epfd, demikernel_epfd);

    return linux_epfd + EPOLL_MAX_FDS;
}