2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following edsclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following edsclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE EDSCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * From: @(#)if_loop.c 8.1 (Berkeley) 6/10/93
34 * Discard interface driver for protocol testing and timing.
35 * Mimics an Ethernet device so that VLANs can be attached to it etc.
38 #include <sys/param.h> /* types, important constants */
39 #include <sys/kernel.h> /* SYSINIT for load-time initializations */
40 #include <sys/malloc.h> /* malloc(9) */
41 #include <sys/module.h> /* module(9) */
42 #include <sys/mbuf.h> /* mbuf(9) */
43 #include <sys/socket.h> /* struct ifreq */
44 #include <sys/sockio.h> /* socket ioctl's */
45 /* #include <sys/systm.h> if you need printf(9) or other all-purpose globals */
47 #include <net/bpf.h> /* bpf(9) */
48 #include <net/ethernet.h> /* Ethernet related constants and types */
49 #include <net/if.h> /* basic part of ifnet(9) */
50 #include <net/if_clone.h> /* network interface cloning */
51 #include <net/if_types.h> /* IFT_ETHER and friends */
52 #include <net/if_var.h> /* kernel-only part of ifnet(9) */
54 static const char edscname[] = "edsc";
57 * Software configuration of an interface specific to this device type.
60 struct ifnet *sc_ifp; /* ptr to generic interface configuration */
63 * A non-null driver can keep various things here, for instance,
64 * the hardware revision, cached values of write-only registers, etc.
69 * Attach to the interface cloning framework.
71 static struct if_clone *edsc_cloner;
72 static int edsc_clone_create(struct if_clone *, int, caddr_t);
73 static void edsc_clone_destroy(struct ifnet *);
76 * Interface driver methods.
78 static void edsc_init(void *dummy);
79 /* static void edsc_input(struct ifnet *ifp, struct mbuf *m); would be here */
80 static int edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
81 static void edsc_start(struct ifnet *ifp);
84 * We'll allocate softc instances from this.
86 static MALLOC_DEFINE(M_EDSC, edscname, "Ethernet discard interface");
89 * Create an interface instance.
92 edsc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
94 struct edsc_softc *sc;
96 static u_char eaddr[ETHER_ADDR_LEN]; /* 0:0:0:0:0:0 */
99 * Allocate soft and ifnet structures. Link each to the other.
101 sc = malloc(sizeof(struct edsc_softc), M_EDSC, M_WAITOK | M_ZERO);
102 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
111 * Get a name for this particular interface in its ifnet structure.
113 if_initname(ifp, edscname, unit);
116 * Typical Ethernet interface flags: we can do broadcast and
117 * multicast but can't hear our own broadcasts or multicasts.
119 ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
122 * We can pretent we have the whole set of hardware features
123 * because we just discard all packets we get from the upper layer.
124 * However, the features are disabled initially. They can be
125 * enabled via edsc_ioctl() when needed.
127 ifp->if_capabilities =
128 IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM |
129 IFCAP_HWCSUM | IFCAP_TSO |
131 ifp->if_capenable = 0;
134 * Set the interface driver methods.
136 ifp->if_init = edsc_init;
137 /* ifp->if_input = edsc_input; */
138 ifp->if_ioctl = edsc_ioctl;
139 ifp->if_start = edsc_start;
142 * Set the maximum output queue length from the global parameter.
144 ifp->if_snd.ifq_maxlen = ifqmaxlen;
147 * Do ifnet initializations common to all Ethernet drivers
148 * and attach to the network interface framework.
149 * TODO: Pick a non-zero link level address.
151 ether_ifattach(ifp, eaddr);
154 * Now we can mark the interface as running, i.e., ready
157 ifp->if_drv_flags |= IFF_DRV_RUNNING;
163 * Destroy an interface instance.
166 edsc_clone_destroy(struct ifnet *ifp)
168 struct edsc_softc *sc = ifp->if_softc;
171 * Detach from the network interface framework.
176 * Free memory occupied by ifnet and softc.
183 * This method is invoked from ether_ioctl() when it's time
184 * to bring up the hardware.
187 edsc_init(void *dummy)
189 #if 0 /* what a hardware driver would do here... */
190 struct edsc_soft *sc = (struct edsc_softc *)dummy;
191 struct ifnet *ifp = sc->sc_ifp;
198 * Network interfaces are controlled via the ioctl(2) syscall.
201 edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
203 struct ifreq *ifr = (struct ifreq *)data;
209 * Just turn on any capabilities requested.
210 * The generic ifioctl() function has already made sure
211 * that they are supported, i.e., set in if_capabilities.
213 ifp->if_capenable = ifr->ifr_reqcap;
216 * A h/w driver would need to analyze the requested
217 * bits and program the hardware, e.g.:
219 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
221 if (mask & IFCAP_VLAN_HWTAGGING) {
222 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
224 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
234 * Offload the rest onto the common Ethernet handler.
236 return (ether_ioctl(ifp, cmd, data));
243 * Process the output queue.
246 edsc_start(struct ifnet *ifp)
251 * A hardware interface driver can set IFF_DRV_OACTIVE
252 * in ifp->if_drv_flags:
254 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
256 * to prevent if_start from being invoked again while the
257 * transmission is under way. The flag is to protect the
258 * device's transmitter, not the method itself. The output
259 * queue is locked and several threads can process it in
260 * parallel safely, so the driver can use other means to
261 * serialize access to the transmitter.
263 * If using IFF_DRV_OACTIVE, the driver should clear the flag
264 * not earlier than the current transmission is complete, e.g.,
265 * upon an interrupt from the device, not just before returning
266 * from if_start. This method merely starts the transmission,
267 * which may proceed asynchronously.
271 * We loop getting packets from the queue until it's empty.
272 * A h/w driver would loop until the device can accept more
273 * data into its buffer, or while there are free transmit
274 * descriptors, or whatever.
278 * Try to dequeue one packet. Stop if the queue is empty.
279 * Use IF_DEQUEUE() here if ALTQ(9) support is unneeded.
281 IFQ_DEQUEUE(&ifp->if_snd, m);
286 * Let bpf(9) at the packet.
291 * Update the interface counters.
293 ifp->if_obytes += m->m_pkthdr.len;
297 * Finally, just drop the packet.
298 * TODO: Reply to ARP requests unless IFF_NOARP is set.
304 * ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
305 * would be here only if the transmission were synchronous.
310 * This function provides handlers for module events, namely load and unload.
313 edsc_modevent(module_t mod, int type, void *data)
319 * Connect to the network interface cloning framework.
320 * The last argument is the number of units to be created
321 * from the outset. It's also the minimum number of units
322 * allowed. We don't want any units created as soon as the
325 edsc_cloner = if_clone_simple(edscname, edsc_clone_create,
326 edsc_clone_destroy, 0);
331 * Disconnect from the cloning framework.
332 * Existing interfaces will be disposed of properly.
334 if_clone_detach(edsc_cloner);
339 * There are other event types, but we don't handle them.
347 static moduledata_t edsc_mod = {
348 "if_edsc", /* name */
349 edsc_modevent, /* event handler */
350 NULL /* additional data */
353 DECLARE_MODULE(if_edsc, edsc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);