blob: cf6e1ac38004845669a8d3276f7c039cbcf6fb3b [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
29#include "android-base/unique_fd.h"
30#include "netdutils/Slice.h"
31#include "netdutils/StatusOr.h"
32
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
143static inline int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
144 return bpf(BPF_PROG_ATTACH, {
145 .target_fd = cg_fd,
146 .attach_bpf_fd = prog_fd,
147 .attach_type = type,
148 });
149}
150
151static inline int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
152 return bpf(BPF_PROG_DETACH, {
153 .target_fd = cg_fd,
154 .attach_type = type,
155 });
156}
157
Chenbo Feng75b410b2018-10-10 15:01:19 -0700158uint64_t getSocketCookie(int sockFd);
Chenbo Feng0a1a9a12019-04-09 12:05:04 -0700159int setrlimitForTest();
Chenbo Feng79b7e612018-12-11 12:24:23 -0800160std::string BpfLevelToString(BpfLevel BpfLevel);
161BpfLevel getBpfSupportLevel();
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800162int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700163
Chenbo Feng79b7e612018-12-11 12:24:23 -0800164#define SKIP_IF_BPF_NOT_SUPPORTED \
165 do { \
166 if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \
167 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
168 return; \
169 } \
170 } while (0)
171
172#define SKIP_IF_BPF_SUPPORTED \
173 do { \
174 if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -0700175 } while (0)
176
Chenbo Feng75b410b2018-10-10 15:01:19 -0700177} // namespace bpf
178} // namespace android
179
180#endif