Skip to content

Commit 18f7972

Browse files
committed
style: apply consistent formatting and indentation across BPF source files
1 parent 7d3db0a commit 18f7972

File tree

7 files changed

+231
-205
lines changed

7 files changed

+231
-205
lines changed

.clang-format

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Minimal format loosely matching the kernel style.
2+
Standard: Cpp03
3+
IndentWidth: 4
4+
TabWidth: 4
5+
UseTab: Never
6+
ColumnLimit: 120
7+
SortIncludes: false
8+
SpaceAfterCStyleCast: true
9+
AllowShortFunctionsOnASingleLine: None
10+
Cpp11BracedListStyle: false
11+
BreakBeforeBraces: Custom
12+
BraceWrapping:
13+
AfterFunction: true

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ clang-format-check:
2525

2626
.PHONY: clang-format
2727
clang-format:
28-
@docker run --rm -v $(PWD)/src:/src silkeh/clang clang-format --style=file -i $(CLANG_FORMAT_FILES)
28+
@docker run --rm -v $(PWD)/src:/src -v $(PWD)/.clang-format:/.clang-format silkeh/clang clang-format --style=file -i $(CLANG_FORMAT_FILES)
2929
@sudo chown -R $(shell id -u):$(shell id -g) src
3030

3131
.PHONY: config-check

src/common/maps.bpf.h

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,93 @@
22

33
// Produce values that are usable for prometheus. See:
44
// * https://github.com/cloudflare/ebpf_exporter/issues/488
5-
static __always_inline u64 log2l_histogram(u64 v) {
6-
u64 rounded = log2l(v);
5+
static __always_inline u64 log2l_histogram(u64 v)
6+
{
7+
u64 rounded = log2l(v);
78

8-
if (rounded == 0) {
9-
return 0;
10-
}
9+
if (rounded == 0) {
10+
return 0;
11+
}
1112

12-
if (2ULL << (rounded - 1) == v) {
13-
return rounded;
14-
} else {
15-
return rounded + 1;
16-
}
13+
if (2ULL << (rounded - 1) == v) {
14+
return rounded;
15+
} else {
16+
return rounded + 1;
17+
}
1718
}
1819

19-
#define lookup_or_zero_init_key(map, key, into) \
20-
u64 zero = 0; \
21-
\
22-
into = bpf_map_lookup_elem(map, key); \
23-
if (!into) { \
24-
bpf_map_update_elem(map, key, &zero, BPF_NOEXIST); \
25-
into = bpf_map_lookup_elem(map, key); \
26-
if (!into) { \
27-
return 0; \
28-
} \
29-
}
20+
#define lookup_or_zero_init_key(map, key, into) \
21+
u64 zero = 0; \
22+
\
23+
into = bpf_map_lookup_elem(map, key); \
24+
if (!into) { \
25+
bpf_map_update_elem(map, key, &zero, BPF_NOEXIST); \
26+
into = bpf_map_lookup_elem(map, key); \
27+
if (!into) { \
28+
return 0; \
29+
} \
30+
}
3031

31-
#define increment_variant(map, key, increment, variant) \
32-
u64 *count; \
33-
\
34-
lookup_or_zero_init_key(map, key, count); \
35-
\
36-
variant; \
37-
\
38-
return *count;
32+
#define increment_variant(map, key, increment, variant) \
33+
u64 *count; \
34+
\
35+
lookup_or_zero_init_key(map, key, count); \
36+
\
37+
variant; \
38+
\
39+
return *count;
3940

40-
static inline int increment_map(void *map, void *key, u64 increment) {
41-
increment_variant(map, key, increment,
42-
__sync_fetch_and_add(count, increment));
41+
static inline int increment_map(void *map, void *key, u64 increment)
42+
{
43+
increment_variant(map, key, increment, __sync_fetch_and_add(count, increment));
4344
}
4445

45-
static inline int increment_map_nosync(void *map, void *key, u64 increment) {
46-
increment_variant(map, key, increment, *count += increment);
46+
static inline int increment_map_nosync(void *map, void *key, u64 increment)
47+
{
48+
increment_variant(map, key, increment, *count += increment);
4749
}
4850

4951
// Arrays are always preallocated, so this only fails if the key is missing
50-
#define read_array_ptr(map, key, into) \
51-
into = bpf_map_lookup_elem(map, key); \
52-
if (!into) { \
53-
return 0; \
54-
}
52+
#define read_array_ptr(map, key, into) \
53+
into = bpf_map_lookup_elem(map, key); \
54+
if (!into) { \
55+
return 0; \
56+
}
5557

56-
#define _increment_histogram(map, key, increment, max_bucket, increment_fn) \
57-
if (key.bucket > max_bucket) { \
58-
key.bucket = max_bucket; \
59-
} \
60-
\
61-
increment_fn(map, &key, 1); \
62-
\
63-
if (increment > 0) { \
64-
key.bucket = max_bucket + 1; \
65-
increment_fn(map, &key, increment); \
66-
}
58+
#define _increment_histogram(map, key, increment, max_bucket, increment_fn) \
59+
if (key.bucket > max_bucket) { \
60+
key.bucket = max_bucket; \
61+
} \
62+
\
63+
increment_fn(map, &key, 1); \
64+
\
65+
if (increment > 0) { \
66+
key.bucket = max_bucket + 1; \
67+
increment_fn(map, &key, increment); \
68+
}
6769

68-
#define _increment_ex2_histogram(map, key, increment, max_bucket, \
69-
increment_fn) \
70-
key.bucket = log2l_histogram(increment); \
71-
\
72-
_increment_histogram(map, key, increment, max_bucket, increment_fn);
70+
#define _increment_ex2_histogram(map, key, increment, max_bucket, increment_fn) \
71+
key.bucket = log2l_histogram(increment); \
72+
\
73+
_increment_histogram(map, key, increment, max_bucket, increment_fn);
7374

74-
#define increment_exp2_histogram(map, key, increment, max_bucket) \
75-
_increment_ex2_histogram(map, key, increment, max_bucket, increment_map)
75+
#define increment_exp2_histogram(map, key, increment, max_bucket) \
76+
_increment_ex2_histogram(map, key, increment, max_bucket, increment_map)
7677

77-
#define increment_exp2_histogram_nosync(map, key, increment, max_bucket) \
78-
_increment_ex2_histogram(map, key, increment, max_bucket, \
79-
increment_map_nosync)
78+
#define increment_exp2_histogram_nosync(map, key, increment, max_bucket) \
79+
_increment_ex2_histogram(map, key, increment, max_bucket, increment_map_nosync)
8080

81-
#define _increment_exp2zero_histogram(map, key, increment, max_bucket, \
82-
increment_fn) \
83-
if (increment == 0) { \
84-
key.bucket = 0; \
85-
} else { \
86-
key.bucket = log2l_histogram(increment) + 1; \
87-
} \
88-
\
89-
_increment_histogram(map, key, increment, max_bucket, increment_fn);
81+
#define _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_fn) \
82+
if (increment == 0) { \
83+
key.bucket = 0; \
84+
} else { \
85+
key.bucket = log2l_histogram(increment) + 1; \
86+
} \
87+
\
88+
_increment_histogram(map, key, increment, max_bucket, increment_fn);
9089

91-
#define increment_exp2zero_histogram(map, key, increment, max_bucket) \
92-
_increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map)
90+
#define increment_exp2zero_histogram(map, key, increment, max_bucket) \
91+
_increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map)
9392

