blob: e94f9e1bb71893dfb3efb10c9fc8fbd7a32b4105 [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 Feng75b410b2018-10-10 15:01:19 -070046int mapRetrieve(const char* pathname, uint32_t flags);
47
Chenbo Feng79b7e612018-12-11 12:24:23 -080048enum class BpfLevel {
49 // Devices shipped before P or kernel version is lower than 4.9 do not
50 // have eBPF enabled.
51 NONE,
52 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
53 // functionalities such as xt_bpf and cgroup skb filter.
54 BASIC,
55 // For devices that have 4.14 kernel. It supports advanced features like
56 // map_in_map and cgroup socket filter.
57 EXTENDED,
58};
59
Chenbo Feng75b410b2018-10-10 15:01:19 -070060#ifndef DEFAULT_OVERFLOWUID
61#define DEFAULT_OVERFLOWUID 65534
62#endif
63
Chenbo Feng75b410b2018-10-10 15:01:19 -070064constexpr const int OVERFLOW_COUNTERSET = 2;
65
66constexpr const uint64_t NONEXISTENT_COOKIE = 0;
67
68constexpr const int MINIMUM_API_REQUIRED = 28;
69
Chenbo Feng1f20ad32018-11-26 15:18:46 -080070int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
71 uint32_t map_flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -070072int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags);
73int findMapEntry(const base::unique_fd& map_fd, void* key, void* value);
74int deleteMapEntry(const base::unique_fd& map_fd, void* key);
75int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key);
76int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey);
Chenbo Feng75b410b2018-10-10 15:01:19 -070077int bpfFdPin(const base::unique_fd& map_fd, const char* pathname);
Chenbo Fengc1dd7642018-12-22 11:41:20 -080078int bpfFdGet(const char* pathname, uint32_t flags);
Chenbo Feng75b410b2018-10-10 15:01:19 -070079int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd);
80int detachProgram(bpf_attach_type type, uint32_t cg_fd);
81uint64_t getSocketCookie(int sockFd);
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070082int setrlimitForTest();
Chenbo Feng79b7e612018-12-11 12:24:23 -080083std::string BpfLevelToString(BpfLevel BpfLevel);
84BpfLevel getBpfSupportLevel();
Chenbo Feng9cd8f142018-12-04 16:54:56 -080085int synchronizeKernelRCU();
Chenbo Feng75b410b2018-10-10 15:01:19 -070086
Chenbo Feng79b7e612018-12-11 12:24:23 -080087#define SKIP_IF_BPF_NOT_SUPPORTED \
88 do { \
89 if (android::bpf::getBpfSupportLevel() == android::bpf::BpfLevel::NONE) { \
90 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
91 return; \
92 } \
93 } while (0)
94
95#define SKIP_IF_BPF_SUPPORTED \
96 do { \
97 if (android::bpf::getBpfSupportLevel() != android::bpf::BpfLevel::NONE) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -070098 } while (0)
99
Chenbo Feng75b410b2018-10-10 15:01:19 -0700100} // namespace bpf
101} // namespace android
102
103#endif