Skip to content

Conversation

@lec-bit
Copy link
Contributor

@lec-bit lec-bit commented Nov 3, 2025

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign nlgwcy for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link

Summary of Changes

Hello @lec-bit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the Kmesh eBPF and kernel module components to enhance compatibility with newer Linux kernels, particularly version 6.x. The core change involves integrating BPF kfuncs for key operations like header parsing and socket option handling, which allows eBPF programs to directly call kernel functions. This adaptation simplifies the codebase by removing older, kernel-specific conditional logic, such as that for openEuler-23.03, and introduces a more robust mechanism for kernel feature detection. The changes also include updates to generated eBPF Go bindings and the Kmesh kernel module's build and initialization processes to support this new kfunc-based architecture.

Highlights

  • BPF kfunc Integration: Introduced BPF kfuncs for header parsing and socket option manipulation, enabling direct kernel function calls from eBPF programs on compatible kernels (Linux 6.x and above with BTF support). This includes new kfunc declarations and wrapper functions like bpf_km_setsockopt and bpf_km_getsockopt.
  • Kernel Version Adaptation: Removed specific compatibility layers related to openEuler-23.03 (OE_23_03 macro and associated conditional code), streamlining the codebase for broader and more generalized kernel compatibility.
  • eBPF Program Generation Updates: Updated bpf2go generated Go files to reflect changes in eBPF map and program definitions. This includes the addition of new kmeshcgroupsock related files for enhanced kernel support and the removal of rate-limiting maps in favor of a KmRouterconfig map in normal kernel builds.
  • Kernel Module Refactoring: Refactored the Kmesh kernel module to initialize and utilize the new kfunc infrastructure. This involved making previously static helper functions globally accessible or exposed as kfuncs, and integrating kmesh_func_init/exit calls into the module's lifecycle.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


In kernel's heart, new kfuncs bloom, Old paths now fade, dispelling gloom. With BTF's light, a clearer way, Code adapts, for a brighter day.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adapts the codebase for Linux kernel 6.6, primarily by introducing kfunc support and removing legacy code for openEuler 23.03. The changes are extensive, touching BPF code, kernel modules, and build scripts. While the direction is correct, I've found several critical issues, including merge conflicts in a generated file, incorrect kfunc signatures and implementations that could lead to memory safety issues or kernel instability, and some less severe maintainability problems. Please address the critical issues before merging.

#if ENHANCED_KERNEL
#if KERNEL_KFUNC
extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym;
extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The signature for bpf_km_header_strnstr_func is missing the subptr__sz parameter. This causes the kfunc implementation to use a hardcoded and likely incorrect size, which can lead to memory safety issues. The signature should include int subptr__sz.

extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr, int subptr__sz) __ksym;

int bpf_km_header_strnstr(void *ctx, const char *key, int key__sz, const char *subptr, int subptr__sz)
{
struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The call to bpf_km_header_strnstr_func is missing the subptr__sz argument, which is required for correctness and memory safety. This is related to the incorrect extern declaration on line 28.

    return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr, subptr__sz);

Comment on lines 96 to 99
<<<<<<< HEAD
=======
KmConfigmap *ebpf.MapSpec `ebpf:"km_configmap"`
>>>>>>> 3bfda9b3 (adapt 6.6)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This generated file contains unresolved merge conflict markers (e.g., here and at lines 139, 157). This will cause the build to fail. Please resolve the conflicts, likely by regenerating the file.

Comment on lines 139 to 138
<<<<<<< HEAD
=======
KmConfigmap *ebpf.Map `ebpf:"km_configmap"`
>>>>>>> 3bfda9b3 (adapt 6.6)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This file contains unresolved merge conflict markers, which will cause the build to fail. Please resolve the conflict.

Comment on lines 157 to 156
<<<<<<< HEAD
=======
m.KmConfigmap,
>>>>>>> 3bfda9b3 (adapt 6.6)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This file contains unresolved merge conflict markers, which will cause the build to fail. Please resolve the conflict.

