-
Notifications
You must be signed in to change notification settings - Fork 132
adapt 6.6 #1520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
adapt 6.6 #1520
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #define _COMMON_H_ | ||
|
|
||
| #include "../../config/kmesh_marcos_def.h" | ||
| #include <linux/in.h> | ||
| #include <stddef.h> | ||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
|
|
@@ -16,8 +17,60 @@ | |
|
|
||
| #include "errno.h" | ||
|
|
||
| struct bpf_mem_ptr { | ||
| void *ptr; | ||
| __u32 size; | ||
| }; | ||
|
|
||
| #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; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature for 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; |
||
| extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym; | ||
| extern int bpf_setsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, const char *optval, int optval__sz) __ksym; | ||
| extern int bpf_getsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, char *optval, int optval__sz) __ksym; | ||
|
|
||
| #define bpf_km_header_strncmp bpf_km_header_strncmp_func | ||
|
|
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| int bpf_parse_header_msg(struct bpf_sock_addr *ctx) | ||
| { | ||
| struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; | ||
| return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr)); | ||
| } | ||
|
|
||
| // Due to the limitation of bpf verifier, optval and optval__sz are required to correspond. | ||
| // The strnlen function cannot be used here, so the string is redefined. | ||
| 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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need check for this function |
||
| 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)); | ||
| } | ||
|
Comment on lines
+49
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz) | ||
| { | ||
| if (level != IPPROTO_TCP) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need check for this function |
||
| return -1; | ||
| } | ||
| struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; | ||
| return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz); | ||
| } | ||
|
|
||
| #else | ||
| #include <bpf_helper_defs_ext.h> | ||
| #define bpf_km_setsockopt bpf_setsockopt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls define macros in |
||
| #define bpf_km_getsockopt bpf_getsockopt | ||
| #endif | ||
| #endif | ||
|
|
||
| #define bpf_unused __attribute__((__unused__)) | ||
|
|
@@ -113,14 +166,8 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4]) | |
| (dst)[3] = (src)[3]; \ | ||
| } while (0) | ||
|
|
||
| #if OE_23_03 | ||
| #define bpf__strncmp bpf_strncmp | ||
| #define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port) | ||
| #else | ||
| #define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16) | ||
| #endif | ||
|
|
||
| #define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port) | ||
| #define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port) | ||
|
|
||
| #define MAX_BUF_LEN 100 | ||
| #define MAX_IP4_LEN 16 | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
| /* Copyright Authors of Kmesh */ | ||
|
|
||
| #include "../../../config/kmesh_marcos_def.h" | ||
| #include <linux/init.h> | ||
| #include <linux/module.h> | ||
| #include <linux/kmod.h> | ||
|
|
@@ -21,9 +20,24 @@ | |
| #include "defer_connect.h" | ||
|
|
||
| static struct proto *kmesh_defer_proto = NULL; | ||
| #define KMESH_DELAY_ERROR -1000 | ||
|
|
||
| #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, t_ctx) \ | ||
| #ifdef KERNEL_KFUNC | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \ | ||
| ({ \ | ||
| int __ret = -1; \ | ||
| if (t_ctx == NULL) { \ | ||
| __ret = -EINVAL; \ | ||
| } else { \ | ||
| __ret = __cgroup_bpf_run_filter_sock_addr(sk, uaddr, uaddrlen, CGROUP_INET4_CONNECT, t_ctx, NULL); \ | ||
| } \ | ||
| __ret; \ | ||
| }) | ||
|
|
||
| #define SET_FDEFER_CONNECT_ON(sk) (inet_set_bit(DEFER_CONNECT, sk)) | ||
| #define SET_FDEFER_CONNECT_OFF(sk) (inet_clear_bit(DEFER_CONNECT, sk)) | ||
| #define IS_DEFER_CONNECT(sk) (inet_test_bit(DEFER_CONNECT, sk)) | ||
| #else | ||
| #define BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, uaddr, uaddrlen, t_ctx) \ | ||
| ({ \ | ||
| int __ret = -1; \ | ||
| if (t_ctx == NULL) { \ | ||
|
|
@@ -34,6 +48,11 @@ static struct proto *kmesh_defer_proto = NULL; | |
| __ret; \ | ||
| }) | ||
|
|
||
| #define SET_FDEFER_CONNECT_ON(sk) (inet_sk(sk)->defer_connect = 1) | ||
| #define SET_FDEFER_CONNECT_OFF(sk) (inet_sk(sk)->defer_connect = 0) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SET_DEFER_CONNECT_ON |
||
| #define IS_DEFER_CONNECT(sk) (inet_sk(sk)->defer_connect == 1) | ||
| #endif | ||
|
|
||
| static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size) | ||
| { | ||
| struct bpf_mem_ptr tmpMem = {0}; | ||
|
|
@@ -43,6 +62,7 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size) | |
| const struct iovec *iov; | ||
| struct bpf_sock_addr_kern sock_addr; | ||
| struct sockaddr_in uaddr; | ||
| int uaddrlen = sizeof(struct sockaddr_in); | ||
| void __user *ubase; | ||
| int err; | ||
| u32 dport, daddr; | ||
|
|
@@ -54,7 +74,11 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size) | |
| ubase = iov->iov_base; | ||
| kbuf_size = iov->iov_len; | ||
| } else if (iter_is_iovec(&msg->msg_iter)) { | ||
| #ifdef KERNEL_KFUNC | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the previous comment. |
||
| iov = msg->msg_iter.__iov; | ||
| #else | ||
| iov = msg->msg_iter.iov; | ||
| #endif | ||
| ubase = iov->iov_base; | ||
| kbuf_size = iov->iov_len; | ||
| #if ITER_TYPE_IS_UBUF | ||
|
|
@@ -79,31 +103,11 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size) | |
| tmpMem.size = kbuf_size; | ||
| tmpMem.ptr = kbuf; | ||
|
|
||
| #if OE_23_03 | ||
| tcp_call_bpf_3arg( | ||
| sk, | ||
| BPF_SOCK_OPS_TCP_DEFER_CONNECT_CB, | ||
| ((u64)(&tmpMem) & U32_MAX), | ||
| (((u64)(&tmpMem) >> 32) & U32_MAX), | ||
| kbuf_size); | ||
| daddr = sk->sk_daddr; | ||
| dport = sk->sk_dport; | ||
|
|
||
| // daddr == 0 && dport == 0 are special flags meaning the circuit breaker is open | ||
| // Should reject connection here | ||
| if (daddr == 0 && dport == 0) { | ||
| tcp_set_state(sk, TCP_CLOSE); | ||
| sk->sk_route_caps = 0; | ||
| inet_sk(sk)->inet_dport = 0; | ||
| err = -1; | ||
| goto out; | ||
| } | ||
| #else | ||
| uaddr.sin_family = AF_INET; | ||
| uaddr.sin_addr.s_addr = daddr; | ||
| uaddr.sin_port = dport; | ||
| err = BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, (struct sockaddr *)&uaddr, &tmpMem); | ||
| #endif | ||
| err = BPF_CGROUP_RUN_PROG_INET4_CONNECT_KMESH(sk, (struct sockaddr *)&uaddr, &uaddrlen, &tmpMem); | ||
|
|
||
| connect: | ||
| err = sk->sk_prot->connect(sk, (struct sockaddr *)&uaddr, sizeof(struct sockaddr_in)); | ||
| if (unlikely(err)) { | ||
|
|
@@ -113,7 +117,7 @@ static int defer_connect(struct sock *sk, struct msghdr *msg, size_t size) | |
| inet_sk(sk)->inet_dport = 0; | ||
| goto out; | ||
| } | ||
| inet_sk(sk)->defer_connect = 0; | ||
| SET_FDEFER_CONNECT_OFF(sk); | ||
|
|
||
| if ((((__u32)1 << sk->sk_state) & ~(__u32)(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) && !tcp_passive_fastopen(sk)) { | ||
| sk_stream_wait_connect(sk, &timeo); | ||
|
|
@@ -128,7 +132,7 @@ static int defer_connect_and_sendmsg(struct sock *sk, struct msghdr *msg, size_t | |
| struct socket *sock; | ||
| int err = 0; | ||
|
|
||
| if (unlikely(inet_sk(sk)->defer_connect == 1)) { | ||
| if (unlikely(IS_DEFER_CONNECT(sk))) { | ||
| lock_sock(sk); | ||
|
|
||
| err = defer_connect(sk, msg, size); | ||
|
|
@@ -163,9 +167,9 @@ static int defer_tcp_connect(struct sock *sk, struct sockaddr *uaddr, int addr_l | |
| * of defer_connect should be 1 and the normal connect function | ||
| * needs to be used. | ||
| */ | ||
| if (inet_sk(sk)->defer_connect) | ||
| if (IS_DEFER_CONNECT(sk)) | ||
| return tcp_v4_connect(sk, uaddr, addr_len); | ||
| inet_sk(sk)->defer_connect = 1; | ||
| SET_FDEFER_CONNECT_ON(sk); | ||
| sk->sk_dport = ((struct sockaddr_in *)uaddr)->sin_port; | ||
| sk_daddr_set(sk, ((struct sockaddr_in *)uaddr)->sin_addr.s_addr); | ||
| sk->sk_socket->state = SS_CONNECTING; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract a kfunc.h