Path: blob/main/sys/compat/linsysfs/linsysfs_net.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Dmitry Chagin <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/eventhandler.h>29#include <sys/kernel.h>30#include <sys/lock.h>31#include <sys/malloc.h>32#include <sys/mutex.h>33#include <sys/sbuf.h>34#include <sys/socket.h>3536#include <net/if.h>37#include <net/if_var.h>38#include <net/vnet.h>3940#include <compat/linux/linux.h>41#include <compat/linux/linux_common.h>42#include <fs/pseudofs/pseudofs.h>4344#include <compat/linsysfs/linsysfs.h>4546struct pfs_node *net;47static eventhandler_tag if_arrival_tag, if_departure_tag;4849static uint32_t net_latch_count = 0;50static struct mtx net_latch_mtx;51MTX_SYSINIT(net_latch_mtx, &net_latch_mtx, "lsfnet", MTX_DEF);5253struct ifp_nodes_queue {54TAILQ_ENTRY(ifp_nodes_queue) ifp_nodes_next;55if_t ifp;56struct vnet *vnet;57struct pfs_node *pn;58};59TAILQ_HEAD(,ifp_nodes_queue) ifp_nodes_q;6061static void62linsysfs_net_latch_hold(void)63{6465mtx_lock(&net_latch_mtx);66if (net_latch_count++ > 0)67mtx_sleep(&net_latch_count, &net_latch_mtx, PDROP, "lsfnet", 0);68else69mtx_unlock(&net_latch_mtx);70}7172static void73linsysfs_net_latch_rele(void)74{7576mtx_lock(&net_latch_mtx);77if (--net_latch_count > 0)78wakeup_one(&net_latch_count);79mtx_unlock(&net_latch_mtx);80}8182static int83linsysfs_if_addr(PFS_FILL_ARGS)84{85struct epoch_tracker et;86struct l_sockaddr lsa;87if_t ifp;88int error;8990CURVNET_SET(TD_TO_VNET(td));91NET_EPOCH_ENTER(et);92ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);93if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)94error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",95lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],96lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);97else98error = ENOENT;99NET_EPOCH_EXIT(et);100CURVNET_RESTORE();101return (error == -1 ? ERANGE : error);102}103104static int105linsysfs_if_addrlen(PFS_FILL_ARGS)106{107108sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);109return (0);110}111112static int113linsysfs_if_flags(PFS_FILL_ARGS)114{115struct epoch_tracker et;116if_t ifp;117int error;118119CURVNET_SET(TD_TO_VNET(td));120NET_EPOCH_ENTER(et);121ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);122if (ifp != NULL)123error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp));124else125error = ENOENT;126NET_EPOCH_EXIT(et);127CURVNET_RESTORE();128return (error == -1 ? ERANGE : error);129}130131static int132linsysfs_if_ifindex(PFS_FILL_ARGS)133{134struct epoch_tracker et;135if_t ifp;136int error;137138CURVNET_SET(TD_TO_VNET(td));139NET_EPOCH_ENTER(et);140ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);141if (ifp != NULL)142error = sbuf_printf(sb, "%u\n", if_getindex(ifp));143else144error = ENOENT;145NET_EPOCH_EXIT(et);146CURVNET_RESTORE();147return (error == -1 ? ERANGE : error);148}149150static int151linsysfs_if_mtu(PFS_FILL_ARGS)152{153struct epoch_tracker et;154if_t ifp;155int error;156157CURVNET_SET(TD_TO_VNET(td));158NET_EPOCH_ENTER(et);159ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);160if (ifp != NULL)161error = sbuf_printf(sb, "%u\n", if_getmtu(ifp));162else163error = ENOENT;164NET_EPOCH_EXIT(et);165CURVNET_RESTORE();166return (error == -1 ? ERANGE : error);167}168169static int170linsysfs_if_txq_len(PFS_FILL_ARGS)171{172173/* XXX */174sbuf_printf(sb, "1000\n");175return (0);176}177178static int179linsysfs_if_type(PFS_FILL_ARGS)180{181struct epoch_tracker et;182struct l_sockaddr lsa;183if_t ifp;184int error;185186CURVNET_SET(TD_TO_VNET(td));187NET_EPOCH_ENTER(et);188ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name);189if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0)190error = sbuf_printf(sb, "%d\n", lsa.sa_family);191else192error = ENOENT;193NET_EPOCH_EXIT(et);194CURVNET_RESTORE();195return (error == -1 ? ERANGE : error);196}197198static int199linsysfs_if_visible(PFS_VIS_ARGS)200{201struct ifp_nodes_queue *nq, *nq_tmp;202struct epoch_tracker et;203if_t ifp;204int visible;205206visible = 0;207CURVNET_SET(TD_TO_VNET(td));208NET_EPOCH_ENTER(et);209ifp = ifname_linux_to_ifp(td, pn->pn_name);210if (ifp != NULL) {211TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {212if (nq->ifp == ifp && nq->vnet == curvnet) {213visible = 1;214break;215}216}217}218NET_EPOCH_EXIT(et);219CURVNET_RESTORE();220return (visible);221}222223static int224linsysfs_net_addif(if_t ifp, void *arg)225{226struct ifp_nodes_queue *nq, *nq_tmp;227struct pfs_node *nic, *dir = arg;228char ifname[LINUX_IFNAMSIZ];229struct epoch_tracker et;230int ret __diagused;231232NET_EPOCH_ENTER(et);233ret = ifname_bsd_to_linux_ifp(ifp, ifname, sizeof(ifname));234NET_EPOCH_EXIT(et);235KASSERT(ret > 0, ("Interface (%s) is not converted", if_name(ifp)));236237nic = pfs_find_node(dir, ifname);238if (nic == NULL) {239pfs_create_dir(dir, &nic, ifname, NULL, linsysfs_if_visible,240NULL, 0);241pfs_create_file(nic, NULL, "address", &linsysfs_if_addr, NULL,242NULL, NULL, PFS_RD);243pfs_create_file(nic, NULL, "addr_len", &linsysfs_if_addrlen,244NULL, NULL, NULL, PFS_RD);245pfs_create_file(nic, NULL, "flags", &linsysfs_if_flags, NULL,246NULL, NULL, PFS_RD);247pfs_create_file(nic, NULL, "ifindex", &linsysfs_if_ifindex,248NULL, NULL, NULL, PFS_RD);249pfs_create_file(nic, NULL, "mtu", &linsysfs_if_mtu, NULL, NULL,250NULL, PFS_RD);251pfs_create_file(nic, NULL, "tx_queue_len", &linsysfs_if_txq_len,252NULL, NULL, NULL, PFS_RD);253pfs_create_file(nic, NULL, "type", &linsysfs_if_type, NULL,254NULL, NULL, PFS_RD);255}256/*257* There is a small window between registering the if_arrival258* eventhandler and creating a list of interfaces.259*/260TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {261if (nq->ifp == ifp && nq->vnet == curvnet)262return (0);263}264nq = malloc(sizeof(*nq), M_LINSYSFS, M_WAITOK);265nq->pn = nic;266nq->ifp = ifp;267nq->vnet = curvnet;268TAILQ_INSERT_TAIL(&ifp_nodes_q, nq, ifp_nodes_next);269return (0);270}271272static void273linsysfs_net_delif(if_t ifp)274{275struct ifp_nodes_queue *nq, *nq_tmp;276struct pfs_node *pn;277278pn = NULL;279TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {280if (nq->ifp == ifp && nq->vnet == curvnet) {281TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);282pn = nq->pn;283free(nq, M_LINSYSFS);284break;285}286}287if (pn == NULL)288return;289TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {290if (nq->pn == pn)291return;292}293pfs_destroy(pn);294}295296static void297linsysfs_if_arrival(void *arg __unused, if_t ifp)298{299300linsysfs_net_latch_hold();301(void)linsysfs_net_addif(ifp, net);302linsysfs_net_latch_rele();303}304305static void306linsysfs_if_departure(void *arg __unused, if_t ifp)307{308309linsysfs_net_latch_hold();310linsysfs_net_delif(ifp);311linsysfs_net_latch_rele();312}313314void315linsysfs_net_init(void)316{317VNET_ITERATOR_DECL(vnet_iter);318319MPASS(net != NULL);320TAILQ_INIT(&ifp_nodes_q);321322if_arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,323linsysfs_if_arrival, NULL, EVENTHANDLER_PRI_ANY);324if_departure_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,325linsysfs_if_departure, NULL, EVENTHANDLER_PRI_ANY);326327linsysfs_net_latch_hold();328VNET_LIST_RLOCK();329VNET_FOREACH(vnet_iter) {330CURVNET_SET(vnet_iter);331if_foreach_sleep(NULL, NULL, linsysfs_net_addif, net);332CURVNET_RESTORE();333}334VNET_LIST_RUNLOCK();335linsysfs_net_latch_rele();336}337338void339linsysfs_net_uninit(void)340{341struct ifp_nodes_queue *nq, *nq_tmp;342343EVENTHANDLER_DEREGISTER(ifnet_arrival_event, if_arrival_tag);344EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_departure_tag);345346linsysfs_net_latch_hold();347TAILQ_FOREACH_SAFE(nq, &ifp_nodes_q, ifp_nodes_next, nq_tmp) {348TAILQ_REMOVE(&ifp_nodes_q, nq, ifp_nodes_next);349free(nq, M_LINSYSFS);350}351linsysfs_net_latch_rele();352}353354355