blob: 5d1bf5377ea26b8f5f1690567a880318a059bda9 [file] [log] [blame]
Maciej Żenczykowskib70da762019-01-28 15:20:48 -08001/*
2 * Copyright 2019 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 * ClatUtilsTest.cpp - unit tests for ClatUtils.cpp
17 */
18
19#include <gtest/gtest.h>
20
21#include "ClatUtils.h"
22
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080023#include <linux/if_arp.h>
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -070024#include <stdlib.h>
25#include <sys/wait.h>
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080026
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070027#include "bpf/BpfUtils.h"
28#include "netdbpf/bpf_shared.h"
29
Maciej Żenczykowskib70da762019-01-28 15:20:48 -080030namespace android {
31namespace net {
32
33class ClatUtilsTest : public ::testing::Test {
34 public:
35 void SetUp() {}
36};
37
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080038TEST_F(ClatUtilsTest, HardwareAddressTypeOfNonExistingIf) {
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070039 ASSERT_EQ(-ENODEV, hardwareAddressType("not_existing_if"));
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080040}
41
42TEST_F(ClatUtilsTest, HardwareAddressTypeOfLoopback) {
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070043 ASSERT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo"));
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080044}
45
46// If wireless 'wlan0' interface exists it should be Ethernet.
47TEST_F(ClatUtilsTest, HardwareAddressTypeOfWireless) {
48 int type = hardwareAddressType("wlan0");
49 if (type == -ENODEV) return;
50
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070051 ASSERT_EQ(ARPHRD_ETHER, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080052}
53
54// If cellular 'rmnet_data0' interface exists it should
55// *probably* not be Ethernet and instead be RawIp.
56TEST_F(ClatUtilsTest, HardwareAddressTypeOfCellular) {
57 int type = hardwareAddressType("rmnet_data0");
58 if (type == -ENODEV) return;
59
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070060 ASSERT_NE(ARPHRD_ETHER, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080061
62 // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices.
63 if (type == 530) return;
64
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070065 ASSERT_EQ(ARPHRD_RAWIP, type);
Maciej Żenczykowski0a7dce82019-01-28 15:31:55 -080066}
67
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080068TEST_F(ClatUtilsTest, GetClatEgressMapFd) {
69 SKIP_IF_BPF_NOT_SUPPORTED;
70
71 int fd = getClatEgressMapFd();
72 ASSERT_LE(3, fd); // 0,1,2 - stdin/out/err, thus 3 <= fd
73 close(fd);
74}
75
Maciej Żenczykowskie870a872019-12-15 13:56:13 -080076TEST_F(ClatUtilsTest, GetClatEgressRawIpProgFd) {
77 SKIP_IF_BPF_NOT_SUPPORTED;
78
79 int fd = getClatEgressProgFd(false);
80 ASSERT_LE(3, fd);
81 close(fd);
82}
83
84TEST_F(ClatUtilsTest, GetClatEgressEtherProgFd) {
85 SKIP_IF_BPF_NOT_SUPPORTED;
86
87 int fd = getClatEgressProgFd(true);
88 ASSERT_LE(3, fd);
89 close(fd);
90}
91
Maciej Żenczykowski4e36f132019-12-15 13:20:15 -080092TEST_F(ClatUtilsTest, GetClatIngressMapFd) {
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070093 SKIP_IF_BPF_NOT_SUPPORTED;
94
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -070095 int fd = getClatIngressMapFd();
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -070096 ASSERT_LE(3, fd); // 0,1,2 - stdin/out/err, thus 3 <= fd
97 close(fd);
Maciej Żenczykowski88d28ff2019-03-25 11:54:32 -070098}
99
Maciej Żenczykowskie870a872019-12-15 13:56:13 -0800100TEST_F(ClatUtilsTest, GetClatIngressRawIpProgFd) {
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -0800101 SKIP_IF_BPF_NOT_SUPPORTED;
102
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -0700103 int fd = getClatIngressProgFd(false);
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -0700104 ASSERT_LE(3, fd);
105 close(fd);
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -0800106}
107
Maciej Żenczykowskie870a872019-12-15 13:56:13 -0800108TEST_F(ClatUtilsTest, GetClatIngressEtherProgFd) {
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -0800109 SKIP_IF_BPF_NOT_SUPPORTED;
110
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -0700111 int fd = getClatIngressProgFd(true);
Maciej Żenczykowski10a2ed52019-03-27 15:46:26 -0700112 ASSERT_LE(3, fd);
113 close(fd);
Maciej Żenczykowski949d84a2019-01-28 17:22:30 -0800114}
115
Maciej Żenczykowski7330b022019-01-28 17:30:24 -0800116TEST_F(ClatUtilsTest, TryOpeningNetlinkSocket) {
117 int fd = openNetlinkSocket();
118 ASSERT_LE(3, fd);
119 close(fd);
120}
121
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700122// The SKIP_IF_BPF_NOT_SUPPORTED macro is effectively a check for 4.9+ kernel
123// combined with a launched on P device. Ie. it's a test for 4.9-P or better.
124
125// NET_SCH_INGRESS is only enabled starting with 4.9-Q and as such we need
126// a separate way to test for this...
127int doKernelSupportsNetSchIngress(void) {
Maciej Żenczykowski4e4aaff2019-04-09 02:51:35 -0700128 // NOLINTNEXTLINE(cert-env33-c)
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700129 return system("zcat /proc/config.gz | egrep -q '^CONFIG_NET_SCH_INGRESS=[my]$'");
130}
131
132// NET_CLS_BPF is only enabled starting with 4.9-Q...
133int doKernelSupportsNetClsBpf(void) {
Maciej Żenczykowski4e4aaff2019-04-09 02:51:35 -0700134 // NOLINTNEXTLINE(cert-env33-c)
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700135 return system("zcat /proc/config.gz | egrep -q '^CONFIG_NET_CLS_BPF=[my]$'");
136}
137
138// Make sure the above functions actually execute correctly rather than failing
139// due to missing binary or execution failure...
140TEST_F(ClatUtilsTest, KernelSupportsNetFuncs) {
141 // Make sure the file is present and readable and decompressable.
Maciej Żenczykowski4e4aaff2019-04-09 02:51:35 -0700142 // NOLINTNEXTLINE(cert-env33-c)
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700143 ASSERT_EQ(W_EXITCODE(0, 0), system("zcat /proc/config.gz > /dev/null"));
144
145 int v = doKernelSupportsNetSchIngress();
146 int w = doKernelSupportsNetClsBpf();
147
148 // They should always either return 0 (match) or 1 (no match),
149 // anything else is some sort of exec/environment/etc failure.
150 if (v != W_EXITCODE(1, 0)) ASSERT_EQ(v, W_EXITCODE(0, 0));
151 if (w != W_EXITCODE(1, 0)) ASSERT_EQ(w, W_EXITCODE(0, 0));
152}
153
154// True iff CONFIG_NET_SCH_INGRESS is enabled in /proc/config.gz
155bool kernelSupportsNetSchIngress(void) {
156 return doKernelSupportsNetSchIngress() == W_EXITCODE(0, 0);
157}
158
159// True iff CONFIG_NET_CLS_BPF is enabled in /proc/config.gz
160bool kernelSupportsNetClsBpf(void) {
161 return doKernelSupportsNetClsBpf() == W_EXITCODE(0, 0);
162}
163
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800164// See Linux kernel source in include/net/flow.h
165#define LOOPBACK_IFINDEX 1
166
167TEST_F(ClatUtilsTest, AttachReplaceDetachClsactLo) {
168 // Technically does not depend on ebpf, but does depend on clsact,
169 // and we do not really care if it works on pre-4.9-Q anyway.
170 SKIP_IF_BPF_NOT_SUPPORTED;
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700171 if (!kernelSupportsNetSchIngress()) return;
Maciej Żenczykowskiff3308d2019-02-12 19:10:55 -0800172
173 int fd = openNetlinkSocket();
174 ASSERT_LE(3, fd);
175
176 // This attaches and detaches a configuration-less and thus no-op clsact
177 // qdisc to loopback interface (and it takes fractions of a second)
178 EXPECT_EQ(0, tcQdiscAddDevClsact(fd, LOOPBACK_IFINDEX));
179 EXPECT_EQ(0, tcQdiscReplaceDevClsact(fd, LOOPBACK_IFINDEX));
180 EXPECT_EQ(0, tcQdiscDelDevClsact(fd, LOOPBACK_IFINDEX));
181 close(fd);
182}
183
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700184void checkAttachBpfFilterClsactLo(const bool ethernet) {
185 // This test requires kernel 4.9-Q or better
186 SKIP_IF_BPF_NOT_SUPPORTED;
Maciej Żenczykowski18edc522019-04-04 00:59:19 -0700187 if (!kernelSupportsNetSchIngress()) return;
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700188 if (!kernelSupportsNetClsBpf()) return;
189
Maciej Żenczykowski4fe857e2019-03-29 23:29:17 -0700190 int bpf_fd = getClatIngressProgFd(false);
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700191 ASSERT_LE(3, bpf_fd);
192
193 int fd = openNetlinkSocket();
194 EXPECT_LE(3, fd);
195 if (fd >= 0) {
196 // This attaches and detaches a clsact plus ebpf program to loopback
197 // interface, but it should not affect traffic by virtue of us not
198 // actually populating the ebpf control map.
199 // Furthermore: it only takes fractions of a second.
200 EXPECT_EQ(0, tcQdiscAddDevClsact(fd, LOOPBACK_IFINDEX));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800201 EXPECT_EQ(0, tcFilterAddDevIngressBpf(fd, LOOPBACK_IFINDEX, bpf_fd, ethernet));
Maciej Żenczykowski2f8ff892019-03-25 13:57:20 -0700202 EXPECT_EQ(0, tcQdiscDelDevClsact(fd, LOOPBACK_IFINDEX));
203 close(fd);
204 }
205
206 close(bpf_fd);
207}
208
209TEST_F(ClatUtilsTest, CheckAttachBpfFilterRawIpClsactLo) {
210 checkAttachBpfFilterClsactLo(false);
211}
212
213TEST_F(ClatUtilsTest, CheckAttachBpfFilterEthernetClsactLo) {
214 checkAttachBpfFilterClsactLo(true);
215}
216
Maciej Żenczykowskib70da762019-01-28 15:20:48 -0800217} // namespace net
218} // namespace android