2023-04-18 16:03:57 +08:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
|
#include <linux/tcp.h>
|
|
|
|
|
#include <linux/netfilter.h> // for NF_ACCEPT
|
|
|
|
|
#include <libnetfilter_queue/libnetfilter_queue.h>
|
|
|
|
|
#include <linux/if_tun.h>
|
2023-04-28 16:18:32 +08:00
|
|
|
#include <MESA/MESA_prof_load.h>
|
2023-04-18 16:03:57 +08:00
|
|
|
|
2023-04-28 16:18:32 +08:00
|
|
|
#include <bpf_obj.h>
|
2023-04-18 16:03:57 +08:00
|
|
|
#include <tfe_utils.h>
|
|
|
|
|
#include <tfe_cmsg.h>
|
|
|
|
|
#include <proxy.h>
|
|
|
|
|
#include "io_uring.h"
|
2023-05-09 22:12:38 +08:00
|
|
|
#include "tfe_packet_io_fs.h"
|
2023-04-18 16:03:57 +08:00
|
|
|
#include "tfe_tcp_restore.h"
|
|
|
|
|
#include "acceptor_kni_v4.h"
|
2023-04-28 16:18:32 +08:00
|
|
|
#include "tap.h"
|
|
|
|
|
#include "tfe_packet_io.h"
|
|
|
|
|
#include "tfe_session_table.h"
|
|
|
|
|
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
void * g_packet_io_logger = NULL;
|
|
|
|
|
|
|
|
|
|
static int tap_read(int tap_fd, char *buff, int buff_size, void *logger)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
int ret = read(tap_fd, buff, buff_size);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
if (errno != EWOULDBLOCK && errno != EAGAIN)
|
|
|
|
|
{
|
2023-05-15 16:41:59 +08:00
|
|
|
TFE_LOG_ERROR(logger, "%s: unable to read data from tapfd %d, aborting: %s", LOG_TAG_PKTIO, tap_fd, strerror(errno));
|
2023-04-28 16:18:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
void acceptor_kni_v4_destroy(struct acceptor_kni_v4 *ctx)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
if (ctx)
|
|
|
|
|
{
|
|
|
|
|
packet_io_destory(ctx->io);
|
2023-05-09 22:12:38 +08:00
|
|
|
packet_io_fs_destory(ctx->packet_io_fs);
|
2023-04-28 16:18:32 +08:00
|
|
|
free(ctx);
|
|
|
|
|
ctx = NULL;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
struct acceptor_kni_v4 *acceptor_ctx_create(const char *profile, void *logger)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
struct acceptor_kni_v4 *ctx = ALLOC(struct acceptor_kni_v4, 1);
|
|
|
|
|
|
|
|
|
|
MESA_load_profile_int_def(profile, "PACKET_IO", "firewall_sids", (int *)&(ctx->firewall_sids), 1000);
|
|
|
|
|
MESA_load_profile_int_def(profile, "PACKET_IO", "proxy_sids", (int *)&(ctx->proxy_sids), 1001);
|
|
|
|
|
MESA_load_profile_int_def(profile, "PACKET_IO", "service_chaining_sids", (int *)&(ctx->sce_sids), 1002);
|
|
|
|
|
MESA_load_profile_int_def(profile, "PACKET_IO", "packet_io_threads", (int *)&(ctx->nr_worker_threads), 8);
|
2023-05-06 18:48:01 +08:00
|
|
|
MESA_load_profile_uint_range(profile, "PACKET_IO", "packet_io_cpu_affinity_mask", TFE_THREAD_MAX, (unsigned int *)ctx->cpu_affinity_mask);
|
2023-04-28 16:18:32 +08:00
|
|
|
ctx->nr_worker_threads = MIN(ctx->nr_worker_threads, TFE_THREAD_MAX);
|
|
|
|
|
|
|
|
|
|
CPU_ZERO(&ctx->coremask);
|
|
|
|
|
for (int i = 0; i < ctx->nr_worker_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
int cpu_id = ctx->cpu_affinity_mask[i];
|
|
|
|
|
CPU_SET(cpu_id, &ctx->coremask);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
ctx->io = packet_io_create(profile, ctx->nr_worker_threads, &ctx->coremask, logger);
|
2023-04-28 16:18:32 +08:00
|
|
|
if (ctx->io == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
ctx->packet_io_fs = packet_io_fs_create(profile);
|
2023-05-09 22:12:38 +08:00
|
|
|
if (ctx->packet_io_fs == NULL)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
|
|
|
|
|
error_out:
|
2023-05-15 16:41:59 +08:00
|
|
|
acceptor_kni_v4_destroy(ctx);
|
2023-04-28 16:18:32 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
static void *worker_thread_cycle(void *arg)
|
|
|
|
|
{
|
2023-05-09 22:12:38 +08:00
|
|
|
struct packet_io_thread_ctx *thread_ctx = (struct packet_io_thread_ctx *)arg;
|
2023-04-18 16:03:57 +08:00
|
|
|
struct packet_io *handle = thread_ctx->ref_io;
|
2023-05-15 16:41:59 +08:00
|
|
|
void * logger = thread_ctx->logger;
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
int pkg_len = 0;
|
2023-05-15 16:41:59 +08:00
|
|
|
char thread_name[32];
|
2023-04-18 16:03:57 +08:00
|
|
|
int n_pkt_recv_from_nf = 0;
|
|
|
|
|
int n_pkt_recv_from_tap = 0;
|
|
|
|
|
int n_pkt_recv_from_tap_c = 0;
|
|
|
|
|
int n_pkt_recv_from_tap_s = 0;
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
snprintf(thread_name, sizeof(thread_name), "packet-io:worker-%d", thread_ctx->thread_index);
|
2023-04-18 16:03:57 +08:00
|
|
|
prctl(PR_SET_NAME, (unsigned long long)thread_name, NULL, NULL, NULL);
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
if (packet_io_thread_init(handle, thread_ctx, logger) != 0)
|
2023-04-18 16:03:57 +08:00
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 16:18:32 +08:00
|
|
|
if (is_enable_iouring(handle)) {
|
2023-04-18 16:03:57 +08:00
|
|
|
io_uring_register_read_callback(thread_ctx->tap_ctx->io_uring_fd, handle_raw_packet_from_tap, thread_ctx);
|
|
|
|
|
io_uring_register_read_callback(thread_ctx->tap_ctx->io_uring_c, handle_decryption_packet_from_tap, thread_ctx);
|
|
|
|
|
io_uring_register_read_callback(thread_ctx->tap_ctx->io_uring_s, handle_decryption_packet_from_tap, thread_ctx);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
thread_ctx->tap_ctx->buff_size = 3000;
|
|
|
|
|
thread_ctx->tap_ctx->buff = ALLOC(char, thread_ctx->tap_ctx->buff_size);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
TFE_LOG_INFO(logger, "%s: worker thread %d is running", "LOG_TAG_KNI", thread_ctx->thread_index);
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
n_pkt_recv_from_nf = packet_io_polling_nf_interface(handle, thread_ctx->thread_index, thread_ctx);
|
2023-04-28 16:18:32 +08:00
|
|
|
if (is_enable_iouring(handle)) {
|
2023-04-18 16:03:57 +08:00
|
|
|
n_pkt_recv_from_tap = io_uring_peek_ready_entrys(thread_ctx->tap_ctx->io_uring_fd);
|
|
|
|
|
n_pkt_recv_from_tap_c = io_uring_peek_ready_entrys(thread_ctx->tap_ctx->io_uring_c);
|
2023-04-28 16:18:32 +08:00
|
|
|
n_pkt_recv_from_tap_s = io_uring_peek_ready_entrys(thread_ctx->tap_ctx->io_uring_s);
|
2023-04-18 16:03:57 +08:00
|
|
|
}
|
|
|
|
|
else {
|
2023-05-15 16:41:59 +08:00
|
|
|
if ((pkg_len = tap_read(thread_ctx->tap_ctx->tap_fd, thread_ctx->tap_ctx->buff, thread_ctx->tap_ctx->buff_size, logger)) > 0)
|
2023-04-18 16:03:57 +08:00
|
|
|
{
|
|
|
|
|
handle_raw_packet_from_tap(thread_ctx->tap_ctx->buff, pkg_len, thread_ctx);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
if ((pkg_len = tap_read(thread_ctx->tap_ctx->tap_c, thread_ctx->tap_ctx->buff, thread_ctx->tap_ctx->buff_size, logger)) > 0)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
handle_decryption_packet_from_tap(thread_ctx->tap_ctx->buff, pkg_len, thread_ctx);
|
|
|
|
|
}
|
2023-04-18 16:03:57 +08:00
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
if ((pkg_len = tap_read(thread_ctx->tap_ctx->tap_s, thread_ctx->tap_ctx->buff, thread_ctx->tap_ctx->buff_size, logger)) > 0)
|
2023-04-28 16:18:32 +08:00
|
|
|
{
|
|
|
|
|
handle_decryption_packet_from_tap(thread_ctx->tap_ctx->buff, pkg_len, thread_ctx);
|
|
|
|
|
}
|
2023-04-18 16:03:57 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 16:18:32 +08:00
|
|
|
if (n_pkt_recv_from_nf == 0 && n_pkt_recv_from_tap == 0 && n_pkt_recv_from_tap_c == 0 && n_pkt_recv_from_tap_s == 0)
|
|
|
|
|
{
|
|
|
|
|
packet_io_thread_wait(handle, thread_ctx, -1);
|
|
|
|
|
}
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
if (__atomic_fetch_add(&thread_ctx->session_table_need_reset, 0, __ATOMIC_RELAXED) > 0)
|
|
|
|
|
{
|
|
|
|
|
session_table_reset(thread_ctx->session_table);
|
|
|
|
|
__atomic_fetch_and(&thread_ctx->session_table_need_reset, 0, __ATOMIC_RELAXED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_out:
|
2023-05-15 16:41:59 +08:00
|
|
|
TFE_LOG_ERROR(logger, "%s: worker thread %d exiting", LOG_TAG_SCE, thread_ctx->thread_index);
|
2023-04-18 16:03:57 +08:00
|
|
|
return (void *)NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
struct acceptor_kni_v4 *acceptor_kni_v4_create(struct tfe_proxy *proxy, const char *profile)
|
2023-04-18 16:03:57 +08:00
|
|
|
{
|
2023-05-15 16:41:59 +08:00
|
|
|
void *packet_io_logger = NULL;
|
|
|
|
|
packet_io_logger = MESA_create_runtime_log_handle("packet_io", RLOG_LV_DEBUG);
|
|
|
|
|
assert(packet_io_logger != NULL);
|
2023-04-28 16:18:32 +08:00
|
|
|
|
2023-05-15 16:41:59 +08:00
|
|
|
g_packet_io_logger = packet_io_logger;
|
|
|
|
|
struct acceptor_kni_v4 *acceptor_ctx = acceptor_ctx_create(profile, packet_io_logger);
|
2023-04-18 16:03:57 +08:00
|
|
|
if (acceptor_ctx == NULL)
|
2023-05-22 15:19:29 +08:00
|
|
|
return NULL;
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
acceptor_ctx->ref_proxy = proxy;
|
|
|
|
|
for (int i = 0; i < acceptor_ctx->nr_worker_threads; i++) {
|
|
|
|
|
acceptor_ctx->work_threads[i].tid = 0;
|
|
|
|
|
acceptor_ctx->work_threads[i].thread_index = i;
|
2023-05-15 16:41:59 +08:00
|
|
|
acceptor_ctx->work_threads[i].ref_io = acceptor_ctx->io;
|
2023-04-18 16:03:57 +08:00
|
|
|
acceptor_ctx->work_threads[i].ref_acceptor_ctx = acceptor_ctx;
|
|
|
|
|
|
|
|
|
|
acceptor_ctx->work_threads[i].tap_ctx = tfe_tap_ctx_create(&acceptor_ctx->work_threads[i]);
|
2023-04-28 16:18:32 +08:00
|
|
|
if (acceptor_ctx->work_threads[i].tap_ctx == NULL)
|
|
|
|
|
goto error_out;
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
acceptor_ctx->work_threads[i].session_table = session_table_create();
|
|
|
|
|
acceptor_ctx->work_threads[i].ref_proxy = proxy;
|
2023-05-09 22:12:38 +08:00
|
|
|
acceptor_ctx->work_threads[i].ret_fs_state = acceptor_ctx->packet_io_fs;
|
2023-05-15 16:41:59 +08:00
|
|
|
acceptor_ctx->work_threads[i].logger = packet_io_logger;
|
2023-04-18 16:03:57 +08:00
|
|
|
acceptor_ctx->work_threads[i].session_table_need_reset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < acceptor_ctx->nr_worker_threads; i++) {
|
2023-05-09 22:12:38 +08:00
|
|
|
struct packet_io_thread_ctx *thread_ctx = &acceptor_ctx->work_threads[i];
|
2023-04-18 16:03:57 +08:00
|
|
|
if (pthread_create(&thread_ctx->tid, NULL, worker_thread_cycle, (void *)thread_ctx) < 0)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 16:18:32 +08:00
|
|
|
return acceptor_ctx;
|
2023-04-18 16:03:57 +08:00
|
|
|
|
|
|
|
|
error_out:
|
2023-04-28 16:18:32 +08:00
|
|
|
for (int i = 0; i < acceptor_ctx->nr_worker_threads; i++) {
|
|
|
|
|
tfe_tap_ctx_destory(acceptor_ctx->work_threads[i].tap_ctx);
|
|
|
|
|
session_table_destory(acceptor_ctx->work_threads[i].session_table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acceptor_kni_v4_destroy(acceptor_ctx);
|
2023-04-18 16:03:57 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|