94-
#define increment_exp2zero_histogram_nosync(map, key, increment, max_bucket) \
95-
_increment_exp2zero_histogram(map, key, increment, max_bucket, \
96-
increment_map_nosync)
93+
#define increment_exp2zero_histogram_nosync(map, key, increment, max_bucket) \
94+
_increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map_nosync)

src/tcp/tcp-retransmit.bpf.c

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,99 @@
1515
#define UPPER_PORT_BOUND 32768
1616

1717
struct ipv4_key_t {
18-
u32 saddr;
19-
u32 daddr;
20-
u16 main_port;
21-
u8 type;
18+
u32 saddr;
19+
u32 daddr;
20+
u16 main_port;
21+
u8 type;
2222
};
2323

2424
struct ipv6_key_t {
25-
u8 saddr[16];
26-
u8 daddr[16];
27-
u16 main_port;
28-
u8 type;
25+
u8 saddr[16];
26+
u8 daddr[16];
27+
u16 main_port;
28+
u8 type;
2929
};
3030

3131
struct {
32-
__uint(type, BPF_MAP_TYPE_LRU_HASH);
33-
__uint(max_entries, MAX_ENTRIES);
34-
__type(key, struct ipv4_key_t);
35-
__type(value, u64);
32+
__uint(type, BPF_MAP_TYPE_LRU_HASH);
33+
__uint(max_entries, MAX_ENTRIES);
34+
__type(key, struct ipv4_key_t);
35+
__type(value, u64);
3636
} tcp_retransmit_ipv4_packets_total SEC(".maps");
3737

3838
struct {
39-
__uint(type, BPF_MAP_TYPE_LRU_HASH);
40-
__uint(max_entries, MAX_ENTRIES);
41-
__type(key, struct ipv6_key_t);
42-
__type(value, u64);
39+
__uint(type, BPF_MAP_TYPE_LRU_HASH);
40+
__uint(max_entries, MAX_ENTRIES);
41+
__type(key, struct ipv6_key_t);
42+
__type(value, u64);
4343
} tcp_retransmit_ipv6_packets_total SEC(".maps");
4444

