From f7ea81dad82720fa7a4bdbb9da72ab2373342b16 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Sun, 24 Apr 2022 14:24:09 +0800 Subject: [PATCH] =?UTF-8?q?TSG-10254=20eBPF=E6=94=AF=E6=8C=81=E4=BA=8C?= =?UTF-8?q?=E5=85=83=E7=BB=84=E5=88=86=E6=B5=81=20TSG-10527=20eBPF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9B=9B=E5=85=83=E7=BB=84=E5=88=86=E6=B5=81?= =?UTF-8?q?=20TSG-10369=20KNI=E5=A4=9A=E7=BA=BF=E7=A8=8B=E5=9C=A8TAP?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=94=AF=E6=8C=81eBPF=E5=88=86?= =?UTF-8?q?=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitlab-ci.yml | 41 ++- CMakeLists.txt | 17 +- bpf/CMakeLists.txt | 1 + bpf/bpf_conf_def.h | 53 ++++ bpf/bpf_conf_kernel.h | 58 +++++ bpf/bpf_conf_user.h | 66 +++++ bpf/bpf_helpers.h | 492 ++++++++++++++++++++++++++++++++++++ bpf/bpf_tun_rss_steering.c | 423 +++++++++++++++++++++++++++++++ bpf/libendian.h | 25 ++ bpf/makefile | 5 + bpf/rte_hash_crc.h | 357 ++++++++++++++++++++++++++ cmake/FindLIBBPF.cmake | 8 + common/include/kni_utils.h | 8 +- conf/kni/kni.conf | 16 +- entry/CMakeLists.txt | 10 +- entry/include/kni_entry.h | 5 +- entry/include/kni_tap_rss.h | 28 ++ entry/include/kni_tun.h | 10 - entry/src/kni_entry.cpp | 184 +++++++++----- entry/src/kni_tap_rss.cpp | 267 +++++++++++++++++++ entry/src/kni_tun.cpp | 91 ------- 21 files changed, 1982 insertions(+), 183 deletions(-) create mode 100644 bpf/CMakeLists.txt create mode 100644 bpf/bpf_conf_def.h create mode 100644 bpf/bpf_conf_kernel.h create mode 100644 bpf/bpf_conf_user.h create mode 100644 bpf/bpf_helpers.h create mode 100644 bpf/bpf_tun_rss_steering.c create mode 100644 bpf/libendian.h create mode 100644 bpf/makefile create mode 100644 bpf/rte_hash_crc.h create mode 100644 cmake/FindLIBBPF.cmake create mode 100644 entry/include/kni_tap_rss.h delete mode 100644 entry/include/kni_tun.h create mode 100644 entry/src/kni_tap_rss.cpp delete mode 100644 entry/src/kni_tun.cpp diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 74e8a0b..a336282 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,7 +10,7 @@ variables: stages: - build -.build_by_travis: +.build_by_travis_for_centos7: before_script: - mkdir -p $BUILD_PADDING_PREFIX/$CI_PROJECT_NAMESPACE/ - ln -s $CI_PROJECT_DIR $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH @@ -22,10 +22,25 @@ stages: tags: - share +.build_by_travis_for_rocklinux: + before_script: + - mkdir -p $BUILD_PADDING_PREFIX/$CI_PROJECT_NAMESPACE/ + - ln -s $CI_PROJECT_DIR $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - cd $BUILD_PADDING_PREFIX/$CI_PROJECT_PATH + - dnf --enablerepo=powertools install -y clang + - dnf --enablerepo=powertools install -y llvm + - dnf --enablerepo=powertools install -y libbpf-devel + - chmod +x ./ci/travis.sh + script: + - yum makecache + - ./ci/travis.sh + tags: + - share + branch_build_debug_centos7: image: $BUILD_BASED_IMAGE_CENTOS7 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: Debug PACKAGE: 1 @@ -42,7 +57,7 @@ branch_build_debug_centos7: branch_build_debug_rockylinux8: image: $BUILD_BASED_IMAGE_ROCKYLINUX8 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux variables: BUILD_TYPE: Debug PACKAGE: 1 @@ -63,7 +78,7 @@ branch_build_release_centos7: BUILD_TYPE: RelWithDebInfo PACKAGE: 1 TESTING_VERSION_BUILD: 1 - extends: .build_by_travis + extends: .build_by_travis_for_centos7 artifacts: name: "kni-branch-$CI_COMMIT_REF_NAME-release" paths: @@ -80,7 +95,7 @@ branch_build_release_rockylinux8: BUILD_TYPE: RelWithDebInfo PACKAGE: 1 TESTING_VERSION_BUILD: 1 - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux artifacts: name: "kni-branch-$CI_COMMIT_REF_NAME-release" paths: @@ -93,7 +108,7 @@ branch_build_release_rockylinux8: develop_build_debug_centos7: image: $BUILD_BASED_IMAGE_CENTOS7 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: Debug PACKAGE: 1 @@ -113,7 +128,7 @@ develop_build_debug_centos7: develop_build_debug_rockylinux8: image: $BUILD_BASED_IMAGE_ROCKYLINUX8 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux variables: BUILD_TYPE: Debug PACKAGE: 1 @@ -133,7 +148,7 @@ develop_build_debug_rockylinux8: develop_build_release_centos7: image: $BUILD_BASED_IMAGE_CENTOS7 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_centos7 variables: BUILD_TYPE: RelWithDebInfo PACKAGE: 1 @@ -153,7 +168,7 @@ develop_build_release_centos7: develop_build_release_rockylinux8: image: $BUILD_BASED_IMAGE_ROCKYLINUX8 stage: build - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux variables: BUILD_TYPE: RelWithDebInfo PACKAGE: 1 @@ -179,7 +194,7 @@ release_build_debug_centos7: UPLOAD: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el7 PULP3_DIST_NAME: tsg-stable-x86_64.el7 - extends: .build_by_travis + extends: .build_by_travis_for_centos7 artifacts: name: "kni-install-$CI_COMMIT_REF_NAME-debug" paths: @@ -196,7 +211,7 @@ release_build_debug_rockylinux8: UPLOAD: 1 PULP3_REPO_NAME: tsg-stable-x86_64.el8 PULP3_DIST_NAME: tsg-stable-x86_64.el8 - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux artifacts: name: "kni-install-$CI_COMMIT_REF_NAME-debug" paths: @@ -215,7 +230,7 @@ release_build_release_centos7: SYMBOL_TARGET: kni PULP3_REPO_NAME: tsg-stable-x86_64.el7 PULP3_DIST_NAME: tsg-stable-x86_64.el7 - extends: .build_by_travis + extends: .build_by_travis_for_centos7 artifacts: name: "kni-install-$CI_COMMIT_REF_NAME-release" paths: @@ -234,7 +249,7 @@ release_build_release_rockylinux8: SYMBOL_TARGET: kni PULP3_REPO_NAME: tsg-stable-x86_64.el8 PULP3_DIST_NAME: tsg-stable-x86_64.el8 - extends: .build_by_travis + extends: .build_by_travis_for_rocklinux artifacts: name: "kni-install-$CI_COMMIT_REF_NAME-release" paths: diff --git a/CMakeLists.txt b/CMakeLists.txt index e00e311..82277fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,8 +28,13 @@ elseif(ASAN_OPTION MATCHES "THREAD") endif() # end of for ASAN - - +find_package(LIBBPF) +if (SUPPORT_BPF) + add_definitions(-DSUPPORT_BPF=1) + message(STATUS "Support BPF") +else() + message(STATUS "Not support BPF") +endif() add_definitions(-D_GNU_SOURCE) @@ -39,16 +44,20 @@ endif() set(CMAKE_INSTALL_PREFIX /home/mesasoft/sapp_run) - add_subdirectory(vendor) add_subdirectory(common) add_subdirectory(entry) +if (SUPPORT_BPF) + add_subdirectory(bpf) +endif() set(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE ${PROJECT_SOURCE_DIR}/cmake/PreInstall.in) set(CPACK_RPM_LIBRARIES_USER_FILELIST "%config(noreplace) ${CMAKE_INSTALL_PREFIX}/etc/kni/kni.conf") install(FILES conf/kni/kni.inf DESTINATION ${CMAKE_INSTALL_PREFIX}/plug/business/kni COMPONENT PROFILE) install(FILES conf/kni/kni.conf DESTINATION ${CMAKE_INSTALL_PREFIX}/etc/kni COMPONENT PROFILE) - +if (SUPPORT_BPF) + install(FILES bpf/bpf_tun_rss_steering.o DESTINATION ${CMAKE_INSTALL_PREFIX}/plug/business/kni/ COMPONENT PROFILE) +endif() include(Package) \ No newline at end of file diff --git a/bpf/CMakeLists.txt b/bpf/CMakeLists.txt new file mode 100644 index 0000000..da808dd --- /dev/null +++ b/bpf/CMakeLists.txt @@ -0,0 +1 @@ +execute_process(COMMAND make WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/bpf/) diff --git a/bpf/bpf_conf_def.h b/bpf/bpf_conf_def.h new file mode 100644 index 0000000..b52171e --- /dev/null +++ b/bpf/bpf_conf_def.h @@ -0,0 +1,53 @@ +#ifndef _BPF_CONF_DEF_H_ +#define _BPF_CONF_DEF_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define BPF_CONF_KEY_DEBUG_LOG "bpf_debug_log" +#define BPF_CONF_KEY_HASH_MODE "bpf_hash_mode" +#define BPF_CONF_KEY_QUEUE_NUM "bpf_queue_num" + +#define __uint(name, val) int(*name)[val] +#define __type(name, val) val *name +#define SEC(NAME) __attribute__((section(NAME), used)) + +typedef enum bpf_hash_mode_s +{ + BPF_HASH_MODE_TUPLE2 = 0x2, + BPF_HASH_MODE_TUPLE4 = 0x4, +} bpf_hash_mode_t; + +typedef struct bpf_conf_key_s +{ + char str[32]; +} bpf_conf_key_t; + +typedef struct bpf_conf_kv_s +{ + bpf_conf_key_t key; + __u32 val; +} bpf_conf_kv_t; + +typedef struct bpf_conf_s +{ + bpf_conf_kv_t debug_log; + bpf_conf_kv_t hash_mode; + bpf_conf_kv_t queue_num; +} bpf_conf_t; + +struct +{ + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, bpf_conf_key_t); + __type(value, __u32); + __uint(max_entries, 16); +} bpf_conf_map SEC(".maps"); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bpf/bpf_conf_kernel.h b/bpf/bpf_conf_kernel.h new file mode 100644 index 0000000..db34294 --- /dev/null +++ b/bpf/bpf_conf_kernel.h @@ -0,0 +1,58 @@ +#ifndef _BPF_CONF_KERNEL_H_ +#define _BPF_CONF_KERNEL_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "bpf_helpers.h" +#include "bpf_conf_def.h" + +static inline void bpf_conf_lookup_map(bpf_conf_t *conf) +{ + __u32 *val; + + __builtin_memcpy(conf->debug_log.key.str, BPF_CONF_KEY_DEBUG_LOG, 13); + val = (__u32 *)bpf_map_lookup_elem(&bpf_conf_map, &conf->debug_log.key); + if (val) + { + conf->debug_log.val = *val; + } + + __builtin_memcpy(conf->hash_mode.key.str, BPF_CONF_KEY_HASH_MODE, 13); + val = (__u32 *)bpf_map_lookup_elem(&bpf_conf_map, &conf->hash_mode.key); + if (val) + { + conf->hash_mode.val = *val; + } + + __builtin_memcpy(conf->queue_num.key.str, BPF_CONF_KEY_QUEUE_NUM, 13); + val = (__u32 *)bpf_map_lookup_elem(&bpf_conf_map, &conf->queue_num.key); + if (val) + + { + conf->queue_num.val = *val; + } +} + +static inline __u32 bpf_conf_get_debug_log(bpf_conf_t *conf) +{ + return conf->debug_log.val; +} + +static inline __u32 bpf_conf_get_hash_mode(bpf_conf_t *conf) +{ + return conf->hash_mode.val; +} + +static inline __u32 bpf_conf_get_queue_num(bpf_conf_t *conf) +{ + return conf->queue_num.val; +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bpf/bpf_conf_user.h b/bpf/bpf_conf_user.h new file mode 100644 index 0000000..aa696b7 --- /dev/null +++ b/bpf/bpf_conf_user.h @@ -0,0 +1,66 @@ +#ifndef _BPF_CONF_USER_H_ +#define _BPF_CONF_USER_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include +#include + +#include "bpf_conf_def.h" + +static inline int bpf_conf_update_map(bpf_conf_t *conf, struct bpf_object *bpf_obj) +{ + int bpf_conf_map_fd = bpf_object__find_map_fd_by_name(bpf_obj, "bpf_conf_map"); + if (bpf_conf_map_fd < 0) + { + printf("ERROR: Could not find bpf_conf_map in bpf obj file\n"); + return -1; + } + + if (bpf_map_update_elem(bpf_conf_map_fd, &conf->debug_log.key, &conf->debug_log.val, 0)) + { + printf("ERROR: Could not update bpf_conf_map element[%s:%d].\n", conf->debug_log.key.str, conf->debug_log.val); + return -1; + } + + if (bpf_map_update_elem(bpf_conf_map_fd, &conf->hash_mode.key, &conf->hash_mode.val, 0)) + { + printf("ERROR: Could not update bpf_conf_map element[%s:%d].\n", conf->hash_mode.key.str, conf->hash_mode.val); + return -1; + } + + if (bpf_map_update_elem(bpf_conf_map_fd, &conf->queue_num.key, &conf->queue_num.val, 0)) + { + printf("ERROR: Could not update bpf_conf_map element[%s:%d].\n", conf->queue_num.key.str, conf->queue_num.val); + return -1; + } + + return 0; +} + +static inline void bpf_conf_set_debug_log(bpf_conf_t *conf, __u32 val) +{ + conf->debug_log.val = val; + memcpy(conf->debug_log.key.str, BPF_CONF_KEY_DEBUG_LOG, strlen(BPF_CONF_KEY_DEBUG_LOG)); +} + +static inline void bpf_conf_set_hash_mode(bpf_conf_t *conf, __u32 val) +{ + conf->hash_mode.val = val; + memcpy(conf->hash_mode.key.str, BPF_CONF_KEY_HASH_MODE, strlen(BPF_CONF_KEY_HASH_MODE)); +} + +static inline void bpf_conf_set_queue_num(bpf_conf_t *conf, __u32 val) +{ + conf->queue_num.val = val; + memcpy(conf->queue_num.key.str, BPF_CONF_KEY_QUEUE_NUM, strlen(BPF_CONF_KEY_QUEUE_NUM)); +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/bpf/bpf_helpers.h b/bpf/bpf_helpers.h new file mode 100644 index 0000000..92a9391 --- /dev/null +++ b/bpf/bpf_helpers.h @@ -0,0 +1,492 @@ +/* From https://github.com/torvalds/linux/blob/v5.4/tools/testing/selftests/bpf/bpf_helpers.h */ + +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __BPF_HELPERS__ +#define __BPF_HELPERS__ + +#define __uint(name, val) int(*name)[val] +#define __type(name, val) val *name + +/* helper macro to print out debug messages */ +#define bpf_printk(fmt, ...) \ + ({ \ + char ____fmt[] = fmt; \ + bpf_trace_printk(____fmt, sizeof(____fmt), \ + ##__VA_ARGS__); \ + }) + +#ifdef __clang__ + +/* helper macro to place programs, maps, license in + * different sections in elf_bpf file. Section names + * are interpreted by elf_bpf loader + */ +#define SEC(NAME) __attribute__((section(NAME), used)) + +/* helper functions called from eBPF programs written in C */ +static void *(*bpf_map_lookup_elem)(void *map, const void *key) = + (void *)BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map, const void *key, const void *value, unsigned long long flags) = + (void *)BPF_FUNC_map_update_elem; +static int (*bpf_map_delete_elem)(void *map, const void *key) = + (void *)BPF_FUNC_map_delete_elem; +static int (*bpf_map_push_elem)(void *map, const void *value, unsigned long long flags) = + (void *)BPF_FUNC_map_push_elem; +static int (*bpf_map_pop_elem)(void *map, void *value) = + (void *)BPF_FUNC_map_pop_elem; +static int (*bpf_map_peek_elem)(void *map, void *value) = + (void *)BPF_FUNC_map_peek_elem; +static int (*bpf_probe_read)(void *dst, int size, const void *unsafe_ptr) = + (void *)BPF_FUNC_probe_read; +static unsigned long long (*bpf_ktime_get_ns)(void) = + (void *)BPF_FUNC_ktime_get_ns; +static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = + (void *)BPF_FUNC_trace_printk; +static void (*bpf_tail_call)(void *ctx, void *map, int index) = + (void *)BPF_FUNC_tail_call; +static unsigned long long (*bpf_get_smp_processor_id)(void) = + (void *)BPF_FUNC_get_smp_processor_id; +static unsigned long long (*bpf_get_current_pid_tgid)(void) = + (void *)BPF_FUNC_get_current_pid_tgid; +static unsigned long long (*bpf_get_current_uid_gid)(void) = + (void *)BPF_FUNC_get_current_uid_gid; +static int (*bpf_get_current_comm)(void *buf, int buf_size) = + (void *)BPF_FUNC_get_current_comm; +static unsigned long long (*bpf_perf_event_read)(void *map, unsigned long long flags) = + (void *)BPF_FUNC_perf_event_read; +static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = + (void *)BPF_FUNC_clone_redirect; +static int (*bpf_redirect)(int ifindex, int flags) = + (void *)BPF_FUNC_redirect; +static int (*bpf_redirect_map)(void *map, int key, int flags) = + (void *)BPF_FUNC_redirect_map; +static int (*bpf_perf_event_output)(void *ctx, void *map, unsigned long long flags, void *data, int size) = + (void *)BPF_FUNC_perf_event_output; +static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = + (void *)BPF_FUNC_get_stackid; +static int (*bpf_probe_write_user)(void *dst, const void *src, int size) = + (void *)BPF_FUNC_probe_write_user; +static int (*bpf_current_task_under_cgroup)(void *map, int index) = + (void *)BPF_FUNC_current_task_under_cgroup; +static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *)BPF_FUNC_skb_get_tunnel_key; +static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *)BPF_FUNC_skb_set_tunnel_key; +static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = + (void *)BPF_FUNC_skb_get_tunnel_opt; +static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = + (void *)BPF_FUNC_skb_set_tunnel_opt; +static unsigned long long (*bpf_get_prandom_u32)(void) = + (void *)BPF_FUNC_get_prandom_u32; +static int (*bpf_xdp_adjust_head)(void *ctx, int offset) = + (void *)BPF_FUNC_xdp_adjust_head; +static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) = + (void *)BPF_FUNC_xdp_adjust_meta; +static int (*bpf_get_socket_cookie)(void *ctx) = + (void *)BPF_FUNC_get_socket_cookie; +static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval, int optlen) = + (void *)BPF_FUNC_setsockopt; +static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval, int optlen) = + (void *)BPF_FUNC_getsockopt; +static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) = + (void *)BPF_FUNC_sock_ops_cb_flags_set; +static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) = + (void *)BPF_FUNC_sk_redirect_map; +static int (*bpf_sk_redirect_hash)(void *ctx, void *map, void *key, int flags) = + (void *)BPF_FUNC_sk_redirect_hash; +static int (*bpf_sock_map_update)(void *map, void *key, void *value, unsigned long long flags) = + (void *)BPF_FUNC_sock_map_update; +static int (*bpf_sock_hash_update)(void *map, void *key, void *value, unsigned long long flags) = + (void *)BPF_FUNC_sock_hash_update; +static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags, void *buf, unsigned int buf_size) = + (void *)BPF_FUNC_perf_event_read_value; +static int (*bpf_perf_prog_read_value)(void *ctx, void *buf, unsigned int buf_size) = + (void *)BPF_FUNC_perf_prog_read_value; +static int (*bpf_override_return)(void *ctx, unsigned long rc) = + (void *)BPF_FUNC_override_return; +static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) = + (void *)BPF_FUNC_msg_redirect_map; +static int (*bpf_msg_redirect_hash)(void *ctx, void *map, void *key, int flags) = + (void *)BPF_FUNC_msg_redirect_hash; +static int (*bpf_msg_apply_bytes)(void *ctx, int len) = + (void *)BPF_FUNC_msg_apply_bytes; +static int (*bpf_msg_cork_bytes)(void *ctx, int len) = + (void *)BPF_FUNC_msg_cork_bytes; +static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) = + (void *)BPF_FUNC_msg_pull_data; +static int (*bpf_msg_push_data)(void *ctx, int start, int end, int flags) = + (void *)BPF_FUNC_msg_push_data; +static int (*bpf_msg_pop_data)(void *ctx, int start, int cut, int flags) = + (void *)BPF_FUNC_msg_pop_data; +static int (*bpf_bind)(void *ctx, void *addr, int addr_len) = + (void *)BPF_FUNC_bind; +static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) = + (void *)BPF_FUNC_xdp_adjust_tail; +static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state, int size, int flags) = + (void *)BPF_FUNC_skb_get_xfrm_state; +static int (*bpf_sk_select_reuseport)(void *ctx, void *map, void *key, __u32 flags) = + (void *)BPF_FUNC_sk_select_reuseport; +static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) = + (void *)BPF_FUNC_get_stack; +static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params, int plen, __u32 flags) = + (void *)BPF_FUNC_fib_lookup; +static int (*bpf_lwt_push_encap)(void *ctx, unsigned int type, void *hdr, unsigned int len) = + (void *)BPF_FUNC_lwt_push_encap; +static int (*bpf_lwt_seg6_store_bytes)(void *ctx, unsigned int offset, void *from, unsigned int len) = + (void *)BPF_FUNC_lwt_seg6_store_bytes; +static int (*bpf_lwt_seg6_action)(void *ctx, unsigned int action, void *param, unsigned int param_len) = + (void *)BPF_FUNC_lwt_seg6_action; +static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, unsigned int offset, unsigned int len) = + (void *)BPF_FUNC_lwt_seg6_adjust_srh; +static int (*bpf_rc_repeat)(void *ctx) = + (void *)BPF_FUNC_rc_repeat; +static int (*bpf_rc_keydown)(void *ctx, unsigned int protocol, unsigned long long scancode, unsigned int toggle) = + (void *)BPF_FUNC_rc_keydown; +static unsigned long long (*bpf_get_current_cgroup_id)(void) = + (void *)BPF_FUNC_get_current_cgroup_id; +static void *(*bpf_get_local_storage)(void *map, unsigned long long flags) = + (void *)BPF_FUNC_get_local_storage; +static unsigned long long (*bpf_skb_cgroup_id)(void *ctx) = + (void *)BPF_FUNC_skb_cgroup_id; +static unsigned long long (*bpf_skb_ancestor_cgroup_id)(void *ctx, int level) = + (void *)BPF_FUNC_skb_ancestor_cgroup_id; +static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx, struct bpf_sock_tuple *tuple, int size, unsigned long long netns_id, unsigned long long flags) = + (void *)BPF_FUNC_sk_lookup_tcp; +static struct bpf_sock *(*bpf_skc_lookup_tcp)(void *ctx, struct bpf_sock_tuple *tuple, int size, unsigned long long netns_id, unsigned long long flags) = + (void *)BPF_FUNC_skc_lookup_tcp; +static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx, struct bpf_sock_tuple *tuple, int size, unsigned long long netns_id, unsigned long long flags) = + (void *)BPF_FUNC_sk_lookup_udp; +static int (*bpf_sk_release)(struct bpf_sock *sk) = + (void *)BPF_FUNC_sk_release; +static int (*bpf_skb_vlan_push)(void *ctx, __be16 vlan_proto, __u16 vlan_tci) = + (void *)BPF_FUNC_skb_vlan_push; +static int (*bpf_skb_vlan_pop)(void *ctx) = + (void *)BPF_FUNC_skb_vlan_pop; +static int (*bpf_rc_pointer_rel)(void *ctx, int rel_x, int rel_y) = + (void *)BPF_FUNC_rc_pointer_rel; +static void (*bpf_spin_lock)(struct bpf_spin_lock *lock) = + (void *)BPF_FUNC_spin_lock; +static void (*bpf_spin_unlock)(struct bpf_spin_lock *lock) = + (void *)BPF_FUNC_spin_unlock; +static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) = + (void *)BPF_FUNC_sk_fullsock; +static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) = + (void *)BPF_FUNC_tcp_sock; +static struct bpf_sock *(*bpf_get_listener_sock)(struct bpf_sock *sk) = + (void *)BPF_FUNC_get_listener_sock; +static int (*bpf_skb_ecn_set_ce)(void *ctx) = + (void *)BPF_FUNC_skb_ecn_set_ce; +static int (*bpf_tcp_check_syncookie)(struct bpf_sock *sk, void *ip, int ip_len, void *tcp, int tcp_len) = + (void *)BPF_FUNC_tcp_check_syncookie; +static int (*bpf_sysctl_get_name)(void *ctx, char *buf, unsigned long long buf_len, unsigned long long flags) = + (void *)BPF_FUNC_sysctl_get_name; +static int (*bpf_sysctl_get_current_value)(void *ctx, char *buf, unsigned long long buf_len) = + (void *)BPF_FUNC_sysctl_get_current_value; +static int (*bpf_sysctl_get_new_value)(void *ctx, char *buf, unsigned long long buf_len) = + (void *)BPF_FUNC_sysctl_get_new_value; +static int (*bpf_sysctl_set_new_value)(void *ctx, const char *buf, unsigned long long buf_len) = + (void *)BPF_FUNC_sysctl_set_new_value; +static int (*bpf_strtol)(const char *buf, unsigned long long buf_len, unsigned long long flags, long *res) = + (void *)BPF_FUNC_strtol; +static int (*bpf_strtoul)(const char *buf, unsigned long long buf_len, unsigned long long flags, unsigned long *res) = + (void *)BPF_FUNC_strtoul; +static void *(*bpf_sk_storage_get)(void *map, struct bpf_sock *sk, void *value, __u64 flags) = + (void *)BPF_FUNC_sk_storage_get; +static int (*bpf_sk_storage_delete)(void *map, struct bpf_sock *sk) = + (void *)BPF_FUNC_sk_storage_delete; +static int (*bpf_send_signal)(unsigned sig) = + (void *)BPF_FUNC_send_signal; +static long long (*bpf_tcp_gen_syncookie)(struct bpf_sock *sk, void *ip, int ip_len, void *tcp, int tcp_len) = + (void *)BPF_FUNC_tcp_gen_syncookie; + +/* llvm builtin functions that eBPF C program may use to + * emit BPF_LD_ABS and BPF_LD_IND instructions + */ +struct sk_buff; +unsigned long long load_byte(void *skb, unsigned long long off) asm("llvm.bpf.load.byte"); +unsigned long long load_half(void *skb, unsigned long long off) asm("llvm.bpf.load.half"); +unsigned long long load_word(void *skb, unsigned long long off) asm("llvm.bpf.load.word"); + +/* a helper structure used by eBPF C program + * to describe map attributes to elf_bpf loader + */ +struct bpf_map_def +{ + unsigned int type; + unsigned int key_size; + unsigned int value_size; + unsigned int max_entries; + unsigned int map_flags; + unsigned int inner_map_idx; + unsigned int numa_node; +}; + +#else + +#include + +#endif + +#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \ + struct ____btf_map_##name \ + { \ + type_key key; \ + type_val value; \ + }; \ + struct ____btf_map_##name \ + __attribute__((section(".maps." #name), used)) \ + ____btf_map_##name = {} + +static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) = + (void *)BPF_FUNC_skb_load_bytes; +static int (*bpf_skb_load_bytes_relative)(void *ctx, int off, void *to, int len, __u32 start_header) = + (void *)BPF_FUNC_skb_load_bytes_relative; +static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = + (void *)BPF_FUNC_skb_store_bytes; +static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *)BPF_FUNC_l3_csum_replace; +static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *)BPF_FUNC_l4_csum_replace; +static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) = + (void *)BPF_FUNC_csum_diff; +static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = + (void *)BPF_FUNC_skb_under_cgroup; +static int (*bpf_skb_change_head)(void *, int len, int flags) = + (void *)BPF_FUNC_skb_change_head; +static int (*bpf_skb_pull_data)(void *, int len) = + (void *)BPF_FUNC_skb_pull_data; +static unsigned int (*bpf_get_cgroup_classid)(void *ctx) = + (void *)BPF_FUNC_get_cgroup_classid; +static unsigned int (*bpf_get_route_realm)(void *ctx) = + (void *)BPF_FUNC_get_route_realm; +static int (*bpf_skb_change_proto)(void *ctx, __be16 proto, __u64 flags) = + (void *)BPF_FUNC_skb_change_proto; +static int (*bpf_skb_change_type)(void *ctx, __u32 type) = + (void *)BPF_FUNC_skb_change_type; +static unsigned int (*bpf_get_hash_recalc)(void *ctx) = + (void *)BPF_FUNC_get_hash_recalc; +static unsigned long long (*bpf_get_current_task)(void) = + (void *)BPF_FUNC_get_current_task; +static int (*bpf_skb_change_tail)(void *ctx, __u32 len, __u64 flags) = + (void *)BPF_FUNC_skb_change_tail; +static long long (*bpf_csum_update)(void *ctx, __u32 csum) = + (void *)BPF_FUNC_csum_update; +static void (*bpf_set_hash_invalid)(void *ctx) = + (void *)BPF_FUNC_set_hash_invalid; +static int (*bpf_get_numa_node_id)(void) = + (void *)BPF_FUNC_get_numa_node_id; +static int (*bpf_probe_read_str)(void *ctx, __u32 size, const void *unsafe_ptr) = + (void *)BPF_FUNC_probe_read_str; +static unsigned int (*bpf_get_socket_uid)(void *ctx) = + (void *)BPF_FUNC_get_socket_uid; +static unsigned int (*bpf_set_hash)(void *ctx, __u32 hash) = + (void *)BPF_FUNC_set_hash; +static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode, unsigned long long flags) = + (void *)BPF_FUNC_skb_adjust_room; + +/* Scan the ARCH passed in from ARCH env variable (see Makefile) */ +#if defined(__TARGET_ARCH_x86) +#define bpf_target_x86 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_s390) +#define bpf_target_s390 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_arm) +#define bpf_target_arm +#define bpf_target_defined +#elif defined(__TARGET_ARCH_arm64) +#define bpf_target_arm64 +#define bpf_target_defined +#elif defined(__TARGET_ARCH_mips) +#define bpf_target_mips +#define bpf_target_defined +#elif defined(__TARGET_ARCH_powerpc) +#define bpf_target_powerpc +#define bpf_target_defined +#elif defined(__TARGET_ARCH_sparc) +#define bpf_target_sparc +#define bpf_target_defined +#else +#undef bpf_target_defined +#endif + +/* Fall back to what the compiler says */ +#ifndef bpf_target_defined +#if defined(__x86_64__) +#define bpf_target_x86 +#elif defined(__s390__) +#define bpf_target_s390 +#elif defined(__arm__) +#define bpf_target_arm +#elif defined(__aarch64__) +#define bpf_target_arm64 +#elif defined(__mips__) +#define bpf_target_mips +#elif defined(__powerpc__) +#define bpf_target_powerpc +#elif defined(__sparc__) +#define bpf_target_sparc +#endif +#endif + +#if defined(bpf_target_x86) + +#ifdef __KERNEL__ +#define PT_REGS_PARM1(x) ((x)->di) +#define PT_REGS_PARM2(x) ((x)->si) +#define PT_REGS_PARM3(x) ((x)->dx) +#define PT_REGS_PARM4(x) ((x)->cx) +#define PT_REGS_PARM5(x) ((x)->r8) +#define PT_REGS_RET(x) ((x)->sp) +#define PT_REGS_FP(x) ((x)->bp) +#define PT_REGS_RC(x) ((x)->ax) +#define PT_REGS_SP(x) ((x)->sp) +#define PT_REGS_IP(x) ((x)->ip) +#else +#ifdef __i386__ +/* i386 kernel is built with -mregparm=3 */ +#define PT_REGS_PARM1(x) ((x)->eax) +#define PT_REGS_PARM2(x) ((x)->edx) +#define PT_REGS_PARM3(x) ((x)->ecx) +#define PT_REGS_PARM4(x) 0 +#define PT_REGS_PARM5(x) 0 +#define PT_REGS_RET(x) ((x)->esp) +#define PT_REGS_FP(x) ((x)->ebp) +#define PT_REGS_RC(x) ((x)->eax) +#define PT_REGS_SP(x) ((x)->esp) +#define PT_REGS_IP(x) ((x)->eip) +#else +#define PT_REGS_PARM1(x) ((x)->rdi) +#define PT_REGS_PARM2(x) ((x)->rsi) +#define PT_REGS_PARM3(x) ((x)->rdx) +#define PT_REGS_PARM4(x) ((x)->rcx) +#define PT_REGS_PARM5(x) ((x)->r8) +#define PT_REGS_RET(x) ((x)->rsp) +#define PT_REGS_FP(x) ((x)->rbp) +#define PT_REGS_RC(x) ((x)->rax) +#define PT_REGS_SP(x) ((x)->rsp) +#define PT_REGS_IP(x) ((x)->rip) +#endif +#endif + +#elif defined(bpf_target_s390) + +/* s390 provides user_pt_regs instead of struct pt_regs to userspace */ +struct pt_regs; +#define PT_REGS_S390 const volatile user_pt_regs +#define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2]) +#define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3]) +#define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4]) +#define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5]) +#define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6]) +#define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14]) +/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11]) +#define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2]) +#define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15]) +#define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr) + +#elif defined(bpf_target_arm) + +#define PT_REGS_PARM1(x) ((x)->uregs[0]) +#define PT_REGS_PARM2(x) ((x)->uregs[1]) +#define PT_REGS_PARM3(x) ((x)->uregs[2]) +#define PT_REGS_PARM4(x) ((x)->uregs[3]) +#define PT_REGS_PARM5(x) ((x)->uregs[4]) +#define PT_REGS_RET(x) ((x)->uregs[14]) +#define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC(x) ((x)->uregs[0]) +#define PT_REGS_SP(x) ((x)->uregs[13]) +#define PT_REGS_IP(x) ((x)->uregs[12]) + +#elif defined(bpf_target_arm64) + +/* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ +struct pt_regs; +#define PT_REGS_ARM64 const volatile struct user_pt_regs +#define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0]) +#define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1]) +#define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2]) +#define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3]) +#define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4]) +#define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30]) +/* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29]) +#define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0]) +#define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp) +#define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc) + +#elif defined(bpf_target_mips) + +#define PT_REGS_PARM1(x) ((x)->regs[4]) +#define PT_REGS_PARM2(x) ((x)->regs[5]) +#define PT_REGS_PARM3(x) ((x)->regs[6]) +#define PT_REGS_PARM4(x) ((x)->regs[7]) +#define PT_REGS_PARM5(x) ((x)->regs[8]) +#define PT_REGS_RET(x) ((x)->regs[31]) +#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC(x) ((x)->regs[1]) +#define PT_REGS_SP(x) ((x)->regs[29]) +#define PT_REGS_IP(x) ((x)->cp0_epc) + +#elif defined(bpf_target_powerpc) + +#define PT_REGS_PARM1(x) ((x)->gpr[3]) +#define PT_REGS_PARM2(x) ((x)->gpr[4]) +#define PT_REGS_PARM3(x) ((x)->gpr[5]) +#define PT_REGS_PARM4(x) ((x)->gpr[6]) +#define PT_REGS_PARM5(x) ((x)->gpr[7]) +#define PT_REGS_RC(x) ((x)->gpr[3]) +#define PT_REGS_SP(x) ((x)->sp) +#define PT_REGS_IP(x) ((x)->nip) + +#elif defined(bpf_target_sparc) + +#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) +#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) +#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) +#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) +#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) +#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) +#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) +#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) + +/* Should this also be a bpf_target check for the sparc case? */ +#if defined(__arch64__) +#define PT_REGS_IP(x) ((x)->tpc) +#else +#define PT_REGS_IP(x) ((x)->pc) +#endif + +#endif + +#if defined(bpf_target_powerpc) +#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) +#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP +#elif defined(bpf_target_sparc) +#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) +#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP +#else +#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) +#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ bpf_probe_read(&(ip), sizeof(ip), (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) +#endif + +/* + * BPF_CORE_READ abstracts away bpf_probe_read() call and captures offset + * relocation for source address using __builtin_preserve_access_index() + * built-in, provided by Clang. + * + * __builtin_preserve_access_index() takes as an argument an expression of + * taking an address of a field within struct/union. It makes compiler emit + * a relocation, which records BTF type ID describing root struct/union and an + * accessor string which describes exact embedded field that was used to take + * an address. See detailed description of this relocation format and + * semantics in comments to struct bpf_offset_reloc in libbpf_internal.h. + * + * This relocation allows libbpf to adjust BPF instruction to use correct + * actual field offset, based on target kernel BTF type that matches original + * (local) BTF, used to record relocation. + */ +#define BPF_CORE_READ(dst, src) bpf_probe_read((dst), sizeof(*(src)), __builtin_preserve_access_index(src)) + +#endif diff --git a/bpf/bpf_tun_rss_steering.c b/bpf/bpf_tun_rss_steering.c new file mode 100644 index 0000000..1d86e95 --- /dev/null +++ b/bpf/bpf_tun_rss_steering.c @@ -0,0 +1,423 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "rte_hash_crc.h" +#include "bpf_conf_kernel.h" +#include "bpf_helpers.h" +#include "libendian.h" + +#define IP6_EXTENSIONS_COUNT 11 + +typedef struct bpf_pkt_s +{ + __u8 is_ipv4; + __u8 is_ipv6; + + __u8 is_udp; + __u8 is_tcp; + + __u8 is_fragmented; + + __u16 src_port; + __u16 dst_port; + + __u32 in4_src; + __u32 in4_dst; + + struct in6_addr in6_src; + struct in6_addr in6_dst; + + __u32 src_addr_hash; + __u32 dst_addr_hash; + __u32 src_port_hash; + __u32 dst_port_hash; + __u32 last_hash; + + int select_queue; + + struct __sk_buff *skb; +} bpf_pkt_t; + +/* + * reutrn 0: 表示不需要处理扩展头 + * return 1: 表示需要处理扩展头 + */ +static inline int ip6_next_header_is_need_proc(__u8 hdr_type) +{ + /* + * TODO + * 因为 kni_ipv6_header_parse() 中只跳过了以下 4 种 IPv6 扩展头部: + * IPPROTO_AH + * IPPROTO_HOPOPTS + * IPPROTO_ROUTING + * IPPROTO_DSTOPTS + * + * 即 KNI 回流给 TFE 的 IPv6 流量中只支持以上 4 种 IPv6 扩展头部。 + * 当 TFE 回注给 KNI 的 IPv6 流量中不会出现其他 IPv6 扩展头部,故此处 BPF 只处理这 4 种 IPv6 扩展头部。 + * + * 由于 BPF 要支持四元组分流,所以要判断 IPv6 是否分片,故此处要处理 IPPROTO_FRAGMENT IPv6 扩展头部。 + */ + + switch (hdr_type) + { + case IPPROTO_AH: + case IPPROTO_HOPOPTS: + case IPPROTO_ROUTING: + case IPPROTO_DSTOPTS: + case IPPROTO_FRAGMENT: + return 1; + default: + return 0; + } +} + +static inline int bpf_pkt_parser_ext6(bpf_pkt_t *pkt, __u8 *l4_protocol, int *l4_offset) +{ + if (!ip6_next_header_is_need_proc(*l4_protocol)) + { + return 0; + } + + struct ipv6_opt_hdr ext_hdr = {0}; + for (unsigned int i = 0; i < IP6_EXTENSIONS_COUNT; ++i) + { + if (bpf_skb_load_bytes_relative(pkt->skb, *l4_offset, &ext_hdr, sizeof(ext_hdr), BPF_HDR_START_NET)) + { + bpf_printk("bpf_tun_rss_steering unable get ipv6 ext header"); + return -1; + } + + if (*l4_protocol == IPPROTO_FRAGMENT) + { + pkt->is_fragmented = 1; + } + + *l4_protocol = ext_hdr.nexthdr; + *l4_offset += (ext_hdr.hdrlen + 1) * 8; + + if (!ip6_next_header_is_need_proc(ext_hdr.nexthdr)) + { + return 0; + } + } + + return -1; +} + +static inline void bpf_dump_ipv4_header(bpf_pkt_t *pkt, struct iphdr *ip4) +{ + bpf_printk("bpf_tun_rss_steering ipv4 %p fragmented, src_addr ip[0-1]: %d.%d", pkt->skb, (pkt->in4_src) & 0xFF, (pkt->in4_src >> 8) & 0xFF); + bpf_printk("bpf_tun_rss_steering ipv4 %p fragmented, src_addr ip[2-3]: %d.%d", pkt->skb, (pkt->in4_src >> 16) & 0xFF, (pkt->in4_src >> 24) & 0xFF); + + bpf_printk("bpf_tun_rss_steering ipv4 %p fragmented, dst_addr ip[0-1]: %d.%d", pkt->skb, (pkt->in4_dst) & 0xFF, (pkt->in4_dst >> 8) & 0xFF); + bpf_printk("bpf_tun_rss_steering ipv4 %p fragmented, dst_addr ip[2-3]: %d.%d", pkt->skb, (pkt->in4_dst >> 16) & 0xFF, (pkt->in4_dst >> 24) & 0xFF); + + char *ptr = (char *)ip4; + int len = sizeof(*ip4); + for (int i = 0; i < len; i++) + { + bpf_printk("bpf_tun_rss_steering ipv4 %p fragmented, dump header hex[%d]: %0x", pkt->skb, i, ptr[i]); + } +} + +static inline void bpf_dump_ipv6_header(bpf_pkt_t *pkt, struct ipv6hdr *ip6) +{ + bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, src_addr ip[0-1]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[0]), bpf_ntohs(pkt->in6_src.s6_addr16[1])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, src_addr ip[2-3]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[2]), bpf_ntohs(pkt->in6_src.s6_addr16[3])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, src_addr ip[4-5]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[4]), bpf_ntohs(pkt->in6_src.s6_addr16[5])); + bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, src_addr ip[6-7]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[6]), bpf_ntohs(pkt->in6_src.s6_addr16[7])); + + bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, dst_addr ip[0-1]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[0]), bpf_ntohs(pkt->in6_dst.s6_addr16[1])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, dst_addr ip[2-3]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[2]), bpf_ntohs(pkt->in6_dst.s6_addr16[3])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, dst_addr ip[4-5]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[4]), bpf_ntohs(pkt->in6_dst.s6_addr16[5])); + bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, dst_addr ip[6-7]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[6]), bpf_ntohs(pkt->in6_dst.s6_addr16[7])); + + char *ptr = (char *)ip6; + int len = sizeof(*ip6); + for (int i = 0; i < len; i++) + { + bpf_printk("bpf_tun_rss_steering ipv6 %p fragmented, dump header hex[%d]: %0x", pkt->skb, i, ptr[i]); + } +} + +static inline int ipv4_is_fragment(const struct iphdr *ip4) +{ + /* The frag_off portion of the header consists of: + * + * +----+----+----+----------------------------------+ + * | RS | DF | MF | ...13 bits of fragment offset... | + * +----+----+----+----------------------------------+ + * + * If "More fragments" or the offset is nonzero, then this is an IP fragment (RFC791). + */ + return ip4->frag_off & bpf_htons(0x3FFF); +} + +static inline int bpf_pkt_parser(bpf_pkt_t *pkt, bpf_conf_t *conf) +{ + int l3_offset = 12; + int l4_offset = 0; + __u8 l4_protocol = 0; + __u16 l3_protocol = 0; + + if (!pkt || !pkt->skb) + { + bpf_printk("bpf_tun_rss_steering skb is null"); + return -1; + } + + if (bpf_skb_load_bytes_relative(pkt->skb, l3_offset, &l3_protocol, sizeof(l3_protocol), BPF_HDR_START_MAC)) + { + bpf_printk("bpf_tun_rss_steering unable get l3 protocol"); + return -1; + } + + if (bpf_ntohs(l3_protocol) == ETH_P_IP) + { + pkt->is_ipv4 = 1; + struct iphdr ip = {}; + + if (bpf_skb_load_bytes_relative(pkt->skb, 0, &ip, sizeof(ip), BPF_HDR_START_NET)) + { + bpf_printk("bpf_tun_rss_steering unable get ipv4 header"); + return -1; + } + + pkt->in4_src = ip.saddr; + pkt->in4_dst = ip.daddr; + + pkt->is_fragmented = ipv4_is_fragment(&ip); + l4_protocol = ip.protocol; + l4_offset = ip.ihl * 4; + + if (pkt->is_fragmented) + { + bpf_printk("bpf_tun_rss_steering ipv4 is fragmented"); + if (bpf_conf_get_debug_log(conf)) + { + bpf_dump_ipv4_header(pkt, &ip); + } + return -1; + } + } + else if (bpf_ntohs(l3_protocol) == ETH_P_IPV6) + { + pkt->is_ipv6 = 1; + struct ipv6hdr ip6 = {}; + + if (bpf_skb_load_bytes_relative(pkt->skb, 0, &ip6, sizeof(ip6), BPF_HDR_START_NET)) + { + bpf_printk("bpf_tun_rss_steering unable get ipv6 header"); + return -1; + } + + pkt->in6_src = ip6.saddr; + pkt->in6_dst = ip6.daddr; + + l4_protocol = ip6.nexthdr; + l4_offset = sizeof(ip6); + + if (bpf_pkt_parser_ext6(pkt, &l4_protocol, &l4_offset) == -1) + { + return -1; + } + + if (pkt->is_fragmented) + { + bpf_printk("bpf_tun_rss_steering ipv6 is fragmented"); + if (bpf_conf_get_debug_log(conf)) + { + bpf_dump_ipv6_header(pkt, &ip6); + } + return -1; + } + } + else + { + bpf_printk("bpf_tun_rss_steering l3 protocol %d not support", bpf_ntohs(l3_protocol)); + return -1; + } + + if (l4_protocol == IPPROTO_TCP) + { + pkt->is_tcp = 1; + struct tcphdr tcp = {}; + + if (bpf_skb_load_bytes_relative(pkt->skb, l4_offset, &tcp, sizeof(tcp), BPF_HDR_START_NET)) + { + bpf_printk("bpf_tun_rss_steering unable get tcp header"); + return -1; + } + + pkt->src_port = tcp.source; + pkt->dst_port = tcp.dest; + } + else if (l4_protocol == IPPROTO_UDP) + { + pkt->is_udp = 1; + struct udphdr udp = {}; + + if (bpf_skb_load_bytes_relative(pkt->skb, l4_offset, &udp, sizeof(udp), BPF_HDR_START_NET)) + { + bpf_printk("bpf_tun_rss_steering unable get udp header"); + return -1; + } + + pkt->src_port = udp.source; + pkt->dst_port = udp.dest; + } + else + { + bpf_printk("bpf_tun_rss_steering l4 protocol %d not support", l4_protocol); + return -1; + } + + return 0; +} + +static inline void bpf_pkt_debug_log(bpf_pkt_t *pkt, bpf_conf_t *conf) +{ + if (pkt->is_ipv4) + { + bpf_printk("bpf_tun_rss_steering ipv4 %p src_addr ip[0-1]: %d.%d", pkt->skb, (pkt->in4_src) & 0xFF, (pkt->in4_src >> 8) & 0xFF); + bpf_printk("bpf_tun_rss_steering ipv4 %p src_addr ip[2-3]: %d.%d", pkt->skb, (pkt->in4_src >> 16) & 0xFF, (pkt->in4_src >> 24) & 0xFF); + + bpf_printk("bpf_tun_rss_steering ipv4 %p dst_addr ip[0-1]: %d.%d", pkt->skb, (pkt->in4_dst) & 0xFF, (pkt->in4_dst >> 8) & 0xFF); + bpf_printk("bpf_tun_rss_steering ipv4 %p dst_addr ip[2-3]: %d.%d", pkt->skb, (pkt->in4_dst >> 16) & 0xFF, (pkt->in4_dst >> 24) & 0xFF); + + bpf_printk("bpf_tun_rss_steering ipv4 %p src_port: %d dst_port: %d", pkt->skb, bpf_ntohs(pkt->src_port), bpf_ntohs(pkt->dst_port)); + + bpf_printk("bpf_tun_rss_steering ipv4 %p src_addr_hash: %d dst_addr_hash: %d", pkt->skb, pkt->src_addr_hash, pkt->dst_addr_hash); + bpf_printk("bpf_tun_rss_steering ipv4 %p src_port_hash: %d dst_port_hash: %d", pkt->skb, pkt->src_port_hash, pkt->dst_port_hash); + + bpf_printk("bpf_tun_rss_steering ipv4 %p last_hash: %d select_queue: %d", pkt->skb, pkt->last_hash, pkt->select_queue); + } + + if (pkt->is_ipv6) + { + bpf_printk("bpf_tun_rss_steering ipv6 %p src_addr ip[0-1]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[0]), bpf_ntohs(pkt->in6_src.s6_addr16[1])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p src_addr ip[2-3]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[2]), bpf_ntohs(pkt->in6_src.s6_addr16[3])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p src_addr ip[4-5]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[4]), bpf_ntohs(pkt->in6_src.s6_addr16[5])); + bpf_printk("bpf_tun_rss_steering ipv6 %p src_addr ip[6-7]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_src.s6_addr16[6]), bpf_ntohs(pkt->in6_src.s6_addr16[7])); + + bpf_printk("bpf_tun_rss_steering ipv6 %p dst_addr ip[0-1]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[0]), bpf_ntohs(pkt->in6_dst.s6_addr16[1])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p dst_addr ip[2-3]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[2]), bpf_ntohs(pkt->in6_dst.s6_addr16[3])); + // bpf_printk("bpf_tun_rss_steering ipv6 %p dst_addr ip[4-5]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[4]), bpf_ntohs(pkt->in6_dst.s6_addr16[5])); + bpf_printk("bpf_tun_rss_steering ipv6 %p dst_addr ip[6-7]: %x:%x", pkt->skb, bpf_ntohs(pkt->in6_dst.s6_addr16[6]), bpf_ntohs(pkt->in6_dst.s6_addr16[7])); + + bpf_printk("bpf_tun_rss_steering ipv6 %p src_port: %d dst_port: %d", pkt->skb, bpf_ntohs(pkt->src_port), bpf_ntohs(pkt->dst_port)); + + bpf_printk("bpf_tun_rss_steering ipv6 %p src_addr_hash: %d dst_addr_hash: %d", pkt->skb, pkt->src_addr_hash, pkt->dst_addr_hash); + bpf_printk("bpf_tun_rss_steering ipv6 %p src_port_hash: %d dst_port_hash: %d", pkt->skb, pkt->src_port_hash, pkt->dst_port_hash); + + bpf_printk("bpf_tun_rss_steering ipv6 %p last_hash: %d select_queue: %d", pkt->skb, pkt->last_hash, pkt->select_queue); + } +} + +static inline void bpf_pkt_select_queue(bpf_pkt_t *pkt, bpf_conf_t *conf) +{ + pkt->select_queue = -1; + + if (pkt->is_ipv4) + { + if (bpf_conf_get_hash_mode(conf) == BPF_HASH_MODE_TUPLE4) + { + pkt->src_addr_hash = rte_hash_crc(&pkt->in4_src, 4, 0); + pkt->dst_addr_hash = rte_hash_crc(&pkt->in4_dst, 4, 0); + pkt->last_hash = pkt->src_addr_hash ^ pkt->dst_addr_hash; + + pkt->src_port_hash = rte_hash_crc(&pkt->src_port, 2, pkt->last_hash); + pkt->dst_port_hash = rte_hash_crc(&pkt->dst_port, 2, pkt->last_hash); + pkt->last_hash = pkt->src_port_hash ^ pkt->dst_port_hash; + + pkt->select_queue = pkt->last_hash % bpf_conf_get_queue_num(conf); + } + else if (bpf_conf_get_hash_mode(conf) == BPF_HASH_MODE_TUPLE2) + { + + pkt->src_addr_hash = rte_hash_crc(&pkt->in4_src, 4, 0); + pkt->dst_addr_hash = rte_hash_crc(&pkt->in4_dst, 4, 0); + pkt->last_hash = pkt->src_addr_hash ^ pkt->dst_addr_hash; + + pkt->select_queue = pkt->last_hash % bpf_conf_get_queue_num(conf); + } + } + + if (pkt->is_ipv6) + { + if (bpf_conf_get_hash_mode(conf) == BPF_HASH_MODE_TUPLE4) + { + pkt->src_addr_hash = rte_hash_crc(&pkt->in6_src, 16, 0); + pkt->dst_addr_hash = rte_hash_crc(&pkt->in6_dst, 16, 0); + pkt->last_hash = pkt->src_addr_hash ^ pkt->dst_addr_hash; + + pkt->src_port_hash = rte_hash_crc(&pkt->src_port, 2, pkt->last_hash); + pkt->dst_port_hash = rte_hash_crc(&pkt->dst_port, 2, pkt->last_hash); + pkt->last_hash = pkt->src_port_hash ^ pkt->dst_port_hash; + + pkt->select_queue = pkt->last_hash % bpf_conf_get_queue_num(conf); + } + else if (bpf_conf_get_hash_mode(conf) == BPF_HASH_MODE_TUPLE2) + { + pkt->src_addr_hash = rte_hash_crc(&pkt->in6_src, 16, 0); + pkt->dst_addr_hash = rte_hash_crc(&pkt->in6_dst, 16, 0); + pkt->last_hash = pkt->src_addr_hash ^ pkt->dst_addr_hash; + + pkt->select_queue = pkt->last_hash % bpf_conf_get_queue_num(conf); + } + } +} + +static void bpf_conf_dump(bpf_conf_t *conf) +{ + if (bpf_conf_get_debug_log(conf)) + { + bpf_printk("bpf_debug_log : %d", bpf_conf_get_debug_log(conf)); + bpf_printk("bpf_queue_num : %d", bpf_conf_get_queue_num(conf)); + bpf_printk("bpf_hash_mode : %d", bpf_conf_get_hash_mode(conf)); + } +} + +SEC("tun_rss_steering") +int bpf_tun_rss_steering(struct __sk_buff *skb) +{ + bpf_pkt_t pkt = {}; + bpf_conf_t conf = {}; + + bpf_conf_lookup_map(&conf); + bpf_conf_dump(&conf); + + if (bpf_conf_get_queue_num(&conf) <= 0) + { + bpf_printk("bpf_tun_rss_steering invalid queue num: %d", bpf_conf_get_queue_num(&conf)); + return -1; + } + + if (bpf_conf_get_hash_mode(&conf) != BPF_HASH_MODE_TUPLE2 && bpf_conf_get_hash_mode(&conf) != BPF_HASH_MODE_TUPLE4) + { + bpf_printk("bpf_tun_rss_steering invalid hash mode: %d", bpf_conf_get_hash_mode(&conf)); + return -1; + } + + pkt.skb = skb; + if (bpf_pkt_parser(&pkt, &conf) == -1) + { + return -1; + } + + bpf_pkt_select_queue(&pkt, &conf); + + if (bpf_conf_get_debug_log(&conf)) + { + bpf_pkt_debug_log(&pkt, &conf); + } + + return pkt.select_queue; +} + +char _license[] SEC("license") = "GPL"; \ No newline at end of file diff --git a/bpf/libendian.h b/bpf/libendian.h new file mode 100644 index 0000000..3aa742c --- /dev/null +++ b/bpf/libendian.h @@ -0,0 +1,25 @@ +/* Copyright Authors of Cilium */ + +#ifndef __LIB_ENDIAN_H_ +#define __LIB_ENDIAN_H_ + +#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define __bpf_ntohs(x) __builtin_bswap16(x) +#define __bpf_htons(x) __builtin_bswap16(x) +#define __bpf_ntohl(x) __builtin_bswap32(x) +#define __bpf_htonl(x) __builtin_bswap32(x) +#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define __bpf_ntohs(x) (x) +#define __bpf_htons(x) (x) +#define __bpf_ntohl(x) (x) +#define __bpf_htonl(x) (x) +#else +#error "Endianness detection needs to be set up for your compiler?!" +#endif + +#define bpf_htons(x) (__builtin_constant_p(x) ? __constant_htons(x) : __bpf_htons(x)) +#define bpf_ntohs(x) (__builtin_constant_p(x) ? __constant_ntohs(x) : __bpf_ntohs(x)) +#define bpf_htonl(x) (__builtin_constant_p(x) ? __constant_htonl(x) : __bpf_htonl(x)) +#define bpf_ntohl(x) (__builtin_constant_p(x) ? __constant_ntohl(x) : __bpf_ntohl(x)) + +#endif \ No newline at end of file diff --git a/bpf/makefile b/bpf/makefile new file mode 100644 index 0000000..dbe911d --- /dev/null +++ b/bpf/makefile @@ -0,0 +1,5 @@ +all: + clang -O2 -g -target bpf -c bpf_tun_rss_steering.c -o bpf_tun_rss_steering.o + +clean: + rm -rf bpf_tun_rss_steering.o diff --git a/bpf/rte_hash_crc.h b/bpf/rte_hash_crc.h new file mode 100644 index 0000000..104f5dc --- /dev/null +++ b/bpf/rte_hash_crc.h @@ -0,0 +1,357 @@ +#ifndef _RTE_HASH_CRC_H_ +#define _RTE_HASH_CRC_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define CRC32_UPD(crc, n) \ + (crc32c_tables[(n)][(crc)&0xFF] ^ \ + crc32c_tables[(n)-1][((crc) >> 8) & 0xFF]) + +static const __u32 crc32c_tables[8][256] = { + {0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4, 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB, + 0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B, 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24, + 0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B, 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384, + 0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54, 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B, + 0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A, 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35, + 0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5, 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA, + 0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45, 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A, + 0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A, 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595, + 0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48, 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957, + 0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687, 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198, + 0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927, 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38, + 0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8, 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7, + 0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096, 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789, + 0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859, 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46, + 0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9, 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6, + 0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36, 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829, + 0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C, 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93, + 0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043, 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C, + 0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3, 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC, + 0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C, 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033, + 0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652, 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D, + 0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D, 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982, + 0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D, 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622, + 0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2, 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED, + 0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530, 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F, + 0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF, 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0, + 0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F, 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540, + 0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90, 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F, + 0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE, 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1, + 0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321, 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E, + 0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81, 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E, + 0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E, 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351}, + {0x00000000, 0x13A29877, 0x274530EE, 0x34E7A899, 0x4E8A61DC, 0x5D28F9AB, 0x69CF5132, 0x7A6DC945, + 0x9D14C3B8, 0x8EB65BCF, 0xBA51F356, 0xA9F36B21, 0xD39EA264, 0xC03C3A13, 0xF4DB928A, 0xE7790AFD, + 0x3FC5F181, 0x2C6769F6, 0x1880C16F, 0x0B225918, 0x714F905D, 0x62ED082A, 0x560AA0B3, 0x45A838C4, + 0xA2D13239, 0xB173AA4E, 0x859402D7, 0x96369AA0, 0xEC5B53E5, 0xFFF9CB92, 0xCB1E630B, 0xD8BCFB7C, + 0x7F8BE302, 0x6C297B75, 0x58CED3EC, 0x4B6C4B9B, 0x310182DE, 0x22A31AA9, 0x1644B230, 0x05E62A47, + 0xE29F20BA, 0xF13DB8CD, 0xC5DA1054, 0xD6788823, 0xAC154166, 0xBFB7D911, 0x8B507188, 0x98F2E9FF, + 0x404E1283, 0x53EC8AF4, 0x670B226D, 0x74A9BA1A, 0x0EC4735F, 0x1D66EB28, 0x298143B1, 0x3A23DBC6, + 0xDD5AD13B, 0xCEF8494C, 0xFA1FE1D5, 0xE9BD79A2, 0x93D0B0E7, 0x80722890, 0xB4958009, 0xA737187E, + 0xFF17C604, 0xECB55E73, 0xD852F6EA, 0xCBF06E9D, 0xB19DA7D8, 0xA23F3FAF, 0x96D89736, 0x857A0F41, + 0x620305BC, 0x71A19DCB, 0x45463552, 0x56E4AD25, 0x2C896460, 0x3F2BFC17, 0x0BCC548E, 0x186ECCF9, + 0xC0D23785, 0xD370AFF2, 0xE797076B, 0xF4359F1C, 0x8E585659, 0x9DFACE2E, 0xA91D66B7, 0xBABFFEC0, + 0x5DC6F43D, 0x4E646C4A, 0x7A83C4D3, 0x69215CA4, 0x134C95E1, 0x00EE0D96, 0x3409A50F, 0x27AB3D78, + 0x809C2506, 0x933EBD71, 0xA7D915E8, 0xB47B8D9F, 0xCE1644DA, 0xDDB4DCAD, 0xE9537434, 0xFAF1EC43, + 0x1D88E6BE, 0x0E2A7EC9, 0x3ACDD650, 0x296F4E27, 0x53028762, 0x40A01F15, 0x7447B78C, 0x67E52FFB, + 0xBF59D487, 0xACFB4CF0, 0x981CE469, 0x8BBE7C1E, 0xF1D3B55B, 0xE2712D2C, 0xD69685B5, 0xC5341DC2, + 0x224D173F, 0x31EF8F48, 0x050827D1, 0x16AABFA6, 0x6CC776E3, 0x7F65EE94, 0x4B82460D, 0x5820DE7A, + 0xFBC3FAF9, 0xE861628E, 0xDC86CA17, 0xCF245260, 0xB5499B25, 0xA6EB0352, 0x920CABCB, 0x81AE33BC, + 0x66D73941, 0x7575A136, 0x419209AF, 0x523091D8, 0x285D589D, 0x3BFFC0EA, 0x0F186873, 0x1CBAF004, + 0xC4060B78, 0xD7A4930F, 0xE3433B96, 0xF0E1A3E1, 0x8A8C6AA4, 0x992EF2D3, 0xADC95A4A, 0xBE6BC23D, + 0x5912C8C0, 0x4AB050B7, 0x7E57F82E, 0x6DF56059, 0x1798A91C, 0x043A316B, 0x30DD99F2, 0x237F0185, + 0x844819FB, 0x97EA818C, 0xA30D2915, 0xB0AFB162, 0xCAC27827, 0xD960E050, 0xED8748C9, 0xFE25D0BE, + 0x195CDA43, 0x0AFE4234, 0x3E19EAAD, 0x2DBB72DA, 0x57D6BB9F, 0x447423E8, 0x70938B71, 0x63311306, + 0xBB8DE87A, 0xA82F700D, 0x9CC8D894, 0x8F6A40E3, 0xF50789A6, 0xE6A511D1, 0xD242B948, 0xC1E0213F, + 0x26992BC2, 0x353BB3B5, 0x01DC1B2C, 0x127E835B, 0x68134A1E, 0x7BB1D269, 0x4F567AF0, 0x5CF4E287, + 0x04D43CFD, 0x1776A48A, 0x23910C13, 0x30339464, 0x4A5E5D21, 0x59FCC556, 0x6D1B6DCF, 0x7EB9F5B8, + 0x99C0FF45, 0x8A626732, 0xBE85CFAB, 0xAD2757DC, 0xD74A9E99, 0xC4E806EE, 0xF00FAE77, 0xE3AD3600, + 0x3B11CD7C, 0x28B3550B, 0x1C54FD92, 0x0FF665E5, 0x759BACA0, 0x663934D7, 0x52DE9C4E, 0x417C0439, + 0xA6050EC4, 0xB5A796B3, 0x81403E2A, 0x92E2A65D, 0xE88F6F18, 0xFB2DF76F, 0xCFCA5FF6, 0xDC68C781, + 0x7B5FDFFF, 0x68FD4788, 0x5C1AEF11, 0x4FB87766, 0x35D5BE23, 0x26772654, 0x12908ECD, 0x013216BA, + 0xE64B1C47, 0xF5E98430, 0xC10E2CA9, 0xD2ACB4DE, 0xA8C17D9B, 0xBB63E5EC, 0x8F844D75, 0x9C26D502, + 0x449A2E7E, 0x5738B609, 0x63DF1E90, 0x707D86E7, 0x0A104FA2, 0x19B2D7D5, 0x2D557F4C, 0x3EF7E73B, + 0xD98EEDC6, 0xCA2C75B1, 0xFECBDD28, 0xED69455F, 0x97048C1A, 0x84A6146D, 0xB041BCF4, 0xA3E32483}, + {0x00000000, 0xA541927E, 0x4F6F520D, 0xEA2EC073, 0x9EDEA41A, 0x3B9F3664, 0xD1B1F617, 0x74F06469, + 0x38513EC5, 0x9D10ACBB, 0x773E6CC8, 0xD27FFEB6, 0xA68F9ADF, 0x03CE08A1, 0xE9E0C8D2, 0x4CA15AAC, + 0x70A27D8A, 0xD5E3EFF4, 0x3FCD2F87, 0x9A8CBDF9, 0xEE7CD990, 0x4B3D4BEE, 0xA1138B9D, 0x045219E3, + 0x48F3434F, 0xEDB2D131, 0x079C1142, 0xA2DD833C, 0xD62DE755, 0x736C752B, 0x9942B558, 0x3C032726, + 0xE144FB14, 0x4405696A, 0xAE2BA919, 0x0B6A3B67, 0x7F9A5F0E, 0xDADBCD70, 0x30F50D03, 0x95B49F7D, + 0xD915C5D1, 0x7C5457AF, 0x967A97DC, 0x333B05A2, 0x47CB61CB, 0xE28AF3B5, 0x08A433C6, 0xADE5A1B8, + 0x91E6869E, 0x34A714E0, 0xDE89D493, 0x7BC846ED, 0x0F382284, 0xAA79B0FA, 0x40577089, 0xE516E2F7, + 0xA9B7B85B, 0x0CF62A25, 0xE6D8EA56, 0x43997828, 0x37691C41, 0x92288E3F, 0x78064E4C, 0xDD47DC32, + 0xC76580D9, 0x622412A7, 0x880AD2D4, 0x2D4B40AA, 0x59BB24C3, 0xFCFAB6BD, 0x16D476CE, 0xB395E4B0, + 0xFF34BE1C, 0x5A752C62, 0xB05BEC11, 0x151A7E6F, 0x61EA1A06, 0xC4AB8878, 0x2E85480B, 0x8BC4DA75, + 0xB7C7FD53, 0x12866F2D, 0xF8A8AF5E, 0x5DE93D20, 0x29195949, 0x8C58CB37, 0x66760B44, 0xC337993A, + 0x8F96C396, 0x2AD751E8, 0xC0F9919B, 0x65B803E5, 0x1148678C, 0xB409F5F2, 0x5E273581, 0xFB66A7FF, + 0x26217BCD, 0x8360E9B3, 0x694E29C0, 0xCC0FBBBE, 0xB8FFDFD7, 0x1DBE4DA9, 0xF7908DDA, 0x52D11FA4, + 0x1E704508, 0xBB31D776, 0x511F1705, 0xF45E857B, 0x80AEE112, 0x25EF736C, 0xCFC1B31F, 0x6A802161, + 0x56830647, 0xF3C29439, 0x19EC544A, 0xBCADC634, 0xC85DA25D, 0x6D1C3023, 0x8732F050, 0x2273622E, + 0x6ED23882, 0xCB93AAFC, 0x21BD6A8F, 0x84FCF8F1, 0xF00C9C98, 0x554D0EE6, 0xBF63CE95, 0x1A225CEB, + 0x8B277743, 0x2E66E53D, 0xC448254E, 0x6109B730, 0x15F9D359, 0xB0B84127, 0x5A968154, 0xFFD7132A, + 0xB3764986, 0x1637DBF8, 0xFC191B8B, 0x595889F5, 0x2DA8ED9C, 0x88E97FE2, 0x62C7BF91, 0xC7862DEF, + 0xFB850AC9, 0x5EC498B7, 0xB4EA58C4, 0x11ABCABA, 0x655BAED3, 0xC01A3CAD, 0x2A34FCDE, 0x8F756EA0, + 0xC3D4340C, 0x6695A672, 0x8CBB6601, 0x29FAF47F, 0x5D0A9016, 0xF84B0268, 0x1265C21B, 0xB7245065, + 0x6A638C57, 0xCF221E29, 0x250CDE5A, 0x804D4C24, 0xF4BD284D, 0x51FCBA33, 0xBBD27A40, 0x1E93E83E, + 0x5232B292, 0xF77320EC, 0x1D5DE09F, 0xB81C72E1, 0xCCEC1688, 0x69AD84F6, 0x83834485, 0x26C2D6FB, + 0x1AC1F1DD, 0xBF8063A3, 0x55AEA3D0, 0xF0EF31AE, 0x841F55C7, 0x215EC7B9, 0xCB7007CA, 0x6E3195B4, + 0x2290CF18, 0x87D15D66, 0x6DFF9D15, 0xC8BE0F6B, 0xBC4E6B02, 0x190FF97C, 0xF321390F, 0x5660AB71, + 0x4C42F79A, 0xE90365E4, 0x032DA597, 0xA66C37E9, 0xD29C5380, 0x77DDC1FE, 0x9DF3018D, 0x38B293F3, + 0x7413C95F, 0xD1525B21, 0x3B7C9B52, 0x9E3D092C, 0xEACD6D45, 0x4F8CFF3B, 0xA5A23F48, 0x00E3AD36, + 0x3CE08A10, 0x99A1186E, 0x738FD81D, 0xD6CE4A63, 0xA23E2E0A, 0x077FBC74, 0xED517C07, 0x4810EE79, + 0x04B1B4D5, 0xA1F026AB, 0x4BDEE6D8, 0xEE9F74A6, 0x9A6F10CF, 0x3F2E82B1, 0xD50042C2, 0x7041D0BC, + 0xAD060C8E, 0x08479EF0, 0xE2695E83, 0x4728CCFD, 0x33D8A894, 0x96993AEA, 0x7CB7FA99, 0xD9F668E7, + 0x9557324B, 0x3016A035, 0xDA386046, 0x7F79F238, 0x0B899651, 0xAEC8042F, 0x44E6C45C, 0xE1A75622, + 0xDDA47104, 0x78E5E37A, 0x92CB2309, 0x378AB177, 0x437AD51E, 0xE63B4760, 0x0C158713, 0xA954156D, + 0xE5F54FC1, 0x40B4DDBF, 0xAA9A1DCC, 0x0FDB8FB2, 0x7B2BEBDB, 0xDE6A79A5, 0x3444B9D6, 0x91052BA8}, + {0x00000000, 0xDD45AAB8, 0xBF672381, 0x62228939, 0x7B2231F3, 0xA6679B4B, 0xC4451272, 0x1900B8CA, + 0xF64463E6, 0x2B01C95E, 0x49234067, 0x9466EADF, 0x8D665215, 0x5023F8AD, 0x32017194, 0xEF44DB2C, + 0xE964B13D, 0x34211B85, 0x560392BC, 0x8B463804, 0x924680CE, 0x4F032A76, 0x2D21A34F, 0xF06409F7, + 0x1F20D2DB, 0xC2657863, 0xA047F15A, 0x7D025BE2, 0x6402E328, 0xB9474990, 0xDB65C0A9, 0x06206A11, + 0xD725148B, 0x0A60BE33, 0x6842370A, 0xB5079DB2, 0xAC072578, 0x71428FC0, 0x136006F9, 0xCE25AC41, + 0x2161776D, 0xFC24DDD5, 0x9E0654EC, 0x4343FE54, 0x5A43469E, 0x8706EC26, 0xE524651F, 0x3861CFA7, + 0x3E41A5B6, 0xE3040F0E, 0x81268637, 0x5C632C8F, 0x45639445, 0x98263EFD, 0xFA04B7C4, 0x27411D7C, + 0xC805C650, 0x15406CE8, 0x7762E5D1, 0xAA274F69, 0xB327F7A3, 0x6E625D1B, 0x0C40D422, 0xD1057E9A, + 0xABA65FE7, 0x76E3F55F, 0x14C17C66, 0xC984D6DE, 0xD0846E14, 0x0DC1C4AC, 0x6FE34D95, 0xB2A6E72D, + 0x5DE23C01, 0x80A796B9, 0xE2851F80, 0x3FC0B538, 0x26C00DF2, 0xFB85A74A, 0x99A72E73, 0x44E284CB, + 0x42C2EEDA, 0x9F874462, 0xFDA5CD5B, 0x20E067E3, 0x39E0DF29, 0xE4A57591, 0x8687FCA8, 0x5BC25610, + 0xB4868D3C, 0x69C32784, 0x0BE1AEBD, 0xD6A40405, 0xCFA4BCCF, 0x12E11677, 0x70C39F4E, 0xAD8635F6, + 0x7C834B6C, 0xA1C6E1D4, 0xC3E468ED, 0x1EA1C255, 0x07A17A9F, 0xDAE4D027, 0xB8C6591E, 0x6583F3A6, + 0x8AC7288A, 0x57828232, 0x35A00B0B, 0xE8E5A1B3, 0xF1E51979, 0x2CA0B3C1, 0x4E823AF8, 0x93C79040, + 0x95E7FA51, 0x48A250E9, 0x2A80D9D0, 0xF7C57368, 0xEEC5CBA2, 0x3380611A, 0x51A2E823, 0x8CE7429B, + 0x63A399B7, 0xBEE6330F, 0xDCC4BA36, 0x0181108E, 0x1881A844, 0xC5C402FC, 0xA7E68BC5, 0x7AA3217D, + 0x52A0C93F, 0x8FE56387, 0xEDC7EABE, 0x30824006, 0x2982F8CC, 0xF4C75274, 0x96E5DB4D, 0x4BA071F5, + 0xA4E4AAD9, 0x79A10061, 0x1B838958, 0xC6C623E0, 0xDFC69B2A, 0x02833192, 0x60A1B8AB, 0xBDE41213, + 0xBBC47802, 0x6681D2BA, 0x04A35B83, 0xD9E6F13B, 0xC0E649F1, 0x1DA3E349, 0x7F816A70, 0xA2C4C0C8, + 0x4D801BE4, 0x90C5B15C, 0xF2E73865, 0x2FA292DD, 0x36A22A17, 0xEBE780AF, 0x89C50996, 0x5480A32E, + 0x8585DDB4, 0x58C0770C, 0x3AE2FE35, 0xE7A7548D, 0xFEA7EC47, 0x23E246FF, 0x41C0CFC6, 0x9C85657E, + 0x73C1BE52, 0xAE8414EA, 0xCCA69DD3, 0x11E3376B, 0x08E38FA1, 0xD5A62519, 0xB784AC20, 0x6AC10698, + 0x6CE16C89, 0xB1A4C631, 0xD3864F08, 0x0EC3E5B0, 0x17C35D7A, 0xCA86F7C2, 0xA8A47EFB, 0x75E1D443, + 0x9AA50F6F, 0x47E0A5D7, 0x25C22CEE, 0xF8878656, 0xE1873E9C, 0x3CC29424, 0x5EE01D1D, 0x83A5B7A5, + 0xF90696D8, 0x24433C60, 0x4661B559, 0x9B241FE1, 0x8224A72B, 0x5F610D93, 0x3D4384AA, 0xE0062E12, + 0x0F42F53E, 0xD2075F86, 0xB025D6BF, 0x6D607C07, 0x7460C4CD, 0xA9256E75, 0xCB07E74C, 0x16424DF4, + 0x106227E5, 0xCD278D5D, 0xAF050464, 0x7240AEDC, 0x6B401616, 0xB605BCAE, 0xD4273597, 0x09629F2F, + 0xE6264403, 0x3B63EEBB, 0x59416782, 0x8404CD3A, 0x9D0475F0, 0x4041DF48, 0x22635671, 0xFF26FCC9, + 0x2E238253, 0xF36628EB, 0x9144A1D2, 0x4C010B6A, 0x5501B3A0, 0x88441918, 0xEA669021, 0x37233A99, + 0xD867E1B5, 0x05224B0D, 0x6700C234, 0xBA45688C, 0xA345D046, 0x7E007AFE, 0x1C22F3C7, 0xC167597F, + 0xC747336E, 0x1A0299D6, 0x782010EF, 0xA565BA57, 0xBC65029D, 0x6120A825, 0x0302211C, 0xDE478BA4, + 0x31035088, 0xEC46FA30, 0x8E647309, 0x5321D9B1, 0x4A21617B, 0x9764CBC3, 0xF54642FA, 0x2803E842}, + {0x00000000, 0x38116FAC, 0x7022DF58, 0x4833B0F4, 0xE045BEB0, 0xD854D11C, 0x906761E8, 0xA8760E44, + 0xC5670B91, 0xFD76643D, 0xB545D4C9, 0x8D54BB65, 0x2522B521, 0x1D33DA8D, 0x55006A79, 0x6D1105D5, + 0x8F2261D3, 0xB7330E7F, 0xFF00BE8B, 0xC711D127, 0x6F67DF63, 0x5776B0CF, 0x1F45003B, 0x27546F97, + 0x4A456A42, 0x725405EE, 0x3A67B51A, 0x0276DAB6, 0xAA00D4F2, 0x9211BB5E, 0xDA220BAA, 0xE2336406, + 0x1BA8B557, 0x23B9DAFB, 0x6B8A6A0F, 0x539B05A3, 0xFBED0BE7, 0xC3FC644B, 0x8BCFD4BF, 0xB3DEBB13, + 0xDECFBEC6, 0xE6DED16A, 0xAEED619E, 0x96FC0E32, 0x3E8A0076, 0x069B6FDA, 0x4EA8DF2E, 0x76B9B082, + 0x948AD484, 0xAC9BBB28, 0xE4A80BDC, 0xDCB96470, 0x74CF6A34, 0x4CDE0598, 0x04EDB56C, 0x3CFCDAC0, + 0x51EDDF15, 0x69FCB0B9, 0x21CF004D, 0x19DE6FE1, 0xB1A861A5, 0x89B90E09, 0xC18ABEFD, 0xF99BD151, + 0x37516AAE, 0x0F400502, 0x4773B5F6, 0x7F62DA5A, 0xD714D41E, 0xEF05BBB2, 0xA7360B46, 0x9F2764EA, + 0xF236613F, 0xCA270E93, 0x8214BE67, 0xBA05D1CB, 0x1273DF8F, 0x2A62B023, 0x625100D7, 0x5A406F7B, + 0xB8730B7D, 0x806264D1, 0xC851D425, 0xF040BB89, 0x5836B5CD, 0x6027DA61, 0x28146A95, 0x10050539, + 0x7D1400EC, 0x45056F40, 0x0D36DFB4, 0x3527B018, 0x9D51BE5C, 0xA540D1F0, 0xED736104, 0xD5620EA8, + 0x2CF9DFF9, 0x14E8B055, 0x5CDB00A1, 0x64CA6F0D, 0xCCBC6149, 0xF4AD0EE5, 0xBC9EBE11, 0x848FD1BD, + 0xE99ED468, 0xD18FBBC4, 0x99BC0B30, 0xA1AD649C, 0x09DB6AD8, 0x31CA0574, 0x79F9B580, 0x41E8DA2C, + 0xA3DBBE2A, 0x9BCAD186, 0xD3F96172, 0xEBE80EDE, 0x439E009A, 0x7B8F6F36, 0x33BCDFC2, 0x0BADB06E, + 0x66BCB5BB, 0x5EADDA17, 0x169E6AE3, 0x2E8F054F, 0x86F90B0B, 0xBEE864A7, 0xF6DBD453, 0xCECABBFF, + 0x6EA2D55C, 0x56B3BAF0, 0x1E800A04, 0x269165A8, 0x8EE76BEC, 0xB6F60440, 0xFEC5B4B4, 0xC6D4DB18, + 0xABC5DECD, 0x93D4B161, 0xDBE70195, 0xE3F66E39, 0x4B80607D, 0x73910FD1, 0x3BA2BF25, 0x03B3D089, + 0xE180B48F, 0xD991DB23, 0x91A26BD7, 0xA9B3047B, 0x01C50A3F, 0x39D46593, 0x71E7D567, 0x49F6BACB, + 0x24E7BF1E, 0x1CF6D0B2, 0x54C56046, 0x6CD40FEA, 0xC4A201AE, 0xFCB36E02, 0xB480DEF6, 0x8C91B15A, + 0x750A600B, 0x4D1B0FA7, 0x0528BF53, 0x3D39D0FF, 0x954FDEBB, 0xAD5EB117, 0xE56D01E3, 0xDD7C6E4F, + 0xB06D6B9A, 0x887C0436, 0xC04FB4C2, 0xF85EDB6E, 0x5028D52A, 0x6839BA86, 0x200A0A72, 0x181B65DE, + 0xFA2801D8, 0xC2396E74, 0x8A0ADE80, 0xB21BB12C, 0x1A6DBF68, 0x227CD0C4, 0x6A4F6030, 0x525E0F9C, + 0x3F4F0A49, 0x075E65E5, 0x4F6DD511, 0x777CBABD, 0xDF0AB4F9, 0xE71BDB55, 0xAF286BA1, 0x9739040D, + 0x59F3BFF2, 0x61E2D05E, 0x29D160AA, 0x11C00F06, 0xB9B60142, 0x81A76EEE, 0xC994DE1A, 0xF185B1B6, + 0x9C94B463, 0xA485DBCF, 0xECB66B3B, 0xD4A70497, 0x7CD10AD3, 0x44C0657F, 0x0CF3D58B, 0x34E2BA27, + 0xD6D1DE21, 0xEEC0B18D, 0xA6F30179, 0x9EE26ED5, 0x36946091, 0x0E850F3D, 0x46B6BFC9, 0x7EA7D065, + 0x13B6D5B0, 0x2BA7BA1C, 0x63940AE8, 0x5B856544, 0xF3F36B00, 0xCBE204AC, 0x83D1B458, 0xBBC0DBF4, + 0x425B0AA5, 0x7A4A6509, 0x3279D5FD, 0x0A68BA51, 0xA21EB415, 0x9A0FDBB9, 0xD23C6B4D, 0xEA2D04E1, + 0x873C0134, 0xBF2D6E98, 0xF71EDE6C, 0xCF0FB1C0, 0x6779BF84, 0x5F68D028, 0x175B60DC, 0x2F4A0F70, + 0xCD796B76, 0xF56804DA, 0xBD5BB42E, 0x854ADB82, 0x2D3CD5C6, 0x152DBA6A, 0x5D1E0A9E, 0x650F6532, + 0x081E60E7, 0x300F0F4B, 0x783CBFBF, 0x402DD013, 0xE85BDE57, 0xD04AB1FB, 0x9879010F, 0xA0686EA3}, + {0x00000000, 0xEF306B19, 0xDB8CA0C3, 0x34BCCBDA, 0xB2F53777, 0x5DC55C6E, 0x697997B4, 0x8649FCAD, + 0x6006181F, 0x8F367306, 0xBB8AB8DC, 0x54BAD3C5, 0xD2F32F68, 0x3DC34471, 0x097F8FAB, 0xE64FE4B2, + 0xC00C303E, 0x2F3C5B27, 0x1B8090FD, 0xF4B0FBE4, 0x72F90749, 0x9DC96C50, 0xA975A78A, 0x4645CC93, + 0xA00A2821, 0x4F3A4338, 0x7B8688E2, 0x94B6E3FB, 0x12FF1F56, 0xFDCF744F, 0xC973BF95, 0x2643D48C, + 0x85F4168D, 0x6AC47D94, 0x5E78B64E, 0xB148DD57, 0x370121FA, 0xD8314AE3, 0xEC8D8139, 0x03BDEA20, + 0xE5F20E92, 0x0AC2658B, 0x3E7EAE51, 0xD14EC548, 0x570739E5, 0xB83752FC, 0x8C8B9926, 0x63BBF23F, + 0x45F826B3, 0xAAC84DAA, 0x9E748670, 0x7144ED69, 0xF70D11C4, 0x183D7ADD, 0x2C81B107, 0xC3B1DA1E, + 0x25FE3EAC, 0xCACE55B5, 0xFE729E6F, 0x1142F576, 0x970B09DB, 0x783B62C2, 0x4C87A918, 0xA3B7C201, + 0x0E045BEB, 0xE13430F2, 0xD588FB28, 0x3AB89031, 0xBCF16C9C, 0x53C10785, 0x677DCC5F, 0x884DA746, + 0x6E0243F4, 0x813228ED, 0xB58EE337, 0x5ABE882E, 0xDCF77483, 0x33C71F9A, 0x077BD440, 0xE84BBF59, + 0xCE086BD5, 0x213800CC, 0x1584CB16, 0xFAB4A00F, 0x7CFD5CA2, 0x93CD37BB, 0xA771FC61, 0x48419778, + 0xAE0E73CA, 0x413E18D3, 0x7582D309, 0x9AB2B810, 0x1CFB44BD, 0xF3CB2FA4, 0xC777E47E, 0x28478F67, + 0x8BF04D66, 0x64C0267F, 0x507CEDA5, 0xBF4C86BC, 0x39057A11, 0xD6351108, 0xE289DAD2, 0x0DB9B1CB, + 0xEBF65579, 0x04C63E60, 0x307AF5BA, 0xDF4A9EA3, 0x5903620E, 0xB6330917, 0x828FC2CD, 0x6DBFA9D4, + 0x4BFC7D58, 0xA4CC1641, 0x9070DD9B, 0x7F40B682, 0xF9094A2F, 0x16392136, 0x2285EAEC, 0xCDB581F5, + 0x2BFA6547, 0xC4CA0E5E, 0xF076C584, 0x1F46AE9D, 0x990F5230, 0x763F3929, 0x4283F2F3, 0xADB399EA, + 0x1C08B7D6, 0xF338DCCF, 0xC7841715, 0x28B47C0C, 0xAEFD80A1, 0x41CDEBB8, 0x75712062, 0x9A414B7B, + 0x7C0EAFC9, 0x933EC4D0, 0xA7820F0A, 0x48B26413, 0xCEFB98BE, 0x21CBF3A7, 0x1577387D, 0xFA475364, + 0xDC0487E8, 0x3334ECF1, 0x0788272B, 0xE8B84C32, 0x6EF1B09F, 0x81C1DB86, 0xB57D105C, 0x5A4D7B45, + 0xBC029FF7, 0x5332F4EE, 0x678E3F34, 0x88BE542D, 0x0EF7A880, 0xE1C7C399, 0xD57B0843, 0x3A4B635A, + 0x99FCA15B, 0x76CCCA42, 0x42700198, 0xAD406A81, 0x2B09962C, 0xC439FD35, 0xF08536EF, 0x1FB55DF6, + 0xF9FAB944, 0x16CAD25D, 0x22761987, 0xCD46729E, 0x4B0F8E33, 0xA43FE52A, 0x90832EF0, 0x7FB345E9, + 0x59F09165, 0xB6C0FA7C, 0x827C31A6, 0x6D4C5ABF, 0xEB05A612, 0x0435CD0B, 0x308906D1, 0xDFB96DC8, + 0x39F6897A, 0xD6C6E263, 0xE27A29B9, 0x0D4A42A0, 0x8B03BE0D, 0x6433D514, 0x508F1ECE, 0xBFBF75D7, + 0x120CEC3D, 0xFD3C8724, 0xC9804CFE, 0x26B027E7, 0xA0F9DB4A, 0x4FC9B053, 0x7B757B89, 0x94451090, + 0x720AF422, 0x9D3A9F3B, 0xA98654E1, 0x46B63FF8, 0xC0FFC355, 0x2FCFA84C, 0x1B736396, 0xF443088F, + 0xD200DC03, 0x3D30B71A, 0x098C7CC0, 0xE6BC17D9, 0x60F5EB74, 0x8FC5806D, 0xBB794BB7, 0x544920AE, + 0xB206C41C, 0x5D36AF05, 0x698A64DF, 0x86BA0FC6, 0x00F3F36B, 0xEFC39872, 0xDB7F53A8, 0x344F38B1, + 0x97F8FAB0, 0x78C891A9, 0x4C745A73, 0xA344316A, 0x250DCDC7, 0xCA3DA6DE, 0xFE816D04, 0x11B1061D, + 0xF7FEE2AF, 0x18CE89B6, 0x2C72426C, 0xC3422975, 0x450BD5D8, 0xAA3BBEC1, 0x9E87751B, 0x71B71E02, + 0x57F4CA8E, 0xB8C4A197, 0x8C786A4D, 0x63480154, 0xE501FDF9, 0x0A3196E0, 0x3E8D5D3A, 0xD1BD3623, + 0x37F2D291, 0xD8C2B988, 0xEC7E7252, 0x034E194B, 0x8507E5E6, 0x6A378EFF, 0x5E8B4525, 0xB1BB2E3C}, + {0x00000000, 0x68032CC8, 0xD0065990, 0xB8057558, 0xA5E0C5D1, 0xCDE3E919, 0x75E69C41, 0x1DE5B089, + 0x4E2DFD53, 0x262ED19B, 0x9E2BA4C3, 0xF628880B, 0xEBCD3882, 0x83CE144A, 0x3BCB6112, 0x53C84DDA, + 0x9C5BFAA6, 0xF458D66E, 0x4C5DA336, 0x245E8FFE, 0x39BB3F77, 0x51B813BF, 0xE9BD66E7, 0x81BE4A2F, + 0xD27607F5, 0xBA752B3D, 0x02705E65, 0x6A7372AD, 0x7796C224, 0x1F95EEEC, 0xA7909BB4, 0xCF93B77C, + 0x3D5B83BD, 0x5558AF75, 0xED5DDA2D, 0x855EF6E5, 0x98BB466C, 0xF0B86AA4, 0x48BD1FFC, 0x20BE3334, + 0x73767EEE, 0x1B755226, 0xA370277E, 0xCB730BB6, 0xD696BB3F, 0xBE9597F7, 0x0690E2AF, 0x6E93CE67, + 0xA100791B, 0xC90355D3, 0x7106208B, 0x19050C43, 0x04E0BCCA, 0x6CE39002, 0xD4E6E55A, 0xBCE5C992, + 0xEF2D8448, 0x872EA880, 0x3F2BDDD8, 0x5728F110, 0x4ACD4199, 0x22CE6D51, 0x9ACB1809, 0xF2C834C1, + 0x7AB7077A, 0x12B42BB2, 0xAAB15EEA, 0xC2B27222, 0xDF57C2AB, 0xB754EE63, 0x0F519B3B, 0x6752B7F3, + 0x349AFA29, 0x5C99D6E1, 0xE49CA3B9, 0x8C9F8F71, 0x917A3FF8, 0xF9791330, 0x417C6668, 0x297F4AA0, + 0xE6ECFDDC, 0x8EEFD114, 0x36EAA44C, 0x5EE98884, 0x430C380D, 0x2B0F14C5, 0x930A619D, 0xFB094D55, + 0xA8C1008F, 0xC0C22C47, 0x78C7591F, 0x10C475D7, 0x0D21C55E, 0x6522E996, 0xDD279CCE, 0xB524B006, + 0x47EC84C7, 0x2FEFA80F, 0x97EADD57, 0xFFE9F19F, 0xE20C4116, 0x8A0F6DDE, 0x320A1886, 0x5A09344E, + 0x09C17994, 0x61C2555C, 0xD9C72004, 0xB1C40CCC, 0xAC21BC45, 0xC422908D, 0x7C27E5D5, 0x1424C91D, + 0xDBB77E61, 0xB3B452A9, 0x0BB127F1, 0x63B20B39, 0x7E57BBB0, 0x16549778, 0xAE51E220, 0xC652CEE8, + 0x959A8332, 0xFD99AFFA, 0x459CDAA2, 0x2D9FF66A, 0x307A46E3, 0x58796A2B, 0xE07C1F73, 0x887F33BB, + 0xF56E0EF4, 0x9D6D223C, 0x25685764, 0x4D6B7BAC, 0x508ECB25, 0x388DE7ED, 0x808892B5, 0xE88BBE7D, + 0xBB43F3A7, 0xD340DF6F, 0x6B45AA37, 0x034686FF, 0x1EA33676, 0x76A01ABE, 0xCEA56FE6, 0xA6A6432E, + 0x6935F452, 0x0136D89A, 0xB933ADC2, 0xD130810A, 0xCCD53183, 0xA4D61D4B, 0x1CD36813, 0x74D044DB, + 0x27180901, 0x4F1B25C9, 0xF71E5091, 0x9F1D7C59, 0x82F8CCD0, 0xEAFBE018, 0x52FE9540, 0x3AFDB988, + 0xC8358D49, 0xA036A181, 0x1833D4D9, 0x7030F811, 0x6DD54898, 0x05D66450, 0xBDD31108, 0xD5D03DC0, + 0x8618701A, 0xEE1B5CD2, 0x561E298A, 0x3E1D0542, 0x23F8B5CB, 0x4BFB9903, 0xF3FEEC5B, 0x9BFDC093, + 0x546E77EF, 0x3C6D5B27, 0x84682E7F, 0xEC6B02B7, 0xF18EB23E, 0x998D9EF6, 0x2188EBAE, 0x498BC766, + 0x1A438ABC, 0x7240A674, 0xCA45D32C, 0xA246FFE4, 0xBFA34F6D, 0xD7A063A5, 0x6FA516FD, 0x07A63A35, + 0x8FD9098E, 0xE7DA2546, 0x5FDF501E, 0x37DC7CD6, 0x2A39CC5F, 0x423AE097, 0xFA3F95CF, 0x923CB907, + 0xC1F4F4DD, 0xA9F7D815, 0x11F2AD4D, 0x79F18185, 0x6414310C, 0x0C171DC4, 0xB412689C, 0xDC114454, + 0x1382F328, 0x7B81DFE0, 0xC384AAB8, 0xAB878670, 0xB66236F9, 0xDE611A31, 0x66646F69, 0x0E6743A1, + 0x5DAF0E7B, 0x35AC22B3, 0x8DA957EB, 0xE5AA7B23, 0xF84FCBAA, 0x904CE762, 0x2849923A, 0x404ABEF2, + 0xB2828A33, 0xDA81A6FB, 0x6284D3A3, 0x0A87FF6B, 0x17624FE2, 0x7F61632A, 0xC7641672, 0xAF673ABA, + 0xFCAF7760, 0x94AC5BA8, 0x2CA92EF0, 0x44AA0238, 0x594FB2B1, 0x314C9E79, 0x8949EB21, 0xE14AC7E9, + 0x2ED97095, 0x46DA5C5D, 0xFEDF2905, 0x96DC05CD, 0x8B39B544, 0xE33A998C, 0x5B3FECD4, 0x333CC01C, + 0x60F48DC6, 0x08F7A10E, 0xB0F2D456, 0xD8F1F89E, 0xC5144817, 0xAD1764DF, 0x15121187, 0x7D113D4F}, + {0x00000000, 0x493C7D27, 0x9278FA4E, 0xDB448769, 0x211D826D, 0x6821FF4A, 0xB3657823, 0xFA590504, + 0x423B04DA, 0x0B0779FD, 0xD043FE94, 0x997F83B3, 0x632686B7, 0x2A1AFB90, 0xF15E7CF9, 0xB86201DE, + 0x847609B4, 0xCD4A7493, 0x160EF3FA, 0x5F328EDD, 0xA56B8BD9, 0xEC57F6FE, 0x37137197, 0x7E2F0CB0, + 0xC64D0D6E, 0x8F717049, 0x5435F720, 0x1D098A07, 0xE7508F03, 0xAE6CF224, 0x7528754D, 0x3C14086A, + 0x0D006599, 0x443C18BE, 0x9F789FD7, 0xD644E2F0, 0x2C1DE7F4, 0x65219AD3, 0xBE651DBA, 0xF759609D, + 0x4F3B6143, 0x06071C64, 0xDD439B0D, 0x947FE62A, 0x6E26E32E, 0x271A9E09, 0xFC5E1960, 0xB5626447, + 0x89766C2D, 0xC04A110A, 0x1B0E9663, 0x5232EB44, 0xA86BEE40, 0xE1579367, 0x3A13140E, 0x732F6929, + 0xCB4D68F7, 0x827115D0, 0x593592B9, 0x1009EF9E, 0xEA50EA9A, 0xA36C97BD, 0x782810D4, 0x31146DF3, + 0x1A00CB32, 0x533CB615, 0x8878317C, 0xC1444C5B, 0x3B1D495F, 0x72213478, 0xA965B311, 0xE059CE36, + 0x583BCFE8, 0x1107B2CF, 0xCA4335A6, 0x837F4881, 0x79264D85, 0x301A30A2, 0xEB5EB7CB, 0xA262CAEC, + 0x9E76C286, 0xD74ABFA1, 0x0C0E38C8, 0x453245EF, 0xBF6B40EB, 0xF6573DCC, 0x2D13BAA5, 0x642FC782, + 0xDC4DC65C, 0x9571BB7B, 0x4E353C12, 0x07094135, 0xFD504431, 0xB46C3916, 0x6F28BE7F, 0x2614C358, + 0x1700AEAB, 0x5E3CD38C, 0x857854E5, 0xCC4429C2, 0x361D2CC6, 0x7F2151E1, 0xA465D688, 0xED59ABAF, + 0x553BAA71, 0x1C07D756, 0xC743503F, 0x8E7F2D18, 0x7426281C, 0x3D1A553B, 0xE65ED252, 0xAF62AF75, + 0x9376A71F, 0xDA4ADA38, 0x010E5D51, 0x48322076, 0xB26B2572, 0xFB575855, 0x2013DF3C, 0x692FA21B, + 0xD14DA3C5, 0x9871DEE2, 0x4335598B, 0x0A0924AC, 0xF05021A8, 0xB96C5C8F, 0x6228DBE6, 0x2B14A6C1, + 0x34019664, 0x7D3DEB43, 0xA6796C2A, 0xEF45110D, 0x151C1409, 0x5C20692E, 0x8764EE47, 0xCE589360, + 0x763A92BE, 0x3F06EF99, 0xE44268F0, 0xAD7E15D7, 0x572710D3, 0x1E1B6DF4, 0xC55FEA9D, 0x8C6397BA, + 0xB0779FD0, 0xF94BE2F7, 0x220F659E, 0x6B3318B9, 0x916A1DBD, 0xD856609A, 0x0312E7F3, 0x4A2E9AD4, + 0xF24C9B0A, 0xBB70E62D, 0x60346144, 0x29081C63, 0xD3511967, 0x9A6D6440, 0x4129E329, 0x08159E0E, + 0x3901F3FD, 0x703D8EDA, 0xAB7909B3, 0xE2457494, 0x181C7190, 0x51200CB7, 0x8A648BDE, 0xC358F6F9, + 0x7B3AF727, 0x32068A00, 0xE9420D69, 0xA07E704E, 0x5A27754A, 0x131B086D, 0xC85F8F04, 0x8163F223, + 0xBD77FA49, 0xF44B876E, 0x2F0F0007, 0x66337D20, 0x9C6A7824, 0xD5560503, 0x0E12826A, 0x472EFF4D, + 0xFF4CFE93, 0xB67083B4, 0x6D3404DD, 0x240879FA, 0xDE517CFE, 0x976D01D9, 0x4C2986B0, 0x0515FB97, + 0x2E015D56, 0x673D2071, 0xBC79A718, 0xF545DA3F, 0x0F1CDF3B, 0x4620A21C, 0x9D642575, 0xD4585852, + 0x6C3A598C, 0x250624AB, 0xFE42A3C2, 0xB77EDEE5, 0x4D27DBE1, 0x041BA6C6, 0xDF5F21AF, 0x96635C88, + 0xAA7754E2, 0xE34B29C5, 0x380FAEAC, 0x7133D38B, 0x8B6AD68F, 0xC256ABA8, 0x19122CC1, 0x502E51E6, + 0xE84C5038, 0xA1702D1F, 0x7A34AA76, 0x3308D751, 0xC951D255, 0x806DAF72, 0x5B29281B, 0x1215553C, + 0x230138CF, 0x6A3D45E8, 0xB179C281, 0xF845BFA6, 0x021CBAA2, 0x4B20C785, 0x906440EC, 0xD9583DCB, + 0x613A3C15, 0x28064132, 0xF342C65B, 0xBA7EBB7C, 0x4027BE78, 0x091BC35F, 0xD25F4436, 0x9B633911, + 0xA777317B, 0xEE4B4C5C, 0x350FCB35, 0x7C33B612, 0x866AB316, 0xCF56CE31, 0x14124958, 0x5D2E347F, + 0xE54C35A1, 0xAC704886, 0x7734CFEF, 0x3E08B2C8, 0xC451B7CC, 0x8D6DCAEB, 0x56294D82, 0x1F1530A5}}; + +static inline __u32 rte_hash_crc_1byte(__u8 data, __u32 init_val) +{ + __u32 crc; + crc = init_val; + crc ^= data; + + return crc32c_tables[0][crc & 0xff] ^ (crc >> 8); +} + +static inline __u32 rte_hash_crc_2byte(__u16 data, __u32 init_val) +{ + __u32 crc; + crc = init_val; + crc ^= data; + + crc = CRC32_UPD(crc, 1) ^ (crc >> 16); + + return crc; +} + +static inline __u32 rte_hash_crc_4byte(__u32 data, __u32 init_val) +{ + __u32 crc, term1, term2; + crc = init_val; + crc ^= data; + + term1 = CRC32_UPD(crc, 3); + term2 = crc >> 16; + crc = term1 ^ CRC32_UPD(term2, 1); + + return crc; +} + +static inline __u32 rte_hash_crc_8byte(void *data, __u32 init_val) +{ + __u32 crc, term1, term2; + __u32 *__u32_ptr = (__u32 *)data; + + crc = init_val; + crc ^= __u32_ptr[0]; + + term1 = CRC32_UPD(crc, 7); + term2 = crc >> 16; + crc = term1 ^ CRC32_UPD(term2, 5); + term1 = CRC32_UPD(__u32_ptr[1], 3); + term2 = __u32_ptr[1] >> 16; + crc ^= term1 ^ CRC32_UPD(term2, 1); + + return crc; +} + +static inline __u32 rte_hash_crc(const void *data, __u32 data_len, __u32 init_val) +{ + unsigned i; + char *pd = (char *)data; + + for (i = 0; i < data_len / 8; i++) + { + init_val = rte_hash_crc_8byte(pd, init_val); + pd += 8; + } + + if (data_len & 0x4) + { + init_val = rte_hash_crc_4byte(*(const __u32 *)pd, init_val); + pd += 4; + } + + if (data_len & 0x2) + { + init_val = rte_hash_crc_2byte(*(const __u16 *)pd, init_val); + pd += 2; + } + + if (data_len & 0x1) + { + init_val = rte_hash_crc_1byte(*(const __u8 *)pd, init_val); + } + + return init_val; +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/cmake/FindLIBBPF.cmake b/cmake/FindLIBBPF.cmake new file mode 100644 index 0000000..e9a315a --- /dev/null +++ b/cmake/FindLIBBPF.cmake @@ -0,0 +1,8 @@ +include(FindPkgConfig) +pkg_check_modules(LIBBPF libbpf) + +if(LIBBPF_FOUND) + set(SUPPORT_BPF true) +else() + set(SUPPORT_BPF false) +endif() \ No newline at end of file diff --git a/common/include/kni_utils.h b/common/include/kni_utils.h index 52b3c87..5f74339 100644 --- a/common/include/kni_utils.h +++ b/common/include/kni_utils.h @@ -14,7 +14,7 @@ #include "MESA/field_stat2.h" #include "MESA/Maat_rule.h" #include "MESA/Maat_command.h" -#include "mrtunnat.h" +//#include "mrtunnat.h" #include #include #include @@ -184,9 +184,9 @@ enum kni_ipv6hdr_parse_error{ KNI_IPV6HDR_PARSE_ERROR_INVALID_TYPE = -3, }; - -enum kni_deploy_mode{ - KNI_DEPLOY_MODE_TUN = 0, +enum kni_deploy_mode +{ + KNI_DEPLOY_MODE_TAP = 0, KNI_DEPLOY_MODE_NORMAL = 1, }; diff --git a/conf/kni/kni.conf b/conf/kni/kni.conf index 802b659..577bedf 100644 --- a/conf/kni/kni.conf +++ b/conf/kni/kni.conf @@ -3,11 +3,25 @@ log_path = ./log/kni/kni.log log_level = 10 tfe_node_count = 3 manage_eth = enp6s0 +# deploy_mode: normal/tap deploy_mode = normal -tun_name = tun_kni src_mac_addr = 00:0e:c6:d6:72:c1 dst_mac_addr = fe:65:b7:03:50:bd +[tap] +tap_name=tap0 + +# 1.tap_allow_mutilthread=1 load bpf rss obj +# 2.tap_allow_mutilthread=0 not load bpf rss obj +tap_allow_mutilthread=1 +bpf_obj=/opt/tsg/sapp/plug/business/kni/bpf_tun_rss_steering.o +bpf_default_queue=-1 +# tap_bpf_debug_log: cat /sys/kernel/debug/tracing/trace_pipe +bpf_debug_log=0 +# 2: BPF 使用二元组分流 +# 4: BPF 使用四元组分流 +bpf_hash_mode=2 + [tfe0] enabled = 1 dev_eth_symbol = ens1f5 diff --git a/entry/CMakeLists.txt b/entry/CMakeLists.txt index d63c40b..f2883da 100644 --- a/entry/CMakeLists.txt +++ b/entry/CMakeLists.txt @@ -1,5 +1,11 @@ set(CMAKE_INSTALL_PREFIX /home/mesasoft/sapp_run) -add_library(kni SHARED src/kni_entry.cpp src/tfe_mgr.cpp src/kni_tun.cpp src/kni_pxy_tcp_option.cpp src/kni_dynamic_bypass.cpp) +add_library(kni SHARED src/kni_entry.cpp src/tfe_mgr.cpp src/kni_tap_rss.cpp src/kni_pxy_tcp_option.cpp src/kni_dynamic_bypass.cpp) target_include_directories(kni PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) -target_link_libraries(kni common MESA_prof_load MESA_htable MESA_field_stat maatframe mrzcpd dabloom) + +if (SUPPORT_BPF) + target_link_libraries(kni common MESA_prof_load MESA_htable MESA_field_stat maatframe mrzcpd dabloom bpf) +else() + target_link_libraries(kni common MESA_prof_load MESA_htable MESA_field_stat maatframe mrzcpd dabloom) +endif() + install(TARGETS kni LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/plug/business/kni COMPONENT LIBRARIES) diff --git a/entry/include/kni_entry.h b/entry/include/kni_entry.h index 1983caf..b5f9670 100644 --- a/entry/include/kni_entry.h +++ b/entry/include/kni_entry.h @@ -190,6 +190,9 @@ struct thread_tfe_cmsg_receiver_args{ struct per_thread_handle{ + int tap_fd; + int buff_size; + char *buff; MESA_htable_handle tuple2stream_htable; MESA_htable_handle traceid2sslinfo_htable; struct expiry_dablooms_handle *dabloom_handle; @@ -209,7 +212,7 @@ struct security_policy_shunt_tsg_diagnose{ struct kni_handle{ struct kni_marsio_handle *marsio_handle; - struct kni_tun_handle *tun_handle; + struct bpf_ctx *tap_bpf_ctx; struct kni_maat_handle *maat_handle; struct kni_send_logger *send_logger; MESA_htable_handle traceid2pme_htable; diff --git a/entry/include/kni_tap_rss.h b/entry/include/kni_tap_rss.h new file mode 100644 index 0000000..1421562 --- /dev/null +++ b/entry/include/kni_tap_rss.h @@ -0,0 +1,28 @@ +#ifndef _KNI_TAP_RSS_H_ +#define _KNI_TAP_RSS_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define TAP_RSS_LOG_TAG "TAP_RSS :" + +struct bpf_ctx; + +int kni_tap_get_bpf_prog_fd(struct bpf_ctx *ctx); + +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); +void kni_tap_global_unload_rss_bpf(struct bpf_ctx *ctx); + +int kni_tap_open_per_thread(const char *tap_dev, int tap_flags, int bpf_prog_fd, void *logger); +void kni_tap_close_per_thread(int tap_fd); + +int kni_tap_read_per_thread(int tap_fd, char *buff, int buff_size, void *logger); +int kni_tap_write_per_thread(int tap_fd, const char *data, int data_len, void *logger); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/entry/include/kni_tun.h b/entry/include/kni_tun.h deleted file mode 100644 index 612a489..0000000 --- a/entry/include/kni_tun.h +++ /dev/null @@ -1,10 +0,0 @@ -enum kni_tun_mode{ - KNI_TUN_MODE_BLOCK = 0, - KNI_TUN_MODE_NOBLOCK = 2, -}; - -struct kni_tun_handle; -void kni_tun_destroy(struct kni_tun_handle *tun_handle); -struct kni_tun_handle* kni_tun_init(char *tun_name, enum kni_tun_mode mode, void *logger); -int kni_tun_write(struct kni_tun_handle *handle, char *buff, uint16_t buff_len); -int kni_tun_read(struct kni_tun_handle *handle, char *buff, uint16_t buff_len); \ No newline at end of file diff --git a/entry/src/kni_entry.cpp b/entry/src/kni_entry.cpp index 96e86d0..b0d581b 100644 --- a/entry/src/kni_entry.cpp +++ b/entry/src/kni_entry.cpp @@ -27,7 +27,8 @@ extern "C" { #ifdef __cplusplus } #endif -#include "kni_tun.h" +#include "kni_tap_rss.h" +#include #include #include #include "kni_entry.h" @@ -805,28 +806,34 @@ static int send_to_tfe_normal_mode(char *raw_data, uint16_t raw_len, int thread_ return 0; } -static int send_to_tfe_tun_mode(char *raw_data, uint16_t raw_len, addr_type_t addr_type){ - struct kni_tun_handle *handle = g_kni_handle->tun_handle; - char *dst_data = ALLOC(char, KNI_MTU); - add_ether_header(dst_data, raw_data, raw_len, addr_type); - int ret = kni_tun_write(handle, dst_data, raw_len + sizeof(struct ethhdr)); - FREE(&dst_data); - if(ret < 0){ +static int send_to_tfe_tap_mode(char *raw_data, uint16_t raw_len, int thread_seq, addr_type_t addr_type, void *logger) +{ + int tap_fd = g_kni_handle->threads_handle[thread_seq].tap_fd; + char *send_buff = g_kni_handle->threads_handle[thread_seq].buff; + + add_ether_header(send_buff, raw_data, raw_len, addr_type); + int ret = kni_tap_write_per_thread(tap_fd, send_buff, raw_len + sizeof(struct ethhdr), logger); + if (ret < 0) + { return -1; } + return 0; } -static int send_to_tfe(char *raw_data, uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type){ +static int send_to_tfe(char *raw_data, uint16_t raw_len, int thread_seq, int tfe_id, addr_type_t addr_type, void *logger) +{ int mode = g_kni_handle->deploy_mode; - int ret; - if(mode == KNI_DEPLOY_MODE_TUN){ - ret = send_to_tfe_tun_mode(raw_data, raw_len, addr_type); + switch (mode) + { + case KNI_DEPLOY_MODE_TAP: + return send_to_tfe_tap_mode(raw_data, raw_len, thread_seq, addr_type, logger); + case KNI_DEPLOY_MODE_NORMAL: + return send_to_tfe_normal_mode(raw_data, raw_len, thread_seq, tfe_id, addr_type); + default: + assert(0); + return -1; } - else{ - ret = send_to_tfe_normal_mode(raw_data, raw_len, thread_seq, tfe_id, addr_type); - } - return ret; } static void wrapped_kni_header_parse(const void *a_packet, struct pme_info *pmeinfo, struct pkt_info *pktinfo){ @@ -1062,10 +1069,6 @@ static int dabloom_add(struct pkt_info *pktinfo, int thread_seq){ return ret; } - - - - static struct session_attribute_label * kni_pull_session_attribute_results(struct streaminfo *a_stream,struct pme_info *pmeinfo) { struct session_attribute_label *session_attribute = NULL; @@ -1148,7 +1151,7 @@ static struct session_attribute_label * kni_pull_session_attribute_results(struc }else{ KNI_LOG_DEBUG(logger, "share-session-attribute: destination ip location country is: %s,stream traceid = %s",session_attribute->server_location->country_full,pmeinfo->stream_traceid); } - if(session_attribute->server_location->province_full,pmeinfo->stream_traceid == NULL){ + if(session_attribute->server_location->province_full == NULL){ KNI_LOG_DEBUG(logger, "share-session-attribute: destination ip location province is NULL,stream traceid = %s",pmeinfo->stream_traceid); }else{ KNI_LOG_DEBUG(logger, "share-session-attribute: destination ip location province is: %s,stream traceid = %s",session_attribute->server_location->province_full,pmeinfo->stream_traceid); @@ -1438,7 +1441,7 @@ static int first_data_intercept(struct streaminfo *stream, struct pme_info *pmei //Note: traceid2pme_add_fail, still work. no cmsg traceid2pme_htable_add(pmeinfo); //send to tfe - ret = send_to_tfe(buff, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type); + ret = send_to_tfe(buff, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type, logger); if(ret < 0){ KNI_LOG_DEBUG(logger, "Intercept error: failed at send first packet to tfe%d, stream traceid = %s, stream addr = %s", pmeinfo->tfe_id, pmeinfo->stream_traceid, pmeinfo->stream_addr); @@ -1578,7 +1581,7 @@ char next_data_intercept(struct pme_info *pmeinfo, const void *a_packet, struct } } - ret = send_to_tfe((char*)a_packet, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type); + ret = send_to_tfe((char*)a_packet, len, thread_seq, pmeinfo->tfe_id, pmeinfo->addr_type, logger); if(ret < 0){ KNI_LOG_ERROR(logger, "Failed at send continue packet to tfe%d, stream traceid = %s, stream addr = %s", pmeinfo->tfe_id, pmeinfo->stream_traceid, pmeinfo->stream_addr); @@ -1878,7 +1881,6 @@ int tuple2stream_htable_search(MESA_htable_handle handle, struct ethhdr *ether_h tuple2stream_htable_value *value = NULL; struct pkt_info pktinfo; int reversed = 0, ret; - int key_size = 0; char key_str[KNI_ADDR_MAX]; struct stream_tuple4_v6 key_v6; struct stream_tuple4_v4 key_v4; @@ -1973,21 +1975,28 @@ extern "C" char kni_polling_all_entry(const struct streaminfo *stream, void** pm marsio_buff_free(g_kni_handle->marsio_handle->instance, rx_buffs, nr_recv, 0, 0); } } - //tun mode - else{ - char buff[KNI_MTU]; - int ret = kni_tun_read(g_kni_handle->tun_handle, buff, sizeof(buff)); - if(ret < 0){ - KNI_LOG_ERROR(logger, "Failed at read from tun"); - } - else{ - if(ret > 0){ - struct ethhdr *ether_hdr = (struct ethhdr*)buff; + else if (g_kni_handle->deploy_mode == KNI_DEPLOY_MODE_TAP) + { + int tap_fd = g_kni_handle->threads_handle[thread_seq].tap_fd; + char *recv_buff = g_kni_handle->threads_handle[thread_seq].buff; + int recv_buff_size = g_kni_handle->threads_handle[thread_seq].buff_size; + + // 只支持一个 TFE, 每次最多收 100 个包 + for (int j = 0; j < 100; j++) + { + if (kni_tap_read_per_thread(tap_fd, recv_buff, recv_buff_size, logger) > 0) + { + struct ethhdr *ether_hdr = (struct ethhdr *)recv_buff; tuple2stream_htable_search(tuple2stream_htable, ether_hdr, thread_seq); flag = POLLING_STATE_WORK; } + else + { + break; + } } } + return flag; } @@ -2433,12 +2442,26 @@ error_out: return NULL; } +extern "C" void kni_destroy() +{ + if (g_kni_handle != NULL) + { + for (int i = 0; i < g_kni_handle->thread_count; i++) + { + kni_tap_close_per_thread(g_kni_handle->threads_handle[i].tap_fd); + + if (g_kni_handle->threads_handle[i].buff) + { + free(g_kni_handle->threads_handle[i].buff); + g_kni_handle->threads_handle[i].buff = NULL; + } + } + + kni_tap_global_unload_rss_bpf(g_kni_handle->tap_bpf_ctx); -extern "C" void kni_destroy(){ - if(g_kni_handle != NULL){ FREE(&g_kni_handle); + g_kni_handle = NULL; } - g_kni_handle = NULL; } //eliminate_type: 0:FIFO; 1:LRU @@ -2514,7 +2537,7 @@ extern "C" int kni_init(){ char *kni_git_verison = (char*)KNI_GIT_VERSION; const char *profile = "./etc/kni/kni.conf"; const char *section = "global"; - + char tap_name[1024] = {0}; //init logger char log_path[KNI_PATH_MAX] = ""; int tfe_node_count = 1; @@ -2551,10 +2574,20 @@ extern "C" int kni_init(){ char deploy_mode[KNI_SYMBOL_MAX]; ret = MESA_load_profile_string_def(profile, section, "deploy_mode", deploy_mode, sizeof(deploy_mode), "normal"); - g_kni_handle->deploy_mode = KNI_DEPLOY_MODE_NORMAL; - if(strcmp(deploy_mode, "tun") == 0){ - g_kni_handle->deploy_mode = KNI_DEPLOY_MODE_TUN; + if (strcmp(deploy_mode, "tap") == 0) + { + g_kni_handle->deploy_mode = KNI_DEPLOY_MODE_TAP; } + else if (strcmp(deploy_mode, "normal") == 0) + { + g_kni_handle->deploy_mode = KNI_DEPLOY_MODE_NORMAL; + } + else + { + KNI_LOG_ERROR(local_logger, "KNI Invalid deploy_mode: %s\n", deploy_mode); + goto error_out; + } + if(g_kni_handle->deploy_mode == KNI_DEPLOY_MODE_NORMAL){ ret = MESA_load_profile_int_nodef(profile, section, "tfe_node_count", &tfe_node_count); if(ret < 0){ @@ -2622,27 +2655,54 @@ extern "C" int kni_init(){ goto error_out; } } + else if (g_kni_handle->deploy_mode == KNI_DEPLOY_MODE_TAP) + { + if (tfe_node_count != 1) + { + KNI_LOG_ERROR(local_logger, TAP_RSS_LOG_TAG "under tap mode, only support one tfe node."); + goto error_out; + } - //init tun - if(g_kni_handle->deploy_mode == KNI_DEPLOY_MODE_TUN){ - if(g_kni_handle->thread_count != 1){ - KNI_LOG_ERROR(local_logger, "Tun mode, thread count must be 1, while it's %d", g_kni_handle->thread_count); + if (MESA_load_profile_string_nodef(profile, "tap", "tap_name", tap_name, sizeof(tap_name)) < 0) + { + KNI_LOG_ERROR(local_logger, TAP_RSS_LOG_TAG "under tap mode, tap_name is required."); goto error_out; } - char tun_name[KNI_SYMBOL_MAX]; - ret = MESA_load_profile_string_nodef(profile, section, "tun_name", tun_name, sizeof(tun_name)); - if(ret < 0){ - KNI_LOG_ERROR(local_logger, "MESA_prof_load: tun_name not set, profile = %s, section = %s", profile, section); - goto error_out; + + int tap_allow_mutilthread = 0; + MESA_load_profile_int_nodef(profile, "tap", "tap_allow_mutilthread", &tap_allow_mutilthread); + if (tap_allow_mutilthread) + { + uint32_t bpf_debug_log = 0; + uint32_t bpf_hash_mode = 2; + uint32_t bpf_queue_num = get_thread_count(); + char bpf_obj[1024] = {0}; + MESA_load_profile_int_nodef(profile, "tap", "bpf_debug_log", (int *)&bpf_debug_log); + MESA_load_profile_int_nodef(profile, "tap", "bpf_hash_mode", (int *)&bpf_hash_mode); + ret = MESA_load_profile_string_nodef(profile, "tap", "bpf_obj", bpf_obj, sizeof(bpf_obj)); + if (ret < 0) + { + KNI_LOG_ERROR(local_logger, TAP_RSS_LOG_TAG "under tap mode, when enable tap_allow_mutilthread, bpf_obj is required."); + goto error_out; + } + + g_kni_handle->tap_bpf_ctx = kni_tap_global_load_rss_bpf(bpf_obj, bpf_queue_num, bpf_hash_mode, bpf_debug_log, local_logger); + if (g_kni_handle->tap_bpf_ctx == NULL) + { + goto error_out; + } } - KNI_LOG_ERROR(local_logger, "MESA_prof_load, [%s]:\n tun_name: %s", section, tun_name); - g_kni_handle->tun_handle = kni_tun_init(tun_name, KNI_TUN_MODE_NOBLOCK, local_logger); - if(g_kni_handle->tun_handle == NULL){ - KNI_LOG_ERROR(local_logger, "Failed at init kni_tun"); - goto error_out; + else + { + if (get_thread_count() > 1) + { + KNI_LOG_ERROR(local_logger, TAP_RSS_LOG_TAG "under tap mode, when disable tap_allow_mutilthread, only support one work thread."); + goto error_out; + } } } - + + //init_filedstat fs_handle = fs_init(profile); if(fs_handle == NULL){ @@ -2673,7 +2733,17 @@ extern "C" int kni_init(){ goto error_out; } g_kni_handle->threads_handle[i].tuple2stream_htable = tuple2stream_htable; - } + if (g_kni_handle->deploy_mode == KNI_DEPLOY_MODE_TAP) + { + g_kni_handle->threads_handle[i].tap_fd = kni_tap_open_per_thread(tap_name, IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE, kni_tap_get_bpf_prog_fd(g_kni_handle->tap_bpf_ctx), local_logger); + if (g_kni_handle->threads_handle[i].tap_fd == -1) + { + goto error_out; + } + g_kni_handle->threads_handle[i].buff_size = KNI_MTU; + g_kni_handle->threads_handle[i].buff = ALLOC(char, g_kni_handle->threads_handle[i].buff_size); + } + } //init ssl dynamic bypass htable ret = ssl_dynamic_bypass_htable_init(profile, local_logger); if(ret < 0){ diff --git a/entry/src/kni_tap_rss.cpp b/entry/src/kni_tap_rss.cpp new file mode 100644 index 0000000..667049c --- /dev/null +++ b/entry/src/kni_tap_rss.cpp @@ -0,0 +1,267 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if (SUPPORT_BPF) +#include "../../bpf/bpf_conf_user.h" +#include +#include +#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\n", 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\n"); + 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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\n", tap_dev, strerror(errno)); + goto error; + } + } + + KNI_LOG_INFO(logger, TAP_RSS_LOG_TAG "using tap device %s with MTU %d\n", 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\n", 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\n", data_len, ret, strerror(errno)); + } + + return ret; +} \ No newline at end of file diff --git a/entry/src/kni_tun.cpp b/entry/src/kni_tun.cpp deleted file mode 100644 index 284b157..0000000 --- a/entry/src/kni_tun.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "kni_utils.h" -#include -#include -#include -#include -#include - -struct kni_tun_handle{ - int fd; - void *logger; - enum kni_tun_mode mode; -}; - -void kni_tun_destroy(struct kni_tun_handle *tun_handle){ - if(tun_handle != NULL){ - close(tun_handle->fd); - FREE(&tun_handle); - } -} - -struct kni_tun_handle* kni_tun_init(char *tun_name, enum kni_tun_mode mode, void *logger){ - struct kni_tun_handle *tun_handle = ALLOC(struct kni_tun_handle, 1); - tun_handle->logger = logger; - char *tun_path = (char*)"/dev/net/tun"; - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - int fd = -1, ret; - ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE; - if(tun_name == NULL || *tun_name == '\0'){ - KNI_LOG_ERROR(logger, "Tap device name is NULL"); - goto error_out; - } - strncpy(ifr.ifr_name, tun_name, IFNAMSIZ); - fd = open(tun_path, O_RDWR); - if(fd < 0){ - KNI_LOG_ERROR(logger, "Failed at open file, filename = %s, errno = %d(%s)", tun_path, errno, strerror(errno)); - goto error_out; - } - tun_handle->fd = fd; - tun_handle->mode = KNI_TUN_MODE_NOBLOCK; - if(tun_handle->mode == KNI_TUN_MODE_NOBLOCK){ - int flags = fcntl(fd, F_GETFL, 0); - ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); - if(ret < 0){ - KNI_LOG_ERROR(logger ,"Failed at fcntl, fd = %d, flags = F_GETFL | O_NONBLOCK, errno = %d(%s)", errno, strerror(errno)); - goto error_out; - } - } - ret = ioctl(tun_handle->fd, TUNSETIFF, (void *)&ifr); - if(ret < 0){ - KNI_LOG_ERROR(logger ,"Failed at ioctl, fd = %d, errno = %d(%s)", errno, strerror(errno)); - goto error_out; - } - return tun_handle; - -error_out: - kni_tun_destroy(tun_handle); - return NULL; -} - - -int kni_tun_write(struct kni_tun_handle *handle, char *buff, uint16_t buff_len){ - uint16_t ret = write(handle->fd, buff, buff_len); - if(ret < 0){ - KNI_LOG_ERROR(handle->logger, "Failed at write, errno = %d(%s)", errno, strerror(errno)); - return -1; - } - if(ret < buff_len){ - KNI_LOG_ERROR(handle->logger, "Failed at write: need send %dB, only send %dB", buff_len, ret); - return -1; - } - return 0; -} - -/* -* >= 0 : read data length -* = -1 : error -*/ -int kni_tun_read(struct kni_tun_handle *handle, char *buff, uint16_t buff_len){ - int ret = 0; - ret = read(handle->fd, buff, buff_len); - if(ret < 0){ - if(ret == -1 && handle->mode == KNI_TUN_MODE_NOBLOCK){ - if(errno == EWOULDBLOCK || errno == EAGAIN){ - return 0; - } - } - KNI_LOG_ERROR(handle->logger, "Failed at read, error = %d(%s)", errno, strerror(errno)); - } - return ret; -} \ No newline at end of file