From nobody Mon Apr 3 08:22:33 2023 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 4PqkQy0LHwz43rbG; Mon, 3 Apr 2023 08:22:34 +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 4PqkQy05Vzz3Gx7; Mon, 3 Apr 2023 08:22:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680510154; 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=aeJD7/sTDPslQU2/p7QPaE0FhbDVC6hJvIxjl6BwUic=; b=k/c/po6ACj1ZW89jdPeY6VnaIO/cDQaJBM9mHilOjDXQjUmit2Tz+DrCZ/WmnHQURw9IY+ oVMtEy1kFI0miHtCxyg/5OUc92Yfl/JM8W63EErKjdtMhZxtI4kPV/rOBfXzC0TYlFq+sJ n8d281ePZywpxOUW51ho6SaBaeWBbiWxWiC6IxPPjs/8XF3BC7xi7fMGX8/R204rJ8fRVM cq3r16Nu1I1XqzTfrXCUVPdXu1JWMZ0cGDJDEWFf1JjVBtDKUkay8kHWH3DZFwobwklqor aoYNE8paV4p2I+mPnHlnJgpAsImJeyT9sR+zTzEZZ5+Gm9giY8hnT472cn8WeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680510154; 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=aeJD7/sTDPslQU2/p7QPaE0FhbDVC6hJvIxjl6BwUic=; b=eVAmYwYr3jfi0kSZwUXiO6sbNzn9J3kns4iAUQE8dM2Sz0xCCd/ccbDoTEXCZEPk5QWIkM PpbKDe8h1gbklrLukuf993fNWuqrToLCwGN3S8OvR6ipoMu/mqKYVDj7X/O/h/Wr7ITii9 qA+UvDoXRekMQ0UHvVcF9der+ygf8BrcoWRvXJkHGf6tW0LSz/TDHtCDCsMXJP7H89Fif2 tcO+Hu5/U5hiAswGOSLNUCnspflsZ8K8i88e1v90CAJdirVQ+FX7peZBZkclAG0Z5c6fOP TrEfa5DP4XgH5NxD9rsxIoUlH29aTQwfEWPq2rBU51hakZB74n9HL4HMlknoFg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1680510154; a=rsa-sha256; cv=none; b=mm8sGJ77RgiUY6CgWmsYsw+u0vYFNY0B2nDBgqFolWLiaVdIHBomSYeqQr9PmgzEUgn9zC 8Q8OLfnZzcL5IQnBYT6jSSU1/FJ21zup3qK04FNotjo3aaEK54JeY2gsVmPimQhGRmmsqO jW8KHIqzqRlFVF6ngPsclG73JyaOv8ms2HGtElCUJv/YLGQFweRvgaHd9CYfscCptgjsuJ xmOh8Lhg/hxSP6bciSlj1Ohiwruxp39au+6vdwFcj+JkuBhO12wEk8YFZ1ziyh3eOSDDqE BmCZ6u1etb/Jp18Sa0xRKOkQsv8Urb7V9wB/upK4ZqucDQKvCxFvPt0VjVOPWg== 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 4PqkQx5yqHzrmZ; Mon, 3 Apr 2023 08:22:33 +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 3338MXoB042362; Mon, 3 Apr 2023 08:22:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 3338MXmY042361; Mon, 3 Apr 2023 08:22:33 GMT (envelope-from git) Date: Mon, 3 Apr 2023 08:22:33 GMT Message-Id: <202304030822.3338MXmY042361@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dmitry Chagin Subject: git: 7ae0972c7b8c - main - linsysfs(4): Reimplement listnics() using ifAPI 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: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7ae0972c7b8cb46d23ff1e9e5f4a07a72f9bd0cb Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=7ae0972c7b8cb46d23ff1e9e5f4a07a72f9bd0cb commit 7ae0972c7b8cb46d23ff1e9e5f4a07a72f9bd0cb Author: Dmitry Chagin AuthorDate: 2023-04-03 08:22:16 +0000 Commit: Dmitry Chagin CommitDate: 2023-04-03 08:22:16 +0000 linsysfs(4): Reimplement listnics() using ifAPI Handle if arrival/departure events and VNETs. Differential Revision: https://reviews.freebsd.org/D38901 MFC after: 1 month XMFC with: ifAPI, pseudofs --- sys/compat/linsysfs/linsysfs.c | 186 +------------------ sys/compat/linsysfs/linsysfs.h | 38 ++++ sys/compat/linsysfs/linsysfs_net.c | 356 +++++++++++++++++++++++++++++++++++++ sys/modules/linsysfs/Makefile | 2 +- 4 files changed, 403 insertions(+), 179 deletions(-) diff --git a/sys/compat/linsysfs/linsysfs.c b/sys/compat/linsysfs/linsysfs.c index 356f1af7f1ac..23abd2c32743 100644 --- a/sys/compat/linsysfs/linsysfs.c +++ b/sys/compat/linsysfs/linsysfs.c @@ -37,22 +37,19 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include #include -#include -#include -#include - -#include -#include #include #include +#include + +MALLOC_DEFINE(M_LINSYSFS, "linsysfs", "Linsysfs structures"); + struct scsi_host_queue { TAILQ_ENTRY(scsi_host_queue) scsi_host_next; char *path; @@ -69,175 +66,6 @@ atoi(const char *str) return (int)strtol(str, (char **)NULL, 10); } -static int -linsysfs_ifnet_addr(PFS_FILL_ARGS) -{ - struct epoch_tracker et; - struct l_sockaddr lsa; - struct ifnet *ifp; - int error; - - CURVNET_SET(TD_TO_VNET(td)); - NET_EPOCH_ENTER(et); - ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); - if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) - error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", - lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2], - lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]); - else - error = ENOENT; - NET_EPOCH_EXIT(et); - CURVNET_RESTORE(); - return (error == -1 ? ERANGE : error); -} - -static int -linsysfs_ifnet_addrlen(PFS_FILL_ARGS) -{ - - sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN); - return (0); -} - -static int -linsysfs_ifnet_flags(PFS_FILL_ARGS) -{ - struct epoch_tracker et; - struct ifnet *ifp; - int error; - - CURVNET_SET(TD_TO_VNET(td)); - NET_EPOCH_ENTER(et); - ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); - if (ifp != NULL) - error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp)); - else - error = ENOENT; - NET_EPOCH_EXIT(et); - CURVNET_RESTORE(); - return (error == -1 ? ERANGE : error); -} - -static int -linsysfs_ifnet_ifindex(PFS_FILL_ARGS) -{ - struct epoch_tracker et; - struct ifnet *ifp; - int error; - - CURVNET_SET(TD_TO_VNET(td)); - NET_EPOCH_ENTER(et); - ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); - if (ifp != NULL) - error = sbuf_printf(sb, "%u\n", if_getindex(ifp)); - else - error = ENOENT; - NET_EPOCH_EXIT(et); - CURVNET_RESTORE(); - return (error == -1 ? ERANGE : error); -} - -static int -linsysfs_ifnet_mtu(PFS_FILL_ARGS) -{ - struct epoch_tracker et; - struct ifnet *ifp; - int error; - - CURVNET_SET(TD_TO_VNET(td)); - NET_EPOCH_ENTER(et); - ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); - if (ifp != NULL) - error = sbuf_printf(sb, "%u\n", if_getmtu(ifp)); - else - error = ENOENT; - NET_EPOCH_EXIT(et); - CURVNET_RESTORE(); - return (error == -1 ? ERANGE : error); -} - -static int -linsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS) -{ - - /* XXX */ - sbuf_printf(sb, "1000\n"); - return (0); -} - -static int -linsysfs_ifnet_type(PFS_FILL_ARGS) -{ - struct epoch_tracker et; - struct l_sockaddr lsa; - struct ifnet *ifp; - int error; - - CURVNET_SET(TD_TO_VNET(td)); - NET_EPOCH_ENTER(et); - ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); - if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) - error = sbuf_printf(sb, "%d\n", lsa.sa_family); - else - error = ENOENT; - NET_EPOCH_EXIT(et); - CURVNET_RESTORE(); - return (error == -1 ? ERANGE : error); -} - -static void -linsysfs_listnics(struct pfs_node *dir) -{ - struct pfs_node *nic; - struct pfs_node *lo; - - nic = pfs_create_dir(dir, "eth0", NULL, NULL, NULL, 0); - - pfs_create_file(nic, "address", &linsysfs_ifnet_addr, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "addr_len", &linsysfs_ifnet_addrlen, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "flags", &linsysfs_ifnet_flags, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "ifindex", &linsysfs_ifnet_ifindex, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "mtu", &linsysfs_ifnet_mtu, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(nic, "type", &linsysfs_ifnet_type, - NULL, NULL, NULL, PFS_RD); - - lo = pfs_create_dir(dir, "lo", NULL, NULL, NULL, 0); - - pfs_create_file(lo, "address", &linsysfs_ifnet_addr, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "addr_len", &linsysfs_ifnet_addrlen, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "flags", &linsysfs_ifnet_flags, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "ifindex", &linsysfs_ifnet_ifindex, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "mtu", &linsysfs_ifnet_mtu, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, - NULL, NULL, NULL, PFS_RD); - - pfs_create_file(lo, "type", &linsysfs_ifnet_type, - NULL, NULL, NULL, PFS_RD); -} - /* * Filler function for proc_name */ @@ -650,7 +478,6 @@ linsysfs_init(PFS_INIT_ARGS) struct pfs_node *drm; struct pfs_node *pci; struct pfs_node *scsi; - struct pfs_node *net; struct pfs_node *devdir, *chardev; struct pfs_node *kernel; devclass_t devclass; @@ -702,13 +529,14 @@ linsysfs_init(PFS_INIT_ARGS) NULL, NULL, NULL, PFS_RD); linsysfs_listcpus(cpu); - linsysfs_listnics(net); /* /sys/kernel */ kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0); /* /sys/kernel/debug, mountpoint for lindebugfs. */ pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0); + linsysfs_net_init(); + return (0); } @@ -727,6 +555,8 @@ linsysfs_uninit(PFS_INIT_ARGS) free(scsi_host, M_TEMP); } + linsysfs_net_uninit(); + return (0); } diff --git a/sys/compat/linsysfs/linsysfs.h b/sys/compat/linsysfs/linsysfs.h new file mode 100644 index 000000000000..005c0e090a6b --- /dev/null +++ b/sys/compat/linsysfs/linsysfs.h @@ -0,0 +1,38 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2023 Dmitry Chagin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _COMPAT_LINSYSFS_LINSYSFS_H_ +#define _COMPAT_LINSYSFS_LINSYSFS_H_ + +MALLOC_DECLARE(M_LINSYSFS); + +extern struct pfs_node *net; + +void linsysfs_net_init(void); +void linsysfs_net_uninit(void); + +#endif /* _COMPAT_LINSYSFS_LINSYSFS_H_ */ diff --git a/sys/compat/linsysfs/linsysfs_net.c b/sys/compat/linsysfs/linsysfs_net.c new file mode 100644 index 000000000000..bd695da2cbb0 --- /dev/null +++ b/sys/compat/linsysfs/linsysfs_net.c @@ -0,0 +1,356 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2023 Dmitry Chagin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +struct pfs_node *net; +static eventhandler_tag if_arrival_tag, if_departure_tag; + +static uint32_t net_latch_count = 0; +static struct mtx net_latch_mtx; +MTX_SYSINIT(net_latch_mtx, &net_latch_mtx, "lsfnet", MTX_DEF); + +struct ifp_nodes_queue { + TAILQ_ENTRY(ifp_nodes_queue) ifp_nodes_next; + if_t ifp; + struct vnet *vnet; + struct pfs_node *pn; +}; +TAILQ_HEAD(,ifp_nodes_queue) ifp_nodes_q; + +static void +linsysfs_net_latch_hold(void) +{ + + mtx_lock(&net_latch_mtx); + if (net_latch_count++ > 0) + mtx_sleep(&net_latch_count, &net_latch_mtx, PDROP, "lsfnet", 0); + else + mtx_unlock(&net_latch_mtx); +} + +static void +linsysfs_net_latch_rele(void) +{ + + mtx_lock(&net_latch_mtx); + if (--net_latch_count > 0) + wakeup_one(&net_latch_count); + mtx_unlock(&net_latch_mtx); +} + +static int +linsysfs_if_addr(PFS_FILL_ARGS) +{ + struct epoch_tracker et; + struct l_sockaddr lsa; + if_t ifp; + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) + error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2], + lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); +} + +static int +linsysfs_if_addrlen(PFS_FILL_ARGS) +{ + + sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN); + return (0); +} + +static int +linsysfs_if_flags(PFS_FILL_ARGS) +{ + struct epoch_tracker et; + if_t ifp; + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); +} + +static int +linsysfs_if_ifindex(PFS_FILL_ARGS) +{ + struct epoch_tracker et; + if_t ifp; + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "%u\n", if_getindex(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); +} + +static int +linsysfs_if_mtu(PFS_FILL_ARGS) +{ + struct epoch_tracker et; + if_t ifp; + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "%u\n", if_getmtu(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); +} + +static int +linsysfs_if_txq_len(PFS_FILL_ARGS) +{ + + /* XXX */ + sbuf_printf(sb, "1000\n"); + return (0); +} + +static int +linsysfs_if_type(PFS_FILL_ARGS) +{ + struct epoch_tracker et; + struct l_sockaddr lsa; + if_t ifp; + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) + error = sbuf_printf(sb, "%d\n", lsa.sa_family); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); +} + +static int +linsysfs_if_visible(PFS_VIS_ARGS) +{ + struct ifp_nodes_queue *nq, *nq_tmp; + struct epoch_tracker et; + if_t ifp; + int visible; + + visible = 0; + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_name); + if (ifp != NULL) { + TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) { + if (nq->ifp == ifp && nq->vnet == curvnet) { + visible = 1; + break; + } + } + } + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (visible); +} + +static int +linsysfs_net_addif(if_t ifp, void *arg) +{ + struct ifp_nodes_queue *nq, *nq_tmp; + struct pfs_node *nic, *dir = arg; + char ifname[LINUX_IFNAMSIZ]; + struct epoch_tracker et; + int ret __diagused; + + NET_EPOCH_ENTER(et); + ret = ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname)); + NET_EPOCH_EXIT(et); + KASSERT(ret > 0, ("Interface (%s) is not converted", if_name(ifp))); + + nic = pfs_find_node(dir, ifname); + if (nic == NULL) { + nic = pfs_create_dir(dir, ifname, NULL, linsysfs_if_visible, + NULL, 0); + pfs_create_file(nic, "address", &linsysfs_if_addr, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "addr_len", &linsysfs_if_addrlen, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "flags", &linsysfs_if_flags, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "ifindex", &linsysfs_if_ifindex, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "mtu", &linsysfs_if_mtu, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "tx_queue_len", &linsysfs_if_txq_len, + NULL, NULL, NULL, PFS_RD); + pfs_create_file(nic, "type", &linsysfs_if_type, + NULL, NULL, NULL, PFS_RD); + } + /* + * There is a small window between registering the if_arrival + * eventhandler and creating a list of interfaces. + */ + TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) { + if (nq->ifp == ifp && nq->vnet == curvnet) + return (0); + } + nq = malloc(sizeof(*nq), M_LINSYSFS, M_WAITOK); + nq->pn = nic; + nq->ifp = ifp; + nq->vnet = curvnet; + TAILQ_INSERT_TAIL(&ifp_nodes_q, nq, ifp_nodes_next); + return (0); +} + +static void +linsysfs_net_delif(if_t ifp) +{ + struct ifp_nodes_queue *nq, *nq_tmp; + struct pfs_node *pn; + + pn = NULL; + TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) { + if (nq->ifp == ifp && nq->vnet == curvnet) { + TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next); + pn = nq->pn; + free(nq, M_LINSYSFS); + break; + } + } + if (pn == NULL) + return; + TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) { + if (nq->pn == pn) + return; + } + pfs_destroy(pn); +} + +static void +linsysfs_if_arrival(void *arg __unused, if_t ifp) +{ + + linsysfs_net_latch_hold(); + (void)linsysfs_net_addif(ifp, net); + linsysfs_net_latch_rele(); +} + +static void +linsysfs_if_departure(void *arg __unused, if_t ifp) +{ + + linsysfs_net_latch_hold(); + linsysfs_net_delif(ifp); + linsysfs_net_latch_rele(); +} + +void +linsysfs_net_init(void) +{ + VNET_ITERATOR_DECL(vnet_iter); + + MPASS(net != NULL); + TAILQ_INIT(&ifp_nodes_q); + + if_arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event, + linsysfs_if_arrival, NULL, EVENTHANDLER_PRI_ANY); + if_departure_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, + linsysfs_if_departure, NULL, EVENTHANDLER_PRI_ANY); + + linsysfs_net_latch_hold(); + VNET_LIST_RLOCK(); + VNET_FOREACH(vnet_iter) { + CURVNET_SET(vnet_iter); + if_foreach_sleep(NULL, NULL, linsysfs_net_addif, net); + CURVNET_RESTORE(); + } + VNET_LIST_RUNLOCK(); + linsysfs_net_latch_rele(); +} + +void +linsysfs_net_uninit(void) +{ + struct ifp_nodes_queue *nq, *nq_tmp; + + EVENTHANDLER_DEREGISTER(ifnet_arrival_event, if_arrival_tag); + EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_departure_tag); + + linsysfs_net_latch_hold(); + TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) { + TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next); + free(nq, M_LINSYSFS); + } + linsysfs_net_latch_rele(); +} diff --git a/sys/modules/linsysfs/Makefile b/sys/modules/linsysfs/Makefile index 058fbbc521b1..8ec02da435e4 100644 --- a/sys/modules/linsysfs/Makefile +++ b/sys/modules/linsysfs/Makefile @@ -5,7 +5,7 @@ KMOD= linsysfs SRCS= vnode_if.h \ device_if.h bus_if.h pci_if.h \ - linsysfs.c + linsysfs.c linsysfs_net.c .if !defined(KERNBUILDDIR) .warning Building Linuxulator outside of a kernel does not make sense