blob: 965840f68b3d9c3e9c795bab9677d2b4a85c62d6 [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
2 * Copyright (C) 2017 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#ifndef BPF_BPFUTILS_H
18#define BPF_BPFUTILS_H
19
20#include <linux/bpf.h>
21#include <linux/if_ether.h>
22#include <linux/in.h>
23#include <linux/unistd.h>
24#include <net/if.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/socket.h>
28
Steven Morelande7cd2a72020-01-10 17:49:35 -080029#include <string>
30
Chenbo Feng75b410b2018-10-10 15:01:19 -070031#include "android-base/unique_fd.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070032
33#define BPF_PASS 1
34#define BPF_DROP 0
35
36#define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x))
37#define DEFAULT_LOG_LEVEL 1
38
Chenbo Feng75b410b2018-10-10 15:01:19 -070039#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
40
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070041#define TEST_LIMIT 8388608
42
Chenbo Feng75b410b2018-10-10 15:01:19 -070043namespace android {
44namespace bpf {
45
Chenbo Feng79b7e612018-12-11 12:24:23 -080046enum class BpfLevel {
47 // Devices shipped before P or kernel version is lower than 4.9 do not
48 // have eBPF enabled.
49 NONE,
50 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
51 // functionalities such as xt_bpf and cgroup skb filter.
52 BASIC,
53 // For devices that have 4.14 kernel. It supports advanced features like
54 // map_in_map and cgroup socket filter.
55 EXTENDED,
56};
57
Chenbo Feng75b410b2018-10-10 15:01:19 -070058#ifndef DEFAULT_OVERFLOWUID
59#define DEFAULT_OVERFLOWUID 65534
60#endif
61
Chenbo Feng75b410b2018-10-10 15:01:19 -070062constexpr const int OVERFLOW_COUNTERSET = 2;
63
64constexpr const uint64_t NONEXISTENT_COOKIE = 0;
65
66constexpr const int MINIMUM_API_REQUIRED = 28;
67
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080068/* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
69 * of it that we are using. The kernel's bpf() system call will perform a strict check to ensure
70 * all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr.
71 */
72
73static inline int bpf(int cmd, const bpf_attr& attr) {
74 return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
75}
76
77static inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
78 uint32_t max_entries, uint32_t map_flags) {
79 return bpf(BPF_MAP_CREATE, {
80 .map_type = map_type,
81 .key_size = key_size,
82 .value_size = value_size,
83 .max_entries = max_entries,
84 .map_flags = map_flags,
85 });
86}
87
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080088static inline int writeToMapEntry(const base::unique_fd& map_fd, const void* key, const void* value,
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080089 uint64_t flags) {
90 return bpf(BPF_MAP_UPDATE_ELEM, {
91 .map_fd = static_cast<__u32>(map_fd.get()),
92 .key = ptr_to_u64(key),
93 .value = ptr_to_u64(value),
94 .flags = flags,
95 });
96}
97
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080098static inline int findMapEntry(const base::unique_fd& map_fd, const void* key, void* value) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080099 return bpf(BPF_MAP_LOOKUP_ELEM, {
100 .map_fd = static_cast<__u32>(map_fd.get()),
101 .key = ptr_to_u64(key),
102 .value = ptr_to_u64(value),
103 });
104}
105
Maciej Żenczykowski79365da2020-01-17 19:07:37 -0800106static inline int deleteMapEntry(const base::unique_fd& map_fd, const void* key) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800107 return bpf(BPF_MAP_DELETE_ELEM, {
108 .map_fd = static_cast<__u32>(map_fd.get()),
109 .key = ptr_to_u64(key),
110 });
111}
112
Maciej Żenczykowski79365da2020-01-17 19:07:37 -0800113static inline int getNextMapKey(const base::unique_fd& map_fd, const void* key, void* next_key) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800114 return bpf(BPF_MAP_GET_NEXT_KEY, {
115 .map_fd = static_cast<__u32>(map_fd.get()),
116 .key = ptr_to_u64(key),
117 .next_key = ptr_to_u64(next_key),
118 });
119}
120
121static inline int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
122 return getNextMapKey(map_fd, NULL, firstKey);
123}
124
125static inline int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) {
126 return bpf(BPF_OBJ_PIN, {
127 .pathname = ptr_to_u64(pathname),
128 .bpf_fd = static_cast<__u32>(map_fd.get()),
129 });
130}
131
132static inline int bpfFdGet(const char* pathname, uint32_t flag) {
133 return bpf(BPF_OBJ_GET, {
134 .pathname = ptr_to_u64(pathname),
135 .file_flags = flag,
136 });
137}
138
139static inline int mapRetrieve(const char* pathname, uint32_t flag) {
140 return bpfFdGet(pathname, flag);
141}
142
Maciej Żenczykowski289742f2020-01-17 19:18:26 -0800143static inline int attachProgram(bpf_attach_type type, const base::unique_fd& prog_fd,
144 const base::unique_fd& cg_fd) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800145 return bpf(BPF_PROG_ATTACH, {
Maciej Żenczykowski289742f2020-01-17 19:18:26 -0800146 .target_fd = static_cast<__u32>(cg_fd.get()),
147 .attach_bpf_fd = static_cast<__u32>(prog_fd.get()),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800148 .attach_type = type,
149 });
150}
151
Maciej Żenczykowski289742f2020-01-17 19:18:26 -0800152static inline int detachProgram(bpf_attach_type type, const base::unique_fd& cg_fd) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800153 return bpf(BPF_PROG_DETACH, {
Maciej Żenczykowski289742f2020-01-17 19:18:26 -0800154 .target_fd = static_cast<__u32>(cg_fd.get()),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800155 .attach_type = type,
156 });
157}
158
Chenbo Feng75b410b2018-10-10 15:01:19 -0700159uint64_t getSocketCookie(int sockFd);
Chenbo Feng0a1a9a12019-04-09 12:05:04 -0700160int setrlimitForTest();
Chenbo Feng79b7e612018-12-11 12:24:23 -0800161std::string BpfLevelToString(BpfLevel BpfLevel);
162BpfLevel getBpfSupportLevel();
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800163int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700164
Chenbo Feng79b7e612018-12-11 12:24:23 -0800165#define SKIP_IF_BPF_NOT_SUPPORTED \
166 do { \
167 if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \
168 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
169 return; \
170 } \
171 } while (0)
172
173#define SKIP_IF_BPF_SUPPORTED \
174 do { \
175 if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -0700176 } while (0)
177
Chenbo Feng75b410b2018-10-10 15:01:19 -0700178} // namespace bpf
179} // namespace android
180
181#endif