xref: /illumos-gate/usr/src/uts/common/io/mac/mac_client.c (revision 7c4d873fd0070a34c29f9e6a7ca74f5cf4d2ab6f)
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
2530  * can specify that it wants to use the primary MAC address of the
2531  * underlying MAC. See the introductory comments at the beginning
2532  * of this file for more more information on primary MAC addresses.
2533  *
2534  * Note also the tuple (MAC address, VID) must be unique
2535  * for the MAC clients defined on top of the same underlying MAC
2536  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
2537  *
2538  * In no case can a client use the PVID for the MAC, if the MAC has one set.
2539  */
2540 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)2541 i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2542     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2543 {
2544 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2545 	mac_impl_t		*mip = mcip->mci_mip;
2546 	int			err;
2547 	uint_t			mac_len = mip->mi_type->mt_addr_length;
2548 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
2549 	boolean_t		fastpath_disabled = B_FALSE;
2550 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
2551 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
2552 	mac_resource_props_t	*mrp;
2553 	boolean_t		passive_client = B_FALSE;
2554 	mac_unicast_impl_t	*muip;
2555 	boolean_t		is_vnic_primary =
2556 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
2557 
2558 	/*
2559 	 * When the VID is non-zero the underlying MAC cannot be a
2560 	 * VNIC. I.e., dladm create-vlan cannot take a VNIC as
2561 	 * argument, only the primary MAC client.
2562 	 */
2563 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != VLAN_ID_NONE)));
2564 
2565 	*diag = MAC_DIAG_NONE;
2566 
2567 	/*
2568 	 * Can't unicast add if the client asked only for minimal datapath
2569 	 * setup.
2570 	 */
2571 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
2572 		return (ENOTSUP);
2573 
2574 	/*
2575 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
2576 	 * No client may use it.
2577 	 */
2578 	if (mip->mi_pvid != VLAN_ID_NONE && vid == mip->mi_pvid)
2579 		return (EBUSY);
2580 
2581 	/*
2582 	 * Check whether it's the primary client and flag it.
2583 	 */
2584 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2585 	    vid == VLAN_ID_NONE)
2586 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
2587 
2588 	/*
2589 	 * is_vnic_primary is true when we come here as a VLAN VNIC
2590 	 * which uses the primary MAC client's address but with a non-zero
2591 	 * VID. In this case the MAC address is not specified by an upper
2592 	 * MAC client.
2593 	 */
2594 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2595 	    !is_vnic_primary) {
2596 		/*
2597 		 * The address is being set by the upper MAC client
2598 		 * of a VNIC. The MAC address was already set by the
2599 		 * VNIC driver during VNIC creation.
2600 		 *
2601 		 * Note: a VNIC has only one MAC address. We return
2602 		 * the MAC unicast address handle of the lower MAC client
2603 		 * corresponding to the VNIC. We allocate a new entry
2604 		 * which is flagged appropriately, so that mac_unicast_remove()
2605 		 * doesn't attempt to free the original entry that
2606 		 * was allocated by the VNIC driver.
2607 		 */
2608 		ASSERT(mcip->mci_unicast != NULL);
2609 
2610 		/* Check for VLAN flags, if present */
2611 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2612 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2613 
2614 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2615 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2616 
2617 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2618 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2619 
2620 		/*
2621 		 * Ensure that the primary unicast address of the VNIC
2622 		 * is added only once unless we have the
2623 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
2624 		 * a passive MAC client).
2625 		 */
2626 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
2627 			if ((mcip->mci_flags &
2628 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2629 			    (mcip->mci_flags &
2630 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2631 				return (EBUSY);
2632 			}
2633 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2634 			passive_client = B_TRUE;
2635 		}
2636 
2637 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
2638 
2639 		/*
2640 		 * Create a handle for vid 0.
2641 		 */
2642 		ASSERT(vid == VLAN_ID_NONE);
2643 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2644 		muip->mui_vid = vid;
2645 		*mah = (mac_unicast_handle_t)muip;
2646 		/*
2647 		 * This will be used by the caller to defer setting the
2648 		 * rx functions.
2649 		 */
2650 		if (passive_client)
2651 			return (EAGAIN);
2652 		return (0);
2653 	}
2654 
2655 	/* primary MAC clients cannot be opened on top of anchor VNICs */
2656 	if ((is_vnic_primary || is_primary) &&
2657 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
2658 		return (ENXIO);
2659 	}
2660 
2661 	/*
2662 	 * If this is a VNIC/VLAN, disable softmac fast-path. This is
2663 	 * only relevant to legacy devices which use softmac to
2664 	 * interface with GLDv3.
2665 	 */
2666 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2667 		err = mac_fastpath_disable((mac_handle_t)mip);
2668 		if (err != 0)
2669 			return (err);
2670 		fastpath_disabled = B_TRUE;
2671 	}
2672 
2673 	/*
2674 	 * Return EBUSY if:
2675 	 *  - there is an exclusively active mac client exists.
2676 	 *  - this is an exclusive active mac client but
2677 	 *	a. there is already active mac clients exist, or
2678 	 *	b. fastpath streams are already plumbed on this legacy device
2679 	 *  - the mac creator has disallowed active mac clients.
2680 	 */
2681 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
2682 		if (fastpath_disabled)
2683 			mac_fastpath_enable((mac_handle_t)mip);
2684 		return (EBUSY);
2685 	}
2686 
2687 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2688 		ASSERT(!fastpath_disabled);
2689 		if (mip->mi_nactiveclients != 0)
2690 			return (EBUSY);
2691 
2692 		if ((mip->mi_state_flags & MIS_LEGACY) &&
2693 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
2694 			return (EBUSY);
2695 		}
2696 		mip->mi_state_flags |= MIS_EXCLUSIVE;
2697 	}
2698 
2699 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
2700 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
2701 	    MCIS_IS_AGGR_PORT))) {
2702 		/*
2703 		 * Apply the property cached in the mac_impl_t to the primary
2704 		 * mac client. If the mac client is a VNIC or an aggregation
2705 		 * port, its property should be set in the mcip when the
2706 		 * VNIC/aggr was created.
2707 		 */
2708 		mac_get_resources((mac_handle_t)mip, mrp);
2709 		(void) mac_client_set_resources(mch, mrp);
2710 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2711 		/*
2712 		 * This is a VLAN client sharing the address of the
2713 		 * primary MAC client; i.e., one created via dladm
2714 		 * create-vlan. We don't support specifying ring
2715 		 * properties for this type of client as it inherits
2716 		 * these from the primary MAC client.
2717 		 */
2718 		if (is_vnic_primary) {
2719 			mac_resource_props_t	*vmrp;
2720 
2721 			vmrp = MCIP_RESOURCE_PROPS(mcip);
2722 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
2723 			    vmrp->mrp_mask & MRP_TX_RINGS) {
2724 				if (fastpath_disabled)
2725 					mac_fastpath_enable((mac_handle_t)mip);
2726 				kmem_free(mrp, sizeof (*mrp));
2727 				return (ENOTSUP);
2728 			}
2729 			/*
2730 			 * Additionally we also need to inherit any
2731 			 * rings property from the MAC.
2732 			 */
2733 			mac_get_resources((mac_handle_t)mip, mrp);
2734 			if (mrp->mrp_mask & MRP_RX_RINGS) {
2735 				vmrp->mrp_mask |= MRP_RX_RINGS;
2736 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
2737 			}
2738 			if (mrp->mrp_mask & MRP_TX_RINGS) {
2739 				vmrp->mrp_mask |= MRP_TX_RINGS;
2740 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
2741 			}
2742 		}
2743 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
2744 	}
2745 
2746 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2747 	muip->mui_vid = vid;
2748 
2749 	if (is_primary || is_vnic_primary) {
2750 		mac_addr = mip->mi_addr;
2751 	} else {
2752 
2753 		/*
2754 		 * Verify the validity of the specified MAC addresses value.
2755 		 */
2756 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
2757 			*diag = MAC_DIAG_MACADDR_INVALID;
2758 			err = EINVAL;
2759 			goto bail_out;
2760 		}
2761 
2762 		/*
2763 		 * Make sure that the specified MAC address is different
2764 		 * than the unicast MAC address of the underlying NIC.
2765 		 */
2766 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
2767 			*diag = MAC_DIAG_MACADDR_NIC;
2768 			err = EINVAL;
2769 			goto bail_out;
2770 		}
2771 	}
2772 
2773 	/*
2774 	 * Set the flags here so that if this is a passive client, we
2775 	 * can return and set it when we call mac_client_datapath_setup
2776 	 * when this becomes the active client. If we defer to using these
2777 	 * flags to mac_client_datapath_setup, then for a passive client,
2778 	 * we'd have to store the flags somewhere (probably fe_flags)
2779 	 * and then use it.
2780 	 */
2781 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2782 		if (is_unicast_hw) {
2783 			/*
2784 			 * The client requires a hardware MAC address slot
2785 			 * for that unicast address. Since we support only
2786 			 * one unicast MAC address per client, flag the
2787 			 * MAC client itself.
2788 			 */
2789 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
2790 		}
2791 
2792 		/* Check for VLAN flags, if present */
2793 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2794 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2795 
2796 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2797 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2798 
2799 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2800 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2801 	} else {
2802 		/*
2803 		 * Assert that the specified flags are consistent with the
2804 		 * flags specified by previous calls to mac_unicast_add().
2805 		 */
2806 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
2807 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
2808 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
2809 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
2810 
2811 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
2812 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
2813 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
2814 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
2815 
2816 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
2817 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
2818 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
2819 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
2820 
2821 		/*
2822 		 * Make sure the client is consistent about its requests
2823 		 * for MAC addresses. I.e. all requests from the clients
2824 		 * must have the MAC_UNICAST_HW flag set or clear.
2825 		 */
2826 		if (((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
2827 		    !is_unicast_hw) ||
2828 		    ((mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
2829 		    is_unicast_hw)) {
2830 			err = EINVAL;
2831 			goto bail_out;
2832 		}
2833 	}
2834 	/*
2835 	 * Make sure the MAC address is not already used by
2836 	 * another MAC client defined on top of the same
2837 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
2838 	 * set when we allow a passive client to be present which will
2839 	 * be activated when the currently active client goes away - this
2840 	 * works only with primary addresses.
2841 	 */
2842 	if ((check_dups || is_primary || is_vnic_primary) &&
2843 	    mac_addr_in_use(mip, mac_addr, vid)) {
2844 		/*
2845 		 * Must have set the multiple primary address flag when
2846 		 * we did a mac_client_open AND this should be a primary
2847 		 * MAC client AND there should not already be a passive
2848 		 * primary. If all is true then we let this succeed
2849 		 * even if the address is a dup.
2850 		 */
2851 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2852 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
2853 		    mac_get_passive_primary_client(mip) != NULL) {
2854 			*diag = MAC_DIAG_MACADDR_INUSE;
2855 			err = EEXIST;
2856 			goto bail_out;
2857 		}
2858 		ASSERT((mcip->mci_flags &
2859 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
2860 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2861 		kmem_free(mrp, sizeof (*mrp));
2862 
2863 		/*
2864 		 * Stash the unicast address handle, we will use it when
2865 		 * we set up the passive client.
2866 		 */
2867 		mcip->mci_p_unicast_list = muip;
2868 		*mah = (mac_unicast_handle_t)muip;
2869 		return (0);
2870 	}
2871 
2872 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
2873 	    is_primary || is_vnic_primary, muip);
2874 	if (err != 0)
2875 		goto bail_out;
2876 
2877 	kmem_free(mrp, sizeof (*mrp));
2878 	*mah = (mac_unicast_handle_t)muip;
2879 	return (0);
2880 
2881 bail_out:
2882 	if (fastpath_disabled)
2883 		mac_fastpath_enable((mac_handle_t)mip);
2884 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2885 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2886 		if (mip->mi_state_flags & MIS_LEGACY) {
2887 			mip->mi_capab_legacy.ml_active_clear(
2888 			    mip->mi_driver);
2889 		}
2890 	}
2891 	kmem_free(mrp, sizeof (*mrp));
2892 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2893 	return (err);
2894 }
2895 
2896 /*
2897  * Wrapper function to mac_unicast_add when we want to have the same mac
2898  * client open for two instances, one that is currently active and another
2899  * that will become active when the current one is removed. In this case
2900  * mac_unicast_add will return EGAIN and we will save the rx function and
2901  * arg which will be used when we activate the passive client in
2902  * mac_unicast_remove.
2903  */
2904 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)2905 mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
2906     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
2907     mac_rx_t rx_fn, void *arg)
2908 {
2909 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2910 	uint_t			err;
2911 
2912 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2913 	if (err != 0 && err != EAGAIN)
2914 		return (err);
2915 	if (err == EAGAIN) {
2916 		if (rx_fn != NULL) {
2917 			mcip->mci_rx_p_fn = rx_fn;
2918 			mcip->mci_rx_p_arg = arg;
2919 		}
2920 		return (0);
2921 	}
2922 	if (rx_fn != NULL)
2923 		mac_rx_set(mch, rx_fn, arg);
2924 	return (err);
2925 }
2926 
2927 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)2928 mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2929     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2930 {
2931 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
2932 	uint_t err;
2933 
2934 	i_mac_perim_enter(mip);
2935 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2936 	i_mac_perim_exit(mip);
2937 
2938 	return (err);
2939 }
2940 
2941 static void
mac_client_datapath_teardown(mac_client_handle_t mch,mac_unicast_impl_t * muip,flow_entry_t * flent)2942 mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
2943     flow_entry_t *flent)
2944 {
2945 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2946 	mac_impl_t		*mip = mcip->mci_mip;
2947 	boolean_t		no_unicast;
2948 
2949 	/*
2950 	 * If we have not added a unicast address for this MAC client, just
2951 	 * teardown the datapath.
2952 	 */
2953 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2954 
2955 	if (!no_unicast) {
2956 		/*
2957 		 * We would have initialized subflows etc. only if we brought
2958 		 * up the primary client and set the unicast unicast address
2959 		 * etc. Deactivate the flows. The flow entry will be removed
2960 		 * from the active flow tables, and the associated SRS,
2961 		 * softrings etc will be deleted. But the flow entry itself
2962 		 * won't be destroyed, instead it will continue to be archived
2963 		 * off the  the global flow hash list, for a possible future
2964 		 * activation when say IP is plumbed again.
2965 		 */
2966 		mac_link_release_flows(mch);
2967 	}
2968 	mip->mi_nactiveclients--;
2969 	mac_update_single_active_client(mip);
2970 
2971 	/* Tear down the data path */
2972 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
2973 
2974 	/*
2975 	 * Prevent any future access to the flow entry through the mci_flent
2976 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
2977 	 * mac_bcast_send is also under mi_rw_lock.
2978 	 */
2979 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2980 	flent = mcip->mci_flent;
2981 	mac_client_remove_flow_from_list(mcip, flent);
2982 
2983 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
2984 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
2985 
2986 	/*
2987 	 * This is the last unicast address being removed and there shouldn't
2988 	 * be any outbound data threads at this point coming down from mac
2989 	 * clients. We have waited for the data threads to finish before
2990 	 * starting dld_str_detach. Non-data threads must access TX SRS
2991 	 * under mi_rw_lock.
2992 	 */
2993 	rw_exit(&mip->mi_rw_lock);
2994 
2995 	/*
2996 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
2997 	 * contain other flags, such as FE_CONDEMNED, which we need to
2998 	 * cleared. We don't call mac_flow_cleanup() for this unicast
2999 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
3000 	 * path). We just clear the stats and reset the initial callback
3001 	 * function, the rest will be set when we call mac_flow_create,
3002 	 * if at all.
3003 	 */
3004 	mutex_enter(&flent->fe_lock);
3005 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
3006 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
3007 	flent->fe_flags = FE_MC_NO_DATAPATH;
3008 	flow_stat_destroy(flent);
3009 	mac_misc_stat_delete(flent);
3010 
3011 	/* Initialize the receiver function to a safe routine */
3012 	flent->fe_cb_fn = (flow_fn_t)mac_rx_def;
3013 	flent->fe_cb_arg1 = NULL;
3014 	flent->fe_cb_arg2 = NULL;
3015 
3016 	flent->fe_index = -1;
3017 	mutex_exit(&flent->fe_lock);
3018 
3019 	if (mip->mi_type->mt_brdcst_addr != NULL) {
3020 		ASSERT(muip != NULL || no_unicast);
3021 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3022 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
3023 	}
3024 
3025 	if (mip->mi_nactiveclients == 1) {
3026 		mac_capab_update((mac_handle_t)mip);
3027 		mac_virtual_link_update(mip);
3028 	}
3029 
3030 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
3031 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
3032 
3033 		if (mip->mi_state_flags & MIS_LEGACY)
3034 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
3035 	}
3036 
3037 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3038 
3039 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3040 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3041 
3042 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3043 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3044 
3045 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3046 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3047 
3048 	if (muip != NULL)
3049 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3050 	mac_protect_cancel_timer(mcip);
3051 	mac_protect_flush_dynamic(mcip);
3052 
3053 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
3054 	/*
3055 	 * Disable fastpath if this is a VNIC or a VLAN.
3056 	 */
3057 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
3058 		mac_fastpath_enable((mac_handle_t)mip);
3059 	mac_stop((mac_handle_t)mip);
3060 }
3061 
3062 /*
3063  * Remove a MAC address which was previously added by mac_unicast_add().
3064  */
3065 int
mac_unicast_remove(mac_client_handle_t mch,mac_unicast_handle_t mah)3066 mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
3067 {
3068 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3069 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
3070 	mac_unicast_impl_t *pre;
3071 	mac_impl_t *mip = mcip->mci_mip;
3072 	flow_entry_t		*flent;
3073 	uint16_t mui_vid;
3074 
3075 	i_mac_perim_enter(mip);
3076 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
3077 		/*
3078 		 * Call made by the upper MAC client of a VNIC.
3079 		 * There's nothing much to do, the unicast address will
3080 		 * be removed by the VNIC driver when the VNIC is deleted,
3081 		 * but let's ensure that all our transmit is done before
3082 		 * the client does a mac_client_stop lest it trigger an
3083 		 * assert in the driver.
3084 		 */
3085 		ASSERT(muip->mui_vid == VLAN_ID_NONE);
3086 
3087 		mac_tx_client_flush(mcip);
3088 
3089 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3090 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3091 			if (mcip->mci_rx_p_fn != NULL) {
3092 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3093 				    mcip->mci_rx_p_arg);
3094 				mcip->mci_rx_p_fn = NULL;
3095 				mcip->mci_rx_p_arg = NULL;
3096 			}
3097 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3098 			i_mac_perim_exit(mip);
3099 			return (0);
3100 		}
3101 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
3102 
3103 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3104 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3105 
3106 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3107 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3108 
3109 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3110 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3111 
3112 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3113 		i_mac_perim_exit(mip);
3114 		return (0);
3115 	}
3116 
3117 	ASSERT(muip != NULL);
3118 
3119 	/*
3120 	 * We are removing a passive client, we haven't setup the datapath
3121 	 * for this yet, so nothing much to do.
3122 	 */
3123 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3124 
3125 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
3126 		ASSERT(mcip->mci_p_unicast_list == muip);
3127 
3128 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3129 
3130 		mcip->mci_p_unicast_list = NULL;
3131 		mcip->mci_rx_p_fn = NULL;
3132 		mcip->mci_rx_p_arg = NULL;
3133 
3134 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3135 
3136 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3137 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3138 
3139 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3140 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3141 
3142 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3143 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3144 
3145 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3146 		i_mac_perim_exit(mip);
3147 		return (0);
3148 	}
3149 
3150 	/*
3151 	 * Remove the VID from the list of client's VIDs.
3152 	 */
3153 	pre = mcip->mci_unicast_list;
3154 	if (muip == pre) {
3155 		mcip->mci_unicast_list = muip->mui_next;
3156 	} else {
3157 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
3158 			pre = pre->mui_next;
3159 		ASSERT(pre->mui_next == muip);
3160 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3161 		pre->mui_next = muip->mui_next;
3162 		rw_exit(&mcip->mci_rw_lock);
3163 	}
3164 
3165 	if (!mac_client_single_rcvr(mcip)) {
3166 		/*
3167 		 * This MAC client is shared by more than one unicast
3168 		 * addresses, so we will just remove the flent
3169 		 * corresponding to the address being removed. We don't invoke
3170 		 * mac_rx_classify_flow_rem() since the additional flow is
3171 		 * not associated with its own separate set of SRS and rings,
3172 		 * and these constructs are still needed for the remaining
3173 		 * flows.
3174 		 */
3175 		flent = mac_client_get_flow(mcip, muip);
3176 		VERIFY3P(flent, !=, NULL);
3177 
3178 		/*
3179 		 * The first one is disappearing, need to make sure
3180 		 * we replace it with another from the list of
3181 		 * shared clients.
3182 		 */
3183 		if (flent == mcip->mci_flent)
3184 			flent = mac_client_swap_mciflent(mcip);
3185 		mac_client_remove_flow_from_list(mcip, flent);
3186 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3187 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3188 
3189 		/*
3190 		 * The multicast groups that were added by the client so
3191 		 * far must be removed from the brodcast domain corresponding
3192 		 * to the VID being removed.
3193 		 */
3194 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
3195 		    (void *)flent, B_FALSE);
3196 
3197 		if (mip->mi_type->mt_brdcst_addr != NULL) {
3198 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3199 			    muip->mui_vid);
3200 		}
3201 
3202 		FLOW_FINAL_REFRELE(flent);
3203 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
3204 
3205 		/*
3206 		 * Enable fastpath if this is a VNIC or a VLAN.
3207 		 */
3208 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
3209 			mac_fastpath_enable((mac_handle_t)mip);
3210 		mac_stop((mac_handle_t)mip);
3211 		i_mac_perim_exit(mip);
3212 		return (0);
3213 	}
3214 
3215 	mui_vid = muip->mui_vid;
3216 	mac_client_datapath_teardown(mch, muip, flent);
3217 
3218 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
3219 	    mui_vid == VLAN_ID_NONE) {
3220 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
3221 	} else {
3222 		i_mac_perim_exit(mip);
3223 		return (0);
3224 	}
3225 
3226 	/*
3227 	 * If we are removing the primary, check if we have a passive primary
3228 	 * client that we need to activate now.
3229 	 */
3230 	mcip = mac_get_passive_primary_client(mip);
3231 	if (mcip != NULL) {
3232 		mac_resource_props_t	*mrp;
3233 		mac_unicast_impl_t	*muip;
3234 
3235 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3236 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3237 
3238 		/*
3239 		 * Apply the property cached in the mac_impl_t to the
3240 		 * primary mac client.
3241 		 */
3242 		mac_get_resources((mac_handle_t)mip, mrp);
3243 		(void) mac_client_set_resources(mch, mrp);
3244 		ASSERT(mcip->mci_p_unicast_list != NULL);
3245 		muip = mcip->mci_p_unicast_list;
3246 		mcip->mci_p_unicast_list = NULL;
3247 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
3248 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
3249 			if (mcip->mci_rx_p_fn != NULL) {
3250 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3251 				    mcip->mci_rx_p_arg);
3252 				mcip->mci_rx_p_fn = NULL;
3253 				mcip->mci_rx_p_arg = NULL;
3254 			}
3255 		} else {
3256 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3257 		}
3258 		kmem_free(mrp, sizeof (*mrp));
3259 	}
3260 	i_mac_perim_exit(mip);
3261 	return (0);
3262 }
3263 
3264 /*
3265  * Multicast add function invoked by MAC clients.
3266  */
3267 int
mac_multicast_add(mac_client_handle_t mch,const uint8_t * addr)3268 mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
3269 {
3270 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3271 	mac_impl_t		*mip = mcip->mci_mip;
3272 	flow_entry_t		*flent = mcip->mci_flent_list;
3273 	flow_entry_t		*prev_fe = NULL;
3274 	uint16_t		vid;
3275 	int			err = 0;
3276 
3277 	/* Verify the address is a valid multicast address */
3278 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
3279 	    mip->mi_pdata)) != 0)
3280 		return (err);
3281 
3282 	i_mac_perim_enter(mip);
3283 	while (flent != NULL) {
3284 		vid = i_mac_flow_vid(flent);
3285 
3286 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
3287 		    MAC_ADDRTYPE_MULTICAST);
3288 		if (err != 0)
3289 			break;
3290 		prev_fe = flent;
3291 		flent = flent->fe_client_next;
3292 	}
3293 
3294 	/*
3295 	 * If we failed adding, then undo all, rather than partial
3296 	 * success.
3297 	 */
3298 	if (flent != NULL && prev_fe != NULL) {
3299 		flent = mcip->mci_flent_list;
3300 		while (flent != prev_fe->fe_client_next) {
3301 			vid = i_mac_flow_vid(flent);
3302 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3303 			flent = flent->fe_client_next;
3304 		}
3305 	}
3306 	i_mac_perim_exit(mip);
3307 	return (err);
3308 }
3309 
3310 /*
3311  * Multicast delete function invoked by MAC clients.
3312  */
3313 void
mac_multicast_remove(mac_client_handle_t mch,const uint8_t * addr)3314 mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
3315 {
3316 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3317 	mac_impl_t		*mip = mcip->mci_mip;
3318 	flow_entry_t		*flent;
3319 	uint16_t		vid;
3320 
3321 	i_mac_perim_enter(mip);
3322 	for (flent = mcip->mci_flent_list; flent != NULL;
3323 	    flent = flent->fe_client_next) {
3324 		vid = i_mac_flow_vid(flent);
3325 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3326 	}
3327 	i_mac_perim_exit(mip);
3328 }
3329 
3330 /*
3331  * When a MAC client desires to capture packets on an interface,
3332  * it registers a promiscuous call back with mac_promisc_add().
3333  * There are three types of promiscuous callbacks:
3334  *
3335  * * MAC_CLIENT_PROMISC_ALL
3336  *   Captures all packets sent and received by the MAC client,
3337  *   the physical interface, as well as all other MAC clients
3338  *   defined on top of the same MAC.
3339  *
3340  * * MAC_CLIENT_PROMISC_FILTERED
3341  *   Captures all packets sent and received by the MAC client,
3342  *   plus all multicast traffic sent and received by the phyisical
3343  *   interface and the other MAC clients.
3344  *
3345  * * MAC_CLIENT_PROMISC_MULTI
3346  *   Captures all broadcast and multicast packets sent and
3347  *   received by the MAC clients as well as the physical interface.
3348  *
3349  * In all cases, the underlying MAC is put in promiscuous mode.
3350  */
3351 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)3352 mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
3353     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
3354 {
3355 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3356 	mac_impl_t *mip = mcip->mci_mip;
3357 	mac_promisc_impl_t *mpip;
3358 	mac_cb_info_t	*mcbi;
3359 	int rc;
3360 
3361 	boolean_t no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
3362 	boolean_t rx_only = ((flags & MAC_PROMISC_FLAGS_RX_ONLY) != 0);
3363 	boolean_t tx_only = ((flags & MAC_PROMISC_FLAGS_TX_ONLY) != 0);
3364 
3365 	if (no_tx_loop && tx_only)
3366 		return (EINVAL);
3367 	if (rx_only && tx_only)
3368 		return (EINVAL);
3369 
3370 	i_mac_perim_enter(mip);
3371 
3372 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
3373 		i_mac_perim_exit(mip);
3374 		return (rc);
3375 	}
3376 
3377 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
3378 	    type == MAC_CLIENT_PROMISC_ALL &&
3379 	    (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
3380 		/*
3381 		 * The function is being invoked by the upper MAC client
3382 		 * of a VNIC. The VNIC should only see the traffic
3383 		 * it is entitled to.
3384 		 */
3385 		type = MAC_CLIENT_PROMISC_FILTERED;
3386 	}
3387 
3388 
3389 	/*
3390 	 * Turn on promiscuous mode for the underlying NIC.
3391 	 * This is needed even for filtered callbacks which
3392 	 * expect to receive all multicast traffic on the wire.
3393 	 *
3394 	 * Physical promiscuous mode should not be turned on if
3395 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
3396 	 */
3397 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
3398 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
3399 			mac_stop((mac_handle_t)mip);
3400 			i_mac_perim_exit(mip);
3401 			return (rc);
3402 		}
3403 	}
3404 
3405 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
3406 
3407 	mpip->mpi_type = type;
3408 	mpip->mpi_fn = fn;
3409 	mpip->mpi_arg = arg;
3410 	mpip->mpi_mcip = mcip;
3411 	mpip->mpi_no_tx_loop = no_tx_loop;
3412 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
3413 	mpip->mpi_strip_vlan_tag =
3414 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
3415 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
3416 	mpip->mpi_rx_only = rx_only;
3417 	mpip->mpi_tx_only = tx_only;
3418 
3419 	mcbi = &mip->mi_promisc_cb_info;
3420 	mutex_enter(mcbi->mcbi_lockp);
3421 
3422 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
3423 	    &mpip->mpi_mci_link);
3424 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
3425 	    &mpip->mpi_mi_link);
3426 
3427 	mutex_exit(mcbi->mcbi_lockp);
3428 
3429 	*mphp = (mac_promisc_handle_t)mpip;
3430 
3431 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3432 		mac_impl_t *umip = mcip->mci_upper_mip;
3433 
3434 		ASSERT(umip != NULL);
3435 		mac_vnic_secondary_update(umip);
3436 	}
3437 
3438 	i_mac_perim_exit(mip);
3439 
3440 	return (0);
3441 }
3442 
3443 /*
3444  * Remove a multicast address previously aded through mac_promisc_add().
3445  */
3446 void
mac_promisc_remove(mac_promisc_handle_t mph)3447 mac_promisc_remove(mac_promisc_handle_t mph)
3448 {
3449 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
3450 	mac_client_impl_t *mcip = mpip->mpi_mcip;
3451 	mac_impl_t *mip = mcip->mci_mip;
3452 	mac_cb_info_t *mcbi;
3453 	int rv;
3454 
3455 	i_mac_perim_enter(mip);
3456 
3457 	/*
3458 	 * Even if the device can't be reset into normal mode, we still
3459 	 * need to clear the client promisc callbacks. The client may want
3460 	 * to close the mac end point and we can't have stale callbacks.
3461 	 */
3462 	if (!(mpip->mpi_no_phys)) {
3463 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
3464 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
3465 			    " mode because of error 0x%x", mip->mi_name, rv);
3466 		}
3467 	}
3468 	mcbi = &mip->mi_promisc_cb_info;
3469 	mutex_enter(mcbi->mcbi_lockp);
3470 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
3471 	    &mpip->mpi_mi_link)) {
3472 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
3473 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
3474 		kmem_cache_free(mac_promisc_impl_cache, mpip);
3475 	} else {
3476 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
3477 	}
3478 
3479 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3480 		mac_impl_t *umip = mcip->mci_upper_mip;
3481 
3482 		ASSERT(umip != NULL);
3483 		mac_vnic_secondary_update(umip);
3484 	}
3485 
3486 	mutex_exit(mcbi->mcbi_lockp);
3487 	mac_stop((mac_handle_t)mip);
3488 
3489 	i_mac_perim_exit(mip);
3490 }
3491 
3492 /*
3493  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
3494  * that a control operation wants to quiesce the Tx data flow in which case
3495  * we return an error. Holding any of the per cpu locks ensures that the
3496  * mci_tx_flag won't change.
3497  *
3498  * 'CPU' must be accessed just once and used to compute the index into the
3499  * percpu array, and that index must be used for the entire duration of the
3500  * packet send operation. Note that the thread may be preempted and run on
3501  * another cpu any time and so we can't use 'CPU' more than once for the
3502  * operation.
3503  */
3504 #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
3505 {									\
3506 	(error) = 0;							\
3507 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
3508 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3509 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
3510 		(mytx)->pcpu_tx_refcnt++;				\
3511 	} else {							\
3512 		(error) = -1;						\
3513 	}								\
3514 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
3515 }
3516 
3517 /*
3518  * Release the reference. If needed, signal any control operation waiting
3519  * for Tx quiescence. The wait and signal are always done using the
3520  * mci_tx_pcpu[0]'s lock
3521  */
3522 #define	MAC_TX_RELE(mcip, mytx) {					\
3523 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3524 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
3525 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
3526 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3527 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3528 		cv_signal(&(mcip)->mci_tx_cv);				\
3529 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3530 	} else {							\
3531 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3532 	}								\
3533 }
3534 
3535 /*
3536  * Send function invoked by MAC clients.
3537  */
3538 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)3539 mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
3540     uint16_t flag, mblk_t **ret_mp)
3541 {
3542 	mac_tx_cookie_t		cookie = 0;
3543 	int			error;
3544 	mac_tx_percpu_t		*mytx;
3545 	mac_soft_ring_set_t	*srs;
3546 	flow_entry_t		*flent;
3547 	boolean_t		is_subflow = B_FALSE;
3548 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3549 	mac_impl_t		*mip = mcip->mci_mip;
3550 	mac_srs_tx_t		*srs_tx;
3551 
3552 	/*
3553 	 * Check whether the active Tx threads count is bumped already.
3554 	 */
3555 	if (!(flag & MAC_TX_NO_HOLD)) {
3556 		MAC_TX_TRY_HOLD(mcip, mytx, error);
3557 		if (error != 0) {
3558 			freemsgchain(mp_chain);
3559 			return (0);
3560 		}
3561 	}
3562 
3563 	/*
3564 	 * If mac protection is enabled, only the permissible packets will be
3565 	 * returned by mac_protect_check().
3566 	 */
3567 	if ((mcip->mci_flent->
3568 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
3569 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
3570 		goto done;
3571 
3572 	if (mcip->mci_subflow_tab != NULL &&
3573 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
3574 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
3575 	    FLOW_OUTBOUND, &flent) == 0) {
3576 		/*
3577 		 * The main assumption here is that if in the event
3578 		 * we get a chain, all the packets will be classified
3579 		 * to the same Flow/SRS. If this changes for any
3580 		 * reason, the following logic should change as well.
3581 		 * I suppose the fanout_hint also assumes this .
3582 		 */
3583 		ASSERT(flent != NULL);
3584 		is_subflow = B_TRUE;
3585 	} else {
3586 		flent = mcip->mci_flent;
3587 	}
3588 
3589 	srs = flent->fe_tx_srs;
3590 	/*
3591 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
3592 	 * against an interface that is not capable of sending. A rewrite
3593 	 * of the mac datapath is required to remove this limitation.
3594 	 */
3595 	if (srs == NULL) {
3596 		freemsgchain(mp_chain);
3597 		goto done;
3598 	}
3599 
3600 	srs_tx = &srs->srs_tx;
3601 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
3602 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
3603 	    mip->mi_nactiveclients == 1 &&
3604 	    mp_chain->b_next == NULL &&
3605 	    (DB_CKSUMFLAGS(mp_chain) & HW_LSO) == 0) {
3606 		uint64_t	obytes;
3607 
3608 		/*
3609 		 * Since dls always opens the underlying MAC, nclients equals
3610 		 * to 1 means that the only active client is dls itself acting
3611 		 * as a primary client of the MAC instance. Since dls will not
3612 		 * send tagged packets in that case, and dls is trusted to send
3613 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
3614 		 * check is required only if nclients is greater than 1.
3615 		 */
3616 		if (mip->mi_nclients > 1) {
3617 			if (MAC_VID_CHECK_NEEDED(mcip)) {
3618 				int	err = 0;
3619 
3620 				MAC_VID_CHECK(mcip, mp_chain, err);
3621 				if (err != 0) {
3622 					freemsg(mp_chain);
3623 					mcip->mci_misc_stat.mms_txerrors++;
3624 					goto done;
3625 				}
3626 			}
3627 			if (MAC_TAG_NEEDED(mcip)) {
3628 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
3629 				    mac_client_vid(mch));
3630 				if (mp_chain == NULL) {
3631 					mcip->mci_misc_stat.mms_txerrors++;
3632 					goto done;
3633 				}
3634 			}
3635 		}
3636 
3637 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
3638 		    msgdsize(mp_chain));
3639 
3640 		mp_chain = mac_provider_tx(mip,
3641 		    (mac_ring_handle_t)srs_tx->st_arg2, mp_chain, mcip);
3642 
3643 		if (mp_chain == NULL) {
3644 			cookie = 0;
3645 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
3646 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
3647 		} else {
3648 			mutex_enter(&srs->srs_lock);
3649 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
3650 			    flag, ret_mp);
3651 			mutex_exit(&srs->srs_lock);
3652 		}
3653 	} else {
3654 		mblk_t *mp = mp_chain;
3655 		mblk_t *new_head = NULL;
3656 		mblk_t *new_tail = NULL;
3657 
3658 		/*
3659 		 * There are occasions where the packets arriving here
3660 		 * may request hardware offloads that are not
3661 		 * available from the underlying MAC provider. This
3662 		 * currently only happens when a packet is sent across
3663 		 * the MAC-loopback path of one MAC and then forwarded
3664 		 * (via IP) to another MAC that lacks one or more of
3665 		 * the hardware offloads provided by the first one.
3666 		 * However, in the future, we may choose to pretend
3667 		 * all MAC providers support all offloads, performing
3668 		 * emulation on Tx as needed.
3669 		 *
3670 		 * We iterate each mblk in-turn, emulating hardware
3671 		 * offloads as required. From this process, we create
3672 		 * a new chain. The new chain may be the same as the
3673 		 * original chain (no hardware emulation needed), a
3674 		 * collection of new mblks (hardware emulation
3675 		 * needed), or a mix. At this point, the chain is safe
3676 		 * for consumption by the underlying MAC provider and
3677 		 * is passed down to the SRS.
3678 		 */
3679 		while (mp != NULL) {
3680 			mblk_t *next = mp->b_next;
3681 			mblk_t *tail = NULL;
3682 			const uint16_t needed =
3683 			    (DB_CKSUMFLAGS(mp) ^ mip->mi_tx_cksum_flags) &
3684 			    DB_CKSUMFLAGS(mp);
3685 
3686 			mp->b_next = NULL;
3687 
3688 			if ((needed & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) {
3689 				mac_emul_t emul = 0;
3690 
3691 				if (needed & HCK_IPV4_HDRCKSUM)
3692 					emul |= MAC_IPCKSUM_EMUL;
3693 				if (needed & (HCK_PARTIALCKSUM | HCK_FULLCKSUM))
3694 					emul |= MAC_HWCKSUM_EMUL;
3695 				if (needed & HW_LSO)
3696 					emul = MAC_LSO_EMUL;
3697 
3698 				mac_hw_emul(&mp, &tail, NULL, emul);
3699 
3700 				if (mp == NULL) {
3701 					mp = next;
3702 					continue;
3703 				}
3704 			}
3705 
3706 			if (new_head == NULL) {
3707 				new_head = mp;
3708 			} else {
3709 				new_tail->b_next = mp;
3710 			}
3711 
3712 			new_tail = (tail == NULL) ? mp : tail;
3713 			mp = next;
3714 		}
3715 
3716 		if (new_head == NULL) {
3717 			cookie = 0;
3718 			goto done;
3719 		}
3720 
3721 		cookie = srs_tx->st_func(srs, new_head, hint, flag, ret_mp);
3722 	}
3723 
3724 done:
3725 	if (is_subflow)
3726 		FLOW_REFRELE(flent);
3727 
3728 	if (!(flag & MAC_TX_NO_HOLD))
3729 		MAC_TX_RELE(mcip, mytx);
3730 
3731 	return (cookie);
3732 }
3733 
3734 /*
3735  * mac_tx_is_blocked
3736  *
3737  * Given a cookie, it returns if the ring identified by the cookie is
3738  * flow-controlled or not. If NULL is passed in place of a cookie,
3739  * then it finds out if any of the underlying rings belonging to the
3740  * SRS is flow controlled or not and returns that status.
3741  */
3742 /* ARGSUSED */
3743 boolean_t
mac_tx_is_flow_blocked(mac_client_handle_t mch,mac_tx_cookie_t cookie)3744 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
3745 {
3746 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3747 	mac_soft_ring_set_t *mac_srs;
3748 	mac_soft_ring_t *sringp;
3749 	boolean_t blocked = B_FALSE;
3750 	mac_tx_percpu_t *mytx;
3751 	int err;
3752 	int i;
3753 
3754 	/*
3755 	 * Bump the reference count so that mac_srs won't be deleted.
3756 	 * If the client is currently quiesced and we failed to bump
3757 	 * the reference, return B_TRUE so that flow control stays
3758 	 * as enabled.
3759 	 *
3760 	 * Flow control will then be disabled once the client is no
3761 	 * longer quiesced.
3762 	 */
3763 	MAC_TX_TRY_HOLD(mcip, mytx, err);
3764 	if (err != 0)
3765 		return (B_TRUE);
3766 
3767 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
3768 		MAC_TX_RELE(mcip, mytx);
3769 		return (B_FALSE);
3770 	}
3771 
3772 	mutex_enter(&mac_srs->srs_lock);
3773 	/*
3774 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
3775 	 * softring (s_ring_state) will have the HIWAT set. This is
3776 	 * the multiple Tx ring flow control case. For all other
3777 	 * case, SRS (srs_state) will store the condition.
3778 	 */
3779 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3780 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
3781 		if (cookie != 0) {
3782 			sringp = (mac_soft_ring_t *)cookie;
3783 			mutex_enter(&sringp->s_ring_lock);
3784 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
3785 				blocked = B_TRUE;
3786 			mutex_exit(&sringp->s_ring_lock);
3787 		} else {
3788 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3789 				sringp = mac_srs->srs_tx_soft_rings[i];
3790 				mutex_enter(&sringp->s_ring_lock);
3791 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
3792 					blocked = B_TRUE;
3793 					mutex_exit(&sringp->s_ring_lock);
3794 					break;
3795 				}
3796 				mutex_exit(&sringp->s_ring_lock);
3797 			}
3798 		}
3799 	} else {
3800 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
3801 	}
3802 	mutex_exit(&mac_srs->srs_lock);
3803 	MAC_TX_RELE(mcip, mytx);
3804 	return (blocked);
3805 }
3806 
3807 /*
3808  * Check if the MAC client is the primary MAC client.
3809  */
3810 boolean_t
mac_is_primary_client(mac_client_impl_t * mcip)3811 mac_is_primary_client(mac_client_impl_t *mcip)
3812 {
3813 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
3814 }
3815 
3816 void
mac_ioctl(mac_handle_t mh,queue_t * wq,mblk_t * bp)3817 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
3818 {
3819 	mac_impl_t	*mip = (mac_impl_t *)mh;
3820 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3821 
3822 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3823 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3824 		/*
3825 		 * If ndd props were registered, call them.
3826 		 * Note that ndd ioctls are Obsolete
3827 		 */
3828 		mac_ndd_ioctl(mip, wq, bp);
3829 		return;
3830 	}
3831 
3832 	/*
3833 	 * Call the driver to handle the ioctl.  The driver may not support
3834 	 * any ioctls, in which case we reply with a NAK on its behalf.
3835 	 */
3836 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3837 		mip->mi_ioctl(mip->mi_driver, wq, bp);
3838 	else
3839 		miocnak(wq, bp, 0, EINVAL);
3840 }
3841 
3842 /*
3843  * Return the link state of the specified MAC instance.
3844  */
3845 link_state_t
mac_link_get(mac_handle_t mh)3846 mac_link_get(mac_handle_t mh)
3847 {
3848 	return (((mac_impl_t *)mh)->mi_linkstate);
3849 }
3850 
3851 /*
3852  * Add a mac client specified notification callback. Please see the comments
3853  * above mac_callback_add() for general information about mac callback
3854  * addition/deletion in the presence of mac callback list walkers
3855  */
3856 mac_notify_handle_t
mac_notify_add(mac_handle_t mh,mac_notify_t notify_fn,void * arg)3857 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3858 {
3859 	mac_impl_t		*mip = (mac_impl_t *)mh;
3860 	mac_notify_cb_t		*mncb;
3861 	mac_cb_info_t		*mcbi;
3862 
3863 	/*
3864 	 * Allocate a notify callback structure, fill in the details and
3865 	 * use the mac callback list manipulation functions to chain into
3866 	 * the list of callbacks.
3867 	 */
3868 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3869 	mncb->mncb_fn = notify_fn;
3870 	mncb->mncb_arg = arg;
3871 	mncb->mncb_mip = mip;
3872 	mncb->mncb_link.mcb_objp = mncb;
3873 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3874 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3875 
3876 	mcbi = &mip->mi_notify_cb_info;
3877 
3878 	i_mac_perim_enter(mip);
3879 	mutex_enter(mcbi->mcbi_lockp);
3880 
3881 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3882 	    &mncb->mncb_link);
3883 
3884 	mutex_exit(mcbi->mcbi_lockp);
3885 	i_mac_perim_exit(mip);
3886 	return ((mac_notify_handle_t)mncb);
3887 }
3888 
3889 void
mac_notify_remove_wait(mac_handle_t mh)3890 mac_notify_remove_wait(mac_handle_t mh)
3891 {
3892 	mac_impl_t	*mip = (mac_impl_t *)mh;
3893 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3894 
3895 	mutex_enter(mcbi->mcbi_lockp);
3896 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3897 	mutex_exit(mcbi->mcbi_lockp);
3898 }
3899 
3900 /*
3901  * Remove a mac client specified notification callback
3902  */
3903 int
mac_notify_remove(mac_notify_handle_t mnh,boolean_t wait)3904 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3905 {
3906 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3907 	mac_impl_t	*mip = mncb->mncb_mip;
3908 	mac_cb_info_t	*mcbi;
3909 	int		err = 0;
3910 
3911 	mcbi = &mip->mi_notify_cb_info;
3912 
3913 	i_mac_perim_enter(mip);
3914 	mutex_enter(mcbi->mcbi_lockp);
3915 
3916 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3917 	/*
3918 	 * If there aren't any list walkers, the remove would succeed
3919 	 * inline, else we wait for the deferred remove to complete
3920 	 */
3921 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3922 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3923 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3924 	} else {
3925 		err = EBUSY;
3926 	}
3927 
3928 	mutex_exit(mcbi->mcbi_lockp);
3929 	i_mac_perim_exit(mip);
3930 
3931 	/*
3932 	 * If we failed to remove the notification callback and "wait" is set
3933 	 * to be B_TRUE, wait for the callback to finish after we exit the
3934 	 * mac perimeter.
3935 	 */
3936 	if (err != 0 && wait) {
3937 		mac_notify_remove_wait((mac_handle_t)mip);
3938 		return (0);
3939 	}
3940 
3941 	return (err);
3942 }
3943 
3944 /*
3945  * Associate resource management callbacks with the specified MAC
3946  * clients.
3947  */
3948 void
mac_resource_set(mac_client_handle_t mch,mac_resource_cb_t * rcbs,boolean_t is_v6)3949 mac_resource_set(mac_client_handle_t mch, mac_resource_cb_t *rcbs,
3950     boolean_t is_v6)
3951 {
3952 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3953 
3954 	if (is_v6) {
3955 		mcip->mci_rcb6 = *rcbs;
3956 	} else {
3957 		mcip->mci_rcb4 = *rcbs;
3958 	}
3959 }
3960 
3961 void
mac_resource_clear(mac_client_handle_t mch,boolean_t is_v6)3962 mac_resource_clear(mac_client_handle_t mch, boolean_t is_v6)
3963 {
3964 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3965 
3966 	if (is_v6) {
3967 		bzero(&mcip->mci_rcb6, sizeof (mcip->mci_rcb6));
3968 	} else {
3969 		bzero(&mcip->mci_rcb4, sizeof (mcip->mci_rcb4));
3970 	}
3971 }
3972 
3973 /*
3974  * Sets up the client resources and enable the polling interface over all the
3975  * SRS's and the soft rings of the client
3976  */
3977 void
mac_client_poll_enable(mac_client_handle_t mch,boolean_t is_v6)3978 mac_client_poll_enable(mac_client_handle_t mch, boolean_t is_v6)
3979 {
3980 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3981 	mac_soft_ring_set_t	*mac_srs;
3982 	flow_entry_t		*flent;
3983 	int			i;
3984 
3985 	flent = mcip->mci_flent;
3986 	ASSERT(flent != NULL);
3987 
3988 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3989 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3990 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3991 		ASSERT(mac_srs->srs_mcip == mcip);
3992 		mac_srs_client_poll_enable(mcip, mac_srs, is_v6);
3993 	}
3994 }
3995 
3996 /*
3997  * Tears down the client resources and disable the polling interface over all
3998  * the SRS's and the soft rings of the client
3999  */
4000 void
mac_client_poll_disable(mac_client_handle_t mch,boolean_t is_v6)4001 mac_client_poll_disable(mac_client_handle_t mch, boolean_t is_v6)
4002 {
4003 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4004 	mac_soft_ring_set_t	*mac_srs;
4005 	flow_entry_t		*flent;
4006 	int			i;
4007 
4008 	flent = mcip->mci_flent;
4009 	ASSERT(flent != NULL);
4010 
4011 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
4012 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
4013 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
4014 		ASSERT(mac_srs->srs_mcip == mcip);
4015 		mac_srs_client_poll_disable(mcip, mac_srs, is_v6);
4016 	}
4017 }
4018 
4019 /*
4020  * Associate the CPUs specified by the given property with a MAC client.
4021  */
4022 int
mac_cpu_set(mac_client_handle_t mch,mac_resource_props_t * mrp)4023 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
4024 {
4025 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4026 	mac_impl_t *mip = mcip->mci_mip;
4027 	int err = 0;
4028 
4029 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4030 
4031 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
4032 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
4033 		return (err);
4034 	}
4035 	if (MCIP_DATAPATH_SETUP(mcip))
4036 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
4037 
4038 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
4039 	return (0);
4040 }
4041 
4042 /*
4043  * Apply the specified properties to the specified MAC client.
4044  */
4045 int
mac_client_set_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4046 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4047 {
4048 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4049 	mac_impl_t *mip = mcip->mci_mip;
4050 	int err = 0;
4051 
4052 	i_mac_perim_enter(mip);
4053 
4054 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
4055 		err = mac_resource_ctl_set(mch, mrp);
4056 		if (err != 0)
4057 			goto done;
4058 	}
4059 
4060 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
4061 		err = mac_cpu_set(mch, mrp);
4062 		if (err != 0)
4063 			goto done;
4064 	}
4065 
4066 	if (mrp->mrp_mask & MRP_PROTECT) {
4067 		err = mac_protect_set(mch, mrp);
4068 		if (err != 0)
4069 			goto done;
4070 	}
4071 
4072 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
4073 		err = mac_resource_ctl_set(mch, mrp);
4074 
4075 done:
4076 	i_mac_perim_exit(mip);
4077 	return (err);
4078 }
4079 
4080 /*
4081  * Return the properties currently associated with the specified MAC client.
4082  */
4083 void
mac_client_get_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4084 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4085 {
4086 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4087 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4088 
4089 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4090 }
4091 
4092 /*
4093  * Return the effective properties currently associated with the specified
4094  * MAC client.
4095  */
4096 void
mac_client_get_effective_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4097 mac_client_get_effective_resources(mac_client_handle_t mch,
4098     mac_resource_props_t *mrp)
4099 {
4100 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4101 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
4102 
4103 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4104 }
4105 
4106 /*
4107  * Pass a copy of the specified packet to the promiscuous callbacks
4108  * of the specified MAC.
4109  *
4110  * If sender is NULL, the function is being invoked for a packet chain
4111  * received from the wire. If sender is non-NULL, it points to
4112  * the MAC client from which the packet is being sent.
4113  *
4114  * The packets are distributed to the promiscuous callbacks as follows:
4115  *
4116  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4117  * - all broadcast and multicast packets are sent to the
4118  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4119  *
4120  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4121  * after classification by mac_rx_deliver().
4122  */
4123 static void
mac_promisc_dispatch_one(mac_promisc_impl_t * mpip,mblk_t * mp,boolean_t loopback,boolean_t local)4124 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4125     boolean_t loopback, boolean_t local)
4126 {
4127 	mblk_t *mp_next;
4128 
4129 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4130 		mblk_t *mp_copy;
4131 
4132 		mp_copy = copymsg(mp);
4133 		if (mp_copy == NULL)
4134 			return;
4135 
4136 		if (mpip->mpi_strip_vlan_tag) {
4137 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
4138 			if (mp_copy == NULL)
4139 				return;
4140 		}
4141 
4142 		/*
4143 		 * There is code upstack that can't deal with message
4144 		 * chains.
4145 		 */
4146 		for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
4147 			mp_next = tmp->b_next;
4148 			tmp->b_next = NULL;
4149 			mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
4150 		}
4151 
4152 		return;
4153 	}
4154 
4155 	mp_next = mp->b_next;
4156 	mp->b_next = NULL;
4157 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
4158 	mp->b_next = mp_next;
4159 }
4160 
4161 /*
4162  * Return the VID of a packet. Zero if the packet is not tagged.
4163  */
4164 static uint16_t
mac_ether_vid(mblk_t * mp)4165 mac_ether_vid(mblk_t *mp)
4166 {
4167 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4168 
4169 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4170 		struct ether_vlan_header *t_evhp =
4171 		    (struct ether_vlan_header *)mp->b_rptr;
4172 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4173 	}
4174 
4175 	return (0);
4176 }
4177 
4178 /*
4179  * Return whether the specified packet contains a multicast or broadcast
4180  * destination MAC address.
4181  */
4182 static boolean_t
mac_is_mcast(mac_impl_t * mip,mblk_t * mp)4183 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4184 {
4185 	mac_header_info_t hdr_info;
4186 
4187 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4188 		return (B_FALSE);
4189 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4190 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4191 }
4192 
4193 /*
4194  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4195  * "sender" points to the sender MAC client for outbound packets, and
4196  * is set to NULL for inbound packets.
4197  */
4198 void
mac_promisc_dispatch(mac_impl_t * mip,mblk_t * mp_chain,mac_client_impl_t * sender,boolean_t local)4199 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4200     mac_client_impl_t *sender, boolean_t local)
4201 {
4202 	mac_promisc_impl_t *mpip;
4203 	mac_cb_t *mcb;
4204 	mblk_t *mp;
4205 	boolean_t is_mcast, is_sender;
4206 
4207 	MAC_PROMISC_WALKER_INC(mip);
4208 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4209 		is_mcast = mac_is_mcast(mip, mp);
4210 		/* send packet to interested callbacks */
4211 		for (mcb = mip->mi_promisc_list; mcb != NULL;
4212 		    mcb = mcb->mcb_nextp) {
4213 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4214 			is_sender = (mpip->mpi_mcip == sender);
4215 
4216 			if (sender != NULL && mpip->mpi_rx_only)
4217 				/*
4218 				 * This client doesn't want outbound packets.
4219 				 */
4220 				continue;
4221 
4222 			if (sender == NULL && mpip->mpi_tx_only)
4223 				/*
4224 				 * This client doesn't want inbound packets.
4225 				 */
4226 				continue;
4227 
4228 			if (is_sender && mpip->mpi_no_tx_loop)
4229 				/*
4230 				 * The sender doesn't want to receive
4231 				 * copies of the packets it sends.
4232 				 */
4233 				continue;
4234 
4235 			/* this client doesn't need any packets (bridge) */
4236 			if (mpip->mpi_fn == NULL)
4237 				continue;
4238 
4239 			/*
4240 			 * For an ethernet MAC, don't displatch a multicast
4241 			 * packet to a non-PROMISC_ALL callbacks unless the VID
4242 			 * of the packet matches the VID of the client.
4243 			 */
4244 			if (is_mcast &&
4245 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4246 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
4247 			    mac_ether_vid(mp)))
4248 				continue;
4249 
4250 			if (is_sender ||
4251 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4252 			    is_mcast) {
4253 				mac_promisc_dispatch_one(mpip, mp, is_sender,
4254 				    local);
4255 			}
4256 		}
4257 	}
4258 	MAC_PROMISC_WALKER_DCR(mip);
4259 }
4260 
4261 void
mac_promisc_client_dispatch(mac_client_impl_t * mcip,mblk_t * mp_chain)4262 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4263 {
4264 	mac_impl_t		*mip = mcip->mci_mip;
4265 	mac_promisc_impl_t	*mpip;
4266 	boolean_t		is_mcast;
4267 	mblk_t			*mp;
4268 	mac_cb_t		*mcb;
4269 
4270 	/*
4271 	 * The unicast packets for the MAC client still
4272 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4273 	 * promiscuous callbacks. The broadcast and multicast
4274 	 * packets were delivered from mac_rx().
4275 	 */
4276 	MAC_PROMISC_WALKER_INC(mip);
4277 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4278 		is_mcast = mac_is_mcast(mip, mp);
4279 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
4280 		    mcb = mcb->mcb_nextp) {
4281 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4282 			if (!mpip->mpi_tx_only &&
4283 			    mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4284 			    !is_mcast) {
4285 				mac_promisc_dispatch_one(mpip, mp, B_FALSE,
4286 				    B_FALSE);
4287 			}
4288 		}
4289 	}
4290 	MAC_PROMISC_WALKER_DCR(mip);
4291 }
4292 
4293 /*
4294  * Return the margin value currently assigned to the specified MAC instance.
4295  */
4296 void
mac_margin_get(mac_handle_t mh,uint32_t * marginp)4297 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4298 {
4299 	mac_impl_t *mip = (mac_impl_t *)mh;
4300 
4301 	rw_enter(&(mip->mi_rw_lock), RW_READER);
4302 	*marginp = mip->mi_margin;
4303 	rw_exit(&(mip->mi_rw_lock));
4304 }
4305 
4306 /*
4307  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4308  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4309  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4310  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4311  * cannot disappear while we are accessing it.
4312  */
4313 typedef struct i_mac_info_state_s {
4314 	const char	*mi_name;
4315 	mac_info_t	*mi_infop;
4316 } i_mac_info_state_t;
4317 
4318 /*ARGSUSED*/
4319 static uint_t
i_mac_info_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)4320 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4321 {
4322 	i_mac_info_state_t *statep = arg;
4323 	mac_impl_t *mip = (mac_impl_t *)val;
4324 
4325 	if (mip->mi_state_flags & MIS_DISABLED)
4326 		return (MH_WALK_CONTINUE);
4327 
4328 	if (strcmp(statep->mi_name,
4329 	    ddi_driver_name(mip->mi_dip)) != 0)
4330 		return (MH_WALK_CONTINUE);
4331 
4332 	statep->mi_infop = &mip->mi_info;
4333 	return (MH_WALK_TERMINATE);
4334 }
4335 
4336 boolean_t
mac_info_get(const char * name,mac_info_t * minfop)4337 mac_info_get(const char *name, mac_info_t *minfop)
4338 {
4339 	i_mac_info_state_t state;
4340 
4341 	rw_enter(&i_mac_impl_lock, RW_READER);
4342 	state.mi_name = name;
4343 	state.mi_infop = NULL;
4344 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4345 	if (state.mi_infop == NULL) {
4346 		rw_exit(&i_mac_impl_lock);
4347 		return (B_FALSE);
4348 	}
4349 	*minfop = *state.mi_infop;
4350 	rw_exit(&i_mac_impl_lock);
4351 	return (B_TRUE);
4352 }
4353 
4354 /*
4355  * To get the capabilities that MAC layer cares about, such as rings, factory
4356  * mac address, vnic or not, it should directly invoke this function.  If the
4357  * link is part of a bridge, then the only "capability" it has is the inability
4358  * to do zero copy.
4359  */
4360 boolean_t
i_mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4361 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4362 {
4363 	mac_impl_t *mip = (mac_impl_t *)mh;
4364 
4365 	if (mip->mi_bridge_link != NULL) {
4366 		return (cap == MAC_CAPAB_NO_ZCOPY);
4367 	} else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
4368 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
4369 	} else {
4370 		return (B_FALSE);
4371 	}
4372 }
4373 
4374 /*
4375  * Capability query function. If number of active mac clients is greater than
4376  * 1, only limited capabilities can be advertised to the caller no matter the
4377  * driver has certain capability or not. Else, we query the driver to get the
4378  * capability.
4379  */
4380 boolean_t
mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4381 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4382 {
4383 	mac_impl_t *mip = (mac_impl_t *)mh;
4384 
4385 	/*
4386 	 * Some capabilities are restricted when there are more than one active
4387 	 * clients on the MAC resource.  The ones noted below are safe,
4388 	 * independent of that count.
4389 	 */
4390 	if (mip->mi_nactiveclients > 1) {
4391 		switch (cap) {
4392 		case MAC_CAPAB_NO_ZCOPY:
4393 			return (B_TRUE);
4394 		case MAC_CAPAB_LEGACY:
4395 		case MAC_CAPAB_HCKSUM:
4396 		case MAC_CAPAB_LSO:
4397 		case MAC_CAPAB_NO_NATIVEVLAN:
4398 			break;
4399 		default:
4400 			return (B_FALSE);
4401 		}
4402 	}
4403 
4404 	/* else get capab from driver */
4405 	return (i_mac_capab_get(mh, cap, cap_data));
4406 }
4407 
4408 boolean_t
mac_sap_verify(mac_handle_t mh,uint32_t sap,uint32_t * bind_sap)4409 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4410 {
4411 	mac_impl_t *mip = (mac_impl_t *)mh;
4412 
4413 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4414 	    mip->mi_pdata));
4415 }
4416 
4417 mblk_t *
mac_header(mac_handle_t mh,const uint8_t * daddr,uint32_t sap,mblk_t * payload,size_t extra_len)4418 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4419     size_t extra_len)
4420 {
4421 	mac_impl_t	*mip = (mac_impl_t *)mh;
4422 	const uint8_t	*hdr_daddr;
4423 
4424 	/*
4425 	 * If the MAC is point-to-point with a fixed destination address, then
4426 	 * we must always use that destination in the MAC header.
4427 	 */
4428 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
4429 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4430 	    mip->mi_pdata, payload, extra_len));
4431 }
4432 
4433 int
mac_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4434 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4435 {
4436 	mac_impl_t *mip = (mac_impl_t *)mh;
4437 
4438 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4439 	    mhip));
4440 }
4441 
4442 int
mac_vlan_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4443 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4444 {
4445 	mac_impl_t	*mip = (mac_impl_t *)mh;
4446 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
4447 	int		err = 0;
4448 
4449 	/*
4450 	 * Packets should always be at least 16 bit aligned.
4451 	 */
4452 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
4453 
4454 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
4455 		return (err);
4456 
4457 	/*
4458 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
4459 	 * mac_header_info_t as returned by mac_header_info() is
4460 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
4461 	 */
4462 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
4463 		struct ether_vlan_header *evhp;
4464 		uint16_t sap;
4465 		mblk_t *tmp = NULL;
4466 		size_t size;
4467 
4468 		size = sizeof (struct ether_vlan_header);
4469 		if (MBLKL(mp) < size) {
4470 			/*
4471 			 * Pullup the message in order to get the MAC header
4472 			 * infomation. Note that this is a read-only function,
4473 			 * we keep the input packet intact.
4474 			 */
4475 			if ((tmp = msgpullup(mp, size)) == NULL)
4476 				return (EINVAL);
4477 
4478 			mp = tmp;
4479 		}
4480 		evhp = (struct ether_vlan_header *)mp->b_rptr;
4481 		sap = ntohs(evhp->ether_type);
4482 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
4483 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
4484 		mhip->mhi_tci = ntohs(evhp->ether_tci);
4485 		mhip->mhi_istagged = B_TRUE;
4486 		freemsg(tmp);
4487 
4488 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
4489 			return (EINVAL);
4490 	} else {
4491 		mhip->mhi_istagged = B_FALSE;
4492 		mhip->mhi_tci = 0;
4493 	}
4494 
4495 	return (0);
4496 }
4497 
4498 mblk_t *
mac_header_cook(mac_handle_t mh,mblk_t * mp)4499 mac_header_cook(mac_handle_t mh, mblk_t *mp)
4500 {
4501 	mac_impl_t *mip = (mac_impl_t *)mh;
4502 
4503 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4504 		if (DB_REF(mp) > 1) {
4505 			mblk_t *newmp = copymsg(mp);
4506 			if (newmp == NULL)
4507 				return (NULL);
4508 			freemsg(mp);
4509 			mp = newmp;
4510 		}
4511 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4512 		    mip->mi_pdata));
4513 	}
4514 	return (mp);
4515 }
4516 
4517 mblk_t *
mac_header_uncook(mac_handle_t mh,mblk_t * mp)4518 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4519 {
4520 	mac_impl_t *mip = (mac_impl_t *)mh;
4521 
4522 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4523 		if (DB_REF(mp) > 1) {
4524 			mblk_t *newmp = copymsg(mp);
4525 			if (newmp == NULL)
4526 				return (NULL);
4527 			freemsg(mp);
4528 			mp = newmp;
4529 		}
4530 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4531 		    mip->mi_pdata));
4532 	}
4533 	return (mp);
4534 }
4535 
4536 uint_t
mac_addr_len(mac_handle_t mh)4537 mac_addr_len(mac_handle_t mh)
4538 {
4539 	mac_impl_t *mip = (mac_impl_t *)mh;
4540 
4541 	return (mip->mi_type->mt_addr_length);
4542 }
4543 
4544 /* True if a MAC is a VNIC */
4545 boolean_t
mac_is_vnic(mac_handle_t mh)4546 mac_is_vnic(mac_handle_t mh)
4547 {
4548 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
4549 }
4550 
4551 boolean_t
mac_is_overlay(mac_handle_t mh)4552 mac_is_overlay(mac_handle_t mh)
4553 {
4554 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
4555 }
4556 
4557 mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)4558 mac_get_lower_mac_handle(mac_handle_t mh)
4559 {
4560 	mac_impl_t *mip = (mac_impl_t *)mh;
4561 
4562 	ASSERT(mac_is_vnic(mh));
4563 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4564 }
4565 
4566 boolean_t
mac_is_vnic_primary(mac_handle_t mh)4567 mac_is_vnic_primary(mac_handle_t mh)
4568 {
4569 	mac_impl_t *mip = (mac_impl_t *)mh;
4570 
4571 	ASSERT(mac_is_vnic(mh));
4572 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4573 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
4574 }
4575 
4576 void
mac_update_resources(mac_resource_props_t * nmrp,mac_resource_props_t * cmrp,boolean_t is_user_flow)4577 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4578     boolean_t is_user_flow)
4579 {
4580 	if (nmrp != NULL && cmrp != NULL) {
4581 		if (nmrp->mrp_mask & MRP_PRIORITY) {
4582 			if (nmrp->mrp_priority == MPL_RESET) {
4583 				cmrp->mrp_mask &= ~MRP_PRIORITY;
4584 				if (is_user_flow) {
4585 					cmrp->mrp_priority =
4586 					    MPL_SUBFLOW_DEFAULT;
4587 				} else {
4588 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
4589 				}
4590 			} else {
4591 				cmrp->mrp_mask |= MRP_PRIORITY;
4592 				cmrp->mrp_priority = nmrp->mrp_priority;
4593 			}
4594 		}
4595 		if (nmrp->mrp_mask & MRP_MAXBW) {
4596 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4597 				cmrp->mrp_mask &= ~MRP_MAXBW;
4598 				cmrp->mrp_maxbw = 0;
4599 			} else {
4600 				cmrp->mrp_mask |= MRP_MAXBW;
4601 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4602 			}
4603 		}
4604 		if (nmrp->mrp_mask & MRP_CPUS)
4605 			MAC_COPY_CPUS(nmrp, cmrp);
4606 
4607 		if (nmrp->mrp_mask & MRP_POOL) {
4608 			if (strlen(nmrp->mrp_pool) == 0) {
4609 				cmrp->mrp_mask &= ~MRP_POOL;
4610 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4611 			} else {
4612 				cmrp->mrp_mask |= MRP_POOL;
4613 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4614 				    sizeof (cmrp->mrp_pool));
4615 			}
4616 
4617 		}
4618 
4619 		if (nmrp->mrp_mask & MRP_PROTECT)
4620 			mac_protect_update(nmrp, cmrp);
4621 
4622 		/*
4623 		 * Update the rings specified.
4624 		 */
4625 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
4626 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4627 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
4628 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4629 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4630 				cmrp->mrp_nrxrings = 0;
4631 			} else {
4632 				cmrp->mrp_mask |= MRP_RX_RINGS;
4633 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4634 			}
4635 		}
4636 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
4637 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4638 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
4639 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4640 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4641 				cmrp->mrp_ntxrings = 0;
4642 			} else {
4643 				cmrp->mrp_mask |= MRP_TX_RINGS;
4644 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4645 			}
4646 		}
4647 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4648 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4649 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4650 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4651 
4652 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4653 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4654 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4655 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4656 	}
4657 }
4658 
4659 /*
4660  * i_mac_set_resources:
4661  *
4662  * This routine associates properties with the primary MAC client of
4663  * the specified MAC instance.
4664  * - Cache the properties in mac_impl_t
4665  * - Apply the properties to the primary MAC client if exists
4666  */
4667 int
i_mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4668 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4669 {
4670 	mac_impl_t		*mip = (mac_impl_t *)mh;
4671 	mac_client_impl_t	*mcip;
4672 	int			err = 0;
4673 	uint32_t		resmask, newresmask;
4674 	mac_resource_props_t	*tmrp, *umrp;
4675 
4676 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4677 
4678 	err = mac_validate_props(mip, mrp);
4679 	if (err != 0)
4680 		return (err);
4681 
4682 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4683 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4684 	resmask = umrp->mrp_mask;
4685 	mac_update_resources(mrp, umrp, B_FALSE);
4686 	newresmask = umrp->mrp_mask;
4687 
4688 	if (resmask == 0 && newresmask != 0) {
4689 		/*
4690 		 * Bandwidth, priority, cpu or pool link properties configured,
4691 		 * must disable fastpath.
4692 		 */
4693 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4694 			kmem_free(umrp, sizeof (*umrp));
4695 			return (err);
4696 		}
4697 	}
4698 
4699 	/*
4700 	 * Since bind_cpu may be modified by mac_client_set_resources()
4701 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4702 	 * This allows us to cache only user edits in mip.
4703 	 */
4704 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4705 	bcopy(mrp, tmrp, sizeof (*tmrp));
4706 	mcip = mac_primary_client_handle(mip);
4707 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4708 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4709 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4710 	    mrp->mrp_mask & MRP_TX_RINGS)) {
4711 		mac_client_impl_t	*vmcip;
4712 
4713 		/*
4714 		 * If the primary is not up, we need to check if there
4715 		 * are any VLANs on this primary. If there are then
4716 		 * we need to set this property on the VLANs since
4717 		 * VLANs follow the primary they are based on. Just
4718 		 * look for the first VLAN and change its properties,
4719 		 * all the other VLANs should be in the same group.
4720 		 */
4721 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
4722 		    vmcip = vmcip->mci_client_next) {
4723 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4724 			    mac_client_vid((mac_client_handle_t)vmcip) !=
4725 			    VLAN_ID_NONE) {
4726 				break;
4727 			}
4728 		}
4729 		if (vmcip != NULL) {
4730 			mac_resource_props_t	*omrp;
4731 			mac_resource_props_t	*vmrp;
4732 
4733 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4734 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4735 			/*
4736 			 * We dont' call mac_update_resources since we
4737 			 * want to take only the ring properties and
4738 			 * not all the properties that may have changed.
4739 			 */
4740 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
4741 			if (mrp->mrp_mask & MRP_RX_RINGS) {
4742 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4743 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
4744 					if (vmrp->mrp_mask &
4745 					    MRP_RXRINGS_UNSPEC) {
4746 						vmrp->mrp_mask &=
4747 						    ~MRP_RXRINGS_UNSPEC;
4748 					}
4749 					vmrp->mrp_nrxrings = 0;
4750 				} else {
4751 					vmrp->mrp_mask |= MRP_RX_RINGS;
4752 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4753 				}
4754 			}
4755 			if (mrp->mrp_mask & MRP_TX_RINGS) {
4756 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4757 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
4758 					if (vmrp->mrp_mask &
4759 					    MRP_TXRINGS_UNSPEC) {
4760 						vmrp->mrp_mask &=
4761 						    ~MRP_TXRINGS_UNSPEC;
4762 					}
4763 					vmrp->mrp_ntxrings = 0;
4764 				} else {
4765 					vmrp->mrp_mask |= MRP_TX_RINGS;
4766 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4767 				}
4768 			}
4769 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4770 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4771 
4772 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4773 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4774 
4775 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
4776 			    omrp)) != 0) {
4777 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4778 				    sizeof (*omrp));
4779 			} else {
4780 				mac_set_prim_vlan_rings(mip, vmrp);
4781 			}
4782 			kmem_free(omrp, sizeof (*omrp));
4783 		}
4784 	}
4785 
4786 	/* Only update the values if mac_client_set_resources succeeded */
4787 	if (err == 0) {
4788 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
4789 		/*
4790 		 * If bandwidth, priority or cpu link properties cleared,
4791 		 * renable fastpath.
4792 		 */
4793 		if (resmask != 0 && newresmask == 0)
4794 			mac_fastpath_enable((mac_handle_t)mip);
4795 	} else if (resmask == 0 && newresmask != 0) {
4796 		mac_fastpath_enable((mac_handle_t)mip);
4797 	}
4798 	kmem_free(tmrp, sizeof (*tmrp));
4799 	kmem_free(umrp, sizeof (*umrp));
4800 	return (err);
4801 }
4802 
4803 int
mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4804 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4805 {
4806 	int err;
4807 
4808 	i_mac_perim_enter((mac_impl_t *)mh);
4809 	err = i_mac_set_resources(mh, mrp);
4810 	i_mac_perim_exit((mac_impl_t *)mh);
4811 	return (err);
4812 }
4813 
4814 /*
4815  * Get the properties cached for the specified MAC instance.
4816  */
4817 void
mac_get_resources(mac_handle_t mh,mac_resource_props_t * mrp)4818 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4819 {
4820 	mac_impl_t		*mip = (mac_impl_t *)mh;
4821 	mac_client_impl_t	*mcip;
4822 
4823 	mcip = mac_primary_client_handle(mip);
4824 	if (mcip != NULL) {
4825 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4826 		return;
4827 	}
4828 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
4829 }
4830 
4831 /*
4832  * Get the effective properties from the primary client of the
4833  * specified MAC instance.
4834  */
4835 void
mac_get_effective_resources(mac_handle_t mh,mac_resource_props_t * mrp)4836 mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4837 {
4838 	mac_impl_t		*mip = (mac_impl_t *)mh;
4839 	mac_client_impl_t	*mcip;
4840 
4841 	mcip = mac_primary_client_handle(mip);
4842 	if (mcip != NULL) {
4843 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
4844 		    mrp);
4845 		return;
4846 	}
4847 	bzero(mrp, sizeof (mac_resource_props_t));
4848 }
4849 
4850 int
mac_set_pvid(mac_handle_t mh,uint16_t pvid)4851 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
4852 {
4853 	mac_impl_t *mip = (mac_impl_t *)mh;
4854 	mac_client_impl_t *mcip;
4855 	mac_unicast_impl_t *muip;
4856 
4857 	i_mac_perim_enter(mip);
4858 	if (pvid != 0) {
4859 		for (mcip = mip->mi_clients_list; mcip != NULL;
4860 		    mcip = mcip->mci_client_next) {
4861 			for (muip = mcip->mci_unicast_list; muip != NULL;
4862 			    muip = muip->mui_next) {
4863 				if (muip->mui_vid == pvid) {
4864 					i_mac_perim_exit(mip);
4865 					return (EBUSY);
4866 				}
4867 			}
4868 		}
4869 	}
4870 	mip->mi_pvid = pvid;
4871 	i_mac_perim_exit(mip);
4872 	return (0);
4873 }
4874 
4875 uint16_t
mac_get_pvid(mac_handle_t mh)4876 mac_get_pvid(mac_handle_t mh)
4877 {
4878 	mac_impl_t *mip = (mac_impl_t *)mh;
4879 
4880 	return (mip->mi_pvid);
4881 }
4882 
4883 uint32_t
mac_get_llimit(mac_handle_t mh)4884 mac_get_llimit(mac_handle_t mh)
4885 {
4886 	mac_impl_t *mip = (mac_impl_t *)mh;
4887 
4888 	return (mip->mi_llimit);
4889 }
4890 
4891 uint32_t
mac_get_ldecay(mac_handle_t mh)4892 mac_get_ldecay(mac_handle_t mh)
4893 {
4894 	mac_impl_t *mip = (mac_impl_t *)mh;
4895 
4896 	return (mip->mi_ldecay);
4897 }
4898 
4899 /*
4900  * Rename a mac client, its flow, and the kstat.
4901  */
4902 int
mac_rename_primary(mac_handle_t mh,const char * new_name)4903 mac_rename_primary(mac_handle_t mh, const char *new_name)
4904 {
4905 	mac_impl_t		*mip = (mac_impl_t *)mh;
4906 	mac_client_impl_t	*cur_clnt = NULL;
4907 	flow_entry_t		*fep;
4908 
4909 	i_mac_perim_enter(mip);
4910 
4911 	/*
4912 	 * VNICs: we need to change the sys flow name and
4913 	 * the associated flow kstat.
4914 	 */
4915 	if (mip->mi_state_flags & MIS_IS_VNIC) {
4916 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
4917 		ASSERT(new_name != NULL);
4918 		mac_rename_flow_names(mcip, new_name);
4919 		mac_stat_rename(mcip);
4920 		goto done;
4921 	}
4922 	/*
4923 	 * This mac may itself be an aggr link, or it may have some client
4924 	 * which is an aggr port. For both cases, we need to change the
4925 	 * aggr port's mac client name, its flow name and the associated flow
4926 	 * kstat.
4927 	 */
4928 	if (mip->mi_state_flags & MIS_IS_AGGR) {
4929 		mac_capab_aggr_t aggr_cap;
4930 		mac_rename_fn_t rename_fn;
4931 		boolean_t ret;
4932 
4933 		ASSERT(new_name != NULL);
4934 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4935 		    (void *)(&aggr_cap));
4936 		ASSERT(ret == B_TRUE);
4937 		rename_fn = aggr_cap.mca_rename_fn;
4938 		rename_fn(new_name, mip->mi_driver);
4939 		/*
4940 		 * The aggr's client name and kstat flow name will be
4941 		 * updated below, i.e. via mac_rename_flow_names.
4942 		 */
4943 	}
4944 
4945 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4946 	    cur_clnt = cur_clnt->mci_client_next) {
4947 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4948 			if (new_name != NULL) {
4949 				char *str_st = cur_clnt->mci_name;
4950 				char *str_del = strchr(str_st, '-');
4951 
4952 				ASSERT(str_del != NULL);
4953 				bzero(str_del + 1, MAXNAMELEN -
4954 				    (str_del - str_st + 1));
4955 				bcopy(new_name, str_del + 1,
4956 				    strlen(new_name));
4957 			}
4958 			fep = cur_clnt->mci_flent;
4959 			mac_rename_flow(fep, cur_clnt->mci_name);
4960 			break;
4961 		} else if (new_name != NULL &&
4962 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4963 			mac_rename_flow_names(cur_clnt, new_name);
4964 			break;
4965 		}
4966 	}
4967 
4968 	/* Recreate kstats associated with aggr pseudo rings */
4969 	if (mip->mi_state_flags & MIS_IS_AGGR)
4970 		mac_pseudo_ring_stat_rename(mip);
4971 
4972 done:
4973 	i_mac_perim_exit(mip);
4974 	return (0);
4975 }
4976 
4977 /*
4978  * Rename the MAC client's flow names
4979  */
4980 static void
mac_rename_flow_names(mac_client_impl_t * mcip,const char * new_name)4981 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4982 {
4983 	flow_entry_t	*flent;
4984 	uint16_t	vid;
4985 	char		flowname[MAXFLOWNAMELEN];
4986 	mac_impl_t	*mip = mcip->mci_mip;
4987 
4988 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4989 
4990 	/*
4991 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4992 	 * see a self-consistent value for mci_name
4993 	 */
4994 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
4995 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4996 	rw_exit(&mip->mi_rw_lock);
4997 
4998 	mac_rename_flow(mcip->mci_flent, new_name);
4999 
5000 	if (mcip->mci_nflents == 1)
5001 		return;
5002 
5003 	/*
5004 	 * We have to rename all the others too, no stats to destroy for
5005 	 * these.
5006 	 */
5007 	for (flent = mcip->mci_flent_list; flent != NULL;
5008 	    flent = flent->fe_client_next) {
5009 		if (flent != mcip->mci_flent) {
5010 			vid = i_mac_flow_vid(flent);
5011 			(void) sprintf(flowname, "%s%u", new_name, vid);
5012 			mac_flow_set_name(flent, flowname);
5013 		}
5014 	}
5015 }
5016 
5017 
5018 /*
5019  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
5020  * defined for the specified MAC client.
5021  */
5022 static void
mac_client_add_to_flow_list(mac_client_impl_t * mcip,flow_entry_t * flent)5023 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5024 {
5025 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5026 	/*
5027 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5028 	 * using mi_rw_lock
5029 	 */
5030 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5031 
5032 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5033 
5034 	/* Add it to the head */
5035 	flent->fe_client_next = mcip->mci_flent_list;
5036 	mcip->mci_flent_list = flent;
5037 	mcip->mci_nflents++;
5038 
5039 	/*
5040 	 * Keep track of the number of non-zero VIDs addresses per MAC
5041 	 * client to avoid figuring it out in the data-path.
5042 	 */
5043 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5044 		mcip->mci_nvids++;
5045 
5046 	rw_exit(&mcip->mci_rw_lock);
5047 }
5048 
5049 /*
5050  * Remove a flow entry from the MAC client's list.
5051  */
5052 static void
mac_client_remove_flow_from_list(mac_client_impl_t * mcip,flow_entry_t * flent)5053 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5054 {
5055 	flow_entry_t	*fe = mcip->mci_flent_list;
5056 	flow_entry_t	*prev_fe = NULL;
5057 
5058 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5059 	/*
5060 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5061 	 * using mci_rw_lock
5062 	 */
5063 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5064 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5065 
5066 	while ((fe != NULL) && (fe != flent)) {
5067 		prev_fe = fe;
5068 		fe = fe->fe_client_next;
5069 	}
5070 
5071 	ASSERT(fe != NULL);
5072 	if (prev_fe == NULL) {
5073 		/* Deleting the first node */
5074 		mcip->mci_flent_list = fe->fe_client_next;
5075 	} else {
5076 		prev_fe->fe_client_next = fe->fe_client_next;
5077 	}
5078 	mcip->mci_nflents--;
5079 
5080 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5081 		mcip->mci_nvids--;
5082 
5083 	rw_exit(&mcip->mci_rw_lock);
5084 }
5085 
5086 /*
5087  * Check if the given VID belongs to this MAC client.
5088  */
5089 boolean_t
mac_client_check_flow_vid(mac_client_impl_t * mcip,uint16_t vid)5090 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
5091 {
5092 	flow_entry_t	*flent;
5093 	uint16_t	mci_vid;
5094 	uint32_t	cache = mcip->mci_vidcache;
5095 
5096 	/*
5097 	 * In hopes of not having to touch the mci_rw_lock, check to see if
5098 	 * this vid matches our cached result.
5099 	 */
5100 	if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
5101 		return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
5102 
5103 	/* The mci_flent_list is protected by mci_rw_lock */
5104 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5105 	for (flent = mcip->mci_flent_list; flent != NULL;
5106 	    flent = flent->fe_client_next) {
5107 		mci_vid = i_mac_flow_vid(flent);
5108 		if (vid == mci_vid) {
5109 			mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
5110 			rw_exit(&mcip->mci_rw_lock);
5111 			return (B_TRUE);
5112 		}
5113 	}
5114 
5115 	mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
5116 	rw_exit(&mcip->mci_rw_lock);
5117 	return (B_FALSE);
5118 }
5119 
5120 /*
5121  * Get the flow entry for the specified <MAC addr, VID> tuple.
5122  */
5123 static flow_entry_t *
mac_client_get_flow(mac_client_impl_t * mcip,mac_unicast_impl_t * muip)5124 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
5125 {
5126 	mac_address_t *map = mcip->mci_unicast;
5127 	flow_entry_t *flent;
5128 	uint16_t vid;
5129 	flow_desc_t flow_desc;
5130 
5131 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5132 
5133 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5134 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5135 		return (NULL);
5136 
5137 	for (flent = mcip->mci_flent_list; flent != NULL;
5138 	    flent = flent->fe_client_next) {
5139 		vid = i_mac_flow_vid(flent);
5140 		if (vid == muip->mui_vid) {
5141 			return (flent);
5142 		}
5143 	}
5144 
5145 	return (NULL);
5146 }
5147 
5148 /*
5149  * Since mci_flent has the SRSs, when we want to remove it, we replace
5150  * the flow_desc_t in mci_flent with that of an existing flent and then
5151  * remove that flent instead of mci_flent.
5152  */
5153 static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t * mcip)5154 mac_client_swap_mciflent(mac_client_impl_t *mcip)
5155 {
5156 	flow_entry_t	*flent = mcip->mci_flent;
5157 	flow_tab_t	*ft = flent->fe_flow_tab;
5158 	flow_entry_t	*flent1;
5159 	flow_desc_t	fl_desc;
5160 	char		fl_name[MAXFLOWNAMELEN];
5161 	int		err;
5162 
5163 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5164 	ASSERT(mcip->mci_nflents > 1);
5165 
5166 	/* get the next flent following the primary flent  */
5167 	flent1 = mcip->mci_flent_list->fe_client_next;
5168 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5169 
5170 	/*
5171 	 * Remove the flent from the flow table before updating the
5172 	 * flow descriptor as the hash depends on the flow descriptor.
5173 	 * This also helps incoming packet classification avoid having
5174 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5175 	 * flow table is done under the fe_lock so that log or stat functions
5176 	 * see a self-consistent fe_flow_desc. The name and desc are specific
5177 	 * to a flow, the rest are shared by all the clients, including
5178 	 * resource control etc.
5179 	 */
5180 	mac_flow_remove(ft, flent, B_TRUE);
5181 	mac_flow_remove(ft, flent1, B_TRUE);
5182 
5183 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5184 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5185 
5186 	/* update the primary flow entry */
5187 	mutex_enter(&flent->fe_lock);
5188 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5189 	    sizeof (flow_desc_t));
5190 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5191 	mutex_exit(&flent->fe_lock);
5192 
5193 	/* update the flow entry that is to be freed */
5194 	mutex_enter(&flent1->fe_lock);
5195 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5196 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5197 	mutex_exit(&flent1->fe_lock);
5198 
5199 	/* now reinsert the flow entries in the table */
5200 	err = mac_flow_add(ft, flent);
5201 	ASSERT(err == 0);
5202 
5203 	err = mac_flow_add(ft, flent1);
5204 	ASSERT(err == 0);
5205 
5206 	return (flent1);
5207 }
5208 
5209 /*
5210  * Return whether there is only one flow entry associated with this
5211  * MAC client.
5212  */
5213 static boolean_t
mac_client_single_rcvr(mac_client_impl_t * mcip)5214 mac_client_single_rcvr(mac_client_impl_t *mcip)
5215 {
5216 	return (mcip->mci_nflents == 1);
5217 }
5218 
5219 int
mac_validate_props(mac_impl_t * mip,mac_resource_props_t * mrp)5220 mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5221 {
5222 	boolean_t		reset;
5223 	uint32_t		rings_needed;
5224 	uint32_t		rings_avail;
5225 	mac_group_type_t	gtype;
5226 	mac_resource_props_t	*mip_mrp;
5227 
5228 	if (mrp == NULL)
5229 		return (0);
5230 
5231 	if (mrp->mrp_mask & MRP_PRIORITY) {
5232 		mac_priority_level_t	pri = mrp->mrp_priority;
5233 
5234 		if (pri < MPL_LOW || pri > MPL_RESET)
5235 			return (EINVAL);
5236 	}
5237 
5238 	if (mrp->mrp_mask & MRP_MAXBW) {
5239 		uint64_t maxbw = mrp->mrp_maxbw;
5240 
5241 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5242 			return (EINVAL);
5243 	}
5244 	if (mrp->mrp_mask & MRP_CPUS) {
5245 		int i, j;
5246 		mac_cpu_mode_t	fanout;
5247 
5248 		if (mrp->mrp_ncpus > ncpus)
5249 			return (EINVAL);
5250 
5251 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5252 			for (j = 0; j < mrp->mrp_ncpus; j++) {
5253 				if (i != j &&
5254 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5255 					return (EINVAL);
5256 				}
5257 			}
5258 		}
5259 
5260 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5261 			cpu_t *cp;
5262 			int rv;
5263 
5264 			mutex_enter(&cpu_lock);
5265 			cp = cpu_get(mrp->mrp_cpu[i]);
5266 			if (cp != NULL)
5267 				rv = cpu_is_online(cp);
5268 			else
5269 				rv = 0;
5270 			mutex_exit(&cpu_lock);
5271 			if (rv == 0)
5272 				return (EINVAL);
5273 		}
5274 
5275 		fanout = mrp->mrp_fanout_mode;
5276 		if (fanout < 0 || fanout > MCM_CPUS)
5277 			return (EINVAL);
5278 	}
5279 
5280 	if (mrp->mrp_mask & MRP_PROTECT) {
5281 		int err = mac_protect_validate(mrp);
5282 		if (err != 0)
5283 			return (err);
5284 	}
5285 
5286 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
5287 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
5288 		return (0);
5289 	}
5290 
5291 	/*
5292 	 * mip will be null when we come from mac_flow_create or
5293 	 * mac_link_flow_modify. In the latter case it is a user flow,
5294 	 * for which we don't support rings. In the former we would
5295 	 * have validated the props beforehand (i_mac_unicast_add ->
5296 	 * mac_client_set_resources -> validate for the primary and
5297 	 * vnic_dev_create -> mac_client_set_resources -> validate for
5298 	 * a vnic.
5299 	 */
5300 	if (mip == NULL)
5301 		return (0);
5302 
5303 	/*
5304 	 * We don't support setting rings property for a VNIC that is using a
5305 	 * primary address (VLAN)
5306 	 */
5307 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
5308 	    mac_is_vnic_primary((mac_handle_t)mip)) {
5309 		return (ENOTSUP);
5310 	}
5311 
5312 	mip_mrp = &mip->mi_resource_props;
5313 	/*
5314 	 * The rings property should be validated against the NICs
5315 	 * resources
5316 	 */
5317 	if (mip->mi_state_flags & MIS_IS_VNIC)
5318 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
5319 
5320 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
5321 	/*
5322 	 * If groups are not supported, return error.
5323 	 */
5324 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
5325 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
5326 		return (EINVAL);
5327 	}
5328 	/*
5329 	 * If we are just resetting, there is no validation needed.
5330 	 */
5331 	if (reset)
5332 		return (0);
5333 
5334 	if (mrp->mrp_mask & MRP_RX_RINGS) {
5335 		rings_needed = mrp->mrp_nrxrings;
5336 		/*
5337 		 * We just want to check if the number of additional
5338 		 * rings requested is available.
5339 		 */
5340 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
5341 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
5342 				/* Just check for the additional rings */
5343 				rings_needed -= mip_mrp->mrp_nrxrings;
5344 			else
5345 				/* We are not asking for additional rings */
5346 				rings_needed = 0;
5347 		}
5348 		rings_avail = mip->mi_rxrings_avail;
5349 		gtype = mip->mi_rx_group_type;
5350 	} else {
5351 		rings_needed = mrp->mrp_ntxrings;
5352 		/* Similarly for the TX rings */
5353 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
5354 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
5355 				/* Just check for the additional rings */
5356 				rings_needed -= mip_mrp->mrp_ntxrings;
5357 			else
5358 				/* We are not asking for additional rings */
5359 				rings_needed = 0;
5360 		}
5361 		rings_avail = mip->mi_txrings_avail;
5362 		gtype = mip->mi_tx_group_type;
5363 	}
5364 
5365 	/* Error if the group is dynamic .. */
5366 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5367 		/*
5368 		 * .. and rings specified are more than available.
5369 		 */
5370 		if (rings_needed > rings_avail)
5371 			return (EINVAL);
5372 	} else {
5373 		/*
5374 		 * OR group is static and we have specified some rings.
5375 		 */
5376 		if (rings_needed > 0)
5377 			return (EINVAL);
5378 	}
5379 	return (0);
5380 }
5381 
5382 /*
5383  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5384  * underlying physical link is down. This is to allow MAC clients to
5385  * communicate with other clients.
5386  */
5387 void
mac_virtual_link_update(mac_impl_t * mip)5388 mac_virtual_link_update(mac_impl_t *mip)
5389 {
5390 	if (mip->mi_linkstate != LINK_STATE_UP)
5391 		i_mac_notify(mip, MAC_NOTE_LINK);
5392 }
5393 
5394 /*
5395  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5396  * mac handle in the client.
5397  */
5398 void
mac_set_upper_mac(mac_client_handle_t mch,mac_handle_t mh,mac_resource_props_t * mrp)5399 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5400     mac_resource_props_t *mrp)
5401 {
5402 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5403 	mac_impl_t		*mip = (mac_impl_t *)mh;
5404 
5405 	mcip->mci_upper_mip = mip;
5406 	/* If there are any properties, copy it over too */
5407 	if (mrp != NULL) {
5408 		bcopy(mrp, &mip->mi_resource_props,
5409 		    sizeof (mac_resource_props_t));
5410 	}
5411 }
5412 
5413 /*
5414  * Mark the mac as being used exclusively by the single mac client that is
5415  * doing some control operation on this mac. No further opens of this mac
5416  * will be allowed until this client calls mac_unmark_exclusive. The mac
5417  * client calling this function must already be in the mac perimeter
5418  */
5419 int
mac_mark_exclusive(mac_handle_t mh)5420 mac_mark_exclusive(mac_handle_t mh)
5421 {
5422 	mac_impl_t	*mip = (mac_impl_t *)mh;
5423 
5424 	ASSERT(MAC_PERIM_HELD(mh));
5425 	/*
5426 	 * Look up its entry in the global hash table.
5427 	 */
5428 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5429 	if (mip->mi_state_flags & MIS_DISABLED) {
5430 		rw_exit(&i_mac_impl_lock);
5431 		return (ENOENT);
5432 	}
5433 
5434 	/*
5435 	 * A reference to mac is held even if the link is not plumbed.
5436 	 * In i_dls_link_create() we open the MAC interface and hold the
5437 	 * reference. There is an additional reference for the mac_open
5438 	 * done in acquiring the mac perimeter
5439 	 */
5440 	if (mip->mi_ref != 2) {
5441 		rw_exit(&i_mac_impl_lock);
5442 		return (EBUSY);
5443 	}
5444 
5445 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5446 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5447 	rw_exit(&i_mac_impl_lock);
5448 	return (0);
5449 }
5450 
5451 void
mac_unmark_exclusive(mac_handle_t mh)5452 mac_unmark_exclusive(mac_handle_t mh)
5453 {
5454 	mac_impl_t	*mip = (mac_impl_t *)mh;
5455 
5456 	ASSERT(MAC_PERIM_HELD(mh));
5457 
5458 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5459 	/* 1 for the creation and another for the perimeter */
5460 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5461 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5462 	rw_exit(&i_mac_impl_lock);
5463 }
5464 
5465 /*
5466  * Set the MTU for the specified MAC.
5467  */
5468 int
mac_set_mtu(mac_handle_t mh,uint_t new_mtu,uint_t * old_mtu_arg)5469 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5470 {
5471 	mac_impl_t *mip = (mac_impl_t *)mh;
5472 	uint_t old_mtu;
5473 	int rv = 0;
5474 
5475 	i_mac_perim_enter(mip);
5476 
5477 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5478 		rv = ENOTSUP;
5479 		goto bail;
5480 	}
5481 
5482 	old_mtu = mip->mi_sdu_max;
5483 
5484 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5485 		rv = EINVAL;
5486 		goto bail;
5487 	}
5488 
5489 	rw_enter(&mip->mi_rw_lock, RW_READER);
5490 	if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5491 		rv = EBUSY;
5492 		rw_exit(&mip->mi_rw_lock);
5493 		goto bail;
5494 	}
5495 	rw_exit(&mip->mi_rw_lock);
5496 
5497 	if (old_mtu != new_mtu) {
5498 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5499 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5500 		if (rv != 0)
5501 			goto bail;
5502 		rv = mac_maxsdu_update(mh, new_mtu);
5503 		ASSERT(rv == 0);
5504 	}
5505 
5506 bail:
5507 	i_mac_perim_exit(mip);
5508 
5509 	if (rv == 0 && old_mtu_arg != NULL)
5510 		*old_mtu_arg = old_mtu;
5511 	return (rv);
5512 }
5513 
5514 /*
5515  * Return the RX h/w information for the group indexed by grp_num.
5516  */
5517 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)5518 mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5519     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5520     char *clnts_name)
5521 {
5522 	mac_impl_t *mip = (mac_impl_t *)mh;
5523 	mac_grp_client_t *mcip;
5524 	uint_t i = 0, index = 0;
5525 	mac_ring_t	*ring;
5526 
5527 	/* Revisit when we implement fully dynamic group allocation */
5528 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5529 
5530 	rw_enter(&mip->mi_rw_lock, RW_READER);
5531 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5532 	*type = mip->mi_rx_groups[grp_index].mrg_type;
5533 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5534 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
5535 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5536 	    index++) {
5537 		rings[index] = ring->mr_index;
5538 		ring = ring->mr_next;
5539 	}
5540 	/* Assuming the 1st is the default group */
5541 	index = 0;
5542 	if (grp_index == 0) {
5543 		(void) strlcpy(clnts_name, "<default,mcast>,",
5544 		    MAXCLIENTNAMELEN);
5545 		index += strlen("<default,mcast>,");
5546 	}
5547 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5548 	    mcip = mcip->mgc_next) {
5549 		int name_len = strlen(mcip->mgc_client->mci_name);
5550 
5551 		/*
5552 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5553 		 * names.
5554 		 * XXXX Formating the client name string needs to be moved
5555 		 * to user land when fixing the size of dhi_clnts in
5556 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5557 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5558 		 */
5559 		if (index + name_len >= MAXCLIENTNAMELEN) {
5560 			index = MAXCLIENTNAMELEN;
5561 			break;
5562 		}
5563 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5564 		    name_len);
5565 		index += name_len;
5566 		clnts_name[index++] = ',';
5567 		i++;
5568 	}
5569 
5570 	/* Get rid of the last , */
5571 	if (index > 0)
5572 		clnts_name[index - 1] = '\0';
5573 	*n_clnts = i;
5574 	rw_exit(&mip->mi_rw_lock);
5575 }
5576 
5577 /*
5578  * Return the TX h/w information for the group indexed by grp_num.
5579  */
5580 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)5581 mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5582     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5583     char *clnts_name)
5584 {
5585 	mac_impl_t *mip = (mac_impl_t *)mh;
5586 	mac_grp_client_t *mcip;
5587 	uint_t i = 0, index = 0;
5588 	mac_ring_t	*ring;
5589 
5590 	/* Revisit when we implement fully dynamic group allocation */
5591 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5592 
5593 	rw_enter(&mip->mi_rw_lock, RW_READER);
5594 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5595 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5596 	*type = mip->mi_tx_groups[grp_index].mrg_type;
5597 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5598 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
5599 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5600 	    index++) {
5601 		rings[index] = ring->mr_index;
5602 		ring = ring->mr_next;
5603 	}
5604 	index = 0;
5605 	/* Default group has an index of -1 */
5606 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5607 		(void) strlcpy(clnts_name, "<default>,",
5608 		    MAXCLIENTNAMELEN);
5609 		index += strlen("<default>,");
5610 	}
5611 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5612 	    mcip = mcip->mgc_next) {
5613 		int name_len = strlen(mcip->mgc_client->mci_name);
5614 
5615 		/*
5616 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5617 		 * names.
5618 		 * XXXX Formating the client name string needs to be moved
5619 		 * to user land when fixing the size of dhi_clnts in
5620 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5621 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5622 		 */
5623 		if (index + name_len >= MAXCLIENTNAMELEN) {
5624 			index = MAXCLIENTNAMELEN;
5625 			break;
5626 		}
5627 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5628 		    name_len);
5629 		index += name_len;
5630 		clnts_name[index++] = ',';
5631 		i++;
5632 	}
5633 
5634 	/* Get rid of the last , */
5635 	if (index > 0)
5636 		clnts_name[index - 1] = '\0';
5637 	*n_clnts = i;
5638 	rw_exit(&mip->mi_rw_lock);
5639 }
5640 
5641 /*
5642  * Return the group count for RX or TX.
5643  */
5644 uint_t
mac_hwgrp_num(mac_handle_t mh,int type)5645 mac_hwgrp_num(mac_handle_t mh, int type)
5646 {
5647 	mac_impl_t *mip = (mac_impl_t *)mh;
5648 
5649 	/*
5650 	 * Return the Rx and Tx group count; for the Tx we need to
5651 	 * include the default too.
5652 	 */
5653 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5654 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5655 }
5656 
5657 /*
5658  * The total number of free TX rings for this MAC.
5659  */
5660 uint_t
mac_txavail_get(mac_handle_t mh)5661 mac_txavail_get(mac_handle_t mh)
5662 {
5663 	mac_impl_t	*mip = (mac_impl_t *)mh;
5664 
5665 	return (mip->mi_txrings_avail);
5666 }
5667 
5668 /*
5669  * The total number of free RX rings for this MAC.
5670  */
5671 uint_t
mac_rxavail_get(mac_handle_t mh)5672 mac_rxavail_get(mac_handle_t mh)
5673 {
5674 	mac_impl_t	*mip = (mac_impl_t *)mh;
5675 
5676 	return (mip->mi_rxrings_avail);
5677 }
5678 
5679 /*
5680  * The total number of reserved RX rings on this MAC.
5681  */
5682 uint_t
mac_rxrsvd_get(mac_handle_t mh)5683 mac_rxrsvd_get(mac_handle_t mh)
5684 {
5685 	mac_impl_t	*mip = (mac_impl_t *)mh;
5686 
5687 	return (mip->mi_rxrings_rsvd);
5688 }
5689 
5690 /*
5691  * The total number of reserved TX rings on this MAC.
5692  */
5693 uint_t
mac_txrsvd_get(mac_handle_t mh)5694 mac_txrsvd_get(mac_handle_t mh)
5695 {
5696 	mac_impl_t	*mip = (mac_impl_t *)mh;
5697 
5698 	return (mip->mi_txrings_rsvd);
5699 }
5700 
5701 /*
5702  * Total number of free RX groups on this MAC.
5703  */
5704 uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)5705 mac_rxhwlnksavail_get(mac_handle_t mh)
5706 {
5707 	mac_impl_t	*mip = (mac_impl_t *)mh;
5708 
5709 	return (mip->mi_rxhwclnt_avail);
5710 }
5711 
5712 /*
5713  * Total number of RX groups reserved on this MAC.
5714  */
5715 uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)5716 mac_rxhwlnksrsvd_get(mac_handle_t mh)
5717 {
5718 	mac_impl_t	*mip = (mac_impl_t *)mh;
5719 
5720 	return (mip->mi_rxhwclnt_used);
5721 }
5722 
5723 /*
5724  * Total number of free TX groups on this MAC.
5725  */
5726 uint_t
mac_txhwlnksavail_get(mac_handle_t mh)5727 mac_txhwlnksavail_get(mac_handle_t mh)
5728 {
5729 	mac_impl_t	*mip = (mac_impl_t *)mh;
5730 
5731 	return (mip->mi_txhwclnt_avail);
5732 }
5733 
5734 /*
5735  * Total number of TX groups reserved on this MAC.
5736  */
5737 uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)5738 mac_txhwlnksrsvd_get(mac_handle_t mh)
5739 {
5740 	mac_impl_t	*mip = (mac_impl_t *)mh;
5741 
5742 	return (mip->mi_txhwclnt_used);
5743 }
5744 
5745 /*
5746  * Initialize the rings property for a mac client. A non-0 value for
5747  * rxring or txring specifies the number of rings required, a value
5748  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5749  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5750  * means the system can decide whether it can give any rings or not.
5751  */
5752 void
mac_client_set_rings(mac_client_handle_t mch,int rxrings,int txrings)5753 mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5754 {
5755 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5756 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
5757 
5758 	if (rxrings != MAC_RXRINGS_DONTCARE) {
5759 		mrp->mrp_mask |= MRP_RX_RINGS;
5760 		mrp->mrp_nrxrings = rxrings;
5761 	}
5762 
5763 	if (txrings != MAC_TXRINGS_DONTCARE) {
5764 		mrp->mrp_mask |= MRP_TX_RINGS;
5765 		mrp->mrp_ntxrings = txrings;
5766 	}
5767 }
5768 
5769 boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)5770 mac_get_promisc_filtered(mac_client_handle_t mch)
5771 {
5772 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5773 
5774 	return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
5775 }
5776 
5777 void
mac_set_promisc_filtered(mac_client_handle_t mch,boolean_t enable)5778 mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
5779 {
5780 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5781 
5782 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5783 	if (enable)
5784 		mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
5785 	else
5786 		mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
5787 }
5788