Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 23 | #include <linux/if_arp.h> |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <sys/wait.h> |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 26 | |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 27 | #include "bpf/BpfUtils.h" |
| 28 | #include "netdbpf/bpf_shared.h" |
| 29 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace net { |
| 32 | |
| 33 | class ClatUtilsTest : public ::testing::Test { |
| 34 | public: |
| 35 | void SetUp() {} |
| 36 | }; |
| 37 | |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 38 | TEST_F(ClatUtilsTest, HardwareAddressTypeOfNonExistingIf) { |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 39 | ASSERT_EQ(-ENODEV, hardwareAddressType("not_existing_if")); |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST_F(ClatUtilsTest, HardwareAddressTypeOfLoopback) { |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 43 | ASSERT_EQ(ARPHRD_LOOPBACK, hardwareAddressType("lo")); |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // If wireless 'wlan0' interface exists it should be Ethernet. |
| 47 | TEST_F(ClatUtilsTest, HardwareAddressTypeOfWireless) { |
| 48 | int type = hardwareAddressType("wlan0"); |
| 49 | if (type == -ENODEV) return; |
| 50 | |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 51 | ASSERT_EQ(ARPHRD_ETHER, type); |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // If cellular 'rmnet_data0' interface exists it should |
| 55 | // *probably* not be Ethernet and instead be RawIp. |
| 56 | TEST_F(ClatUtilsTest, HardwareAddressTypeOfCellular) { |
| 57 | int type = hardwareAddressType("rmnet_data0"); |
| 58 | if (type == -ENODEV) return; |
| 59 | |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 60 | ASSERT_NE(ARPHRD_ETHER, type); |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 61 | |
| 62 | // ARPHRD_RAWIP is 530 on some pre-4.14 Qualcomm devices. |
| 63 | if (type == 530) return; |
| 64 | |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 65 | ASSERT_EQ(ARPHRD_RAWIP, type); |
Maciej Żenczykowski | 0a7dce8 | 2019-01-28 15:31:55 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 68 | TEST_F(ClatUtilsTest, GetClatMapFd) { |
| 69 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 70 | |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 71 | int fd = getClatIngressMapFd(); |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 72 | ASSERT_LE(3, fd); // 0,1,2 - stdin/out/err, thus 3 <= fd |
| 73 | close(fd); |
Maciej Żenczykowski | 88d28ff | 2019-03-25 11:54:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Maciej Żenczykowski | 949d84a | 2019-01-28 17:22:30 -0800 | [diff] [blame] | 76 | TEST_F(ClatUtilsTest, GetClatRawIpProgFd) { |
| 77 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 78 | |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 79 | int fd = getClatIngressProgFd(false); |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 80 | ASSERT_LE(3, fd); |
| 81 | close(fd); |
Maciej Żenczykowski | 949d84a | 2019-01-28 17:22:30 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_F(ClatUtilsTest, GetClatEtherProgFd) { |
| 85 | SKIP_IF_BPF_NOT_SUPPORTED; |
| 86 | |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 87 | int fd = getClatIngressProgFd(true); |
Maciej Żenczykowski | 10a2ed5 | 2019-03-27 15:46:26 -0700 | [diff] [blame] | 88 | ASSERT_LE(3, fd); |
| 89 | close(fd); |
Maciej Żenczykowski | 949d84a | 2019-01-28 17:22:30 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Maciej Żenczykowski | 7330b02 | 2019-01-28 17:30:24 -0800 | [diff] [blame] | 92 | TEST_F(ClatUtilsTest, TryOpeningNetlinkSocket) { |
| 93 | int fd = openNetlinkSocket(); |
| 94 | ASSERT_LE(3, fd); |
| 95 | close(fd); |
| 96 | } |
| 97 | |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 98 | // The SKIP_IF_BPF_NOT_SUPPORTED macro is effectively a check for 4.9+ kernel |
| 99 | // combined with a launched on P device. Ie. it's a test for 4.9-P or better. |
| 100 | |
| 101 | // NET_SCH_INGRESS is only enabled starting with 4.9-Q and as such we need |
| 102 | // a separate way to test for this... |
| 103 | int doKernelSupportsNetSchIngress(void) { |
Maciej Żenczykowski | 4e4aaff | 2019-04-09 02:51:35 -0700 | [diff] [blame^] | 104 | // NOLINTNEXTLINE(cert-env33-c) |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 105 | return system("zcat /proc/config.gz | egrep -q '^CONFIG_NET_SCH_INGRESS=[my]$'"); |
| 106 | } |
| 107 | |
| 108 | // NET_CLS_BPF is only enabled starting with 4.9-Q... |
| 109 | int doKernelSupportsNetClsBpf(void) { |
Maciej Żenczykowski | 4e4aaff | 2019-04-09 02:51:35 -0700 | [diff] [blame^] | 110 | // NOLINTNEXTLINE(cert-env33-c) |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 111 | return system("zcat /proc/config.gz | egrep -q '^CONFIG_NET_CLS_BPF=[my]$'"); |
| 112 | } |
| 113 | |
| 114 | // Make sure the above functions actually execute correctly rather than failing |
| 115 | // due to missing binary or execution failure... |
| 116 | TEST_F(ClatUtilsTest, KernelSupportsNetFuncs) { |
| 117 | // Make sure the file is present and readable and decompressable. |
Maciej Żenczykowski | 4e4aaff | 2019-04-09 02:51:35 -0700 | [diff] [blame^] | 118 | // NOLINTNEXTLINE(cert-env33-c) |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 119 | ASSERT_EQ(W_EXITCODE(0, 0), system("zcat /proc/config.gz > /dev/null")); |
| 120 | |
| 121 | int v = doKernelSupportsNetSchIngress(); |
| 122 | int w = doKernelSupportsNetClsBpf(); |
| 123 | |
| 124 | // They should always either return 0 (match) or 1 (no match), |
| 125 | // anything else is some sort of exec/environment/etc failure. |
| 126 | if (v != W_EXITCODE(1, 0)) ASSERT_EQ(v, W_EXITCODE(0, 0)); |
| 127 | if (w != W_EXITCODE(1, 0)) ASSERT_EQ(w, W_EXITCODE(0, 0)); |
| 128 | } |
| 129 | |
| 130 | // True iff CONFIG_NET_SCH_INGRESS is enabled in /proc/config.gz |
| 131 | bool kernelSupportsNetSchIngress(void) { |
| 132 | return doKernelSupportsNetSchIngress() == W_EXITCODE(0, 0); |
| 133 | } |
| 134 | |
| 135 | // True iff CONFIG_NET_CLS_BPF is enabled in /proc/config.gz |
| 136 | bool kernelSupportsNetClsBpf(void) { |
| 137 | return doKernelSupportsNetClsBpf() == W_EXITCODE(0, 0); |
| 138 | } |
| 139 | |
Maciej Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 140 | // See Linux kernel source in include/net/flow.h |
| 141 | #define LOOPBACK_IFINDEX 1 |
| 142 | |
| 143 | TEST_F(ClatUtilsTest, AttachReplaceDetachClsactLo) { |
| 144 | // Technically does not depend on ebpf, but does depend on clsact, |
| 145 | // and we do not really care if it works on pre-4.9-Q anyway. |
| 146 | SKIP_IF_BPF_NOT_SUPPORTED; |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 147 | if (!kernelSupportsNetSchIngress()) return; |
Maciej Żenczykowski | ff3308d | 2019-02-12 19:10:55 -0800 | [diff] [blame] | 148 | |
| 149 | int fd = openNetlinkSocket(); |
| 150 | ASSERT_LE(3, fd); |
| 151 | |
| 152 | // This attaches and detaches a configuration-less and thus no-op clsact |
| 153 | // qdisc to loopback interface (and it takes fractions of a second) |
| 154 | EXPECT_EQ(0, tcQdiscAddDevClsact(fd, LOOPBACK_IFINDEX)); |
| 155 | EXPECT_EQ(0, tcQdiscReplaceDevClsact(fd, LOOPBACK_IFINDEX)); |
| 156 | EXPECT_EQ(0, tcQdiscDelDevClsact(fd, LOOPBACK_IFINDEX)); |
| 157 | close(fd); |
| 158 | } |
| 159 | |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 160 | void checkAttachBpfFilterClsactLo(const bool ethernet) { |
| 161 | // This test requires kernel 4.9-Q or better |
| 162 | SKIP_IF_BPF_NOT_SUPPORTED; |
Maciej Żenczykowski | 18edc52 | 2019-04-04 00:59:19 -0700 | [diff] [blame] | 163 | if (!kernelSupportsNetSchIngress()) return; |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 164 | if (!kernelSupportsNetClsBpf()) return; |
| 165 | |
Maciej Żenczykowski | 4fe857e | 2019-03-29 23:29:17 -0700 | [diff] [blame] | 166 | int bpf_fd = getClatIngressProgFd(false); |
Maciej Żenczykowski | 2f8ff89 | 2019-03-25 13:57:20 -0700 | [diff] [blame] | 167 | ASSERT_LE(3, bpf_fd); |
| 168 | |
| 169 | int fd = openNetlinkSocket(); |
| 170 | EXPECT_LE(3, fd); |
| 171 | if (fd >= 0) { |
| 172 | // This attaches and detaches a clsact plus ebpf program to loopback |
| 173 | // interface, but it should not affect traffic by virtue of us not |
| 174 | // actually populating the ebpf control map. |
| 175 | // Furthermore: it only takes fractions of a second. |
| 176 | EXPECT_EQ(0, tcQdiscAddDevClsact(fd, LOOPBACK_IFINDEX)); |
| 177 | EXPECT_EQ(0, tcFilterAddDevBpf(fd, LOOPBACK_IFINDEX, bpf_fd, ethernet)); |
| 178 | EXPECT_EQ(0, tcQdiscDelDevClsact(fd, LOOPBACK_IFINDEX)); |
| 179 | close(fd); |
| 180 | } |
| 181 | |
| 182 | close(bpf_fd); |
| 183 | } |
| 184 | |
| 185 | TEST_F(ClatUtilsTest, CheckAttachBpfFilterRawIpClsactLo) { |
| 186 | checkAttachBpfFilterClsactLo(false); |
| 187 | } |
| 188 | |
| 189 | TEST_F(ClatUtilsTest, CheckAttachBpfFilterEthernetClsactLo) { |
| 190 | checkAttachBpfFilterClsactLo(true); |
| 191 | } |
| 192 | |
Maciej Żenczykowski | b70da76 | 2019-01-28 15:20:48 -0800 | [diff] [blame] | 193 | } // namespace net |
| 194 | } // namespace android |