git: 66f0e2017f7c - main - ipsec_offload: add ipsec_accel_drv_sa_lifetime_fetch()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 20 Aug 2024 12:43:52 UTC
The branch main has been updated by kib:

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

commit 66f0e2017f7cd804f31ae4fc2ad38607d85a08d3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-08-18 13:22:28 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-08-20 12:42:13 +0000

    ipsec_offload: add ipsec_accel_drv_sa_lifetime_fetch()
    
    A function to fetch hardware counters for offloaded SA on specific
    interface.
    
    Sponsored by:   NVidia networking
---
 sys/netipsec/ipsec_offload.c | 29 +++++++++++++++++++++++++++++
 sys/netipsec/ipsec_offload.h |  4 ++++
 sys/netipsec/key.c           | 15 +++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/sys/netipsec/ipsec_offload.c b/sys/netipsec/ipsec_offload.c
index 4d81803f4be7..bbf98ac7a676 100644
--- a/sys/netipsec/ipsec_offload.c
+++ b/sys/netipsec/ipsec_offload.c
@@ -157,6 +157,8 @@ static struct mbuf *ipsec_accel_key_setaccelif_impl(struct secasvar *sav);
 static void ipsec_accel_on_ifdown_impl(struct ifnet *ifp);
 static void ipsec_accel_drv_sa_lifetime_update_impl(struct secasvar *sav,
     if_t ifp, u_int drv_spi, uint64_t octets, uint64_t allocs);
+static int ipsec_accel_drv_sa_lifetime_fetch_impl(struct secasvar *sav,
+    if_t ifp, u_int drv_spi, uint64_t *octets, uint64_t *allocs);
 static void ipsec_accel_ifdetach_event(void *arg, struct ifnet *ifp);
 
 static void
@@ -177,6 +179,8 @@ ipsec_accel_init(void *arg)
 	ipsec_accel_on_ifdown_p = ipsec_accel_on_ifdown_impl;
 	ipsec_accel_drv_sa_lifetime_update_p =
 	    ipsec_accel_drv_sa_lifetime_update_impl;
+	ipsec_accel_drv_sa_lifetime_fetch_p =
+	    ipsec_accel_drv_sa_lifetime_fetch_impl;
 	pctrie_init(&drv_spi_pctrie);
 	ipsec_accel_ifdetach_event_tag = EVENTHANDLER_REGISTER(
 	    ifnet_departure_event, ipsec_accel_ifdetach_event, NULL,
@@ -200,6 +204,7 @@ ipsec_accel_fini(void *arg)
 	ipsec_accel_key_setaccelif_p = NULL;
 	ipsec_accel_on_ifdown_p = NULL;
 	ipsec_accel_drv_sa_lifetime_update_p = NULL;
+	ipsec_accel_drv_sa_lifetime_fetch_p = NULL;
 	ipsec_accel_sync_imp();
 	clean_unrhdr(drv_spi_unr);	/* avoid panic, should go later */
 	clear_unrhdr(drv_spi_unr);
@@ -1017,6 +1022,30 @@ out:
 	NET_EPOCH_EXIT(et);
 }
 
+static int
+ipsec_accel_drv_sa_lifetime_fetch_impl(struct secasvar *sav,
+    if_t ifp, u_int drv_spi, uint64_t *octets, uint64_t *allocs)
+{
+	struct ifp_handle_sav *i;
+	int error;
+
+	NET_EPOCH_ASSERT();
+	error = 0;
+
+	mtx_lock(&ipsec_accel_cnt_lock);
+	CK_LIST_FOREACH(i, &sav->accel_ifps, sav_link) {
+		if (i->ifp == ifp && i->drv_spi == drv_spi) {
+			*octets = i->cnt_octets;
+			*allocs = i->cnt_allocs;
+			break;
+		}
+	}
+	if (i == NULL)
+		error = ENOENT;
+	mtx_unlock(&ipsec_accel_cnt_lock);
+	return (error);
+}
+
 static void
 ipsec_accel_sa_lifetime_hw(struct secasvar *sav, if_t ifp,
     struct seclifetime *lft)
diff --git a/sys/netipsec/ipsec_offload.h b/sys/netipsec/ipsec_offload.h
index 72055a110951..904fe6252396 100644
--- a/sys/netipsec/ipsec_offload.h
+++ b/sys/netipsec/ipsec_offload.h
@@ -64,6 +64,8 @@ extern struct mbuf *(*ipsec_accel_key_setaccelif_p)(struct secasvar *sav);
 extern void (*ipsec_accel_on_ifdown_p)(struct ifnet *ifp);
 extern void (*ipsec_accel_drv_sa_lifetime_update_p)(struct secasvar *sav,
     if_t ifp, u_int drv_spi, uint64_t octets, uint64_t allocs);
+extern int (*ipsec_accel_drv_sa_lifetime_fetch_p)(struct secasvar *sav,
+    if_t ifp, u_int drv_spi, uint64_t *octets, uint64_t *allocs);
 
 #ifdef IPSEC_OFFLOAD
 /*
@@ -191,6 +193,8 @@ struct ipsec_accel_in_tag *ipsec_accel_input_tag_lookup(const struct mbuf *);
 void ipsec_accel_on_ifdown(struct ifnet *ifp);
 void ipsec_accel_drv_sa_lifetime_update(struct secasvar *sav, if_t ifp,
     u_int drv_spi, uint64_t octets, uint64_t allocs);
+int ipsec_accel_drv_sa_lifetime_fetch(struct secasvar *sav,
+    if_t ifp, u_int drv_spi, uint64_t *octets, uint64_t *allocs);
 
 #endif	/* _KERNEL */
 
diff --git a/sys/netipsec/key.c b/sys/netipsec/key.c
index 149173e0b5f6..5a3e5727bc2e 100644
--- a/sys/netipsec/key.c
+++ b/sys/netipsec/key.c
@@ -112,6 +112,8 @@ struct mbuf *(*ipsec_accel_key_setaccelif_p)(struct secasvar *sav);
 void (*ipsec_accel_on_ifdown_p)(struct ifnet *ifp);
 void (*ipsec_accel_drv_sa_lifetime_update_p)(struct secasvar *sav, if_t ifp,
     u_int drv_spi, uint64_t octets, uint64_t allocs);
+int (*ipsec_accel_drv_sa_lifetime_fetch_p)(struct secasvar *sav, if_t ifp,
+    u_int drv_spi, uint64_t *octets, uint64_t *allocs);
 #endif
 
 #define FULLMASK	0xff
@@ -8990,4 +8992,17 @@ ipsec_accel_drv_sa_lifetime_update(struct secasvar *sav, if_t ifp,
 	if (p != NULL)
 		p(sav, ifp, drv_spi, octets, allocs);
 }
+
+int
+ipsec_accel_drv_sa_lifetime_fetch(struct secasvar *sav,
+    if_t ifp, u_int drv_spi, uint64_t *octets, uint64_t *allocs)
+{
+	int (*p)(struct secasvar *sav, if_t ifp, u_int drv_spi,
+	    uint64_t *octets, uint64_t *allocs);
+
+	p = atomic_load_ptr(&ipsec_accel_drv_sa_lifetime_fetch_p);
+	if (p == NULL)
+		return (EOPNOTSUPP);
+	return (p(sav, ifp, drv_spi, octets, allocs));
+}
 #endif