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