summaryrefslogtreecommitdiff
path: root/entry/src/kni_tap_rss.cpp
blob: 180164d91563ca92bdc05a5dc5a612c6c41cf6bb (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
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

#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>

#include <linux/if_tun.h>
#include <signal.h>
#include <assert.h>

#if (SUPPORT_BPF)
#include "../../bpf/bpf_conf_user.h"
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#endif

#include "kni_tap_rss.h"
#include "kni_utils.h"

#ifndef TUN_PATH
#define TUN_PATH "/dev/net/tun"
#endif

struct bpf_ctx
{
    int bpf_prog_fd;
    int bpf_map_fd;

    char bpf_file[1024];
#if (SUPPORT_BPF)
    struct bpf_object *bpf_obj;
    bpf_conf_t bpf_conf;
#endif
};

int kni_tap_get_bpf_prog_fd(struct bpf_ctx *ctx)
{
    if (ctx)
    {
        return ctx->bpf_prog_fd;
    }
    else
    {
        return -1;
    }
}

#if (SUPPORT_BPF)
void kni_tap_global_unload_rss_bpf(struct bpf_ctx *ctx)
{
    if (ctx)
    {
        if (ctx->bpf_prog_fd > 0)
        {
            close(ctx->bpf_prog_fd);
        }

        if (ctx->bpf_obj)
        {
            bpf_object__close(ctx->bpf_obj);
            ctx->bpf_obj = NULL;
        }

        free(ctx);
        ctx = NULL;
    }
}
#else
void kni_tap_global_unload_rss_bpf(struct bpf_ctx *ctx)
{
}
#endif

/*
 * bpf_queue_num        : worker thread number
 * bpf_default_queue    : -1: for disable(only use for debug, rss to one queue)
 * bpf_hash_mode        :  2: hash with src/dst addr
 *                         4: hash with src/dst addr and src/dst port
 * bpf_debug_log        :  0 for disable(only use for debug, printf bpf debug log)
 */
#if (SUPPORT_BPF)
struct bpf_ctx *kni_tap_global_load_rss_bpf(const char *bpf_obj_file, uint32_t bpf_queue_num, uint32_t bpf_hash_mode, uint32_t bpf_debug_log, void *logger)
{
    struct bpf_ctx *ctx = (struct bpf_ctx *)calloc(1, sizeof(struct bpf_ctx));
    strncpy(ctx->bpf_file, bpf_obj_file, strlen(bpf_obj_file));

    bpf_conf_set_debug_log(&ctx->bpf_conf, bpf_debug_log);
    bpf_conf_set_hash_mode(&ctx->bpf_conf, bpf_hash_mode);
    bpf_conf_set_queue_num(&ctx->bpf_conf, bpf_queue_num);

    if (bpf_prog_load(ctx->bpf_file, BPF_PROG_TYPE_SOCKET_FILTER, &ctx->bpf_obj, &ctx->bpf_prog_fd) < 0)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to load bpf object %s, aborting: %s", ctx->bpf_file, strerror(errno));
        goto error;
    }

    if (bpf_conf_update_map(&ctx->bpf_conf, ctx->bpf_obj) == -1)
    {
        goto error;
    }

    return ctx;

error:
    kni_tap_global_unload_rss_bpf(ctx);

    return NULL;
}
#else
struct bpf_ctx *kni_tap_global_load_rss_bpf(const char *bpf_obj_file, uint32_t bpf_queue_num, uint32_t bpf_hash_mode, uint32_t bpf_debug_log, void *logger)
{
    KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "not support bpf");
    return NULL;
}
#endif

int kni_tap_open_per_thread(const char *tap_dev, int tap_flags, int bpf_prog_fd, void *logger)
{
    int fd = -1;
    int tap_fd = -1;
    int nonblock_flags = -1;
    struct ifreq ifr;

    tap_fd = open(TUN_PATH, O_RDWR);
    if (tap_fd == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to open " TUN_PATH ", aborting: %s", strerror(errno));
        return -1;
    }

    memset(&ifr, 0, sizeof(ifr));
    ifr.ifr_flags = tap_flags;
    strcpy(ifr.ifr_name, tap_dev);
    if (ioctl(tap_fd, TUNSETIFF, &ifr) == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to attach %s, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }

    /*
     * The TUNSETPERSIST ioctl can be used to make the TUN/TAP interface persistent.
     * In this mode, the interface won't be destroyed when the last process closes the associated /dev/net/tun file descriptor.
     */
    /*
    if (ioctl(tap_fd, TUNSETPERSIST, 1) == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to set persist on %s, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }
    */
#if (SUPPORT_BPF)
    if (bpf_prog_fd > 0)
    {
        // Set bpf
        if (ioctl(tap_fd, TUNSETSTEERINGEBPF, (void *)&bpf_prog_fd) == -1)
        {
            KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to set bpf on %s, aborting: %s", tap_dev, strerror(errno));
            goto error;
        }
    }
#endif

    // Set nonblock
    nonblock_flags = fcntl(tap_fd, F_GETFL);
    if (nonblock_flags == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to get nonblock flags on %s fd, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }
    nonblock_flags |= O_NONBLOCK;
    if (fcntl(tap_fd, F_SETFL, nonblock_flags) == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to set nonblock flags on %s fd, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }

    // Get MTU
    fd = socket(PF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to create socket, aborting: %s", strerror(errno));
        goto error;
    }

    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, tap_dev);
    if (ioctl(fd, SIOCGIFMTU, &ifr) < 0)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to get MTU on %s, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }

    // Set eth up
    if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to get link status on %s, aborting: %s", tap_dev, strerror(errno));
        goto error;
    }

    if ((ifr.ifr_flags & IFF_UP) == 0)
    {
        ifr.ifr_flags |= IFF_UP;
        if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
        {
            KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to set link status on %s, aborting: %s", tap_dev, strerror(errno));
            goto error;
        }
    }

    KNI_LOG_INFO(logger, TAP_RSS_LOG_TAG "using tap device %s with MTU %d", tap_dev, ifr.ifr_mtu);
    close(fd);

    return tap_fd;

error:

    if (fd > 0)
    {
        close(fd);
        fd = -1;
    }

    if (tap_fd > 0)
    {
        close(tap_fd);
        tap_fd = -1;
    }

    return -1;
}

void kni_tap_close_per_thread(int tap_fd)
{
    if (tap_fd > 0)
    {
        close(tap_fd);
    }
}

int kni_tap_read_per_thread(int tap_fd, char *buff, int buff_size, void *logger)
{
    int ret = read(tap_fd, buff, buff_size);
    if (ret < 0)
    {
        if (errno != EWOULDBLOCK && errno != EAGAIN)
        {
            KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "unable to read data from tapfd %d, aborting: %s", tap_fd, strerror(errno));
        }
    }

    return ret;
}

int kni_tap_write_per_thread(int tap_fd, const char *data, int data_len, void *logger)
{
    int ret = write(tap_fd, data, data_len);
    if (ret != data_len)
    {
        KNI_LOG_ERROR(logger, TAP_RSS_LOG_TAG "need send %dB, only send %dB, aborting: %s", data_len, ret, strerror(errno));
    }

    return ret;
}