From nobody Thu Jan 27 06:00:02 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 87752196DD3F; Thu, 27 Jan 2022 06:00:03 +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 4JkqgR24f2z4qNc; Thu, 27 Jan 2022 06:00:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1643263203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0JlLKAREMTUM1v4SmrAN4kqGmX7Rh8nzkerNwZGmfsk=; b=CRn4SQ1k0MT43SLoz0ufXiURz6eHxStI01DgwZ1EzmQQCAe9IG8YzIfPuSgUaiAG21ZxJF pkNBocfvgAREzxSl4FAOGbFD2kFAKCr5n75VnGZ8kf49RcaiRIrivLZA+3YztycqOLPHkN HyQN0PnR28YpQtUrk2p7x3lfFMBgU2VpcnCYI3RJcHHfqWMTDMzKWE6ZFC/yOIj5PFGi3R 5wsdcvfxS1xlsn89nh8OcjU0ImEVu/+8NqMWYuwaCmAqDEr1Zst+hfdwVcvg9GM4ifuKXt XvSjxju7Au2xaHkyMgG4sfhwLd57JHsjkGFoGAneImYwxXdwnxNRUqqO6o75WA== 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 0D48B11587; Thu, 27 Jan 2022 06:00:03 +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 20R602Rd059928; Thu, 27 Jan 2022 06:00:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20R602KH059925; Thu, 27 Jan 2022 06:00:02 GMT (envelope-from git) Date: Thu, 27 Jan 2022 06:00:02 GMT Message-Id: <202201270600.20R602KH059925@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: e1882428dcbb - main - ifnet/mbuf: provide KPI to serialize/restore m->m_pkthdr.rcvif List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e1882428dcbbafd2814d7e17b977a8f686784b39 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1643263203; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=0JlLKAREMTUM1v4SmrAN4kqGmX7Rh8nzkerNwZGmfsk=; b=uJvTy33eEaXV12WQjUng66DHO2rrFirUj+SrhEoAx28v11j2zke3l5cjOPoPhXQScwG9DZ 6mKydRWHyOx4yqeRfNF63fhfB9hHtgCSM5eeniwYFir2+e+H23anXCi2aF4S1AKg+tPUWL 0TRpPNQynUVw+80nrxDXbuubcGjZP6LAPqoL7rfcZV0rQAH5Qx4Vw0oS+fWJUMNXPEKb+Y pBavmC8P+mlmcGuWol9NXyQ2zwYOSe2ZwgJ6m0uMNoNYkbvk44wB4f20LPjWZ//kIeauop lFAJam1fBGUmNcTLeaufHQJat8F2XpxIvRMLWQam8RgUuG3JtkVFeLEYLf6FJA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1643263203; a=rsa-sha256; cv=none; b=u2PhiFYhJRoQ1kqnWJTrRxniSksVtaV83xL7HWKMkrImnucAFrtxBHB7IfsfR6lVME+WVE 3e/y43pMwpMCRZ1xKInrTcJPMKHWOg8FPLCc4q6ryRksoMWwI/i1FsqLseByeq3ik4yCTH DlsjXtUpgd51E7ZY1fS8hBEGRWfIa3ZRFGUVYDF9DXilpLbZW0WUJtaOM+IHDy/JelBuD/ S7krkppRnLqR7Hb9CBQ/9wukrLl3TPRX8OlWMiVjMSh8Q4Tp4cmWXaFAQGH3Nk68ICiMnR CFUFyCBkDzvjd1BbeRAnyS7b3HzHLsP04a0kBoZzGGN8rYm5bJGG0wlcKW++LA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=e1882428dcbbafd2814d7e17b977a8f686784b39 commit e1882428dcbbafd2814d7e17b977a8f686784b39 Author: Gleb Smirnoff AuthorDate: 2022-01-27 05:58:50 +0000 Commit: Gleb Smirnoff CommitDate: 2022-01-27 05:58:50 +0000 ifnet/mbuf: provide KPI to serialize/restore m->m_pkthdr.rcvif Supplement ifindex table with generation count and use it to serialize & restore an ifnet pointer. Reviewed by: kp Differential revision: https://reviews.freebsd.org/D33266 Fun note: git show e6abef09187a --- sys/kern/kern_mbuf.c | 22 ++++++++++++++++++++++ sys/net/if.c | 49 ++++++++++++++++++++++++++++++++++++------------- sys/net/if_var.h | 9 ++++++++- sys/sys/mbuf.h | 6 ++++++ 4 files changed, 72 insertions(+), 14 deletions(-) diff --git a/sys/kern/kern_mbuf.c b/sys/kern/kern_mbuf.c index f1e76ef00c65..5c69f663c0e2 100644 --- a/sys/kern/kern_mbuf.c +++ b/sys/kern/kern_mbuf.c @@ -1635,6 +1635,28 @@ m_snd_tag_destroy(struct m_snd_tag *mst) counter_u64_add(snd_tag_count, -1); } +void +m_rcvif_serialize(struct mbuf *m) +{ + u_short idx, gen; + + M_ASSERTPKTHDR(m); + idx = m->m_pkthdr.rcvif->if_index; + gen = m->m_pkthdr.rcvif->if_idxgen; + m->m_pkthdr.rcvidx = idx; + m->m_pkthdr.rcvgen = gen; +} + +struct ifnet * +m_rcvif_restore(struct mbuf *m) +{ + + M_ASSERTPKTHDR(m); + + return ((m->m_pkthdr.rcvif = ifnet_byindexgen(m->m_pkthdr.rcvidx, + m->m_pkthdr.rcvgen))); +} + /* * Allocate an mbuf with anonymous external pages. */ diff --git a/sys/net/if.c b/sys/net/if.c index f148ae8c9c6d..e8d65e64518a 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -313,7 +313,10 @@ VNET_DEFINE(struct ifgrouphead, ifg_head); /* Table of ifnet by index. */ static int if_index; static int if_indexlim = 8; -static struct ifnet **ifindex_table; +static struct ifindex_entry { + struct ifnet *ife_ifnet; + uint16_t ife_gencnt; +} *ifindex_table; SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, @@ -325,8 +328,8 @@ sysctl_ifcount(SYSCTL_HANDLER_ARGS) IFNET_RLOCK(); for (int i = 1; i <= if_index; i++) - if (ifindex_table[i] != NULL && - ifindex_table[i]->if_vnet == curvnet) + if (ifindex_table[i].ife_ifnet != NULL && + ifindex_table[i].ife_ifnet->if_vnet == curvnet) rv = i; IFNET_RUNLOCK(); @@ -370,7 +373,7 @@ ifnet_byindex(u_int idx) if (__predict_false(idx > if_index)) return (NULL); - ifp = ck_pr_load_ptr(&ifindex_table[idx]); + ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet); if (curvnet != NULL && ifp != NULL && ifp->if_vnet != curvnet) ifp = NULL; @@ -391,6 +394,24 @@ ifnet_byindex_ref(u_int idx) return (ifp); } +struct ifnet * +ifnet_byindexgen(uint16_t idx, uint16_t gen) +{ + struct ifnet *ifp; + + NET_EPOCH_ASSERT(); + + if (__predict_false(idx > if_index)) + return (NULL); + + ifp = ck_pr_load_ptr(&ifindex_table[idx].ife_ifnet); + + if (ifindex_table[idx].ife_gencnt == gen) + return (ifp); + else + return (NULL); +} + struct ifaddr * ifaddr_byindex(u_short idx) { @@ -571,13 +592,13 @@ if_alloc_domain(u_char type, int numa_domain) * next slot. */ for (idx = 1; idx <= if_index; idx++) { - if (ifindex_table[idx] == NULL) + if (ifindex_table[idx].ife_ifnet == NULL) break; } /* Catch if_index overflow. */ if (idx >= if_indexlim) { - struct ifnet **new, **old; + struct ifindex_entry *new, *old; int newlim; newlim = if_indexlim * 2; @@ -593,7 +614,8 @@ if_alloc_domain(u_char type, int numa_domain) if_index = idx; ifp->if_index = idx; - ck_pr_store_ptr(&ifindex_table[idx], ifp); + ifp->if_idxgen = ifindex_table[idx].ife_gencnt; + ck_pr_store_ptr(&ifindex_table[idx].ife_ifnet, ifp); IFNET_WUNLOCK(); return (ifp); @@ -668,9 +690,10 @@ if_free(struct ifnet *ifp) * virtualized and interface would outlive the vnet. */ IFNET_WLOCK(); - MPASS(ifindex_table[ifp->if_index] == ifp); - ck_pr_store_ptr(&ifindex_table[ifp->if_index], NULL); - while (if_index > 0 && ifindex_table[if_index] == NULL) + MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp); + ck_pr_store_ptr(&ifindex_table[ifp->if_index].ife_ifnet, NULL); + ifindex_table[ifp->if_index].ife_gencnt++; + while (if_index > 0 && ifindex_table[if_index].ife_ifnet == NULL) if_index--; IFNET_WUNLOCK(); @@ -819,7 +842,7 @@ if_attach_internal(struct ifnet *ifp, bool vmove) struct sockaddr_dl *sdl; struct ifaddr *ifa; - MPASS(ifindex_table[ifp->if_index] == ifp); + MPASS(ifindex_table[ifp->if_index].ife_ifnet == ifp); #ifdef VIMAGE ifp->if_vnet = curvnet; @@ -4508,8 +4531,8 @@ if_show_ifnet(struct ifnet *ifp) IF_DB_PRINTF("%d", if_dunit); IF_DB_PRINTF("%s", if_description); IF_DB_PRINTF("%u", if_index); + IF_DB_PRINTF("%d", if_idxgen); IF_DB_PRINTF("%u", if_refcount); - IF_DB_PRINTF("%d", if_index_reserved); IF_DB_PRINTF("%p", if_softc); IF_DB_PRINTF("%p", if_l2com); IF_DB_PRINTF("%p", if_llsoftc); @@ -4564,7 +4587,7 @@ DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets) u_short idx; for (idx = 1; idx <= if_index; idx++) { - ifp = ifindex_table[idx]; + ifp = ifindex_table[idx].ife_ifnet; if (ifp == NULL) continue; db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp); diff --git a/sys/net/if_var.h b/sys/net/if_var.h index dedc73718125..21b3687f62c1 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -334,7 +334,7 @@ struct ifnet { const char *if_dname; /* driver name */ int if_dunit; /* unit or IF_DUNIT_NONE */ u_short if_index; /* numeric abbreviation for this if */ - short if_index_reserved; /* spare space to grow if_index */ + u_short if_idxgen; /* ... and its generation count */ char if_xname[IFNAMSIZ]; /* external name (name + unit) */ char *if_description; /* interface description */ @@ -644,6 +644,13 @@ extern struct sx ifnet_sxlock; struct ifnet *ifnet_byindex(u_int); struct ifnet *ifnet_byindex_ref(u_int); +/* + * ifnet_byindexgen() looks up ifnet by index and generation count, + * attempting to restore a weak pointer that had been stored across + * the epoch. + */ +struct ifnet *ifnet_byindexgen(uint16_t idx, uint16_t gen); + /* * Given the index, ifaddr_byindex() returns the one and only * link-level ifaddr for the interface. You are not supposed to use diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h index 77364f428b12..ebe8ef205055 100644 --- a/sys/sys/mbuf.h +++ b/sys/sys/mbuf.h @@ -159,6 +159,10 @@ struct pkthdr { union { struct m_snd_tag *snd_tag; /* send tag, if any */ struct ifnet *rcvif; /* rcv interface */ + struct { + uint16_t rcvidx; /* rcv interface index ... */ + uint16_t rcvgen; /* ... and generation count */ + }; }; SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */ int32_t len; /* total packet length */ @@ -862,6 +866,8 @@ int m_snd_tag_alloc(struct ifnet *, void m_snd_tag_init(struct m_snd_tag *, struct ifnet *, const struct if_snd_tag_sw *); void m_snd_tag_destroy(struct m_snd_tag *); +void m_rcvif_serialize(struct mbuf *); +struct ifnet *m_rcvif_restore(struct mbuf *); static __inline int m_gettype(int size)