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 2025 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, srs_tx->st_arg2, mp_chain,
3641 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
3949 void
mac_resource_set_common(mac_client_handle_t mch,mac_resource_add_t add,mac_resource_remove_t remove,mac_resource_quiesce_t quiesce,mac_resource_restart_t restart,mac_resource_bind_t bind,void * arg)3950 mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3951 mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3952 mac_resource_restart_t restart, mac_resource_bind_t bind,
3953 void *arg)
3954 {
3955 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3956
3957 mcip->mci_resource_add = add;
3958 mcip->mci_resource_remove = remove;
3959 mcip->mci_resource_quiesce = quiesce;
3960 mcip->mci_resource_restart = restart;
3961 mcip->mci_resource_bind = bind;
3962 mcip->mci_resource_arg = arg;
3963 }
3964
3965 void
mac_resource_set(mac_client_handle_t mch,mac_resource_add_t add,void * arg)3966 mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3967 {
3968 /* update the 'resource_add' callback */
3969 mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3970 }
3971
3972 /*
3973 * Sets up the client resources and enable the polling interface over all the
3974 * SRS's and the soft rings of the client
3975 */
3976 void
mac_client_poll_enable(mac_client_handle_t mch)3977 mac_client_poll_enable(mac_client_handle_t mch)
3978 {
3979 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3980 mac_soft_ring_set_t *mac_srs;
3981 flow_entry_t *flent;
3982 int i;
3983
3984 flent = mcip->mci_flent;
3985 ASSERT(flent != NULL);
3986
3987 mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3988 for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3989 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3990 ASSERT(mac_srs->srs_mcip == mcip);
3991 mac_srs_client_poll_enable(mcip, mac_srs);
3992 }
3993 }
3994
3995 /*
3996 * Tears down the client resources and disable the polling interface over all
3997 * the SRS's and the soft rings of the client
3998 */
3999 void
mac_client_poll_disable(mac_client_handle_t mch)4000 mac_client_poll_disable(mac_client_handle_t mch)
4001 {
4002 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4003 mac_soft_ring_set_t *mac_srs;
4004 flow_entry_t *flent;
4005 int i;
4006
4007 flent = mcip->mci_flent;
4008 ASSERT(flent != NULL);
4009
4010 mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
4011 for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
4012 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
4013 ASSERT(mac_srs->srs_mcip == mcip);
4014 mac_srs_client_poll_disable(mcip, mac_srs);
4015 }
4016 }
4017
4018 /*
4019 * Associate the CPUs specified by the given property with a MAC client.
4020 */
4021 int
mac_cpu_set(mac_client_handle_t mch,mac_resource_props_t * mrp)4022 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
4023 {
4024 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4025 mac_impl_t *mip = mcip->mci_mip;
4026 int err = 0;
4027
4028 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4029
4030 if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
4031 mcip->mci_upper_mip : mip, mrp)) != 0) {
4032 return (err);
4033 }
4034 if (MCIP_DATAPATH_SETUP(mcip))
4035 mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
4036
4037 mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
4038 return (0);
4039 }
4040
4041 /*
4042 * Apply the specified properties to the specified MAC client.
4043 */
4044 int
mac_client_set_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4045 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4046 {
4047 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4048 mac_impl_t *mip = mcip->mci_mip;
4049 int err = 0;
4050
4051 i_mac_perim_enter(mip);
4052
4053 if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
4054 err = mac_resource_ctl_set(mch, mrp);
4055 if (err != 0)
4056 goto done;
4057 }
4058
4059 if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
4060 err = mac_cpu_set(mch, mrp);
4061 if (err != 0)
4062 goto done;
4063 }
4064
4065 if (mrp->mrp_mask & MRP_PROTECT) {
4066 err = mac_protect_set(mch, mrp);
4067 if (err != 0)
4068 goto done;
4069 }
4070
4071 if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
4072 err = mac_resource_ctl_set(mch, mrp);
4073
4074 done:
4075 i_mac_perim_exit(mip);
4076 return (err);
4077 }
4078
4079 /*
4080 * Return the properties currently associated with the specified MAC client.
4081 */
4082 void
mac_client_get_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4083 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4084 {
4085 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4086 mac_resource_props_t *mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4087
4088 bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4089 }
4090
4091 /*
4092 * Return the effective properties currently associated with the specified
4093 * MAC client.
4094 */
4095 void
mac_client_get_effective_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4096 mac_client_get_effective_resources(mac_client_handle_t mch,
4097 mac_resource_props_t *mrp)
4098 {
4099 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4100 mac_resource_props_t *mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
4101
4102 bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4103 }
4104
4105 /*
4106 * Pass a copy of the specified packet to the promiscuous callbacks
4107 * of the specified MAC.
4108 *
4109 * If sender is NULL, the function is being invoked for a packet chain
4110 * received from the wire. If sender is non-NULL, it points to
4111 * the MAC client from which the packet is being sent.
4112 *
4113 * The packets are distributed to the promiscuous callbacks as follows:
4114 *
4115 * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4116 * - all broadcast and multicast packets are sent to the
4117 * MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4118 *
4119 * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4120 * after classification by mac_rx_deliver().
4121 */
4122 static void
mac_promisc_dispatch_one(mac_promisc_impl_t * mpip,mblk_t * mp,boolean_t loopback,boolean_t local)4123 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4124 boolean_t loopback, boolean_t local)
4125 {
4126 mblk_t *mp_next;
4127
4128 if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4129 mblk_t *mp_copy;
4130
4131 mp_copy = copymsg(mp);
4132 if (mp_copy == NULL)
4133 return;
4134
4135 if (mpip->mpi_strip_vlan_tag) {
4136 mp_copy = mac_strip_vlan_tag_chain(mp_copy);
4137 if (mp_copy == NULL)
4138 return;
4139 }
4140
4141 /*
4142 * There is code upstack that can't deal with message
4143 * chains.
4144 */
4145 for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
4146 mp_next = tmp->b_next;
4147 tmp->b_next = NULL;
4148 mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
4149 }
4150
4151 return;
4152 }
4153
4154 mp_next = mp->b_next;
4155 mp->b_next = NULL;
4156 mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
4157 mp->b_next = mp_next;
4158 }
4159
4160 /*
4161 * Return the VID of a packet. Zero if the packet is not tagged.
4162 */
4163 static uint16_t
mac_ether_vid(mblk_t * mp)4164 mac_ether_vid(mblk_t *mp)
4165 {
4166 struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4167
4168 if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4169 struct ether_vlan_header *t_evhp =
4170 (struct ether_vlan_header *)mp->b_rptr;
4171 return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4172 }
4173
4174 return (0);
4175 }
4176
4177 /*
4178 * Return whether the specified packet contains a multicast or broadcast
4179 * destination MAC address.
4180 */
4181 static boolean_t
mac_is_mcast(mac_impl_t * mip,mblk_t * mp)4182 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4183 {
4184 mac_header_info_t hdr_info;
4185
4186 if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4187 return (B_FALSE);
4188 return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4189 (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4190 }
4191
4192 /*
4193 * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4194 * "sender" points to the sender MAC client for outbound packets, and
4195 * is set to NULL for inbound packets.
4196 */
4197 void
mac_promisc_dispatch(mac_impl_t * mip,mblk_t * mp_chain,mac_client_impl_t * sender,boolean_t local)4198 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4199 mac_client_impl_t *sender, boolean_t local)
4200 {
4201 mac_promisc_impl_t *mpip;
4202 mac_cb_t *mcb;
4203 mblk_t *mp;
4204 boolean_t is_mcast, is_sender;
4205
4206 MAC_PROMISC_WALKER_INC(mip);
4207 for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4208 is_mcast = mac_is_mcast(mip, mp);
4209 /* send packet to interested callbacks */
4210 for (mcb = mip->mi_promisc_list; mcb != NULL;
4211 mcb = mcb->mcb_nextp) {
4212 mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4213 is_sender = (mpip->mpi_mcip == sender);
4214
4215 if (sender != NULL && mpip->mpi_rx_only)
4216 /*
4217 * This client doesn't want outbound packets.
4218 */
4219 continue;
4220
4221 if (sender == NULL && mpip->mpi_tx_only)
4222 /*
4223 * This client doesn't want inbound packets.
4224 */
4225 continue;
4226
4227 if (is_sender && mpip->mpi_no_tx_loop)
4228 /*
4229 * The sender doesn't want to receive
4230 * copies of the packets it sends.
4231 */
4232 continue;
4233
4234 /* this client doesn't need any packets (bridge) */
4235 if (mpip->mpi_fn == NULL)
4236 continue;
4237
4238 /*
4239 * For an ethernet MAC, don't displatch a multicast
4240 * packet to a non-PROMISC_ALL callbacks unless the VID
4241 * of the packet matches the VID of the client.
4242 */
4243 if (is_mcast &&
4244 mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4245 !mac_client_check_flow_vid(mpip->mpi_mcip,
4246 mac_ether_vid(mp)))
4247 continue;
4248
4249 if (is_sender ||
4250 mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4251 is_mcast) {
4252 mac_promisc_dispatch_one(mpip, mp, is_sender,
4253 local);
4254 }
4255 }
4256 }
4257 MAC_PROMISC_WALKER_DCR(mip);
4258 }
4259
4260 void
mac_promisc_client_dispatch(mac_client_impl_t * mcip,mblk_t * mp_chain)4261 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4262 {
4263 mac_impl_t *mip = mcip->mci_mip;
4264 mac_promisc_impl_t *mpip;
4265 boolean_t is_mcast;
4266 mblk_t *mp;
4267 mac_cb_t *mcb;
4268
4269 /*
4270 * The unicast packets for the MAC client still
4271 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4272 * promiscuous callbacks. The broadcast and multicast
4273 * packets were delivered from mac_rx().
4274 */
4275 MAC_PROMISC_WALKER_INC(mip);
4276 for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4277 is_mcast = mac_is_mcast(mip, mp);
4278 for (mcb = mcip->mci_promisc_list; mcb != NULL;
4279 mcb = mcb->mcb_nextp) {
4280 mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4281 if (!mpip->mpi_tx_only &&
4282 mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4283 !is_mcast) {
4284 mac_promisc_dispatch_one(mpip, mp, B_FALSE,
4285 B_FALSE);
4286 }
4287 }
4288 }
4289 MAC_PROMISC_WALKER_DCR(mip);
4290 }
4291
4292 /*
4293 * Return the margin value currently assigned to the specified MAC instance.
4294 */
4295 void
mac_margin_get(mac_handle_t mh,uint32_t * marginp)4296 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4297 {
4298 mac_impl_t *mip = (mac_impl_t *)mh;
4299
4300 rw_enter(&(mip->mi_rw_lock), RW_READER);
4301 *marginp = mip->mi_margin;
4302 rw_exit(&(mip->mi_rw_lock));
4303 }
4304
4305 /*
4306 * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4307 * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4308 * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4309 * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4310 * cannot disappear while we are accessing it.
4311 */
4312 typedef struct i_mac_info_state_s {
4313 const char *mi_name;
4314 mac_info_t *mi_infop;
4315 } i_mac_info_state_t;
4316
4317 /*ARGSUSED*/
4318 static uint_t
i_mac_info_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)4319 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4320 {
4321 i_mac_info_state_t *statep = arg;
4322 mac_impl_t *mip = (mac_impl_t *)val;
4323
4324 if (mip->mi_state_flags & MIS_DISABLED)
4325 return (MH_WALK_CONTINUE);
4326
4327 if (strcmp(statep->mi_name,
4328 ddi_driver_name(mip->mi_dip)) != 0)
4329 return (MH_WALK_CONTINUE);
4330
4331 statep->mi_infop = &mip->mi_info;
4332 return (MH_WALK_TERMINATE);
4333 }
4334
4335 boolean_t
mac_info_get(const char * name,mac_info_t * minfop)4336 mac_info_get(const char *name, mac_info_t *minfop)
4337 {
4338 i_mac_info_state_t state;
4339
4340 rw_enter(&i_mac_impl_lock, RW_READER);
4341 state.mi_name = name;
4342 state.mi_infop = NULL;
4343 mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4344 if (state.mi_infop == NULL) {
4345 rw_exit(&i_mac_impl_lock);
4346 return (B_FALSE);
4347 }
4348 *minfop = *state.mi_infop;
4349 rw_exit(&i_mac_impl_lock);
4350 return (B_TRUE);
4351 }
4352
4353 /*
4354 * To get the capabilities that MAC layer cares about, such as rings, factory
4355 * mac address, vnic or not, it should directly invoke this function. If the
4356 * link is part of a bridge, then the only "capability" it has is the inability
4357 * to do zero copy.
4358 */
4359 boolean_t
i_mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4360 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4361 {
4362 mac_impl_t *mip = (mac_impl_t *)mh;
4363
4364 if (mip->mi_bridge_link != NULL) {
4365 return (cap == MAC_CAPAB_NO_ZCOPY);
4366 } else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
4367 return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
4368 } else {
4369 return (B_FALSE);
4370 }
4371 }
4372
4373 /*
4374 * Capability query function. If number of active mac clients is greater than
4375 * 1, only limited capabilities can be advertised to the caller no matter the
4376 * driver has certain capability or not. Else, we query the driver to get the
4377 * capability.
4378 */
4379 boolean_t
mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4380 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4381 {
4382 mac_impl_t *mip = (mac_impl_t *)mh;
4383
4384 /*
4385 * Some capabilities are restricted when there are more than one active
4386 * clients on the MAC resource. The ones noted below are safe,
4387 * independent of that count.
4388 */
4389 if (mip->mi_nactiveclients > 1) {
4390 switch (cap) {
4391 case MAC_CAPAB_NO_ZCOPY:
4392 return (B_TRUE);
4393 case MAC_CAPAB_LEGACY:
4394 case MAC_CAPAB_HCKSUM:
4395 case MAC_CAPAB_LSO:
4396 case MAC_CAPAB_NO_NATIVEVLAN:
4397 break;
4398 default:
4399 return (B_FALSE);
4400 }
4401 }
4402
4403 /* else get capab from driver */
4404 return (i_mac_capab_get(mh, cap, cap_data));
4405 }
4406
4407 boolean_t
mac_sap_verify(mac_handle_t mh,uint32_t sap,uint32_t * bind_sap)4408 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4409 {
4410 mac_impl_t *mip = (mac_impl_t *)mh;
4411
4412 return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4413 mip->mi_pdata));
4414 }
4415
4416 mblk_t *
mac_header(mac_handle_t mh,const uint8_t * daddr,uint32_t sap,mblk_t * payload,size_t extra_len)4417 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4418 size_t extra_len)
4419 {
4420 mac_impl_t *mip = (mac_impl_t *)mh;
4421 const uint8_t *hdr_daddr;
4422
4423 /*
4424 * If the MAC is point-to-point with a fixed destination address, then
4425 * we must always use that destination in the MAC header.
4426 */
4427 hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
4428 return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4429 mip->mi_pdata, payload, extra_len));
4430 }
4431
4432 int
mac_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4433 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4434 {
4435 mac_impl_t *mip = (mac_impl_t *)mh;
4436
4437 return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4438 mhip));
4439 }
4440
4441 int
mac_vlan_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4442 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4443 {
4444 mac_impl_t *mip = (mac_impl_t *)mh;
4445 boolean_t is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
4446 int err = 0;
4447
4448 /*
4449 * Packets should always be at least 16 bit aligned.
4450 */
4451 ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
4452
4453 if ((err = mac_header_info(mh, mp, mhip)) != 0)
4454 return (err);
4455
4456 /*
4457 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
4458 * mac_header_info_t as returned by mac_header_info() is
4459 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
4460 */
4461 if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
4462 struct ether_vlan_header *evhp;
4463 uint16_t sap;
4464 mblk_t *tmp = NULL;
4465 size_t size;
4466
4467 size = sizeof (struct ether_vlan_header);
4468 if (MBLKL(mp) < size) {
4469 /*
4470 * Pullup the message in order to get the MAC header
4471 * infomation. Note that this is a read-only function,
4472 * we keep the input packet intact.
4473 */
4474 if ((tmp = msgpullup(mp, size)) == NULL)
4475 return (EINVAL);
4476
4477 mp = tmp;
4478 }
4479 evhp = (struct ether_vlan_header *)mp->b_rptr;
4480 sap = ntohs(evhp->ether_type);
4481 (void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
4482 mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
4483 mhip->mhi_tci = ntohs(evhp->ether_tci);
4484 mhip->mhi_istagged = B_TRUE;
4485 freemsg(tmp);
4486
4487 if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
4488 return (EINVAL);
4489 } else {
4490 mhip->mhi_istagged = B_FALSE;
4491 mhip->mhi_tci = 0;
4492 }
4493
4494 return (0);
4495 }
4496
4497 mblk_t *
mac_header_cook(mac_handle_t mh,mblk_t * mp)4498 mac_header_cook(mac_handle_t mh, mblk_t *mp)
4499 {
4500 mac_impl_t *mip = (mac_impl_t *)mh;
4501
4502 if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4503 if (DB_REF(mp) > 1) {
4504 mblk_t *newmp = copymsg(mp);
4505 if (newmp == NULL)
4506 return (NULL);
4507 freemsg(mp);
4508 mp = newmp;
4509 }
4510 return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4511 mip->mi_pdata));
4512 }
4513 return (mp);
4514 }
4515
4516 mblk_t *
mac_header_uncook(mac_handle_t mh,mblk_t * mp)4517 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4518 {
4519 mac_impl_t *mip = (mac_impl_t *)mh;
4520
4521 if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4522 if (DB_REF(mp) > 1) {
4523 mblk_t *newmp = copymsg(mp);
4524 if (newmp == NULL)
4525 return (NULL);
4526 freemsg(mp);
4527 mp = newmp;
4528 }
4529 return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4530 mip->mi_pdata));
4531 }
4532 return (mp);
4533 }
4534
4535 uint_t
mac_addr_len(mac_handle_t mh)4536 mac_addr_len(mac_handle_t mh)
4537 {
4538 mac_impl_t *mip = (mac_impl_t *)mh;
4539
4540 return (mip->mi_type->mt_addr_length);
4541 }
4542
4543 /* True if a MAC is a VNIC */
4544 boolean_t
mac_is_vnic(mac_handle_t mh)4545 mac_is_vnic(mac_handle_t mh)
4546 {
4547 return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
4548 }
4549
4550 boolean_t
mac_is_overlay(mac_handle_t mh)4551 mac_is_overlay(mac_handle_t mh)
4552 {
4553 return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
4554 }
4555
4556 mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)4557 mac_get_lower_mac_handle(mac_handle_t mh)
4558 {
4559 mac_impl_t *mip = (mac_impl_t *)mh;
4560
4561 ASSERT(mac_is_vnic(mh));
4562 return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4563 }
4564
4565 boolean_t
mac_is_vnic_primary(mac_handle_t mh)4566 mac_is_vnic_primary(mac_handle_t mh)
4567 {
4568 mac_impl_t *mip = (mac_impl_t *)mh;
4569
4570 ASSERT(mac_is_vnic(mh));
4571 return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4572 VNIC_MAC_ADDR_TYPE_PRIMARY);
4573 }
4574
4575 void
mac_update_resources(mac_resource_props_t * nmrp,mac_resource_props_t * cmrp,boolean_t is_user_flow)4576 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4577 boolean_t is_user_flow)
4578 {
4579 if (nmrp != NULL && cmrp != NULL) {
4580 if (nmrp->mrp_mask & MRP_PRIORITY) {
4581 if (nmrp->mrp_priority == MPL_RESET) {
4582 cmrp->mrp_mask &= ~MRP_PRIORITY;
4583 if (is_user_flow) {
4584 cmrp->mrp_priority =
4585 MPL_SUBFLOW_DEFAULT;
4586 } else {
4587 cmrp->mrp_priority = MPL_LINK_DEFAULT;
4588 }
4589 } else {
4590 cmrp->mrp_mask |= MRP_PRIORITY;
4591 cmrp->mrp_priority = nmrp->mrp_priority;
4592 }
4593 }
4594 if (nmrp->mrp_mask & MRP_MAXBW) {
4595 if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4596 cmrp->mrp_mask &= ~MRP_MAXBW;
4597 cmrp->mrp_maxbw = 0;
4598 } else {
4599 cmrp->mrp_mask |= MRP_MAXBW;
4600 cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4601 }
4602 }
4603 if (nmrp->mrp_mask & MRP_CPUS)
4604 MAC_COPY_CPUS(nmrp, cmrp);
4605
4606 if (nmrp->mrp_mask & MRP_POOL) {
4607 if (strlen(nmrp->mrp_pool) == 0) {
4608 cmrp->mrp_mask &= ~MRP_POOL;
4609 bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4610 } else {
4611 cmrp->mrp_mask |= MRP_POOL;
4612 (void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4613 sizeof (cmrp->mrp_pool));
4614 }
4615
4616 }
4617
4618 if (nmrp->mrp_mask & MRP_PROTECT)
4619 mac_protect_update(nmrp, cmrp);
4620
4621 /*
4622 * Update the rings specified.
4623 */
4624 if (nmrp->mrp_mask & MRP_RX_RINGS) {
4625 if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4626 cmrp->mrp_mask &= ~MRP_RX_RINGS;
4627 if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4628 cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4629 cmrp->mrp_nrxrings = 0;
4630 } else {
4631 cmrp->mrp_mask |= MRP_RX_RINGS;
4632 cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4633 }
4634 }
4635 if (nmrp->mrp_mask & MRP_TX_RINGS) {
4636 if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4637 cmrp->mrp_mask &= ~MRP_TX_RINGS;
4638 if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4639 cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4640 cmrp->mrp_ntxrings = 0;
4641 } else {
4642 cmrp->mrp_mask |= MRP_TX_RINGS;
4643 cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4644 }
4645 }
4646 if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4647 cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4648 else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4649 cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4650
4651 if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4652 cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4653 else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4654 cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4655 }
4656 }
4657
4658 /*
4659 * i_mac_set_resources:
4660 *
4661 * This routine associates properties with the primary MAC client of
4662 * the specified MAC instance.
4663 * - Cache the properties in mac_impl_t
4664 * - Apply the properties to the primary MAC client if exists
4665 */
4666 int
i_mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4667 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4668 {
4669 mac_impl_t *mip = (mac_impl_t *)mh;
4670 mac_client_impl_t *mcip;
4671 int err = 0;
4672 uint32_t resmask, newresmask;
4673 mac_resource_props_t *tmrp, *umrp;
4674
4675 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4676
4677 err = mac_validate_props(mip, mrp);
4678 if (err != 0)
4679 return (err);
4680
4681 umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4682 bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4683 resmask = umrp->mrp_mask;
4684 mac_update_resources(mrp, umrp, B_FALSE);
4685 newresmask = umrp->mrp_mask;
4686
4687 if (resmask == 0 && newresmask != 0) {
4688 /*
4689 * Bandwidth, priority, cpu or pool link properties configured,
4690 * must disable fastpath.
4691 */
4692 if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4693 kmem_free(umrp, sizeof (*umrp));
4694 return (err);
4695 }
4696 }
4697
4698 /*
4699 * Since bind_cpu may be modified by mac_client_set_resources()
4700 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4701 * This allows us to cache only user edits in mip.
4702 */
4703 tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4704 bcopy(mrp, tmrp, sizeof (*tmrp));
4705 mcip = mac_primary_client_handle(mip);
4706 if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4707 err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4708 } else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4709 mrp->mrp_mask & MRP_TX_RINGS)) {
4710 mac_client_impl_t *vmcip;
4711
4712 /*
4713 * If the primary is not up, we need to check if there
4714 * are any VLANs on this primary. If there are then
4715 * we need to set this property on the VLANs since
4716 * VLANs follow the primary they are based on. Just
4717 * look for the first VLAN and change its properties,
4718 * all the other VLANs should be in the same group.
4719 */
4720 for (vmcip = mip->mi_clients_list; vmcip != NULL;
4721 vmcip = vmcip->mci_client_next) {
4722 if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4723 mac_client_vid((mac_client_handle_t)vmcip) !=
4724 VLAN_ID_NONE) {
4725 break;
4726 }
4727 }
4728 if (vmcip != NULL) {
4729 mac_resource_props_t *omrp;
4730 mac_resource_props_t *vmrp;
4731
4732 omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4733 bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4734 /*
4735 * We dont' call mac_update_resources since we
4736 * want to take only the ring properties and
4737 * not all the properties that may have changed.
4738 */
4739 vmrp = MCIP_RESOURCE_PROPS(vmcip);
4740 if (mrp->mrp_mask & MRP_RX_RINGS) {
4741 if (mrp->mrp_mask & MRP_RINGS_RESET) {
4742 vmrp->mrp_mask &= ~MRP_RX_RINGS;
4743 if (vmrp->mrp_mask &
4744 MRP_RXRINGS_UNSPEC) {
4745 vmrp->mrp_mask &=
4746 ~MRP_RXRINGS_UNSPEC;
4747 }
4748 vmrp->mrp_nrxrings = 0;
4749 } else {
4750 vmrp->mrp_mask |= MRP_RX_RINGS;
4751 vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4752 }
4753 }
4754 if (mrp->mrp_mask & MRP_TX_RINGS) {
4755 if (mrp->mrp_mask & MRP_RINGS_RESET) {
4756 vmrp->mrp_mask &= ~MRP_TX_RINGS;
4757 if (vmrp->mrp_mask &
4758 MRP_TXRINGS_UNSPEC) {
4759 vmrp->mrp_mask &=
4760 ~MRP_TXRINGS_UNSPEC;
4761 }
4762 vmrp->mrp_ntxrings = 0;
4763 } else {
4764 vmrp->mrp_mask |= MRP_TX_RINGS;
4765 vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4766 }
4767 }
4768 if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4769 vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4770
4771 if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4772 vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4773
4774 if ((err = mac_client_set_rings_prop(vmcip, mrp,
4775 omrp)) != 0) {
4776 bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4777 sizeof (*omrp));
4778 } else {
4779 mac_set_prim_vlan_rings(mip, vmrp);
4780 }
4781 kmem_free(omrp, sizeof (*omrp));
4782 }
4783 }
4784
4785 /* Only update the values if mac_client_set_resources succeeded */
4786 if (err == 0) {
4787 bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
4788 /*
4789 * If bandwidth, priority or cpu link properties cleared,
4790 * renable fastpath.
4791 */
4792 if (resmask != 0 && newresmask == 0)
4793 mac_fastpath_enable((mac_handle_t)mip);
4794 } else if (resmask == 0 && newresmask != 0) {
4795 mac_fastpath_enable((mac_handle_t)mip);
4796 }
4797 kmem_free(tmrp, sizeof (*tmrp));
4798 kmem_free(umrp, sizeof (*umrp));
4799 return (err);
4800 }
4801
4802 int
mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4803 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4804 {
4805 int err;
4806
4807 i_mac_perim_enter((mac_impl_t *)mh);
4808 err = i_mac_set_resources(mh, mrp);
4809 i_mac_perim_exit((mac_impl_t *)mh);
4810 return (err);
4811 }
4812
4813 /*
4814 * Get the properties cached for the specified MAC instance.
4815 */
4816 void
mac_get_resources(mac_handle_t mh,mac_resource_props_t * mrp)4817 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4818 {
4819 mac_impl_t *mip = (mac_impl_t *)mh;
4820 mac_client_impl_t *mcip;
4821
4822 mcip = mac_primary_client_handle(mip);
4823 if (mcip != NULL) {
4824 mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4825 return;
4826 }
4827 bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
4828 }
4829
4830 /*
4831 * Get the effective properties from the primary client of the
4832 * specified MAC instance.
4833 */
4834 void
mac_get_effective_resources(mac_handle_t mh,mac_resource_props_t * mrp)4835 mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4836 {
4837 mac_impl_t *mip = (mac_impl_t *)mh;
4838 mac_client_impl_t *mcip;
4839
4840 mcip = mac_primary_client_handle(mip);
4841 if (mcip != NULL) {
4842 mac_client_get_effective_resources((mac_client_handle_t)mcip,
4843 mrp);
4844 return;
4845 }
4846 bzero(mrp, sizeof (mac_resource_props_t));
4847 }
4848
4849 int
mac_set_pvid(mac_handle_t mh,uint16_t pvid)4850 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
4851 {
4852 mac_impl_t *mip = (mac_impl_t *)mh;
4853 mac_client_impl_t *mcip;
4854 mac_unicast_impl_t *muip;
4855
4856 i_mac_perim_enter(mip);
4857 if (pvid != 0) {
4858 for (mcip = mip->mi_clients_list; mcip != NULL;
4859 mcip = mcip->mci_client_next) {
4860 for (muip = mcip->mci_unicast_list; muip != NULL;
4861 muip = muip->mui_next) {
4862 if (muip->mui_vid == pvid) {
4863 i_mac_perim_exit(mip);
4864 return (EBUSY);
4865 }
4866 }
4867 }
4868 }
4869 mip->mi_pvid = pvid;
4870 i_mac_perim_exit(mip);
4871 return (0);
4872 }
4873
4874 uint16_t
mac_get_pvid(mac_handle_t mh)4875 mac_get_pvid(mac_handle_t mh)
4876 {
4877 mac_impl_t *mip = (mac_impl_t *)mh;
4878
4879 return (mip->mi_pvid);
4880 }
4881
4882 uint32_t
mac_get_llimit(mac_handle_t mh)4883 mac_get_llimit(mac_handle_t mh)
4884 {
4885 mac_impl_t *mip = (mac_impl_t *)mh;
4886
4887 return (mip->mi_llimit);
4888 }
4889
4890 uint32_t
mac_get_ldecay(mac_handle_t mh)4891 mac_get_ldecay(mac_handle_t mh)
4892 {
4893 mac_impl_t *mip = (mac_impl_t *)mh;
4894
4895 return (mip->mi_ldecay);
4896 }
4897
4898 /*
4899 * Rename a mac client, its flow, and the kstat.
4900 */
4901 int
mac_rename_primary(mac_handle_t mh,const char * new_name)4902 mac_rename_primary(mac_handle_t mh, const char *new_name)
4903 {
4904 mac_impl_t *mip = (mac_impl_t *)mh;
4905 mac_client_impl_t *cur_clnt = NULL;
4906 flow_entry_t *fep;
4907
4908 i_mac_perim_enter(mip);
4909
4910 /*
4911 * VNICs: we need to change the sys flow name and
4912 * the associated flow kstat.
4913 */
4914 if (mip->mi_state_flags & MIS_IS_VNIC) {
4915 mac_client_impl_t *mcip = mac_vnic_lower(mip);
4916 ASSERT(new_name != NULL);
4917 mac_rename_flow_names(mcip, new_name);
4918 mac_stat_rename(mcip);
4919 goto done;
4920 }
4921 /*
4922 * This mac may itself be an aggr link, or it may have some client
4923 * which is an aggr port. For both cases, we need to change the
4924 * aggr port's mac client name, its flow name and the associated flow
4925 * kstat.
4926 */
4927 if (mip->mi_state_flags & MIS_IS_AGGR) {
4928 mac_capab_aggr_t aggr_cap;
4929 mac_rename_fn_t rename_fn;
4930 boolean_t ret;
4931
4932 ASSERT(new_name != NULL);
4933 ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4934 (void *)(&aggr_cap));
4935 ASSERT(ret == B_TRUE);
4936 rename_fn = aggr_cap.mca_rename_fn;
4937 rename_fn(new_name, mip->mi_driver);
4938 /*
4939 * The aggr's client name and kstat flow name will be
4940 * updated below, i.e. via mac_rename_flow_names.
4941 */
4942 }
4943
4944 for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4945 cur_clnt = cur_clnt->mci_client_next) {
4946 if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4947 if (new_name != NULL) {
4948 char *str_st = cur_clnt->mci_name;
4949 char *str_del = strchr(str_st, '-');
4950
4951 ASSERT(str_del != NULL);
4952 bzero(str_del + 1, MAXNAMELEN -
4953 (str_del - str_st + 1));
4954 bcopy(new_name, str_del + 1,
4955 strlen(new_name));
4956 }
4957 fep = cur_clnt->mci_flent;
4958 mac_rename_flow(fep, cur_clnt->mci_name);
4959 break;
4960 } else if (new_name != NULL &&
4961 cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4962 mac_rename_flow_names(cur_clnt, new_name);
4963 break;
4964 }
4965 }
4966
4967 /* Recreate kstats associated with aggr pseudo rings */
4968 if (mip->mi_state_flags & MIS_IS_AGGR)
4969 mac_pseudo_ring_stat_rename(mip);
4970
4971 done:
4972 i_mac_perim_exit(mip);
4973 return (0);
4974 }
4975
4976 /*
4977 * Rename the MAC client's flow names
4978 */
4979 static void
mac_rename_flow_names(mac_client_impl_t * mcip,const char * new_name)4980 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4981 {
4982 flow_entry_t *flent;
4983 uint16_t vid;
4984 char flowname[MAXFLOWNAMELEN];
4985 mac_impl_t *mip = mcip->mci_mip;
4986
4987 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4988
4989 /*
4990 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4991 * see a self-consistent value for mci_name
4992 */
4993 rw_enter(&mip->mi_rw_lock, RW_WRITER);
4994 (void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4995 rw_exit(&mip->mi_rw_lock);
4996
4997 mac_rename_flow(mcip->mci_flent, new_name);
4998
4999 if (mcip->mci_nflents == 1)
5000 return;
5001
5002 /*
5003 * We have to rename all the others too, no stats to destroy for
5004 * these.
5005 */
5006 for (flent = mcip->mci_flent_list; flent != NULL;
5007 flent = flent->fe_client_next) {
5008 if (flent != mcip->mci_flent) {
5009 vid = i_mac_flow_vid(flent);
5010 (void) sprintf(flowname, "%s%u", new_name, vid);
5011 mac_flow_set_name(flent, flowname);
5012 }
5013 }
5014 }
5015
5016
5017 /*
5018 * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
5019 * defined for the specified MAC client.
5020 */
5021 static void
mac_client_add_to_flow_list(mac_client_impl_t * mcip,flow_entry_t * flent)5022 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5023 {
5024 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5025 /*
5026 * The promisc Rx data path walks the mci_flent_list. Protect by
5027 * using mi_rw_lock
5028 */
5029 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5030
5031 mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5032
5033 /* Add it to the head */
5034 flent->fe_client_next = mcip->mci_flent_list;
5035 mcip->mci_flent_list = flent;
5036 mcip->mci_nflents++;
5037
5038 /*
5039 * Keep track of the number of non-zero VIDs addresses per MAC
5040 * client to avoid figuring it out in the data-path.
5041 */
5042 if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5043 mcip->mci_nvids++;
5044
5045 rw_exit(&mcip->mci_rw_lock);
5046 }
5047
5048 /*
5049 * Remove a flow entry from the MAC client's list.
5050 */
5051 static void
mac_client_remove_flow_from_list(mac_client_impl_t * mcip,flow_entry_t * flent)5052 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5053 {
5054 flow_entry_t *fe = mcip->mci_flent_list;
5055 flow_entry_t *prev_fe = NULL;
5056
5057 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5058 /*
5059 * The promisc Rx data path walks the mci_flent_list. Protect by
5060 * using mci_rw_lock
5061 */
5062 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5063 mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5064
5065 while ((fe != NULL) && (fe != flent)) {
5066 prev_fe = fe;
5067 fe = fe->fe_client_next;
5068 }
5069
5070 ASSERT(fe != NULL);
5071 if (prev_fe == NULL) {
5072 /* Deleting the first node */
5073 mcip->mci_flent_list = fe->fe_client_next;
5074 } else {
5075 prev_fe->fe_client_next = fe->fe_client_next;
5076 }
5077 mcip->mci_nflents--;
5078
5079 if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5080 mcip->mci_nvids--;
5081
5082 rw_exit(&mcip->mci_rw_lock);
5083 }
5084
5085 /*
5086 * Check if the given VID belongs to this MAC client.
5087 */
5088 boolean_t
mac_client_check_flow_vid(mac_client_impl_t * mcip,uint16_t vid)5089 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
5090 {
5091 flow_entry_t *flent;
5092 uint16_t mci_vid;
5093 uint32_t cache = mcip->mci_vidcache;
5094
5095 /*
5096 * In hopes of not having to touch the mci_rw_lock, check to see if
5097 * this vid matches our cached result.
5098 */
5099 if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
5100 return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
5101
5102 /* The mci_flent_list is protected by mci_rw_lock */
5103 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5104 for (flent = mcip->mci_flent_list; flent != NULL;
5105 flent = flent->fe_client_next) {
5106 mci_vid = i_mac_flow_vid(flent);
5107 if (vid == mci_vid) {
5108 mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
5109 rw_exit(&mcip->mci_rw_lock);
5110 return (B_TRUE);
5111 }
5112 }
5113
5114 mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
5115 rw_exit(&mcip->mci_rw_lock);
5116 return (B_FALSE);
5117 }
5118
5119 /*
5120 * Get the flow entry for the specified <MAC addr, VID> tuple.
5121 */
5122 static flow_entry_t *
mac_client_get_flow(mac_client_impl_t * mcip,mac_unicast_impl_t * muip)5123 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
5124 {
5125 mac_address_t *map = mcip->mci_unicast;
5126 flow_entry_t *flent;
5127 uint16_t vid;
5128 flow_desc_t flow_desc;
5129
5130 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5131
5132 mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5133 if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5134 return (NULL);
5135
5136 for (flent = mcip->mci_flent_list; flent != NULL;
5137 flent = flent->fe_client_next) {
5138 vid = i_mac_flow_vid(flent);
5139 if (vid == muip->mui_vid) {
5140 return (flent);
5141 }
5142 }
5143
5144 return (NULL);
5145 }
5146
5147 /*
5148 * Since mci_flent has the SRSs, when we want to remove it, we replace
5149 * the flow_desc_t in mci_flent with that of an existing flent and then
5150 * remove that flent instead of mci_flent.
5151 */
5152 static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t * mcip)5153 mac_client_swap_mciflent(mac_client_impl_t *mcip)
5154 {
5155 flow_entry_t *flent = mcip->mci_flent;
5156 flow_tab_t *ft = flent->fe_flow_tab;
5157 flow_entry_t *flent1;
5158 flow_desc_t fl_desc;
5159 char fl_name[MAXFLOWNAMELEN];
5160 int err;
5161
5162 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5163 ASSERT(mcip->mci_nflents > 1);
5164
5165 /* get the next flent following the primary flent */
5166 flent1 = mcip->mci_flent_list->fe_client_next;
5167 ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5168
5169 /*
5170 * Remove the flent from the flow table before updating the
5171 * flow descriptor as the hash depends on the flow descriptor.
5172 * This also helps incoming packet classification avoid having
5173 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5174 * flow table is done under the fe_lock so that log or stat functions
5175 * see a self-consistent fe_flow_desc. The name and desc are specific
5176 * to a flow, the rest are shared by all the clients, including
5177 * resource control etc.
5178 */
5179 mac_flow_remove(ft, flent, B_TRUE);
5180 mac_flow_remove(ft, flent1, B_TRUE);
5181
5182 bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5183 bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5184
5185 /* update the primary flow entry */
5186 mutex_enter(&flent->fe_lock);
5187 bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5188 sizeof (flow_desc_t));
5189 bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5190 mutex_exit(&flent->fe_lock);
5191
5192 /* update the flow entry that is to be freed */
5193 mutex_enter(&flent1->fe_lock);
5194 bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5195 bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5196 mutex_exit(&flent1->fe_lock);
5197
5198 /* now reinsert the flow entries in the table */
5199 err = mac_flow_add(ft, flent);
5200 ASSERT(err == 0);
5201
5202 err = mac_flow_add(ft, flent1);
5203 ASSERT(err == 0);
5204
5205 return (flent1);
5206 }
5207
5208 /*
5209 * Return whether there is only one flow entry associated with this
5210 * MAC client.
5211 */
5212 static boolean_t
mac_client_single_rcvr(mac_client_impl_t * mcip)5213 mac_client_single_rcvr(mac_client_impl_t *mcip)
5214 {
5215 return (mcip->mci_nflents == 1);
5216 }
5217
5218 int
mac_validate_props(mac_impl_t * mip,mac_resource_props_t * mrp)5219 mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5220 {
5221 boolean_t reset;
5222 uint32_t rings_needed;
5223 uint32_t rings_avail;
5224 mac_group_type_t gtype;
5225 mac_resource_props_t *mip_mrp;
5226
5227 if (mrp == NULL)
5228 return (0);
5229
5230 if (mrp->mrp_mask & MRP_PRIORITY) {
5231 mac_priority_level_t pri = mrp->mrp_priority;
5232
5233 if (pri < MPL_LOW || pri > MPL_RESET)
5234 return (EINVAL);
5235 }
5236
5237 if (mrp->mrp_mask & MRP_MAXBW) {
5238 uint64_t maxbw = mrp->mrp_maxbw;
5239
5240 if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5241 return (EINVAL);
5242 }
5243 if (mrp->mrp_mask & MRP_CPUS) {
5244 int i, j;
5245 mac_cpu_mode_t fanout;
5246
5247 if (mrp->mrp_ncpus > ncpus)
5248 return (EINVAL);
5249
5250 for (i = 0; i < mrp->mrp_ncpus; i++) {
5251 for (j = 0; j < mrp->mrp_ncpus; j++) {
5252 if (i != j &&
5253 mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5254 return (EINVAL);
5255 }
5256 }
5257 }
5258
5259 for (i = 0; i < mrp->mrp_ncpus; i++) {
5260 cpu_t *cp;
5261 int rv;
5262
5263 mutex_enter(&cpu_lock);
5264 cp = cpu_get(mrp->mrp_cpu[i]);
5265 if (cp != NULL)
5266 rv = cpu_is_online(cp);
5267 else
5268 rv = 0;
5269 mutex_exit(&cpu_lock);
5270 if (rv == 0)
5271 return (EINVAL);
5272 }
5273
5274 fanout = mrp->mrp_fanout_mode;
5275 if (fanout < 0 || fanout > MCM_CPUS)
5276 return (EINVAL);
5277 }
5278
5279 if (mrp->mrp_mask & MRP_PROTECT) {
5280 int err = mac_protect_validate(mrp);
5281 if (err != 0)
5282 return (err);
5283 }
5284
5285 if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
5286 !(mrp->mrp_mask & MRP_TX_RINGS)) {
5287 return (0);
5288 }
5289
5290 /*
5291 * mip will be null when we come from mac_flow_create or
5292 * mac_link_flow_modify. In the latter case it is a user flow,
5293 * for which we don't support rings. In the former we would
5294 * have validated the props beforehand (i_mac_unicast_add ->
5295 * mac_client_set_resources -> validate for the primary and
5296 * vnic_dev_create -> mac_client_set_resources -> validate for
5297 * a vnic.
5298 */
5299 if (mip == NULL)
5300 return (0);
5301
5302 /*
5303 * We don't support setting rings property for a VNIC that is using a
5304 * primary address (VLAN)
5305 */
5306 if ((mip->mi_state_flags & MIS_IS_VNIC) &&
5307 mac_is_vnic_primary((mac_handle_t)mip)) {
5308 return (ENOTSUP);
5309 }
5310
5311 mip_mrp = &mip->mi_resource_props;
5312 /*
5313 * The rings property should be validated against the NICs
5314 * resources
5315 */
5316 if (mip->mi_state_flags & MIS_IS_VNIC)
5317 mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
5318
5319 reset = mrp->mrp_mask & MRP_RINGS_RESET;
5320 /*
5321 * If groups are not supported, return error.
5322 */
5323 if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
5324 ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
5325 return (EINVAL);
5326 }
5327 /*
5328 * If we are just resetting, there is no validation needed.
5329 */
5330 if (reset)
5331 return (0);
5332
5333 if (mrp->mrp_mask & MRP_RX_RINGS) {
5334 rings_needed = mrp->mrp_nrxrings;
5335 /*
5336 * We just want to check if the number of additional
5337 * rings requested is available.
5338 */
5339 if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
5340 if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
5341 /* Just check for the additional rings */
5342 rings_needed -= mip_mrp->mrp_nrxrings;
5343 else
5344 /* We are not asking for additional rings */
5345 rings_needed = 0;
5346 }
5347 rings_avail = mip->mi_rxrings_avail;
5348 gtype = mip->mi_rx_group_type;
5349 } else {
5350 rings_needed = mrp->mrp_ntxrings;
5351 /* Similarly for the TX rings */
5352 if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
5353 if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
5354 /* Just check for the additional rings */
5355 rings_needed -= mip_mrp->mrp_ntxrings;
5356 else
5357 /* We are not asking for additional rings */
5358 rings_needed = 0;
5359 }
5360 rings_avail = mip->mi_txrings_avail;
5361 gtype = mip->mi_tx_group_type;
5362 }
5363
5364 /* Error if the group is dynamic .. */
5365 if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5366 /*
5367 * .. and rings specified are more than available.
5368 */
5369 if (rings_needed > rings_avail)
5370 return (EINVAL);
5371 } else {
5372 /*
5373 * OR group is static and we have specified some rings.
5374 */
5375 if (rings_needed > 0)
5376 return (EINVAL);
5377 }
5378 return (0);
5379 }
5380
5381 /*
5382 * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5383 * underlying physical link is down. This is to allow MAC clients to
5384 * communicate with other clients.
5385 */
5386 void
mac_virtual_link_update(mac_impl_t * mip)5387 mac_virtual_link_update(mac_impl_t *mip)
5388 {
5389 if (mip->mi_linkstate != LINK_STATE_UP)
5390 i_mac_notify(mip, MAC_NOTE_LINK);
5391 }
5392
5393 /*
5394 * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5395 * mac handle in the client.
5396 */
5397 void
mac_set_upper_mac(mac_client_handle_t mch,mac_handle_t mh,mac_resource_props_t * mrp)5398 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5399 mac_resource_props_t *mrp)
5400 {
5401 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5402 mac_impl_t *mip = (mac_impl_t *)mh;
5403
5404 mcip->mci_upper_mip = mip;
5405 /* If there are any properties, copy it over too */
5406 if (mrp != NULL) {
5407 bcopy(mrp, &mip->mi_resource_props,
5408 sizeof (mac_resource_props_t));
5409 }
5410 }
5411
5412 /*
5413 * Mark the mac as being used exclusively by the single mac client that is
5414 * doing some control operation on this mac. No further opens of this mac
5415 * will be allowed until this client calls mac_unmark_exclusive. The mac
5416 * client calling this function must already be in the mac perimeter
5417 */
5418 int
mac_mark_exclusive(mac_handle_t mh)5419 mac_mark_exclusive(mac_handle_t mh)
5420 {
5421 mac_impl_t *mip = (mac_impl_t *)mh;
5422
5423 ASSERT(MAC_PERIM_HELD(mh));
5424 /*
5425 * Look up its entry in the global hash table.
5426 */
5427 rw_enter(&i_mac_impl_lock, RW_WRITER);
5428 if (mip->mi_state_flags & MIS_DISABLED) {
5429 rw_exit(&i_mac_impl_lock);
5430 return (ENOENT);
5431 }
5432
5433 /*
5434 * A reference to mac is held even if the link is not plumbed.
5435 * In i_dls_link_create() we open the MAC interface and hold the
5436 * reference. There is an additional reference for the mac_open
5437 * done in acquiring the mac perimeter
5438 */
5439 if (mip->mi_ref != 2) {
5440 rw_exit(&i_mac_impl_lock);
5441 return (EBUSY);
5442 }
5443
5444 ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5445 mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5446 rw_exit(&i_mac_impl_lock);
5447 return (0);
5448 }
5449
5450 void
mac_unmark_exclusive(mac_handle_t mh)5451 mac_unmark_exclusive(mac_handle_t mh)
5452 {
5453 mac_impl_t *mip = (mac_impl_t *)mh;
5454
5455 ASSERT(MAC_PERIM_HELD(mh));
5456
5457 rw_enter(&i_mac_impl_lock, RW_WRITER);
5458 /* 1 for the creation and another for the perimeter */
5459 ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5460 mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5461 rw_exit(&i_mac_impl_lock);
5462 }
5463
5464 /*
5465 * Set the MTU for the specified MAC.
5466 */
5467 int
mac_set_mtu(mac_handle_t mh,uint_t new_mtu,uint_t * old_mtu_arg)5468 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5469 {
5470 mac_impl_t *mip = (mac_impl_t *)mh;
5471 uint_t old_mtu;
5472 int rv = 0;
5473
5474 i_mac_perim_enter(mip);
5475
5476 if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5477 rv = ENOTSUP;
5478 goto bail;
5479 }
5480
5481 old_mtu = mip->mi_sdu_max;
5482
5483 if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5484 rv = EINVAL;
5485 goto bail;
5486 }
5487
5488 rw_enter(&mip->mi_rw_lock, RW_READER);
5489 if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5490 rv = EBUSY;
5491 rw_exit(&mip->mi_rw_lock);
5492 goto bail;
5493 }
5494 rw_exit(&mip->mi_rw_lock);
5495
5496 if (old_mtu != new_mtu) {
5497 rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5498 "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5499 if (rv != 0)
5500 goto bail;
5501 rv = mac_maxsdu_update(mh, new_mtu);
5502 ASSERT(rv == 0);
5503 }
5504
5505 bail:
5506 i_mac_perim_exit(mip);
5507
5508 if (rv == 0 && old_mtu_arg != NULL)
5509 *old_mtu_arg = old_mtu;
5510 return (rv);
5511 }
5512
5513 /*
5514 * Return the RX h/w information for the group indexed by grp_num.
5515 */
5516 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)5517 mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5518 uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5519 char *clnts_name)
5520 {
5521 mac_impl_t *mip = (mac_impl_t *)mh;
5522 mac_grp_client_t *mcip;
5523 uint_t i = 0, index = 0;
5524 mac_ring_t *ring;
5525
5526 /* Revisit when we implement fully dynamic group allocation */
5527 ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5528
5529 rw_enter(&mip->mi_rw_lock, RW_READER);
5530 *grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5531 *type = mip->mi_rx_groups[grp_index].mrg_type;
5532 *n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5533 ring = mip->mi_rx_groups[grp_index].mrg_rings;
5534 for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5535 index++) {
5536 rings[index] = ring->mr_index;
5537 ring = ring->mr_next;
5538 }
5539 /* Assuming the 1st is the default group */
5540 index = 0;
5541 if (grp_index == 0) {
5542 (void) strlcpy(clnts_name, "<default,mcast>,",
5543 MAXCLIENTNAMELEN);
5544 index += strlen("<default,mcast>,");
5545 }
5546 for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5547 mcip = mcip->mgc_next) {
5548 int name_len = strlen(mcip->mgc_client->mci_name);
5549
5550 /*
5551 * MAXCLIENTNAMELEN is the buffer size reserved for client
5552 * names.
5553 * XXXX Formating the client name string needs to be moved
5554 * to user land when fixing the size of dhi_clnts in
5555 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5556 * dhi_clntsin instead of MAXCLIENTNAMELEN
5557 */
5558 if (index + name_len >= MAXCLIENTNAMELEN) {
5559 index = MAXCLIENTNAMELEN;
5560 break;
5561 }
5562 bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5563 name_len);
5564 index += name_len;
5565 clnts_name[index++] = ',';
5566 i++;
5567 }
5568
5569 /* Get rid of the last , */
5570 if (index > 0)
5571 clnts_name[index - 1] = '\0';
5572 *n_clnts = i;
5573 rw_exit(&mip->mi_rw_lock);
5574 }
5575
5576 /*
5577 * Return the TX h/w information for the group indexed by grp_num.
5578 */
5579 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)5580 mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5581 uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5582 char *clnts_name)
5583 {
5584 mac_impl_t *mip = (mac_impl_t *)mh;
5585 mac_grp_client_t *mcip;
5586 uint_t i = 0, index = 0;
5587 mac_ring_t *ring;
5588
5589 /* Revisit when we implement fully dynamic group allocation */
5590 ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5591
5592 rw_enter(&mip->mi_rw_lock, RW_READER);
5593 *grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5594 mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5595 *type = mip->mi_tx_groups[grp_index].mrg_type;
5596 *n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5597 ring = mip->mi_tx_groups[grp_index].mrg_rings;
5598 for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5599 index++) {
5600 rings[index] = ring->mr_index;
5601 ring = ring->mr_next;
5602 }
5603 index = 0;
5604 /* Default group has an index of -1 */
5605 if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5606 (void) strlcpy(clnts_name, "<default>,",
5607 MAXCLIENTNAMELEN);
5608 index += strlen("<default>,");
5609 }
5610 for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5611 mcip = mcip->mgc_next) {
5612 int name_len = strlen(mcip->mgc_client->mci_name);
5613
5614 /*
5615 * MAXCLIENTNAMELEN is the buffer size reserved for client
5616 * names.
5617 * XXXX Formating the client name string needs to be moved
5618 * to user land when fixing the size of dhi_clnts in
5619 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5620 * dhi_clntsin instead of MAXCLIENTNAMELEN
5621 */
5622 if (index + name_len >= MAXCLIENTNAMELEN) {
5623 index = MAXCLIENTNAMELEN;
5624 break;
5625 }
5626 bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5627 name_len);
5628 index += name_len;
5629 clnts_name[index++] = ',';
5630 i++;
5631 }
5632
5633 /* Get rid of the last , */
5634 if (index > 0)
5635 clnts_name[index - 1] = '\0';
5636 *n_clnts = i;
5637 rw_exit(&mip->mi_rw_lock);
5638 }
5639
5640 /*
5641 * Return the group count for RX or TX.
5642 */
5643 uint_t
mac_hwgrp_num(mac_handle_t mh,int type)5644 mac_hwgrp_num(mac_handle_t mh, int type)
5645 {
5646 mac_impl_t *mip = (mac_impl_t *)mh;
5647
5648 /*
5649 * Return the Rx and Tx group count; for the Tx we need to
5650 * include the default too.
5651 */
5652 return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5653 mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5654 }
5655
5656 /*
5657 * The total number of free TX rings for this MAC.
5658 */
5659 uint_t
mac_txavail_get(mac_handle_t mh)5660 mac_txavail_get(mac_handle_t mh)
5661 {
5662 mac_impl_t *mip = (mac_impl_t *)mh;
5663
5664 return (mip->mi_txrings_avail);
5665 }
5666
5667 /*
5668 * The total number of free RX rings for this MAC.
5669 */
5670 uint_t
mac_rxavail_get(mac_handle_t mh)5671 mac_rxavail_get(mac_handle_t mh)
5672 {
5673 mac_impl_t *mip = (mac_impl_t *)mh;
5674
5675 return (mip->mi_rxrings_avail);
5676 }
5677
5678 /*
5679 * The total number of reserved RX rings on this MAC.
5680 */
5681 uint_t
mac_rxrsvd_get(mac_handle_t mh)5682 mac_rxrsvd_get(mac_handle_t mh)
5683 {
5684 mac_impl_t *mip = (mac_impl_t *)mh;
5685
5686 return (mip->mi_rxrings_rsvd);
5687 }
5688
5689 /*
5690 * The total number of reserved TX rings on this MAC.
5691 */
5692 uint_t
mac_txrsvd_get(mac_handle_t mh)5693 mac_txrsvd_get(mac_handle_t mh)
5694 {
5695 mac_impl_t *mip = (mac_impl_t *)mh;
5696
5697 return (mip->mi_txrings_rsvd);
5698 }
5699
5700 /*
5701 * Total number of free RX groups on this MAC.
5702 */
5703 uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)5704 mac_rxhwlnksavail_get(mac_handle_t mh)
5705 {
5706 mac_impl_t *mip = (mac_impl_t *)mh;
5707
5708 return (mip->mi_rxhwclnt_avail);
5709 }
5710
5711 /*
5712 * Total number of RX groups reserved on this MAC.
5713 */
5714 uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)5715 mac_rxhwlnksrsvd_get(mac_handle_t mh)
5716 {
5717 mac_impl_t *mip = (mac_impl_t *)mh;
5718
5719 return (mip->mi_rxhwclnt_used);
5720 }
5721
5722 /*
5723 * Total number of free TX groups on this MAC.
5724 */
5725 uint_t
mac_txhwlnksavail_get(mac_handle_t mh)5726 mac_txhwlnksavail_get(mac_handle_t mh)
5727 {
5728 mac_impl_t *mip = (mac_impl_t *)mh;
5729
5730 return (mip->mi_txhwclnt_avail);
5731 }
5732
5733 /*
5734 * Total number of TX groups reserved on this MAC.
5735 */
5736 uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)5737 mac_txhwlnksrsvd_get(mac_handle_t mh)
5738 {
5739 mac_impl_t *mip = (mac_impl_t *)mh;
5740
5741 return (mip->mi_txhwclnt_used);
5742 }
5743
5744 /*
5745 * Initialize the rings property for a mac client. A non-0 value for
5746 * rxring or txring specifies the number of rings required, a value
5747 * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5748 * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5749 * means the system can decide whether it can give any rings or not.
5750 */
5751 void
mac_client_set_rings(mac_client_handle_t mch,int rxrings,int txrings)5752 mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5753 {
5754 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5755 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
5756
5757 if (rxrings != MAC_RXRINGS_DONTCARE) {
5758 mrp->mrp_mask |= MRP_RX_RINGS;
5759 mrp->mrp_nrxrings = rxrings;
5760 }
5761
5762 if (txrings != MAC_TXRINGS_DONTCARE) {
5763 mrp->mrp_mask |= MRP_TX_RINGS;
5764 mrp->mrp_ntxrings = txrings;
5765 }
5766 }
5767
5768 boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)5769 mac_get_promisc_filtered(mac_client_handle_t mch)
5770 {
5771 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5772
5773 return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
5774 }
5775
5776 void
mac_set_promisc_filtered(mac_client_handle_t mch,boolean_t enable)5777 mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
5778 {
5779 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5780
5781 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5782 if (enable)
5783 mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
5784 else
5785 mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
5786 }
5787