Index | Thread | Search

From:
David Gwynne <david@gwynne.id.au>
Subject:
convert bpf interface list into a tailq
To:
tech@openbsd.org
Date:
Tue, 19 Nov 2024 22:17:43 +1000

Download raw body.

Thread
at the moment it's a good old hand rolled list.

we have clean water^W^Wsys/queue.h now, so let's use it. this diff also
inserts new bpf_ifs at the end of the list instead of the head. this
means that the list of DLTs per interface will change order, eg, what
tcpdump -i iwx0 -L prints will change order. currently attachinig
a bpf descriptor to an interface prefers low numbered DLTs, so this
doesnt change which one is picked if you run somethinig like tcpdump
without specifying a dlt.

ok?

Index: bpfdesc.h
===================================================================
RCS file: /cvs/src/sys/net/bpfdesc.h,v
diff -u -p -r1.49 bpfdesc.h
--- bpfdesc.h	15 Aug 2024 12:20:20 -0000	1.49
+++ bpfdesc.h	19 Nov 2024 12:07:56 -0000
@@ -114,7 +114,7 @@ struct bpf_d {
  * Descriptor associated with each attached hardware interface.
  */
 struct bpf_if {
-	struct bpf_if *bif_next;	/* list of all interfaces */
+	TAILQ_ENTRY(bpf_if) bif_next;	/* list of all interfaces */
 	SMR_SLIST_HEAD(, bpf_d) bif_dlist;		/* descriptor list */
 	struct bpf_if **bif_driverp;	/* pointer into softc */
 	u_int bif_dlt;			/* link layer type */
Index: bpf.c
===================================================================
RCS file: /cvs/src/sys/net/bpf.c,v
diff -u -p -r1.226 bpf.c
--- bpf.c	17 Nov 2024 12:21:48 -0000	1.226
+++ bpf.c	19 Nov 2024 12:07:56 -0000
@@ -98,8 +98,8 @@ int bpf_maxbufsize = BPF_MAXBUFSIZE;	/* 
  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
  *  bpf_d_list is the list of descriptors
  */
-struct bpf_if	*bpf_iflist;
-LIST_HEAD(, bpf_d) bpf_d_list;
+TAILQ_HEAD(, bpf_if) bpf_iflist = TAILQ_HEAD_INITIALIZER(bpf_iflist);
+LIST_HEAD(, bpf_d) bpf_d_list = LIST_HEAD_INITIALIZER(bpf_d_list);
 
 int	bpf_allocbufs(struct bpf_d *);
 void	bpf_ifname(struct bpf_if*, struct ifreq *);
@@ -369,7 +369,6 @@ bpf_detachd(struct bpf_d *d)
 void
 bpfilterattach(int n)
 {
-	LIST_INIT(&bpf_d_list);
 }
 
 /*
@@ -1187,7 +1186,7 @@ bpf_setif(struct bpf_d *d, struct ifreq 
 	/*
 	 * Look through attached interfaces for the named one.
 	 */
-	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
+	TAILQ_FOREACH(bp, &bpf_iflist, bif_next) {
 		if (strcmp(bp->bif_name, ifr->ifr_name) != 0)
 			continue;
 
@@ -1742,8 +1741,7 @@ bpfsattach(caddr_t *bpfp, const char *na
 	bp->bif_ifp = NULL;
 	bp->bif_dlt = dlt;
 
-	bp->bif_next = bpf_iflist;
-	bpf_iflist = bp;
+	TAILQ_INSERT_TAIL(&bpf_iflist, bp, bif_next);
 
 	*bp->bif_driverp = NULL;
 
@@ -1775,8 +1782,7 @@ bpfdetach(struct ifnet *ifp)
 
 	KERNEL_ASSERT_LOCKED();
 
-	for (bp = bpf_iflist; bp; bp = nbp) {
-		nbp = bp->bif_next;
+	TAILQ_FOREACH_SAFE(bp, &bpf_iflist, bif_next, nbp) {
 		if (bp->bif_ifp == ifp)
 			bpfsdetach(bp);
 	}
@@ -1786,7 +1792,7 @@ bpfdetach(struct ifnet *ifp)
 void
 bpfsdetach(void *p)
 {
-	struct bpf_if *bp = p, *tbp;
+	struct bpf_if *bp = p;
 	struct bpf_d *bd;
 	int maj;
 
@@ -1804,15 +1810,7 @@ bpfsdetach(void *p)
 		bpf_put(bd);
 	}
 
-	for (tbp = bpf_iflist; tbp; tbp = tbp->bif_next) {
-		if (tbp->bif_next == bp) {
-			tbp->bif_next = bp->bif_next;
-			break;
-		}
-	}
-
-	if (bpf_iflist == bp)
-		bpf_iflist = bp->bif_next;
+	TAILQ_REMOVE(&bpf_iflist, bp, bif_next);
 
 	free(bp, M_DEVBUF, sizeof(*bp));
 }
@@ -1865,7 +1863,7 @@ bpf_getdltlist(struct bpf_d *d, struct b
 	name = d->bd_bif->bif_name;
 	n = 0;
 	error = 0;
-	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
+	TAILQ_FOREACH (bp, &bpf_iflist, bif_next) {
 		if (strcmp(name, bp->bif_name) != 0)
 			continue;
 		if (bfl->bfl_list != NULL) {
@@ -1896,7 +1894,7 @@ bpf_setdlt(struct bpf_d *d, u_int dlt)
 	if (d->bd_bif->bif_dlt == dlt)
 		return (0);
 	name = d->bd_bif->bif_name;
-	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
+	TAILQ_FOREACH (bp, &bpf_iflist, bif_next) {
 		if (strcmp(name, bp->bif_name) != 0)
 			continue;
 		if (bp->bif_dlt == dlt)