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