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