summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/connection/native/linux/connection_mgr.c
blob: 00144620624c6acd10e33dc90a87ef38776e43a1 (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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

/*
 * Note:
 * This file implements the linux version connection library which is
 * defined in connection_lib.h.
 * It also provides a reference implementation of connections manager.
 */

#include "connection_lib.h"
#include "bh_platform.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "conn_tcp.h"
#include "conn_udp.h"
#include "conn_uart.h"

#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <fcntl.h>

#define MAX_EVENTS 10
#define IO_BUF_SIZE 256

static bool polling_thread_run = true;

/* Connection type */
typedef enum conn_type {
    CONN_TYPE_TCP,
    CONN_TYPE_UDP,
    CONN_TYPE_UART,
    CONN_TYPE_UNKNOWN
} conn_type_t;

/* Sys connection */
typedef struct sys_connection {
    /* Next connection */
    struct sys_connection *next;

    /* Type */
    conn_type_t type;

    /* Handle to interact with wasm app */
    uint32 handle;

    /* Underlying connection ID, may be socket fd */
    int fd;

    /* Module id that the connection belongs to */
    uint32 module_id;

    /* Argument, such as dest addr for udp */
    void *arg;
} sys_connection_t;

/* Epoll instance */
static int epollfd;

/* Connections list */
static sys_connection_t *g_connections = NULL;

/* Max handle */
static uint32 g_handle_max = 0;

/* Lock to protect g_connections and g_handle_max */
static korp_mutex g_lock;

/* Epoll events */
static struct epoll_event epoll_events[MAX_EVENTS];

/* Buffer to receive data */
static char io_buf[IO_BUF_SIZE];

static uint32
_conn_open(wasm_module_inst_t module_inst, const char *name,
           attr_container_t *args);
static void
_conn_close(uint32 handle);
static int
_conn_send(uint32 handle, const char *data, int len);
static bool
_conn_config(uint32 handle, attr_container_t *cfg);

/* clang-format off */
/*
 * Platform implementation of connection library
 */
connection_interface_t connection_impl = {
    ._open = _conn_open,
    ._close = _conn_close,
    ._send = _conn_send,
    ._config = _conn_config
};
/* clang-format on */

static void
add_connection(sys_connection_t *conn)
{
    os_mutex_lock(&g_lock);

    g_handle_max++;
    if (g_handle_max == -1)
        g_handle_max++;
    conn->handle = g_handle_max;

    if (g_connections) {
        conn->next = g_connections;
        g_connections = conn;
    }
    else {
        g_connections = conn;
    }

    os_mutex_unlock(&g_lock);
}

#define FREE_CONNECTION(conn)             \
    do {                                  \
        if (conn->arg)                    \
            wasm_runtime_free(conn->arg); \
        wasm_runtime_free(conn);          \
    } while (0)

static int
get_app_conns_num(uint32 module_id)
{
    sys_connection_t *conn;
    int num = 0;

    os_mutex_lock(&g_lock);

    conn = g_connections;
    while (conn) {
        if (conn->module_id == module_id)
            num++;
        conn = conn->next;
    }

    os_mutex_unlock(&g_lock);

    return num;
}

static sys_connection_t *
find_connection(uint32 handle, bool remove_found)
{
    sys_connection_t *conn, *prev = NULL;

    os_mutex_lock(&g_lock);

    conn = g_connections;
    while (conn) {
        if (conn->handle == handle) {
            if (remove_found) {
                if (prev != NULL) {
                    prev->next = conn->next;
                }
                else {
                    g_connections = conn->next;
                }
            }
            os_mutex_unlock(&g_lock);
            return conn;
        }
        else {
            prev = conn;
            conn = conn->next;
        }
    }

    os_mutex_unlock(&g_lock);

    return NULL;
}

static void
cleanup_connections(uint32 module_id)
{
    sys_connection_t *conn, *prev = NULL;

    os_mutex_lock(&g_lock);

    conn = g_connections;
    while (conn) {
        if (conn->module_id == module_id) {
            epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
            close(conn->fd);

            if (prev != NULL) {
                prev->next = conn->next;
                FREE_CONNECTION(conn);
                conn = prev->next;
            }
            else {
                g_connections = conn->next;
                FREE_CONNECTION(conn);
                conn = g_connections;
            }
        }
        else {
            prev = conn;
            conn = conn->next;
        }
    }

    os_mutex_unlock(&g_lock);
}

static conn_type_t
get_conn_type(const char *name)
{
    if (strcmp(name, "TCP") == 0)
        return CONN_TYPE_TCP;
    if (strcmp(name, "UDP") == 0)
        return CONN_TYPE_UDP;
    if (strcmp(name, "UART") == 0)
        return CONN_TYPE_UART;

    return CONN_TYPE_UNKNOWN;
}

/* --- connection lib function --- */
static uint32
_conn_open(wasm_module_inst_t module_inst, const char *name,
           attr_container_t *args)
{
    int fd;
    sys_connection_t *conn;
    struct epoll_event ev;
    uint32 module_id = app_manager_get_module_id(Module_WASM_App, module_inst);
    bh_assert(module_id != ID_NONE);

    if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
        return -1;

    conn = (sys_connection_t *)wasm_runtime_malloc(sizeof(*conn));
    if (conn == NULL)
        return -1;

    memset(conn, 0, sizeof(*conn));
    conn->module_id = module_id;
    conn->type = get_conn_type(name);

    /* Generate a handle and add to list */
    add_connection(conn);

    if (conn->type == CONN_TYPE_TCP) {
        char *address;
        uint16 port;

        /* Check and parse connection parameters */
        if (!attr_container_contain_key(args, "address")
            || !attr_container_contain_key(args, "port"))
            goto fail;

        address = attr_container_get_as_string(args, "address");
        port = attr_container_get_as_uint16(args, "port");

        /* Connect to TCP server */
        if (!address || (fd = tcp_open(address, port)) == -1)
            goto fail;
    }
    else if (conn->type == CONN_TYPE_UDP) {
        uint16 port;

        /* Check and parse connection parameters */
        if (!attr_container_contain_key(args, "bind port"))
            goto fail;
        port = attr_container_get_as_uint16(args, "bind port");

        /* Bind port */
        if ((fd = udp_open(port)) == -1)
            goto fail;
    }
    else if (conn->type == CONN_TYPE_UART) {
        char *device;
        int baud;

        /* Check and parse connection parameters */
        if (!attr_container_contain_key(args, "device")
            || !attr_container_contain_key(args, "baudrate"))
            goto fail;
        device = attr_container_get_as_string(args, "device");
        baud = attr_container_get_as_int(args, "baudrate");

        /* Open device */
        if (!device || (fd = uart_open(device, baud)) == -1)
            goto fail;
    }
    else {
        goto fail;
    }

    conn->fd = fd;

    /* Set current connection as event data */
    ev.events = EPOLLIN;
    ev.data.ptr = conn;

    /* Monitor incoming data */
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
        close(fd);
        goto fail;
    }

    return conn->handle;

fail:
    find_connection(conn->handle, true);
    wasm_runtime_free(conn);
    return -1;
}

