Module Name:    src
Committed By:   roy
Date:           Thu Sep 26 09:59:56 UTC 2024

Modified Files:
        src/share/man/man4: vether.4
        src/sys/net: if_vether.c

Log Message:
vether(4): control link state via media rather than flags

This mirrors shmif(4) from rump.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/share/man/man4/vether.4
cvs rdiff -u -r1.3 -r1.4 src/sys/net/if_vether.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man4/vether.4
diff -u src/share/man/man4/vether.4:1.5 src/share/man/man4/vether.4:1.6
--- src/share/man/man4/vether.4:1.5	Tue Sep 24 15:23:53 2024
+++ src/share/man/man4/vether.4	Thu Sep 26 09:59:56 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: vether.4,v 1.5 2024/09/24 15:23:53 roy Exp $
+.\"	$NetBSD: vether.4,v 1.6 2024/09/26 09:59:56 roy Exp $
 .\"
 .\"	$OpenBSD: vether.4,v 1.5 2017/10/17 22:47:58 schwarze Exp $
 .\"
@@ -16,7 +16,7 @@
 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 .\"
-.Dd September 24, 2017
+.Dd September 26, 2017
 .Dt VETHER 4
 .Os
 .Sh NAME
@@ -48,10 +48,10 @@ link state can be controlled using
 .Xr ifconfig 8 :
 .Pp
 .Bl -tag -offset indent -width Cm -compact
-.It Cm link0
+.It Cm media auto
 link state
 .Dq up
-.It Fl link0
+.It Cm media none
 link state
 .Dq down
 .El

Index: src/sys/net/if_vether.c
diff -u src/sys/net/if_vether.c:1.3 src/sys/net/if_vether.c:1.4
--- src/sys/net/if_vether.c:1.3	Tue Sep 24 15:23:53 2024
+++ src/sys/net/if_vether.c	Thu Sep 26 09:59:55 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_vether.c,v 1.3 2024/09/24 15:23:53 roy Exp $	*/
+/*	$NetBSD: if_vether.c,v 1.4 2024/09/26 09:59:55 roy Exp $	*/
 /* $OpenBSD: if_vether.c,v 1.27 2016/04/13 11:41:15 mpi Exp $ */
 
 /*
@@ -19,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_vether.c,v 1.3 2024/09/24 15:23:53 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_vether.c,v 1.4 2024/09/26 09:59:55 roy Exp $");
 
 #include <sys/cprng.h>
 #include <sys/kmem.h>
@@ -27,10 +27,13 @@ __KERNEL_RCSID(0, "$NetBSD: if_vether.c,
 
 #include <net/if.h>
 #include <net/if_ether.h>
+#include <net/if_media.h>
 #include <net/bpf.h>
 
 void		vetherattach(int);
 static int	vether_ioctl(struct ifnet *, u_long, void *);
+static int	vether_mediachange(struct ifnet *);
+static void	vether_mediastatus(struct ifnet *, struct ifmediareq *);
 static void	vether_start(struct ifnet *);
 static int	vether_clone_create(struct if_clone *, int);
 static int	vether_clone_destroy(struct ifnet *);
@@ -40,6 +43,7 @@ static int	vether_init(struct ifnet *);
 
 struct vether_softc {
 	struct ethercom		sc_ec;
+	struct ifmedia		sc_im;
 };
 
 struct if_clone	vether_cloner =
@@ -61,10 +65,17 @@ vether_clone_create(struct if_clone *ifc
 	    { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
 
 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
+
+	sc->sc_ec.ec_ifmedia = &sc->sc_im;
+	ifmedia_init(&sc->sc_im, 0, vether_mediachange, vether_mediastatus);
+	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
+	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_NONE, 0, NULL);
+	ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
+
 	ifp = &sc->sc_ec.ec_if;
 	if_initname(ifp, ifc->ifc_name, unit);
 	ifp->if_softc = sc;
-	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | IFF_LINK0;
+	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 #ifdef NET_MPSAFE
 	ifp->if_extflags = IFEF_MPSAFE;
 #endif
@@ -89,7 +100,8 @@ vether_clone_create(struct if_clone *ifc
 	ether_ifattach(ifp, enaddr);
 	if_register(ifp);
 
-	if_link_state_change(ifp, LINK_STATE_UP);
+	/* Notify our link state */
+	vether_mediachange(ifp);
 
 	return 0;
 }
@@ -114,6 +126,33 @@ vether_init(struct ifnet *ifp)
 	return 0;
 }
 
+static int
+vether_mediachange(struct ifnet *ifp)
+{
+	struct vether_softc *sc = ifp->if_softc;
+	int link_state;
+
+	if (IFM_SUBTYPE(sc->sc_im.ifm_cur->ifm_media) == IFM_NONE)
+		link_state = LINK_STATE_DOWN;
+	else
+		link_state = LINK_STATE_UP;
+
+	if_link_state_change(ifp, link_state);
+	return 0;
+}
+
+static void
+vether_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
+{
+	struct vether_softc *sc = ifp->if_softc;
+
+	imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
+
+	imr->ifm_status = IFM_AVALID;
+	if (IFM_SUBTYPE(imr->ifm_active) != IFM_NONE)
+		imr->ifm_status |= IFM_ACTIVE;
+}
+
 /*
  * The bridge has magically already done all the work for us,
  * and we only need to discard the packets.
@@ -150,13 +189,6 @@ vether_ioctl(struct ifnet *ifp, unsigned
 	case SIOCDELMULTI:
 		break;
 
-        case SIOCSIFFLAGS:
-                if ((error = ifioctl_common(ifp, cmd, data)) != 0)
-			break;
-		if_link_state_change(ifp, ifp->if_flags & IFF_LINK0 ?
-		    LINK_STATE_UP : LINK_STATE_DOWN);
-		break;
-
 	default:
 		error = ether_ioctl(ifp, cmd, data);
 	}

Reply via email to