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