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