xref: /aosp_15_r20/system/bpf/progs/bpfRingbufProg.c (revision 55039e042b8390f50b0bdd70c11a2419f6d8fd50)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "bpf_helpers.h"
18 
19 // This can't be easily changed since the program is loaded on boot and may be
20 // run against tests at a slightly different version.
21 #define TEST_RINGBUF_MAGIC_NUM 12345
22 
23 // This ring buffer is for testing purposes only.
24 DEFINE_BPF_RINGBUF(test_ringbuf, __u64, 4096, AID_ROOT, AID_ROOT, 0660);
25 
26 // This program is for test purposes only - it should never be attached to a
27 // socket, only executed manually with BPF_PROG_RUN.
28 DEFINE_BPF_PROG_KVER("skfilter/ringbuf_test", AID_ROOT, AID_ROOT, test_ringbuf_prog, KVER(5, 8, 0))
29 (void* __unused ctx) {
30     __u64* output = bpf_test_ringbuf_reserve();
31     if (output == NULL) return 1;
32 
33     (*output) = TEST_RINGBUF_MAGIC_NUM;
34     bpf_test_ringbuf_submit(output);
35 
36     return 0;
37 }
38 
39 LICENSE("Apache 2.0");
40 CRITICAL("BPF Ringbuf test");
41