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