xref: /illumos-gate/usr/src/uts/common/sys/mac_client_impl.h (revision 2576e7a56bb1b296053722f3ebc688cef754350f)
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 2026 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 	/*
154 	 * Resource Management Callback Functions
155 	 *
156 	 * A mac client may have both an IPv4 and IPv6 ill_t active on it. In
157 	 * order to avoid stomping on each other we give each their own resource
158 	 * callbacks. At this time resources are used solely by TCP softrings
159 	 * for the purpose of IP ring/squeue creation and polling. Currently the
160 	 * callbacks are identical across protocol types, save the mrc_arg,
161 	 * which is used to pass the ill_t up to IP.
162 	 */
163 	mac_resource_cb_t	mci_rcb4;	/* SL */
164 	mac_resource_cb_t	mci_rcb6;	/* SL */
165 
166 	/* Tx notify callback */
167 	kmutex_t		mci_tx_cb_lock;
168 	mac_cb_info_t		mci_tx_notify_cb_info;	/* cb list info */
169 	mac_cb_t		*mci_tx_notify_cb_list;	/* The cb list */
170 	uintptr_t		mci_tx_notify_id;
171 
172 	/* per MAC client stats */			/* None */
173 	mac_misc_stats_t	mci_misc_stat;
174 
175 	flow_tab_t		*mci_subflow_tab;	/* Rx quiescence */
176 
177 	/*
178 	 * Priority range for this MAC client. This the range
179 	 * corresponding to the priority configured (nr_flow_priority).
180 	 */
181 	pri_t			mci_min_pri;
182 	pri_t			mci_max_pri;
183 
184 	/*
185 	 * Hybrid I/O related definitions.
186 	 */
187 	mac_share_handle_t	mci_share;
188 
189 	/* for multicast support */
190 	struct mac_mcast_addrs_s *mci_mcast_addrs;	/* mi_rw_lock */
191 
192 	/*
193 	 * Mac protection related fields
194 	 */
195 	kmutex_t		mci_protect_lock;
196 	uint32_t		mci_protect_flags;	/* SL */
197 	in6_addr_t		mci_v6_mac_token;	/* SL */
198 	in6_addr_t		mci_v6_local_addr;	/* SL */
199 	avl_tree_t		mci_v4_pending_txn;	/* mci_protect_lock */
200 	avl_tree_t		mci_v4_completed_txn;	/* mci_protect_lock */
201 	avl_tree_t		mci_v4_dyn_ip;		/* mci_protect_lock */
202 	avl_tree_t		mci_v6_pending_txn;	/* mci_protect_lock */
203 	avl_tree_t		mci_v6_cid;		/* mci_protect_lock */
204 	avl_tree_t		mci_v6_dyn_ip;		/* mci_protect_lock */
205 	avl_tree_t		mci_v6_slaac_ip;	/* mci_protect_lock */
206 	timeout_id_t		mci_txn_cleanup_tid;	/* mci_protect_lock */
207 
208 	/*
209 	 * Protected by mci_tx_pcpu[0].pcpu_tx_lock
210 	 */
211 	uint_t			mci_tx_flag;
212 	kcondvar_t		mci_tx_cv;
213 
214 	/* Must be last in the structure for dynamic sizing */
215 	mac_tx_percpu_t		mci_tx_pcpu[1];		/* SL */
216 };
217 
218 #define	MAC_CLIENT_IMPL_SIZE						\
219 	(sizeof (mac_client_impl_t) +					\
220 	    (mac_tx_percpu_cnt * sizeof (mac_tx_percpu_t)))
221 
222 extern	int	mac_tx_percpu_cnt;
223 
224 #define	MCIP_TX_SRS(mcip)	\
225 	((mcip)->mci_flent == NULL ? NULL : (mcip)->mci_flent->fe_tx_srs)
226 
227 /* Defensive coding, non-null mcip_flent could be an assert */
228 
229 #define	MCIP_DATAPATH_SETUP(mcip)		\
230 	((mcip)->mci_flent == NULL ? B_FALSE :	\
231 	!((mcip)->mci_flent->fe_flags & FE_MC_NO_DATAPATH))
232 
233 #define	MCIP_RESOURCE_PROPS(mcip)		\
234 	((mcip)->mci_flent == NULL ? NULL :	\
235 	&(mcip)->mci_flent->fe_resource_props)
236 
237 #define	MCIP_EFFECTIVE_PROPS(mcip)		\
238 	(mcip->mci_flent == NULL ? NULL :	\
239 	&(mcip)->mci_flent->fe_effective_props)
240 
241 #define	MCIP_RESOURCE_PROPS_MASK(mcip)		\
242 	((mcip)->mci_flent == NULL ? 0 :	\
243 	(mcip)->mci_flent->fe_resource_props.mrp_mask)
244 
245 #define	MCIP_RESOURCE_PROPS_MAXBW(mcip)		\
246 	((mcip)->mci_flent == NULL ? 0 :	\
247 	(mcip)->mci_flent->fe_resource_props.mrp_maxbw)
248 
249 #define	MCIP_RESOURCE_PROPS_PRIORITY(mcip)		\
250 	((mcip)->mci_flent == NULL ? 0 :	\
251 	(mcip)->mci_flent->fe_resource_props.mrp_priority)
252 
253 #define	MCIP_RESOURCE_PROPS_CPUS(mcip)		\
254 	((mcip)->mci_flent == NULL ? 0 :	\
255 	&(mcip)->mci_flent->fe_resource_props.mrp_cpus)
256 
257 #define	MCIP_RESOURCE_PROPS_NCPUS(mcip)		\
258 	((mcip)->mci_flent == NULL ? 0 :	\
259 	(mcip)->mci_flent->fe_resource_props.mrp_ncpus)
260 
261 #define	MCIP_RESOURCE_PROPS_CPU(mcip)		\
262 	((mcip)->mci_flent == NULL ? 0 :	\
263 	(mcip)->mci_flent->fe_resource_props.mrp_ncpu)
264 
265 /*
266  * We validate the VLAN id of the packet w.r.t the client's vid,
267  * if required (i.e. !MCIS_DISABLE_TX_VID_CHECK). DLS clients
268  * will have MCIS_DISABLE_TX_VID_CHECK set.
269  * (In the case of aggr when we get back packets, due to
270  * the underlying driver being flow controlled, we won't
271  * drop the packet even if it is VLAN tagged as we
272  * don't set MCIS_DISABLE_TX_VID_CHECK for an aggr.)
273  */
274 #define	MAC_VID_CHECK_NEEDED(mcip)					\
275 	(((mcip)->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0 &&	\
276 	(mcip)->mci_mip->mi_info.mi_nativemedia == DL_ETHER)
277 
278 #define	MAC_VID_CHECK(mcip, mp, err) {					\
279 	if (ntohs(((struct ether_header *)(mp)->b_rptr)->ether_type) ==	\
280 	    ETHERTYPE_VLAN) {						\
281 		/*							\
282 		 * err is set to EINVAL (so the caller can take the	\
283 		 * appropriate action. e.g. freemsg()) for two cases:	\
284 		 * -client is not responsible for filling in the vid.	\
285 		 * -client is responsible for filling in the vid, but	\
286 		 *  the vid doesn't match the vid of the MAC client.	\
287 		 */							\
288 		(err) = EINVAL;						\
289 		if (((mcip)->mci_state_flags & MCIS_TAG_DISABLE) != 0) {\
290 			struct ether_vlan_header	*evhp;		\
291 			uint16_t			vlanid;		\
292 									\
293 			evhp = (struct ether_vlan_header *)(mp)->b_rptr;\
294 			vlanid = VLAN_ID(ntohs(evhp->ether_tci));	\
295 			if (mac_client_check_flow_vid((mcip), vlanid))	\
296 				(err) = 0;				\
297 		}							\
298 	}								\
299 }
300 
301 /*
302  * To allow the hot path to not grab any additional locks, we keep a single
303  * entry VLAN ID cache that caches whether or not a given VID belongs to a
304  * MAC client.
305  */
306 #define	MCIP_VIDCACHE_VALIDSHIFT	31
307 #define	MCIP_VIDCACHE_VIDSHIFT		1
308 #define	MCIP_VIDCACHE_VIDMASK		(UINT16_MAX << MCIP_VIDCACHE_VIDSHIFT)
309 #define	MCIP_VIDCACHE_BOOLSHIFT		0
310 
311 #define	MCIP_VIDCACHE_INVALID		0
312 
313 #define	MCIP_VIDCACHE_CACHE(vid, bool)	\
314 	((1U << MCIP_VIDCACHE_VALIDSHIFT) | \
315 	((vid) << MCIP_VIDCACHE_VIDSHIFT) | \
316 	((bool) ? (1U << MCIP_VIDCACHE_BOOLSHIFT) : 0))
317 
318 #define	MCIP_VIDCACHE_ISVALID(v)	((v) & (1U << MCIP_VIDCACHE_VALIDSHIFT))
319 #define	MCIP_VIDCACHE_VID(v)		\
320 	(((v) & MCIP_VIDCACHE_VIDMASK) >> MCIP_VIDCACHE_VIDSHIFT)
321 #define	MCIP_VIDCACHE_BOOL(v)		((v) & (1U << MCIP_VIDCACHE_BOOLSHIFT))
322 
323 #define	MAC_TAG_NEEDED(mcip)						\
324 	(((mcip)->mci_state_flags & MCIS_TAG_DISABLE) == 0 &&		\
325 	(mcip)->mci_nvids == 1)						\
326 
327 /*
328  * MAC Client Implementation State (mci_state_flags)
329  *
330  * MCIS_IS_VNIC
331  *
332  *	The client is a VNIC.
333  *
334  * MCIS_EXCLUSIVE
335  *
336  *	The client has exclusive control over the MAC, such that it is
337  *	the sole client of the MAC.
338  *
339  * MCIS_TAG_DISABLE
340  *
341  *	MAC will not add VLAN tags to outgoing traffic. If this flag
342  *	is set it is up to the client to add the correct VLAN tag.
343  *
344  * MCIS_STRIP_DISABLE
345  *
346  *	MAC will not strip the VLAN tags on incoming traffic before
347  *	passing it to mci_rx_fn. This only applies to non-bypass
348  *	traffic.
349  *
350  * MCIS_IS_AGGR_PORT
351  *
352  *	The client represents a port on an aggr.
353  *
354  * MCIS_CLIENT_POLL_CAPABLE
355  *
356  *	The client is capable of polling the Rx TCP/UDP softrings.
357  *
358  * MCIS_DESC_LOGGED
359  *
360  *	This flag is set when the client's link info has been logged
361  *	by the mac_log_linkinfo() timer. This ensures that the
362  *	client's link info is only logged once.
363  *
364  * MCIS_SHARE_BOUND
365  *
366  *	This client has an HIO share bound to it.
367  *
368  * MCIS_DISABLE_TX_VID_CHECK
369  *
370  *	MAC will not check the VID of the client's Tx traffic.
371  *
372  * MCIS_USE_DATALINK_NAME
373  *
374  *	The client is using the same name as its underlying MAC. This
375  *	happens when dlmgmtd is unreachable during client creation.
376  *
377  * MCIS_UNICAST_HW
378  *
379  *	The client requires MAC address hardware classification. This
380  *	is only used by sun4v vsw.
381  *
382  * MCIS_IS_AGGR_CLIENT
383  *
384  *	The client sits atop an aggr.
385  *
386  * MCIS_RX_BYPASS_DISABLE
387  *
388  *	Do not allow the client to enable DLS bypass.
389  *
390  * MCIS_NO_UNICAST_ADDR
391  *
392  *	This client has no MAC unicast addresss associated with it.
393  *
394  */
395 /* MCI state flags */
396 #define	MCIS_IS_VNIC			0x0001
397 #define	MCIS_EXCLUSIVE			0x0002
398 #define	MCIS_TAG_DISABLE		0x0004
399 #define	MCIS_STRIP_DISABLE		0x0008
400 #define	MCIS_IS_AGGR_PORT		0x0010
401 #define	MCIS_CLIENT_POLL_CAPABLE	0x0020
402 #define	MCIS_DESC_LOGGED		0x0040
403 #define	MCIS_SHARE_BOUND		0x0080
404 #define	MCIS_DISABLE_TX_VID_CHECK	0x0100
405 #define	MCIS_USE_DATALINK_NAME		0x0200
406 #define	MCIS_UNICAST_HW			0x0400
407 #define	MCIS_IS_AGGR_CLIENT		0x0800
408 #define	MCIS_RX_BYPASS_DISABLE		0x1000
409 #define	MCIS_NO_UNICAST_ADDR		0x2000
410 
411 /* Mac protection flags */
412 #define	MPT_FLAG_V6_LOCAL_ADDR_SET	0x0001
413 #define	MPT_FLAG_PROMISC_FILTERED	0x0002
414 
415 /* in mac_client.c */
416 extern void mac_promisc_client_dispatch(mac_client_impl_t *, mblk_t *);
417 extern void mac_client_init(void);
418 extern void mac_client_fini(void);
419 extern void mac_promisc_dispatch(mac_impl_t *, mblk_t *, mac_client_impl_t *,
420     boolean_t);
421 
422 extern int mac_validate_props(mac_impl_t *, mac_resource_props_t *);
423 
424 extern mac_client_impl_t *mac_vnic_lower(mac_impl_t *);
425 extern mac_client_impl_t *mac_primary_client_handle(mac_impl_t *);
426 extern uint16_t i_mac_flow_vid(flow_entry_t *);
427 extern boolean_t i_mac_capab_get(mac_handle_t, mac_capab_t, void *);
428 
429 extern void mac_unicast_update_clients(mac_impl_t *, mac_address_t *);
430 extern void mac_update_resources(mac_resource_props_t *,
431     mac_resource_props_t *, boolean_t);
432 
433 boolean_t mac_client_check_flow_vid(mac_client_impl_t *, uint16_t);
434 
435 extern boolean_t mac_is_primary_client(mac_client_impl_t *);
436 
437 extern int mac_client_set_rings_prop(mac_client_impl_t *,
438     mac_resource_props_t *, mac_resource_props_t *);
439 extern void mac_set_prim_vlan_rings(mac_impl_t *, mac_resource_props_t *);
440 
441 #ifdef	__cplusplus
442 }
443 #endif
444 
445 #endif	/* _SYS_MAC_CLIENT_IMPL_H */
446