Skip to content

Commit

Permalink
Debug prints
Browse files Browse the repository at this point in the history
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
  • Loading branch information
avasylev authored and facebook-github-bot committed May 16, 2023
1 parent 06d0c85 commit 5def659
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
57 changes: 57 additions & 0 deletions katran/tpr/bpf/bpf_endian.h
Original file line number Diff line number Diff line change
@@ -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 <bpf/vmlinux/vmlinux.h>
#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__ */
56 changes: 56 additions & 0 deletions katran/tpr/bpf/tcp_pkt_router_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,66 @@

#include <bpf/bpf_helpers.h>

#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)

Expand Down
4 changes: 3 additions & 1 deletion katran/tpr/bpf/tcp_pkt_router_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
4 changes: 3 additions & 1 deletion katran/tpr/bpf/tcp_pkt_router_passive_hdlr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 5def659

Please sign in to comment.