Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Dec 2025 20:47:00 +0000
From:      Gleb Smirnoff <glebius@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 0bf42a0a05b9 - main - bpf: virtualize bpf_iflist
Message-ID:  <694316c4.3d86b.2da7d434@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by glebius:

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

commit 0bf42a0a05b9c802a6d9ca4a6b8696b29a26e08b
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-12-17 20:41:05 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-12-17 20:41:05 +0000

    bpf: virtualize bpf_iflist
    
    The reason the global list worked before 8774a990ee40 is that bpf_setif()
    used if_unit(), which is a VNET-aware function, and then went through the
    global list looking for bpf_if with matching pointer.
    
    PR:     291735
    Fixes:  8774a990ee4094f16d596d4b78e0f3239e5d0c88
---
 sys/net/bpf.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/sys/net/bpf.c b/sys/net/bpf.c
index 4026c44112ab..54da49f3d656 100644
--- a/sys/net/bpf.c
+++ b/sys/net/bpf.c
@@ -221,7 +221,8 @@ struct bpf_dltlist32 {
  * structures registered by different layers in the stack (i.e., 802.11
  * frames, ethernet frames, etc).
  */
-static LIST_HEAD(, bpf_if) bpf_iflist = LIST_HEAD_INITIALIZER();
+VNET_DEFINE_STATIC(LIST_HEAD(, bpf_if), bpf_iflist) = LIST_HEAD_INITIALIZER();
+#define	V_bpf_iflist	VNET(bpf_iflist)
 static struct sx	bpf_sx;		/* bpf global lock */
 
 static void	bpfif_ref(struct bpf_if *);
@@ -257,7 +258,8 @@ SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
 static int bpf_zerocopy_enable = 0;
 SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
     &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
-static SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
+static SYSCTL_NODE(_net_bpf, OID_AUTO, stats,
+    CTLFLAG_VNET | CTLFLAG_MPSAFE | CTLFLAG_RW,
     bpf_stats_sysctl, "bpf statistics portal");
 
 VNET_DEFINE_STATIC(int, bpf_optimize_writers) = 0;
@@ -1401,7 +1403,7 @@ bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
 		/*
 		 * Look through attached interfaces for the named one.
 		 */
-		LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+		LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 			if (strncmp(ifr->ifr_name, bp->bif_name,
 			    sizeof(ifr->ifr_name)) == 0)
 				break;
@@ -1728,7 +1730,7 @@ bpf_getiflist(struct bpf_iflist *bi)
 	BPF_LOCK();
 
 	cnt = allsize = size = 0;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		allsize += strlen(bp->bif_name) + 1;
 		if (++cnt == bi->bi_count)
 			size = allsize;
@@ -1748,7 +1750,7 @@ bpf_getiflist(struct bpf_iflist *bi)
 
 	uaddr = bi->bi_ubuf;
 	cnt = 0;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		u_int len;
 		int error;
 
@@ -2654,7 +2656,7 @@ bpf_attach(const char *name, u_int dlt, u_int hdrlen,
 	bp->bif_methods = methods;
 	refcount_init(&bp->bif_refcnt, 1);
 	BPF_LOCK();
-	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
+	LIST_INSERT_HEAD(&V_bpf_iflist, bp, bif_next);
 	BPF_UNLOCK();
 
 	return (bp);
@@ -2672,7 +2674,7 @@ bpf_ifdetach(struct ifnet *ifp)
 	struct bpf_d *d;
 
 	BPF_LOCK();
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		/* XXXGL: assuming softc is ifnet here */
 		if (bp->bif_softc != ifp)
 			continue;
@@ -2743,7 +2745,7 @@ bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
 
 	name = d->bd_bif->bif_name;
 	n1 = 0;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		if (bp->bif_name == name)
 			n1++;
 	}
@@ -2756,7 +2758,7 @@ bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
 
 	lst = malloc(n1 * sizeof(u_int), M_TEMP, M_WAITOK);
 	n = 0;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		if (bp->bif_name != name)
 			continue;
 		lst[n++] = bp->bif_dlt;
@@ -2795,7 +2797,7 @@ bpf_setdlt(struct bpf_d *d, u_int dlt)
 		return (0);
 
 	name = d->bd_bif->bif_name;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		if (bp->bif_name == name && bp->bif_dlt == dlt)
 			break;
 	}
@@ -2843,7 +2845,7 @@ bpf_zero_counters(void)
 	 * We are protected by global lock here, interfaces and
 	 * descriptors can not be deleted while we hold it.
 	 */
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
 			counter_u64_zero(bd->bd_rcount);
 			counter_u64_zero(bd->bd_dcount);
@@ -2930,7 +2932,7 @@ bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
 	}
 	bpfd_cnt = 0;
 	BPF_LOCK();
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		CK_LIST_FOREACH(bd, &bp->bif_wlist, bd_next)
 			bpfd_cnt++;
 		CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next)
@@ -2946,7 +2948,7 @@ bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
 	}
 	xbdbuf = malloc(bpfd_cnt * sizeof(*xbd), M_BPF, M_WAITOK);
 	index = 0;
-	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
+	LIST_FOREACH(bp, &V_bpf_iflist, bif_next) {
 		/* Send writers-only first */
 		CK_LIST_FOREACH(bd, &bp->bif_wlist, bd_next) {
 			MPASS(index <= bpfd_cnt);


help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?694316c4.3d86b.2da7d434>