The branch main has been updated by kp:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=a002c839ec71557c0256bbb4f0f31482af8c6dd6

commit a002c839ec71557c0256bbb4f0f31482af8c6dd6
Author:     Kristof Provost <k...@freebsd.org>
AuthorDate: 2022-12-05 13:14:49 +0000
Commit:     Kristof Provost <k...@freebsd.org>
CommitDate: 2022-12-14 05:48:59 +0000

    if_ovpn: cleanup offsetof() use
    
    Move the use of the `offsetof(struct ovpn_counters, fieldname) /
    sizeof(uint64_t)` construct into a macro.
    This removes a fair bit of code duplication and should make things a
    little easier to read.
    
    Reviewed by:    zlei
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D37607
---
 sys/net/if_ovpn.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/sys/net/if_ovpn.c b/sys/net/if_ovpn.c
index a11dd0b4a6cb..28a0ed33f010 100644
--- a/sys/net/if_ovpn.c
+++ b/sys/net/if_ovpn.c
@@ -250,12 +250,16 @@ VNET_DEFINE_STATIC(struct if_clone *, ovpn_cloner);
 #define OVPN_WASSERT(sc)       rm_assert(&(sc)->lock, RA_WLOCKED)
 #define OVPN_UNLOCK_ASSERT(sc) rm_assert(&(sc)->lock, RA_UNLOCKED)
 
+#define OVPN_COUNTER(sc, name) \
+       ((sc)->counters[offsetof(struct ovpn_counters, name)/sizeof(uint64_t)])
+#define OVPN_PEER_COUNTER(peer, name) \
+       ((peer)->counters[offsetof(struct ovpn_peer_counters, name) / \
+        sizeof(uint64_t)])
+
 #define OVPN_COUNTER_ADD(sc, name, val)        \
-       counter_u64_add(sc->counters[offsetof(struct ovpn_counters, name) / \
-           sizeof(uint64_t)], val)
+       counter_u64_add(OVPN_COUNTER(sc, name), val)
 #define OVPN_PEER_COUNTER_ADD(p, name, val)    \
-       counter_u64_add(p->counters[offsetof(struct ovpn_peer_counters, name) / 
\
-           sizeof(uint64_t)], val)
+       counter_u64_add(OVPN_PEER_COUNTER(p, name), val)
 
 #define TO_IN(x)               ((struct sockaddr_in *)(x))
 #define TO_IN6(x)              ((struct sockaddr_in6 *)(x))
@@ -442,14 +446,10 @@ ovpn_notify_del_peer(struct ovpn_softc *sc, struct 
ovpn_kpeer *peer)
        n->type = OVPN_NOTIF_DEL_PEER;
        n->del_reason = peer->del_reason;
 
-       n->counters.pkt_in = counter_u64_fetch(peer->counters[offsetof(
-           struct ovpn_peer_counters, pkt_in)/sizeof(uint64_t)]);
-       n->counters.pkt_out = counter_u64_fetch(peer->counters[offsetof(
-           struct ovpn_peer_counters, pkt_out)/sizeof(uint64_t)]);
-       n->counters.bytes_in = counter_u64_fetch(peer->counters[offsetof(
-           struct ovpn_peer_counters, bytes_in)/sizeof(uint64_t)]);
-       n->counters.bytes_out = counter_u64_fetch(peer->counters[offsetof(
-           struct ovpn_peer_counters, bytes_out)/sizeof(uint64_t)]);
+       n->counters.pkt_in = counter_u64_fetch(OVPN_PEER_COUNTER(peer, pkt_in));
+       n->counters.pkt_out = counter_u64_fetch(OVPN_PEER_COUNTER(peer, 
pkt_out));
+       n->counters.bytes_in = counter_u64_fetch(OVPN_PEER_COUNTER(peer, 
bytes_in));
+       n->counters.bytes_out = counter_u64_fetch(OVPN_PEER_COUNTER(peer, 
bytes_out));
 
        if (buf_ring_enqueue(sc->notifring, n) != 0) {
                free(n, M_OVPN);
@@ -1248,11 +1248,8 @@ ovpn_get_stats(struct ovpn_softc *sc, nvlist_t **onvl)
 
 #define OVPN_COUNTER_OUT(name, in, out) \
        do { \
-               ret = ovpn_add_counters(nvl, name, \
-                   sc->counters[offsetof(struct ovpn_counters, in) / \
-                   sizeof(uint64_t)], \
-                   sc->counters[offsetof(struct ovpn_counters, out) / \
-                   sizeof(uint64_t)]); \
+               ret = ovpn_add_counters(nvl, name, OVPN_COUNTER(sc, in), \
+                   OVPN_COUNTER(sc, out)); \
                if (ret != 0) \
                        goto error; \
        } while(0)
@@ -1294,10 +1291,7 @@ ovpn_get_peer_stats(struct ovpn_softc *sc, nvlist_t 
**nvl)
 #define OVPN_PEER_COUNTER_OUT(name, in, out) \
        do { \
                ret = ovpn_add_counters(nvpeer, name, \
-                   peer->counters[offsetof(struct ovpn_peer_counters, in) / \
-                   sizeof(uint64_t)], \
-                   peer->counters[offsetof(struct ovpn_peer_counters, out) / \
-                   sizeof(uint64_t)]); \
+                   OVPN_PEER_COUNTER(peer, in), OVPN_PEER_COUNTER(peer, out)); 
\
                if (ret != 0) \
                        goto error; \
        } while(0)

Reply via email to