xref: /illumos-gate/usr/src/uts/common/sys/mac_client_impl.h (revision 5626beece2e5dedec7197ecf325cfaa1854a6c2e)
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  */
25 /*
26  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
27  */
28 
29 #ifndef	_SYS_MAC_CLIENT_IMPL_H
30 #define	_SYS_MAC_CLIENT_IMPL_H
31 
32 #include <sys/modhash.h>
33 #include <sys/mac_client.h>
34 #include <sys/mac_provider.h>
35 #include <sys/mac.h>
36 #include <sys/mac_impl.h>
37 #include <sys/mac_stat.h>
38 #include <net/if.h>
39 #include <sys/mac_flow_impl.h>
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 extern kmem_cache_t	*mac_client_impl_cache;
46 extern kmem_cache_t	*mac_unicast_impl_cache;
47 extern kmem_cache_t	*mac_promisc_impl_cache;
48 
49 /*
50  * Need a list to chain all VIDs assigned to a client. Normally, one
51  * MAC client only has one VID. But vsw might need multiple VIDs.
52  */
53 typedef struct mac_unicast_impl_s {			/* Protected by */
54 	struct mac_unicast_impl_s	*mui_next;	/* SL */
55 	mac_address_t			*mui_map;	/* SL */
56 	uint16_t			mui_vid;	/* SL */
57 } mac_unicast_impl_t;
58 
59 #define	MAC_CLIENT_FLAGS_PRIMARY		0X0001
60 #define	MAC_CLIENT_FLAGS_VNIC_PRIMARY		0x0002
61 #define	MAC_CLIENT_FLAGS_MULTI_PRIMARY		0x0004
62 #define	MAC_CLIENT_FLAGS_PASSIVE_PRIMARY	0x0008
63 
64 /*
65  * One of these is instantiated per MAC client promiscuous callback.
66  *
67  * Each element of this structure belongs to two linked list. One
68  * for the mac_client_impl_t (mci_promisc_list) which created allocated
69  * the callback, the other for the mac_impl_t (mi_promisc_list) corresponding
70  * to the MAC client.
71  * The former allows us to do bookkeeping, the latter allows us
72  * to more efficiently dispatch packets to the promiscuous callbacks.
73  */
74 typedef struct mac_promisc_impl_s {			/* Protected by */
75 	mac_cb_t			mpi_mci_link;	/* mi_promisc_lock */
76 	mac_cb_t			mpi_mi_link;	/* mi_promisc_lock */
77 	mac_client_promisc_type_t	mpi_type;	/* WO */
78 	mac_rx_t			mpi_fn;		/* WO */
79 	void				*mpi_arg;	/* WO */
80 	struct mac_client_impl_s	*mpi_mcip;	/* WO */
81 	boolean_t			mpi_no_tx_loop;	/* WO */
82 	boolean_t			mpi_no_phys;	/* WO */
83 	boolean_t			mpi_strip_vlan_tag;	/* WO */
84 	boolean_t			mpi_no_copy;	/* WO */
85 } mac_promisc_impl_t;
86 
87 typedef union mac_tx_percpu_s {
88 	struct {
89 		kmutex_t	_pcpu_tx_lock;
90 		uint_t		_pcpu_tx_refcnt;
91 	} pcpu_lr;
92 	uchar_t		pcpu_pad[64];
93 } mac_tx_percpu_t;
94 
95 #define	pcpu_tx_lock	pcpu_lr._pcpu_tx_lock
96 #define	pcpu_tx_refcnt	pcpu_lr._pcpu_tx_refcnt
97 
98 /*
99  * One of these is instantiated for each MAC client.
100  */
101 struct mac_client_impl_s {			/* Protected by */
102 	struct mac_client_impl_s *mci_client_next;	/* mi_rw_lock */
103 	char			mci_name[MAXNAMELEN];	/* mi_rw_lock */
104 	/*
105 	 * This flow entry will contain all the internal constructs
106 	 * such as SRS etc. for this MAC client. The MAC client may
107 	 * have more than one flow corresponding to each upper client
108 	 * sharing this mac_client_impl_t.
109 	 */
110 	flow_entry_t		*mci_flent;		/* mi_rw_lock */
111 	struct mac_impl_s	*mci_mip;		/* WO */
112 	/*
113 	 * If this is a client that has a pass thru MAC (e.g. a VNIC),
114 	 * then we also keep the handle for the client's upper MAC.
115 	 */
116 	struct mac_impl_s	*mci_upper_mip;		/* WO */
117 
118 	uint32_t		mci_state_flags;	/* WO */
119 	mac_rx_t		mci_rx_fn;		/* Rx Quiescence */
120 	void			*mci_rx_arg;		/* Rx Quiescence */
121 	mac_direct_rx_t		mci_direct_rx_fn;	/* SL */
122 	void			*mci_direct_rx_arg;	/* SL */
123 	mac_rx_t		mci_rx_p_fn;		/* Rx Quiescence */
124 	void			*mci_rx_p_arg;		/* Rx Quiescence */
125 	void			*mci_p_unicast_list;
126 
127 	mac_cb_t		*mci_promisc_list;	/* mi_promisc_lock */
128 
129 	mac_address_t		*mci_unicast;
130 	uint32_t		mci_flags;		/* SL */
131 	krwlock_t		mci_rw_lock;
132 	mac_unicast_impl_t	*mci_unicast_list;	/* mci_rw_lock */
133 	/*
134 	 * The mac_client_impl_t may be shared by multiple clients, i.e
135 	 * multiple VLANs sharing the same MAC client. In this case the
136 	 * address/vid tubles differ and are each associated with their
137 	 * own flow entry, but the rest underlying components SRS, etc,
138 	 * are common.
139 	 */
140 	flow_entry_t		*mci_flent_list;	/* mci_rw_lock */
141 	uint_t			mci_nflents;		/* mci_rw_lock */
142 	uint_t			mci_nvids;		/* mci_rw_lock */
143 	volatile uint32_t	mci_vidcache;		/* VID cache */
144 
145 	/* Resource Management Functions */
146 	mac_resource_add_t	mci_resource_add;	/* SL */
147 	mac_resource_remove_t	mci_resource_remove;	/* SL */
148 	mac_resource_quiesce_t	mci_resource_quiesce;	/* SL */
149 	mac_resource_restart_t	mci_resource_restart;	/* SL */
150 	mac_resource_bind_t	mci_resource_bind;	/* SL */
151 	void			*mci_resource_arg;	/* SL */
152 
153 
154 	/* Tx notify callback */
155 	kmutex_t		mci_tx_cb_lock;
156 	mac_cb_info_t		mci_tx_notify_cb_info;	/* cb list info */
157 	mac_cb_t		*mci_tx_notify_cb_list;	/* The cb list */
158 	uintptr_t		mci_tx_notify_id;
159 
160 	/* per MAC client stats */			/* None */
161 	mac_misc_stats_t	mci_misc_stat;
162 
163 	flow_tab_t		*mci_subflow_tab;	/* Rx quiescence */
164 
165 	/*
166 	 * Priority range for this MAC client. This the range
167 	 * corresponding to the priority configured (nr_flow_priority).
168 	 */
169 	pri_t			mci_min_pri;
170 	pri_t			mci_max_pri;
171 
172 	/*
173 	 * Hybrid I/O related definitions.
174 	 */
175 	mac_share_handle_t	mci_share;
176 
177 	/* for multicast support */
178 	struct mac_mcast_addrs_s *mci_mcast_addrs;	/* mi_rw_lock */
179 
180 	/*
181 	 * Mac protection related fields
182 	 */
183 	kmutex_t		mci_protect_lock;
184 	uint32_t		mci_protect_flags;	/* SL */
185 	in6_addr_t		mci_v6_mac_token;	/* SL */
186 	in6_addr_t		mci_v6_local_addr;	/* SL */
187 	avl_tree_t		mci_v4_pending_txn;	/* mci_protect_lock */
188 	avl_tree_t		mci_v4_completed_txn;	/* mci_protect_lock */
189 	avl_tree_t		mci_v4_dyn_ip;		/* mci_protect_lock */
190 	avl_tree_t		mci_v6_pending_txn;	/* mci_protect_lock */
191 	avl_tree_t		mci_v6_cid;		/* mci_protect_lock */
192 	avl_tree_t		mci_v6_dyn_ip;		/* mci_protect_lock */
193 	avl_tree_t		mci_v6_slaac_ip;	/* mci_protect_lock */
194 	timeout_id_t		mci_txn_cleanup_tid;	/* mci_protect_lock */
195 
196 	/*
197 	 * Protected by mci_tx_pcpu[0].pcpu_tx_lock
198 	 */
199 	uint_t			mci_tx_flag;
200 	kcondvar_t		mci_tx_cv;
201 
202 	/* Must be last in the structure for dynamic sizing */
203 	mac_tx_percpu_t		mci_tx_pcpu[1];		/* SL */
204 };
205 
206 #define	MAC_CLIENT_IMPL_SIZE						\
207 	(sizeof (mac_client_impl_t) +					\
208 	    (mac_tx_percpu_cnt * sizeof (mac_tx_percpu_t)))
209 
210 extern	int	mac_tx_percpu_cnt;
211 
212 #define	MCIP_TX_SRS(mcip)	\
213 	((mcip)->mci_flent == NULL ? NULL : (mcip)->mci_flent->fe_tx_srs)
214 
215 /* Defensive coding, non-null mcip_flent could be an assert */
216 
217 #define	MCIP_DATAPATH_SETUP(mcip)		\
218 	((mcip)->mci_flent == NULL ? B_FALSE :	\
219 	!((mcip)->mci_flent->fe_flags & FE_MC_NO_DATAPATH))
220 
221 #define	MCIP_RESOURCE_PROPS(mcip)		\
222 	((mcip)->mci_flent == NULL ? NULL :	\
223 	&(mcip)->mci_flent->fe_resource_props)
224 
225 #define	MCIP_EFFECTIVE_PROPS(mcip)		\
226 	(mcip->mci_flent == NULL ? NULL : 	\
227 	&(mcip)->mci_flent->fe_effective_props)
228 
229 #define	MCIP_RESOURCE_PROPS_MASK(mcip)		\
230 	((mcip)->mci_flent == NULL ? 0 :	\
231 	(mcip)->mci_flent->fe_resource_props.mrp_mask)
232 
233 #define	MCIP_RESOURCE_PROPS_MAXBW(mcip)		\
234 	((mcip)->mci_flent == NULL ? 0 :	\
235 	(mcip)->mci_flent->fe_resource_props.mrp_maxbw)
236 
237 #define	MCIP_RESOURCE_PROPS_PRIORITY(mcip)		\
238 	((mcip)->mci_flent == NULL ? 0 :	\
239 	(mcip)->mci_flent->fe_resource_props.mrp_priority)
240 
241 #define	MCIP_RESOURCE_PROPS_CPUS(mcip)		\
242 	((mcip)->mci_flent == NULL ? 0 :	\
243 	&(mcip)->mci_flent->fe_resource_props.mrp_cpus)
244 
245 #define	MCIP_RESOURCE_PROPS_NCPUS(mcip)		\
246 	((mcip)->mci_flent == NULL ? 0 :	\
247 	(mcip)->mci_flent->fe_resource_props.mrp_ncpus)
248 
249 #define	MCIP_RESOURCE_PROPS_CPU(mcip)		\
250 	((mcip)->mci_flent == NULL ? 0 :	\
251 	(mcip)->mci_flent->fe_resource_props.mrp_ncpu)
252 
253 /*
254  * We validate the VLAN id of the packet w.r.t the client's vid,
255  * if required (i.e. !MCIS_DISABLE_TX_VID_CHECK). DLS clients
256  * will have MCIS_DISABLE_TX_VID_CHECK set.
257  * (In the case of aggr when we get back packets, due to
258  * the underlying driver being flow controlled, we won't
259  * drop the packet even if it is VLAN tagged as we
260  * don't set MCIS_DISABLE_TX_VID_CHECK for an aggr.)
261  */
262 #define	MAC_VID_CHECK_NEEDED(mcip)					\
263 	(((mcip)->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0 &&	\
264 	(mcip)->mci_mip->mi_info.mi_nativemedia == DL_ETHER)
265 
266 #define	MAC_VID_CHECK(mcip, mp, err) {					\
267 	if (ntohs(((struct ether_header *)(mp)->b_rptr)->ether_type) ==	\
268 	    ETHERTYPE_VLAN) {						\
269 		/*							\
270 		 * err is set to EINVAL (so the caller can take the	\
271 		 * appropriate action. e.g. freemsg()) for two cases:	\
272 		 * -client is not responsible for filling in the vid.	\
273 		 * -client is responsible for filling in the vid, but	\
274 		 *  the vid doesn't match the vid of the MAC client.	\
275 		 */							\
276 		(err) = EINVAL;						\
277 		if (((mcip)->mci_state_flags & MCIS_TAG_DISABLE) != 0) {\
278 			struct ether_vlan_header	*evhp;		\
279 			uint16_t			vlanid;		\
280 									\
281 			evhp = (struct ether_vlan_header *)(mp)->b_rptr;\
282 			vlanid = VLAN_ID(ntohs(evhp->ether_tci));	\
283 			if (mac_client_check_flow_vid((mcip), vlanid))	\
284 				(err) = 0;				\
285 		}							\
286 	}								\
287 }
288 
289 /*
290  * To allow the hot path to not grab any additional locks, we keep a single
291  * entry VLAN ID cache that caches whether or not a given VID belongs to a
292  * MAC client.
293  */
294 #define	MCIP_VIDCACHE_VALIDSHIFT	31
295 #define	MCIP_VIDCACHE_VIDSHIFT		1
296 #define	MCIP_VIDCACHE_VIDMASK		(UINT16_MAX << MCIP_VIDCACHE_VIDSHIFT)
297 #define	MCIP_VIDCACHE_BOOLSHIFT		0
298 
299 #define	MCIP_VIDCACHE_INVALID		0
300 
301 #define	MCIP_VIDCACHE_CACHE(vid, bool)	\
302 	((1U << MCIP_VIDCACHE_VALIDSHIFT) | \
303 	((vid) << MCIP_VIDCACHE_VIDSHIFT) | \
304 	((bool) ? (1U << MCIP_VIDCACHE_BOOLSHIFT) : 0))
305 
306 #define	MCIP_VIDCACHE_ISVALID(v)	((v) & (1U << MCIP_VIDCACHE_VALIDSHIFT))
307 #define	MCIP_VIDCACHE_VID(v)		\
308 	(((v) & MCIP_VIDCACHE_VIDMASK) >> MCIP_VIDCACHE_VIDSHIFT)
309 #define	MCIP_VIDCACHE_BOOL(v)		((v) & (1U << MCIP_VIDCACHE_BOOLSHIFT))
310 
311 #define	MAC_TAG_NEEDED(mcip)						\
312 	(((mcip)->mci_state_flags & MCIS_TAG_DISABLE) == 0 &&		\
313 	(mcip)->mci_nvids == 1)						\
314 
315 /* MCI state flags */
316 #define	MCIS_IS_VNIC			0x0001
317 #define	MCIS_EXCLUSIVE			0x0002
318 #define	MCIS_TAG_DISABLE		0x0004
319 #define	MCIS_STRIP_DISABLE		0x0008
320 #define	MCIS_IS_AGGR_PORT		0x0010
321 #define	MCIS_CLIENT_POLL_CAPABLE	0x0020
322 #define	MCIS_DESC_LOGGED		0x0040
323 #define	MCIS_SHARE_BOUND		0x0080
324 #define	MCIS_DISABLE_TX_VID_CHECK	0x0100
325 #define	MCIS_USE_DATALINK_NAME		0x0200
326 #define	MCIS_UNICAST_HW			0x0400
327 #define	MCIS_IS_AGGR			0x0800
328 #define	MCIS_RX_BYPASS_DISABLE		0x1000
329 #define	MCIS_NO_UNICAST_ADDR		0x2000
330 
331 /* Mac protection flags */
332 #define	MPT_FLAG_V6_LOCAL_ADDR_SET	0x0001
333 
334 /* in mac_client.c */
335 extern void mac_promisc_client_dispatch(mac_client_impl_t *, mblk_t *);
336 extern void mac_client_init(void);
337 extern void mac_client_fini(void);
338 extern void mac_promisc_dispatch(mac_impl_t *, mblk_t *,
339     mac_client_impl_t *);
340 
341 extern int mac_validate_props(mac_impl_t *, mac_resource_props_t *);
342 
343 extern mac_client_impl_t *mac_vnic_lower(mac_impl_t *);
344 extern mac_client_impl_t *mac_primary_client_handle(mac_impl_t *);
345 extern uint16_t i_mac_flow_vid(flow_entry_t *);
346 extern boolean_t i_mac_capab_get(mac_handle_t, mac_capab_t, void *);
347 
348 extern void mac_unicast_update_clients(mac_impl_t *, mac_address_t *);
349 extern void mac_update_resources(mac_resource_props_t *,
350     mac_resource_props_t *, boolean_t);
351 
352 boolean_t mac_client_check_flow_vid(mac_client_impl_t *, uint16_t);
353 
354 extern boolean_t mac_is_primary_client(mac_client_impl_t *);
355 
356 extern int mac_client_set_rings_prop(mac_client_impl_t *,
357     mac_resource_props_t *, mac_resource_props_t *);
358 extern void mac_set_prim_vlan_rings(mac_impl_t *, mac_resource_props_t *);
359 
360 #ifdef	__cplusplus
361 }
362 #endif
363 
364 #endif	/* _SYS_MAC_CLIENT_IMPL_H */
365