Skip to content

Commit dabb53a

Browse files
authored
Merge pull request #611 from bobrik/ivan/fix-span-submit
Use a separate tagged span base for sock tracing
2 parents 6140e22 + 49146ae commit dabb53a

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

examples/sock-trace.bpf.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
extern int LINUX_KERNEL_VERSION __kconfig;
1212

1313
struct stitch_span_t {
14-
struct span_base_t span_base;
14+
struct span_base_tagged_t span_base;
1515
u64 socket_cookie;
1616
};
1717

1818
struct sock_release_span_t {
19-
struct span_base_t span_base;
19+
struct span_base_tagged_t span_base;
2020
u64 span_id;
2121
};
2222

2323
struct sk_span_t {
24-
struct span_base_t span_base;
24+
struct span_base_tagged_t span_base;
2525
u64 ksym;
2626
};
2727

2828
struct sk_error_report_span_t {
29-
struct span_base_t span_base;
29+
struct span_base_tagged_t span_base;
3030
u64 kstack[MAX_STACK_DEPTH];
3131
u32 sk_err;
3232
};
@@ -55,21 +55,21 @@ struct {
5555
__uint(type, BPF_MAP_TYPE_LRU_HASH);
5656
__uint(max_entries, 1024 * 10);
5757
__type(key, u64);
58-
__type(value, struct span_parent_t);
58+
__type(value, struct span_parent_tagged_t);
5959
} traced_socket_cookies SEC(".maps");
6060

6161
SEC("usdt/./tracing/demos/sock/demo:ebpf_exporter:sock_set_parent_span")
6262
int BPF_USDT(sock_set_parent_span, u64 socket_cookie, u64 trace_id_hi, u64 trace_id_lo, u64 span_id,
6363
u64 example_userspace_tag)
6464
{
65-
struct span_parent_t parent = { .trace_id_hi = trace_id_hi,
66-
.trace_id_lo = trace_id_lo,
67-
.span_id = span_id,
68-
.example_userspace_tag = example_userspace_tag };
65+
struct span_parent_tagged_t parent = { .trace_id_hi = trace_id_hi,
66+
.trace_id_lo = trace_id_lo,
67+
.span_id = span_id,
68+
.example_userspace_tag = example_userspace_tag };
6969

7070
bpf_map_update_elem(&traced_socket_cookies, &socket_cookie, &parent, BPF_ANY);
7171

72-
submit_span(&stitch_spans, struct stitch_span_t, &parent, { span->socket_cookie = socket_cookie; });
72+
submit_span_tagged_base(&stitch_spans, struct stitch_span_t, &parent, { span->socket_cookie = socket_cookie; });
7373

7474
return 0;
7575
}
@@ -78,13 +78,13 @@ SEC("fentry/__sock_release")
7878
int BPF_PROG(__sock_release, struct socket *sock)
7979
{
8080
u64 socket_cookie = bpf_get_socket_cookie(sock->sk);
81-
struct span_parent_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
81+
struct span_parent_tagged_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
8282

8383
if (!parent) {
8484
return 0;
8585
}
8686

87-
submit_span(&sock_release_spans, struct sock_release_span_t, parent, { span->span_id = 0xdead; });
87+
submit_span_tagged_base(&sock_release_spans, struct sock_release_span_t, parent, { span->span_id = 0xdead; });
8888

8989
bpf_map_delete_elem(&traced_socket_cookies, &socket_cookie);
9090

@@ -93,13 +93,13 @@ int BPF_PROG(__sock_release, struct socket *sock)
9393

9494
static int handle_sk(struct pt_regs *ctx, u64 socket_cookie)
9595
{
96-
struct span_parent_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
96+
struct span_parent_tagged_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
9797

9898
if (!parent) {
9999
return 0;
100100
}
101101

102-
submit_span(&sk_spans, struct sk_span_t, parent, {
102+
submit_span_tagged_base(&sk_spans, struct sk_span_t, parent, {
103103
// FIXME: PT_REGS_IP_CORE(ctx) does not work for fentry, so we abuse kstack
104104
bpf_get_stack(ctx, &span->ksym, sizeof(span->ksym), SKIP_FRAMES);
105105
span->ksym -= 8;
@@ -173,13 +173,13 @@ SEC("fentry/sk_error_report")
173173
int BPF_PROG(sk_error_report, struct sock *sk)
174174
{
175175
u64 socket_cookie = bpf_get_socket_cookie(sk);
176-
struct span_parent_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
176+
struct span_parent_tagged_t *parent = bpf_map_lookup_elem(&traced_socket_cookies, &socket_cookie);
177177

178178
if (!parent) {
179179
return 0;
180180
}
181181

182-
submit_span(&sk_error_report_spans, struct sk_error_report_span_t, parent, {
182+
submit_span_tagged_base(&sk_error_report_spans, struct sk_error_report_span_t, parent, {
183183
bpf_get_stack(ctx, &span->kstack, sizeof(span->kstack), SKIP_FRAMES);
184184
span->sk_err = sk->sk_err;
185185
});

examples/tracing.bpf.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ struct span_parent_t {
22
u64 trace_id_hi;
33
u64 trace_id_lo;
44
u64 span_id;
5-
u64 example_userspace_tag;
65
};
76

87
struct span_base_t {
@@ -30,3 +29,37 @@ static inline void fill_span_base(struct span_base_t *span, struct span_parent_t
3029
fill; \
3130
\
3231
bpf_ringbuf_submit(span, 0);
32+
33+
struct span_parent_tagged_t {
34+
u64 trace_id_hi;
35+
u64 trace_id_lo;
36+
u64 span_id;
37+
// extra info to carry in the parent
38+
u64 example_userspace_tag;
39+
};
40+
41+
struct span_base_tagged_t {
42+
struct span_parent_tagged_t parent;
43+
u64 span_id;
44+
u64 span_monotonic_timestamp_ns;
45+
u64 span_duration_ns;
46+
};
47+
48+
static inline void fill_span_base_tagged(struct span_base_tagged_t *span, struct span_parent_tagged_t *parent)
49+
{
50+
span->parent = *parent;
51+
span->span_monotonic_timestamp_ns = bpf_ktime_get_ns();
52+
span->span_duration_ns = 0;
53+
}
54+
55+
#define submit_span_tagged_base(map, type, parent, fill) \
56+
type *span = bpf_ringbuf_reserve(map, sizeof(type), 0); \
57+
if (!span) { \
58+
return 0; \
59+
} \
60+
\
61+
fill_span_base_tagged(&span->span_base, parent); \
62+
\
63+
fill; \
64+
\
65+
bpf_ringbuf_submit(span, 0);

0 commit comments

Comments
 (0)