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>
31 #include "opt_inet6.h"
33 #include "opt_sched.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
38 #include <sys/eventhandler.h>
39 #include <sys/event.h>
40 #include <sys/sockio.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/taskqueue.h>
53 #include <sys/limits.h>
54 #include <sys/queue.h>
60 #include <net/if_var.h>
61 #include <net/if_types.h>
62 #include <net/if_media.h>
63 #include <net/if_clone.h>
65 #include <net/ethernet.h>
68 #include <net/iflib.h>
69 #include <net/iflib_private.h>
73 noop_attach(device_t dev)
79 iflib_pseudo_detach(device_t dev)
83 ctx = device_get_softc(dev);
84 if ((iflib_get_flags(ctx) & IFC_IN_DETACH) == 0)
89 static device_t iflib_pseudodev;
91 static struct mtx pseudoif_mtx;
92 MTX_SYSINIT(pseudoif_mtx, &pseudoif_mtx, "pseudoif_mtx", MTX_DEF);
94 #define PSEUDO_LOCK() mtx_lock(&pseudoif_mtx);
95 #define PSEUDO_UNLOCK() mtx_unlock(&pseudoif_mtx);
98 eventhandler_tag ip_detach_tag;
99 eventhandler_tag ip_lladdr_tag;
100 struct if_clone *ip_ifc;
101 if_shared_ctx_t ip_sctx;
103 LIST_ENTRY(if_pseudo) ip_list;
107 static LIST_HEAD(, if_pseudo) iflib_pseudos = LIST_HEAD_INITIALIZER(iflib_pseudos);
110 * XXX this assumes that the rest of the
111 * code won't hang on to it after it's
115 iflib_ip_lookup(const char *name)
117 if_pseudo_t ip = NULL;
120 LIST_FOREACH(ip, &iflib_pseudos, ip_list) {
121 if (!strcmp(ip->ip_sctx->isc_name, name))
129 iflib_ip_delete(if_pseudo_t ip)
132 if (ip->ip_on_list) {
133 LIST_REMOVE(ip, ip_list);
140 iflib_ip_insert(if_pseudo_t ip)
143 if (!ip->ip_on_list) {
144 LIST_INSERT_HEAD(&iflib_pseudos, ip, ip_list);
151 iflib_ifdetach(void *arg __unused, if_t ifp)
154 /* If the ifnet is just being renamed, don't do anything. */
155 if (ifp->if_flags & IFF_RENAMING)
160 iflib_iflladdr(void *arg __unused, if_t ifp __unused)
166 iflib_clone_create(struct if_clone *ifc, int unit, caddr_t params)
168 const char *name = ifc_name(ifc);
169 struct iflib_cloneattach_ctx clctx;
177 clctx.cc_params = params;
178 clctx.cc_name = name;
180 if (__predict_false(iflib_pseudodev == NULL)) {
181 /* SYSINIT initialization would panic !?! */
183 iflib_pseudodev = device_add_child(root_bus, "ifpseudo", 0);
185 MPASS(iflib_pseudodev != NULL);
187 ip = iflib_ip_lookup(name);
189 printf("no ip found for %s\n", name);
192 if ((dev = devclass_get_device(ip->ip_dc, unit)) != NULL) {
193 printf("unit %d allocated\n", unit);
194 bus_generic_print_child(iflib_pseudodev, dev);
198 dev = device_add_child(iflib_pseudodev, name, unit);
199 device_set_driver(dev, &iflib_pseudodriver);
202 rc = device_attach(dev);
205 MPASS(devclass_get_device(ip->ip_dc, unit) == dev);
206 rc = iflib_pseudo_register(dev, ip->ip_sctx, &ctx, &clctx);
209 device_delete_child(iflib_pseudodev, dev);
212 device_set_softc(dev, ctx);
218 iflib_clone_destroy(if_t ifp)
226 * Detach device / free / free unit
228 ctx = if_getsoftc(ifp);
229 dev = iflib_get_dev(ctx);
230 ctx_lock = iflib_ctx_lock_get(ctx);
232 iflib_set_detach(ctx);
234 sx_xunlock(ctx_lock);
237 rc = device_delete_child(iflib_pseudodev, dev);
240 iflib_pseudo_deregister(ctx);
244 iflib_clone_register(if_shared_ctx_t sctx)
248 if (sctx->isc_name == NULL) {
249 printf("iflib_clone_register failed - shared_ctx needs to have a device name\n");
252 if (iflib_ip_lookup(sctx->isc_name) != NULL) {
253 printf("iflib_clone_register failed - shared_ctx %s alread registered\n",
257 ip = malloc(sizeof(*ip), M_IFLIB, M_WAITOK|M_ZERO);
259 ip->ip_dc = devclass_create(sctx->isc_name);
260 if (ip->ip_dc == NULL)
262 /* XXX --- we can handle clone_advanced later */
263 ip->ip_ifc = if_clone_simple(sctx->isc_name, iflib_clone_create, iflib_clone_destroy, 0);
264 if (ip->ip_ifc == NULL) {
265 printf("clone_simple failed -- cloned %s devices will not be available\n", sctx->isc_name);
268 ip->ip_lladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
269 iflib_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
270 if (ip->ip_lladdr_tag == NULL)
272 ip->ip_detach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
273 iflib_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
275 if (ip->ip_detach_tag == NULL)
281 EVENTHANDLER_DEREGISTER(iflladdr_event, ip->ip_lladdr_tag);
283 if_clone_detach(ip->ip_ifc);
290 iflib_clone_deregister(if_pseudo_t ip)
292 /* XXX check that is not still in use */
294 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ip->ip_detach_tag);
295 EVENTHANDLER_DEREGISTER(iflladdr_event, ip->ip_lladdr_tag);
296 if_clone_detach(ip->ip_ifc);
297 /* XXX free devclass */