Comment on lines +17 to +22
__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
{
struct bpf_sock_addr_kern *sa_kern = ctx;
int subptr__sz = 5;
return bpf_km_header_strnstr_impl(ctx, key, key__sz, subptr, subptr__sz);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This kfunc implementation is incorrect and doesn't match the intended usage. It's missing the subptr__sz parameter, hardcodes a value for it, and incorrectly handles the ctx pointer. The ctx argument is a pointer to bpf_mem_ptr and needs to be dereferenced to get the actual context. The sa_kern variable is also unused.

__bpf_kfunc int bpf_km_header_strnstr_func(void *bpf_mem, int bpf_mem__sz, const char *key, int key__sz, const char *subptr, int subptr__sz)
{
    struct bpf_mem_ptr *tmp = bpf_mem;
    struct bpf_sock_addr_kern *ctx = tmp->ptr;
    return bpf_km_header_strnstr_impl(ctx, key, key__sz, subptr, subptr__sz);
}

Comment on lines 50 to 82
__bpf_kfunc int bpf_getsockopt_func(void *bpf_mem, int bpf_socket__sz, int optname, char *opt, int opt__sz)
{
struct bpf_mem_ptr *tmp = bpf_mem;
struct bpf_sock_addr_kern *ctx = tmp->ptr;
struct sock *sk = ctx->sk;

struct inet_connection_sock *icsk = inet_csk(sk);
struct tcp_sock *tp = tcp_sk(sk);
struct net *net = sock_net(sk);
int val, len;

sockptr_t optval = KERNEL_SOCKPTR(opt);
sockptr_t optlen = KERNEL_SOCKPTR(&opt__sz);

if (copy_from_sockptr(&len, optlen, sizeof(int)))
return -EFAULT;

if (len < 0)
return -EINVAL;

len = min_t(unsigned int, len, TCP_ULP_NAME_MAX);
if (!icsk->icsk_ulp_ops) {
len = 0;
if (copy_to_sockptr(optlen, &len, sizeof(int)))
return -EFAULT;
return -EINVAL;
}
if (copy_to_sockptr(optlen, &len, sizeof(int)))
return -EFAULT;
if (copy_to_sockptr(optval, icsk->icsk_ulp_ops->name, len))
return -EFAULT;
return 0;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This kfunc has several issues:

  1. The variables tp, net, and val are declared but not used, which will cause compiler warnings.
  2. The handling of opt__sz is incorrect and unsafe. opt__sz is an integer passed by value. KERNEL_SOCKPTR(&opt__sz) creates a sockptr to a kernel stack variable, and copy_from_sockptr is not meant for this. This can lead to undefined behavior or kernel crashes.

To correctly handle the output length, the kfunc signature should be changed to accept a pointer int *opt__sz, and the call site in bpf/include/common.h should be updated to pass a pointer to the length variable.

Comment on lines +49 to +58
int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz)
{
const char kmesh_module_name[] = "kmesh_defer";
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name))
return -1;

struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)};
return bpf_setsockopt_func(
&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_name, sizeof(kmesh_module_name));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The function bpf_km_setsockopt has a generic name, but its implementation is highly specific to setting the TCP_ULP option to "kmesh_defer". This is misleading and harms maintainability. Consider renaming the function to reflect its specific purpose, for example, bpf_km_set_tcp_ulp_kmesh_defer.

@hzxuzhonghu hzxuzhonghu requested a review from Copilot November 3, 2025 11:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the kernel compatibility layer to support kernel function (kfunc) based BPF operations for kernel 6.x+. It removes deprecated OpenEuler 23.03-specific code and introduces a new approach for handling kernel functions based on BTF/kfunc availability.

  • Removes OE_23_03 macro and related version-specific code paths
  • Introduces KERNEL_KFUNC macro to conditionally use kfunc-based implementations vs function pointer dispatch
  • Adds new kmesh_func.c module that registers BPF kernel functions when kfunc is available
  • Updates kernel configuration detection to check for BTF support (CONFIG_DEBUG_INFO_BTF_MODULES and CONFIG_DEBUG_INFO_BTF)

