if_edsc.c 9.8 KB

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