/* --- connection lib function --- */
static void
_conn_close(uint32 handle)
{
    sys_connection_t *conn = find_connection(handle, true);

    if (conn != NULL) {
        epoll_ctl(epollfd, EPOLL_CTL_DEL, conn->fd, NULL);
        close(conn->fd);
        FREE_CONNECTION(conn);
    }
}

/* --- connection lib function --- */
static int
_conn_send(uint32 handle, const char *data, int len)
{
    sys_connection_t *conn = find_connection(handle, false);

    if (conn == NULL)
        return -1;

    if (conn->type == CONN_TYPE_TCP)
        return tcp_send(conn->fd, data, len);

    if (conn->type == CONN_TYPE_UDP) {
        struct sockaddr *addr = (struct sockaddr *)conn->arg;
        return udp_send(conn->fd, addr, data, len);
    }

    if (conn->type == CONN_TYPE_UART)
        return uart_send(conn->fd, data, len);

    return -1;
}

/* --- connection lib function --- */
static bool
_conn_config(uint32 handle, attr_container_t *cfg)
{
    sys_connection_t *conn = find_connection(handle, false);

    if (conn == NULL)
        return false;

    if (conn->type == CONN_TYPE_UDP) {
        char *address;
        uint16_t port;
        struct sockaddr_in *addr;

        /* Parse remote address/port */
        if (!attr_container_contain_key(cfg, "address")
            || !attr_container_contain_key(cfg, "port"))
            return false;
        if (!(address = attr_container_get_as_string(cfg, "address")))
            return false;
        port = attr_container_get_as_uint16(cfg, "port");

        if (conn->arg == NULL) {
            addr = (struct sockaddr_in *)wasm_runtime_malloc(sizeof(*addr));
            if (addr == NULL)
                return false;

            memset(addr, 0, sizeof(*addr));
            addr->sin_family = AF_INET;
            addr->sin_addr.s_addr = inet_addr(address);
            addr->sin_port = htons(port);

            /* Set remote address as connection arg */
            conn->arg = addr;
        }
        else {
            addr = (struct sockaddr_in *)conn->arg;
            addr->sin_addr.s_addr = inet_addr(address);
            addr->sin_port = htons(port);
        }

        return true;
    }

    return false;
}

/* --- connection manager reference implementation ---*/

typedef struct connection_event {
    uint32 handle;
    char *data;
    uint32 len;
} connection_event_t;

static void
connection_event_cleaner(connection_event_t *conn_event)
{
    if (conn_event->data != NULL)
        wasm_runtime_free(conn_event->data);
    wasm_runtime_free(conn_event);
}

