OffloadUtils - use less macros
Test: build, atest
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I0b07a61065ea570cd55100db8be6e690257f83e2
diff --git a/server/OffloadUtils.cpp b/server/OffloadUtils.cpp
index 4281b20..4e80ef4 100644
--- a/server/OffloadUtils.cpp
+++ b/server/OffloadUtils.cpp
@@ -35,6 +35,8 @@
namespace android {
namespace net {
+using std::max;
+
int hardwareAddressType(const std::string& interface) {
base::unique_fd ufd(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
@@ -68,7 +70,7 @@
return -err;
}
- const int on = 1;
+ static constexpr int on = 1;
int rv = setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, &on, sizeof(on));
if (rv) ALOGE("setsockopt(fd, SOL_NETLINK, NETLINK_CAP_ACK, %d)", on);
@@ -132,9 +134,8 @@
// Some hoop jumping to make this compile time constant with known size,
// so that the structure declaration is well defined at compile time.
#define CLSACT "clsact"
- static const char clsact[] = CLSACT;
// sizeof() includes the terminating NULL
-#define ASCIIZ_LEN_CLSACT sizeof(clsact)
+ static constexpr size_t ASCIIZ_LEN_CLSACT = sizeof(CLSACT);
const struct {
nlmsghdr n;
@@ -167,7 +168,6 @@
.str = CLSACT,
},
};
-#undef ASCIIZ_LEN_CLSACT
#undef CLSACT
return sendAndProcessNetlinkResponse(&req, sizeof(req));
@@ -187,9 +187,8 @@
// so that we can define the struct further down the function with the
// field for this sized correctly already during the build.
#define BPF "bpf"
- const char bpf[] = BPF;
// sizeof() includes the terminating NULL
-#define ASCIIZ_LEN_BPF sizeof(bpf)
+ static constexpr size_t ASCIIZ_LEN_BPF = sizeof(BPF);
// This is to replicate program name suffix used by 'tc' Linux cli
// when it attaches programs.
@@ -199,48 +198,42 @@
// prog_clatd_schedcls_ingress_clat_rawip:[*fsobj]
// and is the name of the pinned ingress ebpf program for ARPHRD_RAWIP interfaces.
// (also compatible with anything that has 0 size L2 header)
-#define NAME_RX_RAWIP CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX
- const char name_rx_rawip[] = NAME_RX_RAWIP;
+ static constexpr char name_rx_rawip[] = CLAT_INGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
// This macro expands (from header files) to:
// prog_clatd_schedcls_ingress_clat_ether:[*fsobj]
// and is the name of the pinned ingress ebpf program for ARPHRD_ETHER interfaces.
// (also compatible with anything that has standard ethernet header)
-#define NAME_RX_ETHER CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX
- const char name_rx_ether[] = NAME_RX_ETHER;
+ static constexpr char name_rx_ether[] = CLAT_INGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
// This macro expands (from header files) to:
// prog_clatd_schedcls_egress_clat_rawip:[*fsobj]
// and is the name of the pinned egress ebpf program for ARPHRD_RAWIP interfaces.
// (also compatible with anything that has 0 size L2 header)
-#define NAME_TX_RAWIP CLAT_EGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX
- const char name_tx_rawip[] = NAME_TX_RAWIP;
+ static constexpr char name_tx_rawip[] = CLAT_EGRESS_PROG_RAWIP_NAME FSOBJ_SUFFIX;
// This macro expands (from header files) to:
// prog_clatd_schedcls_egress_clat_ether:[*fsobj]
// and is the name of the pinned egress ebpf program for ARPHRD_ETHER interfaces.
// (also compatible with anything that has standard ethernet header)
-#define NAME_TX_ETHER CLAT_EGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX
- const char name_tx_ether[] = NAME_TX_ETHER;
+ static constexpr char name_tx_ether[] = CLAT_EGRESS_PROG_ETHER_NAME FSOBJ_SUFFIX;
+
+#undef FSOBJ_SUFFIX
// The actual name we'll use is determined at run time via 'ethernet' and 'ingress'
// booleans. We need to compile time allocate enough space in the struct
// hence this macro magic to make sure we have enough space for either
// possibility. In practice some of these are actually the same size.
-#define ASCIIZ_MAXLEN_NAME_RX \
- ((sizeof(name_rx_rawip) > sizeof(name_rx_ether)) ? sizeof(name_rx_rawip) \
- : sizeof(name_rx_ether))
-#define ASCIIZ_MAXLEN_NAME_TX \
- ((sizeof(name_tx_rawip) > sizeof(name_tx_ether)) ? sizeof(name_tx_rawip) \
- : sizeof(name_tx_ether))
-#define ASCIIZ_MAXLEN_NAME \
- ((ASCIIZ_MAXLEN_NAME_RX > ASCIIZ_MAXLEN_NAME_TX) ? ASCIIZ_MAXLEN_NAME_RX \
- : ASCIIZ_MAXLEN_NAME_TX)
+ static constexpr size_t ASCIIZ_MAXLEN_NAME_RX =
+ max(sizeof(name_rx_rawip), sizeof(name_rx_ether));
+ static constexpr size_t ASCIIZ_MAXLEN_NAME_TX =
+ max(sizeof(name_tx_rawip), sizeof(name_tx_ether));
+ static constexpr size_t ASCIIZ_MAXLEN_NAME = max(ASCIIZ_MAXLEN_NAME_RX, ASCIIZ_MAXLEN_NAME_TX);
- // These are not compile time constants: NAME is used in strncpy below
-#define NAME_RX (ethernet ? NAME_RX_ETHER : NAME_RX_RAWIP)
-#define NAME_TX (ethernet ? NAME_TX_ETHER : NAME_TX_RAWIP)
-#define NAME (ingress ? NAME_RX : NAME_TX)
+ // These are not compile time constants: 'name' is used in strncpy below
+ const char* const name_rx = ethernet ? name_rx_ether : name_rx_rawip;
+ const char* const name_tx = ethernet ? name_tx_ether : name_tx_rawip;
+ const char* const name = ingress ? name_rx : name_tx;
struct {
nlmsghdr n;
@@ -328,29 +321,16 @@
},
},
};
-
- strncpy(req.options.name.str, NAME, sizeof(req.options.name.str));
-
-#undef NAME
-#undef NAME_TX
-#undef NAME_RX
-#undef ASCIIZ_MAXLEN_NAME
-#undef ASCIIZ_MAXLEN_NAME_TX
-#undef ASCIIZ_MAXLEN_NAME_RX
-#undef NAME_TX_ETHER
-#undef NAME_TX_RAWIP
-#undef NAME_RX_ETHER
-#undef NAME_RX_RAWIP
-#undef FSOBJ_SUFFIX
-#undef ASCIIZ_LEN_BPF
#undef BPF
+ strncpy(req.options.name.str, name, sizeof(req.options.name.str));
+
return sendAndProcessNetlinkResponse(&req, sizeof(req));
}
// tc filter del dev .. in/egress prio .. protocol ..
int tcFilterDelDev(int ifIndex, bool ingress, uint16_t prio, uint16_t proto) {
- struct {
+ const struct {
nlmsghdr n;
tcmsg t;
} req = {