1
2
3
4#include <stdint.h>
5#include <stddef.h>
6#include <stdbool.h>
7#include <linux/bpf.h>
8#include <linux/ptrace.h>
9#include <linux/sched.h>
10#include <linux/types.h>
11#include "bpf_helpers.h"
12
13typedef uint32_t pid_t;
14struct task_struct {};
15
16#define TASK_COMM_LEN 16
17#define PERF_MAX_STACK_DEPTH 127
18
19#define STROBE_TYPE_INVALID 0
20#define STROBE_TYPE_INT 1
21#define STROBE_TYPE_STR 2
22#define STROBE_TYPE_MAP 3
23
24#define STACK_TABLE_EPOCH_SHIFT 20
25#define STROBE_MAX_STR_LEN 1
26#define STROBE_MAX_CFGS 32
27#define STROBE_MAX_PAYLOAD \
28 (STROBE_MAX_STRS * STROBE_MAX_STR_LEN + \
29 STROBE_MAX_MAPS * (1 + STROBE_MAX_MAP_ENTRIES * 2) * STROBE_MAX_STR_LEN)
30
31struct strobe_value_header {
32
33
34
35
36
37
38
39 uint16_t len;
40
41
42
43
44
45 uint8_t _reserved[6];
46};
47
48
49
50
51
52struct strobe_value_generic {
53 struct strobe_value_header header;
54 union {
55 int64_t val;
56 void *ptr;
57 };
58};
59
60struct strobe_value_int {
61 struct strobe_value_header header;
62 int64_t value;
63};
64
65struct strobe_value_str {
66 struct strobe_value_header header;
67 const char* value;
68};
69
70struct strobe_value_map {
71 struct strobe_value_header header;
72 const struct strobe_map_raw* value;
73};
74
75struct strobe_map_entry {
76 const char* key;
77 const char* val;
78};
79
80
81
82
83
84
85
86
87
88struct strobe_map_raw {
89
90
91
92
93
94
95 int64_t id;
96
97 int64_t cnt;
98
99
100
101
102
103 const char* tag;
104
105
106
107
108 struct strobe_map_entry entries[STROBE_MAX_MAP_ENTRIES];
109};
110
111
112#define TLS_NOT_SET -1
113#define TLS_LOCAL_EXEC 0
114#define TLS_IMM_EXEC 1
115#define TLS_GENERAL_DYN 2
116
117
118
119
120
121struct strobe_value_loc {
122
123
124
125
126
127
128
129
130
131
132 int64_t tls_mode;
133
134
135
136
137
138
139
140
141 int64_t offset;
142};
143
144struct strobemeta_cfg {
145 int64_t req_meta_idx;
146 struct strobe_value_loc int_locs[STROBE_MAX_INTS];
147 struct strobe_value_loc str_locs[STROBE_MAX_STRS];
148 struct strobe_value_loc map_locs[STROBE_MAX_MAPS];
149};
150
151struct strobe_map_descr {
152 uint64_t id;
153 int16_t tag_len;
154
155
156
157
158 int16_t cnt;
159
160
161
162
163 uint16_t key_lens[STROBE_MAX_MAP_ENTRIES];
164 uint16_t val_lens[STROBE_MAX_MAP_ENTRIES];
165};
166
167struct strobemeta_payload {
168
169 int64_t req_id;
170 uint8_t req_meta_valid;
171
172
173
174
175 uint64_t int_vals_set_mask;
176 int64_t int_vals[STROBE_MAX_INTS];
177
178 uint16_t str_lens[STROBE_MAX_STRS];
179
180 struct strobe_map_descr map_descrs[STROBE_MAX_MAPS];
181
182
183
184
185
186
187 char payload[STROBE_MAX_PAYLOAD];
188};
189
190struct strobelight_bpf_sample {
191 uint64_t ktime;
192 char comm[TASK_COMM_LEN];
193 pid_t pid;
194 int user_stack_id;
195 int kernel_stack_id;
196 int has_meta;
197 struct strobemeta_payload metadata;
198
199
200
201
202
203
204 char dummy_safeguard;
205};
206
207struct {
208 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
209 __uint(max_entries, 32);
210 __uint(key_size, sizeof(int));
211 __uint(value_size, sizeof(int));
212} samples SEC(".maps");
213
214struct {
215 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
216 __uint(max_entries, 16);
217 __uint(key_size, sizeof(uint32_t));
218 __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
219} stacks_0 SEC(".maps");
220
221struct {
222 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
223 __uint(max_entries, 16);
224 __uint(key_size, sizeof(uint32_t));
225 __uint(value_size, sizeof(uint64_t) * PERF_MAX_STACK_DEPTH);
226} stacks_1 SEC(".maps");
227
228struct {
229 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
230 __uint(max_entries, 1);
231 __type(key, uint32_t);
232 __type(value, struct strobelight_bpf_sample);
233} sample_heap SEC(".maps");
234
235struct {
236 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
237 __uint(max_entries, STROBE_MAX_CFGS);
238 __type(key, pid_t);
239 __type(value, struct strobemeta_cfg);
240} strobemeta_cfgs SEC(".maps");
241
242
243
244typedef union dtv {
245 size_t counter;
246 struct {
247 void* val;
248 bool is_static;
249 } pointer;
250} dtv_t;
251
252
253
254struct tcbhead {
255 void* tcb;
256 dtv_t* dtv;
257};
258
259
260
261
262
263
264struct tls_index {
265 uint64_t module;
266 uint64_t offset;
267};
268
269static __always_inline void *calc_location(struct strobe_value_loc *loc,
270 void *tls_base)
271{
272
273
274
275
276
277
278
279
280
281
282
283
284 if (loc->tls_mode <= TLS_LOCAL_EXEC) {
285
286
287 void *addr = tls_base + loc->offset;
288
289
290 return (void *)((loc->tls_mode + 1) * (int64_t)addr);
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 struct tls_index tls_index;
309 dtv_t *dtv;
310 void *tls_ptr;
311
312 bpf_probe_read(&tls_index, sizeof(struct tls_index),
313 (void *)loc->offset);
314
315 if (tls_index.module > 0) {
316
317 bpf_probe_read(&dtv, sizeof(dtv),
318 &((struct tcbhead *)tls_base)->dtv);
319 dtv += tls_index.module;
320 } else {
321 dtv = NULL;
322 }
323 bpf_probe_read(&tls_ptr, sizeof(void *), dtv);
324
325 return tls_ptr && tls_ptr != (void *)-1
326 ? tls_ptr + tls_index.offset
327 : NULL;
328}
329
330static __always_inline void read_int_var(struct strobemeta_cfg *cfg,
331 size_t idx, void *tls_base,
332 struct strobe_value_generic *value,
333 struct strobemeta_payload *data)
334{
335 void *location = calc_location(&cfg->int_locs[idx], tls_base);
336 if (!location)
337 return;
338
339 bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
340 data->int_vals[idx] = value->val;
341 if (value->header.len)
342 data->int_vals_set_mask |= (1 << idx);
343}
344
345static __always_inline uint64_t read_str_var(struct strobemeta_cfg *cfg,
346 size_t idx, void *tls_base,
347 struct strobe_value_generic *value,
348 struct strobemeta_payload *data,
349 void *payload)
350{
351 void *location;
352 uint32_t len;
353
354 data->str_lens[idx] = 0;
355 location = calc_location(&cfg->str_locs[idx], tls_base);
356 if (!location)
357 return 0;
358
359 bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
360 len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN, value->ptr);
361
362
363
364
365
366
367
368 if (len > STROBE_MAX_STR_LEN)
369 return 0;
370
371 data->str_lens[idx] = len;
372 return len;
373}
374
375static __always_inline void *read_map_var(struct strobemeta_cfg *cfg,
376 size_t idx, void *tls_base,
377 struct strobe_value_generic *value,
378 struct strobemeta_payload *data,
379 void *payload)
380{
381 struct strobe_map_descr* descr = &data->map_descrs[idx];
382 struct strobe_map_raw map;
383 void *location;
384 uint32_t len;
385 int i;
386
387 descr->tag_len = 0;
388 descr->cnt = -1;
389
390 location = calc_location(&cfg->map_locs[idx], tls_base);
391 if (!location)
392 return payload;
393
394 bpf_probe_read(value, sizeof(struct strobe_value_generic), location);
395 if (bpf_probe_read(&map, sizeof(struct strobe_map_raw), value->ptr))
396 return payload;
397
398 descr->id = map.id;
399 descr->cnt = map.cnt;
400 if (cfg->req_meta_idx == idx) {
401 data->req_id = map.id;
402 data->req_meta_valid = 1;
403 }
404
405 len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN, map.tag);
406 if (len <= STROBE_MAX_STR_LEN) {
407 descr->tag_len = len;
408 payload += len;
409 }
410
411#ifdef NO_UNROLL
412#pragma clang loop unroll(disable)
413#else
414#pragma unroll
415#endif
416 for (int i = 0; i < STROBE_MAX_MAP_ENTRIES && i < map.cnt; ++i) {
417 descr->key_lens[i] = 0;
418 len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN,
419 map.entries[i].key);
420 if (len <= STROBE_MAX_STR_LEN) {
421 descr->key_lens[i] = len;
422 payload += len;
423 }
424 descr->val_lens[i] = 0;
425 len = bpf_probe_read_str(payload, STROBE_MAX_STR_LEN,
426 map.entries[i].val);
427 if (len <= STROBE_MAX_STR_LEN) {
428 descr->val_lens[i] = len;
429 payload += len;
430 }
431 }
432
433 return payload;
434}
435
436
437
438
439
440static __always_inline void *read_strobe_meta(struct task_struct *task,
441 struct strobemeta_payload *data)
442{
443 pid_t pid = bpf_get_current_pid_tgid() >> 32;
444 struct strobe_value_generic value = {0};
445 struct strobemeta_cfg *cfg;
446 void *tls_base, *payload;
447
448 cfg = bpf_map_lookup_elem(&strobemeta_cfgs, &pid);
449 if (!cfg)
450 return NULL;
451
452 data->int_vals_set_mask = 0;
453 data->req_meta_valid = 0;
454 payload = data->payload;
455
456
457
458
459 tls_base = (void *)task;
460
461#ifdef NO_UNROLL
462#pragma clang loop unroll(disable)
463#else
464#pragma unroll
465#endif
466 for (int i = 0; i < STROBE_MAX_INTS; ++i) {
467 read_int_var(cfg, i, tls_base, &value, data);
468 }
469#ifdef NO_UNROLL
470#pragma clang loop unroll(disable)
471#else
472#pragma unroll
473#endif
474 for (int i = 0; i < STROBE_MAX_STRS; ++i) {
475 payload += read_str_var(cfg, i, tls_base, &value, data, payload);
476 }
477#ifdef NO_UNROLL
478#pragma clang loop unroll(disable)
479#else
480#pragma unroll
481#endif
482 for (int i = 0; i < STROBE_MAX_MAPS; ++i) {
483 payload = read_map_var(cfg, i, tls_base, &value, data, payload);
484 }
485
486
487
488
489 return payload;
490}
491
492SEC("raw_tracepoint/kfree_skb")
493int on_event(struct pt_regs *ctx) {
494 pid_t pid = bpf_get_current_pid_tgid() >> 32;
495 struct strobelight_bpf_sample* sample;
496 struct task_struct *task;
497 uint32_t zero = 0;
498 uint64_t ktime_ns;
499 void *sample_end;
500
501 sample = bpf_map_lookup_elem(&sample_heap, &zero);
502 if (!sample)
503 return 0;
504
505 sample->pid = pid;
506 bpf_get_current_comm(&sample->comm, TASK_COMM_LEN);
507 ktime_ns = bpf_ktime_get_ns();
508 sample->ktime = ktime_ns;
509
510 task = (struct task_struct *)bpf_get_current_task();
511 sample_end = read_strobe_meta(task, &sample->metadata);
512 sample->has_meta = sample_end != NULL;
513 sample_end = sample_end ? : &sample->metadata;
514
515 if ((ktime_ns >> STACK_TABLE_EPOCH_SHIFT) & 1) {
516 sample->kernel_stack_id = bpf_get_stackid(ctx, &stacks_1, 0);
517 sample->user_stack_id = bpf_get_stackid(ctx, &stacks_1, BPF_F_USER_STACK);
518 } else {
519 sample->kernel_stack_id = bpf_get_stackid(ctx, &stacks_0, 0);
520 sample->user_stack_id = bpf_get_stackid(ctx, &stacks_0, BPF_F_USER_STACK);
521 }
522
523 uint64_t sample_size = sample_end - (void *)sample;
524
525 if (sample_size < sizeof(struct strobelight_bpf_sample))
526 bpf_perf_event_output(ctx, &samples, 0, sample, 1 + sample_size);
527 return 0;
528}
529
530char _license[] SEC("license") = "GPL";
531