diff --git a/.clang-format b/.clang-format index 61b4cb4..dde00a0 100644 --- a/.clang-format +++ b/.clang-format @@ -3,3 +3,4 @@ IncludeBlocks: Preserve IndentWidth: 2 ContinuationIndentWidth: 2 ColumnLimit: 100 +AttributeMacros: [br_unlikely, br_likely, fallthrough] diff --git a/bpf/ingress.c b/bpf/ingress.c index a7ebac5..ee04d28 100644 --- a/bpf/ingress.c +++ b/bpf/ingress.c @@ -237,7 +237,7 @@ int ingress_handler(struct xdp_md* xdp) { } break; - case CONN_ESTABLISHED: // TODO: likely + br_likely case CONN_ESTABLISHED: if (unlikely(tcp->syn)) { goto fsm_error; } else if (ntohl(tcp->seq) == conn->ack_seq - 1) { @@ -255,7 +255,7 @@ int ingress_handler(struct xdp_md* xdp) { } else if (!tcp->psh && payload_len == 0) { // Empty segment without PSH will be treated as control packet will_send_ctrl_packet = false; - } else { // TODO: likely + } else br_likely { will_send_ctrl_packet = will_drop = false; conn->ack_seq += payload_len; __u32 peer_mss = conn->peer_mss ?: 1460; @@ -272,7 +272,7 @@ int ingress_handler(struct xdp_md* xdp) { } break; - default: // TODO: unlikely + br_unlikely default: fsm_error: flags |= TCP_FLAG_RST; swap(pktbuf, conn->pktbuf); diff --git a/common/defs.h b/common/defs.h index 647467f..f93406a 100644 --- a/common/defs.h +++ b/common/defs.h @@ -30,11 +30,28 @@ y = t; \ }) +#if __has_c_attribute(fallthrough) +#define fallthrough [[fallthrough]] +#elif __has_c_attribute(clang::fallthrough) +#define fallthrough [[clang::fallthrough]] +#else #define fallthrough +#endif #define unlikely(expr) __builtin_expect(!!(expr), 0) #define likely(expr) __builtin_expect(!!(expr), 1) +#if __has_c_attribute(clang::unlikely) +#define br_unlikely [[clang::unlikely]] +#else +#define br_unlikely +#endif +#if __has_c_attribute(clang::likely) +#define br_likely [[clang::likely]] +#else +#define br_likely +#endif + #ifdef _MIMIC_BPF // Some missing declaration of vmlinux.h