static void
post_msg_to_module(sys_connection_t *conn, char *data, uint32 len)
{
    module_data *module = module_data_list_lookup_id(conn->module_id);
    char *data_copy = NULL;
    connection_event_t *conn_data_event;
    bh_message_t msg;

    if (module == NULL)
        return;

    conn_data_event =
        (connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event));
    if (conn_data_event == NULL)
        return;

    if (len > 0) {
        data_copy = (char *)wasm_runtime_malloc(len);
        if (data_copy == NULL) {
            wasm_runtime_free(conn_data_event);
            return;
        }
        bh_memcpy_s(data_copy, len, data, len);
    }

    memset(conn_data_event, 0, sizeof(*conn_data_event));
    conn_data_event->handle = conn->handle;
    conn_data_event->data = data_copy;
    conn_data_event->len = len;

    msg = bh_new_msg(CONNECTION_EVENT_WASM, conn_data_event,
                     sizeof(*conn_data_event), connection_event_cleaner);
    if (!msg) {
        connection_event_cleaner(conn_data_event);
        return;
    }

    bh_post_msg2(module->queue, msg);
}

static void *
polling_thread_routine(void *arg)
{
    while (polling_thread_run) {
        int i, n;

        n = epoll_wait(epollfd, epoll_events, MAX_EVENTS, -1);

        if (n == -1 && errno != EINTR)
            continue;

        for (i = 0; i < n; i++) {
            sys_connection_t *conn =
                (sys_connection_t *)epoll_events[i].data.ptr;

            if (conn->type == CONN_TYPE_TCP) {
                int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE);
                if (count <= 0) {
                    /* Connection is closed by peer */
                    post_msg_to_module(conn, NULL, 0);
                    _conn_close(conn->handle);
                }
                else {
                    /* Data is received */
                    post_msg_to_module(conn, io_buf, count);
                }
            }
            else if (conn->type == CONN_TYPE_UDP) {
                int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE);
                if (count > 0)
                    post_msg_to_module(conn, io_buf, count);
            }
            else if (conn->type == CONN_TYPE_UART) {
                int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE);
                if (count > 0)
                    post_msg_to_module(conn, io_buf, count);
            }
        }
    }

    return NULL;
}

void
app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
{
    uint32 argv[3];
    wasm_function_inst_t func_on_conn_data;
    bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg));
    wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
    wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
    connection_event_t *conn_event =
        (connection_event_t *)bh_message_payload(msg);
    int32 data_offset;

    if (conn_event == NULL)
        return;

    func_on_conn_data = wasm_runtime_lookup_function(
        inst, "_on_connection_data", "(i32i32i32)");
    if (!func_on_conn_data)
        func_on_conn_data = wasm_runtime_lookup_function(
            inst, "on_connection_data", "(i32i32i32)");
    if (!func_on_conn_data) {
        printf("Cannot find function on_connection_data\n");
        return;
    }

    /* 0 len means connection closed */
    if (conn_event->len == 0) {
        argv[0] = conn_event->handle;
        argv[1] = 0;
        argv[2] = 0;
        if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data,
                                    3, argv)) {
            const char *exception = wasm_runtime_get_exception(inst);
            bh_assert(exception);
            printf(":Got exception running wasm code: %s\n", exception);
            wasm_runtime_clear_exception(inst);
            return;
        }
    }
    else {
        data_offset = wasm_runtime_module_dup_data(inst, conn_event->data,
                                                   conn_event->len);
        if (data_offset == 0) {
            const char *exception = wasm_runtime_get_exception(inst);
            if (exception) {
                printf("Got exception running wasm code: %s\n", exception);
                wasm_runtime_clear_exception(inst);
            }
            return;
        }

        argv[0] = conn_event->handle;
        argv[1] = (uint32)data_offset;
        argv[2] = conn_event->len;
        if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data,
                                    3, argv)) {
            const char *exception = wasm_runtime_get_exception(inst);
            bh_assert(exception);
            printf(":Got exception running wasm code: %s\n", exception);
            wasm_runtime_clear_exception(inst);
            wasm_runtime_module_free(inst, data_offset);
            return;
        }
        wasm_runtime_module_free(inst, data_offset);
    }
}

bool
init_connection_framework()
{
    korp_tid tid;

    epollfd = epoll_create(MAX_EVENTS);
    if (epollfd == -1)
        return false;

    if (os_mutex_init(&g_lock) != 0) {
        close(epollfd);
        return false;
    }

    if (!wasm_register_cleanup_callback(cleanup_connections)) {
        goto fail;
    }

    if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM,
                                    app_mgr_connection_event_callback)) {
        goto fail;
    }

    if (os_thread_create(&tid, polling_thread_routine, NULL,
                         BH_APPLET_PRESERVED_STACK_SIZE)
        != 0) {
        goto fail;
    }

    return true;

fail:
    os_mutex_destroy(&g_lock);
    close(epollfd);
    return false;
}

void
exit_connection_framework()
{
    polling_thread_run = false;
}