From 5def659c1e80b9c3c1990fdbec8c4b78d696ecc8 Mon Sep 17 00:00:00 2001 From: Andrii Vasylevskyi Date: Tue, 16 May 2023 13:43:55 -0700 Subject: [PATCH] Debug prints Summary: Adding debug prints to TPR prog, handy for debugging/investigation. Disabled with macro by default. Note on bpf_endian.h., copied from katran/lib, TPR code is using skeleton (while main katran not yet) and requires different includes. Also we've been keeping TPR and main katran lib somewhat independent. Reviewed By: sharmafb Differential Revision: D45824117 fbshipit-source-id: 18cd776d3d1ad15ee8b294b8eb4704e8df639b8f --- katran/tpr/bpf/bpf_endian.h | 57 ++++++++++++++++++++ katran/tpr/bpf/tcp_pkt_router_common.h | 56 +++++++++++++++++++ katran/tpr/bpf/tcp_pkt_router_kern.c | 4 +- katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h | 4 +- 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 katran/tpr/bpf/bpf_endian.h diff --git a/katran/tpr/bpf/bpf_endian.h b/katran/tpr/bpf/bpf_endian.h new file mode 100644 index 000000000..441ac88dc --- /dev/null +++ b/katran/tpr/bpf/bpf_endian.h @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BPF_ENDIAN__ +#define __BPF_ENDIAN__ + +#ifdef KATRAN_CMAKE_BUILD +#include "vmlinux.h" +#else +#include +#endif + +/* LLVM's BPF target selects the endianness of the CPU + * it compiles on, or the user specifies (bpfel/bpfeb), + * respectively. The used __BYTE_ORDER__ is defined by + * the compiler, we cannot rely on __BYTE_ORDER from + * libc headers, since it doesn't reflect the actual + * requested byte order. + * + * Note, LLVM's BPF target has different __builtin_bswapX() + * semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE + * in bpfel and bpfeb case, which means below, that we map + * to cpu_to_be16(). We could use it unconditionally in BPF + * case, but better not rely on it, so that this header here + * can be used from application and BPF program side, which + * use different targets. + */ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define __bpf_ntohs(x) __builtin_bswap16(x) +#define __bpf_htons(x) __builtin_bswap16(x) +#define __bpf_constant_ntohs(x) ___constant_swab16(x) +#define __bpf_constant_htons(x) ___constant_swab16(x) +#define __bpf_ntohl(x) __builtin_bswap32(x) +#define __bpf_htonl(x) __builtin_bswap32(x) +#define __bpf_constant_ntohl(x) ___constant_swab32(x) +#define __bpf_constant_htonl(x) ___constant_swab32(x) +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define __bpf_ntohs(x) (x) +#define __bpf_htons(x) (x) +#define __bpf_constant_ntohs(x) (x) +#define __bpf_constant_htons(x) (x) +#define __bpf_ntohl(x) (x) +#define __bpf_htonl(x) (x) +#define __bpf_constant_ntohl(x) (x) +#define __bpf_constant_htonl(x) (x) +#else +#error "Fix your compiler's __BYTE_ORDER__?!" +#endif + +#define bpf_htons(x) \ + (__builtin_constant_p(x) ? __bpf_constant_htons(x) : __bpf_htons(x)) +#define bpf_ntohs(x) \ + (__builtin_constant_p(x) ? __bpf_constant_ntohs(x) : __bpf_ntohs(x)) +#define bpf_htonl(x) \ + (__builtin_constant_p(x) ? __bpf_constant_htonl(x) : __bpf_htonl(x)) +#define bpf_ntohl(x) \ + (__builtin_constant_p(x) ? __bpf_constant_ntohl(x) : __bpf_ntohl(x)) + +#endif /* __BPF_ENDIAN__ */ diff --git a/katran/tpr/bpf/tcp_pkt_router_common.h b/katran/tpr/bpf/tcp_pkt_router_common.h index c5d5226ae..b8bb9b841 100644 --- a/katran/tpr/bpf/tcp_pkt_router_common.h +++ b/katran/tpr/bpf/tcp_pkt_router_common.h @@ -10,10 +10,66 @@ #include +#include "bpf_endian.h" #include "tcp_pkt_router_consts.h" #include "tcp_pkt_router_maps.h" #include "tcp_pkt_router_structs.h" +// Uncomment to enable debug prints and check license +// #define TPR_DEBUG + +#ifdef TPR_DEBUG +static inline const char* sk_op_str(__u32 op) { + switch (op) { + case BPF_SOCK_OPS_VOID: + return "VOID"; + case BPF_SOCK_OPS_TIMEOUT_INIT: + return "TIMEOUT_INIT"; + case BPF_SOCK_OPS_RWND_INIT: + return "RWND_INIT"; + case BPF_SOCK_OPS_TCP_CONNECT_CB: + return "TCP_CONNECT_CB"; + case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: + return "ACTIVE_ESTABLISHED_CB"; + case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: + return "PASSIVE_ESTABLISHED_CB"; + case BPF_SOCK_OPS_NEEDS_ECN: + return "NEEDS_ECN"; + case BPF_SOCK_OPS_BASE_RTT: + return "BASE_RTT"; + case BPF_SOCK_OPS_RTO_CB: + return "RTO_CB"; + case BPF_SOCK_OPS_RETRANS_CB: + return "RETRANS_CB"; + case BPF_SOCK_OPS_STATE_CB: + return "STATE_CB"; + case BPF_SOCK_OPS_TCP_LISTEN_CB: + return "TCP_LISTEN_CB"; + case BPF_SOCK_OPS_RTT_CB: + return "RTT_CB"; + case BPF_SOCK_OPS_PARSE_HDR_OPT_CB: + return "PARSE_HDR_OPT_CB"; + case BPF_SOCK_OPS_HDR_OPT_LEN_CB: + return "HDR_OPT_LEN_CB"; + case BPF_SOCK_OPS_WRITE_HDR_OPT_CB: + return "WRITE_HDR_OPT_CB"; + default: + return "UNKNOWN"; + } +} + +#define TPR_PRINT(skops, fmtStr, ...) \ + bpf_printk( \ + "[op=%s rport=%d lport=%d] " fmtStr, \ + sk_op_str(skops->op), \ + bpf_ntohl(skops->remote_port), \ + skops->local_port, \ + ##__VA_ARGS__); + +#else +#define TPR_PRINT(skops, fmtStr, ...) ; +#endif // TPR_DEBUG + #define _LIKELY(expr) __builtin_expect(!!(expr), 1) #define _UNLIKELY(expr) __builtin_expect(!!(expr), 0) diff --git a/katran/tpr/bpf/tcp_pkt_router_kern.c b/katran/tpr/bpf/tcp_pkt_router_kern.c index 1ae50b47e..ff33092a2 100644 --- a/katran/tpr/bpf/tcp_pkt_router_kern.c +++ b/katran/tpr/bpf/tcp_pkt_router_kern.c @@ -19,6 +19,7 @@ static inline int handle_passive_cb( struct bpf_sock_ops* skops, struct stats* stat, const struct server_info* s_info) { + TPR_PRINT(skops, "passive cb", skops->op); int err; switch (skops->op) { @@ -60,6 +61,7 @@ static inline int handle_passive_cb( static inline int handle_active_cb( struct bpf_sock_ops* skops, struct stats* stat) { + TPR_PRINT(skops, "active cb", skops->op); switch (skops->op) { case BPF_SOCK_OPS_TCP_CONNECT_CB: /* Called before SYN is sent on active side: nth to do */ @@ -117,6 +119,6 @@ int tcp_pkt_router(struct bpf_sock_ops* skops) { return CG_OK; } -// It requires GPL to run bpf_printk +// bpf_printk requires GPL license char _license[] SEC("license") = "Facebook"; int _version SEC("version") = 1; diff --git a/katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h b/katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h index 5e8d80630..112631c34 100644 --- a/katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h +++ b/katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h @@ -39,8 +39,9 @@ static inline int handle_passive_parse_hdr( stat->error_bad_id++; return PASS; } else { - // no need to keep writing this once peer sends the right server_id. stat->server_id_read++; + TPR_PRINT(skops, "passive received server-id option"); + // no need to keep writing this once peer sends the right server_id. err = unset_parse_hdr_cb_flags(skops, stat); err |= unset_write_hdr_cb_flags(skops, stat); return err; @@ -64,6 +65,7 @@ static inline int handle_passive_write_hdr_opt( return err; } stat->server_id_set++; + TPR_PRINT(skops, "passive wrote option"); return SUCCESS; }