diff --git a/sys/compat/linsysfs/linsysfs.h b/sys/compat/linsysfs/linsysfs.h new file mode 100644 --- /dev/null +++ b/sys/compat/linsysfs/linsysfs.h @@ -0,0 +1,36 @@ +/*- + * 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_ + +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.c b/sys/compat/linsysfs/linsysfs.c --- a/sys/compat/linsysfs/linsysfs.c +++ b/sys/compat/linsysfs/linsysfs.c @@ -37,22 +37,17 @@ #include #include #include -#include #include #include #include #include -#include -#include -#include - -#include -#include #include #include +#include + struct scsi_host_queue { TAILQ_ENTRY(scsi_host_queue) scsi_host_next; char *path; @@ -69,175 +64,6 @@ 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 +476,6 @@ 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,7 +527,6 @@ NULL, NULL, NULL, PFS_RD); linsysfs_listcpus(cpu); - linsysfs_listnics(net); /* /sys/kernel */ kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0); @@ -712,6 +536,8 @@ /* /sys/subsystem/... */ dir = pfs_create_dir(root, "subsystem", NULL, NULL, NULL, 0); + linsysfs_net_init(); + return (0); } @@ -730,6 +556,8 @@ free(scsi_host, M_TEMP); } + linsysfs_net_uninit(); + return (0); } diff --git a/sys/compat/linsysfs/linsysfs_net.c b/sys/compat/linsysfs/linsysfs_net.c new file mode 100644 --- /dev/null +++ b/sys/compat/linsysfs/linsysfs_net.c @@ -0,0 +1,274 @@ +/*- + * 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 + +struct pfs_node *net; +static eventhandler_tag if_arrival_tag, if_departure_tag; + +static uint32_t net_path_count = 0; +static struct mtx net_path_mtx; +MTX_SYSINIT(net_path_mtx, &net_path_mtx, "lsfnet_mtx", MTX_DEF); + + +static void +linsysfs_net_path_hold(void) +{ + + mtx_lock(&net_path_mtx); + if (net_path_count++ > 0) + mtx_sleep(&net_path_count, &net_path_mtx, PDROP, "lsfnet", 0); + else + mtx_unlock(&net_path_mtx); +} + +static void +linsysfs_net_path_rele(void) +{ + + mtx_lock(&net_path_mtx); + if (--net_path_count > 0) + wakeup_one(&net_path_count); + mtx_unlock(&net_path_mtx); +} + +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 int +linsysfs_net_addnic(if_t ifp, void *arg) +{ + char ifname[LINUX_IFNAMSIZ]; + struct epoch_tracker et; + struct pfs_node *dir = arg; + struct pfs_node *nic = NULL; + 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_create_dir(dir, ifname, 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); + return (0); +} + +static void +linsysfs_net_listnics(struct pfs_node *dir) +{ + + CURVNET_SET(TD_TO_VNET(curthread)); + if_foreach_sleep(NULL, NULL, linsysfs_net_addnic, dir); + CURVNET_RESTORE(); +} + +static void +linsysfs_ifnet_arrival(void *arg __unused, struct ifnet *ifp) +{ + struct pfs_node *pn; + + linsysfs_net_path_hold(); + pn = pfs_find_node(net, if_name(ifp)); + if (pn == NULL) + linsysfs_net_addnic(ifp, net); + linsysfs_net_path_rele(); +} + +static void +linsysfs_ifnet_departure(void *arg __unused, struct ifnet *ifp) +{ + struct pfs_node *pn; + + linsysfs_net_path_hold(); + pn = pfs_find_node(net, if_name(ifp)); + if (pn != NULL) + pfs_destroy(pn); + linsysfs_net_path_rele(); +} + +void +linsysfs_net_init(void) +{ + + linsysfs_net_listnics(net); + + if_arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event, + linsysfs_ifnet_arrival, NULL, EVENTHANDLER_PRI_ANY); + if_departure_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, + linsysfs_ifnet_departure, NULL, EVENTHANDLER_PRI_ANY); +} + +void +linsysfs_net_uninit(void) +{ + + EVENTHANDLER_DEREGISTER(ifnet_arrival_event, if_arrival_tag); + EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_departure_tag); +} diff --git a/sys/modules/linsysfs/Makefile b/sys/modules/linsysfs/Makefile --- 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