Reviewed Changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
kernel/ko_src/kmesh/kmesh_func.c New file implementing kernel functions as BPF kfuncs or legacy function pointers based on KERNEL_KFUNC macro
kernel/ko_src/kmesh/kmesh_func.h New header declaring init/exit functions for the kmesh function module
kernel/ko_src/kmesh/defer_connect.c Refactored to use conditional macros for defer_connect operations based on KERNEL_KFUNC support
kmesh_macros_env_kernel.sh Added BTF config detection and KERNEL_KFUNC configuration logic
config/kmesh_marcos_def.h Removed OE_23_03 macro, added KERNEL_KFUNC macro definition
bpf/include/common.h Added kfunc declarations and wrapper functions for BPF operations
oncn-mda/include/mesh_accelerate.h Removed OE_23_03 conditional compilation, standardized port handling
bpf/kmesh/bpf2go/kernelnative/enhanced/kmeshsockopscompat_bpfel.go Contains unresolved merge conflict markers
Multiple generated .go files Regenerated BPF program bindings with updated map/program structures

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 96 to 99
<<<<<<< HEAD
=======
KmConfigmap *ebpf.MapSpec `ebpf:"km_configmap"`
>>>>>>> 3bfda9b3 (adapt 6.6)
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolved merge conflict markers present in the generated code. These conflict markers must be resolved before the code can compile.

Suggested change
<<<<<<< HEAD
=======
KmConfigmap *ebpf.MapSpec `ebpf:"km_configmap"`
>>>>>>> 3bfda9b3 (adapt 6.6)
KmConfigmap *ebpf.MapSpec `ebpf:"km_configmap"`

Copilot uses AI. Check for mistakes.
Comment on lines 139 to 138
<<<<<<< HEAD
=======
KmConfigmap *ebpf.Map `ebpf:"km_configmap"`
>>>>>>> 3bfda9b3 (adapt 6.6)
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolved merge conflict markers present in the generated code. These conflict markers must be resolved before the code can compile.

Copilot uses AI. Check for mistakes.
Comment on lines 157 to 156
<<<<<<< HEAD
=======
m.KmConfigmap,
>>>>>>> 3bfda9b3 (adapt 6.6)
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unresolved merge conflict markers present in the generated code. These conflict markers must be resolved before the code can compile.

