blob: 9a74fe746b57a9ea8b0ad098d86b82b51ea11abd [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
Chenbo Feng75b410b2018-10-10 15:01:19 -070020#include <linux/if_ether.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070021#include <net/if.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
25
Steven Morelande7cd2a72020-01-10 17:49:35 -080026#include <string>
27
Hungming Chena46f2172021-01-13 13:56:38 +080028#include "BpfSyscallWrappers.h"
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070029
Chenbo Feng75b410b2018-10-10 15:01:19 -070030namespace android {
31namespace bpf {
32
Chenbo Feng79b7e612018-12-11 12:24:23 -080033enum class BpfLevel {
34 // Devices shipped before P or kernel version is lower than 4.9 do not
35 // have eBPF enabled.
36 NONE,
37 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
38 // functionalities such as xt_bpf and cgroup skb filter.
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -080039 BASIC_4_9,
Chenbo Feng79b7e612018-12-11 12:24:23 -080040 // For devices that have 4.14 kernel. It supports advanced features like
41 // map_in_map and cgroup socket filter.
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -080042 EXTENDED_4_14,
43 EXTENDED_4_19,
44 EXTENDED_5_4,
Chenbo Feng79b7e612018-12-11 12:24:23 -080045};
46
Chenbo Feng75b410b2018-10-10 15:01:19 -070047constexpr const int OVERFLOW_COUNTERSET = 2;
48
49constexpr const uint64_t NONEXISTENT_COOKIE = 0;
50
51constexpr const int MINIMUM_API_REQUIRED = 28;
52
Chenbo Feng75b410b2018-10-10 15:01:19 -070053uint64_t getSocketCookie(int sockFd);
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -080054int synchronizeKernelRCU();
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070055int setrlimitForTest();
Maciej Żenczykowski07375e22020-02-19 14:23:59 -080056unsigned kernelVersion();
Chenbo Feng79b7e612018-12-11 12:24:23 -080057BpfLevel getBpfSupportLevel();
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -080058
Maciej Żenczykowski06caf872020-02-11 16:42:21 -080059inline bool isBpfSupported() {
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -080060 return getBpfSupportLevel() != BpfLevel::NONE;
61}
Chenbo Feng75b410b2018-10-10 15:01:19 -070062
Chenbo Feng79b7e612018-12-11 12:24:23 -080063#define SKIP_IF_BPF_NOT_SUPPORTED \
64 do { \
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -080065 if (!android::bpf::isBpfSupported()) { \
Chenbo Feng79b7e612018-12-11 12:24:23 -080066 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
67 return; \
68 } \
69 } while (0)
70
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -080071#define SKIP_IF_BPF_SUPPORTED \
72 do { \
73 if (android::bpf::isBpfSupported()) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -070074 } while (0)
75
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -080076#define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \
77 do { \
78 if (android::bpf::getBpfSupportLevel() < android::bpf::BpfLevel::EXTENDED_4_14) { \
79 GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \
80 << "not supported\n"; \
81 return; \
82 } \
Maciej Żenczykowski672b0e72020-02-12 04:15:20 -080083 } while (0)
84
Chenbo Feng75b410b2018-10-10 15:01:19 -070085} // namespace bpf
86} // namespace android
87
88#endif