summaryrefslogtreecommitdiff
path: root/common/native/tcutils/tests/tcutils_test.cpp
blob: 77322479b2fb48c67c63390422433b3bdfd8348b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * TcUtilsTest.cpp - unit tests for TcUtils.cpp
 */

#include <gtest/gtest.h>

#include "bpf/KernelUtils.h"
#include <tcutils/tcutils.h>

#include <BpfSyscallWrappers.h>
#include <errno.h>
#include <linux/if_ether.h>

namespace android {

TEST(LibTcUtilsTest, IsEthernetOfNonExistingIf) {
  bool result = false;
  int error = isEthernet("not_existing_if", result);
  ASSERT_FALSE(result);
  ASSERT_EQ(-ENODEV, error);
}

TEST(LibTcUtilsTest, IsEthernetOfLoopback) {
  bool result = false;
  int error = isEthernet("lo", result);
  ASSERT_FALSE(result);
  ASSERT_EQ(-EAFNOSUPPORT, error);
}

// If wireless 'wlan0' interface exists it should be Ethernet.
// See also HardwareAddressTypeOfWireless.
TEST(LibTcUtilsTest, IsEthernetOfWireless) {
  bool result = false;
  int error = isEthernet("wlan0", result);
  if (!result && error == -ENODEV)
    return;

  ASSERT_EQ(0, error);
  ASSERT_TRUE(result);
}

// If cellular 'rmnet_data0' interface exists it should
// *probably* not be Ethernet and instead be RawIp.
// See also HardwareAddressTypeOfCellular.
TEST(LibTcUtilsTest, IsEthernetOfCellular) {
  bool result = false;
  int error = isEthernet("rmnet_data0", result);
  if (!result && error == -ENODEV)
    return;

  ASSERT_EQ(0, error);
  ASSERT_FALSE(result);
}

// See Linux kernel source in include/net/flow.h
static constexpr int LOOPBACK_IFINDEX = 1;

TEST(LibTcUtilsTest, AttachReplaceDetachClsactLo) {
  // This attaches and detaches a configuration-less and thus no-op clsact
  // qdisc to loopback interface (and it takes fractions of a second)
  EXPECT_EQ(0, tcAddQdiscClsact(LOOPBACK_IFINDEX));
  EXPECT_EQ(0, tcReplaceQdiscClsact(LOOPBACK_IFINDEX));
  EXPECT_EQ(0, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
  EXPECT_EQ(-EINVAL, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
}

TEST(LibTcUtilsTest, AddAndDeleteBpfFilter) {
  // TODO: this should likely be in the tethering module, where using netd.h would be ok
  static constexpr char bpfProgPath[] =
      "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_downstream6_ether";
  const int errNOENT = bpf::isAtLeastKernelVersion(4, 19, 0) ? ENOENT : EINVAL;

  // static test values
  static constexpr bool ingress = true;
  static constexpr uint16_t prio = 17;
  static constexpr uint16_t proto = ETH_P_ALL;

  // try to delete missing filter from missing qdisc
  EXPECT_EQ(-EINVAL, tcDeleteFilter(LOOPBACK_IFINDEX, ingress, prio, proto));
  // try to attach bpf filter to missing qdisc
  EXPECT_EQ(-EINVAL, tcAddBpfFilter(LOOPBACK_IFINDEX, ingress, prio, proto,
                                    bpfProgPath));
  // add the clsact qdisc
  EXPECT_EQ(0, tcAddQdiscClsact(LOOPBACK_IFINDEX));
  // try to delete missing filter when there is a qdisc attached
  EXPECT_EQ(-errNOENT, tcDeleteFilter(LOOPBACK_IFINDEX, ingress, prio, proto));
  // add and delete a bpf filter
  EXPECT_EQ(
      0, tcAddBpfFilter(LOOPBACK_IFINDEX, ingress, prio, proto, bpfProgPath));
  EXPECT_EQ(0, tcDeleteFilter(LOOPBACK_IFINDEX, ingress, prio, proto));
  // try to remove the same filter a second time
  EXPECT_EQ(-errNOENT, tcDeleteFilter(LOOPBACK_IFINDEX, ingress, prio, proto));
  // remove the clsact qdisc
  EXPECT_EQ(0, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
  // once again, try to delete missing filter from missing qdisc
  EXPECT_EQ(-EINVAL, tcDeleteFilter(LOOPBACK_IFINDEX, ingress, prio, proto));
}

TEST(LibTcUtilsTest, AddAndDeleteIngressPoliceFilter) {
  // TODO: this should likely be in the tethering module, where using netd.h would be ok
  static constexpr char bpfProgPath[] =
      "/sys/fs/bpf/netd_shared/prog_netd_schedact_ingress_account";
  int fd = bpf::retrieveProgram(bpfProgPath);
  ASSERT_LE(3, fd);
  close(fd);

  const int errNOENT = bpf::isAtLeastKernelVersion(4, 19, 0) ? ENOENT : EINVAL;

  // static test values
  static constexpr unsigned rateInBytesPerSec =
      1024 * 1024; // 8mbit/s => 1mbyte/s => 1024*1024 bytes/s.
  static constexpr uint16_t prio = 17;
  static constexpr uint16_t proto = ETH_P_ALL;

  // try to delete missing filter from missing qdisc
  EXPECT_EQ(-EINVAL,
            tcDeleteFilter(LOOPBACK_IFINDEX, true /*ingress*/, prio, proto));
  // try to attach bpf filter to missing qdisc
  EXPECT_EQ(-EINVAL, tcAddIngressPoliceFilter(LOOPBACK_IFINDEX, prio, proto,
                                              rateInBytesPerSec, bpfProgPath));
  // add the clsact qdisc
  EXPECT_EQ(0, tcAddQdiscClsact(LOOPBACK_IFINDEX));
  // try to delete missing filter when there is a qdisc attached
  EXPECT_EQ(-errNOENT,
            tcDeleteFilter(LOOPBACK_IFINDEX, true /*ingress*/, prio, proto));
  // add and delete a bpf filter
  EXPECT_EQ(0, tcAddIngressPoliceFilter(LOOPBACK_IFINDEX, prio, proto,
                                        rateInBytesPerSec, bpfProgPath));
  EXPECT_EQ(0, tcDeleteFilter(LOOPBACK_IFINDEX, true /*ingress*/, prio, proto));
  // try to remove the same filter a second time
  EXPECT_EQ(-errNOENT,
            tcDeleteFilter(LOOPBACK_IFINDEX, true /*ingress*/, prio, proto));
  // remove the clsact qdisc
  EXPECT_EQ(0, tcDeleteQdiscClsact(LOOPBACK_IFINDEX));
  // once again, try to delete missing filter from missing qdisc
  EXPECT_EQ(-EINVAL,
            tcDeleteFilter(LOOPBACK_IFINDEX, true /*ingress*/, prio, proto));
}

} // namespace android