diff --git a/bpf/include/bpf_common.h b/bpf/include/bpf_common.h index b5df9f0ee..76754c63f 100644 --- a/bpf/include/bpf_common.h +++ b/bpf/include/bpf_common.h @@ -16,14 +16,14 @@ #define map_of_manager kmesh_manage #define MAP_SIZE_OF_MANAGER 8192 -/*0x3a10000 is the specific port handled by the cni to enable kmesh*/ -#define ENABLE_KMESH_PORT 0x3a10000 -/*0x3a20000 is the specific port handled by the cni to enable kmesh*/ -#define DISABLE_KMESH_PORT 0x3a20000 -/*0x3a30000 is the specific port handled by the daemon to enable bypass*/ -#define ENABLE_BYPASS_PORT 0x3a30000 -/*0x3a40000 is the specific port handled by the daemon to enable bypass*/ -#define DISABLE_BYPASS_PORT 0x3a40000 +/*0x3a1(929) is the specific port handled by the cni to enable kmesh*/ +#define ENABLE_KMESH_PORT 0x3a1 +/*0x3a2(930) is the specific port handled by the cni to enable kmesh*/ +#define DISABLE_KMESH_PORT 0x3a2 +/*0x3a3(931) is the specific port handled by the daemon to enable bypass*/ +#define ENABLE_BYPASS_PORT 0x3a3 +/*0x3a4(932) is the specific port handled by the daemon to enable bypass*/ +#define DISABLE_BYPASS_PORT 0x3a4 typedef struct { __u32 is_bypassed; @@ -104,28 +104,28 @@ static inline bool conn_from_bypass_sim_add(struct bpf_sock_addr *ctx) { // daemon sim connect 0.0.0.0:931(0x3a3) // 0x3a3 is the specific port handled by the daemon to enable bypass - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == ENABLE_BYPASS_PORT)); + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohs(ctx->user_port) == ENABLE_BYPASS_PORT)); } static inline bool conn_from_bypass_sim_delete(struct bpf_sock_addr *ctx) { // daemon sim connect 0.0.0.1:932(0x3a4) // 0x3a4 is the specific port handled by the daemon to disable bypass - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == DISABLE_BYPASS_PORT)); + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohs(ctx->user_port) == DISABLE_BYPASS_PORT)); } static inline bool conn_from_cni_sim_add(struct bpf_sock_addr *ctx) { // cni sim connect 0.0.0.0:929(0x3a1) // 0x3a1 is the specific port handled by the cni to enable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == ENABLE_KMESH_PORT)); + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohs(ctx->user_port) == ENABLE_KMESH_PORT)); } static inline bool conn_from_cni_sim_delete(struct bpf_sock_addr *ctx) { // cni sim connect 0.0.0.1:930(0x3a2) // 0x3a2 is the specific port handled by the cni to disable Kmesh - return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohl(ctx->user_port) == DISABLE_KMESH_PORT)); + return ((bpf_ntohl(ctx->user_ip4) == 1) && (bpf_ntohs(ctx->user_port) == DISABLE_KMESH_PORT)); } /* This function is used to store and delete cookie diff --git a/bpf/include/common.h b/bpf/include/common.h index 1da865f70..a4758651d 100644 --- a/bpf/include/common.h +++ b/bpf/include/common.h @@ -69,7 +69,12 @@ static inline int kmesh_map_update_elem(void *map, const void *key, const void * } #if OE_23_03 -#define bpf__strncmp bpf_strncmp +#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) + #endif // _COMMON_H_ diff --git a/bpf/kmesh/ads/sockops.c b/bpf/kmesh/ads/sockops.c index 3ace3f0f9..9e7d7fa6c 100644 --- a/bpf/kmesh/ads/sockops.c +++ b/bpf/kmesh/ads/sockops.c @@ -33,9 +33,8 @@ static int sockops_traffic_control(struct bpf_sock_ops *skops, struct bpf_mem_pt int ret; /* 1 lookup listener */ DECLARE_VAR_ADDRESS(skops, addr); -#if !OE_23_03 - addr.port = addr.port >> 16; -#endif + addr.port = GET_SKOPS_REMOTE_PORT(skops); + Listener__Listener *listener = map_lookup_listener(&addr); if (!listener) { diff --git a/bpf/kmesh/workload/include/backend.h b/bpf/kmesh/workload/include/backend.h index 92c2c685f..444664241 100644 --- a/bpf/kmesh/workload/include/backend.h +++ b/bpf/kmesh/workload/include/backend.h @@ -82,7 +82,7 @@ static inline int backend_manager(ctx_buff_t *ctx, backend_value *backend_v, __u return -EINVAL; } if (service_id == backend_v->service[i]) { - BPF_LOG(DEBUG, BACKEND, "access the backend by service:%d\n", service_id); + BPF_LOG(DEBUG, BACKEND, "access the backend by service:%u\n", service_id); #pragma unroll for (__u32 j = 0; j < MAX_PORT_COUNT; j++) { if (user_port == service_v->service_port[j]) { diff --git a/bpf/kmesh/workload/sockops.c b/bpf/kmesh/workload/sockops.c index eb2d3dcb7..b7cd11c78 100644 --- a/bpf/kmesh/workload/sockops.c +++ b/bpf/kmesh/workload/sockops.c @@ -60,14 +60,11 @@ static inline void extract_skops_to_tuple(struct bpf_sock_ops *skops, struct bpf tuple_key->ipv4.saddr = skops->local_ip4; tuple_key->ipv4.daddr = skops->remote_ip4; // local_port is host byteorder - tuple_key->ipv4.sport = bpf_htonl(skops->local_port) >> FORMAT_IP_LENGTH; + tuple_key->ipv4.sport = bpf_htons(GET_SKOPS_LOCAL_PORT(skops)); // remote_port is network byteorder // openEuler 2303 convert remote port different than other linux vendor -#if !OE_23_03 - tuple_key->ipv4.dport = skops->remote_port >> FORMAT_IP_LENGTH; -#else - tuple_key->ipv4.dport = skops->remote_port; -#endif + + tuple_key->ipv4.dport = GET_SKOPS_REMOTE_PORT(skops); } static inline void extract_skops_to_tuple_reverse(struct bpf_sock_ops *skops, struct bpf_sock_tuple *tuple_key) @@ -75,14 +72,9 @@ static inline void extract_skops_to_tuple_reverse(struct bpf_sock_ops *skops, st tuple_key->ipv4.saddr = skops->remote_ip4; tuple_key->ipv4.daddr = skops->local_ip4; // remote_port is network byteorder - // openEuler 2303 convert remote port different than other linux vendor -#if !OE_23_03 - tuple_key->ipv4.sport = skops->remote_port >> FORMAT_IP_LENGTH; -#else - tuple_key->ipv4.sport = skops->remote_port; -#endif + tuple_key->ipv4.sport = GET_SKOPS_REMOTE_PORT(skops); // local_port is host byteorder - tuple_key->ipv4.dport = bpf_htonl(skops->local_port) >> FORMAT_IP_LENGTH; + tuple_key->ipv4.dport = bpf_htons(GET_SKOPS_LOCAL_PORT(skops)); } // clean map_of_auth @@ -158,14 +150,12 @@ static inline void remove_kmesh_managed_ip(__u32 ip) BPF_LOG(ERR, KMESH, "remove ip failed!, err is %d\n", err); } -static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u32 port) +static inline bool conn_from_sim(struct bpf_sock_ops *skops, __u32 ip, __u16 port) { - __u32 rev_port = bpf_ntohl(skops->remote_port); + __u16 remote_port = GET_SKOPS_REMOTE_PORT(skops); __u32 client_ip = bpf_ntohl(skops->remote_ip4); -#if !OE_23_03 - port >>= 16; -#endif - return (client_ip == ip) && (port == rev_port); + + return (client_ip == ip) && (bpf_ntohs(remote_port) == port); } static inline bool skops_conn_from_cni_sim_add(struct bpf_sock_ops *skops) diff --git a/oncn-mda/ebpf_src/sock_ops.c b/oncn-mda/ebpf_src/sock_ops.c index 92fbfd4b9..68086b0a4 100644 --- a/oncn-mda/ebpf_src/sock_ops.c +++ b/oncn-mda/ebpf_src/sock_ops.c @@ -234,14 +234,10 @@ static void extract_key4_from_ops(struct bpf_sock_ops *const ops, struct sock_ke * optimized to think that only 16-bit data needs to be read here, but most kernels do not support this, * causing the BPF validator to fail. */ - key->sport = (bpf_htonl(ops->local_port) >> FORMAT_IP_LENGTH); + key->sport = bpf_htons(GET_SKOPS_LOCAL_PORT(ops)); key->dip4 = ops->remote_ip4; + key->dport = GET_SKOPS_REMOTE_PORT(ops); -#if !OE_23_03 - key->dport = (force_read(ops->remote_port) >> FORMAT_IP_LENGTH); -#else - key->dport = (force_read(ops->remote_port)); -#endif bpf_log(DEBUG, "sip:%u, sport:%u\n", key->sip4, key->sport); bpf_log(DEBUG, "dip:%u, dport:%u\n", key->dip4, key->dport); @@ -389,7 +385,7 @@ static void clean_ops_map(struct bpf_sock_ops *const skops) reverse_key = bpf_map_lookup_elem(&SOCK_OPS_PROXY_MAP_NAME, &key); ret = bpf_map_delete_elem(&SOCK_OPS_PROXY_MAP_NAME, &key); if (ret && ret != -ENOENT) - bpf_log(INFO, "bpf map delete proxy elem key failed! ret:%d\n", ret); + bpf_log(INFO, "bpf map delete prox elem key failed! ret:%d\n", ret); if (reverse_key == NULL) return; diff --git a/oncn-mda/include/mesh_accelerate.h b/oncn-mda/include/mesh_accelerate.h index 307bc905e..e48c76d42 100644 --- a/oncn-mda/include/mesh_accelerate.h +++ b/oncn-mda/include/mesh_accelerate.h @@ -37,6 +37,14 @@ enum bpf_loglevel { #define BPF_LOGLEVEL BPF_LOG_ERROR +#if OE_23_03 +#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) + #ifndef bpf_printk #define bpf_printk(fmt, ...) \ ({ \