xref: /xnu-11215.41.3/bsd/skywalk/nexus/nexus_adapter.h (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2015-2023 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) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
31  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *   1. Redistributions of source code must retain the above copyright
37  *      notice, this list of conditions and the following disclaimer.
38  *   2. Redistributions in binary form must reproduce the above copyright
39  *      notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  */
54 
55 #ifndef _SKYWALK_NEXUS_ADAPTER_H_
56 #define _SKYWALK_NEXUS_ADAPTER_H_
57 
58 #ifdef BSD_KERNEL_PRIVATE
59 #include <skywalk/os_skywalk_private.h>
60 #include <skywalk/os_packet_private.h>
61 
62 #define NEXUS_ADAPTER_NAMELEN   64
63 
64 struct chreq;
65 struct kern_nexus;
66 struct __kern_channel_ring;
67 struct nexus_vp_adapter;
68 struct nexus_upipe_adapter;
69 
70 typedef enum {
71 	NA_INVALID = 0,         /* uninitialized */
72 	NA_PSEUDO,              /* struct nexus_adapter */
73 #if CONFIG_NEXUS_USER_PIPE
74 	NA_USER_PIPE,           /* struct nexus_upipe_adapter */
75 #endif /* CONFIG_NEXUS_USER_PIPE */
76 #if CONFIG_NEXUS_KERNEL_PIPE
77 	NA_KERNEL_PIPE,         /* struct nexus_kpipe_adapter */
78 #endif /* CONFIG_NEXUS_KERNEL_PIPE */
79 #if CONFIG_NEXUS_MONITOR
80 	NA_MONITOR,             /* struct nexus_monitor_adapter */
81 #endif /* CONFIG_NEXUS_MONITOR */
82 #if CONFIG_NEXUS_NETIF
83 	NA_NETIF_DEV,           /* struct nexus_netif_adapter (dev) */
84 	NA_NETIF_HOST,          /* struct nexus_netif_adapter (host) */
85 	NA_NETIF_COMPAT_DEV,    /* struct nexus_netif_compat_adapter (dev) */
86 	NA_NETIF_COMPAT_HOST,   /* struct nexus_netif_compat_adapter (host) */
87 	NA_NETIF_FILTER,        /* struct nexus_netif_adapter (vp) */
88 	NA_NETIF_VP,            /* struct nexus_netif_adapter (vp) */
89 #endif /* CONFIG_NEXUS_NETIF */
90 #if CONFIG_NEXUS_FLOWSWITCH
91 	NA_FLOWSWITCH_VP,       /* struct nexus_vp_adapter */
92 #endif /* CONFIG_NEXUS_FLOWSWITCH */
93 } nexus_adapter_type_t;
94 
95 typedef enum {
96 	NXSPEC_CMD_CONNECT =    0,
97 	NXSPEC_CMD_DISCONNECT = 1,
98 	NXSPEC_CMD_START =      2,
99 	NXSPEC_CMD_STOP =       3,
100 } nxspec_cmd_t;
101 
102 typedef enum {
103 	NA_ACTIVATE_MODE_ON =   0,      /* activate adapter */
104 	NA_ACTIVATE_MODE_DEFUNCT,       /* defunct an activate adapter */
105 	NA_ACTIVATE_MODE_OFF,           /* deactivate adapter */
106 } na_activate_mode_t;
107 
108 struct nexus_pkt_stats {
109 	uint64_t nps_pkts;
110 	uint64_t nps_bytes;
111 };
112 
113 /*
114  * The "struct nexus_adapter" contains all base fields needed to support
115  * Nexus adapter operations.  There are different types of Nexus adapters
116  * (upipe, kpipe, fsw, monitor, vp, ...) so a nexus_adapter is
117  * always the first field in the derived type.
118  */
119 struct nexus_adapter {
120 	volatile uint32_t               na_flags;       /* NAF_* flags */
121 	nexus_adapter_type_t            na_type;        /* nexus type */
122 	const nexus_meta_type_t         na_md_type;     /* metadata type */
123 	const nexus_meta_subtype_t      na_md_subtype;  /* metadata subtype */
124 
125 	nexus_port_t na_nx_port;
126 
127 	/*
128 	 * Number of user-space descriptors using this interface,
129 	 * which is equal to the number of channel schema objects
130 	 * in the mapped region.
131 	 */
132 	uint32_t na_channels;
133 
134 	/* number of adapter transmit and receive rings */
135 	uint32_t na_num_rx_rings;
136 	uint32_t na_num_tx_rings;
137 
138 	/* number of ring pairs used by packet allocator */
139 	uint32_t na_num_allocator_ring_pairs;
140 
141 	/* number of event rings */
142 	uint32_t na_num_event_rings;
143 
144 	/* number of large buffer alloc rings */
145 	uint32_t na_num_large_buf_alloc_rings;
146 
147 	/* XXX -fbounds-safety: duplicate counts to avoid self-assignments */
148 	uint32_t na_rx_rings_cnt;
149 	uint32_t na_tx_rings_cnt;
150 	uint32_t na_alloc_free_rings_cnt;
151 	uint32_t na_event_rings_cnt;
152 	uint32_t na_large_buf_alloc_rings_cnt;
153 	uint32_t na_slot_ctxs_cnt;
154 	uint32_t na_scratch_cnt;
155 	uint32_t na_all_rings_cnt;
156 
157 	uint64_t na_work_ts;            /* when we last worked on it */
158 
159 	/*
160 	 * na_{tx,rx,alloc,free,event}_rings are private but allocated
161 	 * as a contiguous chunk of memory.
162 	 */
163 	struct __kern_channel_ring *__counted_by(na_tx_rings_cnt) na_tx_rings; /* array of TX rings. */
164 	struct __kern_channel_ring *__counted_by(na_rx_rings_cnt) na_rx_rings; /* array of RX rings. */
165 	struct __kern_channel_ring *__counted_by(na_all_rings_cnt) na_all_rings;
166 
167 	/*
168 	 * na_nx refers to the nexus instance associated with this
169 	 * nexus adapter; in cases such as the virtual port adapter
170 	 * of a flow switch nexus used for user pipe, this will
171 	 * indicate the latter.  The na_nxdom_prov will point to
172 	 * the actual nexus domain associated with the adapter.
173 	 */
174 	struct kern_nexus *na_nx;
175 
176 	/*
177 	 * Standard refcount to control the lifetime of the adapter
178 	 * (it should be equal to the lifetime of the corresponding ifp)
179 	 */
180 	volatile uint32_t na_refcount;
181 
182 	int na_si_users[NR_ALL];         /* # of users per global wait queue */
183 	struct ch_selinfo na_si[NR_ALL]; /* global wait queues */
184 
185 	/*
186 	 * Memory arena.
187 	 */
188 	struct skmem_arena *na_arena;
189 
190 	/*
191 	 * Number of descriptors in each queue.
192 	 */
193 	uint32_t na_num_tx_slots;
194 	uint32_t na_num_rx_slots;
195 	uint32_t na_num_allocator_slots;
196 	uint32_t na_num_event_slots;
197 	uint32_t na_num_large_buf_alloc_slots;
198 
199 	/*
200 	 * Combined slot count of all rings.
201 	 * Used for allocating slot_ctx and scratch memory.
202 	 */
203 	uint32_t na_total_slots;
204 
205 	/*
206 	 * Flow advisory (if applicable).
207 	 */
208 	const uint32_t na_flowadv_max;  /* max # of flow advisory entries */
209 
210 	/*
211 	 * Shareable statistics (if applicable).
212 	 */
213 	const nexus_stats_type_t na_stats_type; /* stats type */
214 
215 	/*
216 	 * Array of packet allocator and event rings
217 	 */
218 	struct __kern_channel_ring *__counted_by(na_alloc_free_rings_cnt)na_alloc_rings;
219 	struct __kern_channel_ring *__counted_by(na_alloc_free_rings_cnt)na_free_rings;
220 	struct __kern_channel_ring *__counted_by(na_event_rings_cnt)na_event_rings;
221 	struct __kern_channel_ring *__counted_by(na_large_buf_alloc_rings_cnt)na_large_buf_alloc_rings;
222 
223 	uint64_t na_ch_mit_ival;        /* mitigation interval */
224 
225 	/*
226 	 * The actual nexus domain associated with the adapter.
227 	 */
228 	struct kern_nexus_domain_provider *na_nxdom_prov;
229 
230 	/*
231 	 * Array of slot contexts.  This covers enough space to hold
232 	 * slot contexts of slot_ctx size for all of the TX and RX rings,
233 	 * It is optional and is requested at na_krings_create() time.
234 	 */
235 	struct slot_ctx *__counted_by(na_slot_ctxs_cnt)na_slot_ctxs;
236 
237 	/*
238 	 * Array of packet handlers, enough for all slots in the
239 	 * TX and RX rings of this adapter.  It is automatically
240 	 * created at na_krings_create() time.
241 	 */
242 	kern_packet_t *__counted_by(na_scratch_cnt)na_scratch;
243 
244 	struct __kern_channel_ring *__counted_by(0) na_tail; /* pointer past the last ring */
245 
246 #if CONFIG_NEXUS_FLOWSWITCH || CONFIG_NEXUS_NETIF
247 	/*
248 	 * Additional information attached to this adapter by other
249 	 * Skywalk subsystems; currently used by flow switch and netif.
250 	 */
251 	void *na_private;
252 
253 	/*
254 	 * References to the ifnet and device routines, used by the netif
255 	 * nexus adapter functions.  A non-NULL na_ifp indicates an io ref
256 	 * count to the ifnet that needs to be released at adapter detach
257 	 * time (at which point it will be nullifed).
258 	 */
259 	struct ifnet *na_ifp;
260 	/*
261 	 * lookup table to retrieve the ring corresponding to a service
262 	 * class. we store the ring index in na_(tx/rx)_rings array.
263 	 */
264 	uint8_t na_kring_svc_lut[KPKT_SC_MAX_CLASSES];
265 #endif /* CONFIG_NEXUS_FLOWSWITCH || CONFIG_NEXUS_NETIF */
266 
267 #if CONFIG_NEXUS_USER_PIPE
268 	uint32_t na_next_pipe;  /* next free slot in the array */
269 	uint32_t na_max_pipes;  /* size of the array */
270 	/* array of pipes that have this adapter as a parent */
271 	struct nexus_upipe_adapter **__counted_by(na_max_pipes) na_pipes;
272 #endif /* CONFIG_NEXUS_USER_PIPE */
273 
274 	char na_name[NEXUS_ADAPTER_NAMELEN];    /* diagnostics */
275 	uuid_t na_uuid;
276 
277 	/*
278 	 * na_activate() is called to activate, defunct or deactivate a nexus
279 	 * adapter.  This is invoked by na_bind_channel(), the first time a
280 	 * channel is opened to the adapter; by na_defunct() when an open
281 	 * channel gets defunct; as well as by na_unbind_channel() when the
282 	 * last channel instance opened to the adapter is closed.
283 	 */
284 	int (*na_activate)(struct nexus_adapter *, na_activate_mode_t);
285 	/*
286 	 * na_special() is an optional callback implemented by nexus types
287 	 * that support kernel channel (special mode).  This allows the nexus
288 	 * to override the logic surrounding na_{bind,unbind}_channel() calls.
289 	 */
290 	int (*na_special)(struct nexus_adapter *, struct kern_channel *,
291 	    struct chreq *, nxspec_cmd_t);
292 	/*
293 	 * na_txsync() pushes packets to the underlying device;
294 	 * na_rxsync() collects packets from the underlying device.
295 	 */
296 	int (*na_txsync)(struct __kern_channel_ring *kring, struct proc *,
297 	    uint32_t flags);
298 	int (*na_rxsync)(struct __kern_channel_ring *kring, struct proc *,
299 	    uint32_t flags);
300 #define NA_SYNCF_MONITOR                0x1
301 #define NA_SYNCF_FORCE_READ             0x2
302 #define NA_SYNCF_FORCE_RECLAIM          0x4
303 #define NA_SYNCF_NETIF                  0x8     /* netif normal sync */
304 #define NA_SYNCF_NETIF_ASYNC            0x10    /* asynchronous doorbell */
305 #define NA_SYNCF_NETIF_DOORBELL         0x20    /* doorbell request */
306 #define NA_SYNCF_NETIF_IFSTART          0x40    /* in if_start context */
307 #define NA_SYNCF_FORCE_UPP_SYNC         0x80    /* force upp sync alloc/free */
308 #define NA_SYNCF_UPP_PURGE              0x100   /* purge upp alloc pool */
309 #define NA_SYNCF_SYNC_ONLY              0x200   /* sync only, no doorbell */
310 
311 	/*
312 	 * na_notify() is used to act after data have become available,
313 	 * or the state of the ring has changed.  Depending on the nexus
314 	 * type, this may involve triggering an event and/or performing
315 	 * additional work such as calling na_txsync().
316 	 */
317 	int (*na_notify)(struct __kern_channel_ring *kring, struct proc *,
318 	    uint32_t flags);
319 #define NA_NOTEF_MONITOR        0x1
320 #define NA_NOTEF_IN_KEVENT      0x2
321 #define NA_NOTEF_CAN_SLEEP      0x4     /* OK to block in kr_enter() */
322 #define NA_NOTEF_NETIF          0x8     /* same as NA_SYNCF_NETIF */
323 #define NA_NOTEF_PUSH           0x100   /* need immediate attention */
324 
325 	/*
326 	 * na_channel_event_notify() is used to send events on the user channel.
327 	 */
328 	int (*na_channel_event_notify)(struct nexus_adapter *,
329 	    struct __kern_channel_event *, uint16_t);
330 	/*
331 	 * na_config() is an optional callback for returning nexus-specific
332 	 * configuration information.  This is implemented by nexus types
333 	 * that handle dynamically changing configs.
334 	 */
335 	int (*na_config)(struct nexus_adapter *,
336 	    uint32_t *txr, uint32_t *txd, uint32_t *rxr, uint32_t *rxd);
337 	/*
338 	 * na_krings_create() creates and initializes the __kern_channel_ring
339 	 * arrays, as well as initializing the callback routines within;
340 	 * na_krings_delete() cleans up and destroys the kernel rings.
341 	 */
342 	int (*na_krings_create)(struct nexus_adapter *, struct kern_channel *);
343 	void (*na_krings_delete)(struct nexus_adapter *, struct kern_channel *,
344 	    boolean_t);
345 	/*
346 	 * na_dtor() is the destructor callback that is invoked when the
347 	 * last reference to the nexus adapter has been released.
348 	 */
349 	void (*na_dtor)(struct nexus_adapter *);
350 	/*
351 	 * na_free() is the free callback that gets invoked after the
352 	 * adapter has been destroyed.
353 	 */
354 	void (*na_free)(struct nexus_adapter *);
355 
356 	/*
357 	 * packet-chain-based callbacks for passing packets up the stack.
358 	 * The inject variant is used by filters for rejecting packets
359 	 * into the rx path from user space.
360 	 */
361 	void (*na_rx)(struct nexus_adapter *,
362 	    struct __kern_packet *, struct nexus_pkt_stats *);
363 };
364 
365 /* valid values for na_flags */
366 #define NAF_ACTIVE              0x1     /* skywalk is active */
367 #define NAF_HOST_ONLY           0x2     /* host adapter (no device rings) */
368 #define NAF_SPEC_INIT           0x4     /* na_special() initialized */
369 #define NAF_NATIVE              0x8     /* skywalk native netif adapter */
370 #define NAF_MEM_NO_INIT         0x10    /* na_kr_setup() skipped */
371 #define NAF_SLOT_CONTEXT        0x20    /* na_slot_ctxs is valid */
372 #define NAF_USER_PKT_POOL       0x40    /* na supports user packet pool */
373 #define NAF_TX_MITIGATION       0x80    /* na supports TX event mitigation */
374 #define NAF_RX_MITIGATION       0x100   /* na supports RX event mitigation */
375 #define NAF_DEFUNCT             0x200   /* no longer in service */
376 #define NAF_MEM_LOANED          0x400   /* arena owned by another adapter */
377 #define NAF_REJECT              0x800   /* not accepting channel activities */
378 #define NAF_EVENT_RING          0x1000  /* NA is providing event ring */
379 #define NAF_CHANNEL_EVENT_ATTACHED 0x2000 /* kevent registered for ch events */
380 #define NAF_VIRTUAL_DEVICE      0x8000  /* netif adapter for virtual device */
381 #define NAF_MODE_FSW            0x10000 /* NA is owned by fsw */
382 #define NAF_MODE_LLW            0x20000 /* NA is owned by llw */
383 #define NAF_LOW_LATENCY         0x40000 /* Low latency NA */
384 #define NAF_DRAINING            0x80000 /* NA is being drained */
385 /*
386  * defunct allowed flag.
387  * Currently used only by the parent nexus adapter of user-pipe nexus
388  * to indicate that defuncting is allowed on the channels.
389  */
390 #define NAF_DEFUNCT_OK          0x100000
391 #define NAF_KERNEL_ONLY (1U << 31) /* used internally, not usable by userland */
392 
393 #define NAF_BITS                                                         \
394 	"\020\01ACTIVE\02HOST_ONLY\03SPEC_INIT\04NATIVE"                 \
395 	"\05MEM_NO_INIT\06SLOT_CONTEXT\07USER_PKT_POOL"                  \
396 	"\010TX_MITIGATION\011RX_MITIGATION\012DEFUNCT\013MEM_LOANED"    \
397 	"\014REJECT\015EVENT_RING\016EVENT_ATTACH"                       \
398 	"\020VIRTUAL\021MODE_FSW\022MODE_LLW\023LOW_LATENCY\024DRAINING" \
399 	"\025DEFUNCT_OK\040KERNEL_ONLY"
400 
401 #define NA_FREE(na) do {                                                 \
402 	(na)->na_free(na);                                               \
403 } while (0)
404 
405 /*
406  * NA returns a pointer to the struct nexus_adapter from the ifp's netif nexus.
407  */
408 #define NA(_ifp)                ((_ifp)->if_na)
409 
410 __attribute__((always_inline))
411 static inline uint32_t
na_get_nslots(const struct nexus_adapter * na,enum txrx t)412 na_get_nslots(const struct nexus_adapter *na, enum txrx t)
413 {
414 	switch (t) {
415 	case NR_TX:
416 		return na->na_num_tx_slots;
417 	case NR_RX:
418 		return na->na_num_rx_slots;
419 	case NR_A:
420 	case NR_F:
421 		return na->na_num_allocator_slots;
422 	case NR_EV:
423 		return na->na_num_event_slots;
424 	case NR_LBA:
425 		return na->na_num_large_buf_alloc_slots;
426 	default:
427 		VERIFY(0);
428 		/* NOTREACHED */
429 		__builtin_unreachable();
430 	}
431 }
432 
433 __attribute__((always_inline))
434 static inline void
na_set_nslots(struct nexus_adapter * na,enum txrx t,uint32_t v)435 na_set_nslots(struct nexus_adapter *na, enum txrx t, uint32_t v)
436 {
437 	switch (t) {
438 	case NR_TX:
439 		na->na_num_tx_slots = v;
440 		break;
441 	case NR_RX:
442 		na->na_num_rx_slots = v;
443 		break;
444 	case NR_A:
445 	case NR_F:
446 		na->na_num_allocator_slots = v;
447 		break;
448 	case NR_EV:
449 		na->na_num_event_slots = v;
450 		break;
451 	case NR_LBA:
452 		na->na_num_large_buf_alloc_slots = v;
453 		break;
454 	default:
455 		VERIFY(0);
456 		/* NOTREACHED */
457 		__builtin_unreachable();
458 	}
459 }
460 
461 __attribute__((always_inline))
462 static inline uint32_t
na_get_nrings(const struct nexus_adapter * na,enum txrx t)463 na_get_nrings(const struct nexus_adapter *na, enum txrx t)
464 {
465 	switch (t) {
466 	case NR_TX:
467 		return na->na_num_tx_rings;
468 	case NR_RX:
469 		return na->na_num_rx_rings;
470 	case NR_A:
471 	case NR_F:
472 		return na->na_num_allocator_ring_pairs;
473 	case NR_EV:
474 		return na->na_num_event_rings;
475 	case NR_LBA:
476 		return na->na_num_large_buf_alloc_rings;
477 	default:
478 		VERIFY(0);
479 		/* NOTREACHED */
480 		__builtin_unreachable();
481 	}
482 }
483 
484 __attribute__((always_inline))
485 static inline void
na_set_nrings(struct nexus_adapter * na,enum txrx t,uint32_t v)486 na_set_nrings(struct nexus_adapter *na, enum txrx t, uint32_t v)
487 {
488 	switch (t) {
489 	case NR_TX:
490 		na->na_num_tx_rings = v;
491 		break;
492 	case NR_RX:
493 		na->na_num_rx_rings = v;
494 		break;
495 	case NR_A:
496 	case NR_F:
497 		na->na_num_allocator_ring_pairs = v;
498 		break;
499 	case NR_EV:
500 		na->na_num_event_rings = v;
501 		break;
502 	case NR_LBA:
503 		/* we only support one ring for now */
504 		ASSERT(v <= 1);
505 		na->na_num_large_buf_alloc_rings = v;
506 		break;
507 	default:
508 		VERIFY(0);
509 		/* NOTREACHED */
510 		__builtin_unreachable();
511 	}
512 }
513 
514 __attribute__((always_inline))
515 static inline struct __kern_channel_ring *__header_indexable
NAKR(struct nexus_adapter * na,enum txrx t)516 NAKR(struct nexus_adapter *na, enum txrx t)
517 {
518 	switch (t) {
519 	case NR_TX:
520 		return na->na_tx_rings;
521 	case NR_RX:
522 		return na->na_rx_rings;
523 	case NR_A:
524 		return na->na_alloc_rings;
525 	case NR_F:
526 		return na->na_free_rings;
527 	case NR_EV:
528 		return na->na_event_rings;
529 	case NR_LBA:
530 		return na->na_large_buf_alloc_rings;
531 	default:
532 		VERIFY(0);
533 		/* NOTREACHED */
534 		__builtin_unreachable();
535 	}
536 }
537 
538 #define KR_SINGLE(kr) (__unsafe_forge_single(struct __kern_channel_ring *, (kr)))
539 
540 /*
541  * If the adapter is owned by the kernel, neither another flow switch nor user
542  * can use it; if the adapter is owned by a user, only users can share it.
543  * Evaluation must be done under SK_LOCK().
544  */
545 #define NA_KERNEL_ONLY(_na)     (((_na)->na_flags & NAF_KERNEL_ONLY) != 0)
546 #define NA_OWNED_BY_ANY(_na) \
547 	(NA_KERNEL_ONLY(_na) || ((_na)->na_channels > 0))
548 #define NA_OWNED_BY_FSW(_na) \
549 	(((_na)->na_flags & NAF_MODE_FSW) != 0)
550 #define NA_OWNED_BY_LLW(_na) \
551 	(((_na)->na_flags & NAF_MODE_LLW) != 0)
552 
553 /*
554  * Whether the adapter has been activated via na_activate() call.
555  */
556 #define NA_IS_ACTIVE(_na)       (((_na)->na_flags & NAF_ACTIVE) != 0)
557 #define NA_IS_DEFUNCT(_na)       (((_na)->na_flags & NAF_DEFUNCT) != 0)
558 #define NA_CHANNEL_EVENT_ATTACHED(_na)   \
559     (((_na)->na_flags & NAF_CHANNEL_EVENT_ATTACHED) != 0)
560 /*
561  * Whether channel activities are rejected by the adapter.  This takes the
562  * nexus adapter argument separately, as ch->ch_na may not be set yet.
563  */
564 __attribute__((always_inline))
565 static inline boolean_t
na_reject_channel(struct kern_channel * ch,struct nexus_adapter * na)566 na_reject_channel(struct kern_channel *ch, struct nexus_adapter *na)
567 {
568 	boolean_t reject;
569 
570 	ASSERT(ch->ch_na == NULL || ch->ch_na == na);
571 
572 	if ((na->na_flags & NAF_REJECT) || NX_REJECT_ACT(na->na_nx)) {
573 		/* set trapdoor NAF_REJECT flag */
574 		if (!(na->na_flags & NAF_REJECT)) {
575 			SK_ERR("%s(%d) marked as non-permissive",
576 			    ch->ch_name, ch->ch_pid);
577 			os_atomic_or(&na->na_flags, NAF_REJECT, relaxed);
578 			ch_deactivate(ch);
579 		}
580 		reject = TRUE;
581 	} else {
582 		reject = FALSE;
583 	}
584 
585 	return reject;
586 }
587 
588 #if SK_LOG
589 __attribute__((always_inline))
590 static inline const char *
na_activate_mode2str(na_activate_mode_t m)591 na_activate_mode2str(na_activate_mode_t m)
592 {
593 	switch (m) {
594 	case NA_ACTIVATE_MODE_ON:
595 		return "on";
596 	case NA_ACTIVATE_MODE_DEFUNCT:
597 		return "defunct";
598 	case NA_ACTIVATE_MODE_OFF:
599 		return "off";
600 	default:
601 		VERIFY(0);
602 		/* NOTREACHED */
603 		__builtin_unreachable();
604 	}
605 }
606 #endif /* SK_LOG */
607 
608 __BEGIN_DECLS
609 extern void na_init(void);
610 extern void na_fini(void);
611 
612 extern int na_bind_channel(struct nexus_adapter *na, struct kern_channel *ch,
613     struct chreq *);
614 extern void na_unbind_channel(struct kern_channel *ch);
615 
616 /*
617  * Common routine for all functions that create a nexus adapter. It performs
618  * two main tasks:
619  * - if the na points to an ifp, mark the ifp as Skywalk capable
620  *   using na as its native adapter;
621  * - provide defaults for the setup callbacks and the memory allocator
622  */
623 extern void na_attach_common(struct nexus_adapter *,
624     struct kern_nexus *, struct kern_nexus_domain_provider *);
625 /*
626  * Update the ring parameters (number and size of tx and rx rings).
627  * It calls the nm_config callback, if available.
628  */
629 extern int na_update_config(struct nexus_adapter *na);
630 
631 extern int na_rings_mem_setup(struct nexus_adapter *, boolean_t,
632     struct kern_channel *);
633 extern void na_rings_mem_teardown(struct nexus_adapter *,
634     struct kern_channel *, boolean_t);
635 extern void na_ch_rings_defunct(struct kern_channel *, struct proc *);
636 
637 /* convenience wrappers for na_set_all_rings, used in drivers */
638 extern void na_disable_all_rings(struct nexus_adapter *);
639 extern void na_enable_all_rings(struct nexus_adapter *);
640 extern void na_lock_all_rings(struct nexus_adapter *);
641 extern void na_unlock_all_rings(struct nexus_adapter *);
642 extern int na_interp_ringid(struct nexus_adapter *, ring_id_t, ring_set_t,
643     uint32_t[NR_TXRX], uint32_t[NR_TXRX]);
644 extern struct kern_pbufpool *na_kr_get_pp(struct nexus_adapter *, enum txrx);
645 
646 extern int na_find(struct kern_channel *, struct kern_nexus *,
647     struct chreq *, struct kern_channel *, struct nxbind *,
648     struct proc *, struct nexus_adapter **, boolean_t);
649 extern void na_retain_locked(struct nexus_adapter *na);
650 extern int na_release_locked(struct nexus_adapter *na);
651 
652 extern int na_connect(struct kern_nexus *, struct kern_channel *,
653     struct chreq *, struct kern_channel *, struct nxbind *, struct proc *);
654 extern void na_disconnect(struct kern_nexus *, struct kern_channel *);
655 extern void na_defunct(struct kern_nexus *, struct kern_channel *,
656     struct nexus_adapter *, boolean_t);
657 extern int na_connect_spec(struct kern_nexus *, struct kern_channel *,
658     struct chreq *, struct proc *);
659 extern void na_disconnect_spec(struct kern_nexus *, struct kern_channel *);
660 extern void na_start_spec(struct kern_nexus *, struct kern_channel *);
661 extern void na_stop_spec(struct kern_nexus *, struct kern_channel *);
662 
663 extern int na_pseudo_create(struct kern_nexus *, struct chreq *,
664     struct nexus_adapter **);
665 extern void na_kr_drop(struct nexus_adapter *, boolean_t);
666 extern void na_flowadv_entry_alloc(const struct nexus_adapter *, uuid_t,
667     const flowadv_idx_t, const uint32_t);
668 extern void na_flowadv_entry_free(const struct nexus_adapter *, uuid_t,
669     const flowadv_idx_t, const uint32_t);
670 extern bool na_flowadv_set(const struct kern_channel *,
671     const flowadv_idx_t, const flowadv_token_t);
672 extern bool na_flowadv_clear(const struct kern_channel *,
673     const flowadv_idx_t, const flowadv_token_t);
674 extern int na_flowadv_report_ce_event(const struct kern_channel *ch,
675     const flowadv_idx_t fe_idx, const flowadv_token_t flow_token,
676     uint32_t ce_cnt, uint32_t total_pkt_cnt);
677 extern void na_flowadv_event(struct __kern_channel_ring *);
678 extern void na_post_event(struct __kern_channel_ring *, boolean_t, boolean_t,
679     boolean_t, uint32_t);
680 
681 extern void na_drain(struct nexus_adapter *, boolean_t);
682 
683 __END_DECLS
684 #endif /* BSD_KERNEL_PRIVATE */
685 #endif /* _SKYWALK_NEXUS_ADAPTER_H_ */
686