• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
127 inline int attachProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd,
128                          const BPF_FD_TYPE cg_fd, uint32_t flags = 0) {
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                                         .attach_flags = flags,
134                                 });
135 }
136 
detachProgram(bpf_attach_type type,const BPF_FD_TYPE cg_fd)137 inline int detachProgram(bpf_attach_type type, const BPF_FD_TYPE cg_fd) {
138     return bpf(BPF_PROG_DETACH, {
139                                         .target_fd = BPF_FD_TO_U32(cg_fd),
140                                         .attach_type = type,
141                                 });
142 }
143 
detachSingleProgram(bpf_attach_type type,const BPF_FD_TYPE prog_fd,const BPF_FD_TYPE cg_fd)144 inline int detachSingleProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd,
145                                const BPF_FD_TYPE cg_fd) {
146     return bpf(BPF_PROG_DETACH, {
147                                         .target_fd = BPF_FD_TO_U32(cg_fd),
148                                         .attach_bpf_fd = BPF_FD_TO_U32(prog_fd),
149                                         .attach_type = type,
150                                 });
151 }
152 
153 // requires 4.14+ kernel
154 
155 #define DEFINE_BPF_GET_FD_INFO(NAME, FIELD) \
156 inline int bpfGetFd ## NAME(const BPF_FD_TYPE map_fd) { \
157     struct bpf_map_info map_info = {}; \
158     union bpf_attr attr = { .info = { \
159         .bpf_fd = BPF_FD_TO_U32(map_fd), \
160         .info_len = sizeof(map_info), \
161         .info = ptr_to_u64(&map_info), \
162     }}; \
163     int rv = bpf(BPF_OBJ_GET_INFO_BY_FD, attr); \
164     if (rv) return rv; \
165     if (attr.info.info_len < offsetof(bpf_map_info, FIELD) + sizeof(map_info.FIELD)) { \
166         errno = EOPNOTSUPP; \
167         return -1; \
168     }; \
169     return map_info.FIELD; \
170 }
171 
172 // All 6 of these fields are already present in Linux v4.14 (even ACK 4.14-P)
173 // while BPF_OBJ_GET_INFO_BY_FD is not implemented at all in v4.9 (even ACK 4.9-Q)
174 DEFINE_BPF_GET_FD_INFO(MapType, type)            // int bpfGetFdMapType(const BPF_FD_TYPE map_fd)
175 DEFINE_BPF_GET_FD_INFO(MapId, id)                // int bpfGetFdMapId(const BPF_FD_TYPE map_fd)
176 DEFINE_BPF_GET_FD_INFO(KeySize, key_size)        // int bpfGetFdKeySize(const BPF_FD_TYPE map_fd)
177 DEFINE_BPF_GET_FD_INFO(ValueSize, value_size)    // int bpfGetFdValueSize(const BPF_FD_TYPE map_fd)
178 DEFINE_BPF_GET_FD_INFO(MaxEntries, max_entries)  // int bpfGetFdMaxEntries(const BPF_FD_TYPE map_fd)
179 DEFINE_BPF_GET_FD_INFO(MapFlags, map_flags)      // int bpfGetFdMapFlags(const BPF_FD_TYPE map_fd)
180 
181 #undef DEFINE_BPF_GET_FD_INFO
182 
183 }  // namespace bpf
184 }  // namespace android
185 
186 #undef ptr_to_u64
187 #undef BPF_FD_TO_U32
188 #undef BPF_FD_TYPE
189 #undef BPF_FD_JUST_USE_INT
190