Skip to content

Commit

Permalink
Improve bpf log,format ipaddr in BPF_LOG
Browse files Browse the repository at this point in the history
Signed-off-by: kangmingfa <1640528278@qq.com>
  • Loading branch information
bfforever committed May 27, 2024
1 parent 728c8f3 commit ea01ba1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
10 changes: 4 additions & 6 deletions bpf/include/bpf_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ enum bpf_loglevel {
} \
} while (0)

/* add this macro to get ip addr from ctx variable, include bpf_sock_addr or bpf_sock_ops, weird
/* Add this macro to get ip addr from ctx variable, include bpf_sock_addr or bpf_sock_ops, weird
reason is direct access would not be print ipaddr when pass `&ctx->remote_ipv4` to bpf_trace_printk */
#define DECLARE_VAR_IPV4(t, ctx_ip, name) \
__u32 ip = 0; \
if ((int)(BPF_LOGTYPE_##t) == BPF_DEBUG_ON) { \
ip = ctx_ip; \
}
#define DECLARE_VAR_IPV4(ctx_ip, name) \
__u32 name = 0; \
name = ctx_ip;

#endif // _BPF_LOG_H_
2 changes: 1 addition & 1 deletion bpf/kmesh/ads/cgroup_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)
if (!listener)
return -ENOENT;
}
DECLARE_VAR_IPV4(KMESH, ctx->user_ip4, ip);
DECLARE_VAR_IPV4(ctx->user_ip4, ip);
BPF_LOG(DEBUG, KMESH, "bpf find listener addr=[%pI4h:%u]\n", &ip, bpf_ntohs(ctx->user_port));

#if ENHANCED_KERNEL
Expand Down
2 changes: 1 addition & 1 deletion bpf/kmesh/ads/include/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int filter_chain_manager(ctx_buff_t *ctx)
/* filter match */
ret = filter_chain_filter_match(filter_chain, &addr, ctx, &filter, &filter_idx);
if (ret != 0) {
BPF_LOG(ERR, FILTERCHAIN, "no match filter, addr=%pI4\n", &addr.ipv4);
BPF_LOG(ERR, FILTERCHAIN, "no match filter, addr=%pI4h\n", &addr.ipv4);
return KMESH_TAIL_CALL_RET(-1);
}

Expand Down
2 changes: 1 addition & 1 deletion bpf/kmesh/ads/sockops.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int sockops_traffic_control(struct bpf_sock_ops *skops, struct bpf_mem_pt
}
}

DECLARE_VAR_IPV4(SOCKOPS, skops->remote_ip4, ip)
DECLARE_VAR_IPV4(skops->remote_ip4, ip)
BPF_LOG(
DEBUG,
SOCKOPS,
Expand Down
2 changes: 1 addition & 1 deletion bpf/kmesh/workload/cgroup_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static inline int sock4_traffic_control(struct bpf_sock_addr *ctx)

DECLARE_FRONTEND_KEY(ctx, frontend_k);

DECLARE_VAR_IPV4(KMESH, ctx->user_ip4, ip);
DECLARE_VAR_IPV4(ctx->user_ip4, ip);
BPF_LOG(DEBUG, KMESH, "origin addr=[%pI4h:%u]\n", &ip, bpf_ntohs(ctx->user_port));
frontend_v = map_lookup_frontend(&frontend_k);
if (!frontend_v) {
Expand Down

0 comments on commit ea01ba1

Please sign in to comment.