From owner-svn-src-stable@freebsd.org Mon Apr 10 05:17:20 2017 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 76996D28964; Mon, 10 Apr 2017 05:17:20 +0000 (UTC) (envelope-from sephe@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3474E7EF; Mon, 10 Apr 2017 05:17:20 +0000 (UTC) (envelope-from sephe@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v3A5HJNT039705; Mon, 10 Apr 2017 05:17:19 GMT (envelope-from sephe@FreeBSD.org) Received: (from sephe@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v3A5HJT6039702; Mon, 10 Apr 2017 05:17:19 GMT (envelope-from sephe@FreeBSD.org) Message-Id: <201704100517.v3A5HJT6039702@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: sephe set sender to sephe@FreeBSD.org using -f From: Sepherosa Ziehau Date: Mon, 10 Apr 2017 05:17:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r316674 - stable/11/sys/dev/hyperv/netvsc X-SVN-Group: stable-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Apr 2017 05:17:20 -0000 Author: sephe Date: Mon Apr 10 05:17:18 2017 New Revision: 316674 URL: https://svnweb.freebsd.org/changeset/base/316674 Log: MFC 316520 hyperv/hn: Fixat RNDIS rxfilter after the successful RNDIS init. Under certain conditions on certain versions of Hyper-V, the RNDIS rxfilter is _not_ zero on the hypervisor side after the successful RNDIS initialization, which breaks the assumption of any following code (well, it breaks the RNDIS API contract actually). Clear the RNDIS rxfilter explicitly, drain packets sneaking through, and drain the interrupt taskqueues scheduled due to the stealth packets. Reported by: dexuan@ Sponsored by: Microsoft Differential Revision: https://reviews.freebsd.org/D10230 Modified: stable/11/sys/dev/hyperv/netvsc/hn_rndis.c stable/11/sys/dev/hyperv/netvsc/hn_rndis.h stable/11/sys/dev/hyperv/netvsc/if_hn.c Directory Properties: stable/11/ (props changed) Modified: stable/11/sys/dev/hyperv/netvsc/hn_rndis.c ============================================================================== --- stable/11/sys/dev/hyperv/netvsc/hn_rndis.c Mon Apr 10 04:59:44 2017 (r316673) +++ stable/11/sys/dev/hyperv/netvsc/hn_rndis.c Mon Apr 10 05:17:18 2017 (r316674) @@ -979,16 +979,19 @@ hn_rndis_query_hwcaps(struct hn_softc *s } int -hn_rndis_attach(struct hn_softc *sc, int mtu) +hn_rndis_attach(struct hn_softc *sc, int mtu, int *init_done) { int error; + *init_done = 0; + /* * Initialize RNDIS. */ error = hn_rndis_init(sc); if (error) return (error); + *init_done = 1; /* * Configure NDIS offload settings. Modified: stable/11/sys/dev/hyperv/netvsc/hn_rndis.h ============================================================================== --- stable/11/sys/dev/hyperv/netvsc/hn_rndis.h Mon Apr 10 04:59:44 2017 (r316673) +++ stable/11/sys/dev/hyperv/netvsc/hn_rndis.h Mon Apr 10 05:17:18 2017 (r316674) @@ -33,7 +33,7 @@ struct hn_softc; -int hn_rndis_attach(struct hn_softc *sc, int mtu); +int hn_rndis_attach(struct hn_softc *sc, int mtu, int *init_done); void hn_rndis_detach(struct hn_softc *sc); int hn_rndis_conf_rss(struct hn_softc *sc, uint16_t flags); int hn_rndis_query_rsscaps(struct hn_softc *sc, int *rxr_cnt); Modified: stable/11/sys/dev/hyperv/netvsc/if_hn.c ============================================================================== --- stable/11/sys/dev/hyperv/netvsc/if_hn.c Mon Apr 10 04:59:44 2017 (r316673) +++ stable/11/sys/dev/hyperv/netvsc/if_hn.c Mon Apr 10 05:17:18 2017 (r316674) @@ -261,6 +261,7 @@ static void hn_rndis_rx_data(struct hn const void *, int); static void hn_rndis_rx_status(struct hn_softc *, const void *, int); +static void hn_rndis_init_fixat(struct hn_softc *, int); static void hn_nvs_handle_notify(struct hn_softc *, const struct vmbus_chanpkt_hdr *); @@ -328,6 +329,8 @@ static void hn_resume_mgmt(struct hn_s static void hn_suspend_mgmt_taskfunc(void *, int); static void hn_chan_drain(struct hn_softc *, struct vmbus_channel *); +static void hn_disable_rx(struct hn_softc *); +static void hn_drain_rxtx(struct hn_softc *, int); static void hn_polling(struct hn_softc *, u_int); static void hn_chan_polling(struct vmbus_channel *, u_int); @@ -2267,6 +2270,18 @@ hn_rxpkt(struct hn_rx_ring *rxr, const v /* If the VF is active, inject the packet through the VF */ ifp = rxr->hn_vf ? rxr->hn_vf : rxr->hn_ifp; + if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { + /* + * NOTE: + * See the NOTE of hn_rndis_init_fixat(). This + * function can be reached, immediately after the + * RNDIS is initialized but before the ifnet is + * setup on the hn_attach() path; drop the unexpected + * packets. + */ + return (0); + } + if (dlen <= MHLEN) { m_new = m_gethdr(M_NOWAIT, MT_DATA); if (m_new == NULL) { @@ -4711,6 +4726,27 @@ hn_synth_attachable(const struct hn_soft return (true); } +/* + * Make sure that the RX filter is zero after the successful + * RNDIS initialization. + * + * NOTE: + * Under certain conditions on certain versions of Hyper-V, + * the RNDIS rxfilter is _not_ zero on the hypervisor side + * after the successful RNDIS initialization, which breaks + * the assumption of any following code (well, it breaks the + * RNDIS API contract actually). Clear the RNDIS rxfilter + * explicitly, drain packets sneaking through, and drain the + * interrupt taskqueues scheduled due to the stealth packets. + */ +static void +hn_rndis_init_fixat(struct hn_softc *sc, int nchan) +{ + + hn_disable_rx(sc); + hn_drain_rxtx(sc, nchan); +} + static int hn_synth_attach(struct hn_softc *sc, int mtu) { @@ -4718,7 +4754,7 @@ hn_synth_attach(struct hn_softc *sc, int #define ATTACHED_RNDIS 0x0004 struct ndis_rssprm_toeplitz *rss = &sc->hn_rss; - int error, nsubch, nchan, i; + int error, nsubch, nchan = 1, i, rndis_inited; uint32_t old_caps, attached = 0; KASSERT((sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) == 0, @@ -4753,10 +4789,11 @@ hn_synth_attach(struct hn_softc *sc, int /* * Attach RNDIS _after_ NVS is attached. */ - error = hn_rndis_attach(sc, mtu); + error = hn_rndis_attach(sc, mtu, &rndis_inited); + if (rndis_inited) + attached |= ATTACHED_RNDIS; if (error) goto failed; - attached |= ATTACHED_RNDIS; /* * Make sure capabilities are not changed. @@ -4859,14 +4896,18 @@ back: * Fixup transmission aggregation setup. */ hn_set_txagg(sc); + hn_rndis_init_fixat(sc, nchan); return (0); failed: if (sc->hn_flags & HN_FLAG_SYNTH_ATTACHED) { + hn_rndis_init_fixat(sc, nchan); hn_synth_detach(sc); } else { - if (attached & ATTACHED_RNDIS) + if (attached & ATTACHED_RNDIS) { + hn_rndis_init_fixat(sc, nchan); hn_rndis_detach(sc); + } if (attached & ATTACHED_NVS) hn_nvs_detach(sc); hn_chan_detach(sc, sc->hn_prichan); @@ -4946,11 +4987,56 @@ hn_chan_drain(struct hn_softc *sc, struc } static void -hn_suspend_data(struct hn_softc *sc) +hn_disable_rx(struct hn_softc *sc) +{ + + /* + * Disable RX by clearing RX filter forcefully. + */ + sc->hn_rx_filter = NDIS_PACKET_TYPE_NONE; + hn_rndis_set_rxfilter(sc, sc->hn_rx_filter); /* ignore error */ + + /* + * Give RNDIS enough time to flush all pending data packets. + */ + pause("waitrx", (200 * hz) / 1000); +} + +/* + * NOTE: + * RX/TX _must_ have been suspended/disabled, before this function + * is called. + */ +static void +hn_drain_rxtx(struct hn_softc *sc, int nchan) { struct vmbus_channel **subch = NULL; + int nsubch; + + /* + * Drain RX/TX bufrings and interrupts. + */ + nsubch = nchan - 1; + if (nsubch > 0) + subch = vmbus_subchan_get(sc->hn_prichan, nsubch); + + if (subch != NULL) { + int i; + + for (i = 0; i < nsubch; ++i) + hn_chan_drain(sc, subch[i]); + } + hn_chan_drain(sc, sc->hn_prichan); + + if (subch != NULL) + vmbus_subchan_rel(subch, nsubch); +} + +static void +hn_suspend_data(struct hn_softc *sc) +{ struct hn_tx_ring *txr; - int i, nsubch; + int i; HN_LOCK_ASSERT(sc); @@ -4978,38 +5064,21 @@ hn_suspend_data(struct hn_softc *sc) } /* - * Disable RX by clearing RX filter. + * Disable RX. */ - hn_set_rxfilter(sc, NDIS_PACKET_TYPE_NONE); + hn_disable_rx(sc); /* - * Give RNDIS enough time to flush all pending data packets. + * Drain RX/TX. */ - pause("waitrx", (200 * hz) / 1000); - - /* - * Drain RX/TX bufrings and interrupts. - */ - nsubch = sc->hn_rx_ring_inuse - 1; - if (nsubch > 0) - subch = vmbus_subchan_get(sc->hn_prichan, nsubch); - - if (subch != NULL) { - for (i = 0; i < nsubch; ++i) - hn_chan_drain(sc, subch[i]); - } - hn_chan_drain(sc, sc->hn_prichan); - - if (subch != NULL) - vmbus_subchan_rel(subch, nsubch); + hn_drain_rxtx(sc, sc->hn_rx_ring_inuse); /* * Drain any pending TX tasks. * * NOTE: - * The above hn_chan_drain() can dispatch TX tasks, so the TX - * tasks will have to be drained _after_ the above hn_chan_drain() - * calls. + * The above hn_drain_rxtx() can dispatch TX tasks, so the TX + * tasks will have to be drained _after_ the above hn_drain_rxtx(). */ for (i = 0; i < sc->hn_tx_ring_inuse; ++i) { txr = &sc->hn_tx_ring[i];