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