From owner-dev-commits-src-main@freebsd.org Tue Jan 19 05:08:22 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0DD7D4E52CA; Tue, 19 Jan 2021 05:08:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DKc9w5bmZz4XBt; Tue, 19 Jan 2021 05:08:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 892842066C; Tue, 19 Jan 2021 05:08:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10J58GGq085848; Tue, 19 Jan 2021 05:08:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10J58GkG085847; Tue, 19 Jan 2021 05:08:16 GMT (envelope-from git) Date: Tue, 19 Jan 2021 05:08:16 GMT Message-Id: <202101190508.10J58GkG085847@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Bryan Venteicher Subject: git: 05041794d06f - main - if_vtnet: Defer updating generated MAC address until attached MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bryanv X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 05041794d06f973cf6b4fb3b14a90718656f7f5a Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Jan 2021 05:08:22 -0000 The branch main has been updated by bryanv: URL: https://cgit.FreeBSD.org/src/commit/?id=05041794d06f973cf6b4fb3b14a90718656f7f5a commit 05041794d06f973cf6b4fb3b14a90718656f7f5a Author: Bryan Venteicher AuthorDate: 2021-01-19 04:55:24 +0000 Commit: Bryan Venteicher CommitDate: 2021-01-19 04:55:24 +0000 if_vtnet: Defer updating generated MAC address until attached This improves spec compliance because the driver is not suppose to notify the device prior to setting the DRIVER_OK status, which could happen with the VIRTIO_NET_F_CTRL_MAC_ADDR. The VIRTIO_NET_F_MAC feature should always be negotiated so would be a rare situation. Reviewed by: grehan (mentor) Differential Revision: https://reviews.freebsd.org/D27910 --- sys/dev/virtio/network/if_vtnet.c | 60 ++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/sys/dev/virtio/network/if_vtnet.c b/sys/dev/virtio/network/if_vtnet.c index 4a26e403071d..f55bd59ef749 100644 --- a/sys/dev/virtio/network/if_vtnet.c +++ b/sys/dev/virtio/network/if_vtnet.c @@ -214,8 +214,9 @@ static int vtnet_is_link_up(struct vtnet_softc *); static void vtnet_update_link_status(struct vtnet_softc *); static int vtnet_ifmedia_upd(struct ifnet *); static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *); -static void vtnet_get_hwaddr(struct vtnet_softc *); -static void vtnet_set_hwaddr(struct vtnet_softc *); +static void vtnet_get_macaddr(struct vtnet_softc *); +static void vtnet_set_macaddr(struct vtnet_softc *); +static void vtnet_attached_set_macaddr(struct vtnet_softc *); static void vtnet_vlan_tag_remove(struct mbuf *); static void vtnet_set_rx_process_limit(struct vtnet_softc *); static void vtnet_set_tx_intr_threshold(struct vtnet_softc *); @@ -568,6 +569,13 @@ vtnet_shutdown(device_t dev) static int vtnet_attach_completed(device_t dev) { + struct vtnet_softc *sc; + + sc = device_get_softc(dev); + + VTNET_CORE_LOCK(sc); + vtnet_attached_set_macaddr(sc); + VTNET_CORE_UNLOCK(sc); return (0); } @@ -1016,7 +1024,7 @@ vtnet_setup_interface(struct vtnet_softc *sc) ifmedia_add(&sc->vtnet_media, IFM_ETHER | IFM_AUTO, 0, NULL); ifmedia_set(&sc->vtnet_media, IFM_ETHER | IFM_AUTO); - vtnet_get_hwaddr(sc); + vtnet_get_macaddr(sc); ether_ifattach(ifp, sc->vtnet_hwaddr); if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) @@ -3108,7 +3116,7 @@ vtnet_reinit(struct vtnet_softc *sc) /* Use the current MAC address. */ bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN); - vtnet_set_hwaddr(sc); + vtnet_set_macaddr(sc); vtnet_set_active_vq_pairs(sc); @@ -3669,22 +3677,36 @@ vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) } static void -vtnet_set_hwaddr(struct vtnet_softc *sc) +vtnet_get_macaddr(struct vtnet_softc *sc) { - device_t dev; - dev = sc->vtnet_dev; + if (sc->vtnet_flags & VTNET_FLAG_MAC) { + virtio_read_device_config_array(sc->vtnet_dev, + offsetof(struct virtio_net_config, mac), + &sc->vtnet_hwaddr[0], sizeof(uint8_t), ETHER_ADDR_LEN); + } else { + /* Generate a random locally administered unicast address. */ + sc->vtnet_hwaddr[0] = 0xB2; + arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0); + } +} + +static void +vtnet_set_macaddr(struct vtnet_softc *sc) +{ + int error; if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) { - if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0) - device_printf(dev, "unable to set MAC address\n"); + error = vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr); + if (error) + if_printf(sc->vtnet_ifp, "unable to set MAC address\n"); return; } - /* In modern VirtIO the MAC config is read-only. */ + /* MAC in config is read-only in modern VirtIO. */ if (!vtnet_modern(sc) && sc->vtnet_flags & VTNET_FLAG_MAC) { for (int i = 0; i < ETHER_ADDR_LEN; i++) { - virtio_write_dev_config_1(dev, + virtio_write_dev_config_1(sc->vtnet_dev, offsetof(struct virtio_net_config, mac) + i, sc->vtnet_hwaddr[i]); } @@ -3692,20 +3714,12 @@ vtnet_set_hwaddr(struct vtnet_softc *sc) } static void -vtnet_get_hwaddr(struct vtnet_softc *sc) +vtnet_attached_set_macaddr(struct vtnet_softc *sc) { - if (sc->vtnet_flags & VTNET_FLAG_MAC) { - virtio_read_device_config_array(sc->vtnet_dev, - offsetof(struct virtio_net_config, mac), - &sc->vtnet_hwaddr[0], sizeof(uint8_t), ETHER_ADDR_LEN); - } else { - /* Generate a random locally administered unicast address. */ - sc->vtnet_hwaddr[0] = 0xB2; - arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0); - /* BMV: FIXME Cannot do before DRIVER_OK! See 3.1.2 */ - vtnet_set_hwaddr(sc); - } + /* Assign MAC address if it was generated. */ + if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) + vtnet_set_macaddr(sc); } static void