45-
static int extract_main_port(const struct sock *sk) {
46-
u16 sport = sk->__sk_common.skc_num;
47-
u16 dport = __builtin_bswap16(sk->__sk_common.skc_dport);
45+
static int extract_main_port(const struct sock *sk)
46+
{
47+
u16 sport = sk->__sk_common.skc_num;
48+
u16 dport = __builtin_bswap16(sk->__sk_common.skc_dport);
4849

49-
if (sport > UPPER_PORT_BOUND && dport > UPPER_PORT_BOUND) {
50-
return 0;
51-
}
50+
if (sport > UPPER_PORT_BOUND && dport > UPPER_PORT_BOUND) {
51+
return 0;
52+
}
5253

53-
if (sport < dport) {
54-
return sport;
55-
}
54+
if (sport < dport) {
55+
return sport;
56+
}
5657

57-
return dport;
58+
return dport;
5859
}
5960

60-
#define TRACE_PROTOCOL(key_type, map, ip_extractor) \
61-
key_type key = {}; \
62-
\
63-
key.type = type; \
64-
key.main_port = extract_main_port(sk); \
65-
\
66-
ip_extractor; \
67-
\
68-
increment_map(map, &key, 1); \
69-
\
70-
return 0;
71-
72-
static int trace_ipv4(const struct sock *sk, u8 type) {
73-
TRACE_PROTOCOL(struct ipv4_key_t, &tcp_retransmit_ipv4_packets_total, {
74-
key.saddr = sk->__sk_common.skc_rcv_saddr;
75-
key.daddr = sk->__sk_common.skc_daddr;
76-
});
61+
#define TRACE_PROTOCOL(key_type, map, ip_extractor) \
62+
key_type key = {}; \
63+
\
64+
key.type = type; \
65+
key.main_port = extract_main_port(sk); \
66+
\
67+
ip_extractor; \
68+
\
69+
increment_map(map, &key, 1); \
70+
\
71+
return 0;
72+
73+
static int trace_ipv4(const struct sock *sk, u8 type)
74+
{
75+
TRACE_PROTOCOL(struct ipv4_key_t, &tcp_retransmit_ipv4_packets_total, {
76+
key.saddr = sk->__sk_common.skc_rcv_saddr;
77+
key.daddr = sk->__sk_common.skc_daddr;
78+
});
7779
}
7880

79-
static int trace_ipv6(const struct sock *sk, u8 type) {
80-
TRACE_PROTOCOL(struct ipv6_key_t, &tcp_retransmit_ipv6_packets_total, {
81-
bpf_probe_read_kernel(&key.saddr, sizeof(key.saddr),
82-
sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
83-
bpf_probe_read_kernel(&key.daddr, sizeof(key.daddr),
84-
sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
85-
});
81+
static int trace_ipv6(const struct sock *sk, u8 type)
82+
{
83+
TRACE_PROTOCOL(struct ipv6_key_t, &tcp_retransmit_ipv6_packets_total, {
84+
bpf_probe_read_kernel(&key.saddr, sizeof(key.saddr), sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
85+
bpf_probe_read_kernel(&key.daddr, sizeof(key.daddr), sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
86+
});
8687
}
8788

88-
static int trace_event(const struct sock *sk, u8 type) {
89-
switch (sk->__sk_common.skc_family) {
90-
case AF_INET:
91-
return trace_ipv4(sk, type);
92-
case AF_INET6:
93-
return trace_ipv6(sk, type);
94-
}
89+
static int trace_event(const struct sock *sk, u8 type)
90+
{
91+
switch (sk->__sk_common.skc_family) {
92+
case AF_INET:
93+
return trace_ipv4(sk, type);
94+
case AF_INET6:
95+
return trace_ipv6(sk, type);
96+
}
9597

96-
return 0;
98+
return 0;
9799
}
98100

99101
SEC("fentry/tcp_send_loss_probe")
100-
int BPF_PROG(tcp_send_loss_probe, struct sock *sk) {
101-
return trace_event(sk, TLP);
102+
int BPF_PROG(tcp_send_loss_probe, struct sock *sk)
103+
{
104+
return trace_event(sk, TLP);
102105
}
103106

104107
SEC("fentry/tcp_retransmit_skb")
105-
int BPF_PROG(tcp_retransmit_skb, struct sock *sk) {
106-
return trace_event(sk, RETRANSMIT);
108+
int BPF_PROG(tcp_retransmit_skb, struct sock *sk)
109+
{
110+
return trace_event(sk, RETRANSMIT);
107111
}
108112

109113
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)