Loading...
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2020 Facebook
3
4#include <linux/bpf.h>
5#include <bpf/bpf_helpers.h>
6
7char _license[] SEC("license") = "GPL";
8
9struct sample {
10 int pid;
11 int seq;
12 long value;
13 char comm[16];
14};
15
16struct ringbuf_map {
17 __uint(type, BPF_MAP_TYPE_RINGBUF);
18 /* libbpf will adjust to valid page size */
19 __uint(max_entries, 1000);
20} ringbuf1 SEC(".maps"),
21 ringbuf2 SEC(".maps");
22
23struct {
24 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
25 __uint(max_entries, 4);
26 __type(key, int);
27 __array(values, struct ringbuf_map);
28} ringbuf_arr SEC(".maps") = {
29 .values = {
30 [0] = &ringbuf1,
31 [2] = &ringbuf2,
32 },
33};
34
35struct {
36 __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
37 __uint(max_entries, 1);
38 __type(key, int);
39 __array(values, struct ringbuf_map);
40} ringbuf_hash SEC(".maps") = {
41 .values = {
42 [0] = &ringbuf1,
43 },
44};
45
46/* inputs */
47int pid = 0;
48int target_ring = 0;
49long value = 0;
50
51/* outputs */
52long total = 0;
53long dropped = 0;
54long skipped = 0;
55
56SEC("tp/syscalls/sys_enter_getpgid")
57int test_ringbuf(void *ctx)
58{
59 int cur_pid = bpf_get_current_pid_tgid() >> 32;
60 struct sample *sample;
61 void *rb;
62
63 if (cur_pid != pid)
64 return 0;
65
66 rb = bpf_map_lookup_elem(&ringbuf_arr, &target_ring);
67 if (!rb) {
68 skipped += 1;
69 return 1;
70 }
71
72 sample = bpf_ringbuf_reserve(rb, sizeof(*sample), 0);
73 if (!sample) {
74 dropped += 1;
75 return 1;
76 }
77
78 sample->pid = pid;
79 bpf_get_current_comm(sample->comm, sizeof(sample->comm));
80 sample->value = value;
81
82 sample->seq = total;
83 total += 1;
84
85 bpf_ringbuf_submit(sample, 0);
86
87 return 0;
88}
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2020 Facebook
3
4#include <linux/bpf.h>
5#include <bpf/bpf_helpers.h>
6
7char _license[] SEC("license") = "GPL";
8
9struct sample {
10 int pid;
11 int seq;
12 long value;
13 char comm[16];
14};
15
16struct ringbuf_map {
17 __uint(type, BPF_MAP_TYPE_RINGBUF);
18} ringbuf1 SEC(".maps"),
19 ringbuf2 SEC(".maps");
20
21struct {
22 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
23 __uint(max_entries, 4);
24 __type(key, int);
25 __array(values, struct ringbuf_map);
26} ringbuf_arr SEC(".maps") = {
27 .values = {
28 [0] = &ringbuf1,
29 [2] = &ringbuf2,
30 },
31};
32
33struct {
34 __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
35 __uint(max_entries, 1);
36 __type(key, int);
37 __array(values, struct ringbuf_map);
38} ringbuf_hash SEC(".maps") = {
39 .values = {
40 [0] = &ringbuf1,
41 },
42};
43
44/* inputs */
45int pid = 0;
46int target_ring = 0;
47long value = 0;
48
49/* outputs */
50long total = 0;
51long dropped = 0;
52long skipped = 0;
53
54SEC("tp/syscalls/sys_enter_getpgid")
55int test_ringbuf(void *ctx)
56{
57 int cur_pid = bpf_get_current_pid_tgid() >> 32;
58 struct sample *sample;
59 void *rb;
60 int zero = 0;
61
62 if (cur_pid != pid)
63 return 0;
64
65 rb = bpf_map_lookup_elem(&ringbuf_arr, &target_ring);
66 if (!rb) {
67 skipped += 1;
68 return 1;
69 }
70
71 sample = bpf_ringbuf_reserve(rb, sizeof(*sample), 0);
72 if (!sample) {
73 dropped += 1;
74 return 1;
75 }
76
77 sample->pid = pid;
78 bpf_get_current_comm(sample->comm, sizeof(sample->comm));
79 sample->value = value;
80
81 sample->seq = total;
82 total += 1;
83
84 bpf_ringbuf_submit(sample, 0);
85
86 return 0;
87}