xref: /xnu-8796.121.2/bsd/skywalk/nexus/flowswitch/nx_flowswitch.h (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2015-2022 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_FLOWSWITCH_H_
56 #define _SKYWALK_NEXUS_FLOWSWITCH_H_
57 
58 #include <skywalk/os_skywalk_private.h>
59 #include <net/ethernet.h>
60 #include <net/if_vlan_var.h>
61 #include <netinet/ip6.h>
62 
63 #include <skywalk/nexus/flowswitch/flow/flow_var.h>
64 
65 #if CONFIG_NEXUS_FLOWSWITCH
66 /* Shared declarations for the flow switch. */
67 #define NX_FSW_NAME             "fsw"   /* prefix for flow switch port name */
68 
69 #define NX_FSW_MAXRINGS         NX_MAX_NUM_RING_PAIR
70 #define NX_FSW_TXRINGSIZE       256     /* default TX ring size */
71 #define NX_FSW_RXRINGSIZE       1024    /* default RX ring size */
72 #define NX_FSW_AFRINGSIZE       256     /* default Alloc/Free ring size */
73 
74 #define NX_FSW_CHUNK            64      /* port chunk */
75 #define NX_FSW_CHUNK_FREE       0xffffffffffffffff /* entire chunk is free */
76 
77 #define NX_FSW_VP_MIN           NX_FSW_CHUNK
78 #define NX_FSW_VP_MAX           4096    /* up to 4k ports */
79 #define NX_FSW_VP_NOPORT        (NX_FSW_VP_MAX+1)
80 #define NX_FSW_VP_BROADCAST     NX_FSW_VP_MAX
81 
82 #define NX_FSW_MINSLOTS         2       /* XXX unclear how many */
83 #define NX_FSW_MAXSLOTS         NX_MAX_NUM_SLOT_PER_RING /* max # of slots */
84 
85 #define NX_FSW_TXBATCH          64      /* default TX batch size */
86 #define NX_FSW_RXBATCH          32      /* default RX batch size */
87 
88 #define NX_FSW_BUFSIZE          (2 * 1024)  /* default buffer size */
89 
90 #define NX_FSW_MINBUFSIZE       512  /* min buffer size */
91 #define NX_FSW_MAXBUFSIZE       (32 * 1024) /* max buffer size */
92 #define NX_FSW_MAXBUFFERS       (4 * 1024) /* max number of buffers */
93 /* max number of buffers for memory constrained device */
94 #define NX_FSW_MAXBUFFERS_MEM_CONSTRAINED (2 * 1024)
95 /* default user buffer segment size for non memory-constrained device */
96 #define NX_FSW_BUF_SEG_SIZE     (32 * 1024)
97 
98 /* {min, def, max} values for large buffer size */
99 #define NX_FSW_MIN_LARGE_BUFSIZE    0
100 #define NX_FSW_DEF_LARGE_BUFSIZE    (16 * 1024)
101 #define NX_FSW_MAX_LARGE_BUFSIZE    (32 * 1024)
102 
103 /*
104  * TODO: [email protected] -- minimum buflets for now; we will need to
105  * have a way to adjust this based on the underlying interface's
106  * parameters, e.g. jumbo MTU, large segment offload, etc.
107  */
108 #define NX_FSW_UMD_SIZE _USER_PACKET_SIZE(BUFLETS_MIN)
109 #define NX_FSW_KMD_SIZE _KERN_PACKET_SIZE(BUFLETS_MIN)
110 
111 #define NX_FSW_EVENT_RING_NUM      1     /* number of event rings */
112 #define NX_FSW_EVENT_RING_SIZE     32    /* default event ring size */
113 
114 
115 /*
116  * macro to check if TCP RX aggregation is enabled in flowswitch.
117  */
118 #define NX_FSW_TCP_RX_AGG_ENABLED()    (sk_fsw_rx_agg_tcp != 0)
119 
120 struct nx_flowswitch;
121 
122 /*
123  * Virtual port nexus adapter
124  */
125 struct nexus_vp_adapter {
126 	/*
127 	 * This is an overlay structure on nexus_adapter;
128 	 * make sure it contains 'up' as the first member.
129 	 */
130 	struct nexus_adapter vpna_up;
131 
132 	/*
133 	 * Flow switch support:
134 	 *
135 	 * If the adapter is associated with a nexus port, vpna_fsw points
136 	 * to the flow switch this NA is attached to; vpna_nx_port is the
137 	 * port number used in the flow switch.  Otherwise, vpna_fsw would
138 	 * be NULL and vpna_nx_port would be NEXUS_PORT_ANY.
139 	 */
140 	struct nx_flowswitch *vpna_fsw;
141 	nexus_port_t    vpna_nx_port;
142 	uint16_t        vpna_gencnt;
143 	boolean_t       vpna_retry;
144 	boolean_t       vpna_pid_bound;
145 	boolean_t       vpna_defunct;
146 	pid_t           vpna_pid;
147 };
148 
149 #define VPNA(_na)       ((struct nexus_vp_adapter *)(_na))
150 
151 #define NEXUS_PROVIDER_FLOW_SWITCH      "com.apple.nexus.flowswitch"
152 
153 /* fsw_state_flags */
154 #define FSW_STATEF_QUIESCED             0x0001
155 #define FSW_STATEF_NETAGENT_ADDED       0x0002
156 #define FSW_STATEF_NETAGENT_ENABLED     0x0004
157 
158 #define FSW_QUIESCED(_fsw) \
159     (((_fsw)->fsw_state_flags & FSW_STATEF_QUIESCED) != 0)
160 
161 #define FSW_NETAGENT_ADDED(_fsw) \
162     (((_fsw)->fsw_state_flags & FSW_STATEF_NETAGENT_ADDED) != 0)
163 
164 #define FSW_NETAGENT_ENABLED(_fsw)                                      \
165     (((_fsw)->fsw_state_flags & FSW_STATEF_NETAGENT_ENABLED) != 0)
166 
167 #if (DEVELOPMENT || DEBUG)
168 #define FSW_RPS_MAX_NTHREADS    64
169 struct fsw_rps_thread {
170 	lck_mtx_t               frt_lock;
171 	struct nx_flowswitch    *frt_fsw;
172 	struct thread           *frt_thread;
173 	struct pktq             frt_pktq;
174 
175 	uint32_t                frt_idx;
176 	uint32_t                frt_flags;
177 	uint32_t                frt_requests;
178 };
179 
180 #define FRT_RUNNING             0x00000001
181 #define FRT_TERMINATEBLOCK      0x10000000
182 #define FRT_TERMINATING         0x20000000
183 #define FRT_TERMINATED          0x40000000
184 #endif /* !DEVELOPMENT && !DEBUG */
185 
186 /*
187  * nx_flowswitch is a descriptor for a flow switch instance.
188  * Interfaces for a flow switch are all in fsw_ports[].
189  * The array has fixed size, an empty entry does not terminate
190  * the search, but lookups only occur on attach/detach so we
191  * don't mind if they are slow.
192  *
193  * The flow switch is non blocking on the transmit ports: excess
194  * packets are dropped if there is no room on the output port.
195  *
196  * fsw_lock protects accesses to the fsw_ports array.
197  * This is a rw lock (or equivalent).
198  */
199 struct nx_flowswitch {
200 	decl_lck_rw_data(, fsw_lock);
201 	uint32_t                fsw_tx_rings;
202 	uint32_t                fsw_rx_rings;
203 
204 	struct kern_nexus       *fsw_nx;
205 
206 	/* packet type enqueued by the class queues */
207 	classq_pkt_type_t       fsw_classq_enq_ptype;
208 	boolean_t               fsw_classq_enabled;
209 
210 	/* packet copy routines */
211 	pkt_copy_from_pkt_t     *fsw_pkt_copy_from_pkt;
212 	pkt_copy_from_mbuf_t    *fsw_pkt_copy_from_mbuf;
213 	pkt_copy_to_mbuf_t      *fsw_pkt_copy_to_mbuf;
214 
215 	uint8_t                 fsw_frame_headroom;
216 	uint32_t                fsw_src_lla_gencnt;
217 	uint32_t                fsw_pending_nonviable;
218 	uint32_t                fsw_low_power_gencnt;
219 
220 	/* The following are protected by fsw_lock. */
221 	struct flow_mgr         *fsw_flow_mgr;
222 	netagent_session_t      fsw_agent_session;
223 	uuid_t                  fsw_agent_uuid;
224 	struct ifnet            *fsw_ifp;        /* host interface */
225 	struct nexus_adapter    *fsw_nifna;      /* netif adapter */
226 	uint32_t                fsw_state_flags; /* FSW_STATEF_* */
227 
228 
229 	union {
230 		uint64_t _buf[1];
231 		uint8_t _eth_src[ETHER_ADDR_LEN];
232 	} __fsw_slladdr __attribute((aligned(sizeof(uint64_t))));
233 
234 #define fsw_slla                 __fsw_slladdr._buf
235 #define fsw_ether_shost          __fsw_slladdr._eth_src
236 
237 	int (*fsw_resolve)(struct nx_flowswitch *, struct flow_route *,
238 	    struct __kern_packet *);
239 	void (*fsw_frame)(struct nx_flowswitch *, struct flow_route *,
240 	    struct __kern_packet *);
241 	sa_family_t (*fsw_demux)(struct nx_flowswitch *,
242 	    struct __kern_packet *);
243 
244 	struct fsw_stats        fsw_stats;
245 
246 	/*
247 	 * The host interface attachment to the flowswitch (fsw_ifp), as well
248 	 * as the netagent registration, are guarded by the flowswitch's RW
249 	 * lock.  During fsw_flow_bind() time, we need to make sure they are
250 	 * valid before proceeding forward, but holding that RW lock across
251 	 * the routine is not possible since the thread may block if there
252 	 * are other threads performing fsw_flow_{bind,unbind} on the same
253 	 * flow owner bucket.  To prevent fsw_dtor() from happening while
254 	 * fsw_flow_bind() is in progress, we need to have it wait until all
255 	 * pending flow binds are done.  To do this we add a busy counter
256 	 * incremented at flow bind time, and use the lock for synchronization.
257 	 */
258 	decl_lck_mtx_data(, fsw_detach_barrier_lock);
259 	uint32_t                fsw_detach_flags;        /* see fsw_DETACHF_* */
260 	uint32_t                fsw_detach_barriers;
261 	uint32_t                fsw_detach_waiters;
262 
263 	uint32_t                fsw_ifp_dlt;
264 
265 	/*
266 	 * largest allocated packet size.
267 	 * used by: mbuf batch allocation logic during netif copy.
268 	 */
269 	uint32_t                fsw_rx_largest_size;
270 
271 	void (*fsw_ctor)(struct nx_flowswitch *, struct flow_route *);
272 
273 	/* store stats from na that is going to be deactivated */
274 	struct __nx_stats_fsw   *fsw_closed_na_stats;
275 
276 	/* ip fragments manager */
277 	struct fsw_ip_frag_mgr  *fsw_ipfm;
278 
279 	struct skoid            fsw_skoid;
280 
281 	/* input network emulator */
282 	struct netem            *fsw_input_netem;
283 
284 	struct kern_channel     *fsw_dev_ch;
285 	struct kern_channel     *fsw_host_ch;
286 
287 	/*
288 	 * The reaper thread gets scheduled on-demand, whenever there
289 	 * is any lingering flow entry needing to be freed or becoming
290 	 * nonviable.  Access is protected by fsw_reap_lock.
291 	 */
292 	decl_lck_mtx_data(, fsw_reap_lock);
293 	uint32_t                fsw_reap_flags;  /* see fsw_REAPF_* */
294 	uint32_t                fsw_reap_requests;
295 	struct thread           *fsw_reap_thread;
296 	char                    fsw_reap_name[MAXTHREADNAMESIZE];
297 
298 	uint64_t                fsw_reap_last;
299 	uint64_t                fsw_drain_channel_chk_last;
300 	uint64_t                fsw_drain_netif_chk_last;
301 
302 	decl_lck_mtx_data(, fsw_linger_lock);
303 	struct flow_entry_linger_head fsw_linger_head;
304 	uint32_t                fsw_linger_cnt;
305 
306 #if (DEVELOPMENT || DEBUG)
307 	uint32_t                fsw_rps_nthreads;
308 	struct fsw_rps_thread   *fsw_rps_threads;
309 #endif /* !DEVELOPMENT && !DEBUG */
310 };
311 
312 #define NX_FSW_PRIVATE(_nx) ((struct nx_flowswitch *)(_nx)->nx_arg)
313 
314 #define FSW_RWINIT(_fsw)                \
315 	lck_rw_init(&(_fsw)->fsw_lock, &nexus_lock_group, &nexus_lock_attr)
316 #define FSW_WLOCK(_fsw)                 \
317 	lck_rw_lock_exclusive(&(_fsw)->fsw_lock)
318 #define FSW_WUNLOCK(_fsw)               \
319 	lck_rw_unlock_exclusive(&(_fsw)->fsw_lock)
320 #define FSW_WLOCKTORLOCK(_fsw)          \
321 	lck_rw_lock_exclusive_to_shared(&(_fsw)->fsw_lock)
322 #define FSW_RLOCK(_fsw)                 \
323 	lck_rw_lock_shared(&(_fsw)->fsw_lock)
324 #define FSW_RLOCKTOWLOCK(_fsw)          \
325 	lck_rw_lock_shared_to_exclusive(&(_fsw)->fsw_lock)
326 #define FSW_RTRYLOCK(_fsw)              \
327 	lck_rw_try_lock(&(_fsw)->fsw_lock, LCK_RW_TYPE_SHARED)
328 #define FSW_RUNLOCK(_fsw)               \
329 	lck_rw_unlock_shared(&(_fsw)->fsw_lock)
330 #define FSW_UNLOCK(_fsw)                \
331 	lck_rw_done(&(_fsw)->fsw_lock)
332 #define FSW_RWDESTROY(_fsw)             \
333 	lck_rw_destroy(&(_fsw)->fsw_lock, &nexus_lock_group)
334 #define FSW_WLOCK_ASSERT_HELD(_fsw)     \
335 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_EXCLUSIVE)
336 #define FSW_RLOCK_ASSERT_HELD(_fsw)     \
337 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_SHARED)
338 #define FSW_LOCK_ASSERT_HELD(_fsw)      \
339 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_HELD)
340 
341 extern struct nxdom nx_flowswitch_dom_s;
342 extern struct kern_nexus_domain_provider nx_fsw_prov_s;
343 
344 SYSCTL_DECL(_kern_skywalk_flowswitch);
345 
346 /* functions used by external modules to interface with flow switch */
347 __BEGIN_DECLS
348 extern int nx_fsw_na_find(struct kern_nexus *, struct kern_channel *,
349     struct chreq *, struct nxbind *, struct proc *, struct nexus_adapter **,
350     boolean_t);
351 extern boolean_t nx_fsw_dom_port_is_reserved(struct kern_nexus *nx,
352     nexus_port_t nx_port);
353 extern int nx_fsw_netagent_add(struct kern_nexus *nx);
354 extern int nx_fsw_netagent_remove(struct kern_nexus *nx);
355 extern void nx_fsw_netagent_update(struct kern_nexus *nx);
356 extern void fsw_devna_rx(struct nexus_adapter *, struct __kern_packet *,
357     struct nexus_pkt_stats *);
358 extern struct nx_flowswitch *fsw_ifp_to_fsw(struct ifnet *);
359 
360 __END_DECLS
361 #endif /* CONFIG_NEXUS_FLOWSWITCH */
362 #endif /* _SKYWALK_NEXUS_FLOWSWITCH_H_ */
363