git: 5243598927a9 - main - linprocfs: Migrate to IfAPI

From: Justin Hibbits <jhibbits_at_FreeBSD.org>
Date: Fri, 03 Feb 2023 14:45:19 UTC
The branch main has been updated by jhibbits:

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

commit 5243598927a95e77b3bb9804b049539b87353a5b
Author:     Justin Hibbits <jhibbits@FreeBSD.org>
AuthorDate: 2023-02-02 21:48:22 +0000
Commit:     Justin Hibbits <jhibbits@FreeBSD.org>
CommitDate: 2023-02-03 14:38:03 +0000

    linprocfs: Migrate to IfAPI
    
    Summary:
    Migrate linprocfs to use the IfAPI interfaces instead of direct ifnet
    accesses.
    
    Reviewed by:    dchagin
    Sponsored by:   Juniper Networks, Inc.
    Differential Revision: https://reviews.freebsd.org/D38358
---
 sys/compat/linprocfs/linprocfs.c | 107 +++++++++++++++++++++++----------------
 1 file changed, 64 insertions(+), 43 deletions(-)

diff --git a/sys/compat/linprocfs/linprocfs.c b/sys/compat/linprocfs/linprocfs.c
index 15c4d3c41749..f2a8c5833c82 100644
--- a/sys/compat/linprocfs/linprocfs.c
+++ b/sys/compat/linprocfs/linprocfs.c
@@ -1474,26 +1474,79 @@ linprocfs_doprocmem(PFS_FILL_ARGS)
 	return (error);
 }
 
+struct linux_ifname_cb_s {
+	struct ifnet *ifp;
+	int ethno;
+	char *buffer;
+	size_t buflen;
+};
+
+static int
+linux_ifname_cb(if_t ifp, void *arg)
+{
+	struct linux_ifname_cb_s *cbs = arg;
+
+	if (ifp == cbs->ifp)
+		return (snprintf(cbs->buffer, cbs->buflen, "eth%d", cbs->ethno));
+	if (!linux_use_real_ifname(ifp))
+		cbs->ethno++;
+	return (0);
+}
+
 static int
 linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
 {
-	struct ifnet *ifscan;
-	int ethno;
+	struct linux_ifname_cb_s arg;
 
 	IFNET_RLOCK_ASSERT();
 
+	arg.ifp = ifp;
+	arg.buffer = buffer;
+	arg.buflen = buflen;
+	arg.ethno = 0;
+
 	/* Short-circuit non ethernet interfaces */
 	if (linux_use_real_ifname(ifp))
-		return (strlcpy(buffer, ifp->if_xname, buflen));
+		return (strlcpy(buffer, if_name(ifp), buflen));
 
 	/* Determine the (relative) unit number for ethernet interfaces */
-	ethno = 0;
-	CK_STAILQ_FOREACH(ifscan, &V_ifnet, if_link) {
-		if (ifscan == ifp)
-			return (snprintf(buffer, buflen, "eth%d", ethno));
-		if (!linux_use_real_ifname(ifscan))
-			ethno++;
-	}
+	return (if_foreach(linux_ifname_cb, &arg));
+}
+
+static int
+linprocfs_donetdev_cb(if_t ifp, void *arg)
+{
+	char ifname[16]; /* XXX LINUX_IFNAMSIZ */
+	struct sbuf *sb = arg;
+
+	linux_ifname(ifp, ifname, sizeof ifname);
+	sbuf_printf(sb, "%6.6s: ", ifname);
+	sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_IBYTES),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_IPACKETS),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_IERRORS),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_IQDROPS),
+						/* rx_missed_errors */
+	    0UL,				/* rx_fifo_errors */
+	    0UL,				/* rx_length_errors +
+						 * rx_over_errors +
+						 * rx_crc_errors +
+						 * rx_frame_errors */
+	    0UL,				/* rx_compressed */
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_IMCASTS));
+						/* XXX-BZ rx only? */
+	sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_OBYTES),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_OPACKETS),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_OERRORS),
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_OQDROPS),
+	    0UL,				/* tx_fifo_errors */
+	    (uintmax_t )if_getcounter(ifp, IFCOUNTER_COLLISIONS),
+	    0UL,				/* tx_carrier_errors +
+						 * tx_aborted_errors +
+						 * tx_window_errors +
+						 * tx_heartbeat_errors*/
+	    0UL);				/* tx_compressed */
 
 	return (0);
 }
@@ -1504,9 +1557,6 @@ linux_ifname(struct ifnet *ifp, char *buffer, size_t buflen)
 static int
 linprocfs_donetdev(PFS_FILL_ARGS)
 {
-	char ifname[16]; /* XXX LINUX_IFNAMSIZ */
-	struct ifnet *ifp;
-
 	sbuf_printf(sb, "%6s|%58s|%s\n"
 	    "%6s|%58s|%58s\n",
 	    "Inter-", "   Receive", "  Transmit",
@@ -1516,36 +1566,7 @@ linprocfs_donetdev(PFS_FILL_ARGS)
 
 	CURVNET_SET(TD_TO_VNET(curthread));
 	IFNET_RLOCK();
-	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
-		linux_ifname(ifp, ifname, sizeof ifname);
-		sbuf_printf(sb, "%6.6s: ", ifname);
-		sbuf_printf(sb, "%7ju %7ju %4ju %4ju %4lu %5lu %10lu %9ju ",
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IBYTES),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IPACKETS),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IERRORS),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IQDROPS),
-							/* rx_missed_errors */
-		    0UL,				/* rx_fifo_errors */
-		    0UL,				/* rx_length_errors +
-							 * rx_over_errors +
-							 * rx_crc_errors +
-							 * rx_frame_errors */
-		    0UL,				/* rx_compressed */
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_IMCASTS));
-							/* XXX-BZ rx only? */
-		sbuf_printf(sb, "%8ju %7ju %4ju %4ju %4lu %5ju %7lu %10lu\n",
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OBYTES),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OPACKETS),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OERRORS),
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS),
-		    0UL,				/* tx_fifo_errors */
-		    (uintmax_t )ifp->if_get_counter(ifp, IFCOUNTER_COLLISIONS),
-		    0UL,				/* tx_carrier_errors +
-							 * tx_aborted_errors +
-							 * tx_window_errors +
-							 * tx_heartbeat_errors*/
-		    0UL);				/* tx_compressed */
-	}
+	if_foreach(linprocfs_donetdev_cb, sb);
 	IFNET_RUNLOCK();
 	CURVNET_RESTORE();