summaryrefslogtreecommitdiff
path: root/entry/src/kni_iouring.cpp
blob: d9845cc48767a06bb8608609f7035343395f610b (plain)
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
#include "kni_iouring.h"

#if (SUPPORT_LIBURING)

struct user_data *io_uring_user_data_create(int sockfd, enum evtype type, int buff_size)
{
    struct user_data *conn = (struct user_data *)calloc(1, sizeof(struct user_data));
    conn->sockfd = sockfd;
    conn->type = type;
    conn->vec.iov_base = (void *)calloc(buff_size, sizeof(char));
    conn->vec.iov_len = buff_size;

    return conn;
}

void io_uring_user_data_destory(struct user_data *conn)
{
    if (conn)
    {
        if (conn->vec.iov_base)
        {
            free(conn->vec.iov_base);
            conn->vec.iov_base = NULL;
        }

        free(conn);
        conn = NULL;
    }
}

void io_uring_handle_destory(struct io_uring_handle *handle)
{
    if (handle)
    {
        io_uring_queue_exit(&handle->ring);

        free(handle);
        handle = NULL;
    }
}

struct io_uring_handle *io_uring_handle_create(int ring_size, int buff_size, int flags, int sq_thread_idle)
{
    struct io_uring_handle *handle = (struct io_uring_handle *)calloc(1, sizeof(struct io_uring_handle));
    if (handle == NULL)
    {
        return NULL;
    }

    handle->ring_size = ring_size;
    handle->buff_size = buff_size;

    /*
     * 参考资料:https://unixism.net/loti/tutorial/sq_poll.html#sq-poll
     *
     * 执行下面命令进行验证 IORING_SETUP_SQPOLL:
     * sudo bpftrace -e 'tracepoint:io_uring:io_uring_submit_sqe {printf("%s(%d)\n", comm, pid);}'
     */
    if (flags)
    {
        handle->params.flags |= flags;
    }
    if (sq_thread_idle)
    {
        handle->params.sq_thread_idle = sq_thread_idle; // milliseconds
    }

    int ret = io_uring_queue_init_params(ring_size, &handle->ring, &handle->params);
    if (ret)
    {
        fprintf(stderr, "Failed at io_uring_queue_init_params(), %s\n", strerror(-ret));
        io_uring_handle_destory(handle);
        return NULL;
    }

    return handle;
}

#endif