From de4c8b6a7b62b9e554da08cb18c74765084184c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Mon, 15 Feb 2021 12:45:42 -0800 Subject: net-test: add bpf_ktime_get_ns / bpf_ktime_get_boot_ns tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tested against various kernels: ARCH=um SUBARCH=x86_64 /aosp-tests/net/test/run_net_test.sh --builder bpf_test.py Test: see above, TreeHugger Signed-off-by: Maciej Żenczykowski Change-Id: I9ede44099bcf3150bc5fc036843ebabc5cda14b8 --- net/test/bpf.py | 6 +++-- net/test/bpf_test.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/net/test/bpf.py b/net/test/bpf.py index 4e4f34f..be4c72f 100755 --- a/net/test/bpf.py +++ b/net/test/bpf.py @@ -161,9 +161,11 @@ BPF_FUNC_unspec = 0 BPF_FUNC_map_lookup_elem = 1 BPF_FUNC_map_update_elem = 2 BPF_FUNC_map_delete_elem = 3 +BPF_FUNC_ktime_get_ns = 5 BPF_FUNC_get_current_uid_gid = 15 BPF_FUNC_get_socket_cookie = 46 BPF_FUNC_get_socket_uid = 47 +BPF_FUNC_ktime_get_boot_ns = 125 # pylint: enable=invalid-name BPF_F_RDONLY = 1 << 3 @@ -253,10 +255,10 @@ def DeleteMap(map_fd, key): BpfSyscall(BPF_MAP_DELETE_ELEM, attr) -def BpfProgLoad(prog_type, instructions): +def BpfProgLoad(prog_type, instructions, prog_license=b"GPL"): bpf_prog = "".join(instructions) insn_buff = ctypes.create_string_buffer(bpf_prog) - gpl_license = ctypes.create_string_buffer(b"GPL") + gpl_license = ctypes.create_string_buffer(prog_license) log_buf = ctypes.create_string_buffer(b"", LOG_SIZE) attr = BpfAttrProgLoad((prog_type, len(insn_buff) / len(BpfInsn), ctypes.addressof(insn_buff), diff --git a/net/test/bpf_test.py b/net/test/bpf_test.py index 33ad1da..4a1790d 100755 --- a/net/test/bpf_test.py +++ b/net/test/bpf_test.py @@ -78,6 +78,7 @@ from bpf import LookupMap from bpf import UpdateMap import csocket import net_test +from net_test import LINUX_VERSION import sock_diag libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True) @@ -85,6 +86,18 @@ libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True) HAVE_EBPF_ACCOUNTING = bpf.HAVE_EBPF_4_9 HAVE_EBPF_SOCKET = bpf.HAVE_EBPF_4_14 +# bpf_ktime_get_ns() was made non-GPL requiring in 5.8 and at the same time +# bpf_ktime_get_boot_ns() was added, both of these changes were backported to +# Android Common Kernel in 4.14.221, 4.19.175, 5.4.97. +# As such we require 4.14.222+ 4.19.176+ 5.4.98+ 5.8.0+, +# but since we only really care about LTS releases: +HAVE_EBPF_KTIME_GET_NS_APACHE2 = ( + ((LINUX_VERSION > (4, 14, 221)) and (LINUX_VERSION < (4, 19, 0))) or + ((LINUX_VERSION > (4, 19, 175)) and (LINUX_VERSION < (5, 4, 0))) or + (LINUX_VERSION > (5, 4, 97)) +) +HAVE_EBPF_KTIME_GET_BOOT_NS = HAVE_EBPF_KTIME_GET_NS_APACHE2 + KEY_SIZE = 8 VALUE_SIZE = 4 TOTAL_ENTRIES = 20 @@ -317,6 +330,60 @@ class BpfTest(net_test.NetworkTest): SocketUDPLoopBack(packet_count, 6, self.prog_fd) self.assertEqual(packet_count * 2, LookupMap(self.map_fd, key).value) + def testKtimeGetNsGPL(self): + instructions = [BpfFuncCall(BPF_FUNC_ktime_get_ns)] + instructions += INS_BPF_EXIT_BLOCK + self.prog_fd = BpfProgLoad(BPF_PROG_TYPE_SCHED_CLS, instructions) + # No exceptions? Good. + + ############################################################################## + # + # Test for presence of kernel patch: + # + # UPSTREAM: net: bpf: Make bpf_ktime_get_ns() available to non GPL programs + # + # 4.14: https://android-review.googlesource.com/c/kernel/common/+/1585269 + # commit cbb4c73f9eab8f3c8ac29175d45c99ccba382e15 + # + # 4.19: https://android-review.googlesource.com/c/kernel/common/+/1355243 + # commit 272e21ccc9a92feeee80aff0587410a314b73c5b + # + # 5.4: https://android-review.googlesource.com/c/kernel/common/+/1355422 + # commit 45217b91eaaa3a563247c4f470f4cb785de6b1c6 + # + @unittest.skipUnless(HAVE_EBPF_KTIME_GET_NS_APACHE2, + "no bpf_ktime_get_ns() support for non-GPL programs") + def testKtimeGetNsApache2(self): + instructions = [BpfFuncCall(BPF_FUNC_ktime_get_ns)] + instructions += INS_BPF_EXIT_BLOCK + self.prog_fd = BpfProgLoad(BPF_PROG_TYPE_SCHED_CLS, instructions, + b"Apache 2.0") + # No exceptions? Good. + + ############################################################################## + # + # Test for presence of kernel patch: + # + # BACKPORT: bpf: add bpf_ktime_get_boot_ns() + # + # 4.14: https://android-review.googlesource.com/c/kernel/common/+/1585587 + # commit 34073d7a8ee47ca908b56e9a1d14ca0615fdfc09 + # + # 4.19: https://android-review.googlesource.com/c/kernel/common/+/1585606 + # commit 4812ec50935dfe59ba9f48a572e278dd0b02af68 + # + # 5.4: https://android-review.googlesource.com/c/kernel/common/+/1585252 + # commit 57b3f4830fb66a6038c4c1c66ca2e138fe8be231 + # + @unittest.skipUnless(HAVE_EBPF_KTIME_GET_BOOT_NS, + "no bpf_ktime_get_boot_ns() support") + def testKtimeGetBootNs(self): + instructions = [BpfFuncCall(BPF_FUNC_ktime_get_boot_ns)] + instructions += INS_BPF_EXIT_BLOCK + self.prog_fd = BpfProgLoad(BPF_PROG_TYPE_SCHED_CLS, instructions, + b"Apache 2.0") + # No exceptions? Good. + def testGetSocketCookie(self): self.map_fd = CreateMap(BPF_MAP_TYPE_HASH, KEY_SIZE, VALUE_SIZE, TOTAL_ENTRIES) -- cgit v1.2.3