/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1982, 1986, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following edsclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following edsclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE EDSCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* Discard interface driver for protocol testing and timing.33* Mimics an Ethernet device so that VLANs can be attached to it etc.34*/3536#include <sys/param.h> /* types, important constants */37#include <sys/kernel.h> /* SYSINIT for load-time initializations */38#include <sys/malloc.h> /* malloc(9) */39#include <sys/module.h> /* module(9) */40#include <sys/mbuf.h> /* mbuf(9) */41#include <sys/socket.h> /* struct ifreq */42#include <sys/sockio.h> /* socket ioctl's */43/* #include <sys/systm.h> if you need printf(9) or other all-purpose globals */4445#include <net/bpf.h> /* bpf(9) */46#include <net/ethernet.h> /* Ethernet related constants and types */47#include <net/if.h>48#include <net/if_var.h> /* basic part of ifnet(9) */49#include <net/if_private.h>50#include <net/if_clone.h> /* network interface cloning */51#include <net/if_types.h> /* IFT_ETHER and friends */52#include <net/vnet.h>5354static const char edscname[] = "edsc";5556/*57* Software configuration of an interface specific to this device type.58*/59struct edsc_softc {60struct ifnet *sc_ifp; /* ptr to generic interface configuration */6162/*63* A non-null driver can keep various things here, for instance,64* the hardware revision, cached values of write-only registers, etc.65*/66};6768/*69* Attach to the interface cloning framework.70*/71VNET_DEFINE_STATIC(struct if_clone *, edsc_cloner);72#define V_edsc_cloner VNET(edsc_cloner)73static int edsc_clone_create(struct if_clone *, int, caddr_t);74static void edsc_clone_destroy(struct ifnet *);7576/*77* Interface driver methods.78*/79static void edsc_init(void *dummy);80/* static void edsc_input(struct ifnet *ifp, struct mbuf *m); would be here */81static int edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);82static void edsc_start(struct ifnet *ifp);8384/*85* We'll allocate softc instances from this.86*/87static MALLOC_DEFINE(M_EDSC, edscname, "Ethernet discard interface");8889/*90* Create an interface instance.91*/92static int93edsc_clone_create(struct if_clone *ifc, int unit, caddr_t params)94{95struct edsc_softc *sc;96struct ifnet *ifp;97struct ether_addr eaddr;9899/*100* Allocate soft and ifnet structures. Link each to the other.101*/102sc = malloc(sizeof(struct edsc_softc), M_EDSC, M_WAITOK | M_ZERO);103ifp = sc->sc_ifp = if_alloc(IFT_ETHER);104ifp->if_softc = sc;105106/*107* Get a name for this particular interface in its ifnet structure.108*/109if_initname(ifp, edscname, unit);110111/*112* Typical Ethernet interface flags: we can do broadcast and113* multicast but can't hear our own broadcasts or multicasts.114*/115ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;116117/*118* We can pretent we have the whole set of hardware features119* because we just discard all packets we get from the upper layer.120* However, the features are disabled initially. They can be121* enabled via edsc_ioctl() when needed.122*/123ifp->if_capabilities =124IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM |125IFCAP_HWCSUM | IFCAP_TSO |126IFCAP_JUMBO_MTU;127ifp->if_capenable = 0;128129/*130* Set the interface driver methods.131*/132ifp->if_init = edsc_init;133/* ifp->if_input = edsc_input; */134ifp->if_ioctl = edsc_ioctl;135ifp->if_start = edsc_start;136137/*138* Set the maximum output queue length from the global parameter.139*/140ifp->if_snd.ifq_maxlen = ifqmaxlen;141142/*143* Generate an arbitrary MAC address for the cloned interface.144*/145ether_gen_addr(ifp, &eaddr);146147/*148* Do ifnet initializations common to all Ethernet drivers149* and attach to the network interface framework.150*/151ether_ifattach(ifp, eaddr.octet);152153/*154* Now we can mark the interface as running, i.e., ready155* for operation.156*/157ifp->if_drv_flags |= IFF_DRV_RUNNING;158159return (0);160}161162/*163* Destroy an interface instance.164*/165static void166edsc_clone_destroy(struct ifnet *ifp)167{168struct edsc_softc *sc = ifp->if_softc;169170/*171* Detach from the network interface framework.172*/173ether_ifdetach(ifp);174175/*176* Free memory occupied by ifnet and softc.177*/178if_free(ifp);179free(sc, M_EDSC);180}181182/*183* This method is invoked from ether_ioctl() when it's time184* to bring up the hardware.185*/186static void187edsc_init(void *dummy)188{189#if 0 /* what a hardware driver would do here... */190struct edsc_soft *sc = (struct edsc_softc *)dummy;191struct ifnet *ifp = sc->sc_ifp;192193/* blah-blah-blah */194#endif195}196197/*198* Network interfaces are controlled via the ioctl(2) syscall.199*/200static int201edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)202{203struct ifreq *ifr = (struct ifreq *)data;204205switch (cmd) {206case SIOCSIFCAP:207#if 1208/*209* Just turn on any capabilities requested.210* The generic ifioctl() function has already made sure211* that they are supported, i.e., set in if_capabilities.212*/213ifp->if_capenable = ifr->ifr_reqcap;214#else215/*216* A h/w driver would need to analyze the requested217* bits and program the hardware, e.g.:218*/219mask = ifp->if_capenable ^ ifr->ifr_reqcap;220221if (mask & IFCAP_VLAN_HWTAGGING) {222ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;223224if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)225/* blah-blah-blah */226else227/* etc-etc-etc */228}229#endif230break;231232default:233/*234* Offload the rest onto the common Ethernet handler.235*/236return (ether_ioctl(ifp, cmd, data));237}238239return (0);240}241242/*243* Process the output queue.244*/245static void246edsc_start(struct ifnet *ifp)247{248struct mbuf *m;249250/*251* A hardware interface driver can set IFF_DRV_OACTIVE252* in ifp->if_drv_flags:253*254* ifp->if_drv_flags |= IFF_DRV_OACTIVE;255*256* to prevent if_start from being invoked again while the257* transmission is under way. The flag is to protect the258* device's transmitter, not the method itself. The output259* queue is locked and several threads can process it in260* parallel safely, so the driver can use other means to261* serialize access to the transmitter.262*263* If using IFF_DRV_OACTIVE, the driver should clear the flag264* not earlier than the current transmission is complete, e.g.,265* upon an interrupt from the device, not just before returning266* from if_start. This method merely starts the transmission,267* which may proceed asynchronously.268*/269270/*271* We loop getting packets from the queue until it's empty.272* A h/w driver would loop until the device can accept more273* data into its buffer, or while there are free transmit274* descriptors, or whatever.275*/276for (;;) {277/*278* Try to dequeue one packet. Stop if the queue is empty.279* Use IF_DEQUEUE() here if ALTQ(9) support is unneeded.280*/281IFQ_DEQUEUE(&ifp->if_snd, m);282if (m == NULL)283break;284285/*286* Let bpf(9) at the packet.287*/288BPF_MTAP(ifp, m);289290/*291* Update the interface counters.292*/293if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);294if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);295296/*297* Finally, just drop the packet.298* TODO: Reply to ARP requests unless IFF_NOARP is set.299*/300m_freem(m);301}302303/*304* ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;305* would be here only if the transmission were synchronous.306*/307}308309static void310vnet_edsc_init(const void *unused __unused)311{312313/*314* Connect to the network interface cloning framework.315* The last argument is the number of units to be created316* from the outset. It's also the minimum number of units317* allowed. We don't want any units created as soon as the318* driver is loaded.319*/320V_edsc_cloner = if_clone_simple(edscname, edsc_clone_create,321edsc_clone_destroy, 0);322}323VNET_SYSINIT(vnet_edsc_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,324vnet_edsc_init, NULL);325326static void327vnet_edsc_uninit(const void *unused __unused)328{329330/*331* Disconnect from the cloning framework.332* Existing interfaces will be disposed of properly.333*/334if_clone_detach(V_edsc_cloner);335}336VNET_SYSUNINIT(vnet_edsc_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,337vnet_edsc_uninit, NULL);338339/*340* This function provides handlers for module events, namely load and unload.341*/342static int343edsc_modevent(module_t mod, int type, void *data)344{345346switch (type) {347case MOD_LOAD:348case MOD_UNLOAD:349break;350default:351/*352* There are other event types, but we don't handle them.353* See module(9).354*/355return (EOPNOTSUPP);356}357return (0);358}359360static moduledata_t edsc_mod = {361"if_edsc", /* name */362edsc_modevent, /* event handler */363NULL /* additional data */364};365366DECLARE_MODULE(if_edsc, edsc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);367368369