Dmitri Plotnikov | bb05a5c | 2020-10-22 17:12:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #pragma once |
| 18 | |
| 19 | /* Mock BPF helpers to be used for testing of BPF programs loaded by Android */ |
| 20 | |
| 21 | #include <linux/bpf.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | typedef void* mock_bpf_map_t; |
| 26 | |
| 27 | /* type safe macro to declare a map and related accessor functions */ |
| 28 | #define DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, usr, grp, md) \ |
| 29 | mock_bpf_map_t mock_bpf_map_##the_map; \ |
| 30 | \ |
| 31 | mock_bpf_map_t get_mock_bpf_map_##the_map() { \ |
| 32 | if (mock_bpf_map_##the_map == 0) { \ |
| 33 | mock_bpf_map_##the_map = mock_bpf_map_create(sizeof(TypeOfKey), sizeof(TypeOfValue), \ |
| 34 | BPF_MAP_TYPE_##TYPE); \ |
| 35 | } \ |
| 36 | return mock_bpf_map_##the_map; \ |
| 37 | } \ |
| 38 | __unused TypeOfValue* bpf_##the_map##_lookup_elem(const TypeOfKey* k) { \ |
| 39 | return (TypeOfValue*)mock_bpf_lookup_elem(get_mock_bpf_map_##the_map(), (void*)k); \ |
| 40 | }; \ |
| 41 | \ |
| 42 | __unused int bpf_##the_map##_update_elem(const TypeOfKey* k, const TypeOfValue* v, \ |
| 43 | uint64_t flags) { \ |
| 44 | return mock_bpf_update_elem(get_mock_bpf_map_##the_map(), (void*)k, (void*)v, flags); \ |
| 45 | }; \ |
| 46 | \ |
| 47 | __unused int bpf_##the_map##_delete_elem(const TypeOfKey* k) { \ |
| 48 | return mock_bpf_delete_elem(get_mock_bpf_map_##the_map(), (void*)k); \ |
| 49 | }; |
| 50 | |
| 51 | #define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ |
| 52 | DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, AID_ROOT, 0600) |
| 53 | |
| 54 | #define DEFINE_BPF_MAP_GWO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ |
| 55 | DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0620) |
| 56 | |
| 57 | #define DEFINE_BPF_MAP_GRO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ |
| 58 | DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0640) |
| 59 | |
| 60 | #define DEFINE_BPF_MAP_GRW(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ |
| 61 | DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0660) |
| 62 | |
| 63 | #define DEFINE_BPF_PROG(section, owner, group, name) int name |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | extern "C" { |
| 67 | #endif |
| 68 | |
| 69 | mock_bpf_map_t mock_bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t type); |
| 70 | void* mock_bpf_lookup_elem(mock_bpf_map_t map, void* key); |
| 71 | int mock_bpf_update_elem(mock_bpf_map_t map, void* key, void* value, uint64_t flags); |
| 72 | int mock_bpf_delete_elem(mock_bpf_map_t map, void* key); |
| 73 | |
| 74 | uint64_t bpf_ktime_get_ns(); |
| 75 | uint64_t bpf_get_smp_processor_id(); |
| 76 | uint64_t bpf_get_current_uid_gid(); |
| 77 | uint64_t bpf_get_current_pid_tgid(); |
| 78 | |
| 79 | void mock_bpf_set_ktime_ns(uint64_t time_ns); |
| 80 | void mock_bpf_set_smp_processor_id(uint32_t cpu); |
| 81 | void mock_bpf_set_current_uid_gid(uint32_t uid); |
| 82 | void mock_bpf_set_current_pid_tgid(uint64_t pid_tgid); |
| 83 | |
| 84 | #ifdef __cplusplus |
| 85 | } // extern "C" |
| 86 | #endif |
| 87 | |
| 88 | /* place things in different elf sections */ |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame^] | 89 | #define SECTION(NAME) __attribute__((section(NAME), used)) |
Dmitri Plotnikov | bb05a5c | 2020-10-22 17:12:44 -0700 | [diff] [blame] | 90 | |
| 91 | /* Example use: LICENSE("GPL"); or LICENSE("Apache 2.0"); */ |
Maciej Żenczykowski | 3adb1d5 | 2021-10-22 19:27:10 -0700 | [diff] [blame^] | 92 | #define LICENSE(NAME) char _license[] SECTION("license") = (NAME) |