xref: /freebsd/sys/net/if_edsc.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following edsclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following edsclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE EDSCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	From: @(#)if_loop.c	8.1 (Berkeley) 6/10/93
32  * $FreeBSD$
33  */
34 
35 /*
36  * Discard interface driver for protocol testing and timing.
37  * Mimics an Ethernet device so that VLANs can be attached to it etc.
38  */
39 
40 #include <sys/param.h>		/* types, important constants */
41 #include <sys/kernel.h>		/* SYSINIT for load-time initializations */
42 #include <sys/malloc.h>		/* malloc(9) */
43 #include <sys/module.h>		/* module(9) */
44 #include <sys/mbuf.h>		/* mbuf(9) */
45 #include <sys/socket.h>		/* struct ifreq */
46 #include <sys/sockio.h>		/* socket ioctl's */
47 /* #include <sys/systm.h> if you need printf(9) or other all-purpose globals */
48 
49 #include <net/bpf.h>		/* bpf(9) */
50 #include <net/ethernet.h>	/* Ethernet related constants and types */
51 #include <net/if.h>
52 #include <net/if_var.h>		/* basic part of ifnet(9) */
53 #include <net/if_clone.h>	/* network interface cloning */
54 #include <net/if_types.h>	/* IFT_ETHER and friends */
55 #include <net/if_var.h>		/* kernel-only part of ifnet(9) */
56 #include <net/vnet.h>
57 
58 static const char edscname[] = "edsc";
59 
60 /*
61  * Software configuration of an interface specific to this device type.
62  */
63 struct edsc_softc {
64 	struct ifnet	*sc_ifp; /* ptr to generic interface configuration */
65 
66 	/*
67 	 * A non-null driver can keep various things here, for instance,
68 	 * the hardware revision, cached values of write-only registers, etc.
69 	 */
70 };
71 
72 /*
73  * Attach to the interface cloning framework.
74  */
75 VNET_DEFINE_STATIC(struct if_clone *, edsc_cloner);
76 #define	V_edsc_cloner	VNET(edsc_cloner)
77 static int	edsc_clone_create(struct if_clone *, int, caddr_t);
78 static void	edsc_clone_destroy(struct ifnet *);
79 
80 /*
81  * Interface driver methods.
82  */
83 static void	edsc_init(void *dummy);
84 /* static void edsc_input(struct ifnet *ifp, struct mbuf *m); would be here */
85 static int	edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
86 static void	edsc_start(struct ifnet *ifp);
87 
88 /*
89  * We'll allocate softc instances from this.
90  */
91 static		MALLOC_DEFINE(M_EDSC, edscname, "Ethernet discard interface");
92 
93 /*
94  * Create an interface instance.
95  */
96 static int
97 edsc_clone_create(struct if_clone *ifc, int unit, caddr_t params)
98 {
99 	struct edsc_softc	*sc;
100 	struct ifnet		*ifp;
101 	struct ether_addr	eaddr;
102 
103 	/*
104 	 * Allocate soft and ifnet structures.  Link each to the other.
105 	 */
106 	sc = malloc(sizeof(struct edsc_softc), M_EDSC, M_WAITOK | M_ZERO);
107 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
108 	if (ifp == NULL) {
109 		free(sc, M_EDSC);
110 		return (ENOSPC);
111 	}
112 
113 	ifp->if_softc = sc;
114 
115 	/*
116 	 * Get a name for this particular interface in its ifnet structure.
117 	 */
118 	if_initname(ifp, edscname, unit);
119 
120 	/*
121 	 * Typical Ethernet interface flags: we can do broadcast and
122 	 * multicast but can't hear our own broadcasts or multicasts.
123 	 */
124 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
125 
126 	/*
127 	 * We can pretent we have the whole set of hardware features
128 	 * because we just discard all packets we get from the upper layer.
129 	 * However, the features are disabled initially.  They can be
130 	 * enabled via edsc_ioctl() when needed.
131 	 */
132 	ifp->if_capabilities =
133 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM |
134 	    IFCAP_HWCSUM | IFCAP_TSO |
135 	    IFCAP_JUMBO_MTU;
136 	ifp->if_capenable = 0;
137 
138 	/*
139 	 * Set the interface driver methods.
140 	 */
141 	ifp->if_init = edsc_init;
142 	/* ifp->if_input = edsc_input; */
143 	ifp->if_ioctl = edsc_ioctl;
144 	ifp->if_start = edsc_start;
145 
146 	/*
147 	 * Set the maximum output queue length from the global parameter.
148 	 */
149 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
150 
151 	/*
152 	 * Generate an arbitrary MAC address for the cloned interface.
153 	 */
154 	ether_gen_addr(ifp, &eaddr);
155 
156 	/*
157 	 * Do ifnet initializations common to all Ethernet drivers
158 	 * and attach to the network interface framework.
159 	 */
160 	ether_ifattach(ifp, eaddr.octet);
161 
162 	/*
163 	 * Now we can mark the interface as running, i.e., ready
164 	 * for operation.
165 	 */
166 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
167 
168 	return (0);
169 }
170 
171 /*
172  * Destroy an interface instance.
173  */
174 static void
175 edsc_clone_destroy(struct ifnet *ifp)
176 {
177 	struct edsc_softc	*sc = ifp->if_softc;
178 
179 	/*
180 	 * Detach from the network interface framework.
181 	 */
182 	ether_ifdetach(ifp);
183 
184 	/*
185 	 * Free memory occupied by ifnet and softc.
186 	 */
187 	if_free(ifp);
188 	free(sc, M_EDSC);
189 }
190 
191 /*
192  * This method is invoked from ether_ioctl() when it's time
193  * to bring up the hardware.
194  */
195 static void
196 edsc_init(void *dummy)
197 {
198 #if 0	/* what a hardware driver would do here... */
199 	struct edsc_soft	*sc = (struct edsc_softc *)dummy;
200 	struct ifnet		*ifp = sc->sc_ifp;
201 
202 	/* blah-blah-blah */
203 #endif
204 }
205 
206 /*
207  * Network interfaces are controlled via the ioctl(2) syscall.
208  */
209 static int
210 edsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
211 {
212 	struct ifreq		*ifr = (struct ifreq *)data;
213 
214 	switch (cmd) {
215 	case SIOCSIFCAP:
216 #if 1
217 		/*
218 		 * Just turn on any capabilities requested.
219 		 * The generic ifioctl() function has already made sure
220 		 * that they are supported, i.e., set in if_capabilities.
221 		 */
222 		ifp->if_capenable = ifr->ifr_reqcap;
223 #else
224 		/*
225 		 * A h/w driver would need to analyze the requested
226 		 * bits and program the hardware, e.g.:
227 		 */
228 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
229 
230 		if (mask & IFCAP_VLAN_HWTAGGING) {
231 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
232 
233 			if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
234 				/* blah-blah-blah */
235 			else
236 				/* etc-etc-etc */
237 		}
238 #endif
239 		break;
240 
241 	default:
242 		/*
243 		 * Offload the rest onto the common Ethernet handler.
244 		 */
245 		return (ether_ioctl(ifp, cmd, data));
246 	}
247 
248 	return (0);
249 }
250 
251 /*
252  * Process the output queue.
253  */
254 static void
255 edsc_start(struct ifnet *ifp)
256 {
257 	struct mbuf		*m;
258 
259 	/*
260 	 * A hardware interface driver can set IFF_DRV_OACTIVE
261 	 * in ifp->if_drv_flags:
262 	 *
263 	 * ifp->if_drv_flags |= IFF_DRV_OACTIVE;
264 	 *
265 	 * to prevent if_start from being invoked again while the
266 	 * transmission is under way.  The flag is to protect the
267 	 * device's transmitter, not the method itself.  The output
268 	 * queue is locked and several threads can process it in
269 	 * parallel safely, so the driver can use other means to
270 	 * serialize access to the transmitter.
271 	 *
272 	 * If using IFF_DRV_OACTIVE, the driver should clear the flag
273 	 * not earlier than the current transmission is complete, e.g.,
274 	 * upon an interrupt from the device, not just before returning
275 	 * from if_start.  This method merely starts the transmission,
276 	 * which may proceed asynchronously.
277 	 */
278 
279 	/*
280 	 * We loop getting packets from the queue until it's empty.
281 	 * A h/w driver would loop until the device can accept more
282 	 * data into its buffer, or while there are free transmit
283 	 * descriptors, or whatever.
284 	 */
285 	for (;;) {
286 		/*
287 		 * Try to dequeue one packet.  Stop if the queue is empty.
288 		 * Use IF_DEQUEUE() here if ALTQ(9) support is unneeded.
289 		 */
290 		IFQ_DEQUEUE(&ifp->if_snd, m);
291 		if (m == NULL)
292 			break;
293 
294 		/*
295 		 * Let bpf(9) at the packet.
296 		 */
297 		BPF_MTAP(ifp, m);
298 
299 		/*
300 		 * Update the interface counters.
301 		 */
302 		if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
303 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
304 
305 		/*
306 		 * Finally, just drop the packet.
307 		 * TODO: Reply to ARP requests unless IFF_NOARP is set.
308 		 */
309 		m_freem(m);
310 	}
311 
312 	/*
313 	 * ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
314 	 * would be here only if the transmission were synchronous.
315 	 */
316 }
317 
318 static void
319 vnet_edsc_init(const void *unused __unused)
320 {
321 
322 	/*
323 	 * Connect to the network interface cloning framework.
324 	 * The last argument is the number of units to be created
325 	 * from the outset.  It's also the minimum number of units
326 	 * allowed.  We don't want any units created as soon as the
327 	 * driver is loaded.
328 	 */
329 	V_edsc_cloner = if_clone_simple(edscname, edsc_clone_create,
330 	    edsc_clone_destroy, 0);
331 }
332 VNET_SYSINIT(vnet_edsc_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
333     vnet_edsc_init, NULL);
334 
335 static void
336 vnet_edsc_uninit(const void *unused __unused)
337 {
338 
339 	/*
340 	 * Disconnect from the cloning framework.
341 	 * Existing interfaces will be disposed of properly.
342 	 */
343 	if_clone_detach(V_edsc_cloner);
344 }
345 VNET_SYSUNINIT(vnet_edsc_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
346     vnet_edsc_uninit, NULL);
347 
348 /*
349  * This function provides handlers for module events, namely load and unload.
350  */
351 static int
352 edsc_modevent(module_t mod, int type, void *data)
353 {
354 
355 	switch (type) {
356 	case MOD_LOAD:
357 	case MOD_UNLOAD:
358 		break;
359 	default:
360 		/*
361 		 * There are other event types, but we don't handle them.
362 		 * See module(9).
363 		 */
364 		return (EOPNOTSUPP);
365 	}
366 	return (0);
367 }
368 
369 static moduledata_t edsc_mod = {
370 	"if_edsc",			/* name */
371 	edsc_modevent,			/* event handler */
372 	NULL				/* additional data */
373 };
374 
375 DECLARE_MODULE(if_edsc, edsc_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
376