1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012, Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2019 Joyent, Inc.
* Copyright 2025 Oxide Computer Company
*/
/*
* Data-Link Services Module
*/
#include <sys/stdbit.h>
#include <sys/strsun.h>
#include <sys/vlan.h>
#include <sys/dld_impl.h>
#include <sys/mac_client_priv.h>
int
dls_open(dls_link_t *dlp, dls_dl_handle_t ddh, dld_str_t *dsp)
{
zoneid_t zid = getzoneid();
boolean_t local;
int err;
/*
* Check whether this client belongs to the zone of this dlp. Note that
* a global zone client is allowed to open a local zone dlp.
*/
if (zid != GLOBAL_ZONEID && dlp->dl_zid != zid)
return (ENOENT);
/*
* mac_start() is required for non-legacy MACs to show accurate
* kstats even before the interface is brought up. For legacy
* drivers, this is not needed. Further, calling mac_start() for
* legacy drivers would make the shared-lower-stream to stay in
* the DL_IDLE state, which in turn causes performance regression.
*/
if (!mac_capab_get(dlp->dl_mh, MAC_CAPAB_LEGACY, NULL) &&
((err = mac_start(dlp->dl_mh)) != 0)) {
return (err);
}
local = (zid == dlp->dl_zid);
dlp->dl_zone_ref += (local ? 1 : 0);
/*
* Cache a copy of the MAC interface handle, a pointer to the
* immutable MAC info.
*/
dsp->ds_dlp = dlp;
dsp->ds_mh = dlp->dl_mh;
dsp->ds_mch = dlp->dl_mch;
dsp->ds_mip = dlp->dl_mip;
dsp->ds_ddh = ddh;
dsp->ds_local = local;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
return (0);
}
void
dls_close(dld_str_t *dsp)
{
dls_link_t *dlp = dsp->ds_dlp;
dls_multicst_addr_t *p;
dls_multicst_addr_t *nextp;
ASSERT(dsp->ds_datathr_cnt == 0);
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
if (dsp->ds_local)
dlp->dl_zone_ref--;
dsp->ds_local = B_FALSE;
/*
* Walk the list of multicast addresses, disabling each at the MAC.
* Note that we must remove multicast address before
* mac_unicast_remove() (called by dls_active_clear()) because
* mac_multicast_remove() relies on the unicast flows on the mac
* client.
*/
for (p = dsp->ds_dmap; p != NULL; p = nextp) {
(void) mac_multicast_remove(dsp->ds_mch, p->dma_addr);
nextp = p->dma_nextp;
kmem_free(p, sizeof (dls_multicst_addr_t));
}
dsp->ds_dmap = NULL;
dls_active_clear(dsp, B_TRUE);
/*
* If the dld_str_t is bound then unbind it.
*/
if (dsp->ds_dlstate == DL_IDLE) {
dls_unbind(dsp);
dsp->ds_dlstate = DL_UNBOUND;
}
/*
* If the MAC has been set in promiscuous mode then disable it.
* This needs to be done before resetting ds_rx.
*/
(void) dls_promisc(dsp, 0);
/*
* At this point we have cutoff inbound packet flow from the mac
* for this 'dsp'. The dls_link_remove above cut off packets meant
* for us and waited for upcalls to finish. Similarly the dls_promisc
* reset above waited for promisc callbacks to finish. Now we can
* safely reset ds_rx to NULL
*/
dsp->ds_rx = NULL;
dsp->ds_rx_arg = NULL;
dsp->ds_dlp = NULL;
if (!mac_capab_get(dsp->ds_mh, MAC_CAPAB_LEGACY, NULL))
mac_stop(dsp->ds_mh);
/*
* Release our reference to the dls_link_t allowing that to be
* destroyed if there are no more dls_impl_t.
*/
dls_link_rele(dlp);
}
int
dls_bind(dld_str_t *dsp, uint32_t sap)
{
uint32_t dls_sap;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
/*
* Check to see the value is legal for the media type.
*/
if (!mac_sap_verify(dsp->ds_mh, sap, &dls_sap))
return (EINVAL);
if (dsp->ds_promisc & DLS_PROMISC_SAP)
dls_sap = DLS_SAP_PROMISC;
/*
* Set up the dld_str_t to mark it as able to receive packets.
*/
dsp->ds_sap = sap;
/*
* The MAC layer does the VLAN demultiplexing and will only pass up
* untagged packets to non-promiscuous primary MAC clients. In order to
* support binding to the VLAN SAP, which is required by DLPI, DLS
* needs to get a copy of all tagged packets when the client binds to
* the VLAN SAP. We do this by registering a separate promiscuous
* callback for each DLS client binding to that SAP.
*
* Note: even though there are two promiscuous handles in dld_str_t,
* ds_mph is for the regular promiscuous mode, ds_vlan_mph is the handle
* to receive VLAN traffic when promiscuous mode is not on. Only one of
* them can be non-NULL at the same time, to avoid receiving duplicate
* copies of packets.
*/
if (sap == ETHERTYPE_VLAN && dsp->ds_promisc == 0) {
int err;
if (dsp->ds_vlan_mph != NULL)
return (EINVAL);
err = mac_promisc_add(dsp->ds_mch,
MAC_CLIENT_PROMISC_ALL, dls_rx_vlan_promisc, dsp,
&dsp->ds_vlan_mph, MAC_PROMISC_FLAGS_NO_PHYS);
if (err == 0 && dsp->ds_nonip &&
dsp->ds_dlp->dl_nonip_cnt++ == 0)
mac_rx_bypass_disable(dsp->ds_mch);
return (err);
}
/*
* Now bind the dld_str_t by adding it into the hash table in the
* dls_link_t.
*/
dls_link_add(dsp->ds_dlp, dls_sap, dsp);
if (dsp->ds_nonip && dsp->ds_dlp->dl_nonip_cnt++ == 0)
mac_rx_bypass_disable(dsp->ds_mch);
return (0);
}
void
dls_unbind(dld_str_t *dsp)
{
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
if (dsp->ds_nonip && --dsp->ds_dlp->dl_nonip_cnt == 0)
mac_rx_bypass_enable(dsp->ds_mch);
/*
* A VLAN SAP does not actually add itself to the STREAM head today.
* While we initially set up a VLAN handle below, it's possible that
* something else will have come in and clobbered it.
*/
if (dsp->ds_sap == ETHERTYPE_VLAN) {
if (dsp->ds_vlan_mph != NULL) {
mac_promisc_remove(dsp->ds_vlan_mph);
dsp->ds_vlan_mph = NULL;
}
return;
}
/*
* Unbind the dld_str_t by removing it from the hash table in the
* dls_link_t.
*/
dls_link_remove(dsp->ds_dlp, dsp);
dsp->ds_sap = 0;
}
/*
* In order to prevent promiscuous-mode processing with dsp->ds_promisc
* set to inaccurate values, this function sets dsp->ds_promisc with new
* flags. For enabling (mac_promisc_add), the flags are set prior to the
* actual enabling. For disabling (mac_promisc_remove), the flags are set
* after the actual disabling.
*/
int
dls_promisc(dld_str_t *dsp, uint32_t new_flags)
{
int err = 0;
uint32_t old_flags = dsp->ds_promisc;
const uint32_t option_flags = DLS_PROMISC_RX_ONLY |
DLS_PROMISC_INCOMING | DLS_PROMISC_OUTGOING;
uint32_t old_type = old_flags & ~option_flags;
uint32_t new_type = new_flags & ~option_flags;
mac_client_promisc_type_t mptype = MAC_CLIENT_PROMISC_ALL;
uint16_t mac_flags = 0;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
ASSERT(!(new_flags & ~(DLS_PROMISC_SAP | DLS_PROMISC_MULTI |
DLS_PROMISC_PHYS | option_flags)));
/*
* If the user has only requested DLS_PROMISC_MULTI then we need to make
* sure that they don't see all packets.
*/
if (new_type == DLS_PROMISC_MULTI)
mptype = MAC_CLIENT_PROMISC_MULTI;
/* Can choose only one of Rx-only or incoming/outoing. */
if (stdc_count_ones_ui(new_flags & option_flags) > 1)
return (EINVAL);
/*
* Look at new flags and figure out the correct mac promisc flags.
* If we've only requested DLS_PROMISC_SAP and not _MULTI or _PHYS,
* don't turn on physical promisc mode.
*
* Note that there are two MAC flags, MAC_PROMISC_FLAGS_NO_TX_LOOP and
* MAC_PROMISC_FLAGS_RX_ONLY, which are similar but subtly different.
* NO_TX_LOOP stops outgoing packets transmitted by a MAC client from
* being delivered to a promsic handler on the same client. Whereas
* RX_ONLY suppresses outgoing packets across all MAC clients. For
* backwards compatibility, we map DLS_PROMISC_RX_ONLY to
* MAC_PROMISC_FLAGS_NO_TX_LOOP and DLS_PROMISC_INCOMING/OUTGOING to
* MAC_PROMISC_FLAGS_RX_ONLY/MAC_PROMISC_FLAGS_TX_ONLY.
*/
if (new_flags & DLS_PROMISC_RX_ONLY)
mac_flags |= MAC_PROMISC_FLAGS_NO_TX_LOOP;
if (new_flags & DLS_PROMISC_INCOMING)
mac_flags |= MAC_PROMISC_FLAGS_RX_ONLY;
if (new_flags & DLS_PROMISC_OUTGOING)
mac_flags |= MAC_PROMISC_FLAGS_TX_ONLY;
if (new_type == DLS_PROMISC_SAP)
mac_flags |= MAC_PROMISC_FLAGS_NO_PHYS;
/*
* There are three cases we care about here with respect to MAC. Going
* from nothing to something, something to nothing, something to
* something where we need to change how we're getting stuff from mac.
* In the last case, as long as they're not equal, we need to assume
* something has changed and do something about it.
*/
if (old_type == 0 && new_type == 0) {
/*
* If there are only option flags in the old and new flags
* then there is no need to talk to MAC. Just update the flags.
*/
dsp->ds_promisc = new_flags;
} else if (old_type == 0 && new_type != 0) {
dsp->ds_promisc = new_flags;
err = mac_promisc_add(dsp->ds_mch, mptype,
dls_rx_promisc, dsp, &dsp->ds_mph, mac_flags);
if (err != 0) {
dsp->ds_promisc = old_flags;
return (err);
}
/* Remove vlan promisc handle to avoid sending dup copy up */
if (dsp->ds_vlan_mph != NULL) {
mac_promisc_remove(dsp->ds_vlan_mph);
dsp->ds_vlan_mph = NULL;
}
} else if (old_type != 0 && new_type == 0) {
ASSERT(dsp->ds_mph != NULL);
mac_promisc_remove(dsp->ds_mph);
dsp->ds_promisc = new_flags;
dsp->ds_mph = NULL;
if (dsp->ds_sap == ETHERTYPE_VLAN &&
dsp->ds_dlstate != DL_UNBOUND) {
if (dsp->ds_vlan_mph != NULL)
return (EINVAL);
err = mac_promisc_add(dsp->ds_mch,
MAC_CLIENT_PROMISC_ALL, dls_rx_vlan_promisc, dsp,
&dsp->ds_vlan_mph, MAC_PROMISC_FLAGS_NO_PHYS);
}
} else if (new_flags != old_flags) {
ASSERT(dsp->ds_mph != NULL);
mac_promisc_remove(dsp->ds_mph);
/* Honors both after-remove and before-add semantics! */
dsp->ds_promisc = new_flags;
err = mac_promisc_add(dsp->ds_mch, mptype,
dls_rx_promisc, dsp, &dsp->ds_mph, mac_flags);
if (err != 0)
dsp->ds_promisc = old_flags;
}
return (err);
}
int
dls_multicst_add(dld_str_t *dsp, const uint8_t *addr)
{
int err;
dls_multicst_addr_t **pp;
dls_multicst_addr_t *p;
uint_t addr_length;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
/*
* Check whether the address is in the list of enabled addresses for
* this dld_str_t.
*/
addr_length = dsp->ds_mip->mi_addr_length;
/*
* Protect against concurrent access of ds_dmap by data threads using
* ds_rw_lock. The mac perimeter serializes the dls_multicst_add and
* remove operations. Dropping the ds_rw_lock across mac calls is thus
* ok and is also required by the locking protocol.
*/
rw_enter(&dsp->ds_rw_lock, RW_WRITER);
for (pp = &(dsp->ds_dmap); (p = *pp) != NULL; pp = &(p->dma_nextp)) {
if (bcmp(addr, p->dma_addr, addr_length) == 0) {
/*
* It is there so there's nothing to do.
*/
err = 0;
goto done;
}
}
/*
* Allocate a new list item and add it to the list.
*/
p = kmem_zalloc(sizeof (dls_multicst_addr_t), KM_SLEEP);
bcopy(addr, p->dma_addr, addr_length);
*pp = p;
rw_exit(&dsp->ds_rw_lock);
/*
* Enable the address at the MAC.
*/
err = mac_multicast_add(dsp->ds_mch, addr);
if (err == 0)
return (0);
/* Undo the operation as it has failed */
rw_enter(&dsp->ds_rw_lock, RW_WRITER);
ASSERT(*pp == p && p->dma_nextp == NULL);
*pp = NULL;
kmem_free(p, sizeof (dls_multicst_addr_t));
done:
rw_exit(&dsp->ds_rw_lock);
return (err);
}
int
dls_multicst_remove(dld_str_t *dsp, const uint8_t *addr)
{
dls_multicst_addr_t **pp;
dls_multicst_addr_t *p;
uint_t addr_length;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
/*
* Find the address in the list of enabled addresses for this
* dld_str_t.
*/
addr_length = dsp->ds_mip->mi_addr_length;
/*
* Protect against concurrent access to ds_dmap by data threads using
* ds_rw_lock. The mac perimeter serializes the dls_multicst_add and
* remove operations. Dropping the ds_rw_lock across mac calls is thus
* ok and is also required by the locking protocol.
*/
rw_enter(&dsp->ds_rw_lock, RW_WRITER);
for (pp = &(dsp->ds_dmap); (p = *pp) != NULL; pp = &(p->dma_nextp)) {
if (bcmp(addr, p->dma_addr, addr_length) == 0)
break;
}
/*
* If we walked to the end of the list then the given address is
* not currently enabled for this dld_str_t.
*/
if (p == NULL) {
rw_exit(&dsp->ds_rw_lock);
return (ENOENT);
}
/*
* Remove the address from the list.
*/
*pp = p->dma_nextp;
rw_exit(&dsp->ds_rw_lock);
/*
* Disable the address at the MAC.
*/
mac_multicast_remove(dsp->ds_mch, addr);
kmem_free(p, sizeof (dls_multicst_addr_t));
return (0);
}
mblk_t *
dls_header(dld_str_t *dsp, const uint8_t *addr, uint16_t sap, uint_t pri,
mblk_t **payloadp)
{
uint16_t vid;
size_t extra_len;
uint16_t mac_sap;
mblk_t *mp, *payload;
boolean_t is_ethernet = (dsp->ds_mip->mi_media == DL_ETHER);
struct ether_vlan_header *evhp;
vid = mac_client_vid(dsp->ds_mch);
payload = (payloadp == NULL) ? NULL : (*payloadp);
/*
* In the case of Ethernet, we need to tell mac_header() if we need
* extra room beyond the Ethernet header for a VLAN header. We'll
* need to add a VLAN header if this isn't an ETHERTYPE_VLAN listener
* (because such streams will be handling VLAN headers on their own)
* and one of the following conditions is satisfied:
*
* - This is a VLAN stream
* - This is a physical stream, the priority is not 0, and user
* priority tagging is allowed.
*/
if (is_ethernet && sap != ETHERTYPE_VLAN &&
(vid != VLAN_ID_NONE ||
(pri != 0 && dsp->ds_dlp->dl_tagmode != LINK_TAGMODE_VLANONLY))) {
extra_len = sizeof (struct ether_vlan_header) -
sizeof (struct ether_header);
mac_sap = ETHERTYPE_VLAN;
} else {
extra_len = 0;
mac_sap = sap;
}
mp = mac_header(dsp->ds_mh, addr, mac_sap, payload, extra_len);
if (mp == NULL)
return (NULL);
if ((vid == VLAN_ID_NONE && (pri == 0 ||
dsp->ds_dlp->dl_tagmode == LINK_TAGMODE_VLANONLY)) || !is_ethernet)
return (mp);
/*
* Fill in the tag information.
*/
ASSERT(MBLKL(mp) == sizeof (struct ether_header));
if (extra_len != 0) {
mp->b_wptr += extra_len;
evhp = (struct ether_vlan_header *)mp->b_rptr;
evhp->ether_tci = htons(VLAN_TCI(pri, ETHER_CFI, vid));
evhp->ether_type = htons(sap);
} else {
/*
* The stream is ETHERTYPE_VLAN listener, so its VLAN tag is
* in the payload. Update the priority.
*/
struct ether_vlan_extinfo *extinfo;
size_t len = sizeof (struct ether_vlan_extinfo);
ASSERT(sap == ETHERTYPE_VLAN);
ASSERT(payload != NULL);
if ((DB_REF(payload) > 1) || (MBLKL(payload) < len)) {
mblk_t *newmp;
/*
* Because some DLS consumers only check the db_ref
* count of the first mblk, we pullup 'payload' into
* a single mblk.
*/
newmp = msgpullup(payload, -1);
if ((newmp == NULL) || (MBLKL(newmp) < len)) {
freemsg(newmp);
freemsg(mp);
return (NULL);
} else {
freemsg(payload);
*payloadp = payload = newmp;
}
}
extinfo = (struct ether_vlan_extinfo *)payload->b_rptr;
extinfo->ether_tci = htons(VLAN_TCI(pri, ETHER_CFI,
VLAN_ID(ntohs(extinfo->ether_tci))));
}
return (mp);
}
void
dls_rx_set(dld_str_t *dsp, dls_rx_t rx, void *arg)
{
mutex_enter(&dsp->ds_lock);
dsp->ds_rx = rx;
dsp->ds_rx_arg = arg;
mutex_exit(&dsp->ds_lock);
}
static boolean_t
dls_accept_common(dld_str_t *dsp, mac_header_info_t *mhip, dls_rx_t *ds_rx,
void **ds_rx_arg, boolean_t promisc, boolean_t promisc_loopback)
{
dls_multicst_addr_t *dmap;
size_t addr_length = dsp->ds_mip->mi_addr_length;
/*
* We must not accept packets if the dld_str_t is not marked as bound
* or is being removed.
*/
if (dsp->ds_dlstate != DL_IDLE)
goto refuse;
if (dsp->ds_promisc != 0) {
/*
* Filter out packets that arrived from the data path
* (i_dls_link_rx) when promisc mode is on. We need to correlate
* the ds_promisc flags with the mac header destination type. If
* only DLS_PROMISC_MULTI is enabled, we need to only reject
* multicast packets as those are the only ones which filter up
* the promiscuous path. If we have DLS_PROMISC_PHYS or
* DLS_PROMISC_SAP set, then we know that we'll be seeing
* everything, so we should drop it now.
*/
if (!promisc && !(dsp->ds_promisc == DLS_PROMISC_MULTI &&
mhip->mhi_dsttype != MAC_ADDRTYPE_MULTICAST))
goto refuse;
/*
* If the dls_impl_t is in 'all physical' mode then
* always accept.
*/
if (dsp->ds_promisc & DLS_PROMISC_PHYS)
goto accept;
/*
* Loopback packets i.e. packets sent out by DLS on a given
* mac end point, will be accepted back by DLS on loopback
* from the mac, only in the 'all physical' mode which has been
* covered by the previous check above
*/
if (promisc_loopback)
goto refuse;
}
switch (mhip->mhi_dsttype) {
case MAC_ADDRTYPE_UNICAST:
case MAC_ADDRTYPE_BROADCAST:
/*
* We can accept unicast and broadcast packets because
* filtering is already done by the mac layer.
*/
goto accept;
case MAC_ADDRTYPE_MULTICAST:
/*
* Additional filtering is needed for multicast addresses
* because different streams may be interested in different
* addresses.
*/
if (dsp->ds_promisc & DLS_PROMISC_MULTI)
goto accept;
rw_enter(&dsp->ds_rw_lock, RW_READER);
for (dmap = dsp->ds_dmap; dmap != NULL;
dmap = dmap->dma_nextp) {
if (memcmp(mhip->mhi_daddr, dmap->dma_addr,
addr_length) == 0) {
rw_exit(&dsp->ds_rw_lock);
goto accept;
}
}
rw_exit(&dsp->ds_rw_lock);
break;
}
refuse:
return (B_FALSE);
accept:
/*
* the returned ds_rx and ds_rx_arg will always be in sync.
*/
mutex_enter(&dsp->ds_lock);
*ds_rx = dsp->ds_rx;
*ds_rx_arg = dsp->ds_rx_arg;
mutex_exit(&dsp->ds_lock);
return (B_TRUE);
}
/* ARGSUSED */
boolean_t
dls_accept(dld_str_t *dsp, mac_header_info_t *mhip, dls_rx_t *ds_rx,
void **ds_rx_arg)
{
return (dls_accept_common(dsp, mhip, ds_rx, ds_rx_arg, B_FALSE,
B_FALSE));
}
boolean_t
dls_accept_promisc(dld_str_t *dsp, mac_header_info_t *mhip, dls_rx_t *ds_rx,
void **ds_rx_arg, boolean_t loopback)
{
return (dls_accept_common(dsp, mhip, ds_rx, ds_rx_arg, B_TRUE,
loopback));
}
int
dls_mac_active_set(dls_link_t *dlp)
{
int err = 0;
/*
* First client; add the primary unicast address.
*/
if (dlp->dl_nactive == 0) {
/*
* First client; add the primary unicast address.
*/
mac_diag_t diag;
/* request the primary MAC address */
if ((err = mac_unicast_add(dlp->dl_mch, NULL,
MAC_UNICAST_PRIMARY | MAC_UNICAST_TAG_DISABLE |
MAC_UNICAST_DISABLE_TX_VID_CHECK, &dlp->dl_mah,
VLAN_ID_NONE, &diag)) != 0) {
return (err);
}
/*
* Set the function to start receiving packets.
*/
mac_rx_set(dlp->dl_mch, i_dls_link_rx, dlp);
}
dlp->dl_nactive++;
return (0);
}
void
dls_mac_active_clear(dls_link_t *dlp)
{
if (--dlp->dl_nactive == 0) {
ASSERT(dlp->dl_mah != NULL);
(void) mac_unicast_remove(dlp->dl_mch, dlp->dl_mah);
dlp->dl_mah = NULL;
mac_rx_clear(dlp->dl_mch);
}
}
int
dls_active_set(dld_str_t *dsp)
{
int err = 0;
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
if (dsp->ds_passivestate == DLD_PASSIVE)
return (0);
/* If we're already active, then there's nothing more to do. */
if ((dsp->ds_nactive == 0) &&
((err = dls_mac_active_set(dsp->ds_dlp)) != 0)) {
/* except for ENXIO all other errors are mapped to EBUSY */
if (err != ENXIO)
return (EBUSY);
return (err);
}
dsp->ds_passivestate = DLD_ACTIVE;
dsp->ds_nactive++;
return (0);
}
/*
* Note that dls_active_set() is called whenever an active operation
* (DL_BIND_REQ, DL_ENABMULTI_REQ ...) is processed and
* dls_active_clear(dsp, B_FALSE) is called whenever the active operation
* is being undone (DL_UNBIND_REQ, DL_DISABMULTI_REQ ...). In some cases,
* a stream is closed without every active operation being undone and we
* need to clear all the "active" states by calling
* dls_active_clear(dsp, B_TRUE).
*/
void
dls_active_clear(dld_str_t *dsp, boolean_t all)
{
ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
if (dsp->ds_passivestate == DLD_PASSIVE)
return;
if (all && dsp->ds_nactive == 0)
return;
ASSERT(dsp->ds_nactive > 0);
dsp->ds_nactive -= (all ? dsp->ds_nactive : 1);
if (dsp->ds_nactive != 0)
return;
ASSERT(dsp->ds_passivestate == DLD_ACTIVE);
dls_mac_active_clear(dsp->ds_dlp);
dsp->ds_passivestate = DLD_UNINITIALIZED;
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2019 Joyent, Inc.
*/
/*
* Data-Link Services Module
*/
#include <sys/sysmacros.h>
#include <sys/strsubr.h>
#include <sys/strsun.h>
#include <sys/vlan.h>
#include <sys/dld_impl.h>
#include <sys/sdt.h>
#include <sys/atomic.h>
#include <sys/sysevent.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/sysevent/datalink.h>
static kmem_cache_t *i_dls_link_cachep;
mod_hash_t *i_dls_link_hash;
static uint_t i_dls_link_count;
#define LINK_HASHSZ 67 /* prime */
#define IMPL_HASHSZ 67 /* prime */
/*
* Construct a hash key from the DLSAP value.
*/
#define MAKE_KEY(_sap) \
((mod_hash_key_t)(uintptr_t)((_sap) << VLAN_ID_SIZE))
#define DLS_STRIP_PADDING(pktsize, p) { \
if (pktsize != 0) { \
ssize_t delta = pktsize - msgdsize(p); \
\
if (delta < 0) \
(void) adjmsg(p, delta); \
} \
}
/*
* Private functions.
*/
/*ARGSUSED*/
static int
i_dls_link_constructor(void *buf, void *arg, int kmflag)
{
dls_link_t *dlp = buf;
char name[MAXNAMELEN];
bzero(buf, sizeof (dls_link_t));
(void) snprintf(name, MAXNAMELEN, "dls_link_t_%p_hash", buf);
dlp->dl_str_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
mod_hash_null_valdtor);
return (0);
}
/*ARGSUSED*/
static void
i_dls_link_destructor(void *buf, void *arg)
{
dls_link_t *dlp = buf;
ASSERT(dlp->dl_ref == 0);
ASSERT(dlp->dl_mh == NULL);
ASSERT(dlp->dl_mah == NULL);
ASSERT(dlp->dl_unknowns == 0);
mod_hash_destroy_idhash(dlp->dl_str_hash);
dlp->dl_str_hash = NULL;
}
/*
* - Parse the mac header information of the given packet.
* - Strip the padding and skip over the header. Note that because some
* DLS consumers only check the db_ref count of the first mblk, we
* pullup the message into a single mblk. Because the original message
* is freed as the result of message pulling up, mac_vlan_header_info()
* is called again to update the mhi_saddr and mhi_daddr pointers in the
* mhip. Further, the mac_vlan_header_info() function ensures that the
* size of the pulled message is greater than the MAC header size,
* therefore we can directly advance b_rptr to point at the payload.
*
* We choose to use a macro for performance reasons.
*/
#define DLS_PREPARE_PKT(mh, mp, mhip, err) { \
mblk_t *nextp = (mp)->b_next; \
if (((err) = mac_vlan_header_info((mh), (mp), (mhip))) == 0) { \
DLS_STRIP_PADDING((mhip)->mhi_pktsize, (mp)); \
if (MBLKL((mp)) < (mhip)->mhi_hdrsize) { \
mblk_t *newmp; \
if ((newmp = msgpullup((mp), -1)) == NULL) { \
(err) = EINVAL; \
} else { \
(mp)->b_next = NULL; \
freemsg((mp)); \
(mp) = newmp; \
VERIFY(mac_vlan_header_info((mh), \
(mp), (mhip)) == 0); \
(mp)->b_next = nextp; \
(mp)->b_rptr += (mhip)->mhi_hdrsize; \
} \
} else { \
(mp)->b_rptr += (mhip)->mhi_hdrsize; \
} \
} \
}
/*
* Truncate the chain starting at mp such that all packets in the chain
* have identical source and destination addresses, saps, and tag types
* (see below). It returns a pointer to the mblk following the chain,
* NULL if there is no further packet following the processed chain.
* The countp argument is set to the number of valid packets in the chain.
* Note that the whole MAC header (including the VLAN tag if any) in each
* packet will be stripped.
*/
static mblk_t *
i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, const mac_header_info_t *mhip,
uint_t *countp)
{
mblk_t *prevp;
uint_t npacket = 1;
size_t addr_size = dlp->dl_mip->mi_addr_length;
uint16_t vid = VLAN_ID(mhip->mhi_tci);
uint16_t pri = VLAN_PRI(mhip->mhi_tci);
/*
* Compare with subsequent headers until we find one that has
* differing header information. After checking each packet
* strip padding and skip over the header.
*/
for (prevp = mp; (mp = mp->b_next) != NULL; prevp = mp) {
mac_header_info_t cmhi;
uint16_t cvid, cpri;
int err;
DLS_PREPARE_PKT(dlp->dl_mh, mp, &cmhi, err);
if (err != 0)
break;
prevp->b_next = mp;
/*
* The source, destination, sap, vlan tag must all match in
* a given subchain.
*/
if (mhip->mhi_saddr == NULL || cmhi.mhi_saddr == NULL ||
memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
mhip->mhi_bindsap != cmhi.mhi_bindsap) {
/*
* Note that we don't need to restore the padding.
*/
mp->b_rptr -= cmhi.mhi_hdrsize;
break;
}
cvid = VLAN_ID(cmhi.mhi_tci);
cpri = VLAN_PRI(cmhi.mhi_tci);
/*
* There are several types of packets. Packets don't match
* if they are classified to different type or if they are
* VLAN packets but belong to different VLANs:
*
* packet type tagged vid pri
* ---------------------------------------------------------
* untagged No zero zero
* VLAN packets Yes non-zero -
* priority tagged Yes zero non-zero
* 0 tagged Yes zero zero
*/
if ((mhip->mhi_istagged != cmhi.mhi_istagged) ||
(vid != cvid) || ((vid == VLAN_ID_NONE) &&
(((pri == 0) && (cpri != 0)) ||
((pri != 0) && (cpri == 0))))) {
mp->b_rptr -= cmhi.mhi_hdrsize;
break;
}
npacket++;
}
/*
* Break the chain at this point and return a pointer to the next
* sub-chain.
*/
prevp->b_next = NULL;
*countp = npacket;
return (mp);
}
/* ARGSUSED */
static int
i_dls_head_hold(mod_hash_key_t key, mod_hash_val_t val)
{
dls_head_t *dhp = (dls_head_t *)val;
/*
* The lock order is mod_hash's internal lock -> dh_lock as in the
* call to i_dls_link_rx -> mod_hash_find_cb_rval -> i_dls_head_hold
*/
mutex_enter(&dhp->dh_lock);
if (dhp->dh_removing) {
mutex_exit(&dhp->dh_lock);
return (-1);
}
dhp->dh_ref++;
mutex_exit(&dhp->dh_lock);
return (0);
}
void
i_dls_head_rele(dls_head_t *dhp)
{
mutex_enter(&dhp->dh_lock);
dhp->dh_ref--;
if (dhp->dh_ref == 0 && dhp->dh_removing != 0)
cv_broadcast(&dhp->dh_cv);
mutex_exit(&dhp->dh_lock);
}
static dls_head_t *
i_dls_head_alloc(mod_hash_key_t key)
{
dls_head_t *dhp;
dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
dhp->dh_key = key;
return (dhp);
}
static void
i_dls_head_free(dls_head_t *dhp)
{
ASSERT(dhp->dh_ref == 0);
kmem_free(dhp, sizeof (dls_head_t));
}
/*
* Try to send mp up to the streams of the given sap. Return the
* number of streams which accepted this message, or 0 if no streams
* accepted the message.
*
* Note that this function copies the message chain and the original
* mp remains valid after this function returns.
*/
static uint_t
i_dls_link_rx_func(dls_link_t *dlp, mac_resource_handle_t mrh,
mac_header_info_t *mhip, mblk_t *mp, uint32_t sap,
boolean_t (*acceptfunc)())
{
mod_hash_t *hash = dlp->dl_str_hash;
mod_hash_key_t key;
dls_head_t *dhp;
dld_str_t *dsp;
mblk_t *nmp;
dls_rx_t ds_rx;
void *ds_rx_arg;
uint_t naccepted = 0;
int rval;
/*
* Construct a hash key from the DLSAP.
*/
key = MAKE_KEY(sap);
/*
* Search the hash table for a dld_str_t eligible to receive a
* packet chain for this DLSAP. The mod hash's internal lock
* serializes find/insert/remove from the mod hash list.
* Incrementing the dh_ref (while holding the mod hash lock)
* ensures dls_link_remove will wait for the upcall to finish.
*/
if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
i_dls_head_hold, &rval) != 0 || (rval != 0)) {
return (0);
}
/*
* Find all dld_str_t that will accept the sub-chain.
*/
for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next) {
if (!acceptfunc(dsp, mhip, &ds_rx, &ds_rx_arg))
continue;
/*
* We have at least one acceptor.
*/
naccepted++;
/*
* There will normally be at least one more dld_str_t
* (since we've yet to check for non-promiscuous
* dld_str_t) so dup the sub-chain.
*/
if ((nmp = copymsgchain(mp)) != NULL)
ds_rx(ds_rx_arg, mrh, nmp, mhip);
}
/*
* Release the hold on the dld_str_t chain now that we have
* finished walking it.
*/
i_dls_head_rele(dhp);
return (naccepted);
}
/* ARGSUSED */
void
i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
boolean_t loopback)
{
dls_link_t *dlp = arg;
mod_hash_t *hash = dlp->dl_str_hash;
mblk_t *nextp;
mac_header_info_t mhi;
dls_head_t *dhp;
dld_str_t *dsp;
dld_str_t *ndsp;
mblk_t *nmp;
mod_hash_key_t key;
uint_t npacket;
boolean_t accepted;
dls_rx_t ds_rx, nds_rx;
void *ds_rx_arg, *nds_rx_arg;
uint16_t vid;
int err, rval;
/*
* Walk the packet chain.
*/
for (; mp != NULL; mp = nextp) {
/*
* Wipe the accepted state.
*/
accepted = B_FALSE;
DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
if (err != 0) {
atomic_inc_32(&(dlp->dl_unknowns));
nextp = mp->b_next;
mp->b_next = NULL;
freemsg(mp);
continue;
}
/*
* Grab the longest sub-chain we can process as a single
* unit.
*/
nextp = i_dls_link_subchain(dlp, mp, &mhi, &npacket);
ASSERT(npacket != 0);
vid = VLAN_ID(mhi.mhi_tci);
/*
* This condition is true only when a sun4v vsw client
* is on the scene; as it is the only type of client
* that multiplexes VLANs on a single client instance.
* All other types of clients have one VLAN per client
* instance. In that case, MAC strips the VLAN tag
* before delivering it to DLS (see mac_rx_deliver()).
*/
if (mhi.mhi_istagged) {
/*
* If it is tagged traffic, send it upstream to
* all dld_str_t which are attached to the physical
* link and bound to SAP 0x8100.
*/
if (i_dls_link_rx_func(dlp, mrh, &mhi, mp,
ETHERTYPE_VLAN, dls_accept) > 0) {
accepted = B_TRUE;
}
/*
* Don't pass the packets up if they are tagged
* packets and:
* - their VID and priority are both zero and the
* original packet isn't using the PVID (invalid
* packets).
* - their sap is ETHERTYPE_VLAN and their VID is
* zero as they have already been sent upstreams.
*/
if ((vid == VLAN_ID_NONE && !mhi.mhi_ispvid &&
VLAN_PRI(mhi.mhi_tci) == 0) ||
(mhi.mhi_bindsap == ETHERTYPE_VLAN &&
vid == VLAN_ID_NONE)) {
freemsgchain(mp);
goto loop;
}
}
/*
* Construct a hash key from the DLSAP.
*/
key = MAKE_KEY(mhi.mhi_bindsap);
/*
* Search the hash table for dld_str_t eligible to receive
* a packet chain for this DLSAP.
*/
if (mod_hash_find_cb_rval(hash, key, (mod_hash_val_t *)&dhp,
i_dls_head_hold, &rval) != 0 || (rval != 0)) {
freemsgchain(mp);
goto loop;
}
/*
* Find the first dld_str_t that will accept the sub-chain.
*/
for (dsp = dhp->dh_list; dsp != NULL; dsp = dsp->ds_next)
if (dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
break;
/*
* If we did not find any dld_str_t willing to accept the
* sub-chain then throw it away.
*/
if (dsp == NULL) {
i_dls_head_rele(dhp);
freemsgchain(mp);
goto loop;
}
/*
* We have at least one acceptor.
*/
accepted = B_TRUE;
for (;;) {
/*
* Find the next dld_str_t that will accept the
* sub-chain.
*/
for (ndsp = dsp->ds_next; ndsp != NULL;
ndsp = ndsp->ds_next)
if (dls_accept(ndsp, &mhi, &nds_rx,
&nds_rx_arg))
break;
/*
* If there are no more dld_str_t that are willing
* to accept the sub-chain then we don't need to dup
* it before handing it to the current one.
*/
if (ndsp == NULL) {
ds_rx(ds_rx_arg, mrh, mp, &mhi);
/*
* Since there are no more dld_str_t, we're
* done.
*/
break;
}
/*
* There are more dld_str_t so dup the sub-chain.
*/
if ((nmp = copymsgchain(mp)) != NULL)
ds_rx(ds_rx_arg, mrh, nmp, &mhi);
dsp = ndsp;
ds_rx = nds_rx;
ds_rx_arg = nds_rx_arg;
}
/*
* Release the hold on the dld_str_t chain now that we have
* finished walking it.
*/
i_dls_head_rele(dhp);
loop:
/*
* If there were no acceptors then add the packet count to the
* 'unknown' count.
*/
if (!accepted)
atomic_add_32(&(dlp->dl_unknowns), npacket);
}
}
/* ARGSUSED */
void
dls_rx_vlan_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
boolean_t loopback)
{
dld_str_t *dsp = arg;
dls_link_t *dlp = dsp->ds_dlp;
mac_header_info_t mhi;
dls_rx_t ds_rx;
void *ds_rx_arg;
int err;
DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
if (err != 0)
goto drop;
/*
* If there is promiscuous handle for vlan, we filter out the untagged
* pkts and pkts that are not for the primary unicast address.
*/
if (dsp->ds_vlan_mph != NULL) {
uint8_t prim_addr[MAXMACADDRLEN];
size_t addr_length = dsp->ds_mip->mi_addr_length;
if (!(mhi.mhi_istagged))
goto drop;
ASSERT(dsp->ds_mh != NULL);
mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)prim_addr);
if (memcmp(mhi.mhi_daddr, prim_addr, addr_length) != 0)
goto drop;
if (!dls_accept(dsp, &mhi, &ds_rx, &ds_rx_arg))
goto drop;
ds_rx(ds_rx_arg, NULL, mp, &mhi);
return;
}
drop:
atomic_inc_32(&dlp->dl_unknowns);
freemsg(mp);
}
/* ARGSUSED */
void
dls_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
boolean_t loopback)
{
dld_str_t *dsp = arg;
dls_link_t *dlp = dsp->ds_dlp;
mac_header_info_t mhi;
dls_rx_t ds_rx;
void *ds_rx_arg;
int err;
dls_head_t *dhp;
mod_hash_key_t key;
/*
* We expect to deal with only a single packet.
*/
ASSERT3P(mp->b_next, ==, NULL);
DLS_PREPARE_PKT(dlp->dl_mh, mp, &mhi, err);
if (err != 0)
goto drop;
/*
* In order to filter out sap pkt that no dls channel listens, search
* the hash table trying to find a dld_str_t eligible to receive the pkt
*/
if ((dsp->ds_promisc & DLS_PROMISC_SAP) == 0) {
key = MAKE_KEY(mhi.mhi_bindsap);
if (mod_hash_find(dsp->ds_dlp->dl_str_hash, key,
(mod_hash_val_t *)&dhp) != 0)
goto drop;
}
if (!dls_accept_promisc(dsp, &mhi, &ds_rx, &ds_rx_arg, loopback))
goto drop;
ds_rx(ds_rx_arg, NULL, mp, &mhi);
return;
drop:
atomic_inc_32(&dlp->dl_unknowns);
freemsg(mp);
}
/*
* We'd like to notify via sysevents that a link state change has occurred.
* There are a couple of challenges associated with this. The first is that if
* the link is flapping a lot, we may not see an accurate state when we launch
* the notification, we're told it changed, not what it changed to.
*
* The next problem is that all of the information that a user has associated
* with this device is the exact opposite of what we have on the dls_link_t. We
* have the name of the mac device, which has no bearing on what users see.
* Likewise, we don't have the datalink id either. So we're going to have to get
* this from dls.
*
* This is all further complicated by the fact that this could be going on in
* another thread at the same time as someone is tearing down the dls_link_t
* that we're associated with. We need to be careful not to grab the mac
* perimeter, otherwise we stand a good chance of deadlock.
*/
static void
dls_link_notify(void *arg, mac_notify_type_t type)
{
dls_link_t *dlp = arg;
dls_dl_handle_t dhp;
nvlist_t *nvp;
sysevent_t *event;
sysevent_id_t eid;
if (type != MAC_NOTE_LINK && type != MAC_NOTE_LOWLINK)
return;
/*
* If we can't find a devnet handle for this link, then there is no user
* knowable device for this at the moment and there's nothing we can
* really share with them that will make sense.
*/
if (dls_devnet_hold_tmp_by_link(dlp, &dhp) != 0)
return;
/*
* Because we're attaching this nvlist_t to the sysevent, it'll get
* cleaned up when we call sysevent_free.
*/
VERIFY(nvlist_alloc(&nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
VERIFY(nvlist_add_int32(nvp, DATALINK_EV_LINK_ID,
dls_devnet_linkid(dhp)) == 0);
VERIFY(nvlist_add_string(nvp, DATALINK_EV_LINK_NAME,
dls_devnet_link(dhp)) == 0);
VERIFY(nvlist_add_int32(nvp, DATALINK_EV_ZONE_ID,
dls_devnet_getzid(dhp)) == 0);
dls_devnet_rele_tmp(dhp);
event = sysevent_alloc(EC_DATALINK, ESC_DATALINK_LINK_STATE,
ILLUMOS_KERN_PUB"dls", SE_SLEEP);
VERIFY(event != NULL);
(void) sysevent_attach_attributes(event, (sysevent_attr_list_t *)nvp);
(void) log_sysevent(event, SE_SLEEP, &eid);
sysevent_free(event);
}
static void
i_dls_link_destroy(dls_link_t *dlp)
{
ASSERT(dlp->dl_nactive == 0);
ASSERT(dlp->dl_impl_count == 0);
ASSERT(dlp->dl_zone_ref == 0);
/*
* Free the structure back to the cache.
*/
if (dlp->dl_mnh != NULL)
mac_notify_remove(dlp->dl_mnh, B_TRUE);
if (dlp->dl_mch != NULL)
mac_client_close(dlp->dl_mch, 0);
if (dlp->dl_mh != NULL) {
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
mac_close(dlp->dl_mh);
}
dlp->dl_mh = NULL;
dlp->dl_mch = NULL;
dlp->dl_mip = NULL;
dlp->dl_mnh = NULL;
dlp->dl_unknowns = 0;
dlp->dl_nonip_cnt = 0;
kmem_cache_free(i_dls_link_cachep, dlp);
}
static int
i_dls_link_create(const char *name, dls_link_t **dlpp)
{
dls_link_t *dlp;
int err;
/*
* Allocate a new dls_link_t structure.
*/
dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
/*
* Name the dls_link_t after the MAC interface it represents.
*/
(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
/*
* First reference; hold open the MAC interface.
*/
ASSERT(dlp->dl_mh == NULL);
err = mac_open(dlp->dl_name, &dlp->dl_mh);
if (err != 0)
goto bail;
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
dlp->dl_mip = mac_info(dlp->dl_mh);
/* DLS is the "primary" MAC client */
ASSERT(dlp->dl_mch == NULL);
err = mac_client_open(dlp->dl_mh, &dlp->dl_mch, NULL,
MAC_OPEN_FLAGS_USE_DATALINK_NAME);
if (err != 0)
goto bail;
dlp->dl_mnh = mac_notify_add(dlp->dl_mh, dls_link_notify, dlp);
DTRACE_PROBE2(dls__primary__client, char *, dlp->dl_name, void *,
dlp->dl_mch);
*dlpp = dlp;
return (0);
bail:
i_dls_link_destroy(dlp);
return (err);
}
/*
* Module initialization functions.
*/
void
dls_link_init(void)
{
/*
* Create a kmem_cache of dls_link_t structures.
*/
i_dls_link_cachep = kmem_cache_create("dls_link_cache",
sizeof (dls_link_t), 0, i_dls_link_constructor,
i_dls_link_destructor, NULL, NULL, NULL, 0);
ASSERT(i_dls_link_cachep != NULL);
/*
* Create a dls_link_t hash table and associated lock.
*/
i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
i_dls_link_count = 0;
}
int
dls_link_fini(void)
{
if (i_dls_link_count > 0)
return (EBUSY);
/*
* Destroy the kmem_cache.
*/
kmem_cache_destroy(i_dls_link_cachep);
/*
* Destroy the hash table and associated lock.
*/
mod_hash_destroy_hash(i_dls_link_hash);
return (0);
}
/*
* Exported functions.
*/
static int
dls_link_hold_common(const char *name, dls_link_t **dlpp, boolean_t create)
{
dls_link_t *dlp;
int err;
/*
* Look up a dls_link_t corresponding to the given macname in the
* global hash table. The i_dls_link_hash itself is protected by the
* mod_hash package's internal lock which synchronizes
* find/insert/remove into the global mod_hash list. Assumes that
* inserts and removes are single threaded on a per mac end point
* by the mac perimeter.
*/
if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
(mod_hash_val_t *)&dlp)) == 0)
goto done;
ASSERT(err == MH_ERR_NOTFOUND);
if (!create)
return (ENOENT);
/*
* We didn't find anything so we need to create one.
*/
if ((err = i_dls_link_create(name, &dlp)) != 0)
return (err);
/*
* Insert the dls_link_t.
*/
err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)dlp->dl_name,
(mod_hash_val_t)dlp);
ASSERT(err == 0);
atomic_inc_32(&i_dls_link_count);
ASSERT(i_dls_link_count != 0);
done:
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
/*
* Bump the reference count and hand back the reference.
*/
dlp->dl_ref++;
*dlpp = dlp;
return (0);
}
int
dls_link_hold_create(const char *name, dls_link_t **dlpp)
{
return (dls_link_hold_common(name, dlpp, B_TRUE));
}
int
dls_link_hold(const char *name, dls_link_t **dlpp)
{
return (dls_link_hold_common(name, dlpp, B_FALSE));
}
dev_info_t *
dls_link_devinfo(dev_t dev)
{
dls_link_t *dlp;
dev_info_t *dip;
char macname[MAXNAMELEN];
char *drv;
mac_perim_handle_t mph;
if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
return (NULL);
(void) snprintf(macname, MAXNAMELEN, "%s%d", drv,
DLS_MINOR2INST(getminor(dev)));
/*
* The code below assumes that the name constructed above is the
* macname. This is not the case for legacy devices. Currently this
* is ok because this function is only called in the getinfo(9e) path,
* which for a legacy device would directly end up in the driver's
* getinfo, rather than here
*/
if (mac_perim_enter_by_macname(macname, &mph) != 0)
return (NULL);
if (dls_link_hold(macname, &dlp) != 0) {
mac_perim_exit(mph);
return (NULL);
}
dip = mac_devinfo_get(dlp->dl_mh);
dls_link_rele(dlp);
mac_perim_exit(mph);
return (dip);
}
dev_t
dls_link_dev(dls_link_t *dlp)
{
return (makedevice(ddi_driver_major(mac_devinfo_get(dlp->dl_mh)),
mac_minor(dlp->dl_mh)));
}
void
dls_link_rele(dls_link_t *dlp)
{
mod_hash_val_t val;
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
/*
* Check if there are any more references.
*/
if (--dlp->dl_ref == 0) {
(void) mod_hash_remove(i_dls_link_hash,
(mod_hash_key_t)dlp->dl_name, &val);
ASSERT(dlp == (dls_link_t *)val);
/*
* Destroy the dls_link_t.
*/
i_dls_link_destroy(dlp);
ASSERT(i_dls_link_count > 0);
atomic_dec_32(&i_dls_link_count);
}
}
int
dls_link_rele_by_name(const char *name)
{
dls_link_t *dlp;
if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
(mod_hash_val_t *)&dlp) != 0)
return (ENOENT);
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
/*
* Must fail detach if mac client is busy.
*/
ASSERT(dlp->dl_ref > 0 && dlp->dl_mch != NULL);
if (mac_link_has_flows(dlp->dl_mch))
return (ENOTEMPTY);
dls_link_rele(dlp);
return (0);
}
int
dls_link_setzid(const char *name, zoneid_t zid)
{
dls_link_t *dlp;
int err = 0;
zoneid_t old_zid;
if ((err = dls_link_hold_create(name, &dlp)) != 0)
return (err);
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
if ((old_zid = dlp->dl_zid) == zid)
goto done;
/*
* Check whether this dlp is used by its own zone. If yes, we cannot
* change its zoneid.
*/
if (dlp->dl_zone_ref != 0) {
err = EBUSY;
goto done;
}
dlp->dl_zid = zid;
if (zid == GLOBAL_ZONEID) {
/*
* The link is moving from a non-global zone to the global
* zone, so we need to release the reference that was held
* when the link was originally assigned to the non-global
* zone.
*/
dls_link_rele(dlp);
}
done:
/*
* We only keep the reference to this link open if the link has
* successfully moved from the global zone to a non-global zone.
*/
if (err != 0 || old_zid != GLOBAL_ZONEID)
dls_link_rele(dlp);
return (err);
}
int
dls_link_getzid(const char *name, zoneid_t *zidp)
{
dls_link_t *dlp;
int err = 0;
if ((err = dls_link_hold(name, &dlp)) != 0)
return (err);
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
*zidp = dlp->dl_zid;
dls_link_rele(dlp);
return (0);
}
void
dls_link_add(dls_link_t *dlp, uint32_t sap, dld_str_t *dsp)
{
mod_hash_t *hash = dlp->dl_str_hash;
mod_hash_key_t key;
dls_head_t *dhp;
dld_str_t *p;
int err;
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
/*
* Generate a hash key based on the sap.
*/
key = MAKE_KEY(sap);
/*
* Search the table for a list head with this key.
*/
if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
dhp = i_dls_head_alloc(key);
err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
ASSERT(err == 0);
}
/*
* Add the dld_str_t to the head of the list. List walkers in
* i_dls_link_rx_* bump up dh_ref to ensure the list does not change
* while they walk the list. The membar below ensures that list walkers
* see exactly the old list or the new list.
*/
ASSERT(dsp->ds_next == NULL);
p = dhp->dh_list;
dsp->ds_next = p;
membar_producer();
dhp->dh_list = dsp;
/*
* Save a pointer to the list head.
*/
dsp->ds_head = dhp;
dlp->dl_impl_count++;
}
void
dls_link_remove(dls_link_t *dlp, dld_str_t *dsp)
{
mod_hash_t *hash = dlp->dl_str_hash;
dld_str_t **pp;
dld_str_t *p;
dls_head_t *dhp;
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
/*
* We set dh_removing here to tell the receive callbacks not to pass
* up packets anymore. Then wait till the current callbacks are done.
* This happens either in the close path or in processing the
* DL_UNBIND_REQ via a taskq thread, and it is ok to cv_wait in either.
* The dh_ref ensures there aren't and there won't be any upcalls
* walking or using the dh_list. The mod hash internal lock ensures
* that the insert/remove of the dls_head_t itself synchronizes with
* any i_dls_link_rx trying to locate it. The perimeter ensures that
* there isn't another simultaneous dls_link_add/remove.
*/
dhp = dsp->ds_head;
mutex_enter(&dhp->dh_lock);
dhp->dh_removing = B_TRUE;
while (dhp->dh_ref != 0)
cv_wait(&dhp->dh_cv, &dhp->dh_lock);
mutex_exit(&dhp->dh_lock);
/*
* Walk the list and remove the dld_str_t.
*/
for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->ds_next)) {
if (p == dsp)
break;
}
ASSERT(p != NULL);
*pp = p->ds_next;
p->ds_next = NULL;
p->ds_head = NULL;
ASSERT(dlp->dl_impl_count != 0);
dlp->dl_impl_count--;
if (dhp->dh_list == NULL) {
mod_hash_val_t val = NULL;
/*
* The list is empty so remove the hash table entry.
*/
(void) mod_hash_remove(hash, dhp->dh_key, &val);
ASSERT(dhp == (dls_head_t *)val);
i_dls_head_free(dhp);
} else {
mutex_enter(&dhp->dh_lock);
dhp->dh_removing = B_FALSE;
mutex_exit(&dhp->dh_lock);
}
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2017 Joyent, Inc.
* Copyright 2025 Oxide Computer Company.
*/
/*
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/*
* Datalink management routines.
*/
#include <sys/types.h>
#include <sys/door.h>
#include <sys/zone.h>
#include <sys/modctl.h>
#include <sys/file.h>
#include <sys/modhash.h>
#include <sys/kstat.h>
#include <sys/vnode.h>
#include <sys/cmn_err.h>
#include <sys/softmac.h>
#include <sys/dls.h>
#include <sys/dls_impl.h>
#include <sys/stropts.h>
#include <sys/netstack.h>
#include <inet/iptun/iptun_impl.h>
/*
* This vanity name management module is treated as part of the GLD framework
* and we don't hold any GLD framework lock across a call to any mac
* function that needs to acquire the mac perimeter. The hierarchy is
* mac perimeter -> framework locks
*/
typedef struct dls_stack {
zoneid_t dlss_zoneid;
} dls_stack_t;
static kmem_cache_t *i_dls_devnet_cachep;
/* Upcall door handle and its lock. */
static kmutex_t i_dls_mgmt_lock;
static door_handle_t dls_mgmt_dh = NULL;
/*
* Any association of <macname, linkid> (set, rename) can require an upcall to
* the daemon for the link vanity name. We want set/rename/unset to be mutually
* exclusive from start to finish, but it is unsafe to hold a write on
* i_dls_devnet_hash_lock during an upcall. Enforce their exclusion using a
* separate lock from the hash tables.
*
* i_dls_devnet_hash_lock protects the hash tables themselves. Taking a write on
* it requires we first hold i_dls_devnet_lock. Thus, we can safely drop,
* reacquire, and upgrade/downgrade it so long as all table updates occur in a
* single write. If a write is intended i_dls_devnet_lock must be acquired
* before i_dls_devnet_hash_lock, leaving the valid lock patterns:
* - i_dls_devnet_lock_enter -> i_dls_devnet_hashmap_write
* - i_dls_devnet_lock_enter -> i_dls_devnet_hashmap_read
* - i_dls_devnet_hashmap_read
* i_dls_devnet_hashmap_write enforces the first invariant.
*/
static kmutex_t i_dls_devnet_lock;
static kcondvar_t i_dls_devnet_cv;
static kthread_t *i_dls_devnet_own;
static krwlock_t i_dls_devnet_hash_lock;
static mod_hash_t *i_dls_devnet_id_hash;
static mod_hash_t *i_dls_devnet_hash;
static void
i_dls_devnet_lock_enter(void)
{
mutex_enter(&i_dls_devnet_lock);
while (i_dls_devnet_own != NULL) {
cv_wait(&i_dls_devnet_cv, &i_dls_devnet_lock);
}
}
static void
i_dls_devnet_lock_exit(void)
{
VERIFY3P(i_dls_devnet_own, ==, NULL);
cv_broadcast(&i_dls_devnet_cv);
mutex_exit(&i_dls_devnet_lock);
}
static void
i_dls_devnet_lock_upcall_start(void)
{
VERIFY(MUTEX_HELD(&i_dls_devnet_lock));
VERIFY3P(i_dls_devnet_own, ==, NULL);
i_dls_devnet_own = curthread;
mutex_exit(&i_dls_devnet_lock);
}
static void
i_dls_devnet_lock_upcall_end(void)
{
mutex_enter(&i_dls_devnet_lock);
VERIFY3P(i_dls_devnet_own, ==, curthread);
i_dls_devnet_own = NULL;
}
static void
i_dls_devnet_hashmap_write(void)
{
VERIFY(MUTEX_HELD(&i_dls_devnet_lock));
rw_enter(&i_dls_devnet_hash_lock, RW_WRITER);
}
static void
i_dls_devnet_hashmap_read(void)
{
rw_enter(&i_dls_devnet_hash_lock, RW_READER);
}
static void
i_dls_devnet_hashmap_exit(void)
{
rw_exit(&i_dls_devnet_hash_lock);
}
boolean_t devnet_need_rebuild;
#define VLAN_HASHSZ 67 /* prime */
/*
* The following macros take a link name without the trailing PPA as input.
* Opening a /dev/net node with one of these names causes a tunnel link to be
* implicitly created in dls_devnet_hold_by_name() for backward compatibility
* with Solaris 10 and prior.
*/
#define IS_IPV4_TUN(name) (strcmp((name), "ip.tun") == 0)
#define IS_IPV6_TUN(name) (strcmp((name), "ip6.tun") == 0)
#define IS_6TO4_TUN(name) (strcmp((name), "ip.6to4tun") == 0)
#define IS_IPTUN_LINK(name) ( \
IS_IPV4_TUN(name) || IS_IPV6_TUN(name) || IS_6TO4_TUN(name))
/* dls_devnet_t dd_flags */
#define DD_CONDEMNED 0x1
#define DD_IMPLICIT_IPTUN 0x2 /* Implicitly-created ip*.*tun* tunnel */
#define DD_INITIALIZING 0x4
/*
* If the link is marked as initializing or condemned then it should
* not be visible outside of the DLS framework.
*/
#define DD_NOT_VISIBLE(flags) ( \
(flags & (DD_CONDEMNED | DD_INITIALIZING)) != 0)
/*
* This structure is used to keep the <linkid, macname> mapping.
* This structure itself is not protected by the mac perimeter, but is
* protected by the dd_mutex and i_dls_devnet_hash_lock. Thus most of the
* functions manipulating this structure such as dls_devnet_set/unset etc.
* may be called while not holding the mac perimeter.
*/
typedef struct dls_devnet_s {
datalink_id_t dd_linkid;
char dd_linkname[MAXLINKNAMELEN];
char dd_mac[MAXNAMELEN];
kstat_t *dd_ksp; /* kstat in owner_zid */
kstat_t *dd_zone_ksp; /* in dd_zid if != owner_zid */
uint32_t dd_ref;
kmutex_t dd_mutex;
kcondvar_t dd_cv;
uint32_t dd_tref;
uint_t dd_flags;
zoneid_t dd_owner_zid; /* zone where node was created */
zoneid_t dd_zid; /* current zone */
boolean_t dd_prop_loaded;
taskqid_t dd_prop_taskid;
boolean_t dd_transient; /* link goes away when zone does */
} dls_devnet_t;
static int i_dls_devnet_create_iptun(const char *, const char *,
datalink_id_t *);
static int i_dls_devnet_destroy_iptun(datalink_id_t);
static int i_dls_devnet_setzid(dls_devnet_t *, zoneid_t, boolean_t, boolean_t);
static int dls_devnet_unset(mac_handle_t, datalink_id_t *, boolean_t);
/*ARGSUSED*/
static int
i_dls_devnet_constructor(void *buf, void *arg, int kmflag)
{
dls_devnet_t *ddp = buf;
bzero(buf, sizeof (dls_devnet_t));
mutex_init(&ddp->dd_mutex, NULL, MUTEX_DEFAULT, NULL);
cv_init(&ddp->dd_cv, NULL, CV_DEFAULT, NULL);
return (0);
}
/*ARGSUSED*/
static void
i_dls_devnet_destructor(void *buf, void *arg)
{
dls_devnet_t *ddp = buf;
VERIFY(ddp->dd_ksp == NULL);
VERIFY(ddp->dd_ref == 0);
VERIFY(ddp->dd_tref == 0);
mutex_destroy(&ddp->dd_mutex);
cv_destroy(&ddp->dd_cv);
}
/* ARGSUSED */
static int
dls_zone_remove(datalink_id_t linkid, void *arg)
{
dls_devnet_t *ddp;
if (dls_devnet_hold_tmp(linkid, &ddp) == 0) {
/*
* Don't bother moving transient links back to the global zone
* since we will simply delete them in dls_devnet_unset.
*/
if (!ddp->dd_transient)
(void) dls_devnet_setzid(ddp, GLOBAL_ZONEID);
dls_devnet_rele_tmp(ddp);
}
return (0);
}
/* ARGSUSED */
static void *
dls_stack_init(netstackid_t stackid, netstack_t *ns)
{
dls_stack_t *dlss;
dlss = kmem_zalloc(sizeof (*dlss), KM_SLEEP);
dlss->dlss_zoneid = netstackid_to_zoneid(stackid);
return (dlss);
}
/* ARGSUSED */
static void
dls_stack_shutdown(netstackid_t stackid, void *arg)
{
dls_stack_t *dlss = (dls_stack_t *)arg;
/* Move remaining datalinks in this zone back to the global zone. */
(void) zone_datalink_walk(dlss->dlss_zoneid, dls_zone_remove, NULL);
}
/* ARGSUSED */
static void
dls_stack_fini(netstackid_t stackid, void *arg)
{
dls_stack_t *dlss = (dls_stack_t *)arg;
kmem_free(dlss, sizeof (*dlss));
}
/*
* Module initialization and finalization functions.
*/
void
dls_mgmt_init(void)
{
mutex_init(&i_dls_mgmt_lock, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&i_dls_devnet_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&i_dls_devnet_cv, NULL, CV_DEFAULT, NULL);
i_dls_devnet_own = NULL;
rw_init(&i_dls_devnet_hash_lock, NULL, RW_DEFAULT, NULL);
/*
* Create a kmem_cache of dls_devnet_t structures.
*/
i_dls_devnet_cachep = kmem_cache_create("dls_devnet_cache",
sizeof (dls_devnet_t), 0, i_dls_devnet_constructor,
i_dls_devnet_destructor, NULL, NULL, NULL, 0);
ASSERT(i_dls_devnet_cachep != NULL);
/*
* Create a hash table, keyed by dd_linkid, of dls_devnet_t.
*/
i_dls_devnet_id_hash = mod_hash_create_idhash("dls_devnet_id_hash",
VLAN_HASHSZ, mod_hash_null_valdtor);
/*
* Create a hash table, keyed by dd_mac
*/
i_dls_devnet_hash = mod_hash_create_extended("dls_devnet_hash",
VLAN_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
devnet_need_rebuild = B_FALSE;
netstack_register(NS_DLS, dls_stack_init, dls_stack_shutdown,
dls_stack_fini);
}
void
dls_mgmt_fini(void)
{
netstack_unregister(NS_DLS);
mod_hash_destroy_hash(i_dls_devnet_hash);
mod_hash_destroy_hash(i_dls_devnet_id_hash);
kmem_cache_destroy(i_dls_devnet_cachep);
rw_destroy(&i_dls_devnet_hash_lock);
cv_destroy(&i_dls_devnet_cv);
mutex_destroy(&i_dls_devnet_lock);
mutex_destroy(&i_dls_mgmt_lock);
}
int
dls_mgmt_door_set(boolean_t start)
{
int err;
/* handle daemon restart */
mutex_enter(&i_dls_mgmt_lock);
if (dls_mgmt_dh != NULL) {
door_ki_rele(dls_mgmt_dh);
dls_mgmt_dh = NULL;
}
if (start && ((err = door_ki_open(DLMGMT_DOOR, &dls_mgmt_dh)) != 0)) {
mutex_exit(&i_dls_mgmt_lock);
return (err);
}
mutex_exit(&i_dls_mgmt_lock);
/*
* Create and associate <link name, linkid> mapping for network devices
* which are already attached before the daemon is started.
*/
if (start)
softmac_recreate();
return (0);
}
static boolean_t
i_dls_mgmt_door_revoked(door_handle_t dh)
{
struct door_info info;
extern int sys_shutdown;
ASSERT(dh != NULL);
if (sys_shutdown) {
cmn_err(CE_NOTE, "dls_mgmt_door: shutdown observed\n");
return (B_TRUE);
}
if (door_ki_info(dh, &info) != 0)
return (B_TRUE);
return ((info.di_attributes & DOOR_REVOKED) != 0);
}
/*
* Upcall to the datalink management daemon (dlmgmtd).
*/
static int
i_dls_mgmt_upcall(void *arg, size_t asize, void *rbuf, size_t rsize)
{
door_arg_t darg, save_arg;
door_handle_t dh;
int err;
int retry = 0;
#define MAXRETRYNUM 3
ASSERT(arg);
darg.data_ptr = arg;
darg.data_size = asize;
darg.desc_ptr = NULL;
darg.desc_num = 0;
darg.rbuf = rbuf;
darg.rsize = rsize;
save_arg = darg;
retry:
mutex_enter(&i_dls_mgmt_lock);
dh = dls_mgmt_dh;
if ((dh == NULL) || i_dls_mgmt_door_revoked(dh)) {
mutex_exit(&i_dls_mgmt_lock);
return (EBADF);
}
door_ki_hold(dh);
mutex_exit(&i_dls_mgmt_lock);
for (;;) {
retry++;
if ((err = door_ki_upcall_limited(dh, &darg, zone_kcred(),
SIZE_MAX, 0)) == 0)
break;
/*
* handle door call errors
*/
darg = save_arg;
switch (err) {
case EINTR:
/*
* If the operation which caused this door upcall gets
* interrupted, return directly.
*/
goto done;
case EAGAIN:
/*
* Repeat upcall if the maximum attempt limit has not
* been reached.
*/
if (retry < MAXRETRYNUM) {
delay(2 * hz);
break;
}
cmn_err(CE_WARN, "dls: dlmgmtd fatal error %d\n", err);
goto done;
default:
/* A fatal door error */
if (i_dls_mgmt_door_revoked(dh)) {
cmn_err(CE_NOTE,
"dls: dlmgmtd door service revoked\n");
if (retry < MAXRETRYNUM) {
door_ki_rele(dh);
goto retry;
}
}
cmn_err(CE_WARN, "dls: dlmgmtd fatal error %d\n", err);
goto done;
}
}
if (darg.rbuf != rbuf) {
/*
* The size of the input rbuf was not big enough, so the
* upcall allocated the rbuf itself. If this happens, assume
* that this was an invalid door call request.
*/
kmem_free(darg.rbuf, darg.rsize);
err = ENOSPC;
goto done;
}
if (darg.rsize != rsize) {
err = EINVAL;
goto done;
}
err = ((dlmgmt_retval_t *)rbuf)->lr_err;
done:
door_ki_rele(dh);
return (err);
}
/*
* Request the datalink management daemon to create a link with the attributes
* below. Upon success, zero is returned and linkidp contains the linkid for
* the new link; otherwise, an errno is returned.
*
* - dev physical dev_t. required for all physical links,
* including GLDv3 links. It will be used to force the
* attachment of a physical device, hence the
* registration of its mac
* - class datalink class
* - media type media type; DL_OTHER means unknown
* - persist whether to persist the datalink
*/
int
dls_mgmt_create(const char *devname, dev_t dev, datalink_class_t class,
uint32_t media, boolean_t persist, datalink_id_t *linkidp)
{
dlmgmt_upcall_arg_create_t create;
dlmgmt_create_retval_t retval;
int err;
create.ld_cmd = DLMGMT_CMD_DLS_CREATE;
create.ld_class = class;
create.ld_media = media;
create.ld_phymaj = getmajor(dev);
create.ld_phyinst = getminor(dev);
create.ld_persist = persist;
if (strlcpy(create.ld_devname, devname, sizeof (create.ld_devname)) >=
sizeof (create.ld_devname))
return (EINVAL);
if ((err = i_dls_mgmt_upcall(&create, sizeof (create), &retval,
sizeof (retval))) == 0) {
*linkidp = retval.lr_linkid;
}
return (err);
}
/*
* Request the datalink management daemon to destroy the specified link.
* Returns zero upon success, or an errno upon failure.
*/
int
dls_mgmt_destroy(datalink_id_t linkid, boolean_t persist)
{
dlmgmt_upcall_arg_destroy_t destroy;
dlmgmt_destroy_retval_t retval;
destroy.ld_cmd = DLMGMT_CMD_DLS_DESTROY;
destroy.ld_linkid = linkid;
destroy.ld_persist = persist;
return (i_dls_mgmt_upcall(&destroy, sizeof (destroy),
&retval, sizeof (retval)));
}
/*
* Request the datalink management daemon to verify/update the information
* for a physical link. Upon success, get its linkid.
*
* - media type media type
* - novanity whether this physical datalink supports vanity naming.
* physical links that do not use the GLDv3 MAC plugin
* cannot suport vanity naming
*
* This function could fail with ENOENT or EEXIST. Two cases return EEXIST:
*
* 1. A link with devname already exists, but the media type does not match.
* In this case, mediap will bee set to the media type of the existing link.
* 2. A link with devname already exists, but its link name does not match
* the device name, although this link does not support vanity naming.
*/
int
dls_mgmt_update(const char *devname, uint32_t media, boolean_t novanity,
uint32_t *mediap, datalink_id_t *linkidp)
{
dlmgmt_upcall_arg_update_t update;
dlmgmt_update_retval_t retval;
int err;
update.ld_cmd = DLMGMT_CMD_DLS_UPDATE;
if (strlcpy(update.ld_devname, devname, sizeof (update.ld_devname)) >=
sizeof (update.ld_devname))
return (EINVAL);
update.ld_media = media;
update.ld_novanity = novanity;
if ((err = i_dls_mgmt_upcall(&update, sizeof (update), &retval,
sizeof (retval))) == EEXIST) {
*linkidp = retval.lr_linkid;
*mediap = retval.lr_media;
} else if (err == 0) {
*linkidp = retval.lr_linkid;
}
return (err);
}
/*
* Request the datalink management daemon to get the information for a link.
* Returns zero upon success, or an errno upon failure.
*
* Only fills in information for argument pointers that are non-NULL.
* Note that the link argument is expected to be MAXLINKNAMELEN bytes.
*/
int
dls_mgmt_get_linkinfo(datalink_id_t linkid, char *link,
datalink_class_t *classp, uint32_t *mediap, uint32_t *flagsp)
{
dlmgmt_door_getname_t getname;
dlmgmt_getname_retval_t retval;
int err, len;
getname.ld_cmd = DLMGMT_CMD_GETNAME;
getname.ld_linkid = linkid;
if ((err = i_dls_mgmt_upcall(&getname, sizeof (getname), &retval,
sizeof (retval))) != 0) {
return (err);
}
len = strlen(retval.lr_link);
if (len <= 1 || len >= MAXLINKNAMELEN)
return (EINVAL);
if (link != NULL)
(void) strlcpy(link, retval.lr_link, MAXLINKNAMELEN);
if (classp != NULL)
*classp = retval.lr_class;
if (mediap != NULL)
*mediap = retval.lr_media;
if (flagsp != NULL)
*flagsp = retval.lr_flags;
return (0);
}
/*
* Request the datalink management daemon to get the linkid for a link.
* Returns a non-zero error code on failure. The linkid argument is only
* set on success (when zero is returned.)
*/
int
dls_mgmt_get_linkid(const char *link, datalink_id_t *linkid)
{
dlmgmt_door_getlinkid_t getlinkid;
dlmgmt_getlinkid_retval_t retval;
int err;
getlinkid.ld_cmd = DLMGMT_CMD_GETLINKID;
(void) strlcpy(getlinkid.ld_link, link, MAXLINKNAMELEN);
if ((err = i_dls_mgmt_upcall(&getlinkid, sizeof (getlinkid), &retval,
sizeof (retval))) == 0) {
*linkid = retval.lr_linkid;
}
return (err);
}
datalink_id_t
dls_mgmt_get_next(datalink_id_t linkid, datalink_class_t class,
datalink_media_t dmedia, uint32_t flags)
{
dlmgmt_door_getnext_t getnext;
dlmgmt_getnext_retval_t retval;
getnext.ld_cmd = DLMGMT_CMD_GETNEXT;
getnext.ld_class = class;
getnext.ld_dmedia = dmedia;
getnext.ld_flags = flags;
getnext.ld_linkid = linkid;
if (i_dls_mgmt_upcall(&getnext, sizeof (getnext), &retval,
sizeof (retval)) != 0) {
return (DATALINK_INVALID_LINKID);
}
return (retval.lr_linkid);
}
static int
i_dls_mgmt_get_linkattr(const datalink_id_t linkid, const char *attr,
void *attrval, size_t *attrszp)
{
dlmgmt_upcall_arg_getattr_t getattr;
dlmgmt_getattr_retval_t retval;
int err;
getattr.ld_cmd = DLMGMT_CMD_DLS_GETATTR;
getattr.ld_linkid = linkid;
(void) strlcpy(getattr.ld_attr, attr, MAXLINKATTRLEN);
if ((err = i_dls_mgmt_upcall(&getattr, sizeof (getattr), &retval,
sizeof (retval))) == 0) {
if (*attrszp < retval.lr_attrsz)
return (EINVAL);
*attrszp = retval.lr_attrsz;
bcopy(retval.lr_attrval, attrval, retval.lr_attrsz);
}
return (err);
}
/*
* Note that this function can only get devp successfully for non-VLAN link.
*/
int
dls_mgmt_get_phydev(datalink_id_t linkid, dev_t *devp)
{
uint64_t maj, inst;
size_t attrsz = sizeof (uint64_t);
if (i_dls_mgmt_get_linkattr(linkid, FPHYMAJ, &maj, &attrsz) != 0 ||
attrsz != sizeof (uint64_t) ||
i_dls_mgmt_get_linkattr(linkid, FPHYINST, &inst, &attrsz) != 0 ||
attrsz != sizeof (uint64_t)) {
return (EINVAL);
}
*devp = makedevice((major_t)maj, (minor_t)inst);
return (0);
}
/*
* Request the datalink management daemon to push in
* all properties associated with the link.
* Returns a non-zero error code on failure.
*/
int
dls_mgmt_linkprop_init(datalink_id_t linkid)
{
dlmgmt_door_linkprop_init_t li;
dlmgmt_linkprop_init_retval_t retval;
int err;
li.ld_cmd = DLMGMT_CMD_LINKPROP_INIT;
li.ld_linkid = linkid;
err = i_dls_mgmt_upcall(&li, sizeof (li), &retval, sizeof (retval));
return (err);
}
static void
dls_devnet_prop_task(void *arg)
{
dls_devnet_t *ddp = arg;
(void) dls_mgmt_linkprop_init(ddp->dd_linkid);
mutex_enter(&ddp->dd_mutex);
ddp->dd_prop_loaded = B_TRUE;
ddp->dd_prop_taskid = 0;
cv_broadcast(&ddp->dd_cv);
mutex_exit(&ddp->dd_mutex);
}
/*
* Ensure property loading task is completed.
*/
void
dls_devnet_prop_task_wait(dls_dl_handle_t ddp)
{
mutex_enter(&ddp->dd_mutex);
while (ddp->dd_prop_taskid != 0)
cv_wait(&ddp->dd_cv, &ddp->dd_mutex);
mutex_exit(&ddp->dd_mutex);
}
void
dls_devnet_rele_tmp(dls_dl_handle_t dlh)
{
dls_devnet_t *ddp = dlh;
mutex_enter(&ddp->dd_mutex);
ASSERT(ddp->dd_tref != 0);
if (--ddp->dd_tref == 0)
cv_signal(&ddp->dd_cv);
mutex_exit(&ddp->dd_mutex);
}
int
dls_devnet_hold_link(datalink_id_t linkid, dls_dl_handle_t *ddhp,
dls_link_t **dlpp)
{
dls_dl_handle_t dlh;
dls_link_t *dlp;
int err;
if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
return (err);
if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0) {
dls_devnet_rele_tmp(dlh);
return (err);
}
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
*ddhp = dlh;
*dlpp = dlp;
return (0);
}
void
dls_devnet_rele_link(dls_dl_handle_t dlh, dls_link_t *dlp)
{
ASSERT(MAC_PERIM_HELD(dlp->dl_mh));
dls_link_rele(dlp);
dls_devnet_rele_tmp(dlh);
}
/*
* "link" kstats related functions.
*/
/*
* Query the "link" kstats.
*
* We may be called from the kstat subsystem in an arbitrary context.
* If the caller is the stack, the context could be an upcall data
* thread. Hence we can't acquire the mac perimeter in this function
* for fear of deadlock.
*/
static int
dls_devnet_stat_update(kstat_t *ksp, int rw)
{
datalink_id_t linkid = (datalink_id_t)(uintptr_t)ksp->ks_private;
dls_devnet_t *ddp;
dls_link_t *dlp;
int err;
if ((err = dls_devnet_hold_tmp(linkid, &ddp)) != 0) {
return (err);
}
/*
* If a device detach happens at this time, it will block in
* dls_devnet_unset since the dd_tref has been bumped in
* dls_devnet_hold_tmp(). So the access to 'dlp' is safe even though
* we don't hold the mac perimeter.
*/
if (mod_hash_find(i_dls_link_hash, (mod_hash_key_t)ddp->dd_mac,
(mod_hash_val_t *)&dlp) != 0) {
dls_devnet_rele_tmp(ddp);
return (ENOENT);
}
err = dls_stat_update(ksp, dlp, rw);
dls_devnet_rele_tmp(ddp);
return (err);
}
/*
* Create the "link" kstats.
*/
static void
dls_devnet_stat_create(dls_devnet_t *ddp, zoneid_t zoneid)
{
kstat_t *ksp;
if (dls_stat_create("link", 0, ddp->dd_linkname, zoneid,
dls_devnet_stat_update, (void *)(uintptr_t)ddp->dd_linkid,
&ksp) == 0) {
ASSERT(ksp != NULL);
if (zoneid == ddp->dd_owner_zid) {
ASSERT(ddp->dd_ksp == NULL);
ddp->dd_ksp = ksp;
} else {
ASSERT(ddp->dd_zone_ksp == NULL);
ddp->dd_zone_ksp = ksp;
}
}
}
/*
* Destroy the "link" kstats.
*/
static void
dls_devnet_stat_destroy(dls_devnet_t *ddp, zoneid_t zoneid)
{
if (zoneid == ddp->dd_owner_zid) {
if (ddp->dd_ksp != NULL) {
kstat_delete(ddp->dd_ksp);
ddp->dd_ksp = NULL;
}
} else {
if (ddp->dd_zone_ksp != NULL) {
kstat_delete(ddp->dd_zone_ksp);
ddp->dd_zone_ksp = NULL;
}
}
}
/*
* The link has been renamed. Destroy the old non-legacy kstats ("link kstats")
* and create the new set using the new name.
*/
static void
dls_devnet_stat_rename(dls_devnet_t *ddp)
{
if (ddp->dd_ksp != NULL) {
kstat_delete(ddp->dd_ksp);
ddp->dd_ksp = NULL;
}
/* We can't rename a link while it's assigned to a non-global zone. */
ASSERT(ddp->dd_zone_ksp == NULL);
dls_devnet_stat_create(ddp, ddp->dd_owner_zid);
}
/*
* Associate the linkid with the link identified by macname. If this
* is called on behalf of a physical link then linkid may be
* DATALINK_INVALID_LINKID. Otherwise, if called on behalf of a
* virtual link, linkid must have a value.
*/
static int
dls_devnet_set(mac_handle_t mh, datalink_id_t linkid, zoneid_t zoneid,
dls_devnet_t **ddpp)
{
const char *macname = mac_name(mh);
dls_devnet_t *ddp = NULL;
datalink_class_t class;
int err;
boolean_t stat_create = B_FALSE;
char linkname[MAXLINKNAMELEN];
i_dls_devnet_lock_enter();
/*
* Don't allow callers to set a link name with a linkid that already
* has a name association (that's what rename is for).
*/
if (linkid != DATALINK_INVALID_LINKID) {
/*
* This temporary read access is valid, as no other set/rename
* operation can attempt an insert on the same linkid while
* i_dls_devnet_lock is held.
*/
i_dls_devnet_hashmap_read();
if (mod_hash_find(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)linkid,
(mod_hash_val_t *)&ddp) == 0) {
err = EEXIST;
goto done;
}
i_dls_devnet_hashmap_exit();
i_dls_devnet_lock_upcall_start();
err = dls_mgmt_get_linkinfo(linkid, linkname, &class,
NULL, NULL);
i_dls_devnet_lock_upcall_end();
if (err != 0)
goto done_rw_unlocked;
}
i_dls_devnet_hashmap_write();
if ((err = mod_hash_find(i_dls_devnet_hash,
(mod_hash_key_t)macname, (mod_hash_val_t *)&ddp)) == 0) {
if (ddp->dd_linkid != DATALINK_INVALID_LINKID) {
err = EEXIST;
goto done;
}
/*
* If we arrive here we know we are attempting to set
* the linkid on a physical link. A virtual link
* should never arrive here because it should never
* call this function without a linkid. Virtual links
* are created through dlgmtmd and thus we know
* dlmgmtd is alive to assign it a linkid (search for
* uses of dladm_create_datalink_id() to prove this to
* yourself); we don't have the same guarantee for a
* physical link which may perform an upcall for a
* linkid while dlmgmtd is down but will continue
* creating a devnet without the linkid (see
* softmac_create_datalink() to see how physical link
* creation works). That is why there is no entry in
* the id hash but there is one in the macname hash --
* softmac couldn't acquire a linkid the first time it
* called this function.
*
* Because of the check above, we also know that
* ddp->dd_linkid is not set. Following this, the link
* must still be in the DD_INITIALIZING state because
* that flag is removed IFF dd_linkid is set. This is
* why we can ASSERT the DD_INITIALIZING flag below if
* the call to i_dls_devnet_setzid() fails.
*/
if (linkid == DATALINK_INVALID_LINKID ||
class != DATALINK_CLASS_PHYS) {
err = EINVAL;
goto done;
}
ASSERT(ddp->dd_flags & DD_INITIALIZING);
} else {
ddp = kmem_cache_alloc(i_dls_devnet_cachep, KM_SLEEP);
ddp->dd_flags = DD_INITIALIZING;
ddp->dd_tref = 0;
ddp->dd_ref++;
ddp->dd_owner_zid = zoneid;
/*
* If we are creating a new devnet which will be owned by a NGZ
* then mark it as transient. This link has never been in the
* GZ, the GZ will not have a hold on its reference, and we do
* not want to return it to the GZ when the zone halts.
*/
if (zoneid != GLOBAL_ZONEID)
ddp->dd_transient = B_TRUE;
(void) strlcpy(ddp->dd_mac, macname, sizeof (ddp->dd_mac));
VERIFY(mod_hash_insert(i_dls_devnet_hash,
(mod_hash_key_t)ddp->dd_mac, (mod_hash_val_t)ddp) == 0);
}
if (linkid != DATALINK_INVALID_LINKID) {
ddp->dd_linkid = linkid;
(void) strlcpy(ddp->dd_linkname, linkname,
sizeof (ddp->dd_linkname));
VERIFY(mod_hash_insert(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)linkid,
(mod_hash_val_t)ddp) == 0);
devnet_need_rebuild = B_TRUE;
stat_create = B_TRUE;
}
err = 0;
done:
/*
* It is safe to drop the i_dls_devnet_hash_lock at this point. In the
* case of physical devices, the softmac framework will fail the device
* detach based on the smac_state or smac_hold_cnt. Other cases like
* vnic and aggr use their own scheme to serialize creates and deletes
* and ensure that *ddp is valid.
*/
i_dls_devnet_hashmap_exit();
done_rw_unlocked:
i_dls_devnet_lock_exit();
if (err == 0 && zoneid != GLOBAL_ZONEID) {
/*
* If this link is being created directly within a non-global
* zone, then flag it as transient so that it will be cleaned
* up when the zone is shut down.
*/
err = i_dls_devnet_setzid(ddp, zoneid, B_FALSE, B_TRUE);
if (err != 0) {
/*
* At this point the link is marked as
* DD_INITIALIZING -- there can be no
* outstanding temp refs and therefore no need
* to wait for them.
*/
ASSERT(ddp->dd_flags & DD_INITIALIZING);
(void) dls_devnet_unset(mh, &linkid, B_FALSE);
return (err);
}
}
if (err == 0) {
/*
* The kstat subsystem holds its own locks (rather perimeter)
* before calling the ks_update (dls_devnet_stat_update) entry
* point which in turn grabs the i_dls_devnet_hash_lock. So the
* lock hierarchy is kstat locks -> i_dls_devnet_hash_lock.
*/
if (stat_create)
dls_devnet_stat_create(ddp, zoneid);
if (ddpp != NULL)
*ddpp = ddp;
mutex_enter(&ddp->dd_mutex);
if (linkid != DATALINK_INVALID_LINKID &&
!ddp->dd_prop_loaded && ddp->dd_prop_taskid == 0) {
ddp->dd_prop_taskid = taskq_dispatch(system_taskq,
dls_devnet_prop_task, ddp, TQ_SLEEP);
}
mutex_exit(&ddp->dd_mutex);
}
return (err);
}
/*
* Disassociate the linkid from the link identified by macname. If
* wait is B_TRUE, wait until all temporary refs are released and the
* prop task is finished.
*
* If waiting then you SHOULD NOT call this from inside the MAC perim
* as deadlock will ensue. Otherwise, this function is safe to call
* from inside or outside the MAC perim.
*/
static int
dls_devnet_unset(mac_handle_t mh, datalink_id_t *id, boolean_t wait)
{
const char *macname = mac_name(mh);
dls_devnet_t *ddp;
int err;
mod_hash_val_t val;
i_dls_devnet_lock_enter();
i_dls_devnet_hashmap_write();
if ((err = mod_hash_find(i_dls_devnet_hash,
(mod_hash_key_t)macname, (mod_hash_val_t *)&ddp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
mutex_enter(&ddp->dd_mutex);
/*
* Make sure downcalls into softmac_create or softmac_destroy from
* devfs don't cv_wait on any devfs related condition for fear of
* deadlock. Return EBUSY if the asynchronous thread started for
* property loading as part of the post attach hasn't yet completed.
*/
VERIFY(ddp->dd_ref != 0);
if ((ddp->dd_ref != 1) || (!wait &&
(ddp->dd_tref != 0 || ddp->dd_prop_taskid != 0))) {
int zstatus = 0;
/*
* There are a couple of alternatives that might be going on
* here; a) the zone is shutting down and it has a transient
* link assigned, in which case we want to clean it up instead
* of moving it back to the global zone, or b) its possible
* that we're trying to clean up an orphaned vnic that was
* delegated to a zone and which wasn't cleaned up properly
* when the zone went away. Check for either of these cases
* before we simply return EBUSY.
*
* zstatus indicates which situation we are dealing with:
* 0 - means return EBUSY
* 1 - means case (a), cleanup transient link
* -1 - means case (b), orphaned VNIC
*/
if (ddp->dd_ref > 1 && ddp->dd_zid != GLOBAL_ZONEID) {
zone_t *zp;
if ((zp = zone_find_by_id(ddp->dd_zid)) == NULL) {
zstatus = -1;
} else {
if (ddp->dd_transient) {
zone_status_t s = zone_status_get(zp);
if (s >= ZONE_IS_SHUTTING_DOWN)
zstatus = 1;
}
zone_rele(zp);
}
}
if (zstatus == 0) {
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
i_dls_devnet_lock_exit();
return (EBUSY);
}
/*
* We want to delete the link, reset ref to 1;
*/
if (zstatus == -1) {
/* Log a warning, but continue in this case */
cmn_err(CE_WARN, "clear orphaned datalink: %s\n",
ddp->dd_linkname);
}
ddp->dd_ref = 1;
}
ddp->dd_flags |= DD_CONDEMNED;
ddp->dd_ref--;
*id = ddp->dd_linkid;
/*
* Remove this dls_devnet_t from the hash table.
*/
VERIFY(mod_hash_remove(i_dls_devnet_hash,
(mod_hash_key_t)ddp->dd_mac, &val) == 0);
if (ddp->dd_linkid != DATALINK_INVALID_LINKID) {
VERIFY(mod_hash_remove(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)ddp->dd_linkid, &val) == 0);
devnet_need_rebuild = B_TRUE;
}
i_dls_devnet_hashmap_exit();
i_dls_devnet_lock_exit();
/*
* It is important to call i_dls_devnet_setzid() WITHOUT the
* i_dls_devnet_hash_lock held. The setzid call grabs the MAC
* perim; thus causing DLS -> MAC lock ordering if performed
* with the i_dls_devnet_hash_lock held. This forces consumers to
* grab the MAC perim before calling dls_devnet_unset() (the
* locking rules state MAC -> DLS order). By performing the
* setzid outside of the i_dls_devnet_hash_lock consumers can
* safely call dls_devnet_unset() outside the MAC perim.
*/
if (ddp->dd_zid != GLOBAL_ZONEID) {
/*
* We need to release the dd_mutex before we try and destroy the
* stat. When we destroy it, we'll need to grab the lock for the
* kstat but if there's a concurrent reader of the kstat, we'll
* be blocked on it. This will lead to deadlock because these
* kstats employ a ks_update function (dls_devnet_stat_update)
* which needs the dd_mutex that we currently hold.
*
* Because we've already flagged the dls_devnet_t as
* DD_CONDEMNED and we still have a write lock on
* i_dls_devnet_hash_lock, we should be able to release the
* dd_mutex.
*/
mutex_exit(&ddp->dd_mutex);
dls_devnet_stat_destroy(ddp, ddp->dd_zid);
mutex_enter(&ddp->dd_mutex);
(void) i_dls_devnet_setzid(ddp, GLOBAL_ZONEID, B_FALSE,
B_FALSE);
}
if (wait) {
/*
* Wait until all temporary references are released.
* The holders of the tref need the MAC perim to
* perform their work and release the tref. To avoid
* deadlock, assert that the perim is never held here.
*/
ASSERT0(MAC_PERIM_HELD(mh));
while ((ddp->dd_tref != 0) || (ddp->dd_prop_taskid != 0))
cv_wait(&ddp->dd_cv, &ddp->dd_mutex);
} else {
VERIFY(ddp->dd_tref == 0);
VERIFY(ddp->dd_prop_taskid == 0);
}
if (ddp->dd_linkid != DATALINK_INVALID_LINKID)
dls_devnet_stat_destroy(ddp, ddp->dd_owner_zid);
ddp->dd_prop_loaded = B_FALSE;
ddp->dd_linkid = DATALINK_INVALID_LINKID;
ddp->dd_flags = 0;
mutex_exit(&ddp->dd_mutex);
kmem_cache_free(i_dls_devnet_cachep, ddp);
return (0);
}
/*
* This is a private hold routine used when we already have the dls_link_t, thus
* we know that it cannot go away.
*/
int
dls_devnet_hold_tmp_by_link(dls_link_t *dlp, dls_dl_handle_t *ddhp)
{
int err;
dls_devnet_t *ddp = NULL;
i_dls_devnet_hashmap_read();
if ((err = mod_hash_find(i_dls_devnet_hash,
(mod_hash_key_t)dlp->dl_name, (mod_hash_val_t *)&ddp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
mutex_enter(&ddp->dd_mutex);
VERIFY(ddp->dd_ref > 0);
if (DD_NOT_VISIBLE(ddp->dd_flags)) {
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
ddp->dd_tref++;
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
*ddhp = ddp;
return (0);
}
static int
dls_devnet_hold_common(datalink_id_t linkid, dls_devnet_t **ddpp,
boolean_t tmp_hold)
{
dls_devnet_t *ddp;
int err;
i_dls_devnet_hashmap_read();
if ((err = mod_hash_find(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)linkid, (mod_hash_val_t *)&ddp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
mutex_enter(&ddp->dd_mutex);
VERIFY(ddp->dd_ref > 0);
if (DD_NOT_VISIBLE(ddp->dd_flags)) {
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
if (tmp_hold)
ddp->dd_tref++;
else
ddp->dd_ref++;
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
*ddpp = ddp;
return (0);
}
int
dls_devnet_hold(datalink_id_t linkid, dls_devnet_t **ddpp)
{
return (dls_devnet_hold_common(linkid, ddpp, B_FALSE));
}
/*
* Hold the vanity naming structure (dls_devnet_t) temporarily. The request to
* delete the dls_devnet_t will wait until the temporary reference is released.
*/
int
dls_devnet_hold_tmp(datalink_id_t linkid, dls_devnet_t **ddpp)
{
return (dls_devnet_hold_common(linkid, ddpp, B_TRUE));
}
/*
* This funtion is called when a DLS client tries to open a device node.
* This dev_t could be a result of a /dev/net node access (returned by
* devnet_create_rvp->dls_devnet_open()) or a direct /dev node access.
* In both cases, this function bumps up the reference count of the
* dls_devnet_t structure. The reference is held as long as the device node
* is open. In the case of /dev/net while it is true that the initial reference
* is held when the devnet_create_rvp->dls_devnet_open call happens, this
* initial reference is released immediately in devnet_inactive_callback ->
* dls_devnet_close(). (Note that devnet_inactive_callback() is called right
* after dld_open completes, not when the /dev/net node is being closed).
* To undo this function, call dls_devnet_rele()
*/
int
dls_devnet_hold_by_dev(dev_t dev, dls_dl_handle_t *ddhp)
{
char name[MAXNAMELEN];
char *drv;
dls_devnet_t *ddp;
int err;
if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
return (EINVAL);
(void) snprintf(name, sizeof (name), "%s%d", drv,
DLS_MINOR2INST(getminor(dev)));
i_dls_devnet_hashmap_read();
if ((err = mod_hash_find(i_dls_devnet_hash,
(mod_hash_key_t)name, (mod_hash_val_t *)&ddp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
mutex_enter(&ddp->dd_mutex);
VERIFY(ddp->dd_ref > 0);
if (DD_NOT_VISIBLE(ddp->dd_flags)) {
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
ddp->dd_ref++;
mutex_exit(&ddp->dd_mutex);
i_dls_devnet_hashmap_exit();
*ddhp = ddp;
return (0);
}
void
dls_devnet_rele(dls_devnet_t *ddp)
{
mutex_enter(&ddp->dd_mutex);
VERIFY(ddp->dd_ref > 1);
ddp->dd_ref--;
if ((ddp->dd_flags & DD_IMPLICIT_IPTUN) && ddp->dd_ref == 1) {
mutex_exit(&ddp->dd_mutex);
if (i_dls_devnet_destroy_iptun(ddp->dd_linkid) != 0)
ddp->dd_flags |= DD_IMPLICIT_IPTUN;
return;
}
mutex_exit(&ddp->dd_mutex);
}
static int
dls_devnet_hold_by_name(const char *link, dls_devnet_t **ddpp)
{
char drv[MAXLINKNAMELEN];
uint_t ppa;
major_t major;
dev_t phy_dev, tmp_dev;
datalink_id_t linkid;
dls_dev_handle_t ddh;
int err;
if ((err = dls_mgmt_get_linkid(link, &linkid)) == 0)
return (dls_devnet_hold(linkid, ddpp));
/*
* If we failed to get the link's linkid because the dlmgmtd daemon
* has not been started, return ENOENT so that the application can
* fallback to open the /dev node.
*/
if (err == EBADF)
return (ENOENT);
if (err != ENOENT)
return (err);
/*
* If we reach this point it means dlmgmtd is up but has no
* mapping for the link name.
*/
if (ddi_parse_dlen(link, drv, MAXLINKNAMELEN, &ppa) != DDI_SUCCESS)
return (ENOENT);
if (IS_IPTUN_LINK(drv)) {
if ((err = i_dls_devnet_create_iptun(link, drv, &linkid)) != 0)
return (err);
/*
* At this point, an IP tunnel MAC has registered, which
* resulted in a link being created.
*/
err = dls_devnet_hold(linkid, ddpp);
if (err != 0) {
VERIFY(i_dls_devnet_destroy_iptun(linkid) == 0);
return (err);
}
/*
* dls_devnet_rele() will know to destroy the implicit IP
* tunnel on last reference release if DD_IMPLICIT_IPTUN is
* set.
*/
(*ddpp)->dd_flags |= DD_IMPLICIT_IPTUN;
return (0);
}
/*
* If this link:
* (a) is a physical device, (b) this is the first boot, (c) the MAC
* is not registered yet, and (d) we cannot find its linkid, then the
* linkname is the same as the devname.
*
* First filter out invalid names.
*/
if ((major = ddi_name_to_major(drv)) == (major_t)-1)
return (ENOENT);
phy_dev = makedevice(major, DLS_PPA2MINOR(ppa));
if (softmac_hold_device(phy_dev, &ddh) != 0)
return (ENOENT);
/*
* At this time, the MAC should be registered, check its phy_dev using
* the given name.
*/
if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0 ||
(err = dls_mgmt_get_phydev(linkid, &tmp_dev)) != 0) {
softmac_rele_device(ddh);
return (err);
}
if (tmp_dev != phy_dev) {
softmac_rele_device(ddh);
return (ENOENT);
}
err = dls_devnet_hold(linkid, ddpp);
softmac_rele_device(ddh);
return (err);
}
int
dls_devnet_macname2linkid(const char *macname, datalink_id_t *linkidp)
{
dls_devnet_t *ddp;
i_dls_devnet_hashmap_read();
if (mod_hash_find(i_dls_devnet_hash, (mod_hash_key_t)macname,
(mod_hash_val_t *)&ddp) != 0) {
i_dls_devnet_hashmap_exit();
return (ENOENT);
}
*linkidp = ddp->dd_linkid;
i_dls_devnet_hashmap_exit();
return (0);
}
/*
* Get linkid for the given dev.
*/
int
dls_devnet_dev2linkid(dev_t dev, datalink_id_t *linkidp)
{
char macname[MAXNAMELEN];
char *drv;
if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
return (EINVAL);
(void) snprintf(macname, sizeof (macname), "%s%d", drv,
DLS_MINOR2INST(getminor(dev)));
return (dls_devnet_macname2linkid(macname, linkidp));
}
/*
* Get the link's physical dev_t. It this is a VLAN, get the dev_t of the
* link this VLAN is created on.
*/
int
dls_devnet_phydev(datalink_id_t vlanid, dev_t *devp)
{
dls_devnet_t *ddp;
int err;
if ((err = dls_devnet_hold_tmp(vlanid, &ddp)) != 0)
return (err);
err = dls_mgmt_get_phydev(ddp->dd_linkid, devp);
dls_devnet_rele_tmp(ddp);
return (err);
}
/*
* Handle the renaming requests. There are two rename cases:
*
* 1. Request to rename a valid link (id1) to an non-existent link name
* (id2). In this case id2 is DATALINK_INVALID_LINKID. Just check whether
* id1 is held by any applications.
*
* In this case, the link's kstats need to be updated using the given name.
*
* 2. Request to rename a valid link (id1) to the name of a REMOVED
* physical link (id2). In this case, check that id1 and its associated
* mac is not held by any application, and update the link's linkid to id2.
*
* This case does not change the <link name, linkid> mapping, so the link's
* kstats need to be updated with using name associated the given id2.
*/
int
dls_devnet_rename(datalink_id_t id1, datalink_id_t id2, const char *link)
{
dls_dev_handle_t ddh = NULL;
int err = 0;
dev_t phydev = 0;
dls_devnet_t *ddp;
mac_perim_handle_t mph = NULL;
mac_handle_t mh;
mod_hash_val_t val;
/*
* In the second case, id2 must be a REMOVED physical link.
*/
if ((id2 != DATALINK_INVALID_LINKID) &&
(dls_mgmt_get_phydev(id2, &phydev) == 0) &&
softmac_hold_device(phydev, &ddh) == 0) {
softmac_rele_device(ddh);
return (EEXIST);
}
/*
* Hold id1 to prevent it from being detached (if a physical link).
*/
if (dls_mgmt_get_phydev(id1, &phydev) == 0)
(void) softmac_hold_device(phydev, &ddh);
/*
* The framework does not hold hold locks across calls to the
* mac perimeter, hence enter the perimeter first. This also waits
* for the property loading to finish.
*/
if ((err = mac_perim_enter_by_linkid(id1, &mph)) != 0) {
softmac_rele_device(ddh);
return (err);
}
i_dls_devnet_lock_enter();
i_dls_devnet_hashmap_read();
if ((err = mod_hash_find(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)id1, (mod_hash_val_t *)&ddp)) != 0) {
ASSERT(err == MH_ERR_NOTFOUND);
err = ENOENT;
goto done;
}
mutex_enter(&ddp->dd_mutex);
if (ddp->dd_ref > 1) {
mutex_exit(&ddp->dd_mutex);
err = EBUSY;
goto done;
}
mutex_exit(&ddp->dd_mutex);
if (id2 == DATALINK_INVALID_LINKID) {
(void) strlcpy(ddp->dd_linkname, link,
sizeof (ddp->dd_linkname));
/* rename mac client name and its flow if exists */
if ((err = mac_open(ddp->dd_mac, &mh)) != 0)
goto done;
(void) mac_rename_primary(mh, link);
mac_close(mh);
goto done;
}
/*
* The second case, check whether the MAC is used by any MAC
* user. This must be a physical link so ddh must not be NULL.
*/
if (ddh == NULL) {
err = EINVAL;
goto done;
}
if ((err = mac_open(ddp->dd_mac, &mh)) != 0)
goto done;
/*
* We release the reference of the MAC which mac_open() is
* holding. Note that this mac will not be unregistered
* because the physical device is held.
*/
mac_close(mh);
/*
* Check if there is any other MAC clients, if not, hold this mac
* exclusively until we are done.
*/
if ((err = mac_mark_exclusive(mh)) != 0)
goto done;
/*
* Update the link's linkid.
*/
if ((err = mod_hash_find(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)id2, &val)) != MH_ERR_NOTFOUND) {
mac_unmark_exclusive(mh);
err = EEXIST;
goto done;
}
/*
* Temporarily drop the hashmap lock for the upcall -- ddp will remain
* valid because we hold i_dls_devnet_lock. Taking this is a
* prerequisite for dls_devnet_unset to proceed, and it is the only
* pathway through which ddp can be freed.
*/
i_dls_devnet_hashmap_exit();
i_dls_devnet_lock_upcall_start();
err = dls_mgmt_get_linkinfo(id2, ddp->dd_linkname, NULL, NULL, NULL);
i_dls_devnet_lock_upcall_end();
if (err != 0) {
mac_unmark_exclusive(mh);
goto done_rw_unlocked;
}
i_dls_devnet_hashmap_write();
(void) mod_hash_remove(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)id1, &val);
ddp->dd_linkid = id2;
(void) mod_hash_insert(i_dls_devnet_id_hash,
(mod_hash_key_t)(uintptr_t)ddp->dd_linkid, (mod_hash_val_t)ddp);
mac_unmark_exclusive(mh);
/* load properties for new id */
mutex_enter(&ddp->dd_mutex);
ddp->dd_prop_loaded = B_FALSE;
ddp->dd_prop_taskid = taskq_dispatch(system_taskq,
dls_devnet_prop_task, ddp, TQ_SLEEP);
mutex_exit(&ddp->dd_mutex);
done:
i_dls_devnet_hashmap_exit();
done_rw_unlocked:
i_dls_devnet_lock_exit();
if (err == 0)
dls_devnet_stat_rename(ddp);
if (mph != NULL)
mac_perim_exit(mph);
softmac_rele_device(ddh);
return (err);
}
static int
i_dls_devnet_setzid(dls_devnet_t *ddp, zoneid_t new_zoneid, boolean_t setprop,
boolean_t transient)
{
int err;
mac_perim_handle_t mph;
boolean_t upcall_done = B_FALSE;
datalink_id_t linkid = ddp->dd_linkid;
zoneid_t old_zoneid = ddp->dd_zid;
dlmgmt_door_setzoneid_t setzid;
dlmgmt_setzoneid_retval_t retval;
if (old_zoneid == new_zoneid)
return (0);
if ((err = mac_perim_enter_by_macname(ddp->dd_mac, &mph)) != 0)
return (err);
/*
* When changing the zoneid of an existing link, we need to tell
* dlmgmtd about it. dlmgmtd already knows the zoneid associated with
* newly created links.
*/
if (setprop) {
setzid.ld_cmd = DLMGMT_CMD_SETZONEID;
setzid.ld_linkid = linkid;
setzid.ld_zoneid = new_zoneid;
err = i_dls_mgmt_upcall(&setzid, sizeof (setzid), &retval,
sizeof (retval));
if (err != 0)
goto done;
/*
* We set upcall_done only if the upcall is
* successful. This way, if dls_link_setzid() fails,
* we know another upcall must be done to reset the
* dlmgmtd state.
*/
upcall_done = B_TRUE;
}
if ((err = dls_link_setzid(ddp->dd_mac, new_zoneid)) == 0) {
ddp->dd_zid = new_zoneid;
ddp->dd_transient = transient;
devnet_need_rebuild = B_TRUE;
}
done:
if (err != 0 && upcall_done) {
setzid.ld_zoneid = old_zoneid;
(void) i_dls_mgmt_upcall(&setzid, sizeof (setzid), &retval,
sizeof (retval));
}
mac_perim_exit(mph);
return (err);
}
int
dls_devnet_setzid(dls_dl_handle_t ddh, zoneid_t new_zid)
{
dls_devnet_t *ddp;
int err;
zoneid_t old_zid;
boolean_t refheld = B_FALSE;
old_zid = ddh->dd_zid;
if (old_zid == new_zid)
return (0);
/*
* Acquire an additional reference to the link if it is being assigned
* to a non-global zone from the global zone.
*/
if (old_zid == GLOBAL_ZONEID && new_zid != GLOBAL_ZONEID) {
if ((err = dls_devnet_hold(ddh->dd_linkid, &ddp)) != 0)
return (err);
refheld = B_TRUE;
}
if ((err = i_dls_devnet_setzid(ddh, new_zid, B_TRUE, B_FALSE)) != 0) {
if (refheld)
dls_devnet_rele(ddp);
return (err);
}
/*
* Release the additional reference if the link is returning to the
* global zone from a non-global zone.
*/
if (old_zid != GLOBAL_ZONEID && new_zid == GLOBAL_ZONEID)
dls_devnet_rele(ddh);
/* Re-create kstats in the appropriate zones. */
if (old_zid != GLOBAL_ZONEID)
dls_devnet_stat_destroy(ddh, old_zid);
if (new_zid != GLOBAL_ZONEID)
dls_devnet_stat_create(ddh, new_zid);
return (0);
}
zoneid_t
dls_devnet_getzid(dls_dl_handle_t ddh)
{
return (((dls_devnet_t *)ddh)->dd_zid);
}
zoneid_t
dls_devnet_getownerzid(dls_dl_handle_t ddh)
{
return (((dls_devnet_t *)ddh)->dd_owner_zid);
}
/*
* Is linkid visible from zoneid? A link is visible if it was created in the
* zone, or if it is currently assigned to the zone.
*/
boolean_t
dls_devnet_islinkvisible(datalink_id_t linkid, zoneid_t zoneid)
{
dls_devnet_t *ddp;
boolean_t result;
if (dls_devnet_hold_tmp(linkid, &ddp) != 0)
return (B_FALSE);
result = (ddp->dd_owner_zid == zoneid || ddp->dd_zid == zoneid);
dls_devnet_rele_tmp(ddp);
return (result);
}
/*
* Access a vanity naming node.
*/
int
dls_devnet_open(const char *link, dls_dl_handle_t *dhp, dev_t *devp)
{
dls_devnet_t *ddp;
dls_link_t *dlp;
zoneid_t zid = getzoneid();
int err;
mac_perim_handle_t mph;
if ((err = dls_devnet_hold_by_name(link, &ddp)) != 0)
return (err);
dls_devnet_prop_task_wait(ddp);
/*
* Opening a link that does not belong to the current non-global zone
* is not allowed.
*/
if (zid != GLOBAL_ZONEID && ddp->dd_zid != zid) {
dls_devnet_rele(ddp);
return (ENOENT);
}
err = mac_perim_enter_by_macname(ddp->dd_mac, &mph);
if (err != 0) {
dls_devnet_rele(ddp);
return (err);
}
err = dls_link_hold_create(ddp->dd_mac, &dlp);
mac_perim_exit(mph);
if (err != 0) {
dls_devnet_rele(ddp);
return (err);
}
*dhp = ddp;
*devp = dls_link_dev(dlp);
return (0);
}
/*
* Close access to a vanity naming node.
*/
void
dls_devnet_close(dls_dl_handle_t dlh)
{
dls_devnet_t *ddp = dlh;
dls_link_t *dlp;
mac_perim_handle_t mph;
VERIFY(mac_perim_enter_by_macname(ddp->dd_mac, &mph) == 0);
VERIFY(dls_link_hold(ddp->dd_mac, &dlp) == 0);
/*
* One rele for the hold placed in dls_devnet_open, another for
* the hold done just above
*/
dls_link_rele(dlp);
dls_link_rele(dlp);
mac_perim_exit(mph);
dls_devnet_rele(ddp);
}
/*
* This is used by /dev/net to rebuild the nodes for readdir(). It is not
* critical and no protection is needed.
*/
boolean_t
dls_devnet_rebuild()
{
boolean_t updated = devnet_need_rebuild;
devnet_need_rebuild = B_FALSE;
return (updated);
}
int
dls_devnet_create(mac_handle_t mh, datalink_id_t linkid, zoneid_t zoneid)
{
dls_link_t *dlp;
dls_devnet_t *ddp;
int err;
mac_perim_handle_t mph;
/*
* Holding the mac perimeter ensures that the downcall from the
* dlmgmt daemon which does the property loading does not proceed
* until we relinquish the perimeter.
*/
mac_perim_enter_by_mh(mh, &mph);
/*
* Make this association before we call dls_link_hold_create as
* we need to use the linkid to get the user name for the link
* when we create the MAC client.
*/
if ((err = dls_devnet_set(mh, linkid, zoneid, &ddp)) == 0) {
if ((err = dls_link_hold_create(mac_name(mh), &dlp)) != 0) {
mac_perim_exit(mph);
(void) dls_devnet_unset(mh, &linkid, B_FALSE);
return (err);
}
/*
* If dd_linkid is set then the link was successfully
* initialized. In this case we can remove the
* initializing flag and make the link visible to the
* rest of the system.
*
* If not set then we were called by softmac and it
* was unable to obtain a linkid for the physical link
* because dlmgmtd is down. In that case softmac will
* eventually obtain a linkid and call
* dls_devnet_recreate() to complete initialization.
*/
mutex_enter(&ddp->dd_mutex);
if (ddp->dd_linkid != DATALINK_INVALID_LINKID)
ddp->dd_flags &= ~DD_INITIALIZING;
mutex_exit(&ddp->dd_mutex);
}
mac_perim_exit(mph);
return (err);
}
/*
* Set the linkid of the dls_devnet_t and add it into the i_dls_devnet_id_hash.
* This is called in the case that the dlmgmtd daemon is started later than
* the physical devices get attached, and the linkid is only known after the
* daemon starts.
*/
int
dls_devnet_recreate(mac_handle_t mh, datalink_id_t linkid)
{
dls_devnet_t *ddp;
int err;
VERIFY(linkid != DATALINK_INVALID_LINKID);
if ((err = dls_devnet_set(mh, linkid, GLOBAL_ZONEID, &ddp)) == 0) {
mutex_enter(&ddp->dd_mutex);
if (ddp->dd_linkid != DATALINK_INVALID_LINKID)
ddp->dd_flags &= ~DD_INITIALIZING;
mutex_exit(&ddp->dd_mutex);
}
return (err);
}
int
dls_devnet_destroy(mac_handle_t mh, datalink_id_t *idp, boolean_t wait)
{
int err;
mac_perim_handle_t mph;
*idp = DATALINK_INVALID_LINKID;
err = dls_devnet_unset(mh, idp, wait);
/*
* We continue on in the face of ENOENT because the devnet
* unset and DLS link release are not atomic and we may have a
* scenario where there is no entry in i_dls_devnet_hash for
* the MAC name but there is an entry in i_dls_link_hash. For
* example, if the following occurred:
*
* 1. dls_devnet_unset() returns success, and
*
* 2. dls_link_rele_by_name() fails with ENOTEMPTY because
* flows still exist, and
*
* 3. dls_devnet_set() fails to set the zone id and calls
* dls_devnet_unset() -- leaving an entry in
* i_dls_link_hash but no corresponding entry in
* i_dls_devnet_hash.
*
* Even if #3 wasn't true the dls_devnet_set() may fail for
* different reasons in the future; the point is that it _can_
* fail as part of its contract. We can't rely on it working
* so we must assume that these two pieces of state (devnet
* and link hashes), which should always be in sync, can get
* out of sync and thus even if we get ENOENT from the devnet
* hash we should still try to delete from the link hash just
* in case.
*
* We could prevent the ENOTEMPTY from dls_link_rele_by_name()
* by calling mac_disable() before calling
* dls_devnet_destroy() but that's not currently possible due
* to a long-standing bug. OpenSolaris 6791335: The semantics
* of mac_disable() were modified by Crossbow such that
* dls_devnet_destroy() needs to be called before
* mac_disable() can succeed. This is because of the implicit
* reference that dls has on the mac_impl_t.
*/
if (err != 0 && err != ENOENT)
return (err);
mac_perim_enter_by_mh(mh, &mph);
err = dls_link_rele_by_name(mac_name(mh));
mac_perim_exit(mph);
if (err != 0) {
dls_devnet_t *ddp;
/*
* XXX It is a general GLDv3 bug that dls_devnet_set() has to
* be called to re-set the link when destroy fails. The
* zoneid below will be incorrect if this function is ever
* called from kernel context or from a zone other than that
* which initially created the link.
*/
(void) dls_devnet_set(mh, *idp, crgetzoneid(CRED()), &ddp);
/*
* You might think dd_linkid should always be set
* here, but in the case where dls_devnet_unset()
* returns ENOENT it will be DATALINK_INVALID_LINKID.
* Stay consistent with the rest of DLS and only
* remove the initializing flag if linkid is set.
*/
mutex_enter(&ddp->dd_mutex);
if (ddp->dd_linkid != DATALINK_INVALID_LINKID)
ddp->dd_flags &= ~DD_INITIALIZING;
mutex_exit(&ddp->dd_mutex);
}
return (err);
}
/*
* Implicitly create an IP tunnel link.
*/
static int
i_dls_devnet_create_iptun(const char *linkname, const char *drvname,
datalink_id_t *linkid)
{
int err;
iptun_kparams_t ik;
uint32_t media;
netstack_t *ns;
major_t iptun_major;
dev_info_t *iptun_dip;
/* First ensure that the iptun device is attached. */
if ((iptun_major = ddi_name_to_major(IPTUN_DRIVER_NAME)) == (major_t)-1)
return (EINVAL);
if ((iptun_dip = ddi_hold_devi_by_instance(iptun_major, 0, 0)) == NULL)
return (EINVAL);
if (IS_IPV4_TUN(drvname)) {
ik.iptun_kparam_type = IPTUN_TYPE_IPV4;
media = DL_IPV4;
} else if (IS_6TO4_TUN(drvname)) {
ik.iptun_kparam_type = IPTUN_TYPE_6TO4;
media = DL_6TO4;
} else if (IS_IPV6_TUN(drvname)) {
ik.iptun_kparam_type = IPTUN_TYPE_IPV6;
media = DL_IPV6;
}
ik.iptun_kparam_flags = (IPTUN_KPARAM_TYPE | IPTUN_KPARAM_IMPLICIT);
/* Obtain a datalink id for this tunnel. */
err = dls_mgmt_create((char *)linkname, 0, DATALINK_CLASS_IPTUN, media,
B_FALSE, &ik.iptun_kparam_linkid);
if (err != 0) {
ddi_release_devi(iptun_dip);
return (err);
}
ns = netstack_get_current();
err = iptun_create(&ik, CRED());
netstack_rele(ns);
if (err != 0)
VERIFY(dls_mgmt_destroy(ik.iptun_kparam_linkid, B_FALSE) == 0);
else
*linkid = ik.iptun_kparam_linkid;
ddi_release_devi(iptun_dip);
return (err);
}
static int
i_dls_devnet_destroy_iptun(datalink_id_t linkid)
{
int err;
/*
* Note the use of zone_kcred() here as opposed to CRED(). This is
* because the process that does the last close of this /dev/net node
* may not have necessary privileges to delete this IP tunnel, but the
* tunnel must always be implicitly deleted on last close.
*/
if ((err = iptun_delete(linkid, zone_kcred())) == 0)
(void) dls_mgmt_destroy(linkid, B_FALSE);
return (err);
}
const char *
dls_devnet_link(dls_dl_handle_t ddh)
{
return (ddh->dd_linkname);
}
const char *
dls_devnet_mac(dls_dl_handle_t ddh)
{
return (ddh->dd_mac);
}
datalink_id_t
dls_devnet_linkid(dls_dl_handle_t ddh)
{
return (ddh->dd_linkid);
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Data-Link Services Module
*/
#include <sys/modctl.h>
#include <sys/dld_impl.h>
static struct modlmisc i_dls_modlmisc = {
&mod_miscops,
DLS_INFO
};
static struct modlinkage i_dls_modlinkage = {
MODREV_1,
&i_dls_modlmisc,
NULL
};
/*
* Module initialization functions.
*/
static void
i_dls_mod_init(void)
{
dls_link_init();
dls_mgmt_init();
}
static int
i_dls_mod_fini(void)
{
int err;
if ((err = dls_link_fini()) != 0)
return (err);
dls_mgmt_fini();
return (0);
}
/*
* modlinkage functions.
*/
int
_init(void)
{
int err;
i_dls_mod_init();
if ((err = mod_install(&i_dls_modlinkage)) != 0) {
(void) i_dls_mod_fini();
return (err);
}
#ifdef DEBUG
cmn_err(CE_NOTE, "!%s loaded", DLS_INFO);
#endif /* DEBUG */
return (0);
}
int
_fini(void)
{
int err;
if ((err = i_dls_mod_fini()) != 0)
return (err);
if ((err = mod_remove(&i_dls_modlinkage)) != 0)
return (err);
#ifdef DEBUG
cmn_err(CE_NOTE, "!%s unloaded", DLS_INFO);
#endif /* DEBUG */
return (0);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&i_dls_modlinkage, modinfop));
}
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Data-Link Services Module
*/
#include <sys/dld_impl.h>
#include <sys/mac_ether.h>
static mac_stat_info_t i_dls_si[] = {
{ MAC_STAT_IFSPEED, "ifspeed", KSTAT_DATA_UINT64, 0 },
{ MAC_STAT_MULTIRCV, "multircv", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_BRDCSTRCV, "brdcstrcv", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_MULTIXMT, "multixmt", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_BRDCSTXMT, "brdcstxmt", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_NORCVBUF, "norcvbuf", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_IERRORS, "ierrors", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_NOXMTBUF, "noxmtbuf", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_OERRORS, "oerrors", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_COLLISIONS, "collisions", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_RBYTES, "rbytes", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_IPACKETS, "ipackets", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_OBYTES, "obytes", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_OPACKETS, "opackets", KSTAT_DATA_UINT32, 0 },
{ MAC_STAT_RBYTES, "rbytes64", KSTAT_DATA_UINT64, 0 },
{ MAC_STAT_IPACKETS, "ipackets64", KSTAT_DATA_UINT64, 0 },
{ MAC_STAT_OBYTES, "obytes64", KSTAT_DATA_UINT64, 0 },
{ MAC_STAT_OPACKETS, "opackets64", KSTAT_DATA_UINT64, 0 },
{ MAC_STAT_LINK_STATE, "link_state", KSTAT_DATA_UINT32,
(uint64_t)LINK_STATE_UNKNOWN}
};
#define STAT_INFO_COUNT (sizeof (i_dls_si) / sizeof (i_dls_si[0]))
/*
* Exported functions.
*/
int
dls_stat_update(kstat_t *ksp, dls_link_t *dlp, int rw)
{
kstat_named_t *knp;
uint_t i;
uint64_t val;
if (rw != KSTAT_READ)
return (EACCES);
knp = (kstat_named_t *)ksp->ks_data;
for (i = 0; i < STAT_INFO_COUNT; i++) {
val = mac_stat_get(dlp->dl_mh, i_dls_si[i].msi_stat);
switch (i_dls_si[i].msi_type) {
case KSTAT_DATA_UINT64:
knp->value.ui64 = val;
break;
case KSTAT_DATA_UINT32:
knp->value.ui32 = (uint32_t)val;
break;
default:
ASSERT(B_FALSE);
}
knp++;
}
/*
* Ethernet specific kstat "link_duplex"
*/
if (dlp->dl_mip->mi_nativemedia != DL_ETHER) {
knp->value.ui32 = LINK_DUPLEX_UNKNOWN;
} else {
val = mac_stat_get(dlp->dl_mh, ETHER_STAT_LINK_DUPLEX);
knp->value.ui32 = (uint32_t)val;
}
knp++;
knp->value.ui32 = dlp->dl_unknowns;
return (0);
}
int
dls_stat_create(const char *module, int instance, const char *name,
zoneid_t zoneid, int (*update)(struct kstat *, int), void *private,
kstat_t **kspp)
{
kstat_t *ksp;
kstat_named_t *knp;
uint_t i;
if ((ksp = kstat_create_zone(module, instance, name, "net",
KSTAT_TYPE_NAMED, STAT_INFO_COUNT + 2, 0, zoneid)) == NULL) {
return (EINVAL);
}
ksp->ks_update = update;
ksp->ks_private = private;
knp = (kstat_named_t *)ksp->ks_data;
for (i = 0; i < STAT_INFO_COUNT; i++) {
kstat_named_init(knp, i_dls_si[i].msi_name,
i_dls_si[i].msi_type);
knp++;
}
kstat_named_init(knp++, "link_duplex", KSTAT_DATA_UINT32);
kstat_named_init(knp, "unknowns", KSTAT_DATA_UINT32);
kstat_install(ksp);
*kspp = ksp;
return (0);
}
|