add getClatEgressProgFd() with tests
Test: atest
Bug: 139396664
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I966c873e1dd46dc1030cbbdb14d1b2f4640ec1ae
diff --git a/server/ClatUtils.cpp b/server/ClatUtils.cpp
index c75fcdd..a5b7c44 100644
--- a/server/ClatUtils.cpp
+++ b/server/ClatUtils.cpp
@@ -67,6 +67,12 @@
return (fd == -1) ? -errno : fd;
}
+int getClatEgressProgFd(bool with_ethernet_header) {
+ const int fd = bpf::bpfFdGet(
+ with_ethernet_header ? CLAT_EGRESS_PROG_ETHER_PATH : CLAT_EGRESS_PROG_RAWIP_PATH, 0);
+ return (fd == -1) ? -errno : fd;
+}
+
int getClatIngressMapFd(void) {
const int fd = bpf::bpfFdGet(CLAT_INGRESS_MAP_PATH, 0);
return (fd == -1) ? -errno : fd;
diff --git a/server/ClatUtils.h b/server/ClatUtils.h
index f9df5c2..392bd44 100644
--- a/server/ClatUtils.h
+++ b/server/ClatUtils.h
@@ -26,6 +26,8 @@
int getClatEgressMapFd(void);
+int getClatEgressProgFd(bool with_ethernet_header);
+
int getClatIngressMapFd(void);
int getClatIngressProgFd(bool with_ethernet_header);
diff --git a/server/ClatUtilsTest.cpp b/server/ClatUtilsTest.cpp
index 77b22a1..b1a265b 100644
--- a/server/ClatUtilsTest.cpp
+++ b/server/ClatUtilsTest.cpp
@@ -73,6 +73,22 @@
close(fd);
}
+TEST_F(ClatUtilsTest, GetClatEgressRawIpProgFd) {
+ SKIP_IF_BPF_NOT_SUPPORTED;
+
+ int fd = getClatEgressProgFd(false);
+ ASSERT_LE(3, fd);
+ close(fd);
+}
+
+TEST_F(ClatUtilsTest, GetClatEgressEtherProgFd) {
+ SKIP_IF_BPF_NOT_SUPPORTED;
+
+ int fd = getClatEgressProgFd(true);
+ ASSERT_LE(3, fd);
+ close(fd);
+}
+
TEST_F(ClatUtilsTest, GetClatIngressMapFd) {
SKIP_IF_BPF_NOT_SUPPORTED;
@@ -81,7 +97,7 @@
close(fd);
}
-TEST_F(ClatUtilsTest, GetClatRawIpProgFd) {
+TEST_F(ClatUtilsTest, GetClatIngressRawIpProgFd) {
SKIP_IF_BPF_NOT_SUPPORTED;
int fd = getClatIngressProgFd(false);
@@ -89,7 +105,7 @@
close(fd);
}
-TEST_F(ClatUtilsTest, GetClatEtherProgFd) {
+TEST_F(ClatUtilsTest, GetClatIngressEtherProgFd) {
SKIP_IF_BPF_NOT_SUPPORTED;
int fd = getClatIngressProgFd(true);