xref: /xnu-8020.140.41/bsd/skywalk/nexus/flowswitch/nx_flowswitch.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
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) 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 #if !XNU_TARGET_OS_OSX
87 #define NX_FSW_RXBATCH          64      /* default RX batch size */
88 #else /* XNU_TARGET_OS_OSX */
89 #define NX_FSW_RXBATCH          32      /* default RX batch size */
90 #endif /* XNU_TARGET_OS_OSX */
91 
92 #define NX_FSW_BUFSIZE          (2 * 1024)  /* default buffer size */
93 
94 #define NX_FSW_MINBUFSIZE       512  /* min buffer size */
95 #define NX_FSW_MAXBUFSIZE       (16 * 1024) /* max buffer size */
96 #define NX_FSW_MAXBUFFERS       (4 * 1024) /* max number of buffers */
97 /* max number of buffers for memory constrained device */
98 #define NX_FSW_MAXBUFFERS_MEM_CONSTRAINED (2 * 1024)
99 /* default user buffer segment size for non memory-constrained device */
100 #define NX_FSW_BUF_SEG_SIZE     (32 * 1024)
101 
102 /*
103  * TODO: [email protected] -- minimum buflets for now; we will need to
104  * have a way to adjust this based on the underlying interface's
105  * parameters, e.g. jumbo MTU, large segment offload, etc.
106  */
107 #define NX_FSW_UMD_SIZE _USER_PACKET_SIZE(BUFLETS_MIN)
108 #define NX_FSW_KMD_SIZE _KERN_PACKET_SIZE(BUFLETS_MIN)
109 
110 struct nx_flowswitch;
111 
112 /*
113  * Virtual port nexus adapter
114  */
115 struct nexus_vp_adapter {
116 	/*
117 	 * This is an overlay structure on nexus_adapter;
118 	 * make sure it contains 'up' as the first member.
119 	 */
120 	struct nexus_adapter vpna_up;
121 
122 	/*
123 	 * Flow switch support:
124 	 *
125 	 * If the adapter is associated with a nexus port, vpna_fsw points
126 	 * to the flow switch this NA is attached to; vpna_nx_port is the
127 	 * port number used in the flow switch.  Otherwise, vpna_fsw would
128 	 * be NULL and vpna_nx_port would be NEXUS_PORT_ANY.
129 	 */
130 	struct nx_flowswitch *vpna_fsw;
131 	nexus_port_t    vpna_nx_port;
132 	boolean_t       vpna_retry;
133 	boolean_t       vpna_pid_bound;
134 	boolean_t       vpna_defunct;
135 	pid_t           vpna_pid;
136 };
137 
138 #define VPNA(_na)       ((struct nexus_vp_adapter *)(_na))
139 
140 #define NEXUS_PROVIDER_FLOW_SWITCH      "com.apple.nexus.flowswitch"
141 
142 /* fsw_state_flags */
143 #define FSW_STATEF_QUIESCED             0x0001
144 #define FSW_STATEF_NETAGENT_ADDED       0x0002
145 #define FSW_STATEF_NETAGENT_ENABLED     0x0004
146 
147 #define FSW_QUIESCED(_fsw) \
148     (((_fsw)->fsw_state_flags & FSW_STATEF_QUIESCED) != 0)
149 
150 #define FSW_NETAGENT_ADDED(_fsw) \
151     (((_fsw)->fsw_state_flags & FSW_STATEF_NETAGENT_ADDED) != 0)
152 
153 #define FSW_NETAGENT_ENABLED(_fsw)                                      \
154     (((_fsw)->fsw_state_flags & FSW_STATEF_NETAGENT_ENABLED) != 0)
155 
156 /*
157  * nx_flowswitch is a descriptor for a flow switch instance.
158  * Interfaces for a flow switch are all in fsw_ports[].
159  * The array has fixed size, an empty entry does not terminate
160  * the search, but lookups only occur on attach/detach so we
161  * don't mind if they are slow.
162  *
163  * The flow switch is non blocking on the transmit ports: excess
164  * packets are dropped if there is no room on the output port.
165  *
166  * fsw_lock protects accesses to the fsw_ports array.
167  * This is a rw lock (or equivalent).
168  */
169 struct nx_flowswitch {
170 	decl_lck_rw_data(, fsw_lock);
171 	uint32_t                fsw_tx_rings;
172 	uint32_t                fsw_rx_rings;
173 
174 	struct kern_nexus       *fsw_nx;
175 
176 	/* packet type enqueued by the class queues */
177 	classq_pkt_type_t       fsw_classq_enq_ptype;
178 	boolean_t               fsw_classq_enabled;
179 
180 	/* packet copy routines */
181 	pkt_copy_from_pkt_t     *fsw_pkt_copy_from_pkt;
182 	pkt_copy_from_mbuf_t    *fsw_pkt_copy_from_mbuf;
183 	pkt_copy_to_mbuf_t      *fsw_pkt_copy_to_mbuf;
184 
185 	uint8_t                 fsw_frame_headroom;
186 	uint32_t                fsw_src_lla_gencnt;
187 	uint32_t                fsw_pending_nonviable;
188 	uint32_t                fsw_low_power_gencnt;
189 
190 	/* The following are protected by fsw_lock. */
191 	struct flow_mgr         *fsw_flow_mgr;
192 	netagent_session_t      fsw_agent_session;
193 	uuid_t                  fsw_agent_uuid;
194 	struct ifnet            *fsw_ifp;        /* host interface */
195 	struct nexus_adapter    *fsw_nifna;      /* netif adapter */
196 	uint32_t                fsw_state_flags; /* FSW_STATEF_* */
197 
198 	union {
199 		uint64_t _buf[1];
200 		uint8_t _eth_src[ETHER_ADDR_LEN];
201 	} __fsw_slladdr __attribute((aligned(sizeof(uint64_t))));
202 
203 #define fsw_slla                 __fsw_slladdr._buf
204 #define fsw_ether_shost          __fsw_slladdr._eth_src
205 
206 	int (*fsw_resolve)(struct nx_flowswitch *, struct flow_route *,
207 	    struct __kern_packet *);
208 	void (*fsw_frame)(struct nx_flowswitch *, struct flow_route *,
209 	    struct __kern_packet *);
210 	sa_family_t (*fsw_demux)(struct nx_flowswitch *,
211 	    struct __kern_packet *);
212 
213 	struct fsw_stats        fsw_stats;
214 
215 	/*
216 	 * The host interface attachment to the flowswitch (fsw_ifp), as well
217 	 * as the netagent registration, are guarded by the flowswitch's RW
218 	 * lock.  During fsw_flow_bind() time, we need to make sure they are
219 	 * valid before proceeding forward, but holding that RW lock across
220 	 * the routine is not possible since the thread may block if there
221 	 * are other threads performing fsw_flow_{bind,unbind} on the same
222 	 * flow owner bucket.  To prevent fsw_dtor() from happening while
223 	 * fsw_flow_bind() is in progress, we need to have it wait until all
224 	 * pending flow binds are done.  To do this we add a busy counter
225 	 * incremented at flow bind time, and use the lock for synchronization.
226 	 */
227 	decl_lck_mtx_data(, fsw_detach_barrier_lock);
228 	uint32_t                fsw_detach_flags;        /* see fsw_DETACHF_* */
229 	uint32_t                fsw_detach_barriers;
230 	uint32_t                fsw_detach_waiters;
231 
232 	u_int                   fsw_ifp_dlt;
233 
234 	void (*fsw_ctor)(struct nx_flowswitch *, struct flow_route *);
235 
236 	/* store stats from na that is going to be deactivated */
237 	struct __nx_stats_fsw   *fsw_closed_na_stats;
238 
239 	/* ip fragments manager */
240 	struct fsw_ip_frag_mgr  *fsw_ipfm;
241 
242 	struct skoid            fsw_skoid;
243 
244 	/* input network emulator */
245 	struct netem            *fsw_input_netem;
246 
247 	struct kern_channel     *fsw_dev_ch;
248 	struct kern_channel     *fsw_host_ch;
249 
250 	/*
251 	 * The reaper thread gets scheduled on-demand, whenever there
252 	 * is any lingering flow entry needing to be freed or becoming
253 	 * nonviable.  Access is protected by fsw_reap_lock.
254 	 */
255 	decl_lck_mtx_data(, fsw_reap_lock);
256 	uint32_t                fsw_reap_flags;  /* see fsw_REAPF_* */
257 	uint32_t                fsw_reap_requests;
258 	struct thread           *fsw_reap_thread;
259 	char                    fsw_reap_name[MAXTHREADNAMESIZE];
260 
261 	uint64_t                fsw_reap_last;
262 	uint64_t                fsw_drain_channel_chk_last;
263 	uint64_t                fsw_drain_netif_chk_last;
264 
265 	decl_lck_mtx_data(, fsw_linger_lock);
266 	struct flow_entry_linger_head fsw_linger_head;
267 	uint32_t                fsw_linger_cnt;
268 };
269 
270 #define NX_FSW_PRIVATE(_nx) ((struct nx_flowswitch *)(_nx)->nx_arg)
271 
272 #define FSW_RWINIT(_fsw)                \
273 	lck_rw_init(&(_fsw)->fsw_lock, &nexus_lock_group, &nexus_lock_attr)
274 #define FSW_WLOCK(_fsw)                 \
275 	lck_rw_lock_exclusive(&(_fsw)->fsw_lock)
276 #define FSW_WUNLOCK(_fsw)               \
277 	lck_rw_unlock_exclusive(&(_fsw)->fsw_lock)
278 #define FSW_WLOCKTORLOCK(_fsw)          \
279 	lck_rw_lock_exclusive_to_shared(&(_fsw)->fsw_lock)
280 #define FSW_RLOCK(_fsw)                 \
281 	lck_rw_lock_shared(&(_fsw)->fsw_lock)
282 #define FSW_RLOCKTOWLOCK(_fsw)          \
283 	lck_rw_lock_shared_to_exclusive(&(_fsw)->fsw_lock)
284 #define FSW_RTRYLOCK(_fsw)              \
285 	lck_rw_try_lock(&(_fsw)->fsw_lock, LCK_RW_TYPE_SHARED)
286 #define FSW_RUNLOCK(_fsw)               \
287 	lck_rw_unlock_shared(&(_fsw)->fsw_lock)
288 #define FSW_UNLOCK(_fsw)                \
289 	lck_rw_done(&(_fsw)->fsw_lock)
290 #define FSW_RWDESTROY(_fsw)             \
291 	lck_rw_destroy(&(_fsw)->fsw_lock, &nexus_lock_group)
292 #define FSW_WLOCK_ASSERT_HELD(_fsw)     \
293 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_EXCLUSIVE)
294 #define FSW_RLOCK_ASSERT_HELD(_fsw)     \
295 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_SHARED)
296 #define FSW_LOCK_ASSERT_HELD(_fsw)      \
297 	LCK_RW_ASSERT(&(_fsw)->fsw_lock, LCK_RW_ASSERT_HELD)
298 
299 extern struct nxdom nx_flowswitch_dom_s;
300 extern struct kern_nexus_domain_provider nx_fsw_prov_s;
301 
302 SYSCTL_DECL(_kern_skywalk_flowswitch);
303 
304 /* functions used by external modules to interface with flow switch */
305 __BEGIN_DECLS
306 extern int nx_fsw_na_find(struct kern_nexus *, struct kern_channel *,
307     struct chreq *, struct nxbind *, struct proc *, struct nexus_adapter **,
308     boolean_t);
309 extern boolean_t nx_fsw_dom_port_is_reserved(struct kern_nexus *nx,
310     nexus_port_t nx_port);
311 extern int nx_fsw_netagent_add(struct kern_nexus *nx);
312 extern int nx_fsw_netagent_remove(struct kern_nexus *nx);
313 extern void nx_fsw_netagent_update(struct kern_nexus *nx);
314 extern void fsw_devna_rx(struct nexus_adapter *, struct __kern_packet *,
315     struct nexus_pkt_stats *);
316 extern struct nx_flowswitch *fsw_ifp_to_fsw(struct ifnet *);
317 
318 __END_DECLS
319 #endif /* CONFIG_NEXUS_FLOWSWITCH */
320 #endif /* _SKYWALK_NEXUS_FLOWSWITCH_H_ */
321