Skip to content

Commit

Permalink
Merge pull request #395 from nlgwcy/remote_port
Browse files Browse the repository at this point in the history
unified handing logic for skops->remote_port/local_port
  • Loading branch information
kmesh-bot authored May 30, 2024
2 parents 7766b16 + f55ce9a commit d1e90d1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
24 changes: 12 additions & 12 deletions bpf/include/bpf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion bpf/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
5 changes: 2 additions & 3 deletions bpf/kmesh/ads/sockops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion bpf/kmesh/workload/include/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
28 changes: 9 additions & 19 deletions bpf/kmesh/workload/sockops.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,21 @@ 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)
{
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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions oncn-mda/ebpf_src/sock_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions oncn-mda/include/mesh_accelerate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) \
({ \
Expand Down

0 comments on commit d1e90d1

Please sign in to comment.