Copilot uses AI. Check for mistakes.
Comment on lines +17 to +20
__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
{
struct bpf_sock_addr_kern *sa_kern = ctx;
int subptr__sz = 5;
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable 'sa_kern' is declared but never used. The hardcoded value '5' for 'subptr__sz' should be explained with a comment or defined as a named constant to clarify its purpose.

Suggested change
__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
{
struct bpf_sock_addr_kern *sa_kern = ctx;
int subptr__sz = 5;
// Length of the substring to search for in header parsing
#define SUBPTR_SZ 5
__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
{
int subptr__sz = SUBPTR_SZ;

Copilot uses AI. Check for mistakes.
struct bpf_mem_ptr *tmp = bpf_mem;
struct bpf_sock_addr_kern *ctx = tmp->ptr;
struct sock *sk = ctx->sk;
int ret = 0;
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable 'ret' is initialized but never used before being overwritten by the return statement. Remove this unused initialization.

Suggested change
int ret = 0;

Copilot uses AI. Check for mistakes.
Comment on lines 57 to 59
struct tcp_sock *tp = tcp_sk(sk);
struct net *net = sock_net(sk);
int val, len;
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables 'tp', 'net', and 'val' are declared but never used. Remove these unused variable declarations.

Suggested change
struct tcp_sock *tp = tcp_sk(sk);
struct net *net = sock_net(sk);
int val, len;
int len;

Copilot uses AI. Check for mistakes.
if [ "$(check_config "CONFIG_DEBUG_INFO_BTF_MODULES")" == "y" ] && \
[ "$(check_config "CONFIG_DEBUG_INFO_BTF")" == "y" ] && \
[ "$VERSION" -ge 6 ]; then
set_config ENHANCED_KERNEL 1
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic unconditionally sets ENHANCED_KERNEL to 1 when KERNEL_KFUNC is enabled, potentially overriding the earlier ENHANCED_KERNEL detection at line 62-66 which checks for 'FN(parse_header_msg)'. This could cause issues if BTF is available but the parse_header_msg function is not. Consider checking the existing ENHANCED_KERNEL value or adjusting the logic.

Suggested change
set_config ENHANCED_KERNEL 1
# Only set ENHANCED_KERNEL to 1 if not already set to 1
if ! grep -qE "ENHANCED_KERNEL[ \t]*1" config/kmesh_marcos_def.h; then
set_config ENHANCED_KERNEL 1
fi

Copilot uses AI. Check for mistakes.
Comment on lines 59 to 64
if [ "$VERSION" -ge 6 ]; then
set_config ENHANCED_KERNEL 1
set_config KERNEL_KFUNC 1
else
set_config KERNEL_KFUNC 0
fi
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the kernel version check from kmesh_macros_env_kernel.sh but lacks the BTF configuration checks. The logic should be consistent between both scripts, or the KERNEL_KFUNC configuration should only be set in one location to avoid conflicts.

Copilot uses AI. Check for mistakes.
@lec-bit lec-bit force-pushed the new-kernel-6.6-1103 branch 5 times, most recently from 743d680 to eba42b6 Compare November 7, 2025 03:41
@kmesh-bot kmesh-bot added size/L and removed size/XXL labels Nov 7, 2025
@lec-bit lec-bit force-pushed the new-kernel-6.6-1103 branch from eba42b6 to 9f824eb Compare November 7, 2025 06:51
Signed-off-by: lec-bit <glfhzmy@126.com>
@lec-bit lec-bit force-pushed the new-kernel-6.6-1103 branch from 9f824eb to 3574d0e Compare November 7, 2025 07:24
@codecov
Copy link

codecov bot commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.81%. Comparing base (93b6b85) to head (afbc03c).
⚠️ Report is 14 commits behind head on main.
see 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 44b2525...afbc03c. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: lec-bit <glfhzmy@126.com>
@lec-bit lec-bit force-pushed the new-kernel-6.6-1103 branch from 841c802 to afbc03c Compare November 10, 2025 09:00
};

#if ENHANCED_KERNEL
#if KERNEL_KFUNC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract a kfunc.h

int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz)
{
const char kmesh_module_name[] = "kmesh_defer";
if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need check for this function


int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz)
{
if (level != IPPROTO_TCP) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need check for this function


#else
#include <bpf_helper_defs_ext.h>
#define bpf_km_setsockopt bpf_setsockopt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls define macros in bpf_helper_defs_ext.h

#define KMESH_DELAY_ERROR -1000

#define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, t_ctx) \
#ifdef KERNEL_KFUNC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a difference introduced by the kernel version. It is inappropriate to use KERNEL_KFUNC to distinguish this difference.

ubase = iov->iov_base;
kbuf_size = iov->iov_len;
} else if (iter_is_iovec(&msg->msg_iter)) {
#ifdef KERNEL_KFUNC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the previous comment.

})

#define SET_FDEFER_CONNECT_ON(sk) (inet_sk(sk)->defer_connect = 1)
#define SET_FDEFER_CONNECT_OFF(sk) (inet_sk(sk)->defer_connect = 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SET_DEFER_CONNECT_ON
SET_DEFER_CONNECT_OFF

__bpf_kfunc int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr)
{
struct bpf_sock_addr_kern *sa_kern = ctx;
int subptr__sz = 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why set subptr__sz 5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants