xref: /freebsd-13-stable/sys/net/iflib_clone.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
3  * Copyright (C) 2017-2018 Joyent Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_acpi.h"
33 #include "opt_sched.h"
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/bus.h>
38 #include <sys/eventhandler.h>
39 #include <sys/event.h>
40 #include <sys/sockio.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/module.h>
45 #include <sys/kobj.h>
46 #include <sys/rman.h>
47 #include <sys/sbuf.h>
48 #include <sys/smp.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>
55 #include <sys/jail.h>
56 #include <sys/md5.h>
57 #include <sys/proc.h>
58 
59 #include <net/if.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>
64 #include <net/bpf.h>
65 #include <net/ethernet.h>
66 #include <net/vnet.h>
67 
68 #include <net/iflib.h>
69 #include <net/iflib_private.h>
70 #include "ifdi_if.h"
71 
72 int
noop_attach(device_t dev)73 noop_attach(device_t dev)
74 {
75 	return (0);
76 }
77 
78 int
iflib_pseudo_detach(device_t dev)79 iflib_pseudo_detach(device_t dev)
80 {
81 	if_ctx_t ctx;
82 
83 	ctx = device_get_softc(dev);
84 	if ((iflib_get_flags(ctx) & IFC_IN_DETACH) == 0)
85 		return (EBUSY);
86 	return (0);
87 }
88 
89 static device_t iflib_pseudodev;
90 
91 static struct mtx pseudoif_mtx;
92 MTX_SYSINIT(pseudoif_mtx, &pseudoif_mtx, "pseudoif_mtx", MTX_DEF);
93 
94 #define PSEUDO_LOCK() mtx_lock(&pseudoif_mtx);
95 #define PSEUDO_UNLOCK() mtx_unlock(&pseudoif_mtx);
96 
97 struct if_pseudo {
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;
102 	devclass_t ip_dc;
103 	LIST_ENTRY(if_pseudo) ip_list;
104 	int ip_on_list;
105 };
106 
107 static LIST_HEAD(, if_pseudo) iflib_pseudos = LIST_HEAD_INITIALIZER(iflib_pseudos);
108 
109 /*
110  * XXX this assumes that the rest of the
111  * code won't hang on to it after it's
112  * removed / unloaded
113  */
114 static if_pseudo_t
iflib_ip_lookup(const char * name)115 iflib_ip_lookup(const char *name)
116 {
117 	if_pseudo_t ip = NULL;
118 
119 	PSEUDO_LOCK();
120 	LIST_FOREACH(ip, &iflib_pseudos, ip_list) {
121 		if (!strcmp(ip->ip_sctx->isc_name, name))
122 			break;
123 	}
124 	PSEUDO_UNLOCK();
125 	return (ip);
126 }
127 
128 static void
iflib_ip_delete(if_pseudo_t ip)129 iflib_ip_delete(if_pseudo_t ip)
130 {
131 	PSEUDO_LOCK();
132 	if (ip->ip_on_list) {
133 		LIST_REMOVE(ip, ip_list);
134 		ip->ip_on_list = 0;
135 	}
136 	PSEUDO_UNLOCK();
137 }
138 
139 static void
iflib_ip_insert(if_pseudo_t ip)140 iflib_ip_insert(if_pseudo_t ip)
141 {
142 	PSEUDO_LOCK();
143 	if (!ip->ip_on_list) {
144 		LIST_INSERT_HEAD(&iflib_pseudos, ip, ip_list);
145 		ip->ip_on_list = 1;
146 	}
147 	PSEUDO_UNLOCK();
148 }
149 
150 static void
iflib_ifdetach(void * arg __unused,if_t ifp)151 iflib_ifdetach(void *arg __unused, if_t ifp)
152 {
153 
154 	/* If the ifnet is just being renamed, don't do anything. */
155 	if (ifp->if_flags & IFF_RENAMING)
156 		return;
157 }
158 
159 static void
iflib_iflladdr(void * arg __unused,if_t ifp __unused)160 iflib_iflladdr(void *arg __unused, if_t ifp __unused)
161 {
162 
163 }
164 
165 static int
iflib_clone_create(struct if_clone * ifc,int unit,caddr_t params)166 iflib_clone_create(struct if_clone *ifc, int unit, caddr_t params)
167 {
168 	const char *name = ifc_name(ifc);
169 	struct iflib_cloneattach_ctx clctx;
170 	if_ctx_t ctx;
171 	if_pseudo_t ip;
172 	device_t dev;
173 	int rc;
174 
175 	clctx.cc_ifc = ifc;
176 	clctx.cc_len = 0;
177 	clctx.cc_params = params;
178 	clctx.cc_name = name;
179 
180 	if (__predict_false(iflib_pseudodev == NULL)) {
181 		/* SYSINIT initialization would panic !?! */
182 		bus_topo_lock();
183 		iflib_pseudodev = device_add_child(root_bus, "ifpseudo", 0);
184 		bus_topo_unlock();
185 		MPASS(iflib_pseudodev != NULL);
186 	}
187 	ip = iflib_ip_lookup(name);
188 	if (ip == NULL) {
189 		printf("no ip found for %s\n", name);
190 		return (ENOENT);
191 	}
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);
195 		return (EBUSY);
196 	}
197 	PSEUDO_LOCK();
198 	dev = device_add_child(iflib_pseudodev, name, unit);
199 	device_set_driver(dev, &iflib_pseudodriver);
200 	PSEUDO_UNLOCK();
201 	device_quiet(dev);
202 	rc = device_attach(dev);
203 	MPASS(rc == 0);
204 	MPASS(dev != NULL);
205 	MPASS(devclass_get_device(ip->ip_dc, unit) == dev);
206 	rc = iflib_pseudo_register(dev, ip->ip_sctx, &ctx, &clctx);
207 	if (rc) {
208 		bus_topo_lock();
209 		device_delete_child(iflib_pseudodev, dev);
210 		bus_topo_unlock();
211 	} else
212 		device_set_softc(dev, ctx);
213 
214 	return (rc);
215 }
216 
217 static void
iflib_clone_destroy(if_t ifp)218 iflib_clone_destroy(if_t ifp)
219 {
220 	if_ctx_t ctx;
221 	device_t dev;
222 	struct sx *ctx_lock;
223 	int rc;
224 
225 	/*
226 	 * Detach device / free / free unit
227 	 */
228 	ctx = if_getsoftc(ifp);
229 	dev = iflib_get_dev(ctx);
230 	ctx_lock = iflib_ctx_lock_get(ctx);
231 	sx_xlock(ctx_lock);
232 	iflib_set_detach(ctx);
233 	iflib_stop(ctx);
234 	sx_xunlock(ctx_lock);
235 
236 	bus_topo_lock();
237 	rc = device_delete_child(iflib_pseudodev, dev);
238 	bus_topo_unlock();
239 	if (rc == 0)
240 		iflib_pseudo_deregister(ctx);
241 }
242 
243 if_pseudo_t
iflib_clone_register(if_shared_ctx_t sctx)244 iflib_clone_register(if_shared_ctx_t sctx)
245 {
246 	if_pseudo_t ip;
247 
248 	if (sctx->isc_name == NULL) {
249 		printf("iflib_clone_register failed - shared_ctx needs to have a device name\n");
250 		return (NULL);
251 	}
252 	if (iflib_ip_lookup(sctx->isc_name) != NULL) {
253 		printf("iflib_clone_register failed - shared_ctx %s alread registered\n",
254 			   sctx->isc_name);
255 		return (NULL);
256 	}
257 	ip = malloc(sizeof(*ip), M_IFLIB, M_WAITOK|M_ZERO);
258 	ip->ip_sctx = sctx;
259 	ip->ip_dc = devclass_create(sctx->isc_name);
260 	if (ip->ip_dc == NULL)
261 		goto fail_clone;
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);
266 		goto fail_clone;
267 	}
268 	ip->ip_lladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
269 											 iflib_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
270 	if (ip->ip_lladdr_tag == NULL)
271 		goto fail_addr;
272 	ip->ip_detach_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
273 											 iflib_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
274 
275 	if (ip->ip_detach_tag == NULL)
276 		goto fail_depart;
277 
278 	iflib_ip_insert(ip);
279 	return (ip);
280  fail_depart:
281 	EVENTHANDLER_DEREGISTER(iflladdr_event, ip->ip_lladdr_tag);
282  fail_addr:
283 	if_clone_detach(ip->ip_ifc);
284  fail_clone:
285 	free(ip, M_IFLIB);
286 	return (NULL);
287 }
288 
289 void
iflib_clone_deregister(if_pseudo_t ip)290 iflib_clone_deregister(if_pseudo_t ip)
291 {
292 	/* XXX check that is not still in use */
293 	iflib_ip_delete(ip);
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 */
298 	free(ip, M_IFLIB);
299 }
300