Print this page
5045 use atomic_{inc,dec}_* instead of atomic_add_*
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/dls/dls_link.c
+++ new/usr/src/uts/common/io/dls/dls_link.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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 /*
27 27 * Data-Link Services Module
28 28 */
29 29
30 30 #include <sys/sysmacros.h>
31 31 #include <sys/strsubr.h>
32 32 #include <sys/strsun.h>
33 33 #include <sys/vlan.h>
34 34 #include <sys/dld_impl.h>
35 35 #include <sys/sdt.h>
36 36 #include <sys/atomic.h>
37 37
38 38 static kmem_cache_t *i_dls_link_cachep;
39 39 mod_hash_t *i_dls_link_hash;
40 40 static uint_t i_dls_link_count;
41 41
42 42 #define LINK_HASHSZ 67 /* prime */
43 43 #define IMPL_HASHSZ 67 /* prime */
44 44
45 45 /*
46 46 * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
47 47 */
48 48 #define MAKE_KEY(_sap) \
49 49 ((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
50 50
51 51 #define DLS_STRIP_PADDING(pktsize, p) { \
52 52 if (pktsize != 0) { \
53 53 ssize_t delta = pktsize - msgdsize(p); \
54 54 \
55 55 if (delta < 0) \
56 56 (void) adjmsg(p, delta); \
57 57 } \
58 58 }
59 59
60 60 /*
61 61 * Private functions.
62 62 */
63 63
64 64 /*ARGSUSED*/
65 65 static int
66 66 i_dls_link_constructor(void *buf, void *arg, int kmflag)
67 67 {
68 68 dls_link_t *dlp = buf;
69 69 char name[MAXNAMELEN];
70 70
71 71 bzero(buf, sizeof (dls_link_t));
72 72
73 73 (void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
74 74 dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
75 75 mod_hash_null_valdtor);
76 76
77 77 return (0);
78 78 }
79 79
80 80 /*ARGSUSED*/
81 81 static void
82 82 i_dls_link_destructor(void *buf, void *arg)
83 83 {
84 84 dls_link_t *dlp = buf;
85 85
86 86 ASSERT(dlp->dl_ref == 0);
87 87 ASSERT(dlp->dl_mh == NULL);
88 88 ASSERT(dlp->dl_mah == NULL);
89 89 ASSERT(dlp->dl_unknowns == 0);
90 90
91 91 mod_hash_destroy_idhash(dlp->dl_str_hash);
92 92 dlp->dl_str_hash = NULL;
93 93
94 94 }
95 95
96 96 /*
97 97 * - Parse the mac header information of the given packet.
98 98 * - Strip the padding and skip over the header. Note that because some
99 99 * DLS consumers only check the db_ref count of the first mblk, we
100 100 * pullup the message into a single mblk. Because the original message
101 101 * is freed as the result of message pulling up, mac_vlan_header_info()
102 102 * is called again to update the mhi_saddr and mhi_daddr pointers in the
103 103 * mhip. Further, the mac_vlan_header_info() function ensures that the
104 104 * size of the pulled message is greater than the MAC header size,
105 105 * therefore we can directly advance b_rptr to point at the payload.
106 106 *
107 107 * We choose to use a macro for performance reasons.
108 108 */
109 109 #define DLS_PREPARE_PKT(mh, mp, mhip, err) { \
110 110 mblk_t *nextp = (mp)->b_next; \
111 111 if (((err) = mac_vlan_header_info((mh), (mp), (mhip))) == 0) { \
112 112 DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp)); \
113 113 if (MBLKL((mp)) < (mhip)->mhi_hdrsize) { \
114 114 mblk_t *newmp; \
115 115 if ((newmp = msgpullup((mp), -1)) == NULL) { \
116 116 (err) = EINVAL; \
117 117 } else { \
118 118 (mp)->b_next = NULL; \
119 119 freemsg((mp)); \
120 120 (mp) = newmp; \
121 121 VERIFY(mac_vlan_header_info((mh), \
122 122 (mp), (mhip)) == 0); \
123 123 (mp)->b_next = nextp; \
124 124 (mp)->b_rptr += (mhip)->mhi_hdrsize; \
125 125 } \
126 126 } else { \
127 127 (mp)->b_rptr += (mhip)->mhi_hdrsize; \
128 128 } \
129 129 } \
130 130 }
131 131
132 132 /*
133 133 * Truncate the chain starting at mp such that all packets in the chain
134 134 * have identical source and destination addresses, saps, and tag types
135 135 * (see below). It returns a pointer to the mblk following the chain,
136 136 * NULL if there is no further packet following the processed chain.
137 137 * The countp argument is set to the number of valid packets in the chain.
138 138 * Note that the whole MAC header (including the VLAN tag if any) in each
139 139 * packet will be stripped.
140 140 */
141 141 static mblk_t *
142 142 i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
143 143 uint_t *countp)
144 144 {
145 145 mblk_t *prevp;
146 146 uint_t npacket = 1;
147 147 size_t addr_size = dlp->dl_mip->mi_addr_length;
148 148 uint16_t vid = VLAN_ID(mhip->mhi_tci);
149 149 uint16_t pri = VLAN_PRI(mhip->mhi_tci);
150 150
151 151 /*
152 152 * Compare with subsequent headers until we find one that has
153 153 * differing header information. After checking each packet
154 154 * strip padding and skip over the header.
155 155 */
156 156 for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
157 157 mac_header_info_t cmhi;
158 158 uint16_t cvid, cpri;
159 159 int err;
160 160
161 161 DLS_PREPARE_PKT(dlp->dl_mh, mp, &cmhi, err);
162 162 if (err != 0)
163 163 break;
164 164
165 165 prevp->b_next = mp;
166 166
167 167 /*
168 168 * The source, destination, sap, vlan tag must all match in
169 169 * a given subchain.
170 170 */
171 171 if (mhip->mhi_saddr == NULL || cmhi.mhi_saddr == NULL ||
172 172 memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
173 173 memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
174 174 mhip->mhi_bindsap != cmhi.mhi_bindsap) {
175 175 /*
176 176 * Note that we don't need to restore the padding.
177 177 */
178 178 mp->b_rptr -= cmhi.mhi_hdrsize;
179 179 break;
180 180 }
181 181
182 182 cvid = VLAN_ID(cmhi.mhi_tci);
183 183 cpri = VLAN_PRI(cmhi.mhi_tci);
184 184
185 185 /*
186 186 * There are several types of packets. Packets don't match
187 187 * if they are classified to different type or if they are
188 188 * VLAN packets but belong to different VLANs:
189 189 *
190 190 * packet type tagged vid pri
191 191 * ---------------------------------------------------------
192 192 * untagged No zero zero
193 193 * VLAN packets Yes non-zero -
194 194 * priority tagged Yes zero non-zero
195 195 * 0 tagged Yes zero zero
196 196 */
197 197 if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
198 198 (vid != cvid) || ((vid == VLAN_ID_NONE) &&
199 199 (((pri == 0) && (cpri != 0)) ||
200 200 ((pri != 0) && (cpri == 0))))) {
201 201 mp->b_rptr -= cmhi.mhi_hdrsize;
202 202 break;
203 203 }
204 204
205 205 npacket++;
206 206 }
207 207
208 208 /*
209 209 * Break the chain at this point and return a pointer to the next
210 210 * sub-chain.
211 211 */
212 212 prevp->b_next = NULL;
213 213 *countp = npacket;
214 214 return (mp);
215 215 }
216 216
217 217 /* ARGSUSED */
218 218 static int
219 219 i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
220 220 {
221 221 dls_head_t *dhp = (dls_head_t *)val;
222 222
223 223 /*
224 224 * The lock order is mod_hash's internal lock -> dh_lock as in the
225 225 * call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
226 226 */
227 227 mutex_enter(&dhp->dh_lock);
228 228 if (dhp->dh_removing) {
229 229 mutex_exit(&dhp->dh_lock);
230 230 return (-1);
231 231 }
232 232 dhp->dh_ref++;
233 233 mutex_exit(&dhp->dh_lock);
234 234 return (0);
235 235 }
236 236
237 237 void
238 238 i_dls_head_rele(dls_head_t *dhp)
239 239 {
240 240 mutex_enter(&dhp->dh_lock);
241 241 dhp->dh_ref--;
242 242 if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
243 243 cv_broadcast(&dhp->dh_cv);
244 244 mutex_exit(&dhp->dh_lock);
245 245 }
246 246
247 247 static dls_head_t *
248 248 i_dls_head_alloc(mod_hash_key_t key)
249 249 {
250 250 dls_head_t *dhp;
251 251
252 252 dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
253 253 dhp->dh_key = key;
254 254 return (dhp);
255 255 }
256 256
257 257 static void
258 258 i_dls_head_free(dls_head_t *dhp)
259 259 {
260 260 ASSERT(dhp->dh_ref == 0);
261 261 kmem_free(dhp, sizeof (dls_head_t));
262 262 }
263 263
264 264 /*
265 265 * Try to send mp up to the streams of the given sap and vid. Return B_TRUE
266 266 * if this message is sent to any streams.
267 267 * Note that this function will copy the message chain and the original
268 268 * mp will remain valid after this function
269 269 */
270 270 static uint_t
271 271 i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
272 272 mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
273 273 boolean_t (*acceptfunc)())
274 274 {
275 275 mod_hash_t *hash = dlp->dl_str_hash;
276 276 mod_hash_key_t key;
277 277 dls_head_t *dhp;
278 278 dld_str_t *dsp;
279 279 mblk_t *nmp;
280 280 dls_rx_t ds_rx;
281 281 void *ds_rx_arg;
282 282 uint_t naccepted = 0;
283 283 int rval;
284 284
285 285 /*
286 286 * Construct a hash key from the VLAN identifier and the
287 287 * DLSAP that represents dld_str_t in promiscuous mode.
288 288 */
289 289 key = MAKE_KEY(sap);
290 290
291 291 /*
292 292 * Search the hash table for dld_str_t eligible to receive
293 293 * a packet chain for this DLSAP/VLAN combination. The mod hash's
294 294 * internal lock serializes find/insert/remove from the mod hash list.
295 295 * Incrementing the dh_ref (while holding the mod hash lock) ensures
296 296 * dls_link_remove will wait for the upcall to finish.
297 297 */
298 298 if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
299 299 i_dls_head_hold, &rval) != 0 || (rval != 0)) {
300 300 return (B_FALSE);
301 301 }
302 302
303 303 /*
304 304 * Find dld_str_t that will accept the sub-chain.
305 305 */
306 306 for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
307 307 if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
308 308 continue;
309 309
310 310 /*
311 311 * We have at least one acceptor.
312 312 */
313 313 naccepted++;
314 314
315 315 /*
316 316 * There will normally be at least more dld_str_t
317 317 * (since we've yet to check for non-promiscuous
318 318 * dld_str_t) so dup the sub-chain.
319 319 */
320 320 if ((nmp = copymsgchain(mp)) != NULL)
321 321 ds_rx(ds_rx_arg, mrh, nmp, mhip);
322 322 }
323 323
324 324 /*
325 325 * Release the hold on the dld_str_t chain now that we have
326 326 * finished walking it.
327 327 */
328 328 i_dls_head_rele(dhp);
329 329 return (naccepted);
330 330 }
331 331
332 332 /* ARGSUSED */
333 333 void
334 334 i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
335 335 boolean_t loopback)
336 336 {
337 337 dls_link_t *dlp = arg;
338 338 mod_hash_t *hash = dlp->dl_str_hash;
339 339 mblk_t *nextp;
340 340 mac_header_info_t mhi;
341 341 dls_head_t *dhp;
342 342 dld_str_t *dsp;
343 343 dld_str_t *ndsp;
344 344 mblk_t *nmp;
345 345 mod_hash_key_t key;
346 346 uint_t npacket;
347 347 boolean_t accepted;
348 348 dls_rx_t ds_rx, nds_rx;
349 349 void *ds_rx_arg, *nds_rx_arg;
350 350 uint16_t vid;
351 351 int err, rval;
352 352
353 353 /*
↓ open down ↓ |
353 lines elided |
↑ open up ↑ |
354 354 * Walk the packet chain.
355 355 */
356 356 for (; mp != NULL; mp = nextp) {
357 357 /*
358 358 * Wipe the accepted state.
359 359 */
360 360 accepted = B_FALSE;
361 361
362 362 DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
363 363 if (err != 0) {
364 - atomic_add_32(&(dlp->dl_unknowns), 1);
364 + atomic_inc_32(&(dlp->dl_unknowns));
365 365 nextp = mp->b_next;
366 366 mp->b_next = NULL;
367 367 freemsg(mp);
368 368 continue;
369 369 }
370 370
371 371 /*
372 372 * Grab the longest sub-chain we can process as a single
373 373 * unit.
374 374 */
375 375 nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
376 376 ASSERT(npacket != 0);
377 377
378 378 vid = VLAN_ID(mhi.mhi_tci);
379 379
380 380 if (mhi.mhi_istagged) {
381 381 /*
382 382 * If it is tagged traffic, send it upstream to
383 383 * all dld_str_t which are attached to the physical
384 384 * link and bound to SAP 0x8100.
385 385 */
386 386 if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
387 387 ETHERTYPE_VLAN, dls_accept) > 0) {
388 388 accepted = B_TRUE;
389 389 }
390 390
391 391 /*
392 392 * Don't pass the packets up if they are tagged
393 393 * packets and:
394 394 * - their VID and priority are both zero and the
395 395 * original packet isn't using the PVID (invalid
396 396 * packets).
397 397 * - their sap is ETHERTYPE_VLAN and their VID is
398 398 * zero as they have already been sent upstreams.
399 399 */
400 400 if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid &&
401 401 VLAN_PRI(mhi.mhi_tci) == 0) ||
402 402 (mhi.mhi_bindsap == ETHERTYPE_VLAN &&
403 403 vid == VLAN_ID_NONE)) {
404 404 freemsgchain(mp);
405 405 goto loop;
406 406 }
407 407 }
408 408
409 409 /*
410 410 * Construct a hash key from the VLAN identifier and the
411 411 * DLSAP.
412 412 */
413 413 key = MAKE_KEY(mhi.mhi_bindsap);
414 414
415 415 /*
416 416 * Search the has table for dld_str_t eligible to receive
417 417 * a packet chain for this DLSAP/VLAN combination.
418 418 */
419 419 if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
420 420 i_dls_head_hold, &rval) != 0 || (rval != 0)) {
421 421 freemsgchain(mp);
422 422 goto loop;
423 423 }
424 424
425 425 /*
426 426 * Find the first dld_str_t that will accept the sub-chain.
427 427 */
428 428 for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
429 429 if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
430 430 break;
431 431
432 432 /*
433 433 * If we did not find any dld_str_t willing to accept the
434 434 * sub-chain then throw it away.
435 435 */
436 436 if (dsp == NULL) {
437 437 i_dls_head_rele(dhp);
438 438 freemsgchain(mp);
439 439 goto loop;
440 440 }
441 441
442 442 /*
443 443 * We have at least one acceptor.
444 444 */
445 445 accepted = B_TRUE;
446 446 for (;;) {
447 447 /*
448 448 * Find the next dld_str_t that will accept the
449 449 * sub-chain.
450 450 */
451 451 for (ndsp = dsp->ds_next; ndsp != NULL;
452 452 ndsp = ndsp->ds_next)
453 453 if (dls_accept(ndsp, &mhi, &nds_rx,
454 454 &nds_rx_arg))
455 455 break;
456 456
457 457 /*
458 458 * If there are no more dld_str_t that are willing
459 459 * to accept the sub-chain then we don't need to dup
460 460 * it before handing it to the current one.
461 461 */
462 462 if (ndsp == NULL) {
463 463 ds_rx(ds_rx_arg, mrh, mp, &mhi);
464 464
465 465 /*
466 466 * Since there are no more dld_str_t, we're
467 467 * done.
468 468 */
469 469 break;
470 470 }
471 471
472 472 /*
473 473 * There are more dld_str_t so dup the sub-chain.
474 474 */
475 475 if ((nmp = copymsgchain(mp)) != NULL)
476 476 ds_rx(ds_rx_arg, mrh, nmp, &mhi);
477 477
478 478 dsp = ndsp;
479 479 ds_rx = nds_rx;
480 480 ds_rx_arg = nds_rx_arg;
481 481 }
482 482
483 483 /*
484 484 * Release the hold on the dld_str_t chain now that we have
485 485 * finished walking it.
486 486 */
487 487 i_dls_head_rele(dhp);
488 488
489 489 loop:
490 490 /*
491 491 * If there were no acceptors then add the packet count to the
492 492 * 'unknown' count.
493 493 */
494 494 if (!accepted)
495 495 atomic_add_32(&(dlp->dl_unknowns), npacket);
496 496 }
497 497 }
498 498
499 499 /* ARGSUSED */
500 500 void
501 501 dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
502 502 boolean_t loopback)
503 503 {
504 504 dld_str_t *dsp = arg;
505 505 dls_link_t *dlp = dsp->ds_dlp;
506 506 mac_header_info_t mhi;
507 507 dls_rx_t ds_rx;
508 508 void *ds_rx_arg;
509 509 int err;
510 510
511 511 DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
512 512 if (err != 0)
513 513 goto drop;
514 514
515 515 /*
516 516 * If there is promiscuous handle for vlan, we filter out the untagged
517 517 * pkts and pkts that are not for the primary unicast address.
518 518 */
519 519 if (dsp->ds_vlan_mph != NULL) {
520 520 uint8_t prim_addr[MAXMACADDRLEN];
521 521 size_t addr_length = dsp->ds_mip->mi_addr_length;
522 522
523 523 if (!(mhi.mhi_istagged))
524 524 goto drop;
525 525 ASSERT(dsp->ds_mh != NULL);
526 526 mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
527 527 if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
↓ open down ↓ |
153 lines elided |
↑ open up ↑ |
528 528 goto drop;
529 529
530 530 if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
531 531 goto drop;
532 532
533 533 ds_rx(ds_rx_arg, NULL, mp, &mhi);
534 534 return;
535 535 }
536 536
537 537 drop:
538 - atomic_add_32(&dlp->dl_unknowns, 1);
538 + atomic_inc_32(&dlp->dl_unknowns);
539 539 freemsg(mp);
540 540 }
541 541
542 542 /* ARGSUSED */
543 543 void
544 544 dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
545 545 boolean_t loopback)
546 546 {
547 547 dld_str_t *dsp = arg;
548 548 dls_link_t *dlp = dsp->ds_dlp;
549 549 mac_header_info_t mhi;
550 550 dls_rx_t ds_rx;
551 551 void *ds_rx_arg;
552 552 int err;
553 553 dls_head_t *dhp;
554 554 mod_hash_key_t key;
555 555
556 556 DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
557 557 if (err != 0)
558 558 goto drop;
559 559
560 560 /*
561 561 * In order to filter out sap pkt that no dls channel listens, search
562 562 * the hash table trying to find a dld_str_t eligible to receive the pkt
563 563 */
564 564 if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
565 565 key = MAKE_KEY(mhi.mhi_bindsap);
566 566 if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
567 567 (mod_hash_val_t *)&dhp) != 0)
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
568 568 goto drop;
569 569 }
570 570
571 571 if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
572 572 goto drop;
573 573
574 574 ds_rx(ds_rx_arg, NULL, mp, &mhi);
575 575 return;
576 576
577 577 drop:
578 - atomic_add_32(&dlp->dl_unknowns, 1);
578 + atomic_inc_32(&dlp->dl_unknowns);
579 579 freemsg(mp);
580 580 }
581 581
582 582 static void
583 583 i_dls_link_destroy(dls_link_t *dlp)
584 584 {
585 585 ASSERT(dlp->dl_nactive == 0);
586 586 ASSERT(dlp->dl_impl_count == 0);
587 587 ASSERT(dlp->dl_zone_ref == 0);
588 588
589 589 /*
590 590 * Free the structure back to the cache.
591 591 */
592 592 if (dlp->dl_mch != NULL)
593 593 mac_client_close(dlp->dl_mch, 0);
594 594
595 595 if (dlp->dl_mh != NULL) {
596 596 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
597 597 mac_close(dlp->dl_mh);
598 598 }
599 599
600 600 dlp->dl_mh = NULL;
601 601 dlp->dl_mch = NULL;
602 602 dlp->dl_mip = NULL;
603 603 dlp->dl_unknowns = 0;
604 604 dlp->dl_nonip_cnt = 0;
605 605 kmem_cache_free(i_dls_link_cachep, dlp);
606 606 }
607 607
608 608 static int
609 609 i_dls_link_create(const char *name, dls_link_t **dlpp)
610 610 {
611 611 dls_link_t *dlp;
612 612 int err;
613 613
614 614 /*
615 615 * Allocate a new dls_link_t structure.
616 616 */
617 617 dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
618 618
619 619 /*
620 620 * Name the dls_link_t after the MAC interface it represents.
621 621 */
622 622 (void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
623 623
624 624 /*
625 625 * First reference; hold open the MAC interface.
626 626 */
627 627 ASSERT(dlp->dl_mh == NULL);
628 628 err = mac_open(dlp->dl_name, &dlp->dl_mh);
629 629 if (err != 0)
630 630 goto bail;
631 631
632 632 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
633 633 dlp->dl_mip = mac_info(dlp->dl_mh);
634 634
635 635 /* DLS is the "primary" MAC client */
636 636 ASSERT(dlp->dl_mch == NULL);
637 637
638 638 err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
639 639 MAC_OPEN_FLAGS_USE_DATALINK_NAME);
640 640 if (err != 0)
641 641 goto bail;
642 642
643 643 DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
644 644 dlp->dl_mch);
645 645
646 646 *dlpp = dlp;
647 647 return (0);
648 648
649 649 bail:
650 650 i_dls_link_destroy(dlp);
651 651 return (err);
652 652 }
653 653
654 654 /*
655 655 * Module initialization functions.
656 656 */
657 657
658 658 void
659 659 dls_link_init(void)
660 660 {
661 661 /*
662 662 * Create a kmem_cache of dls_link_t structures.
663 663 */
664 664 i_dls_link_cachep = kmem_cache_create("dls_link_cache",
665 665 sizeof (dls_link_t), 0, i_dls_link_constructor,
666 666 i_dls_link_destructor, NULL, NULL, NULL, 0);
667 667 ASSERT(i_dls_link_cachep != NULL);
668 668
669 669 /*
670 670 * Create a dls_link_t hash table and associated lock.
671 671 */
672 672 i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
673 673 IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
674 674 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
675 675 i_dls_link_count = 0;
676 676 }
677 677
678 678 int
679 679 dls_link_fini(void)
680 680 {
681 681 if (i_dls_link_count > 0)
682 682 return (EBUSY);
683 683
684 684 /*
685 685 * Destroy the kmem_cache.
686 686 */
687 687 kmem_cache_destroy(i_dls_link_cachep);
688 688
689 689 /*
690 690 * Destroy the hash table and associated lock.
691 691 */
692 692 mod_hash_destroy_hash(i_dls_link_hash);
693 693 return (0);
694 694 }
695 695
696 696 /*
697 697 * Exported functions.
698 698 */
699 699
700 700 static int
701 701 dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
702 702 {
703 703 dls_link_t *dlp;
704 704 int err;
705 705
706 706 /*
707 707 * Look up a dls_link_t corresponding to the given macname in the
708 708 * global hash table. The i_dls_link_hash itself is protected by the
709 709 * mod_hash package's internal lock which synchronizes
710 710 * find/insert/remove into the global mod_hash list. Assumes that
711 711 * inserts and removes are single threaded on a per mac end point
712 712 * by the mac perimeter.
713 713 */
714 714 if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
715 715 (mod_hash_val_t *)&dlp)) == 0)
716 716 goto done;
717 717
718 718 ASSERT(err == MH_ERR_NOTFOUND);
719 719 if (!create)
720 720 return (ENOENT);
721 721
722 722 /*
723 723 * We didn't find anything so we need to create one.
724 724 */
↓ open down ↓ |
136 lines elided |
↑ open up ↑ |
725 725 if ((err = i_dls_link_create(name, &dlp)) != 0)
726 726 return (err);
727 727
728 728 /*
729 729 * Insert the dls_link_t.
730 730 */
731 731 err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
732 732 (mod_hash_val_t)dlp);
733 733 ASSERT(err == 0);
734 734
735 - atomic_add_32(&i_dls_link_count, 1);
735 + atomic_inc_32(&i_dls_link_count);
736 736 ASSERT(i_dls_link_count != 0);
737 737
738 738 done:
739 739 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
740 740 /*
741 741 * Bump the reference count and hand back the reference.
742 742 */
743 743 dlp->dl_ref++;
744 744 *dlpp = dlp;
745 745 return (0);
746 746 }
747 747
748 748 int
749 749 dls_link_hold_create(const char *name, dls_link_t **dlpp)
750 750 {
751 751 return (dls_link_hold_common(name, dlpp, B_TRUE));
752 752 }
753 753
754 754 int
755 755 dls_link_hold(const char *name, dls_link_t **dlpp)
756 756 {
757 757 return (dls_link_hold_common(name, dlpp, B_FALSE));
758 758 }
759 759
760 760 dev_info_t *
761 761 dls_link_devinfo(dev_t dev)
762 762 {
763 763 dls_link_t *dlp;
764 764 dev_info_t *dip;
765 765 char macname[MAXNAMELEN];
766 766 char *drv;
767 767 mac_perim_handle_t mph;
768 768
769 769 if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
770 770 return (NULL);
771 771 (void) snprintf(macname, MAXNAMELEN, "%s%d", drv,
772 772 DLS_MINOR2INST(getminor(dev)));
773 773
774 774 /*
775 775 * The code below assumes that the name constructed above is the
776 776 * macname. This is not the case for legacy devices. Currently this
777 777 * is ok because this function is only called in the getinfo(9e) path,
778 778 * which for a legacy device would directly end up in the driver's
779 779 * getinfo, rather than here
780 780 */
781 781 if (mac_perim_enter_by_macname(macname, &mph) != 0)
782 782 return (NULL);
783 783
784 784 if (dls_link_hold(macname, &dlp) != 0) {
785 785 mac_perim_exit(mph);
786 786 return (NULL);
787 787 }
788 788
789 789 dip = mac_devinfo_get(dlp->dl_mh);
790 790 dls_link_rele(dlp);
791 791 mac_perim_exit(mph);
792 792
793 793 return (dip);
794 794 }
795 795
796 796 dev_t
797 797 dls_link_dev(dls_link_t *dlp)
798 798 {
799 799 return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
800 800 mac_minor(dlp->dl_mh)));
801 801 }
802 802
803 803 void
804 804 dls_link_rele(dls_link_t *dlp)
805 805 {
806 806 mod_hash_val_t val;
807 807
808 808 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
809 809 /*
810 810 * Check if there are any more references.
811 811 */
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
812 812 if (--dlp->dl_ref == 0) {
813 813 (void) mod_hash_remove(i_dls_link_hash,
814 814 (mod_hash_key_t)dlp->dl_name, &val);
815 815 ASSERT(dlp == (dls_link_t *)val);
816 816
817 817 /*
818 818 * Destroy the dls_link_t.
819 819 */
820 820 i_dls_link_destroy(dlp);
821 821 ASSERT(i_dls_link_count > 0);
822 - atomic_add_32(&i_dls_link_count, -1);
822 + atomic_dec_32(&i_dls_link_count);
823 823 }
824 824 }
825 825
826 826 int
827 827 dls_link_rele_by_name(const char *name)
828 828 {
829 829 dls_link_t *dlp;
830 830
831 831 if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
832 832 (mod_hash_val_t *)&dlp) != 0)
833 833 return (ENOENT);
834 834
835 835 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
836 836
837 837 /*
838 838 * Must fail detach if mac client is busy.
839 839 */
840 840 ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
841 841 if (mac_link_has_flows(dlp->dl_mch))
842 842 return (ENOTEMPTY);
843 843
844 844 dls_link_rele(dlp);
845 845 return (0);
846 846 }
847 847
848 848 int
849 849 dls_link_setzid(const char *name, zoneid_t zid)
850 850 {
851 851 dls_link_t *dlp;
852 852 int err = 0;
853 853 zoneid_t old_zid;
854 854
855 855 if ((err = dls_link_hold_create(name, &dlp)) != 0)
856 856 return (err);
857 857
858 858 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
859 859
860 860 if ((old_zid = dlp->dl_zid) == zid)
861 861 goto done;
862 862
863 863 /*
864 864 * Check whether this dlp is used by its own zone. If yes, we cannot
865 865 * change its zoneid.
866 866 */
867 867 if (dlp->dl_zone_ref != 0) {
868 868 err = EBUSY;
869 869 goto done;
870 870 }
871 871
872 872 dlp->dl_zid = zid;
873 873
874 874 if (zid == GLOBAL_ZONEID) {
875 875 /*
876 876 * The link is moving from a non-global zone to the global
877 877 * zone, so we need to release the reference that was held
878 878 * when the link was originally assigned to the non-global
879 879 * zone.
880 880 */
881 881 dls_link_rele(dlp);
882 882 }
883 883
884 884 done:
885 885 /*
886 886 * We only keep the reference to this link open if the link has
887 887 * successfully moved from the global zone to a non-global zone.
888 888 */
889 889 if (err != 0 || old_zid != GLOBAL_ZONEID)
890 890 dls_link_rele(dlp);
891 891 return (err);
892 892 }
893 893
894 894 int
895 895 dls_link_getzid(const char *name, zoneid_t *zidp)
896 896 {
897 897 dls_link_t *dlp;
898 898 int err = 0;
899 899
900 900 if ((err = dls_link_hold(name, &dlp)) != 0)
901 901 return (err);
902 902
903 903 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
904 904
905 905 *zidp = dlp->dl_zid;
906 906
907 907 dls_link_rele(dlp);
908 908 return (0);
909 909 }
910 910
911 911 void
912 912 dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
913 913 {
914 914 mod_hash_t *hash = dlp->dl_str_hash;
915 915 mod_hash_key_t key;
916 916 dls_head_t *dhp;
917 917 dld_str_t *p;
918 918 int err;
919 919
920 920 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
921 921
922 922 /*
923 923 * Generate a hash key based on the sap.
924 924 */
925 925 key = MAKE_KEY(sap);
926 926
927 927 /*
928 928 * Search the table for a list head with this key.
929 929 */
930 930 if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
931 931 ASSERT(err == MH_ERR_NOTFOUND);
932 932
933 933 dhp = i_dls_head_alloc(key);
934 934 err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
935 935 ASSERT(err == 0);
936 936 }
937 937
938 938 /*
939 939 * Add the dld_str_t to the head of the list. List walkers in
940 940 * i_dls_link_rx_* bump up dh_ref to ensure the list does not change
941 941 * while they walk the list. The membar below ensures that list walkers
942 942 * see exactly the old list or the new list.
943 943 */
944 944 ASSERT(dsp->ds_next == NULL);
945 945 p = dhp->dh_list;
946 946 dsp->ds_next = p;
947 947
948 948 membar_producer();
949 949
950 950 dhp->dh_list = dsp;
951 951
952 952 /*
953 953 * Save a pointer to the list head.
954 954 */
955 955 dsp->ds_head = dhp;
956 956 dlp->dl_impl_count++;
957 957 }
958 958
959 959 void
960 960 dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
961 961 {
962 962 mod_hash_t *hash = dlp->dl_str_hash;
963 963 dld_str_t **pp;
964 964 dld_str_t *p;
965 965 dls_head_t *dhp;
966 966
967 967 ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
968 968
969 969 /*
970 970 * We set dh_removing here to tell the receive callbacks not to pass
971 971 * up packets anymore. Then wait till the current callbacks are done.
972 972 * This happens either in the close path or in processing the
973 973 * DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
974 974 * The dh_ref ensures there aren't and there won't be any upcalls
975 975 * walking or using the dh_list. The mod hash internal lock ensures
976 976 * that the insert/remove of the dls_head_t itself synchronizes with
977 977 * any i_dls_link_rx trying to locate it. The perimeter ensures that
978 978 * there isn't another simultaneous dls_link_add/remove.
979 979 */
980 980 dhp = dsp->ds_head;
981 981
982 982 mutex_enter(&dhp->dh_lock);
983 983 dhp->dh_removing = B_TRUE;
984 984 while (dhp->dh_ref != 0)
985 985 cv_wait(&dhp->dh_cv, &dhp->dh_lock);
986 986 mutex_exit(&dhp->dh_lock);
987 987
988 988 /*
989 989 * Walk the list and remove the dld_str_t.
990 990 */
991 991 for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
992 992 if (p == dsp)
993 993 break;
994 994 }
995 995 ASSERT(p != NULL);
996 996 *pp = p->ds_next;
997 997 p->ds_next = NULL;
998 998 p->ds_head = NULL;
999 999
1000 1000 ASSERT(dlp->dl_impl_count != 0);
1001 1001 dlp->dl_impl_count--;
1002 1002
1003 1003 if (dhp->dh_list == NULL) {
1004 1004 mod_hash_val_t val = NULL;
1005 1005
1006 1006 /*
1007 1007 * The list is empty so remove the hash table entry.
1008 1008 */
1009 1009 (void) mod_hash_remove(hash, dhp->dh_key, &val);
1010 1010 ASSERT(dhp == (dls_head_t *)val);
1011 1011 i_dls_head_free(dhp);
1012 1012 } else {
1013 1013 mutex_enter(&dhp->dh_lock);
1014 1014 dhp->dh_removing = B_FALSE;
1015 1015 mutex_exit(&dhp->dh_lock);
1016 1016 }
1017 1017 }
↓ open down ↓ |
185 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX