xref: /xnu-12377.41.6/bsd/skywalk/nexus/kpipe/nx_kernel_pipe.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2015-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 2014 Giuseppe Lettieri. All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  *   1. Redistributions of source code must retain the above copyright
36  *      notice, this list of conditions and the following disclaimer.
37  *   2. Redistributions in binary form must reproduce the above copyright
38  *      notice, this list of conditions and the following disclaimer in the
39  *      documentation and/or other materials provided with the distribution.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  */
53 
54 #include <skywalk/os_skywalk_private.h>
55 #include <skywalk/nexus/upipe/nx_user_pipe.h>
56 #include <skywalk/nexus/kpipe/nx_kernel_pipe.h>
57 
58 /* XXX arbitrary */
59 #define NX_KPIPE_RINGSIZE       128 /* default ring size */
60 #define NX_KPIPE_MINSLOTS       2
61 #define NX_KPIPE_MAXSLOTS       4096
62 #define NX_KPIPE_MAXRINGS       NX_MAX_NUM_RING_PAIR
63 #define NX_KPIPE_BUFSIZE        (2 * 1024)
64 #define NX_KPIPE_MINBUFSIZE     64
65 #define NX_KPIPE_MAXBUFSIZE     (16 * 1024)
66 
67 static int nx_kpipe_na_txsync(struct __kern_channel_ring *, struct proc *,
68     uint32_t);
69 static int nx_kpipe_na_rxsync(struct __kern_channel_ring *, struct proc *,
70     uint32_t);
71 static int nx_kpipe_na_activate(struct nexus_adapter *, na_activate_mode_t);
72 static void nx_kpipe_na_dtor(struct nexus_adapter *);
73 static int nx_kpipe_na_krings_create(struct nexus_adapter *,
74     struct kern_channel *);
75 static void nx_kpipe_na_krings_delete(struct nexus_adapter *,
76     struct kern_channel *, boolean_t);
77 
78 static void nx_kpipe_dom_init(struct nxdom *);
79 static void nx_kpipe_dom_terminate(struct nxdom *);
80 static void nx_kpipe_dom_fini(struct nxdom *);
81 static int nx_kpipe_dom_bind_port(struct kern_nexus *, nexus_port_t *,
82     struct nxbind *, void *);
83 static int nx_kpipe_dom_unbind_port(struct kern_nexus *, nexus_port_t);
84 static int nx_kpipe_dom_connect(struct kern_nexus_domain_provider *,
85     struct kern_nexus *, struct kern_channel *, struct chreq *, struct nxbind *,
86     struct proc *);
87 static void nx_kpipe_dom_disconnect(struct kern_nexus_domain_provider *,
88     struct kern_nexus *, struct kern_channel *);
89 static void nx_kpipe_dom_defunct(struct kern_nexus_domain_provider *,
90     struct kern_nexus *, struct kern_channel *, struct proc *);
91 static void nx_kpipe_dom_defunct_finalize(struct kern_nexus_domain_provider *,
92     struct kern_nexus *, struct kern_channel *, boolean_t);
93 
94 static int nx_kpipe_prov_init(struct kern_nexus_domain_provider *);
95 static int nx_kpipe_prov_params_adjust(
96 	const struct kern_nexus_domain_provider *,
97 	const struct nxprov_params *, struct nxprov_adjusted_params *);
98 static int nx_kpipe_prov_params(struct kern_nexus_domain_provider *,
99     const uint32_t, const struct nxprov_params *, struct nxprov_params *,
100     struct skmem_region_params[SKMEM_REGIONS], uint32_t);
101 static int nx_kpipe_prov_mem_new(struct kern_nexus_domain_provider *,
102     struct kern_nexus *, struct nexus_adapter *);
103 static void nx_kpipe_prov_fini(struct kern_nexus_domain_provider *);
104 static int nx_kpipe_prov_nx_ctor(struct kern_nexus *);
105 static void nx_kpipe_prov_nx_dtor(struct kern_nexus *);
106 static int nx_kpipe_prov_nx_mem_info(struct kern_nexus *,
107     struct kern_pbufpool **, struct kern_pbufpool **);
108 
109 static struct nexus_kpipe_adapter *na_kpipe_alloc(zalloc_flags_t);
110 static void na_kpipe_free(struct nexus_adapter *);
111 
112 struct nxdom nx_kpipe_dom_s = {
113 	.nxdom_prov_head =
114     STAILQ_HEAD_INITIALIZER(nx_kpipe_dom_s.nxdom_prov_head),
115 	.nxdom_type =           NEXUS_TYPE_KERNEL_PIPE,
116 	.nxdom_md_type =        NEXUS_META_TYPE_PACKET,
117 	.nxdom_md_subtype =     NEXUS_META_SUBTYPE_RAW,
118 	.nxdom_name =           "kpipe",
119 	.nxdom_ports =          {
120 		.nb_def = 1,
121 		.nb_min = 1,
122 		.nb_max = 1,
123 	},
124 	.nxdom_tx_rings = {
125 		.nb_def = 1,
126 		.nb_min = 1,
127 		.nb_max = NX_KPIPE_MAXRINGS,
128 	},
129 	.nxdom_rx_rings = {
130 		.nb_def = 1,
131 		.nb_min = 1,
132 		.nb_max = NX_KPIPE_MAXRINGS,
133 	},
134 	.nxdom_tx_slots = {
135 		.nb_def = NX_KPIPE_RINGSIZE,
136 		.nb_min = NX_KPIPE_MINSLOTS,
137 		.nb_max = NX_KPIPE_MAXSLOTS,
138 	},
139 	.nxdom_rx_slots = {
140 		.nb_def = NX_KPIPE_RINGSIZE,
141 		.nb_min = NX_KPIPE_MINSLOTS,
142 		.nb_max = NX_KPIPE_MAXSLOTS,
143 	},
144 	.nxdom_buf_size = {
145 		.nb_def = NX_KPIPE_BUFSIZE,
146 		.nb_min = NX_KPIPE_MINBUFSIZE,
147 		.nb_max = NX_KPIPE_MAXBUFSIZE,
148 	},
149 	.nxdom_large_buf_size = {
150 		.nb_def = 0,
151 		.nb_min = 0,
152 		.nb_max = 0,
153 	},
154 	.nxdom_meta_size = {
155 		.nb_def = NX_METADATA_OBJ_MIN_SZ,
156 		.nb_min = NX_METADATA_OBJ_MIN_SZ,
157 		.nb_max = NX_METADATA_USR_MAX_SZ,
158 	},
159 	.nxdom_stats_size = {
160 		.nb_def = 0,
161 		.nb_min = 0,
162 		.nb_max = NX_STATS_MAX_SZ,
163 	},
164 	.nxdom_pipes = {
165 		.nb_def = 0,
166 		.nb_min = 0,
167 		.nb_max = NX_UPIPE_MAXPIPES,
168 	},
169 	.nxdom_flowadv_max = {
170 		.nb_def = 0,
171 		.nb_min = 0,
172 		.nb_max = NX_FLOWADV_MAX,
173 	},
174 	.nxdom_nexusadv_size = {
175 		.nb_def = 0,
176 		.nb_min = 0,
177 		.nb_max = NX_NEXUSADV_MAX_SZ,
178 	},
179 	.nxdom_capabilities = {
180 		.nb_def = NXPCAP_USER_CHANNEL,
181 		.nb_min = NXPCAP_USER_CHANNEL,
182 		.nb_max = NXPCAP_USER_CHANNEL,
183 	},
184 	.nxdom_qmap = {
185 		.nb_def = NEXUS_QMAP_TYPE_INVALID,
186 		.nb_min = NEXUS_QMAP_TYPE_INVALID,
187 		.nb_max = NEXUS_QMAP_TYPE_INVALID,
188 	},
189 	.nxdom_max_frags = {
190 		.nb_def = NX_PBUF_FRAGS_DEFAULT,
191 		.nb_min = NX_PBUF_FRAGS_MIN,
192 		.nb_max = NX_PBUF_FRAGS_DEFAULT,
193 	},
194 	.nxdom_init =           nx_kpipe_dom_init,
195 	.nxdom_terminate =      nx_kpipe_dom_terminate,
196 	.nxdom_fini =           nx_kpipe_dom_fini,
197 	.nxdom_find_port =      NULL,
198 	.nxdom_port_is_reserved = NULL,
199 	.nxdom_bind_port =      nx_kpipe_dom_bind_port,
200 	.nxdom_unbind_port =    nx_kpipe_dom_unbind_port,
201 	.nxdom_connect =        nx_kpipe_dom_connect,
202 	.nxdom_disconnect =     nx_kpipe_dom_disconnect,
203 	.nxdom_defunct =        nx_kpipe_dom_defunct,
204 	.nxdom_defunct_finalize = nx_kpipe_dom_defunct_finalize,
205 };
206 
207 static struct kern_nexus_domain_provider nx_kpipe_prov_s = {
208 	.nxdom_prov_name =              NEXUS_PROVIDER_KERNEL_PIPE,
209 	.nxdom_prov_flags =             NXDOMPROVF_DEFAULT,
210 	.nxdom_prov_cb = {
211 		.dp_cb_init =           nx_kpipe_prov_init,
212 		.dp_cb_fini =           nx_kpipe_prov_fini,
213 		.dp_cb_params =         nx_kpipe_prov_params,
214 		.dp_cb_mem_new =        nx_kpipe_prov_mem_new,
215 		.dp_cb_config =         NULL,
216 		.dp_cb_nx_ctor =        nx_kpipe_prov_nx_ctor,
217 		.dp_cb_nx_dtor =        nx_kpipe_prov_nx_dtor,
218 		.dp_cb_nx_mem_info =    nx_kpipe_prov_nx_mem_info,
219 		.dp_cb_nx_mib_get =     NULL,
220 		.dp_cb_nx_stop =        NULL,
221 	},
222 };
223 
224 static SKMEM_TYPE_DEFINE(na_kpipe_zone, struct nexus_kpipe_adapter);
225 
226 static void
nx_kpipe_dom_init(struct nxdom * nxdom)227 nx_kpipe_dom_init(struct nxdom *nxdom)
228 {
229 	SK_LOCK_ASSERT_HELD();
230 	ASSERT(!(nxdom->nxdom_flags & NEXUSDOMF_INITIALIZED));
231 
232 	(void) nxdom_prov_add(nxdom, &nx_kpipe_prov_s);
233 }
234 
235 static void
nx_kpipe_dom_terminate(struct nxdom * nxdom)236 nx_kpipe_dom_terminate(struct nxdom *nxdom)
237 {
238 	struct kern_nexus_domain_provider *nxdom_prov, *tnxdp;
239 
240 	SK_LOCK_ASSERT_HELD();
241 
242 	STAILQ_FOREACH_SAFE(nxdom_prov, &nxdom->nxdom_prov_head,
243 	    nxdom_prov_link, tnxdp) {
244 		(void) nxdom_prov_del(nxdom_prov);
245 	}
246 }
247 
248 static void
nx_kpipe_dom_fini(struct nxdom * nxdom)249 nx_kpipe_dom_fini(struct nxdom *nxdom)
250 {
251 #pragma unused(nxdom)
252 }
253 
254 static int
nx_kpipe_dom_bind_port(struct kern_nexus * nx,nexus_port_t * nx_port,struct nxbind * nxb0,void * info)255 nx_kpipe_dom_bind_port(struct kern_nexus *nx, nexus_port_t *nx_port,
256     struct nxbind *nxb0, void *info)
257 {
258 #pragma unused(info)
259 	struct nxbind *nxb = NULL;
260 	int error = 0;
261 
262 	ASSERT(nx_port != NULL);
263 	ASSERT(nxb0 != NULL);
264 
265 	switch (*nx_port) {
266 	case NEXUS_PORT_KERNEL_PIPE_CLIENT:
267 		if (nx->nx_arg != NULL) {
268 			error = EEXIST;
269 			break;
270 		}
271 
272 		nxb = nxb_alloc(Z_WAITOK);
273 		nxb_move(nxb0, nxb);
274 		nx->nx_arg = nxb;
275 
276 		ASSERT(error == 0);
277 		break;
278 
279 	default:
280 		error = EDOM;
281 		break;
282 	}
283 
284 	return error;
285 }
286 
287 static int
nx_kpipe_dom_unbind_port(struct kern_nexus * nx,nexus_port_t nx_port)288 nx_kpipe_dom_unbind_port(struct kern_nexus *nx, nexus_port_t nx_port)
289 {
290 	struct nxbind *__single nxb = NULL;
291 	int error = 0;
292 
293 	ASSERT(nx_port != NEXUS_PORT_ANY);
294 
295 	switch (nx_port) {
296 	case NEXUS_PORT_KERNEL_PIPE_CLIENT:
297 		if ((nxb = nx->nx_arg) == NULL) {
298 			error = ENOENT;
299 			break;
300 		}
301 		nx->nx_arg = NULL;
302 		nxb_free(nxb);
303 		ASSERT(error == 0);
304 		break;
305 
306 	default:
307 		error = EDOM;
308 		break;
309 	}
310 
311 	return error;
312 }
313 
314 static int
nx_kpipe_dom_connect(struct kern_nexus_domain_provider * nxdom_prov,struct kern_nexus * nx,struct kern_channel * ch,struct chreq * chr,struct nxbind * nxb,struct proc * p)315 nx_kpipe_dom_connect(struct kern_nexus_domain_provider *nxdom_prov,
316     struct kern_nexus *nx, struct kern_channel *ch, struct chreq *chr,
317     struct nxbind *nxb, struct proc *p)
318 {
319 #pragma unused(nxdom_prov)
320 	nexus_port_t port = chr->cr_port;
321 	int err = 0;
322 
323 	SK_DF(SK_VERB_KERNEL_PIPE, "port %d mode 0x%x", port, chr->cr_mode);
324 
325 	SK_LOCK_ASSERT_HELD();
326 
327 	ASSERT(nx->nx_prov->nxprov_params->nxp_type ==
328 	    nxdom_prov->nxdom_prov_dom->nxdom_type &&
329 	    nx->nx_prov->nxprov_params->nxp_type == NEXUS_TYPE_KERNEL_PIPE);
330 
331 	if (port != NEXUS_PORT_KERNEL_PIPE_CLIENT) {
332 		err = EINVAL;
333 		goto done;
334 	}
335 
336 	/*
337 	 * XXX: user packet pool is not supported for kernel pipe for now.
338 	 */
339 	if (chr->cr_mode & CHMODE_USER_PACKET_POOL) {
340 		SK_ERR("User Packet pool mode not supported for kpipe");
341 		err = ENOTSUP;
342 		goto done;
343 	}
344 
345 	if (chr->cr_mode & CHMODE_EVENT_RING) {
346 		SK_ERR("event ring is not supported for kpipe");
347 		err = ENOTSUP;
348 		goto done;
349 	}
350 
351 	if (chr->cr_mode & CHMODE_LOW_LATENCY) {
352 		SK_ERR("low latency is not supported for kpipe");
353 		err = ENOTSUP;
354 		goto done;
355 	}
356 
357 	chr->cr_ring_set = RING_SET_DEFAULT;
358 	chr->cr_endpoint = CH_ENDPOINT_KERNEL_PIPE;
359 	(void) snprintf(chr->cr_name, sizeof(chr->cr_name), "kpipe:%llu:%.*s",
360 	    nx->nx_id, (int)nx->nx_prov->nxprov_params->nxp_namelen,
361 	    nx->nx_prov->nxprov_params->nxp_name);
362 
363 	err = na_connect(nx, ch, chr, nxb, p);
364 	if (err == 0) {
365 		/*
366 		 * Mark the kernel slot descriptor region as busy; this
367 		 * prevents it from being torn-down at channel defunct
368 		 * time, as the (external) nexus owner may be calling
369 		 * KPIs that require accessing the slots.
370 		 */
371 		skmem_arena_nexus_sd_set_noidle(
372 			skmem_arena_nexus(ch->ch_na->na_arena), 1);
373 	}
374 
375 done:
376 	return err;
377 }
378 
379 static void
nx_kpipe_dom_disconnect(struct kern_nexus_domain_provider * nxdom_prov,struct kern_nexus * nx,struct kern_channel * ch)380 nx_kpipe_dom_disconnect(struct kern_nexus_domain_provider *nxdom_prov,
381     struct kern_nexus *nx, struct kern_channel *ch)
382 {
383 #pragma unused(nxdom_prov, nx)
384 	SK_LOCK_ASSERT_HELD();
385 
386 	SK_D("channel %p -!- nexus %p (%s:\"%s\":%u:%d)", SK_KVA(ch),
387 	    SK_KVA(nx), nxdom_prov->nxdom_prov_name, ch->ch_na->na_name,
388 	    ch->ch_info->cinfo_nx_port, (int)ch->ch_info->cinfo_ch_ring_id);
389 
390 	/*
391 	 * Release busy assertion held earlier in nx_kpipe_dom_connect();
392 	 * this allows for the final arena teardown to succeed.
393 	 */
394 	skmem_arena_nexus_sd_set_noidle(
395 		skmem_arena_nexus(ch->ch_na->na_arena), -1);
396 
397 	na_disconnect(nx, ch);
398 }
399 
400 static void
nx_kpipe_dom_defunct(struct kern_nexus_domain_provider * nxdom_prov,struct kern_nexus * nx,struct kern_channel * ch,struct proc * p)401 nx_kpipe_dom_defunct(struct kern_nexus_domain_provider *nxdom_prov,
402     struct kern_nexus *nx, struct kern_channel *ch, struct proc *p)
403 {
404 #pragma unused(nxdom_prov, nx)
405 	struct nexus_adapter *na = ch->ch_na;
406 	ring_id_t qfirst;
407 	ring_id_t qlast;
408 	enum txrx t;
409 	uint32_t i;
410 
411 	LCK_MTX_ASSERT(&ch->ch_lock, LCK_MTX_ASSERT_OWNED);
412 	ASSERT(!(ch->ch_flags & CHANF_KERNEL));
413 	ASSERT(ch->ch_na->na_type == NA_KERNEL_PIPE);
414 
415 	/*
416 	 * Interface drivers like utun & IPsec access the kpipe rings
417 	 * outside of a kpipe channel sync context. They hold rights
418 	 * to the ring through kr_enter().
419 	 */
420 	for_rx_tx(t) {
421 		qfirst = ch->ch_first[t];
422 		qlast = ch->ch_last[t];
423 
424 		for (i = qfirst; i < qlast; i++) {
425 			(void) kr_enter(&NAKR(na, t)[i], TRUE);
426 		}
427 	}
428 
429 	na_ch_rings_defunct(ch, p);
430 
431 	for_rx_tx(t) {
432 		qfirst = ch->ch_first[t];
433 		qlast = ch->ch_last[t];
434 
435 		for (i = qfirst; i < qlast; i++) {
436 			kr_exit(&NAKR(na, t)[i]);
437 		}
438 	}
439 }
440 
441 static void
nx_kpipe_dom_defunct_finalize(struct kern_nexus_domain_provider * nxdom_prov,struct kern_nexus * nx,struct kern_channel * ch,boolean_t locked)442 nx_kpipe_dom_defunct_finalize(struct kern_nexus_domain_provider *nxdom_prov,
443     struct kern_nexus *nx, struct kern_channel *ch, boolean_t locked)
444 {
445 #pragma unused(nxdom_prov)
446 	if (!locked) {
447 		SK_LOCK_ASSERT_NOTHELD();
448 		SK_LOCK();
449 		LCK_MTX_ASSERT(&ch->ch_lock, LCK_MTX_ASSERT_NOTOWNED);
450 	} else {
451 		SK_LOCK_ASSERT_HELD();
452 		LCK_MTX_ASSERT(&ch->ch_lock, LCK_MTX_ASSERT_OWNED);
453 	}
454 
455 	ASSERT(!(ch->ch_flags & CHANF_KERNEL));
456 	ASSERT(ch->ch_na->na_type == NA_KERNEL_PIPE);
457 
458 	na_defunct(nx, ch, ch->ch_na, locked);
459 
460 	SK_D("%s(%d): ch %p -/- nx %p (%s:\"%s\":%u:%d)",
461 	    ch->ch_name, ch->ch_pid, SK_KVA(ch), SK_KVA(nx),
462 	    nxdom_prov->nxdom_prov_name, ch->ch_na->na_name,
463 	    ch->ch_info->cinfo_nx_port, (int)ch->ch_info->cinfo_ch_ring_id);
464 
465 	if (!locked) {
466 		LCK_MTX_ASSERT(&ch->ch_lock, LCK_MTX_ASSERT_NOTOWNED);
467 		SK_UNLOCK();
468 	} else {
469 		LCK_MTX_ASSERT(&ch->ch_lock, LCK_MTX_ASSERT_OWNED);
470 		SK_LOCK_ASSERT_HELD();
471 	}
472 }
473 
474 static int
nx_kpipe_prov_init(struct kern_nexus_domain_provider * nxdom_prov)475 nx_kpipe_prov_init(struct kern_nexus_domain_provider *nxdom_prov)
476 {
477 #pragma unused(nxdom_prov)
478 	SK_D("initializing %s", nxdom_prov->nxdom_prov_name);
479 	return 0;
480 }
481 
482 static int
nx_kpipe_prov_params_adjust(const struct kern_nexus_domain_provider * nxdom_prov,const struct nxprov_params * nxp,struct nxprov_adjusted_params * adj)483 nx_kpipe_prov_params_adjust(const struct kern_nexus_domain_provider *nxdom_prov,
484     const struct nxprov_params *nxp, struct nxprov_adjusted_params *adj)
485 {
486 #pragma unused(nxdom_prov, nxp, adj)
487 	return 0;
488 }
489 
490 static int
nx_kpipe_prov_params(struct kern_nexus_domain_provider * nxdom_prov,const uint32_t req,const struct nxprov_params * nxp0,struct nxprov_params * nxp,struct skmem_region_params srp[SKMEM_REGIONS],uint32_t pp_region_config_flags)491 nx_kpipe_prov_params(struct kern_nexus_domain_provider *nxdom_prov,
492     const uint32_t req, const struct nxprov_params *nxp0,
493     struct nxprov_params *nxp, struct skmem_region_params srp[SKMEM_REGIONS],
494     uint32_t pp_region_config_flags)
495 {
496 	struct nxdom *nxdom = nxdom_prov->nxdom_prov_dom;
497 
498 	return nxprov_params_adjust(nxdom_prov, req, nxp0, nxp, srp,
499 	           nxdom, nxdom, nxdom, pp_region_config_flags,
500 	           nx_kpipe_prov_params_adjust);
501 }
502 
503 static int
nx_kpipe_prov_mem_new(struct kern_nexus_domain_provider * nxdom_prov,struct kern_nexus * nx,struct nexus_adapter * na)504 nx_kpipe_prov_mem_new(struct kern_nexus_domain_provider *nxdom_prov,
505     struct kern_nexus *nx, struct nexus_adapter *na)
506 {
507 #pragma unused(nxdom_prov)
508 	int err = 0;
509 
510 	SK_DF(SK_VERB_KERNEL_PIPE,
511 	    "nx %p (\"%s\":\"%s\") na \"%s\" (%p)", SK_KVA(nx),
512 	    NX_DOM(nx)->nxdom_name, nxdom_prov->nxdom_prov_name, na->na_name,
513 	    SK_KVA(na));
514 
515 	ASSERT(na->na_arena == NULL);
516 	ASSERT(NX_USER_CHANNEL_PROV(nx));
517 	/*
518 	 * Store pp in the nexus to handle kern_nexus_get_pbufpool() calls.
519 	 */
520 	na->na_arena = skmem_arena_create_for_nexus(na,
521 	    NX_PROV(nx)->nxprov_region_params, &nx->nx_tx_pp,
522 	    &nx->nx_rx_pp, FALSE, FALSE, NULL, &err);
523 	ASSERT(na->na_arena != NULL || err != 0);
524 	ASSERT(nx->nx_tx_pp == NULL || (nx->nx_tx_pp->pp_md_type ==
525 	    NX_DOM(nx)->nxdom_md_type && nx->nx_tx_pp->pp_md_subtype ==
526 	    NX_DOM(nx)->nxdom_md_subtype));
527 	ASSERT(nx->nx_rx_pp == NULL || (nx->nx_rx_pp->pp_md_type ==
528 	    NX_DOM(nx)->nxdom_md_type && nx->nx_rx_pp->pp_md_subtype ==
529 	    NX_DOM(nx)->nxdom_md_subtype));
530 
531 	return err;
532 }
533 
534 static void
nx_kpipe_prov_fini(struct kern_nexus_domain_provider * nxdom_prov)535 nx_kpipe_prov_fini(struct kern_nexus_domain_provider *nxdom_prov)
536 {
537 #pragma unused(nxdom_prov)
538 	SK_D("destroying %s", nxdom_prov->nxdom_prov_name);
539 }
540 
541 static int
nx_kpipe_prov_nx_ctor(struct kern_nexus * nx)542 nx_kpipe_prov_nx_ctor(struct kern_nexus *nx)
543 {
544 #pragma unused(nx)
545 	SK_LOCK_ASSERT_HELD();
546 	ASSERT(nx->nx_arg == NULL);
547 	return 0;
548 }
549 
550 static void
nx_kpipe_prov_nx_dtor(struct kern_nexus * nx)551 nx_kpipe_prov_nx_dtor(struct kern_nexus *nx)
552 {
553 	struct nxbind *__single nxb;
554 
555 	SK_LOCK_ASSERT_HELD();
556 
557 	if ((nxb = nx->nx_arg) != NULL) {
558 		nxb_free(nxb);
559 		nx->nx_arg = NULL;
560 	}
561 }
562 
563 static int
nx_kpipe_prov_nx_mem_info(struct kern_nexus * nx,struct kern_pbufpool ** tpp,struct kern_pbufpool ** rpp)564 nx_kpipe_prov_nx_mem_info(struct kern_nexus *nx, struct kern_pbufpool **tpp,
565     struct kern_pbufpool **rpp)
566 {
567 	ASSERT(nx->nx_tx_pp != NULL);
568 	ASSERT(nx->nx_rx_pp != NULL);
569 
570 	if (tpp != NULL) {
571 		*tpp = nx->nx_tx_pp;
572 	}
573 	if (rpp != NULL) {
574 		*rpp = nx->nx_rx_pp;
575 	}
576 
577 	return 0;
578 }
579 
580 static struct nexus_kpipe_adapter *
na_kpipe_alloc(zalloc_flags_t how)581 na_kpipe_alloc(zalloc_flags_t how)
582 {
583 	struct nexus_kpipe_adapter *kna;
584 
585 	static_assert(offsetof(struct nexus_kpipe_adapter, kna_up) == 0);
586 
587 	kna = zalloc_flags(na_kpipe_zone, how | Z_ZERO);
588 	if (kna) {
589 		kna->kna_up.na_type = NA_KERNEL_PIPE;
590 		kna->kna_up.na_free = na_kpipe_free;
591 	}
592 	return kna;
593 }
594 
595 static void
na_kpipe_free(struct nexus_adapter * na)596 na_kpipe_free(struct nexus_adapter *na)
597 {
598 	struct nexus_kpipe_adapter *kna = (struct nexus_kpipe_adapter *)na;
599 
600 	ASSERT(kna->kna_up.na_refcount == 0);
601 	SK_DF(SK_VERB_MEM, "kna %p FREE", SK_KVA(kna));
602 	bzero(kna, sizeof(*kna));
603 	zfree(na_kpipe_zone, kna);
604 }
605 
606 static int
nx_kpipe_na_txsync(struct __kern_channel_ring * kring,struct proc * p,uint32_t flags)607 nx_kpipe_na_txsync(struct __kern_channel_ring *kring, struct proc *p,
608     uint32_t flags)
609 {
610 #pragma unused(p)
611 	SK_DF(SK_VERB_KERNEL_PIPE | SK_VERB_SYNC | SK_VERB_TX,
612 	    "%s(%d) kr \"%s\" (%p) krflags 0x%x ring %u flags 0%x",
613 	    sk_proc_name(p), sk_proc_pid(p), kring->ckr_name,
614 	    SK_KVA(kring), kring->ckr_flags, kring->ckr_ring_id, flags);
615 
616 	return nx_sync_tx(kring, (flags & NA_SYNCF_FORCE_RECLAIM));
617 }
618 
619 static int
nx_kpipe_na_rxsync(struct __kern_channel_ring * kring,struct proc * p,uint32_t flags)620 nx_kpipe_na_rxsync(struct __kern_channel_ring *kring, struct proc *p,
621     uint32_t flags)
622 {
623 #pragma unused(p)
624 	SK_DF(SK_VERB_KERNEL_PIPE | SK_VERB_SYNC | SK_VERB_RX,
625 	    "%s(%d) kr \"%s\" (%p) krflags 0x%x ring %u flags 0%x",
626 	    sk_proc_name(p), sk_proc_pid(p), kring->ckr_name,
627 	    SK_KVA(kring), kring->ckr_flags, kring->ckr_ring_id,
628 	    flags);
629 
630 	ASSERT(kring->ckr_rhead <= kring->ckr_lim);
631 
632 	return nx_sync_rx(kring, (flags & NA_SYNCF_FORCE_READ));
633 }
634 
635 static int
nx_kpipe_na_activate(struct nexus_adapter * na,na_activate_mode_t mode)636 nx_kpipe_na_activate(struct nexus_adapter *na, na_activate_mode_t mode)
637 {
638 	ASSERT(na->na_type == NA_KERNEL_PIPE);
639 
640 	SK_DF(SK_VERB_KERNEL_PIPE, "na \"%s\" (%p) %s", na->na_name,
641 	    SK_KVA(na), na_activate_mode2str(mode));
642 
643 	switch (mode) {
644 	case NA_ACTIVATE_MODE_ON:
645 		os_atomic_or(&na->na_flags, NAF_ACTIVE, relaxed);
646 		break;
647 
648 	case NA_ACTIVATE_MODE_DEFUNCT:
649 		break;
650 
651 	case NA_ACTIVATE_MODE_OFF:
652 		os_atomic_andnot(&na->na_flags, NAF_ACTIVE, relaxed);
653 		break;
654 
655 	default:
656 		VERIFY(0);
657 		/* NOTREACHED */
658 		__builtin_unreachable();
659 	}
660 
661 	return 0;
662 }
663 
664 static void
nx_kpipe_na_dtor(struct nexus_adapter * na)665 nx_kpipe_na_dtor(struct nexus_adapter *na)
666 {
667 #pragma unused(na)
668 	ASSERT(na->na_type == NA_KERNEL_PIPE);
669 }
670 
671 static int
nx_kpipe_na_krings_create(struct nexus_adapter * na,struct kern_channel * ch)672 nx_kpipe_na_krings_create(struct nexus_adapter *na, struct kern_channel *ch)
673 {
674 	ASSERT(na->na_type == NA_KERNEL_PIPE);
675 	/*
676 	 * The assumption here is that all kernel pipe instances
677 	 * are handled by IOSkywalkFamily, and thus we allocate
678 	 * the context area for it to store its object references.
679 	 */
680 	return na_rings_mem_setup(na, TRUE, ch);
681 }
682 
683 static void
nx_kpipe_na_krings_delete(struct nexus_adapter * na,struct kern_channel * ch,boolean_t defunct)684 nx_kpipe_na_krings_delete(struct nexus_adapter *na, struct kern_channel *ch,
685     boolean_t defunct)
686 {
687 	ASSERT(na->na_type == NA_KERNEL_PIPE);
688 
689 	na_rings_mem_teardown(na, ch, defunct);
690 }
691 
692 int
nx_kpipe_na_find(struct kern_nexus * nx,struct kern_channel * ch,struct chreq * chr,struct nxbind * nxb,struct proc * p,struct nexus_adapter ** ret,boolean_t create)693 nx_kpipe_na_find(struct kern_nexus *nx, struct kern_channel *ch,
694     struct chreq *chr, struct nxbind *nxb, struct proc *p,
695     struct nexus_adapter **ret, boolean_t create)
696 {
697 #pragma unused(ch, p)
698 	struct nxprov_params *nxp = NX_PROV(nx)->nxprov_params;
699 	struct nexus_kpipe_adapter *kna;
700 	ch_endpoint_t ep = chr->cr_endpoint;
701 	struct nexus_adapter *na = NULL;
702 	int error = 0;
703 
704 	SK_LOCK_ASSERT_HELD();
705 	*ret = NULL;
706 
707 #if SK_LOG
708 	uuid_string_t uuidstr;
709 	SK_PDF(SK_VERB_KERNEL_PIPE, p, "name \"%s\" spec_uuid \"%s\" port %d "
710 	    "mode 0x%x pipe_id %u ring_id %d ring_set %u ep_type %u create %u%s",
711 	    chr->cr_name, sk_uuid_unparse(chr->cr_spec_uuid, uuidstr),
712 	    (int)chr->cr_port, chr->cr_mode, chr->cr_pipe_id,
713 	    (int)chr->cr_ring_id, chr->cr_ring_set, chr->cr_endpoint, create,
714 	    (ep != CH_ENDPOINT_KERNEL_PIPE) ? " (skipped)" : "");
715 #endif /* SK_LOG */
716 
717 	if (ep != CH_ENDPOINT_KERNEL_PIPE) {
718 		return 0;
719 	}
720 
721 	if (!create) {
722 		return ENODEV;
723 	}
724 
725 	/*
726 	 * Check client credentials.
727 	 */
728 	if (!NX_ANONYMOUS_PROV(nx) && (nx->nx_arg == NULL || nxb == NULL ||
729 	    !nxb_is_equal(nx->nx_arg, nxb))) {
730 		return EACCES;
731 	}
732 
733 	kna = na_kpipe_alloc(Z_WAITOK);
734 
735 	na = &kna->kna_up;
736 	ASSERT(na->na_type == NA_KERNEL_PIPE);
737 	ASSERT(na->na_free == na_kpipe_free);
738 
739 	(void) snprintf(na->na_name, sizeof(na->na_name),
740 	    "%s{%u", chr->cr_name, NEXUS_PORT_KERNEL_PIPE_CLIENT);
741 	uuid_generate_random(na->na_uuid);
742 
743 	na->na_txsync = nx_kpipe_na_txsync;
744 	na->na_rxsync = nx_kpipe_na_rxsync;
745 	na->na_activate = nx_kpipe_na_activate;
746 	na->na_dtor = nx_kpipe_na_dtor;
747 	na->na_krings_create = nx_kpipe_na_krings_create;
748 	na->na_krings_delete = nx_kpipe_na_krings_delete;
749 	na_set_nrings(na, NR_TX, nxp->nxp_tx_rings);
750 	na_set_nrings(na, NR_RX, nxp->nxp_rx_rings);
751 	na_set_nslots(na, NR_TX, nxp->nxp_tx_slots);
752 	na_set_nslots(na, NR_RX, nxp->nxp_rx_slots);
753 	/*
754 	 * Verify upper bounds; the parameters must have already been
755 	 * validated by nxdom_prov_params() by the time we get here.
756 	 */
757 	ASSERT(na_get_nrings(na, NR_TX) <= NX_DOM(nx)->nxdom_tx_rings.nb_max);
758 	ASSERT(na_get_nrings(na, NR_RX) <= NX_DOM(nx)->nxdom_rx_rings.nb_max);
759 	ASSERT(na_get_nslots(na, NR_TX) <= NX_DOM(nx)->nxdom_tx_slots.nb_max);
760 	ASSERT(na_get_nslots(na, NR_RX) <= NX_DOM(nx)->nxdom_rx_slots.nb_max);
761 
762 	*(nexus_stats_type_t *)(uintptr_t)&na->na_stats_type =
763 	    NEXUS_STATS_TYPE_INVALID;
764 
765 	na_attach_common(na, nx, &nx_kpipe_prov_s);
766 
767 	if ((error = NX_DOM_PROV(nx)->nxdom_prov_mem_new(NX_DOM_PROV(nx),
768 	    nx, na)) != 0) {
769 		ASSERT(na->na_arena == NULL);
770 		goto err;
771 	}
772 	ASSERT(na->na_arena != NULL);
773 
774 	*(uint32_t *)(uintptr_t)&na->na_flowadv_max = nxp->nxp_flowadv_max;
775 	ASSERT(na->na_flowadv_max == 0 ||
776 	    skmem_arena_nexus(na->na_arena)->arn_flowadv_obj != NULL);
777 
778 #if SK_LOG
779 	SK_DF(SK_VERB_KERNEL_PIPE, "created kpipe adapter %p", SK_KVA(kna));
780 	SK_DF(SK_VERB_KERNEL_PIPE, "na_name: \"%s\"", na->na_name);
781 	SK_DF(SK_VERB_KERNEL_PIPE, "  UUID:        %s",
782 	    sk_uuid_unparse(na->na_uuid, uuidstr));
783 	SK_DF(SK_VERB_KERNEL_PIPE, "  nx:          %p (\"%s\":\"%s\")",
784 	    SK_KVA(na->na_nx), NX_DOM(na->na_nx)->nxdom_name,
785 	    NX_DOM_PROV(na->na_nx)->nxdom_prov_name);
786 	SK_DF(SK_VERB_KERNEL_PIPE, "  flags:       0x%x", na->na_flags);
787 	SK_DF(SK_VERB_KERNEL_PIPE, "  flowadv_max: %u", na->na_flowadv_max);
788 	SK_DF(SK_VERB_KERNEL_PIPE, "  rings:       tx %u rx %u",
789 	    na_get_nrings(na, NR_TX),
790 	    na_get_nrings(na, NR_RX));
791 	SK_DF(SK_VERB_KERNEL_PIPE, "  slots:       tx %u rx %u",
792 	    na_get_nslots(na, NR_TX),
793 	    na_get_nslots(na, NR_RX));
794 #if CONFIG_NEXUS_USER_PIPE
795 	SK_DF(SK_VERB_KERNEL_PIPE, "  next_pipe:   %u", na->na_next_pipe);
796 	SK_DF(SK_VERB_KERNEL_PIPE, "  max_pipes:   %u", na->na_max_pipes);
797 #endif /* CONFIG_NEXUS_USER_PIPE */
798 #endif /* SK_LOG */
799 
800 	*ret = na;
801 	na_retain_locked(*ret);
802 
803 	return 0;
804 
805 err:
806 	ASSERT(na != NULL);
807 	if (na->na_arena != NULL) {
808 		skmem_arena_release(na->na_arena);
809 		na->na_arena = NULL;
810 	}
811 	NA_FREE(na);
812 
813 	return error;
814 }
815 
816 #if (DEVELOPMENT || DEBUG)
817 SYSCTL_NODE(_kern_skywalk, OID_AUTO, kpipe, CTLFLAG_RW | CTLFLAG_LOCKED,
818     0, "Skywalk kpipe tuning");
819 #endif
820