Skip to content

Commit

Permalink
bpf: add branch hint to switch-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Sep 27, 2024
1 parent b12372d commit cb10b66
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ IncludeBlocks: Preserve
IndentWidth: 2
ContinuationIndentWidth: 2
ColumnLimit: 100
AttributeMacros: [br_unlikely, br_likely, fallthrough]
6 changes: 3 additions & 3 deletions bpf/ingress.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions common/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb10b66

Please sign in to comment.