xref: /illumos-gate/usr/src/uts/common/sys/mac_client_impl.h (revision 8a5251963032143e4d8c00bee444d848a79095cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
25  * Copyright 2025 Oxide Computer Company
26  */
27 /*
28  * Copyright 2018 Joyent, Inc.
29  */
30 
31 #ifndef	_SYS_MAC_CLIENT_IMPL_H
32 #define	_SYS_MAC_CLIENT_IMPL_H
33 
34 #include <sys/modhash.h>
35 #include <sys/mac_client.h>
36 #include <sys/mac_provider.h>
37 #include <sys/mac.h>
38 #include <sys/mac_impl.h>
39 #include <sys/mac_stat.h>
40 #include <net/if.h>
41 #include <sys/mac_flow_impl.h>
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 extern kmem_cache_t	*mac_client_impl_cache;
48 extern kmem_cache_t	*mac_unicast_impl_cache;
49 extern kmem_cache_t	*mac_promisc_impl_cache;
50 
51 /*
52  * Need a list to chain all VIDs assigned to a client. Normally, one
53  * MAC client only has one VID. But vsw might need multiple VIDs.
54  */
55 typedef struct mac_unicast_impl_s {			/* Protected by */
56 	struct mac_unicast_impl_s	*mui_next;	/* SL */
57 	mac_address_t			*mui_map;	/* SL */
58 	uint16_t			mui_vid;	/* SL */
59 } mac_unicast_impl_t;
60 
61 #define	MAC_CLIENT_FLAGS_PRIMARY		0x0001
62 #define	MAC_CLIENT_FLAGS_VNIC_PRIMARY		0x0002
63 #define	MAC_CLIENT_FLAGS_MULTI_PRIMARY		0x0004
64 #define	MAC_CLIENT_FLAGS_PASSIVE_PRIMARY	0x0008
65 
66 /*
67  * One of these is instantiated per MAC client promiscuous callback.
68  *
69  * Each element of this structure belongs to two linked list. One
70  * for the mac_client_impl_t (mci_promisc_list) which created allocated
71  * the callback, the other for the mac_impl_t (mi_promisc_list) corresponding
72  * to the MAC client.
73  * The former allows us to do bookkeeping, the latter allows us
74  * to more efficiently dispatch packets to the promiscuous callbacks.
75  */
76 typedef struct mac_promisc_impl_s {			/* Protected by */
77 	mac_cb_t			mpi_mci_link;	/* mi_promisc_lock */
78 	mac_cb_t			mpi_mi_link;	/* mi_promisc_lock */
79 	mac_client_promisc_type_t	mpi_type;	/* WO */
80 	mac_rx_t			mpi_fn;		/* WO */
81 	void				*mpi_arg;	/* WO */
82 	struct mac_client_impl_s	*mpi_mcip;	/* WO */
83 	boolean_t			mpi_no_tx_loop;	/* WO */
84 	boolean_t			mpi_no_phys;	/* WO */
85 	boolean_t			mpi_strip_vlan_tag;	/* WO */
86 	boolean_t			mpi_no_copy;	/* WO */
87 	boolean_t			mpi_rx_only;	/* WO */
88 	boolean_t			mpi_tx_only;	/* WO */
89 } mac_promisc_impl_t;
90 
91 typedef union mac_tx_percpu_s {
92 	struct {
93 		kmutex_t	_pcpu_tx_lock;
94 		uint_t		_pcpu_tx_refcnt;
95 	} pcpu_lr;
96 	uchar_t		pcpu_pad[64];
97 } mac_tx_percpu_t;
98 
99 #define	pcpu_tx_lock	pcpu_lr._pcpu_tx_lock
100 #define	pcpu_tx_refcnt	pcpu_lr._pcpu_tx_refcnt
101 
102 /*
103  * One of these is instantiated for each MAC client.
104  */
105 struct mac_client_impl_s {			/* Protected by */
106 	struct mac_client_impl_s *mci_client_next;	/* mi_rw_lock */
107 	char			mci_name[MAXNAMELEN];	/* mi_rw_lock */
108 	/*
109 	 * This flow entry will contain all the internal constructs
110 	 * such as SRS etc. for this MAC client. The MAC client may
111 	 * have more than one flow corresponding to each upper client
112 	 * sharing this mac_client_impl_t.
113 	 */
114 	flow_entry_t		*mci_flent;		/* mi_rw_lock */
115 	struct mac_impl_s	*mci_mip;		/* WO */
116 	/*
117 	 * If this is a client that has a pass thru MAC (e.g. a VNIC),
118 	 * then we also keep the handle for the client's upper MAC.
119 	 */
120 	struct mac_impl_s	*mci_upper_mip;		/* WO */
121 
122 	uint32_t		mci_state_flags;	/* WO */
123 	mac_rx_t		mci_rx_fn;		/* Rx Quiescence */
124 	void			*mci_rx_arg;		/* Rx Quiescence */
125 	mac_direct_rxs_t	mci_direct_rx;		/* SL */
126 	mac_rx_t		mci_rx_p_fn;		/* Rx Quiescence */
127 	void			*mci_rx_p_arg;		/* Rx Quiescence */
128 	void			*mci_p_unicast_list;
129 
130 	mac_cb_t		*mci_promisc_list;	/* mi_promisc_lock */
131 
132 	mac_address_t		*mci_unicast;
133 	uint32_t		mci_flags;		/* SL */
134 	krwlock_t		mci_rw_lock;
135 	mac_unicast_impl_t	*mci_unicast_list;	/* mci_rw_lock */
136 
137 	/*
138 	 * The mac_client_impl_t may be shared by multiple clients, i.e
139 	 * multiple VLANs sharing the same MAC client. In this case the
140 	 * address/vid tuples differ and are each associated with their
141 	 * own flow entry, but the rest underlying components SRS, etc,
142 	 * are common.
143 	 *
144 	 * This is only needed to support sun4v vsw. There are several
145 	 * places in MAC we could simplify the code if we removed
146 	 * sun4v support.
147 	 */
148 	flow_entry_t		*mci_flent_list;	/* mci_rw_lock */
149 	uint_t			mci_nflents;		/* mci_rw_lock */
150 	uint_t			mci_nvids;		/* mci_rw_lock */
151 	volatile uint32_t	mci_vidcache;		/* VID cache */
152 
153 	/* Resource Management Functions */
154 	mac_resource_add_t	mci_resource_add;	/* SL */
155 	mac_resource_remove_t	mci_resource_remove;	/* SL */
156 	mac_resource_quiesce_t	mci_resource_quiesce;	/* SL */
157 	mac_resource_restart_t	mci_resource_restart;	/* SL */
158 	mac_resource_bind_t	mci_resource_bind;	/* SL */
159 	void			*mci_resource_arg;	/* SL */
160 
161 
162 	/* Tx notify callback */
163 	kmutex_t		mci_tx_cb_lock;
164 	mac_cb_info_t		mci_tx_notify_cb_info;	/* cb list info */
165 	mac_cb_t		*mci_tx_notify_cb_list;	/* The cb list */
166 	uintptr_t		mci_tx_notify_id;
167 
168 	/* per MAC client stats */			/* None */
169 	mac_misc_stats_t	mci_misc_stat;
170 
171 	flow_tab_t		*mci_subflow_tab;	/* Rx quiescence */
172 
173 	/*
174 	 * Priority range for this MAC client. This the range
175 	 * corresponding to the priority configured (nr_flow_priority).
176 	 */
177 	pri_t			mci_min_pri;
178 	pri_t			mci_max_pri;
179 
180 	/*
181 	 * Hybrid I/O related definitions.
182 	 */
183 	mac_share_handle_t	mci_share;
184 
185 	/* for multicast support */
186 	struct mac_mcast_addrs_s *mci_mcast_addrs;	/* mi_rw_lock */
187 
188 	/*
189 	 * Mac protection related fields
190 	 */
191 	kmutex_t		mci_protect_lock;
192 	uint32_t		mci_protect_flags;	/* SL */
193 	in6_addr_t		mci_v6_mac_token;	/* SL */
194 	in6_addr_t		mci_v6_local_addr;	/* SL */
195 	avl_tree_t		mci_v4_pending_txn;	/* mci_protect_lock */
196 	avl_tree_t		mci_v4_completed_txn;	/* mci_protect_lock */
197 	avl_tree_t		mci_v4_dyn_ip;		/* mci_protect_lock */
198 	avl_tree_t		mci_v6_pending_txn;	/* mci_protect_lock */
199 	avl_tree_t		mci_v6_cid;		/* mci_protect_lock */
200 	avl_tree_t		mci_v6_dyn_ip;		/* mci_protect_lock */
201 	avl_tree_t		mci_v6_slaac_ip;	/* mci_protect_lock */
202 	timeout_id_t		mci_txn_cleanup_tid;	/* mci_protect_lock */
203 
204 	/*
205 	 * Protected by mci_tx_pcpu[0].pcpu_tx_lock
206 	 */
207 	uint_t			mci_tx_flag;
208 	kcondvar_t		mci_tx_cv;
209 
210 	/* Must be last in the structure for dynamic sizing */
211 	mac_tx_percpu_t		mci_tx_pcpu[1];		/* SL */
212 };
213 
214 #define	MAC_CLIENT_IMPL_SIZE						\
215 	(sizeof (mac_client_impl_t) +					\
216 	    (mac_tx_percpu_cnt * sizeof (mac_tx_percpu_t)))
217 
218 extern	int	mac_tx_percpu_cnt;
219 
220 #define	MCIP_TX_SRS(mcip)	\
221 	((mcip)->mci_flent == NULL ? NULL : (mcip)->mci_flent->fe_tx_srs)
222 
223 /* Defensive coding, non-null mcip_flent could be an assert */
224 
225 #define	MCIP_DATAPATH_SETUP(mcip)		\
226 	((mcip)->mci_flent == NULL ? B_FALSE :	\
227 	!((mcip)->mci_flent->fe_flags & FE_MC_NO_DATAPATH))
228 
229 #define	MCIP_RESOURCE_PROPS(mcip)		\
230 	((mcip)->mci_flent == NULL ? NULL :	\
231 	&(mcip)->mci_flent->fe_resource_props)
232 
233 #define	MCIP_EFFECTIVE_PROPS(mcip)		\
234 	(mcip->mci_flent == NULL ? NULL :	\
235 	&(mcip)->mci_flent->fe_effective_props)
236 
237 #define	MCIP_RESOURCE_PROPS_MASK(mcip)		\
238 	((mcip)->mci_flent == NULL ? 0 :	\
239 	(mcip)->mci_flent->fe_resource_props.mrp_mask)
240 
241 #define	MCIP_RESOURCE_PROPS_MAXBW(mcip)		\
242 	((mcip)->mci_flent == NULL ? 0 :	\
243 	(mcip)->mci_flent->fe_resource_props.mrp_maxbw)
244 
245 #define	MCIP_RESOURCE_PROPS_PRIORITY(mcip)		\
246 	((mcip)->mci_flent == NULL ? 0 :	\
247 	(mcip)->mci_flent->fe_resource_props.mrp_priority)
248 
249 #define	MCIP_RESOURCE_PROPS_CPUS(mcip)		\
250 	((mcip)->mci_flent == NULL ? 0 :	\
251 	&(mcip)->mci_flent->fe_resource_props.mrp_cpus)
252 
253 #define	MCIP_RESOURCE_PROPS_NCPUS(mcip)		\
254 	((mcip)->mci_flent == NULL ? 0 :	\
255 	(mcip)->mci_flent->fe_resource_props.mrp_ncpus)
256 
257 #define	MCIP_RESOURCE_PROPS_CPU(mcip)		\
258 	((mcip)->mci_flent == NULL ? 0 :	\
259 	(mcip)->mci_flent->fe_resource_props.mrp_ncpu)
260 
261 /*
262  * We validate the VLAN id of the packet w.r.t the client's vid,
263  * if required (i.e. !MCIS_DISABLE_TX_VID_CHECK). DLS clients
264  * will have MCIS_DISABLE_TX_VID_CHECK set.
265  * (In the case of aggr when we get back packets, due to
266  * the underlying driver being flow controlled, we won't
267  * drop the packet even if it is VLAN tagged as we
268  * don't set MCIS_DISABLE_TX_VID_CHECK for an aggr.)
269  */
270 #define	MAC_VID_CHECK_NEEDED(mcip)					\
271 	(((mcip)->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0 &&	\
272 	(mcip)->mci_mip->mi_info.mi_nativemedia == DL_ETHER)
273 
274 #define	MAC_VID_CHECK(mcip, mp, err) {					\
275 	if (ntohs(((struct ether_header *)(mp)->b_rptr)->ether_type) ==	\
276 	    ETHERTYPE_VLAN) {						\
277 		/*							\
278 		 * err is set to EINVAL (so the caller can take the	\
279 		 * appropriate action. e.g. freemsg()) for two cases:	\
280 		 * -client is not responsible for filling in the vid.	\
281 		 * -client is responsible for filling in the vid, but	\
282 		 *  the vid doesn't match the vid of the MAC client.	\
283 		 */							\
284 		(err) = EINVAL;						\
285 		if (((mcip)->mci_state_flags & MCIS_TAG_DISABLE) != 0) {\
286 			struct ether_vlan_header	*evhp;		\
287 			uint16_t			vlanid;		\
288 									\
289 			evhp = (struct ether_vlan_header *)(mp)->b_rptr;\
290 			vlanid = VLAN_ID(ntohs(evhp->ether_tci));	\
291 			if (mac_client_check_flow_vid((mcip), vlanid))	\
292 				(err) = 0;				\
293 		}							\
294 	}								\
295 }
296 
297 /*
298  * To allow the hot path to not grab any additional locks, we keep a single
299  * entry VLAN ID cache that caches whether or not a given VID belongs to a
300  * MAC client.
301  */
302 #define	MCIP_VIDCACHE_VALIDSHIFT	31
303 #define	MCIP_VIDCACHE_VIDSHIFT		1
304 #define	MCIP_VIDCACHE_VIDMASK		(UINT16_MAX << MCIP_VIDCACHE_VIDSHIFT)
305 #define	MCIP_VIDCACHE_BOOLSHIFT		0
306 
307 #define	MCIP_VIDCACHE_INVALID		0
308 
309 #define	MCIP_VIDCACHE_CACHE(vid, bool)	\
310 	((1U << MCIP_VIDCACHE_VALIDSHIFT) | \
311 	((vid) << MCIP_VIDCACHE_VIDSHIFT) | \
312 	((bool) ? (1U << MCIP_VIDCACHE_BOOLSHIFT) : 0))
313 
314 #define	MCIP_VIDCACHE_ISVALID(v)	((v) & (1U << MCIP_VIDCACHE_VALIDSHIFT))
315 #define	MCIP_VIDCACHE_VID(v)		\
316 	(((v) & MCIP_VIDCACHE_VIDMASK) >> MCIP_VIDCACHE_VIDSHIFT)
317 #define	MCIP_VIDCACHE_BOOL(v)		((v) & (1U << MCIP_VIDCACHE_BOOLSHIFT))
318 
319 #define	MAC_TAG_NEEDED(mcip)						\
320 	(((mcip)->mci_state_flags & MCIS_TAG_DISABLE) == 0 &&		\
321 	(mcip)->mci_nvids == 1)						\
322 
323 /*
324  * MAC Client Implementation State (mci_state_flags)
325  *
326  * MCIS_IS_VNIC
327  *
328  *	The client is a VNIC.
329  *
330  * MCIS_EXCLUSIVE
331  *
332  *	The client has exclusive control over the MAC, such that it is
333  *	the sole client of the MAC.
334  *
335  * MCIS_TAG_DISABLE
336  *
337  *	MAC will not add VLAN tags to outgoing traffic. If this flag
338  *	is set it is up to the client to add the correct VLAN tag.
339  *
340  * MCIS_STRIP_DISABLE
341  *
342  *	MAC will not strip the VLAN tags on incoming traffic before
343  *	passing it to mci_rx_fn. This only applies to non-bypass
344  *	traffic.
345  *
346  * MCIS_IS_AGGR_PORT
347  *
348  *	The client represents a port on an aggr.
349  *
350  * MCIS_CLIENT_POLL_CAPABLE
351  *
352  *	The client is capable of polling the Rx TCP/UDP softrings.
353  *
354  * MCIS_DESC_LOGGED
355  *
356  *	This flag is set when the client's link info has been logged
357  *	by the mac_log_linkinfo() timer. This ensures that the
358  *	client's link info is only logged once.
359  *
360  * MCIS_SHARE_BOUND
361  *
362  *	This client has an HIO share bound to it.
363  *
364  * MCIS_DISABLE_TX_VID_CHECK
365  *
366  *	MAC will not check the VID of the client's Tx traffic.
367  *
368  * MCIS_USE_DATALINK_NAME
369  *
370  *	The client is using the same name as its underlying MAC. This
371  *	happens when dlmgmtd is unreachable during client creation.
372  *
373  * MCIS_UNICAST_HW
374  *
375  *	The client requires MAC address hardware classification. This
376  *	is only used by sun4v vsw.
377  *
378  * MCIS_IS_AGGR_CLIENT
379  *
380  *	The client sits atop an aggr.
381  *
382  * MCIS_RX_BYPASS_DISABLE
383  *
384  *	Do not allow the client to enable DLS bypass.
385  *
386  * MCIS_NO_UNICAST_ADDR
387  *
388  *	This client has no MAC unicast addresss associated with it.
389  *
390  */
391 /* MCI state flags */
392 #define	MCIS_IS_VNIC			0x0001
393 #define	MCIS_EXCLUSIVE			0x0002
394 #define	MCIS_TAG_DISABLE		0x0004
395 #define	MCIS_STRIP_DISABLE		0x0008
396 #define	MCIS_IS_AGGR_PORT		0x0010
397 #define	MCIS_CLIENT_POLL_CAPABLE	0x0020
398 #define	MCIS_DESC_LOGGED		0x0040
399 #define	MCIS_SHARE_BOUND		0x0080
400 #define	MCIS_DISABLE_TX_VID_CHECK	0x0100
401 #define	MCIS_USE_DATALINK_NAME		0x0200
402 #define	MCIS_UNICAST_HW			0x0400
403 #define	MCIS_IS_AGGR_CLIENT		0x0800
404 #define	MCIS_RX_BYPASS_DISABLE		0x1000
405 #define	MCIS_NO_UNICAST_ADDR		0x2000
406 
407 /* Mac protection flags */
408 #define	MPT_FLAG_V6_LOCAL_ADDR_SET	0x0001
409 #define	MPT_FLAG_PROMISC_FILTERED	0x0002
410 
411 /* in mac_client.c */
412 extern void mac_promisc_client_dispatch(mac_client_impl_t *, mblk_t *);
413 extern void mac_client_init(void);
414 extern void mac_client_fini(void);
415 extern void mac_promisc_dispatch(mac_impl_t *, mblk_t *, mac_client_impl_t *,
416     boolean_t);
417 
418 extern int mac_validate_props(mac_impl_t *, mac_resource_props_t *);
419 
420 extern mac_client_impl_t *mac_vnic_lower(mac_impl_t *);
421 extern mac_client_impl_t *mac_primary_client_handle(mac_impl_t *);
422 extern uint16_t i_mac_flow_vid(flow_entry_t *);
423 extern boolean_t i_mac_capab_get(mac_handle_t, mac_capab_t, void *);
424 
425 extern void mac_unicast_update_clients(mac_impl_t *, mac_address_t *);
426 extern void mac_update_resources(mac_resource_props_t *,
427     mac_resource_props_t *, boolean_t);
428 
429 boolean_t mac_client_check_flow_vid(mac_client_impl_t *, uint16_t);
430 
431 extern boolean_t mac_is_primary_client(mac_client_impl_t *);
432 
433 extern int mac_client_set_rings_prop(mac_client_impl_t *,
434     mac_resource_props_t *, mac_resource_props_t *);
435 extern void mac_set_prim_vlan_rings(mac_impl_t *, mac_resource_props_t *);
436 
437 #ifdef	__cplusplus
438 }
439 #endif
440 
441 #endif	/* _SYS_MAC_CLIENT_IMPL_H */
442