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 #include <cutils/android_filesystem_config.h> 26 27 typedef void* mock_bpf_map_t; 28 29 /* type safe macro to declare a map and related accessor functions */ 30 #define DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, usr, grp, md) \ 31 mock_bpf_map_t mock_bpf_map_##the_map; \ 32 \ 33 mock_bpf_map_t get_mock_bpf_map_##the_map() { \ 34 if (mock_bpf_map_##the_map == 0) { \ 35 mock_bpf_map_##the_map = mock_bpf_map_create(sizeof(TypeOfKey), sizeof(TypeOfValue), \ 36 BPF_MAP_TYPE_##TYPE); \ 37 } \ 38 return mock_bpf_map_##the_map; \ 39 } \ 40 __unused TypeOfValue* bpf_##the_map##_lookup_elem(const TypeOfKey* k) { \ 41 return (TypeOfValue*)mock_bpf_lookup_elem(get_mock_bpf_map_##the_map(), (void*)k); \ 42 }; \ 43 \ 44 __unused int bpf_##the_map##_update_elem(const TypeOfKey* k, const TypeOfValue* v, \ 45 uint64_t flags) { \ 46 return mock_bpf_update_elem(get_mock_bpf_map_##the_map(), (void*)k, (void*)v, flags); \ 47 }; \ 48 \ 49 __unused int bpf_##the_map##_delete_elem(const TypeOfKey* k) { \ 50 return mock_bpf_delete_elem(get_mock_bpf_map_##the_map(), (void*)k); \ 51 }; 52 53 #define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \ 54 DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, AID_ROOT, 0600) 55 56 #define DEFINE_BPF_MAP_GWO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ 57 DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0620) 58 59 #define DEFINE_BPF_MAP_GRO(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ 60 DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0640) 61 62 #define DEFINE_BPF_MAP_GRW(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, gid) \ 63 DEFINE_BPF_MAP_UGM(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries, AID_ROOT, gid, 0660) 64 65 #define DEFINE_BPF_PROG(section, owner, group, name) int name 66 67 #ifdef __cplusplus 68 extern "C" { 69 #endif 70 71 mock_bpf_map_t mock_bpf_map_create(uint32_t key_size, uint32_t value_size, uint32_t type); 72 void* mock_bpf_lookup_elem(mock_bpf_map_t map, void* key); 73 int mock_bpf_update_elem(mock_bpf_map_t map, void* key, void* value, uint64_t flags); 74 int mock_bpf_delete_elem(mock_bpf_map_t map, void* key); 75 76 uint64_t bpf_ktime_get_ns(); 77 uint64_t bpf_get_smp_processor_id(); 78 uint64_t bpf_get_current_uid_gid(); 79 uint64_t bpf_get_current_pid_tgid(); 80 81 void mock_bpf_set_ktime_ns(uint64_t time_ns); 82 void mock_bpf_set_smp_processor_id(uint32_t cpu); 83 void mock_bpf_set_current_uid_gid(uint32_t uid); 84 void mock_bpf_set_current_pid_tgid(uint64_t pid_tgid); 85 86 #ifdef __cplusplus 87 } // extern "C" 88 #endif 89 90 /* place things in different elf sections */ 91 #define SECTION(NAME) __attribute__((section(NAME), used)) 92 93 /* Example use: LICENSE("GPL"); or LICENSE("Apache 2.0"); */ 94 #define LICENSE(NAME) char _license[] SECTION("license") = (NAME) 95