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 #include <linux/bpf.h>
20 #include <linux/unistd.h>
21
22 #ifdef BPF_FD_JUST_USE_INT
23 #define BPF_FD_TYPE int
24 #define BPF_FD_TO_U32(x) static_cast<__u32>(x)
25 #else
26 #include <android-base/unique_fd.h>
27 #define BPF_FD_TYPE base::unique_fd&
28 #define BPF_FD_TO_U32(x) static_cast<__u32>((x).get())
29 #endif
30
31 #define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x))
32
33 namespace android {
34 namespace bpf {
35
36 /* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
37 * of it that we are using. The kernel's bpf() system call will perform a strict check to ensure
38 * all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr.
39 */
40
bpf(int cmd,const bpf_attr & attr)41 inline int bpf(int cmd, const bpf_attr& attr) {
42 return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
43 }
44
createMap(bpf_map_type map_type,uint32_t key_size,uint32_t value_size,uint32_t max_entries,uint32_t map_flags)45 inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
46 uint32_t max_entries, uint32_t map_flags) {
47 return bpf(BPF_MAP_CREATE, {
48 .map_type = map_type,
49 .key_size = key_size,
50 .value_size = value_size,
51 .max_entries = max_entries,
52 .map_flags = map_flags,
53 });
54 }
55
writeToMapEntry(const BPF_FD_TYPE map_fd,const void * key,const void * value,uint64_t flags)56 inline int writeToMapEntry(const BPF_FD_TYPE map_fd, const void* key, const void* value,
57 uint64_t flags) {
58 return bpf(BPF_MAP_UPDATE_ELEM, {
59 .map_fd = BPF_FD_TO_U32(map_fd),
60 .key = ptr_to_u64(key),
61 .value = ptr_to_u64(value),
62 .flags = flags,
63 });
64 }
65
findMapEntry(const BPF_FD_TYPE map_fd,const void * key,void * value)66 inline int findMapEntry(const BPF_FD_TYPE map_fd, const void* key, void* value) {
67 return bpf(BPF_MAP_LOOKUP_ELEM, {
68 .map_fd = BPF_FD_TO_U32(map_fd),
69 .key = ptr_to_u64(key),
70 .value = ptr_to_u64(value),
71 });
72 }
73
deleteMapEntry(const BPF_FD_TYPE map_fd,const void * key)74 inline int deleteMapEntry(const BPF_FD_TYPE map_fd, const void* key) {
75 return bpf(BPF_MAP_DELETE_ELEM, {
76 .map_fd = BPF_FD_TO_U32(map_fd),
77 .key = ptr_to_u64(key),
78 });
79 }
80
getNextMapKey(const BPF_FD_TYPE map_fd,const void * key,void * next_key)81 inline int getNextMapKey(const BPF_FD_TYPE map_fd, const void* key, void* next_key) {
82 return bpf(BPF_MAP_GET_NEXT_KEY, {
83 .map_fd = BPF_FD_TO_U32(map_fd),
84 .key = ptr_to_u64(key),
85 .next_key = ptr_to_u64(next_key),
86 });
87 }
88
getFirstMapKey(const BPF_FD_TYPE map_fd,void * firstKey)89 inline int getFirstMapKey(const BPF_FD_TYPE map_fd, void* firstKey) {
90 return getNextMapKey(map_fd, NULL, firstKey);
91 }
92
bpfFdPin(const BPF_FD_TYPE map_fd,const char * pathname)93 inline int bpfFdPin(const BPF_FD_TYPE map_fd, const char* pathname) {
94 return bpf(BPF_OBJ_PIN, {
95 .pathname = ptr_to_u64(pathname),
96 .bpf_fd = BPF_FD_TO_U32(map_fd),
97 });
98 }
99
bpfFdGet(const char * pathname,uint32_t flag)100 inline int bpfFdGet(const char* pathname, uint32_t flag) {
101 return bpf(BPF_OBJ_GET, {
102 .pathname = ptr_to_u64(pathname),
103 .file_flags = flag,
104 });
105 }
106
mapRetrieve(const char * pathname,uint32_t flag)107 inline int mapRetrieve(const char* pathname, uint32_t flag) {
108 return bpfFdGet(pathname, flag);
109 }
110
mapRetrieveRW(const char * pathname)111 inline int mapRetrieveRW(const char* pathname) {
112 return mapRetrieve(pathname, 0);
113 }
114
mapRetrieveRO(const char * pathname)115 inline int mapRetrieveRO(const char* pathname) {
116 return mapRetrieve(pathname, BPF_F_RDONLY);
117 }
118
mapRetrieveWO(const char * pathname)119 inline int mapRetrieveWO(const char* pathname) {
120 return mapRetrieve(pathname, BPF_F_WRONLY);
121 }
122
retrieveProgram(const char * pathname)123 inline int retrieveProgram(const char* pathname) {
124 return bpfFdGet(pathname, BPF_F_RDONLY);
125 }
126
attachProgram(bpf_attach_type type,const BPF_FD_TYPE prog_fd,const BPF_FD_TYPE cg_fd)127 inline int attachProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd,
128 const BPF_FD_TYPE cg_fd) {
129 return bpf(BPF_PROG_ATTACH, {
130 .target_fd = BPF_FD_TO_U32(cg_fd),
131 .attach_bpf_fd = BPF_FD_TO_U32(prog_fd),
132 .attach_type = type,
133 });
134 }
135
detachProgram(bpf_attach_type type,const BPF_FD_TYPE cg_fd)136 inline int detachProgram(bpf_attach_type type, const BPF_FD_TYPE cg_fd) {
137 return bpf(BPF_PROG_DETACH, {
138 .target_fd = BPF_FD_TO_U32(cg_fd),
139 .attach_type = type,
140 });
141 }
142
143 } // namespace bpf
144 } // namespace android
145
146 #undef ptr_to_u64
147 #undef BPF_FD_TO_U32
148 #undef BPF_FD_TYPE
149 #undef BPF_FD_JUST_USE_INT
150