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