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 i_mac_perim_enter(mip);
3362
3363 if ((rc = mac_start((mac_handle_t)mip)) != 0) {
3364 i_mac_perim_exit(mip);
3365 return (rc);
3366 }
3367
3368 if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
3369 type == MAC_CLIENT_PROMISC_ALL &&
3370 (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
3371 /*
3372 * The function is being invoked by the upper MAC client
3373 * of a VNIC. The VNIC should only see the traffic
3374 * it is entitled to.
3375 */
3376 type = MAC_CLIENT_PROMISC_FILTERED;
3377 }
3378
3379
3380 /*
3381 * Turn on promiscuous mode for the underlying NIC.
3382 * This is needed even for filtered callbacks which
3383 * expect to receive all multicast traffic on the wire.
3384 *
3385 * Physical promiscuous mode should not be turned on if
3386 * MAC_PROMISC_FLAGS_NO_PHYS is set.
3387 */
3388 if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
3389 if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
3390 mac_stop((mac_handle_t)mip);
3391 i_mac_perim_exit(mip);
3392 return (rc);
3393 }
3394 }
3395
3396 mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
3397
3398 mpip->mpi_type = type;
3399 mpip->mpi_fn = fn;
3400 mpip->mpi_arg = arg;
3401 mpip->mpi_mcip = mcip;
3402 mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
3403 mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
3404 mpip->mpi_strip_vlan_tag =
3405 ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
3406 mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
3407
3408 mcbi = &mip->mi_promisc_cb_info;
3409 mutex_enter(mcbi->mcbi_lockp);
3410
3411 mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
3412 &mpip->mpi_mci_link);
3413 mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
3414 &mpip->mpi_mi_link);
3415
3416 mutex_exit(mcbi->mcbi_lockp);
3417
3418 *mphp = (mac_promisc_handle_t)mpip;
3419
3420 if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3421 mac_impl_t *umip = mcip->mci_upper_mip;
3422
3423 ASSERT(umip != NULL);
3424 mac_vnic_secondary_update(umip);
3425 }
3426
3427 i_mac_perim_exit(mip);
3428
3429 return (0);
3430 }
3431
3432 /*
3433 * Remove a multicast address previously aded through mac_promisc_add().
3434 */
3435 void
mac_promisc_remove(mac_promisc_handle_t mph)3436 mac_promisc_remove(mac_promisc_handle_t mph)
3437 {
3438 mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
3439 mac_client_impl_t *mcip = mpip->mpi_mcip;
3440 mac_impl_t *mip = mcip->mci_mip;
3441 mac_cb_info_t *mcbi;
3442 int rv;
3443
3444 i_mac_perim_enter(mip);
3445
3446 /*
3447 * Even if the device can't be reset into normal mode, we still
3448 * need to clear the client promisc callbacks. The client may want
3449 * to close the mac end point and we can't have stale callbacks.
3450 */
3451 if (!(mpip->mpi_no_phys)) {
3452 if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
3453 cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
3454 " mode because of error 0x%x", mip->mi_name, rv);
3455 }
3456 }
3457 mcbi = &mip->mi_promisc_cb_info;
3458 mutex_enter(mcbi->mcbi_lockp);
3459 if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
3460 &mpip->mpi_mi_link)) {
3461 VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
3462 &mcip->mci_promisc_list, &mpip->mpi_mci_link));
3463 kmem_cache_free(mac_promisc_impl_cache, mpip);
3464 } else {
3465 mac_callback_remove_wait(&mip->mi_promisc_cb_info);
3466 }
3467
3468 if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3469 mac_impl_t *umip = mcip->mci_upper_mip;
3470
3471 ASSERT(umip != NULL);
3472 mac_vnic_secondary_update(umip);
3473 }
3474
3475 mutex_exit(mcbi->mcbi_lockp);
3476 mac_stop((mac_handle_t)mip);
3477
3478 i_mac_perim_exit(mip);
3479 }
3480
3481 /*
3482 * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
3483 * that a control operation wants to quiesce the Tx data flow in which case
3484 * we return an error. Holding any of the per cpu locks ensures that the
3485 * mci_tx_flag won't change.
3486 *
3487 * 'CPU' must be accessed just once and used to compute the index into the
3488 * percpu array, and that index must be used for the entire duration of the
3489 * packet send operation. Note that the thread may be preempted and run on
3490 * another cpu any time and so we can't use 'CPU' more than once for the
3491 * operation.
3492 */
3493 #define MAC_TX_TRY_HOLD(mcip, mytx, error) \
3494 { \
3495 (error) = 0; \
3496 (mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
3497 mutex_enter(&(mytx)->pcpu_tx_lock); \
3498 if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) { \
3499 (mytx)->pcpu_tx_refcnt++; \
3500 } else { \
3501 (error) = -1; \
3502 } \
3503 mutex_exit(&(mytx)->pcpu_tx_lock); \
3504 }
3505
3506 /*
3507 * Release the reference. If needed, signal any control operation waiting
3508 * for Tx quiescence. The wait and signal are always done using the
3509 * mci_tx_pcpu[0]'s lock
3510 */
3511 #define MAC_TX_RELE(mcip, mytx) { \
3512 mutex_enter(&(mytx)->pcpu_tx_lock); \
3513 if (--(mytx)->pcpu_tx_refcnt == 0 && \
3514 (mcip)->mci_tx_flag & MCI_TX_QUIESCE) { \
3515 mutex_exit(&(mytx)->pcpu_tx_lock); \
3516 mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock); \
3517 cv_signal(&(mcip)->mci_tx_cv); \
3518 mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock); \
3519 } else { \
3520 mutex_exit(&(mytx)->pcpu_tx_lock); \
3521 } \
3522 }
3523
3524 /*
3525 * Send function invoked by MAC clients.
3526 */
3527 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)3528 mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
3529 uint16_t flag, mblk_t **ret_mp)
3530 {
3531 mac_tx_cookie_t cookie = 0;
3532 int error;
3533 mac_tx_percpu_t *mytx;
3534 mac_soft_ring_set_t *srs;
3535 flow_entry_t *flent;
3536 boolean_t is_subflow = B_FALSE;
3537 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3538 mac_impl_t *mip = mcip->mci_mip;
3539 mac_srs_tx_t *srs_tx;
3540
3541 /*
3542 * Check whether the active Tx threads count is bumped already.
3543 */
3544 if (!(flag & MAC_TX_NO_HOLD)) {
3545 MAC_TX_TRY_HOLD(mcip, mytx, error);
3546 if (error != 0) {
3547 freemsgchain(mp_chain);
3548 return (0);
3549 }
3550 }
3551
3552 /*
3553 * If mac protection is enabled, only the permissible packets will be
3554 * returned by mac_protect_check().
3555 */
3556 if ((mcip->mci_flent->
3557 fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
3558 (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
3559 goto done;
3560
3561 if (mcip->mci_subflow_tab != NULL &&
3562 mcip->mci_subflow_tab->ft_flow_count > 0 &&
3563 mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
3564 FLOW_OUTBOUND, &flent) == 0) {
3565 /*
3566 * The main assumption here is that if in the event
3567 * we get a chain, all the packets will be classified
3568 * to the same Flow/SRS. If this changes for any
3569 * reason, the following logic should change as well.
3570 * I suppose the fanout_hint also assumes this .
3571 */
3572 ASSERT(flent != NULL);
3573 is_subflow = B_TRUE;
3574 } else {
3575 flent = mcip->mci_flent;
3576 }
3577
3578 srs = flent->fe_tx_srs;
3579 /*
3580 * This is to avoid panics with PF_PACKET that can call mac_tx()
3581 * against an interface that is not capable of sending. A rewrite
3582 * of the mac datapath is required to remove this limitation.
3583 */
3584 if (srs == NULL) {
3585 freemsgchain(mp_chain);
3586 goto done;
3587 }
3588
3589 srs_tx = &srs->srs_tx;
3590 if (srs_tx->st_mode == SRS_TX_DEFAULT &&
3591 (srs->srs_state & SRS_ENQUEUED) == 0 &&
3592 mip->mi_nactiveclients == 1 &&
3593 mp_chain->b_next == NULL &&
3594 (DB_CKSUMFLAGS(mp_chain) & HW_LSO) == 0) {
3595 uint64_t obytes;
3596
3597 /*
3598 * Since dls always opens the underlying MAC, nclients equals
3599 * to 1 means that the only active client is dls itself acting
3600 * as a primary client of the MAC instance. Since dls will not
3601 * send tagged packets in that case, and dls is trusted to send
3602 * packets for its allowed VLAN(s), the VLAN tag insertion and
3603 * check is required only if nclients is greater than 1.
3604 */
3605 if (mip->mi_nclients > 1) {
3606 if (MAC_VID_CHECK_NEEDED(mcip)) {
3607 int err = 0;
3608
3609 MAC_VID_CHECK(mcip, mp_chain, err);
3610 if (err != 0) {
3611 freemsg(mp_chain);
3612 mcip->mci_misc_stat.mms_txerrors++;
3613 goto done;
3614 }
3615 }
3616 if (MAC_TAG_NEEDED(mcip)) {
3617 mp_chain = mac_add_vlan_tag(mp_chain, 0,
3618 mac_client_vid(mch));
3619 if (mp_chain == NULL) {
3620 mcip->mci_misc_stat.mms_txerrors++;
3621 goto done;
3622 }
3623 }
3624 }
3625
3626 obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
3627 msgdsize(mp_chain));
3628
3629 mp_chain = mac_provider_tx(mip, srs_tx->st_arg2, mp_chain,
3630 mcip);
3631
3632 if (mp_chain == NULL) {
3633 cookie = 0;
3634 SRS_TX_STAT_UPDATE(srs, opackets, 1);
3635 SRS_TX_STAT_UPDATE(srs, obytes, obytes);
3636 } else {
3637 mutex_enter(&srs->srs_lock);
3638 cookie = mac_tx_srs_no_desc(srs, mp_chain,
3639 flag, ret_mp);
3640 mutex_exit(&srs->srs_lock);
3641 }
3642 } else {
3643 mblk_t *mp = mp_chain;
3644 mblk_t *new_head = NULL;
3645 mblk_t *new_tail = NULL;
3646
3647 /*
3648 * There are occasions where the packets arriving here
3649 * may request hardware offloads that are not
3650 * available from the underlying MAC provider. This
3651 * currently only happens when a packet is sent across
3652 * the MAC-loopback path of one MAC and then forwarded
3653 * (via IP) to another MAC that lacks one or more of
3654 * the hardware offloads provided by the first one.
3655 * However, in the future, we may choose to pretend
3656 * all MAC providers support all offloads, performing
3657 * emulation on Tx as needed.
3658 *
3659 * We iterate each mblk in-turn, emulating hardware
3660 * offloads as required. From this process, we create
3661 * a new chain. The new chain may be the same as the
3662 * original chain (no hardware emulation needed), a
3663 * collection of new mblks (hardware emulation
3664 * needed), or a mix. At this point, the chain is safe
3665 * for consumption by the underlying MAC provider and
3666 * is passed down to the SRS.
3667 */
3668 while (mp != NULL) {
3669 mblk_t *next = mp->b_next;
3670 mblk_t *tail = NULL;
3671 const uint16_t needed =
3672 (DB_CKSUMFLAGS(mp) ^ mip->mi_tx_cksum_flags) &
3673 DB_CKSUMFLAGS(mp);
3674
3675 mp->b_next = NULL;
3676
3677 if ((needed & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) {
3678 mac_emul_t emul = 0;
3679
3680 if (needed & HCK_IPV4_HDRCKSUM)
3681 emul |= MAC_IPCKSUM_EMUL;
3682 if (needed & (HCK_PARTIALCKSUM | HCK_FULLCKSUM))
3683 emul |= MAC_HWCKSUM_EMUL;
3684 if (needed & HW_LSO)
3685 emul = MAC_LSO_EMUL;
3686
3687 mac_hw_emul(&mp, &tail, NULL, emul);
3688
3689 if (mp == NULL) {
3690 mp = next;
3691 continue;
3692 }
3693 }
3694
3695 if (new_head == NULL) {
3696 new_head = mp;
3697 } else {
3698 new_tail->b_next = mp;
3699 }
3700
3701 new_tail = (tail == NULL) ? mp : tail;
3702 mp = next;
3703 }
3704
3705 if (new_head == NULL) {
3706 cookie = 0;
3707 goto done;
3708 }
3709
3710 cookie = srs_tx->st_func(srs, new_head, hint, flag, ret_mp);
3711 }
3712
3713 done:
3714 if (is_subflow)
3715 FLOW_REFRELE(flent);
3716
3717 if (!(flag & MAC_TX_NO_HOLD))
3718 MAC_TX_RELE(mcip, mytx);
3719
3720 return (cookie);
3721 }
3722
3723 /*
3724 * mac_tx_is_blocked
3725 *
3726 * Given a cookie, it returns if the ring identified by the cookie is
3727 * flow-controlled or not. If NULL is passed in place of a cookie,
3728 * then it finds out if any of the underlying rings belonging to the
3729 * SRS is flow controlled or not and returns that status.
3730 */
3731 /* ARGSUSED */
3732 boolean_t
mac_tx_is_flow_blocked(mac_client_handle_t mch,mac_tx_cookie_t cookie)3733 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
3734 {
3735 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3736 mac_soft_ring_set_t *mac_srs;
3737 mac_soft_ring_t *sringp;
3738 boolean_t blocked = B_FALSE;
3739 mac_tx_percpu_t *mytx;
3740 int err;
3741 int i;
3742
3743 /*
3744 * Bump the reference count so that mac_srs won't be deleted.
3745 * If the client is currently quiesced and we failed to bump
3746 * the reference, return B_TRUE so that flow control stays
3747 * as enabled.
3748 *
3749 * Flow control will then be disabled once the client is no
3750 * longer quiesced.
3751 */
3752 MAC_TX_TRY_HOLD(mcip, mytx, err);
3753 if (err != 0)
3754 return (B_TRUE);
3755
3756 if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
3757 MAC_TX_RELE(mcip, mytx);
3758 return (B_FALSE);
3759 }
3760
3761 mutex_enter(&mac_srs->srs_lock);
3762 /*
3763 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
3764 * softring (s_ring_state) will have the HIWAT set. This is
3765 * the multiple Tx ring flow control case. For all other
3766 * case, SRS (srs_state) will store the condition.
3767 */
3768 if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3769 mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
3770 if (cookie != 0) {
3771 sringp = (mac_soft_ring_t *)cookie;
3772 mutex_enter(&sringp->s_ring_lock);
3773 if (sringp->s_ring_state & S_RING_TX_HIWAT)
3774 blocked = B_TRUE;
3775 mutex_exit(&sringp->s_ring_lock);
3776 } else {
3777 for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3778 sringp = mac_srs->srs_tx_soft_rings[i];
3779 mutex_enter(&sringp->s_ring_lock);
3780 if (sringp->s_ring_state & S_RING_TX_HIWAT) {
3781 blocked = B_TRUE;
3782 mutex_exit(&sringp->s_ring_lock);
3783 break;
3784 }
3785 mutex_exit(&sringp->s_ring_lock);
3786 }
3787 }
3788 } else {
3789 blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
3790 }
3791 mutex_exit(&mac_srs->srs_lock);
3792 MAC_TX_RELE(mcip, mytx);
3793 return (blocked);
3794 }
3795
3796 /*
3797 * Check if the MAC client is the primary MAC client.
3798 */
3799 boolean_t
mac_is_primary_client(mac_client_impl_t * mcip)3800 mac_is_primary_client(mac_client_impl_t *mcip)
3801 {
3802 return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
3803 }
3804
3805 void
mac_ioctl(mac_handle_t mh,queue_t * wq,mblk_t * bp)3806 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
3807 {
3808 mac_impl_t *mip = (mac_impl_t *)mh;
3809 int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3810
3811 if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3812 (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3813 /*
3814 * If ndd props were registered, call them.
3815 * Note that ndd ioctls are Obsolete
3816 */
3817 mac_ndd_ioctl(mip, wq, bp);
3818 return;
3819 }
3820
3821 /*
3822 * Call the driver to handle the ioctl. The driver may not support
3823 * any ioctls, in which case we reply with a NAK on its behalf.
3824 */
3825 if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3826 mip->mi_ioctl(mip->mi_driver, wq, bp);
3827 else
3828 miocnak(wq, bp, 0, EINVAL);
3829 }
3830
3831 /*
3832 * Return the link state of the specified MAC instance.
3833 */
3834 link_state_t
mac_link_get(mac_handle_t mh)3835 mac_link_get(mac_handle_t mh)
3836 {
3837 return (((mac_impl_t *)mh)->mi_linkstate);
3838 }
3839
3840 /*
3841 * Add a mac client specified notification callback. Please see the comments
3842 * above mac_callback_add() for general information about mac callback
3843 * addition/deletion in the presence of mac callback list walkers
3844 */
3845 mac_notify_handle_t
mac_notify_add(mac_handle_t mh,mac_notify_t notify_fn,void * arg)3846 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3847 {
3848 mac_impl_t *mip = (mac_impl_t *)mh;
3849 mac_notify_cb_t *mncb;
3850 mac_cb_info_t *mcbi;
3851
3852 /*
3853 * Allocate a notify callback structure, fill in the details and
3854 * use the mac callback list manipulation functions to chain into
3855 * the list of callbacks.
3856 */
3857 mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3858 mncb->mncb_fn = notify_fn;
3859 mncb->mncb_arg = arg;
3860 mncb->mncb_mip = mip;
3861 mncb->mncb_link.mcb_objp = mncb;
3862 mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3863 mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3864
3865 mcbi = &mip->mi_notify_cb_info;
3866
3867 i_mac_perim_enter(mip);
3868 mutex_enter(mcbi->mcbi_lockp);
3869
3870 mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3871 &mncb->mncb_link);
3872
3873 mutex_exit(mcbi->mcbi_lockp);
3874 i_mac_perim_exit(mip);
3875 return ((mac_notify_handle_t)mncb);
3876 }
3877
3878 void
mac_notify_remove_wait(mac_handle_t mh)3879 mac_notify_remove_wait(mac_handle_t mh)
3880 {
3881 mac_impl_t *mip = (mac_impl_t *)mh;
3882 mac_cb_info_t *mcbi = &mip->mi_notify_cb_info;
3883
3884 mutex_enter(mcbi->mcbi_lockp);
3885 mac_callback_remove_wait(&mip->mi_notify_cb_info);
3886 mutex_exit(mcbi->mcbi_lockp);
3887 }
3888
3889 /*
3890 * Remove a mac client specified notification callback
3891 */
3892 int
mac_notify_remove(mac_notify_handle_t mnh,boolean_t wait)3893 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3894 {
3895 mac_notify_cb_t *mncb = (mac_notify_cb_t *)mnh;
3896 mac_impl_t *mip = mncb->mncb_mip;
3897 mac_cb_info_t *mcbi;
3898 int err = 0;
3899
3900 mcbi = &mip->mi_notify_cb_info;
3901
3902 i_mac_perim_enter(mip);
3903 mutex_enter(mcbi->mcbi_lockp);
3904
3905 ASSERT(mncb->mncb_link.mcb_objp == mncb);
3906 /*
3907 * If there aren't any list walkers, the remove would succeed
3908 * inline, else we wait for the deferred remove to complete
3909 */
3910 if (mac_callback_remove(&mip->mi_notify_cb_info,
3911 &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3912 kmem_free(mncb, sizeof (mac_notify_cb_t));
3913 } else {
3914 err = EBUSY;
3915 }
3916
3917 mutex_exit(mcbi->mcbi_lockp);
3918 i_mac_perim_exit(mip);
3919
3920 /*
3921 * If we failed to remove the notification callback and "wait" is set
3922 * to be B_TRUE, wait for the callback to finish after we exit the
3923 * mac perimeter.
3924 */
3925 if (err != 0 && wait) {
3926 mac_notify_remove_wait((mac_handle_t)mip);
3927 return (0);
3928 }
3929
3930 return (err);
3931 }
3932
3933 /*
3934 * Associate resource management callbacks with the specified MAC
3935 * clients.
3936 */
3937
3938 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)3939 mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3940 mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3941 mac_resource_restart_t restart, mac_resource_bind_t bind,
3942 void *arg)
3943 {
3944 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3945
3946 mcip->mci_resource_add = add;
3947 mcip->mci_resource_remove = remove;
3948 mcip->mci_resource_quiesce = quiesce;
3949 mcip->mci_resource_restart = restart;
3950 mcip->mci_resource_bind = bind;
3951 mcip->mci_resource_arg = arg;
3952 }
3953
3954 void
mac_resource_set(mac_client_handle_t mch,mac_resource_add_t add,void * arg)3955 mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3956 {
3957 /* update the 'resource_add' callback */
3958 mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3959 }
3960
3961 /*
3962 * Sets up the client resources and enable the polling interface over all the
3963 * SRS's and the soft rings of the client
3964 */
3965 void
mac_client_poll_enable(mac_client_handle_t mch)3966 mac_client_poll_enable(mac_client_handle_t mch)
3967 {
3968 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3969 mac_soft_ring_set_t *mac_srs;
3970 flow_entry_t *flent;
3971 int i;
3972
3973 flent = mcip->mci_flent;
3974 ASSERT(flent != NULL);
3975
3976 mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3977 for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3978 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3979 ASSERT(mac_srs->srs_mcip == mcip);
3980 mac_srs_client_poll_enable(mcip, mac_srs);
3981 }
3982 }
3983
3984 /*
3985 * Tears down the client resources and disable the polling interface over all
3986 * the SRS's and the soft rings of the client
3987 */
3988 void
mac_client_poll_disable(mac_client_handle_t mch)3989 mac_client_poll_disable(mac_client_handle_t mch)
3990 {
3991 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3992 mac_soft_ring_set_t *mac_srs;
3993 flow_entry_t *flent;
3994 int i;
3995
3996 flent = mcip->mci_flent;
3997 ASSERT(flent != NULL);
3998
3999 mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
4000 for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
4001 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
4002 ASSERT(mac_srs->srs_mcip == mcip);
4003 mac_srs_client_poll_disable(mcip, mac_srs);
4004 }
4005 }
4006
4007 /*
4008 * Associate the CPUs specified by the given property with a MAC client.
4009 */
4010 int
mac_cpu_set(mac_client_handle_t mch,mac_resource_props_t * mrp)4011 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
4012 {
4013 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4014 mac_impl_t *mip = mcip->mci_mip;
4015 int err = 0;
4016
4017 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4018
4019 if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
4020 mcip->mci_upper_mip : mip, mrp)) != 0) {
4021 return (err);
4022 }
4023 if (MCIP_DATAPATH_SETUP(mcip))
4024 mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
4025
4026 mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
4027 return (0);
4028 }
4029
4030 /*
4031 * Apply the specified properties to the specified MAC client.
4032 */
4033 int
mac_client_set_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4034 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4035 {
4036 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4037 mac_impl_t *mip = mcip->mci_mip;
4038 int err = 0;
4039
4040 i_mac_perim_enter(mip);
4041
4042 if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
4043 err = mac_resource_ctl_set(mch, mrp);
4044 if (err != 0)
4045 goto done;
4046 }
4047
4048 if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
4049 err = mac_cpu_set(mch, mrp);
4050 if (err != 0)
4051 goto done;
4052 }
4053
4054 if (mrp->mrp_mask & MRP_PROTECT) {
4055 err = mac_protect_set(mch, mrp);
4056 if (err != 0)
4057 goto done;
4058 }
4059
4060 if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
4061 err = mac_resource_ctl_set(mch, mrp);
4062
4063 done:
4064 i_mac_perim_exit(mip);
4065 return (err);
4066 }
4067
4068 /*
4069 * Return the properties currently associated with the specified MAC client.
4070 */
4071 void
mac_client_get_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4072 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4073 {
4074 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4075 mac_resource_props_t *mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4076
4077 bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4078 }
4079
4080 /*
4081 * Return the effective properties currently associated with the specified
4082 * MAC client.
4083 */
4084 void
mac_client_get_effective_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4085 mac_client_get_effective_resources(mac_client_handle_t mch,
4086 mac_resource_props_t *mrp)
4087 {
4088 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4089 mac_resource_props_t *mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
4090
4091 bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4092 }
4093
4094 /*
4095 * Pass a copy of the specified packet to the promiscuous callbacks
4096 * of the specified MAC.
4097 *
4098 * If sender is NULL, the function is being invoked for a packet chain
4099 * received from the wire. If sender is non-NULL, it points to
4100 * the MAC client from which the packet is being sent.
4101 *
4102 * The packets are distributed to the promiscuous callbacks as follows:
4103 *
4104 * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4105 * - all broadcast and multicast packets are sent to the
4106 * MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4107 *
4108 * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4109 * after classification by mac_rx_deliver().
4110 */
4111 static void
mac_promisc_dispatch_one(mac_promisc_impl_t * mpip,mblk_t * mp,boolean_t loopback,boolean_t local)4112 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4113 boolean_t loopback, boolean_t local)
4114 {
4115 mblk_t *mp_next;
4116
4117 if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4118 mblk_t *mp_copy;
4119
4120 mp_copy = copymsg(mp);
4121 if (mp_copy == NULL)
4122 return;
4123
4124 if (mpip->mpi_strip_vlan_tag) {
4125 mp_copy = mac_strip_vlan_tag_chain(mp_copy);
4126 if (mp_copy == NULL)
4127 return;
4128 }
4129
4130 /*
4131 * There is code upstack that can't deal with message
4132 * chains.
4133 */
4134 for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
4135 mp_next = tmp->b_next;
4136 tmp->b_next = NULL;
4137 mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
4138 }
4139
4140 return;
4141 }
4142
4143 mp_next = mp->b_next;
4144 mp->b_next = NULL;
4145 mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
4146 mp->b_next = mp_next;
4147 }
4148
4149 /*
4150 * Return the VID of a packet. Zero if the packet is not tagged.
4151 */
4152 static uint16_t
mac_ether_vid(mblk_t * mp)4153 mac_ether_vid(mblk_t *mp)
4154 {
4155 struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4156
4157 if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4158 struct ether_vlan_header *t_evhp =
4159 (struct ether_vlan_header *)mp->b_rptr;
4160 return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4161 }
4162
4163 return (0);
4164 }
4165
4166 /*
4167 * Return whether the specified packet contains a multicast or broadcast
4168 * destination MAC address.
4169 */
4170 static boolean_t
mac_is_mcast(mac_impl_t * mip,mblk_t * mp)4171 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4172 {
4173 mac_header_info_t hdr_info;
4174
4175 if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4176 return (B_FALSE);
4177 return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4178 (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4179 }
4180
4181 /*
4182 * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4183 * "sender" points to the sender MAC client for outbound packets, and
4184 * is set to NULL for inbound packets.
4185 */
4186 void
mac_promisc_dispatch(mac_impl_t * mip,mblk_t * mp_chain,mac_client_impl_t * sender,boolean_t local)4187 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4188 mac_client_impl_t *sender, boolean_t local)
4189 {
4190 mac_promisc_impl_t *mpip;
4191 mac_cb_t *mcb;
4192 mblk_t *mp;
4193 boolean_t is_mcast, is_sender;
4194
4195 MAC_PROMISC_WALKER_INC(mip);
4196 for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4197 is_mcast = mac_is_mcast(mip, mp);
4198 /* send packet to interested callbacks */
4199 for (mcb = mip->mi_promisc_list; mcb != NULL;
4200 mcb = mcb->mcb_nextp) {
4201 mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4202 is_sender = (mpip->mpi_mcip == sender);
4203
4204 if (is_sender && mpip->mpi_no_tx_loop)
4205 /*
4206 * The sender doesn't want to receive
4207 * copies of the packets it sends.
4208 */
4209 continue;
4210
4211 /* this client doesn't need any packets (bridge) */
4212 if (mpip->mpi_fn == NULL)
4213 continue;
4214
4215 /*
4216 * For an ethernet MAC, don't displatch a multicast
4217 * packet to a non-PROMISC_ALL callbacks unless the VID
4218 * of the packet matches the VID of the client.
4219 */
4220 if (is_mcast &&
4221 mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4222 !mac_client_check_flow_vid(mpip->mpi_mcip,
4223 mac_ether_vid(mp)))
4224 continue;
4225
4226 if (is_sender ||
4227 mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4228 is_mcast) {
4229 mac_promisc_dispatch_one(mpip, mp, is_sender,
4230 local);
4231 }
4232 }
4233 }
4234 MAC_PROMISC_WALKER_DCR(mip);
4235 }
4236
4237 void
mac_promisc_client_dispatch(mac_client_impl_t * mcip,mblk_t * mp_chain)4238 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4239 {
4240 mac_impl_t *mip = mcip->mci_mip;
4241 mac_promisc_impl_t *mpip;
4242 boolean_t is_mcast;
4243 mblk_t *mp;
4244 mac_cb_t *mcb;
4245
4246 /*
4247 * The unicast packets for the MAC client still
4248 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4249 * promiscuous callbacks. The broadcast and multicast
4250 * packets were delivered from mac_rx().
4251 */
4252 MAC_PROMISC_WALKER_INC(mip);
4253 for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4254 is_mcast = mac_is_mcast(mip, mp);
4255 for (mcb = mcip->mci_promisc_list; mcb != NULL;
4256 mcb = mcb->mcb_nextp) {
4257 mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4258 if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4259 !is_mcast) {
4260 mac_promisc_dispatch_one(mpip, mp, B_FALSE,
4261 B_FALSE);
4262 }
4263 }
4264 }
4265 MAC_PROMISC_WALKER_DCR(mip);
4266 }
4267
4268 /*
4269 * Return the margin value currently assigned to the specified MAC instance.
4270 */
4271 void
mac_margin_get(mac_handle_t mh,uint32_t * marginp)4272 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4273 {
4274 mac_impl_t *mip = (mac_impl_t *)mh;
4275
4276 rw_enter(&(mip->mi_rw_lock), RW_READER);
4277 *marginp = mip->mi_margin;
4278 rw_exit(&(mip->mi_rw_lock));
4279 }
4280
4281 /*
4282 * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4283 * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4284 * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4285 * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4286 * cannot disappear while we are accessing it.
4287 */
4288 typedef struct i_mac_info_state_s {
4289 const char *mi_name;
4290 mac_info_t *mi_infop;
4291 } i_mac_info_state_t;
4292
4293 /*ARGSUSED*/
4294 static uint_t
i_mac_info_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)4295 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4296 {
4297 i_mac_info_state_t *statep = arg;
4298 mac_impl_t *mip = (mac_impl_t *)val;
4299
4300 if (mip->mi_state_flags & MIS_DISABLED)
4301 return (MH_WALK_CONTINUE);
4302
4303 if (strcmp(statep->mi_name,
4304 ddi_driver_name(mip->mi_dip)) != 0)
4305 return (MH_WALK_CONTINUE);
4306
4307 statep->mi_infop = &mip->mi_info;
4308 return (MH_WALK_TERMINATE);
4309 }
4310
4311 boolean_t
mac_info_get(const char * name,mac_info_t * minfop)4312 mac_info_get(const char *name, mac_info_t *minfop)
4313 {
4314 i_mac_info_state_t state;
4315
4316 rw_enter(&i_mac_impl_lock, RW_READER);
4317 state.mi_name = name;
4318 state.mi_infop = NULL;
4319 mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4320 if (state.mi_infop == NULL) {
4321 rw_exit(&i_mac_impl_lock);
4322 return (B_FALSE);
4323 }
4324 *minfop = *state.mi_infop;
4325 rw_exit(&i_mac_impl_lock);
4326 return (B_TRUE);
4327 }
4328
4329 /*
4330 * To get the capabilities that MAC layer cares about, such as rings, factory
4331 * mac address, vnic or not, it should directly invoke this function. If the
4332 * link is part of a bridge, then the only "capability" it has is the inability
4333 * to do zero copy.
4334 */
4335 boolean_t
i_mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4336 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4337 {
4338 mac_impl_t *mip = (mac_impl_t *)mh;
4339
4340 if (mip->mi_bridge_link != NULL) {
4341 return (cap == MAC_CAPAB_NO_ZCOPY);
4342 } else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
4343 return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
4344 } else {
4345 return (B_FALSE);
4346 }
4347 }
4348
4349 /*
4350 * Capability query function. If number of active mac clients is greater than
4351 * 1, only limited capabilities can be advertised to the caller no matter the
4352 * driver has certain capability or not. Else, we query the driver to get the
4353 * capability.
4354 */
4355 boolean_t
mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4356 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4357 {
4358 mac_impl_t *mip = (mac_impl_t *)mh;
4359
4360 /*
4361 * Some capabilities are restricted when there are more than one active
4362 * clients on the MAC resource. The ones noted below are safe,
4363 * independent of that count.
4364 */
4365 if (mip->mi_nactiveclients > 1) {
4366 switch (cap) {
4367 case MAC_CAPAB_NO_ZCOPY:
4368 return (B_TRUE);
4369 case MAC_CAPAB_LEGACY:
4370 case MAC_CAPAB_HCKSUM:
4371 case MAC_CAPAB_LSO:
4372 case MAC_CAPAB_NO_NATIVEVLAN:
4373 break;
4374 default:
4375 return (B_FALSE);
4376 }
4377 }
4378
4379 /* else get capab from driver */
4380 return (i_mac_capab_get(mh, cap, cap_data));
4381 }
4382
4383 boolean_t
mac_sap_verify(mac_handle_t mh,uint32_t sap,uint32_t * bind_sap)4384 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4385 {
4386 mac_impl_t *mip = (mac_impl_t *)mh;
4387
4388 return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4389 mip->mi_pdata));
4390 }
4391
4392 mblk_t *
mac_header(mac_handle_t mh,const uint8_t * daddr,uint32_t sap,mblk_t * payload,size_t extra_len)4393 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4394 size_t extra_len)
4395 {
4396 mac_impl_t *mip = (mac_impl_t *)mh;
4397 const uint8_t *hdr_daddr;
4398
4399 /*
4400 * If the MAC is point-to-point with a fixed destination address, then
4401 * we must always use that destination in the MAC header.
4402 */
4403 hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
4404 return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4405 mip->mi_pdata, payload, extra_len));
4406 }
4407
4408 int
mac_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4409 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4410 {
4411 mac_impl_t *mip = (mac_impl_t *)mh;
4412
4413 return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4414 mhip));
4415 }
4416
4417 int
mac_vlan_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4418 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4419 {
4420 mac_impl_t *mip = (mac_impl_t *)mh;
4421 boolean_t is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
4422 int err = 0;
4423
4424 /*
4425 * Packets should always be at least 16 bit aligned.
4426 */
4427 ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
4428
4429 if ((err = mac_header_info(mh, mp, mhip)) != 0)
4430 return (err);
4431
4432 /*
4433 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
4434 * mac_header_info_t as returned by mac_header_info() is
4435 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
4436 */
4437 if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
4438 struct ether_vlan_header *evhp;
4439 uint16_t sap;
4440 mblk_t *tmp = NULL;
4441 size_t size;
4442
4443 size = sizeof (struct ether_vlan_header);
4444 if (MBLKL(mp) < size) {
4445 /*
4446 * Pullup the message in order to get the MAC header
4447 * infomation. Note that this is a read-only function,
4448 * we keep the input packet intact.
4449 */
4450 if ((tmp = msgpullup(mp, size)) == NULL)
4451 return (EINVAL);
4452
4453 mp = tmp;
4454 }
4455 evhp = (struct ether_vlan_header *)mp->b_rptr;
4456 sap = ntohs(evhp->ether_type);
4457 (void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
4458 mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
4459 mhip->mhi_tci = ntohs(evhp->ether_tci);
4460 mhip->mhi_istagged = B_TRUE;
4461 freemsg(tmp);
4462
4463 if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
4464 return (EINVAL);
4465 } else {
4466 mhip->mhi_istagged = B_FALSE;
4467 mhip->mhi_tci = 0;
4468 }
4469
4470 return (0);
4471 }
4472
4473 mblk_t *
mac_header_cook(mac_handle_t mh,mblk_t * mp)4474 mac_header_cook(mac_handle_t mh, mblk_t *mp)
4475 {
4476 mac_impl_t *mip = (mac_impl_t *)mh;
4477
4478 if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4479 if (DB_REF(mp) > 1) {
4480 mblk_t *newmp = copymsg(mp);
4481 if (newmp == NULL)
4482 return (NULL);
4483 freemsg(mp);
4484 mp = newmp;
4485 }
4486 return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4487 mip->mi_pdata));
4488 }
4489 return (mp);
4490 }
4491
4492 mblk_t *
mac_header_uncook(mac_handle_t mh,mblk_t * mp)4493 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4494 {
4495 mac_impl_t *mip = (mac_impl_t *)mh;
4496
4497 if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4498 if (DB_REF(mp) > 1) {
4499 mblk_t *newmp = copymsg(mp);
4500 if (newmp == NULL)
4501 return (NULL);
4502 freemsg(mp);
4503 mp = newmp;
4504 }
4505 return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4506 mip->mi_pdata));
4507 }
4508 return (mp);
4509 }
4510
4511 uint_t
mac_addr_len(mac_handle_t mh)4512 mac_addr_len(mac_handle_t mh)
4513 {
4514 mac_impl_t *mip = (mac_impl_t *)mh;
4515
4516 return (mip->mi_type->mt_addr_length);
4517 }
4518
4519 /* True if a MAC is a VNIC */
4520 boolean_t
mac_is_vnic(mac_handle_t mh)4521 mac_is_vnic(mac_handle_t mh)
4522 {
4523 return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
4524 }
4525
4526 boolean_t
mac_is_overlay(mac_handle_t mh)4527 mac_is_overlay(mac_handle_t mh)
4528 {
4529 return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
4530 }
4531
4532 mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)4533 mac_get_lower_mac_handle(mac_handle_t mh)
4534 {
4535 mac_impl_t *mip = (mac_impl_t *)mh;
4536
4537 ASSERT(mac_is_vnic(mh));
4538 return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4539 }
4540
4541 boolean_t
mac_is_vnic_primary(mac_handle_t mh)4542 mac_is_vnic_primary(mac_handle_t mh)
4543 {
4544 mac_impl_t *mip = (mac_impl_t *)mh;
4545
4546 ASSERT(mac_is_vnic(mh));
4547 return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4548 VNIC_MAC_ADDR_TYPE_PRIMARY);
4549 }
4550
4551 void
mac_update_resources(mac_resource_props_t * nmrp,mac_resource_props_t * cmrp,boolean_t is_user_flow)4552 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4553 boolean_t is_user_flow)
4554 {
4555 if (nmrp != NULL && cmrp != NULL) {
4556 if (nmrp->mrp_mask & MRP_PRIORITY) {
4557 if (nmrp->mrp_priority == MPL_RESET) {
4558 cmrp->mrp_mask &= ~MRP_PRIORITY;
4559 if (is_user_flow) {
4560 cmrp->mrp_priority =
4561 MPL_SUBFLOW_DEFAULT;
4562 } else {
4563 cmrp->mrp_priority = MPL_LINK_DEFAULT;
4564 }
4565 } else {
4566 cmrp->mrp_mask |= MRP_PRIORITY;
4567 cmrp->mrp_priority = nmrp->mrp_priority;
4568 }
4569 }
4570 if (nmrp->mrp_mask & MRP_MAXBW) {
4571 if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4572 cmrp->mrp_mask &= ~MRP_MAXBW;
4573 cmrp->mrp_maxbw = 0;
4574 } else {
4575 cmrp->mrp_mask |= MRP_MAXBW;
4576 cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4577 }
4578 }
4579 if (nmrp->mrp_mask & MRP_CPUS)
4580 MAC_COPY_CPUS(nmrp, cmrp);
4581
4582 if (nmrp->mrp_mask & MRP_POOL) {
4583 if (strlen(nmrp->mrp_pool) == 0) {
4584 cmrp->mrp_mask &= ~MRP_POOL;
4585 bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4586 } else {
4587 cmrp->mrp_mask |= MRP_POOL;
4588 (void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4589 sizeof (cmrp->mrp_pool));
4590 }
4591
4592 }
4593
4594 if (nmrp->mrp_mask & MRP_PROTECT)
4595 mac_protect_update(nmrp, cmrp);
4596
4597 /*
4598 * Update the rings specified.
4599 */
4600 if (nmrp->mrp_mask & MRP_RX_RINGS) {
4601 if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4602 cmrp->mrp_mask &= ~MRP_RX_RINGS;
4603 if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4604 cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4605 cmrp->mrp_nrxrings = 0;
4606 } else {
4607 cmrp->mrp_mask |= MRP_RX_RINGS;
4608 cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4609 }
4610 }
4611 if (nmrp->mrp_mask & MRP_TX_RINGS) {
4612 if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4613 cmrp->mrp_mask &= ~MRP_TX_RINGS;
4614 if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4615 cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4616 cmrp->mrp_ntxrings = 0;
4617 } else {
4618 cmrp->mrp_mask |= MRP_TX_RINGS;
4619 cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4620 }
4621 }
4622 if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4623 cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4624 else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4625 cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4626
4627 if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4628 cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4629 else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4630 cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4631 }
4632 }
4633
4634 /*
4635 * i_mac_set_resources:
4636 *
4637 * This routine associates properties with the primary MAC client of
4638 * the specified MAC instance.
4639 * - Cache the properties in mac_impl_t
4640 * - Apply the properties to the primary MAC client if exists
4641 */
4642 int
i_mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4643 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4644 {
4645 mac_impl_t *mip = (mac_impl_t *)mh;
4646 mac_client_impl_t *mcip;
4647 int err = 0;
4648 uint32_t resmask, newresmask;
4649 mac_resource_props_t *tmrp, *umrp;
4650
4651 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4652
4653 err = mac_validate_props(mip, mrp);
4654 if (err != 0)
4655 return (err);
4656
4657 umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4658 bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4659 resmask = umrp->mrp_mask;
4660 mac_update_resources(mrp, umrp, B_FALSE);
4661 newresmask = umrp->mrp_mask;
4662
4663 if (resmask == 0 && newresmask != 0) {
4664 /*
4665 * Bandwidth, priority, cpu or pool link properties configured,
4666 * must disable fastpath.
4667 */
4668 if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4669 kmem_free(umrp, sizeof (*umrp));
4670 return (err);
4671 }
4672 }
4673
4674 /*
4675 * Since bind_cpu may be modified by mac_client_set_resources()
4676 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4677 * This allows us to cache only user edits in mip.
4678 */
4679 tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4680 bcopy(mrp, tmrp, sizeof (*tmrp));
4681 mcip = mac_primary_client_handle(mip);
4682 if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4683 err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4684 } else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4685 mrp->mrp_mask & MRP_TX_RINGS)) {
4686 mac_client_impl_t *vmcip;
4687
4688 /*
4689 * If the primary is not up, we need to check if there
4690 * are any VLANs on this primary. If there are then
4691 * we need to set this property on the VLANs since
4692 * VLANs follow the primary they are based on. Just
4693 * look for the first VLAN and change its properties,
4694 * all the other VLANs should be in the same group.
4695 */
4696 for (vmcip = mip->mi_clients_list; vmcip != NULL;
4697 vmcip = vmcip->mci_client_next) {
4698 if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4699 mac_client_vid((mac_client_handle_t)vmcip) !=
4700 VLAN_ID_NONE) {
4701 break;
4702 }
4703 }
4704 if (vmcip != NULL) {
4705 mac_resource_props_t *omrp;
4706 mac_resource_props_t *vmrp;
4707
4708 omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4709 bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4710 /*
4711 * We dont' call mac_update_resources since we
4712 * want to take only the ring properties and
4713 * not all the properties that may have changed.
4714 */
4715 vmrp = MCIP_RESOURCE_PROPS(vmcip);
4716 if (mrp->mrp_mask & MRP_RX_RINGS) {
4717 if (mrp->mrp_mask & MRP_RINGS_RESET) {
4718 vmrp->mrp_mask &= ~MRP_RX_RINGS;
4719 if (vmrp->mrp_mask &
4720 MRP_RXRINGS_UNSPEC) {
4721 vmrp->mrp_mask &=
4722 ~MRP_RXRINGS_UNSPEC;
4723 }
4724 vmrp->mrp_nrxrings = 0;
4725 } else {
4726 vmrp->mrp_mask |= MRP_RX_RINGS;
4727 vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4728 }
4729 }
4730 if (mrp->mrp_mask & MRP_TX_RINGS) {
4731 if (mrp->mrp_mask & MRP_RINGS_RESET) {
4732 vmrp->mrp_mask &= ~MRP_TX_RINGS;
4733 if (vmrp->mrp_mask &
4734 MRP_TXRINGS_UNSPEC) {
4735 vmrp->mrp_mask &=
4736 ~MRP_TXRINGS_UNSPEC;
4737 }
4738 vmrp->mrp_ntxrings = 0;
4739 } else {
4740 vmrp->mrp_mask |= MRP_TX_RINGS;
4741 vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4742 }
4743 }
4744 if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4745 vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4746
4747 if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4748 vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4749
4750 if ((err = mac_client_set_rings_prop(vmcip, mrp,
4751 omrp)) != 0) {
4752 bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4753 sizeof (*omrp));
4754 } else {
4755 mac_set_prim_vlan_rings(mip, vmrp);
4756 }
4757 kmem_free(omrp, sizeof (*omrp));
4758 }
4759 }
4760
4761 /* Only update the values if mac_client_set_resources succeeded */
4762 if (err == 0) {
4763 bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
4764 /*
4765 * If bandwidth, priority or cpu link properties cleared,
4766 * renable fastpath.
4767 */
4768 if (resmask != 0 && newresmask == 0)
4769 mac_fastpath_enable((mac_handle_t)mip);
4770 } else if (resmask == 0 && newresmask != 0) {
4771 mac_fastpath_enable((mac_handle_t)mip);
4772 }
4773 kmem_free(tmrp, sizeof (*tmrp));
4774 kmem_free(umrp, sizeof (*umrp));
4775 return (err);
4776 }
4777
4778 int
mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4779 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4780 {
4781 int err;
4782
4783 i_mac_perim_enter((mac_impl_t *)mh);
4784 err = i_mac_set_resources(mh, mrp);
4785 i_mac_perim_exit((mac_impl_t *)mh);
4786 return (err);
4787 }
4788
4789 /*
4790 * Get the properties cached for the specified MAC instance.
4791 */
4792 void
mac_get_resources(mac_handle_t mh,mac_resource_props_t * mrp)4793 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4794 {
4795 mac_impl_t *mip = (mac_impl_t *)mh;
4796 mac_client_impl_t *mcip;
4797
4798 mcip = mac_primary_client_handle(mip);
4799 if (mcip != NULL) {
4800 mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4801 return;
4802 }
4803 bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
4804 }
4805
4806 /*
4807 * Get the effective properties from the primary client of the
4808 * specified MAC instance.
4809 */
4810 void
mac_get_effective_resources(mac_handle_t mh,mac_resource_props_t * mrp)4811 mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4812 {
4813 mac_impl_t *mip = (mac_impl_t *)mh;
4814 mac_client_impl_t *mcip;
4815
4816 mcip = mac_primary_client_handle(mip);
4817 if (mcip != NULL) {
4818 mac_client_get_effective_resources((mac_client_handle_t)mcip,
4819 mrp);
4820 return;
4821 }
4822 bzero(mrp, sizeof (mac_resource_props_t));
4823 }
4824
4825 int
mac_set_pvid(mac_handle_t mh,uint16_t pvid)4826 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
4827 {
4828 mac_impl_t *mip = (mac_impl_t *)mh;
4829 mac_client_impl_t *mcip;
4830 mac_unicast_impl_t *muip;
4831
4832 i_mac_perim_enter(mip);
4833 if (pvid != 0) {
4834 for (mcip = mip->mi_clients_list; mcip != NULL;
4835 mcip = mcip->mci_client_next) {
4836 for (muip = mcip->mci_unicast_list; muip != NULL;
4837 muip = muip->mui_next) {
4838 if (muip->mui_vid == pvid) {
4839 i_mac_perim_exit(mip);
4840 return (EBUSY);
4841 }
4842 }
4843 }
4844 }
4845 mip->mi_pvid = pvid;
4846 i_mac_perim_exit(mip);
4847 return (0);
4848 }
4849
4850 uint16_t
mac_get_pvid(mac_handle_t mh)4851 mac_get_pvid(mac_handle_t mh)
4852 {
4853 mac_impl_t *mip = (mac_impl_t *)mh;
4854
4855 return (mip->mi_pvid);
4856 }
4857
4858 uint32_t
mac_get_llimit(mac_handle_t mh)4859 mac_get_llimit(mac_handle_t mh)
4860 {
4861 mac_impl_t *mip = (mac_impl_t *)mh;
4862
4863 return (mip->mi_llimit);
4864 }
4865
4866 uint32_t
mac_get_ldecay(mac_handle_t mh)4867 mac_get_ldecay(mac_handle_t mh)
4868 {
4869 mac_impl_t *mip = (mac_impl_t *)mh;
4870
4871 return (mip->mi_ldecay);
4872 }
4873
4874 /*
4875 * Rename a mac client, its flow, and the kstat.
4876 */
4877 int
mac_rename_primary(mac_handle_t mh,const char * new_name)4878 mac_rename_primary(mac_handle_t mh, const char *new_name)
4879 {
4880 mac_impl_t *mip = (mac_impl_t *)mh;
4881 mac_client_impl_t *cur_clnt = NULL;
4882 flow_entry_t *fep;
4883
4884 i_mac_perim_enter(mip);
4885
4886 /*
4887 * VNICs: we need to change the sys flow name and
4888 * the associated flow kstat.
4889 */
4890 if (mip->mi_state_flags & MIS_IS_VNIC) {
4891 mac_client_impl_t *mcip = mac_vnic_lower(mip);
4892 ASSERT(new_name != NULL);
4893 mac_rename_flow_names(mcip, new_name);
4894 mac_stat_rename(mcip);
4895 goto done;
4896 }
4897 /*
4898 * This mac may itself be an aggr link, or it may have some client
4899 * which is an aggr port. For both cases, we need to change the
4900 * aggr port's mac client name, its flow name and the associated flow
4901 * kstat.
4902 */
4903 if (mip->mi_state_flags & MIS_IS_AGGR) {
4904 mac_capab_aggr_t aggr_cap;
4905 mac_rename_fn_t rename_fn;
4906 boolean_t ret;
4907
4908 ASSERT(new_name != NULL);
4909 ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4910 (void *)(&aggr_cap));
4911 ASSERT(ret == B_TRUE);
4912 rename_fn = aggr_cap.mca_rename_fn;
4913 rename_fn(new_name, mip->mi_driver);
4914 /*
4915 * The aggr's client name and kstat flow name will be
4916 * updated below, i.e. via mac_rename_flow_names.
4917 */
4918 }
4919
4920 for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4921 cur_clnt = cur_clnt->mci_client_next) {
4922 if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4923 if (new_name != NULL) {
4924 char *str_st = cur_clnt->mci_name;
4925 char *str_del = strchr(str_st, '-');
4926
4927 ASSERT(str_del != NULL);
4928 bzero(str_del + 1, MAXNAMELEN -
4929 (str_del - str_st + 1));
4930 bcopy(new_name, str_del + 1,
4931 strlen(new_name));
4932 }
4933 fep = cur_clnt->mci_flent;
4934 mac_rename_flow(fep, cur_clnt->mci_name);
4935 break;
4936 } else if (new_name != NULL &&
4937 cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4938 mac_rename_flow_names(cur_clnt, new_name);
4939 break;
4940 }
4941 }
4942
4943 /* Recreate kstats associated with aggr pseudo rings */
4944 if (mip->mi_state_flags & MIS_IS_AGGR)
4945 mac_pseudo_ring_stat_rename(mip);
4946
4947 done:
4948 i_mac_perim_exit(mip);
4949 return (0);
4950 }
4951
4952 /*
4953 * Rename the MAC client's flow names
4954 */
4955 static void
mac_rename_flow_names(mac_client_impl_t * mcip,const char * new_name)4956 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4957 {
4958 flow_entry_t *flent;
4959 uint16_t vid;
4960 char flowname[MAXFLOWNAMELEN];
4961 mac_impl_t *mip = mcip->mci_mip;
4962
4963 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4964
4965 /*
4966 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4967 * see a self-consistent value for mci_name
4968 */
4969 rw_enter(&mip->mi_rw_lock, RW_WRITER);
4970 (void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4971 rw_exit(&mip->mi_rw_lock);
4972
4973 mac_rename_flow(mcip->mci_flent, new_name);
4974
4975 if (mcip->mci_nflents == 1)
4976 return;
4977
4978 /*
4979 * We have to rename all the others too, no stats to destroy for
4980 * these.
4981 */
4982 for (flent = mcip->mci_flent_list; flent != NULL;
4983 flent = flent->fe_client_next) {
4984 if (flent != mcip->mci_flent) {
4985 vid = i_mac_flow_vid(flent);
4986 (void) sprintf(flowname, "%s%u", new_name, vid);
4987 mac_flow_set_name(flent, flowname);
4988 }
4989 }
4990 }
4991
4992
4993 /*
4994 * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
4995 * defined for the specified MAC client.
4996 */
4997 static void
mac_client_add_to_flow_list(mac_client_impl_t * mcip,flow_entry_t * flent)4998 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
4999 {
5000 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5001 /*
5002 * The promisc Rx data path walks the mci_flent_list. Protect by
5003 * using mi_rw_lock
5004 */
5005 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5006
5007 mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5008
5009 /* Add it to the head */
5010 flent->fe_client_next = mcip->mci_flent_list;
5011 mcip->mci_flent_list = flent;
5012 mcip->mci_nflents++;
5013
5014 /*
5015 * Keep track of the number of non-zero VIDs addresses per MAC
5016 * client to avoid figuring it out in the data-path.
5017 */
5018 if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5019 mcip->mci_nvids++;
5020
5021 rw_exit(&mcip->mci_rw_lock);
5022 }
5023
5024 /*
5025 * Remove a flow entry from the MAC client's list.
5026 */
5027 static void
mac_client_remove_flow_from_list(mac_client_impl_t * mcip,flow_entry_t * flent)5028 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5029 {
5030 flow_entry_t *fe = mcip->mci_flent_list;
5031 flow_entry_t *prev_fe = NULL;
5032
5033 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5034 /*
5035 * The promisc Rx data path walks the mci_flent_list. Protect by
5036 * using mci_rw_lock
5037 */
5038 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5039 mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
5040
5041 while ((fe != NULL) && (fe != flent)) {
5042 prev_fe = fe;
5043 fe = fe->fe_client_next;
5044 }
5045
5046 ASSERT(fe != NULL);
5047 if (prev_fe == NULL) {
5048 /* Deleting the first node */
5049 mcip->mci_flent_list = fe->fe_client_next;
5050 } else {
5051 prev_fe->fe_client_next = fe->fe_client_next;
5052 }
5053 mcip->mci_nflents--;
5054
5055 if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5056 mcip->mci_nvids--;
5057
5058 rw_exit(&mcip->mci_rw_lock);
5059 }
5060
5061 /*
5062 * Check if the given VID belongs to this MAC client.
5063 */
5064 boolean_t
mac_client_check_flow_vid(mac_client_impl_t * mcip,uint16_t vid)5065 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
5066 {
5067 flow_entry_t *flent;
5068 uint16_t mci_vid;
5069 uint32_t cache = mcip->mci_vidcache;
5070
5071 /*
5072 * In hopes of not having to touch the mci_rw_lock, check to see if
5073 * this vid matches our cached result.
5074 */
5075 if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
5076 return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
5077
5078 /* The mci_flent_list is protected by mci_rw_lock */
5079 rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5080 for (flent = mcip->mci_flent_list; flent != NULL;
5081 flent = flent->fe_client_next) {
5082 mci_vid = i_mac_flow_vid(flent);
5083 if (vid == mci_vid) {
5084 mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
5085 rw_exit(&mcip->mci_rw_lock);
5086 return (B_TRUE);
5087 }
5088 }
5089
5090 mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
5091 rw_exit(&mcip->mci_rw_lock);
5092 return (B_FALSE);
5093 }
5094
5095 /*
5096 * Get the flow entry for the specified <MAC addr, VID> tuple.
5097 */
5098 static flow_entry_t *
mac_client_get_flow(mac_client_impl_t * mcip,mac_unicast_impl_t * muip)5099 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
5100 {
5101 mac_address_t *map = mcip->mci_unicast;
5102 flow_entry_t *flent;
5103 uint16_t vid;
5104 flow_desc_t flow_desc;
5105
5106 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5107
5108 mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5109 if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5110 return (NULL);
5111
5112 for (flent = mcip->mci_flent_list; flent != NULL;
5113 flent = flent->fe_client_next) {
5114 vid = i_mac_flow_vid(flent);
5115 if (vid == muip->mui_vid) {
5116 return (flent);
5117 }
5118 }
5119
5120 return (NULL);
5121 }
5122
5123 /*
5124 * Since mci_flent has the SRSs, when we want to remove it, we replace
5125 * the flow_desc_t in mci_flent with that of an existing flent and then
5126 * remove that flent instead of mci_flent.
5127 */
5128 static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t * mcip)5129 mac_client_swap_mciflent(mac_client_impl_t *mcip)
5130 {
5131 flow_entry_t *flent = mcip->mci_flent;
5132 flow_tab_t *ft = flent->fe_flow_tab;
5133 flow_entry_t *flent1;
5134 flow_desc_t fl_desc;
5135 char fl_name[MAXFLOWNAMELEN];
5136 int err;
5137
5138 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5139 ASSERT(mcip->mci_nflents > 1);
5140
5141 /* get the next flent following the primary flent */
5142 flent1 = mcip->mci_flent_list->fe_client_next;
5143 ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5144
5145 /*
5146 * Remove the flent from the flow table before updating the
5147 * flow descriptor as the hash depends on the flow descriptor.
5148 * This also helps incoming packet classification avoid having
5149 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5150 * flow table is done under the fe_lock so that log or stat functions
5151 * see a self-consistent fe_flow_desc. The name and desc are specific
5152 * to a flow, the rest are shared by all the clients, including
5153 * resource control etc.
5154 */
5155 mac_flow_remove(ft, flent, B_TRUE);
5156 mac_flow_remove(ft, flent1, B_TRUE);
5157
5158 bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5159 bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5160
5161 /* update the primary flow entry */
5162 mutex_enter(&flent->fe_lock);
5163 bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5164 sizeof (flow_desc_t));
5165 bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5166 mutex_exit(&flent->fe_lock);
5167
5168 /* update the flow entry that is to be freed */
5169 mutex_enter(&flent1->fe_lock);
5170 bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5171 bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5172 mutex_exit(&flent1->fe_lock);
5173
5174 /* now reinsert the flow entries in the table */
5175 err = mac_flow_add(ft, flent);
5176 ASSERT(err == 0);
5177
5178 err = mac_flow_add(ft, flent1);
5179 ASSERT(err == 0);
5180
5181 return (flent1);
5182 }
5183
5184 /*
5185 * Return whether there is only one flow entry associated with this
5186 * MAC client.
5187 */
5188 static boolean_t
mac_client_single_rcvr(mac_client_impl_t * mcip)5189 mac_client_single_rcvr(mac_client_impl_t *mcip)
5190 {
5191 return (mcip->mci_nflents == 1);
5192 }
5193
5194 int
mac_validate_props(mac_impl_t * mip,mac_resource_props_t * mrp)5195 mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5196 {
5197 boolean_t reset;
5198 uint32_t rings_needed;
5199 uint32_t rings_avail;
5200 mac_group_type_t gtype;
5201 mac_resource_props_t *mip_mrp;
5202
5203 if (mrp == NULL)
5204 return (0);
5205
5206 if (mrp->mrp_mask & MRP_PRIORITY) {
5207 mac_priority_level_t pri = mrp->mrp_priority;
5208
5209 if (pri < MPL_LOW || pri > MPL_RESET)
5210 return (EINVAL);
5211 }
5212
5213 if (mrp->mrp_mask & MRP_MAXBW) {
5214 uint64_t maxbw = mrp->mrp_maxbw;
5215
5216 if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5217 return (EINVAL);
5218 }
5219 if (mrp->mrp_mask & MRP_CPUS) {
5220 int i, j;
5221 mac_cpu_mode_t fanout;
5222
5223 if (mrp->mrp_ncpus > ncpus)
5224 return (EINVAL);
5225
5226 for (i = 0; i < mrp->mrp_ncpus; i++) {
5227 for (j = 0; j < mrp->mrp_ncpus; j++) {
5228 if (i != j &&
5229 mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5230 return (EINVAL);
5231 }
5232 }
5233 }
5234
5235 for (i = 0; i < mrp->mrp_ncpus; i++) {
5236 cpu_t *cp;
5237 int rv;
5238
5239 mutex_enter(&cpu_lock);
5240 cp = cpu_get(mrp->mrp_cpu[i]);
5241 if (cp != NULL)
5242 rv = cpu_is_online(cp);
5243 else
5244 rv = 0;
5245 mutex_exit(&cpu_lock);
5246 if (rv == 0)
5247 return (EINVAL);
5248 }
5249
5250 fanout = mrp->mrp_fanout_mode;
5251 if (fanout < 0 || fanout > MCM_CPUS)
5252 return (EINVAL);
5253 }
5254
5255 if (mrp->mrp_mask & MRP_PROTECT) {
5256 int err = mac_protect_validate(mrp);
5257 if (err != 0)
5258 return (err);
5259 }
5260
5261 if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
5262 !(mrp->mrp_mask & MRP_TX_RINGS)) {
5263 return (0);
5264 }
5265
5266 /*
5267 * mip will be null when we come from mac_flow_create or
5268 * mac_link_flow_modify. In the latter case it is a user flow,
5269 * for which we don't support rings. In the former we would
5270 * have validated the props beforehand (i_mac_unicast_add ->
5271 * mac_client_set_resources -> validate for the primary and
5272 * vnic_dev_create -> mac_client_set_resources -> validate for
5273 * a vnic.
5274 */
5275 if (mip == NULL)
5276 return (0);
5277
5278 /*
5279 * We don't support setting rings property for a VNIC that is using a
5280 * primary address (VLAN)
5281 */
5282 if ((mip->mi_state_flags & MIS_IS_VNIC) &&
5283 mac_is_vnic_primary((mac_handle_t)mip)) {
5284 return (ENOTSUP);
5285 }
5286
5287 mip_mrp = &mip->mi_resource_props;
5288 /*
5289 * The rings property should be validated against the NICs
5290 * resources
5291 */
5292 if (mip->mi_state_flags & MIS_IS_VNIC)
5293 mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
5294
5295 reset = mrp->mrp_mask & MRP_RINGS_RESET;
5296 /*
5297 * If groups are not supported, return error.
5298 */
5299 if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
5300 ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
5301 return (EINVAL);
5302 }
5303 /*
5304 * If we are just resetting, there is no validation needed.
5305 */
5306 if (reset)
5307 return (0);
5308
5309 if (mrp->mrp_mask & MRP_RX_RINGS) {
5310 rings_needed = mrp->mrp_nrxrings;
5311 /*
5312 * We just want to check if the number of additional
5313 * rings requested is available.
5314 */
5315 if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
5316 if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
5317 /* Just check for the additional rings */
5318 rings_needed -= mip_mrp->mrp_nrxrings;
5319 else
5320 /* We are not asking for additional rings */
5321 rings_needed = 0;
5322 }
5323 rings_avail = mip->mi_rxrings_avail;
5324 gtype = mip->mi_rx_group_type;
5325 } else {
5326 rings_needed = mrp->mrp_ntxrings;
5327 /* Similarly for the TX rings */
5328 if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
5329 if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
5330 /* Just check for the additional rings */
5331 rings_needed -= mip_mrp->mrp_ntxrings;
5332 else
5333 /* We are not asking for additional rings */
5334 rings_needed = 0;
5335 }
5336 rings_avail = mip->mi_txrings_avail;
5337 gtype = mip->mi_tx_group_type;
5338 }
5339
5340 /* Error if the group is dynamic .. */
5341 if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5342 /*
5343 * .. and rings specified are more than available.
5344 */
5345 if (rings_needed > rings_avail)
5346 return (EINVAL);
5347 } else {
5348 /*
5349 * OR group is static and we have specified some rings.
5350 */
5351 if (rings_needed > 0)
5352 return (EINVAL);
5353 }
5354 return (0);
5355 }
5356
5357 /*
5358 * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5359 * underlying physical link is down. This is to allow MAC clients to
5360 * communicate with other clients.
5361 */
5362 void
mac_virtual_link_update(mac_impl_t * mip)5363 mac_virtual_link_update(mac_impl_t *mip)
5364 {
5365 if (mip->mi_linkstate != LINK_STATE_UP)
5366 i_mac_notify(mip, MAC_NOTE_LINK);
5367 }
5368
5369 /*
5370 * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5371 * mac handle in the client.
5372 */
5373 void
mac_set_upper_mac(mac_client_handle_t mch,mac_handle_t mh,mac_resource_props_t * mrp)5374 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5375 mac_resource_props_t *mrp)
5376 {
5377 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5378 mac_impl_t *mip = (mac_impl_t *)mh;
5379
5380 mcip->mci_upper_mip = mip;
5381 /* If there are any properties, copy it over too */
5382 if (mrp != NULL) {
5383 bcopy(mrp, &mip->mi_resource_props,
5384 sizeof (mac_resource_props_t));
5385 }
5386 }
5387
5388 /*
5389 * Mark the mac as being used exclusively by the single mac client that is
5390 * doing some control operation on this mac. No further opens of this mac
5391 * will be allowed until this client calls mac_unmark_exclusive. The mac
5392 * client calling this function must already be in the mac perimeter
5393 */
5394 int
mac_mark_exclusive(mac_handle_t mh)5395 mac_mark_exclusive(mac_handle_t mh)
5396 {
5397 mac_impl_t *mip = (mac_impl_t *)mh;
5398
5399 ASSERT(MAC_PERIM_HELD(mh));
5400 /*
5401 * Look up its entry in the global hash table.
5402 */
5403 rw_enter(&i_mac_impl_lock, RW_WRITER);
5404 if (mip->mi_state_flags & MIS_DISABLED) {
5405 rw_exit(&i_mac_impl_lock);
5406 return (ENOENT);
5407 }
5408
5409 /*
5410 * A reference to mac is held even if the link is not plumbed.
5411 * In i_dls_link_create() we open the MAC interface and hold the
5412 * reference. There is an additional reference for the mac_open
5413 * done in acquiring the mac perimeter
5414 */
5415 if (mip->mi_ref != 2) {
5416 rw_exit(&i_mac_impl_lock);
5417 return (EBUSY);
5418 }
5419
5420 ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5421 mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5422 rw_exit(&i_mac_impl_lock);
5423 return (0);
5424 }
5425
5426 void
mac_unmark_exclusive(mac_handle_t mh)5427 mac_unmark_exclusive(mac_handle_t mh)
5428 {
5429 mac_impl_t *mip = (mac_impl_t *)mh;
5430
5431 ASSERT(MAC_PERIM_HELD(mh));
5432
5433 rw_enter(&i_mac_impl_lock, RW_WRITER);
5434 /* 1 for the creation and another for the perimeter */
5435 ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5436 mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5437 rw_exit(&i_mac_impl_lock);
5438 }
5439
5440 /*
5441 * Set the MTU for the specified MAC.
5442 */
5443 int
mac_set_mtu(mac_handle_t mh,uint_t new_mtu,uint_t * old_mtu_arg)5444 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5445 {
5446 mac_impl_t *mip = (mac_impl_t *)mh;
5447 uint_t old_mtu;
5448 int rv = 0;
5449
5450 i_mac_perim_enter(mip);
5451
5452 if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5453 rv = ENOTSUP;
5454 goto bail;
5455 }
5456
5457 old_mtu = mip->mi_sdu_max;
5458
5459 if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5460 rv = EINVAL;
5461 goto bail;
5462 }
5463
5464 rw_enter(&mip->mi_rw_lock, RW_READER);
5465 if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5466 rv = EBUSY;
5467 rw_exit(&mip->mi_rw_lock);
5468 goto bail;
5469 }
5470 rw_exit(&mip->mi_rw_lock);
5471
5472 if (old_mtu != new_mtu) {
5473 rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5474 "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5475 if (rv != 0)
5476 goto bail;
5477 rv = mac_maxsdu_update(mh, new_mtu);
5478 ASSERT(rv == 0);
5479 }
5480
5481 bail:
5482 i_mac_perim_exit(mip);
5483
5484 if (rv == 0 && old_mtu_arg != NULL)
5485 *old_mtu_arg = old_mtu;
5486 return (rv);
5487 }
5488
5489 /*
5490 * Return the RX h/w information for the group indexed by grp_num.
5491 */
5492 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)5493 mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5494 uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5495 char *clnts_name)
5496 {
5497 mac_impl_t *mip = (mac_impl_t *)mh;
5498 mac_grp_client_t *mcip;
5499 uint_t i = 0, index = 0;
5500 mac_ring_t *ring;
5501
5502 /* Revisit when we implement fully dynamic group allocation */
5503 ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5504
5505 rw_enter(&mip->mi_rw_lock, RW_READER);
5506 *grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5507 *type = mip->mi_rx_groups[grp_index].mrg_type;
5508 *n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5509 ring = mip->mi_rx_groups[grp_index].mrg_rings;
5510 for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5511 index++) {
5512 rings[index] = ring->mr_index;
5513 ring = ring->mr_next;
5514 }
5515 /* Assuming the 1st is the default group */
5516 index = 0;
5517 if (grp_index == 0) {
5518 (void) strlcpy(clnts_name, "<default,mcast>,",
5519 MAXCLIENTNAMELEN);
5520 index += strlen("<default,mcast>,");
5521 }
5522 for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5523 mcip = mcip->mgc_next) {
5524 int name_len = strlen(mcip->mgc_client->mci_name);
5525
5526 /*
5527 * MAXCLIENTNAMELEN is the buffer size reserved for client
5528 * names.
5529 * XXXX Formating the client name string needs to be moved
5530 * to user land when fixing the size of dhi_clnts in
5531 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5532 * dhi_clntsin instead of MAXCLIENTNAMELEN
5533 */
5534 if (index + name_len >= MAXCLIENTNAMELEN) {
5535 index = MAXCLIENTNAMELEN;
5536 break;
5537 }
5538 bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5539 name_len);
5540 index += name_len;
5541 clnts_name[index++] = ',';
5542 i++;
5543 }
5544
5545 /* Get rid of the last , */
5546 if (index > 0)
5547 clnts_name[index - 1] = '\0';
5548 *n_clnts = i;
5549 rw_exit(&mip->mi_rw_lock);
5550 }
5551
5552 /*
5553 * Return the TX h/w information for the group indexed by grp_num.
5554 */
5555 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)5556 mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5557 uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5558 char *clnts_name)
5559 {
5560 mac_impl_t *mip = (mac_impl_t *)mh;
5561 mac_grp_client_t *mcip;
5562 uint_t i = 0, index = 0;
5563 mac_ring_t *ring;
5564
5565 /* Revisit when we implement fully dynamic group allocation */
5566 ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5567
5568 rw_enter(&mip->mi_rw_lock, RW_READER);
5569 *grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5570 mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5571 *type = mip->mi_tx_groups[grp_index].mrg_type;
5572 *n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5573 ring = mip->mi_tx_groups[grp_index].mrg_rings;
5574 for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5575 index++) {
5576 rings[index] = ring->mr_index;
5577 ring = ring->mr_next;
5578 }
5579 index = 0;
5580 /* Default group has an index of -1 */
5581 if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5582 (void) strlcpy(clnts_name, "<default>,",
5583 MAXCLIENTNAMELEN);
5584 index += strlen("<default>,");
5585 }
5586 for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5587 mcip = mcip->mgc_next) {
5588 int name_len = strlen(mcip->mgc_client->mci_name);
5589
5590 /*
5591 * MAXCLIENTNAMELEN is the buffer size reserved for client
5592 * names.
5593 * XXXX Formating the client name string needs to be moved
5594 * to user land when fixing the size of dhi_clnts in
5595 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5596 * dhi_clntsin instead of MAXCLIENTNAMELEN
5597 */
5598 if (index + name_len >= MAXCLIENTNAMELEN) {
5599 index = MAXCLIENTNAMELEN;
5600 break;
5601 }
5602 bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5603 name_len);
5604 index += name_len;
5605 clnts_name[index++] = ',';
5606 i++;
5607 }
5608
5609 /* Get rid of the last , */
5610 if (index > 0)
5611 clnts_name[index - 1] = '\0';
5612 *n_clnts = i;
5613 rw_exit(&mip->mi_rw_lock);
5614 }
5615
5616 /*
5617 * Return the group count for RX or TX.
5618 */
5619 uint_t
mac_hwgrp_num(mac_handle_t mh,int type)5620 mac_hwgrp_num(mac_handle_t mh, int type)
5621 {
5622 mac_impl_t *mip = (mac_impl_t *)mh;
5623
5624 /*
5625 * Return the Rx and Tx group count; for the Tx we need to
5626 * include the default too.
5627 */
5628 return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5629 mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5630 }
5631
5632 /*
5633 * The total number of free TX rings for this MAC.
5634 */
5635 uint_t
mac_txavail_get(mac_handle_t mh)5636 mac_txavail_get(mac_handle_t mh)
5637 {
5638 mac_impl_t *mip = (mac_impl_t *)mh;
5639
5640 return (mip->mi_txrings_avail);
5641 }
5642
5643 /*
5644 * The total number of free RX rings for this MAC.
5645 */
5646 uint_t
mac_rxavail_get(mac_handle_t mh)5647 mac_rxavail_get(mac_handle_t mh)
5648 {
5649 mac_impl_t *mip = (mac_impl_t *)mh;
5650
5651 return (mip->mi_rxrings_avail);
5652 }
5653
5654 /*
5655 * The total number of reserved RX rings on this MAC.
5656 */
5657 uint_t
mac_rxrsvd_get(mac_handle_t mh)5658 mac_rxrsvd_get(mac_handle_t mh)
5659 {
5660 mac_impl_t *mip = (mac_impl_t *)mh;
5661
5662 return (mip->mi_rxrings_rsvd);
5663 }
5664
5665 /*
5666 * The total number of reserved TX rings on this MAC.
5667 */
5668 uint_t
mac_txrsvd_get(mac_handle_t mh)5669 mac_txrsvd_get(mac_handle_t mh)
5670 {
5671 mac_impl_t *mip = (mac_impl_t *)mh;
5672
5673 return (mip->mi_txrings_rsvd);
5674 }
5675
5676 /*
5677 * Total number of free RX groups on this MAC.
5678 */
5679 uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)5680 mac_rxhwlnksavail_get(mac_handle_t mh)
5681 {
5682 mac_impl_t *mip = (mac_impl_t *)mh;
5683
5684 return (mip->mi_rxhwclnt_avail);
5685 }
5686
5687 /*
5688 * Total number of RX groups reserved on this MAC.
5689 */
5690 uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)5691 mac_rxhwlnksrsvd_get(mac_handle_t mh)
5692 {
5693 mac_impl_t *mip = (mac_impl_t *)mh;
5694
5695 return (mip->mi_rxhwclnt_used);
5696 }
5697
5698 /*
5699 * Total number of free TX groups on this MAC.
5700 */
5701 uint_t
mac_txhwlnksavail_get(mac_handle_t mh)5702 mac_txhwlnksavail_get(mac_handle_t mh)
5703 {
5704 mac_impl_t *mip = (mac_impl_t *)mh;
5705
5706 return (mip->mi_txhwclnt_avail);
5707 }
5708
5709 /*
5710 * Total number of TX groups reserved on this MAC.
5711 */
5712 uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)5713 mac_txhwlnksrsvd_get(mac_handle_t mh)
5714 {
5715 mac_impl_t *mip = (mac_impl_t *)mh;
5716
5717 return (mip->mi_txhwclnt_used);
5718 }
5719
5720 /*
5721 * Initialize the rings property for a mac client. A non-0 value for
5722 * rxring or txring specifies the number of rings required, a value
5723 * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5724 * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5725 * means the system can decide whether it can give any rings or not.
5726 */
5727 void
mac_client_set_rings(mac_client_handle_t mch,int rxrings,int txrings)5728 mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5729 {
5730 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5731 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip);
5732
5733 if (rxrings != MAC_RXRINGS_DONTCARE) {
5734 mrp->mrp_mask |= MRP_RX_RINGS;
5735 mrp->mrp_nrxrings = rxrings;
5736 }
5737
5738 if (txrings != MAC_TXRINGS_DONTCARE) {
5739 mrp->mrp_mask |= MRP_TX_RINGS;
5740 mrp->mrp_ntxrings = txrings;
5741 }
5742 }
5743
5744 boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)5745 mac_get_promisc_filtered(mac_client_handle_t mch)
5746 {
5747 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5748
5749 return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
5750 }
5751
5752 void
mac_set_promisc_filtered(mac_client_handle_t mch,boolean_t enable)5753 mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
5754 {
5755 mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5756
5757 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5758 if (enable)
5759 mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
5760 else
5761 mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
5762 }
5763