|
5 | 5 | #define _COMMON_H_ |
6 | 6 |
|
7 | 7 | #include "../../config/kmesh_marcos_def.h" |
| 8 | +#include <linux/in.h> |
8 | 9 | #include <stddef.h> |
9 | 10 | #include <stdbool.h> |
10 | 11 | #include <stdint.h> |
|
16 | 17 |
|
17 | 18 | #include "errno.h" |
18 | 19 |
|
| 20 | +struct bpf_mem_ptr { |
| 21 | + void *ptr; |
| 22 | + __u32 size; |
| 23 | +}; |
| 24 | + |
19 | 25 | #if ENHANCED_KERNEL |
| 26 | +#if KERNEL_KFUNC |
| 27 | +extern int bpf_parse_header_msg_func(void *src, int src__sz) __ksym; |
| 28 | +extern int bpf_km_header_strnstr_func(void *ctx, int ctx__sz, const char *key, int key__sz, const char *subptr) __ksym; |
| 29 | +extern int bpf_km_header_strncmp_func(const char *key, int key__sz, const char *target, int target__sz, int opt) __ksym; |
| 30 | +extern int bpf_setsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, const char *optval, int optval__sz) __ksym; |
| 31 | +extern int bpf_getsockopt_func(void *bpf_mem, int bpf_mem__sz, int optname, char *optval, int optval__sz) __ksym; |
| 32 | + |
| 33 | +#define bpf_km_header_strncmp bpf_km_header_strncmp_func |
| 34 | + |
| 35 | +int bpf_km_header_strnstr(void *ctx, const char *key, int key__sz, const char *subptr, int subptr__sz) |
| 36 | +{ |
| 37 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 38 | + return bpf_km_header_strnstr_func(&msg_tmp, sizeof(struct bpf_mem_ptr), key, key__sz, subptr); |
| 39 | +} |
| 40 | + |
| 41 | +int bpf_parse_header_msg(struct bpf_sock_addr *ctx) |
| 42 | +{ |
| 43 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 44 | + return bpf_parse_header_msg_func(&msg_tmp, sizeof(struct bpf_mem_ptr)); |
| 45 | +} |
| 46 | + |
| 47 | +// Due to the limitation of bpf verifier, optval and optval__sz are required to correspond. |
| 48 | +// The strnlen function cannot be used here, so the string is redefined. |
| 49 | +int bpf_km_setsockopt(struct bpf_sock_addr *ctx, int level, int optname, const char *optval, int optval__sz) |
| 50 | +{ |
| 51 | + const char kmesh_module_name[] = "kmesh_defer"; |
| 52 | + if (level != IPPROTO_TCP || optval__sz != sizeof(kmesh_module_name)) |
| 53 | + return -1; |
| 54 | + |
| 55 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 56 | + return bpf_setsockopt_func( |
| 57 | + &msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)kmesh_module_name, sizeof(kmesh_module_name)); |
| 58 | +} |
| 59 | + |
| 60 | +int bpf_km_getsockopt(struct bpf_sock_addr *ctx, int level, int optname, char *optval, int optval__sz) |
| 61 | +{ |
| 62 | + if (level != IPPROTO_TCP) { |
| 63 | + return -1; |
| 64 | + } |
| 65 | + struct bpf_mem_ptr msg_tmp = {.ptr = ctx, .size = sizeof(struct bpf_sock_addr)}; |
| 66 | + return bpf_getsockopt_func(&msg_tmp, sizeof(struct bpf_mem_ptr), optname, (void *)optval, optval__sz); |
| 67 | +} |
| 68 | + |
| 69 | +#else |
20 | 70 | #include <bpf_helper_defs_ext.h> |
| 71 | +#define bpf_km_setsockopt bpf_setsockopt |
| 72 | +#define bpf_km_getsockopt bpf_getsockopt |
| 73 | +#endif |
21 | 74 | #endif |
22 | 75 |
|
23 | 76 | #define bpf_unused __attribute__((__unused__)) |
@@ -113,14 +166,8 @@ static inline bool is_ipv4_mapped_addr(__u32 ip6[4]) |
113 | 166 | (dst)[3] = (src)[3]; \ |
114 | 167 | } while (0) |
115 | 168 |
|
116 | | -#if OE_23_03 |
117 | | -#define bpf__strncmp bpf_strncmp |
118 | | -#define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port) |
119 | | -#else |
120 | 169 | #define GET_SKOPS_REMOTE_PORT(sk_ops) (__u16)((sk_ops)->remote_port >> 16) |
121 | | -#endif |
122 | | - |
123 | | -#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port) |
| 170 | +#define GET_SKOPS_LOCAL_PORT(sk_ops) (__u16)((sk_ops)->local_port) |
124 | 171 |
|
125 | 172 | #define MAX_BUF_LEN 100 |
126 | 173 | #define MAX_IP4_LEN 16 |
|
0 commit comments