xref: /illumos-gate/usr/src/uts/common/io/mac/mac_client.c (revision 67109511d24ce85f582162e3ef53710d440368e2)
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 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  * Copyright 2017 RackTop Systems.
26  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
27  * Copyright 2026 Oxide Computer Company
28  */
29 
30 /*
31  * - General Introduction:
32  *
33  * This file contains the implementation of the MAC client kernel
34  * API and related code. The MAC client API allows a kernel module
35  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
36  * It allows a MAC client to associate itself with a MAC address,
37  * VLANs, callback functions for data traffic and for promiscuous mode.
38  * The MAC client API is also used to specify the properties associated
39  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
40  * These properties are further used to determine the hardware resources
41  * to allocate to the various MAC clients.
42  *
43  * - Primary MAC clients:
44  *
45  * The MAC client API refers to "primary MAC clients". A primary MAC
46  * client is a client which "owns" the primary MAC address of
47  * the underlying MAC instance. The primary MAC address is called out
48  * since it is associated with specific semantics: the primary MAC
49  * address is the MAC address which is assigned to the IP interface
50  * when it is plumbed, and the primary MAC address is assigned
51  * to VLAN data-links. The primary address of a MAC instance can
52  * also change dynamically from under the MAC client, for example
53  * as a result of a change of state of a link aggregation. In that
54  * case the MAC layer automatically updates all data-structures which
55  * refer to the current value of the primary MAC address. Typical
56  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
57  * MAC client is the vnic driver.
58  *
59  * - Virtual Switching:
60  *
61  * The MAC layer implements a virtual switch between the MAC clients
62  * (primary and non-primary) defined on top of the same underlying
63  * NIC (physical, link aggregation, etc). The virtual switch is
64  * VLAN-aware, i.e. it allows multiple MAC clients to be member
65  * of one or more VLANs, and the virtual switch will distribute
66  * multicast tagged packets only to the member of the corresponding
67  * VLANs.
68  *
69  * - Upper vs Lower MAC:
70  *
71  * Creating a VNIC on top of a MAC instance effectively causes
72  * two MAC instances to be layered on top of each other, one for
73  * the VNIC(s), one for the underlying MAC instance (physical NIC,
74  * link aggregation, etc). In the code below we refer to the
75  * underlying NIC as the "lower MAC", and we refer to VNICs as
76  * the "upper MAC".
77  *
78  * - Pass-through for VNICs:
79  *
80  * When VNICs are created on top of an underlying MAC, this causes
81  * a layering of two MAC instances. Since the lower MAC already
82  * does the switching and demultiplexing to its MAC clients, the
83  * upper MAC would simply have to pass packets to the layer below
84  * or above it, which would introduce overhead. In order to avoid
85  * this overhead, the MAC layer implements a pass-through mechanism
86  * for VNICs. When a VNIC opens the lower MAC instance, it saves
87  * the MAC client handle it optains from the MAC layer. When a MAC
88  * client opens a VNIC (upper MAC), the MAC layer detects that
89  * the MAC being opened is a VNIC, and gets the MAC client handle
90  * that the VNIC driver obtained from the lower MAC. This exchange
91  * is done through a private capability between the MAC layer
92  * and the VNIC driver. The upper MAC then returns that handle
93  * directly to its MAC client. Any operation done by the upper
94  * MAC client is now done on the lower MAC client handle, which
95  * allows the VNIC driver to be completely bypassed for the
96  * performance sensitive data-path.
97  *
98  * - Secondary MACs for VNICs:
99  *
100  * VNICs support multiple upper mac clients to enable support for
101  * multiple MAC addresses on the VNIC. When the VNIC is created the
102  * initial mac client is the primary upper mac. Any additional mac
103  * clients are secondary macs. These are kept in sync with the primary
104  * (for things such as the rx function and resource control settings)
105  * using the same private capability interface between the MAC layer
106  * and the VNIC layer.
107  *
108  */
109 
110 #include <sys/types.h>
111 #include <sys/conf.h>
112 #include <sys/id_space.h>
113 #include <sys/esunddi.h>
114 #include <sys/stat.h>
115 #include <sys/mkdev.h>
116 #include <sys/stream.h>
117 #include <sys/strsun.h>
118 #include <sys/strsubr.h>
119 #include <sys/pattr.h>
120 #include <sys/dlpi.h>
121 #include <sys/modhash.h>
122 #include <sys/mac_impl.h>
123 #include <sys/mac_client_impl.h>
124 #include <sys/mac_soft_ring.h>
125 #include <sys/mac_stat.h>
126 #include <sys/dls.h>
127 #include <sys/dld.h>
128 #include <sys/modctl.h>
129 #include <sys/fs/dv_node.h>
130 #include <sys/thread.h>
131 #include <sys/proc.h>
132 #include <sys/callb.h>
133 #include <sys/cpuvar.h>
134 #include <sys/atomic.h>
135 #include <sys/sdt.h>
136 #include <sys/mac_flow.h>
137 #include <sys/ddi_intr_impl.h>
138 #include <sys/disp.h>
139 #include <sys/sdt.h>
140 #include <sys/vnic.h>
141 #include <sys/vnic_impl.h>
142 #include <sys/vlan.h>
143 #include <inet/ip.h>
144 #include <inet/ip6.h>
145 #include <sys/exacct.h>
146 #include <sys/exacct_impl.h>
147 #include <inet/nd.h>
148 #include <sys/ethernet.h>
149 
150 kmem_cache_t	*mac_client_impl_cache;
151 kmem_cache_t	*mac_promisc_impl_cache;
152 
153 static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
154 static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
155 static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
156     mac_unicast_impl_t *);
157 static void mac_client_remove_flow_from_list(mac_client_impl_t *,
158     flow_entry_t *);
159 static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
160 static void mac_rename_flow_names(mac_client_impl_t *, const char *);
161 static void mac_virtual_link_update(mac_impl_t *);
162 static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
163     uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
164 static void mac_client_datapath_teardown(mac_client_handle_t,
165     mac_unicast_impl_t *, flow_entry_t *);
166 static int mac_resource_ctl_set(mac_client_handle_t, mac_resource_props_t *);
167 
168 /* ARGSUSED */
169 static int
i_mac_client_impl_ctor(void * buf,void * arg,int kmflag)170 i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
171 {
172 	int	i;
173 	mac_client_impl_t	*mcip = buf;
174 
175 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
176 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
177 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
178 
179 	ASSERT(mac_tx_percpu_cnt >= 0);
180 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
181 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
182 		    MUTEX_DRIVER, NULL);
183 	}
184 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
185 
186 	return (0);
187 }
188 
189 /* ARGSUSED */
190 static void
i_mac_client_impl_dtor(void * buf,void * arg)191 i_mac_client_impl_dtor(void *buf, void *arg)
192 {
193 	int	i;
194 	mac_client_impl_t *mcip = buf;
195 
196 	ASSERT(mcip->mci_promisc_list == NULL);
197 	ASSERT(mcip->mci_unicast_list == NULL);
198 	ASSERT(mcip->mci_state_flags == 0);
199 	ASSERT(mcip->mci_tx_flag == 0);
200 
201 	mutex_destroy(&mcip->mci_tx_cb_lock);
202 
203 	ASSERT(mac_tx_percpu_cnt >= 0);
204 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
205 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
206 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
207 	}
208 	cv_destroy(&mcip->mci_tx_cv);
209 }
210 
211 /* ARGSUSED */
212 static int
i_mac_promisc_impl_ctor(void * buf,void * arg,int kmflag)213 i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
214 {
215 	mac_promisc_impl_t	*mpip = buf;
216 
217 	bzero(buf, sizeof (mac_promisc_impl_t));
218 	mpip->mpi_mci_link.mcb_objp = buf;
219 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
220 	mpip->mpi_mi_link.mcb_objp = buf;
221 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
222 	return (0);
223 }
224 
225 /* ARGSUSED */
226 static void
i_mac_promisc_impl_dtor(void * buf,void * arg)227 i_mac_promisc_impl_dtor(void *buf, void *arg)
228 {
229 	mac_promisc_impl_t	*mpip = buf;
230 
231 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
232 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
233 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
234 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
235 
236 	mpip->mpi_mci_link.mcb_objp = NULL;
237 	mpip->mpi_mci_link.mcb_objsize = 0;
238 	mpip->mpi_mi_link.mcb_objp = NULL;
239 	mpip->mpi_mi_link.mcb_objsize = 0;
240 
241 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
242 	mpip->mpi_mci_link.mcb_objsize = 0;
243 }
244 
245 void
mac_client_init(void)246 mac_client_init(void)
247 {
248 	ASSERT(mac_tx_percpu_cnt >= 0);
249 
250 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
251 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
252 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
253 	ASSERT(mac_client_impl_cache != NULL);
254 
255 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
256 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
257 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
258 	ASSERT(mac_promisc_impl_cache != NULL);
259 }
260 
261 void
mac_client_fini(void)262 mac_client_fini(void)
263 {
264 	kmem_cache_destroy(mac_client_impl_cache);
265 	kmem_cache_destroy(mac_promisc_impl_cache);
266 }
267 
268 /*
269  * Return the lower MAC client handle from the VNIC driver for the
270  * specified VNIC MAC instance.
271  */
272 mac_client_impl_t *
mac_vnic_lower(mac_impl_t * mip)273 mac_vnic_lower(mac_impl_t *mip)
274 {
275 	mac_capab_vnic_t cap;
276 	mac_client_impl_t *mcip;
277 
278 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
279 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
280 
281 	return (mcip);
282 }
283 
284 /*
285  * Update the secondary macs
286  */
287 void
mac_vnic_secondary_update(mac_impl_t * mip)288 mac_vnic_secondary_update(mac_impl_t *mip)
289 {
290 	mac_capab_vnic_t cap;
291 
292 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
293 	cap.mcv_mac_secondary_update(cap.mcv_arg);
294 }
295 
296 /*
297  * Return the MAC client handle of the primary MAC client for the
298  * specified MAC instance, or NULL otherwise.
299  */
300 mac_client_impl_t *
mac_primary_client_handle(mac_impl_t * mip)301 mac_primary_client_handle(mac_impl_t *mip)
302 {
303 	mac_client_impl_t *mcip;
304 
305 	if (mip->mi_state_flags & MIS_IS_VNIC)
306 		return (mac_vnic_lower(mip));
307 
308 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
309 
310 	for (mcip = mip->mi_clients_list; mcip != NULL;
311 	    mcip = mcip->mci_client_next) {
312 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
313 			return (mcip);
314 	}
315 	return (NULL);
316 }
317 
318 /*
319  * Open a MAC specified by its MAC name.
320  */
321 int
mac_open(const char * macname,mac_handle_t * mhp)322 mac_open(const char *macname, mac_handle_t *mhp)
323 {
324 	mac_impl_t	*mip;
325 	int		err;
326 
327 	/*
328 	 * Look up its entry in the global hash table.
329 	 */
330 	if ((err = mac_hold(macname, &mip)) != 0)
331 		return (err);
332 
333 	/*
334 	 * Hold the dip associated to the MAC to prevent it from being
335 	 * detached. For a softmac, its underlying dip is held by the
336 	 * mi_open() callback.
337 	 *
338 	 * This is done to be more tolerant with some defective drivers,
339 	 * which incorrectly handle mac_unregister() failure in their
340 	 * xxx_detach() routine. For example, some drivers ignore the
341 	 * failure of mac_unregister() and free all resources that
342 	 * that are needed for data transmition.
343 	 */
344 	e_ddi_hold_devi(mip->mi_dip);
345 
346 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
347 		*mhp = (mac_handle_t)mip;
348 		return (0);
349 	}
350 
351 	/*
352 	 * The mac perimeter is used in both mac_open and mac_close by the
353 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
354 	 */
355 	i_mac_perim_enter(mip);
356 	mip->mi_oref++;
357 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
358 		*mhp = (mac_handle_t)mip;
359 		i_mac_perim_exit(mip);
360 		return (0);
361 	}
362 	mip->mi_oref--;
363 	ddi_release_devi(mip->mi_dip);
364 	mac_rele(mip);
365 	i_mac_perim_exit(mip);
366 	return (err);
367 }
368 
369 /*
370  * Open a MAC specified by its linkid.
371  */
372 int
mac_open_by_linkid(datalink_id_t linkid,mac_handle_t * mhp)373 mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
374 {
375 	dls_dl_handle_t	dlh;
376 	int		err;
377 
378 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
379 		return (err);
380 
381 	dls_devnet_prop_task_wait(dlh);
382 
383 	err = mac_open(dls_devnet_mac(dlh), mhp);
384 
385 	dls_devnet_rele_tmp(dlh);
386 	return (err);
387 }
388 
389 /*
390  * Open a MAC specified by its link name.
391  */
392 int
mac_open_by_linkname(const char * link,mac_handle_t * mhp)393 mac_open_by_linkname(const char *link, mac_handle_t *mhp)
394 {
395 	datalink_id_t	linkid;
396 	int		err;
397 
398 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
399 		return (err);
400 	return (mac_open_by_linkid(linkid, mhp));
401 }
402 
403 /*
404  * Close the specified MAC.
405  */
406 void
mac_close(mac_handle_t mh)407 mac_close(mac_handle_t mh)
408 {
409 	mac_impl_t	*mip = (mac_impl_t *)mh;
410 
411 	i_mac_perim_enter(mip);
412 	/*
413 	 * The mac perimeter is used in both mac_open and mac_close by the
414 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
415 	 */
416 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
417 		ASSERT(mip->mi_oref != 0);
418 		if (--mip->mi_oref == 0) {
419 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
420 				mip->mi_close(mip->mi_driver);
421 		}
422 	}
423 	i_mac_perim_exit(mip);
424 	ddi_release_devi(mip->mi_dip);
425 	mac_rele(mip);
426 }
427 
428 /*
429  * Misc utility functions to retrieve various information about a MAC
430  * instance or a MAC client.
431  */
432 
433 const mac_info_t *
mac_info(mac_handle_t mh)434 mac_info(mac_handle_t mh)
435 {
436 	return (&((mac_impl_t *)mh)->mi_info);
437 }
438 
439 dev_info_t *
mac_devinfo_get(mac_handle_t mh)440 mac_devinfo_get(mac_handle_t mh)
441 {
442 	return (((mac_impl_t *)mh)->mi_dip);
443 }
444 
445 void *
mac_driver(mac_handle_t mh)446 mac_driver(mac_handle_t mh)
447 {
448 	return (((mac_impl_t *)mh)->mi_driver);
449 }
450 
451 const char *
mac_name(mac_handle_t mh)452 mac_name(mac_handle_t mh)
453 {
454 	return (((mac_impl_t *)mh)->mi_name);
455 }
456 
457 int
mac_type(mac_handle_t mh)458 mac_type(mac_handle_t mh)
459 {
460 	return (((mac_impl_t *)mh)->mi_type->mt_type);
461 }
462 
463 int
mac_nativetype(mac_handle_t mh)464 mac_nativetype(mac_handle_t mh)
465 {
466 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
467 }
468 
469 char *
mac_client_name(mac_client_handle_t mch)470 mac_client_name(mac_client_handle_t mch)
471 {
472 	return (((mac_client_impl_t *)mch)->mci_name);
473 }
474 
475 minor_t
mac_minor(mac_handle_t mh)476 mac_minor(mac_handle_t mh)
477 {
478 	return (((mac_impl_t *)mh)->mi_minor);
479 }
480 
481 /*
482  * Return the VID associated with a MAC client. This function should
483  * be called for clients which are associated with only one VID.
484  */
485 uint16_t
mac_client_vid(mac_client_handle_t mch)486 mac_client_vid(mac_client_handle_t mch)
487 {
488 	uint16_t		vid = VLAN_ID_NONE;
489 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
490 	flow_desc_t		flow_desc;
491 
492 	if (mcip->mci_nflents == 0)
493 		return (vid);
494 
495 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
496 
497 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
498 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
499 		vid = flow_desc.fd_vid;
500 
501 	return (vid);
502 }
503 
504 /*
505  * Return whether the specified MAC client corresponds to a VLAN VNIC.
506  */
507 boolean_t
mac_client_is_vlan_vnic(mac_client_handle_t mch)508 mac_client_is_vlan_vnic(mac_client_handle_t mch)
509 {
510 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
511 
512 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
513 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
514 }
515 
516 /*
517  * Return the link speed associated with the specified MAC client.
518  *
519  * The link speed of a MAC client is equal to the smallest value of
520  * 1) the current link speed of the underlying NIC, or
521  * 2) the bandwidth limit set for the MAC client.
522  *
523  * Note that the bandwidth limit can be higher than the speed
524  * of the underlying NIC. This is allowed to avoid spurious
525  * administration action failures or artifically lowering the
526  * bandwidth limit of a link that may  have temporarily lowered
527  * its link speed due to hardware problem or administrator action.
528  */
529 static uint64_t
mac_client_ifspeed(mac_client_impl_t * mcip)530 mac_client_ifspeed(mac_client_impl_t *mcip)
531 {
532 	mac_impl_t *mip = mcip->mci_mip;
533 	uint64_t nic_speed;
534 
535 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
536 
537 	if (nic_speed == 0) {
538 		return (0);
539 	} else {
540 		uint64_t policy_limit = (uint64_t)-1;
541 
542 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
543 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
544 
545 		return (MIN(policy_limit, nic_speed));
546 	}
547 }
548 
549 /*
550  * Return the link state of the specified client. If here are more
551  * than one clients of the underying mac_impl_t, the link state
552  * will always be UP regardless of the link state of the underlying
553  * mac_impl_t. This is needed to allow the MAC clients to continue
554  * to communicate with each other even when the physical link of
555  * their mac_impl_t is down.
556  */
557 static uint64_t
mac_client_link_state(mac_client_impl_t * mcip)558 mac_client_link_state(mac_client_impl_t *mcip)
559 {
560 	mac_impl_t *mip = mcip->mci_mip;
561 	uint16_t vid;
562 	mac_client_impl_t *mci_list;
563 	mac_unicast_impl_t *mui_list, *oth_mui_list;
564 
565 	/*
566 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
567 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
568 	 * if 'mcip' has more than one VID's then we match ANY one of the
569 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
570 	 */
571 	rw_enter(&mcip->mci_rw_lock, RW_READER);
572 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
573 	    mui_list = mui_list->mui_next) {
574 		vid = mui_list->mui_vid;
575 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
576 		    mci_list = mci_list->mci_client_next) {
577 			if (mci_list == mcip)
578 				continue;
579 			for (oth_mui_list = mci_list->mci_unicast_list;
580 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
581 			    mui_next) {
582 				if (vid == oth_mui_list->mui_vid) {
583 					rw_exit(&mcip->mci_rw_lock);
584 					return (LINK_STATE_UP);
585 				}
586 			}
587 		}
588 	}
589 	rw_exit(&mcip->mci_rw_lock);
590 
591 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
592 }
593 
594 /*
595  * These statistics are consumed by dladm show-link -s <vnic>,
596  * dladm show-vnic -s and netstat. With the introduction of dlstat,
597  * dladm show-link -s and dladm show-vnic -s witll be EOL'ed while
598  * netstat will consume from kstats introduced for dlstat. This code
599  * will be removed at that time.
600  */
601 
602 /*
603  * Return the statistics of a MAC client. These statistics are different
604  * then the statistics of the underlying MAC which are returned by
605  * mac_stat_get().
606  *
607  * Note that for things based on the tx and rx stats, mac will end up clobbering
608  * those stats when the underlying set of rings in the srs changes. As such, we
609  * need to source not only the current set, but also the historical set when
610  * returning to the client, lest our counters appear to go backwards.
611  */
612 uint64_t
mac_client_stat_get(mac_client_handle_t mch,uint_t stat)613 mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
614 {
615 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
616 	mac_impl_t		*mip = mcip->mci_mip;
617 	flow_entry_t		*flent = mcip->mci_flent;
618 	mac_soft_ring_set_t	*mac_srs;
619 	mac_rx_stats_t		*mac_rx_stat, *old_rx_stat;
620 	mac_tx_stats_t		*mac_tx_stat, *old_tx_stat;
621 	int i;
622 	uint64_t val = 0;
623 
624 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
625 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
626 	old_rx_stat = &mcip->mci_misc_stat.mms_defunctrxlanestats;
627 	old_tx_stat = &mcip->mci_misc_stat.mms_defuncttxlanestats;
628 
629 	switch (stat) {
630 	case MAC_STAT_LINK_STATE:
631 		val = mac_client_link_state(mcip);
632 		break;
633 	case MAC_STAT_LINK_UP:
634 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
635 		break;
636 	case MAC_STAT_PROMISC:
637 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
638 		break;
639 	case MAC_STAT_LOWLINK_STATE:
640 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
641 		break;
642 	case MAC_STAT_IFSPEED:
643 		val = mac_client_ifspeed(mcip);
644 		break;
645 	case MAC_STAT_MULTIRCV:
646 		val = mcip->mci_misc_stat.mms_multircv;
647 		break;
648 	case MAC_STAT_BRDCSTRCV:
649 		val = mcip->mci_misc_stat.mms_brdcstrcv;
650 		break;
651 	case MAC_STAT_MULTIXMT:
652 		val = mcip->mci_misc_stat.mms_multixmt;
653 		break;
654 	case MAC_STAT_BRDCSTXMT:
655 		val = mcip->mci_misc_stat.mms_brdcstxmt;
656 		break;
657 	case MAC_STAT_OBYTES:
658 		val = mac_tx_stat->mts_obytes;
659 		val += old_tx_stat->mts_obytes;
660 		break;
661 	case MAC_STAT_OPACKETS:
662 		val = mac_tx_stat->mts_opackets;
663 		val += old_tx_stat->mts_opackets;
664 		break;
665 	case MAC_STAT_OERRORS:
666 		val = mac_tx_stat->mts_oerrors;
667 		val += old_tx_stat->mts_oerrors;
668 		break;
669 	case MAC_STAT_IPACKETS:
670 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
671 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
672 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
673 			val += mac_rx_stat->mrs_intrcnt +
674 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
675 		}
676 		val += old_rx_stat->mrs_intrcnt + old_rx_stat->mrs_pollcnt +
677 		    old_rx_stat->mrs_lclcnt;
678 		break;
679 	case MAC_STAT_RBYTES:
680 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
681 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
682 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
683 			val += mac_rx_stat->mrs_intrbytes +
684 			    mac_rx_stat->mrs_pollbytes +
685 			    mac_rx_stat->mrs_lclbytes;
686 		}
687 		val += old_rx_stat->mrs_intrbytes + old_rx_stat->mrs_pollbytes +
688 		    old_rx_stat->mrs_lclbytes;
689 		break;
690 	case MAC_STAT_IERRORS:
691 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
692 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
693 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
694 			val += mac_rx_stat->mrs_ierrors;
695 		}
696 		val += old_rx_stat->mrs_ierrors;
697 		break;
698 	default:
699 		val = mac_driver_stat_default(mip, stat);
700 		break;
701 	}
702 
703 	return (val);
704 }
705 
706 /*
707  * Return the statistics of the specified MAC instance.
708  */
709 uint64_t
mac_stat_get(mac_handle_t mh,uint_t stat)710 mac_stat_get(mac_handle_t mh, uint_t stat)
711 {
712 	mac_impl_t	*mip = (mac_impl_t *)mh;
713 	uint64_t	val;
714 	int		ret;
715 
716 	/*
717 	 * The range of stat determines where it is maintained.  Stat
718 	 * values from 0 up to (but not including) MAC_STAT_MIN are
719 	 * mainteined by the mac module itself.  Everything else is
720 	 * maintained by the driver.
721 	 *
722 	 * If the mac_impl_t being queried corresponds to a VNIC,
723 	 * the stats need to be queried from the lower MAC client
724 	 * corresponding to the VNIC. (The mac_link_update()
725 	 * invoked by the driver to the lower MAC causes the *lower
726 	 * MAC* to update its mi_linkstate, and send a notification
727 	 * to its MAC clients. Due to the VNIC passthrough,
728 	 * these notifications are sent to the upper MAC clients
729 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
730 	 * does not have a valid mi_linkstate.
731 	 */
732 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
733 		/* these stats are maintained by the mac module itself */
734 		switch (stat) {
735 		case MAC_STAT_LINK_STATE:
736 			return (mip->mi_linkstate);
737 		case MAC_STAT_LINK_UP:
738 			return (mip->mi_linkstate == LINK_STATE_UP);
739 		case MAC_STAT_PROMISC:
740 			return (mip->mi_devpromisc != 0);
741 		case MAC_STAT_LOWLINK_STATE:
742 			return (mip->mi_lowlinkstate);
743 		default:
744 			ASSERT(B_FALSE);
745 		}
746 	}
747 
748 	/*
749 	 * Call the driver to get the given statistic.
750 	 */
751 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
752 	if (ret != 0) {
753 		/*
754 		 * The driver doesn't support this statistic.  Get the
755 		 * statistic's default value.
756 		 */
757 		val = mac_driver_stat_default(mip, stat);
758 	}
759 	return (val);
760 }
761 
762 /*
763  * Query hardware rx ring corresponding to the pseudo ring.
764  */
765 uint64_t
mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle,uint_t stat)766 mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
767 {
768 	return (mac_rx_ring_stat_get(handle, stat));
769 }
770 
771 /*
772  * Query hardware tx ring corresponding to the pseudo ring.
773  */
774 uint64_t
mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle,uint_t stat)775 mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
776 {
777 	return (mac_tx_ring_stat_get(handle, stat));
778 }
779 
780 /*
781  * Utility function which returns the VID associated with a flow entry.
782  */
783 uint16_t
i_mac_flow_vid(flow_entry_t * flent)784 i_mac_flow_vid(flow_entry_t *flent)
785 {
786 	flow_desc_t	flow_desc;
787 
788 	mac_flow_get_desc(flent, &flow_desc);
789 
790 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
791 		return (flow_desc.fd_vid);
792 	return (VLAN_ID_NONE);
793 }
794 
795 /*
796  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
797  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
798  * length.
799  */
800 boolean_t
mac_unicst_verify(mac_handle_t mh,const uint8_t * addr,uint_t len)801 mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
802 {
803 	mac_impl_t	*mip = (mac_impl_t *)mh;
804 
805 	/*
806 	 * Verify the address. No lock is needed since mi_type and plugin
807 	 * details don't change after mac_register().
808 	 */
809 	if ((len != mip->mi_type->mt_addr_length) ||
810 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
811 	    mip->mi_pdata)) != 0) {
812 		return (B_FALSE);
813 	} else {
814 		return (B_TRUE);
815 	}
816 }
817 
818 void
mac_sdu_get(mac_handle_t mh,uint_t * min_sdu,uint_t * max_sdu)819 mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
820 {
821 	mac_impl_t	*mip = (mac_impl_t *)mh;
822 
823 	if (min_sdu != NULL)
824 		*min_sdu = mip->mi_sdu_min;
825 	if (max_sdu != NULL)
826 		*max_sdu = mip->mi_sdu_max;
827 }
828 
829 void
mac_sdu_get2(mac_handle_t mh,uint_t * min_sdu,uint_t * max_sdu,uint_t * multicast_sdu)830 mac_sdu_get2(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu,
831     uint_t *multicast_sdu)
832 {
833 	mac_impl_t	*mip = (mac_impl_t *)mh;
834 
835 	if (min_sdu != NULL)
836 		*min_sdu = mip->mi_sdu_min;
837 	if (max_sdu != NULL)
838 		*max_sdu = mip->mi_sdu_max;
839 	if (multicast_sdu != NULL)
840 		*multicast_sdu = mip->mi_sdu_multicast;
841 }
842 
843 /*
844  * Update the MAC unicast address of the specified client's flows. Currently
845  * only one unicast MAC unicast address is allowed per client.
846  */
847 static void
mac_unicast_update_client_flow(mac_client_impl_t * mcip)848 mac_unicast_update_client_flow(mac_client_impl_t *mcip)
849 {
850 	mac_impl_t *mip = mcip->mci_mip;
851 	flow_entry_t *flent = mcip->mci_flent;
852 	mac_address_t *map = mcip->mci_unicast;
853 	flow_desc_t flow_desc;
854 
855 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
856 	ASSERT(flent != NULL);
857 
858 	mac_flow_get_desc(flent, &flow_desc);
859 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
860 
861 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
862 	mac_flow_set_desc(flent, &flow_desc);
863 
864 	/*
865 	 * The v6 local and SLAAC addrs (used by mac protection) need to be
866 	 * regenerated because our mac address has changed.
867 	 */
868 	mac_protect_update_mac_token(mcip);
869 
870 	/*
871 	 * When there are multiple VLANs sharing the same MAC address,
872 	 * each gets its own MAC client, except when running on sun4v
873 	 * vsw. In that case the mci_flent_list is used to place
874 	 * multiple VLAN flows on one MAC client. If we ever get rid
875 	 * of vsw then this code can go, but until then we need to
876 	 * update all flow entries.
877 	 */
878 	for (flent = mcip->mci_flent_list; flent != NULL;
879 	    flent = flent->fe_client_next) {
880 		mac_flow_get_desc(flent, &flow_desc);
881 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
882 		    flent->fe_type & FLOW_VNIC_MAC))
883 			continue;
884 
885 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
886 		mac_flow_set_desc(flent, &flow_desc);
887 	}
888 }
889 
890 /*
891  * Update all clients that share the same unicast address.
892  */
893 void
mac_unicast_update_clients(mac_impl_t * mip,mac_address_t * map)894 mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
895 {
896 	mac_client_impl_t *mcip;
897 
898 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
899 
900 	/*
901 	 * Find all clients that share the same unicast MAC address and update
902 	 * them appropriately.
903 	 */
904 	for (mcip = mip->mi_clients_list; mcip != NULL;
905 	    mcip = mcip->mci_client_next) {
906 		/*
907 		 * Ignore clients that don't share this MAC address.
908 		 */
909 		if (map != mcip->mci_unicast)
910 			continue;
911 
912 		/*
913 		 * Update those clients with same old unicast MAC address.
914 		 */
915 		mac_unicast_update_client_flow(mcip);
916 	}
917 }
918 
919 /*
920  * Update the unicast MAC address of the specified VNIC MAC client.
921  *
922  * Check whether the operation is valid. Any of following cases should fail:
923  *
924  * 1. It's a VLAN type of VNIC.
925  * 2. The new value is current "primary" MAC address.
926  * 3. The current MAC address is shared with other clients.
927  * 4. The new MAC address has been used. This case will be valid when
928  *    client migration is fully supported.
929  */
930 int
mac_vnic_unicast_set(mac_client_handle_t mch,const uint8_t * addr)931 mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
932 {
933 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
934 	mac_impl_t *mip = mcip->mci_mip;
935 	mac_address_t *map = mcip->mci_unicast;
936 	int err;
937 
938 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
939 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
940 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
941 
942 	i_mac_perim_enter(mip);
943 
944 	/*
945 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
946 	 * of the underlying interface. Must fail here. Refer to case 1 above.
947 	 */
948 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
949 		i_mac_perim_exit(mip);
950 		return (ENOTSUP);
951 	}
952 
953 	/*
954 	 * If the new address is the "primary" one, must fail. Refer to
955 	 * case 2 above.
956 	 */
957 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
958 		i_mac_perim_exit(mip);
959 		return (EACCES);
960 	}
961 
962 	/*
963 	 * If the address is shared by multiple clients, must fail. Refer
964 	 * to case 3 above.
965 	 */
966 	if (mac_check_macaddr_shared(map)) {
967 		i_mac_perim_exit(mip);
968 		return (EBUSY);
969 	}
970 
971 	/*
972 	 * If the new address has been used, must fail for now. Refer to
973 	 * case 4 above.
974 	 */
975 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
976 		i_mac_perim_exit(mip);
977 		return (ENOTSUP);
978 	}
979 
980 	/*
981 	 * Update the MAC address.
982 	 */
983 	err = mac_update_macaddr(map, (uint8_t *)addr);
984 
985 	if (err != 0) {
986 		i_mac_perim_exit(mip);
987 		return (err);
988 	}
989 
990 	/*
991 	 * Update all flows of this MAC client.
992 	 */
993 	mac_unicast_update_client_flow(mcip);
994 
995 	i_mac_perim_exit(mip);
996 	return (0);
997 }
998 
999 /*
1000  * Program the new primary unicast address of the specified MAC.
1001  *
1002  * Function mac_update_macaddr() takes care different types of underlying
1003  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
1004  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
1005  * which will take care of updating the MAC address of the corresponding
1006  * MAC client.
1007  *
1008  * This is the only interface that allow the client to update the "primary"
1009  * MAC address of the underlying MAC. The new value must have not been
1010  * used by other clients.
1011  */
1012 int
mac_unicast_primary_set(mac_handle_t mh,const uint8_t * addr)1013 mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
1014 {
1015 	mac_impl_t *mip = (mac_impl_t *)mh;
1016 	mac_address_t *map;
1017 	int err;
1018 
1019 	/* verify the address validity */
1020 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
1021 		return (EINVAL);
1022 
1023 	i_mac_perim_enter(mip);
1024 
1025 	/*
1026 	 * If the new value is the same as the current primary address value,
1027 	 * there's nothing to do.
1028 	 */
1029 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
1030 		i_mac_perim_exit(mip);
1031 		return (0);
1032 	}
1033 
1034 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
1035 		i_mac_perim_exit(mip);
1036 		return (EBUSY);
1037 	}
1038 
1039 	map = mac_find_macaddr(mip, mip->mi_addr);
1040 	ASSERT(map != NULL);
1041 
1042 	/*
1043 	 * Update the MAC address.
1044 	 */
1045 	if (mip->mi_state_flags & MIS_IS_AGGR) {
1046 		mac_capab_aggr_t aggr_cap;
1047 
1048 		/*
1049 		 * If the MAC is an aggregation, other than the unicast
1050 		 * addresses programming, aggr must be informed about this
1051 		 * primary unicst address change to change its MAC address
1052 		 * policy to be user-specified.
1053 		 */
1054 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
1055 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
1056 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
1057 		if (err == 0)
1058 			bcopy(addr, map->ma_addr, map->ma_len);
1059 	} else {
1060 		err = mac_update_macaddr(map, (uint8_t *)addr);
1061 	}
1062 
1063 	if (err != 0) {
1064 		i_mac_perim_exit(mip);
1065 		return (err);
1066 	}
1067 
1068 	mac_unicast_update_clients(mip, map);
1069 
1070 	/*
1071 	 * Save the new primary MAC address in mac_impl_t.
1072 	 */
1073 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
1074 
1075 	i_mac_perim_exit(mip);
1076 
1077 	if (err == 0)
1078 		i_mac_notify(mip, MAC_NOTE_UNICST);
1079 
1080 	return (err);
1081 }
1082 
1083 /*
1084  * Return the current primary MAC address of the specified MAC.
1085  */
1086 void
mac_unicast_primary_get(mac_handle_t mh,uint8_t * addr)1087 mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
1088 {
1089 	mac_impl_t *mip = (mac_impl_t *)mh;
1090 
1091 	rw_enter(&mip->mi_rw_lock, RW_READER);
1092 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
1093 	rw_exit(&mip->mi_rw_lock);
1094 }
1095 
1096 /*
1097  * Return the secondary MAC address for the specified handle
1098  */
1099 void
mac_unicast_secondary_get(mac_client_handle_t mh,uint8_t * addr)1100 mac_unicast_secondary_get(mac_client_handle_t mh, uint8_t *addr)
1101 {
1102 	mac_client_impl_t *mcip = (mac_client_impl_t *)mh;
1103 
1104 	ASSERT(mcip->mci_unicast != NULL);
1105 	bcopy(mcip->mci_unicast->ma_addr, addr, mcip->mci_unicast->ma_len);
1106 }
1107 
1108 /*
1109  * Return information about the use of the primary MAC address of the
1110  * specified MAC instance:
1111  *
1112  * - if client_name is non-NULL, it must point to a string of at
1113  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
1114  *   client which uses the primary MAC address.
1115  *
1116  * - if in_use is non-NULL, used to return whether the primary MAC
1117  *   address is currently in use.
1118  */
1119 void
mac_unicast_primary_info(mac_handle_t mh,char * client_name,boolean_t * in_use)1120 mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
1121 {
1122 	mac_impl_t *mip = (mac_impl_t *)mh;
1123 	mac_client_impl_t *cur_client;
1124 
1125 	if (in_use != NULL)
1126 		*in_use = B_FALSE;
1127 	if (client_name != NULL)
1128 		bzero(client_name, MAXNAMELEN);
1129 
1130 	/*
1131 	 * The mi_rw_lock is used to protect threads that don't hold the
1132 	 * mac perimeter to get a consistent view of the mi_clients_list.
1133 	 * Threads that modify the list must hold both the mac perimeter and
1134 	 * mi_rw_lock(RW_WRITER)
1135 	 */
1136 	rw_enter(&mip->mi_rw_lock, RW_READER);
1137 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
1138 	    cur_client = cur_client->mci_client_next) {
1139 		if (mac_is_primary_client(cur_client) ||
1140 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
1141 			rw_exit(&mip->mi_rw_lock);
1142 			if (in_use != NULL)
1143 				*in_use = B_TRUE;
1144 			if (client_name != NULL) {
1145 				bcopy(cur_client->mci_name, client_name,
1146 				    MAXNAMELEN);
1147 			}
1148 			return;
1149 		}
1150 	}
1151 	rw_exit(&mip->mi_rw_lock);
1152 }
1153 
1154 /*
1155  * Return the current destination MAC address of the specified MAC.
1156  */
1157 boolean_t
mac_dst_get(mac_handle_t mh,uint8_t * addr)1158 mac_dst_get(mac_handle_t mh, uint8_t *addr)
1159 {
1160 	mac_impl_t *mip = (mac_impl_t *)mh;
1161 
1162 	rw_enter(&mip->mi_rw_lock, RW_READER);
1163 	if (mip->mi_dstaddr_set)
1164 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
1165 	rw_exit(&mip->mi_rw_lock);
1166 	return (mip->mi_dstaddr_set);
1167 }
1168 
1169 /*
1170  * Add the specified MAC client to the list of clients which opened
1171  * the specified MAC.
1172  */
1173 static void
mac_client_add(mac_client_impl_t * mcip)1174 mac_client_add(mac_client_impl_t *mcip)
1175 {
1176 	mac_impl_t *mip = mcip->mci_mip;
1177 
1178 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1179 
1180 	/* add VNIC to the front of the list */
1181 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1182 	mcip->mci_client_next = mip->mi_clients_list;
1183 	mip->mi_clients_list = mcip;
1184 	mip->mi_nclients++;
1185 	rw_exit(&mip->mi_rw_lock);
1186 }
1187 
1188 /*
1189  * Remove the specified MAC client from the list of clients which opened
1190  * the specified MAC.
1191  */
1192 static void
mac_client_remove(mac_client_impl_t * mcip)1193 mac_client_remove(mac_client_impl_t *mcip)
1194 {
1195 	mac_impl_t *mip = mcip->mci_mip;
1196 	mac_client_impl_t **prev, *cclient;
1197 
1198 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1199 
1200 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1201 	prev = &mip->mi_clients_list;
1202 	cclient = *prev;
1203 	while (cclient != NULL && cclient != mcip) {
1204 		prev = &cclient->mci_client_next;
1205 		cclient = *prev;
1206 	}
1207 	ASSERT(cclient != NULL);
1208 	*prev = cclient->mci_client_next;
1209 	mip->mi_nclients--;
1210 	rw_exit(&mip->mi_rw_lock);
1211 }
1212 
1213 static mac_unicast_impl_t *
mac_client_find_vid(mac_client_impl_t * mcip,uint16_t vid)1214 mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
1215 {
1216 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
1217 
1218 	while ((muip != NULL) && (muip->mui_vid != vid))
1219 		muip = muip->mui_next;
1220 
1221 	return (muip);
1222 }
1223 
1224 /*
1225  * Return whether the specified (MAC address, VID) tuple is already used by
1226  * one of the MAC clients associated with the specified MAC.
1227  */
1228 static boolean_t
mac_addr_in_use(mac_impl_t * mip,uint8_t * mac_addr,uint16_t vid)1229 mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
1230 {
1231 	mac_client_impl_t *client;
1232 	mac_address_t *map;
1233 
1234 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1235 
1236 	for (client = mip->mi_clients_list; client != NULL;
1237 	    client = client->mci_client_next) {
1238 
1239 		/*
1240 		 * Ignore clients that don't have unicast address.
1241 		 */
1242 		if (client->mci_unicast_list == NULL)
1243 			continue;
1244 
1245 		map = client->mci_unicast;
1246 
1247 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
1248 		    (mac_client_find_vid(client, vid) != NULL)) {
1249 			return (B_TRUE);
1250 		}
1251 	}
1252 
1253 	return (B_FALSE);
1254 }
1255 
1256 /*
1257  * Generate a random MAC address. The MAC address prefix is
1258  * stored in the array pointed to by mac_addr, and its length, in bytes,
1259  * is specified by prefix_len. The least significant bits
1260  * after prefix_len bytes are generated, and stored after the prefix
1261  * in the mac_addr array.
1262  */
1263 int
mac_addr_random(mac_client_handle_t mch,uint_t prefix_len,uint8_t * mac_addr,mac_diag_t * diag)1264 mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
1265     uint8_t *mac_addr, mac_diag_t *diag)
1266 {
1267 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1268 	mac_impl_t *mip = mcip->mci_mip;
1269 	size_t addr_len = mip->mi_type->mt_addr_length;
1270 
1271 	if (prefix_len >= addr_len) {
1272 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
1273 		return (EINVAL);
1274 	}
1275 
1276 	/* check the prefix value */
1277 	if (prefix_len > 0) {
1278 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
1279 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
1280 		    addr_len)) {
1281 			*diag = MAC_DIAG_MACPREFIX_INVALID;
1282 			return (EINVAL);
1283 		}
1284 	}
1285 
1286 	/* generate the MAC address */
1287 	if (prefix_len < addr_len) {
1288 		(void) random_get_pseudo_bytes(mac_addr +
1289 		    prefix_len, addr_len - prefix_len);
1290 	}
1291 
1292 	*diag = MAC_DIAG_NONE;
1293 	return (0);
1294 }
1295 
1296 /*
1297  * Set the priority range for this MAC client. This will be used to
1298  * determine the absolute priority for the threads created for this
1299  * MAC client using the specified "low", "medium" and "high" level.
1300  * This will also be used for any subflows on this MAC client.
1301  */
1302 #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
1303 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
1304 	    MAXCLSYSPRI, (pri));					\
1305 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
1306 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
1307 	}
1308 
1309 /*
1310  * MAC client open entry point. Return a new MAC client handle. Each
1311  * MAC client is associated with a name, specified through the 'name'
1312  * argument.
1313  */
1314 int
mac_client_open(mac_handle_t mh,mac_client_handle_t * mchp,char * name,uint16_t flags)1315 mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
1316     uint16_t flags)
1317 {
1318 	mac_impl_t		*mip = (mac_impl_t *)mh;
1319 	mac_client_impl_t	*mcip;
1320 	int			err = 0;
1321 	boolean_t		share_desired;
1322 	flow_entry_t		*flent = NULL;
1323 
1324 	share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
1325 	*mchp = NULL;
1326 
1327 	i_mac_perim_enter(mip);
1328 
1329 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1330 		/*
1331 		 * The underlying MAC is a VNIC. Return the MAC client
1332 		 * handle of the lower MAC which was obtained by
1333 		 * the VNIC driver when it did its mac_client_open().
1334 		 */
1335 
1336 		mcip = mac_vnic_lower(mip);
1337 
1338 		/*
1339 		 * Note that multiple mac clients share the same mcip in
1340 		 * this case.
1341 		 */
1342 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
1343 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1344 
1345 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1346 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1347 
1348 		mip->mi_clients_list = mcip;
1349 		i_mac_perim_exit(mip);
1350 		*mchp = (mac_client_handle_t)mcip;
1351 
1352 		DTRACE_PROBE2(mac__client__open__nonallocated, mac_impl_t *,
1353 		    mcip->mci_mip, mac_client_impl_t *, mcip);
1354 
1355 		return (err);
1356 	}
1357 
1358 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
1359 
1360 	mcip->mci_mip = mip;
1361 	mcip->mci_upper_mip = NULL;
1362 	mcip->mci_rx_fn = mac_rx_def;
1363 	mcip->mci_rx_arg = NULL;
1364 	mcip->mci_rx_p_fn = NULL;
1365 	mcip->mci_rx_p_arg = NULL;
1366 	mcip->mci_p_unicast_list = NULL;
1367 	mcip->mci_direct_rx.mdrx_v4 = NULL;
1368 	mcip->mci_direct_rx.mdrx_v6 = NULL;
1369 	mcip->mci_direct_rx.mdrx_arg_v4 = NULL;
1370 	mcip->mci_direct_rx.mdrx_arg_v6 = NULL;
1371 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
1372 
1373 	mcip->mci_unicast_list = NULL;
1374 
1375 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
1376 		mcip->mci_state_flags |= MCIS_IS_VNIC;
1377 
1378 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
1379 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1380 
1381 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
1382 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
1383 
1384 	if (mip->mi_state_flags & MIS_IS_AGGR)
1385 		mcip->mci_state_flags |= MCIS_IS_AGGR_CLIENT;
1386 
1387 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
1388 		datalink_id_t	linkid;
1389 
1390 		ASSERT(name == NULL);
1391 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
1392 		    &linkid)) != 0) {
1393 			goto done;
1394 		}
1395 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
1396 		    NULL, NULL)) != 0) {
1397 			/*
1398 			 * Use mac name if dlmgmtd is not available.
1399 			 */
1400 			if (err == EBADF) {
1401 				(void) strlcpy(mcip->mci_name, mip->mi_name,
1402 				    sizeof (mcip->mci_name));
1403 				err = 0;
1404 			} else {
1405 				goto done;
1406 			}
1407 		}
1408 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
1409 	} else {
1410 		ASSERT(name != NULL);
1411 		if (strlen(name) > MAXNAMELEN) {
1412 			err = EINVAL;
1413 			goto done;
1414 		}
1415 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
1416 	}
1417 
1418 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1419 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1420 
1421 	if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
1422 		mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
1423 
1424 	mac_protect_init(mcip);
1425 
1426 	/* the subflow table will be created dynamically */
1427 	mcip->mci_subflow_tab = NULL;
1428 
1429 	mcip->mci_misc_stat.mms_multircv = 0;
1430 	mcip->mci_misc_stat.mms_brdcstrcv = 0;
1431 	mcip->mci_misc_stat.mms_multixmt = 0;
1432 	mcip->mci_misc_stat.mms_brdcstxmt = 0;
1433 
1434 	/* Create an initial flow */
1435 
1436 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
1437 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
1438 	    FLOW_PRIMARY_MAC, &flent);
1439 	if (err != 0)
1440 		goto done;
1441 	mcip->mci_flent = flent;
1442 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
1443 	flent->fe_mcip = mcip;
1444 
1445 	/*
1446 	 * Place initial creation reference on the flow. This reference
1447 	 * is released in the corresponding delete action viz.
1448 	 * mac_unicast_remove after waiting for all transient refs to
1449 	 * to go away. The wait happens in mac_flow_wait.
1450 	 */
1451 	FLOW_REFHOLD(flent);
1452 
1453 	/*
1454 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
1455 	 * will have the right value for mac_rx_srs_setup().
1456 	 */
1457 	mac_client_add(mcip);
1458 
1459 	mcip->mci_share = 0;
1460 	if (share_desired)
1461 		i_mac_share_alloc(mcip);
1462 
1463 	/*
1464 	 * We will do mimimal datapath setup to allow a MAC client to
1465 	 * transmit or receive non-unicast packets without waiting
1466 	 * for mac_unicast_add.
1467 	 */
1468 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1469 		if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
1470 		    NULL, NULL, B_TRUE, NULL)) != 0) {
1471 			goto done;
1472 		}
1473 	}
1474 
1475 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
1476 	    mcip->mci_mip, mac_client_impl_t *, mcip);
1477 
1478 	*mchp = (mac_client_handle_t)mcip;
1479 	i_mac_perim_exit(mip);
1480 	return (0);
1481 
1482 done:
1483 	i_mac_perim_exit(mip);
1484 	mcip->mci_state_flags = 0;
1485 	mcip->mci_tx_flag = 0;
1486 	kmem_cache_free(mac_client_impl_cache, mcip);
1487 	return (err);
1488 }
1489 
1490 /*
1491  * Close the specified MAC client handle.
1492  */
1493 void
mac_client_close(mac_client_handle_t mch,uint16_t flags)1494 mac_client_close(mac_client_handle_t mch, uint16_t flags)
1495 {
1496 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1497 	mac_impl_t		*mip = mcip->mci_mip;
1498 	flow_entry_t		*flent;
1499 
1500 	i_mac_perim_enter(mip);
1501 
1502 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
1503 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
1504 
1505 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
1506 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
1507 		/*
1508 		 * This is an upper VNIC client initiated operation.
1509 		 * The lower MAC client will be closed by the VNIC driver
1510 		 * when the VNIC is deleted.
1511 		 */
1512 
1513 		i_mac_perim_exit(mip);
1514 		return;
1515 	}
1516 
1517 	/* If we have only setup up minimal datapth setup, tear it down */
1518 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1519 		mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
1520 		    mcip->mci_flent);
1521 		mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
1522 	}
1523 
1524 	/*
1525 	 * Remove the flent associated with the MAC client
1526 	 */
1527 	flent = mcip->mci_flent;
1528 	mcip->mci_flent = NULL;
1529 	FLOW_FINAL_REFRELE(flent);
1530 
1531 	/*
1532 	 * MAC clients must remove the unicast addresses and promisc callbacks
1533 	 * they added before issuing a mac_client_close().
1534 	 */
1535 	ASSERT(mcip->mci_unicast_list == NULL);
1536 	ASSERT(mcip->mci_promisc_list == NULL);
1537 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
1538 
1539 	i_mac_share_free(mcip);
1540 	mac_protect_fini(mcip);
1541 	mac_client_remove(mcip);
1542 
1543 	i_mac_perim_exit(mip);
1544 	mcip->mci_subflow_tab = NULL;
1545 	mcip->mci_state_flags = 0;
1546 	mcip->mci_tx_flag = 0;
1547 	kmem_cache_free(mac_client_impl_cache, mch);
1548 }
1549 
1550 /*
1551  * Set the Rx bypass receive callback and return B_TRUE. Return
1552  * B_FALSE if it's not possible to enable bypass.
1553  */
1554 boolean_t
mac_rx_bypass_set(mac_client_handle_t mch,mac_direct_rx_t rx_fn,void * arg1,boolean_t v6)1555 mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1,
1556     boolean_t v6)
1557 {
1558 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1559 	mac_impl_t		*mip = mcip->mci_mip;
1560 
1561 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1562 
1563 	/*
1564 	 * If the client has more than one VLAN then process packets
1565 	 * through DLS. This should happen only when sun4v vsw is on
1566 	 * the scene.
1567 	 */
1568 	if (mcip->mci_nvids > 1)
1569 		return (B_FALSE);
1570 
1571 	/*
1572 	 * These are not accessed directly in the data path, and hence
1573 	 * don't need any protection
1574 	 */
1575 	if (v6) {
1576 		mcip->mci_direct_rx.mdrx_v6 = rx_fn;
1577 		mcip->mci_direct_rx.mdrx_arg_v6 = arg1;
1578 	} else {
1579 		mcip->mci_direct_rx.mdrx_v4 = rx_fn;
1580 		mcip->mci_direct_rx.mdrx_arg_v4 = arg1;
1581 	}
1582 	return (B_TRUE);
1583 }
1584 
1585 /*
1586  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
1587  */
1588 void
mac_rx_bypass_enable(mac_client_handle_t mch)1589 mac_rx_bypass_enable(mac_client_handle_t mch)
1590 {
1591 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
1592 }
1593 
1594 void
mac_rx_bypass_disable(mac_client_handle_t mch)1595 mac_rx_bypass_disable(mac_client_handle_t mch)
1596 {
1597 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
1598 }
1599 
1600 /*
1601  * Set the receive callback for the specified MAC client. There can be
1602  * at most one such callback per MAC client.
1603  */
1604 void
mac_rx_set(mac_client_handle_t mch,mac_rx_t rx_fn,void * arg)1605 mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
1606 {
1607 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1608 	mac_impl_t	*mip = mcip->mci_mip;
1609 	mac_impl_t	*umip = mcip->mci_upper_mip;
1610 
1611 	/*
1612 	 * Instead of adding an extra set of locks and refcnts in
1613 	 * the datapath at the mac client boundary, we temporarily quiesce
1614 	 * the SRS and related entities. We then change the receive function
1615 	 * without interference from any receive data thread and then reenable
1616 	 * the data flow subsequently.
1617 	 */
1618 	i_mac_perim_enter(mip);
1619 	mac_rx_client_quiesce(mch);
1620 
1621 	mcip->mci_rx_fn = rx_fn;
1622 	mcip->mci_rx_arg = arg;
1623 	mac_rx_client_restart(mch);
1624 
1625 	/*
1626 	 * If we're changing the Rx function on the primary MAC of a VNIC,
1627 	 * make sure any secondary addresses on the VNIC are updated as well.
1628 	 */
1629 	if (umip != NULL) {
1630 		ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
1631 		mac_vnic_secondary_update(umip);
1632 	}
1633 
1634 	i_mac_perim_exit(mip);
1635 }
1636 
1637 /*
1638  * Reset the receive callback for the specified MAC client.
1639  */
1640 void
mac_rx_clear(mac_client_handle_t mch)1641 mac_rx_clear(mac_client_handle_t mch)
1642 {
1643 	mac_rx_set(mch, mac_rx_def, NULL);
1644 }
1645 
1646 void
mac_rx_barrier(mac_client_handle_t mch)1647 mac_rx_barrier(mac_client_handle_t mch)
1648 {
1649 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1650 	mac_impl_t *mip = mcip->mci_mip;
1651 
1652 	i_mac_perim_enter(mip);
1653 
1654 	/* If a RX callback is set, quiesce and restart that datapath */
1655 	if (mcip->mci_rx_fn != mac_rx_def) {
1656 		mac_rx_client_quiesce(mch);
1657 		mac_rx_client_restart(mch);
1658 	}
1659 
1660 	/* If any promisc callbacks are registered, perform a barrier there */
1661 	if (mcip->mci_promisc_list != NULL || mip->mi_promisc_list != NULL) {
1662 		mac_cb_info_t *mcbi =  &mip->mi_promisc_cb_info;
1663 
1664 		mutex_enter(mcbi->mcbi_lockp);
1665 		mac_callback_barrier(mcbi);
1666 		mutex_exit(mcbi->mcbi_lockp);
1667 	}
1668 
1669 	i_mac_perim_exit(mip);
1670 }
1671 
1672 void
mac_secondary_dup(mac_client_handle_t smch,mac_client_handle_t dmch)1673 mac_secondary_dup(mac_client_handle_t smch, mac_client_handle_t dmch)
1674 {
1675 	mac_client_impl_t *smcip = (mac_client_impl_t *)smch;
1676 	mac_client_impl_t *dmcip = (mac_client_impl_t *)dmch;
1677 	flow_entry_t *flent = dmcip->mci_flent;
1678 
1679 	/* This should only be called to setup secondary macs */
1680 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
1681 
1682 	mac_rx_set(dmch, smcip->mci_rx_fn, smcip->mci_rx_arg);
1683 	dmcip->mci_promisc_list = smcip->mci_promisc_list;
1684 
1685 	/*
1686 	 * Duplicate the primary mac resources to the secondary.
1687 	 * Since we already validated the resource controls when setting
1688 	 * them on the primary, we can ignore errors here.
1689 	 */
1690 	(void) mac_resource_ctl_set(dmch, MCIP_RESOURCE_PROPS(smcip));
1691 }
1692 
1693 /*
1694  * Called when removing a secondary MAC. Currently only clears the promisc_list
1695  * since we share the primary mac's promisc_list.
1696  */
1697 void
mac_secondary_cleanup(mac_client_handle_t mch)1698 mac_secondary_cleanup(mac_client_handle_t mch)
1699 {
1700 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1701 	flow_entry_t *flent = mcip->mci_flent;
1702 
1703 	/* This should only be called for secondary macs */
1704 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
1705 	mcip->mci_promisc_list = NULL;
1706 }
1707 
1708 /*
1709  * Walk the MAC client subflow table and updates their priority values.
1710  */
1711 static int
mac_update_subflow_priority_cb(flow_entry_t * flent,void * arg)1712 mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
1713 {
1714 	mac_flow_update_priority(arg, flent);
1715 	return (0);
1716 }
1717 
1718 void
mac_update_subflow_priority(mac_client_impl_t * mcip)1719 mac_update_subflow_priority(mac_client_impl_t *mcip)
1720 {
1721 	(void) mac_flow_walk(mcip->mci_subflow_tab,
1722 	    mac_update_subflow_priority_cb, mcip);
1723 }
1724 
1725 /*
1726  * Modify the TX or RX ring properties. We could either just move around
1727  * rings, i.e add/remove rings given to a client. Or this might cause the
1728  * client to move from hardware based to software or the other way around.
1729  * If we want to reset this property, then we clear the mask, additionally
1730  * if the client was given a non-default group we remove all rings except
1731  * for 1 and give it back to the default group.
1732  */
1733 int
mac_client_set_rings_prop(mac_client_impl_t * mcip,mac_resource_props_t * mrp,mac_resource_props_t * tmrp)1734 mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
1735     mac_resource_props_t *tmrp)
1736 {
1737 	mac_impl_t		*mip = mcip->mci_mip;
1738 	flow_entry_t		*flent = mcip->mci_flent;
1739 	uint8_t			*mac_addr;
1740 	int			err = 0;
1741 	mac_group_t		*defgrp;
1742 	mac_group_t		*group;
1743 	mac_group_t		*ngrp;
1744 	mac_resource_props_t	*cmrp = MCIP_RESOURCE_PROPS(mcip);
1745 	uint_t			ringcnt;
1746 	boolean_t		unspec;
1747 
1748 	if (mcip->mci_share != 0)
1749 		return (EINVAL);
1750 
1751 	if (mrp->mrp_mask & MRP_RX_RINGS) {
1752 		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
1753 		group = flent->fe_rx_ring_group;
1754 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
1755 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
1756 
1757 		/*
1758 		 * No resulting change. If we are resetting on a client on
1759 		 * which there was no rx rings property. For dynamic group
1760 		 * if we are setting the same number of rings already set.
1761 		 * For static group if we are requesting a group again.
1762 		 */
1763 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1764 			if (!(tmrp->mrp_mask & MRP_RX_RINGS))
1765 				return (0);
1766 		} else {
1767 			if (unspec) {
1768 				if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
1769 					return (0);
1770 			} else if (mip->mi_rx_group_type ==
1771 			    MAC_GROUP_TYPE_DYNAMIC) {
1772 				if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
1773 				    !(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
1774 				    mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
1775 					return (0);
1776 				}
1777 			}
1778 		}
1779 		/* Resetting the prop */
1780 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1781 			/*
1782 			 * We will just keep one ring and give others back if
1783 			 * we are not the primary. For the primary we give
1784 			 * all the rings in the default group except the
1785 			 * default ring. If it is a static group, then
1786 			 * we don't do anything, but clear the MRP_RX_RINGS
1787 			 * flag.
1788 			 */
1789 			if (group != defgrp) {
1790 				if (mip->mi_rx_group_type ==
1791 				    MAC_GROUP_TYPE_DYNAMIC) {
1792 					/*
1793 					 * This group has reserved rings
1794 					 * that need to be released now,
1795 					 * so does the group.
1796 					 */
1797 					MAC_RX_RING_RELEASED(mip,
1798 					    group->mrg_cur_count);
1799 					MAC_RX_GRP_RELEASED(mip);
1800 					if ((flent->fe_type &
1801 					    FLOW_PRIMARY_MAC) != 0) {
1802 						if (mip->mi_nactiveclients ==
1803 						    1) {
1804 							(void)
1805 							    mac_rx_switch_group(
1806 							    mcip, group,
1807 							    defgrp);
1808 							return (0);
1809 						} else {
1810 							cmrp->mrp_nrxrings =
1811 							    group->
1812 							    mrg_cur_count +
1813 							    defgrp->
1814 							    mrg_cur_count - 1;
1815 						}
1816 					} else {
1817 						cmrp->mrp_nrxrings = 1;
1818 					}
1819 					(void) mac_group_ring_modify(mcip,
1820 					    group, defgrp);
1821 				} else {
1822 					/*
1823 					 * If this is a static group, we
1824 					 * need to release the group. The
1825 					 * client will remain in the same
1826 					 * group till some other client
1827 					 * needs this group.
1828 					 */
1829 					MAC_RX_GRP_RELEASED(mip);
1830 				}
1831 			/* Let check if we can give this an excl group */
1832 			} else if (group == defgrp) {
1833 				/*
1834 				 * If multiple clients share an
1835 				 * address then they must stay on the
1836 				 * default group.
1837 				 */
1838 				if (mac_check_macaddr_shared(mcip->mci_unicast))
1839 					return (0);
1840 
1841 				ngrp = mac_reserve_rx_group(mcip, mac_addr,
1842 				    B_TRUE);
1843 				/* Couldn't give it a group, that's fine */
1844 				if (ngrp == NULL)
1845 					return (0);
1846 				/* Switch to H/W */
1847 				if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
1848 				    0) {
1849 					mac_stop_group(ngrp);
1850 					return (0);
1851 				}
1852 			}
1853 			/*
1854 			 * If the client is in the default group, we will
1855 			 * just clear the MRP_RX_RINGS and leave it as
1856 			 * it rather than look for an exclusive group
1857 			 * for it.
1858 			 */
1859 			return (0);
1860 		}
1861 
1862 		if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
1863 			/*
1864 			 * We are requesting Rx rings. Try to reserve
1865 			 * a non-default group.
1866 			 *
1867 			 * If multiple clients share an address then
1868 			 * they must stay on the default group.
1869 			 */
1870 			if (mac_check_macaddr_shared(mcip->mci_unicast))
1871 				return (EINVAL);
1872 
1873 			ngrp = mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
1874 			if (ngrp == NULL)
1875 				return (ENOSPC);
1876 
1877 			/* Switch to H/W */
1878 			if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
1879 				mac_release_rx_group(mcip, ngrp);
1880 				return (ENOSPC);
1881 			}
1882 			MAC_RX_GRP_RESERVED(mip);
1883 			if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
1884 				MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
1885 		} else if (group != defgrp && !unspec &&
1886 		    mrp->mrp_nrxrings == 0) {
1887 			/* Switch to S/W */
1888 			ringcnt = group->mrg_cur_count;
1889 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
1890 				return (ENOSPC);
1891 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1892 				MAC_RX_GRP_RELEASED(mip);
1893 				if (mip->mi_rx_group_type ==
1894 				    MAC_GROUP_TYPE_DYNAMIC) {
1895 					MAC_RX_RING_RELEASED(mip, ringcnt);
1896 				}
1897 			}
1898 		} else if (group != defgrp && mip->mi_rx_group_type ==
1899 		    MAC_GROUP_TYPE_DYNAMIC) {
1900 			ringcnt = group->mrg_cur_count;
1901 			err = mac_group_ring_modify(mcip, group, defgrp);
1902 			if (err != 0)
1903 				return (err);
1904 			/*
1905 			 * Update the accounting. If this group
1906 			 * already had explicitly reserved rings,
1907 			 * we need to update the rings based on
1908 			 * the new ring count. If this group
1909 			 * had not explicitly reserved rings,
1910 			 * then we just reserve the rings asked for
1911 			 * and reserve the group.
1912 			 */
1913 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1914 				if (ringcnt > group->mrg_cur_count) {
1915 					MAC_RX_RING_RELEASED(mip,
1916 					    ringcnt - group->mrg_cur_count);
1917 				} else {
1918 					MAC_RX_RING_RESERVED(mip,
1919 					    group->mrg_cur_count - ringcnt);
1920 				}
1921 			} else {
1922 				MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
1923 				MAC_RX_GRP_RESERVED(mip);
1924 			}
1925 		}
1926 	}
1927 	if (mrp->mrp_mask & MRP_TX_RINGS) {
1928 		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
1929 		group = flent->fe_tx_ring_group;
1930 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
1931 
1932 		/*
1933 		 * For static groups we only allow rings=0 or resetting the
1934 		 * rings property.
1935 		 */
1936 		if (mrp->mrp_ntxrings > 0 &&
1937 		    mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
1938 			return (ENOTSUP);
1939 		}
1940 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1941 			if (!(tmrp->mrp_mask & MRP_TX_RINGS))
1942 				return (0);
1943 		} else {
1944 			if (unspec) {
1945 				if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
1946 					return (0);
1947 			} else if (mip->mi_tx_group_type ==
1948 			    MAC_GROUP_TYPE_DYNAMIC) {
1949 				if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
1950 				    !(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
1951 				    mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
1952 					return (0);
1953 				}
1954 			}
1955 		}
1956 		/* Resetting the prop */
1957 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1958 			if (group != defgrp) {
1959 				if (mip->mi_tx_group_type ==
1960 				    MAC_GROUP_TYPE_DYNAMIC) {
1961 					ringcnt = group->mrg_cur_count;
1962 					if ((flent->fe_type &
1963 					    FLOW_PRIMARY_MAC) != 0) {
1964 						mac_tx_client_quiesce(
1965 						    (mac_client_handle_t)
1966 						    mcip);
1967 						mac_tx_switch_group(mcip,
1968 						    group, defgrp);
1969 						mac_tx_client_restart(
1970 						    (mac_client_handle_t)
1971 						    mcip);
1972 						MAC_TX_GRP_RELEASED(mip);
1973 						MAC_TX_RING_RELEASED(mip,
1974 						    ringcnt);
1975 						return (0);
1976 					}
1977 					cmrp->mrp_ntxrings = 1;
1978 					(void) mac_group_ring_modify(mcip,
1979 					    group, defgrp);
1980 					/*
1981 					 * This group has reserved rings
1982 					 * that need to be released now.
1983 					 */
1984 					MAC_TX_RING_RELEASED(mip, ringcnt);
1985 				}
1986 				/*
1987 				 * If this is a static group, we
1988 				 * need to release the group. The
1989 				 * client will remain in the same
1990 				 * group till some other client
1991 				 * needs this group.
1992 				 */
1993 				MAC_TX_GRP_RELEASED(mip);
1994 			} else if (group == defgrp &&
1995 			    (flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
1996 				ngrp = mac_reserve_tx_group(mcip, B_TRUE);
1997 				if (ngrp == NULL)
1998 					return (0);
1999 				mac_tx_client_quiesce(
2000 				    (mac_client_handle_t)mcip);
2001 				mac_tx_switch_group(mcip, defgrp, ngrp);
2002 				mac_tx_client_restart(
2003 				    (mac_client_handle_t)mcip);
2004 			}
2005 			/*
2006 			 * If the client is in the default group, we will
2007 			 * just clear the MRP_TX_RINGS and leave it as
2008 			 * it rather than look for an exclusive group
2009 			 * for it.
2010 			 */
2011 			return (0);
2012 		}
2013 
2014 		/* Switch to H/W */
2015 		if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
2016 			ngrp = mac_reserve_tx_group(mcip, B_TRUE);
2017 			if (ngrp == NULL)
2018 				return (ENOSPC);
2019 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
2020 			mac_tx_switch_group(mcip, defgrp, ngrp);
2021 			mac_tx_client_restart((mac_client_handle_t)mcip);
2022 			MAC_TX_GRP_RESERVED(mip);
2023 			if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
2024 				MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
2025 		/* Switch to S/W */
2026 		} else if (group != defgrp && !unspec &&
2027 		    mrp->mrp_ntxrings == 0) {
2028 			/* Switch to S/W */
2029 			ringcnt = group->mrg_cur_count;
2030 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
2031 			mac_tx_switch_group(mcip, group, defgrp);
2032 			mac_tx_client_restart((mac_client_handle_t)mcip);
2033 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
2034 				MAC_TX_GRP_RELEASED(mip);
2035 				if (mip->mi_tx_group_type ==
2036 				    MAC_GROUP_TYPE_DYNAMIC) {
2037 					MAC_TX_RING_RELEASED(mip, ringcnt);
2038 				}
2039 			}
2040 		} else if (group != defgrp && mip->mi_tx_group_type ==
2041 		    MAC_GROUP_TYPE_DYNAMIC) {
2042 			ringcnt = group->mrg_cur_count;
2043 			err = mac_group_ring_modify(mcip, group, defgrp);
2044 			if (err != 0)
2045 				return (err);
2046 			/*
2047 			 * Update the accounting. If this group
2048 			 * already had explicitly reserved rings,
2049 			 * we need to update the rings based on
2050 			 * the new ring count. If this group
2051 			 * had not explicitly reserved rings,
2052 			 * then we just reserve the rings asked for
2053 			 * and reserve the group.
2054 			 */
2055 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
2056 				if (ringcnt > group->mrg_cur_count) {
2057 					MAC_TX_RING_RELEASED(mip,
2058 					    ringcnt - group->mrg_cur_count);
2059 				} else {
2060 					MAC_TX_RING_RESERVED(mip,
2061 					    group->mrg_cur_count - ringcnt);
2062 				}
2063 			} else {
2064 				MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
2065 				MAC_TX_GRP_RESERVED(mip);
2066 			}
2067 		}
2068 	}
2069 	return (0);
2070 }
2071 
2072 /*
2073  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
2074  * to initialize the cpu and resource control structure in the
2075  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
2076  * properties before the flow entry for the unicast address was created).
2077  */
2078 static int
mac_resource_ctl_set(mac_client_handle_t mch,mac_resource_props_t * mrp)2079 mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
2080 {
2081 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2082 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
2083 	mac_impl_t		*umip = mcip->mci_upper_mip;
2084 	int			err = 0;
2085 	flow_entry_t		*flent = mcip->mci_flent;
2086 	mac_resource_props_t	*omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
2087 
2088 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2089 
2090 	err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
2091 	    mcip->mci_upper_mip : mip, mrp);
2092 	if (err != 0)
2093 		return (err);
2094 
2095 	/*
2096 	 * Copy over the existing properties since mac_update_resources
2097 	 * will modify the client's mrp. Currently, the saved property
2098 	 * is used to determine the difference between existing and
2099 	 * modified rings property.
2100 	 */
2101 	omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
2102 	bcopy(nmrp, omrp, sizeof (*omrp));
2103 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
2104 	if (MCIP_DATAPATH_SETUP(mcip)) {
2105 		/*
2106 		 * We support rings only for primary client when there are
2107 		 * multiple clients sharing the same MAC address (e.g. VLAN).
2108 		 */
2109 		if (mrp->mrp_mask & MRP_RX_RINGS ||
2110 		    mrp->mrp_mask & MRP_TX_RINGS) {
2111 
2112 			if ((err = mac_client_set_rings_prop(mcip, mrp,
2113 			    omrp)) != 0) {
2114 				if (omrp->mrp_mask & MRP_RX_RINGS) {
2115 					nmrp->mrp_mask |= MRP_RX_RINGS;
2116 					nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
2117 				} else {
2118 					nmrp->mrp_mask &= ~MRP_RX_RINGS;
2119 					nmrp->mrp_nrxrings = 0;
2120 				}
2121 				if (omrp->mrp_mask & MRP_TX_RINGS) {
2122 					nmrp->mrp_mask |= MRP_TX_RINGS;
2123 					nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
2124 				} else {
2125 					nmrp->mrp_mask &= ~MRP_TX_RINGS;
2126 					nmrp->mrp_ntxrings = 0;
2127 				}
2128 				if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
2129 					omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
2130 				else
2131 					omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
2132 
2133 				if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
2134 					omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
2135 				else
2136 					omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
2137 				kmem_free(omrp, sizeof (*omrp));
2138 				return (err);
2139 			}
2140 
2141 			/*
2142 			 * If we modified the rings property of the primary
2143 			 * we need to update the property fields of its
2144 			 * VLANs as they inherit the primary's properites.
2145 			 */
2146 			if (mac_is_primary_client(mcip)) {
2147 				mac_set_prim_vlan_rings(mip,
2148 				    MCIP_RESOURCE_PROPS(mcip));
2149 			}
2150 		}
2151 		/*
2152 		 * We have to set this prior to calling mac_flow_modify.
2153 		 */
2154 		if (mrp->mrp_mask & MRP_PRIORITY) {
2155 			if (mrp->mrp_priority == MPL_RESET) {
2156 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2157 				    MPL_LINK_DEFAULT);
2158 			} else {
2159 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2160 				    mrp->mrp_priority);
2161 			}
2162 		}
2163 
2164 		mac_flow_modify(mip->mi_flow_tab, flent, mrp);
2165 		if (mrp->mrp_mask & MRP_PRIORITY)
2166 			mac_update_subflow_priority(mcip);
2167 
2168 		/* Apply these resource settings to any secondary macs */
2169 		if (umip != NULL) {
2170 			ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
2171 			mac_vnic_secondary_update(umip);
2172 		}
2173 	}
2174 	kmem_free(omrp, sizeof (*omrp));
2175 	return (0);
2176 }
2177 
2178 static int
mac_unicast_flow_create(mac_client_impl_t * mcip,uint8_t * mac_addr,uint16_t vid,boolean_t is_primary,boolean_t first_flow,flow_entry_t ** flent,mac_resource_props_t * mrp)2179 mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
2180     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
2181     flow_entry_t **flent, mac_resource_props_t *mrp)
2182 {
2183 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
2184 	flow_desc_t	flow_desc;
2185 	char		flowname[MAXFLOWNAMELEN];
2186 	int		err;
2187 	uint_t		flent_flags;
2188 
2189 	/*
2190 	 * First unicast address being added, create a new flow
2191 	 * for that MAC client.
2192 	 */
2193 	bzero(&flow_desc, sizeof (flow_desc));
2194 
2195 	ASSERT(mac_addr != NULL ||
2196 	    (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
2197 	if (mac_addr != NULL) {
2198 		flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
2199 		bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
2200 	}
2201 	flow_desc.fd_mask = FLOW_LINK_DST;
2202 	if (vid != 0) {
2203 		flow_desc.fd_vid = vid;
2204 		flow_desc.fd_mask |= FLOW_LINK_VID;
2205 	}
2206 
2207 	/*
2208 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
2209 	 * and FLOW_VNIC. Even though they're a hack inherited
2210 	 * from the SRS code, we'll keep them for now. They're currently
2211 	 * consumed by mac_datapath_setup() to create the SRS.
2212 	 * That code should be eventually moved out of
2213 	 * mac_datapath_setup() and moved to a mac_srs_create()
2214 	 * function of some sort to keep things clean.
2215 	 *
2216 	 * Also, there's no reason why the SRS for the primary MAC
2217 	 * client should be different than any other MAC client. Until
2218 	 * this is cleaned-up, we support only one MAC unicast address
2219 	 * per client.
2220 	 *
2221 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
2222 	 * FLOW_VNIC for everything else.
2223 	 */
2224 	if (is_primary)
2225 		flent_flags = FLOW_PRIMARY_MAC;
2226 	else
2227 		flent_flags = FLOW_VNIC_MAC;
2228 
2229 	/*
2230 	 * For the first flow we use the MAC client's name - mci_name, for
2231 	 * subsequent ones we just create a name with the VID. This is
2232 	 * so that we can add these flows to the same flow table. This is
2233 	 * fine as the flow name (except for the one with the MAC client's
2234 	 * name) is not visible. When the first flow is removed, we just replace
2235 	 * its fdesc with another from the list, so we will still retain the
2236 	 * flent with the MAC client's flow name.
2237 	 */
2238 	if (first_flow) {
2239 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
2240 	} else {
2241 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
2242 		flent_flags = FLOW_NO_STATS;
2243 	}
2244 
2245 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
2246 	    flent_flags, flent)) != 0)
2247 		return (err);
2248 
2249 	mac_misc_stat_create(*flent);
2250 	FLOW_MARK(*flent, FE_INCIPIENT);
2251 	(*flent)->fe_mcip = mcip;
2252 
2253 	/*
2254 	 * Place initial creation reference on the flow. This reference
2255 	 * is released in the corresponding delete action viz.
2256 	 * mac_unicast_remove after waiting for all transient refs to
2257 	 * to go away. The wait happens in mac_flow_wait.
2258 	 * We have already held the reference in mac_client_open().
2259 	 */
2260 	if (!first_flow)
2261 		FLOW_REFHOLD(*flent);
2262 	return (0);
2263 }
2264 
2265 /* Refresh the multicast grouping for this VID. */
2266 int
mac_client_update_mcast(void * arg,boolean_t add,const uint8_t * addrp)2267 mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
2268 {
2269 	flow_entry_t		*flent = arg;
2270 	mac_client_impl_t	*mcip = flent->fe_mcip;
2271 	uint16_t		vid;
2272 	flow_desc_t		flow_desc;
2273 
2274 	mac_flow_get_desc(flent, &flow_desc);
2275 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
2276 	    flow_desc.fd_vid : VLAN_ID_NONE;
2277 
2278 	/*
2279 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
2280 	 * we want to add/remove for this specific vid.
2281 	 */
2282 	if (add) {
2283 		return (mac_bcast_add(mcip, addrp, vid,
2284 		    MAC_ADDRTYPE_MULTICAST));
2285 	} else {
2286 		mac_bcast_delete(mcip, addrp, vid);
2287 		return (0);
2288 	}
2289 }
2290 
2291 static void
mac_update_single_active_client(mac_impl_t * mip)2292 mac_update_single_active_client(mac_impl_t *mip)
2293 {
2294 	mac_client_impl_t *client = NULL;
2295 
2296 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2297 
2298 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2299 	if (mip->mi_nactiveclients == 1) {
2300 		/*
2301 		 * Find the one active MAC client from the list of MAC
2302 		 * clients. The active MAC client has at least one
2303 		 * unicast address.
2304 		 */
2305 		for (client = mip->mi_clients_list; client != NULL;
2306 		    client = client->mci_client_next) {
2307 			if (client->mci_unicast_list != NULL)
2308 				break;
2309 		}
2310 		ASSERT(client != NULL);
2311 	}
2312 
2313 	/*
2314 	 * mi_single_active_client is protected by the MAC impl's read/writer
2315 	 * lock, which allows mac_rx() to check the value of that pointer
2316 	 * as a reader.
2317 	 */
2318 	mip->mi_single_active_client = client;
2319 	rw_exit(&mip->mi_rw_lock);
2320 }
2321 
2322 /*
2323  * Set up the data path. Called from i_mac_unicast_add after having
2324  * done all the validations including making sure this is an active
2325  * client (i.e that is ready to process packets.)
2326  */
2327 static int
mac_client_datapath_setup(mac_client_impl_t * mcip,uint16_t vid,uint8_t * mac_addr,mac_resource_props_t * mrp,boolean_t isprimary,mac_unicast_impl_t * muip)2328 mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
2329     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
2330     mac_unicast_impl_t *muip)
2331 {
2332 	mac_impl_t	*mip = mcip->mci_mip;
2333 	boolean_t	mac_started = B_FALSE;
2334 	boolean_t	bcast_added = B_FALSE;
2335 	boolean_t	nactiveclients_added = B_FALSE;
2336 	flow_entry_t	*flent;
2337 	int		err = 0;
2338 	boolean_t	no_unicast;
2339 
2340 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2341 
2342 	if ((err = mac_start((mac_handle_t)mip)) != 0)
2343 		goto bail;
2344 
2345 	mac_started = B_TRUE;
2346 
2347 	/* add the MAC client to the broadcast address group by default */
2348 	if (mip->mi_type->mt_brdcst_addr != NULL) {
2349 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
2350 		    MAC_ADDRTYPE_BROADCAST);
2351 		if (err != 0)
2352 			goto bail;
2353 		bcast_added = B_TRUE;
2354 	}
2355 
2356 	/*
2357 	 * If this is the first unicast address addition for this
2358 	 * client, reuse the pre-allocated larval flow entry associated with
2359 	 * the MAC client.
2360 	 */
2361 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
2362 
2363 	/* We are configuring the unicast flow now */
2364 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2365 
2366 		if (mrp != NULL) {
2367 			MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2368 			    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
2369 			    MPL_LINK_DEFAULT);
2370 		}
2371 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2372 		    isprimary, B_TRUE, &flent, mrp)) != 0)
2373 			goto bail;
2374 
2375 		mip->mi_nactiveclients++;
2376 		nactiveclients_added = B_TRUE;
2377 
2378 		/*
2379 		 * This will allocate the RX ring group if possible for the
2380 		 * flow and program the software classifier as needed.
2381 		 */
2382 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
2383 			goto bail;
2384 
2385 		if (no_unicast)
2386 			goto done_setup;
2387 		/*
2388 		 * The unicast MAC address must have been added successfully.
2389 		 */
2390 		ASSERT(mcip->mci_unicast != NULL);
2391 
2392 		/*
2393 		 * Push down the sub-flows that were defined on this link
2394 		 * hitherto. The flows are added to the active flow table
2395 		 * and SRS, softrings etc. are created as needed.
2396 		 */
2397 		mac_link_init_flows((mac_client_handle_t)mcip);
2398 	} else {
2399 		mac_address_t *map = mcip->mci_unicast;
2400 
2401 		ASSERT(!no_unicast);
2402 		/*
2403 		 * A unicast flow already exists for that MAC client
2404 		 * so this flow must be the same MAC address but with
2405 		 * a different VID. It has been checked by
2406 		 * mac_addr_in_use().
2407 		 *
2408 		 * We will use the SRS etc. from the initial
2409 		 * mci_flent. We don't need to create a kstat for
2410 		 * this, as except for the fdesc, everything will be
2411 		 * used from the first flent.
2412 		 *
2413 		 * The only time we should see multiple flents on the
2414 		 * same MAC client is on the sun4v vsw. If we removed
2415 		 * that code we should be able to remove the entire
2416 		 * notion of multiple flents on a MAC client (this
2417 		 * doesn't affect sub/user flows because they have
2418 		 * their own list unrelated to mci_flent_list).
2419 		 */
2420 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
2421 			err = EINVAL;
2422 			goto bail;
2423 		}
2424 
2425 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2426 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
2427 			goto bail;
2428 		}
2429 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
2430 			FLOW_FINAL_REFRELE(flent);
2431 			goto bail;
2432 		}
2433 
2434 		/* update the multicast group for this vid */
2435 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
2436 		    (void *)flent, B_TRUE);
2437 
2438 	}
2439 
2440 	/* populate the shared MAC address */
2441 	muip->mui_map = mcip->mci_unicast;
2442 
2443 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
2444 	muip->mui_next = mcip->mci_unicast_list;
2445 	mcip->mci_unicast_list = muip;
2446 	rw_exit(&mcip->mci_rw_lock);
2447 
2448 done_setup:
2449 	/*
2450 	 * First add the flent to the flow list of this mcip. Then set
2451 	 * the mip's mi_single_active_client if needed. The Rx path assumes
2452 	 * that mip->mi_single_active_client will always have an associated
2453 	 * flent.
2454 	 */
2455 	mac_client_add_to_flow_list(mcip, flent);
2456 	if (nactiveclients_added)
2457 		mac_update_single_active_client(mip);
2458 	/*
2459 	 * Trigger a renegotiation of the capabilities when the number of
2460 	 * active clients changes from 1 to 2, since some of the capabilities
2461 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
2462 	 * to all the MAC clients whenever physical link is DOWN.
2463 	 */
2464 	if (mip->mi_nactiveclients == 2) {
2465 		mac_capab_update((mac_handle_t)mip);
2466 		mac_virtual_link_update(mip);
2467 	}
2468 	/*
2469 	 * Now that the setup is complete, clear the INCIPIENT flag.
2470 	 * The flag was set to avoid incoming packets seeing inconsistent
2471 	 * structures while the setup was in progress. Clear the mci_tx_flag
2472 	 * by calling mac_tx_client_block. It is possible that
2473 	 * mac_unicast_remove was called prior to this mac_unicast_add which
2474 	 * could have set the MCI_TX_QUIESCE flag.
2475 	 */
2476 	if (flent->fe_rx_ring_group != NULL)
2477 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
2478 	FLOW_UNMARK(flent, FE_INCIPIENT);
2479 
2480 	/*
2481 	 * If this is an aggr port client, don't enable the flow's
2482 	 * datapath at this stage. Otherwise, bcast traffic could
2483 	 * arrive while the aggr port is in the process of
2484 	 * initializing. Instead, the flow's datapath is started later
2485 	 * when mac_client_set_flow_cb() is called.
2486 	 */
2487 	if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0)
2488 		FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
2489 
2490 	mac_tx_client_unblock(mcip);
2491 	return (0);
2492 bail:
2493 	if (bcast_added)
2494 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
2495 
2496 	if (nactiveclients_added)
2497 		mip->mi_nactiveclients--;
2498 
2499 	if (mac_started)
2500 		mac_stop((mac_handle_t)mip);
2501 
2502 	return (err);
2503 }
2504 
2505 /*
2506  * Return the passive primary MAC client, if present. The passive client is
2507  * a stand-by client that has the same unicast address as another that is
2508  * currenly active. Once the active client goes away, the passive client
2509  * becomes active.
2510  */
2511 static mac_client_impl_t *
mac_get_passive_primary_client(mac_impl_t * mip)2512 mac_get_passive_primary_client(mac_impl_t *mip)
2513 {
2514 	mac_client_impl_t	*mcip;
2515 
2516 	for (mcip = mip->mi_clients_list; mcip != NULL;
2517 	    mcip = mcip->mci_client_next) {
2518 		if (mac_is_primary_client(mcip) &&
2519 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2520 			return (mcip);
2521 		}
2522 	}
2523 	return (NULL);
2524 }
2525 
2526 /*
2527  * Add a new unicast address to the MAC client.
2528  *
2529  * The MAC address can be specified either by value, or the MAC client can
2530  * specify that it wants to use the primary MAC address of the underlying MAC.
2531  * See the introductory comments at the beginning of this file for more
2532  * information on primary MAC addresses.
2533  *
2534  * Note also the tuple (MAC address, VID) must be unique for the MAC clients
2535  * defined on top of the same underlying MAC instance.
2536  *
2537  * In no case can a client use the PVID for the MAC, if the MAC has one set.
2538  */
2539 int
i_mac_unicast_add(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag)2540 i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2541     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2542 {
2543 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2544 	mac_impl_t		*mip = mcip->mci_mip;
2545 	int			err;
2546 	uint_t			mac_len = mip->mi_type->mt_addr_length;
2547 	boolean_t		fastpath_disabled = B_FALSE;
2548 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
2549 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
2550 	mac_resource_props_t	*mrp;
2551 	boolean_t		passive_client = B_FALSE;
2552 	mac_unicast_impl_t	*muip;
2553 	boolean_t		is_vnic_primary =
2554 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
2555 
2556 	/*
2557 	 * When the VID is non-zero the underlying MAC cannot be a
2558 	 * VNIC. I.e., dladm create-vlan cannot take a VNIC as
2559 	 * argument, only the primary MAC client.
2560 	 */
2561 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != VLAN_ID_NONE)));
2562 
2563 	*diag = MAC_DIAG_NONE;
2564 
2565 	/*
2566 	 * Can't unicast add if the client asked only for minimal datapath
2567 	 * setup.
2568 	 */
2569 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
2570 		return (ENOTSUP);
2571 
2572 	/*
2573 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
2574 	 * No client may use it.
2575 	 */
2576 	if (mip->mi_pvid != VLAN_ID_NONE && vid == mip->mi_pvid)
2577 		return (EBUSY);
2578 
2579 	/*
2580 	 * Check whether it's the primary client and flag it.
2581 	 */
2582 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2583 	    vid == VLAN_ID_NONE)
2584 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
2585 
2586 	/*
2587 	 * is_vnic_primary is true when we come here as a VLAN VNIC
2588 	 * which uses the primary MAC client's address but with a non-zero
2589 	 * VID. In this case the MAC address is not specified by an upper
2590 	 * MAC client.
2591 	 */
2592 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2593 	    !is_vnic_primary) {
2594 		/*
2595 		 * The address is being set by the upper MAC client
2596 		 * of a VNIC. The MAC address was already set by the
2597 		 * VNIC driver during VNIC creation.
2598 		 *
2599 		 * Note: a VNIC has only one MAC address. We return
2600 		 * the MAC unicast address handle of the lower MAC client
2601 		 * corresponding to the VNIC. We allocate a new entry
2602 		 * which is flagged appropriately, so that mac_unicast_remove()
2603 		 * doesn't attempt to free the original entry that
2604 		 * was allocated by the VNIC driver.
2605 		 */
2606 		ASSERT(mcip->mci_unicast != NULL);
2607 
2608 		/* Check for VLAN flags, if present */
2609 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2610 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2611 
2612 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2613 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2614 
2615 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2616 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2617 
2618 		/*
2619 		 * Ensure that the primary unicast address of the VNIC
2620 		 * is added only once unless we have the
2621 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
2622 		 * a passive MAC client).
2623 		 */
2624 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
2625 			if ((mcip->mci_flags &
2626 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2627 			    (mcip->mci_flags &
2628 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2629 				return (EBUSY);
2630 			}
2631 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2632 			passive_client = B_TRUE;
2633 		}
2634 
2635 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
2636 
2637 		/*
2638 		 * Create a handle for vid 0.
2639 		 */
2640 		ASSERT(vid == VLAN_ID_NONE);
2641 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2642 		muip->mui_vid = vid;
2643 		*mah = (mac_unicast_handle_t)muip;
2644 		/*
2645 		 * This will be used by the caller to defer setting the
2646 		 * rx functions.
2647 		 */
2648 		if (passive_client)
2649 			return (EAGAIN);
2650 		return (0);
2651 	}
2652 
2653 	/* primary MAC clients cannot be opened on top of anchor VNICs */
2654 	if ((is_vnic_primary || is_primary) &&
2655 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
2656 		return (ENXIO);
2657 	}
2658 
2659 	/*
2660 	 * If this is a VNIC/VLAN, disable softmac fast-path. This is
2661 	 * only relevant to legacy devices which use softmac to
2662 	 * interface with GLDv3.
2663 	 */
2664 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2665 		err = mac_fastpath_disable((mac_handle_t)mip);
2666 		if (err != 0)
2667 			return (err);
2668 		fastpath_disabled = B_TRUE;
2669 	}
2670 
2671 	/*
2672 	 * Return EBUSY if:
2673 	 *  - there is an exclusively active mac client exists.
2674 	 *  - this is an exclusive active mac client but
2675 	 *	a. there is already active mac clients exist, or
2676 	 *	b. fastpath streams are already plumbed on this legacy device
2677 	 *  - the mac creator has disallowed active mac clients.
2678 	 */
2679 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
2680 		if (fastpath_disabled)
2681 			mac_fastpath_enable((mac_handle_t)mip);
2682 		return (EBUSY);
2683 	}
2684 
2685 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2686 		ASSERT(!fastpath_disabled);
2687 		if (mip->mi_nactiveclients != 0)
2688 			return (EBUSY);
2689 
2690 		if ((mip->mi_state_flags & MIS_LEGACY) &&
2691 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
2692 			return (EBUSY);
2693 		}
2694 		mip->mi_state_flags |= MIS_EXCLUSIVE;
2695 	}
2696 
2697 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
2698 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
2699 	    MCIS_IS_AGGR_PORT))) {
2700 		/*
2701 		 * Apply the property cached in the mac_impl_t to the primary
2702 		 * mac client. If the mac client is a VNIC or an aggregation
2703 		 * port, its property should be set in the mcip when the
2704 		 * VNIC/aggr was created.
2705 		 */
2706 		mac_get_resources((mac_handle_t)mip, mrp);
2707 		(void) mac_client_set_resources(mch, mrp);
2708 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2709 		/*
2710 		 * This is a VLAN client sharing the address of the
2711 		 * primary MAC client; i.e., one created via dladm
2712 		 * create-vlan. We don't support specifying ring
2713 		 * properties for this type of client as it inherits
2714 		 * these from the primary MAC client.
2715 		 */
2716 		if (is_vnic_primary) {
2717 			mac_resource_props_t	*vmrp;
2718 
2719 			vmrp = MCIP_RESOURCE_PROPS(mcip);
2720 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
2721 			    vmrp->mrp_mask & MRP_TX_RINGS) {
2722 				if (fastpath_disabled)
2723 					mac_fastpath_enable((mac_handle_t)mip);
2724 				kmem_free(mrp, sizeof (*mrp));
2725 				return (ENOTSUP);
2726 			}
2727 			/*
2728 			 * Additionally we also need to inherit any
2729 			 * rings property from the MAC.
2730 			 */
2731 			mac_get_resources((mac_handle_t)mip, mrp);
2732 			if (mrp->mrp_mask & MRP_RX_RINGS) {
2733 				vmrp->mrp_mask |= MRP_RX_RINGS;
2734 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
2735 			}
2736 			if (mrp->mrp_mask & MRP_TX_RINGS) {
2737 				vmrp->mrp_mask |= MRP_TX_RINGS;
2738 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
2739 			}
2740 		}
2741 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
2742 	}
2743 
2744 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2745 	muip->mui_vid = vid;
2746 
2747 	if (is_primary || is_vnic_primary) {
2748 		mac_addr = mip->mi_addr;
2749 	} else {
2750 
2751 		/*
2752 		 * Verify the validity of the specified MAC addresses value.
2753 		 */
2754 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
2755 			*diag = MAC_DIAG_MACADDR_INVALID;
2756 			err = EINVAL;
2757 			goto bail_out;
2758 		}
2759 
2760 		/*
2761 		 * Make sure that the specified MAC address is different
2762 		 * than the unicast MAC address of the underlying NIC.
2763 		 */
2764 		if (bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
2765 			*diag = MAC_DIAG_MACADDR_NIC;
2766 			err = EINVAL;
2767 			goto bail_out;
2768 		}
2769 	}
2770 
2771 	/*
2772 	 * Set the flags here so that if this is a passive client, we
2773 	 * can return and set it when we call mac_client_datapath_setup
2774 	 * when this becomes the active client. If we defer to using these
2775 	 * flags to mac_client_datapath_setup, then for a passive client,
2776 	 * we'd have to store the flags somewhere (probably fe_flags)
2777 	 * and then use it.
2778 	 */
2779 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2780 		if (is_unicast_hw) {
2781 			/*
2782 			 * The client requires a hardware MAC address slot
2783 			 * for that unicast address. Since we support only
2784 			 * one unicast MAC address per client, flag the
2785 			 * MAC client itself.
2786 			 */
2787 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
2788 		}
2789 
2790 		/* Check for VLAN flags, if present */
2791 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2792 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2793 
2794 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2795 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2796 
2797 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2798 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2799 	} else {
2800 		/*
2801 		 * Assert that the specified flags are consistent with the
2802 		 * flags specified by previous calls to mac_unicast_add().
2803 		 */
2804 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
2805 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
2806 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
2807 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
2808 
2809 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
2810 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
2811 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
2812 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
2813 
2814 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
2815 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
2816 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
2817 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
2818 
2819 		/*
2820 		 * Make sure the client is consistent about its requests
2821 		 * for MAC addresses. I.e. all requests from the clients
2822 		 * must have the MAC_UNICAST_HW flag set or clear.
2823 		 */
2824 		if (((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
2825 		    !is_unicast_hw) ||
2826 		    ((mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
2827 		    is_unicast_hw)) {
2828 			err = EINVAL;
2829 			goto bail_out;
2830 		}
2831 	}
2832 	/*
2833 	 * Make sure the MAC address is not already used by
2834 	 * another MAC client defined on top of the same
2835 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
2836 	 * set when we allow a passive client to be present which will
2837 	 * be activated when the currently active client goes away - this
2838 	 * works only with primary addresses.
2839 	 */
2840 	if (mac_addr_in_use(mip, mac_addr, vid)) {
2841 		/*
2842 		 * Must have set the multiple primary address flag when
2843 		 * we did a mac_client_open AND this should be a primary
2844 		 * MAC client AND there should not already be a passive
2845 		 * primary. If all is true then we let this succeed
2846 		 * even if the address is a dup.
2847 		 */
2848 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2849 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
2850 		    mac_get_passive_primary_client(mip) != NULL) {
2851 			*diag = MAC_DIAG_MACADDR_INUSE;
2852 			err = EEXIST;
2853 			goto bail_out;
2854 		}
2855 		ASSERT((mcip->mci_flags &
2856 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
2857 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2858 		kmem_free(mrp, sizeof (*mrp));
2859 
2860 		/*
2861 		 * Stash the unicast address handle, we will use it when
2862 		 * we set up the passive client.
2863 		 */
2864 		mcip->mci_p_unicast_list = muip;
2865 		*mah = (mac_unicast_handle_t)muip;
2866 		return (0);
2867 	}
2868 
2869 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
2870 	    is_primary || is_vnic_primary, muip);
2871 	if (err != 0)
2872 		goto bail_out;
2873 
2874 	kmem_free(mrp, sizeof (*mrp));
2875 	*mah = (mac_unicast_handle_t)muip;
2876 	return (0);
2877 
2878 bail_out:
2879 	if (fastpath_disabled)
2880 		mac_fastpath_enable((mac_handle_t)mip);
2881 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2882 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2883 		if (mip->mi_state_flags & MIS_LEGACY) {
2884 			mip->mi_capab_legacy.ml_active_clear(
2885 			    mip->mi_driver);
2886 		}
2887 	}
2888 	kmem_free(mrp, sizeof (*mrp));
2889 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2890 	return (err);
2891 }
2892 
2893 /*
2894  * Wrapper function to mac_unicast_add when we want to have the same mac
2895  * client open for two instances, one that is currently active and another
2896  * that will become active when the current one is removed. In this case
2897  * mac_unicast_add will return EGAIN and we will save the rx function and
2898  * arg which will be used when we activate the passive client in
2899  * mac_unicast_remove.
2900  */
2901 int
mac_unicast_add_set_rx(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag,mac_rx_t rx_fn,void * arg)2902 mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
2903     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
2904     mac_rx_t rx_fn, void *arg)
2905 {
2906 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2907 	uint_t			err;
2908 
2909 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2910 	if (err != 0 && err != EAGAIN)
2911 		return (err);
2912 	if (err == EAGAIN) {
2913 		if (rx_fn != NULL) {
2914 			mcip->mci_rx_p_fn = rx_fn;
2915 			mcip->mci_rx_p_arg = arg;
2916 		}
2917 		return (0);
2918 	}
2919 	if (rx_fn != NULL)
2920 		mac_rx_set(mch, rx_fn, arg);
2921 	return (err);
2922 }
2923 
2924 int
mac_unicast_add(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag)2925 mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2926     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2927 {
2928 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
2929 	uint_t err;
2930 
2931 	i_mac_perim_enter(mip);
2932 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2933 	i_mac_perim_exit(mip);
2934 
2935 	return (err);
2936 }
2937 
2938 static void
mac_client_datapath_teardown(mac_client_handle_t mch,mac_unicast_impl_t * muip,flow_entry_t * flent)2939 mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
2940     flow_entry_t *flent)
2941 {
2942 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2943 	mac_impl_t		*mip = mcip->mci_mip;
2944 	boolean_t		no_unicast;
2945 
2946 	/*
2947 	 * If we have not added a unicast address for this MAC client, just
2948 	 * teardown the datapath.
2949 	 */
2950 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2951 
2952 	if (!no_unicast) {
2953 		/*
2954 		 * We would have initialized subflows etc. only if we brought
2955 		 * up the primary client and set the unicast unicast address
2956 		 * etc. Deactivate the flows. The flow entry will be removed
2957 		 * from the active flow tables, and the associated SRS,
2958 		 * softrings etc will be deleted. But the flow entry itself
2959 		 * won't be destroyed, instead it will continue to be archived
2960 		 * off the  the global flow hash list, for a possible future
2961 		 * activation when say IP is plumbed again.
2962 		 */
2963 		mac_link_release_flows(mch);
2964 	}
2965 	mip->mi_nactiveclients--;
2966 	mac_update_single_active_client(mip);
2967 
2968 	/* Tear down the data path */
2969 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
2970 
2971 	/*
2972 	 * Prevent any future access to the flow entry through the mci_flent
2973 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
2974 	 * mac_bcast_send is also under mi_rw_lock.
2975 	 */
2976 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2977 	flent = mcip->mci_flent;
2978 	mac_client_remove_flow_from_list(mcip, flent);
2979 
2980 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
2981 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
2982 
2983 	/*
2984 	 * This is the last unicast address being removed and there shouldn't
2985 	 * be any outbound data threads at this point coming down from mac
2986 	 * clients. We have waited for the data threads to finish before
2987 	 * starting dld_str_detach. Non-data threads must access TX SRS
2988 	 * under mi_rw_lock.
2989 	 */
2990 	rw_exit(&mip->mi_rw_lock);
2991 
2992 	/*
2993 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
2994 	 * contain other flags, such as FE_CONDEMNED, which we need to
2995 	 * cleared. We don't call mac_flow_cleanup() for this unicast
2996 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
2997 	 * path). We just clear the stats and reset the initial callback
2998 	 * function, the rest will be set when we call mac_flow_create,
2999 	 * if at all.
3000 	 */
3001 	mutex_enter(&flent->fe_lock);
3002 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
3003 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
3004 	flent->fe_flags = FE_MC_NO_DATAPATH;
3005 	flow_stat_destroy(flent);
3006 	mac_misc_stat_delete(flent);
3007 
3008 	/* Initialize the receiver function to a safe routine */
3009 	flent->fe_cb_fn = (flow_fn_t)mac_rx_def;
3010 	flent->fe_cb_arg1 = NULL;
3011 	flent->fe_cb_arg2 = NULL;
3012 
3013 	flent->fe_index = -1;
3014 	mutex_exit(&flent->fe_lock);
3015 
3016 	if (mip->mi_type->mt_brdcst_addr != NULL) {
3017 		ASSERT(muip != NULL || no_unicast);
3018 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3019 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
3020 	}
3021 
3022 	if (mip->mi_nactiveclients == 1) {
3023 		mac_capab_update((mac_handle_t)mip);
3024 		mac_virtual_link_update(mip);
3025 	}
3026 
3027 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
3028 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
3029 
3030 		if (mip->mi_state_flags & MIS_LEGACY)
3031 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
3032 	}
3033 
3034 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3035 
3036 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3037 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3038 
3039 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3040 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3041 
3042 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3043 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3044 
3045 	if (muip != NULL)
3046 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3047 	mac_protect_cancel_timer(mcip);
3048 	mac_protect_flush_dynamic(mcip);
3049 
3050 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
3051 	/*
3052 	 * Disable fastpath if this is a VNIC or a VLAN.
3053 	 */
3054 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
3055 		mac_fastpath_enable((mac_handle_t)mip);
3056 	mac_stop((mac_handle_t)mip);
3057 }
3058 
3059 /*
3060  * Remove a MAC address which was previously added by mac_unicast_add().
3061  */
3062 int
mac_unicast_remove(mac_client_handle_t mch,mac_unicast_handle_t mah)3063 mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
3064 {
3065 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3066 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
3067 	mac_unicast_impl_t *pre;
3068 	mac_impl_t *mip = mcip->mci_mip;
3069 	flow_entry_t		*flent;
3070 	uint16_t mui_vid;
3071 
3072 	i_mac_perim_enter(mip);
3073 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
3074 		/*
3075 		 * Call made by the upper MAC client of a VNIC.
3076 		 * There's nothing much to do, the unicast address will
3077 		 * be removed by the VNIC driver when the VNIC is deleted,
3078 		 * but let's ensure that all our transmit is done before
3079 		 * the client does a mac_client_stop lest it trigger an
3080 		 * assert in the driver.
3081 		 */
3082 		ASSERT(muip->mui_vid == VLAN_ID_NONE);
3083 
3084 		mac_tx_client_flush(mcip);
3085 
3086 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3087 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3088 			if (mcip->mci_rx_p_fn != NULL) {
3089 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3090 				    mcip->mci_rx_p_arg);
3091 				mcip->mci_rx_p_fn = NULL;
3092 				mcip->mci_rx_p_arg = NULL;
3093 			}
3094 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3095 			i_mac_perim_exit(mip);
3096 			return (0);
3097 		}
3098 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
3099 
3100 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3101 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3102 
3103 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3104 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3105 
3106 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3107 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3108 
3109 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3110 		i_mac_perim_exit(mip);
3111 		return (0);
3112 	}
3113 
3114 	ASSERT(muip != NULL);
3115 
3116 	/*
3117 	 * We are removing a passive client, we haven't setup the datapath
3118 	 * for this yet, so nothing much to do.
3119 	 */
3120 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3121 
3122 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
3123 		ASSERT(mcip->mci_p_unicast_list == muip);
3124 
3125 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3126 
3127 		mcip->mci_p_unicast_list = NULL;
3128 		mcip->mci_rx_p_fn = NULL;
3129 		mcip->mci_rx_p_arg = NULL;
3130 
3131 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3132 
3133 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3134 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3135 
3136 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3137 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3138 
3139 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3140 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3141 
3142 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3143 		i_mac_perim_exit(mip);
3144 		return (0);
3145 	}
3146 
3147 	/*
3148 	 * Remove the VID from the list of client's VIDs.
3149 	 */
3150 	pre = mcip->mci_unicast_list;
3151 	if (muip == pre) {
3152 		mcip->mci_unicast_list = muip->mui_next;
3153 	} else {
3154 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
3155 			pre = pre->mui_next;
3156 		ASSERT(pre->mui_next == muip);
3157 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3158 		pre->mui_next = muip->mui_next;
3159 		rw_exit(&mcip->mci_rw_lock);
3160 	}
3161 
3162 	if (!mac_client_single_rcvr(mcip)) {
3163 		/*
3164 		 * This MAC client is shared by more than one unicast
3165 		 * addresses, so we will just remove the flent
3166 		 * corresponding to the address being removed. We don't invoke
3167 		 * mac_rx_classify_flow_rem() since the additional flow is
3168 		 * not associated with its own separate set of SRS and rings,
3169 		 * and these constructs are still needed for the remaining
3170 		 * flows.
3171 		 */
3172 		flent = mac_client_get_flow(mcip, muip);
3173 		VERIFY3P(flent, !=, NULL);
3174 
3175 		/*
3176 		 * The first one is disappearing, need to make sure
3177 		 * we replace it with another from the list of
3178 		 * shared clients.
3179 		 */
3180 		if (flent == mcip->mci_flent)
3181 			flent = mac_client_swap_mciflent(mcip);
3182 		mac_client_remove_flow_from_list(mcip, flent);
3183 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3184 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3185 
3186 		/*
3187 		 * The multicast groups that were added by the client so
3188 		 * far must be removed from the brodcast domain corresponding
3189 		 * to the VID being removed.
3190 		 */
3191 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
3192 		    (void *)flent, B_FALSE);
3193 
3194 		if (mip->mi_type->mt_brdcst_addr != NULL) {
3195 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3196 			    muip->mui_vid);
3197 		}
3198 
3199 		FLOW_FINAL_REFRELE(flent);
3200 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
3201 
3202 		/*
3203 		 * Enable fastpath if this is a VNIC or a VLAN.
3204 		 */
3205 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
3206 			mac_fastpath_enable((mac_handle_t)mip);
3207 		mac_stop((mac_handle_t)mip);
3208 		i_mac_perim_exit(mip);
3209 		return (0);
3210 	}
3211 
3212 	mui_vid = muip->mui_vid;
3213 	mac_client_datapath_teardown(mch, muip, flent);
3214 
3215 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
3216 	    mui_vid == VLAN_ID_NONE) {
3217 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
3218 	} else {
3219 		i_mac_perim_exit(mip);
3220 		return (0);
3221 	}
3222 
3223 	/*
3224 	 * If we are removing the primary, check if we have a passive primary
3225 	 * client that we need to activate now.
3226 	 */
3227 	mcip = mac_get_passive_primary_client(mip);
3228 	if (mcip != NULL) {
3229 		mac_resource_props_t	*mrp;
3230 		mac_unicast_impl_t	*muip;
3231 
3232 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3233 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3234 
3235 		/*
3236 		 * Apply the property cached in the mac_impl_t to the
3237 		 * primary mac client.
3238 		 */
3239 		mac_get_resources((mac_handle_t)mip, mrp);
3240 		(void) mac_client_set_resources(mch, mrp);
3241 		ASSERT(mcip->mci_p_unicast_list != NULL);
3242 		muip = mcip->mci_p_unicast_list;
3243 		mcip->mci_p_unicast_list = NULL;
3244 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
3245 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
3246 			if (mcip->mci_rx_p_fn != NULL) {
3247 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3248 				    mcip->mci_rx_p_arg);
3249 				mcip->mci_rx_p_fn = NULL;
3250 				mcip->mci_rx_p_arg = NULL;
3251 			}
3252 		} else {
3253 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3254 		}
3255 		kmem_free(mrp, sizeof (*mrp));
3256 	}
3257 	i_mac_perim_exit(mip);
3258 	return (0);
3259 }
3260 
3261 /*
3262  * Multicast add function invoked by MAC clients.
3263  */
3264 int
mac_multicast_add(mac_client_handle_t mch,const uint8_t * addr)3265 mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
3266 {
3267 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3268 	mac_impl_t		*mip = mcip->mci_mip;
3269 	flow_entry_t		*flent = mcip->mci_flent_list;
3270 	flow_entry_t		*prev_fe = NULL;
3271 	uint16_t		vid;
3272 	int			err = 0;
3273 
3274 	/* Verify the address is a valid multicast address */
3275 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
3276 	    mip->mi_pdata)) != 0)
3277 		return (err);
3278 
3279 	i_mac_perim_enter(mip);
3280 	while (flent != NULL) {
3281 		vid = i_mac_flow_vid(flent);
3282 
3283 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
3284 		    MAC_ADDRTYPE_MULTICAST);
3285 		if (err != 0)
3286 			break;
3287 		prev_fe = flent;
3288 		flent = flent->fe_client_next;
3289 	}
3290 
3291 	/*
3292 	 * If we failed adding, then undo all, rather than partial
3293 	 * success.
3294 	 */
3295 	if (flent != NULL && prev_fe != NULL) {
3296 		flent = mcip->mci_flent_list;
3297 		while (flent != prev_fe->fe_client_next) {
3298 			vid = i_mac_flow_vid(flent);
3299 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3300 			flent = flent->fe_client_next;
3301 		}
3302 	}
3303 	i_mac_perim_exit(mip);
3304 	return (err);
3305 }
3306 
3307 /*
3308  * Multicast delete function invoked by MAC clients.
3309  */
3310 void
mac_multicast_remove(mac_client_handle_t mch,const uint8_t * addr)3311 mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
3312 {
3313 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3314 	mac_impl_t		*mip = mcip->mci_mip;
3315 	flow_entry_t		*flent;
3316 	uint16_t		vid;
3317 
3318 	i_mac_perim_enter(mip);
3319 	for (flent = mcip->mci_flent_list; flent != NULL;
3320 	    flent = flent->fe_client_next) {
3321 		vid = i_mac_flow_vid(flent);
3322 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3323 	}
3324 	i_mac_perim_exit(mip);
3325 }
3326 
3327 /*
3328  * When a MAC client desires to capture packets on an interface,
3329  * it registers a promiscuous call back with mac_promisc_add().
3330  * There are three types of promiscuous callbacks:
3331  *
3332  * * MAC_CLIENT_PROMISC_ALL
3333  *   Captures all packets sent and received by the MAC client,
3334  *   the physical interface, as well as all other MAC clients
3335  *   defined on top of the same MAC.
3336  *
3337  * * MAC_CLIENT_PROMISC_FILTERED
3338  *   Captures all packets sent and received by the MAC client,
3339  *   plus all multicast traffic sent and received by the phyisical
3340  *   interface and the other MAC clients.
3341  *
3342  * * MAC_CLIENT_PROMISC_MULTI
3343  *   Captures all broadcast and multicast packets sent and
3344  *   received by the MAC clients as well as the physical interface.
3345  *
3346  * In all cases, the underlying MAC is put in promiscuous mode.
3347  */
3348 int
mac_promisc_add(mac_client_handle_t mch,mac_client_promisc_type_t type,mac_rx_t fn,void * arg,mac_promisc_handle_t * mphp,uint16_t flags)3349 mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
3350     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
3351 {
3352 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3353 	mac_impl_t *mip = mcip->mci_mip;
3354 	mac_promisc_impl_t *mpip;
3355 	mac_cb_info_t	*mcbi;
3356 	int rc;
3357 
3358 	boolean_t no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
3359 	boolean_t rx_only = ((flags & MAC_PROMISC_FLAGS_RX_ONLY) != 0);
3360 	boolean_t tx_only = ((flags & MAC_PROMISC_FLAGS_TX_ONLY) != 0);
3361 
3362 	if (no_tx_loop && tx_only)
3363 		return (EINVAL);
3364 	if (rx_only && tx_only)
3365 		return (EINVAL);
3366 
3367 	i_mac_perim_enter(mip);
3368 
3369 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
3370 		i_mac_perim_exit(mip);
3371 		return (rc);
3372 	}
3373 
3374 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
3375 	    type == MAC_CLIENT_PROMISC_ALL &&
3376 	    (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
3377 		/*
3378 		 * The function is being invoked by the upper MAC client
3379 		 * of a VNIC. The VNIC should only see the traffic
3380 		 * it is entitled to.
3381 		 */
3382 		type = MAC_CLIENT_PROMISC_FILTERED;
3383 	}
3384 
3385 
3386 	/*
3387 	 * Turn on promiscuous mode for the underlying NIC.
3388 	 * This is needed even for filtered callbacks which
3389 	 * expect to receive all multicast traffic on the wire.
3390 	 *
3391 	 * Physical promiscuous mode should not be turned on if
3392 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
3393 	 */
3394 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
3395 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
3396 			mac_stop((mac_handle_t)mip);
3397 			i_mac_perim_exit(mip);
3398 			return (rc);
3399 		}
3400 	}
3401 
3402 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
3403 
3404 	mpip->mpi_type = type;
3405 	mpip->mpi_fn = fn;
3406 	mpip->mpi_arg = arg;
3407 	mpip->mpi_mcip = mcip;
3408 	mpip->mpi_no_tx_loop = no_tx_loop;
3409 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
3410 	mpip->mpi_strip_vlan_tag =
3411 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
3412 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
3413 	mpip->mpi_rx_only = rx_only;
3414 	mpip->mpi_tx_only = tx_only;
3415 
3416 	mcbi = &mip->mi_promisc_cb_info;
3417 	mutex_enter(mcbi->mcbi_lockp);
3418 
3419 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
3420 	    &mpip->mpi_mci_link);
3421 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
3422 	    &mpip->mpi_mi_link);
3423 
3424 	mutex_exit(mcbi->mcbi_lockp);
3425 
3426 	*mphp = (mac_promisc_handle_t)mpip;
3427 
3428 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3429 		mac_impl_t *umip = mcip->mci_upper_mip;
3430 
3431 		ASSERT(umip != NULL);
3432 		mac_vnic_secondary_update(umip);
3433 	}
3434 
3435 	i_mac_perim_exit(mip);
3436 
3437 	return (0);
3438 }
3439 
3440 /*
3441  * Remove a multicast address previously aded through mac_promisc_add().
3442  */
3443 void
mac_promisc_remove(mac_promisc_handle_t mph)3444 mac_promisc_remove(mac_promisc_handle_t mph)
3445 {
3446 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
3447 	mac_client_impl_t *mcip = mpip->mpi_mcip;
3448 	mac_impl_t *mip = mcip->mci_mip;
3449 	mac_cb_info_t *mcbi;
3450 	int rv;
3451 
3452 	i_mac_perim_enter(mip);
3453 
3454 	/*
3455 	 * Even if the device can't be reset into normal mode, we still
3456 	 * need to clear the client promisc callbacks. The client may want
3457 	 * to close the mac end point and we can't have stale callbacks.
3458 	 */
3459 	if (!(mpip->mpi_no_phys)) {
3460 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
3461 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
3462 			    " mode because of error 0x%x", mip->mi_name, rv);
3463 		}
3464 	}
3465 	mcbi = &mip->mi_promisc_cb_info;
3466 	mutex_enter(mcbi->mcbi_lockp);
3467 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
3468 	    &mpip->mpi_mi_link)) {
3469 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
3470 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
3471 		kmem_cache_free(mac_promisc_impl_cache, mpip);
3472 	} else {
3473 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
3474 	}
3475 
3476 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3477 		mac_impl_t *umip = mcip->mci_upper_mip;
3478 
3479 		ASSERT(umip != NULL);
3480 		mac_vnic_secondary_update(umip);
3481 	}
3482 
3483 	mutex_exit(mcbi->mcbi_lockp);
3484 	mac_stop((mac_handle_t)mip);
3485 
3486 	i_mac_perim_exit(mip);
3487 }
3488 
3489 /*
3490  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
3491  * that a control operation wants to quiesce the Tx data flow in which case
3492  * we return an error. Holding any of the per cpu locks ensures that the
3493  * mci_tx_flag won't change.
3494  *
3495  * 'CPU' must be accessed just once and used to compute the index into the
3496  * percpu array, and that index must be used for the entire duration of the
3497  * packet send operation. Note that the thread may be preempted and run on
3498  * another cpu any time and so we can't use 'CPU' more than once for the
3499  * operation.
3500  */
3501 #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
3502 {									\
3503 	(error) = 0;							\
3504 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
3505 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3506 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
3507 		(mytx)->pcpu_tx_refcnt++;				\
3508 	} else {							\
3509 		(error) = -1;						\
3510 	}								\
3511 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
3512 }
3513 
3514 /*
3515  * Release the reference. If needed, signal any control operation waiting
3516  * for Tx quiescence. The wait and signal are always done using the
3517  * mci_tx_pcpu[0]'s lock
3518  */
3519 #define	MAC_TX_RELE(mcip, mytx) {					\
3520 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3521 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
3522 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
3523 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3524 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3525 		cv_signal(&(mcip)->mci_tx_cv);				\
3526 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3527 	} else {							\
3528 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3529 	}								\
3530 }
3531 
3532 /*
3533  * Send function invoked by MAC clients.
3534  */
3535 mac_tx_cookie_t
mac_tx(mac_client_handle_t mch,mblk_t * mp_chain,uintptr_t hint,uint16_t flag,mblk_t ** ret_mp)3536 mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
3537     uint16_t flag, mblk_t **ret_mp)
3538 {
3539 	mac_tx_cookie_t		cookie = 0;
3540 	int			error;
3541 	mac_tx_percpu_t		*mytx;
3542 	mac_soft_ring_set_t	*srs;
3543 	flow_entry_t		*flent;
3544 	boolean_t		is_subflow = B_FALSE;
3545 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3546 	mac_impl_t		*mip = mcip->mci_mip;
3547 	mac_srs_tx_t		*srs_tx;
3548 
3549 	/*
3550 	 * Check whether the active Tx threads count is bumped already.
3551 	 */
3552 	if (!(flag & MAC_TX_NO_HOLD)) {
3553 		MAC_TX_TRY_HOLD(mcip, mytx, error);
3554 		if (error != 0) {
3555 			freemsgchain(mp_chain);
3556 			return (0);
3557 		}
3558 	}
3559 
3560 	/*
3561 	 * If mac protection is enabled, only the permissible packets will be
3562 	 * returned by mac_protect_check().
3563 	 */
3564 	if ((mcip->mci_flent->
3565 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
3566 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
3567 		goto done;
3568 
3569 	if (mcip->mci_subflow_tab != NULL &&
3570 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
3571 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
3572 	    FLOW_OUTBOUND, &flent) == 0) {
3573 		/*
3574 		 * The main assumption here is that if in the event
3575 		 * we get a chain, all the packets will be classified
3576 		 * to the same Flow/SRS. If this changes for any
3577 		 * reason, the following logic should change as well.
3578 		 * I suppose the fanout_hint also assumes this .
3579 		 */
3580 		ASSERT(flent != NULL);
3581 		is_subflow = B_TRUE;
3582 	} else {
3583 		flent = mcip->mci_flent;
3584 	}
3585 
3586 	srs = flent->fe_tx_srs;
3587 	/*
3588 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
3589 	 * against an interface that is not capable of sending. A rewrite
3590 	 * of the mac datapath is required to remove this limitation.
3591 	 */
3592 	if (srs == NULL) {
3593 		freemsgchain(mp_chain);
3594 		goto done;
3595 	}
3596 
3597 	srs_tx = &srs->srs_tx;
3598 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
3599 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
3600 	    mip->mi_nactiveclients == 1 &&
3601 	    mp_chain->b_next == NULL &&
3602 	    (DB_CKSUMFLAGS(mp_chain) & HW_LSO) == 0) {
3603 		uint64_t	obytes;
3604 
3605 		/*
3606 		 * Since dls always opens the underlying MAC, nclients equals
3607 		 * to 1 means that the only active client is dls itself acting
3608 		 * as a primary client of the MAC instance. Since dls will not
3609 		 * send tagged packets in that case, and dls is trusted to send
3610 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
3611 		 * check is required only if nclients is greater than 1.
3612 		 */
3613 		if (mip->mi_nclients > 1) {
3614 			if (MAC_VID_CHECK_NEEDED(mcip)) {
3615 				int	err = 0;
3616 
3617 				MAC_VID_CHECK(mcip, mp_chain, err);
3618 				if (err != 0) {
3619 					freemsg(mp_chain);
3620 					mcip->mci_misc_stat.mms_txerrors++;
3621 					goto done;
3622 				}
3623 			}
3624 			if (MAC_TAG_NEEDED(mcip)) {
3625 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
3626 				    mac_client_vid(mch));
3627 				if (mp_chain == NULL) {
3628 					mcip->mci_misc_stat.mms_txerrors++;
3629 					goto done;
3630 				}
3631 			}
3632 		}
3633 
3634 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
3635 		    msgdsize(mp_chain));
3636 
3637 		mp_chain = mac_provider_tx(mip,
3638 		    (mac_ring_handle_t)srs_tx->st_arg2, mp_chain, mcip);
3639 
3640 		if (mp_chain == NULL) {
3641 			cookie = 0;
3642 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
3643 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
3644 		} else {
3645 			mutex_enter(&srs->srs_lock);
3646 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
3647 			    flag, ret_mp);
3648 			mutex_exit(&srs->srs_lock);
3649 		}
3650 	} else {
3651 		mblk_t *mp = mp_chain;
3652 		mblk_t *new_head = NULL;
3653 		mblk_t *new_tail = NULL;
3654 
3655 		/*
3656 		 * There are occasions where the packets arriving here
3657 		 * may request hardware offloads that are not
3658 		 * available from the underlying MAC provider. This
3659 		 * currently only happens when a packet is sent across
3660 		 * the MAC-loopback path of one MAC and then forwarded
3661 		 * (via IP) to another MAC that lacks one or more of
3662 		 * the hardware offloads provided by the first one.
3663 		 * However, in the future, we may choose to pretend
3664 		 * all MAC providers support all offloads, performing
3665 		 * emulation on Tx as needed.
3666 		 *
3667 		 * We iterate each mblk in-turn, emulating hardware
3668 		 * offloads as required. From this process, we create
3669 		 * a new chain. The new chain may be the same as the
3670 		 * original chain (no hardware emulation needed), a
3671 		 * collection of new mblks (hardware emulation
3672 		 * needed), or a mix. At this point, the chain is safe
3673 		 * for consumption by the underlying MAC provider and
3674 		 * is passed down to the SRS.
3675 		 */
3676 		while (mp != NULL) {
3677 			mblk_t *next = mp->b_next;
3678 			mblk_t *tail = NULL;
3679 			const uint16_t needed =
3680 			    (DB_CKSUMFLAGS(mp) ^ mip->mi_tx_cksum_flags) &
3681 			    DB_CKSUMFLAGS(mp);
3682 
3683 			mp->b_next = NULL;
3684 
3685 			if ((needed & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) {
3686 				mac_emul_t emul = 0;
3687 
3688 				if (needed & HCK_IPV4_HDRCKSUM)
3689 					emul |= MAC_IPCKSUM_EMUL;
3690 				if (needed & (HCK_PARTIALCKSUM | HCK_FULLCKSUM))
3691 					emul |= MAC_HWCKSUM_EMUL;
3692 				if (needed & HW_LSO)
3693 					emul = MAC_LSO_EMUL;
3694 
3695 				mac_hw_emul(&mp, &tail, NULL, emul);
3696 
3697 				if (mp == NULL) {
3698 					mp = next;
3699 					continue;
3700 				}
3701 			}
3702 
3703 			if (new_head == NULL) {
3704 				new_head = mp;
3705 			} else {
3706 				new_tail->b_next = mp;
3707 			}
3708 
3709 			new_tail = (tail == NULL) ? mp : tail;
3710 			mp = next;
3711 		}
3712 
3713 		if (new_head == NULL) {
3714 			cookie = 0;
3715 			goto done;
3716 		}
3717 
3718 		cookie = srs_tx->st_func(srs, new_head, hint, flag, ret_mp);
3719 	}
3720 
3721 done:
3722 	if (is_subflow)
3723 		FLOW_REFRELE(flent);
3724 
3725 	if (!(flag & MAC_TX_NO_HOLD))
3726 		MAC_TX_RELE(mcip, mytx);
3727 
3728 	return (cookie);
3729 }
3730 
3731 /*
3732  * mac_tx_is_blocked
3733  *
3734  * Given a cookie, it returns if the ring identified by the cookie is
3735  * flow-controlled or not. If NULL is passed in place of a cookie,
3736  * then it finds out if any of the underlying rings belonging to the
3737  * SRS is flow controlled or not and returns that status.
3738  */
3739 /* ARGSUSED */
3740 boolean_t
mac_tx_is_flow_blocked(mac_client_handle_t mch,mac_tx_cookie_t cookie)3741 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
3742 {
3743 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3744 	mac_soft_ring_set_t *mac_srs;
3745 	mac_soft_ring_t *sringp;
3746 	boolean_t blocked = B_FALSE;
3747 	mac_tx_percpu_t *mytx;
3748 	int err;
3749 	int i;
3750 
3751 	/*
3752 	 * Bump the reference count so that mac_srs won't be deleted.
3753 	 * If the client is currently quiesced and we failed to bump
3754 	 * the reference, return B_TRUE so that flow control stays
3755 	 * as enabled.
3756 	 *
3757 	 * Flow control will then be disabled once the client is no
3758 	 * longer quiesced.
3759 	 */
3760 	MAC_TX_TRY_HOLD(mcip, mytx, err);
3761 	if (err != 0)
3762 		return (B_TRUE);
3763 
3764 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
3765 		MAC_TX_RELE(mcip, mytx);
3766 		return (B_FALSE);
3767 	}
3768 
3769 	mutex_enter(&mac_srs->srs_lock);
3770 	/*
3771 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
3772 	 * softring (s_ring_state) will have the HIWAT set. This is
3773 	 * the multiple Tx ring flow control case. For all other
3774 	 * case, SRS (srs_state) will store the condition.
3775 	 */
3776 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3777 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
3778 		if (cookie != 0) {
3779 			sringp = (mac_soft_ring_t *)cookie;
3780 			mutex_enter(&sringp->s_ring_lock);
3781 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
3782 				blocked = B_TRUE;
3783 			mutex_exit(&sringp->s_ring_lock);
3784 		} else {
3785 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3786 				sringp = mac_srs->srs_tx_soft_rings[i];
3787 				mutex_enter(&sringp->s_ring_lock);
3788 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
3789 					blocked = B_TRUE;
3790 					mutex_exit(&sringp->s_ring_lock);
3791 					break;
3792 				}
3793 				mutex_exit(&sringp->s_ring_lock);
3794 			}
3795 		}
3796 	} else {
3797 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
3798 	}
3799 	mutex_exit(&mac_srs->srs_lock);
3800 	MAC_TX_RELE(mcip, mytx);
3801 	return (blocked);
3802 }
3803 
3804 /*
3805  * Check if the MAC client is the primary MAC client.
3806  */
3807 boolean_t
mac_is_primary_client(mac_client_impl_t * mcip)3808 mac_is_primary_client(mac_client_impl_t *mcip)
3809 {
3810 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
3811 }
3812 
3813 void
mac_ioctl(mac_handle_t mh,queue_t * wq,mblk_t * bp)3814 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
3815 {
3816 	mac_impl_t	*mip = (mac_impl_t *)mh;
3817 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3818 
3819 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3820 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3821 		/*
3822 		 * If ndd props were registered, call them.
3823 		 * Note that ndd ioctls are Obsolete
3824 		 */
3825 		mac_ndd_ioctl(mip, wq, bp);
3826 		return;
3827 	}
3828 
3829 	/*
3830 	 * Call the driver to handle the ioctl.  The driver may not support
3831 	 * any ioctls, in which case we reply with a NAK on its behalf.
3832 	 */
3833 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3834 		mip->mi_ioctl(mip->mi_driver, wq, bp);
3835 	else
3836 		miocnak(wq, bp, 0, EINVAL);
3837 }
3838 
3839 /*
3840  * Return the link state of the specified MAC instance.
3841  */
3842 link_state_t
mac_link_get(mac_handle_t mh)3843 mac_link_get(mac_handle_t mh)
3844 {
3845 	return (((mac_impl_t *)mh)->mi_linkstate);
3846 }
3847 
3848 /*
3849  * Add a mac client specified notification callback. Please see the comments
3850  * above mac_callback_add() for general information about mac callback
3851  * addition/deletion in the presence of mac callback list walkers
3852  */
3853 mac_notify_handle_t
mac_notify_add(mac_handle_t mh,mac_notify_t notify_fn,void * arg)3854 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3855 {
3856 	mac_impl_t		*mip = (mac_impl_t *)mh;
3857 	mac_notify_cb_t		*mncb;
3858 	mac_cb_info_t		*mcbi;
3859 
3860 	/*
3861 	 * Allocate a notify callback structure, fill in the details and
3862 	 * use the mac callback list manipulation functions to chain into
3863 	 * the list of callbacks.
3864 	 */
3865 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3866 	mncb->mncb_fn = notify_fn;
3867 	mncb->mncb_arg = arg;
3868 	mncb->mncb_mip = mip;
3869 	mncb->mncb_link.mcb_objp = mncb;
3870 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3871 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3872 
3873 	mcbi = &mip->mi_notify_cb_info;
3874 
3875 	i_mac_perim_enter(mip);
3876 	mutex_enter(mcbi->mcbi_lockp);
3877 
3878 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3879 	    &mncb->mncb_link);
3880 
3881 	mutex_exit(mcbi->mcbi_lockp);
3882 	i_mac_perim_exit(mip);
3883 	return ((mac_notify_handle_t)mncb);
3884 }
3885 
3886 void
mac_notify_remove_wait(mac_handle_t mh)3887 mac_notify_remove_wait(mac_handle_t mh)
3888 {
3889 	mac_impl_t	*mip = (mac_impl_t *)mh;
3890 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3891 
3892 	mutex_enter(mcbi->mcbi_lockp);
3893 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3894 	mutex_exit(mcbi->mcbi_lockp);
3895 }
3896 
3897 /*
3898  * Remove a mac client specified notification callback
3899  */
3900 int
mac_notify_remove(mac_notify_handle_t mnh,boolean_t wait)3901 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3902 {
3903 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3904 	mac_impl_t	*mip = mncb->mncb_mip;
3905 	mac_cb_info_t	*mcbi;
3906 	int		err = 0;
3907 
3908 	mcbi = &mip->mi_notify_cb_info;
3909 
3910 	i_mac_perim_enter(mip);
3911 	mutex_enter(mcbi->mcbi_lockp);
3912 
3913 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3914 	/*
3915 	 * If there aren't any list walkers, the remove would succeed
3916 	 * inline, else we wait for the deferred remove to complete
3917 	 */
3918 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3919 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3920 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3921 	} else {
3922 		err = EBUSY;
3923 	}
3924 
3925 	mutex_exit(mcbi->mcbi_lockp);
3926 	i_mac_perim_exit(mip);
3927 
3928 	/*
3929 	 * If we failed to remove the notification callback and "wait" is set
3930 	 * to be B_TRUE, wait for the callback to finish after we exit the
3931 	 * mac perimeter.
3932 	 */
3933 	if (err != 0 && wait) {
3934 		mac_notify_remove_wait((mac_handle_t)mip);
3935 		return (0);
3936 	}
3937 
3938 	return (err);
3939 }
3940 
3941 /*
3942  * Associate resource management callbacks with the specified MAC
3943  * clients.
3944  */
3945 void
mac_resource_set(mac_client_handle_t mch,mac_resource_cb_t * rcbs,boolean_t is_v6)3946 mac_resource_set(mac_client_handle_t mch, mac_resource_cb_t *rcbs,
3947     boolean_t is_v6)
3948 {
3949 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3950 
3951 	if (is_v6) {
3952 		mcip->mci_rcb6 = *rcbs;
3953 	} else {
3954 		mcip->mci_rcb4 = *rcbs;
3955 	}
3956 }
3957 
3958 void
mac_resource_clear(mac_client_handle_t mch,boolean_t is_v6)3959 mac_resource_clear(mac_client_handle_t mch, boolean_t is_v6)
3960 {
3961 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3962 
3963 	if (is_v6) {
3964 		bzero(&mcip->mci_rcb6, sizeof (mcip->mci_rcb6));
3965 	} else {
3966 		bzero(&mcip->mci_rcb4, sizeof (mcip->mci_rcb4));
3967 	}
3968 }
3969 
3970 /*
3971  * Sets up the client resources and enable the polling interface over all the
3972  * SRS's and the soft rings of the client
3973  */
3974 void
mac_client_poll_enable(mac_client_handle_t mch,boolean_t is_v6)3975 mac_client_poll_enable(mac_client_handle_t mch, boolean_t is_v6)
3976 {
3977 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3978 	mac_soft_ring_set_t	*mac_srs;
3979 	flow_entry_t		*flent;
3980 	int			i;
3981 
3982 	flent = mcip->mci_flent;
3983 	ASSERT(flent != NULL);
3984 
3985 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3986 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3987 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3988 		ASSERT(mac_srs->srs_mcip == mcip);
3989 		mac_srs_client_poll_enable(mcip, mac_srs, is_v6);
3990 	}
3991 }
3992 
3993 /*
3994  * Tears down the client resources and disable the polling interface over all
3995  * the SRS's and the soft rings of the client
3996  */
3997 void
mac_client_poll_disable(mac_client_handle_t mch,boolean_t is_v6)3998 mac_client_poll_disable(mac_client_handle_t mch, boolean_t is_v6)
3999 {
4000 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4001 	mac_soft_ring_set_t	*mac_srs;
4002 	flow_entry_t		*flent;
4003 	int			i;
4004 
4005 	flent = mcip->mci_flent;
4006 	ASSERT(flent != NULL);
4007 
4008 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
4009 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
4010 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
4011 		ASSERT(mac_srs->srs_mcip == mcip);
4012 		mac_srs_client_poll_disable(mcip, mac_srs, is_v6);
4013 	}
4014 }
4015 
4016 /*
4017  * Associate the CPUs specified by the given property with a MAC client.
4018  */
4019 int
mac_cpu_set(mac_client_handle_t mch,mac_resource_props_t * mrp)4020 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
4021 {
4022 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4023 	mac_impl_t *mip = mcip->mci_mip;
4024 	int err = 0;
4025 
4026 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4027 
4028 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
4029 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
4030 		return (err);
4031 	}
4032 	if (MCIP_DATAPATH_SETUP(mcip))
4033 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
4034 
4035 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
4036 	return (0);
4037 }
4038 
4039 /*
4040  * Apply the specified properties to the specified MAC client.
4041  */
4042 int
mac_client_set_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4043 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4044 {
4045 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4046 	mac_impl_t *mip = mcip->mci_mip;
4047 	int err = 0;
4048 
4049 	i_mac_perim_enter(mip);
4050 
4051 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
4052 		err = mac_resource_ctl_set(mch, mrp);
4053 		if (err != 0)
4054 			goto done;
4055 	}
4056 
4057 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
4058 		err = mac_cpu_set(mch, mrp);
4059 		if (err != 0)
4060 			goto done;
4061 	}
4062 
4063 	if (mrp->mrp_mask & MRP_PROTECT) {
4064 		err = mac_protect_set(mch, mrp);
4065 		if (err != 0)
4066 			goto done;
4067 	}
4068 
4069 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
4070 		err = mac_resource_ctl_set(mch, mrp);
4071 
4072 done:
4073 	i_mac_perim_exit(mip);
4074 	return (err);
4075 }
4076 
4077 /*
4078  * Return the properties currently associated with the specified MAC client.
4079  */
4080 void
mac_client_get_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4081 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4082 {
4083 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4084 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4085 
4086 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4087 }
4088 
4089 /*
4090  * Return the effective properties currently associated with the specified
4091  * MAC client.
4092  */
4093 void
mac_client_get_effective_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4094 mac_client_get_effective_resources(mac_client_handle_t mch,
4095     mac_resource_props_t *mrp)
4096 {
4097 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4098 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
4099 
4100 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4101 }
4102 
4103 /*
4104  * Pass a copy of the specified packet to the promiscuous callbacks
4105  * of the specified MAC.
4106  *
4107  * If sender is NULL, the function is being invoked for a packet chain
4108  * received from the wire. If sender is non-NULL, it points to
4109  * the MAC client from which the packet is being sent.
4110  *
4111  * The packets are distributed to the promiscuous callbacks as follows:
4112  *
4113  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4114  * - all broadcast and multicast packets are sent to the
4115  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4116  *
4117  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4118  * after classification by mac_rx_deliver().
4119  */
4120 static void
mac_promisc_dispatch_one(mac_promisc_impl_t * mpip,mblk_t * mp,boolean_t loopback,boolean_t local)4121 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4122     boolean_t loopback, boolean_t local)
4123 {
4124 	mblk_t *mp_next;
4125 
4126 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4127 		mblk_t *mp_copy;
4128 
4129 		mp_copy = copymsg(mp);
4130 		if (mp_copy == NULL)
4131 			return;
4132 
4133 		if (mpip->mpi_strip_vlan_tag) {
4134 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
4135 			if (mp_copy == NULL)
4136 				return;
4137 		}
4138 
4139 		/*
4140 		 * There is code upstack that can't deal with message
4141 		 * chains.
4142 		 */
4143 		for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
4144 			mp_next = tmp->b_next;
4145 			tmp->b_next = NULL;
4146 			mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
4147 		}
4148 
4149 		return;
4150 	}
4151 
4152 	mp_next = mp->b_next;
4153 	mp->b_next = NULL;
4154 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
4155 	mp->b_next = mp_next;
4156 }
4157 
4158 /*
4159  * Return the VID of a packet. Zero if the packet is not tagged.
4160  */
4161 static uint16_t
mac_ether_vid(mblk_t * mp)4162 mac_ether_vid(mblk_t *mp)
4163 {
4164 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4165 
4166 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4167 		struct ether_vlan_header *t_evhp =
4168 		    (struct ether_vlan_header *)mp->b_rptr;
4169 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4170 	}
4171 
4172 	return (0);
4173 }
4174 
4175 /*
4176  * Return whether the specified packet contains a multicast or broadcast
4177  * destination MAC address.
4178  */
4179 static boolean_t
mac_is_mcast(mac_impl_t * mip,mblk_t * mp)4180 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4181 {
4182 	mac_header_info_t hdr_info;
4183 
4184 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4185 		return (B_FALSE);
4186 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4187 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4188 }
4189 
4190 /*
4191  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4192  * "sender" points to the sender MAC client for outbound packets, and
4193  * is set to NULL for inbound packets.
4194  */
4195 void
mac_promisc_dispatch(mac_impl_t * mip,mblk_t * mp_chain,mac_client_impl_t * sender,boolean_t local)4196 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4197     mac_client_impl_t *sender, boolean_t local)
4198 {
4199 	mac_promisc_impl_t *mpip;
4200 	mac_cb_t *mcb;
4201 	mblk_t *mp;
4202 	boolean_t is_mcast, is_sender;
4203 
4204 	MAC_PROMISC_WALKER_INC(mip);
4205 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4206 		is_mcast = mac_is_mcast(mip, mp);
4207 		/* send packet to interested callbacks */
4208 		for (mcb = mip->mi_promisc_list; mcb != NULL;
4209 		    mcb = mcb->mcb_nextp) {
4210 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4211 			is_sender = (mpip->mpi_mcip == sender);
4212 
4213 			if (sender != NULL && mpip->mpi_rx_only)
4214 				/*
4215 				 * This client doesn't want outbound packets.
4216 				 */
4217 				continue;
4218 
4219 			if (sender == NULL && mpip->mpi_tx_only)
4220 				/*
4221 				 * This client doesn't want inbound packets.
4222 				 */
4223 				continue;
4224 
4225 			if (is_sender && mpip->mpi_no_tx_loop)
4226 				/*
4227 				 * The sender doesn't want to receive
4228 				 * copies of the packets it sends.
4229 				 */
4230 				continue;
4231 
4232 			/* this client doesn't need any packets (bridge) */
4233 			if (mpip->mpi_fn == NULL)
4234 				continue;
4235 
4236 			/*
4237 			 * For an ethernet MAC, don't displatch a multicast
4238 			 * packet to a non-PROMISC_ALL callbacks unless the VID
4239 			 * of the packet matches the VID of the client.
4240 			 */
4241 			if (is_mcast &&
4242 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4243 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
4244 			    mac_ether_vid(mp)))
4245 				continue;
4246 
4247 			if (is_sender ||
4248 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4249 			    is_mcast) {
4250 				mac_promisc_dispatch_one(mpip, mp, is_sender,
4251 				    local);
4252 			}
4253 		}
4254 	}
4255 	MAC_PROMISC_WALKER_DCR(mip);
4256 }
4257 
4258 void
mac_promisc_client_dispatch(mac_client_impl_t * mcip,mblk_t * mp_chain)4259 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4260 {
4261 	mac_impl_t		*mip = mcip->mci_mip;
4262 	mac_promisc_impl_t	*mpip;
4263 	boolean_t		is_mcast;
4264 	mblk_t			*mp;
4265 	mac_cb_t		*mcb;
4266 
4267 	/*
4268 	 * The unicast packets for the MAC client still
4269 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4270 	 * promiscuous callbacks. The broadcast and multicast
4271 	 * packets were delivered from mac_rx().
4272 	 */
4273 	MAC_PROMISC_WALKER_INC(mip);
4274 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4275 		is_mcast = mac_is_mcast(mip, mp);
4276 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
4277 		    mcb = mcb->mcb_nextp) {
4278 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4279 			if (!mpip->mpi_tx_only &&
4280 			    mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4281 			    !is_mcast) {
4282 				mac_promisc_dispatch_one(mpip, mp, B_FALSE,
4283 				    B_FALSE);
4284 			}
4285 		}
4286 	}
4287 	MAC_PROMISC_WALKER_DCR(mip);
4288 }
4289 
4290 /*
4291  * Return the margin value currently assigned to the specified MAC instance.
4292  */
4293 void
mac_margin_get(mac_handle_t mh,uint32_t * marginp)4294 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4295 {
4296 	mac_impl_t *mip = (mac_impl_t *)mh;
4297 
4298 	rw_enter(&(mip->mi_rw_lock), RW_READER);
4299 	*marginp = mip->mi_margin;
4300 	rw_exit(&(mip->mi_rw_lock));
4301 }
4302 
4303 /*
4304  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4305  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4306  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4307  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4308  * cannot disappear while we are accessing it.
4309  */
4310 typedef struct i_mac_info_state_s {
4311 	const char	*mi_name;
4312 	mac_info_t	*mi_infop;
4313 } i_mac_info_state_t;
4314 
4315 /*ARGSUSED*/
4316 static uint_t
i_mac_info_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)4317 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4318 {
4319 	i_mac_info_state_t *statep = arg;
4320 	mac_impl_t *mip = (mac_impl_t *)val;
4321 
4322 	if (mip->mi_state_flags & MIS_DISABLED)
4323 		return (MH_WALK_CONTINUE);
4324 
4325 	if (strcmp(statep->mi_name,
4326 	    ddi_driver_name(mip->mi_dip)) != 0)
4327 		return (MH_WALK_CONTINUE);
4328 
4329 	statep->mi_infop = &mip->mi_info;
4330 	return (MH_WALK_TERMINATE);
4331 }
4332 
4333 boolean_t
mac_info_get(const char * name,mac_info_t * minfop)4334 mac_info_get(const char *name, mac_info_t *minfop)
4335 {
4336 	i_mac_info_state_t state;
4337 
4338 	rw_enter(&i_mac_impl_lock, RW_READER);
4339 	state.mi_name = name;
4340 	state.mi_infop = NULL;
4341 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4342 	if (state.mi_infop == NULL) {
4343 		rw_exit(&i_mac_impl_lock);
4344 		return (B_FALSE);
4345 	}
4346 	*minfop = *state.mi_infop;
4347 	rw_exit(&i_mac_impl_lock);
4348 	return (B_TRUE);
4349 }
4350 
4351 /*
4352  * To get the capabilities that MAC layer cares about, such as rings, factory
4353  * mac address, vnic or not, it should directly invoke this function.  If the
4354  * link is part of a bridge, then the only "capability" it has is the inability
4355  * to do zero copy.
4356  */
4357 boolean_t
i_mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4358 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4359 {
4360 	mac_impl_t *mip = (mac_impl_t *)mh;
4361 
4362 	if (mip->mi_bridge_link != NULL) {
4363 		return (cap == MAC_CAPAB_NO_ZCOPY);
4364 	} else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
4365 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
4366 	} else {
4367 		return (B_FALSE);
4368 	}
4369 }
4370 
4371 /*
4372  * Capability query function. If number of active mac clients is greater than
4373  * 1, only limited capabilities can be advertised to the caller no matter the
4374  * driver has certain capability or not. Else, we query the driver to get the
4375  * capability.
4376  */
4377 boolean_t
mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4378 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4379 {
4380 	mac_impl_t *mip = (mac_impl_t *)mh;
4381 
4382 	/*
4383 	 * Some capabilities are restricted when there are more than one active
4384 	 * clients on the MAC resource.  The ones noted below are safe,
4385 	 * independent of that count.
4386 	 */
4387 	if (mip->mi_nactiveclients > 1) {
4388 		switch (cap) {
4389 		case MAC_CAPAB_NO_ZCOPY:
4390 			return (B_TRUE);
4391 		case MAC_CAPAB_LEGACY:
4392 		case MAC_CAPAB_HCKSUM:
4393 		case MAC_CAPAB_LSO:
4394 		case MAC_CAPAB_NO_NATIVEVLAN:
4395 			break;
4396 		default:
4397 			return (B_FALSE);
4398 		}
4399 	}
4400 
4401 	/* else get capab from driver */
4402 	return (i_mac_capab_get(mh, cap, cap_data));
4403 }
4404 
4405 boolean_t
mac_sap_verify(mac_handle_t mh,uint32_t sap,uint32_t * bind_sap)4406 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4407 {
4408 	mac_impl_t *mip = (mac_impl_t *)mh;
4409 
4410 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4411 	    mip->mi_pdata));
4412 }
4413 
4414 mblk_t *
mac_header(mac_handle_t mh,const uint8_t * daddr,uint32_t sap,mblk_t * payload,size_t extra_len)4415 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4416     size_t extra_len)
4417 {
4418 	mac_impl_t	*mip = (mac_impl_t *)mh;
4419 	const uint8_t	*hdr_daddr;
4420 
4421 	/*
4422 	 * If the MAC is point-to-point with a fixed destination address, then
4423 	 * we must always use that destination in the MAC header.
4424 	 */
4425 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
4426 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4427 	    mip->mi_pdata, payload, extra_len));
4428 }
4429 
4430 int
mac_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4431 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4432 {
4433 	mac_impl_t *mip = (mac_impl_t *)mh;
4434 
4435 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4436 	    mhip));
4437 }
4438 
4439 int
mac_vlan_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4440 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4441 {
4442 	mac_impl_t	*mip = (mac_impl_t *)mh;
4443 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
4444 	int		err = 0;
4445 
4446 	/*
4447 	 * Packets should always be at least 16 bit aligned.
4448 	 */
4449 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
4450 
4451 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
4452 		return (err);
4453 
4454 	/*
4455 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
4456 	 * mac_header_info_t as returned by mac_header_info() is
4457 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
4458 	 */
4459 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
4460 		struct ether_vlan_header *evhp;
4461 		uint16_t sap;
4462 		mblk_t *tmp = NULL;
4463 		size_t size;
4464 
4465 		size = sizeof (struct ether_vlan_header);
4466 		if (MBLKL(mp) < size) {
4467 			/*
4468 			 * Pullup the message in order to get the MAC header
4469 			 * infomation. Note that this is a read-only function,
4470 			 * we keep the input packet intact.
4471 			 */
4472 			if ((tmp = msgpullup(mp, size)) == NULL)
4473 				return (EINVAL);
4474 
4475 			mp = tmp;
4476 		}
4477 		evhp = (struct ether_vlan_header *)mp->b_rptr;
4478 		sap = ntohs(evhp->ether_type);
4479 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
4480 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
4481 		mhip->mhi_tci = ntohs(evhp->ether_tci);
4482 		mhip->mhi_istagged = B_TRUE;
4483 		freemsg(tmp);
4484 
4485 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
4486 			return (EINVAL);
4487 	} else {
4488 		mhip->mhi_istagged = B_FALSE;
4489 		mhip->mhi_tci = 0;
4490 	}
4491 
4492 	return (0);
4493 }
4494 
4495 mblk_t *
mac_header_cook(mac_handle_t mh,mblk_t * mp)4496 mac_header_cook(mac_handle_t mh, mblk_t *mp)
4497 {
4498 	mac_impl_t *mip = (mac_impl_t *)mh;
4499 
4500 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4501 		if (DB_REF(mp) > 1) {
4502 			mblk_t *newmp = copymsg(mp);
4503 			if (newmp == NULL)
4504 				return (NULL);
4505 			freemsg(mp);
4506 			mp = newmp;
4507 		}
4508 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4509 		    mip->mi_pdata));
4510 	}
4511 	return (mp);
4512 }
4513 
4514 mblk_t *
mac_header_uncook(mac_handle_t mh,mblk_t * mp)4515 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4516 {
4517 	mac_impl_t *mip = (mac_impl_t *)mh;
4518 
4519 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4520 		if (DB_REF(mp) > 1) {
4521 			mblk_t *newmp = copymsg(mp);
4522 			if (newmp == NULL)
4523 				return (NULL);
4524 			freemsg(mp);
4525 			mp = newmp;
4526 		}
4527 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4528 		    mip->mi_pdata));
4529 	}
4530 	return (mp);
4531 }
4532 
4533 uint_t
mac_addr_len(mac_handle_t mh)4534 mac_addr_len(mac_handle_t mh)
4535 {
4536 	mac_impl_t *mip = (mac_impl_t *)mh;
4537 
4538 	return (mip->mi_type->mt_addr_length);
4539 }
4540 
4541 /* True if a MAC is a VNIC */
4542 boolean_t
mac_is_vnic(mac_handle_t mh)4543 mac_is_vnic(mac_handle_t mh)
4544 {
4545 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
4546 }
4547 
4548 boolean_t
mac_is_overlay(mac_handle_t mh)4549 mac_is_overlay(mac_handle_t mh)
4550 {
4551 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
4552 }
4553 
4554 mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)4555 mac_get_lower_mac_handle(mac_handle_t mh)
4556 {
4557 	mac_impl_t *mip = (mac_impl_t *)mh;
4558 
4559 	ASSERT(mac_is_vnic(mh));
4560 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4561 }
4562 
4563 boolean_t
mac_is_vnic_primary(mac_handle_t mh)4564 mac_is_vnic_primary(mac_handle_t mh)
4565 {
4566 	mac_impl_t *mip = (mac_impl_t *)mh;
4567 
4568 	ASSERT(mac_is_vnic(mh));
4569 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4570 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
4571 }
4572 
4573 void
mac_update_resources(mac_resource_props_t * nmrp,mac_resource_props_t * cmrp,boolean_t is_user_flow)4574 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4575     boolean_t is_user_flow)
4576 {
4577 	if (nmrp != NULL && cmrp != NULL) {
4578 		if (nmrp->mrp_mask & MRP_PRIORITY) {
4579 			if (nmrp->mrp_priority == MPL_RESET) {
4580 				cmrp->mrp_mask &= ~MRP_PRIORITY;
4581 				if (is_user_flow) {
4582 					cmrp->mrp_priority =
4583 					    MPL_SUBFLOW_DEFAULT;
4584 				} else {
4585 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
4586 				}
4587 			} else {
4588 				cmrp->mrp_mask |= MRP_PRIORITY;
4589 				cmrp->mrp_priority = nmrp->mrp_priority;
4590 			}
4591 		}
4592 		if (nmrp->mrp_mask & MRP_MAXBW) {
4593 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4594 				cmrp->mrp_mask &= ~MRP_MAXBW;
4595 				cmrp->mrp_maxbw = 0;
4596 			} else {
4597 				cmrp->mrp_mask |= MRP_MAXBW;
4598 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4599 			}
4600 		}
4601 		if (nmrp->mrp_mask & MRP_CPUS)
4602 			MAC_COPY_CPUS(nmrp, cmrp);
4603 
4604 		if (nmrp->mrp_mask & MRP_POOL) {
4605 			if (strlen(nmrp->mrp_pool) == 0) {
4606 				cmrp->mrp_mask &= ~MRP_POOL;
4607 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4608 			} else {
4609 				cmrp->mrp_mask |= MRP_POOL;
4610 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4611 				    sizeof (cmrp->mrp_pool));
4612 			}
4613 
4614 		}
4615 
4616 		if (nmrp->mrp_mask & MRP_PROTECT)
4617 			mac_protect_update(nmrp, cmrp);
4618 
4619 		/*
4620 		 * Update the rings specified.
4621 		 */
4622 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
4623 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4624 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
4625 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4626 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4627 				cmrp->mrp_nrxrings = 0;
4628 			} else {
4629 				cmrp->mrp_mask |= MRP_RX_RINGS;
4630 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4631 			}
4632 		}
4633 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
4634 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4635 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
4636 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4637 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4638 				cmrp->mrp_ntxrings = 0;
4639 			} else {
4640 				cmrp->mrp_mask |= MRP_TX_RINGS;
4641 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4642 			}
4643 		}
4644 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4645 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4646 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4647 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4648 
4649 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4650 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4651 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4652 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4653 	}
4654 }
4655 
4656 /*
4657  * i_mac_set_resources:
4658  *
4659  * This routine associates properties with the primary MAC client of
4660  * the specified MAC instance.
4661  * - Cache the properties in mac_impl_t
4662  * - Apply the properties to the primary MAC client if exists
4663  */
4664 int
i_mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4665 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4666 {
4667 	mac_impl_t		*mip = (mac_impl_t *)mh;
4668 	mac_client_impl_t	*mcip;
4669 	int			err = 0;
4670 	uint32_t		resmask, newresmask;
4671 	mac_resource_props_t	*tmrp, *umrp;
4672 
4673 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4674 
4675 	err = mac_validate_props(mip, mrp);
4676 	if (err != 0)
4677 		return (err);
4678 
4679 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4680 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4681 	resmask = umrp->mrp_mask;
4682 	mac_update_resources(mrp, umrp, B_FALSE);
4683 	newresmask = umrp->mrp_mask;
4684 
4685 	if (resmask == 0 && newresmask != 0) {
4686 		/*
4687 		 * Bandwidth, priority, cpu or pool link properties configured,
4688 		 * must disable fastpath.
4689 		 */
4690 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4691 			kmem_free(umrp, sizeof (*umrp));
4692 			return (err);
4693 		}
4694 	}
4695 
4696 	/*
4697 	 * Since bind_cpu may be modified by mac_client_set_resources()
4698 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4699 	 * This allows us to cache only user edits in mip.
4700 	 */
4701 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4702 	bcopy(mrp, tmrp, sizeof (*tmrp));
4703 	mcip = mac_primary_client_handle(mip);
4704 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4705 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4706 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4707 	    mrp->mrp_mask & MRP_TX_RINGS)) {
4708 		mac_client_impl_t	*vmcip;
4709 
4710 		/*
4711 		 * If the primary is not up, we need to check if there
4712 		 * are any VLANs on this primary. If there are then
4713 		 * we need to set this property on the VLANs since
4714 		 * VLANs follow the primary they are based on. Just
4715 		 * look for the first VLAN and change its properties,
4716 		 * all the other VLANs should be in the same group.
4717 		 */
4718 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
4719 		    vmcip = vmcip->mci_client_next) {
4720 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4721 			    mac_client_vid((mac_client_handle_t)vmcip) !=
4722 			    VLAN_ID_NONE) {
4723 				break;
4724 			}
4725 		}
4726 		if (vmcip != NULL) {
4727 			mac_resource_props_t	*omrp;
4728 			mac_resource_props_t	*vmrp;
4729 
4730 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4731 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4732 			/*
4733 			 * We dont' call mac_update_resources since we
4734 			 * want to take only the ring properties and
4735 			 * not all the properties that may have changed.
4736 			 */
4737 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
4738 			if (mrp->mrp_mask & MRP_RX_RINGS) {
4739 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4740 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
4741 					if (vmrp->mrp_mask &
4742 					    MRP_RXRINGS_UNSPEC) {
4743 						vmrp->mrp_mask &=
4744 						    ~MRP_RXRINGS_UNSPEC;
4745 					}
4746 					vmrp->mrp_nrxrings = 0;
4747 				} else {
4748 					vmrp->mrp_mask |= MRP_RX_RINGS;
4749 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4750 				}
4751 			}
4752 			if (mrp->mrp_mask & MRP_TX_RINGS) {
4753 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4754 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
4755 					if (vmrp->mrp_mask &
4756 					    MRP_TXRINGS_UNSPEC) {
4757 						vmrp->mrp_mask &=
4758 						    ~MRP_TXRINGS_UNSPEC;
4759 					}
4760 					vmrp->mrp_ntxrings = 0;
4761 				} else {
4762 					vmrp->mrp_mask |= MRP_TX_RINGS;
4763 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4764 				}
4765 			}
4766 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4767 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4768 
4769 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4770 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4771 
4772 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
4773 			    omrp)) != 0) {
4774 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4775 				    sizeof (*omrp));
4776 			} else {
4777 				mac_set_prim_vlan_rings(mip, vmrp);
4778 			}
4779 			kmem_free(omrp, sizeof (*omrp));
4780 		}
4781 	}
4782 
4783 	/* Only update the values if mac_client_set_resources succeeded */
4784 	if (err == 0) {
4785 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
4786 		/*
4787 		 * If bandwidth, priority or cpu link properties cleared,
4788 		 * renable fastpath.
4789 		 */
4790 		if (resmask != 0 && newresmask == 0)
4791 			mac_fastpath_enable((mac_handle_t)mip);
4792 	} else if (resmask == 0 && newresmask != 0) {
4793 		mac_fastpath_enable((mac_handle_t)mip);
4794 	}
4795 	kmem_free(tmrp, sizeof (*tmrp));
4796 	kmem_free(umrp, sizeof (*umrp));
4797 	return (err);
4798 }
4799 
4800 int
mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4801 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4802 {
4803 	int err;
4804 
4805 	i_mac_perim_enter((mac_impl_t *)mh);
4806 	err = i_mac_set_resources(mh, mrp);
4807 	i_mac_perim_exit((mac_impl_t *)mh);
4808 	return (err);
4809 }
4810 
4811 /*
4812  * Get the properties cached for the specified MAC instance.
4813  */
4814 void
mac_get_resources(mac_handle_t mh,mac_resource_props_t * mrp)4815 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4816 {
4817 	mac_impl_t		*mip = (mac_impl_t *)mh;
4818 	mac_client_impl_t	*mcip;
4819 
4820 	mcip = mac_primary_client_handle(mip);
4821 	if (mcip != NULL) {
4822 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4823 		return;
4824 	}
4825 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
4826 }
4827 
4828 /*
4829  * Get the effective properties from the primary client of the
4830  * specified MAC instance.
4831  */
4832 void
mac_get_effective_resources(mac_handle_t mh,mac_resource_props_t * mrp)4833 mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4834 {
4835 	mac_impl_t		*mip = (mac_impl_t *)mh;
4836 	mac_client_impl_t	*mcip;
4837 
4838 	mcip = mac_primary_client_handle(mip);
4839 	if (mcip != NULL) {
4840 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
4841 		    mrp);
4842 		return;
4843 	}
4844 	bzero(mrp, sizeof (mac_resource_props_t));
4845 }
4846 
4847 int
mac_set_pvid(mac_handle_t mh,uint16_t pvid)4848 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
4849 {
4850 	mac_impl_t *mip = (mac_impl_t *)mh;
4851 	mac_client_impl_t *mcip;
4852 	mac_unicast_impl_t *muip;
4853 
4854 	i_mac_perim_enter(mip);
4855 	if (pvid != 0) {
4856 		for (mcip = mip->mi_clients_list; mcip != NULL;
4857 		    mcip = mcip->mci_client_next) {
4858 			for (muip = mcip->mci_unicast_list; muip != NULL;
4859 			    muip = muip->mui_next) {
4860 				if (muip->mui_vid == pvid) {
4861 					i_mac_perim_exit(mip);
4862 					return (EBUSY);
4863 				}
4864 			}
4865 		}
4866 	}
4867 	mip->mi_pvid = pvid;
4868 	i_mac_perim_exit(mip);
4869 	return (0);
4870 }
4871 
4872 uint16_t
mac_get_pvid(mac_handle_t mh)4873 mac_get_pvid(mac_handle_t mh)
4874 {
4875 	mac_impl_t *mip = (mac_impl_t *)mh;
4876 
4877 	return (mip->mi_pvid);
4878 }
4879 
4880 uint32_t
mac_get_llimit(mac_handle_t mh)4881 mac_get_llimit(mac_handle_t mh)
4882 {
4883 	mac_impl_t *mip = (mac_impl_t *)mh;
4884 
4885 	return (mip->mi_llimit);
4886 }
4887 
4888 uint32_t
mac_get_ldecay(mac_handle_t mh)4889 mac_get_ldecay(mac_handle_t mh)
4890 {
4891 	mac_impl_t *mip = (mac_impl_t *)mh;
4892 
4893 	return (mip->mi_ldecay);
4894 }
4895 
4896 /*
4897  * Rename a mac client, its flow, and the kstat.
4898  */
4899 int
mac_rename_primary(mac_handle_t mh,const char * new_name)4900 mac_rename_primary(mac_handle_t mh, const char *new_name)
4901 {
4902 	mac_impl_t		*mip = (mac_impl_t *)mh;
4903 	mac_client_impl_t	*cur_clnt = NULL;
4904 	flow_entry_t		*fep;
4905 
4906 	i_mac_perim_enter(mip);
4907 
4908 	/*
4909 	 * VNICs: we need to change the sys flow name and
4910 	 * the associated flow kstat.
4911 	 */
4912 	if (mip->mi_state_flags & MIS_IS_VNIC) {
4913 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
4914 		ASSERT(new_name != NULL);
4915 		mac_rename_flow_names(mcip, new_name);
4916 		mac_stat_rename(mcip);
4917 		goto done;
4918 	}
4919 	/*
4920 	 * This mac may itself be an aggr link, or it may have some client
4921 	 * which is an aggr port. For both cases, we need to change the
4922 	 * aggr port's mac client name, its flow name and the associated flow
4923 	 * kstat.
4924 	 */
4925 	if (mip->mi_state_flags & MIS_IS_AGGR) {
4926 		mac_capab_aggr_t aggr_cap;
4927 		mac_rename_fn_t rename_fn;
4928 		boolean_t ret;
4929 
4930 		ASSERT(new_name != NULL);
4931 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4932 		    (void *)(&aggr_cap));
4933 		ASSERT(ret == B_TRUE);
4934 		rename_fn = aggr_cap.mca_rename_fn;
4935 		rename_fn(new_name, mip->mi_driver);
4936 		/*
4937 		 * The aggr's client name and kstat flow name will be
4938 		 * updated below, i.e. via mac_rename_flow_names.
4939 		 */
4940 	}
4941 
4942 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4943 	    cur_clnt = cur_clnt->mci_client_next) {
4944 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4945 			if (new_name != NULL) {
4946 				char *str_st = cur_clnt->mci_name;
4947 				char *str_del = strchr(str_st, '-');
4948 
4949 				ASSERT(str_del != NULL);
4950 				bzero(str_del + 1, MAXNAMELEN -
4951 				    (str_del - str_st + 1));
4952 				bcopy(new_name, str_del + 1,
4953 				    strlen(new_name));
4954 			}
4955 			fep = cur_clnt->mci_flent;
4956 			mac_rename_flow(fep, cur_clnt->mci_name);
4957 			break;
4958 		} else if (new_name != NULL &&
4959 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4960 			mac_rename_flow_names(cur_clnt, new_name);
4961 			break;
4962 		}
4963 	}
4964 
4965 	/* Recreate kstats associated with aggr pseudo rings */
4966 	if (mip->mi_state_flags & MIS_IS_AGGR)
4967 		mac_pseudo_ring_stat_rename(mip);
4968 
4969 done:
4970 	i_mac_perim_exit(mip);
4971 	return (0);
4972 }
4973 
4974 /*
4975  * Rename the MAC client's flow names
4976  */
4977 static void
mac_rename_flow_names(mac_client_impl_t * mcip,const char * new_name)4978 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4979 {
4980 	flow_entry_t	*flent;
4981 	uint16_t	vid;
4982 	char		flowname[MAXFLOWNAMELEN];
4983 	mac_impl_t	*mip = mcip->mci_mip;
4984 
4985 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4986 
4987 	/*
4988 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4989 	 * see a self-consistent value for mci_name
4990 	 */
4991 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
4992 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4993 	rw_exit(&mip->mi_rw_lock);
4994 
4995 	mac_rename_flow(mcip->mci_flent, new_name);
4996 
4997 	if (mcip->mci_nflents == 1)
4998 		return;
4999 
5000 	/*
5001 	 * We have to rename all the others too, no stats to destroy for
5002 	 * these.
5003 	 */
5004 	for (flent = mcip->mci_flent_list; flent != NULL;
5005 	    flent = flent->fe_client_next) {
5006 		if (flent != mcip->mci_flent) {
5007 			vid = i_mac_flow_vid(flent);
5008 			(void) sprintf(flowname, "%s%u", new_name, vid);
5009 			mac_flow_set_name(flent, flowname);
5010 		}
5011 	}
5012 }
5013 
5014 
5015 /*
5016  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
5017  * defined for the specified MAC client.
5018  */
5019 static void
mac_client_add_to_flow_list(mac_client_impl_t * mcip,flow_entry_t * flent)5020 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5021 {
5022 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5023 	/*
5024 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5025 	 * using mi_rw_lock
5026 	 */
5027 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5028 
5029 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5030 
5031 	/* Add it to the head */
5032 	flent->fe_client_next = mcip->mci_flent_list;
5033 	mcip->mci_flent_list = flent;
5034 	mcip->mci_nflents++;
5035 
5036 	/*
5037 	 * Keep track of the number of non-zero VIDs addresses per MAC
5038 	 * client to avoid figuring it out in the data-path.
5039 	 */
5040 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5041 		mcip->mci_nvids++;
5042 
5043 	rw_exit(&mcip->mci_rw_lock);
5044 }
5045 
5046 /*
5047  * Remove a flow entry from the MAC client's list.
5048  */
5049 static void
mac_client_remove_flow_from_list(mac_client_impl_t * mcip,flow_entry_t * flent)5050 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5051 {
5052 	flow_entry_t	*fe = mcip->mci_flent_list;
5053 	flow_entry_t	*prev_fe = NULL;
5054 
5055 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5056 	/*
5057 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5058 	 * using mci_rw_lock
5059 	 */
5060 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5061 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5062 
5063 	while ((fe != NULL) && (fe != flent)) {
5064 		prev_fe = fe;
5065 		fe = fe->fe_client_next;
5066 	}
5067 
5068 	ASSERT(fe != NULL);
5069 	if (prev_fe == NULL) {
5070 		/* Deleting the first node */
5071 		mcip->mci_flent_list = fe->fe_client_next;
5072 	} else {
5073 		prev_fe->fe_client_next = fe->fe_client_next;
5074 	}
5075 	mcip->mci_nflents--;
5076 
5077 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5078 		mcip->mci_nvids--;
5079 
5080 	rw_exit(&mcip->mci_rw_lock);
5081 }
5082 
5083 /*
5084  * Check if the given VID belongs to this MAC client.
5085  */
5086 boolean_t
mac_client_check_flow_vid(mac_client_impl_t * mcip,uint16_t vid)5087 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
5088 {
5089 	flow_entry_t	*flent;
5090 	uint16_t	mci_vid;
5091 	uint32_t	cache = mcip->mci_vidcache;
5092 
5093 	/*
5094 	 * In hopes of not having to touch the mci_rw_lock, check to see if
5095 	 * this vid matches our cached result.
5096 	 */
5097 	if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
5098 		return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
5099 
5100 	/* The mci_flent_list is protected by mci_rw_lock */
5101 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5102 	for (flent = mcip->mci_flent_list; flent != NULL;
5103 	    flent = flent->fe_client_next) {
5104 		mci_vid = i_mac_flow_vid(flent);
5105 		if (vid == mci_vid) {
5106 			mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
5107 			rw_exit(&mcip->mci_rw_lock);
5108 			return (B_TRUE);
5109 		}
5110 	}
5111 
5112 	mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
5113 	rw_exit(&mcip->mci_rw_lock);
5114 	return (B_FALSE);
5115 }
5116 
5117 /*
5118  * Get the flow entry for the specified <MAC addr, VID> tuple.
5119  */
5120 static flow_entry_t *
mac_client_get_flow(mac_client_impl_t * mcip,mac_unicast_impl_t * muip)5121 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
5122 {
5123 	mac_address_t *map = mcip->mci_unicast;
5124 	flow_entry_t *flent;
5125 	uint16_t vid;
5126 	flow_desc_t flow_desc;
5127 
5128 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5129 
5130 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5131 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5132 		return (NULL);
5133 
5134 	for (flent = mcip->mci_flent_list; flent != NULL;
5135 	    flent = flent->fe_client_next) {
5136 		vid = i_mac_flow_vid(flent);
5137 		if (vid == muip->mui_vid) {
5138 			return (flent);
5139 		}
5140 	}
5141 
5142 	return (NULL);
5143 }
5144 
5145 /*
5146  * Since mci_flent has the SRSs, when we want to remove it, we replace
5147  * the flow_desc_t in mci_flent with that of an existing flent and then
5148  * remove that flent instead of mci_flent.
5149  */
5150 static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t * mcip)5151 mac_client_swap_mciflent(mac_client_impl_t *mcip)
5152 {
5153 	flow_entry_t	*flent = mcip->mci_flent;
5154 	flow_tab_t	*ft = flent->fe_flow_tab;
5155 	flow_entry_t	*flent1;
5156 	flow_desc_t	fl_desc;
5157 	char		fl_name[MAXFLOWNAMELEN];
5158 	int		err;
5159 
5160 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5161 	ASSERT(mcip->mci_nflents > 1);
5162 
5163 	/* get the next flent following the primary flent  */
5164 	flent1 = mcip->mci_flent_list->fe_client_next;
5165 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5166 
5167 	/*
5168 	 * Remove the flent from the flow table before updating the
5169 	 * flow descriptor as the hash depends on the flow descriptor.
5170 	 * This also helps incoming packet classification avoid having
5171 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5172 	 * flow table is done under the fe_lock so that log or stat functions
5173 	 * see a self-consistent fe_flow_desc. The name and desc are specific
5174 	 * to a flow, the rest are shared by all the clients, including
5175 	 * resource control etc.
5176 	 */
5177 	mac_flow_remove(ft, flent, B_TRUE);
5178 	mac_flow_remove(ft, flent1, B_TRUE);
5179 
5180 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5181 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5182 
5183 	/* update the primary flow entry */
5184 	mutex_enter(&flent->fe_lock);
5185 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5186 	    sizeof (flow_desc_t));
5187 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5188 	mutex_exit(&flent->fe_lock);
5189 
5190 	/* update the flow entry that is to be freed */
5191 	mutex_enter(&flent1->fe_lock);
5192 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5193 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5194 	mutex_exit(&flent1->fe_lock);
5195 
5196 	/* now reinsert the flow entries in the table */
5197 	err = mac_flow_add(ft, flent);
5198 	ASSERT(err == 0);
5199 
5200 	err = mac_flow_add(ft, flent1);
5201 	ASSERT(err == 0);
5202 
5203 	return (flent1);
5204 }
5205 
5206 /*
5207  * Return whether there is only one flow entry associated with this
5208  * MAC client.
5209  */
5210 static boolean_t
mac_client_single_rcvr(mac_client_impl_t * mcip)5211 mac_client_single_rcvr(mac_client_impl_t *mcip)
5212 {
5213 	return (mcip->mci_nflents == 1);
5214 }
5215 
5216 int
mac_validate_props(mac_impl_t * mip,mac_resource_props_t * mrp)5217 mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5218 {
5219 	boolean_t		reset;
5220 	uint32_t		rings_needed;
5221 	uint32_t		rings_avail;
5222 	mac_group_type_t	gtype;
5223 	mac_resource_props_t	*mip_mrp;
5224 
5225 	if (mrp == NULL)
5226 		return (0);
5227 
5228 	if (mrp->mrp_mask & MRP_PRIORITY) {
5229 		mac_priority_level_t	pri = mrp->mrp_priority;
5230 
5231 		if (pri < MPL_LOW || pri > MPL_RESET)
5232 			return (EINVAL);
5233 	}
5234 
5235 	if (mrp->mrp_mask & MRP_MAXBW) {
5236 		uint64_t maxbw = mrp->mrp_maxbw;
5237 
5238 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5239 			return (EINVAL);
5240 	}
5241 	if (mrp->mrp_mask & MRP_CPUS) {
5242 		int i, j;
5243 		mac_cpu_mode_t	fanout;
5244 
5245 		if (mrp->mrp_ncpus > ncpus)
5246 			return (EINVAL);
5247 
5248 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5249 			for (j = 0; j < mrp->mrp_ncpus; j++) {
5250 				if (i != j &&
5251 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5252 					return (EINVAL);
5253 				}
5254 			}
5255 		}
5256 
5257 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5258 			cpu_t *cp;
5259 			int rv;
5260 
5261 			mutex_enter(&cpu_lock);
5262 			cp = cpu_get(mrp->mrp_cpu[i]);
5263 			if (cp != NULL)
5264 				rv = cpu_is_online(cp);
5265 			else
5266 				rv = 0;
5267 			mutex_exit(&cpu_lock);
5268 			if (rv == 0)
5269 				return (EINVAL);
5270 		}
5271 
5272 		fanout = mrp->mrp_fanout_mode;
5273 		if (fanout < 0 || fanout > MCM_CPUS)
5274 			return (EINVAL);
5275 	}
5276 
5277 	if (mrp->mrp_mask & MRP_PROTECT) {
5278 		int err = mac_protect_validate(mrp);
5279 		if (err != 0)
5280 			return (err);
5281 	}
5282 
5283 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
5284 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
5285 		return (0);
5286 	}
5287 
5288 	/*
5289 	 * mip will be null when we come from mac_flow_create or
5290 	 * mac_link_flow_modify. In the latter case it is a user flow,
5291 	 * for which we don't support rings. In the former we would
5292 	 * have validated the props beforehand (i_mac_unicast_add ->
5293 	 * mac_client_set_resources -> validate for the primary and
5294 	 * vnic_dev_create -> mac_client_set_resources -> validate for
5295 	 * a vnic.
5296 	 */
5297 	if (mip == NULL)
5298 		return (0);
5299 
5300 	/*
5301 	 * We don't support setting rings property for a VNIC that is using a
5302 	 * primary address (VLAN)
5303 	 */
5304 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
5305 	    mac_is_vnic_primary((mac_handle_t)mip)) {
5306 		return (ENOTSUP);
5307 	}
5308 
5309 	mip_mrp = &mip->mi_resource_props;
5310 	/*
5311 	 * The rings property should be validated against the NICs
5312 	 * resources
5313 	 */
5314 	if (mip->mi_state_flags & MIS_IS_VNIC)
5315 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
5316 
5317 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
5318 	/*
5319 	 * If groups are not supported, return error.
5320 	 */
5321 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
5322 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
5323 		return (EINVAL);
5324 	}
5325 	/*
5326 	 * If we are just resetting, there is no validation needed.
5327 	 */
5328 	if (reset)
5329 		return (0);
5330 
5331 	if (mrp->mrp_mask & MRP_RX_RINGS) {
5332 		rings_needed = mrp->mrp_nrxrings;
5333 		/*
5334 		 * We just want to check if the number of additional
5335 		 * rings requested is available.
5336 		 */
5337 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
5338 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
5339 				/* Just check for the additional rings */
5340 				rings_needed -= mip_mrp->mrp_nrxrings;
5341 			else
5342 				/* We are not asking for additional rings */
5343 				rings_needed = 0;
5344 		}
5345 		rings_avail = mip->mi_rxrings_avail;
5346 		gtype = mip->mi_rx_group_type;
5347 	} else {
5348 		rings_needed = mrp->mrp_ntxrings;
5349 		/* Similarly for the TX rings */
5350 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
5351 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
5352 				/* Just check for the additional rings */
5353 				rings_needed -= mip_mrp->mrp_ntxrings;
5354 			else
5355 				/* We are not asking for additional rings */
5356 				rings_needed = 0;
5357 		}
5358 		rings_avail = mip->mi_txrings_avail;
5359 		gtype = mip->mi_tx_group_type;
5360 	}
5361 
5362 	/* Error if the group is dynamic .. */
5363 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5364 		/*
5365 		 * .. and rings specified are more than available.
5366 		 */
5367 		if (rings_needed > rings_avail)
5368 			return (EINVAL);
5369 	} else {
5370 		/*
5371 		 * OR group is static and we have specified some rings.
5372 		 */
5373 		if (rings_needed > 0)
5374 			return (EINVAL);
5375 	}
5376 	return (0);
5377 }
5378 
5379 /*
5380  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5381  * underlying physical link is down. This is to allow MAC clients to
5382  * communicate with other clients.
5383  */
5384 void
mac_virtual_link_update(mac_impl_t * mip)5385 mac_virtual_link_update(mac_impl_t *mip)
5386 {
5387 	if (mip->mi_linkstate != LINK_STATE_UP)
5388 		i_mac_notify(mip, MAC_NOTE_LINK);
5389 }
5390 
5391 /*
5392  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5393  * mac handle in the client.
5394  */
5395 void
mac_set_upper_mac(mac_client_handle_t mch,mac_handle_t mh,mac_resource_props_t * mrp)5396 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5397     mac_resource_props_t *mrp)
5398 {
5399 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5400 	mac_impl_t		*mip = (mac_impl_t *)mh;
5401 
5402 	mcip->mci_upper_mip = mip;
5403 	/* If there are any properties, copy it over too */
5404 	if (mrp != NULL) {
5405 		bcopy(mrp, &mip->mi_resource_props,
5406 		    sizeof (mac_resource_props_t));
5407 	}
5408 }
5409 
5410 /*
5411  * Mark the mac as being used exclusively by the single mac client that is
5412  * doing some control operation on this mac. No further opens of this mac
5413  * will be allowed until this client calls mac_unmark_exclusive. The mac
5414  * client calling this function must already be in the mac perimeter
5415  */
5416 int
mac_mark_exclusive(mac_handle_t mh)5417 mac_mark_exclusive(mac_handle_t mh)
5418 {
5419 	mac_impl_t	*mip = (mac_impl_t *)mh;
5420 
5421 	ASSERT(MAC_PERIM_HELD(mh));
5422 	/*
5423 	 * Look up its entry in the global hash table.
5424 	 */
5425 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5426 	if (mip->mi_state_flags & MIS_DISABLED) {
5427 		rw_exit(&i_mac_impl_lock);
5428 		return (ENOENT);
5429 	}
5430 
5431 	/*
5432 	 * A reference to mac is held even if the link is not plumbed.
5433 	 * In i_dls_link_create() we open the MAC interface and hold the
5434 	 * reference. There is an additional reference for the mac_open
5435 	 * done in acquiring the mac perimeter
5436 	 */
5437 	if (mip->mi_ref != 2) {
5438 		rw_exit(&i_mac_impl_lock);
5439 		return (EBUSY);
5440 	}
5441 
5442 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5443 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5444 	rw_exit(&i_mac_impl_lock);
5445 	return (0);
5446 }
5447 
5448 void
mac_unmark_exclusive(mac_handle_t mh)5449 mac_unmark_exclusive(mac_handle_t mh)
5450 {
5451 	mac_impl_t	*mip = (mac_impl_t *)mh;
5452 
5453 	ASSERT(MAC_PERIM_HELD(mh));
5454 
5455 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5456 	/* 1 for the creation and another for the perimeter */
5457 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5458 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5459 	rw_exit(&i_mac_impl_lock);
5460 }
5461 
5462 /*
5463  * Set the MTU for the specified MAC.
5464  */
5465 int
mac_set_mtu(mac_handle_t mh,uint_t new_mtu,uint_t * old_mtu_arg)5466 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5467 {
5468 	mac_impl_t *mip = (mac_impl_t *)mh;
5469 	uint_t old_mtu;
5470 	int rv = 0;
5471 
5472 	i_mac_perim_enter(mip);
5473 
5474 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5475 		rv = ENOTSUP;
5476 		goto bail;
5477 	}
5478 
5479 	old_mtu = mip->mi_sdu_max;
5480 
5481 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5482 		rv = EINVAL;
5483 		goto bail;
5484 	}
5485 
5486 	rw_enter(&mip->mi_rw_lock, RW_READER);
5487 	if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5488 		rv = EBUSY;
5489 		rw_exit(&mip->mi_rw_lock);
5490 		goto bail;
5491 	}
5492 	rw_exit(&mip->mi_rw_lock);
5493 
5494 	if (old_mtu != new_mtu) {
5495 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5496 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5497 		if (rv != 0)
5498 			goto bail;
5499 		rv = mac_maxsdu_update(mh, new_mtu);
5500 		ASSERT(rv == 0);
5501 	}
5502 
5503 bail:
5504 	i_mac_perim_exit(mip);
5505 
5506 	if (rv == 0 && old_mtu_arg != NULL)
5507 		*old_mtu_arg = old_mtu;
5508 	return (rv);
5509 }
5510 
5511 /*
5512  * Return the RX h/w information for the group indexed by grp_num.
5513  */
5514 void
mac_get_hwrxgrp_info(mac_handle_t mh,int grp_index,uint_t * grp_num,uint_t * n_rings,uint_t * rings,uint_t * type,uint_t * n_clnts,char * clnts_name)5515 mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5516     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5517     char *clnts_name)
5518 {
5519 	mac_impl_t *mip = (mac_impl_t *)mh;
5520 	mac_grp_client_t *mcip;
5521 	uint_t i = 0, index = 0;
5522 	mac_ring_t	*ring;
5523 
5524 	/* Revisit when we implement fully dynamic group allocation */
5525 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5526 
5527 	rw_enter(&mip->mi_rw_lock, RW_READER);
5528 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5529 	*type = mip->mi_rx_groups[grp_index].mrg_type;
5530 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5531 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
5532 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5533 	    index++) {
5534 		rings[index] = ring->mr_index;
5535 		ring = ring->mr_next;
5536 	}
5537 	/* Assuming the 1st is the default group */
5538 	index = 0;
5539 	if (grp_index == 0) {
5540 		(void) strlcpy(clnts_name, "<default,mcast>,",
5541 		    MAXCLIENTNAMELEN);
5542 		index += strlen("<default,mcast>,");
5543 	}
5544 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5545 	    mcip = mcip->mgc_next) {
5546 		int name_len = strlen(mcip->mgc_client->mci_name);
5547 
5548 		/*
5549 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5550 		 * names.
5551 		 * XXXX Formating the client name string needs to be moved
5552 		 * to user land when fixing the size of dhi_clnts in
5553 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5554 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5555 		 */
5556 		if (index + name_len >= MAXCLIENTNAMELEN) {
5557 			index = MAXCLIENTNAMELEN;
5558 			break;
5559 		}
5560 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5561 		    name_len);
5562 		index += name_len;
5563 		clnts_name[index++] = ',';
5564 		i++;
5565 	}
5566 
5567 	/* Get rid of the last , */
5568 	if (index > 0)
5569 		clnts_name[index - 1] = '\0';
5570 	*n_clnts = i;
5571 	rw_exit(&mip->mi_rw_lock);
5572 }
5573 
5574 /*
5575  * Return the TX h/w information for the group indexed by grp_num.
5576  */
5577 void
mac_get_hwtxgrp_info(mac_handle_t mh,int grp_index,uint_t * grp_num,uint_t * n_rings,uint_t * rings,uint_t * type,uint_t * n_clnts,char * clnts_name)5578 mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5579     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5580     char *clnts_name)
5581 {
5582 	mac_impl_t *mip = (mac_impl_t *)mh;
5583 	mac_grp_client_t *mcip;
5584 	uint_t i = 0, index = 0;
5585 	mac_ring_t	*ring;
5586 
5587 	/* Revisit when we implement fully dynamic group allocation */
5588 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5589 
5590 	rw_enter(&mip->mi_rw_lock, RW_READER);
5591 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5592 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5593 	*type = mip->mi_tx_groups[grp_index].mrg_type;
5594 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5595 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
5596 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5597 	    index++) {
5598 		rings[index] = ring->mr_index;
5599 		ring = ring->mr_next;
5600 	}
5601 	index = 0;
5602 	/* Default group has an index of -1 */
5603 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5604 		(void) strlcpy(clnts_name, "<default>,",
5605 		    MAXCLIENTNAMELEN);
5606 		index += strlen("<default>,");
5607 	}
5608 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5609 	    mcip = mcip->mgc_next) {
5610 		int name_len = strlen(mcip->mgc_client->mci_name);
5611 
5612 		/*
5613 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5614 		 * names.
5615 		 * XXXX Formating the client name string needs to be moved
5616 		 * to user land when fixing the size of dhi_clnts in
5617 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5618 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5619 		 */
5620 		if (index + name_len >= MAXCLIENTNAMELEN) {
5621 			index = MAXCLIENTNAMELEN;
5622 			break;
5623 		}
5624 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5625 		    name_len);
5626 		index += name_len;
5627 		clnts_name[index++] = ',';
5628 		i++;
5629 	}
5630 
5631 	/* Get rid of the last , */
5632 	if (index > 0)
5633 		clnts_name[index - 1] = '\0';
5634 	*n_clnts = i;
5635 	rw_exit(&mip->mi_rw_lock);
5636 }
5637 
5638 /*
5639  * Return the group count for RX or TX.
5640  */
5641 uint_t
mac_hwgrp_num(mac_handle_t mh,int type)5642 mac_hwgrp_num(mac_handle_t mh, int type)
5643 {
5644 	mac_impl_t *mip = (mac_impl_t *)mh;
5645 
5646 	/*
5647 	 * Return the Rx and Tx group count; for the Tx we need to
5648 	 * include the default too.
5649 	 */
5650 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5651 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5652 }
5653 
5654 /*
5655  * The total number of free TX rings for this MAC.
5656  */
5657 uint_t
mac_txavail_get(mac_handle_t mh)5658 mac_txavail_get(mac_handle_t mh)
5659 {
5660 	mac_impl_t	*mip = (mac_impl_t *)mh;
5661 
5662 	return (mip->mi_txrings_avail);
5663 }
5664 
5665 /*
5666  * The total number of free RX rings for this MAC.
5667  */
5668 uint_t
mac_rxavail_get(mac_handle_t mh)5669 mac_rxavail_get(mac_handle_t mh)
5670 {
5671 	mac_impl_t	*mip = (mac_impl_t *)mh;
5672 
5673 	return (mip->mi_rxrings_avail);
5674 }
5675 
5676 /*
5677  * The total number of reserved RX rings on this MAC.
5678  */
5679 uint_t
mac_rxrsvd_get(mac_handle_t mh)5680 mac_rxrsvd_get(mac_handle_t mh)
5681 {
5682 	mac_impl_t	*mip = (mac_impl_t *)mh;
5683 
5684 	return (mip->mi_rxrings_rsvd);
5685 }
5686 
5687 /*
5688  * The total number of reserved TX rings on this MAC.
5689  */
5690 uint_t
mac_txrsvd_get(mac_handle_t mh)5691 mac_txrsvd_get(mac_handle_t mh)
5692 {
5693 	mac_impl_t	*mip = (mac_impl_t *)mh;
5694 
5695 	return (mip->mi_txrings_rsvd);
5696 }
5697 
5698 /*
5699  * Total number of free RX groups on this MAC.
5700  */
5701 uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)5702 mac_rxhwlnksavail_get(mac_handle_t mh)
5703 {
5704 	mac_impl_t	*mip = (mac_impl_t *)mh;
5705 
5706 	return (mip->mi_rxhwclnt_avail);
5707 }
5708 
5709 /*
5710  * Total number of RX groups reserved on this MAC.
5711  */
5712 uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)5713 mac_rxhwlnksrsvd_get(mac_handle_t mh)
5714 {
5715 	mac_impl_t	*mip = (mac_impl_t *)mh;
5716 
5717 	return (mip->mi_rxhwclnt_used);
5718 }
5719 
5720 /*
5721  * Total number of free TX groups on this MAC.
5722  */
5723 uint_t
mac_txhwlnksavail_get(mac_handle_t mh)5724 mac_txhwlnksavail_get(mac_handle_t mh)
5725 {
5726 	mac_impl_t	*mip = (mac_impl_t *)mh;
5727 
5728 	return (mip->mi_txhwclnt_avail);
5729 }
5730 
5731 /*
5732  * Total number of TX groups reserved on this MAC.
5733  */
5734 uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)5735 mac_txhwlnksrsvd_get(mac_handle_t mh)
5736 {
5737 	mac_impl_t	*mip = (mac_impl_t *)mh;
5738 
5739 	return (mip->mi_txhwclnt_used);
5740 }
5741 
5742 /*
5743  * Initialize the rings property for a mac client. A non-0 value for
5744  * rxring or txring specifies the number of rings required, a value
5745  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5746  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5747  * means the system can decide whether it can give any rings or not.
5748  */
5749 void
mac_client_set_rings(mac_client_handle_t mch,int rxrings,int txrings)5750 mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5751 {
5752 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5753 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
5754 
5755 	if (rxrings != MAC_RXRINGS_DONTCARE) {
5756 		mrp->mrp_mask |= MRP_RX_RINGS;
5757 		mrp->mrp_nrxrings = rxrings;
5758 	}
5759 
5760 	if (txrings != MAC_TXRINGS_DONTCARE) {
5761 		mrp->mrp_mask |= MRP_TX_RINGS;
5762 		mrp->mrp_ntxrings = txrings;
5763 	}
5764 }
5765 
5766 boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)5767 mac_get_promisc_filtered(mac_client_handle_t mch)
5768 {
5769 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5770 
5771 	return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
5772 }
5773 
5774 void
mac_set_promisc_filtered(mac_client_handle_t mch,boolean_t enable)5775 mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
5776 {
5777 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5778 
5779 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5780 	if (enable)
5781 		mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
5782 	else
5783 		mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
5784 }
5785