2 * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
3 * Copyright (C) 2017-2018 Joyent Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Neither the name of Matthew Macy nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_inet6.h"
35 #include "opt_sched.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
40 #include <sys/eventhandler.h>
41 #include <sys/event.h>
42 #include <sys/sockio.h>
43 #include <sys/kernel.h>
45 #include <sys/mutex.h>
46 #include <sys/module.h>
51 #include <sys/socket.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/taskqueue.h>
55 #include <sys/limits.h>
56 #include <sys/queue.h>
62 #include <net/if_var.h>
63 #include <net/if_types.h>
64 #include <net/if_media.h>
65 #include <net/if_clone.h>
67 #include <net/ethernet.h>
70 #include <net/iflib.h>
71 #include <net/iflib_private.h>
75 noop_attach(device_t dev)
81 iflib_pseudo_detach(device_t dev)
86 ctx = device_get_softc(dev);
87 ifc_flags = iflib_get_flags(ctx);
88 if ((ifc_flags & IFC_INIT_DONE) == 0)
90 return (IFDI_DETACH(ctx));
93 static device_t iflib_pseudodev;
95 static struct mtx pseudoif_mtx;
96 MTX_SYSINIT(pseudoif_mtx, &pseudoif_mtx, "pseudoif_mtx", MTX_DEF);
98 #define PSEUDO_LOCK() mtx_lock(&pseudoif_mtx);
99 #define PSEUDO_UNLOCK() mtx_unlock(&pseudoif_mtx);
102 eventhandler_tag ip_detach_tag;
103 eventhandler_tag ip_lladdr_tag;
104 struct if_clone *ip_ifc;
105 if_shared_ctx_t ip_sctx;
107 LIST_ENTRY(if_pseudo) ip_list;
111 static LIST_HEAD(, if_pseudo) iflib_pseudos = LIST_HEAD_INITIALIZER(iflib_pseudos);
114 * XXX this assumes that the rest of the
115 * code won't hang on to it after it's
119 iflib_ip_lookup(const char *name)
121 if_pseudo_t ip = NULL;
124 LIST_FOREACH(ip, &iflib_pseudos, ip_list) {
125 if (!strcmp(ip->ip_sctx->isc_name, name))
133 iflib_ip_delete(if_pseudo_t ip)
136 if (ip->ip_on_list) {
137 LIST_REMOVE(ip, ip_list);
144 iflib_ip_insert(if_pseudo_t ip)
147 if (!ip->ip_on_list) {
148 LIST_INSERT_HEAD(&iflib_pseudos, ip, ip_list);
155 iflib_ifdetach(void *arg __unused, if_t ifp)
158 /* If the ifnet is just being renamed, don't do anything. */
159 if (ifp->if_flags & IFF_RENAMING)
164 iflib_iflladdr(void *arg __unused, if_t ifp __unused)
170 iflib_clone_create(struct if_clone *ifc, int unit, caddr_t params)
172 const char *name = ifc_name(ifc);
173 struct iflib_cloneattach_ctx clctx;
181 clctx.cc_params = params;
182 clctx.cc_name = name;
184 if (__predict_false(iflib_pseudodev == NULL)) {
185 /* SYSINIT initialization would panic !?! */
187 iflib_pseudodev = device_add_child(root_bus, "ifpseudo", 0);
189 MPASS(iflib_pseudodev != NULL);
191 ip = iflib_ip_lookup(name);
193 printf("no ip found for %s\n", name);
196 if ((dev = devclass_get_device(ip->ip_dc, unit)) != NULL) {
197 printf("unit %d allocated\n", unit);
198 bus_generic_print_child(iflib_pseudodev, dev);
202 dev = device_add_child(iflib_pseudodev, name, unit);
203 device_set_driver(dev, &iflib_pseudodriver);
206 rc = device_attach(dev);
209 MPASS(devclass_get_device(ip->ip_dc, unit) == dev);
210 rc = iflib_pseudo_register(dev, ip->ip_sctx, &ctx, &clctx);
213 device_delete_child(iflib_pseudodev, dev);
216 device_set_softc(dev, ctx);
222 iflib_clone_destroy(if_t ifp)
230 * Detach device / free / free unit
232 ctx = if_getsoftc(ifp);
233 dev = iflib_get_dev(ctx);
234 ctx_lock = iflib_ctx_lock_get(ctx);
236 iflib_set_detach(ctx);
238 sx_xunlock(ctx_lock);
241 rc = device_delete_child(iflib_pseudodev, dev);
244 iflib_pseudo_deregister(ctx);
248 iflib_clone_register(if_shared_ctx_t sctx)
252 if (sctx->isc_name == NULL) {
253 printf("iflib_clone_register failed - shared_ctx needs to have a device name\n");
256 if (iflib_ip_lookup(sctx->isc_name) != NULL) {
257 printf("iflib_clone_register failed - shared_ctx %s alread registered\n",
261 ip = malloc(sizeof(*ip), M_IFLIB, M_WAITOK|M_ZERO);
263 ip->ip_dc = devclass_create(sctx->isc_name);
264 if (ip->ip_dc == NULL)
266 /* XXX --- we can handle clone_advanced later */
267 ip->ip_ifc = if_clone_simple(sctx->isc_name, iflib_clone_create, iflib_clone_destroy, 0);
268 if (ip->ip_ifc == NULL) {
269 printf("clone_simple failed -- cloned %s devices will not be available\n", sctx->isc_name);
272 ip->ip_lladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
273 iflib_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
274 if (ip->ip_lladdr_tag == NULL)
276 ip->ip_detach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
277 iflib_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
279 if (ip->ip_detach_tag == NULL)
285 EVENTHANDLER_DEREGISTER(iflladdr_event, ip->ip_lladdr_tag);
287 if_clone_detach(ip->ip_ifc);
294 iflib_clone_deregister(if_pseudo_t ip)
296 /* XXX check that is not still in use */
298 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ip->ip_detach_tag);
299 EVENTHANDLER_DEREGISTER(iflladdr_event, ip->ip_lladdr_tag);
300 if_clone_detach(ip->ip_ifc);
301 /* XXX free devclass */